1 /*
2 * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "audio/voip/audio_channel.h"
12
13 #include <utility>
14 #include <vector>
15
16 #include "api/audio_codecs/audio_format.h"
17 #include "api/task_queue/task_queue_factory.h"
18 #include "modules/rtp_rtcp/include/receive_statistics.h"
19 #include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h"
20 #include "rtc_base/logging.h"
21
22 namespace webrtc {
23
24 namespace {
25
26 constexpr int kRtcpReportIntervalMs = 5000;
27
28 } // namespace
29
AudioChannel(Transport * transport,uint32_t local_ssrc,TaskQueueFactory * task_queue_factory,AudioMixer * audio_mixer,rtc::scoped_refptr<AudioDecoderFactory> decoder_factory)30 AudioChannel::AudioChannel(
31 Transport* transport,
32 uint32_t local_ssrc,
33 TaskQueueFactory* task_queue_factory,
34 AudioMixer* audio_mixer,
35 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory)
36 : audio_mixer_(audio_mixer) {
37 RTC_DCHECK(task_queue_factory);
38 RTC_DCHECK(audio_mixer);
39
40 Clock* clock = Clock::GetRealTimeClock();
41 receive_statistics_ = ReceiveStatistics::Create(clock);
42
43 RtpRtcpInterface::Configuration rtp_config;
44 rtp_config.clock = clock;
45 rtp_config.audio = true;
46 rtp_config.receive_statistics = receive_statistics_.get();
47 rtp_config.rtcp_report_interval_ms = kRtcpReportIntervalMs;
48 rtp_config.outgoing_transport = transport;
49 rtp_config.local_media_ssrc = local_ssrc;
50
51 rtp_rtcp_ = ModuleRtpRtcpImpl2::Create(rtp_config);
52
53 rtp_rtcp_->SetSendingMediaStatus(false);
54 rtp_rtcp_->SetRTCPStatus(RtcpMode::kCompound);
55
56 ingress_ = std::make_unique<AudioIngress>(rtp_rtcp_.get(), clock,
57 receive_statistics_.get(),
58 std::move(decoder_factory));
59 egress_ =
60 std::make_unique<AudioEgress>(rtp_rtcp_.get(), clock, task_queue_factory);
61
62 // Set the instance of audio ingress to be part of audio mixer for ADM to
63 // fetch audio samples to play.
64 audio_mixer_->AddSource(ingress_.get());
65 }
66
~AudioChannel()67 AudioChannel::~AudioChannel() {
68 if (egress_->IsSending()) {
69 StopSend();
70 }
71 if (ingress_->IsPlaying()) {
72 StopPlay();
73 }
74
75 audio_mixer_->RemoveSource(ingress_.get());
76
77 // TODO(bugs.webrtc.org/11581): unclear if we still need to clear `egress_`
78 // here.
79 egress_.reset();
80 ingress_.reset();
81 }
82
StartSend()83 bool AudioChannel::StartSend() {
84 // If encoder has not been set, return false.
85 if (!egress_->StartSend()) {
86 return false;
87 }
88
89 // Start sending with RTP stack if it has not been sending yet.
90 if (!rtp_rtcp_->Sending()) {
91 rtp_rtcp_->SetSendingStatus(true);
92 }
93 return true;
94 }
95
StopSend()96 void AudioChannel::StopSend() {
97 egress_->StopSend();
98
99 // Deactivate RTP stack when both sending and receiving are stopped.
100 // SetSendingStatus(false) triggers the transmission of RTCP BYE
101 // message to remote endpoint.
102 if (!ingress_->IsPlaying() && rtp_rtcp_->Sending()) {
103 rtp_rtcp_->SetSendingStatus(false);
104 }
105 }
106
StartPlay()107 bool AudioChannel::StartPlay() {
108 // If decoders have not been set, return false.
109 if (!ingress_->StartPlay()) {
110 return false;
111 }
112
113 // If RTP stack is not sending then start sending as in recv-only mode, RTCP
114 // receiver report is expected.
115 if (!rtp_rtcp_->Sending()) {
116 rtp_rtcp_->SetSendingStatus(true);
117 }
118 return true;
119 }
120
StopPlay()121 void AudioChannel::StopPlay() {
122 ingress_->StopPlay();
123
124 // Deactivate RTP stack only when both sending and receiving are stopped.
125 if (!rtp_rtcp_->SendingMedia() && rtp_rtcp_->Sending()) {
126 rtp_rtcp_->SetSendingStatus(false);
127 }
128 }
129
GetIngressStatistics()130 IngressStatistics AudioChannel::GetIngressStatistics() {
131 IngressStatistics ingress_stats;
132 NetworkStatistics stats = ingress_->GetNetworkStatistics();
133 ingress_stats.neteq_stats.total_samples_received = stats.totalSamplesReceived;
134 ingress_stats.neteq_stats.concealed_samples = stats.concealedSamples;
135 ingress_stats.neteq_stats.concealment_events = stats.concealmentEvents;
136 ingress_stats.neteq_stats.jitter_buffer_delay_ms = stats.jitterBufferDelayMs;
137 ingress_stats.neteq_stats.jitter_buffer_emitted_count =
138 stats.jitterBufferEmittedCount;
139 ingress_stats.neteq_stats.jitter_buffer_target_delay_ms =
140 stats.jitterBufferTargetDelayMs;
141 ingress_stats.neteq_stats.inserted_samples_for_deceleration =
142 stats.insertedSamplesForDeceleration;
143 ingress_stats.neteq_stats.removed_samples_for_acceleration =
144 stats.removedSamplesForAcceleration;
145 ingress_stats.neteq_stats.silent_concealed_samples =
146 stats.silentConcealedSamples;
147 ingress_stats.neteq_stats.fec_packets_received = stats.fecPacketsReceived;
148 ingress_stats.neteq_stats.fec_packets_discarded = stats.fecPacketsDiscarded;
149 ingress_stats.neteq_stats.delayed_packet_outage_samples =
150 stats.delayedPacketOutageSamples;
151 ingress_stats.neteq_stats.relative_packet_arrival_delay_ms =
152 stats.relativePacketArrivalDelayMs;
153 ingress_stats.neteq_stats.interruption_count = stats.interruptionCount;
154 ingress_stats.neteq_stats.total_interruption_duration_ms =
155 stats.totalInterruptionDurationMs;
156 ingress_stats.total_duration = ingress_->GetOutputTotalDuration();
157 return ingress_stats;
158 }
159
GetChannelStatistics()160 ChannelStatistics AudioChannel::GetChannelStatistics() {
161 ChannelStatistics channel_stat = ingress_->GetChannelStatistics();
162
163 StreamDataCounters rtp_stats, rtx_stats;
164 rtp_rtcp_->GetSendStreamDataCounters(&rtp_stats, &rtx_stats);
165 channel_stat.bytes_sent =
166 rtp_stats.transmitted.payload_bytes + rtx_stats.transmitted.payload_bytes;
167 channel_stat.packets_sent =
168 rtp_stats.transmitted.packets + rtx_stats.transmitted.packets;
169
170 return channel_stat;
171 }
172
173 } // namespace webrtc
174