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