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