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