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