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