1 /*****************************************************************************
2 * mjpeg.c : demuxes mjpeg webcam http streams
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
5 * $Id: mjpeg.c 7196 2004-03-29 21:29:31Z fenrir $
7 * Authors: Henry Jen (slowhog) <henryjen@ztune.net>
8 * Derk-Jan Hartman (thedj)
9 * Sigmund Augdal (Dnumgis)
10 * Laurent Aimar <fenrir@via.ecp.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <stdlib.h> /* malloc(), free() */
33 #include <vlc/input.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
43 #define FPS_TEXT N_("Frames per Second")
44 #define FPS_LONGTEXT N_("Allows you to set the desired frame rate when " \
45 "playing from files, use 0 for live.")
48 set_description( _("JPEG camera demuxer") );
49 set_capability( "demux2", 5 );
50 set_callbacks( Open, Close );
51 add_float( "mjpeg-fps", 0.0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_FALSE );
54 /*****************************************************************************
56 *****************************************************************************/
57 static int MimeDemux( demux_t * );
58 static int MjpgDemux( demux_t * );
59 static int Control( demux_t *, int i_query, va_list args );
67 mtime_t i_frame_length;
69 int i_frame_size_estimate;
74 /*****************************************************************************
75 * Peek: Helper function to peek data with incremental size.
76 * \return VLC_FALSE if peek no more data, VLC_TRUE otherwise.
77 *****************************************************************************/
78 static vlc_bool_t Peek( demux_t *p_demux, vlc_bool_t b_first )
81 demux_sys_t *p_sys = p_demux->p_sys;
85 p_sys->i_data_peeked = 0;
87 else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
89 p_sys->i_frame_size_estimate += 5120;
91 i_data = stream_Peek( p_demux->s, &p_sys->p_peek,
92 p_sys->i_frame_size_estimate );
93 if( i_data == p_sys->i_data_peeked )
95 msg_Warn( p_demux, "no more data" );
98 p_sys->i_data_peeked = i_data;
101 msg_Warn( p_demux, "cannot peek data" );
107 /*****************************************************************************
108 * GetLine: Internal function used to dup a line of string from the buffer
109 *****************************************************************************/
110 static char* GetLine( demux_t *p_demux, int *p_pos )
112 demux_sys_t *p_sys = p_demux->p_sys;
118 while( *p_pos > p_sys->i_data_peeked )
120 if( ! Peek( p_demux, VLC_FALSE ) )
125 p_buf = p_sys->p_peek + *p_pos;
126 i_size = p_sys->i_data_peeked - *p_pos;
128 while( p_buf[i] != '\n' )
133 if( ! Peek( p_demux, VLC_FALSE ) )
138 p_buf = p_sys->p_peek + *p_pos;
139 i_size = p_sys->i_data_peeked - *p_pos;
142 if( i > 0 && '\r' == p_buf[i - 1] )
146 p_line = malloc( i + 1 );
149 msg_Err( p_demux, "out of memory" );
152 strncpy ( p_line, p_buf, i );
154 // msg_Dbg( p_demux, "i = %d, pos = %d, %s", i, *p_pos, p_line );
158 /*****************************************************************************
159 * CheckMimeHeader: Internal function used to verify and skip mime header
160 * \param p_header_size Return size of MIME header, 0 if no MIME header
161 * detected, minus value if error
162 * \return VLC_TRUE if content type is image/jpeg, VLC_FALSE otherwise
163 *****************************************************************************/
164 static vlc_bool_t CheckMimeHeader( demux_t *p_demux, int *p_header_size )
166 vlc_bool_t b_jpeg = VLC_FALSE;
170 demux_sys_t *p_sys = p_demux->p_sys;
172 if( !Peek( p_demux, VLC_TRUE ) )
174 msg_Err( p_demux, "cannot peek" );
178 if( p_sys->i_data_peeked < 3)
180 msg_Err( p_demux, "data shortage" );
184 if( strncmp( p_sys->p_peek, "--", 2 ) )
190 psz_line = GetLine( p_demux, &i_pos );
191 if( NULL == psz_line )
193 msg_Err( p_demux, "no EOL" );
197 if( NULL == p_sys->psz_separator )
199 p_sys->psz_separator = psz_line;
200 msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
201 p_sys->psz_separator );
205 if( strcmp( psz_line, p_sys->psz_separator ) )
207 msg_Warn( p_demux, "separator %s does not match %s", psz_line,
208 p_sys->psz_separator );
212 psz_line = GetLine( p_demux, &i_pos );
213 while( psz_line && *psz_line )
215 if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
217 p_ch = psz_line + 13;
218 while( *p_ch != '\0' && ( *p_ch == ' ' || *p_ch == '\t' ) ) p_ch++;
219 if( strncasecmp( p_ch, "image/jpeg", 10 ) )
221 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
231 msg_Dbg( p_demux, "Discard MIME header: %s", psz_line );
234 psz_line = GetLine( p_demux, &i_pos );
237 if( NULL == psz_line )
239 msg_Err( p_demux, "no EOL" );
246 *p_header_size = i_pos;
250 static int SendBlock( demux_t *p_demux, int i )
252 demux_sys_t *p_sys = p_demux->p_sys;
255 if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
257 msg_Warn( p_demux, "cannot read data" );
261 if( !p_sys->i_frame_length || !p_sys->i_time )
263 p_sys->i_time = p_block->i_dts = p_block->i_pts = mdate();
267 p_block->i_dts = p_block->i_pts = p_sys->i_time;
268 p_sys->i_time += p_sys->i_frame_length;
272 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
273 es_out_Send( p_demux->out, p_sys->p_es, p_block );
277 /*****************************************************************************
278 * Open: check file and initializes structures
279 *****************************************************************************/
280 static int Open( vlc_object_t * p_this )
282 demux_t *p_demux = (demux_t*)p_this;
285 int b_matched = VLC_FALSE;
288 p_demux->pf_control = Control;
289 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
293 var_Create( p_demux, "mjpeg-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
294 var_Get( p_demux, "mjpeg-fps", &val );
295 p_sys->i_frame_length = 0;
298 p_sys->i_frame_length = 1000000.0 / val.f_float;
301 p_sys->psz_separator = NULL;
302 p_sys->i_frame_size_estimate = 15 * 1024;
304 b_matched = CheckMimeHeader( p_demux, &i_size);
307 p_demux->pf_demux = MimeDemux;
308 stream_Read( p_demux->s, NULL, i_size );
310 else if( 0 == i_size )
312 /* 0xffd8 identify a JPEG SOI */
313 if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
315 msg_Dbg( p_demux, "JPEG SOI marker detected" );
316 p_demux->pf_demux = MjpgDemux;
328 es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
329 p_sys->fmt.i_codec = VLC_FOURCC('m','j','p','g');
331 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
335 msg_Warn( p_demux, "JPEG camera module discarded" );
340 /*****************************************************************************
341 * Demux: read packet and send them to decoders
342 *****************************************************************************
343 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
344 *****************************************************************************/
345 static int MjpgDemux( demux_t *p_demux )
347 demux_sys_t *p_sys = p_demux->p_sys;
350 if( !Peek( p_demux, VLC_TRUE ) )
352 msg_Warn( p_demux, "cannot peek data" );
355 if( p_sys->i_data_peeked < 4 )
357 msg_Warn( p_demux, "data shortage" );
361 while( !( 0xFF == p_sys->p_peek[i-1] && 0xD9 == p_sys->p_peek[i] ) )
364 if( i >= p_sys->i_data_peeked )
366 msg_Dbg( p_demux, "Did not find JPEG EOI in %d bytes",
367 p_sys->i_data_peeked );
368 if( !Peek( p_demux, VLC_FALSE ) )
370 msg_Warn( p_demux, "No more data is available at the moment" );
376 msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
377 return SendBlock( p_demux, i );
380 static int MimeDemux( demux_t *p_demux )
382 demux_sys_t *p_sys = p_demux->p_sys;
387 b_match = CheckMimeHeader( p_demux, &i_size );
390 stream_Read( p_demux->s, NULL, i_size );
392 else if( i_size < 0 )
398 // No MIME header, assume OK
402 if( !Peek( p_demux, VLC_TRUE ) )
404 msg_Warn( p_demux, "cannot peek data" );
408 i_size = strlen( p_sys->psz_separator ) + 2;
409 if( p_sys->i_data_peeked < i_size )
411 msg_Warn( p_demux, "data shortage" );
417 while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
421 if( i_size >= p_sys->i_data_peeked )
423 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
424 "data", p_sys->i_data_peeked );
426 if( !Peek( p_demux, VLC_FALSE ) )
428 msg_Warn( p_demux, "No more data is available at the "
434 if( !strncmp( p_sys->psz_separator, p_sys->p_peek + i + 2,
435 strlen( p_sys->psz_separator ) ) )
448 msg_Err( p_demux, "Discard non-JPEG part" );
449 stream_Read( p_demux->s, NULL, i );
453 return SendBlock( p_demux, i );
456 /*****************************************************************************
457 * Close: frees unused data
458 *****************************************************************************/
459 static void Close ( vlc_object_t * p_this )
461 demux_t *p_demux = (demux_t*)p_this;
462 demux_sys_t *p_sys = p_demux->p_sys;
464 if( p_sys->psz_separator )
466 free( p_sys->psz_separator );
471 /*****************************************************************************
473 *****************************************************************************/
474 static int Control( demux_t *p_demux, int i_query, va_list args )
476 return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );