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