Plugins: include vlc_common.h directly instead of vlc/vlc.h
[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 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 ssize_t Write( sout_access_out_t *, block_t * );
67 static int     Seek ( sout_access_out_t *, off_t  );
68 static void ThreadControl( vlc_object_t * );
69
70 struct sout_access_out_sys_t
71 {
72     int active;
73
74     /* thread for filtering and handling control messages */
75     rtmp_control_thread_t *p_thread;
76 };
77
78 /*****************************************************************************
79  * Open: open the rtmp connection
80  *****************************************************************************/
81 static int Open( vlc_object_t *p_this )
82 {
83     sout_access_out_t *p_access = (sout_access_out_t *) p_this;
84     sout_access_out_sys_t *p_sys;
85     char *psz, *p;
86     int length_path, length_media_name;
87     int i;
88
89     if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
90     {
91         msg_Err( p_access, "not enough memory" );
92         return VLC_ENOMEM;
93     }
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         msg_Err( p_access, "out of memory" );
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     length_media_name = strlen( strrchr( p_sys->p_thread->url.psz_path, '/' ) ) - 1;
130
131     p_sys->p_thread->psz_application = strndup( p_sys->p_thread->url.psz_path + 1, length_path - length_media_name - 2 );
132     p_sys->p_thread->psz_media = strdup( p_sys->p_thread->url.psz_path + ( length_path - length_media_name ) );
133
134     msg_Dbg( p_access, "rtmp: host='%s' port=%d path='%s'",
135              p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port, p_sys->p_thread->url.psz_path );
136
137     if( p_sys->p_thread->url.psz_username && *p_sys->p_thread->url.psz_username )
138     {
139         msg_Dbg( p_access, "      user='%s', pwd='%s'",
140                  p_sys->p_thread->url.psz_username, p_sys->p_thread->url.psz_password );
141     }
142
143     /* Initialize thread variables */
144     p_sys->p_thread->b_die = 0;
145     p_sys->p_thread->b_error= 0;
146     p_sys->p_thread->p_fifo_input = block_FifoNew();
147     p_sys->p_thread->p_empty_blocks = block_FifoNew();
148     p_sys->p_thread->has_audio = 0;
149     p_sys->p_thread->has_video = 0;
150     p_sys->p_thread->metadata_received = 0;
151     p_sys->p_thread->first_media_packet = 1;
152     p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; /* FLV_TAG_FIRST_PREVIOUS_TAG_SIZE */
153
154     p_sys->p_thread->flv_body = rtmp_body_new( -1 );
155     p_sys->p_thread->flv_length_body = 0;
156
157     p_sys->p_thread->chunk_size_recv = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
158     p_sys->p_thread->chunk_size_send = 128; /* RTMP_DEFAULT_CHUNK_SIZE */
159     for(i = 0; i < 64; i++)
160     {
161         memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) );
162         p_sys->p_thread->rtmp_headers_send[i].length_header = -1;
163         p_sys->p_thread->rtmp_headers_send[i].stream_index = -1;
164         p_sys->p_thread->rtmp_headers_send[i].timestamp = -1;
165         p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1;
166         p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1;
167         p_sys->p_thread->rtmp_headers_send[i].length_body = -1;
168         p_sys->p_thread->rtmp_headers_send[i].content_type = -1;
169         p_sys->p_thread->rtmp_headers_send[i].src_dst = -1;
170         p_sys->p_thread->rtmp_headers_send[i].body = NULL;
171     }
172
173     vlc_cond_init( p_sys->p_thread, &p_sys->p_thread->wait );
174     vlc_mutex_init( &p_sys->p_thread->lock );
175
176     p_sys->p_thread->result_connect = 1;
177     /* p_sys->p_thread->result_publish = only used on access */
178     p_sys->p_thread->result_play = 1;
179     p_sys->p_thread->result_stop = 0;
180
181     /* Open connection */
182     p_sys->p_thread->fd = net_ConnectTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
183     if( p_sys->p_thread->fd == -1 )
184         goto error2;
185
186     if( vlc_thread_create( p_sys->p_thread, "rtmp control thread", ThreadControl,
187                            VLC_THREAD_PRIORITY_INPUT, false ) )
188     {
189         msg_Err( p_access, "cannot spawn rtmp control thread" );
190         goto error2;
191     }
192
193     if( !p_sys->active )
194     {
195         if( rtmp_connect_passive( p_sys->p_thread ) < 0 )
196         {
197             msg_Err( p_access, "connect passive failed");
198             goto error2;
199         }
200     }
201
202     p_access->pf_write = Write;
203     p_access->pf_seek = Seek;
204
205     return VLC_SUCCESS;
206
207 error2:
208     vlc_cond_destroy( &p_sys->p_thread->wait );
209     vlc_mutex_destroy( &p_sys->p_thread->lock );
210
211     free( p_sys->p_thread->psz_application );
212     free( p_sys->p_thread->psz_media );
213
214     net_Close( p_sys->p_thread->fd );
215 error:
216     vlc_object_detach( p_sys->p_thread );
217     vlc_object_release( p_sys->p_thread );
218
219     vlc_UrlClean( &p_sys->p_thread->url );
220     free( p_sys );
221
222     return VLC_EGENERIC;
223 }
224
225 /*****************************************************************************
226  * Close: close the target
227  *****************************************************************************/
228 static void Close( vlc_object_t * p_this )
229 {
230     sout_access_out_t *p_access = (sout_access_out_t *) p_this;
231     sout_access_out_sys_t *p_sys = p_access->p_sys;
232     int i;
233
234 //    p_sys->p_thread->b_die = true;
235     vlc_object_kill( p_sys->p_thread );
236     block_FifoWake( p_sys->p_thread->p_fifo_input );
237     block_FifoWake( p_sys->p_thread->p_empty_blocks );
238
239     vlc_thread_join( p_sys->p_thread );
240
241     vlc_cond_destroy( &p_sys->p_thread->wait );
242     vlc_mutex_destroy( &p_sys->p_thread->lock );
243
244     block_FifoRelease( p_sys->p_thread->p_fifo_input );
245     block_FifoRelease( p_sys->p_thread->p_empty_blocks );
246
247     for( i = 0; i < 64; i++ ) /* RTMP_HEADER_STREAM_INDEX_MASK */
248     {
249         if( p_sys->p_thread->rtmp_headers_recv[i].body != NULL )
250         {
251             free( p_sys->p_thread->rtmp_headers_recv[i].body->body );
252             free( p_sys->p_thread->rtmp_headers_recv[i].body );
253         }
254     }
255
256     net_Close( p_sys->p_thread->fd );
257
258     vlc_object_detach( p_sys->p_thread );
259     vlc_object_release( p_sys->p_thread );
260
261     vlc_UrlClean( &p_sys->p_thread->url );
262     free( p_sys->p_thread->psz_application );
263     free( p_sys->p_thread->psz_media );
264     free( p_sys );
265 }
266
267 /*****************************************************************************
268  * Write: standard write on a file descriptor.
269  *****************************************************************************/
270 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
271 {
272     rtmp_packet_t *rtmp_packet;
273     uint8_t *tmp_buffer;
274     ssize_t i_ret;
275     ssize_t i_write = 0;
276
277     if( p_access->p_sys->p_thread->first_media_packet )
278     {
279         /* 13 == FLV_HEADER_SIZE + PreviousTagSize*/
280         memmove( p_buffer->p_buffer, p_buffer->p_buffer + 13, p_buffer->i_buffer - 13 );
281         p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer - 13 );
282
283         p_access->p_sys->p_thread->first_media_packet = 0;
284     }
285
286     while( p_buffer )
287     {
288         block_t *p_next = p_buffer->p_next;
289 //////////////////////////////
290 /*msg_Warn(p_access, "XXXXXXXXXXXXXXXXX");
291 int i;
292 for(i = 0; i < p_buffer->i_buffer; i += 16)
293 {
294     msg_Warn(p_access,"%.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x %.2x%.2x",
295 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],
296 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]);
297 }*/
298 ////////////////////////
299 msg_Warn(p_access, "rtmp.c:360 i_dts %d i_pts %d", p_buffer->i_dts, p_buffer->i_pts);
300         rtmp_packet = rtmp_build_flv_over_rtmp( p_access->p_sys->p_thread, p_buffer );
301
302         if( rtmp_packet )
303         {
304             tmp_buffer = rtmp_encode_packet( p_access->p_sys->p_thread, rtmp_packet );
305
306             i_ret = net_Write( p_access->p_sys->p_thread, p_access->p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded );
307             if( i_ret != rtmp_packet->length_encoded )
308             {
309                 free( rtmp_packet->body->body );
310                 free( rtmp_packet->body );
311                 free( rtmp_packet );
312                 free( tmp_buffer );
313                 msg_Err( p_access->p_sys->p_thread, "failed send flv packet" );
314                 return -1;
315             }
316             free( rtmp_packet->body->body );
317             free( rtmp_packet->body );
318             free( rtmp_packet );
319             free( tmp_buffer );
320         }
321
322         i_write += p_buffer->i_buffer;
323
324         p_buffer = p_next;
325     }
326
327     return i_write;
328 }
329
330 /********************a*********************************************************
331  * Seek: seek to a specific location in a file
332  *****************************************************************************/
333 static int Seek( sout_access_out_t *p_access, off_t i_pos )
334 {
335     msg_Err( p_access, "RTMP sout access cannot seek" );
336     return -1;
337 }
338
339 /*****************************************************************************
340  * ThreadControl: manage control messages and pipe media to Read
341  *****************************************************************************/
342 static void ThreadControl( vlc_object_t *p_this )
343 {
344     rtmp_control_thread_t *p_thread = (rtmp_control_thread_t *) p_this;
345     rtmp_packet_t *rtmp_packet;
346
347     rtmp_init_handler( p_thread->rtmp_handler );
348
349     while( !p_thread->b_die )
350     {
351         rtmp_packet = rtmp_read_net_packet( p_thread );
352         if( rtmp_packet != NULL )
353         {
354             if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
355                 || rtmp_packet->content_type > 0x14 ) /* RTMP_CONTENT_TYPE_INVOKE */
356             {
357                 free( rtmp_packet->body->body );
358                 free( rtmp_packet->body );
359                 free( rtmp_packet );
360
361                 msg_Warn( p_thread, "unknown content type received" );
362             }
363             else
364                 p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet );
365         }
366         else
367         {
368             /* Sometimes server close connection too soon */
369             if( p_thread->result_connect )
370             {
371                 vlc_mutex_lock( &p_thread->lock );
372                 vlc_cond_signal( &p_thread->wait );
373                 vlc_mutex_unlock( &p_thread->lock );
374             }
375
376             p_thread->b_die = 1;
377         }
378     }
379 }