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