]> git.sesse.net Git - vlc/blobdiff - modules/access_output/shout.c
The last but not the least commit about these useless tests.
[vlc] / modules / access_output / shout.c
index 9cf4d6f76105c4868743e629b39e2f5820d5a695..21373ebc3e92ae2f1ed5ca23b0fbbef36e14d624 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * shout.c: This module forwards vorbis streams to an icecast server
  *****************************************************************************
- * Copyright (C) 2005 VideoLAN
+ * Copyright (C) 2005 the VideoLAN team
  * $Id$
  *
  * Authors: Daniel Fischer <dan at subsignal dot org>
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <string.h>
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
 #include <vlc/vlc.h>
-#include <vlc/sout.h>
+#include <vlc_sout.h>
+#include <vlc_block.h>
 
 #include <shout/shout.h>
 
@@ -95,12 +99,11 @@ static void Close( vlc_object_t * );
 #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_TEXT N_("Stream public")
 #define PUBLIC_LONGTEXT N_("Make the server publicly available on the 'Yellow Pages' " \
-                           "of icecast/shoutcast (directory listing of broadcast " \
-                           "streams). Requires the bitrate information to be " \
-                           "specified for shoutcast. Requires Ogg streaming " \
-                           "for icecast." )
+                           "(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( _("IceCAST output") );
@@ -120,7 +123,7 @@ vlc_module_begin();
                 GENRE_TEXT, GENRE_LONGTEXT, VLC_FALSE );
     add_string( SOUT_CFG_PREFIX "url", "http://www.videolan.org/vlc", NULL,
                 URL_TEXT, URL_LONGTEXT, VLC_FALSE );
-    add_string( SOUT_CFG_PREFIX "bitrate", "0", NULL,
+    add_string( SOUT_CFG_PREFIX "bitrate", "", NULL,
                 BITRATE_TEXT, BITRATE_LONGTEXT, VLC_FALSE );
     add_string( SOUT_CFG_PREFIX "samplerate", "", NULL,
                 SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, VLC_FALSE );
@@ -145,9 +148,8 @@ static const char *ppsz_sout_options[] = {
 /*****************************************************************************
  * Exported prototypes
  *****************************************************************************/
-static int Write( sout_access_out_t *, block_t * );
+static ssize_t Write( sout_access_out_t *, block_t * );
 static int Seek ( sout_access_out_t *, off_t  );
-static int Read ( sout_access_out_t *, block_t * );
 
 struct sout_access_out_sys_t
 {
@@ -177,13 +179,12 @@ static int Open( vlc_object_t *p_this )
     char *tmp_port = NULL;
     char *psz_genre = NULL;
     char *psz_url = NULL;
-    char *psz_bitrate = NULL;
 
-    sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
+    config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
 
-    psz_accessname = psz_parser = strdup( p_access->psz_name );
+    psz_accessname = psz_parser = strdup( p_access->psz_path );
 
-    if( !p_access->psz_name )
+    if( !p_access->psz_path )
     {
         msg_Err( p_access,
                  "please specify url=user:password@host:port/mountpoint" );
@@ -239,16 +240,10 @@ static int Open( vlc_object_t *p_this )
     else
         free( val.psz_string );
 
-    var_Get( p_access, SOUT_CFG_PREFIX "bitrate", &val );
-    if( *val.psz_string )
-        psz_bitrate = val.psz_string;
-    else
-        free( val.psz_string );
-
     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_HTTP ) != 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
@@ -258,8 +253,7 @@ static int Open( vlc_object_t *p_this )
          || 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_audio_info( p_shout, SHOUT_AI_BITRATE, psz_bitrate ) != SHOUTERR_SUCCESS
-//       || shout_set_nonblocking( p_shout, 1 ) != SHOUTERR_SUCCESS
+         /* || shout_set_nonblocking( p_shout, 1 ) != SHOUTERR_SUCCESS */
       )
     {
         msg_Err( p_access, "failed to initialize shout streaming to %s:%i/%s",
@@ -269,11 +263,10 @@ static int Open( vlc_object_t *p_this )
         return VLC_EGENERIC;
     }
 
-    if( psz_name ) free( psz_name );
-    if( psz_description ) free( psz_description );
-    if( psz_genre ) free( psz_genre );
-    if( psz_url ) free( psz_url );
-    if( psz_bitrate ) free( psz_bitrate );
+    free( psz_name );
+    free( psz_description );
+    free( psz_genre );
+    free( psz_url );
 
     var_Get( p_access, SOUT_CFG_PREFIX "mp3", &val );
     if( val.b_bool == VLC_TRUE )
@@ -289,90 +282,105 @@ static int Open( vlc_object_t *p_this )
         return VLC_EGENERIC;
     }
 
-    var_Get( p_access, SOUT_CFG_PREFIX "samplerate", &val );
+    /* 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 */
+    var_Get( p_access, SOUT_CFG_PREFIX "bitrate", &val );
     if( *val.psz_string )
-        i_ret = shout_set_audio_info( p_shout, SHOUT_AI_SAMPLERATE, val.psz_string );
+    {
+        i_ret = shout_set_audio_info( p_shout, SHOUT_AI_BITRATE, val.psz_string );
+        if( i_ret != SHOUTERR_SUCCESS )
+        {
+            msg_Err( p_access, "failed to set the information about the bitrate" );
+            free( p_access->p_sys );
+            free( psz_accessname );
+            return VLC_EGENERIC;
+        }
+    }
     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)" );
         free( val.psz_string );
+    }
 
