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 *****************************************************************************/
50 #include <vlc_block.h>
52 #include <shout/shout.h>
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
60 #define SOUT_CFG_PREFIX "sout-shout-"
62 #define NAME_TEXT N_("Stream name")
63 #define NAME_LONGTEXT N_("Name to give to this stream/channel on the " \
64 "shoutcast/icecast server." )
66 #define DESCRIPTION_TEXT N_("Stream description")
67 #define DESCRIPTION_LONGTEXT N_("Description of the stream content or " \
68 "information about your channel." )
70 #define MP3_TEXT N_("Stream MP3")
71 #define MP3_LONGTEXT N_("You normally have to feed the shoutcast module " \
72 "with Ogg streams. It is also possible to stream " \
73 "MP3 instead, so you can forward MP3 streams to " \
74 "the shoutcast/icecast server." )
76 /* To be listed properly as a public stream on the Yellow Pages of shoutcast/icecast
77 the genres should match those used on the corresponding sites. Several examples
78 are Alternative, Classical, Comedy, Country etc. */
80 #define GENRE_TEXT N_("Genre description")
81 #define GENRE_LONGTEXT N_("Genre of the content. " )
83 #define URL_TEXT N_("URL description")
84 #define URL_LONGTEXT N_("URL with information about the stream or your channel. " )
86 /* The shout module only "transmits" data. It does not have direct access to
87 "codec level" information. Stream information such as bitrate, samplerate,
88 channel numbers and quality (in case of Ogg streaming) need to be set manually */
90 #define BITRATE_TEXT N_("Bitrate")
91 #define BITRATE_LONGTEXT N_("Bitrate information of the transcoded stream. " )
93 #define SAMPLERATE_TEXT N_("Samplerate")
94 #define SAMPLERATE_LONGTEXT N_("Samplerate information of the transcoded stream. " )
96 #define CHANNELS_TEXT N_("Number of channels")
97 #define CHANNELS_LONGTEXT N_("Number of channels information of the transcoded stream. " )
99 #define QUALITY_TEXT N_("Ogg Vorbis Quality")
100 #define QUALITY_LONGTEXT N_("Ogg Vorbis Quality information of the transcoded stream. " )
102 #define PUBLIC_TEXT N_("Stream public")
103 #define PUBLIC_LONGTEXT N_("Make the server publicly available on the 'Yellow Pages' " \
104 "(directory listing of streams) on the icecast/shoutcast " \
105 "website. Requires the bitrate information specified for " \
106 "shoutcast. Requires Ogg streaming for icecast." )
109 set_description( _("IceCAST output") );
110 set_shortname( "Shoutcast" );
111 set_capability( "sout access", 50 );
112 set_category( CAT_SOUT );
113 set_subcategory( SUBCAT_SOUT_ACO );
114 add_shortcut( "shout" );
115 add_string( SOUT_CFG_PREFIX "name", "VLC media player - Live stream", NULL,
116 NAME_TEXT, NAME_LONGTEXT, false );
117 add_string( SOUT_CFG_PREFIX "description",
118 "Live stream from VLC media player", NULL,
119 DESCRIPTION_TEXT, DESCRIPTION_LONGTEXT, false );
120 add_bool( SOUT_CFG_PREFIX "mp3", false, NULL,
121 MP3_TEXT, MP3_LONGTEXT, true );
122 add_string( SOUT_CFG_PREFIX "genre", "Alternative", NULL,
123 GENRE_TEXT, GENRE_LONGTEXT, false );
124 add_string( SOUT_CFG_PREFIX "url", "http://www.videolan.org/vlc", NULL,
125 URL_TEXT, URL_LONGTEXT, false );
126 add_string( SOUT_CFG_PREFIX "bitrate", "", NULL,
127 BITRATE_TEXT, BITRATE_LONGTEXT, false );
128 add_string( SOUT_CFG_PREFIX "samplerate", "", NULL,
129 SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, false );
130 add_string( SOUT_CFG_PREFIX "channels", "", NULL,
131 CHANNELS_TEXT, CHANNELS_LONGTEXT, false );
132 add_string( SOUT_CFG_PREFIX "quality", "", NULL,
133 QUALITY_TEXT, QUALITY_LONGTEXT, false );
134 add_bool( SOUT_CFG_PREFIX "public", false, NULL,
135 PUBLIC_TEXT, PUBLIC_LONGTEXT, true );
136 set_callbacks( Open, Close );
139 /*****************************************************************************
140 * Exported prototypes
141 *****************************************************************************/
142 static const char *ppsz_sout_options[] = {
143 "name", "description", "mp3", "genre", "url", "bitrate", "samplerate",
144 "channels", "quality", "public", NULL
148 /*****************************************************************************
149 * Exported prototypes
150 *****************************************************************************/
151 static ssize_t Write( sout_access_out_t *, block_t * );
152 static int Seek ( sout_access_out_t *, off_t );
154 struct sout_access_out_sys_t
159 /*****************************************************************************
160 * Open: open the shout connection
161 *****************************************************************************/
162 static int Open( vlc_object_t *p_this )
164 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
165 sout_access_out_sys_t *p_sys;
171 char *psz_accessname = NULL;
172 char *psz_parser = NULL;
173 char *psz_user = NULL;
174 char *psz_pass = NULL;
175 char *psz_host = NULL;
176 char *psz_mount = NULL;
177 char *psz_name = NULL;
178 char *psz_description = NULL;
179 char *tmp_port = NULL;
180 char *psz_genre = NULL;
181 char *psz_url = NULL;
183 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
185 psz_accessname = psz_parser = strdup( p_access->psz_path );
187 if( !p_access->psz_path )
190 "please specify url=user:password@host:port/mountpoint" );
194 /* Parse connection data user:pwd@host:port/mountpoint */
195 psz_user = psz_parser;
196 while( psz_parser[0] && psz_parser[0] != ':' ) psz_parser++;
197 if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
198 psz_pass = 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_host = psz_parser;
202 while( psz_parser[0] && psz_parser[0] != ':' ) psz_parser++;
203 if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
204 tmp_port = psz_parser;
205 while( psz_parser[0] && psz_parser[0] != '/' ) psz_parser++;
206 if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
207 psz_mount = psz_parser;
209 i_port = atoi( tmp_port );
211 p_sys = p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) );
214 msg_Err( p_access, "out of memory" );
215 free( psz_accessname );
219 var_Get( p_access, SOUT_CFG_PREFIX "name", &val );
220 if( *val.psz_string )
221 psz_name = val.psz_string;
223 free( val.psz_string );
225 var_Get( p_access, SOUT_CFG_PREFIX "description", &val );
226 if( *val.psz_string )
227 psz_description = val.psz_string;
229 free( val.psz_string );
231 var_Get( p_access, SOUT_CFG_PREFIX "genre", &val );
232 if( *val.psz_string )
233 psz_genre = val.psz_string;
235 free( val.psz_string );
237 var_Get( p_access, SOUT_CFG_PREFIX "url", &val );
238 if( *val.psz_string )
239 psz_url = val.psz_string;
241 free( val.psz_string );
243 p_shout = p_sys->p_shout = shout_new();
245 || shout_set_host( p_shout, psz_host ) != SHOUTERR_SUCCESS
246 || shout_set_protocol( p_shout, SHOUT_PROTOCOL_ICY ) != SHOUTERR_SUCCESS
247 || shout_set_port( p_shout, i_port ) != SHOUTERR_SUCCESS
248 || shout_set_password( p_shout, psz_pass ) != SHOUTERR_SUCCESS
249 || shout_set_mount( p_shout, psz_mount ) != SHOUTERR_SUCCESS
250 || shout_set_user( p_shout, psz_user ) != SHOUTERR_SUCCESS
251 || shout_set_agent( p_shout, "VLC media player " VERSION ) != SHOUTERR_SUCCESS
252 || shout_set_name( p_shout, psz_name ) != SHOUTERR_SUCCESS
253 || shout_set_description( p_shout, psz_description ) != SHOUTERR_SUCCESS
254 || shout_set_genre( p_shout, psz_genre ) != SHOUTERR_SUCCESS
255 || shout_set_url( p_shout, psz_url ) != SHOUTERR_SUCCESS
256 /* || shout_set_nonblocking( p_shout, 1 ) != SHOUTERR_SUCCESS */
259 msg_Err( p_access, "failed to initialize shout streaming to %s:%i/%s",
260 psz_host, i_port, psz_mount );
261 free( p_access->p_sys );
262 free( psz_accessname );
267 free( psz_description );
271 var_Get( p_access, SOUT_CFG_PREFIX "mp3", &val );
272 if( val.b_bool == true )
273 i_ret = shout_set_format( p_shout, SHOUT_FORMAT_MP3 );
275 i_ret = shout_set_format( p_shout, SHOUT_FORMAT_OGG );
277 if( i_ret != SHOUTERR_SUCCESS )
279 msg_Err( p_access, "failed to set the shoutcast streaming format" );
280 free( p_access->p_sys );
281 free( psz_accessname );
285 /* Don't force bitrate to 0 but only use when specified. This will otherwise
286 show an empty field on icecast directory listing instead of NA */
287 var_Get( p_access, SOUT_CFG_PREFIX "bitrate", &val );
288 if( *val.psz_string )
290 i_ret = shout_set_audio_info( p_shout, SHOUT_AI_BITRATE, val.psz_string );
291 if( i_ret != SHOUTERR_SUCCESS )
293 msg_Err( p_access, "failed to set the information about the bitrate" );
294 free( p_access->p_sys );
295 free( psz_accessname );
301 /* Bitrate information is used for icecast/shoutcast servers directory
302 listings (sorting, stream info etc.) */
303 msg_Warn( p_access, "no bitrate information specified (required for listing " \
304 "the server as public on the shoutcast website)" );
305 free( val.psz_string );
308 /* Information about samplerate, channels and quality will not be propagated
309 through the YP protocol for icecast to the public directory listing when
310 the icecast server is operating in shoutcast compatibility mode */
312 var_Get( p_access, SOUT_CFG_PREFIX "samplerate", &val );
313 if( *val.psz_string )
315 i_ret = shout_set_audio_info( p_shout, SHOUT_AI_SAMPLERATE, val.psz_string );
316 if( i_ret != SHOUTERR_SUCCESS )
318 msg_Err( p_access, "failed to set the information about the samplerate" );
319 free( p_access->p_sys );
320 free( psz_accessname );
325 free( val.psz_string );
327 var_Get( p_access, SOUT_CFG_PREFIX "channels", &val );
328 if( *val.psz_string )
330 i_ret = shout_set_audio_info( p_shout, SHOUT_AI_CHANNELS, val.psz_string );
331 if( i_ret != SHOUTERR_SUCCESS )
333 msg_Err( p_access, "failed to set the information about the number of channels" );
334 free( p_access->p_sys );
335 free( psz_accessname );
340 free( val.psz_string );
342 var_Get( p_access, SOUT_CFG_PREFIX "quality", &val );
343 if( *val.psz_string )
345 i_ret = shout_set_audio_info( p_shout, SHOUT_AI_QUALITY, val.psz_string );
346 if( i_ret != SHOUTERR_SUCCESS )
348 msg_Err( p_access, "failed to set the information about Ogg Vorbis quality" );
349 free( p_access->p_sys );
350 free( psz_accessname );
355 free( val.psz_string );
357 var_Get( p_access, SOUT_CFG_PREFIX "public", &val );
358 if( val.b_bool == true )
360 i_ret = shout_set_public( p_shout, 1 );
361 if( i_ret != SHOUTERR_SUCCESS )
363 msg_Err( p_access, "failed to set the server status setting to public" );
364 free( p_access->p_sys );
365 free( psz_accessname );
370 /* Connect at startup. Cycle through the possible protocols. */
371 i_ret = shout_get_connected( p_shout );
372 while ( i_ret != SHOUTERR_CONNECTED )
374 /* Shout parameters cannot be changed on an open connection */
375 i_ret = shout_close( p_shout );
376 if( i_ret == SHOUTERR_SUCCESS )
378 i_ret = SHOUTERR_UNCONNECTED;
381 /* Re-initialize for Shoutcast using ICY protocol. Not needed for initial connection
382 but it is when we are reconnecting after other protocol was tried. */
383 i_ret = shout_set_protocol( p_shout, SHOUT_PROTOCOL_ICY );
384 if( i_ret != SHOUTERR_SUCCESS )
386 msg_Err( p_access, "failed to set the protocol to 'icy'" );
387 free( p_access->p_sys );
388 free( psz_accessname );
391 i_ret = shout_open( p_shout );
392 if( i_ret == SHOUTERR_SUCCESS )
394 i_ret = SHOUTERR_CONNECTED;
395 msg_Dbg( p_access, "connected using 'icy' (shoutcast) protocol" );
399 msg_Warn( p_access, "failed to connect using 'icy' (shoutcast) protocol" );
401 /* Shout parameters cannot be changed on an open connection */
402 i_ret = shout_close( p_shout );
403 if( i_ret == SHOUTERR_SUCCESS )
405 i_ret = SHOUTERR_UNCONNECTED;
408 /* IceCAST using HTTP protocol */
409 i_ret = shout_set_protocol( p_shout, SHOUT_PROTOCOL_HTTP );
410 if( i_ret != SHOUTERR_SUCCESS )
412 msg_Err( p_access, "failed to set the protocol to 'http'" );
413 free( p_access->p_sys );
414 free( psz_accessname );
417 i_ret = shout_open( p_shout );
418 if( i_ret == SHOUTERR_SUCCESS )
420 i_ret = SHOUTERR_CONNECTED;
421 msg_Dbg( p_access, "connected using 'http' (icecast 2.x) protocol" );
424 msg_Warn( p_access, "failed to connect using 'http' (icecast 2.x) protocol " );
427 for non-blocking, use:
428 while( i_ret == SHOUTERR_BUSY )
431 i_ret = shout_get_connected( p_shout );
434 if ( i_ret != SHOUTERR_CONNECTED )
436 msg_Warn( p_access, "unable to establish connection, retrying..." );
441 if( i_ret != SHOUTERR_CONNECTED )
443 msg_Err( p_access, "failed to open shout stream to %s:%i/%s: %s",
444 psz_host, i_port, psz_mount, shout_get_error(p_shout) );
445 free( p_access->p_sys );
446 free( psz_accessname );
450 p_access->pf_write = Write;
451 p_access->pf_seek = Seek;
453 msg_Dbg( p_access, "shout access output opened (%s@%s:%i/%s)",
454 psz_user, psz_host, i_port, psz_mount );
456 /* Update pace control flag */
457 if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
459 p_access->p_sout->i_out_pace_nocontrol++;
462 free( psz_accessname );
467 /*****************************************************************************
468 * Close: close the target
469 *****************************************************************************/
470 static void Close( vlc_object_t * p_this )
472 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
474 if( p_access->p_sys && p_access->p_sys->p_shout )
476 shout_close( p_access->p_sys->p_shout );
479 free( p_access->p_sys );
481 /* Update pace control flag */
482 if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
484 p_access->p_sout->i_out_pace_nocontrol--;
487 msg_Dbg( p_access, "shout access output closed" );
490 /*****************************************************************************
491 * Write: standard write
492 *****************************************************************************/
493 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
497 shout_sync( p_access->p_sys->p_shout );
500 block_t *p_next = p_buffer->p_next;
502 if( shout_send( p_access->p_sys->p_shout,
503 p_buffer->p_buffer, p_buffer->i_buffer )
504 == SHOUTERR_SUCCESS )
506 i_write += p_buffer->i_buffer;
510 msg_Err( p_access, "cannot write to stream: %s",
511 shout_get_error(p_access->p_sys->p_shout) );
513 /* The most common cause seems to be a server disconnect, resulting in a
514 Socket Error which can only be fixed by closing and reconnecting.
515 Since we already began with a working connection, the most feasable
516 approach to get out of this error status is a (timed) reconnect approach. */
517 shout_close( p_access->p_sys->p_shout );
518 msg_Warn( p_access, "server unavailable? trying to reconnect..." );
519 /* Re-open the connection (protocol params have already been set) and re-sync */
520 if( shout_open( p_access->p_sys->p_shout ) == SHOUTERR_SUCCESS )
522 shout_sync( p_access->p_sys->p_shout );
523 msg_Warn( p_access, "reconnected to server" );
527 msg_Err( p_access, "failed to reconnect to server" );
531 block_Release( p_buffer );
533 /* XXX: Unsure if that's the cause for some audio trouble... */
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 )
546 msg_Err( p_access, "cannot seek on shout" );