]> git.sesse.net Git - vlc/blob - modules/mux/mpjpeg.c
Error handling
[vlc] / modules / mux / mpjpeg.c
1 /*****************************************************************************
2  * mpjpeg.c: mime multipart jpeg  muxer module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001, 2002, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <vlc/vlc.h>
29 #include <vlc_sout.h>
30 #include <vlc_block.h>
31 #include <vlc_codecs.h>
32
33 #define SEPARATOR_TEXT N_( "Multipart separator string" )
34 #define SEPARATOR_LONGTEXT N_( "Multipart strings like MPJPEG use a " \
35                                "specific string to separate its content " \
36                                "pieces. You can select this string. " \
37                                "Default is --myboundary" )
38
39
40 #define CONTENT_TYPE "Content-Type: image/jpeg"
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  Open   ( vlc_object_t * );
45 static void Close  ( vlc_object_t * );
46
47 #define SOUT_CFG_PREFIX "sout-mpjpeg-"
48
49 vlc_module_begin();
50     set_shortname( "MPJPEG" );
51     set_description( _("Multipart JPEG muxer") );
52     set_capability( "sout mux", 5 );
53     add_string( SOUT_CFG_PREFIX "separator", "--myboundary", NULL,
54                               SEPARATOR_TEXT, SEPARATOR_LONGTEXT, VLC_TRUE );
55     set_category( CAT_SOUT );
56     set_subcategory( SUBCAT_SOUT_MUX );
57     set_callbacks( Open, Close );
58     add_shortcut( "mpjpeg" );
59 vlc_module_end();
60
61 /*****************************************************************************
62  * Exported prototypes
63  *****************************************************************************/
64 static const char *ppsz_sout_options[] = { "separator", NULL };
65
66 static int Control  ( sout_mux_t *, int, va_list );
67 static int AddStream( sout_mux_t *, sout_input_t * );
68 static int DelStream( sout_mux_t *, sout_input_t * );
69 static int Mux      ( sout_mux_t * );
70
71 struct sout_mux_sys_t
72 {
73     block_t *p_separator;
74     vlc_bool_t b_send_headers;
75 };
76
77 /*****************************************************************************
78  * Open:
79  *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
81 {
82     sout_mux_t *p_mux = (sout_mux_t*)p_this;
83     sout_mux_sys_t  *p_sys;
84     char *psz_separator_block, *psz_separator;
85
86     msg_Dbg( p_mux, "Multipart jpeg muxer opened" );
87     psz_separator = var_GetNonEmptyString( p_mux, SOUT_CFG_PREFIX"separator" );
88     if( psz_separator == NULL )
89     {
90         msg_Err( p_this, "missing required multipart separator" );
91         return VLC_EGENERIC;
92     }
93
94     config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options,
95                        p_mux->p_cfg );
96
97     p_sys = p_mux->p_sys = malloc( sizeof(sout_mux_sys_t) );
98     if( p_sys == NULL )
99         return VLC_ENOMEM;
100     p_sys->b_send_headers = VLC_TRUE;
101
102     if( asprintf( &psz_separator_block, "\r\n%s\r\n%s\r\n", psz_separator,
103                   CONTENT_TYPE ) == -1 )
104         psz_separator_block = NULL;
105     free( psz_separator_block );
106
107     if( psz_separator_block == NULL )
108     {
109         free( p_sys );
110         return VLC_ENOMEM;
111     }
112
113     p_sys->p_separator = block_New( p_mux, strlen( psz_separator_block ) );
114     strcpy( (char *)p_sys->p_separator->p_buffer, psz_separator_block );
115
116     p_mux->pf_control   = Control;
117     p_mux->pf_addstream = AddStream;
118     p_mux->pf_delstream = DelStream;
119     p_mux->pf_mux       = Mux;
120
121     return VLC_SUCCESS;
122 }
123
124 /*****************************************************************************
125  * Close:
126  *****************************************************************************/
127
128 static void Close( vlc_object_t * p_this )
129 {
130     sout_mux_t *p_mux = (sout_mux_t*)p_this;
131     sout_mux_sys_t *p_sys = p_mux->p_sys;
132
133     msg_Dbg( p_mux, "Multipart jpeg muxer closed" );
134     block_Release( p_sys->p_separator );
135     free( p_sys );
136 }
137
138 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
139 {
140     vlc_bool_t *pb_bool;
141     char **ppsz;
142
143    switch( i_query )
144    {
145        case MUX_CAN_ADD_STREAM_WHILE_MUXING:
146            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
147            *pb_bool = VLC_TRUE;
148            return VLC_SUCCESS;
149
150        case MUX_GET_ADD_STREAM_WAIT:
151            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
152            *pb_bool = VLC_TRUE;
153            return VLC_SUCCESS;
154
155        case MUX_GET_MIME:
156            ppsz = (char**)va_arg( args, char ** );
157            *ppsz = strdup( "multipart/x-mixed-replace; boundary=This Random String" );
158            return VLC_SUCCESS;
159
160         default:
161             return VLC_EGENERIC;
162    }
163 }
164
165 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
166 {
167     if( p_mux->i_nb_inputs > 1 )
168     {
169         msg_Dbg( p_mux, "only 1 input allowed" );
170         return VLC_EGENERIC;
171     }
172
173     msg_Dbg( p_mux, "adding input" );
174     if( p_input->p_fmt->i_codec != VLC_FOURCC('M','J','P','G') &&
175         p_input->p_fmt->i_codec != VLC_FOURCC('m','j','p','g') &&
176         p_input->p_fmt->i_codec != VLC_FOURCC('j','p','e','g') &&
177         p_input->p_fmt->i_codec != VLC_FOURCC('J','P','E','G') &&
178         p_input->p_fmt->i_codec != VLC_FOURCC('J','F','I','F') &&
179         p_input->p_fmt->i_codec != VLC_FOURCC('J','P','G','L') &&
180         p_input->p_fmt->i_codec != VLC_FOURCC('m','j','p','a') )
181     {
182         return VLC_EGENERIC;
183     }
184
185     return VLC_SUCCESS;
186 }
187
188 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
189 {
190     msg_Dbg( p_mux, "removing input" );
191     return VLC_SUCCESS;
192 }
193
194 static int Mux( sout_mux_t *p_mux )
195 {
196     block_fifo_t *p_fifo;
197     sout_mux_sys_t *p_sys = p_mux->p_sys;
198     int i_count;
199     /* Content-Length:.......\r\n */
200     char psz_content_length[25];
201
202     if( p_sys->b_send_headers )
203     {
204         block_t *p_header;
205         char *psz_separator = var_CreateGetString( p_mux,
206                                              SOUT_CFG_PREFIX "separator" );
207         char *psz_separator_block = (char *)malloc( strlen( psz_separator ) +
208                                               2 + strlen( CONTENT_TYPE ) );
209
210         sprintf( psz_separator_block, "%s\r\n%s\r\n", psz_separator,
211                                       CONTENT_TYPE );
212
213         p_header = block_New( p_mux, strlen( psz_separator_block ) );
214         memcpy( p_header->p_buffer, psz_separator_block ,
215                                     strlen( psz_separator_block ) );
216         p_header->i_flags |= BLOCK_FLAG_HEADER;
217         sout_AccessOutWrite( p_mux->p_access, p_header );
218         p_sys->b_send_headers = VLC_FALSE;
219         if( psz_separator_block ) free( psz_separator_block );
220     }
221
222     if( !p_mux->i_nb_inputs ) return VLC_SUCCESS;
223
224     p_fifo = p_mux->pp_inputs[0]->p_fifo;
225     i_count = block_FifoCount( p_fifo );
226     while( i_count > 0 )
227     {
228         block_t *p_length = block_New( p_mux, 25 );
229         block_t *p_data = block_FifoGet( p_fifo );
230         sout_AccessOutWrite( p_mux->p_access,
231                              block_Duplicate( p_sys->p_separator ) );
232         memset( psz_content_length, 0, 25 );
233         snprintf( psz_content_length, 25, "Content-Length: %i\r\n\r\n",
234                                           p_data->i_buffer );
235         memcpy( p_length->p_buffer, psz_content_length, 25 );
236         sout_AccessOutWrite( p_mux->p_access, p_length );
237         sout_AccessOutWrite( p_mux->p_access, p_data );
238
239         i_count--;
240     }
241
242     return VLC_SUCCESS;
243 }