]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl.c
Added ffmpeg producer
[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 = 2048;
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                                 if ( width != this->width || height != this->height )
287                                 {
288                                         this->width = width;
289                                         this->height = height;
290                                         changed = 1;
291                                 }
292                         }
293
294                         if ( sdl_screen == NULL || changed )
295                         {
296                                 double aspect_ratio = mlt_frame_get_aspect_ratio( frame );
297
298                                 if ( mlt_properties_get_double( properties, "aspect_ratio" ) )
299                                         aspect_ratio = mlt_properties_get_double( properties, "aspect_ratio" );
300
301                                 if ( this->window_width == 0 || this->window_height == 0 )
302                                 {
303                                         this->window_width = width;
304                                         this->window_height = height;
305                                 }
306
307                                 // open SDL window with video overlay, if possible
308                                 sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, sdl_flags );
309
310                                 if ( sdl_screen != NULL )
311                                 {
312                                         SDL_Rect rect;
313                                         if ( this->window_width < this->window_height * aspect_ratio )
314                                         {
315                                                 rect.w = this->window_width;
316                                                 rect.h = this->window_width / aspect_ratio;
317                                         }
318                                         else
319                                         {
320                                                 rect.w = this->window_height * aspect_ratio;
321                                                 rect.h = this->window_height;
322                                         }
323
324                                         rect.x = ( this->window_width - rect.w ) / 2;
325                                         rect.y = ( this->window_height - rect.h ) / 2;
326
327                                         SDL_SetClipRect( sdl_screen, &rect );
328                                         
329                                         // Force an overlay recreation
330                                         if ( sdl_overlay != NULL )
331                                                 SDL_FreeYUVOverlay( sdl_overlay );
332                                         sdl_lock_display();
333                                         sdl_overlay = SDL_CreateYUVOverlay( this->width, this->height, SDL_YUY2_OVERLAY, sdl_screen );
334                                         sdl_unlock_display();
335                                 }
336                         }
337                         
338                         if ( sdl_screen != NULL && sdl_overlay != NULL )
339                         {
340                                 buffer = sdl_overlay->pixels[ 0 ];
341                                 if ( sdl_lock_display() )
342                                 {
343                                         if ( SDL_LockYUVOverlay( sdl_overlay ) >= 0 )
344                                         {
345                                                 mlt_resize_yuv422( buffer, this->width, this->height, image, width, height );
346                                                 SDL_UnlockYUVOverlay( sdl_overlay );
347                                                 SDL_DisplayYUVOverlay( sdl_overlay, &sdl_screen->clip_rect );
348                                         }
349                                         sdl_unlock_display();
350                                 }
351                         }
352                         else
353                         {
354                                 // TODO: allocate buffer?
355                         }
356
357                         // Close the frame
358                         mlt_frame_close( frame );
359                 }
360                 else
361                 {
362                         break;
363                 }
364         } // while
365
366         // internal cleanup
367         if ( init_audio == 0 )
368                 SDL_AudioQuit( );
369         if ( sdl_overlay != NULL )
370                 SDL_FreeYUVOverlay( sdl_overlay );
371         SDL_Quit( );
372
373         return NULL;
374 }
375
376 static int consumer_get_dimensions( int *width, int *height )
377 {
378         int changed = 0;
379
380         // SDL windows manager structure
381         SDL_SysWMinfo wm;
382
383         // Specify the SDL Version
384         SDL_VERSION( &wm.version );
385
386         // Get the wm structure
387         if ( SDL_GetWMInfo( &wm ) == 1 )
388         {
389                 // Check that we have the X11 wm
390                 if ( wm.subsystem == SDL_SYSWM_X11 ) 
391                 {
392                         // Get the SDL window
393                         Window window = wm.info.x11.window;
394
395                         // Get the display session
396                         Display *display = wm.info.x11.display;
397
398                         // Get the window attributes
399                         XWindowAttributes attr;
400                         XGetWindowAttributes( display, window, &attr );
401
402                         // Determine whether window has changed
403                         changed = *width != attr.width || *height != attr.height;
404
405                         // Return width and height
406                         *width = attr.width;
407                         *height = attr.height;
408                 }
409         }
410
411         return changed;
412 }
413
414 /** Callback to allow override of the close method.
415 */
416
417 static void consumer_close( mlt_consumer parent )
418 {
419         // Get the actual object
420         consumer_sdl this = parent->child;
421
422         // Kill the thread and clean up
423         this->running = 0;
424
425         pthread_mutex_lock( &this->audio_mutex );
426         pthread_cond_broadcast( &this->audio_cond );
427         pthread_mutex_unlock( &this->audio_mutex );
428
429         pthread_join( this->thread, NULL );
430         pthread_mutex_destroy( &this->audio_mutex );
431         pthread_cond_destroy( &this->audio_cond );
432                 
433         // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
434         parent->close = NULL;
435         mlt_consumer_close( parent );
436
437         // Finally clean up this
438         free( this );
439 }
440