rtmp: add an option to let the user set swfUrl and pageUrl.
[vlc.git] / 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_("Page Referrer URL")
54 #define PAGEURL_LONGTEXT N_("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:///mac.flv", NULL, SWFURL_TEXT,
70                 SWFURL_LONGTEXT, true )
71     add_string( "rtmp-pageurl", "file:///mac.html", 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 *, int64_t );
85 static int Control( access_t *, int, va_list );
86
87 static void* ThreadControl( vlc_object_t * );
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_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     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_die = 0;
153     p_sys->p_thread->b_error= 0;
154     p_sys->p_thread->p_fifo_input = block_FifoNew();
155     p_sys->p_thread->p_empty_blocks = block_FifoNew();
156     p_sys->p_thread->has_audio = 0;
157     p_sys->p_thread->has_video = 0;
158     p_sys->p_thread->metadata_received = 0;
159     p_sys->p_thread->first_media_packet = 1;
160     p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; /* FLV_TAG_FIRST_PREVIOUS_TAG_SIZE */
161     p_sys->p_thread->chunk_size_recv = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
162     p_sys->p_thread->chunk_size_send = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
163     for(i = 0; i < 64; i++)
164     {
165         memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) );
166         p_sys->p_thread->rtmp_headers_send[i].length_header = -1;
167         p_sys->p_thread->rtmp_headers_send[i].stream_index = -1;
168         p_sys->p_thread->rtmp_headers_send[i].timestamp = -1;
169         p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1;
170         p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1;
171         p_sys->p_thread->rtmp_headers_send[i].length_body = -1;
172         p_sys->p_thread->rtmp_headers_send[i].content_type = -1;
173         p_sys->p_thread->rtmp_headers_send[i].src_dst = -1;
174         p_sys->p_thread->rtmp_headers_send[i].body = NULL;
175     }
176
177     p_sys->p_thread->p_base_object = p_this;
178
179     vlc_cond_init( &p_sys->p_thread->wait );
180
181     vlc_mutex_init( &p_sys->p_thread->lock );
182
183     p_sys->p_thread->result_connect = 1;
184     p_sys->p_thread->result_play = 1;
185     p_sys->p_thread->result_stop = 0;
186
187     /* Open connection */
188     p_sys->p_thread->fd = net_ConnectTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
189     if( p_sys->p_thread->fd == -1 )
190     {
191         int *p_fd_listen;
192
193         msg_Warn( p_access, "cannot connect to %s:%d", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
194         msg_Dbg( p_access, "switching to passive mode" );
195
196         p_sys->active = 0;
197
198         p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
199         if( p_fd_listen == NULL )
200         {
201             msg_Err( p_access, "cannot listen to %s port %i", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
202             goto error2;
203         }
204
205         p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen, -1 );
206
207         net_ListenClose( p_fd_listen );
208
209         if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 )
210         {
211             msg_Err( p_access, "handshake passive failed");
212             goto error2;
213         }
214
215         p_sys->p_thread->result_publish = 1;
216     }
217     else
218     {
219         p_sys->active = 1;
220
221         if( rtmp_handshake_active( p_this, p_sys->p_thread->fd ) < 0 )
222         {
223             msg_Err( p_access, "handshake active failed");
224             goto error2;
225         }
226
227         p_sys->p_thread->result_publish = 0;
228     }
229
230     if( vlc_thread_create( p_sys->p_thread, "rtmp control thread", ThreadControl,
231                            VLC_THREAD_PRIORITY_INPUT ) )
232     {
233         msg_Err( p_access, "cannot spawn rtmp control thread" );
234         goto error2;
235     }
236
237     if( p_sys->active )
238     {
239         if( rtmp_connect_active( p_sys->p_thread ) < 0 )
240         {
241             msg_Err( p_access, "connect active failed");
242             /* Kill the running thread */
243             vlc_object_kill( p_sys->p_thread );
244             block_FifoWake( p_sys->p_thread->p_fifo_input );
245             vlc_thread_join( p_sys->p_thread );
246             goto error2;
247         }
248     }
249
250     /* Set vars for reading from fifo */
251     p_access->p_sys->flv_packet = NULL;
252     p_access->p_sys->read_packet = 1;
253
254     /* Update default_pts to a suitable value for rtmp access */
255     var_Create( p_access, "rtmp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
256
257     return VLC_SUCCESS;
258
259 error2:
260     vlc_cond_destroy( &p_sys->p_thread->wait );
261     vlc_mutex_destroy( &p_sys->p_thread->lock );
262
263     block_FifoRelease( p_sys->p_thread->p_fifo_input );
264     block_FifoRelease( p_sys->p_thread->p_empty_blocks );
265
266     free( p_sys->p_thread->psz_application );
267     free( p_sys->p_thread->psz_media );
268     free( p_sys->p_thread->psz_swf_url );
269     free( p_sys->p_thread->psz_page_url );
270
271     net_Close( p_sys->p_thread->fd );
272 error:
273     vlc_UrlClean( &p_sys->p_thread->url );
274
275     vlc_object_detach( p_sys->p_thread );
276     vlc_object_release( p_sys->p_thread );
277
278     free( p_sys );
279
280     return VLC_EGENERIC;
281 }
282
283 /*****************************************************************************
284  * Close: close the rtmp connection
285  *****************************************************************************/
286 static void Close( vlc_object_t * p_this )
287 {
288     access_t     *p_access = (access_t *) p_this;
289     access_sys_t *p_sys = p_access->p_sys;
290     int i;
291
292 /*    p_sys->p_thread->b_die = true;*/
293     vlc_object_kill( p_sys->p_thread );
294     block_FifoWake( p_sys->p_thread->p_fifo_input );
295
296     vlc_thread_join( p_sys->p_thread );
297
298     vlc_cond_destroy( &p_sys->p_thread->wait );
299     vlc_mutex_destroy( &p_sys->p_thread->lock );
300
301     block_FifoRelease( p_sys->p_thread->p_fifo_input );
302     block_FifoRelease( p_sys->p_thread->p_empty_blocks );
303
304     for( i = 0; i < 64; i++ ) /* RTMP_HEADER_STREAM_INDEX_MASK */
305     {
306         if( p_sys->p_thread->rtmp_headers_recv[i].body != NULL )
307         {
308             free( p_sys->p_thread->rtmp_headers_recv[i].body->body );
309             free( p_sys->p_thread->rtmp_headers_recv[i].body );
310         }
311     }
312
313     net_Close( p_sys->p_thread->fd );
314
315     var_Destroy( p_access, "rtmp-caching" );
316
317     vlc_UrlClean( &p_sys->p_thread->url );
318     free( p_sys->p_thread->psz_application );
319     free( p_sys->p_thread->psz_media );
320     free( p_sys->p_thread->psz_swf_url );
321     free( p_sys->p_thread->psz_page_url );
322
323     vlc_object_detach( p_sys->p_thread );
324     vlc_object_release( p_sys->p_thread );
325     free( p_sys );
326 }
327
328 /*****************************************************************************
329  * Read: standard read on a file descriptor.
330  *****************************************************************************/
331 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
332 {
333     access_sys_t *p_sys = p_access->p_sys;
334     rtmp_packet_t *rtmp_packet;
335     uint8_t *tmp_buffer;
336     ssize_t i_ret;
337     size_t i_len_tmp;
338
339     i_len_tmp = 0;
340
341     while( i_len_tmp < i_len )
342     {
343         if( p_sys->p_thread->result_stop || p_access->info.b_eof || !vlc_object_alive (p_access) )
344         {
345             p_access->info.b_eof = true;
346             return 0;
347         }
348
349         if( p_sys->read_packet )
350         {
351             if( !p_sys->p_thread->metadata_received )
352             {
353                 /* Wait until enough data is received for extracting metadata */
354                 if( block_FifoCount( p_sys->p_thread->p_fifo_input ) < 10 )
355                 {
356                     msleep(100000);
357                     continue;
358                 }
359
360                 p_sys->flv_packet = flv_get_metadata( p_access );
361
362                 p_sys->p_thread->metadata_received = 1;
363             }
364             else
365             {
366                 p_sys->flv_packet = block_FifoGet( p_sys->p_thread->p_fifo_input );
367                 if( p_sys->flv_packet == NULL )
368                     continue; /* Forced wake-up */
369             }
370
371             if( p_sys->p_thread->first_media_packet )
372             {
373                 p_sys->flv_packet = flv_insert_header( p_access, p_sys->flv_packet );
374
375                 p_sys->p_thread->first_media_packet = 0;
376             }
377         }
378         if( i_len - i_len_tmp >= p_sys->flv_packet->i_buffer )
379         {
380             p_sys->read_packet = 1;
381
382             memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, p_sys->flv_packet->i_buffer );
383             block_FifoPut( p_sys->p_thread->p_empty_blocks, p_sys->flv_packet );
384
385             i_len_tmp += p_sys->flv_packet->i_buffer;
386         }
387         else
388         {
389             p_sys->read_packet = 0;
390
391             memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, i_len - i_len_tmp);
392             p_sys->flv_packet->i_buffer -= i_len - i_len_tmp;
393             memmove( p_sys->flv_packet->p_buffer, p_sys->flv_packet->p_buffer + i_len - i_len_tmp, p_sys->flv_packet->i_buffer );
394
395             i_len_tmp += i_len - i_len_tmp;
396         }
397     }
398     if( i_len_tmp > 0 )
399     {
400         if( p_sys->p_thread->result_publish )
401         {
402             /* Send publish onStatus event only once */
403             p_sys->p_thread->result_publish = 0;
404
405             rtmp_packet = rtmp_build_publish_start( p_sys->p_thread );
406
407             tmp_buffer = rtmp_encode_packet( p_sys->p_thread, rtmp_packet );
408
409             i_ret = net_Write( p_sys->p_thread, p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
410             if( i_ret != rtmp_packet->length_encoded )
411             {
412                 free( rtmp_packet->body->body );
413                 free( rtmp_packet->body );
414                 free( rtmp_packet );
415                 free( tmp_buffer );
416                 msg_Err( p_access, "failed send publish start" );
417                 return -1;
418             }
419             free( rtmp_packet->body->body );
420             free( rtmp_packet->body );
421             free( rtmp_packet );
422             free( tmp_buffer );
423         }
424
425         p_access->info.i_pos += i_len_tmp;
426
427         rtmp_packet = rtmp_build_bytes_read( p_sys->p_thread, p_access->info.i_pos );
428
429         tmp_buffer = rtmp_encode_packet( p_sys->p_thread, rtmp_packet );
430  
431         i_ret = net_Write( p_sys->p_thread, p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
432         if( i_ret != rtmp_packet->length_encoded )
433         {
434             free( rtmp_packet->body->body );
435             free( rtmp_packet->body );
436             free( rtmp_packet );
437             free( tmp_buffer );
438             msg_Err( p_access, "failed send bytes read" );
439             return -1;
440         }
441         free( rtmp_packet->body->body );
442         free( rtmp_packet->body );
443         free( rtmp_packet );
444         free( tmp_buffer );
445     }
446
447     return i_len_tmp;
448 }
449
450 /*****************************************************************************
451  * Seek: seek to a specific location in a file
452  *****************************************************************************/
453 static int Seek( access_t *p_access, int64_t i_pos )
454 {
455     VLC_UNUSED( p_access );
456     VLC_UNUSED( i_pos );
457 /*msg_Warn ( p_access, "Seek to %lld", i_pos);
458     switch( rtmp_seek( p_access, i_pos ) )
459     {
460         case -1:
461             return VLC_EGENERIC;
462         case 0:
463             break;
464         default:
465             msg_Err( p_access, "You should not be here" );
466             abort();
467     }
468 */
469     return VLC_EGENERIC;
470 }
471
472 /*****************************************************************************
473  * Control:
474  *****************************************************************************/
475 static int Control( access_t *p_access, int i_query, va_list args )
476 {
477     bool    *pb_bool;
478     int64_t *pi_64;
479
480     switch( i_query )
481     {
482         /* */
483         case ACCESS_CAN_SEEK:
484         case ACCESS_CAN_FASTSEEK:
485             pb_bool = (bool*) va_arg( args, bool* );
486             *pb_bool = false; /* TODO */
487             break;
488
489         case ACCESS_CAN_PAUSE:
490             pb_bool = (bool*) va_arg( args, bool* );
491             *pb_bool = false; /* TODO */
492             break;
493
494         case ACCESS_CAN_CONTROL_PACE:
495             pb_bool = (bool*) va_arg( args, bool* );
496             *pb_bool = true;
497             break;
498
499         /* */
500         case ACCESS_GET_PTS_DELAY:
501             pi_64 = (int64_t*) va_arg( args, int64_t * );
502             *pi_64 = var_GetInteger( p_access, "rtmp-caching" ) * INT64_C(1000);
503             break;
504
505         /* */
506         case ACCESS_SET_PAUSE_STATE:
507             /* Nothing to do */
508             break;
509
510         case ACCESS_GET_TITLE_INFO:
511         case ACCESS_SET_TITLE:
512         case ACCESS_SET_SEEKPOINT:
513         case ACCESS_SET_PRIVATE_ID_STATE:
514         case ACCESS_GET_META:
515         case ACCESS_GET_CONTENT_TYPE: /* DOWN: comment this line */
516             return VLC_EGENERIC;
517
518         default:
519             msg_Warn( p_access, "unimplemented query in control" );
520             return VLC_EGENERIC;
521     }
522
523     return VLC_SUCCESS;
524 }
525
526 /*****************************************************************************
527  * ThreadControl: manage control messages and pipe media to Read
528  *****************************************************************************/
529 static void* ThreadControl( vlc_object_t *p_this )
530 {
531     rtmp_control_thread_t *p_thread = (rtmp_control_thread_t *) p_this;
532     rtmp_packet_t *rtmp_packet;
533     int canc = vlc_savecancel ();
534
535     rtmp_init_handler( p_thread->rtmp_handler );
536
537     while( vlc_object_alive (p_thread) )
538     {
539         rtmp_packet = rtmp_read_net_packet( p_thread );
540         if( rtmp_packet != NULL )
541         {
542             if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
543                 || rtmp_packet->content_type > 0x14 ) /* RTMP_CONTENT_TYPE_INVOKE */
544             {
545                 free( rtmp_packet->body->body );
546                 free( rtmp_packet->body );
547                 free( rtmp_packet );
548
549                 msg_Warn( p_thread, "unknown content type received" );
550             }
551             else
552                 p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet );
553         }
554         else
555         {
556             /* Sometimes server close connection too soon */
557             if( p_thread->result_connect )
558             {
559                 vlc_mutex_lock( &p_thread->lock );
560                 vlc_cond_signal( &p_thread->wait );
561                 vlc_mutex_unlock( &p_thread->lock );
562             }
563
564             p_thread->b_die = 1;
565             ((access_t *) p_thread->p_base_object)->info.b_eof = true;
566
567             block_FifoWake( p_thread->p_fifo_input );
568         }
569     }
570     vlc_restorecancel (canc);
571     return NULL;
572 }