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 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 )
81 // Get the parent consumer object
82 mlt_consumer parent
= &this->parent
;
84 // We have stuff to clean up, so override the close method
85 parent
->close
= consumer_close
;
87 // get a handle on properties
88 mlt_service service
= mlt_consumer_service( parent
);
89 this->properties
= mlt_service_properties( service
);
91 // Set the default volume
92 mlt_properties_set_double( this->properties
, "volume", 1.0 );
94 // This is the initialisation of the consumer
96 pthread_mutex_init( &this->audio_mutex
, NULL
);
97 pthread_cond_init( &this->audio_cond
, NULL
);
99 // process actual param
100 if ( arg
== NULL
|| !strcmp( arg
, "PAL" ) )
105 else if ( !strcmp( arg
, "NTSC" ) )
110 else if ( sscanf( arg
, "%dx%d", &this->width
, &this->height
) != 2 )
116 // Default window size and aspect ratio
117 this->aspect_ratio
= 4.0 / 3.0;
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_DOUBLEBUF
| SDL_HWACCEL
| SDL_RESIZABLE
;
124 // Create the the thread
125 pthread_create( &this->thread
, NULL
, consumer_thread
, this );
127 // Return the consumer produced
131 // malloc or consumer init failed
138 static int sdl_lock_display( )
140 SDL_Surface
*screen
= SDL_GetVideoSurface( );
141 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
144 static void sdl_unlock_display( )
146 SDL_Surface
*screen
= SDL_GetVideoSurface( );
147 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
148 SDL_UnlockSurface( screen
);
151 void sdl_fill_audio( void *udata
, uint8_t *stream
, int len
)
153 consumer_sdl
this = udata
;
156 float volume
= mlt_properties_get_double( this->properties
, "volume" );
158 pthread_mutex_lock( &this->audio_mutex
);
160 // Block until audio received
161 while ( this->running
&& len
> this->audio_avail
)
162 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
164 if ( this->audio_avail
>= len
)
166 // Place in the audio buffer
167 SDL_MixAudio( stream
, this->audio_buffer
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
169 // Remove len from the audio available
170 this->audio_avail
-= len
;
172 // Remove the samples
173 memmove( this->audio_buffer
, this->audio_buffer
+ len
, this->audio_avail
);
177 // Just to be safe, wipe the stream first
178 memset( stream
, 0, len
);
180 // Copy what we have into the stream
181 memcpy( stream
, this->audio_buffer
, this->audio_avail
);
184 SDL_MixAudio( stream
, stream
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
187 this->audio_avail
= 0;
190 // We're definitely playing now
193 pthread_cond_broadcast( &this->audio_cond
);
194 pthread_mutex_unlock( &this->audio_mutex
);
197 static int consumer_play_audio( consumer_sdl
this, mlt_frame frame
, int init_audio
)
199 // Get the properties of this consumer
200 mlt_properties properties
= this->properties
;
201 mlt_audio_format afmt
= mlt_audio_pcm
;
208 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
210 if ( mlt_properties_get_int( properties
, "audio_off" ) )
216 if ( init_audio
== 1 )
218 SDL_AudioSpec request
;
221 // specify audio format
222 memset( &request
, 0, sizeof( SDL_AudioSpec
) );
224 request
.freq
= frequency
;
225 request
.format
= AUDIO_S16
;
226 request
.channels
= channels
;
227 request
.samples
= 1024;
228 request
.callback
= sdl_fill_audio
;
229 request
.userdata
= (void *)this;
230 if ( SDL_OpenAudio( &request
, &got
) != 0 )
232 fprintf( stderr
, "SDL failed to open audio: %s\n", SDL_GetError() );
245 if ( init_audio
== 0 )
247 bytes
= ( samples
* channels
* 2 );
248 pthread_mutex_lock( &this->audio_mutex
);
249 while ( bytes
> ( sizeof( this->audio_buffer
) - this->audio_avail
) )
250 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
251 mlt_properties properties
= mlt_frame_properties( frame
);
252 if ( mlt_properties_get_double( properties
, "speed" ) == 1 )
253 memcpy( &this->audio_buffer
[ this->audio_avail
], pcm
, bytes
);
255 memset( &this->audio_buffer
[ this->audio_avail
], 0, bytes
);
256 this->audio_avail
+= bytes
;
257 pthread_cond_broadcast( &this->audio_cond
);
258 pthread_mutex_unlock( &this->audio_mutex
);
268 static int consumer_play_video( consumer_sdl
this, mlt_frame frame
)
270 // Get the properties of this consumer
271 mlt_properties properties
= this->properties
;
273 mlt_image_format vfmt
= mlt_image_yuv422
;
274 int width
= this->width
, height
= this->height
;
278 if ( mlt_properties_get_int( properties
, "video_off" ) )
280 mlt_frame_close( frame
);
284 if ( this->count
== this->size
)
287 this->queue
= realloc( this->queue
, sizeof( mlt_frame
) * this->size
);
289 this->queue
[ this->count
++ ] = frame
;
293 // We're working on the oldest frame now
294 frame
= this->queue
[ 0 ];
296 // Shunt the frames in the queue down
298 for ( i
= 1; i
< this->count
; i
++ )
299 this->queue
[ i
- 1 ] = this->queue
[ i
];
302 // Get the image, width and height
303 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
305 if ( this->sdl_screen
!= NULL
)
309 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
311 while ( SDL_PollEvent( &event
) )
315 case SDL_VIDEORESIZE
:
316 this->window_width
= event
.resize
.w
;
317 this->window_height
= event
.resize
.h
;
322 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
323 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
324 if ( callback
!= NULL
&& producer
!= NULL
)
325 callback( producer
, SDL_GetKeyName(event
.key
.keysym
.sym
) );
333 if ( width
!= this->width
|| height
!= this->height
)
336 this->height
= height
;
340 if ( this->sdl_screen
== NULL
|| changed
)
342 double aspect_ratio
= mlt_frame_get_aspect_ratio( frame
);
343 float display_aspect_ratio
= (float)width
/ (float)height
;
346 if ( mlt_properties_get_double( properties
, "aspect_ratio" ) )
347 aspect_ratio
= mlt_properties_get_double( properties
, "aspect_ratio" );
349 if ( aspect_ratio
== 1 )
351 rect
.w
= this->window_width
;
352 rect
.h
= this->window_height
;
354 else if ( this->window_width
< this->window_height
* aspect_ratio
)
356 rect
.w
= this->window_width
;
357 rect
.h
= this->window_width
/ aspect_ratio
;
361 rect
.w
= this->window_height
* aspect_ratio
;
362 rect
.h
= this->window_height
;
365 if ( mlt_properties_get_int( properties
, "scale_overlay" ) )
367 if ( ( float )rect
.w
* display_aspect_ratio
< this->window_width
)
368 rect
.w
= ( int )( ( float )rect
.w
* display_aspect_ratio
);
369 else if ( ( float )rect
.h
* display_aspect_ratio
< this->window_height
)
370 rect
.h
= ( int )( ( float )rect
.h
* display_aspect_ratio
);
373 rect
.x
= ( this->window_width
- rect
.w
) / 2;
374 rect
.y
= ( this->window_height
- rect
.h
) / 2;
376 // Force an overlay recreation
377 if ( this->sdl_overlay
!= NULL
)
378 SDL_FreeYUVOverlay( this->sdl_overlay
);
380 // open SDL window with video overlay, if possible
381 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
383 if ( this->sdl_screen
!= NULL
)
385 SDL_SetClipRect( this->sdl_screen
, &rect
);
388 this->sdl_overlay
= SDL_CreateYUVOverlay( this->width
- (this->width
% 4), this->height
- (this->height
% 2 ), SDL_YUY2_OVERLAY
, this->sdl_screen
);
389 sdl_unlock_display();
393 if ( this->sdl_screen
!= NULL
&& this->sdl_overlay
!= NULL
)
395 this->buffer
= this->sdl_overlay
->pixels
[ 0 ];
396 if ( SDL_LockYUVOverlay( this->sdl_overlay
) >= 0 )
398 mlt_resize_yuv422( this->buffer
, this->width
- (this->width
% 4 ), this->height
- (this->height
% 2 ), image
, width
, height
);
399 SDL_UnlockYUVOverlay( this->sdl_overlay
);
400 SDL_DisplayYUVOverlay( this->sdl_overlay
, &this->sdl_screen
->clip_rect
);
411 mlt_frame_close( frame
);
414 mlt_frame_get_image( this->queue
[ this->count
- 1 ], &image
, &vfmt
, &width
, &height
, 0 );
419 /** Threaded wrapper for pipe.
422 static void *consumer_thread( void *arg
)
425 consumer_sdl
this = arg
;
428 mlt_consumer consumer
= &this->parent
;
430 // Get the service assoicated to the consumer
431 mlt_service service
= mlt_consumer_service( consumer
);
433 // Define a frame pointer
436 // internal intialization
439 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) < 0 )
441 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
445 // Loop until told not to
446 while( this->running
)
448 // Get a frame from the service (should never return anything other than 0)
449 if ( mlt_service_get_frame( service
, &frame
, 0 ) == 0 )
451 init_audio
= consumer_play_audio( this, frame
, init_audio
);
452 consumer_play_video( this, frame
);
457 if ( init_audio
== 0 )
459 if ( this->sdl_overlay
!= NULL
)
460 SDL_FreeYUVOverlay( this->sdl_overlay
);
466 static int consumer_get_dimensions( int *width
, int *height
)
470 // SDL windows manager structure
473 // Specify the SDL Version
474 SDL_VERSION( &wm
.version
);
476 // Get the wm structure
477 if ( SDL_GetWMInfo( &wm
) == 1 )
479 // Check that we have the X11 wm
480 if ( wm
.subsystem
== SDL_SYSWM_X11
)
482 // Get the SDL window
483 Window window
= wm
.info
.x11
.window
;
485 // Get the display session
486 Display
*display
= wm
.info
.x11
.display
;
488 // Get the window attributes
489 XWindowAttributes attr
;
490 XGetWindowAttributes( display
, window
, &attr
);
492 // Determine whether window has changed
493 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
495 // Return width and height
497 *height
= attr
.height
;
504 /** Callback to allow override of the close method.
507 static void consumer_close( mlt_consumer parent
)
509 // Get the actual object
510 consumer_sdl
this = parent
->child
;
512 // Kill the thread and clean up
515 pthread_mutex_lock( &this->audio_mutex
);
516 pthread_cond_broadcast( &this->audio_cond
);
517 pthread_mutex_unlock( &this->audio_mutex
);
519 pthread_join( this->thread
, NULL
);
520 pthread_mutex_destroy( &this->audio_mutex
);
521 pthread_cond_destroy( &this->audio_cond
);
523 // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
524 parent
->close
= NULL
;
525 mlt_consumer_close( parent
);
527 // Finally clean up this