]> git.sesse.net Git - vlc/blobdiff - src/video_output/vout_intf.c
Fixed bug in snapshot format
[vlc] / src / video_output / vout_intf.c
index d61684fca147e968443811006693250f572126b8..313ced1137580ef9736b435a28f31f8e2b434742 100644 (file)
@@ -459,9 +459,9 @@ void vout_IntfInit( vout_thread_t *p_vout )
  * This function will inject a subpicture into the vout with the provided
  * picture
  */
-static int VoutSnapshotPip( vout_thread_t *p_vout, image_handler_t *p_image, picture_t *p_pic, const video_format_t *p_fmt_in )
+static int VoutSnapshotPip( vout_thread_t *p_vout, picture_t *p_pic )
 {
-    video_format_t fmt_in = *p_fmt_in;
+    video_format_t fmt_in = p_pic->format;
     video_format_t fmt_out;
     picture_t *p_pip;
     subpicture_t *p_subpic;
@@ -472,12 +472,19 @@ static int VoutSnapshotPip( vout_thread_t *p_vout, image_handler_t *p_image, pic
     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
 
     /* */
+    image_handler_t *p_image = image_HandlerCreate( p_vout );
+    if( !p_image )
+        return VLC_EGENERIC;
+
     p_pip = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
+
+    image_HandlerDelete( p_image );
+
     if( !p_pip )
         return VLC_EGENERIC;
 
     p_subpic = subpicture_New();
-    if( p_subpic == NULL )
+    if( !p_subpic )
     {
          picture_Release( p_pip );
          return VLC_EGENERIC;
@@ -576,271 +583,254 @@ static char *VoutSnapshotGetDefaultDirectory( void )
 
     return psz_path;
 }
-
-int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
+/**
+ * This function will save a video snapshot to a file
+ */
+static int VoutWriteSnapshot( vout_thread_t *p_vout, char **ppsz_filename,
+                              const block_t *p_image,
+                              const char *psz_path,
+                              const char *psz_format,
+                              const char *psz_prefix_fmt )
 {
-    image_handler_t *p_image = image_HandlerCreate( p_vout );
-    video_format_t fmt_in, fmt_out;
-    char *psz_filename = NULL;
-    vlc_value_t val, format;
-    DIR *path;
-    int i_ret;
-    bool b_embedded_snapshot;
-    void *p_obj;
-
-    /* */
-    val.psz_string = var_GetNonEmptyString( p_vout, "snapshot-path" );
-
-    /* Embedded snapshot : if snapshot-path == object:object_ptr */
-    if( val.psz_string && sscanf( val.psz_string, "object:%p", &p_obj ) > 0 )
-        b_embedded_snapshot = true;
-    else
-        b_embedded_snapshot = false;
-
-    /* */
-    memset( &fmt_in, 0, sizeof(video_format_t) );
-    fmt_in = p_vout->fmt_out;
-    if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
-    {
-        fmt_in.i_sar_num =
-        fmt_in.i_sar_den = 1;
-    }
-
     /* */
-    memset( &fmt_out, 0, sizeof(video_format_t) );
-    fmt_out.i_sar_num =
-    fmt_out.i_sar_den = 1;
-    fmt_out.i_chroma = b_embedded_snapshot ? VLC_FOURCC('p','n','g',' ') : 0;
-    fmt_out.i_width = var_GetInteger( p_vout, "snapshot-width" );
-    fmt_out.i_height = var_GetInteger( p_vout, "snapshot-height" );
-
-    if( b_embedded_snapshot &&
-        fmt_out.i_width == 0 && fmt_out.i_height == 0 )
-    {
-        /* If snapshot-width and/or snapshot height were not specified,
-           use a default snapshot width of 320 */
-        fmt_out.i_width = 320;
-    }
-
-    if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
-    {
-        fmt_out.i_height = fmt_in.i_height * fmt_out.i_width / fmt_in.i_width;
-        const int i_height = fmt_out.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
-        if( i_height > 0 )
-            fmt_out.i_height = i_height;
-    }
-    else
+    char *psz_filename;
+    DIR *p_path = utf8_opendir( psz_path );
+    if( p_path != NULL )
     {
-        if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
+        /* The use specified a directory path */
+        closedir( p_path );
+
+        /* */
+        char *psz_prefix = NULL;
+        if( psz_prefix_fmt )
+            psz_prefix = str_format( p_vout, psz_prefix_fmt );
+        if( !psz_prefix )
         {
-            fmt_out.i_width = fmt_in.i_width * fmt_out.i_height / fmt_in.i_height;
-        }
-        else
-        {
-            fmt_out.i_width = fmt_in.i_width;
-            fmt_out.i_height = fmt_in.i_height;
-        }
-        const int i_width = fmt_out.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
-        if( i_width > 0 )
-            fmt_out.i_width = i_width;
-    }
-
-    /* Embedded snapshot
-       create a snapshot_t* and store it in
-       object_ptr->p_private, then unlock and signal the
-       waiting object.
-     */
-    if( b_embedded_snapshot )
-    {
-        block_t *p_block;
-        snapshot_t *p_snapshot = p_obj;
-        size_t i_size;
-
-       vlc_mutex_lock( &p_snapshot->p_mutex );
-       p_snapshot->p_data = NULL;
-
-        /* Save the snapshot to a memory zone */
-        p_block = image_Write( p_image, p_pic, &fmt_in, &fmt_out );
-        if( !p_block )
-        {
-            msg_Err( p_vout, "Could not get snapshot" );
-            image_HandlerDelete( p_image );
-           vlc_cond_signal( &p_snapshot->p_condvar );
-           vlc_mutex_unlock( &p_snapshot->p_mutex );
-            return VLC_EGENERIC;
-        }
-
-        i_size = p_block->i_buffer;
-
-        p_snapshot->i_width = fmt_out.i_width;
-        p_snapshot->i_height = fmt_out.i_height;
-        p_snapshot->i_datasize = i_size;
-        p_snapshot->date = p_block->i_pts; /* FIXME ?? */
-        p_snapshot->p_data = malloc( i_size );
-        if( !p_snapshot->p_data )
-        {
-            block_Release( p_block );
-            image_HandlerDelete( p_image );
-           vlc_cond_signal( &p_snapshot->p_condvar );
-           vlc_mutex_unlock( &p_snapshot->p_mutex );
-            return VLC_ENOMEM;
-        }
-        memcpy( p_snapshot->p_data, p_block->p_buffer, p_block->i_buffer );
-
-        block_Release( p_block );
-
-        /* Unlock the object */
-       vlc_cond_signal( &p_snapshot->p_condvar );
-       vlc_mutex_unlock( &p_snapshot->p_mutex );
-
-        image_HandlerDelete( p_image );
-        return VLC_SUCCESS;
-    }
-
-    /* Get default directory if none provided */
-    if( !val.psz_string )
-        val.psz_string = VoutSnapshotGetDefaultDirectory( );
-    if( !val.psz_string )
-    {
-        msg_Err( p_vout, "no path specified for snapshots" );
-        image_HandlerDelete( p_image );
-        return VLC_EGENERIC;
-    }
-
-    /* Get snapshot format, default being "png" */
-    format.psz_string = var_GetNonEmptyString( p_vout, "snapshot-format" );
-    if( !format.psz_string )
-        format.psz_string = strdup( "png" );
-    if( !format.psz_string )
-    {
-        free( val.psz_string );
-        image_HandlerDelete( p_image );
-        return VLC_ENOMEM;
-    }
-
-    /*
-     * Did the user specify a directory? If not, path = NULL.
-     */
-    path = utf8_opendir ( (const char *)val.psz_string  );
-    if( path != NULL )
-    {
-        char *psz_prefix = var_GetNonEmptyString( p_vout, "snapshot-prefix" );
-        if( psz_prefix == NULL )
             psz_prefix = strdup( "vlcsnap-" );
-        else
-        {
-            char *psz_tmp = str_format( p_vout, psz_prefix );
-            filename_sanitize( psz_tmp );
-            free( psz_prefix );
-            psz_prefix = psz_tmp;
+            if( !psz_prefix )
+                goto error;
         }
 
-        closedir( path );
-        if( var_GetBool( p_vout, "snapshot-sequential" ) == true )
+        if( var_GetBool( p_vout, "snapshot-sequential" ) )
         {
             int i_num = var_GetInteger( p_vout, "snapshot-num" );
-            struct stat st;
-
-            do
+            for( ; ; i_num++ )
             {
-                free( psz_filename );
+                struct stat st;
+
                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%05d.%s",
-                              val.psz_string, psz_prefix, i_num++,
-                              format.psz_string ) == -1 )
+                              psz_path, psz_prefix, i_num++, psz_format ) < 0 )
                 {
-                    msg_Err( p_vout, "could not create snapshot" );
-                    image_HandlerDelete( p_image );
-                    return VLC_EGENERIC;
+                    free( psz_prefix );
+                    goto error;
                 }
+                if( utf8_stat( psz_filename, &st ) )
+                    break;
+                free( psz_filename );
             }
-            while( utf8_stat( psz_filename, &st ) == 0 );
 
             var_SetInteger( p_vout, "snapshot-num", i_num );
         }
         else
         {
             struct tm    curtime;
-            time_t        lcurtime ;
-            lcurtime = time( NULL ) ;
-            if ( ( localtime_r( &lcurtime, &curtime ) == NULL ) )
+            time_t       lcurtime = time( NULL ) ;
+
+            if( !localtime_r( &lcurtime, &curtime ) )
             {
+                const unsigned int i_id = (p_image->i_pts / 100000) & 0xFFFFFF;
+
                 msg_Warn( p_vout, "failed to get current time. Falling back to legacy snapshot naming" );
-                /* failed to get current time. Fallback to old format */
+
                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%u.%s",
-                          val.psz_string, psz_prefix,
-                          (unsigned int)(p_pic->date / 100000) & 0xFFFFFF,
-                          format.psz_string ) == -1 )
-                {
-                    msg_Err( p_vout, "could not create snapshot" );
-                    image_HandlerDelete( p_image );
-                    return VLC_EGENERIC;
-                }
+                              psz_path, psz_prefix, i_id, psz_format ) < 0 )
+                    psz_filename = NULL;
             }
             else
             {
-                char psz_curtime[15] ;
-                if( strftime( psz_curtime, 15, "%y%m%d-%H%M%S", &curtime ) == 0 )
-                {
-                    msg_Warn( p_vout, "snapshot date string truncated" ) ;
-                }
+                /* suffix with the last decimal digit in 10s of seconds resolution
+                 * FIXME gni ? */
+                const int i_id = (p_image->i_pts / (100*1000)) & 0xFF;
+                char psz_curtime[128];
+
+                if( !strftime( psz_curtime, sizeof(psz_curtime), "%Y-%m-%d-%Hh%Mm%Ss", &curtime ) )
+                    strcpy( psz_curtime, "error" );
+
                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%s%1u.%s",
-                      val.psz_string, psz_prefix, psz_curtime,
-                     /* suffix with the last decimal digit in 10s of seconds resolution */
-                     (unsigned int)(p_pic->date / (100*1000)) & 0xFF,
-                      format.psz_string ) == -1 )
-                {
-                    msg_Err( p_vout, "could not create snapshot" );
-                    image_HandlerDelete( p_image );
-                    return VLC_EGENERIC;
-                }
-            } //end if time() < 0
-        } //end snapshot sequential
+                              psz_path, psz_prefix, psz_curtime, i_id, psz_format ) < 0 )
+                    psz_filename = NULL;
+            }
+        }
         free( psz_prefix );
     }
