]> git.sesse.net Git - vlc/blobdiff - modules/demux/mjpeg.c
mjpeg : remove optional quotes from MIME boundary
[vlc] / modules / demux / mjpeg.c
index 165b415084f4fa34e96b4b29ec17bf2e95a99e96..8def7f809a298bb877d0b590d380d11b05cc79e4 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * 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>
@@ -9,19 +9,19 @@
  *          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.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -35,8 +35,7 @@
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_demux.h>
-
-#include <vlc_codecs.h>
+#include "mxpeg_helper.h"
 
 /*****************************************************************************
  * Module descriptor
@@ -49,15 +48,15 @@ static void Close( vlc_object_t * );
     "playing MJPEG from a file. Use 0 (this is the default value) for a " \
     "live stream (from a camera).")
 
-vlc_module_begin();
-    set_shortname( "MJPEG");
-    set_description( N_("M-JPEG camera demuxer") );
-    set_capability( "demux", 5 );
-    set_callbacks( Open, Close );
-    set_category( CAT_INPUT );
-    set_subcategory( SUBCAT_INPUT_DEMUX );
-    add_float( "mjpeg-fps", 0.0, NULL, FPS_TEXT, FPS_LONGTEXT, false );
-vlc_module_end();
+vlc_module_begin ()
+    set_shortname( "MJPEG")
+    set_description( N_("M-JPEG camera demuxer") )
+    set_capability( "demux", 5 )
+    set_callbacks( Open, Close )
+    set_category( CAT_INPUT )
+    set_subcategory( SUBCAT_INPUT_DEMUX )
+    add_float( "mjpeg-fps", 0.0, FPS_TEXT, FPS_LONGTEXT, false )
+vlc_module_end ()
 
 /*****************************************************************************
  * Local prototypes
@@ -71,16 +70,15 @@ struct demux_sys_t
     es_format_t     fmt;
     es_out_id_t     *p_es;
 
-    bool      b_still;
+    bool            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;
     const uint8_t   *p_peek;
     int             i_data_peeked;
+    int             i_level;
 };
 
 /*****************************************************************************
@@ -127,7 +125,7 @@ static char* GetLine( demux_t *p_demux, int *p_pos )
     int         i;
     char        *p_line;
 
-    while( *p_pos > p_sys->i_data_peeked )
+    while( *p_pos >= p_sys->i_data_peeked )
     {
         if( ! Peek( p_demux, false ) )
         {
@@ -146,24 +144,20 @@ static char* GetLine( demux_t *p_demux, int *p_pos )
             {
                 return NULL;
             }
+            p_buf = p_sys->p_peek + *p_pos;
+            i_size = p_sys->i_data_peeked - *p_pos;
         }
-        p_buf = p_sys->p_peek + *p_pos;
-        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( NULL == p_line )
-    {
-        msg_Err( p_demux, "out of memory" );
+    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;
 }
 
@@ -175,53 +169,55 @@ static char* GetLine( demux_t *p_demux, int *p_pos )
  *****************************************************************************/
 static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
 {
-    bool  b_jpeg = false;
-    int         i_pos;
+    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 < 3)
+    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 ) )
+    if( strncmp( (char *)p_sys->p_peek, "--", 2 ) != 0
+        && strncmp( (char *)p_sys->p_peek, "\r\n--", 4 ) != 0 )
     {
         *p_header_size = 0;
         return false;
     }
