blob: 1d54821a50c882d1e85a7426295948c475333f0b [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"
pbosad856222015-11-27 09:48:36 -080021#include "webrtc/base/logging.h"
Erik Språng737336d2016-07-29 12:59:36 +020022#include "webrtc/base/rate_limiter.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010023#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000024#include "webrtc/base/timeutils.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020025#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070026#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070027#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000028#include "webrtc/modules/audio_device/include/audio_device.h"
29#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010031#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010032#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
33#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
34#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
nisse657bab22017-02-21 06:28:10 -080035#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000036#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010037#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010038#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000039#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
40#include "webrtc/voice_engine/output_mixer.h"
41#include "webrtc/voice_engine/statistics.h"
42#include "webrtc/voice_engine/transmit_mixer.h"
43#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 }
265 std::vector<PacketInfo> GetTransportFeedbackVector() const override {
266 RTC_NOTREACHED();
267 return std::vector<PacketInfo>();
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
kwiberg55b97fe2016-01-28 05:22:45 -0800449int32_t Channel::InFrameType(FrameType frame_type) {
450 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
451 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000452
kwiberg55b97fe2016-01-28 05:22:45 -0800453 rtc::CritScope cs(&_callbackCritSect);
454 _sendFrameType = (frame_type == kAudioFrameSpeech);
455 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000456}
457
stefan1d8a5062015-10-02 03:39:33 -0700458bool Channel::SendRtp(const uint8_t* data,
459 size_t len,
460 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800461 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
462 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000463
kwiberg55b97fe2016-01-28 05:22:45 -0800464 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000465
kwiberg55b97fe2016-01-28 05:22:45 -0800466 if (_transportPtr == NULL) {
467 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
468 "Channel::SendPacket() failed to send RTP packet due to"
469 " invalid transport object");
470 return false;
471 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000472
kwiberg55b97fe2016-01-28 05:22:45 -0800473 uint8_t* bufferToSendPtr = (uint8_t*)data;
474 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000475
kwiberg55b97fe2016-01-28 05:22:45 -0800476 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
477 std::string transport_name =
478 _externalTransport ? "external transport" : "WebRtc sockets";
479 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
480 "Channel::SendPacket() RTP transmission using %s failed",
481 transport_name.c_str());
482 return false;
483 }
484 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000485}
486
kwiberg55b97fe2016-01-28 05:22:45 -0800487bool Channel::SendRtcp(const uint8_t* data, size_t len) {
488 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
489 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000490
kwiberg55b97fe2016-01-28 05:22:45 -0800491 rtc::CritScope cs(&_callbackCritSect);
492 if (_transportPtr == NULL) {
493 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
494 "Channel::SendRtcp() failed to send RTCP packet"
495 " due to invalid transport object");
496 return false;
497 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000498
kwiberg55b97fe2016-01-28 05:22:45 -0800499 uint8_t* bufferToSendPtr = (uint8_t*)data;
500 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000501
kwiberg55b97fe2016-01-28 05:22:45 -0800502 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
503 if (n < 0) {
504 std::string transport_name =
505 _externalTransport ? "external transport" : "WebRtc sockets";
506 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
507 "Channel::SendRtcp() transmission using %s failed",
508 transport_name.c_str());
509 return false;
510 }
511 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000512}
513
kwiberg55b97fe2016-01-28 05:22:45 -0800514void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
515 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
516 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000517
kwiberg55b97fe2016-01-28 05:22:45 -0800518 // Update ssrc so that NTP for AV sync can be updated.
519 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000520}
521
Peter Boströmac547a62015-09-17 23:03:57 +0200522void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
523 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
524 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
525 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000526}
527
Peter Boströmac547a62015-09-17 23:03:57 +0200528int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000529 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000530 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000531 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800532 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200533 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800534 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
535 "Channel::OnInitializeDecoder(payloadType=%d, "
536 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
537 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000538
kwiberg55b97fe2016-01-28 05:22:45 -0800539 CodecInst receiveCodec = {0};
540 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000541
kwiberg55b97fe2016-01-28 05:22:45 -0800542 receiveCodec.pltype = payloadType;
543 receiveCodec.plfreq = frequency;
544 receiveCodec.channels = channels;
545 receiveCodec.rate = rate;
546 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000547
kwiberg55b97fe2016-01-28 05:22:45 -0800548 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
549 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000550
kwiberg55b97fe2016-01-28 05:22:45 -0800551 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700552 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
553 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800554 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
555 "Channel::OnInitializeDecoder() invalid codec ("
556 "pt=%d, name=%s) received - 1",
557 payloadType, payloadName);
558 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
559 return -1;
560 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000561
kwiberg55b97fe2016-01-28 05:22:45 -0800562 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000563}
564
kwiberg55b97fe2016-01-28 05:22:45 -0800565int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
566 size_t payloadSize,
567 const WebRtcRTPHeader* rtpHeader) {
568 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
569 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
570 ","
571 " payloadType=%u, audioChannel=%" PRIuS ")",
572 payloadSize, rtpHeader->header.payloadType,
573 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000574
kwiberg55b97fe2016-01-28 05:22:45 -0800575 if (!channel_state_.Get().playing) {
576 // Avoid inserting into NetEQ when we are not playing. Count the
577 // packet as discarded.
578 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
579 "received packet is discarded since playing is not"
580 " activated");
niklase@google.com470e71d2011-07-07 08:21:25 +0000581 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800582 }
583
584 // Push the incoming payload (parsed and ready for decoding) into the ACM
585 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
586 0) {
587 _engineStatisticsPtr->SetLastError(
588 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
589 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
590 return -1;
591 }
592
kwiberg55b97fe2016-01-28 05:22:45 -0800593 int64_t round_trip_time = 0;
594 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
595 NULL);
596
597 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
598 if (!nack_list.empty()) {
599 // Can't use nack_list.data() since it's not supported by all
600 // compilers.
601 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
602 }
603 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000604}
605
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000606bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000607 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000608 RTPHeader header;
609 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
610 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
611 "IncomingPacket invalid RTP header");
612 return false;
613 }
614 header.payload_type_frequency =
615 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
616 if (header.payload_type_frequency < 0)
617 return false;
618 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
619}
620
henrik.lundin42dda502016-05-18 05:36:01 -0700621MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
622 int32_t id,
623 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700624 unsigned int ssrc;
nisse7d59f6b2017-02-21 03:40:24 -0800625 RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
ivoc14d5dbe2016-07-04 07:06:55 -0700626 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800627 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700628 bool muted;
629 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
630 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800631 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
632 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
633 // In all likelihood, the audio in this frame is garbage. We return an
634 // error so that the audio mixer module doesn't add it to the mix. As
635 // a result, it won't be played out and the actions skipped here are
636 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700637 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800638 }
henrik.lundina89ab962016-05-18 08:52:45 -0700639
640 if (muted) {
641 // TODO(henrik.lundin): We should be able to do better than this. But we
642 // will have to go through all the cases below where the audio samples may
643 // be used, and handle the muted case in some way.
aleloi6321b492016-12-05 01:46:09 -0800644 AudioFrameOperations::Mute(audioFrame);
henrik.lundina89ab962016-05-18 08:52:45 -0700645 }
kwiberg55b97fe2016-01-28 05:22:45 -0800646
kwiberg55b97fe2016-01-28 05:22:45 -0800647 // Convert module ID to internal VoE channel ID
648 audioFrame->id_ = VoEChannelId(audioFrame->id_);
649 // Store speech type for dead-or-alive detection
650 _outputSpeechType = audioFrame->speech_type_;
651
652 ChannelState::State state = channel_state_.Get();
653
kwiberg55b97fe2016-01-28 05:22:45 -0800654 {
655 // Pass the audio buffers to an optional sink callback, before applying
656 // scaling/panning, as that applies to the mix operation.
657 // External recipients of the audio (e.g. via AudioTrack), will do their
658 // own mixing/dynamic processing.
659 rtc::CritScope cs(&_callbackCritSect);
660 if (audio_sink_) {
661 AudioSinkInterface::Data data(
662 &audioFrame->data_[0], audioFrame->samples_per_channel_,
663 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
664 audioFrame->timestamp_);
665 audio_sink_->OnData(data);
666 }
667 }
668
669 float output_gain = 1.0f;
670 float left_pan = 1.0f;
671 float right_pan = 1.0f;
672 {
673 rtc::CritScope cs(&volume_settings_critsect_);
674 output_gain = _outputGain;
675 left_pan = _panLeft;
676 right_pan = _panRight;
677 }
678
679 // Output volume scaling
680 if (output_gain < 0.99f || output_gain > 1.01f) {
681 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
682 }
683
684 // Scale left and/or right channel(s) if stereo and master balance is
685 // active
686
687 if (left_pan != 1.0f || right_pan != 1.0f) {
688 if (audioFrame->num_channels_ == 1) {
689 // Emulate stereo mode since panning is active.
690 // The mono signal is copied to both left and right channels here.
691 AudioFrameOperations::MonoToStereo(audioFrame);
692 }
693 // For true stereo mode (when we are receiving a stereo signal), no
694 // action is needed.
695
696 // Do the panning operation (the audio frame contains stereo at this
697 // stage)
698 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
699 }
700
701 // Mix decoded PCM output with file if file mixing is enabled
702 if (state.output_file_playing) {
703 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700704 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800705 }
706
kwiberg55b97fe2016-01-28 05:22:45 -0800707 // Record playout if enabled
708 {
709 rtc::CritScope cs(&_fileCritSect);
710
kwiberg5a25d952016-08-17 07:31:12 -0700711 if (_outputFileRecording && output_file_recorder_) {
712 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800713 }
714 }
715
716 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700717 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800718 _outputAudioLevel.ComputeLevel(*audioFrame);
719
720 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
721 // The first frame with a valid rtp timestamp.
722 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
723 }
724
725 if (capture_start_rtp_time_stamp_ >= 0) {
726 // audioFrame.timestamp_ should be valid from now on.
727
728 // Compute elapsed time.
729 int64_t unwrap_timestamp =
730 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
731 audioFrame->elapsed_time_ms_ =
732 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700733 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800734
niklase@google.com470e71d2011-07-07 08:21:25 +0000735 {
kwiberg55b97fe2016-01-28 05:22:45 -0800736 rtc::CritScope lock(&ts_stats_lock_);
737 // Compute ntp time.
738 audioFrame->ntp_time_ms_ =
739 ntp_estimator_.Estimate(audioFrame->timestamp_);
740 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
741 if (audioFrame->ntp_time_ms_ > 0) {
742 // Compute |capture_start_ntp_time_ms_| so that
743 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
744 capture_start_ntp_time_ms_ =
745 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000746 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000747 }
kwiberg55b97fe2016-01-28 05:22:45 -0800748 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000749
henrik.lundin42dda502016-05-18 05:36:01 -0700750 return muted ? MixerParticipant::AudioFrameInfo::kMuted
751 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000752}
753
aleloi6c278492016-10-20 14:24:39 -0700754AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
755 int sample_rate_hz,
756 AudioFrame* audio_frame) {
757 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700758
aleloi6c278492016-10-20 14:24:39 -0700759 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700760
761 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
762 FrameInfo new_audio_frame_info = FrameInfo::kError;
763 switch (frame_info) {
764 case MixerParticipant::AudioFrameInfo::kNormal:
765 new_audio_frame_info = FrameInfo::kNormal;
766 break;
767 case MixerParticipant::AudioFrameInfo::kMuted:
768 new_audio_frame_info = FrameInfo::kMuted;
769 break;
770 case MixerParticipant::AudioFrameInfo::kError:
771 new_audio_frame_info = FrameInfo::kError;
772 break;
773 }
aleloi6c278492016-10-20 14:24:39 -0700774 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700775}
776
kwiberg55b97fe2016-01-28 05:22:45 -0800777int32_t Channel::NeededFrequency(int32_t id) const {
778 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
779 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000780
kwiberg55b97fe2016-01-28 05:22:45 -0800781 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000782
kwiberg55b97fe2016-01-28 05:22:45 -0800783 // Determine highest needed receive frequency
784 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000785
kwiberg55b97fe2016-01-28 05:22:45 -0800786 // Return the bigger of playout and receive frequency in the ACM.
787 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
788 highestNeeded = audio_coding_->PlayoutFrequency();
789 } else {
790 highestNeeded = receiveFrequency;
791 }
792
793 // Special case, if we're playing a file on the playout side
794 // we take that frequency into consideration as well
795 // This is not needed on sending side, since the codec will
796 // limit the spectrum anyway.
797 if (channel_state_.Get().output_file_playing) {
798 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700799 if (output_file_player_) {
800 if (output_file_player_->Frequency() > highestNeeded) {
801 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800802 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000803 }
kwiberg55b97fe2016-01-28 05:22:45 -0800804 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000805
kwiberg55b97fe2016-01-28 05:22:45 -0800806 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000807}
808
ossu5f7cfa52016-05-30 08:11:28 -0700809int32_t Channel::CreateChannel(
810 Channel*& channel,
811 int32_t channelId,
812 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700813 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800814 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
815 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
816 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000817
solenberg88499ec2016-09-07 07:34:41 -0700818 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800819 if (channel == NULL) {
820 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
821 "Channel::CreateChannel() unable to allocate memory for"
822 " channel");
823 return -1;
824 }
825 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000826}
827
kwiberg55b97fe2016-01-28 05:22:45 -0800828void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
829 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
830 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
831 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000832
kwiberg55b97fe2016-01-28 05:22:45 -0800833 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000834}
835
kwiberg55b97fe2016-01-28 05:22:45 -0800836void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
837 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
838 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
839 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000840
kwiberg55b97fe2016-01-28 05:22:45 -0800841 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000842}
843
kwiberg55b97fe2016-01-28 05:22:45 -0800844void Channel::PlayFileEnded(int32_t id) {
845 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
846 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000847
kwiberg55b97fe2016-01-28 05:22:45 -0800848 if (id == _inputFilePlayerId) {
849 channel_state_.SetInputFilePlaying(false);
850 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
851 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000852 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800853 } else if (id == _outputFilePlayerId) {
854 channel_state_.SetOutputFilePlaying(false);
855 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
856 "Channel::PlayFileEnded() => output file player module is"
857 " shutdown");
858 }
859}
860
861void Channel::RecordFileEnded(int32_t id) {
862 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
863 "Channel::RecordFileEnded(id=%d)", id);
864
865 assert(id == _outputFileRecorderId);
866
867 rtc::CritScope cs(&_fileCritSect);
868
869 _outputFileRecording = false;
870 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
871 "Channel::RecordFileEnded() => output file recorder module is"
872 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000873}
874
pbos@webrtc.org92135212013-05-14 08:31:39 +0000875Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000876 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700877 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800878 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100879 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700880 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800881 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100882 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800883 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100884 rtp_receive_statistics_(
885 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
886 rtp_receiver_(
887 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100888 this,
889 this,
890 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700891 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100892 _outputAudioLevel(),
893 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100894 // Avoid conflict with other channels by adding 1024 - 1026,
895 // won't use as much as 1024 channels.
896 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
897 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
898 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
899 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100900 _timeStamp(0), // This is just an offset, RTP module will add it's own
901 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100902 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100903 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100904 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100905 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100906 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
907 capture_start_rtp_time_stamp_(-1),
908 capture_start_ntp_time_ms_(-1),
909 _engineStatisticsPtr(NULL),
910 _outputMixerPtr(NULL),
911 _transmitMixerPtr(NULL),
912 _moduleProcessThreadPtr(NULL),
913 _audioDeviceModulePtr(NULL),
914 _voiceEngineObserverPtr(NULL),
915 _callbackCritSectPtr(NULL),
916 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100917 _sendFrameType(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100918 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700919 input_mute_(false),
920 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100921 _panLeft(1.0f),
922 _panRight(1.0f),
923 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100924 _lastLocalTimeStamp(0),
925 _lastPayloadType(0),
926 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800927 transport_overhead_per_packet_(0),
928 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100929 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100930 restored_packet_in_use_(false),
931 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100932 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700933 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800934 feedback_observer_proxy_(new TransportFeedbackProxy()),
935 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700936 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200937 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
938 kMaxRetransmissionWindowMs)),
michaelt566d8202017-01-12 10:17:38 -0800939 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800940 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
941 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700942 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800943 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700944 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800945 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200946
kwiberg55b97fe2016-01-28 05:22:45 -0800947 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000948
kwiberg55b97fe2016-01-28 05:22:45 -0800949 RtpRtcp::Configuration configuration;
950 configuration.audio = true;
951 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800952 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800953 configuration.receive_statistics = rtp_receive_statistics_.get();
954 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800955 if (pacing_enabled_) {
956 configuration.paced_sender = rtp_packet_sender_proxy_.get();
957 configuration.transport_sequence_number_allocator =
958 seq_num_allocator_proxy_.get();
959 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
960 }
ivoc14d5dbe2016-07-04 07:06:55 -0700961 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800962 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200963 configuration.retransmission_rate_limiter =
964 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000965
kwiberg55b97fe2016-01-28 05:22:45 -0800966 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100967 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000968}
969
kwiberg55b97fe2016-01-28 05:22:45 -0800970Channel::~Channel() {
971 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
972 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
973 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000974
kwiberg55b97fe2016-01-28 05:22:45 -0800975 StopSend();
976 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000977
kwiberg55b97fe2016-01-28 05:22:45 -0800978 {
979 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700980 if (input_file_player_) {
981 input_file_player_->RegisterModuleFileCallback(NULL);
982 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000983 }
kwiberg5a25d952016-08-17 07:31:12 -0700984 if (output_file_player_) {
985 output_file_player_->RegisterModuleFileCallback(NULL);
986 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800987 }
kwiberg5a25d952016-08-17 07:31:12 -0700988 if (output_file_recorder_) {
989 output_file_recorder_->RegisterModuleFileCallback(NULL);
990 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800991 }
992 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000993
kwiberg55b97fe2016-01-28 05:22:45 -0800994 // The order to safely shutdown modules in a channel is:
995 // 1. De-register callbacks in modules
996 // 2. De-register modules in process thread
997 // 3. Destroy modules
998 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
999 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1000 "~Channel() failed to de-register transport callback"
1001 " (Audio coding module)");
1002 }
1003 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
1004 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1005 "~Channel() failed to de-register VAD callback"
1006 " (Audio coding module)");
1007 }
1008 // De-register modules in process thread
1009 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +00001010
kwiberg55b97fe2016-01-28 05:22:45 -08001011 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +00001012}
1013
kwiberg55b97fe2016-01-28 05:22:45 -08001014int32_t Channel::Init() {
1015 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1016 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001017
kwiberg55b97fe2016-01-28 05:22:45 -08001018 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001019
kwiberg55b97fe2016-01-28 05:22:45 -08001020 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +00001021
kwiberg55b97fe2016-01-28 05:22:45 -08001022 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
1023 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1024 "Channel::Init() must call SetEngineInformation() first");
1025 return -1;
1026 }
1027
1028 // --- Add modules to process thread (for periodic schedulation)
1029
1030 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
1031
1032 // --- ACM initialization
1033
1034 if (audio_coding_->InitializeReceiver() == -1) {
1035 _engineStatisticsPtr->SetLastError(
1036 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1037 "Channel::Init() unable to initialize the ACM - 1");
1038 return -1;
1039 }
1040
1041 // --- RTP/RTCP module initialization
1042
1043 // Ensure that RTCP is enabled by default for the created channel.
1044 // Note that, the module will keep generating RTCP until it is explicitly
1045 // disabled by the user.
1046 // After StopListen (when no sockets exists), RTCP packets will no longer
1047 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001048 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001049 // RTCP is enabled by default.
1050 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1051 // --- Register all permanent callbacks
1052 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1053 (audio_coding_->RegisterVADCallback(this) == -1);
1054
1055 if (fail) {
1056 _engineStatisticsPtr->SetLastError(
1057 VE_CANNOT_INIT_CHANNEL, kTraceError,
1058 "Channel::Init() callbacks not registered");
1059 return -1;
1060 }
1061
1062 // --- Register all supported codecs to the receiving side of the
1063 // RTP/RTCP module
1064
1065 CodecInst codec;
1066 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1067
1068 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1069 // Open up the RTP/RTCP receiver for all supported codecs
1070 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08001071 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001072 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1073 "Channel::Init() unable to register %s "
1074 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1075 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1076 codec.rate);
1077 } else {
1078 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1079 "Channel::Init() %s (%d/%d/%" PRIuS
1080 "/%d) has been "
1081 "added to the RTP/RTCP receiver",
1082 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1083 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001084 }
1085
kwiberg55b97fe2016-01-28 05:22:45 -08001086 // Ensure that PCMU is used as default codec on the sending side
1087 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1088 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001089 }
1090
kwiberg55b97fe2016-01-28 05:22:45 -08001091 // Register default PT for outband 'telephone-event'
1092 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001093 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001094 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1095 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001096 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1097 "Channel::Init() failed to register outband "
1098 "'telephone-event' (%d/%d) correctly",
1099 codec.pltype, codec.plfreq);
1100 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001101 }
1102
kwiberg55b97fe2016-01-28 05:22:45 -08001103 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001104 if (!codec_manager_.RegisterEncoder(codec) ||
1105 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001106 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1107 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001108 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001109 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1110 "Channel::Init() failed to register CN (%d/%d) "
1111 "correctly - 1",
1112 codec.pltype, codec.plfreq);
1113 }
1114 }
kwiberg55b97fe2016-01-28 05:22:45 -08001115 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001116
kwiberg55b97fe2016-01-28 05:22:45 -08001117 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001118}
1119
kwiberg55b97fe2016-01-28 05:22:45 -08001120int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1121 OutputMixer& outputMixer,
1122 voe::TransmitMixer& transmitMixer,
1123 ProcessThread& moduleProcessThread,
1124 AudioDeviceModule& audioDeviceModule,
1125 VoiceEngineObserver* voiceEngineObserver,
1126 rtc::CriticalSection* callbackCritSect) {
1127 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1128 "Channel::SetEngineInformation()");
1129 _engineStatisticsPtr = &engineStatistics;
1130 _outputMixerPtr = &outputMixer;
1131 _transmitMixerPtr = &transmitMixer,
1132 _moduleProcessThreadPtr = &moduleProcessThread;
1133 _audioDeviceModulePtr = &audioDeviceModule;
1134 _voiceEngineObserverPtr = voiceEngineObserver;
1135 _callbackCritSectPtr = callbackCritSect;
1136 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001137}
1138
kwiberg55b97fe2016-01-28 05:22:45 -08001139int32_t Channel::UpdateLocalTimeStamp() {
1140 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1141 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001142}
1143
kwibergb7f89d62016-02-17 10:04:18 -08001144void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001145 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001146 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001147}
1148
ossu29b1a8d2016-06-13 07:34:51 -07001149const rtc::scoped_refptr<AudioDecoderFactory>&
1150Channel::GetAudioDecoderFactory() const {
1151 return decoder_factory_;
1152}
1153
kwiberg55b97fe2016-01-28 05:22:45 -08001154int32_t Channel::StartPlayout() {
1155 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1156 "Channel::StartPlayout()");
1157 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001158 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001159 }
1160
solenberge374e012017-02-14 04:55:00 -08001161 // Add participant as candidates for mixing.
1162 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1163 _engineStatisticsPtr->SetLastError(
1164 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1165 "StartPlayout() failed to add participant to mixer");
1166 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001167 }
1168
1169 channel_state_.SetPlaying(true);
1170 if (RegisterFilePlayingToMixer() != 0)
1171 return -1;
1172
1173 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001174}
1175
kwiberg55b97fe2016-01-28 05:22:45 -08001176int32_t Channel::StopPlayout() {
1177 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1178 "Channel::StopPlayout()");
1179 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001180 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001181 }
1182
solenberge374e012017-02-14 04:55:00 -08001183 // Remove participant as candidates for mixing
1184 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1185 _engineStatisticsPtr->SetLastError(
1186 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1187 "StopPlayout() failed to remove participant from mixer");
1188 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001189 }
1190
1191 channel_state_.SetPlaying(false);
1192 _outputAudioLevel.Clear();
1193
1194 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001195}
1196
kwiberg55b97fe2016-01-28 05:22:45 -08001197int32_t Channel::StartSend() {
1198 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1199 "Channel::StartSend()");
kwiberg55b97fe2016-01-28 05:22:45 -08001200 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001201 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001202 }
1203 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001204
solenberg08b19df2017-02-15 00:42:31 -08001205 // Resume the previous sequence number which was reset by StopSend(). This
1206 // needs to be done before |sending| is set to true on the RTP/RTCP module.
1207 if (send_sequence_number_) {
1208 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
1209 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001210 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001211 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1212 _engineStatisticsPtr->SetLastError(
1213 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1214 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001215 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001216 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001217 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001218 return -1;
1219 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001220
kwiberg55b97fe2016-01-28 05:22:45 -08001221 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001222}
1223
kwiberg55b97fe2016-01-28 05:22:45 -08001224int32_t Channel::StopSend() {
1225 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1226 "Channel::StopSend()");
1227 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001228 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001229 }
1230 channel_state_.SetSending(false);
1231
1232 // Store the sequence number to be able to pick up the same sequence for
1233 // the next StartSend(). This is needed for restarting device, otherwise
1234 // it might cause libSRTP to complain about packets being replayed.
1235 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1236 // CL is landed. See issue
1237 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1238 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1239
1240 // Reset sending SSRC and sequence number and triggers direct transmission
1241 // of RTCP BYE
1242 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1243 _engineStatisticsPtr->SetLastError(
1244 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1245 "StartSend() RTP/RTCP failed to stop sending");
1246 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001247 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001248
1249 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001250}
1251
kwiberg55b97fe2016-01-28 05:22:45 -08001252int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1253 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1254 "Channel::RegisterVoiceEngineObserver()");
1255 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001256
kwiberg55b97fe2016-01-28 05:22:45 -08001257 if (_voiceEngineObserverPtr) {
1258 _engineStatisticsPtr->SetLastError(
1259 VE_INVALID_OPERATION, kTraceError,
1260 "RegisterVoiceEngineObserver() observer already enabled");
1261 return -1;
1262 }
1263 _voiceEngineObserverPtr = &observer;
1264 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001265}
1266
kwiberg55b97fe2016-01-28 05:22:45 -08001267int32_t Channel::DeRegisterVoiceEngineObserver() {
1268 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1269 "Channel::DeRegisterVoiceEngineObserver()");
1270 rtc::CritScope cs(&_callbackCritSect);
1271
1272 if (!_voiceEngineObserverPtr) {
1273 _engineStatisticsPtr->SetLastError(
1274 VE_INVALID_OPERATION, kTraceWarning,
1275 "DeRegisterVoiceEngineObserver() observer already disabled");
1276 return 0;
1277 }
1278 _voiceEngineObserverPtr = NULL;
1279 return 0;
1280}
1281
1282int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001283 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001284 if (send_codec) {
1285 codec = *send_codec;
1286 return 0;
1287 }
1288 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001289}
1290
kwiberg55b97fe2016-01-28 05:22:45 -08001291int32_t Channel::GetRecCodec(CodecInst& codec) {
1292 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001293}
1294
kwiberg55b97fe2016-01-28 05:22:45 -08001295int32_t Channel::SetSendCodec(const CodecInst& codec) {
1296 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1297 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001298
kwibergc8d071e2016-04-06 12:22:38 -07001299 if (!codec_manager_.RegisterEncoder(codec) ||
1300 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001301 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1302 "SetSendCodec() failed to register codec to ACM");
1303 return -1;
1304 }
1305
1306 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1307 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1308 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1309 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1310 "SetSendCodec() failed to register codec to"
1311 " RTP/RTCP module");
1312 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001313 }
kwiberg55b97fe2016-01-28 05:22:45 -08001314 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001315
kwiberg55b97fe2016-01-28 05:22:45 -08001316 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001317}
1318
minyue78b4d562016-11-30 04:47:39 -08001319void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001320 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1321 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001322 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001323 if (*encoder) {
1324 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001325 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001326 }
1327 });
michaelt566d8202017-01-12 10:17:38 -08001328 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001329}
1330
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001331void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001332 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1333 if (*encoder)
1334 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1335 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001336}
1337
kwiberg55b97fe2016-01-28 05:22:45 -08001338int32_t Channel::SetVADStatus(bool enableVAD,
1339 ACMVADMode mode,
1340 bool disableDTX) {
1341 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1342 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001343 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1344 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1345 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001346 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1347 kTraceError,
1348 "SetVADStatus() failed to set VAD");
1349 return -1;
1350 }
1351 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001352}
1353
kwiberg55b97fe2016-01-28 05:22:45 -08001354int32_t Channel::GetVADStatus(bool& enabledVAD,
1355 ACMVADMode& mode,
1356 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001357 const auto* params = codec_manager_.GetStackParams();
1358 enabledVAD = params->use_cng;
1359 mode = params->vad_mode;
1360 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001361 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001362}
1363
kwiberg55b97fe2016-01-28 05:22:45 -08001364int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001365 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1366}
1367
1368int32_t Channel::SetRecPayloadType(int payload_type,
1369 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001370 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1371 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001372
kwiberg55b97fe2016-01-28 05:22:45 -08001373 if (channel_state_.Get().playing) {
1374 _engineStatisticsPtr->SetLastError(
1375 VE_ALREADY_PLAYING, kTraceError,
1376 "SetRecPayloadType() unable to set PT while playing");
1377 return -1;
1378 }
kwiberg55b97fe2016-01-28 05:22:45 -08001379
kwibergd32bf752017-01-19 07:03:59 -08001380 const CodecInst codec = [&] {
1381 CodecInst c = SdpToCodecInst(payload_type, format);
1382
1383 // Bug 6986: Emulate an old bug that caused us to always choose to decode
1384 // Opus in stereo. To be able to remove this, we first need to fix the
1385 // other half of bug 6986, which is about losing the Opus "stereo"
1386 // parameter.
1387 // TODO(kwiberg): Remove this special case, a.k.a. fix bug 6986.
1388 if (STR_CASE_CMP(codec.plname, "opus") == 0) {
1389 c.channels = 2;
1390 }
1391
1392 return c;
1393 }();
1394
1395 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001396 // De-register the selected codec (RTP/RTCP module and ACM)
1397
1398 int8_t pltype(-1);
1399 CodecInst rxCodec = codec;
1400
1401 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001402 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001403 rxCodec.pltype = pltype;
1404
1405 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1406 _engineStatisticsPtr->SetLastError(
1407 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1408 "SetRecPayloadType() RTP/RTCP-module deregistration "
1409 "failed");
1410 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001411 }
kwiberg55b97fe2016-01-28 05:22:45 -08001412 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1413 _engineStatisticsPtr->SetLastError(
1414 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1415 "SetRecPayloadType() ACM deregistration failed - 1");
1416 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001417 }
kwiberg55b97fe2016-01-28 05:22:45 -08001418 return 0;
1419 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001420
magjed56124bd2016-11-24 09:34:46 -08001421 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001422 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001423 // TODO(kwiberg): Retrying is probably not necessary, since
1424 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001425 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001426 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001427 _engineStatisticsPtr->SetLastError(
1428 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1429 "SetRecPayloadType() RTP/RTCP-module registration failed");
1430 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001431 }
kwiberg55b97fe2016-01-28 05:22:45 -08001432 }
kwibergd32bf752017-01-19 07:03:59 -08001433 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1434 audio_coding_->UnregisterReceiveCodec(payload_type);
1435 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001436 _engineStatisticsPtr->SetLastError(
1437 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1438 "SetRecPayloadType() ACM registration failed - 1");
1439 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001440 }
kwiberg55b97fe2016-01-28 05:22:45 -08001441 }
1442 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001443}
1444
kwiberg55b97fe2016-01-28 05:22:45 -08001445int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1446 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001447 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001448 _engineStatisticsPtr->SetLastError(
1449 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1450 "GetRecPayloadType() failed to retrieve RX payload type");
1451 return -1;
1452 }
1453 codec.pltype = payloadType;
1454 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001455}
1456
kwiberg55b97fe2016-01-28 05:22:45 -08001457int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1458 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1459 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001460
kwiberg55b97fe2016-01-28 05:22:45 -08001461 CodecInst codec;
1462 int32_t samplingFreqHz(-1);
1463 const size_t kMono = 1;
1464 if (frequency == kFreq32000Hz)
1465 samplingFreqHz = 32000;
1466 else if (frequency == kFreq16000Hz)
1467 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001468
kwiberg55b97fe2016-01-28 05:22:45 -08001469 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1470 _engineStatisticsPtr->SetLastError(
1471 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1472 "SetSendCNPayloadType() failed to retrieve default CN codec "
1473 "settings");
1474 return -1;
1475 }
1476
1477 // Modify the payload type (must be set to dynamic range)
1478 codec.pltype = type;
1479
kwibergc8d071e2016-04-06 12:22:38 -07001480 if (!codec_manager_.RegisterEncoder(codec) ||
1481 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001482 _engineStatisticsPtr->SetLastError(
1483 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1484 "SetSendCNPayloadType() failed to register CN to ACM");
1485 return -1;
1486 }
1487
1488 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1489 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1490 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1491 _engineStatisticsPtr->SetLastError(
1492 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1493 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1494 "module");
1495 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001496 }
kwiberg55b97fe2016-01-28 05:22:45 -08001497 }
1498 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001499}
1500
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001501int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001502 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001503 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001504
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001505 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001506 _engineStatisticsPtr->SetLastError(
1507 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001508 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001509 return -1;
1510 }
1511 return 0;
1512}
1513
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001514int Channel::SetOpusDtx(bool enable_dtx) {
1515 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1516 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001517 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001518 : audio_coding_->DisableOpusDtx();
1519 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001520 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1521 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001522 return -1;
1523 }
1524 return 0;
1525}
1526
ivoc85228d62016-07-27 04:53:47 -07001527int Channel::GetOpusDtx(bool* enabled) {
1528 int success = -1;
1529 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1530 if (encoder) {
1531 *enabled = encoder->GetDtx();
1532 success = 0;
1533 }
1534 });
1535 return success;
1536}
1537
minyue7e304322016-10-12 05:00:55 -07001538bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1539 bool success = false;
1540 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1541 if (*encoder) {
1542 success = (*encoder)->EnableAudioNetworkAdaptor(
michaeltbf279fc2017-01-13 06:02:29 -08001543 config_string, event_log_proxy_.get(), Clock::GetRealTimeClock());
minyue7e304322016-10-12 05:00:55 -07001544 }
1545 });
1546 return success;
1547}
1548
1549void Channel::DisableAudioNetworkAdaptor() {
1550 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1551 if (*encoder)
1552 (*encoder)->DisableAudioNetworkAdaptor();
1553 });
1554}
1555
1556void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1557 int max_frame_length_ms) {
1558 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1559 if (*encoder) {
1560 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1561 max_frame_length_ms);
1562 }
1563 });
1564}
1565
mflodman3d7db262016-04-29 00:57:13 -07001566int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001567 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001568 "Channel::RegisterExternalTransport()");
1569
kwiberg55b97fe2016-01-28 05:22:45 -08001570 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001571 if (_externalTransport) {
1572 _engineStatisticsPtr->SetLastError(
1573 VE_INVALID_OPERATION, kTraceError,
1574 "RegisterExternalTransport() external transport already enabled");
1575 return -1;
1576 }
1577 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001578 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001579 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001580}
1581
kwiberg55b97fe2016-01-28 05:22:45 -08001582int32_t Channel::DeRegisterExternalTransport() {
1583 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1584 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001585
kwiberg55b97fe2016-01-28 05:22:45 -08001586 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001587 if (_transportPtr) {
1588 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1589 "DeRegisterExternalTransport() all transport is disabled");
1590 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001591 _engineStatisticsPtr->SetLastError(
1592 VE_INVALID_OPERATION, kTraceWarning,
1593 "DeRegisterExternalTransport() external transport already "
1594 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001595 }
1596 _externalTransport = false;
1597 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001598 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001599}
1600
nisse657bab22017-02-21 06:28:10 -08001601// TODO(nisse): Delete this method together with ReceivedRTPPacket.
1602// It's a temporary hack to support both ReceivedRTPPacket and
1603// OnRtpPacket interfaces without too much code duplication.
1604bool Channel::OnRtpPacketWithHeader(const uint8_t* received_packet,
1605 size_t length,
1606 RTPHeader *header) {
1607 // Store playout timestamp for the received RTP packet
1608 UpdatePlayoutTimestamp(false);
1609
1610 header->payload_type_frequency =
1611 rtp_payload_registry_->GetPayloadTypeFrequency(header->payloadType);
1612 if (header->payload_type_frequency < 0)
1613 return false;
1614 bool in_order = IsPacketInOrder(*header);
1615 rtp_receive_statistics_->IncomingPacket(
1616 *header, length, IsPacketRetransmitted(*header, in_order));
1617 rtp_payload_registry_->SetIncomingPayloadType(*header);
1618
1619 return ReceivePacket(received_packet, length, *header, in_order);
1620}
1621
mflodman3d7db262016-04-29 00:57:13 -07001622int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001623 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001624 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001625 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001626 "Channel::ReceivedRTPPacket()");
1627
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001628 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001629 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1630 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1631 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001632 return -1;
1633 }
nisse657bab22017-02-21 06:28:10 -08001634 return OnRtpPacketWithHeader(received_packet, length, &header) ? 0 : -1;
1635}
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001636
nisse657bab22017-02-21 06:28:10 -08001637void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
1638 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
1639 "Channel::ReceivedRTPPacket()");
1640
1641 RTPHeader header;
1642 packet.GetHeader(&header);
1643 OnRtpPacketWithHeader(packet.data(), packet.size(), &header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001644}
1645
1646bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001647 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001648 const RTPHeader& header,
1649 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001650 if (rtp_payload_registry_->IsRtx(header)) {
1651 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001652 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001653 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001654 assert(packet_length >= header.headerLength);
1655 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001656 PayloadUnion payload_specific;
1657 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001658 &payload_specific)) {
1659 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001660 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001661 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1662 payload_specific, in_order);
1663}
1664
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001665bool Channel::HandleRtxPacket(const uint8_t* packet,
1666 size_t packet_length,
1667 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001668 if (!rtp_payload_registry_->IsRtx(header))
1669 return false;
1670
1671 // Remove the RTX header and parse the original RTP header.
1672 if (packet_length < header.headerLength)
1673 return false;
1674 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1675 return false;
1676 if (restored_packet_in_use_) {
1677 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1678 "Multiple RTX headers detected, dropping packet");
1679 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001680 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001681 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001682 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1683 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001684 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1685 "Incoming RTX packet: invalid RTP header");
1686 return false;
1687 }
1688 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001689 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001690 restored_packet_in_use_ = false;
1691 return ret;
1692}
1693
1694bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1695 StreamStatistician* statistician =
1696 rtp_receive_statistics_->GetStatistician(header.ssrc);
1697 if (!statistician)
1698 return false;
1699 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001700}
1701
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001702bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1703 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001704 // Retransmissions are handled separately if RTX is enabled.
1705 if (rtp_payload_registry_->RtxEnabled())
1706 return false;
1707 StreamStatistician* statistician =
1708 rtp_receive_statistics_->GetStatistician(header.ssrc);
1709 if (!statistician)
1710 return false;
1711 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001712 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001713 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001714 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001715}
1716
mflodman3d7db262016-04-29 00:57:13 -07001717int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001718 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001719 "Channel::ReceivedRTCPPacket()");
1720 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001721 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001722
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001723 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001724 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001725 _engineStatisticsPtr->SetLastError(
1726 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1727 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1728 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001729
Minyue2013aec2015-05-13 14:14:42 +02001730 int64_t rtt = GetRTT(true);
1731 if (rtt == 0) {
1732 // Waiting for valid RTT.
1733 return 0;
1734 }
Erik Språng737336d2016-07-29 12:59:36 +02001735
1736 int64_t nack_window_ms = rtt;
1737 if (nack_window_ms < kMinRetransmissionWindowMs) {
1738 nack_window_ms = kMinRetransmissionWindowMs;
1739 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1740 nack_window_ms = kMaxRetransmissionWindowMs;
1741 }
1742 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1743
minyue7e304322016-10-12 05:00:55 -07001744 // Invoke audio encoders OnReceivedRtt().
1745 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1746 if (*encoder)
1747 (*encoder)->OnReceivedRtt(rtt);
1748 });
1749
Minyue2013aec2015-05-13 14:14:42 +02001750 uint32_t ntp_secs = 0;
1751 uint32_t ntp_frac = 0;
1752 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001753 if (0 !=
1754 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1755 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001756 // Waiting for RTCP.
1757 return 0;
1758 }
1759
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001760 {
tommi31fc21f2016-01-21 10:37:37 -08001761 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001762 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001763 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001764 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001765}
1766
niklase@google.com470e71d2011-07-07 08:21:25 +00001767int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001768 bool loop,
1769 FileFormats format,
1770 int startPosition,
1771 float volumeScaling,
1772 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001773 const CodecInst* codecInst) {
1774 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1775 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1776 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1777 "stopPosition=%d)",
1778 fileName, loop, format, volumeScaling, startPosition,
1779 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001780
kwiberg55b97fe2016-01-28 05:22:45 -08001781 if (channel_state_.Get().output_file_playing) {
1782 _engineStatisticsPtr->SetLastError(
1783 VE_ALREADY_PLAYING, kTraceError,
1784 "StartPlayingFileLocally() is already playing");
1785 return -1;
1786 }
1787
1788 {
1789 rtc::CritScope cs(&_fileCritSect);
1790
kwiberg5a25d952016-08-17 07:31:12 -07001791 if (output_file_player_) {
1792 output_file_player_->RegisterModuleFileCallback(NULL);
1793 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001794 }
1795
kwiberg5b356f42016-09-08 04:32:33 -07001796 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001797 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001798
kwiberg5a25d952016-08-17 07:31:12 -07001799 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001800 _engineStatisticsPtr->SetLastError(
1801 VE_INVALID_ARGUMENT, kTraceError,
1802 "StartPlayingFileLocally() filePlayer format is not correct");
1803 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001804 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001805
kwiberg55b97fe2016-01-28 05:22:45 -08001806 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001807
kwiberg5a25d952016-08-17 07:31:12 -07001808 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001809 fileName, loop, startPosition, volumeScaling, notificationTime,
1810 stopPosition, (const CodecInst*)codecInst) != 0) {
1811 _engineStatisticsPtr->SetLastError(
1812 VE_BAD_FILE, kTraceError,
1813 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001814 output_file_player_->StopPlayingFile();
1815 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001816 return -1;
1817 }
kwiberg5a25d952016-08-17 07:31:12 -07001818 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001819 channel_state_.SetOutputFilePlaying(true);
1820 }
1821
1822 if (RegisterFilePlayingToMixer() != 0)
1823 return -1;
1824
1825 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001826}
1827
1828int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001829 FileFormats format,
1830 int startPosition,
1831 float volumeScaling,
1832 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001833 const CodecInst* codecInst) {
1834 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1835 "Channel::StartPlayingFileLocally(format=%d,"
1836 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1837 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001838
kwiberg55b97fe2016-01-28 05:22:45 -08001839 if (stream == NULL) {
1840 _engineStatisticsPtr->SetLastError(
1841 VE_BAD_FILE, kTraceError,
1842 "StartPlayingFileLocally() NULL as input stream");
1843 return -1;
1844 }
1845
1846 if (channel_state_.Get().output_file_playing) {
1847 _engineStatisticsPtr->SetLastError(
1848 VE_ALREADY_PLAYING, kTraceError,
1849 "StartPlayingFileLocally() is already playing");
1850 return -1;
1851 }
1852
1853 {
1854 rtc::CritScope cs(&_fileCritSect);
1855
1856 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001857 if (output_file_player_) {
1858 output_file_player_->RegisterModuleFileCallback(NULL);
1859 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001860 }
1861
kwiberg55b97fe2016-01-28 05:22:45 -08001862 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001863 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001864 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001865
kwiberg5a25d952016-08-17 07:31:12 -07001866 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001867 _engineStatisticsPtr->SetLastError(
1868 VE_INVALID_ARGUMENT, kTraceError,
1869 "StartPlayingFileLocally() filePlayer format isnot correct");
1870 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001871 }
1872
kwiberg55b97fe2016-01-28 05:22:45 -08001873 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001874
kwiberg4ec01d92016-08-22 08:43:54 -07001875 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001876 volumeScaling, notificationTime,
1877 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001878 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1879 "StartPlayingFile() failed to "
1880 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001881 output_file_player_->StopPlayingFile();
1882 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001883 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001884 }
kwiberg5a25d952016-08-17 07:31:12 -07001885 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001886 channel_state_.SetOutputFilePlaying(true);
1887 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001888
kwiberg55b97fe2016-01-28 05:22:45 -08001889 if (RegisterFilePlayingToMixer() != 0)
1890 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001891
kwiberg55b97fe2016-01-28 05:22:45 -08001892 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001893}
1894
kwiberg55b97fe2016-01-28 05:22:45 -08001895int Channel::StopPlayingFileLocally() {
1896 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1897 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001898
kwiberg55b97fe2016-01-28 05:22:45 -08001899 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001900 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001901 }
1902
1903 {
1904 rtc::CritScope cs(&_fileCritSect);
1905
kwiberg5a25d952016-08-17 07:31:12 -07001906 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001907 _engineStatisticsPtr->SetLastError(
1908 VE_STOP_RECORDING_FAILED, kTraceError,
1909 "StopPlayingFile() could not stop playing");
1910 return -1;
1911 }
kwiberg5a25d952016-08-17 07:31:12 -07001912 output_file_player_->RegisterModuleFileCallback(NULL);
1913 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001914 channel_state_.SetOutputFilePlaying(false);
1915 }
1916 // _fileCritSect cannot be taken while calling
1917 // SetAnonymousMixibilityStatus. Refer to comments in
1918 // StartPlayingFileLocally(const char* ...) for more details.
1919 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1920 _engineStatisticsPtr->SetLastError(
1921 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1922 "StopPlayingFile() failed to stop participant from playing as"
1923 "file in the mixer");
1924 return -1;
1925 }
1926
1927 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001928}
1929
kwiberg55b97fe2016-01-28 05:22:45 -08001930int Channel::IsPlayingFileLocally() const {
1931 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001932}
1933
kwiberg55b97fe2016-01-28 05:22:45 -08001934int Channel::RegisterFilePlayingToMixer() {
1935 // Return success for not registering for file playing to mixer if:
1936 // 1. playing file before playout is started on that channel.
1937 // 2. starting playout without file playing on that channel.
1938 if (!channel_state_.Get().playing ||
1939 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001940 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001941 }
1942
1943 // |_fileCritSect| cannot be taken while calling
1944 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1945 // frames can be pulled by the mixer. Since the frames are generated from
1946 // the file, _fileCritSect will be taken. This would result in a deadlock.
1947 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1948 channel_state_.SetOutputFilePlaying(false);
1949 rtc::CritScope cs(&_fileCritSect);
1950 _engineStatisticsPtr->SetLastError(
1951 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1952 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001953 output_file_player_->StopPlayingFile();
1954 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001955 return -1;
1956 }
1957
1958 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001959}
1960
niklase@google.com470e71d2011-07-07 08:21:25 +00001961int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001962 bool loop,
1963 FileFormats format,
1964 int startPosition,
1965 float volumeScaling,
1966 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001967 const CodecInst* codecInst) {
1968 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1969 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1970 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1971 "stopPosition=%d)",
1972 fileName, loop, format, volumeScaling, startPosition,
1973 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001974
kwiberg55b97fe2016-01-28 05:22:45 -08001975 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001976
kwiberg55b97fe2016-01-28 05:22:45 -08001977 if (channel_state_.Get().input_file_playing) {
1978 _engineStatisticsPtr->SetLastError(
1979 VE_ALREADY_PLAYING, kTraceWarning,
1980 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001981 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001982 }
1983
1984 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001985 if (input_file_player_) {
1986 input_file_player_->RegisterModuleFileCallback(NULL);
1987 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001988 }
1989
1990 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001991 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001992 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001993
kwiberg5a25d952016-08-17 07:31:12 -07001994 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001995 _engineStatisticsPtr->SetLastError(
1996 VE_INVALID_ARGUMENT, kTraceError,
1997 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1998 return -1;
1999 }
2000
2001 const uint32_t notificationTime(0);
2002
kwiberg5a25d952016-08-17 07:31:12 -07002003 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002004 fileName, loop, startPosition, volumeScaling, notificationTime,
2005 stopPosition, (const CodecInst*)codecInst) != 0) {
2006 _engineStatisticsPtr->SetLastError(
2007 VE_BAD_FILE, kTraceError,
2008 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002009 input_file_player_->StopPlayingFile();
2010 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002011 return -1;
2012 }
kwiberg5a25d952016-08-17 07:31:12 -07002013 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002014 channel_state_.SetInputFilePlaying(true);
2015
2016 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002017}
2018
2019int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002020 FileFormats format,
2021 int startPosition,
2022 float volumeScaling,
2023 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002024 const CodecInst* codecInst) {
2025 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2026 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2027 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2028 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002029
kwiberg55b97fe2016-01-28 05:22:45 -08002030 if (stream == NULL) {
2031 _engineStatisticsPtr->SetLastError(
2032 VE_BAD_FILE, kTraceError,
2033 "StartPlayingFileAsMicrophone NULL as input stream");
2034 return -1;
2035 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002036
kwiberg55b97fe2016-01-28 05:22:45 -08002037 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002038
kwiberg55b97fe2016-01-28 05:22:45 -08002039 if (channel_state_.Get().input_file_playing) {
2040 _engineStatisticsPtr->SetLastError(
2041 VE_ALREADY_PLAYING, kTraceWarning,
2042 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002043 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002044 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002045
kwiberg55b97fe2016-01-28 05:22:45 -08002046 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002047 if (input_file_player_) {
2048 input_file_player_->RegisterModuleFileCallback(NULL);
2049 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002050 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002051
kwiberg55b97fe2016-01-28 05:22:45 -08002052 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002053 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002054 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002055
kwiberg5a25d952016-08-17 07:31:12 -07002056 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002057 _engineStatisticsPtr->SetLastError(
2058 VE_INVALID_ARGUMENT, kTraceError,
2059 "StartPlayingInputFile() filePlayer format isnot correct");
2060 return -1;
2061 }
2062
2063 const uint32_t notificationTime(0);
2064
kwiberg4ec01d92016-08-22 08:43:54 -07002065 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2066 notificationTime, stopPosition,
2067 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002068 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2069 "StartPlayingFile() failed to start "
2070 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002071 input_file_player_->StopPlayingFile();
2072 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002073 return -1;
2074 }
2075
kwiberg5a25d952016-08-17 07:31:12 -07002076 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002077 channel_state_.SetInputFilePlaying(true);
2078
2079 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002080}
2081
kwiberg55b97fe2016-01-28 05:22:45 -08002082int Channel::StopPlayingFileAsMicrophone() {
2083 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2084 "Channel::StopPlayingFileAsMicrophone()");
2085
2086 rtc::CritScope cs(&_fileCritSect);
2087
2088 if (!channel_state_.Get().input_file_playing) {
2089 return 0;
2090 }
2091
kwiberg5a25d952016-08-17 07:31:12 -07002092 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002093 _engineStatisticsPtr->SetLastError(
2094 VE_STOP_RECORDING_FAILED, kTraceError,
2095 "StopPlayingFile() could not stop playing");
2096 return -1;
2097 }
kwiberg5a25d952016-08-17 07:31:12 -07002098 input_file_player_->RegisterModuleFileCallback(NULL);
2099 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002100 channel_state_.SetInputFilePlaying(false);
2101
2102 return 0;
2103}
2104
2105int Channel::IsPlayingFileAsMicrophone() const {
2106 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002107}
2108
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002109int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002110 const CodecInst* codecInst) {
2111 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2112 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002113
kwiberg55b97fe2016-01-28 05:22:45 -08002114 if (_outputFileRecording) {
2115 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2116 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002117 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002118 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002119
kwiberg55b97fe2016-01-28 05:22:45 -08002120 FileFormats format;
2121 const uint32_t notificationTime(0); // Not supported in VoE
2122 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002123
kwiberg55b97fe2016-01-28 05:22:45 -08002124 if ((codecInst != NULL) &&
2125 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2126 _engineStatisticsPtr->SetLastError(
2127 VE_BAD_ARGUMENT, kTraceError,
2128 "StartRecordingPlayout() invalid compression");
2129 return (-1);
2130 }
2131 if (codecInst == NULL) {
2132 format = kFileFormatPcm16kHzFile;
2133 codecInst = &dummyCodec;
2134 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2135 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2136 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2137 format = kFileFormatWavFile;
2138 } else {
2139 format = kFileFormatCompressedFile;
2140 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002141
kwiberg55b97fe2016-01-28 05:22:45 -08002142 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002143
kwiberg55b97fe2016-01-28 05:22:45 -08002144 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002145 if (output_file_recorder_) {
2146 output_file_recorder_->RegisterModuleFileCallback(NULL);
2147 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002148 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002149
kwiberg5a25d952016-08-17 07:31:12 -07002150 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002151 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002152 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002153 _engineStatisticsPtr->SetLastError(
2154 VE_INVALID_ARGUMENT, kTraceError,
2155 "StartRecordingPlayout() fileRecorder format isnot correct");
2156 return -1;
2157 }
2158
kwiberg5a25d952016-08-17 07:31:12 -07002159 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002160 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2161 _engineStatisticsPtr->SetLastError(
2162 VE_BAD_FILE, kTraceError,
2163 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002164 output_file_recorder_->StopRecording();
2165 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002166 return -1;
2167 }
kwiberg5a25d952016-08-17 07:31:12 -07002168 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002169 _outputFileRecording = true;
2170
2171 return 0;
2172}
2173
2174int Channel::StartRecordingPlayout(OutStream* stream,
2175 const CodecInst* codecInst) {
2176 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2177 "Channel::StartRecordingPlayout()");
2178
2179 if (_outputFileRecording) {
2180 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2181 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002182 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002183 }
2184
2185 FileFormats format;
2186 const uint32_t notificationTime(0); // Not supported in VoE
2187 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2188
2189 if (codecInst != NULL && codecInst->channels != 1) {
2190 _engineStatisticsPtr->SetLastError(
2191 VE_BAD_ARGUMENT, kTraceError,
2192 "StartRecordingPlayout() invalid compression");
2193 return (-1);
2194 }
2195 if (codecInst == NULL) {
2196 format = kFileFormatPcm16kHzFile;
2197 codecInst = &dummyCodec;
2198 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2199 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2200 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2201 format = kFileFormatWavFile;
2202 } else {
2203 format = kFileFormatCompressedFile;
2204 }
2205
2206 rtc::CritScope cs(&_fileCritSect);
2207
2208 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002209 if (output_file_recorder_) {
2210 output_file_recorder_->RegisterModuleFileCallback(NULL);
2211 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002212 }
2213
kwiberg5a25d952016-08-17 07:31:12 -07002214 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002215 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002216 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002217 _engineStatisticsPtr->SetLastError(
2218 VE_INVALID_ARGUMENT, kTraceError,
2219 "StartRecordingPlayout() fileRecorder format isnot correct");
2220 return -1;
2221 }
2222
kwiberg4ec01d92016-08-22 08:43:54 -07002223 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002224 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002225 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2226 "StartRecordingPlayout() failed to "
2227 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002228 output_file_recorder_->StopRecording();
2229 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002230 return -1;
2231 }
2232
kwiberg5a25d952016-08-17 07:31:12 -07002233 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002234 _outputFileRecording = true;
2235
2236 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002237}
2238
kwiberg55b97fe2016-01-28 05:22:45 -08002239int Channel::StopRecordingPlayout() {
2240 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2241 "Channel::StopRecordingPlayout()");
2242
2243 if (!_outputFileRecording) {
2244 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2245 "StopRecordingPlayout() isnot recording");
2246 return -1;
2247 }
2248
2249 rtc::CritScope cs(&_fileCritSect);
2250
kwiberg5a25d952016-08-17 07:31:12 -07002251 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002252 _engineStatisticsPtr->SetLastError(
2253 VE_STOP_RECORDING_FAILED, kTraceError,
2254 "StopRecording() could not stop recording");
2255 return (-1);
2256 }
kwiberg5a25d952016-08-17 07:31:12 -07002257 output_file_recorder_->RegisterModuleFileCallback(NULL);
2258 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002259 _outputFileRecording = false;
2260
2261 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002262}
2263
kwiberg55b97fe2016-01-28 05:22:45 -08002264void Channel::SetMixWithMicStatus(bool mix) {
2265 rtc::CritScope cs(&_fileCritSect);
2266 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002267}
2268
kwiberg55b97fe2016-01-28 05:22:45 -08002269int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2270 int8_t currentLevel = _outputAudioLevel.Level();
2271 level = static_cast<int32_t>(currentLevel);
2272 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002273}
2274
kwiberg55b97fe2016-01-28 05:22:45 -08002275int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2276 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2277 level = static_cast<int32_t>(currentLevel);
2278 return 0;
2279}
2280
solenberg1c2af8e2016-03-24 10:36:00 -07002281int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002282 rtc::CritScope cs(&volume_settings_critsect_);
2283 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002284 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002285 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002286 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002287}
2288
solenberg1c2af8e2016-03-24 10:36:00 -07002289bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002290 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002291 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002292}
2293
kwiberg55b97fe2016-01-28 05:22:45 -08002294int Channel::SetOutputVolumePan(float left, float right) {
2295 rtc::CritScope cs(&volume_settings_critsect_);
2296 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002297 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002298 _panLeft = left;
2299 _panRight = right;
2300 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002301}
2302
kwiberg55b97fe2016-01-28 05:22:45 -08002303int Channel::GetOutputVolumePan(float& left, float& right) const {
2304 rtc::CritScope cs(&volume_settings_critsect_);
2305 left = _panLeft;
2306 right = _panRight;
2307 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002308}
2309
kwiberg55b97fe2016-01-28 05:22:45 -08002310int Channel::SetChannelOutputVolumeScaling(float scaling) {
2311 rtc::CritScope cs(&volume_settings_critsect_);
2312 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002313 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002314 _outputGain = scaling;
2315 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002316}
2317
kwiberg55b97fe2016-01-28 05:22:45 -08002318int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2319 rtc::CritScope cs(&volume_settings_critsect_);
2320 scaling = _outputGain;
2321 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002322}
2323
solenberg8842c3e2016-03-11 03:06:41 -08002324int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002325 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002326 "Channel::SendTelephoneEventOutband(...)");
2327 RTC_DCHECK_LE(0, event);
2328 RTC_DCHECK_GE(255, event);
2329 RTC_DCHECK_LE(0, duration_ms);
2330 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002331 if (!Sending()) {
2332 return -1;
2333 }
solenberg8842c3e2016-03-11 03:06:41 -08002334 if (_rtpRtcpModule->SendTelephoneEventOutband(
2335 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002336 _engineStatisticsPtr->SetLastError(
2337 VE_SEND_DTMF_FAILED, kTraceWarning,
2338 "SendTelephoneEventOutband() failed to send event");
2339 return -1;
2340 }
2341 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002342}
2343
solenbergffbbcac2016-11-17 05:25:37 -08002344int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2345 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002346 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002347 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002348 RTC_DCHECK_LE(0, payload_type);
2349 RTC_DCHECK_GE(127, payload_type);
2350 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002351 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002352 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002353 memcpy(codec.plname, "telephone-event", 16);
2354 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2355 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2356 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2357 _engineStatisticsPtr->SetLastError(
2358 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2359 "SetSendTelephoneEventPayloadType() failed to register send"
2360 "payload type");
2361 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002362 }
kwiberg55b97fe2016-01-28 05:22:45 -08002363 }
kwiberg55b97fe2016-01-28 05:22:45 -08002364 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002365}
2366
kwiberg55b97fe2016-01-28 05:22:45 -08002367int Channel::VoiceActivityIndicator(int& activity) {
2368 activity = _sendFrameType;
2369 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002370}
2371
kwiberg55b97fe2016-01-28 05:22:45 -08002372int Channel::SetLocalSSRC(unsigned int ssrc) {
2373 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2374 "Channel::SetLocalSSRC()");
2375 if (channel_state_.Get().sending) {
2376 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2377 "SetLocalSSRC() already sending");
2378 return -1;
2379 }
2380 _rtpRtcpModule->SetSSRC(ssrc);
2381 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002382}
2383
kwiberg55b97fe2016-01-28 05:22:45 -08002384int Channel::GetLocalSSRC(unsigned int& ssrc) {
2385 ssrc = _rtpRtcpModule->SSRC();
2386 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002387}
2388
kwiberg55b97fe2016-01-28 05:22:45 -08002389int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2390 ssrc = rtp_receiver_->SSRC();
2391 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002392}
2393
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002394int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002395 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002396 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002397}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002398
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002399int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2400 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002401 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2402 if (enable &&
2403 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2404 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002405 return -1;
2406 }
2407 return 0;
2408}
2409
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002410void Channel::EnableSendTransportSequenceNumber(int id) {
2411 int ret =
2412 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2413 RTC_DCHECK_EQ(0, ret);
2414}
2415
stefan3313ec92016-01-21 06:32:43 -08002416void Channel::EnableReceiveTransportSequenceNumber(int id) {
2417 rtp_header_parser_->DeregisterRtpHeaderExtension(
2418 kRtpExtensionTransportSequenceNumber);
2419 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2420 kRtpExtensionTransportSequenceNumber, id);
2421 RTC_DCHECK(ret);
2422}
2423
stefanbba9dec2016-02-01 04:39:55 -08002424void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002425 RtpPacketSender* rtp_packet_sender,
2426 TransportFeedbackObserver* transport_feedback_observer,
stefan7de8d642017-02-07 07:14:08 -08002427 PacketRouter* packet_router,
2428 RtcpBandwidthObserver* bandwidth_observer) {
stefanbba9dec2016-02-01 04:39:55 -08002429 RTC_DCHECK(rtp_packet_sender);
2430 RTC_DCHECK(transport_feedback_observer);
2431 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002432 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002433 feedback_observer_proxy_->SetTransportFeedbackObserver(
2434 transport_feedback_observer);
2435 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2436 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2437 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002438 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002439 packet_router_ = packet_router;
2440}
2441
stefanbba9dec2016-02-01 04:39:55 -08002442void Channel::RegisterReceiverCongestionControlObjects(
2443 PacketRouter* packet_router) {
2444 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002445 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002446 packet_router_ = packet_router;
2447}
2448
2449void Channel::ResetCongestionControlObjects() {
2450 RTC_DCHECK(packet_router_);
2451 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002452 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002453 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2454 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002455 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002456 packet_router_ = nullptr;
2457 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2458}
2459
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002460void Channel::SetRTCPStatus(bool enable) {
2461 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2462 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002463 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002464}
2465
kwiberg55b97fe2016-01-28 05:22:45 -08002466int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002467 RtcpMode method = _rtpRtcpModule->RTCP();
2468 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002469 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002470}
2471
kwiberg55b97fe2016-01-28 05:22:45 -08002472int Channel::SetRTCP_CNAME(const char cName[256]) {
2473 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2474 "Channel::SetRTCP_CNAME()");
2475 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2476 _engineStatisticsPtr->SetLastError(
2477 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2478 "SetRTCP_CNAME() failed to set RTCP CNAME");
2479 return -1;
2480 }
2481 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002482}
2483
kwiberg55b97fe2016-01-28 05:22:45 -08002484int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2485 if (cName == NULL) {
2486 _engineStatisticsPtr->SetLastError(
2487 VE_INVALID_ARGUMENT, kTraceError,
2488 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2489 return -1;
2490 }
2491 char cname[RTCP_CNAME_SIZE];
2492 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2493 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2494 _engineStatisticsPtr->SetLastError(
2495 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2496 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2497 return -1;
2498 }
2499 strcpy(cName, cname);
2500 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002501}
2502
kwiberg55b97fe2016-01-28 05:22:45 -08002503int Channel::SendApplicationDefinedRTCPPacket(
2504 unsigned char subType,
2505 unsigned int name,
2506 const char* data,
2507 unsigned short dataLengthInBytes) {
2508 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2509 "Channel::SendApplicationDefinedRTCPPacket()");
2510 if (!channel_state_.Get().sending) {
2511 _engineStatisticsPtr->SetLastError(
2512 VE_NOT_SENDING, kTraceError,
2513 "SendApplicationDefinedRTCPPacket() not sending");
2514 return -1;
2515 }
2516 if (NULL == data) {
2517 _engineStatisticsPtr->SetLastError(
2518 VE_INVALID_ARGUMENT, kTraceError,
2519 "SendApplicationDefinedRTCPPacket() invalid data value");
2520 return -1;
2521 }
2522 if (dataLengthInBytes % 4 != 0) {
2523 _engineStatisticsPtr->SetLastError(
2524 VE_INVALID_ARGUMENT, kTraceError,
2525 "SendApplicationDefinedRTCPPacket() invalid length value");
2526 return -1;
2527 }
2528 RtcpMode status = _rtpRtcpModule->RTCP();
2529 if (status == RtcpMode::kOff) {
2530 _engineStatisticsPtr->SetLastError(
2531 VE_RTCP_ERROR, kTraceError,
2532 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2533 return -1;
2534 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002535
kwiberg55b97fe2016-01-28 05:22:45 -08002536 // Create and schedule the RTCP APP packet for transmission
2537 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2538 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2539 _engineStatisticsPtr->SetLastError(
2540 VE_SEND_ERROR, kTraceError,
2541 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2542 return -1;
2543 }
2544 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002545}
2546
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002547int Channel::GetRemoteRTCPReportBlocks(
2548 std::vector<ReportBlock>* report_blocks) {
2549 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002550 _engineStatisticsPtr->SetLastError(
2551 VE_INVALID_ARGUMENT, kTraceError,
2552 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002553 return -1;
2554 }
2555
2556 // Get the report blocks from the latest received RTCP Sender or Receiver
2557 // Report. Each element in the vector contains the sender's SSRC and a
2558 // report block according to RFC 3550.
2559 std::vector<RTCPReportBlock> rtcp_report_blocks;
2560 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002561 return -1;
2562 }
2563
2564 if (rtcp_report_blocks.empty())
2565 return 0;
2566
2567 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2568 for (; it != rtcp_report_blocks.end(); ++it) {
2569 ReportBlock report_block;
2570 report_block.sender_SSRC = it->remoteSSRC;
2571 report_block.source_SSRC = it->sourceSSRC;
2572 report_block.fraction_lost = it->fractionLost;
2573 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2574 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2575 report_block.interarrival_jitter = it->jitter;
2576 report_block.last_SR_timestamp = it->lastSR;
2577 report_block.delay_since_last_SR = it->delaySinceLastSR;
2578 report_blocks->push_back(report_block);
2579 }
2580 return 0;
2581}
2582
kwiberg55b97fe2016-01-28 05:22:45 -08002583int Channel::GetRTPStatistics(CallStatistics& stats) {
2584 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002585
kwiberg55b97fe2016-01-28 05:22:45 -08002586 // The jitter statistics is updated for each received RTP packet and is
2587 // based on received packets.
2588 RtcpStatistics statistics;
2589 StreamStatistician* statistician =
2590 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002591 if (statistician) {
2592 statistician->GetStatistics(&statistics,
2593 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002594 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002595
kwiberg55b97fe2016-01-28 05:22:45 -08002596 stats.fractionLost = statistics.fraction_lost;
2597 stats.cumulativeLost = statistics.cumulative_lost;
2598 stats.extendedMax = statistics.extended_max_sequence_number;
2599 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002600
kwiberg55b97fe2016-01-28 05:22:45 -08002601 // --- RTT
2602 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002603
kwiberg55b97fe2016-01-28 05:22:45 -08002604 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002605
kwiberg55b97fe2016-01-28 05:22:45 -08002606 size_t bytesSent(0);
2607 uint32_t packetsSent(0);
2608 size_t bytesReceived(0);
2609 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002610
kwiberg55b97fe2016-01-28 05:22:45 -08002611 if (statistician) {
2612 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2613 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002614
kwiberg55b97fe2016-01-28 05:22:45 -08002615 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2616 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2617 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2618 " output will not be complete");
2619 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002620
kwiberg55b97fe2016-01-28 05:22:45 -08002621 stats.bytesSent = bytesSent;
2622 stats.packetsSent = packetsSent;
2623 stats.bytesReceived = bytesReceived;
2624 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002625
kwiberg55b97fe2016-01-28 05:22:45 -08002626 // --- Timestamps
2627 {
2628 rtc::CritScope lock(&ts_stats_lock_);
2629 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2630 }
2631 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002632}
2633
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002634int Channel::SetCodecFECStatus(bool enable) {
2635 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2636 "Channel::SetCodecFECStatus()");
2637
kwibergc8d071e2016-04-06 12:22:38 -07002638 if (!codec_manager_.SetCodecFEC(enable) ||
2639 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002640 _engineStatisticsPtr->SetLastError(
2641 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2642 "SetCodecFECStatus() failed to set FEC state");
2643 return -1;
2644 }
2645 return 0;
2646}
2647
2648bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002649 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002650}
2651
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002652void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2653 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002654 // If pacing is enabled we always store packets.
2655 if (!pacing_enabled_)
2656 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002657 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002658 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002659 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002660 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002661 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002662}
2663
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002664// Called when we are missing one or more packets.
2665int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002666 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2667}
2668
kwiberg55b97fe2016-01-28 05:22:45 -08002669uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2670 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2671 "Channel::Demultiplex()");
2672 _audioFrame.CopyFrom(audioFrame);
2673 _audioFrame.id_ = _channelId;
2674 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002675}
2676
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002677void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002678 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002679 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002680 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002681 CodecInst codec;
2682 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002683
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002684 // Never upsample or upmix the capture signal here. This should be done at the
2685 // end of the send chain.
2686 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2687 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2688 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2689 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002690}
2691
kwiberg55b97fe2016-01-28 05:22:45 -08002692uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2693 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2694 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002695
kwiberg55b97fe2016-01-28 05:22:45 -08002696 if (_audioFrame.samples_per_channel_ == 0) {
2697 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2698 "Channel::PrepareEncodeAndSend() invalid audio frame");
2699 return 0xFFFFFFFF;
2700 }
2701
2702 if (channel_state_.Get().input_file_playing) {
2703 MixOrReplaceAudioWithFile(mixingFrequency);
2704 }
2705
solenberg1c2af8e2016-03-24 10:36:00 -07002706 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2707 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002708
kwiberg55b97fe2016-01-28 05:22:45 -08002709 if (_includeAudioLevelIndication) {
2710 size_t length =
2711 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002712 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002713 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002714 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002715 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002716 rms_level_.Analyze(
2717 rtc::ArrayView<const int16_t>(_audioFrame.data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002718 }
kwiberg55b97fe2016-01-28 05:22:45 -08002719 }
solenberg1c2af8e2016-03-24 10:36:00 -07002720 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002721
kwiberg55b97fe2016-01-28 05:22:45 -08002722 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002723}
2724
kwiberg55b97fe2016-01-28 05:22:45 -08002725uint32_t Channel::EncodeAndSend() {
2726 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2727 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002728
kwiberg55b97fe2016-01-28 05:22:45 -08002729 assert(_audioFrame.num_channels_ <= 2);
2730 if (_audioFrame.samples_per_channel_ == 0) {
2731 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2732 "Channel::EncodeAndSend() invalid audio frame");
2733 return 0xFFFFFFFF;
2734 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002735
kwiberg55b97fe2016-01-28 05:22:45 -08002736 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002737
kwiberg55b97fe2016-01-28 05:22:45 -08002738 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002739
kwiberg55b97fe2016-01-28 05:22:45 -08002740 // The ACM resamples internally.
2741 _audioFrame.timestamp_ = _timeStamp;
2742 // This call will trigger AudioPacketizationCallback::SendData if encoding
2743 // is done and payload is ready for packetization and transmission.
2744 // Otherwise, it will return without invoking the callback.
2745 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2746 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2747 "Channel::EncodeAndSend() ACM encoding failed");
2748 return 0xFFFFFFFF;
2749 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002750
kwiberg55b97fe2016-01-28 05:22:45 -08002751 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2752 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002753}
2754
solenberg7602aab2016-11-14 11:30:07 -08002755void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2756 RTC_DCHECK(!channel.channel() ||
2757 channel.channel()->ChannelId() != _channelId);
2758 rtc::CritScope lock(&assoc_send_channel_lock_);
2759 associate_send_channel_ = channel;
2760}
2761
Minyue2013aec2015-05-13 14:14:42 +02002762void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002763 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002764 Channel* channel = associate_send_channel_.channel();
2765 if (channel && channel->ChannelId() == channel_id) {
2766 // If this channel is associated with a send channel of the specified
2767 // Channel ID, disassociate with it.
2768 ChannelOwner ref(NULL);
2769 associate_send_channel_ = ref;
2770 }
2771}
2772
ivoc14d5dbe2016-07-04 07:06:55 -07002773void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2774 event_log_proxy_->SetEventLog(event_log);
2775}
2776
michaelt9332b7d2016-11-30 07:51:13 -08002777void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2778 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2779}
2780
nisse284542b2017-01-10 08:58:32 -08002781void Channel::UpdateOverheadForEncoder() {
2782 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2783 if (*encoder) {
2784 (*encoder)->OnReceivedOverhead(transport_overhead_per_packet_ +
2785 rtp_overhead_per_packet_);
2786 }
2787 });
2788}
2789
2790void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
2791 transport_overhead_per_packet_ = transport_overhead_per_packet;
2792 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002793}
2794
michaeltbf65be52016-12-15 06:24:49 -08002795void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
nisse284542b2017-01-10 08:58:32 -08002796 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2797 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002798}
2799
kwiberg55b97fe2016-01-28 05:22:45 -08002800int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2801 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002802}
2803
wu@webrtc.org24301a62013-12-13 19:17:43 +00002804void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2805 audio_coding_->GetDecodingCallStatistics(stats);
2806}
2807
solenberg358057b2015-11-27 10:46:42 -08002808uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002809 rtc::CritScope lock(&video_sync_lock_);
2810 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002811}
2812
kwiberg55b97fe2016-01-28 05:22:45 -08002813int Channel::SetMinimumPlayoutDelay(int delayMs) {
2814 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2815 "Channel::SetMinimumPlayoutDelay()");
2816 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2817 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2818 _engineStatisticsPtr->SetLastError(
2819 VE_INVALID_ARGUMENT, kTraceError,
2820 "SetMinimumPlayoutDelay() invalid min delay");
2821 return -1;
2822 }
2823 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2824 _engineStatisticsPtr->SetLastError(
2825 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2826 "SetMinimumPlayoutDelay() failed to set min playout delay");
2827 return -1;
2828 }
2829 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002830}
2831
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002832int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002833 uint32_t playout_timestamp_rtp = 0;
2834 {
tommi31fc21f2016-01-21 10:37:37 -08002835 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002836 playout_timestamp_rtp = playout_timestamp_rtp_;
2837 }
kwiberg55b97fe2016-01-28 05:22:45 -08002838 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002839 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002840 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002841 "GetPlayoutTimestamp() failed to retrieve timestamp");
2842 return -1;
2843 }
deadbeef74375882015-08-13 12:09:10 -07002844 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002845 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002846}
2847
kwiberg55b97fe2016-01-28 05:22:45 -08002848int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2849 RtpReceiver** rtp_receiver) const {
2850 *rtpRtcpModule = _rtpRtcpModule.get();
2851 *rtp_receiver = rtp_receiver_.get();
2852 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002853}
2854
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002855// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2856// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002857int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002858 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002859 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002860
kwiberg55b97fe2016-01-28 05:22:45 -08002861 {
2862 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002863
kwiberg5a25d952016-08-17 07:31:12 -07002864 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002865 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2866 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2867 " doesnt exist");
2868 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002869 }
2870
kwiberg4ec01d92016-08-22 08:43:54 -07002871 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002872 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002873 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2874 "Channel::MixOrReplaceAudioWithFile() file mixing "
2875 "failed");
2876 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002877 }
kwiberg55b97fe2016-01-28 05:22:45 -08002878 if (fileSamples == 0) {
2879 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2880 "Channel::MixOrReplaceAudioWithFile() file is ended");
2881 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002882 }
kwiberg55b97fe2016-01-28 05:22:45 -08002883 }
2884
2885 assert(_audioFrame.samples_per_channel_ == fileSamples);
2886
2887 if (_mixFileWithMicrophone) {
2888 // Currently file stream is always mono.
2889 // TODO(xians): Change the code when FilePlayer supports real stereo.
2890 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
2891 1, fileSamples);
2892 } else {
2893 // Replace ACM audio with file.
2894 // Currently file stream is always mono.
2895 // TODO(xians): Change the code when FilePlayer supports real stereo.
2896 _audioFrame.UpdateFrame(
2897 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2898 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2899 }
2900 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002901}
2902
kwiberg55b97fe2016-01-28 05:22:45 -08002903int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2904 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002905
kwibergb7f89d62016-02-17 10:04:18 -08002906 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002907 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002908
kwiberg55b97fe2016-01-28 05:22:45 -08002909 {
2910 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002911
kwiberg5a25d952016-08-17 07:31:12 -07002912 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002913 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2914 "Channel::MixAudioWithFile() file mixing failed");
2915 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002916 }
2917
kwiberg55b97fe2016-01-28 05:22:45 -08002918 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002919 if (output_file_player_->Get10msAudioFromFile(
2920 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002921 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2922 "Channel::MixAudioWithFile() file mixing failed");
2923 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002924 }
kwiberg55b97fe2016-01-28 05:22:45 -08002925 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002926
kwiberg55b97fe2016-01-28 05:22:45 -08002927 if (audioFrame.samples_per_channel_ == fileSamples) {
2928 // Currently file stream is always mono.
2929 // TODO(xians): Change the code when FilePlayer supports real stereo.
2930 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
2931 fileSamples);
2932 } else {
2933 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2934 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2935 ") != "
2936 "fileSamples(%" PRIuS ")",
2937 audioFrame.samples_per_channel_, fileSamples);
2938 return -1;
2939 }
2940
2941 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002942}
2943
deadbeef74375882015-08-13 12:09:10 -07002944void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002945 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002946
henrik.lundin96bd5022016-04-06 04:13:56 -07002947 if (!jitter_buffer_playout_timestamp_) {
2948 // This can happen if this channel has not received any RTP packets. In
2949 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07002950 return;
2951 }
2952
2953 uint16_t delay_ms = 0;
2954 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002955 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002956 "Channel::UpdatePlayoutTimestamp() failed to read playout"
2957 " delay from the ADM");
2958 _engineStatisticsPtr->SetLastError(
2959 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
2960 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
2961 return;
2962 }
2963
henrik.lundin96bd5022016-04-06 04:13:56 -07002964 RTC_DCHECK(jitter_buffer_playout_timestamp_);
2965 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07002966
2967 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07002968 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07002969
kwiberg55b97fe2016-01-28 05:22:45 -08002970 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002971 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07002972 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07002973
2974 {
tommi31fc21f2016-01-21 10:37:37 -08002975 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08002976 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002977 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07002978 }
2979 playout_delay_ms_ = delay_ms;
2980 }
2981}
2982
kwiberg55b97fe2016-01-28 05:22:45 -08002983void Channel::RegisterReceiveCodecsToRTPModule() {
2984 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2985 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002986
kwiberg55b97fe2016-01-28 05:22:45 -08002987 CodecInst codec;
2988 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002989
kwiberg55b97fe2016-01-28 05:22:45 -08002990 for (int idx = 0; idx < nSupportedCodecs; idx++) {
2991 // Open up the RTP/RTCP receiver for all supported codecs
2992 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08002993 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08002994 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2995 "Channel::RegisterReceiveCodecsToRTPModule() unable"
2996 " to register %s (%d/%d/%" PRIuS
2997 "/%d) to RTP/RTCP "
2998 "receiver",
2999 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3000 codec.rate);
3001 } else {
3002 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3003 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3004 "(%d/%d/%" PRIuS
3005 "/%d) has been added to the RTP/RTCP "
3006 "receiver",
3007 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3008 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003009 }
kwiberg55b97fe2016-01-28 05:22:45 -08003010 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003011}
3012
kwiberg55b97fe2016-01-28 05:22:45 -08003013int Channel::SetSendRtpHeaderExtension(bool enable,
3014 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003015 unsigned char id) {
3016 int error = 0;
3017 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3018 if (enable) {
3019 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3020 }
3021 return error;
3022}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003023
ossue280cde2016-10-12 11:04:10 -07003024int Channel::GetRtpTimestampRateHz() const {
3025 const auto format = audio_coding_->ReceiveFormat();
3026 // Default to the playout frequency if we've not gotten any packets yet.
3027 // TODO(ossu): Zero clockrate can only happen if we've added an external
3028 // decoder for a format we don't support internally. Remove once that way of
3029 // adding decoders is gone!
3030 return (format && format->clockrate_hz != 0)
3031 ? format->clockrate_hz
3032 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003033}
3034
Minyue2013aec2015-05-13 14:14:42 +02003035int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003036 RtcpMode method = _rtpRtcpModule->RTCP();
3037 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003038 return 0;
3039 }
3040 std::vector<RTCPReportBlock> report_blocks;
3041 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003042
3043 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003044 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003045 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003046 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003047 Channel* channel = associate_send_channel_.channel();
3048 // Tries to get RTT from an associated channel. This is important for
3049 // receive-only channels.
3050 if (channel) {
3051 // To prevent infinite recursion and deadlock, calling GetRTT of
3052 // associate channel should always use "false" for argument:
3053 // |allow_associate_channel|.
3054 rtt = channel->GetRTT(false);
3055 }
3056 }
3057 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003058 }
3059
3060 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3061 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3062 for (; it != report_blocks.end(); ++it) {
3063 if (it->remoteSSRC == remoteSSRC)
3064 break;
3065 }
3066 if (it == report_blocks.end()) {
3067 // We have not received packets with SSRC matching the report blocks.
3068 // To calculate RTT we try with the SSRC of the first report block.
3069 // This is very important for send-only channels where we don't know
3070 // the SSRC of the other end.
3071 remoteSSRC = report_blocks[0].remoteSSRC;
3072 }
Minyue2013aec2015-05-13 14:14:42 +02003073
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003074 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003075 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003076 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003077 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3078 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003079 return 0;
3080 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003081 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003082}
3083
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003084} // namespace voe
3085} // namespace webrtc