-    else // The user specified a full path name (including file name)
+    else
     {
-        psz_filename = str_format( p_vout, val.psz_string );
+        /* The user specified a full path name (including file name) */
+        psz_filename = str_format( p_vout, psz_path );
         path_sanitize( psz_filename );
     }
 
-    free( val.psz_string );
-    free( format.psz_string );
+    if( !psz_filename )
+        goto error;
 
     /* Save the snapshot */
-    i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
-    if( i_ret != VLC_SUCCESS )
+    FILE *p_file = utf8_fopen( psz_filename, "wb" );
+    if( !p_file )
     {
-        msg_Err( p_vout, "could not create snapshot %s", psz_filename );
+        msg_Err( p_vout, "Failed to open '%s'", psz_filename );
         free( psz_filename );
-        image_HandlerDelete( p_image );
-        return VLC_EGENERIC;
+        goto error;
+    }
+    if( fwrite( p_image->p_buffer, p_image->i_buffer, 1, p_file ) != 1 )
+    {
+        msg_Err( p_vout, "Failed to write to '%s'", psz_filename );
+        fclose( p_file );
+        free( psz_filename );
+        goto error;
     }
+    fclose( p_file );
 
     /* */
-    msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
-    vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN,
-                     "%s", psz_filename );
+    if( ppsz_filename )
+        *ppsz_filename = psz_filename;
+    else
+        free( psz_filename );
 
