]> git.sesse.net Git - vlc/blob - modules/access_output/shout.c
6e31f0e2ccd45c9334a9eeb72e791ff9803e80e1
[vlc] / modules / access_output / shout.c
1 /*****************************************************************************
2  * shout.c
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 i have lots of problems with audio, but i think they are not caused
29  *   by this patch
30  * o there's a memleak somewhere, quite huge. i'm unsure if its somewhere
31  *   in libshout, in vlc or even in this patch...
32  *
33  * Typical usage:
34  *
35  * vlc v4l:/dev/video:input=2:norm=pal:size=192x144 \
36  * --sout '#transcode{vcodec=theora,vb=300,acodec=vorb,ab=96}\
37  * :std{access=shout,mux=ogg,url=localhost:8005}'
38  *
39  *****************************************************************************/
40
41 /*****************************************************************************
42  * Preamble
43  *****************************************************************************/
44 #include <string.h>
45
46 #include <vlc/vlc.h>
47 #include <vlc/sout.h>
48
49 #include <shout/shout.h>
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 static int  Open ( vlc_object_t * );
55 static void Close( vlc_object_t * );
56
57 #define SOUT_CFG_PREFIX "sout-shout-"
58
59 vlc_module_begin();
60     set_description( _("libshout (icecast) output") );
61     set_shortname( N_("Shout" ));
62     set_capability( "sout access", 50 );
63     set_category( CAT_SOUT );
64     set_subcategory( SUBCAT_SOUT_ACO );
65     add_shortcut( "shout" );
66     set_callbacks( Open, Close );
67 vlc_module_end();
68
69
70 /*****************************************************************************
71  * Exported prototypes
72  *****************************************************************************/
73 static int Write( sout_access_out_t *, block_t * );
74 static int Seek ( sout_access_out_t *, off_t  );
75 static int Read ( sout_access_out_t *, block_t * );
76
77 struct sout_access_out_sys_t
78 {
79     shout_t *p_shout;
80 };
81
82 /*****************************************************************************
83  * Open: open the shout connection
84  *****************************************************************************/
85 static int Open( vlc_object_t *p_this )
86 {
87     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
88     sout_access_out_sys_t *p_sys;
89     shout_t *p_shout;
90     long i_ret;
91
92     char *psz_user, *psz_pass, *psz_host, *psz_mount;
93     unsigned int i_port;
94
95     char *parser = strdup( p_access->psz_name );
96     char *tmp_port;
97
98     if( !p_access->psz_name )
99     {
100         msg_Err( p_access,
101                  "please specify url=user:password@host:port/mountpoint" );
102         return VLC_EGENERIC;
103     }
104
105     /* Parse connection data user:pwd@host:port/mountpoint */
106     psz_user = parser;
107     while( parser[0] && parser[0] != ':' ) parser++;
108     if( parser[0] ) { parser[0] = 0; parser++; }
109     psz_pass = parser;
110     while( parser[0] && parser[0] != '@' ) parser++;
111     if( parser[0] ) { parser[0] = 0; parser++; }
112     psz_host = parser;
113     while( parser[0] && parser[0] != ':' ) parser++;
114     if( parser[0] ) { parser[0] = 0; parser++; }
115     tmp_port = parser;
116     while( parser[0] && parser[0] != '/' ) parser++;
117     if( parser[0] ) { parser[0] = 0; parser++; }
118     psz_mount = parser;
119
120     i_port = atoi( tmp_port );
121
122     p_sys = p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) );
123     if( !p_sys )
124     {
125         msg_Err( p_access, "out of memory" );
126         free( parser );
127         return VLC_ENOMEM;
128     }
129
130     p_shout = p_sys->p_shout = shout_new();
131     if( !p_shout
132          || shout_set_host( p_shout, psz_host ) != SHOUTERR_SUCCESS
133          || shout_set_protocol( p_shout, SHOUT_PROTOCOL_HTTP )
134              != SHOUTERR_SUCCESS
135          || shout_set_port( p_shout, i_port ) != SHOUTERR_SUCCESS
136          || shout_set_password( p_shout, psz_pass ) != SHOUTERR_SUCCESS
137          || shout_set_mount( p_shout, psz_mount ) != SHOUTERR_SUCCESS
138          || shout_set_user( p_shout, psz_user ) != SHOUTERR_SUCCESS
139          || shout_set_format( p_shout, SHOUT_FORMAT_OGG ) != SHOUTERR_SUCCESS
140   //       || shout_set_nonblocking( p_shout, 1 ) != SHOUTERR_SUCCESS
141       )
142     {
143         msg_Err( p_access, "failed to initialize shout streaming to %s:%i%s",
144                  psz_host, i_port, psz_mount );
145         free( p_access->p_sys );
146         free( parser );
147         return VLC_EGENERIC;
148     }
149
150     i_ret = shout_open( p_shout );
151     if( i_ret == SHOUTERR_SUCCESS )
152     {
153         i_ret = SHOUTERR_CONNECTED;
154     }
155
156 /*
157     for non-blocking, use:
158     while( i_ret == SHOUTERR_BUSY )
159     {
160         sleep( 1 );
161         i_ret = shout_get_connected( p_shout );
162     }
163 */
164     if( i_ret != SHOUTERR_CONNECTED )
165     {
166         msg_Err( p_access, "failed to open shout stream to %s:%i%s: %s",
167                  psz_host, i_port, psz_mount, shout_get_error(p_shout) );
168         free( p_access->p_sys );
169         free( parser );
170         return VLC_EGENERIC;
171     }
172
173     p_access->pf_write = Write;
174     p_access->pf_read  = Read;
175     p_access->pf_seek  = Seek;
176
177     msg_Dbg( p_access, "shout access output opened (%s@%s:%i%s)",
178              psz_user, psz_host, i_port, psz_mount );
179
180     /* Update pace control flag */
181     if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
182     {
183         p_access->p_sout->i_out_pace_nocontrol++;
184     }
185
186     /* FIXME: it should be free()d somewhere, but not here.... ? */
187     //free( parser );
188
189     return VLC_SUCCESS;
190 }
191
192 /*****************************************************************************
193  * Close: close the target
194  *****************************************************************************/
195 static void Close( vlc_object_t * p_this )
196 {
197     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
198
199     if( p_access->p_sys && p_access->p_sys->p_shout )
200     {
201         shout_close( p_access->p_sys->p_shout );
202         shout_shutdown();
203     }
204     free( p_access->p_sys );
205
206     /* Update pace control flag */
207     if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
208     {
209         p_access->p_sout->i_out_pace_nocontrol--;
210     }
211
212     msg_Dbg( p_access, "shout access output closed" );
213 }
214
215 /*****************************************************************************
216  * Read: standard read -- not supported
217  *****************************************************************************/
218 static int Read( sout_access_out_t *p_access, block_t *p_buffer )
219 {
220     msg_Err( p_access, "cannot read from shout" );
221     return VLC_EGENERIC;
222 }
223
224 /*****************************************************************************
225  * Write: standard write
226  *****************************************************************************/
227 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
228 {
229     size_t i_write = 0;
230
231     while( p_buffer )
232     {
233         block_t *p_next = p_buffer->p_next;
234
235         if( shout_send( p_access->p_sys->p_shout,
236                         p_buffer->p_buffer, p_buffer->i_buffer )
237              == SHOUTERR_SUCCESS )
238         {
239             i_write += p_buffer->i_buffer;
240         }
241         else
242         {
243             msg_Err( p_access, "cannot write to stream: %s",
244                      shout_get_error(p_access->p_sys->p_shout) );
245         }
246         block_Release( p_buffer );
247
248         /* XXX: Unsure if that's the cause for some audio trouble... */
249         //shout_sync( p_access->p_sys->p_shout );
250
251         p_buffer = p_next;
252     }
253
254     return i_write;
255 }
256
257 /*****************************************************************************
258  * Seek: seek to a specific location -- not supported
259  *****************************************************************************/
260 static int Seek( sout_access_out_t *p_access, off_t i_pos )
261 {
262     msg_Err( p_access, "cannot seek on shout" );
263     return VLC_EGENERIC;
264 }
265