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>
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 );
75 mtime_t i_still_length;
78 mtime_t i_frame_length;
80 int i_frame_size_estimate;
81 const uint8_t *p_peek;
86 /*****************************************************************************
87 * Peek: Helper function to peek data with incremental size.
88 * \return false if peek no more data, true otherwise.
89 *****************************************************************************/
90 static bool Peek( demux_t *p_demux, bool b_first )
93 demux_sys_t *p_sys = p_demux->p_sys;
97 p_sys->i_data_peeked = 0;
99 else if( p_sys->i_data_peeked == p_sys->i_frame_size_estimate )
101 p_sys->i_frame_size_estimate += 5120;
103 i_data = stream_Peek( p_demux->s, &p_sys->p_peek,
104 p_sys->i_frame_size_estimate );
105 if( i_data == p_sys->i_data_peeked )
107 msg_Warn( p_demux, "no more data" );
110 p_sys->i_data_peeked = i_data;
113 msg_Warn( p_demux, "cannot peek data" );
119 /*****************************************************************************
120 * GetLine: Internal function used to dup a line of string from the buffer
121 *****************************************************************************/
122 static char* GetLine( demux_t *p_demux, int *p_pos )
124 demux_sys_t *p_sys = p_demux->p_sys;
125 const uint8_t *p_buf;
130 while( *p_pos >= p_sys->i_data_peeked )
132 if( ! Peek( p_demux, false ) )
137 p_buf = p_sys->p_peek + *p_pos;
138 i_size = p_sys->i_data_peeked - *p_pos;
140 while( p_buf[i] != '\n' )
145 if( ! Peek( p_demux, false ) )
149 p_buf = p_sys->p_peek + *p_pos;
150 i_size = p_sys->i_data_peeked - *p_pos;
154 if( i > 0 && '\r' == p_buf[i - 1] )
158 p_line = malloc( i + 1 );
161 strncpy ( p_line, (char*)p_buf, i );
163 // msg_Dbg( p_demux, "i = %d, pos = %d, %s", i, *p_pos, p_line );
167 /*****************************************************************************
168 * CheckMimeHeader: Internal function used to verify and skip mime header
169 * \param p_header_size Return size of MIME header, 0 if no MIME header
170 * detected, minus value if error
171 * \return true if content type is image/jpeg, false otherwise
172 *****************************************************************************/
173 static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
179 demux_sys_t *p_sys = p_demux->p_sys;
181 if( !Peek( p_demux, true ) )
183 msg_Err( p_demux, "cannot peek" );
187 if( p_sys->i_data_peeked < 5)
189 msg_Err( p_demux, "data shortage" );
193 if( strncmp( (char *)p_sys->p_peek, "--", 2 ) != 0
194 && strncmp( (char *)p_sys->p_peek, "\r\n--", 4 ) != 0 )
196 /* Some broken stream may lack the first boundary */
197 if ( p_sys->psz_separator == NULL )
199 msg_Warn( p_demux, "Malformed stream. Trying to work around");
200 char *content_type = stream_ContentType( p_demux->s );
201 if ( content_type == NULL )
203 const char* boundary = strstr( content_type, "boundary=--" );
204 if ( boundary != NULL )
206 p_sys->psz_separator = strdup( boundary + strlen( "boundary=--" ) );
207 msg_Dbg( p_demux, "Video boundary extracted from Content-Type: %s", p_sys->psz_separator );
208 free( content_type );
209 /* Skip to HTTP header parsing as there's no boundary to extract
214 free( content_type );
226 i_pos = *p_sys->p_peek == '-' ? 2 : 4;
227 psz_line = GetLine( p_demux, &i_pos );
228 if( NULL == psz_line )
230 msg_Err( p_demux, "no EOL" );
235 /* Read the separator and remember it if not yet stored */
236 if( p_sys->psz_separator == NULL )
238 p_sys->psz_separator = psz_line;
239 msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
240 p_sys->psz_separator );
244 if( strcmp( psz_line, p_sys->psz_separator ) )
246 msg_Warn( p_demux, "separator %s does not match %s", psz_line,
247 p_sys->psz_separator );
253 psz_line = GetLine( p_demux, &i_pos );
254 while( psz_line && *psz_line )
256 if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
258 p_ch = psz_line + 13;
259 while( *p_ch != '\0' && ( *p_ch == ' ' || *p_ch == '\t' ) ) p_ch++;
260 if( strncasecmp( p_ch, "image/jpeg", 10 ) )
262 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
272 msg_Dbg( p_demux, "discard MIME header: %s", psz_line );
275 psz_line = GetLine( p_demux, &i_pos );
278 if( NULL == psz_line )
280 msg_Err( p_demux, "no EOL" );
287 *p_header_size = i_pos;
291 static int SendBlock( demux_t *p_demux, int i )
293 demux_sys_t *p_sys = p_demux->p_sys;
296 if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
298 msg_Warn( p_demux, "cannot read data" );
302 if( !p_sys->i_frame_length || !p_sys->i_time )
304 p_sys->i_time = p_block->i_dts = p_block->i_pts = mdate();
308 p_block->i_dts = p_block->i_pts = VLC_TS_0 + p_sys->i_time;
309 p_sys->i_time += p_sys->i_frame_length;
313 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
314 es_out_Send( p_demux->out, p_sys->p_es, p_block );
318 p_sys->i_still_end = mdate() + p_sys->i_still_length;
324 /*****************************************************************************
325 * Open: check file and initializes structures
326 *****************************************************************************/
327 static int Open( vlc_object_t * p_this )
329 demux_t *p_demux = (demux_t*)p_this;
332 bool b_matched = false;
335 p_sys = malloc( sizeof( demux_sys_t ) );
336 if( unlikely(p_sys == NULL) )
339 p_demux->pf_control = Control;
340 p_demux->p_sys = p_sys;
345 p_sys->psz_separator = NULL;
346 p_sys->i_frame_size_estimate = 15 * 1024;
348 if( IsMxpeg( p_demux->s ) && !p_demux->b_force )
350 // let avformat handle this case
354 b_matched = CheckMimeHeader( p_demux, &i_size);
357 p_demux->pf_demux = MimeDemux;
358 stream_Read( p_demux->s, NULL, i_size );
360 else if( 0 == i_size )
362 /* 0xffd8 identify a JPEG SOI */
363 if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
365 msg_Dbg( p_demux, "JPEG SOI marker detected" );
366 p_demux->pf_demux = MjpgDemux;
380 f_fps = var_CreateGetFloat( p_demux, "mjpeg-fps" );
381 p_sys->i_frame_length = 0;
383 /* Check for jpeg file extension */
384 p_sys->b_still = false;
385 p_sys->i_still_end = 0;
386 if( demux_IsPathExtension( p_demux, ".jpeg" ) ||
387 demux_IsPathExtension( p_demux, ".jpg" ) )
389 p_sys->b_still = true;
392 p_sys->i_still_length = 1000000.0 / f_fps;
396 /* Defaults to 1fps */
397 p_sys->i_still_length = 1000000;
402 p_sys->i_frame_length = 1000000.0 / f_fps;
405 es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
406 p_sys->fmt.i_codec = VLC_CODEC_MJPG;
408 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
416 /*****************************************************************************
417 * Demux: read packet and send them to decoders
418 *****************************************************************************
419 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
420 *****************************************************************************/
421 static int MjpgDemux( demux_t *p_demux )
423 demux_sys_t *p_sys = p_demux->p_sys;
426 if( p_sys->b_still && p_sys->i_still_end )
428 /* Still frame, wait until the pause delay is gone */
429 mwait( p_sys->i_still_end );
430 p_sys->i_still_end = 0;
434 if( !Peek( p_demux, true ) )
436 msg_Warn( p_demux, "cannot peek data" );
439 if( p_sys->i_data_peeked < 4 )
441 msg_Warn( p_demux, "data shortage" );
446 while( !( 0xFF == p_sys->p_peek[i-1] && 0xD9 == p_sys->p_peek[i] ) )
448 if( 0xFF == p_sys->p_peek[i-1] && 0xD8 == p_sys->p_peek[i] )
451 msg_Dbg( p_demux, "we found another JPEG SOI at %d", i );
454 if( i >= p_sys->i_data_peeked )
456 msg_Dbg( p_demux, "did not find JPEG EOI in %d bytes",
457 p_sys->i_data_peeked );
458 if( !Peek( p_demux, false ) )
460 msg_Warn( p_demux, "no more data is available at the moment" );
467 msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
470 if( p_sys->i_level > 0 )
472 return SendBlock( p_demux, i );
475 static int MimeDemux( demux_t *p_demux )
477 demux_sys_t *p_sys = p_demux->p_sys;
480 bool b_match = CheckMimeHeader( p_demux, &i_size );
484 stream_Read( p_demux->s, NULL, i_size );
486 else if( i_size < 0 )
492 // No MIME header, assume OK
496 if( !Peek( p_demux, true ) )
498 msg_Warn( p_demux, "cannot peek data" );
503 i_size = strlen( p_sys->psz_separator ) + 2;
504 if( p_sys->i_data_peeked < i_size )
506 msg_Warn( p_demux, "data shortage" );
512 while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
516 if( i_size >= p_sys->i_data_peeked )
518 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
519 "data", p_sys->i_data_peeked );
521 if( !Peek( p_demux, false ) )
523 msg_Warn( p_demux, "no more data is available at the "
530 if( !strncmp( p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
531 strlen( p_sys->psz_separator ) ) )
542 msg_Err( p_demux, "discard non-JPEG part" );
543 stream_Read( p_demux->s, NULL, i );
547 return SendBlock( p_demux, i );
550 /*****************************************************************************
551 * Close: frees unused data
552 *****************************************************************************/
553 static void Close ( vlc_object_t * p_this )
555 demux_t *p_demux = (demux_t*)p_this;
556 demux_sys_t *p_sys = p_demux->p_sys;
558 free( p_sys->psz_separator );
562 /*****************************************************************************
564 *****************************************************************************/
565 static int Control( demux_t *p_demux, int i_query, va_list args )
567 return demux_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );