1 /*****************************************************************************
2 * mjpeg.c : demuxes mjpeg webcam http streams
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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_("This is the desired frame rate when " \
45 "playing MJPEG from a file. Use 0 (this is the default value) for a " \
46 "live stream (from a camera).")
49 set_shortname( "MJPEG");
50 set_description( _("M-JPEG camera demuxer") );
51 set_capability( "demux2", 5 );
52 set_callbacks( Open, Close );
53 set_category( CAT_INPUT );
54 set_subcategory( SUBCAT_INPUT_DEMUX );
55 add_float( "mjpeg-fps", 0.0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_FALSE );
58 /*****************************************************************************
60 *****************************************************************************/
61 static int MimeDemux( demux_t * );
62 static int MjpgDemux( demux_t * );
63 static int Control( demux_t *, int i_query, va_list args );
72 mtime_t i_still_length;
75 mtime_t i_frame_length;
77 int i_frame_size_estimate;
82 /*****************************************************************************
83 * Peek: Helper function to peek data with incremental size.
84 * \return VLC_FALSE if peek no more data, VLC_TRUE otherwise.
85 *****************************************************************************/
86 static vlc_bool_t Peek( demux_t *p_demux, vlc_bool_t b_first )
89 demux_sys_t *p_sys = p_demux->p_sys;
93 p_sys->i_data_peeked = 0;
95 else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
97 p_sys->i_frame_size_estimate += 5120;
99 i_data = stream_Peek( p_demux->s, &p_sys->p_peek,
100 p_sys->i_frame_size_estimate );
101 if( i_data == p_sys->i_data_peeked )
103 msg_Warn( p_demux, "no more data" );
106 p_sys->i_data_peeked = i_data;
109 msg_Warn( p_demux, "cannot peek data" );
115 /*****************************************************************************
116 * GetLine: Internal function used to dup a line of string from the buffer
117 *****************************************************************************/
118 static char* GetLine( demux_t *p_demux, int *p_pos )
120 demux_sys_t *p_sys = p_demux->p_sys;
126 while( *p_pos > p_sys->i_data_peeked )
128 if( ! Peek( p_demux, VLC_FALSE ) )
133 p_buf = p_sys->p_peek + *p_pos;
134 i_size = p_sys->i_data_peeked - *p_pos;
136 while( p_buf[i] != '\n' )
141 if( ! Peek( p_demux, VLC_FALSE ) )
146 p_buf = p_sys->p_peek + *p_pos;
147 i_size = p_sys->i_data_peeked - *p_pos;
150 if( i > 0 && '\r' == p_buf[i - 1] )
154 p_line = malloc( i + 1 );
157 msg_Err( p_demux, "out of memory" );
160 strncpy ( p_line, p_buf, i );
162 // msg_Dbg( p_demux, "i = %d, pos = %d, %s", i, *p_pos, p_line );
166 /*****************************************************************************
167 * CheckMimeHeader: Internal function used to verify and skip mime header
168 * \param p_header_size Return size of MIME header, 0 if no MIME header
169 * detected, minus value if error
170 * \return VLC_TRUE if content type is image/jpeg, VLC_FALSE otherwise
171 *****************************************************************************/
172 static vlc_bool_t CheckMimeHeader( demux_t *p_demux, int *p_header_size )
174 vlc_bool_t b_jpeg = VLC_FALSE;
178 demux_sys_t *p_sys = p_demux->p_sys;
180 if( !Peek( p_demux, VLC_TRUE ) )
182 msg_Err( p_demux, "cannot peek" );
186 if( p_sys->i_data_peeked < 3)
188 msg_Err( p_demux, "data shortage" );
192 if( strncmp( (char *)p_sys->p_peek, "--", 2 ) )
198 psz_line = GetLine( p_demux, &i_pos );
199 if( NULL == psz_line )
201 msg_Err( p_demux, "no EOL" );
205 if( NULL == p_sys->psz_separator )
207 p_sys->psz_separator = psz_line;
208 msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
209 p_sys->psz_separator );
213 if( strcmp( psz_line, p_sys->psz_separator ) )
215 msg_Warn( p_demux, "separator %s does not match %s", psz_line,
216 p_sys->psz_separator );
220 psz_line = GetLine( p_demux, &i_pos );
221 while( psz_line && *psz_line )
223 if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
225 p_ch = psz_line + 13;
226 while( *p_ch != '\0' && ( *p_ch == ' ' || *p_ch == '\t' ) ) p_ch++;
227 if( strncasecmp( p_ch, "image/jpeg", 10 ) )
229 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
239 msg_Dbg( p_demux, "discard MIME header: %s", psz_line );
242 psz_line = GetLine( p_demux, &i_pos );
245 if( NULL == psz_line )
247 msg_Err( p_demux, "no EOL" );
254 *p_header_size = i_pos;
258 static int SendBlock( demux_t *p_demux, int i )
260 demux_sys_t *p_sys = p_demux->p_sys;
263 if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
265 msg_Warn( p_demux, "cannot read data" );
269 if( !p_sys->i_frame_length || !p_sys->i_time )
271 p_sys->i_time = p_block->i_dts = p_block->i_pts = mdate();
275 p_block->i_dts = p_block->i_pts = p_sys->i_time;
276 p_sys->i_time += p_sys->i_frame_length;
280 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
281 es_out_Send( p_demux->out, p_sys->p_es, p_block );
285 p_sys->i_still_end = mdate() + p_sys->i_still_length;
291 /*****************************************************************************
292 * Open: check file and initializes structures
293 *****************************************************************************/
294 static int Open( vlc_object_t * p_this )
296 demux_t *p_demux = (demux_t*)p_this;
299 int b_matched = VLC_FALSE;
303 p_demux->pf_control = Control;
304 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
308 p_sys->psz_separator = NULL;
309 p_sys->i_frame_size_estimate = 15 * 1024;
311 b_matched = CheckMimeHeader( p_demux, &i_size);
314 p_demux->pf_demux = MimeDemux;
315 stream_Read( p_demux->s, NULL, i_size );
317 else if( 0 == i_size )
319 /* 0xffd8 identify a JPEG SOI */
320 if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
322 msg_Dbg( p_demux, "JPEG SOI marker detected" );
323 p_demux->pf_demux = MjpgDemux;
336 var_Create( p_demux, "mjpeg-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
337 var_Get( p_demux, "mjpeg-fps", &val );
338 p_sys->i_frame_length = 0;
340 /* Check for jpeg file extension */
341 p_sys->b_still = VLC_FALSE;
342 p_sys->i_still_end = 0;
343 psz_ext = strrchr( p_demux->psz_path, '.' );
344 if( psz_ext && ( !strcasecmp( psz_ext, ".jpeg" ) ||
345 !strcasecmp( psz_ext, ".jpg" ) ) )
347 p_sys->b_still = VLC_TRUE;
350 p_sys->i_still_length =1000000.0 / val.f_float;
354 /* Defaults to 1fps */
355 p_sys->i_still_length = 1000000;
358 else if ( val.f_float )
360 p_sys->i_frame_length = 1000000.0 / val.f_float;
363 es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
364 p_sys->fmt.i_codec = VLC_FOURCC('m','j','p','g');
366 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
374 /*****************************************************************************
375 * Demux: read packet and send them to decoders
376 *****************************************************************************
377 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
378 *****************************************************************************/
379 static int MjpgDemux( demux_t *p_demux )
381 demux_sys_t *p_sys = p_demux->p_sys;
384 if( p_sys->b_still && p_sys->i_still_end && p_sys->i_still_end < mdate() )
386 /* Still frame, wait until the pause delay is gone */
387 p_sys->i_still_end = 0;
389 else if( p_sys->b_still && p_sys->i_still_end )
395 if( !Peek( p_demux, VLC_TRUE ) )
397 msg_Warn( p_demux, "cannot peek data" );
400 if( p_sys->i_data_peeked < 4 )
402 msg_Warn( p_demux, "data shortage" );
406 while( !( 0xFF == p_sys->p_peek[i-1] && 0xD9 == p_sys->p_peek[i] ) )
409 if( i >= p_sys->i_data_peeked )
411 msg_Dbg( p_demux, "did not find JPEG EOI in %d bytes",
412 p_sys->i_data_peeked );
413 if( !Peek( p_demux, VLC_FALSE ) )
415 msg_Warn( p_demux, "no more data is available at the moment" );
422 msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
423 return SendBlock( p_demux, i );
426 static int MimeDemux( demux_t *p_demux )
428 demux_sys_t *p_sys = p_demux->p_sys;
433 b_match = CheckMimeHeader( p_demux, &i_size );
436 stream_Read( p_demux->s, NULL, i_size );
438 else if( i_size < 0 )
444 // No MIME header, assume OK
448 if( !Peek( p_demux, VLC_TRUE ) )
450 msg_Warn( p_demux, "cannot peek data" );
454 i_size = strlen( p_sys->psz_separator ) + 2;
455 if( p_sys->i_data_peeked < i_size )
457 msg_Warn( p_demux, "data shortage" );
463 while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
467 if( i_size >= p_sys->i_data_peeked )
469 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
470 "data", p_sys->i_data_peeked );
472 if( !Peek( p_demux, VLC_FALSE ) )
474 msg_Warn( p_demux, "no more data is available at the "
480 if( !strncmp( p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
481 strlen( p_sys->psz_separator ) ) )
494 msg_Err( p_demux, "discard non-JPEG part" );
495 stream_Read( p_demux->s, NULL, i );
499 return SendBlock( p_demux, i );
502 /*****************************************************************************
503 * Close: frees unused data
504 *****************************************************************************/
505 static void Close ( vlc_object_t * p_this )
507 demux_t *p_demux = (demux_t*)p_this;
508 demux_sys_t *p_sys = p_demux->p_sys;
510 if( p_sys->psz_separator )
512 free( p_sys->psz_separator );
517 /*****************************************************************************
519 *****************************************************************************/
520 static int Control( demux_t *p_demux, int i_query, va_list args )
522 return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );