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