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