]> git.sesse.net Git - vlc/blob - modules/access/rtmp/access.c
Merge branch 'master' into lpcm_encoder
[vlc] / modules / access / rtmp / access.c
1 /*****************************************************************************
2  * access.c: RTMP input.
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_access.h>
33
34 #include <vlc_network.h> /* DOWN: #include <network.h> */
35 #include <vlc_url.h>
36 #include <vlc_block.h>
37
38 #include "rtmp_amf_flv.h"
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 #define CACHING_TEXT N_("Caching value in ms")
44 #define CACHING_LONGTEXT N_( \
45     "Caching value for RTMP streams. This " \
46     "value should be set in milliseconds." )
47
48 #define SWFURL_TEXT N_("Default SWF Referrer URL")
49 #define SWFURL_LONGTEXT N_("The SFW URL to use as referrer when connecting to "\
50                            "the server. This is the SWF file that contained "  \
51                            "the stream.")
52
53 #define PAGEURL_TEXT N_("Default Page Referrer URL")
54 #define PAGEURL_LONGTEXT N_("The Page URL to use as referrer when connecting to "  \
55                             "the server. This is the page housing the SWF "    \
56                             "file.")
57
58 static int  Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
60
61 vlc_module_begin ()
62     set_description( N_("RTMP input") )
63     set_shortname( N_("RTMP") )
64     set_category( CAT_INPUT )
65     set_subcategory( SUBCAT_INPUT_ACCESS )
66
67     add_integer( "rtmp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
68                  CACHING_LONGTEXT, true )
69     add_string( "rtmp-swfurl", "file:///player.swf", NULL, SWFURL_TEXT,
70                 SWFURL_LONGTEXT, true )
71     add_string( "rtmp-pageurl", "file:///player.htm", NULL, PAGEURL_TEXT,
72                 PAGEURL_LONGTEXT, true )
73
74     set_capability( "access", 0 )
75     set_callbacks( Open, Close )
76     add_shortcut( "rtmp" )
77 vlc_module_end ()
78
79
80 /*****************************************************************************
81  * Local prototypes
82  *****************************************************************************/
83 static ssize_t  Read( access_t *, uint8_t *, size_t );
84 static int Seek( access_t *, uint64_t );
85 static int Control( access_t *, int, va_list );
86
87 static void* ThreadControl( void * );
88
89 /*****************************************************************************
90  * Open: open the rtmp connection
91  *****************************************************************************/
92 static int Open( vlc_object_t *p_this )
93 {
94     access_t *p_access = (access_t *) p_this;
95     access_sys_t *p_sys;
96     char *psz, *p; 
97     int length_path, length_media_name;
98     int i;
99
100     STANDARD_READ_ACCESS_INIT
101
102     p_sys->p_thread =
103         vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) );
104     if( !p_sys->p_thread )
105         return VLC_ENOMEM;
106     vlc_object_attach( p_sys->p_thread, p_access );
107
108     /* Parse URI - remove spaces */
109     p = psz = strdup( p_access->psz_location );
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     p_sys->p_thread->psz_swf_url = var_CreateGetString( p_access, "rtmp-swfurl" );
141     p_sys->p_thread->psz_page_url = var_CreateGetString( p_access, "rtmp-pageurl" );
142
143     msg_Dbg( p_access, "rtmp: host='%s' port=%d path='%s'",
144              p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port, p_sys->p_thread->url.psz_path );
145
146     if( p_sys->p_thread->url.psz_username && *p_sys->p_thread->url.psz_username )
147     {
148         msg_Dbg( p_access, "      user='%s'", p_sys->p_thread->url.psz_username );
149     }
150
151     /* Initialize thread variables */
152     p_sys->p_thread->b_error= 0;
153     p_sys->p_thread->p_fifo_input = block_FifoNew();
154     p_sys->p_thread->p_empty_blocks = block_FifoNew();
155     p_sys->p_thread->has_audio = 0;
156     p_sys->p_thread->has_video = 0;
157     p_sys->p_thread->metadata_received = 0;
158     p_sys->p_thread->first_media_packet = 1;
159     p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; /* FLV_TAG_FIRST_PREVIOUS_TAG_SIZE */
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++)
163     {
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;
174     }
175
176     p_sys->p_thread->p_base_object = p_this;
177
178     vlc_cond_init( &p_sys->p_thread->wait );
179
180     vlc_mutex_init( &p_sys->p_thread->lock );
181
182     p_sys->p_thread->result_connect = 1;
183     p_sys->p_thread->result_play = 1;
184     p_sys->p_thread->result_stop = 0;
185
186     /* Open connection */
187     p_sys->p_thread->fd = net_ConnectTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
188     if( p_sys->p_thread->fd == -1 )
189     {
190         int *p_fd_listen;
191
192         msg_Warn( p_access, "cannot connect to %s:%d", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
193         msg_Dbg( p_access, "switching to passive mode" );
194
195         p_sys->active = 0;
196
197         p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
198         if( p_fd_listen == NULL )
199         {
200             msg_Err( p_access, "cannot listen to %s port %i", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
201             goto error2;
202         }
203
204         p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen );
205
206         net_ListenClose( p_fd_listen );
207
208         if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 )
209         {
210             msg_Err( p_access, "handshake passive failed");
211             goto error2;
212         }
213
214         p_sys->p_thread->result_publish = 1;
215     }
216     else
217     {
218         p_sys->active = 1;
219
220         if( rtmp_handshake_active( p_this, p_sys->p_thread->fd ) < 0 )
221         {
222             msg_Err( p_access, "handshake active failed");
223             goto error2;
224         }
225
226         p_sys->p_thread->result_publish = 0;
227     }
228
229     if( vlc_clone( &p_sys->p_thread->thread, ThreadControl, p_sys->p_thread,
230                    VLC_THREAD_PRIORITY_INPUT ) )
231     {
232         msg_Err( p_access, "cannot spawn rtmp control thread" );
233         goto error2;
234     }
235
236     if( p_sys->active )
237     {
238         if( rtmp_connect_active( p_sys->p_thread ) < 0 )
239         {
240             msg_Err( p_access, "connect active failed");
241             /* Kill the running thread */
242             vlc_cancel( p_sys->p_thread->thread );
243             vlc_join( p_sys->p_thread->thread, NULL );
244             goto error2;
245         }
246     }
247
248     /* Set vars for reading from fifo */
249     p_access->p_sys->flv_packet = NULL;
250     p_access->p_sys->read_packet = 1;
251
252     /* Update default_pts to a suitable value for rtmp access */
253     var_Create( p_access, "rtmp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
254
255     return VLC_SUCCESS;
256
257 error2:
258     vlc_cond_destroy( &p_sys->p_thread->wait );
259     vlc_mutex_destroy( &p_sys->p_thread->lock );
260
261     block_FifoRelease( p_sys->p_thread->p_fifo_input );
262     block_FifoRelease( p_sys->p_thread->p_empty_blocks );
263
264     free( p_sys->p_thread->psz_application );
265     free( p_sys->p_thread->psz_media );
266     free( p_sys->p_thread->psz_swf_url );
267     free( p_sys->p_thread->psz_page_url );
268
269     net_Close( p_sys->p_thread->fd );
270 error:
271     vlc_UrlClean( &p_sys->p_thread->url );
272
273     vlc_object_release( p_sys->p_thread );
274
275     free( p_sys );
276
277     return VLC_EGENERIC;
278 }
279
280 /*****************************************************************************
281  * Close: close the rtmp connection
282  *****************************************************************************/
283 static void Close( vlc_object_t * p_this )
284 {
285     access_t     *p_access = (access_t *) p_this;
286     access_sys_t *p_sys = p_access->p_sys;
287     int i;
288
289     vlc_cancel( p_sys->p_thread->thread );
290     vlc_join( p_sys->p_thread->thread, NULL );
291
292     vlc_cond_destroy( &p_sys->p_thread->wait );
293     vlc_mutex_destroy( &p_sys->p_thread->lock );
294
295     block_FifoRelease( p_sys->p_thread->p_fifo_input );
296     block_FifoRelease( p_sys->p_thread->p_empty_blocks );
297
298     for( i = 0; i < 64; i++ ) /* RTMP_HEADER_STREAM_INDEX_MASK */
299     {
300         if( p_sys->p_thread->rtmp_headers_recv[i].body != NULL )
301         {
302             free( p_sys->p_thread->rtmp_headers_recv[i].body->body );
303             free( p_sys->p_thread->rtmp_headers_recv[i].body );
304         }
305     }
306
307     net_Close( p_sys->p_thread->fd );
308
309     var_Destroy( p_access, "rtmp-caching" );
310
311     vlc_UrlClean( &p_sys->p_thread->url );
312     free( p_sys->p_thread->psz_application );
313     free( p_sys->p_thread->psz_media );
314     free( p_sys->p_thread->psz_swf_url );
315     free( p_sys->p_thread->psz_page_url );
316
317     vlc_object_release( p_sys->p_thread );
318     free( p_sys );
319 }
320
321 /*****************************************************************************
322  * Read: standard read on a file descriptor.
323  *****************************************************************************/
324 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
325 {
326     access_sys_t *p_sys = p_access->p_sys;
327     rtmp_packet_t *rtmp_packet;
328     uint8_t *tmp_buffer;
329     ssize_t i_ret;
330     size_t i_len_tmp;
331
332     i_len_tmp = 0;
333
334     while( i_len_tmp < i_len )
335     {
336         if( p_sys->p_thread->result_stop || p_access->info.b_eof || !vlc_object_alive (p_access) )
337         {
338             p_access->info.b_eof = true;
339             return 0;
340         }
341
342         if( p_sys->read_packet )
343         {
344             if( !p_sys->p_thread->metadata_received )
345             {
346                 /* Wait until enough data is received for extracting metadata */
347 #warning This is not thread-safe (because block_FifoCount() is not)!
348                 if( block_FifoCount( p_sys->p_thread->p_fifo_input ) < 10 )
349                 {
350 #warning This is wrong!
351                     msleep(100000);
352                     continue;
353                 }
354
355                 p_sys->flv_packet = flv_get_metadata( p_access );
356
357                 p_sys->p_thread->metadata_received = 1;
358             }
359             else
360             {
361                 p_sys->flv_packet = block_FifoGet( p_sys->p_thread->p_fifo_input );
362                 if( p_sys->flv_packet == NULL )
363                     continue; /* Forced wake-up */
364             }
365
366             if( p_sys->p_thread->first_media_packet )
367             {
368                 p_sys->flv_packet = flv_insert_header( p_access, p_sys->flv_packet );
369
370                 p_sys->p_thread->first_media_packet = 0;
371             }
372         }
373         if( i_len - i_len_tmp >= p_sys->flv_packet->i_buffer )
374         {
375             p_sys->read_packet = 1;
376
377             memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, p_sys->flv_packet->i_buffer );
378             block_FifoPut( p_sys->p_thread->p_empty_blocks, p_sys->flv_packet );
379
380             i_len_tmp += p_sys->flv_packet->i_buffer;
381         }
382         else
383         {
384             p_sys->read_packet = 0;
385
386             memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, i_len - i_len_tmp);
387             p_sys->flv_packet->i_buffer -= i_len - i_len_tmp;
388             memmove( p_sys->flv_packet->p_buffer, p_sys->flv_packet->p_buffer + i_len - i_len_tmp, p_sys->flv_packet->i_buffer );
389
390             i_len_tmp += i_len - i_len_tmp;
391         }
392     }
393     if( i_len_tmp > 0 )
394     {
395         if( p_sys->p_thread->result_publish )
396         {
397             /* Send publish onStatus event only once */
398             p_sys->p_thread->result_publish = 0;
399
400             rtmp_packet = rtmp_build_publish_start( p_sys->p_thread );
401
402             tmp_buffer = rtmp_encode_packet( p_sys->p_thread, rtmp_packet );
403
404             i_ret = net_Write( p_sys->p_thread, p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
405             if( i_ret != rtmp_packet->length_encoded )
406             {
407                 msg_Err( p_access, "failed send publish start" );
408                 goto error;
409             }
410             free( rtmp_packet->body->body );
411             free( rtmp_packet->body );
412             free( rtmp_packet );
413             free( tmp_buffer );
414         }
415
416         p_access->info.i_pos += i_len_tmp;
417
418         rtmp_packet = rtmp_build_bytes_read( p_sys->p_thread, p_access->info.i_pos );
419
420         tmp_buffer = rtmp_encode_packet( p_sys->p_thread, rtmp_packet );
421  
422         i_ret = net_Write( p_sys->p_thread, p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
423         if( i_ret != rtmp_packet->length_encoded )
424         {
425             msg_Err( p_access, "failed send bytes read" );
426             goto error;
427         }
428         free( rtmp_packet->body->body );
429         free( rtmp_packet->body );
430         free( rtmp_packet );
431         free( tmp_buffer );
432     }
433
434     return i_len_tmp;
435
436 error:
437     free( rtmp_packet->body->body );
438     free( rtmp_packet->body );
439     free( rtmp_packet );
440     free( tmp_buffer );
441     return -1;
442 }
443
444 /*****************************************************************************
445  * Seek: seek to a specific location in a file
446  *****************************************************************************/
447 static int Seek( access_t *p_access, uint64_t i_pos )
448 {
449     VLC_UNUSED( p_access );
450     VLC_UNUSED( i_pos );
451 /*msg_Warn ( p_access, "Seek to %lld", i_pos);
452     switch( rtmp_seek( p_access, i_pos ) )
453     {
454         case -1:
455             return VLC_EGENERIC;
456         case 0:
457             break;
458         default:
459             msg_Err( p_access, "You should not be here" );
460             abort();
461     }
462 */
463     return VLC_EGENERIC;
464 }
465
466 /*****************************************************************************
467  * Control:
468  *****************************************************************************/
469 static int Control( access_t *p_access, int i_query, va_list args )
470 {
471     bool    *pb_bool;
472     int64_t *pi_64;
473
474     switch( i_query )
475     {
476         /* */
477         case ACCESS_CAN_SEEK:
478         case ACCESS_CAN_FASTSEEK:
479             pb_bool = (bool*) va_arg( args, bool* );
480             *pb_bool = false; /* TODO */
481             break;
482
483         case ACCESS_CAN_PAUSE:
484             pb_bool = (bool*) va_arg( args, bool* );
485             *pb_bool = false; /* TODO */
486             break;
487
488         case ACCESS_CAN_CONTROL_PACE:
489             pb_bool = (bool*) va_arg( args, bool* );
490             *pb_bool = true;
491             break;
492
493         /* */
494         case ACCESS_GET_PTS_DELAY:
495             pi_64 = (int64_t*) va_arg( args, int64_t * );
496             *pi_64 = var_GetInteger( p_access, "rtmp-caching" ) * INT64_C(1000);
497             break;
498
499         /* */
500         case ACCESS_SET_PAUSE_STATE:
501             /* Nothing to do */
502             break;
503
504         case ACCESS_GET_TITLE_INFO:
505         case ACCESS_SET_TITLE:
506         case ACCESS_SET_SEEKPOINT:
507         case ACCESS_SET_PRIVATE_ID_STATE:
508         case ACCESS_GET_META:
509         case ACCESS_GET_CONTENT_TYPE: /* DOWN: comment this line */
510             return VLC_EGENERIC;
511
512         default:
513             msg_Warn( p_access, "unimplemented query in control" );
514             return VLC_EGENERIC;
515     }
516
517     return VLC_SUCCESS;
518 }
519
520 /*****************************************************************************
521  * ThreadControl: manage control messages and pipe media to Read
522  *****************************************************************************/
523 static void* ThreadControl( void *p_this )
524 {
525     rtmp_control_thread_t *p_thread = p_this;
526     rtmp_packet_t *rtmp_packet;
527     int canc = vlc_savecancel ();
528
529     rtmp_init_handler( p_thread->rtmp_handler );
530
531     for( ;; )
532     {
533         vlc_restorecancel( canc );
534         rtmp_packet = rtmp_read_net_packet( p_thread );
535         canc = vlc_savecancel( );
536         if( rtmp_packet != NULL )
537         {
538             if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
539                 || rtmp_packet->content_type > 0x14 ) /* RTMP_CONTENT_TYPE_INVOKE */
540             {
541                 free( rtmp_packet->body->body );
542                 free( rtmp_packet->body );
543                 free( rtmp_packet );
544
545                 msg_Warn( p_thread, "unknown content type received" );
546             }
547             else
548                 p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet );
549         }
550         else
551         {
552             /* Sometimes server close connection too soon */
553             if( p_thread->result_connect )
554             {
555 #warning There must be a bug here!
556                 vlc_mutex_lock( &p_thread->lock );
557                 vlc_cond_signal( &p_thread->wait );
558                 vlc_mutex_unlock( &p_thread->lock );
559             }
560
561 #warning info cannot be accessed outside input thread!
562             ((access_t *) p_thread->p_base_object)->info.b_eof = true;
563             block_FifoWake( p_thread->p_fifo_input );
564             break;
565         }
566     }
567     vlc_restorecancel (canc);
568     return NULL;
569 }