3 * \brief abstraction for all consumer services
6 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "mlt_consumer.h"
25 #include "mlt_factory.h"
26 #include "mlt_producer.h"
27 #include "mlt_frame.h"
28 #include "mlt_profile.h"
36 /** Define this if you want an automatic deinterlace (if necessary) when the
37 * consumer's producer is not running at normal speed.
39 #undef DEINTERLACE_ON_NOT_NORMAL_SPEED
41 /** This is not the ideal place for this, but it is needed by VDPAU as well.
43 pthread_mutex_t mlt_sdl_mutex = PTHREAD_MUTEX_INITIALIZER;
45 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
46 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
47 static void mlt_consumer_property_changed( mlt_properties owner, mlt_consumer this, char *name );
48 static void apply_profile_properties( mlt_consumer this, mlt_profile profile, mlt_properties properties );
49 static void on_consumer_frame_show( mlt_properties owner, mlt_consumer this, mlt_frame frame );
51 /** Initialize a consumer service.
53 * \public \memberof mlt_consumer_s
54 * \param this the consumer to initialize
55 * \param child a pointer to the object for the subclass
56 * \param profile the \p mlt_profile_s to use (optional but recommended,
57 * uses the environment variable MLT if this is NULL)
58 * \return true if there was an error
61 int mlt_consumer_init( mlt_consumer this, void *child, mlt_profile profile )
64 memset( this, 0, sizeof( struct mlt_consumer_s ) );
66 error = mlt_service_init( &this->parent, this );
69 // Get the properties from the service
70 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
72 // Apply profile to properties
73 if ( profile == NULL )
75 // Normally the application creates the profile and controls its lifetime
76 // This is the fallback exception handling
77 profile = mlt_profile_init( NULL );
78 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
79 mlt_properties_set_data( properties, "_profile", profile, 0, (mlt_destructor)mlt_profile_close, NULL );
81 apply_profile_properties( this, profile, properties );
83 // Default rescaler for all consumers
84 mlt_properties_set( properties, "rescale", "bilinear" );
86 // Default read ahead buffer size
87 mlt_properties_set_int( properties, "buffer", 25 );
89 // Default audio frequency and channels
90 mlt_properties_set_int( properties, "frequency", 48000 );
91 mlt_properties_set_int( properties, "channels", 2 );
93 // Default of all consumers is real time
94 mlt_properties_set_int( properties, "real_time", 1 );
96 // Default to environment test card
97 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
99 // Hmm - default all consumers to yuv422 :-/
100 this->format = mlt_image_yuv422;
102 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
103 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
104 mlt_events_register( properties, "consumer-stopped", NULL );
105 mlt_events_listen( properties, this, "consumer-frame-show", ( mlt_listener )on_consumer_frame_show );
107 // Register a property-changed listener to handle the profile property -
108 // subsequent properties can override the profile
109 this->event_listener = mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_consumer_property_changed );
111 // Create the push mutex and condition
112 pthread_mutex_init( &this->put_mutex, NULL );
113 pthread_cond_init( &this->put_cond, NULL );
119 /** Convert the profile into properties on the consumer.
121 * \private \memberof mlt_consumer_s
122 * \param this a consumer
123 * \param profile a profile
124 * \param properties a properties list (typically, the consumer's)
127 static void apply_profile_properties( mlt_consumer this, mlt_profile profile, mlt_properties properties )
129 mlt_event_block( this->event_listener );
130 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
131 mlt_properties_set_int( properties, "frame_rate_num", profile->frame_rate_num );
132 mlt_properties_set_int( properties, "frame_rate_den", profile->frame_rate_den );
133 mlt_properties_set_int( properties, "width", profile->width );
134 mlt_properties_set_int( properties, "height", profile->height );
135 mlt_properties_set_int( properties, "progressive", profile->progressive );
136 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
137 mlt_properties_set_int( properties, "sample_aspect_num", profile->sample_aspect_num );
138 mlt_properties_set_int( properties, "sample_aspect_den", profile->sample_aspect_den );
139 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
140 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
141 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
142 mlt_properties_set_int( properties, "colorspace", profile->colorspace );
143 mlt_event_unblock( this->event_listener );
146 /** The property-changed event listener
148 * \private \memberof mlt_consumer_s
149 * \param owner the events object
150 * \param this the consumer
151 * \param name the name of the property that changed
154 static void mlt_consumer_property_changed( mlt_properties owner, mlt_consumer this, char *name )
156 if ( !strcmp( name, "profile" ) )
159 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
161 // Get the current profile
162 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
164 // Load the new profile
165 mlt_profile new_profile = mlt_profile_init( mlt_properties_get( properties, name ) );
170 if ( profile != NULL )
172 free( profile->description );
173 memcpy( profile, new_profile, sizeof( struct mlt_profile_s ) );
174 profile->description = strdup( new_profile->description );
175 mlt_profile_close( new_profile );
179 profile = new_profile;
182 // Apply to properties
183 apply_profile_properties( this, profile, properties );
186 else if ( !strcmp( name, "frame_rate_num" ) )
188 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
189 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
192 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
193 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
196 else if ( !strcmp( name, "frame_rate_den" ) )
198 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
199 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
202 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
203 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
206 else if ( !strcmp( name, "width" ) )
208 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
209 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
211 profile->width = mlt_properties_get_int( properties, "width" );
213 else if ( !strcmp( name, "height" ) )
215 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
216 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
218 profile->height = mlt_properties_get_int( properties, "height" );
220 else if ( !strcmp( name, "progressive" ) )
222 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
223 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
225 profile->progressive = mlt_properties_get_int( properties, "progressive" );
227 else if ( !strcmp( name, "sample_aspect_num" ) )
229 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
230 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
231 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
233 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
235 else if ( !strcmp( name, "sample_aspect_den" ) )
237 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
238 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
239 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
241 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
243 else if ( !strcmp( name, "display_aspect_num" ) )
245 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
246 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
249 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
250 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
253 else if ( !strcmp( name, "display_aspect_den" ) )
255 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
256 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
259 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
260 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
263 else if ( !strcmp( name, "colorspace" ) )
265 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
266 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
268 profile->colorspace = mlt_properties_get_int( properties, "colorspace" );
272 /** The transmitter for the consumer-frame-show event
274 * Invokes the listener.
276 * \private \memberof mlt_consumer_s
277 * \param listener a function pointer that will be invoked
278 * \param owner the events object that will be passed to \p listener
279 * \param this a service that will be passed to \p listener
280 * \param args an array of pointers - the first entry is passed as a string to \p listener
283 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
285 if ( listener != NULL )
286 listener( owner, this, ( mlt_frame )args[ 0 ] );
289 /** The transmitter for the consumer-frame-render event
291 * Invokes the listener.
293 * \private \memberof mlt_consumer_s
294 * \param listener a function pointer that will be invoked
295 * \param owner the events object that will be passed to \p listener
296 * \param this a service that will be passed to \p listener
297 * \param args an array of pointers - the first entry is passed as a string to \p listener
300 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
302 if ( listener != NULL )
303 listener( owner, this, ( mlt_frame )args[ 0 ] );
306 /** A listener on the consumer-frame-show event
308 * Saves the position of the frame shown.
310 * \private \memberof mlt_consumer_s
311 * \param owner the events object
312 * \param consumer the consumer on which this event occurred
313 * \param frame the frame that was shown
316 static void on_consumer_frame_show( mlt_properties owner, mlt_consumer consumer, mlt_frame frame )
319 consumer->position = mlt_frame_get_position( frame );
322 /** Create a new consumer.
324 * \public \memberof mlt_consumer_s
325 * \param profile a profile (optional, but recommended)
326 * \return a new consumer
329 mlt_consumer mlt_consumer_new( mlt_profile profile )
331 // Create the memory for the structure
332 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
336 mlt_consumer_init( this, NULL, profile );
342 /** Get the parent service object.
344 * \public \memberof mlt_consumer_s
345 * \param this a consumer
346 * \return the parent service class
347 * \see MLT_CONSUMER_SERVICE
350 mlt_service mlt_consumer_service( mlt_consumer this )
352 return this != NULL ? &this->parent : NULL;
355 /** Get the consumer properties.
357 * \public \memberof mlt_consumer_s
358 * \param this a consumer
359 * \return the consumer's properties list
360 * \see MLT_CONSUMER_PROPERTIES
363 mlt_properties mlt_consumer_properties( mlt_consumer this )
365 return this != NULL ? MLT_SERVICE_PROPERTIES( &this->parent ) : NULL;
368 /** Connect the consumer to the producer.
370 * \public \memberof mlt_consumer_s
371 * \param this a consumer
372 * \param producer a producer
373 * \return > 0 warning, == 0 success, < 0 serious error,
374 * 1 = this service does not accept input,
375 * 2 = the producer is invalid,
376 * 3 = the producer is already registered with this consumer
379 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
381 return mlt_service_connect_producer( &this->parent, producer, 0 );
384 /** Start the consumer.
386 * \public \memberof mlt_consumer_s
387 * \param this a consumer
388 * \return true if there was an error
391 int mlt_consumer_start( mlt_consumer this )
393 // Stop listening to the property-changed event
394 mlt_event_block( this->event_listener );
397 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
399 // Determine if there's a test card producer
400 char *test_card = mlt_properties_get( properties, "test_card" );
402 // Just to make sure nothing is hanging around...
403 mlt_frame_close( this->put );
405 this->put_active = 1;
408 if ( test_card != NULL )
410 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
412 // Create a test card producer
413 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
414 mlt_producer producer = mlt_factory_producer( profile, NULL, test_card );
416 // Do we have a producer
417 if ( producer != NULL )
419 // Test card should loop I guess...
420 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
421 //mlt_producer_set_speed( producer, 0 );
422 //mlt_producer_set_in_and_out( producer, 0, 0 );
424 // Set the test card on the consumer
425 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
431 // Allow the hash table to speed things up
432 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
435 // Set the frame duration in microseconds for the frame-dropping heuristic
436 int frame_duration = 1000000 / mlt_properties_get_int( properties, "frame_rate_num" ) *
437 mlt_properties_get_int( properties, "frame_rate_den" );
438 mlt_properties_set_int( properties, "frame_duration", frame_duration );
440 // Check and run an ante command
441 if ( mlt_properties_get( properties, "ante" ) )
442 if ( system( mlt_properties_get( properties, "ante" ) ) == -1 )
443 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "ante" ) );
445 // Set the real_time preference
446 this->real_time = mlt_properties_get_int( properties, "real_time" );
449 if ( this->start != NULL )
450 return this->start( this );
455 /** An alternative method to feed frames into the consumer.
457 * Only valid if the consumer itself is not connected.
459 * \public \memberof mlt_consumer_s
460 * \param this a consumer
461 * \param frame a frame
462 * \return true (ignore this for now)
465 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
469 // Get the service assoicated to the consumer
470 mlt_service service = MLT_CONSUMER_SERVICE( this );
472 if ( mlt_service_producer( service ) == NULL )
476 pthread_mutex_lock( &this->put_mutex );
477 while ( this->put_active && this->put != NULL )
479 gettimeofday( &now, NULL );
480 tm.tv_sec = now.tv_sec + 1;
481 tm.tv_nsec = now.tv_usec * 1000;
482 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
484 if ( this->put_active && this->put == NULL )
487 mlt_frame_close( frame );
488 pthread_cond_broadcast( &this->put_cond );
489 pthread_mutex_unlock( &this->put_mutex );
493 mlt_frame_close( frame );
499 /** Protected method for consumer to get frames from connected service
501 * \public \memberof mlt_consumer_s
502 * \param this a consumer
506 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
509 mlt_frame frame = NULL;
511 // Get the service assoicated to the consumer
512 mlt_service service = MLT_CONSUMER_SERVICE( this );
514 // Get the consumer properties
515 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
518 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
522 pthread_mutex_lock( &this->put_mutex );
523 while ( this->put_active && this->put == NULL )
525 gettimeofday( &now, NULL );
526 tm.tv_sec = now.tv_sec + 1;
527 tm.tv_nsec = now.tv_usec * 1000;
528 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
532 pthread_cond_broadcast( &this->put_cond );
533 pthread_mutex_unlock( &this->put_mutex );
535 mlt_service_apply_filters( service, frame, 0 );
537 else if ( mlt_service_producer( service ) != NULL )
539 mlt_service_get_frame( service, &frame, 0 );
543 frame = mlt_frame_init( service );
548 // Get the frame properties
549 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
551 // Get the test card producer
552 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
554 // Attach the test frame producer to it.
555 if ( test_card != NULL )
556 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
558 // Attach the rescale property
559 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
561 // Aspect ratio and other jiggery pokery
562 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
563 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
564 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
571 /** Compute the time difference between now and a time value.
573 * \private \memberof mlt_consumer_s
574 * \param time1 a time value to be compared against now
575 * \return the difference in microseconds
578 static inline long time_difference( struct timeval *time1 )
580 struct timeval time2;
581 time2.tv_sec = time1->tv_sec;
582 time2.tv_usec = time1->tv_usec;
583 gettimeofday( time1, NULL );
584 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
587 /** The thread procedure for asynchronously pulling frames through the service
588 * network connected to a consumer.
590 * \private \memberof mlt_consumer_s
591 * \param arg a consumer
594 static void *consumer_read_ahead_thread( void *arg )
596 // The argument is the consumer
597 mlt_consumer this = arg;
599 // Get the properties of the consumer
600 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
602 // Get the width and height
603 int width = mlt_properties_get_int( properties, "width" );
604 int height = mlt_properties_get_int( properties, "height" );
606 // See if video is turned off
607 int video_off = mlt_properties_get_int( properties, "video_off" );
608 int preview_off = mlt_properties_get_int( properties, "preview_off" );
609 int preview_format = mlt_properties_get_int( properties, "preview_format" );
611 // Get the audio settings
612 mlt_audio_format afmt = mlt_audio_s16;
614 double fps = mlt_properties_get_double( properties, "fps" );
615 int channels = mlt_properties_get_int( properties, "channels" );
616 int frequency = mlt_properties_get_int( properties, "frequency" );
620 // See if audio is turned off
621 int audio_off = mlt_properties_get_int( properties, "audio_off" );
623 // Get the maximum size of the buffer
624 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
626 // General frame variable
627 mlt_frame frame = NULL;
628 uint8_t *image = NULL;
633 // Average time for get_frame and get_image
636 int64_t time_wait = 0;
637 int64_t time_frame = 0;
638 int64_t time_process = 0;
640 mlt_service lock_object = NULL;
642 if ( preview_off && preview_format != 0 )
643 this->format = preview_format;
645 // Get the first frame
646 frame = mlt_consumer_get_frame( this );
648 // Get the lock object
649 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
652 if ( lock_object ) mlt_service_lock( lock_object );
654 // Get the image of the first frame
657 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
658 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
663 samples = mlt_sample_calculator( fps, frequency, counter++ );
664 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
667 // Unlock the lock object
668 if ( lock_object ) mlt_service_unlock( lock_object );
671 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
673 // Get the starting time (can ignore the times above)
674 gettimeofday( &ante, NULL );
676 // Continue to read ahead
677 while ( this->ahead )
679 // Fetch width/height again
680 width = mlt_properties_get_int( properties, "width" );
681 height = mlt_properties_get_int( properties, "height" );
683 // Put the current frame into the queue
684 pthread_mutex_lock( &this->frame_queue_mutex );
685 while( this->ahead && mlt_deque_count( this->frame_queue ) >= buffer )
686 pthread_cond_wait( &this->frame_queue_cond, &this->frame_queue_mutex );
687 mlt_deque_push_back( this->frame_queue, frame );
688 pthread_cond_broadcast( &this->frame_queue_cond );
689 pthread_mutex_unlock( &this->frame_queue_mutex );
691 time_wait += time_difference( &ante );
693 // Get the next frame
694 frame = mlt_consumer_get_frame( this );
695 time_frame += time_difference( &ante );
697 // If there's no frame, we're probably stopped...
701 // Attempt to fetch the lock object
702 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
704 // Increment the count
707 // Lock if there's a lock object
708 if ( lock_object ) mlt_service_lock( lock_object );
710 // All non normal playback frames should be shown
711 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
713 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
714 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
725 if ( !skip_next || this->real_time == -1 )
727 // Get the image, mark as rendered and time it
730 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
731 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
733 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
737 // Increment the number of sequentially skipped frames
741 // If we've reached an unacceptable level, reset everything
752 // Always process audio
755 samples = mlt_sample_calculator( fps, frequency, counter++ );
756 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
759 // Increment the time take for this frame
760 time_process += time_difference( &ante );
762 // Determine if the next frame should be skipped
763 if ( mlt_deque_count( this->frame_queue ) <= 5 )
765 int frame_duration = mlt_properties_get_int( properties, "frame_duration" );
766 if ( ( ( time_wait + time_frame + time_process ) / count ) > frame_duration )
770 // Unlock if there's a lock object
771 if ( lock_object ) mlt_service_unlock( lock_object );
774 // Remove the last frame
775 mlt_frame_close( frame );
780 /** Start the read/render thread.
782 * \private \memberof mlt_consumer_s
783 * \param this a consumer
786 static void consumer_read_ahead_start( mlt_consumer this )
791 // Create the frame queue
792 this->frame_queue = mlt_deque_init( );
794 // Create the frame_queue mutex
795 pthread_mutex_init( &this->frame_queue_mutex, NULL );
797 // Create the condition
798 pthread_cond_init( &this->frame_queue_cond, NULL );
800 // Create the read ahead
801 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "priority" ) )
803 struct sched_param priority;
804 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this ), "priority" );
805 pthread_attr_t thread_attributes;
806 pthread_attr_init( &thread_attributes );
807 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
808 pthread_attr_setschedparam( &thread_attributes, &priority );
809 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
810 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
811 if ( pthread_create( &this->ahead_thread, &thread_attributes, consumer_read_ahead_thread, this ) < 0 )
812 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
813 pthread_attr_destroy( &thread_attributes );
817 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
821 /** Stop the read/render thread.
823 * \private \memberof mlt_consumer_s
824 * \param this a consumer
827 static void consumer_read_ahead_stop( mlt_consumer this )
829 // Make sure we're running
832 // Inform thread to stop
835 // Broadcast to the condition in case it's waiting
836 pthread_mutex_lock( &this->frame_queue_mutex );
837 pthread_cond_broadcast( &this->frame_queue_cond );
838 pthread_mutex_unlock( &this->frame_queue_mutex );
840 // Broadcast to the put condition in case it's waiting
841 pthread_mutex_lock( &this->put_mutex );
842 pthread_cond_broadcast( &this->put_cond );
843 pthread_mutex_unlock( &this->put_mutex );
846 pthread_join( this->ahead_thread, NULL );
848 // Destroy the frame queue mutex
849 pthread_mutex_destroy( &this->frame_queue_mutex );
851 // Destroy the condition
852 pthread_cond_destroy( &this->frame_queue_cond );
855 while ( mlt_deque_count( this->frame_queue ) )
856 mlt_frame_close( mlt_deque_pop_back( this->frame_queue ) );
859 mlt_deque_close( this->frame_queue );
863 /** Flush the read/render thread's buffer.
865 * \public \memberof mlt_consumer_s
866 * \param this a consumer
869 void mlt_consumer_purge( mlt_consumer this )
873 pthread_mutex_lock( &this->frame_queue_mutex );
874 while ( mlt_deque_count( this->frame_queue ) )
875 mlt_frame_close( mlt_deque_pop_back( this->frame_queue ) );
876 pthread_cond_broadcast( &this->frame_queue_cond );
877 pthread_mutex_unlock( &this->frame_queue_mutex );
881 /** Get the next frame from the producer connected to a consumer.
883 * Typically, one uses this instead of \p mlt_consumer_get_frame to make
884 * the asynchronous/real-time behavior configurable at runtime.
885 * You should close the frame returned from this when you are done with it.
887 * \public \memberof mlt_consumer_s
888 * \param this a consumer
892 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
895 mlt_frame frame = NULL;
897 // Get the properties
898 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
900 // Check if the user has requested real time or not
901 if ( this->real_time )
905 // Is the read ahead running?
906 if ( this->ahead == 0 )
908 int buffer = mlt_properties_get_int( properties, "buffer" );
909 int prefill = mlt_properties_get_int( properties, "prefill" );
910 consumer_read_ahead_start( this );
912 size = prefill > 0 && prefill < buffer ? prefill : buffer;
915 // Get frame from queue
916 pthread_mutex_lock( &this->frame_queue_mutex );
917 while( this->ahead && mlt_deque_count( this->frame_queue ) < size )
918 pthread_cond_wait( &this->frame_queue_cond, &this->frame_queue_mutex );
919 frame = mlt_deque_pop_front( this->frame_queue );
920 pthread_cond_broadcast( &this->frame_queue_cond );
921 pthread_mutex_unlock( &this->frame_queue_mutex );
925 // Get the frame in non real time
926 frame = mlt_consumer_get_frame( this );
928 // This isn't true, but from the consumers perspective it is
930 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
936 /** Callback for the implementation to indicate a stopped condition.
938 * \public \memberof mlt_consumer_s
939 * \param this a consumer
942 void mlt_consumer_stopped( mlt_consumer this )
944 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
945 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
946 mlt_event_unblock( this->event_listener );
949 /** Stop the consumer.
951 * \public \memberof mlt_consumer_s
952 * \param this a consumer
953 * \return true if there was an error
956 int mlt_consumer_stop( mlt_consumer this )
959 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
962 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping put waiting\n" );
963 pthread_mutex_lock( &this->put_mutex );
964 this->put_active = 0;
965 pthread_cond_broadcast( &this->put_cond );
966 pthread_mutex_unlock( &this->put_mutex );
969 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping consumer\n" );
970 if ( this->stop != NULL )
973 // Check if the user has requested real time or not and stop if necessary
974 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping read_ahead\n" );
975 if ( mlt_properties_get_int( properties, "real_time" ) )
976 consumer_read_ahead_stop( this );
978 // Kill the test card
979 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
981 // Check and run a post command
982 if ( mlt_properties_get( properties, "post" ) )
983 if (system( mlt_properties_get( properties, "post" ) ) == -1 )
984 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "post" ) );
986 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopped\n" );
991 /** Determine if the consumer is stopped.
993 * \public \memberof mlt_consumer_s
994 * \param this a consumer
995 * \return true if the consumer is stopped
998 int mlt_consumer_is_stopped( mlt_consumer this )
1000 // Check if the consumer is stopped
1001 if ( this->is_stopped != NULL )
1002 return this->is_stopped( this );
1007 /** Close and destroy the consumer.
1009 * \public \memberof mlt_consumer_s
1010 * \param this a consumer
1013 void mlt_consumer_close( mlt_consumer this )
1015 if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
1017 // Get the childs close function
1018 void ( *consumer_close )( ) = this->close;
1020 if ( consumer_close )
1023 //mlt_consumer_stop( this );
1026 consumer_close( this );
1030 // Make sure it only gets called once
1031 this->parent.close = NULL;
1033 // Destroy the push mutex and condition
1034 pthread_mutex_destroy( &this->put_mutex );
1035 pthread_cond_destroy( &this->put_cond );
1037 mlt_service_close( &this->parent );
1042 /** Get the position of the last frame shown.
1044 * \public \memberof mlt_consumer_s
1045 * \param consumer a consumer
1046 * \return the position
1049 mlt_position mlt_consumer_position( mlt_consumer consumer )
1051 return consumer->position;