diff --git a/audio/voip/voip_core.cc b/audio/voip/voip_core.cc index 2a29033a2b..baab43b2ab 100644 --- a/audio/voip/voip_core.cc +++ b/audio/voip/voip_core.cc @@ -117,7 +117,7 @@ bool VoipCore::Init(rtc::scoped_refptr encoder_factory, absl::optional VoipCore::CreateChannel( Transport* transport, absl::optional local_ssrc) { - absl::optional channel; + absl::optional channel_id; // Set local ssrc to random if not set by caller. if (!local_ssrc) { @@ -125,7 +125,7 @@ absl::optional VoipCore::CreateChannel( local_ssrc = random.Rand(); } - rtc::scoped_refptr audio_channel = + rtc::scoped_refptr channel = new rtc::RefCountedObject( transport, local_ssrc.value(), task_queue_factory_.get(), process_thread_.get(), audio_mixer_.get(), decoder_factory_); @@ -139,8 +139,8 @@ absl::optional VoipCore::CreateChannel( // Start process thread if the channel is the first one. start_process_thread = channels_.empty(); - channel = static_cast(next_channel_id_); - channels_[*channel] = audio_channel; + channel_id = static_cast(next_channel_id_); + channels_[*channel_id] = channel; next_channel_id_++; if (next_channel_id_ >= kMaxChannelId) { next_channel_id_ = 0; @@ -148,40 +148,40 @@ absl::optional VoipCore::CreateChannel( } // Set ChannelId in audio channel for logging/debugging purpose. - audio_channel->SetId(*channel); + channel->SetId(*channel_id); if (start_process_thread) { process_thread_->Start(); } - return channel; + return channel_id; } -void VoipCore::ReleaseChannel(ChannelId channel) { +void VoipCore::ReleaseChannel(ChannelId channel_id) { // Destroy channel outside of the lock. - rtc::scoped_refptr audio_channel; + rtc::scoped_refptr channel; bool no_channels_after_release = false; { MutexLock lock(&lock_); - auto iter = channels_.find(channel); + auto iter = channels_.find(channel_id); if (iter != channels_.end()) { - audio_channel = std::move(iter->second); + channel = std::move(iter->second); channels_.erase(iter); } no_channels_after_release = channels_.empty(); } - if (!audio_channel) { - RTC_LOG(LS_WARNING) << "Channel " << channel << " not found"; + if (!channel) { + RTC_LOG(LS_WARNING) << "Channel " << channel_id << " not found"; } if (no_channels_after_release) { // Release audio channel first to have it DeRegisterModule first. - audio_channel = nullptr; + channel = nullptr; process_thread_->Stop(); // Make sure to stop playout on ADM if it is playing. @@ -193,19 +193,19 @@ void VoipCore::ReleaseChannel(ChannelId channel) { } } -rtc::scoped_refptr VoipCore::GetChannel(ChannelId channel) { - rtc::scoped_refptr audio_channel; +rtc::scoped_refptr VoipCore::GetChannel(ChannelId channel_id) { + rtc::scoped_refptr channel; { MutexLock lock(&lock_); - auto iter = channels_.find(channel); + auto iter = channels_.find(channel_id); if (iter != channels_.end()) { - audio_channel = iter->second; + channel = iter->second; } } - if (!audio_channel) { - RTC_LOG(LS_ERROR) << "Channel " << channel << " not found"; + if (!channel) { + RTC_LOG(LS_ERROR) << "Channel " << channel_id << " not found"; } - return audio_channel; + return channel; } bool VoipCore::UpdateAudioTransportWithSenders() { @@ -263,37 +263,40 @@ bool VoipCore::UpdateAudioTransportWithSenders() { return true; } -bool VoipCore::StartSend(ChannelId channel) { - auto audio_channel = GetChannel(channel); - if (!audio_channel || !audio_channel->StartSend()) { +bool VoipCore::StartSend(ChannelId channel_id) { + rtc::scoped_refptr channel = GetChannel(channel_id); + + if (!channel || !channel->StartSend()) { return false; } return UpdateAudioTransportWithSenders(); } -bool VoipCore::StopSend(ChannelId channel) { - auto audio_channel = GetChannel(channel); - if (!audio_channel) { +bool VoipCore::StopSend(ChannelId channel_id) { + rtc::scoped_refptr channel = GetChannel(channel_id); + + if (!channel) { return false; } - audio_channel->StopSend(); + channel->StopSend(); return UpdateAudioTransportWithSenders(); } -bool VoipCore::StartPlayout(ChannelId channel) { - auto audio_channel = GetChannel(channel); - if (!audio_channel) { +bool VoipCore::StartPlayout(ChannelId channel_id) { + rtc::scoped_refptr channel = GetChannel(channel_id); + + if (!channel) { return false; } - if (audio_channel->IsPlaying()) { + if (channel->IsPlaying()) { return true; } - if (!audio_channel->StartPlay()) { + if (!channel->StartPlay()) { return false; } @@ -310,77 +313,86 @@ bool VoipCore::StartPlayout(ChannelId channel) { return true; } -bool VoipCore::StopPlayout(ChannelId channel) { - auto audio_channel = GetChannel(channel); - if (!audio_channel) { +bool VoipCore::StopPlayout(ChannelId channel_id) { + rtc::scoped_refptr channel = GetChannel(channel_id); + + if (!channel) { return false; } - audio_channel->StopPlay(); + channel->StopPlay(); return true; } -void VoipCore::ReceivedRTPPacket(ChannelId channel, +void VoipCore::ReceivedRTPPacket(ChannelId channel_id, rtc::ArrayView rtp_packet) { - // Failure to locate channel is logged internally in GetChannel. - if (auto audio_channel = GetChannel(channel)) { - audio_channel->ReceivedRTPPacket(rtp_packet); + rtc::scoped_refptr channel = GetChannel(channel_id); + + if (channel) { + channel->ReceivedRTPPacket(rtp_packet); } } -void VoipCore::ReceivedRTCPPacket(ChannelId channel, +void VoipCore::ReceivedRTCPPacket(ChannelId channel_id, rtc::ArrayView rtcp_packet) { - // Failure to locate channel is logged internally in GetChannel. - if (auto audio_channel = GetChannel(channel)) { - audio_channel->ReceivedRTCPPacket(rtcp_packet); + rtc::scoped_refptr channel = GetChannel(channel_id); + + if (channel) { + channel->ReceivedRTCPPacket(rtcp_packet); } } -void VoipCore::SetSendCodec(ChannelId channel, +void VoipCore::SetSendCodec(ChannelId channel_id, int payload_type, const SdpAudioFormat& encoder_format) { - // Failure to locate channel is logged internally in GetChannel. - if (auto audio_channel = GetChannel(channel)) { + rtc::scoped_refptr channel = GetChannel(channel_id); + + if (channel) { auto encoder = encoder_factory_->MakeAudioEncoder( payload_type, encoder_format, absl::nullopt); - audio_channel->SetEncoder(payload_type, encoder_format, std::move(encoder)); + channel->SetEncoder(payload_type, encoder_format, std::move(encoder)); } } void VoipCore::SetReceiveCodecs( - ChannelId channel, + ChannelId channel_id, const std::map& decoder_specs) { - // Failure to locate channel is logged internally in GetChannel. - if (auto audio_channel = GetChannel(channel)) { - audio_channel->SetReceiveCodecs(decoder_specs); + rtc::scoped_refptr channel = GetChannel(channel_id); + + if (channel) { + channel->SetReceiveCodecs(decoder_specs); } } -void VoipCore::RegisterTelephoneEventType(ChannelId channel, +void VoipCore::RegisterTelephoneEventType(ChannelId channel_id, int rtp_payload_type, int sample_rate_hz) { - // Failure to locate channel is logged internally in GetChannel. - if (auto audio_channel = GetChannel(channel)) { - audio_channel->RegisterTelephoneEventType(rtp_payload_type, sample_rate_hz); + rtc::scoped_refptr channel = GetChannel(channel_id); + + if (channel) { + channel->RegisterTelephoneEventType(rtp_payload_type, sample_rate_hz); } } -bool VoipCore::SendDtmfEvent(ChannelId channel, +bool VoipCore::SendDtmfEvent(ChannelId channel_id, DtmfEvent dtmf_event, int duration_ms) { - // Failure to locate channel is logged internally in GetChannel. - if (auto audio_channel = GetChannel(channel)) { - return audio_channel->SendTelephoneEvent(static_cast(dtmf_event), - duration_ms); + rtc::scoped_refptr channel = GetChannel(channel_id); + + if (channel) { + return channel->SendTelephoneEvent(static_cast(dtmf_event), + duration_ms); } return false; } absl::optional VoipCore::GetIngressStatistics( - ChannelId channel) { - if (auto audio_channel = GetChannel(channel)) { - return audio_channel->GetIngressStatistics(); + ChannelId channel_id) { + rtc::scoped_refptr channel = GetChannel(channel_id); + + if (channel) { + return channel->GetIngressStatistics(); } return absl::nullopt; } diff --git a/audio/voip/voip_core.h b/audio/voip/voip_core.h index 8c1a44c338..950d2bba84 100644 --- a/audio/voip/voip_core.h +++ b/audio/voip/voip_core.h @@ -78,42 +78,42 @@ class VoipCore : public VoipEngine, absl::optional CreateChannel( Transport* transport, absl::optional local_ssrc) override; - void ReleaseChannel(ChannelId channel) override; - bool StartSend(ChannelId channel) override; - bool StopSend(ChannelId channel) override; - bool StartPlayout(ChannelId channel) override; - bool StopPlayout(ChannelId channel) override; + void ReleaseChannel(ChannelId channel_id) override; + bool StartSend(ChannelId channel_id) override; + bool StopSend(ChannelId channel_id) override; + bool StartPlayout(ChannelId channel_id) override; + bool StopPlayout(ChannelId channel_id) override; // Implements VoipNetwork interfaces. - void ReceivedRTPPacket(ChannelId channel, + void ReceivedRTPPacket(ChannelId channel_id, rtc::ArrayView rtp_packet) override; - void ReceivedRTCPPacket(ChannelId channel, + void ReceivedRTCPPacket(ChannelId channel_id, rtc::ArrayView rtcp_packet) override; // Implements VoipCodec interfaces. - void SetSendCodec(ChannelId channel, + void SetSendCodec(ChannelId channel_id, int payload_type, const SdpAudioFormat& encoder_format) override; void SetReceiveCodecs( - ChannelId channel, + ChannelId channel_id, const std::map& decoder_specs) override; // Implements VoipDtmf interfaces. - void RegisterTelephoneEventType(ChannelId channel, + void RegisterTelephoneEventType(ChannelId channel_id, int rtp_payload_type, int sample_rate_hz) override; - bool SendDtmfEvent(ChannelId channel, + bool SendDtmfEvent(ChannelId channel_id, DtmfEvent dtmf_event, int duration_ms) override; // Implements VoipStatistics interfaces. absl::optional GetIngressStatistics( - ChannelId channel) override; + ChannelId channel_id) override; private: // Fetches the corresponding AudioChannel assigned with given |channel|. // Returns nullptr if not found. - rtc::scoped_refptr GetChannel(ChannelId channel); + rtc::scoped_refptr GetChannel(ChannelId channel_id); // Updates AudioTransportImpl with a new set of actively sending AudioSender // (AudioEgress). This needs to be invoked whenever StartSend/StopSend is