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;
103 mlt_properties_set( properties, "mlt_image_format", mlt_image_format_name( self->format ) );
104 mlt_properties_set( properties, "mlt_audio_format", mlt_audio_format_name( mlt_audio_s16 ) );
106 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
107 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
108 mlt_events_register( properties, "consumer-stopped", NULL );
109 mlt_events_listen( properties, self, "consumer-frame-show", ( mlt_listener )on_consumer_frame_show );
111 // Register a property-changed listener to handle the profile property -
112 // subsequent properties can override the profile
113 self->event_listener = mlt_events_listen( properties, self, "property-changed", ( mlt_listener )mlt_consumer_property_changed );
115 // Create the push mutex and condition
116 pthread_mutex_init( &self->put_mutex, NULL );
117 pthread_cond_init( &self->put_cond, NULL );
123 /** Convert the profile into properties on the consumer.
125 * \private \memberof mlt_consumer_s
126 * \param self a consumer
127 * \param profile a profile
128 * \param properties a properties list (typically, the consumer's)
131 static void apply_profile_properties( mlt_consumer self, mlt_profile profile, mlt_properties properties )
133 mlt_event_block( self->event_listener );
134 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
135 mlt_properties_set_int( properties, "frame_rate_num", profile->frame_rate_num );
136 mlt_properties_set_int( properties, "frame_rate_den", profile->frame_rate_den );
137 mlt_properties_set_int( properties, "width", profile->width );
138 mlt_properties_set_int( properties, "height", profile->height );
139 mlt_properties_set_int( properties, "progressive", profile->progressive );
140 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
141 mlt_properties_set_int( properties, "sample_aspect_num", profile->sample_aspect_num );
142 mlt_properties_set_int( properties, "sample_aspect_den", profile->sample_aspect_den );
143 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
144 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
145 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
146 mlt_properties_set_int( properties, "colorspace", profile->colorspace );
147 mlt_event_unblock( self->event_listener );
150 /** The property-changed event listener
152 * \private \memberof mlt_consumer_s
153 * \param owner the events object
154 * \param self the consumer
155 * \param name the name of the property that changed
158 static void mlt_consumer_property_changed( mlt_properties owner, mlt_consumer self, char *name )
160 if ( !strcmp( name, "mlt_profile" ) )
163 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
165 // Get the current profile
166 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
168 // Load the new profile
169 mlt_profile new_profile = mlt_profile_init( mlt_properties_get( properties, name ) );
174 if ( profile != NULL )
176 free( profile->description );
177 memcpy( profile, new_profile, sizeof( struct mlt_profile_s ) );
178 profile->description = strdup( new_profile->description );
182 profile = new_profile;
185 // Apply to properties
186 apply_profile_properties( self, profile, properties );
187 mlt_profile_close( new_profile );
190 else if ( !strcmp( name, "frame_rate_num" ) )
192 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
193 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
196 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
197 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
200 else if ( !strcmp( name, "frame_rate_den" ) )
202 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
203 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
206 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
207 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
210 else if ( !strcmp( name, "width" ) )
212 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
213 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
215 profile->width = mlt_properties_get_int( properties, "width" );
217 else if ( !strcmp( name, "height" ) )
219 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
220 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
222 profile->height = mlt_properties_get_int( properties, "height" );
224 else if ( !strcmp( name, "progressive" ) )
226 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
227 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
229 profile->progressive = mlt_properties_get_int( properties, "progressive" );
231 else if ( !strcmp( name, "sample_aspect_num" ) )
233 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
234 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
237 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
238 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
241 else if ( !strcmp( name, "sample_aspect_den" ) )
243 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
244 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
247 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
248 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
251 else if ( !strcmp( name, "display_aspect_num" ) )
253 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
254 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
257 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
258 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
261 else if ( !strcmp( name, "display_aspect_den" ) )
263 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
264 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
267 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
268 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
271 else if ( !strcmp( name, "colorspace" ) )
273 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
274 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
276 profile->colorspace = mlt_properties_get_int( properties, "colorspace" );
280 /** The transmitter for the consumer-frame-show event
282 * Invokes the listener.
284 * \private \memberof mlt_consumer_s
285 * \param listener a function pointer that will be invoked
286 * \param owner the events object that will be passed to \p listener
287 * \param self a service that will be passed to \p listener
288 * \param args an array of pointers - the first entry is passed as a frame to \p listener
291 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
293 if ( listener != NULL )
294 listener( owner, self, ( mlt_frame )args[ 0 ] );
297 /** The transmitter for the consumer-frame-render event
299 * Invokes the listener.
301 * \private \memberof mlt_consumer_s
302 * \param listener a function pointer that will be invoked
303 * \param owner the events object that will be passed to \p listener
304 * \param self a service that will be passed to \p listener
305 * \param args an array of pointers - the first entry is passed as a frame to \p listener
308 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
310 if ( listener != NULL )
311 listener( owner, self, ( mlt_frame )args[ 0 ] );
314 /** A listener on the consumer-frame-show event
316 * Saves the position of the frame shown.
318 * \private \memberof mlt_consumer_s
319 * \param owner the events object
320 * \param consumer the consumer on which this event occurred
321 * \param frame the frame that was shown
324 static void on_consumer_frame_show( mlt_properties owner, mlt_consumer consumer, mlt_frame frame )
327 consumer->position = mlt_frame_get_position( frame );
330 /** Create a new consumer.
332 * \public \memberof mlt_consumer_s
333 * \param profile a profile (optional, but recommended)
334 * \return a new consumer
337 mlt_consumer mlt_consumer_new( mlt_profile profile )
339 // Create the memory for the structure
340 mlt_consumer self = malloc( sizeof( struct mlt_consumer_s ) );
343 if ( self != NULL && mlt_consumer_init( self, NULL, profile ) == 0 )
355 /** Get the parent service object.
357 * \public \memberof mlt_consumer_s
358 * \param self a consumer
359 * \return the parent service class
360 * \see MLT_CONSUMER_SERVICE
363 mlt_service mlt_consumer_service( mlt_consumer self )
365 return self != NULL ? &self->parent : NULL;
368 /** Get the consumer properties.
370 * \public \memberof mlt_consumer_s
371 * \param self a consumer
372 * \return the consumer's properties list
373 * \see MLT_CONSUMER_PROPERTIES
376 mlt_properties mlt_consumer_properties( mlt_consumer self )
378 return self != NULL ? MLT_SERVICE_PROPERTIES( &self->parent ) : NULL;
381 /** Connect the consumer to the producer.
383 * \public \memberof mlt_consumer_s
384 * \param self a consumer
385 * \param producer a producer
386 * \return > 0 warning, == 0 success, < 0 serious error,
387 * 1 = this service does not accept input,
388 * 2 = the producer is invalid,
389 * 3 = the producer is already registered with this consumer
392 int mlt_consumer_connect( mlt_consumer self, mlt_service producer )
394 return mlt_service_connect_producer( &self->parent, producer, 0 );
397 /** Start the consumer.
399 * \public \memberof mlt_consumer_s
400 * \param self a consumer
401 * \return true if there was an error
404 int mlt_consumer_start( mlt_consumer self )
406 if ( !mlt_consumer_is_stopped( self ) )
409 // Stop listening to the property-changed event
410 mlt_event_block( self->event_listener );
413 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
415 // Determine if there's a test card producer
416 char *test_card = mlt_properties_get( properties, "test_card" );
418 // Just to make sure nothing is hanging around...
419 pthread_mutex_lock( &self->put_mutex );
421 self->put_active = 1;
422 pthread_mutex_unlock( &self->put_mutex );
425 if ( test_card != NULL )
427 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
429 // Create a test card producer
430 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
431 mlt_producer producer = mlt_factory_producer( profile, NULL, test_card );
433 // Do we have a producer
434 if ( producer != NULL )
436 // Test card should loop I guess...
437 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
438 //mlt_producer_set_speed( producer, 0 );
439 //mlt_producer_set_in_and_out( producer, 0, 0 );
441 // Set the test card on the consumer
442 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
448 // Allow the hash table to speed things up
449 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
452 // The profile could have changed between a stop and a restart.
453 apply_profile_properties( self, mlt_service_profile( MLT_CONSUMER_SERVICE(self) ), properties );
455 // Set the frame duration in microseconds for the frame-dropping heuristic
456 int frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
457 int frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
458 int frame_duration = 0;
460 if ( frame_rate_num && frame_rate_den )
462 frame_duration = 1000000 / frame_rate_num * frame_rate_den;
465 mlt_properties_set_int( properties, "frame_duration", frame_duration );
467 // Check and run an ante command
468 if ( mlt_properties_get( properties, "ante" ) )
469 if ( system( mlt_properties_get( properties, "ante" ) ) == -1 )
470 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "ante" ) );
472 // Set the real_time preference
473 self->real_time = mlt_properties_get_int( properties, "real_time" );
475 // For worker threads implementation, buffer must be at least # threads
476 if ( abs( self->real_time ) > 1 && mlt_properties_get_int( properties, "buffer" ) <= abs( self->real_time ) )
477 mlt_properties_set_int( properties, "_buffer", abs( self->real_time ) + 1 );
479 // Get the image format to use for rendering threads
480 const char* format = mlt_properties_get( properties, "mlt_image_format" );
483 if ( !strcmp( format, "rgb24" ) )
484 self->format = mlt_image_rgb24;
485 else if ( !strcmp( format, "rgb24a" ) )
486 self->format = mlt_image_rgb24a;
487 else if ( !strcmp( format, "yuv420p" ) )
488 self->format = mlt_image_yuv420p;
489 else if ( !strcmp( format, "none" ) )
490 self->format = mlt_image_none;
492 self->format = mlt_image_yuv422;
496 if ( self->start != NULL )
497 return self->start( self );
502 /** An alternative method to feed frames into the consumer.
504 * Only valid if the consumer itself is not connected.
506 * \public \memberof mlt_consumer_s
507 * \param self a consumer
508 * \param frame a frame
509 * \return true (ignore self for now)
512 int mlt_consumer_put_frame( mlt_consumer self, mlt_frame frame )
516 // Get the service assoicated to the consumer
517 mlt_service service = MLT_CONSUMER_SERVICE( self );
519 if ( mlt_service_producer( service ) == NULL )
523 pthread_mutex_lock( &self->put_mutex );
524 while ( self->put_active && self->put != NULL )
526 gettimeofday( &now, NULL );
527 tm.tv_sec = now.tv_sec + 1;
528 tm.tv_nsec = now.tv_usec * 1000;
529 pthread_cond_timedwait( &self->put_cond, &self->put_mutex, &tm );
531 if ( self->put_active && self->put == NULL )
534 mlt_frame_close( frame );
535 pthread_cond_broadcast( &self->put_cond );
536 pthread_mutex_unlock( &self->put_mutex );
540 mlt_frame_close( frame );
546 /** Protected method for consumer to get frames from connected service
548 * \public \memberof mlt_consumer_s
549 * \param self a consumer
553 mlt_frame mlt_consumer_get_frame( mlt_consumer self )
556 mlt_frame frame = NULL;
558 // Get the service assoicated to the consumer
559 mlt_service service = MLT_CONSUMER_SERVICE( self );
561 // Get the consumer properties
562 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
565 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
569 pthread_mutex_lock( &self->put_mutex );
570 while ( self->put_active && self->put == NULL )
572 gettimeofday( &now, NULL );
573 tm.tv_sec = now.tv_sec + 1;
574 tm.tv_nsec = now.tv_usec * 1000;
575 pthread_cond_timedwait( &self->put_cond, &self->put_mutex, &tm );
579 pthread_cond_broadcast( &self->put_cond );
580 pthread_mutex_unlock( &self->put_mutex );
582 mlt_service_apply_filters( service, frame, 0 );
584 else if ( mlt_service_producer( service ) != NULL )
586 mlt_service_get_frame( service, &frame, 0 );
590 frame = mlt_frame_init( service );
595 // Get the frame properties
596 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
598 // Get the test card producer
599 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
601 // Attach the test frame producer to it.
602 if ( test_card != NULL )
603 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
605 // Pass along the interpolation and deinterlace options
606 // TODO: get rid of consumer_deinterlace and use profile.progressive
607 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
608 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
609 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
610 mlt_properties_set_int( frame_properties, "consumer_tff", mlt_properties_get_int( properties, "top_field_first" ) );
617 /** Compute the time difference between now and a time value.
619 * \private \memberof mlt_consumer_s
620 * \param time1 a time value to be compared against now
621 * \return the difference in microseconds
624 static inline long time_difference( struct timeval *time1 )
626 struct timeval time2;
627 time2.tv_sec = time1->tv_sec;
628 time2.tv_usec = time1->tv_usec;
629 gettimeofday( time1, NULL );
630 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
633 /** The thread procedure for asynchronously pulling frames through the service
634 * network connected to a consumer.
636 * \private \memberof mlt_consumer_s
637 * \param arg a consumer
640 static void *consumer_read_ahead_thread( void *arg )
642 // The argument is the consumer
643 mlt_consumer self = arg;
645 // Get the properties of the consumer
646 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
648 // Get the width and height
649 int width = mlt_properties_get_int( properties, "width" );
650 int height = mlt_properties_get_int( properties, "height" );
652 // See if video is turned off
653 int video_off = mlt_properties_get_int( properties, "video_off" );
654 int preview_off = mlt_properties_get_int( properties, "preview_off" );
655 int preview_format = mlt_properties_get_int( properties, "preview_format" );
657 // Get the audio settings
658 mlt_audio_format afmt = mlt_audio_s16;
659 const char *format = mlt_properties_get( properties, "mlt_audio_format" );
662 if ( !strcmp( format, "none" ) )
663 afmt = mlt_audio_none;
664 else if ( !strcmp( format, "s32" ) )
665 afmt = mlt_audio_s32;
666 else if ( !strcmp( format, "s32le" ) )
667 afmt = mlt_audio_s32le;
668 else if ( !strcmp( format, "float" ) )
669 afmt = mlt_audio_float;
670 else if ( !strcmp( format, "f32le" ) )
671 afmt = mlt_audio_f32le;
674 double fps = mlt_properties_get_double( properties, "fps" );
675 int channels = mlt_properties_get_int( properties, "channels" );
676 int frequency = mlt_properties_get_int( properties, "frequency" );
680 // See if audio is turned off
681 int audio_off = mlt_properties_get_int( properties, "audio_off" );
683 // Get the maximum size of the buffer
684 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
686 // General frame variable
687 mlt_frame frame = NULL;
688 uint8_t *image = NULL;
693 // Average time for get_frame and get_image
696 int64_t time_process = 0;
698 mlt_position pos = 0;
699 mlt_position start_pos = 0;
700 mlt_position last_pos = 0;
701 int frame_duration = mlt_properties_get_int( properties, "frame_duration" );
702 int drop_max = mlt_properties_get_int( properties, "drop_max" );
704 if ( preview_off && preview_format != 0 )
705 self->format = preview_format;
707 // Get the first frame
708 frame = mlt_consumer_get_frame( self );
712 // Get the image of the first frame
715 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
716 mlt_frame_get_image( frame, &image, &self->format, &width, &height, 0 );
721 samples = mlt_sample_calculator( fps, frequency, counter++ );
722 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
726 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
727 last_pos = start_pos = pos = mlt_frame_get_position( frame );
730 // Get the starting time (can ignore the times above)
731 gettimeofday( &ante, NULL );
733 // Continue to read ahead
734 while ( self->ahead )
736 // Put the current frame into the queue
737 pthread_mutex_lock( &self->queue_mutex );
738 while( self->ahead && mlt_deque_count( self->queue ) >= buffer )
739 pthread_cond_wait( &self->queue_cond, &self->queue_mutex );
740 mlt_deque_push_back( self->queue, frame );
741 pthread_cond_broadcast( &self->queue_cond );
742 pthread_mutex_unlock( &self->queue_mutex );
744 // Get the next frame
745 frame = mlt_consumer_get_frame( self );
747 // If there's no frame, we're probably stopped...
750 pos = mlt_frame_get_position( frame );
752 // Increment the counter used for averaging processing cost
755 // All non-normal playback frames should be shown
756 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
758 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
759 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
761 // Indicate seeking or trick-play
765 // If skip flag not set or frame-dropping disabled
766 if ( !skip_next || self->real_time == -1 )
770 // Reset width/height - could have been changed by previous mlt_frame_get_image
771 width = mlt_properties_get_int( properties, "width" );
772 height = mlt_properties_get_int( properties, "height" );
775 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
776 mlt_frame_get_image( frame, &image, &self->format, &width, &height, 0 );
779 // Indicate the rendered image is available.
780 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
782 // Reset consecutively-skipped counter
785 else // Skip image processing
787 // Increment the number of consecutively-skipped frames
790 // If too many (1 sec) consecutively-skipped frames
791 if ( skipped > drop_max )
793 // Reset cost tracker
796 mlt_log_verbose( self, "too many frames dropped - forcing next frame\n" );
800 // Always process audio
803 samples = mlt_sample_calculator( fps, frequency, counter++ );
804 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
807 // Get the time to process this frame
808 int64_t time_current = time_difference( &ante );
810 // If the current time is not suddenly some large amount
811 if ( time_current < time_process / count * 20 || !time_process || count < 5 )
813 // Accumulate the cost for processing this frame
814 time_process += time_current;
818 mlt_log_debug( self, "current %"PRId64" threshold %"PRId64" count %d\n",
819 time_current, (int64_t) (time_process / count * 20), count );
820 // Ignore the cost of this frame's time
824 // Determine if we started, resumed, or seeked
825 if ( pos != last_pos + 1 )
829 // Do not skip the first 20% of buffer at start, resume, or seek
830 if ( pos - start_pos <= buffer / 5 + 1 )
832 // Reset cost tracker
840 // Only consider skipping if the buffer level is low (or really small)
841 if ( mlt_deque_count( self->queue ) <= buffer / 5 + 1 )
843 // Skip next frame if average cost exceeds frame duration.
844 if ( time_process / count > frame_duration )
847 mlt_log_debug( self, "avg usec %"PRId64" (%"PRId64"/%d) duration %d\n",
848 time_process/count, time_process, count, frame_duration);
852 // Remove the last frame
853 mlt_frame_close( frame );
858 /** Locate the first unprocessed frame in the queue.
860 * When playing with realtime behavior, we do not use the true head, but
861 * rather an adjusted process_head. The process_head is adjusted based on
862 * the rate of frame-dropping or recovery from frame-dropping. The idea is
863 * that as the level of frame-dropping increases to move the process_head
864 * closer to the tail because the frames are not completing processing prior
865 * to their playout! Then, as frames are not dropped the process_head moves
866 * back closer to the head of the queue so that worker threads can work
867 * ahead of the playout point (queue head).
869 * \private \memberof mlt_consumer_s
870 * \param self a consumer
871 * \return an index into the queue
874 static inline int first_unprocessed_frame( mlt_consumer self )
876 int index = self->real_time <= 0 ? 0 : self->process_head;
877 while ( index < mlt_deque_count( self->queue ) && MLT_FRAME( mlt_deque_peek( self->queue, index ) )->is_processing )
882 /** The worker thread procedure for parallel processing frames.
884 * \private \memberof mlt_consumer_s
885 * \param arg a consumer
888 static void *consumer_worker_thread( void *arg )
890 // The argument is the consumer
891 mlt_consumer self = arg;
893 // Get the properties of the consumer
894 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
896 // Get the width and height
897 int width = mlt_properties_get_int( properties, "width" );
898 int height = mlt_properties_get_int( properties, "height" );
899 mlt_image_format format = self->format;
901 // See if video is turned off
902 int video_off = mlt_properties_get_int( properties, "video_off" );
903 int preview_off = mlt_properties_get_int( properties, "preview_off" );
904 int preview_format = mlt_properties_get_int( properties, "preview_format" );
906 // General frame variable
907 mlt_frame frame = NULL;
908 uint8_t *image = NULL;
910 if ( preview_off && preview_format != 0 )
911 format = preview_format;
913 // Continue to read ahead
914 while ( self->ahead )
916 // Get the next unprocessed frame from the work queue
917 pthread_mutex_lock( &self->queue_mutex );
918 int index = first_unprocessed_frame( self );
919 while ( self->ahead && index >= mlt_deque_count( self->queue ) )
921 mlt_log_debug( MLT_CONSUMER_SERVICE(self), "waiting in worker index = %d queue count = %d\n",
922 index, mlt_deque_count( self->queue ) );
923 pthread_cond_wait( &self->queue_cond, &self->queue_mutex );
924 index = first_unprocessed_frame( self );
927 // Mark the frame for processing
928 frame = mlt_deque_peek( self->queue, index );
931 mlt_log_debug( MLT_CONSUMER_SERVICE(self), "worker processing index = %d frame %d queue count = %d\n",
932 index, mlt_frame_get_position(frame), mlt_deque_count( self->queue ) );
933 frame->is_processing = 1;
934 mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( frame ) );
936 pthread_mutex_unlock( &self->queue_mutex );
938 // If there's no frame, we're probably stopped...
942 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
943 // All non normal playback frames should be shown
944 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
945 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
951 // Fetch width/height again
952 width = mlt_properties_get_int( properties, "width" );
953 height = mlt_properties_get_int( properties, "height" );
954 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
955 mlt_frame_get_image( frame, &image, &format, &width, &height, 0 );
957 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
958 mlt_frame_close( frame );
960 // Tell a waiting thread (non-realtime main consumer thread) that we are done.
961 pthread_mutex_lock( &self->done_mutex );
962 pthread_cond_broadcast( &self->done_cond );
963 pthread_mutex_unlock( &self->done_mutex );
969 /** Start the read/render thread.
971 * \private \memberof mlt_consumer_s
972 * \param self a consumer
975 static void consumer_read_ahead_start( mlt_consumer self )
980 // Create the frame queue
981 self->queue = mlt_deque_init( );
983 // Create the queue mutex
984 pthread_mutex_init( &self->queue_mutex, NULL );
986 // Create the condition
987 pthread_cond_init( &self->queue_cond, NULL );
989 // Create the read ahead
990 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( self ), "priority" ) )
992 struct sched_param priority;
993 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( self ), "priority" );
994 pthread_attr_t thread_attributes;
995 pthread_attr_init( &thread_attributes );
996 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
997 pthread_attr_setschedparam( &thread_attributes, &priority );
998 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
999 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
1000 if ( pthread_create( &self->ahead_thread, &thread_attributes, consumer_read_ahead_thread, self ) < 0 )
1001 pthread_create( &self->ahead_thread, NULL, consumer_read_ahead_thread, self );
1002 pthread_attr_destroy( &thread_attributes );
1006 pthread_create( &self->ahead_thread, NULL, consumer_read_ahead_thread, self );
1011 /** Start the worker threads.
1013 * \private \memberof mlt_consumer_s
1014 * \param self a consumer
1017 static void consumer_work_start( mlt_consumer self )
1019 int n = abs( self->real_time );
1020 pthread_t *thread = calloc( 1, sizeof( pthread_t ) * n );
1022 // We're running now
1024 self->threads = thread;
1026 // These keep track of the accelleration of frame dropping or recovery.
1027 self->consecutive_dropped = 0;
1028 self->consecutive_rendered = 0;
1030 // This is the position in the queue from which to look for a frame to process.
1031 // If we always start from the head, then we may likely not complete processing
1032 // before the frame is played out.
1033 self->process_head = 0;
1035 // Create the queues
1036 self->queue = mlt_deque_init();
1037 self->worker_threads = mlt_deque_init();
1039 // Create the mutexes
1040 pthread_mutex_init( &self->queue_mutex, NULL );
1041 pthread_mutex_init( &self->done_mutex, NULL );
1043 // Create the conditions
1044 pthread_cond_init( &self->queue_cond, NULL );
1045 pthread_cond_init( &self->done_cond, NULL );
1047 // Create the read ahead
1048 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( self ), "priority" ) )
1051 struct sched_param priority;
1052 pthread_attr_t thread_attributes;
1054 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( self ), "priority" );
1055 pthread_attr_init( &thread_attributes );
1056 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
1057 pthread_attr_setschedparam( &thread_attributes, &priority );
1058 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
1059 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
1063 if ( pthread_create( thread, &thread_attributes, consumer_worker_thread, self ) < 0 )
1064 if ( pthread_create( thread, NULL, consumer_worker_thread, self ) == 0 )
1065 mlt_deque_push_back( self->worker_threads, thread );
1068 pthread_attr_destroy( &thread_attributes );
1075 if ( pthread_create( thread, NULL, consumer_worker_thread, self ) == 0 )
1076 mlt_deque_push_back( self->worker_threads, thread );
1083 /** Stop the read/render thread.
1085 * \private \memberof mlt_consumer_s
1086 * \param self a consumer
1089 static void consumer_read_ahead_stop( mlt_consumer self )
1091 // Make sure we're running
1092 if ( self->started )
1094 // Inform thread to stop
1097 // Broadcast to the condition in case it's waiting
1098 pthread_mutex_lock( &self->queue_mutex );
1099 pthread_cond_broadcast( &self->queue_cond );
1100 pthread_mutex_unlock( &self->queue_mutex );
1102 // Broadcast to the put condition in case it's waiting
1103 pthread_mutex_lock( &self->put_mutex );
1104 pthread_cond_broadcast( &self->put_cond );
1105 pthread_mutex_unlock( &self->put_mutex );
1108 pthread_join( self->ahead_thread, NULL );
1111 // Destroy the frame queue mutex
1112 pthread_mutex_destroy( &self->queue_mutex );
1114 // Destroy the condition
1115 pthread_cond_destroy( &self->queue_cond );
1118 while ( mlt_deque_count( self->queue ) )
1119 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
1122 mlt_deque_close( self->queue );
1126 /** Stop the worker threads.
1128 * \private \memberof mlt_consumer_s
1129 * \param self a consumer
1132 static void consumer_work_stop( mlt_consumer self )
1134 // Make sure we're running
1135 if ( self->started )
1137 // Inform thread to stop
1140 // Broadcast to the queue condition in case it's waiting
1141 pthread_mutex_lock( &self->queue_mutex );
1142 pthread_cond_broadcast( &self->queue_cond );
1143 pthread_mutex_unlock( &self->queue_mutex );
1145 // Broadcast to the put condition in case it's waiting
1146 pthread_mutex_lock( &self->put_mutex );
1147 pthread_cond_broadcast( &self->put_cond );
1148 pthread_mutex_unlock( &self->put_mutex );
1150 // Broadcast to the done condition in case it's waiting
1151 pthread_mutex_lock( &self->done_mutex );
1152 pthread_cond_broadcast( &self->done_cond );
1153 pthread_mutex_unlock( &self->done_mutex );
1157 while ( ( thread = mlt_deque_pop_back( self->worker_threads ) ) )
1158 pthread_join( *thread, NULL );
1160 // Deallocate the array of threads
1161 if ( self->threads )
1162 free( self->threads );
1164 // Indicate that worker threads no longer running
1167 // Destroy the mutexes
1168 pthread_mutex_destroy( &self->queue_mutex );
1169 pthread_mutex_destroy( &self->done_mutex );
1171 // Destroy the conditions
1172 pthread_cond_destroy( &self->queue_cond );
1173 pthread_cond_destroy( &self->done_cond );
1176 while ( mlt_deque_count( self->queue ) )
1177 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
1180 mlt_deque_close( self->queue );
1181 mlt_deque_close( self->worker_threads );
1185 /** Flush the read/render thread's buffer.
1187 * \public \memberof mlt_consumer_s
1188 * \param self a consumer
1191 void mlt_consumer_purge( mlt_consumer self )
1193 if ( self && self->ahead )
1195 pthread_mutex_lock( &self->queue_mutex );
1196 while ( mlt_deque_count( self->queue ) )
1197 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
1198 pthread_cond_broadcast( &self->queue_cond );
1199 pthread_mutex_unlock( &self->queue_mutex );
1203 /** Use multiple worker threads and a work queue.
1206 static mlt_frame worker_get_frame( mlt_consumer self, mlt_properties properties )
1209 mlt_frame frame = NULL;
1211 double fps = mlt_properties_get_double( properties, "fps" );
1212 int threads = abs( self->real_time );
1213 int buffer = mlt_properties_get_int( properties, "_buffer" );
1214 buffer = buffer > 0 ? buffer : mlt_properties_get_int( properties, "buffer" );
1215 // This is a heuristic to determine a suitable minimum buffer size for the number of threads.
1216 int headroom = 2 + threads * threads;
1217 buffer = buffer < headroom ? headroom : buffer;
1219 // Start worker threads if not already started.
1220 if ( ! self->ahead )
1222 int prefill = mlt_properties_get_int( properties, "prefill" );
1223 prefill = prefill > 0 && prefill < buffer ? prefill : buffer;
1225 consumer_work_start( self );
1227 // Fill the work queue.
1229 while ( self->ahead && i-- )
1231 frame = mlt_consumer_get_frame( self );
1234 pthread_mutex_lock( &self->queue_mutex );
1235 mlt_deque_push_back( self->queue, frame );
1236 pthread_cond_signal( &self->queue_cond );
1237 pthread_mutex_unlock( &self->queue_mutex );
1242 while ( self->ahead && first_unprocessed_frame( self ) < prefill )
1244 pthread_mutex_lock( &self->done_mutex );
1245 pthread_cond_wait( &self->done_cond, &self->done_mutex );
1246 pthread_mutex_unlock( &self->done_mutex );
1248 self->process_head = threads;
1251 // mlt_log_verbose( MLT_CONSUMER_SERVICE(self), "size %d done count %d work count %d process_head %d\n",
1252 // threads, first_unprocessed_frame( self ), mlt_deque_count( self->queue ), self->process_head );
1254 // Feed the work queue
1255 while ( self->ahead && mlt_deque_count( self->queue ) < buffer )
1257 frame = mlt_consumer_get_frame( self );
1260 pthread_mutex_lock( &self->queue_mutex );
1261 mlt_deque_push_back( self->queue, frame );
1262 pthread_cond_signal( &self->queue_cond );
1263 pthread_mutex_unlock( &self->queue_mutex );
1266 // Wait if not realtime.
1267 mlt_frame head_frame = MLT_FRAME( mlt_deque_peek_front( self->queue ) );
1268 while ( self->ahead && self->real_time < 0 &&
1269 !( head_frame && mlt_properties_get_int( MLT_FRAME_PROPERTIES( head_frame ), "rendered" ) ) )
1271 pthread_mutex_lock( &self->done_mutex );
1272 pthread_cond_wait( &self->done_cond, &self->done_mutex );
1273 pthread_mutex_unlock( &self->done_mutex );
1276 // Get the frame from the queue.
1277 pthread_mutex_lock( &self->queue_mutex );
1278 frame = mlt_deque_pop_front( self->queue );
1279 pthread_mutex_unlock( &self->queue_mutex );
1281 // Adapt the worker process head to the runtime conditions.
1282 if ( self->real_time > 0 )
1284 if ( frame && mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "rendered" ) )
1286 self->consecutive_dropped = 0;
1287 if ( self->process_head > threads && self->consecutive_rendered >= self->process_head )
1288 self->process_head--;
1290 self->consecutive_rendered++;
1294 self->consecutive_rendered = 0;
1295 if ( self->process_head < buffer - threads && self->consecutive_dropped > threads )
1296 self->process_head++;
1298 self->consecutive_dropped++;
1300 // mlt_log_verbose( MLT_CONSUMER_SERVICE(self), "dropped %d rendered %d process_head %d\n",
1301 // self->consecutive_dropped, self->consecutive_rendered, self->process_head );
1303 // Check for too many consecutively dropped frames
1304 if ( self->consecutive_dropped > mlt_properties_get_int( properties, "drop_max" ) )
1306 int orig_buffer = mlt_properties_get_int( properties, "buffer" );
1307 int prefill = mlt_properties_get_int( properties, "prefill" );
1308 mlt_log_verbose( self, "too many frames dropped - " );
1310 // If using a default low-latency buffer level (SDL) and below the limit
1311 if ( ( orig_buffer == 1 || prefill == 1 ) && buffer < (threads + 1) * 10 )
1313 // Auto-scale the buffer to compensate
1314 mlt_log_verbose( self, "increasing buffer to %d\n", buffer + threads );
1315 mlt_properties_set_int( properties, "_buffer", buffer + threads );
1316 self->consecutive_dropped = fps / 2;
1320 // Tell the consumer to render it
1321 mlt_log_verbose( self, "forcing next frame\n" );
1322 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
1323 self->consecutive_dropped = 0;
1331 /** Get the next frame from the producer connected to a consumer.
1333 * Typically, one uses this instead of \p mlt_consumer_get_frame to make
1334 * the asynchronous/real-time behavior configurable at runtime.
1335 * You should close the frame returned from this when you are done with it.
1337 * \public \memberof mlt_consumer_s
1338 * \param self a consumer
1342 mlt_frame mlt_consumer_rt_frame( mlt_consumer self )
1345 mlt_frame frame = NULL;
1347 // Get the properties
1348 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
1350 // Check if the user has requested real time or not
1351 if ( self->real_time > 1 || self->real_time < -1 )
1354 return worker_get_frame( self, properties );
1356 else if ( self->real_time == 1 || self->real_time == -1 )
1360 // Is the read ahead running?
1361 if ( self->ahead == 0 )
1363 int buffer = mlt_properties_get_int( properties, "buffer" );
1364 int prefill = mlt_properties_get_int( properties, "prefill" );
1365 consumer_read_ahead_start( self );
1367 size = prefill > 0 && prefill < buffer ? prefill : buffer;
1370 // Get frame from queue
1371 pthread_mutex_lock( &self->queue_mutex );
1372 while( self->ahead && mlt_deque_count( self->queue ) < size )
1373 pthread_cond_wait( &self->queue_cond, &self->queue_mutex );
1374 frame = mlt_deque_pop_front( self->queue );
1375 pthread_cond_broadcast( &self->queue_cond );
1376 pthread_mutex_unlock( &self->queue_mutex );
1378 else // real_time == 0
1380 // Get the frame in non real time
1381 frame = mlt_consumer_get_frame( self );
1383 // This isn't true, but from the consumers perspective it is
1384 if ( frame != NULL )
1385 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
1391 /** Callback for the implementation to indicate a stopped condition.
1393 * \public \memberof mlt_consumer_s
1394 * \param self a consumer
1397 void mlt_consumer_stopped( mlt_consumer self )
1399 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( self ), "running", 0 );
1400 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-stopped", NULL );
1401 mlt_event_unblock( self->event_listener );
1404 /** Stop the consumer.
1406 * \public \memberof mlt_consumer_s
1407 * \param self a consumer
1408 * \return true if there was an error
1411 int mlt_consumer_stop( mlt_consumer self )
1413 // Get the properies
1414 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
1417 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping put waiting\n" );
1418 pthread_mutex_lock( &self->put_mutex );
1419 self->put_active = 0;
1420 pthread_cond_broadcast( &self->put_cond );
1421 pthread_mutex_unlock( &self->put_mutex );
1423 // Stop the consumer
1424 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping consumer\n" );
1426 // Cancel the read ahead threads
1428 if ( self->started )
1430 // Unblock the consumer calling mlt_consumer_rt_frame
1431 pthread_mutex_lock( &self->queue_mutex );
1432 pthread_cond_broadcast( &self->queue_cond );
1433 pthread_mutex_unlock( &self->queue_mutex );
1436 // Invoke the child callback
1437 if ( self->stop != NULL )
1440 // Check if the user has requested real time or not and stop if necessary
1441 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping read_ahead\n" );
1442 if ( abs( self->real_time ) == 1 )
1443 consumer_read_ahead_stop( self );
1444 else if ( abs( self->real_time ) > 1 )
1445 consumer_work_stop( self );
1447 // Kill the test card
1448 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
1450 // Check and run a post command
1451 if ( mlt_properties_get( properties, "post" ) )
1452 if (system( mlt_properties_get( properties, "post" ) ) == -1 )
1453 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "post" ) );
1455 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopped\n" );
1460 /** Determine if the consumer is stopped.
1462 * \public \memberof mlt_consumer_s
1463 * \param self a consumer
1464 * \return true if the consumer is stopped
1467 int mlt_consumer_is_stopped( mlt_consumer self )
1469 // Check if the consumer is stopped
1470 if ( self && self->is_stopped )
1471 return self->is_stopped( self );
1476 /** Close and destroy the consumer.
1478 * \public \memberof mlt_consumer_s
1479 * \param self a consumer
1482 void mlt_consumer_close( mlt_consumer self )
1484 if ( self != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( self ) ) <= 0 )
1486 // Get the childs close function
1487 void ( *consumer_close )( ) = self->close;
1489 if ( consumer_close )
1492 //mlt_consumer_stop( self );
1495 consumer_close( self );
1499 // Make sure it only gets called once
1500 self->parent.close = NULL;
1502 // Destroy the push mutex and condition
1503 pthread_mutex_destroy( &self->put_mutex );
1504 pthread_cond_destroy( &self->put_cond );
1506 mlt_service_close( &self->parent );
1511 /** Get the position of the last frame shown.
1513 * \public \memberof mlt_consumer_s
1514 * \param consumer a consumer
1515 * \return the position
1518 mlt_position mlt_consumer_position( mlt_consumer consumer )
1520 return consumer->position;