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
;
58 /** Forward references to static functions.
61 static void consumer_close( mlt_consumer parent
);
62 static void *consumer_thread( void * );
63 static int consumer_get_dimensions( int *width
, int *height
);
65 /** This is what will be called by the factory - anything can be passed in
66 via the argument, but keep it simple.
69 mlt_consumer
consumer_sdl_init( char *arg
)
71 // Create the consumer object
72 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
74 // If no malloc'd and consumer init ok
75 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
77 // Get the parent consumer object
78 mlt_consumer parent
= &this->parent
;
80 // We have stuff to clean up, so override the close method
81 parent
->close
= consumer_close
;
83 // get a handle on properties
84 mlt_service service
= mlt_consumer_service( parent
);
85 this->properties
= mlt_service_properties( service
);
87 // Set the default volume
88 mlt_properties_set_double( this->properties
, "volume", 1.0 );
90 // This is the initialisation of the consumer
92 pthread_mutex_init( &this->audio_mutex
, NULL
);
93 pthread_cond_init( &this->audio_cond
, NULL
);
95 // process actual param
96 if ( arg
== NULL
|| !strcmp( arg
, "PAL" ) )
101 else if ( !strcmp( arg
, "NTSC" ) )
106 else if ( sscanf( arg
, "%dx%d", &this->width
, &this->height
) != 2 )
112 // Default window size and aspect ratio
113 this->aspect_ratio
= 4.0 / 3.0;
114 this->window_width
= (int)( (float)this->height
* this->aspect_ratio
) + 1;
115 this->window_height
= this->height
;
117 // Create the the thread
118 pthread_create( &this->thread
, NULL
, consumer_thread
, this );
120 // Return the consumer produced
124 // malloc or consumer init failed
131 static int sdl_lock_display( )
133 SDL_Surface
*screen
= SDL_GetVideoSurface( );
134 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
137 static void sdl_unlock_display( )
139 SDL_Surface
*screen
= SDL_GetVideoSurface( );
140 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
141 SDL_UnlockSurface( screen
);
144 void sdl_fill_audio( void *udata
, uint8_t *stream
, int len
)
146 consumer_sdl
this = udata
;
149 float volume
= mlt_properties_get_double( this->properties
, "volume" );
151 pthread_mutex_lock( &this->audio_mutex
);
153 // Block until audio received
154 while ( this->running
&& len
> this->audio_avail
)
155 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
157 if ( this->audio_avail
>= len
)
159 // Place in the audio buffer
160 SDL_MixAudio( stream
, this->audio_buffer
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
162 // Remove len from the audio available
163 this->audio_avail
-= len
;
165 // Remove the samples
166 memmove( this->audio_buffer
, this->audio_buffer
+ len
, this->audio_avail
);
170 // Just to be safe, wipe the stream first
171 memset( stream
, 0, len
);
173 // Copy what we have into the stream
174 memcpy( stream
, this->audio_buffer
, this->audio_avail
);
177 SDL_MixAudio( stream
, stream
, len
, ( int )( ( float )SDL_MIX_MAXVOLUME
* volume
) );
180 this->audio_avail
= 0;
183 // We're definitely playing now
186 pthread_cond_broadcast( &this->audio_cond
);
187 pthread_mutex_unlock( &this->audio_mutex
);
190 static int consumer_play_audio( consumer_sdl
this, mlt_frame frame
, int init_audio
)
192 mlt_audio_format afmt
= mlt_audio_pcm
;
199 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
201 if ( init_audio
== 1 )
203 SDL_AudioSpec request
;
206 // specify audio format
207 memset( &request
, 0, sizeof( SDL_AudioSpec
) );
209 request
.freq
= frequency
;
210 request
.format
= AUDIO_S16
;
211 request
.channels
= channels
;
212 request
.samples
= 1024;
213 request
.callback
= sdl_fill_audio
;
214 request
.userdata
= (void *)this;
215 if ( SDL_OpenAudio( &request
, &got
) != 0 )
217 fprintf( stderr
, "SDL failed to open audio: %s\n", SDL_GetError() );
230 if ( init_audio
== 0 )
232 bytes
= ( samples
* channels
* 2 );
233 pthread_mutex_lock( &this->audio_mutex
);
234 while ( bytes
> ( sizeof( this->audio_buffer
) - this->audio_avail
) )
235 pthread_cond_wait( &this->audio_cond
, &this->audio_mutex
);
236 mlt_properties properties
= mlt_frame_properties( frame
);
237 if ( mlt_properties_get_double( properties
, "speed" ) == 1 )
238 memcpy( &this->audio_buffer
[ this->audio_avail
], pcm
, bytes
);
240 memset( &this->audio_buffer
[ this->audio_avail
], 0, bytes
);
241 this->audio_avail
+= bytes
;
242 pthread_cond_broadcast( &this->audio_cond
);
243 pthread_mutex_unlock( &this->audio_mutex
);
253 /** Threaded wrapper for pipe.
256 static void *consumer_thread( void *arg
)
259 consumer_sdl
this = arg
;
262 mlt_consumer consumer
= &this->parent
;
264 // Get the service assoicated to the consumer
265 mlt_service service
= mlt_consumer_service( consumer
);
267 // Get the properties of this consumer
268 mlt_properties properties
= this->properties
;
270 // Define a frame pointer
273 // internal intialization
274 int sdl_flags
= SDL_HWSURFACE
| SDL_DOUBLEBUF
| SDL_HWACCEL
| SDL_RESIZABLE
;
275 SDL_Surface
*sdl_screen
= NULL
;
276 SDL_Overlay
*sdl_overlay
= NULL
;
277 uint8_t *buffer
= NULL
;
280 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) < 0 )
282 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
286 // Loop until told not to
287 while( this->running
)
289 // Get a frame from the service (should never return anything other than 0)
290 if ( mlt_service_get_frame( service
, &frame
, 0 ) == 0 )
292 init_audio
= consumer_play_audio( this, frame
, init_audio
);
294 mlt_image_format vfmt
= mlt_image_yuv422
;
295 int width
= this->width
, height
= this->height
;
299 if ( this->count
== this->size
)
302 this->queue
= realloc( this->queue
, sizeof( mlt_frame
) * this->size
);
304 this->queue
[ this->count
++ ] = frame
;
308 // We're working on the oldest frame now
309 frame
= this->queue
[ 0 ];
311 // Shunt the frames in the queue down
313 for ( i
= 1; i
< this->count
; i
++ )
314 this->queue
[ i
- 1 ] = this->queue
[ i
];
317 // Get the image, width and height
318 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
320 if ( sdl_screen
!= NULL
)
324 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
326 while ( SDL_PollEvent( &event
) )
330 case SDL_VIDEORESIZE
:
331 this->window_width
= event
.resize
.w
;
332 this->window_height
= event
.resize
.h
;
337 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
338 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
339 if ( callback
!= NULL
&& producer
!= NULL
)
340 callback( producer
, SDL_GetKeyName(event
.key
.keysym
.sym
) );
348 if ( width
!= this->width
|| height
!= this->height
)
351 this->height
= height
;
355 if ( sdl_screen
== NULL
|| changed
)
357 double aspect_ratio
= mlt_frame_get_aspect_ratio( frame
);
359 if ( mlt_properties_get_double( properties
, "aspect_ratio" ) )
360 aspect_ratio
= mlt_properties_get_double( properties
, "aspect_ratio" );
362 int full_width
= height
* aspect_ratio
+ 1;
363 int full_height
= height
;
364 float scale_width
= (float)full_width
/ (float)this->window_width
;
365 float scale_height
= (float)full_height
/ (float)this->window_height
;
366 float display_aspect_ratio
= (float)this->window_width
/ (float)this->window_height
;
370 if ( aspect_ratio
== 1 )
372 rect
.w
= this->window_width
;
373 rect
.h
= this->window_height
;
375 else if ( this->window_width
< this->window_height
* aspect_ratio
)
377 rect
.w
= this->window_width
;
378 rect
.h
= this->window_width
/ aspect_ratio
;
382 rect
.w
= this->window_height
* aspect_ratio
;
383 rect
.h
= this->window_height
;
386 rect
.x
= ( this->window_width
- rect
.w
) / 2;
387 rect
.y
= ( this->window_height
- rect
.h
) / 2;
389 // Force an overlay recreation
390 if ( sdl_overlay
!= NULL
)
391 SDL_FreeYUVOverlay( sdl_overlay
);
393 // open SDL window with video overlay, if possible
394 sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, sdl_flags
);
396 if ( sdl_screen
!= NULL
)
398 SDL_SetClipRect( sdl_screen
, &rect
);
401 sdl_overlay
= SDL_CreateYUVOverlay( this->width
- (this->width
% 4), this->height
- (this->height
% 2 ), SDL_YUY2_OVERLAY
, sdl_screen
);
402 sdl_unlock_display();
406 if ( sdl_screen
!= NULL
&& sdl_overlay
!= NULL
)
408 buffer
= sdl_overlay
->pixels
[ 0 ];
409 if ( SDL_LockYUVOverlay( sdl_overlay
) >= 0 )
411 mlt_resize_yuv422( buffer
, this->width
- (this->width
% 4 ), this->height
- (this->height
% 2 ), image
, width
, height
);
412 SDL_UnlockYUVOverlay( sdl_overlay
);
413 SDL_DisplayYUVOverlay( sdl_overlay
, &sdl_screen
->clip_rect
);
424 mlt_frame_close( frame
);
427 mlt_frame_get_image( this->queue
[ this->count
- 1 ], &image
, &vfmt
, &width
, &height
, 0 );
436 if ( init_audio
== 0 )
438 if ( sdl_overlay
!= NULL
)
439 SDL_FreeYUVOverlay( sdl_overlay
);
445 static int consumer_get_dimensions( int *width
, int *height
)
449 // SDL windows manager structure
452 // Specify the SDL Version
453 SDL_VERSION( &wm
.version
);
455 // Get the wm structure
456 if ( SDL_GetWMInfo( &wm
) == 1 )
458 // Check that we have the X11 wm
459 if ( wm
.subsystem
== SDL_SYSWM_X11
)
461 // Get the SDL window
462 Window window
= wm
.info
.x11
.window
;
464 // Get the display session
465 Display
*display
= wm
.info
.x11
.display
;
467 // Get the window attributes
468 XWindowAttributes attr
;
469 XGetWindowAttributes( display
, window
, &attr
);
471 // Determine whether window has changed
472 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
474 // Return width and height
476 *height
= attr
.height
;
483 /** Callback to allow override of the close method.
486 static void consumer_close( mlt_consumer parent
)
488 // Get the actual object
489 consumer_sdl
this = parent
->child
;
491 // Kill the thread and clean up
494 pthread_mutex_lock( &this->audio_mutex
);
495 pthread_cond_broadcast( &this->audio_cond
);
496 pthread_mutex_unlock( &this->audio_mutex
);
498 pthread_join( this->thread
, NULL
);
499 pthread_mutex_destroy( &this->audio_mutex
);
500 pthread_cond_destroy( &this->audio_cond
);
502 // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
503 parent
->close
= NULL
;
504 mlt_consumer_close( parent
);
506 // Finally clean up this