blob: 1d73db6d396a29ab1bc3ca12beee0c8ce1106ae0 [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"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010024#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000025#include "webrtc/base/timeutils.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020026#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070027#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070028#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000029#include "webrtc/modules/audio_device/include/audio_device.h"
30#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010031#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010032#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010033#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
34#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
35#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
nisse657bab22017-02-21 06:28:10 -080036#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000037#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010038#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010039#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000040#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
41#include "webrtc/voice_engine/output_mixer.h"
42#include "webrtc/voice_engine/statistics.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000043#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000044
andrew@webrtc.org50419b02012-11-14 19:07:54 +000045namespace webrtc {
46namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000047
kwibergc8d071e2016-04-06 12:22:38 -070048namespace {
49
Erik Språng737336d2016-07-29 12:59:36 +020050constexpr int64_t kMaxRetransmissionWindowMs = 1000;
51constexpr int64_t kMinRetransmissionWindowMs = 30;
52
kwibergc8d071e2016-04-06 12:22:38 -070053} // namespace
54
solenberg8842c3e2016-03-11 03:06:41 -080055const int kTelephoneEventAttenuationdB = 10;
56
ivoc14d5dbe2016-07-04 07:06:55 -070057class RtcEventLogProxy final : public webrtc::RtcEventLog {
58 public:
59 RtcEventLogProxy() : event_log_(nullptr) {}
60
61 bool StartLogging(const std::string& file_name,
62 int64_t max_size_bytes) override {
63 RTC_NOTREACHED();
64 return false;
65 }
66
67 bool StartLogging(rtc::PlatformFile log_file,
68 int64_t max_size_bytes) override {
69 RTC_NOTREACHED();
70 return false;
71 }
72
73 void StopLogging() override { RTC_NOTREACHED(); }
74
75 void LogVideoReceiveStreamConfig(
76 const webrtc::VideoReceiveStream::Config& config) override {
77 rtc::CritScope lock(&crit_);
78 if (event_log_) {
79 event_log_->LogVideoReceiveStreamConfig(config);
80 }
81 }
82
83 void LogVideoSendStreamConfig(
84 const webrtc::VideoSendStream::Config& config) override {
85 rtc::CritScope lock(&crit_);
86 if (event_log_) {
87 event_log_->LogVideoSendStreamConfig(config);
88 }
89 }
90
ivoce0928d82016-10-10 05:12:51 -070091 void LogAudioReceiveStreamConfig(
92 const webrtc::AudioReceiveStream::Config& config) override {
93 rtc::CritScope lock(&crit_);
94 if (event_log_) {
95 event_log_->LogAudioReceiveStreamConfig(config);
96 }
97 }
98
99 void LogAudioSendStreamConfig(
100 const webrtc::AudioSendStream::Config& config) override {
101 rtc::CritScope lock(&crit_);
102 if (event_log_) {
103 event_log_->LogAudioSendStreamConfig(config);
104 }
105 }
106
ivoc14d5dbe2016-07-04 07:06:55 -0700107 void LogRtpHeader(webrtc::PacketDirection direction,
108 webrtc::MediaType media_type,
109 const uint8_t* header,
110 size_t packet_length) override {
philipel32d00102017-02-27 02:18:46 -0800111 LogRtpHeader(direction, media_type, header, packet_length,
112 PacedPacketInfo::kNotAProbe);
113 }
114
115 void LogRtpHeader(webrtc::PacketDirection direction,
116 webrtc::MediaType media_type,
117 const uint8_t* header,
118 size_t packet_length,
119 int probe_cluster_id) override {
ivoc14d5dbe2016-07-04 07:06:55 -0700120 rtc::CritScope lock(&crit_);
121 if (event_log_) {
philipel32d00102017-02-27 02:18:46 -0800122 event_log_->LogRtpHeader(direction, media_type, header, packet_length,
123 probe_cluster_id);
ivoc14d5dbe2016-07-04 07:06:55 -0700124 }
125 }
126
127 void LogRtcpPacket(webrtc::PacketDirection direction,
128 webrtc::MediaType media_type,
129 const uint8_t* packet,
130 size_t length) override {
131 rtc::CritScope lock(&crit_);
132 if (event_log_) {
133 event_log_->LogRtcpPacket(direction, media_type, packet, length);
134 }
135 }
136
137 void LogAudioPlayout(uint32_t ssrc) override {
138 rtc::CritScope lock(&crit_);
139 if (event_log_) {
140 event_log_->LogAudioPlayout(ssrc);
141 }
142 }
143
terelius424e6cf2017-02-20 05:14:41 -0800144 void LogLossBasedBweUpdate(int32_t bitrate_bps,
ivoc14d5dbe2016-07-04 07:06:55 -0700145 uint8_t fraction_loss,
146 int32_t total_packets) override {
147 rtc::CritScope lock(&crit_);
148 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800149 event_log_->LogLossBasedBweUpdate(bitrate_bps, fraction_loss,
150 total_packets);
ivoc14d5dbe2016-07-04 07:06:55 -0700151 }
152 }
153
terelius424e6cf2017-02-20 05:14:41 -0800154 void LogDelayBasedBweUpdate(int32_t bitrate_bps,
terelius0baf55d2017-02-17 03:38:28 -0800155 BandwidthUsage detector_state) override {
156 rtc::CritScope lock(&crit_);
157 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800158 event_log_->LogDelayBasedBweUpdate(bitrate_bps, detector_state);
terelius0baf55d2017-02-17 03:38:28 -0800159 }
160 }
161
minyue4b7c9522017-01-24 04:54:59 -0800162 void LogAudioNetworkAdaptation(
163 const AudioNetworkAdaptor::EncoderRuntimeConfig& config) override {
164 rtc::CritScope lock(&crit_);
165 if (event_log_) {
166 event_log_->LogAudioNetworkAdaptation(config);
167 }
168 }
169
philipel32d00102017-02-27 02:18:46 -0800170 void LogProbeClusterCreated(int id,
171 int bitrate_bps,
172 int min_probes,
173 int min_bytes) override {
174 rtc::CritScope lock(&crit_);
175 if (event_log_) {
176 event_log_->LogProbeClusterCreated(id, bitrate_bps, min_probes,
177 min_bytes);
178 }
179 };
180
181 void LogProbeResultSuccess(int id, int bitrate_bps) override {
182 rtc::CritScope lock(&crit_);
183 if (event_log_) {
184 event_log_->LogProbeResultSuccess(id, bitrate_bps);
185 }
186 };
187
188 void LogProbeResultFailure(int id,
189 ProbeFailureReason failure_reason) override {
190 rtc::CritScope lock(&crit_);
191 if (event_log_) {
192 event_log_->LogProbeResultFailure(id, failure_reason);
193 }
194 };
195
ivoc14d5dbe2016-07-04 07:06:55 -0700196 void SetEventLog(RtcEventLog* event_log) {
197 rtc::CritScope lock(&crit_);
198 event_log_ = event_log;
199 }
200
201 private:
202 rtc::CriticalSection crit_;
203 RtcEventLog* event_log_ GUARDED_BY(crit_);
204 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
205};
206
michaelt9332b7d2016-11-30 07:51:13 -0800207class RtcpRttStatsProxy final : public RtcpRttStats {
208 public:
209 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
210
211 void OnRttUpdate(int64_t rtt) override {
212 rtc::CritScope lock(&crit_);
213 if (rtcp_rtt_stats_)
214 rtcp_rtt_stats_->OnRttUpdate(rtt);
215 }
216
217 int64_t LastProcessedRtt() const override {
218 rtc::CritScope lock(&crit_);
219 if (!rtcp_rtt_stats_)
220 return 0;
221 return rtcp_rtt_stats_->LastProcessedRtt();
222 }
223
224 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
225 rtc::CritScope lock(&crit_);
226 rtcp_rtt_stats_ = rtcp_rtt_stats;
227 }
228
229 private:
230 rtc::CriticalSection crit_;
231 RtcpRttStats* rtcp_rtt_stats_ GUARDED_BY(crit_);
232 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
233};
234
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100235class TransportFeedbackProxy : public TransportFeedbackObserver {
236 public:
237 TransportFeedbackProxy() : feedback_observer_(nullptr) {
238 pacer_thread_.DetachFromThread();
239 network_thread_.DetachFromThread();
240 }
241
242 void SetTransportFeedbackObserver(
243 TransportFeedbackObserver* feedback_observer) {
244 RTC_DCHECK(thread_checker_.CalledOnValidThread());
245 rtc::CritScope lock(&crit_);
246 feedback_observer_ = feedback_observer;
247 }
248
249 // Implements TransportFeedbackObserver.
250 void AddPacket(uint16_t sequence_number,
251 size_t length,
philipel8aadd502017-02-23 02:56:13 -0800252 const PacedPacketInfo& pacing_info) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100253 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
254 rtc::CritScope lock(&crit_);
255 if (feedback_observer_)
philipel8aadd502017-02-23 02:56:13 -0800256 feedback_observer_->AddPacket(sequence_number, length, pacing_info);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100257 }
philipel8aadd502017-02-23 02:56:13 -0800258
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100259 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
260 RTC_DCHECK(network_thread_.CalledOnValidThread());
261 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700262 if (feedback_observer_)
263 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200264 }
elad.alonf9490002017-03-06 05:32:21 -0800265 std::vector<PacketFeedback> GetTransportFeedbackVector() const override {
Stefan Holmer60e43462016-09-07 09:58:20 +0200266 RTC_NOTREACHED();
elad.alonf9490002017-03-06 05:32:21 -0800267 return std::vector<PacketFeedback>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100268 }
269
270 private:
271 rtc::CriticalSection crit_;
272 rtc::ThreadChecker thread_checker_;
273 rtc::ThreadChecker pacer_thread_;
274 rtc::ThreadChecker network_thread_;
275 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
276};
277
278class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
279 public:
280 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
281 pacer_thread_.DetachFromThread();
282 }
283
284 void SetSequenceNumberAllocator(
285 TransportSequenceNumberAllocator* seq_num_allocator) {
286 RTC_DCHECK(thread_checker_.CalledOnValidThread());
287 rtc::CritScope lock(&crit_);
288 seq_num_allocator_ = seq_num_allocator;
289 }
290
291 // Implements TransportSequenceNumberAllocator.
292 uint16_t AllocateSequenceNumber() override {
293 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
294 rtc::CritScope lock(&crit_);
295 if (!seq_num_allocator_)
296 return 0;
297 return seq_num_allocator_->AllocateSequenceNumber();
298 }
299
300 private:
301 rtc::CriticalSection crit_;
302 rtc::ThreadChecker thread_checker_;
303 rtc::ThreadChecker pacer_thread_;
304 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
305};
306
307class RtpPacketSenderProxy : public RtpPacketSender {
308 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800309 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100310
311 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
312 RTC_DCHECK(thread_checker_.CalledOnValidThread());
313 rtc::CritScope lock(&crit_);
314 rtp_packet_sender_ = rtp_packet_sender;
315 }
316
317 // Implements RtpPacketSender.
318 void InsertPacket(Priority priority,
319 uint32_t ssrc,
320 uint16_t sequence_number,
321 int64_t capture_time_ms,
322 size_t bytes,
323 bool retransmission) override {
324 rtc::CritScope lock(&crit_);
325 if (rtp_packet_sender_) {
326 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
327 capture_time_ms, bytes, retransmission);
328 }
329 }
330
331 private:
332 rtc::ThreadChecker thread_checker_;
333 rtc::CriticalSection crit_;
334 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
335};
336
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000337class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000338 public:
stefan7de8d642017-02-07 07:14:08 -0800339 explicit VoERtcpObserver(Channel* owner)
340 : owner_(owner), bandwidth_observer_(nullptr) {}
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000341 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000342
stefan7de8d642017-02-07 07:14:08 -0800343 void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
344 rtc::CritScope lock(&crit_);
345 bandwidth_observer_ = bandwidth_observer;
346 }
347
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000348 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
stefan7de8d642017-02-07 07:14:08 -0800349 rtc::CritScope lock(&crit_);
350 if (bandwidth_observer_) {
351 bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
352 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000353 }
354
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000355 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
356 int64_t rtt,
357 int64_t now_ms) override {
stefan7de8d642017-02-07 07:14:08 -0800358 {
359 rtc::CritScope lock(&crit_);
360 if (bandwidth_observer_) {
361 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
362 now_ms);
363 }
364 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000365 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
366 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
367 // report for VoiceEngine?
368 if (report_blocks.empty())
369 return;
370
371 int fraction_lost_aggregate = 0;
372 int total_number_of_packets = 0;
373
374 // If receiving multiple report blocks, calculate the weighted average based
375 // on the number of packets a report refers to.
376 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
377 block_it != report_blocks.end(); ++block_it) {
378 // Find the previous extended high sequence number for this remote SSRC,
379 // to calculate the number of RTP packets this report refers to. Ignore if
380 // we haven't seen this SSRC before.
381 std::map<uint32_t, uint32_t>::iterator seq_num_it =
382 extended_max_sequence_number_.find(block_it->sourceSSRC);
383 int number_of_packets = 0;
384 if (seq_num_it != extended_max_sequence_number_.end()) {
385 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
386 }
387 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
388 total_number_of_packets += number_of_packets;
389
390 extended_max_sequence_number_[block_it->sourceSSRC] =
391 block_it->extendedHighSeqNum;
392 }
393 int weighted_fraction_lost = 0;
394 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800395 weighted_fraction_lost =
396 (fraction_lost_aggregate + total_number_of_packets / 2) /
397 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000398 }
399 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000400 }
401
402 private:
403 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000404 // Maps remote side ssrc to extended highest sequence number received.
405 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800406 rtc::CriticalSection crit_;
407 RtcpBandwidthObserver* bandwidth_observer_ GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000408};
409
kwiberg55b97fe2016-01-28 05:22:45 -0800410int32_t Channel::SendData(FrameType frameType,
411 uint8_t payloadType,
412 uint32_t timeStamp,
413 const uint8_t* payloadData,
414 size_t payloadSize,
415 const RTPFragmentationHeader* fragmentation) {
416 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
417 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
418 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
419 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000420
kwiberg55b97fe2016-01-28 05:22:45 -0800421 if (_includeAudioLevelIndication) {
422 // Store current audio level in the RTP/RTCP module.
423 // The level will be used in combination with voice-activity state
424 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800425 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800426 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000427
kwiberg55b97fe2016-01-28 05:22:45 -0800428 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
429 // packetization.
430 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700431 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800432 (FrameType&)frameType, payloadType, timeStamp,
433 // Leaving the time when this frame was
434 // received from the capture device as
435 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700436 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800437 _engineStatisticsPtr->SetLastError(
438 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
439 "Channel::SendData() failed to send data to RTP/RTCP module");
440 return -1;
441 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000442
kwiberg55b97fe2016-01-28 05:22:45 -0800443 _lastLocalTimeStamp = timeStamp;
444 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000445
kwiberg55b97fe2016-01-28 05:22:45 -0800446 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000447}
448
stefan1d8a5062015-10-02 03:39:33 -0700449bool Channel::SendRtp(const uint8_t* data,
450 size_t len,
451 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800452 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
453 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000454
kwiberg55b97fe2016-01-28 05:22:45 -0800455 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000456
kwiberg55b97fe2016-01-28 05:22:45 -0800457 if (_transportPtr == NULL) {
458 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
459 "Channel::SendPacket() failed to send RTP packet due to"
460 " invalid transport object");
461 return false;
462 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000463
kwiberg55b97fe2016-01-28 05:22:45 -0800464 uint8_t* bufferToSendPtr = (uint8_t*)data;
465 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000466
kwiberg55b97fe2016-01-28 05:22:45 -0800467 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
468 std::string transport_name =
469 _externalTransport ? "external transport" : "WebRtc sockets";
470 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
471 "Channel::SendPacket() RTP transmission using %s failed",
472 transport_name.c_str());
473 return false;
474 }
475 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000476}
477
kwiberg55b97fe2016-01-28 05:22:45 -0800478bool Channel::SendRtcp(const uint8_t* data, size_t len) {
479 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
480 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000481
kwiberg55b97fe2016-01-28 05:22:45 -0800482 rtc::CritScope cs(&_callbackCritSect);
483 if (_transportPtr == NULL) {
484 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
485 "Channel::SendRtcp() failed to send RTCP packet"
486 " due to invalid transport object");
487 return false;
488 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000489
kwiberg55b97fe2016-01-28 05:22:45 -0800490 uint8_t* bufferToSendPtr = (uint8_t*)data;
491 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
kwiberg55b97fe2016-01-28 05:22:45 -0800493 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
494 if (n < 0) {
495 std::string transport_name =
496 _externalTransport ? "external transport" : "WebRtc sockets";
497 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
498 "Channel::SendRtcp() transmission using %s failed",
499 transport_name.c_str());
500 return false;
501 }
502 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000503}
504
kwiberg55b97fe2016-01-28 05:22:45 -0800505void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
506 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
507 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000508
kwiberg55b97fe2016-01-28 05:22:45 -0800509 // Update ssrc so that NTP for AV sync can be updated.
510 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000511}
512
Peter Boströmac547a62015-09-17 23:03:57 +0200513void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
514 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
515 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
516 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000517}
518
Peter Boströmac547a62015-09-17 23:03:57 +0200519int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000520 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000521 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000522 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800523 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200524 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800525 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
526 "Channel::OnInitializeDecoder(payloadType=%d, "
527 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
528 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000529
kwiberg55b97fe2016-01-28 05:22:45 -0800530 CodecInst receiveCodec = {0};
531 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000532
kwiberg55b97fe2016-01-28 05:22:45 -0800533 receiveCodec.pltype = payloadType;
534 receiveCodec.plfreq = frequency;
535 receiveCodec.channels = channels;
536 receiveCodec.rate = rate;
537 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000538
kwiberg55b97fe2016-01-28 05:22:45 -0800539 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
540 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000541
kwiberg55b97fe2016-01-28 05:22:45 -0800542 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700543 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
544 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800545 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
546 "Channel::OnInitializeDecoder() invalid codec ("
547 "pt=%d, name=%s) received - 1",
548 payloadType, payloadName);
549 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
550 return -1;
551 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000552
kwiberg55b97fe2016-01-28 05:22:45 -0800553 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000554}
555
kwiberg55b97fe2016-01-28 05:22:45 -0800556int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
557 size_t payloadSize,
558 const WebRtcRTPHeader* rtpHeader) {
559 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
560 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
561 ","
562 " payloadType=%u, audioChannel=%" PRIuS ")",
563 payloadSize, rtpHeader->header.payloadType,
564 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000565
kwiberg55b97fe2016-01-28 05:22:45 -0800566 if (!channel_state_.Get().playing) {
567 // Avoid inserting into NetEQ when we are not playing. Count the
568 // packet as discarded.
569 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
570 "received packet is discarded since playing is not"
571 " activated");
niklase@google.com470e71d2011-07-07 08:21:25 +0000572 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800573 }
574
575 // Push the incoming payload (parsed and ready for decoding) into the ACM
576 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
577 0) {
578 _engineStatisticsPtr->SetLastError(
579 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
580 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
581 return -1;
582 }
583
kwiberg55b97fe2016-01-28 05:22:45 -0800584 int64_t round_trip_time = 0;
585 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
586 NULL);
587
588 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
589 if (!nack_list.empty()) {
590 // Can't use nack_list.data() since it's not supported by all
591 // compilers.
592 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
593 }
594 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000595}
596
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000597bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000598 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000599 RTPHeader header;
600 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
601 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
602 "IncomingPacket invalid RTP header");
603 return false;
604 }
605 header.payload_type_frequency =
606 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
607 if (header.payload_type_frequency < 0)
608 return false;
609 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
610}
611
henrik.lundin42dda502016-05-18 05:36:01 -0700612MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
613 int32_t id,
614 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700615 unsigned int ssrc;
nisse7d59f6b2017-02-21 03:40:24 -0800616 RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
ivoc14d5dbe2016-07-04 07:06:55 -0700617 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800618 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700619 bool muted;
620 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
621 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800622 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
623 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
624 // In all likelihood, the audio in this frame is garbage. We return an
625 // error so that the audio mixer module doesn't add it to the mix. As
626 // a result, it won't be played out and the actions skipped here are
627 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700628 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800629 }
henrik.lundina89ab962016-05-18 08:52:45 -0700630
631 if (muted) {
632 // TODO(henrik.lundin): We should be able to do better than this. But we
633 // will have to go through all the cases below where the audio samples may
634 // be used, and handle the muted case in some way.
aleloi6321b492016-12-05 01:46:09 -0800635 AudioFrameOperations::Mute(audioFrame);
henrik.lundina89ab962016-05-18 08:52:45 -0700636 }
kwiberg55b97fe2016-01-28 05:22:45 -0800637
kwiberg55b97fe2016-01-28 05:22:45 -0800638 // Convert module ID to internal VoE channel ID
639 audioFrame->id_ = VoEChannelId(audioFrame->id_);
640 // Store speech type for dead-or-alive detection
641 _outputSpeechType = audioFrame->speech_type_;
642
643 ChannelState::State state = channel_state_.Get();
644
kwiberg55b97fe2016-01-28 05:22:45 -0800645 {
646 // Pass the audio buffers to an optional sink callback, before applying
647 // scaling/panning, as that applies to the mix operation.
648 // External recipients of the audio (e.g. via AudioTrack), will do their
649 // own mixing/dynamic processing.
650 rtc::CritScope cs(&_callbackCritSect);
651 if (audio_sink_) {
652 AudioSinkInterface::Data data(
653 &audioFrame->data_[0], audioFrame->samples_per_channel_,
654 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
655 audioFrame->timestamp_);
656 audio_sink_->OnData(data);
657 }
658 }
659
660 float output_gain = 1.0f;
kwiberg55b97fe2016-01-28 05:22:45 -0800661 {
662 rtc::CritScope cs(&volume_settings_critsect_);
663 output_gain = _outputGain;
kwiberg55b97fe2016-01-28 05:22:45 -0800664 }
665
666 // Output volume scaling
667 if (output_gain < 0.99f || output_gain > 1.01f) {
solenberg8d73f8c2017-03-08 01:52:20 -0800668 // TODO(solenberg): Combine with mute state - this can cause clicks!
oprypin67fdb802017-03-09 06:25:06 -0800669 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800670 }
671
kwiberg55b97fe2016-01-28 05:22:45 -0800672 // Mix decoded PCM output with file if file mixing is enabled
673 if (state.output_file_playing) {
674 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700675 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800676 }
677
kwiberg55b97fe2016-01-28 05:22:45 -0800678 // Record playout if enabled
679 {
680 rtc::CritScope cs(&_fileCritSect);
681
kwiberg5a25d952016-08-17 07:31:12 -0700682 if (_outputFileRecording && output_file_recorder_) {
683 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800684 }
685 }
686
687 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700688 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800689 _outputAudioLevel.ComputeLevel(*audioFrame);
690
691 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
692 // The first frame with a valid rtp timestamp.
693 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
694 }
695
696 if (capture_start_rtp_time_stamp_ >= 0) {
697 // audioFrame.timestamp_ should be valid from now on.
698
699 // Compute elapsed time.
700 int64_t unwrap_timestamp =
701 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
702 audioFrame->elapsed_time_ms_ =
703 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700704 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800705
niklase@google.com470e71d2011-07-07 08:21:25 +0000706 {
kwiberg55b97fe2016-01-28 05:22:45 -0800707 rtc::CritScope lock(&ts_stats_lock_);
708 // Compute ntp time.
709 audioFrame->ntp_time_ms_ =
710 ntp_estimator_.Estimate(audioFrame->timestamp_);
711 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
712 if (audioFrame->ntp_time_ms_ > 0) {
713 // Compute |capture_start_ntp_time_ms_| so that
714 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
715 capture_start_ntp_time_ms_ =
716 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000717 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000718 }
kwiberg55b97fe2016-01-28 05:22:45 -0800719 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000720
henrik.lundin42dda502016-05-18 05:36:01 -0700721 return muted ? MixerParticipant::AudioFrameInfo::kMuted
722 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000723}
724
aleloi6c278492016-10-20 14:24:39 -0700725AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
726 int sample_rate_hz,
727 AudioFrame* audio_frame) {
728 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700729
aleloi6c278492016-10-20 14:24:39 -0700730 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700731
732 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
733 FrameInfo new_audio_frame_info = FrameInfo::kError;
734 switch (frame_info) {
735 case MixerParticipant::AudioFrameInfo::kNormal:
736 new_audio_frame_info = FrameInfo::kNormal;
737 break;
738 case MixerParticipant::AudioFrameInfo::kMuted:
739 new_audio_frame_info = FrameInfo::kMuted;
740 break;
741 case MixerParticipant::AudioFrameInfo::kError:
742 new_audio_frame_info = FrameInfo::kError;
743 break;
744 }
aleloi6c278492016-10-20 14:24:39 -0700745 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700746}
747
kwiberg55b97fe2016-01-28 05:22:45 -0800748int32_t Channel::NeededFrequency(int32_t id) const {
749 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
750 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000751
kwiberg55b97fe2016-01-28 05:22:45 -0800752 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000753
kwiberg55b97fe2016-01-28 05:22:45 -0800754 // Determine highest needed receive frequency
755 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000756
kwiberg55b97fe2016-01-28 05:22:45 -0800757 // Return the bigger of playout and receive frequency in the ACM.
758 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
759 highestNeeded = audio_coding_->PlayoutFrequency();
760 } else {
761 highestNeeded = receiveFrequency;
762 }
763
764 // Special case, if we're playing a file on the playout side
765 // we take that frequency into consideration as well
766 // This is not needed on sending side, since the codec will
767 // limit the spectrum anyway.
768 if (channel_state_.Get().output_file_playing) {
769 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700770 if (output_file_player_) {
771 if (output_file_player_->Frequency() > highestNeeded) {
772 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800773 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000774 }
kwiberg55b97fe2016-01-28 05:22:45 -0800775 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000776
kwiberg55b97fe2016-01-28 05:22:45 -0800777 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000778}
779
ossu5f7cfa52016-05-30 08:11:28 -0700780int32_t Channel::CreateChannel(
781 Channel*& channel,
782 int32_t channelId,
783 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700784 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800785 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
786 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
787 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000788
solenberg88499ec2016-09-07 07:34:41 -0700789 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800790 if (channel == NULL) {
791 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
792 "Channel::CreateChannel() unable to allocate memory for"
793 " channel");
794 return -1;
795 }
796 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000797}
798
kwiberg55b97fe2016-01-28 05:22:45 -0800799void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
800 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
801 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
802 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000803
kwiberg55b97fe2016-01-28 05:22:45 -0800804 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000805}
806
kwiberg55b97fe2016-01-28 05:22:45 -0800807void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
808 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
809 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
810 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000811
kwiberg55b97fe2016-01-28 05:22:45 -0800812 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000813}
814
kwiberg55b97fe2016-01-28 05:22:45 -0800815void Channel::PlayFileEnded(int32_t id) {
816 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
817 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000818
kwiberg55b97fe2016-01-28 05:22:45 -0800819 if (id == _inputFilePlayerId) {
820 channel_state_.SetInputFilePlaying(false);
821 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
822 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000823 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800824 } else if (id == _outputFilePlayerId) {
825 channel_state_.SetOutputFilePlaying(false);
826 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
827 "Channel::PlayFileEnded() => output file player module is"
828 " shutdown");
829 }
830}
831
832void Channel::RecordFileEnded(int32_t id) {
833 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
834 "Channel::RecordFileEnded(id=%d)", id);
835
836 assert(id == _outputFileRecorderId);
837
838 rtc::CritScope cs(&_fileCritSect);
839
840 _outputFileRecording = false;
841 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
842 "Channel::RecordFileEnded() => output file recorder module is"
843 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000844}
845
pbos@webrtc.org92135212013-05-14 08:31:39 +0000846Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000847 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700848 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800849 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100850 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700851 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800852 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100853 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800854 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100855 rtp_receive_statistics_(
856 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
857 rtp_receiver_(
858 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100859 this,
860 this,
861 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700862 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100863 _outputAudioLevel(),
864 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100865 // Avoid conflict with other channels by adding 1024 - 1026,
866 // won't use as much as 1024 channels.
867 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
868 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
869 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
870 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100871 _timeStamp(0), // This is just an offset, RTP module will add it's own
872 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100873 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100874 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100875 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100876 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100877 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
878 capture_start_rtp_time_stamp_(-1),
879 capture_start_ntp_time_ms_(-1),
880 _engineStatisticsPtr(NULL),
881 _outputMixerPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100882 _moduleProcessThreadPtr(NULL),
883 _audioDeviceModulePtr(NULL),
884 _voiceEngineObserverPtr(NULL),
885 _callbackCritSectPtr(NULL),
886 _transportPtr(NULL),
solenberg1c2af8e2016-03-24 10:36:00 -0700887 input_mute_(false),
888 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100889 _outputGain(1.0f),
solenberg8d73f8c2017-03-08 01:52:20 -0800890 _mixFileWithMicrophone(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100891 _lastLocalTimeStamp(0),
892 _lastPayloadType(0),
893 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800894 transport_overhead_per_packet_(0),
895 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100896 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100897 restored_packet_in_use_(false),
898 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100899 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700900 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800901 feedback_observer_proxy_(new TransportFeedbackProxy()),
902 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700903 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200904 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
905 kMaxRetransmissionWindowMs)),
michaelt566d8202017-01-12 10:17:38 -0800906 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800907 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
908 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700909 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800910 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700911 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800912 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200913
kwiberg55b97fe2016-01-28 05:22:45 -0800914 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000915
kwiberg55b97fe2016-01-28 05:22:45 -0800916 RtpRtcp::Configuration configuration;
917 configuration.audio = true;
918 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800919 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800920 configuration.receive_statistics = rtp_receive_statistics_.get();
921 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800922 if (pacing_enabled_) {
923 configuration.paced_sender = rtp_packet_sender_proxy_.get();
924 configuration.transport_sequence_number_allocator =
925 seq_num_allocator_proxy_.get();
926 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
927 }
ivoc14d5dbe2016-07-04 07:06:55 -0700928 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800929 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200930 configuration.retransmission_rate_limiter =
931 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000932
kwiberg55b97fe2016-01-28 05:22:45 -0800933 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100934 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000935}
936
kwiberg55b97fe2016-01-28 05:22:45 -0800937Channel::~Channel() {
938 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
939 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
940 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000941
kwiberg55b97fe2016-01-28 05:22:45 -0800942 StopSend();
943 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000944
kwiberg55b97fe2016-01-28 05:22:45 -0800945 {
946 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700947 if (input_file_player_) {
948 input_file_player_->RegisterModuleFileCallback(NULL);
949 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000950 }
kwiberg5a25d952016-08-17 07:31:12 -0700951 if (output_file_player_) {
952 output_file_player_->RegisterModuleFileCallback(NULL);
953 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800954 }
kwiberg5a25d952016-08-17 07:31:12 -0700955 if (output_file_recorder_) {
956 output_file_recorder_->RegisterModuleFileCallback(NULL);
957 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800958 }
959 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000960
kwiberg55b97fe2016-01-28 05:22:45 -0800961 // The order to safely shutdown modules in a channel is:
962 // 1. De-register callbacks in modules
963 // 2. De-register modules in process thread
964 // 3. Destroy modules
965 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
966 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
967 "~Channel() failed to de-register transport callback"
968 " (Audio coding module)");
969 }
970 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
971 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
972 "~Channel() failed to de-register VAD callback"
973 " (Audio coding module)");
974 }
975 // De-register modules in process thread
976 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000977
kwiberg55b97fe2016-01-28 05:22:45 -0800978 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000979}
980
kwiberg55b97fe2016-01-28 05:22:45 -0800981int32_t Channel::Init() {
982 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
983 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000984
kwiberg55b97fe2016-01-28 05:22:45 -0800985 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000986
kwiberg55b97fe2016-01-28 05:22:45 -0800987 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000988
kwiberg55b97fe2016-01-28 05:22:45 -0800989 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
990 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
991 "Channel::Init() must call SetEngineInformation() first");
992 return -1;
993 }
994
995 // --- Add modules to process thread (for periodic schedulation)
996
tommidea489f2017-03-03 03:20:24 -0800997 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
kwiberg55b97fe2016-01-28 05:22:45 -0800998
999 // --- ACM initialization
1000
1001 if (audio_coding_->InitializeReceiver() == -1) {
1002 _engineStatisticsPtr->SetLastError(
1003 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1004 "Channel::Init() unable to initialize the ACM - 1");
1005 return -1;
1006 }
1007
1008 // --- RTP/RTCP module initialization
1009
1010 // Ensure that RTCP is enabled by default for the created channel.
1011 // Note that, the module will keep generating RTCP until it is explicitly
1012 // disabled by the user.
1013 // After StopListen (when no sockets exists), RTCP packets will no longer
1014 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001015 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001016 // RTCP is enabled by default.
1017 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1018 // --- Register all permanent callbacks
solenbergfe7dd6d2017-03-11 08:10:43 -08001019 if (audio_coding_->RegisterTransportCallback(this) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001020 _engineStatisticsPtr->SetLastError(
1021 VE_CANNOT_INIT_CHANNEL, kTraceError,
1022 "Channel::Init() callbacks not registered");
1023 return -1;
1024 }
1025
1026 // --- Register all supported codecs to the receiving side of the
1027 // RTP/RTCP module
1028
1029 CodecInst codec;
1030 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1031
1032 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1033 // Open up the RTP/RTCP receiver for all supported codecs
1034 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08001035 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001036 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1037 "Channel::Init() unable to register %s "
1038 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1039 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1040 codec.rate);
1041 } else {
1042 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1043 "Channel::Init() %s (%d/%d/%" PRIuS
1044 "/%d) has been "
1045 "added to the RTP/RTCP receiver",
1046 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1047 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001048 }
1049
kwiberg55b97fe2016-01-28 05:22:45 -08001050 // Ensure that PCMU is used as default codec on the sending side
1051 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1052 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001053 }
1054
kwiberg55b97fe2016-01-28 05:22:45 -08001055 // Register default PT for outband 'telephone-event'
1056 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001057 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001058 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1059 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001060 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1061 "Channel::Init() failed to register outband "
1062 "'telephone-event' (%d/%d) correctly",
1063 codec.pltype, codec.plfreq);
1064 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001065 }
1066
kwiberg55b97fe2016-01-28 05:22:45 -08001067 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001068 if (!codec_manager_.RegisterEncoder(codec) ||
1069 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001070 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1071 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001072 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001073 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1074 "Channel::Init() failed to register CN (%d/%d) "
1075 "correctly - 1",
1076 codec.pltype, codec.plfreq);
1077 }
1078 }
kwiberg55b97fe2016-01-28 05:22:45 -08001079 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001080
kwiberg55b97fe2016-01-28 05:22:45 -08001081 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001082}
1083
kwiberg55b97fe2016-01-28 05:22:45 -08001084int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1085 OutputMixer& outputMixer,
kwiberg55b97fe2016-01-28 05:22:45 -08001086 ProcessThread& moduleProcessThread,
1087 AudioDeviceModule& audioDeviceModule,
1088 VoiceEngineObserver* voiceEngineObserver,
1089 rtc::CriticalSection* callbackCritSect) {
1090 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1091 "Channel::SetEngineInformation()");
1092 _engineStatisticsPtr = &engineStatistics;
1093 _outputMixerPtr = &outputMixer;
kwiberg55b97fe2016-01-28 05:22:45 -08001094 _moduleProcessThreadPtr = &moduleProcessThread;
1095 _audioDeviceModulePtr = &audioDeviceModule;
1096 _voiceEngineObserverPtr = voiceEngineObserver;
1097 _callbackCritSectPtr = callbackCritSect;
1098 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001099}
1100
kwiberg55b97fe2016-01-28 05:22:45 -08001101int32_t Channel::UpdateLocalTimeStamp() {
1102 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1103 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001104}
1105
kwibergb7f89d62016-02-17 10:04:18 -08001106void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001107 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001108 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001109}
1110
ossu29b1a8d2016-06-13 07:34:51 -07001111const rtc::scoped_refptr<AudioDecoderFactory>&
1112Channel::GetAudioDecoderFactory() const {
1113 return decoder_factory_;
1114}
1115
kwiberg55b97fe2016-01-28 05:22:45 -08001116int32_t Channel::StartPlayout() {
1117 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1118 "Channel::StartPlayout()");
1119 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001120 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001121 }
1122
solenberge374e012017-02-14 04:55:00 -08001123 // Add participant as candidates for mixing.
1124 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1125 _engineStatisticsPtr->SetLastError(
1126 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1127 "StartPlayout() failed to add participant to mixer");
1128 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001129 }
1130
1131 channel_state_.SetPlaying(true);
1132 if (RegisterFilePlayingToMixer() != 0)
1133 return -1;
1134
1135 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001136}
1137
kwiberg55b97fe2016-01-28 05:22:45 -08001138int32_t Channel::StopPlayout() {
1139 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1140 "Channel::StopPlayout()");
1141 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001142 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001143 }
1144
solenberge374e012017-02-14 04:55:00 -08001145 // Remove participant as candidates for mixing
1146 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1147 _engineStatisticsPtr->SetLastError(
1148 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1149 "StopPlayout() failed to remove participant from mixer");
1150 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001151 }
1152
1153 channel_state_.SetPlaying(false);
1154 _outputAudioLevel.Clear();
1155
1156 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001157}
1158
kwiberg55b97fe2016-01-28 05:22:45 -08001159int32_t Channel::StartSend() {
1160 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1161 "Channel::StartSend()");
kwiberg55b97fe2016-01-28 05:22:45 -08001162 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001163 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001164 }
1165 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001166
solenberg08b19df2017-02-15 00:42:31 -08001167 // Resume the previous sequence number which was reset by StopSend(). This
1168 // needs to be done before |sending| is set to true on the RTP/RTCP module.
1169 if (send_sequence_number_) {
1170 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
1171 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001172 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001173 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1174 _engineStatisticsPtr->SetLastError(
1175 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1176 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001177 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001178 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001179 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001180 return -1;
1181 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001182
kwiberg55b97fe2016-01-28 05:22:45 -08001183 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001184}
1185
kwiberg55b97fe2016-01-28 05:22:45 -08001186int32_t Channel::StopSend() {
1187 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1188 "Channel::StopSend()");
1189 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001190 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001191 }
1192 channel_state_.SetSending(false);
1193
1194 // Store the sequence number to be able to pick up the same sequence for
1195 // the next StartSend(). This is needed for restarting device, otherwise
1196 // it might cause libSRTP to complain about packets being replayed.
1197 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1198 // CL is landed. See issue
1199 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1200 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1201
1202 // Reset sending SSRC and sequence number and triggers direct transmission
1203 // of RTCP BYE
1204 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1205 _engineStatisticsPtr->SetLastError(
1206 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1207 "StartSend() RTP/RTCP failed to stop sending");
1208 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001209 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001210
1211 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001212}
1213
kwiberg55b97fe2016-01-28 05:22:45 -08001214int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1215 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1216 "Channel::RegisterVoiceEngineObserver()");
1217 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001218
kwiberg55b97fe2016-01-28 05:22:45 -08001219 if (_voiceEngineObserverPtr) {
1220 _engineStatisticsPtr->SetLastError(
1221 VE_INVALID_OPERATION, kTraceError,
1222 "RegisterVoiceEngineObserver() observer already enabled");
1223 return -1;
1224 }
1225 _voiceEngineObserverPtr = &observer;
1226 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001227}
1228
kwiberg55b97fe2016-01-28 05:22:45 -08001229int32_t Channel::DeRegisterVoiceEngineObserver() {
1230 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1231 "Channel::DeRegisterVoiceEngineObserver()");
1232 rtc::CritScope cs(&_callbackCritSect);
1233
1234 if (!_voiceEngineObserverPtr) {
1235 _engineStatisticsPtr->SetLastError(
1236 VE_INVALID_OPERATION, kTraceWarning,
1237 "DeRegisterVoiceEngineObserver() observer already disabled");
1238 return 0;
1239 }
1240 _voiceEngineObserverPtr = NULL;
1241 return 0;
1242}
1243
1244int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001245 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001246 if (send_codec) {
1247 codec = *send_codec;
1248 return 0;
1249 }
1250 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001251}
1252
kwiberg55b97fe2016-01-28 05:22:45 -08001253int32_t Channel::GetRecCodec(CodecInst& codec) {
1254 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001255}
1256
kwiberg55b97fe2016-01-28 05:22:45 -08001257int32_t Channel::SetSendCodec(const CodecInst& codec) {
1258 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1259 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001260
kwibergc8d071e2016-04-06 12:22:38 -07001261 if (!codec_manager_.RegisterEncoder(codec) ||
1262 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001263 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1264 "SetSendCodec() failed to register codec to ACM");
1265 return -1;
1266 }
1267
1268 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1269 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1270 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1271 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1272 "SetSendCodec() failed to register codec to"
1273 " RTP/RTCP module");
1274 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001275 }
kwiberg55b97fe2016-01-28 05:22:45 -08001276 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001277
kwiberg55b97fe2016-01-28 05:22:45 -08001278 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001279}
1280
minyue78b4d562016-11-30 04:47:39 -08001281void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001282 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1283 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001284 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001285 if (*encoder) {
1286 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001287 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001288 }
1289 });
michaelt566d8202017-01-12 10:17:38 -08001290 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001291}
1292
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001293void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001294 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1295 if (*encoder)
1296 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1297 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001298}
1299
kwiberg55b97fe2016-01-28 05:22:45 -08001300int32_t Channel::SetVADStatus(bool enableVAD,
1301 ACMVADMode mode,
1302 bool disableDTX) {
1303 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1304 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001305 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1306 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1307 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001308 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1309 kTraceError,
1310 "SetVADStatus() failed to set VAD");
1311 return -1;
1312 }
1313 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001314}
1315
kwiberg55b97fe2016-01-28 05:22:45 -08001316int32_t Channel::GetVADStatus(bool& enabledVAD,
1317 ACMVADMode& mode,
1318 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001319 const auto* params = codec_manager_.GetStackParams();
1320 enabledVAD = params->use_cng;
1321 mode = params->vad_mode;
1322 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001323 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001324}
1325
kwiberg55b97fe2016-01-28 05:22:45 -08001326int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001327 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1328}
1329
1330int32_t Channel::SetRecPayloadType(int payload_type,
1331 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001332 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1333 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001334
kwiberg55b97fe2016-01-28 05:22:45 -08001335 if (channel_state_.Get().playing) {
1336 _engineStatisticsPtr->SetLastError(
1337 VE_ALREADY_PLAYING, kTraceError,
1338 "SetRecPayloadType() unable to set PT while playing");
1339 return -1;
1340 }
kwiberg55b97fe2016-01-28 05:22:45 -08001341
kwiberg09f090c2017-03-01 01:57:11 -08001342 const CodecInst codec = SdpToCodecInst(payload_type, format);
kwibergd32bf752017-01-19 07:03:59 -08001343
1344 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001345 // De-register the selected codec (RTP/RTCP module and ACM)
1346
1347 int8_t pltype(-1);
1348 CodecInst rxCodec = codec;
1349
1350 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001351 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001352 rxCodec.pltype = pltype;
1353
1354 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1355 _engineStatisticsPtr->SetLastError(
1356 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1357 "SetRecPayloadType() RTP/RTCP-module deregistration "
1358 "failed");
1359 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001360 }
kwiberg55b97fe2016-01-28 05:22:45 -08001361 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1362 _engineStatisticsPtr->SetLastError(
1363 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1364 "SetRecPayloadType() ACM deregistration failed - 1");
1365 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001366 }
kwiberg55b97fe2016-01-28 05:22:45 -08001367 return 0;
1368 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001369
magjed56124bd2016-11-24 09:34:46 -08001370 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001371 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001372 // TODO(kwiberg): Retrying is probably not necessary, since
1373 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001374 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001375 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001376 _engineStatisticsPtr->SetLastError(
1377 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1378 "SetRecPayloadType() RTP/RTCP-module registration failed");
1379 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001380 }
kwiberg55b97fe2016-01-28 05:22:45 -08001381 }
kwibergd32bf752017-01-19 07:03:59 -08001382 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1383 audio_coding_->UnregisterReceiveCodec(payload_type);
1384 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001385 _engineStatisticsPtr->SetLastError(
1386 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1387 "SetRecPayloadType() ACM registration failed - 1");
1388 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001389 }
kwiberg55b97fe2016-01-28 05:22:45 -08001390 }
1391 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001392}
1393
kwiberg55b97fe2016-01-28 05:22:45 -08001394int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1395 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001396 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001397 _engineStatisticsPtr->SetLastError(
1398 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1399 "GetRecPayloadType() failed to retrieve RX payload type");
1400 return -1;
1401 }
1402 codec.pltype = payloadType;
1403 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001404}
1405
kwiberg55b97fe2016-01-28 05:22:45 -08001406int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1407 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1408 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001409
kwiberg55b97fe2016-01-28 05:22:45 -08001410 CodecInst codec;
1411 int32_t samplingFreqHz(-1);
1412 const size_t kMono = 1;
1413 if (frequency == kFreq32000Hz)
1414 samplingFreqHz = 32000;
1415 else if (frequency == kFreq16000Hz)
1416 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001417
kwiberg55b97fe2016-01-28 05:22:45 -08001418 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1419 _engineStatisticsPtr->SetLastError(
1420 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1421 "SetSendCNPayloadType() failed to retrieve default CN codec "
1422 "settings");
1423 return -1;
1424 }
1425
1426 // Modify the payload type (must be set to dynamic range)
1427 codec.pltype = type;
1428
kwibergc8d071e2016-04-06 12:22:38 -07001429 if (!codec_manager_.RegisterEncoder(codec) ||
1430 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001431 _engineStatisticsPtr->SetLastError(
1432 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1433 "SetSendCNPayloadType() failed to register CN to ACM");
1434 return -1;
1435 }
1436
1437 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1438 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1439 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1440 _engineStatisticsPtr->SetLastError(
1441 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1442 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1443 "module");
1444 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001445 }
kwiberg55b97fe2016-01-28 05:22:45 -08001446 }
1447 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001448}
1449
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001450int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001451 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001452 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001453
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001454 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001455 _engineStatisticsPtr->SetLastError(
1456 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001457 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001458 return -1;
1459 }
1460 return 0;
1461}
1462
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001463int Channel::SetOpusDtx(bool enable_dtx) {
1464 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1465 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001466 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001467 : audio_coding_->DisableOpusDtx();
1468 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001469 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1470 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001471 return -1;
1472 }
1473 return 0;
1474}
1475
ivoc85228d62016-07-27 04:53:47 -07001476int Channel::GetOpusDtx(bool* enabled) {
1477 int success = -1;
1478 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1479 if (encoder) {
1480 *enabled = encoder->GetDtx();
1481 success = 0;
1482 }
1483 });
1484 return success;
1485}
1486
minyue7e304322016-10-12 05:00:55 -07001487bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1488 bool success = false;
1489 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1490 if (*encoder) {
1491 success = (*encoder)->EnableAudioNetworkAdaptor(
michaeltbf279fc2017-01-13 06:02:29 -08001492 config_string, event_log_proxy_.get(), Clock::GetRealTimeClock());
minyue7e304322016-10-12 05:00:55 -07001493 }
1494 });
1495 return success;
1496}
1497
1498void Channel::DisableAudioNetworkAdaptor() {
1499 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1500 if (*encoder)
1501 (*encoder)->DisableAudioNetworkAdaptor();
1502 });
1503}
1504
1505void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1506 int max_frame_length_ms) {
1507 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1508 if (*encoder) {
1509 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1510 max_frame_length_ms);
1511 }
1512 });
1513}
1514
mflodman3d7db262016-04-29 00:57:13 -07001515int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001516 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001517 "Channel::RegisterExternalTransport()");
1518
kwiberg55b97fe2016-01-28 05:22:45 -08001519 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001520 if (_externalTransport) {
1521 _engineStatisticsPtr->SetLastError(
1522 VE_INVALID_OPERATION, kTraceError,
1523 "RegisterExternalTransport() external transport already enabled");
1524 return -1;
1525 }
1526 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001527 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001528 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001529}
1530
kwiberg55b97fe2016-01-28 05:22:45 -08001531int32_t Channel::DeRegisterExternalTransport() {
1532 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1533 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001534
kwiberg55b97fe2016-01-28 05:22:45 -08001535 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001536 if (_transportPtr) {
1537 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1538 "DeRegisterExternalTransport() all transport is disabled");
1539 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001540 _engineStatisticsPtr->SetLastError(
1541 VE_INVALID_OPERATION, kTraceWarning,
1542 "DeRegisterExternalTransport() external transport already "
1543 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001544 }
1545 _externalTransport = false;
1546 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001547 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001548}
1549
nisse657bab22017-02-21 06:28:10 -08001550// TODO(nisse): Delete this method together with ReceivedRTPPacket.
1551// It's a temporary hack to support both ReceivedRTPPacket and
1552// OnRtpPacket interfaces without too much code duplication.
1553bool Channel::OnRtpPacketWithHeader(const uint8_t* received_packet,
1554 size_t length,
1555 RTPHeader *header) {
1556 // Store playout timestamp for the received RTP packet
1557 UpdatePlayoutTimestamp(false);
1558
1559 header->payload_type_frequency =
1560 rtp_payload_registry_->GetPayloadTypeFrequency(header->payloadType);
1561 if (header->payload_type_frequency < 0)
1562 return false;
1563 bool in_order = IsPacketInOrder(*header);
1564 rtp_receive_statistics_->IncomingPacket(
1565 *header, length, IsPacketRetransmitted(*header, in_order));
1566 rtp_payload_registry_->SetIncomingPayloadType(*header);
1567
1568 return ReceivePacket(received_packet, length, *header, in_order);
1569}
1570
mflodman3d7db262016-04-29 00:57:13 -07001571int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001572 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001573 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001574 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001575 "Channel::ReceivedRTPPacket()");
1576
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001577 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001578 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1579 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1580 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001581 return -1;
1582 }
nisse657bab22017-02-21 06:28:10 -08001583 return OnRtpPacketWithHeader(received_packet, length, &header) ? 0 : -1;
1584}
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001585
nisse657bab22017-02-21 06:28:10 -08001586void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
1587 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
1588 "Channel::ReceivedRTPPacket()");
1589
1590 RTPHeader header;
1591 packet.GetHeader(&header);
1592 OnRtpPacketWithHeader(packet.data(), packet.size(), &header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001593}
1594
1595bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001596 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001597 const RTPHeader& header,
1598 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001599 if (rtp_payload_registry_->IsRtx(header)) {
1600 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001601 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001602 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001603 assert(packet_length >= header.headerLength);
1604 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001605 PayloadUnion payload_specific;
1606 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001607 &payload_specific)) {
1608 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001609 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001610 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1611 payload_specific, in_order);
1612}
1613
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001614bool Channel::HandleRtxPacket(const uint8_t* packet,
1615 size_t packet_length,
1616 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001617 if (!rtp_payload_registry_->IsRtx(header))
1618 return false;
1619
1620 // Remove the RTX header and parse the original RTP header.
1621 if (packet_length < header.headerLength)
1622 return false;
1623 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1624 return false;
1625 if (restored_packet_in_use_) {
1626 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1627 "Multiple RTX headers detected, dropping packet");
1628 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001629 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001630 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001631 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1632 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001633 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1634 "Incoming RTX packet: invalid RTP header");
1635 return false;
1636 }
1637 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001638 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001639 restored_packet_in_use_ = false;
1640 return ret;
1641}
1642
1643bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1644 StreamStatistician* statistician =
1645 rtp_receive_statistics_->GetStatistician(header.ssrc);
1646 if (!statistician)
1647 return false;
1648 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001649}
1650
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001651bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1652 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001653 // Retransmissions are handled separately if RTX is enabled.
1654 if (rtp_payload_registry_->RtxEnabled())
1655 return false;
1656 StreamStatistician* statistician =
1657 rtp_receive_statistics_->GetStatistician(header.ssrc);
1658 if (!statistician)
1659 return false;
1660 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001661 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001662 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001663 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001664}
1665
mflodman3d7db262016-04-29 00:57:13 -07001666int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001667 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001668 "Channel::ReceivedRTCPPacket()");
1669 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001670 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001671
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001672 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001673 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001674 _engineStatisticsPtr->SetLastError(
1675 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1676 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1677 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001678
Minyue2013aec2015-05-13 14:14:42 +02001679 int64_t rtt = GetRTT(true);
1680 if (rtt == 0) {
1681 // Waiting for valid RTT.
1682 return 0;
1683 }
Erik Språng737336d2016-07-29 12:59:36 +02001684
1685 int64_t nack_window_ms = rtt;
1686 if (nack_window_ms < kMinRetransmissionWindowMs) {
1687 nack_window_ms = kMinRetransmissionWindowMs;
1688 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1689 nack_window_ms = kMaxRetransmissionWindowMs;
1690 }
1691 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1692
minyue7e304322016-10-12 05:00:55 -07001693 // Invoke audio encoders OnReceivedRtt().
1694 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1695 if (*encoder)
1696 (*encoder)->OnReceivedRtt(rtt);
1697 });
1698
Minyue2013aec2015-05-13 14:14:42 +02001699 uint32_t ntp_secs = 0;
1700 uint32_t ntp_frac = 0;
1701 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001702 if (0 !=
1703 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1704 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001705 // Waiting for RTCP.
1706 return 0;
1707 }
1708
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001709 {
tommi31fc21f2016-01-21 10:37:37 -08001710 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001711 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001712 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001713 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001714}
1715
niklase@google.com470e71d2011-07-07 08:21:25 +00001716int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001717 bool loop,
1718 FileFormats format,
1719 int startPosition,
1720 float volumeScaling,
1721 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001722 const CodecInst* codecInst) {
1723 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1724 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1725 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1726 "stopPosition=%d)",
1727 fileName, loop, format, volumeScaling, startPosition,
1728 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001729
kwiberg55b97fe2016-01-28 05:22:45 -08001730 if (channel_state_.Get().output_file_playing) {
1731 _engineStatisticsPtr->SetLastError(
1732 VE_ALREADY_PLAYING, kTraceError,
1733 "StartPlayingFileLocally() is already playing");
1734 return -1;
1735 }
1736
1737 {
1738 rtc::CritScope cs(&_fileCritSect);
1739
kwiberg5a25d952016-08-17 07:31:12 -07001740 if (output_file_player_) {
1741 output_file_player_->RegisterModuleFileCallback(NULL);
1742 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001743 }
1744
kwiberg5b356f42016-09-08 04:32:33 -07001745 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001746 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001747
kwiberg5a25d952016-08-17 07:31:12 -07001748 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001749 _engineStatisticsPtr->SetLastError(
1750 VE_INVALID_ARGUMENT, kTraceError,
1751 "StartPlayingFileLocally() filePlayer format is not correct");
1752 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001753 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001754
kwiberg55b97fe2016-01-28 05:22:45 -08001755 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001756
kwiberg5a25d952016-08-17 07:31:12 -07001757 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001758 fileName, loop, startPosition, volumeScaling, notificationTime,
1759 stopPosition, (const CodecInst*)codecInst) != 0) {
1760 _engineStatisticsPtr->SetLastError(
1761 VE_BAD_FILE, kTraceError,
1762 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001763 output_file_player_->StopPlayingFile();
1764 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001765 return -1;
1766 }
kwiberg5a25d952016-08-17 07:31:12 -07001767 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001768 channel_state_.SetOutputFilePlaying(true);
1769 }
1770
1771 if (RegisterFilePlayingToMixer() != 0)
1772 return -1;
1773
1774 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001775}
1776
1777int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001778 FileFormats format,
1779 int startPosition,
1780 float volumeScaling,
1781 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001782 const CodecInst* codecInst) {
1783 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1784 "Channel::StartPlayingFileLocally(format=%d,"
1785 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1786 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001787
kwiberg55b97fe2016-01-28 05:22:45 -08001788 if (stream == NULL) {
1789 _engineStatisticsPtr->SetLastError(
1790 VE_BAD_FILE, kTraceError,
1791 "StartPlayingFileLocally() NULL as input stream");
1792 return -1;
1793 }
1794
1795 if (channel_state_.Get().output_file_playing) {
1796 _engineStatisticsPtr->SetLastError(
1797 VE_ALREADY_PLAYING, kTraceError,
1798 "StartPlayingFileLocally() is already playing");
1799 return -1;
1800 }
1801
1802 {
1803 rtc::CritScope cs(&_fileCritSect);
1804
1805 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001806 if (output_file_player_) {
1807 output_file_player_->RegisterModuleFileCallback(NULL);
1808 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001809 }
1810
kwiberg55b97fe2016-01-28 05:22:45 -08001811 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001812 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001813 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001814
kwiberg5a25d952016-08-17 07:31:12 -07001815 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001816 _engineStatisticsPtr->SetLastError(
1817 VE_INVALID_ARGUMENT, kTraceError,
1818 "StartPlayingFileLocally() filePlayer format isnot correct");
1819 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001820 }
1821
kwiberg55b97fe2016-01-28 05:22:45 -08001822 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001823
kwiberg4ec01d92016-08-22 08:43:54 -07001824 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001825 volumeScaling, notificationTime,
1826 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001827 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1828 "StartPlayingFile() failed to "
1829 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001830 output_file_player_->StopPlayingFile();
1831 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001832 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001833 }
kwiberg5a25d952016-08-17 07:31:12 -07001834 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001835 channel_state_.SetOutputFilePlaying(true);
1836 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001837
kwiberg55b97fe2016-01-28 05:22:45 -08001838 if (RegisterFilePlayingToMixer() != 0)
1839 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001840
kwiberg55b97fe2016-01-28 05:22:45 -08001841 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001842}
1843
kwiberg55b97fe2016-01-28 05:22:45 -08001844int Channel::StopPlayingFileLocally() {
1845 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1846 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001847
kwiberg55b97fe2016-01-28 05:22:45 -08001848 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001849 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001850 }
1851
1852 {
1853 rtc::CritScope cs(&_fileCritSect);
1854
kwiberg5a25d952016-08-17 07:31:12 -07001855 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001856 _engineStatisticsPtr->SetLastError(
1857 VE_STOP_RECORDING_FAILED, kTraceError,
1858 "StopPlayingFile() could not stop playing");
1859 return -1;
1860 }
kwiberg5a25d952016-08-17 07:31:12 -07001861 output_file_player_->RegisterModuleFileCallback(NULL);
1862 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001863 channel_state_.SetOutputFilePlaying(false);
1864 }
1865 // _fileCritSect cannot be taken while calling
1866 // SetAnonymousMixibilityStatus. Refer to comments in
1867 // StartPlayingFileLocally(const char* ...) for more details.
1868 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1869 _engineStatisticsPtr->SetLastError(
1870 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1871 "StopPlayingFile() failed to stop participant from playing as"
1872 "file in the mixer");
1873 return -1;
1874 }
1875
1876 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001877}
1878
kwiberg55b97fe2016-01-28 05:22:45 -08001879int Channel::IsPlayingFileLocally() const {
1880 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001881}
1882
kwiberg55b97fe2016-01-28 05:22:45 -08001883int Channel::RegisterFilePlayingToMixer() {
1884 // Return success for not registering for file playing to mixer if:
1885 // 1. playing file before playout is started on that channel.
1886 // 2. starting playout without file playing on that channel.
1887 if (!channel_state_.Get().playing ||
1888 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001889 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001890 }
1891
1892 // |_fileCritSect| cannot be taken while calling
1893 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1894 // frames can be pulled by the mixer. Since the frames are generated from
1895 // the file, _fileCritSect will be taken. This would result in a deadlock.
1896 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1897 channel_state_.SetOutputFilePlaying(false);
1898 rtc::CritScope cs(&_fileCritSect);
1899 _engineStatisticsPtr->SetLastError(
1900 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1901 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001902 output_file_player_->StopPlayingFile();
1903 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001904 return -1;
1905 }
1906
1907 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001908}
1909
niklase@google.com470e71d2011-07-07 08:21:25 +00001910int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001911 bool loop,
1912 FileFormats format,
1913 int startPosition,
1914 float volumeScaling,
1915 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001916 const CodecInst* codecInst) {
1917 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1918 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1919 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1920 "stopPosition=%d)",
1921 fileName, loop, format, volumeScaling, startPosition,
1922 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001923
kwiberg55b97fe2016-01-28 05:22:45 -08001924 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001925
kwiberg55b97fe2016-01-28 05:22:45 -08001926 if (channel_state_.Get().input_file_playing) {
1927 _engineStatisticsPtr->SetLastError(
1928 VE_ALREADY_PLAYING, kTraceWarning,
1929 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001930 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001931 }
1932
1933 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001934 if (input_file_player_) {
1935 input_file_player_->RegisterModuleFileCallback(NULL);
1936 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001937 }
1938
1939 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001940 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001941 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001942
kwiberg5a25d952016-08-17 07:31:12 -07001943 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001944 _engineStatisticsPtr->SetLastError(
1945 VE_INVALID_ARGUMENT, kTraceError,
1946 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1947 return -1;
1948 }
1949
1950 const uint32_t notificationTime(0);
1951
kwiberg5a25d952016-08-17 07:31:12 -07001952 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001953 fileName, loop, startPosition, volumeScaling, notificationTime,
1954 stopPosition, (const CodecInst*)codecInst) != 0) {
1955 _engineStatisticsPtr->SetLastError(
1956 VE_BAD_FILE, kTraceError,
1957 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001958 input_file_player_->StopPlayingFile();
1959 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001960 return -1;
1961 }
kwiberg5a25d952016-08-17 07:31:12 -07001962 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001963 channel_state_.SetInputFilePlaying(true);
1964
1965 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001966}
1967
1968int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001969 FileFormats format,
1970 int startPosition,
1971 float volumeScaling,
1972 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001973 const CodecInst* codecInst) {
1974 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1975 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1976 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1977 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001978
kwiberg55b97fe2016-01-28 05:22:45 -08001979 if (stream == NULL) {
1980 _engineStatisticsPtr->SetLastError(
1981 VE_BAD_FILE, kTraceError,
1982 "StartPlayingFileAsMicrophone NULL as input stream");
1983 return -1;
1984 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001985
kwiberg55b97fe2016-01-28 05:22:45 -08001986 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001987
kwiberg55b97fe2016-01-28 05:22:45 -08001988 if (channel_state_.Get().input_file_playing) {
1989 _engineStatisticsPtr->SetLastError(
1990 VE_ALREADY_PLAYING, kTraceWarning,
1991 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001992 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001993 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001994
kwiberg55b97fe2016-01-28 05:22:45 -08001995 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001996 if (input_file_player_) {
1997 input_file_player_->RegisterModuleFileCallback(NULL);
1998 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001999 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002000
kwiberg55b97fe2016-01-28 05:22:45 -08002001 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002002 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002003 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002004
kwiberg5a25d952016-08-17 07:31:12 -07002005 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002006 _engineStatisticsPtr->SetLastError(
2007 VE_INVALID_ARGUMENT, kTraceError,
2008 "StartPlayingInputFile() filePlayer format isnot correct");
2009 return -1;
2010 }
2011
2012 const uint32_t notificationTime(0);
2013
kwiberg4ec01d92016-08-22 08:43:54 -07002014 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2015 notificationTime, stopPosition,
2016 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002017 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2018 "StartPlayingFile() failed to start "
2019 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002020 input_file_player_->StopPlayingFile();
2021 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002022 return -1;
2023 }
2024
kwiberg5a25d952016-08-17 07:31:12 -07002025 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002026 channel_state_.SetInputFilePlaying(true);
2027
2028 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002029}
2030
kwiberg55b97fe2016-01-28 05:22:45 -08002031int Channel::StopPlayingFileAsMicrophone() {
2032 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2033 "Channel::StopPlayingFileAsMicrophone()");
2034
2035 rtc::CritScope cs(&_fileCritSect);
2036
2037 if (!channel_state_.Get().input_file_playing) {
2038 return 0;
2039 }
2040
kwiberg5a25d952016-08-17 07:31:12 -07002041 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002042 _engineStatisticsPtr->SetLastError(
2043 VE_STOP_RECORDING_FAILED, kTraceError,
2044 "StopPlayingFile() could not stop playing");
2045 return -1;
2046 }
kwiberg5a25d952016-08-17 07:31:12 -07002047 input_file_player_->RegisterModuleFileCallback(NULL);
2048 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002049 channel_state_.SetInputFilePlaying(false);
2050
2051 return 0;
2052}
2053
2054int Channel::IsPlayingFileAsMicrophone() const {
2055 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002056}
2057
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002058int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002059 const CodecInst* codecInst) {
2060 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2061 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002062
kwiberg55b97fe2016-01-28 05:22:45 -08002063 if (_outputFileRecording) {
2064 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2065 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002066 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002067 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002068
kwiberg55b97fe2016-01-28 05:22:45 -08002069 FileFormats format;
2070 const uint32_t notificationTime(0); // Not supported in VoE
2071 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002072
kwiberg55b97fe2016-01-28 05:22:45 -08002073 if ((codecInst != NULL) &&
2074 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2075 _engineStatisticsPtr->SetLastError(
2076 VE_BAD_ARGUMENT, kTraceError,
2077 "StartRecordingPlayout() invalid compression");
2078 return (-1);
2079 }
2080 if (codecInst == NULL) {
2081 format = kFileFormatPcm16kHzFile;
2082 codecInst = &dummyCodec;
2083 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2084 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2085 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2086 format = kFileFormatWavFile;
2087 } else {
2088 format = kFileFormatCompressedFile;
2089 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002090
kwiberg55b97fe2016-01-28 05:22:45 -08002091 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002092
kwiberg55b97fe2016-01-28 05:22:45 -08002093 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002094 if (output_file_recorder_) {
2095 output_file_recorder_->RegisterModuleFileCallback(NULL);
2096 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002097 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002098
kwiberg5a25d952016-08-17 07:31:12 -07002099 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002100 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002101 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002102 _engineStatisticsPtr->SetLastError(
2103 VE_INVALID_ARGUMENT, kTraceError,
2104 "StartRecordingPlayout() fileRecorder format isnot correct");
2105 return -1;
2106 }
2107
kwiberg5a25d952016-08-17 07:31:12 -07002108 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002109 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2110 _engineStatisticsPtr->SetLastError(
2111 VE_BAD_FILE, kTraceError,
2112 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002113 output_file_recorder_->StopRecording();
2114 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002115 return -1;
2116 }
kwiberg5a25d952016-08-17 07:31:12 -07002117 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002118 _outputFileRecording = true;
2119
2120 return 0;
2121}
2122
2123int Channel::StartRecordingPlayout(OutStream* stream,
2124 const CodecInst* codecInst) {
2125 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2126 "Channel::StartRecordingPlayout()");
2127
2128 if (_outputFileRecording) {
2129 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2130 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002131 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002132 }
2133
2134 FileFormats format;
2135 const uint32_t notificationTime(0); // Not supported in VoE
2136 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2137
2138 if (codecInst != NULL && codecInst->channels != 1) {
2139 _engineStatisticsPtr->SetLastError(
2140 VE_BAD_ARGUMENT, kTraceError,
2141 "StartRecordingPlayout() invalid compression");
2142 return (-1);
2143 }
2144 if (codecInst == NULL) {
2145 format = kFileFormatPcm16kHzFile;
2146 codecInst = &dummyCodec;
2147 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2148 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2149 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2150 format = kFileFormatWavFile;
2151 } else {
2152 format = kFileFormatCompressedFile;
2153 }
2154
2155 rtc::CritScope cs(&_fileCritSect);
2156
2157 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002158 if (output_file_recorder_) {
2159 output_file_recorder_->RegisterModuleFileCallback(NULL);
2160 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002161 }
2162
kwiberg5a25d952016-08-17 07:31:12 -07002163 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002164 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002165 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002166 _engineStatisticsPtr->SetLastError(
2167 VE_INVALID_ARGUMENT, kTraceError,
2168 "StartRecordingPlayout() fileRecorder format isnot correct");
2169 return -1;
2170 }
2171
kwiberg4ec01d92016-08-22 08:43:54 -07002172 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002173 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002174 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2175 "StartRecordingPlayout() failed to "
2176 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002177 output_file_recorder_->StopRecording();
2178 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002179 return -1;
2180 }
2181
kwiberg5a25d952016-08-17 07:31:12 -07002182 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002183 _outputFileRecording = true;
2184
2185 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002186}
2187
kwiberg55b97fe2016-01-28 05:22:45 -08002188int Channel::StopRecordingPlayout() {
2189 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2190 "Channel::StopRecordingPlayout()");
2191
2192 if (!_outputFileRecording) {
2193 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2194 "StopRecordingPlayout() isnot recording");
2195 return -1;
2196 }
2197
2198 rtc::CritScope cs(&_fileCritSect);
2199
kwiberg5a25d952016-08-17 07:31:12 -07002200 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002201 _engineStatisticsPtr->SetLastError(
2202 VE_STOP_RECORDING_FAILED, kTraceError,
2203 "StopRecording() could not stop recording");
2204 return (-1);
2205 }
kwiberg5a25d952016-08-17 07:31:12 -07002206 output_file_recorder_->RegisterModuleFileCallback(NULL);
2207 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002208 _outputFileRecording = false;
2209
2210 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002211}
2212
kwiberg55b97fe2016-01-28 05:22:45 -08002213void Channel::SetMixWithMicStatus(bool mix) {
2214 rtc::CritScope cs(&_fileCritSect);
2215 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002216}
2217
solenberg8d73f8c2017-03-08 01:52:20 -08002218int Channel::GetSpeechOutputLevel() const {
2219 return _outputAudioLevel.Level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002220}
2221
solenberg8d73f8c2017-03-08 01:52:20 -08002222int Channel::GetSpeechOutputLevelFullRange() const {
2223 return _outputAudioLevel.LevelFullRange();
kwiberg55b97fe2016-01-28 05:22:45 -08002224}
2225
solenberg8d73f8c2017-03-08 01:52:20 -08002226void Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002227 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002228 input_mute_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002229}
2230
solenberg1c2af8e2016-03-24 10:36:00 -07002231bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002232 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002233 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002234}
2235
solenberg8d73f8c2017-03-08 01:52:20 -08002236void Channel::SetChannelOutputVolumeScaling(float scaling) {
kwiberg55b97fe2016-01-28 05:22:45 -08002237 rtc::CritScope cs(&volume_settings_critsect_);
kwiberg55b97fe2016-01-28 05:22:45 -08002238 _outputGain = scaling;
niklase@google.com470e71d2011-07-07 08:21:25 +00002239}
2240
solenberg8842c3e2016-03-11 03:06:41 -08002241int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002242 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002243 "Channel::SendTelephoneEventOutband(...)");
2244 RTC_DCHECK_LE(0, event);
2245 RTC_DCHECK_GE(255, event);
2246 RTC_DCHECK_LE(0, duration_ms);
2247 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002248 if (!Sending()) {
2249 return -1;
2250 }
solenberg8842c3e2016-03-11 03:06:41 -08002251 if (_rtpRtcpModule->SendTelephoneEventOutband(
2252 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002253 _engineStatisticsPtr->SetLastError(
2254 VE_SEND_DTMF_FAILED, kTraceWarning,
2255 "SendTelephoneEventOutband() failed to send event");
2256 return -1;
2257 }
2258 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002259}
2260
solenbergffbbcac2016-11-17 05:25:37 -08002261int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2262 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002263 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002264 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002265 RTC_DCHECK_LE(0, payload_type);
2266 RTC_DCHECK_GE(127, payload_type);
2267 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002268 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002269 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002270 memcpy(codec.plname, "telephone-event", 16);
2271 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2272 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2273 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2274 _engineStatisticsPtr->SetLastError(
2275 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2276 "SetSendTelephoneEventPayloadType() failed to register send"
2277 "payload type");
2278 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002279 }
kwiberg55b97fe2016-01-28 05:22:45 -08002280 }
kwiberg55b97fe2016-01-28 05:22:45 -08002281 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002282}
2283
kwiberg55b97fe2016-01-28 05:22:45 -08002284int Channel::SetLocalSSRC(unsigned int ssrc) {
2285 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2286 "Channel::SetLocalSSRC()");
2287 if (channel_state_.Get().sending) {
2288 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2289 "SetLocalSSRC() already sending");
2290 return -1;
2291 }
2292 _rtpRtcpModule->SetSSRC(ssrc);
2293 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002294}
2295
kwiberg55b97fe2016-01-28 05:22:45 -08002296int Channel::GetLocalSSRC(unsigned int& ssrc) {
2297 ssrc = _rtpRtcpModule->SSRC();
2298 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002299}
2300
kwiberg55b97fe2016-01-28 05:22:45 -08002301int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2302 ssrc = rtp_receiver_->SSRC();
2303 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002304}
2305
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002306int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002307 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002308 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002309}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002310
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002311int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2312 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002313 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2314 if (enable &&
2315 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2316 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002317 return -1;
2318 }
2319 return 0;
2320}
2321
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002322void Channel::EnableSendTransportSequenceNumber(int id) {
2323 int ret =
2324 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2325 RTC_DCHECK_EQ(0, ret);
2326}
2327
stefan3313ec92016-01-21 06:32:43 -08002328void Channel::EnableReceiveTransportSequenceNumber(int id) {
2329 rtp_header_parser_->DeregisterRtpHeaderExtension(
2330 kRtpExtensionTransportSequenceNumber);
2331 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2332 kRtpExtensionTransportSequenceNumber, id);
2333 RTC_DCHECK(ret);
2334}
2335
stefanbba9dec2016-02-01 04:39:55 -08002336void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002337 RtpPacketSender* rtp_packet_sender,
2338 TransportFeedbackObserver* transport_feedback_observer,
stefan7de8d642017-02-07 07:14:08 -08002339 PacketRouter* packet_router,
2340 RtcpBandwidthObserver* bandwidth_observer) {
stefanbba9dec2016-02-01 04:39:55 -08002341 RTC_DCHECK(rtp_packet_sender);
2342 RTC_DCHECK(transport_feedback_observer);
2343 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002344 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002345 feedback_observer_proxy_->SetTransportFeedbackObserver(
2346 transport_feedback_observer);
2347 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2348 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2349 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002350 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002351 packet_router_ = packet_router;
2352}
2353
stefanbba9dec2016-02-01 04:39:55 -08002354void Channel::RegisterReceiverCongestionControlObjects(
2355 PacketRouter* packet_router) {
2356 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002357 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002358 packet_router_ = packet_router;
2359}
2360
2361void Channel::ResetCongestionControlObjects() {
2362 RTC_DCHECK(packet_router_);
2363 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002364 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002365 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2366 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002367 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002368 packet_router_ = nullptr;
2369 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2370}
2371
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002372void Channel::SetRTCPStatus(bool enable) {
2373 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2374 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002375 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002376}
2377
kwiberg55b97fe2016-01-28 05:22:45 -08002378int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002379 RtcpMode method = _rtpRtcpModule->RTCP();
2380 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002381 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002382}
2383
kwiberg55b97fe2016-01-28 05:22:45 -08002384int Channel::SetRTCP_CNAME(const char cName[256]) {
2385 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2386 "Channel::SetRTCP_CNAME()");
2387 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2388 _engineStatisticsPtr->SetLastError(
2389 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2390 "SetRTCP_CNAME() failed to set RTCP CNAME");
2391 return -1;
2392 }
2393 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002394}
2395
kwiberg55b97fe2016-01-28 05:22:45 -08002396int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2397 if (cName == NULL) {
2398 _engineStatisticsPtr->SetLastError(
2399 VE_INVALID_ARGUMENT, kTraceError,
2400 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2401 return -1;
2402 }
2403 char cname[RTCP_CNAME_SIZE];
2404 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2405 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2406 _engineStatisticsPtr->SetLastError(
2407 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2408 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2409 return -1;
2410 }
2411 strcpy(cName, cname);
2412 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002413}
2414
kwiberg55b97fe2016-01-28 05:22:45 -08002415int Channel::SendApplicationDefinedRTCPPacket(
2416 unsigned char subType,
2417 unsigned int name,
2418 const char* data,
2419 unsigned short dataLengthInBytes) {
2420 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2421 "Channel::SendApplicationDefinedRTCPPacket()");
2422 if (!channel_state_.Get().sending) {
2423 _engineStatisticsPtr->SetLastError(
2424 VE_NOT_SENDING, kTraceError,
2425 "SendApplicationDefinedRTCPPacket() not sending");
2426 return -1;
2427 }
2428 if (NULL == data) {
2429 _engineStatisticsPtr->SetLastError(
2430 VE_INVALID_ARGUMENT, kTraceError,
2431 "SendApplicationDefinedRTCPPacket() invalid data value");
2432 return -1;
2433 }
2434 if (dataLengthInBytes % 4 != 0) {
2435 _engineStatisticsPtr->SetLastError(
2436 VE_INVALID_ARGUMENT, kTraceError,
2437 "SendApplicationDefinedRTCPPacket() invalid length value");
2438 return -1;
2439 }
2440 RtcpMode status = _rtpRtcpModule->RTCP();
2441 if (status == RtcpMode::kOff) {
2442 _engineStatisticsPtr->SetLastError(
2443 VE_RTCP_ERROR, kTraceError,
2444 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2445 return -1;
2446 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002447
kwiberg55b97fe2016-01-28 05:22:45 -08002448 // Create and schedule the RTCP APP packet for transmission
2449 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2450 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2451 _engineStatisticsPtr->SetLastError(
2452 VE_SEND_ERROR, kTraceError,
2453 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2454 return -1;
2455 }
2456 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002457}
2458
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002459int Channel::GetRemoteRTCPReportBlocks(
2460 std::vector<ReportBlock>* report_blocks) {
2461 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002462 _engineStatisticsPtr->SetLastError(
2463 VE_INVALID_ARGUMENT, kTraceError,
2464 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002465 return -1;
2466 }
2467
2468 // Get the report blocks from the latest received RTCP Sender or Receiver
2469 // Report. Each element in the vector contains the sender's SSRC and a
2470 // report block according to RFC 3550.
2471 std::vector<RTCPReportBlock> rtcp_report_blocks;
2472 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002473 return -1;
2474 }
2475
2476 if (rtcp_report_blocks.empty())
2477 return 0;
2478
2479 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2480 for (; it != rtcp_report_blocks.end(); ++it) {
2481 ReportBlock report_block;
2482 report_block.sender_SSRC = it->remoteSSRC;
2483 report_block.source_SSRC = it->sourceSSRC;
2484 report_block.fraction_lost = it->fractionLost;
2485 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2486 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2487 report_block.interarrival_jitter = it->jitter;
2488 report_block.last_SR_timestamp = it->lastSR;
2489 report_block.delay_since_last_SR = it->delaySinceLastSR;
2490 report_blocks->push_back(report_block);
2491 }
2492 return 0;
2493}
2494
kwiberg55b97fe2016-01-28 05:22:45 -08002495int Channel::GetRTPStatistics(CallStatistics& stats) {
2496 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002497
kwiberg55b97fe2016-01-28 05:22:45 -08002498 // The jitter statistics is updated for each received RTP packet and is
2499 // based on received packets.
2500 RtcpStatistics statistics;
2501 StreamStatistician* statistician =
2502 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002503 if (statistician) {
2504 statistician->GetStatistics(&statistics,
2505 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002506 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002507
kwiberg55b97fe2016-01-28 05:22:45 -08002508 stats.fractionLost = statistics.fraction_lost;
2509 stats.cumulativeLost = statistics.cumulative_lost;
2510 stats.extendedMax = statistics.extended_max_sequence_number;
2511 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002512
kwiberg55b97fe2016-01-28 05:22:45 -08002513 // --- RTT
2514 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002515
kwiberg55b97fe2016-01-28 05:22:45 -08002516 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002517
kwiberg55b97fe2016-01-28 05:22:45 -08002518 size_t bytesSent(0);
2519 uint32_t packetsSent(0);
2520 size_t bytesReceived(0);
2521 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002522
kwiberg55b97fe2016-01-28 05:22:45 -08002523 if (statistician) {
2524 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2525 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002526
kwiberg55b97fe2016-01-28 05:22:45 -08002527 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2528 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2529 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2530 " output will not be complete");
2531 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002532
kwiberg55b97fe2016-01-28 05:22:45 -08002533 stats.bytesSent = bytesSent;
2534 stats.packetsSent = packetsSent;
2535 stats.bytesReceived = bytesReceived;
2536 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002537
kwiberg55b97fe2016-01-28 05:22:45 -08002538 // --- Timestamps
2539 {
2540 rtc::CritScope lock(&ts_stats_lock_);
2541 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2542 }
2543 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002544}
2545
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002546int Channel::SetCodecFECStatus(bool enable) {
2547 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2548 "Channel::SetCodecFECStatus()");
2549
kwibergc8d071e2016-04-06 12:22:38 -07002550 if (!codec_manager_.SetCodecFEC(enable) ||
2551 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002552 _engineStatisticsPtr->SetLastError(
2553 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2554 "SetCodecFECStatus() failed to set FEC state");
2555 return -1;
2556 }
2557 return 0;
2558}
2559
2560bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002561 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002562}
2563
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002564void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2565 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002566 // If pacing is enabled we always store packets.
2567 if (!pacing_enabled_)
2568 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002569 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002570 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002571 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002572 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002573 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002574}
2575
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002576// Called when we are missing one or more packets.
2577int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002578 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2579}
2580
kwiberg55b97fe2016-01-28 05:22:45 -08002581uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2582 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2583 "Channel::Demultiplex()");
2584 _audioFrame.CopyFrom(audioFrame);
2585 _audioFrame.id_ = _channelId;
2586 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002587}
2588
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002589void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002590 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002591 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002592 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002593 CodecInst codec;
2594 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002595
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002596 // Never upsample or upmix the capture signal here. This should be done at the
2597 // end of the send chain.
2598 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2599 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2600 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2601 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002602}
2603
kwiberg55b97fe2016-01-28 05:22:45 -08002604uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2605 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2606 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002607
kwiberg55b97fe2016-01-28 05:22:45 -08002608 if (_audioFrame.samples_per_channel_ == 0) {
2609 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2610 "Channel::PrepareEncodeAndSend() invalid audio frame");
2611 return 0xFFFFFFFF;
2612 }
2613
2614 if (channel_state_.Get().input_file_playing) {
2615 MixOrReplaceAudioWithFile(mixingFrequency);
2616 }
2617
solenberg1c2af8e2016-03-24 10:36:00 -07002618 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2619 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002620
kwiberg55b97fe2016-01-28 05:22:45 -08002621 if (_includeAudioLevelIndication) {
2622 size_t length =
2623 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002624 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002625 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002626 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002627 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002628 rms_level_.Analyze(
2629 rtc::ArrayView<const int16_t>(_audioFrame.data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002630 }
kwiberg55b97fe2016-01-28 05:22:45 -08002631 }
solenberg1c2af8e2016-03-24 10:36:00 -07002632 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002633
kwiberg55b97fe2016-01-28 05:22:45 -08002634 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002635}
2636
kwiberg55b97fe2016-01-28 05:22:45 -08002637uint32_t Channel::EncodeAndSend() {
2638 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2639 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002640
kwiberg55b97fe2016-01-28 05:22:45 -08002641 assert(_audioFrame.num_channels_ <= 2);
2642 if (_audioFrame.samples_per_channel_ == 0) {
2643 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2644 "Channel::EncodeAndSend() invalid audio frame");
2645 return 0xFFFFFFFF;
2646 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002647
kwiberg55b97fe2016-01-28 05:22:45 -08002648 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002649
kwiberg55b97fe2016-01-28 05:22:45 -08002650 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002651
kwiberg55b97fe2016-01-28 05:22:45 -08002652 // The ACM resamples internally.
2653 _audioFrame.timestamp_ = _timeStamp;
2654 // This call will trigger AudioPacketizationCallback::SendData if encoding
2655 // is done and payload is ready for packetization and transmission.
2656 // Otherwise, it will return without invoking the callback.
2657 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2658 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2659 "Channel::EncodeAndSend() ACM encoding failed");
2660 return 0xFFFFFFFF;
2661 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002662
kwiberg55b97fe2016-01-28 05:22:45 -08002663 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2664 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002665}
2666
solenberg7602aab2016-11-14 11:30:07 -08002667void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2668 RTC_DCHECK(!channel.channel() ||
2669 channel.channel()->ChannelId() != _channelId);
2670 rtc::CritScope lock(&assoc_send_channel_lock_);
2671 associate_send_channel_ = channel;
2672}
2673
Minyue2013aec2015-05-13 14:14:42 +02002674void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002675 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002676 Channel* channel = associate_send_channel_.channel();
2677 if (channel && channel->ChannelId() == channel_id) {
2678 // If this channel is associated with a send channel of the specified
2679 // Channel ID, disassociate with it.
2680 ChannelOwner ref(NULL);
2681 associate_send_channel_ = ref;
2682 }
2683}
2684
ivoc14d5dbe2016-07-04 07:06:55 -07002685void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2686 event_log_proxy_->SetEventLog(event_log);
2687}
2688
michaelt9332b7d2016-11-30 07:51:13 -08002689void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2690 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2691}
2692
nisse284542b2017-01-10 08:58:32 -08002693void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08002694 size_t overhead_per_packet =
2695 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08002696 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2697 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08002698 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08002699 }
2700 });
2701}
2702
2703void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002704 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002705 transport_overhead_per_packet_ = transport_overhead_per_packet;
2706 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002707}
2708
hbos3fd31fe2017-02-28 05:43:16 -08002709// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08002710void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002711 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002712 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2713 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002714}
2715
kwiberg55b97fe2016-01-28 05:22:45 -08002716int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2717 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002718}
2719
wu@webrtc.org24301a62013-12-13 19:17:43 +00002720void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2721 audio_coding_->GetDecodingCallStatistics(stats);
2722}
2723
solenberg358057b2015-11-27 10:46:42 -08002724uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002725 rtc::CritScope lock(&video_sync_lock_);
2726 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002727}
2728
kwiberg55b97fe2016-01-28 05:22:45 -08002729int Channel::SetMinimumPlayoutDelay(int delayMs) {
2730 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2731 "Channel::SetMinimumPlayoutDelay()");
2732 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2733 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2734 _engineStatisticsPtr->SetLastError(
2735 VE_INVALID_ARGUMENT, kTraceError,
2736 "SetMinimumPlayoutDelay() invalid min delay");
2737 return -1;
2738 }
2739 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2740 _engineStatisticsPtr->SetLastError(
2741 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2742 "SetMinimumPlayoutDelay() failed to set min playout delay");
2743 return -1;
2744 }
2745 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002746}
2747
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002748int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002749 uint32_t playout_timestamp_rtp = 0;
2750 {
tommi31fc21f2016-01-21 10:37:37 -08002751 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002752 playout_timestamp_rtp = playout_timestamp_rtp_;
2753 }
kwiberg55b97fe2016-01-28 05:22:45 -08002754 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002755 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002756 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002757 "GetPlayoutTimestamp() failed to retrieve timestamp");
2758 return -1;
2759 }
deadbeef74375882015-08-13 12:09:10 -07002760 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002761 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002762}
2763
kwiberg55b97fe2016-01-28 05:22:45 -08002764int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2765 RtpReceiver** rtp_receiver) const {
2766 *rtpRtcpModule = _rtpRtcpModule.get();
2767 *rtp_receiver = rtp_receiver_.get();
2768 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002769}
2770
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002771// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2772// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002773int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002774 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002775 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002776
kwiberg55b97fe2016-01-28 05:22:45 -08002777 {
2778 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002779
kwiberg5a25d952016-08-17 07:31:12 -07002780 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002781 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2782 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2783 " doesnt exist");
2784 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002785 }
2786
kwiberg4ec01d92016-08-22 08:43:54 -07002787 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002788 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002789 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2790 "Channel::MixOrReplaceAudioWithFile() file mixing "
2791 "failed");
2792 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002793 }
kwiberg55b97fe2016-01-28 05:22:45 -08002794 if (fileSamples == 0) {
2795 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2796 "Channel::MixOrReplaceAudioWithFile() file is ended");
2797 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002798 }
kwiberg55b97fe2016-01-28 05:22:45 -08002799 }
2800
2801 assert(_audioFrame.samples_per_channel_ == fileSamples);
2802
2803 if (_mixFileWithMicrophone) {
2804 // Currently file stream is always mono.
2805 // TODO(xians): Change the code when FilePlayer supports real stereo.
2806 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
2807 1, fileSamples);
2808 } else {
2809 // Replace ACM audio with file.
2810 // Currently file stream is always mono.
2811 // TODO(xians): Change the code when FilePlayer supports real stereo.
2812 _audioFrame.UpdateFrame(
2813 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2814 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2815 }
2816 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002817}
2818
kwiberg55b97fe2016-01-28 05:22:45 -08002819int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2820 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002821
kwibergb7f89d62016-02-17 10:04:18 -08002822 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002823 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002824
kwiberg55b97fe2016-01-28 05:22:45 -08002825 {
2826 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002827
kwiberg5a25d952016-08-17 07:31:12 -07002828 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002829 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2830 "Channel::MixAudioWithFile() file mixing failed");
2831 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002832 }
2833
kwiberg55b97fe2016-01-28 05:22:45 -08002834 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002835 if (output_file_player_->Get10msAudioFromFile(
2836 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002837 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2838 "Channel::MixAudioWithFile() file mixing failed");
2839 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002840 }
kwiberg55b97fe2016-01-28 05:22:45 -08002841 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002842
kwiberg55b97fe2016-01-28 05:22:45 -08002843 if (audioFrame.samples_per_channel_ == fileSamples) {
2844 // Currently file stream is always mono.
2845 // TODO(xians): Change the code when FilePlayer supports real stereo.
2846 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
2847 fileSamples);
2848 } else {
2849 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2850 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2851 ") != "
2852 "fileSamples(%" PRIuS ")",
2853 audioFrame.samples_per_channel_, fileSamples);
2854 return -1;
2855 }
2856
2857 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002858}
2859
deadbeef74375882015-08-13 12:09:10 -07002860void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002861 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002862
henrik.lundin96bd5022016-04-06 04:13:56 -07002863 if (!jitter_buffer_playout_timestamp_) {
2864 // This can happen if this channel has not received any RTP packets. In
2865 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07002866 return;
2867 }
2868
2869 uint16_t delay_ms = 0;
2870 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002871 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002872 "Channel::UpdatePlayoutTimestamp() failed to read playout"
2873 " delay from the ADM");
2874 _engineStatisticsPtr->SetLastError(
2875 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
2876 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
2877 return;
2878 }
2879
henrik.lundin96bd5022016-04-06 04:13:56 -07002880 RTC_DCHECK(jitter_buffer_playout_timestamp_);
2881 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07002882
2883 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07002884 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07002885
kwiberg55b97fe2016-01-28 05:22:45 -08002886 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002887 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07002888 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07002889
2890 {
tommi31fc21f2016-01-21 10:37:37 -08002891 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08002892 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002893 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07002894 }
2895 playout_delay_ms_ = delay_ms;
2896 }
2897}
2898
kwiberg55b97fe2016-01-28 05:22:45 -08002899void Channel::RegisterReceiveCodecsToRTPModule() {
2900 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2901 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002902
kwiberg55b97fe2016-01-28 05:22:45 -08002903 CodecInst codec;
2904 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002905
kwiberg55b97fe2016-01-28 05:22:45 -08002906 for (int idx = 0; idx < nSupportedCodecs; idx++) {
2907 // Open up the RTP/RTCP receiver for all supported codecs
2908 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08002909 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08002910 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2911 "Channel::RegisterReceiveCodecsToRTPModule() unable"
2912 " to register %s (%d/%d/%" PRIuS
2913 "/%d) to RTP/RTCP "
2914 "receiver",
2915 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2916 codec.rate);
2917 } else {
2918 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2919 "Channel::RegisterReceiveCodecsToRTPModule() %s "
2920 "(%d/%d/%" PRIuS
2921 "/%d) has been added to the RTP/RTCP "
2922 "receiver",
2923 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2924 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00002925 }
kwiberg55b97fe2016-01-28 05:22:45 -08002926 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002927}
2928
kwiberg55b97fe2016-01-28 05:22:45 -08002929int Channel::SetSendRtpHeaderExtension(bool enable,
2930 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002931 unsigned char id) {
2932 int error = 0;
2933 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
2934 if (enable) {
2935 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
2936 }
2937 return error;
2938}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002939
ossue280cde2016-10-12 11:04:10 -07002940int Channel::GetRtpTimestampRateHz() const {
2941 const auto format = audio_coding_->ReceiveFormat();
2942 // Default to the playout frequency if we've not gotten any packets yet.
2943 // TODO(ossu): Zero clockrate can only happen if we've added an external
2944 // decoder for a format we don't support internally. Remove once that way of
2945 // adding decoders is gone!
2946 return (format && format->clockrate_hz != 0)
2947 ? format->clockrate_hz
2948 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00002949}
2950
Minyue2013aec2015-05-13 14:14:42 +02002951int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07002952 RtcpMode method = _rtpRtcpModule->RTCP();
2953 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002954 return 0;
2955 }
2956 std::vector<RTCPReportBlock> report_blocks;
2957 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02002958
2959 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002960 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02002961 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08002962 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002963 Channel* channel = associate_send_channel_.channel();
2964 // Tries to get RTT from an associated channel. This is important for
2965 // receive-only channels.
2966 if (channel) {
2967 // To prevent infinite recursion and deadlock, calling GetRTT of
2968 // associate channel should always use "false" for argument:
2969 // |allow_associate_channel|.
2970 rtt = channel->GetRTT(false);
2971 }
2972 }
2973 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002974 }
2975
2976 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2977 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
2978 for (; it != report_blocks.end(); ++it) {
2979 if (it->remoteSSRC == remoteSSRC)
2980 break;
2981 }
2982 if (it == report_blocks.end()) {
2983 // We have not received packets with SSRC matching the report blocks.
2984 // To calculate RTT we try with the SSRC of the first report block.
2985 // This is very important for send-only channels where we don't know
2986 // the SSRC of the other end.
2987 remoteSSRC = report_blocks[0].remoteSSRC;
2988 }
Minyue2013aec2015-05-13 14:14:42 +02002989
pkasting@chromium.org16825b12015-01-12 21:51:21 +00002990 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002991 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00002992 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002993 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
2994 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002995 return 0;
2996 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00002997 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002998}
2999
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003000} // namespace voe
3001} // namespace webrtc