]> git.sesse.net Git - mlt/blobdiff - src/modules/sdl/consumer_sdl_preview.c
Add audio_off and frequency to properties passed to sdl consumer.
[mlt] / src / modules / sdl / consumer_sdl_preview.c
index 5a0b2d9700d4b437b4ec52db55af632a14fbc314..ced12060704fc5671c631a54a3937e2c0108c0b8 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * consumer_sdl_preview.c -- A Simple DirectMedia Layer consumer
- * Copyright (C) 2004-2005 Ushodaya Enterprises Limited
+ * Copyright (C) 2004-2005, 2010 Ushodaya Enterprises Limited
  * Author: Charles Yates
  *
  * This library is free software; you can redistribute it and/or
 #include <framework/mlt_frame.h>
 #include <framework/mlt_factory.h>
 #include <framework/mlt_producer.h>
+#include <framework/mlt_log.h>
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
-#include <SDL/SDL.h>
-#include <SDL/SDL_syswm.h>
+#include <SDL.h>
+#include <SDL_syswm.h>
+#include <sys/time.h>
 
 extern pthread_mutex_t mlt_sdl_mutex;
 
@@ -44,6 +46,7 @@ struct consumer_sdl_s
        int running;
        int sdl_flags;
        double last_speed;
+       mlt_position last_position;
 
        pthread_cond_t refresh_cond;
        pthread_mutex_t refresh_mutex;
@@ -87,8 +90,9 @@ mlt_consumer consumer_sdl_preview_init( mlt_profile profile, mlt_service_type ty
                // Create child consumers
                this->play = mlt_factory_consumer( profile, "sdl", arg );
                this->still = mlt_factory_consumer( profile, "sdl_still", arg );
-               mlt_properties_set( MLT_CONSUMER_PROPERTIES( parent ), "real_time", "0" );
-               mlt_properties_set( MLT_CONSUMER_PROPERTIES( parent ), "rescale", "nearest" );
+               mlt_properties_set( properties, "rescale", "nearest" );
+               mlt_properties_set( properties, "deinterlace_method", "onefield" );
+               mlt_properties_set_int( properties, "prefill", 1 );
                parent->close = consumer_close;
                parent->start = consumer_start;
                parent->stop = consumer_stop;
@@ -101,6 +105,7 @@ mlt_consumer consumer_sdl_preview_init( mlt_profile profile, mlt_service_type ty
                pthread_cond_init( &this->refresh_cond, NULL );
                pthread_mutex_init( &this->refresh_mutex, NULL );
                mlt_events_listen( MLT_CONSUMER_PROPERTIES( parent ), this, "property-changed", ( mlt_listener )consumer_refresh_cb );
+               mlt_events_register( properties, "consumer-sdl-paused", NULL );
                return parent;
        }
        free( this );
@@ -111,6 +116,7 @@ void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer parent, mlt_frame fr
 {
        consumer_sdl this = parent->child;
        this->last_speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
+       this->last_position = mlt_frame_get_position( frame );
        mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-frame-show", frame, NULL );
 }
 
@@ -125,7 +131,7 @@ static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *na
        {
                consumer_sdl this = parent->child;
                pthread_mutex_lock( &this->refresh_mutex );
-               this->refresh_count = this->refresh_count <= 0 ? 1 : this->refresh_count ++;
+               this->refresh_count = this->refresh_count <= 0 ? 1 : this->refresh_count + 1;
                pthread_cond_broadcast( &this->refresh_cond );
                pthread_mutex_unlock( &this->refresh_mutex );
        }
@@ -170,7 +176,10 @@ static int consumer_start( mlt_consumer parent )
                if ( audio_device != NULL )
                        setenv( "AUDIODEV", audio_device, 1 );
 
-               if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
+               pthread_mutex_lock( &mlt_sdl_mutex );
+               int ret = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
+               pthread_mutex_unlock( &mlt_sdl_mutex );
+               if ( ret < 0 )
                {
                        fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
                        return -1;
@@ -188,12 +197,11 @@ static int consumer_start( mlt_consumer parent )
                mlt_properties_set_int( play, "progressive", progressive );
                mlt_properties_set_int( still, "progressive", progressive );
 
-               mlt_properties_pass_list( play, properties, "resize,rescale,width,height,aspect_ratio,display_ratio,volume" );
-               mlt_properties_pass_list( still, properties, "resize,rescale,width,height,aspect_ratio,display_ratio" );
-               mlt_properties_pass_list( play, properties, "deinterlace_method" );
-               mlt_properties_pass_list( still, properties, "deinterlace_method" );
-               mlt_properties_pass_list( play, properties, "preview_off,preview_format,window_background" );
-               mlt_properties_pass_list( still, properties, "preview_off,preview_format,window_background" );
+               mlt_properties_pass_list( play, properties,
+                       "deinterlace_method,resize,rescale,width,height,aspect_ratio,display_ratio,preview_off,preview_format,window_background"
+                       ",volume,real_time,buffer,prefill,audio_off,frequency" );
+               mlt_properties_pass_list( still, properties,
+                       "deinterlace_method,resize,rescale,width,height,aspect_ratio,display_ratio,preview_off,preview_format,window_background" );
 
                mlt_properties_pass( play, properties, "play." );
                mlt_properties_pass( still, properties, "still." );
@@ -205,6 +213,7 @@ static int consumer_start( mlt_consumer parent )
 
                mlt_properties_set_int( play, "put_mode", 1 );
                mlt_properties_set_int( still, "put_mode", 1 );
+               mlt_properties_set_int( play, "terminate_on_pause", 1 );
 
                // Start the still producer just to initialise the gui
                mlt_consumer_start( this->still );
@@ -240,7 +249,9 @@ static int consumer_stop( mlt_consumer parent )
                pthread_mutex_lock( &this->refresh_mutex );
                pthread_cond_broadcast( &this->refresh_cond );
                pthread_mutex_unlock( &this->refresh_mutex );
+#ifndef WIN32
                if ( this->thread )
+#endif
                        pthread_join( this->thread, NULL );
                this->joined = 1;
 
@@ -275,6 +286,8 @@ static void *consumer_thread( void *arg )
        int first = 1;
        mlt_frame frame = NULL;
        int last_position = -1;
+       int eos = 0;
+       int eos_threshold = 20 + mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this->play ), "buffer" );
 
        // Determine if the application is dealing with the preview
        int preview_off = mlt_properties_get_int( properties, "preview_off" );
