ppm ffmpeg
[melted] / mlt / 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 // Get the properties of this consumer
192 mlt_properties properties = this->properties;
193
194 // Define a frame pointer
195 mlt_frame frame;
196
197 // internal intialization
198 int sdl_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_HWACCEL | SDL_RESIZABLE;
199 SDL_Surface *sdl_screen = NULL;
200 SDL_Overlay *sdl_overlay = NULL;
201 uint8_t *buffer = NULL;
202 int init_audio = 1;
203 int bytes;
204
205 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
206 {
207 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
208 return NULL;
209 }
210
211 // Loop until told not to
212 while( this->running )
213 {
214 // Get a frame from the service (should never return anything other than 0)
215 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
216 {
217 mlt_image_format vfmt = mlt_image_yuv422;
218 int width = this->width, height = this->height;
219 uint8_t *image;
220
221 mlt_audio_format afmt = mlt_audio_pcm;
222 int channels;
223 int samples;
224 int frequency;
225 int16_t *pcm;
226 int changed = 0;
227
228 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
229 if ( init_audio == 1 )
230 {
231 SDL_AudioSpec request;
232
233 // specify audio format
234 request.freq = frequency;
235 request.format = AUDIO_S16;
236 request.channels = channels;
237 request.samples = 512;
238 request.callback = sdl_fill_audio;
239 request.userdata = (void *)this;
240 if ( SDL_OpenAudio( &request, NULL ) < 0 )
241 {
242 fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
243 break;
244 }
245 SDL_PauseAudio( 0 );
246 init_audio = 0;
247 }
248 bytes = ( samples * channels * 2 );
249 pthread_mutex_lock( &this->audio_mutex );
250 while ( bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
251 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
252 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
253 this->audio_avail += bytes;
254 pthread_cond_broadcast( &this->audio_cond );
255 pthread_mutex_unlock( &this->audio_mutex );
256
257 // Get the image, width and height
258 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
259
260 if ( sdl_screen != NULL )
261 {
262 SDL_Event event;
263
264 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
265
266 while ( SDL_PollEvent( &event ) )
267 {
268 switch( event.type )
269 {
270 case SDL_VIDEORESIZE:
271 this->window_width = event.resize.w;
272 this->window_height = event.resize.h;
273 changed = 1;
274 break;
275 case SDL_KEYDOWN:
276 {
277 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
278 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
279 if ( callback != NULL && producer != NULL )
280 callback( producer, SDL_GetKeyName(event.key.keysym.sym) );
281 }
282 break;
283 }
284 }
285 }
286
287 if ( sdl_screen == NULL || changed )
288 {
289 double aspect_ratio = mlt_frame_get_aspect_ratio( frame );
290
291 if ( this->window_width == 0 || this->window_height == 0 )
292 {
293 this->window_width = width;
294 this->window_height = height;
295 }
296
297 // open SDL window with video overlay, if possible
298 sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, sdl_flags );
299
300 if ( sdl_screen != NULL )
301 {
302 SDL_Rect rect;
303 if ( this->window_width < this->window_height * aspect_ratio )
304 {
305 rect.w = this->window_width;
306 rect.h = this->window_width / aspect_ratio;
307 }
308 else
309 {
310 rect.w = this->window_height * aspect_ratio;
311 rect.h = this->window_height;
312 }
313
314 rect.x = ( this->window_width - rect.w ) / 2;
315 rect.y = ( this->window_height - rect.h ) / 2;
316
317 SDL_SetClipRect( sdl_screen, &rect );
318
319 // Force an overlay recreation
320 if ( sdl_overlay != NULL )
321 SDL_FreeYUVOverlay( sdl_overlay );
322 sdl_lock_display();
323 sdl_overlay = SDL_CreateYUVOverlay( this->width, this->height, SDL_YUY2_OVERLAY, sdl_screen );
324 sdl_unlock_display();
325 }
326 }
327
328 if ( sdl_screen != NULL && sdl_overlay != NULL )
329 {
330 buffer = sdl_overlay->pixels[ 0 ];
331 if ( sdl_lock_display() )
332 {
333 if ( SDL_LockYUVOverlay( sdl_overlay ) >= 0 )
334 {
335 mlt_resize_yuv422( buffer, this->width, this->height, image, width, height );
336 SDL_UnlockYUVOverlay( sdl_overlay );
337 SDL_DisplayYUVOverlay( sdl_overlay, &sdl_screen->clip_rect );
338 }
339 sdl_unlock_display();
340 }
341 }
342 else
343 {
344 // TODO: allocate buffer?
345 }
346
347 // Close the frame
348 mlt_frame_close( frame );
349 }
350 else
351 {
352 break;
353 }
354 } // while
355
356 // internal cleanup
357 if ( init_audio == 0 )
358 SDL_AudioQuit( );
359 if ( sdl_overlay != NULL )
360 SDL_FreeYUVOverlay( sdl_overlay );
361 SDL_Quit( );
362
363 return NULL;
364 }
365
366 static int consumer_get_dimensions( int *width, int *height )
367 {
368 int changed = 0;
369
370 // SDL windows manager structure
371 SDL_SysWMinfo wm;
372
373 // Specify the SDL Version
374 SDL_VERSION( &wm.version );
375
376 // Get the wm structure
377 if ( SDL_GetWMInfo( &wm ) == 1 )
378 {
379 // Check that we have the X11 wm
380 if ( wm.subsystem == SDL_SYSWM_X11 )
381 {
382 // Get the SDL window
383 Window window = wm.info.x11.window;
384
385 // Get the display session
386 Display *display = wm.info.x11.display;
387
388 // Get the window attributes
389 XWindowAttributes attr;
390 XGetWindowAttributes( display, window, &attr );
391
392 // Determine whether window has changed
393 changed = *width != attr.width || *height != attr.height;
394
395 // Return width and height
396 *width = attr.width;
397 *height = attr.height;
398 }
399 }
400
401 return changed;
402 }
403
404 /** Callback to allow override of the close method.
405 */
406
407 static void consumer_close( mlt_consumer parent )
408 {
409 // Get the actual object
410 consumer_sdl this = parent->child;
411
412 // Kill the thread and clean up
413 this->running = 0;
414
415 pthread_mutex_lock( &this->audio_mutex );
416 pthread_cond_broadcast( &this->audio_cond );
417 pthread_mutex_unlock( &this->audio_mutex );
418
419 pthread_join( this->thread, NULL );
420 pthread_mutex_destroy( &this->audio_mutex );
421 pthread_cond_destroy( &this->audio_cond );
422
423 // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
424 parent->close = NULL;
425 mlt_consumer_close( parent );
426
427 // Finally clean up this
428 free( this );
429 }
430