-    /* Generate a media player event  - Right now just trigger a global libvlc var
-        CHECK: Could not find a more local object. The goal is to communicate
-        vout_thread with libvlc_media_player or its input_thread*/
-    val.psz_string =  psz_filename  ;
+    return VLC_SUCCESS;
 
-    var_Set( p_vout->p_libvlc, "vout-snapshottaken", val );
-    /* var_Set duplicates data for transport so we can free*/
-    free( psz_filename );
+error:
+    msg_Err( p_vout, "could not save snapshot" );
+    return VLC_EGENERIC;
+}
+
+/**
+ * This function will display the name and a PIP of the provided snapshot
+ */
+static void VoutOsdSnapshot( vout_thread_t *p_vout, picture_t *p_pic, const char *psz_filename )
+{
+    msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
+    vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN, "%s", psz_filename );
 
-    /* */
     if( var_GetBool( p_vout, "snapshot-preview" ) )
     {
-        if( VoutSnapshotPip( p_vout, p_image, p_pic, &fmt_in ) )
+        if( VoutSnapshotPip( p_vout, p_pic ) )
             msg_Warn( p_vout, "Failed to display snapshot" );
     }
-    image_HandlerDelete( p_image );
+}
+
+/* */
+int vout_GetSnapshot( vout_thread_t *p_vout,
+                      block_t **pp_image, picture_t **pp_picture,
+                      video_format_t *p_fmt,
+                      const char *psz_format, mtime_t i_timeout )
+{
+    vout_thread_sys_t *p_sys = p_vout->p;
+
+    vlc_mutex_lock( &p_sys->snapshot.lock );
+    p_sys->snapshot.i_request++;
+
+    const mtime_t i_deadline = mdate() + i_timeout;
+    while( p_sys->snapshot.b_available && !p_sys->snapshot.p_picture &&
+           mdate() < i_deadline )
+    {
+        vlc_cond_timedwait( &p_sys->snapshot.wait, &p_sys->snapshot.lock,
+                            i_timeout );
+    }
 
+    picture_t *p_picture = p_sys->snapshot.p_picture;
+    if( p_picture )
+        p_sys->snapshot.p_picture = p_picture->p_next;
+    else if( p_sys->snapshot.i_request > 0 )
+        p_sys->snapshot.i_request--;
+    vlc_mutex_unlock( &p_sys->snapshot.lock );
+
+    if( !p_picture )
+    {
+        msg_Err( p_vout, "Failed to grab a snapshot" );
+        return VLC_EGENERIC;
+    }
+
+    if( pp_image )
+    {
+        vlc_fourcc_t i_format = VLC_FOURCC('p','n','g',' ');
+        if( psz_format && image_Type2Fourcc( psz_format ) )
+            i_format = image_Type2Fourcc( psz_format );
+
+        const int i_override_width  = var_GetInteger( p_vout, "snapshot-width" );
+        const int i_override_height = var_GetInteger( p_vout, "snapshot-height" );
+
+        if( picture_Export( VLC_OBJECT(p_vout), pp_image, p_fmt,
+                            p_picture, i_format, i_override_width, i_override_height ) )
+        {
+            msg_Err( p_vout, "Failed to convert image for snapshot" );
+            picture_Release( p_picture );
+            return VLC_EGENERIC;
+        }
+    }
+    if( pp_picture )
+        *pp_picture = p_picture;
+    else
+        picture_Release( p_picture );
     return VLC_SUCCESS;
 }
 
