macOS: Begin wait from end of work.

ms-pipeline-optim-2
Mooneer Salem 2025-08-10 00:22:03 -07:00
parent d95bdcf523
commit 7c719a3e3c
1 changed files with 11 additions and 12 deletions

View File

@ -39,7 +39,6 @@ using namespace std::placeholders;
thread_local void* MacAudioDevice::Workgroup_ = nullptr;
thread_local void* MacAudioDevice::JoinToken_ = nullptr;
thread_local int MacAudioDevice::CurrentCoreAudioId_ = 0;
thread_local int64_t MacAudioDevice::AddedWaitDuration_ = 0;
// Conversion factors.
constexpr static int MS_TO_SEC = 1000;
@ -880,24 +879,24 @@ void MacAudioDevice::startRealTimeWork()
#endif // 0
// Record current time. We'll wait up to (latency) ms after this.
waitTime_ = dispatch_time(DISPATCH_TIME_NOW, 0);
//waitTime_ = dispatch_time(DISPATCH_TIME_NOW, 0);
}
void MacAudioDevice::stopRealTimeWork(bool fastMode)
{
auto timeToWaitMilliseconds = ((1000 * chosenFrameSize_) / sampleRate_) >> (fastMode ? 1 : 0);
timeToWaitMilliseconds -= AddedWaitDuration_;
//timeToWaitMilliseconds -= AddedWaitDuration_;
auto waitStart = std::chrono::high_resolution_clock::now();
dispatch_semaphore_wait(sem_, dispatch_time(waitTime_, MS_TO_NSEC * timeToWaitMilliseconds));
auto waitEnd = std::chrono::high_resolution_clock::now();
auto waitDurationMilliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(waitEnd - waitStart).count();
//auto waitStart = std::chrono::high_resolution_clock::now();
dispatch_semaphore_wait(sem_, dispatch_time(DISPATCH_TIME_NOW, MS_TO_NSEC * timeToWaitMilliseconds));
//auto waitEnd = std::chrono::high_resolution_clock::now();
//auto waitDurationMilliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(waitEnd - waitStart).count();
AddedWaitDuration_ += waitDurationMilliseconds - timeToWaitMilliseconds;
if (AddedWaitDuration_ < 0)
{
AddedWaitDuration_ = 0;
}
//AddedWaitDuration_ += waitDurationMilliseconds - timeToWaitMilliseconds;
//if (AddedWaitDuration_ < 0)
//{
// AddedWaitDuration_ = 0;
//}
}
void MacAudioDevice::clearHelperRealTime()