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_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
33 /** Public final methods
36 int mlt_consumer_init( mlt_consumer this, void *child )
39 memset( this, 0, sizeof( struct mlt_consumer_s ) );
41 error = mlt_service_init( &this->parent, this );
44 // Get the properties from the service
45 mlt_properties properties = mlt_service_properties( &this->parent );
47 // Get the normalisation preference
48 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
50 // Deal with normalisation
51 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
53 mlt_properties_set( properties, "normalisation", "PAL" );
54 mlt_properties_set_double( properties, "fps", 25.0 );
55 mlt_properties_set_int( properties, "width", 720 );
56 mlt_properties_set_int( properties, "height", 576 );
57 mlt_properties_set_int( properties, "progressive", 0 );
58 mlt_properties_set_double( properties, "aspect_ratio", 59.0 / 54.0 );
62 mlt_properties_set( properties, "normalisation", "NTSC" );
63 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
64 mlt_properties_set_int( properties, "width", 720 );
65 mlt_properties_set_int( properties, "height", 480 );
66 mlt_properties_set_int( properties, "progressive", 0 );
67 mlt_properties_set_double( properties, "aspect_ratio", 10.0 / 11.0 );
70 // Default rescaler for all consumers
71 mlt_properties_set( properties, "rescale", "bilinear" );
73 // Default read ahead buffer size
74 mlt_properties_set_int( properties, "buffer", 25 );
76 // Default audio frequency and channels
77 mlt_properties_set_int( properties, "frequency", 48000 );
78 mlt_properties_set_int( properties, "channels", 2 );
80 // Default of all consumers is real time
81 mlt_properties_set_int( properties, "real_time", 1 );
83 // Default to environment test card
84 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
86 // Hmm - default all consumers to yuv422 :-/
87 this->format = mlt_image_yuv422;
89 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
90 mlt_events_register( properties, "consumer-stopped", NULL );
92 // Create the push mutex and condition
93 pthread_mutex_init( &this->put_mutex, NULL );
94 pthread_cond_init( &this->put_cond, NULL );
100 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
102 if ( listener != NULL )
103 listener( owner, this, ( mlt_frame )args[ 0 ] );
106 /** Create a new consumer.
109 mlt_consumer mlt_consumer_new( )
111 // Create the memory for the structure
112 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
116 mlt_consumer_init( this, NULL );
122 /** Get the parent service object.
125 mlt_service mlt_consumer_service( mlt_consumer this )
127 return this != NULL ? &this->parent : NULL;
130 /** Get the consumer properties.
133 mlt_properties mlt_consumer_properties( mlt_consumer this )
135 return this != NULL ? mlt_service_properties( &this->parent ) : NULL;
138 /** Connect the consumer to the producer.
141 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
143 return mlt_service_connect_producer( &this->parent, producer, 0 );
146 /** Start the consumer.
149 int mlt_consumer_start( mlt_consumer this )
152 mlt_properties properties = mlt_consumer_properties( this );
154 // Determine if there's a test card producer
155 char *test_card = mlt_properties_get( properties, "test_card" );
158 if ( test_card != NULL )
160 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
162 // Create a test card producer
163 mlt_producer producer = mlt_factory_producer( NULL, test_card );
165 // Do we have a producer
166 if ( producer != NULL )
168 // Test card should loop I guess...
169 mlt_properties_set( mlt_producer_properties( producer ), "eof", "loop" );
171 // Set the test card on the consumer
172 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
178 // Allow the hash table to speed things up
179 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
182 // Check and run an ante command
183 if ( mlt_properties_get( properties, "ante" ) )
184 system( mlt_properties_get( properties, "ante" ) );
186 // Set the real_time preference
187 this->real_time = mlt_properties_get_int( properties, "real_time" );
190 if ( this->start != NULL )
191 return this->start( this );
196 /** An alternative method to feed frames into the consumer - only valid if
197 the consumer itself is not connected.
200 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
204 // Get the service assoicated to the consumer
205 mlt_service service = mlt_consumer_service( this );
207 if ( mlt_service_producer( service ) == NULL )
209 pthread_mutex_lock( &this->put_mutex );
210 if ( this->put != NULL )
211 pthread_cond_wait( &this->put_cond, &this->put_mutex );
212 if ( this->put == NULL )
215 mlt_frame_close( frame );
216 pthread_cond_broadcast( &this->put_cond );
217 pthread_mutex_unlock( &this->put_mutex );
221 mlt_frame_close( frame );
227 /** Protected method for consumer to get frames from connected service
230 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
233 mlt_frame frame = NULL;
235 // Get the service assoicated to the consumer
236 mlt_service service = mlt_consumer_service( this );
238 // Get the consumer properties
239 mlt_properties properties = mlt_consumer_properties( this );
242 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
244 pthread_mutex_lock( &this->put_mutex );
245 if ( this->put == NULL )
246 pthread_cond_wait( &this->put_cond, &this->put_mutex );
249 pthread_cond_broadcast( &this->put_cond );
250 pthread_mutex_unlock( &this->put_mutex );
252 mlt_service_apply_filters( service, frame, 0 );
255 if ( frame != NULL || mlt_service_get_frame( service, &frame, 0 ) == 0 )
257 // Get the frame properties
258 mlt_properties frame_properties = mlt_frame_properties( frame );
260 // Get the test card producer
261 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
263 // Attach the test frame producer to it.
264 if ( test_card != NULL )
265 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
267 // Attach the rescale property
268 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
270 // Aspect ratio and other jiggery pokery
271 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
272 if ( mlt_properties_get_int( properties, "progressive" ) || mlt_properties_get_int( properties, "deinterlace" ) )
273 mlt_properties_set_int( frame_properties, "consumer_deinterlace", 1 );
280 static inline long time_difference( struct timeval *time1 )
282 struct timeval time2;
283 time2.tv_sec = time1->tv_sec;
284 time2.tv_usec = time1->tv_usec;
285 gettimeofday( time1, NULL );
286 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
289 static void *consumer_read_ahead_thread( void *arg )
291 // The argument is the consumer
292 mlt_consumer this = arg;
294 // Get the properties of the consumer
295 mlt_properties properties = mlt_consumer_properties( this );
297 // Get the width and height
298 int width = mlt_properties_get_int( properties, "width" );
299 int height = mlt_properties_get_int( properties, "height" );
301 // See if video is turned off
302 int video_off = mlt_properties_get_int( properties, "video_off" );
304 // Get the audio settings
305 mlt_audio_format afmt = mlt_audio_pcm;
307 double fps = mlt_properties_get_double( properties, "fps" );
308 int channels = mlt_properties_get_int( properties, "channels" );
309 int frequency = mlt_properties_get_int( properties, "frequency" );
313 // See if audio is turned off
314 int audio_off = mlt_properties_get_int( properties, "audio_off" );
316 // Get the maximum size of the buffer
317 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
319 // General frame variable
320 mlt_frame frame = NULL;
321 uint8_t *image = NULL;
326 // Average time for get_frame and get_image
329 int64_t time_wait = 0;
330 int64_t time_frame = 0;
331 int64_t time_process = 0;
334 // Get the first frame
335 frame = mlt_consumer_get_frame( this );
337 // Get the image of the first frame
339 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
343 samples = mlt_sample_calculator( fps, frequency, counter++ );
344 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
345 frame->get_audio = NULL;
348 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
350 // Get the starting time (can ignore the times above)
351 gettimeofday( &ante, NULL );
353 // Continue to read ahead
354 while ( this->ahead )
356 // Put the current frame into the queue
357 pthread_mutex_lock( &this->mutex );
358 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
359 pthread_cond_wait( &this->cond, &this->mutex );
360 mlt_deque_push_back( this->queue, frame );
361 pthread_cond_broadcast( &this->cond );
362 pthread_mutex_unlock( &this->mutex );
363 time_wait += time_difference( &ante );
365 // Get the next frame
366 frame = mlt_consumer_get_frame( this );
367 time_frame += time_difference( &ante );
369 // Increment the count
372 // All non normal playback frames should be shown
373 if ( mlt_properties_get_int( mlt_frame_properties( frame ), "_speed" ) != 1 )
386 // Get the image, mark as rendered and time it
388 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
389 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
393 // Increment the number of sequentially skipped frames
397 // If we've reached an unacceptable level, reset everything
408 // Always process audio
411 samples = mlt_sample_calculator( fps, frequency, counter++ );
412 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
413 frame->get_audio = NULL;
416 // Increment the time take for this frame
417 time_process += time_difference( &ante );
419 // Determine if the next frame should be skipped
420 if ( mlt_deque_count( this->queue ) <= 5 && ( ( time_wait + time_frame + time_process ) / count ) > 40000 )
424 // Remove the last frame
425 mlt_frame_close( frame );
430 static void consumer_read_ahead_start( mlt_consumer this )
432 pthread_attr_t thread_attributes;
437 // Create the frame queue
438 this->queue = mlt_deque_init( );
441 pthread_mutex_init( &this->mutex, NULL );
443 // Create the condition
444 pthread_cond_init( &this->cond, NULL );
446 // Inherit the scheduling priority
447 pthread_attr_init( &thread_attributes );
448 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
450 // Create the read ahead
451 pthread_create( &this->ahead_thread, &thread_attributes, consumer_read_ahead_thread, this );
454 static void consumer_read_ahead_stop( mlt_consumer this )
456 // Make sure we're running
459 // Inform thread to stop
462 // Broadcast to the condition in case it's waiting
463 pthread_mutex_lock( &this->mutex );
464 pthread_cond_broadcast( &this->cond );
465 pthread_mutex_unlock( &this->mutex );
467 // Broadcast to the put condition in case it's waiting
468 pthread_mutex_lock( &this->put_mutex );
469 pthread_cond_broadcast( &this->put_cond );
470 pthread_mutex_unlock( &this->put_mutex );
473 pthread_join( this->ahead_thread, NULL );
476 pthread_mutex_destroy( &this->mutex );
478 // Destroy the condition
479 pthread_cond_destroy( &this->cond );
482 while ( mlt_deque_count( this->queue ) )
483 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
486 mlt_deque_close( this->queue );
490 void mlt_consumer_purge( mlt_consumer this )
494 pthread_mutex_lock( &this->mutex );
495 while ( mlt_deque_count( this->queue ) )
496 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
497 pthread_cond_broadcast( &this->cond );
498 pthread_mutex_unlock( &this->mutex );
502 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
505 mlt_frame frame = NULL;
507 // Get the properties
508 mlt_properties properties = mlt_consumer_properties( this );
510 // Check if the user has requested real time or not
511 if ( this->real_time )
515 // Is the read ahead running?
516 if ( this->ahead == 0 )
518 int buffer = mlt_properties_get_int( properties, "buffer" );
519 int prefill = mlt_properties_get_int( properties, "prefill" );
520 consumer_read_ahead_start( this );
522 size = prefill > 0 && prefill < buffer ? prefill : buffer;
525 // Get frame from queue
526 pthread_mutex_lock( &this->mutex );
527 while( this->ahead && mlt_deque_count( this->queue ) < size )
528 pthread_cond_wait( &this->cond, &this->mutex );
529 frame = mlt_deque_pop_front( this->queue );
530 pthread_cond_broadcast( &this->cond );
531 pthread_mutex_unlock( &this->mutex );
535 // Get the frame in non real time
536 frame = mlt_consumer_get_frame( this );
538 // This isn't true, but from the consumers perspective it is
539 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
545 /** Callback for the implementation to indicate a stopped condition.
548 void mlt_consumer_stopped( mlt_consumer this )
550 mlt_properties_set_int( mlt_consumer_properties( this ), "running", 0 );
551 mlt_events_fire( mlt_consumer_properties( this ), "consumer-stopped", NULL );
554 /** Stop the consumer.
557 int mlt_consumer_stop( mlt_consumer this )
560 mlt_properties properties = mlt_consumer_properties( this );
563 if ( this->stop != NULL )
566 // Check if the user has requested real time or not and stop if necessary
567 if ( mlt_properties_get_int( properties, "real_time" ) )
568 consumer_read_ahead_stop( this );
571 pthread_mutex_lock( &this->put_mutex );
572 pthread_cond_broadcast( &this->put_cond );
573 pthread_mutex_unlock( &this->put_mutex );
575 // Kill the test card
576 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
578 // Check and run a post command
579 if ( mlt_properties_get( properties, "post" ) )
580 system( mlt_properties_get( properties, "post" ) );
582 if ( this->put != NULL )
584 mlt_frame_close( this->put );
591 /** Determine if the consumer is stopped.
594 int mlt_consumer_is_stopped( mlt_consumer this )
596 // Check if the consumer is stopped
597 if ( this->is_stopped != NULL )
598 return this->is_stopped( this );
603 /** Close the consumer.
606 void mlt_consumer_close( mlt_consumer this )
608 if ( this != NULL && mlt_properties_dec_ref( mlt_consumer_properties( this ) ) <= 0 )
610 // Get the childs close function
611 void ( *consumer_close )( ) = this->close;
613 // Make sure it only gets called once
615 this->parent.close = NULL;
617 // Destroy the push mutex and condition
618 pthread_cond_broadcast( &this->put_cond );
619 pthread_mutex_destroy( &this->put_mutex );
620 pthread_cond_destroy( &this->put_cond );
622 // Call the childs close if available
623 if ( consumer_close != NULL )
624 consumer_close( this );
626 mlt_service_close( &this->parent );