]> git.sesse.net Git - vlc/blob - modules/access_output/rtmp.c
http output: kill config_Get (commented out anyway)
[vlc] / modules / access_output / rtmp.c
1 /*****************************************************************************
2  * rtmp.c: RTMP output.
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_sout.h>
33
34 #include <vlc_network.h> /* DOWN: #include <network.h> */
35 #include <vlc_url.h>
36 #include <vlc_block.h>
37
38 #include "../access/rtmp/rtmp_amf_flv.h"
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43
44 #define RTMP_CONNECT_TEXT N_( "Active TCP connection" )
45 #define RTMP_CONNECT_LONGTEXT N_( \
46     "If enabled, VLC will connect to a remote destination instead of " \
47     "waiting for an incoming connection." )
48
49 static int  Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
51
52 #define SOUT_CFG_PREFIX "sout-rtmp-"
53
54 vlc_module_begin ()
55     set_description( N_("RTMP stream output") )
56     set_shortname( N_("RTMP" ) )
57     set_capability( "sout access", 0 )
58     set_category( CAT_SOUT )
59     set_subcategory( SUBCAT_SOUT_STREAM )
60     add_shortcut( "rtmp" )
61     set_callbacks( Open, Close )
62     add_bool( "rtmp-connect", false, NULL, RTMP_CONNECT_TEXT,
63               RTMP_CONNECT_LONGTEXT, false )
64 vlc_module_end ()
65
66 /*****************************************************************************
67  * Local prototypes
68  *****************************************************************************/
69 static ssize_t Write( sout_access_out_t *, block_t * );
70 static int     Seek ( sout_access_out_t *, off_t  );
71 static void* ThreadControl( void * );
72
73 struct sout_access_out_sys_t
74 {
75     int active;
76
77     /* thread for filtering and handling control messages */
78     rtmp_control_thread_t *p_thread;
79 };
80
81 /*****************************************************************************
82  * Open: open the rtmp connection
83  *****************************************************************************/
84 static int Open( vlc_object_t *p_this )
85 {
86     sout_access_out_t *p_access = (sout_access_out_t *) p_this;
87     sout_access_out_sys_t *p_sys;
88     char *psz, *p;
89     int length_path, length_media_name;
90     int i;
91
92     if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
93         return VLC_ENOMEM;
94     p_access->p_sys = p_sys;
95
96     p_sys->p_thread =
97         vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) );
98     if( !p_sys->p_thread )
99     {
100         free( p_sys );
101         return VLC_ENOMEM;
102     }
103     vlc_object_attach( p_sys->p_thread, p_access );
104
105     /* Parse URI - remove spaces */
106     p = psz = strdup( p_access->psz_path );
107     while( ( p = strchr( p, ' ' ) ) != NULL )
108         *p = '+';
109     vlc_UrlParse( &p_sys->p_thread->url, psz, 0 );
110     free( psz );
111
112     if( p_sys->p_thread->url.psz_host == NULL
113         || *p_sys->p_thread->url.psz_host == '\0' )
114     {
115          msg_Warn( p_access, "invalid host" );
116          goto error;
117     }
118
119     if( p_sys->p_thread->url.i_port <= 0 )
120         p_sys->p_thread->url.i_port = 1935;
121
122     if ( p_sys->p_thread->url.psz_path == NULL )
123     {
124         msg_Warn( p_access, "invalid path" );
125         goto error;
126     }
127
128     length_path = strlen( p_sys->p_thread->url.psz_path );
129     char* psz_tmp = strrchr( p_sys->p_thread->url.psz_path, '/' );
130     if( !psz_tmp )
131         goto error;
132     length_media_name = strlen( psz_tmp ) - 1;
133
134     p_sys->p_thread->psz_application = strndup( p_sys->p_thread->url.psz_path + 1, length_path - length_media_name - 2 );
135     p_sys->p_thread->psz_media = strdup( p_sys->p_thread->url.psz_path + ( length_path - length_media_name ) );
136
137     msg_Dbg( p_access, "rtmp: host='%s' port=%d path='%s'",
138              p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port, p_sys->p_thread->url.psz_path );
139
140     if( p_sys->p_thread->url.psz_username && *p_sys->p_thread->url.psz_username )
141     {
142         msg_Dbg( p_access, "      user='%s'", p_sys->p_thread->url.psz_username );
143     }
144
145     /* Initialize thread variables */
146     p_sys->p_thread->b_error= 0;
147     p_sys->p_thread->p_fifo_input = block_FifoNew();
148     p_sys->p_thread->p_empty_blocks = block_FifoNew();
149     p_sys->p_thread->has_audio = 0;
150     p_sys->p_thread->has_video = 0;
151     p_sys->p_thread->metadata_received = 0;
152     p_sys->p_thread->first_media_packet = 1;
153     p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; /* FLV_TAG_FIRST_PREVIOUS_TAG_SIZE */
154
155     p_sys->p_thread->flv_body = rtmp_body_new( -1 );
156     p_sys->p_thread->flv_length_body = 0;
157
158     p_sys->p_thread->chunk_size_recv = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
159     p_sys->p_thread->chunk_size_send = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
160     for(i = 0; i < 64; i++)
161     {
162         memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) );
163         p_sys->p_thread->rtmp_headers_send[i].length_header = -1;
164         p_sys->p_thread->rtmp_headers_send[i].stream_index = -1;
165         p_sys->p_thread->rtmp_headers_send[i].timestamp = -1;
166         p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1;
167         p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1;
168         p_sys->p_thread->rtmp_headers_send[i].length_body = -1;
169         p_sys->p_thread->rtmp_headers_send[i].content_type = -1;
170         p_sys->p_thread->rtmp_headers_send[i].src_dst = -1;
171         p_sys->p_thread->rtmp_headers_send[i].body = NULL;
172     }
173
174     vlc_cond_init( &p_sys->p_thread->wait );
175     vlc_mutex_init( &p_sys->p_thread->lock );
176
177     p_sys->p_thread->result_connect = 1;
178     /* p_sys->p_thread->result_publish = only used on access */
179     p_sys->p_thread->result_play = 1;
180     p_sys->p_thread->result_stop = 0;
181     p_sys->p_thread->fd = -1;
182
183     /* Open connection */
184     if( var_CreateGetBool( p_access, "rtmp-connect" ) > 0 )
185     {
186 #if 0
187         p_sys->p_thread->fd = net_ConnectTCP( p_access,
188                                               p_sys->p_thread->url.psz_host,
189                                               p_sys->p_thread->url.i_port );
190 #endif
191         msg_Err( p_access, "to be implemented" );
192         goto error2;
193     }
194     else
195     {
196         int *p_fd_listen;
197
198         p_sys->active = 0;
199         p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host,
200                                      p_sys->p_thread->url.i_port );
201         if( p_fd_listen == NULL )
202         {
203             msg_Warn( p_access, "cannot listen to %s port %i",
204                       p_sys->p_thread->url.psz_host,
205                       p_sys->p_thread->url.i_port );
206             goto error2;
207         }
208
209         do
210             p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen );
211         while( p_sys->p_thread->fd == -1 );
212         net_ListenClose( p_fd_listen );
213
214         if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 )
215         {
216             msg_Err( p_access, "handshake passive failed");
217             goto error2;
218         }
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_passive( p_sys->p_thread ) < 0 )
231         {
232             msg_Err( p_access, "connect passive failed");
233             vlc_cancel( p_sys->p_thread->thread );
234             vlc_join( p_sys->p_thread->thread, NULL );
235             goto error2;
236         }
237     }
238
239     p_access->pf_write = Write;
240     p_access->pf_seek = Seek;
241
242     return VLC_SUCCESS;
243
244 error2:
245     vlc_cond_destroy( &p_sys->p_thread->wait );
246     vlc_mutex_destroy( &p_sys->p_thread->lock );
247
248     free( p_sys->p_thread->psz_application );
249     free( p_sys->p_thread->psz_media );
250
251     if( p_sys->p_thread->fd != -1 )
252         net_Close( p_sys->p_thread->fd );
253 error:
254     vlc_UrlClean( &p_sys->p_thread->url );
255     vlc_object_detach( p_sys->p_thread );
256     vlc_object_release( p_sys->p_thread );
257     free( p_sys );
258
259     return VLC_EGENERIC;
260 }
261
262 /*****************************************************************************
263  * Close: close the target
264  *****************************************************************************/
265 static void Close( vlc_object_t * p_this )
266 {
267     sout_access_out_t *p_access = (sout_access_out_t *) p_this;
268     sout_access_out_sys_t *p_sys = p_access->p_sys;
269     int i;
270
271     vlc_cancel( p_sys->p_thread->thread );
272     vlc_join( p_sys->p_thread->thread, NULL );
273
274     vlc_cond_destroy( &p_sys->p_thread->wait );
275     vlc_mutex_destroy( &p_sys->p_thread->lock );
276
277     block_FifoRelease( p_sys->p_thread->p_fifo_input );
278     block_FifoRelease( p_sys->p_thread->p_empty_blocks );
279
280     for( i = 0; i < 64; i++ ) /* RTMP_HEADER_STREAM_INDEX_MASK */
281     {
282         if( p_sys->p_thread->rtmp_headers_recv[i].body != NULL )
283         {
284             free( p_sys->p_thread->rtmp_headers_recv[i].body->body );
285             free( p_sys->p_thread->rtmp_headers_recv[i].body );
286         }
287     }
288
289     net_Close( p_sys->p_thread->fd );
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  * Write: standard write on a file descriptor.
302  *****************************************************************************/
303 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
304 {
305     rtmp_packet_t *rtmp_packet;
306     uint8_t *tmp_buffer;
307     ssize_t i_ret;
308     ssize_t i_write = 0;
309
310     if( p_access->p_sys->p_thread->first_media_packet )
311     {
312         /* 13 == FLV_HEADER_SIZE + PreviousTagSize*/
313         memmove( p_buffer->p_buffer, p_buffer->p_buffer + 13, p_buffer->i_buffer - 13 );
314         p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer - 13 );
315
316         p_access->p_sys->p_thread->first_media_packet = 0;
317     }
318
319     while( p_buffer )
320     {
321         block_t *p_next = p_buffer->p_next;
322 //////////////////////////////
323 /*msg_Warn(p_access, "XXXXXXXXXXXXXXXXX");
324 int i;
325 for(i = 0; i < p_buffer->i_buffer; i += 16)
326 {
327     msg_Warn(p_access,"%.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x",
328 p_buffer->p_buffer[i], p_buffer->p_buffer[i+1], p_buffer->p_buffer[i+2], p_buffer->p_buffer[i+3], p_buffer->p_buffer[i+4], p_buffer->p_buffer[i+5], p_buffer->p_buffer[i+6], p_buffer->p_buffer[i+7],
329 p_buffer->p_buffer[i+8], p_buffer->p_buffer[i+9], p_buffer->p_buffer[i+10], p_buffer->p_buffer[i+11], p_buffer->p_buffer[i+12], p_buffer->p_buffer[i+13], p_buffer->p_buffer[i+14], p_buffer->p_buffer[i+15]);
330 }*/
331 ////////////////////////
332         msg_Warn(p_access, "rtmp.c:360 i_dts %"PRIu64" i_pts %"PRIu64,
333                  p_buffer->i_dts, p_buffer->i_pts);
334         rtmp_packet = rtmp_build_flv_over_rtmp( p_access->p_sys->p_thread, p_buffer );
335
336         if( rtmp_packet )
337         {
338             tmp_buffer = rtmp_encode_packet( p_access->p_sys->p_thread, rtmp_packet );
339
340             i_ret = net_Write( p_access->p_sys->p_thread, p_access->p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
341             if( i_ret != rtmp_packet->length_encoded )
342             {
343                 free( rtmp_packet->body->body );
344                 free( rtmp_packet->body );
345                 free( rtmp_packet );
346                 free( tmp_buffer );
347                 msg_Err( p_access->p_sys->p_thread, "failed send flv packet" );
348                 return -1;
349             }
350             free( rtmp_packet->body->body );
351             free( rtmp_packet->body );
352             free( rtmp_packet );
353             free( tmp_buffer );
354         }
355
356         i_write += p_buffer->i_buffer;
357
358         p_buffer = p_next;
359     }
360
361     return i_write;
362 }
363
364 /********************a*********************************************************
365  * Seek: seek to a specific location in a file
366  *****************************************************************************/
367 static int Seek( sout_access_out_t *p_access, off_t i_pos )
368 {
369     (void)i_pos;
370     msg_Err( p_access, "RTMP sout access cannot seek" );
371     return -1;
372 }
373
374 /*****************************************************************************
375  * ThreadControl: manage control messages and pipe media to Read
376  *****************************************************************************/
377 static void* ThreadControl( void *p_this )
378 {
379     rtmp_control_thread_t *p_thread = p_this;
380     rtmp_packet_t *rtmp_packet;
381     int canc = vlc_savecancel ();
382
383     rtmp_init_handler( p_thread->rtmp_handler );
384
385     for( ;; )
386     {
387         vlc_restorecancel( canc );
388         rtmp_packet = rtmp_read_net_packet( p_thread );
389         canc = vlc_savecancel( );
390         if( rtmp_packet != NULL )
391         {
392             if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
393                 || rtmp_packet->content_type > 0x14 ) /* RTMP_CONTENT_TYPE_INVOKE */
394             {
395                 free( rtmp_packet->body->body );
396                 free( rtmp_packet->body );
397                 free( rtmp_packet );
398
399                 msg_Warn( p_thread, "unknown content type received" );
400             }
401             else
402                 p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet );
403         }
404         else
405         {
406             /* Sometimes server close connection too soon */
407 #warning Locking bug here.
408             if( p_thread->result_connect )
409             {
410                 vlc_mutex_lock( &p_thread->lock );
411                 vlc_cond_signal( &p_thread->wait );
412                 vlc_mutex_unlock( &p_thread->lock );
413             }
414             break;
415         }
416     }
417     vlc_restorecancel (canc);
418     return NULL;
419 }