]> git.sesse.net Git - vlc/blob - modules/access/rtmp/access.c
Merge branch 'master' of git@git.videolan.org:vlc
[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     /* 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, 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
281     msg_Warn(p_access, "Close");
282
283     vlc_object_kill( p_sys->p_thread );
284     block_FifoWake( p_sys->p_thread->p_fifo_media );
285     block_FifoWake( p_sys->p_thread->p_empty_blocks );
286     /*
287     for( i = 0; i < 5; i++ )
288     {
289         block_t *p_dummy = block_New( p_access, 256 );
290         p_dummy->i_dts = 0;
291         p_dummy->i_pts = 0;
292         p_dummy->i_length = 0;
293         memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
294         block_FifoPut( p_sys->p_thread->p_fifo_media, p_dummy );
295     }
296     for( i = 0; i < 5; i++ )
297     {
298         block_t *p_dummy = block_New( p_access, 256 );
299         p_dummy->i_dts = 0;
300         p_dummy->i_pts = 0;
301         p_dummy->i_length = 0;
302         memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
303         block_FifoPut( p_sys->p_thread->p_empty_blocks, p_dummy );
304     }*/
305     vlc_thread_join( p_sys->p_thread );
306
307     vlc_cond_destroy( &p_sys->p_thread->wait );
308     vlc_mutex_destroy( &p_sys->p_thread->lock );
309
310     block_FifoRelease( p_sys->p_thread->p_fifo_media );
311     block_FifoRelease( p_sys->p_thread->p_empty_blocks );
312
313     net_Close( p_sys->fd );
314
315     var_Destroy( p_access, "rtmp-caching" );
316
317
318     vlc_UrlClean( &p_sys->url );
319     free( p_sys->psz_application );
320     free( p_sys->psz_media );
321     free( p_sys );
322 }
323
324 /*****************************************************************************
325  * Read: standard read on a file descriptor.
326  *****************************************************************************/
327 static int Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
328 {
329     access_sys_t *p_sys = p_access->p_sys;
330     int i_len_tmp;
331
332     if( p_sys->fd < 0 )
333     {
334         p_access->info.b_eof = true;
335         return 0;
336     }
337
338     i_len_tmp = 0;
339
340     while( i_len_tmp < i_len )
341     {
342         if( p_sys->read_packet )
343         {
344             if( !p_sys->p_thread->metadata_received )
345             {
346                 p_sys->flv_packet = flv_get_metadata( p_access );
347
348                 p_sys->p_thread->metadata_received = 1;
349             }
350             else
351             {
352                 if( p_sys->active && block_FifoCount( p_sys->p_thread->p_fifo_media ) == 0 )
353                 {
354                     p_access->info.b_eof = true;
355                     break;
356                 }
357
358                 p_sys->flv_packet = block_FifoGet( p_sys->p_thread->p_fifo_media );
359                 if( p_sys->flv_packet == NULL )
360                     continue; /* Forced wake-up */
361             }
362
363             if( p_sys->p_thread->first_media_packet )
364             {
365                 p_sys->flv_packet = flv_insert_header( p_access, p_sys->flv_packet );
366
367                 p_sys->p_thread->first_media_packet = 0;
368             }
369         }
370         if( i_len - i_len_tmp >= p_sys->flv_packet->i_buffer )
371         {
372             p_sys->read_packet = 1;
373
374             memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, p_sys->flv_packet->i_buffer );
375             block_FifoPut( p_sys->p_thread->p_empty_blocks, p_sys->flv_packet );
376
377             i_len_tmp += p_sys->flv_packet->i_buffer;
378         }
379         else
380         {
381             p_sys->read_packet = 0;
382
383             memcpy( p_buffer + i_len_tmp, p_sys->flv_packet->p_buffer, i_len - i_len_tmp);
384             p_sys->flv_packet->i_buffer -= i_len - i_len_tmp;
385             memmove( p_sys->flv_packet->p_buffer, p_sys->flv_packet->p_buffer + i_len - i_len_tmp, p_sys->flv_packet->i_buffer );
386
387             i_len_tmp += i_len - i_len_tmp;
388         }
389     }
390 /*int i;
391 for(i = 0; i < i_len_tmp; i += 16)
392 {
393     msg_Warn(p_access,"%.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x",
394 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],
395 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]);
396 }*/
397     if( i_len_tmp > 0 ) {
398         if( p_sys->p_thread->result_publish )
399         {
400             /* Send publish onStatus event only once */
401             p_sys->p_thread->result_publish = 0;
402
403             rtmp_send_publish_start( p_access );
404         }
405
406         p_access->info.i_pos += i_len_tmp;
407
408         rtmp_send_bytes_read( p_access, p_access->info.i_pos );
409     }
410
411     return i_len_tmp;
412 }
413
414 /*****************************************************************************
415  * Seek: seek to a specific location in a file
416  *****************************************************************************/
417 static int Seek( access_t *p_access, int64_t i_pos )
418 {
419 /*msg_Warn ( p_access, "Seek to %lld", i_pos);
420     switch( rtmp_seek( p_access, i_pos ) )
421     {
422         case -1:
423             return VLC_EGENERIC;
424         case 0:
425             break;
426         default:
427             msg_Err( p_access, "You should not be here" );
428             abort();
429     }
430 */
431     return VLC_EGENERIC;
432 }
433
434 /*****************************************************************************
435  * Control:
436  *****************************************************************************/
437 static int Control( access_t *p_access, int i_query, va_list args )
438 {
439     bool   *pb_bool;
440     int          *pi_int;
441     int64_t      *pi_64;
442
443     switch( i_query )
444     {
445         /* */
446         case ACCESS_CAN_SEEK:
447         case ACCESS_CAN_FASTSEEK:
448             pb_bool = (bool*)va_arg( args, bool* );
449             *pb_bool = false; /* TODO */
450             break;
451
452         case ACCESS_CAN_PAUSE:
453             pb_bool = (bool*)va_arg( args, bool* );
454             *pb_bool = false; /* TODO */
455             break;
456
457         case ACCESS_CAN_CONTROL_PACE:
458             pb_bool = (bool*)va_arg( args, bool* );
459             *pb_bool = true;
460             break;
461
462         /* */
463         case ACCESS_GET_MTU:
464             pi_int = (int*)va_arg( args, int * );
465             *pi_int = 0;
466             break;
467
468         case ACCESS_GET_PTS_DELAY:
469             pi_64 = (int64_t*)va_arg( args, int64_t * );
470             *pi_64 = var_GetInteger( p_access, "rtmp-caching" ) * I64C(1000);
471             break;
472
473         /* */
474         case ACCESS_SET_PAUSE_STATE:
475             /* Nothing to do */
476             break;
477
478         case ACCESS_GET_TITLE_INFO:
479         case ACCESS_SET_TITLE:
480         case ACCESS_SET_SEEKPOINT:
481         case ACCESS_SET_PRIVATE_ID_STATE:
482         case ACCESS_GET_META:
483         case ACCESS_GET_CONTENT_TYPE: /* DOWN: comment this line */
484             return VLC_EGENERIC;
485
486         default:
487             msg_Warn( p_access, "unimplemented query in control" );
488             return VLC_EGENERIC;
489     }
490
491     return VLC_SUCCESS;
492 }
493
494 /*****************************************************************************
495  * ThreadControl: manage control messages and pipe media to Read
496  *****************************************************************************/
497 static void ThreadControl( vlc_object_t *p_this )
498 {
499     rtmp_control_thread_t *p_thread = (rtmp_control_thread_t *) p_this;
500     rtmp_packet_t *rtmp_packet;
501
502     rtmp_init_handler( p_thread->rtmp_handler );
503
504     while( !p_thread->b_die )
505     {
506
507         rtmp_packet = rtmp_read_net_packet( p_thread );
508         if( rtmp_packet != NULL )
509         {
510             if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
511                 || rtmp_packet->content_type > 0x14 ) /* RTMP_CONTENT_TYPE_INVOKE */
512                 msg_Warn( p_thread, "unknown content type received" );
513             else
514                 p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet );
515         }
516         else
517         {
518             /* Sometimes server close connection too soon */
519             if( p_thread->result_connect )
520             {
521                 vlc_mutex_lock( &p_thread->lock );
522                 vlc_cond_signal( &p_thread->wait );
523                 vlc_mutex_unlock( &p_thread->lock );
524             }
525
526             p_thread->b_die = 1;
527         }
528     }
529 }