+/**
+ * This function will handle a snapshot request
+ */
+static void VoutSaveSnapshot( vout_thread_t *p_vout )
+{
+    char *psz_path = var_GetNonEmptyString( p_vout, "snapshot-path" );
+    char *psz_format = var_GetNonEmptyString( p_vout, "snapshot-format" );
+    char *psz_prefix = var_GetNonEmptyString( p_vout, "snapshot-prefix" );
+
+    /* */
+    picture_t *p_picture;
+    block_t *p_image;
+    video_format_t fmt;
+
+    /* 500ms timeout
+     * XXX it will cause trouble with low fps video (< 2fps) */
+    if( vout_GetSnapshot( p_vout, &p_image, &p_picture, &fmt, psz_format, 500*1000 ) )
+    {
+        p_picture = NULL;
+        p_image = NULL;
+        goto exit;
+    }
+
+    if( !psz_path )
+    {
+        psz_path = VoutSnapshotGetDefaultDirectory();
+        if( !psz_path )
+        {
+            msg_Err( p_vout, "no path specified for snapshots" );
+            goto exit;
+        }
+    }
+
+    char *psz_filename;
+    if( VoutWriteSnapshot( p_vout, &psz_filename,
+                           p_image,
+                           psz_path, psz_format, psz_prefix ) )
+        goto exit;
+
+    VoutOsdSnapshot( p_vout, p_picture, psz_filename );
+
+    /* Generate a media player event  - Right now just trigger a global libvlc var
+        CHECK: Could not find a more local object. The goal is to communicate
+        vout_thread with libvlc_media_player or its input_thread */
+    var_SetString( p_vout->p_libvlc, "vout-snapshottaken", psz_filename );
+    free( psz_filename );
+
+exit:
+    if( p_image )
+        block_Release( p_image );
+    if( p_picture )
+        picture_Release( p_picture );
+    free( psz_prefix );
+    free( psz_format );
+    free( psz_path );
+}
+
 /*****************************************************************************
  * Handle filters
  *****************************************************************************/
