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 ******************************************************************************/
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;
226 /*****************************************************************************
227 * static RTMP functions:
228 ******************************************************************************/
229 static void rtmp_handler_null ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
230 static void rtmp_handler_chunk_size ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
231 static void rtmp_handler_invoke ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
232 static void rtmp_handler_audio_data ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
233 static void rtmp_handler_video_data ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
234 static void rtmp_handler_notify ( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
236 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 );
237 static block_t *rtmp_new_block( rtmp_control_thread_t *p_thread, uint8_t *buffer, int32_t length_buffer );
239 static rtmp_packet_t *rtmp_encode_onBWDone( rtmp_control_thread_t *p_thread, double number );
240 static rtmp_packet_t *rtmp_encode_server_bw( rtmp_control_thread_t *p_thread, uint32_t number );
241 static rtmp_packet_t *rtmp_encode_NetConnection_connect_result( rtmp_control_thread_t *p_thread, double number );
242 static rtmp_packet_t *rtmp_encode_createStream_result( rtmp_control_thread_t *p_thread, double stream_client, double stream_server );
243 static rtmp_packet_t *rtmp_encode_ping_reset_stream( rtmp_control_thread_t *p_thread );
244 static rtmp_packet_t *rtmp_encode_ping_clear_stream( rtmp_control_thread_t *p_thread, uint32_t src_dst );
245 static rtmp_packet_t *rtmp_encode_NetStream_play_reset_onStatus( rtmp_control_thread_t *p_thread, char *psz_media );
246 static rtmp_packet_t *rtmp_encode_NetStream_play_start_onStatus( rtmp_control_thread_t *p_thread, char *psz_media );
247 static uint8_t rtmp_encode_header_size( vlc_object_t *p_this, uint8_t header_size );
248 static uint8_t rtmp_decode_header_size( vlc_object_t *p_this, uint8_t header_size );
249 static uint8_t rtmp_get_stream_index( uint8_t content_type );
251 static void rtmp_body_append( rtmp_body_t *rtmp_body, uint8_t *buffer, uint32_t length );
253 static uint8_t *rtmp_encode_ping( uint16_t type, uint32_t src_dst, uint32_t third_arg, uint32_t fourth_arg );
255 /*****************************************************************************
256 * static AMF functions:
257 ******************************************************************************/
258 static uint8_t *amf_encode_element( uint8_t element, const void *value );
259 static uint8_t *amf_encode_object_variable( const char *key, uint8_t element, const void *value );
260 static double amf_decode_number( uint8_t **buffer );
261 static int amf_decode_boolean( uint8_t **buffer );
262 static char *amf_decode_string( uint8_t **buffer );
263 static char *amf_decode_object( uint8_t **buffer );
265 /*****************************************************************************
266 * static FLV functions:
267 ******************************************************************************/
268 static void flv_rebuild( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet );
269 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 );
270 static void flv_get_metadata_video( rtmp_control_thread_t *p_thread, rtmp_packet_t *packet_video, uint8_t *videocodecid, uint8_t *frametype );
271 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 );
273 /*****************************************************************************
274 * RTMP implementation:
275 ******************************************************************************/
277 rtmp_handshake_passive( vlc_object_t *p_this, int fd )
279 uint8_t p_read[RTMP_HANDSHAKE_BODY_SIZE + 1];
280 uint8_t p_write[RTMP_HANDSHAKE_BODY_SIZE * 2 + 1];
284 /* Receive handshake */
285 i_ret = net_Read( p_this, fd, NULL, p_read, RTMP_HANDSHAKE_BODY_SIZE + 1, true );
286 if( i_ret != RTMP_HANDSHAKE_BODY_SIZE + 1 )
288 msg_Err( p_this, "failed to receive handshake" );
292 /* Check handshake */
293 if ( p_read[0] != RTMP_HANDSHAKE )
295 msg_Err( p_this, "first byte in handshake corrupt" );
299 /* Answer handshake */
300 p_write[0] = RTMP_HANDSHAKE;
301 memset( p_write + 1, 0, RTMP_HANDSHAKE_BODY_SIZE );
302 memcpy( p_write + 1 + RTMP_HANDSHAKE_BODY_SIZE, p_read + 1, RTMP_HANDSHAKE_BODY_SIZE );
305 i_ret = net_Write( p_this, fd, NULL, p_write, RTMP_HANDSHAKE_BODY_SIZE * 2 + 1 );
306 if( i_ret != RTMP_HANDSHAKE_BODY_SIZE * 2 + 1 )
308 msg_Err( p_this, "failed to send handshake" );
312 /* Receive acknowledge */
313 i_ret = net_Read( p_this, fd, NULL, p_read, RTMP_HANDSHAKE_BODY_SIZE, true );
314 if( i_ret != RTMP_HANDSHAKE_BODY_SIZE )
316 msg_Err( p_this, "failed to receive acknowledge" );
320 /* Check acknowledge */
321 for(i = 8; i < RTMP_HANDSHAKE_BODY_SIZE; i++ )
322 if( p_write[i + 1] != p_read[i] )
324 msg_Err( p_this, "body acknowledge received corrupt" );
332 rtmp_handshake_active( vlc_object_t *p_this, int fd )
334 uint8_t p_read[RTMP_HANDSHAKE_BODY_SIZE * 2 + 1];
335 uint8_t p_write[RTMP_HANDSHAKE_BODY_SIZE + 1];
339 p_write[0] = RTMP_HANDSHAKE;
340 for( i = 0; i < RTMP_HANDSHAKE_BODY_SIZE; i++ )
341 p_write[i + 1] = i & 0xFF;
344 i_ret = net_Write( p_this, fd, NULL, p_write, RTMP_HANDSHAKE_BODY_SIZE + 1 );
345 if( i_ret != RTMP_HANDSHAKE_BODY_SIZE + 1 )
347 msg_Err( p_this, "failed to send handshake" );
351 /* Receive handshake */
352 i_ret = net_Read( p_this, fd, NULL, p_read, RTMP_HANDSHAKE_BODY_SIZE * 2 + 1, true );
353 if( i_ret != RTMP_HANDSHAKE_BODY_SIZE * 2 + 1 )
355 msg_Err( p_this, "failed to receive handshake" );
359 /* Check handshake */
360 if( p_read[0] != RTMP_HANDSHAKE )
362 msg_Err( p_this, "first byte in handshake received corrupt" );
366 for(i = 8; i < RTMP_HANDSHAKE_BODY_SIZE; i++ )
367 if( p_write[i + 1] != p_read[i + 1 + RTMP_HANDSHAKE_BODY_SIZE] )
369 msg_Err( p_this, "body handshake received corrupt" );
373 /* Acknowledge handshake */
374 i_ret = net_Write( p_this, fd, NULL, p_read + 1, RTMP_HANDSHAKE_BODY_SIZE );
375 if( i_ret != RTMP_HANDSHAKE_BODY_SIZE )
377 msg_Err( p_this, "failed to acknowledge handshake" );
385 rtmp_connect_active( rtmp_control_thread_t *p_thread )
387 rtmp_packet_t *rtmp_packet;
388 rtmp_body_t *rtmp_body;
393 /* Build NetConnection.connect call */
394 rtmp_body = rtmp_body_new( -1 );
396 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "connect" );
397 rtmp_body_append( rtmp_body, tmp_buffer,
398 AMF_DATATYPE_SIZE_STRING + strlen( "connect" ) );
401 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
402 &AMF_CALL_NETCONNECTION_CONNECT );
403 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
406 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
407 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
410 tmp_buffer = amf_encode_object_variable( "app",
411 AMF_DATATYPE_STRING, p_thread->psz_application );
412 rtmp_body_append( rtmp_body, tmp_buffer,
413 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "app" ) +
414 AMF_DATATYPE_SIZE_STRING + strlen( p_thread->psz_application ) );
417 tmp_buffer = amf_encode_object_variable( "flashVer",
418 AMF_DATATYPE_STRING, "LNX 9,0,48,0" );
419 rtmp_body_append( rtmp_body, tmp_buffer,
420 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "flashVer" ) +
421 AMF_DATATYPE_SIZE_STRING + strlen( "LNX 9,0,48,0" ) );
424 tmp_buffer = amf_encode_object_variable( "swfUrl",
425 AMF_DATATYPE_STRING, "file:///mac.flv" );
426 rtmp_body_append( rtmp_body, tmp_buffer,
427 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "swfUrl" ) +
428 AMF_DATATYPE_SIZE_STRING + strlen( "file:///mac.flv" ) );
431 tmp_url = (char *) malloc( strlen( "rtmp://") + strlen( p_thread->url.psz_buffer ) + 1 );
432 sprintf( tmp_url, "rtmp://%s", p_thread->url.psz_buffer );
433 tmp_buffer = amf_encode_object_variable( "tcUrl",
434 AMF_DATATYPE_STRING, tmp_url );
435 rtmp_body_append( rtmp_body, tmp_buffer,
436 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "tcUrl" ) +
437 AMF_DATATYPE_SIZE_STRING + strlen( tmp_url ) );
441 tmp_buffer = amf_encode_object_variable( "fpad",
442 AMF_DATATYPE_BOOLEAN, &AMF_BOOLEAN_FALSE );
443 rtmp_body_append( rtmp_body, tmp_buffer,
444 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "fpad" ) +
445 AMF_DATATYPE_SIZE_BOOLEAN );
448 tmp_buffer = amf_encode_object_variable( "audioCodecs",
449 AMF_DATATYPE_NUMBER, &AMF_CALL_NETCONNECTION_CONNECT_AUDIOCODECS );
450 rtmp_body_append( rtmp_body, tmp_buffer,
451 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audioCodecs" ) +
452 AMF_DATATYPE_SIZE_NUMBER );
455 tmp_buffer = amf_encode_object_variable( "videoCodecs",
456 AMF_DATATYPE_NUMBER, &AMF_CALL_NETCONNECTION_CONNECT_VIDEOCODECS );
457 rtmp_body_append( rtmp_body, tmp_buffer,
458 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "videoCodecs" ) +
459 AMF_DATATYPE_SIZE_NUMBER );
462 tmp_buffer = amf_encode_object_variable( "videoFunction",
463 AMF_DATATYPE_NUMBER, &AMF_CALL_NETCONNECTION_CONNECT_VIDEOFUNCTION );
464 rtmp_body_append( rtmp_body, tmp_buffer,
465 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "videoFunction" ) +
466 AMF_DATATYPE_SIZE_NUMBER );
469 tmp_buffer = amf_encode_object_variable( "pageUrl",
470 AMF_DATATYPE_STRING, "file:///mac.html" );
471 rtmp_body_append( rtmp_body, tmp_buffer,
472 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "pageUrl" ) +
473 AMF_DATATYPE_SIZE_STRING + strlen( "file:///mac.html" ) );
476 tmp_buffer = amf_encode_object_variable( "objectEncoding",
477 AMF_DATATYPE_NUMBER, &AMF_CALL_NETCONNECTION_CONNECT_OBJECTENCODING );
478 rtmp_body_append( rtmp_body, tmp_buffer,
479 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "objectEncoding" ) +
480 AMF_DATATYPE_SIZE_NUMBER );
483 tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
484 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
487 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
488 0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
489 free( rtmp_body->body );
492 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
494 /* Call NetConnection.connect */
495 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
496 if( i_ret != rtmp_packet->length_encoded )
498 free( rtmp_packet->body->body );
499 free( rtmp_packet->body );
502 msg_Err( p_thread, "failed send call NetConnection.connect" );
505 free( rtmp_packet->body->body );
506 free( rtmp_packet->body );
510 /* Wait for NetConnection.connect result */
511 vlc_mutex_lock( &p_thread->lock );
512 vlc_cond_wait( &p_thread->wait, &p_thread->lock );
513 vlc_mutex_unlock( &p_thread->lock );
515 if( p_thread->result_connect )
517 msg_Err( p_thread, "failed call NetConnection.connect" );
521 /* Force control thread to stop if receive NetStream.play call and wait is not ready */
522 vlc_mutex_lock( &p_thread->lock );
524 /* Build NetStream.createStream call */
525 rtmp_body = rtmp_body_new( -1 );
527 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "createStream" );
528 rtmp_body_append( rtmp_body, tmp_buffer,
529 AMF_DATATYPE_SIZE_STRING + strlen( "createStream" ) );
532 p_thread->stream_client_id = RTMP_DEFAULT_STREAM_CLIENT_ID;
534 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
535 &AMF_CALL_STREAM_CLIENT_NUMBER );
536 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
539 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
540 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
543 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
544 0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
545 free( rtmp_body->body );
548 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
550 /* Call NetStream.createStream */
551 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
552 if( i_ret != rtmp_packet->length_encoded )
554 free( rtmp_packet->body->body );
555 free( rtmp_packet->body );
558 msg_Err( p_thread, "failed send call NetStream.createStream" );
561 free( rtmp_packet->body->body );
562 free( rtmp_packet->body );
565 /*TODO: read server stream number*/
566 /* Build ping packet */
567 rtmp_body = rtmp_body_new( -1 );
569 tmp_buffer = rtmp_encode_ping( RTMP_PING_BUFFER_TIME_CLIENT, RTMP_SRC_DST_CONNECT_OBJECT, RTMP_TIME_CLIENT_BUFFER, 0 );
570 rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_BUFFER_TIME_CLIENT );
573 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
574 0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
575 free( rtmp_body->body );
578 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
580 /* Send ping packet */
581 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
582 if( i_ret != rtmp_packet->length_encoded )
584 free( rtmp_packet->body->body );
585 free( rtmp_packet->body );
588 msg_Err( p_thread, "failed send ping BUFFER_TIME_CLIENT" );
591 free( rtmp_packet->body->body );
592 free( rtmp_packet->body );
596 /* Build NetStream.play call */
597 rtmp_body = rtmp_body_new( -1 );
599 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "play" );
600 rtmp_body_append( rtmp_body, tmp_buffer,
601 AMF_DATATYPE_SIZE_STRING + strlen( "play" ) );
604 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
605 &AMF_CALL_NETSTREAM_PLAY );
606 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
609 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
610 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
613 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, p_thread->psz_media );
614 rtmp_body_append( rtmp_body, tmp_buffer,
615 AMF_DATATYPE_SIZE_STRING + strlen( p_thread->psz_media ) );
618 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
619 0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
620 free( rtmp_body->body );
623 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
625 /* Call NetStream.play */
626 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
627 if( i_ret != rtmp_packet->length_encoded )
629 free( rtmp_packet->body->body );
630 free( rtmp_packet->body );
633 msg_Err( p_thread, "failed send call NetStream.play" );
636 free( rtmp_packet->body->body );
637 free( rtmp_packet->body );
641 /* Build ping packet */
642 rtmp_body = rtmp_body_new( -1 );
644 tmp_buffer = rtmp_encode_ping( RTMP_PING_BUFFER_TIME_CLIENT, RTMP_SRC_DST_CONNECT_OBJECT2, RTMP_TIME_CLIENT_BUFFER, 0 );
645 rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_BUFFER_TIME_CLIENT );
648 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
649 0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
650 free( rtmp_body->body );
653 tmp_buffer = rtmp_encode_packet( p_thread, rtmp_packet );
655 /* Send ping packet */
656 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
657 if( i_ret != rtmp_packet->length_encoded )
659 free( rtmp_packet->body->body );
660 free( rtmp_packet->body );
663 msg_Err( p_thread, "failed send ping BUFFER_TIME_CLIENT" );
666 free( rtmp_packet->body->body );
667 free( rtmp_packet->body );
671 /* Wait for NetStream.play.start result */
672 vlc_cond_wait( &p_thread->wait, &p_thread->lock );
673 vlc_mutex_unlock( &p_thread->lock );
675 if( p_thread->result_play )
677 msg_Err( p_thread, "failed call NetStream.play" );
681 /* Next packet is the beginning of flv stream */
682 msg_Dbg( p_thread, "next packet is the beginning of flv stream" );
688 rtmp_connect_passive( rtmp_control_thread_t *p_thread )
690 /* Force control thread to stop if receive NetStream.play call and wait is not ready */
691 vlc_mutex_lock( &p_thread->lock );
693 /* Wait for NetStream.play.start result */
694 vlc_cond_wait( &p_thread->wait, &p_thread->lock );
695 vlc_mutex_unlock( &p_thread->lock );
697 if( p_thread->result_play )
699 msg_Err( p_thread, "failed call NetStream.play" );
708 rtmp_seek( access_t *p_access, int64_t i_pos )
710 access_sys_t *p_sys = p_access->p_sys;
711 rtmp_packet_t *rtmp_packet;
712 rtmp_body_t *rtmp_body;
716 msg_Warn(p_access, "i_pos %lld", i_pos);
717 // Build NetStream.seek call //
718 rtmp_body = rtmp_body_new();
720 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "seek" );
721 rtmp_body_append( rtmp_body, tmp_buffer,
722 AMF_DATATYPE_SIZE_STRING + strlen( "seek" ) );
726 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
728 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
731 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
732 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
734 //TODO: convert i_pos to double and see if they are milliseconds
735 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
737 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
740 rtmp_packet = rtmp_new_packet( p_sys->p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
741 0, RTMP_DATATYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
742 free( rtmp_body->body );
745 tmp_buffer = rtmp_encode_packet( p_access, rtmp_packet );
747 // Call NetStream.seek //
748 i_ret = net_Write( p_access, p_sys->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
749 if( i_ret < rtmp_packet->length_encoded )
751 free( rtmp_packet->body->body );
752 free( rtmp_packet->body );
755 msg_Err( p_access, "failed call NetStream.seek" );
758 free( rtmp_packet->body->body );
759 free( rtmp_packet->body );
763 // Receive TODO: see what //
764 rtmp_packet = rtmp_read_net_packet( p_access, p_sys->fd );
765 free( rtmp_packet->body->body );
766 free( rtmp_packet->body );
774 rtmp_build_bytes_read( rtmp_control_thread_t *p_thread, uint32_t reply )
776 rtmp_packet_t *rtmp_packet;
777 rtmp_body_t *rtmp_body;
780 /* Build bytes read packet */
781 rtmp_body = rtmp_body_new( -1 );
783 tmp_buffer = (uint8_t *) malloc( sizeof( uint32_t ) * sizeof( uint8_t ) );
784 reply = hton32( reply );
785 memcpy( tmp_buffer, &reply, sizeof( uint32_t ) );
787 rtmp_body_append( rtmp_body, tmp_buffer, sizeof( uint32_t ) );
790 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
791 0, RTMP_CONTENT_TYPE_BYTES_READ, 0, rtmp_body );
792 free( rtmp_body->body );
799 rtmp_build_publish_start( rtmp_control_thread_t *p_thread )
801 rtmp_packet_t *rtmp_packet;
802 rtmp_body_t *rtmp_body;
805 /* Build publish start event */
806 rtmp_body = rtmp_body_new( -1 );
808 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onStatus" );
809 rtmp_body_append( rtmp_body, tmp_buffer,
810 AMF_DATATYPE_SIZE_STRING + strlen( "onStatus" ) );
813 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER,
814 &p_thread->stream_server_id );
815 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
818 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
819 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
822 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
823 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
826 tmp_buffer = amf_encode_object_variable( "level",
827 AMF_DATATYPE_STRING, "status" );
828 rtmp_body_append( rtmp_body, tmp_buffer,
829 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
830 AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
833 tmp_buffer = amf_encode_object_variable( "code",
834 AMF_DATATYPE_STRING, "NetStream.Publish.Start" );
835 rtmp_body_append( rtmp_body, tmp_buffer,
836 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
837 AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Publish.Start" ) );
840 tmp_buffer = amf_encode_object_variable( "description",
841 AMF_DATATYPE_STRING, "" );
842 rtmp_body_append( rtmp_body, tmp_buffer,
843 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
844 AMF_DATATYPE_SIZE_STRING + strlen( "" ) );
847 tmp_buffer = amf_encode_object_variable( "details",
848 AMF_DATATYPE_STRING, p_thread->psz_publish );
849 rtmp_body_append( rtmp_body, tmp_buffer,
850 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "details" ) +
851 AMF_DATATYPE_SIZE_STRING + strlen( p_thread->psz_publish ) );
854 tmp_buffer = amf_encode_object_variable( "clientid",
855 AMF_DATATYPE_NUMBER, &p_thread->stream_client_id );
856 rtmp_body_append( rtmp_body, tmp_buffer,
857 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "clientid" ) +
858 AMF_DATATYPE_SIZE_NUMBER );
861 tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
862 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
865 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
866 0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
867 free( rtmp_body->body );
874 rtmp_build_flv_over_rtmp( rtmp_control_thread_t *p_thread, block_t *p_buffer )
876 rtmp_packet_t *rtmp_packet;
878 if( p_thread->flv_length_body > 0 )
880 p_thread->flv_length_body -= p_buffer->i_buffer;
881 rtmp_body_append( p_thread->flv_body, p_buffer->p_buffer, p_buffer->i_buffer );
883 if( p_thread->flv_length_body > 0 )
889 p_thread->flv_content_type = *p_buffer->p_buffer;
891 p_buffer->p_buffer[0] = 0;
892 p_thread->flv_length_body = ntoh32( *(uint32_t *) (p_buffer->p_buffer) );
894 p_buffer->p_buffer[3] = 0;
895 p_thread->flv_timestamp = ntoh32( *(uint32_t *) (p_buffer->p_buffer + 3) );
898 if( p_thread->flv_length_body > p_buffer->i_buffer - FLV_TAG_SIZE - FLV_TAG_PREVIOUS_TAG_SIZE )
900 p_thread->flv_length_body -= p_buffer->i_buffer - FLV_TAG_SIZE - FLV_TAG_PREVIOUS_TAG_SIZE;
901 rtmp_body_append( p_thread->flv_body, p_buffer->p_buffer + FLV_TAG_SIZE, p_buffer->i_buffer - FLV_TAG_SIZE );
907 rtmp_body_append( p_thread->flv_body, p_buffer->p_buffer + FLV_TAG_SIZE, p_thread->flv_length_body );
910 rtmp_packet = rtmp_new_packet( p_thread, rtmp_get_stream_index( p_thread->flv_content_type ),
911 p_thread->flv_timestamp, p_thread->flv_content_type, RTMP_SRC_DST_DEFAULT, p_thread->flv_body );
912 p_thread->flv_length_body = 0;
913 rtmp_body_reset( p_thread->flv_body );
919 rtmp_read_net_packet( rtmp_control_thread_t *p_thread )
925 rtmp_packet_t *rtmp_packet;
931 i_ret = net_Read( p_thread, p_thread->fd, NULL, p_read, 1, true );
935 length_header = rtmp_decode_header_size( (vlc_object_t *) p_thread, p_read[0] & RTMP_HEADER_SIZE_MASK );
936 stream_index = p_read[0] & RTMP_HEADER_STREAM_INDEX_MASK;
938 i_ret = net_Read( p_thread, p_thread->fd, NULL, p_read + 1, length_header - 1, true );
939 if( i_ret != length_header - 1 )
942 /* Update timestamp if not is an interchunk packet */
943 if( length_header == 1 && p_thread->rtmp_headers_recv[stream_index].body == NULL )
945 p_thread->rtmp_headers_recv[stream_index].timestamp +=
946 p_thread->rtmp_headers_recv[stream_index].timestamp_relative;
949 /* Length 4 and 8 headers have relative timestamp */
950 if( length_header == 4 || length_header == 8 )
954 p_thread->rtmp_headers_recv[stream_index].timestamp_relative = ntoh32( *(uint32_t *) p_read );
955 p_thread->rtmp_headers_recv[stream_index].timestamp +=
956 p_thread->rtmp_headers_recv[stream_index].timestamp_relative;
959 if( length_header >= 8 )
963 p_thread->rtmp_headers_recv[stream_index].length_body = ntoh32( *(uint32_t *) (p_read + 3) );
964 p_thread->rtmp_headers_recv[stream_index].content_type = p_read[7];
967 /* Length 12 headers have absolute timestamp */
968 if( length_header >= 12 )
972 p_thread->rtmp_headers_recv[stream_index].timestamp = ntoh32( *(uint32_t *) p_read );
973 p_thread->rtmp_headers_recv[stream_index].src_dst = ntoh32( *(uint32_t *) (p_read + 8) );
976 if( p_thread->rtmp_headers_recv[stream_index].body == NULL )
978 p_thread->rtmp_headers_recv[stream_index].body =
979 rtmp_body_new( p_thread->rtmp_headers_recv[stream_index].length_body );
982 bytes_left = p_thread->rtmp_headers_recv[stream_index].body->length_buffer -
983 p_thread->rtmp_headers_recv[stream_index].body->length_body;
985 if( bytes_left > p_thread->chunk_size_recv )
986 bytes_left = p_thread->chunk_size_recv;
988 i_ret = net_Read( p_thread, p_thread->fd, NULL,
989 p_thread->rtmp_headers_recv[stream_index].body->body +
990 p_thread->rtmp_headers_recv[stream_index].body->length_body,
992 if( i_ret != bytes_left )
995 p_thread->rtmp_headers_recv[stream_index].body->length_body += bytes_left;
997 if( p_thread->rtmp_headers_recv[stream_index].length_body == p_thread->rtmp_headers_recv[stream_index].body->length_body )
999 rtmp_packet = (rtmp_packet_t *) malloc( sizeof( rtmp_packet_t ) );
1001 rtmp_packet->stream_index = stream_index;
1002 rtmp_packet->timestamp = p_thread->rtmp_headers_recv[stream_index].timestamp;
1003 rtmp_packet->timestamp_relative = p_thread->rtmp_headers_recv[stream_index].timestamp_relative;
1004 rtmp_packet->content_type = p_thread->rtmp_headers_recv[stream_index].content_type;
1005 rtmp_packet->src_dst = p_thread->rtmp_headers_recv[stream_index].src_dst;
1006 rtmp_packet->length_body = p_thread->rtmp_headers_recv[stream_index].length_body;
1007 rtmp_packet->body = p_thread->rtmp_headers_recv[stream_index].body;
1009 p_thread->rtmp_headers_recv[stream_index].body = NULL;
1016 msg_Err( p_thread, "rtmp_read_net_packet: net_Read error");
1022 rtmp_init_handler( rtmp_handler_t *rtmp_handler )
1024 rtmp_handler[RTMP_CONTENT_TYPE_CHUNK_SIZE] = rtmp_handler_chunk_size;
1025 rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_02] = rtmp_handler_null;
1026 rtmp_handler[RTMP_CONTENT_TYPE_BYTES_READ] = rtmp_handler_null;
1027 rtmp_handler[RTMP_CONTENT_TYPE_PING] = rtmp_handler_null;
1028 rtmp_handler[RTMP_CONTENT_TYPE_SERVER_BW] = rtmp_handler_null;
1029 rtmp_handler[RTMP_CONTENT_TYPE_CLIENT_BW] = rtmp_handler_null;
1030 rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_07] = rtmp_handler_null;
1031 rtmp_handler[RTMP_CONTENT_TYPE_AUDIO_DATA] = rtmp_handler_audio_data;
1032 rtmp_handler[RTMP_CONTENT_TYPE_VIDEO_DATA] = rtmp_handler_video_data;
1033 rtmp_handler[RTMP_CONTENT_TYPE_UNKNOWN_0A_0E] = rtmp_handler_null;
1034 rtmp_handler[RTMP_CONTENT_TYPE_FLEX_STREAM] = rtmp_handler_null;
1035 rtmp_handler[RTMP_CONTENT_TYPE_FLEX_SHARED_OBJECT] = rtmp_handler_null;
1036 rtmp_handler[RTMP_CONTENT_TYPE_MESSAGE] = rtmp_handler_null;
1037 rtmp_handler[RTMP_CONTENT_TYPE_NOTIFY] = rtmp_handler_notify;
1038 rtmp_handler[RTMP_CONTENT_TYPE_SHARED_OBJECT] = rtmp_handler_null;
1039 rtmp_handler[RTMP_CONTENT_TYPE_INVOKE] = rtmp_handler_invoke;
1043 rtmp_handler_null( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1045 free( rtmp_packet->body->body );
1046 free( rtmp_packet->body );
1047 free( rtmp_packet );
1051 rtmp_handler_chunk_size( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1053 p_thread->chunk_size_recv = ntoh32( *(uint32_t *) (rtmp_packet->body->body) );
1055 free( rtmp_packet->body->body );
1056 free( rtmp_packet->body );
1057 free( rtmp_packet );
1061 rtmp_handler_audio_data( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1065 if( !p_thread->has_audio )
1067 p_thread->has_audio = 1;
1069 flv_get_metadata_audio( p_thread, rtmp_packet,
1070 &p_thread->metadata_stereo, &p_thread->metadata_samplesize,
1071 &p_thread->metadata_samplerate, &p_thread->metadata_audiocodecid );
1074 flv_rebuild( p_thread, rtmp_packet );
1075 p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
1076 block_FifoPut( p_thread->p_fifo_input, p_buffer );
1078 free( rtmp_packet->body->body );
1079 free( rtmp_packet->body );
1080 free( rtmp_packet );
1084 rtmp_handler_video_data( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1088 if( !p_thread->has_video )
1090 p_thread->has_video = 1;
1092 flv_get_metadata_video( p_thread, rtmp_packet,
1093 &p_thread->metadata_videocodecid, &p_thread->metadata_frametype );
1096 flv_rebuild( p_thread, rtmp_packet );
1097 p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
1098 block_FifoPut( p_thread->p_fifo_input, p_buffer );
1100 free( rtmp_packet->body->body );
1101 free( rtmp_packet->body );
1102 free( rtmp_packet );
1106 rtmp_handler_notify( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1110 p_thread->metadata_received = 1;
1112 flv_rebuild( p_thread, rtmp_packet );
1113 p_buffer = rtmp_new_block( p_thread, rtmp_packet->body->body, rtmp_packet->body->length_body );
1114 block_FifoPut( p_thread->p_fifo_input, p_buffer );
1116 free( rtmp_packet->body->body );
1117 free( rtmp_packet->body );
1118 free( rtmp_packet );
1122 rtmp_handler_invoke( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1124 rtmp_packet_t *tmp_rtmp_packet;
1125 uint8_t *i, *end, *tmp_buffer;
1127 char *string, *string2;
1130 i = rtmp_packet->body->body;
1131 end = rtmp_packet->body->body + rtmp_packet->body->length_body;
1133 i++; /* Pass over AMF_DATATYPE_STRING */
1134 string = amf_decode_string( &i );
1136 i++; /* Pass over AMF_DATATYPE_NUMBER */
1137 number = amf_decode_number( &i );
1139 msg_Dbg( p_thread, "%s %.1f", string, number );
1141 if( strcmp( "connect", string ) == 0 )
1143 /* Connection bandwith */
1144 tmp_rtmp_packet = rtmp_encode_onBWDone( p_thread, AMF_CALL_ONBWDONE );
1146 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1148 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1149 if( i_ret != tmp_rtmp_packet->length_encoded )
1151 free( tmp_rtmp_packet->body->body );
1152 free( tmp_rtmp_packet->body );
1153 free( tmp_rtmp_packet );
1155 msg_Err( p_thread, "failed send connection bandwith" );
1157 free( tmp_rtmp_packet->body->body );
1158 free( tmp_rtmp_packet->body );
1159 free( tmp_rtmp_packet );
1162 /* Server bandwith */
1163 tmp_rtmp_packet = rtmp_encode_server_bw( p_thread, RTMP_SERVER_BW );
1165 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1167 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1168 if( i_ret != tmp_rtmp_packet->length_encoded )
1170 free( tmp_rtmp_packet->body->body );
1171 free( tmp_rtmp_packet->body );
1172 free( tmp_rtmp_packet );
1174 msg_Err( p_thread, "failed send server bandwith" );
1176 free( tmp_rtmp_packet->body->body );
1177 free( tmp_rtmp_packet->body );
1178 free( tmp_rtmp_packet );
1182 tmp_rtmp_packet = rtmp_encode_ping_clear_stream( p_thread, RTMP_SRC_DST_CONNECT_OBJECT );
1184 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1186 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1187 if( i_ret != tmp_rtmp_packet->length_encoded )
1189 free( tmp_rtmp_packet->body->body );
1190 free( tmp_rtmp_packet->body );
1191 free( tmp_rtmp_packet );
1193 msg_Err( p_thread, "failed send clear stream" );
1195 free( tmp_rtmp_packet->body->body );
1196 free( tmp_rtmp_packet->body );
1197 free( tmp_rtmp_packet );
1200 /* Reply NetConnection.connect */
1201 tmp_rtmp_packet = rtmp_encode_NetConnection_connect_result( p_thread, number );
1203 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1205 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1206 if( i_ret != tmp_rtmp_packet->length_encoded )
1208 free( tmp_rtmp_packet->body->body );
1209 free( tmp_rtmp_packet->body );
1210 free( tmp_rtmp_packet );
1212 msg_Err( p_thread, "failed send reply NetConnection.connect" );
1214 free( tmp_rtmp_packet->body->body );
1215 free( tmp_rtmp_packet->body );
1216 free( tmp_rtmp_packet );
1219 else if( strcmp( "createStream", string ) == 0 )
1221 p_thread->stream_client_id = number;
1222 p_thread->stream_server_id = RTMP_DEFAULT_STREAM_SERVER_ID;
1224 /* Reply createStream */
1225 tmp_rtmp_packet = rtmp_encode_createStream_result( p_thread, p_thread->stream_client_id, p_thread->stream_server_id );
1227 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1229 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1230 if( i_ret != tmp_rtmp_packet->length_encoded )
1232 free( tmp_rtmp_packet->body->body );
1233 free( tmp_rtmp_packet->body );
1234 free( tmp_rtmp_packet );
1236 msg_Err( p_thread, "failed send reply createStream" );
1238 free( tmp_rtmp_packet->body->body );
1239 free( tmp_rtmp_packet->body );
1240 free( tmp_rtmp_packet );
1244 tmp_rtmp_packet = rtmp_encode_ping_reset_stream( p_thread );
1246 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1248 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1249 if( i_ret != tmp_rtmp_packet->length_encoded )
1251 free( tmp_rtmp_packet->body->body );
1252 free( tmp_rtmp_packet->body );
1253 free( tmp_rtmp_packet );
1255 msg_Err( p_thread, "failed send reset stream" );
1257 free( tmp_rtmp_packet->body->body );
1258 free( tmp_rtmp_packet->body );
1259 free( tmp_rtmp_packet );
1263 tmp_rtmp_packet = rtmp_encode_ping_clear_stream( p_thread, RTMP_SRC_DST_CONNECT_OBJECT2 );
1265 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1267 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1268 if( i_ret != tmp_rtmp_packet->length_encoded )
1270 free( tmp_rtmp_packet->body->body );
1271 free( tmp_rtmp_packet->body );
1272 free( tmp_rtmp_packet );
1274 msg_Err( p_thread, "failed send clear stream" );
1276 free( tmp_rtmp_packet->body->body );
1277 free( tmp_rtmp_packet->body );
1278 free( tmp_rtmp_packet );
1281 else if( strcmp( "publish", string ) == 0 )
1284 msg_Dbg( p_thread, "null" );
1287 string2 = amf_decode_string( &i );
1288 msg_Dbg( p_thread, "string: %s", string2 );
1290 p_thread->psz_publish = strdup( string2 );
1294 else if( strcmp( "play", string ) == 0 )
1297 msg_Dbg( p_thread, "null" );
1300 string2 = amf_decode_string( &i );
1301 msg_Dbg( p_thread, "string: %s", string2 );
1303 /* Reply NetStream.play.reset */
1304 tmp_rtmp_packet = rtmp_encode_NetStream_play_reset_onStatus( p_thread, string2 );
1306 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1308 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1309 if( i_ret != tmp_rtmp_packet->length_encoded )
1311 free( tmp_rtmp_packet->body->body );
1312 free( tmp_rtmp_packet->body );
1313 free( tmp_rtmp_packet );
1315 msg_Err( p_thread, "failed send reply NetStream.play.reset" );
1317 free( tmp_rtmp_packet->body->body );
1318 free( tmp_rtmp_packet->body );
1319 free( tmp_rtmp_packet );
1322 /* Reply NetStream.play.start */
1323 tmp_rtmp_packet = rtmp_encode_NetStream_play_start_onStatus( p_thread, string2 );
1325 tmp_buffer = rtmp_encode_packet( p_thread, tmp_rtmp_packet );
1327 i_ret = net_Write( p_thread, p_thread->fd, NULL, tmp_buffer, tmp_rtmp_packet->length_encoded );
1328 if( i_ret != tmp_rtmp_packet->length_encoded )
1330 free( tmp_rtmp_packet->body->body );
1331 free( tmp_rtmp_packet->body );
1332 free( tmp_rtmp_packet );
1334 msg_Err( p_thread, "failed send reply NetStream.play.start" );
1336 free( tmp_rtmp_packet->body->body );
1337 free( tmp_rtmp_packet->body );
1338 free( tmp_rtmp_packet );
1343 p_thread->result_play = 0;
1345 vlc_mutex_lock( &p_thread->lock );
1346 vlc_cond_signal( &p_thread->wait );
1347 vlc_mutex_unlock( &p_thread->lock );
1354 if( *i == AMF_DATATYPE_NUMBER )
1357 msg_Dbg( p_thread, "number: %le", amf_decode_number( &i ) );
1359 else if( *i == AMF_DATATYPE_BOOLEAN )
1362 msg_Dbg( p_thread, "boolean: %s", amf_decode_boolean( &i ) ? "true" : "false" );
1364 else if( *i == AMF_DATATYPE_STRING )
1367 string = amf_decode_string( &i );
1368 msg_Dbg( p_thread, "string: %s", string );
1371 else if( *i == AMF_DATATYPE_OBJECT )
1374 msg_Dbg( p_thread, "object" );
1375 while( ( string = amf_decode_object( &i ) ) != NULL )
1377 if( *i == AMF_DATATYPE_NUMBER )
1380 msg_Dbg( p_thread, "key: %s value: %le", string, amf_decode_number( &i ) );
1382 else if( *i == AMF_DATATYPE_BOOLEAN )
1385 msg_Dbg( p_thread, "key: %s value: %s", string, amf_decode_boolean( &i ) ? "true" : "false" );
1387 else if( *i == AMF_DATATYPE_STRING )
1390 string2 = amf_decode_string( &i );
1391 msg_Dbg( p_thread, "key: %s value: %s", string, string2 );
1392 if( strcmp( "code", string ) == 0 )
1394 if( strcmp( "NetConnection.Connect.Success", string2 ) == 0 )
1396 p_thread->result_connect = 0;
1398 vlc_mutex_lock( &p_thread->lock );
1399 vlc_cond_signal( &p_thread->wait );
1400 vlc_mutex_unlock( &p_thread->lock );
1402 else if( strcmp( "NetConnection.Connect.InvalidApp", string2 ) == 0 )
1404 p_thread->b_die = 1;
1406 vlc_mutex_lock( &p_thread->lock );
1407 vlc_cond_signal( &p_thread->wait );
1408 vlc_mutex_unlock( &p_thread->lock );
1410 else if( strcmp( "NetStream.Play.Start", string2 ) == 0 )
1412 p_thread->result_play = 0;
1414 vlc_mutex_lock( &p_thread->lock );
1415 vlc_cond_signal( &p_thread->wait );
1416 vlc_mutex_unlock( &p_thread->lock );
1418 else if( strcmp( "NetStream.Play.Stop", string2 ) == 0 )
1420 p_thread->result_stop = 1;
1422 block_FifoWake( p_thread->p_fifo_input );
1427 else if( *i == AMF_DATATYPE_NULL )
1430 msg_Dbg( p_thread, "key: %s value: Null", string );
1432 else if( *i == AMF_DATATYPE_UNDEFINED )
1435 msg_Dbg( p_thread, "key: %s value: undefined (Null)", string );
1440 msg_Warn( p_thread, "key: %s value: undefined AMF type", string );
1444 msg_Dbg( p_thread, "end of object" );
1446 else if( *i == AMF_DATATYPE_NULL)
1449 msg_Dbg( p_thread, "null" );
1451 else if( *i == AMF_DATATYPE_UNDEFINED )
1454 msg_Dbg( p_thread, "undefined (null)" );
1459 msg_Warn( p_thread, "undefined AMF type" );
1463 free( rtmp_packet->body->body );
1464 free( rtmp_packet->body );
1465 free( rtmp_packet );
1468 /* length header calculated automatically based on last packet in the same channel */
1469 /* timestamps passed are always absolute */
1470 static rtmp_packet_t *
1471 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 )
1473 int interchunk_headers;
1474 rtmp_packet_t *rtmp_packet;
1476 rtmp_packet = (rtmp_packet_t *) malloc( sizeof( rtmp_packet_t ) );
1477 interchunk_headers = body->length_body / p_thread->chunk_size_send;
1478 if( body->length_body % p_thread->chunk_size_send == 0 )
1479 interchunk_headers--;
1481 if( src_dst != p_thread->rtmp_headers_send[stream_index].src_dst )
1483 p_thread->rtmp_headers_send[stream_index].timestamp = timestamp;
1484 p_thread->rtmp_headers_send[stream_index].length_body = body->length_body;
1485 p_thread->rtmp_headers_send[stream_index].content_type = content_type;
1486 p_thread->rtmp_headers_send[stream_index].src_dst = src_dst;
1488 rtmp_packet->length_header = 12;
1490 else if( content_type != p_thread->rtmp_headers_send[stream_index].content_type
1491 || body->length_body != p_thread->rtmp_headers_send[stream_index].length_body )
1493 p_thread->rtmp_headers_send[stream_index].timestamp_relative =
1494 timestamp - p_thread->rtmp_headers_send[stream_index].timestamp;
1495 p_thread->rtmp_headers_send[stream_index].timestamp = timestamp;
1496 p_thread->rtmp_headers_send[stream_index].length_body = body->length_body;
1497 p_thread->rtmp_headers_send[stream_index].content_type = content_type;
1499 rtmp_packet->length_header = 8;
1501 else if( timestamp != p_thread->rtmp_headers_send[stream_index].timestamp )
1503 p_thread->rtmp_headers_send[stream_index].timestamp_relative =
1504 timestamp - p_thread->rtmp_headers_send[stream_index].timestamp;
1505 p_thread->rtmp_headers_send[stream_index].timestamp = timestamp;
1507 rtmp_packet->length_header = 4;
1511 rtmp_packet->length_header = 1;
1513 /*TODO: puede que no haga falta guardar el timestamp relative */
1514 rtmp_packet->stream_index = stream_index;
1515 if( rtmp_packet->length_header == 12 )
1517 rtmp_packet->timestamp = timestamp;
1518 rtmp_packet->timestamp_relative = 0;
1522 rtmp_packet->timestamp = timestamp;
1523 rtmp_packet->timestamp_relative = p_thread->rtmp_headers_send[stream_index].timestamp_relative;
1525 rtmp_packet->length_encoded = rtmp_packet->length_header + body->length_body + interchunk_headers;
1526 rtmp_packet->length_body = body->length_body;
1527 rtmp_packet->content_type = content_type;
1528 rtmp_packet->src_dst = src_dst;
1530 rtmp_packet->body = (rtmp_body_t *) malloc( sizeof( rtmp_body_t ) );
1532 rtmp_packet->body->length_body = body->length_body;
1533 rtmp_packet->body->length_buffer = body->length_body;
1534 rtmp_packet->body->body = (uint8_t *) malloc( rtmp_packet->body->length_buffer * sizeof( uint8_t ) );
1535 memcpy( rtmp_packet->body->body, body->body, rtmp_packet->body->length_body );
1541 rtmp_new_block( rtmp_control_thread_t *p_thread, uint8_t *buffer, int32_t length_buffer )
1544 /* DOWN: p_thread->p_empty_blocks->i_depth */
1545 while ( block_FifoCount( p_thread->p_empty_blocks ) > MAX_EMPTY_BLOCKS )
1547 p_buffer = block_FifoGet( p_thread->p_empty_blocks );
1548 block_Release( p_buffer );
1550 /* DOWN: p_thread->p_empty_blocks->i_depth */
1551 if( block_FifoCount( p_thread->p_empty_blocks ) == 0 )
1553 p_buffer = block_New( p_thread, length_buffer );
1557 p_buffer = block_FifoGet( p_thread->p_empty_blocks );
1558 p_buffer = block_Realloc( p_buffer, 0, length_buffer );
1561 p_buffer->i_buffer = length_buffer;
1563 memcpy( p_buffer->p_buffer, buffer, p_buffer->i_buffer );
1568 /* call sequence for each packet rtmp_new_packet -> rtmp_encode_packet -> send */
1569 /* no parallelism allowed because of optimization in header length */
1571 rtmp_encode_packet( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
1574 int interchunk_headers;
1575 uint32_t timestamp, length_body, src_dst;
1578 out = (uint8_t *) malloc( rtmp_packet->length_encoded * sizeof( uint8_t ) );
1579 interchunk_headers = rtmp_packet->body->length_body / p_thread->chunk_size_send;
1580 if( rtmp_packet->body->length_body % p_thread->chunk_size_send == 0 )
1581 interchunk_headers--;
1583 if( rtmp_packet->length_header == 12 )
1585 /* Timestamp absolute */
1586 timestamp = hton32( rtmp_packet->timestamp );
1587 memcpy( out, ×tamp, sizeof( uint32_t ) );
1589 src_dst = hton32( rtmp_packet->src_dst );
1590 memcpy( out + 8, &src_dst, sizeof( uint32_t ) );
1593 if( rtmp_packet->length_header >= 8 )
1595 /* Length without inter chunk headers */
1596 length_body = hton32( rtmp_packet->body->length_body );
1597 memcpy( out + 3, &length_body, sizeof( uint32_t ) );
1599 out[7] = rtmp_packet->content_type;
1601 if( rtmp_packet->length_header >= 4 && rtmp_packet->length_header != 12 )
1603 /* Timestamp relative */
1604 timestamp = hton32( rtmp_packet->timestamp_relative );
1605 memcpy( out, ×tamp, sizeof( uint32_t ) );
1608 out[0] = rtmp_encode_header_size( (vlc_object_t *) p_thread, rtmp_packet->length_header ) + rtmp_packet->stream_index;
1610 /* Insert inter chunk headers */
1611 for(i = 0, j = 0; i < rtmp_packet->body->length_body + interchunk_headers; i++, j++)
1613 if( j % p_thread->chunk_size_send == 0 && j != 0 )
1614 out[rtmp_packet->length_header + i++] = RTMP_HEADER_SIZE_1 + rtmp_packet->stream_index;
1615 out[rtmp_packet->length_header + i] = rtmp_packet->body->body[j];
1621 static rtmp_packet_t *
1622 rtmp_encode_onBWDone( rtmp_control_thread_t *p_thread, double number )
1624 rtmp_packet_t *rtmp_packet;
1625 rtmp_body_t *rtmp_body;
1626 uint8_t *tmp_buffer;
1628 /* Build onBWDone */
1629 rtmp_body = rtmp_body_new( -1 );
1631 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onBWDone" );
1632 rtmp_body_append( rtmp_body, tmp_buffer,
1633 AMF_DATATYPE_SIZE_STRING + strlen( "onBWDone" ) );
1636 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1637 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1640 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1641 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1644 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1645 0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_CONNECT_OBJECT, rtmp_body );
1646 free( rtmp_body->body );
1652 static rtmp_packet_t *
1653 rtmp_encode_server_bw( rtmp_control_thread_t *p_thread, uint32_t number )
1655 rtmp_packet_t *rtmp_packet;
1656 rtmp_body_t *rtmp_body;
1658 /* Build server bw */
1659 rtmp_body = rtmp_body_new( -1 );
1661 rtmp_body_append( rtmp_body, (uint8_t *) &number, sizeof( uint32_t ) );
1663 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1664 0, RTMP_CONTENT_TYPE_SERVER_BW, RTMP_SRC_DST_CONNECT_OBJECT, rtmp_body );
1665 free( rtmp_body->body );
1671 static rtmp_packet_t *
1672 rtmp_encode_NetConnection_connect_result( rtmp_control_thread_t *p_thread, double number )
1674 rtmp_packet_t *rtmp_packet;
1675 rtmp_body_t *rtmp_body;
1676 uint8_t *tmp_buffer;
1678 /* Build NetConnection.connect result */
1679 rtmp_body = rtmp_body_new( -1 );
1681 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "_result" );
1682 rtmp_body_append( rtmp_body, tmp_buffer,
1683 AMF_DATATYPE_SIZE_STRING + strlen( "_result" ) );
1686 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1687 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1690 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1691 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1694 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1695 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1698 tmp_buffer = amf_encode_object_variable( "level",
1699 AMF_DATATYPE_STRING, "status" );
1700 rtmp_body_append( rtmp_body, tmp_buffer,
1701 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1702 AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1705 tmp_buffer = amf_encode_object_variable( "code",
1706 AMF_DATATYPE_STRING, "NetConnection.Connect.Success" );
1707 rtmp_body_append( rtmp_body, tmp_buffer,
1708 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1709 AMF_DATATYPE_SIZE_STRING + strlen( "NetConnection.Connect.Success" ) );
1712 tmp_buffer = amf_encode_object_variable( "description",
1713 AMF_DATATYPE_STRING, "Connection succeeded." );
1714 rtmp_body_append( rtmp_body, tmp_buffer,
1715 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1716 AMF_DATATYPE_SIZE_STRING + strlen( "Connection succeeded." ) );
1719 tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1720 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1723 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1724 0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
1725 free( rtmp_body->body );
1731 static rtmp_packet_t *
1732 rtmp_encode_createStream_result( rtmp_control_thread_t *p_thread, double stream_client_id, double stream_server_id )
1734 rtmp_packet_t *rtmp_packet;
1735 rtmp_body_t *rtmp_body;
1736 uint8_t *tmp_buffer;
1738 /* Build createStream result */
1739 rtmp_body = rtmp_body_new( -1 );
1741 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "_result" );
1742 rtmp_body_append( rtmp_body, tmp_buffer,
1743 AMF_DATATYPE_SIZE_STRING + strlen( "_result" ) );
1746 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &stream_client_id );
1747 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1750 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1751 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1754 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &stream_server_id );
1755 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1758 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
1759 0, RTMP_CONTENT_TYPE_INVOKE, 0, rtmp_body );
1760 free( rtmp_body->body );
1766 static rtmp_packet_t *
1767 rtmp_encode_ping_reset_stream( rtmp_control_thread_t *p_thread )
1769 rtmp_packet_t *rtmp_packet;
1770 rtmp_body_t *rtmp_body;
1771 uint8_t *tmp_buffer;
1773 /* Build ping reset stream */
1774 rtmp_body = rtmp_body_new( -1 );
1776 tmp_buffer = rtmp_encode_ping( RTMP_PING_RESET_STREAM, RTMP_SRC_DST_CONNECT_OBJECT2, 0, 0 );
1777 rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_RESET_STREAM );
1780 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1781 0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
1782 free( rtmp_body->body );
1788 static rtmp_packet_t *
1789 rtmp_encode_ping_clear_stream( rtmp_control_thread_t *p_thread, uint32_t src_dst )
1791 rtmp_packet_t *rtmp_packet;
1792 rtmp_body_t *rtmp_body;
1793 uint8_t *tmp_buffer;
1795 /* Build ping clear stream */
1796 rtmp_body = rtmp_body_new( -1 );
1798 tmp_buffer = rtmp_encode_ping( RTMP_PING_CLEAR_STREAM, src_dst, 0, 0 );
1799 rtmp_body_append( rtmp_body, tmp_buffer, RTMP_PING_SIZE_CLEAR_STREAM );
1802 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_CONTROL,
1803 0, RTMP_CONTENT_TYPE_PING, 0, rtmp_body );
1804 free( rtmp_body->body );
1810 static rtmp_packet_t *
1811 rtmp_encode_NetStream_play_reset_onStatus( rtmp_control_thread_t *p_thread, char *psz_media )
1813 rtmp_packet_t *rtmp_packet;
1814 rtmp_body_t *rtmp_body;
1815 uint8_t *tmp_buffer;
1819 /* Build NetStream.play.reset onStatus */
1820 rtmp_body = rtmp_body_new( -1 );
1822 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onStatus" );
1823 rtmp_body_append( rtmp_body, tmp_buffer,
1824 AMF_DATATYPE_SIZE_STRING + strlen( "onStatus" ) );
1827 number = 1; /* TODO: review this number*/
1828 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1829 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1832 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1833 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1836 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1837 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1840 tmp_buffer = amf_encode_object_variable( "level",
1841 AMF_DATATYPE_STRING, "status" );
1842 rtmp_body_append( rtmp_body, tmp_buffer,
1843 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1844 AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1847 tmp_buffer = amf_encode_object_variable( "code",
1848 AMF_DATATYPE_STRING, "NetStream.Play.Reset" );
1849 rtmp_body_append( rtmp_body, tmp_buffer,
1850 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1851 AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Play.Reset" ) );
1854 description = (char *) malloc( strlen( "Playing and resetting ") + strlen( psz_media ) + strlen( "." ) + 1 );
1855 sprintf( description, "Playing and resetting %s.", psz_media );
1856 tmp_buffer = amf_encode_object_variable( "description",
1857 AMF_DATATYPE_STRING, description );
1858 rtmp_body_append( rtmp_body, tmp_buffer,
1859 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1860 AMF_DATATYPE_SIZE_STRING + strlen( description ) );
1862 free( description );
1864 tmp_buffer = amf_encode_object_variable( "details",
1865 AMF_DATATYPE_STRING, psz_media );
1866 rtmp_body_append( rtmp_body, tmp_buffer,
1867 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "details" ) +
1868 AMF_DATATYPE_SIZE_STRING + strlen( psz_media ) );
1871 tmp_buffer = amf_encode_object_variable( "clientid",
1872 AMF_DATATYPE_NUMBER, &p_thread->stream_client_id );
1873 rtmp_body_append( rtmp_body, tmp_buffer,
1874 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "clientid" ) +
1875 AMF_DATATYPE_SIZE_NUMBER );
1878 tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1879 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1882 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_NOTIFY,
1883 0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
1884 free( rtmp_body->body );
1890 static rtmp_packet_t *
1891 rtmp_encode_NetStream_play_start_onStatus( rtmp_control_thread_t *p_thread, char *psz_media )
1893 rtmp_packet_t *rtmp_packet;
1894 rtmp_body_t *rtmp_body;
1895 uint8_t *tmp_buffer;
1899 /* Build NetStream.play.start onStatus */
1900 rtmp_body = rtmp_body_new( -1 );
1902 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onStatus" );
1903 rtmp_body_append( rtmp_body, tmp_buffer,
1904 AMF_DATATYPE_SIZE_STRING + strlen( "onStatus" ) );
1907 number = 1; /* TODO: review this number*/
1908 tmp_buffer = amf_encode_element( AMF_DATATYPE_NUMBER, &number );
1909 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NUMBER );
1912 tmp_buffer = amf_encode_element( AMF_DATATYPE_NULL, NULL );
1913 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_NULL );
1916 tmp_buffer = amf_encode_element( AMF_DATATYPE_OBJECT, NULL );
1917 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_OBJECT );
1920 tmp_buffer = amf_encode_object_variable( "level",
1921 AMF_DATATYPE_STRING, "status" );
1922 rtmp_body_append( rtmp_body, tmp_buffer,
1923 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "level" ) +
1924 AMF_DATATYPE_SIZE_STRING + strlen( "status" ) );
1927 tmp_buffer = amf_encode_object_variable( "code",
1928 AMF_DATATYPE_STRING, "NetStream.Play.Start" );
1929 rtmp_body_append( rtmp_body, tmp_buffer,
1930 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "code" ) +
1931 AMF_DATATYPE_SIZE_STRING + strlen( "NetStream.Play.Start" ) );
1934 description = (char *) malloc( strlen( "Started playing ") + strlen( psz_media ) + strlen( "." ) + 1 );
1935 sprintf( description, "Started playing %s.", psz_media );
1936 tmp_buffer = amf_encode_object_variable( "description",
1937 AMF_DATATYPE_STRING, description );
1938 rtmp_body_append( rtmp_body, tmp_buffer,
1939 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "description" ) +
1940 AMF_DATATYPE_SIZE_STRING + strlen( description ) );
1942 free( description );
1944 tmp_buffer = amf_encode_object_variable( "details",
1945 AMF_DATATYPE_STRING, psz_media );
1946 rtmp_body_append( rtmp_body, tmp_buffer,
1947 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "details" ) +
1948 AMF_DATATYPE_SIZE_STRING + strlen( psz_media ) );
1951 tmp_buffer = amf_encode_object_variable( "clientid",
1952 AMF_DATATYPE_NUMBER, &p_thread->stream_client_id );
1953 rtmp_body_append( rtmp_body, tmp_buffer,
1954 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "clientid" ) +
1955 AMF_DATATYPE_SIZE_NUMBER );
1958 tmp_buffer = amf_encode_element ( AMF_DATATYPE_END_OF_OBJECT, NULL );
1959 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
1962 rtmp_packet = rtmp_new_packet( p_thread, RTMP_DEFAULT_STREAM_INDEX_NOTIFY,
1963 0, RTMP_CONTENT_TYPE_INVOKE, RTMP_SRC_DST_DEFAULT, rtmp_body );
1964 free( rtmp_body->body );
1971 rtmp_encode_header_size( vlc_object_t *p_this, uint8_t header_size )
1973 if( header_size == 1 )
1974 return RTMP_HEADER_SIZE_1;
1975 else if( header_size == 4 )
1976 return RTMP_HEADER_SIZE_4;
1977 else if( header_size == 8 )
1978 return RTMP_HEADER_SIZE_8;
1979 else if( header_size == 12 )
1980 return RTMP_HEADER_SIZE_12;
1983 msg_Err( p_this, "invalid header size for encoding" );
1989 rtmp_decode_header_size( vlc_object_t *p_this, uint8_t header_size )
1991 if( header_size == RTMP_HEADER_SIZE_1 )
1993 else if( header_size == RTMP_HEADER_SIZE_4 )
1995 else if( header_size == RTMP_HEADER_SIZE_8 )
1997 else if( header_size == RTMP_HEADER_SIZE_12 )
2001 msg_Err( p_this, "invalid RTMP_HEADER_SIZE_XX " );
2007 rtmp_get_stream_index( uint8_t content_type )
2009 if( content_type == RTMP_CONTENT_TYPE_AUDIO_DATA )
2010 return RTMP_DEFAULT_STREAM_INDEX_AUDIO_DATA;
2011 else if( content_type == RTMP_CONTENT_TYPE_VIDEO_DATA )
2012 return RTMP_DEFAULT_STREAM_INDEX_VIDEO_DATA;
2013 else if( content_type == RTMP_CONTENT_TYPE_NOTIFY )
2014 return RTMP_DEFAULT_STREAM_INDEX_NOTIFY;
2019 /*****************************************************************************
2020 * Body handling implementation:
2021 ******************************************************************************/
2023 rtmp_body_new( int length_buffer )
2025 rtmp_body_t *rtmp_body;
2027 rtmp_body = (rtmp_body_t *) malloc( sizeof( rtmp_body_t ) );
2029 rtmp_body->length_body = 0;
2030 if( length_buffer < 0 )
2031 rtmp_body->length_buffer = RTMP_BODY_SIZE_ALLOC;
2033 rtmp_body->length_buffer = length_buffer;
2034 rtmp_body->body = (uint8_t *) malloc( rtmp_body->length_buffer * sizeof( uint8_t ) );
2040 rtmp_body_reset( rtmp_body_t *rtmp_body )
2042 rtmp_body->length_body = 0;
2046 rtmp_body_append( rtmp_body_t *rtmp_body, uint8_t *buffer, uint32_t length )
2048 if( rtmp_body->length_body + length > rtmp_body->length_buffer )
2050 rtmp_body->length_buffer = rtmp_body->length_body + length;
2051 rtmp_body->body = (uint8_t *) realloc( rtmp_body->body, rtmp_body->length_buffer * sizeof( uint8_t ) );
2054 memcpy( rtmp_body->body + rtmp_body->length_body, buffer, length );
2055 rtmp_body->length_body += length;
2058 /*****************************************************************************
2059 * RTMP ping implementation:
2060 ******************************************************************************/
2062 rtmp_encode_ping( uint16_t type, uint32_t src_dst, uint32_t third_arg, uint32_t fourth_arg )
2066 if( type == RTMP_PING_CLEAR_STREAM )
2067 out = (uint8_t *) malloc( RTMP_PING_SIZE_CLEAR_STREAM * sizeof( uint8_t ) );
2068 else if( type == RTMP_PING_CLEAR_PLAYING_BUFFER )
2069 out = (uint8_t *) malloc( RTMP_PING_SIZE_CLEAR_PLAYING_BUFFER * sizeof( uint8_t ) );
2070 else if( type == RTMP_PING_BUFFER_TIME_CLIENT )
2072 out = (uint8_t *) malloc( RTMP_PING_SIZE_BUFFER_TIME_CLIENT * sizeof( uint8_t ) );
2074 third_arg = hton32( third_arg );
2075 memcpy( out + 6, &third_arg, sizeof( uint32_t ) );
2077 else if( type == RTMP_PING_RESET_STREAM )
2079 out = (uint8_t *) malloc( RTMP_PING_SIZE_RESET_STREAM * sizeof( uint8_t ) );
2081 /* else if( type == RTMP_PING_CLIENT_FROM_SERVER ) TODO: research this
2084 else if( type == RTMP_PING_PONG_FROM_CLIENT )
2089 out = (uint8_t *) malloc( RTMP_PING_SIZE_BUFFER_TIME_CLIENT * sizeof( uint8_t ) );
2091 out[6] = 0x0D; out[7] = 0x0E; out[8] = 0x0A; out[9] = 0x0D;
2094 type = hton16( type );
2095 memcpy( out, &type, sizeof( uint16_t ) );
2097 src_dst = hton32( src_dst );
2098 memcpy( out + 2, &src_dst, sizeof( uint32_t ) );
2103 /*****************************************************************************
2104 * AMF implementation:
2105 ******************************************************************************/
2107 amf_encode_element( uint8_t element, const void *value )
2111 if ( element == AMF_DATATYPE_NUMBER )
2113 uint64_t number = *(uint64_t *) value;
2115 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2117 number = hton64( number );
2118 out[0] = AMF_DATATYPE_NUMBER;
2119 memcpy( out + 1, &number, sizeof( uint64_t ) );
2120 } else if ( element == AMF_DATATYPE_BOOLEAN )
2122 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_BOOLEAN * sizeof( uint8_t ) );
2124 out[0] = AMF_DATATYPE_BOOLEAN;
2125 out[1] = *(uint8_t *) value;
2126 } else if ( element == AMF_DATATYPE_STRING )
2128 uint16_t length_psz, length_psz_cpy;
2130 length_psz = length_psz_cpy = strlen( (char *) value );
2131 out = (uint8_t *) malloc( ( AMF_DATATYPE_SIZE_STRING + length_psz ) * sizeof( uint8_t ) );
2133 out[0] = AMF_DATATYPE_STRING;
2134 length_psz = hton16( length_psz );
2135 memcpy( out + 1, &length_psz, sizeof( uint16_t ) );
2136 memcpy( out + 3, value, length_psz_cpy );
2137 } else if ( element == AMF_DATATYPE_OBJECT )
2139 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_OBJECT * sizeof( uint8_t ) );
2141 out[0] = AMF_DATATYPE_OBJECT;
2142 } else if ( element == AMF_DATATYPE_NULL )
2144 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NULL * sizeof( uint8_t ) );
2146 out[0] = AMF_DATATYPE_NULL;
2147 } else if ( element == AMF_DATATYPE_MIXED_ARRAY )
2149 uint32_t highest_index = *(uint32_t *) value;
2151 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_MIXED_ARRAY * sizeof( uint8_t ) );
2153 highest_index = hton32( highest_index );
2154 out[0] = AMF_DATATYPE_MIXED_ARRAY;
2155 memcpy( out + 1, &highest_index, sizeof( uint32_t ) );
2156 } else if ( element == AMF_DATATYPE_END_OF_OBJECT )
2158 out = (uint8_t *) calloc( AMF_DATATYPE_SIZE_END_OF_OBJECT, sizeof( uint8_t ) );
2160 out[AMF_DATATYPE_SIZE_END_OF_OBJECT - 1] = AMF_DATATYPE_END_OF_OBJECT;
2163 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2165 out[0] = AMF_DATATYPE_NUMBER;
2166 out[1] = 0x0D; out[2] = 0x0E; out[3] = 0x0A; out[4] = 0x0D;
2167 out[5] = 0x0B; out[6] = 0x0E; out[7] = 0x0E; out[8] = 0x0F;
2174 amf_encode_object_variable( const char *key, uint8_t element, const void *value )
2176 uint8_t *out, *out_value;
2178 uint16_t length_psz, length_psz_cpy;
2180 length_psz = length_psz_cpy = strlen( key );
2182 if( element == AMF_DATATYPE_NUMBER )
2183 length_value = AMF_DATATYPE_SIZE_NUMBER;
2184 else if( element == AMF_DATATYPE_BOOLEAN )
2185 length_value = AMF_DATATYPE_SIZE_BOOLEAN;
2186 else if( element == AMF_DATATYPE_STRING )
2187 length_value = AMF_DATATYPE_SIZE_STRING + strlen( (char *) value );
2188 else if( element == AMF_DATATYPE_NULL )
2189 length_value = AMF_DATATYPE_SIZE_NULL;
2192 out = (uint8_t *) malloc( AMF_DATATYPE_SIZE_NUMBER * sizeof( uint8_t ) );
2194 out[0] = AMF_DATATYPE_NUMBER;
2195 out[1] = 0xD; out[2] = 0xE; out[3] = 0xA; out[4] = 0xD;
2196 out[5] = 0xB; out[6] = 0xE; out[7] = 0xE; out[8] = 0xF;
2201 out = (uint8_t *) malloc( ( AMF_DATATYPE_SIZE_OBJECT_VARIABLE + length_psz + length_value ) * sizeof( uint8_t ) );
2203 length_psz = hton16( length_psz );
2204 memcpy( out, &length_psz, sizeof( uint16_t ) );
2205 memcpy( out + 2, key, length_psz_cpy );
2207 out_value = amf_encode_element( element, value );
2208 memcpy( out + 2 + length_psz_cpy, out_value, length_value );
2215 amf_decode_number( uint8_t **buffer )
2220 number = ntoh64( *(uint64_t *) *buffer );
2221 memcpy(&out, &number, sizeof( uint64_t ) );
2222 *buffer += sizeof( uint64_t );
2228 amf_decode_boolean( uint8_t **buffer )
2238 /* return value allocated dinamically */
2240 amf_decode_string( uint8_t **buffer )
2246 length = ntoh16( *(uint16_t *) *buffer );
2247 *buffer += sizeof( uint16_t );
2249 out = (char *) malloc( length + 1 ); /* '\0' terminated */
2251 for(i = 0; i < length; i++)
2252 out[i] = (*buffer)[i];
2261 /* returns in each call next key, at end of object returns NULL */
2262 /* need to decode value of key after call */
2264 amf_decode_object( uint8_t **buffer )
2266 if( **buffer == 0x0 && *(*buffer + 1) == 0x00 && *(*buffer + 2) == 0x09)
2273 return amf_decode_string( buffer );
2276 /*****************************************************************************
2277 * FLV rebuilding implementation:
2278 ******************************************************************************/
2280 flv_rebuild( rtmp_control_thread_t *p_thread, rtmp_packet_t *rtmp_packet )
2282 uint32_t length_tag, timestamp;
2284 rtmp_packet->body->body = (uint8_t *) realloc( rtmp_packet->body->body,
2285 rtmp_packet->body->length_body + FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE );
2286 memmove( rtmp_packet->body->body + FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE,
2287 rtmp_packet->body->body, rtmp_packet->body->length_body );
2290 p_thread->flv_tag_previous_tag_size = hton32( p_thread->flv_tag_previous_tag_size );
2291 memcpy( rtmp_packet->body->body, &p_thread->flv_tag_previous_tag_size, sizeof( uint32_t ) );
2293 /* Fill backwards because of overlapping*/
2294 rtmp_packet->body->body[11] = 0x00;
2296 timestamp = hton32( rtmp_packet->timestamp );
2297 memcpy( rtmp_packet->body->body + 7, ×tamp, sizeof( uint32_t ) );
2299 length_tag = hton32( rtmp_packet->body->length_body );
2300 memcpy( rtmp_packet->body->body + 4, &length_tag, sizeof( uint32_t ) );
2302 rtmp_packet->body->body[4] = rtmp_packet->content_type;
2304 rtmp_packet->body->body[12] = 0x00;
2305 rtmp_packet->body->body[13] = 0x00;
2306 rtmp_packet->body->body[14] = 0x00;
2308 p_thread->flv_tag_previous_tag_size = rtmp_packet->body->length_body + FLV_TAG_SIZE;
2311 rtmp_packet->body->length_body += FLV_TAG_PREVIOUS_TAG_SIZE + FLV_TAG_SIZE;
2312 rtmp_packet->body->length_buffer = rtmp_packet->body->length_body;
2316 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 )
2320 data_audio = *packet_audio->body->body;
2322 if( ( data_audio & FLV_AUDIO_STEREO_MASK ) == FLV_AUDIO_STEREO_MONO )
2323 *stereo = FLV_AUDIO_STEREO_MONO;
2324 else if( ( data_audio & FLV_AUDIO_STEREO_MASK ) == FLV_AUDIO_STEREO_STEREO )
2325 *stereo = FLV_AUDIO_STEREO_STEREO;
2327 msg_Warn( p_thread, "unknown metadata audio stereo" );
2329 if( ( data_audio & FLV_AUDIO_SIZE_MASK ) == FLV_AUDIO_SIZE_8_BIT )
2330 *audiosamplesize = FLV_AUDIO_SIZE_8_BIT >> 1;
2331 else if( ( data_audio & FLV_AUDIO_SIZE_MASK ) == FLV_AUDIO_SIZE_16_BIT )
2332 *audiosamplesize = FLV_AUDIO_SIZE_16_BIT >> 1;
2334 msg_Warn( p_thread, "unknown metadata audio sample size" );
2336 if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_5_5_KHZ )
2337 *audiosamplerate = 5512;
2338 else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_11_KHZ )
2339 *audiosamplerate = 11025;
2340 else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_22_KHZ )
2341 *audiosamplerate = 22050;
2342 else if( ( data_audio & FLV_AUDIO_RATE_MASK ) == FLV_AUDIO_RATE_44_KHZ )
2343 *audiosamplerate = 44100;
2345 msg_Warn( p_thread, "unknown metadata audio sample rate" );
2347 if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_UNCOMPRESSED )
2348 *audiocodecid = FLV_AUDIO_CODEC_ID_UNCOMPRESSED >> 4;
2349 else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_ADPCM )
2350 *audiocodecid = FLV_AUDIO_CODEC_ID_ADPCM >> 4;
2351 else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_MP3 )
2352 *audiocodecid = FLV_AUDIO_CODEC_ID_MP3 >> 4;
2353 else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_NELLYMOSER_8KHZ_MONO )
2354 *audiocodecid = FLV_AUDIO_CODEC_ID_NELLYMOSER_8KHZ_MONO >> 4;
2355 else if( ( data_audio & FLV_AUDIO_CODEC_ID_MASK ) == FLV_AUDIO_CODEC_ID_NELLYMOSER )
2356 *audiocodecid = FLV_AUDIO_CODEC_ID_NELLYMOSER >> 4;
2358 msg_Warn( p_thread, "unknown metadata audio codec id" );
2362 flv_get_metadata_video( rtmp_control_thread_t *p_thread, rtmp_packet_t *packet_video, uint8_t *videocodecid, uint8_t *frametype )
2366 data_video = *packet_video->body->body;
2368 if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SORENSEN_H263 )
2369 *videocodecid = FLV_VIDEO_CODEC_ID_SORENSEN_H263;
2370 else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SCREEN_VIDEO )
2371 *videocodecid = FLV_VIDEO_CODEC_ID_SCREEN_VIDEO;
2372 else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_ON2_VP6 )
2373 *videocodecid = FLV_VIDEO_CODEC_ID_ON2_VP6;
2374 else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_ON2_VP6_ALPHA )
2375 *videocodecid = FLV_VIDEO_CODEC_ID_ON2_VP6_ALPHA;
2376 else if( ( data_video & FLV_VIDEO_CODEC_ID_MASK ) == FLV_VIDEO_CODEC_ID_SCREEN_VIDEO_2 )
2377 *videocodecid = FLV_VIDEO_CODEC_ID_SCREEN_VIDEO_2;
2379 msg_Warn( p_thread, "unknown metadata video codec id" );
2381 if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_KEYFRAME )
2382 *frametype = FLV_VIDEO_FRAME_TYPE_KEYFRAME >> 4;
2383 else if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_INTER_FRAME )
2384 *frametype = FLV_VIDEO_FRAME_TYPE_INTER_FRAME >> 4;
2385 else if( ( data_video & FLV_VIDEO_FRAME_TYPE_MASK ) == FLV_VIDEO_FRAME_TYPE_DISPOSABLE_INTER_FRAME )
2386 *frametype = FLV_VIDEO_FRAME_TYPE_DISPOSABLE_INTER_FRAME >> 4;
2388 msg_Warn( p_thread, "unknown metadata video frame type" );
2391 static rtmp_packet_t *
2392 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 )
2394 rtmp_packet_t *rtmp_packet;
2395 rtmp_body_t *rtmp_body;
2396 uint8_t *tmp_buffer;
2399 rtmp_body = rtmp_body_new( -1 );
2401 tmp_buffer = amf_encode_element( AMF_DATATYPE_STRING, "onMetaData" );
2402 rtmp_body_append( rtmp_body, tmp_buffer,
2403 AMF_DATATYPE_SIZE_STRING + strlen( "onMetaData" ) );
2407 tmp_buffer = amf_encode_element( AMF_DATATYPE_MIXED_ARRAY, &number );
2408 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_MIXED_ARRAY );
2412 tmp_buffer = amf_encode_object_variable( "duration",
2413 AMF_DATATYPE_NUMBER, &number );
2414 rtmp_body_append( rtmp_body, tmp_buffer,
2415 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "duration" ) +
2416 AMF_DATATYPE_SIZE_NUMBER );
2419 tmp_buffer = amf_encode_object_variable( "stereo",
2420 AMF_DATATYPE_BOOLEAN, &stereo );
2421 rtmp_body_append( rtmp_body, tmp_buffer,
2422 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "stereo" ) +
2423 AMF_DATATYPE_SIZE_BOOLEAN );
2426 number = audiosamplesize;
2427 tmp_buffer = amf_encode_object_variable( "audiosamplesize",
2428 AMF_DATATYPE_NUMBER, &number );
2429 rtmp_body_append( rtmp_body, tmp_buffer,
2430 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiosamplesize" ) +
2431 AMF_DATATYPE_SIZE_NUMBER );
2434 number = audiosamplerate;
2435 tmp_buffer = amf_encode_object_variable( "audiosamplerate",
2436 AMF_DATATYPE_NUMBER, &number );
2437 rtmp_body_append( rtmp_body, tmp_buffer,
2438 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiosamplerate" ) +
2439 AMF_DATATYPE_SIZE_NUMBER );
2442 number = audiocodecid;
2443 tmp_buffer = amf_encode_object_variable( "audiocodecid",
2444 AMF_DATATYPE_NUMBER, &number );
2445 rtmp_body_append( rtmp_body, tmp_buffer,
2446 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "audiocodecid" ) +
2447 AMF_DATATYPE_SIZE_NUMBER );
2450 number = videocodecid;
2451 tmp_buffer = amf_encode_object_variable( "videocodecid",
2452 AMF_DATATYPE_NUMBER, &number );
2453 rtmp_body_append( rtmp_body, tmp_buffer,
2454 AMF_DATATYPE_SIZE_OBJECT_VARIABLE + strlen( "videocodecid" ) +
2455 AMF_DATATYPE_SIZE_NUMBER );
2458 tmp_buffer = amf_encode_element( AMF_DATATYPE_END_OF_OBJECT, NULL );
2459 rtmp_body_append( rtmp_body, tmp_buffer, AMF_DATATYPE_SIZE_END_OF_OBJECT );
2462 rtmp_packet = rtmp_new_packet( p_access->p_sys->p_thread, RTMP_DEFAULT_STREAM_INDEX_INVOKE,
2463 0, RTMP_CONTENT_TYPE_NOTIFY, 0, rtmp_body );
2464 free( rtmp_body->body );
2471 flv_get_metadata( access_t *p_access )
2473 access_sys_t *p_sys = p_access->p_sys;
2474 rtmp_packet_t *flv_metadata_packet;
2477 flv_metadata_packet = flv_build_onMetaData( p_access, 0, p_sys->p_thread->metadata_stereo,
2478 p_sys->p_thread->metadata_samplesize, p_sys->p_thread->metadata_samplerate,
2479 p_sys->p_thread->metadata_audiocodecid, p_sys->p_thread->metadata_videocodecid );
2480 flv_rebuild( p_sys->p_thread, flv_metadata_packet );
2481 p_buffer = rtmp_new_block( p_sys->p_thread, flv_metadata_packet->body->body, flv_metadata_packet->body->length_buffer );
2483 free( flv_metadata_packet->body->body );
2484 free( flv_metadata_packet->body );
2485 free( flv_metadata_packet );
2491 flv_insert_header( access_t *p_access, block_t *first_packet )
2493 access_sys_t *p_sys = p_access->p_sys;
2494 int old_buffer_size;
2495 uint32_t tmp_number;
2497 old_buffer_size = first_packet->i_buffer;
2499 first_packet = block_Realloc( first_packet, 0, first_packet->i_buffer + FLV_HEADER_SIZE );
2501 memmove( first_packet->p_buffer + FLV_HEADER_SIZE,
2502 first_packet->p_buffer, old_buffer_size );
2504 memcpy( first_packet->p_buffer, FLV_HEADER_SIGNATURE, sizeof( FLV_HEADER_SIGNATURE ) );
2505 first_packet->p_buffer[3] = FLV_HEADER_VERSION;
2506 if( p_sys->p_thread->has_audio && p_sys->p_thread->has_video )
2507 first_packet->p_buffer[4] = FLV_HEADER_AUDIO | FLV_HEADER_VIDEO;
2508 else if( p_sys->p_thread->has_audio )
2509 first_packet->p_buffer[4] = FLV_HEADER_AUDIO;
2511 first_packet->p_buffer[4] = FLV_HEADER_VIDEO;
2512 tmp_number = hton32( FLV_HEADER_SIZE );
2513 memcpy( first_packet->p_buffer + 5, &tmp_number, sizeof( uint32_t ) );
2515 return first_packet;