3 * \brief abstraction for all consumer services
5 * Copyright (C) 2003-2008 Ushodaya Enterprises Limited
6 * \author Charles Yates <charles.yates@pandora.be>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "mlt_consumer.h"
24 #include "mlt_factory.h"
25 #include "mlt_producer.h"
26 #include "mlt_frame.h"
27 #include "mlt_profile.h"
35 /** Define this if you want an automatic deinterlace (if necessary) when the
36 * consumer's producer is not running at normal speed.
38 #undef DEINTERLACE_ON_NOT_NORMAL_SPEED
40 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
41 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
42 static void mlt_consumer_property_changed( mlt_service owner, mlt_consumer this, char *name );
43 static void apply_profile_properties( mlt_consumer this, mlt_profile profile, mlt_properties properties );
45 /** Initialize a consumer service.
47 * \public \memberof mlt_consumer_s
48 * \param this the consumer to initialize
49 * \param child a pointer to the object for the subclass
50 * \param profile the \p mlt_profile_s to use (optional but recommended,
51 * uses the environment variable MLT if this is NULL)
52 * \return true if there was an error
55 int mlt_consumer_init( mlt_consumer this, void *child, mlt_profile profile )
58 memset( this, 0, sizeof( struct mlt_consumer_s ) );
60 error = mlt_service_init( &this->parent, this );
63 // Get the properties from the service
64 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
66 // Apply profile to properties
67 if ( profile == NULL )
69 // Normally the application creates the profile and controls its lifetime
70 // This is the fallback exception handling
71 profile = mlt_profile_init( NULL );
72 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
73 mlt_properties_set_data( properties, "_profile", profile, 0, (mlt_destructor)mlt_profile_close, NULL );
75 apply_profile_properties( this, profile, properties );
77 // Default rescaler for all consumers
78 mlt_properties_set( properties, "rescale", "bilinear" );
80 // Default read ahead buffer size
81 mlt_properties_set_int( properties, "buffer", 25 );
83 // Default audio frequency and channels
84 mlt_properties_set_int( properties, "frequency", 48000 );
85 mlt_properties_set_int( properties, "channels", 2 );
87 // Default of all consumers is real time
88 mlt_properties_set_int( properties, "real_time", 1 );
90 // Default to environment test card
91 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
93 // Hmm - default all consumers to yuv422 :-/
94 this->format = mlt_image_yuv422;
96 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
97 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
98 mlt_events_register( properties, "consumer-stopped", NULL );
100 // Register a property-changed listener to handle the profile property -
101 // subsequent properties can override the profile
102 this->event_listener = mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_consumer_property_changed );
104 // Create the push mutex and condition
105 pthread_mutex_init( &this->put_mutex, NULL );
106 pthread_cond_init( &this->put_cond, NULL );
112 /** Convert the profile into properties on the consumer.
114 * \private \memberof mlt_consumer_s
115 * \param this a consumer
116 * \param profile a profile
117 * \param properties a properties list (typically, the consumer's)
120 static void apply_profile_properties( mlt_consumer this, mlt_profile profile, mlt_properties properties )
122 mlt_event_block( this->event_listener );
123 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
124 mlt_properties_set_int( properties, "frame_rate_num", profile->frame_rate_num );
125 mlt_properties_set_int( properties, "frame_rate_den", profile->frame_rate_den );
126 mlt_properties_set_int( properties, "width", profile->width );
127 mlt_properties_set_int( properties, "height", profile->height );
128 mlt_properties_set_int( properties, "progressive", profile->progressive );
129 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
130 mlt_properties_set_int( properties, "sample_aspect_num", profile->sample_aspect_num );
131 mlt_properties_set_int( properties, "sample_aspect_den", profile->sample_aspect_den );
132 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
133 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
134 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
135 mlt_event_unblock( this->event_listener );
138 /** The property-changed event listener
140 * \private \memberof mlt_consumer_s
141 * \param owner the service a service (ignored)
142 * \param this the consumer
143 * \param name the name of the property that changed
146 static void mlt_consumer_property_changed( mlt_service owner, mlt_consumer this, char *name )
148 if ( !strcmp( name, "profile" ) )
151 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
153 // Get the current profile
154 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
156 // Load the new profile
157 mlt_profile new_profile = mlt_profile_init( mlt_properties_get( properties, name ) );
162 if ( profile != NULL )
164 free( profile->description );
165 memcpy( profile, new_profile, sizeof( struct mlt_profile_s ) );
166 profile->description = strdup( new_profile->description );
167 mlt_profile_close( new_profile );
171 profile = new_profile;
174 // Apply to properties
175 apply_profile_properties( this, profile, properties );
178 else if ( !strcmp( name, "frame_rate_num" ) )
180 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
181 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
184 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
185 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
188 else if ( !strcmp( name, "frame_rate_den" ) )
190 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
191 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
194 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
195 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
198 else if ( !strcmp( name, "width" ) )
200 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
201 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
203 profile->width = mlt_properties_get_int( properties, "width" );
205 else if ( !strcmp( name, "height" ) )
207 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
208 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
210 profile->height = mlt_properties_get_int( properties, "height" );
212 else if ( !strcmp( name, "progressive" ) )
214 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
215 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
217 profile->progressive = mlt_properties_get_int( properties, "progressive" );
219 else if ( !strcmp( name, "sample_aspect_num" ) )
221 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
222 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
223 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
225 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
227 else if ( !strcmp( name, "sample_aspect_den" ) )
229 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
230 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
231 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
233 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
235 else if ( !strcmp( name, "display_aspect_num" ) )
237 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
238 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
241 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
242 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
245 else if ( !strcmp( name, "display_aspect_den" ) )
247 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
248 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
251 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
252 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
257 /** The transmitter for the consumer-frame-show event
259 * Invokes the listener.
261 * \private \memberof mlt_consumer_s
262 * \param listener a function pointer that will be invoked
263 * \param owner a properties list that will be passed to \p listener
264 * \param this a service that will be passed to \p listener
265 * \param args an array of pointers - the first entry is passed as a string to \p listener
268 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
270 if ( listener != NULL )
271 listener( owner, this, ( mlt_frame )args[ 0 ] );
274 /** The transmitter for the consumer-frame-render event
276 * Invokes the listener.
278 * \private \memberof mlt_consumer_s
279 * \param listener a function pointer that will be invoked
280 * \param owner a properties list that will be passed to \p listener
281 * \param this a service that will be passed to \p listener
282 * \param args an array of pointers - the first entry is passed as a string to \p listener
285 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
287 if ( listener != NULL )
288 listener( owner, this, ( mlt_frame )args[ 0 ] );
291 /** Create a new consumer.
293 * \public \memberof mlt_consumer_s
294 * \param profile a profile (optional, but recommended)
295 * \return a new consumer
298 mlt_consumer mlt_consumer_new( mlt_profile profile )
300 // Create the memory for the structure
301 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
305 mlt_consumer_init( this, NULL, profile );
311 /** Get the parent service object.
313 * \public \memberof mlt_consumer_s
314 * \param this a consumer
315 * \return the parent service class
316 * \see MLT_CONSUMER_SERVICE
319 mlt_service mlt_consumer_service( mlt_consumer this )
321 return this != NULL ? &this->parent : NULL;
324 /** Get the consumer properties.
326 * \public \memberof mlt_consumer_s
327 * \param this a consumer
328 * \return the consumer's properties list
329 * \see MLT_CONSUMER_PROPERTIES
332 mlt_properties mlt_consumer_properties( mlt_consumer this )
334 return this != NULL ? MLT_SERVICE_PROPERTIES( &this->parent ) : NULL;
337 /** Connect the consumer to the producer.
339 * \public \memberof mlt_consumer_s
340 * \param this a consumer
341 * \param producer a producer
342 * \return > 0 warning, == 0 success, < 0 serious error,
343 * 1 = this service does not accept input,
344 * 2 = the producer is invalid,
345 * 3 = the producer is already registered with this consumer
348 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
350 return mlt_service_connect_producer( &this->parent, producer, 0 );
353 /** Start the consumer.
355 * \public \memberof mlt_consumer_s
356 * \param this a consumer
357 * \return true if there was an error
360 int mlt_consumer_start( mlt_consumer this )
362 // Stop listening to the property-changed event
363 mlt_event_block( this->event_listener );
366 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
368 // Determine if there's a test card producer
369 char *test_card = mlt_properties_get( properties, "test_card" );
371 // Just to make sure nothing is hanging around...
372 mlt_frame_close( this->put );
374 this->put_active = 1;
377 if ( test_card != NULL )
379 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
381 // Create a test card producer
382 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
383 mlt_producer producer = mlt_factory_producer( profile, NULL, test_card );
385 // Do we have a producer
386 if ( producer != NULL )
388 // Test card should loop I guess...
389 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
390 //mlt_producer_set_speed( producer, 0 );
391 //mlt_producer_set_in_and_out( producer, 0, 0 );
393 // Set the test card on the consumer
394 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
400 // Allow the hash table to speed things up
401 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
404 // Set the frame duration in microseconds for the frame-dropping heuristic
405 int frame_duration = 1000000 / mlt_properties_get_int( properties, "frame_rate_num" ) *
406 mlt_properties_get_int( properties, "frame_rate_den" );
407 mlt_properties_set_int( properties, "frame_duration", frame_duration );
409 // Check and run an ante command
410 if ( mlt_properties_get( properties, "ante" ) )
411 system( mlt_properties_get( properties, "ante" ) );
413 // Set the real_time preference
414 this->real_time = mlt_properties_get_int( properties, "real_time" );
417 if ( this->start != NULL )
418 return this->start( this );
423 /** An alternative method to feed frames into the consumer.
425 * Only valid if the consumer itself is not connected.
427 * \public \memberof mlt_consumer_s
428 * \param this a consumer
429 * \param frame a frame
430 * \return true (ignore this for now)
433 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
437 // Get the service assoicated to the consumer
438 mlt_service service = MLT_CONSUMER_SERVICE( this );
440 if ( mlt_service_producer( service ) == NULL )
444 pthread_mutex_lock( &this->put_mutex );
445 while ( this->put_active && this->put != NULL )
447 gettimeofday( &now, NULL );
448 tm.tv_sec = now.tv_sec + 1;
449 tm.tv_nsec = now.tv_usec * 1000;
450 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
452 if ( this->put_active && this->put == NULL )
455 mlt_frame_close( frame );
456 pthread_cond_broadcast( &this->put_cond );
457 pthread_mutex_unlock( &this->put_mutex );
461 mlt_frame_close( frame );
467 /** Protected method for consumer to get frames from connected service
469 * \public \memberof mlt_consumer_s
470 * \param this a consumer
474 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
477 mlt_frame frame = NULL;
479 // Get the service assoicated to the consumer
480 mlt_service service = MLT_CONSUMER_SERVICE( this );
482 // Get the consumer properties
483 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
486 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
490 pthread_mutex_lock( &this->put_mutex );
491 while ( this->put_active && this->put == NULL )
493 gettimeofday( &now, NULL );
494 tm.tv_sec = now.tv_sec + 1;
495 tm.tv_nsec = now.tv_usec * 1000;
496 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
500 pthread_cond_broadcast( &this->put_cond );
501 pthread_mutex_unlock( &this->put_mutex );
503 mlt_service_apply_filters( service, frame, 0 );
505 else if ( mlt_service_producer( service ) != NULL )
507 mlt_service_get_frame( service, &frame, 0 );
511 frame = mlt_frame_init( service );
516 // Get the frame properties
517 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
519 // Get the test card producer
520 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
522 // Attach the test frame producer to it.
523 if ( test_card != NULL )
524 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
526 // Attach the rescale property
527 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
529 // Aspect ratio and other jiggery pokery
530 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
531 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
532 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
539 /** Compute the time difference between now and a time value.
541 * \private \memberof mlt_consumer_s
542 * \param time1 a time value to be compared against now
543 * \return the difference in microseconds
546 static inline long time_difference( struct timeval *time1 )
548 struct timeval time2;
549 time2.tv_sec = time1->tv_sec;
550 time2.tv_usec = time1->tv_usec;
551 gettimeofday( time1, NULL );
552 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
555 /** The thread procedure for asynchronously pulling frames through the service
556 * network connected to a consumer.
558 * \private \memberof mlt_consumer_s
559 * \param arg a consumer
562 static void *consumer_read_ahead_thread( void *arg )
564 // The argument is the consumer
565 mlt_consumer this = arg;
567 // Get the properties of the consumer
568 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
570 // Get the width and height
571 int width = mlt_properties_get_int( properties, "width" );
572 int height = mlt_properties_get_int( properties, "height" );
574 // See if video is turned off
575 int video_off = mlt_properties_get_int( properties, "video_off" );
576 int preview_off = mlt_properties_get_int( properties, "preview_off" );
577 int preview_format = mlt_properties_get_int( properties, "preview_format" );
579 // Get the audio settings
580 mlt_audio_format afmt = mlt_audio_pcm;
582 double fps = mlt_properties_get_double( properties, "fps" );
583 int channels = mlt_properties_get_int( properties, "channels" );
584 int frequency = mlt_properties_get_int( properties, "frequency" );
588 // See if audio is turned off
589 int audio_off = mlt_properties_get_int( properties, "audio_off" );
591 // Get the maximum size of the buffer
592 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
594 // General frame variable
595 mlt_frame frame = NULL;
596 uint8_t *image = NULL;
601 // Average time for get_frame and get_image
604 int64_t time_wait = 0;
605 int64_t time_frame = 0;
606 int64_t time_process = 0;
608 mlt_service lock_object = NULL;
610 if ( preview_off && preview_format != 0 )
611 this->format = preview_format;
613 // Get the first frame
614 frame = mlt_consumer_get_frame( this );
616 // Get the lock object
617 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
620 if ( lock_object ) mlt_service_lock( lock_object );
622 // Get the image of the first frame
625 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
626 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
631 samples = mlt_sample_calculator( fps, frequency, counter++ );
632 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
635 // Unlock the lock object
636 if ( lock_object ) mlt_service_unlock( lock_object );
639 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
641 // Get the starting time (can ignore the times above)
642 gettimeofday( &ante, NULL );
644 // Continue to read ahead
645 while ( this->ahead )
647 // Fetch width/height again
648 width = mlt_properties_get_int( properties, "width" );
649 height = mlt_properties_get_int( properties, "height" );
651 // Put the current frame into the queue
652 pthread_mutex_lock( &this->mutex );
653 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
654 pthread_cond_wait( &this->cond, &this->mutex );
655 mlt_deque_push_back( this->queue, frame );
656 pthread_cond_broadcast( &this->cond );
657 pthread_mutex_unlock( &this->mutex );
659 time_wait += time_difference( &ante );
661 // Get the next frame
662 frame = mlt_consumer_get_frame( this );
663 time_frame += time_difference( &ante );
665 // If there's no frame, we're probably stopped...
669 // Attempt to fetch the lock object
670 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
672 // Increment the count
675 // Lock if there's a lock object
676 if ( lock_object ) mlt_service_lock( lock_object );
678 // All non normal playback frames should be shown
679 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
681 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
682 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
693 if ( !skip_next || this->real_time == -1 )
695 // Get the image, mark as rendered and time it
698 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
699 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
701 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
705 // Increment the number of sequentially skipped frames
709 // If we've reached an unacceptable level, reset everything
720 // Always process audio
723 samples = mlt_sample_calculator( fps, frequency, counter++ );
724 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
727 // Increment the time take for this frame
728 time_process += time_difference( &ante );
730 // Determine if the next frame should be skipped
731 if ( mlt_deque_count( this->queue ) <= 5 )
733 int frame_duration = mlt_properties_get_int( properties, "frame_duration" );
734 if ( ( ( time_wait + time_frame + time_process ) / count ) > frame_duration )
738 // Unlock if there's a lock object
739 if ( lock_object ) mlt_service_unlock( lock_object );
742 // Remove the last frame
743 mlt_frame_close( frame );
748 /** Start the read/render thread.
750 * \private \memberof mlt_consumer_s
751 * \param this a consumer
754 static void consumer_read_ahead_start( mlt_consumer this )
759 // Create the frame queue
760 this->queue = mlt_deque_init( );
763 pthread_mutex_init( &this->mutex, NULL );
765 // Create the condition
766 pthread_cond_init( &this->cond, NULL );
768 // Create the read ahead
769 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "priority" ) )
771 struct sched_param priority;
772 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this ), "priority" );
773 pthread_attr_t thread_attributes;
774 pthread_attr_init( &thread_attributes );
775 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
776 pthread_attr_setschedparam( &thread_attributes, &priority );
777 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
778 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
779 if ( pthread_create( &this->ahead_thread, &thread_attributes, consumer_read_ahead_thread, this ) < 0 )
780 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
781 pthread_attr_destroy( &thread_attributes );
785 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
789 /** Stop the read/render thread.
791 * \private \memberof mlt_consumer_s
792 * \param this a consumer
795 static void consumer_read_ahead_stop( mlt_consumer this )
797 // Make sure we're running
800 // Inform thread to stop
803 // Broadcast to the condition in case it's waiting
804 pthread_mutex_lock( &this->mutex );
805 pthread_cond_broadcast( &this->cond );
806 pthread_mutex_unlock( &this->mutex );
808 // Broadcast to the put condition in case it's waiting
809 pthread_mutex_lock( &this->put_mutex );
810 pthread_cond_broadcast( &this->put_cond );
811 pthread_mutex_unlock( &this->put_mutex );
814 pthread_join( this->ahead_thread, NULL );
817 pthread_mutex_destroy( &this->mutex );
819 // Destroy the condition
820 pthread_cond_destroy( &this->cond );
823 while ( mlt_deque_count( this->queue ) )
824 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
827 mlt_deque_close( this->queue );
831 /** Flush the read/render thread's buffer.
833 * \public \memberof mlt_consumer_s
834 * \param this a consumer
837 void mlt_consumer_purge( mlt_consumer this )
841 pthread_mutex_lock( &this->mutex );
842 while ( mlt_deque_count( this->queue ) )
843 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
844 pthread_cond_broadcast( &this->cond );
845 pthread_mutex_unlock( &this->mutex );
849 /** Get the next frame from the producer connected to a consumer.
851 * Typically, one uses this instead of \p mlt_consumer_get_frame to make
852 * the asynchronous/real-time behavior configurable at runtime.
853 * You should close the frame returned from this when you are done with it.
855 * \public \memberof mlt_consumer_s
856 * \param this a consumer
860 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
863 mlt_frame frame = NULL;
865 // Get the properties
866 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
868 // Check if the user has requested real time or not
869 if ( this->real_time )
873 // Is the read ahead running?
874 if ( this->ahead == 0 )
876 int buffer = mlt_properties_get_int( properties, "buffer" );
877 int prefill = mlt_properties_get_int( properties, "prefill" );
878 consumer_read_ahead_start( this );
880 size = prefill > 0 && prefill < buffer ? prefill : buffer;
883 // Get frame from queue
884 pthread_mutex_lock( &this->mutex );
885 while( this->ahead && mlt_deque_count( this->queue ) < size )
886 pthread_cond_wait( &this->cond, &this->mutex );
887 frame = mlt_deque_pop_front( this->queue );
888 pthread_cond_broadcast( &this->cond );
889 pthread_mutex_unlock( &this->mutex );
893 // Get the frame in non real time
894 frame = mlt_consumer_get_frame( this );
896 // This isn't true, but from the consumers perspective it is
898 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
904 /** Callback for the implementation to indicate a stopped condition.
906 * \public \memberof mlt_consumer_s
907 * \param this a consumer
910 void mlt_consumer_stopped( mlt_consumer this )
912 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
913 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
914 mlt_event_unblock( this->event_listener );
917 /** Stop the consumer.
919 * \public \memberof mlt_consumer_s
920 * \param this a consumer
921 * \return true if there was an error
924 int mlt_consumer_stop( mlt_consumer this )
927 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
930 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping put waiting\n" );
931 pthread_mutex_lock( &this->put_mutex );
932 this->put_active = 0;
933 pthread_cond_broadcast( &this->put_cond );
934 pthread_mutex_unlock( &this->put_mutex );
937 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping consumer\n" );
938 if ( this->stop != NULL )
941 // Check if the user has requested real time or not and stop if necessary
942 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping read_ahead\n" );
943 if ( mlt_properties_get_int( properties, "real_time" ) )
944 consumer_read_ahead_stop( this );
946 // Kill the test card
947 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
949 // Check and run a post command
950 if ( mlt_properties_get( properties, "post" ) )
951 system( mlt_properties_get( properties, "post" ) );
953 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopped\n" );
958 /** Determine if the consumer is stopped.
960 * \public \memberof mlt_consumer_s
961 * \param this a consumer
962 * \return true if the consumer is stopped
965 int mlt_consumer_is_stopped( mlt_consumer this )
967 // Check if the consumer is stopped
968 if ( this->is_stopped != NULL )
969 return this->is_stopped( this );
974 /** Close and destroy the consumer.
976 * \public \memberof mlt_consumer_s
977 * \param this a consumer
980 void mlt_consumer_close( mlt_consumer this )
982 if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
984 // Get the childs close function
985 void ( *consumer_close )( ) = this->close;
987 if ( consumer_close )
990 //mlt_consumer_stop( this );
993 consumer_close( this );
997 // Make sure it only gets called once
998 this->parent.close = NULL;
1000 // Destroy the push mutex and condition
1001 pthread_mutex_destroy( &this->put_mutex );
1002 pthread_cond_destroy( &this->put_cond );
1004 mlt_service_close( &this->parent );