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-stopping", NULL );
144 mlt_events_register( properties, "consumer-stopped", NULL );
145 mlt_events_listen( properties, self, "consumer-frame-show", ( mlt_listener )on_consumer_frame_show );
147 // Register a property-changed listener to handle the profile property -
148 // subsequent properties can override the profile
149 priv->event_listener = mlt_events_listen( properties, self, "property-changed", ( mlt_listener )mlt_consumer_property_changed );
151 // Create the push mutex and condition
152 pthread_mutex_init( &priv->put_mutex, NULL );
153 pthread_cond_init( &priv->put_cond, NULL );
159 /** Convert the profile into properties on the consumer.
161 * \private \memberof mlt_consumer_s
162 * \param self a consumer
163 * \param profile a profile
164 * \param properties a properties list (typically, the consumer's)
167 static void apply_profile_properties( mlt_consumer self, mlt_profile profile, mlt_properties properties )
169 consumer_private *priv = self->local;
170 mlt_event_block( priv->event_listener );
171 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
172 mlt_properties_set_int( properties, "frame_rate_num", profile->frame_rate_num );
173 mlt_properties_set_int( properties, "frame_rate_den", profile->frame_rate_den );
174 mlt_properties_set_int( properties, "width", profile->width );
175 mlt_properties_set_int( properties, "height", profile->height );
176 mlt_properties_set_int( properties, "progressive", profile->progressive );
177 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
178 mlt_properties_set_int( properties, "sample_aspect_num", profile->sample_aspect_num );
179 mlt_properties_set_int( properties, "sample_aspect_den", profile->sample_aspect_den );
180 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
181 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
182 mlt_properties_set_int( properties, "display_aspect_den", profile->display_aspect_den );
183 mlt_properties_set_int( properties, "colorspace", profile->colorspace );
184 mlt_event_unblock( priv->event_listener );
187 /** The property-changed event listener
189 * \private \memberof mlt_consumer_s
190 * \param owner the events object
191 * \param self the consumer
192 * \param name the name of the property that changed
195 static void mlt_consumer_property_changed( mlt_properties owner, mlt_consumer self, char *name )
197 if ( !strcmp( name, "mlt_profile" ) )
200 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
202 // Get the current profile
203 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
205 // Load the new profile
206 mlt_profile new_profile = mlt_profile_init( mlt_properties_get( properties, name ) );
211 if ( profile != NULL )
213 free( profile->description );
214 memcpy( profile, new_profile, sizeof( struct mlt_profile_s ) );
215 profile->description = strdup( new_profile->description );
219 profile = new_profile;
222 // Apply to properties
223 apply_profile_properties( self, profile, properties );
224 mlt_profile_close( new_profile );
227 else if ( !strcmp( name, "frame_rate_num" ) )
229 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
230 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
233 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
234 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
237 else if ( !strcmp( name, "frame_rate_den" ) )
239 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
240 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
243 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
244 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
247 else if ( !strcmp( name, "width" ) )
249 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
250 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
252 profile->width = mlt_properties_get_int( properties, "width" );
254 else if ( !strcmp( name, "height" ) )
256 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
257 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
259 profile->height = mlt_properties_get_int( properties, "height" );
261 else if ( !strcmp( name, "progressive" ) )
263 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
264 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
266 profile->progressive = mlt_properties_get_int( properties, "progressive" );
268 else if ( !strcmp( name, "sample_aspect_num" ) )
270 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
271 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
274 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
275 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
278 else if ( !strcmp( name, "sample_aspect_den" ) )
280 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
281 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
284 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
285 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
288 else if ( !strcmp( name, "display_aspect_num" ) )
290 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
291 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
294 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
295 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
298 else if ( !strcmp( name, "display_aspect_den" ) )
300 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
301 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
304 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
305 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
308 else if ( !strcmp( name, "colorspace" ) )
310 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
311 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
313 profile->colorspace = mlt_properties_get_int( properties, "colorspace" );
317 /** The transmitter for the consumer-frame-show event
319 * Invokes the listener.
321 * \private \memberof mlt_consumer_s
322 * \param listener a function pointer that will be invoked
323 * \param owner the events object that will be passed to \p listener
324 * \param self a service that will be passed to \p listener
325 * \param args an array of pointers - the first entry is passed as a frame to \p listener
328 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
330 if ( listener != NULL )
331 listener( owner, self, ( mlt_frame )args[ 0 ] );
334 /** The transmitter for the consumer-frame-render event
336 * Invokes the listener.
338 * \private \memberof mlt_consumer_s
339 * \param listener a function pointer that will be invoked
340 * \param owner the events object that will be passed to \p listener
341 * \param self a service that will be passed to \p listener
342 * \param args an array of pointers - the first entry is passed as a frame to \p listener
345 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
347 if ( listener != NULL )
348 listener( owner, self, ( mlt_frame )args[ 0 ] );
351 /** A listener on the consumer-frame-show event
353 * Saves the position of the frame shown.
355 * \private \memberof mlt_consumer_s
356 * \param owner the events object
357 * \param consumer the consumer on which this event occurred
358 * \param frame the frame that was shown
361 static void on_consumer_frame_show( mlt_properties owner, mlt_consumer consumer, mlt_frame frame )
364 ( ( consumer_private*) consumer->local )->position = mlt_frame_get_position( frame );
367 /** Create a new consumer.
369 * \public \memberof mlt_consumer_s
370 * \param profile a profile (optional, but recommended)
371 * \return a new consumer
374 mlt_consumer mlt_consumer_new( mlt_profile profile )
376 // Create the memory for the structure
377 mlt_consumer self = malloc( sizeof( struct mlt_consumer_s ) );
380 if ( self != NULL && mlt_consumer_init( self, NULL, profile ) == 0 )
392 /** Get the parent service object.
394 * \public \memberof mlt_consumer_s
395 * \param self a consumer
396 * \return the parent service class
397 * \see MLT_CONSUMER_SERVICE
400 mlt_service mlt_consumer_service( mlt_consumer self )
402 return self != NULL ? &self->parent : NULL;
405 /** Get the consumer properties.
407 * \public \memberof mlt_consumer_s
408 * \param self a consumer
409 * \return the consumer's properties list
410 * \see MLT_CONSUMER_PROPERTIES
413 mlt_properties mlt_consumer_properties( mlt_consumer self )
415 return self != NULL ? MLT_SERVICE_PROPERTIES( &self->parent ) : NULL;
418 /** Connect the consumer to the producer.
420 * \public \memberof mlt_consumer_s
421 * \param self a consumer
422 * \param producer a producer
423 * \return > 0 warning, == 0 success, < 0 serious error,
424 * 1 = this service does not accept input,
425 * 2 = the producer is invalid,
426 * 3 = the producer is already registered with this consumer
429 int mlt_consumer_connect( mlt_consumer self, mlt_service producer )
431 return mlt_service_connect_producer( &self->parent, producer, 0 );
434 /** Start the consumer.
436 * \public \memberof mlt_consumer_s
437 * \param self a consumer
438 * \return true if there was an error
441 int mlt_consumer_start( mlt_consumer self )
443 if ( !mlt_consumer_is_stopped( self ) )
446 consumer_private *priv = self->local;
448 // Stop listening to the property-changed event
449 mlt_event_block( priv->event_listener );
452 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
454 // Determine if there's a test card producer
455 char *test_card = mlt_properties_get( properties, "test_card" );
457 // Just to make sure nothing is hanging around...
458 pthread_mutex_lock( &priv->put_mutex );
460 priv->put_active = 1;
461 pthread_mutex_unlock( &priv->put_mutex );
464 if ( test_card != NULL )
466 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
468 // Create a test card producer
469 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
470 mlt_producer producer = mlt_factory_producer( profile, NULL, test_card );
472 // Do we have a producer
473 if ( producer != NULL )
475 // Test card should loop I guess...
476 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
477 //mlt_producer_set_speed( producer, 0 );
478 //mlt_producer_set_in_and_out( producer, 0, 0 );
480 // Set the test card on the consumer
481 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
487 // Allow the hash table to speed things up
488 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
491 // The profile could have changed between a stop and a restart.
492 apply_profile_properties( self, mlt_service_profile( MLT_CONSUMER_SERVICE(self) ), properties );
494 // Set the frame duration in microseconds for the frame-dropping heuristic
495 int frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
496 int frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
497 int frame_duration = 0;
499 if ( frame_rate_num && frame_rate_den )
501 frame_duration = 1000000 / frame_rate_num * frame_rate_den;
504 mlt_properties_set_int( properties, "frame_duration", frame_duration );
506 // Check and run an ante command
507 if ( mlt_properties_get( properties, "ante" ) )
508 if ( system( mlt_properties_get( properties, "ante" ) ) == -1 )
509 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "ante" ) );
511 // Set the real_time preference
512 priv->real_time = mlt_properties_get_int( properties, "real_time" );
514 // For worker threads implementation, buffer must be at least # threads
515 if ( abs( priv->real_time ) > 1 && mlt_properties_get_int( properties, "buffer" ) <= abs( priv->real_time ) )
516 mlt_properties_set_int( properties, "_buffer", abs( priv->real_time ) + 1 );
518 // Get the image format to use for rendering threads
519 const char* format = mlt_properties_get( properties, "mlt_image_format" );
522 if ( !strcmp( format, "rgb24" ) )
523 priv->format = mlt_image_rgb24;
524 else if ( !strcmp( format, "rgb24a" ) )
525 priv->format = mlt_image_rgb24a;
526 else if ( !strcmp( format, "yuv420p" ) )
527 priv->format = mlt_image_yuv420p;
528 else if ( !strcmp( format, "none" ) )
529 priv->format = mlt_image_none;
530 else if ( !strcmp( format, "glsl" ) )
531 priv->format = mlt_image_glsl_texture;
533 priv->format = mlt_image_yuv422;
537 if ( self->start != NULL )
538 return self->start( self );
543 /** An alternative method to feed frames into the consumer.
545 * Only valid if the consumer itself is not connected.
547 * \public \memberof mlt_consumer_s
548 * \param self a consumer
549 * \param frame a frame
550 * \return true (ignore self for now)
553 int mlt_consumer_put_frame( mlt_consumer self, mlt_frame frame )
557 // Get the service assoicated to the consumer
558 mlt_service service = MLT_CONSUMER_SERVICE( self );
560 if ( mlt_service_producer( service ) == NULL )
564 consumer_private *priv = self->local;
566 pthread_mutex_lock( &priv->put_mutex );
567 while ( priv->put_active && priv->put != NULL )
569 gettimeofday( &now, NULL );
570 tm.tv_sec = now.tv_sec + 1;
571 tm.tv_nsec = now.tv_usec * 1000;
572 pthread_cond_timedwait( &priv->put_cond, &priv->put_mutex, &tm );
574 if ( priv->put_active && priv->put == NULL )
577 mlt_frame_close( frame );
578 pthread_cond_broadcast( &priv->put_cond );
579 pthread_mutex_unlock( &priv->put_mutex );
583 mlt_frame_close( frame );
589 /** Protected method for consumer to get frames from connected service
591 * \public \memberof mlt_consumer_s
592 * \param self a consumer
596 mlt_frame mlt_consumer_get_frame( mlt_consumer self )
599 mlt_frame frame = NULL;
601 // Get the service assoicated to the consumer
602 mlt_service service = MLT_CONSUMER_SERVICE( self );
604 // Get the consumer properties
605 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
608 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
612 consumer_private *priv = self->local;
614 pthread_mutex_lock( &priv->put_mutex );
615 while ( priv->put_active && priv->put == NULL )
617 gettimeofday( &now, NULL );
618 tm.tv_sec = now.tv_sec + 1;
619 tm.tv_nsec = now.tv_usec * 1000;
620 pthread_cond_timedwait( &priv->put_cond, &priv->put_mutex, &tm );
624 pthread_cond_broadcast( &priv->put_cond );
625 pthread_mutex_unlock( &priv->put_mutex );
627 mlt_service_apply_filters( service, frame, 0 );
629 else if ( mlt_service_producer( service ) != NULL )
631 mlt_service_get_frame( service, &frame, 0 );
635 frame = mlt_frame_init( service );
640 // Get the frame properties
641 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
643 // Get the test card producer
644 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
646 // Attach the test frame producer to it.
647 if ( test_card != NULL )
648 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
650 // Pass along the interpolation and deinterlace options
651 // TODO: get rid of consumer_deinterlace and use profile.progressive
652 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
653 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
654 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
655 mlt_properties_set_int( frame_properties, "consumer_tff", mlt_properties_get_int( properties, "top_field_first" ) );
662 /** Compute the time difference between now and a time value.
664 * \private \memberof mlt_consumer_s
665 * \param time1 a time value to be compared against now
666 * \return the difference in microseconds
669 static inline long time_difference( struct timeval *time1 )
671 struct timeval time2;
672 time2.tv_sec = time1->tv_sec;
673 time2.tv_usec = time1->tv_usec;
674 gettimeofday( time1, NULL );
675 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
678 /** The thread procedure for asynchronously pulling frames through the service
679 * network connected to a consumer.
681 * \private \memberof mlt_consumer_s
682 * \param arg a consumer
685 static void *consumer_read_ahead_thread( void *arg )
687 // The argument is the consumer
688 mlt_consumer self = arg;
689 consumer_private *priv = self->local;
691 // Get the properties of the consumer
692 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
694 // Get the width and height
695 int width = mlt_properties_get_int( properties, "width" );
696 int height = mlt_properties_get_int( properties, "height" );
698 // See if video is turned off
699 int video_off = mlt_properties_get_int( properties, "video_off" );
700 int preview_off = mlt_properties_get_int( properties, "preview_off" );
701 int preview_format = mlt_properties_get_int( properties, "preview_format" );
703 // Get the audio settings
704 mlt_audio_format afmt = mlt_audio_s16;
705 const char *format = mlt_properties_get( properties, "mlt_audio_format" );
708 if ( !strcmp( format, "none" ) )
709 afmt = mlt_audio_none;
710 else if ( !strcmp( format, "s32" ) )
711 afmt = mlt_audio_s32;
712 else if ( !strcmp( format, "s32le" ) )
713 afmt = mlt_audio_s32le;
714 else if ( !strcmp( format, "float" ) )
715 afmt = mlt_audio_float;
716 else if ( !strcmp( format, "f32le" ) )
717 afmt = mlt_audio_f32le;
718 else if ( !strcmp( format, "u8" ) )
722 double fps = mlt_properties_get_double( properties, "fps" );
723 int channels = mlt_properties_get_int( properties, "channels" );
724 int frequency = mlt_properties_get_int( properties, "frequency" );
728 // See if audio is turned off
729 int audio_off = mlt_properties_get_int( properties, "audio_off" );
731 // Get the maximum size of the buffer
732 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
734 // General frame variable
735 mlt_frame frame = NULL;
736 uint8_t *image = NULL;
741 // Average time for get_frame and get_image
744 int64_t time_process = 0;
746 mlt_position pos = 0;
747 mlt_position start_pos = 0;
748 mlt_position last_pos = 0;
749 int frame_duration = mlt_properties_get_int( properties, "frame_duration" );
750 int drop_max = mlt_properties_get_int( properties, "drop_max" );
752 if ( preview_off && preview_format != 0 )
753 priv->format = preview_format;
755 mlt_events_fire( properties, "consumer-thread-started", NULL );
757 // Get the first frame
758 frame = mlt_consumer_get_frame( self );
762 // Get the image of the first frame
765 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
766 mlt_frame_get_image( frame, &image, &priv->format, &width, &height, 0 );
771 samples = mlt_sample_calculator( fps, frequency, counter++ );
772 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
776 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
777 last_pos = start_pos = pos = mlt_frame_get_position( frame );
780 // Get the starting time (can ignore the times above)
781 gettimeofday( &ante, NULL );
783 // Continue to read ahead
784 while ( priv->ahead )
786 // Put the current frame into the queue
787 pthread_mutex_lock( &priv->queue_mutex );
788 while( priv->ahead && mlt_deque_count( priv->queue ) >= buffer )
789 pthread_cond_wait( &priv->queue_cond, &priv->queue_mutex );
790 if ( priv->is_purge )
792 mlt_frame_close( frame );
797 mlt_deque_push_back( priv->queue, frame );
799 pthread_cond_broadcast( &priv->queue_cond );
800 pthread_mutex_unlock( &priv->queue_mutex );
802 // Get the next frame
803 frame = mlt_consumer_get_frame( self );
805 // If there's no frame, we're probably stopped...
808 pos = mlt_frame_get_position( frame );
810 // WebVfx uses this to setup a consumer-stopping event handler.
811 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "consumer", self, 0, NULL, NULL );
813 // Increment the counter used for averaging processing cost
816 // All non-normal playback frames should be shown
817 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
819 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
820 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
822 // Indicate seeking or trick-play
826 // If skip flag not set or frame-dropping disabled
827 if ( !skip_next || priv->real_time == -1 )
831 // Reset width/height - could have been changed by previous mlt_frame_get_image
832 width = mlt_properties_get_int( properties, "width" );
833 height = mlt_properties_get_int( properties, "height" );
836 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
837 mlt_frame_get_image( frame, &image, &priv->format, &width, &height, 0 );
840 // Indicate the rendered image is available.
841 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
843 // Reset consecutively-skipped counter
846 else // Skip image processing
848 // Increment the number of consecutively-skipped frames
851 // If too many (1 sec) consecutively-skipped frames
852 if ( skipped > drop_max )
854 // Reset cost tracker
857 mlt_log_verbose( self, "too many frames dropped - forcing next frame\n" );
861 // Always process audio
864 samples = mlt_sample_calculator( fps, frequency, counter++ );
865 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
868 // Get the time to process this frame
869 int64_t time_current = time_difference( &ante );
871 // If the current time is not suddenly some large amount
872 if ( time_current < time_process / count * 20 || !time_process || count < 5 )
874 // Accumulate the cost for processing this frame
875 time_process += time_current;
879 mlt_log_debug( self, "current %"PRId64" threshold %"PRId64" count %d\n",
880 time_current, (int64_t) (time_process / count * 20), count );
881 // Ignore the cost of this frame's time
885 // Determine if we started, resumed, or seeked
886 if ( pos != last_pos + 1 )
890 // Do not skip the first 20% of buffer at start, resume, or seek
891 if ( pos - start_pos <= buffer / 5 + 1 )
893 // Reset cost tracker
901 // Only consider skipping if the buffer level is low (or really small)
902 if ( mlt_deque_count( priv->queue ) <= buffer / 5 + 1 )
904 // Skip next frame if average cost exceeds frame duration.
905 if ( time_process / count > frame_duration )
908 mlt_log_debug( self, "avg usec %"PRId64" (%"PRId64"/%d) duration %d\n",
909 time_process/count, time_process, count, frame_duration);
913 // Remove the last frame
914 mlt_frame_close( frame );
919 /** Locate the first unprocessed frame in the queue.
921 * When playing with realtime behavior, we do not use the true head, but
922 * rather an adjusted process_head. The process_head is adjusted based on
923 * the rate of frame-dropping or recovery from frame-dropping. The idea is
924 * that as the level of frame-dropping increases to move the process_head
925 * closer to the tail because the frames are not completing processing prior
926 * to their playout! Then, as frames are not dropped the process_head moves
927 * back closer to the head of the queue so that worker threads can work
928 * ahead of the playout point (queue head).
930 * \private \memberof mlt_consumer_s
931 * \param self a consumer
932 * \return an index into the queue
935 static inline int first_unprocessed_frame( mlt_consumer self )
937 consumer_private *priv = self->local;
938 int index = priv->real_time <= 0 ? 0 : priv->process_head;
939 while ( index < mlt_deque_count( priv->queue ) && MLT_FRAME( mlt_deque_peek( priv->queue, index ) )->is_processing )
944 /** The worker thread procedure for parallel processing frames.
946 * \private \memberof mlt_consumer_s
947 * \param arg a consumer
950 static void *consumer_worker_thread( void *arg )
952 // The argument is the consumer
953 mlt_consumer self = arg;
954 consumer_private *priv = self->local;
956 // Get the properties of the consumer
957 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
959 // Get the width and height
960 int width = mlt_properties_get_int( properties, "width" );
961 int height = mlt_properties_get_int( properties, "height" );
962 mlt_image_format format = priv->format;
964 // See if video is turned off
965 int video_off = mlt_properties_get_int( properties, "video_off" );
966 int preview_off = mlt_properties_get_int( properties, "preview_off" );
967 int preview_format = mlt_properties_get_int( properties, "preview_format" );
969 // General frame variable
970 mlt_frame frame = NULL;
971 uint8_t *image = NULL;
973 if ( preview_off && preview_format != 0 )
974 format = preview_format;
976 mlt_events_fire( properties, "consumer-thread-started", NULL );
978 // Continue to read ahead
979 while ( priv->ahead )
981 // Get the next unprocessed frame from the work queue
982 pthread_mutex_lock( &priv->queue_mutex );
983 int index = first_unprocessed_frame( self );
984 while ( priv->ahead && index >= mlt_deque_count( priv->queue ) )
986 mlt_log_debug( MLT_CONSUMER_SERVICE(self), "waiting in worker index = %d queue count = %d\n",
987 index, mlt_deque_count( priv->queue ) );
988 pthread_cond_wait( &priv->queue_cond, &priv->queue_mutex );
989 index = first_unprocessed_frame( self );
992 // Mark the frame for processing
993 frame = mlt_deque_peek( priv->queue, index );
996 mlt_log_debug( MLT_CONSUMER_SERVICE(self), "worker processing index = %d frame " MLT_POSITION_FMT " queue count = %d\n",
997 index, mlt_frame_get_position(frame), mlt_deque_count( priv->queue ) );
998 frame->is_processing = 1;
999 mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( frame ) );
1001 pthread_mutex_unlock( &priv->queue_mutex );
1003 // If there's no frame, we're probably stopped...
1004 if ( frame == NULL )
1007 // WebVfx uses this to setup a consumer-stopping event handler.
1008 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "consumer", self, 0, NULL, NULL );
1010 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
1011 // All non normal playback frames should be shown
1012 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
1013 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
1019 // Fetch width/height again
1020 width = mlt_properties_get_int( properties, "width" );
1021 height = mlt_properties_get_int( properties, "height" );
1022 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
1023 mlt_frame_get_image( frame, &image, &format, &width, &height, 0 );
1025 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
1026 mlt_frame_close( frame );
1028 // Tell a waiting thread (non-realtime main consumer thread) that we are done.
1029 pthread_mutex_lock( &priv->done_mutex );
1030 pthread_cond_broadcast( &priv->done_cond );
1031 pthread_mutex_unlock( &priv->done_mutex );
1037 /** Start the read/render thread.
1039 * \private \memberof mlt_consumer_s
1040 * \param self a consumer
1043 static void consumer_read_ahead_start( mlt_consumer self )
1045 consumer_private *priv = self->local;
1047 if ( priv->started )
1050 // We're running now
1053 // Create the frame queue
1054 priv->queue = mlt_deque_init( );
1056 // Create the queue mutex
1057 pthread_mutex_init( &priv->queue_mutex, NULL );
1059 // Create the condition
1060 pthread_cond_init( &priv->queue_cond, NULL );
1062 // Create the read ahead
1063 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( self ), "priority" ) )
1065 struct sched_param priority;
1066 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( self ), "priority" );
1067 pthread_attr_t thread_attributes;
1068 pthread_attr_init( &thread_attributes );
1069 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
1070 pthread_attr_setschedparam( &thread_attributes, &priority );
1071 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
1072 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
1073 if ( pthread_create( &priv->ahead_thread, &thread_attributes, consumer_read_ahead_thread, self ) < 0 )
1074 pthread_create( &priv->ahead_thread, NULL, consumer_read_ahead_thread, self );
1075 pthread_attr_destroy( &thread_attributes );
1079 pthread_create( &priv->ahead_thread, NULL, consumer_read_ahead_thread, self );
1084 /** Start the worker threads.
1086 * \private \memberof mlt_consumer_s
1087 * \param self a consumer
1090 static void consumer_work_start( mlt_consumer self )
1092 consumer_private *priv = self->local;
1093 int n = abs( priv->real_time );
1096 if ( priv->started )
1099 thread = calloc( 1, sizeof( pthread_t ) * n );
1101 // We're running now
1103 priv->threads = thread;
1105 // These keep track of the accelleration of frame dropping or recovery.
1106 priv->consecutive_dropped = 0;
1107 priv->consecutive_rendered = 0;
1109 // This is the position in the queue from which to look for a frame to process.
1110 // If we always start from the head, then we may likely not complete processing
1111 // before the frame is played out.
1112 priv->process_head = 0;
1114 // Create the queues
1115 priv->queue = mlt_deque_init();
1116 priv->worker_threads = mlt_deque_init();
1118 // Create the mutexes
1119 pthread_mutex_init( &priv->queue_mutex, NULL );
1120 pthread_mutex_init( &priv->done_mutex, NULL );
1122 // Create the conditions
1123 pthread_cond_init( &priv->queue_cond, NULL );
1124 pthread_cond_init( &priv->done_cond, NULL );
1126 // Create the read ahead
1127 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( self ), "priority" ) )
1130 struct sched_param priority;
1131 pthread_attr_t thread_attributes;
1133 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( self ), "priority" );
1134 pthread_attr_init( &thread_attributes );
1135 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
1136 pthread_attr_setschedparam( &thread_attributes, &priority );
1137 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
1138 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
1142 if ( pthread_create( thread, &thread_attributes, consumer_worker_thread, self ) < 0 )
1143 if ( pthread_create( thread, NULL, consumer_worker_thread, self ) == 0 )
1144 mlt_deque_push_back( priv->worker_threads, thread );
1147 pthread_attr_destroy( &thread_attributes );
1154 if ( pthread_create( thread, NULL, consumer_worker_thread, self ) == 0 )
1155 mlt_deque_push_back( priv->worker_threads, thread );
1162 /** Stop the read/render thread.
1164 * \private \memberof mlt_consumer_s
1165 * \param self a consumer
1168 static void consumer_read_ahead_stop( mlt_consumer self )
1170 consumer_private *priv = self->local;
1172 // Make sure we're running
1173 // TODO improve support for atomic ops in general (see libavutil/atomic.h)
1174 #ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
1175 if ( __sync_val_compare_and_swap( &priv->started, 1, 0 ) )
1178 if ( priv->started )
1182 // Inform thread to stop
1184 mlt_events_fire( MLT_CONSUMER_PROPERTIES(self), "consumer-stopping", NULL );
1186 // Broadcast to the condition in case it's waiting
1187 pthread_mutex_lock( &priv->queue_mutex );
1188 pthread_cond_broadcast( &priv->queue_cond );
1189 pthread_mutex_unlock( &priv->queue_mutex );
1191 // Broadcast to the put condition in case it's waiting
1192 pthread_mutex_lock( &priv->put_mutex );
1193 pthread_cond_broadcast( &priv->put_cond );
1194 pthread_mutex_unlock( &priv->put_mutex );
1197 pthread_join( priv->ahead_thread, NULL );
1199 // Destroy the frame queue mutex
1200 pthread_mutex_destroy( &priv->queue_mutex );
1202 // Destroy the condition
1203 pthread_cond_destroy( &priv->queue_cond );
1206 while ( mlt_deque_count( priv->queue ) )
1207 mlt_frame_close( mlt_deque_pop_back( priv->queue ) );
1210 mlt_deque_close( priv->queue );
1212 mlt_events_fire( MLT_CONSUMER_PROPERTIES(self), "consumer-thread-stopped", NULL );
1216 /** Stop the worker threads.
1218 * \private \memberof mlt_consumer_s
1219 * \param self a consumer
1222 static void consumer_work_stop( mlt_consumer self )
1224 consumer_private *priv = self->local;
1226 // Make sure we're running
1227 #ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
1228 if ( __sync_val_compare_and_swap( &priv->started, 1, 0 ) )
1231 if ( priv->started )
1235 // Inform thread to stop
1237 mlt_events_fire( MLT_CONSUMER_PROPERTIES(self), "consumer-stopping", NULL );
1239 // Broadcast to the queue condition in case it's waiting
1240 pthread_mutex_lock( &priv->queue_mutex );
1241 pthread_cond_broadcast( &priv->queue_cond );
1242 pthread_mutex_unlock( &priv->queue_mutex );
1244 // Broadcast to the put condition in case it's waiting
1245 pthread_mutex_lock( &priv->put_mutex );
1246 pthread_cond_broadcast( &priv->put_cond );
1247 pthread_mutex_unlock( &priv->put_mutex );
1249 // Broadcast to the done condition in case it's waiting
1250 pthread_mutex_lock( &priv->done_mutex );
1251 pthread_cond_broadcast( &priv->done_cond );
1252 pthread_mutex_unlock( &priv->done_mutex );
1256 while ( ( thread = mlt_deque_pop_back( priv->worker_threads ) ) )
1257 pthread_join( *thread, NULL );
1259 // Deallocate the array of threads
1260 if ( priv->threads )
1261 free( priv->threads );
1263 // Destroy the mutexes
1264 pthread_mutex_destroy( &priv->queue_mutex );
1265 pthread_mutex_destroy( &priv->done_mutex );
1267 // Destroy the conditions
1268 pthread_cond_destroy( &priv->queue_cond );
1269 pthread_cond_destroy( &priv->done_cond );
1272 while ( mlt_deque_count( priv->queue ) )
1273 mlt_frame_close( mlt_deque_pop_back( priv->queue ) );
1276 mlt_deque_close( priv->queue );
1277 mlt_deque_close( priv->worker_threads );
1279 mlt_events_fire( MLT_CONSUMER_PROPERTIES(self), "consumer-thread-stopped", NULL );
1283 /** Flush the read/render thread's buffer.
1285 * \public \memberof mlt_consumer_s
1286 * \param self a consumer
1289 void mlt_consumer_purge( mlt_consumer self )
1293 consumer_private *priv = self->local;
1295 pthread_mutex_lock( &priv->put_mutex );
1297 mlt_frame_close( priv->put );
1300 pthread_cond_broadcast( &priv->put_cond );
1301 pthread_mutex_unlock( &priv->put_mutex );
1304 self->purge( self );
1306 if ( priv->started && priv->real_time )
1307 pthread_mutex_lock( &priv->queue_mutex );
1309 while ( priv->started && mlt_deque_count( priv->queue ) )
1310 mlt_frame_close( mlt_deque_pop_back( priv->queue ) );
1312 if ( priv->started && priv->real_time )
1315 pthread_cond_broadcast( &priv->queue_cond );
1316 pthread_mutex_unlock( &priv->queue_mutex );
1317 if ( abs( priv->real_time ) > 1 )
1319 pthread_mutex_lock( &priv->done_mutex );
1320 pthread_cond_broadcast( &priv->done_cond );
1321 pthread_mutex_unlock( &priv->done_mutex );
1325 pthread_mutex_lock( &priv->put_mutex );
1327 mlt_frame_close( priv->put );
1330 pthread_cond_broadcast( &priv->put_cond );
1331 pthread_mutex_unlock( &priv->put_mutex );
1335 /** Use multiple worker threads and a work queue.
1338 static mlt_frame worker_get_frame( mlt_consumer self, mlt_properties properties )
1341 mlt_frame frame = NULL;
1342 consumer_private *priv = self->local;
1343 double fps = mlt_properties_get_double( properties, "fps" );
1344 int threads = abs( priv->real_time );
1345 int buffer = mlt_properties_get_int( properties, "_buffer" );
1346 buffer = buffer > 0 ? buffer : mlt_properties_get_int( properties, "buffer" );
1347 // This is a heuristic to determine a suitable minimum buffer size for the number of threads.
1348 int headroom = 2 + threads * threads;
1349 buffer = buffer < headroom ? headroom : buffer;
1351 // Start worker threads if not already started.
1352 if ( ! priv->ahead )
1354 int prefill = mlt_properties_get_int( properties, "prefill" );
1355 prefill = prefill > 0 && prefill < buffer ? prefill : buffer;
1357 consumer_work_start( self );
1359 // Fill the work queue.
1361 while ( priv->ahead && i-- )
1363 frame = mlt_consumer_get_frame( self );
1366 pthread_mutex_lock( &priv->queue_mutex );
1367 mlt_deque_push_back( priv->queue, frame );
1368 pthread_cond_signal( &priv->queue_cond );
1369 pthread_mutex_unlock( &priv->queue_mutex );
1374 while ( priv->ahead && first_unprocessed_frame( self ) < prefill )
1376 pthread_mutex_lock( &priv->done_mutex );
1377 pthread_cond_wait( &priv->done_cond, &priv->done_mutex );
1378 pthread_mutex_unlock( &priv->done_mutex );
1380 priv->process_head = threads;
1383 // mlt_log_verbose( MLT_CONSUMER_SERVICE(self), "size %d done count %d work count %d process_head %d\n",
1384 // threads, first_unprocessed_frame( self ), mlt_deque_count( priv->queue ), priv->process_head );
1386 // Feed the work queue
1387 while ( priv->ahead && mlt_deque_count( priv->queue ) < buffer )
1389 frame = mlt_consumer_get_frame( self );
1392 pthread_mutex_lock( &priv->queue_mutex );
1393 mlt_deque_push_back( priv->queue, frame );
1394 pthread_cond_signal( &priv->queue_cond );
1395 pthread_mutex_unlock( &priv->queue_mutex );
1399 // Wait if not realtime.
1400 while ( priv->ahead && priv->real_time < 0 && !priv->is_purge &&
1401 !( mlt_properties_get_int( MLT_FRAME_PROPERTIES( MLT_FRAME( mlt_deque_peek_front( priv->queue ) ) ), "rendered" ) ) )
1403 pthread_mutex_lock( &priv->done_mutex );
1404 pthread_cond_wait( &priv->done_cond, &priv->done_mutex );
1405 pthread_mutex_unlock( &priv->done_mutex );
1408 // Get the frame from the queue.
1409 pthread_mutex_lock( &priv->queue_mutex );
1410 frame = mlt_deque_pop_front( priv->queue );
1411 pthread_mutex_unlock( &priv->queue_mutex );
1417 // Adapt the worker process head to the runtime conditions.
1418 if ( priv->real_time > 0 )
1420 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "rendered" ) )
1422 priv->consecutive_dropped = 0;
1423 if ( priv->process_head > threads && priv->consecutive_rendered >= priv->process_head )
1424 priv->process_head--;
1426 priv->consecutive_rendered++;
1430 priv->consecutive_rendered = 0;
1431 if ( priv->process_head < buffer - threads && priv->consecutive_dropped > threads )
1432 priv->process_head++;
1434 priv->consecutive_dropped++;
1436 // mlt_log_verbose( MLT_CONSUMER_SERVICE(self), "dropped %d rendered %d process_head %d\n",
1437 // priv->consecutive_dropped, priv->consecutive_rendered, priv->process_head );
1439 // Check for too many consecutively dropped frames
1440 if ( priv->consecutive_dropped > mlt_properties_get_int( properties, "drop_max" ) )
1442 int orig_buffer = mlt_properties_get_int( properties, "buffer" );
1443 int prefill = mlt_properties_get_int( properties, "prefill" );
1444 mlt_log_verbose( self, "too many frames dropped - " );
1446 // If using a default low-latency buffer level (SDL) and below the limit
1447 if ( ( orig_buffer == 1 || prefill == 1 ) && buffer < (threads + 1) * 10 )
1449 // Auto-scale the buffer to compensate
1450 mlt_log_verbose( self, "increasing buffer to %d\n", buffer + threads );
1451 mlt_properties_set_int( properties, "_buffer", buffer + threads );
1452 priv->consecutive_dropped = fps / 2;
1456 // Tell the consumer to render it
1457 mlt_log_verbose( self, "forcing next frame\n" );
1458 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
1459 priv->consecutive_dropped = 0;
1463 if ( priv->is_purge ) {
1465 mlt_frame_close( frame );
1471 /** Get the next frame from the producer connected to a consumer.
1473 * Typically, one uses this instead of \p mlt_consumer_get_frame to make
1474 * the asynchronous/real-time behavior configurable at runtime.
1475 * You should close the frame returned from this when you are done with it.
1477 * \public \memberof mlt_consumer_s
1478 * \param self a consumer
1482 mlt_frame mlt_consumer_rt_frame( mlt_consumer self )
1485 mlt_frame frame = NULL;
1487 // Get the properties
1488 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
1489 consumer_private *priv = self->local;
1491 // Check if the user has requested real time or not
1492 if ( priv->real_time > 1 || priv->real_time < -1 )
1495 return worker_get_frame( self, properties );
1497 else if ( priv->real_time == 1 || priv->real_time == -1 )
1501 // Is the read ahead running?
1502 if ( priv->ahead == 0 )
1504 int buffer = mlt_properties_get_int( properties, "buffer" );
1505 int prefill = mlt_properties_get_int( properties, "prefill" );
1506 consumer_read_ahead_start( self );
1508 size = prefill > 0 && prefill < buffer ? prefill : buffer;
1511 // Get frame from queue
1512 pthread_mutex_lock( &priv->queue_mutex );
1513 while( priv->ahead && mlt_deque_count( priv->queue ) < size )
1514 pthread_cond_wait( &priv->queue_cond, &priv->queue_mutex );
1515 frame = mlt_deque_pop_front( priv->queue );
1516 pthread_cond_broadcast( &priv->queue_cond );
1517 pthread_mutex_unlock( &priv->queue_mutex );
1519 else // real_time == 0
1524 mlt_events_fire( properties, "consumer-thread-started", NULL );
1526 // Get the frame in non real time
1527 frame = mlt_consumer_get_frame( self );
1529 // This isn't true, but from the consumers perspective it is
1530 if ( frame != NULL )
1532 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
1534 // WebVfx uses this to setup a consumer-stopping event handler.
1535 mlt_properties_set_data( MLT_FRAME_PROPERTIES( frame ), "consumer", self, 0, NULL, NULL );
1542 /** Callback for the implementation to indicate a stopped condition.
1544 * \public \memberof mlt_consumer_s
1545 * \param self a consumer
1548 void mlt_consumer_stopped( mlt_consumer self )
1550 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( self ), "running", 0 );
1551 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-stopped", NULL );
1552 mlt_event_unblock( ( ( consumer_private* ) self->local )->event_listener );
1555 /** Stop the consumer.
1557 * \public \memberof mlt_consumer_s
1558 * \param self a consumer
1559 * \return true if there was an error
1562 int mlt_consumer_stop( mlt_consumer self )
1564 // Get the properies
1565 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
1566 consumer_private *priv = self->local;
1569 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping put waiting\n" );
1570 pthread_mutex_lock( &priv->put_mutex );
1571 priv->put_active = 0;
1572 pthread_cond_broadcast( &priv->put_cond );
1573 pthread_mutex_unlock( &priv->put_mutex );
1575 // Stop the consumer
1576 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping consumer\n" );
1578 // Cancel the read ahead threads
1579 if ( priv->started )
1581 // Unblock the consumer calling mlt_consumer_rt_frame
1582 pthread_mutex_lock( &priv->queue_mutex );
1583 pthread_cond_broadcast( &priv->queue_cond );
1584 pthread_mutex_unlock( &priv->queue_mutex );
1587 // Invoke the child callback
1588 if ( self->stop != NULL )
1591 // Check if the user has requested real time or not and stop if necessary
1592 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping read_ahead\n" );
1593 if ( abs( priv->real_time ) == 1 )
1594 consumer_read_ahead_stop( self );
1595 else if ( abs( priv->real_time ) > 1 )
1596 consumer_work_stop( self );
1598 // Kill the test card
1599 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
1601 // Check and run a post command
1602 if ( mlt_properties_get( properties, "post" ) )
1603 if (system( mlt_properties_get( properties, "post" ) ) == -1 )
1604 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "post" ) );
1606 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopped\n" );
1611 /** Determine if the consumer is stopped.
1613 * \public \memberof mlt_consumer_s
1614 * \param self a consumer
1615 * \return true if the consumer is stopped
1618 int mlt_consumer_is_stopped( mlt_consumer self )
1620 // Check if the consumer is stopped
1621 if ( self && self->is_stopped )
1622 return self->is_stopped( self );
1627 /** Close and destroy the consumer.
1629 * \public \memberof mlt_consumer_s
1630 * \param self a consumer
1633 void mlt_consumer_close( mlt_consumer self )
1635 if ( self != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( self ) ) <= 0 )
1637 // Get the childs close function
1638 void ( *consumer_close )( ) = self->close;
1640 if ( consumer_close )
1643 //mlt_consumer_stop( self );
1646 consumer_close( self );
1650 consumer_private *priv = self->local;
1652 // Make sure it only gets called once
1653 self->parent.close = NULL;
1655 // Destroy the push mutex and condition
1656 pthread_mutex_destroy( &priv->put_mutex );
1657 pthread_cond_destroy( &priv->put_cond );
1659 mlt_service_close( &self->parent );
1665 /** Get the position of the last frame shown.
1667 * \public \memberof mlt_consumer_s
1668 * \param consumer a consumer
1669 * \return the position
1672 mlt_position mlt_consumer_position( mlt_consumer consumer )
1674 return ( ( consumer_private* ) consumer->local )->position;