From 4a4befa7a6ca3aca424b629e203c05e10e5a32ce Mon Sep 17 00:00:00 2001 From: Goncalo Mendes Date: Fri, 18 Nov 2022 14:30:19 +0000 Subject: [PATCH] Add range to estimated playout delay --- .../OpenTok Live/OTBroadcastExtAudioDevice.m | 15 +++++++++------ .../External-Audio-Device/OTDefaultAudioDevice.m | 15 +++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Broadcast-Ext/OpenTok Live/OTBroadcastExtAudioDevice.m b/Broadcast-Ext/OpenTok Live/OTBroadcastExtAudioDevice.m index 1b7f3888..756bb6c4 100755 --- a/Broadcast-Ext/OpenTok Live/OTBroadcastExtAudioDevice.m +++ b/Broadcast-Ext/OpenTok Live/OTBroadcastExtAudioDevice.m @@ -47,6 +47,8 @@ #endif static double kPreferredIOBufferDuration = 0.01; +static double kToMicroSecond = 1000000; +static UInt8 kMaxPlayoutDelay = 150; static mach_timebase_info_data_t info; @@ -280,7 +282,7 @@ - (BOOL)isCapturing - (uint16_t)estimatedRenderDelay { - return _playoutDelay; + return (uint16_t)MIN(_playoutDelay, (UInt32)kMaxPlayoutDelay); } - (uint16_t)estimatedCaptureDelay @@ -670,17 +672,18 @@ static void update_playout_delay(OTBroadcastExtAudioDevice* device) { // HW output latency NSTimeInterval interval = [mySession outputLatency]; - device->_playoutDelay += (int)(interval * 1000000); + device->_playoutDelay += (int)(interval * kToMicroSecond); // HW buffer duration interval = [mySession IOBufferDuration]; - device->_playoutDelay += (int)(interval * 1000000); + device->_playoutDelay += (int)(interval * kToMicroSecond); - device->_playoutDelay += (int)(device->_playout_AudioUnitProperty_Latency * 1000000); + device->_playoutDelay += (int)(device->_playout_AudioUnitProperty_Latency * kToMicroSecond); // To ms - device->_playoutDelay = (device->_playoutDelay - 500) / 1000; - + if ( device->_playoutDelay >= 500 ) { + device->_playoutDelay = (device->_playoutDelay - 500) / 1000; + } // Reset counter device->_playoutDelayMeasurementCounter = 0; } diff --git a/Custom-Audio-Driver/External-Audio-Device/OTDefaultAudioDevice.m b/Custom-Audio-Driver/External-Audio-Device/OTDefaultAudioDevice.m index 56c1c2d9..178cd7b3 100644 --- a/Custom-Audio-Driver/External-Audio-Device/OTDefaultAudioDevice.m +++ b/Custom-Audio-Driver/External-Audio-Device/OTDefaultAudioDevice.m @@ -47,6 +47,8 @@ #endif static double kPreferredIOBufferDuration = 0.01; +static double kToMicroSecond = 1000000; +static UInt8 kMaxPlayoutDelay = 150; static mach_timebase_info_data_t info; @@ -331,7 +333,7 @@ - (BOOL)isCapturing - (uint16_t)estimatedRenderDelay { - return _playoutDelay; + return (uint16_t)MIN(_playoutDelay, (UInt32)kMaxPlayoutDelay); } - (uint16_t)estimatedCaptureDelay @@ -878,17 +880,18 @@ static void update_playout_delay(OTDefaultAudioDevice* device) { // HW output latency NSTimeInterval interval = [mySession outputLatency]; - device->_playoutDelay += (int)(interval * 1000000); + device->_playoutDelay += (int)(interval * kToMicroSecond); // HW buffer duration interval = [mySession IOBufferDuration]; - device->_playoutDelay += (int)(interval * 1000000); + device->_playoutDelay += (int)(interval * kToMicroSecond); - device->_playoutDelay += (int)(device->_playout_AudioUnitProperty_Latency * 1000000); + device->_playoutDelay += (int)(device->_playout_AudioUnitProperty_Latency * kToMicroSecond); // To ms - device->_playoutDelay = (device->_playoutDelay - 500) / 1000; - + if ( device->_playoutDelay >= 500 ) { + device->_playoutDelay = (device->_playoutDelay - 500) / 1000; + } // Reset counter device->_playoutDelayMeasurementCounter = 0; }