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 )
83 sout_mux_t *p_mux = (sout_mux_t*)p_this;
84 sout_mux_sys_t *p_sys;
85 char *psz_separator_block, *psz_separator;
87 msg_Dbg( p_mux, "Multipart jpeg muxer opened" );
88 config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
90 p_sys = p_mux->p_sys = malloc( sizeof(sout_mux_sys_t) );
91 p_sys->b_send_headers = VLC_TRUE;
93 psz_separator = var_GetString( p_mux, SOUT_CFG_PREFIX "separator" );
94 i_size = strlen( psz_separator ) + 2 + 2 + 2 + strlen( CONTENT_TYPE );
95 psz_separator_block = (char*)malloc( i_size );
96 sprintf( psz_separator_block, "\r\n%s\r\n%s\r\n", psz_separator,
98 p_sys->p_separator = block_New( p_mux, i_size );
99 memcpy( p_sys->p_separator->p_buffer, psz_separator_block , i_size );
101 if( psz_separator_block ) free( psz_separator_block );
103 p_mux->pf_control = Control;
104 p_mux->pf_addstream = AddStream;
105 p_mux->pf_delstream = DelStream;
111 /*****************************************************************************
113 *****************************************************************************/
115 static void Close( vlc_object_t * p_this )
117 sout_mux_t *p_mux = (sout_mux_t*)p_this;
118 sout_mux_sys_t *p_sys = p_mux->p_sys;
120 msg_Dbg( p_mux, "Multipart jpeg muxer closed" );
121 block_Release( p_sys->p_separator );
125 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
132 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
133 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
137 case MUX_GET_ADD_STREAM_WAIT:
138 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
143 ppsz = (char**)va_arg( args, char ** );
144 *ppsz = strdup( "multipart/x-mixed-replace; boundary=This Random String" );
152 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
154 if( p_mux->i_nb_inputs > 1 )
156 msg_Dbg( p_mux, "only 1 input allowed" );
160 msg_Dbg( p_mux, "adding input" );
161 if( p_input->p_fmt->i_codec != VLC_FOURCC('M','J','P','G') &&
162 p_input->p_fmt->i_codec != VLC_FOURCC('m','j','p','g') &&
163 p_input->p_fmt->i_codec != VLC_FOURCC('j','p','e','g') &&
164 p_input->p_fmt->i_codec != VLC_FOURCC('J','P','E','G') &&
165 p_input->p_fmt->i_codec != VLC_FOURCC('J','F','I','F') &&
166 p_input->p_fmt->i_codec != VLC_FOURCC('J','P','G','L') &&
167 p_input->p_fmt->i_codec != VLC_FOURCC('m','j','p','a') )
175 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
177 msg_Dbg( p_mux, "removing input" );
181 static int Mux( sout_mux_t *p_mux )
183 block_fifo_t *p_fifo;
184 sout_mux_sys_t *p_sys = p_mux->p_sys;
186 /* Content-Length:.......\r\n */
187 char psz_content_length[25];
189 if( p_sys->b_send_headers )
192 char *psz_separator = var_CreateGetString( p_mux,
193 SOUT_CFG_PREFIX "separator" );
194 char *psz_separator_block = (char *)malloc( strlen( psz_separator ) +
195 2 + strlen( CONTENT_TYPE ) );
197 sprintf( psz_separator_block, "%s\r\n%s\r\n", psz_separator,
200 p_header = block_New( p_mux, strlen( psz_separator_block ) );
201 memcpy( p_header->p_buffer, psz_separator_block ,
202 strlen( psz_separator_block ) );
203 p_header->i_flags |= BLOCK_FLAG_HEADER;
204 sout_AccessOutWrite( p_mux->p_access, p_header );
205 p_sys->b_send_headers = VLC_FALSE;
206 if( psz_separator_block ) free( psz_separator_block );
209 if( !p_mux->i_nb_inputs ) return VLC_SUCCESS;
211 p_fifo = p_mux->pp_inputs[0]->p_fifo;
212 i_count = p_fifo->i_depth;
215 block_t *p_length = block_New( p_mux, 25 );
216 block_t *p_data = block_FifoGet( p_fifo );
217 sout_AccessOutWrite( p_mux->p_access,
218 block_Duplicate( p_sys->p_separator ) );
219 memset( psz_content_length, 0, 25 );
220 snprintf( psz_content_length, 25, "Content-Length: %i\r\n\r\n",
222 memcpy( p_length->p_buffer, psz_content_length, 25 );
223 sout_AccessOutWrite( p_mux->p_access, p_length );
224 sout_AccessOutWrite( p_mux->p_access, p_data );