add_savefile: remove callback parameter
[vlc/asuraparaju-public.git] / modules / access_output / shout.c
blob984a84f3f3463c8a26a30d57e4f124560c16a308
1 /*****************************************************************************
2 * shout.c: This module forwards vorbis streams to an icecast server
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
26 * Some Comments:
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.
32 * Typical usage:
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 /*****************************************************************************
41 * Preamble
42 *****************************************************************************/
44 #ifdef HAVE_CONFIG_H
45 # include "config.h"
46 #endif
48 #include <vlc_common.h>
49 #include <vlc_plugin.h>
50 #include <vlc_sout.h>
51 #include <vlc_block.h>
53 #include <shout/shout.h>
55 /*****************************************************************************
56 * Module descriptor
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." )
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",
117 NAME_TEXT, NAME_LONGTEXT, false )
118 add_string( SOUT_CFG_PREFIX "description", "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",
123 GENRE_TEXT, GENRE_LONGTEXT, false )
124 add_string( SOUT_CFG_PREFIX "url", "http://www.videolan.org/vlc",
125 URL_TEXT, URL_LONGTEXT, false )
126 add_string( SOUT_CFG_PREFIX "bitrate", "",
127 BITRATE_TEXT, BITRATE_LONGTEXT, false )
128 add_string( SOUT_CFG_PREFIX "samplerate", "",
129 SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, false )
130 add_string( SOUT_CFG_PREFIX "channels", "",
131 CHANNELS_TEXT, CHANNELS_LONGTEXT, false )
132 add_string( SOUT_CFG_PREFIX "quality", "",
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 )
137 vlc_module_end ()
139 /*****************************************************************************
140 * Exported prototypes
141 *****************************************************************************/
142 static const char *const 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 );
153 static int Control( sout_access_out_t *, int, va_list );
155 struct sout_access_out_sys_t
157 shout_t *p_shout;
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;
167 shout_t *p_shout;
168 long i_ret;
169 unsigned int i_port;
170 char *psz_val;
172 char *psz_accessname;
173 char *psz_parser;
174 const char *psz_user;
175 char *psz_pass;
176 char *psz_host;
177 char *psz_mount;
178 char *psz_port;
179 char *psz_name;
180 char *psz_description;
181 char *psz_genre;
182 char *psz_url;
184 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
186 if( !p_access->psz_path )
188 msg_Err( p_access,
189 "please specify url=user:password@host:port/mountpoint" );
190 return VLC_EGENERIC;
193 psz_accessname = psz_parser = strdup( p_access->psz_path );
194 if( !psz_parser )
195 return VLC_ENOMEM;
197 /* Parse connection data user:pwd@host:port/mountpoint */
198 psz_host = strchr( psz_parser, '@' );
199 if( psz_host )
201 psz_user = psz_parser;
202 *(psz_host++) = '\0';
204 else
205 psz_user = "";
207 psz_pass = strchr( psz_user, ':' );
208 if( psz_pass )
209 *(psz_pass++) = '\0';
210 else
211 psz_pass = "";
213 psz_mount = strchr( psz_host, '/' );
214 if( psz_mount )
215 *(psz_mount++) = '\0';
216 else
217 psz_mount = "";
219 if( psz_host[0] == '[' )
221 psz_port = strstr( psz_host, "]:" );
222 if( psz_port )
224 *psz_port = '\0';
225 psz_port += 2;
228 else
230 psz_port = strchr( psz_host, ':' );
231 if( psz_port )
232 *(psz_port++) = '\0';
234 i_port = psz_port ? atoi( psz_port ) : 8000;
236 p_sys = p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) );
237 if( !p_sys )
239 free( psz_accessname );
240 return VLC_ENOMEM;
243 psz_name = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "name" );
244 psz_description = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "description" );
245 psz_genre = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "genre" );
246 psz_url = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "url" );
248 p_shout = p_sys->p_shout = shout_new();
249 if( !p_shout
250 || shout_set_host( p_shout, psz_host ) != SHOUTERR_SUCCESS
251 || shout_set_protocol( p_shout, SHOUT_PROTOCOL_ICY ) != SHOUTERR_SUCCESS
252 || shout_set_port( p_shout, i_port ) != SHOUTERR_SUCCESS
253 || shout_set_password( p_shout, psz_pass ) != SHOUTERR_SUCCESS
254 || shout_set_mount( p_shout, psz_mount ) != SHOUTERR_SUCCESS
255 || shout_set_user( p_shout, psz_user ) != SHOUTERR_SUCCESS
256 || shout_set_agent( p_shout, "VLC media player " VERSION ) != SHOUTERR_SUCCESS
257 || shout_set_name( p_shout, psz_name ) != SHOUTERR_SUCCESS
258 || shout_set_description( p_shout, psz_description ) != SHOUTERR_SUCCESS
259 || shout_set_genre( p_shout, psz_genre ) != SHOUTERR_SUCCESS
260 || shout_set_url( p_shout, psz_url ) != SHOUTERR_SUCCESS
261 /* || shout_set_nonblocking( p_shout, 1 ) != SHOUTERR_SUCCESS */
264 msg_Err( p_access, "failed to initialize shout streaming to %s:%i/%s",
265 psz_host, i_port, psz_mount );
266 free( p_access->p_sys );
267 free( psz_accessname );
268 free( psz_name );
269 free( psz_description );
270 free( psz_genre );
271 free( psz_url );
272 return VLC_EGENERIC;
275 free( psz_name );
276 free( psz_description );
277 free( psz_genre );
278 free( psz_url );
280 if( var_GetBool( p_access, SOUT_CFG_PREFIX "mp3" ) )
281 i_ret = shout_set_format( p_shout, SHOUT_FORMAT_MP3 );
282 else
283 i_ret = shout_set_format( p_shout, SHOUT_FORMAT_OGG );
285 if( i_ret != SHOUTERR_SUCCESS )
287 msg_Err( p_access, "failed to set the shoutcast streaming format" );
288 goto error;
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 psz_val = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "bitrate" );
294 if( psz_val )
296 i_ret = shout_set_audio_info( p_shout, SHOUT_AI_BITRATE, psz_val );
297 free( psz_val );
298 if( i_ret != SHOUTERR_SUCCESS )
300 msg_Err( p_access, "failed to set the information about the bitrate" );
301 goto error;
304 else
306 /* Bitrate information is used for icecast/shoutcast servers directory
307 listings (sorting, stream info etc.) */
308 msg_Warn( p_access, "no bitrate information specified (required for listing " \
309 "the server as public on the shoutcast website)" );
312 /* Information about samplerate, channels and quality will not be propagated
313 through the YP protocol for icecast to the public directory listing when
314 the icecast server is operating in shoutcast compatibility mode */
316 psz_val = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "samplerate" );
317 if( psz_val )
319 i_ret = shout_set_audio_info( p_shout, SHOUT_AI_SAMPLERATE, psz_val );
320 free( psz_val );
321 if( i_ret != SHOUTERR_SUCCESS )
323 msg_Err( p_access, "failed to set the information about the samplerate" );
324 goto error;
328 psz_val = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "channels" );
329 if( psz_val )
331 i_ret = shout_set_audio_info( p_shout, SHOUT_AI_CHANNELS, psz_val );
332 free( psz_val );
333 if( i_ret != SHOUTERR_SUCCESS )
335 msg_Err( p_access, "failed to set the information about the number of channels" );
336 goto error;
340 psz_val = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "quality" );
341 if( psz_val )
343 i_ret = shout_set_audio_info( p_shout, SHOUT_AI_QUALITY, psz_val );
344 free( psz_val );
345 if( i_ret != SHOUTERR_SUCCESS )
347 msg_Err( p_access, "failed to set the information about Ogg Vorbis quality" );
348 goto error;
352 if( var_GetBool( p_access, SOUT_CFG_PREFIX "public" ) )
354 i_ret = shout_set_public( p_shout, 1 );
355 if( i_ret != SHOUTERR_SUCCESS )
357 msg_Err( p_access, "failed to set the server status setting to public" );
358 goto error;
362 /* Connect at startup. Cycle through the possible protocols. */
363 i_ret = shout_get_connected( p_shout );
364 while ( i_ret != SHOUTERR_CONNECTED )
366 /* Shout parameters cannot be changed on an open connection */
367 i_ret = shout_close( p_shout );
368 if( i_ret == SHOUTERR_SUCCESS )
370 i_ret = SHOUTERR_UNCONNECTED;
373 /* Re-initialize for Shoutcast using ICY protocol. Not needed for initial connection
374 but it is when we are reconnecting after other protocol was tried. */
375 i_ret = shout_set_protocol( p_shout, SHOUT_PROTOCOL_ICY );
376 if( i_ret != SHOUTERR_SUCCESS )
378 msg_Err( p_access, "failed to set the protocol to 'icy'" );
379 goto error;
381 i_ret = shout_open( p_shout );
382 if( i_ret == SHOUTERR_SUCCESS )
384 i_ret = SHOUTERR_CONNECTED;
385 msg_Dbg( p_access, "connected using 'icy' (shoutcast) protocol" );
387 else
389 msg_Warn( p_access, "failed to connect using 'icy' (shoutcast) protocol" );
391 /* Shout parameters cannot be changed on an open connection */
392 i_ret = shout_close( p_shout );
393 if( i_ret == SHOUTERR_SUCCESS )
395 i_ret = SHOUTERR_UNCONNECTED;
398 /* IceCAST using HTTP protocol */
399 i_ret = shout_set_protocol( p_shout, SHOUT_PROTOCOL_HTTP );
400 if( i_ret != SHOUTERR_SUCCESS )
402 msg_Err( p_access, "failed to set the protocol to 'http'" );
403 goto error;
405 i_ret = shout_open( p_shout );
406 if( i_ret == SHOUTERR_SUCCESS )
408 i_ret = SHOUTERR_CONNECTED;
409 msg_Dbg( p_access, "connected using 'http' (icecast 2.x) protocol" );
411 else
412 msg_Warn( p_access, "failed to connect using 'http' (icecast 2.x) protocol " );
415 for non-blocking, use:
416 while( i_ret == SHOUTERR_BUSY )
418 sleep( 1 );
419 i_ret = shout_get_connected( p_shout );
422 if ( i_ret != SHOUTERR_CONNECTED )
424 msg_Warn( p_access, "unable to establish connection, retrying..." );
425 msleep( 30000000 );
429 if( i_ret != SHOUTERR_CONNECTED )
431 msg_Err( p_access, "failed to open shout stream to %s:%i/%s: %s",
432 psz_host, i_port, psz_mount, shout_get_error(p_shout) );
433 free( p_access->p_sys );
434 free( psz_accessname );
435 return VLC_EGENERIC;
438 p_access->pf_write = Write;
439 p_access->pf_seek = Seek;
440 p_access->pf_control = Control;
442 msg_Dbg( p_access, "shout access output opened (%s@%s:%i/%s)",
443 psz_user, psz_host, i_port, psz_mount );
444 free( psz_accessname );
446 return VLC_SUCCESS;
448 error:
449 free( psz_accessname );
450 free( p_sys );
451 return VLC_EGENERIC;
454 /*****************************************************************************
455 * Close: close the target
456 *****************************************************************************/
457 static void Close( vlc_object_t * p_this )
459 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
461 if( p_access->p_sys && p_access->p_sys->p_shout )
463 shout_close( p_access->p_sys->p_shout );
464 shout_shutdown();
466 free( p_access->p_sys );
467 msg_Dbg( p_access, "shout access output closed" );
470 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
472 switch( i_query )
474 case ACCESS_OUT_CONTROLS_PACE:
476 bool *pb = va_arg( args, bool * );
477 *pb = strcmp( p_access->psz_access, "stream" );
478 break;
481 default:
482 return VLC_EGENERIC;
484 return VLC_SUCCESS;
487 /*****************************************************************************
488 * Write: standard write
489 *****************************************************************************/
490 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
492 size_t i_write = 0;
494 shout_sync( p_access->p_sys->p_shout );
495 while( p_buffer )
497 block_t *p_next = p_buffer->p_next;
499 if( shout_send( p_access->p_sys->p_shout,
500 p_buffer->p_buffer, p_buffer->i_buffer )
501 == SHOUTERR_SUCCESS )
503 i_write += p_buffer->i_buffer;
505 else
507 msg_Err( p_access, "cannot write to stream: %s",
508 shout_get_error(p_access->p_sys->p_shout) );
510 /* The most common cause seems to be a server disconnect, resulting in a
511 Socket Error which can only be fixed by closing and reconnecting.
512 Since we already began with a working connection, the most feasable
513 approach to get out of this error status is a (timed) reconnect approach. */
514 shout_close( p_access->p_sys->p_shout );
515 msg_Warn( p_access, "server unavailable? trying to reconnect..." );
516 /* Re-open the connection (protocol params have already been set) and re-sync */
517 if( shout_open( p_access->p_sys->p_shout ) == SHOUTERR_SUCCESS )
519 shout_sync( p_access->p_sys->p_shout );
520 msg_Warn( p_access, "reconnected to server" );
522 else
524 msg_Err( p_access, "failed to reconnect to server" );
525 block_ChainRelease (p_buffer);
526 return VLC_EGENERIC;
530 block_Release( p_buffer );
532 /* XXX: Unsure if that's the cause for some audio trouble... */
534 p_buffer = p_next;
537 return i_write;
540 /*****************************************************************************
541 * Seek: seek to a specific location -- not supported
542 *****************************************************************************/
543 static int Seek( sout_access_out_t *p_access, off_t i_pos )
545 VLC_UNUSED(i_pos);
546 msg_Err( p_access, "cannot seek on shout" );
547 return VLC_EGENERIC;