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