]> git.sesse.net Git - mlt/blob - src/framework/mlt_consumer.c
b13c802b7f4b2e31381828b59d8e52743f964e75
[mlt] / src / framework / mlt_consumer.c
1 /**
2  * \file mlt_consumer.c
3  * \brief abstraction for all consumer services
4  * \see mlt_consumer_s
5  *
6  * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7  * \author Charles Yates <charles.yates@pandora.be>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "mlt_consumer.h"
25 #include "mlt_factory.h"
26 #include "mlt_producer.h"
27 #include "mlt_frame.h"
28 #include "mlt_profile.h"
29 #include "mlt_log.h"
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <sys/time.h>
35
36 /** Define this if you want an automatic deinterlace (if necessary) when the
37  * consumer's producer is not running at normal speed.
38  */
39 #undef DEINTERLACE_ON_NOT_NORMAL_SPEED
40
41 /** This is not the ideal place for this, but it is needed by VDPAU as well.
42  */
43 pthread_mutex_t mlt_sdl_mutex = PTHREAD_MUTEX_INITIALIZER;
44
45 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
46 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
47 static void mlt_consumer_property_changed( mlt_service owner, mlt_consumer this, char *name );
48 static void apply_profile_properties( mlt_consumer this, mlt_profile profile, mlt_properties properties );
49
50 /** Initialize a consumer service.
51  *
52  * \public \memberof mlt_consumer_s
53  * \param this the consumer to initialize
54  * \param child a pointer to the object for the subclass
55  * \param profile the \p mlt_profile_s to use (optional but recommended,
56  * uses the environment variable MLT if this is NULL)
57  * \return true if there was an error
58  */
59
60 int mlt_consumer_init( mlt_consumer this, void *child, mlt_profile profile )
61 {
62         int error = 0;
63         memset( this, 0, sizeof( struct mlt_consumer_s ) );
64         this->child = child;
65         error = mlt_service_init( &this->parent, this );
66         if ( error == 0 )
67         {
68                 // Get the properties from the service
69                 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
70
71                 // Apply profile to properties
72                 if ( profile == NULL )
73                 {
74                         // Normally the application creates the profile and controls its lifetime
75                         // This is the fallback exception handling
76                         profile = mlt_profile_init( NULL );
77                         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
78                         mlt_properties_set_data( properties, "_profile", profile, 0, (mlt_destructor)mlt_profile_close, NULL );
79                 }
80                 apply_profile_properties( this, profile, properties );
81
82                 // Default rescaler for all consumers
83                 mlt_properties_set( properties, "rescale", "bilinear" );
84
85                 // Default read ahead buffer size
86                 mlt_properties_set_int( properties, "buffer", 25 );
87
88                 // Default audio frequency and channels
89                 mlt_properties_set_int( properties, "frequency", 48000 );
90                 mlt_properties_set_int( properties, "channels", 2 );
91
92                 // Default of all consumers is real time
93                 mlt_properties_set_int( properties, "real_time", 1 );
94
95                 // Default to environment test card
96                 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
97
98                 // Hmm - default all consumers to yuv422 :-/
99                 this->format = mlt_image_yuv422;
100
101                 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
102                 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
103                 mlt_events_register( properties, "consumer-stopped", NULL );
104
105                 // Register a property-changed listener to handle the profile property -
106                 // subsequent properties can override the profile
107                 this->event_listener = mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_consumer_property_changed );
108
109                 // Create the push mutex and condition
110                 pthread_mutex_init( &this->put_mutex, NULL );
111                 pthread_cond_init( &this->put_cond, NULL );
112
113         }
114         return error;
115 }
116
117 /** Convert the profile into properties on the consumer.
118  *
119  * \private \memberof mlt_consumer_s
120  * \param this a consumer
121  * \param profile a profile
122  * \param properties a properties list (typically, the consumer's)
123  */
124
125 static void apply_profile_properties( mlt_consumer this, mlt_profile profile, mlt_properties properties )
126 {
127         mlt_event_block( this->event_listener );
128         mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
129         mlt_properties_set_int( properties, "frame_rate_num", profile->frame_rate_num );
130         mlt_properties_set_int( properties, "frame_rate_den", profile->frame_rate_den );
131         mlt_properties_set_int( properties, "width", profile->width );
132         mlt_properties_set_int( properties, "height", profile->height );
133         mlt_properties_set_int( properties, "progressive", profile->progressive );
134         mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile )  );
135         mlt_properties_set_int( properties, "sample_aspect_num", profile->sample_aspect_num );
136         mlt_properties_set_int( properties, "sample_aspect_den", profile->sample_aspect_den );
137         mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile )  );
138         mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
139         mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
140         mlt_properties_set_int( properties, "colorspace", profile->colorspace );
141         mlt_event_unblock( this->event_listener );
142 }
143
144 /** The property-changed event listener
145  *
146  * \private \memberof mlt_consumer_s
147  * \param owner the service a service (ignored)
148  * \param this the consumer
149  * \param name the name of the property that changed
150  */
151
152 static void mlt_consumer_property_changed( mlt_service owner, mlt_consumer this, char *name )
153 {
154         if ( !strcmp( name, "profile" ) )
155         {
156                 // Get the properies
157                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
158
159                 // Get the current profile
160                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
161
162                 // Load the new profile
163                 mlt_profile new_profile = mlt_profile_init( mlt_properties_get( properties, name ) );
164
165                 if ( new_profile )
166                 {
167                         // Copy the profile
168                         if ( profile != NULL )
169                         {
170                                 free( profile->description );
171                                 memcpy( profile, new_profile, sizeof( struct mlt_profile_s ) );
172                                 profile->description = strdup( new_profile->description );
173                                 mlt_profile_close( new_profile );
174                         }
175                         else
176                         {
177                                 profile = new_profile;
178                         }
179
180                         // Apply to properties
181                         apply_profile_properties( this, profile, properties );
182                 }
183         }
184         else if ( !strcmp( name, "frame_rate_num" ) )
185         {
186                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
187                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
188                 if ( profile )
189                 {
190                         profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
191                         mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
192                 }
193         }
194         else if ( !strcmp( name, "frame_rate_den" ) )
195         {
196                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
197                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
198                 if ( profile )
199                 {
200                         profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
201                         mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
202                 }
203         }
204         else if ( !strcmp( name, "width" ) )
205         {
206                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
207                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
208                 if ( profile )
209                         profile->width = mlt_properties_get_int( properties, "width" );
210         }
211         else if ( !strcmp( name, "height" ) )
212         {
213                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
214                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
215                 if ( profile )
216                         profile->height = mlt_properties_get_int( properties, "height" );
217         }
218         else if ( !strcmp( name, "progressive" ) )
219         {
220                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
221                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
222                 if ( profile )
223                         profile->progressive = mlt_properties_get_int( properties, "progressive" );
224         }
225         else if ( !strcmp( name, "sample_aspect_num" ) )
226         {
227                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
228                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
229                 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
230                 if ( profile )
231                         mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile )  );
232         }
233         else if ( !strcmp( name, "sample_aspect_den" ) )
234         {
235                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
236                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
237                 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
238                 if ( profile )
239                         mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile )  );
240         }
241         else if ( !strcmp( name, "display_aspect_num" ) )
242         {
243                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
244                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
245                 if ( profile )
246                 {
247                         profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
248                         mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile )  );
249                 }
250         }
251         else if ( !strcmp( name, "display_aspect_den" ) )
252         {
253                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
254                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
255                 if ( profile )
256                 {
257                         profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
258                         mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile )  );
259                 }
260         }
261         else if ( !strcmp( name, "colorspace" ) )
262         {
263                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
264                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
265                 if ( profile )
266                         profile->colorspace = mlt_properties_get_int( properties, "colorspace" );
267         }
268 }
269
270 /** The transmitter for the consumer-frame-show event
271  *
272  * Invokes the listener.
273  *
274  * \private \memberof mlt_consumer_s
275  * \param listener a function pointer that will be invoked
276  * \param owner  a properties list that will be passed to \p listener
277  * \param this  a service that will be passed to \p listener
278  * \param args an array of pointers - the first entry is passed as a string to \p listener
279  */
280
281 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
282 {
283         if ( listener != NULL )
284                 listener( owner, this, ( mlt_frame )args[ 0 ] );
285 }
286
287 /** The transmitter for the consumer-frame-render event
288  *
289  * Invokes the listener.
290  *
291  * \private \memberof mlt_consumer_s
292  * \param listener a function pointer that will be invoked
293  * \param owner  a properties list that will be passed to \p listener
294  * \param this  a service that will be passed to \p listener
295  * \param args an array of pointers - the first entry is passed as a string to \p listener
296  */
297
298 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
299 {
300         if ( listener != NULL )
301                 listener( owner, this, ( mlt_frame )args[ 0 ] );
302 }
303
304 /** Create a new consumer.
305  *
306  * \public \memberof mlt_consumer_s
307  * \param profile a profile (optional, but recommended)
308  * \return a new consumer
309  */
310
311 mlt_consumer mlt_consumer_new( mlt_profile profile )
312 {
313         // Create the memory for the structure
314         mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
315
316         // Initialise it
317         if ( this != NULL )
318                 mlt_consumer_init( this, NULL, profile );
319
320         // Return it
321         return this;
322 }
323
324 /** Get the parent service object.
325  *
326  * \public \memberof mlt_consumer_s
327  * \param this a consumer
328  * \return the parent service class
329  * \see MLT_CONSUMER_SERVICE
330  */
331
332 mlt_service mlt_consumer_service( mlt_consumer this )
333 {
334         return this != NULL ? &this->parent : NULL;
335 }
336
337 /** Get the consumer properties.
338  *
339  * \public \memberof mlt_consumer_s
340  * \param this a consumer
341  * \return the consumer's properties list
342  * \see MLT_CONSUMER_PROPERTIES
343  */
344
345 mlt_properties mlt_consumer_properties( mlt_consumer this )
346 {
347         return this != NULL ? MLT_SERVICE_PROPERTIES( &this->parent ) : NULL;
348 }
349
350 /** Connect the consumer to the producer.
351  *
352  * \public \memberof mlt_consumer_s
353  * \param this a consumer
354  * \param producer a producer
355  * \return > 0 warning, == 0 success, < 0 serious error,
356  *         1 = this service does not accept input,
357  *         2 = the producer is invalid,
358  *         3 = the producer is already registered with this consumer
359  */
360
361 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
362 {
363         return mlt_service_connect_producer( &this->parent, producer, 0 );
364 }
365
366 /** Start the consumer.
367  *
368  * \public \memberof mlt_consumer_s
369  * \param this a consumer
370  * \return true if there was an error
371  */
372
373 int mlt_consumer_start( mlt_consumer this )
374 {
375         // Stop listening to the property-changed event
376         mlt_event_block( this->event_listener );
377
378         // Get the properies
379         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
380
381         // Determine if there's a test card producer
382         char *test_card = mlt_properties_get( properties, "test_card" );
383
384         // Just to make sure nothing is hanging around...
385         mlt_frame_close( this->put );
386         this->put = NULL;
387         this->put_active = 1;
388
389         // Deal with it now.
390         if ( test_card != NULL )
391         {
392                 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
393                 {
394                         // Create a test card producer
395                         mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
396                         mlt_producer producer = mlt_factory_producer( profile, NULL, test_card );
397
398                         // Do we have a producer
399                         if ( producer != NULL )
400                         {
401                                 // Test card should loop I guess...
402                                 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
403                                 //mlt_producer_set_speed( producer, 0 );
404                                 //mlt_producer_set_in_and_out( producer, 0, 0 );
405
406                                 // Set the test card on the consumer
407                                 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
408                         }
409                 }
410         }
411         else
412         {
413                 // Allow the hash table to speed things up
414                 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
415         }
416
417         // Set the frame duration in microseconds for the frame-dropping heuristic
418         int frame_duration = 1000000 / mlt_properties_get_int( properties, "frame_rate_num" ) *
419                         mlt_properties_get_int( properties, "frame_rate_den" );
420         mlt_properties_set_int( properties, "frame_duration", frame_duration );
421
422         // Check and run an ante command
423         if ( mlt_properties_get( properties, "ante" ) )
424                 if ( system( mlt_properties_get( properties, "ante" ) ) == -1 )
425                         mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "ante" ) );
426
427         // Set the real_time preference
428         this->real_time = mlt_properties_get_int( properties, "real_time" );
429
430         // Start the service
431         if ( this->start != NULL )
432                 return this->start( this );
433
434         return 0;
435 }
436
437 /** An alternative method to feed frames into the consumer.
438  *
439  * Only valid if the consumer itself is not connected.
440  *
441  * \public \memberof mlt_consumer_s
442  * \param this a consumer
443  * \param frame a frame
444  * \return true (ignore this for now)
445  */
446
447 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
448 {
449         int error = 1;
450
451         // Get the service assoicated to the consumer
452         mlt_service service = MLT_CONSUMER_SERVICE( this );
453
454         if ( mlt_service_producer( service ) == NULL )
455         {
456                 struct timeval now;
457                 struct timespec tm;
458                 pthread_mutex_lock( &this->put_mutex );
459                 while ( this->put_active && this->put != NULL )
460                 {
461                         gettimeofday( &now, NULL );
462                         tm.tv_sec = now.tv_sec + 1;
463                         tm.tv_nsec = now.tv_usec * 1000;
464                         pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
465                 }
466                 if ( this->put_active && this->put == NULL )
467                         this->put = frame;
468                 else
469                         mlt_frame_close( frame );
470                 pthread_cond_broadcast( &this->put_cond );
471                 pthread_mutex_unlock( &this->put_mutex );
472         }
473         else
474         {
475                 mlt_frame_close( frame );
476         }
477
478         return error;
479 }
480
481 /** Protected method for consumer to get frames from connected service
482  *
483  * \public \memberof mlt_consumer_s
484  * \param this a consumer
485  * \return a frame
486  */
487
488 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
489 {
490         // Frame to return
491         mlt_frame frame = NULL;
492
493         // Get the service assoicated to the consumer
494         mlt_service service = MLT_CONSUMER_SERVICE( this );
495
496         // Get the consumer properties
497         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
498
499         // Get the frame
500         if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
501         {
502                 struct timeval now;
503                 struct timespec tm;
504                 pthread_mutex_lock( &this->put_mutex );
505                 while ( this->put_active && this->put == NULL )
506                 {
507                         gettimeofday( &now, NULL );
508                         tm.tv_sec = now.tv_sec + 1;
509                         tm.tv_nsec = now.tv_usec * 1000;
510                         pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
511                 }
512                 frame = this->put;
513                 this->put = NULL;
514                 pthread_cond_broadcast( &this->put_cond );
515                 pthread_mutex_unlock( &this->put_mutex );
516                 if ( frame != NULL )
517                         mlt_service_apply_filters( service, frame, 0 );
518         }
519         else if ( mlt_service_producer( service ) != NULL )
520         {
521                 mlt_service_get_frame( service, &frame, 0 );
522         }
523         else
524         {
525                 frame = mlt_frame_init( service );
526         }
527
528         if ( frame != NULL )
529         {
530                 // Get the frame properties
531                 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
532
533                 // Get the test card producer
534                 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
535
536                 // Attach the test frame producer to it.
537                 if ( test_card != NULL )
538                         mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
539
540                 // Attach the rescale property
541                 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
542
543                 // Aspect ratio and other jiggery pokery
544                 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
545                 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
546                 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
547         }
548
549         // Return the frame
550         return frame;
551 }
552
553 /** Compute the time difference between now and a time value.
554  *
555  * \private \memberof mlt_consumer_s
556  * \param time1 a time value to be compared against now
557  * \return the difference in microseconds
558  */
559
560 static inline long time_difference( struct timeval *time1 )
561 {
562         struct timeval time2;
563         time2.tv_sec = time1->tv_sec;
564         time2.tv_usec = time1->tv_usec;
565         gettimeofday( time1, NULL );
566         return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
567 }
568
569 /** The thread procedure for asynchronously pulling frames through the service
570  * network connected to a consumer.
571  *
572  * \private \memberof mlt_consumer_s
573  * \param arg a consumer
574  */
575
576 static void *consumer_read_ahead_thread( void *arg )
577 {
578         // The argument is the consumer
579         mlt_consumer this = arg;
580
581         // Get the properties of the consumer
582         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
583
584         // Get the width and height
585         int width = mlt_properties_get_int( properties, "width" );
586         int height = mlt_properties_get_int( properties, "height" );
587
588         // See if video is turned off
589         int video_off = mlt_properties_get_int( properties, "video_off" );
590         int preview_off = mlt_properties_get_int( properties, "preview_off" );
591         int preview_format = mlt_properties_get_int( properties, "preview_format" );
592
593         // Get the audio settings
594         mlt_audio_format afmt = mlt_audio_s16;
595         int counter = 0;
596         double fps = mlt_properties_get_double( properties, "fps" );
597         int channels = mlt_properties_get_int( properties, "channels" );
598         int frequency = mlt_properties_get_int( properties, "frequency" );
599         int samples = 0;
600         void *audio = NULL;
601
602         // See if audio is turned off
603         int audio_off = mlt_properties_get_int( properties, "audio_off" );
604
605         // Get the maximum size of the buffer
606         int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
607
608         // General frame variable
609         mlt_frame frame = NULL;
610         uint8_t *image = NULL;
611
612         // Time structures
613         struct timeval ante;
614
615         // Average time for get_frame and get_image
616         int count = 1;
617         int skipped = 0;
618         int64_t time_wait = 0;
619         int64_t time_frame = 0;
620         int64_t time_process = 0;
621         int skip_next = 0;
622         mlt_service lock_object = NULL;
623
624         if ( preview_off && preview_format != 0 )
625                 this->format = preview_format;
626
627         // Get the first frame
628         frame = mlt_consumer_get_frame( this );
629
630         // Get the lock object
631         lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
632
633         // Lock it
634         if ( lock_object ) mlt_service_lock( lock_object );
635
636         // Get the image of the first frame
637         if ( !video_off )
638         {
639                 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
640                 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
641         }
642
643         if ( !audio_off )
644         {
645                 samples = mlt_sample_calculator( fps, frequency, counter++ );
646                 mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
647         }
648
649         // Unlock the lock object
650         if ( lock_object ) mlt_service_unlock( lock_object );
651
652         // Mark as rendered
653         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
654
655         // Get the starting time (can ignore the times above)
656         gettimeofday( &ante, NULL );
657
658         // Continue to read ahead
659         while ( this->ahead )
660         {
661                 // Fetch width/height again
662                 width = mlt_properties_get_int( properties, "width" );
663                 height = mlt_properties_get_int( properties, "height" );
664
665                 // Put the current frame into the queue
666                 pthread_mutex_lock( &this->mutex );
667                 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
668                         pthread_cond_wait( &this->cond, &this->mutex );
669                 mlt_deque_push_back( this->queue, frame );
670                 pthread_cond_broadcast( &this->cond );
671                 pthread_mutex_unlock( &this->mutex );
672
673                 time_wait += time_difference( &ante );
674
675                 // Get the next frame
676                 frame = mlt_consumer_get_frame( this );
677                 time_frame += time_difference( &ante );
678
679                 // If there's no frame, we're probably stopped...
680                 if ( frame == NULL )
681                         continue;
682
683                 // Attempt to fetch the lock object
684                 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
685
686                 // Increment the count
687                 count ++;
688
689                 // Lock if there's a lock object
690                 if ( lock_object ) mlt_service_lock( lock_object );
691
692                 // All non normal playback frames should be shown
693                 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
694                 {
695 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
696                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
697 #endif
698                         skipped = 0;
699                         time_frame = 0;
700                         time_process = 0;
701                         time_wait = 0;
702                         count = 1;
703                         skip_next = 0;
704                 }
705
706                 // Get the image
707                 if ( !skip_next || this->real_time == -1 )
708                 {
709                         // Get the image, mark as rendered and time it
710                         if ( !video_off )
711                         {
712                                 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
713                                 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
714                         }
715                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
716                 }
717                 else
718                 {
719                         // Increment the number of sequentially skipped frames
720                         skipped ++;
721                         skip_next = 0;
722
723                         // If we've reached an unacceptable level, reset everything
724                         if ( skipped > 5 )
725                         {
726                                 skipped = 0;
727                                 time_frame = 0;
728                                 time_process = 0;
729                                 time_wait = 0;
730                                 count = 1;
731                         }
732                 }
733
734                 // Always process audio
735                 if ( !audio_off )
736                 {
737                         samples = mlt_sample_calculator( fps, frequency, counter++ );
738                         mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
739                 }
740
741                 // Increment the time take for this frame
742                 time_process += time_difference( &ante );
743
744                 // Determine if the next frame should be skipped
745                 if ( mlt_deque_count( this->queue ) <= 5 )
746                 {
747                         int frame_duration = mlt_properties_get_int( properties, "frame_duration" );
748                         if ( ( ( time_wait + time_frame + time_process ) / count ) > frame_duration )
749                                 skip_next = 1;
750                 }
751
752                 // Unlock if there's a lock object
753                 if ( lock_object ) mlt_service_unlock( lock_object );
754         }
755
756         // Remove the last frame
757         mlt_frame_close( frame );
758
759         return NULL;
760 }
761
762 /** Start the read/render thread.
763  *
764  * \private \memberof mlt_consumer_s
765  * \param this a consumer
766  */
767
768 static void consumer_read_ahead_start( mlt_consumer this )
769 {
770         // We're running now
771         this->ahead = 1;
772
773         // Create the frame queue
774         this->queue = mlt_deque_init( );
775
776         // Create the mutex
777         pthread_mutex_init( &this->mutex, NULL );
778
779         // Create the condition
780         pthread_cond_init( &this->cond, NULL );
781
782         // Create the read ahead
783         if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "priority" ) )
784         {
785                 struct sched_param priority;
786                 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this ), "priority" );
787                 pthread_attr_t thread_attributes;
788                 pthread_attr_init( &thread_attributes );
789                 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
790                 pthread_attr_setschedparam( &thread_attributes, &priority );
791                 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
792                 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
793                 if ( pthread_create( &this->ahead_thread, &thread_attributes, consumer_read_ahead_thread, this ) < 0 )
794                         pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
795                 pthread_attr_destroy( &thread_attributes );
796         }
797         else
798         {
799                 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
800         }
801 }
802
803 /** Stop the read/render thread.
804  *
805  * \private \memberof mlt_consumer_s
806  * \param this a consumer
807  */
808
809 static void consumer_read_ahead_stop( mlt_consumer this )
810 {
811         // Make sure we're running
812         if ( this->ahead )
813         {
814                 // Inform thread to stop
815                 this->ahead = 0;
816
817                 // Broadcast to the condition in case it's waiting
818                 pthread_mutex_lock( &this->mutex );
819                 pthread_cond_broadcast( &this->cond );
820                 pthread_mutex_unlock( &this->mutex );
821
822                 // Broadcast to the put condition in case it's waiting
823                 pthread_mutex_lock( &this->put_mutex );
824                 pthread_cond_broadcast( &this->put_cond );
825                 pthread_mutex_unlock( &this->put_mutex );
826
827                 // Join the thread
828                 pthread_join( this->ahead_thread, NULL );
829
830                 // Destroy the mutex
831                 pthread_mutex_destroy( &this->mutex );
832
833                 // Destroy the condition
834                 pthread_cond_destroy( &this->cond );
835
836                 // Wipe the queue
837                 while ( mlt_deque_count( this->queue ) )
838                         mlt_frame_close( mlt_deque_pop_back( this->queue ) );
839
840                 // Close the queue
841                 mlt_deque_close( this->queue );
842         }
843 }
844
845 /** Flush the read/render thread's buffer.
846  *
847  * \public \memberof mlt_consumer_s
848  * \param this a consumer
849  */
850
851 void mlt_consumer_purge( mlt_consumer this )
852 {
853         if ( this->ahead )
854         {
855                 pthread_mutex_lock( &this->mutex );
856                 while ( mlt_deque_count( this->queue ) )
857                         mlt_frame_close( mlt_deque_pop_back( this->queue ) );
858                 pthread_cond_broadcast( &this->cond );
859                 pthread_mutex_unlock( &this->mutex );
860         }
861 }
862
863 /** Get the next frame from the producer connected to a consumer.
864  *
865  * Typically, one uses this instead of \p mlt_consumer_get_frame to make
866  * the asynchronous/real-time behavior configurable at runtime.
867  * You should close the frame returned from this when you are done with it.
868  *
869  * \public \memberof mlt_consumer_s
870  * \param this a consumer
871  * \return a frame
872  */
873
874 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
875 {
876         // Frame to return
877         mlt_frame frame = NULL;
878
879         // Get the properties
880         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
881
882         // Check if the user has requested real time or not
883         if ( this->real_time )
884         {
885                 int size = 1;
886
887                 // Is the read ahead running?
888                 if ( this->ahead == 0 )
889                 {
890                         int buffer = mlt_properties_get_int( properties, "buffer" );
891                         int prefill = mlt_properties_get_int( properties, "prefill" );
892                         consumer_read_ahead_start( this );
893                         if ( buffer > 1 )
894                                 size = prefill > 0 && prefill < buffer ? prefill : buffer;
895                 }
896
897                 // Get frame from queue
898                 pthread_mutex_lock( &this->mutex );
899                 while( this->ahead && mlt_deque_count( this->queue ) < size )
900                         pthread_cond_wait( &this->cond, &this->mutex );
901                 frame = mlt_deque_pop_front( this->queue );
902                 pthread_cond_broadcast( &this->cond );
903                 pthread_mutex_unlock( &this->mutex );
904         }
905         else
906         {
907                 // Get the frame in non real time
908                 frame = mlt_consumer_get_frame( this );
909
910                 // This isn't true, but from the consumers perspective it is
911                 if ( frame != NULL )
912                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
913         }
914
915         return frame;
916 }
917
918 /** Callback for the implementation to indicate a stopped condition.
919  *
920  * \public \memberof mlt_consumer_s
921  * \param this a consumer
922  */
923
924 void mlt_consumer_stopped( mlt_consumer this )
925 {
926         mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
927         mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
928         mlt_event_unblock( this->event_listener );
929 }
930
931 /** Stop the consumer.
932  *
933  * \public \memberof mlt_consumer_s
934  * \param this a consumer
935  * \return true if there was an error
936  */
937
938 int mlt_consumer_stop( mlt_consumer this )
939 {
940         // Get the properies
941         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
942
943         // Just in case...
944         mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping put waiting\n" );
945         pthread_mutex_lock( &this->put_mutex );
946         this->put_active = 0;
947         pthread_cond_broadcast( &this->put_cond );
948         pthread_mutex_unlock( &this->put_mutex );
949
950         // Stop the consumer
951         mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping consumer\n" );
952         if ( this->stop != NULL )
953                 this->stop( this );
954
955         // Check if the user has requested real time or not and stop if necessary
956         mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping read_ahead\n" );
957         if ( mlt_properties_get_int( properties, "real_time" ) )
958                 consumer_read_ahead_stop( this );
959
960         // Kill the test card
961         mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
962
963         // Check and run a post command
964         if ( mlt_properties_get( properties, "post" ) )
965                 if (system( mlt_properties_get( properties, "post" ) ) == -1 )
966                         mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "post" ) );
967
968         mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopped\n" );
969
970         return 0;
971 }
972
973 /** Determine if the consumer is stopped.
974  *
975  * \public \memberof mlt_consumer_s
976  * \param this a consumer
977  * \return true if the consumer is stopped
978  */
979
980 int mlt_consumer_is_stopped( mlt_consumer this )
981 {
982         // Check if the consumer is stopped
983         if ( this->is_stopped != NULL )
984                 return this->is_stopped( this );
985
986         return 0;
987 }
988
989 /** Close and destroy the consumer.
990  *
991  * \public \memberof mlt_consumer_s
992  * \param this a consumer
993  */
994
995 void mlt_consumer_close( mlt_consumer this )
996 {
997         if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
998         {
999                 // Get the childs close function
1000                 void ( *consumer_close )( ) = this->close;
1001
1002                 if ( consumer_close )
1003                 {
1004                         // Just in case...
1005                         //mlt_consumer_stop( this );
1006
1007                         this->close = NULL;
1008                         consumer_close( this );
1009                 }
1010                 else
1011                 {
1012                         // Make sure it only gets called once
1013                         this->parent.close = NULL;
1014
1015                         // Destroy the push mutex and condition
1016                         pthread_mutex_destroy( &this->put_mutex );
1017                         pthread_cond_destroy( &this->put_cond );
1018
1019                         mlt_service_close( &this->parent );
1020                 }
1021         }
1022 }