consumer read ahead and int32_t migration
[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 }
57 else
58 {
59 mlt_properties_set( properties, "normalisation", "NTSC" );
60 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
61 mlt_properties_set_int( properties, "width", 720 );
62 mlt_properties_set_int( properties, "height", 480 );
63 mlt_properties_set_int( properties, "progressive", 0 );
64 }
65 mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
66
67 // Default rescaler for all consumers
68 mlt_properties_set( properties, "rescale", "bilinear" );
69 }
70 return error;
71 }
72
73 /** Get the parent service object.
74 */
75
76 mlt_service mlt_consumer_service( mlt_consumer this )
77 {
78 return &this->parent;
79 }
80
81 /** Get the consumer properties.
82 */
83
84 mlt_properties mlt_consumer_properties( mlt_consumer this )
85 {
86 return mlt_service_properties( &this->parent );
87 }
88
89 /** Connect the consumer to the producer.
90 */
91
92 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
93 {
94 return mlt_service_connect_producer( &this->parent, producer, 0 );
95 }
96
97 /** Start the consumer.
98 */
99
100 int mlt_consumer_start( mlt_consumer this )
101 {
102 // Get the properies
103 mlt_properties properties = mlt_consumer_properties( this );
104
105 // Determine if there's a test card producer
106 char *test_card = mlt_properties_get( properties, "test_card" );
107
108 // Deal with it now.
109 if ( test_card != NULL )
110 {
111 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
112 {
113 // Create a test card producer
114 // TODO: do we want to use fezzik here?
115 mlt_producer producer = mlt_factory_producer( "fezzik", test_card );
116
117 // Do we have a producer
118 if ( producer != NULL )
119 {
120 // Test card should loop I guess...
121 mlt_properties_set( mlt_producer_properties( producer ), "eof", "loop" );
122
123 // Set the test card on the consumer
124 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
125 }
126
127 // Check and run an ante command
128 if ( mlt_properties_get( properties, "ante" ) )
129 system( mlt_properties_get( properties, "ante" ) );
130 }
131 }
132 else
133 {
134 // Allow the hash table to speed things up
135 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
136 }
137
138 // Start the service
139 if ( this->start != NULL )
140 return this->start( this );
141
142 return 0;
143 }
144
145 /** Protected method :-/ for consumer to get frames from connected service
146 */
147
148 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
149 {
150 // Frame to return
151 mlt_frame frame = NULL;
152
153 // Get the service assoicated to the consumer
154 mlt_service service = mlt_consumer_service( this );
155
156 // Get the frame
157 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
158 {
159 // Get the consumer properties
160 mlt_properties properties = mlt_consumer_properties( this );
161
162 // Get the frame properties
163 mlt_properties frame_properties = mlt_frame_properties( frame );
164
165 // Get the test card producer
166 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
167
168 // Attach the test frame producer to it.
169 if ( test_card != NULL )
170 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
171
172 // Attach the rescale property
173 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
174
175 // Aspect ratio and other jiggery pokery
176 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
177 mlt_properties_set_int( frame_properties, "consumer_progressive", mlt_properties_get_int( properties, "progressive" ) );
178 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "deinterlace" ) );
179 }
180
181 // Return the frame
182 return frame;
183 }
184
185 static inline long time_difference( struct timeval *time1 )
186 {
187 struct timeval time2;
188 time2.tv_sec = time1->tv_sec;
189 time2.tv_usec = time1->tv_usec;
190 gettimeofday( time1, NULL );
191 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
192 }
193
194 static void *consumer_read_ahead_thread( void *arg )
195 {
196 // The argument is the consumer
197 mlt_consumer this = arg;
198
199 // Get the properties of the consumer
200 mlt_properties properties = mlt_consumer_properties( this );
201
202 // Get the width and height
203 int width = mlt_properties_get_int( properties, "width" );
204 int height = mlt_properties_get_int( properties, "height" );
205
206 // General frame variable
207 mlt_frame frame = NULL;
208 uint8_t *image = NULL;
209
210 // Time structures
211 struct timeval ante;
212
213 // Average time for get_frame and get_image
214 int count = 1;
215 int64_t time_wait = 0;
216 int64_t time_frame = 0;
217 int64_t time_image = 0;
218
219 // Get the first frame
220 gettimeofday( &ante, NULL );
221 frame = mlt_consumer_get_frame( this );
222 time_frame = time_difference( &ante );
223
224 // Get the image of the first frame
225 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
226 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
227 time_image = time_difference( &ante );
228
229 // Continue to read ahead
230 while ( this->ahead )
231 {
232 // Put the current frame into the queue
233 pthread_mutex_lock( &this->mutex );
234 while( this->ahead && mlt_deque_count( this->queue ) >= 25 )
235 pthread_cond_wait( &this->cond, &this->mutex );
236 mlt_deque_push_back( this->queue, frame );
237 pthread_cond_broadcast( &this->cond );
238 pthread_mutex_unlock( &this->mutex );
239 time_wait += time_difference( &ante );
240
241 // Get the next frame
242 frame = mlt_consumer_get_frame( this );
243 time_frame += time_difference( &ante );
244
245 // Increment the count
246 count ++;
247
248 // Get the image
249 if ( ( time_frame + time_image + time_wait ) / count < 40000 )
250 {
251 // Get the image, mark as rendered and time it
252 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
253 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
254 time_image += time_difference( &ante );
255 }
256 else
257 {
258 // This is wrong, but it avoids slower machines getting starved
259 // It is, after all, impossible to go back in time :-/
260 time_image -= ( time_image / count / 8 );
261 }
262 }
263
264 // Remove the last frame
265 mlt_frame_close( frame );
266
267 return NULL;
268 }
269
270 static void consumer_read_ahead_start( mlt_consumer this )
271 {
272 // We're running now
273 this->ahead = 1;
274
275 // Create the frame queue
276 this->queue = mlt_deque_init( );
277
278 // Create the mutex
279 pthread_mutex_init( &this->mutex, NULL );
280
281 // Create the condition
282 pthread_cond_init( &this->cond, NULL );
283
284 // Create the read ahead
285 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
286
287 }
288
289 static void consumer_read_ahead_stop( mlt_consumer this )
290 {
291 // Make sure we're running
292 if ( this->ahead )
293 {
294 // Inform thread to stop
295 this->ahead = 0;
296
297 // Broadcast to the condition in case it's waiting
298 pthread_mutex_lock( &this->mutex );
299 pthread_cond_broadcast( &this->cond );
300 pthread_mutex_unlock( &this->mutex );
301
302 // Join the thread
303 pthread_join( this->ahead_thread, NULL );
304
305 // Destroy the mutex
306 pthread_mutex_destroy( &this->mutex );
307
308 // Destroy the condition
309 pthread_cond_destroy( &this->cond );
310
311 // Wipe the queue
312 while ( mlt_deque_count( this->queue ) )
313 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
314 }
315 }
316
317 mlt_frame mlt_consumer_rt_frame( mlt_consumer this, mlt_image_format format )
318 {
319 // Frame to return
320 mlt_frame frame = NULL;
321 int size = 1;
322
323
324 // Is the read ahead running?
325 if ( this->ahead == 0 )
326 {
327 this->format = format;
328 consumer_read_ahead_start( this );
329 size = 12;
330 }
331
332 // Get frame from queue
333 pthread_mutex_lock( &this->mutex );
334 while( this->ahead && mlt_deque_count( this->queue ) < size )
335 pthread_cond_wait( &this->cond, &this->mutex );
336 frame = mlt_deque_pop_front( this->queue );
337 pthread_cond_broadcast( &this->cond );
338 pthread_mutex_unlock( &this->mutex );
339
340 return frame;
341 }
342
343 /** Stop the consumer.
344 */
345
346 int mlt_consumer_stop( mlt_consumer this )
347 {
348 // Get the properies
349 mlt_properties properties = mlt_consumer_properties( this );
350
351 // Stop the consumer
352 if ( this->stop != NULL )
353 this->stop( this );
354
355 // Kill the read ahead
356 consumer_read_ahead_stop( this );
357
358 // Kill the test card
359 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
360
361 // Check and run a post command
362 if ( mlt_properties_get( properties, "post" ) )
363 system( mlt_properties_get( properties, "post" ) );
364
365 return 0;
366 }
367
368 /** Determine if the consumer is stopped.
369 */
370
371 int mlt_consumer_is_stopped( mlt_consumer this )
372 {
373 // Check if the consumer is stopped
374 if ( this->is_stopped != NULL )
375 return this->is_stopped( this );
376
377 return 0;
378 }
379
380 /** Close the consumer.
381 */
382
383 void mlt_consumer_close( mlt_consumer this )
384 {
385 // Get the childs close function
386 void ( *consumer_close )( ) = this->close;
387
388 // Make sure it only gets called once
389 this->close = NULL;
390
391 // Call the childs close if available
392 if ( consumer_close != NULL )
393 consumer_close( this );
394 else
395 mlt_service_close( &this->parent );
396 }
397