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