1 /*****************************************************************************
2 * rtmp_amf_flv.c: RTMP, AMF and FLV over RTMP implementation.
3 *****************************************************************************
4 * Copyright (C) URJC - LADyR - Luis Lopez Fernandez
6 * Author: Miguel Angel Cabrera Moya
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.
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.
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 *****************************************************************************/
23 /*****************************************************************************
25 ******************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_access.h>
33 #include <vlc_network.h> /* DOWN: #include <network.h> */
35 #include <vlc_block.h>
42 #include "rtmp_amf_flv.h"
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;
52 const uint8_t RTMP_HEADER_STREAM_MAX = 64;
53 const uint8_t RTMP_HEADER_STREAM_INDEX_MASK = 0x3F;
56 const uint8_t RTMP_HANDSHAKE = 0x03;
57 const uint16_t RTMP_HANDSHAKE_BODY_SIZE = 1536;
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;
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;
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;
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;
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;
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 /*****************************************************************************
130 ******************************************************************************/
132 /* boolean constants */
133 const uint8_t AMF_BOOLEAN_FALSE = 0x00;
134 const uint8_t AMF_BOOLEAN_TRUE = 0x01;
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;
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;
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;
176 /*****************************************************************************
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 */
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;
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;
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;
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;
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;
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;
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;
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 );
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 );
246 static void rtmp_body_append( rtmp_body_t *rtmp_body, uint8_t *buffer, uint32_t length );
248 static uint8_t *rtmp_encode_ping( uint16_t type, uint32_t src_dst, uint32_t third_arg, uint32_t fourth_arg );
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 );
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 );
268 /*****************************************************************************
269 * RTMP implementation:
270 ******************************************************************************/
272 rtmp_handshake_passive( vlc_object_t *p_this, int fd )
274 uint8_t p_read[RTMP_HANDSHAKE_BODY_SIZE + 1];
275 uint8_t p_write[RTMP_HANDSHAKE_BODY_SIZE * 2 + 1];
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 )
283 msg_Err( p_this, "failed to receive handshake" );
287 /* Check handshake */
288 if ( p_read[0] != RTMP_HANDSHAKE )
290 msg_Err( p_this, "first byte in handshake corrupt" );
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 );
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 )
303 msg_Err( p_this, "failed to send handshake" );
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 )
311 msg_Err( p_this, "failed to receive acknowledge" );
315 /* Check acknowledge */
316 for(i = 8; i < RTMP_HANDSHAKE_BODY_SIZE; i++ )
317 if( p_write[i + 1] != p_read[i] )
319 msg_Err( p_this, "body acknowledge received corrupt" );
327 rtmp_handshake_active( vlc_object_t *p_this, int fd )
329 uint8_t p_read[RTMP_HANDSHAKE_BODY_SIZE * 2 + 1];
330 uint8_t p_write[RTMP_HANDSHAKE_BODY_SIZE + 1];
334 p_write[0] = RTMP_HANDSHAKE;
336 for( i = 0; i < 8; i++ )
337 p_write[i + 1] = 0x00;
339 vlc_rand_bytes( p_write+1+8, RTMP_HANDSHAKE_BODY_SIZE-8 );
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 )
345 msg_Err( p_this, "failed to send handshake" );
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 )
353 msg_Err( p_this, "failed to receive handshake" );
357 /* Check handshake */
358 if( p_read[0] != RTMP_HANDSHAKE )
360 msg_Err( p_this, "first byte in handshake received corrupt" );
364 for(i = 8; i < RTMP_HANDSHAKE_BODY_SIZE; i++ )
365 if( p_write[i + 1] != p_read[i + 1 + RTMP_HANDSHAKE_BODY_SIZE] )
367 msg_Err( p_this, "body handshake received corrupt" );
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 )
375 msg_Err( p_this, "failed to acknowledge handshake" );
383 write_rtmp( rtmp_control_thread_t *p_thread, uint8_t *buf,
384 rtmp_packet_t *pkt, const char *errmsg )
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 );
394 msg_Err( p_thread, "%s", errmsg );
401 rtmp_connect_active( rtmp_control_thread_t *p_thread )
403 rtmp_packet_t *rtmp_packet;
404 rtmp_body_t *rtmp_body;
408 /* Build NetConnection.connect call */
409 rtmp_body = rtmp_body_new( -1 );
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" ) );
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 );
421 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
422 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
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 ) );
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" ) );
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 ) );
446 if( asprintf( &tmp_url, "rtmp://%s", p_thread->url.psz_buffer ) == -1 )
448 free( rtmp_body->body );
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 ) );
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 );
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 );
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 );
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 );
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 ) );
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 );
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 );
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 );
511 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
513 /* Call NetConnection.connect */
514 if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
515 "failed send call NetConnection.connect" ) )
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 );
523 if( p_thread->result_connect )
525 msg_Err( p_thread, "failed call NetConnection.connect" );
529 /* Force control thread to stop if receive NetStream.play call and wait is not ready */
530 vlc_mutex_lock( &p_thread->lock );
532 /* Build NetStream.createStream call */
533 rtmp_body = rtmp_body_new( -1 );
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" ) );
540 p_thread->stream_client_id = RTMP_DEFAULT_STREAM_CLIENT_ID;
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 );
547 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
548 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
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 );
556 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
558 /* Call NetStream.createStream */
559 if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
560 "failed send call NetStream.createStream" ) )
563 /*TODO: read server stream number*/
564 /* Build ping packet */
565 rtmp_body = rtmp_body_new( -1 );
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 );
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 );
576 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
578 /* Send ping packet */
579 if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
580 "failed send ping BUFFER_TIME_CLIENT" ) )
583 /* Build NetStream.play call */
584 rtmp_body = rtmp_body_new( -1 );
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" ) );
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 );
596 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
597 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
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 ) );
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 );
610 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
612 /* Call NetStream.play */
613 if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
614 "failed send call NetStream.play" ) )
618 /* Build ping packet */
619 rtmp_body = rtmp_body_new( -1 );
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 );
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 );
630 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
632 /* Send ping packet */
633 if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
634 "failed send ping BUFFER_TIME_CLIENT" ) )
637 /* Wait for NetStream.play.start result */
638 vlc_cond_wait( &p_thread->wait, &p_thread->lock );
639 vlc_mutex_unlock( &p_thread->lock );
641 if( p_thread->result_play )
643 msg_Err( p_thread, "failed call NetStream.play" );
647 /* Next packet is the beginning of flv stream */
648 msg_Dbg( p_thread, "next packet is the beginning of flv stream" );
654 rtmp_connect_passive( rtmp_control_thread_t *p_thread )
656 /* Force control thread to stop if receive NetStream.play call and wait is not ready */
657 vlc_mutex_lock( &p_thread->lock );
659 /* Wait for NetStream.play.start result */
660 vlc_cond_wait( &p_thread->wait, &p_thread->lock );
661 vlc_mutex_unlock( &p_thread->lock );
663 if( p_thread->result_play )
665 msg_Err( p_thread, "failed call NetStream.play" );
673 rtmp_packet_free( rtmp_packet_t *pkt )
675 free( pkt->body->body );
682 rtmp_seek( access_t *p_access, int64_t i_pos )
684 access_sys_t *p_sys = p_access->p_sys;
685 rtmp_packet_t *rtmp_packet;
686 rtmp_body_t *rtmp_body;
690 msg_Warn(p_access, "i_pos %lld", i_pos);
691 // Build NetStream.seek call //
692 rtmp_body = rtmp_body_new();
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" ) );
700 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
702 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
705 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
706 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
708 //TODO: convert i_pos to double and see if they are milliseconds
709 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
711 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
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 );
719 tmp_buffer = rtmp_encode_packet( p_access, rtmp_packet );
721 // Call NetStream.seek //
722 if( !write_rtmp( p_thread, tmp_buffer, rtmp_packet,
723 "failed call NetStream.seek" ) )
726 // Receive TODO: see what //
727 rtmp_packet = rtmp_read_net_packet( p_access, p_sys->fd );
728 rtmp_packet_free( rtmp_packet );
735 rtmp_build_bytes_read( rtmp_control_thread_t *p_thread, uint32_t reply )
737 rtmp_packet_t *rtmp_packet;
738 rtmp_body_t *rtmp_body;
741 /* Build bytes read packet */
742 rtmp_body = rtmp_body_new( -1 );
744 tmp_buffer = (uint8_t *) malloc( sizeof( uint32_t ) );
745 if( !tmp_buffer ) return NULL;
747 reply = hton32( reply );
748 memcpy( tmp_buffer, &reply, sizeof( uint32_t ) );
750 rtmp_body_append( rtmp_body, tmp_buffer, sizeof( uint32_t ) );
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 );
762 rtmp_build_publish_start( rtmp_control_thread_t *p_thread )
764 rtmp_packet_t *rtmp_packet;
765 rtmp_body_t *rtmp_body;
768 /* Build publish start event */
769 rtmp_body = rtmp_body_new( -1 );
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" ) );
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 );
781 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
782 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
785 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
786 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
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" ) );
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" ) );
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( "" ) );
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 ) );
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 );
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 );
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 );
837 rtmp_build_flv_over_rtmp( rtmp_control_thread_t *p_thread, block_t *p_buffer )
839 rtmp_packet_t *rtmp_packet;
841 if( p_thread->flv_length_body > 0 )
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 );
846 if( p_thread->flv_length_body > 0 )
852 p_thread->flv_content_type = *p_buffer->p_buffer;
854 p_buffer->p_buffer[0] = 0;
855 p_thread->flv_length_body = ntoh32( *(uint32_t *) (p_buffer->p_buffer) );
857 p_buffer->p_buffer[3] = 0;
858 p_thread->flv_timestamp = ntoh32( *(uint32_t *) (p_buffer->p_buffer + 3) );
861 if( p_thread->flv_length_body > p_buffer->i_buffer - FLV_TAG_SIZE - FLV_TAG_PREVIOUS_TAG_SIZE )
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 );
870 rtmp_body_append( p_thread->flv_body, p_buffer->p_buffer + FLV_TAG_SIZE, p_thread->flv_length_body );
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 );
881 /* This function must be cancellation-safe! */
883 rtmp_read_net_packet( rtmp_control_thread_t *p_thread )
889 rtmp_packet_t *header;
894 i_ret = net_Read( p_thread, p_thread->fd, NULL, p_read, 1, true );
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;
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 )
907 /* Update timestamp if not is an interchunk packet */
908 if( length_header == 1 && header->body == NULL )
910 header->timestamp += header->timestamp_relative;
913 /* Length 4 and 8 headers have relative timestamp */
914 if( length_header == 4 || length_header == 8 )
918 header->timestamp_relative = ntoh32( *(uint32_t *) p_read );
919 header->timestamp += header->timestamp_relative;
922 if( length_header >= 8 )
926 header->length_body = ntoh32( *(uint32_t *) (p_read + 3) );
927 header->content_type = p_read[7];
930 /* Length 12 headers have absolute timestamp */
931 if( length_header >= 12 )
935 header->timestamp = ntoh32( *(uint32_t *) p_read );
936 header->src_dst = ntoh32( *(uint32_t *) (p_read + 8) );
939 if( header->body == NULL )
941 header->body = rtmp_body_new( header->length_body );
944 bytes_left = header->body->length_buffer - header->body->length_body;
946 if( bytes_left > p_thread->chunk_size_recv )
947 bytes_left = p_thread->chunk_size_recv;
949 i_ret = net_Read( p_thread, p_thread->fd, NULL,
950 header->body->body + header->body->length_body, bytes_left, true );
952 if( i_ret != (ssize_t)bytes_left )
955 header->body->length_body += bytes_left;
957 if( header->length_body == header->body->length_body )
959 rtmp_packet_t *rpkt = (rtmp_packet_t*)malloc(sizeof(rtmp_packet_t));
960 if( !rpkt ) return NULL;
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;
977 msg_Err( p_thread, "rtmp_read_net_packet: net_Read error");
983 rtmp_handler_null( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
985 VLC_UNUSED(p_thread);
986 rtmp_packet_free( rtmp_packet );
990 rtmp_handler_chunk_size( rtmp_control_thread_t *p_thread,
991 rtmp_packet_t *rtmp_packet )
993 p_thread->chunk_size_recv =
994 ntoh32( *(uint32_t *) (rtmp_packet->body->body) );
995 rtmp_packet_free( rtmp_packet );
999 rtmp_handler_audio_data( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1003 if( !p_thread->has_audio )
1005 p_thread->has_audio = 1;
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 );
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 );
1016 rtmp_packet_free( rtmp_packet );
1020 rtmp_handler_video_data( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1024 if( !p_thread->has_video )
1026 p_thread->has_video = 1;
1028 flv_get_metadata_video( p_thread, rtmp_packet,
1029 &p_thread->metadata_videocodecid, &p_thread->metadata_frametype );
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 );
1036 rtmp_packet_free( rtmp_packet );
1040 rtmp_handler_notify( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1044 p_thread->metadata_received = 1;
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 );
1050 rtmp_packet_free( rtmp_packet );
1054 rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1056 rtmp_packet_t *tmp_rtmp_packet;
1057 uint8_t *i, *end, *tmp_buffer;
1059 char *string, *string2;
1062 i = rtmp_packet->body->body;
1063 end = rtmp_packet->body->body + rtmp_packet->body->length_body;
1065 i++; /* Pass over AMF_DATATYPE_STRING */
1066 string = amf_decode_string( &i );
1069 msg_Err(p_thread,"Seriously broken stream");
1073 i++; /* Pass over AMF_DATATYPE_NUMBER */
1074 number = amf_decode_number( &i );
1076 msg_Dbg( p_thread, "%s %.1f", string, number );
1078 if( strcmp( "connect", string ) == 0 )
1080 /* Connection bandwith */
1081 tmp_rtmp_packet = rtmp_encode_onBWDone( p_thread, AMF_CALL_ONBWDONE );
1083 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1085 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1086 if( i_ret != tmp_rtmp_packet->length_encoded )
1088 msg_Err( p_thread, "failed send connection bandwith" );
1091 rtmp_packet_free( rtmp_packet );
1094 /* Server bandwith */
1095 tmp_rtmp_packet = rtmp_encode_server_bw( p_thread, RTMP_SERVER_BW );
1097 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1099 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1100 if( i_ret != tmp_rtmp_packet->length_encoded )
1102 msg_Err( p_thread, "failed send server bandwith" );
1105 rtmp_packet_free( rtmp_packet );
1109 tmp_rtmp_packet = rtmp_encode_ping_clear_stream( p_thread, RTMP_SRC_DST_CONNECT_OBJECT );
1111 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1113 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1114 if( i_ret != tmp_rtmp_packet->length_encoded )
1116 msg_Err( p_thread, "failed send clear stream" );
1119 rtmp_packet_free( rtmp_packet );
1122 /* Reply NetConnection.connect */
1123 tmp_rtmp_packet = rtmp_encode_NetConnection_connect_result( p_thread, number );
1125 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1127 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1128 if( i_ret != tmp_rtmp_packet->length_encoded )
1130 msg_Err( p_thread, "failed send reply NetConnection.connect" );
1133 rtmp_packet_free( rtmp_packet );
1136 else if( strcmp( "createStream", string ) == 0 )
1138 p_thread->stream_client_id = number;
1139 p_thread->stream_server_id = RTMP_DEFAULT_STREAM_SERVER_ID;
1141 /* Reply createStream */
1142 tmp_rtmp_packet = rtmp_encode_createStream_result( p_thread, p_thread->stream_client_id, p_thread->stream_server_id );
1144 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1146 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1147 if( i_ret != tmp_rtmp_packet->length_encoded )
1149 msg_Err( p_thread, "failed send reply createStream" );
1152 rtmp_packet_free( rtmp_packet );
1156 tmp_rtmp_packet = rtmp_encode_ping_reset_stream( p_thread );
1158 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1160 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1161 if( i_ret != tmp_rtmp_packet->length_encoded )
1163 msg_Err( p_thread, "failed send reset stream" );
1166 rtmp_packet_free( rtmp_packet );
1170 tmp_rtmp_packet = rtmp_encode_ping_clear_stream( p_thread, RTMP_SRC_DST_CONNECT_OBJECT2 );
1172 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1174 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1175 if( i_ret != tmp_rtmp_packet->length_encoded )
1177 msg_Err( p_thread, "failed send clear stream" );
1180 rtmp_packet_free( rtmp_packet );
1183 else if( strcmp( "publish", string ) == 0 )
1186 msg_Dbg( p_thread, "null" );
1189 string2 = amf_decode_string( &i );
1190 msg_Dbg( p_thread, "string: %s", string2 );
1192 p_thread->psz_publish = strdup( string2 );
1196 else if( strcmp( "play", string ) == 0 )
1199 msg_Dbg( p_thread, "null" );
1202 string2 = amf_decode_string( &i );
1203 msg_Dbg( p_thread, "string: %s", string2 );
1205 /* Reply NetStream.play.reset */
1206 tmp_rtmp_packet = rtmp_encode_NetStream_play_reset_onStatus( p_thread, string2 );
1208 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1210 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1211 if( i_ret != tmp_rtmp_packet->length_encoded )
1213 msg_Err( p_thread, "failed send reply NetStream.play.reset" );
1216 rtmp_packet_free( rtmp_packet );
1219 /* Reply NetStream.play.start */
1220 tmp_rtmp_packet = rtmp_encode_NetStream_play_start_onStatus( p_thread, string2 );
1222 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1224 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1225 if( i_ret != tmp_rtmp_packet->length_encoded )
1227 msg_Err( p_thread, "failed send reply NetStream.play.start" );
1230 rtmp_packet_free( rtmp_packet );
1235 p_thread->result_play = 0;
1237 vlc_mutex_lock( &p_thread->lock );
1238 vlc_cond_signal( &p_thread->wait );
1239 vlc_mutex_unlock( &p_thread->lock );
1246 if( *i == AMF_DATATYPE_NUMBER )
1249 msg_Dbg( p_thread, "number: %le", amf_decode_number( &i ) );
1251 else if( *i == AMF_DATATYPE_BOOLEAN )
1254 msg_Dbg( p_thread, "boolean: %s", amf_decode_boolean( &i ) ? "true" : "false" );
1256 else if( *i == AMF_DATATYPE_STRING )
1259 string = amf_decode_string( &i );
1260 msg_Dbg( p_thread, "string: %s", string );
1263 else if( *i == AMF_DATATYPE_OBJECT )
1266 msg_Dbg( p_thread, "object" );
1267 while( ( string = amf_decode_object( &i ) ) != NULL )
1269 if( *i == AMF_DATATYPE_NUMBER )
1272 msg_Dbg( p_thread, "key: %s value: %le", string, amf_decode_number( &i ) );
1274 else if( *i == AMF_DATATYPE_BOOLEAN )
1277 msg_Dbg( p_thread, "key: %s value: %s", string, amf_decode_boolean( &i ) ? "true" : "false" );
1279 else if( *i == AMF_DATATYPE_STRING )
1282 string2 = amf_decode_string( &i );
1283 msg_Dbg( p_thread, "key: %s value: %s", string, string2 );
1284 if( strcmp( "code", string ) == 0 )
1286 #warning Locking bugs here.
1287 if( strcmp( "NetConnection.Connect.Success", string2 ) == 0 )
1289 p_thread->result_connect = 0;
1291 vlc_mutex_lock( &p_thread->lock );
1292 vlc_cond_signal( &p_thread->wait );
1293 vlc_mutex_unlock( &p_thread->lock );
1295 else if( strcmp( "NetConnection.Connect.InvalidApp", string2 ) == 0 )
1297 vlc_mutex_lock( &p_thread->lock );
1298 vlc_cond_signal( &p_thread->wait );
1299 vlc_mutex_unlock( &p_thread->lock );
1301 else if( strcmp( "NetStream.Play.Start", string2 ) == 0 )
1303 p_thread->result_play = 0;
1305 vlc_mutex_lock( &p_thread->lock );
1306 vlc_cond_signal( &p_thread->wait );
1307 vlc_mutex_unlock( &p_thread->lock );
1309 else if( strcmp( "NetStream.Play.Stop", string2 ) == 0 )
1311 p_thread->result_stop = 1;
1313 block_FifoWake( p_thread->p_fifo_input );
1318 else if( *i == AMF_DATATYPE_NULL )
1321 msg_Dbg( p_thread, "key: %s value: Null", string );
1323 else if( *i == AMF_DATATYPE_UNDEFINED )
1326 msg_Dbg( p_thread, "key: %s value: undefined (Null)", string );
1331 msg_Warn( p_thread, "key: %s value: undefined AMF type", string );
1335 msg_Dbg( p_thread, "end of object" );
1337 else if( *i == AMF_DATATYPE_NULL)
1340 msg_Dbg( p_thread, "null" );
1342 else if( *i == AMF_DATATYPE_UNDEFINED )
1345 msg_Dbg( p_thread, "undefined (null)" );
1350 msg_Warn( p_thread, "undefined AMF type" );
1354 rtmp_packet_free( rtmp_packet );
1359 rtmp_packet_free( rtmp_packet );
1363 /* length header calculated automatically based on last packet in the same channel */
1364 /* timestamps passed are always absolute */
1365 static rtmp_packet_t *
1366 rtmp_new_packet( rtmp_control_thread_t *p_thread, uint8_t stream_index,
1367 uint32_t timestamp, uint8_t content_type,
1368 uint32_t src_dst, rtmp_body_t *body )
1370 int interchunk_headers;
1371 rtmp_packet_t *rtmp_packet;
1372 rtmp_packet_t *rtmp_send = p_thread->rtmp_headers_send+stream_index;
1374 rtmp_packet = (rtmp_packet_t *) malloc( sizeof( rtmp_packet_t ) );
1375 if( !rtmp_packet ) return NULL;
1377 interchunk_headers = body->length_body / p_thread->chunk_size_send;
1378 if( body->length_body % p_thread->chunk_size_send == 0 )
1379 interchunk_headers--;
1381 if( src_dst != rtmp_send->src_dst )
1383 rtmp_send->timestamp = timestamp;
1384 rtmp_send->length_body = body->length_body;
1385 rtmp_send->content_type = content_type;
1386 rtmp_send->src_dst = src_dst;
1388 rtmp_packet->length_header = 12;
1390 else if( content_type != rtmp_send->content_type
1391 || body->length_body != rtmp_send->length_body )
1393 rtmp_send->timestamp_relative =
1394 timestamp - rtmp_send->timestamp;
1395 rtmp_send->timestamp = timestamp;
1396 rtmp_send->length_body = body->length_body;
1397 rtmp_send->content_type = content_type;
1399 rtmp_packet->length_header = 8;
1401 else if( timestamp != rtmp_send->timestamp )
1403 rtmp_send->timestamp_relative =
1404 timestamp - rtmp_send->timestamp;
1405 rtmp_send->timestamp = timestamp;
1407 rtmp_packet->length_header = 4;
1411 rtmp_packet->length_header = 1;
1413 /*TODO: puede que no haga falta guardar el timestamp relative */
1414 rtmp_packet->stream_index = stream_index;
1415 if( rtmp_packet->length_header == 12 )
1417 rtmp_packet->timestamp = timestamp;
1418 rtmp_packet->timestamp_relative = 0;
1422 rtmp_packet->timestamp = timestamp;
1423 rtmp_packet->timestamp_relative = rtmp_send->timestamp_relative;
1426 rtmp_packet->length_encoded = rtmp_packet->length_header
1427 + body->length_body + interchunk_headers;
1428 rtmp_packet->length_body = body->length_body;
1429 rtmp_packet->content_type = content_type;
1430 rtmp_packet->src_dst = src_dst;
1432 rtmp_packet->body = (rtmp_body_t *) malloc( sizeof( rtmp_body_t ) );
1433 if( !rtmp_packet->body )
1435 free( rtmp_packet );
1439 rtmp_packet->body->length_body = body->length_body;
1440 rtmp_packet->body->length_buffer = body->length_body;
1441 rtmp_packet->body->body = (uint8_t *) malloc( rtmp_packet->body->length_buffer * sizeof( uint8_t ) );
1442 if( !rtmp_packet->body->body )
1444 free( rtmp_packet->body );
1445 free( rtmp_packet );
1448 memcpy( rtmp_packet->body->body, body->body, rtmp_packet->body->length_body );
1454 rtmp_new_block( rtmp_control_thread_t *p_thread, uint8_t *buffer, int32_t length_buffer )
1457 /* DOWN: p_thread->p_empty_blocks->i_depth */
1458 while ( block_FifoCount( p_thread->p_empty_blocks ) > MAX_EMPTY_BLOCKS )
1460 p_buffer = block_FifoGet( p_thread->p_empty_blocks );
1461 block_Release( p_buffer );
1463 /* DOWN: p_thread->p_empty_blocks->i_depth */
1464 if( block_FifoCount( p_thread->p_empty_blocks ) == 0 )
1466 p_buffer = block_New( p_thread, length_buffer );
1470 p_buffer = block_FifoGet( p_thread->p_empty_blocks );
1471 p_buffer = block_Realloc( p_buffer, 0, length_buffer );
1474 p_buffer->i_buffer = length_buffer;
1476 memcpy( p_buffer->p_buffer, buffer, p_buffer->i_buffer );
1481 /* Call sequence for each packet:
1482 * rtmp_new_packet -> rtmp_encode_packet -> send .
1483 * No parallelism allowed because of optimization in header length. */
1486 rtmp_encode_packet( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1489 int interchunk_headers;
1490 uint32_t timestamp, length_body, src_dst;
1493 out = (uint8_t *) malloc( rtmp_packet->length_encoded * sizeof( uint8_t ) );
1494 if( !out ) return NULL;
1496 interchunk_headers = rtmp_packet->body->length_body / p_thread->chunk_size_send;
1497 if( rtmp_packet->body->length_body % p_thread->chunk_size_send == 0 )
1498 interchunk_headers--;
1500 if( rtmp_packet->length_header == 12 )
1502 /* Timestamp absolute */
1503 timestamp = hton32( rtmp_packet->timestamp );
1504 memcpy( out, ×tamp, sizeof( uint32_t ) );
1506 src_dst = hton32( rtmp_packet->src_dst );
1507 memcpy( out + 8, &src_dst, sizeof( uint32_t ) );
1510 if( rtmp_packet->length_header >= 8 )
1512 /* Length without inter chunk headers */
1513 length_body = hton32( rtmp_packet->body->length_body );
1514 memcpy( out + 3, &length_body, sizeof( uint32_t ) );
1516 out[7] = rtmp_packet->content_type;
1518 if( rtmp_packet->length_header >= 4 && rtmp_packet->length_header != 12 )
1520 /* Timestamp relative */
1521 timestamp = hton32( rtmp_packet->timestamp_relative );
1522 memcpy( out, ×tamp, sizeof( uint32_t ) );
1525 out[0] = rtmp_encode_header_size( (vlc_object_t *) p_thread, rtmp_packet->length_header ) + rtmp_packet->stream_index;
1527 /* Insert inter chunk headers */
1528 for(i = 0, j = 0; i < rtmp_packet->body->length_body + interchunk_headers; i++, j++)
1530 if( j % p_thread->chunk_size_send == 0 && j != 0 )
1531 out[rtmp_packet->length_header + i++] = RTMP_HEADER_SIZE_1 + rtmp_packet->stream_index;
1532 out[rtmp_packet->length_header + i] = rtmp_packet->body->body[j];
1538 static rtmp_packet_t *
1539 rtmp_encode_onBWDone( rtmp_control_thread_t *p_thread, double number )
1541 rtmp_packet_t *rtmp_packet;
1542 rtmp_body_t *rtmp_body;
1543 uint8_t *tmp_buffer;
1545 /* Build onBWDone */
1546 rtmp_body = rtmp_body_new( -1 );
1548 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onBWDone" );
1549 rtmp_body_append( rtmp_body, tmp_buffer,
1550 AMF_DATATYPE_SIZE_STRING + strlen( "onBWDone" ) );
1553 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1554 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1557 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1558 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1561 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1562 0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_CONNECT_OBJECT, rtmp_body );
1563 free( rtmp_body->body );
1569 static rtmp_packet_t *
1570 rtmp_encode_server_bw( rtmp_control_thread_t *p_thread, uint32_t number )
1572 rtmp_packet_t *rtmp_packet;
1573 rtmp_body_t *rtmp_body;
1575 /* Build server bw */
1576 rtmp_body = rtmp_body_new( -1 );
1578 rtmp_body_append( rtmp_body, (uint8_t *) &number, sizeof( uint32_t ) );
1580 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1581 0, RTMP_CONTENT_TYPE_SERVER_BW, RTMP_SRC_DST_CONNECT_OBJECT, rtmp_body );
1582 free( rtmp_body->body );
1588 static rtmp_packet_t *
1589 rtmp_encode_NetConnection_connect_result( rtmp_control_thread_t *p_thread, double number )
1591 rtmp_packet_t *rtmp_packet;
1592 rtmp_body_t *rtmp_body;
1593 uint8_t *tmp_buffer;
1595 /* Build NetConnection.connect result */
1596 rtmp_body = rtmp_body_new( -1 );
1598 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "_result" );
1599 rtmp_body_append( rtmp_body, tmp_buffer,
1600 AMF_DATATYPE_SIZE_STRING + strlen( "_result" ) );
1603 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1604 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1607 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1608 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1611 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1612 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1615 tmp_buffer = amf_encode_object_variable( "level",
1616 AMF_DATATYPE_STRING, "status" );
1617 rtmp_body_append( rtmp_body, tmp_buffer,
1618 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1619 AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1622 tmp_buffer = amf_encode_object_variable( "code",
1623 AMF_DATATYPE_STRING, "NetConnection.Connect.Success" );
1624 rtmp_body_append( rtmp_body, tmp_buffer,
1625 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1626 AMF_DATATYPE_SIZE_STRING + strlen( "NetConnection.Connect.Success" ) );
1629 tmp_buffer = amf_encode_object_variable( "description",
1630 AMF_DATATYPE_STRING, "Connection succeeded." );
1631 rtmp_body_append( rtmp_body, tmp_buffer,
1632 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1633 AMF_DATATYPE_SIZE_STRING + strlen( "Connection succeeded." ) );
1636 tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1637 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1640 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1641 0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
1642 free( rtmp_body->body );
1648 static rtmp_packet_t *
1649 rtmp_encode_createStream_result( rtmp_control_thread_t *p_thread, double stream_client_id, double stream_server_id )
1651 rtmp_packet_t *rtmp_packet;
1652 rtmp_body_t *rtmp_body;
1653 uint8_t *tmp_buffer;
1655 /* Build createStream result */
1656 rtmp_body = rtmp_body_new( -1 );
1658 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "_result" );
1659 rtmp_body_append( rtmp_body, tmp_buffer,
1660 AMF_DATATYPE_SIZE_STRING + strlen( "_result" ) );
1663 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &stream_client_id );
1664 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1667 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1668 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1671 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &stream_server_id );
1672 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1675 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1676 0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
1677 free( rtmp_body->body );
1683 static rtmp_packet_t *
1684 rtmp_encode_ping_reset_stream( rtmp_control_thread_t *p_thread )
1686 rtmp_packet_t *rtmp_packet;
1687 rtmp_body_t *rtmp_body;
1688 uint8_t *tmp_buffer;
1690 /* Build ping reset stream */
1691 rtmp_body = rtmp_body_new( -1 );
1693 tmp_buffer = rtmp_encode_ping( RTMP_PING_RESET_STREAM, RTMP_SRC_DST_CONNECT_OBJECT2, 0, 0 );
1694 rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_RESET_STREAM );
1697 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1698 0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
1699 free( rtmp_body->body );
1705 static rtmp_packet_t *
1706 rtmp_encode_ping_clear_stream( rtmp_control_thread_t *p_thread, uint32_t src_dst )
1708 rtmp_packet_t *rtmp_packet;
1709 rtmp_body_t *rtmp_body;
1710 uint8_t *tmp_buffer;
1712 /* Build ping clear stream */
1713 rtmp_body = rtmp_body_new( -1 );
1715 tmp_buffer = rtmp_encode_ping( RTMP_PING_CLEAR_STREAM, src_dst, 0, 0 );
1716 rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_CLEAR_STREAM );
1719 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1720 0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
1721 free( rtmp_body->body );
1727 static rtmp_packet_t *
1728 rtmp_encode_NetStream_play_reset_onStatus( rtmp_control_thread_t *p_thread, char *psz_media )
1730 rtmp_packet_t *rtmp_packet;
1731 rtmp_body_t *rtmp_body;
1732 uint8_t *tmp_buffer;
1736 /* Build NetStream.play.reset onStatus */
1737 rtmp_body = rtmp_body_new( -1 );
1739 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onStatus" );
1740 rtmp_body_append( rtmp_body, tmp_buffer,
1741 AMF_DATATYPE_SIZE_STRING + strlen( "onStatus" ) );
1744 number = 1; /* TODO: review this number*/
1745 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1746 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1749 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1750 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1753 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1754 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1757 tmp_buffer = amf_encode_object_variable( "level",
1758 AMF_DATATYPE_STRING, "status" );
1759 rtmp_body_append( rtmp_body, tmp_buffer,
1760 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1761 AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1764 tmp_buffer = amf_encode_object_variable( "code",
1765 AMF_DATATYPE_STRING, "NetStream.Play.Reset" );
1766 rtmp_body_append( rtmp_body, tmp_buffer,
1767 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1768 AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Play.Reset" ) );
1771 if( asprintf( &description, "Playing and resetting %s.", psz_media ) == -1 )
1773 free( rtmp_body->body );
1777 tmp_buffer = amf_encode_object_variable( "description",
1778 AMF_DATATYPE_STRING, description );
1779 rtmp_body_append( rtmp_body, tmp_buffer,
1780 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1781 AMF_DATATYPE_SIZE_STRING + strlen( description ) );
1783 free( description );
1785 tmp_buffer = amf_encode_object_variable( "details",
1786 AMF_DATATYPE_STRING, psz_media );
1787 rtmp_body_append( rtmp_body, tmp_buffer,
1788 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "details" ) +
1789 AMF_DATATYPE_SIZE_STRING + strlen( psz_media ) );
1792 tmp_buffer = amf_encode_object_variable( "clientid",
1793 AMF_DATATYPE_NUMBER, &p_thread->stream_client_id );
1794 rtmp_body_append( rtmp_body, tmp_buffer,
1795 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "clientid" ) +
1796 AMF_DATATYPE_SIZE_NUMBER );
1799 tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1800 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1803 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_NOTIFY,
1804 0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
1805 free( rtmp_body->body );
1811 static rtmp_packet_t *
1812 rtmp_encode_NetStream_play_start_onStatus( rtmp_control_thread_t *p_thread, char *psz_media )
1814 rtmp_packet_t *rtmp_packet;
1815 rtmp_body_t *rtmp_body;
1816 uint8_t *tmp_buffer;
1820 /* Build NetStream.play.start onStatus */
1821 rtmp_body = rtmp_body_new( -1 );
1823 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onStatus" );
1824 rtmp_body_append( rtmp_body, tmp_buffer,
1825 AMF_DATATYPE_SIZE_STRING + strlen( "onStatus" ) );
1828 number = 1; /* TODO: review this number*/
1829 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1830 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1833 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1834 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1837 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1838 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1841 tmp_buffer = amf_encode_object_variable( "level",
1842 AMF_DATATYPE_STRING, "status" );
1843 rtmp_body_append( rtmp_body, tmp_buffer,
1844 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1845 AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1848 tmp_buffer = amf_encode_object_variable( "code",
1849 AMF_DATATYPE_STRING, "NetStream.Play.Start" );
1850 rtmp_body_append( rtmp_body, tmp_buffer,
1851 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1852 AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Play.Start" ) );
1855 if( asprintf( &description, "Started playing %s.", psz_media ) == -1 )
1857 free( rtmp_body->body );
1862 tmp_buffer = amf_encode_object_variable( "description",
1863 AMF_DATATYPE_STRING, description );
1864 rtmp_body_append( rtmp_body, tmp_buffer,
1865 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1866 AMF_DATATYPE_SIZE_STRING + strlen( description ) );
1868 free( description );
1870 tmp_buffer = amf_encode_object_variable( "details",
1871 AMF_DATATYPE_STRING, psz_media );
1872 rtmp_body_append( rtmp_body, tmp_buffer,
1873 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "details" ) +
1874 AMF_DATATYPE_SIZE_STRING + strlen( psz_media ) );
1877 tmp_buffer = amf_encode_object_variable( "clientid",
1878 AMF_DATATYPE_NUMBER, &p_thread->stream_client_id );
1879 rtmp_body_append( rtmp_body, tmp_buffer,
1880 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "clientid" ) +
1881 AMF_DATATYPE_SIZE_NUMBER );
1884 tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1885 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1888 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_NOTIFY,
1889 0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
1890 free( rtmp_body->body );
1897 rtmp_encode_header_size( vlc_object_t *p_this, uint8_t header_size )
1899 if( header_size == 1 )
1900 return RTMP_HEADER_SIZE_1;
1901 else if( header_size == 4 )
1902 return RTMP_HEADER_SIZE_4;
1903 else if( header_size == 8 )
1904 return RTMP_HEADER_SIZE_8;
1905 else if( header_size == 12 )
1906 return RTMP_HEADER_SIZE_12;
1909 msg_Err( p_this, "invalid header size for encoding" );
1915 rtmp_decode_header_size( vlc_object_t *p_this, uint8_t header_size )
1917 if( header_size == RTMP_HEADER_SIZE_1 )
1919 else if( header_size == RTMP_HEADER_SIZE_4 )
1921 else if( header_size == RTMP_HEADER_SIZE_8 )
1923 else if( header_size == RTMP_HEADER_SIZE_12 )
1927 msg_Err( p_this, "invalid RTMP_HEADER_SIZE_XX " );
1933 rtmp_get_stream_index( uint8_t content_type )
1935 if( content_type == RTMP_CONTENT_TYPE_AUDIO_DATA )
1936 return RTMP_DEFAULT_STREAM_INDEX_AUDIO_DATA;
1937 else if( content_type == RTMP_CONTENT_TYPE_VIDEO_DATA )
1938 return RTMP_DEFAULT_STREAM_INDEX_VIDEO_DATA;
1939 else if( content_type == RTMP_CONTENT_TYPE_NOTIFY )
1940 return RTMP_DEFAULT_STREAM_INDEX_NOTIFY;
1945 /*****************************************************************************
1946 * Body handling implementation:
1947 ******************************************************************************/
1949 rtmp_body_new( int length_buffer )
1951 rtmp_body_t *rtmp_body;
1953 rtmp_body = (rtmp_body_t *) malloc( sizeof( rtmp_body_t ) );
1954 if( !rtmp_body ) return NULL;
1956 rtmp_body->length_body = 0;
1957 if( length_buffer < 0 )
1958 rtmp_body->length_buffer = RTMP_BODY_SIZE_ALLOC;
1960 rtmp_body->length_buffer = length_buffer;
1961 rtmp_body->body = (uint8_t *) malloc( rtmp_body->length_buffer * sizeof( uint8_t ) );
1962 if( !rtmp_body->body )
1971 rtmp_body_reset( rtmp_body_t *rtmp_body )
1973 rtmp_body->length_body = 0;
1977 rtmp_body_append( rtmp_body_t *rtmp_body, uint8_t *buffer, uint32_t length )
1979 if( (rtmp_body->length_body + length) > rtmp_body->length_buffer )
1982 rtmp_body->length_buffer = rtmp_body->length_body + length;
1983 tmp = realloc( rtmp_body->body,
1984 rtmp_body->length_buffer * sizeof( uint8_t ) );
1986 rtmp_body->body = tmp;
1989 memcpy( rtmp_body->body + rtmp_body->length_body, buffer, length );
1990 rtmp_body->length_body += length;
1993 /*****************************************************************************
1994 * RTMP ping implementation:
1995 ******************************************************************************/
1997 rtmp_encode_ping( uint16_t type, uint32_t src_dst, uint32_t third_arg, uint32_t fourth_arg )
1999 uint8_t *out = NULL;
2000 VLC_UNUSED(fourth_arg);
2002 if( type == RTMP_PING_CLEAR_STREAM )
2003 out = (uint8_t *) malloc( RTMP_PING_SIZE_CLEAR_STREAM * sizeof( uint8_t ) );
2004 else if( type == RTMP_PING_CLEAR_PLAYING_BUFFER )
2005 out = (uint8_t *) malloc( RTMP_PING_SIZE_CLEAR_PLAYING_BUFFER * sizeof( uint8_t ) );
2006 else if( type == RTMP_PING_BUFFER_TIME_CLIENT )
2008 out = (uint8_t *) malloc( RTMP_PING_SIZE_BUFFER_TIME_CLIENT * sizeof( uint8_t ) );
2009 if( !out ) goto error;
2010 third_arg = hton32( third_arg );
2011 memcpy( out + 6, &third_arg, sizeof( uint32_t ) );
2013 else if( type == RTMP_PING_RESET_STREAM )
2015 out = (uint8_t *) malloc( RTMP_PING_SIZE_RESET_STREAM * sizeof( uint8_t ) );
2017 /* else if( type == RTMP_PING_CLIENT_FROM_SERVER ) TODO: research this
2020 else if( type == RTMP_PING_PONG_FROM_CLIENT )
2025 out = (uint8_t *) malloc( RTMP_PING_SIZE_BUFFER_TIME_CLIENT * sizeof( uint8_t ) );
2026 if( !out ) goto error;
2027 out[6] = 0x0D; out[7] = 0x0E; out[8] = 0x0A; out[9] = 0x0D;
2030 if( !out ) goto error;
2032 type = hton16( type );
2033 memcpy( out, &type, sizeof( uint16_t ) );
2035 src_dst = hton32( src_dst );
2036 memcpy( out + 2, &src_dst, sizeof( uint32_t ) );
2044 /*****************************************************************************
2045 * AMF implementation:
2046 ******************************************************************************/
2048 amf_encode_element( uint8_t element, const void *value )
2052 if ( element == AMF_DATATYPE_NUMBER )
2054 uint64_t number = *(uint64_t *) value;
2056 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2057 if( !out ) return NULL;
2059 number = hton64( number );
2060 out[0] = AMF_DATATYPE_NUMBER;
2061 memcpy( out + 1, &number, sizeof( uint64_t ) );
2062 } else if ( element == AMF_DATATYPE_BOOLEAN )
2064 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_BOOLEAN * sizeof( uint8_t ) );
2065 if( !out ) return NULL;
2067 out[0] = AMF_DATATYPE_BOOLEAN;
2068 out[1] = *(uint8_t *) value;
2069 } else if ( element == AMF_DATATYPE_STRING )
2071 uint16_t length_psz, length_psz_cpy;
2073 length_psz = length_psz_cpy = strlen( (char *) value );
2074 out = (uint8_t *) malloc( ( AMF_DATATYPE_SIZE_STRING + length_psz ) * sizeof( uint8_t ) );
2075 if( !out ) return NULL;
2077 out[0] = AMF_DATATYPE_STRING;
2078 length_psz = hton16( length_psz );
2079 memcpy( out + 1, &length_psz, sizeof( uint16_t ) );
2080 memcpy( out + 3, value, length_psz_cpy );
2081 } else if ( element == AMF_DATATYPE_OBJECT )
2083 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_OBJECT * sizeof( uint8_t ) );
2084 if( !out ) return NULL;
2086 out[0] = AMF_DATATYPE_OBJECT;
2087 } else if ( element == AMF_DATATYPE_NULL )
2089 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NULL * sizeof( uint8_t ) );
2090 if( !out ) return NULL;
2092 out[0] = AMF_DATATYPE_NULL;
2093 } else if ( element == AMF_DATATYPE_MIXED_ARRAY )
2095 uint32_t highest_index = *(uint32_t *) value;
2097 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_MIXED_ARRAY * sizeof( uint8_t ) );
2098 if( !out ) return NULL;
2100 highest_index = hton32( highest_index );
2101 out[0] = AMF_DATATYPE_MIXED_ARRAY;
2102 memcpy( out + 1, &highest_index, sizeof( uint32_t ) );
2103 } else if ( element == AMF_DATATYPE_END_OF_OBJECT )
2105 out = (uint8_t *) calloc( AMF_DATATYPE_SIZE_END_OF_OBJECT, sizeof( uint8_t ) );
2107 out[AMF_DATATYPE_SIZE_END_OF_OBJECT - 1] = AMF_DATATYPE_END_OF_OBJECT;
2110 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2111 if( !out ) return NULL;
2113 out[0] = AMF_DATATYPE_NUMBER;
2114 out[1] = 0x0D; out[2] = 0x0E; out[3] = 0x0A; out[4] = 0x0D;
2115 out[5] = 0x0B; out[6] = 0x0E; out[7] = 0x0E; out[8] = 0x0F;
2122 amf_encode_object_variable( const char *key, uint8_t element, const void *value )
2124 uint8_t *out, *out_value;
2126 uint16_t length_psz, length_psz_cpy;
2128 length_psz = length_psz_cpy = strlen( key );
2130 if( element == AMF_DATATYPE_NUMBER )
2131 length_value = AMF_DATATYPE_SIZE_NUMBER;
2132 else if( element == AMF_DATATYPE_BOOLEAN )
2133 length_value = AMF_DATATYPE_SIZE_BOOLEAN;
2134 else if( element == AMF_DATATYPE_STRING )
2135 length_value = AMF_DATATYPE_SIZE_STRING + strlen( (char *) value );
2136 else if( element == AMF_DATATYPE_NULL )
2137 length_value = AMF_DATATYPE_SIZE_NULL;
2140 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2141 if( !out ) return NULL;
2143 out[0] = AMF_DATATYPE_NUMBER;
2144 out[1] = 0xD; out[2] = 0xE; out[3] = 0xA; out[4] = 0xD;
2145 out[5] = 0xB; out[6] = 0xE; out[7] = 0xE; out[8] = 0xF;
2150 out = (uint8_t *) malloc( ( AMF_DATATYPE_SIZE_OBJECT_VARIABLE + length_psz + length_value ) * sizeof( uint8_t ) );
2151 if( !out ) return NULL;
2153 length_psz = hton16( length_psz );
2154 memcpy( out, &length_psz, sizeof( uint16_t ) );
2155 memcpy( out + 2, key, length_psz_cpy );
2157 out_value = amf_encode_element( element, value );
2158 memcpy( out + 2 + length_psz_cpy, out_value, length_value );
2165 amf_decode_number( uint8_t **buffer )
2170 number = ntoh64( *(uint64_t *) *buffer );
2171 memcpy(&out, &number, sizeof( uint64_t ) );
2172 *buffer += sizeof( uint64_t );
2178 amf_decode_boolean( uint8_t **buffer )
2188 /* return value allocated dinamically */
2190 amf_decode_string( uint8_t **buffer )
2196 length = ntoh16( *(uint16_t *) *buffer );
2197 *buffer += sizeof( uint16_t );
2199 #error This size is wrong and breaks just about everything.
2200 if( length > sizeof( *buffer ) / sizeof( uint8_t ))
2203 out = (char *) malloc( length + 1 ); /* '\0' terminated */
2204 if( !out ) return NULL;
2206 for(i = 0; i < length; i++)
2207 out[i] = (*buffer)[i];
2216 /* returns in each call next key, at end of object returns NULL */
2217 /* need to decode value of key after call */
2219 amf_decode_object( uint8_t **buffer )
2221 if( **buffer == 0x0 && *(*buffer + 1) == 0x00 && *(*buffer + 2) == 0x09)
2228 return amf_decode_string( buffer );
2231 /*****************************************************************************
2232 * FLV rebuilding implementation:
2233 ******************************************************************************/
2235 flv_rebuild( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
2237 uint32_t length_tag, timestamp;
2240 tmp = (uint8_t *) realloc( rtmp_packet->body->body,
2241 rtmp_packet->body->length_body +
2242 FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE );
2244 rtmp_packet->body->body = tmp;
2245 memmove( rtmp_packet->body->body + FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE,
2246 rtmp_packet->body->body, rtmp_packet->body->length_body );
2249 p_thread->flv_tag_previous_tag_size = hton32( p_thread->flv_tag_previous_tag_size );
2250 memcpy( rtmp_packet->body->body, &p_thread->flv_tag_previous_tag_size, sizeof( uint32_t ) );
2252 /* Fill backwards because of overlapping*/
2253 rtmp_packet->body->body[11] = 0x00;
2255 timestamp = hton32( rtmp_packet->timestamp );
2256 memcpy( rtmp_packet->body->body + 7, ×tamp, sizeof( uint32_t ) );
2258 length_tag = hton32( rtmp_packet->body->length_body );
2259 memcpy( rtmp_packet->body->body + 4, &length_tag, sizeof( uint32_t ) );
2261 rtmp_packet->body->body[4] = rtmp_packet->content_type;
2263 rtmp_packet->body->body[12] = 0x00;
2264 rtmp_packet->body->body[13] = 0x00;
2265 rtmp_packet->body->body[14] = 0x00;
2267 p_thread->flv_tag_previous_tag_size = rtmp_packet->body->length_body + FLV_TAG_SIZE;
2270 rtmp_packet->body->length_body += FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE;
2271 rtmp_packet->body->length_buffer = rtmp_packet->body->length_body;
2275 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 )
2279 data_audio = *packet_audio->body->body;
2281 if( ( data_audio & FLV_AUDIO_STEREO_MASK ) == FLV_AUDIO_STEREO_MONO )
2282 *stereo = FLV_AUDIO_STEREO_MONO;
2283 else if( ( data_audio & FLV_AUDIO_STEREO_MASK ) == FLV_AUDIO_STEREO_STEREO )
2284 *stereo = FLV_AUDIO_STEREO_STEREO;
2286 msg_Warn( p_thread, "unknown metadata audio stereo" );
2288 if( ( data_audio & FLV_AUDIO_SIZE_MASK ) == FLV_AUDIO_SIZE_8_BIT )
2289 *audiosamplesize = FLV_AUDIO_SIZE_8_BIT >> 1;
2290 else if( ( data_audio & FLV_AUDIO_SIZE_MASK ) == FLV_AUDIO_SIZE_16_BIT )
2291 *audiosamplesize = FLV_AUDIO_SIZE_16_BIT >> 1;
2293 msg_Warn( p_thread, "unknown metadata audio sample size" );
2295 if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_5_5_KHZ )
2296 *audiosamplerate = 5512;
2297 else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_11_KHZ )
2298 *audiosamplerate = 11025;
2299 else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_22_KHZ )
2300 *audiosamplerate = 22050;
2301 else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_44_KHZ )
2302 *audiosamplerate = 44100;
2304 msg_Warn( p_thread, "unknown metadata audio sample rate" );
2306 if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_UNCOMPRESSED )
2307 *audiocodecid = FLV_AUDIO_CODEC_ID_UNCOMPRESSED >> 4;
2308 else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_ADPCM )
2309 *audiocodecid = FLV_AUDIO_CODEC_ID_ADPCM >> 4;
2310 else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_MP3 )
2311 *audiocodecid = FLV_AUDIO_CODEC_ID_MP3 >> 4;
2312 else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_NELLYMOSER_8KHZ_MONO )
2313 *audiocodecid = FLV_AUDIO_CODEC_ID_NELLYMOSER_8KHZ_MONO >> 4;
2314 else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_NELLYMOSER )
2315 *audiocodecid = FLV_AUDIO_CODEC_ID_NELLYMOSER >> 4;
2317 msg_Warn( p_thread, "unknown metadata audio codec id" );
2321 flv_get_metadata_video( rtmp_control_thread_t *p_thread, rtmp_packet_t *packet_video, uint8_t *videocodecid, uint8_t *frametype )
2325 data_video = *packet_video->body->body;
2327 if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SORENSEN_H263 )
2328 *videocodecid = FLV_VIDEO_CODEC_ID_SORENSEN_H263;
2329 else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SCREEN_VIDEO )
2330 *videocodecid = FLV_VIDEO_CODEC_ID_SCREEN_VIDEO;
2331 else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_ON2_VP6 )
2332 *videocodecid = FLV_VIDEO_CODEC_ID_ON2_VP6;
2333 else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_ON2_VP6_ALPHA )
2334 *videocodecid = FLV_VIDEO_CODEC_ID_ON2_VP6_ALPHA;
2335 else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SCREEN_VIDEO_2 )
2336 *videocodecid = FLV_VIDEO_CODEC_ID_SCREEN_VIDEO_2;
2338 msg_Warn( p_thread, "unknown metadata video codec id" );
2340 if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_KEYFRAME )
2341 *frametype = FLV_VIDEO_FRAME_TYPE_KEYFRAME >> 4;
2342 else if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_INTER_FRAME )
2343 *frametype = FLV_VIDEO_FRAME_TYPE_INTER_FRAME >> 4;
2344 else if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_DISPOSABLE_INTER_FRAME )
2345 *frametype = FLV_VIDEO_FRAME_TYPE_DISPOSABLE_INTER_FRAME >> 4;
2347 msg_Warn( p_thread, "unknown metadata video frame type" );
2350 static rtmp_packet_t *
2351 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 )
2353 rtmp_packet_t *rtmp_packet;
2354 rtmp_body_t *rtmp_body;
2355 uint8_t *tmp_buffer;
2358 rtmp_body = rtmp_body_new( -1 );
2360 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onMetaData" );
2361 rtmp_body_append( rtmp_body, tmp_buffer,
2362 AMF_DATATYPE_SIZE_STRING + strlen( "onMetaData" ) );
2366 tmp_buffer = amf_encode_element( AMF_DATATYPE_MIXED_ARRAY, &number );
2367 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_MIXED_ARRAY );
2371 tmp_buffer = amf_encode_object_variable( "duration",
2372 AMF_DATATYPE_NUMBER, &number );
2373 rtmp_body_append( rtmp_body, tmp_buffer,
2374 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "duration" ) +
2375 AMF_DATATYPE_SIZE_NUMBER );
2378 tmp_buffer = amf_encode_object_variable( "stereo",
2379 AMF_DATATYPE_BOOLEAN, &stereo );
2380 rtmp_body_append( rtmp_body, tmp_buffer,
2381 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "stereo" ) +
2382 AMF_DATATYPE_SIZE_BOOLEAN );
2385 number = audiosamplesize;
2386 tmp_buffer = amf_encode_object_variable( "audiosamplesize",
2387 AMF_DATATYPE_NUMBER, &number );
2388 rtmp_body_append( rtmp_body, tmp_buffer,
2389 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiosamplesize" ) +
2390 AMF_DATATYPE_SIZE_NUMBER );
2393 number = audiosamplerate;
2394 tmp_buffer = amf_encode_object_variable( "audiosamplerate",
2395 AMF_DATATYPE_NUMBER, &number );
2396 rtmp_body_append( rtmp_body, tmp_buffer,
2397 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiosamplerate" ) +
2398 AMF_DATATYPE_SIZE_NUMBER );
2401 number = audiocodecid;
2402 tmp_buffer = amf_encode_object_variable( "audiocodecid",
2403 AMF_DATATYPE_NUMBER, &number );
2404 rtmp_body_append( rtmp_body, tmp_buffer,
2405 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiocodecid" ) +
2406 AMF_DATATYPE_SIZE_NUMBER );
2409 number = videocodecid;
2410 tmp_buffer = amf_encode_object_variable( "videocodecid",
2411 AMF_DATATYPE_NUMBER, &number );
2412 rtmp_body_append( rtmp_body, tmp_buffer,
2413 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "videocodecid" ) +
2414 AMF_DATATYPE_SIZE_NUMBER );
2417 tmp_buffer = amf_encode_element( AMF_DATATYPE_END_OF_OBJECT, NULL );
2418 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
2421 rtmp_packet = rtmp_new_packet( p_access->p_sys->p_thread,
2422 RTMP_DEFAULT_STREAM_INDEX_INVOKE,
2423 0, RTMP_CONTENT_TYPE_NOTIFY, 0, rtmp_body );
2424 free( rtmp_body->body );
2431 flv_get_metadata( access_t *p_access )
2433 rtmp_control_thread_t *p_thread=p_access->p_sys->p_thread;
2436 rtmp_packet_t *p_md = flv_build_onMetaData( p_access, 0,
2437 p_thread->metadata_stereo,
2438 p_thread->metadata_samplesize,
2439 p_thread->metadata_samplerate,
2440 p_thread->metadata_audiocodecid,
2441 p_thread->metadata_videocodecid );
2443 flv_rebuild( p_thread, p_md );
2444 p_buf = rtmp_new_block( p_thread,
2445 p_md->body->body, p_md->body->length_buffer );
2447 rtmp_packet_free( p_md );
2452 flv_insert_header( access_t *p_access, block_t *first_packet )
2454 access_sys_t *p_sys = p_access->p_sys;
2455 int old_buffer_size;
2456 uint32_t tmp_number;
2458 old_buffer_size = first_packet->i_buffer;
2460 first_packet = block_Realloc( first_packet, 0, first_packet->i_buffer + FLV_HEADER_SIZE );
2462 memmove( first_packet->p_buffer + FLV_HEADER_SIZE,
2463 first_packet->p_buffer, old_buffer_size );
2465 memcpy( first_packet->p_buffer, FLV_HEADER_SIGNATURE, sizeof( FLV_HEADER_SIGNATURE ) );
2466 first_packet->p_buffer[3] = FLV_HEADER_VERSION;
2467 if( p_sys->p_thread->has_audio && p_sys->p_thread->has_video )
2468 first_packet->p_buffer[4] = FLV_HEADER_AUDIO | FLV_HEADER_VIDEO;
2469 else if( p_sys->p_thread->has_audio )
2470 first_packet->p_buffer[4] = FLV_HEADER_AUDIO;
2472 first_packet->p_buffer[4] = FLV_HEADER_VIDEO;
2473 tmp_number = hton32( FLV_HEADER_SIZE );
2474 memcpy( first_packet->p_buffer + 5, &tmp_number, sizeof( uint32_t ) );
2476 return first_packet;
2480 rtmp_init_handler( rtmp_handler_t *rtmp_handler )
2482 rtmp_handler[RTMP_CONTENT_TYPE_CHUNK_SIZE] = rtmp_handler_chunk_size;
2483 rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_02] = rtmp_handler_null;
2484 rtmp_handler[RTMP_CONTENT_TYPE_BYTES_READ] = rtmp_handler_null;
2485 rtmp_handler[RTMP_CONTENT_TYPE_PING] = rtmp_handler_null;
2486 rtmp_handler[RTMP_CONTENT_TYPE_SERVER_BW] = rtmp_handler_null;
2487 rtmp_handler[RTMP_CONTENT_TYPE_CLIENT_BW] = rtmp_handler_null;
2488 rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_07] = rtmp_handler_null;
2489 rtmp_handler[RTMP_CONTENT_TYPE_AUDIO_DATA] = rtmp_handler_audio_data;
2490 rtmp_handler[RTMP_CONTENT_TYPE_VIDEO_DATA] = rtmp_handler_video_data;
2491 rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_0A_0E] = rtmp_handler_null;
2492 rtmp_handler[RTMP_CONTENT_TYPE_FLEX_STREAM] = rtmp_handler_null;
2493 rtmp_handler[RTMP_CONTENT_TYPE_FLEX_SHARED_OBJECT] = rtmp_handler_null;
2494 rtmp_handler[RTMP_CONTENT_TYPE_MESSAGE] = rtmp_handler_null;
2495 rtmp_handler[RTMP_CONTENT_TYPE_NOTIFY] = rtmp_handler_notify;
2496 rtmp_handler[RTMP_CONTENT_TYPE_SHARED_OBJECT] = rtmp_handler_null;
2497 rtmp_handler[RTMP_CONTENT_TYPE_INVOKE] = rtmp_handler_invoke;