]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl.c
0de719e7f4235e97f95c0a05b279a2c11d024e4d
[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 <framework/mlt_deque.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <pthread.h>
28 #include <SDL/SDL.h>
29 #include <SDL/SDL_syswm.h>
30 #include <sys/time.h>
31
32 /** This classes definition.
33 */
34
35 typedef struct consumer_sdl_s *consumer_sdl;
36
37 struct consumer_sdl_s
38 {
39         struct mlt_consumer_s parent;
40         mlt_properties properties;
41         mlt_deque queue;
42         pthread_t thread;
43         int running;
44         uint8_t audio_buffer[ 4096 * 19 ];
45         int audio_avail;
46         pthread_mutex_t audio_mutex;
47         pthread_cond_t audio_cond;
48         int window_width;
49         int window_height;
50         float aspect_ratio;
51         int width;
52         int height;
53         int playing;
54         int sdl_flags;
55         SDL_Surface *sdl_screen;
56         SDL_Overlay *sdl_overlay;
57         uint8_t *buffer;
58         int time_taken;
59 };
60
61 /** Forward references to static functions.
62 */
63
64 static int consumer_start( mlt_consumer parent );
65 static int consumer_stop( mlt_consumer parent );
66 static int consumer_is_stopped( mlt_consumer parent );
67 static void consumer_close( mlt_consumer parent );
68 static void *consumer_thread( void * );
69 static int consumer_get_dimensions( int *width, int *height );
70
71 /** This is what will be called by the factory - anything can be passed in
72         via the argument, but keep it simple.
73 */
74
75 mlt_consumer consumer_sdl_init( char *arg )
76 {
77         // Create the consumer object
78         consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
79
80         // If no malloc'd and consumer init ok
81         if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
82         {
83                 // Create the queue
84                 this->queue = mlt_deque_init( );
85
86                 // Get the parent consumer object
87                 mlt_consumer parent = &this->parent;
88
89                 // We have stuff to clean up, so override the close method
90                 parent->close = consumer_close;
91
92                 // get a handle on properties
93                 mlt_service service = mlt_consumer_service( parent );
94                 this->properties = mlt_service_properties( service );
95
96                 // Set the default volume
97                 mlt_properties_set_double( this->properties, "volume", 1.0 );
98
99                 // This is the initialisation of the consumer
100                 pthread_mutex_init( &this->audio_mutex, NULL );
101                 pthread_cond_init( &this->audio_cond, NULL);
102                 
103                 // Default scaler (for now we'll use nearest)
104                 mlt_properties_set( this->properties, "rescale", "nearest" );
105
106                 // Default buffer for low latency
107                 mlt_properties_set_int( this->properties, "buffer", 8 );
108
109                 // Default progressive true
110                 mlt_properties_set_int( this->properties, "progressive", 1 );
111
112                 // Get aspect ratio
113                 this->aspect_ratio = mlt_properties_get_double( this->properties, "aspect_ratio" );
114                 
115                 // process actual param
116                 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
117                 {
118                         this->width = mlt_properties_get_int( this->properties, "width" );
119                         this->height = mlt_properties_get_int( this->properties, "height" );
120                 }
121
122                 // Default window size
123                 this->window_width = (int)( (float)this->height * this->aspect_ratio ) + 1;
124                 this->window_height = this->height;
125                 
126                 // Set the sdl flags
127                 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE;
128
129                 // Allow thread to be started/stopped
130                 parent->start = consumer_start;
131                 parent->stop = consumer_stop;
132                 parent->is_stopped = consumer_is_stopped;
133
134                 // Return the consumer produced
135                 return parent;
136         }
137
138         // malloc or consumer init failed
139         free( this );
140
141         // Indicate failure
142         return NULL;
143 }
144
145 int consumer_start( mlt_consumer parent )
146 {
147         consumer_sdl this = parent->child;
148
149         if ( !this->running )
150         {
151                 this->running = 1;
152                 pthread_create( &this->thread, NULL, consumer_thread, this );
153         }
154
155         return 0;
156 }
157
158 int consumer_stop( mlt_consumer parent )
159 {
160         // Get the actual object
161         consumer_sdl this = parent->child;
162
163         if ( this->running )
164         {
165                 // Kill the thread and clean up
166                 this->running = 0;
167
168                 pthread_mutex_lock( &this->audio_mutex );
169                 pthread_cond_broadcast( &this->audio_cond );
170                 pthread_mutex_unlock( &this->audio_mutex );
171
172                 pthread_join( this->thread, NULL );
173         }
174
175         return 0;
176 }
177
178 int consumer_is_stopped( mlt_consumer parent )
179 {
180         consumer_sdl this = parent->child;
181         return !this->running;
182 }
183
184 static int sdl_lock_display( )
185 {
186         SDL_Surface *screen = SDL_GetVideoSurface( );
187         return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
188 }
189
190 static void sdl_unlock_display( )
191 {
192         SDL_Surface *screen = SDL_GetVideoSurface( );
193         if ( screen != NULL && SDL_MUSTLOCK( screen ) )
194                 SDL_UnlockSurface( screen );
195 }
196
197 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
198 {
199         consumer_sdl this = udata;
200
201         // Get the volume
202         float volume = mlt_properties_get_double( this->properties, "volume" );
203
204         pthread_mutex_lock( &this->audio_mutex );
205
206         // Block until audio received
207         while ( this->running && len > this->audio_avail )
208                 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
209
210         if ( this->audio_avail >= len )
211         {
212                 // Place in the audio buffer
213                 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
214
215                 // Remove len from the audio available
216                 this->audio_avail -= len;
217
218                 // Remove the samples
219                 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
220         }
221         else
222         {
223                 // Just to be safe, wipe the stream first
224                 memset( stream, 0, len );
225
226                 // Copy what we have into the stream
227                 memcpy( stream, this->audio_buffer, this->audio_avail );
228
229                 // Mix the audio 
230                 SDL_MixAudio( stream, stream, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
231
232                 // No audio left
233                 this->audio_avail = 0;
234         }
235
236         // We're definitely playing now
237         this->playing = 1;
238
239         pthread_cond_broadcast( &this->audio_cond );
240         pthread_mutex_unlock( &this->audio_mutex );
241 }
242
243 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
244 {
245         // Get the properties of this consumer
246         mlt_properties properties = this->properties;
247         mlt_audio_format afmt = mlt_audio_pcm;
248
249         // Set the preferred params of the test card signal
250         int channels = 2;
251         int frequency = 48000;
252         static int counter = 0;
253         if ( mlt_properties_get_int( properties, "frequency" ) != 0 )
254                 frequency =  mlt_properties_get_int( properties, "frequency" );
255
256         int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
257         
258         int16_t *pcm;
259         int bytes;
260
261         mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
262         *duration = ( ( samples * 1000 ) / frequency );
263
264         if ( mlt_properties_get_int( properties, "audio_off" ) )
265         {
266                 this->playing = 1;
267                 return init_audio;
268         }
269
270         if ( init_audio == 1 )
271         {
272                 SDL_AudioSpec request;
273                 SDL_AudioSpec got;
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 = 4096;
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, int64_t elapsed, int64_t playtime )
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         // Set skip
336         mlt_properties_set_position( mlt_frame_properties( frame ), "playtime", playtime );
337         mlt_properties_set_double( mlt_frame_properties( frame ), "consumer_scale", ( double )height / mlt_properties_get_double( properties, "height" ) );
338
339         // Push this frame to the back of the queue
340         mlt_deque_push_back( this->queue, frame );
341         frame = NULL;
342
343         if ( this->playing )
344                 frame = mlt_deque_pop_front( this->queue );
345
346         if ( this->playing && frame != NULL && mlt_properties_get_int( mlt_frame_properties( frame ), "rendered" ) == 1 )
347         {
348                 playtime = mlt_properties_get_position( mlt_frame_properties( frame ), "playtime" );
349
350                 // Get the image, width and height
351                 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
352
353                 if ( playtime > elapsed + 25000 )
354                 {
355                         struct timespec tm = { ( playtime - elapsed ) / 1000000, ( ( playtime - elapsed ) % 1000000 ) * 1000 };
356                         nanosleep( &tm, NULL );
357                 }
358
359                 // Handle events
360                 if ( this->sdl_screen != NULL )
361                 {
362                         SDL_Event event;
363         
364                         changed = consumer_get_dimensions( &this->window_width, &this->window_height );
365         
366                         while ( SDL_PollEvent( &event ) )
367                         {
368                                 switch( event.type )
369                                 {
370                                         case SDL_VIDEORESIZE:
371                                                 this->window_width = event.resize.w;
372                                                 this->window_height = event.resize.h;
373                                                 changed = 1;
374                                                 break;
375                                         case SDL_KEYDOWN:
376                                                 {
377                                                         mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
378                                                         char keyboard[ 2 ] = " ";
379                                                         void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
380                                                         if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
381                                                         {
382                                                                 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
383                                                                 callback( producer, keyboard );
384                                                         }
385                                                 }
386                                                 break;
387                                 }
388                         }
389                 }
390         
391                 if ( width != this->width || height != this->height )
392                 {
393                         this->width = width;
394                         this->height = height;
395                         changed = 1;
396                 }
397
398                 if ( this->sdl_screen == NULL || changed )
399                 {
400                         double aspect_ratio = mlt_frame_get_aspect_ratio( frame );
401                         float display_aspect_ratio = (float)width / (float)height;
402                         SDL_Rect rect;
403
404                         if ( mlt_properties_get_double( properties, "aspect_ratio" ) )
405                                 aspect_ratio = mlt_properties_get_double( properties, "aspect_ratio" );
406
407                         if ( aspect_ratio == 1 )
408                         {
409                                 rect.w = this->window_width;
410                                 rect.h = this->window_height;
411                         }
412                         else if ( this->window_width < this->window_height * aspect_ratio )
413                         {
414                                 rect.w = this->window_width;
415                                 rect.h = this->window_width / aspect_ratio;
416                         }
417                         else
418                         {
419                                 rect.w = this->window_height * aspect_ratio;
420                                 rect.h = this->window_height;
421                         }
422
423                         if ( mlt_properties_get_int( properties, "scale_overlay" ) )
424                         {
425                                 if ( ( float )rect.w * display_aspect_ratio < this->window_width )
426                                         rect.w = ( int )( ( float )rect.w * display_aspect_ratio );
427                                 else if ( ( float )rect.h * display_aspect_ratio < this->window_height )
428                                         rect.h = ( int )( ( float )rect.h * display_aspect_ratio );
429                         }
430
431                         rect.x = ( this->window_width - rect.w ) / 2;
432                         rect.y = ( this->window_height - rect.h ) / 2;
433
434                         // Force an overlay recreation
435                         if ( this->sdl_overlay != NULL )
436                                 SDL_FreeYUVOverlay( this->sdl_overlay );
437
438                         // open SDL window with video overlay, if possible
439                         this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
440
441                         if ( this->sdl_screen != NULL )
442                         {
443                                 SDL_SetClipRect( this->sdl_screen, &rect );
444                         
445                                 sdl_lock_display();
446                                 this->sdl_overlay = SDL_CreateYUVOverlay( this->width - (this->width % 4), this->height- (this->height % 2 ), SDL_YUY2_OVERLAY, this->sdl_screen );
447                                 sdl_unlock_display();
448                         }
449                 }
450                         
451                 if ( this->sdl_screen != NULL && this->sdl_overlay != NULL )
452                 {
453                         this->buffer = this->sdl_overlay->pixels[ 0 ];
454                         if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
455                         {
456                                 memcpy( this->buffer, image, width * height * 2 );
457                                 SDL_UnlockYUVOverlay( this->sdl_overlay );
458                                 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
459                         }
460                 }
461         }
462
463         // Close the frame
464         if ( frame != NULL )
465                 mlt_frame_close( frame );
466
467         return 0;
468 }
469
470 /** Threaded wrapper for pipe.
471 */
472
473 static void *consumer_thread( void *arg )
474 {
475         // Identify the arg
476         consumer_sdl this = arg;
477
478         // Get the consumer
479         mlt_consumer consumer = &this->parent;
480
481         // internal intialization
482         int init_audio = 1;
483
484         // Obtain time of thread start
485         struct timeval now;
486         int64_t start = 0;
487         int64_t elapsed = 0;
488         int duration = 0;
489         int64_t playtime = 0;
490
491         if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
492         {
493                 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
494                 return NULL;
495         }
496
497         SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
498         SDL_EnableUNICODE( 1 );
499
500         // Loop until told not to
501         while( this->running )
502         {
503                 // Get a frame from the attached producer
504                 mlt_frame frame = mlt_consumer_rt_frame( consumer );
505
506                 // Ensure that we have a frame
507                 if ( frame != NULL )
508                 {
509                         // Play audio
510                         init_audio = consumer_play_audio( this, frame, init_audio, &duration );
511
512                         if ( this->playing )
513                         {
514                                 // Get the current time
515                                 gettimeofday( &now, NULL );
516
517                                 // Determine elapsed time
518                                 if ( start == 0 )
519                                         start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
520                                 else
521                                         elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec) - start;
522
523                         }
524
525                         consumer_play_video( this, frame, elapsed, playtime );
526
527                         playtime += ( duration * 1000 );
528                 }
529         }
530
531         // internal cleanup
532         if ( init_audio == 0 )
533                 SDL_AudioQuit( );
534         if ( this->sdl_overlay != NULL )
535                 SDL_FreeYUVOverlay( this->sdl_overlay );
536         SDL_Quit( );
537
538         while( mlt_deque_count( this->queue ) )
539                 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
540
541         this->sdl_screen = NULL;
542         this->sdl_overlay = NULL;
543         this->audio_avail = 0;
544
545         return NULL;
546 }
547
548 static int consumer_get_dimensions( int *width, int *height )
549 {
550         int changed = 0;
551
552         // SDL windows manager structure
553         SDL_SysWMinfo wm;
554
555         // Specify the SDL Version
556         SDL_VERSION( &wm.version );
557
558         // Get the wm structure
559         if ( SDL_GetWMInfo( &wm ) == 1 )
560         {
561                 // Check that we have the X11 wm
562                 if ( wm.subsystem == SDL_SYSWM_X11 ) 
563                 {
564                         // Get the SDL window
565                         Window window = wm.info.x11.window;
566
567                         // Get the display session
568                         Display *display = wm.info.x11.display;
569
570                         // Get the window attributes
571                         XWindowAttributes attr;
572                         XGetWindowAttributes( display, window, &attr );
573
574                         // Determine whether window has changed
575                         changed = *width != attr.width || *height != attr.height;
576
577                         // Return width and height
578                         *width = attr.width;
579                         *height = attr.height;
580                 }
581         }
582
583         return changed;
584 }
585
586 /** Callback to allow override of the close method.
587 */
588
589 static void consumer_close( mlt_consumer parent )
590 {
591         // Get the actual object
592         consumer_sdl this = parent->child;
593
594         // Stop the consumer
595         mlt_consumer_stop( parent );
596
597         // Destroy mutexes
598         pthread_mutex_destroy( &this->audio_mutex );
599         pthread_cond_destroy( &this->audio_cond );
600                 
601         // Now clean up the rest
602         mlt_consumer_close( parent );
603
604         // Finally clean up this
605         free( this );
606 }
607