framework: remove global profile, rather share one mlt_profile across a service netwo...
[melted] / src / modules / sdl / consumer_sdl.c
1 /*
2 * consumer_sdl.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
5 *
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.
10 *
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.
15 *
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
19 */
20
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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pthread.h>
30 #include <SDL/SDL.h>
31 #include <SDL/SDL_syswm.h>
32 #include <sys/time.h>
33
34 /** This classes definition.
35 */
36
37 typedef struct consumer_sdl_s *consumer_sdl;
38
39 struct consumer_sdl_s
40 {
41 struct mlt_consumer_s parent;
42 mlt_properties properties;
43 mlt_deque queue;
44 pthread_t thread;
45 int joined;
46 int running;
47 uint8_t audio_buffer[ 4096 * 10 ];
48 int audio_avail;
49 pthread_mutex_t audio_mutex;
50 pthread_cond_t audio_cond;
51 pthread_mutex_t video_mutex;
52 pthread_cond_t video_cond;
53 int window_width;
54 int window_height;
55 int previous_width;
56 int previous_height;
57 int width;
58 int height;
59 int playing;
60 int sdl_flags;
61 SDL_Surface *sdl_screen;
62 SDL_Overlay *sdl_overlay;
63 SDL_Rect rect;
64 uint8_t *buffer;
65 int bpp;
66 int filtered;
67 };
68
69 /** Forward references to static functions.
70 */
71
72 static int consumer_start( mlt_consumer parent );
73 static int consumer_stop( mlt_consumer parent );
74 static int consumer_is_stopped( mlt_consumer parent );
75 static void consumer_close( mlt_consumer parent );
76 static void *consumer_thread( void * );
77 static int consumer_get_dimensions( int *width, int *height );
78 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
79
80 /** This is what will be called by the factory - anything can be passed in
81 via the argument, but keep it simple.
82 */
83
84 mlt_consumer consumer_sdl_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
85 {
86 // Create the consumer object
87 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
88
89 // If no malloc'd and consumer init ok
90 if ( this != NULL && mlt_consumer_init( &this->parent, this, profile ) == 0 )
91 {
92 // Create the queue
93 this->queue = mlt_deque_init( );
94
95 // Get the parent consumer object
96 mlt_consumer parent = &this->parent;
97
98 // We have stuff to clean up, so override the close method
99 parent->close = consumer_close;
100
101 // get a handle on properties
102 mlt_service service = MLT_CONSUMER_SERVICE( parent );
103 this->properties = MLT_SERVICE_PROPERTIES( service );
104
105 // Set the default volume
106 mlt_properties_set_double( this->properties, "volume", 1.0 );
107
108 // This is the initialisation of the consumer
109 pthread_mutex_init( &this->audio_mutex, NULL );
110 pthread_cond_init( &this->audio_cond, NULL);
111 pthread_mutex_init( &this->video_mutex, NULL );
112 pthread_cond_init( &this->video_cond, NULL);
113
114 // Default scaler (for now we'll use nearest)
115 mlt_properties_set( this->properties, "rescale", "nearest" );
116
117 // Default buffer for low latency
118 mlt_properties_set_int( this->properties, "buffer", 1 );
119
120 // Default progressive true
121 mlt_properties_set_int( this->properties, "progressive", 0 );
122
123 // Default audio buffer
124 mlt_properties_set_int( this->properties, "audio_buffer", 512 );
125
126 // Ensure we don't join on a non-running object
127 this->joined = 1;
128
129 // process actual param
130 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
131 {
132 this->width = mlt_properties_get_int( this->properties, "width" );
133 this->height = mlt_properties_get_int( this->properties, "height" );
134 }
135
136 // Set the sdl flags
137 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
138
139 // Allow thread to be started/stopped
140 parent->start = consumer_start;
141 parent->stop = consumer_stop;
142 parent->is_stopped = consumer_is_stopped;
143
144 // Register specific events
145 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
146
147 // Return the consumer produced
148 return parent;
149 }
150
151 // malloc or consumer init failed
152 free( this );
153
154 // Indicate failure
155 return NULL;
156 }
157
158 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
159 {
160 if ( listener != NULL )
161 listener( owner, this, ( SDL_Event * )args[ 0 ] );
162 }
163
164 int consumer_start( mlt_consumer parent )
165 {
166 consumer_sdl this = parent->child;
167
168 if ( !this->running )
169 {
170 int video_off = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "video_off" );
171 int preview_off = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "preview_off" );
172 int display_off = video_off | preview_off;
173 int audio_off = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "audio_off" );
174 int sdl_started = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" );
175
176 consumer_stop( parent );
177
178 this->running = 1;
179 this->joined = 0;
180
181 if ( mlt_properties_get_int( this->properties, "width" ) > 0 )
182 this->width = mlt_properties_get_int( this->properties, "width" );
183 if ( mlt_properties_get_int( this->properties, "height" ) > 0 )
184 this->height = mlt_properties_get_int( this->properties, "height" );
185
186 this->bpp = mlt_properties_get_int( this->properties, "bpp" );
187
188 // Attach a colour space converter
189 if ( preview_off && !this->filtered )
190 {
191 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( parent ) );
192 mlt_filter filter = mlt_factory_filter( profile, "avcolour_space", NULL );
193 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "forced", mlt_image_yuv422 );
194 mlt_service_attach( MLT_CONSUMER_SERVICE( parent ), filter );
195 mlt_filter_close( filter );
196 this->filtered = 1;
197 }
198
199 if ( sdl_started == 0 && display_off == 0 )
200 {
201 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
202 {
203 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
204 return -1;
205 }
206
207 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
208 SDL_EnableUNICODE( 1 );
209 }
210 else if ( display_off == 0 )
211 {
212 this->sdl_screen = SDL_GetVideoSurface( );
213 }
214
215 if ( audio_off == 0 )
216 SDL_InitSubSystem( SDL_INIT_AUDIO );
217
218 // Default window size
219 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
220 this->window_width = ( double )this->height * display_ratio;
221 this->window_height = this->height;
222
223 if ( this->sdl_screen == NULL && display_off == 0 )
224 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
225
226 pthread_create( &this->thread, NULL, consumer_thread, this );
227 }
228
229 return 0;
230 }
231
232 int consumer_stop( mlt_consumer parent )
233 {
234 // Get the actual object
235 consumer_sdl this = parent->child;
236
237 if ( this->joined == 0 )
238 {
239 // Kill the thread and clean up
240 this->joined = 1;
241 this->running = 0;
242 pthread_join( this->thread, NULL );
243
244 // internal cleanup
245 if ( this->sdl_overlay != NULL )
246 SDL_FreeYUVOverlay( this->sdl_overlay );
247 this->sdl_overlay = NULL;
248
249 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "audio_off" ) )
250 {
251 pthread_mutex_lock( &this->audio_mutex );
252 pthread_cond_broadcast( &this->audio_cond );
253 pthread_mutex_unlock( &this->audio_mutex );
254 SDL_QuitSubSystem( SDL_INIT_AUDIO );
255 }
256
257 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" ) == 0 )
258 SDL_Quit( );
259
260 this->sdl_screen = NULL;
261 }
262
263 return 0;
264 }
265
266 int consumer_is_stopped( mlt_consumer parent )
267 {
268 consumer_sdl this = parent->child;
269 return !this->running;
270 }
271
272 static int sdl_lock_display( )
273 {
274 SDL_Surface *screen = SDL_GetVideoSurface( );
275 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
276 }
277
278 static void sdl_unlock_display( )
279 {
280 SDL_Surface *screen = SDL_GetVideoSurface( );
281 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
282 SDL_UnlockSurface( screen );
283 }
284
285 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
286 {
287 consumer_sdl this = udata;
288
289 // Get the volume
290 double volume = mlt_properties_get_double( this->properties, "volume" );
291
292 pthread_mutex_lock( &this->audio_mutex );
293
294 // Block until audio received
295 while ( this->running && len > this->audio_avail )
296 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
297
298 if ( this->audio_avail >= len )
299 {
300 // Place in the audio buffer
301 if ( volume != 1.0 )
302 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
303 else
304 memcpy( stream, this->audio_buffer, len );
305
306 // Remove len from the audio available
307 this->audio_avail -= len;
308
309 // Remove the samples
310 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
311 }
312 else
313 {
314 // Just to be safe, wipe the stream first
315 memset( stream, 0, len );
316
317 // Mix the audio
318 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
319
320 // No audio left
321 this->audio_avail = 0;
322 }
323
324 // We're definitely playing now
325 this->playing = 1;
326
327 pthread_cond_broadcast( &this->audio_cond );
328 pthread_mutex_unlock( &this->audio_mutex );
329 }
330
331 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
332 {
333 // Get the properties of this consumer
334 mlt_properties properties = this->properties;
335 mlt_audio_format afmt = mlt_audio_pcm;
336
337 // Set the preferred params of the test card signal
338 int channels = mlt_properties_get_int( properties, "channels" );
339 int frequency = mlt_properties_get_int( properties, "frequency" );
340 static int counter = 0;
341
342 int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
343
344 int16_t *pcm;
345 int bytes;
346
347 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
348 *duration = ( ( samples * 1000 ) / frequency );
349
350 if ( mlt_properties_get_int( properties, "audio_off" ) )
351 {
352 this->playing = 1;
353 init_audio = 1;
354 return init_audio;
355 }
356
357 if ( init_audio == 1 )
358 {
359 SDL_AudioSpec request;
360 SDL_AudioSpec got;
361
362 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
363
364 // specify audio format
365 memset( &request, 0, sizeof( SDL_AudioSpec ) );
366 this->playing = 0;
367 request.freq = frequency;
368 request.format = AUDIO_S16SYS;
369 request.channels = channels;
370 request.samples = audio_buffer;
371 request.callback = sdl_fill_audio;
372 request.userdata = (void *)this;
373 if ( SDL_OpenAudio( &request, &got ) != 0 )
374 {
375 fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
376 init_audio = 2;
377 }
378 else if ( got.size != 0 )
379 {
380 SDL_PauseAudio( 0 );
381 init_audio = 0;
382 }
383 }
384
385 if ( init_audio == 0 )
386 {
387 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
388 bytes = ( samples * channels * 2 );
389 pthread_mutex_lock( &this->audio_mutex );
390 while ( this->running && bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
391 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
392 if ( this->running )
393 {
394 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
395 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
396 else
397 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
398 this->audio_avail += bytes;
399 }
400 pthread_cond_broadcast( &this->audio_cond );
401 pthread_mutex_unlock( &this->audio_mutex );
402 }
403 else
404 {
405 this->playing = 1;
406 }
407
408 return init_audio;
409 }
410
411 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
412 {
413 // Get the properties of this consumer
414 mlt_properties properties = this->properties;
415
416 mlt_image_format vfmt = mlt_image_yuv422;
417 int width = this->width, height = this->height;
418 uint8_t *image;
419 int changed = 0;
420
421 int video_off = mlt_properties_get_int( properties, "video_off" );
422 int preview_off = mlt_properties_get_int( properties, "preview_off" );
423 mlt_image_format preview_format = mlt_properties_get_int( properties, "preview_format" );
424 int display_off = video_off | preview_off;
425
426 if ( this->running && display_off == 0 )
427 {
428 // Get the image, width and height
429 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
430 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "format", vfmt );
431 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
432
433 // Handle events
434 if ( this->sdl_screen != NULL )
435 {
436 SDL_Event event;
437
438 sdl_lock_display( );
439 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
440 sdl_unlock_display( );
441
442 while ( SDL_PollEvent( &event ) )
443 {
444 mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
445
446 switch( event.type )
447 {
448 case SDL_VIDEORESIZE:
449 this->window_width = event.resize.w;
450 this->window_height = event.resize.h;
451 changed = 1;
452 break;
453 case SDL_QUIT:
454 this->running = 0;
455 break;
456 case SDL_KEYDOWN:
457 {
458 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
459 char keyboard[ 2 ] = " ";
460 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
461 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
462 {
463 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
464 callback( producer, keyboard );
465 }
466 }
467 break;
468 }
469 }
470 }
471
472 sdl_lock_display();
473
474 if ( width != this->width || height != this->height )
475 {
476 if ( this->sdl_overlay != NULL )
477 SDL_FreeYUVOverlay( this->sdl_overlay );
478 this->sdl_overlay = NULL;
479 }
480
481 if ( this->running && ( this->sdl_screen == NULL || changed ) )
482 {
483 // Force an overlay recreation
484 if ( this->sdl_overlay != NULL )
485 SDL_FreeYUVOverlay( this->sdl_overlay );
486 this->sdl_overlay = NULL;
487
488 // open SDL window with video overlay, if possible
489 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
490 if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
491 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
492 }
493
494 if ( this->running )
495 {
496 // Determine window's new display aspect ratio
497 double this_aspect = ( double )this->window_width / this->window_height;
498
499 // Get the display aspect ratio
500 double display_ratio = mlt_properties_get_double( properties, "display_ratio" );
501
502 // Determine frame's display aspect ratio
503 double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
504
505 // Store the width and height received
506 this->width = width;
507 this->height = height;
508
509 // If using hardware scaler
510 if ( mlt_properties_get( properties, "rescale" ) != NULL &&
511 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
512 {
513 // Use hardware scaler to normalise display aspect ratio
514 this->rect.w = frame_aspect / this_aspect * this->window_width;
515 this->rect.h = this->window_height;
516 if ( this->rect.w > this->window_width )
517 {
518 this->rect.w = this->window_width;
519 this->rect.h = this_aspect / frame_aspect * this->window_height;
520 }
521 }
522 // Special case optimisation to negate odd effect of sample aspect ratio
523 // not corresponding exactly with image resolution.
524 else if ( (int)( this_aspect * 1000 ) == (int)( display_ratio * 1000 ) )
525 {
526 this->rect.w = this->window_width;
527 this->rect.h = this->window_height;
528 }
529 // Use hardware scaler to normalise sample aspect ratio
530 else if ( this->window_height * display_ratio > this->window_width )
531 {
532 this->rect.w = this->window_width;
533 this->rect.h = this->window_width / display_ratio;
534 }
535 else
536 {
537 this->rect.w = this->window_height * display_ratio;
538 this->rect.h = this->window_height;
539 }
540
541 this->rect.x = ( this->window_width - this->rect.w ) / 2;
542 this->rect.y = ( this->window_height - this->rect.h ) / 2;
543 this->rect.x -= this->rect.x % 2;
544
545 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
546 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
547 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
548 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
549
550 SDL_SetClipRect( this->sdl_screen, &this->rect );
551 }
552
553 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay == NULL )
554 {
555 SDL_SetClipRect( this->sdl_screen, &this->rect );
556 this->sdl_overlay = SDL_CreateYUVOverlay( width, height, SDL_YUY2_OVERLAY, this->sdl_screen );
557 }
558
559 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay != NULL )
560 {
561 this->buffer = this->sdl_overlay->pixels[ 0 ];
562 if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
563 {
564 if ( image != NULL )
565 memcpy( this->buffer, image, width * height * 2 );
566 SDL_UnlockYUVOverlay( this->sdl_overlay );
567 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
568 }
569 }
570
571 sdl_unlock_display();
572 }
573 else if ( this->running )
574 {
575 vfmt = preview_format == mlt_image_none ? mlt_image_rgb24a : preview_format;
576 if ( !video_off )
577 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
578 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "format", vfmt );
579 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
580 }
581
582 return 0;
583 }
584
585 static void *video_thread( void *arg )
586 {
587 // Identify the arg
588 consumer_sdl this = arg;
589
590 // Obtain time of thread start
591 struct timeval now;
592 int64_t start = 0;
593 int64_t elapsed = 0;
594 struct timespec tm;
595 mlt_frame next = NULL;
596 mlt_properties properties = NULL;
597 double speed = 0;
598
599 // Get real time flag
600 int real_time = mlt_properties_get_int( this->properties, "real_time" );
601
602 // Get the current time
603 gettimeofday( &now, NULL );
604
605 // Determine start time
606 start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
607
608 while ( this->running )
609 {
610 // Pop the next frame
611 pthread_mutex_lock( &this->video_mutex );
612 next = mlt_deque_pop_front( this->queue );
613 while ( next == NULL && this->running )
614 {
615 pthread_cond_wait( &this->video_cond, &this->video_mutex );
616 next = mlt_deque_pop_front( this->queue );
617 }
618 pthread_mutex_unlock( &this->video_mutex );
619
620 if ( !this->running || next == NULL ) break;
621
622 // Get the properties
623 properties = MLT_FRAME_PROPERTIES( next );
624
625 // Get the speed of the frame
626 speed = mlt_properties_get_double( properties, "_speed" );
627
628 // Get the current time
629 gettimeofday( &now, NULL );
630
631 // Get the elapsed time
632 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
633
634 // See if we have to delay the display of the current frame
635 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && this->running )
636 {
637 // Obtain the scheduled playout time
638 int64_t scheduled = mlt_properties_get_int( properties, "playtime" );
639
640 // Determine the difference between the elapsed time and the scheduled playout time
641 int64_t difference = scheduled - elapsed;
642
643 // Smooth playback a bit
644 if ( real_time && ( difference > 20000 && speed == 1.0 ) )
645 {
646 tm.tv_sec = difference / 1000000;
647 tm.tv_nsec = ( difference % 1000000 ) * 500;
648 nanosleep( &tm, NULL );
649 }
650
651 // Show current frame if not too old
652 if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( this->queue ) < 2 ) )
653 consumer_play_video( this, next );
654
655 // If the queue is empty, recalculate start to allow build up again
656 if ( real_time && ( mlt_deque_count( this->queue ) == 0 && speed == 1.0 ) )
657 {
658 gettimeofday( &now, NULL );
659 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
660 }
661 }
662
663 // This frame can now be closed
664 mlt_frame_close( next );
665 next = NULL;
666 }
667
668 if ( next != NULL )
669 mlt_frame_close( next );
670
671 mlt_consumer_stopped( &this->parent );
672
673 return NULL;
674 }
675
676 /** Threaded wrapper for pipe.
677 */
678
679 static void *consumer_thread( void *arg )
680 {
681 // Identify the arg
682 consumer_sdl this = arg;
683
684 // Get the consumer
685 mlt_consumer consumer = &this->parent;
686
687 // Convenience functionality
688 int terminate_on_pause = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "terminate_on_pause" );
689 int terminated = 0;
690
691 // Video thread
692 pthread_t thread;
693
694 // internal intialization
695 int init_audio = 1;
696 int init_video = 1;
697 mlt_frame frame = NULL;
698 mlt_properties properties = NULL;
699 int duration = 0;
700 int64_t playtime = 0;
701 struct timespec tm = { 0, 100000 };
702
703 // Loop until told not to
704 while( !terminated && this->running )
705 {
706 // Get a frame from the attached producer
707 frame = mlt_consumer_rt_frame( consumer );
708
709 // Check for termination
710 if ( terminate_on_pause && frame != NULL )
711 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
712
713 // Ensure that we have a frame
714 if ( frame != NULL )
715 {
716 // Get the frame properties
717 properties = MLT_FRAME_PROPERTIES( frame );
718
719 // Play audio
720 init_audio = consumer_play_audio( this, frame, init_audio, &duration );
721
722 // Determine the start time now
723 if ( this->playing && init_video )
724 {
725 // Create the video thread
726 pthread_create( &thread, NULL, video_thread, this );
727
728 // Video doesn't need to be initialised any more
729 init_video = 0;
730 }
731
732 // Set playtime for this frame
733 mlt_properties_set_int( properties, "playtime", playtime );
734
735 while ( this->running && mlt_deque_count( this->queue ) > 15 )
736 nanosleep( &tm, NULL );
737
738 // Push this frame to the back of the queue
739 pthread_mutex_lock( &this->video_mutex );
740 mlt_deque_push_back( this->queue, frame );
741 pthread_cond_broadcast( &this->video_cond );
742 pthread_mutex_unlock( &this->video_mutex );
743
744 // Calculate the next playtime
745 playtime += ( duration * 1000 );
746 }
747 }
748
749 this->running = 0;
750
751 // Kill the video thread
752 if ( init_video == 0 )
753 {
754 pthread_mutex_lock( &this->video_mutex );
755 pthread_cond_broadcast( &this->video_cond );
756 pthread_mutex_unlock( &this->video_mutex );
757 pthread_join( thread, NULL );
758 }
759
760 while( mlt_deque_count( this->queue ) )
761 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
762
763 this->sdl_screen = NULL;
764 this->audio_avail = 0;
765
766 return NULL;
767 }
768
769 static int consumer_get_dimensions( int *width, int *height )
770 {
771 int changed = 0;
772
773 // SDL windows manager structure
774 SDL_SysWMinfo wm;
775
776 // Specify the SDL Version
777 SDL_VERSION( &wm.version );
778
779 // Lock the display
780 //sdl_lock_display();
781
782 #ifndef __DARWIN__
783 // Get the wm structure
784 if ( SDL_GetWMInfo( &wm ) == 1 )
785 {
786 // Check that we have the X11 wm
787 if ( wm.subsystem == SDL_SYSWM_X11 )
788 {
789 // Get the SDL window
790 Window window = wm.info.x11.window;
791
792 // Get the display session
793 Display *display = wm.info.x11.display;
794
795 // Get the window attributes
796 XWindowAttributes attr;
797 XGetWindowAttributes( display, window, &attr );
798
799 // Determine whether window has changed
800 changed = *width != attr.width || *height != attr.height;
801
802 // Return width and height
803 *width = attr.width;
804 *height = attr.height;
805 }
806 }
807 #endif
808
809 // Unlock the display
810 //sdl_unlock_display();
811
812 return changed;
813 }
814
815 /** Callback to allow override of the close method.
816 */
817
818 static void consumer_close( mlt_consumer parent )
819 {
820 // Get the actual object
821 consumer_sdl this = parent->child;
822
823 // Stop the consumer
824 ///mlt_consumer_stop( parent );
825
826 // Now clean up the rest
827 mlt_consumer_close( parent );
828
829 // Close the queue
830 mlt_deque_close( this->queue );
831
832 // Destroy mutexes
833 pthread_mutex_destroy( &this->audio_mutex );
834 pthread_cond_destroy( &this->audio_cond );
835
836 // Finally clean up this
837 free( this );
838 }