rtc::Event: Finalize migration to TimeDelta.

Bug: webrtc:14366
Change-Id: Icd8792a2f9efa5609dd13da2e175042fac101d36
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/272101
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Markus Handell <handellm@webrtc.org>
Auto-Submit: Markus Handell <handellm@webrtc.org>
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37844}
This commit is contained in:
Markus Handell 2022-08-19 12:42:31 +00:00 committed by WebRTC LUCI CQ
parent 99d7d6b4f6
commit 0cd0dd3b07
7 changed files with 41 additions and 61 deletions

View file

@ -60,7 +60,7 @@ namespace webrtc {
// an event indicating that the test was OK. // an event indicating that the test was OK.
static const size_t kNumCallbacks = 10; static const size_t kNumCallbacks = 10;
// Max amount of time we wait for an event to be set while counting callbacks. // Max amount of time we wait for an event to be set while counting callbacks.
static const int kTestTimeOutInMilliseconds = 10 * 1000; static constexpr TimeDelta kTestTimeOut = TimeDelta::Seconds(10);
// Average number of audio callbacks per second assuming 10ms packet size. // Average number of audio callbacks per second assuming 10ms packet size.
static const size_t kNumCallbacksPerSecond = 100; static const size_t kNumCallbacksPerSecond = 100;
// Play out a test file during this time (unit is in seconds). // Play out a test file during this time (unit is in seconds).
@ -69,7 +69,7 @@ static const size_t kBitsPerSample = 16;
static const size_t kBytesPerSample = kBitsPerSample / 8; static const size_t kBytesPerSample = kBitsPerSample / 8;
// Run the full-duplex test during this time (unit is in seconds). // Run the full-duplex test during this time (unit is in seconds).
// Note that first `kNumIgnoreFirstCallbacks` are ignored. // Note that first `kNumIgnoreFirstCallbacks` are ignored.
static const int kFullDuplexTimeInSec = 5; static constexpr TimeDelta kFullDuplexTime = TimeDelta::Seconds(5);
// Wait for the callback sequence to stabilize by ignoring this amount of the // Wait for the callback sequence to stabilize by ignoring this amount of the
// initial callbacks (avoids initial FIFO access). // initial callbacks (avoids initial FIFO access).
// Only used in the RunPlayoutAndRecordingInFullDuplex test. // Only used in the RunPlayoutAndRecordingInFullDuplex test.
@ -77,8 +77,8 @@ static const size_t kNumIgnoreFirstCallbacks = 50;
// Sets the number of impulses per second in the latency test. // Sets the number of impulses per second in the latency test.
static const int kImpulseFrequencyInHz = 1; static const int kImpulseFrequencyInHz = 1;
// Length of round-trip latency measurements. Number of transmitted impulses // Length of round-trip latency measurements. Number of transmitted impulses
// is kImpulseFrequencyInHz * kMeasureLatencyTimeInSec - 1. // is kImpulseFrequencyInHz * kMeasureLatencyTime - 1.
static const int kMeasureLatencyTimeInSec = 11; static constexpr TimeDelta kMeasureLatencyTime = TimeDelta::Seconds(11);
// Utilized in round-trip latency measurements to avoid capturing noise samples. // Utilized in round-trip latency measurements to avoid capturing noise samples.
static const int kImpulseThreshold = 1000; static const int kImpulseThreshold = 1000;
static const char kTag[] = "[..........] "; static const char kTag[] = "[..........] ";
@ -877,7 +877,7 @@ TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) {
.Times(AtLeast(kNumCallbacks)); .Times(AtLeast(kNumCallbacks));
EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
StartPlayout(); StartPlayout();
test_is_done_.Wait(kTestTimeOutInMilliseconds); test_is_done_.Wait(kTestTimeOut);
StopPlayout(); StopPlayout();
} }
@ -896,7 +896,7 @@ TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) {
EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
StartRecording(); StartRecording();
test_is_done_.Wait(kTestTimeOutInMilliseconds); test_is_done_.Wait(kTestTimeOut);
StopRecording(); StopRecording();
} }
@ -917,7 +917,7 @@ TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) {
EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
StartPlayout(); StartPlayout();
StartRecording(); StartRecording();
test_is_done_.Wait(kTestTimeOutInMilliseconds); test_is_done_.Wait(kTestTimeOut);
StopRecording(); StopRecording();
StopPlayout(); StopPlayout();
} }
@ -937,7 +937,7 @@ TEST_F(AudioDeviceTest, RunPlayoutWithFileAsSource) {
// SetMaxPlayoutVolume(); // SetMaxPlayoutVolume();
EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
StartPlayout(); StartPlayout();
test_is_done_.Wait(kTestTimeOutInMilliseconds); test_is_done_.Wait(kTestTimeOut);
StopPlayout(); StopPlayout();
} }
@ -967,13 +967,12 @@ TEST_F(AudioDeviceTest, DISABLED_RunPlayoutAndRecordingInFullDuplex) {
std::unique_ptr<FifoAudioStream> fifo_audio_stream( std::unique_ptr<FifoAudioStream> fifo_audio_stream(
new FifoAudioStream(playout_frames_per_10ms_buffer())); new FifoAudioStream(playout_frames_per_10ms_buffer()));
mock.HandleCallbacks(&test_is_done_, fifo_audio_stream.get(), mock.HandleCallbacks(&test_is_done_, fifo_audio_stream.get(),
kFullDuplexTimeInSec * kNumCallbacksPerSecond); kFullDuplexTime.seconds() * kNumCallbacksPerSecond);
SetMaxPlayoutVolume(); SetMaxPlayoutVolume();
EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
StartRecording(); StartRecording();
StartPlayout(); StartPlayout();
test_is_done_.Wait( test_is_done_.Wait(std::max(kTestTimeOut, kFullDuplexTime));
std::max(kTestTimeOutInMilliseconds, 1000 * kFullDuplexTimeInSec));
StopPlayout(); StopPlayout();
StopRecording(); StopRecording();
@ -1000,20 +999,19 @@ TEST_F(AudioDeviceTest, DISABLED_MeasureLoopbackLatency) {
std::unique_ptr<LatencyMeasuringAudioStream> latency_audio_stream( std::unique_ptr<LatencyMeasuringAudioStream> latency_audio_stream(
new LatencyMeasuringAudioStream(playout_frames_per_10ms_buffer())); new LatencyMeasuringAudioStream(playout_frames_per_10ms_buffer()));
mock.HandleCallbacks(&test_is_done_, latency_audio_stream.get(), mock.HandleCallbacks(&test_is_done_, latency_audio_stream.get(),
kMeasureLatencyTimeInSec * kNumCallbacksPerSecond); kMeasureLatencyTime.seconds() * kNumCallbacksPerSecond);
EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
SetMaxPlayoutVolume(); SetMaxPlayoutVolume();
DisableBuiltInAECIfAvailable(); DisableBuiltInAECIfAvailable();
StartRecording(); StartRecording();
StartPlayout(); StartPlayout();
test_is_done_.Wait( test_is_done_.Wait(std::max(kTestTimeOut, kMeasureLatencyTime));
std::max(kTestTimeOutInMilliseconds, 1000 * kMeasureLatencyTimeInSec));
StopPlayout(); StopPlayout();
StopRecording(); StopRecording();
// Verify that the correct number of transmitted impulses are detected. // Verify that the correct number of transmitted impulses are detected.
EXPECT_EQ(latency_audio_stream->num_latency_values(), EXPECT_EQ(latency_audio_stream->num_latency_values(),
static_cast<size_t>( static_cast<size_t>(
kImpulseFrequencyInHz * kMeasureLatencyTimeInSec - 1)); kImpulseFrequencyInHz * kMeasureLatencyTime.seconds() - 1));
latency_audio_stream->PrintResults(); latency_audio_stream->PrintResults();
} }

