]> git.sesse.net Git - mlt/blob - src/framework/mlt_consumer.c
5b48e6d330c4593bf5606c0f934a44dbd1a99555
[mlt] / src / framework / mlt_consumer.c
1 /*
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>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "config.h"
22 #include "mlt_consumer.h"
23 #include "mlt_factory.h"
24 #include "mlt_producer.h"
25 #include "mlt_frame.h"
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <sys/time.h>
30
31 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
32 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
33
34 /** Public final methods
35 */
36
37 int mlt_consumer_init( mlt_consumer this, void *child )
38 {
39         int error = 0;
40         memset( this, 0, sizeof( struct mlt_consumer_s ) );
41         this->child = child;
42         error = mlt_service_init( &this->parent, this );
43         if ( error == 0 )
44         {
45                 // Get the properties from the service
46                 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
47
48                 // Get the normalisation preference
49                 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
50
51                 // Deal with normalisation
52                 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
53                 {
54                         mlt_properties_set( properties, "normalisation", "PAL" );
55                         mlt_properties_set_double( properties, "fps", 25.0 );
56                         mlt_properties_set_int( properties, "frame_rate_num", 25 );
57                         mlt_properties_set_int( properties, "frame_rate_den", 1 );
58                         mlt_properties_set_int( properties, "width", 720 );
59                         mlt_properties_set_int( properties, "height", 576 );
60                         mlt_properties_set_int( properties, "progressive", 0 );
61                         mlt_properties_set_double( properties, "aspect_ratio", 59.0 / 54.0 );
62                         mlt_properties_set_int( properties, "aspect_ratio_num", 59 );
63                         mlt_properties_set_int( properties, "aspect_ratio_den", 54 );
64                         mlt_properties_set_double( properties, "display_ratio", 4.0 / 3.0 );
65                         mlt_properties_set_int( properties, "display_ratio_num", 4 );
66                         mlt_properties_set_int( properties, "display_ratio_den", 3 );
67                 }
68                 else
69                 {
70                         mlt_properties_set( properties, "normalisation", "NTSC" );
71                         mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
72                         mlt_properties_set_int( properties, "frame_rate_num", 30000 );
73                         mlt_properties_set_int( properties, "frame_rate_den", 1001 );
74                         mlt_properties_set_int( properties, "width", 720 );
75                         mlt_properties_set_int( properties, "height", 480 );
76                         mlt_properties_set_int( properties, "progressive", 0 );
77                         mlt_properties_set_double( properties, "aspect_ratio", 10.0 / 11.0 );
78                         mlt_properties_set_int( properties, "aspect_ratio_num", 10 );
79                         mlt_properties_set_int( properties, "aspect_ratio_den", 11 );
80                         mlt_properties_set_double( properties, "display_ratio", 4.0 / 3.0 );
81                         mlt_properties_set_int( properties, "display_ratio_num", 4 );
82                         mlt_properties_set_int( properties, "display_ratio_den", 3 );
83                 }
84
85                 // Default rescaler for all consumers
86                 mlt_properties_set( properties, "rescale", "bilinear" );
87
88                 // Default read ahead buffer size
89                 mlt_properties_set_int( properties, "buffer", 25 );
90
91                 // Default audio frequency and channels
92                 mlt_properties_set_int( properties, "frequency", 48000 );
93                 mlt_properties_set_int( properties, "channels", 2 );
94
95                 // Default of all consumers is real time
96                 mlt_properties_set_int( properties, "real_time", 1 );
97
98                 // Default to environment test card
99                 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
100
101                 // Hmm - default all consumers to yuv422 :-/
102                 this->format = mlt_image_yuv422;
103
104                 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
105                 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
106                 mlt_events_register( properties, "consumer-stopped", NULL );
107
108                 // Create the push mutex and condition
109                 pthread_mutex_init( &this->put_mutex, NULL );
110                 pthread_cond_init( &this->put_cond, NULL );
111
112         }
113         return error;
114 }
115
116 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
117 {
118         if ( listener != NULL )
119                 listener( owner, this, ( mlt_frame )args[ 0 ] );
120 }
121
122 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
123 {
124         if ( listener != NULL )
125                 listener( owner, this, ( mlt_frame )args[ 0 ] );
126 }
127
128 /** Create a new consumer.
129 */
130
131 mlt_consumer mlt_consumer_new( )
132 {
133         // Create the memory for the structure
134         mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
135
136         // Initialise it
137         if ( this != NULL )
138                 mlt_consumer_init( this, NULL );
139
140         // Return it
141         return this;
142 }
143
144 /** Get the parent service object.
145 */
146
147 mlt_service mlt_consumer_service( mlt_consumer this )
148 {
149         return this != NULL ? &this->parent : NULL;
150 }
151
152 /** Get the consumer properties.
153 */
154
155 mlt_properties mlt_consumer_properties( mlt_consumer this )
156 {
157         return this != NULL ? MLT_SERVICE_PROPERTIES( &this->parent ) : NULL;
158 }
159
160 /** Connect the consumer to the producer.
161 */
162
163 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
164 {
165         return mlt_service_connect_producer( &this->parent, producer, 0 );
166 }
167
168 /** Start the consumer.
169 */
170
171 int mlt_consumer_start( mlt_consumer this )
172 {
173         // Get the properies
174         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
175
176         // Determine if there's a test card producer
177         char *test_card = mlt_properties_get( properties, "test_card" );
178
179         // Handle profiles
180         char *profile = mlt_properties_get( properties, "profile" );
181         int profile_ok = mlt_consumer_profile( properties, profile );
182
183         // Check that everything is OK
184         if ( !profile_ok )
185                 fprintf( stderr, "Unrecognised profile %s - continuing with defaults.\n", mlt_properties_get( properties, "profile" ) );
186         else if ( profile_ok < 0 )
187                 fprintf( stderr, "Recognised profile %s with warnings - trying to continue with some defaults.\n", mlt_properties_get( properties, "profile" ) );
188
189         // Just to make sure nothing is hanging around...
190         mlt_frame_close( this->put );
191         this->put = NULL;
192         this->put_active = 1;
193
194         // Deal with it now.
195         if ( test_card != NULL )
196         {
197                 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
198                 {
199                         // Create a test card producer
200                         mlt_producer producer = mlt_factory_producer( NULL, test_card );
201
202                         // Do we have a producer
203                         if ( producer != NULL )
204                         {
205                                 // Test card should loop I guess...
206                                 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
207                                 //mlt_producer_set_speed( producer, 0 );
208                                 //mlt_producer_set_in_and_out( producer, 0, 0 );
209
210                                 // Set the test card on the consumer
211                                 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
212                         }
213                 }
214         }
215         else
216         {
217                 // Allow the hash table to speed things up
218                 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
219         }
220
221         // Check and run an ante command
222         if ( mlt_properties_get( properties, "ante" ) )
223                 system( mlt_properties_get( properties, "ante" ) );
224
225         // Set the real_time preference
226         this->real_time = mlt_properties_get_int( properties, "real_time" );
227
228         // Start the service
229         if ( this->start != NULL )
230                 return this->start( this );
231
232         return 0;
233 }
234
235 /** An alternative method to feed frames into the consumer - only valid if
236         the consumer itself is not connected.
237 */
238
239 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
240 {
241         int error = 1;
242
243         // Get the service assoicated to the consumer
244         mlt_service service = MLT_CONSUMER_SERVICE( this );
245
246         if ( mlt_service_producer( service ) == NULL )
247         {
248                 struct timeval now;
249                 struct timespec tm;
250                 pthread_mutex_lock( &this->put_mutex );
251                 while ( this->put_active && this->put != NULL )
252                 {
253                         gettimeofday( &now, NULL );
254                         tm.tv_sec = now.tv_sec + 1;
255                         tm.tv_nsec = now.tv_usec * 1000;
256                         pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
257                 }
258                 if ( this->put_active && this->put == NULL )
259                         this->put = frame;
260                 else
261                         mlt_frame_close( frame );
262                 pthread_cond_broadcast( &this->put_cond );
263                 pthread_mutex_unlock( &this->put_mutex );
264         }
265         else
266         {
267                 mlt_frame_close( frame );
268         }
269
270         return error;
271 }
272
273 /** Protected method for consumer to get frames from connected service
274 */
275
276 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
277 {
278         // Frame to return
279         mlt_frame frame = NULL;
280
281         // Get the service assoicated to the consumer
282         mlt_service service = MLT_CONSUMER_SERVICE( this );
283
284         // Get the consumer properties
285         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
286
287         // Get the frame
288         if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
289         {
290                 struct timeval now;
291                 struct timespec tm;
292                 pthread_mutex_lock( &this->put_mutex );
293                 while ( this->put_active && this->put == NULL )
294                 {
295                         gettimeofday( &now, NULL );
296                         tm.tv_sec = now.tv_sec + 1;
297                         tm.tv_nsec = now.tv_usec * 1000;
298                         pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
299                 }
300                 frame = this->put;
301                 this->put = NULL;
302                 pthread_cond_broadcast( &this->put_cond );
303                 pthread_mutex_unlock( &this->put_mutex );
304                 if ( frame != NULL )
305                         mlt_service_apply_filters( service, frame, 0 );
306         }
307         else if ( mlt_service_producer( service ) != NULL )
308         {
309                 mlt_service_get_frame( service, &frame, 0 );
310         }
311         else
312         {
313                 frame = mlt_frame_init( );
314         }
315
316         if ( frame != NULL )
317         {
318                 // Get the frame properties
319                 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
320
321                 // Get the test card producer
322                 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
323
324                 // Attach the test frame producer to it.
325                 if ( test_card != NULL )
326                         mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
327
328                 // Attach the rescale property
329                 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
330
331                 // Aspect ratio and other jiggery pokery
332                 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
333                 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
334                 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
335         }
336
337         // Return the frame
338         return frame;
339 }
340
341 static inline long time_difference( struct timeval *time1 )
342 {
343         struct timeval time2;
344         time2.tv_sec = time1->tv_sec;
345         time2.tv_usec = time1->tv_usec;
346         gettimeofday( time1, NULL );
347         return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
348 }
349
350 int mlt_consumer_profile( mlt_properties properties, char *profile )
351 {
352         double fps = mlt_properties_get_double( properties, "fps" );
353         int recognised = 1;
354         double display_num = 0;
355         double display_den = 0;
356         double pixel_num = 0;
357         double pixel_den = 0;
358         int width = 0;
359         int height = 0;
360
361         if ( fps == 25.0 && profile != NULL )
362         {
363                 width = 720;
364                 height = 576;
365                 display_num = 4;
366                 display_den = 3;
367                 pixel_num = 59;
368                 pixel_den = 54;
369
370                 if ( !strcmp( profile, "dv_wide" ) )
371                 {
372                         display_num = 16;
373                         display_den = 9;
374                         pixel_num = 118;
375                         pixel_den = 81;
376                 }
377                 else if ( !strncmp( profile, "square_wide", 11 ) )
378                 {
379                         display_num = 16;
380                         display_den = 9;
381                         pixel_num = 1;
382                         pixel_den = 1;
383                         width = 1024;
384                         height = 576;
385                 }
386                 else if ( !strncmp( profile, "square", 6 ) )
387                 {
388                         pixel_num = 1;
389                         pixel_den = 1;
390                         width = 768;
391                         height = 576;
392                 }
393                 else if ( !strncmp( profile, "vcd", 3 ) )
394                 {
395                         width = 352;
396                         height = 288;
397                 }
398                 else if ( !strncmp( profile, "cvd", 3 ) )
399                 {
400                         width = 352;
401                         height = 576;
402                         pixel_num = 59;
403                         pixel_den = 27;
404                 }
405                 else if ( !strncmp( profile, "svcd_wide", 9 ) )
406                 {
407                         width = 480;
408                         height = 576;
409                         pixel_num = 59;
410                         pixel_den = 27;
411                         display_num = 16;
412                         display_den = 9;
413                 }
414                 else if ( !strncmp( profile, "svcd", 4 ) )
415                 {
416                         width = 480;
417                         height = 576;
418                         pixel_num = 59;
419                         pixel_den = 36;
420                 }
421                 else if ( strncmp( profile, "frame", 5 ) && strcmp( profile, "dv" ) )
422                 {
423                         recognised = 0;
424                 }
425         }
426         else if ( profile != NULL )
427         {
428                 width = 720;
429                 height = 480;
430                 display_num = 4;
431                 display_den = 3;
432                 pixel_num = 10;
433                 pixel_den = 11;
434
435                 if ( !strcmp( profile, "dv_wide" ) )
436                 {
437                         display_num = 16;
438                         display_den = 9;
439                         pixel_num = 40;
440                         pixel_den = 33;
441                 }
442                 else if ( !strncmp( profile, "square_wide", 11 ) )
443                 {
444                         display_num = 16;
445                         display_den = 9;
446                         pixel_num = 1;
447                         pixel_den = 1;
448                         width = 854;
449                         height = 480;
450                 }
451                 else if ( !strncmp( profile, "square", 6 ) )
452                 {
453                         pixel_num = 1;
454                         pixel_den = 1;
455                         width = 640;
456                         height = 480;
457                 }
458                 else if ( !strncmp( profile, "vcd", 3 ) )
459                 {
460                         width = 352;
461                         height = 240;
462                 }
463                 else if ( !strncmp( profile, "cvd", 3 ) )
464                 {
465                         width = 352;
466                         height = 480;
467                         pixel_num = 20;
468                         pixel_den = 11;
469                 }
470                 else if ( !strncmp( profile, "svcd_wide", 9 ) )
471                 {
472                         width = 480;
473                         height = 480;
474                         pixel_num = 20;
475                         pixel_den = 11;
476                         display_num = 16;
477                         display_den = 9;
478                 }
479                 else if ( !strncmp( profile, "svcd", 4 ) )
480                 {
481                         width = 480;
482                         height = 480;
483                         pixel_num = 15;
484                         pixel_den = 11;
485                 }
486                 else if ( strncmp( profile, "frame", 5 ) && strcmp( profile, "dv" ) )
487                 {
488                         recognised = 0;
489                 }
490         }
491
492         // If width (or any of the above are 0), we use defaults otherwise we switch to recognised
493         if ( width != 0 && recognised )
494         {
495                 if ( recognised && strchr( profile, ':' ) )
496                         if ( sscanf( strchr( profile, ':' ) + 1, "%dx%d", &width, &height ) != 2 )
497                                 recognised = -1;
498
499                 mlt_properties_set_int( properties, "width", width );
500                 mlt_properties_set_int( properties, "height", height );
501                 mlt_properties_set_double( properties, "aspect_ratio", pixel_num / pixel_den );
502                 mlt_properties_set_int( properties, "aspect_ratio_num", pixel_num );
503                 mlt_properties_set_int( properties, "aspect_ratio_den", pixel_den );
504                 mlt_properties_set_double( properties, "display_ratio", display_num / display_den );
505                 mlt_properties_set_int( properties, "display_ratio_num", display_num );
506                 mlt_properties_set_int( properties, "display_ratio_den", display_den );
507         }
508
509         return recognised;
510 }
511
512 static void *consumer_read_ahead_thread( void *arg )
513 {
514         // The argument is the consumer
515         mlt_consumer this = arg;
516
517         // Get the properties of the consumer
518         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
519
520         // Get the width and height
521         int width = mlt_properties_get_int( properties, "width" );
522         int height = mlt_properties_get_int( properties, "height" );
523
524         // See if video is turned off
525         int video_off = mlt_properties_get_int( properties, "video_off" );
526
527         // Get the audio settings
528         mlt_audio_format afmt = mlt_audio_pcm;
529         int counter = 0;
530         double fps = mlt_properties_get_double( properties, "fps" );
531         int channels = mlt_properties_get_int( properties, "channels" );
532         int frequency = mlt_properties_get_int( properties, "frequency" );
533         int samples = 0;
534         int16_t *pcm = NULL;
535
536         // See if audio is turned off
537         int audio_off = mlt_properties_get_int( properties, "audio_off" );
538
539         // Get the maximum size of the buffer
540         int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
541
542         // General frame variable
543         mlt_frame frame = NULL;
544         uint8_t *image = NULL;
545
546         // Time structures
547         struct timeval ante;
548
549         // Average time for get_frame and get_image
550         int count = 1;
551         int skipped = 0;
552         int64_t time_wait = 0;
553         int64_t time_frame = 0;
554         int64_t time_process = 0;
555         int skip_next = 0;
556         mlt_service lock_object = NULL;
557
558         // Get the first frame
559         frame = mlt_consumer_get_frame( this );
560
561         // Get the lock object
562         lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
563
564         // Lock it
565         if ( lock_object ) mlt_service_lock( lock_object );
566
567         // Get the image of the first frame
568         if ( !video_off )
569         {
570                 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
571                 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
572         }
573
574         if ( !audio_off )
575         {
576                 samples = mlt_sample_calculator( fps, frequency, counter++ );
577                 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
578         }
579
580         // Unlock the lock object
581         if ( lock_object ) mlt_service_unlock( lock_object );
582
583         // Mark as rendered
584         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
585
586         // Get the starting time (can ignore the times above)
587         gettimeofday( &ante, NULL );
588
589         // Continue to read ahead
590         while ( this->ahead )
591         {
592                 // Fetch width/height again
593                 width = mlt_properties_get_int( properties, "width" );
594                 height = mlt_properties_get_int( properties, "height" );
595
596                 // Put the current frame into the queue
597                 pthread_mutex_lock( &this->mutex );
598                 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
599                         pthread_cond_wait( &this->cond, &this->mutex );
600                 mlt_deque_push_back( this->queue, frame );
601                 pthread_cond_broadcast( &this->cond );
602                 pthread_mutex_unlock( &this->mutex );
603
604                 time_wait += time_difference( &ante );
605
606                 // Get the next frame
607                 frame = mlt_consumer_get_frame( this );
608                 time_frame += time_difference( &ante );
609
610                 // If there's no frame, we're probably stopped...
611                 if ( frame == NULL )
612                         continue;
613
614                 // Attempt to fetch the lock object
615                 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
616
617                 // Increment the count
618                 count ++;
619
620                 // Lock if there's a lock object
621                 if ( lock_object ) mlt_service_lock( lock_object );
622
623                 // All non normal playback frames should be shown
624                 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
625                 {
626                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
627                         skipped = 0;
628                         time_frame = 0;
629                         time_process = 0;
630                         time_wait = 0;
631                         count = 1;
632                         skip_next = 0;
633                 }
634
635                 // Get the image
636                 if ( !skip_next )
637                 {
638                         // Get the image, mark as rendered and time it
639                         if ( !video_off )
640                         {
641                                 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
642                                 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
643                         }
644                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
645                 }
646                 else
647                 {
648                         // Increment the number of sequentially skipped frames
649                         skipped ++;
650                         skip_next = 0;
651
652                         // If we've reached an unacceptable level, reset everything
653                         if ( skipped > 5 )
654                         {
655                                 skipped = 0;
656                                 time_frame = 0;
657                                 time_process = 0;
658                                 time_wait = 0;
659                                 count = 1;
660                         }
661                 }
662
663                 // Always process audio
664                 if ( !audio_off )
665                 {
666                         samples = mlt_sample_calculator( fps, frequency, counter++ );
667                         mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
668                 }
669
670                 // Increment the time take for this frame
671                 time_process += time_difference( &ante );
672
673                 // Determine if the next frame should be skipped
674                 if ( mlt_deque_count( this->queue ) <= 5 && ( ( time_wait + time_frame + time_process ) / count ) > 40000 )
675                         skip_next = 1;
676
677                 // Unlock if there's a lock object
678                 if ( lock_object ) mlt_service_unlock( lock_object );
679         }
680
681         // Remove the last frame
682         mlt_frame_close( frame );
683
684         return NULL;
685 }
686
687 static void consumer_read_ahead_start( mlt_consumer this )
688 {
689         // We're running now
690         this->ahead = 1;
691
692         // Create the frame queue
693         this->queue = mlt_deque_init( );
694
695         // Create the mutex
696         pthread_mutex_init( &this->mutex, NULL );
697
698         // Create the condition
699         pthread_cond_init( &this->cond, NULL );
700
701         // Create the read ahead 
702         pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
703 }
704
705 static void consumer_read_ahead_stop( mlt_consumer this )
706 {
707         // Make sure we're running
708         if ( this->ahead )
709         {
710                 // Inform thread to stop
711                 this->ahead = 0;
712
713                 // Broadcast to the condition in case it's waiting
714                 pthread_mutex_lock( &this->mutex );
715                 pthread_cond_broadcast( &this->cond );
716                 pthread_mutex_unlock( &this->mutex );
717
718                 // Broadcast to the put condition in case it's waiting
719                 pthread_mutex_lock( &this->put_mutex );
720                 pthread_cond_broadcast( &this->put_cond );
721                 pthread_mutex_unlock( &this->put_mutex );
722
723                 // Join the thread
724                 pthread_join( this->ahead_thread, NULL );
725
726                 // Destroy the mutex
727                 pthread_mutex_destroy( &this->mutex );
728
729                 // Destroy the condition
730                 pthread_cond_destroy( &this->cond );
731
732                 // Wipe the queue
733                 while ( mlt_deque_count( this->queue ) )
734                         mlt_frame_close( mlt_deque_pop_back( this->queue ) );
735
736                 // Close the queue
737                 mlt_deque_close( this->queue );
738         }
739 }
740
741 void mlt_consumer_purge( mlt_consumer this )
742 {
743         if ( this->ahead )
744         {
745                 pthread_mutex_lock( &this->mutex );
746                 while ( mlt_deque_count( this->queue ) )
747                         mlt_frame_close( mlt_deque_pop_back( this->queue ) );
748                 pthread_cond_broadcast( &this->cond );
749                 pthread_mutex_unlock( &this->mutex );
750         }
751 }
752
753 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
754 {
755         // Frame to return
756         mlt_frame frame = NULL;
757
758         // Get the properties
759         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
760
761         // Check if the user has requested real time or not
762         if ( this->real_time )
763         {
764                 int size = 1;
765
766                 // Is the read ahead running?
767                 if ( this->ahead == 0 )
768                 {
769                         int buffer = mlt_properties_get_int( properties, "buffer" );
770                         int prefill = mlt_properties_get_int( properties, "prefill" );
771                         consumer_read_ahead_start( this );
772                         if ( buffer > 1 )
773                                 size = prefill > 0 && prefill < buffer ? prefill : buffer;
774                 }
775         
776                 // Get frame from queue
777                 pthread_mutex_lock( &this->mutex );
778                 while( this->ahead && mlt_deque_count( this->queue ) < size )
779                         pthread_cond_wait( &this->cond, &this->mutex );
780                 frame = mlt_deque_pop_front( this->queue );
781                 pthread_cond_broadcast( &this->cond );
782                 pthread_mutex_unlock( &this->mutex );
783         }
784         else
785         {
786                 // Get the frame in non real time
787                 frame = mlt_consumer_get_frame( this );
788
789                 // This isn't true, but from the consumers perspective it is
790                 if ( frame != NULL )
791                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
792         }
793
794         return frame;
795 }
796
797 /** Callback for the implementation to indicate a stopped condition.
798 */
799
800 void mlt_consumer_stopped( mlt_consumer this )
801 {
802         mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
803         mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
804 }
805
806 /** Stop the consumer.
807 */
808
809 int mlt_consumer_stop( mlt_consumer this )
810 {
811         // Get the properies
812         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
813         char *debug = mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "debug" );
814
815         // Just in case...
816         if ( debug ) fprintf( stderr, "%s: stopping put waiting\n", debug );
817         pthread_mutex_lock( &this->put_mutex );
818         this->put_active = 0;
819         pthread_cond_broadcast( &this->put_cond );
820         pthread_mutex_unlock( &this->put_mutex );
821
822         // Stop the consumer
823         if ( debug ) fprintf( stderr, "%s: stopping consumer\n", debug );
824         if ( this->stop != NULL )
825                 this->stop( this );
826
827         // Check if the user has requested real time or not and stop if necessary
828         if ( debug ) fprintf( stderr, "%s: stopping read_ahead\n", debug );
829         if ( mlt_properties_get_int( properties, "real_time" ) )
830                 consumer_read_ahead_stop( this );
831
832         // Kill the test card
833         mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
834
835         // Check and run a post command
836         if ( mlt_properties_get( properties, "post" ) )
837                 system( mlt_properties_get( properties, "post" ) );
838
839         if ( debug ) fprintf( stderr, "%s: stopped\n", debug );
840
841         return 0;
842 }
843
844 /** Determine if the consumer is stopped.
845 */
846
847 int mlt_consumer_is_stopped( mlt_consumer this )
848 {
849         // Check if the consumer is stopped
850         if ( this->is_stopped != NULL )
851                 return this->is_stopped( this );
852
853         return 0;
854 }
855
856 /** Close the consumer.
857 */
858
859 void mlt_consumer_close( mlt_consumer this )
860 {
861         if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
862         {
863                 // Get the childs close function
864                 void ( *consumer_close )( ) = this->close;
865
866                 if ( consumer_close )
867                 {
868                         // Just in case...
869                         //mlt_consumer_stop( this );
870
871                         this->close = NULL;
872                         consumer_close( this );
873                 }
874                 else
875                 {
876                         // Make sure it only gets called once
877                         this->parent.close = NULL;
878
879                         // Destroy the push mutex and condition
880                         pthread_mutex_destroy( &this->put_mutex );
881                         pthread_cond_destroy( &this->put_cond );
882
883                         mlt_service_close( &this->parent );
884                 }
885         }
886 }