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