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>
28 #include <SDL/SDL_syswm.h>
30 /** This classes definition.
33 typedef struct consumer_sdl_s
*consumer_sdl
;
37 struct mlt_consumer_s parent
;
38 mlt_properties properties
;
43 uint8_t audio_buffer
[ 4096 * 3 ];
45 pthread_mutex_t audio_mutex
;
46 pthread_cond_t audio_cond
;
57 SDL_Surface
*sdl_screen
;
58 SDL_Overlay
*sdl_overlay
;
62 /** Forward references to static functions.
65 static void consumer_close( mlt_consumer parent
);
66 static void *consumer_thread( void * );
67 static int consumer_get_dimensions( int *width
, int *height
);
69 /** This is what will be called by the factory - anything can be passed in
70 via the argument, but keep it simple.
73 mlt_consumer
consumer_sdl_init( char *arg
)
75 // Create the consumer object
76 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
78 // If no malloc'd and consumer init ok
79 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
81 // Get the parent consumer object
82 mlt_consumer parent
= &this->parent
;
84 // We have stuff to clean up, so override the close method
85 parent
->close
= consumer_close
;
87 // get a handle on properties
88 mlt_service service
= mlt_consumer_service( parent
);
89 this->properties
= mlt_service_properties( service
);
91 // Set the default volume
92 mlt_properties_set_double( this->properties
, "volume", 1.0 );
94 // This is the initialisation of the consumer
96 pthread_mutex_init( &this->audio_mutex
, NULL
);
97 pthread_cond_init( &this->audio_cond
, NULL
);
99 // process actual param
100 if ( arg
== NULL
|| !strcmp( arg
, "PAL" ) )
105 else if ( !strcmp( arg
, "NTSC" ) )
110 else if ( sscanf( arg
, "%dx%d", &this->width
, &this->height
) != 2 )
116 // Default window size and aspect ratio
117 this->aspect_ratio
= 4.0 / 3.0;
118 this->window_width
= (int)( (float)this->height
* this->aspect_ratio
) + 1;
119 this->window_height
= this->height
;
122 this->sdl_flags
= SDL_HWSURFACE
| SDL_DOUBLEBUF
| SDL_HWACCEL
| SDL_RESIZABLE
;
124 // Create the the thread
125 pthread_create( &this->thread
, NULL
, consumer_thread
, this );
127 // Return the consumer produced
131 // malloc or consumer init failed
138 static int sdl_lock_display( )
140 SDL_Surface
*screen
= SDL_GetVideoSurface( );
141 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
144 static void sdl_unlock_display( )
146 SDL_Surface
*screen
= SDL_GetVideoSurface( );
147 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
148 SDL_UnlockSurface( screen
);
151 static void sdl_fill_audio( void *udata
, uint8_t *stream
, int len
)
153 consumer_sdl
this = udata
;
156 float volume
= mlt_properties_get_double( this->properties
, "volume" );
158 pthread_mutex_lock( &this->audio_mutex
);
160 // Block until audio received
161 while ( this->running
&& len
> this->audio_avail
)
162 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
164 if ( this->audio_avail
>= len
)
166 // Place in the audio buffer
167 SDL_MixAudio( stream
, this->audio_buffer
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
169 // Remove len from the audio available
170 this->audio_avail
-= len
;
172 // Remove the samples
173 memmove( this->audio_buffer
, this->audio_buffer
+ len
, this->audio_avail
);
177 // Just to be safe, wipe the stream first
178 memset( stream
, 0, len
);
180 // Copy what we have into the stream
181 memcpy( stream
, this->audio_buffer
, this->audio_avail
);
184 SDL_MixAudio( stream
, stream
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
187 this->audio_avail
= 0;
190 // We're definitely playing now
193 pthread_cond_broadcast( &this->audio_cond
);
194 pthread_mutex_unlock( &this->audio_mutex
);
197 static int consumer_play_audio( consumer_sdl
this, mlt_frame frame
, int init_audio
)
199 // Get the properties of this consumer
200 mlt_properties properties
= this->properties
;
201 mlt_audio_format afmt
= mlt_audio_pcm
;
208 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
210 if ( mlt_properties_get_int( properties
, "audio_off" ) )
216 if ( init_audio
== 1 )
218 SDL_AudioSpec request
;
221 // specify audio format
222 memset( &request
, 0, sizeof( SDL_AudioSpec
) );
224 request
.freq
= frequency
;
225 request
.format
= AUDIO_S16
;
226 request
.channels
= channels
;
227 request
.samples
= 1024;
228 request
.callback
= sdl_fill_audio
;
229 request
.userdata
= (void *)this;
230 if ( SDL_OpenAudio( &request
, &got
) != 0 )
232 fprintf( stderr
, "SDL failed to open audio: %s\n", SDL_GetError() );
235 else if ( got
.size
!= 0 )
242 if ( init_audio
== 0 )
244 bytes
= ( samples
* channels
* 2 );
245 pthread_mutex_lock( &this->audio_mutex
);
246 while ( bytes
> ( sizeof( this->audio_buffer
) - this->audio_avail
) )
247 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
248 mlt_properties properties
= mlt_frame_properties( frame
);
249 if ( mlt_properties_get_double( properties
, "speed" ) == 1 )
250 memcpy( &this->audio_buffer
[ this->audio_avail
], pcm
, bytes
);
252 memset( &this->audio_buffer
[ this->audio_avail
], 0, bytes
);
253 this->audio_avail
+= bytes
;
254 pthread_cond_broadcast( &this->audio_cond
);
255 pthread_mutex_unlock( &this->audio_mutex
);
265 static int consumer_play_video( consumer_sdl
this, mlt_frame frame
)
267 // Get the properties of this consumer
268 mlt_properties properties
= this->properties
;
270 mlt_image_format vfmt
= mlt_image_yuv422
;
271 int width
= this->width
, height
= this->height
;
275 if ( mlt_properties_get_int( properties
, "video_off" ) )
277 mlt_frame_close( frame
);
281 if ( this->count
== this->size
)
284 this->queue
= realloc( this->queue
, sizeof( mlt_frame
) * this->size
);
286 this->queue
[ this->count
++ ] = frame
;
290 // We're working on the oldest frame now
291 frame
= this->queue
[ 0 ];
293 // Shunt the frames in the queue down
295 for ( i
= 1; i
< this->count
; i
++ )
296 this->queue
[ i
- 1 ] = this->queue
[ i
];
299 // Get the image, width and height
300 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
302 if ( this->sdl_screen
!= NULL
)
306 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
308 while ( SDL_PollEvent( &event
) )
312 case SDL_VIDEORESIZE
:
313 this->window_width
= event
.resize
.w
;
314 this->window_height
= event
.resize
.h
;
319 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
320 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
321 if ( callback
!= NULL
&& producer
!= NULL
)
322 callback( producer
, SDL_GetKeyName(event
.key
.keysym
.sym
) );
330 if ( width
!= this->width
|| height
!= this->height
)
333 this->height
= height
;
337 if ( this->sdl_screen
== NULL
|| changed
)
339 double aspect_ratio
= mlt_frame_get_aspect_ratio( frame
);
340 float display_aspect_ratio
= (float)width
/ (float)height
;
343 if ( mlt_properties_get_double( properties
, "aspect_ratio" ) )
344 aspect_ratio
= mlt_properties_get_double( properties
, "aspect_ratio" );
346 if ( aspect_ratio
== 1 )
348 rect
.w
= this->window_width
;
349 rect
.h
= this->window_height
;
351 else if ( this->window_width
< this->window_height
* aspect_ratio
)
353 rect
.w
= this->window_width
;
354 rect
.h
= this->window_width
/ aspect_ratio
;
358 rect
.w
= this->window_height
* aspect_ratio
;
359 rect
.h
= this->window_height
;
362 if ( mlt_properties_get_int( properties
, "scale_overlay" ) )
364 if ( ( float )rect
.w
* display_aspect_ratio
< this->window_width
)
365 rect
.w
= ( int )( ( float )rect
.w
* display_aspect_ratio
);
366 else if ( ( float )rect
.h
* display_aspect_ratio
< this->window_height
)
367 rect
.h
= ( int )( ( float )rect
.h
* display_aspect_ratio
);
370 rect
.x
= ( this->window_width
- rect
.w
) / 2;
371 rect
.y
= ( this->window_height
- rect
.h
) / 2;
373 // Force an overlay recreation
374 if ( this->sdl_overlay
!= NULL
)
375 SDL_FreeYUVOverlay( this->sdl_overlay
);
377 // open SDL window with video overlay, if possible
378 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
380 if ( this->sdl_screen
!= NULL
)
382 SDL_SetClipRect( this->sdl_screen
, &rect
);
385 this->sdl_overlay
= SDL_CreateYUVOverlay( this->width
- (this->width
% 4), this->height
- (this->height
% 2 ), SDL_YUY2_OVERLAY
, this->sdl_screen
);
386 sdl_unlock_display();
390 if ( this->sdl_screen
!= NULL
&& this->sdl_overlay
!= NULL
)
392 this->buffer
= this->sdl_overlay
->pixels
[ 0 ];
393 if ( SDL_LockYUVOverlay( this->sdl_overlay
) >= 0 )
395 mlt_resize_yuv422( this->buffer
, this->width
- (this->width
% 4 ), this->height
- (this->height
% 2 ), image
, width
, height
);
396 SDL_UnlockYUVOverlay( this->sdl_overlay
);
397 SDL_DisplayYUVOverlay( this->sdl_overlay
, &this->sdl_screen
->clip_rect
);
408 mlt_frame_close( frame
);
411 mlt_frame_get_image( this->queue
[ this->count
- 1 ], &image
, &vfmt
, &width
, &height
, 0 );
416 /** Threaded wrapper for pipe.
419 static void *consumer_thread( void *arg
)
422 consumer_sdl
this = arg
;
425 mlt_consumer consumer
= &this->parent
;
427 // Get the service assoicated to the consumer
428 mlt_service service
= mlt_consumer_service( consumer
);
430 // Define a frame pointer
433 // internal intialization
436 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) < 0 )
438 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
442 // Loop until told not to
443 while( this->running
)
445 // Get a frame from the service (should never return anything other than 0)
446 if ( mlt_service_get_frame( service
, &frame
, 0 ) == 0 )
448 init_audio
= consumer_play_audio( this, frame
, init_audio
);
449 consumer_play_video( this, frame
);
454 if ( init_audio
== 0 )
456 if ( this->sdl_overlay
!= NULL
)
457 SDL_FreeYUVOverlay( this->sdl_overlay
);
463 static int consumer_get_dimensions( int *width
, int *height
)
467 // SDL windows manager structure
470 // Specify the SDL Version
471 SDL_VERSION( &wm
.version
);
473 // Get the wm structure
474 if ( SDL_GetWMInfo( &wm
) == 1 )
476 // Check that we have the X11 wm
477 if ( wm
.subsystem
== SDL_SYSWM_X11
)
479 // Get the SDL window
480 Window window
= wm
.info
.x11
.window
;
482 // Get the display session
483 Display
*display
= wm
.info
.x11
.display
;
485 // Get the window attributes
486 XWindowAttributes attr
;
487 XGetWindowAttributes( display
, window
, &attr
);
489 // Determine whether window has changed
490 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
492 // Return width and height
494 *height
= attr
.height
;
501 /** Callback to allow override of the close method.
504 static void consumer_close( mlt_consumer parent
)
506 // Get the actual object
507 consumer_sdl
this = parent
->child
;
509 // Kill the thread and clean up
512 pthread_mutex_lock( &this->audio_mutex
);
513 pthread_cond_broadcast( &this->audio_cond
);
514 pthread_mutex_unlock( &this->audio_mutex
);
516 pthread_join( this->thread
, NULL
);
517 pthread_mutex_destroy( &this->audio_mutex
);
518 pthread_cond_destroy( &this->audio_cond
);
520 // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
521 parent
->close
= NULL
;
522 mlt_consumer_close( parent
);
524 // Finally clean up this