2 * consumer_sdl.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
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.
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.
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.
21 #include "consumer_sdl.h"
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_deque.h>
29 #include <SDL/SDL_syswm.h>
32 /** This classes definition.
35 typedef struct consumer_sdl_s
*consumer_sdl
;
39 struct mlt_consumer_s parent
;
40 mlt_properties properties
;
44 uint8_t audio_buffer
[ 4096 * 19 ];
46 pthread_mutex_t audio_mutex
;
47 pthread_cond_t audio_cond
;
56 SDL_Surface
*sdl_screen
;
57 SDL_Overlay
*sdl_overlay
;
61 /** Forward references to static functions.
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
);
71 /** This is what will be called by the factory - anything can be passed in
72 via the argument, but keep it simple.
75 mlt_consumer
consumer_sdl_init( char *arg
)
77 // Create the consumer object
78 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
80 // If no malloc'd and consumer init ok
81 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
84 this->queue
= mlt_deque_init( );
86 // Get the parent consumer object
87 mlt_consumer parent
= &this->parent
;
89 // We have stuff to clean up, so override the close method
90 parent
->close
= consumer_close
;
92 // get a handle on properties
93 mlt_service service
= mlt_consumer_service( parent
);
94 this->properties
= mlt_service_properties( service
);
96 // Set the default volume
97 mlt_properties_set_double( this->properties
, "volume", 1.0 );
99 // This is the initialisation of the consumer
100 pthread_mutex_init( &this->audio_mutex
, NULL
);
101 pthread_cond_init( &this->audio_cond
, NULL
);
103 // Default scaler (for now we'll use nearest)
104 mlt_properties_set( this->properties
, "rescale", "nearest" );
106 // Default buffer for low latency
107 mlt_properties_set_int( this->properties
, "buffer", 1 );
109 // Default progressive true
110 mlt_properties_set_int( this->properties
, "progressive", 0 );
112 // Get sample aspect ratio
113 this->aspect_ratio
= mlt_properties_get_double( this->properties
, "aspect_ratio" );
115 // Default display aspect ratio
116 this->display_aspect
= 4.0 / 3.0;
118 // process actual param
119 if ( arg
== NULL
|| sscanf( arg
, "%dx%d", &this->width
, &this->height
) != 2 )
121 this->width
= mlt_properties_get_int( this->properties
, "width" );
122 this->height
= mlt_properties_get_int( this->properties
, "height" );
124 // Default window size
125 this->window_width
= ( float )this->height
* this->display_aspect
+ 0.5;
126 this->window_height
= this->height
;
130 if ( (int)( ( float )this->width
/ this->height
* 1000 ) !=
131 (int)( this->display_aspect
* 1000 ) )
134 this->display_aspect
= ( float )this->width
/ this->height
;
135 this->aspect_ratio
= 1.0;
136 mlt_properties_set_double( this->properties
, "aspect_ratio", this->aspect_ratio
);
140 this->window_width
= this->width
;
141 this->window_height
= this->height
;
145 this->sdl_flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
| SDL_RESIZABLE
;
147 // Allow thread to be started/stopped
148 parent
->start
= consumer_start
;
149 parent
->stop
= consumer_stop
;
150 parent
->is_stopped
= consumer_is_stopped
;
152 // Return the consumer produced
156 // malloc or consumer init failed
163 int consumer_start( mlt_consumer parent
)
165 consumer_sdl
this = parent
->child
;
167 if ( !this->running
)
169 pthread_attr_t thread_attributes
;
173 // Inherit the scheduling priority
174 pthread_attr_init( &thread_attributes
);
175 pthread_attr_setinheritsched( &thread_attributes
, PTHREAD_INHERIT_SCHED
);
177 pthread_create( &this->thread
, &thread_attributes
, consumer_thread
, this );
183 int consumer_stop( mlt_consumer parent
)
185 // Get the actual object
186 consumer_sdl
this = parent
->child
;
190 // Kill the thread and clean up
193 pthread_mutex_lock( &this->audio_mutex
);
194 pthread_cond_broadcast( &this->audio_cond
);
195 pthread_mutex_unlock( &this->audio_mutex
);
197 pthread_join( this->thread
, NULL
);
203 int consumer_is_stopped( mlt_consumer parent
)
205 consumer_sdl
this = parent
->child
;
206 return !this->running
;
209 static int sdl_lock_display( )
211 SDL_Surface
*screen
= SDL_GetVideoSurface( );
212 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
215 static void sdl_unlock_display( )
217 SDL_Surface
*screen
= SDL_GetVideoSurface( );
218 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
219 SDL_UnlockSurface( screen
);
222 static void sdl_fill_audio( void *udata
, uint8_t *stream
, int len
)
224 consumer_sdl
this = udata
;
227 float volume
= mlt_properties_get_double( this->properties
, "volume" );
229 pthread_mutex_lock( &this->audio_mutex
);
231 // Block until audio received
232 while ( this->running
&& len
> this->audio_avail
)
233 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
235 if ( this->audio_avail
>= len
)
237 // Place in the audio buffer
238 SDL_MixAudio( stream
, this->audio_buffer
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
240 // Remove len from the audio available
241 this->audio_avail
-= len
;
243 // Remove the samples
244 memmove( this->audio_buffer
, this->audio_buffer
+ len
, this->audio_avail
);
248 // Just to be safe, wipe the stream first
249 memset( stream
, 0, len
);
251 // Copy what we have into the stream
252 memcpy( stream
, this->audio_buffer
, this->audio_avail
);
255 SDL_MixAudio( stream
, stream
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
258 this->audio_avail
= 0;
261 // We're definitely playing now
264 pthread_cond_broadcast( &this->audio_cond
);
265 pthread_mutex_unlock( &this->audio_mutex
);
268 static int consumer_play_audio( consumer_sdl
this, mlt_frame frame
, int init_audio
, int *duration
)
270 // Get the properties of this consumer
271 mlt_properties properties
= this->properties
;
272 mlt_audio_format afmt
= mlt_audio_pcm
;
274 // Set the preferred params of the test card signal
275 int channels
= mlt_properties_get_int( properties
, "channels" );
276 int frequency
= mlt_properties_get_int( properties
, "frequency" );
277 static int counter
= 0;
279 int samples
= mlt_sample_calculator( mlt_properties_get_double( this->properties
, "fps" ), frequency
, counter
++ );
284 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
285 *duration
= ( ( samples
* 1000 ) / frequency
);
287 if ( mlt_properties_get_int( properties
, "audio_off" ) )
294 if ( init_audio
== 1 )
296 SDL_AudioSpec request
;
299 // specify audio format
300 memset( &request
, 0, sizeof( SDL_AudioSpec
) );
302 request
.freq
= frequency
;
303 request
.format
= AUDIO_S16
;
304 request
.channels
= channels
;
305 request
.samples
= 1024;
306 request
.callback
= sdl_fill_audio
;
307 request
.userdata
= (void *)this;
308 if ( SDL_OpenAudio( &request
, &got
) != 0 )
310 fprintf( stderr
, "SDL failed to open audio: %s\n", SDL_GetError() );
313 else if ( got
.size
!= 0 )
320 if ( init_audio
== 0 )
322 mlt_properties properties
= mlt_frame_properties( frame
);
323 bytes
= ( samples
* channels
* 2 );
324 pthread_mutex_lock( &this->audio_mutex
);
325 while ( bytes
> ( sizeof( this->audio_buffer
) - this->audio_avail
) )
326 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
327 if ( mlt_properties_get_double( properties
, "_speed" ) == 1 )
328 memcpy( &this->audio_buffer
[ this->audio_avail
], pcm
, bytes
);
330 memset( &this->audio_buffer
[ this->audio_avail
], 0, bytes
);
331 this->audio_avail
+= bytes
;
332 pthread_cond_broadcast( &this->audio_cond
);
333 pthread_mutex_unlock( &this->audio_mutex
);
343 static int consumer_play_video( consumer_sdl
this, mlt_frame frame
)
345 // Get the properties of this consumer
346 mlt_properties properties
= this->properties
;
348 mlt_image_format vfmt
= mlt_image_yuv422
;
349 int width
= this->width
, height
= this->height
;
353 if ( mlt_properties_get_int( properties
, "video_off" ) == 0 )
355 // Get the image, width and height
356 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
359 if ( this->sdl_screen
!= NULL
)
363 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
365 while ( SDL_PollEvent( &event
) )
369 case SDL_VIDEORESIZE
:
370 this->window_width
= event
.resize
.w
;
371 this->window_height
= event
.resize
.h
;
379 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
380 char keyboard
[ 2 ] = " ";
381 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
382 if ( callback
!= NULL
&& producer
!= NULL
&& event
.key
.keysym
.unicode
< 0x80 && event
.key
.keysym
.unicode
> 0 )
384 keyboard
[ 0 ] = ( char )event
.key
.keysym
.unicode
;
385 callback( producer
, keyboard
);
393 if ( width
!= this->width
|| height
!= this->height
)
396 this->height
= height
;
400 if ( this->sdl_screen
== NULL
|| changed
)
404 // Determine frame's display aspect ratio
405 float frame_aspect
= mlt_frame_get_aspect_ratio( frame
) * this->width
/ this->height
;
407 // Determine window's new display aspect ratio
408 float this_aspect
= ( float )this->window_width
/ this->window_height
;
410 // If using hardware scaler
411 if ( mlt_properties_get( properties
, "rescale" ) != NULL
&&
412 !strcmp( mlt_properties_get( properties
, "rescale" ), "none" ) )
414 // Special case optimisation to negate odd effect of sample aspect ratio
415 // not corresponding exactly with image resolution.
416 if ( ( (int)( this_aspect
* 1000 ) == (int)( this->display_aspect
* 1000 ) ) &&
417 ( (int)( mlt_frame_get_aspect_ratio( frame
) * 1000 ) == (int)( this->aspect_ratio
* 1000 ) ) )
419 rect
.w
= this->window_width
;
420 rect
.h
= this->window_height
;
424 // Use hardware scaler to normalise display aspect ratio
425 rect
.w
= frame_aspect
/ this_aspect
* this->window_width
+ 0.5;
426 rect
.h
= this->window_height
;
427 if ( rect
.w
> this->window_width
)
429 rect
.w
= this->window_width
;
430 rect
.h
= this_aspect
/ frame_aspect
* this->window_height
+ 0.5;
434 // Special case optimisation to negate odd effect of sample aspect ratio
435 // not corresponding exactly with image resolution.
436 else if ( (int)( this_aspect
* 1000 ) == (int)( this->display_aspect
* 1000 ) )
438 rect
.w
= this->window_width
;
439 rect
.h
= this->window_height
;
441 // Use hardware scaler to normalise sample aspect ratio
442 else if ( this->window_height
* frame_aspect
> this->window_width
)
444 rect
.w
= this->window_width
;
445 rect
.h
= this->window_width
/ frame_aspect
+ 0.5;
449 rect
.w
= this->window_height
* frame_aspect
+ 0.5;
450 rect
.h
= this->window_height
;
453 rect
.x
= ( this->window_width
- rect
.w
) / 2;
454 rect
.y
= ( this->window_height
- rect
.h
) / 2;
456 // Force an overlay recreation
457 if ( this->sdl_overlay
!= NULL
)
458 SDL_FreeYUVOverlay( this->sdl_overlay
);
460 // open SDL window with video overlay, if possible
461 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
463 if ( this->sdl_screen
!= NULL
)
465 SDL_SetClipRect( this->sdl_screen
, &rect
);
468 this->sdl_overlay
= SDL_CreateYUVOverlay( this->width
- (this->width
% 4), this->height
- (this->height
% 2 ), SDL_YUY2_OVERLAY
, this->sdl_screen
);
469 sdl_unlock_display();
473 if ( this->sdl_screen
!= NULL
&& this->sdl_overlay
!= NULL
)
475 this->buffer
= this->sdl_overlay
->pixels
[ 0 ];
476 if ( SDL_LockYUVOverlay( this->sdl_overlay
) >= 0 )
478 memcpy( this->buffer
, image
, width
* height
* 2 );
479 SDL_UnlockYUVOverlay( this->sdl_overlay
);
480 SDL_DisplayYUVOverlay( this->sdl_overlay
, &this->sdl_screen
->clip_rect
);
486 mlt_frame_close( frame
);
491 /** Threaded wrapper for pipe.
494 static void *consumer_thread( void *arg
)
497 consumer_sdl
this = arg
;
500 mlt_consumer consumer
= &this->parent
;
502 // internal intialization
505 // Obtain time of thread start
510 int64_t playtime
= 0;
512 mlt_frame next
= NULL
;
513 mlt_frame frame
= NULL
;
515 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) < 0 )
517 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
521 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
522 SDL_EnableUNICODE( 1 );
524 // Loop until told not to
525 while( this->running
)
527 // Get a frame from the attached producer
528 frame
= mlt_consumer_rt_frame( consumer
);
530 // Ensure that we have a frame
534 init_audio
= consumer_play_audio( this, frame
, init_audio
, &duration
);
538 // Get the current time
539 gettimeofday( &now
, NULL
);
541 // Determine elapsed time
543 start
= ( int64_t )now
.tv_sec
* 1000000 + now
.tv_usec
;
545 elapsed
= ( ( int64_t )now
.tv_sec
* 1000000 + now
.tv_usec
) - start
;
548 // Set playtime for this frame
549 mlt_properties_set_position( mlt_frame_properties( frame
), "playtime", playtime
);
551 // Push this frame to the back of the queue
552 mlt_deque_push_back( this->queue
, frame
);
554 // Calculate the next playtime
555 playtime
+= ( duration
* 1000 );
560 // Pop the next frame
561 next
= mlt_deque_pop_front( this->queue
);
563 // See if we have to delay the display of the current frame
564 if ( next
!= NULL
&& mlt_properties_get_int( mlt_frame_properties( next
), "rendered" ) == 1 )
566 mlt_position scheduled
= mlt_properties_get_position( mlt_frame_properties( next
), "playtime" ) + 5000;
567 if ( scheduled
> elapsed
&& mlt_deque_count( this->queue
) > 25 )
569 tm
.tv_sec
= ( scheduled
- elapsed
) / 1000000;
570 tm
.tv_nsec
= ( ( scheduled
- elapsed
) % 1000000 ) * 1000;
571 nanosleep( &tm
, NULL
);
573 // Show current frame
574 consumer_play_video( this, next
);
576 else if ( scheduled
> elapsed
)
579 mlt_deque_push_front( this->queue
, next
);
583 // Show current frame
584 consumer_play_video( this, next
);
589 // This is an unrendered frame - just close it
590 mlt_frame_close( next
);
596 if ( init_audio
== 0 )
598 if ( this->sdl_overlay
!= NULL
)
599 SDL_FreeYUVOverlay( this->sdl_overlay
);
602 while( mlt_deque_count( this->queue
) )
603 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
605 this->sdl_screen
= NULL
;
606 this->sdl_overlay
= NULL
;
607 this->audio_avail
= 0;
612 static int consumer_get_dimensions( int *width
, int *height
)
616 // SDL windows manager structure
619 // Specify the SDL Version
620 SDL_VERSION( &wm
.version
);
622 // Get the wm structure
623 if ( SDL_GetWMInfo( &wm
) == 1 )
625 // Check that we have the X11 wm
626 if ( wm
.subsystem
== SDL_SYSWM_X11
)
628 // Get the SDL window
629 Window window
= wm
.info
.x11
.window
;
631 // Get the display session
632 Display
*display
= wm
.info
.x11
.display
;
634 // Get the window attributes
635 XWindowAttributes attr
;
636 XGetWindowAttributes( display
, window
, &attr
);
638 // Determine whether window has changed
639 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
641 // Return width and height
643 *height
= attr
.height
;
650 /** Callback to allow override of the close method.
653 static void consumer_close( mlt_consumer parent
)
655 // Get the actual object
656 consumer_sdl
this = parent
->child
;
659 mlt_consumer_stop( parent
);
662 pthread_mutex_destroy( &this->audio_mutex
);
663 pthread_cond_destroy( &this->audio_cond
);
665 // Now clean up the rest
666 mlt_consumer_close( parent
);
668 // Finally clean up this