Add basic memory-used stuff for butler and reduce minimum audio
[dcpomatic.git] / src / lib / butler.cc
index 2b6010aaa6df777498f90a7c969e667670eea623..56f8919f5516487742c44b308bf345ceb350d687 100644 (file)
@@ -29,6 +29,7 @@
 using std::cout;
 using std::pair;
 using std::make_pair;
+using std::string;
 using boost::weak_ptr;
 using boost::shared_ptr;
 using boost::bind;
@@ -37,11 +38,11 @@ using boost::optional;
 /** Minimum video readahead in frames */
 #define MINIMUM_VIDEO_READAHEAD 10
 /** Maximum video readahead in frames; should never be reached unless there are bugs in Player */
-#define MAXIMUM_VIDEO_READAHEAD 240
+#define MAXIMUM_VIDEO_READAHEAD 24
 /** Minimum audio readahead in frames */
-#define MINIMUM_AUDIO_READAHEAD (48000*5)
+#define MINIMUM_AUDIO_READAHEAD (48000 * MINIMUM_VIDEO_READAHEAD / 24)
 /** Minimum audio readahead in frames; should never be reached unless there are bugs in Player */
-#define MAXIMUM_AUDIO_READAHEAD (48000*60)
+#define MAXIMUM_AUDIO_READAHEAD (48000 * MAXIMUM_VIDEO_READAHEAD / 24)
 
 #define LOG_WARNING(...) _log->log (String::compose(__VA_ARGS__), LogEntry::TYPE_WARNING);
 
@@ -59,7 +60,6 @@ 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));
-       _player_changed_connection = _player->Changed.connect (bind (&Butler::player_changed, this));
        _thread = new boost::thread (bind (&Butler::thread, this));
 
        /* Create some threads to do work on the PlayerVideos we are creating; at present this is used to
@@ -102,12 +102,18 @@ Butler::should_run () const
                LOG_WARNING ("Butler audio buffers reached %1 frames", _audio.size());
        }
 
-       return (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD))
-               && (_video.size() < MAXIMUM_VIDEO_READAHEAD)
-               && (_audio.size() < MAXIMUM_AUDIO_READAHEAD)
-               && !_stop_thread
-               && !_finished
-               && !_died;
+       if (_stop_thread || _finished || _died) {
+               /* Definitely do not run */
+               return false;
+       }
+
+       if (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD)) {
+               /* Definitely do run: we need data */
+               return true;
+       }
+
+       /* Run if we aren't full of video or audio */
+       return (_video.size() < MAXIMUM_VIDEO_READAHEAD) && (_audio.size() < MAXIMUM_AUDIO_READAHEAD);
 }
 
 void
@@ -231,14 +237,6 @@ Butler::audio (shared_ptr<AudioBuffers> audio)
        _audio.put (remap (audio, _audio_channels, _audio_mapping));
 }
 
-void
-Butler::player_changed ()
-{
-       _video.clear ();
-       _audio.clear ();
-       _summon.notify_all ();
-}
-
 /** Try to get `frames' frames of audio and copy it into `out'.  Silence
  *  will be filled if no audio is available.
  *  @return true if there was a buffer underrun, otherwise false.
@@ -257,3 +255,10 @@ Butler::disable_audio ()
        boost::mutex::scoped_lock lm (_mutex);
        _disable_audio = true;
 }
+
+pair<size_t, string>
+Butler::memory_used () const
+{
+       /* XXX: should also look at _audio.memory_used() */
+       return _video.memory_used();
+}