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