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
;
51 /** Forward references to static functions.
54 static void consumer_close( mlt_consumer parent
);
55 static void *consumer_thread( void * );
56 static int consumer_get_dimensions( int *width
, int *height
);
58 /** This is what will be called by the factory - anything can be passed in
59 via the argument, but keep it simple.
62 mlt_consumer
consumer_sdl_init( void *dummy
)
64 // Create the consumer object
65 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
67 // If no malloc'd and consumer init ok
68 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
70 // Get the parent consumer object
71 mlt_consumer parent
= &this->parent
;
73 // We have stuff to clean up, so override the close method
74 parent
->close
= consumer_close
;
76 // get a handle on properties
77 mlt_service service
= mlt_consumer_service( parent
);
78 this->properties
= mlt_service_properties( service
);
80 // Set the default volume
81 mlt_properties_set_double( this->properties
, "volume", 1.0 );
83 // This is the initialisation of the consumer
85 pthread_mutex_init( &this->audio_mutex
, NULL
);
86 pthread_cond_init( &this->audio_cond
, NULL
);
88 // TODO: process actual param
90 // Create the the thread
91 pthread_create( &this->thread
, NULL
, consumer_thread
, this );
93 // Return the consumer produced
97 // malloc or consumer init failed
104 static int sdl_lock_display( )
106 SDL_Surface
*screen
= SDL_GetVideoSurface( );
107 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
110 static void sdl_unlock_display( )
112 SDL_Surface
*screen
= SDL_GetVideoSurface( );
113 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
114 SDL_UnlockSurface( screen
);
117 void sdl_fill_audio( void *udata
, uint8_t *stream
, int len
)
119 consumer_sdl
this = udata
;
122 float volume
= mlt_properties_get_double( this->properties
, "volume" );
124 pthread_mutex_lock( &this->audio_mutex
);
126 // Experimental - block until audio received
127 while ( this->running
&& len
> this->audio_avail
)
128 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
130 if ( this->audio_avail
>= len
)
132 // Place in the audio buffer
133 SDL_MixAudio( stream
, this->audio_buffer
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
135 // Remove len from the audio available
136 this->audio_avail
-= len
;
138 // Remove the samples
139 memmove( this->audio_buffer
, this->audio_buffer
+ len
, this->audio_avail
);
143 // Just to be safe, wipe the stream first
144 memset( stream
, 0, len
);
146 // Copy what we have into the stream
147 memcpy( stream
, this->audio_buffer
, this->audio_avail
);
150 SDL_MixAudio( stream
, stream
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
153 this->audio_avail
= 0;
155 pthread_cond_broadcast( &this->audio_cond
);
156 pthread_mutex_unlock( &this->audio_mutex
);
159 /** Threaded wrapper for pipe.
162 static void *consumer_thread( void *arg
)
165 consumer_sdl
this = arg
;
168 mlt_consumer consumer
= &this->parent
;
170 // Get the service assoicated to the consumer
171 mlt_service service
= mlt_consumer_service( consumer
);
173 // Define a frame pointer
176 // internal intialization
177 int sdl_flags
= SDL_HWSURFACE
| SDL_DOUBLEBUF
| SDL_HWACCEL
| SDL_RESIZABLE
;
178 SDL_Surface
*sdl_screen
= NULL
;
179 SDL_Overlay
*sdl_overlay
= NULL
;
180 uint8_t *buffer
= NULL
;
184 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) < 0 )
186 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
190 // Loop until told not to
191 while( this->running
)
193 // Get a frame from the service (should never return anything other than 0)
194 if ( mlt_service_get_frame( service
, &frame
, 0 ) == 0 )
196 mlt_image_format vfmt
= mlt_image_yuv422
;
200 mlt_audio_format afmt
= mlt_audio_pcm
;
207 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
208 if ( init_audio
== 1 )
210 SDL_AudioSpec request
;
212 // specify audio format
213 request
.freq
= frequency
;
214 request
.format
= AUDIO_S16
;
215 request
.channels
= channels
;
216 request
.samples
= 1024;
217 request
.callback
= sdl_fill_audio
;
218 request
.userdata
= (void *)this;
219 if ( SDL_OpenAudio( &request
, NULL
) < 0 )
221 fprintf( stderr
, "SDL failed to open audio: %s\n", SDL_GetError() );
227 bytes
= ( samples
* channels
* 2 );
228 pthread_mutex_lock( &this->audio_mutex
);
229 while ( bytes
> ( sizeof( this->audio_buffer
) - this->audio_avail
) )
230 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
231 memcpy( &this->audio_buffer
[ this->audio_avail
], pcm
, bytes
);
232 this->audio_avail
+= bytes
;
233 pthread_cond_broadcast( &this->audio_cond
);
234 pthread_mutex_unlock( &this->audio_mutex
);
236 // Get the image, width and height
237 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
239 if ( sdl_screen
!= NULL
)
243 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
245 while ( SDL_PollEvent( &event
) )
249 case SDL_VIDEORESIZE
:
250 this->window_width
= event
.resize
.w
;
251 this->window_height
= event
.resize
.h
;
258 if ( sdl_screen
== NULL
|| changed
)
260 double aspect_ratio
= mlt_frame_get_aspect_ratio( frame
);
262 if ( this->window_width
== 0 || this->window_height
== 0 )
264 this->window_width
= width
;
265 this->window_height
= height
;
268 // open SDL window with video overlay, if possible
269 sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, sdl_flags
);
271 if ( sdl_screen
!= NULL
)
274 if ( this->window_width
< this->window_height
* aspect_ratio
)
276 rect
.w
= this->window_width
;
277 rect
.h
= this->window_width
/ aspect_ratio
;
281 rect
.w
= this->window_height
* aspect_ratio
;
282 rect
.h
= this->window_height
;
285 rect
.x
= ( this->window_width
- rect
.w
) / 2;
286 rect
.y
= ( this->window_height
- rect
.h
) / 2;
288 SDL_SetClipRect( sdl_screen
, &rect
);
290 // Force an overlay recreation
291 if ( sdl_overlay
!= NULL
)
292 SDL_FreeYUVOverlay( sdl_overlay
);
294 sdl_overlay
= SDL_CreateYUVOverlay( width
, height
, SDL_YUY2_OVERLAY
, sdl_screen
);
295 sdl_unlock_display();
299 if ( sdl_screen
!= NULL
&& sdl_overlay
!= NULL
)
301 buffer
= sdl_overlay
->pixels
[ 0 ];
302 if ( sdl_lock_display() )
304 if ( SDL_LockYUVOverlay( sdl_overlay
) >= 0 )
306 memcpy( buffer
, image
, width
* height
* 2 );
307 SDL_UnlockYUVOverlay( sdl_overlay
);
308 SDL_DisplayYUVOverlay( sdl_overlay
, &sdl_screen
->clip_rect
);
310 sdl_unlock_display();
315 // TODO: allocate buffer?
319 mlt_frame_close( frame
);
328 if ( init_audio
== 0 )
330 if ( sdl_overlay
!= NULL
)
331 SDL_FreeYUVOverlay( sdl_overlay
);
337 static int consumer_get_dimensions( int *width
, int *height
)
341 // SDL windows manager structure
344 // Specify the SDL Version
345 SDL_VERSION( &wm
.version
);
347 // Get the wm structure
348 if ( SDL_GetWMInfo( &wm
) == 1 )
350 // Check that we have the X11 wm
351 if ( wm
.subsystem
== SDL_SYSWM_X11
)
353 // Get the SDL window
354 Window window
= wm
.info
.x11
.window
;
356 // Get the display session
357 Display
*display
= wm
.info
.x11
.display
;
359 // Get the window attributes
360 XWindowAttributes attr
;
361 XGetWindowAttributes( display
, window
, &attr
);
363 // Determine whether window has changed
364 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
366 // Return width and height
368 *height
= attr
.height
;
375 /** Callback to allow override of the close method.
378 static void consumer_close( mlt_consumer parent
)
380 // Get the actual object
381 consumer_sdl
this = parent
->child
;
383 // Kill the thread and clean up
386 pthread_mutex_lock( &this->audio_mutex
);
387 pthread_cond_broadcast( &this->audio_cond
);
388 pthread_mutex_unlock( &this->audio_mutex
);
390 pthread_join( this->thread
, NULL
);
391 pthread_mutex_destroy( &this->audio_mutex
);
392 pthread_cond_destroy( &this->audio_cond
);
394 // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
395 parent
->close
= NULL
;
396 mlt_consumer_close( parent
);
398 // Finally clean up this