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 msg_Dbg( p_demux, "%s", psz_line );
216 if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
218 p_ch = psz_line + 13;
219 while( *p_ch != '\0' && ( *p_ch == ' ' || *p_ch == '\t' ) ) p_ch++;
220 if( strncasecmp( p_ch, "image/jpeg", 10 ) )
222 msg_Warn( p_demux, "%s, image/jpeg is expected", psz_line );
232 msg_Dbg( p_demux, "Discard MIME header: %s", psz_line );
235 psz_line = GetLine( p_demux, &i_pos );
237 if( NULL == psz_line )
239 msg_Err( p_demux, "no EOL" );
243 *p_header_size = i_pos;
247 static int SendBlock( demux_t *p_demux, int i )
249 demux_sys_t *p_sys = p_demux->p_sys;
252 if( ( p_block = stream_Block( p_demux->s, i ) ) == NULL )
254 msg_Warn( p_demux, "cannot read data" );
258 if( 0 == p_sys->i_frame_length )
260 p_block->i_dts = p_block->i_pts = mdate() + 1;
264 p_block->i_dts = p_block->i_pts = p_sys->i_time;
265 p_sys->i_time += p_sys->i_frame_length;
269 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
270 es_out_Send( p_demux->out, p_sys->p_es, p_block );
274 /*****************************************************************************
275 * Open: check file and initializes structures
276 *****************************************************************************/
277 static int Open( vlc_object_t * p_this )
279 demux_t *p_demux = (demux_t*)p_this;
282 int b_matched = VLC_FALSE;
285 p_demux->pf_control = Control;
286 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
290 var_Create( p_demux, "mjpeg-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
291 var_Get( p_demux, "mjpeg-fps", &val );
292 p_sys->i_frame_length = 0;
295 p_sys->i_frame_length = 1000000.0 / val.f_float;
298 p_sys->psz_separator = NULL;
299 p_sys->i_frame_size_estimate = 15 * 1024;
301 b_matched = CheckMimeHeader( p_demux, &i_size);
304 p_demux->pf_demux = MimeDemux;
305 stream_Read( p_demux->s, NULL, i_size );
307 else if( 0 == i_size )
309 /* 0xffd8 identify a JPEG SOI */
310 if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
312 msg_Dbg( p_demux, "JPEG SOI marker detected" );
313 p_demux->pf_demux = MjpgDemux;
325 es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
326 p_sys->fmt.i_codec = VLC_FOURCC('m','j','p','g');
328 p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
332 msg_Warn( p_demux, "JPEG camera module discarded" );
337 /*****************************************************************************
338 * Demux: read packet and send them to decoders
339 *****************************************************************************
340 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
341 *****************************************************************************/
342 static int MjpgDemux( demux_t *p_demux )
344 demux_sys_t *p_sys = p_demux->p_sys;
347 if( !Peek( p_demux, VLC_TRUE ) )
349 msg_Warn( p_demux, "cannot peek data" );
352 if( p_sys->i_data_peeked < 4 )
354 msg_Warn( p_demux, "data shortage" );
358 while( !( 0xFF == p_sys->p_peek[i-1] && 0xD9 == p_sys->p_peek[i] ) )
361 if( i >= p_sys->i_data_peeked )
363 msg_Dbg( p_demux, "Did not find JPEG EOI in %d bytes",
364 p_sys->i_data_peeked );
365 if( !Peek( p_demux, VLC_FALSE ) )
367 msg_Warn( p_demux, "No more data is available at the moment" );
373 msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
374 return SendBlock( p_demux, i );
377 static int MimeDemux( demux_t *p_demux )
379 demux_sys_t *p_sys = p_demux->p_sys;
384 b_match = CheckMimeHeader( p_demux, &i_size );
387 stream_Read( p_demux->s, NULL, i_size );
389 else if( i_size < 0 )
395 // No MIME header, assume OK
399 if( !Peek( p_demux, VLC_TRUE ) )
401 msg_Warn( p_demux, "cannot peek data" );
405 i_size = strlen( p_sys->psz_separator ) + 2;
406 if( p_sys->i_data_peeked < i_size )
408 msg_Warn( p_demux, "data shortage" );
414 while( !( p_sys->p_peek[i] == '-' && p_sys->p_peek[i+1] == '-' ) )
418 if( i_size >= p_sys->i_data_peeked )
420 msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
421 "data", p_sys->i_data_peeked );
423 if( !Peek( p_demux, VLC_FALSE ) )
425 msg_Warn( p_demux, "No more data is available at the "
431 if( !strncmp( p_sys->psz_separator, p_sys->p_peek + i + 2,
432 strlen( p_sys->psz_separator ) ) )
435 msg_Dbg( p_demux, "MIME boundary detected at %d", i );
441 msg_Dbg( p_demux, "not done" );
445 msg_Dbg( p_demux, "i is %d", i );
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 demux_sys_t *p_sys = p_demux->p_sys;
478 return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );