86bed309025fe804f53021d5bf4e8b92bb93a520
[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 /** Public final methods
32 */
33
34 int mlt_consumer_init( mlt_consumer this, void *child )
35 {
36 int error = 0;
37 memset( this, 0, sizeof( struct mlt_consumer_s ) );
38 this->child = child;
39 error = mlt_service_init( &this->parent, this );
40 if ( error == 0 )
41 {
42 // Get the properties from the service
43 mlt_properties properties = mlt_service_properties( &this->parent );
44
45 // Get the normalisation preference
46 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
47
48 // Deal with normalisation
49 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
50 {
51 mlt_properties_set( properties, "normalisation", "PAL" );
52 mlt_properties_set_double( properties, "fps", 25.0 );
53 mlt_properties_set_int( properties, "width", 720 );
54 mlt_properties_set_int( properties, "height", 576 );
55 mlt_properties_set_int( properties, "progressive", 0 );
56 mlt_properties_set_double( properties, "aspect_ratio", 128.0 / 117.0 );
57 }
58 else
59 {
60 mlt_properties_set( properties, "normalisation", "NTSC" );
61 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
62 mlt_properties_set_int( properties, "width", 720 );
63 mlt_properties_set_int( properties, "height", 480 );
64 mlt_properties_set_int( properties, "progressive", 0 );
65 mlt_properties_set_double( properties, "aspect_ratio", 72.0 / 79.0 );
66 }
67
68 // Default rescaler for all consumers
69 mlt_properties_set( properties, "rescale", "bilinear" );
70
71 // Default read ahead buffer size
72 mlt_properties_set_int( properties, "buffer", 25 );
73
74 // Default audio frequency and channels
75 mlt_properties_set_int( properties, "frequency", 48000 );
76 mlt_properties_set_int( properties, "channels", 2 );
77
78 // Default of all consumers is real time
79 mlt_properties_set_int( properties, "real_time", 1 );
80
81 // Default to environment test card
82 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
83
84 // Hmm - default all consumers to yuv422 :-/
85 this->format = mlt_image_yuv422;
86 }
87 return error;
88 }
89
90 /** Create a new consumer.
91 */
92
93 mlt_consumer mlt_consumer_new( )
94 {
95 // Create the memory for the structure
96 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
97
98 // Initialise it
99 if ( this != NULL )
100 mlt_consumer_init( this, NULL );
101
102 // Return it
103 return this;
104 }
105
106 /** Get the parent service object.
107 */
108
109 mlt_service mlt_consumer_service( mlt_consumer this )
110 {
111 return &this->parent;
112 }
113
114 /** Get the consumer properties.
115 */
116
117 mlt_properties mlt_consumer_properties( mlt_consumer this )
118 {
119 return mlt_service_properties( &this->parent );
120 }
121
122 /** Connect the consumer to the producer.
123 */
124
125 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
126 {
127 return mlt_service_connect_producer( &this->parent, producer, 0 );
128 }
129
130 /** Start the consumer.
131 */
132
133 int mlt_consumer_start( mlt_consumer this )
134 {
135 // Get the properies
136 mlt_properties properties = mlt_consumer_properties( this );
137
138 // Determine if there's a test card producer
139 char *test_card = mlt_properties_get( properties, "test_card" );
140
141 // Deal with it now.
142 if ( test_card != NULL )
143 {
144 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
145 {
146 // Create a test card producer
147 mlt_producer producer = mlt_factory_producer( NULL, test_card );
148
149 // Do we have a producer
150 if ( producer != NULL )
151 {
152 // Test card should loop I guess...
153 mlt_properties_set( mlt_producer_properties( producer ), "eof", "loop" );
154
155 // Set the test card on the consumer
156 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
157 }
158
159 // Check and run an ante command
160 if ( mlt_properties_get( properties, "ante" ) )
161 system( mlt_properties_get( properties, "ante" ) );
162 }
163 }
164 else
165 {
166 // Allow the hash table to speed things up
167 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
168 }
169
170 // Set the real_time preference
171 this->real_time = mlt_properties_get_int( properties, "real_time" );
172
173 // Start the service
174 if ( this->start != NULL )
175 return this->start( this );
176
177 return 0;
178 }
179
180 /** Protected method for consumer to get frames from connected service
181 */
182
183 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
184 {
185 // Frame to return
186 mlt_frame frame = NULL;
187
188 // Get the service assoicated to the consumer
189 mlt_service service = mlt_consumer_service( this );
190
191 // Get the frame
192 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
193 {
194 // Get the consumer properties
195 mlt_properties properties = mlt_consumer_properties( this );
196
197 // Get the frame properties
198 mlt_properties frame_properties = mlt_frame_properties( frame );
199
200 // Get the test card producer
201 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
202
203 // Attach the test frame producer to it.
204 if ( test_card != NULL )
205 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
206
207 // Attach the rescale property
208 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
209
210 // Aspect ratio and other jiggery pokery
211 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
212 mlt_properties_set_int( frame_properties, "consumer_progressive", mlt_properties_get_int( properties, "progressive" ) );
213 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "deinterlace" ) );
214 }
215
216 // Return the frame
217 return frame;
218 }
219
220 static inline long time_difference( struct timeval *time1 )
221 {
222 struct timeval time2;
223 time2.tv_sec = time1->tv_sec;
224 time2.tv_usec = time1->tv_usec;
225 gettimeofday( time1, NULL );
226 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
227 }
228
229 static void *consumer_read_ahead_thread( void *arg )
230 {
231 // The argument is the consumer
232 mlt_consumer this = arg;
233
234 // Get the properties of the consumer
235 mlt_properties properties = mlt_consumer_properties( this );
236
237 // Get the width and height
238 int width = mlt_properties_get_int( properties, "width" );
239 int height = mlt_properties_get_int( properties, "height" );
240
241 // See if video is turned off
242 int video_off = mlt_properties_get_int( properties, "video_off" );
243
244 // Get the audio settings
245 mlt_audio_format afmt = mlt_audio_pcm;
246 int counter = 0;
247 double fps = mlt_properties_get_double( properties, "fps" );
248 int channels = mlt_properties_get_int( properties, "channels" );
249 int frequency = mlt_properties_get_int( properties, "frequency" );
250 int samples = 0;
251 int16_t *pcm = NULL;
252
253 // See if audio is turned off
254 int audio_off = mlt_properties_get_int( properties, "audio_off" );
255
256 // Get the maximum size of the buffer
257 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
258
259 // General frame variable
260 mlt_frame frame = NULL;
261 uint8_t *image = NULL;
262
263 // Time structures
264 struct timeval ante;
265
266 // Average time for get_frame and get_image
267 int count = 1;
268 int skipped = 0;
269 int64_t time_wait = 0;
270 int64_t time_frame = 0;
271 int64_t time_image = 0;
272
273 // Get the first frame
274 frame = mlt_consumer_get_frame( this );
275
276 // Get the image of the first frame
277 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
278 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
279
280 // Get the starting time (can ignore the times above)
281 gettimeofday( &ante, NULL );
282
283 // Continue to read ahead
284 while ( this->ahead )
285 {
286 // Put the current frame into the queue
287 time_difference( &ante );
288 pthread_mutex_lock( &this->mutex );
289 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
290 pthread_cond_wait( &this->cond, &this->mutex );
291 mlt_deque_push_back( this->queue, frame );
292 pthread_cond_broadcast( &this->cond );
293 pthread_mutex_unlock( &this->mutex );
294 time_wait = time_difference( &ante );
295
296 // Get the next frame
297 frame = mlt_consumer_get_frame( this );
298 time_frame += time_difference( &ante );
299
300 // Increment the count
301 count ++;
302
303 // All non normal playback frames should be shown
304 if ( mlt_properties_get_int( mlt_frame_properties( frame ), "_speed" ) != 1 )
305 {
306 skipped = 0;
307 time_frame = 0;
308 time_image = 0;
309 count = 1;
310 }
311
312 // Get the image
313 if ( ( time_frame + time_image ) / count < 40000 )
314 {
315 // Get the image, mark as rendered and time it
316 if ( !video_off )
317 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
318 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
319
320 // Reset the skipped count
321 skipped = 0;
322 }
323 else
324 {
325 // Increment the number of sequentially skipped frames
326 skipped ++;
327
328 // If we've reached an unacceptable level, reset everything
329 if ( skipped > 10 )
330 {
331 skipped = 0;
332 time_frame = 0;
333 time_image = 0;
334 count = 0;
335 }
336 }
337
338 // Always process audio
339 if ( !audio_off )
340 {
341 samples = mlt_sample_calculator( fps, frequency, counter++ );
342 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
343 frame->get_audio = NULL;
344 }
345
346 // Increment the time take for this frame
347 time_image += time_difference( &ante );
348 }
349
350 // Remove the last frame
351 mlt_frame_close( frame );
352
353 return NULL;
354 }
355
356 static void consumer_read_ahead_start( mlt_consumer this )
357 {
358 pthread_attr_t thread_attributes;
359
360 // We're running now
361 this->ahead = 1;
362
363 // Create the frame queue
364 this->queue = mlt_deque_init( );
365
366 // Create the mutex
367 pthread_mutex_init( &this->mutex, NULL );
368
369 // Create the condition
370 pthread_cond_init( &this->cond, NULL );
371
372 // Inherit the scheduling priority
373 pthread_attr_init( &thread_attributes );
374 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
375
376 // Create the read ahead
377 pthread_create( &this->ahead_thread, &thread_attributes, consumer_read_ahead_thread, this );
378 }
379
380 static void consumer_read_ahead_stop( mlt_consumer this )
381 {
382 // Make sure we're running
383 if ( this->ahead )
384 {
385 // Inform thread to stop
386 this->ahead = 0;
387
388 // Broadcast to the condition in case it's waiting
389 pthread_mutex_lock( &this->mutex );
390 pthread_cond_broadcast( &this->cond );
391 pthread_mutex_unlock( &this->mutex );
392
393 // Join the thread
394 pthread_join( this->ahead_thread, NULL );
395
396 // Destroy the mutex
397 pthread_mutex_destroy( &this->mutex );
398
399 // Destroy the condition
400 pthread_cond_destroy( &this->cond );
401
402 // Wipe the queue
403 while ( mlt_deque_count( this->queue ) )
404 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
405
406 // Close the queue
407 mlt_deque_close( this->queue );
408 }
409 }
410
411 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
412 {
413 // Frame to return
414 mlt_frame frame = NULL;
415
416 // Get the properties
417 mlt_properties properties = mlt_consumer_properties( this );
418
419 // Check if the user has requested real time or not
420 if ( this->real_time )
421 {
422 int size = 1;
423
424 // Is the read ahead running?
425 if ( this->ahead == 0 )
426 {
427 int buffer = mlt_properties_get_int( properties, "buffer" );
428 consumer_read_ahead_start( this );
429 if ( buffer > 1 )
430 size = buffer;
431 }
432
433 // Get frame from queue
434 pthread_mutex_lock( &this->mutex );
435 while( this->ahead && mlt_deque_count( this->queue ) < size )
436 pthread_cond_wait( &this->cond, &this->mutex );
437 frame = mlt_deque_pop_front( this->queue );
438 pthread_cond_broadcast( &this->cond );
439 pthread_mutex_unlock( &this->mutex );
440 }
441 else
442 {
443 // Get the frame in non real time
444 frame = mlt_consumer_get_frame( this );
445
446 // This isn't true, but from the consumers perspective it is
447 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
448 }
449
450 return frame;
451 }
452
453 /** Stop the consumer.
454 */
455
456 int mlt_consumer_stop( mlt_consumer this )
457 {
458 // Get the properies
459 mlt_properties properties = mlt_consumer_properties( this );
460
461 // Stop the consumer
462 if ( this->stop != NULL )
463 this->stop( this );
464
465 // Check if the user has requested real time or not and stop if necessary
466 if ( mlt_properties_get_int( properties, "real_time" ) )
467 consumer_read_ahead_stop( this );
468
469 // Kill the test card
470 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
471
472 // Check and run a post command
473 if ( mlt_properties_get( properties, "post" ) )
474 system( mlt_properties_get( properties, "post" ) );
475
476 return 0;
477 }
478
479 /** Determine if the consumer is stopped.
480 */
481
482 int mlt_consumer_is_stopped( mlt_consumer this )
483 {
484 // Check if the consumer is stopped
485 if ( this->is_stopped != NULL )
486 return this->is_stopped( this );
487
488 return 0;
489 }
490
491 /** Close the consumer.
492 */
493
494 void mlt_consumer_close( mlt_consumer this )
495 {
496 // Get the childs close function
497 void ( *consumer_close )( ) = this->close;
498
499 // Make sure it only gets called once
500 this->close = NULL;
501
502 // Call the childs close if available
503 if ( consumer_close != NULL )
504 consumer_close( this );
505 else
506 mlt_service_close( &this->parent );
507 }