add transition caching with pref, rev read frame caching, cache lock tweaks, fix...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / playbackengine.C
index 228f3890b2def426bf3d37f808c81fbeeaabb6ad..571afe2a83a174a3eaeeab536182258131839d2b 100644 (file)
@@ -33,6 +33,7 @@
 #include "mwindowgui.h"
 #include "patchbay.h"
 #include "tracking.h"
+#include "tracks.h"
 #include "playbackengine.h"
 #include "playtransport.h"
 #include "preferences.h"
@@ -40,6 +41,8 @@
 #include "mainsession.h"
 #include "trackcanvas.h"
 #include "transportque.h"
+#include "videodevice.h"
+#include "vdevicex11.h"
 #include "vrender.h"
 
 
@@ -68,6 +71,7 @@ PlaybackEngine::PlaybackEngine(MWindow *mwindow, Canvas *output)
        tracking_done = new Condition(1, "PlaybackEngine::tracking_done");
        pause_lock = new Condition(0, "PlaybackEngine::pause_lock");
        start_lock = new Condition(0, "PlaybackEngine::start_lock");
+       cache_lock = new Mutex("PlaybackEngine::cache_lock");
        input_lock = new Condition(1, "PlaybackEngine::input_lock");
        output_lock = new Condition(0, "PlaybackEngine::output_lock", 1);
 
@@ -82,12 +86,15 @@ PlaybackEngine::~PlaybackEngine()
        Thread::join();
        delete preferences;
        delete_render_engine();
-       delete audio_cache;
-       delete video_cache;
+       if( audio_cache )
+               audio_cache->remove_user();
+       if( video_cache )
+               video_cache->remove_user();
        delete tracking_lock;
        delete tracking_done;
        delete pause_lock;
        delete start_lock;
+       delete cache_lock;
        delete renderengine_lock;
        delete command;
        delete next_command;
@@ -159,10 +166,14 @@ void PlaybackEngine::wait_render_engine()
 
 void PlaybackEngine::create_cache()
 {
-       if(audio_cache) { delete audio_cache;  audio_cache = 0; }
-       if(video_cache) { delete video_cache;  video_cache = 0; }
-       if(!audio_cache) audio_cache = new CICache(preferences);
-       if(!video_cache) video_cache = new CICache(preferences);
+       cache_lock->lock("PlaybackEngine::create_cache");
+       if( audio_cache )
+               audio_cache->remove_user();
+       if( video_cache )
+               video_cache->remove_user();
+       audio_cache = new CICache(preferences);
+       video_cache = new CICache(preferences);
+       cache_lock->unlock();
 }
 
 
@@ -255,8 +266,9 @@ void PlaybackEngine::init_tracking()
        init_meters();
 }
 
-void PlaybackEngine::stop_tracking()
+void PlaybackEngine::stop_tracking(double position)
 {
+       tracking_position = position;
        tracking_active = 0;
        stop_cursor();
        tracking_done->unlock();
@@ -399,20 +411,18 @@ void PlaybackEngine::run()
 // Dispatch the command
                        start_render_engine();
                        break;
-
-               case SINGLE_FRAME_FWD:
-               case SINGLE_FRAME_REWIND:
 // fall through
                default:
                        is_playing_back = 1;
+               case REWIND:
+               case GOTO_END:
                        perform_change();
                        arm_render_engine();
 
 // Start tracking after arming so the tracking position doesn't change.
 // The tracking for a single frame command occurs during PAUSE
                        init_tracking();
-                       if( !command->single_frame() )
-                               clear_output();
+                       clear_borders();
 // Dispatch the command
                        start_render_engine();
                        break;
@@ -421,13 +431,22 @@ void PlaybackEngine::run()
        }
 }
 
