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
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
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>
41 #include "rtmp_amf_flv.h"
43 /* header length (including itself) */
44 const uint8_t RTMP_HEADER_SIZE_MASK = 0xC0;
45 const uint8_t RTMP_HEADER_SIZE_12 = 0x00;
46 const uint8_t RTMP_HEADER_SIZE_8 = 0x40;
47 const uint8_t RTMP_HEADER_SIZE_4 = 0x80;
48 const uint8_t RTMP_HEADER_SIZE_1 = 0xC0;
51 const uint8_t RTMP_HEADER_STREAM_MAX = 64;
52 const uint8_t RTMP_HEADER_STREAM_INDEX_MASK = 0x3F;
55 const uint8_t RTMP_HANDSHAKE = 0x03;
56 const uint16_t RTMP_HANDSHAKE_BODY_SIZE = 1536;
59 const uint8_t RTMP_CONTENT_TYPE_CHUNK_SIZE = 0x01;
60 const uint8_t RTMP_CONTENT_TYPE_UNKNOWN_02 = 0x02;
61 const uint8_t RTMP_CONTENT_TYPE_BYTES_READ = 0x03;
62 const uint8_t RTMP_CONTENT_TYPE_PING = 0x04;
63 const uint8_t RTMP_CONTENT_TYPE_SERVER_BW = 0x05;
64 const uint8_t RTMP_CONTENT_TYPE_CLIENT_BW = 0x06;
65 const uint8_t RTMP_CONTENT_TYPE_UNKNOWN_07 = 0x07;
66 const uint8_t RTMP_CONTENT_TYPE_AUDIO_DATA = 0x08;
67 const uint8_t RTMP_CONTENT_TYPE_VIDEO_DATA = 0x09;
68 const uint8_t RTMP_CONTENT_TYPE_UNKNOWN_0A_0E = 0x0A;
69 const uint8_t RTMP_CONTENT_TYPE_FLEX_STREAM = 0x0F;
70 const uint8_t RTMP_CONTENT_TYPE_FLEX_SHARED_OBJECT = 0x10;
71 const uint8_t RTMP_CONTENT_TYPE_MESSAGE = 0x11;
72 const uint8_t RTMP_CONTENT_TYPE_NOTIFY = 0x12;
73 const uint8_t RTMP_CONTENT_TYPE_SHARED_OBJECT = 0x13;
74 const uint8_t RTMP_CONTENT_TYPE_INVOKE = 0x14;
76 /* shared object datatypes */
77 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_CONNECT = 0x01;
78 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_DISCONNECT = 0x02;
79 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_SET_ATTRIBUTE = 0x03;
80 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_UPDATE_DATA = 0x04;
81 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_UPDATE_ATTRIBUTE = 0x05;
82 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_SEND_MESSAGE = 0x06;
83 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_STATUS = 0x07;
84 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_CLEAR_DATA = 0x08;
85 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_DELETE_DATA = 0x09;
86 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_DELETE_ATTRIBUTE = 0x0A;
87 const uint8_t RTMP_SHARED_OBJECT_DATATYPE_INITIAL_DATA = 0x0B;
90 const uint16_t RTMP_PING_CLEAR_STREAM = 0x0000;
91 const uint16_t RTMP_PING_CLEAR_PLAYING_BUFFER = 0x0001;
92 const uint16_t RTMP_PING_BUFFER_TIME_CLIENT = 0x0003;
93 const uint16_t RTMP_PING_RESET_STREAM = 0x0004;
94 const uint16_t RTMP_PING_CLIENT_FROM_SERVER = 0x0006;
95 const uint16_t RTMP_PING_PONG_FROM_CLIENT = 0x0007;
98 const uint8_t RTMP_PING_SIZE_CLEAR_STREAM = 6;
99 const uint8_t RTMP_PING_SIZE_CLEAR_PLAYING_BUFFER = 6;
100 const uint8_t RTMP_PING_SIZE_BUFFER_TIME_CLIENT = 10;
101 const uint8_t RTMP_PING_SIZE_RESET_STREAM = 6;
102 /*const uint8_t RTMP_PING_SIZE_CLIENT_FROM_SERVER = 0x0006; TODO
103 const uint8_t RTMP_PING_SIZE_PONG_FROM_CLIENT = 0x0007;
107 const uint8_t RTMP_DEFAULT_STREAM_INDEX_CONTROL = 0x02;
108 const uint8_t RTMP_DEFAULT_STREAM_INDEX_INVOKE = 0x03;
109 const uint8_t RTMP_DEFAULT_STREAM_INDEX_NOTIFY = 0x04;
110 const uint8_t RTMP_DEFAULT_STREAM_INDEX_VIDEO_DATA = 0x05;
111 const uint8_t RTMP_DEFAULT_STREAM_INDEX_AUDIO_DATA = 0x06;
112 const uint32_t RTMP_DEFAULT_CHUNK_SIZE = 128;
113 const double RTMP_DEFAULT_STREAM_CLIENT_ID = 1.0;
114 const double RTMP_DEFAULT_STREAM_SERVER_ID = 1.0;
117 const uint16_t MAX_EMPTY_BLOCKS = 200; /* empty blocks in fifo for media*/
118 const uint16_t RTMP_BODY_SIZE_ALLOC = 1024;
119 const uint32_t RTMP_TIME_CLIENT_BUFFER = 2000; /* miliseconds */
120 const uint32_t RTMP_SERVER_BW = 0x00000200;
121 const uint32_t RTMP_SRC_DST_CONNECT_OBJECT = 0x00000000;
122 const uint32_t RTMP_SRC_DST_CONNECT_OBJECT2 = 0x00000001;
123 const uint32_t RTMP_SRC_DST_DEFAULT = 0x01000000;
124 const uint64_t RTMP_AUDIOCODECS = 0x4083380000000000;
125 const uint64_t RTMP_VIDEOCODECS = 0x405f000000000000;
126 const uint64_t RTMP_VIDEOFUNCTION = 0x3ff0000000000000;
127 /*****************************************************************************
129 ******************************************************************************/
131 /* boolean constants */
132 const uint8_t AMF_BOOLEAN_FALSE = 0x00;
133 const uint8_t AMF_BOOLEAN_TRUE = 0x01;
136 const uint8_t AMF_DATATYPE_NUMBER = 0x00;
137 const uint8_t AMF_DATATYPE_BOOLEAN = 0x01;
138 const uint8_t AMF_DATATYPE_STRING = 0x02;
139 const uint8_t AMF_DATATYPE_OBJECT = 0x03;
140 const uint8_t AMF_DATATYPE_MOVIE_CLIP = 0x04;
141 const uint8_t AMF_DATATYPE_NULL = 0x05;
142 const uint8_t AMF_DATATYPE_UNDEFINED = 0x06;
143 const uint8_t AMF_DATATYPE_REFERENCE = 0x07;
144 const uint8_t AMF_DATATYPE_MIXED_ARRAY = 0x08;
145 const uint8_t AMF_DATATYPE_END_OF_OBJECT = 0x09;
146 const uint8_t AMF_DATATYPE_ARRAY = 0x0A;
147 const uint8_t AMF_DATATYPE_DATE = 0x0B;
148 const uint8_t AMF_DATATYPE_LONG_STRING = 0x0C;
149 const uint8_t AMF_DATATYPE_UNSUPPORTED = 0x0D;
150 const uint8_t AMF_DATATYPE_RECORDSET = 0x0E;
151 const uint8_t AMF_DATATYPE_XML = 0x0F;
152 const uint8_t AMF_DATATYPE_TYPED_OBJECT = 0x10;
153 const uint8_t AMF_DATATYPE_AMF3_DATA = 0x11;
155 /* datatypes sizes */
156 const uint8_t AMF_DATATYPE_SIZE_NUMBER = 9;
157 const uint8_t AMF_DATATYPE_SIZE_BOOLEAN = 2;
158 const uint8_t AMF_DATATYPE_SIZE_STRING = 3;
159 const uint8_t AMF_DATATYPE_SIZE_OBJECT = 1;
160 const uint8_t AMF_DATATYPE_SIZE_NULL = 1;
161 const uint8_t AMF_DATATYPE_SIZE_OBJECT_VARIABLE = 2;
162 const uint8_t AMF_DATATYPE_SIZE_MIXED_ARRAY = 5;
163 const uint8_t AMF_DATATYPE_SIZE_END_OF_OBJECT = 3;
165 /* amf remote calls */
166 const uint64_t AMF_CALL_NETCONNECTION_CONNECT = 0x3FF0000000000000;
167 const uint64_t AMF_CALL_NETCONNECTION_CONNECT_AUDIOCODECS = 0x4083380000000000;
168 const uint64_t AMF_CALL_NETCONNECTION_CONNECT_VIDEOCODECS = 0x405F000000000000;
169 const uint64_t AMF_CALL_NETCONNECTION_CONNECT_VIDEOFUNCTION = 0x3FF0000000000000;
170 const uint64_t AMF_CALL_NETCONNECTION_CONNECT_OBJECTENCODING = 0x0;
171 const double AMF_CALL_STREAM_CLIENT_NUMBER = 3.0;
172 const double AMF_CALL_ONBWDONE = 2.0;
173 const uint64_t AMF_CALL_NETSTREAM_PLAY = 0x0;
175 /*****************************************************************************
177 ******************************************************************************/
178 const uint8_t FLV_HEADER_SIGNATURE[3] = { 0x46, 0x4C, 0x56 }; /* always "FLV" */
179 const uint8_t FLV_HEADER_VERSION = 0x01;
180 const uint8_t FLV_HEADER_AUDIO = 0x04;
181 const uint8_t FLV_HEADER_VIDEO = 0x01;
182 const uint32_t FLV_HEADER_SIZE = 0x00000009; /* always 9 for known FLV files */
184 const uint32_t FLV_TAG_FIRST_PREVIOUS_TAG_SIZE = 0x00000000;
185 const uint8_t FLV_TAG_PREVIOUS_TAG_SIZE = 4;
186 const uint8_t FLV_TAG_SIZE = 11;
188 /* audio stereo types */
189 const uint8_t FLV_AUDIO_STEREO_MASK = 0x01;
190 const uint8_t FLV_AUDIO_STEREO_MONO = 0x00;
191 const uint8_t FLV_AUDIO_STEREO_STEREO = 0x01;
194 const uint8_t FLV_AUDIO_SIZE_MASK = 0x02;
195 const uint8_t FLV_AUDIO_SIZE_8_BIT = 0x00;
196 const uint8_t FLV_AUDIO_SIZE_16_BIT = 0x02;
199 const uint8_t FLV_AUDIO_RATE_MASK = 0x0C;
200 const uint8_t FLV_AUDIO_RATE_5_5_KHZ = 0x00;
201 const uint8_t FLV_AUDIO_RATE_11_KHZ = 0x04;
202 const uint8_t FLV_AUDIO_RATE_22_KHZ = 0x08;
203 const uint8_t FLV_AUDIO_RATE_44_KHZ = 0x0C;
205 /* audio codec types */
206 const uint8_t FLV_AUDIO_CODEC_ID_MASK = 0xF0;
207 const uint8_t FLV_AUDIO_CODEC_ID_UNCOMPRESSED = 0x00;
208 const uint8_t FLV_AUDIO_CODEC_ID_ADPCM = 0x10;
209 const uint8_t FLV_AUDIO_CODEC_ID_MP3 = 0x20;
210 const uint8_t FLV_AUDIO_CODEC_ID_NELLYMOSER_8KHZ_MONO = 0x50;
211 const uint8_t FLV_AUDIO_CODEC_ID_NELLYMOSER = 0x60;
213 /* video codec types */
214 const uint8_t FLV_VIDEO_CODEC_ID_MASK = 0x0F;
215 const uint8_t FLV_VIDEO_CODEC_ID_SORENSEN_H263 = 0x02;
216 const uint8_t FLV_VIDEO_CODEC_ID_SCREEN_VIDEO = 0x03;
217 const uint8_t FLV_VIDEO_CODEC_ID_ON2_VP6 = 0x04;
218 const uint8_t FLV_VIDEO_CODEC_ID_ON2_VP6_ALPHA = 0x05;
219 const uint8_t FLV_VIDEO_CODEC_ID_SCREEN_VIDEO_2 = 0x06;
221 /* video frame types */
222 const uint8_t FLV_VIDEO_FRAME_TYPE_MASK = 0xF0;
223 const uint8_t FLV_VIDEO_FRAME_TYPE_KEYFRAME = 0x10;
224 const uint8_t FLV_VIDEO_FRAME_TYPE_INTER_FRAME = 0x20;
225 const uint8_t FLV_VIDEO_FRAME_TYPE_DISPOSABLE_INTER_FRAME = 0x30;
227 /*****************************************************************************
228 * static RTMP functions:
229 ******************************************************************************/
230 static void rtmp_handler_null ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
231 static void rtmp_handler_chunk_size ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
232 static void rtmp_handler_invoke ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
233 static void rtmp_handler_audio_data ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
234 static void rtmp_handler_video_data ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
235 static void rtmp_handler_notify ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
237 static rtmp_packet_t *rtmp_new_packet( rtmp_control_thread_t *p_thread, uint8_t stream_index, uint32_t timestamp, uint8_t content_type, uint32_t src_dst, rtmp_body_t *body );
238 static block_t *rtmp_new_block( rtmp_control_thread_t *p_thread, uint8_t *buffer, int32_t length_buffer );
240 static rtmp_packet_t *rtmp_encode_onBWDone( rtmp_control_thread_t *p_thread, double number );
241 static rtmp_packet_t *rtmp_encode_server_bw( rtmp_control_thread_t *p_thread, uint32_t number );
242 static rtmp_packet_t *rtmp_encode_NetConnection_connect_result( rtmp_control_thread_t *p_thread, double number );
243 static rtmp_packet_t *rtmp_encode_createStream_result( rtmp_control_thread_t *p_thread, double stream_client, double stream_server );
244 static rtmp_packet_t *rtmp_encode_ping_reset_stream( rtmp_control_thread_t *p_thread );
245 static rtmp_packet_t *rtmp_encode_ping_clear_stream( rtmp_control_thread_t *p_thread, uint32_t src_dst );
246 static rtmp_packet_t *rtmp_encode_NetStream_play_reset_onStatus( rtmp_control_thread_t *p_thread, char *psz_media );
247 static rtmp_packet_t *rtmp_encode_NetStream_play_start_onStatus( rtmp_control_thread_t *p_thread, char *psz_media );
248 static uint8_t rtmp_encode_header_size( vlc_object_t *p_this, uint8_t header_size );
249 static uint8_t rtmp_decode_header_size( vlc_object_t *p_this, uint8_t header_size );
250 static uint8_t rtmp_get_stream_index( uint8_t content_type );
252 static void rtmp_body_append( rtmp_body_t *rtmp_body, uint8_t *buffer, uint32_t length );
254 static uint8_t *rtmp_encode_ping( uint16_t type, uint32_t src_dst, uint32_t third_arg, uint32_t fourth_arg );
256 /*****************************************************************************
257 * static AMF functions:
258 ******************************************************************************/
259 static uint8_t *amf_encode_element( uint8_t element, const void *value );
260 static uint8_t *amf_encode_object_variable( const char *key, uint8_t element, const void *value );
261 static double amf_decode_number( uint8_t **buffer );
262 static int amf_decode_boolean( uint8_t **buffer );
263 static char *amf_decode_string( uint8_t **buffer );
264 static char *amf_decode_object( uint8_t **buffer );
266 /*****************************************************************************
267 * static FLV functions:
268 ******************************************************************************/
269 static void flv_rebuild( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
270 static void flv_get_metadata_audio( rtmp_control_thread_t *p_thread, rtmp_packet_t *packet_audio, uint8_t *stereo, uint8_t *audiosamplesize, uint32_t *audiosamplerate, uint8_t *audiocodecid );
271 static void flv_get_metadata_video( rtmp_control_thread_t *p_thread, rtmp_packet_t *packet_video, uint8_t *videocodecid, uint8_t *frametype );
272 static rtmp_packet_t *flv_build_onMetaData( access_t *p_access, uint64_t duration, uint8_t stereo, uint8_t audiosamplesize, uint32_t audiosamplerate, uint8_t audiocodecid, uint8_t videocodecid );
274 /*****************************************************************************
275 * RTMP implementation:
276 ******************************************************************************/
278 rtmp_handshake_passive( vlc_object_t *p_this, int fd )
280 uint8_t p_read[RTMP_HANDSHAKE_BODY_SIZE + 1];
281 uint8_t p_write[RTMP_HANDSHAKE_BODY_SIZE * 2 + 1];
285 /* Receive handshake */
286 i_ret = net_Read( p_this, fd, NULL, p_read, RTMP_HANDSHAKE_BODY_SIZE + 1, true );
287 if( i_ret != RTMP_HANDSHAKE_BODY_SIZE + 1 )
289 msg_Err( p_this, "failed to receive handshake" );
293 /* Check handshake */
294 if ( p_read[0] != RTMP_HANDSHAKE )
296 msg_Err( p_this, "first byte in handshake corrupt" );
300 /* Answer handshake */
301 p_write[0] = RTMP_HANDSHAKE;
302 memset( p_write + 1, 0, RTMP_HANDSHAKE_BODY_SIZE );
303 memcpy( p_write + 1 + RTMP_HANDSHAKE_BODY_SIZE, p_read + 1, RTMP_HANDSHAKE_BODY_SIZE );
306 i_ret = net_Write( p_this, fd, NULL, p_write, RTMP_HANDSHAKE_BODY_SIZE * 2 + 1 );
307 if( i_ret != RTMP_HANDSHAKE_BODY_SIZE * 2 + 1 )
309 msg_Err( p_this, "failed to send handshake" );
313 /* Receive acknowledge */
314 i_ret = net_Read( p_this, fd, NULL, p_read, RTMP_HANDSHAKE_BODY_SIZE, true );
315 if( i_ret != RTMP_HANDSHAKE_BODY_SIZE )
317 msg_Err( p_this, "failed to receive acknowledge" );
321 /* Check acknowledge */
322 for(i = 8; i < RTMP_HANDSHAKE_BODY_SIZE; i++ )
323 if( p_write[i + 1] != p_read[i] )
325 msg_Err( p_this, "body acknowledge received corrupt" );
333 rtmp_handshake_active( vlc_object_t *p_this, int fd )
335 uint8_t p_read[RTMP_HANDSHAKE_BODY_SIZE * 2 + 1];
336 uint8_t p_write[RTMP_HANDSHAKE_BODY_SIZE + 1];
340 p_write[0] = RTMP_HANDSHAKE;
341 for( i = 0; i < RTMP_HANDSHAKE_BODY_SIZE; i++ )
342 p_write[i + 1] = i & 0xFF;
345 i_ret = net_Write( p_this, fd, NULL, p_write, RTMP_HANDSHAKE_BODY_SIZE + 1 );
346 if( i_ret != RTMP_HANDSHAKE_BODY_SIZE + 1 )
348 msg_Err( p_this, "failed to send handshake" );
352 /* Receive handshake */
353 i_ret = net_Read( p_this, fd, NULL, p_read, RTMP_HANDSHAKE_BODY_SIZE * 2 + 1, true );
354 if( i_ret != RTMP_HANDSHAKE_BODY_SIZE * 2 + 1 )
356 msg_Err( p_this, "failed to receive handshake" );
360 /* Check handshake */
361 if( p_read[0] != RTMP_HANDSHAKE )
363 msg_Err( p_this, "first byte in handshake received corrupt" );
367 for(i = 8; i < RTMP_HANDSHAKE_BODY_SIZE; i++ )
368 if( p_write[i + 1] != p_read[i + 1 + RTMP_HANDSHAKE_BODY_SIZE] )
370 msg_Err( p_this, "body handshake received corrupt" );
374 /* Acknowledge handshake */
375 i_ret = net_Write( p_this, fd, NULL, p_read + 1, RTMP_HANDSHAKE_BODY_SIZE );
376 if( i_ret != RTMP_HANDSHAKE_BODY_SIZE )
378 msg_Err( p_this, "failed to acknowledge handshake" );
386 rtmp_connect_active( rtmp_control_thread_t *p_thread )
388 rtmp_packet_t *rtmp_packet;
389 rtmp_body_t *rtmp_body;
394 /* Build NetConnection.connect call */
395 rtmp_body = rtmp_body_new( -1 );
397 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "connect" );
398 rtmp_body_append( rtmp_body, tmp_buffer,
399 AMF_DATATYPE_SIZE_STRING + strlen( "connect" ) );
402 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
403 &AMF_CALL_NETCONNECTION_CONNECT );
404 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
407 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
408 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
411 tmp_buffer = amf_encode_object_variable( "app",
412 AMF_DATATYPE_STRING, p_thread->psz_application );
413 rtmp_body_append( rtmp_body, tmp_buffer,
414 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "app" ) +
415 AMF_DATATYPE_SIZE_STRING + strlen( p_thread->psz_application ) );
418 tmp_buffer = amf_encode_object_variable( "flashVer",
419 AMF_DATATYPE_STRING, "LNX 9,0,48,0" );
420 rtmp_body_append( rtmp_body, tmp_buffer,
421 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "flashVer" ) +
422 AMF_DATATYPE_SIZE_STRING + strlen( "LNX 9,0,48,0" ) );
425 tmp_buffer = amf_encode_object_variable( "swfUrl",
426 AMF_DATATYPE_STRING, "file:///mac.flv" );
427 rtmp_body_append( rtmp_body, tmp_buffer,
428 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "swfUrl" ) +
429 AMF_DATATYPE_SIZE_STRING + strlen( "file:///mac.flv" ) );
432 tmp_url = (char *) malloc( strlen( "rtmp://") + strlen( p_thread->url.psz_buffer ) + 1 );
435 free( rtmp_body->body );
439 sprintf( tmp_url, "rtmp://%s", p_thread->url.psz_buffer );
440 tmp_buffer = amf_encode_object_variable( "tcUrl",
441 AMF_DATATYPE_STRING, tmp_url );
442 rtmp_body_append( rtmp_body, tmp_buffer,
443 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "tcUrl" ) +
444 AMF_DATATYPE_SIZE_STRING + strlen( tmp_url ) );
448 tmp_buffer = amf_encode_object_variable( "fpad",
449 AMF_DATATYPE_BOOLEAN, &AMF_BOOLEAN_FALSE );
450 rtmp_body_append( rtmp_body, tmp_buffer,
451 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "fpad" ) +
452 AMF_DATATYPE_SIZE_BOOLEAN );
455 tmp_buffer = amf_encode_object_variable( "audioCodecs",
456 AMF_DATATYPE_NUMBER, &AMF_CALL_NETCONNECTION_CONNECT_AUDIOCODECS );
457 rtmp_body_append( rtmp_body, tmp_buffer,
458 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audioCodecs" ) +
459 AMF_DATATYPE_SIZE_NUMBER );
462 tmp_buffer = amf_encode_object_variable( "videoCodecs",
463 AMF_DATATYPE_NUMBER, &AMF_CALL_NETCONNECTION_CONNECT_VIDEOCODECS );
464 rtmp_body_append( rtmp_body, tmp_buffer,
465 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "videoCodecs" ) +
466 AMF_DATATYPE_SIZE_NUMBER );
469 tmp_buffer = amf_encode_object_variable( "videoFunction",
470 AMF_DATATYPE_NUMBER, &AMF_CALL_NETCONNECTION_CONNECT_VIDEOFUNCTION );
471 rtmp_body_append( rtmp_body, tmp_buffer,
472 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "videoFunction" ) +
473 AMF_DATATYPE_SIZE_NUMBER );
476 tmp_buffer = amf_encode_object_variable( "pageUrl",
477 AMF_DATATYPE_STRING, "file:///mac.html" );
478 rtmp_body_append( rtmp_body, tmp_buffer,
479 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "pageUrl" ) +
480 AMF_DATATYPE_SIZE_STRING + strlen( "file:///mac.html" ) );
483 tmp_buffer = amf_encode_object_variable( "objectEncoding",
484 AMF_DATATYPE_NUMBER, &AMF_CALL_NETCONNECTION_CONNECT_OBJECTENCODING );
485 rtmp_body_append( rtmp_body, tmp_buffer,
486 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "objectEncoding" ) +
487 AMF_DATATYPE_SIZE_NUMBER );
490 tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
491 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
494 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
495 0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
496 free( rtmp_body->body );
499 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
501 /* Call NetConnection.connect */
502 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
503 if( i_ret != rtmp_packet->length_encoded )
505 free( rtmp_packet->body->body );
506 free( rtmp_packet->body );
509 msg_Err( p_thread, "failed send call NetConnection.connect" );
512 free( rtmp_packet->body->body );
513 free( rtmp_packet->body );
517 /* Wait for NetConnection.connect result */
518 vlc_mutex_lock( &p_thread->lock );
519 vlc_cond_wait( &p_thread->wait, &p_thread->lock );
520 vlc_mutex_unlock( &p_thread->lock );
522 if( p_thread->result_connect )
524 msg_Err( p_thread, "failed call NetConnection.connect" );
528 /* Force control thread to stop if receive NetStream.play call and wait is not ready */
529 vlc_mutex_lock( &p_thread->lock );
531 /* Build NetStream.createStream call */
532 rtmp_body = rtmp_body_new( -1 );
534 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "createStream" );
535 rtmp_body_append( rtmp_body, tmp_buffer,
536 AMF_DATATYPE_SIZE_STRING + strlen( "createStream" ) );
539 p_thread->stream_client_id = RTMP_DEFAULT_STREAM_CLIENT_ID;
541 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
542 &AMF_CALL_STREAM_CLIENT_NUMBER );
543 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
546 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
547 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
550 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
551 0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
552 free( rtmp_body->body );
555 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
557 /* Call NetStream.createStream */
558 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
559 if( i_ret != rtmp_packet->length_encoded )
561 free( rtmp_packet->body->body );
562 free( rtmp_packet->body );
565 msg_Err( p_thread, "failed send call NetStream.createStream" );
568 free( rtmp_packet->body->body );
569 free( rtmp_packet->body );
572 /*TODO: read server stream number*/
573 /* Build ping packet */
574 rtmp_body = rtmp_body_new( -1 );
576 tmp_buffer = rtmp_encode_ping( RTMP_PING_BUFFER_TIME_CLIENT, RTMP_SRC_DST_CONNECT_OBJECT, RTMP_TIME_CLIENT_BUFFER, 0 );
577 rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_BUFFER_TIME_CLIENT );
580 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
581 0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
582 free( rtmp_body->body );
585 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
587 /* Send ping packet */
588 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
589 if( i_ret != rtmp_packet->length_encoded )
591 free( rtmp_packet->body->body );
592 free( rtmp_packet->body );
595 msg_Err( p_thread, "failed send ping BUFFER_TIME_CLIENT" );
598 free( rtmp_packet->body->body );
599 free( rtmp_packet->body );
603 /* Build NetStream.play call */
604 rtmp_body = rtmp_body_new( -1 );
606 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "play" );
607 rtmp_body_append( rtmp_body, tmp_buffer,
608 AMF_DATATYPE_SIZE_STRING + strlen( "play" ) );
611 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
612 &AMF_CALL_NETSTREAM_PLAY );
613 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
616 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
617 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
620 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, p_thread->psz_media );
621 rtmp_body_append( rtmp_body, tmp_buffer,
622 AMF_DATATYPE_SIZE_STRING + strlen( p_thread->psz_media ) );
625 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
626 0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
627 free( rtmp_body->body );
630 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
632 /* Call NetStream.play */
633 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
634 if( i_ret != rtmp_packet->length_encoded )
636 free( rtmp_packet->body->body );
637 free( rtmp_packet->body );
640 msg_Err( p_thread, "failed send call NetStream.play" );
643 free( rtmp_packet->body->body );
644 free( rtmp_packet->body );
648 /* Build ping packet */
649 rtmp_body = rtmp_body_new( -1 );
651 tmp_buffer = rtmp_encode_ping( RTMP_PING_BUFFER_TIME_CLIENT, RTMP_SRC_DST_CONNECT_OBJECT2, RTMP_TIME_CLIENT_BUFFER, 0 );
652 rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_BUFFER_TIME_CLIENT );
655 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
656 0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
657 free( rtmp_body->body );
660 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
662 /* Send ping packet */
663 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
664 if( i_ret != rtmp_packet->length_encoded )
666 free( rtmp_packet->body->body );
667 free( rtmp_packet->body );
670 msg_Err( p_thread, "failed send ping BUFFER_TIME_CLIENT" );
673 free( rtmp_packet->body->body );
674 free( rtmp_packet->body );
678 /* Wait for NetStream.play.start result */
679 vlc_cond_wait( &p_thread->wait, &p_thread->lock );
680 vlc_mutex_unlock( &p_thread->lock );
682 if( p_thread->result_play )
684 msg_Err( p_thread, "failed call NetStream.play" );
688 /* Next packet is the beginning of flv stream */
689 msg_Dbg( p_thread, "next packet is the beginning of flv stream" );
695 rtmp_connect_passive( rtmp_control_thread_t *p_thread )
697 /* Force control thread to stop if receive NetStream.play call and wait is not ready */
698 vlc_mutex_lock( &p_thread->lock );
700 /* Wait for NetStream.play.start result */
701 vlc_cond_wait( &p_thread->wait, &p_thread->lock );
702 vlc_mutex_unlock( &p_thread->lock );
704 if( p_thread->result_play )
706 msg_Err( p_thread, "failed call NetStream.play" );
715 rtmp_seek( access_t *p_access, int64_t i_pos )
717 access_sys_t *p_sys = p_access->p_sys;
718 rtmp_packet_t *rtmp_packet;
719 rtmp_body_t *rtmp_body;
723 msg_Warn(p_access, "i_pos %lld", i_pos);
724 // Build NetStream.seek call //
725 rtmp_body = rtmp_body_new();
727 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "seek" );
728 rtmp_body_append( rtmp_body, tmp_buffer,
729 AMF_DATATYPE_SIZE_STRING + strlen( "seek" ) );
733 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
735 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
738 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
739 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
741 //TODO: convert i_pos to double and see if they are milliseconds
742 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
744 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
747 rtmp_packet = rtmp_new_packet( p_sys->p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
748 0, RTMP_DATATYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
749 free( rtmp_body->body );
752 tmp_buffer = rtmp_encode_packet( p_access, rtmp_packet );
754 // Call NetStream.seek //
755 i_ret = net_Write( p_access, p_sys->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
756 if( i_ret < rtmp_packet->length_encoded )
758 free( rtmp_packet->body->body );
759 free( rtmp_packet->body );
762 msg_Err( p_access, "failed call NetStream.seek" );
765 free( rtmp_packet->body->body );
766 free( rtmp_packet->body );
770 // Receive TODO: see what //
771 rtmp_packet = rtmp_read_net_packet( p_access, p_sys->fd );
772 free( rtmp_packet->body->body );
773 free( rtmp_packet->body );
781 rtmp_build_bytes_read( rtmp_control_thread_t *p_thread, uint32_t reply )
783 rtmp_packet_t *rtmp_packet;
784 rtmp_body_t *rtmp_body;
787 /* Build bytes read packet */
788 rtmp_body = rtmp_body_new( -1 );
790 tmp_buffer = (uint8_t *) malloc( sizeof( uint32_t ) * sizeof( uint8_t ) );
791 if( !tmp_buffer ) return NULL;
793 reply = hton32( reply );
794 memcpy( tmp_buffer, &reply, sizeof( uint32_t ) );
796 rtmp_body_append( rtmp_body, tmp_buffer, sizeof( uint32_t ) );
799 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
800 0, RTMP_CONTENT_TYPE_BYTES_READ, 0, rtmp_body );
801 free( rtmp_body->body );
808 rtmp_build_publish_start( rtmp_control_thread_t *p_thread )
810 rtmp_packet_t *rtmp_packet;
811 rtmp_body_t *rtmp_body;
814 /* Build publish start event */
815 rtmp_body = rtmp_body_new( -1 );
817 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onStatus" );
818 rtmp_body_append( rtmp_body, tmp_buffer,
819 AMF_DATATYPE_SIZE_STRING + strlen( "onStatus" ) );
822 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
823 &p_thread->stream_server_id );
824 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
827 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
828 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
831 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
832 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
835 tmp_buffer = amf_encode_object_variable( "level",
836 AMF_DATATYPE_STRING, "status" );
837 rtmp_body_append( rtmp_body, tmp_buffer,
838 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
839 AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
842 tmp_buffer = amf_encode_object_variable( "code",
843 AMF_DATATYPE_STRING, "NetStream.Publish.Start" );
844 rtmp_body_append( rtmp_body, tmp_buffer,
845 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
846 AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Publish.Start" ) );
849 tmp_buffer = amf_encode_object_variable( "description",
850 AMF_DATATYPE_STRING, "" );
851 rtmp_body_append( rtmp_body, tmp_buffer,
852 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
853 AMF_DATATYPE_SIZE_STRING + strlen( "" ) );
856 tmp_buffer = amf_encode_object_variable( "details",
857 AMF_DATATYPE_STRING, p_thread->psz_publish );
858 rtmp_body_append( rtmp_body, tmp_buffer,
859 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "details" ) +
860 AMF_DATATYPE_SIZE_STRING + strlen( p_thread->psz_publish ) );
863 tmp_buffer = amf_encode_object_variable( "clientid",
864 AMF_DATATYPE_NUMBER, &p_thread->stream_client_id );
865 rtmp_body_append( rtmp_body, tmp_buffer,
866 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "clientid" ) +
867 AMF_DATATYPE_SIZE_NUMBER );
870 tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
871 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
874 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
875 0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
876 free( rtmp_body->body );
883 rtmp_build_flv_over_rtmp( rtmp_control_thread_t *p_thread, block_t *p_buffer )
885 rtmp_packet_t *rtmp_packet;
887 if( p_thread->flv_length_body > 0 )
889 p_thread->flv_length_body -= p_buffer->i_buffer;
890 rtmp_body_append( p_thread->flv_body, p_buffer->p_buffer, p_buffer->i_buffer );
892 if( p_thread->flv_length_body > 0 )
898 p_thread->flv_content_type = *p_buffer->p_buffer;
900 p_buffer->p_buffer[0] = 0;
901 p_thread->flv_length_body = ntoh32( *(uint32_t *) (p_buffer->p_buffer) );
903 p_buffer->p_buffer[3] = 0;
904 p_thread->flv_timestamp = ntoh32( *(uint32_t *) (p_buffer->p_buffer + 3) );
907 if( p_thread->flv_length_body > p_buffer->i_buffer - FLV_TAG_SIZE - FLV_TAG_PREVIOUS_TAG_SIZE )
909 p_thread->flv_length_body -= p_buffer->i_buffer - FLV_TAG_SIZE - FLV_TAG_PREVIOUS_TAG_SIZE;
910 rtmp_body_append( p_thread->flv_body, p_buffer->p_buffer + FLV_TAG_SIZE, p_buffer->i_buffer - FLV_TAG_SIZE );
916 rtmp_body_append( p_thread->flv_body, p_buffer->p_buffer + FLV_TAG_SIZE, p_thread->flv_length_body );
919 rtmp_packet = rtmp_new_packet( p_thread, rtmp_get_stream_index( p_thread->flv_content_type ),
920 p_thread->flv_timestamp, p_thread->flv_content_type, RTMP_SRC_DST_DEFAULT, p_thread->flv_body );
921 p_thread->flv_length_body = 0;
922 rtmp_body_reset( p_thread->flv_body );
928 rtmp_read_net_packet( rtmp_control_thread_t *p_thread )
934 rtmp_packet_t *rtmp_packet;
940 i_ret = net_Read( p_thread, p_thread->fd, NULL, p_read, 1, true );
944 length_header = rtmp_decode_header_size( (vlc_object_t *) p_thread, p_read[0] & RTMP_HEADER_SIZE_MASK );
945 stream_index = p_read[0] & RTMP_HEADER_STREAM_INDEX_MASK;
947 i_ret = net_Read( p_thread, p_thread->fd, NULL, p_read + 1, length_header - 1, true );
948 if( i_ret != length_header - 1 )
951 /* Update timestamp if not is an interchunk packet */
952 if( length_header == 1 && p_thread->rtmp_headers_recv[stream_index].body == NULL )
954 p_thread->rtmp_headers_recv[stream_index].timestamp +=
955 p_thread->rtmp_headers_recv[stream_index].timestamp_relative;
958 /* Length 4 and 8 headers have relative timestamp */
959 if( length_header == 4 || length_header == 8 )
963 p_thread->rtmp_headers_recv[stream_index].timestamp_relative = ntoh32( *(uint32_t *) p_read );
964 p_thread->rtmp_headers_recv[stream_index].timestamp +=
965 p_thread->rtmp_headers_recv[stream_index].timestamp_relative;
968 if( length_header >= 8 )
972 p_thread->rtmp_headers_recv[stream_index].length_body = ntoh32( *(uint32_t *) (p_read + 3) );
973 p_thread->rtmp_headers_recv[stream_index].content_type = p_read[7];
976 /* Length 12 headers have absolute timestamp */
977 if( length_header >= 12 )
981 p_thread->rtmp_headers_recv[stream_index].timestamp = ntoh32( *(uint32_t *) p_read );
982 p_thread->rtmp_headers_recv[stream_index].src_dst = ntoh32( *(uint32_t *) (p_read + 8) );
985 if( p_thread->rtmp_headers_recv[stream_index].body == NULL )
987 p_thread->rtmp_headers_recv[stream_index].body =
988 rtmp_body_new( p_thread->rtmp_headers_recv[stream_index].length_body );
991 bytes_left = p_thread->rtmp_headers_recv[stream_index].body->length_buffer -
992 p_thread->rtmp_headers_recv[stream_index].body->length_body;
994 if( bytes_left > p_thread->chunk_size_recv )
995 bytes_left = p_thread->chunk_size_recv;
997 i_ret = net_Read( p_thread, p_thread->fd, NULL,
998 p_thread->rtmp_headers_recv[stream_index].body->body +
999 p_thread->rtmp_headers_recv[stream_index].body->length_body,
1001 if( i_ret != bytes_left )
1004 p_thread->rtmp_headers_recv[stream_index].body->length_body += bytes_left;
1006 if( p_thread->rtmp_headers_recv[stream_index].length_body == p_thread->rtmp_headers_recv[stream_index].body->length_body )
1008 rtmp_packet = (rtmp_packet_t *) malloc( sizeof( rtmp_packet_t ) );
1009 if( !rtmp_packet ) goto error;
1011 rtmp_packet->stream_index = stream_index;
1012 rtmp_packet->timestamp = p_thread->rtmp_headers_recv[stream_index].timestamp;
1013 rtmp_packet->timestamp_relative = p_thread->rtmp_headers_recv[stream_index].timestamp_relative;
1014 rtmp_packet->content_type = p_thread->rtmp_headers_recv[stream_index].content_type;
1015 rtmp_packet->src_dst = p_thread->rtmp_headers_recv[stream_index].src_dst;
1016 rtmp_packet->length_body = p_thread->rtmp_headers_recv[stream_index].length_body;
1017 rtmp_packet->body = p_thread->rtmp_headers_recv[stream_index].body;
1019 p_thread->rtmp_headers_recv[stream_index].body = NULL;
1026 msg_Err( p_thread, "rtmp_read_net_packet: net_Read error");
1031 rtmp_init_handler( rtmp_handler_t *rtmp_handler )
1033 rtmp_handler[RTMP_CONTENT_TYPE_CHUNK_SIZE] = rtmp_handler_chunk_size;
1034 rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_02] = rtmp_handler_null;
1035 rtmp_handler[RTMP_CONTENT_TYPE_BYTES_READ] = rtmp_handler_null;
1036 rtmp_handler[RTMP_CONTENT_TYPE_PING] = rtmp_handler_null;
1037 rtmp_handler[RTMP_CONTENT_TYPE_SERVER_BW] = rtmp_handler_null;
1038 rtmp_handler[RTMP_CONTENT_TYPE_CLIENT_BW] = rtmp_handler_null;
1039 rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_07] = rtmp_handler_null;
1040 rtmp_handler[RTMP_CONTENT_TYPE_AUDIO_DATA] = rtmp_handler_audio_data;
1041 rtmp_handler[RTMP_CONTENT_TYPE_VIDEO_DATA] = rtmp_handler_video_data;
1042 rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_0A_0E] = rtmp_handler_null;
1043 rtmp_handler[RTMP_CONTENT_TYPE_FLEX_STREAM] = rtmp_handler_null;
1044 rtmp_handler[RTMP_CONTENT_TYPE_FLEX_SHARED_OBJECT] = rtmp_handler_null;
1045 rtmp_handler[RTMP_CONTENT_TYPE_MESSAGE] = rtmp_handler_null;
1046 rtmp_handler[RTMP_CONTENT_TYPE_NOTIFY] = rtmp_handler_notify;
1047 rtmp_handler[RTMP_CONTENT_TYPE_SHARED_OBJECT] = rtmp_handler_null;
1048 rtmp_handler[RTMP_CONTENT_TYPE_INVOKE] = rtmp_handler_invoke;
1052 rtmp_handler_null( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1054 VLC_UNUSED(p_thread);
1055 free( rtmp_packet->body->body );
1056 free( rtmp_packet->body );
1057 free( rtmp_packet );
1061 rtmp_handler_chunk_size( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1063 p_thread->chunk_size_recv = ntoh32( *(uint32_t *) (rtmp_packet->body->body) );
1065 free( rtmp_packet->body->body );
1066 free( rtmp_packet->body );
1067 free( rtmp_packet );
1071 rtmp_handler_audio_data( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1075 if( !p_thread->has_audio )
1077 p_thread->has_audio = 1;
1079 flv_get_metadata_audio( p_thread, rtmp_packet,
1080 &p_thread->metadata_stereo, &p_thread->metadata_samplesize,
1081 &p_thread->metadata_samplerate, &p_thread->metadata_audiocodecid );
1084 flv_rebuild( p_thread, rtmp_packet );
1085 p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
1086 block_FifoPut( p_thread->p_fifo_input, p_buffer );
1088 free( rtmp_packet->body->body );
1089 free( rtmp_packet->body );
1090 free( rtmp_packet );
1094 rtmp_handler_video_data( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1098 if( !p_thread->has_video )
1100 p_thread->has_video = 1;
1102 flv_get_metadata_video( p_thread, rtmp_packet,
1103 &p_thread->metadata_videocodecid, &p_thread->metadata_frametype );
1106 flv_rebuild( p_thread, rtmp_packet );
1107 p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
1108 block_FifoPut( p_thread->p_fifo_input, p_buffer );
1110 free( rtmp_packet->body->body );
1111 free( rtmp_packet->body );
1112 free( rtmp_packet );
1116 rtmp_handler_notify( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1120 p_thread->metadata_received = 1;
1122 flv_rebuild( p_thread, rtmp_packet );
1123 p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
1124 block_FifoPut( p_thread->p_fifo_input, p_buffer );
1126 free( rtmp_packet->body->body );
1127 free( rtmp_packet->body );
1128 free( rtmp_packet );
1132 rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1134 rtmp_packet_t *tmp_rtmp_packet;
1135 uint8_t *i, *end, *tmp_buffer;
1137 char *string, *string2;
1140 i = rtmp_packet->body->body;
1141 end = rtmp_packet->body->body + rtmp_packet->body->length_body;
1143 i++; /* Pass over AMF_DATATYPE_STRING */
1144 string = amf_decode_string( &i );
1146 i++; /* Pass over AMF_DATATYPE_NUMBER */
1147 number = amf_decode_number( &i );
1149 msg_Dbg( p_thread, "%s %.1f", string, number );
1151 if( strcmp( "connect", string ) == 0 )
1153 /* Connection bandwith */
1154 tmp_rtmp_packet = rtmp_encode_onBWDone( p_thread, AMF_CALL_ONBWDONE );
1156 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1158 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1159 if( i_ret != tmp_rtmp_packet->length_encoded )
1161 msg_Err( p_thread, "failed send connection bandwith" );
1164 free( tmp_rtmp_packet->body->body );
1165 free( tmp_rtmp_packet->body );
1166 free( tmp_rtmp_packet );
1169 /* Server bandwith */
1170 tmp_rtmp_packet = rtmp_encode_server_bw( p_thread, RTMP_SERVER_BW );
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 server bandwith" );
1180 free( tmp_rtmp_packet->body->body );
1181 free( tmp_rtmp_packet->body );
1182 free( tmp_rtmp_packet );
1186 tmp_rtmp_packet = rtmp_encode_ping_clear_stream( p_thread, RTMP_SRC_DST_CONNECT_OBJECT );
1188 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1190 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1191 if( i_ret != tmp_rtmp_packet->length_encoded )
1193 msg_Err( p_thread, "failed send clear stream" );
1196 free( tmp_rtmp_packet->body->body );
1197 free( tmp_rtmp_packet->body );
1198 free( tmp_rtmp_packet );
1201 /* Reply NetConnection.connect */
1202 tmp_rtmp_packet = rtmp_encode_NetConnection_connect_result( p_thread, number );
1204 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1206 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1207 if( i_ret != tmp_rtmp_packet->length_encoded )
1209 msg_Err( p_thread, "failed send reply NetConnection.connect" );
1212 free( tmp_rtmp_packet->body->body );
1213 free( tmp_rtmp_packet->body );
1214 free( tmp_rtmp_packet );
1217 else if( strcmp( "createStream", string ) == 0 )
1219 p_thread->stream_client_id = number;
1220 p_thread->stream_server_id = RTMP_DEFAULT_STREAM_SERVER_ID;
1222 /* Reply createStream */
1223 tmp_rtmp_packet = rtmp_encode_createStream_result( p_thread, p_thread->stream_client_id, p_thread->stream_server_id );
1225 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1227 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1228 if( i_ret != tmp_rtmp_packet->length_encoded )
1230 msg_Err( p_thread, "failed send reply createStream" );
1233 free( tmp_rtmp_packet->body->body );
1234 free( tmp_rtmp_packet->body );
1235 free( tmp_rtmp_packet );
1239 tmp_rtmp_packet = rtmp_encode_ping_reset_stream( p_thread );
1241 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1243 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1244 if( i_ret != tmp_rtmp_packet->length_encoded )
1246 msg_Err( p_thread, "failed send reset stream" );
1249 free( tmp_rtmp_packet->body->body );
1250 free( tmp_rtmp_packet->body );
1251 free( tmp_rtmp_packet );
1255 tmp_rtmp_packet = rtmp_encode_ping_clear_stream( p_thread, RTMP_SRC_DST_CONNECT_OBJECT2 );
1257 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1259 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1260 if( i_ret != tmp_rtmp_packet->length_encoded )
1262 msg_Err( p_thread, "failed send clear stream" );
1265 free( tmp_rtmp_packet->body->body );
1266 free( tmp_rtmp_packet->body );
1267 free( tmp_rtmp_packet );
1270 else if( strcmp( "publish", string ) == 0 )
1273 msg_Dbg( p_thread, "null" );
1276 string2 = amf_decode_string( &i );
1277 msg_Dbg( p_thread, "string: %s", string2 );
1279 p_thread->psz_publish = strdup( string2 );
1283 else if( strcmp( "play", string ) == 0 )
1286 msg_Dbg( p_thread, "null" );
1289 string2 = amf_decode_string( &i );
1290 msg_Dbg( p_thread, "string: %s", string2 );
1292 /* Reply NetStream.play.reset */
1293 tmp_rtmp_packet = rtmp_encode_NetStream_play_reset_onStatus( p_thread, string2 );
1295 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1297 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1298 if( i_ret != tmp_rtmp_packet->length_encoded )
1300 msg_Err( p_thread, "failed send reply NetStream.play.reset" );
1303 free( tmp_rtmp_packet->body->body );
1304 free( tmp_rtmp_packet->body );
1305 free( tmp_rtmp_packet );
1308 /* Reply NetStream.play.start */
1309 tmp_rtmp_packet = rtmp_encode_NetStream_play_start_onStatus( p_thread, string2 );
1311 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1313 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1314 if( i_ret != tmp_rtmp_packet->length_encoded )
1316 msg_Err( p_thread, "failed send reply NetStream.play.start" );
1319 free( tmp_rtmp_packet->body->body );
1320 free( tmp_rtmp_packet->body );
1321 free( tmp_rtmp_packet );
1326 p_thread->result_play = 0;
1328 vlc_mutex_lock( &p_thread->lock );
1329 vlc_cond_signal( &p_thread->wait );
1330 vlc_mutex_unlock( &p_thread->lock );
1337 if( *i == AMF_DATATYPE_NUMBER )
1340 msg_Dbg( p_thread, "number: %le", amf_decode_number( &i ) );
1342 else if( *i == AMF_DATATYPE_BOOLEAN )
1345 msg_Dbg( p_thread, "boolean: %s", amf_decode_boolean( &i ) ? "true" : "false" );
1347 else if( *i == AMF_DATATYPE_STRING )
1350 string = amf_decode_string( &i );
1351 msg_Dbg( p_thread, "string: %s", string );
1354 else if( *i == AMF_DATATYPE_OBJECT )
1357 msg_Dbg( p_thread, "object" );
1358 while( ( string = amf_decode_object( &i ) ) != NULL )
1360 if( *i == AMF_DATATYPE_NUMBER )
1363 msg_Dbg( p_thread, "key: %s value: %le", string, amf_decode_number( &i ) );
1365 else if( *i == AMF_DATATYPE_BOOLEAN )
1368 msg_Dbg( p_thread, "key: %s value: %s", string, amf_decode_boolean( &i ) ? "true" : "false" );
1370 else if( *i == AMF_DATATYPE_STRING )
1373 string2 = amf_decode_string( &i );
1374 msg_Dbg( p_thread, "key: %s value: %s", string, string2 );
1375 if( strcmp( "code", string ) == 0 )
1377 if( strcmp( "NetConnection.Connect.Success", string2 ) == 0 )
1379 p_thread->result_connect = 0;
1381 vlc_mutex_lock( &p_thread->lock );
1382 vlc_cond_signal( &p_thread->wait );
1383 vlc_mutex_unlock( &p_thread->lock );
1385 else if( strcmp( "NetConnection.Connect.InvalidApp", string2 ) == 0 )
1387 p_thread->b_die = 1;
1389 vlc_mutex_lock( &p_thread->lock );
1390 vlc_cond_signal( &p_thread->wait );
1391 vlc_mutex_unlock( &p_thread->lock );
1393 else if( strcmp( "NetStream.Play.Start", string2 ) == 0 )
1395 p_thread->result_play = 0;
1397 vlc_mutex_lock( &p_thread->lock );
1398 vlc_cond_signal( &p_thread->wait );
1399 vlc_mutex_unlock( &p_thread->lock );
1401 else if( strcmp( "NetStream.Play.Stop", string2 ) == 0 )
1403 p_thread->result_stop = 1;
1405 block_FifoWake( p_thread->p_fifo_input );
1410 else if( *i == AMF_DATATYPE_NULL )
1413 msg_Dbg( p_thread, "key: %s value: Null", string );
1415 else if( *i == AMF_DATATYPE_UNDEFINED )
1418 msg_Dbg( p_thread, "key: %s value: undefined (Null)", string );
1423 msg_Warn( p_thread, "key: %s value: undefined AMF type", string );
1427 msg_Dbg( p_thread, "end of object" );
1429 else if( *i == AMF_DATATYPE_NULL)
1432 msg_Dbg( p_thread, "null" );
1434 else if( *i == AMF_DATATYPE_UNDEFINED )
1437 msg_Dbg( p_thread, "undefined (null)" );
1442 msg_Warn( p_thread, "undefined AMF type" );
1446 free( rtmp_packet->body->body );
1447 free( rtmp_packet->body );
1448 free( rtmp_packet );
1453 free( tmp_rtmp_packet->body->body );
1454 free( tmp_rtmp_packet->body );
1455 free( tmp_rtmp_packet );
1459 /* length header calculated automatically based on last packet in the same channel */
1460 /* timestamps passed are always absolute */
1461 static rtmp_packet_t *
1462 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 )
1464 int interchunk_headers;
1465 rtmp_packet_t *rtmp_packet;
1467 rtmp_packet = (rtmp_packet_t *) malloc( sizeof( rtmp_packet_t ) );
1468 if( !rtmp_packet ) return NULL;
1470 interchunk_headers = body->length_body / p_thread->chunk_size_send;
1471 if( body->length_body % p_thread->chunk_size_send == 0 )
1472 interchunk_headers--;
1474 if( src_dst != p_thread->rtmp_headers_send[stream_index].src_dst )
1476 p_thread->rtmp_headers_send[stream_index].timestamp = timestamp;
1477 p_thread->rtmp_headers_send[stream_index].length_body = body->length_body;
1478 p_thread->rtmp_headers_send[stream_index].content_type = content_type;
1479 p_thread->rtmp_headers_send[stream_index].src_dst = src_dst;
1481 rtmp_packet->length_header = 12;
1483 else if( content_type != p_thread->rtmp_headers_send[stream_index].content_type
1484 || body->length_body != p_thread->rtmp_headers_send[stream_index].length_body )
1486 p_thread->rtmp_headers_send[stream_index].timestamp_relative =
1487 timestamp - p_thread->rtmp_headers_send[stream_index].timestamp;
1488 p_thread->rtmp_headers_send[stream_index].timestamp = timestamp;
1489 p_thread->rtmp_headers_send[stream_index].length_body = body->length_body;
1490 p_thread->rtmp_headers_send[stream_index].content_type = content_type;
1492 rtmp_packet->length_header = 8;
1494 else if( timestamp != p_thread->rtmp_headers_send[stream_index].timestamp )
1496 p_thread->rtmp_headers_send[stream_index].timestamp_relative =
1497 timestamp - p_thread->rtmp_headers_send[stream_index].timestamp;
1498 p_thread->rtmp_headers_send[stream_index].timestamp = timestamp;
1500 rtmp_packet->length_header = 4;
1504 rtmp_packet->length_header = 1;
1506 /*TODO: puede que no haga falta guardar el timestamp relative */
1507 rtmp_packet->stream_index = stream_index;
1508 if( rtmp_packet->length_header == 12 )
1510 rtmp_packet->timestamp = timestamp;
1511 rtmp_packet->timestamp_relative = 0;
1515 rtmp_packet->timestamp = timestamp;
1516 rtmp_packet->timestamp_relative = p_thread->rtmp_headers_send[stream_index].timestamp_relative;
1518 rtmp_packet->length_encoded = rtmp_packet->length_header + body->length_body + interchunk_headers;
1519 rtmp_packet->length_body = body->length_body;
1520 rtmp_packet->content_type = content_type;
1521 rtmp_packet->src_dst = src_dst;
1523 rtmp_packet->body = (rtmp_body_t *) malloc( sizeof( rtmp_body_t ) );
1524 if( !rtmp_packet->body )
1526 free( rtmp_packet );
1530 rtmp_packet->body->length_body = body->length_body;
1531 rtmp_packet->body->length_buffer = body->length_body;
1532 rtmp_packet->body->body = (uint8_t *) malloc( rtmp_packet->body->length_buffer * sizeof( uint8_t ) );
1533 if( !rtmp_packet->body->body )
1535 free( rtmp_packet->body );
1536 free( rtmp_packet );
1539 memcpy( rtmp_packet->body->body, body->body, rtmp_packet->body->length_body );
1545 rtmp_new_block( rtmp_control_thread_t *p_thread, uint8_t *buffer, int32_t length_buffer )
1548 /* DOWN: p_thread->p_empty_blocks->i_depth */
1549 while ( block_FifoCount( p_thread->p_empty_blocks ) > MAX_EMPTY_BLOCKS )
1551 p_buffer = block_FifoGet( p_thread->p_empty_blocks );
1552 block_Release( p_buffer );
1554 /* DOWN: p_thread->p_empty_blocks->i_depth */
1555 if( block_FifoCount( p_thread->p_empty_blocks ) == 0 )
1557 p_buffer = block_New( p_thread, length_buffer );
1561 p_buffer = block_FifoGet( p_thread->p_empty_blocks );
1562 p_buffer = block_Realloc( p_buffer, 0, length_buffer );
1565 p_buffer->i_buffer = length_buffer;
1567 memcpy( p_buffer->p_buffer, buffer, p_buffer->i_buffer );
1572 /* call sequence for each packet rtmp_new_packet -> rtmp_encode_packet -> send */
1573 /* no parallelism allowed because of optimization in header length */
1575 rtmp_encode_packet( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1578 int interchunk_headers;
1579 uint32_t timestamp, length_body, src_dst;
1582 out = (uint8_t *) malloc( rtmp_packet->length_encoded * sizeof( uint8_t ) );
1583 if( !out ) return NULL;
1585 interchunk_headers = rtmp_packet->body->length_body / p_thread->chunk_size_send;
1586 if( rtmp_packet->body->length_body % p_thread->chunk_size_send == 0 )
1587 interchunk_headers--;
1589 if( rtmp_packet->length_header == 12 )
1591 /* Timestamp absolute */
1592 timestamp = hton32( rtmp_packet->timestamp );
1593 memcpy( out, ×tamp, sizeof( uint32_t ) );
1595 src_dst = hton32( rtmp_packet->src_dst );
1596 memcpy( out + 8, &src_dst, sizeof( uint32_t ) );
1599 if( rtmp_packet->length_header >= 8 )
1601 /* Length without inter chunk headers */
1602 length_body = hton32( rtmp_packet->body->length_body );
1603 memcpy( out + 3, &length_body, sizeof( uint32_t ) );
1605 out[7] = rtmp_packet->content_type;
1607 if( rtmp_packet->length_header >= 4 && rtmp_packet->length_header != 12 )
1609 /* Timestamp relative */
1610 timestamp = hton32( rtmp_packet->timestamp_relative );
1611 memcpy( out, ×tamp, sizeof( uint32_t ) );
1614 out[0] = rtmp_encode_header_size( (vlc_object_t *) p_thread, rtmp_packet->length_header ) + rtmp_packet->stream_index;
1616 /* Insert inter chunk headers */
1617 for(i = 0, j = 0; i < rtmp_packet->body->length_body + interchunk_headers; i++, j++)
1619 if( j % p_thread->chunk_size_send == 0 && j != 0 )
1620 out[rtmp_packet->length_header + i++] = RTMP_HEADER_SIZE_1 + rtmp_packet->stream_index;
1621 out[rtmp_packet->length_header + i] = rtmp_packet->body->body[j];
1627 static rtmp_packet_t *
1628 rtmp_encode_onBWDone( rtmp_control_thread_t *p_thread, double number )
1630 rtmp_packet_t *rtmp_packet;
1631 rtmp_body_t *rtmp_body;
1632 uint8_t *tmp_buffer;
1634 /* Build onBWDone */
1635 rtmp_body = rtmp_body_new( -1 );
1637 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onBWDone" );
1638 rtmp_body_append( rtmp_body, tmp_buffer,
1639 AMF_DATATYPE_SIZE_STRING + strlen( "onBWDone" ) );
1642 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1643 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1646 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1647 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1650 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1651 0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_CONNECT_OBJECT, rtmp_body );
1652 free( rtmp_body->body );
1658 static rtmp_packet_t *
1659 rtmp_encode_server_bw( rtmp_control_thread_t *p_thread, uint32_t number )
1661 rtmp_packet_t *rtmp_packet;
1662 rtmp_body_t *rtmp_body;
1664 /* Build server bw */
1665 rtmp_body = rtmp_body_new( -1 );
1667 rtmp_body_append( rtmp_body, (uint8_t *) &number, sizeof( uint32_t ) );
1669 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1670 0, RTMP_CONTENT_TYPE_SERVER_BW, RTMP_SRC_DST_CONNECT_OBJECT, rtmp_body );
1671 free( rtmp_body->body );
1677 static rtmp_packet_t *
1678 rtmp_encode_NetConnection_connect_result( rtmp_control_thread_t *p_thread, double number )
1680 rtmp_packet_t *rtmp_packet;
1681 rtmp_body_t *rtmp_body;
1682 uint8_t *tmp_buffer;
1684 /* Build NetConnection.connect result */
1685 rtmp_body = rtmp_body_new( -1 );
1687 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "_result" );
1688 rtmp_body_append( rtmp_body, tmp_buffer,
1689 AMF_DATATYPE_SIZE_STRING + strlen( "_result" ) );
1692 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1693 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1696 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1697 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1700 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1701 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1704 tmp_buffer = amf_encode_object_variable( "level",
1705 AMF_DATATYPE_STRING, "status" );
1706 rtmp_body_append( rtmp_body, tmp_buffer,
1707 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1708 AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1711 tmp_buffer = amf_encode_object_variable( "code",
1712 AMF_DATATYPE_STRING, "NetConnection.Connect.Success" );
1713 rtmp_body_append( rtmp_body, tmp_buffer,
1714 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1715 AMF_DATATYPE_SIZE_STRING + strlen( "NetConnection.Connect.Success" ) );
1718 tmp_buffer = amf_encode_object_variable( "description",
1719 AMF_DATATYPE_STRING, "Connection succeeded." );
1720 rtmp_body_append( rtmp_body, tmp_buffer,
1721 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1722 AMF_DATATYPE_SIZE_STRING + strlen( "Connection succeeded." ) );
1725 tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1726 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1729 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1730 0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
1731 free( rtmp_body->body );
1737 static rtmp_packet_t *
1738 rtmp_encode_createStream_result( rtmp_control_thread_t *p_thread, double stream_client_id, double stream_server_id )
1740 rtmp_packet_t *rtmp_packet;
1741 rtmp_body_t *rtmp_body;
1742 uint8_t *tmp_buffer;
1744 /* Build createStream result */
1745 rtmp_body = rtmp_body_new( -1 );
1747 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "_result" );
1748 rtmp_body_append( rtmp_body, tmp_buffer,
1749 AMF_DATATYPE_SIZE_STRING + strlen( "_result" ) );
1752 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &stream_client_id );
1753 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1756 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1757 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1760 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &stream_server_id );
1761 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1764 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1765 0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
1766 free( rtmp_body->body );
1772 static rtmp_packet_t *
1773 rtmp_encode_ping_reset_stream( rtmp_control_thread_t *p_thread )
1775 rtmp_packet_t *rtmp_packet;
1776 rtmp_body_t *rtmp_body;
1777 uint8_t *tmp_buffer;
1779 /* Build ping reset stream */
1780 rtmp_body = rtmp_body_new( -1 );
1782 tmp_buffer = rtmp_encode_ping( RTMP_PING_RESET_STREAM, RTMP_SRC_DST_CONNECT_OBJECT2, 0, 0 );
1783 rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_RESET_STREAM );
1786 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1787 0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
1788 free( rtmp_body->body );
1794 static rtmp_packet_t *
1795 rtmp_encode_ping_clear_stream( rtmp_control_thread_t *p_thread, uint32_t src_dst )
1797 rtmp_packet_t *rtmp_packet;
1798 rtmp_body_t *rtmp_body;
1799 uint8_t *tmp_buffer;
1801 /* Build ping clear stream */
1802 rtmp_body = rtmp_body_new( -1 );
1804 tmp_buffer = rtmp_encode_ping( RTMP_PING_CLEAR_STREAM, src_dst, 0, 0 );
1805 rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_CLEAR_STREAM );
1808 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1809 0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
1810 free( rtmp_body->body );
1816 static rtmp_packet_t *
1817 rtmp_encode_NetStream_play_reset_onStatus( rtmp_control_thread_t *p_thread, char *psz_media )
1819 rtmp_packet_t *rtmp_packet;
1820 rtmp_body_t *rtmp_body;
1821 uint8_t *tmp_buffer;
1825 /* Build NetStream.play.reset onStatus */
1826 rtmp_body = rtmp_body_new( -1 );
1828 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onStatus" );
1829 rtmp_body_append( rtmp_body, tmp_buffer,
1830 AMF_DATATYPE_SIZE_STRING + strlen( "onStatus" ) );
1833 number = 1; /* TODO: review this number*/
1834 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1835 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1838 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1839 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1842 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1843 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1846 tmp_buffer = amf_encode_object_variable( "level",
1847 AMF_DATATYPE_STRING, "status" );
1848 rtmp_body_append( rtmp_body, tmp_buffer,
1849 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1850 AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1853 tmp_buffer = amf_encode_object_variable( "code",
1854 AMF_DATATYPE_STRING, "NetStream.Play.Reset" );
1855 rtmp_body_append( rtmp_body, tmp_buffer,
1856 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1857 AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Play.Reset" ) );
1860 description = (char *) malloc( strlen( "Playing and resetting ") + strlen( psz_media ) + strlen( "." ) + 1 );
1863 free( rtmp_body->body );
1867 sprintf( description, "Playing and resetting %s.", psz_media );
1868 tmp_buffer = amf_encode_object_variable( "description",
1869 AMF_DATATYPE_STRING, description );
1870 rtmp_body_append( rtmp_body, tmp_buffer,
1871 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1872 AMF_DATATYPE_SIZE_STRING + strlen( description ) );
1874 free( description );
1876 tmp_buffer = amf_encode_object_variable( "details",
1877 AMF_DATATYPE_STRING, psz_media );
1878 rtmp_body_append( rtmp_body, tmp_buffer,
1879 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "details" ) +
1880 AMF_DATATYPE_SIZE_STRING + strlen( psz_media ) );
1883 tmp_buffer = amf_encode_object_variable( "clientid",
1884 AMF_DATATYPE_NUMBER, &p_thread->stream_client_id );
1885 rtmp_body_append( rtmp_body, tmp_buffer,
1886 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "clientid" ) +
1887 AMF_DATATYPE_SIZE_NUMBER );
1890 tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1891 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1894 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_NOTIFY,
1895 0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
1896 free( rtmp_body->body );
1902 static rtmp_packet_t *
1903 rtmp_encode_NetStream_play_start_onStatus( rtmp_control_thread_t *p_thread, char *psz_media )
1905 rtmp_packet_t *rtmp_packet;
1906 rtmp_body_t *rtmp_body;
1907 uint8_t *tmp_buffer;
1911 /* Build NetStream.play.start onStatus */
1912 rtmp_body = rtmp_body_new( -1 );
1914 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onStatus" );
1915 rtmp_body_append( rtmp_body, tmp_buffer,
1916 AMF_DATATYPE_SIZE_STRING + strlen( "onStatus" ) );
1919 number = 1; /* TODO: review this number*/
1920 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1921 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1924 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1925 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1928 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1929 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1932 tmp_buffer = amf_encode_object_variable( "level",
1933 AMF_DATATYPE_STRING, "status" );
1934 rtmp_body_append( rtmp_body, tmp_buffer,
1935 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1936 AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1939 tmp_buffer = amf_encode_object_variable( "code",
1940 AMF_DATATYPE_STRING, "NetStream.Play.Start" );
1941 rtmp_body_append( rtmp_body, tmp_buffer,
1942 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1943 AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Play.Start" ) );
1946 description = (char *) malloc( strlen( "Started playing ") + strlen( psz_media ) + strlen( "." ) + 1 );
1949 free( rtmp_body->body );
1954 sprintf( description, "Started playing %s.", psz_media );
1955 tmp_buffer = amf_encode_object_variable( "description",
1956 AMF_DATATYPE_STRING, description );
1957 rtmp_body_append( rtmp_body, tmp_buffer,
1958 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1959 AMF_DATATYPE_SIZE_STRING + strlen( description ) );
1961 free( description );
1963 tmp_buffer = amf_encode_object_variable( "details",
1964 AMF_DATATYPE_STRING, psz_media );
1965 rtmp_body_append( rtmp_body, tmp_buffer,
1966 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "details" ) +
1967 AMF_DATATYPE_SIZE_STRING + strlen( psz_media ) );
1970 tmp_buffer = amf_encode_object_variable( "clientid",
1971 AMF_DATATYPE_NUMBER, &p_thread->stream_client_id );
1972 rtmp_body_append( rtmp_body, tmp_buffer,
1973 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "clientid" ) +
1974 AMF_DATATYPE_SIZE_NUMBER );
1977 tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1978 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1981 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_NOTIFY,
1982 0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
1983 free( rtmp_body->body );
1990 rtmp_encode_header_size( vlc_object_t *p_this, uint8_t header_size )
1992 if( header_size == 1 )
1993 return RTMP_HEADER_SIZE_1;
1994 else if( header_size == 4 )
1995 return RTMP_HEADER_SIZE_4;
1996 else if( header_size == 8 )
1997 return RTMP_HEADER_SIZE_8;
1998 else if( header_size == 12 )
1999 return RTMP_HEADER_SIZE_12;
2002 msg_Err( p_this, "invalid header size for encoding" );
2008 rtmp_decode_header_size( vlc_object_t *p_this, uint8_t header_size )
2010 if( header_size == RTMP_HEADER_SIZE_1 )
2012 else if( header_size == RTMP_HEADER_SIZE_4 )
2014 else if( header_size == RTMP_HEADER_SIZE_8 )
2016 else if( header_size == RTMP_HEADER_SIZE_12 )
2020 msg_Err( p_this, "invalid RTMP_HEADER_SIZE_XX " );
2026 rtmp_get_stream_index( uint8_t content_type )
2028 if( content_type == RTMP_CONTENT_TYPE_AUDIO_DATA )
2029 return RTMP_DEFAULT_STREAM_INDEX_AUDIO_DATA;
2030 else if( content_type == RTMP_CONTENT_TYPE_VIDEO_DATA )
2031 return RTMP_DEFAULT_STREAM_INDEX_VIDEO_DATA;
2032 else if( content_type == RTMP_CONTENT_TYPE_NOTIFY )
2033 return RTMP_DEFAULT_STREAM_INDEX_NOTIFY;
2038 /*****************************************************************************
2039 * Body handling implementation:
2040 ******************************************************************************/
2042 rtmp_body_new( int length_buffer )
2044 rtmp_body_t *rtmp_body;
2046 rtmp_body = (rtmp_body_t *) malloc( sizeof( rtmp_body_t ) );
2047 if( !rtmp_body ) return NULL;
2049 rtmp_body->length_body = 0;
2050 if( length_buffer < 0 )
2051 rtmp_body->length_buffer = RTMP_BODY_SIZE_ALLOC;
2053 rtmp_body->length_buffer = length_buffer;
2054 rtmp_body->body = (uint8_t *) malloc( rtmp_body->length_buffer * sizeof( uint8_t ) );
2055 if( !rtmp_body->body )
2064 rtmp_body_reset( rtmp_body_t *rtmp_body )
2066 rtmp_body->length_body = 0;
2070 rtmp_body_append( rtmp_body_t *rtmp_body, uint8_t *buffer, uint32_t length )
2072 if( rtmp_body->length_body + length > rtmp_body->length_buffer )
2075 rtmp_body->length_buffer = rtmp_body->length_body + length;
2076 tmp = realloc( rtmp_body->body,
2077 rtmp_body->length_buffer * sizeof( uint8_t ) );
2079 rtmp_body->body = tmp;
2082 memcpy( rtmp_body->body + rtmp_body->length_body, buffer, length );
2083 rtmp_body->length_body += length;
2086 /*****************************************************************************
2087 * RTMP ping implementation:
2088 ******************************************************************************/
2090 rtmp_encode_ping( uint16_t type, uint32_t src_dst, uint32_t third_arg, uint32_t fourth_arg )
2092 uint8_t *out = NULL;
2093 VLC_UNUSED(fourth_arg);
2095 if( type == RTMP_PING_CLEAR_STREAM )
2096 out = (uint8_t *) malloc( RTMP_PING_SIZE_CLEAR_STREAM * sizeof( uint8_t ) );
2097 else if( type == RTMP_PING_CLEAR_PLAYING_BUFFER )
2098 out = (uint8_t *) malloc( RTMP_PING_SIZE_CLEAR_PLAYING_BUFFER * sizeof( uint8_t ) );
2099 else if( type == RTMP_PING_BUFFER_TIME_CLIENT )
2101 out = (uint8_t *) malloc( RTMP_PING_SIZE_BUFFER_TIME_CLIENT * sizeof( uint8_t ) );
2102 if( !out ) goto error;
2103 third_arg = hton32( third_arg );
2104 memcpy( out + 6, &third_arg, sizeof( uint32_t ) );
2106 else if( type == RTMP_PING_RESET_STREAM )
2108 out = (uint8_t *) malloc( RTMP_PING_SIZE_RESET_STREAM * sizeof( uint8_t ) );
2110 /* else if( type == RTMP_PING_CLIENT_FROM_SERVER ) TODO: research this
2113 else if( type == RTMP_PING_PONG_FROM_CLIENT )
2118 out = (uint8_t *) malloc( RTMP_PING_SIZE_BUFFER_TIME_CLIENT * sizeof( uint8_t ) );
2119 if( !out ) goto error;
2120 out[6] = 0x0D; out[7] = 0x0E; out[8] = 0x0A; out[9] = 0x0D;
2123 if( !out ) goto error;
2125 type = hton16( type );
2126 memcpy( out, &type, sizeof( uint16_t ) );
2128 src_dst = hton32( src_dst );
2129 memcpy( out + 2, &src_dst, sizeof( uint32_t ) );
2137 /*****************************************************************************
2138 * AMF implementation:
2139 ******************************************************************************/
2141 amf_encode_element( uint8_t element, const void *value )
2145 if ( element == AMF_DATATYPE_NUMBER )
2147 uint64_t number = *(uint64_t *) value;
2149 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2150 if( !out ) return NULL;
2152 number = hton64( number );
2153 out[0] = AMF_DATATYPE_NUMBER;
2154 memcpy( out + 1, &number, sizeof( uint64_t ) );
2155 } else if ( element == AMF_DATATYPE_BOOLEAN )
2157 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_BOOLEAN * sizeof( uint8_t ) );
2158 if( !out ) return NULL;
2160 out[0] = AMF_DATATYPE_BOOLEAN;
2161 out[1] = *(uint8_t *) value;
2162 } else if ( element == AMF_DATATYPE_STRING )
2164 uint16_t length_psz, length_psz_cpy;
2166 length_psz = length_psz_cpy = strlen( (char *) value );
2167 out = (uint8_t *) malloc( ( AMF_DATATYPE_SIZE_STRING + length_psz ) * sizeof( uint8_t ) );
2168 if( !out ) return NULL;
2170 out[0] = AMF_DATATYPE_STRING;
2171 length_psz = hton16( length_psz );
2172 memcpy( out + 1, &length_psz, sizeof( uint16_t ) );
2173 memcpy( out + 3, value, length_psz_cpy );
2174 } else if ( element == AMF_DATATYPE_OBJECT )
2176 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_OBJECT * sizeof( uint8_t ) );
2177 if( !out ) return NULL;
2179 out[0] = AMF_DATATYPE_OBJECT;
2180 } else if ( element == AMF_DATATYPE_NULL )
2182 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NULL * sizeof( uint8_t ) );
2183 if( !out ) return NULL;
2185 out[0] = AMF_DATATYPE_NULL;
2186 } else if ( element == AMF_DATATYPE_MIXED_ARRAY )
2188 uint32_t highest_index = *(uint32_t *) value;
2190 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_MIXED_ARRAY * sizeof( uint8_t ) );
2191 if( !out ) return NULL;
2193 highest_index = hton32( highest_index );
2194 out[0] = AMF_DATATYPE_MIXED_ARRAY;
2195 memcpy( out + 1, &highest_index, sizeof( uint32_t ) );
2196 } else if ( element == AMF_DATATYPE_END_OF_OBJECT )
2198 out = (uint8_t *) calloc( AMF_DATATYPE_SIZE_END_OF_OBJECT, sizeof( uint8_t ) );
2200 out[AMF_DATATYPE_SIZE_END_OF_OBJECT - 1] = AMF_DATATYPE_END_OF_OBJECT;
2203 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2204 if( !out ) return NULL;
2206 out[0] = AMF_DATATYPE_NUMBER;
2207 out[1] = 0x0D; out[2] = 0x0E; out[3] = 0x0A; out[4] = 0x0D;
2208 out[5] = 0x0B; out[6] = 0x0E; out[7] = 0x0E; out[8] = 0x0F;
2215 amf_encode_object_variable( const char *key, uint8_t element, const void *value )
2217 uint8_t *out, *out_value;
2219 uint16_t length_psz, length_psz_cpy;
2221 length_psz = length_psz_cpy = strlen( key );
2223 if( element == AMF_DATATYPE_NUMBER )
2224 length_value = AMF_DATATYPE_SIZE_NUMBER;
2225 else if( element == AMF_DATATYPE_BOOLEAN )
2226 length_value = AMF_DATATYPE_SIZE_BOOLEAN;
2227 else if( element == AMF_DATATYPE_STRING )
2228 length_value = AMF_DATATYPE_SIZE_STRING + strlen( (char *) value );
2229 else if( element == AMF_DATATYPE_NULL )
2230 length_value = AMF_DATATYPE_SIZE_NULL;
2233 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2234 if( !out ) return NULL;
2236 out[0] = AMF_DATATYPE_NUMBER;
2237 out[1] = 0xD; out[2] = 0xE; out[3] = 0xA; out[4] = 0xD;
2238 out[5] = 0xB; out[6] = 0xE; out[7] = 0xE; out[8] = 0xF;
2243 out = (uint8_t *) malloc( ( AMF_DATATYPE_SIZE_OBJECT_VARIABLE + length_psz + length_value ) * sizeof( uint8_t ) );
2244 if( !out ) return NULL;
2246 length_psz = hton16( length_psz );
2247 memcpy( out, &length_psz, sizeof( uint16_t ) );
2248 memcpy( out + 2, key, length_psz_cpy );
2250 out_value = amf_encode_element( element, value );
2251 memcpy( out + 2 + length_psz_cpy, out_value, length_value );
2258 amf_decode_number( uint8_t **buffer )
2263 number = ntoh64( *(uint64_t *) *buffer );
2264 memcpy(&out, &number, sizeof( uint64_t ) );
2265 *buffer += sizeof( uint64_t );
2271 amf_decode_boolean( uint8_t **buffer )
2281 /* return value allocated dinamically */
2283 amf_decode_string( uint8_t **buffer )
2289 length = ntoh16( *(uint16_t *) *buffer );
2290 *buffer += sizeof( uint16_t );
2292 out = (char *) malloc( length + 1 ); /* '\0' terminated */
2293 if( !out ) return NULL;
2295 for(i = 0; i < length; i++)
2296 out[i] = (*buffer)[i];
2305 /* returns in each call next key, at end of object returns NULL */
2306 /* need to decode value of key after call */
2308 amf_decode_object( uint8_t **buffer )
2310 if( **buffer == 0x0 && *(*buffer + 1) == 0x00 && *(*buffer + 2) == 0x09)
2317 return amf_decode_string( buffer );
2320 /*****************************************************************************
2321 * FLV rebuilding implementation:
2322 ******************************************************************************/
2324 flv_rebuild( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
2326 uint32_t length_tag, timestamp;
2329 tmp = (uint8_t *) realloc( rtmp_packet->body->body,
2330 rtmp_packet->body->length_body +
2331 FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE );
2333 rtmp_packet->body->body = tmp;
2334 memmove( rtmp_packet->body->body + FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE,
2335 rtmp_packet->body->body, rtmp_packet->body->length_body );
2338 p_thread->flv_tag_previous_tag_size = hton32( p_thread->flv_tag_previous_tag_size );
2339 memcpy( rtmp_packet->body->body, &p_thread->flv_tag_previous_tag_size, sizeof( uint32_t ) );
2341 /* Fill backwards because of overlapping*/
2342 rtmp_packet->body->body[11] = 0x00;
2344 timestamp = hton32( rtmp_packet->timestamp );
2345 memcpy( rtmp_packet->body->body + 7, ×tamp, sizeof( uint32_t ) );
2347 length_tag = hton32( rtmp_packet->body->length_body );
2348 memcpy( rtmp_packet->body->body + 4, &length_tag, sizeof( uint32_t ) );
2350 rtmp_packet->body->body[4] = rtmp_packet->content_type;
2352 rtmp_packet->body->body[12] = 0x00;
2353 rtmp_packet->body->body[13] = 0x00;
2354 rtmp_packet->body->body[14] = 0x00;
2356 p_thread->flv_tag_previous_tag_size = rtmp_packet->body->length_body + FLV_TAG_SIZE;
2359 rtmp_packet->body->length_body += FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE;
2360 rtmp_packet->body->length_buffer = rtmp_packet->body->length_body;
2364 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 )
2368 data_audio = *packet_audio->body->body;
2370 if( ( data_audio & FLV_AUDIO_STEREO_MASK ) == FLV_AUDIO_STEREO_MONO )
2371 *stereo = FLV_AUDIO_STEREO_MONO;
2372 else if( ( data_audio & FLV_AUDIO_STEREO_MASK ) == FLV_AUDIO_STEREO_STEREO )
2373 *stereo = FLV_AUDIO_STEREO_STEREO;
2375 msg_Warn( p_thread, "unknown metadata audio stereo" );
2377 if( ( data_audio & FLV_AUDIO_SIZE_MASK ) == FLV_AUDIO_SIZE_8_BIT )
2378 *audiosamplesize = FLV_AUDIO_SIZE_8_BIT >> 1;
2379 else if( ( data_audio & FLV_AUDIO_SIZE_MASK ) == FLV_AUDIO_SIZE_16_BIT )
2380 *audiosamplesize = FLV_AUDIO_SIZE_16_BIT >> 1;
2382 msg_Warn( p_thread, "unknown metadata audio sample size" );
2384 if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_5_5_KHZ )
2385 *audiosamplerate = 5512;
2386 else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_11_KHZ )
2387 *audiosamplerate = 11025;
2388 else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_22_KHZ )
2389 *audiosamplerate = 22050;
2390 else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_44_KHZ )
2391 *audiosamplerate = 44100;
2393 msg_Warn( p_thread, "unknown metadata audio sample rate" );
2395 if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_UNCOMPRESSED )
2396 *audiocodecid = FLV_AUDIO_CODEC_ID_UNCOMPRESSED >> 4;
2397 else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_ADPCM )
2398 *audiocodecid = FLV_AUDIO_CODEC_ID_ADPCM >> 4;
2399 else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_MP3 )
2400 *audiocodecid = FLV_AUDIO_CODEC_ID_MP3 >> 4;
2401 else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_NELLYMOSER_8KHZ_MONO )
2402 *audiocodecid = FLV_AUDIO_CODEC_ID_NELLYMOSER_8KHZ_MONO >> 4;
2403 else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_NELLYMOSER )
2404 *audiocodecid = FLV_AUDIO_CODEC_ID_NELLYMOSER >> 4;
2406 msg_Warn( p_thread, "unknown metadata audio codec id" );
2410 flv_get_metadata_video( rtmp_control_thread_t *p_thread, rtmp_packet_t *packet_video, uint8_t *videocodecid, uint8_t *frametype )
2414 data_video = *packet_video->body->body;
2416 if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SORENSEN_H263 )
2417 *videocodecid = FLV_VIDEO_CODEC_ID_SORENSEN_H263;
2418 else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SCREEN_VIDEO )
2419 *videocodecid = FLV_VIDEO_CODEC_ID_SCREEN_VIDEO;
2420 else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_ON2_VP6 )
2421 *videocodecid = FLV_VIDEO_CODEC_ID_ON2_VP6;
2422 else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_ON2_VP6_ALPHA )
2423 *videocodecid = FLV_VIDEO_CODEC_ID_ON2_VP6_ALPHA;
2424 else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SCREEN_VIDEO_2 )
2425 *videocodecid = FLV_VIDEO_CODEC_ID_SCREEN_VIDEO_2;
2427 msg_Warn( p_thread, "unknown metadata video codec id" );
2429 if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_KEYFRAME )
2430 *frametype = FLV_VIDEO_FRAME_TYPE_KEYFRAME >> 4;
2431 else if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_INTER_FRAME )
2432 *frametype = FLV_VIDEO_FRAME_TYPE_INTER_FRAME >> 4;
2433 else if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_DISPOSABLE_INTER_FRAME )
2434 *frametype = FLV_VIDEO_FRAME_TYPE_DISPOSABLE_INTER_FRAME >> 4;
2436 msg_Warn( p_thread, "unknown metadata video frame type" );
2439 static rtmp_packet_t *
2440 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 )
2442 rtmp_packet_t *rtmp_packet;
2443 rtmp_body_t *rtmp_body;
2444 uint8_t *tmp_buffer;
2447 rtmp_body = rtmp_body_new( -1 );
2449 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onMetaData" );
2450 rtmp_body_append( rtmp_body, tmp_buffer,
2451 AMF_DATATYPE_SIZE_STRING + strlen( "onMetaData" ) );
2455 tmp_buffer = amf_encode_element( AMF_DATATYPE_MIXED_ARRAY, &number );
2456 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_MIXED_ARRAY );
2460 tmp_buffer = amf_encode_object_variable( "duration",
2461 AMF_DATATYPE_NUMBER, &number );
2462 rtmp_body_append( rtmp_body, tmp_buffer,
2463 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "duration" ) +
2464 AMF_DATATYPE_SIZE_NUMBER );
2467 tmp_buffer = amf_encode_object_variable( "stereo",
2468 AMF_DATATYPE_BOOLEAN, &stereo );
2469 rtmp_body_append( rtmp_body, tmp_buffer,
2470 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "stereo" ) +
2471 AMF_DATATYPE_SIZE_BOOLEAN );
2474 number = audiosamplesize;
2475 tmp_buffer = amf_encode_object_variable( "audiosamplesize",
2476 AMF_DATATYPE_NUMBER, &number );
2477 rtmp_body_append( rtmp_body, tmp_buffer,
2478 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiosamplesize" ) +
2479 AMF_DATATYPE_SIZE_NUMBER );
2482 number = audiosamplerate;
2483 tmp_buffer = amf_encode_object_variable( "audiosamplerate",
2484 AMF_DATATYPE_NUMBER, &number );
2485 rtmp_body_append( rtmp_body, tmp_buffer,
2486 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiosamplerate" ) +
2487 AMF_DATATYPE_SIZE_NUMBER );
2490 number = audiocodecid;
2491 tmp_buffer = amf_encode_object_variable( "audiocodecid",
2492 AMF_DATATYPE_NUMBER, &number );
2493 rtmp_body_append( rtmp_body, tmp_buffer,
2494 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiocodecid" ) +
2495 AMF_DATATYPE_SIZE_NUMBER );
2498 number = videocodecid;
2499 tmp_buffer = amf_encode_object_variable( "videocodecid",
2500 AMF_DATATYPE_NUMBER, &number );
2501 rtmp_body_append( rtmp_body, tmp_buffer,
2502 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "videocodecid" ) +
2503 AMF_DATATYPE_SIZE_NUMBER );
2506 tmp_buffer = amf_encode_element( AMF_DATATYPE_END_OF_OBJECT, NULL );
2507 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
2510 rtmp_packet = rtmp_new_packet( p_access->p_sys->p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
2511 0, RTMP_CONTENT_TYPE_NOTIFY, 0, rtmp_body );
2512 free( rtmp_body->body );
2519 flv_get_metadata( access_t *p_access )
2521 access_sys_t *p_sys = p_access->p_sys;
2522 rtmp_packet_t *flv_metadata_packet;
2525 flv_metadata_packet = flv_build_onMetaData( p_access, 0, p_sys->p_thread->metadata_stereo,
2526 p_sys->p_thread->metadata_samplesize, p_sys->p_thread->metadata_samplerate,
2527 p_sys->p_thread->metadata_audiocodecid, p_sys->p_thread->metadata_videocodecid );
2528 flv_rebuild( p_sys->p_thread, flv_metadata_packet );
2529 p_buffer = rtmp_new_block( p_sys->p_thread, flv_metadata_packet->body->body, flv_metadata_packet->body->length_buffer );
2531 free( flv_metadata_packet->body->body );
2532 free( flv_metadata_packet->body );
2533 free( flv_metadata_packet );
2539 flv_insert_header( access_t *p_access, block_t *first_packet )
2541 access_sys_t *p_sys = p_access->p_sys;
2542 int old_buffer_size;
2543 uint32_t tmp_number;
2545 old_buffer_size = first_packet->i_buffer;
2547 first_packet = block_Realloc( first_packet, 0, first_packet->i_buffer + FLV_HEADER_SIZE );
2549 memmove( first_packet->p_buffer + FLV_HEADER_SIZE,
2550 first_packet->p_buffer, old_buffer_size );
2552 memcpy( first_packet->p_buffer, FLV_HEADER_SIGNATURE, sizeof( FLV_HEADER_SIGNATURE ) );
2553 first_packet->p_buffer[3] = FLV_HEADER_VERSION;
2554 if( p_sys->p_thread->has_audio && p_sys->p_thread->has_video )
2555 first_packet->p_buffer[4] = FLV_HEADER_AUDIO | FLV_HEADER_VIDEO;
2556 else if( p_sys->p_thread->has_audio )
2557 first_packet->p_buffer[4] = FLV_HEADER_AUDIO;
2559 first_packet->p_buffer[4] = FLV_HEADER_VIDEO;
2560 tmp_number = hton32( FLV_HEADER_SIZE );
2561 memcpy( first_packet->p_buffer + 5, &tmp_number, sizeof( uint32_t ) );
2563 return first_packet;