blob: 60e213d291ece9f4147438c106d51baf4c0be1fc [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
kwiberg09f090c2017-03-01 01:57:11 -08001380 const CodecInst codec = SdpToCodecInst(payload_type, format);
kwibergd32bf752017-01-19 07:03:59 -08001381
1382 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001383 // De-register the selected codec (RTP/RTCP module and ACM)
1384
1385 int8_t pltype(-1);
1386 CodecInst rxCodec = codec;
1387
1388 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001389 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001390 rxCodec.pltype = pltype;
1391
1392 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1393 _engineStatisticsPtr->SetLastError(
1394 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1395 "SetRecPayloadType() RTP/RTCP-module deregistration "
1396 "failed");
1397 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001398 }
kwiberg55b97fe2016-01-28 05:22:45 -08001399 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1400 _engineStatisticsPtr->SetLastError(
1401 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1402 "SetRecPayloadType() ACM deregistration failed - 1");
1403 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001404 }
kwiberg55b97fe2016-01-28 05:22:45 -08001405 return 0;
1406 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001407
magjed56124bd2016-11-24 09:34:46 -08001408 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001409 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001410 // TODO(kwiberg): Retrying is probably not necessary, since
1411 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001412 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001413 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001414 _engineStatisticsPtr->SetLastError(
1415 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1416 "SetRecPayloadType() RTP/RTCP-module registration failed");
1417 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001418 }
kwiberg55b97fe2016-01-28 05:22:45 -08001419 }
kwibergd32bf752017-01-19 07:03:59 -08001420 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1421 audio_coding_->UnregisterReceiveCodec(payload_type);
1422 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001423 _engineStatisticsPtr->SetLastError(
1424 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1425 "SetRecPayloadType() ACM registration failed - 1");
1426 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001427 }
kwiberg55b97fe2016-01-28 05:22:45 -08001428 }
1429 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001430}
1431
kwiberg55b97fe2016-01-28 05:22:45 -08001432int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1433 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001434 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001435 _engineStatisticsPtr->SetLastError(
1436 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1437 "GetRecPayloadType() failed to retrieve RX payload type");
1438 return -1;
1439 }
1440 codec.pltype = payloadType;
1441 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001442}
1443
kwiberg55b97fe2016-01-28 05:22:45 -08001444int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1445 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1446 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001447
kwiberg55b97fe2016-01-28 05:22:45 -08001448 CodecInst codec;
1449 int32_t samplingFreqHz(-1);
1450 const size_t kMono = 1;
1451 if (frequency == kFreq32000Hz)
1452 samplingFreqHz = 32000;
1453 else if (frequency == kFreq16000Hz)
1454 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001455
kwiberg55b97fe2016-01-28 05:22:45 -08001456 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1457 _engineStatisticsPtr->SetLastError(
1458 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1459 "SetSendCNPayloadType() failed to retrieve default CN codec "
1460 "settings");
1461 return -1;
1462 }
1463
1464 // Modify the payload type (must be set to dynamic range)
1465 codec.pltype = type;
1466
kwibergc8d071e2016-04-06 12:22:38 -07001467 if (!codec_manager_.RegisterEncoder(codec) ||
1468 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001469 _engineStatisticsPtr->SetLastError(
1470 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1471 "SetSendCNPayloadType() failed to register CN to ACM");
1472 return -1;
1473 }
1474
1475 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1476 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1477 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1478 _engineStatisticsPtr->SetLastError(
1479 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1480 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1481 "module");
1482 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001483 }
kwiberg55b97fe2016-01-28 05:22:45 -08001484 }
1485 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001486}
1487
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001488int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001489 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001490 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001491
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001492 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001493 _engineStatisticsPtr->SetLastError(
1494 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001495 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001496 return -1;
1497 }
1498 return 0;
1499}
1500
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001501int Channel::SetOpusDtx(bool enable_dtx) {
1502 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1503 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001504 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001505 : audio_coding_->DisableOpusDtx();
1506 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001507 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1508 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001509 return -1;
1510 }
1511 return 0;
1512}
1513
ivoc85228d62016-07-27 04:53:47 -07001514int Channel::GetOpusDtx(bool* enabled) {
1515 int success = -1;
1516 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1517 if (encoder) {
1518 *enabled = encoder->GetDtx();
1519 success = 0;
1520 }
1521 });
1522 return success;
1523}
1524
minyue7e304322016-10-12 05:00:55 -07001525bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1526 bool success = false;
1527 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1528 if (*encoder) {
1529 success = (*encoder)->EnableAudioNetworkAdaptor(
michaeltbf279fc2017-01-13 06:02:29 -08001530 config_string, event_log_proxy_.get(), Clock::GetRealTimeClock());
minyue7e304322016-10-12 05:00:55 -07001531 }
1532 });
1533 return success;
1534}
1535
1536void Channel::DisableAudioNetworkAdaptor() {
1537 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1538 if (*encoder)
1539 (*encoder)->DisableAudioNetworkAdaptor();
1540 });
1541}
1542
1543void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1544 int max_frame_length_ms) {
1545 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1546 if (*encoder) {
1547 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1548 max_frame_length_ms);
1549 }
1550 });
1551}
1552
mflodman3d7db262016-04-29 00:57:13 -07001553int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001554 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001555 "Channel::RegisterExternalTransport()");
1556
kwiberg55b97fe2016-01-28 05:22:45 -08001557 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001558 if (_externalTransport) {
1559 _engineStatisticsPtr->SetLastError(
1560 VE_INVALID_OPERATION, kTraceError,
1561 "RegisterExternalTransport() external transport already enabled");
1562 return -1;
1563 }
1564 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001565 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001566 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001567}
1568
kwiberg55b97fe2016-01-28 05:22:45 -08001569int32_t Channel::DeRegisterExternalTransport() {
1570 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1571 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001572
kwiberg55b97fe2016-01-28 05:22:45 -08001573 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001574 if (_transportPtr) {
1575 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1576 "DeRegisterExternalTransport() all transport is disabled");
1577 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001578 _engineStatisticsPtr->SetLastError(
1579 VE_INVALID_OPERATION, kTraceWarning,
1580 "DeRegisterExternalTransport() external transport already "
1581 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001582 }
1583 _externalTransport = false;
1584 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001585 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001586}
1587
nisse657bab22017-02-21 06:28:10 -08001588// TODO(nisse): Delete this method together with ReceivedRTPPacket.
1589// It's a temporary hack to support both ReceivedRTPPacket and
1590// OnRtpPacket interfaces without too much code duplication.
1591bool Channel::OnRtpPacketWithHeader(const uint8_t* received_packet,
1592 size_t length,
1593 RTPHeader *header) {
1594 // Store playout timestamp for the received RTP packet
1595 UpdatePlayoutTimestamp(false);
1596
1597 header->payload_type_frequency =
1598 rtp_payload_registry_->GetPayloadTypeFrequency(header->payloadType);
1599 if (header->payload_type_frequency < 0)
1600 return false;
1601 bool in_order = IsPacketInOrder(*header);
1602 rtp_receive_statistics_->IncomingPacket(
1603 *header, length, IsPacketRetransmitted(*header, in_order));
1604 rtp_payload_registry_->SetIncomingPayloadType(*header);
1605
1606 return ReceivePacket(received_packet, length, *header, in_order);
1607}
1608
mflodman3d7db262016-04-29 00:57:13 -07001609int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001610 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001611 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001612 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001613 "Channel::ReceivedRTPPacket()");
1614
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001615 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001616 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1617 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1618 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001619 return -1;
1620 }
nisse657bab22017-02-21 06:28:10 -08001621 return OnRtpPacketWithHeader(received_packet, length, &header) ? 0 : -1;
1622}
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001623
nisse657bab22017-02-21 06:28:10 -08001624void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
1625 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
1626 "Channel::ReceivedRTPPacket()");
1627
1628 RTPHeader header;
1629 packet.GetHeader(&header);
1630 OnRtpPacketWithHeader(packet.data(), packet.size(), &header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001631}
1632
1633bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001634 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001635 const RTPHeader& header,
1636 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001637 if (rtp_payload_registry_->IsRtx(header)) {
1638 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001639 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001640 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001641 assert(packet_length >= header.headerLength);
1642 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001643 PayloadUnion payload_specific;
1644 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001645 &payload_specific)) {
1646 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001647 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001648 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1649 payload_specific, in_order);
1650}
1651
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001652bool Channel::HandleRtxPacket(const uint8_t* packet,
1653 size_t packet_length,
1654 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001655 if (!rtp_payload_registry_->IsRtx(header))
1656 return false;
1657
1658 // Remove the RTX header and parse the original RTP header.
1659 if (packet_length < header.headerLength)
1660 return false;
1661 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1662 return false;
1663 if (restored_packet_in_use_) {
1664 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1665 "Multiple RTX headers detected, dropping packet");
1666 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001667 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001668 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001669 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1670 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001671 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1672 "Incoming RTX packet: invalid RTP header");
1673 return false;
1674 }
1675 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001676 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001677 restored_packet_in_use_ = false;
1678 return ret;
1679}
1680
1681bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1682 StreamStatistician* statistician =
1683 rtp_receive_statistics_->GetStatistician(header.ssrc);
1684 if (!statistician)
1685 return false;
1686 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001687}
1688
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001689bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1690 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001691 // Retransmissions are handled separately if RTX is enabled.
1692 if (rtp_payload_registry_->RtxEnabled())
1693 return false;
1694 StreamStatistician* statistician =
1695 rtp_receive_statistics_->GetStatistician(header.ssrc);
1696 if (!statistician)
1697 return false;
1698 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001699 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001700 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001701 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001702}
1703
mflodman3d7db262016-04-29 00:57:13 -07001704int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001705 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001706 "Channel::ReceivedRTCPPacket()");
1707 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001708 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001709
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001710 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001711 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001712 _engineStatisticsPtr->SetLastError(
1713 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1714 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1715 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001716
Minyue2013aec2015-05-13 14:14:42 +02001717 int64_t rtt = GetRTT(true);
1718 if (rtt == 0) {
1719 // Waiting for valid RTT.
1720 return 0;
1721 }
Erik Språng737336d2016-07-29 12:59:36 +02001722
1723 int64_t nack_window_ms = rtt;
1724 if (nack_window_ms < kMinRetransmissionWindowMs) {
1725 nack_window_ms = kMinRetransmissionWindowMs;
1726 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1727 nack_window_ms = kMaxRetransmissionWindowMs;
1728 }
1729 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1730
minyue7e304322016-10-12 05:00:55 -07001731 // Invoke audio encoders OnReceivedRtt().
1732 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1733 if (*encoder)
1734 (*encoder)->OnReceivedRtt(rtt);
1735 });
1736
Minyue2013aec2015-05-13 14:14:42 +02001737 uint32_t ntp_secs = 0;
1738 uint32_t ntp_frac = 0;
1739 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001740 if (0 !=
1741 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1742 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001743 // Waiting for RTCP.
1744 return 0;
1745 }
1746
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001747 {
tommi31fc21f2016-01-21 10:37:37 -08001748 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001749 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001750 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001751 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001752}
1753
niklase@google.com470e71d2011-07-07 08:21:25 +00001754int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001755 bool loop,
1756 FileFormats format,
1757 int startPosition,
1758 float volumeScaling,
1759 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001760 const CodecInst* codecInst) {
1761 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1762 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1763 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1764 "stopPosition=%d)",
1765 fileName, loop, format, volumeScaling, startPosition,
1766 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001767
kwiberg55b97fe2016-01-28 05:22:45 -08001768 if (channel_state_.Get().output_file_playing) {
1769 _engineStatisticsPtr->SetLastError(
1770 VE_ALREADY_PLAYING, kTraceError,
1771 "StartPlayingFileLocally() is already playing");
1772 return -1;
1773 }
1774
1775 {
1776 rtc::CritScope cs(&_fileCritSect);
1777
kwiberg5a25d952016-08-17 07:31:12 -07001778 if (output_file_player_) {
1779 output_file_player_->RegisterModuleFileCallback(NULL);
1780 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001781 }
1782
kwiberg5b356f42016-09-08 04:32:33 -07001783 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001784 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001785
kwiberg5a25d952016-08-17 07:31:12 -07001786 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001787 _engineStatisticsPtr->SetLastError(
1788 VE_INVALID_ARGUMENT, kTraceError,
1789 "StartPlayingFileLocally() filePlayer format is not correct");
1790 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001791 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001792
kwiberg55b97fe2016-01-28 05:22:45 -08001793 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001794
kwiberg5a25d952016-08-17 07:31:12 -07001795 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001796 fileName, loop, startPosition, volumeScaling, notificationTime,
1797 stopPosition, (const CodecInst*)codecInst) != 0) {
1798 _engineStatisticsPtr->SetLastError(
1799 VE_BAD_FILE, kTraceError,
1800 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001801 output_file_player_->StopPlayingFile();
1802 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001803 return -1;
1804 }
kwiberg5a25d952016-08-17 07:31:12 -07001805 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001806 channel_state_.SetOutputFilePlaying(true);
1807 }
1808
1809 if (RegisterFilePlayingToMixer() != 0)
1810 return -1;
1811
1812 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001813}
1814
1815int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001816 FileFormats format,
1817 int startPosition,
1818 float volumeScaling,
1819 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001820 const CodecInst* codecInst) {
1821 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1822 "Channel::StartPlayingFileLocally(format=%d,"
1823 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1824 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001825
kwiberg55b97fe2016-01-28 05:22:45 -08001826 if (stream == NULL) {
1827 _engineStatisticsPtr->SetLastError(
1828 VE_BAD_FILE, kTraceError,
1829 "StartPlayingFileLocally() NULL as input stream");
1830 return -1;
1831 }
1832
1833 if (channel_state_.Get().output_file_playing) {
1834 _engineStatisticsPtr->SetLastError(
1835 VE_ALREADY_PLAYING, kTraceError,
1836 "StartPlayingFileLocally() is already playing");
1837 return -1;
1838 }
1839
1840 {
1841 rtc::CritScope cs(&_fileCritSect);
1842
1843 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001844 if (output_file_player_) {
1845 output_file_player_->RegisterModuleFileCallback(NULL);
1846 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001847 }
1848
kwiberg55b97fe2016-01-28 05:22:45 -08001849 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001850 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001851 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001852
kwiberg5a25d952016-08-17 07:31:12 -07001853 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001854 _engineStatisticsPtr->SetLastError(
1855 VE_INVALID_ARGUMENT, kTraceError,
1856 "StartPlayingFileLocally() filePlayer format isnot correct");
1857 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001858 }
1859
kwiberg55b97fe2016-01-28 05:22:45 -08001860 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001861
kwiberg4ec01d92016-08-22 08:43:54 -07001862 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001863 volumeScaling, notificationTime,
1864 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001865 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1866 "StartPlayingFile() failed to "
1867 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001868 output_file_player_->StopPlayingFile();
1869 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001870 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001871 }
kwiberg5a25d952016-08-17 07:31:12 -07001872 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001873 channel_state_.SetOutputFilePlaying(true);
1874 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001875
kwiberg55b97fe2016-01-28 05:22:45 -08001876 if (RegisterFilePlayingToMixer() != 0)
1877 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001878
kwiberg55b97fe2016-01-28 05:22:45 -08001879 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001880}
1881
kwiberg55b97fe2016-01-28 05:22:45 -08001882int Channel::StopPlayingFileLocally() {
1883 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1884 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001885
kwiberg55b97fe2016-01-28 05:22:45 -08001886 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001887 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001888 }
1889
1890 {
1891 rtc::CritScope cs(&_fileCritSect);
1892
kwiberg5a25d952016-08-17 07:31:12 -07001893 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001894 _engineStatisticsPtr->SetLastError(
1895 VE_STOP_RECORDING_FAILED, kTraceError,
1896 "StopPlayingFile() could not stop playing");
1897 return -1;
1898 }
kwiberg5a25d952016-08-17 07:31:12 -07001899 output_file_player_->RegisterModuleFileCallback(NULL);
1900 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001901 channel_state_.SetOutputFilePlaying(false);
1902 }
1903 // _fileCritSect cannot be taken while calling
1904 // SetAnonymousMixibilityStatus. Refer to comments in
1905 // StartPlayingFileLocally(const char* ...) for more details.
1906 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1907 _engineStatisticsPtr->SetLastError(
1908 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1909 "StopPlayingFile() failed to stop participant from playing as"
1910 "file in the mixer");
1911 return -1;
1912 }
1913
1914 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001915}
1916
kwiberg55b97fe2016-01-28 05:22:45 -08001917int Channel::IsPlayingFileLocally() const {
1918 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001919}
1920
kwiberg55b97fe2016-01-28 05:22:45 -08001921int Channel::RegisterFilePlayingToMixer() {
1922 // Return success for not registering for file playing to mixer if:
1923 // 1. playing file before playout is started on that channel.
1924 // 2. starting playout without file playing on that channel.
1925 if (!channel_state_.Get().playing ||
1926 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001927 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001928 }
1929
1930 // |_fileCritSect| cannot be taken while calling
1931 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1932 // frames can be pulled by the mixer. Since the frames are generated from
1933 // the file, _fileCritSect will be taken. This would result in a deadlock.
1934 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1935 channel_state_.SetOutputFilePlaying(false);
1936 rtc::CritScope cs(&_fileCritSect);
1937 _engineStatisticsPtr->SetLastError(
1938 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1939 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001940 output_file_player_->StopPlayingFile();
1941 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001942 return -1;
1943 }
1944
1945 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001946}
1947
niklase@google.com470e71d2011-07-07 08:21:25 +00001948int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001949 bool loop,
1950 FileFormats format,
1951 int startPosition,
1952 float volumeScaling,
1953 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001954 const CodecInst* codecInst) {
1955 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1956 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1957 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1958 "stopPosition=%d)",
1959 fileName, loop, format, volumeScaling, startPosition,
1960 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001961
kwiberg55b97fe2016-01-28 05:22:45 -08001962 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001963
kwiberg55b97fe2016-01-28 05:22:45 -08001964 if (channel_state_.Get().input_file_playing) {
1965 _engineStatisticsPtr->SetLastError(
1966 VE_ALREADY_PLAYING, kTraceWarning,
1967 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001968 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001969 }
1970
1971 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001972 if (input_file_player_) {
1973 input_file_player_->RegisterModuleFileCallback(NULL);
1974 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001975 }
1976
1977 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001978 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001979 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001980
kwiberg5a25d952016-08-17 07:31:12 -07001981 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001982 _engineStatisticsPtr->SetLastError(
1983 VE_INVALID_ARGUMENT, kTraceError,
1984 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1985 return -1;
1986 }
1987
1988 const uint32_t notificationTime(0);
1989
kwiberg5a25d952016-08-17 07:31:12 -07001990 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001991 fileName, loop, startPosition, volumeScaling, notificationTime,
1992 stopPosition, (const CodecInst*)codecInst) != 0) {
1993 _engineStatisticsPtr->SetLastError(
1994 VE_BAD_FILE, kTraceError,
1995 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001996 input_file_player_->StopPlayingFile();
1997 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001998 return -1;
1999 }
kwiberg5a25d952016-08-17 07:31:12 -07002000 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002001 channel_state_.SetInputFilePlaying(true);
2002
2003 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002004}
2005
2006int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002007 FileFormats format,
2008 int startPosition,
2009 float volumeScaling,
2010 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002011 const CodecInst* codecInst) {
2012 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2013 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2014 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2015 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002016
kwiberg55b97fe2016-01-28 05:22:45 -08002017 if (stream == NULL) {
2018 _engineStatisticsPtr->SetLastError(
2019 VE_BAD_FILE, kTraceError,
2020 "StartPlayingFileAsMicrophone NULL as input stream");
2021 return -1;
2022 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002023
kwiberg55b97fe2016-01-28 05:22:45 -08002024 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002025
kwiberg55b97fe2016-01-28 05:22:45 -08002026 if (channel_state_.Get().input_file_playing) {
2027 _engineStatisticsPtr->SetLastError(
2028 VE_ALREADY_PLAYING, kTraceWarning,
2029 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002030 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002031 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002032
kwiberg55b97fe2016-01-28 05:22:45 -08002033 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002034 if (input_file_player_) {
2035 input_file_player_->RegisterModuleFileCallback(NULL);
2036 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002037 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002038
kwiberg55b97fe2016-01-28 05:22:45 -08002039 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002040 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002041 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002042
kwiberg5a25d952016-08-17 07:31:12 -07002043 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002044 _engineStatisticsPtr->SetLastError(
2045 VE_INVALID_ARGUMENT, kTraceError,
2046 "StartPlayingInputFile() filePlayer format isnot correct");
2047 return -1;
2048 }
2049
2050 const uint32_t notificationTime(0);
2051
kwiberg4ec01d92016-08-22 08:43:54 -07002052 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2053 notificationTime, stopPosition,
2054 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002055 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2056 "StartPlayingFile() failed to start "
2057 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002058 input_file_player_->StopPlayingFile();
2059 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002060 return -1;
2061 }
2062
kwiberg5a25d952016-08-17 07:31:12 -07002063 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002064 channel_state_.SetInputFilePlaying(true);
2065
2066 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002067}
2068
kwiberg55b97fe2016-01-28 05:22:45 -08002069int Channel::StopPlayingFileAsMicrophone() {
2070 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2071 "Channel::StopPlayingFileAsMicrophone()");
2072
2073 rtc::CritScope cs(&_fileCritSect);
2074
2075 if (!channel_state_.Get().input_file_playing) {
2076 return 0;
2077 }
2078
kwiberg5a25d952016-08-17 07:31:12 -07002079 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002080 _engineStatisticsPtr->SetLastError(
2081 VE_STOP_RECORDING_FAILED, kTraceError,
2082 "StopPlayingFile() could not stop playing");
2083 return -1;
2084 }
kwiberg5a25d952016-08-17 07:31:12 -07002085 input_file_player_->RegisterModuleFileCallback(NULL);
2086 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002087 channel_state_.SetInputFilePlaying(false);
2088
2089 return 0;
2090}
2091
2092int Channel::IsPlayingFileAsMicrophone() const {
2093 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002094}
2095
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002096int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002097 const CodecInst* codecInst) {
2098 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2099 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002100
kwiberg55b97fe2016-01-28 05:22:45 -08002101 if (_outputFileRecording) {
2102 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2103 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002104 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002105 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002106
kwiberg55b97fe2016-01-28 05:22:45 -08002107 FileFormats format;
2108 const uint32_t notificationTime(0); // Not supported in VoE
2109 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002110
kwiberg55b97fe2016-01-28 05:22:45 -08002111 if ((codecInst != NULL) &&
2112 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2113 _engineStatisticsPtr->SetLastError(
2114 VE_BAD_ARGUMENT, kTraceError,
2115 "StartRecordingPlayout() invalid compression");
2116 return (-1);
2117 }
2118 if (codecInst == NULL) {
2119 format = kFileFormatPcm16kHzFile;
2120 codecInst = &dummyCodec;
2121 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2122 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2123 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2124 format = kFileFormatWavFile;
2125 } else {
2126 format = kFileFormatCompressedFile;
2127 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002128
kwiberg55b97fe2016-01-28 05:22:45 -08002129 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002130
kwiberg55b97fe2016-01-28 05:22:45 -08002131 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002132 if (output_file_recorder_) {
2133 output_file_recorder_->RegisterModuleFileCallback(NULL);
2134 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002135 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002136
kwiberg5a25d952016-08-17 07:31:12 -07002137 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002138 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002139 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002140 _engineStatisticsPtr->SetLastError(
2141 VE_INVALID_ARGUMENT, kTraceError,
2142 "StartRecordingPlayout() fileRecorder format isnot correct");
2143 return -1;
2144 }
2145
kwiberg5a25d952016-08-17 07:31:12 -07002146 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002147 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2148 _engineStatisticsPtr->SetLastError(
2149 VE_BAD_FILE, kTraceError,
2150 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002151 output_file_recorder_->StopRecording();
2152 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002153 return -1;
2154 }
kwiberg5a25d952016-08-17 07:31:12 -07002155 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002156 _outputFileRecording = true;
2157
2158 return 0;
2159}
2160
2161int Channel::StartRecordingPlayout(OutStream* stream,
2162 const CodecInst* codecInst) {
2163 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2164 "Channel::StartRecordingPlayout()");
2165
2166 if (_outputFileRecording) {
2167 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2168 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002169 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002170 }
2171
2172 FileFormats format;
2173 const uint32_t notificationTime(0); // Not supported in VoE
2174 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2175
2176 if (codecInst != NULL && codecInst->channels != 1) {
2177 _engineStatisticsPtr->SetLastError(
2178 VE_BAD_ARGUMENT, kTraceError,
2179 "StartRecordingPlayout() invalid compression");
2180 return (-1);
2181 }
2182 if (codecInst == NULL) {
2183 format = kFileFormatPcm16kHzFile;
2184 codecInst = &dummyCodec;
2185 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2186 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2187 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2188 format = kFileFormatWavFile;
2189 } else {
2190 format = kFileFormatCompressedFile;
2191 }
2192
2193 rtc::CritScope cs(&_fileCritSect);
2194
2195 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002196 if (output_file_recorder_) {
2197 output_file_recorder_->RegisterModuleFileCallback(NULL);
2198 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002199 }
2200
kwiberg5a25d952016-08-17 07:31:12 -07002201 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002202 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002203 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002204 _engineStatisticsPtr->SetLastError(
2205 VE_INVALID_ARGUMENT, kTraceError,
2206 "StartRecordingPlayout() fileRecorder format isnot correct");
2207 return -1;
2208 }
2209
kwiberg4ec01d92016-08-22 08:43:54 -07002210 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002211 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002212 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2213 "StartRecordingPlayout() failed to "
2214 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002215 output_file_recorder_->StopRecording();
2216 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002217 return -1;
2218 }
2219
kwiberg5a25d952016-08-17 07:31:12 -07002220 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002221 _outputFileRecording = true;
2222
2223 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002224}
2225
kwiberg55b97fe2016-01-28 05:22:45 -08002226int Channel::StopRecordingPlayout() {
2227 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2228 "Channel::StopRecordingPlayout()");
2229
2230 if (!_outputFileRecording) {
2231 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2232 "StopRecordingPlayout() isnot recording");
2233 return -1;
2234 }
2235
2236 rtc::CritScope cs(&_fileCritSect);
2237
kwiberg5a25d952016-08-17 07:31:12 -07002238 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002239 _engineStatisticsPtr->SetLastError(
2240 VE_STOP_RECORDING_FAILED, kTraceError,
2241 "StopRecording() could not stop recording");
2242 return (-1);
2243 }
kwiberg5a25d952016-08-17 07:31:12 -07002244 output_file_recorder_->RegisterModuleFileCallback(NULL);
2245 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002246 _outputFileRecording = false;
2247
2248 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002249}
2250
kwiberg55b97fe2016-01-28 05:22:45 -08002251void Channel::SetMixWithMicStatus(bool mix) {
2252 rtc::CritScope cs(&_fileCritSect);
2253 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002254}
2255
kwiberg55b97fe2016-01-28 05:22:45 -08002256int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2257 int8_t currentLevel = _outputAudioLevel.Level();
2258 level = static_cast<int32_t>(currentLevel);
2259 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002260}
2261
kwiberg55b97fe2016-01-28 05:22:45 -08002262int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2263 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2264 level = static_cast<int32_t>(currentLevel);
2265 return 0;
2266}
2267
solenberg1c2af8e2016-03-24 10:36:00 -07002268int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002269 rtc::CritScope cs(&volume_settings_critsect_);
2270 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002271 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002272 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002273 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002274}
2275
solenberg1c2af8e2016-03-24 10:36:00 -07002276bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002277 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002278 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002279}
2280
kwiberg55b97fe2016-01-28 05:22:45 -08002281int Channel::SetOutputVolumePan(float left, float right) {
2282 rtc::CritScope cs(&volume_settings_critsect_);
2283 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002284 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002285 _panLeft = left;
2286 _panRight = right;
2287 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002288}
2289
kwiberg55b97fe2016-01-28 05:22:45 -08002290int Channel::GetOutputVolumePan(float& left, float& right) const {
2291 rtc::CritScope cs(&volume_settings_critsect_);
2292 left = _panLeft;
2293 right = _panRight;
2294 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002295}
2296
kwiberg55b97fe2016-01-28 05:22:45 -08002297int Channel::SetChannelOutputVolumeScaling(float scaling) {
2298 rtc::CritScope cs(&volume_settings_critsect_);
2299 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002300 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002301 _outputGain = scaling;
2302 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002303}
2304
kwiberg55b97fe2016-01-28 05:22:45 -08002305int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2306 rtc::CritScope cs(&volume_settings_critsect_);
2307 scaling = _outputGain;
2308 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002309}
2310
solenberg8842c3e2016-03-11 03:06:41 -08002311int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002312 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002313 "Channel::SendTelephoneEventOutband(...)");
2314 RTC_DCHECK_LE(0, event);
2315 RTC_DCHECK_GE(255, event);
2316 RTC_DCHECK_LE(0, duration_ms);
2317 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002318 if (!Sending()) {
2319 return -1;
2320 }
solenberg8842c3e2016-03-11 03:06:41 -08002321 if (_rtpRtcpModule->SendTelephoneEventOutband(
2322 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002323 _engineStatisticsPtr->SetLastError(
2324 VE_SEND_DTMF_FAILED, kTraceWarning,
2325 "SendTelephoneEventOutband() failed to send event");
2326 return -1;
2327 }
2328 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002329}
2330
solenbergffbbcac2016-11-17 05:25:37 -08002331int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2332 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002333 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002334 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002335 RTC_DCHECK_LE(0, payload_type);
2336 RTC_DCHECK_GE(127, payload_type);
2337 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002338 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002339 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002340 memcpy(codec.plname, "telephone-event", 16);
2341 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2342 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2343 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2344 _engineStatisticsPtr->SetLastError(
2345 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2346 "SetSendTelephoneEventPayloadType() failed to register send"
2347 "payload type");
2348 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002349 }
kwiberg55b97fe2016-01-28 05:22:45 -08002350 }
kwiberg55b97fe2016-01-28 05:22:45 -08002351 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002352}
2353
kwiberg55b97fe2016-01-28 05:22:45 -08002354int Channel::VoiceActivityIndicator(int& activity) {
2355 activity = _sendFrameType;
2356 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002357}
2358
kwiberg55b97fe2016-01-28 05:22:45 -08002359int Channel::SetLocalSSRC(unsigned int ssrc) {
2360 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2361 "Channel::SetLocalSSRC()");
2362 if (channel_state_.Get().sending) {
2363 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2364 "SetLocalSSRC() already sending");
2365 return -1;
2366 }
2367 _rtpRtcpModule->SetSSRC(ssrc);
2368 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002369}
2370
kwiberg55b97fe2016-01-28 05:22:45 -08002371int Channel::GetLocalSSRC(unsigned int& ssrc) {
2372 ssrc = _rtpRtcpModule->SSRC();
2373 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002374}
2375
kwiberg55b97fe2016-01-28 05:22:45 -08002376int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2377 ssrc = rtp_receiver_->SSRC();
2378 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002379}
2380
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002381int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002382 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002383 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002384}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002385
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002386int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2387 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002388 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2389 if (enable &&
2390 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2391 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002392 return -1;
2393 }
2394 return 0;
2395}
2396
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002397void Channel::EnableSendTransportSequenceNumber(int id) {
2398 int ret =
2399 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2400 RTC_DCHECK_EQ(0, ret);
2401}
2402
stefan3313ec92016-01-21 06:32:43 -08002403void Channel::EnableReceiveTransportSequenceNumber(int id) {
2404 rtp_header_parser_->DeregisterRtpHeaderExtension(
2405 kRtpExtensionTransportSequenceNumber);
2406 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2407 kRtpExtensionTransportSequenceNumber, id);
2408 RTC_DCHECK(ret);
2409}
2410
stefanbba9dec2016-02-01 04:39:55 -08002411void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002412 RtpPacketSender* rtp_packet_sender,
2413 TransportFeedbackObserver* transport_feedback_observer,
stefan7de8d642017-02-07 07:14:08 -08002414 PacketRouter* packet_router,
2415 RtcpBandwidthObserver* bandwidth_observer) {
stefanbba9dec2016-02-01 04:39:55 -08002416 RTC_DCHECK(rtp_packet_sender);
2417 RTC_DCHECK(transport_feedback_observer);
2418 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002419 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002420 feedback_observer_proxy_->SetTransportFeedbackObserver(
2421 transport_feedback_observer);
2422 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2423 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2424 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002425 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002426 packet_router_ = packet_router;
2427}
2428
stefanbba9dec2016-02-01 04:39:55 -08002429void Channel::RegisterReceiverCongestionControlObjects(
2430 PacketRouter* packet_router) {
2431 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002432 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002433 packet_router_ = packet_router;
2434}
2435
2436void Channel::ResetCongestionControlObjects() {
2437 RTC_DCHECK(packet_router_);
2438 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002439 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002440 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2441 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002442 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002443 packet_router_ = nullptr;
2444 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2445}
2446
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002447void Channel::SetRTCPStatus(bool enable) {
2448 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2449 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002450 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002451}
2452
kwiberg55b97fe2016-01-28 05:22:45 -08002453int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002454 RtcpMode method = _rtpRtcpModule->RTCP();
2455 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002456 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002457}
2458
kwiberg55b97fe2016-01-28 05:22:45 -08002459int Channel::SetRTCP_CNAME(const char cName[256]) {
2460 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2461 "Channel::SetRTCP_CNAME()");
2462 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2463 _engineStatisticsPtr->SetLastError(
2464 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2465 "SetRTCP_CNAME() failed to set RTCP CNAME");
2466 return -1;
2467 }
2468 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002469}
2470
kwiberg55b97fe2016-01-28 05:22:45 -08002471int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2472 if (cName == NULL) {
2473 _engineStatisticsPtr->SetLastError(
2474 VE_INVALID_ARGUMENT, kTraceError,
2475 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2476 return -1;
2477 }
2478 char cname[RTCP_CNAME_SIZE];
2479 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2480 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2481 _engineStatisticsPtr->SetLastError(
2482 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2483 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2484 return -1;
2485 }
2486 strcpy(cName, cname);
2487 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002488}
2489
kwiberg55b97fe2016-01-28 05:22:45 -08002490int Channel::SendApplicationDefinedRTCPPacket(
2491 unsigned char subType,
2492 unsigned int name,
2493 const char* data,
2494 unsigned short dataLengthInBytes) {
2495 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2496 "Channel::SendApplicationDefinedRTCPPacket()");
2497 if (!channel_state_.Get().sending) {
2498 _engineStatisticsPtr->SetLastError(
2499 VE_NOT_SENDING, kTraceError,
2500 "SendApplicationDefinedRTCPPacket() not sending");
2501 return -1;
2502 }
2503 if (NULL == data) {
2504 _engineStatisticsPtr->SetLastError(
2505 VE_INVALID_ARGUMENT, kTraceError,
2506 "SendApplicationDefinedRTCPPacket() invalid data value");
2507 return -1;
2508 }
2509 if (dataLengthInBytes % 4 != 0) {
2510 _engineStatisticsPtr->SetLastError(
2511 VE_INVALID_ARGUMENT, kTraceError,
2512 "SendApplicationDefinedRTCPPacket() invalid length value");
2513 return -1;
2514 }
2515 RtcpMode status = _rtpRtcpModule->RTCP();
2516 if (status == RtcpMode::kOff) {
2517 _engineStatisticsPtr->SetLastError(
2518 VE_RTCP_ERROR, kTraceError,
2519 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2520 return -1;
2521 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002522
kwiberg55b97fe2016-01-28 05:22:45 -08002523 // Create and schedule the RTCP APP packet for transmission
2524 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2525 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2526 _engineStatisticsPtr->SetLastError(
2527 VE_SEND_ERROR, kTraceError,
2528 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2529 return -1;
2530 }
2531 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002532}
2533
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002534int Channel::GetRemoteRTCPReportBlocks(
2535 std::vector<ReportBlock>* report_blocks) {
2536 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002537 _engineStatisticsPtr->SetLastError(
2538 VE_INVALID_ARGUMENT, kTraceError,
2539 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002540 return -1;
2541 }
2542
2543 // Get the report blocks from the latest received RTCP Sender or Receiver
2544 // Report. Each element in the vector contains the sender's SSRC and a
2545 // report block according to RFC 3550.
2546 std::vector<RTCPReportBlock> rtcp_report_blocks;
2547 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002548 return -1;
2549 }
2550
2551 if (rtcp_report_blocks.empty())
2552 return 0;
2553
2554 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2555 for (; it != rtcp_report_blocks.end(); ++it) {
2556 ReportBlock report_block;
2557 report_block.sender_SSRC = it->remoteSSRC;
2558 report_block.source_SSRC = it->sourceSSRC;
2559 report_block.fraction_lost = it->fractionLost;
2560 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2561 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2562 report_block.interarrival_jitter = it->jitter;
2563 report_block.last_SR_timestamp = it->lastSR;
2564 report_block.delay_since_last_SR = it->delaySinceLastSR;
2565 report_blocks->push_back(report_block);
2566 }
2567 return 0;
2568}
2569
kwiberg55b97fe2016-01-28 05:22:45 -08002570int Channel::GetRTPStatistics(CallStatistics& stats) {
2571 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002572
kwiberg55b97fe2016-01-28 05:22:45 -08002573 // The jitter statistics is updated for each received RTP packet and is
2574 // based on received packets.
2575 RtcpStatistics statistics;
2576 StreamStatistician* statistician =
2577 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002578 if (statistician) {
2579 statistician->GetStatistics(&statistics,
2580 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002581 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002582
kwiberg55b97fe2016-01-28 05:22:45 -08002583 stats.fractionLost = statistics.fraction_lost;
2584 stats.cumulativeLost = statistics.cumulative_lost;
2585 stats.extendedMax = statistics.extended_max_sequence_number;
2586 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002587
kwiberg55b97fe2016-01-28 05:22:45 -08002588 // --- RTT
2589 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002590
kwiberg55b97fe2016-01-28 05:22:45 -08002591 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002592
kwiberg55b97fe2016-01-28 05:22:45 -08002593 size_t bytesSent(0);
2594 uint32_t packetsSent(0);
2595 size_t bytesReceived(0);
2596 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002597
kwiberg55b97fe2016-01-28 05:22:45 -08002598 if (statistician) {
2599 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2600 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002601
kwiberg55b97fe2016-01-28 05:22:45 -08002602 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2603 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2604 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2605 " output will not be complete");
2606 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002607
kwiberg55b97fe2016-01-28 05:22:45 -08002608 stats.bytesSent = bytesSent;
2609 stats.packetsSent = packetsSent;
2610 stats.bytesReceived = bytesReceived;
2611 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002612
kwiberg55b97fe2016-01-28 05:22:45 -08002613 // --- Timestamps
2614 {
2615 rtc::CritScope lock(&ts_stats_lock_);
2616 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2617 }
2618 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002619}
2620
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002621int Channel::SetCodecFECStatus(bool enable) {
2622 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2623 "Channel::SetCodecFECStatus()");
2624
kwibergc8d071e2016-04-06 12:22:38 -07002625 if (!codec_manager_.SetCodecFEC(enable) ||
2626 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002627 _engineStatisticsPtr->SetLastError(
2628 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2629 "SetCodecFECStatus() failed to set FEC state");
2630 return -1;
2631 }
2632 return 0;
2633}
2634
2635bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002636 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002637}
2638
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002639void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2640 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002641 // If pacing is enabled we always store packets.
2642 if (!pacing_enabled_)
2643 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002644 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002645 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002646 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002647 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002648 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002649}
2650
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002651// Called when we are missing one or more packets.
2652int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002653 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2654}
2655
kwiberg55b97fe2016-01-28 05:22:45 -08002656uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2657 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2658 "Channel::Demultiplex()");
2659 _audioFrame.CopyFrom(audioFrame);
2660 _audioFrame.id_ = _channelId;
2661 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002662}
2663
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002664void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002665 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002666 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002667 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002668 CodecInst codec;
2669 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002670
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002671 // Never upsample or upmix the capture signal here. This should be done at the
2672 // end of the send chain.
2673 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2674 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2675 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2676 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002677}
2678
kwiberg55b97fe2016-01-28 05:22:45 -08002679uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2680 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2681 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002682
kwiberg55b97fe2016-01-28 05:22:45 -08002683 if (_audioFrame.samples_per_channel_ == 0) {
2684 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2685 "Channel::PrepareEncodeAndSend() invalid audio frame");
2686 return 0xFFFFFFFF;
2687 }
2688
2689 if (channel_state_.Get().input_file_playing) {
2690 MixOrReplaceAudioWithFile(mixingFrequency);
2691 }
2692
solenberg1c2af8e2016-03-24 10:36:00 -07002693 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2694 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002695
kwiberg55b97fe2016-01-28 05:22:45 -08002696 if (_includeAudioLevelIndication) {
2697 size_t length =
2698 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002699 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002700 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002701 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002702 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002703 rms_level_.Analyze(
2704 rtc::ArrayView<const int16_t>(_audioFrame.data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002705 }
kwiberg55b97fe2016-01-28 05:22:45 -08002706 }
solenberg1c2af8e2016-03-24 10:36:00 -07002707 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002708
kwiberg55b97fe2016-01-28 05:22:45 -08002709 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002710}
2711
kwiberg55b97fe2016-01-28 05:22:45 -08002712uint32_t Channel::EncodeAndSend() {
2713 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2714 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002715
kwiberg55b97fe2016-01-28 05:22:45 -08002716 assert(_audioFrame.num_channels_ <= 2);
2717 if (_audioFrame.samples_per_channel_ == 0) {
2718 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2719 "Channel::EncodeAndSend() invalid audio frame");
2720 return 0xFFFFFFFF;
2721 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002722
kwiberg55b97fe2016-01-28 05:22:45 -08002723 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002724
kwiberg55b97fe2016-01-28 05:22:45 -08002725 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002726
kwiberg55b97fe2016-01-28 05:22:45 -08002727 // The ACM resamples internally.
2728 _audioFrame.timestamp_ = _timeStamp;
2729 // This call will trigger AudioPacketizationCallback::SendData if encoding
2730 // is done and payload is ready for packetization and transmission.
2731 // Otherwise, it will return without invoking the callback.
2732 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2733 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2734 "Channel::EncodeAndSend() ACM encoding failed");
2735 return 0xFFFFFFFF;
2736 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002737
kwiberg55b97fe2016-01-28 05:22:45 -08002738 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2739 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002740}
2741
solenberg7602aab2016-11-14 11:30:07 -08002742void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2743 RTC_DCHECK(!channel.channel() ||
2744 channel.channel()->ChannelId() != _channelId);
2745 rtc::CritScope lock(&assoc_send_channel_lock_);
2746 associate_send_channel_ = channel;
2747}
2748
Minyue2013aec2015-05-13 14:14:42 +02002749void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002750 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002751 Channel* channel = associate_send_channel_.channel();
2752 if (channel && channel->ChannelId() == channel_id) {
2753 // If this channel is associated with a send channel of the specified
2754 // Channel ID, disassociate with it.
2755 ChannelOwner ref(NULL);
2756 associate_send_channel_ = ref;
2757 }
2758}
2759
ivoc14d5dbe2016-07-04 07:06:55 -07002760void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2761 event_log_proxy_->SetEventLog(event_log);
2762}
2763
michaelt9332b7d2016-11-30 07:51:13 -08002764void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2765 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2766}
2767
nisse284542b2017-01-10 08:58:32 -08002768void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08002769 size_t overhead_per_packet =
2770 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08002771 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2772 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08002773 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08002774 }
2775 });
2776}
2777
2778void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002779 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002780 transport_overhead_per_packet_ = transport_overhead_per_packet;
2781 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002782}
2783
hbos3fd31fe2017-02-28 05:43:16 -08002784// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08002785void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002786 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002787 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2788 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002789}
2790
kwiberg55b97fe2016-01-28 05:22:45 -08002791int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2792 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002793}
2794
wu@webrtc.org24301a62013-12-13 19:17:43 +00002795void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2796 audio_coding_->GetDecodingCallStatistics(stats);
2797}
2798
solenberg358057b2015-11-27 10:46:42 -08002799uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002800 rtc::CritScope lock(&video_sync_lock_);
2801 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002802}
2803
kwiberg55b97fe2016-01-28 05:22:45 -08002804int Channel::SetMinimumPlayoutDelay(int delayMs) {
2805 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2806 "Channel::SetMinimumPlayoutDelay()");
2807 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2808 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2809 _engineStatisticsPtr->SetLastError(
2810 VE_INVALID_ARGUMENT, kTraceError,
2811 "SetMinimumPlayoutDelay() invalid min delay");
2812 return -1;
2813 }
2814 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2815 _engineStatisticsPtr->SetLastError(
2816 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2817 "SetMinimumPlayoutDelay() failed to set min playout delay");
2818 return -1;
2819 }
2820 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002821}
2822
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002823int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002824 uint32_t playout_timestamp_rtp = 0;
2825 {
tommi31fc21f2016-01-21 10:37:37 -08002826 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002827 playout_timestamp_rtp = playout_timestamp_rtp_;
2828 }
kwiberg55b97fe2016-01-28 05:22:45 -08002829 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002830 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002831 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002832 "GetPlayoutTimestamp() failed to retrieve timestamp");
2833 return -1;
2834 }
deadbeef74375882015-08-13 12:09:10 -07002835 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002836 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002837}
2838
kwiberg55b97fe2016-01-28 05:22:45 -08002839int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2840 RtpReceiver** rtp_receiver) const {
2841 *rtpRtcpModule = _rtpRtcpModule.get();
2842 *rtp_receiver = rtp_receiver_.get();
2843 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002844}
2845
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002846// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2847// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002848int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002849 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002850 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002851
kwiberg55b97fe2016-01-28 05:22:45 -08002852 {
2853 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002854
kwiberg5a25d952016-08-17 07:31:12 -07002855 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002856 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2857 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2858 " doesnt exist");
2859 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002860 }
2861
kwiberg4ec01d92016-08-22 08:43:54 -07002862 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002863 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002864 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2865 "Channel::MixOrReplaceAudioWithFile() file mixing "
2866 "failed");
2867 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002868 }
kwiberg55b97fe2016-01-28 05:22:45 -08002869 if (fileSamples == 0) {
2870 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2871 "Channel::MixOrReplaceAudioWithFile() file is ended");
2872 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002873 }
kwiberg55b97fe2016-01-28 05:22:45 -08002874 }
2875
2876 assert(_audioFrame.samples_per_channel_ == fileSamples);
2877
2878 if (_mixFileWithMicrophone) {
2879 // Currently file stream is always mono.
2880 // TODO(xians): Change the code when FilePlayer supports real stereo.
2881 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
2882 1, fileSamples);
2883 } else {
2884 // Replace ACM audio with file.
2885 // Currently file stream is always mono.
2886 // TODO(xians): Change the code when FilePlayer supports real stereo.
2887 _audioFrame.UpdateFrame(
2888 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2889 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2890 }
2891 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002892}
2893
kwiberg55b97fe2016-01-28 05:22:45 -08002894int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2895 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002896
kwibergb7f89d62016-02-17 10:04:18 -08002897 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002898 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002899
kwiberg55b97fe2016-01-28 05:22:45 -08002900 {
2901 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002902
kwiberg5a25d952016-08-17 07:31:12 -07002903 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002904 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2905 "Channel::MixAudioWithFile() file mixing failed");
2906 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002907 }
2908
kwiberg55b97fe2016-01-28 05:22:45 -08002909 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002910 if (output_file_player_->Get10msAudioFromFile(
2911 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002912 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2913 "Channel::MixAudioWithFile() file mixing failed");
2914 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002915 }
kwiberg55b97fe2016-01-28 05:22:45 -08002916 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002917
kwiberg55b97fe2016-01-28 05:22:45 -08002918 if (audioFrame.samples_per_channel_ == fileSamples) {
2919 // Currently file stream is always mono.
2920 // TODO(xians): Change the code when FilePlayer supports real stereo.
2921 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
2922 fileSamples);
2923 } else {
2924 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2925 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2926 ") != "
2927 "fileSamples(%" PRIuS ")",
2928 audioFrame.samples_per_channel_, fileSamples);
2929 return -1;
2930 }
2931
2932 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002933}
2934
deadbeef74375882015-08-13 12:09:10 -07002935void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002936 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002937
henrik.lundin96bd5022016-04-06 04:13:56 -07002938 if (!jitter_buffer_playout_timestamp_) {
2939 // This can happen if this channel has not received any RTP packets. In
2940 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07002941 return;
2942 }
2943
2944 uint16_t delay_ms = 0;
2945 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002946 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002947 "Channel::UpdatePlayoutTimestamp() failed to read playout"
2948 " delay from the ADM");
2949 _engineStatisticsPtr->SetLastError(
2950 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
2951 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
2952 return;
2953 }
2954
henrik.lundin96bd5022016-04-06 04:13:56 -07002955 RTC_DCHECK(jitter_buffer_playout_timestamp_);
2956 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07002957
2958 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07002959 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07002960
kwiberg55b97fe2016-01-28 05:22:45 -08002961 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002962 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07002963 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07002964
2965 {
tommi31fc21f2016-01-21 10:37:37 -08002966 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08002967 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002968 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07002969 }
2970 playout_delay_ms_ = delay_ms;
2971 }
2972}
2973
kwiberg55b97fe2016-01-28 05:22:45 -08002974void Channel::RegisterReceiveCodecsToRTPModule() {
2975 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2976 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002977
kwiberg55b97fe2016-01-28 05:22:45 -08002978 CodecInst codec;
2979 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002980
kwiberg55b97fe2016-01-28 05:22:45 -08002981 for (int idx = 0; idx < nSupportedCodecs; idx++) {
2982 // Open up the RTP/RTCP receiver for all supported codecs
2983 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08002984 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08002985 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2986 "Channel::RegisterReceiveCodecsToRTPModule() unable"
2987 " to register %s (%d/%d/%" PRIuS
2988 "/%d) to RTP/RTCP "
2989 "receiver",
2990 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2991 codec.rate);
2992 } else {
2993 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2994 "Channel::RegisterReceiveCodecsToRTPModule() %s "
2995 "(%d/%d/%" PRIuS
2996 "/%d) has been added to the RTP/RTCP "
2997 "receiver",
2998 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2999 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003000 }
kwiberg55b97fe2016-01-28 05:22:45 -08003001 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003002}
3003
kwiberg55b97fe2016-01-28 05:22:45 -08003004int Channel::SetSendRtpHeaderExtension(bool enable,
3005 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003006 unsigned char id) {
3007 int error = 0;
3008 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3009 if (enable) {
3010 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3011 }
3012 return error;
3013}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003014
ossue280cde2016-10-12 11:04:10 -07003015int Channel::GetRtpTimestampRateHz() const {
3016 const auto format = audio_coding_->ReceiveFormat();
3017 // Default to the playout frequency if we've not gotten any packets yet.
3018 // TODO(ossu): Zero clockrate can only happen if we've added an external
3019 // decoder for a format we don't support internally. Remove once that way of
3020 // adding decoders is gone!
3021 return (format && format->clockrate_hz != 0)
3022 ? format->clockrate_hz
3023 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003024}
3025
Minyue2013aec2015-05-13 14:14:42 +02003026int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003027 RtcpMode method = _rtpRtcpModule->RTCP();
3028 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003029 return 0;
3030 }
3031 std::vector<RTCPReportBlock> report_blocks;
3032 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003033
3034 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003035 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003036 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003037 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003038 Channel* channel = associate_send_channel_.channel();
3039 // Tries to get RTT from an associated channel. This is important for
3040 // receive-only channels.
3041 if (channel) {
3042 // To prevent infinite recursion and deadlock, calling GetRTT of
3043 // associate channel should always use "false" for argument:
3044 // |allow_associate_channel|.
3045 rtt = channel->GetRTT(false);
3046 }
3047 }
3048 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003049 }
3050
3051 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3052 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3053 for (; it != report_blocks.end(); ++it) {
3054 if (it->remoteSSRC == remoteSSRC)
3055 break;
3056 }
3057 if (it == report_blocks.end()) {
3058 // We have not received packets with SSRC matching the report blocks.
3059 // To calculate RTT we try with the SSRC of the first report block.
3060 // This is very important for send-only channels where we don't know
3061 // the SSRC of the other end.
3062 remoteSSRC = report_blocks[0].remoteSSRC;
3063 }
Minyue2013aec2015-05-13 14:14:42 +02003064
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003065 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003066 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003067 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003068 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3069 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003070 return 0;
3071 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003072 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003073}
3074
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003075} // namespace voe
3076} // namespace webrtc