1 /*****************************************************************************
2 * shout.c: This module forwards vorbis streams to an icecast server
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Daniel Fischer <dan at subsignal dot org>
8 * Derk-Jan Hartman <hartman at videolan dot org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
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.
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}'
38 *****************************************************************************/
40 /*****************************************************************************
42 *****************************************************************************/
48 #include <vlc_common.h>
49 #include <vlc_plugin.h>
51 #include <vlc_block.h>
53 #include <shout/shout.h>
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
61 #define SOUT_CFG_PREFIX "sout-shout-"
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." )
67 #define DESCRIPTION_TEXT N_("Stream description")
68 #define DESCRIPTION_LONGTEXT N_("Description of the stream content or " \
69 "information about your channel." )
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." )
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. */
81 #define GENRE_TEXT N_("Genre description")
82 #define GENRE_LONGTEXT N_("Genre of the content. " )
84 #define URL_TEXT N_("URL description")
85 #define URL_LONGTEXT N_("URL with information about the stream or your channel. " )
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 */
91 #define BITRATE_TEXT N_("Bitrate")
92 #define BITRATE_LONGTEXT N_("Bitrate information of the transcoded stream. " )
94 #define SAMPLERATE_TEXT N_("Samplerate")
95 #define SAMPLERATE_LONGTEXT N_("Samplerate information of the transcoded stream. " )
97 #define CHANNELS_TEXT N_("Number of channels")
98 #define CHANNELS_LONGTEXT N_("Number of channels information of the transcoded stream. " )
100 #define QUALITY_TEXT N_("Ogg Vorbis Quality")
101 #define QUALITY_LONGTEXT N_("Ogg Vorbis Quality information of the transcoded stream. " )
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." )
110 set_description( N_("IceCAST output") );
111 set_shortname( "Shoutcast" );
112 set_capability( "sout access", 50 );
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 );
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
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 );
155 struct sout_access_out_sys_t
160 /*****************************************************************************
161 * Open: open the shout connection
162 *****************************************************************************/
163 static int Open( vlc_object_t *p_this )
165 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
166 sout_access_out_sys_t *p_sys;
172 char *psz_accessname = NULL;
173 char *psz_parser = NULL;
174 char *psz_user = NULL;
175 char *psz_pass = NULL;
176 char *psz_host = NULL;
177 char *psz_mount = NULL;
178 char *psz_name = NULL;
179 char *psz_description = NULL;
180 char *tmp_port = NULL;
181 char *psz_genre = NULL;
182 char *psz_url = NULL;
184 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
186 if( !p_access->psz_path )
189 "please specify url=user:password@host:port/mountpoint" );
193 psz_accessname = psz_parser = strdup( p_access->psz_path );
197 /* Parse connection data user:pwd@host:port/mountpoint */
198 psz_user = psz_parser;
199 while( psz_parser[0] && psz_parser[0] != ':' ) psz_parser++;
200 if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
201 psz_pass = psz_parser;
202 while( psz_parser[0] && psz_parser[0] != '@' ) psz_parser++;
203 if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
204 psz_host = psz_parser;
205 while( psz_parser[0] && psz_parser[0] != ':' ) psz_parser++;
206 if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
207 tmp_port = psz_parser;
208 while( psz_parser[0] && psz_parser[0] != '/' ) psz_parser++;
209 if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
210 psz_mount = psz_parser;
212 i_port = atoi( tmp_port );
214 p_sys = p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) );
217 free( psz_accessname );
221 var_Get( p_access, SOUT_CFG_PREFIX "name", &val );
222 if( *val.psz_string )
223 psz_name = val.psz_string;
225 free( val.psz_string );
227 var_Get( p_access, SOUT_CFG_PREFIX "description", &val );
228 if( *val.psz_string )
229 psz_description = val.psz_string;
231 free( val.psz_string );
233 var_Get( p_access, SOUT_CFG_PREFIX "genre", &val );
234 if( *val.psz_string )
235 psz_genre = val.psz_string;
237 free( val.psz_string );
239 var_Get( p_access, SOUT_CFG_PREFIX "url", &val );
240 if( *val.psz_string )
241 psz_url = val.psz_string;
243 free( val.psz_string );
245 p_shout = p_sys->p_shout = shout_new();
247 || shout_set_host( p_shout, psz_host ) != SHOUTERR_SUCCESS
248 || shout_set_protocol( p_shout, SHOUT_PROTOCOL_ICY ) != SHOUTERR_SUCCESS
249 || shout_set_port( p_shout, i_port ) != SHOUTERR_SUCCESS
250 || shout_set_password( p_shout, psz_pass ) != SHOUTERR_SUCCESS
251 || shout_set_mount( p_shout, psz_mount ) != SHOUTERR_SUCCESS
252 || shout_set_user( p_shout, psz_user ) != SHOUTERR_SUCCESS
253 || shout_set_agent( p_shout, "VLC media player " VERSION ) != SHOUTERR_SUCCESS
254 || shout_set_name( p_shout, psz_name ) != SHOUTERR_SUCCESS
255 || shout_set_description( p_shout, psz_description ) != SHOUTERR_SUCCESS
256 || shout_set_genre( p_shout, psz_genre ) != SHOUTERR_SUCCESS
257 || shout_set_url( p_shout, psz_url ) != SHOUTERR_SUCCESS
258 /* || shout_set_nonblocking( p_shout, 1 ) != SHOUTERR_SUCCESS */
261 msg_Err( p_access, "failed to initialize shout streaming to %s:%i/%s",
262 psz_host, i_port, psz_mount );
263 free( p_access->p_sys );
264 free( psz_accessname );
266 free( psz_description );
273 free( psz_description );
277 var_Get( p_access, SOUT_CFG_PREFIX "mp3", &val );
278 if( val.b_bool == true )
279 i_ret = shout_set_format( p_shout, SHOUT_FORMAT_MP3 );
281 i_ret = shout_set_format( p_shout, SHOUT_FORMAT_OGG );
283 if( i_ret != SHOUTERR_SUCCESS )
285 msg_Err( p_access, "failed to set the shoutcast streaming format" );
286 free( p_access->p_sys );
287 free( psz_accessname );
291 /* Don't force bitrate to 0 but only use when specified. This will otherwise
292 show an empty field on icecast directory listing instead of NA */
293 var_Get( p_access, SOUT_CFG_PREFIX "bitrate", &val );
294 if( *val.psz_string )
296 i_ret = shout_set_audio_info( p_shout, SHOUT_AI_BITRATE, val.psz_string );
297 if( i_ret != SHOUTERR_SUCCESS )
299 msg_Err( p_access, "failed to set the information about the bitrate" );
300 free( val.psz_string );
301 free( p_access->p_sys );
302 free( psz_accessname );
308 /* Bitrate information is used for icecast/shoutcast servers directory
309 listings (sorting, stream info etc.) */
310 msg_Warn( p_access, "no bitrate information specified (required for listing " \
311 "the server as public on the shoutcast website)" );
312 free( val.psz_string );
315 /* Information about samplerate, channels and quality will not be propagated
316 through the YP protocol for icecast to the public directory listing when
317 the icecast server is operating in shoutcast compatibility mode */
319 var_Get( p_access, SOUT_CFG_PREFIX "samplerate", &val );
320 if( *val.psz_string )
322 i_ret = shout_set_audio_info( p_shout, SHOUT_AI_SAMPLERATE, val.psz_string );
323 if( i_ret != SHOUTERR_SUCCESS )
325 msg_Err( p_access, "failed to set the information about the samplerate" );
326 free( val.psz_string );
327 free( p_access->p_sys );
328 free( psz_accessname );
333 free( val.psz_string );
335 var_Get( p_access, SOUT_CFG_PREFIX "channels", &val );
336 if( *val.psz_string )
338 i_ret = shout_set_audio_info( p_shout, SHOUT_AI_CHANNELS, val.psz_string );
339 if( i_ret != SHOUTERR_SUCCESS )
341 msg_Err( p_access, "failed to set the information about the number of channels" );
342 free( val.psz_string );
343 free( p_access->p_sys );
344 free( psz_accessname );
349 free( val.psz_string );
351 var_Get( p_access, SOUT_CFG_PREFIX "quality", &val );
352 if( *val.psz_string )
354 i_ret = shout_set_audio_info( p_shout, SHOUT_AI_QUALITY, val.psz_string );
355 if( i_ret != SHOUTERR_SUCCESS )
357 msg_Err( p_access, "failed to set the information about Ogg Vorbis quality" );
358 free( val.psz_string );
359 free( p_access->p_sys );
360 free( psz_accessname );
365 free( val.psz_string );
367 var_Get( p_access, SOUT_CFG_PREFIX "public", &val );
368 if( val.b_bool == true )
370 i_ret = shout_set_public( p_shout, 1 );
371 if( i_ret != SHOUTERR_SUCCESS )
373 msg_Err( p_access, "failed to set the server status setting to public" );
374 free( p_access->p_sys );
375 free( psz_accessname );
380 /* Connect at startup. Cycle through the possible protocols. */
381 i_ret = shout_get_connected( p_shout );
382 while ( i_ret != SHOUTERR_CONNECTED )
384 /* Shout parameters cannot be changed on an open connection */
385 i_ret = shout_close( p_shout );
386 if( i_ret == SHOUTERR_SUCCESS )
388 i_ret = SHOUTERR_UNCONNECTED;
391 /* Re-initialize for Shoutcast using ICY protocol. Not needed for initial connection
392 but it is when we are reconnecting after other protocol was tried. */
393 i_ret = shout_set_protocol( p_shout, SHOUT_PROTOCOL_ICY );
394 if( i_ret != SHOUTERR_SUCCESS )
396 msg_Err( p_access, "failed to set the protocol to 'icy'" );
397 free( p_access->p_sys );
398 free( psz_accessname );
401 i_ret = shout_open( p_shout );
402 if( i_ret == SHOUTERR_SUCCESS )
404 i_ret = SHOUTERR_CONNECTED;
405 msg_Dbg( p_access, "connected using 'icy' (shoutcast) protocol" );
409 msg_Warn( p_access, "failed to connect using 'icy' (shoutcast) protocol" );
411 /* Shout parameters cannot be changed on an open connection */
412 i_ret = shout_close( p_shout );
413 if( i_ret == SHOUTERR_SUCCESS )
415 i_ret = SHOUTERR_UNCONNECTED;
418 /* IceCAST using HTTP protocol */
419 i_ret = shout_set_protocol( p_shout, SHOUT_PROTOCOL_HTTP );
420 if( i_ret != SHOUTERR_SUCCESS )
422 msg_Err( p_access, "failed to set the protocol to 'http'" );
423 free( p_access->p_sys );
424 free( psz_accessname );
427 i_ret = shout_open( p_shout );
428 if( i_ret == SHOUTERR_SUCCESS )
430 i_ret = SHOUTERR_CONNECTED;
431 msg_Dbg( p_access, "connected using 'http' (icecast 2.x) protocol" );
434 msg_Warn( p_access, "failed to connect using 'http' (icecast 2.x) protocol " );
437 for non-blocking, use:
438 while( i_ret == SHOUTERR_BUSY )
441 i_ret = shout_get_connected( p_shout );
444 if ( i_ret != SHOUTERR_CONNECTED )
446 msg_Warn( p_access, "unable to establish connection, retrying..." );
451 if( i_ret != SHOUTERR_CONNECTED )
453 msg_Err( p_access, "failed to open shout stream to %s:%i/%s: %s",
454 psz_host, i_port, psz_mount, shout_get_error(p_shout) );
455 free( p_access->p_sys );
456 free( psz_accessname );
460 p_access->pf_write = Write;
461 p_access->pf_seek = Seek;
463 msg_Dbg( p_access, "shout access output opened (%s@%s:%i/%s)",
464 psz_user, psz_host, i_port, psz_mount );
466 /* Update pace control flag */
467 if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
469 p_access->p_sout->i_out_pace_nocontrol++;
472 free( psz_accessname );
477 /*****************************************************************************
478 * Close: close the target
479 *****************************************************************************/
480 static void Close( vlc_object_t * p_this )
482 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
484 if( p_access->p_sys && p_access->p_sys->p_shout )
486 shout_close( p_access->p_sys->p_shout );
489 free( p_access->p_sys );
491 /* Update pace control flag */
492 if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
494 p_access->p_sout->i_out_pace_nocontrol--;
497 msg_Dbg( p_access, "shout access output closed" );
500 /*****************************************************************************
501 * Write: standard write
502 *****************************************************************************/
503 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
507 shout_sync( p_access->p_sys->p_shout );
510 block_t *p_next = p_buffer->p_next;
512 if( shout_send( p_access->p_sys->p_shout,
513 p_buffer->p_buffer, p_buffer->i_buffer )
514 == SHOUTERR_SUCCESS )
516 i_write += p_buffer->i_buffer;
520 msg_Err( p_access, "cannot write to stream: %s",
521 shout_get_error(p_access->p_sys->p_shout) );
523 /* The most common cause seems to be a server disconnect, resulting in a
524 Socket Error which can only be fixed by closing and reconnecting.
525 Since we already began with a working connection, the most feasable
526 approach to get out of this error status is a (timed) reconnect approach. */
527 shout_close( p_access->p_sys->p_shout );
528 msg_Warn( p_access, "server unavailable? trying to reconnect..." );
529 /* Re-open the connection (protocol params have already been set) and re-sync */
530 if( shout_open( p_access->p_sys->p_shout ) == SHOUTERR_SUCCESS )
532 shout_sync( p_access->p_sys->p_shout );
533 msg_Warn( p_access, "reconnected to server" );
537 msg_Err( p_access, "failed to reconnect to server" );
538 block_ChainRelease (p_buffer);
543 block_Release( p_buffer );
545 /* XXX: Unsure if that's the cause for some audio trouble... */
553 /*****************************************************************************
554 * Seek: seek to a specific location -- not supported
555 *****************************************************************************/
556 static int Seek( sout_access_out_t *p_access, off_t i_pos )
558 msg_Err( p_access, "cannot seek on shout" );