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