bug fixes
[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 * 3 ];
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 float aspect_ratio;
50 int width;
51 int height;
52 int playing;
53 mlt_frame *queue;
54 int size;
55 int count;
56 int sdl_flags;
57 SDL_Surface *sdl_screen;
58 SDL_Overlay *sdl_overlay;
59 uint8_t *buffer;
60 };
61
62 /** Forward references to static functions.
63 */
64
65 static int consumer_start( mlt_consumer parent );
66 static int consumer_stop( mlt_consumer parent );
67 static int consumer_is_stopped( mlt_consumer parent );
68 static void consumer_close( mlt_consumer parent );
69 static void *consumer_thread( void * );
70 static int consumer_get_dimensions( int *width, int *height );
71
72 /** This is what will be called by the factory - anything can be passed in
73 via the argument, but keep it simple.
74 */
75
76 mlt_consumer consumer_sdl_init( char *arg )
77 {
78 // Create the consumer object
79 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
80
81 // If no malloc'd and consumer init ok
82 if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
83 {
84 // Get the parent consumer object
85 mlt_consumer parent = &this->parent;
86
87 // We have stuff to clean up, so override the close method
88 parent->close = consumer_close;
89
90 // get a handle on properties
91 mlt_service service = mlt_consumer_service( parent );
92 this->properties = mlt_service_properties( service );
93
94 // Set the default volume
95 mlt_properties_set_double( this->properties, "volume", 1.0 );
96
97 // This is the initialisation of the consumer
98 pthread_mutex_init( &this->audio_mutex, NULL );
99 pthread_cond_init( &this->audio_cond, NULL);
100
101 // Default scaler (for now we'll use nearest)
102 mlt_properties_set( this->properties, "rescale", "nearest" );
103
104 // Get aspect ratio
105 this->aspect_ratio = mlt_properties_get_double( this->properties, "aspect_ratio" );
106
107 // process actual param
108 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
109 {
110 this->width = mlt_properties_get_int( this->properties, "width" );
111 this->height = mlt_properties_get_int( this->properties, "height" );
112 }
113
114 // Default window size
115 this->window_width = (int)( (float)this->height * this->aspect_ratio ) + 1;
116 this->window_height = this->height;
117
118 // Set the sdl flags
119 this->sdl_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_HWACCEL | SDL_RESIZABLE;
120
121 // Allow thread to be started/stopped
122 parent->start = consumer_start;
123 parent->stop = consumer_stop;
124 parent->is_stopped = consumer_is_stopped;
125
126 // Return the consumer produced
127 return parent;
128 }
129
130 // malloc or consumer init failed
131 free( this );
132
133 // Indicate failure
134 return NULL;
135 }
136
137 int consumer_start( mlt_consumer parent )
138 {
139 consumer_sdl this = parent->child;
140
141 if ( !this->running )
142 {
143 this->running = 1;
144 pthread_create( &this->thread, NULL, consumer_thread, this );
145 }
146
147 return 0;
148 }
149
150 int consumer_stop( mlt_consumer parent )
151 {
152 // Get the actual object
153 consumer_sdl this = parent->child;
154
155 if ( this->running )
156 {
157 // Kill the thread and clean up
158 this->running = 0;
159
160 pthread_mutex_lock( &this->audio_mutex );
161 pthread_cond_broadcast( &this->audio_cond );
162 pthread_mutex_unlock( &this->audio_mutex );
163
164 pthread_join( this->thread, NULL );
165 }
166
167 return 0;
168 }
169
170 int consumer_is_stopped( mlt_consumer parent )
171 {
172 consumer_sdl this = parent->child;
173 return !this->running;
174 }
175
176 static int sdl_lock_display( )
177 {
178 SDL_Surface *screen = SDL_GetVideoSurface( );
179 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
180 }
181
182 static void sdl_unlock_display( )
183 {
184 SDL_Surface *screen = SDL_GetVideoSurface( );
185 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
186 SDL_UnlockSurface( screen );
187 }
188
189 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
190 {
191 consumer_sdl this = udata;
192
193 // Get the volume
194 float volume = mlt_properties_get_double( this->properties, "volume" );
195
196 pthread_mutex_lock( &this->audio_mutex );
197
198 // Block until audio received
199 while ( this->running && len > this->audio_avail )
200 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
201
202 if ( this->audio_avail >= len )
203 {
204 // Place in the audio buffer
205 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
206
207 // Remove len from the audio available
208 this->audio_avail -= len;
209
210 // Remove the samples
211 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
212 }
213 else
214 {
215 // Just to be safe, wipe the stream first
216 memset( stream, 0, len );
217
218 // Copy what we have into the stream
219 memcpy( stream, this->audio_buffer, this->audio_avail );
220
221 // Mix the audio
222 SDL_MixAudio( stream, stream, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
223
224 // No audio left
225 this->audio_avail = 0;
226 }
227
228 // We're definitely playing now
229 this->playing = 1;
230
231 pthread_cond_broadcast( &this->audio_cond );
232 pthread_mutex_unlock( &this->audio_mutex );
233 }
234
235 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio )
236 {
237 // Get the properties of this consumer
238 mlt_properties properties = this->properties;
239 mlt_audio_format afmt = mlt_audio_pcm;
240
241 // Set the preferred params of the test card signal
242 int channels = 2;
243 int frequency = 48000;
244 static int counter = 0;
245 if ( mlt_properties_get_int( properties, "frequency" ) != 0 )
246 frequency = mlt_properties_get_int( properties, "frequency" );
247
248 int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
249
250 int16_t *pcm;
251 int bytes;
252
253 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
254
255 if ( mlt_properties_get_int( properties, "audio_off" ) )
256 {
257 this->playing = 1;
258 return init_audio;
259 }
260
261 if ( init_audio == 1 )
262 {
263 SDL_AudioSpec request;
264 SDL_AudioSpec got;
265
266 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
267 SDL_EnableUNICODE( 1 );
268
269 // specify audio format
270 memset( &request, 0, sizeof( SDL_AudioSpec ) );
271 this->playing = 0;
272 request.freq = frequency;
273 request.format = AUDIO_S16;
274 request.channels = channels;
275 request.samples = 1024;
276 request.callback = sdl_fill_audio;
277 request.userdata = (void *)this;
278 if ( SDL_OpenAudio( &request, &got ) != 0 )
279 {
280 fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
281 init_audio = 2;
282 }
283 else if ( got.size != 0 )
284 {
285 SDL_PauseAudio( 0 );
286 init_audio = 0;
287 }
288 }
289
290 if ( init_audio == 0 )
291 {
292 bytes = ( samples * channels * 2 );
293 pthread_mutex_lock( &this->audio_mutex );
294 while ( bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
295 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
296 mlt_properties properties = mlt_frame_properties( frame );
297 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
298 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
299 else
300 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
301 this->audio_avail += bytes;
302 pthread_cond_broadcast( &this->audio_cond );
303 pthread_mutex_unlock( &this->audio_mutex );
304 }
305 else
306 {
307 this->playing = 1;
308 }
309
310 return init_audio;
311 }
312
313 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
314 {
315 // Get the properties of this consumer
316 mlt_properties properties = this->properties;
317
318 mlt_image_format vfmt = mlt_image_yuv422;
319 int width = this->width, height = this->height;
320 uint8_t *image;
321 int changed = 0;
322
323 if ( mlt_properties_get_int( properties, "video_off" ) )
324 {
325 mlt_frame_close( frame );
326 return 0;
327 }
328
329 if ( this->count == this->size )
330 {
331 this->size += 25;
332 this->queue = realloc( this->queue, sizeof( mlt_frame ) * this->size );
333 }
334 this->queue[ this->count ++ ] = frame;
335
336 if ( this->playing )
337 {
338 // We're working on the oldest frame now
339 frame = this->queue[ 0 ];
340
341 // Shunt the frames in the queue down
342 int i = 0;
343 for ( i = 1; i < this->count; i ++ )
344 this->queue[ i - 1 ] = this->queue[ i ];
345 this->count --;
346
347 // Get the image, width and height
348 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
349
350 if ( this->sdl_screen != NULL )
351 {
352 SDL_Event event;
353
354 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
355
356 while ( SDL_PollEvent( &event ) )
357 {
358 switch( event.type )
359 {
360 case SDL_VIDEORESIZE:
361 this->window_width = event.resize.w;
362 this->window_height = event.resize.h;
363 changed = 1;
364 break;
365 case SDL_KEYDOWN:
366 {
367 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
368 char keyboard[ 2 ] = " ";
369 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
370 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
371 {
372 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
373 callback( producer, keyboard );
374 }
375 }
376 break;
377 }
378 }
379
380 }
381
382 if ( width != this->width || height != this->height )
383 {
384 this->width = width;
385 this->height = height;
386 changed = 1;
387 }
388
389 if ( this->sdl_screen == NULL || changed )
390 {
391 double aspect_ratio = mlt_frame_get_aspect_ratio( frame );
392 float display_aspect_ratio = (float)width / (float)height;
393 SDL_Rect rect;
394
395 if ( mlt_properties_get_double( properties, "aspect_ratio" ) )
396 aspect_ratio = mlt_properties_get_double( properties, "aspect_ratio" );
397
398 if ( aspect_ratio == 1 )
399 {
400 rect.w = this->window_width;
401 rect.h = this->window_height;
402 }
403 else if ( this->window_width < this->window_height * aspect_ratio )
404 {
405 rect.w = this->window_width;
406 rect.h = this->window_width / aspect_ratio;
407 }
408 else
409 {
410 rect.w = this->window_height * aspect_ratio;
411 rect.h = this->window_height;
412 }
413
414 if ( mlt_properties_get_int( properties, "scale_overlay" ) )
415 {
416 if ( ( float )rect.w * display_aspect_ratio < this->window_width )
417 rect.w = ( int )( ( float )rect.w * display_aspect_ratio );
418 else if ( ( float )rect.h * display_aspect_ratio < this->window_height )
419 rect.h = ( int )( ( float )rect.h * display_aspect_ratio );
420 }
421
422 rect.x = ( this->window_width - rect.w ) / 2;
423 rect.y = ( this->window_height - rect.h ) / 2;
424
425 // Force an overlay recreation
426 if ( this->sdl_overlay != NULL )
427 SDL_FreeYUVOverlay( this->sdl_overlay );
428
429 // open SDL window with video overlay, if possible
430 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
431
432 if ( this->sdl_screen != NULL )
433 {
434 SDL_SetClipRect( this->sdl_screen, &rect );
435
436 sdl_lock_display();
437 this->sdl_overlay = SDL_CreateYUVOverlay( this->width - (this->width % 4), this->height- (this->height % 2 ), SDL_YUY2_OVERLAY, this->sdl_screen );
438 sdl_unlock_display();
439 }
440 }
441
442 if ( this->sdl_screen != NULL && this->sdl_overlay != NULL )
443 {
444 this->buffer = this->sdl_overlay->pixels[ 0 ];
445 if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
446 {
447 mlt_resize_yuv422( this->buffer, this->width - (this->width % 4 ), this->height- (this->height % 2 ), image, width, height );
448 SDL_UnlockYUVOverlay( this->sdl_overlay );
449 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
450 }
451 }
452 }
453 else
454 {
455 frame = NULL;
456 }
457
458 // Close the frame
459 if ( frame != NULL )
460 mlt_frame_close( frame );
461
462 if ( this->count )
463 {
464 // Tell the producers about our scale relative to the normalisation
465 mlt_properties_set_double( mlt_frame_properties( this->queue[ this->count - 1 ] ), "consumer_scale",
466 ( double )height / mlt_properties_get_double( properties, "height" ) );
467 mlt_frame_get_image( this->queue[ this->count - 1 ], &image, &vfmt, &width, &height, 0 );
468 }
469
470 return 0;
471 }
472
473 /** Threaded wrapper for pipe.
474 */
475
476 static void *consumer_thread( void *arg )
477 {
478 // Identify the arg
479 consumer_sdl this = arg;
480
481 // Get the consumer
482 mlt_consumer consumer = &this->parent;
483
484 // internal intialization
485 int init_audio = 1;
486
487 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
488 {
489 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
490 return NULL;
491 }
492
493 // Loop until told not to
494 while( this->running )
495 {
496 // Get a frame from the attached producer
497 mlt_frame frame = mlt_consumer_get_frame( consumer );
498
499 // Ensure that we have a frame
500 if ( frame != NULL )
501 {
502 // SDL adapts display aspect, but set this so pixel aspect can be normalised
503 // mlt_properties_set_double( mlt_frame_properties( frame ), "consumer_aspect_ratio",
504 // mlt_frame_get_aspect_ratio( frame ) );
505
506 init_audio = consumer_play_audio( this, frame, init_audio );
507 consumer_play_video( this, frame );
508 }
509 }
510
511 // internal cleanup
512 if ( init_audio == 0 )
513 SDL_AudioQuit( );
514 if ( this->sdl_overlay != NULL )
515 SDL_FreeYUVOverlay( this->sdl_overlay );
516 SDL_Quit( );
517
518 this->sdl_screen = NULL;
519 this->sdl_overlay = NULL;
520 this->audio_avail = 0;
521
522 return NULL;
523 }
524
525 static int consumer_get_dimensions( int *width, int *height )
526 {
527 int changed = 0;
528
529 // SDL windows manager structure
530 SDL_SysWMinfo wm;
531
532 // Specify the SDL Version
533 SDL_VERSION( &wm.version );
534
535 // Get the wm structure
536 if ( SDL_GetWMInfo( &wm ) == 1 )
537 {
538 // Check that we have the X11 wm
539 if ( wm.subsystem == SDL_SYSWM_X11 )
540 {
541 // Get the SDL window
542 Window window = wm.info.x11.window;
543
544 // Get the display session
545 Display *display = wm.info.x11.display;
546
547 // Get the window attributes
548 XWindowAttributes attr;
549 XGetWindowAttributes( display, window, &attr );
550
551 // Determine whether window has changed
552 changed = *width != attr.width || *height != attr.height;
553
554 // Return width and height
555 *width = attr.width;
556 *height = attr.height;
557 }
558 }
559
560 return changed;
561 }
562
563 /** Callback to allow override of the close method.
564 */
565
566 static void consumer_close( mlt_consumer parent )
567 {
568 // Get the actual object
569 consumer_sdl this = parent->child;
570
571 // Stop the consumer
572 mlt_consumer_stop( parent );
573
574 // Destroy mutexes
575 pthread_mutex_destroy( &this->audio_mutex );
576 pthread_cond_destroy( &this->audio_cond );
577
578 // Now clean up the rest
579 mlt_consumer_close( parent );
580
581 // Finally clean up this
582 free( this );
583 }
584