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