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