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 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_demux.h>
39 #include <vlc_codecs.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
47 #define FPS_TEXT N_("Frames per Second")
48 #define FPS_LONGTEXT N_("This is the desired frame rate when " \
49 "playing MJPEG from a file. Use 0 (this is the default value) for a " \
50 "live stream (from a camera).")
53 set_shortname( "MJPEG")
54 set_description( N_("M-JPEG camera demuxer") )
55 set_capability( "demux", 5 )
56 set_callbacks( Open, Close )
57 set_category( CAT_INPUT )
58 set_subcategory( SUBCAT_INPUT_DEMUX )
59 add_float( "mjpeg-fps", 0.0, NULL, FPS_TEXT, FPS_LONGTEXT, false )
62 /*****************************************************************************
64 *****************************************************************************/
65 static int MimeDemux( demux_t * );
66 static int MjpgDemux( demux_t * );
67 static int Control( demux_t *, int i_query, va_list args );
76 mtime_t i_still_length;
79 mtime_t i_frame_length;
81 int i_frame_size_estimate;
82 const uint8_t *p_peek;
87 /*****************************************************************************
88 * Peek: Helper function to peek data with incremental size.
89 * \return false if peek no more data, true otherwise.
90 *****************************************************************************/
91 static bool Peek( demux_t *p_demux, bool b_first )
94 demux_sys_t *p_sys = p_demux->p_sys;
98 p_sys->i_data_peeked = 0;
100 else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
102 p_sys->i_frame_size_estimate += 5120;
104 i_data = stream_Peek( p_demux->s, &p_sys->p_peek,
105 p_sys->i_frame_size_estimate );
106 if( i_data == p_sys->i_data_peeked )
108 msg_Warn( p_demux, "no more data" );
111 p_sys->i_data_peeked = i_data;
114 msg_Warn( p_demux, "cannot peek data" );
120 /*****************************************************************************
121 * GetLine: Internal function used to dup a line of string from the buffer
122 *****************************************************************************/
123 static char* GetLine( demux_t *p_demux, int *p_pos )
125 demux_sys_t *p_sys = p_demux->p_sys;
126 const uint8_t *p_buf;
131 while( *p_pos > p_sys->i_data_peeked )
133 if( ! Peek( p_demux, false ) )
138 p_buf = p_sys->p_peek + *p_pos;
139 i_size = p_sys->i_data_peeked - *p_pos;
141 while( p_buf[i] != '\n' )
146 if( ! Peek( p_demux, false ) )
150 p_buf = p_sys->p_peek + *p_pos;
151 i_size = p_sys->i_data_peeked - *p_pos;
155 if( i > 0 && '\r' == p_buf[i - 1] )
159 p_line = malloc( i + 1 );
162 strncpy ( p_line, (char*)p_buf, i );
164 // msg_Dbg( p_demux, "i = %d, pos = %d, %s", i, *p_pos, p_line );
168 /*****************************************************************************
169 * CheckMimeHeader: Internal function used to verify and skip mime header
170 * \param p_header_size Return size of MIME header, 0 if no MIME header
171 * detected, minus value if error
172 * \return true if content type is image/jpeg, false otherwise
173 *****************************************************************************/
174 static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
180 demux_sys_t *p_sys = p_demux->p_sys;
182 if( !Peek( p_demux, true ) )
184 msg_Err( p_demux, "cannot peek" );
188 if( p_sys->i_data_peeked < 5)
190 msg_Err( p_demux, "data shortage" );
194 if( strncmp( (char *)p_sys->p_peek, "--", 2 ) != 0
195 && strncmp( (char *)p_sys->p_peek, "\r\n--", 4 ) != 0 )
200 i_pos = *p_sys->p_peek == '-' ? 2 : 4;
201 psz_line = GetLine( p_demux, &i_pos );
202 if( NULL == psz_line )
204 msg_Err( p_demux, "no EOL" );
209 /* Read the separator and remember it if not yet stored */
210 if( p_sys->psz_separator == NULL )
212 p_sys->psz_separator = psz_line;
213 msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
214 p_sys->psz_separator );
218 if( strcmp( psz_line, p_sys->psz_separator ) )
220 msg_Warn( p_demux, "separator %s does not match %s", psz_line,
221 p_sys->psz_separator );
226 psz_line = GetLine( p_demux, &i_pos );
227 while( psz_line && *psz_line )
229 if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
231 p_ch = psz_line + 13;
232 while( *p_ch != '\0' && ( *p_ch == ' ' || *p_ch == '\t' ) ) p_ch++;
233 if( strncasecmp( p_ch, "image/jpeg", 10 ) )
235 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
245 msg_Dbg( p_demux, "discard MIME header: %s", psz_line );
248 psz_line = GetLine( p_demux, &i_pos );
251 if( NULL == psz_line )
253 msg_Err( p_demux, "no EOL" );
260 *p_header_size = i_pos;
264 static int SendBlock( demux_t *p_demux, int i )
266 demux_sys_t *p_sys = p_demux->p_sys;
269 if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
271 msg_Warn( p_demux, "cannot read data" );
275 if( !p_sys->i_frame_length || !p_sys->i_time )
277 p_sys->i_time = p_block->i_dts = p_block->i_pts = mdate();
281 p_block->i_dts = p_block->i_pts = p_sys->i_time;
282 p_sys->i_time += p_sys->i_frame_length;
286 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
287 es_out_Send( p_demux->out, p_sys->p_es, p_block );
291 p_sys->i_still_end = mdate() + p_sys->i_still_length;
297 /*****************************************************************************
298 * Open: check file and initializes structures
299 *****************************************************************************/
300 static int Open( vlc_object_t * p_this )
302 demux_t *p_demux = (demux_t*)p_this;
305 bool b_matched = false;
308 p_demux->pf_control = Control;
309 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
314 p_sys->psz_separator = NULL;
315 p_sys->i_frame_size_estimate = 15 * 1024;
317 b_matched = CheckMimeHeader( p_demux, &i_size);
320 p_demux->pf_demux = MimeDemux;
321 stream_Read( p_demux->s, NULL, i_size );
323 else if( 0 == i_size )
325 /* 0xffd8 identify a JPEG SOI */
326 if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
328 msg_Dbg( p_demux, "JPEG SOI marker detected" );
329 p_demux->pf_demux = MjpgDemux;
343 var_Create( p_demux, "mjpeg-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
344 var_Get( p_demux, "mjpeg-fps", &val );
345 p_sys->i_frame_length = 0;
347 /* Check for jpeg file extension */
348 p_sys->b_still = false;
349 p_sys->i_still_end = 0;
350 if( demux_IsPathExtension( p_demux, ".jpeg" ) ||
351 demux_IsPathExtension( p_demux, ".jpg" ) )
353 p_sys->b_still = true;
356 p_sys->i_still_length = 1000000.0 / val.f_float;
360 /* Defaults to 1fps */
361 p_sys->i_still_length = 1000000;
364 else if ( val.f_float )
366 p_sys->i_frame_length = 1000000.0 / val.f_float;
369 es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
370 p_sys->fmt.i_codec = VLC_CODEC_MJPG;
372 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
380 /*****************************************************************************
381 * Demux: read packet and send them to decoders
382 *****************************************************************************
383 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
384 *****************************************************************************/
385 static int MjpgDemux( demux_t *p_demux )
387 demux_sys_t *p_sys = p_demux->p_sys;
390 if( p_sys->b_still && p_sys->i_still_end )
392 /* Still frame, wait until the pause delay is gone */
393 mwait( p_sys->i_still_end );
394 p_sys->i_still_end = 0;
398 if( !Peek( p_demux, true ) )
400 msg_Warn( p_demux, "cannot peek data" );
403 if( p_sys->i_data_peeked < 4 )
405 msg_Warn( p_demux, "data shortage" );
410 while( !( 0xFF == p_sys->p_peek[i-1] && 0xD9 == p_sys->p_peek[i] ) )
412 if( 0xFF == p_sys->p_peek[i-1] && 0xD8 == p_sys->p_peek[i] )
415 msg_Dbg( p_demux, "we found another JPEG SOI at %d", i );
418 if( i >= p_sys->i_data_peeked )
420 msg_Dbg( p_demux, "did not find JPEG EOI in %d bytes",
421 p_sys->i_data_peeked );
422 if( !Peek( p_demux, false ) )
424 msg_Warn( p_demux, "no more data is available at the moment" );
431 msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
434 if( p_sys->i_level > 0 )
436 return SendBlock( p_demux, i );
439 static int MimeDemux( demux_t *p_demux )
441 demux_sys_t *p_sys = p_demux->p_sys;
444 bool b_match = CheckMimeHeader( p_demux, &i_size );
448 stream_Read( p_demux->s, NULL, i_size );
450 else if( i_size < 0 )
456 // No MIME header, assume OK
460 if( !Peek( p_demux, true ) )
462 msg_Warn( p_demux, "cannot peek data" );
467 i_size = strlen( p_sys->psz_separator ) + 2;
468 if( p_sys->i_data_peeked < i_size )
470 msg_Warn( p_demux, "data shortage" );
476 while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
480 if( i_size >= p_sys->i_data_peeked )
482 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
483 "data", p_sys->i_data_peeked );
485 if( !Peek( p_demux, false ) )
487 msg_Warn( p_demux, "no more data is available at the "
494 if( !strncmp( p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
495 strlen( p_sys->psz_separator ) ) )
506 msg_Err( p_demux, "discard non-JPEG part" );
507 stream_Read( p_demux->s, NULL, i );
511 return SendBlock( p_demux, i );
514 /*****************************************************************************
515 * Close: frees unused data
516 *****************************************************************************/
517 static void Close ( vlc_object_t * p_this )
519 demux_t *p_demux = (demux_t*)p_this;
520 demux_sys_t *p_sys = p_demux->p_sys;
522 free( p_sys->psz_separator );
526 /*****************************************************************************
528 *****************************************************************************/
529 static int Control( demux_t *p_demux, int i_query, va_list args )
531 return demux_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );