/*****************************************************************************
* mjpeg.c : demuxes mjpeg webcam http streams
*****************************************************************************
- * Copyright (C) 2004 the VideoLAN team
+ * Copyright (C) 2004 VLC authors and VideoLAN
* $Id$
*
* Authors: Henry Jen (slowhog) <henryjen@ztune.net>
* Sigmund Augdal (Dnumgis)
* Laurent Aimar <fenrir@via.ecp.fr>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
*
- * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
i_size = p_sys->i_data_peeked - *p_pos;
}
}
- *p_pos += ( i + 1 );
- if( i > 0 && '\r' == p_buf[i - 1] )
+ *p_pos += i + 1;
+ if( i > 0 && p_buf[i - 1] == '\r' )
{
i--;
}
p_line = malloc( i + 1 );
- if( p_line == NULL )
+ if( unlikely( p_line == NULL ) )
return NULL;
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;
}
*****************************************************************************/
static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
{
- bool b_jpeg = false;
+ bool b_jpeg = false;
int i_pos = 0;
char *psz_line;
char *p_ch;
demux_sys_t *p_sys = p_demux->p_sys;
+ *p_header_size = -1;
if( !Peek( p_demux, true ) )
{
msg_Err( p_demux, "cannot peek" );
- *p_header_size = -1;
return false;
}
if( p_sys->i_data_peeked < 5)
{
msg_Err( p_demux, "data shortage" );
- *p_header_size = -2;
return false;
}
if( strncmp( (char *)p_sys->p_peek, "--", 2 ) != 0
&& strncmp( (char *)p_sys->p_peek, "\r\n--", 4 ) != 0 )
{
- /* Some broken stream may lack the first boundary */
- if ( p_sys->psz_separator == NULL )
- {
- msg_Warn( p_demux, "Malformed stream. Trying to work around");
- char *content_type = stream_ContentType( p_demux->s );
- if ( content_type == NULL )
- return false;
- const char* boundary = strstr( content_type, "boundary=--" );
- if ( boundary != NULL )
- {
- p_sys->psz_separator = strdup( boundary + strlen( "boundary=--" ) );
- msg_Dbg( p_demux, "Video boundary extracted from Content-Type: %s", p_sys->psz_separator );
- free( content_type );
- /* Skip to HTTP header parsing as there's no boundary to extract
- * from the stream */
- }
- else
- {
- free( content_type );
- return false;
- }
- }
- else
- {
- *p_header_size = 0;
- return false;
- }
+ *p_header_size = 0;
+ return false;
}
else
{
if( NULL == psz_line )
{
msg_Err( p_demux, "no EOL" );
- *p_header_size = -3;
return false;
}
if( NULL == psz_line )
{
msg_Err( p_demux, "no EOL" );
- *p_header_size = -3;
return false;
}
static int Open( vlc_object_t * p_this )
{
demux_t *p_demux = (demux_t*)p_this;
- demux_sys_t *p_sys;
int i_size;
bool b_matched = false;
- p_sys = malloc( sizeof( demux_sys_t ) );
+ if( IsMxpeg( p_demux->s ) && !p_demux->b_force )
+ // let avformat handle this case
+ return VLC_EGENERIC;
+
+ demux_sys_t *p_sys = malloc( sizeof( demux_sys_t ) );
if( unlikely(p_sys == NULL) )
return VLC_ENOMEM;
p_sys->psz_separator = NULL;
p_sys->i_frame_size_estimate = 15 * 1024;
- if( IsMxpeg( p_demux->s ) && !p_demux->b_force )
+ char *content_type = stream_ContentType( p_demux->s );
+ if ( content_type )
{
- // let avformat handle this case
- goto error;
+ //FIXME: this is not fully match to RFC
+ char* boundary = strstr( content_type, "boundary=" );
+ if( boundary )
+ {
+ boundary += strlen( "boundary=" );
+ size_t len = strlen( boundary );
+ if( len > 2 && boundary[0] == '"'
+ && boundary[len-1] == '"' )
+ {
+ boundary[len-1] = '\0';
+ boundary++;
+ }
+ p_sys->psz_separator = strdup( boundary );
+ if( !p_sys->psz_separator )
+ {
+ free( content_type );
+ goto error;
+ }
+ }
+ free( content_type );
}
b_matched = CheckMimeHeader( p_demux, &i_size);
p_demux->pf_demux = MimeDemux;
stream_Read( p_demux->s, NULL, i_size );
}
- else if( 0 == i_size )
+ else if( i_size == 0 )
{
/* 0xffd8 identify a JPEG SOI */
if( p_sys->p_peek[0] == 0xFF && p_sys->p_peek[1] == 0xD8 )
return VLC_SUCCESS;
error:
+ free( p_sys->psz_separator );
free( p_sys );
return VLC_EGENERIC;
}
}
i = 3;
FIND_NEXT_EOI:
- while( !( 0xFF == p_sys->p_peek[i-1] && 0xD9 == p_sys->p_peek[i] ) )
+ while( !( p_sys->p_peek[i-1] == 0xFF && p_sys->p_peek[i] == 0xD9 ) )
{
- if( 0xFF == p_sys->p_peek[i-1] && 0xD8 == p_sys->p_peek[i] )
+ if( p_sys->p_peek[i-1] == 0xFF && p_sys->p_peek[i] == 0xD9 )
{
p_sys->i_level++;
msg_Dbg( p_demux, "we found another JPEG SOI at %d", i );