]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl.c
added setenv_mc
[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         float display_aspect;
52         int width;
53         int height;
54         int playing;
55         int sdl_flags;
56         SDL_Surface *sdl_screen;
57         SDL_Overlay *sdl_overlay;
58         uint8_t *buffer;
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", 1 );
108
109                 // Default progressive true
110                 mlt_properties_set_int( this->properties, "progressive", 0 );
111
112                 // Get sample aspect ratio
113                 this->aspect_ratio = mlt_properties_get_double( this->properties, "aspect_ratio" );
114                 
115                 // Default display aspect ratio
116                 this->display_aspect = 4.0 / 3.0;
117                 
118                 // process actual param
119                 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
120                 {
121                         this->width = mlt_properties_get_int( this->properties, "width" );
122                         this->height = mlt_properties_get_int( this->properties, "height" );
123                         
124                         // Default window size
125                         this->window_width = ( float )this->height * this->display_aspect + 0.5;
126                         this->window_height = this->height;
127                 }
128                 else
129                 {
130                         if ( (int)( ( float )this->width / this->height * 1000 ) != 
131                                  (int)( this->display_aspect * 1000 ) ) 
132                         {
133                                 // Override these
134                                 this->display_aspect = ( float )this->width / this->height;
135                                 this->aspect_ratio = 1.0;
136                                 mlt_properties_set_double( this->properties, "aspect_ratio", this->aspect_ratio );
137                         }
138                         
139                         // Set window size
140                         this->window_width = this->width;
141                         this->window_height = this->height;
142                 }
143
144                 // Set the sdl flags
145                 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE;
146
147                 // Allow thread to be started/stopped
148                 parent->start = consumer_start;
149                 parent->stop = consumer_stop;
150                 parent->is_stopped = consumer_is_stopped;
151
152                 // Return the consumer produced
153                 return parent;
154         }
155
156         // malloc or consumer init failed
157         free( this );
158
159         // Indicate failure
160         return NULL;
161 }
162
163 int consumer_start( mlt_consumer parent )
164 {
165         consumer_sdl this = parent->child;
166
167         if ( !this->running )
168         {
169                 pthread_attr_t thread_attributes;
170                 
171                 this->running = 1;
172                 
173                 // Inherit the scheduling priority
174                 pthread_attr_init( &thread_attributes );
175                 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
176         
177                 pthread_create( &this->thread, &thread_attributes, consumer_thread, this );
178         }
179
180         return 0;
181 }
182
183 int consumer_stop( mlt_consumer parent )
184 {
185         // Get the actual object
186         consumer_sdl this = parent->child;
187
188         if ( this->running )
189         {
190                 // Kill the thread and clean up
191                 this->running = 0;
192
193                 pthread_mutex_lock( &this->audio_mutex );
194                 pthread_cond_broadcast( &this->audio_cond );
195                 pthread_mutex_unlock( &this->audio_mutex );
196
197                 pthread_join( this->thread, NULL );
198         }
199
200         return 0;
201 }
202
203 int consumer_is_stopped( mlt_consumer parent )
204 {
205         consumer_sdl this = parent->child;
206         return !this->running;
207 }
208
209 static int sdl_lock_display( )
210 {
211         SDL_Surface *screen = SDL_GetVideoSurface( );
212         return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
213 }
214
215 static void sdl_unlock_display( )
216 {
217         SDL_Surface *screen = SDL_GetVideoSurface( );
218         if ( screen != NULL && SDL_MUSTLOCK( screen ) )
219                 SDL_UnlockSurface( screen );
220 }
221
222 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
223 {
224         consumer_sdl this = udata;
225
226         // Get the volume
227         float volume = mlt_properties_get_double( this->properties, "volume" );
228
229         pthread_mutex_lock( &this->audio_mutex );
230
231         // Block until audio received
232         while ( this->running && len > this->audio_avail )
233                 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
234
235         if ( this->audio_avail >= len )
236         {
237                 // Place in the audio buffer
238                 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
239
240                 // Remove len from the audio available
241                 this->audio_avail -= len;
242
243                 // Remove the samples
244                 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
245         }
246         else
247         {
248                 // Just to be safe, wipe the stream first
249                 memset( stream, 0, len );
250
251                 // Copy what we have into the stream
252                 memcpy( stream, this->audio_buffer, this->audio_avail );
253
254                 // Mix the audio 
255                 SDL_MixAudio( stream, stream, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
256
257                 // No audio left
258                 this->audio_avail = 0;
259         }
260
261         // We're definitely playing now
262         this->playing = 1;
263
264         pthread_cond_broadcast( &this->audio_cond );
265         pthread_mutex_unlock( &this->audio_mutex );
266 }
267
268 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
269 {
270         // Get the properties of this consumer
271         mlt_properties properties = this->properties;
272         mlt_audio_format afmt = mlt_audio_pcm;
273
274         // Set the preferred params of the test card signal
275         int channels = mlt_properties_get_int( properties, "channels" );
276         int frequency = mlt_properties_get_int( properties, "frequency" );
277         static int counter = 0;
278
279         int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
280         
281         int16_t *pcm;
282         int bytes;
283
284         mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
285         *duration = ( ( samples * 1000 ) / frequency );
286
287         if ( mlt_properties_get_int( properties, "audio_off" ) )
288         {
289                 this->playing = 1;
290                 init_audio = 1;
291                 return init_audio;
292         }
293
294         if ( init_audio == 1 )
295         {
296                 SDL_AudioSpec request;
297                 SDL_AudioSpec got;
298
299                 // specify audio format
300                 memset( &request, 0, sizeof( SDL_AudioSpec ) );
301                 this->playing = 0;
302                 request.freq = frequency;
303                 request.format = AUDIO_S16;
304                 request.channels = channels;
305                 request.samples = 1024;
306                 request.callback = sdl_fill_audio;
307                 request.userdata = (void *)this;
308                 if ( SDL_OpenAudio( &request, &got ) != 0 )
309                 {
310                         fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
311                         init_audio = 2;
312                 }
313                 else if ( got.size != 0 )
314                 {
315                         SDL_PauseAudio( 0 );
316                         init_audio = 0;
317                 }
318         }
319
320         if ( init_audio == 0 )
321         {
322                 mlt_properties properties = mlt_frame_properties( frame );
323                 bytes = ( samples * channels * 2 );
324                 pthread_mutex_lock( &this->audio_mutex );
325                 while ( bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
326                         pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
327                 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
328                         memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
329                 else
330                         memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
331                 this->audio_avail += bytes;
332                 pthread_cond_broadcast( &this->audio_cond );
333                 pthread_mutex_unlock( &this->audio_mutex );
334         }
335         else
336         {
337                 this->playing = 1;
338         }
339
340         return init_audio;
341 }
342
343 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
344 {
345         // Get the properties of this consumer
346         mlt_properties properties = this->properties;
347
348         mlt_image_format vfmt = mlt_image_yuv422;
349         int width = this->width, height = this->height;
350         uint8_t *image;
351         int changed = 0;
352
353         if ( mlt_properties_get_int( properties, "video_off" ) == 0 )
354         {
355                 // Get the image, width and height
356                 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
357                 
358                 // Handle events
359                 if ( this->sdl_screen != NULL )
360                 {
361                         SDL_Event event;
362         
363                         changed = consumer_get_dimensions( &this->window_width, &this->window_height );
364         
365                         while ( SDL_PollEvent( &event ) )
366                         {
367                                 switch( event.type )
368                                 {
369                                         case SDL_VIDEORESIZE:
370                                                 this->window_width = event.resize.w;
371                                                 this->window_height = event.resize.h;
372                                                 changed = 1;
373                                                 break;
374                                         case SDL_QUIT:
375                                                 this->running = 0;
376                                                 break;
377                                         case SDL_KEYDOWN:
378                                                 {
379                                                         mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
380                                                         char keyboard[ 2 ] = " ";
381                                                         void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
382                                                         if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
383                                                         {
384                                                                 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
385                                                                 callback( producer, keyboard );
386                                                         }
387                                                 }
388                                                 break;
389                                 }
390                         }
391                 }
392         
393                 if ( width != this->width || height != this->height )
394                 {
395                         this->width = width;
396                         this->height = height;
397                         changed = 1;
398                 }
399
400                         SDL_Rect rect;
401                         
402                         // Determine frame's display aspect ratio
403                         float frame_aspect = mlt_frame_get_aspect_ratio( frame ) * this->width / this->height;
404                         
405                         // Determine window's new display aspect ratio
406                         float this_aspect = ( float )this->window_width / this->window_height;
407
408                         // If using hardware scaler
409                         if ( mlt_properties_get( properties, "rescale" ) != NULL &&
410                                 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
411                         {
412                                 // Special case optimisation to negate odd effect of sample aspect ratio
413                                 // not corresponding exactly with image resolution.
414                                 if ( ( (int)( this_aspect * 1000 ) == (int)( this->display_aspect * 1000 ) ) && 
415                                          ( (int)( mlt_frame_get_aspect_ratio( frame ) * 1000 ) == (int)( this->aspect_ratio * 1000 ) ) )
416                                 {
417                                         rect.w = this->window_width;
418                                         rect.h = this->window_height;
419                                 }
420                                 else
421                                 {
422                                         // Use hardware scaler to normalise display aspect ratio
423                                         rect.w = frame_aspect / this_aspect * this->window_width + 0.5;
424                                         rect.h = this->window_height;
425                                         if ( rect.w > this->window_width )
426                                         {
427                                                 rect.w = this->window_width;
428                                                 rect.h = this_aspect / frame_aspect * this->window_height + 0.5;
429                                         }
430                                 }
431                         }
432                         // Special case optimisation to negate odd effect of sample aspect ratio
433                         // not corresponding exactly with image resolution.
434                         else if ( (int)( this_aspect * 1000 ) == (int)( this->display_aspect * 1000 ) ) 
435                         {
436                                 rect.w = this->window_width;
437                                 rect.h = this->window_height;
438                         }
439                         // Use hardware scaler to normalise sample aspect ratio
440                         else if ( this->window_height * frame_aspect > this->window_width )
441                         {
442                                 rect.w = this->window_width;
443                                 rect.h = this->window_width / frame_aspect + 0.5;
444                         }
445                         else
446                         {
447                                 rect.w = this->window_height * frame_aspect + 0.5;
448                                 rect.h = this->window_height;
449                         }
450                         
451                         rect.x = ( this->window_width - rect.w ) / 2;
452                         rect.y = ( this->window_height - rect.h ) / 2;
453                         
454                 if ( this->sdl_screen == NULL || changed )
455                 {
456                         // Force an overlay recreation
457                         if ( this->sdl_overlay != NULL )
458                                 SDL_FreeYUVOverlay( this->sdl_overlay );
459
460                         // open SDL window with video overlay, if possible
461                         this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
462
463                         if ( this->sdl_screen != NULL )
464                         {
465                                 SDL_SetClipRect( this->sdl_screen, &rect );
466                         
467                                 sdl_lock_display();
468                                 this->sdl_overlay = SDL_CreateYUVOverlay( this->width - (this->width % 4), this->height - (this->height % 2 ), SDL_YUY2_OVERLAY, this->sdl_screen );
469                                 sdl_unlock_display();
470                         }
471                 }
472                 else
473                 {
474                         SDL_SetClipRect( this->sdl_screen, &rect );
475                 }
476
477                 if ( this->sdl_screen != NULL && this->sdl_overlay != NULL )
478                 {
479                         this->buffer = this->sdl_overlay->pixels[ 0 ];
480                         if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
481                         {
482                                 memcpy( this->buffer, image, width * height * 2 );
483                                 SDL_UnlockYUVOverlay( this->sdl_overlay );
484                                 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
485                         }
486                 }
487         }
488
489         // Close the frame
490         mlt_frame_close( frame );
491
492         return 0;
493 }
494
495 /** Threaded wrapper for pipe.
496 */
497
498 static void *consumer_thread( void *arg )
499 {
500         // Identify the arg
501         consumer_sdl this = arg;
502
503         // Get the consumer
504         mlt_consumer consumer = &this->parent;
505
506         // internal intialization
507         int init_audio = 1;
508
509         // Obtain time of thread start
510         struct timeval now;
511         int64_t start = 0;
512         int64_t elapsed = 0;
513         int duration = 0;
514         int64_t playtime = 0;
515         struct timespec tm;
516         mlt_frame next = NULL;
517         mlt_frame frame = NULL;
518
519         if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
520         {
521                 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
522                 return NULL;
523         }
524
525         SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
526         SDL_EnableUNICODE( 1 );
527
528         // Loop until told not to
529         while( this->running )
530         {
531                 // Get a frame from the attached producer
532                 frame = mlt_consumer_rt_frame( consumer );
533
534                 // Ensure that we have a frame
535                 if ( frame != NULL )
536                 {
537                         // Play audio
538                         init_audio = consumer_play_audio( this, frame, init_audio, &duration );
539
540                         if ( this->playing )
541                         {
542                                 // Get the current time
543                                 gettimeofday( &now, NULL );
544
545                                 // Determine elapsed time
546                                 if ( start == 0 )
547                                         start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
548                                 else
549                                         elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec) - start;
550                         }
551
552                         // Set playtime for this frame
553                         mlt_properties_set_position( mlt_frame_properties( frame ), "playtime", playtime );
554
555                         // Push this frame to the back of the queue
556                         mlt_deque_push_back( this->queue, frame );
557
558                         // Calculate the next playtime
559                         playtime += ( duration * 1000 );
560                 }
561
562                 if ( this->playing )
563                 {
564                         // Pop the next frame
565                         next = mlt_deque_pop_front( this->queue );
566
567                         // See if we have to delay the display of the current frame
568                         if ( next != NULL && mlt_properties_get_int( mlt_frame_properties( next ), "rendered" ) == 1 )
569                         {
570                                 mlt_position scheduled = mlt_properties_get_position( mlt_frame_properties( next ), "playtime" ) + 5000;
571                                 if ( scheduled > elapsed && mlt_deque_count( this->queue ) > 25 )
572                                 {
573                                         tm.tv_sec = ( scheduled - elapsed ) / 1000000;
574                                         tm.tv_nsec = ( ( scheduled - elapsed ) % 1000000 ) * 1000;
575                                         nanosleep( &tm, NULL );
576
577                                         // Show current frame
578                                         consumer_play_video( this, next );
579                                 }
580                                 else if ( scheduled > elapsed )
581                                 {
582                                         // More time to kill
583                                         mlt_deque_push_front( this->queue, next );
584                                 }
585                                 else
586                                 {
587                                         // Show current frame
588                                         consumer_play_video( this, next );
589                                 }
590                         }
591                         else
592                         {
593                                 // This is an unrendered frame - just close it
594                                 mlt_frame_close( next );
595                         }
596                 }
597         }
598
599         // internal cleanup
600         if ( init_audio == 0 )
601                 SDL_AudioQuit( );
602         if ( this->sdl_overlay != NULL )
603                 SDL_FreeYUVOverlay( this->sdl_overlay );
604         SDL_Quit( );
605
606         while( mlt_deque_count( this->queue ) )
607                 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
608
609         this->sdl_screen = NULL;
610         this->sdl_overlay = NULL;
611         this->audio_avail = 0;
612
613         return NULL;
614 }
615
616 static int consumer_get_dimensions( int *width, int *height )
617 {
618         int changed = 0;
619
620         // SDL windows manager structure
621         SDL_SysWMinfo wm;
622
623         // Specify the SDL Version
624         SDL_VERSION( &wm.version );
625
626         // Get the wm structure
627         if ( SDL_GetWMInfo( &wm ) == 1 )
628         {
629                 // Check that we have the X11 wm
630                 if ( wm.subsystem == SDL_SYSWM_X11 ) 
631                 {
632                         // Get the SDL window
633                         Window window = wm.info.x11.window;
634
635                         // Get the display session
636                         Display *display = wm.info.x11.display;
637
638                         // Get the window attributes
639                         XWindowAttributes attr;
640                         XGetWindowAttributes( display, window, &attr );
641
642                         // Determine whether window has changed
643                         changed = *width != attr.width || *height != attr.height;
644
645                         // Return width and height
646                         *width = attr.width;
647                         *height = attr.height;
648                 }
649         }
650
651         return changed;
652 }
653
654 /** Callback to allow override of the close method.
655 */
656
657 static void consumer_close( mlt_consumer parent )
658 {
659         // Get the actual object
660         consumer_sdl this = parent->child;
661
662         // Stop the consumer
663         mlt_consumer_stop( parent );
664
665         // Destroy mutexes
666         pthread_mutex_destroy( &this->audio_mutex );
667         pthread_cond_destroy( &this->audio_cond );
668                 
669         // Now clean up the rest
670         mlt_consumer_close( parent );
671
672         // Finally clean up this
673         free( this );
674 }