SDL updates and resizing fix
[melted] / src / modules / sdl / consumer_sdl.c
1 /*
2 * consumer_sdl.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include "consumer_sdl.h"
22 #include <framework/mlt_frame.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <pthread.h>
27 #include <SDL/SDL.h>
28 #include <SDL/SDL_syswm.h>
29
30 /** This classes definition.
31 */
32
33 typedef struct consumer_sdl_s *consumer_sdl;
34
35 struct consumer_sdl_s
36 {
37 struct mlt_consumer_s parent;
38 mlt_properties properties;
39 int format;
40 int video;
41 pthread_t thread;
42 int running;
43 uint8_t audio_buffer[ 4096 * 6 ];
44 int audio_avail;
45 pthread_mutex_t audio_mutex;
46 pthread_cond_t audio_cond;
47 int window_width;
48 int window_height;
49 };
50
51 /** Forward references to static functions.
52 */
53
54 static void consumer_close( mlt_consumer parent );
55 static void *consumer_thread( void * );
56 static int consumer_get_dimensions( int *width, int *height );
57
58 /** This is what will be called by the factory - anything can be passed in
59 via the argument, but keep it simple.
60 */
61
62 mlt_consumer consumer_sdl_init( void *dummy )
63 {
64 // Create the consumer object
65 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
66
67 // If no malloc'd and consumer init ok
68 if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
69 {
70 // Get the parent consumer object
71 mlt_consumer parent = &this->parent;
72
73 // We have stuff to clean up, so override the close method
74 parent->close = consumer_close;
75
76 // get a handle on properties
77 mlt_service service = mlt_consumer_service( parent );
78 this->properties = mlt_service_properties( service );
79
80 // Set the default volume
81 mlt_properties_set_double( this->properties, "volume", 1.0 );
82
83 // This is the initialisation of the consumer
84 this->running = 1;
85 pthread_mutex_init( &this->audio_mutex, NULL );
86 pthread_cond_init( &this->audio_cond, NULL);
87
88 // TODO: process actual param
89
90 // Create the the thread
91 pthread_create( &this->thread, NULL, consumer_thread, this );
92
93 // Return the consumer produced
94 return parent;
95 }
96
97 // malloc or consumer init failed
98 free( this );
99
100 // Indicate failure
101 return NULL;
102 }
103
104 static int sdl_lock_display( )
105 {
106 SDL_Surface *screen = SDL_GetVideoSurface( );
107 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
108 }
109
110 static void sdl_unlock_display( )
111 {
112 SDL_Surface *screen = SDL_GetVideoSurface( );
113 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
114 SDL_UnlockSurface( screen );
115 }
116
117 void sdl_fill_audio( void *udata, uint8_t *stream, int len )
118 {
119 consumer_sdl this = udata;
120
121 // Get the volume
122 float volume = mlt_properties_get_double( this->properties, "volume" );
123
124 pthread_mutex_lock( &this->audio_mutex );
125
126 // Experimental - block until audio received
127 while ( this->running && len > this->audio_avail )
128 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
129
130 if ( this->audio_avail >= len )
131 {
132 // Place in the audio buffer
133 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
134
135 // Remove len from the audio available
136 this->audio_avail -= len;
137
138 // Remove the samples
139 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
140 }
141 else
142 {
143 // Just to be safe, wipe the stream first
144 memset( stream, 0, len );
145
146 // Copy what we have into the stream
147 memcpy( stream, this->audio_buffer, this->audio_avail );
148
149 // Mix the audio
150 SDL_MixAudio( stream, stream, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
151
152 // No audio left
153 this->audio_avail = 0;
154 }
155 pthread_cond_broadcast( &this->audio_cond );
156 pthread_mutex_unlock( &this->audio_mutex );
157 }
158
159 /** Threaded wrapper for pipe.
160 */
161
162 static void *consumer_thread( void *arg )
163 {
164 // Identify the arg
165 consumer_sdl this = arg;
166
167 // Get the consumer
168 mlt_consumer consumer = &this->parent;
169
170 // Get the service assoicated to the consumer
171 mlt_service service = mlt_consumer_service( consumer );
172
173 // Define a frame pointer
174 mlt_frame frame;
175
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;
181 int init_audio = 1;
182 int bytes;
183
184 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
185 {
186 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
187 return NULL;
188 }
189
190 // Loop until told not to
191 while( this->running )
192 {
193 // Get a frame from the service (should never return anything other than 0)
194 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
195 {
196 mlt_image_format vfmt = mlt_image_yuv422;
197 int width, height;
198 uint8_t *image;
199
200 mlt_audio_format afmt = mlt_audio_pcm;
201 int channels;
202 int samples;
203 int frequency;
204 int16_t *pcm;
205 int changed = 0;
206
207 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
208 if ( init_audio == 1 )
209 {
210 SDL_AudioSpec request;
211
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 )
220 {
221 fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
222 break;
223 }
224 SDL_PauseAudio( 0 );
225 init_audio = 0;
226 }
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 );
235
236 // Get the image, width and height
237 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
238
239 if ( sdl_screen != NULL )
240 {
241 SDL_Event event;
242
243 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
244
245 while ( SDL_PollEvent( &event ) )
246 {
247 switch( event.type )
248 {
249 case SDL_VIDEORESIZE:
250 this->window_width = event.resize.w;
251 this->window_height = event.resize.h;
252 changed = 1;
253 break;
254 }
255 }
256 }
257
258 if ( sdl_screen == NULL || changed )
259 {
260 double aspect_ratio = mlt_frame_get_aspect_ratio( frame );
261
262 if ( this->window_width == 0 || this->window_height == 0 )
263 {
264 this->window_width = width;
265 this->window_height = height;
266 }
267
268 // open SDL window with video overlay, if possible
269 sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, sdl_flags );
270
271 if ( sdl_screen != NULL )
272 {
273 SDL_Rect rect;
274 if ( this->window_width < this->window_height * aspect_ratio )
275 {
276 rect.w = this->window_width;
277 rect.h = this->window_width / aspect_ratio;
278 }
279 else
280 {
281 rect.w = this->window_height * aspect_ratio;
282 rect.h = this->window_height;
283 }
284
285 rect.x = ( this->window_width - rect.w ) / 2;
286 rect.y = ( this->window_height - rect.h ) / 2;
287
288 SDL_SetClipRect( sdl_screen, &rect );
289
290 // Force an overlay recreation
291 if ( sdl_overlay != NULL )
292 SDL_FreeYUVOverlay( sdl_overlay );
293 sdl_lock_display();
294 sdl_overlay = SDL_CreateYUVOverlay( width, height, SDL_YUY2_OVERLAY, sdl_screen );
295 sdl_unlock_display();
296 }
297 }
298
299 if ( sdl_screen != NULL && sdl_overlay != NULL )
300 {
301 buffer = sdl_overlay->pixels[ 0 ];
302 if ( sdl_lock_display() )
303 {
304 if ( SDL_LockYUVOverlay( sdl_overlay ) >= 0 )
305 {
306 memcpy( buffer, image, width * height * 2 );
307 SDL_UnlockYUVOverlay( sdl_overlay );
308 SDL_DisplayYUVOverlay( sdl_overlay, &sdl_screen->clip_rect );
309 }
310 sdl_unlock_display();
311 }
312 }
313 else
314 {
315 // TODO: allocate buffer?
316 }
317
318 // Close the frame
319 mlt_frame_close( frame );
320 }
321 else
322 {
323 break;
324 }
325 } // while
326
327 // internal cleanup
328 if ( init_audio == 0 )
329 SDL_AudioQuit( );
330 if ( sdl_overlay != NULL )
331 SDL_FreeYUVOverlay( sdl_overlay );
332 SDL_Quit( );
333
334 return NULL;
335 }
336
337 static int consumer_get_dimensions( int *width, int *height )
338 {
339 int changed = 0;
340
341 // SDL windows manager structure
342 SDL_SysWMinfo wm;
343
344 // Specify the SDL Version
345 SDL_VERSION( &wm.version );
346
347 // Get the wm structure
348 if ( SDL_GetWMInfo( &wm ) == 1 )
349 {
350 // Check that we have the X11 wm
351 if ( wm.subsystem == SDL_SYSWM_X11 )
352 {
353 // Get the SDL window
354 Window window = wm.info.x11.window;
355
356 // Get the display session
357 Display *display = wm.info.x11.display;
358
359 // Get the window attributes
360 XWindowAttributes attr;
361 XGetWindowAttributes( display, window, &attr );
362
363 // Determine whether window has changed
364 changed = *width != attr.width || *height != attr.height;
365
366 // Return width and height
367 *width = attr.width;
368 *height = attr.height;
369 }
370 }
371
372 return changed;
373 }
374
375 /** Callback to allow override of the close method.
376 */
377
378 static void consumer_close( mlt_consumer parent )
379 {
380 // Get the actual object
381 consumer_sdl this = parent->child;
382
383 // Kill the thread and clean up
384 this->running = 0;
385
386 pthread_mutex_lock( &this->audio_mutex );
387 pthread_cond_broadcast( &this->audio_cond );
388 pthread_mutex_unlock( &this->audio_mutex );
389
390 pthread_join( this->thread, NULL );
391 pthread_mutex_destroy( &this->audio_mutex );
392 pthread_cond_destroy( &this->audio_cond );
393
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 );
397
398 // Finally clean up this
399 free( this );
400 }
401