]> git.sesse.net Git - vlc/blob - modules/access/rtmp/rtmp_amf_flv.c
ff5673f33a30ff7422ea3e327abc8597820d8275
[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, "%s", 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 /* This function must be cancellation-safe! */
882 rtmp_packet_t *
883 rtmp_read_net_packet( rtmp_control_thread_t *p_thread )
884 {
885     int length_header;
886     int stream_index;
887     size_t bytes_left;
888     uint8_t p_read[12];
889     rtmp_packet_t *header;
890     ssize_t i_ret;
891
892     for(;;)
893     {
894         i_ret = net_Read( p_thread, p_thread->fd, NULL, p_read, 1, true );
895         if( i_ret != 1 )
896             goto error;
897
898         length_header = rtmp_decode_header_size( VLC_OBJECT(p_thread),
899                                           p_read[0] & RTMP_HEADER_SIZE_MASK );
900         stream_index = p_read[0] & RTMP_HEADER_STREAM_INDEX_MASK;
901         header = p_thread->rtmp_headers_recv+stream_index;
902
903         i_ret = net_Read( p_thread, p_thread->fd, NULL, p_read + 1, length_header - 1, true );
904         if( i_ret != length_header - 1 )
905             goto error;
906
907         /* Update timestamp if not is an interchunk packet */
908         if( length_header == 1 && header->body == NULL )
909         {
910             header->timestamp += header->timestamp_relative;
911         }
912
913         /* Length 4 and 8 headers have relative timestamp */
914         if( length_header == 4 || length_header == 8 )
915         {
916             p_read[0] = 0;
917
918             header->timestamp_relative = ntoh32( *(uint32_t *) p_read );
919             header->timestamp += header->timestamp_relative;
920         }
921
922         if( length_header >= 8 )
923         {
924             p_read[3] = 0;
925
926             header->length_body = ntoh32( *(uint32_t *) (p_read + 3) );
927             header->content_type = p_read[7];
928         }
929
930         /* Length 12 headers have absolute timestamp */
931         if( length_header >= 12 )
932         {
933             p_read[0] = 0;
934
935             header->timestamp = ntoh32( *(uint32_t *) p_read );
936             header->src_dst = ntoh32( *(uint32_t *) (p_read + 8) );
937         }
938
939         if( header->body == NULL )
940         {
941             header->body = rtmp_body_new( header->length_body );
942         }
943
944         bytes_left = header->body->length_buffer - header->body->length_body;
945
946         if( bytes_left > p_thread->chunk_size_recv )
947             bytes_left = p_thread->chunk_size_recv;
948
949         i_ret = net_Read( p_thread, p_thread->fd, NULL,
950             header->body->body + header->body->length_body, bytes_left, true );
951
952         if( i_ret != (ssize_t)bytes_left )
953             goto error;
954
955         header->body->length_body += bytes_left;
956
957         if( header->length_body == header->body->length_body )
958         {
959             rtmp_packet_t *rpkt = (rtmp_packet_t*)malloc(sizeof(rtmp_packet_t));
960             if( !rpkt ) return NULL;
961
962             rpkt->stream_index       = stream_index;
963             rpkt->timestamp          = header->timestamp;
964             rpkt->timestamp_relative = header->timestamp_relative;
965             rpkt->content_type       = header->content_type;
966             rpkt->src_dst            = header->src_dst;
967             rpkt->length_body        = header->length_body;
968             rpkt->body               = header->body;
969
970             header->body = NULL;
971
972             return rpkt;
973         }
974     }
975
976 error:
977     msg_Err( p_thread, "rtmp_read_net_packet: net_Read error");
978     return NULL;
979 }
980
981
982 static void
983 rtmp_handler_null( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
984 {
985     VLC_UNUSED(p_thread);
986     rtmp_packet_free( rtmp_packet );
987 }
988
989 static void
990 rtmp_handler_chunk_size( rtmp_control_thread_t *p_thread,
991                          rtmp_packet_t *rtmp_packet )
992 {
993     p_thread->chunk_size_recv =
994                             ntoh32( *(uint32_t *) (rtmp_packet->body->body) );
995     rtmp_packet_free( rtmp_packet );
996 }
997
998 static void
999 rtmp_handler_audio_data( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1000 {
1001     block_t *p_buffer;
1002
1003     if( !p_thread->has_audio )
1004     {
1005         p_thread->has_audio = 1;
1006
1007         flv_get_metadata_audio( p_thread, rtmp_packet,
1008             &p_thread->metadata_stereo, &p_thread->metadata_samplesize,
1009             &p_thread->metadata_samplerate, &p_thread->metadata_audiocodecid );
1010     }
1011
1012     flv_rebuild( p_thread, rtmp_packet );
1013     p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
1014     block_FifoPut( p_thread->p_fifo_input, p_buffer );
1015
1016     rtmp_packet_free( rtmp_packet );
1017 }
1018
1019 static void
1020 rtmp_handler_video_data( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1021 {
1022     block_t *p_buffer;
1023
1024     if( !p_thread->has_video )
1025     {
1026         p_thread->has_video = 1;
1027
1028         flv_get_metadata_video( p_thread, rtmp_packet,
1029             &p_thread->metadata_videocodecid, &p_thread->metadata_frametype );
1030     }
1031
1032     flv_rebuild( p_thread, rtmp_packet );
1033     p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
1034     block_FifoPut( p_thread->p_fifo_input, p_buffer );
1035
1036     rtmp_packet_free( rtmp_packet );
1037 }
1038
1039 static void
1040 rtmp_handler_notify( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1041 {
1042     block_t *p_buffer;
1043
1044     p_thread->metadata_received = 1;
1045
1046     flv_rebuild( p_thread, rtmp_packet );
1047     p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
1048     block_FifoPut( p_thread->p_fifo_input, p_buffer );
1049
1050     rtmp_packet_free( rtmp_packet );
1051 }
1052
1053 static void
1054 rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1055 {
1056     rtmp_packet_t *tmp_rtmp_packet;
1057     uint8_t *i, *end, *tmp_buffer;
1058     double number;
1059     char *string, *string2;
1060     ssize_t i_ret;
1061
1062     i = rtmp_packet->body->body;
1063     end = rtmp_packet->body->body + rtmp_packet->body->length_body;
1064
1065     i++; /* Pass over AMF_DATATYPE_STRING */
1066     string = amf_decode_string( &i );
1067
1068     i++; /* Pass over AMF_DATATYPE_NUMBER */
1069     number = amf_decode_number( &i );
1070
1071     msg_Dbg( p_thread, "%s %.1f", string, number );
1072
1073     if( strcmp( "connect", string ) == 0 )
1074     {
1075         /* Connection bandwith */
1076         tmp_rtmp_packet = rtmp_encode_onBWDone( p_thread, AMF_CALL_ONBWDONE );
1077
1078         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1079
1080         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1081         if( i_ret != tmp_rtmp_packet->length_encoded )
1082         {
1083             msg_Err( p_thread, "failed send connection bandwith" );
1084             goto error;
1085         }
1086         rtmp_packet_free( rtmp_packet );
1087         free( tmp_buffer );
1088
1089         /* Server bandwith */
1090         tmp_rtmp_packet = rtmp_encode_server_bw( p_thread, RTMP_SERVER_BW );
1091
1092         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1093
1094         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1095         if( i_ret != tmp_rtmp_packet->length_encoded )
1096         {
1097             msg_Err( p_thread, "failed send server bandwith" );
1098             goto error;
1099         }
1100         rtmp_packet_free( rtmp_packet );
1101         free( tmp_buffer );
1102
1103         /* Clear stream */
1104         tmp_rtmp_packet = rtmp_encode_ping_clear_stream( p_thread, RTMP_SRC_DST_CONNECT_OBJECT );
1105
1106         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1107
1108         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1109         if( i_ret != tmp_rtmp_packet->length_encoded )
1110         {
1111             msg_Err( p_thread, "failed send clear stream" );
1112             goto error;
1113         }
1114         rtmp_packet_free( rtmp_packet );
1115         free( tmp_buffer );
1116
1117         /* Reply NetConnection.connect */
1118         tmp_rtmp_packet = rtmp_encode_NetConnection_connect_result( p_thread, number );
1119
1120         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1121
1122         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1123         if( i_ret != tmp_rtmp_packet->length_encoded )
1124         {
1125             msg_Err( p_thread, "failed send reply NetConnection.connect" );
1126             goto error;
1127         }
1128         rtmp_packet_free( rtmp_packet );
1129         free( tmp_buffer );
1130     }
1131     else if( strcmp( "createStream", string ) == 0 )
1132     {
1133         p_thread->stream_client_id = number;
1134         p_thread->stream_server_id = RTMP_DEFAULT_STREAM_SERVER_ID;
1135
1136         /* Reply createStream */
1137         tmp_rtmp_packet = rtmp_encode_createStream_result( p_thread, p_thread->stream_client_id, p_thread->stream_server_id );
1138
1139         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1140
1141         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1142         if( i_ret != tmp_rtmp_packet->length_encoded )
1143         {
1144             msg_Err( p_thread, "failed send reply createStream" );
1145             goto error;
1146         }
1147         rtmp_packet_free( rtmp_packet );
1148         free( tmp_buffer );
1149
1150         /* Reset stream */
1151         tmp_rtmp_packet = rtmp_encode_ping_reset_stream( p_thread );
1152
1153         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1154
1155         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1156         if( i_ret != tmp_rtmp_packet->length_encoded )
1157         {
1158             msg_Err( p_thread, "failed send reset stream" );
1159             goto error;
1160         }
1161         rtmp_packet_free( rtmp_packet );
1162         free( tmp_buffer );
1163
1164         /* Clear stream */
1165         tmp_rtmp_packet = rtmp_encode_ping_clear_stream( p_thread, RTMP_SRC_DST_CONNECT_OBJECT2 );
1166
1167         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1168     
1169         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1170         if( i_ret != tmp_rtmp_packet->length_encoded )
1171         {
1172             msg_Err( p_thread, "failed send clear stream" );
1173             goto error;
1174         }
1175         rtmp_packet_free( rtmp_packet );
1176         free( tmp_buffer );
1177     }
1178     else if( strcmp( "publish", string ) == 0 )
1179     {
1180         i++;
1181         msg_Dbg( p_thread, "null" );
1182
1183         i++;
1184         string2 = amf_decode_string( &i );
1185         msg_Dbg( p_thread, "string: %s", string2 );
1186
1187         p_thread->psz_publish = strdup( string2 );
1188
1189         free( string2 );
1190     }
1191     else if( strcmp( "play", string ) == 0 )
1192     {
1193         i++;
1194         msg_Dbg( p_thread, "null" );
1195
1196         i++;
1197         string2 = amf_decode_string( &i );
1198         msg_Dbg( p_thread, "string: %s", string2 );
1199
1200         /* Reply NetStream.play.reset */
1201         tmp_rtmp_packet = rtmp_encode_NetStream_play_reset_onStatus( p_thread, string2 );
1202
1203         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1204
1205         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1206         if( i_ret != tmp_rtmp_packet->length_encoded )
1207         {
1208             msg_Err( p_thread, "failed send reply NetStream.play.reset" );
1209             goto error;
1210         }
1211         rtmp_packet_free( rtmp_packet );
1212         free( tmp_buffer );
1213
1214         /* Reply NetStream.play.start */
1215         tmp_rtmp_packet = rtmp_encode_NetStream_play_start_onStatus( p_thread, string2 );
1216
1217         tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1218
1219         i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1220         if( i_ret != tmp_rtmp_packet->length_encoded )
1221         {
1222             msg_Err( p_thread, "failed send reply NetStream.play.start" );
1223             goto error;
1224         }
1225         rtmp_packet_free( rtmp_packet );
1226         free( tmp_buffer );
1227
1228         free( string2 );
1229
1230         p_thread->result_play = 0;
1231
1232         vlc_mutex_lock( &p_thread->lock );
1233         vlc_cond_signal( &p_thread->wait );
1234         vlc_mutex_unlock( &p_thread->lock );
1235     }
1236
1237     free( string );
1238
1239     while( i < end )
1240     {
1241         if( *i == AMF_DATATYPE_NUMBER )
1242         {
1243             i++;
1244             msg_Dbg( p_thread, "number: %le", amf_decode_number( &i ) );
1245         }
1246         else if( *i == AMF_DATATYPE_BOOLEAN )
1247         {
1248             i++;
1249             msg_Dbg( p_thread, "boolean: %s", amf_decode_boolean( &i ) ? "true" : "false" );
1250         }
1251         else if( *i == AMF_DATATYPE_STRING )
1252         {
1253             i++;
1254             string = amf_decode_string( &i );
1255             msg_Dbg( p_thread, "string: %s", string );
1256             free( string );
1257         }
1258         else if( *i == AMF_DATATYPE_OBJECT )
1259         {
1260             i++;
1261             msg_Dbg( p_thread, "object" );
1262             while( ( string = amf_decode_object( &i ) ) != NULL )
1263             {
1264                 if( *i == AMF_DATATYPE_NUMBER )
1265                 {
1266                     i++;
1267                     msg_Dbg( p_thread, "key: %s value: %le", string, amf_decode_number( &i ) );
1268                 }
1269                 else if( *i == AMF_DATATYPE_BOOLEAN )
1270                 {
1271                     i++;
1272                     msg_Dbg( p_thread, "key: %s value: %s", string, amf_decode_boolean( &i ) ? "true" : "false" );
1273                 }
1274                 else if( *i == AMF_DATATYPE_STRING )
1275                 {
1276                     i++;
1277                     string2 = amf_decode_string( &i );
1278                     msg_Dbg( p_thread, "key: %s value: %s", string, string2 );
1279                     if( strcmp( "code", string ) == 0 )
1280                     {
1281 #warning Locking bugs here.
1282                         if( strcmp( "NetConnection.Connect.Success", string2 ) == 0 )
1283                         {
1284                             p_thread->result_connect = 0;
1285
1286                             vlc_mutex_lock( &p_thread->lock );
1287                             vlc_cond_signal( &p_thread->wait );
1288                             vlc_mutex_unlock( &p_thread->lock );
1289                         }
1290                         else if( strcmp( "NetConnection.Connect.InvalidApp", string2 ) == 0 )
1291                         {
1292                             vlc_mutex_lock( &p_thread->lock );
1293                             vlc_cond_signal( &p_thread->wait );
1294                             vlc_mutex_unlock( &p_thread->lock );
1295                         }
1296                         else if( strcmp( "NetStream.Play.Start", string2 ) == 0 )
1297                         {
1298                             p_thread->result_play = 0;
1299
1300                             vlc_mutex_lock( &p_thread->lock );
1301                             vlc_cond_signal( &p_thread->wait );
1302                             vlc_mutex_unlock( &p_thread->lock );
1303                         }
1304                         else if( strcmp( "NetStream.Play.Stop", string2 ) == 0 )
1305                         {
1306                             p_thread->result_stop = 1;
1307
1308                             block_FifoWake( p_thread->p_fifo_input );
1309                         }
1310                     }
1311                     free( string2 );
1312                 }
1313                 else if( *i == AMF_DATATYPE_NULL )
1314                 {
1315                     i++;
1316                     msg_Dbg( p_thread, "key: %s value: Null", string );
1317                 }
1318                 else if( *i == AMF_DATATYPE_UNDEFINED )
1319                 {
1320                     i++;
1321                     msg_Dbg( p_thread, "key: %s value: undefined (Null)", string );
1322                 }
1323                 else
1324                 {
1325                     i++;
1326                     msg_Warn( p_thread, "key: %s value: undefined AMF type", string );
1327                 }
1328                 free( string );
1329             }
1330             msg_Dbg( p_thread, "end of object" );
1331         }
1332         else if( *i == AMF_DATATYPE_NULL)
1333         {
1334             i++;
1335             msg_Dbg( p_thread, "null" );
1336         }
1337         else if( *i == AMF_DATATYPE_UNDEFINED )
1338         {
1339             i++;
1340             msg_Dbg( p_thread, "undefined (null)" );
1341         }
1342         else
1343         {
1344             i++;
1345             msg_Warn( p_thread, "undefined AMF type" );
1346         }
1347     }
1348     
1349     rtmp_packet_free( rtmp_packet );
1350     return;
1351
1352 error:
1353     free( string );
1354     rtmp_packet_free( rtmp_packet );
1355     free( tmp_buffer );
1356 }
1357
1358 /* length header calculated automatically based on last packet in the same channel */
1359 /* timestamps passed are always absolute */
1360 static rtmp_packet_t *
1361 rtmp_new_packet( rtmp_control_thread_t *p_thread, uint8_t stream_index,
1362                  uint32_t timestamp, uint8_t content_type,
1363                  uint32_t src_dst, rtmp_body_t *body )
1364 {
1365     int interchunk_headers;
1366     rtmp_packet_t *rtmp_packet;
1367     rtmp_packet_t *rtmp_send = p_thread->rtmp_headers_send+stream_index;
1368
1369     rtmp_packet = (rtmp_packet_t *) malloc( sizeof( rtmp_packet_t ) );
1370     if( !rtmp_packet ) return NULL;
1371
1372     interchunk_headers = body->length_body / p_thread->chunk_size_send;
1373     if( body->length_body % p_thread->chunk_size_send == 0 )
1374         interchunk_headers--;
1375
1376     if( src_dst != rtmp_send->src_dst )
1377     {
1378         rtmp_send->timestamp = timestamp;
1379         rtmp_send->length_body = body->length_body;
1380         rtmp_send->content_type = content_type;
1381         rtmp_send->src_dst = src_dst;
1382         
1383         rtmp_packet->length_header = 12;
1384     }
1385     else if( content_type != rtmp_send->content_type
1386         || body->length_body != rtmp_send->length_body )
1387     {
1388         rtmp_send->timestamp_relative =
1389             timestamp - rtmp_send->timestamp;
1390         rtmp_send->timestamp = timestamp;
1391         rtmp_send->length_body = body->length_body;
1392         rtmp_send->content_type = content_type;
1393
1394         rtmp_packet->length_header = 8;
1395     }
1396     else if( timestamp != rtmp_send->timestamp )
1397     {
1398         rtmp_send->timestamp_relative =
1399             timestamp - rtmp_send->timestamp;
1400         rtmp_send->timestamp = timestamp;
1401
1402         rtmp_packet->length_header = 4;
1403     }
1404     else
1405     {
1406         rtmp_packet->length_header = 1;
1407     }
1408 /*TODO: puede que no haga falta guardar el timestamp relative */
1409     rtmp_packet->stream_index = stream_index;
1410     if( rtmp_packet->length_header == 12 )
1411     {
1412         rtmp_packet->timestamp = timestamp;
1413         rtmp_packet->timestamp_relative = 0;
1414     }
1415     else
1416     {
1417         rtmp_packet->timestamp = timestamp;
1418         rtmp_packet->timestamp_relative = rtmp_send->timestamp_relative;
1419     }
1420
1421     rtmp_packet->length_encoded = rtmp_packet->length_header
1422                                 + body->length_body + interchunk_headers;
1423     rtmp_packet->length_body = body->length_body;
1424     rtmp_packet->content_type = content_type;
1425     rtmp_packet->src_dst = src_dst;
1426
1427     rtmp_packet->body = (rtmp_body_t *) malloc( sizeof( rtmp_body_t ) );
1428     if( !rtmp_packet->body )
1429     {
1430        free( rtmp_packet );
1431        return NULL;
1432     }
1433
1434     rtmp_packet->body->length_body = body->length_body;
1435     rtmp_packet->body->length_buffer = body->length_body;
1436     rtmp_packet->body->body = (uint8_t *) malloc( rtmp_packet->body->length_buffer * sizeof( uint8_t ) );
1437     if( !rtmp_packet->body->body )
1438     {
1439         free( rtmp_packet->body );
1440         free( rtmp_packet );
1441         return NULL;
1442     }
1443     memcpy( rtmp_packet->body->body, body->body, rtmp_packet->body->length_body );
1444
1445     return rtmp_packet;
1446 }
1447
1448 static block_t *
1449 rtmp_new_block( rtmp_control_thread_t *p_thread, uint8_t *buffer, int32_t length_buffer )
1450 {
1451     block_t *p_buffer;
1452     /* DOWN: p_thread->p_empty_blocks->i_depth */
1453     while ( block_FifoCount( p_thread->p_empty_blocks ) > MAX_EMPTY_BLOCKS )
1454     {
1455         p_buffer = block_FifoGet( p_thread->p_empty_blocks );
1456         block_Release( p_buffer );
1457     }
1458     /* DOWN: p_thread->p_empty_blocks->i_depth */
1459     if( block_FifoCount( p_thread->p_empty_blocks ) == 0 )
1460     {
1461         p_buffer = block_New( p_thread, length_buffer );
1462     }
1463     else
1464     {
1465         p_buffer = block_FifoGet( p_thread->p_empty_blocks );
1466         p_buffer = block_Realloc( p_buffer, 0, length_buffer );
1467     }
1468
1469     p_buffer->i_buffer = length_buffer;
1470
1471     memcpy( p_buffer->p_buffer, buffer, p_buffer->i_buffer );
1472
1473     return p_buffer;
1474 }
1475
1476 /* Call sequence for each packet:
1477  * rtmp_new_packet -> rtmp_encode_packet -> send .
1478  * No parallelism allowed because of optimization in header length. */
1479
1480 uint8_t *
1481 rtmp_encode_packet( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1482 {
1483     uint8_t *out;
1484     int interchunk_headers;
1485     uint32_t timestamp, length_body, src_dst;
1486     int i, j;
1487
1488     out = (uint8_t *) malloc( rtmp_packet->length_encoded * sizeof( uint8_t ) );
1489     if( !out ) return NULL;
1490
1491     interchunk_headers = rtmp_packet->body->length_body / p_thread->chunk_size_send;
1492     if( rtmp_packet->body->length_body % p_thread->chunk_size_send == 0 )
1493         interchunk_headers--;
1494
1495     if( rtmp_packet->length_header == 12 )
1496     {
1497         /* Timestamp absolute */
1498         timestamp = hton32( rtmp_packet->timestamp );
1499         memcpy( out, &timestamp, sizeof( uint32_t ) );
1500
1501         src_dst = hton32( rtmp_packet->src_dst );
1502         memcpy( out + 8, &src_dst, sizeof( uint32_t ) );
1503     }
1504
1505     if( rtmp_packet->length_header >= 8 )
1506     {
1507         /* Length without inter chunk headers */
1508         length_body = hton32( rtmp_packet->body->length_body );
1509         memcpy( out + 3, &length_body, sizeof( uint32_t ) );
1510
1511         out[7] = rtmp_packet->content_type;
1512     }
1513     if( rtmp_packet->length_header >= 4 && rtmp_packet->length_header != 12 )
1514     {
1515         /* Timestamp relative */
1516         timestamp = hton32( rtmp_packet->timestamp_relative );
1517         memcpy( out, &timestamp, sizeof( uint32_t ) );
1518     }
1519
1520     out[0] = rtmp_encode_header_size( (vlc_object_t *) p_thread, rtmp_packet->length_header ) + rtmp_packet->stream_index;
1521
1522     /* Insert inter chunk headers */
1523     for(i = 0, j = 0; i < rtmp_packet->body->length_body + interchunk_headers; i++, j++)
1524     {
1525         if( j % p_thread->chunk_size_send == 0 && j != 0 )
1526             out[rtmp_packet->length_header + i++] = RTMP_HEADER_SIZE_1 + rtmp_packet->stream_index;
1527         out[rtmp_packet->length_header + i] = rtmp_packet->body->body[j];
1528     }
1529
1530     return out;
1531 }
1532
1533 static rtmp_packet_t *
1534 rtmp_encode_onBWDone( rtmp_control_thread_t *p_thread, double number )
1535 {
1536     rtmp_packet_t *rtmp_packet;
1537     rtmp_body_t *rtmp_body;
1538     uint8_t *tmp_buffer;
1539
1540     /* Build onBWDone */
1541     rtmp_body = rtmp_body_new( -1 );
1542
1543     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onBWDone" );
1544     rtmp_body_append( rtmp_body, tmp_buffer,
1545         AMF_DATATYPE_SIZE_STRING + strlen( "onBWDone" ) );
1546     free( tmp_buffer );
1547
1548     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1549     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1550     free( tmp_buffer );
1551
1552     tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1553     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1554     free( tmp_buffer );
1555
1556     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1557         0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_CONNECT_OBJECT, rtmp_body );
1558     free( rtmp_body->body );
1559     free( rtmp_body );
1560
1561     return rtmp_packet;
1562 }
1563
1564 static rtmp_packet_t *
1565 rtmp_encode_server_bw( rtmp_control_thread_t *p_thread, uint32_t number )
1566 {
1567     rtmp_packet_t *rtmp_packet;
1568     rtmp_body_t *rtmp_body;
1569
1570     /* Build server bw */
1571     rtmp_body = rtmp_body_new( -1 );
1572
1573     rtmp_body_append( rtmp_body, (uint8_t *) &number, sizeof( uint32_t ) );
1574
1575     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1576       0, RTMP_CONTENT_TYPE_SERVER_BW, RTMP_SRC_DST_CONNECT_OBJECT, rtmp_body );
1577     free( rtmp_body->body );
1578     free( rtmp_body );
1579
1580     return rtmp_packet;
1581 }
1582
1583 static rtmp_packet_t *
1584 rtmp_encode_NetConnection_connect_result( rtmp_control_thread_t *p_thread, double number )
1585 {
1586     rtmp_packet_t *rtmp_packet;
1587     rtmp_body_t *rtmp_body;
1588     uint8_t *tmp_buffer;
1589
1590     /* Build NetConnection.connect result */
1591     rtmp_body = rtmp_body_new( -1 );
1592
1593     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "_result" );
1594     rtmp_body_append( rtmp_body, tmp_buffer,
1595         AMF_DATATYPE_SIZE_STRING + strlen( "_result" ) );
1596     free( tmp_buffer );
1597
1598     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1599     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1600     free( tmp_buffer );
1601
1602     tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1603     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1604     free( tmp_buffer );
1605
1606     tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1607     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1608     free( tmp_buffer );
1609
1610     tmp_buffer = amf_encode_object_variable( "level",
1611         AMF_DATATYPE_STRING, "status" );
1612     rtmp_body_append( rtmp_body, tmp_buffer,
1613         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1614         AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1615     free( tmp_buffer );
1616
1617     tmp_buffer = amf_encode_object_variable( "code",
1618         AMF_DATATYPE_STRING, "NetConnection.Connect.Success" );
1619     rtmp_body_append( rtmp_body, tmp_buffer,
1620         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1621         AMF_DATATYPE_SIZE_STRING + strlen( "NetConnection.Connect.Success" ) );
1622     free( tmp_buffer );
1623
1624     tmp_buffer = amf_encode_object_variable( "description",
1625         AMF_DATATYPE_STRING, "Connection succeeded." );
1626     rtmp_body_append( rtmp_body, tmp_buffer,
1627         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1628         AMF_DATATYPE_SIZE_STRING + strlen( "Connection succeeded." ) );
1629     free( tmp_buffer );
1630
1631     tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1632     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1633     free( tmp_buffer );
1634
1635     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1636         0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
1637     free( rtmp_body->body );
1638     free( rtmp_body );
1639
1640     return rtmp_packet;
1641 }
1642
1643 static rtmp_packet_t *
1644 rtmp_encode_createStream_result( rtmp_control_thread_t *p_thread, double stream_client_id, double stream_server_id )
1645 {
1646     rtmp_packet_t *rtmp_packet;
1647     rtmp_body_t *rtmp_body;
1648     uint8_t *tmp_buffer;
1649
1650     /* Build createStream result */
1651     rtmp_body = rtmp_body_new( -1 );
1652
1653     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "_result" );
1654     rtmp_body_append( rtmp_body, tmp_buffer,
1655         AMF_DATATYPE_SIZE_STRING + strlen( "_result" ) );
1656     free( tmp_buffer );
1657
1658     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &stream_client_id );
1659     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1660     free( tmp_buffer );
1661
1662     tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1663     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1664     free( tmp_buffer );
1665
1666     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &stream_server_id );
1667     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1668     free( tmp_buffer );
1669
1670     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1671         0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
1672     free( rtmp_body->body );
1673     free( rtmp_body );
1674
1675     return rtmp_packet;
1676 }
1677
1678 static rtmp_packet_t *
1679 rtmp_encode_ping_reset_stream( rtmp_control_thread_t *p_thread )
1680 {
1681     rtmp_packet_t *rtmp_packet;
1682     rtmp_body_t *rtmp_body;
1683     uint8_t *tmp_buffer;
1684
1685     /* Build ping reset stream */
1686     rtmp_body = rtmp_body_new( -1 );
1687
1688     tmp_buffer = rtmp_encode_ping( RTMP_PING_RESET_STREAM, RTMP_SRC_DST_CONNECT_OBJECT2, 0, 0 );
1689     rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_RESET_STREAM );
1690     free( tmp_buffer );
1691
1692     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1693         0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
1694     free( rtmp_body->body );
1695     free( rtmp_body );
1696
1697     return rtmp_packet;
1698 }
1699
1700 static rtmp_packet_t *
1701 rtmp_encode_ping_clear_stream( rtmp_control_thread_t *p_thread, uint32_t src_dst )
1702 {
1703     rtmp_packet_t *rtmp_packet;
1704     rtmp_body_t *rtmp_body;
1705     uint8_t *tmp_buffer;
1706
1707     /* Build ping clear stream */
1708     rtmp_body = rtmp_body_new( -1 );
1709
1710     tmp_buffer = rtmp_encode_ping( RTMP_PING_CLEAR_STREAM, src_dst, 0, 0 );
1711     rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_CLEAR_STREAM );
1712     free( tmp_buffer );
1713
1714     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1715         0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
1716     free( rtmp_body->body );
1717     free( rtmp_body );
1718
1719     return rtmp_packet;
1720 }
1721
1722 static rtmp_packet_t *
1723 rtmp_encode_NetStream_play_reset_onStatus( rtmp_control_thread_t *p_thread, char *psz_media )
1724 {
1725     rtmp_packet_t *rtmp_packet;
1726     rtmp_body_t *rtmp_body;
1727     uint8_t *tmp_buffer;
1728     double number;
1729     char *description;
1730
1731     /* Build NetStream.play.reset onStatus */
1732     rtmp_body = rtmp_body_new( -1 );
1733
1734     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onStatus" );
1735     rtmp_body_append( rtmp_body, tmp_buffer,
1736         AMF_DATATYPE_SIZE_STRING + strlen( "onStatus" ) );
1737     free( tmp_buffer );
1738
1739     number = 1; /* TODO: review this number*/
1740     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1741     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1742     free( tmp_buffer );
1743
1744     tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1745     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1746     free( tmp_buffer );
1747
1748     tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1749     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1750     free( tmp_buffer );
1751
1752     tmp_buffer = amf_encode_object_variable( "level",
1753         AMF_DATATYPE_STRING, "status" );
1754     rtmp_body_append( rtmp_body, tmp_buffer,
1755         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1756         AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1757     free( tmp_buffer );
1758
1759     tmp_buffer = amf_encode_object_variable( "code",
1760         AMF_DATATYPE_STRING, "NetStream.Play.Reset" );
1761     rtmp_body_append( rtmp_body, tmp_buffer,
1762         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1763         AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Play.Reset" ) );
1764     free( tmp_buffer );
1765
1766     if( asprintf( &description, "Playing and resetting %s.", psz_media ) == -1 )
1767     {
1768         free( rtmp_body->body );
1769         free( rtmp_body );
1770         return NULL;
1771     }
1772     tmp_buffer = amf_encode_object_variable( "description",
1773         AMF_DATATYPE_STRING, description );
1774     rtmp_body_append( rtmp_body, tmp_buffer,
1775         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1776         AMF_DATATYPE_SIZE_STRING + strlen( description ) );
1777     free( tmp_buffer );
1778     free( description );
1779
1780     tmp_buffer = amf_encode_object_variable( "details",
1781         AMF_DATATYPE_STRING, psz_media );
1782     rtmp_body_append( rtmp_body, tmp_buffer,
1783         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "details" ) +
1784         AMF_DATATYPE_SIZE_STRING + strlen( psz_media ) );
1785     free( tmp_buffer );
1786
1787     tmp_buffer = amf_encode_object_variable( "clientid",
1788         AMF_DATATYPE_NUMBER, &p_thread->stream_client_id );
1789     rtmp_body_append( rtmp_body, tmp_buffer,
1790         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "clientid" ) +
1791         AMF_DATATYPE_SIZE_NUMBER );
1792     free( tmp_buffer );
1793
1794     tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1795     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1796     free( tmp_buffer );
1797
1798     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_NOTIFY,
1799         0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
1800     free( rtmp_body->body );
1801     free( rtmp_body );
1802
1803     return rtmp_packet;
1804 }
1805
1806 static rtmp_packet_t *
1807 rtmp_encode_NetStream_play_start_onStatus( rtmp_control_thread_t *p_thread, char *psz_media )
1808 {
1809     rtmp_packet_t *rtmp_packet;
1810     rtmp_body_t *rtmp_body;
1811     uint8_t *tmp_buffer;
1812     double number;
1813     char *description;
1814
1815     /* Build NetStream.play.start onStatus */
1816     rtmp_body = rtmp_body_new( -1 );
1817
1818     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onStatus" );
1819     rtmp_body_append( rtmp_body, tmp_buffer,
1820         AMF_DATATYPE_SIZE_STRING + strlen( "onStatus" ) );
1821     free( tmp_buffer );
1822
1823     number = 1; /* TODO: review this number*/
1824     tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1825     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1826     free( tmp_buffer );
1827
1828     tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1829     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1830     free( tmp_buffer );
1831
1832     tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1833     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1834     free( tmp_buffer );
1835
1836     tmp_buffer = amf_encode_object_variable( "level",
1837         AMF_DATATYPE_STRING, "status" );
1838     rtmp_body_append( rtmp_body, tmp_buffer,
1839         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1840         AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1841     free( tmp_buffer );
1842
1843     tmp_buffer = amf_encode_object_variable( "code",
1844         AMF_DATATYPE_STRING, "NetStream.Play.Start" );
1845     rtmp_body_append( rtmp_body, tmp_buffer,
1846         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1847         AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Play.Start" ) );
1848     free( tmp_buffer );
1849
1850     if( asprintf( &description, "Started playing %s.", psz_media ) == -1 )
1851     {
1852         free( rtmp_body->body );
1853         free( rtmp_body );
1854         return NULL;
1855     }
1856
1857     tmp_buffer = amf_encode_object_variable( "description",
1858         AMF_DATATYPE_STRING, description );
1859     rtmp_body_append( rtmp_body, tmp_buffer,
1860         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1861         AMF_DATATYPE_SIZE_STRING + strlen( description ) );
1862     free( tmp_buffer );
1863     free( description );
1864
1865     tmp_buffer = amf_encode_object_variable( "details",
1866         AMF_DATATYPE_STRING, psz_media );
1867     rtmp_body_append( rtmp_body, tmp_buffer,
1868         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "details" ) +
1869         AMF_DATATYPE_SIZE_STRING + strlen( psz_media ) );
1870     free( tmp_buffer );
1871
1872     tmp_buffer = amf_encode_object_variable( "clientid",
1873         AMF_DATATYPE_NUMBER, &p_thread->stream_client_id );
1874     rtmp_body_append( rtmp_body, tmp_buffer,
1875         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "clientid" ) +
1876         AMF_DATATYPE_SIZE_NUMBER );
1877     free( tmp_buffer );
1878
1879     tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1880     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1881     free( tmp_buffer );
1882
1883     rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_NOTIFY,
1884         0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
1885     free( rtmp_body->body );
1886     free( rtmp_body );
1887
1888     return rtmp_packet;
1889 }
1890
1891 static uint8_t
1892 rtmp_encode_header_size( vlc_object_t *p_this, uint8_t header_size )
1893 {
1894     if( header_size == 1 )
1895         return RTMP_HEADER_SIZE_1;
1896     else if( header_size == 4 )
1897         return RTMP_HEADER_SIZE_4;
1898     else if( header_size == 8 )
1899         return RTMP_HEADER_SIZE_8;
1900     else if( header_size == 12 )
1901         return RTMP_HEADER_SIZE_12;
1902     else
1903     {
1904         msg_Err( p_this, "invalid header size for encoding" );
1905         return 0;
1906     }
1907 }
1908
1909 static uint8_t
1910 rtmp_decode_header_size( vlc_object_t *p_this, uint8_t header_size )
1911 {
1912     if( header_size == RTMP_HEADER_SIZE_1 )
1913         return 1;
1914     else if( header_size == RTMP_HEADER_SIZE_4 )
1915         return 4;
1916     else if( header_size == RTMP_HEADER_SIZE_8 )
1917         return 8;
1918     else if( header_size == RTMP_HEADER_SIZE_12 )
1919         return 12;
1920     else
1921     {
1922         msg_Err( p_this, "invalid RTMP_HEADER_SIZE_XX " );
1923         return 0;
1924     }
1925 }
1926
1927 static uint8_t
1928 rtmp_get_stream_index( uint8_t content_type )
1929 {
1930     if( content_type == RTMP_CONTENT_TYPE_AUDIO_DATA )
1931         return RTMP_DEFAULT_STREAM_INDEX_AUDIO_DATA;
1932     else if( content_type == RTMP_CONTENT_TYPE_VIDEO_DATA )
1933         return RTMP_DEFAULT_STREAM_INDEX_VIDEO_DATA;
1934     else if( content_type == RTMP_CONTENT_TYPE_NOTIFY )
1935         return RTMP_DEFAULT_STREAM_INDEX_NOTIFY;
1936     else
1937         return -1;
1938 }
1939
1940 /*****************************************************************************
1941  * Body handling implementation:
1942  ******************************************************************************/
1943 rtmp_body_t *
1944 rtmp_body_new( int length_buffer )
1945 {
1946     rtmp_body_t *rtmp_body;
1947
1948     rtmp_body = (rtmp_body_t *) malloc( sizeof( rtmp_body_t ) );
1949     if( !rtmp_body ) return NULL;
1950
1951     rtmp_body->length_body = 0;
1952     if( length_buffer < 0 )
1953         rtmp_body->length_buffer = RTMP_BODY_SIZE_ALLOC;
1954     else
1955         rtmp_body->length_buffer = length_buffer;
1956     rtmp_body->body = (uint8_t *) malloc( rtmp_body->length_buffer * sizeof( uint8_t ) );
1957     if( !rtmp_body->body )
1958     {
1959         free( rtmp_body );
1960         return NULL;
1961     }
1962     return rtmp_body;
1963 }
1964
1965 void
1966 rtmp_body_reset( rtmp_body_t *rtmp_body )
1967 {
1968     rtmp_body->length_body = 0;
1969 }
1970
1971 static void
1972 rtmp_body_append( rtmp_body_t *rtmp_body, uint8_t *buffer, uint32_t length )
1973 {
1974     if( (rtmp_body->length_body + length) > rtmp_body->length_buffer )
1975     {
1976         uint8_t *tmp;
1977         rtmp_body->length_buffer = rtmp_body->length_body + length;
1978         tmp =  realloc( rtmp_body->body,
1979                         rtmp_body->length_buffer * sizeof( uint8_t ) );
1980         if( !tmp ) return;
1981         rtmp_body->body = tmp;
1982     }
1983
1984     memcpy( rtmp_body->body + rtmp_body->length_body, buffer, length );
1985     rtmp_body->length_body += length;
1986 }
1987
1988 /*****************************************************************************
1989  * RTMP ping implementation:
1990  ******************************************************************************/
1991 static uint8_t *
1992 rtmp_encode_ping( uint16_t type, uint32_t src_dst, uint32_t third_arg, uint32_t fourth_arg )
1993 {
1994     uint8_t *out = NULL;
1995     VLC_UNUSED(fourth_arg);
1996
1997     if( type == RTMP_PING_CLEAR_STREAM )
1998         out = (uint8_t *) malloc( RTMP_PING_SIZE_CLEAR_STREAM * sizeof( uint8_t ) );
1999     else if( type == RTMP_PING_CLEAR_PLAYING_BUFFER )
2000         out = (uint8_t *) malloc( RTMP_PING_SIZE_CLEAR_PLAYING_BUFFER * sizeof( uint8_t ) );
2001     else if( type == RTMP_PING_BUFFER_TIME_CLIENT )
2002     {
2003         out = (uint8_t *) malloc( RTMP_PING_SIZE_BUFFER_TIME_CLIENT * sizeof( uint8_t ) );
2004         if( !out ) goto error;
2005         third_arg = hton32( third_arg );
2006         memcpy( out + 6, &third_arg, sizeof( uint32_t ) );
2007     }
2008     else if( type == RTMP_PING_RESET_STREAM )
2009     {
2010         out = (uint8_t *) malloc( RTMP_PING_SIZE_RESET_STREAM * sizeof( uint8_t ) );
2011     }
2012 /*    else if( type == RTMP_PING_CLIENT_FROM_SERVER ) TODO: research this
2013     {
2014     }
2015     else if( type == RTMP_PING_PONG_FROM_CLIENT )
2016     {
2017     }
2018 */    else
2019     {
2020         out = (uint8_t *) malloc( RTMP_PING_SIZE_BUFFER_TIME_CLIENT * sizeof( uint8_t ) );
2021         if( !out ) goto error;
2022         out[6] = 0x0D; out[7] = 0x0E; out[8] = 0x0A; out[9] = 0x0D;
2023     }
2024
2025     if( !out ) goto error;
2026
2027     type = hton16( type );
2028     memcpy( out, &type, sizeof( uint16_t ) );
2029
2030     src_dst = hton32( src_dst );
2031     memcpy( out + 2, &src_dst, sizeof( uint32_t ) );
2032
2033     return out;
2034
2035 error:
2036     return NULL;
2037 }
2038
2039 /*****************************************************************************
2040  * AMF implementation:
2041  ******************************************************************************/
2042 static uint8_t *
2043 amf_encode_element( uint8_t element, const void *value )
2044 {
2045     uint8_t *out;
2046
2047     if ( element == AMF_DATATYPE_NUMBER )
2048     {
2049         uint64_t number = *(uint64_t *) value;
2050
2051         out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2052         if( !out ) return NULL;
2053         
2054         number = hton64( number );
2055         out[0] = AMF_DATATYPE_NUMBER;
2056         memcpy( out + 1, &number, sizeof( uint64_t ) );
2057     } else if ( element == AMF_DATATYPE_BOOLEAN )
2058     {
2059         out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_BOOLEAN * sizeof( uint8_t ) );
2060         if( !out ) return NULL;
2061
2062         out[0] = AMF_DATATYPE_BOOLEAN;
2063         out[1] = *(uint8_t *) value;
2064     } else if ( element == AMF_DATATYPE_STRING )
2065     {
2066         uint16_t length_psz, length_psz_cpy;
2067
2068         length_psz = length_psz_cpy = strlen( (char *) value );
2069         out = (uint8_t *) malloc( ( AMF_DATATYPE_SIZE_STRING + length_psz ) * sizeof( uint8_t ) );
2070         if( !out ) return NULL;
2071
2072         out[0] = AMF_DATATYPE_STRING;
2073         length_psz = hton16( length_psz );
2074         memcpy( out + 1, &length_psz, sizeof( uint16_t ) );
2075         memcpy( out + 3, value, length_psz_cpy );
2076     } else if ( element == AMF_DATATYPE_OBJECT )
2077     {
2078         out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_OBJECT * sizeof( uint8_t ) );
2079         if( !out ) return NULL;
2080
2081         out[0] = AMF_DATATYPE_OBJECT;
2082     } else if ( element == AMF_DATATYPE_NULL )
2083     {
2084         out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NULL * sizeof( uint8_t ) );
2085         if( !out ) return NULL;
2086
2087         out[0] = AMF_DATATYPE_NULL;
2088     } else if ( element == AMF_DATATYPE_MIXED_ARRAY )
2089     {
2090         uint32_t highest_index = *(uint32_t *) value;
2091
2092         out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_MIXED_ARRAY * sizeof( uint8_t ) );
2093         if( !out ) return NULL;
2094
2095         highest_index = hton32( highest_index );
2096         out[0] = AMF_DATATYPE_MIXED_ARRAY;
2097         memcpy( out + 1, &highest_index, sizeof( uint32_t ) );
2098     } else if ( element == AMF_DATATYPE_END_OF_OBJECT )
2099     {
2100         out = (uint8_t *) calloc( AMF_DATATYPE_SIZE_END_OF_OBJECT, sizeof( uint8_t ) );
2101
2102         out[AMF_DATATYPE_SIZE_END_OF_OBJECT - 1] = AMF_DATATYPE_END_OF_OBJECT;
2103     } else
2104     {
2105         out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2106         if( !out ) return NULL;
2107
2108         out[0] = AMF_DATATYPE_NUMBER;
2109         out[1] = 0x0D; out[2] = 0x0E; out[3] = 0x0A; out[4] = 0x0D;
2110         out[5] = 0x0B; out[6] = 0x0E; out[7] = 0x0E; out[8] = 0x0F;
2111     }
2112
2113     return out;
2114 }
2115
2116 static uint8_t *
2117 amf_encode_object_variable( const char *key, uint8_t element, const void *value )
2118 {
2119     uint8_t *out, *out_value;
2120     int length_value;
2121     uint16_t length_psz, length_psz_cpy;
2122
2123     length_psz = length_psz_cpy = strlen( key );
2124
2125     if( element == AMF_DATATYPE_NUMBER )
2126         length_value = AMF_DATATYPE_SIZE_NUMBER;
2127     else if( element == AMF_DATATYPE_BOOLEAN )
2128         length_value = AMF_DATATYPE_SIZE_BOOLEAN;
2129     else if( element == AMF_DATATYPE_STRING )
2130         length_value = AMF_DATATYPE_SIZE_STRING + strlen( (char *) value );
2131     else if( element == AMF_DATATYPE_NULL )
2132         length_value = AMF_DATATYPE_SIZE_NULL;
2133     else
2134     {
2135         out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2136         if( !out ) return NULL;
2137
2138         out[0] = AMF_DATATYPE_NUMBER;
2139         out[1] = 0xD; out[2] = 0xE; out[3] = 0xA; out[4] = 0xD;
2140         out[5] = 0xB; out[6] = 0xE; out[7] = 0xE; out[8] = 0xF;
2141
2142         return out;
2143     }
2144
2145     out = (uint8_t *) malloc( ( AMF_DATATYPE_SIZE_OBJECT_VARIABLE + length_psz + length_value ) * sizeof( uint8_t ) );
2146     if( !out ) return NULL;
2147
2148     length_psz = hton16( length_psz );
2149     memcpy( out, &length_psz, sizeof( uint16_t ) );
2150     memcpy( out + 2, key, length_psz_cpy );
2151
2152     out_value = amf_encode_element( element, value );
2153     memcpy( out + 2 + length_psz_cpy, out_value, length_value );
2154     free( out_value );
2155
2156     return out;
2157 }
2158
2159 static double
2160 amf_decode_number( uint8_t **buffer )
2161 {
2162     uint64_t number;
2163     double out;
2164
2165     number = ntoh64( *(uint64_t *) *buffer );
2166     memcpy(&out, &number, sizeof( uint64_t ) );
2167     *buffer += sizeof( uint64_t );
2168
2169     return out;
2170 }
2171
2172 static int
2173 amf_decode_boolean( uint8_t **buffer )
2174 {
2175     int out;
2176
2177     out = **buffer;
2178     *buffer += 1;
2179
2180     return out;
2181 }
2182
2183 /* return value allocated dinamically */
2184 static char *
2185 amf_decode_string( uint8_t **buffer )
2186 {
2187     char *out;
2188     int length;
2189     int i;
2190
2191     length = ntoh16( *(uint16_t *) *buffer );
2192     *buffer += sizeof( uint16_t );
2193
2194     out = (char *) malloc( length + 1 ); /* '\0' terminated */
2195     if( !out ) return NULL;
2196
2197     for(i = 0; i < length; i++)
2198         out[i] = (*buffer)[i];
2199
2200     *buffer += length;
2201
2202     out[i] = '\0';
2203
2204     return out;
2205 }
2206
2207 /* returns in each call next key, at end of object returns NULL */
2208 /* need to decode value of key after call */
2209 static char *
2210 amf_decode_object( uint8_t **buffer )
2211 {
2212     if( **buffer == 0x0 && *(*buffer + 1) == 0x00 && *(*buffer + 2) == 0x09)
2213     {
2214         *buffer += 3;
2215
2216         return NULL;
2217     }
2218     else
2219         return amf_decode_string( buffer );
2220 }
2221
2222 /*****************************************************************************
2223  * FLV rebuilding implementation:
2224  ******************************************************************************/
2225 static void
2226 flv_rebuild( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
2227 {
2228     uint32_t length_tag, timestamp;
2229     uint8_t *tmp;
2230
2231     tmp = (uint8_t *) realloc( rtmp_packet->body->body,
2232                                rtmp_packet->body->length_body +
2233                                FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE );
2234     if( !tmp ) return;
2235     rtmp_packet->body->body = tmp;
2236     memmove( rtmp_packet->body->body + FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE,
2237              rtmp_packet->body->body, rtmp_packet->body->length_body );
2238
2239     /* Insert tag */
2240     p_thread->flv_tag_previous_tag_size = hton32( p_thread->flv_tag_previous_tag_size );
2241     memcpy( rtmp_packet->body->body, &p_thread->flv_tag_previous_tag_size, sizeof( uint32_t ) );
2242
2243     /* Fill backwards because of overlapping*/
2244     rtmp_packet->body->body[11] = 0x00;
2245
2246     timestamp = hton32( rtmp_packet->timestamp );
2247     memcpy( rtmp_packet->body->body + 7, &timestamp, sizeof( uint32_t ) );
2248
2249     length_tag = hton32( rtmp_packet->body->length_body );
2250     memcpy( rtmp_packet->body->body + 4, &length_tag, sizeof( uint32_t ) );
2251
2252     rtmp_packet->body->body[4] = rtmp_packet->content_type;
2253
2254     rtmp_packet->body->body[12] = 0x00;
2255     rtmp_packet->body->body[13] = 0x00;
2256     rtmp_packet->body->body[14] = 0x00;
2257
2258     p_thread->flv_tag_previous_tag_size = rtmp_packet->body->length_body + FLV_TAG_SIZE;
2259
2260     /* Update size */
2261     rtmp_packet->body->length_body += FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE;
2262     rtmp_packet->body->length_buffer = rtmp_packet->body->length_body;
2263 }
2264
2265 static void
2266 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 )
2267 {
2268     uint8_t data_audio;
2269
2270     data_audio = *packet_audio->body->body;
2271
2272     if( ( data_audio & FLV_AUDIO_STEREO_MASK ) == FLV_AUDIO_STEREO_MONO )
2273         *stereo = FLV_AUDIO_STEREO_MONO;
2274     else if( ( data_audio & FLV_AUDIO_STEREO_MASK ) == FLV_AUDIO_STEREO_STEREO )
2275         *stereo = FLV_AUDIO_STEREO_STEREO;
2276     else
2277         msg_Warn( p_thread, "unknown metadata audio stereo" );
2278
2279     if( ( data_audio & FLV_AUDIO_SIZE_MASK ) == FLV_AUDIO_SIZE_8_BIT )
2280         *audiosamplesize = FLV_AUDIO_SIZE_8_BIT >> 1;
2281     else if( ( data_audio & FLV_AUDIO_SIZE_MASK ) == FLV_AUDIO_SIZE_16_BIT )
2282         *audiosamplesize = FLV_AUDIO_SIZE_16_BIT >> 1;
2283     else
2284         msg_Warn( p_thread, "unknown metadata audio sample size" );
2285
2286     if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_5_5_KHZ )
2287         *audiosamplerate = 5512;
2288     else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_11_KHZ )
2289         *audiosamplerate = 11025;
2290     else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_22_KHZ )
2291         *audiosamplerate = 22050;
2292     else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_44_KHZ )
2293         *audiosamplerate = 44100;
2294     else
2295         msg_Warn( p_thread, "unknown metadata audio sample rate" );
2296
2297     if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_UNCOMPRESSED )
2298         *audiocodecid = FLV_AUDIO_CODEC_ID_UNCOMPRESSED >> 4;
2299     else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_ADPCM )
2300         *audiocodecid = FLV_AUDIO_CODEC_ID_ADPCM >> 4;
2301     else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_MP3 )
2302         *audiocodecid = FLV_AUDIO_CODEC_ID_MP3 >> 4;
2303     else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_NELLYMOSER_8KHZ_MONO )
2304         *audiocodecid = FLV_AUDIO_CODEC_ID_NELLYMOSER_8KHZ_MONO >> 4;
2305     else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_NELLYMOSER )
2306         *audiocodecid = FLV_AUDIO_CODEC_ID_NELLYMOSER >> 4;
2307     else
2308         msg_Warn( p_thread, "unknown metadata audio codec id" );
2309 }
2310
2311 static void
2312 flv_get_metadata_video( rtmp_control_thread_t *p_thread, rtmp_packet_t *packet_video, uint8_t *videocodecid, uint8_t *frametype )
2313 {
2314     uint8_t data_video;
2315
2316     data_video = *packet_video->body->body;
2317
2318     if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SORENSEN_H263 )
2319         *videocodecid = FLV_VIDEO_CODEC_ID_SORENSEN_H263;
2320     else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SCREEN_VIDEO )
2321         *videocodecid = FLV_VIDEO_CODEC_ID_SCREEN_VIDEO;
2322     else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_ON2_VP6 )
2323         *videocodecid = FLV_VIDEO_CODEC_ID_ON2_VP6;
2324     else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_ON2_VP6_ALPHA )
2325         *videocodecid = FLV_VIDEO_CODEC_ID_ON2_VP6_ALPHA;
2326     else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SCREEN_VIDEO_2 )
2327         *videocodecid = FLV_VIDEO_CODEC_ID_SCREEN_VIDEO_2;
2328     else
2329         msg_Warn( p_thread, "unknown metadata video codec id" );
2330
2331     if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_KEYFRAME )
2332         *frametype = FLV_VIDEO_FRAME_TYPE_KEYFRAME >> 4;
2333     else if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_INTER_FRAME )
2334         *frametype = FLV_VIDEO_FRAME_TYPE_INTER_FRAME >> 4;
2335     else if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_DISPOSABLE_INTER_FRAME )
2336         *frametype = FLV_VIDEO_FRAME_TYPE_DISPOSABLE_INTER_FRAME >> 4;
2337     else
2338         msg_Warn( p_thread, "unknown metadata video frame type" );
2339 }
2340
2341 static rtmp_packet_t *
2342 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 )
2343 {
2344     rtmp_packet_t *rtmp_packet;
2345     rtmp_body_t *rtmp_body;
2346     uint8_t *tmp_buffer;
2347     double number;
2348
2349     rtmp_body = rtmp_body_new( -1 );
2350
2351     tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onMetaData" );
2352     rtmp_body_append( rtmp_body, tmp_buffer,
2353         AMF_DATATYPE_SIZE_STRING + strlen( "onMetaData" ) );
2354     free( tmp_buffer );
2355
2356     number = 0;
2357     tmp_buffer = amf_encode_element( AMF_DATATYPE_MIXED_ARRAY, &number );
2358     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_MIXED_ARRAY );
2359     free( tmp_buffer );
2360
2361     number = duration;
2362     tmp_buffer = amf_encode_object_variable( "duration",
2363         AMF_DATATYPE_NUMBER, &number );
2364     rtmp_body_append( rtmp_body, tmp_buffer,
2365         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "duration" ) +
2366         AMF_DATATYPE_SIZE_NUMBER );
2367     free( tmp_buffer );
2368
2369     tmp_buffer = amf_encode_object_variable( "stereo",
2370         AMF_DATATYPE_BOOLEAN, &stereo );
2371     rtmp_body_append( rtmp_body, tmp_buffer,
2372         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "stereo" ) +
2373         AMF_DATATYPE_SIZE_BOOLEAN );
2374     free( tmp_buffer );
2375
2376     number = audiosamplesize;
2377     tmp_buffer = amf_encode_object_variable( "audiosamplesize",
2378         AMF_DATATYPE_NUMBER, &number );
2379     rtmp_body_append( rtmp_body, tmp_buffer,
2380         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiosamplesize" ) +
2381         AMF_DATATYPE_SIZE_NUMBER );
2382     free( tmp_buffer );
2383
2384     number = audiosamplerate;
2385     tmp_buffer = amf_encode_object_variable( "audiosamplerate",
2386         AMF_DATATYPE_NUMBER, &number );
2387     rtmp_body_append( rtmp_body, tmp_buffer,
2388         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiosamplerate" ) +
2389         AMF_DATATYPE_SIZE_NUMBER );
2390     free( tmp_buffer );
2391
2392     number = audiocodecid;
2393     tmp_buffer = amf_encode_object_variable( "audiocodecid",
2394         AMF_DATATYPE_NUMBER, &number );
2395     rtmp_body_append( rtmp_body, tmp_buffer,
2396         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiocodecid" ) +
2397         AMF_DATATYPE_SIZE_NUMBER );
2398     free( tmp_buffer );
2399
2400     number = videocodecid;
2401     tmp_buffer = amf_encode_object_variable( "videocodecid",
2402         AMF_DATATYPE_NUMBER, &number );
2403     rtmp_body_append( rtmp_body, tmp_buffer,
2404         AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "videocodecid" ) +
2405         AMF_DATATYPE_SIZE_NUMBER );
2406     free( tmp_buffer );
2407
2408     tmp_buffer = amf_encode_element( AMF_DATATYPE_END_OF_OBJECT, NULL );
2409     rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
2410     free( tmp_buffer );
2411
2412     rtmp_packet = rtmp_new_packet( p_access->p_sys->p_thread,
2413         RTMP_DEFAULT_STREAM_INDEX_INVOKE,
2414         0, RTMP_CONTENT_TYPE_NOTIFY, 0, rtmp_body );
2415     free( rtmp_body->body );
2416     free( rtmp_body );
2417
2418     return rtmp_packet;
2419 }
2420
2421 block_t *
2422 flv_get_metadata( access_t *p_access )
2423 {
2424     rtmp_control_thread_t *p_thread=p_access->p_sys->p_thread;
2425     block_t *p_buf;
2426
2427     rtmp_packet_t *p_md = flv_build_onMetaData( p_access, 0,
2428         p_thread->metadata_stereo,
2429         p_thread->metadata_samplesize,
2430         p_thread->metadata_samplerate,
2431         p_thread->metadata_audiocodecid,
2432         p_thread->metadata_videocodecid );
2433
2434     flv_rebuild( p_thread, p_md );
2435     p_buf = rtmp_new_block( p_thread,
2436                             p_md->body->body, p_md->body->length_buffer );
2437
2438     rtmp_packet_free( p_md );
2439     return p_buf;
2440 }
2441
2442 block_t *
2443 flv_insert_header( access_t *p_access, block_t *first_packet )
2444 {
2445     access_sys_t *p_sys = p_access->p_sys;
2446     int old_buffer_size;
2447     uint32_t tmp_number;
2448
2449     old_buffer_size = first_packet->i_buffer;
2450
2451     first_packet = block_Realloc( first_packet, 0, first_packet->i_buffer + FLV_HEADER_SIZE );
2452
2453     memmove( first_packet->p_buffer + FLV_HEADER_SIZE,
2454         first_packet->p_buffer, old_buffer_size );
2455
2456     memcpy( first_packet->p_buffer, FLV_HEADER_SIGNATURE, sizeof( FLV_HEADER_SIGNATURE ) );
2457     first_packet->p_buffer[3] = FLV_HEADER_VERSION;
2458     if( p_sys->p_thread->has_audio && p_sys->p_thread->has_video )
2459         first_packet->p_buffer[4] = FLV_HEADER_AUDIO | FLV_HEADER_VIDEO;
2460     else if( p_sys->p_thread->has_audio )
2461         first_packet->p_buffer[4] = FLV_HEADER_AUDIO;
2462     else
2463         first_packet->p_buffer[4] = FLV_HEADER_VIDEO;
2464     tmp_number = hton32( FLV_HEADER_SIZE );
2465     memcpy( first_packet->p_buffer + 5, &tmp_number, sizeof( uint32_t ) );
2466
2467     return first_packet;
2468 }
2469
2470 void
2471 rtmp_init_handler( rtmp_handler_t *rtmp_handler )
2472 {
2473     rtmp_handler[RTMP_CONTENT_TYPE_CHUNK_SIZE] = rtmp_handler_chunk_size;
2474     rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_02] = rtmp_handler_null;
2475     rtmp_handler[RTMP_CONTENT_TYPE_BYTES_READ] = rtmp_handler_null;
2476     rtmp_handler[RTMP_CONTENT_TYPE_PING] = rtmp_handler_null;
2477     rtmp_handler[RTMP_CONTENT_TYPE_SERVER_BW] = rtmp_handler_null;
2478     rtmp_handler[RTMP_CONTENT_TYPE_CLIENT_BW] = rtmp_handler_null;
2479     rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_07] = rtmp_handler_null;
2480     rtmp_handler[RTMP_CONTENT_TYPE_AUDIO_DATA] = rtmp_handler_audio_data;
2481     rtmp_handler[RTMP_CONTENT_TYPE_VIDEO_DATA] = rtmp_handler_video_data;
2482     rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_0A_0E] = rtmp_handler_null;
2483     rtmp_handler[RTMP_CONTENT_TYPE_FLEX_STREAM] = rtmp_handler_null;
2484     rtmp_handler[RTMP_CONTENT_TYPE_FLEX_SHARED_OBJECT] = rtmp_handler_null;
2485     rtmp_handler[RTMP_CONTENT_TYPE_MESSAGE] = rtmp_handler_null;
2486     rtmp_handler[RTMP_CONTENT_TYPE_NOTIFY] = rtmp_handler_notify;
2487     rtmp_handler[RTMP_CONTENT_TYPE_SHARED_OBJECT] = rtmp_handler_null;
2488     rtmp_handler[RTMP_CONTENT_TYPE_INVOKE] = rtmp_handler_invoke;
2489 }