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