-void PlaybackEngine::clear_output()
+void PlaybackEngine::clear_borders()
 {
-       BC_WindowBase *cwdw = output->get_canvas();
-       if( !cwdw ) return;
-       cwdw->lock_window("PlaybackEngine::clear_output");
-       output->clear();
-       cwdw->unlock_window();
+       EDL *edl = command->get_edl();
+       PlaybackConfig *config = edl->session->playback_config;
+       if( config->vconfig->driver == PLAYBACK_X11_GL ) {
+               if( render_engine && render_engine->video ) {
+                       VDeviceBase *vdriver = render_engine->video->get_output_base();
+                       ((VDeviceX11*)vdriver)->clear_output();
+                       return;
+               }
+       }
+       BC_WindowBase *window = output->get_canvas();
+       if( !window ) return;
+       window->lock_window("PlaybackEngine::clear_output");
+       output->clear_borders(edl);
+       window->unlock_window();
 }
 
 void PlaybackEngine::stop_playback(int wait_tracking)
@@ -441,12 +460,22 @@ void PlaybackEngine::stop_playback(int wait_tracking)
        renderengine_lock->unlock();
 }
 
+int PlaybackEngine::get_direction()
+{
+       int curr_command = is_playing_back ? this->command->command : STOP;
+       return TransportCommand::get_direction(curr_command);
+}
+
+void PlaybackEngine::update_preferences(Preferences *prefs)
+{
+       preferences->copy_from(prefs);
+       create_render_engine();
+}
 
 void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int use_inout)
 {
 //printf("PlaybackEngine::send_command 1 %d\n", command);
 // Stop requires transferring the output buffer to a refresh buffer.
-       int do_stop = 0;
        int curr_command = is_playing_back ? this->command->command : STOP;
        int curr_single_frame = TransportCommand::single_frame(curr_command);
        int curr_audio = this->command->toggle_audio ?
@@ -454,9 +483,11 @@ void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int
        int single_frame = TransportCommand::single_frame(command);
        int next_audio = next_command->toggle_audio ? !single_frame : single_frame;
        float next_speed = next_command->speed;
-
 // Dispatch command
        switch( command ) {
+       case STOP:
+               transport_stop(wait_tracking);
+               break;
        case FAST_REWIND:       // Commands that play back
        case NORMAL_REWIND:
        case SLOW_REWIND:
@@ -471,38 +502,38 @@ void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int
                if( next_speed ) curr_command = COMMAND_NONE;
 // Same direction pressed twice, not shuttle, and no change in audio state,  Stop
                if( curr_command == command && !curr_single_frame &&
-                   curr_audio == next_audio ) { do_stop = 1;  break; }
-
+                   curr_audio == next_audio ) {
+                       transport_stop(wait_tracking);
+                       break;
+               }
 // Resume or change direction
                switch( curr_command ) {
-               default:
-                       transport_stop(0);
-                       next_command->resume = 1;
-// fall through
+               case REWIND:
+               case GOTO_END:
                case STOP:
                case COMMAND_NONE:
                case SINGLE_FRAME_FWD:
                case SINGLE_FRAME_REWIND:
                case CURRENT_FRAME:
                case LAST_FRAME:
-                       next_command->realtime = 1;
-// Start from scratch
-                       transport_command(command, CHANGE_NONE, edl, use_inout);
+// already stopped
+                       break;
+               default:
+                       transport_stop(0);
+                       next_command->resume = 1;
                        break;
                }
+               next_command->realtime = 1;
+               transport_command(command, CHANGE_NONE, edl, use_inout);
                break;
-
-// Commands that stop
-       case STOP:
        case REWIND:
        case GOTO_END:
-               do_stop = 1;
+               transport_stop(1);
+               next_command->realtime = 1;
+               transport_command(command, CHANGE_NONE, edl, use_inout);
+               stop_tracking(this->command->playbackstart);
                break;
        }
-
-       if( do_stop ) {
-               transport_stop(wait_tracking);
-       }
 }
 
 int PlaybackEngine::put_command(TransportCommand *command, int reset)