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>
31 /** This classes definition.
34 typedef struct consumer_sdl_s
*consumer_sdl
;
38 struct mlt_consumer_s parent
;
39 mlt_properties properties
;
43 uint8_t audio_buffer
[ 4096 * 3 ];
45 pthread_mutex_t audio_mutex
;
46 pthread_cond_t audio_cond
;
54 SDL_Surface
*sdl_screen
;
55 SDL_Overlay
*sdl_overlay
;
59 /** Forward references to static functions.
62 static int consumer_start( mlt_consumer parent
);
63 static int consumer_stop( mlt_consumer parent
);
64 static int consumer_is_stopped( mlt_consumer parent
);
65 static void consumer_close( mlt_consumer parent
);
66 static void *consumer_thread( void * );
67 static int consumer_get_dimensions( int *width
, int *height
);
69 /** This is what will be called by the factory - anything can be passed in
70 via the argument, but keep it simple.
73 mlt_consumer
consumer_sdl_init( char *arg
)
75 // Create the consumer object
76 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
78 // If no malloc'd and consumer init ok
79 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
82 this->queue
= mlt_deque_init( );
84 // Get the parent consumer object
85 mlt_consumer parent
= &this->parent
;
87 // We have stuff to clean up, so override the close method
88 parent
->close
= consumer_close
;
90 // get a handle on properties
91 mlt_service service
= mlt_consumer_service( parent
);
92 this->properties
= mlt_service_properties( service
);
94 // Set the default volume
95 mlt_properties_set_double( this->properties
, "volume", 1.0 );
97 // This is the initialisation of the consumer
98 pthread_mutex_init( &this->audio_mutex
, NULL
);
99 pthread_cond_init( &this->audio_cond
, NULL
);
101 // Default scaler (for now we'll use nearest)
102 mlt_properties_set( this->properties
, "rescale", "nearest" );
104 // Default progressive true
105 mlt_properties_set_int( this->properties
, "progressive", 1 );
108 this->aspect_ratio
= mlt_properties_get_double( this->properties
, "aspect_ratio" );
110 // process actual param
111 if ( arg
== NULL
|| sscanf( arg
, "%dx%d", &this->width
, &this->height
) != 2 )
113 this->width
= mlt_properties_get_int( this->properties
, "width" );
114 this->height
= mlt_properties_get_int( this->properties
, "height" );
117 // Default window size
118 this->window_width
= (int)( (float)this->height
* this->aspect_ratio
) + 1;
119 this->window_height
= this->height
;
122 this->sdl_flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
| SDL_RESIZABLE
;
124 // Allow thread to be started/stopped
125 parent
->start
= consumer_start
;
126 parent
->stop
= consumer_stop
;
127 parent
->is_stopped
= consumer_is_stopped
;
129 // Return the consumer produced
133 // malloc or consumer init failed
140 int consumer_start( mlt_consumer parent
)
142 consumer_sdl
this = parent
->child
;
144 if ( !this->running
)
147 pthread_create( &this->thread
, NULL
, consumer_thread
, this );
153 int consumer_stop( mlt_consumer parent
)
155 // Get the actual object
156 consumer_sdl
this = parent
->child
;
160 // Kill the thread and clean up
163 pthread_mutex_lock( &this->audio_mutex
);
164 pthread_cond_broadcast( &this->audio_cond
);
165 pthread_mutex_unlock( &this->audio_mutex
);
167 pthread_join( this->thread
, NULL
);
173 int consumer_is_stopped( mlt_consumer parent
)
175 consumer_sdl
this = parent
->child
;
176 return !this->running
;
179 static int sdl_lock_display( )
181 SDL_Surface
*screen
= SDL_GetVideoSurface( );
182 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
185 static void sdl_unlock_display( )
187 SDL_Surface
*screen
= SDL_GetVideoSurface( );
188 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
189 SDL_UnlockSurface( screen
);
192 static void sdl_fill_audio( void *udata
, uint8_t *stream
, int len
)
194 consumer_sdl
this = udata
;
197 float volume
= mlt_properties_get_double( this->properties
, "volume" );
199 pthread_mutex_lock( &this->audio_mutex
);
201 // Block until audio received
202 while ( this->running
&& len
> this->audio_avail
)
203 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
205 if ( this->audio_avail
>= len
)
207 // Place in the audio buffer
208 SDL_MixAudio( stream
, this->audio_buffer
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
210 // Remove len from the audio available
211 this->audio_avail
-= len
;
213 // Remove the samples
214 memmove( this->audio_buffer
, this->audio_buffer
+ len
, this->audio_avail
);
218 // Just to be safe, wipe the stream first
219 memset( stream
, 0, len
);
221 // Copy what we have into the stream
222 memcpy( stream
, this->audio_buffer
, this->audio_avail
);
225 SDL_MixAudio( stream
, stream
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
228 this->audio_avail
= 0;
231 // We're definitely playing now
234 pthread_cond_broadcast( &this->audio_cond
);
235 pthread_mutex_unlock( &this->audio_mutex
);
238 static int consumer_play_audio( consumer_sdl
this, mlt_frame frame
, int init_audio
)
240 // Get the properties of this consumer
241 mlt_properties properties
= this->properties
;
242 mlt_audio_format afmt
= mlt_audio_pcm
;
244 // Set the preferred params of the test card signal
246 int frequency
= 48000;
247 static int counter
= 0;
248 if ( mlt_properties_get_int( properties
, "frequency" ) != 0 )
249 frequency
= mlt_properties_get_int( properties
, "frequency" );
251 int samples
= mlt_sample_calculator( mlt_properties_get_double( this->properties
, "fps" ), frequency
, counter
++ );
256 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
258 if ( mlt_properties_get_int( properties
, "audio_off" ) )
264 if ( init_audio
== 1 )
266 SDL_AudioSpec request
;
269 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
270 SDL_EnableUNICODE( 1 );
272 // specify audio format
273 memset( &request
, 0, sizeof( SDL_AudioSpec
) );
275 request
.freq
= frequency
;
276 request
.format
= AUDIO_S16
;
277 request
.channels
= channels
;
278 request
.samples
= 1024;
279 request
.callback
= sdl_fill_audio
;
280 request
.userdata
= (void *)this;
281 if ( SDL_OpenAudio( &request
, &got
) != 0 )
283 fprintf( stderr
, "SDL failed to open audio: %s\n", SDL_GetError() );
286 else if ( got
.size
!= 0 )
293 if ( init_audio
== 0 )
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
);
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
);
316 static int consumer_play_video( consumer_sdl
this, mlt_frame frame
)
318 // Get the properties of this consumer
319 mlt_properties properties
= this->properties
;
321 mlt_image_format vfmt
= mlt_image_yuv422
;
322 int width
= this->width
, height
= this->height
;
326 if ( mlt_properties_get_int( properties
, "video_off" ) )
328 mlt_frame_close( frame
);
332 // Push this frame to the back of the queue
333 mlt_deque_push_back( this->queue
, frame
);
337 // Get the frame at the front of the queue
338 frame
= mlt_deque_pop_front( this->queue
);
340 // Get the image, width and height
341 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
343 if ( this->sdl_screen
!= NULL
)
347 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
349 while ( SDL_PollEvent( &event
) )
353 case SDL_VIDEORESIZE
:
354 this->window_width
= event
.resize
.w
;
355 this->window_height
= event
.resize
.h
;
360 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
361 char keyboard
[ 2 ] = " ";
362 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
363 if ( callback
!= NULL
&& producer
!= NULL
&& event
.key
.keysym
.unicode
< 0x80 && event
.key
.keysym
.unicode
> 0 )
365 keyboard
[ 0 ] = ( char )event
.key
.keysym
.unicode
;
366 callback( producer
, keyboard
);
375 if ( width
!= this->width
|| height
!= this->height
)
378 this->height
= height
;
382 if ( this->sdl_screen
== NULL
|| changed
)
384 double aspect_ratio
= mlt_frame_get_aspect_ratio( frame
);
385 float display_aspect_ratio
= (float)width
/ (float)height
;
388 if ( mlt_properties_get_double( properties
, "aspect_ratio" ) )
389 aspect_ratio
= mlt_properties_get_double( properties
, "aspect_ratio" );
391 if ( aspect_ratio
== 1 )
393 rect
.w
= this->window_width
;
394 rect
.h
= this->window_height
;
396 else if ( this->window_width
< this->window_height
* aspect_ratio
)
398 rect
.w
= this->window_width
;
399 rect
.h
= this->window_width
/ aspect_ratio
;
403 rect
.w
= this->window_height
* aspect_ratio
;
404 rect
.h
= this->window_height
;
407 if ( mlt_properties_get_int( properties
, "scale_overlay" ) )
409 if ( ( float )rect
.w
* display_aspect_ratio
< this->window_width
)
410 rect
.w
= ( int )( ( float )rect
.w
* display_aspect_ratio
);
411 else if ( ( float )rect
.h
* display_aspect_ratio
< this->window_height
)
412 rect
.h
= ( int )( ( float )rect
.h
* display_aspect_ratio
);
415 rect
.x
= ( this->window_width
- rect
.w
) / 2;
416 rect
.y
= ( this->window_height
- rect
.h
) / 2;
418 // Force an overlay recreation
419 if ( this->sdl_overlay
!= NULL
)
420 SDL_FreeYUVOverlay( this->sdl_overlay
);
422 // open SDL window with video overlay, if possible
423 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
425 if ( this->sdl_screen
!= NULL
)
427 SDL_SetClipRect( this->sdl_screen
, &rect
);
430 this->sdl_overlay
= SDL_CreateYUVOverlay( this->width
- (this->width
% 4), this->height
- (this->height
% 2 ), SDL_YUY2_OVERLAY
, this->sdl_screen
);
431 sdl_unlock_display();
435 if ( this->sdl_screen
!= NULL
&& this->sdl_overlay
!= NULL
)
437 this->buffer
= this->sdl_overlay
->pixels
[ 0 ];
438 if ( SDL_LockYUVOverlay( this->sdl_overlay
) >= 0 )
440 memcpy( this->buffer
, image
, width
* height
* 2 );
441 SDL_UnlockYUVOverlay( this->sdl_overlay
);
442 SDL_DisplayYUVOverlay( this->sdl_overlay
, &this->sdl_screen
->clip_rect
);
453 mlt_frame_close( frame
);
455 if ( mlt_deque_count( this->queue
) )
457 // Tell the producers about our scale relative to the normalisation
458 frame
= mlt_deque_peek_front( this->queue
);
459 mlt_properties_set_double( mlt_frame_properties( frame
), "consumer_scale",
460 ( double )height
/ mlt_properties_get_double( properties
, "height" ) );
461 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
467 /** Threaded wrapper for pipe.
470 static void *consumer_thread( void *arg
)
473 consumer_sdl
this = arg
;
476 mlt_consumer consumer
= &this->parent
;
478 // internal intialization
481 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) < 0 )
483 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
487 // Loop until told not to
488 while( this->running
)
490 // Get a frame from the attached producer
491 mlt_frame frame
= mlt_consumer_get_frame( consumer
);
493 // Ensure that we have a frame
496 // SDL adapts display aspect, but set this so pixel aspect can be normalised
497 // mlt_properties_set_double( mlt_frame_properties( frame ), "consumer_aspect_ratio",
498 // mlt_frame_get_aspect_ratio( frame ) );
500 init_audio
= consumer_play_audio( this, frame
, init_audio
);
501 consumer_play_video( this, frame
);
506 if ( init_audio
== 0 )
508 if ( this->sdl_overlay
!= NULL
)
509 SDL_FreeYUVOverlay( this->sdl_overlay
);
512 while( mlt_deque_count( this->queue
) )
513 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
515 this->sdl_screen
= NULL
;
516 this->sdl_overlay
= NULL
;
517 this->audio_avail
= 0;
522 static int consumer_get_dimensions( int *width
, int *height
)
526 // SDL windows manager structure
529 // Specify the SDL Version
530 SDL_VERSION( &wm
.version
);
532 // Get the wm structure
533 if ( SDL_GetWMInfo( &wm
) == 1 )
535 // Check that we have the X11 wm
536 if ( wm
.subsystem
== SDL_SYSWM_X11
)
538 // Get the SDL window
539 Window window
= wm
.info
.x11
.window
;
541 // Get the display session
542 Display
*display
= wm
.info
.x11
.display
;
544 // Get the window attributes
545 XWindowAttributes attr
;
546 XGetWindowAttributes( display
, window
, &attr
);
548 // Determine whether window has changed
549 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
551 // Return width and height
553 *height
= attr
.height
;
560 /** Callback to allow override of the close method.
563 static void consumer_close( mlt_consumer parent
)
565 // Get the actual object
566 consumer_sdl
this = parent
->child
;
569 mlt_consumer_stop( parent
);
572 pthread_mutex_destroy( &this->audio_mutex
);
573 pthread_cond_destroy( &this->audio_cond
);
575 // Now clean up the rest
576 mlt_consumer_close( parent
);
578 // Finally clean up this