]> git.sesse.net Git - vlc/blob - modules/access_output/shout.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / access_output / shout.c
1 /*****************************************************************************
2  * shout.c: This module forwards vorbis streams to an icecast server
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Daniel Fischer <dan at subsignal dot org>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Some Comments:
27  *
28  * - this only works for ogg and/or mp3, and we don't check this yet.
29  * - MP3 metadata is not passed along, since metadata is only available after
30  *   this module is opened.
31  *
32  * Typical usage:
33  *
34  * vlc v4l:/dev/video:input=2:norm=pal:size=192x144 \
35  * --sout '#transcode{vcodec=theora,vb=300,acodec=vorb,ab=96}\
36  * :std{access=shout,mux=ogg,dst=localhost:8005}'
37  *
38  *****************************************************************************/
39
40 /*****************************************************************************
41  * Preamble
42  *****************************************************************************/
43
44 #ifdef HAVE_CONFIG_H
45 # include "config.h"
46 #endif
47
48 #include <vlc_common.h>
49 #include <vlc_plugin.h>
50 #include <vlc_sout.h>
51 #include <vlc_block.h>
52
53 #include <shout/shout.h>
54
55 /*****************************************************************************
56  * Module descriptor
57  *****************************************************************************/
58 static int  Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
60
61 #define SOUT_CFG_PREFIX "sout-shout-"
62
63 #define NAME_TEXT N_("Stream name")
64 #define NAME_LONGTEXT N_("Name to give to this stream/channel on the " \
65                          "shoutcast/icecast server." )
66
67 #define DESCRIPTION_TEXT N_("Stream description")
68 #define DESCRIPTION_LONGTEXT N_("Description of the stream content or " \
69                                 "information about your channel." )
70
71 #define MP3_TEXT N_("Stream MP3")
72 #define MP3_LONGTEXT N_("You normally have to feed the shoutcast module " \
73                         "with Ogg streams. It is also possible to stream " \
74                         "MP3 instead, so you can forward MP3 streams to " \
75                         "the shoutcast/icecast server." )
76
77 /* To be listed properly as a public stream on the Yellow Pages of shoutcast/icecast
78    the genres should match those used on the corresponding sites. Several examples
79    are Alternative, Classical, Comedy, Country etc. */
80
81 #define GENRE_TEXT N_("Genre description")
82 #define GENRE_LONGTEXT N_("Genre of the content. " )
83
84 #define URL_TEXT N_("URL description")
85 #define URL_LONGTEXT N_("URL with information about the stream or your channel. " )
86
87 /* The shout module only "transmits" data. It does not have direct access to
88    "codec level" information. Stream information such as bitrate, samplerate,
89    channel numbers and quality (in case of Ogg streaming) need to be set manually */
90
91 #define BITRATE_TEXT N_("Bitrate")
92 #define BITRATE_LONGTEXT N_("Bitrate information of the transcoded stream. " )
93
94 #define SAMPLERATE_TEXT N_("Samplerate")
95 #define SAMPLERATE_LONGTEXT N_("Samplerate information of the transcoded stream. " )
96
97 #define CHANNELS_TEXT N_("Number of channels")
98 #define CHANNELS_LONGTEXT N_("Number of channels information of the transcoded stream. " )
99
100 #define QUALITY_TEXT N_("Ogg Vorbis Quality")
101 #define QUALITY_LONGTEXT N_("Ogg Vorbis Quality information of the transcoded stream. " )
102
103 #define PUBLIC_TEXT N_("Stream public")
104 #define PUBLIC_LONGTEXT N_("Make the server publicly available on the 'Yellow Pages' " \
105                            "(directory listing of streams) on the icecast/shoutcast " \
106                            "website. Requires the bitrate information specified for " \
107                            "shoutcast. Requires Ogg streaming for icecast." )
108
109 vlc_module_begin ()
110     set_description( N_("IceCAST output") )
111     set_shortname( "Shoutcast" )
112     set_capability( "sout access", 0 )
113     set_category( CAT_SOUT )
114     set_subcategory( SUBCAT_SOUT_ACO )
115     add_shortcut( "shout" )
116     add_string( SOUT_CFG_PREFIX "name", "VLC media player - Live stream", NULL,
117                 NAME_TEXT, NAME_LONGTEXT, false )
118     add_string( SOUT_CFG_PREFIX "description",
119                  "Live stream from VLC media player", NULL,
120                 DESCRIPTION_TEXT, DESCRIPTION_LONGTEXT, false )
121     add_bool(   SOUT_CFG_PREFIX "mp3", false, NULL,
122                 MP3_TEXT, MP3_LONGTEXT, true )
123     add_string( SOUT_CFG_PREFIX "genre", "Alternative", NULL,
124                 GENRE_TEXT, GENRE_LONGTEXT, false )
125     add_string( SOUT_CFG_PREFIX "url", "http://www.videolan.org/vlc", NULL,
126                 URL_TEXT, URL_LONGTEXT, false )
127     add_string( SOUT_CFG_PREFIX "bitrate", "", NULL,
128                 BITRATE_TEXT, BITRATE_LONGTEXT, false )
129     add_string( SOUT_CFG_PREFIX "samplerate", "", NULL,
130                 SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, false )
131     add_string( SOUT_CFG_PREFIX "channels", "", NULL,
132                 CHANNELS_TEXT, CHANNELS_LONGTEXT, false )
133     add_string( SOUT_CFG_PREFIX "quality", "", NULL,
134                 QUALITY_TEXT, QUALITY_LONGTEXT, false )
135     add_bool(   SOUT_CFG_PREFIX "public", false, NULL,
136                 PUBLIC_TEXT, PUBLIC_LONGTEXT, true )
137     set_callbacks( Open, Close )
138 vlc_module_end ()
139
140 /*****************************************************************************
141  * Exported prototypes
142  *****************************************************************************/
143 static const char *const ppsz_sout_options[] = {
144     "name", "description", "mp3", "genre", "url", "bitrate", "samplerate",
145     "channels", "quality", "public", NULL
146 };
147
148
149 /*****************************************************************************
150  * Exported prototypes
151  *****************************************************************************/
152 static ssize_t Write( sout_access_out_t *, block_t * );
153 static int Seek ( sout_access_out_t *, off_t  );
154 static int Control( sout_access_out_t *, int, va_list );
155
156 struct sout_access_out_sys_t
157 {
158     shout_t *p_shout;
159 };
160
161 /*****************************************************************************
162  * Open: open the shout connection
163  *****************************************************************************/
164 static int Open( vlc_object_t *p_this )
165 {
166     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
167     sout_access_out_sys_t *p_sys;
168     shout_t *p_shout;
169     long i_ret;
170     unsigned int i_port;
171     char *psz_val;
172
173     char *psz_accessname;
174     char *psz_parser;
175     const char *psz_user;
176     char *psz_pass;
177     char *psz_host;
178     char *psz_mount;
179     char *psz_port;
180     char *psz_name;
181     char *psz_description;
182     char *psz_genre;
183     char *psz_url;
184
185     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
186
187     if( !p_access->psz_path )
188     {
189         msg_Err( p_access,
190                  "please specify url=user:password@host:port/mountpoint" );
191         return VLC_EGENERIC;
192     }
193
194     psz_accessname = psz_parser = strdup( p_access->psz_path );
195     if( !psz_parser )
196         return VLC_ENOMEM;
197
198     /* Parse connection data user:pwd@host:port/mountpoint */
199     psz_host = strchr( psz_parser, '@' );
200     if( psz_host )
201     {
202         psz_user = psz_parser;
203         *(psz_host++) = '\0';
204     }
205     else
206         psz_user = "";
207
208     psz_pass = strchr( psz_user, ':' );
209     if( psz_pass )
210         *(psz_pass++) = '\0';
211     else
212         psz_pass = "";
213
214     psz_mount = strchr( psz_host, '/' );
215     if( psz_mount )
216         *(psz_mount++) = '\0';
217     else
218         psz_mount = "";
219
220     if( psz_host[0] == '[' )
221     {
222         psz_port = strstr( psz_host, "]:" );
223         if( psz_port )
224         {
225             *psz_port = '\0';
226             psz_port += 2;
227         }
228     }
229     else
230     {
231         psz_port = strchr( psz_host, ':' );
232         if( psz_port )
233             *(psz_port++) = '\0';
234     }
235     i_port = psz_port ? atoi( psz_port ) : 8000;
236
237     p_sys = p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) );
238     if( !p_sys )
239     {
240         free( psz_accessname );
241         return VLC_ENOMEM;
242     }
243
244     psz_name = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "name" );
245     psz_description = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "description" );
246     psz_genre = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "genre" );
247     psz_url = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "url" );
248
249     p_shout = p_sys->p_shout = shout_new();
250     if( !p_shout
251          || shout_set_host( p_shout, psz_host ) != SHOUTERR_SUCCESS
252          || shout_set_protocol( p_shout, SHOUT_PROTOCOL_ICY ) != SHOUTERR_SUCCESS
253          || shout_set_port( p_shout, i_port ) != SHOUTERR_SUCCESS
254          || shout_set_password( p_shout, psz_pass ) != SHOUTERR_SUCCESS
255          || shout_set_mount( p_shout, psz_mount ) != SHOUTERR_SUCCESS
256          || shout_set_user( p_shout, psz_user ) != SHOUTERR_SUCCESS
257          || shout_set_agent( p_shout, "VLC media player " VERSION ) != SHOUTERR_SUCCESS
258          || shout_set_name( p_shout, psz_name ) != SHOUTERR_SUCCESS
259          || shout_set_description( p_shout, psz_description ) != SHOUTERR_SUCCESS
260          || shout_set_genre( p_shout, psz_genre ) != SHOUTERR_SUCCESS
261          || shout_set_url( p_shout, psz_url ) != SHOUTERR_SUCCESS
262          /* || shout_set_nonblocking( p_shout, 1 ) != SHOUTERR_SUCCESS */
263       )
264     {
265         msg_Err( p_access, "failed to initialize shout streaming to %s:%i/%s",
266                  psz_host, i_port, psz_mount );
267         free( p_access->p_sys );
268         free( psz_accessname );
269         free( psz_name );
270         free( psz_description );
271         free( psz_genre );
272         free( psz_url );
273         return VLC_EGENERIC;
274     }
275
276     free( psz_name );
277     free( psz_description );
278     free( psz_genre );
279     free( psz_url );
280
281     if( var_GetBool( p_access, SOUT_CFG_PREFIX "mp3" ) )
282         i_ret = shout_set_format( p_shout, SHOUT_FORMAT_MP3 );
283     else
284         i_ret = shout_set_format( p_shout, SHOUT_FORMAT_OGG );
285
286     if( i_ret != SHOUTERR_SUCCESS )
287     {
288         msg_Err( p_access, "failed to set the shoutcast streaming format" );
289         goto error;
290     }
291
292     /* Don't force bitrate to 0 but only use when specified. This will otherwise
293        show an empty field on icecast directory listing instead of NA */
294     psz_val = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "bitrate" );
295     if( psz_val )
296     {
297         i_ret = shout_set_audio_info( p_shout, SHOUT_AI_BITRATE, psz_val );
298         free( psz_val );
299         if( i_ret != SHOUTERR_SUCCESS )
300         {
301             msg_Err( p_access, "failed to set the information about the bitrate" );
302             goto error;
303         }
304     }
305     else
306     {
307         /* Bitrate information is used for icecast/shoutcast servers directory
308            listings (sorting, stream info etc.) */
309         msg_Warn( p_access, "no bitrate information specified (required for listing " \
310                             "the server as public on the shoutcast website)" );
311     }
312
313     /* Information about samplerate, channels and quality will not be propagated
314        through the YP protocol for icecast to the public directory listing when
315        the icecast server is operating in shoutcast compatibility mode */
316
317     psz_val = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "samplerate" );
318     if( psz_val )
319     {
320         i_ret = shout_set_audio_info( p_shout, SHOUT_AI_SAMPLERATE, psz_val );
321         free( psz_val );
322         if( i_ret != SHOUTERR_SUCCESS )
323         {
324             msg_Err( p_access, "failed to set the information about the samplerate" );
325             goto error;
326         }
327     }
328
329     psz_val = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "channels" );
330     if( psz_val )
331     {
332         i_ret = shout_set_audio_info( p_shout, SHOUT_AI_CHANNELS, psz_val );
333         free( psz_val );
334         if( i_ret != SHOUTERR_SUCCESS )
335         {
336             msg_Err( p_access, "failed to set the information about the number of channels" );
337             goto error;
338         }
339     }
340
341     psz_val = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "quality" );
342     if( psz_val )
343     {
344         i_ret = shout_set_audio_info( p_shout, SHOUT_AI_QUALITY, psz_val );
345         free( psz_val );
346         if( i_ret != SHOUTERR_SUCCESS )
347         {
348             msg_Err( p_access, "failed to set the information about Ogg Vorbis quality" );
349             goto error;
350         }
351     }
352
353     if( var_GetBool( p_access, SOUT_CFG_PREFIX "public" ) )
354     {
355         i_ret = shout_set_public( p_shout, 1 );
356         if( i_ret != SHOUTERR_SUCCESS )
357         {
358             msg_Err( p_access, "failed to set the server status setting to public" );
359             goto error;
360         }
361     }
362
363     /* Connect at startup. Cycle through the possible protocols. */
364     i_ret = shout_get_connected( p_shout );
365     while ( i_ret != SHOUTERR_CONNECTED )
366     {
367         /* Shout parameters cannot be changed on an open connection */
368         i_ret = shout_close( p_shout );
369         if( i_ret == SHOUTERR_SUCCESS )
370         {
371             i_ret = SHOUTERR_UNCONNECTED;
372         }
373
374         /* Re-initialize for Shoutcast using ICY protocol. Not needed for initial connection
375            but it is when we are reconnecting after other protocol was tried. */
376         i_ret = shout_set_protocol( p_shout, SHOUT_PROTOCOL_ICY );
377         if( i_ret != SHOUTERR_SUCCESS )
378         {
379             msg_Err( p_access, "failed to set the protocol to 'icy'" );
380             goto error;
381         }
382         i_ret = shout_open( p_shout );
383         if( i_ret == SHOUTERR_SUCCESS )
384         {
385             i_ret = SHOUTERR_CONNECTED;
386             msg_Dbg( p_access, "connected using 'icy' (shoutcast) protocol" );
387         }
388         else
389         {
390             msg_Warn( p_access, "failed to connect using 'icy' (shoutcast) protocol" );
391
392             /* Shout parameters cannot be changed on an open connection */
393             i_ret = shout_close( p_shout );
394             if( i_ret == SHOUTERR_SUCCESS )
395             {
396                 i_ret = SHOUTERR_UNCONNECTED;
397             }
398
399             /* IceCAST using HTTP protocol */
400             i_ret = shout_set_protocol( p_shout, SHOUT_PROTOCOL_HTTP );
401             if( i_ret != SHOUTERR_SUCCESS )
402             {
403                 msg_Err( p_access, "failed to set the protocol to 'http'" );
404                 goto error;
405             }
406             i_ret = shout_open( p_shout );
407             if( i_ret == SHOUTERR_SUCCESS )
408             {
409                 i_ret = SHOUTERR_CONNECTED;
410                 msg_Dbg( p_access, "connected using 'http' (icecast 2.x) protocol" );
411             }
412             else
413                 msg_Warn( p_access, "failed to connect using 'http' (icecast 2.x) protocol " );
414         }
415 /*
416         for non-blocking, use:
417         while( i_ret == SHOUTERR_BUSY )
418         {
419             sleep( 1 );
420             i_ret = shout_get_connected( p_shout );
421         }
422 */
423         if ( i_ret != SHOUTERR_CONNECTED )
424         {
425             msg_Warn( p_access, "unable to establish connection, retrying..." );
426             msleep( 30000000 );
427         }
428     }
429
430     if( i_ret != SHOUTERR_CONNECTED )
431     {
432         msg_Err( p_access, "failed to open shout stream to %s:%i/%s: %s",
433                  psz_host, i_port, psz_mount, shout_get_error(p_shout) );
434         free( p_access->p_sys );
435         free( psz_accessname );
436         return VLC_EGENERIC;
437     }
438
439     p_access->pf_write = Write;
440     p_access->pf_seek  = Seek;
441     p_access->pf_control = Control;
442
443     msg_Dbg( p_access, "shout access output opened (%s@%s:%i/%s)",
444              psz_user, psz_host, i_port, psz_mount );
445     free( psz_accessname );
446
447     return VLC_SUCCESS;
448
449 error:
450     free( psz_accessname );
451     free( p_sys );
452     return VLC_EGENERIC;
453 }
454
455 /*****************************************************************************
456  * Close: close the target
457  *****************************************************************************/
458 static void Close( vlc_object_t * p_this )
459 {
460     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
461
462     if( p_access->p_sys && p_access->p_sys->p_shout )
463     {
464         shout_close( p_access->p_sys->p_shout );
465         shout_shutdown();
466     }
467     free( p_access->p_sys );
468     msg_Dbg( p_access, "shout access output closed" );
469 }
470
471 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
472 {
473     switch( i_query )
474     {
475         case ACCESS_OUT_CONTROLS_PACE:
476         {
477             bool *pb = va_arg( args, bool * );
478             *pb = strcmp( p_access->psz_access, "stream" );
479             break;
480         }
481
482         default:
483             return VLC_EGENERIC;
484     }
485     return VLC_SUCCESS;
486 }
487
488 /*****************************************************************************
489  * Write: standard write
490  *****************************************************************************/
491 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
492 {
493     size_t i_write = 0;
494
495     shout_sync( p_access->p_sys->p_shout );
496     while( p_buffer )
497     {
498         block_t *p_next = p_buffer->p_next;
499
500         if( shout_send( p_access->p_sys->p_shout,
501                         p_buffer->p_buffer, p_buffer->i_buffer )
502              == SHOUTERR_SUCCESS )
503         {
504             i_write += p_buffer->i_buffer;
505         }
506         else
507         {
508             msg_Err( p_access, "cannot write to stream: %s",
509                      shout_get_error(p_access->p_sys->p_shout) );
510
511             /* The most common cause seems to be a server disconnect, resulting in a
512                Socket Error which can only be fixed by closing and reconnecting.
513                Since we already began with a working connection, the most feasable
514                approach to get out of this error status is a (timed) reconnect approach. */
515             shout_close( p_access->p_sys->p_shout );
516             msg_Warn( p_access, "server unavailable? trying to reconnect..." );
517             /* Re-open the connection (protocol params have already been set) and re-sync */
518             if( shout_open( p_access->p_sys->p_shout ) == SHOUTERR_SUCCESS )
519             {
520                 shout_sync( p_access->p_sys->p_shout );
521                 msg_Warn( p_access, "reconnected to server" );
522             }
523             else
524             {
525                 msg_Err( p_access, "failed to reconnect to server" );
526                 block_ChainRelease (p_buffer);
527                 return VLC_EGENERIC;
528             }
529
530         }
531         block_Release( p_buffer );
532
533         /* XXX: Unsure if that's the cause for some audio trouble... */
534
535         p_buffer = p_next;
536     }
537
538     return i_write;
539 }
540
541 /*****************************************************************************
542  * Seek: seek to a specific location -- not supported
543  *****************************************************************************/
544 static int Seek( sout_access_out_t *p_access, off_t i_pos )
545 {
546     VLC_UNUSED(i_pos);
547     msg_Err( p_access, "cannot seek on shout" );
548     return VLC_EGENERIC;
549 }
550