consumer_sdl.c: let it work without filter_avcolour_space
[melted] / src / modules / sdl / consumer_sdl.c
1 /*
2 * consumer_sdl.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
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 <framework/mlt_consumer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_deque.h>
24 #include <framework/mlt_factory.h>
25 #include <framework/mlt_filter.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pthread.h>
30 #include <SDL/SDL.h>
31 #include <SDL/SDL_syswm.h>
32 #include <sys/time.h>
33
34 /** This classes definition.
35 */
36
37 typedef struct consumer_sdl_s *consumer_sdl;
38
39 struct consumer_sdl_s
40 {
41 struct mlt_consumer_s parent;
42 mlt_properties properties;
43 mlt_deque queue;
44 pthread_t thread;
45 int joined;
46 int running;
47 uint8_t audio_buffer[ 4096 * 10 ];
48 int audio_avail;
49 pthread_mutex_t audio_mutex;
50 pthread_cond_t audio_cond;
51 pthread_mutex_t video_mutex;
52 pthread_cond_t video_cond;
53 int window_width;
54 int window_height;
55 int previous_width;
56 int previous_height;
57 int width;
58 int height;
59 int playing;
60 int sdl_flags;
61 SDL_Surface *sdl_screen;
62 SDL_Overlay *sdl_overlay;
63 SDL_Rect rect;
64 uint8_t *buffer;
65 int bpp;
66 int filtered;
67 };
68
69 /** Forward references to static functions.
70 */
71
72 static int consumer_start( mlt_consumer parent );
73 static int consumer_stop( mlt_consumer parent );
74 static int consumer_is_stopped( mlt_consumer parent );
75 static void consumer_close( mlt_consumer parent );
76 static void *consumer_thread( void * );
77 static int consumer_get_dimensions( int *width, int *height );
78 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
79
80 /** This is what will be called by the factory - anything can be passed in
81 via the argument, but keep it simple.
82 */
83
84 mlt_consumer consumer_sdl_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
85 {
86 // Create the consumer object
87 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
88
89 // If no malloc'd and consumer init ok
90 if ( this != NULL && mlt_consumer_init( &this->parent, this, profile ) == 0 )
91 {
92 // Create the queue
93 this->queue = mlt_deque_init( );
94
95 // Get the parent consumer object
96 mlt_consumer parent = &this->parent;
97
98 // We have stuff to clean up, so override the close method
99 parent->close = consumer_close;
100
101 // get a handle on properties
102 mlt_service service = MLT_CONSUMER_SERVICE( parent );
103 this->properties = MLT_SERVICE_PROPERTIES( service );
104
105 // Set the default volume
106 mlt_properties_set_double( this->properties, "volume", 1.0 );
107
108 // This is the initialisation of the consumer
109 pthread_mutex_init( &this->audio_mutex, NULL );
110 pthread_cond_init( &this->audio_cond, NULL);
111 pthread_mutex_init( &this->video_mutex, NULL );
112 pthread_cond_init( &this->video_cond, NULL);
113
114 // Default scaler (for now we'll use nearest)
115 mlt_properties_set( this->properties, "rescale", "nearest" );
116
117 // Default buffer for low latency
118 mlt_properties_set_int( this->properties, "buffer", 1 );
119
120 // Default progressive true
121 mlt_properties_set_int( this->properties, "progressive", 0 );
122
123 // Default audio buffer
124 mlt_properties_set_int( this->properties, "audio_buffer", 512 );
125
126 // Ensure we don't join on a non-running object
127 this->joined = 1;
128
129 // process actual param
130 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
131 {
132 this->width = mlt_properties_get_int( this->properties, "width" );
133 this->height = mlt_properties_get_int( this->properties, "height" );
134 }
135
136 // Set the sdl flags
137 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
138
139 // Allow thread to be started/stopped
140 parent->start = consumer_start;
141 parent->stop = consumer_stop;
142 parent->is_stopped = consumer_is_stopped;
143
144 // Register specific events
145 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
146
147 // Return the consumer produced
148 return parent;
149 }
150
151 // malloc or consumer init failed
152 free( this );
153
154 // Indicate failure
155 return NULL;
156 }
157
158 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
159 {
160 if ( listener != NULL )
161 listener( owner, this, ( SDL_Event * )args[ 0 ] );
162 }
163
164 int consumer_start( mlt_consumer parent )
165 {
166 consumer_sdl this = parent->child;
167
168 if ( !this->running )
169 {
170 int video_off = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "video_off" );
171 int preview_off = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "preview_off" );
172 int display_off = video_off | preview_off;
173 int audio_off = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "audio_off" );
174 int sdl_started = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" );
175
176 consumer_stop( parent );
177
178 this->running = 1;
179 this->joined = 0;
180
181 if ( mlt_properties_get_int( this->properties, "width" ) > 0 )
182 this->width = mlt_properties_get_int( this->properties, "width" );
183 if ( mlt_properties_get_int( this->properties, "height" ) > 0 )
184 this->height = mlt_properties_get_int( this->properties, "height" );
185
186 this->bpp = mlt_properties_get_int( this->properties, "bpp" );
187
188 // Attach a colour space converter
189 if ( preview_off && !this->filtered )
190 {
191 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( parent ) );
192 mlt_filter filter = mlt_factory_filter( profile, "avcolour_space", NULL );
193 if ( filter )
194 {
195 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "forced", mlt_image_yuv422 );
196 mlt_service_attach( MLT_CONSUMER_SERVICE( parent ), filter );
197 mlt_filter_close( filter );
198 }
199 this->filtered = 1;
200 }
201
202 if ( sdl_started == 0 && display_off == 0 )
203 {
204 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
205 {
206 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
207 return -1;
208 }
209
210 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
211 SDL_EnableUNICODE( 1 );
212 }
213 else if ( display_off == 0 )
214 {
215 this->sdl_screen = SDL_GetVideoSurface( );
216 }
217
218 if ( audio_off == 0 )
219 SDL_InitSubSystem( SDL_INIT_AUDIO );
220
221 // Default window size
222 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
223 this->window_width = ( double )this->height * display_ratio;
224 this->window_height = this->height;
225
226 if ( this->sdl_screen == NULL && display_off == 0 )
227 {
228 if ( mlt_properties_get_int( this->properties, "fullscreen" ) )
229 {
230 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
231 this->window_width = vi->current_w;
232 this->window_height = vi->current_h;
233 this->sdl_flags |= SDL_FULLSCREEN;
234 SDL_ShowCursor( SDL_DISABLE );
235 }
236 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
237 }
238
239 pthread_create( &this->thread, NULL, consumer_thread, this );
240 }
241
242 return 0;
243 }
244
245 int consumer_stop( mlt_consumer parent )
246 {
247 // Get the actual object
248 consumer_sdl this = parent->child;
249
250 if ( this->joined == 0 )
251 {
252 // Kill the thread and clean up
253 this->joined = 1;
254 this->running = 0;
255 pthread_join( this->thread, NULL );
256
257 // internal cleanup
258 if ( this->sdl_overlay != NULL )
259 SDL_FreeYUVOverlay( this->sdl_overlay );
260 this->sdl_overlay = NULL;
261
262 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "audio_off" ) )
263 {
264 pthread_mutex_lock( &this->audio_mutex );
265 pthread_cond_broadcast( &this->audio_cond );
266 pthread_mutex_unlock( &this->audio_mutex );
267 SDL_QuitSubSystem( SDL_INIT_AUDIO );
268 }
269
270 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" ) == 0 )
271 SDL_Quit( );
272
273 this->sdl_screen = NULL;
274 }
275
276 return 0;
277 }
278
279 int consumer_is_stopped( mlt_consumer parent )
280 {
281 consumer_sdl this = parent->child;
282 return !this->running;
283 }
284
285 static int sdl_lock_display( )
286 {
287 SDL_Surface *screen = SDL_GetVideoSurface( );
288 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
289 }
290
291 static void sdl_unlock_display( )
292 {
293 SDL_Surface *screen = SDL_GetVideoSurface( );
294 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
295 SDL_UnlockSurface( screen );
296 }
297
298 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
299 {
300 consumer_sdl this = udata;
301
302 // Get the volume
303 double volume = mlt_properties_get_double( this->properties, "volume" );
304
305 pthread_mutex_lock( &this->audio_mutex );
306
307 // Block until audio received
308 while ( this->running && len > this->audio_avail )
309 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
310
311 if ( this->audio_avail >= len )
312 {
313 // Place in the audio buffer
314 if ( volume != 1.0 )
315 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
316 else
317 memcpy( stream, this->audio_buffer, len );
318
319 // Remove len from the audio available
320 this->audio_avail -= len;
321
322 // Remove the samples
323 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
324 }
325 else
326 {
327 // Just to be safe, wipe the stream first
328 memset( stream, 0, len );
329
330 // Mix the audio
331 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
332
333 // No audio left
334 this->audio_avail = 0;
335 }
336
337 // We're definitely playing now
338 this->playing = 1;
339
340 pthread_cond_broadcast( &this->audio_cond );
341 pthread_mutex_unlock( &this->audio_mutex );
342 }
343
344 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
345 {
346 // Get the properties of this consumer
347 mlt_properties properties = this->properties;
348 mlt_audio_format afmt = mlt_audio_pcm;
349
350 // Set the preferred params of the test card signal
351 int channels = mlt_properties_get_int( properties, "channels" );
352 int frequency = mlt_properties_get_int( properties, "frequency" );
353 static int counter = 0;
354
355 int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
356
357 int16_t *pcm;
358 int bytes;
359
360 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
361 *duration = ( ( samples * 1000 ) / frequency );
362
363 if ( mlt_properties_get_int( properties, "audio_off" ) )
364 {
365 this->playing = 1;
366 init_audio = 1;
367 return init_audio;
368 }
369
370 if ( init_audio == 1 )
371 {
372 SDL_AudioSpec request;
373 SDL_AudioSpec got;
374
375 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
376
377 // specify audio format
378 memset( &request, 0, sizeof( SDL_AudioSpec ) );
379 this->playing = 0;
380 request.freq = frequency;
381 request.format = AUDIO_S16SYS;
382 request.channels = channels;
383 request.samples = audio_buffer;
384 request.callback = sdl_fill_audio;
385 request.userdata = (void *)this;
386 if ( SDL_OpenAudio( &request, &got ) != 0 )
387 {
388 fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
389 init_audio = 2;
390 }
391 else if ( got.size != 0 )
392 {
393 SDL_PauseAudio( 0 );
394 init_audio = 0;
395 }
396 }
397
398 if ( init_audio == 0 )
399 {
400 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
401 bytes = ( samples * channels * 2 );
402 pthread_mutex_lock( &this->audio_mutex );
403 while ( this->running && bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
404 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
405 if ( this->running )
406 {
407 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
408 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
409 else
410 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
411 this->audio_avail += bytes;
412 }
413 pthread_cond_broadcast( &this->audio_cond );
414 pthread_mutex_unlock( &this->audio_mutex );
415 }
416 else
417 {
418 this->playing = 1;
419 }
420
421 return init_audio;
422 }
423
424 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
425 {
426 // Get the properties of this consumer
427 mlt_properties properties = this->properties;
428
429 mlt_image_format vfmt = mlt_image_yuv422;
430 int width = this->width, height = this->height;
431 uint8_t *image;
432 int changed = 0;
433
434 int video_off = mlt_properties_get_int( properties, "video_off" );
435 int preview_off = mlt_properties_get_int( properties, "preview_off" );
436 mlt_image_format preview_format = mlt_properties_get_int( properties, "preview_format" );
437 int display_off = video_off | preview_off;
438
439 if ( this->running && display_off == 0 )
440 {
441 // Get the image, width and height
442 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
443 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "format", vfmt );
444 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
445
446 // Handle events
447 if ( this->sdl_screen != NULL )
448 {
449 SDL_Event event;
450
451 sdl_lock_display( );
452 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
453 sdl_unlock_display( );
454
455 while ( SDL_PollEvent( &event ) )
456 {
457 mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
458
459 switch( event.type )
460 {
461 case SDL_VIDEORESIZE:
462 this->window_width = event.resize.w;
463 this->window_height = event.resize.h;
464 changed = 1;
465 break;
466 case SDL_QUIT:
467 this->running = 0;
468 break;
469 case SDL_KEYDOWN:
470 {
471 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
472 char keyboard[ 2 ] = " ";
473 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
474 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
475 {
476 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
477 callback( producer, keyboard );
478 }
479 }
480 break;
481 }
482 }
483 }
484
485 sdl_lock_display();
486
487 if ( width != this->width || height != this->height )
488 {
489 if ( this->sdl_overlay != NULL )
490 SDL_FreeYUVOverlay( this->sdl_overlay );
491 this->sdl_overlay = NULL;
492 }
493
494 if ( this->running && ( this->sdl_screen == NULL || changed ) )
495 {
496 // Force an overlay recreation
497 if ( this->sdl_overlay != NULL )
498 SDL_FreeYUVOverlay( this->sdl_overlay );
499 this->sdl_overlay = NULL;
500
501 // open SDL window with video overlay, if possible
502 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
503 if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
504 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
505 }
506
507 if ( this->running )
508 {
509 // Determine window's new display aspect ratio
510 double this_aspect = ( double )this->window_width / this->window_height;
511
512 // Get the display aspect ratio
513 double display_ratio = mlt_properties_get_double( properties, "display_ratio" );
514
515 // Determine frame's display aspect ratio
516 double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
517
518 // Store the width and height received
519 this->width = width;
520 this->height = height;
521
522 // If using hardware scaler
523 if ( mlt_properties_get( properties, "rescale" ) != NULL &&
524 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
525 {
526 // Use hardware scaler to normalise display aspect ratio
527 this->rect.w = frame_aspect / this_aspect * this->window_width;
528 this->rect.h = this->window_height;
529 if ( this->rect.w > this->window_width )
530 {
531 this->rect.w = this->window_width;
532 this->rect.h = this_aspect / frame_aspect * this->window_height;
533 }
534 }
535 // Special case optimisation to negate odd effect of sample aspect ratio
536 // not corresponding exactly with image resolution.
537 else if ( (int)( this_aspect * 1000 ) == (int)( display_ratio * 1000 ) )
538 {
539 this->rect.w = this->window_width;
540 this->rect.h = this->window_height;
541 }
542 // Use hardware scaler to normalise sample aspect ratio
543 else if ( this->window_height * display_ratio > this->window_width )
544 {
545 this->rect.w = this->window_width;
546 this->rect.h = this->window_width / display_ratio;
547 }
548 else
549 {
550 this->rect.w = this->window_height * display_ratio;
551 this->rect.h = this->window_height;
552 }
553
554 this->rect.x = ( this->window_width - this->rect.w ) / 2;
555 this->rect.y = ( this->window_height - this->rect.h ) / 2;
556 this->rect.x -= this->rect.x % 2;
557
558 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
559 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
560 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
561 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
562
563 SDL_SetClipRect( this->sdl_screen, &this->rect );
564 }
565
566 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay == NULL )
567 {
568 SDL_SetClipRect( this->sdl_screen, &this->rect );
569 this->sdl_overlay = SDL_CreateYUVOverlay( width, height, SDL_YUY2_OVERLAY, this->sdl_screen );
570 }
571
572 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay != NULL )
573 {
574 this->buffer = this->sdl_overlay->pixels[ 0 ];
575 if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
576 {
577 if ( image != NULL )
578 memcpy( this->buffer, image, width * height * 2 );
579 SDL_UnlockYUVOverlay( this->sdl_overlay );
580 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
581 }
582 }
583
584 sdl_unlock_display();
585 }
586 else if ( this->running )
587 {
588 vfmt = preview_format == mlt_image_none ? mlt_image_rgb24a : preview_format;
589 if ( !video_off )
590 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
591 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "format", vfmt );
592 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
593 }
594
595 return 0;
596 }
597
598 static void *video_thread( void *arg )
599 {
600 // Identify the arg
601 consumer_sdl this = arg;
602
603 // Obtain time of thread start
604 struct timeval now;
605 int64_t start = 0;
606 int64_t elapsed = 0;
607 struct timespec tm;
608 mlt_frame next = NULL;
609 mlt_properties properties = NULL;
610 double speed = 0;
611
612 // Get real time flag
613 int real_time = mlt_properties_get_int( this->properties, "real_time" );
614
615 // Get the current time
616 gettimeofday( &now, NULL );
617
618 // Determine start time
619 start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
620
621 while ( this->running )
622 {
623 // Pop the next frame
624 pthread_mutex_lock( &this->video_mutex );
625 next = mlt_deque_pop_front( this->queue );
626 while ( next == NULL && this->running )
627 {
628 pthread_cond_wait( &this->video_cond, &this->video_mutex );
629 next = mlt_deque_pop_front( this->queue );
630 }
631 pthread_mutex_unlock( &this->video_mutex );
632
633 if ( !this->running || next == NULL ) break;
634
635 // Get the properties
636 properties = MLT_FRAME_PROPERTIES( next );
637
638 // Get the speed of the frame
639 speed = mlt_properties_get_double( properties, "_speed" );
640
641 // Get the current time
642 gettimeofday( &now, NULL );
643
644 // Get the elapsed time
645 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
646
647 // See if we have to delay the display of the current frame
648 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && this->running )
649 {
650 // Obtain the scheduled playout time
651 int64_t scheduled = mlt_properties_get_int( properties, "playtime" );
652
653 // Determine the difference between the elapsed time and the scheduled playout time
654 int64_t difference = scheduled - elapsed;
655
656 // Smooth playback a bit
657 if ( real_time && ( difference > 20000 && speed == 1.0 ) )
658 {
659 tm.tv_sec = difference / 1000000;
660 tm.tv_nsec = ( difference % 1000000 ) * 500;
661 nanosleep( &tm, NULL );
662 }
663
664 // Show current frame if not too old
665 if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( this->queue ) < 2 ) )
666 consumer_play_video( this, next );
667
668 // If the queue is empty, recalculate start to allow build up again
669 if ( real_time && ( mlt_deque_count( this->queue ) == 0 && speed == 1.0 ) )
670 {
671 gettimeofday( &now, NULL );
672 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
673 }
674 }
675
676 // This frame can now be closed
677 mlt_frame_close( next );
678 next = NULL;
679 }
680
681 if ( next != NULL )
682 mlt_frame_close( next );
683
684 mlt_consumer_stopped( &this->parent );
685
686 return NULL;
687 }
688
689 /** Threaded wrapper for pipe.
690 */
691
692 static void *consumer_thread( void *arg )
693 {
694 // Identify the arg
695 consumer_sdl this = arg;
696
697 // Get the consumer
698 mlt_consumer consumer = &this->parent;
699
700 // Convenience functionality
701 int terminate_on_pause = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "terminate_on_pause" );
702 int terminated = 0;
703
704 // Video thread
705 pthread_t thread;
706
707 // internal intialization
708 int init_audio = 1;
709 int init_video = 1;
710 mlt_frame frame = NULL;
711 mlt_properties properties = NULL;
712 int duration = 0;
713 int64_t playtime = 0;
714 struct timespec tm = { 0, 100000 };
715
716 // Loop until told not to
717 while( !terminated && this->running )
718 {
719 // Get a frame from the attached producer
720 frame = mlt_consumer_rt_frame( consumer );
721
722 // Check for termination
723 if ( terminate_on_pause && frame != NULL )
724 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
725
726 // Ensure that we have a frame
727 if ( frame != NULL )
728 {
729 // Get the frame properties
730 properties = MLT_FRAME_PROPERTIES( frame );
731
732 // Play audio
733 init_audio = consumer_play_audio( this, frame, init_audio, &duration );
734
735 // Determine the start time now
736 if ( this->playing && init_video )
737 {
738 // Create the video thread
739 pthread_create( &thread, NULL, video_thread, this );
740
741 // Video doesn't need to be initialised any more
742 init_video = 0;
743 }
744
745 // Set playtime for this frame
746 mlt_properties_set_int( properties, "playtime", playtime );
747
748 while ( this->running && mlt_deque_count( this->queue ) > 15 )
749 nanosleep( &tm, NULL );
750
751 // Push this frame to the back of the queue
752 pthread_mutex_lock( &this->video_mutex );
753 mlt_deque_push_back( this->queue, frame );
754 pthread_cond_broadcast( &this->video_cond );
755 pthread_mutex_unlock( &this->video_mutex );
756
757 // Calculate the next playtime
758 playtime += ( duration * 1000 );
759 }
760 }
761
762 this->running = 0;
763
764 // Kill the video thread
765 if ( init_video == 0 )
766 {
767 pthread_mutex_lock( &this->video_mutex );
768 pthread_cond_broadcast( &this->video_cond );
769 pthread_mutex_unlock( &this->video_mutex );
770 pthread_join( thread, NULL );
771 }
772
773 while( mlt_deque_count( this->queue ) )
774 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
775
776 this->sdl_screen = NULL;
777 this->audio_avail = 0;
778
779 return NULL;
780 }
781
782 static int consumer_get_dimensions( int *width, int *height )
783 {
784 int changed = 0;
785
786 // SDL windows manager structure
787 SDL_SysWMinfo wm;
788
789 // Specify the SDL Version
790 SDL_VERSION( &wm.version );
791
792 // Lock the display
793 //sdl_lock_display();
794
795 #ifndef __DARWIN__
796 // Get the wm structure
797 if ( SDL_GetWMInfo( &wm ) == 1 )
798 {
799 // Check that we have the X11 wm
800 if ( wm.subsystem == SDL_SYSWM_X11 )
801 {
802 // Get the SDL window
803 Window window = wm.info.x11.window;
804
805 // Get the display session
806 Display *display = wm.info.x11.display;
807
808 // Get the window attributes
809 XWindowAttributes attr;
810 XGetWindowAttributes( display, window, &attr );
811
812 // Determine whether window has changed
813 changed = *width != attr.width || *height != attr.height;
814
815 // Return width and height
816 *width = attr.width;
817 *height = attr.height;
818 }
819 }
820 #endif
821
822 // Unlock the display
823 //sdl_unlock_display();
824
825 return changed;
826 }
827
828 /** Callback to allow override of the close method.
829 */
830
831 static void consumer_close( mlt_consumer parent )
832 {
833 // Get the actual object
834 consumer_sdl this = parent->child;
835
836 // Stop the consumer
837 ///mlt_consumer_stop( parent );
838
839 // Now clean up the rest
840 mlt_consumer_close( parent );
841
842 // Close the queue
843 mlt_deque_close( this->queue );
844
845 // Destroy mutexes
846 pthread_mutex_destroy( &this->audio_mutex );
847 pthread_cond_destroy( &this->audio_cond );
848
849 // Finally clean up this
850 free( this );
851 }