]> git.sesse.net Git - vlc/blob - modules/access_output/http.c
* all: use sout_ParseCfg.
[vlc] / modules / access_output / http.c
1 /*****************************************************************************
2  * http.c
3  *****************************************************************************
4  * Copyright (C) 2001-2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/sout.h>
31
32 #include "vlc_httpd.h"
33
34 #define FREE( p ) if( p ) { free( p); (p) = NULL; }
35
36 #define DEFAULT_PORT 8080
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
43
44 #define SOUT_CFG_PREFIX "sout-http-"
45
46 vlc_module_begin();
47     set_description( _("HTTP stream ouput") );
48     set_capability( "sout access", 0 );
49     add_shortcut( "http" );
50     add_shortcut( "mmsh" );
51     add_string( SOUT_CFG_PREFIX "user", "", NULL, "User", "", VLC_TRUE );
52     add_string( SOUT_CFG_PREFIX "pwd", "", NULL, "Password", "", VLC_TRUE );
53     set_callbacks( Open, Close );
54 vlc_module_end();
55
56
57 /*****************************************************************************
58  * Exported prototypes
59  *****************************************************************************/
60 static const char *ppsz_sout_options[] = {
61     "user", "pwd", NULL
62 };
63
64 static int Write( sout_access_out_t *, block_t * );
65 static int Seek ( sout_access_out_t *, off_t  );
66
67 struct sout_access_out_sys_t
68 {
69     /* host */
70     httpd_host_t        *p_httpd_host;
71
72     /* stream */
73     httpd_stream_t      *p_httpd_stream;
74
75     /* gather header from stream */
76     int                 i_header_allocated;
77     int                 i_header_size;
78     uint8_t             *p_header;
79     vlc_bool_t          b_header_complete;
80 };
81
82 /*****************************************************************************
83  * Open: open the file
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
90     char                *psz_parser, *psz_name;
91
92     char                *psz_bind_addr;
93     int                 i_bind_port;
94     char                *psz_file_name;
95     char                *psz_user = NULL;
96     char                *psz_pwd = NULL;
97     char                *psz_mime = NULL;
98     vlc_value_t         val;
99
100     if( !( p_sys = p_access->p_sys =
101                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
102     {
103         msg_Err( p_access, "Not enough memory" );
104         return( VLC_EGENERIC );
105     }
106
107     sout_ParseCfg( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
108
109     /* p_access->psz_name host.name:port/filename */
110     psz_name = psz_parser = strdup( p_access->psz_name );
111
112     psz_bind_addr = psz_parser;
113     i_bind_port = 0;
114     psz_file_name = "";
115
116     while( *psz_parser && *psz_parser != ':' && *psz_parser != '/' )
117     {
118         psz_parser++;
119     }
120     if( *psz_parser == ':' )
121     {
122         *psz_parser = '\0';
123         psz_parser++;
124         i_bind_port = atoi( psz_parser );
125
126         while( *psz_parser && *psz_parser != '/' )
127         {
128             psz_parser++;
129         }
130     }
131     if( *psz_parser == '/' )
132     {
133         *psz_parser = '\0';
134         psz_parser++;
135         psz_file_name = psz_parser;
136     }
137
138     if( i_bind_port <= 0 )
139     {
140         i_bind_port = DEFAULT_PORT;
141     }
142
143     if( !*psz_file_name )
144     {
145         psz_file_name = strdup( "/" );
146     }
147     else if( *psz_file_name != '/' )
148     {
149         char *p = psz_file_name;
150
151         psz_file_name = malloc( strlen( p ) + 2 );
152         strcpy( psz_file_name, "/" );
153         strcat( psz_file_name, p );
154     }
155     else
156     {
157         psz_file_name = strdup( psz_file_name );
158     }
159
160     p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_access), psz_bind_addr,
161                                          i_bind_port );
162     if( p_sys->p_httpd_host == NULL )
163     {
164         msg_Err( p_access, "cannot listen on %s:%d",
165                  psz_bind_addr, i_bind_port );
166
167         free( psz_name );
168         free( psz_file_name );
169         free( p_sys );
170         return VLC_EGENERIC;
171     }
172
173     if( p_access->psz_access && !strcmp( p_access->psz_access, "mmsh" ) )
174     {
175         psz_mime = "video/x-ms-asf-stream";
176     }
177
178     var_Get( p_access, SOUT_CFG_PREFIX "user", &val );
179     if( val.psz_string && *val.psz_string )
180         psz_user = val.psz_string;
181     else if( val.psz_string )
182         free( val.psz_string );
183
184     var_Get( p_access, SOUT_CFG_PREFIX "pwd", &val );
185     if( val.psz_string && *val.psz_string )
186         psz_pwd = val.psz_string;
187     else if( val.psz_string )
188         free( val.psz_string );
189
190     p_sys->p_httpd_stream =
191         httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime,
192                          psz_user, psz_pwd );
193     if( psz_user ) free( psz_user );
194     if( psz_pwd ) free( psz_pwd );
195
196     if( p_sys->p_httpd_stream == NULL )
197     {
198         msg_Err( p_access, "cannot add stream %s", psz_file_name );
199         httpd_HostDelete( p_sys->p_httpd_host );
200
201         free( psz_name );
202         free( psz_file_name );
203         free( p_sys );
204         return VLC_EGENERIC;
205     }
206
207     free( psz_file_name );
208     free( psz_name );
209
210     p_sys->i_header_allocated = 1024;
211     p_sys->i_header_size      = 0;
212     p_sys->p_header           = malloc( p_sys->i_header_allocated );
213     p_sys->b_header_complete  = VLC_FALSE;
214
215     p_access->pf_write       = Write;
216     p_access->pf_seek        = Seek;
217
218
219     /* update p_sout->i_out_pace_nocontrol */
220     p_access->p_sout->i_out_pace_nocontrol++;
221
222     return VLC_SUCCESS;
223 }
224
225 /*****************************************************************************
226  * Close: close the target
227  *****************************************************************************/
228 static void Close( vlc_object_t * p_this )
229 {
230     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
231     sout_access_out_sys_t   *p_sys = p_access->p_sys;
232
233     /* update p_sout->i_out_pace_nocontrol */
234     p_access->p_sout->i_out_pace_nocontrol--;
235
236     httpd_StreamDelete( p_sys->p_httpd_stream );
237     httpd_HostDelete( p_sys->p_httpd_host );
238
239     FREE( p_sys->p_header );
240
241     msg_Dbg( p_access, "Close" );
242
243     free( p_sys );
244 }
245
246 /*****************************************************************************
247  * Write:
248  *****************************************************************************/
249 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
250 {
251     sout_access_out_sys_t *p_sys = p_access->p_sys;
252     int i_err = 0;
253
254     while( p_buffer )
255     {
256         block_t *p_next;
257
258         if( p_buffer->i_flags & BLOCK_FLAG_HEADER )
259         {
260             /* gather header */
261             if( p_sys->b_header_complete )
262             {
263                 /* free previously gathered header */
264                 p_sys->i_header_size = 0;
265                 p_sys->b_header_complete = VLC_FALSE;
266             }
267             if( (int)(p_buffer->i_buffer + p_sys->i_header_size) >
268                 p_sys->i_header_allocated )
269             {
270                 p_sys->i_header_allocated =
271                     p_buffer->i_buffer + p_sys->i_header_size + 1024;
272                 p_sys->p_header =
273                     realloc( p_sys->p_header, p_sys->i_header_allocated );
274             }
275             memcpy( &p_sys->p_header[p_sys->i_header_size],
276                     p_buffer->p_buffer,
277                     p_buffer->i_buffer );
278             p_sys->i_header_size += p_buffer->i_buffer;
279         }
280         else if( !p_sys->b_header_complete )
281         {
282             p_sys->b_header_complete = VLC_TRUE;
283
284             httpd_StreamHeader( p_sys->p_httpd_stream, p_sys->p_header,
285                                 p_sys->i_header_size );
286         }
287
288         /* send data */
289         i_err = httpd_StreamSend( p_sys->p_httpd_stream, p_buffer->p_buffer,
290                                   p_buffer->i_buffer );
291
292         p_next = p_buffer->p_next;
293         block_Release( p_buffer );
294         p_buffer = p_next;
295
296         if( i_err < 0 )
297         {
298             break;
299         }
300     }
301
302     if( i_err < 0 )
303     {
304         block_ChainRelease( p_buffer );
305     }
306
307     return( i_err < 0 ? VLC_EGENERIC : VLC_SUCCESS );
308 }
309
310 /*****************************************************************************
311  * Seek: seek to a specific location in a file
312  *****************************************************************************/
313 static int Seek( sout_access_out_t *p_access, off_t i_pos )
314 {
315     msg_Warn( p_access, "HTTP sout access cannot seek" );
316     return VLC_EGENERIC;
317 }