blob: d1c3a4bac69ec767a69f86ccb1745932f2b35a85 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrika@webrtc.org2919e952012-01-31 08:45:03 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000011#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Henrik Lundin64dad832015-05-11 12:44:23 +020013#include <algorithm>
Tommif888bb52015-12-12 01:37:01 +010014#include <utility>
Henrik Lundin64dad832015-05-11 12:44:23 +020015
kwiberg529662a2017-09-04 05:43:17 -070016#include "webrtc/api/array_view.h"
aleloi6321b492016-12-05 01:46:09 -080017#include "webrtc/audio/utility/audio_frame_operations.h"
nissecae45d02017-04-24 05:53:20 -070018#include "webrtc/call/rtp_transport_controller_send_interface.h"
skvladcc91d282016-10-03 18:31:22 -070019#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070020#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000021#include "webrtc/modules/audio_device/include/audio_device.h"
22#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010023#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010024#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010025#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
26#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
27#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
nisse657bab22017-02-21 06:28:10 -080028#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000029#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/utility/include/process_thread.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020031#include "webrtc/rtc_base/checks.h"
32#include "webrtc/rtc_base/criticalsection.h"
33#include "webrtc/rtc_base/format_macros.h"
34#include "webrtc/rtc_base/location.h"
35#include "webrtc/rtc_base/logging.h"
36#include "webrtc/rtc_base/rate_limiter.h"
37#include "webrtc/rtc_base/task_queue.h"
38#include "webrtc/rtc_base/thread_checker.h"
39#include "webrtc/rtc_base/timeutils.h"
elad.alon28770482017-03-28 05:03:55 -070040#include "webrtc/system_wrappers/include/field_trial.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010041#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000042#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
43#include "webrtc/voice_engine/output_mixer.h"
44#include "webrtc/voice_engine/statistics.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000045#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000046
andrew@webrtc.org50419b02012-11-14 19:07:54 +000047namespace webrtc {
48namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000049
kwibergc8d071e2016-04-06 12:22:38 -070050namespace {
51
zsteine76bd3a2017-07-14 12:17:49 -070052constexpr double kAudioSampleDurationSeconds = 0.01;
Erik Språng737336d2016-07-29 12:59:36 +020053constexpr int64_t kMaxRetransmissionWindowMs = 1000;
54constexpr int64_t kMinRetransmissionWindowMs = 30;
55
kwibergc8d071e2016-04-06 12:22:38 -070056} // namespace
57
solenberg8842c3e2016-03-11 03:06:41 -080058const int kTelephoneEventAttenuationdB = 10;
59
ivoc14d5dbe2016-07-04 07:06:55 -070060class RtcEventLogProxy final : public webrtc::RtcEventLog {
61 public:
62 RtcEventLogProxy() : event_log_(nullptr) {}
63
64 bool StartLogging(const std::string& file_name,
65 int64_t max_size_bytes) override {
66 RTC_NOTREACHED();
67 return false;
68 }
69
70 bool StartLogging(rtc::PlatformFile log_file,
71 int64_t max_size_bytes) override {
72 RTC_NOTREACHED();
73 return false;
74 }
75
76 void StopLogging() override { RTC_NOTREACHED(); }
77
78 void LogVideoReceiveStreamConfig(
perkj09e71da2017-05-22 03:26:49 -070079 const webrtc::rtclog::StreamConfig&) override {
80 RTC_NOTREACHED();
ivoc14d5dbe2016-07-04 07:06:55 -070081 }
82
perkjc0876aa2017-05-22 04:08:28 -070083 void LogVideoSendStreamConfig(const webrtc::rtclog::StreamConfig&) override {
84 RTC_NOTREACHED();
ivoc14d5dbe2016-07-04 07:06:55 -070085 }
86
ivoce0928d82016-10-10 05:12:51 -070087 void LogAudioReceiveStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -070088 const webrtc::rtclog::StreamConfig& config) override {
ivoce0928d82016-10-10 05:12:51 -070089 rtc::CritScope lock(&crit_);
90 if (event_log_) {
91 event_log_->LogAudioReceiveStreamConfig(config);
92 }
93 }
94
95 void LogAudioSendStreamConfig(
perkjf4726992017-05-22 10:12:26 -070096 const webrtc::rtclog::StreamConfig& config) override {
ivoce0928d82016-10-10 05:12:51 -070097 rtc::CritScope lock(&crit_);
98 if (event_log_) {
99 event_log_->LogAudioSendStreamConfig(config);
100 }
101 }
102
ivoc14d5dbe2016-07-04 07:06:55 -0700103 void LogRtpHeader(webrtc::PacketDirection direction,
ivoc14d5dbe2016-07-04 07:06:55 -0700104 const uint8_t* header,
105 size_t packet_length) override {
perkj77cd58e2017-05-30 03:52:10 -0700106 LogRtpHeader(direction, header, packet_length, PacedPacketInfo::kNotAProbe);
philipel32d00102017-02-27 02:18:46 -0800107 }
108
109 void LogRtpHeader(webrtc::PacketDirection direction,
philipel32d00102017-02-27 02:18:46 -0800110 const uint8_t* header,
111 size_t packet_length,
112 int probe_cluster_id) override {
ivoc14d5dbe2016-07-04 07:06:55 -0700113 rtc::CritScope lock(&crit_);
114 if (event_log_) {
perkj77cd58e2017-05-30 03:52:10 -0700115 event_log_->LogRtpHeader(direction, header, packet_length,
philipel32d00102017-02-27 02:18:46 -0800116 probe_cluster_id);
ivoc14d5dbe2016-07-04 07:06:55 -0700117 }
118 }
119
120 void LogRtcpPacket(webrtc::PacketDirection direction,
ivoc14d5dbe2016-07-04 07:06:55 -0700121 const uint8_t* packet,
122 size_t length) override {
123 rtc::CritScope lock(&crit_);
124 if (event_log_) {
perkj77cd58e2017-05-30 03:52:10 -0700125 event_log_->LogRtcpPacket(direction, packet, length);
ivoc14d5dbe2016-07-04 07:06:55 -0700126 }
127 }
128
129 void LogAudioPlayout(uint32_t ssrc) override {
130 rtc::CritScope lock(&crit_);
131 if (event_log_) {
132 event_log_->LogAudioPlayout(ssrc);
133 }
134 }
135
terelius424e6cf2017-02-20 05:14:41 -0800136 void LogLossBasedBweUpdate(int32_t bitrate_bps,
ivoc14d5dbe2016-07-04 07:06:55 -0700137 uint8_t fraction_loss,
138 int32_t total_packets) override {
139 rtc::CritScope lock(&crit_);
140 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800141 event_log_->LogLossBasedBweUpdate(bitrate_bps, fraction_loss,
142 total_packets);
ivoc14d5dbe2016-07-04 07:06:55 -0700143 }
144 }
145
terelius424e6cf2017-02-20 05:14:41 -0800146 void LogDelayBasedBweUpdate(int32_t bitrate_bps,
terelius0baf55d2017-02-17 03:38:28 -0800147 BandwidthUsage detector_state) override {
148 rtc::CritScope lock(&crit_);
149 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800150 event_log_->LogDelayBasedBweUpdate(bitrate_bps, detector_state);
terelius0baf55d2017-02-17 03:38:28 -0800151 }
152 }
153
minyue4b7c9522017-01-24 04:54:59 -0800154 void LogAudioNetworkAdaptation(
michaeltcde46b72017-04-06 05:59:10 -0700155 const AudioEncoderRuntimeConfig& config) override {
minyue4b7c9522017-01-24 04:54:59 -0800156 rtc::CritScope lock(&crit_);
157 if (event_log_) {
158 event_log_->LogAudioNetworkAdaptation(config);
159 }
160 }
161
philipel32d00102017-02-27 02:18:46 -0800162 void LogProbeClusterCreated(int id,
163 int bitrate_bps,
164 int min_probes,
165 int min_bytes) override {
166 rtc::CritScope lock(&crit_);
167 if (event_log_) {
168 event_log_->LogProbeClusterCreated(id, bitrate_bps, min_probes,
169 min_bytes);
170 }
171 };
172
173 void LogProbeResultSuccess(int id, int bitrate_bps) override {
174 rtc::CritScope lock(&crit_);
175 if (event_log_) {
176 event_log_->LogProbeResultSuccess(id, bitrate_bps);
177 }
178 };
179
180 void LogProbeResultFailure(int id,
181 ProbeFailureReason failure_reason) override {
182 rtc::CritScope lock(&crit_);
183 if (event_log_) {
184 event_log_->LogProbeResultFailure(id, failure_reason);
185 }
186 };
187
ivoc14d5dbe2016-07-04 07:06:55 -0700188 void SetEventLog(RtcEventLog* event_log) {
189 rtc::CritScope lock(&crit_);
190 event_log_ = event_log;
191 }
192
193 private:
194 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700195 RtcEventLog* event_log_ RTC_GUARDED_BY(crit_);
ivoc14d5dbe2016-07-04 07:06:55 -0700196 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
197};
198
michaelt9332b7d2016-11-30 07:51:13 -0800199class RtcpRttStatsProxy final : public RtcpRttStats {
200 public:
201 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
202
203 void OnRttUpdate(int64_t rtt) override {
204 rtc::CritScope lock(&crit_);
205 if (rtcp_rtt_stats_)
206 rtcp_rtt_stats_->OnRttUpdate(rtt);
207 }
208
209 int64_t LastProcessedRtt() const override {
210 rtc::CritScope lock(&crit_);
211 if (!rtcp_rtt_stats_)
212 return 0;
213 return rtcp_rtt_stats_->LastProcessedRtt();
214 }
215
216 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
217 rtc::CritScope lock(&crit_);
218 rtcp_rtt_stats_ = rtcp_rtt_stats;
219 }
220
221 private:
222 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700223 RtcpRttStats* rtcp_rtt_stats_ RTC_GUARDED_BY(crit_);
michaelt9332b7d2016-11-30 07:51:13 -0800224 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
225};
226
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100227class TransportFeedbackProxy : public TransportFeedbackObserver {
228 public:
229 TransportFeedbackProxy() : feedback_observer_(nullptr) {
230 pacer_thread_.DetachFromThread();
231 network_thread_.DetachFromThread();
232 }
233
234 void SetTransportFeedbackObserver(
235 TransportFeedbackObserver* feedback_observer) {
236 RTC_DCHECK(thread_checker_.CalledOnValidThread());
237 rtc::CritScope lock(&crit_);
238 feedback_observer_ = feedback_observer;
239 }
240
241 // Implements TransportFeedbackObserver.
elad.alond12a8e12017-03-23 11:04:48 -0700242 void AddPacket(uint32_t ssrc,
243 uint16_t sequence_number,
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100244 size_t length,
philipel8aadd502017-02-23 02:56:13 -0800245 const PacedPacketInfo& pacing_info) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100246 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
247 rtc::CritScope lock(&crit_);
248 if (feedback_observer_)
elad.alond12a8e12017-03-23 11:04:48 -0700249 feedback_observer_->AddPacket(ssrc, sequence_number, length, pacing_info);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100250 }
philipel8aadd502017-02-23 02:56:13 -0800251
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100252 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
253 RTC_DCHECK(network_thread_.CalledOnValidThread());
254 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700255 if (feedback_observer_)
256 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200257 }
elad.alonf9490002017-03-06 05:32:21 -0800258 std::vector<PacketFeedback> GetTransportFeedbackVector() const override {
Stefan Holmer60e43462016-09-07 09:58:20 +0200259 RTC_NOTREACHED();
elad.alonf9490002017-03-06 05:32:21 -0800260 return std::vector<PacketFeedback>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100261 }
262
263 private:
264 rtc::CriticalSection crit_;
265 rtc::ThreadChecker thread_checker_;
266 rtc::ThreadChecker pacer_thread_;
267 rtc::ThreadChecker network_thread_;
danilchapa37de392017-09-09 04:17:22 -0700268 TransportFeedbackObserver* feedback_observer_ RTC_GUARDED_BY(&crit_);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100269};
270
271class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
272 public:
273 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
274 pacer_thread_.DetachFromThread();
275 }
276
277 void SetSequenceNumberAllocator(
278 TransportSequenceNumberAllocator* seq_num_allocator) {
279 RTC_DCHECK(thread_checker_.CalledOnValidThread());
280 rtc::CritScope lock(&crit_);
281 seq_num_allocator_ = seq_num_allocator;
282 }
283
284 // Implements TransportSequenceNumberAllocator.
285 uint16_t AllocateSequenceNumber() override {
286 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
287 rtc::CritScope lock(&crit_);
288 if (!seq_num_allocator_)
289 return 0;
290 return seq_num_allocator_->AllocateSequenceNumber();
291 }
292
293 private:
294 rtc::CriticalSection crit_;
295 rtc::ThreadChecker thread_checker_;
296 rtc::ThreadChecker pacer_thread_;
danilchapa37de392017-09-09 04:17:22 -0700297 TransportSequenceNumberAllocator* seq_num_allocator_ RTC_GUARDED_BY(&crit_);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100298};
299
300class RtpPacketSenderProxy : public RtpPacketSender {
301 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800302 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100303
304 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
305 RTC_DCHECK(thread_checker_.CalledOnValidThread());
306 rtc::CritScope lock(&crit_);
307 rtp_packet_sender_ = rtp_packet_sender;
308 }
309
310 // Implements RtpPacketSender.
311 void InsertPacket(Priority priority,
312 uint32_t ssrc,
313 uint16_t sequence_number,
314 int64_t capture_time_ms,
315 size_t bytes,
316 bool retransmission) override {
317 rtc::CritScope lock(&crit_);
318 if (rtp_packet_sender_) {
319 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
320 capture_time_ms, bytes, retransmission);
321 }
322 }
323
324 private:
325 rtc::ThreadChecker thread_checker_;
326 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700327 RtpPacketSender* rtp_packet_sender_ RTC_GUARDED_BY(&crit_);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100328};
329
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000330class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000331 public:
stefan7de8d642017-02-07 07:14:08 -0800332 explicit VoERtcpObserver(Channel* owner)
333 : owner_(owner), bandwidth_observer_(nullptr) {}
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000334 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000335
stefan7de8d642017-02-07 07:14:08 -0800336 void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
337 rtc::CritScope lock(&crit_);
338 bandwidth_observer_ = bandwidth_observer;
339 }
340
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000341 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
stefan7de8d642017-02-07 07:14:08 -0800342 rtc::CritScope lock(&crit_);
343 if (bandwidth_observer_) {
344 bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
345 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000346 }
347
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000348 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
349 int64_t rtt,
350 int64_t now_ms) override {
stefan7de8d642017-02-07 07:14:08 -0800351 {
352 rtc::CritScope lock(&crit_);
353 if (bandwidth_observer_) {
354 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
355 now_ms);
356 }
357 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000358 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
359 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
360 // report for VoiceEngine?
361 if (report_blocks.empty())
362 return;
363
364 int fraction_lost_aggregate = 0;
365 int total_number_of_packets = 0;
366
367 // If receiving multiple report blocks, calculate the weighted average based
368 // on the number of packets a report refers to.
369 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
370 block_it != report_blocks.end(); ++block_it) {
371 // Find the previous extended high sequence number for this remote SSRC,
372 // to calculate the number of RTP packets this report refers to. Ignore if
373 // we haven't seen this SSRC before.
374 std::map<uint32_t, uint32_t>::iterator seq_num_it =
srte3e69e5c2017-08-09 06:13:45 -0700375 extended_max_sequence_number_.find(block_it->source_ssrc);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000376 int number_of_packets = 0;
377 if (seq_num_it != extended_max_sequence_number_.end()) {
srte3e69e5c2017-08-09 06:13:45 -0700378 number_of_packets =
379 block_it->extended_highest_sequence_number - seq_num_it->second;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000380 }
srte3e69e5c2017-08-09 06:13:45 -0700381 fraction_lost_aggregate += number_of_packets * block_it->fraction_lost;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000382 total_number_of_packets += number_of_packets;
383
srte3e69e5c2017-08-09 06:13:45 -0700384 extended_max_sequence_number_[block_it->source_ssrc] =
385 block_it->extended_highest_sequence_number;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000386 }
387 int weighted_fraction_lost = 0;
388 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800389 weighted_fraction_lost =
390 (fraction_lost_aggregate + total_number_of_packets / 2) /
391 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000392 }
elad.alond12a8e12017-03-23 11:04:48 -0700393 owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000394 }
395
396 private:
397 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000398 // Maps remote side ssrc to extended highest sequence number received.
399 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800400 rtc::CriticalSection crit_;
danilchapa37de392017-09-09 04:17:22 -0700401 RtcpBandwidthObserver* bandwidth_observer_ RTC_GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000402};
403
henrikaec6fbd22017-03-31 05:43:36 -0700404class Channel::ProcessAndEncodeAudioTask : public rtc::QueuedTask {
405 public:
406 ProcessAndEncodeAudioTask(std::unique_ptr<AudioFrame> audio_frame,
407 Channel* channel)
408 : audio_frame_(std::move(audio_frame)), channel_(channel) {
409 RTC_DCHECK(channel_);
410 }
411
412 private:
413 bool Run() override {
414 RTC_DCHECK_RUN_ON(channel_->encoder_queue_);
415 channel_->ProcessAndEncodeAudioOnTaskQueue(audio_frame_.get());
416 return true;
417 }
418
419 std::unique_ptr<AudioFrame> audio_frame_;
420 Channel* const channel_;
421};
422
kwiberg55b97fe2016-01-28 05:22:45 -0800423int32_t Channel::SendData(FrameType frameType,
424 uint8_t payloadType,
425 uint32_t timeStamp,
426 const uint8_t* payloadData,
427 size_t payloadSize,
428 const RTPFragmentationHeader* fragmentation) {
henrikaec6fbd22017-03-31 05:43:36 -0700429 RTC_DCHECK_RUN_ON(encoder_queue_);
kwiberg55b97fe2016-01-28 05:22:45 -0800430 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
431 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
432 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
433 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000434
kwiberg55b97fe2016-01-28 05:22:45 -0800435 if (_includeAudioLevelIndication) {
436 // Store current audio level in the RTP/RTCP module.
437 // The level will be used in combination with voice-activity state
438 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800439 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800440 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000441
kwiberg55b97fe2016-01-28 05:22:45 -0800442 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
443 // packetization.
444 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700445 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800446 (FrameType&)frameType, payloadType, timeStamp,
447 // Leaving the time when this frame was
448 // received from the capture device as
449 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700450 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800451 _engineStatisticsPtr->SetLastError(
452 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
453 "Channel::SendData() failed to send data to RTP/RTCP module");
454 return -1;
455 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000456
kwiberg55b97fe2016-01-28 05:22:45 -0800457 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000458}
459
stefan1d8a5062015-10-02 03:39:33 -0700460bool Channel::SendRtp(const uint8_t* data,
461 size_t len,
462 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800463 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
464 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000465
kwiberg55b97fe2016-01-28 05:22:45 -0800466 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000467
kwiberg55b97fe2016-01-28 05:22:45 -0800468 if (_transportPtr == NULL) {
469 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
470 "Channel::SendPacket() failed to send RTP packet due to"
471 " invalid transport object");
472 return false;
473 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000474
kwiberg55b97fe2016-01-28 05:22:45 -0800475 uint8_t* bufferToSendPtr = (uint8_t*)data;
476 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000477
kwiberg55b97fe2016-01-28 05:22:45 -0800478 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
479 std::string transport_name =
480 _externalTransport ? "external transport" : "WebRtc sockets";
481 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
482 "Channel::SendPacket() RTP transmission using %s failed",
483 transport_name.c_str());
484 return false;
485 }
486 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000487}
488
kwiberg55b97fe2016-01-28 05:22:45 -0800489bool Channel::SendRtcp(const uint8_t* data, size_t len) {
490 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
491 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
kwiberg55b97fe2016-01-28 05:22:45 -0800493 rtc::CritScope cs(&_callbackCritSect);
494 if (_transportPtr == NULL) {
495 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
496 "Channel::SendRtcp() failed to send RTCP packet"
497 " due to invalid transport object");
498 return false;
499 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000500
kwiberg55b97fe2016-01-28 05:22:45 -0800501 uint8_t* bufferToSendPtr = (uint8_t*)data;
502 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000503
kwiberg55b97fe2016-01-28 05:22:45 -0800504 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
505 if (n < 0) {
506 std::string transport_name =
507 _externalTransport ? "external transport" : "WebRtc sockets";
508 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
509 "Channel::SendRtcp() transmission using %s failed",
510 transport_name.c_str());
511 return false;
512 }
513 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000514}
515
kwiberg55b97fe2016-01-28 05:22:45 -0800516void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
517 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
518 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000519
kwiberg55b97fe2016-01-28 05:22:45 -0800520 // Update ssrc so that NTP for AV sync can be updated.
521 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000522}
523
Peter Boströmac547a62015-09-17 23:03:57 +0200524void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
525 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
526 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
527 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000528}
529
Peter Boströmac547a62015-09-17 23:03:57 +0200530int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000531 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000532 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000533 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800534 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200535 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800536 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
537 "Channel::OnInitializeDecoder(payloadType=%d, "
538 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
539 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000540
kwiberg55b97fe2016-01-28 05:22:45 -0800541 CodecInst receiveCodec = {0};
542 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000543
kwiberg55b97fe2016-01-28 05:22:45 -0800544 receiveCodec.pltype = payloadType;
545 receiveCodec.plfreq = frequency;
546 receiveCodec.channels = channels;
547 receiveCodec.rate = rate;
548 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000549
kwiberg55b97fe2016-01-28 05:22:45 -0800550 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
551 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000552
kwiberg55b97fe2016-01-28 05:22:45 -0800553 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700554 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
555 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800556 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
557 "Channel::OnInitializeDecoder() invalid codec ("
558 "pt=%d, name=%s) received - 1",
559 payloadType, payloadName);
560 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
561 return -1;
562 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000563
kwiberg55b97fe2016-01-28 05:22:45 -0800564 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000565}
566
kwiberg55b97fe2016-01-28 05:22:45 -0800567int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
568 size_t payloadSize,
569 const WebRtcRTPHeader* rtpHeader) {
570 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
571 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
572 ","
573 " payloadType=%u, audioChannel=%" PRIuS ")",
574 payloadSize, rtpHeader->header.payloadType,
575 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000576
kwiberg55b97fe2016-01-28 05:22:45 -0800577 if (!channel_state_.Get().playing) {
578 // Avoid inserting into NetEQ when we are not playing. Count the
579 // packet as discarded.
580 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
581 "received packet is discarded since playing is not"
582 " activated");
niklase@google.com470e71d2011-07-07 08:21:25 +0000583 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800584 }
585
586 // Push the incoming payload (parsed and ready for decoding) into the ACM
587 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
588 0) {
589 _engineStatisticsPtr->SetLastError(
590 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
591 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
592 return -1;
593 }
594
kwiberg55b97fe2016-01-28 05:22:45 -0800595 int64_t round_trip_time = 0;
596 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
597 NULL);
598
599 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
600 if (!nack_list.empty()) {
601 // Can't use nack_list.data() since it's not supported by all
602 // compilers.
603 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
604 }
605 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000606}
607
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000608bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000609 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000610 RTPHeader header;
611 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
612 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
613 "IncomingPacket invalid RTP header");
614 return false;
615 }
616 header.payload_type_frequency =
617 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
618 if (header.payload_type_frequency < 0)
619 return false;
620 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
621}
622
henrik.lundin42dda502016-05-18 05:36:01 -0700623MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
624 int32_t id,
625 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700626 unsigned int ssrc;
nisse7d59f6b2017-02-21 03:40:24 -0800627 RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
ivoc14d5dbe2016-07-04 07:06:55 -0700628 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800629 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700630 bool muted;
631 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
632 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800633 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
634 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
635 // In all likelihood, the audio in this frame is garbage. We return an
636 // error so that the audio mixer module doesn't add it to the mix. As
637 // a result, it won't be played out and the actions skipped here are
638 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700639 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800640 }
henrik.lundina89ab962016-05-18 08:52:45 -0700641
642 if (muted) {
643 // TODO(henrik.lundin): We should be able to do better than this. But we
644 // will have to go through all the cases below where the audio samples may
645 // be used, and handle the muted case in some way.
aleloi6321b492016-12-05 01:46:09 -0800646 AudioFrameOperations::Mute(audioFrame);
henrik.lundina89ab962016-05-18 08:52:45 -0700647 }
kwiberg55b97fe2016-01-28 05:22:45 -0800648
kwiberg55b97fe2016-01-28 05:22:45 -0800649 // Convert module ID to internal VoE channel ID
650 audioFrame->id_ = VoEChannelId(audioFrame->id_);
651 // Store speech type for dead-or-alive detection
652 _outputSpeechType = audioFrame->speech_type_;
653
654 ChannelState::State state = channel_state_.Get();
655
kwiberg55b97fe2016-01-28 05:22:45 -0800656 {
657 // Pass the audio buffers to an optional sink callback, before applying
658 // scaling/panning, as that applies to the mix operation.
659 // External recipients of the audio (e.g. via AudioTrack), will do their
660 // own mixing/dynamic processing.
661 rtc::CritScope cs(&_callbackCritSect);
662 if (audio_sink_) {
663 AudioSinkInterface::Data data(
yujo36b1a5f2017-06-12 12:45:32 -0700664 audioFrame->data(), audioFrame->samples_per_channel_,
kwiberg55b97fe2016-01-28 05:22:45 -0800665 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
666 audioFrame->timestamp_);
667 audio_sink_->OnData(data);
668 }
669 }
670
671 float output_gain = 1.0f;
kwiberg55b97fe2016-01-28 05:22:45 -0800672 {
673 rtc::CritScope cs(&volume_settings_critsect_);
674 output_gain = _outputGain;
kwiberg55b97fe2016-01-28 05:22:45 -0800675 }
676
677 // Output volume scaling
678 if (output_gain < 0.99f || output_gain > 1.01f) {
solenberg8d73f8c2017-03-08 01:52:20 -0800679 // TODO(solenberg): Combine with mute state - this can cause clicks!
oprypin67fdb802017-03-09 06:25:06 -0800680 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800681 }
682
kwiberg55b97fe2016-01-28 05:22:45 -0800683 // Mix decoded PCM output with file if file mixing is enabled
684 if (state.output_file_playing) {
685 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700686 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800687 }
688
kwiberg55b97fe2016-01-28 05:22:45 -0800689 // Record playout if enabled
690 {
691 rtc::CritScope cs(&_fileCritSect);
692
kwiberg5a25d952016-08-17 07:31:12 -0700693 if (_outputFileRecording && output_file_recorder_) {
694 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800695 }
696 }
697
698 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700699 // TODO(henrik.lundin) Use the |muted| information here too.
zstein3c451862017-07-20 09:57:42 -0700700 // TODO(deadbeef): Use RmsLevel for |_outputAudioLevel| (see
zsteine76bd3a2017-07-14 12:17:49 -0700701 // https://crbug.com/webrtc/7517).
zstein3c451862017-07-20 09:57:42 -0700702 _outputAudioLevel.ComputeLevel(*audioFrame, kAudioSampleDurationSeconds);
kwiberg55b97fe2016-01-28 05:22:45 -0800703
704 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
705 // The first frame with a valid rtp timestamp.
706 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
707 }
708
709 if (capture_start_rtp_time_stamp_ >= 0) {
710 // audioFrame.timestamp_ should be valid from now on.
711
712 // Compute elapsed time.
713 int64_t unwrap_timestamp =
714 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
715 audioFrame->elapsed_time_ms_ =
716 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700717 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800718
niklase@google.com470e71d2011-07-07 08:21:25 +0000719 {
kwiberg55b97fe2016-01-28 05:22:45 -0800720 rtc::CritScope lock(&ts_stats_lock_);
721 // Compute ntp time.
722 audioFrame->ntp_time_ms_ =
723 ntp_estimator_.Estimate(audioFrame->timestamp_);
724 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
725 if (audioFrame->ntp_time_ms_ > 0) {
726 // Compute |capture_start_ntp_time_ms_| so that
727 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
728 capture_start_ntp_time_ms_ =
729 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000730 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000731 }
kwiberg55b97fe2016-01-28 05:22:45 -0800732 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000733
henrik.lundin42dda502016-05-18 05:36:01 -0700734 return muted ? MixerParticipant::AudioFrameInfo::kMuted
735 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000736}
737
aleloi6c278492016-10-20 14:24:39 -0700738AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
739 int sample_rate_hz,
740 AudioFrame* audio_frame) {
741 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700742
aleloi6c278492016-10-20 14:24:39 -0700743 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700744
745 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
746 FrameInfo new_audio_frame_info = FrameInfo::kError;
747 switch (frame_info) {
748 case MixerParticipant::AudioFrameInfo::kNormal:
749 new_audio_frame_info = FrameInfo::kNormal;
750 break;
751 case MixerParticipant::AudioFrameInfo::kMuted:
752 new_audio_frame_info = FrameInfo::kMuted;
753 break;
754 case MixerParticipant::AudioFrameInfo::kError:
755 new_audio_frame_info = FrameInfo::kError;
756 break;
757 }
aleloi6c278492016-10-20 14:24:39 -0700758 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700759}
760
kwiberg55b97fe2016-01-28 05:22:45 -0800761int32_t Channel::NeededFrequency(int32_t id) const {
762 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
763 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000764
kwiberg55b97fe2016-01-28 05:22:45 -0800765 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000766
kwiberg55b97fe2016-01-28 05:22:45 -0800767 // Determine highest needed receive frequency
768 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000769
kwiberg55b97fe2016-01-28 05:22:45 -0800770 // Return the bigger of playout and receive frequency in the ACM.
771 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
772 highestNeeded = audio_coding_->PlayoutFrequency();
773 } else {
774 highestNeeded = receiveFrequency;
775 }
776
777 // Special case, if we're playing a file on the playout side
778 // we take that frequency into consideration as well
779 // This is not needed on sending side, since the codec will
780 // limit the spectrum anyway.
781 if (channel_state_.Get().output_file_playing) {
782 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700783 if (output_file_player_) {
784 if (output_file_player_->Frequency() > highestNeeded) {
785 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800786 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000787 }
kwiberg55b97fe2016-01-28 05:22:45 -0800788 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000789
kwiberg55b97fe2016-01-28 05:22:45 -0800790 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000791}
792
henrikaec6fbd22017-03-31 05:43:36 -0700793int32_t Channel::CreateChannel(Channel*& channel,
794 int32_t channelId,
795 uint32_t instanceId,
796 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800797 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
798 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
799 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000800
solenberg88499ec2016-09-07 07:34:41 -0700801 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800802 if (channel == NULL) {
803 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
804 "Channel::CreateChannel() unable to allocate memory for"
805 " channel");
806 return -1;
807 }
808 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000809}
810
kwiberg55b97fe2016-01-28 05:22:45 -0800811void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
812 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
813 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
814 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000815
kwiberg55b97fe2016-01-28 05:22:45 -0800816 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000817}
818
kwiberg55b97fe2016-01-28 05:22:45 -0800819void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
820 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
821 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
822 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000823
kwiberg55b97fe2016-01-28 05:22:45 -0800824 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000825}
826
kwiberg55b97fe2016-01-28 05:22:45 -0800827void Channel::PlayFileEnded(int32_t id) {
828 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
829 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000830
kwiberg55b97fe2016-01-28 05:22:45 -0800831 if (id == _inputFilePlayerId) {
832 channel_state_.SetInputFilePlaying(false);
833 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
834 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000835 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800836 } else if (id == _outputFilePlayerId) {
837 channel_state_.SetOutputFilePlaying(false);
838 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
839 "Channel::PlayFileEnded() => output file player module is"
840 " shutdown");
841 }
842}
843
844void Channel::RecordFileEnded(int32_t id) {
845 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
846 "Channel::RecordFileEnded(id=%d)", id);
847
848 assert(id == _outputFileRecorderId);
849
850 rtc::CritScope cs(&_fileCritSect);
851
852 _outputFileRecording = false;
853 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
854 "Channel::RecordFileEnded() => output file recorder module is"
855 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000856}
857
pbos@webrtc.org92135212013-05-14 08:31:39 +0000858Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000859 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700860 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800861 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100862 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700863 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800864 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100865 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800866 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100867 rtp_receive_statistics_(
868 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
869 rtp_receiver_(
870 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100871 this,
872 this,
873 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700874 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100875 _outputAudioLevel(),
876 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100877 // Avoid conflict with other channels by adding 1024 - 1026,
878 // won't use as much as 1024 channels.
879 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
880 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
881 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
882 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100883 _timeStamp(0), // This is just an offset, RTP module will add it's own
884 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100885 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100886 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100887 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100888 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100889 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
890 capture_start_rtp_time_stamp_(-1),
891 capture_start_ntp_time_ms_(-1),
892 _engineStatisticsPtr(NULL),
893 _outputMixerPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100894 _moduleProcessThreadPtr(NULL),
895 _audioDeviceModulePtr(NULL),
896 _voiceEngineObserverPtr(NULL),
897 _callbackCritSectPtr(NULL),
898 _transportPtr(NULL),
solenberg1c2af8e2016-03-24 10:36:00 -0700899 input_mute_(false),
900 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100901 _outputGain(1.0f),
solenberg8d73f8c2017-03-08 01:52:20 -0800902 _mixFileWithMicrophone(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100903 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800904 transport_overhead_per_packet_(0),
905 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100906 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100907 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100908 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700909 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800910 feedback_observer_proxy_(new TransportFeedbackProxy()),
911 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700912 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200913 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
914 kMaxRetransmissionWindowMs)),
elad.alond12a8e12017-03-23 11:04:48 -0700915 decoder_factory_(config.acm_config.decoder_factory),
elad.alon28770482017-03-28 05:03:55 -0700916 use_twcc_plr_for_ana_(
917 webrtc::field_trial::FindFullName("UseTwccPlrForAna") == "Enabled") {
kwiberg55b97fe2016-01-28 05:22:45 -0800918 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
919 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700920 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800921 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700922 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800923 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200924
kwiberg55b97fe2016-01-28 05:22:45 -0800925 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000926
kwiberg55b97fe2016-01-28 05:22:45 -0800927 RtpRtcp::Configuration configuration;
928 configuration.audio = true;
929 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800930 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800931 configuration.receive_statistics = rtp_receive_statistics_.get();
932 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800933 if (pacing_enabled_) {
934 configuration.paced_sender = rtp_packet_sender_proxy_.get();
935 configuration.transport_sequence_number_allocator =
936 seq_num_allocator_proxy_.get();
937 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
938 }
ivoc14d5dbe2016-07-04 07:06:55 -0700939 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800940 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200941 configuration.retransmission_rate_limiter =
942 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000943
kwiberg55b97fe2016-01-28 05:22:45 -0800944 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100945 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000946}
947
kwiberg55b97fe2016-01-28 05:22:45 -0800948Channel::~Channel() {
tommi0a2391f2017-03-21 02:31:51 -0700949 RTC_DCHECK(!channel_state_.Get().sending);
950 RTC_DCHECK(!channel_state_.Get().playing);
niklase@google.com470e71d2011-07-07 08:21:25 +0000951}
952
kwiberg55b97fe2016-01-28 05:22:45 -0800953int32_t Channel::Init() {
tommi0a2391f2017-03-21 02:31:51 -0700954 RTC_DCHECK(construction_thread_.CalledOnValidThread());
kwiberg55b97fe2016-01-28 05:22:45 -0800955 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
956 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000957
kwiberg55b97fe2016-01-28 05:22:45 -0800958 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000959
kwiberg55b97fe2016-01-28 05:22:45 -0800960 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000961
kwiberg55b97fe2016-01-28 05:22:45 -0800962 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
963 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
964 "Channel::Init() must call SetEngineInformation() first");
965 return -1;
966 }
967
968 // --- Add modules to process thread (for periodic schedulation)
969
tommidea489f2017-03-03 03:20:24 -0800970 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
kwiberg55b97fe2016-01-28 05:22:45 -0800971
972 // --- ACM initialization
973
974 if (audio_coding_->InitializeReceiver() == -1) {
975 _engineStatisticsPtr->SetLastError(
976 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
977 "Channel::Init() unable to initialize the ACM - 1");
978 return -1;
979 }
980
981 // --- RTP/RTCP module initialization
982
983 // Ensure that RTCP is enabled by default for the created channel.
984 // Note that, the module will keep generating RTCP until it is explicitly
985 // disabled by the user.
986 // After StopListen (when no sockets exists), RTCP packets will no longer
987 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -0700988 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800989 // RTCP is enabled by default.
990 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
991 // --- Register all permanent callbacks
solenbergfe7dd6d2017-03-11 08:10:43 -0800992 if (audio_coding_->RegisterTransportCallback(this) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800993 _engineStatisticsPtr->SetLastError(
994 VE_CANNOT_INIT_CHANNEL, kTraceError,
995 "Channel::Init() callbacks not registered");
996 return -1;
997 }
998
kwiberg1c07c702017-03-27 07:15:49 -0700999 // Register a default set of send codecs.
1000 const int nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
kwiberg55b97fe2016-01-28 05:22:45 -08001001 for (int idx = 0; idx < nSupportedCodecs; idx++) {
kwiberg1c07c702017-03-27 07:15:49 -07001002 CodecInst codec;
1003 RTC_CHECK_EQ(0, audio_coding_->Codec(idx, &codec));
1004
1005 // Ensure that PCMU is used as default send codec.
1006 if (STR_CASE_CMP(codec.plname, "PCMU") == 0 && codec.channels == 1) {
1007 SetSendCodec(codec);
1008 }
1009
1010 // Register default PT for 'telephone-event'
1011 if (STR_CASE_CMP(codec.plname, "telephone-event") == 0) {
1012 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1) {
1013 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1014 "Channel::Init() failed to register outband "
1015 "'telephone-event' (%d/%d) correctly",
1016 codec.pltype, codec.plfreq);
1017 }
1018 }
1019
1020 if (STR_CASE_CMP(codec.plname, "CN") == 0) {
1021 if (!codec_manager_.RegisterEncoder(codec) ||
1022 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
1023 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
1024 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1025 "Channel::Init() failed to register CN (%d/%d) "
1026 "correctly - 1",
1027 codec.pltype, codec.plfreq);
1028 }
1029 }
1030 }
1031
1032 return 0;
1033}
1034
1035void Channel::RegisterLegacyReceiveCodecs() {
1036 const int nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1037 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1038 CodecInst codec;
1039 RTC_CHECK_EQ(0, audio_coding_->Codec(idx, &codec));
1040
kwiberg55b97fe2016-01-28 05:22:45 -08001041 // Open up the RTP/RTCP receiver for all supported codecs
kwiberg1c07c702017-03-27 07:15:49 -07001042 if (rtp_receiver_->RegisterReceivePayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001043 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1044 "Channel::Init() unable to register %s "
1045 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1046 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1047 codec.rate);
1048 } else {
1049 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1050 "Channel::Init() %s (%d/%d/%" PRIuS
1051 "/%d) has been "
1052 "added to the RTP/RTCP receiver",
1053 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1054 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001055 }
1056
kwiberg1c07c702017-03-27 07:15:49 -07001057 // Register default PT for 'telephone-event'
1058 if (STR_CASE_CMP(codec.plname, "telephone-event") == 0) {
1059 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
kwibergda2bf4e2016-10-24 13:47:09 -07001060 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001061 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
kwiberg1c07c702017-03-27 07:15:49 -07001062 "Channel::Init() failed to register inband "
kwiberg55b97fe2016-01-28 05:22:45 -08001063 "'telephone-event' (%d/%d) correctly",
1064 codec.pltype, codec.plfreq);
1065 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001066 }
1067
kwiberg1c07c702017-03-27 07:15:49 -07001068 if (STR_CASE_CMP(codec.plname, "CN") == 0) {
1069 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1070 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001071 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1072 "Channel::Init() failed to register CN (%d/%d) "
1073 "correctly - 1",
1074 codec.pltype, codec.plfreq);
1075 }
1076 }
kwiberg55b97fe2016-01-28 05:22:45 -08001077 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001078}
1079
tommi0a2391f2017-03-21 02:31:51 -07001080void Channel::Terminate() {
1081 RTC_DCHECK(construction_thread_.CalledOnValidThread());
1082 // Must be called on the same thread as Init().
1083 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
1084 "Channel::Terminate");
1085
1086 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
1087
1088 StopSend();
1089 StopPlayout();
1090
1091 {
1092 rtc::CritScope cs(&_fileCritSect);
1093 if (input_file_player_) {
1094 input_file_player_->RegisterModuleFileCallback(NULL);
1095 input_file_player_->StopPlayingFile();
1096 }
1097 if (output_file_player_) {
1098 output_file_player_->RegisterModuleFileCallback(NULL);
1099 output_file_player_->StopPlayingFile();
1100 }
1101 if (output_file_recorder_) {
1102 output_file_recorder_->RegisterModuleFileCallback(NULL);
1103 output_file_recorder_->StopRecording();
1104 }
1105 }
1106
1107 // The order to safely shutdown modules in a channel is:
1108 // 1. De-register callbacks in modules
1109 // 2. De-register modules in process thread
1110 // 3. Destroy modules
1111 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
1112 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1113 "Terminate() failed to de-register transport callback"
1114 " (Audio coding module)");
1115 }
1116
1117 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
1118 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1119 "Terminate() failed to de-register VAD callback"
1120 " (Audio coding module)");
1121 }
1122
1123 // De-register modules in process thread
1124 if (_moduleProcessThreadPtr)
1125 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
1126
1127 // End of modules shutdown
1128}
1129
kwiberg55b97fe2016-01-28 05:22:45 -08001130int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1131 OutputMixer& outputMixer,
kwiberg55b97fe2016-01-28 05:22:45 -08001132 ProcessThread& moduleProcessThread,
1133 AudioDeviceModule& audioDeviceModule,
1134 VoiceEngineObserver* voiceEngineObserver,
henrikaec6fbd22017-03-31 05:43:36 -07001135 rtc::CriticalSection* callbackCritSect,
1136 rtc::TaskQueue* encoder_queue) {
1137 RTC_DCHECK(encoder_queue);
1138 RTC_DCHECK(!encoder_queue_);
kwiberg55b97fe2016-01-28 05:22:45 -08001139 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1140 "Channel::SetEngineInformation()");
1141 _engineStatisticsPtr = &engineStatistics;
1142 _outputMixerPtr = &outputMixer;
kwiberg55b97fe2016-01-28 05:22:45 -08001143 _moduleProcessThreadPtr = &moduleProcessThread;
1144 _audioDeviceModulePtr = &audioDeviceModule;
1145 _voiceEngineObserverPtr = voiceEngineObserver;
1146 _callbackCritSectPtr = callbackCritSect;
henrikaec6fbd22017-03-31 05:43:36 -07001147 encoder_queue_ = encoder_queue;
kwiberg55b97fe2016-01-28 05:22:45 -08001148 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001149}
1150
kwibergb7f89d62016-02-17 10:04:18 -08001151void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001152 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001153 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001154}
1155
ossu29b1a8d2016-06-13 07:34:51 -07001156const rtc::scoped_refptr<AudioDecoderFactory>&
1157Channel::GetAudioDecoderFactory() const {
1158 return decoder_factory_;
1159}
1160
kwiberg55b97fe2016-01-28 05:22:45 -08001161int32_t Channel::StartPlayout() {
1162 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1163 "Channel::StartPlayout()");
1164 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001165 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001166 }
1167
solenberge374e012017-02-14 04:55:00 -08001168 // Add participant as candidates for mixing.
1169 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1170 _engineStatisticsPtr->SetLastError(
1171 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1172 "StartPlayout() failed to add participant to mixer");
1173 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001174 }
1175
1176 channel_state_.SetPlaying(true);
1177 if (RegisterFilePlayingToMixer() != 0)
1178 return -1;
1179
1180 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001181}
1182
kwiberg55b97fe2016-01-28 05:22:45 -08001183int32_t Channel::StopPlayout() {
1184 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1185 "Channel::StopPlayout()");
1186 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001187 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001188 }
1189
solenberge374e012017-02-14 04:55:00 -08001190 // Remove participant as candidates for mixing
1191 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1192 _engineStatisticsPtr->SetLastError(
1193 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1194 "StopPlayout() failed to remove participant from mixer");
1195 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001196 }
1197
1198 channel_state_.SetPlaying(false);
1199 _outputAudioLevel.Clear();
1200
1201 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001202}
1203
kwiberg55b97fe2016-01-28 05:22:45 -08001204int32_t Channel::StartSend() {
1205 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1206 "Channel::StartSend()");
kwiberg55b97fe2016-01-28 05:22:45 -08001207 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001208 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001209 }
1210 channel_state_.SetSending(true);
henrika4515fa02017-05-03 08:30:15 -07001211 {
1212 // It is now OK to start posting tasks to the encoder task queue.
1213 rtc::CritScope cs(&encoder_queue_lock_);
1214 encoder_queue_is_active_ = true;
1215 }
solenberg08b19df2017-02-15 00:42:31 -08001216 // Resume the previous sequence number which was reset by StopSend(). This
1217 // needs to be done before |sending| is set to true on the RTP/RTCP module.
1218 if (send_sequence_number_) {
1219 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
1220 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001221 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001222 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1223 _engineStatisticsPtr->SetLastError(
1224 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1225 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001226 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001227 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001228 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001229 return -1;
1230 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001231
kwiberg55b97fe2016-01-28 05:22:45 -08001232 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001233}
1234
henrikaec6fbd22017-03-31 05:43:36 -07001235void Channel::StopSend() {
kwiberg55b97fe2016-01-28 05:22:45 -08001236 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1237 "Channel::StopSend()");
1238 if (!channel_state_.Get().sending) {
henrikaec6fbd22017-03-31 05:43:36 -07001239 return;
kwiberg55b97fe2016-01-28 05:22:45 -08001240 }
1241 channel_state_.SetSending(false);
1242
henrikaec6fbd22017-03-31 05:43:36 -07001243 // Post a task to the encoder thread which sets an event when the task is
1244 // executed. We know that no more encoding tasks will be added to the task
1245 // queue for this channel since sending is now deactivated. It means that,
1246 // if we wait for the event to bet set, we know that no more pending tasks
1247 // exists and it is therfore guaranteed that the task queue will never try
1248 // to acccess and invalid channel object.
1249 RTC_DCHECK(encoder_queue_);
henrika4515fa02017-05-03 08:30:15 -07001250
henrikaec6fbd22017-03-31 05:43:36 -07001251 rtc::Event flush(false, false);
henrika4515fa02017-05-03 08:30:15 -07001252 {
1253 // Clear |encoder_queue_is_active_| under lock to prevent any other tasks
1254 // than this final "flush task" to be posted on the queue.
1255 rtc::CritScope cs(&encoder_queue_lock_);
1256 encoder_queue_is_active_ = false;
1257 encoder_queue_->PostTask([&flush]() { flush.Set(); });
1258 }
henrikaec6fbd22017-03-31 05:43:36 -07001259 flush.Wait(rtc::Event::kForever);
1260
kwiberg55b97fe2016-01-28 05:22:45 -08001261 // Store the sequence number to be able to pick up the same sequence for
1262 // the next StartSend(). This is needed for restarting device, otherwise
1263 // it might cause libSRTP to complain about packets being replayed.
1264 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1265 // CL is landed. See issue
1266 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1267 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1268
1269 // Reset sending SSRC and sequence number and triggers direct transmission
1270 // of RTCP BYE
1271 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1272 _engineStatisticsPtr->SetLastError(
1273 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1274 "StartSend() RTP/RTCP failed to stop sending");
1275 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001276 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001277}
1278
ossu1ffbd6c2017-04-06 12:05:04 -07001279bool Channel::SetEncoder(int payload_type,
1280 std::unique_ptr<AudioEncoder> encoder) {
1281 RTC_DCHECK_GE(payload_type, 0);
1282 RTC_DCHECK_LE(payload_type, 127);
ossu76d29f92017-06-09 07:30:13 -07001283 // TODO(ossu): Make CodecInsts up, for now: one for the RTP/RTCP module and
1284 // one for for us to keep track of sample rate and number of channels, etc.
1285
1286 // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate)
1287 // as well as some other things, so we collect this info and send it along.
1288 CodecInst rtp_codec;
1289 rtp_codec.pltype = payload_type;
1290 strncpy(rtp_codec.plname, "audio", sizeof(rtp_codec.plname));
1291 rtp_codec.plname[sizeof(rtp_codec.plname) - 1] = 0;
ossu1ffbd6c2017-04-06 12:05:04 -07001292 // Seems unclear if it should be clock rate or sample rate. CodecInst
1293 // supposedly carries the sample rate, but only clock rate seems sensible to
1294 // send to the RTP/RTCP module.
ossu76d29f92017-06-09 07:30:13 -07001295 rtp_codec.plfreq = encoder->RtpTimestampRateHz();
1296 rtp_codec.pacsize = rtc::CheckedDivExact(
1297 static_cast<int>(encoder->Max10MsFramesInAPacket() * rtp_codec.plfreq),
1298 100);
1299 rtp_codec.channels = encoder->NumChannels();
1300 rtp_codec.rate = 0;
ossu1ffbd6c2017-04-06 12:05:04 -07001301
ossu76d29f92017-06-09 07:30:13 -07001302 // For audio encoding we need, instead, the actual sample rate of the codec.
1303 // The rest of the information should be the same.
1304 CodecInst send_codec = rtp_codec;
1305 send_codec.plfreq = encoder->SampleRateHz();
1306 cached_send_codec_.emplace(send_codec);
1307
1308 if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) {
ossu1ffbd6c2017-04-06 12:05:04 -07001309 _rtpRtcpModule->DeRegisterSendPayload(payload_type);
ossu76d29f92017-06-09 07:30:13 -07001310 if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) {
ossu1ffbd6c2017-04-06 12:05:04 -07001311 WEBRTC_TRACE(
1312 kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1313 "SetEncoder() failed to register codec to RTP/RTCP module");
1314 return false;
1315 }
1316 }
1317
1318 audio_coding_->SetEncoder(std::move(encoder));
ossu20a4b3f2017-04-27 02:08:52 -07001319 codec_manager_.UnsetCodecInst();
ossu1ffbd6c2017-04-06 12:05:04 -07001320 return true;
1321}
1322
ossu20a4b3f2017-04-27 02:08:52 -07001323void Channel::ModifyEncoder(
1324 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
1325 audio_coding_->ModifyEncoder(modifier);
1326}
1327
kwiberg55b97fe2016-01-28 05:22:45 -08001328int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1329 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1330 "Channel::RegisterVoiceEngineObserver()");
1331 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001332
kwiberg55b97fe2016-01-28 05:22:45 -08001333 if (_voiceEngineObserverPtr) {
1334 _engineStatisticsPtr->SetLastError(
1335 VE_INVALID_OPERATION, kTraceError,
1336 "RegisterVoiceEngineObserver() observer already enabled");
1337 return -1;
1338 }
1339 _voiceEngineObserverPtr = &observer;
1340 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001341}
1342
kwiberg55b97fe2016-01-28 05:22:45 -08001343int32_t Channel::DeRegisterVoiceEngineObserver() {
1344 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1345 "Channel::DeRegisterVoiceEngineObserver()");
1346 rtc::CritScope cs(&_callbackCritSect);
1347
1348 if (!_voiceEngineObserverPtr) {
1349 _engineStatisticsPtr->SetLastError(
1350 VE_INVALID_OPERATION, kTraceWarning,
1351 "DeRegisterVoiceEngineObserver() observer already disabled");
1352 return 0;
1353 }
1354 _voiceEngineObserverPtr = NULL;
1355 return 0;
1356}
1357
1358int32_t Channel::GetSendCodec(CodecInst& codec) {
ossu76d29f92017-06-09 07:30:13 -07001359 if (cached_send_codec_) {
1360 codec = *cached_send_codec_;
1361 return 0;
1362 } else {
ossu20a4b3f2017-04-27 02:08:52 -07001363 const CodecInst* send_codec = codec_manager_.GetCodecInst();
1364 if (send_codec) {
1365 codec = *send_codec;
1366 return 0;
1367 }
1368 }
kwiberg1fd4a4a2015-11-03 11:20:50 -08001369 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001370}
1371
kwiberg55b97fe2016-01-28 05:22:45 -08001372int32_t Channel::GetRecCodec(CodecInst& codec) {
1373 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001374}
1375
kwiberg55b97fe2016-01-28 05:22:45 -08001376int32_t Channel::SetSendCodec(const CodecInst& codec) {
1377 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1378 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001379
kwibergc8d071e2016-04-06 12:22:38 -07001380 if (!codec_manager_.RegisterEncoder(codec) ||
1381 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001382 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1383 "SetSendCodec() failed to register codec to ACM");
1384 return -1;
1385 }
1386
1387 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1388 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1389 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1390 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1391 "SetSendCodec() failed to register codec to"
1392 " RTP/RTCP module");
1393 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001394 }
kwiberg55b97fe2016-01-28 05:22:45 -08001395 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001396
ossu76d29f92017-06-09 07:30:13 -07001397 cached_send_codec_.reset();
1398
kwiberg55b97fe2016-01-28 05:22:45 -08001399 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001400}
1401
minyue78b4d562016-11-30 04:47:39 -08001402void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001403 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1404 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001405 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001406 if (*encoder) {
1407 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001408 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001409 }
1410 });
michaelt566d8202017-01-12 10:17:38 -08001411 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001412}
1413
elad.alond12a8e12017-03-23 11:04:48 -07001414void Channel::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
1415 if (!use_twcc_plr_for_ana_)
1416 return;
minyue7e304322016-10-12 05:00:55 -07001417 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
elad.alond12a8e12017-03-23 11:04:48 -07001418 if (*encoder) {
1419 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1420 }
1421 });
1422}
1423
elad.alondadb4dc2017-03-23 15:29:50 -07001424void Channel::OnRecoverableUplinkPacketLossRate(
1425 float recoverable_packet_loss_rate) {
1426 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1427 if (*encoder) {
1428 (*encoder)->OnReceivedUplinkRecoverablePacketLossFraction(
1429 recoverable_packet_loss_rate);
1430 }
1431 });
1432}
1433
elad.alond12a8e12017-03-23 11:04:48 -07001434void Channel::OnUplinkPacketLossRate(float packet_loss_rate) {
1435 if (use_twcc_plr_for_ana_)
1436 return;
1437 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1438 if (*encoder) {
1439 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1440 }
minyue7e304322016-10-12 05:00:55 -07001441 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001442}
1443
kwiberg55b97fe2016-01-28 05:22:45 -08001444int32_t Channel::SetVADStatus(bool enableVAD,
1445 ACMVADMode mode,
1446 bool disableDTX) {
1447 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1448 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001449 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1450 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1451 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001452 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1453 kTraceError,
1454 "SetVADStatus() failed to set VAD");
1455 return -1;
1456 }
1457 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001458}
1459
kwiberg55b97fe2016-01-28 05:22:45 -08001460int32_t Channel::GetVADStatus(bool& enabledVAD,
1461 ACMVADMode& mode,
1462 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001463 const auto* params = codec_manager_.GetStackParams();
1464 enabledVAD = params->use_cng;
1465 mode = params->vad_mode;
1466 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001467 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001468}
1469
kwiberg1c07c702017-03-27 07:15:49 -07001470void Channel::SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) {
1471 rtp_payload_registry_->SetAudioReceivePayloads(codecs);
1472 audio_coding_->SetReceiveCodecs(codecs);
1473}
1474
kwiberg55b97fe2016-01-28 05:22:45 -08001475int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001476 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1477}
1478
1479int32_t Channel::SetRecPayloadType(int payload_type,
1480 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001481 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1482 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001483
kwiberg55b97fe2016-01-28 05:22:45 -08001484 if (channel_state_.Get().playing) {
1485 _engineStatisticsPtr->SetLastError(
1486 VE_ALREADY_PLAYING, kTraceError,
1487 "SetRecPayloadType() unable to set PT while playing");
1488 return -1;
1489 }
kwiberg55b97fe2016-01-28 05:22:45 -08001490
kwiberg09f090c2017-03-01 01:57:11 -08001491 const CodecInst codec = SdpToCodecInst(payload_type, format);
kwibergd32bf752017-01-19 07:03:59 -08001492
1493 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001494 // De-register the selected codec (RTP/RTCP module and ACM)
1495
1496 int8_t pltype(-1);
1497 CodecInst rxCodec = codec;
1498
1499 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001500 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001501 rxCodec.pltype = pltype;
1502
1503 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1504 _engineStatisticsPtr->SetLastError(
1505 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1506 "SetRecPayloadType() RTP/RTCP-module deregistration "
1507 "failed");
1508 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001509 }
kwiberg55b97fe2016-01-28 05:22:45 -08001510 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1511 _engineStatisticsPtr->SetLastError(
1512 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1513 "SetRecPayloadType() ACM deregistration failed - 1");
1514 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001515 }
kwiberg55b97fe2016-01-28 05:22:45 -08001516 return 0;
1517 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001518
magjed56124bd2016-11-24 09:34:46 -08001519 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001520 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001521 // TODO(kwiberg): Retrying is probably not necessary, since
1522 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001523 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001524 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001525 _engineStatisticsPtr->SetLastError(
1526 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1527 "SetRecPayloadType() RTP/RTCP-module registration failed");
1528 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001529 }
kwiberg55b97fe2016-01-28 05:22:45 -08001530 }
kwibergd32bf752017-01-19 07:03:59 -08001531 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1532 audio_coding_->UnregisterReceiveCodec(payload_type);
1533 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001534 _engineStatisticsPtr->SetLastError(
1535 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1536 "SetRecPayloadType() ACM registration failed - 1");
1537 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001538 }
kwiberg55b97fe2016-01-28 05:22:45 -08001539 }
1540 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001541}
1542
kwiberg55b97fe2016-01-28 05:22:45 -08001543int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1544 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001545 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001546 _engineStatisticsPtr->SetLastError(
1547 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1548 "GetRecPayloadType() failed to retrieve RX payload type");
1549 return -1;
1550 }
1551 codec.pltype = payloadType;
1552 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001553}
1554
kwiberg55b97fe2016-01-28 05:22:45 -08001555int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1556 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1557 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001558
kwiberg55b97fe2016-01-28 05:22:45 -08001559 CodecInst codec;
1560 int32_t samplingFreqHz(-1);
1561 const size_t kMono = 1;
1562 if (frequency == kFreq32000Hz)
1563 samplingFreqHz = 32000;
1564 else if (frequency == kFreq16000Hz)
1565 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001566
kwiberg55b97fe2016-01-28 05:22:45 -08001567 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1568 _engineStatisticsPtr->SetLastError(
1569 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1570 "SetSendCNPayloadType() failed to retrieve default CN codec "
1571 "settings");
1572 return -1;
1573 }
1574
1575 // Modify the payload type (must be set to dynamic range)
1576 codec.pltype = type;
1577
kwibergc8d071e2016-04-06 12:22:38 -07001578 if (!codec_manager_.RegisterEncoder(codec) ||
1579 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001580 _engineStatisticsPtr->SetLastError(
1581 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1582 "SetSendCNPayloadType() failed to register CN to ACM");
1583 return -1;
1584 }
1585
1586 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1587 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1588 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1589 _engineStatisticsPtr->SetLastError(
1590 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1591 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1592 "module");
1593 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001594 }
kwiberg55b97fe2016-01-28 05:22:45 -08001595 }
1596 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001597}
1598
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001599int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001600 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001601 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001602
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001603 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001604 _engineStatisticsPtr->SetLastError(
1605 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001606 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001607 return -1;
1608 }
1609 return 0;
1610}
1611
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001612int Channel::SetOpusDtx(bool enable_dtx) {
1613 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1614 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001615 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001616 : audio_coding_->DisableOpusDtx();
1617 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001618 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1619 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001620 return -1;
1621 }
1622 return 0;
1623}
1624
ivoc85228d62016-07-27 04:53:47 -07001625int Channel::GetOpusDtx(bool* enabled) {
1626 int success = -1;
1627 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1628 if (encoder) {
1629 *enabled = encoder->GetDtx();
1630 success = 0;
1631 }
1632 });
1633 return success;
1634}
1635
minyue7e304322016-10-12 05:00:55 -07001636bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1637 bool success = false;
1638 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1639 if (*encoder) {
michaelt92aef172017-04-18 00:11:48 -07001640 success = (*encoder)->EnableAudioNetworkAdaptor(config_string,
1641 event_log_proxy_.get());
minyue7e304322016-10-12 05:00:55 -07001642 }
1643 });
1644 return success;
1645}
1646
1647void Channel::DisableAudioNetworkAdaptor() {
1648 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1649 if (*encoder)
1650 (*encoder)->DisableAudioNetworkAdaptor();
1651 });
1652}
1653
1654void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1655 int max_frame_length_ms) {
1656 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1657 if (*encoder) {
1658 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1659 max_frame_length_ms);
1660 }
1661 });
1662}
1663
mflodman3d7db262016-04-29 00:57:13 -07001664int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001665 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001666 "Channel::RegisterExternalTransport()");
1667
kwiberg55b97fe2016-01-28 05:22:45 -08001668 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001669 if (_externalTransport) {
1670 _engineStatisticsPtr->SetLastError(
1671 VE_INVALID_OPERATION, kTraceError,
1672 "RegisterExternalTransport() external transport already enabled");
1673 return -1;
1674 }
1675 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001676 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001677 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001678}
1679
kwiberg55b97fe2016-01-28 05:22:45 -08001680int32_t Channel::DeRegisterExternalTransport() {
1681 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1682 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001683
kwiberg55b97fe2016-01-28 05:22:45 -08001684 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001685 if (_transportPtr) {
1686 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1687 "DeRegisterExternalTransport() all transport is disabled");
1688 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001689 _engineStatisticsPtr->SetLastError(
1690 VE_INVALID_OPERATION, kTraceWarning,
1691 "DeRegisterExternalTransport() external transport already "
1692 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001693 }
1694 _externalTransport = false;
1695 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001696 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001697}
1698
nisse657bab22017-02-21 06:28:10 -08001699// TODO(nisse): Delete this method together with ReceivedRTPPacket.
1700// It's a temporary hack to support both ReceivedRTPPacket and
1701// OnRtpPacket interfaces without too much code duplication.
1702bool Channel::OnRtpPacketWithHeader(const uint8_t* received_packet,
1703 size_t length,
1704 RTPHeader *header) {
1705 // Store playout timestamp for the received RTP packet
1706 UpdatePlayoutTimestamp(false);
1707
1708 header->payload_type_frequency =
1709 rtp_payload_registry_->GetPayloadTypeFrequency(header->payloadType);
1710 if (header->payload_type_frequency < 0)
1711 return false;
1712 bool in_order = IsPacketInOrder(*header);
1713 rtp_receive_statistics_->IncomingPacket(
1714 *header, length, IsPacketRetransmitted(*header, in_order));
1715 rtp_payload_registry_->SetIncomingPayloadType(*header);
1716
1717 return ReceivePacket(received_packet, length, *header, in_order);
1718}
1719
mflodman3d7db262016-04-29 00:57:13 -07001720int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001721 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001722 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001723 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001724 "Channel::ReceivedRTPPacket()");
1725
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001726 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001727 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1728 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1729 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001730 return -1;
1731 }
nisse657bab22017-02-21 06:28:10 -08001732 return OnRtpPacketWithHeader(received_packet, length, &header) ? 0 : -1;
1733}
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001734
nisse657bab22017-02-21 06:28:10 -08001735void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
1736 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
1737 "Channel::ReceivedRTPPacket()");
1738
1739 RTPHeader header;
1740 packet.GetHeader(&header);
1741 OnRtpPacketWithHeader(packet.data(), packet.size(), &header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001742}
1743
1744bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001745 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001746 const RTPHeader& header,
1747 bool in_order) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001748 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001749 assert(packet_length >= header.headerLength);
1750 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001751 PayloadUnion payload_specific;
1752 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001753 &payload_specific)) {
1754 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001755 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001756 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1757 payload_specific, in_order);
1758}
1759
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001760bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1761 StreamStatistician* statistician =
1762 rtp_receive_statistics_->GetStatistician(header.ssrc);
1763 if (!statistician)
1764 return false;
1765 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001766}
1767
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001768bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1769 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001770 StreamStatistician* statistician =
1771 rtp_receive_statistics_->GetStatistician(header.ssrc);
1772 if (!statistician)
1773 return false;
1774 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001775 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001776 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001777 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001778}
1779
mflodman3d7db262016-04-29 00:57:13 -07001780int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001781 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001782 "Channel::ReceivedRTCPPacket()");
1783 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001784 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001785
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001786 // Deliver RTCP packet to RTP/RTCP module for parsing
nisse479d3d72017-09-13 07:53:37 -07001787 _rtpRtcpModule->IncomingRtcpPacket(data, length);
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001788
Minyue2013aec2015-05-13 14:14:42 +02001789 int64_t rtt = GetRTT(true);
1790 if (rtt == 0) {
1791 // Waiting for valid RTT.
1792 return 0;
1793 }
Erik Språng737336d2016-07-29 12:59:36 +02001794
1795 int64_t nack_window_ms = rtt;
1796 if (nack_window_ms < kMinRetransmissionWindowMs) {
1797 nack_window_ms = kMinRetransmissionWindowMs;
1798 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1799 nack_window_ms = kMaxRetransmissionWindowMs;
1800 }
1801 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1802
minyue7e304322016-10-12 05:00:55 -07001803 // Invoke audio encoders OnReceivedRtt().
1804 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1805 if (*encoder)
1806 (*encoder)->OnReceivedRtt(rtt);
1807 });
1808
Minyue2013aec2015-05-13 14:14:42 +02001809 uint32_t ntp_secs = 0;
1810 uint32_t ntp_frac = 0;
1811 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001812 if (0 !=
1813 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1814 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001815 // Waiting for RTCP.
1816 return 0;
1817 }
1818
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001819 {
tommi31fc21f2016-01-21 10:37:37 -08001820 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001821 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001822 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001823 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001824}
1825
niklase@google.com470e71d2011-07-07 08:21:25 +00001826int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001827 bool loop,
1828 FileFormats format,
1829 int startPosition,
1830 float volumeScaling,
1831 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001832 const CodecInst* codecInst) {
1833 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1834 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1835 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1836 "stopPosition=%d)",
1837 fileName, loop, format, volumeScaling, startPosition,
1838 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001839
kwiberg55b97fe2016-01-28 05:22:45 -08001840 if (channel_state_.Get().output_file_playing) {
1841 _engineStatisticsPtr->SetLastError(
1842 VE_ALREADY_PLAYING, kTraceError,
1843 "StartPlayingFileLocally() is already playing");
1844 return -1;
1845 }
1846
1847 {
1848 rtc::CritScope cs(&_fileCritSect);
1849
kwiberg5a25d952016-08-17 07:31:12 -07001850 if (output_file_player_) {
1851 output_file_player_->RegisterModuleFileCallback(NULL);
1852 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001853 }
1854
kwiberg5b356f42016-09-08 04:32:33 -07001855 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001856 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001857
kwiberg5a25d952016-08-17 07:31:12 -07001858 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001859 _engineStatisticsPtr->SetLastError(
1860 VE_INVALID_ARGUMENT, kTraceError,
1861 "StartPlayingFileLocally() filePlayer format is not correct");
1862 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001863 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001864
kwiberg55b97fe2016-01-28 05:22:45 -08001865 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001866
kwiberg5a25d952016-08-17 07:31:12 -07001867 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001868 fileName, loop, startPosition, volumeScaling, notificationTime,
1869 stopPosition, (const CodecInst*)codecInst) != 0) {
1870 _engineStatisticsPtr->SetLastError(
1871 VE_BAD_FILE, kTraceError,
1872 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001873 output_file_player_->StopPlayingFile();
1874 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001875 return -1;
1876 }
kwiberg5a25d952016-08-17 07:31:12 -07001877 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001878 channel_state_.SetOutputFilePlaying(true);
1879 }
1880
1881 if (RegisterFilePlayingToMixer() != 0)
1882 return -1;
1883
1884 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001885}
1886
1887int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001888 FileFormats format,
1889 int startPosition,
1890 float volumeScaling,
1891 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001892 const CodecInst* codecInst) {
1893 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1894 "Channel::StartPlayingFileLocally(format=%d,"
1895 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1896 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001897
kwiberg55b97fe2016-01-28 05:22:45 -08001898 if (stream == NULL) {
1899 _engineStatisticsPtr->SetLastError(
1900 VE_BAD_FILE, kTraceError,
1901 "StartPlayingFileLocally() NULL as input stream");
1902 return -1;
1903 }
1904
1905 if (channel_state_.Get().output_file_playing) {
1906 _engineStatisticsPtr->SetLastError(
1907 VE_ALREADY_PLAYING, kTraceError,
1908 "StartPlayingFileLocally() is already playing");
1909 return -1;
1910 }
1911
1912 {
1913 rtc::CritScope cs(&_fileCritSect);
1914
1915 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001916 if (output_file_player_) {
1917 output_file_player_->RegisterModuleFileCallback(NULL);
1918 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001919 }
1920
kwiberg55b97fe2016-01-28 05:22:45 -08001921 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001922 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001923 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001924
kwiberg5a25d952016-08-17 07:31:12 -07001925 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001926 _engineStatisticsPtr->SetLastError(
1927 VE_INVALID_ARGUMENT, kTraceError,
1928 "StartPlayingFileLocally() filePlayer format isnot correct");
1929 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001930 }
1931
kwiberg55b97fe2016-01-28 05:22:45 -08001932 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001933
kwiberg4ec01d92016-08-22 08:43:54 -07001934 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001935 volumeScaling, notificationTime,
1936 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001937 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1938 "StartPlayingFile() failed to "
1939 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001940 output_file_player_->StopPlayingFile();
1941 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001942 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001943 }
kwiberg5a25d952016-08-17 07:31:12 -07001944 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001945 channel_state_.SetOutputFilePlaying(true);
1946 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001947
kwiberg55b97fe2016-01-28 05:22:45 -08001948 if (RegisterFilePlayingToMixer() != 0)
1949 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001950
kwiberg55b97fe2016-01-28 05:22:45 -08001951 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001952}
1953
kwiberg55b97fe2016-01-28 05:22:45 -08001954int Channel::StopPlayingFileLocally() {
1955 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1956 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001957
kwiberg55b97fe2016-01-28 05:22:45 -08001958 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001959 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001960 }
1961
1962 {
1963 rtc::CritScope cs(&_fileCritSect);
1964
kwiberg5a25d952016-08-17 07:31:12 -07001965 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001966 _engineStatisticsPtr->SetLastError(
1967 VE_STOP_RECORDING_FAILED, kTraceError,
1968 "StopPlayingFile() could not stop playing");
1969 return -1;
1970 }
kwiberg5a25d952016-08-17 07:31:12 -07001971 output_file_player_->RegisterModuleFileCallback(NULL);
1972 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001973 channel_state_.SetOutputFilePlaying(false);
1974 }
1975 // _fileCritSect cannot be taken while calling
1976 // SetAnonymousMixibilityStatus. Refer to comments in
1977 // StartPlayingFileLocally(const char* ...) for more details.
1978 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1979 _engineStatisticsPtr->SetLastError(
1980 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1981 "StopPlayingFile() failed to stop participant from playing as"
1982 "file in the mixer");
1983 return -1;
1984 }
1985
1986 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001987}
1988
kwiberg55b97fe2016-01-28 05:22:45 -08001989int Channel::IsPlayingFileLocally() const {
1990 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001991}
1992
kwiberg55b97fe2016-01-28 05:22:45 -08001993int Channel::RegisterFilePlayingToMixer() {
1994 // Return success for not registering for file playing to mixer if:
1995 // 1. playing file before playout is started on that channel.
1996 // 2. starting playout without file playing on that channel.
1997 if (!channel_state_.Get().playing ||
1998 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001999 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002000 }
2001
2002 // |_fileCritSect| cannot be taken while calling
2003 // SetAnonymousMixabilityStatus() since as soon as the participant is added
2004 // frames can be pulled by the mixer. Since the frames are generated from
2005 // the file, _fileCritSect will be taken. This would result in a deadlock.
2006 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
2007 channel_state_.SetOutputFilePlaying(false);
2008 rtc::CritScope cs(&_fileCritSect);
2009 _engineStatisticsPtr->SetLastError(
2010 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2011 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07002012 output_file_player_->StopPlayingFile();
2013 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002014 return -1;
2015 }
2016
2017 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002018}
2019
niklase@google.com470e71d2011-07-07 08:21:25 +00002020int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002021 bool loop,
2022 FileFormats format,
2023 int startPosition,
2024 float volumeScaling,
2025 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002026 const CodecInst* codecInst) {
2027 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2028 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2029 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2030 "stopPosition=%d)",
2031 fileName, loop, format, volumeScaling, startPosition,
2032 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002033
kwiberg55b97fe2016-01-28 05:22:45 -08002034 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002035
kwiberg55b97fe2016-01-28 05:22:45 -08002036 if (channel_state_.Get().input_file_playing) {
2037 _engineStatisticsPtr->SetLastError(
2038 VE_ALREADY_PLAYING, kTraceWarning,
2039 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002040 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002041 }
2042
2043 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002044 if (input_file_player_) {
2045 input_file_player_->RegisterModuleFileCallback(NULL);
2046 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002047 }
2048
2049 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002050 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002051 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002052
kwiberg5a25d952016-08-17 07:31:12 -07002053 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002054 _engineStatisticsPtr->SetLastError(
2055 VE_INVALID_ARGUMENT, kTraceError,
2056 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2057 return -1;
2058 }
2059
2060 const uint32_t notificationTime(0);
2061
kwiberg5a25d952016-08-17 07:31:12 -07002062 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002063 fileName, loop, startPosition, volumeScaling, notificationTime,
2064 stopPosition, (const CodecInst*)codecInst) != 0) {
2065 _engineStatisticsPtr->SetLastError(
2066 VE_BAD_FILE, kTraceError,
2067 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002068 input_file_player_->StopPlayingFile();
2069 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002070 return -1;
2071 }
kwiberg5a25d952016-08-17 07:31:12 -07002072 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002073 channel_state_.SetInputFilePlaying(true);
2074
2075 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002076}
2077
2078int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002079 FileFormats format,
2080 int startPosition,
2081 float volumeScaling,
2082 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002083 const CodecInst* codecInst) {
2084 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2085 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2086 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2087 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002088
kwiberg55b97fe2016-01-28 05:22:45 -08002089 if (stream == NULL) {
2090 _engineStatisticsPtr->SetLastError(
2091 VE_BAD_FILE, kTraceError,
2092 "StartPlayingFileAsMicrophone NULL as input stream");
2093 return -1;
2094 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002095
kwiberg55b97fe2016-01-28 05:22:45 -08002096 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002097
kwiberg55b97fe2016-01-28 05:22:45 -08002098 if (channel_state_.Get().input_file_playing) {
2099 _engineStatisticsPtr->SetLastError(
2100 VE_ALREADY_PLAYING, kTraceWarning,
2101 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002102 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002103 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002104
kwiberg55b97fe2016-01-28 05:22:45 -08002105 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002106 if (input_file_player_) {
2107 input_file_player_->RegisterModuleFileCallback(NULL);
2108 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002109 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002110
kwiberg55b97fe2016-01-28 05:22:45 -08002111 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002112 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002113 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002114
kwiberg5a25d952016-08-17 07:31:12 -07002115 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002116 _engineStatisticsPtr->SetLastError(
2117 VE_INVALID_ARGUMENT, kTraceError,
2118 "StartPlayingInputFile() filePlayer format isnot correct");
2119 return -1;
2120 }
2121
2122 const uint32_t notificationTime(0);
2123
kwiberg4ec01d92016-08-22 08:43:54 -07002124 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2125 notificationTime, stopPosition,
2126 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002127 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2128 "StartPlayingFile() failed to start "
2129 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002130 input_file_player_->StopPlayingFile();
2131 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002132 return -1;
2133 }
2134
kwiberg5a25d952016-08-17 07:31:12 -07002135 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002136 channel_state_.SetInputFilePlaying(true);
2137
2138 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002139}
2140
kwiberg55b97fe2016-01-28 05:22:45 -08002141int Channel::StopPlayingFileAsMicrophone() {
2142 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2143 "Channel::StopPlayingFileAsMicrophone()");
2144
2145 rtc::CritScope cs(&_fileCritSect);
2146
2147 if (!channel_state_.Get().input_file_playing) {
2148 return 0;
2149 }
2150
kwiberg5a25d952016-08-17 07:31:12 -07002151 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002152 _engineStatisticsPtr->SetLastError(
2153 VE_STOP_RECORDING_FAILED, kTraceError,
2154 "StopPlayingFile() could not stop playing");
2155 return -1;
2156 }
kwiberg5a25d952016-08-17 07:31:12 -07002157 input_file_player_->RegisterModuleFileCallback(NULL);
2158 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002159 channel_state_.SetInputFilePlaying(false);
2160
2161 return 0;
2162}
2163
2164int Channel::IsPlayingFileAsMicrophone() const {
2165 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002166}
2167
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002168int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002169 const CodecInst* codecInst) {
2170 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2171 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002172
kwiberg55b97fe2016-01-28 05:22:45 -08002173 if (_outputFileRecording) {
2174 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2175 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002176 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002177 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002178
kwiberg55b97fe2016-01-28 05:22:45 -08002179 FileFormats format;
2180 const uint32_t notificationTime(0); // Not supported in VoE
2181 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002182
kwiberg55b97fe2016-01-28 05:22:45 -08002183 if ((codecInst != NULL) &&
2184 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2185 _engineStatisticsPtr->SetLastError(
2186 VE_BAD_ARGUMENT, kTraceError,
2187 "StartRecordingPlayout() invalid compression");
2188 return (-1);
2189 }
2190 if (codecInst == NULL) {
2191 format = kFileFormatPcm16kHzFile;
2192 codecInst = &dummyCodec;
2193 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2194 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2195 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2196 format = kFileFormatWavFile;
2197 } else {
2198 format = kFileFormatCompressedFile;
2199 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002200
kwiberg55b97fe2016-01-28 05:22:45 -08002201 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002202
kwiberg55b97fe2016-01-28 05:22:45 -08002203 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002204 if (output_file_recorder_) {
2205 output_file_recorder_->RegisterModuleFileCallback(NULL);
2206 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002207 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002208
kwiberg5a25d952016-08-17 07:31:12 -07002209 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002210 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002211 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002212 _engineStatisticsPtr->SetLastError(
2213 VE_INVALID_ARGUMENT, kTraceError,
2214 "StartRecordingPlayout() fileRecorder format isnot correct");
2215 return -1;
2216 }
2217
kwiberg5a25d952016-08-17 07:31:12 -07002218 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002219 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2220 _engineStatisticsPtr->SetLastError(
2221 VE_BAD_FILE, kTraceError,
2222 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002223 output_file_recorder_->StopRecording();
2224 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002225 return -1;
2226 }
kwiberg5a25d952016-08-17 07:31:12 -07002227 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002228 _outputFileRecording = true;
2229
2230 return 0;
2231}
2232
2233int Channel::StartRecordingPlayout(OutStream* stream,
2234 const CodecInst* codecInst) {
2235 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2236 "Channel::StartRecordingPlayout()");
2237
2238 if (_outputFileRecording) {
2239 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2240 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002241 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002242 }
2243
2244 FileFormats format;
2245 const uint32_t notificationTime(0); // Not supported in VoE
2246 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2247
2248 if (codecInst != NULL && codecInst->channels != 1) {
2249 _engineStatisticsPtr->SetLastError(
2250 VE_BAD_ARGUMENT, kTraceError,
2251 "StartRecordingPlayout() invalid compression");
2252 return (-1);
2253 }
2254 if (codecInst == NULL) {
2255 format = kFileFormatPcm16kHzFile;
2256 codecInst = &dummyCodec;
2257 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2258 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2259 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2260 format = kFileFormatWavFile;
2261 } else {
2262 format = kFileFormatCompressedFile;
2263 }
2264
2265 rtc::CritScope cs(&_fileCritSect);
2266
2267 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002268 if (output_file_recorder_) {
2269 output_file_recorder_->RegisterModuleFileCallback(NULL);
2270 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002271 }
2272
kwiberg5a25d952016-08-17 07:31:12 -07002273 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002274 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002275 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002276 _engineStatisticsPtr->SetLastError(
2277 VE_INVALID_ARGUMENT, kTraceError,
2278 "StartRecordingPlayout() fileRecorder format isnot correct");
2279 return -1;
2280 }
2281
kwiberg4ec01d92016-08-22 08:43:54 -07002282 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002283 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002284 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2285 "StartRecordingPlayout() failed to "
2286 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002287 output_file_recorder_->StopRecording();
2288 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002289 return -1;
2290 }
2291
kwiberg5a25d952016-08-17 07:31:12 -07002292 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002293 _outputFileRecording = true;
2294
2295 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002296}
2297
kwiberg55b97fe2016-01-28 05:22:45 -08002298int Channel::StopRecordingPlayout() {
2299 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2300 "Channel::StopRecordingPlayout()");
2301
2302 if (!_outputFileRecording) {
2303 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2304 "StopRecordingPlayout() isnot recording");
2305 return -1;
2306 }
2307
2308 rtc::CritScope cs(&_fileCritSect);
2309
kwiberg5a25d952016-08-17 07:31:12 -07002310 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002311 _engineStatisticsPtr->SetLastError(
2312 VE_STOP_RECORDING_FAILED, kTraceError,
2313 "StopRecording() could not stop recording");
2314 return (-1);
2315 }
kwiberg5a25d952016-08-17 07:31:12 -07002316 output_file_recorder_->RegisterModuleFileCallback(NULL);
2317 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002318 _outputFileRecording = false;
2319
2320 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002321}
2322
kwiberg55b97fe2016-01-28 05:22:45 -08002323void Channel::SetMixWithMicStatus(bool mix) {
2324 rtc::CritScope cs(&_fileCritSect);
2325 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002326}
2327
solenberg8d73f8c2017-03-08 01:52:20 -08002328int Channel::GetSpeechOutputLevel() const {
2329 return _outputAudioLevel.Level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002330}
2331
solenberg8d73f8c2017-03-08 01:52:20 -08002332int Channel::GetSpeechOutputLevelFullRange() const {
2333 return _outputAudioLevel.LevelFullRange();
kwiberg55b97fe2016-01-28 05:22:45 -08002334}
2335
zsteine76bd3a2017-07-14 12:17:49 -07002336double Channel::GetTotalOutputEnergy() const {
zstein3c451862017-07-20 09:57:42 -07002337 return _outputAudioLevel.TotalEnergy();
zsteine76bd3a2017-07-14 12:17:49 -07002338}
2339
2340double Channel::GetTotalOutputDuration() const {
zstein3c451862017-07-20 09:57:42 -07002341 return _outputAudioLevel.TotalDuration();
zsteine76bd3a2017-07-14 12:17:49 -07002342}
2343
solenberg8d73f8c2017-03-08 01:52:20 -08002344void Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002345 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002346 input_mute_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002347}
2348
solenberg1c2af8e2016-03-24 10:36:00 -07002349bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002350 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002351 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002352}
2353
solenberg8d73f8c2017-03-08 01:52:20 -08002354void Channel::SetChannelOutputVolumeScaling(float scaling) {
kwiberg55b97fe2016-01-28 05:22:45 -08002355 rtc::CritScope cs(&volume_settings_critsect_);
kwiberg55b97fe2016-01-28 05:22:45 -08002356 _outputGain = scaling;
niklase@google.com470e71d2011-07-07 08:21:25 +00002357}
2358
solenberg8842c3e2016-03-11 03:06:41 -08002359int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002360 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002361 "Channel::SendTelephoneEventOutband(...)");
2362 RTC_DCHECK_LE(0, event);
2363 RTC_DCHECK_GE(255, event);
2364 RTC_DCHECK_LE(0, duration_ms);
2365 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002366 if (!Sending()) {
2367 return -1;
2368 }
solenberg8842c3e2016-03-11 03:06:41 -08002369 if (_rtpRtcpModule->SendTelephoneEventOutband(
2370 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002371 _engineStatisticsPtr->SetLastError(
2372 VE_SEND_DTMF_FAILED, kTraceWarning,
2373 "SendTelephoneEventOutband() failed to send event");
2374 return -1;
2375 }
2376 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002377}
2378
solenbergffbbcac2016-11-17 05:25:37 -08002379int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2380 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002381 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002382 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002383 RTC_DCHECK_LE(0, payload_type);
2384 RTC_DCHECK_GE(127, payload_type);
2385 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002386 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002387 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002388 memcpy(codec.plname, "telephone-event", 16);
2389 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2390 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2391 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2392 _engineStatisticsPtr->SetLastError(
2393 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2394 "SetSendTelephoneEventPayloadType() failed to register send"
2395 "payload type");
2396 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002397 }
kwiberg55b97fe2016-01-28 05:22:45 -08002398 }
kwiberg55b97fe2016-01-28 05:22:45 -08002399 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002400}
2401
kwiberg55b97fe2016-01-28 05:22:45 -08002402int Channel::SetLocalSSRC(unsigned int ssrc) {
2403 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2404 "Channel::SetLocalSSRC()");
2405 if (channel_state_.Get().sending) {
2406 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2407 "SetLocalSSRC() already sending");
2408 return -1;
2409 }
2410 _rtpRtcpModule->SetSSRC(ssrc);
2411 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002412}
2413
kwiberg55b97fe2016-01-28 05:22:45 -08002414int Channel::GetLocalSSRC(unsigned int& ssrc) {
2415 ssrc = _rtpRtcpModule->SSRC();
2416 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002417}
2418
kwiberg55b97fe2016-01-28 05:22:45 -08002419int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2420 ssrc = rtp_receiver_->SSRC();
2421 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002422}
2423
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002424int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002425 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002426 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002427}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002428
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002429int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2430 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002431 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2432 if (enable &&
2433 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2434 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002435 return -1;
2436 }
2437 return 0;
2438}
2439
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002440void Channel::EnableSendTransportSequenceNumber(int id) {
2441 int ret =
2442 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2443 RTC_DCHECK_EQ(0, ret);
2444}
2445
stefan3313ec92016-01-21 06:32:43 -08002446void Channel::EnableReceiveTransportSequenceNumber(int id) {
2447 rtp_header_parser_->DeregisterRtpHeaderExtension(
2448 kRtpExtensionTransportSequenceNumber);
2449 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2450 kRtpExtensionTransportSequenceNumber, id);
2451 RTC_DCHECK(ret);
2452}
2453
stefanbba9dec2016-02-01 04:39:55 -08002454void Channel::RegisterSenderCongestionControlObjects(
nisseb8f9a322017-03-27 05:36:15 -07002455 RtpTransportControllerSendInterface* transport,
stefan7de8d642017-02-07 07:14:08 -08002456 RtcpBandwidthObserver* bandwidth_observer) {
nisseb8f9a322017-03-27 05:36:15 -07002457 RtpPacketSender* rtp_packet_sender = transport->packet_sender();
2458 TransportFeedbackObserver* transport_feedback_observer =
2459 transport->transport_feedback_observer();
2460 PacketRouter* packet_router = transport->packet_router();
2461
stefanbba9dec2016-02-01 04:39:55 -08002462 RTC_DCHECK(rtp_packet_sender);
2463 RTC_DCHECK(transport_feedback_observer);
kwibergee89e782017-08-09 17:22:01 -07002464 RTC_DCHECK(packet_router);
2465 RTC_DCHECK(!packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002466 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002467 feedback_observer_proxy_->SetTransportFeedbackObserver(
2468 transport_feedback_observer);
2469 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2470 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2471 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
eladalon822ff2b2017-08-01 06:30:28 -07002472 constexpr bool remb_candidate = false;
2473 packet_router->AddSendRtpModule(_rtpRtcpModule.get(), remb_candidate);
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002474 packet_router_ = packet_router;
2475}
2476
stefanbba9dec2016-02-01 04:39:55 -08002477void Channel::RegisterReceiverCongestionControlObjects(
2478 PacketRouter* packet_router) {
kwibergee89e782017-08-09 17:22:01 -07002479 RTC_DCHECK(packet_router);
2480 RTC_DCHECK(!packet_router_);
eladalon822ff2b2017-08-01 06:30:28 -07002481 constexpr bool remb_candidate = false;
2482 packet_router->AddReceiveRtpModule(_rtpRtcpModule.get(), remb_candidate);
stefanbba9dec2016-02-01 04:39:55 -08002483 packet_router_ = packet_router;
2484}
2485
nissefdbfdc92017-03-31 05:44:52 -07002486void Channel::ResetSenderCongestionControlObjects() {
stefanbba9dec2016-02-01 04:39:55 -08002487 RTC_DCHECK(packet_router_);
2488 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002489 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002490 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2491 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
nissefdbfdc92017-03-31 05:44:52 -07002492 packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002493 packet_router_ = nullptr;
2494 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2495}
2496
nissefdbfdc92017-03-31 05:44:52 -07002497void Channel::ResetReceiverCongestionControlObjects() {
2498 RTC_DCHECK(packet_router_);
2499 packet_router_->RemoveReceiveRtpModule(_rtpRtcpModule.get());
2500 packet_router_ = nullptr;
2501}
2502
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002503void Channel::SetRTCPStatus(bool enable) {
2504 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2505 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002506 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002507}
2508
kwiberg55b97fe2016-01-28 05:22:45 -08002509int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002510 RtcpMode method = _rtpRtcpModule->RTCP();
2511 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002512 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002513}
2514
kwiberg55b97fe2016-01-28 05:22:45 -08002515int Channel::SetRTCP_CNAME(const char cName[256]) {
2516 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2517 "Channel::SetRTCP_CNAME()");
2518 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2519 _engineStatisticsPtr->SetLastError(
2520 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2521 "SetRTCP_CNAME() failed to set RTCP CNAME");
2522 return -1;
2523 }
2524 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002525}
2526
kwiberg55b97fe2016-01-28 05:22:45 -08002527int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2528 if (cName == NULL) {
2529 _engineStatisticsPtr->SetLastError(
2530 VE_INVALID_ARGUMENT, kTraceError,
2531 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2532 return -1;
2533 }
2534 char cname[RTCP_CNAME_SIZE];
2535 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2536 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2537 _engineStatisticsPtr->SetLastError(
2538 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2539 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2540 return -1;
2541 }
2542 strcpy(cName, cname);
2543 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002544}
2545
kwiberg55b97fe2016-01-28 05:22:45 -08002546int Channel::SendApplicationDefinedRTCPPacket(
2547 unsigned char subType,
2548 unsigned int name,
2549 const char* data,
2550 unsigned short dataLengthInBytes) {
2551 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2552 "Channel::SendApplicationDefinedRTCPPacket()");
2553 if (!channel_state_.Get().sending) {
2554 _engineStatisticsPtr->SetLastError(
2555 VE_NOT_SENDING, kTraceError,
2556 "SendApplicationDefinedRTCPPacket() not sending");
2557 return -1;
2558 }
2559 if (NULL == data) {
2560 _engineStatisticsPtr->SetLastError(
2561 VE_INVALID_ARGUMENT, kTraceError,
2562 "SendApplicationDefinedRTCPPacket() invalid data value");
2563 return -1;
2564 }
2565 if (dataLengthInBytes % 4 != 0) {
2566 _engineStatisticsPtr->SetLastError(
2567 VE_INVALID_ARGUMENT, kTraceError,
2568 "SendApplicationDefinedRTCPPacket() invalid length value");
2569 return -1;
2570 }
2571 RtcpMode status = _rtpRtcpModule->RTCP();
2572 if (status == RtcpMode::kOff) {
2573 _engineStatisticsPtr->SetLastError(
2574 VE_RTCP_ERROR, kTraceError,
2575 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2576 return -1;
2577 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002578
kwiberg55b97fe2016-01-28 05:22:45 -08002579 // Create and schedule the RTCP APP packet for transmission
2580 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2581 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2582 _engineStatisticsPtr->SetLastError(
2583 VE_SEND_ERROR, kTraceError,
2584 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2585 return -1;
2586 }
2587 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002588}
2589
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002590int Channel::GetRemoteRTCPReportBlocks(
2591 std::vector<ReportBlock>* report_blocks) {
2592 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002593 _engineStatisticsPtr->SetLastError(
2594 VE_INVALID_ARGUMENT, kTraceError,
2595 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002596 return -1;
2597 }
2598
2599 // Get the report blocks from the latest received RTCP Sender or Receiver
2600 // Report. Each element in the vector contains the sender's SSRC and a
2601 // report block according to RFC 3550.
2602 std::vector<RTCPReportBlock> rtcp_report_blocks;
2603 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002604 return -1;
2605 }
2606
2607 if (rtcp_report_blocks.empty())
2608 return 0;
2609
2610 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2611 for (; it != rtcp_report_blocks.end(); ++it) {
2612 ReportBlock report_block;
srte3e69e5c2017-08-09 06:13:45 -07002613 report_block.sender_SSRC = it->sender_ssrc;
2614 report_block.source_SSRC = it->source_ssrc;
2615 report_block.fraction_lost = it->fraction_lost;
2616 report_block.cumulative_num_packets_lost = it->packets_lost;
2617 report_block.extended_highest_sequence_number =
2618 it->extended_highest_sequence_number;
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002619 report_block.interarrival_jitter = it->jitter;
srte3e69e5c2017-08-09 06:13:45 -07002620 report_block.last_SR_timestamp = it->last_sender_report_timestamp;
2621 report_block.delay_since_last_SR = it->delay_since_last_sender_report;
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002622 report_blocks->push_back(report_block);
2623 }
2624 return 0;
2625}
2626
kwiberg55b97fe2016-01-28 05:22:45 -08002627int Channel::GetRTPStatistics(CallStatistics& stats) {
2628 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002629
kwiberg55b97fe2016-01-28 05:22:45 -08002630 // The jitter statistics is updated for each received RTP packet and is
2631 // based on received packets.
2632 RtcpStatistics statistics;
2633 StreamStatistician* statistician =
2634 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002635 if (statistician) {
2636 statistician->GetStatistics(&statistics,
2637 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002638 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002639
kwiberg55b97fe2016-01-28 05:22:45 -08002640 stats.fractionLost = statistics.fraction_lost;
srte186d9c32017-08-04 05:03:53 -07002641 stats.cumulativeLost = statistics.packets_lost;
2642 stats.extendedMax = statistics.extended_highest_sequence_number;
kwiberg55b97fe2016-01-28 05:22:45 -08002643 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002644
kwiberg55b97fe2016-01-28 05:22:45 -08002645 // --- RTT
2646 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002647
kwiberg55b97fe2016-01-28 05:22:45 -08002648 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002649
kwiberg55b97fe2016-01-28 05:22:45 -08002650 size_t bytesSent(0);
2651 uint32_t packetsSent(0);
2652 size_t bytesReceived(0);
2653 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002654
kwiberg55b97fe2016-01-28 05:22:45 -08002655 if (statistician) {
2656 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2657 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002658
kwiberg55b97fe2016-01-28 05:22:45 -08002659 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2660 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2661 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2662 " output will not be complete");
2663 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002664
kwiberg55b97fe2016-01-28 05:22:45 -08002665 stats.bytesSent = bytesSent;
2666 stats.packetsSent = packetsSent;
2667 stats.bytesReceived = bytesReceived;
2668 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002669
kwiberg55b97fe2016-01-28 05:22:45 -08002670 // --- Timestamps
2671 {
2672 rtc::CritScope lock(&ts_stats_lock_);
2673 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2674 }
2675 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002676}
2677
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002678int Channel::SetCodecFECStatus(bool enable) {
2679 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2680 "Channel::SetCodecFECStatus()");
2681
kwibergc8d071e2016-04-06 12:22:38 -07002682 if (!codec_manager_.SetCodecFEC(enable) ||
2683 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002684 _engineStatisticsPtr->SetLastError(
2685 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2686 "SetCodecFECStatus() failed to set FEC state");
2687 return -1;
2688 }
2689 return 0;
2690}
2691
2692bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002693 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002694}
2695
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002696void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2697 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002698 // If pacing is enabled we always store packets.
2699 if (!pacing_enabled_)
2700 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002701 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002702 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002703 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002704 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002705 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002706}
2707
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002708// Called when we are missing one or more packets.
2709int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002710 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2711}
2712
henrikaec6fbd22017-03-31 05:43:36 -07002713void Channel::ProcessAndEncodeAudio(const AudioFrame& audio_input) {
henrika4515fa02017-05-03 08:30:15 -07002714 // Avoid posting any new tasks if sending was already stopped in StopSend().
2715 rtc::CritScope cs(&encoder_queue_lock_);
2716 if (!encoder_queue_is_active_) {
2717 return;
2718 }
henrikaec6fbd22017-03-31 05:43:36 -07002719 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
2720 // TODO(henrika): try to avoid copying by moving ownership of audio frame
2721 // either into pool of frames or into the task itself.
2722 audio_frame->CopyFrom(audio_input);
2723 audio_frame->id_ = ChannelId();
2724 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
2725 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
niklase@google.com470e71d2011-07-07 08:21:25 +00002726}
2727
henrikaec6fbd22017-03-31 05:43:36 -07002728void Channel::ProcessAndEncodeAudio(const int16_t* audio_data,
2729 int sample_rate,
2730 size_t number_of_frames,
2731 size_t number_of_channels) {
henrika4515fa02017-05-03 08:30:15 -07002732 // Avoid posting as new task if sending was already stopped in StopSend().
2733 rtc::CritScope cs(&encoder_queue_lock_);
2734 if (!encoder_queue_is_active_) {
2735 return;
2736 }
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002737 CodecInst codec;
ossu950c1c92017-07-11 08:19:31 -07002738 const int result = GetSendCodec(codec);
henrikaec6fbd22017-03-31 05:43:36 -07002739 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
2740 audio_frame->id_ = ChannelId();
ossu950c1c92017-07-11 08:19:31 -07002741 // TODO(ossu): Investigate how this could happen. b/62909493
2742 if (result == 0) {
2743 audio_frame->sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2744 audio_frame->num_channels_ = std::min(number_of_channels, codec.channels);
2745 } else {
2746 audio_frame->sample_rate_hz_ = sample_rate;
2747 audio_frame->num_channels_ = number_of_channels;
2748 LOG(LS_WARNING) << "Unable to get send codec for channel " << ChannelId();
2749 RTC_NOTREACHED();
2750 }
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002751 RemixAndResample(audio_data, number_of_frames, number_of_channels,
henrikaec6fbd22017-03-31 05:43:36 -07002752 sample_rate, &input_resampler_, audio_frame.get());
2753 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
2754 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002755}
2756
henrikaec6fbd22017-03-31 05:43:36 -07002757void Channel::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
2758 RTC_DCHECK_RUN_ON(encoder_queue_);
2759 RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
2760 RTC_DCHECK_LE(audio_input->num_channels_, 2);
2761 RTC_DCHECK_EQ(audio_input->id_, ChannelId());
kwiberg55b97fe2016-01-28 05:22:45 -08002762
2763 if (channel_state_.Get().input_file_playing) {
henrikaec6fbd22017-03-31 05:43:36 -07002764 MixOrReplaceAudioWithFile(audio_input);
kwiberg55b97fe2016-01-28 05:22:45 -08002765 }
2766
henrikaec6fbd22017-03-31 05:43:36 -07002767 bool is_muted = InputMute();
2768 AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002769
kwiberg55b97fe2016-01-28 05:22:45 -08002770 if (_includeAudioLevelIndication) {
2771 size_t length =
henrikaec6fbd22017-03-31 05:43:36 -07002772 audio_input->samples_per_channel_ * audio_input->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002773 RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes);
solenberg1c2af8e2016-03-24 10:36:00 -07002774 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002775 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002776 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002777 rms_level_.Analyze(
yujo36b1a5f2017-06-12 12:45:32 -07002778 rtc::ArrayView<const int16_t>(audio_input->data(), length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002779 }
kwiberg55b97fe2016-01-28 05:22:45 -08002780 }
solenberg1c2af8e2016-03-24 10:36:00 -07002781 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002782
henrikaec6fbd22017-03-31 05:43:36 -07002783 // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002784
kwiberg55b97fe2016-01-28 05:22:45 -08002785 // The ACM resamples internally.
henrikaec6fbd22017-03-31 05:43:36 -07002786 audio_input->timestamp_ = _timeStamp;
kwiberg55b97fe2016-01-28 05:22:45 -08002787 // This call will trigger AudioPacketizationCallback::SendData if encoding
2788 // is done and payload is ready for packetization and transmission.
2789 // Otherwise, it will return without invoking the callback.
henrikaec6fbd22017-03-31 05:43:36 -07002790 if (audio_coding_->Add10MsData(*audio_input) < 0) {
2791 LOG(LS_ERROR) << "ACM::Add10MsData() failed for channel " << _channelId;
2792 return;
kwiberg55b97fe2016-01-28 05:22:45 -08002793 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002794
henrikaec6fbd22017-03-31 05:43:36 -07002795 _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002796}
2797
solenberg7602aab2016-11-14 11:30:07 -08002798void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2799 RTC_DCHECK(!channel.channel() ||
2800 channel.channel()->ChannelId() != _channelId);
2801 rtc::CritScope lock(&assoc_send_channel_lock_);
2802 associate_send_channel_ = channel;
2803}
2804
Minyue2013aec2015-05-13 14:14:42 +02002805void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002806 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002807 Channel* channel = associate_send_channel_.channel();
2808 if (channel && channel->ChannelId() == channel_id) {
2809 // If this channel is associated with a send channel of the specified
2810 // Channel ID, disassociate with it.
2811 ChannelOwner ref(NULL);
2812 associate_send_channel_ = ref;
2813 }
2814}
2815
ivoc14d5dbe2016-07-04 07:06:55 -07002816void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2817 event_log_proxy_->SetEventLog(event_log);
2818}
2819
michaelt9332b7d2016-11-30 07:51:13 -08002820void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2821 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2822}
2823
nisse284542b2017-01-10 08:58:32 -08002824void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08002825 size_t overhead_per_packet =
2826 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08002827 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2828 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08002829 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08002830 }
2831 });
2832}
2833
2834void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002835 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002836 transport_overhead_per_packet_ = transport_overhead_per_packet;
2837 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002838}
2839
hbos3fd31fe2017-02-28 05:43:16 -08002840// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08002841void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002842 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002843 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2844 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002845}
2846
kwiberg55b97fe2016-01-28 05:22:45 -08002847int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2848 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002849}
2850
wu@webrtc.org24301a62013-12-13 19:17:43 +00002851void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2852 audio_coding_->GetDecodingCallStatistics(stats);
2853}
2854
ivoce1198e02017-09-08 08:13:19 -07002855ANAStats Channel::GetANAStatistics() const {
2856 return audio_coding_->GetANAStats();
2857}
2858
solenberg358057b2015-11-27 10:46:42 -08002859uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002860 rtc::CritScope lock(&video_sync_lock_);
2861 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002862}
2863
kwiberg55b97fe2016-01-28 05:22:45 -08002864int Channel::SetMinimumPlayoutDelay(int delayMs) {
2865 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2866 "Channel::SetMinimumPlayoutDelay()");
2867 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2868 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2869 _engineStatisticsPtr->SetLastError(
2870 VE_INVALID_ARGUMENT, kTraceError,
2871 "SetMinimumPlayoutDelay() invalid min delay");
2872 return -1;
2873 }
2874 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2875 _engineStatisticsPtr->SetLastError(
2876 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2877 "SetMinimumPlayoutDelay() failed to set min playout delay");
2878 return -1;
2879 }
2880 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002881}
2882
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002883int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002884 uint32_t playout_timestamp_rtp = 0;
2885 {
tommi31fc21f2016-01-21 10:37:37 -08002886 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002887 playout_timestamp_rtp = playout_timestamp_rtp_;
2888 }
kwiberg55b97fe2016-01-28 05:22:45 -08002889 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002890 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002891 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002892 "GetPlayoutTimestamp() failed to retrieve timestamp");
2893 return -1;
2894 }
deadbeef74375882015-08-13 12:09:10 -07002895 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002896 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002897}
2898
kwiberg55b97fe2016-01-28 05:22:45 -08002899int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2900 RtpReceiver** rtp_receiver) const {
2901 *rtpRtcpModule = _rtpRtcpModule.get();
2902 *rtp_receiver = rtp_receiver_.get();
2903 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002904}
2905
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002906// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2907// a shared helper.
henrikaec6fbd22017-03-31 05:43:36 -07002908int32_t Channel::MixOrReplaceAudioWithFile(AudioFrame* audio_input) {
2909 RTC_DCHECK_RUN_ON(encoder_queue_);
kwibergb7f89d62016-02-17 10:04:18 -08002910 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002911 size_t fileSamples(0);
henrikaec6fbd22017-03-31 05:43:36 -07002912 const int mixingFrequency = audio_input->sample_rate_hz_;
kwiberg55b97fe2016-01-28 05:22:45 -08002913 {
2914 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002915
kwiberg5a25d952016-08-17 07:31:12 -07002916 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002917 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2918 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2919 " doesnt exist");
2920 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002921 }
2922
kwiberg4ec01d92016-08-22 08:43:54 -07002923 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002924 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002925 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2926 "Channel::MixOrReplaceAudioWithFile() file mixing "
2927 "failed");
2928 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002929 }
kwiberg55b97fe2016-01-28 05:22:45 -08002930 if (fileSamples == 0) {
2931 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2932 "Channel::MixOrReplaceAudioWithFile() file is ended");
2933 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002934 }
kwiberg55b97fe2016-01-28 05:22:45 -08002935 }
2936
henrikaec6fbd22017-03-31 05:43:36 -07002937 RTC_DCHECK_EQ(audio_input->samples_per_channel_, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08002938
2939 if (_mixFileWithMicrophone) {
2940 // Currently file stream is always mono.
2941 // TODO(xians): Change the code when FilePlayer supports real stereo.
yujo36b1a5f2017-06-12 12:45:32 -07002942 MixWithSat(audio_input->mutable_data(), audio_input->num_channels_,
2943 fileBuffer.get(), 1, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08002944 } else {
2945 // Replace ACM audio with file.
2946 // Currently file stream is always mono.
2947 // TODO(xians): Change the code when FilePlayer supports real stereo.
henrikaec6fbd22017-03-31 05:43:36 -07002948 audio_input->UpdateFrame(
kwiberg55b97fe2016-01-28 05:22:45 -08002949 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2950 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2951 }
2952 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002953}
2954
kwiberg55b97fe2016-01-28 05:22:45 -08002955int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2956 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002957
kwibergb7f89d62016-02-17 10:04:18 -08002958 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002959 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002960
kwiberg55b97fe2016-01-28 05:22:45 -08002961 {
2962 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002963
kwiberg5a25d952016-08-17 07:31:12 -07002964 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002965 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2966 "Channel::MixAudioWithFile() file mixing failed");
2967 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002968 }
2969
kwiberg55b97fe2016-01-28 05:22:45 -08002970 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002971 if (output_file_player_->Get10msAudioFromFile(
2972 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002973 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2974 "Channel::MixAudioWithFile() file mixing failed");
2975 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002976 }
kwiberg55b97fe2016-01-28 05:22:45 -08002977 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002978
kwiberg55b97fe2016-01-28 05:22:45 -08002979 if (audioFrame.samples_per_channel_ == fileSamples) {
2980 // Currently file stream is always mono.
2981 // TODO(xians): Change the code when FilePlayer supports real stereo.
yujo36b1a5f2017-06-12 12:45:32 -07002982 MixWithSat(audioFrame.mutable_data(), audioFrame.num_channels_,
2983 fileBuffer.get(), 1, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08002984 } else {
2985 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2986 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2987 ") != "
2988 "fileSamples(%" PRIuS ")",
2989 audioFrame.samples_per_channel_, fileSamples);
2990 return -1;
2991 }
2992
2993 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002994}
2995
deadbeef74375882015-08-13 12:09:10 -07002996void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002997 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002998
henrik.lundin96bd5022016-04-06 04:13:56 -07002999 if (!jitter_buffer_playout_timestamp_) {
3000 // This can happen if this channel has not received any RTP packets. In
3001 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003002 return;
3003 }
3004
3005 uint16_t delay_ms = 0;
3006 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003007 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003008 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3009 " delay from the ADM");
3010 _engineStatisticsPtr->SetLastError(
3011 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3012 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3013 return;
3014 }
3015
henrik.lundin96bd5022016-04-06 04:13:56 -07003016 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3017 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003018
3019 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003020 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003021
kwiberg55b97fe2016-01-28 05:22:45 -08003022 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003023 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003024 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003025
3026 {
tommi31fc21f2016-01-21 10:37:37 -08003027 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08003028 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003029 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003030 }
3031 playout_delay_ms_ = delay_ms;
3032 }
3033}
3034
kwiberg55b97fe2016-01-28 05:22:45 -08003035void Channel::RegisterReceiveCodecsToRTPModule() {
3036 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3037 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003038
kwiberg55b97fe2016-01-28 05:22:45 -08003039 CodecInst codec;
3040 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003041
kwiberg55b97fe2016-01-28 05:22:45 -08003042 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3043 // Open up the RTP/RTCP receiver for all supported codecs
3044 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08003045 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08003046 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3047 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3048 " to register %s (%d/%d/%" PRIuS
3049 "/%d) to RTP/RTCP "
3050 "receiver",
3051 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3052 codec.rate);
3053 } else {
3054 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3055 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3056 "(%d/%d/%" PRIuS
3057 "/%d) has been added to the RTP/RTCP "
3058 "receiver",
3059 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3060 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003061 }
kwiberg55b97fe2016-01-28 05:22:45 -08003062 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003063}
3064
kwiberg55b97fe2016-01-28 05:22:45 -08003065int Channel::SetSendRtpHeaderExtension(bool enable,
3066 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003067 unsigned char id) {
3068 int error = 0;
3069 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3070 if (enable) {
3071 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3072 }
3073 return error;
3074}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003075
ossue280cde2016-10-12 11:04:10 -07003076int Channel::GetRtpTimestampRateHz() const {
3077 const auto format = audio_coding_->ReceiveFormat();
3078 // Default to the playout frequency if we've not gotten any packets yet.
3079 // TODO(ossu): Zero clockrate can only happen if we've added an external
3080 // decoder for a format we don't support internally. Remove once that way of
3081 // adding decoders is gone!
3082 return (format && format->clockrate_hz != 0)
3083 ? format->clockrate_hz
3084 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003085}
3086
Minyue2013aec2015-05-13 14:14:42 +02003087int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003088 RtcpMode method = _rtpRtcpModule->RTCP();
3089 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003090 return 0;
3091 }
3092 std::vector<RTCPReportBlock> report_blocks;
3093 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003094
3095 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003096 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003097 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003098 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003099 Channel* channel = associate_send_channel_.channel();
3100 // Tries to get RTT from an associated channel. This is important for
3101 // receive-only channels.
3102 if (channel) {
3103 // To prevent infinite recursion and deadlock, calling GetRTT of
3104 // associate channel should always use "false" for argument:
3105 // |allow_associate_channel|.
3106 rtt = channel->GetRTT(false);
3107 }
3108 }
3109 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003110 }
3111
3112 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3113 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3114 for (; it != report_blocks.end(); ++it) {
srte3e69e5c2017-08-09 06:13:45 -07003115 if (it->sender_ssrc == remoteSSRC)
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003116 break;
3117 }
3118 if (it == report_blocks.end()) {
3119 // We have not received packets with SSRC matching the report blocks.
3120 // To calculate RTT we try with the SSRC of the first report block.
3121 // This is very important for send-only channels where we don't know
3122 // the SSRC of the other end.
srte3e69e5c2017-08-09 06:13:45 -07003123 remoteSSRC = report_blocks[0].sender_ssrc;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003124 }
Minyue2013aec2015-05-13 14:14:42 +02003125
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003126 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003127 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003128 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003129 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3130 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003131 return 0;
3132 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003133 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003134}
3135
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003136} // namespace voe
3137} // namespace webrtc