1 /*****************************************************************************
2 * mjpeg.c : demuxes mjpeg webcam http streams
3 *****************************************************************************
4 * Copyright (C) 2004 VLC authors and VideoLAN
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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * 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>
38 #include "mxpeg_helper.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
46 #define FPS_TEXT N_("Frames per Second")
47 #define FPS_LONGTEXT N_("This is the desired frame rate when " \
48 "playing MJPEG from a file. Use 0 (this is the default value) for a " \
49 "live stream (from a camera).")
52 set_shortname( "MJPEG")
53 set_description( N_("M-JPEG camera demuxer") )
54 set_capability( "demux", 5 )
55 set_callbacks( Open, Close )
56 set_category( CAT_INPUT )
57 set_subcategory( SUBCAT_INPUT_DEMUX )
58 add_float( "mjpeg-fps", 0.0, FPS_TEXT, FPS_LONGTEXT, false )
61 /*****************************************************************************
63 *****************************************************************************/
64 static int MimeDemux( demux_t * );
65 static int MjpgDemux( demux_t * );
66 static int Control( demux_t *, int i_query, va_list args );
76 mtime_t i_frame_length;
78 int i_frame_size_estimate;
79 const uint8_t *p_peek;
84 /*****************************************************************************
85 * Peek: Helper function to peek data with incremental size.
86 * \return false if peek no more data, true otherwise.
87 *****************************************************************************/
88 static bool Peek( demux_t *p_demux, bool b_first )
91 demux_sys_t *p_sys = p_demux->p_sys;
95 p_sys->i_data_peeked = 0;
97 else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
99 p_sys->i_frame_size_estimate += 5120;
101 i_data = stream_Peek( p_demux->s, &p_sys->p_peek,
102 p_sys->i_frame_size_estimate );
103 if( i_data == p_sys->i_data_peeked )
105 msg_Warn( p_demux, "no more data" );
108 p_sys->i_data_peeked = i_data;
111 msg_Warn( p_demux, "cannot peek data" );
117 /*****************************************************************************
118 * GetLine: Internal function used to dup a line of string from the buffer
119 *****************************************************************************/
120 static char* GetLine( demux_t *p_demux, int *p_pos )
122 demux_sys_t *p_sys = p_demux->p_sys;
123 const uint8_t *p_buf;
128 while( *p_pos >= p_sys->i_data_peeked )
130 if( ! Peek( p_demux, false ) )
135 p_buf = p_sys->p_peek + *p_pos;
136 i_size = p_sys->i_data_peeked - *p_pos;
138 while( p_buf[i] != '\n' )
143 if( ! Peek( p_demux, false ) )
147 p_buf = p_sys->p_peek + *p_pos;
148 i_size = p_sys->i_data_peeked - *p_pos;
152 if( i > 0 && p_buf[i - 1] == '\r' )
156 p_line = malloc( i + 1 );
157 if( unlikely( p_line == NULL ) )
159 strncpy ( p_line, (char*)p_buf, i );
164 /*****************************************************************************
165 * CheckMimeHeader: Internal function used to verify and skip mime header
166 * \param p_header_size Return size of MIME header, 0 if no MIME header
167 * detected, minus value if error
168 * \return true if content type is image/jpeg, false otherwise
169 *****************************************************************************/
170 static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
176 demux_sys_t *p_sys = p_demux->p_sys;
179 if( !Peek( p_demux, true ) )
181 msg_Err( p_demux, "cannot peek" );
184 if( p_sys->i_data_peeked < 5)
186 msg_Err( p_demux, "data shortage" );
189 if( strncmp( (char *)p_sys->p_peek, "--", 2 ) != 0
190 && strncmp( (char *)p_sys->p_peek, "\r\n--", 4 ) != 0 )
197 i_pos = *p_sys->p_peek == '-' ? 2 : 4;
198 psz_line = GetLine( p_demux, &i_pos );
199 if( NULL == psz_line )
201 msg_Err( p_demux, "no EOL" );
205 /* Read the separator and remember it if not yet stored */
206 if( p_sys->psz_separator == NULL )
208 p_sys->psz_separator = psz_line;
209 msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
210 p_sys->psz_separator );
214 if( strcmp( psz_line, p_sys->psz_separator ) )
216 msg_Warn( p_demux, "separator %s does not match %s", psz_line,
217 p_sys->psz_separator );
223 psz_line = GetLine( p_demux, &i_pos );
224 while( psz_line && *psz_line )
226 if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
228 p_ch = psz_line + 13;
229 while( *p_ch != '\0' && ( *p_ch == ' ' || *p_ch == '\t' ) ) p_ch++;
230 if( strncasecmp( p_ch, "image/jpeg", 10 ) )
232 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
242 msg_Dbg( p_demux, "discard MIME header: %s", psz_line );
245 psz_line = GetLine( p_demux, &i_pos );
248 if( NULL == psz_line )
250 msg_Err( p_demux, "no EOL" );
256 *p_header_size = i_pos;
260 static int SendBlock( demux_t *p_demux, int i )
262 demux_sys_t *p_sys = p_demux->p_sys;
265 if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
267 msg_Warn( p_demux, "cannot read data" );
271 if( p_sys->i_frame_length )
273 p_block->i_pts = p_sys->i_time;
274 p_sys->i_time += p_sys->i_frame_length;
278 p_block->i_pts = mdate();
280 p_block->i_dts = p_block->i_pts;
283 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
284 es_out_Send( p_demux->out, p_sys->p_es, p_block );
287 p_sys->i_still_end = mdate() + p_sys->i_frame_length;
292 /*****************************************************************************
293 * Open: check file and initializes structures
294 *****************************************************************************/
295 static int Open( vlc_object_t * p_this )
297 demux_t *p_demux = (demux_t*)p_this;
299 bool b_matched = false;
301 if( IsMxpeg( p_demux->s ) && !p_demux->b_force )
302 // let avformat handle this case
305 demux_sys_t *p_sys = malloc( sizeof( demux_sys_t ) );
306 if( unlikely(p_sys == NULL) )
309 p_demux->pf_control = Control;
310 p_demux->p_sys = p_sys;
312 p_sys->i_time = VLC_TS_0;
315 p_sys->psz_separator = NULL;
316 p_sys->i_frame_size_estimate = 15 * 1024;
318 char *content_type = stream_ContentType( p_demux->s );
321 //FIXME: this is not fully match to RFC
322 char* boundary = strstr( content_type, "boundary=" );
325 p_sys->psz_separator = strdup( boundary + strlen("boundary=") );
326 if( !p_sys->psz_separator )
328 free( content_type );
332 free( content_type );
335 b_matched = CheckMimeHeader( p_demux, &i_size);
338 p_demux->pf_demux = MimeDemux;
339 stream_Read( p_demux->s, NULL, i_size );
341 else if( i_size == 0 )
343 /* 0xffd8 identify a JPEG SOI */
344 if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
346 msg_Dbg( p_demux, "JPEG SOI marker detected" );
347 p_demux->pf_demux = MjpgDemux;
361 float f_fps = var_InheritFloat( p_demux, "mjpeg-fps" );
363 p_sys->i_still_end = 0;
364 if( demux_IsPathExtension( p_demux, ".jpeg" ) ||
365 demux_IsPathExtension( p_demux, ".jpg" ) )
367 /* Plain JPEG file = single still picture */
368 p_sys->b_still = true;
370 /* Defaults to 1fps */
374 p_sys->b_still = false;
375 p_sys->i_frame_length = f_fps ? (CLOCK_FREQ / f_fps) : 0;
377 es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
378 p_sys->fmt.i_codec = VLC_CODEC_MJPG;
380 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
384 free( p_sys->psz_separator );
389 /*****************************************************************************
390 * Demux: read packet and send them to decoders
391 *****************************************************************************
392 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
393 *****************************************************************************/
394 static int MjpgDemux( demux_t *p_demux )
396 demux_sys_t *p_sys = p_demux->p_sys;
399 if( p_sys->b_still && p_sys->i_still_end )
401 /* Still frame, wait until the pause delay is gone */
402 mwait( p_sys->i_still_end );
403 p_sys->i_still_end = 0;
407 if( !Peek( p_demux, true ) )
409 msg_Warn( p_demux, "cannot peek data" );
412 if( p_sys->i_data_peeked < 4 )
414 msg_Warn( p_demux, "data shortage" );
419 while( !( p_sys->p_peek[i-1] == 0xFF && p_sys->p_peek[i] == 0xD9 ) )
421 if( p_sys->p_peek[i-1] == 0xFF && p_sys->p_peek[i] == 0xD9 )
424 msg_Dbg( p_demux, "we found another JPEG SOI at %d", i );
427 if( i >= p_sys->i_data_peeked )
429 msg_Dbg( p_demux, "did not find JPEG EOI in %d bytes",
430 p_sys->i_data_peeked );
431 if( !Peek( p_demux, false ) )
433 msg_Warn( p_demux, "no more data is available at the moment" );
440 msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
443 if( p_sys->i_level > 0 )
445 return SendBlock( p_demux, i );
448 static int MimeDemux( demux_t *p_demux )
450 demux_sys_t *p_sys = p_demux->p_sys;
453 bool b_match = CheckMimeHeader( p_demux, &i_size );
457 stream_Read( p_demux->s, NULL, i_size );
459 else if( i_size < 0 )
465 // No MIME header, assume OK
469 if( !Peek( p_demux, true ) )
471 msg_Warn( p_demux, "cannot peek data" );
476 i_size = strlen( p_sys->psz_separator ) + 2;
477 if( p_sys->i_data_peeked < i_size )
479 msg_Warn( p_demux, "data shortage" );
485 while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
489 if( i_size >= p_sys->i_data_peeked )
491 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
492 "data", p_sys->i_data_peeked );
494 if( !Peek( p_demux, false ) )
496 msg_Warn( p_demux, "no more data is available at the "
503 if( !strncmp( p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
504 strlen( p_sys->psz_separator ) ) )
515 msg_Err( p_demux, "discard non-JPEG part" );
516 stream_Read( p_demux->s, NULL, i );
520 return SendBlock( p_demux, i );
523 /*****************************************************************************
524 * Close: frees unused data
525 *****************************************************************************/
526 static void Close ( vlc_object_t * p_this )
528 demux_t *p_demux = (demux_t*)p_this;
529 demux_sys_t *p_sys = p_demux->p_sys;
531 free( p_sys->psz_separator );
535 /*****************************************************************************
537 *****************************************************************************/
538 static int Control( demux_t *p_demux, int i_query, va_list args )
540 return demux_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );