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_service owner, mlt_consumer this, char *name );
48 static void apply_profile_properties( mlt_consumer this, mlt_profile profile, mlt_properties properties );
50 /** Initialize a consumer service.
52 * \public \memberof mlt_consumer_s
53 * \param this the consumer to initialize
54 * \param child a pointer to the object for the subclass
55 * \param profile the \p mlt_profile_s to use (optional but recommended,
56 * uses the environment variable MLT if this is NULL)
57 * \return true if there was an error
60 int mlt_consumer_init( mlt_consumer this, void *child, mlt_profile profile )
63 memset( this, 0, sizeof( struct mlt_consumer_s ) );
65 error = mlt_service_init( &this->parent, this );
68 // Get the properties from the service
69 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
71 // Apply profile to properties
72 if ( profile == NULL )
74 // Normally the application creates the profile and controls its lifetime
75 // This is the fallback exception handling
76 profile = mlt_profile_init( NULL );
77 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
78 mlt_properties_set_data( properties, "_profile", profile, 0, (mlt_destructor)mlt_profile_close, NULL );
80 apply_profile_properties( this, profile, properties );
82 // Default rescaler for all consumers
83 mlt_properties_set( properties, "rescale", "bilinear" );
85 // Default read ahead buffer size
86 mlt_properties_set_int( properties, "buffer", 25 );
88 // Default audio frequency and channels
89 mlt_properties_set_int( properties, "frequency", 48000 );
90 mlt_properties_set_int( properties, "channels", 2 );
92 // Default of all consumers is real time
93 mlt_properties_set_int( properties, "real_time", 1 );
95 // Default to environment test card
96 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
98 // Hmm - default all consumers to yuv422 :-/
99 this->format = mlt_image_yuv422;
101 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
102 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
103 mlt_events_register( properties, "consumer-stopped", NULL );
105 // Register a property-changed listener to handle the profile property -
106 // subsequent properties can override the profile
107 this->event_listener = mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_consumer_property_changed );
109 // Create the push mutex and condition
110 pthread_mutex_init( &this->put_mutex, NULL );
111 pthread_cond_init( &this->put_cond, NULL );
117 /** Convert the profile into properties on the consumer.
119 * \private \memberof mlt_consumer_s
120 * \param this a consumer
121 * \param profile a profile
122 * \param properties a properties list (typically, the consumer's)
125 static void apply_profile_properties( mlt_consumer this, mlt_profile profile, mlt_properties properties )
127 mlt_event_block( this->event_listener );
128 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
129 mlt_properties_set_int( properties, "frame_rate_num", profile->frame_rate_num );
130 mlt_properties_set_int( properties, "frame_rate_den", profile->frame_rate_den );
131 mlt_properties_set_int( properties, "width", profile->width );
132 mlt_properties_set_int( properties, "height", profile->height );
133 mlt_properties_set_int( properties, "progressive", profile->progressive );
134 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
135 mlt_properties_set_int( properties, "sample_aspect_num", profile->sample_aspect_num );
136 mlt_properties_set_int( properties, "sample_aspect_den", profile->sample_aspect_den );
137 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
138 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
139 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
140 mlt_properties_set_int( properties, "colorspace", profile->colorspace );
141 mlt_event_unblock( this->event_listener );
144 /** The property-changed event listener
146 * \private \memberof mlt_consumer_s
147 * \param owner the service a service (ignored)
148 * \param this the consumer
149 * \param name the name of the property that changed
152 static void mlt_consumer_property_changed( mlt_service owner, mlt_consumer this, char *name )
154 if ( !strcmp( name, "profile" ) )
157 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
159 // Get the current profile
160 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
162 // Load the new profile
163 mlt_profile new_profile = mlt_profile_init( mlt_properties_get( properties, name ) );
168 if ( profile != NULL )
170 free( profile->description );
171 memcpy( profile, new_profile, sizeof( struct mlt_profile_s ) );
172 profile->description = strdup( new_profile->description );
173 mlt_profile_close( new_profile );
177 profile = new_profile;
180 // Apply to properties
181 apply_profile_properties( this, profile, properties );
184 else if ( !strcmp( name, "frame_rate_num" ) )
186 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
187 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
190 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
191 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
194 else if ( !strcmp( name, "frame_rate_den" ) )
196 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
197 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
200 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
201 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
204 else if ( !strcmp( name, "width" ) )
206 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
207 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
209 profile->width = mlt_properties_get_int( properties, "width" );
211 else if ( !strcmp( name, "height" ) )
213 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
214 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
216 profile->height = mlt_properties_get_int( properties, "height" );
218 else if ( !strcmp( name, "progressive" ) )
220 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
221 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
223 profile->progressive = mlt_properties_get_int( properties, "progressive" );
225 else if ( !strcmp( name, "sample_aspect_num" ) )
227 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
228 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
229 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
231 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
233 else if ( !strcmp( name, "sample_aspect_den" ) )
235 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
236 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
237 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
239 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
241 else if ( !strcmp( name, "display_aspect_num" ) )
243 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
244 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
247 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
248 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
251 else if ( !strcmp( name, "display_aspect_den" ) )
253 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
254 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
257 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
258 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
261 else if ( !strcmp( name, "colorspace" ) )
263 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
264 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
266 profile->colorspace = mlt_properties_get_int( properties, "colorspace" );
270 /** The transmitter for the consumer-frame-show event
272 * Invokes the listener.
274 * \private \memberof mlt_consumer_s
275 * \param listener a function pointer that will be invoked
276 * \param owner a properties list that will be passed to \p listener
277 * \param this a service that will be passed to \p listener
278 * \param args an array of pointers - the first entry is passed as a string to \p listener
281 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
283 if ( listener != NULL )
284 listener( owner, this, ( mlt_frame )args[ 0 ] );
287 /** The transmitter for the consumer-frame-render event
289 * Invokes the listener.
291 * \private \memberof mlt_consumer_s
292 * \param listener a function pointer that will be invoked
293 * \param owner a properties list that will be passed to \p listener
294 * \param this a service that will be passed to \p listener
295 * \param args an array of pointers - the first entry is passed as a string to \p listener
298 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
300 if ( listener != NULL )
301 listener( owner, this, ( mlt_frame )args[ 0 ] );
304 /** Create a new consumer.
306 * \public \memberof mlt_consumer_s
307 * \param profile a profile (optional, but recommended)
308 * \return a new consumer
311 mlt_consumer mlt_consumer_new( mlt_profile profile )
313 // Create the memory for the structure
314 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
318 mlt_consumer_init( this, NULL, profile );
324 /** Get the parent service object.
326 * \public \memberof mlt_consumer_s
327 * \param this a consumer
328 * \return the parent service class
329 * \see MLT_CONSUMER_SERVICE
332 mlt_service mlt_consumer_service( mlt_consumer this )
334 return this != NULL ? &this->parent : NULL;
337 /** Get the consumer properties.
339 * \public \memberof mlt_consumer_s
340 * \param this a consumer
341 * \return the consumer's properties list
342 * \see MLT_CONSUMER_PROPERTIES
345 mlt_properties mlt_consumer_properties( mlt_consumer this )
347 return this != NULL ? MLT_SERVICE_PROPERTIES( &this->parent ) : NULL;
350 /** Connect the consumer to the producer.
352 * \public \memberof mlt_consumer_s
353 * \param this a consumer
354 * \param producer a producer
355 * \return > 0 warning, == 0 success, < 0 serious error,
356 * 1 = this service does not accept input,
357 * 2 = the producer is invalid,
358 * 3 = the producer is already registered with this consumer
361 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
363 return mlt_service_connect_producer( &this->parent, producer, 0 );
366 /** Start the consumer.
368 * \public \memberof mlt_consumer_s
369 * \param this a consumer
370 * \return true if there was an error
373 int mlt_consumer_start( mlt_consumer this )
375 // Stop listening to the property-changed event
376 mlt_event_block( this->event_listener );
379 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
381 // Determine if there's a test card producer
382 char *test_card = mlt_properties_get( properties, "test_card" );
384 // Just to make sure nothing is hanging around...
385 mlt_frame_close( this->put );
387 this->put_active = 1;
390 if ( test_card != NULL )
392 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
394 // Create a test card producer
395 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
396 mlt_producer producer = mlt_factory_producer( profile, NULL, test_card );
398 // Do we have a producer
399 if ( producer != NULL )
401 // Test card should loop I guess...
402 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
403 //mlt_producer_set_speed( producer, 0 );
404 //mlt_producer_set_in_and_out( producer, 0, 0 );
406 // Set the test card on the consumer
407 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
413 // Allow the hash table to speed things up
414 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
417 // Set the frame duration in microseconds for the frame-dropping heuristic
418 int frame_duration = 1000000 / mlt_properties_get_int( properties, "frame_rate_num" ) *
419 mlt_properties_get_int( properties, "frame_rate_den" );
420 mlt_properties_set_int( properties, "frame_duration", frame_duration );
422 // Check and run an ante command
423 if ( mlt_properties_get( properties, "ante" ) )
424 if ( system( mlt_properties_get( properties, "ante" ) ) == -1 )
425 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "ante" ) );
427 // Set the real_time preference
428 this->real_time = mlt_properties_get_int( properties, "real_time" );
431 if ( this->start != NULL )
432 return this->start( this );
437 /** An alternative method to feed frames into the consumer.
439 * Only valid if the consumer itself is not connected.
441 * \public \memberof mlt_consumer_s
442 * \param this a consumer
443 * \param frame a frame
444 * \return true (ignore this for now)
447 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
451 // Get the service assoicated to the consumer
452 mlt_service service = MLT_CONSUMER_SERVICE( this );
454 if ( mlt_service_producer( service ) == NULL )
458 pthread_mutex_lock( &this->put_mutex );
459 while ( this->put_active && this->put != NULL )
461 gettimeofday( &now, NULL );
462 tm.tv_sec = now.tv_sec + 1;
463 tm.tv_nsec = now.tv_usec * 1000;
464 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
466 if ( this->put_active && this->put == NULL )
469 mlt_frame_close( frame );
470 pthread_cond_broadcast( &this->put_cond );
471 pthread_mutex_unlock( &this->put_mutex );
475 mlt_frame_close( frame );
481 /** Protected method for consumer to get frames from connected service
483 * \public \memberof mlt_consumer_s
484 * \param this a consumer
488 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
491 mlt_frame frame = NULL;
493 // Get the service assoicated to the consumer
494 mlt_service service = MLT_CONSUMER_SERVICE( this );
496 // Get the consumer properties
497 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
500 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
504 pthread_mutex_lock( &this->put_mutex );
505 while ( this->put_active && this->put == NULL )
507 gettimeofday( &now, NULL );
508 tm.tv_sec = now.tv_sec + 1;
509 tm.tv_nsec = now.tv_usec * 1000;
510 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
514 pthread_cond_broadcast( &this->put_cond );
515 pthread_mutex_unlock( &this->put_mutex );
517 mlt_service_apply_filters( service, frame, 0 );
519 else if ( mlt_service_producer( service ) != NULL )
521 mlt_service_get_frame( service, &frame, 0 );
525 frame = mlt_frame_init( service );
530 // Get the frame properties
531 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
533 // Get the test card producer
534 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
536 // Attach the test frame producer to it.
537 if ( test_card != NULL )
538 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
540 // Attach the rescale property
541 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
543 // Aspect ratio and other jiggery pokery
544 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
545 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
546 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
553 /** Compute the time difference between now and a time value.
555 * \private \memberof mlt_consumer_s
556 * \param time1 a time value to be compared against now
557 * \return the difference in microseconds
560 static inline long time_difference( struct timeval *time1 )
562 struct timeval time2;
563 time2.tv_sec = time1->tv_sec;
564 time2.tv_usec = time1->tv_usec;
565 gettimeofday( time1, NULL );
566 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
569 /** The thread procedure for asynchronously pulling frames through the service
570 * network connected to a consumer.
572 * \private \memberof mlt_consumer_s
573 * \param arg a consumer
576 static void *consumer_read_ahead_thread( void *arg )
578 // The argument is the consumer
579 mlt_consumer this = arg;
581 // Get the properties of the consumer
582 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
584 // Get the width and height
585 int width = mlt_properties_get_int( properties, "width" );
586 int height = mlt_properties_get_int( properties, "height" );
588 // See if video is turned off
589 int video_off = mlt_properties_get_int( properties, "video_off" );
590 int preview_off = mlt_properties_get_int( properties, "preview_off" );
591 int preview_format = mlt_properties_get_int( properties, "preview_format" );
593 // Get the audio settings
594 mlt_audio_format afmt = mlt_audio_s16;
596 double fps = mlt_properties_get_double( properties, "fps" );
597 int channels = mlt_properties_get_int( properties, "channels" );
598 int frequency = mlt_properties_get_int( properties, "frequency" );
602 // See if audio is turned off
603 int audio_off = mlt_properties_get_int( properties, "audio_off" );
605 // Get the maximum size of the buffer
606 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
608 // General frame variable
609 mlt_frame frame = NULL;
610 uint8_t *image = NULL;
615 // Average time for get_frame and get_image
618 int64_t time_wait = 0;
619 int64_t time_frame = 0;
620 int64_t time_process = 0;
622 mlt_service lock_object = NULL;
624 if ( preview_off && preview_format != 0 )
625 this->format = preview_format;
627 // Get the first frame
628 frame = mlt_consumer_get_frame( this );
630 // Get the lock object
631 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
634 if ( lock_object ) mlt_service_lock( lock_object );
636 // Get the image of the first frame
639 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
640 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
645 samples = mlt_sample_calculator( fps, frequency, counter++ );
646 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
649 // Unlock the lock object
650 if ( lock_object ) mlt_service_unlock( lock_object );
653 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
655 // Get the starting time (can ignore the times above)
656 gettimeofday( &ante, NULL );
658 // Continue to read ahead
659 while ( this->ahead )
661 // Fetch width/height again
662 width = mlt_properties_get_int( properties, "width" );
663 height = mlt_properties_get_int( properties, "height" );
665 // Put the current frame into the queue
666 pthread_mutex_lock( &this->mutex );
667 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
668 pthread_cond_wait( &this->cond, &this->mutex );
669 mlt_deque_push_back( this->queue, frame );
670 pthread_cond_broadcast( &this->cond );
671 pthread_mutex_unlock( &this->mutex );
673 time_wait += time_difference( &ante );
675 // Get the next frame
676 frame = mlt_consumer_get_frame( this );
677 time_frame += time_difference( &ante );
679 // If there's no frame, we're probably stopped...
683 // Attempt to fetch the lock object
684 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
686 // Increment the count
689 // Lock if there's a lock object
690 if ( lock_object ) mlt_service_lock( lock_object );
692 // All non normal playback frames should be shown
693 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
695 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
696 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
707 if ( !skip_next || this->real_time == -1 )
709 // Get the image, mark as rendered and time it
712 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
713 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
715 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
719 // Increment the number of sequentially skipped frames
723 // If we've reached an unacceptable level, reset everything
734 // Always process audio
737 samples = mlt_sample_calculator( fps, frequency, counter++ );
738 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
741 // Increment the time take for this frame
742 time_process += time_difference( &ante );
744 // Determine if the next frame should be skipped
745 if ( mlt_deque_count( this->queue ) <= 5 )
747 int frame_duration = mlt_properties_get_int( properties, "frame_duration" );
748 if ( ( ( time_wait + time_frame + time_process ) / count ) > frame_duration )
752 // Unlock if there's a lock object
753 if ( lock_object ) mlt_service_unlock( lock_object );
756 // Remove the last frame
757 mlt_frame_close( frame );
762 /** Start the read/render thread.
764 * \private \memberof mlt_consumer_s
765 * \param this a consumer
768 static void consumer_read_ahead_start( mlt_consumer this )
773 // Create the frame queue
774 this->queue = mlt_deque_init( );
777 pthread_mutex_init( &this->mutex, NULL );
779 // Create the condition
780 pthread_cond_init( &this->cond, NULL );
782 // Create the read ahead
783 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "priority" ) )
785 struct sched_param priority;
786 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this ), "priority" );
787 pthread_attr_t thread_attributes;
788 pthread_attr_init( &thread_attributes );
789 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
790 pthread_attr_setschedparam( &thread_attributes, &priority );
791 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
792 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
793 if ( pthread_create( &this->ahead_thread, &thread_attributes, consumer_read_ahead_thread, this ) < 0 )
794 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
795 pthread_attr_destroy( &thread_attributes );
799 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
803 /** Stop the read/render thread.
805 * \private \memberof mlt_consumer_s
806 * \param this a consumer
809 static void consumer_read_ahead_stop( mlt_consumer this )
811 // Make sure we're running
814 // Inform thread to stop
817 // Broadcast to the condition in case it's waiting
818 pthread_mutex_lock( &this->mutex );
819 pthread_cond_broadcast( &this->cond );
820 pthread_mutex_unlock( &this->mutex );
822 // Broadcast to the put condition in case it's waiting
823 pthread_mutex_lock( &this->put_mutex );
824 pthread_cond_broadcast( &this->put_cond );
825 pthread_mutex_unlock( &this->put_mutex );
828 pthread_join( this->ahead_thread, NULL );
831 pthread_mutex_destroy( &this->mutex );
833 // Destroy the condition
834 pthread_cond_destroy( &this->cond );
837 while ( mlt_deque_count( this->queue ) )
838 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
841 mlt_deque_close( this->queue );
845 /** Flush the read/render thread's buffer.
847 * \public \memberof mlt_consumer_s
848 * \param this a consumer
851 void mlt_consumer_purge( mlt_consumer this )
855 pthread_mutex_lock( &this->mutex );
856 while ( mlt_deque_count( this->queue ) )
857 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
858 pthread_cond_broadcast( &this->cond );
859 pthread_mutex_unlock( &this->mutex );
863 /** Get the next frame from the producer connected to a consumer.
865 * Typically, one uses this instead of \p mlt_consumer_get_frame to make
866 * the asynchronous/real-time behavior configurable at runtime.
867 * You should close the frame returned from this when you are done with it.
869 * \public \memberof mlt_consumer_s
870 * \param this a consumer
874 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
877 mlt_frame frame = NULL;
879 // Get the properties
880 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
882 // Check if the user has requested real time or not
883 if ( this->real_time )
887 // Is the read ahead running?
888 if ( this->ahead == 0 )
890 int buffer = mlt_properties_get_int( properties, "buffer" );
891 int prefill = mlt_properties_get_int( properties, "prefill" );
892 consumer_read_ahead_start( this );
894 size = prefill > 0 && prefill < buffer ? prefill : buffer;
897 // Get frame from queue
898 pthread_mutex_lock( &this->mutex );
899 while( this->ahead && mlt_deque_count( this->queue ) < size )
900 pthread_cond_wait( &this->cond, &this->mutex );
901 frame = mlt_deque_pop_front( this->queue );
902 pthread_cond_broadcast( &this->cond );
903 pthread_mutex_unlock( &this->mutex );
907 // Get the frame in non real time
908 frame = mlt_consumer_get_frame( this );
910 // This isn't true, but from the consumers perspective it is
912 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
918 /** Callback for the implementation to indicate a stopped condition.
920 * \public \memberof mlt_consumer_s
921 * \param this a consumer
924 void mlt_consumer_stopped( mlt_consumer this )
926 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
927 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
928 mlt_event_unblock( this->event_listener );
931 /** Stop the consumer.
933 * \public \memberof mlt_consumer_s
934 * \param this a consumer
935 * \return true if there was an error
938 int mlt_consumer_stop( mlt_consumer this )
941 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
944 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping put waiting\n" );
945 pthread_mutex_lock( &this->put_mutex );
946 this->put_active = 0;
947 pthread_cond_broadcast( &this->put_cond );
948 pthread_mutex_unlock( &this->put_mutex );
951 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping consumer\n" );
952 if ( this->stop != NULL )
955 // Check if the user has requested real time or not and stop if necessary
956 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping read_ahead\n" );
957 if ( mlt_properties_get_int( properties, "real_time" ) )
958 consumer_read_ahead_stop( this );
960 // Kill the test card
961 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
963 // Check and run a post command
964 if ( mlt_properties_get( properties, "post" ) )
965 if (system( mlt_properties_get( properties, "post" ) ) == -1 )
966 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "post" ) );
968 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopped\n" );
973 /** Determine if the consumer is stopped.
975 * \public \memberof mlt_consumer_s
976 * \param this a consumer
977 * \return true if the consumer is stopped
980 int mlt_consumer_is_stopped( mlt_consumer this )
982 // Check if the consumer is stopped
983 if ( this->is_stopped != NULL )
984 return this->is_stopped( this );
989 /** Close and destroy the consumer.
991 * \public \memberof mlt_consumer_s
992 * \param this a consumer
995 void mlt_consumer_close( mlt_consumer this )
997 if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
999 // Get the childs close function
1000 void ( *consumer_close )( ) = this->close;
1002 if ( consumer_close )
1005 //mlt_consumer_stop( this );
1008 consumer_close( this );
1012 // Make sure it only gets called once
1013 this->parent.close = NULL;
1015 // Destroy the push mutex and condition
1016 pthread_mutex_destroy( &this->put_mutex );
1017 pthread_cond_destroy( &this->put_cond );
1019 mlt_service_close( &this->parent );