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