* \brief abstraction for all consumer services
* \see mlt_consumer_s
*
- * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
+ * Copyright (C) 2003-2010 Ushodaya Enterprises Limited
* \author Charles Yates <charles.yates@pandora.be>
+ * \author Dan Dennedy <dan@dennedy.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
// Set the real_time preference
this->real_time = mlt_properties_get_int( properties, "real_time" );
+ // For worker threads implementation, buffer must be at least # threads
+ if ( abs( this->real_time ) > 1 && mlt_properties_get_int( properties, "buffer" ) <= abs( this->real_time ) )
+ mlt_properties_set_int( properties, "buffer", abs( this->real_time ) + 1 );
+
// Start the service
if ( this->start != NULL )
return this->start( this );
int64_t time_frame = 0;
int64_t time_process = 0;
int skip_next = 0;
- mlt_service lock_object = NULL;
if ( preview_off && preview_format != 0 )
this->format = preview_format;
// Get the first frame
frame = mlt_consumer_get_frame( this );
- // Get the lock object
- lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
-
- // Lock it
- if ( lock_object ) mlt_service_lock( lock_object );
-
// Get the image of the first frame
if ( !video_off )
{
mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
}
- // Unlock the lock object
- if ( lock_object ) mlt_service_unlock( lock_object );
-
// Mark as rendered
mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
height = mlt_properties_get_int( properties, "height" );
// Put the current frame into the queue
- pthread_mutex_lock( &this->mutex );
+ pthread_mutex_lock( &this->queue_mutex );
while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
- pthread_cond_wait( &this->cond, &this->mutex );
+ pthread_cond_wait( &this->queue_cond, &this->queue_mutex );
mlt_deque_push_back( this->queue, frame );
- pthread_cond_broadcast( &this->cond );
- pthread_mutex_unlock( &this->mutex );
+ pthread_cond_broadcast( &this->queue_cond );
+ pthread_mutex_unlock( &this->queue_mutex );
time_wait += time_difference( &ante );
if ( frame == NULL )
continue;
- // Attempt to fetch the lock object
- lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
-
// Increment the count
count ++;
- // Lock if there's a lock object
- if ( lock_object ) mlt_service_lock( lock_object );
-
// All non normal playback frames should be shown
if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
{
skip_next = 0;
// If we've reached an unacceptable level, reset everything
- if ( skipped > 5 )
+ if ( skipped > fps * 2 )
{
skipped = 0;
time_frame = 0;
if ( ( ( time_wait + time_frame + time_process ) / count ) > frame_duration )
skip_next = 1;
}
-
- // Unlock if there's a lock object
- if ( lock_object ) mlt_service_unlock( lock_object );
}
// Remove the last frame
return NULL;
}
+/** Locate the first unprocessed frame in the queue.
+ *
+ * When playing with realtime behavior, we do not use the true head, but
+ * rather an adjusted process_head. The process_head is adjusted based on
+ * the rate of frame-dropping or recovery from frame-dropping. The idea is
+ * that as the level of frame-dropping increases to move the process_head
+ * closer to the tail because the frames are not completing processing prior
+ * to their playout! Then, as frames are not dropped the process_head moves
+ * back closer to the head of the queue so that worker threads can work
+ * ahead of the playout point (queue head).
+ *
+ * \private \memberof mlt_consumer_s
+ * \param this a consumer
+ * \return an index into the queue
+ */
+
+static inline int first_unprocessed_frame( mlt_consumer this )
+{
+ int index = this->real_time <= 0 ? 0 : this->process_head;
+ while ( index < mlt_deque_count( this->queue ) && MLT_FRAME( mlt_deque_peek( this->queue, index ) )->is_processing )
+ index++;
+ return index;
+}
+
+/** The worker thread procedure for parallel processing frames.
+ *
+ * \private \memberof mlt_consumer_s
+ * \param arg a consumer
+ */
+
+static void *consumer_worker_thread( void *arg )
+{
+ // The argument is the consumer
+ mlt_consumer this = arg;
+
+ // Get the properties of the consumer
+ mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
+
+ // Get the width and height
+ int width = mlt_properties_get_int( properties, "width" );
+ int height = mlt_properties_get_int( properties, "height" );
+ mlt_image_format format = this->format;
+
+ // See if video is turned off
+ int video_off = mlt_properties_get_int( properties, "video_off" );
+ int preview_off = mlt_properties_get_int( properties, "preview_off" );
+ int preview_format = mlt_properties_get_int( properties, "preview_format" );
+
+ // General frame variable
+ mlt_frame frame = NULL;
+ uint8_t *image = NULL;
+
+ if ( preview_off && preview_format != 0 )
+ format = preview_format;
+
+ // Continue to read ahead
+ while ( this->ahead )
+ {
+ // Get the next unprocessed frame from the work queue
+ pthread_mutex_lock( &this->queue_mutex );
+ int index = first_unprocessed_frame( this );
+ while ( this->ahead && index >= mlt_deque_count( this->queue ) )
+ {
+ mlt_log_debug( MLT_CONSUMER_SERVICE(this), "waiting in worker index = %d queue count = %d\n",
+ index, mlt_deque_count( this->queue ) );
+ pthread_cond_wait( &this->queue_cond, &this->queue_mutex );
+ index = first_unprocessed_frame( this );
+ }
+ pthread_mutex_unlock( &this->queue_mutex );
+
+ // Mark the frame for processing
+ frame = mlt_deque_peek( this->queue, index );
+ if ( frame )
+ {
+ mlt_log_debug( MLT_CONSUMER_SERVICE(this), "worker processing index = %d frame %d queue count = %d\n",
+ index, mlt_frame_get_position(frame), mlt_deque_count( this->queue ) );
+ frame->is_processing = 1;
+ mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( frame ) );
+ }
+
+ // If there's no frame, we're probably stopped...
+ if ( frame == NULL )
+ continue;
+
+#ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
+ // All non normal playback frames should be shown
+ if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
+ mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
+#endif
+
+ // Get the image
+ if ( !video_off )
+ {
+ // Fetch width/height again
+ width = mlt_properties_get_int( properties, "width" );
+ height = mlt_properties_get_int( properties, "height" );
+ mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
+ mlt_frame_get_image( frame, &image, &format, &width, &height, 0 );
+ }
+ mlt_frame_close( frame );
+ mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
+
+ // Tell a waiting thread (non-realtime main consumer thread) that we are done.
+ pthread_mutex_lock( &this->done_mutex );
+ pthread_cond_broadcast( &this->done_cond );
+ pthread_mutex_unlock( &this->done_mutex );
+ }
+
+ return NULL;
+}
+
/** Start the read/render thread.
*
* \private \memberof mlt_consumer_s
// Create the frame queue
this->queue = mlt_deque_init( );
- // Create the mutex
- pthread_mutex_init( &this->mutex, NULL );
+ // Create the queue mutex
+ pthread_mutex_init( &this->queue_mutex, NULL );
// Create the condition
- pthread_cond_init( &this->cond, NULL );
+ pthread_cond_init( &this->queue_cond, NULL );
// Create the read ahead
if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "priority" ) )
}
}
+/** Start the worker threads.
+ *
+ * \private \memberof mlt_consumer_s
+ * \param this a consumer
+ */
+
+static void consumer_work_start( mlt_consumer this )
+{
+ int n = abs( this->real_time );
+ pthread_t thread;
+
+ // We're running now
+ this->ahead = 1;
+
+ // These keep track of the accelleration of frame dropping or recovery.
+ this->consecutive_dropped = 0;
+ this->consecutive_rendered = 0;
+
+ // This is the position in the queue from which to look for a frame to process.
+ // If we always start from the head, then we may likely not complete processing
+ // before the frame is played out.
+ this->process_head = 0;
+
+ // Create the queues
+ this->queue = mlt_deque_init();
+ this->worker_threads = mlt_deque_init();
+
+ // Create the mutexes
+ pthread_mutex_init( &this->queue_mutex, NULL );
+ pthread_mutex_init( &this->done_mutex, NULL );
+
+ // Create the conditions
+ pthread_cond_init( &this->queue_cond, NULL );
+ pthread_cond_init( &this->done_cond, NULL );
+
+ // Create the read ahead
+ if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "priority" ) )
+ {
+
+ struct sched_param priority;
+ pthread_attr_t thread_attributes;
+
+ priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this ), "priority" );
+ pthread_attr_init( &thread_attributes );
+ pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
+ pthread_attr_setschedparam( &thread_attributes, &priority );
+ pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
+ pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
+
+ while ( n-- )
+ {
+ if ( pthread_create( &thread, &thread_attributes, consumer_read_ahead_thread, this ) < 0 )
+ if ( pthread_create( &thread, NULL, consumer_read_ahead_thread, this ) == 0 )
+ mlt_deque_push_back( this->worker_threads, (void*) thread );
+ }
+ pthread_attr_destroy( &thread_attributes );
+ }
+
+ else
+ {
+ while ( n-- )
+ {
+ if ( pthread_create( &thread, NULL, consumer_worker_thread, this ) == 0 )
+ mlt_deque_push_back( this->worker_threads, (void*) thread );
+ }
+ }
+}
+
/** Stop the read/render thread.
*
* \private \memberof mlt_consumer_s
this->ahead = 0;
// Broadcast to the condition in case it's waiting
- pthread_mutex_lock( &this->mutex );
- pthread_cond_broadcast( &this->cond );
- pthread_mutex_unlock( &this->mutex );
+ pthread_mutex_lock( &this->queue_mutex );
+ pthread_cond_broadcast( &this->queue_cond );
+ pthread_mutex_unlock( &this->queue_mutex );
// Broadcast to the put condition in case it's waiting
pthread_mutex_lock( &this->put_mutex );
// Join the thread
pthread_join( this->ahead_thread, NULL );
- // Destroy the mutex
- pthread_mutex_destroy( &this->mutex );
+ // Destroy the frame queue mutex
+ pthread_mutex_destroy( &this->queue_mutex );
// Destroy the condition
- pthread_cond_destroy( &this->cond );
+ pthread_cond_destroy( &this->queue_cond );
// Wipe the queue
while ( mlt_deque_count( this->queue ) )
}
}
+/** Stop the worker threads.
+ *
+ * \private \memberof mlt_consumer_s
+ * \param this a consumer
+ */
+
+static void consumer_work_stop( mlt_consumer this )
+{
+ // Make sure we're running
+ if ( this->ahead )
+ {
+ // Inform thread to stop
+ this->ahead = 0;
+
+ // Broadcast to the queue condition in case it's waiting
+ pthread_mutex_lock( &this->queue_mutex );
+ pthread_cond_broadcast( &this->queue_cond );
+ pthread_mutex_unlock( &this->queue_mutex );
+
+ // Broadcast to the put condition in case it's waiting
+ pthread_mutex_lock( &this->put_mutex );
+ pthread_cond_broadcast( &this->put_cond );
+ pthread_mutex_unlock( &this->put_mutex );
+
+ // Join the threads
+ pthread_t thread;
+ while ( ( thread = (pthread_t) mlt_deque_pop_front( this->worker_threads ) ) )
+ pthread_join( thread, NULL );
+
+ // Destroy the mutexes
+ pthread_mutex_destroy( &this->queue_mutex );
+ pthread_mutex_destroy( &this->done_mutex );
+
+ // Destroy the conditions
+ pthread_cond_destroy( &this->queue_cond );
+ pthread_cond_destroy( &this->done_cond );
+
+ // Wipe the queues
+ while ( mlt_deque_count( this->queue ) )
+ mlt_frame_close( mlt_deque_pop_back( this->queue ) );
+
+ // Close the queues
+ mlt_deque_close( this->queue );
+ mlt_deque_close( this->worker_threads );
+ }
+}
+
/** Flush the read/render thread's buffer.
*
* \public \memberof mlt_consumer_s
{
if ( this->ahead )
{
- pthread_mutex_lock( &this->mutex );
+ pthread_mutex_lock( &this->queue_mutex );
while ( mlt_deque_count( this->queue ) )
mlt_frame_close( mlt_deque_pop_back( this->queue ) );
- pthread_cond_broadcast( &this->cond );
- pthread_mutex_unlock( &this->mutex );
+ pthread_cond_broadcast( &this->queue_cond );
+ pthread_mutex_unlock( &this->queue_mutex );
}
}
+/** Use multiple worker threads and a work queue.
+ */
+
+static mlt_frame worker_get_frame( mlt_consumer this, mlt_properties properties )
+{
+ // Frame to return
+ mlt_frame frame = NULL;
+
+ int size = abs( this->real_time );
+ int buffer = mlt_properties_get_int( properties, "buffer" );
+ // This is a heuristic to determine a suitable minimum buffer size for the number of threads.
+ int headroom = 2 + size * size;
+ buffer = buffer < headroom ? headroom : buffer;
+
+ // Start worker threads if not already started.
+ if ( ! this->ahead )
+ {
+ int prefill = mlt_properties_get_int( properties, "prefill" );
+ prefill = prefill > 0 && prefill < buffer ? prefill : buffer;
+
+ consumer_work_start( this );
+
+ // Fill the work queue.
+ int i = buffer;
+ while ( i-- )
+ {
+ frame = mlt_consumer_get_frame( this );
+ if ( frame )
+ {
+ pthread_mutex_lock( &this->queue_mutex );
+ mlt_deque_push_back( this->queue, frame );
+ pthread_cond_signal( &this->queue_cond );
+ pthread_mutex_unlock( &this->queue_mutex );
+ }
+ }
+
+ // Wait for prefill
+ while ( this->ahead && first_unprocessed_frame( this ) < prefill )
+ {
+ pthread_mutex_lock( &this->done_mutex );
+ pthread_cond_wait( &this->done_cond, &this->done_mutex );
+ pthread_mutex_unlock( &this->done_mutex );
+ }
+ this->process_head = size;
+ }
+
+ mlt_log_debug( MLT_CONSUMER_SERVICE(this), "size %d done count %d work count %d process_head %d\n",
+ size, first_unprocessed_frame( this ), mlt_deque_count( this->queue ), this->process_head );
+
+ // Feed the work queue
+ frame = mlt_consumer_get_frame( this );
+ if ( ! frame )
+ return frame;
+ pthread_mutex_lock( &this->queue_mutex );
+ mlt_deque_push_back( this->queue, frame );
+ pthread_cond_signal( &this->queue_cond );
+ pthread_mutex_unlock( &this->queue_mutex );
+
+ // Wait if not realtime.
+ while( this->ahead && this->real_time < 0 &&
+ ! mlt_properties_get_int( MLT_FRAME_PROPERTIES( MLT_FRAME( mlt_deque_peek_front( this->queue ) ) ), "rendered" ) )
+ {
+ pthread_mutex_lock( &this->done_mutex );
+ pthread_cond_wait( &this->done_cond, &this->done_mutex );
+ pthread_mutex_unlock( &this->done_mutex );
+ }
+
+ // Get the frame from the queue.
+ pthread_mutex_lock( &this->queue_mutex );
+ frame = mlt_deque_pop_front( this->queue );
+ pthread_mutex_unlock( &this->queue_mutex );
+
+ // Adapt the worker process head to the runtime conditions.
+ if ( this->real_time > 0 )
+ {
+ if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "rendered" ) )
+ {
+ this->consecutive_dropped = 0;
+ if ( this->process_head > size && this->consecutive_rendered >= this->process_head )
+ this->process_head--;
+ else
+ this->consecutive_rendered++;
+ }
+ else
+ {
+ this->consecutive_rendered = 0;
+ if ( this->process_head < buffer - size && this->consecutive_dropped > size )
+ this->process_head++;
+ else
+ this->consecutive_dropped++;
+ }
+ mlt_log_debug( MLT_CONSUMER_SERVICE(this), "dropped %d rendered %d process_head %d\n",
+ this->consecutive_dropped, this->consecutive_rendered, this->process_head );
+ }
+
+ return frame;
+}
+
/** Get the next frame from the producer connected to a consumer.
*
* Typically, one uses this instead of \p mlt_consumer_get_frame to make
mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
// Check if the user has requested real time or not
- if ( this->real_time )
+ if ( this->real_time > 1 || this->real_time < -1 )
+ {
+ // see above
+ return worker_get_frame( this, properties );
+ }
+ else if ( this->real_time == 1 || this->real_time == -1 )
{
int size = 1;
}
// Get frame from queue
- pthread_mutex_lock( &this->mutex );
+ pthread_mutex_lock( &this->queue_mutex );
while( this->ahead && mlt_deque_count( this->queue ) < size )
- pthread_cond_wait( &this->cond, &this->mutex );
+ pthread_cond_wait( &this->queue_cond, &this->queue_mutex );
frame = mlt_deque_pop_front( this->queue );
- pthread_cond_broadcast( &this->cond );
- pthread_mutex_unlock( &this->mutex );
+ pthread_cond_broadcast( &this->queue_cond );
+ pthread_mutex_unlock( &this->queue_mutex );
}
- else
+ else // real_time == 0
{
// Get the frame in non real time
frame = mlt_consumer_get_frame( this );
// Check if the user has requested real time or not and stop if necessary
mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping read_ahead\n" );
- if ( mlt_properties_get_int( properties, "real_time" ) )
+ if ( abs( this->real_time ) == 1 )
consumer_read_ahead_stop( this );
+ else if ( abs( this->real_time ) > 1 )
+ consumer_work_stop( this );
// Kill the test card
mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );