/*****************************************************************************
* mjpeg.c : demuxes mjpeg webcam http streams
*****************************************************************************
- * Copyright (C) 2004 VideoLAN
- * $Id: mjpeg.c 7196 2004-03-29 21:29:31Z fenrir $
+ * Copyright (C) 2004 the VideoLAN team
+ * $Id$
*
* Authors: Henry Jen (slowhog) <henryjen@ztune.net>
* Derk-Jan Hartman (thedj)
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
-#include <stdlib.h> /* malloc(), free() */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
#include <vlc/vlc.h>
-#include <vlc/input.h>
+#include <vlc_demux.h>
-#include <codecs.h>
+#include <vlc_codecs.h>
/*****************************************************************************
* Module descriptor
static void Close( vlc_object_t * );
#define FPS_TEXT N_("Frames per Second")
-#define FPS_LONGTEXT N_("Allows you to set the desired frame rate when " \
- "playing from files, use 0 for live.")
-#define VAR_FPS "mjpeg-fps"
+#define FPS_LONGTEXT N_("This is the desired frame rate when " \
+ "playing MJPEG from a file. Use 0 (this is the default value) for a " \
+ "live stream (from a camera).")
vlc_module_begin();
- set_description( _("JPEG camera demuxer") );
+ set_shortname( "MJPEG");
+ set_description( _("M-JPEG camera demuxer") );
set_capability( "demux2", 5 );
set_callbacks( Open, Close );
- add_float( VAR_FPS, 0.0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_FALSE );
+ set_category( CAT_INPUT );
+ set_subcategory( SUBCAT_INPUT_DEMUX );
+ add_float( "mjpeg-fps", 0.0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_FALSE );
vlc_module_end();
/*****************************************************************************
es_format_t fmt;
es_out_id_t *p_es;
- int64_t i_data_pos;
- unsigned int i_data_size;
+ vlc_bool_t b_still;
+ mtime_t i_still_end;
+ mtime_t i_still_length;
mtime_t i_time;
mtime_t i_frame_length;
char *psz_separator;
int i_frame_size_estimate;
- uint8_t *p_peek;
+ const uint8_t *p_peek;
int i_data_peeked;
};
/*****************************************************************************
- * Peek: Helper function to peek data with incremental size.
+ * Peek: Helper function to peek data with incremental size.
* \return VLC_FALSE if peek no more data, VLC_TRUE otherwise.
*****************************************************************************/
static vlc_bool_t Peek( demux_t *p_demux, vlc_bool_t b_first )
{
p_sys->i_frame_size_estimate += 5120;
}
- i_data = stream_Peek( p_demux->s, &p_sys->p_peek, p_sys->i_frame_size_estimate );
+ i_data = stream_Peek( p_demux->s, &p_sys->p_peek,
+ p_sys->i_frame_size_estimate );
if( i_data == p_sys->i_data_peeked )
{
msg_Warn( p_demux, "no more data" );
static char* GetLine( demux_t *p_demux, int *p_pos )
{
demux_sys_t *p_sys = p_demux->p_sys;
- uint8_t *p_buf;
+ const uint8_t *p_buf;
int i_size;
int i;
char *p_line;
msg_Err( p_demux, "out of memory" );
return NULL;
}
- strncpy ( p_line, p_buf, i );
+ strncpy ( p_line, (char*)p_buf, i );
p_line[i] = '\0';
// msg_Dbg( p_demux, "i = %d, pos = %d, %s", i, *p_pos, p_line );
return p_line;
vlc_bool_t b_jpeg = VLC_FALSE;
int i_pos;
char *psz_line;
- char *p_ch;
+ char *p_ch;
demux_sys_t *p_sys = p_demux->p_sys;
if( !Peek( p_demux, VLC_TRUE ) )
*p_header_size = -2;
return VLC_FALSE;
}
- if( strncmp( p_sys->p_peek, "--", 2 ) )
+ if( strncmp( (char *)p_sys->p_peek, "--", 2 ) )
{
*p_header_size = 0;
return VLC_FALSE;
*p_header_size = -3;
return VLC_FALSE;
}
- if( NULL == p_sys->psz_separator )
+
+ /* Read the separator and remember it if not yet stored */
+ if( p_sys->psz_separator == NULL )
{
p_sys->psz_separator = psz_line;
- msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s", p_sys->psz_separator );
+ msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
+ p_sys->psz_separator );
}
else
{
if( strcmp( psz_line, p_sys->psz_separator ) )
{
- msg_Warn( p_demux, "separator %s does not match %s", psz_line, p_sys->psz_separator );
+ msg_Warn( p_demux, "separator %s does not match %s", psz_line,
+ p_sys->psz_separator );
}
free( psz_line );
}
+
psz_line = GetLine( p_demux, &i_pos );
while( psz_line && *psz_line )
{
- msg_Dbg( p_demux, "%s", psz_line );
if( !strncasecmp( psz_line, "Content-Type:", 13 ) )
{
p_ch = psz_line + 13;
}
else
{
- msg_Dbg( p_demux, "Discard MIME header: %s", psz_line );
+ msg_Dbg( p_demux, "discard MIME header: %s", psz_line );
}
free( psz_line );
psz_line = GetLine( p_demux, &i_pos );
}
+
if( NULL == psz_line )
{
msg_Err( p_demux, "no EOL" );
*p_header_size = -3;
return VLC_FALSE;
}
+
+ free( psz_line );
+
*p_header_size = i_pos;
return b_jpeg;
}
return 0;
}
- if( 0 == p_sys->i_frame_length )
+ if( !p_sys->i_frame_length || !p_sys->i_time )
{
- p_block->i_dts = p_block->i_pts = mdate() + 1;
+ p_sys->i_time = p_block->i_dts = p_block->i_pts = mdate();
}
else
{
/* set PCR */
es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
es_out_Send( p_demux->out, p_sys->p_es, p_block );
+
+ if( p_sys->b_still )
+ {
+ p_sys->i_still_end = mdate() + p_sys->i_still_length;
+ }
+
return 1;
}
int i_size;
int b_matched = VLC_FALSE;
vlc_value_t val;
- float f_fps;
p_demux->pf_control = Control;
p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
p_sys->p_es = NULL;
- p_sys->i_time = 1;
- var_Create( p_demux, VAR_FPS, VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
- var_Get( p_demux, VAR_FPS, &val );
- f_fps = val.f_float;
- p_sys->i_frame_length = 1000000.0 / f_fps;
+ p_sys->i_time = 0;
+
p_sys->psz_separator = NULL;
p_sys->i_frame_size_estimate = 15 * 1024;
goto error;
}
+
+ var_Create( p_demux, "mjpeg-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
+ var_Get( p_demux, "mjpeg-fps", &val );
+ p_sys->i_frame_length = 0;
+
+ /* Check for jpeg file extension */
+ p_sys->b_still = VLC_FALSE;
+ p_sys->i_still_end = 0;
+ if( demux2_IsPathExtension( p_demux, ".jpeg" ) ||
+ demux2_IsPathExtension( p_demux, ".jpg" ) )
+ {
+ p_sys->b_still = VLC_TRUE;
+ if( val.f_float)
+ {
+ p_sys->i_still_length =1000000.0 / val.f_float;
+ }
+ else
+ {
+ /* Defaults to 1fps */
+ p_sys->i_still_length = 1000000;
+ }
+ }
+ else if ( val.f_float )
+ {
+ p_sys->i_frame_length = 1000000.0 / val.f_float;
+ }
+
es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
p_sys->fmt.i_codec = VLC_FOURCC('m','j','p','g');
return VLC_SUCCESS;
error:
- msg_Warn( p_demux, "JPEG camera module discarded" );
free( p_sys );
return VLC_EGENERIC;
}
static int MjpgDemux( demux_t *p_demux )
{
demux_sys_t *p_sys = p_demux->p_sys;
- int i;
+ int i;
+
+ if( p_sys->b_still && p_sys->i_still_end && p_sys->i_still_end < mdate() )
+ {
+ /* Still frame, wait until the pause delay is gone */
+ p_sys->i_still_end = 0;
+ }
+ else if( p_sys->b_still && p_sys->i_still_end )
+ {
+ msleep( 400 );
+ return 1;
+ }
if( !Peek( p_demux, VLC_TRUE ) )
{
i++;
if( i >= p_sys->i_data_peeked )
{
- msg_Dbg( p_demux, "Did not find JPEG EOI in %d bytes", p_sys->i_data_peeked );
+ msg_Dbg( p_demux, "did not find JPEG EOI in %d bytes",
+ p_sys->i_data_peeked );
if( !Peek( p_demux, VLC_FALSE ) )
{
- msg_Warn( p_demux, "No more data is available at the moment" );
+ msg_Warn( p_demux, "no more data is available at the moment" );
return 0;
}
}
}
i++;
+
msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
return SendBlock( p_demux, i );
}
i_size++;
if( i_size >= p_sys->i_data_peeked )
{
- msg_Dbg( p_demux, "MIME boundary not found in %d bytes of data", p_sys->i_data_peeked );
+ msg_Dbg( p_demux, "MIME boundary not found in %d bytes of "
+ "data", p_sys->i_data_peeked );
+
if( !Peek( p_demux, VLC_FALSE ) )
{
- msg_Warn( p_demux, "No more data is available at the moment" );
+ msg_Warn( p_demux, "no more data is available at the "
+ "moment" );
return 0;
}
}
}
- if( !strncmp( p_sys->psz_separator, p_sys->p_peek + i + 2, strlen( p_sys->psz_separator ) ) )
+ if( !strncmp( p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
+ strlen( p_sys->psz_separator ) ) )
{
b_done = VLC_TRUE;
- msg_Dbg( p_demux, "MIME boundary detected at %d", i );
}
else
{
i++;
i_size++;
- msg_Dbg( p_demux, "not done" );
}
}
- msg_Dbg( p_demux, "i is %d", i );
if( !b_match )
{
- msg_Err( p_demux, "Discard non-JPEG part" );
+ msg_Err( p_demux, "discard non-JPEG part" );
stream_Read( p_demux->s, NULL, i );
return 0;
}
demux_t *p_demux = (demux_t*)p_this;
demux_sys_t *p_sys = p_demux->p_sys;
- if( p_sys->psz_separator )
- {
- free( p_sys->psz_separator );
- }
+ free( p_sys->psz_separator );
free( p_sys );
}
*****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
- demux_sys_t *p_sys = p_demux->p_sys;
- int64_t i_end = -1;
- if( p_sys->i_data_size > 0 )
- {
- i_end = p_sys->i_data_pos + p_sys->i_data_size;
- }
- return demux2_vaControlHelper( p_demux->s,
- p_sys->i_data_pos, i_end,
- p_sys->fmt.i_bitrate, p_sys->fmt.audio.i_blockalign,
- i_query, args );
+ return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 0, i_query, args );
}
-
-