diff --git a/TODO b/TODO index 9d265a9..a1a02ef 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,7 @@ - implement chain-reuse for output +- move shared bufs-related code to a separate .c + - get rid of greedy send_chain - remove macros hell from ngx_rtmp_send.c diff --git a/doc/flv.html b/doc/flv.html new file mode 100644 index 0000000..f15feca --- /dev/null +++ b/doc/flv.html @@ -0,0 +1,990 @@ + + + + + + flv [Open Source Flash] + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+
+ [[flv]] +
+ + +
+
+ + +
+
+
+ +
+
  +
+ +
+
+ + + + +
+ + +
+ + + + + +

Flash Video (FLV)

+
+ +

+ +Flash Video is the name of a file format used to deliver video over the Internet using Adobe Flash Player version 6 or newer. Flash Video content may also be embedded within SWF files. Until version 9 update 3 of the Flash Player, Flash Video referred to a proprietary file format, having the extension .FLV but Adobe introduced new file extensions and MIME types and suggests to use those instead of the old FLV: + +

+
+ + + + + + + + + + + + + + + + + + +
File Extension FTYP MIME Type Description
.f4v 'F4V ' video/mp4 Video for Adobe Flash Player
.f4p 'F4P ' video/mp4 Protected Media for Adobe Flash Player
.f4a 'F4A ' video/mp4 Audio for Adobe Flash Player
.f4b 'F4B ' video/mp4 Audio Book for Adobe Flash Player
.flv video/x-flv Flash Video
+ +

+ +It is possible to place H.264 and AAC streams into the traditional FLV file, but Adobe strongly encourages everyone to embrace the new standard file format. There are functional limits with the FLV structure when streaming H.264 which couldn't be overcome without a redesign of the file format. This is one of the reasons Adobe is moving away from the traditional FLV file structure. Specifically dealing with sequence headers and enders is tricky with FLV streams. Adobe is still working out if it's possible to place On2 VP6 streams into the new file format. +

+ +
+
+

Overview

+
+
    +
  • File format parser implementing parts of ISO 14496-12 (very limited sub set of MPEG-4, 3GP and QuickTime movie support).
    +
  • +
  • Support for the 3GPP timed text specification 3GPP TS 26.245. Essentially this is a standardized subtitle format within 3GP files. Any number of text tracks are supported and all the information, including esoteric stuff like karaoke meta data is dumped in 'onMetaData' and a new 'onTextData' NetStream callback. Language information in the individual tracks is also reported. That means you can have sub titles in several languages. Check the 3GPP TS 26.245 specification to see what information is available. Note that you have to take care of the formatting and placement of the text yourself, the Flash Player will do nothing here. You can use MP4Box to inject text data into existing files.
    +
  • +
  • Partial parsing support for the 'ilst' atom which is the ID3 equivalent iTunes uses to store meta data. This is usually present in iTunes files. It contains ID3 like information and is reported in the onMetaData callback as key/value pairs in a mixed array with the name 'tags'. ID3V2 is not supported right now.
    +
  • +
  • A software based H.264 codec with the ability to decode Base, Mainline and High profiles.
    +
  • +
  • An AAC decoder supporting AAC Main, AAC LC and SBR (also known as HE-AAC 1).
    +
  • +
+ +
+
+

Issues

+
+ +

+Tools to solve FLV-related issues: + +

+ + +
+
+

Video

+
+ +
+ +

Overview

+
+ +

+You load and play .mp4,.m4v,.m4a,.mov and .3gp files using the same NetStream API you use to load FLV files. There are a few things to be aware of: +

+
    +
  • Video needs to be in H.264 format only. MPEG-4 Part 2 (Xvid, DivX etc.) video is not supported, H.263 video is not supported, Sorenson Video is not supported. A lot of pod casts are still using MPEG-4 Part 2 so do not be surprised if you do not see any video.
    +
  • +
  • the Flash Player is close to 100% compliant to the H.264 standard, all Base, Main, High and High 10 bit streams should play.
    +
  • +
  • Extended, High 4:2:2 and High 4:4:4 profiles are not officially supported at this time. They might or might not work depending on what features are used. There are no artificial lower limit on B-frames or any problems with B-pyramids like other players do.
    +
  • +
  • Since these files contain an index unlike old FLV files, the Flash Player provides a list of save seek points, e.g. times you can seek to without having the play head jump around. You'll get this information through the onMetaData callback in an array with the name 'seekpoints'. On the downside, some files are missing this information which also means that these files are not seekable at all! This is very different from the traditional FLV file format which is rather based on the notion of key frames to determine the seek points.
    +
  • +
+ +
+ +

Codecs

+
+
+ + + + + + + + + + + + + + + + + + + + + +
Codec Introduced in Flash Player version Introduced in Flash Lite version Container Formats ISO Specification Codec Id
Sorenson Spark 2) 6 3 FLV 2
Macromedia Screen Video 3) 6 - FLV 3
Macromedia ScreenVideo 2 4) 8 - FLV 6
On2 TrueMotion VP6-E 8 3 MOV 4
On2 TrueMotion VP6-S 9.0.115.0 - MP4V, M4V 5
H.264 (MPEG-4 Part 10) 9.0.115.0 - MP4, F4V, 3GP, 3G2 ISO 14496-10
+ +

+Adobe Tech Note +

+ +
+
+

Audio

+
+ +
+ +

Overview

+
+
    +
  • Audio can be either AAC Main, AAC LC or SBR, corresponding to audio object types 0, 1 and 2.
    +
  • +
  • The '.mp3' sample type for tracks with mp3 audio is also supported.
    +
  • +
  • MP3inMP4 which intends to do multi-channel mp3 playback within mp4 files is not supported.
    +
  • +
  • The old QuickTime specific style of embedding AAC and MP3 data is not supported. It is unlikely though that you will run into these kind of files.
    +
  • +
  • Unencrypted audio book files contain chapter information. This information is exposed in the onMetaData callback as an array of objects with name 'chapters'.
    +
  • +
  • The Flash Player can play back multi-channel AAC files, though the sound is mixed down to two channels and resampled to 44.1Khz. Multi channel playback is targeted for one of the next major revisions of the Flash Player. This requires complete redesign of the sound engine in the Flash Player which dates from circa 1996 and has not been improved since.
    +
  • +
  • All sampling rates from 8Khz to 96Khz are supported. A 32 tap Kaiser Bessel based FIR filter which resamples the sound to 44.1Khz, retaining high quality. The most common sample rate combinations have a hard coded number of phases. In case of a 48000 to 44100 Hz conversion the filter has 147 phases f.ex.
    +
  • +
  • Flash Player Update 3 Beta 2 now can play back any MP3 sampling rate leveraging the same AAC implementation. No more chipmunks. Ever.
    +
  • +
+ +
+ +

Codecs

+
+
+ + + + + + + + + + + + + + + + + + + + + +
Codec Introduced in Flash Player version Container Formats ISO Specification Codec Id
MP3 6 MP3 2
Nellymoser ASAO Codec (speech compression) audio content 6 FLV 5, 6
Raw PCM sampled audio content 6 WAV 0
ADPCM (Adaptive Delta Pulse Code Modulation) audio content 6 1
AAC (HE-AAC/AAC SBR, AAC Main Profile, and AAC-LC) 9.0.115.0 M4A, MP4 ISO 14496-3
Speex 10 FLV Wiki 11
+ +
+
+

Image

+
+
    +
  • Image tracks encoded in JPEG, GIF and PNG are accessible in AS3 as a byte array through the callback 'onImageData'. You can simply take that byte array and use the Loader class to display the images. Most often these images represent cover artwork for audio files.
    +
  • +
  • TIFF image tracks are not supported, you might come across files using this.
    +
  • +
  • Support for the 'covr' meta data stored in iTunes files, accessible as byte arrays.
    +
  • +
+ +
+
+

Metadata

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Property Value Notes
duration Obvious. Unlike for FLV files this field will always be present.
videocodecid For H.264 it reports 'avc1'.
audiocodecid For AAC it reports 'mp4a', for MP3 it reports '.mp3'.
avcprofile 66, 77, 88, 100, 110, 122 or 144 Corresponds to the H.264 profiles
avclevel A number between 10 and 51. Consult this list to find out more.
aottype Either 0, 1 or 2. This corresponds to AAC Main, AAC LC and SBR audio types.
moovposition int The offset in bytes of the moov atom in a file.
trackinfo Array An array of objects containing various infomation about all the tracks in a file.
chapters Array Information about chapters in audiobooks.
seekpoints Array Times you can directly feed into NetStream.seek();
videoframerate int The frame rate of the video if a monotone frame rate is used. Most videos will have a monotone frame rate.
audiosamplerate The original sampling rate of the audio track.
audiochannels The original number of channels of the audio track.
tags ID3 like tag information
+ +
+
+

