Use the right declaration for threaded functions.
[vlc.git] / 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", 50 );
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( vlc_object_t * );
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     {
94         msg_Err( p_access, "not enough memory" );
95         return VLC_ENOMEM;
96     }
97     p_access->p_sys = p_sys;
98
99     p_sys->p_thread =
100         vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) );
101     if( !p_sys->p_thread )
102     {
103         msg_Err( p_access, "out of memory" );
104         return VLC_ENOMEM;
105     }
106     vlc_object_attach( p_sys->p_thread, p_access );
107
108     /* Parse URI - remove spaces */
109     p = psz = strdup( p_access->psz_path );
110     while( ( p = strchr( p, ' ' ) ) != NULL )
111         *p = '+';
112     vlc_UrlParse( &p_sys->p_thread->url, psz, 0 );
113     free( psz );
114
115     if( p_sys->p_thread->url.psz_host == NULL
116         || *p_sys->p_thread->url.psz_host == '\0' )
117     {
118          msg_Warn( p_access, "invalid host" );
119          goto error;
120     }
121
122     if( p_sys->p_thread->url.i_port <= 0 )
123         p_sys->p_thread->url.i_port = 1935;
124
125     if ( p_sys->p_thread->url.psz_path == NULL )
126     {
127         msg_Warn( p_access, "invalid path" );
128         goto error;
129     }
130
131     length_path = strlen( p_sys->p_thread->url.psz_path );
132     length_media_name = strlen( strrchr( p_sys->p_thread->url.psz_path, '/' ) ) - 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', pwd='%s'",
143                  p_sys->p_thread->url.psz_username, p_sys->p_thread->url.psz_password );
144     }
145
146     /* Initialize thread variables */
147     p_sys->p_thread->b_die = 0;
148     p_sys->p_thread->b_error= 0;
149     p_sys->p_thread->p_fifo_input = block_FifoNew();
150     p_sys->p_thread->p_empty_blocks = block_FifoNew();
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
157     p_sys->p_thread->flv_body = rtmp_body_new( -1 );
158     p_sys->p_thread->flv_length_body = 0;
159
160     p_sys->p_thread->chunk_size_recv = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
161     p_sys->p_thread->chunk_size_send = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
162     for(i = 0; i < 64; i++)
163     {
164         memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) );
165         p_sys->p_thread->rtmp_headers_send[i].length_header = -1;
166         p_sys->p_thread->rtmp_headers_send[i].stream_index = -1;
167         p_sys->p_thread->rtmp_headers_send[i].timestamp = -1;
168         p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1;
169         p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1;
170         p_sys->p_thread->rtmp_headers_send[i].length_body = -1;
171         p_sys->p_thread->rtmp_headers_send[i].content_type = -1;
172         p_sys->p_thread->rtmp_headers_send[i].src_dst = -1;
173         p_sys->p_thread->rtmp_headers_send[i].body = NULL;
174     }
175
176     vlc_cond_init( p_sys->p_thread, &p_sys->p_thread->wait );
177     vlc_mutex_init( &p_sys->p_thread->lock );
178
179     p_sys->p_thread->result_connect = 1;
180     /* p_sys->p_thread->result_publish = only used on access */
181     p_sys->p_thread->result_play = 1;
182     p_sys->p_thread->result_stop = 0;
183     p_sys->p_thread->fd = -1;
184
185     /* Open connection */
186     if( var_CreateGetBool( p_access, "rtmp-connect" ) > 0 )
187     {
188 #if 0
189         p_sys->p_thread->fd = net_ConnectTCP( p_access,
190                                               p_sys->p_thread->url.psz_host,
191                                               p_sys->p_thread->url.i_port );
192 #endif
193         msg_Err( p_access, "to be implemented" );
194         goto error2;
195     }
196     else
197     {
198         int *p_fd_listen;
199
200         p_sys->active = 0;
201         p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host,
202                                      p_sys->p_thread->url.i_port );
203         if( p_fd_listen == NULL )
204         {
205             msg_Warn( p_access, "cannot listen to %s port %i",
206                       p_sys->p_thread->url.psz_host,
207                       p_sys->p_thread->url.i_port );
208             goto error2;
209         }
210
211         do
212             p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen, -1 );
213         while( p_sys->p_thread->fd == -1 );
214         net_ListenClose( p_fd_listen );
215
216         if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 )
217         {
218             msg_Err( p_access, "handshake passive failed");
219             goto error2;
220         }
221     }
222
223     if( vlc_thread_create( p_sys->p_thread, "rtmp control thread", ThreadControl,
224                            VLC_THREAD_PRIORITY_INPUT, false ) )
225     {
226         msg_Err( p_access, "cannot spawn rtmp control thread" );
227         goto error2;
228     }
229
230     if( !p_sys->active )
231     {
232         if( rtmp_connect_passive( p_sys->p_thread ) < 0 )
233         {
234             msg_Err( p_access, "connect passive failed");
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_object_detach( p_sys->p_thread );
255     vlc_object_release( p_sys->p_thread );
256
257     vlc_UrlClean( &p_sys->p_thread->url );
258     free( p_sys );
259
260     return VLC_EGENERIC;
261 }
262
263 /*****************************************************************************
264  * Close: close the target
265  *****************************************************************************/
266 static void Close( vlc_object_t * p_this )
267 {
268     sout_access_out_t *p_access = (sout_access_out_t *) p_this;
269     sout_access_out_sys_t *p_sys = p_access->p_sys;
270     int i;
271
272 //    p_sys->p_thread->b_die = true;
273     vlc_object_kill( p_sys->p_thread );
274     block_FifoWake( p_sys->p_thread->p_fifo_input );
275     block_FifoWake( p_sys->p_thread->p_empty_blocks );
276
277     vlc_thread_join( p_sys->p_thread );
278
279     vlc_cond_destroy( &p_sys->p_thread->wait );
280     vlc_mutex_destroy( &p_sys->p_thread->lock );
281
282     block_FifoRelease( p_sys->p_thread->p_fifo_input );
283     block_FifoRelease( p_sys->p_thread->p_empty_blocks );
284
285     for( i = 0; i < 64; i++ ) /* RTMP_HEADER_STREAM_INDEX_MASK */
286     {
287         if( p_sys->p_thread->rtmp_headers_recv[i].body != NULL )
288         {
289             free( p_sys->p_thread->rtmp_headers_recv[i].body->body );
290             free( p_sys->p_thread->rtmp_headers_recv[i].body );
291         }
292     }
293
294     net_Close( p_sys->p_thread->fd );
295
296     vlc_object_detach( p_sys->p_thread );
297     vlc_object_release( p_sys->p_thread );
298
299     vlc_UrlClean( &p_sys->p_thread->url );
300     free( p_sys->p_thread->psz_application );
301     free( p_sys->p_thread->psz_media );
302     free( p_sys );
303 }
304
305 /*****************************************************************************
306  * Write: standard write on a file descriptor.
307  *****************************************************************************/
308 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
309 {
310     rtmp_packet_t *rtmp_packet;
311     uint8_t *tmp_buffer;
312     ssize_t i_ret;
313     ssize_t i_write = 0;
314
315     if( p_access->p_sys->p_thread->first_media_packet )
316     {
317         /* 13 == FLV_HEADER_SIZE + PreviousTagSize*/
318         memmove( p_buffer->p_buffer, p_buffer->p_buffer + 13, p_buffer->i_buffer - 13 );
319         p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer - 13 );
320
321         p_access->p_sys->p_thread->first_media_packet = 0;
322     }
323
324     while( p_buffer )
325     {
326         block_t *p_next = p_buffer->p_next;
327 //////////////////////////////
328 /*msg_Warn(p_access, "XXXXXXXXXXXXXXXXX");
329 int i;
330 for(i = 0; i < p_buffer->i_buffer; i += 16)
331 {
332     msg_Warn(p_access,"%.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x",
333 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],
334 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]);
335 }*/
336 ////////////////////////
337         msg_Warn(p_access, "rtmp.c:360 i_dts %"PRIu64" i_pts %"PRIu64,
338                  p_buffer->i_dts, p_buffer->i_pts);
339         rtmp_packet = rtmp_build_flv_over_rtmp( p_access->p_sys->p_thread, p_buffer );
340
341         if( rtmp_packet )
342         {
343             tmp_buffer = rtmp_encode_packet( p_access->p_sys->p_thread, rtmp_packet );
344
345             i_ret = net_Write( p_access->p_sys->p_thread, p_access->p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
346             if( i_ret != rtmp_packet->length_encoded )
347             {
348                 free( rtmp_packet->body->body );
349                 free( rtmp_packet->body );
350                 free( rtmp_packet );
351                 free( tmp_buffer );
352                 msg_Err( p_access->p_sys->p_thread, "failed send flv packet" );
353                 return -1;
354             }
355             free( rtmp_packet->body->body );
356             free( rtmp_packet->body );
357             free( rtmp_packet );
358             free( tmp_buffer );
359         }
360
361         i_write += p_buffer->i_buffer;
362
363         p_buffer = p_next;
364     }
365
366     return i_write;
367 }
368
369 /********************a*********************************************************
370  * Seek: seek to a specific location in a file
371  *****************************************************************************/
372 static int Seek( sout_access_out_t *p_access, off_t i_pos )
373 {
374     (void)i_pos;
375     msg_Err( p_access, "RTMP sout access cannot seek" );
376     return -1;
377 }
378
379 /*****************************************************************************
380  * ThreadControl: manage control messages and pipe media to Read
381  *****************************************************************************/
382 static void* ThreadControl( vlc_object_t *p_this )
383 {
384     rtmp_control_thread_t *p_thread = (rtmp_control_thread_t *) p_this;
385     rtmp_packet_t *rtmp_packet;
386
387     rtmp_init_handler( p_thread->rtmp_handler );
388
389     while( vlc_object_alive (p_thread) )
390     {
391         rtmp_packet = rtmp_read_net_packet( p_thread );
392         if( rtmp_packet != NULL )
393         {
394             if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
395                 || rtmp_packet->content_type > 0x14 ) /* RTMP_CONTENT_TYPE_INVOKE */
396             {
397                 free( rtmp_packet->body->body );
398                 free( rtmp_packet->body );
399                 free( rtmp_packet );
400
401                 msg_Warn( p_thread, "unknown content type received" );
402             }
403             else
404                 p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet );
405         }
406         else
407         {
408             /* Sometimes server close connection too soon */
409             if( p_thread->result_connect )
410             {
411                 vlc_mutex_lock( &p_thread->lock );
412                 vlc_cond_signal( &p_thread->wait );
413                 vlc_mutex_unlock( &p_thread->lock );
414             }
415
416             p_thread->b_die = 1;
417         }
418     }
419     return NULL;
420 }