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