]> git.sesse.net Git - vlc/blob - modules/access_output/shout.c
* added --sout-shout-name to set a stream name
[vlc] / modules / access_output / shout.c
1 /*****************************************************************************
2  * shout.c: This module forwards vorbis streams to an icecast server
3  *****************************************************************************
4  * Copyright (C) 2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Daniel Fischer <dan at subsignal dot org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Some Comments:
26  *
27  * o this only works for ogg streams, and there's no checking about that yet.
28  * o there's a memleak somewhere, quite huge. i'm unsure if its somewhere
29  *   in libshout, in vlc or even in this patch...
30  *
31  * Typical usage:
32  *
33  * vlc v4l:/dev/video:input=2:norm=pal:size=192x144 \
34  * --sout '#transcode{vcodec=theora,vb=300,acodec=vorb,ab=96}\
35  * :std{access=shout,mux=ogg,url=localhost:8005}'
36  *
37  *****************************************************************************/
38
39 /*****************************************************************************
40  * Preamble
41  *****************************************************************************/
42 #include <string.h>
43
44 #include <vlc/vlc.h>
45 #include <vlc/sout.h>
46
47 #include <shout/shout.h>
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 static int  Open ( vlc_object_t * );
53 static void Close( vlc_object_t * );
54
55 #define SOUT_CFG_PREFIX "sout-shout-"
56
57 #define NAME_TEXT N_("Stream-name")
58 #define NAME_LONGTEXT N_("The name this stream/channel will get on the icecast server." )
59
60 #define DESCRIPTION_TEXT N_("Stream-description")
61 #define DESCRIPTION_LONGTEXT N_("A description of the stream content. (Information about " \
62                          "your channel)." )
63
64 #define MP3_TEXT N_("Stream MP3")
65 #define MP3_LONGTEXT N_("Normally you have to feed the shoutcast module with Ogg streams. " \
66                          "This option allows you to feed MP3 streams instead, so you can " \
67                          "forward MP3 streams to the icecast server." )
68
69 vlc_module_begin();
70     set_description( _("libshout (icecast) output") );
71     set_shortname( N_("Shout" ));
72     set_capability( "sout access", 50 );
73     set_category( CAT_SOUT );
74     set_subcategory( SUBCAT_SOUT_ACO );
75     add_shortcut( "shout" );
76     add_string( SOUT_CFG_PREFIX "name", "VLC media player - Live stream", NULL,
77                 NAME_TEXT, NAME_LONGTEXT, VLC_FALSE );
78     add_string( SOUT_CFG_PREFIX "description", "Live stream from VLC media player. " \
79                 "http://www.videolan.org/vlc", NULL,
80                 DESCRIPTION_TEXT, DESCRIPTION_LONGTEXT, VLC_FALSE );
81     add_bool(   SOUT_CFG_PREFIX "mp3", VLC_FALSE, NULL,
82                 MP3_TEXT, MP3_LONGTEXT, VLC_TRUE );
83     set_callbacks( Open, Close );
84 vlc_module_end();
85
86 /*****************************************************************************
87  * Exported prototypes
88  *****************************************************************************/
89 static const char *ppsz_sout_options[] = {
90     "name", "description", "mp3", NULL
91 };
92
93
94 /*****************************************************************************
95  * Exported prototypes
96  *****************************************************************************/
97 static int Write( sout_access_out_t *, block_t * );
98 static int Seek ( sout_access_out_t *, off_t  );
99 static int Read ( sout_access_out_t *, block_t * );
100
101 struct sout_access_out_sys_t
102 {
103     shout_t *p_shout;
104 };
105
106 /*****************************************************************************
107  * Open: open the shout connection
108  *****************************************************************************/
109 static int Open( vlc_object_t *p_this )
110 {
111     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
112     sout_access_out_sys_t *p_sys;
113     shout_t *p_shout;
114     long i_ret;
115     unsigned int i_port;
116     vlc_value_t val;
117
118     char *psz_accessname = NULL;
119     char *psz_parser = NULL;
120     char *psz_user = NULL;
121     char *psz_pass = NULL;
122     char *psz_host = NULL;
123     char *psz_mount = NULL;
124     char *psz_name = NULL;
125     char *psz_description = NULL;
126     char *tmp_port = NULL;
127   
128     sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
129
130     psz_accessname = psz_parser = strdup( p_access->psz_name );
131
132     if( !p_access->psz_name )
133     {
134         msg_Err( p_access,
135                  "please specify url=user:password@host:port/mountpoint" );
136         return VLC_EGENERIC;
137     }
138
139     /* Parse connection data user:pwd@host:port/mountpoint */
140     psz_user = psz_parser;
141     while( psz_parser[0] && psz_parser[0] != ':' ) psz_parser++;
142     if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
143     psz_pass = psz_parser;
144     while( psz_parser[0] && psz_parser[0] != '@' ) psz_parser++;
145     if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
146     psz_host = psz_parser;
147     while( psz_parser[0] && psz_parser[0] != ':' ) psz_parser++;
148     if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
149     tmp_port = psz_parser;
150     while( psz_parser[0] && psz_parser[0] != '/' ) psz_parser++;
151     if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
152     psz_mount = psz_parser;
153
154     i_port = atoi( tmp_port );
155
156     p_sys = p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) );
157     if( !p_sys )
158     {
159         msg_Err( p_access, "out of memory" );
160         free( psz_accessname );
161         return VLC_ENOMEM;
162     }
163
164     var_Get( p_access, SOUT_CFG_PREFIX "name", &val );
165     if( *val.psz_string )
166         psz_name = val.psz_string;
167     else
168         free( val.psz_string );
169
170     var_Get( p_access, SOUT_CFG_PREFIX "description", &val );
171     if( *val.psz_string )
172         psz_description = val.psz_string;
173     else
174         free( val.psz_string );
175
176     p_shout = p_sys->p_shout = shout_new();
177     if( !p_shout
178          || shout_set_host( p_shout, psz_host ) != SHOUTERR_SUCCESS
179          || shout_set_protocol( p_shout, SHOUT_PROTOCOL_HTTP )
180              != SHOUTERR_SUCCESS
181          || shout_set_port( p_shout, i_port ) != SHOUTERR_SUCCESS
182          || shout_set_password( p_shout, psz_pass ) != SHOUTERR_SUCCESS
183          || shout_set_mount( p_shout, psz_mount ) != SHOUTERR_SUCCESS
184          || shout_set_user( p_shout, psz_user ) != SHOUTERR_SUCCESS
185          || shout_set_agent( p_shout, "VLC media player " VERSION ) != SHOUTERR_SUCCESS
186          || shout_set_name( p_shout, psz_name ) != SHOUTERR_SUCCESS
187          || shout_set_description( p_shout, psz_description ) != SHOUTERR_SUCCESS 
188 //       || shout_set_nonblocking( p_shout, 1 ) != SHOUTERR_SUCCESS
189       )
190     {
191         msg_Err( p_access, "failed to initialize shout streaming to %s:%i/%s",
192                  psz_host, i_port, psz_mount );
193         free( p_access->p_sys );
194         free( psz_accessname );
195         return VLC_EGENERIC;
196     }
197
198     if( psz_name ) free( psz_name );
199     if( psz_description ) free( psz_description );
200
201     var_Get( p_access, SOUT_CFG_PREFIX "mp3", &val );
202     if( val.b_bool == VLC_TRUE )
203         i_ret = shout_set_format( p_shout, SHOUT_FORMAT_MP3 );
204     else
205         i_ret = shout_set_format( p_shout, SHOUT_FORMAT_OGG );
206
207     if( i_ret != SHOUTERR_SUCCESS )
208     {
209         msg_Err( p_access, "failed to set the shoutcast streaming format" );
210         free( p_access->p_sys );
211         free( psz_accessname );
212         return VLC_EGENERIC;
213     }
214
215     i_ret = shout_open( p_shout );
216     if( i_ret == SHOUTERR_SUCCESS )
217     {
218         i_ret = SHOUTERR_CONNECTED;
219     }
220
221 /*
222     for non-blocking, use:
223     while( i_ret == SHOUTERR_BUSY )
224     {
225         sleep( 1 );
226         i_ret = shout_get_connected( p_shout );
227     }
228 */
229     if( i_ret != SHOUTERR_CONNECTED )
230     {
231         msg_Err( p_access, "failed to open shout stream to %s:%i/%s: %s",
232                  psz_host, i_port, psz_mount, shout_get_error(p_shout) );
233         free( p_access->p_sys );
234         free( psz_accessname );
235         return VLC_EGENERIC;
236     }
237
238     p_access->pf_write = Write;
239     p_access->pf_read  = Read;
240     p_access->pf_seek  = Seek;
241
242     msg_Dbg( p_access, "shout access output opened (%s@%s:%i/%s)",
243              psz_user, psz_host, i_port, psz_mount );
244
245     /* Update pace control flag */
246     if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
247     {
248         p_access->p_sout->i_out_pace_nocontrol++;
249     }
250
251     free( psz_accessname );
252
253     return VLC_SUCCESS;
254 }
255
256 /*****************************************************************************
257  * Close: close the target
258  *****************************************************************************/
259 static void Close( vlc_object_t * p_this )
260 {
261     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
262
263     if( p_access->p_sys && p_access->p_sys->p_shout )
264     {
265         shout_close( p_access->p_sys->p_shout );
266         shout_shutdown();
267     }
268     free( p_access->p_sys );
269
270     /* Update pace control flag */
271     if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
272     {
273         p_access->p_sout->i_out_pace_nocontrol--;
274     }
275
276     msg_Dbg( p_access, "shout access output closed" );
277 }
278
279 /*****************************************************************************
280  * Read: standard read -- not supported
281  *****************************************************************************/
282 static int Read( sout_access_out_t *p_access, block_t *p_buffer )
283 {
284     msg_Err( p_access, "cannot read from shout" );
285     return VLC_EGENERIC;
286 }
287
288 /*****************************************************************************
289  * Write: standard write
290  *****************************************************************************/
291 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
292 {
293     size_t i_write = 0;
294
295     shout_sync( p_access->p_sys->p_shout );
296     while( p_buffer )
297     {
298         block_t *p_next = p_buffer->p_next;
299
300         if( shout_send( p_access->p_sys->p_shout,
301                         p_buffer->p_buffer, p_buffer->i_buffer )
302              == SHOUTERR_SUCCESS )
303         {
304             i_write += p_buffer->i_buffer;
305         }
306         else
307         {
308             msg_Err( p_access, "cannot write to stream: %s",
309                      shout_get_error(p_access->p_sys->p_shout) );
310         }
311         block_Release( p_buffer );
312
313         /* XXX: Unsure if that's the cause for some audio trouble... */
314
315         p_buffer = p_next;
316     }
317
318     return i_write;
319 }
320
321 /*****************************************************************************
322  * Seek: seek to a specific location -- not supported
323  *****************************************************************************/
324 static int Seek( sout_access_out_t *p_access, off_t i_pos )
325 {
326     msg_Err( p_access, "cannot seek on shout" );
327     return VLC_EGENERIC;
328 }
329