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 /** \brief private members of mlt_consumer */
52 mlt_image_format format;
54 pthread_t ahead_thread;
55 pthread_mutex_t queue_mutex;
56 pthread_cond_t queue_cond;
57 pthread_mutex_t put_mutex;
58 pthread_cond_t put_cond;
61 mlt_event event_listener;
62 mlt_position position;
65 /* additional fields added for the parallel work queue */
66 mlt_deque worker_threads;
67 pthread_mutex_t done_mutex;
68 pthread_cond_t done_cond;
69 int consecutive_dropped;
70 int consecutive_rendered;
73 pthread_t *threads; /**< used to deallocate all threads */
77 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service self, void **args );
78 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service self, void **args );
79 static void mlt_consumer_property_changed( mlt_properties owner, mlt_consumer self, char *name );
80 static void apply_profile_properties( mlt_consumer self, mlt_profile profile, mlt_properties properties );
81 static void on_consumer_frame_show( mlt_properties owner, mlt_consumer self, mlt_frame frame );
83 /** Initialize a consumer service.
85 * \public \memberof mlt_consumer_s
86 * \param self the consumer to initialize
87 * \param child a pointer to the object for the subclass
88 * \param profile the \p mlt_profile_s to use (optional but recommended,
89 * uses the environment variable MLT if self is NULL)
90 * \return true if there was an error
93 int mlt_consumer_init( mlt_consumer self, void *child, mlt_profile profile )
96 memset( self, 0, sizeof( struct mlt_consumer_s ) );
98 consumer_private *priv = self->local = calloc( 1, sizeof( consumer_private ) );
100 error = mlt_service_init( &self->parent, self );
103 // Get the properties from the service
104 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
106 // Apply profile to properties
107 if ( profile == NULL )
109 // Normally the application creates the profile and controls its lifetime
110 // This is the fallback exception handling
111 profile = mlt_profile_init( NULL );
112 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
113 mlt_properties_set_data( properties, "_profile", profile, 0, (mlt_destructor)mlt_profile_close, NULL );
115 apply_profile_properties( self, profile, properties );
117 // Default rescaler for all consumers
118 mlt_properties_set( properties, "rescale", "bilinear" );
120 // Default read ahead buffer size
121 mlt_properties_set_int( properties, "buffer", 25 );
122 mlt_properties_set_int( properties, "drop_max", 5 );
124 // Default audio frequency and channels
125 mlt_properties_set_int( properties, "frequency", 48000 );
126 mlt_properties_set_int( properties, "channels", 2 );
128 // Default of all consumers is real time
129 mlt_properties_set_int( properties, "real_time", 1 );
131 // Default to environment test card
132 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
134 // Hmm - default all consumers to yuv422 :-/
135 priv->format = mlt_image_yuv422;
136 mlt_properties_set( properties, "mlt_image_format", mlt_image_format_name( priv->format ) );
137 mlt_properties_set( properties, "mlt_audio_format", mlt_audio_format_name( mlt_audio_s16 ) );
139 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
140 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
141 mlt_events_register( properties, "consumer-thread-started", NULL );
142 mlt_events_register( properties, "consumer-thread-stopped", NULL );
143 mlt_events_register( properties, "consumer-stopped", NULL );
144 mlt_events_listen( properties, self, "consumer-frame-show", ( mlt_listener )on_consumer_frame_show );
146 // Register a property-changed listener to handle the profile property -
147 // subsequent properties can override the profile
148 priv->event_listener = mlt_events_listen( properties, self, "property-changed", ( mlt_listener )mlt_consumer_property_changed );
150 // Create the push mutex and condition
151 pthread_mutex_init( &priv->put_mutex, NULL );
152 pthread_cond_init( &priv->put_cond, NULL );
158 /** Convert the profile into properties on the consumer.
160 * \private \memberof mlt_consumer_s
161 * \param self a consumer
162 * \param profile a profile
163 * \param properties a properties list (typically, the consumer's)
166 static void apply_profile_properties( mlt_consumer self, mlt_profile profile, mlt_properties properties )
168 consumer_private *priv = self->local;
169 mlt_event_block( priv->event_listener );
170 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
171 mlt_properties_set_int( properties, "frame_rate_num", profile->frame_rate_num );
172 mlt_properties_set_int( properties, "frame_rate_den", profile->frame_rate_den );
173 mlt_properties_set_int( properties, "width", profile->width );
174 mlt_properties_set_int( properties, "height", profile->height );
175 mlt_properties_set_int( properties, "progressive", profile->progressive );
176 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
177 mlt_properties_set_int( properties, "sample_aspect_num", profile->sample_aspect_num );
178 mlt_properties_set_int( properties, "sample_aspect_den", profile->sample_aspect_den );
179 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
180 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
181 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
182 mlt_properties_set_int( properties, "colorspace", profile->colorspace );
183 mlt_event_unblock( priv->event_listener );
186 /** The property-changed event listener
188 * \private \memberof mlt_consumer_s
189 * \param owner the events object
190 * \param self the consumer
191 * \param name the name of the property that changed
194 static void mlt_consumer_property_changed( mlt_properties owner, mlt_consumer self, char *name )
196 if ( !strcmp( name, "mlt_profile" ) )
199 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
201 // Get the current profile
202 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
204 // Load the new profile
205 mlt_profile new_profile = mlt_profile_init( mlt_properties_get( properties, name ) );
210 if ( profile != NULL )
212 free( profile->description );
213 memcpy( profile, new_profile, sizeof( struct mlt_profile_s ) );
214 profile->description = strdup( new_profile->description );
218 profile = new_profile;
221 // Apply to properties
222 apply_profile_properties( self, profile, properties );
223 mlt_profile_close( new_profile );
226 else if ( !strcmp( name, "frame_rate_num" ) )
228 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
229 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
232 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
233 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
236 else if ( !strcmp( name, "frame_rate_den" ) )
238 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
239 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
242 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
243 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
246 else if ( !strcmp( name, "width" ) )
248 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
249 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
251 profile->width = mlt_properties_get_int( properties, "width" );
253 else if ( !strcmp( name, "height" ) )
255 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
256 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
258 profile->height = mlt_properties_get_int( properties, "height" );
260 else if ( !strcmp( name, "progressive" ) )
262 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
263 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
265 profile->progressive = mlt_properties_get_int( properties, "progressive" );
267 else if ( !strcmp( name, "sample_aspect_num" ) )
269 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
270 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
273 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
274 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
277 else if ( !strcmp( name, "sample_aspect_den" ) )
279 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
280 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
283 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
284 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
287 else if ( !strcmp( name, "display_aspect_num" ) )
289 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
290 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
293 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
294 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
297 else if ( !strcmp( name, "display_aspect_den" ) )
299 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
300 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
303 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
304 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
307 else if ( !strcmp( name, "colorspace" ) )
309 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
310 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
312 profile->colorspace = mlt_properties_get_int( properties, "colorspace" );
316 /** The transmitter for the consumer-frame-show event
318 * Invokes the listener.
320 * \private \memberof mlt_consumer_s
321 * \param listener a function pointer that will be invoked
322 * \param owner the events object that will be passed to \p listener
323 * \param self a service that will be passed to \p listener
324 * \param args an array of pointers - the first entry is passed as a frame to \p listener
327 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
329 if ( listener != NULL )
330 listener( owner, self, ( mlt_frame )args[ 0 ] );
333 /** The transmitter for the consumer-frame-render event
335 * Invokes the listener.
337 * \private \memberof mlt_consumer_s
338 * \param listener a function pointer that will be invoked
339 * \param owner the events object that will be passed to \p listener
340 * \param self a service that will be passed to \p listener
341 * \param args an array of pointers - the first entry is passed as a frame to \p listener
344 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
346 if ( listener != NULL )
347 listener( owner, self, ( mlt_frame )args[ 0 ] );
350 /** A listener on the consumer-frame-show event
352 * Saves the position of the frame shown.
354 * \private \memberof mlt_consumer_s
355 * \param owner the events object
356 * \param consumer the consumer on which this event occurred
357 * \param frame the frame that was shown
360 static void on_consumer_frame_show( mlt_properties owner, mlt_consumer consumer, mlt_frame frame )
363 ( ( consumer_private*) consumer->local )->position = mlt_frame_get_position( frame );
366 /** Create a new consumer.
368 * \public \memberof mlt_consumer_s
369 * \param profile a profile (optional, but recommended)
370 * \return a new consumer
373 mlt_consumer mlt_consumer_new( mlt_profile profile )
375 // Create the memory for the structure
376 mlt_consumer self = malloc( sizeof( struct mlt_consumer_s ) );
379 if ( self != NULL && mlt_consumer_init( self, NULL, profile ) == 0 )
391 /** Get the parent service object.
393 * \public \memberof mlt_consumer_s
394 * \param self a consumer
395 * \return the parent service class
396 * \see MLT_CONSUMER_SERVICE
399 mlt_service mlt_consumer_service( mlt_consumer self )
401 return self != NULL ? &self->parent : NULL;
404 /** Get the consumer properties.
406 * \public \memberof mlt_consumer_s
407 * \param self a consumer
408 * \return the consumer's properties list
409 * \see MLT_CONSUMER_PROPERTIES
412 mlt_properties mlt_consumer_properties( mlt_consumer self )
414 return self != NULL ? MLT_SERVICE_PROPERTIES( &self->parent ) : NULL;
417 /** Connect the consumer to the producer.
419 * \public \memberof mlt_consumer_s
420 * \param self a consumer
421 * \param producer a producer
422 * \return > 0 warning, == 0 success, < 0 serious error,
423 * 1 = this service does not accept input,
424 * 2 = the producer is invalid,
425 * 3 = the producer is already registered with this consumer
428 int mlt_consumer_connect( mlt_consumer self, mlt_service producer )
430 return mlt_service_connect_producer( &self->parent, producer, 0 );
433 /** Start the consumer.
435 * \public \memberof mlt_consumer_s
436 * \param self a consumer
437 * \return true if there was an error
440 int mlt_consumer_start( mlt_consumer self )
442 if ( !mlt_consumer_is_stopped( self ) )
445 consumer_private *priv = self->local;
447 // Stop listening to the property-changed event
448 mlt_event_block( priv->event_listener );
451 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
453 // Determine if there's a test card producer
454 char *test_card = mlt_properties_get( properties, "test_card" );
456 // Just to make sure nothing is hanging around...
457 pthread_mutex_lock( &priv->put_mutex );
459 priv->put_active = 1;
460 pthread_mutex_unlock( &priv->put_mutex );
463 if ( test_card != NULL )
465 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
467 // Create a test card producer
468 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
469 mlt_producer producer = mlt_factory_producer( profile, NULL, test_card );
471 // Do we have a producer
472 if ( producer != NULL )
474 // Test card should loop I guess...
475 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
476 //mlt_producer_set_speed( producer, 0 );
477 //mlt_producer_set_in_and_out( producer, 0, 0 );
479 // Set the test card on the consumer
480 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
486 // Allow the hash table to speed things up
487 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
490 // The profile could have changed between a stop and a restart.
491 apply_profile_properties( self, mlt_service_profile( MLT_CONSUMER_SERVICE(self) ), properties );
493 // Set the frame duration in microseconds for the frame-dropping heuristic
494 int frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
495 int frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
496 int frame_duration = 0;
498 if ( frame_rate_num && frame_rate_den )
500 frame_duration = 1000000 / frame_rate_num * frame_rate_den;
503 mlt_properties_set_int( properties, "frame_duration", frame_duration );
505 // Check and run an ante command
506 if ( mlt_properties_get( properties, "ante" ) )
507 if ( system( mlt_properties_get( properties, "ante" ) ) == -1 )
508 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "ante" ) );
510 // Set the real_time preference
511 priv->real_time = mlt_properties_get_int( properties, "real_time" );
513 // For worker threads implementation, buffer must be at least # threads
514 if ( abs( priv->real_time ) > 1 && mlt_properties_get_int( properties, "buffer" ) <= abs( priv->real_time ) )
515 mlt_properties_set_int( properties, "_buffer", abs( priv->real_time ) + 1 );
517 // Get the image format to use for rendering threads
518 const char* format = mlt_properties_get( properties, "mlt_image_format" );
521 if ( !strcmp( format, "rgb24" ) )
522 priv->format = mlt_image_rgb24;
523 else if ( !strcmp( format, "rgb24a" ) )
524 priv->format = mlt_image_rgb24a;
525 else if ( !strcmp( format, "yuv420p" ) )
526 priv->format = mlt_image_yuv420p;
527 else if ( !strcmp( format, "none" ) )
528 priv->format = mlt_image_none;
529 else if ( !strcmp( format, "glsl" ) )
530 priv->format = mlt_image_glsl_texture;
532 priv->format = mlt_image_yuv422;
536 if ( self->start != NULL )
537 return self->start( self );
542 /** An alternative method to feed frames into the consumer.
544 * Only valid if the consumer itself is not connected.
546 * \public \memberof mlt_consumer_s
547 * \param self a consumer
548 * \param frame a frame
549 * \return true (ignore self for now)
552 int mlt_consumer_put_frame( mlt_consumer self, mlt_frame frame )
556 // Get the service assoicated to the consumer
557 mlt_service service = MLT_CONSUMER_SERVICE( self );
559 if ( mlt_service_producer( service ) == NULL )
563 consumer_private *priv = self->local;
565 pthread_mutex_lock( &priv->put_mutex );
566 while ( priv->put_active && priv->put != NULL )
568 gettimeofday( &now, NULL );
569 tm.tv_sec = now.tv_sec + 1;
570 tm.tv_nsec = now.tv_usec * 1000;
571 pthread_cond_timedwait( &priv->put_cond, &priv->put_mutex, &tm );
573 if ( priv->put_active && priv->put == NULL )
576 mlt_frame_close( frame );
577 pthread_cond_broadcast( &priv->put_cond );
578 pthread_mutex_unlock( &priv->put_mutex );
582 mlt_frame_close( frame );
588 /** Protected method for consumer to get frames from connected service
590 * \public \memberof mlt_consumer_s
591 * \param self a consumer
595 mlt_frame mlt_consumer_get_frame( mlt_consumer self )
598 mlt_frame frame = NULL;
600 // Get the service assoicated to the consumer
601 mlt_service service = MLT_CONSUMER_SERVICE( self );
603 // Get the consumer properties
604 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
607 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
611 consumer_private *priv = self->local;
613 pthread_mutex_lock( &priv->put_mutex );
614 while ( priv->put_active && priv->put == NULL )
616 gettimeofday( &now, NULL );
617 tm.tv_sec = now.tv_sec + 1;
618 tm.tv_nsec = now.tv_usec * 1000;
619 pthread_cond_timedwait( &priv->put_cond, &priv->put_mutex, &tm );
623 pthread_cond_broadcast( &priv->put_cond );
624 pthread_mutex_unlock( &priv->put_mutex );
626 mlt_service_apply_filters( service, frame, 0 );
628 else if ( mlt_service_producer( service ) != NULL )
630 mlt_service_get_frame( service, &frame, 0 );
634 frame = mlt_frame_init( service );
639 // Get the frame properties
640 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
642 // Get the test card producer
643 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
645 // Attach the test frame producer to it.
646 if ( test_card != NULL )
647 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
649 // Pass along the interpolation and deinterlace options
650 // TODO: get rid of consumer_deinterlace and use profile.progressive
651 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
652 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
653 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
654 mlt_properties_set_int( frame_properties, "consumer_tff", mlt_properties_get_int( properties, "top_field_first" ) );
661 /** Compute the time difference between now and a time value.
663 * \private \memberof mlt_consumer_s
664 * \param time1 a time value to be compared against now
665 * \return the difference in microseconds
668 static inline long time_difference( struct timeval *time1 )
670 struct timeval time2;
671 time2.tv_sec = time1->tv_sec;
672 time2.tv_usec = time1->tv_usec;
673 gettimeofday( time1, NULL );
674 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
677 /** The thread procedure for asynchronously pulling frames through the service
678 * network connected to a consumer.
680 * \private \memberof mlt_consumer_s
681 * \param arg a consumer
684 static void *consumer_read_ahead_thread( void *arg )
686 // The argument is the consumer
687 mlt_consumer self = arg;
688 consumer_private *priv = self->local;
690 // Get the properties of the consumer
691 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
693 // Get the width and height
694 int width = mlt_properties_get_int( properties, "width" );
695 int height = mlt_properties_get_int( properties, "height" );
697 // See if video is turned off
698 int video_off = mlt_properties_get_int( properties, "video_off" );
699 int preview_off = mlt_properties_get_int( properties, "preview_off" );
700 int preview_format = mlt_properties_get_int( properties, "preview_format" );
702 // Get the audio settings
703 mlt_audio_format afmt = mlt_audio_s16;
704 const char *format = mlt_properties_get( properties, "mlt_audio_format" );
707 if ( !strcmp( format, "none" ) )
708 afmt = mlt_audio_none;
709 else if ( !strcmp( format, "s32" ) )
710 afmt = mlt_audio_s32;
711 else if ( !strcmp( format, "s32le" ) )
712 afmt = mlt_audio_s32le;
713 else if ( !strcmp( format, "float" ) )
714 afmt = mlt_audio_float;
715 else if ( !strcmp( format, "f32le" ) )
716 afmt = mlt_audio_f32le;
717 else if ( !strcmp( format, "u8" ) )
721 double fps = mlt_properties_get_double( properties, "fps" );
722 int channels = mlt_properties_get_int( properties, "channels" );
723 int frequency = mlt_properties_get_int( properties, "frequency" );
727 // See if audio is turned off
728 int audio_off = mlt_properties_get_int( properties, "audio_off" );
730 // Get the maximum size of the buffer
731 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
733 // General frame variable
734 mlt_frame frame = NULL;
735 uint8_t *image = NULL;
740 // Average time for get_frame and get_image
743 int64_t time_process = 0;
745 mlt_position pos = 0;
746 mlt_position start_pos = 0;
747 mlt_position last_pos = 0;
748 int frame_duration = mlt_properties_get_int( properties, "frame_duration" );
749 int drop_max = mlt_properties_get_int( properties, "drop_max" );
751 if ( preview_off && preview_format != 0 )
752 priv->format = preview_format;
754 mlt_events_fire( properties, "consumer-thread-started", NULL );
756 // Get the first frame
757 frame = mlt_consumer_get_frame( self );
761 // Get the image of the first frame
764 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
765 mlt_frame_get_image( frame, &image, &priv->format, &width, &height, 0 );
770 samples = mlt_sample_calculator( fps, frequency, counter++ );
771 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
775 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
776 last_pos = start_pos = pos = mlt_frame_get_position( frame );
779 // Get the starting time (can ignore the times above)
780 gettimeofday( &ante, NULL );
782 // Continue to read ahead
783 while ( priv->ahead )
785 // Put the current frame into the queue
786 pthread_mutex_lock( &priv->queue_mutex );
787 while( priv->ahead && mlt_deque_count( priv->queue ) >= buffer )
788 pthread_cond_wait( &priv->queue_cond, &priv->queue_mutex );
789 if ( priv->is_purge )
791 mlt_frame_close( frame );
796 mlt_deque_push_back( priv->queue, frame );
798 pthread_cond_broadcast( &priv->queue_cond );
799 pthread_mutex_unlock( &priv->queue_mutex );
801 // Get the next frame
802 frame = mlt_consumer_get_frame( self );
804 // If there's no frame, we're probably stopped...
807 pos = mlt_frame_get_position( frame );
809 // Increment the counter used for averaging processing cost
812 // All non-normal playback frames should be shown
813 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
815 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
816 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
818 // Indicate seeking or trick-play
822 // If skip flag not set or frame-dropping disabled
823 if ( !skip_next || priv->real_time == -1 )
827 // Reset width/height - could have been changed by previous mlt_frame_get_image
828 width = mlt_properties_get_int( properties, "width" );
829 height = mlt_properties_get_int( properties, "height" );
832 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
833 mlt_frame_get_image( frame, &image, &priv->format, &width, &height, 0 );
836 // Indicate the rendered image is available.
837 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
839 // Reset consecutively-skipped counter
842 else // Skip image processing
844 // Increment the number of consecutively-skipped frames
847 // If too many (1 sec) consecutively-skipped frames
848 if ( skipped > drop_max )
850 // Reset cost tracker
853 mlt_log_verbose( self, "too many frames dropped - forcing next frame\n" );
857 // Always process audio
860 samples = mlt_sample_calculator( fps, frequency, counter++ );
861 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
864 // Get the time to process this frame
865 int64_t time_current = time_difference( &ante );
867 // If the current time is not suddenly some large amount
868 if ( time_current < time_process / count * 20 || !time_process || count < 5 )
870 // Accumulate the cost for processing this frame
871 time_process += time_current;
875 mlt_log_debug( self, "current %"PRId64" threshold %"PRId64" count %d\n",
876 time_current, (int64_t) (time_process / count * 20), count );
877 // Ignore the cost of this frame's time
881 // Determine if we started, resumed, or seeked
882 if ( pos != last_pos + 1 )
886 // Do not skip the first 20% of buffer at start, resume, or seek
887 if ( pos - start_pos <= buffer / 5 + 1 )
889 // Reset cost tracker
897 // Only consider skipping if the buffer level is low (or really small)
898 if ( mlt_deque_count( priv->queue ) <= buffer / 5 + 1 )
900 // Skip next frame if average cost exceeds frame duration.
901 if ( time_process / count > frame_duration )
904 mlt_log_debug( self, "avg usec %"PRId64" (%"PRId64"/%d) duration %d\n",
905 time_process/count, time_process, count, frame_duration);
909 // Remove the last frame
910 mlt_frame_close( frame );
911 mlt_events_fire( properties, "consumer-thread-stopped", NULL );
916 /** Locate the first unprocessed frame in the queue.
918 * When playing with realtime behavior, we do not use the true head, but
919 * rather an adjusted process_head. The process_head is adjusted based on
920 * the rate of frame-dropping or recovery from frame-dropping. The idea is
921 * that as the level of frame-dropping increases to move the process_head
922 * closer to the tail because the frames are not completing processing prior
923 * to their playout! Then, as frames are not dropped the process_head moves
924 * back closer to the head of the queue so that worker threads can work
925 * ahead of the playout point (queue head).
927 * \private \memberof mlt_consumer_s
928 * \param self a consumer
929 * \return an index into the queue
932 static inline int first_unprocessed_frame( mlt_consumer self )
934 consumer_private *priv = self->local;
935 int index = priv->real_time <= 0 ? 0 : priv->process_head;
936 while ( index < mlt_deque_count( priv->queue ) && MLT_FRAME( mlt_deque_peek( priv->queue, index ) )->is_processing )
941 /** The worker thread procedure for parallel processing frames.
943 * \private \memberof mlt_consumer_s
944 * \param arg a consumer
947 static void *consumer_worker_thread( void *arg )
949 // The argument is the consumer
950 mlt_consumer self = arg;
951 consumer_private *priv = self->local;
953 // Get the properties of the consumer
954 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
956 // Get the width and height
957 int width = mlt_properties_get_int( properties, "width" );
958 int height = mlt_properties_get_int( properties, "height" );
959 mlt_image_format format = priv->format;
961 // See if video is turned off
962 int video_off = mlt_properties_get_int( properties, "video_off" );
963 int preview_off = mlt_properties_get_int( properties, "preview_off" );
964 int preview_format = mlt_properties_get_int( properties, "preview_format" );
966 // General frame variable
967 mlt_frame frame = NULL;
968 uint8_t *image = NULL;
970 if ( preview_off && preview_format != 0 )
971 format = preview_format;
973 mlt_events_fire( properties, "consumer-thread-started", NULL );
975 // Continue to read ahead
976 while ( priv->ahead )
978 // Get the next unprocessed frame from the work queue
979 pthread_mutex_lock( &priv->queue_mutex );
980 int index = first_unprocessed_frame( self );
981 while ( priv->ahead && index >= mlt_deque_count( priv->queue ) )
983 mlt_log_debug( MLT_CONSUMER_SERVICE(self), "waiting in worker index = %d queue count = %d\n",
984 index, mlt_deque_count( priv->queue ) );
985 pthread_cond_wait( &priv->queue_cond, &priv->queue_mutex );
986 index = first_unprocessed_frame( self );
989 // Mark the frame for processing
990 frame = mlt_deque_peek( priv->queue, index );
993 mlt_log_debug( MLT_CONSUMER_SERVICE(self), "worker processing index = %d frame " MLT_POSITION_FMT " queue count = %d\n",
994 index, mlt_frame_get_position(frame), mlt_deque_count( priv->queue ) );
995 frame->is_processing = 1;
996 mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( frame ) );
998 pthread_mutex_unlock( &priv->queue_mutex );
1000 // If there's no frame, we're probably stopped...
1001 if ( frame == NULL )
1004 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
1005 // All non normal playback frames should be shown
1006 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
1007 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
1013 // Fetch width/height again
1014 width = mlt_properties_get_int( properties, "width" );
1015 height = mlt_properties_get_int( properties, "height" );
1016 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
1017 mlt_frame_get_image( frame, &image, &format, &width, &height, 0 );
1019 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
1020 mlt_frame_close( frame );
1022 // Tell a waiting thread (non-realtime main consumer thread) that we are done.
1023 pthread_mutex_lock( &priv->done_mutex );
1024 pthread_cond_broadcast( &priv->done_cond );
1025 pthread_mutex_unlock( &priv->done_mutex );
1027 mlt_events_fire( properties, "consumer-thread-stopped", NULL );
1032 /** Start the read/render thread.
1034 * \private \memberof mlt_consumer_s
1035 * \param self a consumer
1038 static void consumer_read_ahead_start( mlt_consumer self )
1040 consumer_private *priv = self->local;
1042 // We're running now
1045 // Create the frame queue
1046 priv->queue = mlt_deque_init( );
1048 // Create the queue mutex
1049 pthread_mutex_init( &priv->queue_mutex, NULL );
1051 // Create the condition
1052 pthread_cond_init( &priv->queue_cond, NULL );
1054 // Create the read ahead
1055 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( self ), "priority" ) )
1057 struct sched_param priority;
1058 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( self ), "priority" );
1059 pthread_attr_t thread_attributes;
1060 pthread_attr_init( &thread_attributes );
1061 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
1062 pthread_attr_setschedparam( &thread_attributes, &priority );
1063 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
1064 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
1065 if ( pthread_create( &priv->ahead_thread, &thread_attributes, consumer_read_ahead_thread, self ) < 0 )
1066 pthread_create( &priv->ahead_thread, NULL, consumer_read_ahead_thread, self );
1067 pthread_attr_destroy( &thread_attributes );
1071 pthread_create( &priv->ahead_thread, NULL, consumer_read_ahead_thread, self );
1076 /** Start the worker threads.
1078 * \private \memberof mlt_consumer_s
1079 * \param self a consumer
1082 static void consumer_work_start( mlt_consumer self )
1084 consumer_private *priv = self->local;
1085 int n = abs( priv->real_time );
1086 pthread_t *thread = calloc( 1, sizeof( pthread_t ) * n );
1088 // We're running now
1090 priv->threads = thread;
1092 // These keep track of the accelleration of frame dropping or recovery.
1093 priv->consecutive_dropped = 0;
1094 priv->consecutive_rendered = 0;
1096 // This is the position in the queue from which to look for a frame to process.
1097 // If we always start from the head, then we may likely not complete processing
1098 // before the frame is played out.
1099 priv->process_head = 0;
1101 // Create the queues
1102 priv->queue = mlt_deque_init();
1103 priv->worker_threads = mlt_deque_init();
1105 // Create the mutexes
1106 pthread_mutex_init( &priv->queue_mutex, NULL );
1107 pthread_mutex_init( &priv->done_mutex, NULL );
1109 // Create the conditions
1110 pthread_cond_init( &priv->queue_cond, NULL );
1111 pthread_cond_init( &priv->done_cond, NULL );
1113 // Create the read ahead
1114 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( self ), "priority" ) )
1117 struct sched_param priority;
1118 pthread_attr_t thread_attributes;
1120 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( self ), "priority" );
1121 pthread_attr_init( &thread_attributes );
1122 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
1123 pthread_attr_setschedparam( &thread_attributes, &priority );
1124 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
1125 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
1129 if ( pthread_create( thread, &thread_attributes, consumer_worker_thread, self ) < 0 )
1130 if ( pthread_create( thread, NULL, consumer_worker_thread, self ) == 0 )
1131 mlt_deque_push_back( priv->worker_threads, thread );
1134 pthread_attr_destroy( &thread_attributes );
1141 if ( pthread_create( thread, NULL, consumer_worker_thread, self ) == 0 )
1142 mlt_deque_push_back( priv->worker_threads, thread );
1149 /** Stop the read/render thread.
1151 * \private \memberof mlt_consumer_s
1152 * \param self a consumer
1155 static void consumer_read_ahead_stop( mlt_consumer self )
1157 consumer_private *priv = self->local;
1159 // Make sure we're running
1160 // TODO improve support for atomic ops in general (see libavutil/atomic.h)
1161 #ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
1162 if ( __sync_val_compare_and_swap( &priv->started, 1, 0 ) )
1165 if ( priv->started )
1169 // Inform thread to stop
1172 // Broadcast to the condition in case it's waiting
1173 pthread_mutex_lock( &priv->queue_mutex );
1174 pthread_cond_broadcast( &priv->queue_cond );
1175 pthread_mutex_unlock( &priv->queue_mutex );
1177 // Broadcast to the put condition in case it's waiting
1178 pthread_mutex_lock( &priv->put_mutex );
1179 pthread_cond_broadcast( &priv->put_cond );
1180 pthread_mutex_unlock( &priv->put_mutex );
1183 pthread_join( priv->ahead_thread, NULL );
1185 // Destroy the frame queue mutex
1186 pthread_mutex_destroy( &priv->queue_mutex );
1188 // Destroy the condition
1189 pthread_cond_destroy( &priv->queue_cond );
1192 while ( mlt_deque_count( priv->queue ) )
1193 mlt_frame_close( mlt_deque_pop_back( priv->queue ) );
1196 mlt_deque_close( priv->queue );
1200 /** Stop the worker threads.
1202 * \private \memberof mlt_consumer_s
1203 * \param self a consumer
1206 static void consumer_work_stop( mlt_consumer self )
1208 consumer_private *priv = self->local;
1210 // Make sure we're running
1211 #ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
1212 if ( __sync_val_compare_and_swap( &priv->started, 1, 0 ) )
1215 if ( priv->started )
1219 // Inform thread to stop
1222 // Broadcast to the queue condition in case it's waiting
1223 pthread_mutex_lock( &priv->queue_mutex );
1224 pthread_cond_broadcast( &priv->queue_cond );
1225 pthread_mutex_unlock( &priv->queue_mutex );
1227 // Broadcast to the put condition in case it's waiting
1228 pthread_mutex_lock( &priv->put_mutex );
1229 pthread_cond_broadcast( &priv->put_cond );
1230 pthread_mutex_unlock( &priv->put_mutex );
1232 // Broadcast to the done condition in case it's waiting
1233 pthread_mutex_lock( &priv->done_mutex );
1234 pthread_cond_broadcast( &priv->done_cond );
1235 pthread_mutex_unlock( &priv->done_mutex );
1239 while ( ( thread = mlt_deque_pop_back( priv->worker_threads ) ) )
1240 pthread_join( *thread, NULL );
1242 // Deallocate the array of threads
1243 if ( priv->threads )
1244 free( priv->threads );
1246 // Destroy the mutexes
1247 pthread_mutex_destroy( &priv->queue_mutex );
1248 pthread_mutex_destroy( &priv->done_mutex );
1250 // Destroy the conditions
1251 pthread_cond_destroy( &priv->queue_cond );
1252 pthread_cond_destroy( &priv->done_cond );
1255 while ( mlt_deque_count( priv->queue ) )
1256 mlt_frame_close( mlt_deque_pop_back( priv->queue ) );
1259 mlt_deque_close( priv->queue );
1260 mlt_deque_close( priv->worker_threads );
1264 /** Flush the read/render thread's buffer.
1266 * \public \memberof mlt_consumer_s
1267 * \param self a consumer
1270 void mlt_consumer_purge( mlt_consumer self )
1274 consumer_private *priv = self->local;
1276 pthread_mutex_lock( &priv->put_mutex );
1278 mlt_frame_close( priv->put );
1281 pthread_cond_broadcast( &priv->put_cond );
1282 pthread_mutex_unlock( &priv->put_mutex );
1284 if ( priv->started && priv->real_time )
1285 pthread_mutex_lock( &priv->queue_mutex );
1288 self->purge( self );
1290 while ( priv->started && mlt_deque_count( priv->queue ) )
1291 mlt_frame_close( mlt_deque_pop_back( priv->queue ) );
1292 if ( priv->started && priv->real_time )
1295 pthread_cond_broadcast( &priv->queue_cond );
1296 pthread_mutex_unlock( &priv->queue_mutex );
1297 if ( abs( priv->real_time ) > 1 )
1299 pthread_mutex_lock( &priv->done_mutex );
1300 pthread_cond_broadcast( &priv->done_cond );
1301 pthread_mutex_unlock( &priv->done_mutex );
1305 pthread_mutex_lock( &priv->put_mutex );
1307 mlt_frame_close( priv->put );
1310 pthread_cond_broadcast( &priv->put_cond );
1311 pthread_mutex_unlock( &priv->put_mutex );
1315 /** Use multiple worker threads and a work queue.
1318 static mlt_frame worker_get_frame( mlt_consumer self, mlt_properties properties )
1321 mlt_frame frame = NULL;
1322 consumer_private *priv = self->local;
1323 double fps = mlt_properties_get_double( properties, "fps" );
1324 int threads = abs( priv->real_time );
1325 int buffer = mlt_properties_get_int( properties, "_buffer" );
1326 buffer = buffer > 0 ? buffer : mlt_properties_get_int( properties, "buffer" );
1327 // This is a heuristic to determine a suitable minimum buffer size for the number of threads.
1328 int headroom = 2 + threads * threads;
1329 buffer = buffer < headroom ? headroom : buffer;
1331 // Start worker threads if not already started.
1332 if ( ! priv->ahead )
1334 int prefill = mlt_properties_get_int( properties, "prefill" );
1335 prefill = prefill > 0 && prefill < buffer ? prefill : buffer;
1337 consumer_work_start( self );
1339 // Fill the work queue.
1341 while ( priv->ahead && i-- )
1343 frame = mlt_consumer_get_frame( self );
1346 pthread_mutex_lock( &priv->queue_mutex );
1347 mlt_deque_push_back( priv->queue, frame );
1348 pthread_cond_signal( &priv->queue_cond );
1349 pthread_mutex_unlock( &priv->queue_mutex );
1354 while ( priv->ahead && first_unprocessed_frame( self ) < prefill )
1356 pthread_mutex_lock( &priv->done_mutex );
1357 pthread_cond_wait( &priv->done_cond, &priv->done_mutex );
1358 pthread_mutex_unlock( &priv->done_mutex );
1360 priv->process_head = threads;
1363 // mlt_log_verbose( MLT_CONSUMER_SERVICE(self), "size %d done count %d work count %d process_head %d\n",
1364 // threads, first_unprocessed_frame( self ), mlt_deque_count( priv->queue ), priv->process_head );
1366 // Feed the work queue
1367 while ( priv->ahead && mlt_deque_count( priv->queue ) < buffer )
1369 frame = mlt_consumer_get_frame( self );
1372 pthread_mutex_lock( &priv->queue_mutex );
1373 mlt_deque_push_back( priv->queue, frame );
1374 pthread_cond_signal( &priv->queue_cond );
1375 pthread_mutex_unlock( &priv->queue_mutex );
1379 // Wait if not realtime.
1380 while ( priv->ahead && priv->real_time < 0 && !priv->is_purge &&
1381 !( mlt_properties_get_int( MLT_FRAME_PROPERTIES( MLT_FRAME( mlt_deque_peek_front( priv->queue ) ) ), "rendered" ) ) )
1383 pthread_mutex_lock( &priv->done_mutex );
1384 pthread_cond_wait( &priv->done_cond, &priv->done_mutex );
1385 pthread_mutex_unlock( &priv->done_mutex );
1388 // Get the frame from the queue.
1389 pthread_mutex_lock( &priv->queue_mutex );
1390 frame = mlt_deque_pop_front( priv->queue );
1391 pthread_mutex_unlock( &priv->queue_mutex );
1397 // Adapt the worker process head to the runtime conditions.
1398 if ( priv->real_time > 0 )
1400 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "rendered" ) )
1402 priv->consecutive_dropped = 0;
1403 if ( priv->process_head > threads && priv->consecutive_rendered >= priv->process_head )
1404 priv->process_head--;
1406 priv->consecutive_rendered++;
1410 priv->consecutive_rendered = 0;
1411 if ( priv->process_head < buffer - threads && priv->consecutive_dropped > threads )
1412 priv->process_head++;
1414 priv->consecutive_dropped++;
1416 // mlt_log_verbose( MLT_CONSUMER_SERVICE(self), "dropped %d rendered %d process_head %d\n",
1417 // priv->consecutive_dropped, priv->consecutive_rendered, priv->process_head );
1419 // Check for too many consecutively dropped frames
1420 if ( priv->consecutive_dropped > mlt_properties_get_int( properties, "drop_max" ) )
1422 int orig_buffer = mlt_properties_get_int( properties, "buffer" );
1423 int prefill = mlt_properties_get_int( properties, "prefill" );
1424 mlt_log_verbose( self, "too many frames dropped - " );
1426 // If using a default low-latency buffer level (SDL) and below the limit
1427 if ( ( orig_buffer == 1 || prefill == 1 ) && buffer < (threads + 1) * 10 )
1429 // Auto-scale the buffer to compensate
1430 mlt_log_verbose( self, "increasing buffer to %d\n", buffer + threads );
1431 mlt_properties_set_int( properties, "_buffer", buffer + threads );
1432 priv->consecutive_dropped = fps / 2;
1436 // Tell the consumer to render it
1437 mlt_log_verbose( self, "forcing next frame\n" );
1438 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
1439 priv->consecutive_dropped = 0;
1443 if ( priv->is_purge ) {
1445 mlt_frame_close( frame );
1451 /** Get the next frame from the producer connected to a consumer.
1453 * Typically, one uses this instead of \p mlt_consumer_get_frame to make
1454 * the asynchronous/real-time behavior configurable at runtime.
1455 * You should close the frame returned from this when you are done with it.
1457 * \public \memberof mlt_consumer_s
1458 * \param self a consumer
1462 mlt_frame mlt_consumer_rt_frame( mlt_consumer self )
1465 mlt_frame frame = NULL;
1467 // Get the properties
1468 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
1469 consumer_private *priv = self->local;
1471 // Check if the user has requested real time or not
1472 if ( priv->real_time > 1 || priv->real_time < -1 )
1475 return worker_get_frame( self, properties );
1477 else if ( priv->real_time == 1 || priv->real_time == -1 )
1481 // Is the read ahead running?
1482 if ( priv->ahead == 0 )
1484 int buffer = mlt_properties_get_int( properties, "buffer" );
1485 int prefill = mlt_properties_get_int( properties, "prefill" );
1486 consumer_read_ahead_start( self );
1488 size = prefill > 0 && prefill < buffer ? prefill : buffer;
1491 // Get frame from queue
1492 pthread_mutex_lock( &priv->queue_mutex );
1493 while( priv->ahead && mlt_deque_count( priv->queue ) < size )
1494 pthread_cond_wait( &priv->queue_cond, &priv->queue_mutex );
1495 frame = mlt_deque_pop_front( priv->queue );
1496 pthread_cond_broadcast( &priv->queue_cond );
1497 pthread_mutex_unlock( &priv->queue_mutex );
1499 else // real_time == 0
1504 mlt_events_fire( properties, "consumer-thread-started", NULL );
1506 // Get the frame in non real time
1507 frame = mlt_consumer_get_frame( self );
1509 // This isn't true, but from the consumers perspective it is
1510 if ( frame != NULL )
1511 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
1517 /** Callback for the implementation to indicate a stopped condition.
1519 * \public \memberof mlt_consumer_s
1520 * \param self a consumer
1523 void mlt_consumer_stopped( mlt_consumer self )
1525 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( self ), "running", 0 );
1526 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-stopped", NULL );
1527 mlt_event_unblock( ( ( consumer_private* ) self->local )->event_listener );
1530 /** Stop the consumer.
1532 * \public \memberof mlt_consumer_s
1533 * \param self a consumer
1534 * \return true if there was an error
1537 int mlt_consumer_stop( mlt_consumer self )
1539 // Get the properies
1540 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
1541 consumer_private *priv = self->local;
1544 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping put waiting\n" );
1545 pthread_mutex_lock( &priv->put_mutex );
1546 priv->put_active = 0;
1547 pthread_cond_broadcast( &priv->put_cond );
1548 pthread_mutex_unlock( &priv->put_mutex );
1550 // Stop the consumer
1551 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping consumer\n" );
1553 // Cancel the read ahead threads
1555 if ( priv->started )
1557 // Unblock the consumer calling mlt_consumer_rt_frame
1558 pthread_mutex_lock( &priv->queue_mutex );
1559 pthread_cond_broadcast( &priv->queue_cond );
1560 pthread_mutex_unlock( &priv->queue_mutex );
1563 // Invoke the child callback
1564 if ( self->stop != NULL )
1567 // Check if the user has requested real time or not and stop if necessary
1568 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping read_ahead\n" );
1569 if ( abs( priv->real_time ) == 1 )
1570 consumer_read_ahead_stop( self );
1571 else if ( abs( priv->real_time ) > 1 )
1572 consumer_work_stop( self );
1574 // Kill the test card
1575 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
1577 // Check and run a post command
1578 if ( mlt_properties_get( properties, "post" ) )
1579 if (system( mlt_properties_get( properties, "post" ) ) == -1 )
1580 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "post" ) );
1582 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopped\n" );
1587 /** Determine if the consumer is stopped.
1589 * \public \memberof mlt_consumer_s
1590 * \param self a consumer
1591 * \return true if the consumer is stopped
1594 int mlt_consumer_is_stopped( mlt_consumer self )
1596 // Check if the consumer is stopped
1597 if ( self && self->is_stopped )
1598 return self->is_stopped( self );
1603 /** Close and destroy the consumer.
1605 * \public \memberof mlt_consumer_s
1606 * \param self a consumer
1609 void mlt_consumer_close( mlt_consumer self )
1611 if ( self != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( self ) ) <= 0 )
1613 // Get the childs close function
1614 void ( *consumer_close )( ) = self->close;
1616 if ( consumer_close )
1619 //mlt_consumer_stop( self );
1622 consumer_close( self );
1626 consumer_private *priv = self->local;
1628 // Make sure it only gets called once
1629 self->parent.close = NULL;
1631 // Destroy the push mutex and condition
1632 pthread_mutex_destroy( &priv->put_mutex );
1633 pthread_cond_destroy( &priv->put_cond );
1635 mlt_service_close( &self->parent );
1641 /** Get the position of the last frame shown.
1643 * \public \memberof mlt_consumer_s
1644 * \param consumer a consumer
1645 * \return the position
1648 mlt_position mlt_consumer_position( mlt_consumer consumer )
1650 return ( ( consumer_private* ) consumer->local )->position;