FLV Format

+
+ +

+A Flash Video file (.FLV file extension) consists of a short header, and then interleaved audio, video, and metadata packets. The audio and video packets are stored very similarly to those in SWF, and the metadata packets consist of AMF data. +

+ +
+ +

FLV Header

+
+
+ + + + + + + + + + + + + + + +
Field Data Type Example Description
Signature byte[3] “FLV” Always “FLV”
Version uint8 “\x01” (1) Currently 1 for known FLV files
Flags uint8 bitmask “\x05” (5, audio+video) Bitmask: 4 is audio, 1 is video
Offset uint32_be “\x00\x00\x00\x09” (9) Total size of header (always 9 for known FLV files)
+ +
+ +

FLV Stream

+
+
+ + + + + + +
Field Data Type Example Description
PreviousTagSize uint32_be “\x00\x00\x00\x00” (0) Always 0
+ +

+ +Then a sequence of tags followed by their size until EOF. +

+ +
+ +

FLV Tag

+
+
+ + + + + + + + + + + + + + + + + + + + + +
Field Data Type Example Description
Type uint8 “\x12” (0x12, META) Determines the layout of Body, see below for tag types
BodyLength uint24_be “\x00\x00\xe0” (224) Size of Body (total tag size - 11)
Timestamp uint24_be “\x00\x00\x00” (0) Timestamp of tag (in milliseconds)
TimestampExtended uint8 “\x00” (0) Timestamp extension to form a uint32_be. This field has the upper 8 bits.
StreamId uint24_be “\x00\x00\x00” (0) Always 0
Body byte[BodyLength] Dependent on the value of Type
+ +
+ +

Previous tag size

+
+
+ + + + + + +
Field Data Type Example Description
PreviousTagSize uint32_be “\x00\x00\x00\x00” (0) Total size of previous tag, or 0 for first tag
+ +
+ +

FLV Tag Types

+
+
+ + + + + + + + + + + + +
Tag code Name Description
0x08 AUDIO Contains an audio packet similar to a SWF SoundStreamBlock plus codec information
0x09 VIDEO Contains a video packet similar to a SWF VideoFrame plus codec information
0x12 META Contains two AMF packets, the name of the event and the data to go with it
+ +
+ +

FLV Tag 0x08: AUDIO

+
+ +

+ +The first byte of an audio packet contains bitflags that +describe the codec used, with the following layout: + +

+
+ + + + + + + + + + + + + + + +
Name Expression Description
soundType (byte & 0x01) » 0 0: mono, 1: stereo
soundSize (byte & 0x02) » 1 0: 8-bit, 1: 16-bit
soundRate (byte & 0x0C) » 2 0: 5.5 kHz (or speex 16kHz), 1: 11 kHz, 2: 22 kHz, 3: 44 kHz
soundFormat (byte & 0xf0) » 4 0: Uncompressed, 1: ADPCM, 2: MP3, 5: Nellymoser 8kHz mono, 6: Nellymoser, 11: Speex
+ +

+ +The rest of the audio packet is simply the relevant data for that format, as per a SWF SoundStreamBlock. +

+ +
+ +

FLV Tag 0x09: VIDEO

+
+ +

+ +The first byte of a video packet describes contains bitflags +that describe the codec used, and the type of frame + +

+
+ + + + + + + + + +
Name Expression Description
codecID (byte & 0x0f) » 0 2: Sorensen H.263, 3: Screen video, 4: On2 VP6, 5: On2 VP6 Alpha, 6: ScreenVideo 2
frameType (byte & 0xf0) » 4 1: keyframe, 2: inter frame, 3: disposable inter frame
+ +

+ +In some cases it is also useful to decode some of the body of the video +packet, such as to acquire its resolution (if the initial onMetaData META +tag is missing, for example). +

+ +

+TODO: Describe the techniques for acquiring this information. Until then, you can +consult the flashticle sources. +

+ +
+ +

FLV Tag 0x12: META

+
+ +

+ +The contents of a meta packet are two AMF packets. The first is +almost always a short uint16_be length-prefixed UTF-8 string +(AMF type 0x02), and the second is typically a mixed array +(AMF type 0x08). However, the second chunk typically contains +a variety of types, so a full AMF parser should be used. +

+ +
+
+

HTTP Streaming

+
+ +

+ +It is possible to semi-stream flv over http using a trick which sends the normal headers then skips forward to a desired point in the file and moves the timestamps forward accordingly. +

+ +

+A sample php script and fla is available at FlashComGuru +

+ +

+Another tool that you can use to stream flv files using http is using Flv4PHP this tool is both a FLV Metadata injector and a stream tool, using php 4.x. this Project is GPL. + +

+ +
+
+
1) +The support of AAC allows you to encode audio to 64Kbit/s with the same quality of a 128Kbit/s encoded MP3. Further more, for other use more susceptible to bandwidth usage, like Internet Radio, HE-AAC v2 gives the possibility to encode audio to 32Kbit/s or lower with a surprisingly good final result. In low bitrate streaming scenarios this can make the difference.
+
2) +Flash documentation does not state a number for “their” version of Sorenson but describes the codec as a variant of ITU-T (International Telecommunications Union-Telecommunication Standardization Sector) recommendation H.263 (MPEG-4_V). In early 2006, one of Sorenson's compression applications to produce content for Flash offered the Sorenson_3 codec, described by experts as a variant of ITU-T H.264 (MPEG-4_AVC). By late 2006, Sorenson offered new compression applications with other outputs.
+
3) +This codec divides the screen in wide macroblocks (es: 64×64 pixels), reduces the number of colors, and transmits the changed blocks after compressing them in zlib. This is very similar to what VNC does.formats are bitmap tile based, can be lossy by reducing color depths and are compressed
+
4) +This codec can use two different types of macroblock: Iblock and Kblock. The Kblock works like a keyframe and is archived for future references. The Iblock is encoded as differences from a previous block. This new approach, similar to the usual compression of generic video content, guarantees a much better compression ratio, especially in a standard “moving windows” scenario.
+
+
+

+ Discussion +

+
+
+
+ + WattsArlene, 2011/01/11 01:54 +
+
+That's good that we can get the <a href="http://bestfinance-blog.com/topics/personal-loans">personal loans</a> moreover, this opens up completely new possibilities. +
+
+
+
+ + + + + +
+
+
+
+
+
+ + koisirinut, 2011/01/15 11:03 +
+
+**Bold Text** +
+
+
+
+ + + + + +
+
+
+
+
+
+ + muhammad basar, 2011/01/16 09:35 +
+
+http://bestfinance-blog.com/topics/personal-loans">personal loans</a> moreover, this opens up completely new possibilities. +
+
+
+
+ + + + + +
+
+
+
+
+
+ + Actarus, 2011/01/29 08:24 +
+
+Great documentation! Now, the RIFF format should be documented as well to complete this jewel. I have benchmarked the flv format using the wonderful getID3 library, it's here: http://tinyurl.com/flvdump, Enjoy. +
+
+
+
+ + + + + +
+
+
+
+
+
+ + khan, 2011/02/22 08:40 +
+
+**Bold Text** +
+
+
+
+ + + + + +
+
+
+
+
+
+ + mortgage loans, 2011/06/02 17:16 +
+
+Some time before, I really needed to buy a good car for my corporation but I did not have enough money and could not buy anything. Thank heaven my colleague suggested to try to take the credit loans at trustworthy creditors. Hence, I acted that and was satisfied with my consolidation loan. +
+
+
+
+ + + + + +
+
+
+
+
+
+ + loans, 2011/06/29 04:15 +
+
+The loan suppose to be important for people, which want to start their own organization. In fact, it's very comfortable to get a student loan. +
+
+
+
+ + + + + +
+
+
+
+
+
+ + home loans, 2011/07/13 23:30 +
+
+Every one acknowledges that men's life seems to be high priced, nevertheless people require cash for various issues and not every person gets big sums money. Hence to get fast mortgage loans and credit loan would be a correct solution. +
+
+
+
+ + + + + +
+
+
+
+
+
+ + johncein, 2011/07/16 02:06 +
+
+informative read... thanks for the source.



