1 /*****************************************************************************
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_plugin.h>
34 #include <vlc_network.h> /* DOWN: #include <network.h> */
36 #include <vlc_block.h>
38 #include "../access/rtmp/rtmp_amf_flv.h"
40 /*****************************************************************************
42 *****************************************************************************/
44 #define RTMP_CONNECT_TEXT N_( "Active TCP connection" )
45 #define RTMP_CONNECT_LONGTEXT N_( \
46 "If enabled, VLC will connect to a remote destination instead of " \
47 "waiting for an incoming connection." )
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
52 #define SOUT_CFG_PREFIX "sout-rtmp-"
55 set_description( N_("RTMP stream output") );
56 set_shortname( N_("RTMP" ) );
57 set_capability( "sout access", 50 );
58 set_category( CAT_SOUT );
59 set_subcategory( SUBCAT_SOUT_STREAM );
60 add_shortcut( "rtmp" );
61 set_callbacks( Open, Close );
62 add_bool( "rtmp-connect", false, NULL, RTMP_CONNECT_TEXT,
63 RTMP_CONNECT_LONGTEXT, false );
66 /*****************************************************************************
68 *****************************************************************************/
69 static ssize_t Write( sout_access_out_t *, block_t * );
70 static int Seek ( sout_access_out_t *, off_t );
71 static void* ThreadControl( vlc_object_t * );
73 struct sout_access_out_sys_t
77 /* thread for filtering and handling control messages */
78 rtmp_control_thread_t *p_thread;
81 /*****************************************************************************
82 * Open: open the rtmp connection
83 *****************************************************************************/
84 static int Open( vlc_object_t *p_this )
86 sout_access_out_t *p_access = (sout_access_out_t *) p_this;
87 sout_access_out_sys_t *p_sys;
89 int length_path, length_media_name;
92 if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
94 msg_Err( p_access, "not enough memory" );
97 p_access->p_sys = p_sys;
100 vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) );
101 if( !p_sys->p_thread )
103 msg_Err( p_access, "out of memory" );
106 vlc_object_attach( p_sys->p_thread, p_access );
108 /* Parse URI - remove spaces */
109 p = psz = strdup( p_access->psz_path );
110 while( ( p = strchr( p, ' ' ) ) != NULL )
112 vlc_UrlParse( &p_sys->p_thread->url, psz, 0 );
115 if( p_sys->p_thread->url.psz_host == NULL
116 || *p_sys->p_thread->url.psz_host == '\0' )
118 msg_Warn( p_access, "invalid host" );
122 if( p_sys->p_thread->url.i_port <= 0 )
123 p_sys->p_thread->url.i_port = 1935;
125 if ( p_sys->p_thread->url.psz_path == NULL )
127 msg_Warn( p_access, "invalid path" );
131 length_path = strlen( p_sys->p_thread->url.psz_path );
132 length_media_name = strlen( strrchr( p_sys->p_thread->url.psz_path, '/' ) ) - 1;
134 p_sys->p_thread->psz_application = strndup( p_sys->p_thread->url.psz_path + 1, length_path - length_media_name - 2 );
135 p_sys->p_thread->psz_media = strdup( p_sys->p_thread->url.psz_path + ( length_path - length_media_name ) );
137 msg_Dbg( p_access, "rtmp: host='%s' port=%d path='%s'",
138 p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port, p_sys->p_thread->url.psz_path );
140 if( p_sys->p_thread->url.psz_username && *p_sys->p_thread->url.psz_username )
142 msg_Dbg( p_access, " user='%s', pwd='%s'",
143 p_sys->p_thread->url.psz_username, p_sys->p_thread->url.psz_password );
146 /* Initialize thread variables */
147 p_sys->p_thread->b_die = 0;
148 p_sys->p_thread->b_error= 0;
149 p_sys->p_thread->p_fifo_input = block_FifoNew();
150 p_sys->p_thread->p_empty_blocks = block_FifoNew();
151 p_sys->p_thread->has_audio = 0;
152 p_sys->p_thread->has_video = 0;
153 p_sys->p_thread->metadata_received = 0;
154 p_sys->p_thread->first_media_packet = 1;
155 p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; /* FLV_TAG_FIRST_PREVIOUS_TAG_SIZE */
157 p_sys->p_thread->flv_body = rtmp_body_new( -1 );
158 p_sys->p_thread->flv_length_body = 0;
160 p_sys->p_thread->chunk_size_recv = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
161 p_sys->p_thread->chunk_size_send = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
162 for(i = 0; i < 64; i++)
164 memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) );
165 p_sys->p_thread->rtmp_headers_send[i].length_header = -1;
166 p_sys->p_thread->rtmp_headers_send[i].stream_index = -1;
167 p_sys->p_thread->rtmp_headers_send[i].timestamp = -1;
168 p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1;
169 p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1;
170 p_sys->p_thread->rtmp_headers_send[i].length_body = -1;
171 p_sys->p_thread->rtmp_headers_send[i].content_type = -1;
172 p_sys->p_thread->rtmp_headers_send[i].src_dst = -1;
173 p_sys->p_thread->rtmp_headers_send[i].body = NULL;
176 vlc_cond_init( &p_sys->p_thread->wait );
177 vlc_mutex_init( &p_sys->p_thread->lock );
179 p_sys->p_thread->result_connect = 1;
180 /* p_sys->p_thread->result_publish = only used on access */
181 p_sys->p_thread->result_play = 1;
182 p_sys->p_thread->result_stop = 0;
183 p_sys->p_thread->fd = -1;
185 /* Open connection */
186 if( var_CreateGetBool( p_access, "rtmp-connect" ) > 0 )
189 p_sys->p_thread->fd = net_ConnectTCP( p_access,
190 p_sys->p_thread->url.psz_host,
191 p_sys->p_thread->url.i_port );
193 msg_Err( p_access, "to be implemented" );
201 p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host,
202 p_sys->p_thread->url.i_port );
203 if( p_fd_listen == NULL )
205 msg_Warn( p_access, "cannot listen to %s port %i",
206 p_sys->p_thread->url.psz_host,
207 p_sys->p_thread->url.i_port );
212 p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen, -1 );
213 while( p_sys->p_thread->fd == -1 );
214 net_ListenClose( p_fd_listen );
216 if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 )
218 msg_Err( p_access, "handshake passive failed");
223 if( vlc_thread_create( p_sys->p_thread, "rtmp control thread", ThreadControl,
224 VLC_THREAD_PRIORITY_INPUT, false ) )
226 msg_Err( p_access, "cannot spawn rtmp control thread" );
232 if( rtmp_connect_passive( p_sys->p_thread ) < 0 )
234 msg_Err( p_access, "connect passive failed");
239 p_access->pf_write = Write;
240 p_access->pf_seek = Seek;
245 vlc_cond_destroy( &p_sys->p_thread->wait );
246 vlc_mutex_destroy( &p_sys->p_thread->lock );
248 free( p_sys->p_thread->psz_application );
249 free( p_sys->p_thread->psz_media );
251 if( p_sys->p_thread->fd != -1 )
252 net_Close( p_sys->p_thread->fd );
254 vlc_object_detach( p_sys->p_thread );
255 vlc_object_release( p_sys->p_thread );
257 vlc_UrlClean( &p_sys->p_thread->url );
263 /*****************************************************************************
264 * Close: close the target
265 *****************************************************************************/
266 static void Close( vlc_object_t * p_this )
268 sout_access_out_t *p_access = (sout_access_out_t *) p_this;
269 sout_access_out_sys_t *p_sys = p_access->p_sys;
272 // p_sys->p_thread->b_die = true;
273 vlc_object_kill( p_sys->p_thread );
274 block_FifoWake( p_sys->p_thread->p_fifo_input );
275 block_FifoWake( p_sys->p_thread->p_empty_blocks );
277 vlc_thread_join( p_sys->p_thread );
279 vlc_cond_destroy( &p_sys->p_thread->wait );
280 vlc_mutex_destroy( &p_sys->p_thread->lock );
282 block_FifoRelease( p_sys->p_thread->p_fifo_input );
283 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
285 for( i = 0; i < 64; i++ ) /* RTMP_HEADER_STREAM_INDEX_MASK */
287 if( p_sys->p_thread->rtmp_headers_recv[i].body != NULL )
289 free( p_sys->p_thread->rtmp_headers_recv[i].body->body );
290 free( p_sys->p_thread->rtmp_headers_recv[i].body );
294 net_Close( p_sys->p_thread->fd );
296 vlc_object_detach( p_sys->p_thread );
297 vlc_object_release( p_sys->p_thread );
299 vlc_UrlClean( &p_sys->p_thread->url );
300 free( p_sys->p_thread->psz_application );
301 free( p_sys->p_thread->psz_media );
305 /*****************************************************************************
306 * Write: standard write on a file descriptor.
307 *****************************************************************************/
308 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
310 rtmp_packet_t *rtmp_packet;
315 if( p_access->p_sys->p_thread->first_media_packet )
317 /* 13 == FLV_HEADER_SIZE + PreviousTagSize*/
318 memmove( p_buffer->p_buffer, p_buffer->p_buffer + 13, p_buffer->i_buffer - 13 );
319 p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer - 13 );
321 p_access->p_sys->p_thread->first_media_packet = 0;
326 block_t *p_next = p_buffer->p_next;
327 //////////////////////////////
328 /*msg_Warn(p_access, "XXXXXXXXXXXXXXXXX");
330 for(i = 0; i < p_buffer->i_buffer; i += 16)
332 msg_Warn(p_access,"%.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x",
333 p_buffer->p_buffer[i], p_buffer->p_buffer[i+1], p_buffer->p_buffer[i+2], p_buffer->p_buffer[i+3], p_buffer->p_buffer[i+4], p_buffer->p_buffer[i+5], p_buffer->p_buffer[i+6], p_buffer->p_buffer[i+7],
334 p_buffer->p_buffer[i+8], p_buffer->p_buffer[i+9], p_buffer->p_buffer[i+10], p_buffer->p_buffer[i+11], p_buffer->p_buffer[i+12], p_buffer->p_buffer[i+13], p_buffer->p_buffer[i+14], p_buffer->p_buffer[i+15]);
336 ////////////////////////
337 msg_Warn(p_access, "rtmp.c:360 i_dts %"PRIu64" i_pts %"PRIu64,
338 p_buffer->i_dts, p_buffer->i_pts);
339 rtmp_packet = rtmp_build_flv_over_rtmp( p_access->p_sys->p_thread, p_buffer );
343 tmp_buffer = rtmp_encode_packet( p_access->p_sys->p_thread, rtmp_packet );
345 i_ret = net_Write( p_access->p_sys->p_thread, p_access->p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
346 if( i_ret != rtmp_packet->length_encoded )
348 free( rtmp_packet->body->body );
349 free( rtmp_packet->body );
352 msg_Err( p_access->p_sys->p_thread, "failed send flv packet" );
355 free( rtmp_packet->body->body );
356 free( rtmp_packet->body );
361 i_write += p_buffer->i_buffer;
369 /********************a*********************************************************
370 * Seek: seek to a specific location in a file
371 *****************************************************************************/
372 static int Seek( sout_access_out_t *p_access, off_t i_pos )
375 msg_Err( p_access, "RTMP sout access cannot seek" );
379 /*****************************************************************************
380 * ThreadControl: manage control messages and pipe media to Read
381 *****************************************************************************/
382 static void* ThreadControl( vlc_object_t *p_this )
384 rtmp_control_thread_t *p_thread = (rtmp_control_thread_t *) p_this;
385 rtmp_packet_t *rtmp_packet;
386 int canc = vlc_savecancel ();
388 rtmp_init_handler( p_thread->rtmp_handler );
390 while( vlc_object_alive (p_thread) )
392 rtmp_packet = rtmp_read_net_packet( p_thread );
393 if( rtmp_packet != NULL )
395 if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
396 || rtmp_packet->content_type > 0x14 ) /* RTMP_CONTENT_TYPE_INVOKE */
398 free( rtmp_packet->body->body );
399 free( rtmp_packet->body );
402 msg_Warn( p_thread, "unknown content type received" );
405 p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet );
409 /* Sometimes server close connection too soon */
410 if( p_thread->result_connect )
412 vlc_mutex_lock( &p_thread->lock );
413 vlc_cond_signal( &p_thread->wait );
414 vlc_mutex_unlock( &p_thread->lock );
420 vlc_restorecancel (canc);