field and playlist enhancements, producer pixbuf reorg
[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 };
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 void 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
206 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
207 if ( init_audio == 1 )
208 {
209 SDL_AudioSpec request;
210
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 )
219 {
220 fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
221 break;
222 }
223 SDL_PauseAudio( 0 );
224 init_audio = 0;
225 }
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 );
234
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 )
238 {
239 SDL_Rect rect;
240 this->window_width = rect.w = width;
241 this->window_height = rect.h = height;
242
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 )
247 {
248 rect.x = rect.y = 0;
249
250
251 // XXX: this is a little hack until we have some sort of aspect
252 // ratio property on images.
253 if ( rect.h <= 486 )
254 {
255 rect.w = 640;
256 rect.x = ( 720 - 640 ) / 2;
257 }
258 else
259 {
260 rect.h = 540;
261 rect.y = ( 576 - 540 ) / 2;
262 }
263
264 SDL_SetClipRect( sdl_screen, &rect );
265
266 // Force an overlay recreation
267 if ( sdl_overlay != NULL )
268 SDL_FreeYUVOverlay( sdl_overlay );
269 sdl_lock_display();
270 sdl_overlay = SDL_CreateYUVOverlay( width, height, SDL_YUY2_OVERLAY, sdl_screen );
271 sdl_unlock_display();
272 }
273 }
274
275 if ( sdl_screen != NULL && sdl_overlay != NULL )
276 {
277 buffer = sdl_overlay->pixels[ 0 ];
278 if ( sdl_lock_display() )
279 {
280 if ( SDL_LockYUVOverlay( sdl_overlay ) >= 0 )
281 {
282 memcpy( buffer, image, width * height * 2 );
283 SDL_UnlockYUVOverlay( sdl_overlay );
284 SDL_DisplayYUVOverlay( sdl_overlay, &sdl_screen->clip_rect );
285 }
286 sdl_unlock_display();
287 }
288 }
289 else
290 {
291 // TODO: allocate buffer?
292 }
293
294 // Close the frame
295 mlt_frame_close( frame );
296 }
297 else
298 {
299 break;
300 }
301 } // while
302
303 // internal cleanup
304 if ( init_audio == 0 )
305 SDL_AudioQuit( );
306 if ( sdl_overlay != NULL )
307 SDL_FreeYUVOverlay( sdl_overlay );
308 SDL_Quit( );
309
310 return NULL;
311 }
312
313 static void consumer_get_dimensions( int *width, int *height )
314 {
315 // SDL windows manager structure
316 SDL_SysWMinfo wm;
317
318 // Specify the SDL Version
319 SDL_VERSION( &wm.version );
320
321 // Get the wm structure
322 if ( SDL_GetWMInfo( &wm ) == 1 )
323 {
324 // Check that we have the X11 wm
325 if ( wm.subsystem == SDL_SYSWM_X11 )
326 {
327 // Get the SDL window
328 Window window = wm.info.x11.window;
329
330 // Get the display session
331 Display *display = wm.info.x11.display;
332
333 // Get the window attributes
334 XWindowAttributes attr;
335 XGetWindowAttributes( display, window, &attr );
336
337 // Return width and height
338 *width = attr.width;
339 *height = attr.height;
340 }
341 }
342 }
343
344 /** Callback to allow override of the close method.
345 */
346
347 static void consumer_close( mlt_consumer parent )
348 {
349 // Get the actual object
350 consumer_sdl this = parent->child;
351
352 // Kill the thread and clean up
353 this->running = 0;
354
355 pthread_mutex_lock( &this->audio_mutex );
356 pthread_cond_broadcast( &this->audio_cond );
357 pthread_mutex_unlock( &this->audio_mutex );
358
359 pthread_join( this->thread, NULL );
360 pthread_mutex_destroy( &this->audio_mutex );
361 pthread_cond_destroy( &this->audio_cond );
362
363 // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
364 parent->close = NULL;
365 mlt_consumer_close( parent );
366
367 // Finally clean up this
368 free( this );
369 }
370