<a href="http://hdwebplayer.com">Flv Player</a> +
+
+
+
+ + + + + +
+
+
+
+
+
+ + valerie, 2011/08/27 02:00 +
+
+Eben pagan has released his new course guru blueprint and in this guru masterclass he teaches all that he has learn in his marketing career.
Check this website to know more about Eben pagan guru Master class..<a href=>]Eben pagan guru blueprint</a>
If you want to know more about the course you can follow the video on youtube about the guru blueprint..
<a href=http://www.youtube.com/watch?v=rm8Spt2_Zv0>Eben pagan guru blueprint</a>
<a href=http://www.youtube.com/watch?v=5MrCM4MnSqM>Eben pagan guru blueprint</a>
<a href=http://www.youtube.com/watch?v=ZEYhOkb9Wys>Eben pagan guru blueprint</a>

you can go here to find out more about the guru masterclass.. <a href=http://www.helpandinfo.com/guru-masterclass-free-training-video-three.html>Eben pagan guru blueprint</a>



You can learn much more about marketing by just listening to this guy talk. he is such an amazing personality. See as eben pagan releases his master class..
guru blueprint.. +
+
+
+
+ + + + + +
+
+
+
+
+
+ + loans, 2011/09/01 17:28 +
+
+People in the world take the mortgage loans from various creditors, because that is easy. +
+
+
+
+ + + + + +
+
+
+
+
+
+ + business loans, 2011/09/20 11:06 +
+
+This is known that money can make people independent. But how to act if someone doesn't have cash? The one way only is to get the mortgage loans or just credit loan. +
+
+
+
+ + + + + +
+
+
+
+
+
+ + confused, 2011/10/13 18:15 +
+
+WHAT THE HELL ARE DUMB ASS BOTS DOING ON HERE!!! NOBODY WANTS YOUR SCAMS OR ANY OTHER HORSE SHIT YOU CAN SHOVEL, GET A FUCKING LIFE. +
+
+
+
+ + + + + +
+
+
+
+
+
+ + gymnsmitift, 2011/11/22 18:38 +
+
+wow, great forum! <a href=https://wiki.citizen.apps.gov/NEA_OPI/index.php/About_Antennadeals.com>Antennadeals.com</a>? +
+
+
+
+ + + + + +
+
+
+
+
+
+ + Postupalka, 2011/12/06 15:38 +
+
+Прокси-сервер работает на 8080 <a href="http://avtomast.com/ceni_na_toplivo-3.html">http://avtomast.com/</a> каневской чат +
+
+
+
+ + + + + +
+
+
+
+
+
+ + Ali, 2012/01/02 07:38 +
+
+**Bold Text**Hello
I am working on a project in Flash which the user can draw some animation and then covert the final product as an FLV file so he can share it on facebook and youtube,
I have searched for a suitable solution but yet can’t find any, in go Animat (www.goAnimat.com) engine covert to FLV very smoothly,
So is it possible to guide me on how to do this, or maybe provide my with this service on servers.
I would really appreciate you help on this. +
+
+
+
+ + + + + +
+
+
+
+ +
+
+
+ + + + + +
+ +
+
+ +
+
+
+ +
+
Проверка по слову reCAPTCHA

Обновить
Звуковая проверкаВизуальная проверка
Справка
+ + + + +
+ + +
+ +
+
 
+
+
+
+
+
+ + +
+ +
 
+ + +
+ +
+
+
+
+ flv.txt · Last modified: 2011/12/02 01:16 by 219.161.44.239
+
+ + +
+
+
+
+
  +
