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