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