1 /*****************************************************************************
2 * mpjpeg.c: mime multipart jpeg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 the VideoLAN team
7 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc_block.h>
31 #include <vlc_codecs.h>
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" )
40 #define CONTENT_TYPE "Content-Type: image/jpeg"
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close ( vlc_object_t * );
47 #define SOUT_CFG_PREFIX "sout-mpjpeg-"
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" );
61 /*****************************************************************************
63 *****************************************************************************/
64 static const char *ppsz_sout_options[] = { "separator", NULL };
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 * );
74 vlc_bool_t b_send_headers;
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
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;
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 )
90 msg_Err( p_this, "missing required multipart separator" );
94 config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options,
97 p_sys = p_mux->p_sys = malloc( sizeof(sout_mux_sys_t) );
100 p_sys->b_send_headers = VLC_TRUE;
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 );
107 if( psz_separator_block == NULL )
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 );
116 p_mux->pf_control = Control;
117 p_mux->pf_addstream = AddStream;
118 p_mux->pf_delstream = DelStream;
124 /*****************************************************************************
126 *****************************************************************************/
128 static void Close( vlc_object_t * p_this )
130 sout_mux_t *p_mux = (sout_mux_t*)p_this;
131 sout_mux_sys_t *p_sys = p_mux->p_sys;
133 msg_Dbg( p_mux, "Multipart jpeg muxer closed" );
134 block_Release( p_sys->p_separator );
138 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
145 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
146 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
150 case MUX_GET_ADD_STREAM_WAIT:
151 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
156 ppsz = (char**)va_arg( args, char ** );
157 *ppsz = strdup( "multipart/x-mixed-replace; boundary=This Random String" );
165 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
167 if( p_mux->i_nb_inputs > 1 )
169 msg_Dbg( p_mux, "only 1 input allowed" );
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') )
188 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
190 msg_Dbg( p_mux, "removing input" );
194 static int Mux( sout_mux_t *p_mux )
196 block_fifo_t *p_fifo;
197 sout_mux_sys_t *p_sys = p_mux->p_sys;
199 /* Content-Length:.......\r\n */
200 char psz_content_length[25];
202 if( p_sys->b_send_headers )
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 ) );
210 sprintf( psz_separator_block, "%s\r\n%s\r\n", psz_separator,
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 );
222 if( !p_mux->i_nb_inputs ) return VLC_SUCCESS;
224 p_fifo = p_mux->pp_inputs[0]->p_fifo;
225 i_count = block_FifoCount( p_fifo );
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",
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 );