-    i_pos = 2;
-    psz_line = GetLine( p_demux, &i_pos );
-    if( NULL == psz_line )
-    {
-        msg_Err( p_demux, "no EOL" );
-        *p_header_size = -3;
-        return false;
-    }
-
-    /* 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 );
-    }
     else
     {
-        if( strcmp( psz_line, p_sys->psz_separator ) )
+        i_pos = *p_sys->p_peek == '-' ? 2 : 4;
+        psz_line = GetLine( p_demux, &i_pos );
+        if( NULL == psz_line )
         {
-            msg_Warn( p_demux, "separator %s does not match %s", psz_line,
-                      p_sys->psz_separator );
+            msg_Err( p_demux, "no EOL" );
+            return false;
+        }
+
+        /* 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 );
+        }
+        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 );
+            }
+            free( psz_line );
         }
-        free( psz_line );
     }
 
     psz_line = GetLine( p_demux, &i_pos );
@@ -252,7 +248,6 @@ static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
     if( NULL == psz_line )
     {
         msg_Err( p_demux, "no EOL" );
-        *p_header_size = -3;
         return false;
     }
 
@@ -273,24 +268,23 @@ static int SendBlock( demux_t *p_demux, int i )
         return 0;
     }
 
-    if( !p_sys->i_frame_length || !p_sys->i_time )
+    if( p_sys->i_frame_length )
     {
-        p_sys->i_time = p_block->i_dts = p_block->i_pts = mdate();
+        p_block->i_pts = p_sys->i_time;
+        p_sys->i_time += p_sys->i_frame_length;
     }
     else
     {
-        p_block->i_dts = p_block->i_pts = p_sys->i_time;
-        p_sys->i_time += p_sys->i_frame_length;
+        p_block->i_pts = mdate();
     }
+    p_block->i_dts = p_block->i_pts;
 
     /* 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;
-    }
+        p_sys->i_still_end = mdate() + p_sys->i_frame_length;
 
     return 1;
 }
@@ -301,32 +295,65 @@ static int SendBlock( demux_t *p_demux, int i )
 static int Open( vlc_object_t * p_this )
 {
     demux_t     *p_demux = (demux_t*)p_this;
-    demux_sys_t *p_sys;
     int         i_size;
-    int         b_matched = false;
-    vlc_value_t val;
+    bool        b_matched = false;
+
+    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_demux->pf_control = Control;
-    p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
+    p_demux->p_sys      = p_sys;
     p_sys->p_es         = NULL;
-    p_sys->i_time       = 0;
+    p_sys->i_time       = VLC_TS_0;
+    p_sys->i_level      = 0;
 
     p_sys->psz_separator = NULL;
     p_sys->i_frame_size_estimate = 15 * 1024;
 
+    char *content_type = stream_ContentType( p_demux->s );
+    if ( content_type )
+    {
+        //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);
     if( b_matched )
     {
         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 )
         {
             msg_Dbg( p_demux, "JPEG SOI marker detected" );
             p_demux->pf_demux = MjpgDemux;
+            p_sys->i_level++;
         }
         else
         {
@@ -338,40 +365,31 @@ static int Open( vlc_object_t * p_this )
         goto error;
     }
 
+    /* Frame rate */
+    float f_fps = var_InheritFloat( p_demux, "mjpeg-fps" );
 
-    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 = false;
     p_sys->i_still_end = 0;
     if( demux_IsPathExtension( p_demux, ".jpeg" ) ||
         demux_IsPathExtension( p_demux, ".jpg" ) )
     {
+        /* Plain JPEG file = single still picture */
         p_sys->b_still = true;
-        if( val.f_float)
-        {
-            p_sys->i_still_length = 1000000.0 / val.f_float;
-        }
-        else
-        {
+        if( f_fps == 0.f )
             /* Defaults to 1fps */
-            p_sys->i_still_length = 1000000;
-        }
-    }
-    else if ( val.f_float )
-    {
-        p_sys->i_frame_length = 1000000.0 / val.f_float;
+            f_fps = 1.f;
     }
+    else
+        p_sys->b_still = false;
+    p_sys->i_frame_length = f_fps ? (CLOCK_FREQ / f_fps) : 0;
 
     es_format_Init( &p_sys->fmt, VIDEO_ES, 0 );
-    p_sys->fmt.i_codec = VLC_FOURCC('m','j','p','g');
+    p_sys->fmt.i_codec = VLC_CODEC_MJPG;
 
     p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
     return VLC_SUCCESS;
 
 error:
+    free( p_sys->psz_separator );
     free( p_sys );
     return VLC_EGENERIC;
 }
@@ -386,14 +404,11 @@ static int MjpgDemux( demux_t *p_demux )
     demux_sys_t *p_sys = p_demux->p_sys;
     int i;
 
-    if( p_sys->b_still && p_sys->i_still_end && p_sys->i_still_end < mdate() )
+    if( p_sys->b_still && p_sys->i_still_end )
     {
         /* Still frame, wait until the pause delay is gone */
+        mwait( p_sys->i_still_end );
         p_sys->i_still_end = 0;
-    }
-    else if( p_sys->b_still && p_sys->i_still_end )
-    {
-        msleep( 400 );
         return 1;
     }
 
@@ -408,8 +423,14 @@ static int MjpgDemux( demux_t *p_demux )
         return 0;
     }
     i = 3;
-    while( !( 0xFF == p_sys->p_peek[i-1] && 0xD9 == p_sys->p_peek[i] ) )
+FIND_NEXT_EOI:
+    while( !( p_sys->p_peek[i-1] == 0xFF && p_sys->p_peek[i] == 0xD9 ) )
     {
+        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 );
+        }
         i++;
         if( i >= p_sys->i_data_peeked )
         {
@@ -425,6 +446,10 @@ static int MjpgDemux( demux_t *p_demux )
     i++;
 
     msg_Dbg( p_demux, "JPEG EOI detected at %d", i );
+    p_sys->i_level--;
+
+    if( p_sys->i_level > 0 )
+        goto FIND_NEXT_EOI;
     return SendBlock( p_demux, i );
 }