SDL a/v sync issues [incomplete]
[melted] / src / modules / sdl / consumer_sdl.c
1 /*
2 * consumer_sdl.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "consumer_sdl.h"
22 #include <framework/mlt_frame.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <pthread.h>
27 #include <SDL/SDL.h>
28 #include <SDL/SDL_syswm.h>
29
30 /** This classes definition.
31 */
32
33 typedef struct consumer_sdl_s *consumer_sdl;
34
35 struct consumer_sdl_s
36 {
37 struct mlt_consumer_s parent;
38 mlt_properties properties;
39 int format;
40 int video;
41 pthread_t thread;
42 int running;
43 uint8_t audio_buffer[ 4096 * 6 ];
44 int audio_avail;
45 pthread_mutex_t audio_mutex;
46 pthread_cond_t audio_cond;
47 int window_width;
48 int window_height;
49 int width;
50 int height;
51 int playing;
52 mlt_frame *queue;
53 int size;
54 int count;
55 };
56
57 /** Forward references to static functions.
58 */
59
60 static void consumer_close( mlt_consumer parent );
61 static void *consumer_thread( void * );
62 static int consumer_get_dimensions( int *width, int *height );
63
64 /** This is what will be called by the factory - anything can be passed in
65 via the argument, but keep it simple.
66 */
67
68 mlt_consumer consumer_sdl_init( char *arg )
69 {
70 // Create the consumer object
71 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
72
73 // If no malloc'd and consumer init ok
74 if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
75 {
76 // Get the parent consumer object
77 mlt_consumer parent = &this->parent;
78
79 // We have stuff to clean up, so override the close method
80 parent->close = consumer_close;
81
82 // get a handle on properties
83 mlt_service service = mlt_consumer_service( parent );
84 this->properties = mlt_service_properties( service );
85
86 // Set the default volume
87 mlt_properties_set_double( this->properties, "volume", 1.0 );
88
89 // This is the initialisation of the consumer
90 this->running = 1;
91 pthread_mutex_init( &this->audio_mutex, NULL );
92 pthread_cond_init( &this->audio_cond, NULL);
93
94 // process actual param
95 if ( arg == NULL || !strcmp( arg, "PAL" ) )
96 {
97 this->width = 720;
98 this->height = 576;
99 }
100 else if ( !strcmp( arg, "NTSC" ) )
101 {
102 this->width = 720;
103 this->height = 480;
104 }
105 else if ( sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
106 {
107 this->width = 720;
108 this->height = 576;
109 }
110
111 // Create the the thread
112 pthread_create( &this->thread, NULL, consumer_thread, this );
113
114 // Return the consumer produced
115 return parent;
116 }
117
118 // malloc or consumer init failed
119 free( this );
120
121 // Indicate failure
122 return NULL;
123 }
124
125 static int sdl_lock_display( )
126 {
127 SDL_Surface *screen = SDL_GetVideoSurface( );
128 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
129 }
130
131 static void sdl_unlock_display( )
132 {
133 SDL_Surface *screen = SDL_GetVideoSurface( );
134 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
135 SDL_UnlockSurface( screen );
136 }
137
138 void sdl_fill_audio( void *udata, uint8_t *stream, int len )
139 {
140 consumer_sdl this = udata;
141
142 // Get the volume
143 float volume = mlt_properties_get_double( this->properties, "volume" );
144
145 pthread_mutex_lock( &this->audio_mutex );
146
147 // Block until audio received
148 while ( this->running && len > this->audio_avail )
149 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
150
151 if ( this->audio_avail >= len )
152 {
153 // Place in the audio buffer
154 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
155
156 // Remove len from the audio available
157 this->audio_avail -= len;
158
159 // Remove the samples
160 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
161 }
162 else
163 {
164 // Just to be safe, wipe the stream first
165 memset( stream, 0, len );
166
167 // Copy what we have into the stream
168 memcpy( stream, this->audio_buffer, this->audio_avail );
169
170 // Mix the audio
171 SDL_MixAudio( stream, stream, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
172
173 // No audio left
174 this->audio_avail = 0;
175 }
176
177 // We're definitely playing now
178 this->playing = 1;
179
180 pthread_cond_broadcast( &this->audio_cond );
181 pthread_mutex_unlock( &this->audio_mutex );
182 }
183
184 /** Threaded wrapper for pipe.
185 */
186
187 static void *consumer_thread( void *arg )
188 {
189 // Identify the arg
190 consumer_sdl this = arg;
191
192 // Get the consumer
193 mlt_consumer consumer = &this->parent;
194
195 // Get the service assoicated to the consumer
196 mlt_service service = mlt_consumer_service( consumer );
197
198 // Get the properties of this consumer
199 mlt_properties properties = this->properties;
200
201 // Define a frame pointer
202 mlt_frame frame;
203
204 // internal intialization
205 int sdl_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_HWACCEL | SDL_RESIZABLE;
206 SDL_Surface *sdl_screen = NULL;
207 SDL_Overlay *sdl_overlay = NULL;
208 uint8_t *buffer = NULL;
209 int init_audio = 1;
210 int bytes;
211
212 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
213 {
214 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
215 return NULL;
216 }
217
218 // Loop until told not to
219 while( this->running )
220 {
221 // Get a frame from the service (should never return anything other than 0)
222 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
223 {
224 mlt_image_format vfmt = mlt_image_yuv422;
225 int width = this->width, height = this->height;
226 uint8_t *image;
227
228 mlt_audio_format afmt = mlt_audio_pcm;
229 int channels;
230 int samples;
231 int frequency;
232 int16_t *pcm;
233 int changed = 0;
234
235 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
236 if ( init_audio == 1 )
237 {
238 SDL_AudioSpec request;
239
240 // specify audio format
241 request.freq = frequency;
242 request.format = AUDIO_S16;
243 request.channels = channels;
244 request.samples = 2048;
245 request.callback = sdl_fill_audio;
246 request.userdata = (void *)this;
247 if ( SDL_OpenAudio( &request, NULL ) < 0 )
248 {
249 fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
250 break;
251 }
252 SDL_PauseAudio( 0 );
253 init_audio = 0;
254 }
255 bytes = ( samples * channels * 2 );
256 pthread_mutex_lock( &this->audio_mutex );
257 while ( bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
258 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
259 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
260 this->audio_avail += bytes;
261 pthread_cond_broadcast( &this->audio_cond );
262 pthread_mutex_unlock( &this->audio_mutex );
263
264 if ( this->count == this->size )
265 {
266 this->size += 25;
267 this->queue = realloc( this->queue, sizeof( mlt_frame ) * this->size );
268 }
269 this->queue[ this->count ++ ] = frame;
270
271 if ( this->playing )
272 {
273 // We're working on the oldest frame now
274 frame = this->queue[ 0 ];
275
276 // Shunt the frames in the queue down
277 int i = 0;
278 for ( i = 1; i < this->count; i ++ )
279 this->queue[ i - 1 ] = this->queue[ i ];
280 this->count --;
281
282 // Get the image, width and height
283 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
284
285 if ( sdl_screen != NULL )
286 {
287 SDL_Event event;
288
289 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
290
291 while ( SDL_PollEvent( &event ) )
292 {
293 switch( event.type )
294 {
295 case SDL_VIDEORESIZE:
296 this->window_width = event.resize.w;
297 this->window_height = event.resize.h;
298 changed = 1;
299 break;
300 case SDL_KEYDOWN:
301 {
302 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
303 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
304 if ( callback != NULL && producer != NULL )
305 callback( producer, SDL_GetKeyName(event.key.keysym.sym) );
306 }
307 break;
308 }
309 }
310
311 if ( width != this->width || height != this->height )
312 {
313 this->width = width;
314 this->height = height;
315 changed = 1;
316 }
317 }
318
319 if ( sdl_screen == NULL || changed )
320 {
321 double aspect_ratio = mlt_frame_get_aspect_ratio( frame );
322
323 if ( mlt_properties_get_double( properties, "aspect_ratio" ) )
324 aspect_ratio = mlt_properties_get_double( properties, "aspect_ratio" );
325
326 if ( this->window_width == 0 || this->window_height == 0 )
327 {
328 this->window_width = width;
329 this->window_height = height;
330 }
331
332 // open SDL window with video overlay, if possible
333 sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, sdl_flags );
334
335 if ( sdl_screen != NULL )
336 {
337 SDL_Rect rect;
338 if ( this->window_width < this->window_height * aspect_ratio )
339 {
340 rect.w = this->window_width;
341 rect.h = this->window_width / aspect_ratio;
342 }
343 else
344 {
345 rect.w = this->window_height * aspect_ratio;
346 rect.h = this->window_height;
347 }
348
349 rect.x = ( this->window_width - rect.w ) / 2;
350 rect.y = ( this->window_height - rect.h ) / 2;
351
352 SDL_SetClipRect( sdl_screen, &rect );
353
354 // Force an overlay recreation
355 if ( sdl_overlay != NULL )
356 SDL_FreeYUVOverlay( sdl_overlay );
357 sdl_lock_display();
358 sdl_overlay = SDL_CreateYUVOverlay( this->width, this->height, SDL_YUY2_OVERLAY, sdl_screen );
359 sdl_unlock_display();
360 }
361 }
362
363 if ( sdl_screen != NULL && sdl_overlay != NULL )
364 {
365 buffer = sdl_overlay->pixels[ 0 ];
366 if ( sdl_lock_display() )
367 {
368 if ( SDL_LockYUVOverlay( sdl_overlay ) >= 0 )
369 {
370 mlt_resize_yuv422( buffer, this->width, this->height, image, width, height );
371 SDL_UnlockYUVOverlay( sdl_overlay );
372 SDL_DisplayYUVOverlay( sdl_overlay, &sdl_screen->clip_rect );
373 }
374 sdl_unlock_display();
375 }
376 }
377 }
378 else
379 {
380 frame = NULL;
381 }
382
383 // Close the frame
384 if ( frame != NULL )
385 mlt_frame_close( frame );
386
387 if ( this->count )
388 mlt_frame_get_image( this->queue[ this->count - 1 ], &image, &vfmt, &width, &height, 0 );
389 }
390 else
391 {
392 break;
393 }
394 } // while
395
396 // internal cleanup
397 if ( init_audio == 0 )
398 SDL_AudioQuit( );
399 if ( sdl_overlay != NULL )
400 SDL_FreeYUVOverlay( sdl_overlay );
401 SDL_Quit( );
402
403 return NULL;
404 }
405
406 static int consumer_get_dimensions( int *width, int *height )
407 {
408 int changed = 0;
409
410 // SDL windows manager structure
411 SDL_SysWMinfo wm;
412
413 // Specify the SDL Version
414 SDL_VERSION( &wm.version );
415
416 // Get the wm structure
417 if ( SDL_GetWMInfo( &wm ) == 1 )
418 {
419 // Check that we have the X11 wm
420 if ( wm.subsystem == SDL_SYSWM_X11 )
421 {
422 // Get the SDL window
423 Window window = wm.info.x11.window;
424
425 // Get the display session
426 Display *display = wm.info.x11.display;
427
428 // Get the window attributes
429 XWindowAttributes attr;
430 XGetWindowAttributes( display, window, &attr );
431
432 // Determine whether window has changed
433 changed = *width != attr.width || *height != attr.height;
434
435 // Return width and height
436 *width = attr.width;
437 *height = attr.height;
438 }
439 }
440
441 return changed;
442 }
443
444 /** Callback to allow override of the close method.
445 */
446
447 static void consumer_close( mlt_consumer parent )
448 {
449 // Get the actual object
450 consumer_sdl this = parent->child;
451
452 // Kill the thread and clean up
453 this->running = 0;
454
455 pthread_mutex_lock( &this->audio_mutex );
456 pthread_cond_broadcast( &this->audio_cond );
457 pthread_mutex_unlock( &this->audio_mutex );
458
459 pthread_join( this->thread, NULL );
460 pthread_mutex_destroy( &this->audio_mutex );
461 pthread_cond_destroy( &this->audio_cond );
462
463 // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
464 parent->close = NULL;
465 mlt_consumer_close( parent );
466
467 // Finally clean up this
468 free( this );
469 }
470