Config option to disable preview audio.
[dcpomatic.git] / src / lib / butler.cc
index 1dbad61521b65d6927c494e35ae8b6505231d210..32d607c5d22c080477889cbb4646776cefe845f4 100644 (file)
@@ -33,20 +33,28 @@ using boost::optional;
 
 /** Video readahead in frames */
 #define VIDEO_READAHEAD 10
+/** Audio readahead in frames */
+#define AUDIO_READAHEAD (48000*5)
 
-Butler::Butler (weak_ptr<const Film> film, shared_ptr<Player> player)
+Butler::Butler (weak_ptr<const Film> film, shared_ptr<Player> player, AudioMapping audio_mapping, int audio_channels)
        : _film (film)
        , _player (player)
        , _pending_seek_accurate (false)
        , _finished (false)
+       , _audio_mapping (audio_mapping)
+       , _audio_channels (audio_channels)
+       , _stop_thread (false)
+       , _disable_audio (false)
 {
        _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));
        _thread = new boost::thread (bind (&Butler::thread, this));
 }
 
 Butler::~Butler ()
 {
+       _stop_thread = true;
        _thread->interrupt ();
        try {
                _thread->join ();
@@ -56,14 +64,21 @@ Butler::~Butler ()
        delete _thread;
 }
 
+bool
+Butler::should_run () const
+{
+       return (_video.size() < VIDEO_READAHEAD || _audio.size() < AUDIO_READAHEAD) && !_stop_thread && !_finished;
+}
+
 void
 Butler::thread ()
+try
 {
        while (true) {
                boost::mutex::scoped_lock lm (_mutex);
 
                /* Wait until we have something to do */
-               while (_video.size() >= VIDEO_READAHEAD && !_pending_seek_position) {
+               while (!should_run() && !_pending_seek_position) {
                        _summon.wait (lm);
                }
 
@@ -73,11 +88,11 @@ Butler::thread ()
                        _pending_seek_position = optional<DCPTime> ();
                }
 
-               /* Fill _video.  Don't try to carry on if a pending seek appears
+               /* Fill _video and _audio.  Don't try to carry on if a pending seek appears
                   while lm is unlocked, as in that state nothing will be added to
-                  _video.
+                  _video/_audio.
                */
-               while (_video.size() < VIDEO_READAHEAD && !_pending_seek_position) {
+               while (should_run() && !_pending_seek_position) {
                        lm.unlock ();
                        if (_player->pass ()) {
                                _finished = true;
@@ -88,6 +103,10 @@ Butler::thread ()
                        _arrived.notify_all ();
                }
        }
+} catch (boost::thread_interrupted) {
+       /* The butler thread is being terminated */
+} catch (...) {
+       store_current ();
 }
 
 pair<shared_ptr<PlayerVideo>, DCPTime>
@@ -100,7 +119,7 @@ Butler::get_video ()
                _arrived.wait (lm);
        }
 
-       if (_finished) {
+       if (_video.empty() && _finished) {
                return make_pair (shared_ptr<PlayerVideo>(), DCPTime());
        }
 
@@ -114,6 +133,7 @@ Butler::seek (DCPTime position, bool accurate)
 {
        boost::mutex::scoped_lock lm (_mutex);
        _video.clear ();
+       _audio.clear ();
        _finished = false;
        _pending_seek_position = position;
        _pending_seek_accurate = accurate;
@@ -134,6 +154,20 @@ Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
        _video.put (video, time);
 }
 
+void
+Butler::audio (shared_ptr<AudioBuffers> audio, DCPTime time)
+{
+       {
+               boost::mutex::scoped_lock lm (_mutex);
+               if (_pending_seek_position || _disable_audio) {
+                       /* Don't store any audio while a seek is pending, or if audio is disabled */
+                       return;
+               }
+       }
+
+       _audio.put (audio, time);
+}
+
 void
 Butler::player_changed ()
 {
@@ -146,5 +180,22 @@ Butler::player_changed ()
 
        if (t) {
                seek (*t, true);
+       } else {
+               _video.clear ();
+               _audio.clear ();
        }
 }
+
+void
+Butler::get_audio (float* out, Frame frames)
+{
+       _audio.get (out, _audio_channels, frames);
+       _summon.notify_all ();
+}
+
+void
+Butler::disable_audio ()
+{
+       boost::mutex::scoped_lock lm (_mutex);
+       _disable_audio = true;
+}