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
* );
45 #define FPS_TEXT N_("Frames per Second")
46 #define FPS_LONGTEXT N_("This is the desired frame rate when " \
47 "playing MJPEG from a file. Use 0 (this is the default value) for a " \
48 "live stream (from a camera).")
51 set_shortname( "MJPEG")
52 set_description( N_("M-JPEG camera demuxer") )
53 set_capability( "demux", 5 )
54 set_callbacks( Open
, NULL
)
55 set_category( CAT_INPUT
)
56 set_subcategory( SUBCAT_INPUT_DEMUX
)
57 add_float( "mjpeg-fps", 0.0, FPS_TEXT
, FPS_LONGTEXT
, false )
60 /*****************************************************************************
62 *****************************************************************************/
63 static int MimeDemux( demux_t
* );
64 static int MjpgDemux( demux_t
* );
65 static int Control( demux_t
*, int i_query
, va_list args
);
73 vlc_tick_t i_still_end
;
75 vlc_tick_t i_frame_length
;
77 int i_frame_size_estimate
;
78 const uint8_t *p_peek
;
83 /*****************************************************************************
84 * Peek: Helper function to peek data with incremental size.
85 * \return false if peek no more data, true otherwise.
86 *****************************************************************************/
87 static bool Peek( demux_t
*p_demux
, bool b_first
)
90 demux_sys_t
*p_sys
= p_demux
->p_sys
;
94 p_sys
->i_data_peeked
= 0;
96 else if( p_sys
->i_data_peeked
== p_sys
->i_frame_size_estimate
)
98 p_sys
->i_frame_size_estimate
+= 5120;
100 i_data
= vlc_stream_Peek( p_demux
->s
, &p_sys
->p_peek
,
101 p_sys
->i_frame_size_estimate
);
102 if( i_data
== p_sys
->i_data_peeked
)
104 msg_Warn( p_demux
, "no more data" );
107 p_sys
->i_data_peeked
= i_data
;
110 msg_Warn( p_demux
, "cannot peek data" );
116 /*****************************************************************************
117 * GetLine: Internal function used to dup a line of string from the buffer
118 *****************************************************************************/
119 static char* GetLine( demux_t
*p_demux
, int *p_pos
)
121 demux_sys_t
*p_sys
= p_demux
->p_sys
;
122 const uint8_t *p_buf
;
127 while( *p_pos
>= p_sys
->i_data_peeked
)
129 if( ! Peek( p_demux
, false ) )
134 p_buf
= p_sys
->p_peek
+ *p_pos
;
135 i_size
= p_sys
->i_data_peeked
- *p_pos
;
137 while( p_buf
[i
] != '\n' )
142 if( ! Peek( p_demux
, false ) )
146 p_buf
= p_sys
->p_peek
+ *p_pos
;
147 i_size
= p_sys
->i_data_peeked
- *p_pos
;
151 if( i
> 0 && p_buf
[i
- 1] == '\r' )
155 p_line
= malloc( i
+ 1 );
156 if( unlikely( p_line
== NULL
) )
158 strncpy ( p_line
, (char*)p_buf
, i
);
163 /*****************************************************************************
164 * CheckMimeHeader: Internal function used to verify and skip mime header
165 * \param p_header_size Return size of MIME header, 0 if no MIME header
166 * detected, minus value if error
167 * \return true if content type is image/jpeg, false otherwise
168 *****************************************************************************/
169 static bool CheckMimeHeader( demux_t
*p_demux
, int *p_header_size
)
175 demux_sys_t
*p_sys
= p_demux
->p_sys
;
178 if( !Peek( p_demux
, true ) )
180 msg_Err( p_demux
, "cannot peek" );
183 if( p_sys
->i_data_peeked
< 5)
185 msg_Err( p_demux
, "data shortage" );
188 if( strncmp( (char *)p_sys
->p_peek
, "--", 2 ) != 0
189 && strncmp( (char *)p_sys
->p_peek
, "\r\n--", 4 ) != 0 )
196 i_pos
= *p_sys
->p_peek
== '-' ? 2 : 4;
197 psz_line
= GetLine( p_demux
, &i_pos
);
198 if( NULL
== psz_line
)
200 msg_Err( p_demux
, "no EOL" );
204 /* Read the separator and remember it if not yet stored */
205 if( p_sys
->psz_separator
== NULL
)
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
);
222 psz_line
= GetLine( p_demux
, &i_pos
);
223 while( psz_line
&& *psz_line
)
225 if( !strncasecmp( psz_line
, "Content-Type:", 13 ) )
227 p_ch
= psz_line
+ 13;
228 while( *p_ch
!= '\0' && ( *p_ch
== ' ' || *p_ch
== '\t' ) ) p_ch
++;
229 if( strncasecmp( p_ch
, "image/jpeg", 10 ) )
231 msg_Warn( p_demux
, "%s, image/jpeg is expected", psz_line
);
241 msg_Dbg( p_demux
, "discard MIME header: %s", psz_line
);
244 psz_line
= GetLine( p_demux
, &i_pos
);
247 if( NULL
== psz_line
)
249 msg_Err( p_demux
, "no EOL" );
255 *p_header_size
= i_pos
;
259 static int SendBlock( demux_t
*p_demux
, int i
)
261 demux_sys_t
*p_sys
= p_demux
->p_sys
;
264 if( ( p_block
= vlc_stream_Block( p_demux
->s
, i
) ) == NULL
)
266 msg_Warn( p_demux
, "cannot read data" );
267 return VLC_DEMUXER_EOF
;
270 if( p_sys
->i_frame_length
)
272 p_block
->i_pts
= p_sys
->i_time
;
273 p_sys
->i_time
+= p_sys
->i_frame_length
;
277 p_block
->i_pts
= vlc_tick_now();
279 p_block
->i_dts
= p_block
->i_pts
;
281 es_out_SetPCR( p_demux
->out
, p_block
->i_pts
);
282 es_out_Send( p_demux
->out
, p_sys
->p_es
, p_block
);
285 p_sys
->i_still_end
= vlc_tick_now() + p_sys
->i_frame_length
;
287 return VLC_DEMUXER_SUCCESS
;
290 /*****************************************************************************
291 * Open: check file and initializes structures
292 *****************************************************************************/
293 static int Open( vlc_object_t
* p_this
)
295 demux_t
*p_demux
= (demux_t
*)p_this
;
297 bool b_matched
= false;
299 if( IsMxpeg( p_demux
->s
) && !p_demux
->obj
.force
)
300 // let avformat handle this case
303 demux_sys_t
*p_sys
= vlc_obj_malloc( p_this
, sizeof (*p_sys
) );
304 if( unlikely(p_sys
== NULL
) )
307 p_demux
->p_sys
= p_sys
;
309 p_sys
->i_time
= VLC_TICK_0
;
312 p_sys
->psz_separator
= NULL
;
313 p_sys
->i_frame_size_estimate
= 15 * 1024;
315 char *content_type
= stream_ContentType( p_demux
->s
);
318 //FIXME: this is not fully match to RFC
319 char* boundary
= strstr( content_type
, "boundary=" );
322 boundary
+= strlen( "boundary=" );
323 size_t len
= strlen( boundary
);
324 if( len
> 2 && boundary
[0] == '"'
325 && boundary
[len
-1] == '"' )
327 boundary
[len
-1] = '\0';
330 p_sys
->psz_separator
= vlc_obj_strdup( p_this
, boundary
);
331 if( !p_sys
->psz_separator
)
333 free( content_type
);
337 free( content_type
);
340 b_matched
= CheckMimeHeader( p_demux
, &i_size
);
343 p_demux
->pf_demux
= MimeDemux
;
344 if( vlc_stream_Read( p_demux
->s
, NULL
, i_size
) < i_size
)
347 else if( i_size
== 0 )
349 /* 0xffd8 identify a JPEG SOI */
350 if( p_sys
->p_peek
[0] == 0xFF && p_sys
->p_peek
[1] == 0xD8 )
352 msg_Dbg( p_demux
, "JPEG SOI marker detected" );
353 p_demux
->pf_demux
= MjpgDemux
;
367 float f_fps
= var_InheritFloat( p_demux
, "mjpeg-fps" );
369 p_sys
->i_still_end
= 0;
370 if( demux_IsPathExtension( p_demux
, ".jpeg" ) ||
371 demux_IsPathExtension( p_demux
, ".jpg" ) )
373 /* Plain JPEG file = single still picture */
374 p_sys
->b_still
= true;
376 /* Defaults to 1fps */
380 p_sys
->b_still
= false;
381 p_sys
->i_frame_length
= f_fps
? (CLOCK_FREQ
/ f_fps
) : 0;
383 es_format_Init( &p_sys
->fmt
, VIDEO_ES
, VLC_CODEC_MJPG
);
385 p_sys
->p_es
= es_out_Add( p_demux
->out
, &p_sys
->fmt
);
386 if( unlikely(p_sys
->p_es
== NULL
) )
389 p_demux
->pf_control
= Control
;
393 /*****************************************************************************
394 * Demux: read packet and send them to decoders
395 *****************************************************************************
396 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
397 *****************************************************************************/
398 static int MjpgDemux( demux_t
*p_demux
)
400 demux_sys_t
*p_sys
= p_demux
->p_sys
;
403 if( p_sys
->b_still
&& p_sys
->i_still_end
)
405 /* Still frame, wait until the pause delay is gone */
406 vlc_tick_wait( p_sys
->i_still_end
);
407 p_sys
->i_still_end
= 0;
408 return VLC_DEMUXER_SUCCESS
;
411 if( !Peek( p_demux
, true ) )
413 msg_Warn( p_demux
, "cannot peek data" );
414 return VLC_DEMUXER_EOF
;
416 if( p_sys
->i_data_peeked
< 4 )
418 msg_Warn( p_demux
, "data shortage" );
419 return VLC_DEMUXER_EOF
;
423 while( !( p_sys
->p_peek
[i
-1] == 0xFF && p_sys
->p_peek
[i
] == 0xD9 ) )
425 if( p_sys
->p_peek
[i
-1] == 0xFF && p_sys
->p_peek
[i
] == 0xD9 )
428 msg_Dbg( p_demux
, "we found another JPEG SOI at %d", i
);
431 if( i
>= p_sys
->i_data_peeked
)
433 msg_Dbg( p_demux
, "did not find JPEG EOI in %d bytes",
434 p_sys
->i_data_peeked
);
435 if( !Peek( p_demux
, false ) )
437 msg_Warn( p_demux
, "no more data is available at the moment" );
438 return VLC_DEMUXER_EOF
;
444 msg_Dbg( p_demux
, "JPEG EOI detected at %d", i
);
447 if( p_sys
->i_level
> 0 )
449 return SendBlock( p_demux
, i
);
452 static int MimeDemux( demux_t
*p_demux
)
454 demux_sys_t
*p_sys
= p_demux
->p_sys
;
457 bool b_match
= CheckMimeHeader( p_demux
, &i_size
);
461 if( vlc_stream_Read( p_demux
->s
, NULL
, i_size
) != i_size
)
462 return VLC_DEMUXER_EOF
;
464 else if( i_size
< 0 )
466 return VLC_DEMUXER_EOF
;
470 // No MIME header, assume OK
474 if( !Peek( p_demux
, true ) )
476 msg_Warn( p_demux
, "cannot peek data" );
477 return VLC_DEMUXER_EOF
;
481 i_size
= strlen( p_sys
->psz_separator
) + 2;
482 if( p_sys
->i_data_peeked
< i_size
)
484 msg_Warn( p_demux
, "data shortage" );
485 return VLC_DEMUXER_EOF
;
490 while( !( p_sys
->p_peek
[i
] == '-' && p_sys
->p_peek
[i
+1] == '-' ) )
494 if( i_size
>= p_sys
->i_data_peeked
)
496 msg_Dbg( p_demux
, "MIME boundary not found in %d bytes of "
497 "data", p_sys
->i_data_peeked
);
499 if( !Peek( p_demux
, false ) )
501 msg_Warn( p_demux
, "no more data is available at the "
503 return VLC_DEMUXER_EOF
;
508 /* Handle old and new style of separators */
509 if (!strncmp(p_sys
->psz_separator
, (char *)(p_sys
->p_peek
+ i
+ 2),
510 strlen( p_sys
->psz_separator
))
511 || ((strlen(p_sys
->psz_separator
) > 4)
512 && !strncmp(p_sys
->psz_separator
, "--", 2)
513 && !strncmp(p_sys
->psz_separator
, (char *)(p_sys
->p_peek
+ i
),
514 strlen( p_sys
->psz_separator
))))
525 msg_Err( p_demux
, "discard non-JPEG part" );
526 return VLC_DEMUXER_EOF
;
529 return SendBlock( p_demux
, i
);
532 /*****************************************************************************
534 *****************************************************************************/
535 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
537 return demux_vaControlHelper( p_demux
->s
, 0, 0, 0, 0, i_query
, args
);