3 * \brief abstraction for all consumer services
6 * Copyright (C) 2003-2010 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
8 * \author Dan Dennedy <dan@dennedy.org>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "mlt_consumer.h"
26 #include "mlt_factory.h"
27 #include "mlt_producer.h"
28 #include "mlt_frame.h"
29 #include "mlt_profile.h"
37 /** Define this if you want an automatic deinterlace (if necessary) when the
38 * consumer's producer is not running at normal speed.
40 #undef DEINTERLACE_ON_NOT_NORMAL_SPEED
42 /** This is not the ideal place for this, but it is needed by VDPAU as well.
44 pthread_mutex_t mlt_sdl_mutex = PTHREAD_MUTEX_INITIALIZER;
46 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
47 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
48 static void mlt_consumer_property_changed( mlt_properties owner, mlt_consumer this, char *name );
49 static void apply_profile_properties( mlt_consumer this, mlt_profile profile, mlt_properties properties );
50 static void on_consumer_frame_show( mlt_properties owner, mlt_consumer this, mlt_frame frame );
52 /** Initialize a consumer service.
54 * \public \memberof mlt_consumer_s
55 * \param this the consumer to initialize
56 * \param child a pointer to the object for the subclass
57 * \param profile the \p mlt_profile_s to use (optional but recommended,
58 * uses the environment variable MLT if this is NULL)
59 * \return true if there was an error
62 int mlt_consumer_init( mlt_consumer this, void *child, mlt_profile profile )
65 memset( this, 0, sizeof( struct mlt_consumer_s ) );
67 error = mlt_service_init( &this->parent, this );
70 // Get the properties from the service
71 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
73 // Apply profile to properties
74 if ( profile == NULL )
76 // Normally the application creates the profile and controls its lifetime
77 // This is the fallback exception handling
78 profile = mlt_profile_init( NULL );
79 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
80 mlt_properties_set_data( properties, "_profile", profile, 0, (mlt_destructor)mlt_profile_close, NULL );
82 apply_profile_properties( this, profile, properties );
84 // Default rescaler for all consumers
85 mlt_properties_set( properties, "rescale", "bilinear" );
87 // Default read ahead buffer size
88 mlt_properties_set_int( properties, "buffer", 25 );
90 // Default audio frequency and channels
91 mlt_properties_set_int( properties, "frequency", 48000 );
92 mlt_properties_set_int( properties, "channels", 2 );
94 // Default of all consumers is real time
95 mlt_properties_set_int( properties, "real_time", 1 );
97 // Default to environment test card
98 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
100 // Hmm - default all consumers to yuv422 :-/
101 this->format = mlt_image_yuv422;
103 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
104 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
105 mlt_events_register( properties, "consumer-stopped", NULL );
106 mlt_events_listen( properties, this, "consumer-frame-show", ( mlt_listener )on_consumer_frame_show );
108 // Register a property-changed listener to handle the profile property -
109 // subsequent properties can override the profile
110 this->event_listener = mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_consumer_property_changed );
112 // Create the push mutex and condition
113 pthread_mutex_init( &this->put_mutex, NULL );
114 pthread_cond_init( &this->put_cond, NULL );
120 /** Convert the profile into properties on the consumer.
122 * \private \memberof mlt_consumer_s
123 * \param this a consumer
124 * \param profile a profile
125 * \param properties a properties list (typically, the consumer's)
128 static void apply_profile_properties( mlt_consumer this, mlt_profile profile, mlt_properties properties )
130 mlt_event_block( this->event_listener );
131 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
132 mlt_properties_set_int( properties, "frame_rate_num", profile->frame_rate_num );
133 mlt_properties_set_int( properties, "frame_rate_den", profile->frame_rate_den );
134 mlt_properties_set_int( properties, "width", profile->width );
135 mlt_properties_set_int( properties, "height", profile->height );
136 mlt_properties_set_int( properties, "progressive", profile->progressive );
137 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
138 mlt_properties_set_int( properties, "sample_aspect_num", profile->sample_aspect_num );
139 mlt_properties_set_int( properties, "sample_aspect_den", profile->sample_aspect_den );
140 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
141 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
142 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
143 mlt_properties_set_int( properties, "colorspace", profile->colorspace );
144 mlt_event_unblock( this->event_listener );
147 /** The property-changed event listener
149 * \private \memberof mlt_consumer_s
150 * \param owner the events object
151 * \param this the consumer
152 * \param name the name of the property that changed
155 static void mlt_consumer_property_changed( mlt_properties owner, mlt_consumer this, char *name )
157 if ( !strcmp( name, "profile" ) )
160 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
162 // Get the current profile
163 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
165 // Load the new profile
166 mlt_profile new_profile = mlt_profile_init( mlt_properties_get( properties, name ) );
171 if ( profile != NULL )
173 free( profile->description );
174 memcpy( profile, new_profile, sizeof( struct mlt_profile_s ) );
175 profile->description = strdup( new_profile->description );
176 mlt_profile_close( new_profile );
180 profile = new_profile;
183 // Apply to properties
184 apply_profile_properties( this, profile, properties );
187 else if ( !strcmp( name, "frame_rate_num" ) )
189 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
190 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
193 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
194 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
197 else if ( !strcmp( name, "frame_rate_den" ) )
199 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
200 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
203 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
204 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
207 else if ( !strcmp( name, "width" ) )
209 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
210 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
212 profile->width = mlt_properties_get_int( properties, "width" );
214 else if ( !strcmp( name, "height" ) )
216 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
217 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
219 profile->height = mlt_properties_get_int( properties, "height" );
221 else if ( !strcmp( name, "progressive" ) )
223 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
224 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
226 profile->progressive = mlt_properties_get_int( properties, "progressive" );
228 else if ( !strcmp( name, "sample_aspect_num" ) )
230 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
231 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
232 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
234 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
236 else if ( !strcmp( name, "sample_aspect_den" ) )
238 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
239 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
240 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
242 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
244 else if ( !strcmp( name, "display_aspect_num" ) )
246 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
247 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
250 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
251 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
254 else if ( !strcmp( name, "display_aspect_den" ) )
256 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
257 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
260 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
261 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
264 else if ( !strcmp( name, "colorspace" ) )
266 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
267 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
269 profile->colorspace = mlt_properties_get_int( properties, "colorspace" );
273 /** The transmitter for the consumer-frame-show event
275 * Invokes the listener.
277 * \private \memberof mlt_consumer_s
278 * \param listener a function pointer that will be invoked
279 * \param owner the events object that will be passed to \p listener
280 * \param this a service that will be passed to \p listener
281 * \param args an array of pointers - the first entry is passed as a string to \p listener
284 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
286 if ( listener != NULL )
287 listener( owner, this, ( mlt_frame )args[ 0 ] );
290 /** The transmitter for the consumer-frame-render event
292 * Invokes the listener.
294 * \private \memberof mlt_consumer_s
295 * \param listener a function pointer that will be invoked
296 * \param owner the events object that will be passed to \p listener
297 * \param this a service that will be passed to \p listener
298 * \param args an array of pointers - the first entry is passed as a string to \p listener
301 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
303 if ( listener != NULL )
304 listener( owner, this, ( mlt_frame )args[ 0 ] );
307 /** A listener on the consumer-frame-show event
309 * Saves the position of the frame shown.
311 * \private \memberof mlt_consumer_s
312 * \param owner the events object
313 * \param consumer the consumer on which this event occurred
314 * \param frame the frame that was shown
317 static void on_consumer_frame_show( mlt_properties owner, mlt_consumer consumer, mlt_frame frame )
320 consumer->position = mlt_frame_get_position( frame );
323 /** Create a new consumer.
325 * \public \memberof mlt_consumer_s
326 * \param profile a profile (optional, but recommended)
327 * \return a new consumer
330 mlt_consumer mlt_consumer_new( mlt_profile profile )
332 // Create the memory for the structure
333 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
337 mlt_consumer_init( this, NULL, profile );
343 /** Get the parent service object.
345 * \public \memberof mlt_consumer_s
346 * \param this a consumer
347 * \return the parent service class
348 * \see MLT_CONSUMER_SERVICE
351 mlt_service mlt_consumer_service( mlt_consumer this )
353 return this != NULL ? &this->parent : NULL;
356 /** Get the consumer properties.
358 * \public \memberof mlt_consumer_s
359 * \param this a consumer
360 * \return the consumer's properties list
361 * \see MLT_CONSUMER_PROPERTIES
364 mlt_properties mlt_consumer_properties( mlt_consumer this )
366 return this != NULL ? MLT_SERVICE_PROPERTIES( &this->parent ) : NULL;
369 /** Connect the consumer to the producer.
371 * \public \memberof mlt_consumer_s
372 * \param this a consumer
373 * \param producer a producer
374 * \return > 0 warning, == 0 success, < 0 serious error,
375 * 1 = this service does not accept input,
376 * 2 = the producer is invalid,
377 * 3 = the producer is already registered with this consumer
380 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
382 return mlt_service_connect_producer( &this->parent, producer, 0 );
385 /** Start the consumer.
387 * \public \memberof mlt_consumer_s
388 * \param this a consumer
389 * \return true if there was an error
392 int mlt_consumer_start( mlt_consumer this )
394 // Stop listening to the property-changed event
395 mlt_event_block( this->event_listener );
398 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
400 // Determine if there's a test card producer
401 char *test_card = mlt_properties_get( properties, "test_card" );
403 // Just to make sure nothing is hanging around...
404 mlt_frame_close( this->put );
406 this->put_active = 1;
409 if ( test_card != NULL )
411 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
413 // Create a test card producer
414 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
415 mlt_producer producer = mlt_factory_producer( profile, NULL, test_card );
417 // Do we have a producer
418 if ( producer != NULL )
420 // Test card should loop I guess...
421 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
422 //mlt_producer_set_speed( producer, 0 );
423 //mlt_producer_set_in_and_out( producer, 0, 0 );
425 // Set the test card on the consumer
426 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
432 // Allow the hash table to speed things up
433 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
436 // Set the frame duration in microseconds for the frame-dropping heuristic
437 int frame_duration = 1000000 / mlt_properties_get_int( properties, "frame_rate_num" ) *
438 mlt_properties_get_int( properties, "frame_rate_den" );
439 mlt_properties_set_int( properties, "frame_duration", frame_duration );
441 // Check and run an ante command
442 if ( mlt_properties_get( properties, "ante" ) )
443 if ( system( mlt_properties_get( properties, "ante" ) ) == -1 )
444 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "ante" ) );
446 // Set the real_time preference
447 this->real_time = mlt_properties_get_int( properties, "real_time" );
449 // For worker threads implementation, buffer must be at least # threads
450 if ( abs( this->real_time ) > 1 && mlt_properties_get_int( properties, "buffer" ) <= abs( this->real_time ) )
451 mlt_properties_set_int( properties, "buffer", abs( this->real_time ) + 1 );
454 if ( this->start != NULL )
455 return this->start( this );
460 /** An alternative method to feed frames into the consumer.
462 * Only valid if the consumer itself is not connected.
464 * \public \memberof mlt_consumer_s
465 * \param this a consumer
466 * \param frame a frame
467 * \return true (ignore this for now)
470 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
474 // Get the service assoicated to the consumer
475 mlt_service service = MLT_CONSUMER_SERVICE( this );
477 if ( mlt_service_producer( service ) == NULL )
481 pthread_mutex_lock( &this->put_mutex );
482 while ( this->put_active && this->put != NULL )
484 gettimeofday( &now, NULL );
485 tm.tv_sec = now.tv_sec + 1;
486 tm.tv_nsec = now.tv_usec * 1000;
487 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
489 if ( this->put_active && this->put == NULL )
492 mlt_frame_close( frame );
493 pthread_cond_broadcast( &this->put_cond );
494 pthread_mutex_unlock( &this->put_mutex );
498 mlt_frame_close( frame );
504 /** Protected method for consumer to get frames from connected service
506 * \public \memberof mlt_consumer_s
507 * \param this a consumer
511 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
514 mlt_frame frame = NULL;
516 // Get the service assoicated to the consumer
517 mlt_service service = MLT_CONSUMER_SERVICE( this );
519 // Get the consumer properties
520 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
523 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
527 pthread_mutex_lock( &this->put_mutex );
528 while ( this->put_active && this->put == NULL )
530 gettimeofday( &now, NULL );
531 tm.tv_sec = now.tv_sec + 1;
532 tm.tv_nsec = now.tv_usec * 1000;
533 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
537 pthread_cond_broadcast( &this->put_cond );
538 pthread_mutex_unlock( &this->put_mutex );
540 mlt_service_apply_filters( service, frame, 0 );
542 else if ( mlt_service_producer( service ) != NULL )
544 mlt_service_get_frame( service, &frame, 0 );
548 frame = mlt_frame_init( service );
553 // Get the frame properties
554 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
556 // Get the test card producer
557 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
559 // Attach the test frame producer to it.
560 if ( test_card != NULL )
561 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
563 // Attach the rescale property
564 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
566 // Aspect ratio and other jiggery pokery
567 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
568 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
569 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
576 /** Compute the time difference between now and a time value.
578 * \private \memberof mlt_consumer_s
579 * \param time1 a time value to be compared against now
580 * \return the difference in microseconds
583 static inline long time_difference( struct timeval *time1 )
585 struct timeval time2;
586 time2.tv_sec = time1->tv_sec;
587 time2.tv_usec = time1->tv_usec;
588 gettimeofday( time1, NULL );
589 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
592 /** The thread procedure for asynchronously pulling frames through the service
593 * network connected to a consumer.
595 * \private \memberof mlt_consumer_s
596 * \param arg a consumer
599 static void *consumer_read_ahead_thread( void *arg )
601 // The argument is the consumer
602 mlt_consumer this = arg;
604 // Get the properties of the consumer
605 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
607 // Get the width and height
608 int width = mlt_properties_get_int( properties, "width" );
609 int height = mlt_properties_get_int( properties, "height" );
611 // See if video is turned off
612 int video_off = mlt_properties_get_int( properties, "video_off" );
613 int preview_off = mlt_properties_get_int( properties, "preview_off" );
614 int preview_format = mlt_properties_get_int( properties, "preview_format" );
616 // Get the audio settings
617 mlt_audio_format afmt = mlt_audio_s16;
619 double fps = mlt_properties_get_double( properties, "fps" );
620 int channels = mlt_properties_get_int( properties, "channels" );
621 int frequency = mlt_properties_get_int( properties, "frequency" );
625 // See if audio is turned off
626 int audio_off = mlt_properties_get_int( properties, "audio_off" );
628 // Get the maximum size of the buffer
629 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
631 // General frame variable
632 mlt_frame frame = NULL;
633 uint8_t *image = NULL;
638 // Average time for get_frame and get_image
641 int64_t time_wait = 0;
642 int64_t time_frame = 0;
643 int64_t time_process = 0;
646 if ( preview_off && preview_format != 0 )
647 this->format = preview_format;
649 // Get the first frame
650 frame = mlt_consumer_get_frame( this );
652 // Get the image of the first frame
655 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
656 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
661 samples = mlt_sample_calculator( fps, frequency, counter++ );
662 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
666 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
668 // Get the starting time (can ignore the times above)
669 gettimeofday( &ante, NULL );
671 // Continue to read ahead
672 while ( this->ahead )
674 // Fetch width/height again
675 width = mlt_properties_get_int( properties, "width" );
676 height = mlt_properties_get_int( properties, "height" );
678 // Put the current frame into the queue
679 pthread_mutex_lock( &this->queue_mutex );
680 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
681 pthread_cond_wait( &this->queue_cond, &this->queue_mutex );
682 mlt_deque_push_back( this->queue, frame );
683 pthread_cond_broadcast( &this->queue_cond );
684 pthread_mutex_unlock( &this->queue_mutex );
686 time_wait += time_difference( &ante );
688 // Get the next frame
689 frame = mlt_consumer_get_frame( this );
690 time_frame += time_difference( &ante );
692 // If there's no frame, we're probably stopped...
696 // Increment the count
699 // All non normal playback frames should be shown
700 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
702 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
703 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
714 if ( !skip_next || this->real_time == -1 )
716 // Get the image, mark as rendered and time it
719 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
720 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
722 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
726 // Increment the number of sequentially skipped frames
730 // If we've reached an unacceptable level, reset everything
731 if ( skipped > fps * 2 )
741 // Always process audio
744 samples = mlt_sample_calculator( fps, frequency, counter++ );
745 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
748 // Increment the time take for this frame
749 time_process += time_difference( &ante );
751 // Determine if the next frame should be skipped
752 if ( mlt_deque_count( this->queue ) <= 5 )
754 int frame_duration = mlt_properties_get_int( properties, "frame_duration" );
755 if ( ( ( time_wait + time_frame + time_process ) / count ) > frame_duration )
760 // Remove the last frame
761 mlt_frame_close( frame );
766 /** Locate the first unprocessed frame in the queue.
768 * When playing with realtime behavior, we do not use the true head, but
769 * rather an adjusted process_head. The process_head is adjusted based on
770 * the rate of frame-dropping or recovery from frame-dropping. The idea is
771 * that as the level of frame-dropping increases to move the process_head
772 * closer to the tail because the frames are not completing processing prior
773 * to their playout! Then, as frames are not dropped the process_head moves
774 * back closer to the head of the queue so that worker threads can work
775 * ahead of the playout point (queue head).
777 * \private \memberof mlt_consumer_s
778 * \param this a consumer
779 * \return an index into the queue
782 static inline int first_unprocessed_frame( mlt_consumer this )
784 int index = this->real_time <= 0 ? 0 : this->process_head;
785 while ( index < mlt_deque_count( this->queue ) && MLT_FRAME( mlt_deque_peek( this->queue, index ) )->is_processing )
790 /** The worker thread procedure for parallel processing frames.
792 * \private \memberof mlt_consumer_s
793 * \param arg a consumer
796 static void *consumer_worker_thread( void *arg )
798 // The argument is the consumer
799 mlt_consumer this = arg;
801 // Get the properties of the consumer
802 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
804 // Get the width and height
805 int width = mlt_properties_get_int( properties, "width" );
806 int height = mlt_properties_get_int( properties, "height" );
807 mlt_image_format format = this->format;
809 // See if video is turned off
810 int video_off = mlt_properties_get_int( properties, "video_off" );
811 int preview_off = mlt_properties_get_int( properties, "preview_off" );
812 int preview_format = mlt_properties_get_int( properties, "preview_format" );
814 // General frame variable
815 mlt_frame frame = NULL;
816 uint8_t *image = NULL;
818 if ( preview_off && preview_format != 0 )
819 format = preview_format;
821 // Continue to read ahead
822 while ( this->ahead )
824 // Get the next unprocessed frame from the work queue
825 pthread_mutex_lock( &this->queue_mutex );
826 int index = first_unprocessed_frame( this );
827 while ( this->ahead && index >= mlt_deque_count( this->queue ) )
829 mlt_log_debug( MLT_CONSUMER_SERVICE(this), "waiting in worker index = %d queue count = %d\n",
830 index, mlt_deque_count( this->queue ) );
831 pthread_cond_wait( &this->queue_cond, &this->queue_mutex );
832 index = first_unprocessed_frame( this );
835 // Mark the frame for processing
836 frame = mlt_deque_peek( this->queue, index );
839 mlt_log_debug( MLT_CONSUMER_SERVICE(this), "worker processing index = %d frame %d queue count = %d\n",
840 index, mlt_frame_get_position(frame), mlt_deque_count( this->queue ) );
841 frame->is_processing = 1;
842 mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( frame ) );
844 pthread_mutex_unlock( &this->queue_mutex );
846 // If there's no frame, we're probably stopped...
850 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
851 // All non normal playback frames should be shown
852 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
853 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
859 // Fetch width/height again
860 width = mlt_properties_get_int( properties, "width" );
861 height = mlt_properties_get_int( properties, "height" );
862 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
863 mlt_frame_get_image( frame, &image, &format, &width, &height, 0 );
865 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
866 mlt_frame_close( frame );
868 // Tell a waiting thread (non-realtime main consumer thread) that we are done.
869 pthread_mutex_lock( &this->done_mutex );
870 pthread_cond_broadcast( &this->done_cond );
871 pthread_mutex_unlock( &this->done_mutex );
877 /** Start the read/render thread.
879 * \private \memberof mlt_consumer_s
880 * \param this a consumer
883 static void consumer_read_ahead_start( mlt_consumer this )
888 // Create the frame queue
889 this->queue = mlt_deque_init( );
891 // Create the queue mutex
892 pthread_mutex_init( &this->queue_mutex, NULL );
894 // Create the condition
895 pthread_cond_init( &this->queue_cond, NULL );
897 // Create the read ahead
898 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "priority" ) )
900 struct sched_param priority;
901 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this ), "priority" );
902 pthread_attr_t thread_attributes;
903 pthread_attr_init( &thread_attributes );
904 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
905 pthread_attr_setschedparam( &thread_attributes, &priority );
906 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
907 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
908 if ( pthread_create( &this->ahead_thread, &thread_attributes, consumer_read_ahead_thread, this ) < 0 )
909 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
910 pthread_attr_destroy( &thread_attributes );
914 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
918 /** Start the worker threads.
920 * \private \memberof mlt_consumer_s
921 * \param this a consumer
924 static void consumer_work_start( mlt_consumer this )
926 int n = abs( this->real_time );
927 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
932 // These keep track of the accelleration of frame dropping or recovery.
933 this->consecutive_dropped = 0;
934 this->consecutive_rendered = 0;
936 // This is the position in the queue from which to look for a frame to process.
937 // If we always start from the head, then we may likely not complete processing
938 // before the frame is played out.
939 this->process_head = 0;
942 this->queue = mlt_deque_init();
943 this->worker_threads = mlt_deque_init();
945 // Create the mutexes
946 pthread_mutex_init( &this->queue_mutex, NULL );
947 pthread_mutex_init( &this->done_mutex, NULL );
949 // Create the conditions
950 pthread_cond_init( &this->queue_cond, NULL );
951 pthread_cond_init( &this->done_cond, NULL );
953 // Create the read ahead
954 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "priority" ) )
957 struct sched_param priority;
958 pthread_attr_t thread_attributes;
960 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this ), "priority" );
961 pthread_attr_init( &thread_attributes );
962 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
963 pthread_attr_setschedparam( &thread_attributes, &priority );
964 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
965 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
969 if ( pthread_create( thread, &thread_attributes, consumer_read_ahead_thread, this ) < 0 )
970 if ( pthread_create( thread, NULL, consumer_read_ahead_thread, this ) == 0 )
971 mlt_deque_push_back( this->worker_threads, thread );
973 pthread_attr_destroy( &thread_attributes );
980 if ( pthread_create( thread, NULL, consumer_worker_thread, this ) == 0 )
981 mlt_deque_push_back( this->worker_threads, thread );
986 /** Stop the read/render thread.
988 * \private \memberof mlt_consumer_s
989 * \param this a consumer
992 static void consumer_read_ahead_stop( mlt_consumer this )
994 // Make sure we're running
997 // Inform thread to stop
1000 // Broadcast to the condition in case it's waiting
1001 pthread_mutex_lock( &this->queue_mutex );
1002 pthread_cond_broadcast( &this->queue_cond );
1003 pthread_mutex_unlock( &this->queue_mutex );
1005 // Broadcast to the put condition in case it's waiting
1006 pthread_mutex_lock( &this->put_mutex );
1007 pthread_cond_broadcast( &this->put_cond );
1008 pthread_mutex_unlock( &this->put_mutex );
1011 pthread_join( this->ahead_thread, NULL );
1013 // Destroy the frame queue mutex
1014 pthread_mutex_destroy( &this->queue_mutex );
1016 // Destroy the condition
1017 pthread_cond_destroy( &this->queue_cond );
1020 while ( mlt_deque_count( this->queue ) )
1021 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
1024 mlt_deque_close( this->queue );
1028 /** Stop the worker threads.
1030 * \private \memberof mlt_consumer_s
1031 * \param this a consumer
1034 static void consumer_work_stop( mlt_consumer this )
1036 // Make sure we're running
1039 // Inform thread to stop
1042 // Broadcast to the queue condition in case it's waiting
1043 pthread_mutex_lock( &this->queue_mutex );
1044 pthread_cond_broadcast( &this->queue_cond );
1045 pthread_mutex_unlock( &this->queue_mutex );
1047 // Broadcast to the put condition in case it's waiting
1048 pthread_mutex_lock( &this->put_mutex );
1049 pthread_cond_broadcast( &this->put_cond );
1050 pthread_mutex_unlock( &this->put_mutex );
1054 while ( ( thread = mlt_deque_pop_front( this->worker_threads ) ) )
1055 pthread_join( *thread, NULL );
1057 // Destroy the mutexes
1058 pthread_mutex_destroy( &this->queue_mutex );
1059 pthread_mutex_destroy( &this->done_mutex );
1061 // Destroy the conditions
1062 pthread_cond_destroy( &this->queue_cond );
1063 pthread_cond_destroy( &this->done_cond );
1066 while ( mlt_deque_count( this->queue ) )
1067 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
1070 mlt_deque_close( this->queue );
1071 mlt_deque_close( this->worker_threads );
1075 /** Flush the read/render thread's buffer.
1077 * \public \memberof mlt_consumer_s
1078 * \param this a consumer
1081 void mlt_consumer_purge( mlt_consumer this )
1085 pthread_mutex_lock( &this->queue_mutex );
1086 while ( mlt_deque_count( this->queue ) )
1087 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
1088 pthread_cond_broadcast( &this->queue_cond );
1089 pthread_mutex_unlock( &this->queue_mutex );
1093 /** Use multiple worker threads and a work queue.
1096 static mlt_frame worker_get_frame( mlt_consumer this, mlt_properties properties )
1099 mlt_frame frame = NULL;
1101 int size = abs( this->real_time );
1102 int buffer = mlt_properties_get_int( properties, "buffer" );
1103 // This is a heuristic to determine a suitable minimum buffer size for the number of threads.
1104 int headroom = 2 + size * size;
1105 buffer = buffer < headroom ? headroom : buffer;
1107 // Start worker threads if not already started.
1108 if ( ! this->ahead )
1110 int prefill = mlt_properties_get_int( properties, "prefill" );
1111 prefill = prefill > 0 && prefill < buffer ? prefill : buffer;
1113 consumer_work_start( this );
1115 // Fill the work queue.
1119 frame = mlt_consumer_get_frame( this );
1122 pthread_mutex_lock( &this->queue_mutex );
1123 mlt_deque_push_back( this->queue, frame );
1124 pthread_cond_signal( &this->queue_cond );
1125 pthread_mutex_unlock( &this->queue_mutex );
1130 while ( this->ahead && first_unprocessed_frame( this ) < prefill )
1132 pthread_mutex_lock( &this->done_mutex );
1133 pthread_cond_wait( &this->done_cond, &this->done_mutex );
1134 pthread_mutex_unlock( &this->done_mutex );
1136 this->process_head = size;
1139 mlt_log_debug( MLT_CONSUMER_SERVICE(this), "size %d done count %d work count %d process_head %d\n",
1140 size, first_unprocessed_frame( this ), mlt_deque_count( this->queue ), this->process_head );
1142 // Feed the work queue
1143 frame = mlt_consumer_get_frame( this );
1146 pthread_mutex_lock( &this->queue_mutex );
1147 mlt_deque_push_back( this->queue, frame );
1148 pthread_cond_signal( &this->queue_cond );
1149 pthread_mutex_unlock( &this->queue_mutex );
1151 // Wait if not realtime.
1152 while( this->ahead && this->real_time < 0 &&
1153 ! mlt_properties_get_int( MLT_FRAME_PROPERTIES( MLT_FRAME( mlt_deque_peek_front( this->queue ) ) ), "rendered" ) )
1155 pthread_mutex_lock( &this->done_mutex );
1156 pthread_cond_wait( &this->done_cond, &this->done_mutex );
1157 pthread_mutex_unlock( &this->done_mutex );
1160 // Get the frame from the queue.
1161 pthread_mutex_lock( &this->queue_mutex );
1162 frame = mlt_deque_pop_front( this->queue );
1163 pthread_mutex_unlock( &this->queue_mutex );
1165 // Adapt the worker process head to the runtime conditions.
1166 if ( this->real_time > 0 )
1168 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "rendered" ) )
1170 this->consecutive_dropped = 0;
1171 if ( this->process_head > size && this->consecutive_rendered >= this->process_head )
1172 this->process_head--;
1174 this->consecutive_rendered++;
1178 this->consecutive_rendered = 0;
1179 if ( this->process_head < buffer - size && this->consecutive_dropped > size )
1180 this->process_head++;
1182 this->consecutive_dropped++;
1184 mlt_log_debug( MLT_CONSUMER_SERVICE(this), "dropped %d rendered %d process_head %d\n",
1185 this->consecutive_dropped, this->consecutive_rendered, this->process_head );
1191 /** Get the next frame from the producer connected to a consumer.
1193 * Typically, one uses this instead of \p mlt_consumer_get_frame to make
1194 * the asynchronous/real-time behavior configurable at runtime.
1195 * You should close the frame returned from this when you are done with it.
1197 * \public \memberof mlt_consumer_s
1198 * \param this a consumer
1202 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
1205 mlt_frame frame = NULL;
1207 // Get the properties
1208 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
1210 // Check if the user has requested real time or not
1211 if ( this->real_time > 1 || this->real_time < -1 )
1214 return worker_get_frame( this, properties );
1216 else if ( this->real_time == 1 || this->real_time == -1 )
1220 // Is the read ahead running?
1221 if ( this->ahead == 0 )
1223 int buffer = mlt_properties_get_int( properties, "buffer" );
1224 int prefill = mlt_properties_get_int( properties, "prefill" );
1225 consumer_read_ahead_start( this );
1227 size = prefill > 0 && prefill < buffer ? prefill : buffer;
1230 // Get frame from queue
1231 pthread_mutex_lock( &this->queue_mutex );
1232 while( this->ahead && mlt_deque_count( this->queue ) < size )
1233 pthread_cond_wait( &this->queue_cond, &this->queue_mutex );
1234 frame = mlt_deque_pop_front( this->queue );
1235 pthread_cond_broadcast( &this->queue_cond );
1236 pthread_mutex_unlock( &this->queue_mutex );
1238 else // real_time == 0
1240 // Get the frame in non real time
1241 frame = mlt_consumer_get_frame( this );
1243 // This isn't true, but from the consumers perspective it is
1244 if ( frame != NULL )
1245 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
1251 /** Callback for the implementation to indicate a stopped condition.
1253 * \public \memberof mlt_consumer_s
1254 * \param this a consumer
1257 void mlt_consumer_stopped( mlt_consumer this )
1259 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
1260 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
1261 mlt_event_unblock( this->event_listener );
1264 /** Stop the consumer.
1266 * \public \memberof mlt_consumer_s
1267 * \param this a consumer
1268 * \return true if there was an error
1271 int mlt_consumer_stop( mlt_consumer this )
1273 // Get the properies
1274 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
1277 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping put waiting\n" );
1278 pthread_mutex_lock( &this->put_mutex );
1279 this->put_active = 0;
1280 pthread_cond_broadcast( &this->put_cond );
1281 pthread_mutex_unlock( &this->put_mutex );
1283 // Stop the consumer
1284 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping consumer\n" );
1285 if ( this->stop != NULL )
1288 // Check if the user has requested real time or not and stop if necessary
1289 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping read_ahead\n" );
1290 if ( abs( this->real_time ) == 1 )
1291 consumer_read_ahead_stop( this );
1292 else if ( abs( this->real_time ) > 1 )
1293 consumer_work_stop( this );
1295 // Kill the test card
1296 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
1298 // Check and run a post command
1299 if ( mlt_properties_get( properties, "post" ) )
1300 if (system( mlt_properties_get( properties, "post" ) ) == -1 )
1301 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "post" ) );
1303 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopped\n" );
1308 /** Determine if the consumer is stopped.
1310 * \public \memberof mlt_consumer_s
1311 * \param this a consumer
1312 * \return true if the consumer is stopped
1315 int mlt_consumer_is_stopped( mlt_consumer this )
1317 // Check if the consumer is stopped
1318 if ( this->is_stopped != NULL )
1319 return this->is_stopped( this );
1324 /** Close and destroy the consumer.
1326 * \public \memberof mlt_consumer_s
1327 * \param this a consumer
1330 void mlt_consumer_close( mlt_consumer this )
1332 if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
1334 // Get the childs close function
1335 void ( *consumer_close )( ) = this->close;
1337 if ( consumer_close )
1340 //mlt_consumer_stop( this );
1343 consumer_close( this );
1347 // Make sure it only gets called once
1348 this->parent.close = NULL;
1350 // Destroy the push mutex and condition
1351 pthread_mutex_destroy( &this->put_mutex );
1352 pthread_cond_destroy( &this->put_cond );
1354 mlt_service_close( &this->parent );
1359 /** Get the position of the last frame shown.
1361 * \public \memberof mlt_consumer_s
1362 * \param consumer a consumer
1363 * \return the position
1366 mlt_position mlt_consumer_position( mlt_consumer consumer )
1368 return consumer->position;