2 * mlt_consumer.c -- abstraction for all consumer services
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "mlt_consumer.h"
23 #include "mlt_factory.h"
24 #include "mlt_producer.h"
25 #include "mlt_frame.h"
26 #include "mlt_profile.h"
33 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
34 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
35 static void mlt_consumer_property_changed( mlt_service owner, mlt_consumer this, char *name );
36 static void apply_profile_properties( mlt_profile profile, mlt_properties properties );
38 static mlt_event g_event_listener = NULL;
40 /** Public final methods
43 int mlt_consumer_init( mlt_consumer this, void *child )
46 memset( this, 0, sizeof( struct mlt_consumer_s ) );
48 error = mlt_service_init( &this->parent, this );
51 // Get the properties from the service
52 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
54 // Apply the profile to properties for legacy integration
55 apply_profile_properties( mlt_profile_get(), properties );
57 // Default rescaler for all consumers
58 mlt_properties_set( properties, "rescale", "bilinear" );
60 // Default read ahead buffer size
61 mlt_properties_set_int( properties, "buffer", 25 );
63 // Default audio frequency and channels
64 mlt_properties_set_int( properties, "frequency", 48000 );
65 mlt_properties_set_int( properties, "channels", 2 );
67 // Default of all consumers is real time
68 mlt_properties_set_int( properties, "real_time", 1 );
70 // Default to environment test card
71 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
73 // Hmm - default all consumers to yuv422 :-/
74 this->format = mlt_image_yuv422;
76 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
77 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
78 mlt_events_register( properties, "consumer-stopped", NULL );
80 // Register a property-changed listener to handle the profile property -
81 // subsequent properties can override the profile
82 g_event_listener = mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_consumer_property_changed );
84 // Create the push mutex and condition
85 pthread_mutex_init( &this->put_mutex, NULL );
86 pthread_cond_init( &this->put_cond, NULL );
92 static void apply_profile_properties( mlt_profile profile, mlt_properties properties )
94 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
95 mlt_properties_set_int( properties, "frame_rate_num", profile->frame_rate_num );
96 mlt_properties_set_int( properties, "frame_rate_den", profile->frame_rate_den );
97 mlt_properties_set_int( properties, "width", profile->width );
98 mlt_properties_set_int( properties, "height", profile->height );
99 mlt_properties_set_int( properties, "progressive", profile->progressive );
100 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
101 mlt_properties_set_int( properties, "sample_aspect_num", profile->sample_aspect_num );
102 mlt_properties_set_int( properties, "sample_aspect_den", profile->sample_aspect_den );
103 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
104 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
105 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
108 static void mlt_consumer_property_changed( mlt_service owner, mlt_consumer this, char *name )
110 if ( !strcmp( name, "profile" ) )
113 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
115 // Locate the profile
116 mlt_profile_select( mlt_properties_get( properties, "profile" ) );
118 // Stop listening to this
119 mlt_event_close( g_event_listener );
120 g_event_listener = NULL;
122 // Apply to properties
123 apply_profile_properties( mlt_profile_get(), properties );
127 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
129 if ( listener != NULL )
130 listener( owner, this, ( mlt_frame )args[ 0 ] );
133 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
135 if ( listener != NULL )
136 listener( owner, this, ( mlt_frame )args[ 0 ] );
139 /** Create a new consumer.
142 mlt_consumer mlt_consumer_new( )
144 // Create the memory for the structure
145 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
149 mlt_consumer_init( this, NULL );
155 /** Get the parent service object.
158 mlt_service mlt_consumer_service( mlt_consumer this )
160 return this != NULL ? &this->parent : NULL;
163 /** Get the consumer properties.
166 mlt_properties mlt_consumer_properties( mlt_consumer this )
168 return this != NULL ? MLT_SERVICE_PROPERTIES( &this->parent ) : NULL;
171 /** Connect the consumer to the producer.
174 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
176 return mlt_service_connect_producer( &this->parent, producer, 0 );
179 /** Start the consumer.
182 int mlt_consumer_start( mlt_consumer this )
185 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
187 // Determine if there's a test card producer
188 char *test_card = mlt_properties_get( properties, "test_card" );
190 // Just to make sure nothing is hanging around...
191 mlt_frame_close( this->put );
193 this->put_active = 1;
196 if ( test_card != NULL )
198 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
200 // Create a test card producer
201 mlt_producer producer = mlt_factory_producer( NULL, test_card );
203 // Do we have a producer
204 if ( producer != NULL )
206 // Test card should loop I guess...
207 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
208 //mlt_producer_set_speed( producer, 0 );
209 //mlt_producer_set_in_and_out( producer, 0, 0 );
211 // Set the test card on the consumer
212 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
218 // Allow the hash table to speed things up
219 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
222 // Check and run an ante command
223 if ( mlt_properties_get( properties, "ante" ) )
224 system( mlt_properties_get( properties, "ante" ) );
226 // Set the real_time preference
227 this->real_time = mlt_properties_get_int( properties, "real_time" );
230 if ( this->start != NULL )
231 return this->start( this );
236 /** An alternative method to feed frames into the consumer - only valid if
237 the consumer itself is not connected.
240 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
244 // Get the service assoicated to the consumer
245 mlt_service service = MLT_CONSUMER_SERVICE( this );
247 if ( mlt_service_producer( service ) == NULL )
251 pthread_mutex_lock( &this->put_mutex );
252 while ( this->put_active && this->put != NULL )
254 gettimeofday( &now, NULL );
255 tm.tv_sec = now.tv_sec + 1;
256 tm.tv_nsec = now.tv_usec * 1000;
257 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
259 if ( this->put_active && this->put == NULL )
262 mlt_frame_close( frame );
263 pthread_cond_broadcast( &this->put_cond );
264 pthread_mutex_unlock( &this->put_mutex );
268 mlt_frame_close( frame );
274 /** Protected method for consumer to get frames from connected service
277 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
280 mlt_frame frame = NULL;
282 // Get the service assoicated to the consumer
283 mlt_service service = MLT_CONSUMER_SERVICE( this );
285 // Get the consumer properties
286 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
289 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
293 pthread_mutex_lock( &this->put_mutex );
294 while ( this->put_active && this->put == NULL )
296 gettimeofday( &now, NULL );
297 tm.tv_sec = now.tv_sec + 1;
298 tm.tv_nsec = now.tv_usec * 1000;
299 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
303 pthread_cond_broadcast( &this->put_cond );
304 pthread_mutex_unlock( &this->put_mutex );
306 mlt_service_apply_filters( service, frame, 0 );
308 else if ( mlt_service_producer( service ) != NULL )
310 mlt_service_get_frame( service, &frame, 0 );
314 frame = mlt_frame_init( );
319 // Get the frame properties
320 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
322 // Get the test card producer
323 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
325 // Attach the test frame producer to it.
326 if ( test_card != NULL )
327 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
329 // Attach the rescale property
330 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
332 // Aspect ratio and other jiggery pokery
333 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
334 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
335 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
342 static inline long time_difference( struct timeval *time1 )
344 struct timeval time2;
345 time2.tv_sec = time1->tv_sec;
346 time2.tv_usec = time1->tv_usec;
347 gettimeofday( time1, NULL );
348 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
351 int mlt_consumer_profile( mlt_properties properties, char *profile )
353 mlt_profile p = mlt_profile_select( profile );
356 apply_profile_properties( p, properties );
365 static void *consumer_read_ahead_thread( void *arg )
367 // The argument is the consumer
368 mlt_consumer this = arg;
370 // Get the properties of the consumer
371 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
373 // Get the width and height
374 int width = mlt_properties_get_int( properties, "width" );
375 int height = mlt_properties_get_int( properties, "height" );
377 // See if video is turned off
378 int video_off = mlt_properties_get_int( properties, "video_off" );
379 int preview_off = mlt_properties_get_int( properties, "preview_off" );
380 int preview_format = mlt_properties_get_int( properties, "preview_format" );
382 // Get the audio settings
383 mlt_audio_format afmt = mlt_audio_pcm;
385 double fps = mlt_properties_get_double( properties, "fps" );
386 int channels = mlt_properties_get_int( properties, "channels" );
387 int frequency = mlt_properties_get_int( properties, "frequency" );
391 // See if audio is turned off
392 int audio_off = mlt_properties_get_int( properties, "audio_off" );
394 // Get the maximum size of the buffer
395 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
397 // General frame variable
398 mlt_frame frame = NULL;
399 uint8_t *image = NULL;
404 // Average time for get_frame and get_image
407 int64_t time_wait = 0;
408 int64_t time_frame = 0;
409 int64_t time_process = 0;
411 mlt_service lock_object = NULL;
413 if ( preview_off && preview_format != 0 )
414 this->format = preview_format;
416 // Get the first frame
417 frame = mlt_consumer_get_frame( this );
419 // Get the lock object
420 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
423 if ( lock_object ) mlt_service_lock( lock_object );
425 // Get the image of the first frame
428 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
429 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
434 samples = mlt_sample_calculator( fps, frequency, counter++ );
435 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
438 // Unlock the lock object
439 if ( lock_object ) mlt_service_unlock( lock_object );
442 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
444 // Get the starting time (can ignore the times above)
445 gettimeofday( &ante, NULL );
447 // Continue to read ahead
448 while ( this->ahead )
450 // Fetch width/height again
451 width = mlt_properties_get_int( properties, "width" );
452 height = mlt_properties_get_int( properties, "height" );
454 // Put the current frame into the queue
455 pthread_mutex_lock( &this->mutex );
456 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
457 pthread_cond_wait( &this->cond, &this->mutex );
458 mlt_deque_push_back( this->queue, frame );
459 pthread_cond_broadcast( &this->cond );
460 pthread_mutex_unlock( &this->mutex );
462 time_wait += time_difference( &ante );
464 // Get the next frame
465 frame = mlt_consumer_get_frame( this );
466 time_frame += time_difference( &ante );
468 // If there's no frame, we're probably stopped...
472 // Attempt to fetch the lock object
473 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
475 // Increment the count
478 // Lock if there's a lock object
479 if ( lock_object ) mlt_service_lock( lock_object );
481 // All non normal playback frames should be shown
482 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
484 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
496 // Get the image, mark as rendered and time it
499 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
500 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
502 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
506 // Increment the number of sequentially skipped frames
510 // If we've reached an unacceptable level, reset everything
521 // Always process audio
524 samples = mlt_sample_calculator( fps, frequency, counter++ );
525 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
528 // Increment the time take for this frame
529 time_process += time_difference( &ante );
531 // Determine if the next frame should be skipped
532 if ( mlt_deque_count( this->queue ) <= 5 && ( ( time_wait + time_frame + time_process ) / count ) > 40000 )
535 // Unlock if there's a lock object
536 if ( lock_object ) mlt_service_unlock( lock_object );
539 // Remove the last frame
540 mlt_frame_close( frame );
545 static void consumer_read_ahead_start( mlt_consumer this )
550 // Create the frame queue
551 this->queue = mlt_deque_init( );
554 pthread_mutex_init( &this->mutex, NULL );
556 // Create the condition
557 pthread_cond_init( &this->cond, NULL );
559 // Create the read ahead
560 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
563 static void consumer_read_ahead_stop( mlt_consumer this )
565 // Make sure we're running
568 // Inform thread to stop
571 // Broadcast to the condition in case it's waiting
572 pthread_mutex_lock( &this->mutex );
573 pthread_cond_broadcast( &this->cond );
574 pthread_mutex_unlock( &this->mutex );
576 // Broadcast to the put condition in case it's waiting
577 pthread_mutex_lock( &this->put_mutex );
578 pthread_cond_broadcast( &this->put_cond );
579 pthread_mutex_unlock( &this->put_mutex );
582 pthread_join( this->ahead_thread, NULL );
585 pthread_mutex_destroy( &this->mutex );
587 // Destroy the condition
588 pthread_cond_destroy( &this->cond );
591 while ( mlt_deque_count( this->queue ) )
592 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
595 mlt_deque_close( this->queue );
599 void mlt_consumer_purge( mlt_consumer this )
603 pthread_mutex_lock( &this->mutex );
604 while ( mlt_deque_count( this->queue ) )
605 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
606 pthread_cond_broadcast( &this->cond );
607 pthread_mutex_unlock( &this->mutex );
611 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
614 mlt_frame frame = NULL;
616 // Get the properties
617 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
619 // Check if the user has requested real time or not
620 if ( this->real_time )
624 // Is the read ahead running?
625 if ( this->ahead == 0 )
627 int buffer = mlt_properties_get_int( properties, "buffer" );
628 int prefill = mlt_properties_get_int( properties, "prefill" );
629 consumer_read_ahead_start( this );
631 size = prefill > 0 && prefill < buffer ? prefill : buffer;
634 // Get frame from queue
635 pthread_mutex_lock( &this->mutex );
636 while( this->ahead && mlt_deque_count( this->queue ) < size )
637 pthread_cond_wait( &this->cond, &this->mutex );
638 frame = mlt_deque_pop_front( this->queue );
639 pthread_cond_broadcast( &this->cond );
640 pthread_mutex_unlock( &this->mutex );
644 // Get the frame in non real time
645 frame = mlt_consumer_get_frame( this );
647 // This isn't true, but from the consumers perspective it is
649 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
655 /** Callback for the implementation to indicate a stopped condition.
658 void mlt_consumer_stopped( mlt_consumer this )
660 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
661 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
664 /** Stop the consumer.
667 int mlt_consumer_stop( mlt_consumer this )
670 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
671 char *debug = mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "debug" );
674 if ( debug ) fprintf( stderr, "%s: stopping put waiting\n", debug );
675 pthread_mutex_lock( &this->put_mutex );
676 this->put_active = 0;
677 pthread_cond_broadcast( &this->put_cond );
678 pthread_mutex_unlock( &this->put_mutex );
681 if ( debug ) fprintf( stderr, "%s: stopping consumer\n", debug );
682 if ( this->stop != NULL )
685 // Check if the user has requested real time or not and stop if necessary
686 if ( debug ) fprintf( stderr, "%s: stopping read_ahead\n", debug );
687 if ( mlt_properties_get_int( properties, "real_time" ) )
688 consumer_read_ahead_stop( this );
690 // Kill the test card
691 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
693 // Check and run a post command
694 if ( mlt_properties_get( properties, "post" ) )
695 system( mlt_properties_get( properties, "post" ) );
697 if ( debug ) fprintf( stderr, "%s: stopped\n", debug );
702 /** Determine if the consumer is stopped.
705 int mlt_consumer_is_stopped( mlt_consumer this )
707 // Check if the consumer is stopped
708 if ( this->is_stopped != NULL )
709 return this->is_stopped( this );
714 /** Close the consumer.
717 void mlt_consumer_close( mlt_consumer this )
719 if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
721 // Get the childs close function
722 void ( *consumer_close )( ) = this->close;
724 if ( consumer_close )
727 //mlt_consumer_stop( this );
730 consumer_close( this );
734 // Make sure it only gets called once
735 this->parent.close = NULL;
737 // Destroy the push mutex and condition
738 pthread_mutex_destroy( &this->put_mutex );
739 pthread_cond_destroy( &this->put_cond );
741 mlt_service_close( &this->parent );