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