]> git.sesse.net Git - vlc/blob - modules/access_output/shout.c
Remove most stray semi-colons in module descriptions
[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", 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 )
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     vlc_value_t val;
172
173     char *psz_accessname = NULL;
174     char *psz_parser = NULL;
175     char *psz_user = NULL;
176     char *psz_pass = NULL;
177     char *psz_host = NULL;
178     char *psz_mount = NULL;
179     char *psz_name = NULL;
180     char *psz_description = NULL;
181     char *tmp_port = NULL;
182     char *psz_genre = NULL;
183     char *psz_url = NULL;
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_user = psz_parser;
200     while( psz_parser[0] && psz_parser[0] != ':' ) psz_parser++;
201     if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
202     psz_pass = psz_parser;
203     while( psz_parser[0] && psz_parser[0] != '@' ) psz_parser++;
204     if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
205     psz_host = psz_parser;
206     while( psz_parser[0] && psz_parser[0] != ':' ) psz_parser++;
207     if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
208     tmp_port = psz_parser;
209     while( psz_parser[0] && psz_parser[0] != '/' ) psz_parser++;
210     if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
211     psz_mount = psz_parser;
212
213     i_port = atoi( tmp_port );
214
215     p_sys = p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) );
216     if( !p_sys )
217     {
218         free( psz_accessname );
219         return VLC_ENOMEM;
220     }
221
222     var_Get( p_access, SOUT_CFG_PREFIX "name", &val );
223     if( *val.psz_string )
224         psz_name = val.psz_string;
225     else
226         free( val.psz_string );
227
228     var_Get( p_access, SOUT_CFG_PREFIX "description", &val );
229     if( *val.psz_string )
230         psz_description = val.psz_string;
231     else
232         free( val.psz_string );
233
234     var_Get( p_access, SOUT_CFG_PREFIX "genre", &val );
235     if( *val.psz_string )
236         psz_genre = val.psz_string;
237     else
238         free( val.psz_string );
239
240     var_Get( p_access, SOUT_CFG_PREFIX "url", &val );
241     if( *val.psz_string )
242         psz_url = val.psz_string;
243     else
244         free( val.psz_string );
245
246     p_shout = p_sys->p_shout = shout_new();
247     if( !p_shout
248          || shout_set_host( p_shout, psz_host ) != SHOUTERR_SUCCESS
249          || shout_set_protocol( p_shout, SHOUT_PROTOCOL_ICY ) != SHOUTERR_SUCCESS
250          || shout_set_port( p_shout, i_port ) != SHOUTERR_SUCCESS
251          || shout_set_password( p_shout, psz_pass ) != SHOUTERR_SUCCESS
252          || shout_set_mount( p_shout, psz_mount ) != SHOUTERR_SUCCESS
253          || shout_set_user( p_shout, psz_user ) != SHOUTERR_SUCCESS
254          || shout_set_agent( p_shout, "VLC media player " VERSION ) != SHOUTERR_SUCCESS
255          || shout_set_name( p_shout, psz_name ) != SHOUTERR_SUCCESS
256          || shout_set_description( p_shout, psz_description ) != SHOUTERR_SUCCESS
257          || shout_set_genre( p_shout, psz_genre ) != SHOUTERR_SUCCESS
258          || shout_set_url( p_shout, psz_url ) != SHOUTERR_SUCCESS
259          /* || shout_set_nonblocking( p_shout, 1 ) != SHOUTERR_SUCCESS */
260       )
261     {
262         msg_Err( p_access, "failed to initialize shout streaming to %s:%i/%s",
263                  psz_host, i_port, psz_mount );
264         free( p_access->p_sys );
265         free( psz_accessname );
266         free( psz_name );
267         free( psz_description );
268         free( psz_genre );
269         free( psz_url );
270         return VLC_EGENERIC;
271     }
272
273     free( psz_name );
274     free( psz_description );
275     free( psz_genre );
276     free( psz_url );
277
278     var_Get( p_access, SOUT_CFG_PREFIX "mp3", &val );
279     if( val.b_bool == true )
280         i_ret = shout_set_format( p_shout, SHOUT_FORMAT_MP3 );
281     else
282         i_ret = shout_set_format( p_shout, SHOUT_FORMAT_OGG );
283
284     if( i_ret != SHOUTERR_SUCCESS )
285     {
286         msg_Err( p_access, "failed to set the shoutcast streaming format" );
287         free( p_access->p_sys );
288         free( psz_accessname );
289         return VLC_EGENERIC;
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     var_Get( p_access, SOUT_CFG_PREFIX "bitrate", &val );
295     if( *val.psz_string )
296     {
297         i_ret = shout_set_audio_info( p_shout, SHOUT_AI_BITRATE, val.psz_string );
298         if( i_ret != SHOUTERR_SUCCESS )
299         {
300             msg_Err( p_access, "failed to set the information about the bitrate" );
301             free( val.psz_string );
302             free( p_access->p_sys );
303             free( psz_accessname );
304             return VLC_EGENERIC;
305         }
306     }
307     else
308     {
309         /* Bitrate information is used for icecast/shoutcast servers directory
310            listings (sorting, stream info etc.) */
311         msg_Warn( p_access, "no bitrate information specified (required for listing " \
312                             "the server as public on the shoutcast website)" );
313         free( val.psz_string );
314     }
315
316     /* Information about samplerate, channels and quality will not be propagated
317        through the YP protocol for icecast to the public directory listing when
318        the icecast server is operating in shoutcast compatibility mode */
319
320     var_Get( p_access, SOUT_CFG_PREFIX "samplerate", &val );
321     if( *val.psz_string )
322     {
323         i_ret = shout_set_audio_info( p_shout, SHOUT_AI_SAMPLERATE, val.psz_string );
324         if( i_ret != SHOUTERR_SUCCESS )
325         {
326             msg_Err( p_access, "failed to set the information about the samplerate" );
327             free( val.psz_string );
328             free( p_access->p_sys );
329             free( psz_accessname );
330             return VLC_EGENERIC;
331         }
332     }
333     else
334         free( val.psz_string );
335
336     var_Get( p_access, SOUT_CFG_PREFIX "channels", &val );
337     if( *val.psz_string )
338     {
339         i_ret = shout_set_audio_info( p_shout, SHOUT_AI_CHANNELS, val.psz_string );
340         if( i_ret != SHOUTERR_SUCCESS )
341         {
342             msg_Err( p_access, "failed to set the information about the number of channels" );
343             free( val.psz_string );
344             free( p_access->p_sys );
345             free( psz_accessname );
346             return VLC_EGENERIC;
347         }
348     }
349     else
350         free( val.psz_string );
351
352     var_Get( p_access, SOUT_CFG_PREFIX "quality", &val );
353     if( *val.psz_string )
354     {
355         i_ret = shout_set_audio_info( p_shout, SHOUT_AI_QUALITY, val.psz_string );
356         if( i_ret != SHOUTERR_SUCCESS )
357         {
358             msg_Err( p_access, "failed to set the information about Ogg Vorbis quality" );
359             free( val.psz_string );
360             free( p_access->p_sys );
361             free( psz_accessname );
362             return VLC_EGENERIC;
363         }
364     }
365     else
366         free( val.psz_string );
367
368     var_Get( p_access, SOUT_CFG_PREFIX "public", &val );
369     if( val.b_bool == true )
370     {
371         i_ret = shout_set_public( p_shout, 1 );
372         if( i_ret != SHOUTERR_SUCCESS )
373         {
374             msg_Err( p_access, "failed to set the server status setting to public" );
375             free( p_access->p_sys );
376             free( psz_accessname );
377             return VLC_EGENERIC;
378         }
379     }
380
381     /* Connect at startup. Cycle through the possible protocols. */
382     i_ret = shout_get_connected( p_shout );
383     while ( i_ret != SHOUTERR_CONNECTED )
384     {
385         /* Shout parameters cannot be changed on an open connection */
386         i_ret = shout_close( p_shout );
387         if( i_ret == SHOUTERR_SUCCESS )
388         {
389             i_ret = SHOUTERR_UNCONNECTED;
390         }
391
392         /* Re-initialize for Shoutcast using ICY protocol. Not needed for initial connection
393            but it is when we are reconnecting after other protocol was tried. */
394         i_ret = shout_set_protocol( p_shout, SHOUT_PROTOCOL_ICY );
395         if( i_ret != SHOUTERR_SUCCESS )
396         {
397             msg_Err( p_access, "failed to set the protocol to 'icy'" );
398             free( p_access->p_sys );
399             free( psz_accessname );
400             return VLC_EGENERIC;
401         }
402         i_ret = shout_open( p_shout );
403         if( i_ret == SHOUTERR_SUCCESS )
404         {
405             i_ret = SHOUTERR_CONNECTED;
406             msg_Dbg( p_access, "connected using 'icy' (shoutcast) protocol" );
407         }
408         else
409         {
410             msg_Warn( p_access, "failed to connect using 'icy' (shoutcast) protocol" );
411
412             /* Shout parameters cannot be changed on an open connection */
413             i_ret = shout_close( p_shout );
414             if( i_ret == SHOUTERR_SUCCESS )
415             {
416                 i_ret = SHOUTERR_UNCONNECTED;
417             }
418
419             /* IceCAST using HTTP protocol */
420             i_ret = shout_set_protocol( p_shout, SHOUT_PROTOCOL_HTTP );
421             if( i_ret != SHOUTERR_SUCCESS )
422             {
423                 msg_Err( p_access, "failed to set the protocol to 'http'" );
424                 free( p_access->p_sys );
425                 free( psz_accessname );
426                 return VLC_EGENERIC;
427             }
428             i_ret = shout_open( p_shout );
429             if( i_ret == SHOUTERR_SUCCESS )
430             {
431                 i_ret = SHOUTERR_CONNECTED;
432                 msg_Dbg( p_access, "connected using 'http' (icecast 2.x) protocol" );
433             }
434             else
435                 msg_Warn( p_access, "failed to connect using 'http' (icecast 2.x) protocol " );
436         }
437 /*
438         for non-blocking, use:
439         while( i_ret == SHOUTERR_BUSY )
440         {
441             sleep( 1 );
442             i_ret = shout_get_connected( p_shout );
443         }
444 */
445         if ( i_ret != SHOUTERR_CONNECTED )
446         {
447             msg_Warn( p_access, "unable to establish connection, retrying..." );
448             msleep( 30000000 );
449         }
450     }
451
452     if( i_ret != SHOUTERR_CONNECTED )
453     {
454         msg_Err( p_access, "failed to open shout stream to %s:%i/%s: %s",
455                  psz_host, i_port, psz_mount, shout_get_error(p_shout) );
456         free( p_access->p_sys );
457         free( psz_accessname );
458         return VLC_EGENERIC;
459     }
460
461     p_access->pf_write = Write;
462     p_access->pf_seek  = Seek;
463     p_access->pf_control = Control;
464
465     msg_Dbg( p_access, "shout access output opened (%s@%s:%i/%s)",
466              psz_user, psz_host, i_port, psz_mount );
467     free( psz_accessname );
468
469     return VLC_SUCCESS;
470 }
471
472 /*****************************************************************************
473  * Close: close the target
474  *****************************************************************************/
475 static void Close( vlc_object_t * p_this )
476 {
477     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
478
479     if( p_access->p_sys && p_access->p_sys->p_shout )
480     {
481         shout_close( p_access->p_sys->p_shout );
482         shout_shutdown();
483     }
484     free( p_access->p_sys );
485     msg_Dbg( p_access, "shout access output closed" );
486 }
487
488 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
489 {
490     switch( i_query )
491     {
492         case ACCESS_OUT_CONTROLS_PACE:
493         {
494             bool *pb = va_arg( args, bool * );
495             *pb = strcmp( p_access->psz_access, "stream" );
496             break;
497         }
498
499         default:
500             return VLC_EGENERIC;
501     }
502     return VLC_SUCCESS;
503 }
504
505 /*****************************************************************************
506  * Write: standard write
507  *****************************************************************************/
508 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
509 {
510     size_t i_write = 0;
511
512     shout_sync( p_access->p_sys->p_shout );
513     while( p_buffer )
514     {
515         block_t *p_next = p_buffer->p_next;
516
517         if( shout_send( p_access->p_sys->p_shout,
518                         p_buffer->p_buffer, p_buffer->i_buffer )
519              == SHOUTERR_SUCCESS )
520         {
521             i_write += p_buffer->i_buffer;
522         }
523         else
524         {
525             msg_Err( p_access, "cannot write to stream: %s",
526                      shout_get_error(p_access->p_sys->p_shout) );
527
528             /* The most common cause seems to be a server disconnect, resulting in a
529                Socket Error which can only be fixed by closing and reconnecting.
530                Since we already began with a working connection, the most feasable
531                approach to get out of this error status is a (timed) reconnect approach. */
532             shout_close( p_access->p_sys->p_shout );
533             msg_Warn( p_access, "server unavailable? trying to reconnect..." );
534             /* Re-open the connection (protocol params have already been set) and re-sync */
535             if( shout_open( p_access->p_sys->p_shout ) == SHOUTERR_SUCCESS )
536             {
537                 shout_sync( p_access->p_sys->p_shout );
538                 msg_Warn( p_access, "reconnected to server" );
539             }
540             else
541             {
542                 msg_Err( p_access, "failed to reconnect to server" );
543                 block_ChainRelease (p_buffer);
544                 return VLC_EGENERIC;
545             }
546
547         }
548         block_Release( p_buffer );
549
550         /* XXX: Unsure if that's the cause for some audio trouble... */
551
552         p_buffer = p_next;
553     }
554
555     return i_write;
556 }
557
558 /*****************************************************************************
559  * Seek: seek to a specific location -- not supported
560  *****************************************************************************/
561 static int Seek( sout_access_out_t *p_access, off_t i_pos )
562 {
563     msg_Err( p_access, "cannot seek on shout" );
564     return VLC_EGENERIC;
565 }
566