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