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