blob: b0bae4e4ceb0e05cd1ca421efdbc5943cf640e19 [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
aleloi6321b492016-12-05 01:46:09 -080016#include "webrtc/audio/utility/audio_frame_operations.h"
henrik.lundin50499422016-11-29 04:26:24 -080017#include "webrtc/base/array_view.h"
Ivo Creusenae856f22015-09-17 16:30:16 +020018#include "webrtc/base/checks.h"
tommi31fc21f2016-01-21 10:37:37 -080019#include "webrtc/base/criticalsection.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000020#include "webrtc/base/format_macros.h"
tommidea489f2017-03-03 03:20:24 -080021#include "webrtc/base/location.h"
pbosad856222015-11-27 09:48:36 -080022#include "webrtc/base/logging.h"
Erik Språng737336d2016-07-29 12:59:36 +020023#include "webrtc/base/rate_limiter.h"
henrikaec6fbd22017-03-31 05:43:36 -070024#include "webrtc/base/task_queue.h"
25#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000026#include "webrtc/base/timeutils.h"
nissecae45d02017-04-24 05:53:20 -070027#include "webrtc/call/rtp_transport_controller_send_interface.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020028#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070029#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070030#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000031#include "webrtc/modules/audio_device/include/audio_device.h"
32#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010033#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010034#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010035#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
36#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
37#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
nisse657bab22017-02-21 06:28:10 -080038#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000039#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010040#include "webrtc/modules/utility/include/process_thread.h"
elad.alon28770482017-03-28 05:03:55 -070041#include "webrtc/system_wrappers/include/field_trial.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010042#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000043#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
44#include "webrtc/voice_engine/output_mixer.h"
45#include "webrtc/voice_engine/statistics.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000046#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000047
andrew@webrtc.org50419b02012-11-14 19:07:54 +000048namespace webrtc {
49namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000050
kwibergc8d071e2016-04-06 12:22:38 -070051namespace {
52
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_;
195 RtcEventLog* event_log_ GUARDED_BY(crit_);
196 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_;
223 RtcpRttStats* rtcp_rtt_stats_ GUARDED_BY(crit_);
224 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_;
268 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
269};
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_;
297 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
298};
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_;
327 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
328};
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 =
375 extended_max_sequence_number_.find(block_it->sourceSSRC);
376 int number_of_packets = 0;
377 if (seq_num_it != extended_max_sequence_number_.end()) {
378 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
379 }
380 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
381 total_number_of_packets += number_of_packets;
382
383 extended_max_sequence_number_[block_it->sourceSSRC] =
384 block_it->extendedHighSeqNum;
385 }
386 int weighted_fraction_lost = 0;
387 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800388 weighted_fraction_lost =
389 (fraction_lost_aggregate + total_number_of_packets / 2) /
390 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000391 }
elad.alond12a8e12017-03-23 11:04:48 -0700392 owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000393 }
394
395 private:
396 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000397 // Maps remote side ssrc to extended highest sequence number received.
398 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800399 rtc::CriticalSection crit_;
400 RtcpBandwidthObserver* bandwidth_observer_ GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000401};
402
henrikaec6fbd22017-03-31 05:43:36 -0700403class Channel::ProcessAndEncodeAudioTask : public rtc::QueuedTask {
404 public:
405 ProcessAndEncodeAudioTask(std::unique_ptr<AudioFrame> audio_frame,
406 Channel* channel)
407 : audio_frame_(std::move(audio_frame)), channel_(channel) {
408 RTC_DCHECK(channel_);
409 }
410
411 private:
412 bool Run() override {
413 RTC_DCHECK_RUN_ON(channel_->encoder_queue_);
414 channel_->ProcessAndEncodeAudioOnTaskQueue(audio_frame_.get());
415 return true;
416 }
417
418 std::unique_ptr<AudioFrame> audio_frame_;
419 Channel* const channel_;
420};
421
kwiberg55b97fe2016-01-28 05:22:45 -0800422int32_t Channel::SendData(FrameType frameType,
423 uint8_t payloadType,
424 uint32_t timeStamp,
425 const uint8_t* payloadData,
426 size_t payloadSize,
427 const RTPFragmentationHeader* fragmentation) {
henrikaec6fbd22017-03-31 05:43:36 -0700428 RTC_DCHECK_RUN_ON(encoder_queue_);
kwiberg55b97fe2016-01-28 05:22:45 -0800429 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
430 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
431 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
432 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000433
kwiberg55b97fe2016-01-28 05:22:45 -0800434 if (_includeAudioLevelIndication) {
435 // Store current audio level in the RTP/RTCP module.
436 // The level will be used in combination with voice-activity state
437 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800438 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800439 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000440
kwiberg55b97fe2016-01-28 05:22:45 -0800441 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
442 // packetization.
443 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700444 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800445 (FrameType&)frameType, payloadType, timeStamp,
446 // Leaving the time when this frame was
447 // received from the capture device as
448 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700449 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800450 _engineStatisticsPtr->SetLastError(
451 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
452 "Channel::SendData() failed to send data to RTP/RTCP module");
453 return -1;
454 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000455
kwiberg55b97fe2016-01-28 05:22:45 -0800456 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000457}
458
stefan1d8a5062015-10-02 03:39:33 -0700459bool Channel::SendRtp(const uint8_t* data,
460 size_t len,
461 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800462 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
463 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000464
kwiberg55b97fe2016-01-28 05:22:45 -0800465 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000466
kwiberg55b97fe2016-01-28 05:22:45 -0800467 if (_transportPtr == NULL) {
468 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
469 "Channel::SendPacket() failed to send RTP packet due to"
470 " invalid transport object");
471 return false;
472 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000473
kwiberg55b97fe2016-01-28 05:22:45 -0800474 uint8_t* bufferToSendPtr = (uint8_t*)data;
475 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000476
kwiberg55b97fe2016-01-28 05:22:45 -0800477 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
478 std::string transport_name =
479 _externalTransport ? "external transport" : "WebRtc sockets";
480 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
481 "Channel::SendPacket() RTP transmission using %s failed",
482 transport_name.c_str());
483 return false;
484 }
485 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000486}
487
kwiberg55b97fe2016-01-28 05:22:45 -0800488bool Channel::SendRtcp(const uint8_t* data, size_t len) {
489 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
490 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000491
kwiberg55b97fe2016-01-28 05:22:45 -0800492 rtc::CritScope cs(&_callbackCritSect);
493 if (_transportPtr == NULL) {
494 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
495 "Channel::SendRtcp() failed to send RTCP packet"
496 " due to invalid transport object");
497 return false;
498 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000499
kwiberg55b97fe2016-01-28 05:22:45 -0800500 uint8_t* bufferToSendPtr = (uint8_t*)data;
501 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000502
kwiberg55b97fe2016-01-28 05:22:45 -0800503 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
504 if (n < 0) {
505 std::string transport_name =
506 _externalTransport ? "external transport" : "WebRtc sockets";
507 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
508 "Channel::SendRtcp() transmission using %s failed",
509 transport_name.c_str());
510 return false;
511 }
512 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000513}
514
kwiberg55b97fe2016-01-28 05:22:45 -0800515void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
516 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
517 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000518
kwiberg55b97fe2016-01-28 05:22:45 -0800519 // Update ssrc so that NTP for AV sync can be updated.
520 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000521}
522
Peter Boströmac547a62015-09-17 23:03:57 +0200523void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
524 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
525 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
526 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000527}
528
Peter Boströmac547a62015-09-17 23:03:57 +0200529int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000530 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000531 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000532 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800533 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200534 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800535 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
536 "Channel::OnInitializeDecoder(payloadType=%d, "
537 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
538 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000539
kwiberg55b97fe2016-01-28 05:22:45 -0800540 CodecInst receiveCodec = {0};
541 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000542
kwiberg55b97fe2016-01-28 05:22:45 -0800543 receiveCodec.pltype = payloadType;
544 receiveCodec.plfreq = frequency;
545 receiveCodec.channels = channels;
546 receiveCodec.rate = rate;
547 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000548
kwiberg55b97fe2016-01-28 05:22:45 -0800549 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
550 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000551
kwiberg55b97fe2016-01-28 05:22:45 -0800552 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700553 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
554 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800555 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
556 "Channel::OnInitializeDecoder() invalid codec ("
557 "pt=%d, name=%s) received - 1",
558 payloadType, payloadName);
559 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
560 return -1;
561 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000562
kwiberg55b97fe2016-01-28 05:22:45 -0800563 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000564}
565
kwiberg55b97fe2016-01-28 05:22:45 -0800566int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
567 size_t payloadSize,
568 const WebRtcRTPHeader* rtpHeader) {
569 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
570 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
571 ","
572 " payloadType=%u, audioChannel=%" PRIuS ")",
573 payloadSize, rtpHeader->header.payloadType,
574 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000575
kwiberg55b97fe2016-01-28 05:22:45 -0800576 if (!channel_state_.Get().playing) {
577 // Avoid inserting into NetEQ when we are not playing. Count the
578 // packet as discarded.
579 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
580 "received packet is discarded since playing is not"
581 " activated");
niklase@google.com470e71d2011-07-07 08:21:25 +0000582 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800583 }
584
585 // Push the incoming payload (parsed and ready for decoding) into the ACM
586 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
587 0) {
588 _engineStatisticsPtr->SetLastError(
589 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
590 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
591 return -1;
592 }
593
kwiberg55b97fe2016-01-28 05:22:45 -0800594 int64_t round_trip_time = 0;
595 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
596 NULL);
597
598 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
599 if (!nack_list.empty()) {
600 // Can't use nack_list.data() since it's not supported by all
601 // compilers.
602 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
603 }
604 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000605}
606
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000607bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000608 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000609 RTPHeader header;
610 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
611 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
612 "IncomingPacket invalid RTP header");
613 return false;
614 }
615 header.payload_type_frequency =
616 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
617 if (header.payload_type_frequency < 0)
618 return false;
619 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
620}
621
henrik.lundin42dda502016-05-18 05:36:01 -0700622MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
623 int32_t id,
624 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700625 unsigned int ssrc;
nisse7d59f6b2017-02-21 03:40:24 -0800626 RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
ivoc14d5dbe2016-07-04 07:06:55 -0700627 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800628 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700629 bool muted;
630 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
631 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800632 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
633 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
634 // In all likelihood, the audio in this frame is garbage. We return an
635 // error so that the audio mixer module doesn't add it to the mix. As
636 // a result, it won't be played out and the actions skipped here are
637 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700638 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800639 }
henrik.lundina89ab962016-05-18 08:52:45 -0700640
641 if (muted) {
642 // TODO(henrik.lundin): We should be able to do better than this. But we
643 // will have to go through all the cases below where the audio samples may
644 // be used, and handle the muted case in some way.
aleloi6321b492016-12-05 01:46:09 -0800645 AudioFrameOperations::Mute(audioFrame);
henrik.lundina89ab962016-05-18 08:52:45 -0700646 }
kwiberg55b97fe2016-01-28 05:22:45 -0800647
kwiberg55b97fe2016-01-28 05:22:45 -0800648 // Convert module ID to internal VoE channel ID
649 audioFrame->id_ = VoEChannelId(audioFrame->id_);
650 // Store speech type for dead-or-alive detection
651 _outputSpeechType = audioFrame->speech_type_;
652
653 ChannelState::State state = channel_state_.Get();
654
kwiberg55b97fe2016-01-28 05:22:45 -0800655 {
656 // Pass the audio buffers to an optional sink callback, before applying
657 // scaling/panning, as that applies to the mix operation.
658 // External recipients of the audio (e.g. via AudioTrack), will do their
659 // own mixing/dynamic processing.
660 rtc::CritScope cs(&_callbackCritSect);
661 if (audio_sink_) {
662 AudioSinkInterface::Data data(
663 &audioFrame->data_[0], audioFrame->samples_per_channel_,
664 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
665 audioFrame->timestamp_);
666 audio_sink_->OnData(data);
667 }
668 }
669
670 float output_gain = 1.0f;
kwiberg55b97fe2016-01-28 05:22:45 -0800671 {
672 rtc::CritScope cs(&volume_settings_critsect_);
673 output_gain = _outputGain;
kwiberg55b97fe2016-01-28 05:22:45 -0800674 }
675
676 // Output volume scaling
677 if (output_gain < 0.99f || output_gain > 1.01f) {
solenberg8d73f8c2017-03-08 01:52:20 -0800678 // TODO(solenberg): Combine with mute state - this can cause clicks!
oprypin67fdb802017-03-09 06:25:06 -0800679 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800680 }
681
kwiberg55b97fe2016-01-28 05:22:45 -0800682 // Mix decoded PCM output with file if file mixing is enabled
683 if (state.output_file_playing) {
684 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700685 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800686 }
687
kwiberg55b97fe2016-01-28 05:22:45 -0800688 // Record playout if enabled
689 {
690 rtc::CritScope cs(&_fileCritSect);
691
kwiberg5a25d952016-08-17 07:31:12 -0700692 if (_outputFileRecording && output_file_recorder_) {
693 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800694 }
695 }
696
697 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700698 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800699 _outputAudioLevel.ComputeLevel(*audioFrame);
700
701 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
702 // The first frame with a valid rtp timestamp.
703 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
704 }
705
706 if (capture_start_rtp_time_stamp_ >= 0) {
707 // audioFrame.timestamp_ should be valid from now on.
708
709 // Compute elapsed time.
710 int64_t unwrap_timestamp =
711 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
712 audioFrame->elapsed_time_ms_ =
713 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700714 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800715
niklase@google.com470e71d2011-07-07 08:21:25 +0000716 {
kwiberg55b97fe2016-01-28 05:22:45 -0800717 rtc::CritScope lock(&ts_stats_lock_);
718 // Compute ntp time.
719 audioFrame->ntp_time_ms_ =
720 ntp_estimator_.Estimate(audioFrame->timestamp_);
721 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
722 if (audioFrame->ntp_time_ms_ > 0) {
723 // Compute |capture_start_ntp_time_ms_| so that
724 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
725 capture_start_ntp_time_ms_ =
726 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000727 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000728 }
kwiberg55b97fe2016-01-28 05:22:45 -0800729 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000730
henrik.lundin42dda502016-05-18 05:36:01 -0700731 return muted ? MixerParticipant::AudioFrameInfo::kMuted
732 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000733}
734
aleloi6c278492016-10-20 14:24:39 -0700735AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
736 int sample_rate_hz,
737 AudioFrame* audio_frame) {
738 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700739
aleloi6c278492016-10-20 14:24:39 -0700740 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700741
742 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
743 FrameInfo new_audio_frame_info = FrameInfo::kError;
744 switch (frame_info) {
745 case MixerParticipant::AudioFrameInfo::kNormal:
746 new_audio_frame_info = FrameInfo::kNormal;
747 break;
748 case MixerParticipant::AudioFrameInfo::kMuted:
749 new_audio_frame_info = FrameInfo::kMuted;
750 break;
751 case MixerParticipant::AudioFrameInfo::kError:
752 new_audio_frame_info = FrameInfo::kError;
753 break;
754 }
aleloi6c278492016-10-20 14:24:39 -0700755 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700756}
757
kwiberg55b97fe2016-01-28 05:22:45 -0800758int32_t Channel::NeededFrequency(int32_t id) const {
759 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
760 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000761
kwiberg55b97fe2016-01-28 05:22:45 -0800762 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000763
kwiberg55b97fe2016-01-28 05:22:45 -0800764 // Determine highest needed receive frequency
765 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000766
kwiberg55b97fe2016-01-28 05:22:45 -0800767 // Return the bigger of playout and receive frequency in the ACM.
768 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
769 highestNeeded = audio_coding_->PlayoutFrequency();
770 } else {
771 highestNeeded = receiveFrequency;
772 }
773
774 // Special case, if we're playing a file on the playout side
775 // we take that frequency into consideration as well
776 // This is not needed on sending side, since the codec will
777 // limit the spectrum anyway.
778 if (channel_state_.Get().output_file_playing) {
779 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700780 if (output_file_player_) {
781 if (output_file_player_->Frequency() > highestNeeded) {
782 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800783 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000784 }
kwiberg55b97fe2016-01-28 05:22:45 -0800785 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000786
kwiberg55b97fe2016-01-28 05:22:45 -0800787 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000788}
789
henrikaec6fbd22017-03-31 05:43:36 -0700790int32_t Channel::CreateChannel(Channel*& channel,
791 int32_t channelId,
792 uint32_t instanceId,
793 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800794 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
795 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
796 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000797
solenberg88499ec2016-09-07 07:34:41 -0700798 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800799 if (channel == NULL) {
800 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
801 "Channel::CreateChannel() unable to allocate memory for"
802 " channel");
803 return -1;
804 }
805 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000806}
807
kwiberg55b97fe2016-01-28 05:22:45 -0800808void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
809 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
810 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
811 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000812
kwiberg55b97fe2016-01-28 05:22:45 -0800813 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000814}
815
kwiberg55b97fe2016-01-28 05:22:45 -0800816void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
817 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
818 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
819 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000820
kwiberg55b97fe2016-01-28 05:22:45 -0800821 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000822}
823
kwiberg55b97fe2016-01-28 05:22:45 -0800824void Channel::PlayFileEnded(int32_t id) {
825 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
826 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000827
kwiberg55b97fe2016-01-28 05:22:45 -0800828 if (id == _inputFilePlayerId) {
829 channel_state_.SetInputFilePlaying(false);
830 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
831 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000832 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800833 } else if (id == _outputFilePlayerId) {
834 channel_state_.SetOutputFilePlaying(false);
835 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
836 "Channel::PlayFileEnded() => output file player module is"
837 " shutdown");
838 }
839}
840
841void Channel::RecordFileEnded(int32_t id) {
842 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
843 "Channel::RecordFileEnded(id=%d)", id);
844
845 assert(id == _outputFileRecorderId);
846
847 rtc::CritScope cs(&_fileCritSect);
848
849 _outputFileRecording = false;
850 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
851 "Channel::RecordFileEnded() => output file recorder module is"
852 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000853}
854
pbos@webrtc.org92135212013-05-14 08:31:39 +0000855Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000856 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700857 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800858 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100859 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700860 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800861 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100862 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800863 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100864 rtp_receive_statistics_(
865 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
866 rtp_receiver_(
867 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100868 this,
869 this,
870 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700871 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100872 _outputAudioLevel(),
873 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100874 // Avoid conflict with other channels by adding 1024 - 1026,
875 // won't use as much as 1024 channels.
876 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
877 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
878 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
879 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100880 _timeStamp(0), // This is just an offset, RTP module will add it's own
881 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100882 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100883 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100884 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100885 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100886 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
887 capture_start_rtp_time_stamp_(-1),
888 capture_start_ntp_time_ms_(-1),
889 _engineStatisticsPtr(NULL),
890 _outputMixerPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100891 _moduleProcessThreadPtr(NULL),
892 _audioDeviceModulePtr(NULL),
893 _voiceEngineObserverPtr(NULL),
894 _callbackCritSectPtr(NULL),
895 _transportPtr(NULL),
solenberg1c2af8e2016-03-24 10:36:00 -0700896 input_mute_(false),
897 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100898 _outputGain(1.0f),
solenberg8d73f8c2017-03-08 01:52:20 -0800899 _mixFileWithMicrophone(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100900 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800901 transport_overhead_per_packet_(0),
902 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100903 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100904 restored_packet_in_use_(false),
905 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100906 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700907 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800908 feedback_observer_proxy_(new TransportFeedbackProxy()),
909 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700910 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200911 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
912 kMaxRetransmissionWindowMs)),
elad.alond12a8e12017-03-23 11:04:48 -0700913 decoder_factory_(config.acm_config.decoder_factory),
elad.alon28770482017-03-28 05:03:55 -0700914 use_twcc_plr_for_ana_(
915 webrtc::field_trial::FindFullName("UseTwccPlrForAna") == "Enabled") {
kwiberg55b97fe2016-01-28 05:22:45 -0800916 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
917 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700918 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800919 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700920 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800921 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200922
kwiberg55b97fe2016-01-28 05:22:45 -0800923 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000924
kwiberg55b97fe2016-01-28 05:22:45 -0800925 RtpRtcp::Configuration configuration;
926 configuration.audio = true;
927 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800928 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800929 configuration.receive_statistics = rtp_receive_statistics_.get();
930 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800931 if (pacing_enabled_) {
932 configuration.paced_sender = rtp_packet_sender_proxy_.get();
933 configuration.transport_sequence_number_allocator =
934 seq_num_allocator_proxy_.get();
935 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
936 }
ivoc14d5dbe2016-07-04 07:06:55 -0700937 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800938 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200939 configuration.retransmission_rate_limiter =
940 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000941
kwiberg55b97fe2016-01-28 05:22:45 -0800942 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100943 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000944}
945
kwiberg55b97fe2016-01-28 05:22:45 -0800946Channel::~Channel() {
tommi0a2391f2017-03-21 02:31:51 -0700947 RTC_DCHECK(!channel_state_.Get().sending);
948 RTC_DCHECK(!channel_state_.Get().playing);
niklase@google.com470e71d2011-07-07 08:21:25 +0000949}
950
kwiberg55b97fe2016-01-28 05:22:45 -0800951int32_t Channel::Init() {
tommi0a2391f2017-03-21 02:31:51 -0700952 RTC_DCHECK(construction_thread_.CalledOnValidThread());
kwiberg55b97fe2016-01-28 05:22:45 -0800953 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
954 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000955
kwiberg55b97fe2016-01-28 05:22:45 -0800956 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000957
kwiberg55b97fe2016-01-28 05:22:45 -0800958 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000959
kwiberg55b97fe2016-01-28 05:22:45 -0800960 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
961 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
962 "Channel::Init() must call SetEngineInformation() first");
963 return -1;
964 }
965
966 // --- Add modules to process thread (for periodic schedulation)
967
tommidea489f2017-03-03 03:20:24 -0800968 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
kwiberg55b97fe2016-01-28 05:22:45 -0800969
970 // --- ACM initialization
971
972 if (audio_coding_->InitializeReceiver() == -1) {
973 _engineStatisticsPtr->SetLastError(
974 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
975 "Channel::Init() unable to initialize the ACM - 1");
976 return -1;
977 }
978
979 // --- RTP/RTCP module initialization
980
981 // Ensure that RTCP is enabled by default for the created channel.
982 // Note that, the module will keep generating RTCP until it is explicitly
983 // disabled by the user.
984 // After StopListen (when no sockets exists), RTCP packets will no longer
985 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -0700986 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800987 // RTCP is enabled by default.
988 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
989 // --- Register all permanent callbacks
solenbergfe7dd6d2017-03-11 08:10:43 -0800990 if (audio_coding_->RegisterTransportCallback(this) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800991 _engineStatisticsPtr->SetLastError(
992 VE_CANNOT_INIT_CHANNEL, kTraceError,
993 "Channel::Init() callbacks not registered");
994 return -1;
995 }
996
kwiberg1c07c702017-03-27 07:15:49 -0700997 // Register a default set of send codecs.
998 const int nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
kwiberg55b97fe2016-01-28 05:22:45 -0800999 for (int idx = 0; idx < nSupportedCodecs; idx++) {
kwiberg1c07c702017-03-27 07:15:49 -07001000 CodecInst codec;
1001 RTC_CHECK_EQ(0, audio_coding_->Codec(idx, &codec));
1002
1003 // Ensure that PCMU is used as default send codec.
1004 if (STR_CASE_CMP(codec.plname, "PCMU") == 0 && codec.channels == 1) {
1005 SetSendCodec(codec);
1006 }
1007
1008 // Register default PT for 'telephone-event'
1009 if (STR_CASE_CMP(codec.plname, "telephone-event") == 0) {
1010 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1) {
1011 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1012 "Channel::Init() failed to register outband "
1013 "'telephone-event' (%d/%d) correctly",
1014 codec.pltype, codec.plfreq);
1015 }
1016 }
1017
1018 if (STR_CASE_CMP(codec.plname, "CN") == 0) {
1019 if (!codec_manager_.RegisterEncoder(codec) ||
1020 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
1021 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
1022 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1023 "Channel::Init() failed to register CN (%d/%d) "
1024 "correctly - 1",
1025 codec.pltype, codec.plfreq);
1026 }
1027 }
1028 }
1029
1030 return 0;
1031}
1032
1033void Channel::RegisterLegacyReceiveCodecs() {
1034 const int nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1035 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1036 CodecInst codec;
1037 RTC_CHECK_EQ(0, audio_coding_->Codec(idx, &codec));
1038
kwiberg55b97fe2016-01-28 05:22:45 -08001039 // Open up the RTP/RTCP receiver for all supported codecs
kwiberg1c07c702017-03-27 07:15:49 -07001040 if (rtp_receiver_->RegisterReceivePayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001041 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1042 "Channel::Init() unable to register %s "
1043 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1044 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1045 codec.rate);
1046 } else {
1047 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1048 "Channel::Init() %s (%d/%d/%" PRIuS
1049 "/%d) has been "
1050 "added to the RTP/RTCP receiver",
1051 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1052 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001053 }
1054
kwiberg1c07c702017-03-27 07:15:49 -07001055 // Register default PT for 'telephone-event'
1056 if (STR_CASE_CMP(codec.plname, "telephone-event") == 0) {
1057 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
kwibergda2bf4e2016-10-24 13:47:09 -07001058 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001059 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
kwiberg1c07c702017-03-27 07:15:49 -07001060 "Channel::Init() failed to register inband "
kwiberg55b97fe2016-01-28 05:22:45 -08001061 "'telephone-event' (%d/%d) correctly",
1062 codec.pltype, codec.plfreq);
1063 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001064 }
1065
kwiberg1c07c702017-03-27 07:15:49 -07001066 if (STR_CASE_CMP(codec.plname, "CN") == 0) {
1067 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1068 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001069 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1070 "Channel::Init() failed to register CN (%d/%d) "
1071 "correctly - 1",
1072 codec.pltype, codec.plfreq);
1073 }
1074 }
kwiberg55b97fe2016-01-28 05:22:45 -08001075 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001076}
1077
tommi0a2391f2017-03-21 02:31:51 -07001078void Channel::Terminate() {
1079 RTC_DCHECK(construction_thread_.CalledOnValidThread());
1080 // Must be called on the same thread as Init().
1081 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
1082 "Channel::Terminate");
1083
1084 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
1085
1086 StopSend();
1087 StopPlayout();
1088
1089 {
1090 rtc::CritScope cs(&_fileCritSect);
1091 if (input_file_player_) {
1092 input_file_player_->RegisterModuleFileCallback(NULL);
1093 input_file_player_->StopPlayingFile();
1094 }
1095 if (output_file_player_) {
1096 output_file_player_->RegisterModuleFileCallback(NULL);
1097 output_file_player_->StopPlayingFile();
1098 }
1099 if (output_file_recorder_) {
1100 output_file_recorder_->RegisterModuleFileCallback(NULL);
1101 output_file_recorder_->StopRecording();
1102 }
1103 }
1104
1105 // The order to safely shutdown modules in a channel is:
1106 // 1. De-register callbacks in modules
1107 // 2. De-register modules in process thread
1108 // 3. Destroy modules
1109 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
1110 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1111 "Terminate() failed to de-register transport callback"
1112 " (Audio coding module)");
1113 }
1114
1115 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
1116 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1117 "Terminate() failed to de-register VAD callback"
1118 " (Audio coding module)");
1119 }
1120
1121 // De-register modules in process thread
1122 if (_moduleProcessThreadPtr)
1123 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
1124
1125 // End of modules shutdown
1126}
1127
kwiberg55b97fe2016-01-28 05:22:45 -08001128int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1129 OutputMixer& outputMixer,
kwiberg55b97fe2016-01-28 05:22:45 -08001130 ProcessThread& moduleProcessThread,
1131 AudioDeviceModule& audioDeviceModule,
1132 VoiceEngineObserver* voiceEngineObserver,
henrikaec6fbd22017-03-31 05:43:36 -07001133 rtc::CriticalSection* callbackCritSect,
1134 rtc::TaskQueue* encoder_queue) {
1135 RTC_DCHECK(encoder_queue);
1136 RTC_DCHECK(!encoder_queue_);
kwiberg55b97fe2016-01-28 05:22:45 -08001137 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1138 "Channel::SetEngineInformation()");
1139 _engineStatisticsPtr = &engineStatistics;
1140 _outputMixerPtr = &outputMixer;
kwiberg55b97fe2016-01-28 05:22:45 -08001141 _moduleProcessThreadPtr = &moduleProcessThread;
1142 _audioDeviceModulePtr = &audioDeviceModule;
1143 _voiceEngineObserverPtr = voiceEngineObserver;
1144 _callbackCritSectPtr = callbackCritSect;
henrikaec6fbd22017-03-31 05:43:36 -07001145 encoder_queue_ = encoder_queue;
kwiberg55b97fe2016-01-28 05:22:45 -08001146 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001147}
1148
kwibergb7f89d62016-02-17 10:04:18 -08001149void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001150 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001151 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001152}
1153
ossu29b1a8d2016-06-13 07:34:51 -07001154const rtc::scoped_refptr<AudioDecoderFactory>&
1155Channel::GetAudioDecoderFactory() const {
1156 return decoder_factory_;
1157}
1158
kwiberg55b97fe2016-01-28 05:22:45 -08001159int32_t Channel::StartPlayout() {
1160 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1161 "Channel::StartPlayout()");
1162 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001163 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001164 }
1165
solenberge374e012017-02-14 04:55:00 -08001166 // Add participant as candidates for mixing.
1167 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1168 _engineStatisticsPtr->SetLastError(
1169 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1170 "StartPlayout() failed to add participant to mixer");
1171 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001172 }
1173
1174 channel_state_.SetPlaying(true);
1175 if (RegisterFilePlayingToMixer() != 0)
1176 return -1;
1177
1178 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001179}
1180
kwiberg55b97fe2016-01-28 05:22:45 -08001181int32_t Channel::StopPlayout() {
1182 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1183 "Channel::StopPlayout()");
1184 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001185 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001186 }
1187
solenberge374e012017-02-14 04:55:00 -08001188 // Remove participant as candidates for mixing
1189 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1190 _engineStatisticsPtr->SetLastError(
1191 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1192 "StopPlayout() failed to remove participant from mixer");
1193 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001194 }
1195
1196 channel_state_.SetPlaying(false);
1197 _outputAudioLevel.Clear();
1198
1199 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001200}
1201
kwiberg55b97fe2016-01-28 05:22:45 -08001202int32_t Channel::StartSend() {
1203 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1204 "Channel::StartSend()");
kwiberg55b97fe2016-01-28 05:22:45 -08001205 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001206 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001207 }
1208 channel_state_.SetSending(true);
henrika4515fa02017-05-03 08:30:15 -07001209 {
1210 // It is now OK to start posting tasks to the encoder task queue.
1211 rtc::CritScope cs(&encoder_queue_lock_);
1212 encoder_queue_is_active_ = true;
1213 }
solenberg08b19df2017-02-15 00:42:31 -08001214 // Resume the previous sequence number which was reset by StopSend(). This
1215 // needs to be done before |sending| is set to true on the RTP/RTCP module.
1216 if (send_sequence_number_) {
1217 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
1218 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001219 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001220 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1221 _engineStatisticsPtr->SetLastError(
1222 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1223 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001224 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001225 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001226 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001227 return -1;
1228 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001229
kwiberg55b97fe2016-01-28 05:22:45 -08001230 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001231}
1232
henrikaec6fbd22017-03-31 05:43:36 -07001233void Channel::StopSend() {
kwiberg55b97fe2016-01-28 05:22:45 -08001234 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1235 "Channel::StopSend()");
1236 if (!channel_state_.Get().sending) {
henrikaec6fbd22017-03-31 05:43:36 -07001237 return;
kwiberg55b97fe2016-01-28 05:22:45 -08001238 }
1239 channel_state_.SetSending(false);
1240
henrikaec6fbd22017-03-31 05:43:36 -07001241 // Post a task to the encoder thread which sets an event when the task is
1242 // executed. We know that no more encoding tasks will be added to the task
1243 // queue for this channel since sending is now deactivated. It means that,
1244 // if we wait for the event to bet set, we know that no more pending tasks
1245 // exists and it is therfore guaranteed that the task queue will never try
1246 // to acccess and invalid channel object.
1247 RTC_DCHECK(encoder_queue_);
henrika4515fa02017-05-03 08:30:15 -07001248
henrikaec6fbd22017-03-31 05:43:36 -07001249 rtc::Event flush(false, false);
henrika4515fa02017-05-03 08:30:15 -07001250 {
1251 // Clear |encoder_queue_is_active_| under lock to prevent any other tasks
1252 // than this final "flush task" to be posted on the queue.
1253 rtc::CritScope cs(&encoder_queue_lock_);
1254 encoder_queue_is_active_ = false;
1255 encoder_queue_->PostTask([&flush]() { flush.Set(); });
1256 }
henrikaec6fbd22017-03-31 05:43:36 -07001257 flush.Wait(rtc::Event::kForever);
1258
kwiberg55b97fe2016-01-28 05:22:45 -08001259 // Store the sequence number to be able to pick up the same sequence for
1260 // the next StartSend(). This is needed for restarting device, otherwise
1261 // it might cause libSRTP to complain about packets being replayed.
1262 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1263 // CL is landed. See issue
1264 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1265 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1266
1267 // Reset sending SSRC and sequence number and triggers direct transmission
1268 // of RTCP BYE
1269 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1270 _engineStatisticsPtr->SetLastError(
1271 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1272 "StartSend() RTP/RTCP failed to stop sending");
1273 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001274 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001275}
1276
ossu1ffbd6c2017-04-06 12:05:04 -07001277bool Channel::SetEncoder(int payload_type,
1278 std::unique_ptr<AudioEncoder> encoder) {
1279 RTC_DCHECK_GE(payload_type, 0);
1280 RTC_DCHECK_LE(payload_type, 127);
1281 // TODO(ossu): Make a CodecInst up for now. It seems like very little of this
1282 // information is actually used, possibly only payload type and clock rate.
1283 CodecInst lies;
1284 lies.pltype = payload_type;
1285 strncpy(lies.plname, "audio", sizeof(lies.plname));
1286 lies.plname[sizeof(lies.plname) - 1] = 0;
1287 // Seems unclear if it should be clock rate or sample rate. CodecInst
1288 // supposedly carries the sample rate, but only clock rate seems sensible to
1289 // send to the RTP/RTCP module.
1290 lies.plfreq = encoder->RtpTimestampRateHz();
1291 lies.pacsize = 0;
1292 lies.channels = encoder->NumChannels();
1293 lies.rate = 0;
1294
1295 if (_rtpRtcpModule->RegisterSendPayload(lies) != 0) {
1296 _rtpRtcpModule->DeRegisterSendPayload(payload_type);
1297 if (_rtpRtcpModule->RegisterSendPayload(lies) != 0) {
1298 WEBRTC_TRACE(
1299 kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1300 "SetEncoder() failed to register codec to RTP/RTCP module");
1301 return false;
1302 }
1303 }
1304
1305 audio_coding_->SetEncoder(std::move(encoder));
ossu20a4b3f2017-04-27 02:08:52 -07001306 codec_manager_.UnsetCodecInst();
ossu1ffbd6c2017-04-06 12:05:04 -07001307 return true;
1308}
1309
ossu20a4b3f2017-04-27 02:08:52 -07001310void Channel::ModifyEncoder(
1311 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
1312 audio_coding_->ModifyEncoder(modifier);
1313}
1314
kwiberg55b97fe2016-01-28 05:22:45 -08001315int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1316 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1317 "Channel::RegisterVoiceEngineObserver()");
1318 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001319
kwiberg55b97fe2016-01-28 05:22:45 -08001320 if (_voiceEngineObserverPtr) {
1321 _engineStatisticsPtr->SetLastError(
1322 VE_INVALID_OPERATION, kTraceError,
1323 "RegisterVoiceEngineObserver() observer already enabled");
1324 return -1;
1325 }
1326 _voiceEngineObserverPtr = &observer;
1327 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001328}
1329
kwiberg55b97fe2016-01-28 05:22:45 -08001330int32_t Channel::DeRegisterVoiceEngineObserver() {
1331 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1332 "Channel::DeRegisterVoiceEngineObserver()");
1333 rtc::CritScope cs(&_callbackCritSect);
1334
1335 if (!_voiceEngineObserverPtr) {
1336 _engineStatisticsPtr->SetLastError(
1337 VE_INVALID_OPERATION, kTraceWarning,
1338 "DeRegisterVoiceEngineObserver() observer already disabled");
1339 return 0;
1340 }
1341 _voiceEngineObserverPtr = NULL;
1342 return 0;
1343}
1344
1345int32_t Channel::GetSendCodec(CodecInst& codec) {
ossu20a4b3f2017-04-27 02:08:52 -07001346 {
1347 const CodecInst* send_codec = codec_manager_.GetCodecInst();
1348 if (send_codec) {
1349 codec = *send_codec;
1350 return 0;
1351 }
1352 }
1353 rtc::Optional<CodecInst> acm_send_codec = audio_coding_->SendCodec();
1354 if (acm_send_codec) {
1355 codec = *acm_send_codec;
kwiberg1fd4a4a2015-11-03 11:20:50 -08001356 return 0;
1357 }
1358 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001359}
1360
kwiberg55b97fe2016-01-28 05:22:45 -08001361int32_t Channel::GetRecCodec(CodecInst& codec) {
1362 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001363}
1364
kwiberg55b97fe2016-01-28 05:22:45 -08001365int32_t Channel::SetSendCodec(const CodecInst& codec) {
1366 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1367 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001368
kwibergc8d071e2016-04-06 12:22:38 -07001369 if (!codec_manager_.RegisterEncoder(codec) ||
1370 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001371 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1372 "SetSendCodec() failed to register codec to ACM");
1373 return -1;
1374 }
1375
1376 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1377 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1378 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1379 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1380 "SetSendCodec() failed to register codec to"
1381 " RTP/RTCP module");
1382 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001383 }
kwiberg55b97fe2016-01-28 05:22:45 -08001384 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001385
kwiberg55b97fe2016-01-28 05:22:45 -08001386 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001387}
1388
minyue78b4d562016-11-30 04:47:39 -08001389void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001390 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1391 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001392 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001393 if (*encoder) {
1394 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001395 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001396 }
1397 });
michaelt566d8202017-01-12 10:17:38 -08001398 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001399}
1400
elad.alond12a8e12017-03-23 11:04:48 -07001401void Channel::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
1402 if (!use_twcc_plr_for_ana_)
1403 return;
minyue7e304322016-10-12 05:00:55 -07001404 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
elad.alond12a8e12017-03-23 11:04:48 -07001405 if (*encoder) {
1406 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1407 }
1408 });
1409}
1410
elad.alondadb4dc2017-03-23 15:29:50 -07001411void Channel::OnRecoverableUplinkPacketLossRate(
1412 float recoverable_packet_loss_rate) {
1413 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1414 if (*encoder) {
1415 (*encoder)->OnReceivedUplinkRecoverablePacketLossFraction(
1416 recoverable_packet_loss_rate);
1417 }
1418 });
1419}
1420
elad.alond12a8e12017-03-23 11:04:48 -07001421void Channel::OnUplinkPacketLossRate(float packet_loss_rate) {
1422 if (use_twcc_plr_for_ana_)
1423 return;
1424 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1425 if (*encoder) {
1426 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1427 }
minyue7e304322016-10-12 05:00:55 -07001428 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001429}
1430
kwiberg55b97fe2016-01-28 05:22:45 -08001431int32_t Channel::SetVADStatus(bool enableVAD,
1432 ACMVADMode mode,
1433 bool disableDTX) {
1434 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1435 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001436 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1437 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1438 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001439 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1440 kTraceError,
1441 "SetVADStatus() failed to set VAD");
1442 return -1;
1443 }
1444 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001445}
1446
kwiberg55b97fe2016-01-28 05:22:45 -08001447int32_t Channel::GetVADStatus(bool& enabledVAD,
1448 ACMVADMode& mode,
1449 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001450 const auto* params = codec_manager_.GetStackParams();
1451 enabledVAD = params->use_cng;
1452 mode = params->vad_mode;
1453 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001454 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001455}
1456
kwiberg1c07c702017-03-27 07:15:49 -07001457void Channel::SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) {
1458 rtp_payload_registry_->SetAudioReceivePayloads(codecs);
1459 audio_coding_->SetReceiveCodecs(codecs);
1460}
1461
kwiberg55b97fe2016-01-28 05:22:45 -08001462int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001463 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1464}
1465
1466int32_t Channel::SetRecPayloadType(int payload_type,
1467 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001468 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1469 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001470
kwiberg55b97fe2016-01-28 05:22:45 -08001471 if (channel_state_.Get().playing) {
1472 _engineStatisticsPtr->SetLastError(
1473 VE_ALREADY_PLAYING, kTraceError,
1474 "SetRecPayloadType() unable to set PT while playing");
1475 return -1;
1476 }
kwiberg55b97fe2016-01-28 05:22:45 -08001477
kwiberg09f090c2017-03-01 01:57:11 -08001478 const CodecInst codec = SdpToCodecInst(payload_type, format);
kwibergd32bf752017-01-19 07:03:59 -08001479
1480 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001481 // De-register the selected codec (RTP/RTCP module and ACM)
1482
1483 int8_t pltype(-1);
1484 CodecInst rxCodec = codec;
1485
1486 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001487 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001488 rxCodec.pltype = pltype;
1489
1490 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1491 _engineStatisticsPtr->SetLastError(
1492 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1493 "SetRecPayloadType() RTP/RTCP-module deregistration "
1494 "failed");
1495 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001496 }
kwiberg55b97fe2016-01-28 05:22:45 -08001497 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1498 _engineStatisticsPtr->SetLastError(
1499 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1500 "SetRecPayloadType() ACM deregistration failed - 1");
1501 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001502 }
kwiberg55b97fe2016-01-28 05:22:45 -08001503 return 0;
1504 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001505
magjed56124bd2016-11-24 09:34:46 -08001506 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001507 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001508 // TODO(kwiberg): Retrying is probably not necessary, since
1509 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001510 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001511 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001512 _engineStatisticsPtr->SetLastError(
1513 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1514 "SetRecPayloadType() RTP/RTCP-module registration failed");
1515 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001516 }
kwiberg55b97fe2016-01-28 05:22:45 -08001517 }
kwibergd32bf752017-01-19 07:03:59 -08001518 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1519 audio_coding_->UnregisterReceiveCodec(payload_type);
1520 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001521 _engineStatisticsPtr->SetLastError(
1522 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1523 "SetRecPayloadType() ACM registration failed - 1");
1524 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001525 }
kwiberg55b97fe2016-01-28 05:22:45 -08001526 }
1527 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001528}
1529
kwiberg55b97fe2016-01-28 05:22:45 -08001530int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1531 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001532 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001533 _engineStatisticsPtr->SetLastError(
1534 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1535 "GetRecPayloadType() failed to retrieve RX payload type");
1536 return -1;
1537 }
1538 codec.pltype = payloadType;
1539 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001540}
1541
kwiberg55b97fe2016-01-28 05:22:45 -08001542int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1543 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1544 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001545
kwiberg55b97fe2016-01-28 05:22:45 -08001546 CodecInst codec;
1547 int32_t samplingFreqHz(-1);
1548 const size_t kMono = 1;
1549 if (frequency == kFreq32000Hz)
1550 samplingFreqHz = 32000;
1551 else if (frequency == kFreq16000Hz)
1552 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001553
kwiberg55b97fe2016-01-28 05:22:45 -08001554 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1555 _engineStatisticsPtr->SetLastError(
1556 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1557 "SetSendCNPayloadType() failed to retrieve default CN codec "
1558 "settings");
1559 return -1;
1560 }
1561
1562 // Modify the payload type (must be set to dynamic range)
1563 codec.pltype = type;
1564
kwibergc8d071e2016-04-06 12:22:38 -07001565 if (!codec_manager_.RegisterEncoder(codec) ||
1566 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001567 _engineStatisticsPtr->SetLastError(
1568 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1569 "SetSendCNPayloadType() failed to register CN to ACM");
1570 return -1;
1571 }
1572
1573 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1574 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1575 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1576 _engineStatisticsPtr->SetLastError(
1577 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1578 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1579 "module");
1580 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001581 }
kwiberg55b97fe2016-01-28 05:22:45 -08001582 }
1583 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001584}
1585
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001586int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001587 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001588 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001589
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001590 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001591 _engineStatisticsPtr->SetLastError(
1592 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001593 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001594 return -1;
1595 }
1596 return 0;
1597}
1598
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001599int Channel::SetOpusDtx(bool enable_dtx) {
1600 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1601 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001602 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001603 : audio_coding_->DisableOpusDtx();
1604 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001605 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1606 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001607 return -1;
1608 }
1609 return 0;
1610}
1611
ivoc85228d62016-07-27 04:53:47 -07001612int Channel::GetOpusDtx(bool* enabled) {
1613 int success = -1;
1614 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1615 if (encoder) {
1616 *enabled = encoder->GetDtx();
1617 success = 0;
1618 }
1619 });
1620 return success;
1621}
1622
minyue7e304322016-10-12 05:00:55 -07001623bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1624 bool success = false;
1625 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1626 if (*encoder) {
michaelt92aef172017-04-18 00:11:48 -07001627 success = (*encoder)->EnableAudioNetworkAdaptor(config_string,
1628 event_log_proxy_.get());
minyue7e304322016-10-12 05:00:55 -07001629 }
1630 });
1631 return success;
1632}
1633
1634void Channel::DisableAudioNetworkAdaptor() {
1635 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1636 if (*encoder)
1637 (*encoder)->DisableAudioNetworkAdaptor();
1638 });
1639}
1640
1641void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1642 int max_frame_length_ms) {
1643 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1644 if (*encoder) {
1645 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1646 max_frame_length_ms);
1647 }
1648 });
1649}
1650
mflodman3d7db262016-04-29 00:57:13 -07001651int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001652 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001653 "Channel::RegisterExternalTransport()");
1654
kwiberg55b97fe2016-01-28 05:22:45 -08001655 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001656 if (_externalTransport) {
1657 _engineStatisticsPtr->SetLastError(
1658 VE_INVALID_OPERATION, kTraceError,
1659 "RegisterExternalTransport() external transport already enabled");
1660 return -1;
1661 }
1662 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001663 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001664 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001665}
1666
kwiberg55b97fe2016-01-28 05:22:45 -08001667int32_t Channel::DeRegisterExternalTransport() {
1668 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1669 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001670
kwiberg55b97fe2016-01-28 05:22:45 -08001671 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001672 if (_transportPtr) {
1673 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1674 "DeRegisterExternalTransport() all transport is disabled");
1675 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001676 _engineStatisticsPtr->SetLastError(
1677 VE_INVALID_OPERATION, kTraceWarning,
1678 "DeRegisterExternalTransport() external transport already "
1679 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001680 }
1681 _externalTransport = false;
1682 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001683 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001684}
1685
nisse657bab22017-02-21 06:28:10 -08001686// TODO(nisse): Delete this method together with ReceivedRTPPacket.
1687// It's a temporary hack to support both ReceivedRTPPacket and
1688// OnRtpPacket interfaces without too much code duplication.
1689bool Channel::OnRtpPacketWithHeader(const uint8_t* received_packet,
1690 size_t length,
1691 RTPHeader *header) {
1692 // Store playout timestamp for the received RTP packet
1693 UpdatePlayoutTimestamp(false);
1694
1695 header->payload_type_frequency =
1696 rtp_payload_registry_->GetPayloadTypeFrequency(header->payloadType);
1697 if (header->payload_type_frequency < 0)
1698 return false;
1699 bool in_order = IsPacketInOrder(*header);
1700 rtp_receive_statistics_->IncomingPacket(
1701 *header, length, IsPacketRetransmitted(*header, in_order));
1702 rtp_payload_registry_->SetIncomingPayloadType(*header);
1703
1704 return ReceivePacket(received_packet, length, *header, in_order);
1705}
1706
mflodman3d7db262016-04-29 00:57:13 -07001707int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001708 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001709 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001710 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001711 "Channel::ReceivedRTPPacket()");
1712
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001713 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001714 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1715 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1716 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001717 return -1;
1718 }
nisse657bab22017-02-21 06:28:10 -08001719 return OnRtpPacketWithHeader(received_packet, length, &header) ? 0 : -1;
1720}
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001721
nisse657bab22017-02-21 06:28:10 -08001722void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
1723 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
1724 "Channel::ReceivedRTPPacket()");
1725
1726 RTPHeader header;
1727 packet.GetHeader(&header);
1728 OnRtpPacketWithHeader(packet.data(), packet.size(), &header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001729}
1730
1731bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001732 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001733 const RTPHeader& header,
1734 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001735 if (rtp_payload_registry_->IsRtx(header)) {
1736 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001737 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001738 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001739 assert(packet_length >= header.headerLength);
1740 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001741 PayloadUnion payload_specific;
1742 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001743 &payload_specific)) {
1744 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001745 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001746 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1747 payload_specific, in_order);
1748}
1749
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001750bool Channel::HandleRtxPacket(const uint8_t* packet,
1751 size_t packet_length,
1752 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001753 if (!rtp_payload_registry_->IsRtx(header))
1754 return false;
1755
1756 // Remove the RTX header and parse the original RTP header.
1757 if (packet_length < header.headerLength)
1758 return false;
1759 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1760 return false;
1761 if (restored_packet_in_use_) {
1762 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1763 "Multiple RTX headers detected, dropping packet");
1764 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001765 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001766 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001767 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1768 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001769 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1770 "Incoming RTX packet: invalid RTP header");
1771 return false;
1772 }
1773 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001774 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001775 restored_packet_in_use_ = false;
1776 return ret;
1777}
1778
1779bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1780 StreamStatistician* statistician =
1781 rtp_receive_statistics_->GetStatistician(header.ssrc);
1782 if (!statistician)
1783 return false;
1784 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001785}
1786
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001787bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1788 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001789 // Retransmissions are handled separately if RTX is enabled.
1790 if (rtp_payload_registry_->RtxEnabled())
1791 return false;
1792 StreamStatistician* statistician =
1793 rtp_receive_statistics_->GetStatistician(header.ssrc);
1794 if (!statistician)
1795 return false;
1796 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001797 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001798 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001799 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001800}
1801
mflodman3d7db262016-04-29 00:57:13 -07001802int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001803 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001804 "Channel::ReceivedRTCPPacket()");
1805 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001806 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001807
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001808 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001809 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001810 _engineStatisticsPtr->SetLastError(
1811 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1812 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1813 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001814
Minyue2013aec2015-05-13 14:14:42 +02001815 int64_t rtt = GetRTT(true);
1816 if (rtt == 0) {
1817 // Waiting for valid RTT.
1818 return 0;
1819 }
Erik Språng737336d2016-07-29 12:59:36 +02001820
1821 int64_t nack_window_ms = rtt;
1822 if (nack_window_ms < kMinRetransmissionWindowMs) {
1823 nack_window_ms = kMinRetransmissionWindowMs;
1824 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1825 nack_window_ms = kMaxRetransmissionWindowMs;
1826 }
1827 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1828
minyue7e304322016-10-12 05:00:55 -07001829 // Invoke audio encoders OnReceivedRtt().
1830 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1831 if (*encoder)
1832 (*encoder)->OnReceivedRtt(rtt);
1833 });
1834
Minyue2013aec2015-05-13 14:14:42 +02001835 uint32_t ntp_secs = 0;
1836 uint32_t ntp_frac = 0;
1837 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001838 if (0 !=
1839 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1840 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001841 // Waiting for RTCP.
1842 return 0;
1843 }
1844
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001845 {
tommi31fc21f2016-01-21 10:37:37 -08001846 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001847 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001848 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001849 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001850}
1851
niklase@google.com470e71d2011-07-07 08:21:25 +00001852int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001853 bool loop,
1854 FileFormats format,
1855 int startPosition,
1856 float volumeScaling,
1857 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001858 const CodecInst* codecInst) {
1859 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1860 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1861 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1862 "stopPosition=%d)",
1863 fileName, loop, format, volumeScaling, startPosition,
1864 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001865
kwiberg55b97fe2016-01-28 05:22:45 -08001866 if (channel_state_.Get().output_file_playing) {
1867 _engineStatisticsPtr->SetLastError(
1868 VE_ALREADY_PLAYING, kTraceError,
1869 "StartPlayingFileLocally() is already playing");
1870 return -1;
1871 }
1872
1873 {
1874 rtc::CritScope cs(&_fileCritSect);
1875
kwiberg5a25d952016-08-17 07:31:12 -07001876 if (output_file_player_) {
1877 output_file_player_->RegisterModuleFileCallback(NULL);
1878 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001879 }
1880
kwiberg5b356f42016-09-08 04:32:33 -07001881 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001882 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001883
kwiberg5a25d952016-08-17 07:31:12 -07001884 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001885 _engineStatisticsPtr->SetLastError(
1886 VE_INVALID_ARGUMENT, kTraceError,
1887 "StartPlayingFileLocally() filePlayer format is not correct");
1888 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001889 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001890
kwiberg55b97fe2016-01-28 05:22:45 -08001891 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001892
kwiberg5a25d952016-08-17 07:31:12 -07001893 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001894 fileName, loop, startPosition, volumeScaling, notificationTime,
1895 stopPosition, (const CodecInst*)codecInst) != 0) {
1896 _engineStatisticsPtr->SetLastError(
1897 VE_BAD_FILE, kTraceError,
1898 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001899 output_file_player_->StopPlayingFile();
1900 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001901 return -1;
1902 }
kwiberg5a25d952016-08-17 07:31:12 -07001903 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001904 channel_state_.SetOutputFilePlaying(true);
1905 }
1906
1907 if (RegisterFilePlayingToMixer() != 0)
1908 return -1;
1909
1910 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001911}
1912
1913int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001914 FileFormats format,
1915 int startPosition,
1916 float volumeScaling,
1917 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001918 const CodecInst* codecInst) {
1919 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1920 "Channel::StartPlayingFileLocally(format=%d,"
1921 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1922 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001923
kwiberg55b97fe2016-01-28 05:22:45 -08001924 if (stream == NULL) {
1925 _engineStatisticsPtr->SetLastError(
1926 VE_BAD_FILE, kTraceError,
1927 "StartPlayingFileLocally() NULL as input stream");
1928 return -1;
1929 }
1930
1931 if (channel_state_.Get().output_file_playing) {
1932 _engineStatisticsPtr->SetLastError(
1933 VE_ALREADY_PLAYING, kTraceError,
1934 "StartPlayingFileLocally() is already playing");
1935 return -1;
1936 }
1937
1938 {
1939 rtc::CritScope cs(&_fileCritSect);
1940
1941 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001942 if (output_file_player_) {
1943 output_file_player_->RegisterModuleFileCallback(NULL);
1944 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001945 }
1946
kwiberg55b97fe2016-01-28 05:22:45 -08001947 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001948 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001949 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001950
kwiberg5a25d952016-08-17 07:31:12 -07001951 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001952 _engineStatisticsPtr->SetLastError(
1953 VE_INVALID_ARGUMENT, kTraceError,
1954 "StartPlayingFileLocally() filePlayer format isnot correct");
1955 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001956 }
1957
kwiberg55b97fe2016-01-28 05:22:45 -08001958 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001959
kwiberg4ec01d92016-08-22 08:43:54 -07001960 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001961 volumeScaling, notificationTime,
1962 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001963 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1964 "StartPlayingFile() failed to "
1965 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001966 output_file_player_->StopPlayingFile();
1967 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001968 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001969 }
kwiberg5a25d952016-08-17 07:31:12 -07001970 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001971 channel_state_.SetOutputFilePlaying(true);
1972 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001973
kwiberg55b97fe2016-01-28 05:22:45 -08001974 if (RegisterFilePlayingToMixer() != 0)
1975 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001976
kwiberg55b97fe2016-01-28 05:22:45 -08001977 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001978}
1979
kwiberg55b97fe2016-01-28 05:22:45 -08001980int Channel::StopPlayingFileLocally() {
1981 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1982 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001983
kwiberg55b97fe2016-01-28 05:22:45 -08001984 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001985 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001986 }
1987
1988 {
1989 rtc::CritScope cs(&_fileCritSect);
1990
kwiberg5a25d952016-08-17 07:31:12 -07001991 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001992 _engineStatisticsPtr->SetLastError(
1993 VE_STOP_RECORDING_FAILED, kTraceError,
1994 "StopPlayingFile() could not stop playing");
1995 return -1;
1996 }
kwiberg5a25d952016-08-17 07:31:12 -07001997 output_file_player_->RegisterModuleFileCallback(NULL);
1998 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001999 channel_state_.SetOutputFilePlaying(false);
2000 }
2001 // _fileCritSect cannot be taken while calling
2002 // SetAnonymousMixibilityStatus. Refer to comments in
2003 // StartPlayingFileLocally(const char* ...) for more details.
2004 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
2005 _engineStatisticsPtr->SetLastError(
2006 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2007 "StopPlayingFile() failed to stop participant from playing as"
2008 "file in the mixer");
2009 return -1;
2010 }
2011
2012 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002013}
2014
kwiberg55b97fe2016-01-28 05:22:45 -08002015int Channel::IsPlayingFileLocally() const {
2016 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002017}
2018
kwiberg55b97fe2016-01-28 05:22:45 -08002019int Channel::RegisterFilePlayingToMixer() {
2020 // Return success for not registering for file playing to mixer if:
2021 // 1. playing file before playout is started on that channel.
2022 // 2. starting playout without file playing on that channel.
2023 if (!channel_state_.Get().playing ||
2024 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002025 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002026 }
2027
2028 // |_fileCritSect| cannot be taken while calling
2029 // SetAnonymousMixabilityStatus() since as soon as the participant is added
2030 // frames can be pulled by the mixer. Since the frames are generated from
2031 // the file, _fileCritSect will be taken. This would result in a deadlock.
2032 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
2033 channel_state_.SetOutputFilePlaying(false);
2034 rtc::CritScope cs(&_fileCritSect);
2035 _engineStatisticsPtr->SetLastError(
2036 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2037 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07002038 output_file_player_->StopPlayingFile();
2039 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002040 return -1;
2041 }
2042
2043 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002044}
2045
niklase@google.com470e71d2011-07-07 08:21:25 +00002046int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002047 bool loop,
2048 FileFormats format,
2049 int startPosition,
2050 float volumeScaling,
2051 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002052 const CodecInst* codecInst) {
2053 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2054 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2055 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2056 "stopPosition=%d)",
2057 fileName, loop, format, volumeScaling, startPosition,
2058 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002059
kwiberg55b97fe2016-01-28 05:22:45 -08002060 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002061
kwiberg55b97fe2016-01-28 05:22:45 -08002062 if (channel_state_.Get().input_file_playing) {
2063 _engineStatisticsPtr->SetLastError(
2064 VE_ALREADY_PLAYING, kTraceWarning,
2065 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002066 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002067 }
2068
2069 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002070 if (input_file_player_) {
2071 input_file_player_->RegisterModuleFileCallback(NULL);
2072 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002073 }
2074
2075 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002076 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002077 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002078
kwiberg5a25d952016-08-17 07:31:12 -07002079 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002080 _engineStatisticsPtr->SetLastError(
2081 VE_INVALID_ARGUMENT, kTraceError,
2082 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2083 return -1;
2084 }
2085
2086 const uint32_t notificationTime(0);
2087
kwiberg5a25d952016-08-17 07:31:12 -07002088 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002089 fileName, loop, startPosition, volumeScaling, notificationTime,
2090 stopPosition, (const CodecInst*)codecInst) != 0) {
2091 _engineStatisticsPtr->SetLastError(
2092 VE_BAD_FILE, kTraceError,
2093 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002094 input_file_player_->StopPlayingFile();
2095 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002096 return -1;
2097 }
kwiberg5a25d952016-08-17 07:31:12 -07002098 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002099 channel_state_.SetInputFilePlaying(true);
2100
2101 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002102}
2103
2104int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002105 FileFormats format,
2106 int startPosition,
2107 float volumeScaling,
2108 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002109 const CodecInst* codecInst) {
2110 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2111 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2112 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2113 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002114
kwiberg55b97fe2016-01-28 05:22:45 -08002115 if (stream == NULL) {
2116 _engineStatisticsPtr->SetLastError(
2117 VE_BAD_FILE, kTraceError,
2118 "StartPlayingFileAsMicrophone NULL as input stream");
2119 return -1;
2120 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002121
kwiberg55b97fe2016-01-28 05:22:45 -08002122 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002123
kwiberg55b97fe2016-01-28 05:22:45 -08002124 if (channel_state_.Get().input_file_playing) {
2125 _engineStatisticsPtr->SetLastError(
2126 VE_ALREADY_PLAYING, kTraceWarning,
2127 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002128 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002129 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002130
kwiberg55b97fe2016-01-28 05:22:45 -08002131 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002132 if (input_file_player_) {
2133 input_file_player_->RegisterModuleFileCallback(NULL);
2134 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002135 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002136
kwiberg55b97fe2016-01-28 05:22:45 -08002137 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002138 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002139 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002140
kwiberg5a25d952016-08-17 07:31:12 -07002141 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002142 _engineStatisticsPtr->SetLastError(
2143 VE_INVALID_ARGUMENT, kTraceError,
2144 "StartPlayingInputFile() filePlayer format isnot correct");
2145 return -1;
2146 }
2147
2148 const uint32_t notificationTime(0);
2149
kwiberg4ec01d92016-08-22 08:43:54 -07002150 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2151 notificationTime, stopPosition,
2152 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002153 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2154 "StartPlayingFile() failed to start "
2155 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002156 input_file_player_->StopPlayingFile();
2157 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002158 return -1;
2159 }
2160
kwiberg5a25d952016-08-17 07:31:12 -07002161 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002162 channel_state_.SetInputFilePlaying(true);
2163
2164 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002165}
2166
kwiberg55b97fe2016-01-28 05:22:45 -08002167int Channel::StopPlayingFileAsMicrophone() {
2168 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2169 "Channel::StopPlayingFileAsMicrophone()");
2170
2171 rtc::CritScope cs(&_fileCritSect);
2172
2173 if (!channel_state_.Get().input_file_playing) {
2174 return 0;
2175 }
2176
kwiberg5a25d952016-08-17 07:31:12 -07002177 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002178 _engineStatisticsPtr->SetLastError(
2179 VE_STOP_RECORDING_FAILED, kTraceError,
2180 "StopPlayingFile() could not stop playing");
2181 return -1;
2182 }
kwiberg5a25d952016-08-17 07:31:12 -07002183 input_file_player_->RegisterModuleFileCallback(NULL);
2184 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002185 channel_state_.SetInputFilePlaying(false);
2186
2187 return 0;
2188}
2189
2190int Channel::IsPlayingFileAsMicrophone() const {
2191 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002192}
2193
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002194int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002195 const CodecInst* codecInst) {
2196 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2197 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002198
kwiberg55b97fe2016-01-28 05:22:45 -08002199 if (_outputFileRecording) {
2200 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2201 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002202 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002203 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002204
kwiberg55b97fe2016-01-28 05:22:45 -08002205 FileFormats format;
2206 const uint32_t notificationTime(0); // Not supported in VoE
2207 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002208
kwiberg55b97fe2016-01-28 05:22:45 -08002209 if ((codecInst != NULL) &&
2210 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2211 _engineStatisticsPtr->SetLastError(
2212 VE_BAD_ARGUMENT, kTraceError,
2213 "StartRecordingPlayout() invalid compression");
2214 return (-1);
2215 }
2216 if (codecInst == NULL) {
2217 format = kFileFormatPcm16kHzFile;
2218 codecInst = &dummyCodec;
2219 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2220 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2221 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2222 format = kFileFormatWavFile;
2223 } else {
2224 format = kFileFormatCompressedFile;
2225 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002226
kwiberg55b97fe2016-01-28 05:22:45 -08002227 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002228
kwiberg55b97fe2016-01-28 05:22:45 -08002229 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002230 if (output_file_recorder_) {
2231 output_file_recorder_->RegisterModuleFileCallback(NULL);
2232 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002233 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002234
kwiberg5a25d952016-08-17 07:31:12 -07002235 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002236 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002237 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002238 _engineStatisticsPtr->SetLastError(
2239 VE_INVALID_ARGUMENT, kTraceError,
2240 "StartRecordingPlayout() fileRecorder format isnot correct");
2241 return -1;
2242 }
2243
kwiberg5a25d952016-08-17 07:31:12 -07002244 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002245 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2246 _engineStatisticsPtr->SetLastError(
2247 VE_BAD_FILE, kTraceError,
2248 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002249 output_file_recorder_->StopRecording();
2250 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002251 return -1;
2252 }
kwiberg5a25d952016-08-17 07:31:12 -07002253 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002254 _outputFileRecording = true;
2255
2256 return 0;
2257}
2258
2259int Channel::StartRecordingPlayout(OutStream* stream,
2260 const CodecInst* codecInst) {
2261 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2262 "Channel::StartRecordingPlayout()");
2263
2264 if (_outputFileRecording) {
2265 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2266 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002267 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002268 }
2269
2270 FileFormats format;
2271 const uint32_t notificationTime(0); // Not supported in VoE
2272 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2273
2274 if (codecInst != NULL && codecInst->channels != 1) {
2275 _engineStatisticsPtr->SetLastError(
2276 VE_BAD_ARGUMENT, kTraceError,
2277 "StartRecordingPlayout() invalid compression");
2278 return (-1);
2279 }
2280 if (codecInst == NULL) {
2281 format = kFileFormatPcm16kHzFile;
2282 codecInst = &dummyCodec;
2283 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2284 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2285 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2286 format = kFileFormatWavFile;
2287 } else {
2288 format = kFileFormatCompressedFile;
2289 }
2290
2291 rtc::CritScope cs(&_fileCritSect);
2292
2293 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002294 if (output_file_recorder_) {
2295 output_file_recorder_->RegisterModuleFileCallback(NULL);
2296 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002297 }
2298
kwiberg5a25d952016-08-17 07:31:12 -07002299 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002300 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002301 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002302 _engineStatisticsPtr->SetLastError(
2303 VE_INVALID_ARGUMENT, kTraceError,
2304 "StartRecordingPlayout() fileRecorder format isnot correct");
2305 return -1;
2306 }
2307
kwiberg4ec01d92016-08-22 08:43:54 -07002308 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002309 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002310 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2311 "StartRecordingPlayout() failed to "
2312 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002313 output_file_recorder_->StopRecording();
2314 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002315 return -1;
2316 }
2317
kwiberg5a25d952016-08-17 07:31:12 -07002318 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002319 _outputFileRecording = true;
2320
2321 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002322}
2323
kwiberg55b97fe2016-01-28 05:22:45 -08002324int Channel::StopRecordingPlayout() {
2325 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2326 "Channel::StopRecordingPlayout()");
2327
2328 if (!_outputFileRecording) {
2329 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2330 "StopRecordingPlayout() isnot recording");
2331 return -1;
2332 }
2333
2334 rtc::CritScope cs(&_fileCritSect);
2335
kwiberg5a25d952016-08-17 07:31:12 -07002336 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002337 _engineStatisticsPtr->SetLastError(
2338 VE_STOP_RECORDING_FAILED, kTraceError,
2339 "StopRecording() could not stop recording");
2340 return (-1);
2341 }
kwiberg5a25d952016-08-17 07:31:12 -07002342 output_file_recorder_->RegisterModuleFileCallback(NULL);
2343 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002344 _outputFileRecording = false;
2345
2346 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002347}
2348
kwiberg55b97fe2016-01-28 05:22:45 -08002349void Channel::SetMixWithMicStatus(bool mix) {
2350 rtc::CritScope cs(&_fileCritSect);
2351 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002352}
2353
solenberg8d73f8c2017-03-08 01:52:20 -08002354int Channel::GetSpeechOutputLevel() const {
2355 return _outputAudioLevel.Level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002356}
2357
solenberg8d73f8c2017-03-08 01:52:20 -08002358int Channel::GetSpeechOutputLevelFullRange() const {
2359 return _outputAudioLevel.LevelFullRange();
kwiberg55b97fe2016-01-28 05:22:45 -08002360}
2361
solenberg8d73f8c2017-03-08 01:52:20 -08002362void Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002363 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002364 input_mute_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002365}
2366
solenberg1c2af8e2016-03-24 10:36:00 -07002367bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002368 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002369 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002370}
2371
solenberg8d73f8c2017-03-08 01:52:20 -08002372void Channel::SetChannelOutputVolumeScaling(float scaling) {
kwiberg55b97fe2016-01-28 05:22:45 -08002373 rtc::CritScope cs(&volume_settings_critsect_);
kwiberg55b97fe2016-01-28 05:22:45 -08002374 _outputGain = scaling;
niklase@google.com470e71d2011-07-07 08:21:25 +00002375}
2376
solenberg8842c3e2016-03-11 03:06:41 -08002377int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002378 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002379 "Channel::SendTelephoneEventOutband(...)");
2380 RTC_DCHECK_LE(0, event);
2381 RTC_DCHECK_GE(255, event);
2382 RTC_DCHECK_LE(0, duration_ms);
2383 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002384 if (!Sending()) {
2385 return -1;
2386 }
solenberg8842c3e2016-03-11 03:06:41 -08002387 if (_rtpRtcpModule->SendTelephoneEventOutband(
2388 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002389 _engineStatisticsPtr->SetLastError(
2390 VE_SEND_DTMF_FAILED, kTraceWarning,
2391 "SendTelephoneEventOutband() failed to send event");
2392 return -1;
2393 }
2394 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002395}
2396
solenbergffbbcac2016-11-17 05:25:37 -08002397int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2398 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002399 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002400 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002401 RTC_DCHECK_LE(0, payload_type);
2402 RTC_DCHECK_GE(127, payload_type);
2403 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002404 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002405 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002406 memcpy(codec.plname, "telephone-event", 16);
2407 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2408 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2409 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2410 _engineStatisticsPtr->SetLastError(
2411 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2412 "SetSendTelephoneEventPayloadType() failed to register send"
2413 "payload type");
2414 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002415 }
kwiberg55b97fe2016-01-28 05:22:45 -08002416 }
kwiberg55b97fe2016-01-28 05:22:45 -08002417 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002418}
2419
kwiberg55b97fe2016-01-28 05:22:45 -08002420int Channel::SetLocalSSRC(unsigned int ssrc) {
2421 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2422 "Channel::SetLocalSSRC()");
2423 if (channel_state_.Get().sending) {
2424 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2425 "SetLocalSSRC() already sending");
2426 return -1;
2427 }
2428 _rtpRtcpModule->SetSSRC(ssrc);
2429 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002430}
2431
kwiberg55b97fe2016-01-28 05:22:45 -08002432int Channel::GetLocalSSRC(unsigned int& ssrc) {
2433 ssrc = _rtpRtcpModule->SSRC();
2434 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002435}
2436
kwiberg55b97fe2016-01-28 05:22:45 -08002437int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2438 ssrc = rtp_receiver_->SSRC();
2439 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002440}
2441
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002442int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002443 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002444 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002445}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002446
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002447int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2448 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002449 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2450 if (enable &&
2451 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2452 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002453 return -1;
2454 }
2455 return 0;
2456}
2457
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002458void Channel::EnableSendTransportSequenceNumber(int id) {
2459 int ret =
2460 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2461 RTC_DCHECK_EQ(0, ret);
2462}
2463
stefan3313ec92016-01-21 06:32:43 -08002464void Channel::EnableReceiveTransportSequenceNumber(int id) {
2465 rtp_header_parser_->DeregisterRtpHeaderExtension(
2466 kRtpExtensionTransportSequenceNumber);
2467 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2468 kRtpExtensionTransportSequenceNumber, id);
2469 RTC_DCHECK(ret);
2470}
2471
stefanbba9dec2016-02-01 04:39:55 -08002472void Channel::RegisterSenderCongestionControlObjects(
nisseb8f9a322017-03-27 05:36:15 -07002473 RtpTransportControllerSendInterface* transport,
stefan7de8d642017-02-07 07:14:08 -08002474 RtcpBandwidthObserver* bandwidth_observer) {
nisseb8f9a322017-03-27 05:36:15 -07002475 RtpPacketSender* rtp_packet_sender = transport->packet_sender();
2476 TransportFeedbackObserver* transport_feedback_observer =
2477 transport->transport_feedback_observer();
2478 PacketRouter* packet_router = transport->packet_router();
2479
stefanbba9dec2016-02-01 04:39:55 -08002480 RTC_DCHECK(rtp_packet_sender);
2481 RTC_DCHECK(transport_feedback_observer);
2482 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002483 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002484 feedback_observer_proxy_->SetTransportFeedbackObserver(
2485 transport_feedback_observer);
2486 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2487 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2488 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
nissefdbfdc92017-03-31 05:44:52 -07002489 packet_router->AddSendRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002490 packet_router_ = packet_router;
2491}
2492
stefanbba9dec2016-02-01 04:39:55 -08002493void Channel::RegisterReceiverCongestionControlObjects(
2494 PacketRouter* packet_router) {
2495 RTC_DCHECK(packet_router && !packet_router_);
nissefdbfdc92017-03-31 05:44:52 -07002496 packet_router->AddReceiveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002497 packet_router_ = packet_router;
2498}
2499
nissefdbfdc92017-03-31 05:44:52 -07002500void Channel::ResetSenderCongestionControlObjects() {
stefanbba9dec2016-02-01 04:39:55 -08002501 RTC_DCHECK(packet_router_);
2502 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002503 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002504 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2505 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
nissefdbfdc92017-03-31 05:44:52 -07002506 packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002507 packet_router_ = nullptr;
2508 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2509}
2510
nissefdbfdc92017-03-31 05:44:52 -07002511void Channel::ResetReceiverCongestionControlObjects() {
2512 RTC_DCHECK(packet_router_);
2513 packet_router_->RemoveReceiveRtpModule(_rtpRtcpModule.get());
2514 packet_router_ = nullptr;
2515}
2516
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002517void Channel::SetRTCPStatus(bool enable) {
2518 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2519 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002520 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002521}
2522
kwiberg55b97fe2016-01-28 05:22:45 -08002523int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002524 RtcpMode method = _rtpRtcpModule->RTCP();
2525 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002526 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002527}
2528
kwiberg55b97fe2016-01-28 05:22:45 -08002529int Channel::SetRTCP_CNAME(const char cName[256]) {
2530 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2531 "Channel::SetRTCP_CNAME()");
2532 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2533 _engineStatisticsPtr->SetLastError(
2534 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2535 "SetRTCP_CNAME() failed to set RTCP CNAME");
2536 return -1;
2537 }
2538 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002539}
2540
kwiberg55b97fe2016-01-28 05:22:45 -08002541int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2542 if (cName == NULL) {
2543 _engineStatisticsPtr->SetLastError(
2544 VE_INVALID_ARGUMENT, kTraceError,
2545 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2546 return -1;
2547 }
2548 char cname[RTCP_CNAME_SIZE];
2549 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2550 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2551 _engineStatisticsPtr->SetLastError(
2552 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2553 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2554 return -1;
2555 }
2556 strcpy(cName, cname);
2557 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002558}
2559
kwiberg55b97fe2016-01-28 05:22:45 -08002560int Channel::SendApplicationDefinedRTCPPacket(
2561 unsigned char subType,
2562 unsigned int name,
2563 const char* data,
2564 unsigned short dataLengthInBytes) {
2565 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2566 "Channel::SendApplicationDefinedRTCPPacket()");
2567 if (!channel_state_.Get().sending) {
2568 _engineStatisticsPtr->SetLastError(
2569 VE_NOT_SENDING, kTraceError,
2570 "SendApplicationDefinedRTCPPacket() not sending");
2571 return -1;
2572 }
2573 if (NULL == data) {
2574 _engineStatisticsPtr->SetLastError(
2575 VE_INVALID_ARGUMENT, kTraceError,
2576 "SendApplicationDefinedRTCPPacket() invalid data value");
2577 return -1;
2578 }
2579 if (dataLengthInBytes % 4 != 0) {
2580 _engineStatisticsPtr->SetLastError(
2581 VE_INVALID_ARGUMENT, kTraceError,
2582 "SendApplicationDefinedRTCPPacket() invalid length value");
2583 return -1;
2584 }
2585 RtcpMode status = _rtpRtcpModule->RTCP();
2586 if (status == RtcpMode::kOff) {
2587 _engineStatisticsPtr->SetLastError(
2588 VE_RTCP_ERROR, kTraceError,
2589 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2590 return -1;
2591 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002592
kwiberg55b97fe2016-01-28 05:22:45 -08002593 // Create and schedule the RTCP APP packet for transmission
2594 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2595 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2596 _engineStatisticsPtr->SetLastError(
2597 VE_SEND_ERROR, kTraceError,
2598 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2599 return -1;
2600 }
2601 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002602}
2603
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002604int Channel::GetRemoteRTCPReportBlocks(
2605 std::vector<ReportBlock>* report_blocks) {
2606 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002607 _engineStatisticsPtr->SetLastError(
2608 VE_INVALID_ARGUMENT, kTraceError,
2609 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002610 return -1;
2611 }
2612
2613 // Get the report blocks from the latest received RTCP Sender or Receiver
2614 // Report. Each element in the vector contains the sender's SSRC and a
2615 // report block according to RFC 3550.
2616 std::vector<RTCPReportBlock> rtcp_report_blocks;
2617 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002618 return -1;
2619 }
2620
2621 if (rtcp_report_blocks.empty())
2622 return 0;
2623
2624 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2625 for (; it != rtcp_report_blocks.end(); ++it) {
2626 ReportBlock report_block;
2627 report_block.sender_SSRC = it->remoteSSRC;
2628 report_block.source_SSRC = it->sourceSSRC;
2629 report_block.fraction_lost = it->fractionLost;
2630 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2631 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2632 report_block.interarrival_jitter = it->jitter;
2633 report_block.last_SR_timestamp = it->lastSR;
2634 report_block.delay_since_last_SR = it->delaySinceLastSR;
2635 report_blocks->push_back(report_block);
2636 }
2637 return 0;
2638}
2639
kwiberg55b97fe2016-01-28 05:22:45 -08002640int Channel::GetRTPStatistics(CallStatistics& stats) {
2641 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002642
kwiberg55b97fe2016-01-28 05:22:45 -08002643 // The jitter statistics is updated for each received RTP packet and is
2644 // based on received packets.
2645 RtcpStatistics statistics;
2646 StreamStatistician* statistician =
2647 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002648 if (statistician) {
2649 statistician->GetStatistics(&statistics,
2650 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002651 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002652
kwiberg55b97fe2016-01-28 05:22:45 -08002653 stats.fractionLost = statistics.fraction_lost;
2654 stats.cumulativeLost = statistics.cumulative_lost;
2655 stats.extendedMax = statistics.extended_max_sequence_number;
2656 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002657
kwiberg55b97fe2016-01-28 05:22:45 -08002658 // --- RTT
2659 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002660
kwiberg55b97fe2016-01-28 05:22:45 -08002661 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002662
kwiberg55b97fe2016-01-28 05:22:45 -08002663 size_t bytesSent(0);
2664 uint32_t packetsSent(0);
2665 size_t bytesReceived(0);
2666 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002667
kwiberg55b97fe2016-01-28 05:22:45 -08002668 if (statistician) {
2669 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2670 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002671
kwiberg55b97fe2016-01-28 05:22:45 -08002672 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2673 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2674 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2675 " output will not be complete");
2676 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002677
kwiberg55b97fe2016-01-28 05:22:45 -08002678 stats.bytesSent = bytesSent;
2679 stats.packetsSent = packetsSent;
2680 stats.bytesReceived = bytesReceived;
2681 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002682
kwiberg55b97fe2016-01-28 05:22:45 -08002683 // --- Timestamps
2684 {
2685 rtc::CritScope lock(&ts_stats_lock_);
2686 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2687 }
2688 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002689}
2690
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002691int Channel::SetCodecFECStatus(bool enable) {
2692 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2693 "Channel::SetCodecFECStatus()");
2694
kwibergc8d071e2016-04-06 12:22:38 -07002695 if (!codec_manager_.SetCodecFEC(enable) ||
2696 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002697 _engineStatisticsPtr->SetLastError(
2698 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2699 "SetCodecFECStatus() failed to set FEC state");
2700 return -1;
2701 }
2702 return 0;
2703}
2704
2705bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002706 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002707}
2708
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002709void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2710 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002711 // If pacing is enabled we always store packets.
2712 if (!pacing_enabled_)
2713 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002714 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002715 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002716 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002717 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002718 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002719}
2720
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002721// Called when we are missing one or more packets.
2722int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002723 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2724}
2725
henrikaec6fbd22017-03-31 05:43:36 -07002726void Channel::ProcessAndEncodeAudio(const AudioFrame& audio_input) {
henrika4515fa02017-05-03 08:30:15 -07002727 // Avoid posting any new tasks if sending was already stopped in StopSend().
2728 rtc::CritScope cs(&encoder_queue_lock_);
2729 if (!encoder_queue_is_active_) {
2730 return;
2731 }
henrikaec6fbd22017-03-31 05:43:36 -07002732 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
2733 // TODO(henrika): try to avoid copying by moving ownership of audio frame
2734 // either into pool of frames or into the task itself.
2735 audio_frame->CopyFrom(audio_input);
2736 audio_frame->id_ = ChannelId();
2737 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
2738 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
niklase@google.com470e71d2011-07-07 08:21:25 +00002739}
2740
henrikaec6fbd22017-03-31 05:43:36 -07002741void Channel::ProcessAndEncodeAudio(const int16_t* audio_data,
2742 int sample_rate,
2743 size_t number_of_frames,
2744 size_t number_of_channels) {
henrika4515fa02017-05-03 08:30:15 -07002745 // Avoid posting as new task if sending was already stopped in StopSend().
2746 rtc::CritScope cs(&encoder_queue_lock_);
2747 if (!encoder_queue_is_active_) {
2748 return;
2749 }
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002750 CodecInst codec;
2751 GetSendCodec(codec);
henrikaec6fbd22017-03-31 05:43:36 -07002752 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
2753 audio_frame->id_ = ChannelId();
2754 audio_frame->sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2755 audio_frame->num_channels_ = std::min(number_of_channels, codec.channels);
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002756 RemixAndResample(audio_data, number_of_frames, number_of_channels,
henrikaec6fbd22017-03-31 05:43:36 -07002757 sample_rate, &input_resampler_, audio_frame.get());
2758 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
2759 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002760}
2761
henrikaec6fbd22017-03-31 05:43:36 -07002762void Channel::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
2763 RTC_DCHECK_RUN_ON(encoder_queue_);
2764 RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
2765 RTC_DCHECK_LE(audio_input->num_channels_, 2);
2766 RTC_DCHECK_EQ(audio_input->id_, ChannelId());
kwiberg55b97fe2016-01-28 05:22:45 -08002767
2768 if (channel_state_.Get().input_file_playing) {
henrikaec6fbd22017-03-31 05:43:36 -07002769 MixOrReplaceAudioWithFile(audio_input);
kwiberg55b97fe2016-01-28 05:22:45 -08002770 }
2771
henrikaec6fbd22017-03-31 05:43:36 -07002772 bool is_muted = InputMute();
2773 AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002774
kwiberg55b97fe2016-01-28 05:22:45 -08002775 if (_includeAudioLevelIndication) {
2776 size_t length =
henrikaec6fbd22017-03-31 05:43:36 -07002777 audio_input->samples_per_channel_ * audio_input->num_channels_;
2778 RTC_CHECK_LE(length, sizeof(audio_input->data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002779 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002780 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002781 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002782 rms_level_.Analyze(
henrikaec6fbd22017-03-31 05:43:36 -07002783 rtc::ArrayView<const int16_t>(audio_input->data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002784 }
kwiberg55b97fe2016-01-28 05:22:45 -08002785 }
solenberg1c2af8e2016-03-24 10:36:00 -07002786 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002787
henrikaec6fbd22017-03-31 05:43:36 -07002788 // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002789
kwiberg55b97fe2016-01-28 05:22:45 -08002790 // The ACM resamples internally.
henrikaec6fbd22017-03-31 05:43:36 -07002791 audio_input->timestamp_ = _timeStamp;
kwiberg55b97fe2016-01-28 05:22:45 -08002792 // This call will trigger AudioPacketizationCallback::SendData if encoding
2793 // is done and payload is ready for packetization and transmission.
2794 // Otherwise, it will return without invoking the callback.
henrikaec6fbd22017-03-31 05:43:36 -07002795 if (audio_coding_->Add10MsData(*audio_input) < 0) {
2796 LOG(LS_ERROR) << "ACM::Add10MsData() failed for channel " << _channelId;
2797 return;
kwiberg55b97fe2016-01-28 05:22:45 -08002798 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002799
henrikaec6fbd22017-03-31 05:43:36 -07002800 _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002801}
2802
solenberg7602aab2016-11-14 11:30:07 -08002803void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2804 RTC_DCHECK(!channel.channel() ||
2805 channel.channel()->ChannelId() != _channelId);
2806 rtc::CritScope lock(&assoc_send_channel_lock_);
2807 associate_send_channel_ = channel;
2808}
2809
Minyue2013aec2015-05-13 14:14:42 +02002810void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002811 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002812 Channel* channel = associate_send_channel_.channel();
2813 if (channel && channel->ChannelId() == channel_id) {
2814 // If this channel is associated with a send channel of the specified
2815 // Channel ID, disassociate with it.
2816 ChannelOwner ref(NULL);
2817 associate_send_channel_ = ref;
2818 }
2819}
2820
ivoc14d5dbe2016-07-04 07:06:55 -07002821void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2822 event_log_proxy_->SetEventLog(event_log);
2823}
2824
michaelt9332b7d2016-11-30 07:51:13 -08002825void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2826 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2827}
2828
nisse284542b2017-01-10 08:58:32 -08002829void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08002830 size_t overhead_per_packet =
2831 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08002832 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2833 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08002834 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08002835 }
2836 });
2837}
2838
2839void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002840 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002841 transport_overhead_per_packet_ = transport_overhead_per_packet;
2842 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002843}
2844
hbos3fd31fe2017-02-28 05:43:16 -08002845// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08002846void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002847 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002848 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2849 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002850}
2851
kwiberg55b97fe2016-01-28 05:22:45 -08002852int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2853 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002854}
2855
wu@webrtc.org24301a62013-12-13 19:17:43 +00002856void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2857 audio_coding_->GetDecodingCallStatistics(stats);
2858}
2859
solenberg358057b2015-11-27 10:46:42 -08002860uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002861 rtc::CritScope lock(&video_sync_lock_);
2862 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002863}
2864
kwiberg55b97fe2016-01-28 05:22:45 -08002865int Channel::SetMinimumPlayoutDelay(int delayMs) {
2866 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2867 "Channel::SetMinimumPlayoutDelay()");
2868 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2869 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2870 _engineStatisticsPtr->SetLastError(
2871 VE_INVALID_ARGUMENT, kTraceError,
2872 "SetMinimumPlayoutDelay() invalid min delay");
2873 return -1;
2874 }
2875 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2876 _engineStatisticsPtr->SetLastError(
2877 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2878 "SetMinimumPlayoutDelay() failed to set min playout delay");
2879 return -1;
2880 }
2881 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002882}
2883
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002884int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002885 uint32_t playout_timestamp_rtp = 0;
2886 {
tommi31fc21f2016-01-21 10:37:37 -08002887 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002888 playout_timestamp_rtp = playout_timestamp_rtp_;
2889 }
kwiberg55b97fe2016-01-28 05:22:45 -08002890 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002891 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002892 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002893 "GetPlayoutTimestamp() failed to retrieve timestamp");
2894 return -1;
2895 }
deadbeef74375882015-08-13 12:09:10 -07002896 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002897 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002898}
2899
kwiberg55b97fe2016-01-28 05:22:45 -08002900int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2901 RtpReceiver** rtp_receiver) const {
2902 *rtpRtcpModule = _rtpRtcpModule.get();
2903 *rtp_receiver = rtp_receiver_.get();
2904 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002905}
2906
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002907// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2908// a shared helper.
henrikaec6fbd22017-03-31 05:43:36 -07002909int32_t Channel::MixOrReplaceAudioWithFile(AudioFrame* audio_input) {
2910 RTC_DCHECK_RUN_ON(encoder_queue_);
kwibergb7f89d62016-02-17 10:04:18 -08002911 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002912 size_t fileSamples(0);
henrikaec6fbd22017-03-31 05:43:36 -07002913 const int mixingFrequency = audio_input->sample_rate_hz_;
kwiberg55b97fe2016-01-28 05:22:45 -08002914 {
2915 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002916
kwiberg5a25d952016-08-17 07:31:12 -07002917 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002918 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2919 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2920 " doesnt exist");
2921 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002922 }
2923
kwiberg4ec01d92016-08-22 08:43:54 -07002924 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002925 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002926 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2927 "Channel::MixOrReplaceAudioWithFile() file mixing "
2928 "failed");
2929 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002930 }
kwiberg55b97fe2016-01-28 05:22:45 -08002931 if (fileSamples == 0) {
2932 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2933 "Channel::MixOrReplaceAudioWithFile() file is ended");
2934 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002935 }
kwiberg55b97fe2016-01-28 05:22:45 -08002936 }
2937
henrikaec6fbd22017-03-31 05:43:36 -07002938 RTC_DCHECK_EQ(audio_input->samples_per_channel_, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08002939
2940 if (_mixFileWithMicrophone) {
2941 // Currently file stream is always mono.
2942 // TODO(xians): Change the code when FilePlayer supports real stereo.
henrikaec6fbd22017-03-31 05:43:36 -07002943 MixWithSat(audio_input->data_, audio_input->num_channels_, fileBuffer.get(),
kwiberg55b97fe2016-01-28 05:22:45 -08002944 1, fileSamples);
2945 } else {
2946 // Replace ACM audio with file.
2947 // Currently file stream is always mono.
2948 // TODO(xians): Change the code when FilePlayer supports real stereo.
henrikaec6fbd22017-03-31 05:43:36 -07002949 audio_input->UpdateFrame(
kwiberg55b97fe2016-01-28 05:22:45 -08002950 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2951 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2952 }
2953 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002954}
2955
kwiberg55b97fe2016-01-28 05:22:45 -08002956int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2957 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002958
kwibergb7f89d62016-02-17 10:04:18 -08002959 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002960 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002961
kwiberg55b97fe2016-01-28 05:22:45 -08002962 {
2963 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002964
kwiberg5a25d952016-08-17 07:31:12 -07002965 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002966 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2967 "Channel::MixAudioWithFile() file mixing failed");
2968 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002969 }
2970
kwiberg55b97fe2016-01-28 05:22:45 -08002971 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002972 if (output_file_player_->Get10msAudioFromFile(
2973 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002974 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2975 "Channel::MixAudioWithFile() file mixing failed");
2976 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002977 }
kwiberg55b97fe2016-01-28 05:22:45 -08002978 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002979
kwiberg55b97fe2016-01-28 05:22:45 -08002980 if (audioFrame.samples_per_channel_ == fileSamples) {
2981 // Currently file stream is always mono.
2982 // TODO(xians): Change the code when FilePlayer supports real stereo.
2983 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
2984 fileSamples);
2985 } else {
2986 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2987 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2988 ") != "
2989 "fileSamples(%" PRIuS ")",
2990 audioFrame.samples_per_channel_, fileSamples);
2991 return -1;
2992 }
2993
2994 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002995}
2996
deadbeef74375882015-08-13 12:09:10 -07002997void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002998 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002999
henrik.lundin96bd5022016-04-06 04:13:56 -07003000 if (!jitter_buffer_playout_timestamp_) {
3001 // This can happen if this channel has not received any RTP packets. In
3002 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003003 return;
3004 }
3005
3006 uint16_t delay_ms = 0;
3007 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003008 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003009 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3010 " delay from the ADM");
3011 _engineStatisticsPtr->SetLastError(
3012 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3013 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3014 return;
3015 }
3016
henrik.lundin96bd5022016-04-06 04:13:56 -07003017 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3018 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003019
3020 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003021 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003022
kwiberg55b97fe2016-01-28 05:22:45 -08003023 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003024 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003025 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003026
3027 {
tommi31fc21f2016-01-21 10:37:37 -08003028 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08003029 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003030 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003031 }
3032 playout_delay_ms_ = delay_ms;
3033 }
3034}
3035
kwiberg55b97fe2016-01-28 05:22:45 -08003036void Channel::RegisterReceiveCodecsToRTPModule() {
3037 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3038 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003039
kwiberg55b97fe2016-01-28 05:22:45 -08003040 CodecInst codec;
3041 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003042
kwiberg55b97fe2016-01-28 05:22:45 -08003043 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3044 // Open up the RTP/RTCP receiver for all supported codecs
3045 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08003046 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08003047 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3048 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3049 " to register %s (%d/%d/%" PRIuS
3050 "/%d) to RTP/RTCP "
3051 "receiver",
3052 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3053 codec.rate);
3054 } else {
3055 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3056 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3057 "(%d/%d/%" PRIuS
3058 "/%d) has been added to the RTP/RTCP "
3059 "receiver",
3060 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3061 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003062 }
kwiberg55b97fe2016-01-28 05:22:45 -08003063 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003064}
3065
kwiberg55b97fe2016-01-28 05:22:45 -08003066int Channel::SetSendRtpHeaderExtension(bool enable,
3067 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003068 unsigned char id) {
3069 int error = 0;
3070 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3071 if (enable) {
3072 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3073 }
3074 return error;
3075}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003076
ossue280cde2016-10-12 11:04:10 -07003077int Channel::GetRtpTimestampRateHz() const {
3078 const auto format = audio_coding_->ReceiveFormat();
3079 // Default to the playout frequency if we've not gotten any packets yet.
3080 // TODO(ossu): Zero clockrate can only happen if we've added an external
3081 // decoder for a format we don't support internally. Remove once that way of
3082 // adding decoders is gone!
3083 return (format && format->clockrate_hz != 0)
3084 ? format->clockrate_hz
3085 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003086}
3087
Minyue2013aec2015-05-13 14:14:42 +02003088int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003089 RtcpMode method = _rtpRtcpModule->RTCP();
3090 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003091 return 0;
3092 }
3093 std::vector<RTCPReportBlock> report_blocks;
3094 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003095
3096 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003097 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003098 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003099 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003100 Channel* channel = associate_send_channel_.channel();
3101 // Tries to get RTT from an associated channel. This is important for
3102 // receive-only channels.
3103 if (channel) {
3104 // To prevent infinite recursion and deadlock, calling GetRTT of
3105 // associate channel should always use "false" for argument:
3106 // |allow_associate_channel|.
3107 rtt = channel->GetRTT(false);
3108 }
3109 }
3110 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003111 }
3112
3113 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3114 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3115 for (; it != report_blocks.end(); ++it) {
3116 if (it->remoteSSRC == remoteSSRC)
3117 break;
3118 }
3119 if (it == report_blocks.end()) {
3120 // We have not received packets with SSRC matching the report blocks.
3121 // To calculate RTT we try with the SSRC of the first report block.
3122 // This is very important for send-only channels where we don't know
3123 // the SSRC of the other end.
3124 remoteSSRC = report_blocks[0].remoteSSRC;
3125 }
Minyue2013aec2015-05-13 14:14:42 +02003126
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003127 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003128 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003129 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003130 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3131 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003132 return 0;
3133 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003134 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003135}
3136
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003137} // namespace voe
3138} // namespace webrtc