1 /*****************************************************************************
2 * mpjpeg.c: mime multipart jpeg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: dummy.c 7047 2004-03-11 17:37:50Z fenrir $
7 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #define SEPARATOR_TEXT N_( "Multipart separator string" )
33 #define SEPARATOR_LONGTEXT N_( "Multipart strings like MPJPEG use a " \
34 "separator string betwen content pieces. "\
35 "You can select this string. Default is "\
39 #define CONTENT_TYPE "Content-Type: image/jpeg"
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close ( vlc_object_t * );
46 #define SOUT_CFG_PREFIX "sout-mpjpeg-"
49 set_shortname( _("MPJPEG"));
50 set_description( _("Multipart jpeg muxer") );
51 set_capability( "sout mux", 5 );
52 add_string( SOUT_CFG_PREFIX "separator", "--myboundary", NULL,
53 SEPARATOR_TEXT, SEPARATOR_LONGTEXT, VLC_TRUE );
54 set_category( CAT_SOUT );
55 set_subcategory( SUBCAT_SOUT_MUX );
56 set_callbacks( Open, Close );
57 add_shortcut( "mpjpeg" );
60 /*****************************************************************************
62 *****************************************************************************/
63 static const char *ppsz_sout_options[] = { "separator", NULL };
65 static int Control ( sout_mux_t *, int, va_list );
66 static int AddStream( sout_mux_t *, sout_input_t * );
67 static int DelStream( sout_mux_t *, sout_input_t * );
68 static int Mux ( sout_mux_t * );
73 vlc_bool_t b_send_headers;
76 /*****************************************************************************
78 *****************************************************************************/
79 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 sout_CfgParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
89 p_sys = p_mux->p_sys = malloc( sizeof(sout_mux_sys_t) );
90 p_sys->b_send_headers = VLC_TRUE;
92 psz_separator = var_GetString( p_mux, SOUT_CFG_PREFIX "separator" );
93 i_size = strlen( psz_separator ) + 2 + 2 + 2 + strlen( CONTENT_TYPE );
94 psz_separator_block = (char*)malloc( i_size );
95 sprintf( psz_separator_block, "\r\n%s\r\n%s\r\n", psz_separator,
97 p_sys->p_separator = block_New( p_mux, i_size );
98 memcpy( p_sys->p_separator->p_buffer, psz_separator_block , i_size );
100 if( psz_separator_block ) free( psz_separator_block );
102 p_mux->pf_control = Control;
103 p_mux->pf_addstream = AddStream;
104 p_mux->pf_delstream = DelStream;
110 /*****************************************************************************
112 *****************************************************************************/
114 static void Close( vlc_object_t * p_this )
116 sout_mux_t *p_mux = (sout_mux_t*)p_this;
117 sout_mux_sys_t *p_sys = p_mux->p_sys;
119 msg_Dbg( p_mux, "Multipart jpeg muxer closed" );
120 block_Release( p_sys->p_separator );
124 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
131 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
132 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
136 case MUX_GET_ADD_STREAM_WAIT:
137 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
142 ppsz = (char**)va_arg( args, char ** );
143 *ppsz = strdup( "multipart/x-mixed-replace; boundary=This Random String" );
151 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
153 if( p_mux->i_nb_inputs > 1 )
155 msg_Dbg( p_mux, "only 1 input allowed" );
159 msg_Dbg( p_mux, "adding input" );
160 if( p_input->p_fmt->i_codec != VLC_FOURCC('M','J','P','G') &&
161 p_input->p_fmt->i_codec != VLC_FOURCC('m','j','p','g') &&
162 p_input->p_fmt->i_codec != VLC_FOURCC('j','p','e','g') &&
163 p_input->p_fmt->i_codec != VLC_FOURCC('J','P','E','G') &&
164 p_input->p_fmt->i_codec != VLC_FOURCC('J','F','I','F') &&
165 p_input->p_fmt->i_codec != VLC_FOURCC('J','P','G','L') &&
166 p_input->p_fmt->i_codec != VLC_FOURCC('m','j','p','a') )
174 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
176 msg_Dbg( p_mux, "removing input" );
180 static int Mux( sout_mux_t *p_mux )
182 block_fifo_t *p_fifo;
183 sout_mux_sys_t *p_sys = p_mux->p_sys;
185 /* Content-Length:.......\r\n */
186 char psz_content_length[25];
188 if( p_sys->b_send_headers )
191 char *psz_separator = var_CreateGetString( p_mux,
192 SOUT_CFG_PREFIX "separator" );
193 char *psz_separator_block = (char *)malloc( strlen( psz_separator ) +
194 2 + strlen( CONTENT_TYPE ) );
196 sprintf( psz_separator_block, "%s\r\n%s\r\n", psz_separator,
199 p_header = block_New( p_mux, strlen( psz_separator_block ) );
200 memcpy( p_header->p_buffer, psz_separator_block ,
201 strlen( psz_separator_block ) );
202 p_header->i_flags |= BLOCK_FLAG_HEADER;
203 sout_AccessOutWrite( p_mux->p_access, p_header );
204 p_sys->b_send_headers = VLC_FALSE;
205 if( psz_separator_block ) free( psz_separator_block );
208 if( !p_mux->i_nb_inputs ) return VLC_SUCCESS;
210 p_fifo = p_mux->pp_inputs[0]->p_fifo;
211 i_count = p_fifo->i_depth;
214 block_t *p_length = block_New( p_mux, 25 );
215 block_t *p_data = block_FifoGet( p_fifo );
216 sout_AccessOutWrite( p_mux->p_access,
217 block_Duplicate( p_sys->p_separator ) );
218 memset( psz_content_length, 0, 25 );
219 snprintf( psz_content_length, 25, "Content-Length: %i\r\n\r\n",
221 memcpy( p_length->p_buffer, psz_content_length, 25 );
222 sout_AccessOutWrite( p_mux->p_access, p_length );
223 sout_AccessOutWrite( p_mux->p_access, p_data );