blob: 746db8114c600ab0d7afa7c0e2b4007d78b4b8da [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
mflodman3d7db262016-04-29 00:57:13 -07001787 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001788 _engineStatisticsPtr->SetLastError(
1789 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1790 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1791 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001792
Minyue2013aec2015-05-13 14:14:42 +02001793 int64_t rtt = GetRTT(true);
1794 if (rtt == 0) {
1795 // Waiting for valid RTT.
1796 return 0;
1797 }
Erik Språng737336d2016-07-29 12:59:36 +02001798
1799 int64_t nack_window_ms = rtt;
1800 if (nack_window_ms < kMinRetransmissionWindowMs) {
1801 nack_window_ms = kMinRetransmissionWindowMs;
1802 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1803 nack_window_ms = kMaxRetransmissionWindowMs;
1804 }
1805 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1806
minyue7e304322016-10-12 05:00:55 -07001807 // Invoke audio encoders OnReceivedRtt().
1808 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1809 if (*encoder)
1810 (*encoder)->OnReceivedRtt(rtt);
1811 });
1812
Minyue2013aec2015-05-13 14:14:42 +02001813 uint32_t ntp_secs = 0;
1814 uint32_t ntp_frac = 0;
1815 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001816 if (0 !=
1817 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1818 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001819 // Waiting for RTCP.
1820 return 0;
1821 }
1822
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001823 {
tommi31fc21f2016-01-21 10:37:37 -08001824 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001825 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001826 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001827 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001828}
1829
niklase@google.com470e71d2011-07-07 08:21:25 +00001830int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001831 bool loop,
1832 FileFormats format,
1833 int startPosition,
1834 float volumeScaling,
1835 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001836 const CodecInst* codecInst) {
1837 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1838 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1839 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1840 "stopPosition=%d)",
1841 fileName, loop, format, volumeScaling, startPosition,
1842 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001843
kwiberg55b97fe2016-01-28 05:22:45 -08001844 if (channel_state_.Get().output_file_playing) {
1845 _engineStatisticsPtr->SetLastError(
1846 VE_ALREADY_PLAYING, kTraceError,
1847 "StartPlayingFileLocally() is already playing");
1848 return -1;
1849 }
1850
1851 {
1852 rtc::CritScope cs(&_fileCritSect);
1853
kwiberg5a25d952016-08-17 07:31:12 -07001854 if (output_file_player_) {
1855 output_file_player_->RegisterModuleFileCallback(NULL);
1856 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001857 }
1858
kwiberg5b356f42016-09-08 04:32:33 -07001859 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001860 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001861
kwiberg5a25d952016-08-17 07:31:12 -07001862 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001863 _engineStatisticsPtr->SetLastError(
1864 VE_INVALID_ARGUMENT, kTraceError,
1865 "StartPlayingFileLocally() filePlayer format is not correct");
1866 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001867 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001868
kwiberg55b97fe2016-01-28 05:22:45 -08001869 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001870
kwiberg5a25d952016-08-17 07:31:12 -07001871 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001872 fileName, loop, startPosition, volumeScaling, notificationTime,
1873 stopPosition, (const CodecInst*)codecInst) != 0) {
1874 _engineStatisticsPtr->SetLastError(
1875 VE_BAD_FILE, kTraceError,
1876 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001877 output_file_player_->StopPlayingFile();
1878 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001879 return -1;
1880 }
kwiberg5a25d952016-08-17 07:31:12 -07001881 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001882 channel_state_.SetOutputFilePlaying(true);
1883 }
1884
1885 if (RegisterFilePlayingToMixer() != 0)
1886 return -1;
1887
1888 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001889}
1890
1891int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001892 FileFormats format,
1893 int startPosition,
1894 float volumeScaling,
1895 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001896 const CodecInst* codecInst) {
1897 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1898 "Channel::StartPlayingFileLocally(format=%d,"
1899 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1900 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001901
kwiberg55b97fe2016-01-28 05:22:45 -08001902 if (stream == NULL) {
1903 _engineStatisticsPtr->SetLastError(
1904 VE_BAD_FILE, kTraceError,
1905 "StartPlayingFileLocally() NULL as input stream");
1906 return -1;
1907 }
1908
1909 if (channel_state_.Get().output_file_playing) {
1910 _engineStatisticsPtr->SetLastError(
1911 VE_ALREADY_PLAYING, kTraceError,
1912 "StartPlayingFileLocally() is already playing");
1913 return -1;
1914 }
1915
1916 {
1917 rtc::CritScope cs(&_fileCritSect);
1918
1919 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001920 if (output_file_player_) {
1921 output_file_player_->RegisterModuleFileCallback(NULL);
1922 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001923 }
1924
kwiberg55b97fe2016-01-28 05:22:45 -08001925 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001926 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001927 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001928
kwiberg5a25d952016-08-17 07:31:12 -07001929 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001930 _engineStatisticsPtr->SetLastError(
1931 VE_INVALID_ARGUMENT, kTraceError,
1932 "StartPlayingFileLocally() filePlayer format isnot correct");
1933 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001934 }
1935
kwiberg55b97fe2016-01-28 05:22:45 -08001936 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001937
kwiberg4ec01d92016-08-22 08:43:54 -07001938 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001939 volumeScaling, notificationTime,
1940 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001941 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1942 "StartPlayingFile() failed to "
1943 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001944 output_file_player_->StopPlayingFile();
1945 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001946 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001947 }
kwiberg5a25d952016-08-17 07:31:12 -07001948 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001949 channel_state_.SetOutputFilePlaying(true);
1950 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001951
kwiberg55b97fe2016-01-28 05:22:45 -08001952 if (RegisterFilePlayingToMixer() != 0)
1953 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001954
kwiberg55b97fe2016-01-28 05:22:45 -08001955 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001956}
1957
kwiberg55b97fe2016-01-28 05:22:45 -08001958int Channel::StopPlayingFileLocally() {
1959 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1960 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001961
kwiberg55b97fe2016-01-28 05:22:45 -08001962 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001963 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001964 }
1965
1966 {
1967 rtc::CritScope cs(&_fileCritSect);
1968
kwiberg5a25d952016-08-17 07:31:12 -07001969 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001970 _engineStatisticsPtr->SetLastError(
1971 VE_STOP_RECORDING_FAILED, kTraceError,
1972 "StopPlayingFile() could not stop playing");
1973 return -1;
1974 }
kwiberg5a25d952016-08-17 07:31:12 -07001975 output_file_player_->RegisterModuleFileCallback(NULL);
1976 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001977 channel_state_.SetOutputFilePlaying(false);
1978 }
1979 // _fileCritSect cannot be taken while calling
1980 // SetAnonymousMixibilityStatus. Refer to comments in
1981 // StartPlayingFileLocally(const char* ...) for more details.
1982 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1983 _engineStatisticsPtr->SetLastError(
1984 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1985 "StopPlayingFile() failed to stop participant from playing as"
1986 "file in the mixer");
1987 return -1;
1988 }
1989
1990 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001991}
1992
kwiberg55b97fe2016-01-28 05:22:45 -08001993int Channel::IsPlayingFileLocally() const {
1994 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001995}
1996
kwiberg55b97fe2016-01-28 05:22:45 -08001997int Channel::RegisterFilePlayingToMixer() {
1998 // Return success for not registering for file playing to mixer if:
1999 // 1. playing file before playout is started on that channel.
2000 // 2. starting playout without file playing on that channel.
2001 if (!channel_state_.Get().playing ||
2002 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002003 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002004 }
2005
2006 // |_fileCritSect| cannot be taken while calling
2007 // SetAnonymousMixabilityStatus() since as soon as the participant is added
2008 // frames can be pulled by the mixer. Since the frames are generated from
2009 // the file, _fileCritSect will be taken. This would result in a deadlock.
2010 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
2011 channel_state_.SetOutputFilePlaying(false);
2012 rtc::CritScope cs(&_fileCritSect);
2013 _engineStatisticsPtr->SetLastError(
2014 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2015 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07002016 output_file_player_->StopPlayingFile();
2017 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002018 return -1;
2019 }
2020
2021 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002022}
2023
niklase@google.com470e71d2011-07-07 08:21:25 +00002024int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002025 bool loop,
2026 FileFormats format,
2027 int startPosition,
2028 float volumeScaling,
2029 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002030 const CodecInst* codecInst) {
2031 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2032 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2033 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2034 "stopPosition=%d)",
2035 fileName, loop, format, volumeScaling, startPosition,
2036 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002037
kwiberg55b97fe2016-01-28 05:22:45 -08002038 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002039
kwiberg55b97fe2016-01-28 05:22:45 -08002040 if (channel_state_.Get().input_file_playing) {
2041 _engineStatisticsPtr->SetLastError(
2042 VE_ALREADY_PLAYING, kTraceWarning,
2043 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002044 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002045 }
2046
2047 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002048 if (input_file_player_) {
2049 input_file_player_->RegisterModuleFileCallback(NULL);
2050 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002051 }
2052
2053 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002054 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002055 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002056
kwiberg5a25d952016-08-17 07:31:12 -07002057 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002058 _engineStatisticsPtr->SetLastError(
2059 VE_INVALID_ARGUMENT, kTraceError,
2060 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2061 return -1;
2062 }
2063
2064 const uint32_t notificationTime(0);
2065
kwiberg5a25d952016-08-17 07:31:12 -07002066 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002067 fileName, loop, startPosition, volumeScaling, notificationTime,
2068 stopPosition, (const CodecInst*)codecInst) != 0) {
2069 _engineStatisticsPtr->SetLastError(
2070 VE_BAD_FILE, kTraceError,
2071 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002072 input_file_player_->StopPlayingFile();
2073 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002074 return -1;
2075 }
kwiberg5a25d952016-08-17 07:31:12 -07002076 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002077 channel_state_.SetInputFilePlaying(true);
2078
2079 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002080}
2081
2082int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002083 FileFormats format,
2084 int startPosition,
2085 float volumeScaling,
2086 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002087 const CodecInst* codecInst) {
2088 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2089 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2090 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2091 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002092
kwiberg55b97fe2016-01-28 05:22:45 -08002093 if (stream == NULL) {
2094 _engineStatisticsPtr->SetLastError(
2095 VE_BAD_FILE, kTraceError,
2096 "StartPlayingFileAsMicrophone NULL as input stream");
2097 return -1;
2098 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002099
kwiberg55b97fe2016-01-28 05:22:45 -08002100 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002101
kwiberg55b97fe2016-01-28 05:22:45 -08002102 if (channel_state_.Get().input_file_playing) {
2103 _engineStatisticsPtr->SetLastError(
2104 VE_ALREADY_PLAYING, kTraceWarning,
2105 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002106 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002107 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002108
kwiberg55b97fe2016-01-28 05:22:45 -08002109 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002110 if (input_file_player_) {
2111 input_file_player_->RegisterModuleFileCallback(NULL);
2112 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002113 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002114
kwiberg55b97fe2016-01-28 05:22:45 -08002115 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002116 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002117 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002118
kwiberg5a25d952016-08-17 07:31:12 -07002119 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002120 _engineStatisticsPtr->SetLastError(
2121 VE_INVALID_ARGUMENT, kTraceError,
2122 "StartPlayingInputFile() filePlayer format isnot correct");
2123 return -1;
2124 }
2125
2126 const uint32_t notificationTime(0);
2127
kwiberg4ec01d92016-08-22 08:43:54 -07002128 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2129 notificationTime, stopPosition,
2130 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002131 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2132 "StartPlayingFile() failed to start "
2133 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002134 input_file_player_->StopPlayingFile();
2135 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002136 return -1;
2137 }
2138
kwiberg5a25d952016-08-17 07:31:12 -07002139 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002140 channel_state_.SetInputFilePlaying(true);
2141
2142 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002143}
2144
kwiberg55b97fe2016-01-28 05:22:45 -08002145int Channel::StopPlayingFileAsMicrophone() {
2146 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2147 "Channel::StopPlayingFileAsMicrophone()");
2148
2149 rtc::CritScope cs(&_fileCritSect);
2150
2151 if (!channel_state_.Get().input_file_playing) {
2152 return 0;
2153 }
2154
kwiberg5a25d952016-08-17 07:31:12 -07002155 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002156 _engineStatisticsPtr->SetLastError(
2157 VE_STOP_RECORDING_FAILED, kTraceError,
2158 "StopPlayingFile() could not stop playing");
2159 return -1;
2160 }
kwiberg5a25d952016-08-17 07:31:12 -07002161 input_file_player_->RegisterModuleFileCallback(NULL);
2162 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002163 channel_state_.SetInputFilePlaying(false);
2164
2165 return 0;
2166}
2167
2168int Channel::IsPlayingFileAsMicrophone() const {
2169 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002170}
2171
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002172int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002173 const CodecInst* codecInst) {
2174 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2175 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002176
kwiberg55b97fe2016-01-28 05:22:45 -08002177 if (_outputFileRecording) {
2178 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2179 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002180 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002181 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002182
kwiberg55b97fe2016-01-28 05:22:45 -08002183 FileFormats format;
2184 const uint32_t notificationTime(0); // Not supported in VoE
2185 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002186
kwiberg55b97fe2016-01-28 05:22:45 -08002187 if ((codecInst != NULL) &&
2188 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2189 _engineStatisticsPtr->SetLastError(
2190 VE_BAD_ARGUMENT, kTraceError,
2191 "StartRecordingPlayout() invalid compression");
2192 return (-1);
2193 }
2194 if (codecInst == NULL) {
2195 format = kFileFormatPcm16kHzFile;
2196 codecInst = &dummyCodec;
2197 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2198 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2199 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2200 format = kFileFormatWavFile;
2201 } else {
2202 format = kFileFormatCompressedFile;
2203 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002204
kwiberg55b97fe2016-01-28 05:22:45 -08002205 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002206
kwiberg55b97fe2016-01-28 05:22:45 -08002207 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002208 if (output_file_recorder_) {
2209 output_file_recorder_->RegisterModuleFileCallback(NULL);
2210 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002211 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002212
kwiberg5a25d952016-08-17 07:31:12 -07002213 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002214 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002215 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002216 _engineStatisticsPtr->SetLastError(
2217 VE_INVALID_ARGUMENT, kTraceError,
2218 "StartRecordingPlayout() fileRecorder format isnot correct");
2219 return -1;
2220 }
2221
kwiberg5a25d952016-08-17 07:31:12 -07002222 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002223 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2224 _engineStatisticsPtr->SetLastError(
2225 VE_BAD_FILE, kTraceError,
2226 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002227 output_file_recorder_->StopRecording();
2228 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002229 return -1;
2230 }
kwiberg5a25d952016-08-17 07:31:12 -07002231 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002232 _outputFileRecording = true;
2233
2234 return 0;
2235}
2236
2237int Channel::StartRecordingPlayout(OutStream* stream,
2238 const CodecInst* codecInst) {
2239 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2240 "Channel::StartRecordingPlayout()");
2241
2242 if (_outputFileRecording) {
2243 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2244 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002245 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002246 }
2247
2248 FileFormats format;
2249 const uint32_t notificationTime(0); // Not supported in VoE
2250 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2251
2252 if (codecInst != NULL && codecInst->channels != 1) {
2253 _engineStatisticsPtr->SetLastError(
2254 VE_BAD_ARGUMENT, kTraceError,
2255 "StartRecordingPlayout() invalid compression");
2256 return (-1);
2257 }
2258 if (codecInst == NULL) {
2259 format = kFileFormatPcm16kHzFile;
2260 codecInst = &dummyCodec;
2261 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2262 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2263 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2264 format = kFileFormatWavFile;
2265 } else {
2266 format = kFileFormatCompressedFile;
2267 }
2268
2269 rtc::CritScope cs(&_fileCritSect);
2270
2271 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002272 if (output_file_recorder_) {
2273 output_file_recorder_->RegisterModuleFileCallback(NULL);
2274 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002275 }
2276
kwiberg5a25d952016-08-17 07:31:12 -07002277 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002278 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002279 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002280 _engineStatisticsPtr->SetLastError(
2281 VE_INVALID_ARGUMENT, kTraceError,
2282 "StartRecordingPlayout() fileRecorder format isnot correct");
2283 return -1;
2284 }
2285
kwiberg4ec01d92016-08-22 08:43:54 -07002286 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002287 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002288 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2289 "StartRecordingPlayout() failed to "
2290 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002291 output_file_recorder_->StopRecording();
2292 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002293 return -1;
2294 }
2295
kwiberg5a25d952016-08-17 07:31:12 -07002296 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002297 _outputFileRecording = true;
2298
2299 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002300}
2301
kwiberg55b97fe2016-01-28 05:22:45 -08002302int Channel::StopRecordingPlayout() {
2303 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2304 "Channel::StopRecordingPlayout()");
2305
2306 if (!_outputFileRecording) {
2307 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2308 "StopRecordingPlayout() isnot recording");
2309 return -1;
2310 }
2311
2312 rtc::CritScope cs(&_fileCritSect);
2313
kwiberg5a25d952016-08-17 07:31:12 -07002314 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002315 _engineStatisticsPtr->SetLastError(
2316 VE_STOP_RECORDING_FAILED, kTraceError,
2317 "StopRecording() could not stop recording");
2318 return (-1);
2319 }
kwiberg5a25d952016-08-17 07:31:12 -07002320 output_file_recorder_->RegisterModuleFileCallback(NULL);
2321 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002322 _outputFileRecording = false;
2323
2324 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002325}
2326
kwiberg55b97fe2016-01-28 05:22:45 -08002327void Channel::SetMixWithMicStatus(bool mix) {
2328 rtc::CritScope cs(&_fileCritSect);
2329 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002330}
2331
solenberg8d73f8c2017-03-08 01:52:20 -08002332int Channel::GetSpeechOutputLevel() const {
2333 return _outputAudioLevel.Level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002334}
2335
solenberg8d73f8c2017-03-08 01:52:20 -08002336int Channel::GetSpeechOutputLevelFullRange() const {
2337 return _outputAudioLevel.LevelFullRange();
kwiberg55b97fe2016-01-28 05:22:45 -08002338}
2339
zsteine76bd3a2017-07-14 12:17:49 -07002340double Channel::GetTotalOutputEnergy() const {
zstein3c451862017-07-20 09:57:42 -07002341 return _outputAudioLevel.TotalEnergy();
zsteine76bd3a2017-07-14 12:17:49 -07002342}
2343
2344double Channel::GetTotalOutputDuration() const {
zstein3c451862017-07-20 09:57:42 -07002345 return _outputAudioLevel.TotalDuration();
zsteine76bd3a2017-07-14 12:17:49 -07002346}
2347
solenberg8d73f8c2017-03-08 01:52:20 -08002348void Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002349 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002350 input_mute_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002351}
2352
solenberg1c2af8e2016-03-24 10:36:00 -07002353bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002354 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002355 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002356}
2357
solenberg8d73f8c2017-03-08 01:52:20 -08002358void Channel::SetChannelOutputVolumeScaling(float scaling) {
kwiberg55b97fe2016-01-28 05:22:45 -08002359 rtc::CritScope cs(&volume_settings_critsect_);
kwiberg55b97fe2016-01-28 05:22:45 -08002360 _outputGain = scaling;
niklase@google.com470e71d2011-07-07 08:21:25 +00002361}
2362
solenberg8842c3e2016-03-11 03:06:41 -08002363int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002364 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002365 "Channel::SendTelephoneEventOutband(...)");
2366 RTC_DCHECK_LE(0, event);
2367 RTC_DCHECK_GE(255, event);
2368 RTC_DCHECK_LE(0, duration_ms);
2369 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002370 if (!Sending()) {
2371 return -1;
2372 }
solenberg8842c3e2016-03-11 03:06:41 -08002373 if (_rtpRtcpModule->SendTelephoneEventOutband(
2374 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002375 _engineStatisticsPtr->SetLastError(
2376 VE_SEND_DTMF_FAILED, kTraceWarning,
2377 "SendTelephoneEventOutband() failed to send event");
2378 return -1;
2379 }
2380 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002381}
2382
solenbergffbbcac2016-11-17 05:25:37 -08002383int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2384 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002385 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002386 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002387 RTC_DCHECK_LE(0, payload_type);
2388 RTC_DCHECK_GE(127, payload_type);
2389 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002390 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002391 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002392 memcpy(codec.plname, "telephone-event", 16);
2393 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2394 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2395 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2396 _engineStatisticsPtr->SetLastError(
2397 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2398 "SetSendTelephoneEventPayloadType() failed to register send"
2399 "payload type");
2400 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002401 }
kwiberg55b97fe2016-01-28 05:22:45 -08002402 }
kwiberg55b97fe2016-01-28 05:22:45 -08002403 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002404}
2405
kwiberg55b97fe2016-01-28 05:22:45 -08002406int Channel::SetLocalSSRC(unsigned int ssrc) {
2407 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2408 "Channel::SetLocalSSRC()");
2409 if (channel_state_.Get().sending) {
2410 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2411 "SetLocalSSRC() already sending");
2412 return -1;
2413 }
2414 _rtpRtcpModule->SetSSRC(ssrc);
2415 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002416}
2417
kwiberg55b97fe2016-01-28 05:22:45 -08002418int Channel::GetLocalSSRC(unsigned int& ssrc) {
2419 ssrc = _rtpRtcpModule->SSRC();
2420 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002421}
2422
kwiberg55b97fe2016-01-28 05:22:45 -08002423int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2424 ssrc = rtp_receiver_->SSRC();
2425 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002426}
2427
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002428int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002429 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002430 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002431}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002432
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002433int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2434 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002435 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2436 if (enable &&
2437 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2438 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002439 return -1;
2440 }
2441 return 0;
2442}
2443
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002444void Channel::EnableSendTransportSequenceNumber(int id) {
2445 int ret =
2446 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2447 RTC_DCHECK_EQ(0, ret);
2448}
2449
stefan3313ec92016-01-21 06:32:43 -08002450void Channel::EnableReceiveTransportSequenceNumber(int id) {
2451 rtp_header_parser_->DeregisterRtpHeaderExtension(
2452 kRtpExtensionTransportSequenceNumber);
2453 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2454 kRtpExtensionTransportSequenceNumber, id);
2455 RTC_DCHECK(ret);
2456}
2457
stefanbba9dec2016-02-01 04:39:55 -08002458void Channel::RegisterSenderCongestionControlObjects(
nisseb8f9a322017-03-27 05:36:15 -07002459 RtpTransportControllerSendInterface* transport,
stefan7de8d642017-02-07 07:14:08 -08002460 RtcpBandwidthObserver* bandwidth_observer) {
nisseb8f9a322017-03-27 05:36:15 -07002461 RtpPacketSender* rtp_packet_sender = transport->packet_sender();
2462 TransportFeedbackObserver* transport_feedback_observer =
2463 transport->transport_feedback_observer();
2464 PacketRouter* packet_router = transport->packet_router();
2465
stefanbba9dec2016-02-01 04:39:55 -08002466 RTC_DCHECK(rtp_packet_sender);
2467 RTC_DCHECK(transport_feedback_observer);
kwibergee89e782017-08-09 17:22:01 -07002468 RTC_DCHECK(packet_router);
2469 RTC_DCHECK(!packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002470 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002471 feedback_observer_proxy_->SetTransportFeedbackObserver(
2472 transport_feedback_observer);
2473 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2474 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2475 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
eladalon822ff2b2017-08-01 06:30:28 -07002476 constexpr bool remb_candidate = false;
2477 packet_router->AddSendRtpModule(_rtpRtcpModule.get(), remb_candidate);
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002478 packet_router_ = packet_router;
2479}
2480
stefanbba9dec2016-02-01 04:39:55 -08002481void Channel::RegisterReceiverCongestionControlObjects(
2482 PacketRouter* packet_router) {
kwibergee89e782017-08-09 17:22:01 -07002483 RTC_DCHECK(packet_router);
2484 RTC_DCHECK(!packet_router_);
eladalon822ff2b2017-08-01 06:30:28 -07002485 constexpr bool remb_candidate = false;
2486 packet_router->AddReceiveRtpModule(_rtpRtcpModule.get(), remb_candidate);
stefanbba9dec2016-02-01 04:39:55 -08002487 packet_router_ = packet_router;
2488}
2489
nissefdbfdc92017-03-31 05:44:52 -07002490void Channel::ResetSenderCongestionControlObjects() {
stefanbba9dec2016-02-01 04:39:55 -08002491 RTC_DCHECK(packet_router_);
2492 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002493 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002494 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2495 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
nissefdbfdc92017-03-31 05:44:52 -07002496 packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002497 packet_router_ = nullptr;
2498 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2499}
2500
nissefdbfdc92017-03-31 05:44:52 -07002501void Channel::ResetReceiverCongestionControlObjects() {
2502 RTC_DCHECK(packet_router_);
2503 packet_router_->RemoveReceiveRtpModule(_rtpRtcpModule.get());
2504 packet_router_ = nullptr;
2505}
2506
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002507void Channel::SetRTCPStatus(bool enable) {
2508 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2509 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002510 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002511}
2512
kwiberg55b97fe2016-01-28 05:22:45 -08002513int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002514 RtcpMode method = _rtpRtcpModule->RTCP();
2515 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002516 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002517}
2518
kwiberg55b97fe2016-01-28 05:22:45 -08002519int Channel::SetRTCP_CNAME(const char cName[256]) {
2520 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2521 "Channel::SetRTCP_CNAME()");
2522 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2523 _engineStatisticsPtr->SetLastError(
2524 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2525 "SetRTCP_CNAME() failed to set RTCP CNAME");
2526 return -1;
2527 }
2528 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002529}
2530
kwiberg55b97fe2016-01-28 05:22:45 -08002531int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2532 if (cName == NULL) {
2533 _engineStatisticsPtr->SetLastError(
2534 VE_INVALID_ARGUMENT, kTraceError,
2535 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2536 return -1;
2537 }
2538 char cname[RTCP_CNAME_SIZE];
2539 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2540 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2541 _engineStatisticsPtr->SetLastError(
2542 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2543 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2544 return -1;
2545 }
2546 strcpy(cName, cname);
2547 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002548}
2549
kwiberg55b97fe2016-01-28 05:22:45 -08002550int Channel::SendApplicationDefinedRTCPPacket(
2551 unsigned char subType,
2552 unsigned int name,
2553 const char* data,
2554 unsigned short dataLengthInBytes) {
2555 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2556 "Channel::SendApplicationDefinedRTCPPacket()");
2557 if (!channel_state_.Get().sending) {
2558 _engineStatisticsPtr->SetLastError(
2559 VE_NOT_SENDING, kTraceError,
2560 "SendApplicationDefinedRTCPPacket() not sending");
2561 return -1;
2562 }
2563 if (NULL == data) {
2564 _engineStatisticsPtr->SetLastError(
2565 VE_INVALID_ARGUMENT, kTraceError,
2566 "SendApplicationDefinedRTCPPacket() invalid data value");
2567 return -1;
2568 }
2569 if (dataLengthInBytes % 4 != 0) {
2570 _engineStatisticsPtr->SetLastError(
2571 VE_INVALID_ARGUMENT, kTraceError,
2572 "SendApplicationDefinedRTCPPacket() invalid length value");
2573 return -1;
2574 }
2575 RtcpMode status = _rtpRtcpModule->RTCP();
2576 if (status == RtcpMode::kOff) {
2577 _engineStatisticsPtr->SetLastError(
2578 VE_RTCP_ERROR, kTraceError,
2579 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2580 return -1;
2581 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002582
kwiberg55b97fe2016-01-28 05:22:45 -08002583 // Create and schedule the RTCP APP packet for transmission
2584 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2585 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2586 _engineStatisticsPtr->SetLastError(
2587 VE_SEND_ERROR, kTraceError,
2588 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2589 return -1;
2590 }
2591 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002592}
2593
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002594int Channel::GetRemoteRTCPReportBlocks(
2595 std::vector<ReportBlock>* report_blocks) {
2596 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002597 _engineStatisticsPtr->SetLastError(
2598 VE_INVALID_ARGUMENT, kTraceError,
2599 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002600 return -1;
2601 }
2602
2603 // Get the report blocks from the latest received RTCP Sender or Receiver
2604 // Report. Each element in the vector contains the sender's SSRC and a
2605 // report block according to RFC 3550.
2606 std::vector<RTCPReportBlock> rtcp_report_blocks;
2607 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002608 return -1;
2609 }
2610
2611 if (rtcp_report_blocks.empty())
2612 return 0;
2613
2614 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2615 for (; it != rtcp_report_blocks.end(); ++it) {
2616 ReportBlock report_block;
srte3e69e5c2017-08-09 06:13:45 -07002617 report_block.sender_SSRC = it->sender_ssrc;
2618 report_block.source_SSRC = it->source_ssrc;
2619 report_block.fraction_lost = it->fraction_lost;
2620 report_block.cumulative_num_packets_lost = it->packets_lost;
2621 report_block.extended_highest_sequence_number =
2622 it->extended_highest_sequence_number;
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002623 report_block.interarrival_jitter = it->jitter;
srte3e69e5c2017-08-09 06:13:45 -07002624 report_block.last_SR_timestamp = it->last_sender_report_timestamp;
2625 report_block.delay_since_last_SR = it->delay_since_last_sender_report;
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002626 report_blocks->push_back(report_block);
2627 }
2628 return 0;
2629}
2630
kwiberg55b97fe2016-01-28 05:22:45 -08002631int Channel::GetRTPStatistics(CallStatistics& stats) {
2632 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002633
kwiberg55b97fe2016-01-28 05:22:45 -08002634 // The jitter statistics is updated for each received RTP packet and is
2635 // based on received packets.
2636 RtcpStatistics statistics;
2637 StreamStatistician* statistician =
2638 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002639 if (statistician) {
2640 statistician->GetStatistics(&statistics,
2641 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002642 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002643
kwiberg55b97fe2016-01-28 05:22:45 -08002644 stats.fractionLost = statistics.fraction_lost;
srte186d9c32017-08-04 05:03:53 -07002645 stats.cumulativeLost = statistics.packets_lost;
2646 stats.extendedMax = statistics.extended_highest_sequence_number;
kwiberg55b97fe2016-01-28 05:22:45 -08002647 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002648
kwiberg55b97fe2016-01-28 05:22:45 -08002649 // --- RTT
2650 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002651
kwiberg55b97fe2016-01-28 05:22:45 -08002652 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002653
kwiberg55b97fe2016-01-28 05:22:45 -08002654 size_t bytesSent(0);
2655 uint32_t packetsSent(0);
2656 size_t bytesReceived(0);
2657 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002658
kwiberg55b97fe2016-01-28 05:22:45 -08002659 if (statistician) {
2660 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2661 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002662
kwiberg55b97fe2016-01-28 05:22:45 -08002663 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2664 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2665 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2666 " output will not be complete");
2667 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002668
kwiberg55b97fe2016-01-28 05:22:45 -08002669 stats.bytesSent = bytesSent;
2670 stats.packetsSent = packetsSent;
2671 stats.bytesReceived = bytesReceived;
2672 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002673
kwiberg55b97fe2016-01-28 05:22:45 -08002674 // --- Timestamps
2675 {
2676 rtc::CritScope lock(&ts_stats_lock_);
2677 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2678 }
2679 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002680}
2681
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002682int Channel::SetCodecFECStatus(bool enable) {
2683 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2684 "Channel::SetCodecFECStatus()");
2685
kwibergc8d071e2016-04-06 12:22:38 -07002686 if (!codec_manager_.SetCodecFEC(enable) ||
2687 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002688 _engineStatisticsPtr->SetLastError(
2689 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2690 "SetCodecFECStatus() failed to set FEC state");
2691 return -1;
2692 }
2693 return 0;
2694}
2695
2696bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002697 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002698}
2699
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002700void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2701 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002702 // If pacing is enabled we always store packets.
2703 if (!pacing_enabled_)
2704 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002705 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002706 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002707 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002708 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002709 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002710}
2711
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002712// Called when we are missing one or more packets.
2713int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002714 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2715}
2716
henrikaec6fbd22017-03-31 05:43:36 -07002717void Channel::ProcessAndEncodeAudio(const AudioFrame& audio_input) {
henrika4515fa02017-05-03 08:30:15 -07002718 // Avoid posting any new tasks if sending was already stopped in StopSend().
2719 rtc::CritScope cs(&encoder_queue_lock_);
2720 if (!encoder_queue_is_active_) {
2721 return;
2722 }
henrikaec6fbd22017-03-31 05:43:36 -07002723 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
2724 // TODO(henrika): try to avoid copying by moving ownership of audio frame
2725 // either into pool of frames or into the task itself.
2726 audio_frame->CopyFrom(audio_input);
2727 audio_frame->id_ = ChannelId();
2728 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
2729 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
niklase@google.com470e71d2011-07-07 08:21:25 +00002730}
2731
henrikaec6fbd22017-03-31 05:43:36 -07002732void Channel::ProcessAndEncodeAudio(const int16_t* audio_data,
2733 int sample_rate,
2734 size_t number_of_frames,
2735 size_t number_of_channels) {
henrika4515fa02017-05-03 08:30:15 -07002736 // Avoid posting as new task if sending was already stopped in StopSend().
2737 rtc::CritScope cs(&encoder_queue_lock_);
2738 if (!encoder_queue_is_active_) {
2739 return;
2740 }
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002741 CodecInst codec;
ossu950c1c92017-07-11 08:19:31 -07002742 const int result = GetSendCodec(codec);
henrikaec6fbd22017-03-31 05:43:36 -07002743 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
2744 audio_frame->id_ = ChannelId();
ossu950c1c92017-07-11 08:19:31 -07002745 // TODO(ossu): Investigate how this could happen. b/62909493
2746 if (result == 0) {
2747 audio_frame->sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2748 audio_frame->num_channels_ = std::min(number_of_channels, codec.channels);
2749 } else {
2750 audio_frame->sample_rate_hz_ = sample_rate;
2751 audio_frame->num_channels_ = number_of_channels;
2752 LOG(LS_WARNING) << "Unable to get send codec for channel " << ChannelId();
2753 RTC_NOTREACHED();
2754 }
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002755 RemixAndResample(audio_data, number_of_frames, number_of_channels,
henrikaec6fbd22017-03-31 05:43:36 -07002756 sample_rate, &input_resampler_, audio_frame.get());
2757 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
2758 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002759}
2760
henrikaec6fbd22017-03-31 05:43:36 -07002761void Channel::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
2762 RTC_DCHECK_RUN_ON(encoder_queue_);
2763 RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
2764 RTC_DCHECK_LE(audio_input->num_channels_, 2);
2765 RTC_DCHECK_EQ(audio_input->id_, ChannelId());
kwiberg55b97fe2016-01-28 05:22:45 -08002766
2767 if (channel_state_.Get().input_file_playing) {
henrikaec6fbd22017-03-31 05:43:36 -07002768 MixOrReplaceAudioWithFile(audio_input);
kwiberg55b97fe2016-01-28 05:22:45 -08002769 }
2770
henrikaec6fbd22017-03-31 05:43:36 -07002771 bool is_muted = InputMute();
2772 AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002773
kwiberg55b97fe2016-01-28 05:22:45 -08002774 if (_includeAudioLevelIndication) {
2775 size_t length =
henrikaec6fbd22017-03-31 05:43:36 -07002776 audio_input->samples_per_channel_ * audio_input->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002777 RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes);
solenberg1c2af8e2016-03-24 10:36:00 -07002778 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002779 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002780 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002781 rms_level_.Analyze(
yujo36b1a5f2017-06-12 12:45:32 -07002782 rtc::ArrayView<const int16_t>(audio_input->data(), length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002783 }
kwiberg55b97fe2016-01-28 05:22:45 -08002784 }
solenberg1c2af8e2016-03-24 10:36:00 -07002785 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002786
henrikaec6fbd22017-03-31 05:43:36 -07002787 // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002788
kwiberg55b97fe2016-01-28 05:22:45 -08002789 // The ACM resamples internally.
henrikaec6fbd22017-03-31 05:43:36 -07002790 audio_input->timestamp_ = _timeStamp;
kwiberg55b97fe2016-01-28 05:22:45 -08002791 // This call will trigger AudioPacketizationCallback::SendData if encoding
2792 // is done and payload is ready for packetization and transmission.
2793 // Otherwise, it will return without invoking the callback.
henrikaec6fbd22017-03-31 05:43:36 -07002794 if (audio_coding_->Add10MsData(*audio_input) < 0) {
2795 LOG(LS_ERROR) << "ACM::Add10MsData() failed for channel " << _channelId;
2796 return;
kwiberg55b97fe2016-01-28 05:22:45 -08002797 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002798
henrikaec6fbd22017-03-31 05:43:36 -07002799 _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002800}
2801
solenberg7602aab2016-11-14 11:30:07 -08002802void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2803 RTC_DCHECK(!channel.channel() ||
2804 channel.channel()->ChannelId() != _channelId);
2805 rtc::CritScope lock(&assoc_send_channel_lock_);
2806 associate_send_channel_ = channel;
2807}
2808
Minyue2013aec2015-05-13 14:14:42 +02002809void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002810 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002811 Channel* channel = associate_send_channel_.channel();
2812 if (channel && channel->ChannelId() == channel_id) {
2813 // If this channel is associated with a send channel of the specified
2814 // Channel ID, disassociate with it.
2815 ChannelOwner ref(NULL);
2816 associate_send_channel_ = ref;
2817 }
2818}
2819
ivoc14d5dbe2016-07-04 07:06:55 -07002820void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2821 event_log_proxy_->SetEventLog(event_log);
2822}
2823
michaelt9332b7d2016-11-30 07:51:13 -08002824void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2825 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2826}
2827
nisse284542b2017-01-10 08:58:32 -08002828void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08002829 size_t overhead_per_packet =
2830 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08002831 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2832 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08002833 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08002834 }
2835 });
2836}
2837
2838void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002839 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002840 transport_overhead_per_packet_ = transport_overhead_per_packet;
2841 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002842}
2843
hbos3fd31fe2017-02-28 05:43:16 -08002844// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08002845void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002846 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002847 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2848 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002849}
2850
kwiberg55b97fe2016-01-28 05:22:45 -08002851int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2852 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002853}
2854
wu@webrtc.org24301a62013-12-13 19:17:43 +00002855void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2856 audio_coding_->GetDecodingCallStatistics(stats);
2857}
2858
ivoce1198e02017-09-08 08:13:19 -07002859ANAStats Channel::GetANAStatistics() const {
2860 return audio_coding_->GetANAStats();
2861}
2862
solenberg358057b2015-11-27 10:46:42 -08002863uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002864 rtc::CritScope lock(&video_sync_lock_);
2865 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002866}
2867
kwiberg55b97fe2016-01-28 05:22:45 -08002868int Channel::SetMinimumPlayoutDelay(int delayMs) {
2869 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2870 "Channel::SetMinimumPlayoutDelay()");
2871 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2872 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2873 _engineStatisticsPtr->SetLastError(
2874 VE_INVALID_ARGUMENT, kTraceError,
2875 "SetMinimumPlayoutDelay() invalid min delay");
2876 return -1;
2877 }
2878 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2879 _engineStatisticsPtr->SetLastError(
2880 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2881 "SetMinimumPlayoutDelay() failed to set min playout delay");
2882 return -1;
2883 }
2884 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002885}
2886
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002887int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002888 uint32_t playout_timestamp_rtp = 0;
2889 {
tommi31fc21f2016-01-21 10:37:37 -08002890 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002891 playout_timestamp_rtp = playout_timestamp_rtp_;
2892 }
kwiberg55b97fe2016-01-28 05:22:45 -08002893 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002894 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002895 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002896 "GetPlayoutTimestamp() failed to retrieve timestamp");
2897 return -1;
2898 }
deadbeef74375882015-08-13 12:09:10 -07002899 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002900 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002901}
2902
kwiberg55b97fe2016-01-28 05:22:45 -08002903int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2904 RtpReceiver** rtp_receiver) const {
2905 *rtpRtcpModule = _rtpRtcpModule.get();
2906 *rtp_receiver = rtp_receiver_.get();
2907 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002908}
2909
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002910// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2911// a shared helper.
henrikaec6fbd22017-03-31 05:43:36 -07002912int32_t Channel::MixOrReplaceAudioWithFile(AudioFrame* audio_input) {
2913 RTC_DCHECK_RUN_ON(encoder_queue_);
kwibergb7f89d62016-02-17 10:04:18 -08002914 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002915 size_t fileSamples(0);
henrikaec6fbd22017-03-31 05:43:36 -07002916 const int mixingFrequency = audio_input->sample_rate_hz_;
kwiberg55b97fe2016-01-28 05:22:45 -08002917 {
2918 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002919
kwiberg5a25d952016-08-17 07:31:12 -07002920 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002921 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2922 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2923 " doesnt exist");
2924 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002925 }
2926
kwiberg4ec01d92016-08-22 08:43:54 -07002927 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002928 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002929 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2930 "Channel::MixOrReplaceAudioWithFile() file mixing "
2931 "failed");
2932 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002933 }
kwiberg55b97fe2016-01-28 05:22:45 -08002934 if (fileSamples == 0) {
2935 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2936 "Channel::MixOrReplaceAudioWithFile() file is ended");
2937 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002938 }
kwiberg55b97fe2016-01-28 05:22:45 -08002939 }
2940
henrikaec6fbd22017-03-31 05:43:36 -07002941 RTC_DCHECK_EQ(audio_input->samples_per_channel_, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08002942
2943 if (_mixFileWithMicrophone) {
2944 // Currently file stream is always mono.
2945 // TODO(xians): Change the code when FilePlayer supports real stereo.
yujo36b1a5f2017-06-12 12:45:32 -07002946 MixWithSat(audio_input->mutable_data(), audio_input->num_channels_,
2947 fileBuffer.get(), 1, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08002948 } else {
2949 // Replace ACM audio with file.
2950 // Currently file stream is always mono.
2951 // TODO(xians): Change the code when FilePlayer supports real stereo.
henrikaec6fbd22017-03-31 05:43:36 -07002952 audio_input->UpdateFrame(
kwiberg55b97fe2016-01-28 05:22:45 -08002953 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2954 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2955 }
2956 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002957}
2958
kwiberg55b97fe2016-01-28 05:22:45 -08002959int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2960 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002961
kwibergb7f89d62016-02-17 10:04:18 -08002962 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002963 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002964
kwiberg55b97fe2016-01-28 05:22:45 -08002965 {
2966 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002967
kwiberg5a25d952016-08-17 07:31:12 -07002968 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002969 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2970 "Channel::MixAudioWithFile() file mixing failed");
2971 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002972 }
2973
kwiberg55b97fe2016-01-28 05:22:45 -08002974 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002975 if (output_file_player_->Get10msAudioFromFile(
2976 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002977 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2978 "Channel::MixAudioWithFile() file mixing failed");
2979 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002980 }
kwiberg55b97fe2016-01-28 05:22:45 -08002981 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002982
kwiberg55b97fe2016-01-28 05:22:45 -08002983 if (audioFrame.samples_per_channel_ == fileSamples) {
2984 // Currently file stream is always mono.
2985 // TODO(xians): Change the code when FilePlayer supports real stereo.
yujo36b1a5f2017-06-12 12:45:32 -07002986 MixWithSat(audioFrame.mutable_data(), audioFrame.num_channels_,
2987 fileBuffer.get(), 1, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08002988 } else {
2989 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2990 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2991 ") != "
2992 "fileSamples(%" PRIuS ")",
2993 audioFrame.samples_per_channel_, fileSamples);
2994 return -1;
2995 }
2996
2997 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002998}
2999
deadbeef74375882015-08-13 12:09:10 -07003000void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003001 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003002
henrik.lundin96bd5022016-04-06 04:13:56 -07003003 if (!jitter_buffer_playout_timestamp_) {
3004 // This can happen if this channel has not received any RTP packets. In
3005 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003006 return;
3007 }
3008
3009 uint16_t delay_ms = 0;
3010 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003011 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003012 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3013 " delay from the ADM");
3014 _engineStatisticsPtr->SetLastError(
3015 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3016 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3017 return;
3018 }
3019
henrik.lundin96bd5022016-04-06 04:13:56 -07003020 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3021 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003022
3023 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003024 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003025
kwiberg55b97fe2016-01-28 05:22:45 -08003026 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003027 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003028 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003029
3030 {
tommi31fc21f2016-01-21 10:37:37 -08003031 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08003032 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003033 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003034 }
3035 playout_delay_ms_ = delay_ms;
3036 }
3037}
3038
kwiberg55b97fe2016-01-28 05:22:45 -08003039void Channel::RegisterReceiveCodecsToRTPModule() {
3040 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3041 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003042
kwiberg55b97fe2016-01-28 05:22:45 -08003043 CodecInst codec;
3044 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003045
kwiberg55b97fe2016-01-28 05:22:45 -08003046 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3047 // Open up the RTP/RTCP receiver for all supported codecs
3048 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08003049 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08003050 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3051 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3052 " to register %s (%d/%d/%" PRIuS
3053 "/%d) to RTP/RTCP "
3054 "receiver",
3055 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3056 codec.rate);
3057 } else {
3058 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3059 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3060 "(%d/%d/%" PRIuS
3061 "/%d) has been added to the RTP/RTCP "
3062 "receiver",
3063 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3064 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003065 }
kwiberg55b97fe2016-01-28 05:22:45 -08003066 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003067}
3068
kwiberg55b97fe2016-01-28 05:22:45 -08003069int Channel::SetSendRtpHeaderExtension(bool enable,
3070 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003071 unsigned char id) {
3072 int error = 0;
3073 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3074 if (enable) {
3075 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3076 }
3077 return error;
3078}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003079
ossue280cde2016-10-12 11:04:10 -07003080int Channel::GetRtpTimestampRateHz() const {
3081 const auto format = audio_coding_->ReceiveFormat();
3082 // Default to the playout frequency if we've not gotten any packets yet.
3083 // TODO(ossu): Zero clockrate can only happen if we've added an external
3084 // decoder for a format we don't support internally. Remove once that way of
3085 // adding decoders is gone!
3086 return (format && format->clockrate_hz != 0)
3087 ? format->clockrate_hz
3088 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003089}
3090
Minyue2013aec2015-05-13 14:14:42 +02003091int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003092 RtcpMode method = _rtpRtcpModule->RTCP();
3093 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003094 return 0;
3095 }
3096 std::vector<RTCPReportBlock> report_blocks;
3097 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003098
3099 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003100 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003101 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003102 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003103 Channel* channel = associate_send_channel_.channel();
3104 // Tries to get RTT from an associated channel. This is important for
3105 // receive-only channels.
3106 if (channel) {
3107 // To prevent infinite recursion and deadlock, calling GetRTT of
3108 // associate channel should always use "false" for argument:
3109 // |allow_associate_channel|.
3110 rtt = channel->GetRTT(false);
3111 }
3112 }
3113 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003114 }
3115
3116 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3117 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3118 for (; it != report_blocks.end(); ++it) {
srte3e69e5c2017-08-09 06:13:45 -07003119 if (it->sender_ssrc == remoteSSRC)
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003120 break;
3121 }
3122 if (it == report_blocks.end()) {
3123 // We have not received packets with SSRC matching the report blocks.
3124 // To calculate RTT we try with the SSRC of the first report block.
3125 // This is very important for send-only channels where we don't know
3126 // the SSRC of the other end.
srte3e69e5c2017-08-09 06:13:45 -07003127 remoteSSRC = report_blocks[0].sender_ssrc;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003128 }
Minyue2013aec2015-05-13 14:14:42 +02003129
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003130 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003131 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003132 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003133 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3134 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003135 return 0;
3136 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003137 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003138}
3139
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003140} // namespace voe
3141} // namespace webrtc