View file

@ -1329,7 +1329,7 @@ int32_t AudioDeviceMac::StopRecording() {
_recording = false; _recording = false;
_doStopRec = true; // Signal to io proc to stop audio device _doStopRec = true; // Signal to io proc to stop audio device
mutex_.Unlock(); // Cannot be under lock, risk of deadlock mutex_.Unlock(); // Cannot be under lock, risk of deadlock
if (!_stopEventRec.Wait(2000)) { if (!_stopEventRec.Wait(TimeDelta::Seconds(2))) {
MutexLock lockScoped(&mutex_); MutexLock lockScoped(&mutex_);
RTC_LOG(LS_WARNING) << "Timed out stopping the capture IOProc." RTC_LOG(LS_WARNING) << "Timed out stopping the capture IOProc."
"We may have failed to detect a device removal."; "We may have failed to detect a device removal.";

View file

@ -42,8 +42,8 @@ constexpr auto kPixelFormat = ABI::Windows::Graphics::DirectX::
// The maximum time `GetFrame` will wait for a frame to arrive, if we don't have // The maximum time `GetFrame` will wait for a frame to arrive, if we don't have
// any in the pool. // any in the pool.
constexpr int kMaxWaitForFrameMs = 50; constexpr TimeDelta kMaxWaitForFrame = TimeDelta::Millis(50);
constexpr int kMaxWaitForFirstFrameMs = 500; constexpr TimeDelta kMaxWaitForFirstFrame = TimeDelta::Millis(500);
// These values are persisted to logs. Entries should not be renumbered and // These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused. // numeric values should never be reused.
@ -213,8 +213,8 @@ HRESULT WgcCaptureSession::GetFrame(
RTC_DCHECK(is_capture_started_); RTC_DCHECK(is_capture_started_);
if (frames_in_pool_ < 1) if (frames_in_pool_ < 1)
wait_for_frame_event_.Wait(first_frame_ ? kMaxWaitForFirstFrameMs wait_for_frame_event_.Wait(first_frame_ ? kMaxWaitForFirstFrame
: kMaxWaitForFrameMs); : kMaxWaitForFrame);
ComPtr<WGC::IDirect3D11CaptureFrame> capture_frame; ComPtr<WGC::IDirect3D11CaptureFrame> capture_frame;
HRESULT hr = frame_pool_->TryGetNextFrame(&capture_frame); HRESULT hr = frame_pool_->TryGetNextFrame(&capture_frame);

View file

@ -50,29 +50,13 @@ class Event {
bool Wait(webrtc::TimeDelta give_up_after, webrtc::TimeDelta warn_after); bool Wait(webrtc::TimeDelta give_up_after, webrtc::TimeDelta warn_after);
// Waits with the given timeout and a reasonable default warning timeout. // Waits with the given timeout and a reasonable default warning timeout.
// TODO(bugs.webrtc.org/14366): De-template this after millisec-based Wait is bool Wait(webrtc::TimeDelta give_up_after) {
// removed. return Wait(give_up_after, give_up_after.IsPlusInfinity()
template <class T> ? webrtc::TimeDelta::Seconds(3)
bool Wait(T give_up_after) { : kForever);
webrtc::TimeDelta duration = ToTimeDelta(give_up_after);
return Wait(duration, duration.IsPlusInfinity()
? webrtc::TimeDelta::Seconds(3)
: kForever);
} }
private: private:
// TODO(bugs.webrtc.org/14366): Remove after millisec-based Wait is removed.
static webrtc::TimeDelta ToTimeDelta(int duration) {
// SocketServer users can get here with SocketServer::kForever which is
// -1. Mirror the definition here to avoid dependence.
constexpr int kForeverMs = -1;
return duration == kForeverMs ? kForever
: webrtc::TimeDelta::Millis(duration);
}
static webrtc::TimeDelta ToTimeDelta(webrtc::TimeDelta duration) {
return duration;
}
#if defined(WEBRTC_WIN) #if defined(WEBRTC_WIN)
HANDLE event_handle_; HANDLE event_handle_;
#elif defined(WEBRTC_POSIX) #elif defined(WEBRTC_POSIX)

View file

@ -57,7 +57,7 @@ namespace jni {
// an event indicating that the test was OK. // an event indicating that the test was OK.
static const size_t kNumCallbacks = 10; static const size_t kNumCallbacks = 10;
// Max amount of time we wait for an event to be set while counting callbacks. // Max amount of time we wait for an event to be set while counting callbacks.
static const int kTestTimeOutInMilliseconds = 10 * 1000; static constexpr TimeDelta kTestTimeOut = TimeDelta::Seconds(10);
// Average number of audio callbacks per second assuming 10ms packet size. // Average number of audio callbacks per second assuming 10ms packet size.
static const size_t kNumCallbacksPerSecond = 100; static const size_t kNumCallbacksPerSecond = 100;
// Play out a test file during this time (unit is in seconds). // Play out a test file during this time (unit is in seconds).
@ -66,7 +66,7 @@ static const size_t kBitsPerSample = 16;
static const size_t kBytesPerSample = kBitsPerSample / 8; static const size_t kBytesPerSample = kBitsPerSample / 8;
// Run the full-duplex test during this time (unit is in seconds). // Run the full-duplex test during this time (unit is in seconds).
// Note that first `kNumIgnoreFirstCallbacks` are ignored. // Note that first `kNumIgnoreFirstCallbacks` are ignored.
static const int kFullDuplexTimeInSec = 5; static constexpr TimeDelta kFullDuplexTime = TimeDelta::Seconds(5);
// Wait for the callback sequence to stabilize by ignoring this amount of the // Wait for the callback sequence to stabilize by ignoring this amount of the
// initial callbacks (avoids initial FIFO access). // initial callbacks (avoids initial FIFO access).
// Only used in the RunPlayoutAndRecordingInFullDuplex test. // Only used in the RunPlayoutAndRecordingInFullDuplex test.
@ -74,8 +74,8 @@ static const size_t kNumIgnoreFirstCallbacks = 50;
// Sets the number of impulses per second in the latency test. // Sets the number of impulses per second in the latency test.
static const int kImpulseFrequencyInHz = 1; static const int kImpulseFrequencyInHz = 1;
// Length of round-trip latency measurements. Number of transmitted impulses // Length of round-trip latency measurements. Number of transmitted impulses
// is kImpulseFrequencyInHz * kMeasureLatencyTimeInSec - 1. // is kImpulseFrequencyInHz * kMeasureLatencyTime - 1.
static const int kMeasureLatencyTimeInSec = 11; static constexpr TimeDelta kMeasureLatencyTime = TimeDelta::Seconds(11);
// Utilized in round-trip latency measurements to avoid capturing noise samples. // Utilized in round-trip latency measurements to avoid capturing noise samples.
static const int kImpulseThreshold = 1000; static const int kImpulseThreshold = 1000;
static const char kTag[] = "[..........] "; static const char kTag[] = "[..........] ";
@ -880,7 +880,7 @@ TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) {
.Times(AtLeast(kNumCallbacks)); .Times(AtLeast(kNumCallbacks));
EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
StartPlayout(); StartPlayout();
test_is_done_.Wait(kTestTimeOutInMilliseconds); test_is_done_.Wait(kTestTimeOut);
StopPlayout(); StopPlayout();
} }
@ -897,7 +897,7 @@ TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) {
EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
StartRecording(); StartRecording();
test_is_done_.Wait(kTestTimeOutInMilliseconds); test_is_done_.Wait(kTestTimeOut);
StopRecording(); StopRecording();
} }
@ -918,7 +918,7 @@ TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) {
EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
StartPlayout(); StartPlayout();
StartRecording(); StartRecording();
test_is_done_.Wait(kTestTimeOutInMilliseconds); test_is_done_.Wait(kTestTimeOut);
StopRecording(); StopRecording();
StopPlayout(); StopPlayout();
} }
@ -938,7 +938,7 @@ TEST_F(AudioDeviceTest, RunPlayoutWithFileAsSource) {
// SetMaxPlayoutVolume(); // SetMaxPlayoutVolume();
EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
StartPlayout(); StartPlayout();
test_is_done_.Wait(kTestTimeOutInMilliseconds); test_is_done_.Wait(kTestTimeOut);
StopPlayout(); StopPlayout();
} }
@ -1059,7 +1059,7 @@ TEST_F(AudioDeviceTest, AudioParametersWithNonDefaultConstruction) {
// one packet on average. However, under more realistic conditions, the size // one packet on average. However, under more realistic conditions, the size
// of the FIFO will vary more due to an unbalance between the two sides. // of the FIFO will vary more due to an unbalance between the two sides.
// This test tries to verify that the device maintains a balanced callback- // This test tries to verify that the device maintains a balanced callback-
// sequence by running in loopback for kFullDuplexTimeInSec seconds while // sequence by running in loopback for kFullDuplexTime seconds while
// measuring the size (max and average) of the FIFO. The size of the FIFO is // measuring the size (max and average) of the FIFO. The size of the FIFO is
// increased by the recording side and decreased by the playout side. // increased by the recording side and decreased by the playout side.
// TODO(henrika): tune the final test parameters after running tests on several // TODO(henrika): tune the final test parameters after running tests on several
@ -1077,13 +1077,12 @@ TEST_F(AudioDeviceTest, DISABLED_RunPlayoutAndRecordingInFullDuplex) {
std::unique_ptr<FifoAudioStream> fifo_audio_stream( std::unique_ptr<FifoAudioStream> fifo_audio_stream(
new FifoAudioStream(playout_frames_per_10ms_buffer())); new FifoAudioStream(playout_frames_per_10ms_buffer()));
mock.HandleCallbacks(&test_is_done_, fifo_audio_stream.get(), mock.HandleCallbacks(&test_is_done_, fifo_audio_stream.get(),
kFullDuplexTimeInSec * kNumCallbacksPerSecond); kFullDuplexTime.seconds() * kNumCallbacksPerSecond);
SetMaxPlayoutVolume(); SetMaxPlayoutVolume();
EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
StartRecording(); StartRecording();
StartPlayout(); StartPlayout();
test_is_done_.Wait( test_is_done_.Wait(std::max(kTestTimeOut, kFullDuplexTime));
std::max(kTestTimeOutInMilliseconds, 1000 * kFullDuplexTimeInSec));
StopPlayout(); StopPlayout();
StopRecording(); StopRecording();
@ -1110,20 +1109,19 @@ TEST_F(AudioDeviceTest, DISABLED_MeasureLoopbackLatency) {
std::unique_ptr<LatencyMeasuringAudioStream> latency_audio_stream( std::unique_ptr<LatencyMeasuringAudioStream> latency_audio_stream(
new LatencyMeasuringAudioStream(playout_frames_per_10ms_buffer())); new LatencyMeasuringAudioStream(playout_frames_per_10ms_buffer()));
mock.HandleCallbacks(&test_is_done_, latency_audio_stream.get(), mock.HandleCallbacks(&test_is_done_, latency_audio_stream.get(),
kMeasureLatencyTimeInSec * kNumCallbacksPerSecond); kMeasureLatencyTime.seconds() * kNumCallbacksPerSecond);
EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock)); EXPECT_EQ(0, audio_device()->RegisterAudioCallback(&mock));
SetMaxPlayoutVolume(); SetMaxPlayoutVolume();
DisableBuiltInAECIfAvailable(); DisableBuiltInAECIfAvailable();
StartRecording(); StartRecording();
StartPlayout(); StartPlayout();
test_is_done_.Wait( test_is_done_.Wait(std::max(kTestTimeOut, kMeasureLatencyTime));
std::max(kTestTimeOutInMilliseconds, 1000 * kMeasureLatencyTimeInSec));
StopPlayout(); StopPlayout();
StopRecording(); StopRecording();
// Verify that the correct number of transmitted impulses are detected. // Verify that the correct number of transmitted impulses are detected.
EXPECT_EQ(latency_audio_stream->num_latency_values(), EXPECT_EQ(latency_audio_stream->num_latency_values(),
static_cast<size_t>( static_cast<size_t>(
kImpulseFrequencyInHz * kMeasureLatencyTimeInSec - 1)); kImpulseFrequencyInHz * kMeasureLatencyTime.seconds() - 1));
latency_audio_stream->PrintResults(); latency_audio_stream->PrintResults();
} }

