2 * consumer_sdl.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
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.
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.
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.
21 #include "consumer_sdl.h"
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_deque.h>
29 #include <SDL/SDL_syswm.h>
32 /** This classes definition.
35 typedef struct consumer_sdl_s
*consumer_sdl
;
39 struct mlt_consumer_s parent
;
40 mlt_properties properties
;
44 uint8_t audio_buffer
[ 4096 * 19 ];
46 pthread_mutex_t audio_mutex
;
47 pthread_cond_t audio_cond
;
56 SDL_Surface
*sdl_screen
;
57 SDL_Overlay
*sdl_overlay
;
62 /** Forward references to static functions.
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
);
72 /** This is what will be called by the factory - anything can be passed in
73 via the argument, but keep it simple.
76 mlt_consumer
consumer_sdl_init( char *arg
)
78 // Create the consumer object
79 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
81 // If no malloc'd and consumer init ok
82 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
85 this->queue
= mlt_deque_init( );
87 // Get the parent consumer object
88 mlt_consumer parent
= &this->parent
;
90 // We have stuff to clean up, so override the close method
91 parent
->close
= consumer_close
;
93 // get a handle on properties
94 mlt_service service
= mlt_consumer_service( parent
);
95 this->properties
= mlt_service_properties( service
);
97 // Set the default volume
98 mlt_properties_set_double( this->properties
, "volume", 1.0 );
100 // This is the initialisation of the consumer
101 pthread_mutex_init( &this->audio_mutex
, NULL
);
102 pthread_cond_init( &this->audio_cond
, NULL
);
104 // Default scaler (for now we'll use nearest)
105 mlt_properties_set( this->properties
, "rescale", "nearest" );
107 // Default buffer for low latency
108 mlt_properties_set_int( this->properties
, "buffer", 1 );
110 // Default progressive true
111 mlt_properties_set_int( this->properties
, "progressive", 0 );
113 // Default audio buffer
114 mlt_properties_set_int( this->properties
, "audio_buffer", 1024 );
116 // Get sample aspect ratio
117 this->aspect_ratio
= mlt_properties_get_double( this->properties
, "aspect_ratio" );
119 // Default display aspect ratio
120 this->display_aspect
= 4.0 / 3.0;
122 // process actual param
123 if ( arg
== NULL
|| sscanf( arg
, "%dx%d", &this->width
, &this->height
) != 2 )
125 this->width
= mlt_properties_get_int( this->properties
, "width" );
126 this->height
= mlt_properties_get_int( this->properties
, "height" );
128 // Default window size
129 this->window_width
= ( float )this->height
* this->display_aspect
+ 0.5;
130 this->window_height
= this->height
;
134 if ( (int)( ( float )this->width
/ this->height
* 1000 ) !=
135 (int)( this->display_aspect
* 1000 ) )
138 this->display_aspect
= ( float )this->width
/ this->height
;
139 this->aspect_ratio
= 1.0;
140 mlt_properties_set_double( this->properties
, "aspect_ratio", this->aspect_ratio
);
144 this->window_width
= this->width
;
145 this->window_height
= this->height
;
149 this->sdl_flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
| SDL_RESIZABLE
| SDL_DOUBLEBUF
;
151 // Allow thread to be started/stopped
152 parent
->start
= consumer_start
;
153 parent
->stop
= consumer_stop
;
154 parent
->is_stopped
= consumer_is_stopped
;
156 // Return the consumer produced
160 // malloc or consumer init failed
167 int consumer_start( mlt_consumer parent
)
169 consumer_sdl
this = parent
->child
;
171 if ( !this->running
)
173 pthread_attr_t thread_attributes
;
177 // Allow the user to force resizing to window size
178 if ( mlt_properties_get_int( this->properties
, "resize" ) )
180 mlt_properties_set_int( this->properties
, "width", this->width
);
181 mlt_properties_set_int( this->properties
, "height", this->height
);
184 // Inherit the scheduling priority
185 pthread_attr_init( &thread_attributes
);
186 pthread_attr_setinheritsched( &thread_attributes
, PTHREAD_INHERIT_SCHED
);
188 pthread_create( &this->thread
, &thread_attributes
, consumer_thread
, this );
194 int consumer_stop( mlt_consumer parent
)
196 // Get the actual object
197 consumer_sdl
this = parent
->child
;
201 // Kill the thread and clean up
204 pthread_mutex_lock( &this->audio_mutex
);
205 pthread_cond_broadcast( &this->audio_cond
);
206 pthread_mutex_unlock( &this->audio_mutex
);
208 pthread_join( this->thread
, NULL
);
214 int consumer_is_stopped( mlt_consumer parent
)
216 consumer_sdl
this = parent
->child
;
217 return !this->running
;
220 static int sdl_lock_display( )
222 SDL_Surface
*screen
= SDL_GetVideoSurface( );
223 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
226 static void sdl_unlock_display( )
228 SDL_Surface
*screen
= SDL_GetVideoSurface( );
229 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
230 SDL_UnlockSurface( screen
);
233 static void sdl_fill_audio( void *udata
, uint8_t *stream
, int len
)
235 consumer_sdl
this = udata
;
238 float volume
= mlt_properties_get_double( this->properties
, "volume" );
240 pthread_mutex_lock( &this->audio_mutex
);
242 // Block until audio received
243 while ( this->running
&& len
> this->audio_avail
)
244 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
246 if ( this->audio_avail
>= len
)
248 // Place in the audio buffer
249 memcpy( stream
, this->audio_buffer
, len
);
251 // Remove len from the audio available
252 this->audio_avail
-= len
;
254 // Remove the samples
255 memmove( this->audio_buffer
, this->audio_buffer
+ len
, this->audio_avail
);
259 // Just to be safe, wipe the stream first
260 memset( stream
, 0, len
);
262 // Copy what we have into the stream
263 memcpy( stream
, this->audio_buffer
, this->audio_avail
);
266 SDL_MixAudio( stream
, stream
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
269 this->audio_avail
= 0;
272 // We're definitely playing now
275 pthread_cond_broadcast( &this->audio_cond
);
276 pthread_mutex_unlock( &this->audio_mutex
);
279 static int consumer_play_audio( consumer_sdl
this, mlt_frame frame
, int init_audio
, int *duration
)
281 // Get the properties of this consumer
282 mlt_properties properties
= this->properties
;
283 mlt_audio_format afmt
= mlt_audio_pcm
;
285 // Set the preferred params of the test card signal
286 int channels
= mlt_properties_get_int( properties
, "channels" );
287 int frequency
= mlt_properties_get_int( properties
, "frequency" );
288 static int counter
= 0;
290 int samples
= mlt_sample_calculator( mlt_properties_get_double( this->properties
, "fps" ), frequency
, counter
++ );
295 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
296 *duration
= ( ( samples
* 1000 ) / frequency
);
298 if ( mlt_properties_get_int( properties
, "audio_off" ) )
305 if ( init_audio
== 1 )
307 SDL_AudioSpec request
;
310 int audio_buffer
= mlt_properties_get_int( properties
, "audio_buffer" );
312 // specify audio format
313 memset( &request
, 0, sizeof( SDL_AudioSpec
) );
315 request
.freq
= frequency
;
316 request
.format
= AUDIO_S16
;
317 request
.channels
= channels
;
318 request
.samples
= audio_buffer
;
319 request
.callback
= sdl_fill_audio
;
320 request
.userdata
= (void *)this;
321 if ( SDL_OpenAudio( &request
, &got
) != 0 )
323 fprintf( stderr
, "SDL failed to open audio: %s\n", SDL_GetError() );
326 else if ( got
.size
!= 0 )
333 if ( init_audio
== 0 )
335 mlt_properties properties
= mlt_frame_properties( frame
);
336 bytes
= ( samples
* channels
* 2 );
337 pthread_mutex_lock( &this->audio_mutex
);
338 while ( bytes
> ( sizeof( this->audio_buffer
) - this->audio_avail
) )
339 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
340 if ( mlt_properties_get_double( properties
, "_speed" ) == 1 )
341 memcpy( &this->audio_buffer
[ this->audio_avail
], pcm
, bytes
);
343 memset( &this->audio_buffer
[ this->audio_avail
], 0, bytes
);
344 this->audio_avail
+= bytes
;
345 pthread_cond_broadcast( &this->audio_cond
);
346 pthread_mutex_unlock( &this->audio_mutex
);
356 static int consumer_play_video( consumer_sdl
this, mlt_frame frame
)
358 // Get the properties of this consumer
359 mlt_properties properties
= this->properties
;
361 mlt_image_format vfmt
= mlt_image_yuv422
;
362 int width
= this->width
, height
= this->height
;
366 if ( mlt_properties_get_int( properties
, "video_off" ) == 0 )
368 // Get the image, width and height
369 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
372 if ( this->sdl_screen
!= NULL
)
376 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
378 while ( SDL_PollEvent( &event
) )
382 case SDL_VIDEORESIZE
:
383 this->window_width
= event
.resize
.w
;
384 this->window_height
= event
.resize
.h
;
392 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
393 char keyboard
[ 2 ] = " ";
394 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
395 if ( callback
!= NULL
&& producer
!= NULL
&& event
.key
.keysym
.unicode
< 0x80 && event
.key
.keysym
.unicode
> 0 )
397 keyboard
[ 0 ] = ( char )event
.key
.keysym
.unicode
;
398 callback( producer
, keyboard
);
406 if ( width
!= this->width
|| height
!= this->height
)
409 this->height
= height
;
413 if ( this->sdl_screen
== NULL
|| changed
)
415 // Determine frame's display aspect ratio
416 float frame_aspect
= mlt_frame_get_aspect_ratio( frame
) * this->width
/ this->height
;
418 // Determine window's new display aspect ratio
419 float this_aspect
= ( float )this->window_width
/ this->window_height
;
421 // If using hardware scaler
422 if ( mlt_properties_get( properties
, "rescale" ) != NULL
&&
423 !strcmp( mlt_properties_get( properties
, "rescale" ), "none" ) )
425 // Special case optimisation to negate odd effect of sample aspect ratio
426 // not corresponding exactly with image resolution.
427 if ( ( (int)( this_aspect
* 1000 ) == (int)( this->display_aspect
* 1000 ) ) &&
428 ( (int)( mlt_frame_get_aspect_ratio( frame
) * 1000 ) == (int)( this->aspect_ratio
* 1000 ) ) )
430 this->rect
.w
= this->window_width
;
431 this->rect
.h
= this->window_height
;
435 // Use hardware scaler to normalise display aspect ratio
436 this->rect
.w
= frame_aspect
/ this_aspect
* this->window_width
+ 0.5;
437 this->rect
.h
= this->window_height
;
438 if ( this->rect
.w
> this->window_width
)
440 this->rect
.w
= this->window_width
;
441 this->rect
.h
= this_aspect
/ frame_aspect
* this->window_height
+ 0.5;
445 // Special case optimisation to negate odd effect of sample aspect ratio
446 // not corresponding exactly with image resolution.
447 else if ( (int)( this_aspect
* 1000 ) == (int)( this->display_aspect
* 1000 ) )
449 this->rect
.w
= this->window_width
;
450 this->rect
.h
= this->window_height
;
452 // Use hardware scaler to normalise sample aspect ratio
453 else if ( this->window_height
* frame_aspect
> this->window_width
)
455 this->rect
.w
= this->window_width
;
456 this->rect
.h
= this->window_width
/ frame_aspect
+ 0.5;
460 this->rect
.w
= this->window_height
* frame_aspect
+ 0.5;
461 this->rect
.h
= this->window_height
;
464 this->rect
.x
= ( this->window_width
- this->rect
.w
) / 2;
465 this->rect
.y
= ( this->window_height
- this->rect
.h
) / 2;
467 // Force an overlay recreation
468 if ( this->sdl_overlay
!= NULL
)
469 SDL_FreeYUVOverlay( this->sdl_overlay
);
471 // open SDL window with video overlay, if possible
472 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
474 if ( this->sdl_screen
!= NULL
)
476 SDL_SetClipRect( this->sdl_screen
, &this->rect
);
478 this->sdl_overlay
= SDL_CreateYUVOverlay( this->width
, this->height
, SDL_YUY2_OVERLAY
, this->sdl_screen
);
479 sdl_unlock_display();
483 if ( mlt_properties_get_int( properties
, "changed" ) )
486 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
487 SDL_SetClipRect( this->sdl_screen
, &this->rect
);
488 SDL_Flip( this->sdl_screen
);
489 sdl_unlock_display();
490 mlt_properties_set_int( properties
, "changed", 0 );
493 if ( this->sdl_screen
!= NULL
&& this->sdl_overlay
!= NULL
)
495 this->buffer
= this->sdl_overlay
->pixels
[ 0 ];
496 if ( SDL_LockYUVOverlay( this->sdl_overlay
) >= 0 )
499 memcpy( this->buffer
, image
, width
* height
* 2 );
500 SDL_UnlockYUVOverlay( this->sdl_overlay
);
501 SDL_DisplayYUVOverlay( this->sdl_overlay
, &this->sdl_screen
->clip_rect
);
510 /** Threaded wrapper for pipe.
513 static void *consumer_thread( void *arg
)
516 consumer_sdl
this = arg
;
519 mlt_consumer consumer
= &this->parent
;
521 // internal intialization
524 // Obtain time of thread start
529 int64_t playtime
= 0;
531 mlt_frame next
= NULL
;
532 mlt_frame frame
= NULL
;
533 mlt_properties properties
= NULL
;
535 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) < 0 )
537 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
541 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
542 SDL_EnableUNICODE( 1 );
544 // Loop until told not to
545 while( this->running
)
547 // Get a frame from the attached producer
548 frame
= mlt_consumer_rt_frame( consumer
);
550 // Ensure that we have a frame
553 // Get the frame properties
554 properties
= mlt_frame_properties( frame
);
557 init_audio
= consumer_play_audio( this, frame
, init_audio
, &duration
);
559 // Determine the start time now
560 if ( this->playing
&& start
== 0 )
562 // Get the current time
563 gettimeofday( &now
, NULL
);
565 // Determine start time
566 start
= ( int64_t )now
.tv_sec
* 1000000 + now
.tv_usec
;
569 // Set playtime for this frame
570 mlt_properties_set_position( properties
, "playtime", playtime
);
572 // Push this frame to the back of the queue
573 mlt_deque_push_back( this->queue
, frame
);
575 // Calculate the next playtime
576 playtime
+= ( duration
* 1000 );
579 // Pop the next frame
580 next
= mlt_deque_pop_front( this->queue
);
582 while ( next
!= NULL
&& this->playing
)
584 // Get the properties
585 properties
= mlt_frame_properties( next
);
587 // Get the current time
588 gettimeofday( &now
, NULL
);
590 // Get the elapsed time
591 elapsed
= ( ( int64_t )now
.tv_sec
* 1000000 + now
.tv_usec
) - start
;
593 // See if we have to delay the display of the current frame
594 if ( mlt_properties_get_int( properties
, "rendered" ) == 1 )
596 // Obtain the scheduled playout time
597 mlt_position scheduled
= mlt_properties_get_position( properties
, "playtime" );
599 // Determine the difference between the elapsed time and the scheduled playout time
600 mlt_position difference
= scheduled
- elapsed
;
602 // If the frame is quite some way in the future, go get another
603 if ( difference
>= 30000 && mlt_deque_count( this->queue
) < 10 )
606 // Smooth playback a bit
607 if ( difference
> 20000 && mlt_properties_get_double( properties
, "_speed" ) == 1.0 )
609 tm
.tv_sec
= difference
/ 1000000;
610 tm
.tv_nsec
= ( difference
% 1000000 ) * 1000;
611 nanosleep( &tm
, NULL
);
614 // Show current frame if not too old
615 if ( difference
> -10000 || mlt_properties_get_double( properties
, "_speed" ) != 1.0 )
616 consumer_play_video( this, next
);
618 start
= start
- difference
;
621 // This is an unrendered frame - just close it
622 mlt_frame_close( next
);
624 // Pop the next frame
625 next
= mlt_deque_pop_front( this->queue
);
629 mlt_deque_push_front( this->queue
, next
);
633 if ( this->sdl_overlay
!= NULL
)
634 SDL_FreeYUVOverlay( this->sdl_overlay
);
637 while( mlt_deque_count( this->queue
) )
638 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
640 this->sdl_screen
= NULL
;
641 this->sdl_overlay
= NULL
;
642 this->audio_avail
= 0;
647 static int consumer_get_dimensions( int *width
, int *height
)
651 // SDL windows manager structure
654 // Specify the SDL Version
655 SDL_VERSION( &wm
.version
);
657 // Get the wm structure
658 if ( SDL_GetWMInfo( &wm
) == 1 )
660 // Check that we have the X11 wm
661 if ( wm
.subsystem
== SDL_SYSWM_X11
)
663 // Get the SDL window
664 Window window
= wm
.info
.x11
.window
;
666 // Get the display session
667 Display
*display
= wm
.info
.x11
.display
;
669 // Get the window attributes
670 XWindowAttributes attr
;
671 XGetWindowAttributes( display
, window
, &attr
);
673 // Determine whether window has changed
674 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
676 // Return width and height
678 *height
= attr
.height
;
685 /** Callback to allow override of the close method.
688 static void consumer_close( mlt_consumer parent
)
690 // Get the actual object
691 consumer_sdl
this = parent
->child
;
694 mlt_consumer_stop( parent
);
697 mlt_deque_close( this->queue
);
700 pthread_mutex_destroy( &this->audio_mutex
);
701 pthread_cond_destroy( &this->audio_cond
);
703 // Now clean up the rest
704 mlt_consumer_close( parent
);
706 // Finally clean up this