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