More audio readahead.
[dcpomatic.git] / src / lib / butler.cc
index e40ae0b24f958767f85f318543112957fe4818d3..96546a673fa70c474b18e8f15fb1066a206540fe 100644 (file)
@@ -33,19 +33,27 @@ 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)
 {
-       _player->Video.connect (bind (&VideoRingBuffers::put, &_video, _1, _2));
+       _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 ();
@@ -57,34 +65,48 @@ Butler::~Butler ()
 
 void
 Butler::thread ()
+try
 {
        while (true) {
                boost::mutex::scoped_lock lm (_mutex);
 
-               while (_video.size() > VIDEO_READAHEAD && !_pending_seek_position) {
+               /* Wait until we have something to do */
+               while ((_video.size() >= VIDEO_READAHEAD && _audio.size() >= AUDIO_READAHEAD && !_pending_seek_position) || _stop_thread) {
                        _summon.wait (lm);
                }
 
+               /* Do any seek that has been requested */
                if (_pending_seek_position) {
                        _player->seek (*_pending_seek_position, _pending_seek_accurate);
                        _pending_seek_position = optional<DCPTime> ();
                }
 
-               while (_video.size() < VIDEO_READAHEAD) {
-                       _arrived.notify_all ();
+               /* 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/_audio.
+               */
+               while ((_video.size() < VIDEO_READAHEAD || _audio.size() < AUDIO_READAHEAD) && !_pending_seek_position && !_stop_thread) {
+                       lm.unlock ();
                        if (_player->pass ()) {
                                _finished = true;
+                               _arrived.notify_all ();
                                break;
                        }
+                       lm.lock ();
+                       _arrived.notify_all ();
                }
        }
+} catch (boost::thread_interrupted) {
+       /* The butler thread is being terminated */
 }
 
 pair<shared_ptr<PlayerVideo>, DCPTime>
 Butler::get_video ()
 {
        boost::mutex::scoped_lock lm (_mutex);
-       while (_video.size() == 0 && !_finished) {
+
+       /* Wait for data if we have none */
+       while (_video.empty() && !_finished) {
                _arrived.wait (lm);
        }
 
@@ -92,20 +114,68 @@ Butler::get_video ()
                return make_pair (shared_ptr<PlayerVideo>(), DCPTime());
        }
 
-       return _video.get ();
+       pair<shared_ptr<PlayerVideo>, DCPTime> const r = _video.get ();
+       _summon.notify_all ();
+       return r;
 }
 
 void
 Butler::seek (DCPTime position, bool accurate)
 {
+       boost::mutex::scoped_lock lm (_mutex);
        _video.clear ();
+       _finished = false;
+       _pending_seek_position = position;
+       _pending_seek_accurate = accurate;
+       _summon.notify_all ();
+}
 
+void
+Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
+{
        {
                boost::mutex::scoped_lock lm (_mutex);
-               _finished = false;
-               _pending_seek_position = position;
-               _pending_seek_accurate = accurate;
+               if (_pending_seek_position) {
+                       /* Don't store any video while a seek is pending */
+                       return;
+               }
        }
 
+       _video.put (video, time);
+}
+
+void
+Butler::audio (shared_ptr<AudioBuffers> audio, DCPTime time)
+{
+       {
+               boost::mutex::scoped_lock lm (_mutex);
+               if (_pending_seek_position) {
+                       /* Don't store any audio while a seek is pending */
+                       return;
+               }
+       }
+
+       _audio.put (audio, time);
+}
+
+void
+Butler::player_changed ()
+{
+       optional<DCPTime> t;
+
+       {
+               boost::mutex::scoped_lock lm (_mutex);
+               t = _video.earliest ();
+       }
+
+       if (t) {
+               seek (*t, true);
+       }
+}
+
+void
+Butler::get_audio (float* out, Frame frames)
+{
+       _audio.get (reinterpret_cast<float*> (out), _audio_channels, frames);
        _summon.notify_all ();
 }