Commit 1fb01424 authored by Olivier Aubert's avatar Olivier Aubert

embedded snapshot: use condition variable

Let's see if this fits courmisch's standards...
parent 39276331
...@@ -714,6 +714,8 @@ typedef struct snapshot_t { ...@@ -714,6 +714,8 @@ typedef struct snapshot_t {
int i_height; /* In pixels */ int i_height; /* In pixels */
int i_datasize; /* In bytes */ int i_datasize; /* In bytes */
mtime_t date; /* Presentation time */ mtime_t date; /* Presentation time */
vlc_cond_t p_condvar;
vlc_mutex_t p_mutex;
} snapshot_t; } snapshot_t;
/**@}*/ /**@}*/
......
...@@ -56,7 +56,6 @@ mediacontrol_snapshot( mediacontrol_Instance *self, ...@@ -56,7 +56,6 @@ mediacontrol_snapshot( mediacontrol_Instance *self,
mediacontrol_Exception *exception ) mediacontrol_Exception *exception )
{ {
(void)a_position; (void)a_position;
vlc_object_t* p_cache;
vout_thread_t* p_vout; vout_thread_t* p_vout;
input_thread_t *p_input; input_thread_t *p_input;
mediacontrol_RGBPicture *p_pic = NULL; mediacontrol_RGBPicture *p_pic = NULL;
...@@ -67,6 +66,13 @@ mediacontrol_snapshot( mediacontrol_Instance *self, ...@@ -67,6 +66,13 @@ mediacontrol_snapshot( mediacontrol_Instance *self,
libvlc_exception_init( &ex ); libvlc_exception_init( &ex );
mediacontrol_exception_init( exception ); mediacontrol_exception_init( exception );
p_snapshot = malloc( sizeof( snapshot_t ) );
if( ! p_snapshot )
{
RAISE_NULL( mediacontrol_InternalException, "Cannot allocate snapshot" );
}
p_input = libvlc_get_input_thread( self->p_media_player, &ex ); p_input = libvlc_get_input_thread( self->p_media_player, &ex );
if( ! p_input ) if( ! p_input )
{ {
...@@ -79,26 +85,49 @@ mediacontrol_snapshot( mediacontrol_Instance *self, ...@@ -79,26 +85,49 @@ mediacontrol_snapshot( mediacontrol_Instance *self,
{ {
RAISE_NULL( mediacontrol_InternalException, "No video output" ); RAISE_NULL( mediacontrol_InternalException, "No video output" );
} }
p_cache = vlc_object_create( p_input, sizeof( vlc_object_t ) );
if( p_cache == NULL ) /* TODO:
{ vlc_mutex_lock (&lock);
vlc_object_release( p_vout ); mutex_cleanup_push (&lock); // release the mutex in case of cancellation
vlc_object_release( p_input );
RAISE_NULL( mediacontrol_InternalException, "Out of memory" ); while (!foobar)
} vlc_cond_wait (&wait, &lock);
snprintf( path, 255, "object:%p", p_cache );
--- foobar is now true, do something about it here --
vlc_cleanup_run (); // release the mutex
*/
snprintf( path, 255, "object:%p", p_snapshot );
var_SetString( p_vout, "snapshot-path", path ); var_SetString( p_vout, "snapshot-path", path );
var_SetString( p_vout, "snapshot-format", "png" ); var_SetString( p_vout, "snapshot-format", "png" );
vlc_object_lock( p_cache ); vlc_mutex_init( &p_snapshot->p_mutex );
vlc_cond_init( &p_snapshot->p_condvar );
vlc_mutex_lock( &p_snapshot->p_mutex );
mutex_cleanup_push( &p_snapshot->p_mutex );
/* Use p_snapshot address as sentinel against spurious vlc_object_wait wakeups.
If a legitimate wakeup occurs, then p_snapshot->p_data will hold either
NULL (in case of error) or a pointer to valid data. */
p_snapshot->p_data = ( char* )p_snapshot;
vout_Control( p_vout, VOUT_SNAPSHOT ); vout_Control( p_vout, VOUT_SNAPSHOT );
while ( p_snapshot->p_data == ( char* )p_snapshot )
{
vlc_cond_wait( &p_snapshot->p_condvar, &p_snapshot->p_mutex );
}
vlc_cleanup_pop();
vlc_object_release( p_vout ); vlc_object_release( p_vout );
p_snapshot = ( snapshot_t* ) p_cache->p_private; vlc_mutex_unlock( &p_snapshot->p_mutex );
vlc_object_unlock( p_cache ); vlc_cond_destroy( &p_snapshot->p_condvar );
vlc_object_release( p_cache ); vlc_mutex_destroy( &p_snapshot->p_mutex );
if( p_snapshot ) if( p_snapshot->p_data )
{ {
/* Note: p_snapshot->p_data is directly used, not copied. Thus /* Note: p_snapshot->p_data is directly used, not copied. Thus
do not free it here. */ do not free it here. */
......
...@@ -638,13 +638,12 @@ int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic ) ...@@ -638,13 +638,12 @@ int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
*/ */
if( b_embedded_snapshot ) if( b_embedded_snapshot )
{ {
vlc_object_t* p_dest = p_obj;
block_t *p_block; block_t *p_block;
snapshot_t *p_snapshot; snapshot_t *p_snapshot = p_obj;
size_t i_size; size_t i_size;
vlc_object_lock( p_dest ); vlc_mutex_lock( &p_snapshot->p_mutex );
p_dest->p_private = NULL; p_snapshot->p_data = NULL;
/* Save the snapshot to a memory zone */ /* Save the snapshot to a memory zone */
p_block = image_Write( p_image, p_pic, &fmt_in, &fmt_out ); p_block = image_Write( p_image, p_pic, &fmt_in, &fmt_out );
...@@ -652,23 +651,11 @@ int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic ) ...@@ -652,23 +651,11 @@ int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
{ {
msg_Err( p_vout, "Could not get snapshot" ); msg_Err( p_vout, "Could not get snapshot" );
image_HandlerDelete( p_image ); image_HandlerDelete( p_image );
vlc_object_signal_unlocked( p_dest ); vlc_cond_signal( &p_snapshot->p_condvar );
vlc_object_unlock( p_dest ); vlc_mutex_unlock( &p_snapshot->p_mutex );
return VLC_EGENERIC; return VLC_EGENERIC;
} }
/* Copy the p_block data to a snapshot structure */
/* FIXME: get the timestamp */
p_snapshot = malloc( sizeof( snapshot_t ) );
if( !p_snapshot )
{
block_Release( p_block );
image_HandlerDelete( p_image );
vlc_object_signal_unlocked( p_dest );
vlc_object_unlock( p_dest );
return VLC_ENOMEM;
}
i_size = p_block->i_buffer; i_size = p_block->i_buffer;
p_snapshot->i_width = fmt_out.i_width; p_snapshot->i_width = fmt_out.i_width;
...@@ -679,21 +666,18 @@ int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic ) ...@@ -679,21 +666,18 @@ int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
if( !p_snapshot->p_data ) if( !p_snapshot->p_data )
{ {
block_Release( p_block ); block_Release( p_block );
free( p_snapshot );
image_HandlerDelete( p_image ); image_HandlerDelete( p_image );
vlc_object_signal_unlocked( p_dest ); vlc_cond_signal( &p_snapshot->p_condvar );
vlc_object_unlock( p_dest ); vlc_mutex_unlock( &p_snapshot->p_mutex );
return VLC_ENOMEM; return VLC_ENOMEM;
} }
memcpy( p_snapshot->p_data, p_block->p_buffer, p_block->i_buffer ); memcpy( p_snapshot->p_data, p_block->p_buffer, p_block->i_buffer );
p_dest->p_private = p_snapshot;
block_Release( p_block ); block_Release( p_block );
/* Unlock the object */ /* Unlock the object */
vlc_object_signal_unlocked( p_dest ); vlc_cond_signal( &p_snapshot->p_condvar );
vlc_object_unlock( p_dest ); vlc_mutex_unlock( &p_snapshot->p_mutex );
image_HandlerDelete( p_image ); image_HandlerDelete( p_image );
return VLC_SUCCESS; return VLC_SUCCESS;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment