Start/stop ProcessThread as first/last audio channel is added/removed.

Bug: webrtc:11989
Change-Id: I3fb068f53693b1b59759dac64022844ed3a25075
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/185361
Reviewed-by: Per Åhgren <peah@webrtc.org>
Commit-Queue: Tim Na <natim@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#32218}
This commit is contained in:
Tim Na 2020-09-24 17:25:15 -07:00 committed by Commit Bot
parent 6ecfbb5155
commit 6154a74e54

View File

@ -126,9 +126,15 @@ absl::optional<ChannelId> VoipCore::CreateChannel(
transport, local_ssrc.value(), task_queue_factory_.get(),
process_thread_.get(), audio_mixer_.get(), decoder_factory_);
// Check if we need to start the process thread.
bool start_process_thread = false;
{
MutexLock lock(&lock_);
// Start process thread if the channel is the first one.
start_process_thread = channels_.empty();
channel = static_cast<ChannelId>(next_channel_id_);
channels_[*channel] = audio_channel;
next_channel_id_++;
@ -140,12 +146,20 @@ absl::optional<ChannelId> VoipCore::CreateChannel(
// Set ChannelId in audio channel for logging/debugging purpose.
audio_channel->SetId(*channel);
if (start_process_thread) {
process_thread_->Start();
}
return channel;
}
void VoipCore::ReleaseChannel(ChannelId channel) {
// Destroy channel outside of the lock.
rtc::scoped_refptr<AudioChannel> audio_channel;
// Check if process thread is no longer needed.
bool stop_process_thread = false;
{
MutexLock lock(&lock_);
@ -154,10 +168,20 @@ void VoipCore::ReleaseChannel(ChannelId channel) {
audio_channel = std::move(iter->second);
channels_.erase(iter);
}
// Check if this is the last channel we have.
stop_process_thread = channels_.empty();
}
if (!audio_channel) {
RTC_LOG(LS_WARNING) << "Channel " << channel << " not found";
}
if (stop_process_thread) {
// Release audio channel first to have it DeRegisterModule first.
audio_channel = nullptr;
process_thread_->Stop();
}
}
rtc::scoped_refptr<AudioChannel> VoipCore::GetChannel(ChannelId channel) {