-    if( i_ret != SHOUTERR_SUCCESS )
+    /* 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 */
+
+    var_Get( p_access, SOUT_CFG_PREFIX "samplerate", &val );
+    if( *val.psz_string )
     {
-        msg_Err( p_access, "failed to set the information about the samplerate" );
-        free( p_access->p_sys );
-        free( psz_accessname );
-        return VLC_EGENERIC;
+        i_ret = shout_set_audio_info( p_shout, SHOUT_AI_SAMPLERATE, val.psz_string );
+        if( i_ret != SHOUTERR_SUCCESS )
+        {
+            msg_Err( p_access, "failed to set the information about the samplerate" );
+            free( p_access->p_sys );
+            free( psz_accessname );
+            return VLC_EGENERIC;
+        }
     }
+    else
+        free( val.psz_string );
 
     var_Get( p_access, SOUT_CFG_PREFIX "channels", &val );
     if( *val.psz_string )
+    {
         i_ret = shout_set_audio_info( p_shout, SHOUT_AI_CHANNELS, val.psz_string );
+        if( i_ret != SHOUTERR_SUCCESS )
+        {
+            msg_Err( p_access, "failed to set the information about the number of channels" );
+            free( p_access->p_sys );
+            free( psz_accessname );
+            return VLC_EGENERIC;
+        }
+    }
     else
         free( val.psz_string );
 
-    if( i_ret != SHOUTERR_SUCCESS )
-    {
-        msg_Err( p_access, "failed to set the information about the number of channels" );
-        free( p_access->p_sys );
-        free( psz_accessname );
-        return VLC_EGENERIC;
-    }
-
     var_Get( p_access, SOUT_CFG_PREFIX "quality", &val );
     if( *val.psz_string )
+    {
         i_ret = shout_set_audio_info( p_shout, SHOUT_AI_QUALITY, val.psz_string );
+        if( i_ret != SHOUTERR_SUCCESS )
+        {
+            msg_Err( p_access, "failed to set the information about Ogg Vorbis quality" );
+            free( p_access->p_sys );
+            free( psz_accessname );
+            return VLC_EGENERIC;
+        }
+    }
     else
         free( val.psz_string );
 
-    if( i_ret != SHOUTERR_SUCCESS )
-    {
-        msg_Err( p_access, "failed to set the information about Ogg Vorbis quality" );
-        free( p_access->p_sys );
-        free( psz_accessname );
-        return VLC_EGENERIC;
-    }
-
     var_Get( p_access, SOUT_CFG_PREFIX "public", &val );
     if( val.b_bool == VLC_TRUE )