+
+
+ +
+ + +
+ +
+ + Recent changes RSS feed + + + Donate + + Powered by PHP + + Valid XHTML 1.0 + + Valid CSS + + Driven by DokuWiki + + + +
+ +
+ + +
\ No newline at end of file diff --git a/ngx_rtmp.c b/ngx_rtmp.c index 1a1d256..9f489c9 100644 --- a/ngx_rtmp.c +++ b/ngx_rtmp.c @@ -272,7 +272,7 @@ ngx_rtmp_init_events(ngx_conf_t *cf, ngx_rtmp_core_main_conf_t *cmcf) } } - if (ngx_array_init(&cmcf->calls, cf->pool, 1, + if (ngx_array_init(&cmcf->amf0, cf->pool, 1, sizeof(ngx_hash_key_t)) != NGX_OK) { return NGX_ERROR; @@ -288,6 +288,42 @@ ngx_rtmp_init_events(ngx_conf_t *cf, ngx_rtmp_core_main_conf_t *cmcf) return NGX_OK; } +/* +static ngx_int_t +ngx_rtmp_init_amf0_handler(ngx_rtmp_core_main_conf_t *cmcf, + ngx_int_t id, ngx_array_t *array, ngx_hash_t *hash, + ngx_rtmp_event_handler_pt handler) +{ + ngx_hash_init_t calls_hash; + ngx_rtmp_event_handler_pt *eh; + ngx_hash_key_t *h; + size_t n; + + eh = ngx_array_push(&cmcf->events[id]); + *eh = handler; + + h = array->elts; + for(n = 0; n < array->nelts; ++n, ++h) { + h->key_hash = ngx_hash_key_lc(h->key.data, h->key.len); + } + + calls_hash.hash = hash; + calls_hash.key = ngx_hash_key_lc; + calls_hash.max_size = 512; + calls_hash.bucket_size = ngx_cacheline_size; + calls_hash.name = "amf0_hash"; + calls_hash.pool = cf->pool; + calls_hash.temp_pool = NULL; + + if (ngx_hash_init(&calls_hash, array->elts, array->nelts) + != NGX_OK) + { + return NGX_ERROR; + } + + return NGX_OK; +} +*/ static ngx_int_t ngx_rtmp_init_event_handlers(ngx_conf_t *cf, ngx_rtmp_core_main_conf_t *cmcf) @@ -296,6 +332,7 @@ ngx_rtmp_init_event_handlers(ngx_conf_t *cf, ngx_rtmp_core_main_conf_t *cmcf) ngx_rtmp_event_handler_pt *eh; ngx_hash_key_t *h; size_t n; + static size_t pm_events[] = { NGX_RTMP_MSG_CHUNK_SIZE, NGX_RTMP_MSG_ABORT, @@ -303,6 +340,11 @@ ngx_rtmp_init_event_handlers(ngx_conf_t *cf, ngx_rtmp_core_main_conf_t *cmcf) NGX_RTMP_MSG_ACK_SIZE, NGX_RTMP_MSG_BANDWIDTH }; + static size_t amf0_events[] = { + NGX_RTMP_MSG_AMF0_META, + NGX_RTMP_MSG_AMF0_SHARED, + NGX_RTMP_MSG_AMF0_CMD + }; /* init events */ for(n = 0; n < sizeof(pm_events) / sizeof(pm_events[0]); ++n) { @@ -310,19 +352,20 @@ ngx_rtmp_init_event_handlers(ngx_conf_t *cf, ngx_rtmp_core_main_conf_t *cmcf) *eh = ngx_rtmp_protocol_message_handler; } + for(n = 0; n < sizeof(amf0_events) / sizeof(amf0_events[0]); ++n) { + eh = ngx_array_push(&cmcf->events[amf0_events[n]]); + *eh = ngx_rtmp_amf0_message_handler; + } + eh = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_USER]); *eh = ngx_rtmp_user_message_handler; - eh = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_AMF0_CMD]); - *eh = ngx_rtmp_amf0_message_handler; - - /* init calls */ - h = cmcf->calls.elts; - for(n = 0; n < cmcf->calls.nelts; ++n, ++h) { + h = cmcf->amf0.elts; + for(n = 0; n < cmcf->amf0.nelts; ++n, ++h) { h->key_hash = ngx_hash_key_lc(h->key.data, h->key.len); } - calls_hash.hash = &cmcf->calls_hash; + calls_hash.hash = &cmcf->amf0_hash; calls_hash.key = ngx_hash_key_lc; calls_hash.max_size = 512; calls_hash.bucket_size = ngx_cacheline_size; @@ -330,7 +373,7 @@ ngx_rtmp_init_event_handlers(ngx_conf_t *cf, ngx_rtmp_core_main_conf_t *cmcf) calls_hash.pool = cf->pool; calls_hash.temp_pool = NULL; - if (ngx_hash_init(&calls_hash, cmcf->calls.elts, cmcf->calls.nelts) + if (ngx_hash_init(&calls_hash, cmcf->amf0.elts, cmcf->amf0.nelts) != NGX_OK) { return NGX_ERROR; diff --git a/ngx_rtmp.h b/ngx_rtmp.h index a453274..bf7f465 100644 --- a/ngx_rtmp.h +++ b/ngx_rtmp.h @@ -158,7 +158,7 @@ typedef struct { } ngx_rtmp_header_t; -typedef struct ngx_rtmp_stream_t { +typedef struct { ngx_rtmp_header_t hdr; uint32_t len; /* current fragment length */ ngx_chain_t *in; @@ -201,8 +201,6 @@ typedef struct ngx_rtmp_session_s { typedef ngx_int_t (*ngx_rtmp_event_handler_pt)(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in); -typedef ngx_int_t (*ngx_rtmp_call_handler_pt)(ngx_rtmp_session_t *s, - double trans, ngx_chain_t *in); typedef ngx_int_t (*ngx_rtmp_disconnect_handler_pt)(ngx_rtmp_session_t *s); @@ -211,8 +209,10 @@ typedef struct { ngx_array_t listen; /* ngx_rtmp_listen_t */ ngx_array_t events[NGX_RTMP_MSG_MAX]; - ngx_hash_t calls_hash; - ngx_array_t calls; + + ngx_hash_t amf0_hash; + ngx_array_t amf0; + ngx_array_t disconnect; } ngx_rtmp_core_main_conf_t; @@ -275,6 +275,11 @@ typedef struct { ((ngx_rtmp_conf_ctx_t *) cf->ctx)->srv_conf[module.ctx_index] +#ifdef NGX_DEBUG +char* ngx_rtmp_message_type(uint8_t type); +char* ngx_rtmp_user_message_type(uint16_t evt); +#endif + void ngx_rtmp_init_connection(ngx_connection_t *c); void ngx_rtmp_close_connection(ngx_connection_t *c); u_char * ngx_rtmp_log_error(ngx_log_t *log, u_char *buf, size_t len); @@ -291,10 +296,14 @@ ngx_int_t ngx_rtmp_amf0_message_handler(ngx_rtmp_session_t *s, /* Sending messages */ ngx_chain_t * ngx_rtmp_alloc_shared_buf(ngx_rtmp_session_t *s); -ngx_int_t ngx_rtmp_release_shared_buf(ngx_rtmp_session_t *s, +ngx_chain_t * ngx_rtmp_append_shared_bufs(ngx_rtmp_session_t *s, + ngx_chain_t *out, ngx_chain_t *in); +ngx_int_t ngx_rtmp_addref_shared_bufs(ngx_chain_t *in); +ngx_int_t ngx_rtmp_free_shared_buf(ngx_rtmp_session_t *s, ngx_chain_t *out); + void ngx_rtmp_prepare_message(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, - ngx_chain_t *out, uint8_t fmt); + ngx_rtmp_header_t *lh, ngx_chain_t *out); ngx_int_t ngx_rtmp_send_message(ngx_rtmp_session_t *s, ngx_chain_t *out); #define NGX_RTMP_LIMIT_SOFT 0 @@ -330,8 +339,7 @@ ngx_int_t ngx_rtmp_send_user_ping_response(ngx_rtmp_session_t *s, uint32_t timestamp); /* AMF0 sender/receiver */ -ngx_int_t ngx_rtmp_send_amf0(ngx_rtmp_session_t *s, - uint32_t csid, uint32_t msid, +ngx_int_t ngx_rtmp_send_amf0(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_rtmp_amf0_elt_t *elts, size_t nelts); ngx_int_t ngx_rtmp_receive_amf0(ngx_rtmp_session_t *s, ngx_chain_t *in, ngx_rtmp_amf0_elt_t *elts, size_t nelts); diff --git a/ngx_rtmp_amf0.c b/ngx_rtmp_amf0.c index 174264b..b5c632e 100644 --- a/ngx_rtmp_amf0.c +++ b/ngx_rtmp_amf0.c @@ -223,7 +223,8 @@ ngx_rtmp_amf0_read_object(ngx_rtmp_amf0_ctx_t *ctx, ngx_rtmp_amf0_elt_t *elts, #define NGX_RTMP_AMF0_TILL_END_FLAG ((size_t)1 << (sizeof(size_t) * 8 - 1)) ngx_int_t -ngx_rtmp_amf0_read(ngx_rtmp_amf0_ctx_t *ctx, ngx_rtmp_amf0_elt_t *elts, size_t nelts) +ngx_rtmp_amf0_read(ngx_rtmp_amf0_ctx_t *ctx, ngx_rtmp_amf0_elt_t *elts, + size_t nelts) { void *data; uint8_t type; diff --git a/ngx_rtmp_broadcast_module.c b/ngx_rtmp_broadcast_module.c index 3a4a0f0..f661024 100644 --- a/ngx_rtmp_broadcast_module.c +++ b/ngx_rtmp_broadcast_module.c @@ -14,20 +14,22 @@ static char * ngx_rtmp_broadcast_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child); static ngx_int_t ngx_rtmp_broadcast_connect(ngx_rtmp_session_t *s, - double in_trans, ngx_chain_t *in); + ngx_rtmp_header_t *h, ngx_chain_t *in); static ngx_int_t ngx_rtmp_broadcast_create_stream(ngx_rtmp_session_t *s, - double in_trans, ngx_chain_t *in); + ngx_rtmp_header_t *h, ngx_chain_t *in); static ngx_int_t ngx_rtmp_broadcast_publish(ngx_rtmp_session_t *s, - double in_trans, ngx_chain_t *in); + ngx_rtmp_header_t *h, ngx_chain_t *in); static ngx_int_t ngx_rtmp_broadcast_play(ngx_rtmp_session_t *s, - double in_trans, ngx_chain_t *in); + ngx_rtmp_header_t *h, ngx_chain_t *in); +static ngx_int_t ngx_rtmp_broadcast_set_data_frame(ngx_rtmp_session_t *s, + ngx_rtmp_header_t *h, ngx_chain_t *in); static ngx_int_t ngx_rtmp_broadcast_ok(ngx_rtmp_session_t *s, - double in_trans, ngx_chain_t *in); + ngx_rtmp_header_t *h, ngx_chain_t *in); typedef struct { ngx_str_t name; - ngx_rtmp_call_handler_pt handler; + ngx_rtmp_event_handler_pt handler; } ngx_rtmp_broadcast_map_t; @@ -36,6 +38,7 @@ static ngx_rtmp_broadcast_map_t ngx_rtmp_broadcast_map[] = { { ngx_string("createStream"), ngx_rtmp_broadcast_create_stream }, { ngx_string("publish"), ngx_rtmp_broadcast_publish }, { ngx_string("play"), ngx_rtmp_broadcast_play }, + { ngx_string("@setDataFrame"), ngx_rtmp_broadcast_set_data_frame }, { ngx_string("releaseStream"), ngx_rtmp_broadcast_ok }, { ngx_string("FCPublish"), ngx_rtmp_broadcast_ok }, { ngx_string("FCSubscribe"), ngx_rtmp_broadcast_ok }, @@ -91,14 +94,17 @@ ngx_module_t ngx_rtmp_broadcast_module = { #define NGX_RTMP_BROADCAST_PUBLISHER 0x01 #define NGX_RTMP_BROADCAST_SUBSCRIBER 0x02 #define NGX_RTMP_BROADCAST_WANT_KEYFRAME 0x04 +#define NGX_RTMP_BROADCAST_DATA_FRAME 0x08 typedef struct ngx_rtmp_broadcast_ctx_s { ngx_str_t stream; ngx_rtmp_session_t *session; struct ngx_rtmp_broadcast_ctx_s *next; - ngx_uint_t flags; /* publisher/subscriber */ + ngx_uint_t flags; /* publisher/subscriber */ uint32_t csid; + ngx_rtmp_header_t lh; /* last a/v header */ + ngx_chain_t *data_frame; } ngx_rtmp_broadcast_ctx_t; @@ -242,20 +248,14 @@ ngx_rtmp_broadcast_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, { ngx_connection_t *c; ngx_rtmp_broadcast_ctx_t *ctx, *cctx; - ngx_chain_t *out, *l, **ll; - u_char *p; - size_t nsubs, size; + ngx_chain_t *out; + size_t nsubs; ngx_int_t vftype; c = s->connection; ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_broadcast_module); - /* - h->timestamp -= s->peer_epoch; - h->timestamp += s->epoch; - */ - if (ctx == NULL || !(ctx->flags & NGX_RTMP_BROADCAST_PUBLISHER)) { @@ -273,39 +273,9 @@ ngx_rtmp_broadcast_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, vftype = ngx_rtmp_get_video_frame_type(in); } - /* copy data to output stream */ - out = NULL; - ll = &out; - p = in->buf->pos; + out = ngx_rtmp_append_shared_bufs(s, NULL, in); - for ( ;; ) { - l = ngx_rtmp_alloc_shared_buf(s); - if (l == NULL || l->buf == NULL) { - return NGX_ERROR; - } - - *ll = l; - ll = &l->next; - - while (l->buf->end - l->buf->last >= in->buf->last - p) { - l->buf->last = ngx_cpymem(l->buf->last, p, - in->buf->last - p); - in = in->next; - if (in == NULL) { - goto done; - } - p = in->buf->pos; - } - - size = l->buf->end - l->buf->last; - l->buf->last = ngx_cpymem(l->buf->last, p, size); - p += size; - } - -done: - *ll = NULL; - - ngx_rtmp_prepare_message(s, h, out, 0/*fmt*/); + ngx_rtmp_prepare_message(s, h, &ctx->lh, out); /* broadcast to all subscribers */ nsubs = 0; @@ -318,6 +288,24 @@ done: && !ngx_strncmp(cctx->stream.data, ctx->stream.data, ctx->stream.len)) { + /* if we have metadata check if the subscriber + * has already received one */ + if (ctx->data_frame + && !(cctx->flags & NGX_RTMP_BROADCAST_DATA_FRAME)) + { + ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, + "sending data_frame"); + + if (ngx_rtmp_send_message(cctx->session, ctx->data_frame) + != NGX_OK) + { + return NGX_ERROR; + } + cctx->flags |= NGX_RTMP_BROADCAST_DATA_FRAME; + } + + /* is the subscriber waiting for + * a key frame? */ if (h->type == NGX_RTMP_MSG_VIDEO && cctx->flags & NGX_RTMP_BROADCAST_WANT_KEYFRAME) { @@ -326,6 +314,7 @@ done: } cctx->flags &= ~NGX_RTMP_BROADCAST_WANT_KEYFRAME; } + if (ngx_rtmp_send_message(cctx->session, out) != NGX_OK) { return NGX_ERROR; } @@ -333,9 +322,11 @@ done: } } + /* TODO: implement proper (refcount-based) buffer deletion */ + /* no one subscriber? */ if (!nsubs - && ngx_rtmp_release_shared_buf(s, out) != NGX_OK) + && ngx_rtmp_free_shared_buf(s, out) != NGX_OK) { return NGX_ERROR; } @@ -345,7 +336,7 @@ done: static ngx_int_t -ngx_rtmp_broadcast_connect(ngx_rtmp_session_t *s, double in_trans, +ngx_rtmp_broadcast_connect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in) { ngx_rtmp_core_srv_conf_t *cscf; @@ -369,6 +360,7 @@ ngx_rtmp_broadcast_connect(ngx_rtmp_session_t *s, double in_trans, }; static ngx_rtmp_amf0_elt_t in_elts[] = { + { NGX_RTMP_AMF0_NUMBER, 0, &trans, sizeof(trans) }, { NGX_RTMP_AMF0_OBJECT, NULL, in_cmd, sizeof(in_cmd) }, }; @@ -387,7 +379,6 @@ ngx_rtmp_broadcast_connect(ngx_rtmp_session_t *s, double in_trans, cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); - trans = in_trans; ngx_str_set(&out_inf[0], "NetConnection.Connect.Success"); ngx_str_set(&out_inf[1], "status"); ngx_str_set(&out_inf[2], "Connection succeeded."); @@ -409,7 +400,7 @@ ngx_rtmp_broadcast_connect(ngx_rtmp_session_t *s, double in_trans, return ngx_rtmp_send_ack_size(s, cscf->ack_window) || ngx_rtmp_send_bandwidth(s, cscf->ack_window, NGX_RTMP_LIMIT_SOFT) || ngx_rtmp_send_user_stream_begin(s, 0) - || ngx_rtmp_send_amf0(s, 3, 0, out_elts, + || ngx_rtmp_send_amf0(s, h, out_elts, sizeof(out_elts) / sizeof(out_elts[0])) ? NGX_ERROR : NGX_OK; @@ -417,12 +408,16 @@ ngx_rtmp_broadcast_connect(ngx_rtmp_session_t *s, double in_trans, static ngx_int_t -ngx_rtmp_broadcast_create_stream(ngx_rtmp_session_t *s, double in_trans, +ngx_rtmp_broadcast_create_stream(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in) { static double trans; static double stream; + static ngx_rtmp_amf0_elt_t in_elts[] = { + { NGX_RTMP_AMF0_NUMBER, 0, &trans, sizeof(trans) }, + }; + static ngx_rtmp_amf0_elt_t out_elts[] = { { NGX_RTMP_AMF0_STRING, NULL, "_result", sizeof("_result") - 1 }, { NGX_RTMP_AMF0_NUMBER, NULL, &trans, sizeof(trans) }, @@ -433,18 +428,25 @@ ngx_rtmp_broadcast_create_stream(ngx_rtmp_session_t *s, double in_trans, ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "createStream() called"); - trans = in_trans; + if (ngx_rtmp_receive_amf0(s, in, in_elts, + sizeof(in_elts) / sizeof(in_elts[0]))) + { + return NGX_ERROR; + } + stream = 1; - return ngx_rtmp_send_amf0(s, 3, 0, out_elts, + return ngx_rtmp_send_amf0(s, h, out_elts, sizeof(out_elts) / sizeof(out_elts[0])); } static ngx_int_t -ngx_rtmp_broadcast_publish(ngx_rtmp_session_t *s, double in_trans, +ngx_rtmp_broadcast_publish(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in) { + ngx_rtmp_header_t sh; + static double trans; static u_char pub_name[1024]; static u_char pub_type[1024]; @@ -456,6 +458,7 @@ ngx_rtmp_broadcast_publish(ngx_rtmp_session_t *s, double in_trans, }; static ngx_rtmp_amf0_elt_t in_elts[] = { + { NGX_RTMP_AMF0_NUMBER, 0, &trans, sizeof(trans) }, { NGX_RTMP_AMF0_NULL, NULL, NULL, 0 }, { NGX_RTMP_AMF0_STRING, NULL, pub_name, sizeof(pub_name) }, { NGX_RTMP_AMF0_STRING, NULL, pub_type, sizeof(pub_type) }, @@ -468,7 +471,6 @@ ngx_rtmp_broadcast_publish(ngx_rtmp_session_t *s, double in_trans, { NGX_RTMP_AMF0_OBJECT, NULL, out_inf, sizeof(out_inf) }, }; - if (ngx_rtmp_receive_amf0(s, in, in_elts, sizeof(in_elts) / sizeof(in_elts[0]))) { @@ -479,14 +481,22 @@ ngx_rtmp_broadcast_publish(ngx_rtmp_session_t *s, double in_trans, "publish() called; pubName='%s' pubType='%s'", pub_name, pub_type); + if (ngx_rtmp_send_user_stream_begin(s, 1) != NGX_OK) { + return NGX_ERROR; + } + ngx_rtmp_broadcast_set_flags(s, NGX_RTMP_BROADCAST_PUBLISHER); - trans = in_trans; ngx_str_set(&out_inf[0], "NetStream.Publish.Start"); ngx_str_set(&out_inf[1], "status"); ngx_str_set(&out_inf[2], "Publish succeeded."); - if (ngx_rtmp_send_amf0(s, 3, 0, out_elts, + memset(&sh, 0, sizeof(sh)); + sh.type = NGX_RTMP_MSG_AMF0_CMD; + sh.csid = 5; /*FIXME*/ + sh.msid = h->msid; + + if (ngx_rtmp_send_amf0(s, &sh, out_elts, sizeof(out_elts) / sizeof(out_elts[0])) != NGX_OK) { return NGX_ERROR; @@ -497,12 +507,14 @@ ngx_rtmp_broadcast_publish(ngx_rtmp_session_t *s, double in_trans, static ngx_int_t -ngx_rtmp_broadcast_play(ngx_rtmp_session_t *s, double in_trans, +ngx_rtmp_broadcast_play(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in) { + ngx_rtmp_header_t sh; + static double trans; - static u_char pub_name[1024]; - static u_char pub_type[1024]; + static u_char play_name[1024]; + static int bfalse; static ngx_rtmp_amf0_elt_t out_inf[] = { { NGX_RTMP_AMF0_STRING, "code", NULL, 0 }, @@ -510,10 +522,14 @@ ngx_rtmp_broadcast_play(ngx_rtmp_session_t *s, double in_trans, { NGX_RTMP_AMF0_STRING, "description", NULL, 0 }, }; + static ngx_rtmp_amf0_elt_t out2_inf[] = { + { NGX_RTMP_AMF0_STRING, "code", NULL, 0 }, + }; + static ngx_rtmp_amf0_elt_t in_elts[] = { + { NGX_RTMP_AMF0_NUMBER, 0, &trans, sizeof(trans) }, { NGX_RTMP_AMF0_NULL, NULL, NULL, 0 }, - { NGX_RTMP_AMF0_STRING, NULL, pub_name, sizeof(pub_name) }, - { NGX_RTMP_AMF0_STRING, NULL, pub_type, sizeof(pub_type) }, + { NGX_RTMP_AMF0_STRING, NULL, play_name, sizeof(play_name) }, }; static ngx_rtmp_amf0_elt_t out_elts[] = { @@ -523,6 +539,16 @@ ngx_rtmp_broadcast_play(ngx_rtmp_session_t *s, double in_trans, { NGX_RTMP_AMF0_OBJECT, NULL, out_inf, sizeof(out_inf) }, }; + static ngx_rtmp_amf0_elt_t out2_elts[] = { + { NGX_RTMP_AMF0_STRING, NULL, "onStatus", sizeof("onStatus") - 1 }, + { NGX_RTMP_AMF0_OBJECT, NULL, out2_inf, sizeof(out2_inf) }, + }; + + static ngx_rtmp_amf0_elt_t out3_elts[] = { + { NGX_RTMP_AMF0_STRING, NULL, "|RtmpSampleAccess", sizeof("|RtmpSampleAccess") - 1 }, + { NGX_RTMP_AMF0_BOOLEAN, NULL, &bfalse, sizeof(bfalse) }, + }; + if (ngx_rtmp_receive_amf0(s, in, in_elts, sizeof(in_elts) / sizeof(in_elts[0]))) @@ -530,33 +556,133 @@ ngx_rtmp_broadcast_play(ngx_rtmp_session_t *s, double in_trans, return NGX_ERROR; } - ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, - "play() called; pubName='%s' pubType='%s'", - pub_name, pub_type); + play_name[0] = 0; + ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, + "play() called; playame='%s'", + play_name); + + if (ngx_rtmp_send_user_stream_begin(s, 1) != NGX_OK) { + return NGX_ERROR; + } ngx_rtmp_broadcast_set_flags(s, NGX_RTMP_BROADCAST_SUBSCRIBER | NGX_RTMP_BROADCAST_WANT_KEYFRAME); - trans = in_trans; + memset(&sh, 0, sizeof(sh)); + sh.type = NGX_RTMP_MSG_AMF0_CMD; + sh.csid = 5; /*FIXME*/ + sh.msid = h->msid; + + ngx_str_set(&out_inf[0], "NetStream.Play.Reset"); + ngx_str_set(&out_inf[1], "status"); + ngx_str_set(&out_inf[2], "Playing and resetting."); + + if (ngx_rtmp_send_amf0(s, &sh, out_elts, + sizeof(out_elts) / sizeof(out_elts[0])) != NGX_OK) + { + return NGX_ERROR; + } + ngx_str_set(&out_inf[0], "NetStream.Play.Start"); ngx_str_set(&out_inf[1], "status"); ngx_str_set(&out_inf[2], "Started playing."); - if (ngx_rtmp_send_amf0(s, 3, 0, out_elts, + if (ngx_rtmp_send_amf0(s, &sh, out_elts, sizeof(out_elts) / sizeof(out_elts[0])) != NGX_OK) { return NGX_ERROR; } + ngx_str_set(&out2_inf[0], "NetStream.Data.Start"); + sh.type = NGX_RTMP_MSG_AMF0_META; + + if (ngx_rtmp_send_amf0(s, &sh, out3_elts, + sizeof(out3_elts) / sizeof(out3_elts[0])) != NGX_OK) + { + return NGX_ERROR; + } + + if (ngx_rtmp_send_amf0(s, &sh, out2_elts, + sizeof(out2_elts) / sizeof(out2_elts[0])) != NGX_OK) + { + return NGX_ERROR; + } + return NGX_OK; } -static ngx_int_t ngx_rtmp_broadcast_ok(ngx_rtmp_session_t *s, - double in_trans, ngx_chain_t *in) +static ngx_int_t +ngx_rtmp_broadcast_set_data_frame(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, + ngx_chain_t *in) +{ + ngx_connection_t *c; + ngx_rtmp_broadcast_ctx_t *ctx; + ngx_rtmp_amf0_ctx_t act; + ngx_rtmp_header_t sh; + + static ngx_rtmp_amf0_elt_t out_elts[] = { + { NGX_RTMP_AMF0_STRING, NULL, + "@setDataFrame", sizeof("@setDataFrame") - 1 }, + }; + + c = s->connection; + + ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_broadcast_module); + + ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "data_frame arrived"); + + if (ctx->data_frame) { + ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, + "duplicate data_frame"); + return NGX_OK; + } + + /* create full metadata chain for output */ + memset(&act, 0, sizeof(act)); + act.arg = s; + act.alloc = ngx_rtmp_alloc_shared_buf; + act.log = c->log; + + if (ngx_rtmp_amf0_write(&act, out_elts, + sizeof(out_elts) / sizeof(out_elts[0])) != NGX_OK) + { + return NGX_ERROR; + } + + if (act.first == NULL) { + return NGX_OK; + } + + ctx->data_frame = act.first; + + if (ngx_rtmp_append_shared_bufs(s, ctx->data_frame, in) == NULL) { + return NGX_ERROR; + } + + memset(&sh, 0, sizeof(sh)); + sh.csid = 5; + sh.msid = h->msid; + sh.type = h->type; + + ngx_rtmp_addref_shared_bufs(ctx->data_frame); + + ngx_rtmp_prepare_message(s, h, NULL, ctx->data_frame); + + return NGX_OK; +} + + +static ngx_int_t +ngx_rtmp_broadcast_ok(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, + ngx_chain_t *in) { static double trans; + static ngx_rtmp_amf0_elt_t in_elts[] = { + { NGX_RTMP_AMF0_NUMBER, 0, &trans, sizeof(trans) }, + }; + static ngx_rtmp_amf0_elt_t out_inf[] = { { NGX_RTMP_AMF0_STRING, "code", NULL, 0 }, { NGX_RTMP_AMF0_STRING, "level", NULL, 0 }, @@ -570,10 +696,13 @@ static ngx_int_t ngx_rtmp_broadcast_ok(ngx_rtmp_session_t *s, { NGX_RTMP_AMF0_OBJECT, NULL, out_inf, sizeof(out_inf) }, }; + if (ngx_rtmp_receive_amf0(s, in, in_elts, + sizeof(in_elts) / sizeof(in_elts[0]))) + { + return NGX_ERROR; + } - trans = in_trans; - - return ngx_rtmp_send_amf0(s, 3, 0, out_elts, + return ngx_rtmp_send_amf0(s, h, out_elts, sizeof(out_elts) / sizeof(out_elts[0])); } @@ -609,7 +738,7 @@ ngx_rtmp_broadcast_postconfiguration(ngx_conf_t *cf) /* register AMF0 call handlers */ ncalls = sizeof(ngx_rtmp_broadcast_map) / sizeof(ngx_rtmp_broadcast_map[0]); - h = ngx_array_push_n(&cmcf->calls, ncalls); + h = ngx_array_push_n(&cmcf->amf0, ncalls); if (h == NULL) { return NGX_ERROR; } diff --git a/ngx_rtmp_core_module.c b/ngx_rtmp_core_module.c index 0f475dc..e5dd125 100644 --- a/ngx_rtmp_core_module.c +++ b/ngx_rtmp_core_module.c @@ -168,7 +168,7 @@ ngx_rtmp_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_uint_value(conf->ack_window, prev->ack_window, 5000000); if (prev->out_pool == NULL) { - prev->out_pool = ngx_create_pool(4096, cf->log); + prev->out_pool = ngx_create_pool(8192, cf->log); if (prev->out_pool == NULL) { return NGX_CONF_ERROR; } diff --git a/ngx_rtmp_handler.c b/ngx_rtmp_handler.c index 939bc55..ea461f7 100644 --- a/ngx_rtmp_handler.c +++ b/ngx_rtmp_handler.c @@ -25,8 +25,8 @@ static ngx_int_t ngx_rtmp_receive_message(ngx_rtmp_session_t *s, #ifdef NGX_DEBUG -static char* -ngx_rtmp_packet_type(uint8_t type) { +char* +ngx_rtmp_message_type(uint8_t type) { static char* types[] = { "?", "chunk_size", @@ -57,6 +57,24 @@ ngx_rtmp_packet_type(uint8_t type) { ? types[type] : "?"; } + + +char* +ngx_rtmp_user_message_type(uint16_t evt) { + static char* evts[] = { + "stream_begin", + "stream_eof", + "stream dry", + "set_buflen", + "recorded", + "ping_request", + "ping_response", + }; + + return evt < sizeof(evts) / sizeof(evts[0]) + ? evts[evt] + : "?"; +} #endif void @@ -216,14 +234,12 @@ ngx_rtmp_init_session(ngx_connection_t *c) ngx_rtmp_close_connection(c); return; } - + + size = NGX_RTMP_HANDSHAKE_SIZE + 1; s->in_chunk_size = NGX_RTMP_DEFAULT_CHUNK_SIZE; - s->in_pool = ngx_create_pool(2 * (NGX_RTMP_HANDSHAKE_SIZE + 1) - + sizeof(ngx_pool_t), c->log); + s->in_pool = ngx_create_pool(2 * size + sizeof(ngx_pool_t), c->log); /* start handshake */ - size = NGX_RTMP_HANDSHAKE_SIZE + 1; - b = &s->hs_in_buf; b->start = b->pos = b->last = ngx_pcalloc(s->in_pool, size); b->end = b->start + size; @@ -350,7 +366,7 @@ ngx_rtmp_handshake_recv(ngx_event_t *rev) } /* reply timestamp is the same as out epoch */ - ngx_memcpy(s->hs_in_buf.pos + 4, b->pos + 1, 4); + /*ngx_memcpy(s->hs_in_buf.pos + 4, b->pos + 1, 4);*/ ngx_rtmp_handshake_send(c->write); @@ -439,6 +455,8 @@ restart: ngx_rtmp_handshake_recv(c->read); } +ngx_chain_t * tmp; + void ngx_rtmp_recv(ngx_event_t *rev) @@ -483,6 +501,10 @@ ngx_rtmp_recv(ngx_event_t *rev) return; } + ngx_log_debug2(NGX_LOG_DEBUG_RTMP, c->log, 0, + "allocating input buffer %p : %p", + st->in, st->in->buf); + st->in->next = NULL; b = st->in->buf; size = s->in_chunk_size + NGX_RTMP_MAX_CHUNK_HEADER; @@ -577,7 +599,7 @@ ngx_rtmp_recv(ngx_event_t *rev) if (csid >= cscf->max_streams) { ngx_log_error(NGX_LOG_INFO, c->log, NGX_ERROR, - "RTMP chunk stream too big: %D >= %D", + "RTMP in chunk stream too big: %D >= %D", csid, cscf->max_streams); ngx_rtmp_close_connection(c); return; @@ -648,12 +670,13 @@ ngx_rtmp_recv(ngx_event_t *rev) if (timestamp == 0x00ffffff) { if (b->last - p < 4) continue; - pp = (u_char*)&h->timestamp; + pp = (u_char*)×tamp; pp[3] = *p++; pp[2] = *p++; pp[1] = *p++; pp[0] = *p++; - } else if (fmt) { + } + if (fmt) { h->timestamp += timestamp; } else { h->timestamp = timestamp; @@ -662,10 +685,15 @@ ngx_rtmp_recv(ngx_event_t *rev) ngx_log_debug6(NGX_LOG_DEBUG_RTMP, c->log, 0, "RTMP mheader %s (%d) " - "timestamp=%D mlen=%D len=%D msid=%D", - ngx_rtmp_packet_type(h->type), (int)h->type, + "timestamp=%uD mlen=%D len=%D msid=%D", + ngx_rtmp_message_type(h->type), (int)h->type, h->timestamp, h->mlen, st->len, h->msid); + if (h->mlen==51441 && st->len==20864) { + /*asm("int $0x03");*/ + tmp = in; + } + /* header done */ b->pos = p; } @@ -689,7 +717,8 @@ ngx_rtmp_recv(ngx_event_t *rev) /* handle! */ head = st->in->next; st->in->next = NULL; - old_pos = b->pos + fsize; + b->last = b->pos + fsize; + old_pos = b->last; old_size = size - fsize; st->len = 0; @@ -713,7 +742,6 @@ ngx_rtmp_recv(ngx_event_t *rev) #define ngx_rtmp_buf_addref(b) \ (++*(int*)&((b)->tag)) - #define ngx_rtmp_buf_release(b) \ (--*(int*)&((b)->tag)) @@ -725,6 +753,7 @@ ngx_rtmp_send(ngx_event_t *wev) ngx_rtmp_session_t *s; ngx_rtmp_core_srv_conf_t *cscf; ngx_chain_t *out, *l; + u_char *p; c = wev->data; s = c->data; @@ -747,6 +776,8 @@ ngx_rtmp_send(ngx_event_t *wev) } while (s->out) { + p = s->out->buf->pos; + out = c->send_chain(c, s->out, 0); if (out == NGX_CHAIN_ERROR) { @@ -754,9 +785,7 @@ ngx_rtmp_send(ngx_event_t *wev) return; } - if (out == s->out - && out->buf->pos == out->buf->last) - { + if (out == s->out && out->buf->pos == p) { cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); ngx_add_timer(c->write, cscf->timeout); if (ngx_handle_write_event(c->write, 0) != NGX_OK) { @@ -769,7 +798,7 @@ ngx_rtmp_send(ngx_event_t *wev) l = s->out; if (l->buf->pos < l->buf->last) { - l->buf->pos = l->buf->last; + /*l->buf->pos = l->buf->last;*/ break; } @@ -784,7 +813,7 @@ ngx_rtmp_send(ngx_event_t *wev) } /* return buffer to core */ - if (ngx_rtmp_release_shared_buf(s, l)) { + if (ngx_rtmp_free_shared_buf(s, l)) { ngx_rtmp_close_connection(c); return; } @@ -815,14 +844,7 @@ ngx_rtmp_alloc_shared_buf(ngx_rtmp_session_t *s) out = cscf->out_free; cscf->out_free = out->next; - ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, - "reuse shared buf"); - } else { - - ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, - "alloc shared buf"); - out = ngx_alloc_chain_link(cscf->out_pool); if (out == NULL) { return NULL; @@ -850,8 +872,74 @@ ngx_rtmp_alloc_shared_buf(ngx_rtmp_session_t *s) } +ngx_chain_t * +ngx_rtmp_append_shared_bufs(ngx_rtmp_session_t *s, ngx_chain_t *out, + ngx_chain_t *in) +{ + ngx_connection_t *c; + ngx_chain_t *l, **ll; + u_char *p; + size_t size; + + c = s->connection; + ll = &out; + p = in->buf->pos; + l = out; + + if (l) { + for(; l->next; l = l->next); + ll = &l->next; + } + + for ( ;; ) { + + if (l == NULL || l->buf->last == l->buf->end) { + l = ngx_rtmp_alloc_shared_buf(s); + if (l == NULL || l->buf == NULL) { + return NULL; + } + + *ll = l; + ll = &l->next; + } + + while (l->buf->end - l->buf->last >= in->buf->last - p) { + l->buf->last = ngx_cpymem(l->buf->last, p, + in->buf->last - p); + in = in->next; + if (in == NULL) { + goto done; + } + ngx_log_debug2(NGX_LOG_DEBUG_RTMP, c->log, 0, + "copy link %p : %p", in, in->buf); + p = in->buf->pos; + } + + size = l->buf->end - l->buf->last; + l->buf->last = ngx_cpymem(l->buf->last, p, size); + p += size; + } + +done: + *ll = NULL; + + return out; +} + + ngx_int_t -ngx_rtmp_release_shared_buf(ngx_rtmp_session_t *s, +ngx_rtmp_addref_shared_bufs(ngx_chain_t *in) +{ + for(; in; in = in->next) { + ngx_rtmp_buf_addref(in->buf); + } + + return NGX_OK; +} + + +ngx_int_t +ngx_rtmp_free_shared_buf(ngx_rtmp_session_t *s, ngx_chain_t *out) { ngx_rtmp_core_srv_conf_t *cscf; @@ -875,14 +963,28 @@ ngx_rtmp_release_shared_buf(ngx_rtmp_session_t *s, void ngx_rtmp_prepare_message(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, - ngx_chain_t *out, uint8_t fmt) + ngx_rtmp_header_t *lh, ngx_chain_t *out) { - ngx_chain_t *l; - u_char *p, *pp; - ngx_int_t hsize, thsize, nbufs; - uint32_t mlen, timestamp, ext_timestamp; - static uint8_t hdrsize[] = { 12, 8, 4, 1 }; - u_char th[3]; + ngx_chain_t *l; + u_char *p, *pp; + ngx_int_t hsize, thsize, nbufs; + uint32_t mlen, timestamp, ext_timestamp; + static uint8_t hdrsize[] = { 12, 8, 4, 1 }; + u_char th[3]; + ngx_rtmp_core_srv_conf_t *cscf; + uint8_t fmt; + ngx_connection_t *c; + + c = s->connection; + cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); + + if (h->csid >= cscf->max_streams) { + ngx_log_error(NGX_LOG_INFO, c->log, NGX_ERROR, + "RTMP out chunk stream too big: %D >= %D", + h->csid, cscf->max_streams); + ngx_rtmp_close_connection(c); + return; + } /* detect packet size */ mlen = 0; @@ -892,22 +994,40 @@ ngx_rtmp_prepare_message(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ++nbufs; } - ngx_log_debug8(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, - "RTMP prep %s (%d) fmt=%d csid=%D timestamp=%D " - "mlen=%D msid=%D nbufs=%d", - ngx_rtmp_packet_type(h->type), (int)h->type, (int)fmt, - h->csid, h->timestamp, mlen, h->msid, nbufs); - - /* determine initial header size */ - hsize = hdrsize[fmt]; - - if (h->timestamp >= 0x00ffffff) { - timestamp = 0x00ffffff; - ext_timestamp = h->timestamp; - hsize += 4; + fmt = 0; + if (lh && lh->csid && h->msid == lh->msid) { + ++fmt; + if (h->type == lh->type + && mlen == lh->mlen) + { + ++fmt; + if (h->timestamp == lh->timestamp) { + ++fmt; + } + } + timestamp = h->timestamp - lh->timestamp; } else { timestamp = h->timestamp; - ext_timestamp = 0; + } + + if (lh) { + *lh = *h; + lh->mlen = mlen; + } + + hsize = hdrsize[fmt]; + + ngx_log_debug8(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, + "RTMP prep %s (%d) fmt=%d csid=%uD timestamp=%uD " + "mlen=%uD msid=%uD nbufs=%d", + ngx_rtmp_message_type(h->type), (int)h->type, (int)fmt, + h->csid, h->timestamp, h->mlen, h->msid, nbufs); + + ext_timestamp = 0; + if (timestamp >= 0x00ffffff) { + ext_timestamp = timestamp; + timestamp = 0x00ffffff; + hsize += 4; } if (h->csid >= 64) { @@ -1041,7 +1161,7 @@ ngx_rtmp_receive_message(ngx_rtmp_session_t *s, ngx_log_debug7(NGX_LOG_DEBUG_RTMP, c->log, 0, "RTMP recv %s (%d) csid=%D timestamp=%D " "mlen=%D msid=%D nbufs=%d", - ngx_rtmp_packet_type(h->type), (int)h->type, + ngx_rtmp_message_type(h->type), (int)h->type, h->csid, h->timestamp, h->mlen, h->msid, nbufs); } #endif @@ -1107,6 +1227,11 @@ ngx_rtmp_close_connection(ngx_connection_t *c) } } + if (s->out) { + ngx_rtmp_free_shared_buf(s, s->out); + s->out = NULL; + } + c->destroyed = 1; pool = c->pool; ngx_close_connection(c); diff --git a/ngx_rtmp_receive.c b/ngx_rtmp_receive.c index e0b5cbd..92b408b 100644 --- a/ngx_rtmp_receive.c +++ b/ngx_rtmp_receive.c @@ -95,6 +95,10 @@ ngx_rtmp_user_message_handler(ngx_rtmp_session_t *s, p[0] = b->pos[1]; p[1] = b->pos[0]; + ngx_log_debug2(NGX_LOG_DEBUG_RTMP, c->log, 0, + "RTMP recv user evt %s (%d)", + ngx_rtmp_user_message_type(evt), (int)evt); + p = (u_char*)&val; p[0] = b->pos[5]; p[1] = b->pos[4]; @@ -159,7 +163,7 @@ ngx_rtmp_amf0_message_handler(ngx_rtmp_session_t *s, ngx_rtmp_amf0_ctx_t act; ngx_connection_t *c; ngx_rtmp_core_main_conf_t *cmcf; - ngx_rtmp_call_handler_pt ch; + ngx_rtmp_event_handler_pt ch; size_t len; static double trans; @@ -167,7 +171,6 @@ ngx_rtmp_amf0_message_handler(ngx_rtmp_session_t *s, static ngx_rtmp_amf0_elt_t elts[] = { { NGX_RTMP_AMF0_STRING, 0, func, sizeof(func) }, - { NGX_RTMP_AMF0_NUMBER, 0, &trans, sizeof(trans) }, }; c = s->connection; @@ -192,14 +195,14 @@ ngx_rtmp_amf0_message_handler(ngx_rtmp_session_t *s, * only the first handler is called so far * because ngx_hash_find only returns one item; * no good to patch NGINX core ;) */ - ch = ngx_hash_find(&cmcf->calls_hash, + ch = ngx_hash_find(&cmcf->amf0_hash, ngx_hash_strlow(func, func, len), func, len); if (ch) { ngx_log_debug2(NGX_LOG_DEBUG_RTMP, c->log, 0, "AMF0 func '%s' trans=%f passed to handler", func, trans); - return ch(s, trans, in); + return ch(s, h, in); } ngx_log_debug2(NGX_LOG_DEBUG_RTMP, c->log, 0, diff --git a/ngx_rtmp_send.c b/ngx_rtmp_send.c index 8566ce1..b100d26 100644 --- a/ngx_rtmp_send.c +++ b/ngx_rtmp_send.c @@ -36,7 +36,7 @@ *(__b->last++) = ((u_char*)&v)[0]; #define NGX_RTMP_USER_END(s) \ - ngx_rtmp_prepare_message(s, &__h, __l, 0); \ + ngx_rtmp_prepare_message(s, &__h, NULL, __l); \ return ngx_rtmp_send_message(s, __l); \ @@ -180,10 +180,9 @@ ngx_rtmp_send_user_ping_response(ngx_rtmp_session_t *s, uint32_t timestamp) /* AMF0 sender */ ngx_int_t -ngx_rtmp_send_amf0(ngx_rtmp_session_t *s, uint32_t csid, uint32_t msid, +ngx_rtmp_send_amf0(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_rtmp_amf0_elt_t *elts, size_t nelts) { - ngx_rtmp_header_t h; ngx_rtmp_amf0_ctx_t act; memset(&act, 0, sizeof(act)); @@ -191,17 +190,12 @@ ngx_rtmp_send_amf0(ngx_rtmp_session_t *s, uint32_t csid, uint32_t msid, act.alloc = ngx_rtmp_alloc_shared_buf; act.log = s->connection->log; - memset(&h, 0, sizeof(h)); - h.type = NGX_RTMP_MSG_AMF0_CMD; - h.csid = csid; - h.msid = msid; - if (ngx_rtmp_amf0_write(&act, elts, nelts) != NGX_OK) { return NGX_ERROR; } if (act.first) { - ngx_rtmp_prepare_message(s, &h, act.first, 0); + ngx_rtmp_prepare_message(s, h, NULL, act.first); return ngx_rtmp_send_message(s, act.first); }