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" ) );
320 static inline long time_difference( struct timeval *time1 )
322 struct timeval time2;
323 time2.tv_sec = time1->tv_sec;
324 time2.tv_usec = time1->tv_usec;
325 gettimeofday( time1, NULL );
326 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
329 static void *consumer_read_ahead_thread( void *arg )
331 // The argument is the consumer
332 mlt_consumer this = arg;
334 // Get the properties of the consumer
335 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
337 // Get the width and height
338 int width = mlt_properties_get_int( properties, "width" );
339 int height = mlt_properties_get_int( properties, "height" );
341 // See if video is turned off
342 int video_off = mlt_properties_get_int( properties, "video_off" );
344 // Get the audio settings
345 mlt_audio_format afmt = mlt_audio_pcm;
347 double fps = mlt_properties_get_double( properties, "fps" );
348 int channels = mlt_properties_get_int( properties, "channels" );
349 int frequency = mlt_properties_get_int( properties, "frequency" );
353 // See if audio is turned off
354 int audio_off = mlt_properties_get_int( properties, "audio_off" );
356 // Get the maximum size of the buffer
357 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
359 // General frame variable
360 mlt_frame frame = NULL;
361 uint8_t *image = NULL;
366 // Average time for get_frame and get_image
369 int64_t time_wait = 0;
370 int64_t time_frame = 0;
371 int64_t time_process = 0;
373 mlt_service lock_object = NULL;
375 // Get the first frame
376 frame = mlt_consumer_get_frame( this );
378 // Get the lock object
379 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
382 if ( lock_object ) mlt_service_lock( lock_object );
384 // Get the image of the first frame
387 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
388 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
393 samples = mlt_sample_calculator( fps, frequency, counter++ );
394 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
397 // Unlock the lock object
398 if ( lock_object ) mlt_service_unlock( lock_object );
401 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
403 // Get the starting time (can ignore the times above)
404 gettimeofday( &ante, NULL );
406 // Continue to read ahead
407 while ( this->ahead )
409 // Put the current frame into the queue
410 pthread_mutex_lock( &this->mutex );
411 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
412 pthread_cond_wait( &this->cond, &this->mutex );
413 mlt_deque_push_back( this->queue, frame );
414 pthread_cond_broadcast( &this->cond );
415 pthread_mutex_unlock( &this->mutex );
417 time_wait += time_difference( &ante );
419 // Get the next frame
420 frame = mlt_consumer_get_frame( this );
421 time_frame += time_difference( &ante );
423 // If there's no frame, we're probably stopped...
427 // Attempt to fetch the lock object
428 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
430 // Increment the count
433 // Lock if there's a lock object
434 if ( lock_object ) mlt_service_lock( lock_object );
436 // All non normal playback frames should be shown
437 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
439 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
451 // Get the image, mark as rendered and time it
454 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
455 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
457 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
461 // Increment the number of sequentially skipped frames
465 // If we've reached an unacceptable level, reset everything
476 // Always process audio
479 samples = mlt_sample_calculator( fps, frequency, counter++ );
480 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
483 // Increment the time take for this frame
484 time_process += time_difference( &ante );
486 // Determine if the next frame should be skipped
487 if ( mlt_deque_count( this->queue ) <= 5 && ( ( time_wait + time_frame + time_process ) / count ) > 40000 )
490 // Unlock if there's a lock object
491 if ( lock_object ) mlt_service_unlock( lock_object );
494 // Remove the last frame
495 mlt_frame_close( frame );
500 static void consumer_read_ahead_start( mlt_consumer this )
505 // Create the frame queue
506 this->queue = mlt_deque_init( );
509 pthread_mutex_init( &this->mutex, NULL );
511 // Create the condition
512 pthread_cond_init( &this->cond, NULL );
514 // Create the read ahead
515 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
518 static void consumer_read_ahead_stop( mlt_consumer this )
520 // Make sure we're running
523 // Inform thread to stop
526 // Broadcast to the condition in case it's waiting
527 pthread_mutex_lock( &this->mutex );
528 pthread_cond_broadcast( &this->cond );
529 pthread_mutex_unlock( &this->mutex );
531 // Broadcast to the put condition in case it's waiting
532 pthread_mutex_lock( &this->put_mutex );
533 pthread_cond_broadcast( &this->put_cond );
534 pthread_mutex_unlock( &this->put_mutex );
537 pthread_join( this->ahead_thread, NULL );
540 pthread_mutex_destroy( &this->mutex );
542 // Destroy the condition
543 pthread_cond_destroy( &this->cond );
546 while ( mlt_deque_count( this->queue ) )
547 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
550 mlt_deque_close( this->queue );
554 void mlt_consumer_purge( mlt_consumer this )
558 pthread_mutex_lock( &this->mutex );
559 while ( mlt_deque_count( this->queue ) )
560 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
561 pthread_cond_broadcast( &this->cond );
562 pthread_mutex_unlock( &this->mutex );
566 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
569 mlt_frame frame = NULL;
571 // Get the properties
572 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
574 // Check if the user has requested real time or not
575 if ( this->real_time )
579 // Is the read ahead running?
580 if ( this->ahead == 0 )
582 int buffer = mlt_properties_get_int( properties, "buffer" );
583 int prefill = mlt_properties_get_int( properties, "prefill" );
584 consumer_read_ahead_start( this );
586 size = prefill > 0 && prefill < buffer ? prefill : buffer;
589 // Get frame from queue
590 pthread_mutex_lock( &this->mutex );
591 while( this->ahead && mlt_deque_count( this->queue ) < size )
592 pthread_cond_wait( &this->cond, &this->mutex );
593 frame = mlt_deque_pop_front( this->queue );
594 pthread_cond_broadcast( &this->cond );
595 pthread_mutex_unlock( &this->mutex );
599 // Get the frame in non real time
600 frame = mlt_consumer_get_frame( this );
602 // This isn't true, but from the consumers perspective it is
604 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
610 /** Callback for the implementation to indicate a stopped condition.
613 void mlt_consumer_stopped( mlt_consumer this )
615 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
616 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
619 /** Stop the consumer.
622 int mlt_consumer_stop( mlt_consumer this )
625 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
626 char *debug = mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "debug" );
629 if ( debug ) fprintf( stderr, "%s: stopping put waiting\n", debug );
630 pthread_mutex_lock( &this->put_mutex );
631 this->put_active = 0;
632 pthread_cond_broadcast( &this->put_cond );
633 pthread_mutex_unlock( &this->put_mutex );
636 if ( debug ) fprintf( stderr, "%s: stopping consumer\n", debug );
637 if ( this->stop != NULL )
640 // Check if the user has requested real time or not and stop if necessary
641 if ( debug ) fprintf( stderr, "%s: stopping read_ahead\n", debug );
642 if ( mlt_properties_get_int( properties, "real_time" ) )
643 consumer_read_ahead_stop( this );
645 // Kill the test card
646 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
648 // Check and run a post command
649 if ( mlt_properties_get( properties, "post" ) )
650 system( mlt_properties_get( properties, "post" ) );
652 if ( debug ) fprintf( stderr, "%s: stopped\n", debug );
657 /** Determine if the consumer is stopped.
660 int mlt_consumer_is_stopped( mlt_consumer this )
662 // Check if the consumer is stopped
663 if ( this->is_stopped != NULL )
664 return this->is_stopped( this );
669 /** Close the consumer.
672 void mlt_consumer_close( mlt_consumer this )
674 if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
676 // Get the childs close function
677 void ( *consumer_close )( ) = this->close;
679 if ( consumer_close )
682 //mlt_consumer_stop( this );
685 consumer_close( this );
689 // Make sure it only gets called once
690 this->parent.close = NULL;
692 // Destroy the push mutex and condition
693 pthread_mutex_destroy( &this->put_mutex );
694 pthread_cond_destroy( &this->put_cond );
696 mlt_service_close( &this->parent );