]> git.sesse.net Git - mlt/blob - mlt/src/modules/sdl/consumer_sdl.c
714fc66f5a6351512192b8b14b781d74601415ac
[mlt] / 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 * 3 ];
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         float aspect_ratio;
50         int width;
51         int height;
52         int playing;
53         mlt_frame *queue;
54         int size;
55         int count;
56         int sdl_flags;
57         SDL_Surface *sdl_screen;
58         SDL_Overlay *sdl_overlay;
59         uint8_t *buffer;
60 };
61
62 /** Forward references to static functions.
63 */
64
65 static void consumer_close( mlt_consumer parent );
66 static void *consumer_thread( void * );
67 static int consumer_get_dimensions( int *width, int *height );
68
69 /** This is what will be called by the factory - anything can be passed in
70         via the argument, but keep it simple.
71 */
72
73 mlt_consumer consumer_sdl_init( char *arg )
74 {
75         // Create the consumer object
76         consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
77
78         // If no malloc'd and consumer init ok
79         if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
80         {
81                 // Get the parent consumer object
82                 mlt_consumer parent = &this->parent;
83
84                 // We have stuff to clean up, so override the close method
85                 parent->close = consumer_close;
86
87                 // get a handle on properties
88                 mlt_service service = mlt_consumer_service( parent );
89                 this->properties = mlt_service_properties( service );
90
91                 // Set the default volume
92                 mlt_properties_set_double( this->properties, "volume", 1.0 );
93
94                 // This is the initialisation of the consumer
95                 this->running = 1;
96                 pthread_mutex_init( &this->audio_mutex, NULL );
97                 pthread_cond_init( &this->audio_cond, NULL);
98                 
99                 // process actual param
100                 if ( arg == NULL || !strcmp( arg, "PAL" ) )
101                 {
102                         this->width = 720;
103                         this->height = 576;
104                 }
105                 else if ( !strcmp( arg, "NTSC" ) )
106                 {
107                         this->width = 720;
108                         this->height = 480;
109                 }
110                 else if ( sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
111                 {
112                         this->width = 720;
113                         this->height = 576;
114                 }
115
116                 // Default window size and aspect ratio
117                 this->aspect_ratio = 4.0 / 3.0;
118                 this->window_width = (int)( (float)this->height * this->aspect_ratio ) + 1;
119                 this->window_height = this->height;
120
121                 // Set the sdl flags
122                 this->sdl_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_HWACCEL | SDL_RESIZABLE;
123
124                 // Create the the thread
125                 pthread_create( &this->thread, NULL, consumer_thread, this );
126
127                 // Return the consumer produced
128                 return parent;
129         }
130
131         // malloc or consumer init failed
132         free( this );
133
134         // Indicate failure
135         return NULL;
136 }
137
138 static int sdl_lock_display( )
139 {
140         SDL_Surface *screen = SDL_GetVideoSurface( );
141         return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
142 }
143
144 static void sdl_unlock_display( )
145 {
146         SDL_Surface *screen = SDL_GetVideoSurface( );
147         if ( screen != NULL && SDL_MUSTLOCK( screen ) )
148                 SDL_UnlockSurface( screen );
149 }
150
151 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
152 {
153         consumer_sdl this = udata;
154
155         // Get the volume
156         float volume = mlt_properties_get_double( this->properties, "volume" );
157
158         pthread_mutex_lock( &this->audio_mutex );
159
160         // Block until audio received
161         while ( this->running && len > this->audio_avail )
162                 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
163
164         if ( this->audio_avail >= len )
165         {
166                 // Place in the audio buffer
167                 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
168
169                 // Remove len from the audio available
170                 this->audio_avail -= len;
171
172                 // Remove the samples
173                 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
174         }
175         else
176         {
177                 // Just to be safe, wipe the stream first
178                 memset( stream, 0, len );
179
180                 // Copy what we have into the stream
181                 memcpy( stream, this->audio_buffer, this->audio_avail );
182
183                 // Mix the audio 
184                 SDL_MixAudio( stream, stream, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
185
186                 // No audio left
187                 this->audio_avail = 0;
188         }
189
190         // We're definitely playing now
191         this->playing = 1;
192
193         pthread_cond_broadcast( &this->audio_cond );
194         pthread_mutex_unlock( &this->audio_mutex );
195 }
196
197 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio )
198 {
199         // Get the properties of this consumer
200         mlt_properties properties = this->properties;
201         mlt_audio_format afmt = mlt_audio_pcm;
202         int channels;
203         int samples;
204         int frequency;
205         int16_t *pcm;
206         int bytes;
207
208         mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
209
210         if ( mlt_properties_get_int( properties, "audio_off" ) )
211         {
212                 this->playing = 1;
213                 return init_audio;
214         }
215
216         if ( init_audio == 1 )
217         {
218                 SDL_AudioSpec request;
219                 SDL_AudioSpec got;
220
221                 // specify audio format
222                 memset( &request, 0, sizeof( SDL_AudioSpec ) );
223                 this->playing = 0;
224                 request.freq = frequency;
225                 request.format = AUDIO_S16;
226                 request.channels = channels;
227                 request.samples = 1024;
228                 request.callback = sdl_fill_audio;
229                 request.userdata = (void *)this;
230                 if ( SDL_OpenAudio( &request, &got ) != 0 )
231                 {
232                         fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
233                         init_audio = 2;
234                 }
235                 else
236                 {
237                         if ( got.size != 0 )
238                         {
239                                 SDL_PauseAudio( 0 );
240                                 init_audio = 0;
241                         }
242                 }
243         }
244
245         if ( init_audio == 0 )
246         {
247                 bytes = ( samples * channels * 2 );
248                 pthread_mutex_lock( &this->audio_mutex );
249                 while ( bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
250                         pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
251                 mlt_properties properties = mlt_frame_properties( frame );
252                 if ( mlt_properties_get_double( properties, "speed" ) == 1 )
253                         memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
254                 else
255                         memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
256                 this->audio_avail += bytes;
257                 pthread_cond_broadcast( &this->audio_cond );
258                 pthread_mutex_unlock( &this->audio_mutex );
259         }
260         else
261         {
262                 this->playing = 1;
263         }
264
265         return init_audio;
266 }
267
268 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
269 {
270         // Get the properties of this consumer
271         mlt_properties properties = this->properties;
272
273         mlt_image_format vfmt = mlt_image_yuv422;
274         int width = this->width, height = this->height;
275         uint8_t *image;
276         int changed = 0;
277
278         if ( mlt_properties_get_int( properties, "video_off" ) )
279         {
280                 mlt_frame_close( frame );
281                 return 0;
282         }
283
284         if ( this->count == this->size )
285         {
286                 this->size += 25;
287                 this->queue = realloc( this->queue, sizeof( mlt_frame ) * this->size );
288         }
289         this->queue[ this->count ++ ] = frame;
290
291         if ( this->playing )
292         {
293                 // We're working on the oldest frame now
294                 frame = this->queue[ 0 ];
295
296                 // Shunt the frames in the queue down
297                 int i = 0;
298                 for ( i = 1; i < this->count; i ++ )
299                         this->queue[ i - 1 ] = this->queue[ i ];
300                 this->count --;
301
302                 // Get the image, width and height
303                 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
304
305                 if ( this->sdl_screen != NULL )
306                 {
307                         SDL_Event event;
308
309                         changed = consumer_get_dimensions( &this->window_width, &this->window_height );
310
311                         while ( SDL_PollEvent( &event ) )
312                         {
313                                 switch( event.type )
314                                 {
315                                         case SDL_VIDEORESIZE:
316                                                 this->window_width = event.resize.w;
317                                                 this->window_height = event.resize.h;
318                                                 changed = 1;
319                                                 break;
320                                         case SDL_KEYDOWN:
321                                                 {
322                                                         mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
323                                                         void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
324                                                         if ( callback != NULL && producer != NULL )
325                                                                 callback( producer, SDL_GetKeyName(event.key.keysym.sym) );
326                                                 }
327                                                 break;
328                                 }
329                         }
330
331                 }
332
333                 if ( width != this->width || height != this->height )
334                 {
335                         this->width = width;
336                         this->height = height;
337                         changed = 1;
338                 }
339
340                 if ( this->sdl_screen == NULL || changed )
341                 {
342                         double aspect_ratio = mlt_frame_get_aspect_ratio( frame );
343                         float display_aspect_ratio = (float)width / (float)height;
344                         SDL_Rect rect;
345
346                         if ( mlt_properties_get_double( properties, "aspect_ratio" ) )
347                                 aspect_ratio = mlt_properties_get_double( properties, "aspect_ratio" );
348
349                         if ( aspect_ratio == 1 )
350                         {
351                                 rect.w = this->window_width;
352                                 rect.h = this->window_height;
353                         }
354                         else if ( this->window_width < this->window_height * aspect_ratio )
355                         {
356                                 rect.w = this->window_width;
357                                 rect.h = this->window_width / aspect_ratio;
358                         }
359                         else
360                         {
361                                 rect.w = this->window_height * aspect_ratio;
362                                 rect.h = this->window_height;
363                         }
364
365                         if ( mlt_properties_get_int( properties, "scale_overlay" ) )
366                         {
367                                 if ( ( float )rect.w * display_aspect_ratio < this->window_width )
368                                         rect.w = ( int )( ( float )rect.w * display_aspect_ratio );
369                                 else if ( ( float )rect.h * display_aspect_ratio < this->window_height )
370                                         rect.h = ( int )( ( float )rect.h * display_aspect_ratio );
371                         }
372
373                         rect.x = ( this->window_width - rect.w ) / 2;
374                         rect.y = ( this->window_height - rect.h ) / 2;
375
376                         // Force an overlay recreation
377                         if ( this->sdl_overlay != NULL )
378                                 SDL_FreeYUVOverlay( this->sdl_overlay );
379
380                         // open SDL window with video overlay, if possible
381                         this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
382
383                         if ( this->sdl_screen != NULL )
384                         {
385                                 SDL_SetClipRect( this->sdl_screen, &rect );
386                         
387                                 sdl_lock_display();
388                                 this->sdl_overlay = SDL_CreateYUVOverlay( this->width - (this->width % 4), this->height- (this->height % 2 ), SDL_YUY2_OVERLAY, this->sdl_screen );
389                                 sdl_unlock_display();
390                         }
391                 }
392                         
393                 if ( this->sdl_screen != NULL && this->sdl_overlay != NULL )
394                 {
395                         this->buffer = this->sdl_overlay->pixels[ 0 ];
396                         if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
397                         {
398                                 mlt_resize_yuv422( this->buffer, this->width - (this->width % 4 ), this->height- (this->height % 2 ), image, width, height );
399                                 SDL_UnlockYUVOverlay( this->sdl_overlay );
400                                 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
401                         }
402                 }
403         }
404         else
405         {
406                 frame = NULL;
407         }
408
409         // Close the frame
410         if ( frame != NULL )
411                 mlt_frame_close( frame );
412
413         if ( this->count )
414                 mlt_frame_get_image( this->queue[ this->count - 1 ], &image, &vfmt, &width, &height, 0 );
415
416         return 0;
417 }
418
419 /** Threaded wrapper for pipe.
420 */
421
422 static void *consumer_thread( void *arg )
423 {
424         // Identify the arg
425         consumer_sdl this = arg;
426
427         // Get the consumer
428         mlt_consumer consumer = &this->parent;
429
430         // Get the service assoicated to the consumer
431         mlt_service service = mlt_consumer_service( consumer );
432
433         // Define a frame pointer
434         mlt_frame frame;
435
436         // internal intialization
437         int init_audio = 1;
438
439         if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
440         {
441                 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
442                 return NULL;
443         }
444         
445         // Loop until told not to
446         while( this->running )
447         {
448                 // Get a frame from the service (should never return anything other than 0)
449                 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
450                 {
451                         init_audio = consumer_play_audio( this, frame, init_audio );
452                         consumer_play_video( this, frame );
453                 }
454         }
455
456         // internal cleanup
457         if ( init_audio == 0 )
458                 SDL_AudioQuit( );
459         if ( this->sdl_overlay != NULL )
460                 SDL_FreeYUVOverlay( this->sdl_overlay );
461         SDL_Quit( );
462
463         return NULL;
464 }
465
466 static int consumer_get_dimensions( int *width, int *height )
467 {
468         int changed = 0;
469
470         // SDL windows manager structure
471         SDL_SysWMinfo wm;
472
473         // Specify the SDL Version
474         SDL_VERSION( &wm.version );
475
476         // Get the wm structure
477         if ( SDL_GetWMInfo( &wm ) == 1 )
478         {
479                 // Check that we have the X11 wm
480                 if ( wm.subsystem == SDL_SYSWM_X11 ) 
481                 {
482                         // Get the SDL window
483                         Window window = wm.info.x11.window;
484
485                         // Get the display session
486                         Display *display = wm.info.x11.display;
487
488                         // Get the window attributes
489                         XWindowAttributes attr;
490                         XGetWindowAttributes( display, window, &attr );
491
492                         // Determine whether window has changed
493                         changed = *width != attr.width || *height != attr.height;
494
495                         // Return width and height
496                         *width = attr.width;
497                         *height = attr.height;
498                 }
499         }
500
501         return changed;
502 }
503
504 /** Callback to allow override of the close method.
505 */
506
507 static void consumer_close( mlt_consumer parent )
508 {
509         // Get the actual object
510         consumer_sdl this = parent->child;
511
512         // Kill the thread and clean up
513         this->running = 0;
514
515         pthread_mutex_lock( &this->audio_mutex );
516         pthread_cond_broadcast( &this->audio_cond );
517         pthread_mutex_unlock( &this->audio_mutex );
518
519         pthread_join( this->thread, NULL );
520         pthread_mutex_destroy( &this->audio_mutex );
521         pthread_cond_destroy( &this->audio_cond );
522                 
523         // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
524         parent->close = NULL;
525         mlt_consumer_close( parent );
526
527         // Finally clean up this
528         free( this );
529 }
530