]> git.sesse.net Git - vlc/blob - modules/access_output/shout.c
FSF address change.
[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  *          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,url=localhost:8005}'
37  *
38  *****************************************************************************/
39
40 /*****************************************************************************
41  * Preamble
42  *****************************************************************************/
43 #include <string.h>
44
45 #include <vlc/vlc.h>
46 #include <vlc/sout.h>
47
48 #include <shout/shout.h>
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 static int  Open ( vlc_object_t * );
54 static void Close( vlc_object_t * );
55
56 #define SOUT_CFG_PREFIX "sout-shout-"
57
58 #define NAME_TEXT N_("Stream-name")
59 #define NAME_LONGTEXT N_("The name this stream/channel will get on the icecast server." )
60
61 #define DESCRIPTION_TEXT N_("Stream-description")
62 #define DESCRIPTION_LONGTEXT N_("A description of the stream content. (Information about " \
63                          "your channel)." )
64
65 #define MP3_TEXT N_("Stream MP3")
66 #define MP3_LONGTEXT N_("Normally you have to feed the shoutcast module with Ogg streams. " \
67                          "This option allows you to feed MP3 streams instead, so you can " \
68                          "forward MP3 streams to the icecast server." )
69
70 vlc_module_begin();
71     set_description( _("libshout (icecast) output") );
72     set_shortname( "Shout" );
73     set_capability( "sout access", 50 );
74     set_category( CAT_SOUT );
75     set_subcategory( SUBCAT_SOUT_ACO );
76     add_shortcut( "shout" );
77     add_string( SOUT_CFG_PREFIX "name", "VLC media player - Live stream", NULL,
78                 NAME_TEXT, NAME_LONGTEXT, VLC_FALSE );
79     add_string( SOUT_CFG_PREFIX "description", "Live stream from VLC media player. " \
80                 "http://www.videolan.org/vlc", NULL,
81                 DESCRIPTION_TEXT, DESCRIPTION_LONGTEXT, VLC_FALSE );
82     add_bool(   SOUT_CFG_PREFIX "mp3", VLC_FALSE, NULL,
83                 MP3_TEXT, MP3_LONGTEXT, VLC_TRUE );
84     set_callbacks( Open, Close );
85 vlc_module_end();
86
87 /*****************************************************************************
88  * Exported prototypes
89  *****************************************************************************/
90 static const char *ppsz_sout_options[] = {
91     "name", "description", "mp3", NULL
92 };
93
94
95 /*****************************************************************************
96  * Exported prototypes
97  *****************************************************************************/
98 static int Write( sout_access_out_t *, block_t * );
99 static int Seek ( sout_access_out_t *, off_t  );
100 static int Read ( sout_access_out_t *, block_t * );
101
102 struct sout_access_out_sys_t
103 {
104     shout_t *p_shout;
105 };
106
107 /*****************************************************************************
108  * Open: open the shout connection
109  *****************************************************************************/
110 static int Open( vlc_object_t *p_this )
111 {
112     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
113     sout_access_out_sys_t *p_sys;
114     shout_t *p_shout;
115     long i_ret;
116     unsigned int i_port;
117     vlc_value_t val;
118
119     char *psz_accessname = NULL;
120     char *psz_parser = NULL;
121     char *psz_user = NULL;
122     char *psz_pass = NULL;
123     char *psz_host = NULL;
124     char *psz_mount = NULL;
125     char *psz_name = NULL;
126     char *psz_description = NULL;
127     char *tmp_port = NULL;
128   
129     sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
130
131     psz_accessname = psz_parser = strdup( p_access->psz_name );
132
133     if( !p_access->psz_name )
134     {
135         msg_Err( p_access,
136                  "please specify url=user:password@host:port/mountpoint" );
137         return VLC_EGENERIC;
138     }
139
140     /* Parse connection data user:pwd@host:port/mountpoint */
141     psz_user = psz_parser;
142     while( psz_parser[0] && psz_parser[0] != ':' ) psz_parser++;
143     if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
144     psz_pass = psz_parser;
145     while( psz_parser[0] && psz_parser[0] != '@' ) psz_parser++;
146     if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
147     psz_host = psz_parser;
148     while( psz_parser[0] && psz_parser[0] != ':' ) psz_parser++;
149     if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
150     tmp_port = psz_parser;
151     while( psz_parser[0] && psz_parser[0] != '/' ) psz_parser++;
152     if( psz_parser[0] ) { psz_parser[0] = 0; psz_parser++; }
153     psz_mount = psz_parser;
154
155     i_port = atoi( tmp_port );
156
157     p_sys = p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) );
158     if( !p_sys )
159     {
160         msg_Err( p_access, "out of memory" );
161         free( psz_accessname );
162         return VLC_ENOMEM;
163     }
164
165     var_Get( p_access, SOUT_CFG_PREFIX "name", &val );
166     if( *val.psz_string )
167         psz_name = val.psz_string;
168     else
169         free( val.psz_string );
170
171     var_Get( p_access, SOUT_CFG_PREFIX "description", &val );
172     if( *val.psz_string )
173         psz_description = val.psz_string;
174     else
175         free( val.psz_string );
176
177     p_shout = p_sys->p_shout = shout_new();
178     if( !p_shout
179          || shout_set_host( p_shout, psz_host ) != SHOUTERR_SUCCESS
180          || shout_set_protocol( p_shout, SHOUT_PROTOCOL_HTTP )
181              != SHOUTERR_SUCCESS
182          || shout_set_port( p_shout, i_port ) != SHOUTERR_SUCCESS
183          || shout_set_password( p_shout, psz_pass ) != SHOUTERR_SUCCESS
184          || shout_set_mount( p_shout, psz_mount ) != SHOUTERR_SUCCESS
185          || shout_set_user( p_shout, psz_user ) != SHOUTERR_SUCCESS
186          || shout_set_agent( p_shout, "VLC media player " VERSION ) != SHOUTERR_SUCCESS
187          || shout_set_name( p_shout, psz_name ) != SHOUTERR_SUCCESS
188          || shout_set_description( p_shout, psz_description ) != SHOUTERR_SUCCESS 
189 //       || shout_set_nonblocking( p_shout, 1 ) != SHOUTERR_SUCCESS
190       )
191     {
192         msg_Err( p_access, "failed to initialize shout streaming to %s:%i/%s",
193                  psz_host, i_port, psz_mount );
194         free( p_access->p_sys );
195         free( psz_accessname );
196         return VLC_EGENERIC;
197     }
198
199     if( psz_name ) free( psz_name );
200     if( psz_description ) free( psz_description );
201
202     var_Get( p_access, SOUT_CFG_PREFIX "mp3", &val );
203     if( val.b_bool == VLC_TRUE )
204         i_ret = shout_set_format( p_shout, SHOUT_FORMAT_MP3 );
205     else
206         i_ret = shout_set_format( p_shout, SHOUT_FORMAT_OGG );
207
208     if( i_ret != SHOUTERR_SUCCESS )
209     {
210         msg_Err( p_access, "failed to set the shoutcast streaming format" );
211         free( p_access->p_sys );
212         free( psz_accessname );
213         return VLC_EGENERIC;
214     }
215
216     i_ret = shout_open( p_shout );
217     if( i_ret == SHOUTERR_SUCCESS )
218     {
219         i_ret = SHOUTERR_CONNECTED;
220     }
221
222 /*
223     for non-blocking, use:
224     while( i_ret == SHOUTERR_BUSY )
225     {
226         sleep( 1 );
227         i_ret = shout_get_connected( p_shout );
228     }
229 */
230     if( i_ret != SHOUTERR_CONNECTED )
231     {
232         msg_Err( p_access, "failed to open shout stream to %s:%i/%s: %s",
233                  psz_host, i_port, psz_mount, shout_get_error(p_shout) );
234         free( p_access->p_sys );
235         free( psz_accessname );
236         return VLC_EGENERIC;
237     }
238
239     p_access->pf_write = Write;
240     p_access->pf_read  = Read;
241     p_access->pf_seek  = Seek;
242
243     msg_Dbg( p_access, "shout access output opened (%s@%s:%i/%s)",
244              psz_user, psz_host, i_port, psz_mount );
245
246     /* Update pace control flag */
247     if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
248     {
249         p_access->p_sout->i_out_pace_nocontrol++;
250     }
251
252     free( psz_accessname );
253
254     return VLC_SUCCESS;
255 }
256
257 /*****************************************************************************
258  * Close: close the target
259  *****************************************************************************/
260 static void Close( vlc_object_t * p_this )
261 {
262     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
263
264     if( p_access->p_sys && p_access->p_sys->p_shout )
265     {
266         shout_close( p_access->p_sys->p_shout );
267         shout_shutdown();
268     }
269     free( p_access->p_sys );
270
271     /* Update pace control flag */
272     if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
273     {
274         p_access->p_sout->i_out_pace_nocontrol--;
275     }
276
277     msg_Dbg( p_access, "shout access output closed" );
278 }
279
280 /*****************************************************************************
281  * Read: standard read -- not supported
282  *****************************************************************************/
283 static int Read( sout_access_out_t *p_access, block_t *p_buffer )
284 {
285     msg_Err( p_access, "cannot read from shout" );
286     return VLC_EGENERIC;
287 }
288
289 /*****************************************************************************
290  * Write: standard write
291  *****************************************************************************/
292 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
293 {
294     size_t i_write = 0;
295
296     shout_sync( p_access->p_sys->p_shout );
297     while( p_buffer )
298     {
299         block_t *p_next = p_buffer->p_next;
300
301         if( shout_send( p_access->p_sys->p_shout,
302                         p_buffer->p_buffer, p_buffer->i_buffer )
303              == SHOUTERR_SUCCESS )
304         {
305             i_write += p_buffer->i_buffer;
306         }
307         else
308         {
309             msg_Err( p_access, "cannot write to stream: %s",
310                      shout_get_error(p_access->p_sys->p_shout) );
311         }
312         block_Release( p_buffer );
313
314         /* XXX: Unsure if that's the cause for some audio trouble... */
315
316         p_buffer = p_next;
317     }
318
319     return i_write;
320 }
321
322 /*****************************************************************************
323  * Seek: seek to a specific location -- not supported
324  *****************************************************************************/
325 static int Seek( sout_access_out_t *p_access, off_t i_pos )
326 {
327     msg_Err( p_access, "cannot seek on shout" );
328     return VLC_EGENERIC;
329 }
330