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