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