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