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_event_block( g_event_listener );
95 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
96 mlt_properties_set_int( properties, "frame_rate_num", profile->frame_rate_num );
97 mlt_properties_set_int( properties, "frame_rate_den", profile->frame_rate_den );
98 mlt_properties_set_int( properties, "width", profile->width );
99 mlt_properties_set_int( properties, "height", profile->height );
100 mlt_properties_set_int( properties, "progressive", profile->progressive );
101 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
102 mlt_properties_set_int( properties, "sample_aspect_num", profile->sample_aspect_num );
103 mlt_properties_set_int( properties, "sample_aspect_den", profile->sample_aspect_den );
104 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
105 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
106 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
107 mlt_event_unblock( g_event_listener );
110 static void mlt_consumer_property_changed( mlt_service owner, mlt_consumer this, char *name )
112 if ( !strcmp( name, "profile" ) )
115 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
117 // Locate the profile
118 mlt_profile_select( mlt_properties_get( properties, "profile" ) );
120 // Apply to properties
121 apply_profile_properties( mlt_profile_get(), properties );
123 else if ( !strcmp( name, "frame_rate_num" ) )
125 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
126 mlt_profile_get()->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
127 mlt_properties_set_double( properties, "fps", mlt_profile_fps( NULL ) );
129 else if ( !strcmp( name, "frame_rate_den" ) )
131 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
132 mlt_profile_get()->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
133 mlt_properties_set_double( properties, "fps", mlt_profile_fps( NULL ) );
135 else if ( !strcmp( name, "width" ) )
137 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
138 mlt_profile_get()->width = mlt_properties_get_int( properties, "width" );
140 else if ( !strcmp( name, "height" ) )
142 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
143 mlt_profile_get()->height = mlt_properties_get_int( properties, "height" );
145 else if ( !strcmp( name, "progressive" ) )
147 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
148 mlt_profile_get()->progressive = mlt_properties_get_int( properties, "progressive" );
150 else if ( !strcmp( name, "sample_aspect_num" ) )
152 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
153 mlt_profile_get()->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
154 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( NULL ) );
156 else if ( !strcmp( name, "sample_aspect_den" ) )
158 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
159 mlt_profile_get()->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
160 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( NULL ) );
162 else if ( !strcmp( name, "display_aspect_num" ) )
164 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
165 mlt_profile_get()->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
166 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( NULL ) );
168 else if ( !strcmp( name, "display_aspect_den" ) )
170 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
171 mlt_profile_get()->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
172 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( NULL ) );
176 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
178 if ( listener != NULL )
179 listener( owner, this, ( mlt_frame )args[ 0 ] );
182 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
184 if ( listener != NULL )
185 listener( owner, this, ( mlt_frame )args[ 0 ] );
188 /** Create a new consumer.
191 mlt_consumer mlt_consumer_new( )
193 // Create the memory for the structure
194 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
198 mlt_consumer_init( this, NULL );
204 /** Get the parent service object.
207 mlt_service mlt_consumer_service( mlt_consumer this )
209 return this != NULL ? &this->parent : NULL;
212 /** Get the consumer properties.
215 mlt_properties mlt_consumer_properties( mlt_consumer this )
217 return this != NULL ? MLT_SERVICE_PROPERTIES( &this->parent ) : NULL;
220 /** Connect the consumer to the producer.
223 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
225 return mlt_service_connect_producer( &this->parent, producer, 0 );
228 /** Start the consumer.
231 int mlt_consumer_start( mlt_consumer this )
233 // Stop listening to the property-changed event
234 mlt_event_block( g_event_listener );
237 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
239 // Determine if there's a test card producer
240 char *test_card = mlt_properties_get( properties, "test_card" );
242 // Just to make sure nothing is hanging around...
243 mlt_frame_close( this->put );
245 this->put_active = 1;
248 if ( test_card != NULL )
250 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
252 // Create a test card producer
253 mlt_producer producer = mlt_factory_producer( NULL, test_card );
255 // Do we have a producer
256 if ( producer != NULL )
258 // Test card should loop I guess...
259 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
260 //mlt_producer_set_speed( producer, 0 );
261 //mlt_producer_set_in_and_out( producer, 0, 0 );
263 // Set the test card on the consumer
264 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
270 // Allow the hash table to speed things up
271 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
274 // Check and run an ante command
275 if ( mlt_properties_get( properties, "ante" ) )
276 system( mlt_properties_get( properties, "ante" ) );
278 // Set the real_time preference
279 this->real_time = mlt_properties_get_int( properties, "real_time" );
282 if ( this->start != NULL )
283 return this->start( this );
288 /** An alternative method to feed frames into the consumer - only valid if
289 the consumer itself is not connected.
292 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
296 // Get the service assoicated to the consumer
297 mlt_service service = MLT_CONSUMER_SERVICE( this );
299 if ( mlt_service_producer( service ) == NULL )
303 pthread_mutex_lock( &this->put_mutex );
304 while ( this->put_active && this->put != NULL )
306 gettimeofday( &now, NULL );
307 tm.tv_sec = now.tv_sec + 1;
308 tm.tv_nsec = now.tv_usec * 1000;
309 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
311 if ( this->put_active && this->put == NULL )
314 mlt_frame_close( frame );
315 pthread_cond_broadcast( &this->put_cond );
316 pthread_mutex_unlock( &this->put_mutex );
320 mlt_frame_close( frame );
326 /** Protected method for consumer to get frames from connected service
329 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
332 mlt_frame frame = NULL;
334 // Get the service assoicated to the consumer
335 mlt_service service = MLT_CONSUMER_SERVICE( this );
337 // Get the consumer properties
338 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
341 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
345 pthread_mutex_lock( &this->put_mutex );
346 while ( this->put_active && this->put == NULL )
348 gettimeofday( &now, NULL );
349 tm.tv_sec = now.tv_sec + 1;
350 tm.tv_nsec = now.tv_usec * 1000;
351 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
355 pthread_cond_broadcast( &this->put_cond );
356 pthread_mutex_unlock( &this->put_mutex );
358 mlt_service_apply_filters( service, frame, 0 );
360 else if ( mlt_service_producer( service ) != NULL )
362 mlt_service_get_frame( service, &frame, 0 );
366 frame = mlt_frame_init( );
371 // Get the frame properties
372 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
374 // Get the test card producer
375 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
377 // Attach the test frame producer to it.
378 if ( test_card != NULL )
379 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
381 // Attach the rescale property
382 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
384 // Aspect ratio and other jiggery pokery
385 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
386 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
387 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
394 static inline long time_difference( struct timeval *time1 )
396 struct timeval time2;
397 time2.tv_sec = time1->tv_sec;
398 time2.tv_usec = time1->tv_usec;
399 gettimeofday( time1, NULL );
400 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
403 int mlt_consumer_profile( mlt_properties properties, char *profile )
405 mlt_profile p = mlt_profile_select( profile );
408 apply_profile_properties( p, properties );
417 static void *consumer_read_ahead_thread( void *arg )
419 // The argument is the consumer
420 mlt_consumer this = arg;
422 // Get the properties of the consumer
423 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
425 // Get the width and height
426 int width = mlt_properties_get_int( properties, "width" );
427 int height = mlt_properties_get_int( properties, "height" );
429 // See if video is turned off
430 int video_off = mlt_properties_get_int( properties, "video_off" );
431 int preview_off = mlt_properties_get_int( properties, "preview_off" );
432 int preview_format = mlt_properties_get_int( properties, "preview_format" );
434 // Get the audio settings
435 mlt_audio_format afmt = mlt_audio_pcm;
437 double fps = mlt_properties_get_double( properties, "fps" );
438 int channels = mlt_properties_get_int( properties, "channels" );
439 int frequency = mlt_properties_get_int( properties, "frequency" );
443 // See if audio is turned off
444 int audio_off = mlt_properties_get_int( properties, "audio_off" );
446 // Get the maximum size of the buffer
447 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
449 // General frame variable
450 mlt_frame frame = NULL;
451 uint8_t *image = NULL;
456 // Average time for get_frame and get_image
459 int64_t time_wait = 0;
460 int64_t time_frame = 0;
461 int64_t time_process = 0;
463 mlt_service lock_object = NULL;
465 if ( preview_off && preview_format != 0 )
466 this->format = preview_format;
468 // Get the first frame
469 frame = mlt_consumer_get_frame( this );
471 // Get the lock object
472 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
475 if ( lock_object ) mlt_service_lock( lock_object );
477 // Get the image of the first frame
480 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
481 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
486 samples = mlt_sample_calculator( fps, frequency, counter++ );
487 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
490 // Unlock the lock object
491 if ( lock_object ) mlt_service_unlock( lock_object );
494 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
496 // Get the starting time (can ignore the times above)
497 gettimeofday( &ante, NULL );
499 // Continue to read ahead
500 while ( this->ahead )
502 // Fetch width/height again
503 width = mlt_properties_get_int( properties, "width" );
504 height = mlt_properties_get_int( properties, "height" );
506 // Put the current frame into the queue
507 pthread_mutex_lock( &this->mutex );
508 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
509 pthread_cond_wait( &this->cond, &this->mutex );
510 mlt_deque_push_back( this->queue, frame );
511 pthread_cond_broadcast( &this->cond );
512 pthread_mutex_unlock( &this->mutex );
514 time_wait += time_difference( &ante );
516 // Get the next frame
517 frame = mlt_consumer_get_frame( this );
518 time_frame += time_difference( &ante );
520 // If there's no frame, we're probably stopped...
524 // Attempt to fetch the lock object
525 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
527 // Increment the count
530 // Lock if there's a lock object
531 if ( lock_object ) mlt_service_lock( lock_object );
533 // All non normal playback frames should be shown
534 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
536 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
548 // Get the image, mark as rendered and time it
551 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
552 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
554 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
558 // Increment the number of sequentially skipped frames
562 // If we've reached an unacceptable level, reset everything
573 // Always process audio
576 samples = mlt_sample_calculator( fps, frequency, counter++ );
577 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
580 // Increment the time take for this frame
581 time_process += time_difference( &ante );
583 // Determine if the next frame should be skipped
584 if ( mlt_deque_count( this->queue ) <= 5 && ( ( time_wait + time_frame + time_process ) / count ) > 40000 )
587 // Unlock if there's a lock object
588 if ( lock_object ) mlt_service_unlock( lock_object );
591 // Remove the last frame
592 mlt_frame_close( frame );
597 static void consumer_read_ahead_start( mlt_consumer this )
602 // Create the frame queue
603 this->queue = mlt_deque_init( );
606 pthread_mutex_init( &this->mutex, NULL );
608 // Create the condition
609 pthread_cond_init( &this->cond, NULL );
611 // Create the read ahead
612 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
615 static void consumer_read_ahead_stop( mlt_consumer this )
617 // Make sure we're running
620 // Inform thread to stop
623 // Broadcast to the condition in case it's waiting
624 pthread_mutex_lock( &this->mutex );
625 pthread_cond_broadcast( &this->cond );
626 pthread_mutex_unlock( &this->mutex );
628 // Broadcast to the put condition in case it's waiting
629 pthread_mutex_lock( &this->put_mutex );
630 pthread_cond_broadcast( &this->put_cond );
631 pthread_mutex_unlock( &this->put_mutex );
634 pthread_join( this->ahead_thread, NULL );
637 pthread_mutex_destroy( &this->mutex );
639 // Destroy the condition
640 pthread_cond_destroy( &this->cond );
643 while ( mlt_deque_count( this->queue ) )
644 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
647 mlt_deque_close( this->queue );
651 void mlt_consumer_purge( mlt_consumer this )
655 pthread_mutex_lock( &this->mutex );
656 while ( mlt_deque_count( this->queue ) )
657 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
658 pthread_cond_broadcast( &this->cond );
659 pthread_mutex_unlock( &this->mutex );
663 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
666 mlt_frame frame = NULL;
668 // Get the properties
669 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
671 // Check if the user has requested real time or not
672 if ( this->real_time )
676 // Is the read ahead running?
677 if ( this->ahead == 0 )
679 int buffer = mlt_properties_get_int( properties, "buffer" );
680 int prefill = mlt_properties_get_int( properties, "prefill" );
681 consumer_read_ahead_start( this );
683 size = prefill > 0 && prefill < buffer ? prefill : buffer;
686 // Get frame from queue
687 pthread_mutex_lock( &this->mutex );
688 while( this->ahead && mlt_deque_count( this->queue ) < size )
689 pthread_cond_wait( &this->cond, &this->mutex );
690 frame = mlt_deque_pop_front( this->queue );
691 pthread_cond_broadcast( &this->cond );
692 pthread_mutex_unlock( &this->mutex );
696 // Get the frame in non real time
697 frame = mlt_consumer_get_frame( this );
699 // This isn't true, but from the consumers perspective it is
701 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
707 /** Callback for the implementation to indicate a stopped condition.
710 void mlt_consumer_stopped( mlt_consumer this )
712 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
713 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
714 mlt_event_unblock( g_event_listener );
717 /** Stop the consumer.
720 int mlt_consumer_stop( mlt_consumer this )
723 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
724 char *debug = mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "debug" );
727 if ( debug ) fprintf( stderr, "%s: stopping put waiting\n", debug );
728 pthread_mutex_lock( &this->put_mutex );
729 this->put_active = 0;
730 pthread_cond_broadcast( &this->put_cond );
731 pthread_mutex_unlock( &this->put_mutex );
734 if ( debug ) fprintf( stderr, "%s: stopping consumer\n", debug );
735 if ( this->stop != NULL )
738 // Check if the user has requested real time or not and stop if necessary
739 if ( debug ) fprintf( stderr, "%s: stopping read_ahead\n", debug );
740 if ( mlt_properties_get_int( properties, "real_time" ) )
741 consumer_read_ahead_stop( this );
743 // Kill the test card
744 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
746 // Check and run a post command
747 if ( mlt_properties_get( properties, "post" ) )
748 system( mlt_properties_get( properties, "post" ) );
750 if ( debug ) fprintf( stderr, "%s: stopped\n", debug );
755 /** Determine if the consumer is stopped.
758 int mlt_consumer_is_stopped( mlt_consumer this )
760 // Check if the consumer is stopped
761 if ( this->is_stopped != NULL )
762 return this->is_stopped( this );
767 /** Close the consumer.
770 void mlt_consumer_close( mlt_consumer this )
772 if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
774 // Get the childs close function
775 void ( *consumer_close )( ) = this->close;
777 if ( consumer_close )
780 //mlt_consumer_stop( this );
783 consumer_close( this );
787 // Make sure it only gets called once
788 this->parent.close = NULL;
790 // Destroy the push mutex and condition
791 pthread_mutex_destroy( &this->put_mutex );
792 pthread_cond_destroy( &this->put_cond );
794 mlt_service_close( &this->parent );