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
;
55 SDL_Surface
*sdl_screen
;
56 SDL_Overlay
*sdl_overlay
;
60 /** Forward references to static functions.
63 static int consumer_start( mlt_consumer parent
);
64 static int consumer_stop( mlt_consumer parent
);
65 static int consumer_is_stopped( mlt_consumer parent
);
66 static void consumer_close( mlt_consumer parent
);
67 static void *consumer_thread( void * );
68 static int consumer_get_dimensions( int *width
, int *height
);
70 /** This is what will be called by the factory - anything can be passed in
71 via the argument, but keep it simple.
74 mlt_consumer
consumer_sdl_init( char *arg
)
76 // Create the consumer object
77 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
79 // If no malloc'd and consumer init ok
80 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
83 this->queue
= mlt_deque_init( );
85 // Get the parent consumer object
86 mlt_consumer parent
= &this->parent
;
88 // We have stuff to clean up, so override the close method
89 parent
->close
= consumer_close
;
91 // get a handle on properties
92 mlt_service service
= mlt_consumer_service( parent
);
93 this->properties
= mlt_service_properties( service
);
95 // Set the default volume
96 mlt_properties_set_double( this->properties
, "volume", 1.0 );
98 // This is the initialisation of the consumer
99 pthread_mutex_init( &this->audio_mutex
, NULL
);
100 pthread_cond_init( &this->audio_cond
, NULL
);
102 // Default scaler (for now we'll use nearest)
103 mlt_properties_set( this->properties
, "rescale", "nearest" );
105 // Default buffer for low latency
106 mlt_properties_set_int( this->properties
, "buffer", 2 );
108 // Default progressive true
109 mlt_properties_set_int( this->properties
, "progressive", 0 );
112 this->aspect_ratio
= mlt_properties_get_double( this->properties
, "aspect_ratio" );
114 // process actual param
115 if ( arg
== NULL
|| sscanf( arg
, "%dx%d", &this->width
, &this->height
) != 2 )
117 this->width
= mlt_properties_get_int( this->properties
, "width" );
118 this->height
= mlt_properties_get_int( this->properties
, "height" );
121 // Default window size
122 this->window_width
= (int)( (float)this->height
* 4.0/3.0 + 0.5);
123 this->window_height
= this->height
;
126 this->sdl_flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
| SDL_RESIZABLE
;
128 // Allow thread to be started/stopped
129 parent
->start
= consumer_start
;
130 parent
->stop
= consumer_stop
;
131 parent
->is_stopped
= consumer_is_stopped
;
133 // Return the consumer produced
137 // malloc or consumer init failed
144 int consumer_start( mlt_consumer parent
)
146 consumer_sdl
this = parent
->child
;
148 if ( !this->running
)
151 pthread_create( &this->thread
, NULL
, consumer_thread
, this );
157 int consumer_stop( mlt_consumer parent
)
159 // Get the actual object
160 consumer_sdl
this = parent
->child
;
164 // Kill the thread and clean up
167 pthread_mutex_lock( &this->audio_mutex
);
168 pthread_cond_broadcast( &this->audio_cond
);
169 pthread_mutex_unlock( &this->audio_mutex
);
171 pthread_join( this->thread
, NULL
);
177 int consumer_is_stopped( mlt_consumer parent
)
179 consumer_sdl
this = parent
->child
;
180 return !this->running
;
183 static int sdl_lock_display( )
185 SDL_Surface
*screen
= SDL_GetVideoSurface( );
186 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
189 static void sdl_unlock_display( )
191 SDL_Surface
*screen
= SDL_GetVideoSurface( );
192 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
193 SDL_UnlockSurface( screen
);
196 static void sdl_fill_audio( void *udata
, uint8_t *stream
, int len
)
198 consumer_sdl
this = udata
;
201 float volume
= mlt_properties_get_double( this->properties
, "volume" );
203 pthread_mutex_lock( &this->audio_mutex
);
205 // Block until audio received
206 while ( this->running
&& len
> this->audio_avail
)
207 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
209 if ( this->audio_avail
>= len
)
211 // Place in the audio buffer
212 SDL_MixAudio( stream
, this->audio_buffer
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
214 // Remove len from the audio available
215 this->audio_avail
-= len
;
217 // Remove the samples
218 memmove( this->audio_buffer
, this->audio_buffer
+ len
, this->audio_avail
);
222 // Just to be safe, wipe the stream first
223 memset( stream
, 0, len
);
225 // Copy what we have into the stream
226 memcpy( stream
, this->audio_buffer
, this->audio_avail
);
229 SDL_MixAudio( stream
, stream
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
232 this->audio_avail
= 0;
235 // We're definitely playing now
238 pthread_cond_broadcast( &this->audio_cond
);
239 pthread_mutex_unlock( &this->audio_mutex
);
242 static int consumer_play_audio( consumer_sdl
this, mlt_frame frame
, int init_audio
, int *duration
)
244 // Get the properties of this consumer
245 mlt_properties properties
= this->properties
;
246 mlt_audio_format afmt
= mlt_audio_pcm
;
248 // Set the preferred params of the test card signal
250 int frequency
= 48000;
251 static int counter
= 0;
252 if ( mlt_properties_get_int( properties
, "frequency" ) != 0 )
253 frequency
= mlt_properties_get_int( properties
, "frequency" );
255 int samples
= mlt_sample_calculator( mlt_properties_get_double( this->properties
, "fps" ), frequency
, counter
++ );
260 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
261 *duration
= ( ( samples
* 1000 ) / frequency
);
263 if ( mlt_properties_get_int( properties
, "audio_off" ) )
270 if ( init_audio
== 1 )
272 SDL_AudioSpec request
;
275 // specify audio format
276 memset( &request
, 0, sizeof( SDL_AudioSpec
) );
278 request
.freq
= frequency
;
279 request
.format
= AUDIO_S16
;
280 request
.channels
= channels
;
281 request
.samples
= 1024;
282 request
.callback
= sdl_fill_audio
;
283 request
.userdata
= (void *)this;
284 if ( SDL_OpenAudio( &request
, &got
) != 0 )
286 fprintf( stderr
, "SDL failed to open audio: %s\n", SDL_GetError() );
289 else if ( got
.size
!= 0 )
296 if ( init_audio
== 0 )
298 mlt_properties properties
= mlt_frame_properties( frame
);
299 bytes
= ( samples
* channels
* 2 );
300 pthread_mutex_lock( &this->audio_mutex
);
301 while ( bytes
> ( sizeof( this->audio_buffer
) - this->audio_avail
) )
302 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
303 if ( mlt_properties_get_double( properties
, "_speed" ) == 1 )
304 memcpy( &this->audio_buffer
[ this->audio_avail
], pcm
, bytes
);
306 memset( &this->audio_buffer
[ this->audio_avail
], 0, bytes
);
307 this->audio_avail
+= bytes
;
308 pthread_cond_broadcast( &this->audio_cond
);
309 pthread_mutex_unlock( &this->audio_mutex
);
319 static int consumer_play_video( consumer_sdl
this, mlt_frame frame
)
321 // Get the properties of this consumer
322 mlt_properties properties
= this->properties
;
324 mlt_image_format vfmt
= mlt_image_yuv422
;
325 int width
= this->width
, height
= this->height
;
329 if ( mlt_properties_get_int( properties
, "video_off" ) == 0 )
331 // Get the image, width and height
332 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
335 if ( this->sdl_screen
!= NULL
)
339 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
341 while ( SDL_PollEvent( &event
) )
345 case SDL_VIDEORESIZE
:
346 this->window_width
= event
.resize
.w
;
347 this->window_height
= event
.resize
.h
;
355 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
356 char keyboard
[ 2 ] = " ";
357 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
358 if ( callback
!= NULL
&& producer
!= NULL
&& event
.key
.keysym
.unicode
< 0x80 && event
.key
.keysym
.unicode
> 0 )
360 keyboard
[ 0 ] = ( char )event
.key
.keysym
.unicode
;
361 callback( producer
, keyboard
);
369 if ( width
!= this->width
|| height
!= this->height
)
372 this->height
= height
;
376 if ( this->sdl_screen
== NULL
|| changed
)
378 double aspect_ratio
= ( float )this->aspect_ratio
* this->width
/ this->height
;
379 float display_aspect_ratio
= ( float )width
/ height
;
381 if ( mlt_properties_get( properties
, "rescale" ) != NULL
&&
382 !strcmp( mlt_properties_get( properties
, "rescale" ), "none" ) )
383 aspect_ratio
= mlt_frame_get_aspect_ratio( frame
) * mlt_properties_get_int(mlt_frame_properties(frame
),"width") / mlt_properties_get_int(mlt_frame_properties(frame
),"height");
385 if ( aspect_ratio
== 1 )
387 rect
.w
= this->window_width
;
388 rect
.h
= this->window_height
;
390 else if ( this->window_width
< this->window_height
* aspect_ratio
)
392 rect
.w
= this->window_width
;
393 rect
.h
= this->window_width
/ aspect_ratio
+ 1;
397 rect
.w
= this->window_height
* aspect_ratio
+ 1;
398 rect
.h
= this->window_height
;
401 if ( mlt_properties_get_int( properties
, "scale_overlay" ) )
403 if ( ( float )rect
.w
* display_aspect_ratio
< this->window_width
)
404 rect
.w
= ( int )( ( float )rect
.w
* display_aspect_ratio
);
405 else if ( ( float )rect
.h
* display_aspect_ratio
< this->window_height
)
406 rect
.h
= ( int )( ( float )rect
.h
* display_aspect_ratio
);
409 rect
.x
= ( this->window_width
- rect
.w
) / 2;
410 rect
.y
= ( this->window_height
- rect
.h
) / 2;
412 // Force an overlay recreation
413 if ( this->sdl_overlay
!= NULL
)
414 SDL_FreeYUVOverlay( this->sdl_overlay
);
416 // open SDL window with video overlay, if possible
417 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
419 if ( this->sdl_screen
!= NULL
)
421 SDL_SetClipRect( this->sdl_screen
, &rect
);
424 this->sdl_overlay
= SDL_CreateYUVOverlay( this->width
- (this->width
% 4), this->height
- (this->height
% 2 ), SDL_YUY2_OVERLAY
, this->sdl_screen
);
425 sdl_unlock_display();
429 if ( this->sdl_screen
!= NULL
&& this->sdl_overlay
!= NULL
)
431 this->buffer
= this->sdl_overlay
->pixels
[ 0 ];
432 if ( SDL_LockYUVOverlay( this->sdl_overlay
) >= 0 )
434 memcpy( this->buffer
, image
, width
* height
* 2 );
435 SDL_UnlockYUVOverlay( this->sdl_overlay
);
436 SDL_DisplayYUVOverlay( this->sdl_overlay
, &this->sdl_screen
->clip_rect
);
442 mlt_frame_close( frame
);
447 /** Threaded wrapper for pipe.
450 static void *consumer_thread( void *arg
)
453 consumer_sdl
this = arg
;
456 mlt_consumer consumer
= &this->parent
;
458 // internal intialization
461 // Obtain time of thread start
466 int64_t playtime
= 0;
468 mlt_frame next
= NULL
;
469 mlt_frame frame
= NULL
;
471 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) < 0 )
473 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
477 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
478 SDL_EnableUNICODE( 1 );
480 // Loop until told not to
481 while( this->running
)
483 // Get a frame from the attached producer
484 frame
= mlt_consumer_rt_frame( consumer
);
486 // Ensure that we have a frame
490 init_audio
= consumer_play_audio( this, frame
, init_audio
, &duration
);
494 // Get the current time
495 gettimeofday( &now
, NULL
);
497 // Determine elapsed time
499 start
= ( int64_t )now
.tv_sec
* 1000000 + now
.tv_usec
;
501 elapsed
= ( ( int64_t )now
.tv_sec
* 1000000 + now
.tv_usec
) - start
;
504 // Set playtime for this frame
505 mlt_properties_set_position( mlt_frame_properties( frame
), "playtime", playtime
);
507 // Push this frame to the back of the queue
508 mlt_deque_push_back( this->queue
, frame
);
510 // Calculate the next playtime
511 playtime
+= ( duration
* 1000 );
516 // Pop the next frame
517 next
= mlt_deque_pop_front( this->queue
);
519 // See if we have to delay the display of the current frame
520 if ( next
!= NULL
&& mlt_properties_get_int( mlt_frame_properties( next
), "rendered" ) == 1 )
522 mlt_position scheduled
= mlt_properties_get_position( mlt_frame_properties( next
), "playtime" ) + 5000;
523 if ( scheduled
> elapsed
&& mlt_deque_count( this->queue
) > 25 )
525 tm
.tv_sec
= ( scheduled
- elapsed
) / 1000000;
526 tm
.tv_nsec
= ( ( scheduled
- elapsed
) % 1000000 ) * 1000;
527 nanosleep( &tm
, NULL
);
529 // Show current frame
530 consumer_play_video( this, next
);
532 else if ( scheduled
> elapsed
)
535 mlt_deque_push_front( this->queue
, next
);
539 // Show current frame
540 consumer_play_video( this, next
);
545 // This is an unrendered frame - just close it
546 mlt_frame_close( next
);
552 if ( init_audio
== 0 )
554 if ( this->sdl_overlay
!= NULL
)
555 SDL_FreeYUVOverlay( this->sdl_overlay
);
558 while( mlt_deque_count( this->queue
) )
559 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
561 this->sdl_screen
= NULL
;
562 this->sdl_overlay
= NULL
;
563 this->audio_avail
= 0;
568 static int consumer_get_dimensions( int *width
, int *height
)
572 // SDL windows manager structure
575 // Specify the SDL Version
576 SDL_VERSION( &wm
.version
);
578 // Get the wm structure
579 if ( SDL_GetWMInfo( &wm
) == 1 )
581 // Check that we have the X11 wm
582 if ( wm
.subsystem
== SDL_SYSWM_X11
)
584 // Get the SDL window
585 Window window
= wm
.info
.x11
.window
;
587 // Get the display session
588 Display
*display
= wm
.info
.x11
.display
;
590 // Get the window attributes
591 XWindowAttributes attr
;
592 XGetWindowAttributes( display
, window
, &attr
);
594 // Determine whether window has changed
595 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
597 // Return width and height
599 *height
= attr
.height
;
606 /** Callback to allow override of the close method.
609 static void consumer_close( mlt_consumer parent
)
611 // Get the actual object
612 consumer_sdl
this = parent
->child
;
615 mlt_consumer_stop( parent
);
618 pthread_mutex_destroy( &this->audio_mutex
);
619 pthread_cond_destroy( &this->audio_cond
);
621 // Now clean up the rest
622 mlt_consumer_close( parent
);
624 // Finally clean up this