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