]> git.sesse.net Git - vlc/blob - modules/access/rtmp/rtmp_amf_flv.c
a639221c4560380f1b5249864548c3d4ebff0e48
[vlc] / modules / access / rtmp / rtmp_amf_flv.c
1 /*****************************************************************************
2  * rtmp_amf_flv.c: RTMP, AMF and FLV over RTMP implementation.
3  *****************************************************************************
4  * Copyright (C) URJC - LADyR - Luis Lopez Fernandez
5  *
6  * Author: Miguel Angel Cabrera Moya
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * RTMP header:
25  ******************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_access.h>
32
33 #include <vlc_network.h> /* DOWN: #include <network.h> */
34 #include <vlc_url.h>
35 #include <vlc_block.h>
36 #include <vlc_rand.h>
37
38 #include <stdlib.h>
39 #include <stdint.h>
40 #include <string.h>
41
42 #include "rtmp_amf_flv.h"
43
44 /* header length (including itself) */
45 const uint8_t RTMP_HEADER_SIZE_MASK = 0xC0;
46 const uint8_t RTMP_HEADER_SIZE_12 = 0x00; 
47 const uint8_t RTMP_HEADER_SIZE_8 = 0x40;
48 const uint8_t RTMP_HEADER_SIZE_4 = 0x80;
49 const uint8_t RTMP_HEADER_SIZE_1 = 0xC0;
50
51 /* streams */
52 const uint8_t RTMP_HEADER_STREAM_MAX = 64;
53 const uint8_t RTMP_HEADER_STREAM_INDEX_MASK = 0x3F;
54
55 /* handshake */
56 const uint8_t RTMP_HANDSHAKE = 0x03;
57 const uint16_t RTMP_HANDSHAKE_BODY_SIZE = 1536;
58
59 /* content types */
60 const uint8_t RTMP_CONTENT_TYPE_CHUNK_SIZE = 0x01;
61 const uint8_t RTMP_CONTENT_TYPE_UNKNOWN_02 = 0x02;
62 const uint8_t RTMP_CONTENT_TYPE_BYTES_READ = 0x03;
63 const uint8_t RTMP_CONTENT_TYPE_PING = 0x04;
64 const uint8_t RTMP_CONTENT_TYPE_SERVER_BW = 0x05;
65 const uint8_t RTMP_CONTENT_TYPE_CLIENT_BW = 0x06;
66 const uint8_t RTMP_CONTENT_TYPE_UNKNOWN_07 = 0x07;
67 const uint8_t RTMP_CONTENT_TYPE_AUDIO_DATA = 0x08;
68 const uint8_t RTMP_CONTENT_TYPE_VIDEO_DATA = 0x09;
69 const uint8_t RTMP_CONTENT_TYPE_UNKNOWN_0A_0E = 0x0A;
70 const uint8_t RTMP_CONTENT_TYPE_FLEX_STREAM = 0x0F;
71 const uint8_t RTMP_CONTENT_TYPE_FLEX_SHARED_OBJECT = 0x10;
72 const uint8_t RTMP_CONTENT_TYPE_MESSAGE = 0x11;
73 const uint8_t RTMP_CONTENT_TYPE_NOTIFY = 0x12;
74 const uint8_t RTMP_CONTENT_TYPE_SHARED_OBJECT = 0x13;
75 const uint8_t RTMP_CONTENT_TYPE_INVOKE = 0x14;
76
77 /* shared object datatypes */
78 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_CONNECT = 0x01;
79 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_DISCONNECT = 0x02;
80 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_SET_ATTRIBUTE = 0x03;
81 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_UPDATE_DATA = 0x04;
82 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_UPDATE_ATTRIBUTE = 0x05;
83 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_SEND_MESSAGE = 0x06;
84 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_STATUS = 0x07;
85 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_CLEAR_DATA = 0x08;
86 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_DELETE_DATA = 0x09;
87 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_DELETE_ATTRIBUTE = 0x0A;
88 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_INITIAL_DATA = 0x0B;
89
90 /* pings */
91 const uint16_t RTMP_PING_CLEAR_STREAM = 0x0000;
92 const uint16_t RTMP_PING_CLEAR_PLAYING_BUFFER = 0x0001;
93 const uint16_t RTMP_PING_BUFFER_TIME_CLIENT = 0x0003;
94 const uint16_t RTMP_PING_RESET_STREAM = 0x0004;
95 const uint16_t RTMP_PING_CLIENT_FROM_SERVER = 0x0006;
96 const uint16_t RTMP_PING_PONG_FROM_CLIENT = 0x0007;
97
98 /* pings sizes */
99 const uint8_t RTMP_PING_SIZE_CLEAR_STREAM = 6;
100 const uint8_t RTMP_PING_SIZE_CLEAR_PLAYING_BUFFER = 6;
101 const uint8_t RTMP_PING_SIZE_BUFFER_TIME_CLIENT = 10;
102 const uint8_t RTMP_PING_SIZE_RESET_STREAM = 6;
103 /*const uint8_t RTMP_PING_SIZE_CLIENT_FROM_SERVER = 0x0006; TODO
104 const uint8_t RTMP_PING_SIZE_PONG_FROM_CLIENT = 0x0007;
105 */
106
107 /* default values */
108 const uint8_t RTMP_DEFAULT_STREAM_INDEX_CONTROL = 0x02;
109 const uint8_t RTMP_DEFAULT_STREAM_INDEX_INVOKE = 0x03;
110 const uint8_t RTMP_DEFAULT_STREAM_INDEX_NOTIFY = 0x04;
111 const uint8_t RTMP_DEFAULT_STREAM_INDEX_VIDEO_DATA = 0x05;
112 const uint8_t RTMP_DEFAULT_STREAM_INDEX_AUDIO_DATA = 0x06;
113 const uint32_t RTMP_DEFAULT_CHUNK_SIZE = 128;
114 const double RTMP_DEFAULT_STREAM_CLIENT_ID = 1.0;
115 const double RTMP_DEFAULT_STREAM_SERVER_ID = 1.0;
116
117 /* misc */
118 const uint16_t MAX_EMPTY_BLOCKS = 200; /* empty blocks in fifo for media*/
119 const uint16_t RTMP_BODY_SIZE_ALLOC = 1024;
120 const uint32_t RTMP_TIME_CLIENT_BUFFER = 2000; /* milliseconds */
121 const uint32_t RTMP_SERVER_BW = 0x00000200;
122 const uint32_t RTMP_SRC_DST_CONNECT_OBJECT = 0x00000000;
123 const uint32_t RTMP_SRC_DST_CONNECT_OBJECT2 = 0x00000001;
124 const uint32_t RTMP_SRC_DST_DEFAULT = 0x01000000;
125 const uint64_t RTMP_AUDIOCODECS = 0x4083380000000000;
126 const uint64_t RTMP_VIDEOCODECS = 0x405f000000000000;
127 const uint64_t RTMP_VIDEOFUNCTION = 0x3ff0000000000000;
128 /*****************************************************************************
129  * AMF header:
130  ******************************************************************************/
131
132 /* boolean constants */
133 const uint8_t AMF_BOOLEAN_FALSE = 0x00;
134 const uint8_t AMF_BOOLEAN_TRUE = 0x01;
135
136 /* datatypes */
137 const uint8_t AMF_DATATYPE_NUMBER = 0x00;
138 const uint8_t AMF_DATATYPE_BOOLEAN = 0x01;
139 const uint8_t AMF_DATATYPE_STRING = 0x02;
140 const uint8_t AMF_DATATYPE_OBJECT = 0x03;
141 const uint8_t AMF_DATATYPE_MOVIE_CLIP = 0x04;
142 const uint8_t AMF_DATATYPE_NULL = 0x05;
143 const uint8_t AMF_DATATYPE_UNDEFINED = 0x06;
144 const uint8_t AMF_DATATYPE_REFERENCE = 0x07;
145 const uint8_t AMF_DATATYPE_MIXED_ARRAY = 0x08;
146 const uint8_t AMF_DATATYPE_END_OF_OBJECT = 0x09;
147 const uint8_t AMF_DATATYPE_ARRAY = 0x0A;
148 const uint8_t AMF_DATATYPE_DATE = 0x0B;
149 const uint8_t AMF_DATATYPE_LONG_STRING = 0x0C;
150 const uint8_t AMF_DATATYPE_UNSUPPORTED = 0x0D;
151 const uint8_t AMF_DATATYPE_RECORDSET = 0x0E;
152 const uint8_t AMF_DATATYPE_XML = 0x0F;
153 const uint8_t AMF_DATATYPE_TYPED_OBJECT = 0x10;
154 const uint8_t AMF_DATATYPE_AMF3_DATA = 0x11;
155
156 /* datatypes sizes */
157 const uint8_t AMF_DATATYPE_SIZE_NUMBER = 9;
158 const uint8_t AMF_DATATYPE_SIZE_BOOLEAN = 2;
159 const uint8_t AMF_DATATYPE_SIZE_STRING = 3;
160 const uint8_t AMF_DATATYPE_SIZE_OBJECT = 1;
161 const uint8_t AMF_DATATYPE_SIZE_NULL = 1;
162 const uint8_t AMF_DATATYPE_SIZE_OBJECT_VARIABLE = 2;
163 const uint8_t AMF_DATATYPE_SIZE_MIXED_ARRAY = 5;
164 const uint8_t AMF_DATATYPE_SIZE_END_OF_OBJECT = 3;
165
166 /* amf remote calls */
167 const uint64_t AMF_CALL_NETCONNECTION_CONNECT = 0x3FF0000000000000;
168 const uint64_t AMF_CALL_NETCONNECTION_CONNECT_AUDIOCODECS = 0x4083380000000000;
169 const uint64_t AMF_CALL_NETCONNECTION_CONNECT_VIDEOCODECS = 0x405F000000000000;
170 const uint64_t AMF_CALL_NETCONNECTION_CONNECT_VIDEOFUNCTION = 0x3FF0000000000000;
171 const uint64_t AMF_CALL_NETCONNECTION_CONNECT_OBJECTENCODING = 0x0;
172 const double AMF_CALL_STREAM_CLIENT_NUMBER = 3.0;
173 const double AMF_CALL_ONBWDONE = 2.0; 
174 const uint64_t AMF_CALL_NETSTREAM_PLAY = 0x0;
175
176 /*****************************************************************************
177  * FLV header:
178  ******************************************************************************/
179 const uint8_t FLV_HEADER_SIGNATURE[3] = { 0x46, 0x4C, 0x56 }; /* always "FLV" */
180 const uint8_t FLV_HEADER_VERSION = 0x01;
181 const uint8_t FLV_HEADER_AUDIO = 0x04;
182 const uint8_t FLV_HEADER_VIDEO = 0x01;
183 const uint32_t FLV_HEADER_SIZE = 0x00000009; /* always 9 for known FLV files */
184
185 const uint32_t FLV_TAG_FIRST_PREVIOUS_TAG_SIZE = 0x00000000;
186 const uint8_t FLV_TAG_PREVIOUS_TAG_SIZE = 4;
187 const uint8_t FLV_TAG_SIZE = 11;
188
189 /* audio stereo types */
190 const uint8_t FLV_AUDIO_STEREO_MASK = 0x01;
191 const uint8_t FLV_AUDIO_STEREO_MONO = 0x00;
192 const uint8_t FLV_AUDIO_STEREO_STEREO = 0x01;
193
194 /* audio size */
195 const uint8_t FLV_AUDIO_SIZE_MASK = 0x02;
196 const uint8_t FLV_AUDIO_SIZE_8_BIT = 0x00;
197 const uint8_t FLV_AUDIO_SIZE_16_BIT = 0x02;
198
199 /* audio rate */
200 const uint8_t FLV_AUDIO_RATE_MASK = 0x0C;
201 const uint8_t FLV_AUDIO_RATE_5_5_KHZ = 0x00;
202 const uint8_t FLV_AUDIO_RATE_11_KHZ = 0x04;
203 const uint8_t FLV_AUDIO_RATE_22_KHZ = 0x08;
204 const uint8_t FLV_AUDIO_RATE_44_KHZ = 0x0C;
205
206 /* audio codec types */
207 const uint8_t FLV_AUDIO_CODEC_ID_MASK = 0xF0;
208 const uint8_t FLV_AUDIO_CODEC_ID_UNCOMPRESSED = 0x00;
209 const uint8_t FLV_AUDIO_CODEC_ID_ADPCM = 0x10;
210 const uint8_t FLV_AUDIO_CODEC_ID_MP3 = 0x20;
211 const uint8_t FLV_AUDIO_CODEC_ID_NELLYMOSER_8KHZ_MONO = 0x50;
212 const uint8_t FLV_AUDIO_CODEC_ID_NELLYMOSER = 0x60;
213
214 /* video codec types */
215 const uint8_t FLV_VIDEO_CODEC_ID_MASK = 0x0F;
216 const uint8_t FLV_VIDEO_CODEC_ID_SORENSEN_H263 = 0x02;
217 const uint8_t FLV_VIDEO_CODEC_ID_SCREEN_VIDEO = 0x03;
218 const uint8_t FLV_VIDEO_CODEC_ID_ON2_VP6 = 0x04;
219 const uint8_t FLV_VIDEO_CODEC_ID_ON2_VP6_ALPHA = 0x05;
220 const uint8_t FLV_VIDEO_CODEC_ID_SCREEN_VIDEO_2 = 0x06;
221
222 /* video frame types */
223 const uint8_t FLV_VIDEO_FRAME_TYPE_MASK = 0xF0;
224 const uint8_t FLV_VIDEO_FRAME_TYPE_KEYFRAME = 0x10;
225 const uint8_t FLV_VIDEO_FRAME_TYPE_INTER_FRAME = 0x20;
226 const uint8_t FLV_VIDEO_FRAME_TYPE_DISPOSABLE_INTER_FRAME = 0x30;
227
228 /*****************************************************************************
229  * static RTMP functions:
230  ******************************************************************************/
231 static rtmp_packet_t *rtmp_new_packet( rtmp_control_thread_t *p_thread, uint8_t stream_index, uint32_t timestamp, uint8_t content_type, uint32_t src_dst, rtmp_body_t *body );
232 static block_t *rtmp_new_block( rtmp_control_thread_t *p_thread, uint8_t *buffer, int32_t length_buffer );
233
234 static rtmp_packet_t *rtmp_encode_onBWDone( rtmp_control_thread_t *p_thread, double number );
235 static rtmp_packet_t *rtmp_encode_server_bw( rtmp_control_thread_t *p_thread, uint32_t number );
236 static rtmp_packet_t *rtmp_encode_NetConnection_connect_result( rtmp_control_thread_t *p_thread, double number );
237 static rtmp_packet_t *rtmp_encode_createStream_result( rtmp_control_thread_t *p_thread, double stream_client, double stream_server );
238 static rtmp_packet_t *rtmp_encode_ping_reset_stream( rtmp_control_thread_t *p_thread );
239 static rtmp_packet_t *rtmp_encode_ping_clear_stream( rtmp_control_thread_t *p_thread, uint32_t src_dst );
240 static rtmp_packet_t *rtmp_encode_NetStream_play_reset_onStatus( rtmp_control_thread_t *p_thread, char *psz_media );
241 static rtmp_packet_t *rtmp_encode_NetStream_play_start_onStatus( rtmp_control_thread_t *p_thread, char *psz_media );
242 static uint8_t rtmp_encode_header_size( vlc_object_t *p_this, uint8_t header_size );
243 static uint8_t rtmp_decode_header_size( vlc_object_t *p_this, uint8_t header_size );
244 static uint8_t rtmp_get_stream_index( uint8_t content_type );
245
246 static void rtmp_body_append( rtmp_body_t *rtmp_body, uint8_t *buffer, uint32_t length );
247
248 static uint8_t *rtmp_encode_ping( uint16_t type, uint32_t src_dst, uint32_t third_arg, uint32_t fourth_arg );
249
250 /*****************************************************************************
251  * static AMF functions:
252  ******************************************************************************/
253 static uint8_t *amf_encode_element( uint8_t element, const void *value );
254 static uint8_t *amf_encode_object_variable( const char *key, uint8_t element, const void *value );
255 static double amf_decode_number( uint8_t **buffer );
256 static int amf_decode_boolean( uint8_t **buffer );
257 static char *amf_decode_string( uint8_t **buffer );
258 static char *amf_decode_object( uint8_t **buffer );
259
260 /*****************************************************************************
261  * static FLV functions:
262  ******************************************************************************/
263 static void flv_rebuild( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
264 static void flv_get_metadata_audio( rtmp_control_thread_t *p_thread, rtmp_packet_t *packet_audio, uint8_t *stereo, uint8_t *audiosamplesize, uint32_t *audiosamplerate, uint8_t *audiocodecid );
265 static void flv_get_metadata_video( rtmp_control_thread_t *p_thread, rtmp_packet_t *packet_video, uint8_t *videocodecid, uint8_t *frametype );
266 static rtmp_packet_t *flv_build_onMetaData( access_t *p_access, uint64_t duration, uint8_t stereo, uint8_t audiosamplesize, uint32_t audiosamplerate, uint8_t audiocodecid, uint8_t videocodecid );
267
268 /*****************************************************************************
269  * RTMP implementation:
270  ******************************************************************************/
271 int
272 rtmp_handshake_passive( vlc_object_t *p_this, int fd )
273 {
274     uint8_t p_read[RTMP_HANDSHAKE_BODY_SIZE + 1];
275     uint8_t p_write[RTMP_HANDSHAKE_BODY_SIZE * 2 + 1];
276     ssize_t i_ret;
277     int i;
278
279     /* Receive handshake */
280     i_ret = net_Read( p_this, fd, NULL, p_read, RTMP_HANDSHAKE_BODY_SIZE + 1, true );
281     if( i_ret != RTMP_HANDSHAKE_BODY_SIZE + 1 )
282     {
283         msg_Err( p_this, "failed to receive handshake" );
284         return -1;
285     }
286
287     /* Check handshake */
288     if ( p_read[0] != RTMP_HANDSHAKE )
289     {
290         msg_Err( p_this, "first byte in handshake corrupt" );
291         return -1;
292     }
293
294     /* Answer handshake */
295     p_write[0] = RTMP_HANDSHAKE;
296     memset( p_write + 1, 0, RTMP_HANDSHAKE_BODY_SIZE );
297     memcpy( p_write + 1 + RTMP_HANDSHAKE_BODY_SIZE, p_read + 1, RTMP_HANDSHAKE_BODY_SIZE );
298
299     /* Send handshake*/
300     i_ret = net_Write( p_this, fd, NULL, p_write, RTMP_HANDSHAKE_BODY_SIZE * 2 + 1 );
301     if( i_ret != RTMP_HANDSHAKE_BODY_SIZE * 2 + 1 )
302     {
303         msg_Err( p_this, "failed to send handshake" );
304         return -1;
305     }
306
307     /* Receive acknowledge */
308     i_ret = net_Read( p_this, fd, NULL, p_read, RTMP_HANDSHAKE_BODY_SIZE, true );
309     if( i_ret != RTMP_HANDSHAKE_BODY_SIZE )
310     {
311         msg_Err( p_this, "failed to receive acknowledge" );
312         return -1;
313     }
314
315     /* Check acknowledge */
316     for(i = 8; i < RTMP_HANDSHAKE_BODY_SIZE; i++ )
317         if( p_write[i + 1] != p_read[i] )
318         {
319             msg_Err( p_this, "body acknowledge received corrupt" );
320             return -1;
321         }
322
323     return 0;
324 }
325
326 int
327 rtmp_handshake_active( vlc_object_t *p_this, int fd )
328 {
329     uint8_t p_read[RTMP_HANDSHAKE_BODY_SIZE * 2 + 1];
330     uint8_t p_write[RTMP_HANDSHAKE_BODY_SIZE + 1];
331     ssize_t i_ret;
332     int i;
333
334     p_write[0] = RTMP_HANDSHAKE;
335
336     for( i = 0; i < 8; i++ )
337         p_write[i + 1] = 0x00;
338
339     vlc_rand_bytes( p_write+1+8, RTMP_HANDSHAKE_BODY_SIZE-8 );
340
341     /* Send handshake*/
342     i_ret = net_Write( p_this, fd, NULL, p_write, RTMP_HANDSHAKE_BODY_SIZE+1 );
343     if( i_ret != RTMP_HANDSHAKE_BODY_SIZE + 1 )
344     {
345         msg_Err( p_this, "failed to send handshake" );
346         return -1;
347     }
348
349     /* Receive handshake */
350     i_ret = net_Read( p_this, fd, NULL, p_read, RTMP_HANDSHAKE_BODY_SIZE * 2 + 1, true );
351     if( i_ret != RTMP_HANDSHAKE_BODY_SIZE * 2 + 1 )
352     {
353         msg_Err( p_this, "failed to receive handshake" );
354         return -1;
355     }
356
357     /* Check handshake */
358     if( p_read[0] != RTMP_HANDSHAKE )
359     {
360         msg_Err( p_this, "first byte in handshake received corrupt" );
361         return -1;
362     }
363
364     for(i = 8; i < RTMP_HANDSHAKE_BODY_SIZE; i++ )
365         if( p_write[i + 1] != p_read[i + 1 + RTMP_HANDSHAKE_BODY_SIZE] )
366         {
367             msg_Err( p_this, "body handshake received corrupt" );
368             return -1;
369         }
370
371     /* Acknowledge handshake */
372     i_ret = net_Write( p_this, fd, NULL, p_read + 1, RTMP_HANDSHAKE_BODY_SIZE );
373     if( i_ret != RTMP_HANDSHAKE_BODY_SIZE )
374     {
375         msg_Err( p_this, "failed to acknowledge handshake" );
376         return -1;
377     }
378
379     return 0;
380 }
381
382 static int
383 write_rtmp( rtmp_control_thread_t *p_thread, uint8_t *buf,
384             rtmp_packet_t *pkt, const char *errmsg )
385 {
386     int32_t enclen = pkt->length_encoded;
387     int ret = net_Write( p_thread, p_thread->fd, NULL, buf, enclen );
388     free( pkt->body->body );
389     free( pkt->body );
390     free( pkt );
391     free( buf );
392     if( ret != enclen )
393     {
394         msg_Err( p_thread, "%s", errmsg );
395         return 0;
396     }
397     return 1;
398 }
399
400 int
401 rtmp_connect_active( rtmp_control_thread_t *p_thread )
402 {
403     rtmp_packet_t *rtmp_packet;
404     rtmp_body_t *rtmp_body;
405     uint8_t *tmp_buffer;
406     char *tmp_url;
407
408     /* Build NetConnection.connect call */
409     rtmp_body = rtmp_body_new( -1 );
410
411     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "connect" );
412     rtmp_body_append( rtmp_body, tmp_buffer, 
413         AMF_DATATYPE_SIZE_STRING + strlen( "connect" ) );
414     free( tmp_buffer );
415
416     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
417         &AMF_CALL_NETCONNECTION_CONNECT );
418     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
419     free( tmp_buffer );
420
421     tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
422     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
423     free( tmp_buffer );
424
425     tmp_buffer = amf_encode_object_variable( "app",
426         AMF_DATATYPE_STRING, p_thread->psz_application );
427     rtmp_body_append( rtmp_body, tmp_buffer,
428         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "app" ) + 
429         AMF_DATATYPE_SIZE_STRING + strlen( p_thread->psz_application ) );
430     free( tmp_buffer );
431
432     tmp_buffer = amf_encode_object_variable( "flashVer",
433         AMF_DATATYPE_STRING, "LNX 9,0,48,0" );
434     rtmp_body_append( rtmp_body, tmp_buffer,
435         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "flashVer" ) +
436         AMF_DATATYPE_SIZE_STRING + strlen( "LNX 9,0,48,0" ) );
437     free( tmp_buffer );
438
439     tmp_buffer = amf_encode_object_variable( "swfUrl",
440          AMF_DATATYPE_STRING, p_thread->psz_swf_url );
441     rtmp_body_append( rtmp_body, tmp_buffer,
442         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "swfUrl" ) +
443         AMF_DATATYPE_SIZE_STRING + strlen( p_thread->psz_swf_url ) );
444     free( tmp_buffer );
445
446     if( asprintf( &tmp_url, "rtmp://%s", p_thread->url.psz_buffer ) == -1 )
447     {
448         free( rtmp_body->body );
449         free( rtmp_body );
450         return -1;
451     }
452     tmp_buffer = amf_encode_object_variable( "tcUrl",
453         AMF_DATATYPE_STRING, tmp_url );
454     rtmp_body_append( rtmp_body, tmp_buffer,
455         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "tcUrl" ) +
456         AMF_DATATYPE_SIZE_STRING + strlen( tmp_url ) );
457     free( tmp_url );
458     free( tmp_buffer );
459
460     tmp_buffer = amf_encode_object_variable( "fpad",
461         AMF_DATATYPE_BOOLEAN, &AMF_BOOLEAN_FALSE );
462     rtmp_body_append( rtmp_body, tmp_buffer,
463         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "fpad" ) +
464         AMF_DATATYPE_SIZE_BOOLEAN );
465     free( tmp_buffer );
466
467     tmp_buffer = amf_encode_object_variable( "audioCodecs",
468         AMF_DATATYPE_NUMBER, &AMF_CALL_NETCONNECTION_CONNECT_AUDIOCODECS );
469     rtmp_body_append( rtmp_body, tmp_buffer,
470         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audioCodecs" ) +
471         AMF_DATATYPE_SIZE_NUMBER );
472     free( tmp_buffer );
473
474     tmp_buffer = amf_encode_object_variable( "videoCodecs",
475         AMF_DATATYPE_NUMBER, &AMF_CALL_NETCONNECTION_CONNECT_VIDEOCODECS );
476     rtmp_body_append( rtmp_body, tmp_buffer,
477         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "videoCodecs" ) +
478         AMF_DATATYPE_SIZE_NUMBER );
479     free( tmp_buffer );
480
481     tmp_buffer = amf_encode_object_variable( "videoFunction",
482         AMF_DATATYPE_NUMBER, &AMF_CALL_NETCONNECTION_CONNECT_VIDEOFUNCTION );
483     rtmp_body_append( rtmp_body, tmp_buffer,
484         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "videoFunction" ) +
485         AMF_DATATYPE_SIZE_NUMBER );
486     free( tmp_buffer );
487
488     tmp_buffer = amf_encode_object_variable( "pageUrl",
489         AMF_DATATYPE_STRING, p_thread->psz_page_url );
490     rtmp_body_append( rtmp_body, tmp_buffer,
491         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "pageUrl" ) +
492         AMF_DATATYPE_SIZE_STRING + strlen( p_thread->psz_page_url ) );
493     free( tmp_buffer );
494
495     tmp_buffer = amf_encode_object_variable( "objectEncoding",
496         AMF_DATATYPE_NUMBER, &AMF_CALL_NETCONNECTION_CONNECT_OBJECTENCODING );
497     rtmp_body_append( rtmp_body, tmp_buffer,
498         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "objectEncoding" ) +
499         AMF_DATATYPE_SIZE_NUMBER );
500     free( tmp_buffer );
501
502     tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
503     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
504     free( tmp_buffer );
505
506     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
507         0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
508     free( rtmp_body->body );
509     free( rtmp_body );
510
511     tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
512
513     /* Call NetConnection.connect */
514     if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
515                      "failed send call NetConnection.connect" ) )
516         return -1;
517
518     /* Wait for NetConnection.connect result */
519     vlc_mutex_lock( &p_thread->lock );
520     vlc_cond_wait( &p_thread->wait, &p_thread->lock );
521     vlc_mutex_unlock( &p_thread->lock );
522
523     if( p_thread->result_connect )
524     {
525         msg_Err( p_thread, "failed call NetConnection.connect" );
526         return -1;
527     }
528
529     /* Force control thread to stop if receive NetStream.play call and wait is not ready */
530     vlc_mutex_lock( &p_thread->lock );
531
532     /* Build NetStream.createStream call */
533     rtmp_body = rtmp_body_new( -1 );
534
535     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "createStream" );
536     rtmp_body_append( rtmp_body, tmp_buffer, 
537         AMF_DATATYPE_SIZE_STRING + strlen( "createStream" ) );
538     free( tmp_buffer );
539
540     p_thread->stream_client_id = RTMP_DEFAULT_STREAM_CLIENT_ID;
541
542     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
543         &AMF_CALL_STREAM_CLIENT_NUMBER );
544     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
545     free( tmp_buffer );
546
547     tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
548     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
549     free( tmp_buffer );
550
551     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE, 
552         0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
553     free( rtmp_body->body );
554     free( rtmp_body );
555
556     tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
557
558     /* Call NetStream.createStream */
559     if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
560                      "failed send call NetStream.createStream" ) )
561         return -1;
562
563 /*TODO: read server stream number*/
564     /* Build ping packet */
565     rtmp_body = rtmp_body_new( -1 );
566
567     tmp_buffer = rtmp_encode_ping( RTMP_PING_BUFFER_TIME_CLIENT, RTMP_SRC_DST_CONNECT_OBJECT, RTMP_TIME_CLIENT_BUFFER, 0 );
568     rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_BUFFER_TIME_CLIENT );
569     free( tmp_buffer );
570
571     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
572         0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
573     free( rtmp_body->body );
574     free( rtmp_body );
575
576     tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
577
578     /* Send ping packet */
579     if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
580                      "failed send ping BUFFER_TIME_CLIENT" ) )
581         return -1;
582
583     /* Build NetStream.play call */
584     rtmp_body = rtmp_body_new( -1 );
585
586     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "play" );
587     rtmp_body_append( rtmp_body, tmp_buffer,
588         AMF_DATATYPE_SIZE_STRING + strlen( "play" ) );
589     free( tmp_buffer );
590
591     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
592         &AMF_CALL_NETSTREAM_PLAY );
593     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
594     free( tmp_buffer );
595
596     tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
597     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
598     free( tmp_buffer );
599
600     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, p_thread->psz_media );
601     rtmp_body_append( rtmp_body, tmp_buffer,
602         AMF_DATATYPE_SIZE_STRING + strlen( p_thread->psz_media ) );
603     free( tmp_buffer );
604
605     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
606         0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
607     free( rtmp_body->body );
608     free( rtmp_body );
609
610     tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
611
612     /* Call NetStream.play */
613     if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
614                      "failed send call NetStream.play" ) )
615         return -1;
616
617
618     /* Build ping packet */
619     rtmp_body = rtmp_body_new( -1 );
620
621     tmp_buffer = rtmp_encode_ping( RTMP_PING_BUFFER_TIME_CLIENT, RTMP_SRC_DST_CONNECT_OBJECT2, RTMP_TIME_CLIENT_BUFFER, 0 );
622     rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_BUFFER_TIME_CLIENT );
623     free( tmp_buffer );
624
625     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
626         0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
627     free( rtmp_body->body );
628     free( rtmp_body );
629
630     tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
631
632     /* Send ping packet */
633     if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
634                      "failed send ping BUFFER_TIME_CLIENT" ) )
635         return -1;
636
637     /* Wait for NetStream.play.start result */
638     vlc_cond_wait( &p_thread->wait, &p_thread->lock );
639     vlc_mutex_unlock( &p_thread->lock );
640
641     if( p_thread->result_play )
642     {
643         msg_Err( p_thread, "failed call NetStream.play" );
644         return -1;
645     }
646
647     /* Next packet is the beginning of flv stream */
648     msg_Dbg( p_thread, "next packet is the beginning of flv stream" );
649
650     return 0;
651 }
652
653 int
654 rtmp_connect_passive( rtmp_control_thread_t *p_thread )
655 {
656     /* Force control thread to stop if receive NetStream.play call and wait is not ready */
657     vlc_mutex_lock( &p_thread->lock );
658
659     /* Wait for NetStream.play.start result */
660     vlc_cond_wait( &p_thread->wait, &p_thread->lock );
661     vlc_mutex_unlock( &p_thread->lock );
662
663     if( p_thread->result_play )
664     {
665         msg_Err( p_thread, "failed call NetStream.play" );
666         return -1;
667     }
668
669     return 0;
670 }
671
672 static void
673 rtmp_packet_free( rtmp_packet_t *pkt )
674 {
675     free( pkt->body->body );
676     free( pkt->body );
677     free( pkt );
678 }
679
680 /* TODO
681 int
682 rtmp_seek( access_t *p_access, int64_t i_pos )
683 {
684     access_sys_t *p_sys = p_access->p_sys;
685     rtmp_packet_t *rtmp_packet;
686     rtmp_body_t *rtmp_body;
687     uint8_t *tmp_buffer;
688     uint64_t tmp_number;
689     ssize_t i_ret;
690 msg_Warn(p_access, "i_pos %lld", i_pos);
691     // Build NetStream.seek call //
692     rtmp_body = rtmp_body_new();
693
694     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "seek" );
695     rtmp_body_append( rtmp_body, tmp_buffer,
696         AMF_DATATYPE_SIZE_STRING + strlen( "seek" ) );
697     free( tmp_buffer );
698
699     tmp_number = 0;
700     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
701         &tmp_number );
702     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
703     free( tmp_buffer );
704
705     tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
706     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
707     free( tmp_buffer );
708 //TODO: convert i_pos to double and see if they are milliseconds
709     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
710         &i_pos );
711     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
712     free( tmp_buffer );
713
714     rtmp_packet = rtmp_new_packet( p_sys->p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
715         0, RTMP_DATATYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
716     free( rtmp_body->body );
717     free( rtmp_body );
718
719     tmp_buffer = rtmp_encode_packet( p_access, rtmp_packet ); 
720
721     // Call NetStream.seek //
722     if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
723                      "failed call NetStream.seek" ) )
724         return -1;
725
726     // Receive TODO: see what //
727     rtmp_packet = rtmp_read_net_packet( p_access, p_sys->fd );
728     rtmp_packet_free( rtmp_packet );
729
730     return 0;
731 }
732 */
733
734 rtmp_packet_t *
735 rtmp_build_bytes_read( rtmp_control_thread_t *p_thread, uint32_t reply )
736 {
737     rtmp_packet_t *rtmp_packet;
738     rtmp_body_t *rtmp_body;
739     uint8_t *tmp_buffer;
740
741     /* Build bytes read packet */
742     rtmp_body = rtmp_body_new( -1 );
743
744     tmp_buffer = (uint8_t *) malloc( sizeof( uint32_t ) );
745     if( !tmp_buffer ) return NULL;
746
747     reply = hton32( reply );
748     memcpy( tmp_buffer, &reply, sizeof( uint32_t ) );
749
750     rtmp_body_append( rtmp_body, tmp_buffer, sizeof( uint32_t ) );
751     free( tmp_buffer );
752
753     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
754         0, RTMP_CONTENT_TYPE_BYTES_READ, 0, rtmp_body );
755     free( rtmp_body->body );
756     free( rtmp_body );
757
758     return rtmp_packet;
759 }
760
761 rtmp_packet_t *
762 rtmp_build_publish_start( rtmp_control_thread_t *p_thread )
763 {
764     rtmp_packet_t *rtmp_packet;
765     rtmp_body_t *rtmp_body;
766     uint8_t *tmp_buffer;
767
768     /* Build publish start event */
769     rtmp_body = rtmp_body_new( -1 );
770
771     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onStatus" );
772     rtmp_body_append( rtmp_body, tmp_buffer,
773         AMF_DATATYPE_SIZE_STRING + strlen( "onStatus" ) );
774     free( tmp_buffer );
775
776     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
777         &p_thread->stream_server_id );
778     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
779     free( tmp_buffer );
780
781     tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
782     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
783     free( tmp_buffer );
784
785     tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
786     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
787     free( tmp_buffer );
788
789     tmp_buffer = amf_encode_object_variable( "level",
790         AMF_DATATYPE_STRING, "status" );
791     rtmp_body_append( rtmp_body, tmp_buffer,
792         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
793         AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
794     free ( tmp_buffer );
795
796     tmp_buffer = amf_encode_object_variable( "code",
797         AMF_DATATYPE_STRING, "NetStream.Publish.Start" );
798     rtmp_body_append( rtmp_body, tmp_buffer,
799         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
800         AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Publish.Start" ) );
801     free ( tmp_buffer );
802
803     tmp_buffer = amf_encode_object_variable( "description",
804         AMF_DATATYPE_STRING, "" );
805     rtmp_body_append( rtmp_body, tmp_buffer,
806         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
807         AMF_DATATYPE_SIZE_STRING + strlen( "" ) );
808     free ( tmp_buffer );
809
810     tmp_buffer = amf_encode_object_variable( "details",
811         AMF_DATATYPE_STRING, p_thread->psz_publish );
812     rtmp_body_append( rtmp_body, tmp_buffer,
813         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "details" ) +
814         AMF_DATATYPE_SIZE_STRING + strlen( p_thread->psz_publish ) );
815     free ( tmp_buffer );
816
817     tmp_buffer = amf_encode_object_variable( "clientid",
818         AMF_DATATYPE_NUMBER, &p_thread->stream_client_id );
819     rtmp_body_append( rtmp_body, tmp_buffer,
820         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "clientid" ) +
821         AMF_DATATYPE_SIZE_NUMBER );
822     free( tmp_buffer );
823
824     tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
825     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
826     free( tmp_buffer );
827
828     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
829         0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
830     free( rtmp_body->body );
831     free( rtmp_body );
832
833     return rtmp_packet;
834 }
835
836 rtmp_packet_t *
837 rtmp_build_flv_over_rtmp( rtmp_control_thread_t *p_thread, block_t *p_buffer )
838 {
839     rtmp_packet_t *rtmp_packet;
840
841     if( p_thread->flv_length_body > 0 )
842     {
843         p_thread->flv_length_body -= p_buffer->i_buffer;
844         rtmp_body_append( p_thread->flv_body, p_buffer->p_buffer, p_buffer->i_buffer );
845
846         if( p_thread->flv_length_body > 0 )
847             return NULL;
848
849     }
850     else
851     {
852         p_thread->flv_content_type = *p_buffer->p_buffer;
853
854         p_buffer->p_buffer[0] = 0;
855         p_thread->flv_length_body = ntoh32( *(uint32_t *) (p_buffer->p_buffer) );
856
857         p_buffer->p_buffer[3] = 0;
858         p_thread->flv_timestamp = ntoh32( *(uint32_t *) (p_buffer->p_buffer + 3) );
859     }
860
861     if( p_thread->flv_length_body > p_buffer->i_buffer - FLV_TAG_SIZE - FLV_TAG_PREVIOUS_TAG_SIZE )
862     {
863         p_thread->flv_length_body -= p_buffer->i_buffer - FLV_TAG_SIZE - FLV_TAG_PREVIOUS_TAG_SIZE;
864         rtmp_body_append( p_thread->flv_body, p_buffer->p_buffer + FLV_TAG_SIZE, p_buffer->i_buffer - FLV_TAG_SIZE );
865
866         return NULL;
867     }
868     else
869     {
870         rtmp_body_append( p_thread->flv_body, p_buffer->p_buffer + FLV_TAG_SIZE, p_thread->flv_length_body );
871     }
872
873     rtmp_packet = rtmp_new_packet( p_thread, rtmp_get_stream_index( p_thread->flv_content_type ),
874         p_thread->flv_timestamp, p_thread->flv_content_type, RTMP_SRC_DST_DEFAULT, p_thread->flv_body );
875     p_thread->flv_length_body = 0;
876     rtmp_body_reset( p_thread->flv_body );
877
878     return rtmp_packet;
879 }
880
881 /* This function must be cancellation-safe! */
882 rtmp_packet_t *
883 rtmp_read_net_packet( rtmp_control_thread_t *p_thread )
884 {
885     int length_header;
886     int stream_index;
887     size_t bytes_left;
888     uint8_t p_read[12];
889     rtmp_packet_t *header;
890     ssize_t i_ret;
891
892     for(;;)
893     {
894         i_ret = net_Read( p_thread, p_thread->fd, NULL, p_read, 1, true );
895         if( i_ret != 1 )
896             goto error;
897
898         length_header = rtmp_decode_header_size( VLC_OBJECT(p_thread),
899                                           p_read[0] & RTMP_HEADER_SIZE_MASK );
900         stream_index = p_read[0] & RTMP_HEADER_STREAM_INDEX_MASK;
901         header = p_thread->rtmp_headers_recv+stream_index;
902
903         i_ret = net_Read( p_thread, p_thread->fd, NULL, p_read + 1, length_header - 1, true );
904         if( i_ret != length_header - 1 )
905             goto error;
906
907         /* Update timestamp if not is an interchunk packet */
908         if( length_header == 1 && header->body == NULL )
909         {
910             header->timestamp += header->timestamp_relative;
911         }
912
913         /* Length 4 and 8 headers have relative timestamp */
914         if( length_header == 4 || length_header == 8 )
915         {
916             p_read[0] = 0;
917
918             header->timestamp_relative = ntoh32( *(uint32_t *) p_read );
919             header->timestamp += header->timestamp_relative;
920         }
921
922         if( length_header >= 8 )
923         {
924             p_read[3] = 0;
925
926             header->length_body = ntoh32( *(uint32_t *) (p_read + 3) );
927             header->content_type = p_read[7];
928         }
929
930         /* Length 12 headers have absolute timestamp */
931         if( length_header >= 12 )
932         {
933             p_read[0] = 0;
934
935             header->timestamp = ntoh32( *(uint32_t *) p_read );
936             header->src_dst = ntoh32( *(uint32_t *) (p_read + 8) );
937         }
938
939         if( header->body == NULL )
940         {
941             header->body = rtmp_body_new( header->length_body );
942         }
943
944         bytes_left = header->body->length_buffer - header->body->length_body;
945
946         if( bytes_left > p_thread->chunk_size_recv )
947             bytes_left = p_thread->chunk_size_recv;
948
949         i_ret = net_Read( p_thread, p_thread->fd, NULL,
950             header->body->body + header->body->length_body, bytes_left, true );
951
952         if( i_ret != (ssize_t)bytes_left )
953             goto error;
954
955         header->body->length_body += bytes_left;
956
957         if( header->length_body == header->body->length_body )
958         {
959             rtmp_packet_t *rpkt = (rtmp_packet_t*)malloc(sizeof(rtmp_packet_t));
960             if( !rpkt ) return NULL;
961
962             rpkt->stream_index       = stream_index;
963             rpkt->timestamp          = header->timestamp;
964             rpkt->timestamp_relative = header->timestamp_relative;
965             rpkt->content_type       = header->content_type;
966             rpkt->src_dst            = header->src_dst;
967             rpkt->length_body        = header->length_body;
968             rpkt->body               = header->body;
969
970             header->body = NULL;
971
972             return rpkt;
973         }
974     }
975
976 error:
977     msg_Err( p_thread, "rtmp_read_net_packet: net_Read error");
978     return NULL;
979 }
980
981
982 static void
983 rtmp_handler_null( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
984 {
985     VLC_UNUSED(p_thread);
986     rtmp_packet_free( rtmp_packet );
987 }
988
989 static void
990 rtmp_handler_chunk_size( rtmp_control_thread_t *p_thread,
991                          rtmp_packet_t *rtmp_packet )
992 {
993     p_thread->chunk_size_recv =
994                             ntoh32( *(uint32_t *) (rtmp_packet->body->body) );
995     rtmp_packet_free( rtmp_packet );
996 }
997
998 static void
999 rtmp_handler_audio_data( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1000 {
1001     block_t *p_buffer;
1002
1003     if( !p_thread->has_audio )
1004     {
1005         p_thread->has_audio = 1;
1006
1007         flv_get_metadata_audio( p_thread, rtmp_packet,
1008             &p_thread->metadata_stereo, &p_thread->metadata_samplesize,
1009             &p_thread->metadata_samplerate, &p_thread->metadata_audiocodecid );
1010     }
1011
1012     flv_rebuild( p_thread, rtmp_packet );
1013     p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
1014     block_FifoPut( p_thread->p_fifo_input, p_buffer );
1015
1016     rtmp_packet_free( rtmp_packet );
1017 }
1018
1019 static void
1020 rtmp_handler_video_data( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1021 {
1022     block_t *p_buffer;
1023
1024     if( !p_thread->has_video )
1025     {
1026         p_thread->has_video = 1;
1027
1028         flv_get_metadata_video( p_thread, rtmp_packet,
1029             &p_thread->metadata_videocodecid, &p_thread->metadata_frametype );
1030     }
1031
1032     flv_rebuild( p_thread, rtmp_packet );
1033     p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
1034     block_FifoPut( p_thread->p_fifo_input, p_buffer );
1035
1036     rtmp_packet_free( rtmp_packet );
1037 }
1038
1039 static void
1040 rtmp_handler_notify( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1041 {
1042     block_t *p_buffer;
1043
1044     p_thread->metadata_received = 1;
1045
1046     flv_rebuild( p_thread, rtmp_packet );
1047     p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
1048     block_FifoPut( p_thread->p_fifo_input, p_buffer );
1049
1050     rtmp_packet_free( rtmp_packet );
1051 }
1052
1053 static void
1054 rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1055 {
1056     rtmp_packet_t *tmp_rtmp_packet;
1057     uint8_t *i, *end, *tmp_buffer;
1058     double number;
1059     char *string, *string2;
1060     ssize_t i_ret;
1061
1062     i = rtmp_packet->body->body;
1063     end = rtmp_packet->body->body + rtmp_packet->body->length_body;
1064
1065     i++; /* Pass over AMF_DATATYPE_STRING */
1066     string = amf_decode_string( &i );
1067     if( !string )
1068     {
1069         msg_Err(p_thread,"Seriously broken stream");
1070         return;
1071     }
1072
1073     i++; /* Pass over AMF_DATATYPE_NUMBER */
1074     number = amf_decode_number( &i );
1075
1076     msg_Dbg( p_thread, "%s %.1f", string, number );
1077
1078     if( strcmp( "connect", string ) == 0 )
1079     {
1080         /* Connection bandwith */
1081         tmp_rtmp_packet = rtmp_encode_onBWDone( p_thread, AMF_CALL_ONBWDONE );
1082
1083         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1084
1085         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1086         if( i_ret != tmp_rtmp_packet->length_encoded )
1087         {
1088             msg_Err( p_thread, "failed send connection bandwith" );
1089             goto error;
1090         }
1091         rtmp_packet_free( rtmp_packet );
1092         free( tmp_buffer );
1093
1094         /* Server bandwith */
1095         tmp_rtmp_packet = rtmp_encode_server_bw( p_thread, RTMP_SERVER_BW );
1096
1097         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1098
1099         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1100         if( i_ret != tmp_rtmp_packet->length_encoded )
1101         {
1102             msg_Err( p_thread, "failed send server bandwith" );
1103             goto error;
1104         }
1105         rtmp_packet_free( rtmp_packet );
1106         free( tmp_buffer );
1107
1108         /* Clear stream */
1109         tmp_rtmp_packet = rtmp_encode_ping_clear_stream( p_thread, RTMP_SRC_DST_CONNECT_OBJECT );
1110
1111         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1112
1113         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1114         if( i_ret != tmp_rtmp_packet->length_encoded )
1115         {
1116             msg_Err( p_thread, "failed send clear stream" );
1117             goto error;
1118         }
1119         rtmp_packet_free( rtmp_packet );
1120         free( tmp_buffer );
1121
1122         /* Reply NetConnection.connect */
1123         tmp_rtmp_packet = rtmp_encode_NetConnection_connect_result( p_thread, number );
1124
1125         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1126
1127         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1128         if( i_ret != tmp_rtmp_packet->length_encoded )
1129         {
1130             msg_Err( p_thread, "failed send reply NetConnection.connect" );
1131             goto error;
1132         }
1133         rtmp_packet_free( rtmp_packet );
1134         free( tmp_buffer );
1135     }
1136     else if( strcmp( "createStream", string ) == 0 )
1137     {
1138         p_thread->stream_client_id = number;
1139         p_thread->stream_server_id = RTMP_DEFAULT_STREAM_SERVER_ID;
1140
1141         /* Reply createStream */
1142         tmp_rtmp_packet = rtmp_encode_createStream_result( p_thread, p_thread->stream_client_id, p_thread->stream_server_id );
1143
1144         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1145
1146         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1147         if( i_ret != tmp_rtmp_packet->length_encoded )
1148         {
1149             msg_Err( p_thread, "failed send reply createStream" );
1150             goto error;
1151         }
1152         rtmp_packet_free( rtmp_packet );
1153         free( tmp_buffer );
1154
1155         /* Reset stream */
1156         tmp_rtmp_packet = rtmp_encode_ping_reset_stream( p_thread );
1157
1158         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1159
1160         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1161         if( i_ret != tmp_rtmp_packet->length_encoded )
1162         {
1163             msg_Err( p_thread, "failed send reset stream" );
1164             goto error;
1165         }
1166         rtmp_packet_free( rtmp_packet );
1167         free( tmp_buffer );
1168
1169         /* Clear stream */
1170         tmp_rtmp_packet = rtmp_encode_ping_clear_stream( p_thread, RTMP_SRC_DST_CONNECT_OBJECT2 );
1171
1172         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1173     
1174         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1175         if( i_ret != tmp_rtmp_packet->length_encoded )
1176         {
1177             msg_Err( p_thread, "failed send clear stream" );
1178             goto error;
1179         }
1180         rtmp_packet_free( rtmp_packet );
1181         free( tmp_buffer );
1182     }
1183     else if( strcmp( "publish", string ) == 0 )
1184     {
1185         i++;
1186         msg_Dbg( p_thread, "null" );
1187
1188         i++;
1189         string2 = amf_decode_string( &i );
1190         msg_Dbg( p_thread, "string: %s", string2 );
1191
1192         p_thread->psz_publish = strdup( string2 );
1193
1194         free( string2 );
1195     }
1196     else if( strcmp( "play", string ) == 0 )
1197     {
1198         i++;
1199         msg_Dbg( p_thread, "null" );
1200
1201         i++;
1202         string2 = amf_decode_string( &i );
1203         msg_Dbg( p_thread, "string: %s", string2 );
1204
1205         /* Reply NetStream.play.reset */
1206         tmp_rtmp_packet = rtmp_encode_NetStream_play_reset_onStatus( p_thread, string2 );
1207
1208         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1209
1210         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1211         if( i_ret != tmp_rtmp_packet->length_encoded )
1212         {
1213             msg_Err( p_thread, "failed send reply NetStream.play.reset" );
1214             goto error;
1215         }
1216         rtmp_packet_free( rtmp_packet );
1217         free( tmp_buffer );
1218
1219         /* Reply NetStream.play.start */
1220         tmp_rtmp_packet = rtmp_encode_NetStream_play_start_onStatus( p_thread, string2 );
1221
1222         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1223
1224         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1225         if( i_ret != tmp_rtmp_packet->length_encoded )
1226         {
1227             msg_Err( p_thread, "failed send reply NetStream.play.start" );
1228             goto error;
1229         }
1230         rtmp_packet_free( rtmp_packet );
1231         free( tmp_buffer );
1232
1233         free( string2 );
1234
1235         p_thread->result_play = 0;
1236
1237         vlc_mutex_lock( &p_thread->lock );
1238         vlc_cond_signal( &p_thread->wait );
1239         vlc_mutex_unlock( &p_thread->lock );
1240     }
1241
1242     free( string );
1243
1244     while( i < end )
1245     {
1246         if( *i == AMF_DATATYPE_NUMBER )
1247         {
1248             i++;
1249             msg_Dbg( p_thread, "number: %le", amf_decode_number( &i ) );
1250         }
1251         else if( *i == AMF_DATATYPE_BOOLEAN )
1252         {
1253             i++;
1254             msg_Dbg( p_thread, "boolean: %s", amf_decode_boolean( &i ) ? "true" : "false" );
1255         }
1256         else if( *i == AMF_DATATYPE_STRING )
1257         {
1258             i++;
1259             string = amf_decode_string( &i );
1260             msg_Dbg( p_thread, "string: %s", string );
1261             free( string );
1262         }
1263         else if( *i == AMF_DATATYPE_OBJECT )
1264         {
1265             i++;
1266             msg_Dbg( p_thread, "object" );
1267             while( ( string = amf_decode_object( &i ) ) != NULL )
1268             {
1269                 if( *i == AMF_DATATYPE_NUMBER )
1270                 {
1271                     i++;
1272                     msg_Dbg( p_thread, "key: %s value: %le", string, amf_decode_number( &i ) );
1273                 }
1274                 else if( *i == AMF_DATATYPE_BOOLEAN )
1275                 {
1276                     i++;
1277                     msg_Dbg( p_thread, "key: %s value: %s", string, amf_decode_boolean( &i ) ? "true" : "false" );
1278                 }
1279                 else if( *i == AMF_DATATYPE_STRING )
1280                 {
1281                     i++;
1282                     string2 = amf_decode_string( &i );
1283                     msg_Dbg( p_thread, "key: %s value: %s", string, string2 );
1284                     if( strcmp( "code", string ) == 0 )
1285                     {
1286 #warning Locking bugs here.
1287                         if( strcmp( "NetConnection.Connect.Success", string2 ) == 0 )
1288                         {
1289                             p_thread->result_connect = 0;
1290
1291                             vlc_mutex_lock( &p_thread->lock );
1292                             vlc_cond_signal( &p_thread->wait );
1293                             vlc_mutex_unlock( &p_thread->lock );
1294                         }
1295                         else if( strcmp( "NetConnection.Connect.InvalidApp", string2 ) == 0 )
1296                         {
1297                             vlc_mutex_lock( &p_thread->lock );
1298                             vlc_cond_signal( &p_thread->wait );
1299                             vlc_mutex_unlock( &p_thread->lock );
1300                         }
1301                         else if( strcmp( "NetStream.Play.Start", string2 ) == 0 )
1302                         {
1303                             p_thread->result_play = 0;
1304
1305                             vlc_mutex_lock( &p_thread->lock );
1306                             vlc_cond_signal( &p_thread->wait );
1307                             vlc_mutex_unlock( &p_thread->lock );
1308                         }
1309                         else if( strcmp( "NetStream.Play.Stop", string2 ) == 0 )
1310                         {
1311                             p_thread->result_stop = 1;
1312
1313                             block_FifoWake( p_thread->p_fifo_input );
1314                         }
1315                     }
1316                     free( string2 );
1317                 }
1318                 else if( *i == AMF_DATATYPE_NULL )
1319                 {
1320                     i++;
1321                     msg_Dbg( p_thread, "key: %s value: Null", string );
1322                 }
1323                 else if( *i == AMF_DATATYPE_UNDEFINED )
1324                 {
1325                     i++;
1326                     msg_Dbg( p_thread, "key: %s value: undefined (Null)", string );
1327                 }
1328                 else
1329                 {
1330                     i++;
1331                     msg_Warn( p_thread, "key: %s value: undefined AMF type", string );
1332                 }
1333                 free( string );
1334             }
1335             msg_Dbg( p_thread, "end of object" );
1336         }
1337         else if( *i == AMF_DATATYPE_NULL)
1338         {
1339             i++;
1340             msg_Dbg( p_thread, "null" );
1341         }
1342         else if( *i == AMF_DATATYPE_UNDEFINED )
1343         {
1344             i++;
1345             msg_Dbg( p_thread, "undefined (null)" );
1346         }
1347         else
1348         {
1349             i++;
1350             msg_Warn( p_thread, "undefined AMF type" );
1351         }
1352     }
1353     
1354     rtmp_packet_free( rtmp_packet );
1355     return;
1356
1357 error:
1358     free( string );
1359     rtmp_packet_free( rtmp_packet );
1360     free( tmp_buffer );
1361 }
1362
1363 /* length header calculated automatically based on last packet in the same channel */
1364 /* timestamps passed are always absolute */
1365 static rtmp_packet_t *
1366 rtmp_new_packet( rtmp_control_thread_t *p_thread, uint8_t stream_index,
1367                  uint32_t timestamp, uint8_t content_type,
1368                  uint32_t src_dst, rtmp_body_t *body )
1369 {
1370     int interchunk_headers;
1371     rtmp_packet_t *rtmp_packet;
1372     rtmp_packet_t *rtmp_send = p_thread->rtmp_headers_send+stream_index;
1373
1374     rtmp_packet = (rtmp_packet_t *) malloc( sizeof( rtmp_packet_t ) );
1375     if( !rtmp_packet ) return NULL;
1376
1377     interchunk_headers = body->length_body / p_thread->chunk_size_send;
1378     if( body->length_body % p_thread->chunk_size_send == 0 )
1379         interchunk_headers--;
1380
1381     if( src_dst != rtmp_send->src_dst )
1382     {
1383         rtmp_send->timestamp = timestamp;
1384         rtmp_send->length_body = body->length_body;
1385         rtmp_send->content_type = content_type;
1386         rtmp_send->src_dst = src_dst;
1387         
1388         rtmp_packet->length_header = 12;
1389     }
1390     else if( content_type != rtmp_send->content_type
1391         || body->length_body != rtmp_send->length_body )
1392     {
1393         rtmp_send->timestamp_relative =
1394             timestamp - rtmp_send->timestamp;
1395         rtmp_send->timestamp = timestamp;
1396         rtmp_send->length_body = body->length_body;
1397         rtmp_send->content_type = content_type;
1398
1399         rtmp_packet->length_header = 8;
1400     }
1401     else if( timestamp != rtmp_send->timestamp )
1402     {
1403         rtmp_send->timestamp_relative =
1404             timestamp - rtmp_send->timestamp;
1405         rtmp_send->timestamp = timestamp;
1406
1407         rtmp_packet->length_header = 4;
1408     }
1409     else
1410     {
1411         rtmp_packet->length_header = 1;
1412     }
1413 /*TODO: puede que no haga falta guardar el timestamp relative */
1414     rtmp_packet->stream_index = stream_index;
1415     if( rtmp_packet->length_header == 12 )
1416     {
1417         rtmp_packet->timestamp = timestamp;
1418         rtmp_packet->timestamp_relative = 0;
1419     }
1420     else
1421     {
1422         rtmp_packet->timestamp = timestamp;
1423         rtmp_packet->timestamp_relative = rtmp_send->timestamp_relative;
1424     }
1425
1426     rtmp_packet->length_encoded = rtmp_packet->length_header
1427                                 + body->length_body + interchunk_headers;
1428     rtmp_packet->length_body = body->length_body;
1429     rtmp_packet->content_type = content_type;
1430     rtmp_packet->src_dst = src_dst;
1431
1432     rtmp_packet->body = (rtmp_body_t *) malloc( sizeof( rtmp_body_t ) );
1433     if( !rtmp_packet->body )
1434     {
1435        free( rtmp_packet );
1436        return NULL;
1437     }
1438
1439     rtmp_packet->body->length_body = body->length_body;
1440     rtmp_packet->body->length_buffer = body->length_body;
1441     rtmp_packet->body->body = (uint8_t *) malloc( rtmp_packet->body->length_buffer * sizeof( uint8_t ) );
1442     if( !rtmp_packet->body->body )
1443     {
1444         free( rtmp_packet->body );
1445         free( rtmp_packet );
1446         return NULL;
1447     }
1448     memcpy( rtmp_packet->body->body, body->body, rtmp_packet->body->length_body );
1449
1450     return rtmp_packet;
1451 }
1452
1453 static block_t *
1454 rtmp_new_block( rtmp_control_thread_t *p_thread, uint8_t *buffer, int32_t length_buffer )
1455 {
1456     block_t *p_buffer;
1457     /* DOWN: p_thread->p_empty_blocks->i_depth */
1458     while ( block_FifoCount( p_thread->p_empty_blocks ) > MAX_EMPTY_BLOCKS )
1459     {
1460         p_buffer = block_FifoGet( p_thread->p_empty_blocks );
1461         block_Release( p_buffer );
1462     }
1463     /* DOWN: p_thread->p_empty_blocks->i_depth */
1464     if( block_FifoCount( p_thread->p_empty_blocks ) == 0 )
1465     {
1466         p_buffer = block_New( p_thread, length_buffer );
1467     }
1468     else
1469     {
1470         p_buffer = block_FifoGet( p_thread->p_empty_blocks );
1471         p_buffer = block_Realloc( p_buffer, 0, length_buffer );
1472     }
1473
1474     p_buffer->i_buffer = length_buffer;
1475
1476     memcpy( p_buffer->p_buffer, buffer, p_buffer->i_buffer );
1477
1478     return p_buffer;
1479 }
1480
1481 /* Call sequence for each packet:
1482  * rtmp_new_packet -> rtmp_encode_packet -> send .
1483  * No parallelism allowed because of optimization in header length. */
1484
1485 uint8_t *
1486 rtmp_encode_packet( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1487 {
1488     uint8_t *out;
1489     int interchunk_headers;
1490     uint32_t timestamp, length_body, src_dst;
1491     int i, j;
1492
1493     out = (uint8_t *) malloc( rtmp_packet->length_encoded * sizeof( uint8_t ) );
1494     if( !out ) return NULL;
1495
1496     interchunk_headers = rtmp_packet->body->length_body / p_thread->chunk_size_send;
1497     if( rtmp_packet->body->length_body % p_thread->chunk_size_send == 0 )
1498         interchunk_headers--;
1499
1500     if( rtmp_packet->length_header == 12 )
1501     {
1502         /* Timestamp absolute */
1503         timestamp = hton32( rtmp_packet->timestamp );
1504         memcpy( out, &timestamp, sizeof( uint32_t ) );
1505
1506         src_dst = hton32( rtmp_packet->src_dst );
1507         memcpy( out + 8, &src_dst, sizeof( uint32_t ) );
1508     }
1509
1510     if( rtmp_packet->length_header >= 8 )
1511     {
1512         /* Length without inter chunk headers */
1513         length_body = hton32( rtmp_packet->body->length_body );
1514         memcpy( out + 3, &length_body, sizeof( uint32_t ) );
1515
1516         out[7] = rtmp_packet->content_type;
1517     }
1518     if( rtmp_packet->length_header >= 4 && rtmp_packet->length_header != 12 )
1519     {
1520         /* Timestamp relative */
1521         timestamp = hton32( rtmp_packet->timestamp_relative );
1522         memcpy( out, &timestamp, sizeof( uint32_t ) );
1523     }
1524
1525     out[0] = rtmp_encode_header_size( (vlc_object_t *) p_thread, rtmp_packet->length_header ) + rtmp_packet->stream_index;
1526
1527     /* Insert inter chunk headers */
1528     for(i = 0, j = 0; i < rtmp_packet->body->length_body + interchunk_headers; i++, j++)
1529     {
1530         if( j % p_thread->chunk_size_send == 0 && j != 0 )
1531             out[rtmp_packet->length_header + i++] = RTMP_HEADER_SIZE_1 + rtmp_packet->stream_index;
1532         out[rtmp_packet->length_header + i] = rtmp_packet->body->body[j];
1533     }
1534
1535     return out;
1536 }
1537
1538 static rtmp_packet_t *
1539 rtmp_encode_onBWDone( rtmp_control_thread_t *p_thread, double number )
1540 {
1541     rtmp_packet_t *rtmp_packet;
1542     rtmp_body_t *rtmp_body;
1543     uint8_t *tmp_buffer;
1544
1545     /* Build onBWDone */
1546     rtmp_body = rtmp_body_new( -1 );
1547
1548     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onBWDone" );
1549     rtmp_body_append( rtmp_body, tmp_buffer,
1550         AMF_DATATYPE_SIZE_STRING + strlen( "onBWDone" ) );
1551     free( tmp_buffer );
1552
1553     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1554     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1555     free( tmp_buffer );
1556
1557     tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1558     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1559     free( tmp_buffer );
1560
1561     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1562         0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_CONNECT_OBJECT, rtmp_body );
1563     free( rtmp_body->body );
1564     free( rtmp_body );
1565
1566     return rtmp_packet;
1567 }
1568
1569 static rtmp_packet_t *
1570 rtmp_encode_server_bw( rtmp_control_thread_t *p_thread, uint32_t number )
1571 {
1572     rtmp_packet_t *rtmp_packet;
1573     rtmp_body_t *rtmp_body;
1574
1575     /* Build server bw */
1576     rtmp_body = rtmp_body_new( -1 );
1577
1578     rtmp_body_append( rtmp_body, (uint8_t *) &number, sizeof( uint32_t ) );
1579
1580     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1581       0, RTMP_CONTENT_TYPE_SERVER_BW, RTMP_SRC_DST_CONNECT_OBJECT, rtmp_body );
1582     free( rtmp_body->body );
1583     free( rtmp_body );
1584
1585     return rtmp_packet;
1586 }
1587
1588 static rtmp_packet_t *
1589 rtmp_encode_NetConnection_connect_result( rtmp_control_thread_t *p_thread, double number )
1590 {
1591     rtmp_packet_t *rtmp_packet;
1592     rtmp_body_t *rtmp_body;
1593     uint8_t *tmp_buffer;
1594
1595     /* Build NetConnection.connect result */
1596     rtmp_body = rtmp_body_new( -1 );
1597
1598     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "_result" );
1599     rtmp_body_append( rtmp_body, tmp_buffer,
1600         AMF_DATATYPE_SIZE_STRING + strlen( "_result" ) );
1601     free( tmp_buffer );
1602
1603     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1604     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1605     free( tmp_buffer );
1606
1607     tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1608     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1609     free( tmp_buffer );
1610
1611     tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1612     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1613     free( tmp_buffer );
1614
1615     tmp_buffer = amf_encode_object_variable( "level",
1616         AMF_DATATYPE_STRING, "status" );
1617     rtmp_body_append( rtmp_body, tmp_buffer,
1618         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1619         AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1620     free( tmp_buffer );
1621
1622     tmp_buffer = amf_encode_object_variable( "code",
1623         AMF_DATATYPE_STRING, "NetConnection.Connect.Success" );
1624     rtmp_body_append( rtmp_body, tmp_buffer,
1625         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1626         AMF_DATATYPE_SIZE_STRING + strlen( "NetConnection.Connect.Success" ) );
1627     free( tmp_buffer );
1628
1629     tmp_buffer = amf_encode_object_variable( "description",
1630         AMF_DATATYPE_STRING, "Connection succeeded." );
1631     rtmp_body_append( rtmp_body, tmp_buffer,
1632         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1633         AMF_DATATYPE_SIZE_STRING + strlen( "Connection succeeded." ) );
1634     free( tmp_buffer );
1635
1636     tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1637     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1638     free( tmp_buffer );
1639
1640     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1641         0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
1642     free( rtmp_body->body );
1643     free( rtmp_body );
1644
1645     return rtmp_packet;
1646 }
1647
1648 static rtmp_packet_t *
1649 rtmp_encode_createStream_result( rtmp_control_thread_t *p_thread, double stream_client_id, double stream_server_id )
1650 {
1651     rtmp_packet_t *rtmp_packet;
1652     rtmp_body_t *rtmp_body;
1653     uint8_t *tmp_buffer;
1654
1655     /* Build createStream result */
1656     rtmp_body = rtmp_body_new( -1 );
1657
1658     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "_result" );
1659     rtmp_body_append( rtmp_body, tmp_buffer,
1660         AMF_DATATYPE_SIZE_STRING + strlen( "_result" ) );
1661     free( tmp_buffer );
1662
1663     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &stream_client_id );
1664     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1665     free( tmp_buffer );
1666
1667     tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1668     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1669     free( tmp_buffer );
1670
1671     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &stream_server_id );
1672     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1673     free( tmp_buffer );
1674
1675     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1676         0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
1677     free( rtmp_body->body );
1678     free( rtmp_body );
1679
1680     return rtmp_packet;
1681 }
1682
1683 static rtmp_packet_t *
1684 rtmp_encode_ping_reset_stream( rtmp_control_thread_t *p_thread )
1685 {
1686     rtmp_packet_t *rtmp_packet;
1687     rtmp_body_t *rtmp_body;
1688     uint8_t *tmp_buffer;
1689
1690     /* Build ping reset stream */
1691     rtmp_body = rtmp_body_new( -1 );
1692
1693     tmp_buffer = rtmp_encode_ping( RTMP_PING_RESET_STREAM, RTMP_SRC_DST_CONNECT_OBJECT2, 0, 0 );
1694     rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_RESET_STREAM );
1695     free( tmp_buffer );
1696
1697     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1698         0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
1699     free( rtmp_body->body );
1700     free( rtmp_body );
1701
1702     return rtmp_packet;
1703 }
1704
1705 static rtmp_packet_t *
1706 rtmp_encode_ping_clear_stream( rtmp_control_thread_t *p_thread, uint32_t src_dst )
1707 {
1708     rtmp_packet_t *rtmp_packet;
1709     rtmp_body_t *rtmp_body;
1710     uint8_t *tmp_buffer;
1711
1712     /* Build ping clear stream */
1713     rtmp_body = rtmp_body_new( -1 );
1714
1715     tmp_buffer = rtmp_encode_ping( RTMP_PING_CLEAR_STREAM, src_dst, 0, 0 );
1716     rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_CLEAR_STREAM );
1717     free( tmp_buffer );
1718
1719     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1720         0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
1721     free( rtmp_body->body );
1722     free( rtmp_body );
1723
1724     return rtmp_packet;
1725 }
1726
1727 static rtmp_packet_t *
1728 rtmp_encode_NetStream_play_reset_onStatus( rtmp_control_thread_t *p_thread, char *psz_media )
1729 {
1730     rtmp_packet_t *rtmp_packet;
1731     rtmp_body_t *rtmp_body;
1732     uint8_t *tmp_buffer;
1733     double number;
1734     char *description;
1735
1736     /* Build NetStream.play.reset onStatus */
1737     rtmp_body = rtmp_body_new( -1 );
1738
1739     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onStatus" );
1740     rtmp_body_append( rtmp_body, tmp_buffer,
1741         AMF_DATATYPE_SIZE_STRING + strlen( "onStatus" ) );
1742     free( tmp_buffer );
1743
1744     number = 1; /* TODO: review this number*/
1745     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1746     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1747     free( tmp_buffer );
1748
1749     tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1750     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1751     free( tmp_buffer );
1752
1753     tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1754     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1755     free( tmp_buffer );
1756
1757     tmp_buffer = amf_encode_object_variable( "level",
1758         AMF_DATATYPE_STRING, "status" );
1759     rtmp_body_append( rtmp_body, tmp_buffer,
1760         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1761         AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1762     free( tmp_buffer );
1763
1764     tmp_buffer = amf_encode_object_variable( "code",
1765         AMF_DATATYPE_STRING, "NetStream.Play.Reset" );
1766     rtmp_body_append( rtmp_body, tmp_buffer,
1767         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1768         AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Play.Reset" ) );
1769     free( tmp_buffer );
1770
1771     if( asprintf( &description, "Playing and resetting %s.", psz_media ) == -1 )
1772     {
1773         free( rtmp_body->body );
1774         free( rtmp_body );
1775         return NULL;
1776     }
1777     tmp_buffer = amf_encode_object_variable( "description",
1778         AMF_DATATYPE_STRING, description );
1779     rtmp_body_append( rtmp_body, tmp_buffer,
1780         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1781         AMF_DATATYPE_SIZE_STRING + strlen( description ) );
1782     free( tmp_buffer );
1783     free( description );
1784
1785     tmp_buffer = amf_encode_object_variable( "details",
1786         AMF_DATATYPE_STRING, psz_media );
1787     rtmp_body_append( rtmp_body, tmp_buffer,
1788         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "details" ) +
1789         AMF_DATATYPE_SIZE_STRING + strlen( psz_media ) );
1790     free( tmp_buffer );
1791
1792     tmp_buffer = amf_encode_object_variable( "clientid",
1793         AMF_DATATYPE_NUMBER, &p_thread->stream_client_id );
1794     rtmp_body_append( rtmp_body, tmp_buffer,
1795         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "clientid" ) +
1796         AMF_DATATYPE_SIZE_NUMBER );
1797     free( tmp_buffer );
1798
1799     tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1800     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1801     free( tmp_buffer );
1802
1803     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_NOTIFY,
1804         0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
1805     free( rtmp_body->body );
1806     free( rtmp_body );
1807
1808     return rtmp_packet;
1809 }
1810
1811 static rtmp_packet_t *
1812 rtmp_encode_NetStream_play_start_onStatus( rtmp_control_thread_t *p_thread, char *psz_media )
1813 {
1814     rtmp_packet_t *rtmp_packet;
1815     rtmp_body_t *rtmp_body;
1816     uint8_t *tmp_buffer;
1817     double number;
1818     char *description;
1819
1820     /* Build NetStream.play.start onStatus */
1821     rtmp_body = rtmp_body_new( -1 );
1822
1823     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onStatus" );
1824     rtmp_body_append( rtmp_body, tmp_buffer,
1825         AMF_DATATYPE_SIZE_STRING + strlen( "onStatus" ) );
1826     free( tmp_buffer );
1827
1828     number = 1; /* TODO: review this number*/
1829     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1830     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1831     free( tmp_buffer );
1832
1833     tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1834     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1835     free( tmp_buffer );
1836
1837     tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1838     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1839     free( tmp_buffer );
1840
1841     tmp_buffer = amf_encode_object_variable( "level",
1842         AMF_DATATYPE_STRING, "status" );
1843     rtmp_body_append( rtmp_body, tmp_buffer,
1844         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1845         AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1846     free( tmp_buffer );
1847
1848     tmp_buffer = amf_encode_object_variable( "code",
1849         AMF_DATATYPE_STRING, "NetStream.Play.Start" );
1850     rtmp_body_append( rtmp_body, tmp_buffer,
1851         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1852         AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Play.Start" ) );
1853     free( tmp_buffer );
1854
1855     if( asprintf( &description, "Started playing %s.", psz_media ) == -1 )
1856     {
1857         free( rtmp_body->body );
1858         free( rtmp_body );
1859         return NULL;
1860     }
1861
1862     tmp_buffer = amf_encode_object_variable( "description",
1863         AMF_DATATYPE_STRING, description );
1864     rtmp_body_append( rtmp_body, tmp_buffer,
1865         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1866         AMF_DATATYPE_SIZE_STRING + strlen( description ) );
1867     free( tmp_buffer );
1868     free( description );
1869
1870     tmp_buffer = amf_encode_object_variable( "details",
1871         AMF_DATATYPE_STRING, psz_media );
1872     rtmp_body_append( rtmp_body, tmp_buffer,
1873         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "details" ) +
1874         AMF_DATATYPE_SIZE_STRING + strlen( psz_media ) );
1875     free( tmp_buffer );
1876
1877     tmp_buffer = amf_encode_object_variable( "clientid",
1878         AMF_DATATYPE_NUMBER, &p_thread->stream_client_id );
1879     rtmp_body_append( rtmp_body, tmp_buffer,
1880         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "clientid" ) +
1881         AMF_DATATYPE_SIZE_NUMBER );
1882     free( tmp_buffer );
1883
1884     tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1885     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1886     free( tmp_buffer );
1887
1888     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_NOTIFY,
1889         0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
1890     free( rtmp_body->body );
1891     free( rtmp_body );
1892
1893     return rtmp_packet;
1894 }
1895
1896 static uint8_t
1897 rtmp_encode_header_size( vlc_object_t *p_this, uint8_t header_size )
1898 {
1899     if( header_size == 1 )
1900         return RTMP_HEADER_SIZE_1;
1901     else if( header_size == 4 )
1902         return RTMP_HEADER_SIZE_4;
1903     else if( header_size == 8 )
1904         return RTMP_HEADER_SIZE_8;
1905     else if( header_size == 12 )
1906         return RTMP_HEADER_SIZE_12;
1907     else
1908     {
1909         msg_Err( p_this, "invalid header size for encoding" );
1910         return 0;
1911     }
1912 }
1913
1914 static uint8_t
1915 rtmp_decode_header_size( vlc_object_t *p_this, uint8_t header_size )
1916 {
1917     if( header_size == RTMP_HEADER_SIZE_1 )
1918         return 1;
1919     else if( header_size == RTMP_HEADER_SIZE_4 )
1920         return 4;
1921     else if( header_size == RTMP_HEADER_SIZE_8 )
1922         return 8;
1923     else if( header_size == RTMP_HEADER_SIZE_12 )
1924         return 12;
1925     else
1926     {
1927         msg_Err( p_this, "invalid RTMP_HEADER_SIZE_XX " );
1928         return 0;
1929     }
1930 }
1931
1932 static uint8_t
1933 rtmp_get_stream_index( uint8_t content_type )
1934 {
1935     if( content_type == RTMP_CONTENT_TYPE_AUDIO_DATA )
1936         return RTMP_DEFAULT_STREAM_INDEX_AUDIO_DATA;
1937     else if( content_type == RTMP_CONTENT_TYPE_VIDEO_DATA )
1938         return RTMP_DEFAULT_STREAM_INDEX_VIDEO_DATA;
1939     else if( content_type == RTMP_CONTENT_TYPE_NOTIFY )
1940         return RTMP_DEFAULT_STREAM_INDEX_NOTIFY;
1941     else
1942         return -1;
1943 }
1944
1945 /*****************************************************************************
1946  * Body handling implementation:
1947  ******************************************************************************/
1948 rtmp_body_t *
1949 rtmp_body_new( int length_buffer )
1950 {
1951     rtmp_body_t *rtmp_body;
1952
1953     rtmp_body = (rtmp_body_t *) malloc( sizeof( rtmp_body_t ) );
1954     if( !rtmp_body ) return NULL;
1955
1956     rtmp_body->length_body = 0;
1957     if( length_buffer < 0 )
1958         rtmp_body->length_buffer = RTMP_BODY_SIZE_ALLOC;
1959     else
1960         rtmp_body->length_buffer = length_buffer;
1961     rtmp_body->body = (uint8_t *) malloc( rtmp_body->length_buffer * sizeof( uint8_t ) );
1962     if( !rtmp_body->body )
1963     {
1964         free( rtmp_body );
1965         return NULL;
1966     }
1967     return rtmp_body;
1968 }
1969
1970 void
1971 rtmp_body_reset( rtmp_body_t *rtmp_body )
1972 {
1973     rtmp_body->length_body = 0;
1974 }
1975
1976 static void
1977 rtmp_body_append( rtmp_body_t *rtmp_body, uint8_t *buffer, uint32_t length )
1978 {
1979     if( (rtmp_body->length_body + length) > rtmp_body->length_buffer )
1980     {
1981         uint8_t *tmp;
1982         rtmp_body->length_buffer = rtmp_body->length_body + length;
1983         tmp =  realloc( rtmp_body->body,
1984                         rtmp_body->length_buffer * sizeof( uint8_t ) );
1985         if( !tmp ) return;
1986         rtmp_body->body = tmp;
1987     }
1988
1989     memcpy( rtmp_body->body + rtmp_body->length_body, buffer, length );
1990     rtmp_body->length_body += length;
1991 }
1992
1993 /*****************************************************************************
1994  * RTMP ping implementation:
1995  ******************************************************************************/
1996 static uint8_t *
1997 rtmp_encode_ping( uint16_t type, uint32_t src_dst, uint32_t third_arg, uint32_t fourth_arg )
1998 {
1999     uint8_t *out = NULL;
2000     VLC_UNUSED(fourth_arg);
2001
2002     if( type == RTMP_PING_CLEAR_STREAM )
2003         out = (uint8_t *) malloc( RTMP_PING_SIZE_CLEAR_STREAM * sizeof( uint8_t ) );
2004     else if( type == RTMP_PING_CLEAR_PLAYING_BUFFER )
2005         out = (uint8_t *) malloc( RTMP_PING_SIZE_CLEAR_PLAYING_BUFFER * sizeof( uint8_t ) );
2006     else if( type == RTMP_PING_BUFFER_TIME_CLIENT )
2007     {
2008         out = (uint8_t *) malloc( RTMP_PING_SIZE_BUFFER_TIME_CLIENT * sizeof( uint8_t ) );
2009         if( !out ) goto error;
2010         third_arg = hton32( third_arg );
2011         memcpy( out + 6, &third_arg, sizeof( uint32_t ) );
2012     }
2013     else if( type == RTMP_PING_RESET_STREAM )
2014     {
2015         out = (uint8_t *) malloc( RTMP_PING_SIZE_RESET_STREAM * sizeof( uint8_t ) );
2016     }
2017 /*    else if( type == RTMP_PING_CLIENT_FROM_SERVER ) TODO: research this
2018     {
2019     }
2020     else if( type == RTMP_PING_PONG_FROM_CLIENT )
2021     {
2022     }
2023 */    else
2024     {
2025         out = (uint8_t *) malloc( RTMP_PING_SIZE_BUFFER_TIME_CLIENT * sizeof( uint8_t ) );
2026         if( !out ) goto error;
2027         out[6] = 0x0D; out[7] = 0x0E; out[8] = 0x0A; out[9] = 0x0D;
2028     }
2029
2030     if( !out ) goto error;
2031
2032     type = hton16( type );
2033     memcpy( out, &type, sizeof( uint16_t ) );
2034
2035     src_dst = hton32( src_dst );
2036     memcpy( out + 2, &src_dst, sizeof( uint32_t ) );
2037
2038     return out;
2039
2040 error:
2041     return NULL;
2042 }
2043
2044 /*****************************************************************************
2045  * AMF implementation:
2046  ******************************************************************************/
2047 static uint8_t *
2048 amf_encode_element( uint8_t element, const void *value )
2049 {
2050     uint8_t *out;
2051
2052     if ( element == AMF_DATATYPE_NUMBER )
2053     {
2054         uint64_t number = *(uint64_t *) value;
2055
2056         out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2057         if( !out ) return NULL;
2058         
2059         number = hton64( number );
2060         out[0] = AMF_DATATYPE_NUMBER;
2061         memcpy( out + 1, &number, sizeof( uint64_t ) );
2062     } else if ( element == AMF_DATATYPE_BOOLEAN )
2063     {
2064         out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_BOOLEAN * sizeof( uint8_t ) );
2065         if( !out ) return NULL;
2066
2067         out[0] = AMF_DATATYPE_BOOLEAN;
2068         out[1] = *(uint8_t *) value;
2069     } else if ( element == AMF_DATATYPE_STRING )
2070     {
2071         uint16_t length_psz, length_psz_cpy;
2072
2073         length_psz = length_psz_cpy = strlen( (char *) value );
2074         out = (uint8_t *) malloc( ( AMF_DATATYPE_SIZE_STRING + length_psz ) * sizeof( uint8_t ) );
2075         if( !out ) return NULL;
2076
2077         out[0] = AMF_DATATYPE_STRING;
2078         length_psz = hton16( length_psz );
2079         memcpy( out + 1, &length_psz, sizeof( uint16_t ) );
2080         memcpy( out + 3, value, length_psz_cpy );
2081     } else if ( element == AMF_DATATYPE_OBJECT )
2082     {
2083         out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_OBJECT * sizeof( uint8_t ) );
2084         if( !out ) return NULL;
2085
2086         out[0] = AMF_DATATYPE_OBJECT;
2087     } else if ( element == AMF_DATATYPE_NULL )
2088     {
2089         out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NULL * sizeof( uint8_t ) );
2090         if( !out ) return NULL;
2091
2092         out[0] = AMF_DATATYPE_NULL;
2093     } else if ( element == AMF_DATATYPE_MIXED_ARRAY )
2094     {
2095         uint32_t highest_index = *(uint32_t *) value;
2096
2097         out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_MIXED_ARRAY * sizeof( uint8_t ) );
2098         if( !out ) return NULL;
2099
2100         highest_index = hton32( highest_index );
2101         out[0] = AMF_DATATYPE_MIXED_ARRAY;
2102         memcpy( out + 1, &highest_index, sizeof( uint32_t ) );
2103     } else if ( element == AMF_DATATYPE_END_OF_OBJECT )
2104     {
2105         out = (uint8_t *) calloc( AMF_DATATYPE_SIZE_END_OF_OBJECT, sizeof( uint8_t ) );
2106
2107         out[AMF_DATATYPE_SIZE_END_OF_OBJECT - 1] = AMF_DATATYPE_END_OF_OBJECT;
2108     } else
2109     {
2110         out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2111         if( !out ) return NULL;
2112
2113         out[0] = AMF_DATATYPE_NUMBER;
2114         out[1] = 0x0D; out[2] = 0x0E; out[3] = 0x0A; out[4] = 0x0D;
2115         out[5] = 0x0B; out[6] = 0x0E; out[7] = 0x0E; out[8] = 0x0F;
2116     }
2117
2118     return out;
2119 }
2120
2121 static uint8_t *
2122 amf_encode_object_variable( const char *key, uint8_t element, const void *value )
2123 {
2124     uint8_t *out, *out_value;
2125     int length_value;
2126     uint16_t length_psz, length_psz_cpy;
2127
2128     length_psz = length_psz_cpy = strlen( key );
2129
2130     if( element == AMF_DATATYPE_NUMBER )
2131         length_value = AMF_DATATYPE_SIZE_NUMBER;
2132     else if( element == AMF_DATATYPE_BOOLEAN )
2133         length_value = AMF_DATATYPE_SIZE_BOOLEAN;
2134     else if( element == AMF_DATATYPE_STRING )
2135         length_value = AMF_DATATYPE_SIZE_STRING + strlen( (char *) value );
2136     else if( element == AMF_DATATYPE_NULL )
2137         length_value = AMF_DATATYPE_SIZE_NULL;
2138     else
2139     {
2140         out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2141         if( !out ) return NULL;
2142
2143         out[0] = AMF_DATATYPE_NUMBER;
2144         out[1] = 0xD; out[2] = 0xE; out[3] = 0xA; out[4] = 0xD;
2145         out[5] = 0xB; out[6] = 0xE; out[7] = 0xE; out[8] = 0xF;
2146
2147         return out;
2148     }
2149
2150     out = (uint8_t *) malloc( ( AMF_DATATYPE_SIZE_OBJECT_VARIABLE + length_psz + length_value ) * sizeof( uint8_t ) );
2151     if( !out ) return NULL;
2152
2153     length_psz = hton16( length_psz );
2154     memcpy( out, &length_psz, sizeof( uint16_t ) );
2155     memcpy( out + 2, key, length_psz_cpy );
2156
2157     out_value = amf_encode_element( element, value );
2158     memcpy( out + 2 + length_psz_cpy, out_value, length_value );
2159     free( out_value );
2160
2161     return out;
2162 }
2163
2164 static double
2165 amf_decode_number( uint8_t **buffer )
2166 {
2167     uint64_t number;
2168     double out;
2169
2170     number = ntoh64( *(uint64_t *) *buffer );
2171     memcpy(&out, &number, sizeof( uint64_t ) );
2172     *buffer += sizeof( uint64_t );
2173
2174     return out;
2175 }
2176
2177 static int
2178 amf_decode_boolean( uint8_t **buffer )
2179 {
2180     int out;
2181
2182     out = **buffer;
2183     *buffer += 1;
2184
2185     return out;
2186 }
2187
2188 /* return value allocated dinamically */
2189 static char *
2190 amf_decode_string( uint8_t **buffer )
2191 {
2192     char *out;
2193     int length;
2194     int i;
2195
2196     length = ntoh16( *(uint16_t *) *buffer );
2197     *buffer += sizeof( uint16_t );
2198
2199 #error This size is wrong and breaks just about everything.
2200     if( length > sizeof( *buffer ) / sizeof( uint8_t ))
2201         return NULL;
2202
2203     out = (char *) malloc( length + 1 ); /* '\0' terminated */
2204     if( !out ) return NULL;
2205
2206     for(i = 0; i < length; i++)
2207         out[i] = (*buffer)[i];
2208
2209     *buffer += length;
2210
2211     out[i] = '\0';
2212
2213     return out;
2214 }
2215
2216 /* returns in each call next key, at end of object returns NULL */
2217 /* need to decode value of key after call */
2218 static char *
2219 amf_decode_object( uint8_t **buffer )
2220 {
2221     if( **buffer == 0x0 && *(*buffer + 1) == 0x00 && *(*buffer + 2) == 0x09)
2222     {
2223         *buffer += 3;
2224
2225         return NULL;
2226     }
2227     else
2228         return amf_decode_string( buffer );
2229 }
2230
2231 /*****************************************************************************
2232  * FLV rebuilding implementation:
2233  ******************************************************************************/
2234 static void
2235 flv_rebuild( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
2236 {
2237     uint32_t length_tag, timestamp;
2238     uint8_t *tmp;
2239
2240     tmp = (uint8_t *) realloc( rtmp_packet->body->body,
2241                                rtmp_packet->body->length_body +
2242                                FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE );
2243     if( !tmp ) return;
2244     rtmp_packet->body->body = tmp;
2245     memmove( rtmp_packet->body->body + FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE,
2246              rtmp_packet->body->body, rtmp_packet->body->length_body );
2247
2248     /* Insert tag */
2249     p_thread->flv_tag_previous_tag_size = hton32( p_thread->flv_tag_previous_tag_size );
2250     memcpy( rtmp_packet->body->body, &p_thread->flv_tag_previous_tag_size, sizeof( uint32_t ) );
2251
2252     /* Fill backwards because of overlapping*/
2253     rtmp_packet->body->body[11] = 0x00;
2254
2255     timestamp = hton32( rtmp_packet->timestamp );
2256     memcpy( rtmp_packet->body->body + 7, &timestamp, sizeof( uint32_t ) );
2257
2258     length_tag = hton32( rtmp_packet->body->length_body );
2259     memcpy( rtmp_packet->body->body + 4, &length_tag, sizeof( uint32_t ) );
2260
2261     rtmp_packet->body->body[4] = rtmp_packet->content_type;
2262
2263     rtmp_packet->body->body[12] = 0x00;
2264     rtmp_packet->body->body[13] = 0x00;
2265     rtmp_packet->body->body[14] = 0x00;
2266
2267     p_thread->flv_tag_previous_tag_size = rtmp_packet->body->length_body + FLV_TAG_SIZE;
2268
2269     /* Update size */
2270     rtmp_packet->body->length_body += FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE;
2271     rtmp_packet->body->length_buffer = rtmp_packet->body->length_body;
2272 }
2273
2274 static void
2275 flv_get_metadata_audio( rtmp_control_thread_t *p_thread, rtmp_packet_t *packet_audio, uint8_t *stereo, uint8_t *audiosamplesize, uint32_t *audiosamplerate, uint8_t *audiocodecid )
2276 {
2277     uint8_t data_audio;
2278
2279     data_audio = *packet_audio->body->body;
2280
2281     if( ( data_audio & FLV_AUDIO_STEREO_MASK ) == FLV_AUDIO_STEREO_MONO )
2282         *stereo = FLV_AUDIO_STEREO_MONO;
2283     else if( ( data_audio & FLV_AUDIO_STEREO_MASK ) == FLV_AUDIO_STEREO_STEREO )
2284         *stereo = FLV_AUDIO_STEREO_STEREO;
2285     else
2286         msg_Warn( p_thread, "unknown metadata audio stereo" );
2287
2288     if( ( data_audio & FLV_AUDIO_SIZE_MASK ) == FLV_AUDIO_SIZE_8_BIT )
2289         *audiosamplesize = FLV_AUDIO_SIZE_8_BIT >> 1;
2290     else if( ( data_audio & FLV_AUDIO_SIZE_MASK ) == FLV_AUDIO_SIZE_16_BIT )
2291         *audiosamplesize = FLV_AUDIO_SIZE_16_BIT >> 1;
2292     else
2293         msg_Warn( p_thread, "unknown metadata audio sample size" );
2294
2295     if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_5_5_KHZ )
2296         *audiosamplerate = 5512;
2297     else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_11_KHZ )
2298         *audiosamplerate = 11025;
2299     else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_22_KHZ )
2300         *audiosamplerate = 22050;
2301     else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_44_KHZ )
2302         *audiosamplerate = 44100;
2303     else
2304         msg_Warn( p_thread, "unknown metadata audio sample rate" );
2305
2306     if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_UNCOMPRESSED )
2307         *audiocodecid = FLV_AUDIO_CODEC_ID_UNCOMPRESSED >> 4;
2308     else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_ADPCM )
2309         *audiocodecid = FLV_AUDIO_CODEC_ID_ADPCM >> 4;
2310     else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_MP3 )
2311         *audiocodecid = FLV_AUDIO_CODEC_ID_MP3 >> 4;
2312     else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_NELLYMOSER_8KHZ_MONO )
2313         *audiocodecid = FLV_AUDIO_CODEC_ID_NELLYMOSER_8KHZ_MONO >> 4;
2314     else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_NELLYMOSER )
2315         *audiocodecid = FLV_AUDIO_CODEC_ID_NELLYMOSER >> 4;
2316     else
2317         msg_Warn( p_thread, "unknown metadata audio codec id" );
2318 }
2319
2320 static void
2321 flv_get_metadata_video( rtmp_control_thread_t *p_thread, rtmp_packet_t *packet_video, uint8_t *videocodecid, uint8_t *frametype )
2322 {
2323     uint8_t data_video;
2324
2325     data_video = *packet_video->body->body;
2326
2327     if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SORENSEN_H263 )
2328         *videocodecid = FLV_VIDEO_CODEC_ID_SORENSEN_H263;
2329     else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SCREEN_VIDEO )
2330         *videocodecid = FLV_VIDEO_CODEC_ID_SCREEN_VIDEO;
2331     else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_ON2_VP6 )
2332         *videocodecid = FLV_VIDEO_CODEC_ID_ON2_VP6;
2333     else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_ON2_VP6_ALPHA )
2334         *videocodecid = FLV_VIDEO_CODEC_ID_ON2_VP6_ALPHA;
2335     else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SCREEN_VIDEO_2 )
2336         *videocodecid = FLV_VIDEO_CODEC_ID_SCREEN_VIDEO_2;
2337     else
2338         msg_Warn( p_thread, "unknown metadata video codec id" );
2339
2340     if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_KEYFRAME )
2341         *frametype = FLV_VIDEO_FRAME_TYPE_KEYFRAME >> 4;
2342     else if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_INTER_FRAME )
2343         *frametype = FLV_VIDEO_FRAME_TYPE_INTER_FRAME >> 4;
2344     else if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_DISPOSABLE_INTER_FRAME )
2345         *frametype = FLV_VIDEO_FRAME_TYPE_DISPOSABLE_INTER_FRAME >> 4;
2346     else
2347         msg_Warn( p_thread, "unknown metadata video frame type" );
2348 }
2349
2350 static rtmp_packet_t *
2351 flv_build_onMetaData( access_t *p_access, uint64_t duration, uint8_t stereo, uint8_t audiosamplesize, uint32_t audiosamplerate, uint8_t audiocodecid, uint8_t videocodecid )
2352 {
2353     rtmp_packet_t *rtmp_packet;
2354     rtmp_body_t *rtmp_body;
2355     uint8_t *tmp_buffer;
2356     double number;
2357
2358     rtmp_body = rtmp_body_new( -1 );
2359
2360     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onMetaData" );
2361     rtmp_body_append( rtmp_body, tmp_buffer,
2362         AMF_DATATYPE_SIZE_STRING + strlen( "onMetaData" ) );
2363     free( tmp_buffer );
2364
2365     number = 0;
2366     tmp_buffer = amf_encode_element( AMF_DATATYPE_MIXED_ARRAY, &number );
2367     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_MIXED_ARRAY );
2368     free( tmp_buffer );
2369
2370     number = duration;
2371     tmp_buffer = amf_encode_object_variable( "duration",
2372         AMF_DATATYPE_NUMBER, &number );
2373     rtmp_body_append( rtmp_body, tmp_buffer,
2374         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "duration" ) +
2375         AMF_DATATYPE_SIZE_NUMBER );
2376     free( tmp_buffer );
2377
2378     tmp_buffer = amf_encode_object_variable( "stereo",
2379         AMF_DATATYPE_BOOLEAN, &stereo );
2380     rtmp_body_append( rtmp_body, tmp_buffer,
2381         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "stereo" ) +
2382         AMF_DATATYPE_SIZE_BOOLEAN );
2383     free( tmp_buffer );
2384
2385     number = audiosamplesize;
2386     tmp_buffer = amf_encode_object_variable( "audiosamplesize",
2387         AMF_DATATYPE_NUMBER, &number );
2388     rtmp_body_append( rtmp_body, tmp_buffer,
2389         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiosamplesize" ) +
2390         AMF_DATATYPE_SIZE_NUMBER );
2391     free( tmp_buffer );
2392
2393     number = audiosamplerate;
2394     tmp_buffer = amf_encode_object_variable( "audiosamplerate",
2395         AMF_DATATYPE_NUMBER, &number );
2396     rtmp_body_append( rtmp_body, tmp_buffer,
2397         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiosamplerate" ) +
2398         AMF_DATATYPE_SIZE_NUMBER );
2399     free( tmp_buffer );
2400
2401     number = audiocodecid;
2402     tmp_buffer = amf_encode_object_variable( "audiocodecid",
2403         AMF_DATATYPE_NUMBER, &number );
2404     rtmp_body_append( rtmp_body, tmp_buffer,
2405         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiocodecid" ) +
2406         AMF_DATATYPE_SIZE_NUMBER );
2407     free( tmp_buffer );
2408
2409     number = videocodecid;
2410     tmp_buffer = amf_encode_object_variable( "videocodecid",
2411         AMF_DATATYPE_NUMBER, &number );
2412     rtmp_body_append( rtmp_body, tmp_buffer,
2413         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "videocodecid" ) +
2414         AMF_DATATYPE_SIZE_NUMBER );
2415     free( tmp_buffer );
2416
2417     tmp_buffer = amf_encode_element( AMF_DATATYPE_END_OF_OBJECT, NULL );
2418     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
2419     free( tmp_buffer );
2420
2421     rtmp_packet = rtmp_new_packet( p_access->p_sys->p_thread,
2422         RTMP_DEFAULT_STREAM_INDEX_INVOKE,
2423         0, RTMP_CONTENT_TYPE_NOTIFY, 0, rtmp_body );
2424     free( rtmp_body->body );
2425     free( rtmp_body );
2426
2427     return rtmp_packet;
2428 }
2429
2430 block_t *
2431 flv_get_metadata( access_t *p_access )
2432 {
2433     rtmp_control_thread_t *p_thread=p_access->p_sys->p_thread;
2434     block_t *p_buf;
2435
2436     rtmp_packet_t *p_md = flv_build_onMetaData( p_access, 0,
2437         p_thread->metadata_stereo,
2438         p_thread->metadata_samplesize,
2439         p_thread->metadata_samplerate,
2440         p_thread->metadata_audiocodecid,
2441         p_thread->metadata_videocodecid );
2442
2443     flv_rebuild( p_thread, p_md );
2444     p_buf = rtmp_new_block( p_thread,
2445                             p_md->body->body, p_md->body->length_buffer );
2446
2447     rtmp_packet_free( p_md );
2448     return p_buf;
2449 }
2450
2451 block_t *
2452 flv_insert_header( access_t *p_access, block_t *first_packet )
2453 {
2454     access_sys_t *p_sys = p_access->p_sys;
2455     int old_buffer_size;
2456     uint32_t tmp_number;
2457
2458     old_buffer_size = first_packet->i_buffer;
2459
2460     first_packet = block_Realloc( first_packet, 0, first_packet->i_buffer + FLV_HEADER_SIZE );
2461
2462     memmove( first_packet->p_buffer + FLV_HEADER_SIZE,
2463         first_packet->p_buffer, old_buffer_size );
2464
2465     memcpy( first_packet->p_buffer, FLV_HEADER_SIGNATURE, sizeof( FLV_HEADER_SIGNATURE ) );
2466     first_packet->p_buffer[3] = FLV_HEADER_VERSION;
2467     if( p_sys->p_thread->has_audio && p_sys->p_thread->has_video )
2468         first_packet->p_buffer[4] = FLV_HEADER_AUDIO | FLV_HEADER_VIDEO;
2469     else if( p_sys->p_thread->has_audio )
2470         first_packet->p_buffer[4] = FLV_HEADER_AUDIO;
2471     else
2472         first_packet->p_buffer[4] = FLV_HEADER_VIDEO;
2473     tmp_number = hton32( FLV_HEADER_SIZE );
2474     memcpy( first_packet->p_buffer + 5, &tmp_number, sizeof( uint32_t ) );
2475
2476     return first_packet;
2477 }
2478
2479 void
2480 rtmp_init_handler( rtmp_handler_t *rtmp_handler )
2481 {
2482     rtmp_handler[RTMP_CONTENT_TYPE_CHUNK_SIZE] = rtmp_handler_chunk_size;
2483     rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_02] = rtmp_handler_null;
2484     rtmp_handler[RTMP_CONTENT_TYPE_BYTES_READ] = rtmp_handler_null;
2485     rtmp_handler[RTMP_CONTENT_TYPE_PING] = rtmp_handler_null;
2486     rtmp_handler[RTMP_CONTENT_TYPE_SERVER_BW] = rtmp_handler_null;
2487     rtmp_handler[RTMP_CONTENT_TYPE_CLIENT_BW] = rtmp_handler_null;
2488     rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_07] = rtmp_handler_null;
2489     rtmp_handler[RTMP_CONTENT_TYPE_AUDIO_DATA] = rtmp_handler_audio_data;
2490     rtmp_handler[RTMP_CONTENT_TYPE_VIDEO_DATA] = rtmp_handler_video_data;
2491     rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_0A_0E] = rtmp_handler_null;
2492     rtmp_handler[RTMP_CONTENT_TYPE_FLEX_STREAM] = rtmp_handler_null;
2493     rtmp_handler[RTMP_CONTENT_TYPE_FLEX_SHARED_OBJECT] = rtmp_handler_null;
2494     rtmp_handler[RTMP_CONTENT_TYPE_MESSAGE] = rtmp_handler_null;
2495     rtmp_handler[RTMP_CONTENT_TYPE_NOTIFY] = rtmp_handler_notify;
2496     rtmp_handler[RTMP_CONTENT_TYPE_SHARED_OBJECT] = rtmp_handler_null;
2497     rtmp_handler[RTMP_CONTENT_TYPE_INVOKE] = rtmp_handler_invoke;
2498 }