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>
28 #include <SDL/SDL_syswm.h>
30 /** This classes definition.
33 typedef struct consumer_sdl_s
*consumer_sdl
;
37 struct mlt_consumer_s parent
;
38 mlt_properties properties
;
43 uint8_t audio_buffer
[ 4096 * 3 ];
45 pthread_mutex_t audio_mutex
;
46 pthread_cond_t audio_cond
;
57 SDL_Surface
*sdl_screen
;
58 SDL_Overlay
*sdl_overlay
;
62 /** Forward references to static functions.
65 static int consumer_start( mlt_consumer parent
);
66 static int consumer_stop( mlt_consumer parent
);
67 static int consumer_is_stopped( mlt_consumer parent
);
68 static void consumer_close( mlt_consumer parent
);
69 static void *consumer_thread( void * );
70 static int consumer_get_dimensions( int *width
, int *height
);
72 /** This is what will be called by the factory - anything can be passed in
73 via the argument, but keep it simple.
76 mlt_consumer
consumer_sdl_init( char *arg
)
78 // Create the consumer object
79 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
81 // If no malloc'd and consumer init ok
82 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
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
);
102 mlt_properties_set_double( this->properties
, "fps", 25 );
104 // process actual param
105 if ( arg
== NULL
|| !strcmp( arg
, "PAL" ) )
110 else if ( !strcmp( arg
, "NTSC" ) )
114 mlt_properties_set_double( this->properties
, "fps", 29.97 );
116 else if ( sscanf( arg
, "%dx%d", &this->width
, &this->height
) != 2 )
122 // Default window size and aspect ratio
123 this->aspect_ratio
= 4.0 / 3.0;
124 this->window_width
= (int)( (float)this->height
* this->aspect_ratio
) + 1;
125 this->window_height
= this->height
;
128 this->sdl_flags
= SDL_HWSURFACE
| SDL_DOUBLEBUF
| SDL_HWACCEL
| SDL_RESIZABLE
;
130 // Allow thread to be started/stopped
131 parent
->start
= consumer_start
;
132 parent
->stop
= consumer_stop
;
133 parent
->is_stopped
= consumer_is_stopped
;
135 // Return the consumer produced
139 // malloc or consumer init failed
146 int consumer_start( mlt_consumer parent
)
148 consumer_sdl
this = parent
->child
;
150 if ( !this->running
)
153 pthread_create( &this->thread
, NULL
, consumer_thread
, this );
159 int consumer_stop( mlt_consumer parent
)
161 // Get the actual object
162 consumer_sdl
this = parent
->child
;
166 // Kill the thread and clean up
169 pthread_mutex_lock( &this->audio_mutex
);
170 pthread_cond_broadcast( &this->audio_cond
);
171 pthread_mutex_unlock( &this->audio_mutex
);
173 pthread_join( this->thread
, NULL
);
179 int consumer_is_stopped( mlt_consumer parent
)
181 consumer_sdl
this = parent
->child
;
182 return !this->running
;
185 static int sdl_lock_display( )
187 SDL_Surface
*screen
= SDL_GetVideoSurface( );
188 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
191 static void sdl_unlock_display( )
193 SDL_Surface
*screen
= SDL_GetVideoSurface( );
194 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
195 SDL_UnlockSurface( screen
);
198 static void sdl_fill_audio( void *udata
, uint8_t *stream
, int len
)
200 consumer_sdl
this = udata
;
203 float volume
= mlt_properties_get_double( this->properties
, "volume" );
205 pthread_mutex_lock( &this->audio_mutex
);
207 // Block until audio received
208 while ( this->running
&& len
> this->audio_avail
)
209 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
211 if ( this->audio_avail
>= len
)
213 // Place in the audio buffer
214 SDL_MixAudio( stream
, this->audio_buffer
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
216 // Remove len from the audio available
217 this->audio_avail
-= len
;
219 // Remove the samples
220 memmove( this->audio_buffer
, this->audio_buffer
+ len
, this->audio_avail
);
224 // Just to be safe, wipe the stream first
225 memset( stream
, 0, len
);
227 // Copy what we have into the stream
228 memcpy( stream
, this->audio_buffer
, this->audio_avail
);
231 SDL_MixAudio( stream
, stream
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
234 this->audio_avail
= 0;
237 // We're definitely playing now
240 pthread_cond_broadcast( &this->audio_cond
);
241 pthread_mutex_unlock( &this->audio_mutex
);
244 static int consumer_play_audio( consumer_sdl
this, mlt_frame frame
, int init_audio
)
246 // Get the properties of this consumer
247 mlt_properties properties
= this->properties
;
248 mlt_audio_format afmt
= mlt_audio_pcm
;
250 // Set the preferred params of the test card signal
252 int frequency
= 48000;
253 static int counter
= 0;
254 if ( mlt_properties_get_int( properties
, "frequency" ) != 0 )
255 frequency
= mlt_properties_get_int( properties
, "frequency" );
257 int samples
= mlt_sample_calculator( mlt_properties_get_double( this->properties
, "fps" ), frequency
, counter
++ );
262 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
264 if ( mlt_properties_get_int( properties
, "audio_off" ) )
270 if ( init_audio
== 1 )
272 SDL_AudioSpec request
;
275 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
276 SDL_EnableUNICODE( 1 );
278 // specify audio format
279 memset( &request
, 0, sizeof( SDL_AudioSpec
) );
281 request
.freq
= frequency
;
282 request
.format
= AUDIO_S16
;
283 request
.channels
= channels
;
284 request
.samples
= 1024;
285 request
.callback
= sdl_fill_audio
;
286 request
.userdata
= (void *)this;
287 if ( SDL_OpenAudio( &request
, &got
) != 0 )
289 fprintf( stderr
, "SDL failed to open audio: %s\n", SDL_GetError() );
292 else if ( got
.size
!= 0 )
299 if ( init_audio
== 0 )
301 bytes
= ( samples
* channels
* 2 );
302 pthread_mutex_lock( &this->audio_mutex
);
303 while ( bytes
> ( sizeof( this->audio_buffer
) - this->audio_avail
) )
304 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
305 mlt_properties properties
= mlt_frame_properties( frame
);
306 if ( mlt_properties_get_double( properties
, "_speed" ) == 1 )
307 memcpy( &this->audio_buffer
[ this->audio_avail
], pcm
, bytes
);
309 memset( &this->audio_buffer
[ this->audio_avail
], 0, bytes
);
310 this->audio_avail
+= bytes
;
311 pthread_cond_broadcast( &this->audio_cond
);
312 pthread_mutex_unlock( &this->audio_mutex
);
322 static int consumer_play_video( consumer_sdl
this, mlt_frame frame
)
324 // Get the properties of this consumer
325 mlt_properties properties
= this->properties
;
327 mlt_image_format vfmt
= mlt_image_yuv422
;
328 int width
= this->width
, height
= this->height
;
332 if ( mlt_properties_get_int( properties
, "video_off" ) )
334 mlt_frame_close( frame
);
338 if ( this->count
== this->size
)
341 this->queue
= realloc( this->queue
, sizeof( mlt_frame
) * this->size
);
343 this->queue
[ this->count
++ ] = frame
;
347 // We're working on the oldest frame now
348 frame
= this->queue
[ 0 ];
350 // Shunt the frames in the queue down
352 for ( i
= 1; i
< this->count
; i
++ )
353 this->queue
[ i
- 1 ] = this->queue
[ i
];
356 // Get the image, width and height
357 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
;
376 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
377 char keyboard
[ 2 ] = " ";
378 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
379 if ( callback
!= NULL
&& producer
!= NULL
&& event
.key
.keysym
.unicode
< 0x80 && event
.key
.keysym
.unicode
> 0 )
381 keyboard
[ 0 ] = ( char )event
.key
.keysym
.unicode
;
382 callback( producer
, keyboard
);
391 if ( width
!= this->width
|| height
!= this->height
)
394 this->height
= height
;
398 if ( this->sdl_screen
== NULL
|| changed
)
400 double aspect_ratio
= mlt_frame_get_aspect_ratio( frame
);
401 float display_aspect_ratio
= (float)width
/ (float)height
;
404 if ( mlt_properties_get_double( properties
, "aspect_ratio" ) )
405 aspect_ratio
= mlt_properties_get_double( properties
, "aspect_ratio" );
407 if ( aspect_ratio
== 1 )
409 rect
.w
= this->window_width
;
410 rect
.h
= this->window_height
;
412 else if ( this->window_width
< this->window_height
* aspect_ratio
)
414 rect
.w
= this->window_width
;
415 rect
.h
= this->window_width
/ aspect_ratio
;
419 rect
.w
= this->window_height
* aspect_ratio
;
420 rect
.h
= this->window_height
;
423 if ( mlt_properties_get_int( properties
, "scale_overlay" ) )
425 if ( ( float )rect
.w
* display_aspect_ratio
< this->window_width
)
426 rect
.w
= ( int )( ( float )rect
.w
* display_aspect_ratio
);
427 else if ( ( float )rect
.h
* display_aspect_ratio
< this->window_height
)
428 rect
.h
= ( int )( ( float )rect
.h
* display_aspect_ratio
);
431 rect
.x
= ( this->window_width
- rect
.w
) / 2;
432 rect
.y
= ( this->window_height
- rect
.h
) / 2;
434 // Force an overlay recreation
435 if ( this->sdl_overlay
!= NULL
)
436 SDL_FreeYUVOverlay( this->sdl_overlay
);
438 // open SDL window with video overlay, if possible
439 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
441 if ( this->sdl_screen
!= NULL
)
443 SDL_SetClipRect( this->sdl_screen
, &rect
);
446 this->sdl_overlay
= SDL_CreateYUVOverlay( this->width
- (this->width
% 4), this->height
- (this->height
% 2 ), SDL_YUY2_OVERLAY
, this->sdl_screen
);
447 sdl_unlock_display();
451 if ( this->sdl_screen
!= NULL
&& this->sdl_overlay
!= NULL
)
453 this->buffer
= this->sdl_overlay
->pixels
[ 0 ];
454 if ( SDL_LockYUVOverlay( this->sdl_overlay
) >= 0 )
456 mlt_resize_yuv422( this->buffer
, this->width
- (this->width
% 4 ), this->height
- (this->height
% 2 ), image
, width
, height
);
457 SDL_UnlockYUVOverlay( this->sdl_overlay
);
458 SDL_DisplayYUVOverlay( this->sdl_overlay
, &this->sdl_screen
->clip_rect
);
469 mlt_frame_close( frame
);
472 mlt_frame_get_image( this->queue
[ this->count
- 1 ], &image
, &vfmt
, &width
, &height
, 0 );
477 /** Threaded wrapper for pipe.
480 static void *consumer_thread( void *arg
)
483 consumer_sdl
this = arg
;
486 mlt_consumer consumer
= &this->parent
;
488 // Get the service assoicated to the consumer
489 mlt_service service
= mlt_consumer_service( consumer
);
491 // Define a frame pointer
494 // internal intialization
497 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) < 0 )
499 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
503 // Loop until told not to
504 while( this->running
)
506 // Get a frame from the service (should never return anything other than 0)
507 if ( mlt_service_get_frame( service
, &frame
, 0 ) == 0 )
509 init_audio
= consumer_play_audio( this, frame
, init_audio
);
510 consumer_play_video( this, frame
);
515 if ( init_audio
== 0 )
517 if ( this->sdl_overlay
!= NULL
)
518 SDL_FreeYUVOverlay( this->sdl_overlay
);
521 this->sdl_screen
= NULL
;
522 this->sdl_overlay
= NULL
;
523 this->audio_avail
= 0;
528 static int consumer_get_dimensions( int *width
, int *height
)
532 // SDL windows manager structure
535 // Specify the SDL Version
536 SDL_VERSION( &wm
.version
);
538 // Get the wm structure
539 if ( SDL_GetWMInfo( &wm
) == 1 )
541 // Check that we have the X11 wm
542 if ( wm
.subsystem
== SDL_SYSWM_X11
)
544 // Get the SDL window
545 Window window
= wm
.info
.x11
.window
;
547 // Get the display session
548 Display
*display
= wm
.info
.x11
.display
;
550 // Get the window attributes
551 XWindowAttributes attr
;
552 XGetWindowAttributes( display
, window
, &attr
);
554 // Determine whether window has changed
555 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
557 // Return width and height
559 *height
= attr
.height
;
566 /** Callback to allow override of the close method.
569 static void consumer_close( mlt_consumer parent
)
571 // Get the actual object
572 consumer_sdl
this = parent
->child
;
575 mlt_consumer_stop( parent
);
578 pthread_mutex_destroy( &this->audio_mutex
);
579 pthread_cond_destroy( &this->audio_cond
);
581 // Now clean up the rest
582 mlt_consumer_close( parent
);
584 // Finally clean up this