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 * 6 ];
45 pthread_mutex_t audio_mutex
;
46 pthread_cond_t audio_cond
;
57 /** Forward references to static functions.
60 static void consumer_close( mlt_consumer parent
);
61 static void *consumer_thread( void * );
62 static int consumer_get_dimensions( int *width
, int *height
);
64 /** This is what will be called by the factory - anything can be passed in
65 via the argument, but keep it simple.
68 mlt_consumer
consumer_sdl_init( char *arg
)
70 // Create the consumer object
71 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
73 // If no malloc'd and consumer init ok
74 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
76 // Get the parent consumer object
77 mlt_consumer parent
= &this->parent
;
79 // We have stuff to clean up, so override the close method
80 parent
->close
= consumer_close
;
82 // get a handle on properties
83 mlt_service service
= mlt_consumer_service( parent
);
84 this->properties
= mlt_service_properties( service
);
86 // Set the default volume
87 mlt_properties_set_double( this->properties
, "volume", 1.0 );
89 // This is the initialisation of the consumer
91 pthread_mutex_init( &this->audio_mutex
, NULL
);
92 pthread_cond_init( &this->audio_cond
, NULL
);
94 // process actual param
95 if ( arg
== NULL
|| !strcmp( arg
, "PAL" ) )
97 this->window_width
= 720;
98 this->window_height
= 576;
100 else if ( !strcmp( arg
, "NTSC" ) )
102 this->window_width
= 720;
103 this->window_height
= 480;
105 else if ( sscanf( arg
, "%dx%d", &this->window_width
, &this->window_height
) != 2 )
107 this->window_width
= 720;
108 this->window_height
= 576;
111 // Create the the thread
112 pthread_create( &this->thread
, NULL
, consumer_thread
, this );
114 // Return the consumer produced
118 // malloc or consumer init failed
125 static int sdl_lock_display( )
127 SDL_Surface
*screen
= SDL_GetVideoSurface( );
128 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
131 static void sdl_unlock_display( )
133 SDL_Surface
*screen
= SDL_GetVideoSurface( );
134 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
135 SDL_UnlockSurface( screen
);
138 void sdl_fill_audio( void *udata
, uint8_t *stream
, int len
)
140 consumer_sdl
this = udata
;
143 float volume
= mlt_properties_get_double( this->properties
, "volume" );
145 pthread_mutex_lock( &this->audio_mutex
);
147 // Block until audio received
148 while ( this->running
&& len
> this->audio_avail
)
149 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
151 if ( this->audio_avail
>= len
)
153 // Place in the audio buffer
154 SDL_MixAudio( stream
, this->audio_buffer
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
156 // Remove len from the audio available
157 this->audio_avail
-= len
;
159 // Remove the samples
160 memmove( this->audio_buffer
, this->audio_buffer
+ len
, this->audio_avail
);
164 // Just to be safe, wipe the stream first
165 memset( stream
, 0, len
);
167 // Copy what we have into the stream
168 memcpy( stream
, this->audio_buffer
, this->audio_avail
);
171 SDL_MixAudio( stream
, stream
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
174 this->audio_avail
= 0;
177 // We're definitely playing now
180 pthread_cond_broadcast( &this->audio_cond
);
181 pthread_mutex_unlock( &this->audio_mutex
);
184 /** Threaded wrapper for pipe.
187 static void *consumer_thread( void *arg
)
190 consumer_sdl
this = arg
;
193 mlt_consumer consumer
= &this->parent
;
195 // Get the service assoicated to the consumer
196 mlt_service service
= mlt_consumer_service( consumer
);
198 // Get the properties of this consumer
199 mlt_properties properties
= this->properties
;
201 // Define a frame pointer
204 // internal intialization
205 int sdl_flags
= SDL_HWSURFACE
| SDL_DOUBLEBUF
| SDL_HWACCEL
| SDL_RESIZABLE
;
206 SDL_Surface
*sdl_screen
= NULL
;
207 SDL_Overlay
*sdl_overlay
= NULL
;
208 uint8_t *buffer
= NULL
;
212 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) < 0 )
214 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
218 // Loop until told not to
219 while( this->running
)
221 // Get a frame from the service (should never return anything other than 0)
222 if ( mlt_service_get_frame( service
, &frame
, 0 ) == 0 )
224 mlt_image_format vfmt
= mlt_image_yuv422
;
225 int width
= this->width
, height
= this->height
;
228 mlt_audio_format afmt
= mlt_audio_pcm
;
235 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
236 if ( init_audio
== 1 )
238 SDL_AudioSpec request
;
240 // specify audio format
241 request
.freq
= frequency
;
242 request
.format
= AUDIO_S16
;
243 request
.channels
= channels
;
244 request
.samples
= 2048;
245 request
.callback
= sdl_fill_audio
;
246 request
.userdata
= (void *)this;
247 if ( SDL_OpenAudio( &request
, NULL
) < 0 )
249 fprintf( stderr
, "SDL failed to open audio: %s\n", SDL_GetError() );
255 bytes
= ( samples
* channels
* 2 );
256 pthread_mutex_lock( &this->audio_mutex
);
257 while ( bytes
> ( sizeof( this->audio_buffer
) - this->audio_avail
) )
258 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
259 memcpy( &this->audio_buffer
[ this->audio_avail
], pcm
, bytes
);
260 this->audio_avail
+= bytes
;
261 pthread_cond_broadcast( &this->audio_cond
);
262 pthread_mutex_unlock( &this->audio_mutex
);
264 if ( this->count
== this->size
)
267 this->queue
= realloc( this->queue
, sizeof( mlt_frame
) * this->size
);
269 this->queue
[ this->count
++ ] = frame
;
273 // We're working on the oldest frame now
274 frame
= this->queue
[ 0 ];
276 // Shunt the frames in the queue down
278 for ( i
= 1; i
< this->count
; i
++ )
279 this->queue
[ i
- 1 ] = this->queue
[ i
];
282 // Get the image, width and height
283 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
285 if ( sdl_screen
!= NULL
)
289 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
291 while ( SDL_PollEvent( &event
) )
295 case SDL_VIDEORESIZE
:
296 this->window_width
= event
.resize
.w
;
297 this->window_height
= event
.resize
.h
;
302 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
303 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
304 if ( callback
!= NULL
&& producer
!= NULL
)
305 callback( producer
, SDL_GetKeyName(event
.key
.keysym
.sym
) );
313 if ( width
!= this->width
|| height
!= this->height
)
316 this->height
= height
;
320 if ( sdl_screen
== NULL
|| changed
)
322 double aspect_ratio
= mlt_frame_get_aspect_ratio( frame
);
324 if ( mlt_properties_get_double( properties
, "aspect_ratio" ) )
325 aspect_ratio
= mlt_properties_get_double( properties
, "aspect_ratio" );
327 // open SDL window with video overlay, if possible
328 sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, sdl_flags
);
330 if ( sdl_screen
!= NULL
)
333 if ( this->window_width
< this->window_height
* aspect_ratio
)
335 rect
.w
= this->window_width
;
336 rect
.h
= this->window_width
/ aspect_ratio
;
340 rect
.w
= this->window_height
* aspect_ratio
;
341 rect
.h
= this->window_height
;
344 rect
.x
= ( this->window_width
- rect
.w
) / 2;
345 rect
.y
= ( this->window_height
- rect
.h
) / 2;
347 SDL_SetClipRect( sdl_screen
, &rect
);
349 // Force an overlay recreation
350 if ( sdl_overlay
!= NULL
)
351 SDL_FreeYUVOverlay( sdl_overlay
);
353 sdl_overlay
= SDL_CreateYUVOverlay( this->width
, this->height
, SDL_YUY2_OVERLAY
, sdl_screen
);
354 sdl_unlock_display();
358 if ( sdl_screen
!= NULL
&& sdl_overlay
!= NULL
)
360 buffer
= sdl_overlay
->pixels
[ 0 ];
361 if ( sdl_lock_display() )
363 if ( SDL_LockYUVOverlay( sdl_overlay
) >= 0 )
365 mlt_resize_yuv422( buffer
, this->width
, this->height
, image
, width
, height
);
366 SDL_UnlockYUVOverlay( sdl_overlay
);
367 SDL_DisplayYUVOverlay( sdl_overlay
, &sdl_screen
->clip_rect
);
369 sdl_unlock_display();
380 mlt_frame_close( frame
);
383 mlt_frame_get_image( this->queue
[ this->count
- 1 ], &image
, &vfmt
, &width
, &height
, 0 );
392 if ( init_audio
== 0 )
394 if ( sdl_overlay
!= NULL
)
395 SDL_FreeYUVOverlay( sdl_overlay
);
401 static int consumer_get_dimensions( int *width
, int *height
)
405 // SDL windows manager structure
408 // Specify the SDL Version
409 SDL_VERSION( &wm
.version
);
411 // Get the wm structure
412 if ( SDL_GetWMInfo( &wm
) == 1 )
414 // Check that we have the X11 wm
415 if ( wm
.subsystem
== SDL_SYSWM_X11
)
417 // Get the SDL window
418 Window window
= wm
.info
.x11
.window
;
420 // Get the display session
421 Display
*display
= wm
.info
.x11
.display
;
423 // Get the window attributes
424 XWindowAttributes attr
;
425 XGetWindowAttributes( display
, window
, &attr
);
427 // Determine whether window has changed
428 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
430 // Return width and height
432 *height
= attr
.height
;
439 /** Callback to allow override of the close method.
442 static void consumer_close( mlt_consumer parent
)
444 // Get the actual object
445 consumer_sdl
this = parent
->child
;
447 // Kill the thread and clean up
450 pthread_mutex_lock( &this->audio_mutex
);
451 pthread_cond_broadcast( &this->audio_cond
);
452 pthread_mutex_unlock( &this->audio_mutex
);
454 pthread_join( this->thread
, NULL
);
455 pthread_mutex_destroy( &this->audio_mutex
);
456 pthread_cond_destroy( &this->audio_cond
);
458 // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
459 parent
->close
= NULL
;
460 mlt_consumer_close( parent
);
462 // Finally clean up this