View file

@ -263,7 +263,7 @@ TEST(Stacktrace, TestRtcEventDeadlockDetection) {
// The message should appear after 3 sec. We'll wait up to 10 sec in an // The message should appear after 3 sec. We'll wait up to 10 sec in an
// attempt to not be flaky. // attempt to not be flaky.
EXPECT_TRUE(sink.WhenFound().Wait(10000)); EXPECT_TRUE(sink.WhenFound().Wait(TimeDelta::Seconds(10)));
// Unblock the thread and shut it down. // Unblock the thread and shut it down.
ev.Set(); ev.Set();

View file

@ -287,15 +287,15 @@ OCMLocation *OCMMakeLocation(id testCase, const char *fileCString, int line){
rtc::Event waitLock; rtc::Event waitLock;
rtc::Event waitCleanup; rtc::Event waitCleanup;
constexpr int timeoutMs = 5000; constexpr webrtc::TimeDelta timeout = webrtc::TimeDelta::Seconds(5);
thread->PostTask([audioSession, &waitLock, &waitCleanup] { thread->PostTask([audioSession, &waitLock, &waitCleanup, timeout] {
[audioSession lockForConfiguration]; [audioSession lockForConfiguration];
waitLock.Set(); waitLock.Set();
waitCleanup.Wait(timeoutMs); waitCleanup.Wait(timeout);
[audioSession unlockForConfiguration]; [audioSession unlockForConfiguration];
}); });
waitLock.Wait(timeoutMs); waitLock.Wait(timeout);
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:0 error:&error]; [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:0 error:&error];
EXPECT_TRUE(error != nil); EXPECT_TRUE(error != nil);
EXPECT_EQ(error.domain, kRTCAudioSessionErrorDomain); EXPECT_EQ(error.domain, kRTCAudioSessionErrorDomain);