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 void 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
*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
;
206 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
207 if ( init_audio
== 1 )
209 SDL_AudioSpec request
;
211 // specify audio format
212 request
.freq
= frequency
;
213 request
.format
= AUDIO_S16
;
214 request
.channels
= channels
;
215 request
.samples
= 1024;
216 request
.callback
= sdl_fill_audio
;
217 request
.userdata
= (void *)this;
218 if ( SDL_OpenAudio( &request
, NULL
) < 0 )
220 fprintf( stderr
, "SDL failed to open audio: %s\n", SDL_GetError() );
226 bytes
= ( samples
* channels
* 2 );
227 pthread_mutex_lock( &this->audio_mutex
);
228 while ( bytes
> ( sizeof( this->audio_buffer
) - this->audio_avail
) )
229 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
230 memcpy( &this->audio_buffer
[ this->audio_avail
], pcm
, bytes
);
231 this->audio_avail
+= bytes
;
232 pthread_cond_broadcast( &this->audio_cond
);
233 pthread_mutex_unlock( &this->audio_mutex
);
235 // Get the image, width and height
236 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
237 if ( width
!= this->window_width
|| height
!= this->window_height
)
240 this->window_width
= rect
.w
= width
;
241 this->window_height
= rect
.h
= height
;
243 // open SDL window with video overlay, if possible
244 if ( sdl_screen
== NULL
)
245 sdl_screen
= SDL_SetVideoMode( width
, height
, 0, sdl_flags
);
246 if ( sdl_screen
!= NULL
)
251 // XXX: this is a little hack until we have some sort of aspect
252 // ratio property on images.
256 rect
.x
= ( 720 - 640 ) / 2;
261 rect
.y
= ( 576 - 540 ) / 2;
264 SDL_SetClipRect( sdl_screen
, &rect
);
266 // Force an overlay recreation
267 if ( sdl_overlay
!= NULL
)
268 SDL_FreeYUVOverlay( sdl_overlay
);
270 sdl_overlay
= SDL_CreateYUVOverlay( width
, height
, SDL_YUY2_OVERLAY
, sdl_screen
);
271 sdl_unlock_display();
275 if ( sdl_screen
!= NULL
&& sdl_overlay
!= NULL
)
277 buffer
= sdl_overlay
->pixels
[ 0 ];
278 if ( sdl_lock_display() )
280 if ( SDL_LockYUVOverlay( sdl_overlay
) >= 0 )
282 memcpy( buffer
, image
, width
* height
* 2 );
283 SDL_UnlockYUVOverlay( sdl_overlay
);
284 SDL_DisplayYUVOverlay( sdl_overlay
, &sdl_screen
->clip_rect
);
286 sdl_unlock_display();
291 // TODO: allocate buffer?
295 mlt_frame_close( frame
);
304 if ( init_audio
== 0 )
306 if ( sdl_overlay
!= NULL
)
307 SDL_FreeYUVOverlay( sdl_overlay
);
313 static void consumer_get_dimensions( int *width
, int *height
)
315 // SDL windows manager structure
318 // Specify the SDL Version
319 SDL_VERSION( &wm
.version
);
321 // Get the wm structure
322 if ( SDL_GetWMInfo( &wm
) == 1 )
324 // Check that we have the X11 wm
325 if ( wm
.subsystem
== SDL_SYSWM_X11
)
327 // Get the SDL window
328 Window window
= wm
.info
.x11
.window
;
330 // Get the display session
331 Display
*display
= wm
.info
.x11
.display
;
333 // Get the window attributes
334 XWindowAttributes attr
;
335 XGetWindowAttributes( display
, window
, &attr
);
337 // Return width and height
339 *height
= attr
.height
;
344 /** Callback to allow override of the close method.
347 static void consumer_close( mlt_consumer parent
)
349 // Get the actual object
350 consumer_sdl
this = parent
->child
;
352 // Kill the thread and clean up
354 pthread_join( this->thread
, NULL
);
356 pthread_mutex_destroy( &this->audio_mutex
);
357 pthread_cond_destroy( &this->audio_cond
);
359 // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
360 parent
->close
= NULL
;
361 mlt_consumer_close( parent
);
363 // Finally clean up this