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