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