@@ -918,7 +908,6 @@ static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
     {
         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
-        goto initwsize_end;
     }
     else if( i_width > 0 )
     {
@@ -926,7 +915,6 @@ static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
             p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
             FP_FACTOR / p_vout->fmt_in.i_visible_width );
-        goto initwsize_end;
     }
     else if( i_height > 0 )
     {
@@ -934,10 +922,9 @@ static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
             p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
             FP_FACTOR / p_vout->fmt_in.i_visible_height );
-        goto initwsize_end;
     }
-
-    if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 ) {
+    else if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 )
+    {
         msg_Warn( p_vout, "aspect ratio screwed up" );
         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
@@ -957,9 +944,7 @@ static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
             p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
     }
 
-initwsize_end:
-    msg_Dbg( p_vout, "window size: %dx%d", p_vout->i_window_width,
-             p_vout->i_window_height );
+    msg_Dbg( p_vout, "window size: %ux%u", *pi_width, *pi_height );
 
 #undef FP_FACTOR
 }
@@ -1251,11 +1236,10 @@ static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
     vout_thread_t *p_vout = (vout_thread_t *)p_this;
-
-    /* FIXME: this is definitely not thread-safe */
-    p_vout->p->b_snapshot = true;
     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
     VLC_UNUSED(newval); VLC_UNUSED(p_data);
+
+    VoutSaveSnapshot( p_vout );
     return VLC_SUCCESS;
 }