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;
56 SDL_Surface *sdl_screen;
60 mlt_producer last_producer;
63 /** Forward references to static functions.
66 static int consumer_start( mlt_consumer parent );
67 static int consumer_stop( mlt_consumer parent );
68 static int consumer_is_stopped( mlt_consumer parent );
69 static void consumer_close( mlt_consumer parent );
70 static void *consumer_thread( void * );
71 static int consumer_get_dimensions( int *width, int *height );
72 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
74 /** This is what will be called by the factory - anything can be passed in
75 via the argument, but keep it simple.
78 mlt_consumer consumer_sdl_still_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
80 // Create the consumer object
81 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
83 // If no malloc'd and consumer init ok
84 if ( this != NULL && mlt_consumer_init( &this->parent, this, profile ) == 0 )
86 // Get the parent consumer object
87 mlt_consumer parent = &this->parent;
89 // get a handle on properties
90 mlt_service service = MLT_CONSUMER_SERVICE( parent );
91 this->properties = MLT_SERVICE_PROPERTIES( service );
93 // We have stuff to clean up, so override the close method
94 parent->close = consumer_close;
96 // Default scaler (for now we'll use nearest)
97 mlt_properties_set( this->properties, "rescale", "nearest" );
99 // We're always going to run this in non-realtime mode
100 mlt_properties_set( this->properties, "real_time", "0" );
102 // Ensure we don't join on a non-running object
105 // process actual param
106 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
108 this->width = mlt_properties_get_int( this->properties, "width" );
109 this->height = mlt_properties_get_int( this->properties, "height" );
113 mlt_properties_set_int( this->properties, "width", this->width );
114 mlt_properties_set_int( this->properties, "height", this->height );
118 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
120 // Allow thread to be started/stopped
121 parent->start = consumer_start;
122 parent->stop = consumer_stop;
123 parent->is_stopped = consumer_is_stopped;
125 // Register specific events
126 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
128 // Return the consumer produced
132 // malloc or consumer init failed
139 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
141 if ( listener != NULL )
142 listener( owner, this, ( SDL_Event * )args[ 0 ] );
145 static int consumer_start( mlt_consumer parent )
147 consumer_sdl this = parent->child;
149 if ( !this->running )
151 int preview_off = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "preview_off" );
152 int sdl_started = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" );
154 consumer_stop( parent );
156 this->last_position = -1;
160 // Allow the user to force resizing to window size
161 this->width = mlt_properties_get_int( this->properties, "width" );
162 this->height = mlt_properties_get_int( this->properties, "height" );
164 // Default window size
165 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
166 this->window_width = ( double )this->height * display_ratio;
167 this->window_height = this->height;
169 if ( sdl_started == 0 && preview_off == 0 )
171 pthread_mutex_lock( &mlt_sdl_mutex );
172 int ret = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
173 pthread_mutex_unlock( &mlt_sdl_mutex );
176 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
180 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
181 SDL_EnableUNICODE( 1 );
183 else if ( preview_off == 0 )
185 pthread_mutex_lock( &mlt_sdl_mutex );
186 SDL_Surface *screen = SDL_GetVideoSurface( );
187 pthread_mutex_unlock( &mlt_sdl_mutex );
188 if ( screen != NULL )
190 this->sdl_screen = screen;
194 if ( this->sdl_screen == NULL && preview_off == 0 )
196 pthread_mutex_lock( &mlt_sdl_mutex );
197 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
198 pthread_mutex_unlock( &mlt_sdl_mutex );
201 pthread_create( &this->thread, NULL, consumer_thread, this );
207 static int consumer_stop( mlt_consumer parent )
209 // Get the actual object
210 consumer_sdl this = parent->child;
212 if ( this->joined == 0 )
214 int preview_off = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "preview_off" );
215 int sdl_started = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" );
217 // Kill the thread and clean up
220 pthread_join( this->thread, NULL );
223 if ( sdl_started == 0 && preview_off == 0 )
225 pthread_mutex_lock( &mlt_sdl_mutex );
227 pthread_mutex_unlock( &mlt_sdl_mutex );
230 this->sdl_screen = NULL;
236 static int consumer_is_stopped( mlt_consumer parent )
238 consumer_sdl this = parent->child;
239 return !this->running;
242 static int sdl_lock_display( )
244 pthread_mutex_lock( &mlt_sdl_mutex );
245 SDL_Surface *screen = SDL_GetVideoSurface( );
246 int result = screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
247 pthread_mutex_unlock( &mlt_sdl_mutex );
251 static void sdl_unlock_display( )
253 pthread_mutex_lock( &mlt_sdl_mutex );
254 SDL_Surface *screen = SDL_GetVideoSurface( );
255 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
256 SDL_UnlockSurface( screen );
257 pthread_mutex_unlock( &mlt_sdl_mutex );
260 static inline void display_1( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
262 // Generate the affine transform scaling values
263 if ( rect.w == 0 || rect.h == 0 ) return;
264 int scale_width = ( width << 16 ) / rect.w;
265 int scale_height = ( height << 16 ) / rect.h;
266 int stride = width * 4;
270 // Constants defined for clarity and optimisation
271 int scanlength = screen->pitch;
272 uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
275 // Iterate through the screen using a very basic scaling algorithm
276 for ( y = 0; y < rect.h; y ++ )
279 row_index = ( 32768 + scale_height * y ) >> 16;
280 row = image + stride * row_index;
281 for ( x = 0; x < rect.w; x ++ )
283 q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 4 );
284 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
290 static inline void display_2( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
292 // Generate the affine transform scaling values
293 if ( rect.w == 0 || rect.h == 0 ) return;
294 int scale_width = ( width << 16 ) / rect.w;
295 int scale_height = ( height << 16 ) / rect.h;
296 int stride = width * 4;
300 // Constants defined for clarity and optimisation
301 int scanlength = screen->pitch / 2;
302 uint16_t *start = ( uint16_t * )screen->pixels + rect.y * scanlength + rect.x;
305 // Iterate through the screen using a very basic scaling algorithm
306 for ( y = 0; y < rect.h; y ++ )
309 row_index = ( 32768 + scale_height * y ) >> 16;
310 row = image + stride * row_index;
311 for ( x = 0; x < rect.w; x ++ )
313 q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 4 );
314 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
320 static inline void display_3( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
322 // Generate the affine transform scaling values
323 if ( rect.w == 0 || rect.h == 0 ) return;
324 int scale_width = ( width << 16 ) / rect.w;
325 int scale_height = ( height << 16 ) / rect.h;
326 int stride = width * 4;
330 // Constants defined for clarity and optimisation
331 int scanlength = screen->pitch;
332 uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
336 // Iterate through the screen using a very basic scaling algorithm
337 for ( y = 0; y < rect.h; y ++ )
340 row_index = ( 32768 + scale_height * y ) >> 16;
341 row = image + stride * row_index;
342 for ( x = 0; x < rect.w; x ++ )
344 q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 4 );
345 pixel = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
346 *p ++ = (pixel & 0xFF0000) >> 16;
347 *p ++ = (pixel & 0x00FF00) >> 8;
348 *p ++ = (pixel & 0x0000FF);
354 static inline void display_4( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
356 // Generate the affine transform scaling values
357 if ( rect.w == 0 || rect.h == 0 ) return;
358 int scale_width = ( width << 16 ) / rect.w;
359 int scale_height = ( height << 16 ) / rect.h;
360 int stride = width * 4;
364 // Constants defined for clarity and optimisation
365 int scanlength = screen->pitch / 4;
366 uint32_t *start = ( uint32_t * )screen->pixels + rect.y * scanlength + rect.x;
369 // Iterate through the screen using a very basic scaling algorithm
370 for ( y = 0; y < rect.h; y ++ )
373 row_index = ( 32768 + scale_height * y ) >> 16;
374 row = image + stride * row_index;
375 for ( x = 0; x < rect.w; x ++ )
377 q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 4 );
378 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
384 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
386 // Get the properties of this consumer
387 mlt_properties properties = this->properties;
389 mlt_image_format vfmt = mlt_image_rgb24a;
390 int height = this->height;
391 int width = this->width;
392 uint8_t *image = NULL;
394 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
396 void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
397 void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
399 if ( lock != NULL ) lock( );
400 void *pool = mlt_cocoa_autorelease_init();
404 if ( this->sdl_screen != NULL )
408 pthread_mutex_lock( &mlt_sdl_mutex );
409 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
410 pthread_mutex_unlock( &mlt_sdl_mutex );
412 while ( SDL_PollEvent( &event ) )
414 mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
418 case SDL_VIDEORESIZE:
419 this->window_width = event.resize.w;
420 this->window_height = event.resize.h;
428 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
429 char keyboard[ 2 ] = " ";
430 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
431 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
433 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
434 callback( producer, keyboard );
442 if ( this->sdl_screen == NULL || changed )
445 pthread_mutex_lock( &mlt_sdl_mutex );
446 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
447 if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
448 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
450 uint32_t color = mlt_properties_get_int( this->properties, "window_background" );
451 if ( this->sdl_screen )
453 SDL_FillRect( this->sdl_screen, NULL, color >> 8 );
456 pthread_mutex_unlock( &mlt_sdl_mutex );
458 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "test_audio", 1 );
460 this->last_position == mlt_frame_get_position( frame ) &&
461 this->last_producer == mlt_frame_get_original_producer( frame ) &&
462 !mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "refresh" ) )
464 sdl_unlock_display( );
465 mlt_cocoa_autorelease_close( pool );
466 if ( unlock != NULL ) unlock( );
467 struct timespec tm = { 0, 100000 };
468 nanosleep( &tm, NULL );
472 // Update last frame shown info
473 this->last_position = mlt_frame_get_position( frame );
474 this->last_producer = mlt_frame_get_original_producer( frame );
476 // Get the image, width and height
477 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
481 char *rescale = mlt_properties_get( properties, "rescale" );
482 if ( rescale != NULL && strcmp( rescale, "none" ) )
484 double this_aspect = display_ratio / ( ( double )this->window_width / ( double )this->window_height );
485 this->rect.w = this_aspect * this->window_width;
486 this->rect.h = this->window_height;
487 if ( this->rect.w > this->window_width )
489 this->rect.w = this->window_width;
490 this->rect.h = ( 1.0 / this_aspect ) * this->window_height;
495 double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
496 this->rect.w = frame_aspect * this->window_height;
497 this->rect.h = this->window_height;
498 if ( this->rect.w > this->window_width )
500 this->rect.w = this->window_width;
501 this->rect.h = ( 1.0 / frame_aspect ) * this->window_width;
505 this->rect.x = ( this->window_width - this->rect.w ) / 2;
506 this->rect.y = ( this->window_height - this->rect.h ) / 2;
508 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
509 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
510 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
511 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
514 pthread_mutex_lock( &mlt_sdl_mutex );
515 SDL_Surface *screen = SDL_GetVideoSurface( );
516 pthread_mutex_unlock( &mlt_sdl_mutex );
517 if ( !mlt_consumer_is_stopped( &this->parent ) && screen != NULL && this->sdl_screen != NULL && this->sdl_screen->pixels != NULL )
519 switch( this->sdl_screen->format->BytesPerPixel )
522 display_1( this->sdl_screen, this->rect, image, width, height );
525 display_2( this->sdl_screen, this->rect, image, width, height );
528 display_3( this->sdl_screen, this->rect, image, width, height );
531 display_4( this->sdl_screen, this->rect, image, width, height );
534 fprintf( stderr, "Unsupported video depth %d\n", this->sdl_screen->format->BytesPerPixel );
538 // Flip it into sight
539 pthread_mutex_lock( &mlt_sdl_mutex );
540 SDL_Flip( this->sdl_screen );
541 pthread_mutex_unlock( &mlt_sdl_mutex );
544 sdl_unlock_display();
545 mlt_cocoa_autorelease_close( pool );
546 if ( unlock != NULL ) unlock( );
547 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
552 /** Threaded wrapper for pipe.
555 static void *consumer_thread( void *arg )
558 consumer_sdl this = arg;
561 mlt_consumer consumer = &this->parent;
562 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
563 mlt_frame frame = NULL;
565 // Allow the hosting app to provide the preview
566 int preview_off = mlt_properties_get_int( properties, "preview_off" );
568 // Loop until told not to
569 while( this->running )
571 // Get a frame from the attached producer
572 frame = mlt_consumer_rt_frame( consumer );
574 // Ensure that we have a frame
575 if ( this->running && frame != NULL )
577 if ( preview_off == 0 )
579 consumer_play_video( this, frame );
583 mlt_image_format vfmt = mlt_image_rgb24a;
584 int height = this->height;
585 int width = this->width;
586 uint8_t *image = NULL;
587 mlt_image_format preview_format = mlt_properties_get_int( properties, "preview_format" );
589 // Check if a specific colour space has been requested
590 if ( preview_off && preview_format != mlt_image_none )
591 vfmt = preview_format;
593 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
594 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "format", vfmt );
595 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
597 mlt_frame_close( frame );
601 if ( frame ) mlt_frame_close( frame );
609 static int consumer_get_dimensions( int *width, int *height )
613 // SDL windows manager structure
616 // Specify the SDL Version
617 SDL_VERSION( &wm.version );
619 // Get the wm structure
620 if ( SDL_GetWMInfo( &wm ) == 1 )
623 // Check that we have the X11 wm
624 if ( wm.subsystem == SDL_SYSWM_X11 )
626 // Get the SDL window
627 Window window = wm.info.x11.window;
629 // Get the display session
630 Display *display = wm.info.x11.display;
632 // Get the window attributes
633 XWindowAttributes attr;
634 XGetWindowAttributes( display, window, &attr );
636 // Determine whether window has changed
637 changed = *width != attr.width || *height != attr.height;
639 // Return width and height
641 *height = attr.height;
649 /** Callback to allow override of the close method.
652 static void consumer_close( mlt_consumer parent )
654 // Get the actual object
655 consumer_sdl this = parent->child;
658 mlt_consumer_stop( parent );
660 // Now clean up the rest
661 mlt_consumer_close( parent );
663 // Finally clean up this