Skip to content

Commit

Permalink
Delete deprecated PlatformThread looping
Browse files Browse the repository at this point in the history
Bug: webrtc:10594, webrtc:7187
Change-Id: Icba3a5cf6dbe817ead427c27645b3ad7bc8819be
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/134642
Commit-Queue: Niels Moller <nisse@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Tommi <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27833}
  • Loading branch information
Niels Möller authored and Commit Bot committed May 3, 2019
1 parent da87648 commit 4731f00
Show file tree
Hide file tree
Showing 22 changed files with 251 additions and 353 deletions.
96 changes: 61 additions & 35 deletions modules/audio_coding/acm2/audio_coding_module_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <stdio.h>
#include <string.h>
#include <atomic>
#include <memory>
#include <vector>

Expand Down Expand Up @@ -484,9 +485,15 @@ class AudioCodingModuleMtTestOldApi : public AudioCodingModuleTestOldApi {

AudioCodingModuleMtTestOldApi()
: AudioCodingModuleTestOldApi(),
send_thread_(CbSendThread, this, "send"),
insert_packet_thread_(CbInsertPacketThread, this, "insert_packet"),
pull_audio_thread_(CbPullAudioThread, this, "pull_audio"),
send_thread_(CbSendThread, this, "send", rtc::kRealtimePriority),
insert_packet_thread_(CbInsertPacketThread,
this,
"insert_packet",
rtc::kRealtimePriority),
pull_audio_thread_(CbPullAudioThread,
this,
"pull_audio",
rtc::kRealtimePriority),
send_count_(0),
insert_packet_count_(0),
pull_audio_count_(0),
Expand All @@ -502,16 +509,15 @@ class AudioCodingModuleMtTestOldApi : public AudioCodingModuleTestOldApi {
}

void StartThreads() {
quit_.store(false);
send_thread_.Start();
send_thread_.SetPriority(rtc::kRealtimePriority);
insert_packet_thread_.Start();
insert_packet_thread_.SetPriority(rtc::kRealtimePriority);
pull_audio_thread_.Start();
pull_audio_thread_.SetPriority(rtc::kRealtimePriority);
}

void TearDown() {
AudioCodingModuleTestOldApi::TearDown();
quit_.store(true);
pull_audio_thread_.Stop();
send_thread_.Stop();
insert_packet_thread_.Stop();
Expand All @@ -532,14 +538,17 @@ class AudioCodingModuleMtTestOldApi : public AudioCodingModuleTestOldApi {
return false;
}

static bool CbSendThread(void* context) {
return reinterpret_cast<AudioCodingModuleMtTestOldApi*>(context)
->CbSendImpl();
static void CbSendThread(void* context) {
AudioCodingModuleMtTestOldApi* fixture =
reinterpret_cast<AudioCodingModuleMtTestOldApi*>(context);
while (!fixture->quit_.load()) {
fixture->CbSendImpl();
}
}

// The send thread doesn't have to care about the current simulated time,
// since only the AcmReceiver is using the clock.
bool CbSendImpl() {
void CbSendImpl() {
SleepMs(1);
if (HasFatalFailure()) {
// End the test early if a fatal failure (ASSERT_*) has occurred.
Expand All @@ -550,53 +559,59 @@ class AudioCodingModuleMtTestOldApi : public AudioCodingModuleTestOldApi {
if (TestDone()) {
test_complete_.Set();
}
return true;
}

static bool CbInsertPacketThread(void* context) {
return reinterpret_cast<AudioCodingModuleMtTestOldApi*>(context)
->CbInsertPacketImpl();
static void CbInsertPacketThread(void* context) {
AudioCodingModuleMtTestOldApi* fixture =
reinterpret_cast<AudioCodingModuleMtTestOldApi*>(context);
while (!fixture->quit_.load()) {
fixture->CbInsertPacketImpl();
}
}

bool CbInsertPacketImpl() {
void CbInsertPacketImpl() {
SleepMs(1);
{
rtc::CritScope lock(&crit_sect_);
if (clock_->TimeInMilliseconds() < next_insert_packet_time_ms_) {
return true;
return;
}
next_insert_packet_time_ms_ += 10;
}
// Now we're not holding the crit sect when calling ACM.
++insert_packet_count_;
InsertPacket();
return true;
}

static bool CbPullAudioThread(void* context) {
return reinterpret_cast<AudioCodingModuleMtTestOldApi*>(context)
->CbPullAudioImpl();
static void CbPullAudioThread(void* context) {
AudioCodingModuleMtTestOldApi* fixture =
reinterpret_cast<AudioCodingModuleMtTestOldApi*>(context);
while (!fixture->quit_.load()) {
fixture->CbPullAudioImpl();
}
}

bool CbPullAudioImpl() {
void CbPullAudioImpl() {
SleepMs(1);
{
rtc::CritScope lock(&crit_sect_);
// Don't let the insert thread fall behind.
if (next_insert_packet_time_ms_ < clock_->TimeInMilliseconds()) {
return true;
return;
}
++pull_audio_count_;
}
// Now we're not holding the crit sect when calling ACM.
PullAudio();
fake_clock_->AdvanceTimeMilliseconds(10);
return true;
}

rtc::PlatformThread send_thread_;
rtc::PlatformThread insert_packet_thread_;
rtc::PlatformThread pull_audio_thread_;
// Used to force worker threads to stop looping.
std::atomic<bool> quit_;

rtc::Event test_complete_;
int send_count_;
int insert_packet_count_;
Expand Down Expand Up @@ -734,10 +749,14 @@ class AcmReRegisterIsacMtTestOldApi : public AudioCodingModuleTestOldApi {

AcmReRegisterIsacMtTestOldApi()
: AudioCodingModuleTestOldApi(),
receive_thread_(CbReceiveThread, this, "receive"),
receive_thread_(CbReceiveThread,
this,
"receive",
rtc::kRealtimePriority),
codec_registration_thread_(CbCodecRegistrationThread,
this,
"codec_registration"),
"codec_registration",
rtc::kRealtimePriority),
codec_registered_(false),
receive_packet_count_(0),
next_insert_packet_time_ms_(0),
Expand Down Expand Up @@ -768,14 +787,14 @@ class AcmReRegisterIsacMtTestOldApi : public AudioCodingModuleTestOldApi {
}

void StartThreads() {
quit_.store(false);
receive_thread_.Start();
receive_thread_.SetPriority(rtc::kRealtimePriority);
codec_registration_thread_.Start();
codec_registration_thread_.SetPriority(rtc::kRealtimePriority);
}

void TearDown() override {
AudioCodingModuleTestOldApi::TearDown();
quit_.store(true);
receive_thread_.Stop();
codec_registration_thread_.Stop();
}
Expand All @@ -784,9 +803,11 @@ class AcmReRegisterIsacMtTestOldApi : public AudioCodingModuleTestOldApi {
return test_complete_.Wait(10 * 60 * 1000); // 10 minutes' timeout.
}

static bool CbReceiveThread(void* context) {
return reinterpret_cast<AcmReRegisterIsacMtTestOldApi*>(context)
->CbReceiveImpl();
static void CbReceiveThread(void* context) {
AcmReRegisterIsacMtTestOldApi* fixture =
reinterpret_cast<AcmReRegisterIsacMtTestOldApi*>(context);
while (!fixture->quit_.load() && fixture->CbReceiveImpl()) {
}
}

bool CbReceiveImpl() {
Expand Down Expand Up @@ -834,12 +855,15 @@ class AcmReRegisterIsacMtTestOldApi : public AudioCodingModuleTestOldApi {
return true;
}

static bool CbCodecRegistrationThread(void* context) {
return reinterpret_cast<AcmReRegisterIsacMtTestOldApi*>(context)
->CbCodecRegistrationImpl();
static void CbCodecRegistrationThread(void* context) {
AcmReRegisterIsacMtTestOldApi* fixture =
reinterpret_cast<AcmReRegisterIsacMtTestOldApi*>(context);
while (!fixture->quit_.load()) {
fixture->CbCodecRegistrationImpl();
}
}

bool CbCodecRegistrationImpl() {
void CbCodecRegistrationImpl() {
SleepMs(1);
if (HasFatalFailure()) {
// End the test early if a fatal failure (ASSERT_*) has occurred.
Expand All @@ -856,11 +880,13 @@ class AcmReRegisterIsacMtTestOldApi : public AudioCodingModuleTestOldApi {
if (codec_registered_ && receive_packet_count_ > kNumPackets) {
test_complete_.Set();
}
return true;
}

rtc::PlatformThread receive_thread_;
rtc::PlatformThread codec_registration_thread_;
// Used to force worker threads to stop looping.
std::atomic<bool> quit_;

rtc::Event test_complete_;
rtc::CriticalSection crit_sect_;
bool codec_registered_ RTC_GUARDED_BY(crit_sect_);
Expand Down
20 changes: 12 additions & 8 deletions modules/audio_device/dummy/file_audio_device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ int32_t FileAudioDevice::StartPlayout() {
}

_ptrThreadPlay.reset(new rtc::PlatformThread(
PlayThreadFunc, this, "webrtc_audio_module_play_thread"));
PlayThreadFunc, this, "webrtc_audio_module_play_thread",
rtc::kRealtimePriority));
_ptrThreadPlay->Start();
_ptrThreadPlay->SetPriority(rtc::kRealtimePriority);

RTC_LOG(LS_INFO) << "Started playout capture to output file: "
<< _outputFilename;
Expand Down Expand Up @@ -277,10 +277,10 @@ int32_t FileAudioDevice::StartRecording() {
}

_ptrThreadRec.reset(new rtc::PlatformThread(
RecThreadFunc, this, "webrtc_audio_module_capture_thread"));
RecThreadFunc, this, "webrtc_audio_module_capture_thread",
rtc::kRealtimePriority));

_ptrThreadRec->Start();
_ptrThreadRec->SetPriority(rtc::kRealtimePriority);

RTC_LOG(LS_INFO) << "Started recording from input file: " << _inputFilename;

Expand Down Expand Up @@ -439,12 +439,16 @@ void FileAudioDevice::AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) {
_ptrAudioBuffer->SetPlayoutChannels(0);
}

bool FileAudioDevice::PlayThreadFunc(void* pThis) {
return (static_cast<FileAudioDevice*>(pThis)->PlayThreadProcess());
void FileAudioDevice::PlayThreadFunc(void* pThis) {
FileAudioDevice* device = static_cast<FileAudioDevice*>(pThis);
while (device->PlayThreadProcess()) {
}
}

bool FileAudioDevice::RecThreadFunc(void* pThis) {
return (static_cast<FileAudioDevice*>(pThis)->RecThreadProcess());
void FileAudioDevice::RecThreadFunc(void* pThis) {
FileAudioDevice* device = static_cast<FileAudioDevice*>(pThis);
while (device->RecThreadProcess()) {
}
}

bool FileAudioDevice::PlayThreadProcess() {
Expand Down
4 changes: 2 additions & 2 deletions modules/audio_device/dummy/file_audio_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ class FileAudioDevice : public AudioDeviceGeneric {
void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) override;

private:
static bool RecThreadFunc(void*);
static bool PlayThreadFunc(void*);
static void RecThreadFunc(void*);
static void PlayThreadFunc(void*);
bool RecThreadProcess();
bool PlayThreadProcess();

Expand Down
20 changes: 12 additions & 8 deletions modules/audio_device/linux/audio_device_alsa_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1027,10 +1027,10 @@ int32_t AudioDeviceLinuxALSA::StartRecording() {
}
// RECORDING
_ptrThreadRec.reset(new rtc::PlatformThread(
RecThreadFunc, this, "webrtc_audio_module_capture_thread"));
RecThreadFunc, this, "webrtc_audio_module_capture_thread",
rtc::kRealtimePriority));

_ptrThreadRec->Start();
_ptrThreadRec->SetPriority(rtc::kRealtimePriority);

errVal = LATE(snd_pcm_prepare)(_handleRecord);
if (errVal < 0) {
Expand Down Expand Up @@ -1145,9 +1145,9 @@ int32_t AudioDeviceLinuxALSA::StartPlayout() {

// PLAYOUT
_ptrThreadPlay.reset(new rtc::PlatformThread(
PlayThreadFunc, this, "webrtc_audio_module_play_thread"));
PlayThreadFunc, this, "webrtc_audio_module_play_thread",
rtc::kRealtimePriority));
_ptrThreadPlay->Start();
_ptrThreadPlay->SetPriority(rtc::kRealtimePriority);

int errVal = LATE(snd_pcm_prepare)(_handlePlayout);
if (errVal < 0) {
Expand Down Expand Up @@ -1456,12 +1456,16 @@ int32_t AudioDeviceLinuxALSA::ErrorRecovery(int32_t error,
// Thread Methods
// ============================================================================

bool AudioDeviceLinuxALSA::PlayThreadFunc(void* pThis) {
return (static_cast<AudioDeviceLinuxALSA*>(pThis)->PlayThreadProcess());
void AudioDeviceLinuxALSA::PlayThreadFunc(void* pThis) {
AudioDeviceLinuxALSA* device = static_cast<AudioDeviceLinuxALSA*>(pThis);
while (device->PlayThreadProcess()) {
}
}

bool AudioDeviceLinuxALSA::RecThreadFunc(void* pThis) {
return (static_cast<AudioDeviceLinuxALSA*>(pThis)->RecThreadProcess());
void AudioDeviceLinuxALSA::RecThreadFunc(void* pThis) {
AudioDeviceLinuxALSA* device = static_cast<AudioDeviceLinuxALSA*>(pThis);
while (device->RecThreadProcess()) {
}
}

bool AudioDeviceLinuxALSA::PlayThreadProcess() {
Expand Down
4 changes: 2 additions & 2 deletions modules/audio_device/linux/audio_device_alsa_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ class AudioDeviceLinuxALSA : public AudioDeviceGeneric {
inline int32_t InputSanityCheckAfterUnlockedPeriod() const;
inline int32_t OutputSanityCheckAfterUnlockedPeriod() const;

static bool RecThreadFunc(void*);
static bool PlayThreadFunc(void*);
static void RecThreadFunc(void*);
static void PlayThreadFunc(void*);
bool RecThreadProcess();
bool PlayThreadProcess();

Expand Down
Loading

0 comments on commit 4731f00

Please sign in to comment.