@@ -326,30 +339,79 @@ static void *consumer_thread( void *arg )
                                last_position = -1;
                        }
 
-                       // If we're not the first frame and both consumers are stopped, then stop ourselves
-                       if ( !first && mlt_consumer_is_stopped( this->play ) && mlt_consumer_is_stopped( this->still ) )
-                       {
-                               this->running = 0;
-                               mlt_frame_close( frame );
-                       }
-                       // Allow a little grace time before switching consumers on speed changes
-                       else if ( this->ignore_change -- > 0 && this->active != NULL && !mlt_consumer_is_stopped( this->active ) )
-                       {
-                               mlt_consumer_put_frame( this->active, frame );
-                       }
                        // If we aren't playing normally, then use the still
-                       else if ( speed != 1 )
+                       if ( speed != 1 )
                        {
-                               if ( !mlt_consumer_is_stopped( this->play ) )
-                                       mlt_consumer_stop( this->play );
-                               if ( mlt_consumer_is_stopped( this->still ) )
+                               mlt_producer producer = MLT_PRODUCER( mlt_service_get_producer( MLT_CONSUMER_SERVICE( consumer ) ) );
+                               mlt_position duration = mlt_producer_get_playtime( producer );
+                               int pause = 0;
+
+#ifndef SKIP_WAIT_EOS
+                               if ( this->active == this->play )
+                               {
+                                       // Do not interrupt the play consumer near the end
+                                       if ( duration - this->last_position > eos_threshold )
+                                       {
+                                               // Get a new frame at the sought position
+                                               mlt_frame_close( frame );
+                                               if ( producer )
+                                                       mlt_producer_seek( producer, this->last_position );
+                                               frame = mlt_consumer_get_frame( consumer );
+                                               pause = 1;
+                                       }
+                                       else
+                                       {
+                                               // Send frame with speed 0 to stop it
+                                               if ( frame && !mlt_consumer_is_stopped( this->play ) )
+                                               {
+                                                       mlt_consumer_put_frame( this->play, frame );
+                                                       frame = NULL;
+                                                       eos = 1;
+                                               }
+
+                                               // Check for end of stream
+                                               if ( mlt_consumer_is_stopped( this->play ) )
+                                               {
+                                                       // Stream has ended
+                                                       mlt_log_verbose( MLT_CONSUMER_SERVICE( consumer ), "END OF STREAM\n" );
+                                                       pause = 1;
+                                                       eos = 0; // reset eos indicator
+                                               }
+                                               else
+                                               {
+                                                       // Prevent a tight busy loop
+                                                       struct timespec tm = { 0, 100000L }; // 100 usec
+                                                       nanosleep( &tm, NULL );
+                                               }
+                                       }
+                               }
+#else
+                               pause = this->active == this->play;
+#endif
+                               if ( pause )
                                {
+                                       // Start the still consumer
+                                       mlt_consumer_stop( this->play );
                                        this->last_speed = speed;
                                        this->active = this->still;
                                        this->ignore_change = 0;
                                        mlt_consumer_start( this->still );
                                }
-                               mlt_consumer_put_frame( this->still, frame );
+                               // Send the frame to the active child
+                               if ( frame && !eos )
+                               {
+                                       mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "refresh", 1 );
+                                       mlt_consumer_put_frame( this->active, frame );
+                               }
+                               if ( pause && speed == 0.0 )
+                               {
+                                       mlt_events_fire( properties, "consumer-sdl-paused", NULL );
+                               }
+                       }
+                       // Allow a little grace time before switching consumers on speed changes
+                       else if ( this->ignore_change -- > 0 && this->active != NULL && !mlt_consumer_is_stopped( this->active ) )
+                       {
+                               mlt_consumer_put_frame( this->active, frame );
                        }
                        // Otherwise use the normal player
                        else
@@ -360,7 +422,7 @@ static void *consumer_thread( void *arg )
                                {
                                        this->last_speed = speed;
                                        this->active = this->play;
-                                       this->ignore_change = 25;
+                                       this->ignore_change = 0;
                                        mlt_consumer_start( this->play );
                                }
                                mlt_consumer_put_frame( this->play, frame );
@@ -382,7 +444,10 @@ static void *consumer_thread( void *arg )
                        {
                                pthread_mutex_lock( &this->refresh_mutex );
                                if ( this->running && speed == 0 && this->refresh_count <= 0 )
+                               {
+                                       mlt_events_fire( properties, "consumer-sdl-paused", NULL );
                                        pthread_cond_wait( &this->refresh_cond, &this->refresh_mutex );
+                               }
                                this->refresh_count --;
                                pthread_mutex_unlock( &this->refresh_mutex );
                        }