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 program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * 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_den", 25 );
57 mlt_properties_set_int( properties, "frame_rate_num", 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 );
65 mlt_properties_set( properties, "normalisation", "NTSC" );
66 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
67 mlt_properties_set_int( properties, "frame_rate_den", 30000 );
68 mlt_properties_set_int( properties, "frame_rate_num", 1001 );
69 mlt_properties_set_int( properties, "width", 720 );
70 mlt_properties_set_int( properties, "height", 480 );
71 mlt_properties_set_int( properties, "progressive", 0 );
72 mlt_properties_set_double( properties, "aspect_ratio", 10.0 / 11.0 );
75 // Default rescaler for all consumers
76 mlt_properties_set( properties, "rescale", "bilinear" );
78 // Default read ahead buffer size
79 mlt_properties_set_int( properties, "buffer", 25 );
81 // Default audio frequency and channels
82 mlt_properties_set_int( properties, "frequency", 48000 );
83 mlt_properties_set_int( properties, "channels", 2 );
85 // Default of all consumers is real time
86 mlt_properties_set_int( properties, "real_time", 1 );
88 // Default to environment test card
89 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
91 // Hmm - default all consumers to yuv422 :-/
92 this->format = mlt_image_yuv422;
94 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
95 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
96 mlt_events_register( properties, "consumer-stopped", NULL );
98 // Create the push mutex and condition
99 pthread_mutex_init( &this->put_mutex, NULL );
100 pthread_cond_init( &this->put_cond, NULL );
106 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
108 if ( listener != NULL )
109 listener( owner, this, ( mlt_frame )args[ 0 ] );
112 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
114 if ( listener != NULL )
115 listener( owner, this, ( mlt_frame )args[ 0 ] );
118 /** Create a new consumer.
121 mlt_consumer mlt_consumer_new( )
123 // Create the memory for the structure
124 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
128 mlt_consumer_init( this, NULL );
134 /** Get the parent service object.
137 mlt_service mlt_consumer_service( mlt_consumer this )
139 return this != NULL ? &this->parent : NULL;
142 /** Get the consumer properties.
145 mlt_properties mlt_consumer_properties( mlt_consumer this )
147 return this != NULL ? MLT_SERVICE_PROPERTIES( &this->parent ) : NULL;
150 /** Connect the consumer to the producer.
153 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
155 return mlt_service_connect_producer( &this->parent, producer, 0 );
158 /** Start the consumer.
161 int mlt_consumer_start( mlt_consumer this )
164 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
166 // Determine if there's a test card producer
167 char *test_card = mlt_properties_get( properties, "test_card" );
169 // Just to make sure nothing is hanging around...
170 mlt_frame_close( this->put );
172 this->put_active = 1;
175 if ( test_card != NULL )
177 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
179 // Create a test card producer
180 mlt_producer producer = mlt_factory_producer( NULL, test_card );
182 // Do we have a producer
183 if ( producer != NULL )
185 // Test card should loop I guess...
186 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
187 //mlt_producer_set_speed( producer, 0 );
188 //mlt_producer_set_in_and_out( producer, 0, 0 );
190 // Set the test card on the consumer
191 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
197 // Allow the hash table to speed things up
198 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
201 // Check and run an ante command
202 if ( mlt_properties_get( properties, "ante" ) )
203 system( mlt_properties_get( properties, "ante" ) );
205 // Set the real_time preference
206 this->real_time = mlt_properties_get_int( properties, "real_time" );
209 if ( this->start != NULL )
210 return this->start( this );
215 /** An alternative method to feed frames into the consumer - only valid if
216 the consumer itself is not connected.
219 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
223 // Get the service assoicated to the consumer
224 mlt_service service = MLT_CONSUMER_SERVICE( this );
226 if ( mlt_service_producer( service ) == NULL )
230 pthread_mutex_lock( &this->put_mutex );
231 while ( this->put_active && this->put != NULL )
233 gettimeofday( &now, NULL );
234 tm.tv_sec = now.tv_sec + 1;
235 tm.tv_nsec = now.tv_usec * 1000;
236 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
238 if ( this->put_active && this->put == NULL )
241 mlt_frame_close( frame );
242 pthread_cond_broadcast( &this->put_cond );
243 pthread_mutex_unlock( &this->put_mutex );
247 mlt_frame_close( frame );
253 /** Protected method for consumer to get frames from connected service
256 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
259 mlt_frame frame = NULL;
261 // Get the service assoicated to the consumer
262 mlt_service service = MLT_CONSUMER_SERVICE( this );
264 // Get the consumer properties
265 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
268 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
272 pthread_mutex_lock( &this->put_mutex );
273 while ( this->put_active && this->put == NULL )
275 gettimeofday( &now, NULL );
276 tm.tv_sec = now.tv_sec + 1;
277 tm.tv_nsec = now.tv_usec * 1000;
278 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
282 pthread_cond_broadcast( &this->put_cond );
283 pthread_mutex_unlock( &this->put_mutex );
285 mlt_service_apply_filters( service, frame, 0 );
287 else if ( mlt_service_producer( service ) != NULL )
289 mlt_service_get_frame( service, &frame, 0 );
293 frame = mlt_frame_init( );
298 // Get the frame properties
299 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
301 // Get the test card producer
302 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
304 // Attach the test frame producer to it.
305 if ( test_card != NULL )
306 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
308 // Attach the rescale property
309 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
311 // Aspect ratio and other jiggery pokery
312 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
313 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
314 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
321 static inline long time_difference( struct timeval *time1 )
323 struct timeval time2;
324 time2.tv_sec = time1->tv_sec;
325 time2.tv_usec = time1->tv_usec;
326 gettimeofday( time1, NULL );
327 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
330 static void *consumer_read_ahead_thread( void *arg )
332 // The argument is the consumer
333 mlt_consumer this = arg;
335 // Get the properties of the consumer
336 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
338 // Get the width and height
339 int width = mlt_properties_get_int( properties, "width" );
340 int height = mlt_properties_get_int( properties, "height" );
342 // See if video is turned off
343 int video_off = mlt_properties_get_int( properties, "video_off" );
345 // Get the audio settings
346 mlt_audio_format afmt = mlt_audio_pcm;
348 double fps = mlt_properties_get_double( properties, "fps" );
349 int channels = mlt_properties_get_int( properties, "channels" );
350 int frequency = mlt_properties_get_int( properties, "frequency" );
354 // See if audio is turned off
355 int audio_off = mlt_properties_get_int( properties, "audio_off" );
357 // Get the maximum size of the buffer
358 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
360 // General frame variable
361 mlt_frame frame = NULL;
362 uint8_t *image = NULL;
367 // Average time for get_frame and get_image
370 int64_t time_wait = 0;
371 int64_t time_frame = 0;
372 int64_t time_process = 0;
374 mlt_service lock_object = NULL;
376 // Get the first frame
377 frame = mlt_consumer_get_frame( this );
379 // Get the lock object
380 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
383 if ( lock_object ) mlt_service_lock( lock_object );
385 // Get the image of the first frame
388 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
389 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
394 samples = mlt_sample_calculator( fps, frequency, counter++ );
395 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
398 // Unlock the lock object
399 if ( lock_object ) mlt_service_unlock( lock_object );
402 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
404 // Get the starting time (can ignore the times above)
405 gettimeofday( &ante, NULL );
407 // Continue to read ahead
408 while ( this->ahead )
410 // Put the current frame into the queue
411 pthread_mutex_lock( &this->mutex );
412 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
413 pthread_cond_wait( &this->cond, &this->mutex );
414 mlt_deque_push_back( this->queue, frame );
415 pthread_cond_broadcast( &this->cond );
416 pthread_mutex_unlock( &this->mutex );
418 time_wait += time_difference( &ante );
420 // Get the next frame
421 frame = mlt_consumer_get_frame( this );
422 time_frame += time_difference( &ante );
424 // If there's no frame, we're probably stopped...
428 // Attempt to fetch the lock object
429 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
431 // Increment the count
434 // Lock if there's a lock object
435 if ( lock_object ) mlt_service_lock( lock_object );
437 // All non normal playback frames should be shown
438 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
440 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
452 // Get the image, mark as rendered and time it
455 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
456 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
458 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
462 // Increment the number of sequentially skipped frames
466 // If we've reached an unacceptable level, reset everything
477 // Always process audio
480 samples = mlt_sample_calculator( fps, frequency, counter++ );
481 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
484 // Increment the time take for this frame
485 time_process += time_difference( &ante );
487 // Determine if the next frame should be skipped
488 if ( mlt_deque_count( this->queue ) <= 5 && ( ( time_wait + time_frame + time_process ) / count ) > 40000 )
491 // Unlock if there's a lock object
492 if ( lock_object ) mlt_service_unlock( lock_object );
495 // Remove the last frame
496 mlt_frame_close( frame );
501 static void consumer_read_ahead_start( mlt_consumer this )
506 // Create the frame queue
507 this->queue = mlt_deque_init( );
510 pthread_mutex_init( &this->mutex, NULL );
512 // Create the condition
513 pthread_cond_init( &this->cond, NULL );
515 // Create the read ahead
516 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
519 static void consumer_read_ahead_stop( mlt_consumer this )
521 // Make sure we're running
524 // Inform thread to stop
527 // Broadcast to the condition in case it's waiting
528 pthread_mutex_lock( &this->mutex );
529 pthread_cond_broadcast( &this->cond );
530 pthread_mutex_unlock( &this->mutex );
532 // Broadcast to the put condition in case it's waiting
533 pthread_mutex_lock( &this->put_mutex );
534 pthread_cond_broadcast( &this->put_cond );
535 pthread_mutex_unlock( &this->put_mutex );
538 pthread_join( this->ahead_thread, NULL );
541 pthread_mutex_destroy( &this->mutex );
543 // Destroy the condition
544 pthread_cond_destroy( &this->cond );
547 while ( mlt_deque_count( this->queue ) )
548 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
551 mlt_deque_close( this->queue );
555 void mlt_consumer_purge( mlt_consumer this )
559 pthread_mutex_lock( &this->mutex );
560 while ( mlt_deque_count( this->queue ) )
561 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
562 pthread_cond_broadcast( &this->cond );
563 pthread_mutex_unlock( &this->mutex );
567 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
570 mlt_frame frame = NULL;
572 // Get the properties
573 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
575 // Check if the user has requested real time or not
576 if ( this->real_time )
580 // Is the read ahead running?
581 if ( this->ahead == 0 )
583 int buffer = mlt_properties_get_int( properties, "buffer" );
584 int prefill = mlt_properties_get_int( properties, "prefill" );
585 consumer_read_ahead_start( this );
587 size = prefill > 0 && prefill < buffer ? prefill : buffer;
590 // Get frame from queue
591 pthread_mutex_lock( &this->mutex );
592 while( this->ahead && mlt_deque_count( this->queue ) < size )
593 pthread_cond_wait( &this->cond, &this->mutex );
594 frame = mlt_deque_pop_front( this->queue );
595 pthread_cond_broadcast( &this->cond );
596 pthread_mutex_unlock( &this->mutex );
600 // Get the frame in non real time
601 frame = mlt_consumer_get_frame( this );
603 // This isn't true, but from the consumers perspective it is
605 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
611 /** Callback for the implementation to indicate a stopped condition.
614 void mlt_consumer_stopped( mlt_consumer this )
616 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
617 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
620 /** Stop the consumer.
623 int mlt_consumer_stop( mlt_consumer this )
626 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
627 char *debug = mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "debug" );
630 if ( debug ) fprintf( stderr, "%s: stopping put waiting\n", debug );
631 pthread_mutex_lock( &this->put_mutex );
632 this->put_active = 0;
633 pthread_cond_broadcast( &this->put_cond );
634 pthread_mutex_unlock( &this->put_mutex );
637 if ( debug ) fprintf( stderr, "%s: stopping consumer\n", debug );
638 if ( this->stop != NULL )
641 // Check if the user has requested real time or not and stop if necessary
642 if ( debug ) fprintf( stderr, "%s: stopping read_ahead\n", debug );
643 if ( mlt_properties_get_int( properties, "real_time" ) )
644 consumer_read_ahead_stop( this );
646 // Kill the test card
647 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
649 // Check and run a post command
650 if ( mlt_properties_get( properties, "post" ) )
651 system( mlt_properties_get( properties, "post" ) );
653 if ( debug ) fprintf( stderr, "%s: stopped\n", debug );
658 /** Determine if the consumer is stopped.
661 int mlt_consumer_is_stopped( mlt_consumer this )
663 // Check if the consumer is stopped
664 if ( this->is_stopped != NULL )
665 return this->is_stopped( this );
670 /** Close the consumer.
673 void mlt_consumer_close( mlt_consumer this )
675 if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
677 // Get the childs close function
678 void ( *consumer_close )( ) = this->close;
680 if ( consumer_close )
683 //mlt_consumer_stop( this );
686 consumer_close( this );
690 // Make sure it only gets called once
691 this->parent.close = NULL;
693 // Destroy the push mutex and condition
694 pthread_mutex_destroy( &this->put_mutex );
695 pthread_cond_destroy( &this->put_cond );
697 mlt_service_close( &this->parent );