]> git.sesse.net Git - vlc/blob - modules/access_output/rtmp.c
Don't dereference strrchr without check (CID 185)
[vlc] / modules / access_output / rtmp.c
1 /*****************************************************************************
2  * rtmp.c: RTMP output.
3  *****************************************************************************
4  * Copyright (C) URJC - LADyR - Luis Lopez Fernandez
5  *
6  * Author: Miguel Angel Cabrera Moya
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
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  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_sout.h>
33
34 #include <vlc_network.h> /* DOWN: #include <network.h> */
35 #include <vlc_url.h>
36 #include <vlc_block.h>
37
38 #include "../access/rtmp/rtmp_amf_flv.h"
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43
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." )
48
49 static int  Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
51
52 #define SOUT_CFG_PREFIX "sout-rtmp-"
53
54 vlc_module_begin();
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 );
64 vlc_module_end();
65
66 /*****************************************************************************
67  * Local prototypes
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 * );
72
73 struct sout_access_out_sys_t
74 {
75     int active;
76
77     /* thread for filtering and handling control messages */
78     rtmp_control_thread_t *p_thread;
79 };
80
81 /*****************************************************************************
82  * Open: open the rtmp connection
83  *****************************************************************************/
84 static int Open( vlc_object_t *p_this )
85 {
86     sout_access_out_t *p_access = (sout_access_out_t *) p_this;
87     sout_access_out_sys_t *p_sys;
88     char *psz, *p;
89     int length_path, length_media_name;
90     int i;
91
92     if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
93     {
94         msg_Err( p_access, "not enough memory" );
95         return VLC_ENOMEM;
96     }
97     p_access->p_sys = p_sys;
98
99     p_sys->p_thread =
100         vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) );
101     if( !p_sys->p_thread )
102     {
103         msg_Err( p_access, "out of memory" );
104         return VLC_ENOMEM;
105     }
106     vlc_object_attach( p_sys->p_thread, p_access );
107
108     /* Parse URI - remove spaces */
109     p = psz = strdup( p_access->psz_path );
110     while( ( p = strchr( p, ' ' ) ) != NULL )
111         *p = '+';
112     vlc_UrlParse( &p_sys->p_thread->url, psz, 0 );
113     free( psz );
114
115     if( p_sys->p_thread->url.psz_host == NULL
116         || *p_sys->p_thread->url.psz_host == '\0' )
117     {
118          msg_Warn( p_access, "invalid host" );
119          goto error;
120     }
121
122     if( p_sys->p_thread->url.i_port <= 0 )
123         p_sys->p_thread->url.i_port = 1935;
124
125     if ( p_sys->p_thread->url.psz_path == NULL )
126     {
127         msg_Warn( p_access, "invalid path" );
128         goto error;
129     }
130
131     length_path = strlen( p_sys->p_thread->url.psz_path );
132     char* psz_tmp = strrchr( p_sys->p_thread->url.psz_path, '/' );
133     if( !psz_tmp )
134         goto error;
135     length_media_name = strlen( psz_tmp ) - 1;
136
137     p_sys->p_thread->psz_application = strndup( p_sys->p_thread->url.psz_path + 1, length_path - length_media_name - 2 );
138     p_sys->p_thread->psz_media = strdup( p_sys->p_thread->url.psz_path + ( length_path - length_media_name ) );
139
140     msg_Dbg( p_access, "rtmp: host='%s' port=%d path='%s'",
141              p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port, p_sys->p_thread->url.psz_path );
142
143     if( p_sys->p_thread->url.psz_username && *p_sys->p_thread->url.psz_username )
144     {
145         msg_Dbg( p_access, "      user='%s', pwd='%s'",
146                  p_sys->p_thread->url.psz_username, p_sys->p_thread->url.psz_password );
147     }
148
149     /* Initialize thread variables */
150     p_sys->p_thread->b_die = 0;
151     p_sys->p_thread->b_error= 0;
152     p_sys->p_thread->p_fifo_input = block_FifoNew();
153     p_sys->p_thread->p_empty_blocks = block_FifoNew();
154     p_sys->p_thread->has_audio = 0;
155     p_sys->p_thread->has_video = 0;
156     p_sys->p_thread->metadata_received = 0;
157     p_sys->p_thread->first_media_packet = 1;
158     p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; /* FLV_TAG_FIRST_PREVIOUS_TAG_SIZE */
159
160     p_sys->p_thread->flv_body = rtmp_body_new( -1 );
161     p_sys->p_thread->flv_length_body = 0;
162
163     p_sys->p_thread->chunk_size_recv = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
164     p_sys->p_thread->chunk_size_send = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
165     for(i = 0; i < 64; i++)
166     {
167         memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) );
168         p_sys->p_thread->rtmp_headers_send[i].length_header = -1;
169         p_sys->p_thread->rtmp_headers_send[i].stream_index = -1;
170         p_sys->p_thread->rtmp_headers_send[i].timestamp = -1;
171         p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1;
172         p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1;
173         p_sys->p_thread->rtmp_headers_send[i].length_body = -1;
174         p_sys->p_thread->rtmp_headers_send[i].content_type = -1;
175         p_sys->p_thread->rtmp_headers_send[i].src_dst = -1;
176         p_sys->p_thread->rtmp_headers_send[i].body = NULL;
177     }
178
179     vlc_cond_init( &p_sys->p_thread->wait );
180     vlc_mutex_init( &p_sys->p_thread->lock );
181
182     p_sys->p_thread->result_connect = 1;
183     /* p_sys->p_thread->result_publish = only used on access */
184     p_sys->p_thread->result_play = 1;
185     p_sys->p_thread->result_stop = 0;
186     p_sys->p_thread->fd = -1;
187
188     /* Open connection */
189     if( var_CreateGetBool( p_access, "rtmp-connect" ) > 0 )
190     {
191 #if 0
192         p_sys->p_thread->fd = net_ConnectTCP( p_access,
193                                               p_sys->p_thread->url.psz_host,
194                                               p_sys->p_thread->url.i_port );
195 #endif
196         msg_Err( p_access, "to be implemented" );
197         goto error2;
198     }
199     else
200     {
201         int *p_fd_listen;
202
203         p_sys->active = 0;
204         p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host,
205                                      p_sys->p_thread->url.i_port );
206         if( p_fd_listen == NULL )
207         {
208             msg_Warn( p_access, "cannot listen to %s port %i",
209                       p_sys->p_thread->url.psz_host,
210                       p_sys->p_thread->url.i_port );
211             goto error2;
212         }
213
214         do
215             p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen, -1 );
216         while( p_sys->p_thread->fd == -1 );
217         net_ListenClose( p_fd_listen );
218
219         if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 )
220         {
221             msg_Err( p_access, "handshake passive failed");
222             goto error2;
223         }
224     }
225
226     if( vlc_thread_create( p_sys->p_thread, "rtmp control thread", ThreadControl,
227                            VLC_THREAD_PRIORITY_INPUT, false ) )
228     {
229         msg_Err( p_access, "cannot spawn rtmp control thread" );
230         goto error2;
231     }
232
233     if( !p_sys->active )
234     {
235         if( rtmp_connect_passive( p_sys->p_thread ) < 0 )
236         {
237             msg_Err( p_access, "connect passive failed");
238             goto error2;
239         }
240     }
241
242     p_access->pf_write = Write;
243     p_access->pf_seek = Seek;
244
245     return VLC_SUCCESS;
246
247 error2:
248     vlc_cond_destroy( &p_sys->p_thread->wait );
249     vlc_mutex_destroy( &p_sys->p_thread->lock );
250
251     free( p_sys->p_thread->psz_application );
252     free( p_sys->p_thread->psz_media );
253
254     if( p_sys->p_thread->fd != -1 )
255         net_Close( p_sys->p_thread->fd );
256 error:
257     vlc_object_detach( p_sys->p_thread );
258     vlc_object_release( p_sys->p_thread );
259
260     vlc_UrlClean( &p_sys->p_thread->url );
261     free( p_sys );
262
263     return VLC_EGENERIC;
264 }
265
266 /*****************************************************************************
267  * Close: close the target
268  *****************************************************************************/
269 static void Close( vlc_object_t * p_this )
270 {
271     sout_access_out_t *p_access = (sout_access_out_t *) p_this;
272     sout_access_out_sys_t *p_sys = p_access->p_sys;
273     int i;
274
275 //    p_sys->p_thread->b_die = true;
276     vlc_object_kill( p_sys->p_thread );
277     block_FifoWake( p_sys->p_thread->p_fifo_input );
278
279     vlc_thread_join( p_sys->p_thread );
280
281     vlc_cond_destroy( &p_sys->p_thread->wait );
282     vlc_mutex_destroy( &p_sys->p_thread->lock );
283
284     block_FifoRelease( p_sys->p_thread->p_fifo_input );
285     block_FifoRelease( p_sys->p_thread->p_empty_blocks );
286
287     for( i = 0; i < 64; i++ ) /* RTMP_HEADER_STREAM_INDEX_MASK */
288     {
289         if( p_sys->p_thread->rtmp_headers_recv[i].body != NULL )
290         {
291             free( p_sys->p_thread->rtmp_headers_recv[i].body->body );
292             free( p_sys->p_thread->rtmp_headers_recv[i].body );
293         }
294     }
295
296     net_Close( p_sys->p_thread->fd );
297
298     vlc_object_detach( p_sys->p_thread );
299     vlc_object_release( p_sys->p_thread );
300
301     vlc_UrlClean( &p_sys->p_thread->url );
302     free( p_sys->p_thread->psz_application );
303     free( p_sys->p_thread->psz_media );
304     free( p_sys );
305 }
306
307 /*****************************************************************************
308  * Write: standard write on a file descriptor.
309  *****************************************************************************/
310 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
311 {
312     rtmp_packet_t *rtmp_packet;
313     uint8_t *tmp_buffer;
314     ssize_t i_ret;
315     ssize_t i_write = 0;
316
317     if( p_access->p_sys->p_thread->first_media_packet )
318     {
319         /* 13 == FLV_HEADER_SIZE + PreviousTagSize*/
320         memmove( p_buffer->p_buffer, p_buffer->p_buffer + 13, p_buffer->i_buffer - 13 );
321         p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer - 13 );
322
323         p_access->p_sys->p_thread->first_media_packet = 0;
324     }
325
326     while( p_buffer )
327     {
328         block_t *p_next = p_buffer->p_next;
329 //////////////////////////////
330 /*msg_Warn(p_access, "XXXXXXXXXXXXXXXXX");
331 int i;
332 for(i = 0; i < p_buffer->i_buffer; i += 16)
333 {
334     msg_Warn(p_access,"%.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x",
335 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],
336 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]);
337 }*/
338 ////////////////////////
339         msg_Warn(p_access, "rtmp.c:360 i_dts %"PRIu64" i_pts %"PRIu64,
340                  p_buffer->i_dts, p_buffer->i_pts);
341         rtmp_packet = rtmp_build_flv_over_rtmp( p_access->p_sys->p_thread, p_buffer );
342
343         if( rtmp_packet )
344         {
345             tmp_buffer = rtmp_encode_packet( p_access->p_sys->p_thread, rtmp_packet );
346
347             i_ret = net_Write( p_access->p_sys->p_thread, p_access->p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
348             if( i_ret != rtmp_packet->length_encoded )
349             {
350                 free( rtmp_packet->body->body );
351                 free( rtmp_packet->body );
352                 free( rtmp_packet );
353                 free( tmp_buffer );
354                 msg_Err( p_access->p_sys->p_thread, "failed send flv packet" );
355                 return -1;
356             }
357             free( rtmp_packet->body->body );
358             free( rtmp_packet->body );
359             free( rtmp_packet );
360             free( tmp_buffer );
361         }
362
363         i_write += p_buffer->i_buffer;
364
365         p_buffer = p_next;
366     }
367
368     return i_write;
369 }
370
371 /********************a*********************************************************
372  * Seek: seek to a specific location in a file
373  *****************************************************************************/
374 static int Seek( sout_access_out_t *p_access, off_t i_pos )
375 {
376     (void)i_pos;
377     msg_Err( p_access, "RTMP sout access cannot seek" );
378     return -1;
379 }
380
381 /*****************************************************************************
382  * ThreadControl: manage control messages and pipe media to Read
383  *****************************************************************************/
384 static void* ThreadControl( vlc_object_t *p_this )
385 {
386     rtmp_control_thread_t *p_thread = (rtmp_control_thread_t *) p_this;
387     rtmp_packet_t *rtmp_packet;
388     int canc = vlc_savecancel ();
389
390     rtmp_init_handler( p_thread->rtmp_handler );
391
392     while( vlc_object_alive (p_thread) )
393     {
394         rtmp_packet = rtmp_read_net_packet( p_thread );
395         if( rtmp_packet != NULL )
396         {
397             if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
398                 || rtmp_packet->content_type > 0x14 ) /* RTMP_CONTENT_TYPE_INVOKE */
399             {
400                 free( rtmp_packet->body->body );
401                 free( rtmp_packet->body );
402                 free( rtmp_packet );
403
404                 msg_Warn( p_thread, "unknown content type received" );
405             }
406             else
407                 p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet );
408         }
409         else
410         {
411             /* Sometimes server close connection too soon */
412             if( p_thread->result_connect )
413             {
414                 vlc_mutex_lock( &p_thread->lock );
415                 vlc_cond_signal( &p_thread->wait );
416                 vlc_mutex_unlock( &p_thread->lock );
417             }
418
419             p_thread->b_die = 1;
420         }
421     }
422     vlc_restorecancel (canc);
423     return NULL;
424 }