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
;
53 /** Forward references to static functions.
56 static void consumer_close( mlt_consumer parent
);
57 static void *consumer_thread( void * );
58 static int consumer_get_dimensions( int *width
, int *height
);
60 /** This is what will be called by the factory - anything can be passed in
61 via the argument, but keep it simple.
64 mlt_consumer
consumer_sdl_init( char *arg
)
66 // Create the consumer object
67 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
69 // If no malloc'd and consumer init ok
70 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
72 // Get the parent consumer object
73 mlt_consumer parent
= &this->parent
;
75 // We have stuff to clean up, so override the close method
76 parent
->close
= consumer_close
;
78 // get a handle on properties
79 mlt_service service
= mlt_consumer_service( parent
);
80 this->properties
= mlt_service_properties( service
);
82 // Set the default volume
83 mlt_properties_set_double( this->properties
, "volume", 1.0 );
85 // This is the initialisation of the consumer
87 pthread_mutex_init( &this->audio_mutex
, NULL
);
88 pthread_cond_init( &this->audio_cond
, NULL
);
90 // process actual param
91 if ( arg
== NULL
|| !strcmp( arg
, "PAL" ) )
96 else if ( !strcmp( arg
, "NTSC" ) )
101 else if ( sscanf( arg
, "%dx%d", &this->width
, &this->height
) != 2 )
108 // Create the the thread
109 pthread_create( &this->thread
, NULL
, consumer_thread
, this );
111 // Return the consumer produced
115 // malloc or consumer init failed
122 static int sdl_lock_display( )
124 SDL_Surface
*screen
= SDL_GetVideoSurface( );
125 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
128 static void sdl_unlock_display( )
130 SDL_Surface
*screen
= SDL_GetVideoSurface( );
131 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
132 SDL_UnlockSurface( screen
);
135 void sdl_fill_audio( void *udata
, uint8_t *stream
, int len
)
137 consumer_sdl
this = udata
;
140 float volume
= mlt_properties_get_double( this->properties
, "volume" );
142 pthread_mutex_lock( &this->audio_mutex
);
144 // Experimental - block until audio received
145 while ( this->running
&& len
> this->audio_avail
)
146 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
148 if ( this->audio_avail
>= len
)
150 // Place in the audio buffer
151 SDL_MixAudio( stream
, this->audio_buffer
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
153 // Remove len from the audio available
154 this->audio_avail
-= len
;
156 // Remove the samples
157 memmove( this->audio_buffer
, this->audio_buffer
+ len
, this->audio_avail
);
161 // Just to be safe, wipe the stream first
162 memset( stream
, 0, len
);
164 // Copy what we have into the stream
165 memcpy( stream
, this->audio_buffer
, this->audio_avail
);
168 SDL_MixAudio( stream
, stream
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
171 this->audio_avail
= 0;
173 pthread_cond_broadcast( &this->audio_cond
);
174 pthread_mutex_unlock( &this->audio_mutex
);
177 /** Threaded wrapper for pipe.
180 static void *consumer_thread( void *arg
)
183 consumer_sdl
this = arg
;
186 mlt_consumer consumer
= &this->parent
;
188 // Get the service assoicated to the consumer
189 mlt_service service
= mlt_consumer_service( consumer
);
191 // Get the properties of this consumer
192 mlt_properties properties
= this->properties
;
194 // Define a frame pointer
197 // internal intialization
198 int sdl_flags
= SDL_HWSURFACE
| SDL_DOUBLEBUF
| SDL_HWACCEL
| SDL_RESIZABLE
;
199 SDL_Surface
*sdl_screen
= NULL
;
200 SDL_Overlay
*sdl_overlay
= NULL
;
201 uint8_t *buffer
= NULL
;
205 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) < 0 )
207 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
211 // Loop until told not to
212 while( this->running
)
214 // Get a frame from the service (should never return anything other than 0)
215 if ( mlt_service_get_frame( service
, &frame
, 0 ) == 0 )
217 mlt_image_format vfmt
= mlt_image_yuv422
;
218 int width
= this->width
, height
= this->height
;
221 mlt_audio_format afmt
= mlt_audio_pcm
;
228 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
229 if ( init_audio
== 1 )
231 SDL_AudioSpec request
;
233 // specify audio format
234 request
.freq
= frequency
;
235 request
.format
= AUDIO_S16
;
236 request
.channels
= channels
;
237 request
.samples
= 1024;
238 request
.callback
= sdl_fill_audio
;
239 request
.userdata
= (void *)this;
240 if ( SDL_OpenAudio( &request
, NULL
) < 0 )
242 fprintf( stderr
, "SDL failed to open audio: %s\n", SDL_GetError() );
248 bytes
= ( samples
* channels
* 2 );
249 pthread_mutex_lock( &this->audio_mutex
);
250 while ( bytes
> ( sizeof( this->audio_buffer
) - this->audio_avail
) )
251 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
252 memcpy( &this->audio_buffer
[ this->audio_avail
], pcm
, bytes
);
253 this->audio_avail
+= bytes
;
254 pthread_cond_broadcast( &this->audio_cond
);
255 pthread_mutex_unlock( &this->audio_mutex
);
257 // Get the image, width and height
258 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
260 if ( sdl_screen
!= NULL
)
264 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
266 while ( SDL_PollEvent( &event
) )
270 case SDL_VIDEORESIZE
:
271 this->window_width
= event
.resize
.w
;
272 this->window_height
= event
.resize
.h
;
277 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
278 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
279 if ( callback
!= NULL
&& producer
!= NULL
)
280 callback( producer
, SDL_GetKeyName(event
.key
.keysym
.sym
) );
287 if ( sdl_screen
== NULL
|| changed
)
289 double aspect_ratio
= mlt_frame_get_aspect_ratio( frame
);
291 if ( this->window_width
== 0 || this->window_height
== 0 )
293 this->window_width
= width
;
294 this->window_height
= height
;
297 // open SDL window with video overlay, if possible
298 sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, sdl_flags
);
300 if ( sdl_screen
!= NULL
)
303 if ( this->window_width
< this->window_height
* aspect_ratio
)
305 rect
.w
= this->window_width
;
306 rect
.h
= this->window_width
/ aspect_ratio
;
310 rect
.w
= this->window_height
* aspect_ratio
;
311 rect
.h
= this->window_height
;
314 rect
.x
= ( this->window_width
- rect
.w
) / 2;
315 rect
.y
= ( this->window_height
- rect
.h
) / 2;
317 SDL_SetClipRect( sdl_screen
, &rect
);
319 // Force an overlay recreation
320 if ( sdl_overlay
!= NULL
)
321 SDL_FreeYUVOverlay( sdl_overlay
);
323 sdl_overlay
= SDL_CreateYUVOverlay( this->width
, this->height
, SDL_YUY2_OVERLAY
, sdl_screen
);
324 sdl_unlock_display();
328 if ( sdl_screen
!= NULL
&& sdl_overlay
!= NULL
)
330 buffer
= sdl_overlay
->pixels
[ 0 ];
331 if ( sdl_lock_display() )
333 if ( SDL_LockYUVOverlay( sdl_overlay
) >= 0 )
335 mlt_resize_yuv422( buffer
, this->width
, this->height
, image
, width
, height
);
336 SDL_UnlockYUVOverlay( sdl_overlay
);
337 SDL_DisplayYUVOverlay( sdl_overlay
, &sdl_screen
->clip_rect
);
339 sdl_unlock_display();
344 // TODO: allocate buffer?
348 mlt_frame_close( frame
);
357 if ( init_audio
== 0 )
359 if ( sdl_overlay
!= NULL
)
360 SDL_FreeYUVOverlay( sdl_overlay
);
366 static int consumer_get_dimensions( int *width
, int *height
)
370 // SDL windows manager structure
373 // Specify the SDL Version
374 SDL_VERSION( &wm
.version
);
376 // Get the wm structure
377 if ( SDL_GetWMInfo( &wm
) == 1 )
379 // Check that we have the X11 wm
380 if ( wm
.subsystem
== SDL_SYSWM_X11
)
382 // Get the SDL window
383 Window window
= wm
.info
.x11
.window
;
385 // Get the display session
386 Display
*display
= wm
.info
.x11
.display
;
388 // Get the window attributes
389 XWindowAttributes attr
;
390 XGetWindowAttributes( display
, window
, &attr
);
392 // Determine whether window has changed
393 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
395 // Return width and height
397 *height
= attr
.height
;
404 /** Callback to allow override of the close method.
407 static void consumer_close( mlt_consumer parent
)
409 // Get the actual object
410 consumer_sdl
this = parent
->child
;
412 // Kill the thread and clean up
415 pthread_mutex_lock( &this->audio_mutex
);
416 pthread_cond_broadcast( &this->audio_cond
);
417 pthread_mutex_unlock( &this->audio_mutex
);
419 pthread_join( this->thread
, NULL
);
420 pthread_mutex_destroy( &this->audio_mutex
);
421 pthread_cond_destroy( &this->audio_cond
);
423 // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
424 parent
->close
= NULL
;
425 mlt_consumer_close( parent
);
427 // Finally clean up this