3 * \brief abstraction for all consumer services
6 * Copyright (C) 2003-2010 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
8 * \author Dan Dennedy <dan@dennedy.org>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "mlt_consumer.h"
26 #include "mlt_factory.h"
27 #include "mlt_producer.h"
28 #include "mlt_frame.h"
29 #include "mlt_profile.h"
37 /** Define this if you want an automatic deinterlace (if necessary) when the
38 * consumer's producer is not running at normal speed.
40 #undef DEINTERLACE_ON_NOT_NORMAL_SPEED
42 /** This is not the ideal place for this, but it is needed by VDPAU as well.
44 pthread_mutex_t mlt_sdl_mutex = PTHREAD_MUTEX_INITIALIZER;
46 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service self, void **args );
47 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service self, void **args );
48 static void mlt_consumer_property_changed( mlt_properties owner, mlt_consumer self, char *name );
49 static void apply_profile_properties( mlt_consumer self, mlt_profile profile, mlt_properties properties );
50 static void on_consumer_frame_show( mlt_properties owner, mlt_consumer self, mlt_frame frame );
52 /** Initialize a consumer service.
54 * \public \memberof mlt_consumer_s
55 * \param self the consumer to initialize
56 * \param child a pointer to the object for the subclass
57 * \param profile the \p mlt_profile_s to use (optional but recommended,
58 * uses the environment variable MLT if self is NULL)
59 * \return true if there was an error
62 int mlt_consumer_init( mlt_consumer self, void *child, mlt_profile profile )
65 memset( self, 0, sizeof( struct mlt_consumer_s ) );
67 error = mlt_service_init( &self->parent, self );
70 // Get the properties from the service
71 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
73 // Apply profile to properties
74 if ( profile == NULL )
76 // Normally the application creates the profile and controls its lifetime
77 // This is the fallback exception handling
78 profile = mlt_profile_init( NULL );
79 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
80 mlt_properties_set_data( properties, "_profile", profile, 0, (mlt_destructor)mlt_profile_close, NULL );
82 apply_profile_properties( self, profile, properties );
84 // Default rescaler for all consumers
85 mlt_properties_set( properties, "rescale", "bilinear" );
87 // Default read ahead buffer size
88 mlt_properties_set_int( properties, "buffer", 25 );
89 mlt_properties_set_int( properties, "drop_max", 5 );
91 // Default audio frequency and channels
92 mlt_properties_set_int( properties, "frequency", 48000 );
93 mlt_properties_set_int( properties, "channels", 2 );
95 // Default of all consumers is real time
96 mlt_properties_set_int( properties, "real_time", 1 );
98 // Default to environment test card
99 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
101 // Hmm - default all consumers to yuv422 :-/
102 self->format = mlt_image_yuv422;
104 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
105 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
106 mlt_events_register( properties, "consumer-stopped", NULL );
107 mlt_events_listen( properties, self, "consumer-frame-show", ( mlt_listener )on_consumer_frame_show );
109 // Register a property-changed listener to handle the profile property -
110 // subsequent properties can override the profile
111 self->event_listener = mlt_events_listen( properties, self, "property-changed", ( mlt_listener )mlt_consumer_property_changed );
113 // Create the push mutex and condition
114 pthread_mutex_init( &self->put_mutex, NULL );
115 pthread_cond_init( &self->put_cond, NULL );
121 /** Convert the profile into properties on the consumer.
123 * \private \memberof mlt_consumer_s
124 * \param self a consumer
125 * \param profile a profile
126 * \param properties a properties list (typically, the consumer's)
129 static void apply_profile_properties( mlt_consumer self, mlt_profile profile, mlt_properties properties )
131 mlt_event_block( self->event_listener );
132 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
133 mlt_properties_set_int( properties, "frame_rate_num", profile->frame_rate_num );
134 mlt_properties_set_int( properties, "frame_rate_den", profile->frame_rate_den );
135 mlt_properties_set_int( properties, "width", profile->width );
136 mlt_properties_set_int( properties, "height", profile->height );
137 mlt_properties_set_int( properties, "progressive", profile->progressive );
138 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
139 mlt_properties_set_int( properties, "sample_aspect_num", profile->sample_aspect_num );
140 mlt_properties_set_int( properties, "sample_aspect_den", profile->sample_aspect_den );
141 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
142 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
143 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
144 mlt_properties_set_int( properties, "colorspace", profile->colorspace );
145 mlt_event_unblock( self->event_listener );
148 /** The property-changed event listener
150 * \private \memberof mlt_consumer_s
151 * \param owner the events object
152 * \param self the consumer
153 * \param name the name of the property that changed
156 static void mlt_consumer_property_changed( mlt_properties owner, mlt_consumer self, char *name )
158 if ( !strcmp( name, "mlt_profile" ) )
161 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
163 // Get the current profile
164 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
166 // Load the new profile
167 mlt_profile new_profile = mlt_profile_init( mlt_properties_get( properties, name ) );
172 if ( profile != NULL )
174 free( profile->description );
175 memcpy( profile, new_profile, sizeof( struct mlt_profile_s ) );
176 profile->description = strdup( new_profile->description );
177 mlt_profile_close( new_profile );
181 profile = new_profile;
184 // Apply to properties
185 apply_profile_properties( self, profile, properties );
188 else if ( !strcmp( name, "frame_rate_num" ) )
190 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
191 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
194 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
195 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
198 else if ( !strcmp( name, "frame_rate_den" ) )
200 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
201 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
204 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
205 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
208 else if ( !strcmp( name, "width" ) )
210 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
211 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
213 profile->width = mlt_properties_get_int( properties, "width" );
215 else if ( !strcmp( name, "height" ) )
217 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
218 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
220 profile->height = mlt_properties_get_int( properties, "height" );
222 else if ( !strcmp( name, "progressive" ) )
224 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
225 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
227 profile->progressive = mlt_properties_get_int( properties, "progressive" );
229 else if ( !strcmp( name, "sample_aspect_num" ) )
231 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
232 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
233 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
235 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
237 else if ( !strcmp( name, "sample_aspect_den" ) )
239 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
240 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
241 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
243 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
245 else if ( !strcmp( name, "display_aspect_num" ) )
247 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
248 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
251 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
252 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
255 else if ( !strcmp( name, "display_aspect_den" ) )
257 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
258 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
261 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
262 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
265 else if ( !strcmp( name, "colorspace" ) )
267 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
268 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
270 profile->colorspace = mlt_properties_get_int( properties, "colorspace" );
274 /** The transmitter for the consumer-frame-show event
276 * Invokes the listener.
278 * \private \memberof mlt_consumer_s
279 * \param listener a function pointer that will be invoked
280 * \param owner the events object that will be passed to \p listener
281 * \param self a service that will be passed to \p listener
282 * \param args an array of pointers - the first entry is passed as a string to \p listener
285 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
287 if ( listener != NULL )
288 listener( owner, self, ( mlt_frame )args[ 0 ] );
291 /** The transmitter for the consumer-frame-render event
293 * Invokes the listener.
295 * \private \memberof mlt_consumer_s
296 * \param listener a function pointer that will be invoked
297 * \param owner the events object that will be passed to \p listener
298 * \param self a service that will be passed to \p listener
299 * \param args an array of pointers - the first entry is passed as a string to \p listener
302 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
304 if ( listener != NULL )
305 listener( owner, self, ( mlt_frame )args[ 0 ] );
308 /** A listener on the consumer-frame-show event
310 * Saves the position of the frame shown.
312 * \private \memberof mlt_consumer_s
313 * \param owner the events object
314 * \param consumer the consumer on which this event occurred
315 * \param frame the frame that was shown
318 static void on_consumer_frame_show( mlt_properties owner, mlt_consumer consumer, mlt_frame frame )
321 consumer->position = mlt_frame_get_position( frame );
324 /** Create a new consumer.
326 * \public \memberof mlt_consumer_s
327 * \param profile a profile (optional, but recommended)
328 * \return a new consumer
331 mlt_consumer mlt_consumer_new( mlt_profile profile )
333 // Create the memory for the structure
334 mlt_consumer self = malloc( sizeof( struct mlt_consumer_s ) );
338 mlt_consumer_init( self, NULL, profile );
344 /** Get the parent service object.
346 * \public \memberof mlt_consumer_s
347 * \param self a consumer
348 * \return the parent service class
349 * \see MLT_CONSUMER_SERVICE
352 mlt_service mlt_consumer_service( mlt_consumer self )
354 return self != NULL ? &self->parent : NULL;
357 /** Get the consumer properties.
359 * \public \memberof mlt_consumer_s
360 * \param self a consumer
361 * \return the consumer's properties list
362 * \see MLT_CONSUMER_PROPERTIES
365 mlt_properties mlt_consumer_properties( mlt_consumer self )
367 return self != NULL ? MLT_SERVICE_PROPERTIES( &self->parent ) : NULL;
370 /** Connect the consumer to the producer.
372 * \public \memberof mlt_consumer_s
373 * \param self a consumer
374 * \param producer a producer
375 * \return > 0 warning, == 0 success, < 0 serious error,
376 * 1 = this service does not accept input,
377 * 2 = the producer is invalid,
378 * 3 = the producer is already registered with this consumer
381 int mlt_consumer_connect( mlt_consumer self, mlt_service producer )
383 return mlt_service_connect_producer( &self->parent, producer, 0 );
386 /** Start the consumer.
388 * \public \memberof mlt_consumer_s
389 * \param self a consumer
390 * \return true if there was an error
393 int mlt_consumer_start( mlt_consumer self )
395 if ( !mlt_consumer_is_stopped( self ) )
398 // Stop listening to the property-changed event
399 mlt_event_block( self->event_listener );
402 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
404 // Determine if there's a test card producer
405 char *test_card = mlt_properties_get( properties, "test_card" );
407 // Just to make sure nothing is hanging around...
409 self->put_active = 1;
412 if ( test_card != NULL )
414 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
416 // Create a test card producer
417 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
418 mlt_producer producer = mlt_factory_producer( profile, NULL, test_card );
420 // Do we have a producer
421 if ( producer != NULL )
423 // Test card should loop I guess...
424 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
425 //mlt_producer_set_speed( producer, 0 );
426 //mlt_producer_set_in_and_out( producer, 0, 0 );
428 // Set the test card on the consumer
429 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
435 // Allow the hash table to speed things up
436 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
439 // Set the frame duration in microseconds for the frame-dropping heuristic
440 int frame_duration = 1000000 / mlt_properties_get_int( properties, "frame_rate_num" ) *
441 mlt_properties_get_int( properties, "frame_rate_den" );
442 mlt_properties_set_int( properties, "frame_duration", frame_duration );
444 // Check and run an ante command
445 if ( mlt_properties_get( properties, "ante" ) )
446 if ( system( mlt_properties_get( properties, "ante" ) ) == -1 )
447 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "ante" ) );
449 // Set the real_time preference
450 self->real_time = mlt_properties_get_int( properties, "real_time" );
452 // For worker threads implementation, buffer must be at least # threads
453 if ( abs( self->real_time ) > 1 && mlt_properties_get_int( properties, "buffer" ) <= abs( self->real_time ) )
454 mlt_properties_set_int( properties, "_buffer", abs( self->real_time ) + 1 );
457 if ( self->start != NULL )
458 return self->start( self );
463 /** An alternative method to feed frames into the consumer.
465 * Only valid if the consumer itself is not connected.
467 * \public \memberof mlt_consumer_s
468 * \param self a consumer
469 * \param frame a frame
470 * \return true (ignore self for now)
473 int mlt_consumer_put_frame( mlt_consumer self, mlt_frame frame )
477 // Get the service assoicated to the consumer
478 mlt_service service = MLT_CONSUMER_SERVICE( self );
480 if ( mlt_service_producer( service ) == NULL )
484 pthread_mutex_lock( &self->put_mutex );
485 while ( self->put_active && self->put != NULL )
487 gettimeofday( &now, NULL );
488 tm.tv_sec = now.tv_sec + 1;
489 tm.tv_nsec = now.tv_usec * 1000;
490 pthread_cond_timedwait( &self->put_cond, &self->put_mutex, &tm );
492 if ( self->put_active && self->put == NULL )
495 mlt_frame_close( frame );
496 pthread_cond_broadcast( &self->put_cond );
497 pthread_mutex_unlock( &self->put_mutex );
501 mlt_frame_close( frame );
507 /** Protected method for consumer to get frames from connected service
509 * \public \memberof mlt_consumer_s
510 * \param self a consumer
514 mlt_frame mlt_consumer_get_frame( mlt_consumer self )
517 mlt_frame frame = NULL;
519 // Get the service assoicated to the consumer
520 mlt_service service = MLT_CONSUMER_SERVICE( self );
522 // Get the consumer properties
523 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
526 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
530 pthread_mutex_lock( &self->put_mutex );
531 while ( self->put_active && self->put == NULL )
533 gettimeofday( &now, NULL );
534 tm.tv_sec = now.tv_sec + 1;
535 tm.tv_nsec = now.tv_usec * 1000;
536 pthread_cond_timedwait( &self->put_cond, &self->put_mutex, &tm );
540 pthread_cond_broadcast( &self->put_cond );
541 pthread_mutex_unlock( &self->put_mutex );
543 mlt_service_apply_filters( service, frame, 0 );
545 else if ( mlt_service_producer( service ) != NULL )
547 mlt_service_get_frame( service, &frame, 0 );
551 frame = mlt_frame_init( service );
556 // Get the frame properties
557 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
559 // Get the test card producer
560 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
562 // Attach the test frame producer to it.
563 if ( test_card != NULL )
564 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
566 // Attach the rescale property
567 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
569 // Aspect ratio and other jiggery pokery
570 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
571 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
572 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
573 mlt_properties_set_int( frame_properties, "consumer_tff", mlt_properties_get_int( properties, "top_field_first" ) );
580 /** Compute the time difference between now and a time value.
582 * \private \memberof mlt_consumer_s
583 * \param time1 a time value to be compared against now
584 * \return the difference in microseconds
587 static inline long time_difference( struct timeval *time1 )
589 struct timeval time2;
590 time2.tv_sec = time1->tv_sec;
591 time2.tv_usec = time1->tv_usec;
592 gettimeofday( time1, NULL );
593 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
596 /** The thread procedure for asynchronously pulling frames through the service
597 * network connected to a consumer.
599 * \private \memberof mlt_consumer_s
600 * \param arg a consumer
603 static void *consumer_read_ahead_thread( void *arg )
605 // The argument is the consumer
606 mlt_consumer self = arg;
608 // Get the properties of the consumer
609 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
611 // Get the width and height
612 int width = mlt_properties_get_int( properties, "width" );
613 int height = mlt_properties_get_int( properties, "height" );
615 // See if video is turned off
616 int video_off = mlt_properties_get_int( properties, "video_off" );
617 int preview_off = mlt_properties_get_int( properties, "preview_off" );
618 int preview_format = mlt_properties_get_int( properties, "preview_format" );
620 // Get the audio settings
621 mlt_audio_format afmt = mlt_audio_s16;
623 double fps = mlt_properties_get_double( properties, "fps" );
624 int channels = mlt_properties_get_int( properties, "channels" );
625 int frequency = mlt_properties_get_int( properties, "frequency" );
629 // See if audio is turned off
630 int audio_off = mlt_properties_get_int( properties, "audio_off" );
632 // Get the maximum size of the buffer
633 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
635 // General frame variable
636 mlt_frame frame = NULL;
637 uint8_t *image = NULL;
642 // Average time for get_frame and get_image
645 int64_t time_process = 0;
647 mlt_position pos = 0;
648 mlt_position start_pos = 0;
649 mlt_position last_pos = 0;
650 int frame_duration = mlt_properties_get_int( properties, "frame_duration" );
651 int drop_max = mlt_properties_get_int( properties, "drop_max" );
653 if ( preview_off && preview_format != 0 )
654 self->format = preview_format;
656 // Get the first frame
657 frame = mlt_consumer_get_frame( self );
661 // Get the image of the first frame
664 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
665 mlt_frame_get_image( frame, &image, &self->format, &width, &height, 0 );
670 samples = mlt_sample_calculator( fps, frequency, counter++ );
671 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
675 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
676 last_pos = start_pos = pos = mlt_frame_get_position( frame );
679 // Get the starting time (can ignore the times above)
680 gettimeofday( &ante, NULL );
682 // Continue to read ahead
683 while ( self->ahead )
685 // Put the current frame into the queue
686 pthread_mutex_lock( &self->queue_mutex );
687 while( self->ahead && mlt_deque_count( self->queue ) >= buffer )
688 pthread_cond_wait( &self->queue_cond, &self->queue_mutex );
689 mlt_deque_push_back( self->queue, frame );
690 pthread_cond_broadcast( &self->queue_cond );
691 pthread_mutex_unlock( &self->queue_mutex );
693 // Get the next frame
694 frame = mlt_consumer_get_frame( self );
696 // If there's no frame, we're probably stopped...
699 pos = mlt_frame_get_position( frame );
701 // Increment the counter used for averaging processing cost
704 // All non-normal playback frames should be shown
705 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
707 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
708 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
710 // Indicate seeking or trick-play
714 // If skip flag not set or frame-dropping disabled
715 if ( !skip_next || self->real_time == -1 )
719 // Reset width/height - could have been changed by previous mlt_frame_get_image
720 width = mlt_properties_get_int( properties, "width" );
721 height = mlt_properties_get_int( properties, "height" );
724 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
725 mlt_frame_get_image( frame, &image, &self->format, &width, &height, 0 );
728 // Indicate the rendered image is available.
729 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
731 // Reset consecutively-skipped counter
734 else // Skip image processing
736 // Increment the number of consecutively-skipped frames
739 // If too many (1 sec) consecutively-skipped frames
740 if ( skipped > drop_max )
742 // Reset cost tracker
745 mlt_log_verbose( self, "too many frames dropped - forcing next frame\n" );
749 // Always process audio
752 samples = mlt_sample_calculator( fps, frequency, counter++ );
753 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
756 // Get the time to process this frame
757 int64_t time_current = time_difference( &ante );
759 // If the current time is not suddenly some large amount
760 if ( time_current < time_process / count * 20 || !time_process || count < 5 )
762 // Accumulate the cost for processing this frame
763 time_process += time_current;
767 mlt_log_debug( self, "current %"PRId64" threshold %"PRId64" count %d\n",
768 time_current, (int64_t) (time_process / count * 20), count );
769 // Ignore the cost of this frame's time
773 // Determine if we started, resumed, or seeked
774 if ( pos != last_pos + 1 )
778 // Do not skip the first 20% of buffer at start, resume, or seek
779 if ( pos - start_pos <= buffer / 5 + 1 )
781 // Reset cost tracker
789 // Only consider skipping if the buffer level is low (or really small)
790 if ( mlt_deque_count( self->queue ) <= buffer / 5 + 1 )
792 // Skip next frame if average cost exceeds frame duration.
793 if ( time_process / count > frame_duration )
796 mlt_log_debug( self, "avg usec %"PRId64" (%"PRId64"/%d) duration %d\n",
797 time_process/count, time_process, count, frame_duration);
801 // Remove the last frame
802 mlt_frame_close( frame );
807 /** Locate the first unprocessed frame in the queue.
809 * When playing with realtime behavior, we do not use the true head, but
810 * rather an adjusted process_head. The process_head is adjusted based on
811 * the rate of frame-dropping or recovery from frame-dropping. The idea is
812 * that as the level of frame-dropping increases to move the process_head
813 * closer to the tail because the frames are not completing processing prior
814 * to their playout! Then, as frames are not dropped the process_head moves
815 * back closer to the head of the queue so that worker threads can work
816 * ahead of the playout point (queue head).
818 * \private \memberof mlt_consumer_s
819 * \param self a consumer
820 * \return an index into the queue
823 static inline int first_unprocessed_frame( mlt_consumer self )
825 int index = self->real_time <= 0 ? 0 : self->process_head;
826 while ( index < mlt_deque_count( self->queue ) && MLT_FRAME( mlt_deque_peek( self->queue, index ) )->is_processing )
831 /** The worker thread procedure for parallel processing frames.
833 * \private \memberof mlt_consumer_s
834 * \param arg a consumer
837 static void *consumer_worker_thread( void *arg )
839 // The argument is the consumer
840 mlt_consumer self = arg;
842 // Get the properties of the consumer
843 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
845 // Get the width and height
846 int width = mlt_properties_get_int( properties, "width" );
847 int height = mlt_properties_get_int( properties, "height" );
848 mlt_image_format format = self->format;
850 // See if video is turned off
851 int video_off = mlt_properties_get_int( properties, "video_off" );
852 int preview_off = mlt_properties_get_int( properties, "preview_off" );
853 int preview_format = mlt_properties_get_int( properties, "preview_format" );
855 // General frame variable
856 mlt_frame frame = NULL;
857 uint8_t *image = NULL;
859 if ( preview_off && preview_format != 0 )
860 format = preview_format;
862 // Continue to read ahead
863 while ( self->ahead )
865 // Get the next unprocessed frame from the work queue
866 pthread_mutex_lock( &self->queue_mutex );
867 int index = first_unprocessed_frame( self );
868 while ( self->ahead && index >= mlt_deque_count( self->queue ) )
870 mlt_log_debug( MLT_CONSUMER_SERVICE(self), "waiting in worker index = %d queue count = %d\n",
871 index, mlt_deque_count( self->queue ) );
872 pthread_cond_wait( &self->queue_cond, &self->queue_mutex );
873 index = first_unprocessed_frame( self );
876 // Mark the frame for processing
877 frame = mlt_deque_peek( self->queue, index );
880 mlt_log_debug( MLT_CONSUMER_SERVICE(self), "worker processing index = %d frame %d queue count = %d\n",
881 index, mlt_frame_get_position(frame), mlt_deque_count( self->queue ) );
882 frame->is_processing = 1;
883 mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( frame ) );
885 pthread_mutex_unlock( &self->queue_mutex );
887 // If there's no frame, we're probably stopped...
891 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
892 // All non normal playback frames should be shown
893 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
894 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
900 // Fetch width/height again
901 width = mlt_properties_get_int( properties, "width" );
902 height = mlt_properties_get_int( properties, "height" );
903 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
904 mlt_frame_get_image( frame, &image, &format, &width, &height, 0 );
906 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
907 mlt_frame_close( frame );
909 // Tell a waiting thread (non-realtime main consumer thread) that we are done.
910 pthread_mutex_lock( &self->done_mutex );
911 pthread_cond_broadcast( &self->done_cond );
912 pthread_mutex_unlock( &self->done_mutex );
918 /** Start the read/render thread.
920 * \private \memberof mlt_consumer_s
921 * \param self a consumer
924 static void consumer_read_ahead_start( mlt_consumer self )
929 // Create the frame queue
930 self->queue = mlt_deque_init( );
932 // Create the queue mutex
933 pthread_mutex_init( &self->queue_mutex, NULL );
935 // Create the condition
936 pthread_cond_init( &self->queue_cond, NULL );
938 // Create the read ahead
939 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( self ), "priority" ) )
941 struct sched_param priority;
942 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( self ), "priority" );
943 pthread_attr_t thread_attributes;
944 pthread_attr_init( &thread_attributes );
945 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
946 pthread_attr_setschedparam( &thread_attributes, &priority );
947 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
948 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
949 if ( pthread_create( &self->ahead_thread, &thread_attributes, consumer_read_ahead_thread, self ) < 0 )
950 pthread_create( &self->ahead_thread, NULL, consumer_read_ahead_thread, self );
951 pthread_attr_destroy( &thread_attributes );
955 pthread_create( &self->ahead_thread, NULL, consumer_read_ahead_thread, self );
960 /** Start the worker threads.
962 * \private \memberof mlt_consumer_s
963 * \param self a consumer
966 static void consumer_work_start( mlt_consumer self )
968 int n = abs( self->real_time );
969 pthread_t *thread = calloc( 1, sizeof( pthread_t ) * n );
974 // These keep track of the accelleration of frame dropping or recovery.
975 self->consecutive_dropped = 0;
976 self->consecutive_rendered = 0;
978 // This is the position in the queue from which to look for a frame to process.
979 // If we always start from the head, then we may likely not complete processing
980 // before the frame is played out.
981 self->process_head = 0;
984 self->queue = mlt_deque_init();
985 self->worker_threads = mlt_deque_init();
987 // Create the mutexes
988 pthread_mutex_init( &self->queue_mutex, NULL );
989 pthread_mutex_init( &self->done_mutex, NULL );
991 // Create the conditions
992 pthread_cond_init( &self->queue_cond, NULL );
993 pthread_cond_init( &self->done_cond, NULL );
995 // Create the read ahead
996 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( self ), "priority" ) )
999 struct sched_param priority;
1000 pthread_attr_t thread_attributes;
1002 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( self ), "priority" );
1003 pthread_attr_init( &thread_attributes );
1004 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
1005 pthread_attr_setschedparam( &thread_attributes, &priority );
1006 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
1007 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
1011 if ( pthread_create( thread, &thread_attributes, consumer_worker_thread, self ) < 0 )
1012 if ( pthread_create( thread, NULL, consumer_worker_thread, self ) == 0 )
1013 mlt_deque_push_back( self->worker_threads, thread );
1016 pthread_attr_destroy( &thread_attributes );
1023 if ( pthread_create( thread, NULL, consumer_worker_thread, self ) == 0 )
1024 mlt_deque_push_back( self->worker_threads, thread );
1031 /** Stop the read/render thread.
1033 * \private \memberof mlt_consumer_s
1034 * \param self a consumer
1037 static void consumer_read_ahead_stop( mlt_consumer self )
1039 // Make sure we're running
1040 if ( self->started )
1042 // Inform thread to stop
1045 // Broadcast to the condition in case it's waiting
1046 pthread_mutex_lock( &self->queue_mutex );
1047 pthread_cond_broadcast( &self->queue_cond );
1048 pthread_mutex_unlock( &self->queue_mutex );
1050 // Broadcast to the put condition in case it's waiting
1051 pthread_mutex_lock( &self->put_mutex );
1052 pthread_cond_broadcast( &self->put_cond );
1053 pthread_mutex_unlock( &self->put_mutex );
1056 pthread_join( self->ahead_thread, NULL );
1059 // Destroy the frame queue mutex
1060 pthread_mutex_destroy( &self->queue_mutex );
1062 // Destroy the condition
1063 pthread_cond_destroy( &self->queue_cond );
1066 while ( mlt_deque_count( self->queue ) )
1067 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
1070 mlt_deque_close( self->queue );
1074 /** Stop the worker threads.
1076 * \private \memberof mlt_consumer_s
1077 * \param self a consumer
1080 static void consumer_work_stop( mlt_consumer self )
1082 // Make sure we're running
1083 if ( self->started )
1085 // Inform thread to stop
1088 // Broadcast to the queue condition in case it's waiting
1089 pthread_mutex_lock( &self->queue_mutex );
1090 pthread_cond_broadcast( &self->queue_cond );
1091 pthread_mutex_unlock( &self->queue_mutex );
1093 // Broadcast to the put condition in case it's waiting
1094 pthread_mutex_lock( &self->put_mutex );
1095 pthread_cond_broadcast( &self->put_cond );
1096 pthread_mutex_unlock( &self->put_mutex );
1098 // Broadcast to the done condition in case it's waiting
1099 pthread_mutex_lock( &self->done_mutex );
1100 pthread_cond_broadcast( &self->done_cond );
1101 pthread_mutex_unlock( &self->done_mutex );
1105 while ( ( thread = mlt_deque_pop_back( self->worker_threads ) ) )
1106 pthread_join( *thread, NULL );
1108 // Deallocate the array of threads
1112 // Indicate that worker threads no longer running
1115 // Destroy the mutexes
1116 pthread_mutex_destroy( &self->queue_mutex );
1117 pthread_mutex_destroy( &self->done_mutex );
1119 // Destroy the conditions
1120 pthread_cond_destroy( &self->queue_cond );
1121 pthread_cond_destroy( &self->done_cond );
1124 while ( mlt_deque_count( self->queue ) )
1125 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
1128 mlt_deque_close( self->queue );
1129 mlt_deque_close( self->worker_threads );
1133 /** Flush the read/render thread's buffer.
1135 * \public \memberof mlt_consumer_s
1136 * \param self a consumer
1139 void mlt_consumer_purge( mlt_consumer self )
1143 pthread_mutex_lock( &self->queue_mutex );
1144 while ( mlt_deque_count( self->queue ) )
1145 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
1146 pthread_cond_broadcast( &self->queue_cond );
1147 pthread_mutex_unlock( &self->queue_mutex );
1151 /** Use multiple worker threads and a work queue.
1154 static mlt_frame worker_get_frame( mlt_consumer self, mlt_properties properties )
1157 mlt_frame frame = NULL;
1159 double fps = mlt_properties_get_double( properties, "fps" );
1160 int threads = abs( self->real_time );
1161 int buffer = mlt_properties_get_int( properties, "_buffer" );
1162 buffer = buffer > 0 ? buffer : mlt_properties_get_int( properties, "buffer" );
1163 // This is a heuristic to determine a suitable minimum buffer size for the number of threads.
1164 int headroom = 2 + threads * threads;
1165 buffer = buffer < headroom ? headroom : buffer;
1167 // Start worker threads if not already started.
1168 if ( ! self->ahead )
1170 int prefill = mlt_properties_get_int( properties, "prefill" );
1171 prefill = prefill > 0 && prefill < buffer ? prefill : buffer;
1173 consumer_work_start( self );
1175 // Fill the work queue.
1177 while ( self->ahead && i-- )
1179 frame = mlt_consumer_get_frame( self );
1182 pthread_mutex_lock( &self->queue_mutex );
1183 mlt_deque_push_back( self->queue, frame );
1184 pthread_cond_signal( &self->queue_cond );
1185 pthread_mutex_unlock( &self->queue_mutex );
1190 while ( self->ahead && first_unprocessed_frame( self ) < prefill )
1192 pthread_mutex_lock( &self->done_mutex );
1193 pthread_cond_wait( &self->done_cond, &self->done_mutex );
1194 pthread_mutex_unlock( &self->done_mutex );
1196 self->process_head = threads;
1199 // mlt_log_verbose( MLT_CONSUMER_SERVICE(self), "size %d done count %d work count %d process_head %d\n",
1200 // threads, first_unprocessed_frame( self ), mlt_deque_count( self->queue ), self->process_head );
1202 // Feed the work queue
1203 while ( self->ahead && mlt_deque_count( self->queue ) < buffer )
1205 frame = mlt_consumer_get_frame( self );
1208 pthread_mutex_lock( &self->queue_mutex );
1209 mlt_deque_push_back( self->queue, frame );
1210 pthread_cond_signal( &self->queue_cond );
1211 pthread_mutex_unlock( &self->queue_mutex );
1214 // Wait if not realtime.
1215 mlt_frame head_frame = MLT_FRAME( mlt_deque_peek_front( self->queue ) );
1216 while ( self->ahead && self->real_time < 0 &&
1217 !( head_frame && mlt_properties_get_int( MLT_FRAME_PROPERTIES( head_frame ), "rendered" ) ) )
1219 pthread_mutex_lock( &self->done_mutex );
1220 pthread_cond_wait( &self->done_cond, &self->done_mutex );
1221 pthread_mutex_unlock( &self->done_mutex );
1224 // Get the frame from the queue.
1225 pthread_mutex_lock( &self->queue_mutex );
1226 frame = mlt_deque_pop_front( self->queue );
1227 pthread_mutex_unlock( &self->queue_mutex );
1229 // Adapt the worker process head to the runtime conditions.
1230 if ( self->real_time > 0 )
1232 if ( frame && mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "rendered" ) )
1234 self->consecutive_dropped = 0;
1235 if ( self->process_head > threads && self->consecutive_rendered >= self->process_head )
1236 self->process_head--;
1238 self->consecutive_rendered++;
1242 self->consecutive_rendered = 0;
1243 if ( self->process_head < buffer - threads && self->consecutive_dropped > threads )
1244 self->process_head++;
1246 self->consecutive_dropped++;
1248 // mlt_log_verbose( MLT_CONSUMER_SERVICE(self), "dropped %d rendered %d process_head %d\n",
1249 // self->consecutive_dropped, self->consecutive_rendered, self->process_head );
1251 // Check for too many consecutively dropped frames
1252 if ( self->consecutive_dropped > mlt_properties_get_int( properties, "drop_max" ) )
1254 int orig_buffer = mlt_properties_get_int( properties, "buffer" );
1255 int prefill = mlt_properties_get_int( properties, "prefill" );
1256 mlt_log_verbose( self, "too many frames dropped - " );
1258 // If using a default low-latency buffer level (SDL) and below the limit
1259 if ( ( orig_buffer == 1 || prefill == 1 ) && buffer < (threads + 1) * 10 )
1261 // Auto-scale the buffer to compensate
1262 mlt_log_verbose( self, "increasing buffer to %d\n", buffer + threads );
1263 mlt_properties_set_int( properties, "_buffer", buffer + threads );
1264 self->consecutive_dropped = fps / 2;
1268 // Tell the consumer to render it
1269 mlt_log_verbose( self, "forcing next frame\n" );
1270 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
1271 self->consecutive_dropped = 0;
1279 /** Get the next frame from the producer connected to a consumer.
1281 * Typically, one uses this instead of \p mlt_consumer_get_frame to make
1282 * the asynchronous/real-time behavior configurable at runtime.
1283 * You should close the frame returned from this when you are done with it.
1285 * \public \memberof mlt_consumer_s
1286 * \param self a consumer
1290 mlt_frame mlt_consumer_rt_frame( mlt_consumer self )
1293 mlt_frame frame = NULL;
1295 // Get the properties
1296 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
1298 // Check if the user has requested real time or not
1299 if ( self->real_time > 1 || self->real_time < -1 )
1302 return worker_get_frame( self, properties );
1304 else if ( self->real_time == 1 || self->real_time == -1 )
1308 // Is the read ahead running?
1309 if ( self->ahead == 0 )
1311 int buffer = mlt_properties_get_int( properties, "buffer" );
1312 int prefill = mlt_properties_get_int( properties, "prefill" );
1313 consumer_read_ahead_start( self );
1315 size = prefill > 0 && prefill < buffer ? prefill : buffer;
1318 // Get frame from queue
1319 pthread_mutex_lock( &self->queue_mutex );
1320 while( self->ahead && mlt_deque_count( self->queue ) < size )
1321 pthread_cond_wait( &self->queue_cond, &self->queue_mutex );
1322 frame = mlt_deque_pop_front( self->queue );
1323 pthread_cond_broadcast( &self->queue_cond );
1324 pthread_mutex_unlock( &self->queue_mutex );
1326 else // real_time == 0
1328 // Get the frame in non real time
1329 frame = mlt_consumer_get_frame( self );
1331 // This isn't true, but from the consumers perspective it is
1332 if ( frame != NULL )
1333 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
1339 /** Callback for the implementation to indicate a stopped condition.
1341 * \public \memberof mlt_consumer_s
1342 * \param self a consumer
1345 void mlt_consumer_stopped( mlt_consumer self )
1347 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( self ), "running", 0 );
1348 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-stopped", NULL );
1349 mlt_event_unblock( self->event_listener );
1352 /** Stop the consumer.
1354 * \public \memberof mlt_consumer_s
1355 * \param self a consumer
1356 * \return true if there was an error
1359 int mlt_consumer_stop( mlt_consumer self )
1361 // Get the properies
1362 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
1365 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping put waiting\n" );
1366 pthread_mutex_lock( &self->put_mutex );
1367 self->put_active = 0;
1368 pthread_cond_broadcast( &self->put_cond );
1369 pthread_mutex_unlock( &self->put_mutex );
1371 // Stop the consumer
1372 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping consumer\n" );
1374 // Cancel the read ahead threads
1376 if ( self->started )
1378 // Unblock the consumer calling mlt_consumer_rt_frame
1379 pthread_mutex_lock( &self->queue_mutex );
1380 pthread_cond_broadcast( &self->queue_cond );
1381 pthread_mutex_unlock( &self->queue_mutex );
1384 // Invoke the child callback
1385 if ( self->stop != NULL )
1388 // Check if the user has requested real time or not and stop if necessary
1389 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping read_ahead\n" );
1390 if ( abs( self->real_time ) == 1 )
1391 consumer_read_ahead_stop( self );
1392 else if ( abs( self->real_time ) > 1 )
1393 consumer_work_stop( self );
1395 // Kill the test card
1396 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
1398 // Check and run a post command
1399 if ( mlt_properties_get( properties, "post" ) )
1400 if (system( mlt_properties_get( properties, "post" ) ) == -1 )
1401 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "post" ) );
1403 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopped\n" );
1408 /** Determine if the consumer is stopped.
1410 * \public \memberof mlt_consumer_s
1411 * \param self a consumer
1412 * \return true if the consumer is stopped
1415 int mlt_consumer_is_stopped( mlt_consumer self )
1417 // Check if the consumer is stopped
1418 if ( self->is_stopped != NULL )
1419 return self->is_stopped( self );
1424 /** Close and destroy the consumer.
1426 * \public \memberof mlt_consumer_s
1427 * \param self a consumer
1430 void mlt_consumer_close( mlt_consumer self )
1432 if ( self != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( self ) ) <= 0 )
1434 // Get the childs close function
1435 void ( *consumer_close )( ) = self->close;
1437 if ( consumer_close )
1440 //mlt_consumer_stop( self );
1443 consumer_close( self );
1447 // Make sure it only gets called once
1448 self->parent.close = NULL;
1450 // Destroy the push mutex and condition
1451 pthread_mutex_destroy( &self->put_mutex );
1452 pthread_cond_destroy( &self->put_cond );
1454 mlt_service_close( &self->parent );
1459 /** Get the position of the last frame shown.
1461 * \public \memberof mlt_consumer_s
1462 * \param consumer a consumer
1463 * \return the position
1466 mlt_position mlt_consumer_position( mlt_consumer consumer )
1468 return consumer->position;