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
;
45 uint8_t audio_buffer
[ 4096 * 10 ];
47 pthread_mutex_t audio_mutex
;
48 pthread_cond_t audio_cond
;
49 pthread_mutex_t video_mutex
;
50 pthread_cond_t video_cond
;
55 double last_frame_aspect
;
60 SDL_Surface
*sdl_screen
;
61 SDL_Overlay
*sdl_overlay
;
66 /** Forward references to static functions.
69 static int consumer_start( mlt_consumer parent
);
70 static int consumer_stop( mlt_consumer parent
);
71 static int consumer_is_stopped( mlt_consumer parent
);
72 static void consumer_close( mlt_consumer parent
);
73 static void *consumer_thread( void * );
74 static int consumer_get_dimensions( int *width
, int *height
);
75 static void consumer_sdl_event( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
);
77 /** This is what will be called by the factory - anything can be passed in
78 via the argument, but keep it simple.
81 mlt_consumer
consumer_sdl_init( char *arg
)
83 // Create the consumer object
84 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
86 // If no malloc'd and consumer init ok
87 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
90 this->queue
= mlt_deque_init( );
92 // Get the parent consumer object
93 mlt_consumer parent
= &this->parent
;
95 // We have stuff to clean up, so override the close method
96 parent
->close
= consumer_close
;
98 // get a handle on properties
99 mlt_service service
= mlt_consumer_service( parent
);
100 this->properties
= mlt_service_properties( service
);
102 // Set the default volume
103 mlt_properties_set_double( this->properties
, "volume", 1.0 );
105 // This is the initialisation of the consumer
106 pthread_mutex_init( &this->audio_mutex
, NULL
);
107 pthread_cond_init( &this->audio_cond
, NULL
);
108 pthread_mutex_init( &this->video_mutex
, NULL
);
109 pthread_cond_init( &this->video_cond
, NULL
);
111 // Default scaler (for now we'll use nearest)
112 mlt_properties_set( this->properties
, "rescale", "nearest" );
114 // Default buffer for low latency
115 mlt_properties_set_int( this->properties
, "buffer", 1 );
117 // Default progressive true
118 mlt_properties_set_int( this->properties
, "progressive", 0 );
120 // Default audio buffer
121 mlt_properties_set_int( this->properties
, "audio_buffer", 1024 );
123 // Get sample aspect ratio
124 this->aspect_ratio
= mlt_properties_get_double( this->properties
, "aspect_ratio" );
126 // Ensure we don't join on a non-running object
129 // Default display aspect ratio
130 this->display_aspect
= 4.0 / 3.0;
132 // process actual param
133 if ( arg
== NULL
|| sscanf( arg
, "%dx%d", &this->width
, &this->height
) != 2 )
135 this->width
= mlt_properties_get_int( this->properties
, "width" );
136 this->height
= mlt_properties_get_int( this->properties
, "height" );
138 // Default window size
139 this->window_width
= ( float )this->height
* this->display_aspect
;
140 this->window_height
= this->height
;
144 if ( (int)( ( float )this->width
/ this->height
* 1000 ) !=
145 (int)( this->display_aspect
* 1000 ) )
148 this->display_aspect
= ( float )this->width
/ this->height
;
149 this->aspect_ratio
= 1.0;
150 mlt_properties_set_double( this->properties
, "aspect_ratio", this->aspect_ratio
);
154 this->window_width
= this->width
;
155 this->window_height
= this->height
;
159 this->sdl_flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
| SDL_RESIZABLE
| SDL_DOUBLEBUF
;
161 // Allow thread to be started/stopped
162 parent
->start
= consumer_start
;
163 parent
->stop
= consumer_stop
;
164 parent
->is_stopped
= consumer_is_stopped
;
166 // Register specific events
167 mlt_events_register( this->properties
, "consumer-sdl-event", ( mlt_transmitter
)consumer_sdl_event
);
169 // Return the consumer produced
173 // malloc or consumer init failed
180 static void consumer_sdl_event( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
182 if ( listener
!= NULL
)
183 listener( owner
, this, ( SDL_Event
* )args
[ 0 ] );
186 int consumer_start( mlt_consumer parent
)
188 consumer_sdl
this = parent
->child
;
190 if ( !this->running
)
192 pthread_attr_t thread_attributes
;
194 consumer_stop( parent
);
199 // Allow the user to force resizing to window size
200 if ( mlt_properties_get_int( this->properties
, "resize" ) )
202 mlt_properties_set_int( this->properties
, "width", this->width
);
203 mlt_properties_set_int( this->properties
, "height", this->height
);
206 // Inherit the scheduling priority
207 pthread_attr_init( &thread_attributes
);
208 pthread_attr_setinheritsched( &thread_attributes
, PTHREAD_INHERIT_SCHED
);
210 pthread_create( &this->thread
, &thread_attributes
, consumer_thread
, this );
216 int consumer_stop( mlt_consumer parent
)
218 // Get the actual object
219 consumer_sdl
this = parent
->child
;
221 if ( this->joined
== 0 )
223 // Kill the thread and clean up
226 pthread_mutex_lock( &this->audio_mutex
);
227 pthread_cond_broadcast( &this->audio_cond
);
228 pthread_mutex_unlock( &this->audio_mutex
);
230 pthread_join( this->thread
, NULL
);
237 int consumer_is_stopped( mlt_consumer parent
)
239 consumer_sdl
this = parent
->child
;
240 return !this->running
;
243 static int sdl_lock_display( )
245 SDL_Surface
*screen
= SDL_GetVideoSurface( );
246 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
249 static void sdl_unlock_display( )
251 SDL_Surface
*screen
= SDL_GetVideoSurface( );
252 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
253 SDL_UnlockSurface( screen
);
256 static void sdl_fill_audio( void *udata
, uint8_t *stream
, int len
)
258 consumer_sdl
this = udata
;
261 float volume
= mlt_properties_get_double( this->properties
, "volume" );
263 pthread_mutex_lock( &this->audio_mutex
);
265 // Block until audio received
266 while ( this->running
&& len
> this->audio_avail
)
267 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
269 if ( this->audio_avail
>= len
)
271 // Place in the audio buffer
272 memcpy( stream
, this->audio_buffer
, len
);
274 // Remove len from the audio available
275 this->audio_avail
-= len
;
277 // Remove the samples
278 memmove( this->audio_buffer
, this->audio_buffer
+ len
, this->audio_avail
);
282 // Just to be safe, wipe the stream first
283 memset( stream
, 0, len
);
285 // Copy what we have into the stream
286 memcpy( stream
, this->audio_buffer
, this->audio_avail
);
289 SDL_MixAudio( stream
, stream
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
292 this->audio_avail
= 0;
295 // We're definitely playing now
298 pthread_cond_broadcast( &this->audio_cond
);
299 pthread_mutex_unlock( &this->audio_mutex
);
302 static int consumer_play_audio( consumer_sdl
this, mlt_frame frame
, int init_audio
, int *duration
)
304 // Get the properties of this consumer
305 mlt_properties properties
= this->properties
;
306 mlt_audio_format afmt
= mlt_audio_pcm
;
308 // Set the preferred params of the test card signal
309 int channels
= mlt_properties_get_int( properties
, "channels" );
310 int frequency
= mlt_properties_get_int( properties
, "frequency" );
311 static int counter
= 0;
313 int samples
= mlt_sample_calculator( mlt_properties_get_double( this->properties
, "fps" ), frequency
, counter
++ );
318 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
319 *duration
= ( ( samples
* 1000 ) / frequency
);
321 if ( mlt_properties_get_int( properties
, "audio_off" ) )
328 if ( init_audio
== 1 )
330 SDL_AudioSpec request
;
333 int audio_buffer
= mlt_properties_get_int( properties
, "audio_buffer" );
335 // specify audio format
336 memset( &request
, 0, sizeof( SDL_AudioSpec
) );
338 request
.freq
= frequency
;
339 request
.format
= AUDIO_S16
;
340 request
.channels
= channels
;
341 request
.samples
= audio_buffer
;
342 request
.callback
= sdl_fill_audio
;
343 request
.userdata
= (void *)this;
344 if ( SDL_OpenAudio( &request
, &got
) != 0 )
346 fprintf( stderr
, "SDL failed to open audio: %s\n", SDL_GetError() );
349 else if ( got
.size
!= 0 )
356 if ( init_audio
== 0 )
358 mlt_properties properties
= mlt_frame_properties( frame
);
359 bytes
= ( samples
* channels
* 2 );
360 pthread_mutex_lock( &this->audio_mutex
);
361 while ( this->running
&& bytes
> ( sizeof( this->audio_buffer
) - this->audio_avail
) )
362 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
365 if ( mlt_properties_get_double( properties
, "_speed" ) == 1 )
366 memcpy( &this->audio_buffer
[ this->audio_avail
], pcm
, bytes
);
368 memset( &this->audio_buffer
[ this->audio_avail
], 0, bytes
);
369 this->audio_avail
+= bytes
;
371 pthread_cond_broadcast( &this->audio_cond
);
372 pthread_mutex_unlock( &this->audio_mutex
);
382 static int consumer_play_video( consumer_sdl
this, mlt_frame frame
)
384 // Get the properties of this consumer
385 mlt_properties properties
= this->properties
;
387 mlt_image_format vfmt
= mlt_image_yuv422
;
388 int width
= this->width
, height
= this->height
;
392 if ( mlt_properties_get_int( properties
, "video_off" ) == 0 )
394 // Get the image, width and height
395 mlt_events_fire( properties
, "consumer-frame-show", frame
, NULL
);
396 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
399 if ( this->sdl_screen
!= NULL
)
403 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
405 while ( SDL_PollEvent( &event
) )
407 mlt_events_fire( this->properties
, "consumer-sdl-event", &event
, NULL
);
411 case SDL_VIDEORESIZE
:
412 this->window_width
= event
.resize
.w
;
413 this->window_height
= event
.resize
.h
;
421 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
422 char keyboard
[ 2 ] = " ";
423 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
424 if ( callback
!= NULL
&& producer
!= NULL
&& event
.key
.keysym
.unicode
< 0x80 && event
.key
.keysym
.unicode
> 0 )
426 keyboard
[ 0 ] = ( char )event
.key
.keysym
.unicode
;
427 callback( producer
, keyboard
);
435 if ( width
!= this->width
|| height
!= this->height
||
436 ( int )( this->last_frame_aspect
* 1000 ) != ( int )( mlt_frame_get_aspect_ratio( frame
) * 1000 ) )
438 if ( mlt_frame_get_aspect_ratio( frame
) != 1.0 || this->last_frame_aspect
== 0.0 )
441 this->height
= height
;
442 this->last_frame_aspect
= mlt_frame_get_aspect_ratio( frame
);
447 if ( this->sdl_screen
== NULL
|| changed
)
449 // Determine frame's display aspect ratio
450 float frame_aspect
= mlt_frame_get_aspect_ratio( frame
) * this->width
/ this->height
;
452 // Determine window's new display aspect ratio
453 float this_aspect
= ( float )this->window_width
/ this->window_height
;
455 // If using hardware scaler
456 if ( mlt_properties_get( properties
, "rescale" ) != NULL
&&
457 !strcmp( mlt_properties_get( properties
, "rescale" ), "none" ) )
459 // Special case optimisation to negate odd effect of sample aspect ratio
460 // not corresponding exactly with image resolution.
461 if ( ( (int)( this_aspect
* 1000 ) == (int)( this->display_aspect
* 1000 ) ) &&
462 ( (int)( mlt_frame_get_aspect_ratio( frame
) * 1000 ) == (int)( this->aspect_ratio
* 1000 ) ) )
464 this->rect
.w
= this->window_width
;
465 this->rect
.h
= this->window_height
;
469 // Use hardware scaler to normalise display aspect ratio
470 this->rect
.w
= frame_aspect
/ this_aspect
* this->window_width
;
471 this->rect
.h
= this->window_height
;
472 if ( this->rect
.w
> this->window_width
)
474 this->rect
.w
= this->window_width
;
475 this->rect
.h
= this_aspect
/ frame_aspect
* this->window_height
;
479 // Special case optimisation to negate odd effect of sample aspect ratio
480 // not corresponding exactly with image resolution.
481 else if ( (int)( this_aspect
* 1000 ) == (int)( this->display_aspect
* 1000 ) )
483 this->rect
.w
= this->window_width
;
484 this->rect
.h
= this->window_height
;
486 // Use hardware scaler to normalise sample aspect ratio
487 else if ( this->window_height
* this->display_aspect
> this->window_width
)
489 this->rect
.w
= this->window_width
;
490 this->rect
.h
= this->window_width
/ this->display_aspect
;
494 this->rect
.w
= this->window_height
* this->display_aspect
;
495 this->rect
.h
= this->window_height
;
498 this->rect
.x
= ( this->window_width
- this->rect
.w
) / 2;
499 this->rect
.y
= ( this->window_height
- this->rect
.h
) / 2;
501 mlt_properties_set_int( this->properties
, "rect_x", this->rect
.x
);
502 mlt_properties_set_int( this->properties
, "rect_y", this->rect
.y
);
503 mlt_properties_set_int( this->properties
, "rect_w", this->rect
.w
);
504 mlt_properties_set_int( this->properties
, "rect_h", this->rect
.h
);
506 // Force an overlay recreation
507 if ( this->sdl_overlay
!= NULL
)
508 SDL_FreeYUVOverlay( this->sdl_overlay
);
510 // open SDL window with video overlay, if possible
512 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
513 sdl_unlock_display();
515 if ( this->sdl_screen
!= NULL
)
517 SDL_SetClipRect( this->sdl_screen
, &this->rect
);
519 this->sdl_overlay
= SDL_CreateYUVOverlay( this->width
, this->height
, SDL_YUY2_OVERLAY
, this->sdl_screen
);
520 sdl_unlock_display();
524 if ( mlt_properties_get_int( properties
, "changed" ) )
527 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
528 SDL_SetClipRect( this->sdl_screen
, &this->rect
);
529 SDL_Flip( this->sdl_screen
);
530 sdl_unlock_display();
531 mlt_properties_set_int( properties
, "changed", 0 );
534 if ( this->sdl_screen
!= NULL
&& this->sdl_overlay
!= NULL
)
536 this->buffer
= this->sdl_overlay
->pixels
[ 0 ];
538 if ( SDL_LockYUVOverlay( this->sdl_overlay
) >= 0 )
541 memcpy( this->buffer
, image
, width
* height
* 2 );
542 SDL_UnlockYUVOverlay( this->sdl_overlay
);
543 SDL_DisplayYUVOverlay( this->sdl_overlay
, &this->sdl_screen
->clip_rect
);
545 sdl_unlock_display();
552 static void *video_thread( void *arg
)
555 consumer_sdl
this = arg
;
557 // Obtain time of thread start
562 mlt_frame next
= NULL
;
563 mlt_properties properties
= NULL
;
566 // Get real time flag
567 int real_time
= mlt_properties_get_int( this->properties
, "real_time" );
569 // Get the current time
570 gettimeofday( &now
, NULL
);
572 // Determine start time
573 start
= ( int64_t )now
.tv_sec
* 1000000 + now
.tv_usec
;
575 while ( this->running
)
577 // Pop the next frame
578 pthread_mutex_lock( &this->video_mutex
);
579 while ( ( next
= mlt_deque_pop_front( this->queue
) ) == NULL
&& this->running
)
580 pthread_cond_wait( &this->video_cond
, &this->video_mutex
);
581 pthread_mutex_unlock( &this->video_mutex
);
583 // Get the properties
584 properties
= mlt_frame_properties( next
);
586 // Get the speed of the frame
587 speed
= mlt_properties_get_double( properties
, "_speed" );
589 // Get the current time
590 gettimeofday( &now
, NULL
);
592 // Get the elapsed time
593 elapsed
= ( ( int64_t )now
.tv_sec
* 1000000 + now
.tv_usec
) - start
;
595 // See if we have to delay the display of the current frame
596 if ( mlt_properties_get_int( properties
, "rendered" ) == 1 && this->running
)
598 // Obtain the scheduled playout time
599 mlt_position scheduled
= mlt_properties_get_position( properties
, "playtime" );
601 // Determine the difference between the elapsed time and the scheduled playout time
602 mlt_position difference
= scheduled
- elapsed
;
604 // Smooth playback a bit
605 if ( real_time
&& ( difference
> 20000 && speed
== 1.0 ) )
607 tm
.tv_sec
= difference
/ 1000000;
608 tm
.tv_nsec
= ( difference
% 1000000 ) * 500;
609 nanosleep( &tm
, NULL
);
612 // Show current frame if not too old
613 if ( !real_time
|| ( difference
> -10000 || speed
!= 1.0 || mlt_deque_count( this->queue
) < 2 ) )
614 consumer_play_video( this, next
);
616 // If the queue is empty, recalculate start to allow build up again
617 if ( real_time
&& ( mlt_deque_count( this->queue
) == 0 && speed
== 1.0 ) )
619 gettimeofday( &now
, NULL
);
620 start
= ( ( int64_t )now
.tv_sec
* 1000000 + now
.tv_usec
) - scheduled
+ 20000;
624 // This frame can now be closed
625 mlt_frame_close( next
);
628 mlt_consumer_stopped( &this->parent
);
633 /** Threaded wrapper for pipe.
636 static void *consumer_thread( void *arg
)
639 consumer_sdl
this = arg
;
642 mlt_consumer consumer
= &this->parent
;
647 // internal intialization
650 mlt_frame frame
= NULL
;
651 mlt_properties properties
= NULL
;
653 int64_t playtime
= 0;
654 struct timespec tm
= { 0, 100000 };
656 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) < 0 )
658 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
662 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
663 SDL_EnableUNICODE( 1 );
665 // Loop until told not to
666 while( this->running
)
668 // Get a frame from the attached producer
669 frame
= mlt_consumer_rt_frame( consumer
);
671 // Ensure that we have a frame
674 // Get the frame properties
675 properties
= mlt_frame_properties( frame
);
678 init_audio
= consumer_play_audio( this, frame
, init_audio
, &duration
);
680 // Determine the start time now
681 if ( this->playing
&& init_video
)
683 // Create the video thread
684 pthread_create( &thread
, NULL
, video_thread
, this );
686 // Video doesn't need to be initialised any more
690 // Set playtime for this frame
691 mlt_properties_set_position( properties
, "playtime", playtime
);
693 while ( this->running
&& mlt_deque_count( this->queue
) > 15 )
694 nanosleep( &tm
, NULL
);
696 // Push this frame to the back of the queue
697 pthread_mutex_lock( &this->video_mutex
);
698 mlt_deque_push_back( this->queue
, frame
);
699 pthread_cond_broadcast( &this->video_cond
);
700 pthread_mutex_unlock( &this->video_mutex
);
702 // Calculate the next playtime
703 playtime
+= ( duration
* 1000 );
707 // Kill the video thread
708 if ( init_video
== 0 )
710 pthread_mutex_lock( &this->video_mutex
);
711 pthread_cond_broadcast( &this->video_cond
);
712 pthread_mutex_unlock( &this->video_mutex
);
713 pthread_join( thread
, NULL
);
717 if ( this->sdl_overlay
!= NULL
)
718 SDL_FreeYUVOverlay( this->sdl_overlay
);
721 while( mlt_deque_count( this->queue
) )
722 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
724 this->sdl_screen
= NULL
;
725 this->sdl_overlay
= NULL
;
726 this->audio_avail
= 0;
731 static int consumer_get_dimensions( int *width
, int *height
)
735 // SDL windows manager structure
738 // Specify the SDL Version
739 SDL_VERSION( &wm
.version
);
744 // Get the wm structure
745 if ( SDL_GetWMInfo( &wm
) == 1 )
747 // Check that we have the X11 wm
748 if ( wm
.subsystem
== SDL_SYSWM_X11
)
750 // Get the SDL window
751 Window window
= wm
.info
.x11
.window
;
753 // Get the display session
754 Display
*display
= wm
.info
.x11
.display
;
756 // Get the window attributes
757 XWindowAttributes attr
;
758 XGetWindowAttributes( display
, window
, &attr
);
760 // Determine whether window has changed
761 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
763 // Return width and height
765 *height
= attr
.height
;
769 // Unlock the display
775 /** Callback to allow override of the close method.
778 static void consumer_close( mlt_consumer parent
)
780 // Get the actual object
781 consumer_sdl
this = parent
->child
;
784 mlt_consumer_stop( parent
);
787 mlt_deque_close( this->queue
);
790 pthread_mutex_destroy( &this->audio_mutex
);
791 pthread_cond_destroy( &this->audio_cond
);
793 // Now clean up the rest
794 mlt_consumer_close( parent
);
796 // Finally clean up this