get back to ideal frame time when slow

This commit is contained in:
Stephen Birarda 2018-09-05 16:55:46 -07:00
parent 925c39cbd3
commit aea9775e4d
2 changed files with 18 additions and 16 deletions

View file

@ -386,15 +386,18 @@ void AudioMixer::start() {
// mix state // mix state
unsigned int frame = 1; unsigned int frame = 1;
auto frameTimestamp = p_high_resolution_clock::now();
while (!_isFinished) { while (!_isFinished) {
auto ticTimer = _ticTiming.timer(); auto ticTimer = _ticTiming.timer();
{ if (_startFrameTimestamp.time_since_epoch().count() == 0) {
auto timer = _checkTimeTiming.timer(); _startFrameTimestamp = _idealFrameTimestamp = p_high_resolution_clock::now();
auto frameDuration = timeFrame(frameTimestamp); } else {
throttle(frameDuration, frame); {
auto timer = _checkTimeTiming.timer();
auto frameDuration = timeFrame();
throttle(frameDuration, frame);
}
} }
auto frameTimer = _frameTiming.timer(); auto frameTimer = _frameTiming.timer();
@ -449,26 +452,22 @@ void AudioMixer::start() {
} }
} }
chrono::microseconds AudioMixer::timeFrame(p_high_resolution_clock::time_point& timestamp) { chrono::microseconds AudioMixer::timeFrame() {
// advance the next frame // advance the next frame
auto nextTimestamp = timestamp + chrono::microseconds(AudioConstants::NETWORK_FRAME_USECS);
auto now = p_high_resolution_clock::now(); auto now = p_high_resolution_clock::now();
// compute how long the last frame took // compute how long the last frame took
auto duration = chrono::duration_cast<chrono::microseconds>(now - timestamp); auto duration = chrono::duration_cast<chrono::microseconds>(now - _startFrameTimestamp);
// set the new frame timestamp _idealFrameTimestamp += chrono::microseconds(AudioConstants::NETWORK_FRAME_USECS);
timestamp = max(now, nextTimestamp);
{ {
auto timer = _sleepTiming.timer(); auto timer = _sleepTiming.timer();
this_thread::sleep_until(_idealFrameTimestamp);
// sleep until the next frame should start
// WIN32 sleep_until is broken until VS2015 Update 2
// instead, max (above) guarantees that timestamp >= now, so we can sleep_for
this_thread::sleep_for(timestamp - now);
} }
_startFrameTimestamp = p_high_resolution_clock::now();
return duration; return duration;
} }

View file

@ -84,7 +84,7 @@ private slots:
private: private:
// mixing helpers // mixing helpers
std::chrono::microseconds timeFrame(p_high_resolution_clock::time_point& timestamp); std::chrono::microseconds timeFrame();
void throttle(std::chrono::microseconds frameDuration, int frame); void throttle(std::chrono::microseconds frameDuration, int frame);
AudioMixerClientData* getOrCreateClientData(Node* node); AudioMixerClientData* getOrCreateClientData(Node* node);
@ -94,6 +94,9 @@ private:
void parseSettingsObject(const QJsonObject& settingsObject); void parseSettingsObject(const QJsonObject& settingsObject);
void clearDomainSettings(); void clearDomainSettings();
p_high_resolution_clock::time_point _idealFrameTimestamp;
p_high_resolution_clock::time_point _startFrameTimestamp;
float _trailingMixRatio { 0.0f }; float _trailingMixRatio { 0.0f };
float _throttlingRatio { 0.0f }; float _throttlingRatio { 0.0f };