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 self, void **args );
47 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service self, void **args );
48 static void mlt_consumer_property_changed( mlt_properties owner, mlt_consumer self, char *name );
49 static void apply_profile_properties( mlt_consumer self, mlt_profile profile, mlt_properties properties );
50 static void on_consumer_frame_show( mlt_properties owner, mlt_consumer self, mlt_frame frame );
52 /** Initialize a consumer service.
54 * \public \memberof mlt_consumer_s
55 * \param self 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 self is NULL)
59 * \return true if there was an error
62 int mlt_consumer_init( mlt_consumer self, void *child, mlt_profile profile )
65 memset( self, 0, sizeof( struct mlt_consumer_s ) );
67 error = mlt_service_init( &self->parent, self );
70 // Get the properties from the service
71 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->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( self );
80 mlt_properties_set_data( properties, "_profile", profile, 0, (mlt_destructor)mlt_profile_close, NULL );
82 apply_profile_properties( self, 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 self->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, self, "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 self->event_listener = mlt_events_listen( properties, self, "property-changed", ( mlt_listener )mlt_consumer_property_changed );
112 // Create the push mutex and condition
113 pthread_mutex_init( &self->put_mutex, NULL );
114 pthread_cond_init( &self->put_cond, NULL );
120 /** Convert the profile into properties on the consumer.
122 * \private \memberof mlt_consumer_s
123 * \param self 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 self, mlt_profile profile, mlt_properties properties )
130 mlt_event_block( self->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( self->event_listener );
147 /** The property-changed event listener
149 * \private \memberof mlt_consumer_s
150 * \param owner the events object
151 * \param self the consumer
152 * \param name the name of the property that changed
155 static void mlt_consumer_property_changed( mlt_properties owner, mlt_consumer self, char *name )
157 if ( !strcmp( name, "profile" ) )
160 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
162 // Get the current profile
163 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
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( self, profile, properties );
187 else if ( !strcmp( name, "frame_rate_num" ) )
189 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
190 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
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( self );
200 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
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( self );
210 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
212 profile->width = mlt_properties_get_int( properties, "width" );
214 else if ( !strcmp( name, "height" ) )
216 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
217 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
219 profile->height = mlt_properties_get_int( properties, "height" );
221 else if ( !strcmp( name, "progressive" ) )
223 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
224 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
226 profile->progressive = mlt_properties_get_int( properties, "progressive" );
228 else if ( !strcmp( name, "sample_aspect_num" ) )
230 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
231 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
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( self );
239 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
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( self );
247 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
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( self );
257 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
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( self );
267 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
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 self 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 self, void **args )
286 if ( listener != NULL )
287 listener( owner, self, ( 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 self 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 self, void **args )
303 if ( listener != NULL )
304 listener( owner, self, ( 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 self = malloc( sizeof( struct mlt_consumer_s ) );
337 mlt_consumer_init( self, NULL, profile );
343 /** Get the parent service object.
345 * \public \memberof mlt_consumer_s
346 * \param self a consumer
347 * \return the parent service class
348 * \see MLT_CONSUMER_SERVICE
351 mlt_service mlt_consumer_service( mlt_consumer self )
353 return self != NULL ? &self->parent : NULL;
356 /** Get the consumer properties.
358 * \public \memberof mlt_consumer_s
359 * \param self a consumer
360 * \return the consumer's properties list
361 * \see MLT_CONSUMER_PROPERTIES
364 mlt_properties mlt_consumer_properties( mlt_consumer self )
366 return self != NULL ? MLT_SERVICE_PROPERTIES( &self->parent ) : NULL;
369 /** Connect the consumer to the producer.
371 * \public \memberof mlt_consumer_s
372 * \param self 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 self, mlt_service producer )
382 return mlt_service_connect_producer( &self->parent, producer, 0 );
385 /** Start the consumer.
387 * \public \memberof mlt_consumer_s
388 * \param self a consumer
389 * \return true if there was an error
392 int mlt_consumer_start( mlt_consumer self )
394 if ( !mlt_consumer_is_stopped( self ) )
397 // Stop listening to the property-changed event
398 mlt_event_block( self->event_listener );
401 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
403 // Determine if there's a test card producer
404 char *test_card = mlt_properties_get( properties, "test_card" );
406 // Just to make sure nothing is hanging around...
408 self->put_active = 1;
411 if ( test_card != NULL )
413 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
415 // Create a test card producer
416 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
417 mlt_producer producer = mlt_factory_producer( profile, NULL, test_card );
419 // Do we have a producer
420 if ( producer != NULL )
422 // Test card should loop I guess...
423 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
424 //mlt_producer_set_speed( producer, 0 );
425 //mlt_producer_set_in_and_out( producer, 0, 0 );
427 // Set the test card on the consumer
428 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
434 // Allow the hash table to speed things up
435 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
438 // Set the frame duration in microseconds for the frame-dropping heuristic
439 int frame_duration = 1000000 / mlt_properties_get_int( properties, "frame_rate_num" ) *
440 mlt_properties_get_int( properties, "frame_rate_den" );
441 mlt_properties_set_int( properties, "frame_duration", frame_duration );
443 // Check and run an ante command
444 if ( mlt_properties_get( properties, "ante" ) )
445 if ( system( mlt_properties_get( properties, "ante" ) ) == -1 )
446 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "ante" ) );
448 // Set the real_time preference
449 self->real_time = mlt_properties_get_int( properties, "real_time" );
451 // For worker threads implementation, buffer must be at least # threads
452 if ( abs( self->real_time ) > 1 && mlt_properties_get_int( properties, "buffer" ) <= abs( self->real_time ) )
453 mlt_properties_set_int( properties, "buffer", abs( self->real_time ) + 1 );
456 if ( self->start != NULL )
457 return self->start( self );
462 /** An alternative method to feed frames into the consumer.
464 * Only valid if the consumer itself is not connected.
466 * \public \memberof mlt_consumer_s
467 * \param self a consumer
468 * \param frame a frame
469 * \return true (ignore self for now)
472 int mlt_consumer_put_frame( mlt_consumer self, mlt_frame frame )
476 // Get the service assoicated to the consumer
477 mlt_service service = MLT_CONSUMER_SERVICE( self );
479 if ( mlt_service_producer( service ) == NULL )
483 pthread_mutex_lock( &self->put_mutex );
484 while ( self->put_active && self->put != NULL )
486 gettimeofday( &now, NULL );
487 tm.tv_sec = now.tv_sec + 1;
488 tm.tv_nsec = now.tv_usec * 1000;
489 pthread_cond_timedwait( &self->put_cond, &self->put_mutex, &tm );
491 if ( self->put_active && self->put == NULL )
494 mlt_frame_close( frame );
495 pthread_cond_broadcast( &self->put_cond );
496 pthread_mutex_unlock( &self->put_mutex );
500 mlt_frame_close( frame );
506 /** Protected method for consumer to get frames from connected service
508 * \public \memberof mlt_consumer_s
509 * \param self a consumer
513 mlt_frame mlt_consumer_get_frame( mlt_consumer self )
516 mlt_frame frame = NULL;
518 // Get the service assoicated to the consumer
519 mlt_service service = MLT_CONSUMER_SERVICE( self );
521 // Get the consumer properties
522 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
525 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
529 pthread_mutex_lock( &self->put_mutex );
530 while ( self->put_active && self->put == NULL )
532 gettimeofday( &now, NULL );
533 tm.tv_sec = now.tv_sec + 1;
534 tm.tv_nsec = now.tv_usec * 1000;
535 pthread_cond_timedwait( &self->put_cond, &self->put_mutex, &tm );
539 pthread_cond_broadcast( &self->put_cond );
540 pthread_mutex_unlock( &self->put_mutex );
542 mlt_service_apply_filters( service, frame, 0 );
544 else if ( mlt_service_producer( service ) != NULL )
546 mlt_service_get_frame( service, &frame, 0 );
550 frame = mlt_frame_init( service );
555 // Get the frame properties
556 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
558 // Get the test card producer
559 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
561 // Attach the test frame producer to it.
562 if ( test_card != NULL )
563 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
565 // Attach the rescale property
566 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
568 // Aspect ratio and other jiggery pokery
569 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
570 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
571 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
578 /** Compute the time difference between now and a time value.
580 * \private \memberof mlt_consumer_s
581 * \param time1 a time value to be compared against now
582 * \return the difference in microseconds
585 static inline long time_difference( struct timeval *time1 )
587 struct timeval time2;
588 time2.tv_sec = time1->tv_sec;
589 time2.tv_usec = time1->tv_usec;
590 gettimeofday( time1, NULL );
591 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
594 /** The thread procedure for asynchronously pulling frames through the service
595 * network connected to a consumer.
597 * \private \memberof mlt_consumer_s
598 * \param arg a consumer
601 static void *consumer_read_ahead_thread( void *arg )
603 // The argument is the consumer
604 mlt_consumer self = arg;
606 // Get the properties of the consumer
607 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
609 // Get the width and height
610 int width = mlt_properties_get_int( properties, "width" );
611 int height = mlt_properties_get_int( properties, "height" );
613 // See if video is turned off
614 int video_off = mlt_properties_get_int( properties, "video_off" );
615 int preview_off = mlt_properties_get_int( properties, "preview_off" );
616 int preview_format = mlt_properties_get_int( properties, "preview_format" );
618 // Get the audio settings
619 mlt_audio_format afmt = mlt_audio_s16;
621 double fps = mlt_properties_get_double( properties, "fps" );
622 int channels = mlt_properties_get_int( properties, "channels" );
623 int frequency = mlt_properties_get_int( properties, "frequency" );
627 // See if audio is turned off
628 int audio_off = mlt_properties_get_int( properties, "audio_off" );
630 // Get the maximum size of the buffer
631 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
633 // General frame variable
634 mlt_frame frame = NULL;
635 uint8_t *image = NULL;
640 // Average time for get_frame and get_image
643 int64_t time_wait = 0;
644 int64_t time_frame = 0;
645 int64_t time_process = 0;
647 mlt_position pos = 0;
648 mlt_position start_pos = 0;
649 mlt_position last_pos = 0;
651 if ( preview_off && preview_format != 0 )
652 self->format = preview_format;
654 // Get the first frame
655 frame = mlt_consumer_get_frame( self );
659 // Get the image of the first frame
662 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
663 mlt_frame_get_image( frame, &image, &self->format, &width, &height, 0 );
668 samples = mlt_sample_calculator( fps, frequency, counter++ );
669 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
673 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
674 last_pos = start_pos = pos = mlt_frame_get_position( frame );
677 // Get the starting time (can ignore the times above)
678 gettimeofday( &ante, NULL );
680 // Continue to read ahead
681 while ( self->ahead )
683 // Fetch width/height again
684 width = mlt_properties_get_int( properties, "width" );
685 height = mlt_properties_get_int( properties, "height" );
687 // Put the current frame into the queue
688 pthread_mutex_lock( &self->queue_mutex );
689 while( self->ahead && mlt_deque_count( self->queue ) >= buffer )
690 pthread_cond_wait( &self->queue_cond, &self->queue_mutex );
691 mlt_deque_push_back( self->queue, frame );
692 pthread_cond_broadcast( &self->queue_cond );
693 pthread_mutex_unlock( &self->queue_mutex );
695 time_wait += time_difference( &ante );
697 // Get the next frame
698 frame = mlt_consumer_get_frame( self );
699 time_frame += time_difference( &ante );
701 // If there's no frame, we're probably stopped...
704 pos = mlt_frame_get_position( frame );
706 // Increment the count
709 // All non normal playback frames should be shown
710 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
712 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
713 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
725 if ( !skip_next || self->real_time == -1 )
727 // Get the image, mark as rendered and time it
730 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
731 mlt_frame_get_image( frame, &image, &self->format, &width, &height, 0 );
733 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
738 // Increment the number of sequentially skipped frames
742 // If we've reached an unacceptable level, reset everything
743 if ( skipped > fps * 2 )
753 // Always process audio
756 samples = mlt_sample_calculator( fps, frequency, counter++ );
757 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
760 // Increment the time take for self frame
761 time_process += time_difference( &ante );
763 // Determine if the next frame should be skipped
764 if ( pos != last_pos + 1 )
767 if ( pos - start_pos <= buffer/5 )
769 // Do not skip the first 20% of buffer at start, resuming, or seeking
776 if ( mlt_deque_count( self->queue ) <= buffer/5 )
778 int frame_duration = mlt_properties_get_int( properties, "frame_duration" );
779 if ( ( ( time_wait + time_frame + time_process ) / count ) > frame_duration )
784 // Remove the last frame
785 mlt_frame_close( frame );
790 /** Locate the first unprocessed frame in the queue.
792 * When playing with realtime behavior, we do not use the true head, but
793 * rather an adjusted process_head. The process_head is adjusted based on
794 * the rate of frame-dropping or recovery from frame-dropping. The idea is
795 * that as the level of frame-dropping increases to move the process_head
796 * closer to the tail because the frames are not completing processing prior
797 * to their playout! Then, as frames are not dropped the process_head moves
798 * back closer to the head of the queue so that worker threads can work
799 * ahead of the playout point (queue head).
801 * \private \memberof mlt_consumer_s
802 * \param self a consumer
803 * \return an index into the queue
806 static inline int first_unprocessed_frame( mlt_consumer self )
808 int index = self->real_time <= 0 ? 0 : self->process_head;
809 while ( index < mlt_deque_count( self->queue ) && MLT_FRAME( mlt_deque_peek( self->queue, index ) )->is_processing )
814 /** The worker thread procedure for parallel processing frames.
816 * \private \memberof mlt_consumer_s
817 * \param arg a consumer
820 static void *consumer_worker_thread( void *arg )
822 // The argument is the consumer
823 mlt_consumer self = arg;
825 // Get the properties of the consumer
826 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
828 // Get the width and height
829 int width = mlt_properties_get_int( properties, "width" );
830 int height = mlt_properties_get_int( properties, "height" );
831 mlt_image_format format = self->format;
833 // See if video is turned off
834 int video_off = mlt_properties_get_int( properties, "video_off" );
835 int preview_off = mlt_properties_get_int( properties, "preview_off" );
836 int preview_format = mlt_properties_get_int( properties, "preview_format" );
838 // General frame variable
839 mlt_frame frame = NULL;
840 uint8_t *image = NULL;
842 if ( preview_off && preview_format != 0 )
843 format = preview_format;
845 // Continue to read ahead
846 while ( self->ahead )
848 // Get the next unprocessed frame from the work queue
849 pthread_mutex_lock( &self->queue_mutex );
850 int index = first_unprocessed_frame( self );
851 while ( self->ahead && index >= mlt_deque_count( self->queue ) )
853 mlt_log_debug( MLT_CONSUMER_SERVICE(self), "waiting in worker index = %d queue count = %d\n",
854 index, mlt_deque_count( self->queue ) );
855 pthread_cond_wait( &self->queue_cond, &self->queue_mutex );
856 index = first_unprocessed_frame( self );
859 // Mark the frame for processing
860 frame = mlt_deque_peek( self->queue, index );
863 mlt_log_debug( MLT_CONSUMER_SERVICE(self), "worker processing index = %d frame %d queue count = %d\n",
864 index, mlt_frame_get_position(frame), mlt_deque_count( self->queue ) );
865 frame->is_processing = 1;
866 mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( frame ) );
868 pthread_mutex_unlock( &self->queue_mutex );
870 // If there's no frame, we're probably stopped...
874 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
875 // All non normal playback frames should be shown
876 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
877 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
883 // Fetch width/height again
884 width = mlt_properties_get_int( properties, "width" );
885 height = mlt_properties_get_int( properties, "height" );
886 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
887 mlt_frame_get_image( frame, &image, &format, &width, &height, 0 );
889 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
890 mlt_frame_close( frame );
892 // Tell a waiting thread (non-realtime main consumer thread) that we are done.
893 pthread_mutex_lock( &self->done_mutex );
894 pthread_cond_broadcast( &self->done_cond );
895 pthread_mutex_unlock( &self->done_mutex );
901 /** Start the read/render thread.
903 * \private \memberof mlt_consumer_s
904 * \param self a consumer
907 static void consumer_read_ahead_start( mlt_consumer self )
912 // Create the frame queue
913 self->queue = mlt_deque_init( );
915 // Create the queue mutex
916 pthread_mutex_init( &self->queue_mutex, NULL );
918 // Create the condition
919 pthread_cond_init( &self->queue_cond, NULL );
921 // Create the read ahead
922 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( self ), "priority" ) )
924 struct sched_param priority;
925 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( self ), "priority" );
926 pthread_attr_t thread_attributes;
927 pthread_attr_init( &thread_attributes );
928 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
929 pthread_attr_setschedparam( &thread_attributes, &priority );
930 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
931 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
932 if ( pthread_create( &self->ahead_thread, &thread_attributes, consumer_read_ahead_thread, self ) < 0 )
933 pthread_create( &self->ahead_thread, NULL, consumer_read_ahead_thread, self );
934 pthread_attr_destroy( &thread_attributes );
938 pthread_create( &self->ahead_thread, NULL, consumer_read_ahead_thread, self );
943 /** Start the worker threads.
945 * \private \memberof mlt_consumer_s
946 * \param self a consumer
949 static void consumer_work_start( mlt_consumer self )
951 int n = abs( self->real_time );
952 pthread_t *thread = calloc( 1, sizeof( pthread_t ) * n );
957 // These keep track of the accelleration of frame dropping or recovery.
958 self->consecutive_dropped = 0;
959 self->consecutive_rendered = 0;
961 // This is the position in the queue from which to look for a frame to process.
962 // If we always start from the head, then we may likely not complete processing
963 // before the frame is played out.
964 self->process_head = 0;
967 self->queue = mlt_deque_init();
968 self->worker_threads = mlt_deque_init();
970 // Create the mutexes
971 pthread_mutex_init( &self->queue_mutex, NULL );
972 pthread_mutex_init( &self->done_mutex, NULL );
974 // Create the conditions
975 pthread_cond_init( &self->queue_cond, NULL );
976 pthread_cond_init( &self->done_cond, NULL );
978 // Create the read ahead
979 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( self ), "priority" ) )
982 struct sched_param priority;
983 pthread_attr_t thread_attributes;
985 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( self ), "priority" );
986 pthread_attr_init( &thread_attributes );
987 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
988 pthread_attr_setschedparam( &thread_attributes, &priority );
989 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
990 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
994 if ( pthread_create( thread, &thread_attributes, consumer_worker_thread, self ) < 0 )
995 if ( pthread_create( thread, NULL, consumer_worker_thread, self ) == 0 )
996 mlt_deque_push_back( self->worker_threads, thread );
999 pthread_attr_destroy( &thread_attributes );
1006 if ( pthread_create( thread, NULL, consumer_worker_thread, self ) == 0 )
1007 mlt_deque_push_back( self->worker_threads, thread );
1014 /** Stop the read/render thread.
1016 * \private \memberof mlt_consumer_s
1017 * \param self a consumer
1020 static void consumer_read_ahead_stop( mlt_consumer self )
1022 // Make sure we're running
1023 if ( self->started )
1025 // Inform thread to stop
1028 // Broadcast to the condition in case it's waiting
1029 pthread_mutex_lock( &self->queue_mutex );
1030 pthread_cond_broadcast( &self->queue_cond );
1031 pthread_mutex_unlock( &self->queue_mutex );
1033 // Broadcast to the put condition in case it's waiting
1034 pthread_mutex_lock( &self->put_mutex );
1035 pthread_cond_broadcast( &self->put_cond );
1036 pthread_mutex_unlock( &self->put_mutex );
1039 pthread_join( self->ahead_thread, NULL );
1042 // Destroy the frame queue mutex
1043 pthread_mutex_destroy( &self->queue_mutex );
1045 // Destroy the condition
1046 pthread_cond_destroy( &self->queue_cond );
1049 while ( mlt_deque_count( self->queue ) )
1050 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
1053 mlt_deque_close( self->queue );
1057 /** Stop the worker threads.
1059 * \private \memberof mlt_consumer_s
1060 * \param self a consumer
1063 static void consumer_work_stop( mlt_consumer self )
1065 // Make sure we're running
1066 if ( self->started )
1068 // Inform thread to stop
1071 // Broadcast to the queue condition in case it's waiting
1072 pthread_mutex_lock( &self->queue_mutex );
1073 pthread_cond_broadcast( &self->queue_cond );
1074 pthread_mutex_unlock( &self->queue_mutex );
1076 // Broadcast to the put condition in case it's waiting
1077 pthread_mutex_lock( &self->put_mutex );
1078 pthread_cond_broadcast( &self->put_cond );
1079 pthread_mutex_unlock( &self->put_mutex );
1081 // Broadcast to the done condition in case it's waiting
1082 pthread_mutex_lock( &self->done_mutex );
1083 pthread_cond_broadcast( &self->done_cond );
1084 pthread_mutex_unlock( &self->done_mutex );
1088 while ( ( thread = mlt_deque_pop_back( self->worker_threads ) ) )
1089 pthread_join( *thread, NULL );
1091 // Deallocate the array of threads
1095 // Indicate that worker threads no longer running
1098 // Destroy the mutexes
1099 pthread_mutex_destroy( &self->queue_mutex );
1100 pthread_mutex_destroy( &self->done_mutex );
1102 // Destroy the conditions
1103 pthread_cond_destroy( &self->queue_cond );
1104 pthread_cond_destroy( &self->done_cond );
1107 while ( mlt_deque_count( self->queue ) )
1108 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
1111 mlt_deque_close( self->queue );
1112 mlt_deque_close( self->worker_threads );
1116 /** Flush the read/render thread's buffer.
1118 * \public \memberof mlt_consumer_s
1119 * \param self a consumer
1122 void mlt_consumer_purge( mlt_consumer self )
1126 pthread_mutex_lock( &self->queue_mutex );
1127 while ( mlt_deque_count( self->queue ) )
1128 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
1129 pthread_cond_broadcast( &self->queue_cond );
1130 pthread_mutex_unlock( &self->queue_mutex );
1134 /** Use multiple worker threads and a work queue.
1137 static mlt_frame worker_get_frame( mlt_consumer self, mlt_properties properties )
1140 mlt_frame frame = NULL;
1142 int size = abs( self->real_time );
1143 int buffer = mlt_properties_get_int( properties, "buffer" );
1144 // This is a heuristic to determine a suitable minimum buffer size for the number of threads.
1145 int headroom = 2 + size * size;
1146 buffer = buffer < headroom ? headroom : buffer;
1148 // Start worker threads if not already started.
1149 if ( ! self->ahead )
1151 int prefill = mlt_properties_get_int( properties, "prefill" );
1152 prefill = prefill > 0 && prefill < buffer ? prefill : buffer;
1154 consumer_work_start( self );
1156 // Fill the work queue.
1158 while ( self->ahead && i-- )
1160 frame = mlt_consumer_get_frame( self );
1163 pthread_mutex_lock( &self->queue_mutex );
1164 mlt_deque_push_back( self->queue, frame );
1165 pthread_cond_signal( &self->queue_cond );
1166 pthread_mutex_unlock( &self->queue_mutex );
1171 while ( self->ahead && first_unprocessed_frame( self ) < prefill )
1173 pthread_mutex_lock( &self->done_mutex );
1174 pthread_cond_wait( &self->done_cond, &self->done_mutex );
1175 pthread_mutex_unlock( &self->done_mutex );
1177 self->process_head = size;
1180 // mlt_log_verbose( MLT_CONSUMER_SERVICE(self), "size %d done count %d work count %d process_head %d\n",
1181 // size, first_unprocessed_frame( self ), mlt_deque_count( self->queue ), self->process_head );
1183 // Feed the work queue
1184 while ( self->ahead && mlt_deque_count( self->queue ) < buffer )
1186 frame = mlt_consumer_get_frame( self );
1189 pthread_mutex_lock( &self->queue_mutex );
1190 mlt_deque_push_back( self->queue, frame );
1191 pthread_cond_signal( &self->queue_cond );
1192 pthread_mutex_unlock( &self->queue_mutex );
1195 // Wait if not realtime.
1196 while( self->ahead && self->real_time < 0 &&
1197 ! mlt_properties_get_int( MLT_FRAME_PROPERTIES( MLT_FRAME( mlt_deque_peek_front( self->queue ) ) ), "rendered" ) )
1199 pthread_mutex_lock( &self->done_mutex );
1200 pthread_cond_wait( &self->done_cond, &self->done_mutex );
1201 pthread_mutex_unlock( &self->done_mutex );
1204 // Get the frame from the queue.
1205 pthread_mutex_lock( &self->queue_mutex );
1206 frame = mlt_deque_pop_front( self->queue );
1207 pthread_mutex_unlock( &self->queue_mutex );
1209 // Adapt the worker process head to the runtime conditions.
1210 if ( self->real_time > 0 )
1212 if ( frame && mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "rendered" ) )
1214 self->consecutive_dropped = 0;
1215 if ( self->process_head > size && self->consecutive_rendered >= self->process_head )
1216 self->process_head--;
1218 self->consecutive_rendered++;
1222 self->consecutive_rendered = 0;
1223 if ( self->process_head < buffer - size && self->consecutive_dropped > size )
1224 self->process_head++;
1226 self->consecutive_dropped++;
1228 // mlt_log_verbose( MLT_CONSUMER_SERVICE(self), "dropped %d rendered %d process_head %d\n",
1229 // self->consecutive_dropped, self->consecutive_rendered, self->process_head );
1235 /** Get the next frame from the producer connected to a consumer.
1237 * Typically, one uses this instead of \p mlt_consumer_get_frame to make
1238 * the asynchronous/real-time behavior configurable at runtime.
1239 * You should close the frame returned from this when you are done with it.
1241 * \public \memberof mlt_consumer_s
1242 * \param self a consumer
1246 mlt_frame mlt_consumer_rt_frame( mlt_consumer self )
1249 mlt_frame frame = NULL;
1251 // Get the properties
1252 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
1254 // Check if the user has requested real time or not
1255 if ( self->real_time > 1 || self->real_time < -1 )
1258 return worker_get_frame( self, properties );
1260 else if ( self->real_time == 1 || self->real_time == -1 )
1264 // Is the read ahead running?
1265 if ( self->ahead == 0 )
1267 int buffer = mlt_properties_get_int( properties, "buffer" );
1268 int prefill = mlt_properties_get_int( properties, "prefill" );
1269 consumer_read_ahead_start( self );
1271 size = prefill > 0 && prefill < buffer ? prefill : buffer;
1274 // Get frame from queue
1275 pthread_mutex_lock( &self->queue_mutex );
1276 while( self->ahead && mlt_deque_count( self->queue ) < size )
1277 pthread_cond_wait( &self->queue_cond, &self->queue_mutex );
1278 frame = mlt_deque_pop_front( self->queue );
1279 pthread_cond_broadcast( &self->queue_cond );
1280 pthread_mutex_unlock( &self->queue_mutex );
1282 else // real_time == 0
1284 // Get the frame in non real time
1285 frame = mlt_consumer_get_frame( self );
1287 // This isn't true, but from the consumers perspective it is
1288 if ( frame != NULL )
1289 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
1295 /** Callback for the implementation to indicate a stopped condition.
1297 * \public \memberof mlt_consumer_s
1298 * \param self a consumer
1301 void mlt_consumer_stopped( mlt_consumer self )
1303 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( self ), "running", 0 );
1304 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-stopped", NULL );
1305 mlt_event_unblock( self->event_listener );
1308 /** Stop the consumer.
1310 * \public \memberof mlt_consumer_s
1311 * \param self a consumer
1312 * \return true if there was an error
1315 int mlt_consumer_stop( mlt_consumer self )
1317 // Get the properies
1318 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
1321 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping put waiting\n" );
1322 pthread_mutex_lock( &self->put_mutex );
1323 self->put_active = 0;
1324 pthread_cond_broadcast( &self->put_cond );
1325 pthread_mutex_unlock( &self->put_mutex );
1327 // Stop the consumer
1328 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping consumer\n" );
1330 // Cancel the read ahead threads
1332 if ( self->started )
1334 // Unblock the consumer calling mlt_consumer_rt_frame
1335 pthread_mutex_lock( &self->queue_mutex );
1336 pthread_cond_broadcast( &self->queue_cond );
1337 pthread_mutex_unlock( &self->queue_mutex );
1340 // Invoke the child callback
1341 if ( self->stop != NULL )
1344 // Check if the user has requested real time or not and stop if necessary
1345 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping read_ahead\n" );
1346 if ( abs( self->real_time ) == 1 )
1347 consumer_read_ahead_stop( self );
1348 else if ( abs( self->real_time ) > 1 )
1349 consumer_work_stop( self );
1351 // Kill the test card
1352 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
1354 // Check and run a post command
1355 if ( mlt_properties_get( properties, "post" ) )
1356 if (system( mlt_properties_get( properties, "post" ) ) == -1 )
1357 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "post" ) );
1359 mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopped\n" );
1364 /** Determine if the consumer is stopped.
1366 * \public \memberof mlt_consumer_s
1367 * \param self a consumer
1368 * \return true if the consumer is stopped
1371 int mlt_consumer_is_stopped( mlt_consumer self )
1373 // Check if the consumer is stopped
1374 if ( self->is_stopped != NULL )
1375 return self->is_stopped( self );
1380 /** Close and destroy the consumer.
1382 * \public \memberof mlt_consumer_s
1383 * \param self a consumer
1386 void mlt_consumer_close( mlt_consumer self )
1388 if ( self != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( self ) ) <= 0 )
1390 // Get the childs close function
1391 void ( *consumer_close )( ) = self->close;
1393 if ( consumer_close )
1396 //mlt_consumer_stop( self );
1399 consumer_close( self );
1403 // Make sure it only gets called once
1404 self->parent.close = NULL;
1406 // Destroy the push mutex and condition
1407 pthread_mutex_destroy( &self->put_mutex );
1408 pthread_cond_destroy( &self->put_cond );
1410 mlt_service_close( &self->parent );
1415 /** Get the position of the last frame shown.
1417 * \public \memberof mlt_consumer_s
1418 * \param consumer a consumer
1419 * \return the position
1422 mlt_position mlt_consumer_position( mlt_consumer consumer )
1424 return consumer->position;