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