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 // Define a frame pointer
194 // internal intialization
195 int sdl_flags
= SDL_HWSURFACE
| SDL_DOUBLEBUF
| SDL_HWACCEL
| SDL_RESIZABLE
;
196 SDL_Surface
*sdl_screen
= NULL
;
197 SDL_Overlay
*sdl_overlay
= NULL
;
198 uint8_t *buffer
= NULL
;
202 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) < 0 )
204 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
208 // Loop until told not to
209 while( this->running
)
211 // Get a frame from the service (should never return anything other than 0)
212 if ( mlt_service_get_frame( service
, &frame
, 0 ) == 0 )
214 mlt_image_format vfmt
= mlt_image_yuv422
;
215 int width
= this->width
, height
= this->height
;
218 mlt_audio_format afmt
= mlt_audio_pcm
;
225 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
226 if ( init_audio
== 1 )
228 SDL_AudioSpec request
;
230 // specify audio format
231 request
.freq
= frequency
;
232 request
.format
= AUDIO_S16
;
233 request
.channels
= channels
;
234 request
.samples
= 1024;
235 request
.callback
= sdl_fill_audio
;
236 request
.userdata
= (void *)this;
237 if ( SDL_OpenAudio( &request
, NULL
) < 0 )
239 fprintf( stderr
, "SDL failed to open audio: %s\n", SDL_GetError() );
245 bytes
= ( samples
* channels
* 2 );
246 pthread_mutex_lock( &this->audio_mutex
);
247 while ( bytes
> ( sizeof( this->audio_buffer
) - this->audio_avail
) )
248 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
249 memcpy( &this->audio_buffer
[ this->audio_avail
], pcm
, bytes
);
250 this->audio_avail
+= bytes
;
251 pthread_cond_broadcast( &this->audio_cond
);
252 pthread_mutex_unlock( &this->audio_mutex
);
254 // Get the image, width and height
255 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
257 if ( sdl_screen
!= NULL
)
261 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
263 while ( SDL_PollEvent( &event
) )
267 case SDL_VIDEORESIZE
:
268 this->window_width
= event
.resize
.w
;
269 this->window_height
= event
.resize
.h
;
276 if ( sdl_screen
== NULL
|| changed
)
278 double aspect_ratio
= mlt_frame_get_aspect_ratio( frame
);
280 if ( this->window_width
== 0 || this->window_height
== 0 )
282 this->window_width
= width
;
283 this->window_height
= height
;
286 // open SDL window with video overlay, if possible
287 sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, sdl_flags
);
289 if ( sdl_screen
!= NULL
)
292 if ( this->window_width
< this->window_height
* aspect_ratio
)
294 rect
.w
= this->window_width
;
295 rect
.h
= this->window_width
/ aspect_ratio
;
299 rect
.w
= this->window_height
* aspect_ratio
;
300 rect
.h
= this->window_height
;
303 rect
.x
= ( this->window_width
- rect
.w
) / 2;
304 rect
.y
= ( this->window_height
- rect
.h
) / 2;
306 SDL_SetClipRect( sdl_screen
, &rect
);
308 // Force an overlay recreation
309 if ( sdl_overlay
!= NULL
)
310 SDL_FreeYUVOverlay( sdl_overlay
);
312 sdl_overlay
= SDL_CreateYUVOverlay( width
, height
, SDL_YUY2_OVERLAY
, sdl_screen
);
313 sdl_unlock_display();
317 if ( sdl_screen
!= NULL
&& sdl_overlay
!= NULL
)
319 buffer
= sdl_overlay
->pixels
[ 0 ];
320 if ( sdl_lock_display() )
322 if ( SDL_LockYUVOverlay( sdl_overlay
) >= 0 )
324 memcpy( buffer
, image
, width
* height
* 2 );
325 SDL_UnlockYUVOverlay( sdl_overlay
);
326 SDL_DisplayYUVOverlay( sdl_overlay
, &sdl_screen
->clip_rect
);
328 sdl_unlock_display();
333 // TODO: allocate buffer?
337 mlt_frame_close( frame
);
346 if ( init_audio
== 0 )
348 if ( sdl_overlay
!= NULL
)
349 SDL_FreeYUVOverlay( sdl_overlay
);
355 static int consumer_get_dimensions( int *width
, int *height
)
359 // SDL windows manager structure
362 // Specify the SDL Version
363 SDL_VERSION( &wm
.version
);
365 // Get the wm structure
366 if ( SDL_GetWMInfo( &wm
) == 1 )
368 // Check that we have the X11 wm
369 if ( wm
.subsystem
== SDL_SYSWM_X11
)
371 // Get the SDL window
372 Window window
= wm
.info
.x11
.window
;
374 // Get the display session
375 Display
*display
= wm
.info
.x11
.display
;
377 // Get the window attributes
378 XWindowAttributes attr
;
379 XGetWindowAttributes( display
, window
, &attr
);
381 // Determine whether window has changed
382 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
384 // Return width and height
386 *height
= attr
.height
;
393 /** Callback to allow override of the close method.
396 static void consumer_close( mlt_consumer parent
)
398 // Get the actual object
399 consumer_sdl
this = parent
->child
;
401 // Kill the thread and clean up
404 pthread_mutex_lock( &this->audio_mutex
);
405 pthread_cond_broadcast( &this->audio_cond
);
406 pthread_mutex_unlock( &this->audio_mutex
);
408 pthread_join( this->thread
, NULL
);
409 pthread_mutex_destroy( &this->audio_mutex
);
410 pthread_cond_destroy( &this->audio_cond
);
412 // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
413 parent
->close
= NULL
;
414 mlt_consumer_close( parent
);
416 // Finally clean up this