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