More fixes for errors / crashes / misbehaviour with content changes
[dcpomatic.git] / src / lib / butler.cc
index aee5846542d977586bae2d65f04466deedafc7a8..386e7f9db78a90b723bb6b5463f5bed802ab20b9 100644 (file)
@@ -63,7 +63,9 @@ Butler::Butler (shared_ptr<Player> player, shared_ptr<Log> log, AudioMapping aud
 {
        _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
        _player_audio_connection = _player->Audio.connect (bind (&Butler::audio, this, _1, _2));
-       _player_changed_connection = _player->Changed.connect (bind (&Butler::player_changed, this, _1));
+       _player_text_connection = _player->Text.connect (bind (&Butler::text, this, _1, _2, _3));
+       _player_changed_connection = _player->Changed.connect (bind (&Butler::return_seek, this, _2));
+       _player_not_changed_connection = _player->NotChanged.connect (bind (&Butler::return_seek, this, false));
        _thread = new boost::thread (bind (&Butler::thread, this));
 #ifdef DCPOMATIC_LINUX
        pthread_setname_np (_thread->native_handle(), "butler");
@@ -203,6 +205,13 @@ Butler::get_video ()
        return r;
 }
 
+optional<pair<PlayerText, DCPTimePeriod> >
+Butler::get_closed_caption ()
+{
+       boost::mutex::scoped_lock lm (_mutex);
+       return _closed_caption.get ();
+}
+
 void
 Butler::seek (DCPTime position, bool accurate)
 {
@@ -217,15 +226,17 @@ Butler::seek_unlocked (DCPTime position, bool accurate)
                return;
        }
 
+       _finished = false;
+       _pending_seek_position = position;
+       _pending_seek_accurate = accurate;
+
        {
-               boost::mutex::scoped_lock lm (_video_audio_mutex);
+               boost::mutex::scoped_lock lm (_buffers_mutex);
                _video.clear ();
                _audio.clear ();
+               _closed_caption.clear ();
        }
 
-       _finished = false;
-       _pending_seek_position = position;
-       _pending_seek_accurate = accurate;
        _summon.notify_all ();
 }
 
@@ -253,7 +264,7 @@ Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
 
        _prepare_service.post (bind (&Butler::prepare, this, weak_ptr<PlayerVideo>(video)));
 
-       boost::mutex::scoped_lock lm2 (_video_audio_mutex);
+       boost::mutex::scoped_lock lm2 (_buffers_mutex);
        _video.put (video, time);
 }
 
@@ -268,7 +279,7 @@ Butler::audio (shared_ptr<AudioBuffers> audio, DCPTime time)
                }
        }
 
-       boost::mutex::scoped_lock lm2 (_video_audio_mutex);
+       boost::mutex::scoped_lock lm2 (_buffers_mutex);
        _audio.put (remap (audio, _audio_channels, _audio_mapping), time);
 }
 
@@ -299,10 +310,10 @@ Butler::memory_used () const
 }
 
 void
-Butler::player_changed (int what)
+Butler::return_seek (bool frequent)
 {
        boost::mutex::scoped_lock lm (_mutex);
-       if (_died || _pending_seek_position) {
+       if (_died || _pending_seek_position || frequent) {
                return;
        }
 
@@ -317,15 +328,17 @@ Butler::player_changed (int what)
                seek_to = next;
        }
 
-       {
-               boost::mutex::scoped_lock lm (_video_audio_mutex);
-               _video.clear ();
-               _audio.clear ();
-       }
-
-       _finished = false;
-       _summon.notify_all ();
-
        seek_unlocked (seek_to, true);
        _awaiting = seek_to;
 }
+
+void
+Butler::text (PlayerText pt, TextType type, DCPTimePeriod period)
+{
+       if (type != TEXT_CLOSED_CAPTION) {
+               return;
+       }
+
+       boost::mutex::scoped_lock lm2 (_buffers_mutex);
+       _closed_caption.put (make_pair(pt, period));
+}