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" );
157 // Just to make sure nothing is hanging around...
158 mlt_frame_close( this->put );
160 this->put_active = 1;
163 if ( test_card != NULL )
165 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
167 // Create a test card producer
168 mlt_producer producer = mlt_factory_producer( NULL, test_card );
170 // Do we have a producer
171 if ( producer != NULL )
173 // Test card should loop I guess...
174 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "pause" );
175 mlt_producer_set_speed( producer, 0 );
176 mlt_producer_set_in_and_out( producer, 0, 0 );
178 // Set the test card on the consumer
179 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
185 // Allow the hash table to speed things up
186 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
189 // Check and run an ante command
190 if ( mlt_properties_get( properties, "ante" ) )
191 system( mlt_properties_get( properties, "ante" ) );
193 // Set the real_time preference
194 this->real_time = mlt_properties_get_int( properties, "real_time" );
197 if ( this->start != NULL )
198 return this->start( this );
203 /** An alternative method to feed frames into the consumer - only valid if
204 the consumer itself is not connected.
207 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
211 // Get the service assoicated to the consumer
212 mlt_service service = MLT_CONSUMER_SERVICE( this );
214 if ( mlt_service_producer( service ) == NULL )
218 pthread_mutex_lock( &this->put_mutex );
219 while ( this->put_active && this->put != NULL )
221 gettimeofday( &now, NULL );
222 tm.tv_sec = now.tv_sec + 1;
223 tm.tv_nsec = now.tv_usec * 1000;
224 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
226 if ( this->put_active && this->put == NULL )
229 mlt_frame_close( frame );
230 pthread_cond_broadcast( &this->put_cond );
231 pthread_mutex_unlock( &this->put_mutex );
235 mlt_frame_close( frame );
241 /** Protected method for consumer to get frames from connected service
244 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
247 mlt_frame frame = NULL;
249 // Get the service assoicated to the consumer
250 mlt_service service = MLT_CONSUMER_SERVICE( this );
252 // Get the consumer properties
253 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
256 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
260 pthread_mutex_lock( &this->put_mutex );
261 while ( this->put_active && this->put == NULL )
263 gettimeofday( &now, NULL );
264 tm.tv_sec = now.tv_sec + 1;
265 tm.tv_nsec = now.tv_usec * 1000;
266 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
270 pthread_cond_broadcast( &this->put_cond );
271 pthread_mutex_unlock( &this->put_mutex );
273 mlt_service_apply_filters( service, frame, 0 );
275 else if ( mlt_service_producer( service ) != NULL )
277 mlt_service_get_frame( service, &frame, 0 );
282 // Get the frame properties
283 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
285 // Get the test card producer
286 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
288 // Attach the test frame producer to it.
289 if ( test_card != NULL )
290 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
292 // Attach the rescale property
293 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
295 // Aspect ratio and other jiggery pokery
296 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
297 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
304 static inline long time_difference( struct timeval *time1 )
306 struct timeval time2;
307 time2.tv_sec = time1->tv_sec;
308 time2.tv_usec = time1->tv_usec;
309 gettimeofday( time1, NULL );
310 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
313 static void *consumer_read_ahead_thread( void *arg )
315 // The argument is the consumer
316 mlt_consumer this = arg;
318 // Get the properties of the consumer
319 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
321 // Get the width and height
322 int width = mlt_properties_get_int( properties, "width" );
323 int height = mlt_properties_get_int( properties, "height" );
325 // See if video is turned off
326 int video_off = mlt_properties_get_int( properties, "video_off" );
328 // Get the audio settings
329 mlt_audio_format afmt = mlt_audio_pcm;
331 double fps = mlt_properties_get_double( properties, "fps" );
332 int channels = mlt_properties_get_int( properties, "channels" );
333 int frequency = mlt_properties_get_int( properties, "frequency" );
337 // See if audio is turned off
338 int audio_off = mlt_properties_get_int( properties, "audio_off" );
340 // Get the maximum size of the buffer
341 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
343 // General frame variable
344 mlt_frame frame = NULL;
345 uint8_t *image = NULL;
350 // Average time for get_frame and get_image
353 int64_t time_wait = 0;
354 int64_t time_frame = 0;
355 int64_t time_process = 0;
357 mlt_service lock_object = NULL;
359 // Get the first frame
360 frame = mlt_consumer_get_frame( this );
362 // Get the lock object
363 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
366 if ( lock_object ) mlt_service_lock( lock_object );
368 // Get the image of the first frame
370 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
374 samples = mlt_sample_calculator( fps, frequency, counter++ );
375 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
378 // Unlock the lock object
379 if ( lock_object ) mlt_service_unlock( lock_object );
382 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
384 // Get the starting time (can ignore the times above)
385 gettimeofday( &ante, NULL );
387 // Continue to read ahead
388 while ( this->ahead )
390 // Put the current frame into the queue
391 pthread_mutex_lock( &this->mutex );
392 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
393 pthread_cond_wait( &this->cond, &this->mutex );
394 mlt_deque_push_back( this->queue, frame );
395 pthread_cond_broadcast( &this->cond );
396 pthread_mutex_unlock( &this->mutex );
398 time_wait += time_difference( &ante );
400 // Get the next frame
401 frame = mlt_consumer_get_frame( this );
402 time_frame += time_difference( &ante );
404 // If there's no frame, we're probably stopped...
408 // Attempt to fetch the lock object
409 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
411 // Increment the count
414 // Lock if there's a lock object
415 if ( lock_object ) mlt_service_lock( lock_object );
417 // All non normal playback frames should be shown
418 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
431 // Get the image, mark as rendered and time it
433 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
434 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
438 // Increment the number of sequentially skipped frames
442 // If we've reached an unacceptable level, reset everything
453 // Always process audio
456 samples = mlt_sample_calculator( fps, frequency, counter++ );
457 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
460 // Increment the time take for this frame
461 time_process += time_difference( &ante );
463 // Determine if the next frame should be skipped
464 if ( mlt_deque_count( this->queue ) <= 5 && ( ( time_wait + time_frame + time_process ) / count ) > 40000 )
467 // Unlock if there's a lock object
468 if ( lock_object ) mlt_service_unlock( lock_object );
471 // Remove the last frame
472 mlt_frame_close( frame );
477 static void consumer_read_ahead_start( mlt_consumer this )
479 pthread_attr_t thread_attributes;
484 // Create the frame queue
485 this->queue = mlt_deque_init( );
488 pthread_mutex_init( &this->mutex, NULL );
490 // Create the condition
491 pthread_cond_init( &this->cond, NULL );
493 // Inherit the scheduling priority
494 pthread_attr_init( &thread_attributes );
495 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
497 // Create the read ahead
498 pthread_create( &this->ahead_thread, &thread_attributes, consumer_read_ahead_thread, this );
501 static void consumer_read_ahead_stop( mlt_consumer this )
503 // Make sure we're running
506 // Inform thread to stop
509 // Broadcast to the condition in case it's waiting
510 pthread_mutex_lock( &this->mutex );
511 pthread_cond_broadcast( &this->cond );
512 pthread_mutex_unlock( &this->mutex );
514 // Broadcast to the put condition in case it's waiting
515 pthread_mutex_lock( &this->put_mutex );
516 pthread_cond_broadcast( &this->put_cond );
517 pthread_mutex_unlock( &this->put_mutex );
520 pthread_join( this->ahead_thread, NULL );
523 pthread_mutex_destroy( &this->mutex );
525 // Destroy the condition
526 pthread_cond_destroy( &this->cond );
529 while ( mlt_deque_count( this->queue ) )
530 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
533 mlt_deque_close( this->queue );
537 void mlt_consumer_purge( mlt_consumer this )
541 pthread_mutex_lock( &this->mutex );
542 while ( mlt_deque_count( this->queue ) )
543 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
544 pthread_cond_broadcast( &this->cond );
545 pthread_mutex_unlock( &this->mutex );
549 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
552 mlt_frame frame = NULL;
554 // Get the properties
555 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
557 // Check if the user has requested real time or not
558 if ( this->real_time )
562 // Is the read ahead running?
563 if ( this->ahead == 0 )
565 int buffer = mlt_properties_get_int( properties, "buffer" );
566 int prefill = mlt_properties_get_int( properties, "prefill" );
567 consumer_read_ahead_start( this );
569 size = prefill > 0 && prefill < buffer ? prefill : buffer;
572 // Get frame from queue
573 pthread_mutex_lock( &this->mutex );
574 while( this->ahead && mlt_deque_count( this->queue ) < size )
575 pthread_cond_wait( &this->cond, &this->mutex );
576 frame = mlt_deque_pop_front( this->queue );
577 pthread_cond_broadcast( &this->cond );
578 pthread_mutex_unlock( &this->mutex );
582 // Get the frame in non real time
583 frame = mlt_consumer_get_frame( this );
585 // This isn't true, but from the consumers perspective it is
587 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
593 /** Callback for the implementation to indicate a stopped condition.
596 void mlt_consumer_stopped( mlt_consumer this )
598 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
599 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
602 /** Stop the consumer.
605 int mlt_consumer_stop( mlt_consumer this )
608 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
609 char *debug = mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "debug" );
612 if ( debug ) fprintf( stderr, "%s: stopping put waiting\n", debug );
613 pthread_mutex_lock( &this->put_mutex );
614 this->put_active = 0;
615 pthread_cond_broadcast( &this->put_cond );
616 pthread_mutex_unlock( &this->put_mutex );
619 if ( debug ) fprintf( stderr, "%s: stopping consumer\n", debug );
620 if ( this->stop != NULL )
623 // Check if the user has requested real time or not and stop if necessary
624 if ( debug ) fprintf( stderr, "%s: stopping read_ahead\n", debug );
625 if ( mlt_properties_get_int( properties, "real_time" ) )
626 consumer_read_ahead_stop( this );
628 // Kill the test card
629 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
631 // Check and run a post command
632 if ( mlt_properties_get( properties, "post" ) )
633 system( mlt_properties_get( properties, "post" ) );
635 if ( debug ) fprintf( stderr, "%s: stopped\n", debug );
640 /** Determine if the consumer is stopped.
643 int mlt_consumer_is_stopped( mlt_consumer this )
645 // Check if the consumer is stopped
646 if ( this->is_stopped != NULL )
647 return this->is_stopped( this );
652 /** Close the consumer.
655 void mlt_consumer_close( mlt_consumer this )
657 if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
659 // Get the childs close function
660 void ( *consumer_close )( ) = this->close;
662 if ( consumer_close )
665 //mlt_consumer_stop( this );
668 consumer_close( this );
672 // Make sure it only gets called once
673 this->parent.close = NULL;
675 // Destroy the push mutex and condition
676 pthread_mutex_destroy( &this->put_mutex );
677 pthread_cond_destroy( &this->put_cond );
679 mlt_service_close( &this->parent );