2 * consumer_sdl_still.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2003-2004, 2010 Ushodaya Enterprises Limited
4 * Author: Charles Yates
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt_consumer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_deque.h>
24 #include <framework/mlt_factory.h>
25 #include <framework/mlt_filter.h>
26 #include <framework/mlt_log.h>
32 #include <SDL_syswm.h>
34 #include "consumer_sdl_osx.h"
36 extern pthread_mutex_t mlt_sdl_mutex;
38 /** This classes definition.
41 typedef struct consumer_sdl_s *consumer_sdl;
45 struct mlt_consumer_s parent;
46 mlt_properties properties;
59 mlt_producer last_producer;
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 );
71 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
73 /** This is what will be called by the factory - anything can be passed in
74 via the argument, but keep it simple.
77 mlt_consumer consumer_sdl_still_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
79 // Create the consumer object
80 consumer_sdl this = calloc( 1, sizeof( struct consumer_sdl_s ) );
82 // If no malloc'd and consumer init ok
83 if ( this != NULL && mlt_consumer_init( &this->parent, this, profile ) == 0 )
85 // Get the parent consumer object
86 mlt_consumer parent = &this->parent;
88 // get a handle on properties
89 mlt_service service = MLT_CONSUMER_SERVICE( parent );
90 this->properties = MLT_SERVICE_PROPERTIES( service );
92 // We have stuff to clean up, so override the close method
93 parent->close = consumer_close;
95 // Default scaler (for now we'll use nearest)
96 mlt_properties_set( this->properties, "rescale", "nearest" );
98 // We're always going to run this in non-realtime mode
99 mlt_properties_set( this->properties, "real_time", "0" );
101 // Ensure we don't join on a non-running object
104 // process actual param
105 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
107 this->width = mlt_properties_get_int( this->properties, "width" );
108 this->height = mlt_properties_get_int( this->properties, "height" );
112 mlt_properties_set_int( this->properties, "width", this->width );
113 mlt_properties_set_int( this->properties, "height", this->height );
117 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
119 // Allow thread to be started/stopped
120 parent->start = consumer_start;
121 parent->stop = consumer_stop;
122 parent->is_stopped = consumer_is_stopped;
124 // Register specific events
125 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
127 // Return the consumer produced
131 // malloc or consumer init failed
138 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
140 if ( listener != NULL )
141 listener( owner, this, ( SDL_Event * )args[ 0 ] );
144 static int consumer_start( mlt_consumer parent )
146 consumer_sdl this = parent->child;
148 if ( !this->running )
150 int preview_off = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "preview_off" );
151 int sdl_started = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" );
153 consumer_stop( parent );
155 this->last_position = -1;
159 // Allow the user to force resizing to window size
160 this->width = mlt_properties_get_int( this->properties, "width" );
161 this->height = mlt_properties_get_int( this->properties, "height" );
163 // Default window size
164 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
165 this->window_width = ( double )this->height * display_ratio + 0.5;
166 this->window_height = this->height;
168 if ( sdl_started == 0 && preview_off == 0 )
170 pthread_mutex_lock( &mlt_sdl_mutex );
171 int ret = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
172 pthread_mutex_unlock( &mlt_sdl_mutex );
175 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
179 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
180 SDL_EnableUNICODE( 1 );
183 pthread_mutex_lock( &mlt_sdl_mutex );
184 if ( !SDL_GetVideoSurface() && preview_off == 0 )
185 SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
186 pthread_mutex_unlock( &mlt_sdl_mutex );
188 pthread_create( &this->thread, NULL, consumer_thread, this );
194 static int consumer_stop( mlt_consumer parent )
196 // Get the actual object
197 consumer_sdl this = parent->child;
199 if ( this->joined == 0 )
201 int preview_off = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "preview_off" );
202 int sdl_started = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" );
204 // Kill the thread and clean up
207 pthread_join( this->thread, NULL );
210 if ( sdl_started == 0 && preview_off == 0 )
212 pthread_mutex_lock( &mlt_sdl_mutex );
214 pthread_mutex_unlock( &mlt_sdl_mutex );
221 static int consumer_is_stopped( mlt_consumer parent )
223 consumer_sdl this = parent->child;
224 return !this->running;
227 static int sdl_lock_display( )
229 pthread_mutex_lock( &mlt_sdl_mutex );
230 SDL_Surface *screen = SDL_GetVideoSurface( );
231 int result = screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
232 pthread_mutex_unlock( &mlt_sdl_mutex );
236 static void sdl_unlock_display( )
238 pthread_mutex_lock( &mlt_sdl_mutex );
239 SDL_Surface *screen = SDL_GetVideoSurface( );
240 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
241 SDL_UnlockSurface( screen );
242 pthread_mutex_unlock( &mlt_sdl_mutex );
245 static inline void display_1( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
247 // Generate the affine transform scaling values
248 if ( rect.w == 0 || rect.h == 0 ) return;
249 int scale_width = ( width << 16 ) / rect.w;
250 int scale_height = ( height << 16 ) / rect.h;
251 int stride = width * 4;
255 // Constants defined for clarity and optimisation
256 int scanlength = screen->pitch;
257 uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
260 // Iterate through the screen using a very basic scaling algorithm
261 for ( y = 0; y < rect.h; y ++ )
264 row_index = ( 32768 + scale_height * y ) >> 16;
265 row = image + stride * row_index;
266 for ( x = 0; x < rect.w; x ++ )
268 q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 4 );
269 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
275 static inline void display_2( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
277 // Generate the affine transform scaling values
278 if ( rect.w == 0 || rect.h == 0 ) return;
279 int scale_width = ( width << 16 ) / rect.w;
280 int scale_height = ( height << 16 ) / rect.h;
281 int stride = width * 4;
285 // Constants defined for clarity and optimisation
286 int scanlength = screen->pitch / 2;
287 uint16_t *start = ( uint16_t * )screen->pixels + rect.y * scanlength + rect.x;
290 // Iterate through the screen using a very basic scaling algorithm
291 for ( y = 0; y < rect.h; y ++ )
294 row_index = ( 32768 + scale_height * y ) >> 16;
295 row = image + stride * row_index;
296 for ( x = 0; x < rect.w; x ++ )
298 q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 4 );
299 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
305 static inline void display_3( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
307 // Generate the affine transform scaling values
308 if ( rect.w == 0 || rect.h == 0 ) return;
309 int scale_width = ( width << 16 ) / rect.w;
310 int scale_height = ( height << 16 ) / rect.h;
311 int stride = width * 4;
315 // Constants defined for clarity and optimisation
316 int scanlength = screen->pitch;
317 uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
321 // Iterate through the screen using a very basic scaling algorithm
322 for ( y = 0; y < rect.h; y ++ )
325 row_index = ( 32768 + scale_height * y ) >> 16;
326 row = image + stride * row_index;
327 for ( x = 0; x < rect.w; x ++ )
329 q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 4 );
330 pixel = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
331 *p ++ = (pixel & 0xFF0000) >> 16;
332 *p ++ = (pixel & 0x00FF00) >> 8;
333 *p ++ = (pixel & 0x0000FF);
339 static inline void display_4( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
341 // Generate the affine transform scaling values
342 if ( rect.w == 0 || rect.h == 0 ) return;
343 int scale_width = ( width << 16 ) / rect.w;
344 int scale_height = ( height << 16 ) / rect.h;
345 int stride = width * 4;
349 // Constants defined for clarity and optimisation
350 int scanlength = screen->pitch / 4;
351 uint32_t *start = ( uint32_t * )screen->pixels + rect.y * scanlength + rect.x;
354 // Iterate through the screen using a very basic scaling algorithm
355 for ( y = 0; y < rect.h; y ++ )
358 row_index = ( 32768 + scale_height * y ) >> 16;
359 row = image + stride * row_index;
360 for ( x = 0; x < rect.w; x ++ )
362 q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 4 );
363 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
369 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
371 // Get the properties of this consumer
372 mlt_properties properties = this->properties;
374 mlt_image_format vfmt = mlt_image_rgb24a;
375 int height = this->height;
376 int width = this->width;
377 uint8_t *image = NULL;
379 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
381 void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
382 void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
384 if ( lock != NULL ) lock( );
385 void *pool = mlt_cocoa_autorelease_init();
389 if ( SDL_GetVideoSurface() )
393 pthread_mutex_lock( &mlt_sdl_mutex );
394 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
395 pthread_mutex_unlock( &mlt_sdl_mutex );
397 while ( SDL_PollEvent( &event ) )
399 mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
403 case SDL_VIDEORESIZE:
404 this->window_width = event.resize.w;
405 this->window_height = event.resize.h;
413 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
414 char keyboard[ 2 ] = " ";
415 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
416 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
418 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
419 callback( producer, keyboard );
427 if ( !SDL_GetVideoSurface() || changed )
430 pthread_mutex_lock( &mlt_sdl_mutex );
431 SDL_Surface *screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
432 if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
433 screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
437 uint32_t color = mlt_properties_get_int( this->properties, "window_background" );
438 SDL_FillRect( screen, NULL, color >> 8 );
441 pthread_mutex_unlock( &mlt_sdl_mutex );
443 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "test_audio", 1 );
445 this->last_position == mlt_frame_get_position( frame ) &&
446 this->last_producer == mlt_frame_get_original_producer( frame ) &&
447 !mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "refresh" ) )
449 sdl_unlock_display( );
450 mlt_cocoa_autorelease_close( pool );
451 if ( unlock != NULL ) unlock( );
452 struct timespec tm = { 0, 100000 };
453 nanosleep( &tm, NULL );
457 // Update last frame shown info
458 this->last_position = mlt_frame_get_position( frame );
459 this->last_producer = mlt_frame_get_original_producer( frame );
461 // Get the image, width and height
462 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
466 char *rescale = mlt_properties_get( properties, "rescale" );
467 if ( rescale != NULL && strcmp( rescale, "none" ) )
469 double this_aspect = display_ratio / ( ( double )this->window_width / ( double )this->window_height );
470 this->rect.w = 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 = ( 1.0 / this_aspect ) * this->window_height;
480 double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
481 this->rect.w = frame_aspect * this->window_height;
482 this->rect.h = this->window_height;
483 if ( this->rect.w > this->window_width )
485 this->rect.w = this->window_width;
486 this->rect.h = ( 1.0 / frame_aspect ) * this->window_width;
490 this->rect.x = ( this->window_width - this->rect.w ) / 2;
491 this->rect.y = ( this->window_height - this->rect.h ) / 2;
493 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
494 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
495 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
496 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
499 pthread_mutex_lock( &mlt_sdl_mutex );
500 SDL_Surface *screen = SDL_GetVideoSurface( );
501 if ( !mlt_consumer_is_stopped( &this->parent ) && screen && screen->pixels )
503 switch( screen->format->BytesPerPixel )
506 display_1( screen, this->rect, image, width, height );
509 display_2( screen, this->rect, image, width, height );
512 display_3( screen, this->rect, image, width, height );
515 display_4( screen, this->rect, image, width, height );
518 fprintf( stderr, "Unsupported video depth %d\n", screen->format->BytesPerPixel );
522 // Flip it into sight
525 pthread_mutex_unlock( &mlt_sdl_mutex );
527 sdl_unlock_display();
528 mlt_cocoa_autorelease_close( pool );
529 if ( unlock != NULL ) unlock( );
530 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
535 /** Threaded wrapper for pipe.
538 static void *consumer_thread( void *arg )
541 consumer_sdl this = arg;
544 mlt_consumer consumer = &this->parent;
545 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
546 mlt_frame frame = NULL;
548 // Allow the hosting app to provide the preview
549 int preview_off = mlt_properties_get_int( properties, "preview_off" );
551 // Loop until told not to
552 while( this->running )
554 // Get a frame from the attached producer
555 frame = mlt_consumer_rt_frame( consumer );
557 // Ensure that we have a frame
558 if ( this->running && frame != NULL )
560 if ( preview_off == 0 )
562 consumer_play_video( this, frame );
566 mlt_image_format vfmt = mlt_image_rgb24a;
567 int height = this->height;
568 int width = this->width;
569 uint8_t *image = NULL;
570 mlt_image_format preview_format = mlt_properties_get_int( properties, "preview_format" );
572 // Check if a specific colour space has been requested
573 if ( preview_off && preview_format != mlt_image_none )
574 vfmt = preview_format;
576 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
577 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "format", vfmt );
578 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
580 mlt_frame_close( frame );
584 if ( frame ) mlt_frame_close( frame );
592 static int consumer_get_dimensions( int *width, int *height )
596 // SDL windows manager structure
599 // Specify the SDL Version
600 SDL_VERSION( &wm.version );
603 // Get the wm structure
604 if ( SDL_GetWMInfo( &wm ) == 1 )
607 // Check that we have the X11 wm
608 if ( wm.subsystem == SDL_SYSWM_X11 )
610 // Get the SDL window
611 Window window = wm.info.x11.window;
613 // Get the display session
614 Display *display = wm.info.x11.display;
616 // Get the window attributes
617 XWindowAttributes attr;
618 XGetWindowAttributes( display, window, &attr );
620 // Determine whether window has changed
621 changed = *width != attr.width || *height != attr.height;
623 // Return width and height
625 *height = attr.height;
634 /** Callback to allow override of the close method.
637 static void consumer_close( mlt_consumer parent )
639 // Get the actual object
640 consumer_sdl this = parent->child;
643 mlt_consumer_stop( parent );
645 // Now clean up the rest
646 mlt_consumer_close( parent );
648 // Finally clean up this