-        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" );
-        free( p_access->p_sys );
-        free( psz_accessname );
-        return VLC_EGENERIC;
-    }
-
-    i_ret = shout_open( p_shout );
-    if( i_ret == SHOUTERR_SUCCESS )
     {
-        i_ret = SHOUTERR_CONNECTED;
-    }
-    else
-    {
-       /* If default 'http' protocol for icecast 2.x fails, fall back to 'icy'
-          for shoutcast server */
-        msg_Warn( p_access, "failed to connect using 'http' (icecast 2.x) protocol, "
-                            "switching to 'icy' (shoutcast)" );
-
-       i_ret = shout_get_format( p_shout );
-       if( i_ret != SHOUT_FORMAT_MP3 )
+        i_ret = shout_set_public( p_shout, 1 );
+        if( i_ret != SHOUTERR_SUCCESS )
         {
-            msg_Err( p_access, "failed to use 'icy' protocol: only MP3 " \
-                               "streaming to shoutcast is supported" );
+            msg_Err( p_access, "failed to set the server status setting to public" );
             free( p_access->p_sys );
             free( psz_accessname );
             return VLC_EGENERIC;
         }
+    }
 
-       /* Shout parameters cannot be changed on an open connection */
+    /* 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;
         }
 
-       i_ret = shout_set_protocol( p_shout, SHOUT_PROTOCOL_ICY );
+        /* 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'" );
@@ -380,35 +388,56 @@ static int Open( vlc_object_t *p_this )
             free( psz_accessname );
             return VLC_EGENERIC;
         }
-
-        /* Server status public requires the shout bitrate to be specified */
-       if( shout_get_audio_info ( p_shout, SHOUT_AI_BITRATE ) == "0" )
+        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
         {
-            if ( shout_get_public ( p_shout ))
+            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_Warn( p_access, "server status public for shoutcast not supported: " \
-                                    "no bitrate specified" );
+                msg_Err( p_access, "failed to set the protocol to 'http'" );
                 free( p_access->p_sys );
                 free( psz_accessname );
                 return VLC_EGENERIC;
             }
+            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 " );
         }
-
-        i_ret = shout_open( p_shout );
-        if( i_ret == SHOUTERR_SUCCESS )
+/*
+        for non-blocking, use:
+        while( i_ret == SHOUTERR_BUSY )
         {
-            i_ret = SHOUTERR_CONNECTED;
+            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 );
         }
     }
 
-/*
-    for non-blocking, use:
-    while( i_ret == SHOUTERR_BUSY )
-    {
-        sleep( 1 );
-        i_ret = shout_get_connected( p_shout );
-    }
-*/
     if( i_ret != SHOUTERR_CONNECTED )
     {
         msg_Err( p_access, "failed to open shout stream to %s:%i/%s: %s",
@@ -419,7 +448,6 @@ static int Open( vlc_object_t *p_this )
     }
 
     p_access->pf_write = Write;
-    p_access->pf_read  = Read;
     p_access->pf_seek  = Seek;
 
     msg_Dbg( p_access, "shout access output opened (%s@%s:%i/%s)",
@@ -459,19 +487,10 @@ static void Close( vlc_object_t * p_this )
     msg_Dbg( p_access, "shout access output closed" );
 }
 
-/*****************************************************************************
- * Read: standard read -- not supported
- *****************************************************************************/
-static int Read( sout_access_out_t *p_access, block_t *p_buffer )
-{
-    msg_Err( p_access, "cannot read from shout" );
-    return VLC_EGENERIC;
-}
-
 /*****************************************************************************
  * Write: standard write
  *****************************************************************************/
-static int Write( sout_access_out_t *p_access, block_t *p_buffer )
+static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
 {
     size_t i_write = 0;
 
@@ -490,6 +509,24 @@ static int Write( sout_access_out_t *p_access, block_t *p_buffer )
         {
             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_Release( p_buffer );