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"
31 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
32 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
34 /** Public final methods
37 int mlt_consumer_init( mlt_consumer this, void *child )
40 memset( this, 0, sizeof( struct mlt_consumer_s ) );
42 error = mlt_service_init( &this->parent, this );
45 // Get the properties from the service
46 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
48 // Get the normalisation preference
49 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
51 // Deal with normalisation
52 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
54 mlt_properties_set( properties, "normalisation", "PAL" );
55 mlt_properties_set_double( properties, "fps", 25.0 );
56 mlt_properties_set_int( properties, "frame_rate_num", 25 );
57 mlt_properties_set_int( properties, "frame_rate_den", 1 );
58 mlt_properties_set_int( properties, "width", 720 );
59 mlt_properties_set_int( properties, "height", 576 );
60 mlt_properties_set_int( properties, "progressive", 0 );
61 mlt_properties_set_double( properties, "aspect_ratio", 59.0 / 54.0 );
62 mlt_properties_set_int( properties, "aspect_ratio_num", 59 );
63 mlt_properties_set_int( properties, "aspect_ratio_den", 54 );
64 mlt_properties_set_double( properties, "display_ratio", 4.0 / 3.0 );
65 mlt_properties_set_int( properties, "display_ratio_num", 4 );
66 mlt_properties_set_int( properties, "display_ratio_den", 3 );
70 mlt_properties_set( properties, "normalisation", "NTSC" );
71 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
72 mlt_properties_set_int( properties, "frame_rate_num", 30000 );
73 mlt_properties_set_int( properties, "frame_rate_den", 1001 );
74 mlt_properties_set_int( properties, "width", 720 );
75 mlt_properties_set_int( properties, "height", 480 );
76 mlt_properties_set_int( properties, "progressive", 0 );
77 mlt_properties_set_double( properties, "aspect_ratio", 10.0 / 11.0 );
78 mlt_properties_set_int( properties, "aspect_ratio_num", 10 );
79 mlt_properties_set_int( properties, "aspect_ratio_den", 11 );
80 mlt_properties_set_double( properties, "display_ratio", 4.0 / 3.0 );
81 mlt_properties_set_int( properties, "display_ratio_num", 4 );
82 mlt_properties_set_int( properties, "display_ratio_den", 3 );
85 // Default rescaler for all consumers
86 mlt_properties_set( properties, "rescale", "bilinear" );
88 // Default read ahead buffer size
89 mlt_properties_set_int( properties, "buffer", 25 );
91 // Default audio frequency and channels
92 mlt_properties_set_int( properties, "frequency", 48000 );
93 mlt_properties_set_int( properties, "channels", 2 );
95 // Default of all consumers is real time
96 mlt_properties_set_int( properties, "real_time", 1 );
98 // Default to environment test card
99 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
101 // Hmm - default all consumers to yuv422 :-/
102 this->format = mlt_image_yuv422;
104 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
105 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
106 mlt_events_register( properties, "consumer-stopped", NULL );
108 // Create the push mutex and condition
109 pthread_mutex_init( &this->put_mutex, NULL );
110 pthread_cond_init( &this->put_cond, NULL );
116 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
118 if ( listener != NULL )
119 listener( owner, this, ( mlt_frame )args[ 0 ] );
122 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
124 if ( listener != NULL )
125 listener( owner, this, ( mlt_frame )args[ 0 ] );
128 /** Create a new consumer.
131 mlt_consumer mlt_consumer_new( )
133 // Create the memory for the structure
134 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
138 mlt_consumer_init( this, NULL );
144 /** Get the parent service object.
147 mlt_service mlt_consumer_service( mlt_consumer this )
149 return this != NULL ? &this->parent : NULL;
152 /** Get the consumer properties.
155 mlt_properties mlt_consumer_properties( mlt_consumer this )
157 return this != NULL ? MLT_SERVICE_PROPERTIES( &this->parent ) : NULL;
160 /** Connect the consumer to the producer.
163 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
165 return mlt_service_connect_producer( &this->parent, producer, 0 );
168 /** Start the consumer.
171 int mlt_consumer_start( mlt_consumer this )
174 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
176 // Determine if there's a test card producer
177 char *test_card = mlt_properties_get( properties, "test_card" );
180 char *profile = mlt_properties_get( properties, "profile" );
181 int profile_ok = mlt_consumer_profile( properties, profile );
183 // Check that everything is OK
185 fprintf( stderr, "Unrecognised profile %s - continuing with defaults.\n", mlt_properties_get( properties, "profile" ) );
186 else if ( profile_ok < 0 )
187 fprintf( stderr, "Recognised profile %s with warnings - trying to continue with some defaults.\n", mlt_properties_get( properties, "profile" ) );
189 // Just to make sure nothing is hanging around...
190 mlt_frame_close( this->put );
192 this->put_active = 1;
195 if ( test_card != NULL )
197 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
199 // Create a test card producer
200 mlt_producer producer = mlt_factory_producer( NULL, test_card );
202 // Do we have a producer
203 if ( producer != NULL )
205 // Test card should loop I guess...
206 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
207 //mlt_producer_set_speed( producer, 0 );
208 //mlt_producer_set_in_and_out( producer, 0, 0 );
210 // Set the test card on the consumer
211 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
217 // Allow the hash table to speed things up
218 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
221 // Check and run an ante command
222 if ( mlt_properties_get( properties, "ante" ) )
223 system( mlt_properties_get( properties, "ante" ) );
225 // Set the real_time preference
226 this->real_time = mlt_properties_get_int( properties, "real_time" );
229 if ( this->start != NULL )
230 return this->start( this );
235 /** An alternative method to feed frames into the consumer - only valid if
236 the consumer itself is not connected.
239 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
243 // Get the service assoicated to the consumer
244 mlt_service service = MLT_CONSUMER_SERVICE( this );
246 if ( mlt_service_producer( service ) == NULL )
250 pthread_mutex_lock( &this->put_mutex );
251 while ( this->put_active && this->put != NULL )
253 gettimeofday( &now, NULL );
254 tm.tv_sec = now.tv_sec + 1;
255 tm.tv_nsec = now.tv_usec * 1000;
256 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
258 if ( this->put_active && this->put == NULL )
261 mlt_frame_close( frame );
262 pthread_cond_broadcast( &this->put_cond );
263 pthread_mutex_unlock( &this->put_mutex );
267 mlt_frame_close( frame );
273 /** Protected method for consumer to get frames from connected service
276 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
279 mlt_frame frame = NULL;
281 // Get the service assoicated to the consumer
282 mlt_service service = MLT_CONSUMER_SERVICE( this );
284 // Get the consumer properties
285 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
288 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
292 pthread_mutex_lock( &this->put_mutex );
293 while ( this->put_active && this->put == NULL )
295 gettimeofday( &now, NULL );
296 tm.tv_sec = now.tv_sec + 1;
297 tm.tv_nsec = now.tv_usec * 1000;
298 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
302 pthread_cond_broadcast( &this->put_cond );
303 pthread_mutex_unlock( &this->put_mutex );
305 mlt_service_apply_filters( service, frame, 0 );
307 else if ( mlt_service_producer( service ) != NULL )
309 mlt_service_get_frame( service, &frame, 0 );
313 frame = mlt_frame_init( );
318 // Get the frame properties
319 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
321 // Get the test card producer
322 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
324 // Attach the test frame producer to it.
325 if ( test_card != NULL )
326 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
328 // Attach the rescale property
329 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
331 // Aspect ratio and other jiggery pokery
332 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
333 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
334 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
341 static inline long time_difference( struct timeval *time1 )
343 struct timeval time2;
344 time2.tv_sec = time1->tv_sec;
345 time2.tv_usec = time1->tv_usec;
346 gettimeofday( time1, NULL );
347 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
350 int mlt_consumer_profile( mlt_properties properties, char *profile )
352 double fps = mlt_properties_get_double( properties, "fps" );
354 double display_num = 0;
355 double display_den = 0;
356 double pixel_num = 0;
357 double pixel_den = 0;
361 if ( fps == 25.0 && profile != NULL )
370 if ( !strcmp( profile, "dv_wide" ) )
377 else if ( !strncmp( profile, "square_wide", 11 ) )
386 else if ( !strncmp( profile, "square", 6 ) )
393 else if ( !strncmp( profile, "vcd", 3 ) )
398 else if ( !strncmp( profile, "cvd", 3 ) )
405 else if ( !strncmp( profile, "svcd_wide", 9 ) )
414 else if ( !strncmp( profile, "svcd", 4 ) )
421 else if ( strncmp( profile, "frame", 5 ) && strcmp( profile, "dv" ) )
426 else if ( profile != NULL )
435 if ( !strcmp( profile, "dv_wide" ) )
442 else if ( !strncmp( profile, "square_wide", 11 ) )
451 else if ( !strncmp( profile, "square", 6 ) )
458 else if ( !strncmp( profile, "vcd", 3 ) )
463 else if ( !strncmp( profile, "cvd", 3 ) )
470 else if ( !strncmp( profile, "svcd_wide", 9 ) )
479 else if ( !strncmp( profile, "svcd", 4 ) )
486 else if ( strncmp( profile, "frame", 5 ) && strcmp( profile, "dv" ) )
492 // If width (or any of the above are 0), we use defaults otherwise we switch to recognised
493 if ( width != 0 && recognised )
495 if ( recognised && strchr( profile, ':' ) )
496 if ( sscanf( strchr( profile, ':' ) + 1, "%dx%d", &width, &height ) != 2 )
499 mlt_properties_set_int( properties, "width", width );
500 mlt_properties_set_int( properties, "height", height );
501 mlt_properties_set_double( properties, "aspect_ratio", pixel_num / pixel_den );
502 mlt_properties_set_int( properties, "aspect_ratio_num", pixel_num );
503 mlt_properties_set_int( properties, "aspect_ratio_den", pixel_den );
504 mlt_properties_set_double( properties, "display_ratio", display_num / display_den );
505 mlt_properties_set_int( properties, "display_ratio_num", display_num );
506 mlt_properties_set_int( properties, "display_ratio_den", display_den );
512 static void *consumer_read_ahead_thread( void *arg )
514 // The argument is the consumer
515 mlt_consumer this = arg;
517 // Get the properties of the consumer
518 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
520 // Get the width and height
521 int width = mlt_properties_get_int( properties, "width" );
522 int height = mlt_properties_get_int( properties, "height" );
524 // See if video is turned off
525 int video_off = mlt_properties_get_int( properties, "video_off" );
526 int preview_off = mlt_properties_get_int( properties, "preview_off" );
527 int preview_format = mlt_properties_get_int( properties, "preview_format" );
529 // Get the audio settings
530 mlt_audio_format afmt = mlt_audio_pcm;
532 double fps = mlt_properties_get_double( properties, "fps" );
533 int channels = mlt_properties_get_int( properties, "channels" );
534 int frequency = mlt_properties_get_int( properties, "frequency" );
538 // See if audio is turned off
539 int audio_off = mlt_properties_get_int( properties, "audio_off" );
541 // Get the maximum size of the buffer
542 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
544 // General frame variable
545 mlt_frame frame = NULL;
546 uint8_t *image = NULL;
551 // Average time for get_frame and get_image
554 int64_t time_wait = 0;
555 int64_t time_frame = 0;
556 int64_t time_process = 0;
558 mlt_service lock_object = NULL;
560 if ( preview_off && preview_format != 0 )
561 this->format = preview_format;
563 // Get the first frame
564 frame = mlt_consumer_get_frame( this );
566 // Get the lock object
567 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
570 if ( lock_object ) mlt_service_lock( lock_object );
572 // Get the image of the first frame
575 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
576 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
581 samples = mlt_sample_calculator( fps, frequency, counter++ );
582 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
585 // Unlock the lock object
586 if ( lock_object ) mlt_service_unlock( lock_object );
589 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
591 // Get the starting time (can ignore the times above)
592 gettimeofday( &ante, NULL );
594 // Continue to read ahead
595 while ( this->ahead )
597 // Fetch width/height again
598 width = mlt_properties_get_int( properties, "width" );
599 height = mlt_properties_get_int( properties, "height" );
601 // Put the current frame into the queue
602 pthread_mutex_lock( &this->mutex );
603 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
604 pthread_cond_wait( &this->cond, &this->mutex );
605 mlt_deque_push_back( this->queue, frame );
606 pthread_cond_broadcast( &this->cond );
607 pthread_mutex_unlock( &this->mutex );
609 time_wait += time_difference( &ante );
611 // Get the next frame
612 frame = mlt_consumer_get_frame( this );
613 time_frame += time_difference( &ante );
615 // If there's no frame, we're probably stopped...
619 // Attempt to fetch the lock object
620 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
622 // Increment the count
625 // Lock if there's a lock object
626 if ( lock_object ) mlt_service_lock( lock_object );
628 // All non normal playback frames should be shown
629 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
631 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
643 // Get the image, mark as rendered and time it
646 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
647 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
649 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
653 // Increment the number of sequentially skipped frames
657 // If we've reached an unacceptable level, reset everything
668 // Always process audio
671 samples = mlt_sample_calculator( fps, frequency, counter++ );
672 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
675 // Increment the time take for this frame
676 time_process += time_difference( &ante );
678 // Determine if the next frame should be skipped
679 if ( mlt_deque_count( this->queue ) <= 5 && ( ( time_wait + time_frame + time_process ) / count ) > 40000 )
682 // Unlock if there's a lock object
683 if ( lock_object ) mlt_service_unlock( lock_object );
686 // Remove the last frame
687 mlt_frame_close( frame );
692 static void consumer_read_ahead_start( mlt_consumer this )
697 // Create the frame queue
698 this->queue = mlt_deque_init( );
701 pthread_mutex_init( &this->mutex, NULL );
703 // Create the condition
704 pthread_cond_init( &this->cond, NULL );
706 // Create the read ahead
707 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
710 static void consumer_read_ahead_stop( mlt_consumer this )
712 // Make sure we're running
715 // Inform thread to stop
718 // Broadcast to the condition in case it's waiting
719 pthread_mutex_lock( &this->mutex );
720 pthread_cond_broadcast( &this->cond );
721 pthread_mutex_unlock( &this->mutex );
723 // Broadcast to the put condition in case it's waiting
724 pthread_mutex_lock( &this->put_mutex );
725 pthread_cond_broadcast( &this->put_cond );
726 pthread_mutex_unlock( &this->put_mutex );
729 pthread_join( this->ahead_thread, NULL );
732 pthread_mutex_destroy( &this->mutex );
734 // Destroy the condition
735 pthread_cond_destroy( &this->cond );
738 while ( mlt_deque_count( this->queue ) )
739 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
742 mlt_deque_close( this->queue );
746 void mlt_consumer_purge( mlt_consumer this )
750 pthread_mutex_lock( &this->mutex );
751 while ( mlt_deque_count( this->queue ) )
752 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
753 pthread_cond_broadcast( &this->cond );
754 pthread_mutex_unlock( &this->mutex );
758 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
761 mlt_frame frame = NULL;
763 // Get the properties
764 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
766 // Check if the user has requested real time or not
767 if ( this->real_time )
771 // Is the read ahead running?
772 if ( this->ahead == 0 )
774 int buffer = mlt_properties_get_int( properties, "buffer" );
775 int prefill = mlt_properties_get_int( properties, "prefill" );
776 consumer_read_ahead_start( this );
778 size = prefill > 0 && prefill < buffer ? prefill : buffer;
781 // Get frame from queue
782 pthread_mutex_lock( &this->mutex );
783 while( this->ahead && mlt_deque_count( this->queue ) < size )
784 pthread_cond_wait( &this->cond, &this->mutex );
785 frame = mlt_deque_pop_front( this->queue );
786 pthread_cond_broadcast( &this->cond );
787 pthread_mutex_unlock( &this->mutex );
791 // Get the frame in non real time
792 frame = mlt_consumer_get_frame( this );
794 // This isn't true, but from the consumers perspective it is
796 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
802 /** Callback for the implementation to indicate a stopped condition.
805 void mlt_consumer_stopped( mlt_consumer this )
807 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
808 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
811 /** Stop the consumer.
814 int mlt_consumer_stop( mlt_consumer this )
817 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
818 char *debug = mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "debug" );
821 if ( debug ) fprintf( stderr, "%s: stopping put waiting\n", debug );
822 pthread_mutex_lock( &this->put_mutex );
823 this->put_active = 0;
824 pthread_cond_broadcast( &this->put_cond );
825 pthread_mutex_unlock( &this->put_mutex );
828 if ( debug ) fprintf( stderr, "%s: stopping consumer\n", debug );
829 if ( this->stop != NULL )
832 // Check if the user has requested real time or not and stop if necessary
833 if ( debug ) fprintf( stderr, "%s: stopping read_ahead\n", debug );
834 if ( mlt_properties_get_int( properties, "real_time" ) )
835 consumer_read_ahead_stop( this );
837 // Kill the test card
838 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
840 // Check and run a post command
841 if ( mlt_properties_get( properties, "post" ) )
842 system( mlt_properties_get( properties, "post" ) );
844 if ( debug ) fprintf( stderr, "%s: stopped\n", debug );
849 /** Determine if the consumer is stopped.
852 int mlt_consumer_is_stopped( mlt_consumer this )
854 // Check if the consumer is stopped
855 if ( this->is_stopped != NULL )
856 return this->is_stopped( this );
861 /** Close the consumer.
864 void mlt_consumer_close( mlt_consumer this )
866 if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
868 // Get the childs close function
869 void ( *consumer_close )( ) = this->close;
871 if ( consumer_close )
874 //mlt_consumer_stop( this );
877 consumer_close( this );
881 // Make sure it only gets called once
882 this->parent.close = NULL;
884 // Destroy the push mutex and condition
885 pthread_mutex_destroy( &this->put_mutex );
886 pthread_cond_destroy( &this->put_cond );
888 mlt_service_close( &this->parent );