blob: 66d66e6191cb8b0dc1db116738631764640e6a74 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrika@webrtc.org2919e952012-01-31 08:45:03 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000011#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Henrik Lundin64dad832015-05-11 12:44:23 +020013#include <algorithm>
Tommif888bb52015-12-12 01:37:01 +010014#include <utility>
Henrik Lundin64dad832015-05-11 12:44:23 +020015
aleloi6321b492016-12-05 01:46:09 -080016#include "webrtc/audio/utility/audio_frame_operations.h"
henrik.lundin50499422016-11-29 04:26:24 -080017#include "webrtc/base/array_view.h"
Ivo Creusenae856f22015-09-17 16:30:16 +020018#include "webrtc/base/checks.h"
tommi31fc21f2016-01-21 10:37:37 -080019#include "webrtc/base/criticalsection.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000020#include "webrtc/base/format_macros.h"
tommidea489f2017-03-03 03:20:24 -080021#include "webrtc/base/location.h"
pbosad856222015-11-27 09:48:36 -080022#include "webrtc/base/logging.h"
Erik Språng737336d2016-07-29 12:59:36 +020023#include "webrtc/base/rate_limiter.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010024#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000025#include "webrtc/base/timeutils.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020026#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070027#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070028#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000029#include "webrtc/modules/audio_device/include/audio_device.h"
30#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010031#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010032#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010033#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
34#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
35#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
nisse657bab22017-02-21 06:28:10 -080036#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000037#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010038#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010039#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000040#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
41#include "webrtc/voice_engine/output_mixer.h"
42#include "webrtc/voice_engine/statistics.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000043#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000044
andrew@webrtc.org50419b02012-11-14 19:07:54 +000045namespace webrtc {
46namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000047
kwibergc8d071e2016-04-06 12:22:38 -070048namespace {
49
Erik Språng737336d2016-07-29 12:59:36 +020050constexpr int64_t kMaxRetransmissionWindowMs = 1000;
51constexpr int64_t kMinRetransmissionWindowMs = 30;
52
kwibergc8d071e2016-04-06 12:22:38 -070053} // namespace
54
solenberg8842c3e2016-03-11 03:06:41 -080055const int kTelephoneEventAttenuationdB = 10;
56
ivoc14d5dbe2016-07-04 07:06:55 -070057class RtcEventLogProxy final : public webrtc::RtcEventLog {
58 public:
59 RtcEventLogProxy() : event_log_(nullptr) {}
60
61 bool StartLogging(const std::string& file_name,
62 int64_t max_size_bytes) override {
63 RTC_NOTREACHED();
64 return false;
65 }
66
67 bool StartLogging(rtc::PlatformFile log_file,
68 int64_t max_size_bytes) override {
69 RTC_NOTREACHED();
70 return false;
71 }
72
73 void StopLogging() override { RTC_NOTREACHED(); }
74
75 void LogVideoReceiveStreamConfig(
76 const webrtc::VideoReceiveStream::Config& config) override {
77 rtc::CritScope lock(&crit_);
78 if (event_log_) {
79 event_log_->LogVideoReceiveStreamConfig(config);
80 }
81 }
82
83 void LogVideoSendStreamConfig(
84 const webrtc::VideoSendStream::Config& config) override {
85 rtc::CritScope lock(&crit_);
86 if (event_log_) {
87 event_log_->LogVideoSendStreamConfig(config);
88 }
89 }
90
ivoce0928d82016-10-10 05:12:51 -070091 void LogAudioReceiveStreamConfig(
92 const webrtc::AudioReceiveStream::Config& config) override {
93 rtc::CritScope lock(&crit_);
94 if (event_log_) {
95 event_log_->LogAudioReceiveStreamConfig(config);
96 }
97 }
98
99 void LogAudioSendStreamConfig(
100 const webrtc::AudioSendStream::Config& config) override {
101 rtc::CritScope lock(&crit_);
102 if (event_log_) {
103 event_log_->LogAudioSendStreamConfig(config);
104 }
105 }
106
ivoc14d5dbe2016-07-04 07:06:55 -0700107 void LogRtpHeader(webrtc::PacketDirection direction,
108 webrtc::MediaType media_type,
109 const uint8_t* header,
110 size_t packet_length) override {
philipel32d00102017-02-27 02:18:46 -0800111 LogRtpHeader(direction, media_type, header, packet_length,
112 PacedPacketInfo::kNotAProbe);
113 }
114
115 void LogRtpHeader(webrtc::PacketDirection direction,
116 webrtc::MediaType media_type,
117 const uint8_t* header,
118 size_t packet_length,
119 int probe_cluster_id) override {
ivoc14d5dbe2016-07-04 07:06:55 -0700120 rtc::CritScope lock(&crit_);
121 if (event_log_) {
philipel32d00102017-02-27 02:18:46 -0800122 event_log_->LogRtpHeader(direction, media_type, header, packet_length,
123 probe_cluster_id);
ivoc14d5dbe2016-07-04 07:06:55 -0700124 }
125 }
126
127 void LogRtcpPacket(webrtc::PacketDirection direction,
128 webrtc::MediaType media_type,
129 const uint8_t* packet,
130 size_t length) override {
131 rtc::CritScope lock(&crit_);
132 if (event_log_) {
133 event_log_->LogRtcpPacket(direction, media_type, packet, length);
134 }
135 }
136
137 void LogAudioPlayout(uint32_t ssrc) override {
138 rtc::CritScope lock(&crit_);
139 if (event_log_) {
140 event_log_->LogAudioPlayout(ssrc);
141 }
142 }
143
terelius424e6cf2017-02-20 05:14:41 -0800144 void LogLossBasedBweUpdate(int32_t bitrate_bps,
ivoc14d5dbe2016-07-04 07:06:55 -0700145 uint8_t fraction_loss,
146 int32_t total_packets) override {
147 rtc::CritScope lock(&crit_);
148 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800149 event_log_->LogLossBasedBweUpdate(bitrate_bps, fraction_loss,
150 total_packets);
ivoc14d5dbe2016-07-04 07:06:55 -0700151 }
152 }
153
terelius424e6cf2017-02-20 05:14:41 -0800154 void LogDelayBasedBweUpdate(int32_t bitrate_bps,
terelius0baf55d2017-02-17 03:38:28 -0800155 BandwidthUsage detector_state) override {
156 rtc::CritScope lock(&crit_);
157 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800158 event_log_->LogDelayBasedBweUpdate(bitrate_bps, detector_state);
terelius0baf55d2017-02-17 03:38:28 -0800159 }
160 }
161
minyue4b7c9522017-01-24 04:54:59 -0800162 void LogAudioNetworkAdaptation(
163 const AudioNetworkAdaptor::EncoderRuntimeConfig& config) override {
164 rtc::CritScope lock(&crit_);
165 if (event_log_) {
166 event_log_->LogAudioNetworkAdaptation(config);
167 }
168 }
169
philipel32d00102017-02-27 02:18:46 -0800170 void LogProbeClusterCreated(int id,
171 int bitrate_bps,
172 int min_probes,
173 int min_bytes) override {
174 rtc::CritScope lock(&crit_);
175 if (event_log_) {
176 event_log_->LogProbeClusterCreated(id, bitrate_bps, min_probes,
177 min_bytes);
178 }
179 };
180
181 void LogProbeResultSuccess(int id, int bitrate_bps) override {
182 rtc::CritScope lock(&crit_);
183 if (event_log_) {
184 event_log_->LogProbeResultSuccess(id, bitrate_bps);
185 }
186 };
187
188 void LogProbeResultFailure(int id,
189 ProbeFailureReason failure_reason) override {
190 rtc::CritScope lock(&crit_);
191 if (event_log_) {
192 event_log_->LogProbeResultFailure(id, failure_reason);
193 }
194 };
195
ivoc14d5dbe2016-07-04 07:06:55 -0700196 void SetEventLog(RtcEventLog* event_log) {
197 rtc::CritScope lock(&crit_);
198 event_log_ = event_log;
199 }
200
201 private:
202 rtc::CriticalSection crit_;
203 RtcEventLog* event_log_ GUARDED_BY(crit_);
204 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
205};
206
michaelt9332b7d2016-11-30 07:51:13 -0800207class RtcpRttStatsProxy final : public RtcpRttStats {
208 public:
209 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
210
211 void OnRttUpdate(int64_t rtt) override {
212 rtc::CritScope lock(&crit_);
213 if (rtcp_rtt_stats_)
214 rtcp_rtt_stats_->OnRttUpdate(rtt);
215 }
216
217 int64_t LastProcessedRtt() const override {
218 rtc::CritScope lock(&crit_);
219 if (!rtcp_rtt_stats_)
220 return 0;
221 return rtcp_rtt_stats_->LastProcessedRtt();
222 }
223
224 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
225 rtc::CritScope lock(&crit_);
226 rtcp_rtt_stats_ = rtcp_rtt_stats;
227 }
228
229 private:
230 rtc::CriticalSection crit_;
231 RtcpRttStats* rtcp_rtt_stats_ GUARDED_BY(crit_);
232 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
233};
234
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100235class TransportFeedbackProxy : public TransportFeedbackObserver {
236 public:
237 TransportFeedbackProxy() : feedback_observer_(nullptr) {
238 pacer_thread_.DetachFromThread();
239 network_thread_.DetachFromThread();
240 }
241
242 void SetTransportFeedbackObserver(
243 TransportFeedbackObserver* feedback_observer) {
244 RTC_DCHECK(thread_checker_.CalledOnValidThread());
245 rtc::CritScope lock(&crit_);
246 feedback_observer_ = feedback_observer;
247 }
248
249 // Implements TransportFeedbackObserver.
250 void AddPacket(uint16_t sequence_number,
251 size_t length,
philipel8aadd502017-02-23 02:56:13 -0800252 const PacedPacketInfo& pacing_info) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100253 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
254 rtc::CritScope lock(&crit_);
255 if (feedback_observer_)
philipel8aadd502017-02-23 02:56:13 -0800256 feedback_observer_->AddPacket(sequence_number, length, pacing_info);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100257 }
philipel8aadd502017-02-23 02:56:13 -0800258
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100259 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
260 RTC_DCHECK(network_thread_.CalledOnValidThread());
261 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700262 if (feedback_observer_)
263 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200264 }
elad.alonf9490002017-03-06 05:32:21 -0800265 std::vector<PacketFeedback> GetTransportFeedbackVector() const override {
Stefan Holmer60e43462016-09-07 09:58:20 +0200266 RTC_NOTREACHED();
elad.alonf9490002017-03-06 05:32:21 -0800267 return std::vector<PacketFeedback>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100268 }
269
270 private:
271 rtc::CriticalSection crit_;
272 rtc::ThreadChecker thread_checker_;
273 rtc::ThreadChecker pacer_thread_;
274 rtc::ThreadChecker network_thread_;
275 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
276};
277
278class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
279 public:
280 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
281 pacer_thread_.DetachFromThread();
282 }
283
284 void SetSequenceNumberAllocator(
285 TransportSequenceNumberAllocator* seq_num_allocator) {
286 RTC_DCHECK(thread_checker_.CalledOnValidThread());
287 rtc::CritScope lock(&crit_);
288 seq_num_allocator_ = seq_num_allocator;
289 }
290
291 // Implements TransportSequenceNumberAllocator.
292 uint16_t AllocateSequenceNumber() override {
293 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
294 rtc::CritScope lock(&crit_);
295 if (!seq_num_allocator_)
296 return 0;
297 return seq_num_allocator_->AllocateSequenceNumber();
298 }
299
300 private:
301 rtc::CriticalSection crit_;
302 rtc::ThreadChecker thread_checker_;
303 rtc::ThreadChecker pacer_thread_;
304 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
305};
306
307class RtpPacketSenderProxy : public RtpPacketSender {
308 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800309 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100310
311 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
312 RTC_DCHECK(thread_checker_.CalledOnValidThread());
313 rtc::CritScope lock(&crit_);
314 rtp_packet_sender_ = rtp_packet_sender;
315 }
316
317 // Implements RtpPacketSender.
318 void InsertPacket(Priority priority,
319 uint32_t ssrc,
320 uint16_t sequence_number,
321 int64_t capture_time_ms,
322 size_t bytes,
323 bool retransmission) override {
324 rtc::CritScope lock(&crit_);
325 if (rtp_packet_sender_) {
326 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
327 capture_time_ms, bytes, retransmission);
328 }
329 }
330
331 private:
332 rtc::ThreadChecker thread_checker_;
333 rtc::CriticalSection crit_;
334 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
335};
336
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000337class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000338 public:
stefan7de8d642017-02-07 07:14:08 -0800339 explicit VoERtcpObserver(Channel* owner)
340 : owner_(owner), bandwidth_observer_(nullptr) {}
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000341 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000342
stefan7de8d642017-02-07 07:14:08 -0800343 void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
344 rtc::CritScope lock(&crit_);
345 bandwidth_observer_ = bandwidth_observer;
346 }
347
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000348 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
stefan7de8d642017-02-07 07:14:08 -0800349 rtc::CritScope lock(&crit_);
350 if (bandwidth_observer_) {
351 bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
352 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000353 }
354
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000355 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
356 int64_t rtt,
357 int64_t now_ms) override {
stefan7de8d642017-02-07 07:14:08 -0800358 {
359 rtc::CritScope lock(&crit_);
360 if (bandwidth_observer_) {
361 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
362 now_ms);
363 }
364 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000365 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
366 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
367 // report for VoiceEngine?
368 if (report_blocks.empty())
369 return;
370
371 int fraction_lost_aggregate = 0;
372 int total_number_of_packets = 0;
373
374 // If receiving multiple report blocks, calculate the weighted average based
375 // on the number of packets a report refers to.
376 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
377 block_it != report_blocks.end(); ++block_it) {
378 // Find the previous extended high sequence number for this remote SSRC,
379 // to calculate the number of RTP packets this report refers to. Ignore if
380 // we haven't seen this SSRC before.
381 std::map<uint32_t, uint32_t>::iterator seq_num_it =
382 extended_max_sequence_number_.find(block_it->sourceSSRC);
383 int number_of_packets = 0;
384 if (seq_num_it != extended_max_sequence_number_.end()) {
385 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
386 }
387 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
388 total_number_of_packets += number_of_packets;
389
390 extended_max_sequence_number_[block_it->sourceSSRC] =
391 block_it->extendedHighSeqNum;
392 }
393 int weighted_fraction_lost = 0;
394 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800395 weighted_fraction_lost =
396 (fraction_lost_aggregate + total_number_of_packets / 2) /
397 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000398 }
399 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000400 }
401
402 private:
403 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000404 // Maps remote side ssrc to extended highest sequence number received.
405 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800406 rtc::CriticalSection crit_;
407 RtcpBandwidthObserver* bandwidth_observer_ GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000408};
409
kwiberg55b97fe2016-01-28 05:22:45 -0800410int32_t Channel::SendData(FrameType frameType,
411 uint8_t payloadType,
412 uint32_t timeStamp,
413 const uint8_t* payloadData,
414 size_t payloadSize,
415 const RTPFragmentationHeader* fragmentation) {
416 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
417 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
418 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
419 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000420
kwiberg55b97fe2016-01-28 05:22:45 -0800421 if (_includeAudioLevelIndication) {
422 // Store current audio level in the RTP/RTCP module.
423 // The level will be used in combination with voice-activity state
424 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800425 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800426 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000427
kwiberg55b97fe2016-01-28 05:22:45 -0800428 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
429 // packetization.
430 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700431 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800432 (FrameType&)frameType, payloadType, timeStamp,
433 // Leaving the time when this frame was
434 // received from the capture device as
435 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700436 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800437 _engineStatisticsPtr->SetLastError(
438 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
439 "Channel::SendData() failed to send data to RTP/RTCP module");
440 return -1;
441 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000442
kwiberg55b97fe2016-01-28 05:22:45 -0800443 _lastLocalTimeStamp = timeStamp;
444 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000445
kwiberg55b97fe2016-01-28 05:22:45 -0800446 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000447}
448
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;
kwiberg55b97fe2016-01-28 05:22:45 -0800670 {
671 rtc::CritScope cs(&volume_settings_critsect_);
672 output_gain = _outputGain;
kwiberg55b97fe2016-01-28 05:22:45 -0800673 }
674
675 // Output volume scaling
676 if (output_gain < 0.99f || output_gain > 1.01f) {
solenberg8d73f8c2017-03-08 01:52:20 -0800677 // TODO(solenberg): Combine with mute state - this can cause clicks!
kwiberg55b97fe2016-01-28 05:22:45 -0800678 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
679 }
680
kwiberg55b97fe2016-01-28 05:22:45 -0800681 // Mix decoded PCM output with file if file mixing is enabled
682 if (state.output_file_playing) {
683 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700684 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800685 }
686
kwiberg55b97fe2016-01-28 05:22:45 -0800687 // Record playout if enabled
688 {
689 rtc::CritScope cs(&_fileCritSect);
690
kwiberg5a25d952016-08-17 07:31:12 -0700691 if (_outputFileRecording && output_file_recorder_) {
692 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800693 }
694 }
695
696 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700697 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800698 _outputAudioLevel.ComputeLevel(*audioFrame);
699
700 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
701 // The first frame with a valid rtp timestamp.
702 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
703 }
704
705 if (capture_start_rtp_time_stamp_ >= 0) {
706 // audioFrame.timestamp_ should be valid from now on.
707
708 // Compute elapsed time.
709 int64_t unwrap_timestamp =
710 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
711 audioFrame->elapsed_time_ms_ =
712 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700713 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800714
niklase@google.com470e71d2011-07-07 08:21:25 +0000715 {
kwiberg55b97fe2016-01-28 05:22:45 -0800716 rtc::CritScope lock(&ts_stats_lock_);
717 // Compute ntp time.
718 audioFrame->ntp_time_ms_ =
719 ntp_estimator_.Estimate(audioFrame->timestamp_);
720 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
721 if (audioFrame->ntp_time_ms_ > 0) {
722 // Compute |capture_start_ntp_time_ms_| so that
723 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
724 capture_start_ntp_time_ms_ =
725 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000726 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000727 }
kwiberg55b97fe2016-01-28 05:22:45 -0800728 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000729
henrik.lundin42dda502016-05-18 05:36:01 -0700730 return muted ? MixerParticipant::AudioFrameInfo::kMuted
731 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000732}
733
aleloi6c278492016-10-20 14:24:39 -0700734AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
735 int sample_rate_hz,
736 AudioFrame* audio_frame) {
737 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700738
aleloi6c278492016-10-20 14:24:39 -0700739 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700740
741 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
742 FrameInfo new_audio_frame_info = FrameInfo::kError;
743 switch (frame_info) {
744 case MixerParticipant::AudioFrameInfo::kNormal:
745 new_audio_frame_info = FrameInfo::kNormal;
746 break;
747 case MixerParticipant::AudioFrameInfo::kMuted:
748 new_audio_frame_info = FrameInfo::kMuted;
749 break;
750 case MixerParticipant::AudioFrameInfo::kError:
751 new_audio_frame_info = FrameInfo::kError;
752 break;
753 }
aleloi6c278492016-10-20 14:24:39 -0700754 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700755}
756
kwiberg55b97fe2016-01-28 05:22:45 -0800757int32_t Channel::NeededFrequency(int32_t id) const {
758 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
759 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000760
kwiberg55b97fe2016-01-28 05:22:45 -0800761 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000762
kwiberg55b97fe2016-01-28 05:22:45 -0800763 // Determine highest needed receive frequency
764 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000765
kwiberg55b97fe2016-01-28 05:22:45 -0800766 // Return the bigger of playout and receive frequency in the ACM.
767 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
768 highestNeeded = audio_coding_->PlayoutFrequency();
769 } else {
770 highestNeeded = receiveFrequency;
771 }
772
773 // Special case, if we're playing a file on the playout side
774 // we take that frequency into consideration as well
775 // This is not needed on sending side, since the codec will
776 // limit the spectrum anyway.
777 if (channel_state_.Get().output_file_playing) {
778 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700779 if (output_file_player_) {
780 if (output_file_player_->Frequency() > highestNeeded) {
781 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800782 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000783 }
kwiberg55b97fe2016-01-28 05:22:45 -0800784 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000785
kwiberg55b97fe2016-01-28 05:22:45 -0800786 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000787}
788
ossu5f7cfa52016-05-30 08:11:28 -0700789int32_t Channel::CreateChannel(
790 Channel*& channel,
791 int32_t channelId,
792 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700793 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800794 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
795 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
796 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000797
solenberg88499ec2016-09-07 07:34:41 -0700798 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800799 if (channel == NULL) {
800 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
801 "Channel::CreateChannel() unable to allocate memory for"
802 " channel");
803 return -1;
804 }
805 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000806}
807
kwiberg55b97fe2016-01-28 05:22:45 -0800808void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
809 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
810 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
811 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000812
kwiberg55b97fe2016-01-28 05:22:45 -0800813 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000814}
815
kwiberg55b97fe2016-01-28 05:22:45 -0800816void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
817 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
818 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
819 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000820
kwiberg55b97fe2016-01-28 05:22:45 -0800821 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000822}
823
kwiberg55b97fe2016-01-28 05:22:45 -0800824void Channel::PlayFileEnded(int32_t id) {
825 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
826 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000827
kwiberg55b97fe2016-01-28 05:22:45 -0800828 if (id == _inputFilePlayerId) {
829 channel_state_.SetInputFilePlaying(false);
830 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
831 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000832 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800833 } else if (id == _outputFilePlayerId) {
834 channel_state_.SetOutputFilePlaying(false);
835 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
836 "Channel::PlayFileEnded() => output file player module is"
837 " shutdown");
838 }
839}
840
841void Channel::RecordFileEnded(int32_t id) {
842 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
843 "Channel::RecordFileEnded(id=%d)", id);
844
845 assert(id == _outputFileRecorderId);
846
847 rtc::CritScope cs(&_fileCritSect);
848
849 _outputFileRecording = false;
850 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
851 "Channel::RecordFileEnded() => output file recorder module is"
852 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000853}
854
pbos@webrtc.org92135212013-05-14 08:31:39 +0000855Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000856 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700857 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800858 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100859 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700860 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800861 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100862 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800863 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100864 rtp_receive_statistics_(
865 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
866 rtp_receiver_(
867 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100868 this,
869 this,
870 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700871 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100872 _outputAudioLevel(),
873 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100874 // Avoid conflict with other channels by adding 1024 - 1026,
875 // won't use as much as 1024 channels.
876 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
877 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
878 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
879 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100880 _timeStamp(0), // This is just an offset, RTP module will add it's own
881 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100882 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100883 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100884 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100885 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100886 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
887 capture_start_rtp_time_stamp_(-1),
888 capture_start_ntp_time_ms_(-1),
889 _engineStatisticsPtr(NULL),
890 _outputMixerPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100891 _moduleProcessThreadPtr(NULL),
892 _audioDeviceModulePtr(NULL),
893 _voiceEngineObserverPtr(NULL),
894 _callbackCritSectPtr(NULL),
895 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100896 _sendFrameType(0),
solenberg1c2af8e2016-03-24 10:36:00 -0700897 input_mute_(false),
898 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100899 _outputGain(1.0f),
solenberg8d73f8c2017-03-08 01:52:20 -0800900 _mixFileWithMicrophone(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100901 _lastLocalTimeStamp(0),
902 _lastPayloadType(0),
903 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800904 transport_overhead_per_packet_(0),
905 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100906 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100907 restored_packet_in_use_(false),
908 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100909 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700910 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800911 feedback_observer_proxy_(new TransportFeedbackProxy()),
912 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700913 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200914 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
915 kMaxRetransmissionWindowMs)),
michaelt566d8202017-01-12 10:17:38 -0800916 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800917 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
918 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700919 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800920 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700921 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800922 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200923
kwiberg55b97fe2016-01-28 05:22:45 -0800924 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000925
kwiberg55b97fe2016-01-28 05:22:45 -0800926 RtpRtcp::Configuration configuration;
927 configuration.audio = true;
928 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800929 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800930 configuration.receive_statistics = rtp_receive_statistics_.get();
931 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800932 if (pacing_enabled_) {
933 configuration.paced_sender = rtp_packet_sender_proxy_.get();
934 configuration.transport_sequence_number_allocator =
935 seq_num_allocator_proxy_.get();
936 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
937 }
ivoc14d5dbe2016-07-04 07:06:55 -0700938 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800939 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200940 configuration.retransmission_rate_limiter =
941 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000942
kwiberg55b97fe2016-01-28 05:22:45 -0800943 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100944 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000945}
946
kwiberg55b97fe2016-01-28 05:22:45 -0800947Channel::~Channel() {
948 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
949 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
950 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000951
kwiberg55b97fe2016-01-28 05:22:45 -0800952 StopSend();
953 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000954
kwiberg55b97fe2016-01-28 05:22:45 -0800955 {
956 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700957 if (input_file_player_) {
958 input_file_player_->RegisterModuleFileCallback(NULL);
959 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000960 }
kwiberg5a25d952016-08-17 07:31:12 -0700961 if (output_file_player_) {
962 output_file_player_->RegisterModuleFileCallback(NULL);
963 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800964 }
kwiberg5a25d952016-08-17 07:31:12 -0700965 if (output_file_recorder_) {
966 output_file_recorder_->RegisterModuleFileCallback(NULL);
967 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800968 }
969 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000970
kwiberg55b97fe2016-01-28 05:22:45 -0800971 // The order to safely shutdown modules in a channel is:
972 // 1. De-register callbacks in modules
973 // 2. De-register modules in process thread
974 // 3. Destroy modules
975 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
976 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
977 "~Channel() failed to de-register transport callback"
978 " (Audio coding module)");
979 }
980 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
981 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
982 "~Channel() failed to de-register VAD callback"
983 " (Audio coding module)");
984 }
985 // De-register modules in process thread
986 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000987
kwiberg55b97fe2016-01-28 05:22:45 -0800988 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000989}
990
kwiberg55b97fe2016-01-28 05:22:45 -0800991int32_t Channel::Init() {
992 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
993 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000994
kwiberg55b97fe2016-01-28 05:22:45 -0800995 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000996
kwiberg55b97fe2016-01-28 05:22:45 -0800997 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000998
kwiberg55b97fe2016-01-28 05:22:45 -0800999 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
1000 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1001 "Channel::Init() must call SetEngineInformation() first");
1002 return -1;
1003 }
1004
1005 // --- Add modules to process thread (for periodic schedulation)
1006
tommidea489f2017-03-03 03:20:24 -08001007 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
kwiberg55b97fe2016-01-28 05:22:45 -08001008
1009 // --- ACM initialization
1010
1011 if (audio_coding_->InitializeReceiver() == -1) {
1012 _engineStatisticsPtr->SetLastError(
1013 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1014 "Channel::Init() unable to initialize the ACM - 1");
1015 return -1;
1016 }
1017
1018 // --- RTP/RTCP module initialization
1019
1020 // Ensure that RTCP is enabled by default for the created channel.
1021 // Note that, the module will keep generating RTCP until it is explicitly
1022 // disabled by the user.
1023 // After StopListen (when no sockets exists), RTCP packets will no longer
1024 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001025 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001026 // RTCP is enabled by default.
1027 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1028 // --- Register all permanent callbacks
1029 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1030 (audio_coding_->RegisterVADCallback(this) == -1);
1031
1032 if (fail) {
1033 _engineStatisticsPtr->SetLastError(
1034 VE_CANNOT_INIT_CHANNEL, kTraceError,
1035 "Channel::Init() callbacks not registered");
1036 return -1;
1037 }
1038
1039 // --- Register all supported codecs to the receiving side of the
1040 // RTP/RTCP module
1041
1042 CodecInst codec;
1043 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1044
1045 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1046 // Open up the RTP/RTCP receiver for all supported codecs
1047 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08001048 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001049 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1050 "Channel::Init() unable to register %s "
1051 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1052 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1053 codec.rate);
1054 } else {
1055 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1056 "Channel::Init() %s (%d/%d/%" PRIuS
1057 "/%d) has been "
1058 "added to the RTP/RTCP receiver",
1059 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1060 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001061 }
1062
kwiberg55b97fe2016-01-28 05:22:45 -08001063 // Ensure that PCMU is used as default codec on the sending side
1064 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1065 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001066 }
1067
kwiberg55b97fe2016-01-28 05:22:45 -08001068 // Register default PT for outband 'telephone-event'
1069 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001070 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001071 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1072 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001073 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1074 "Channel::Init() failed to register outband "
1075 "'telephone-event' (%d/%d) correctly",
1076 codec.pltype, codec.plfreq);
1077 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001078 }
1079
kwiberg55b97fe2016-01-28 05:22:45 -08001080 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001081 if (!codec_manager_.RegisterEncoder(codec) ||
1082 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001083 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1084 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001085 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001086 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1087 "Channel::Init() failed to register CN (%d/%d) "
1088 "correctly - 1",
1089 codec.pltype, codec.plfreq);
1090 }
1091 }
kwiberg55b97fe2016-01-28 05:22:45 -08001092 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001093
kwiberg55b97fe2016-01-28 05:22:45 -08001094 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001095}
1096
kwiberg55b97fe2016-01-28 05:22:45 -08001097int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1098 OutputMixer& outputMixer,
kwiberg55b97fe2016-01-28 05:22:45 -08001099 ProcessThread& moduleProcessThread,
1100 AudioDeviceModule& audioDeviceModule,
1101 VoiceEngineObserver* voiceEngineObserver,
1102 rtc::CriticalSection* callbackCritSect) {
1103 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1104 "Channel::SetEngineInformation()");
1105 _engineStatisticsPtr = &engineStatistics;
1106 _outputMixerPtr = &outputMixer;
kwiberg55b97fe2016-01-28 05:22:45 -08001107 _moduleProcessThreadPtr = &moduleProcessThread;
1108 _audioDeviceModulePtr = &audioDeviceModule;
1109 _voiceEngineObserverPtr = voiceEngineObserver;
1110 _callbackCritSectPtr = callbackCritSect;
1111 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001112}
1113
kwiberg55b97fe2016-01-28 05:22:45 -08001114int32_t Channel::UpdateLocalTimeStamp() {
1115 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1116 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001117}
1118
kwibergb7f89d62016-02-17 10:04:18 -08001119void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001120 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001121 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001122}
1123
ossu29b1a8d2016-06-13 07:34:51 -07001124const rtc::scoped_refptr<AudioDecoderFactory>&
1125Channel::GetAudioDecoderFactory() const {
1126 return decoder_factory_;
1127}
1128
kwiberg55b97fe2016-01-28 05:22:45 -08001129int32_t Channel::StartPlayout() {
1130 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1131 "Channel::StartPlayout()");
1132 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001133 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001134 }
1135
solenberge374e012017-02-14 04:55:00 -08001136 // Add participant as candidates for mixing.
1137 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1138 _engineStatisticsPtr->SetLastError(
1139 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1140 "StartPlayout() failed to add participant to mixer");
1141 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001142 }
1143
1144 channel_state_.SetPlaying(true);
1145 if (RegisterFilePlayingToMixer() != 0)
1146 return -1;
1147
1148 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001149}
1150
kwiberg55b97fe2016-01-28 05:22:45 -08001151int32_t Channel::StopPlayout() {
1152 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1153 "Channel::StopPlayout()");
1154 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001155 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001156 }
1157
solenberge374e012017-02-14 04:55:00 -08001158 // Remove participant as candidates for mixing
1159 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1160 _engineStatisticsPtr->SetLastError(
1161 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1162 "StopPlayout() failed to remove participant from mixer");
1163 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001164 }
1165
1166 channel_state_.SetPlaying(false);
1167 _outputAudioLevel.Clear();
1168
1169 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001170}
1171
kwiberg55b97fe2016-01-28 05:22:45 -08001172int32_t Channel::StartSend() {
1173 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1174 "Channel::StartSend()");
kwiberg55b97fe2016-01-28 05:22:45 -08001175 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001176 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001177 }
1178 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001179
solenberg08b19df2017-02-15 00:42:31 -08001180 // Resume the previous sequence number which was reset by StopSend(). This
1181 // needs to be done before |sending| is set to true on the RTP/RTCP module.
1182 if (send_sequence_number_) {
1183 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
1184 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001185 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001186 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1187 _engineStatisticsPtr->SetLastError(
1188 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1189 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001190 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001191 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001192 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001193 return -1;
1194 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001195
kwiberg55b97fe2016-01-28 05:22:45 -08001196 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001197}
1198
kwiberg55b97fe2016-01-28 05:22:45 -08001199int32_t Channel::StopSend() {
1200 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1201 "Channel::StopSend()");
1202 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001203 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001204 }
1205 channel_state_.SetSending(false);
1206
1207 // Store the sequence number to be able to pick up the same sequence for
1208 // the next StartSend(). This is needed for restarting device, otherwise
1209 // it might cause libSRTP to complain about packets being replayed.
1210 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1211 // CL is landed. See issue
1212 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1213 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1214
1215 // Reset sending SSRC and sequence number and triggers direct transmission
1216 // of RTCP BYE
1217 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1218 _engineStatisticsPtr->SetLastError(
1219 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1220 "StartSend() RTP/RTCP failed to stop sending");
1221 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001222 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001223
1224 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001225}
1226
kwiberg55b97fe2016-01-28 05:22:45 -08001227int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1228 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1229 "Channel::RegisterVoiceEngineObserver()");
1230 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001231
kwiberg55b97fe2016-01-28 05:22:45 -08001232 if (_voiceEngineObserverPtr) {
1233 _engineStatisticsPtr->SetLastError(
1234 VE_INVALID_OPERATION, kTraceError,
1235 "RegisterVoiceEngineObserver() observer already enabled");
1236 return -1;
1237 }
1238 _voiceEngineObserverPtr = &observer;
1239 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001240}
1241
kwiberg55b97fe2016-01-28 05:22:45 -08001242int32_t Channel::DeRegisterVoiceEngineObserver() {
1243 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1244 "Channel::DeRegisterVoiceEngineObserver()");
1245 rtc::CritScope cs(&_callbackCritSect);
1246
1247 if (!_voiceEngineObserverPtr) {
1248 _engineStatisticsPtr->SetLastError(
1249 VE_INVALID_OPERATION, kTraceWarning,
1250 "DeRegisterVoiceEngineObserver() observer already disabled");
1251 return 0;
1252 }
1253 _voiceEngineObserverPtr = NULL;
1254 return 0;
1255}
1256
1257int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001258 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001259 if (send_codec) {
1260 codec = *send_codec;
1261 return 0;
1262 }
1263 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001264}
1265
kwiberg55b97fe2016-01-28 05:22:45 -08001266int32_t Channel::GetRecCodec(CodecInst& codec) {
1267 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001268}
1269
kwiberg55b97fe2016-01-28 05:22:45 -08001270int32_t Channel::SetSendCodec(const CodecInst& codec) {
1271 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1272 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001273
kwibergc8d071e2016-04-06 12:22:38 -07001274 if (!codec_manager_.RegisterEncoder(codec) ||
1275 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001276 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1277 "SetSendCodec() failed to register codec to ACM");
1278 return -1;
1279 }
1280
1281 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1282 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1283 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1284 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1285 "SetSendCodec() failed to register codec to"
1286 " RTP/RTCP module");
1287 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001288 }
kwiberg55b97fe2016-01-28 05:22:45 -08001289 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001290
kwiberg55b97fe2016-01-28 05:22:45 -08001291 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001292}
1293
minyue78b4d562016-11-30 04:47:39 -08001294void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001295 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1296 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001297 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001298 if (*encoder) {
1299 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001300 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001301 }
1302 });
michaelt566d8202017-01-12 10:17:38 -08001303 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001304}
1305
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001306void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001307 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1308 if (*encoder)
1309 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1310 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001311}
1312
kwiberg55b97fe2016-01-28 05:22:45 -08001313int32_t Channel::SetVADStatus(bool enableVAD,
1314 ACMVADMode mode,
1315 bool disableDTX) {
1316 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1317 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001318 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1319 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1320 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001321 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1322 kTraceError,
1323 "SetVADStatus() failed to set VAD");
1324 return -1;
1325 }
1326 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001327}
1328
kwiberg55b97fe2016-01-28 05:22:45 -08001329int32_t Channel::GetVADStatus(bool& enabledVAD,
1330 ACMVADMode& mode,
1331 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001332 const auto* params = codec_manager_.GetStackParams();
1333 enabledVAD = params->use_cng;
1334 mode = params->vad_mode;
1335 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001336 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001337}
1338
kwiberg55b97fe2016-01-28 05:22:45 -08001339int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001340 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1341}
1342
1343int32_t Channel::SetRecPayloadType(int payload_type,
1344 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001345 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1346 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001347
kwiberg55b97fe2016-01-28 05:22:45 -08001348 if (channel_state_.Get().playing) {
1349 _engineStatisticsPtr->SetLastError(
1350 VE_ALREADY_PLAYING, kTraceError,
1351 "SetRecPayloadType() unable to set PT while playing");
1352 return -1;
1353 }
kwiberg55b97fe2016-01-28 05:22:45 -08001354
kwiberg09f090c2017-03-01 01:57:11 -08001355 const CodecInst codec = SdpToCodecInst(payload_type, format);
kwibergd32bf752017-01-19 07:03:59 -08001356
1357 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001358 // De-register the selected codec (RTP/RTCP module and ACM)
1359
1360 int8_t pltype(-1);
1361 CodecInst rxCodec = codec;
1362
1363 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001364 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001365 rxCodec.pltype = pltype;
1366
1367 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1368 _engineStatisticsPtr->SetLastError(
1369 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1370 "SetRecPayloadType() RTP/RTCP-module deregistration "
1371 "failed");
1372 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001373 }
kwiberg55b97fe2016-01-28 05:22:45 -08001374 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1375 _engineStatisticsPtr->SetLastError(
1376 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1377 "SetRecPayloadType() ACM deregistration failed - 1");
1378 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001379 }
kwiberg55b97fe2016-01-28 05:22:45 -08001380 return 0;
1381 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001382
magjed56124bd2016-11-24 09:34:46 -08001383 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001384 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001385 // TODO(kwiberg): Retrying is probably not necessary, since
1386 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001387 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001388 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001389 _engineStatisticsPtr->SetLastError(
1390 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1391 "SetRecPayloadType() RTP/RTCP-module registration failed");
1392 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001393 }
kwiberg55b97fe2016-01-28 05:22:45 -08001394 }
kwibergd32bf752017-01-19 07:03:59 -08001395 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1396 audio_coding_->UnregisterReceiveCodec(payload_type);
1397 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001398 _engineStatisticsPtr->SetLastError(
1399 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1400 "SetRecPayloadType() ACM registration failed - 1");
1401 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001402 }
kwiberg55b97fe2016-01-28 05:22:45 -08001403 }
1404 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001405}
1406
kwiberg55b97fe2016-01-28 05:22:45 -08001407int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1408 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001409 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001410 _engineStatisticsPtr->SetLastError(
1411 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1412 "GetRecPayloadType() failed to retrieve RX payload type");
1413 return -1;
1414 }
1415 codec.pltype = payloadType;
1416 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001417}
1418
kwiberg55b97fe2016-01-28 05:22:45 -08001419int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1420 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1421 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001422
kwiberg55b97fe2016-01-28 05:22:45 -08001423 CodecInst codec;
1424 int32_t samplingFreqHz(-1);
1425 const size_t kMono = 1;
1426 if (frequency == kFreq32000Hz)
1427 samplingFreqHz = 32000;
1428 else if (frequency == kFreq16000Hz)
1429 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001430
kwiberg55b97fe2016-01-28 05:22:45 -08001431 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1432 _engineStatisticsPtr->SetLastError(
1433 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1434 "SetSendCNPayloadType() failed to retrieve default CN codec "
1435 "settings");
1436 return -1;
1437 }
1438
1439 // Modify the payload type (must be set to dynamic range)
1440 codec.pltype = type;
1441
kwibergc8d071e2016-04-06 12:22:38 -07001442 if (!codec_manager_.RegisterEncoder(codec) ||
1443 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001444 _engineStatisticsPtr->SetLastError(
1445 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1446 "SetSendCNPayloadType() failed to register CN to ACM");
1447 return -1;
1448 }
1449
1450 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1451 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1452 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1453 _engineStatisticsPtr->SetLastError(
1454 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1455 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1456 "module");
1457 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001458 }
kwiberg55b97fe2016-01-28 05:22:45 -08001459 }
1460 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001461}
1462
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001463int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001464 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001465 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001466
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001467 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001468 _engineStatisticsPtr->SetLastError(
1469 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001470 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001471 return -1;
1472 }
1473 return 0;
1474}
1475
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001476int Channel::SetOpusDtx(bool enable_dtx) {
1477 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1478 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001479 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001480 : audio_coding_->DisableOpusDtx();
1481 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001482 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1483 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001484 return -1;
1485 }
1486 return 0;
1487}
1488
ivoc85228d62016-07-27 04:53:47 -07001489int Channel::GetOpusDtx(bool* enabled) {
1490 int success = -1;
1491 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1492 if (encoder) {
1493 *enabled = encoder->GetDtx();
1494 success = 0;
1495 }
1496 });
1497 return success;
1498}
1499
minyue7e304322016-10-12 05:00:55 -07001500bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1501 bool success = false;
1502 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1503 if (*encoder) {
1504 success = (*encoder)->EnableAudioNetworkAdaptor(
michaeltbf279fc2017-01-13 06:02:29 -08001505 config_string, event_log_proxy_.get(), Clock::GetRealTimeClock());
minyue7e304322016-10-12 05:00:55 -07001506 }
1507 });
1508 return success;
1509}
1510
1511void Channel::DisableAudioNetworkAdaptor() {
1512 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1513 if (*encoder)
1514 (*encoder)->DisableAudioNetworkAdaptor();
1515 });
1516}
1517
1518void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1519 int max_frame_length_ms) {
1520 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1521 if (*encoder) {
1522 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1523 max_frame_length_ms);
1524 }
1525 });
1526}
1527
mflodman3d7db262016-04-29 00:57:13 -07001528int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001529 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001530 "Channel::RegisterExternalTransport()");
1531
kwiberg55b97fe2016-01-28 05:22:45 -08001532 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001533 if (_externalTransport) {
1534 _engineStatisticsPtr->SetLastError(
1535 VE_INVALID_OPERATION, kTraceError,
1536 "RegisterExternalTransport() external transport already enabled");
1537 return -1;
1538 }
1539 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001540 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001541 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001542}
1543
kwiberg55b97fe2016-01-28 05:22:45 -08001544int32_t Channel::DeRegisterExternalTransport() {
1545 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1546 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001547
kwiberg55b97fe2016-01-28 05:22:45 -08001548 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001549 if (_transportPtr) {
1550 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1551 "DeRegisterExternalTransport() all transport is disabled");
1552 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001553 _engineStatisticsPtr->SetLastError(
1554 VE_INVALID_OPERATION, kTraceWarning,
1555 "DeRegisterExternalTransport() external transport already "
1556 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001557 }
1558 _externalTransport = false;
1559 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001560 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001561}
1562
nisse657bab22017-02-21 06:28:10 -08001563// TODO(nisse): Delete this method together with ReceivedRTPPacket.
1564// It's a temporary hack to support both ReceivedRTPPacket and
1565// OnRtpPacket interfaces without too much code duplication.
1566bool Channel::OnRtpPacketWithHeader(const uint8_t* received_packet,
1567 size_t length,
1568 RTPHeader *header) {
1569 // Store playout timestamp for the received RTP packet
1570 UpdatePlayoutTimestamp(false);
1571
1572 header->payload_type_frequency =
1573 rtp_payload_registry_->GetPayloadTypeFrequency(header->payloadType);
1574 if (header->payload_type_frequency < 0)
1575 return false;
1576 bool in_order = IsPacketInOrder(*header);
1577 rtp_receive_statistics_->IncomingPacket(
1578 *header, length, IsPacketRetransmitted(*header, in_order));
1579 rtp_payload_registry_->SetIncomingPayloadType(*header);
1580
1581 return ReceivePacket(received_packet, length, *header, in_order);
1582}
1583
mflodman3d7db262016-04-29 00:57:13 -07001584int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001585 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001586 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001587 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001588 "Channel::ReceivedRTPPacket()");
1589
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001590 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001591 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1592 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1593 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001594 return -1;
1595 }
nisse657bab22017-02-21 06:28:10 -08001596 return OnRtpPacketWithHeader(received_packet, length, &header) ? 0 : -1;
1597}
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001598
nisse657bab22017-02-21 06:28:10 -08001599void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
1600 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
1601 "Channel::ReceivedRTPPacket()");
1602
1603 RTPHeader header;
1604 packet.GetHeader(&header);
1605 OnRtpPacketWithHeader(packet.data(), packet.size(), &header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001606}
1607
1608bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001609 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001610 const RTPHeader& header,
1611 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001612 if (rtp_payload_registry_->IsRtx(header)) {
1613 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001614 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001615 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001616 assert(packet_length >= header.headerLength);
1617 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001618 PayloadUnion payload_specific;
1619 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001620 &payload_specific)) {
1621 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001622 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001623 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1624 payload_specific, in_order);
1625}
1626
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001627bool Channel::HandleRtxPacket(const uint8_t* packet,
1628 size_t packet_length,
1629 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001630 if (!rtp_payload_registry_->IsRtx(header))
1631 return false;
1632
1633 // Remove the RTX header and parse the original RTP header.
1634 if (packet_length < header.headerLength)
1635 return false;
1636 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1637 return false;
1638 if (restored_packet_in_use_) {
1639 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1640 "Multiple RTX headers detected, dropping packet");
1641 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001642 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001643 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001644 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1645 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001646 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1647 "Incoming RTX packet: invalid RTP header");
1648 return false;
1649 }
1650 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001651 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001652 restored_packet_in_use_ = false;
1653 return ret;
1654}
1655
1656bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1657 StreamStatistician* statistician =
1658 rtp_receive_statistics_->GetStatistician(header.ssrc);
1659 if (!statistician)
1660 return false;
1661 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001662}
1663
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001664bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1665 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001666 // Retransmissions are handled separately if RTX is enabled.
1667 if (rtp_payload_registry_->RtxEnabled())
1668 return false;
1669 StreamStatistician* statistician =
1670 rtp_receive_statistics_->GetStatistician(header.ssrc);
1671 if (!statistician)
1672 return false;
1673 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001674 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001675 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001676 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001677}
1678
mflodman3d7db262016-04-29 00:57:13 -07001679int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001680 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001681 "Channel::ReceivedRTCPPacket()");
1682 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001683 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001684
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001685 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001686 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001687 _engineStatisticsPtr->SetLastError(
1688 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1689 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1690 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001691
Minyue2013aec2015-05-13 14:14:42 +02001692 int64_t rtt = GetRTT(true);
1693 if (rtt == 0) {
1694 // Waiting for valid RTT.
1695 return 0;
1696 }
Erik Språng737336d2016-07-29 12:59:36 +02001697
1698 int64_t nack_window_ms = rtt;
1699 if (nack_window_ms < kMinRetransmissionWindowMs) {
1700 nack_window_ms = kMinRetransmissionWindowMs;
1701 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1702 nack_window_ms = kMaxRetransmissionWindowMs;
1703 }
1704 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1705
minyue7e304322016-10-12 05:00:55 -07001706 // Invoke audio encoders OnReceivedRtt().
1707 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1708 if (*encoder)
1709 (*encoder)->OnReceivedRtt(rtt);
1710 });
1711
Minyue2013aec2015-05-13 14:14:42 +02001712 uint32_t ntp_secs = 0;
1713 uint32_t ntp_frac = 0;
1714 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001715 if (0 !=
1716 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1717 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001718 // Waiting for RTCP.
1719 return 0;
1720 }
1721
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001722 {
tommi31fc21f2016-01-21 10:37:37 -08001723 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001724 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001725 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001726 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001727}
1728
niklase@google.com470e71d2011-07-07 08:21:25 +00001729int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001730 bool loop,
1731 FileFormats format,
1732 int startPosition,
1733 float volumeScaling,
1734 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001735 const CodecInst* codecInst) {
1736 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1737 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1738 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1739 "stopPosition=%d)",
1740 fileName, loop, format, volumeScaling, startPosition,
1741 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001742
kwiberg55b97fe2016-01-28 05:22:45 -08001743 if (channel_state_.Get().output_file_playing) {
1744 _engineStatisticsPtr->SetLastError(
1745 VE_ALREADY_PLAYING, kTraceError,
1746 "StartPlayingFileLocally() is already playing");
1747 return -1;
1748 }
1749
1750 {
1751 rtc::CritScope cs(&_fileCritSect);
1752
kwiberg5a25d952016-08-17 07:31:12 -07001753 if (output_file_player_) {
1754 output_file_player_->RegisterModuleFileCallback(NULL);
1755 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001756 }
1757
kwiberg5b356f42016-09-08 04:32:33 -07001758 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001759 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001760
kwiberg5a25d952016-08-17 07:31:12 -07001761 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001762 _engineStatisticsPtr->SetLastError(
1763 VE_INVALID_ARGUMENT, kTraceError,
1764 "StartPlayingFileLocally() filePlayer format is not correct");
1765 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001766 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001767
kwiberg55b97fe2016-01-28 05:22:45 -08001768 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001769
kwiberg5a25d952016-08-17 07:31:12 -07001770 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001771 fileName, loop, startPosition, volumeScaling, notificationTime,
1772 stopPosition, (const CodecInst*)codecInst) != 0) {
1773 _engineStatisticsPtr->SetLastError(
1774 VE_BAD_FILE, kTraceError,
1775 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001776 output_file_player_->StopPlayingFile();
1777 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001778 return -1;
1779 }
kwiberg5a25d952016-08-17 07:31:12 -07001780 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001781 channel_state_.SetOutputFilePlaying(true);
1782 }
1783
1784 if (RegisterFilePlayingToMixer() != 0)
1785 return -1;
1786
1787 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001788}
1789
1790int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001791 FileFormats format,
1792 int startPosition,
1793 float volumeScaling,
1794 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001795 const CodecInst* codecInst) {
1796 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1797 "Channel::StartPlayingFileLocally(format=%d,"
1798 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1799 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001800
kwiberg55b97fe2016-01-28 05:22:45 -08001801 if (stream == NULL) {
1802 _engineStatisticsPtr->SetLastError(
1803 VE_BAD_FILE, kTraceError,
1804 "StartPlayingFileLocally() NULL as input stream");
1805 return -1;
1806 }
1807
1808 if (channel_state_.Get().output_file_playing) {
1809 _engineStatisticsPtr->SetLastError(
1810 VE_ALREADY_PLAYING, kTraceError,
1811 "StartPlayingFileLocally() is already playing");
1812 return -1;
1813 }
1814
1815 {
1816 rtc::CritScope cs(&_fileCritSect);
1817
1818 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001819 if (output_file_player_) {
1820 output_file_player_->RegisterModuleFileCallback(NULL);
1821 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001822 }
1823
kwiberg55b97fe2016-01-28 05:22:45 -08001824 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001825 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001826 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001827
kwiberg5a25d952016-08-17 07:31:12 -07001828 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001829 _engineStatisticsPtr->SetLastError(
1830 VE_INVALID_ARGUMENT, kTraceError,
1831 "StartPlayingFileLocally() filePlayer format isnot correct");
1832 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001833 }
1834
kwiberg55b97fe2016-01-28 05:22:45 -08001835 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001836
kwiberg4ec01d92016-08-22 08:43:54 -07001837 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001838 volumeScaling, notificationTime,
1839 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001840 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1841 "StartPlayingFile() failed to "
1842 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001843 output_file_player_->StopPlayingFile();
1844 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001845 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001846 }
kwiberg5a25d952016-08-17 07:31:12 -07001847 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001848 channel_state_.SetOutputFilePlaying(true);
1849 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001850
kwiberg55b97fe2016-01-28 05:22:45 -08001851 if (RegisterFilePlayingToMixer() != 0)
1852 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001853
kwiberg55b97fe2016-01-28 05:22:45 -08001854 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001855}
1856
kwiberg55b97fe2016-01-28 05:22:45 -08001857int Channel::StopPlayingFileLocally() {
1858 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1859 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001860
kwiberg55b97fe2016-01-28 05:22:45 -08001861 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001862 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001863 }
1864
1865 {
1866 rtc::CritScope cs(&_fileCritSect);
1867
kwiberg5a25d952016-08-17 07:31:12 -07001868 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001869 _engineStatisticsPtr->SetLastError(
1870 VE_STOP_RECORDING_FAILED, kTraceError,
1871 "StopPlayingFile() could not stop playing");
1872 return -1;
1873 }
kwiberg5a25d952016-08-17 07:31:12 -07001874 output_file_player_->RegisterModuleFileCallback(NULL);
1875 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001876 channel_state_.SetOutputFilePlaying(false);
1877 }
1878 // _fileCritSect cannot be taken while calling
1879 // SetAnonymousMixibilityStatus. Refer to comments in
1880 // StartPlayingFileLocally(const char* ...) for more details.
1881 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1882 _engineStatisticsPtr->SetLastError(
1883 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1884 "StopPlayingFile() failed to stop participant from playing as"
1885 "file in the mixer");
1886 return -1;
1887 }
1888
1889 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001890}
1891
kwiberg55b97fe2016-01-28 05:22:45 -08001892int Channel::IsPlayingFileLocally() const {
1893 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001894}
1895
kwiberg55b97fe2016-01-28 05:22:45 -08001896int Channel::RegisterFilePlayingToMixer() {
1897 // Return success for not registering for file playing to mixer if:
1898 // 1. playing file before playout is started on that channel.
1899 // 2. starting playout without file playing on that channel.
1900 if (!channel_state_.Get().playing ||
1901 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001902 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001903 }
1904
1905 // |_fileCritSect| cannot be taken while calling
1906 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1907 // frames can be pulled by the mixer. Since the frames are generated from
1908 // the file, _fileCritSect will be taken. This would result in a deadlock.
1909 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1910 channel_state_.SetOutputFilePlaying(false);
1911 rtc::CritScope cs(&_fileCritSect);
1912 _engineStatisticsPtr->SetLastError(
1913 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1914 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001915 output_file_player_->StopPlayingFile();
1916 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001917 return -1;
1918 }
1919
1920 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001921}
1922
niklase@google.com470e71d2011-07-07 08:21:25 +00001923int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001924 bool loop,
1925 FileFormats format,
1926 int startPosition,
1927 float volumeScaling,
1928 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001929 const CodecInst* codecInst) {
1930 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1931 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1932 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1933 "stopPosition=%d)",
1934 fileName, loop, format, volumeScaling, startPosition,
1935 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001936
kwiberg55b97fe2016-01-28 05:22:45 -08001937 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001938
kwiberg55b97fe2016-01-28 05:22:45 -08001939 if (channel_state_.Get().input_file_playing) {
1940 _engineStatisticsPtr->SetLastError(
1941 VE_ALREADY_PLAYING, kTraceWarning,
1942 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001943 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001944 }
1945
1946 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001947 if (input_file_player_) {
1948 input_file_player_->RegisterModuleFileCallback(NULL);
1949 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001950 }
1951
1952 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001953 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001954 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001955
kwiberg5a25d952016-08-17 07:31:12 -07001956 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001957 _engineStatisticsPtr->SetLastError(
1958 VE_INVALID_ARGUMENT, kTraceError,
1959 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1960 return -1;
1961 }
1962
1963 const uint32_t notificationTime(0);
1964
kwiberg5a25d952016-08-17 07:31:12 -07001965 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001966 fileName, loop, startPosition, volumeScaling, notificationTime,
1967 stopPosition, (const CodecInst*)codecInst) != 0) {
1968 _engineStatisticsPtr->SetLastError(
1969 VE_BAD_FILE, kTraceError,
1970 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001971 input_file_player_->StopPlayingFile();
1972 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001973 return -1;
1974 }
kwiberg5a25d952016-08-17 07:31:12 -07001975 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001976 channel_state_.SetInputFilePlaying(true);
1977
1978 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001979}
1980
1981int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001982 FileFormats format,
1983 int startPosition,
1984 float volumeScaling,
1985 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001986 const CodecInst* codecInst) {
1987 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1988 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1989 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1990 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001991
kwiberg55b97fe2016-01-28 05:22:45 -08001992 if (stream == NULL) {
1993 _engineStatisticsPtr->SetLastError(
1994 VE_BAD_FILE, kTraceError,
1995 "StartPlayingFileAsMicrophone NULL as input stream");
1996 return -1;
1997 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001998
kwiberg55b97fe2016-01-28 05:22:45 -08001999 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002000
kwiberg55b97fe2016-01-28 05:22:45 -08002001 if (channel_state_.Get().input_file_playing) {
2002 _engineStatisticsPtr->SetLastError(
2003 VE_ALREADY_PLAYING, kTraceWarning,
2004 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002005 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002006 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002007
kwiberg55b97fe2016-01-28 05:22:45 -08002008 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002009 if (input_file_player_) {
2010 input_file_player_->RegisterModuleFileCallback(NULL);
2011 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002012 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002013
kwiberg55b97fe2016-01-28 05:22:45 -08002014 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002015 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002016 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002017
kwiberg5a25d952016-08-17 07:31:12 -07002018 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002019 _engineStatisticsPtr->SetLastError(
2020 VE_INVALID_ARGUMENT, kTraceError,
2021 "StartPlayingInputFile() filePlayer format isnot correct");
2022 return -1;
2023 }
2024
2025 const uint32_t notificationTime(0);
2026
kwiberg4ec01d92016-08-22 08:43:54 -07002027 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2028 notificationTime, stopPosition,
2029 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002030 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2031 "StartPlayingFile() failed to start "
2032 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002033 input_file_player_->StopPlayingFile();
2034 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002035 return -1;
2036 }
2037
kwiberg5a25d952016-08-17 07:31:12 -07002038 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002039 channel_state_.SetInputFilePlaying(true);
2040
2041 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002042}
2043
kwiberg55b97fe2016-01-28 05:22:45 -08002044int Channel::StopPlayingFileAsMicrophone() {
2045 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2046 "Channel::StopPlayingFileAsMicrophone()");
2047
2048 rtc::CritScope cs(&_fileCritSect);
2049
2050 if (!channel_state_.Get().input_file_playing) {
2051 return 0;
2052 }
2053
kwiberg5a25d952016-08-17 07:31:12 -07002054 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002055 _engineStatisticsPtr->SetLastError(
2056 VE_STOP_RECORDING_FAILED, kTraceError,
2057 "StopPlayingFile() could not stop playing");
2058 return -1;
2059 }
kwiberg5a25d952016-08-17 07:31:12 -07002060 input_file_player_->RegisterModuleFileCallback(NULL);
2061 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002062 channel_state_.SetInputFilePlaying(false);
2063
2064 return 0;
2065}
2066
2067int Channel::IsPlayingFileAsMicrophone() const {
2068 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002069}
2070
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002071int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002072 const CodecInst* codecInst) {
2073 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2074 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002075
kwiberg55b97fe2016-01-28 05:22:45 -08002076 if (_outputFileRecording) {
2077 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2078 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002079 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002080 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002081
kwiberg55b97fe2016-01-28 05:22:45 -08002082 FileFormats format;
2083 const uint32_t notificationTime(0); // Not supported in VoE
2084 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002085
kwiberg55b97fe2016-01-28 05:22:45 -08002086 if ((codecInst != NULL) &&
2087 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2088 _engineStatisticsPtr->SetLastError(
2089 VE_BAD_ARGUMENT, kTraceError,
2090 "StartRecordingPlayout() invalid compression");
2091 return (-1);
2092 }
2093 if (codecInst == NULL) {
2094 format = kFileFormatPcm16kHzFile;
2095 codecInst = &dummyCodec;
2096 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2097 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2098 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2099 format = kFileFormatWavFile;
2100 } else {
2101 format = kFileFormatCompressedFile;
2102 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002103
kwiberg55b97fe2016-01-28 05:22:45 -08002104 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002105
kwiberg55b97fe2016-01-28 05:22:45 -08002106 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002107 if (output_file_recorder_) {
2108 output_file_recorder_->RegisterModuleFileCallback(NULL);
2109 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002110 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002111
kwiberg5a25d952016-08-17 07:31:12 -07002112 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002113 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002114 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002115 _engineStatisticsPtr->SetLastError(
2116 VE_INVALID_ARGUMENT, kTraceError,
2117 "StartRecordingPlayout() fileRecorder format isnot correct");
2118 return -1;
2119 }
2120
kwiberg5a25d952016-08-17 07:31:12 -07002121 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002122 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2123 _engineStatisticsPtr->SetLastError(
2124 VE_BAD_FILE, kTraceError,
2125 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002126 output_file_recorder_->StopRecording();
2127 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002128 return -1;
2129 }
kwiberg5a25d952016-08-17 07:31:12 -07002130 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002131 _outputFileRecording = true;
2132
2133 return 0;
2134}
2135
2136int Channel::StartRecordingPlayout(OutStream* stream,
2137 const CodecInst* codecInst) {
2138 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2139 "Channel::StartRecordingPlayout()");
2140
2141 if (_outputFileRecording) {
2142 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2143 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002144 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002145 }
2146
2147 FileFormats format;
2148 const uint32_t notificationTime(0); // Not supported in VoE
2149 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2150
2151 if (codecInst != NULL && codecInst->channels != 1) {
2152 _engineStatisticsPtr->SetLastError(
2153 VE_BAD_ARGUMENT, kTraceError,
2154 "StartRecordingPlayout() invalid compression");
2155 return (-1);
2156 }
2157 if (codecInst == NULL) {
2158 format = kFileFormatPcm16kHzFile;
2159 codecInst = &dummyCodec;
2160 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2161 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2162 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2163 format = kFileFormatWavFile;
2164 } else {
2165 format = kFileFormatCompressedFile;
2166 }
2167
2168 rtc::CritScope cs(&_fileCritSect);
2169
2170 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002171 if (output_file_recorder_) {
2172 output_file_recorder_->RegisterModuleFileCallback(NULL);
2173 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002174 }
2175
kwiberg5a25d952016-08-17 07:31:12 -07002176 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002177 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002178 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002179 _engineStatisticsPtr->SetLastError(
2180 VE_INVALID_ARGUMENT, kTraceError,
2181 "StartRecordingPlayout() fileRecorder format isnot correct");
2182 return -1;
2183 }
2184
kwiberg4ec01d92016-08-22 08:43:54 -07002185 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002186 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002187 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2188 "StartRecordingPlayout() failed to "
2189 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002190 output_file_recorder_->StopRecording();
2191 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002192 return -1;
2193 }
2194
kwiberg5a25d952016-08-17 07:31:12 -07002195 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002196 _outputFileRecording = true;
2197
2198 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002199}
2200
kwiberg55b97fe2016-01-28 05:22:45 -08002201int Channel::StopRecordingPlayout() {
2202 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2203 "Channel::StopRecordingPlayout()");
2204
2205 if (!_outputFileRecording) {
2206 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2207 "StopRecordingPlayout() isnot recording");
2208 return -1;
2209 }
2210
2211 rtc::CritScope cs(&_fileCritSect);
2212
kwiberg5a25d952016-08-17 07:31:12 -07002213 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002214 _engineStatisticsPtr->SetLastError(
2215 VE_STOP_RECORDING_FAILED, kTraceError,
2216 "StopRecording() could not stop recording");
2217 return (-1);
2218 }
kwiberg5a25d952016-08-17 07:31:12 -07002219 output_file_recorder_->RegisterModuleFileCallback(NULL);
2220 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002221 _outputFileRecording = false;
2222
2223 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002224}
2225
kwiberg55b97fe2016-01-28 05:22:45 -08002226void Channel::SetMixWithMicStatus(bool mix) {
2227 rtc::CritScope cs(&_fileCritSect);
2228 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002229}
2230
solenberg8d73f8c2017-03-08 01:52:20 -08002231int Channel::GetSpeechOutputLevel() const {
2232 return _outputAudioLevel.Level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002233}
2234
solenberg8d73f8c2017-03-08 01:52:20 -08002235int Channel::GetSpeechOutputLevelFullRange() const {
2236 return _outputAudioLevel.LevelFullRange();
kwiberg55b97fe2016-01-28 05:22:45 -08002237}
2238
solenberg8d73f8c2017-03-08 01:52:20 -08002239void Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002240 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002241 input_mute_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002242}
2243
solenberg1c2af8e2016-03-24 10:36:00 -07002244bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002245 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002246 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002247}
2248
solenberg8d73f8c2017-03-08 01:52:20 -08002249void Channel::SetChannelOutputVolumeScaling(float scaling) {
kwiberg55b97fe2016-01-28 05:22:45 -08002250 rtc::CritScope cs(&volume_settings_critsect_);
kwiberg55b97fe2016-01-28 05:22:45 -08002251 _outputGain = scaling;
niklase@google.com470e71d2011-07-07 08:21:25 +00002252}
2253
solenberg8842c3e2016-03-11 03:06:41 -08002254int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002255 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002256 "Channel::SendTelephoneEventOutband(...)");
2257 RTC_DCHECK_LE(0, event);
2258 RTC_DCHECK_GE(255, event);
2259 RTC_DCHECK_LE(0, duration_ms);
2260 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002261 if (!Sending()) {
2262 return -1;
2263 }
solenberg8842c3e2016-03-11 03:06:41 -08002264 if (_rtpRtcpModule->SendTelephoneEventOutband(
2265 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002266 _engineStatisticsPtr->SetLastError(
2267 VE_SEND_DTMF_FAILED, kTraceWarning,
2268 "SendTelephoneEventOutband() failed to send event");
2269 return -1;
2270 }
2271 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002272}
2273
solenbergffbbcac2016-11-17 05:25:37 -08002274int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2275 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002276 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002277 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002278 RTC_DCHECK_LE(0, payload_type);
2279 RTC_DCHECK_GE(127, payload_type);
2280 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002281 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002282 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002283 memcpy(codec.plname, "telephone-event", 16);
2284 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2285 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2286 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2287 _engineStatisticsPtr->SetLastError(
2288 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2289 "SetSendTelephoneEventPayloadType() failed to register send"
2290 "payload type");
2291 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002292 }
kwiberg55b97fe2016-01-28 05:22:45 -08002293 }
kwiberg55b97fe2016-01-28 05:22:45 -08002294 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002295}
2296
kwiberg55b97fe2016-01-28 05:22:45 -08002297int Channel::VoiceActivityIndicator(int& activity) {
2298 activity = _sendFrameType;
2299 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002300}
2301
kwiberg55b97fe2016-01-28 05:22:45 -08002302int Channel::SetLocalSSRC(unsigned int ssrc) {
2303 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2304 "Channel::SetLocalSSRC()");
2305 if (channel_state_.Get().sending) {
2306 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2307 "SetLocalSSRC() already sending");
2308 return -1;
2309 }
2310 _rtpRtcpModule->SetSSRC(ssrc);
2311 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002312}
2313
kwiberg55b97fe2016-01-28 05:22:45 -08002314int Channel::GetLocalSSRC(unsigned int& ssrc) {
2315 ssrc = _rtpRtcpModule->SSRC();
2316 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002317}
2318
kwiberg55b97fe2016-01-28 05:22:45 -08002319int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2320 ssrc = rtp_receiver_->SSRC();
2321 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002322}
2323
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002324int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002325 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002326 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002327}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002328
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002329int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2330 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002331 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2332 if (enable &&
2333 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2334 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002335 return -1;
2336 }
2337 return 0;
2338}
2339
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002340void Channel::EnableSendTransportSequenceNumber(int id) {
2341 int ret =
2342 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2343 RTC_DCHECK_EQ(0, ret);
2344}
2345
stefan3313ec92016-01-21 06:32:43 -08002346void Channel::EnableReceiveTransportSequenceNumber(int id) {
2347 rtp_header_parser_->DeregisterRtpHeaderExtension(
2348 kRtpExtensionTransportSequenceNumber);
2349 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2350 kRtpExtensionTransportSequenceNumber, id);
2351 RTC_DCHECK(ret);
2352}
2353
stefanbba9dec2016-02-01 04:39:55 -08002354void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002355 RtpPacketSender* rtp_packet_sender,
2356 TransportFeedbackObserver* transport_feedback_observer,
stefan7de8d642017-02-07 07:14:08 -08002357 PacketRouter* packet_router,
2358 RtcpBandwidthObserver* bandwidth_observer) {
stefanbba9dec2016-02-01 04:39:55 -08002359 RTC_DCHECK(rtp_packet_sender);
2360 RTC_DCHECK(transport_feedback_observer);
2361 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002362 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002363 feedback_observer_proxy_->SetTransportFeedbackObserver(
2364 transport_feedback_observer);
2365 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2366 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2367 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002368 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002369 packet_router_ = packet_router;
2370}
2371
stefanbba9dec2016-02-01 04:39:55 -08002372void Channel::RegisterReceiverCongestionControlObjects(
2373 PacketRouter* packet_router) {
2374 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002375 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002376 packet_router_ = packet_router;
2377}
2378
2379void Channel::ResetCongestionControlObjects() {
2380 RTC_DCHECK(packet_router_);
2381 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002382 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002383 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2384 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002385 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002386 packet_router_ = nullptr;
2387 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2388}
2389
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002390void Channel::SetRTCPStatus(bool enable) {
2391 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2392 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002393 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002394}
2395
kwiberg55b97fe2016-01-28 05:22:45 -08002396int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002397 RtcpMode method = _rtpRtcpModule->RTCP();
2398 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002399 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002400}
2401
kwiberg55b97fe2016-01-28 05:22:45 -08002402int Channel::SetRTCP_CNAME(const char cName[256]) {
2403 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2404 "Channel::SetRTCP_CNAME()");
2405 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2406 _engineStatisticsPtr->SetLastError(
2407 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2408 "SetRTCP_CNAME() failed to set RTCP CNAME");
2409 return -1;
2410 }
2411 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002412}
2413
kwiberg55b97fe2016-01-28 05:22:45 -08002414int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2415 if (cName == NULL) {
2416 _engineStatisticsPtr->SetLastError(
2417 VE_INVALID_ARGUMENT, kTraceError,
2418 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2419 return -1;
2420 }
2421 char cname[RTCP_CNAME_SIZE];
2422 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2423 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2424 _engineStatisticsPtr->SetLastError(
2425 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2426 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2427 return -1;
2428 }
2429 strcpy(cName, cname);
2430 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002431}
2432
kwiberg55b97fe2016-01-28 05:22:45 -08002433int Channel::SendApplicationDefinedRTCPPacket(
2434 unsigned char subType,
2435 unsigned int name,
2436 const char* data,
2437 unsigned short dataLengthInBytes) {
2438 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2439 "Channel::SendApplicationDefinedRTCPPacket()");
2440 if (!channel_state_.Get().sending) {
2441 _engineStatisticsPtr->SetLastError(
2442 VE_NOT_SENDING, kTraceError,
2443 "SendApplicationDefinedRTCPPacket() not sending");
2444 return -1;
2445 }
2446 if (NULL == data) {
2447 _engineStatisticsPtr->SetLastError(
2448 VE_INVALID_ARGUMENT, kTraceError,
2449 "SendApplicationDefinedRTCPPacket() invalid data value");
2450 return -1;
2451 }
2452 if (dataLengthInBytes % 4 != 0) {
2453 _engineStatisticsPtr->SetLastError(
2454 VE_INVALID_ARGUMENT, kTraceError,
2455 "SendApplicationDefinedRTCPPacket() invalid length value");
2456 return -1;
2457 }
2458 RtcpMode status = _rtpRtcpModule->RTCP();
2459 if (status == RtcpMode::kOff) {
2460 _engineStatisticsPtr->SetLastError(
2461 VE_RTCP_ERROR, kTraceError,
2462 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2463 return -1;
2464 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002465
kwiberg55b97fe2016-01-28 05:22:45 -08002466 // Create and schedule the RTCP APP packet for transmission
2467 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2468 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2469 _engineStatisticsPtr->SetLastError(
2470 VE_SEND_ERROR, kTraceError,
2471 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2472 return -1;
2473 }
2474 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002475}
2476
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002477int Channel::GetRemoteRTCPReportBlocks(
2478 std::vector<ReportBlock>* report_blocks) {
2479 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002480 _engineStatisticsPtr->SetLastError(
2481 VE_INVALID_ARGUMENT, kTraceError,
2482 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002483 return -1;
2484 }
2485
2486 // Get the report blocks from the latest received RTCP Sender or Receiver
2487 // Report. Each element in the vector contains the sender's SSRC and a
2488 // report block according to RFC 3550.
2489 std::vector<RTCPReportBlock> rtcp_report_blocks;
2490 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002491 return -1;
2492 }
2493
2494 if (rtcp_report_blocks.empty())
2495 return 0;
2496
2497 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2498 for (; it != rtcp_report_blocks.end(); ++it) {
2499 ReportBlock report_block;
2500 report_block.sender_SSRC = it->remoteSSRC;
2501 report_block.source_SSRC = it->sourceSSRC;
2502 report_block.fraction_lost = it->fractionLost;
2503 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2504 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2505 report_block.interarrival_jitter = it->jitter;
2506 report_block.last_SR_timestamp = it->lastSR;
2507 report_block.delay_since_last_SR = it->delaySinceLastSR;
2508 report_blocks->push_back(report_block);
2509 }
2510 return 0;
2511}
2512
kwiberg55b97fe2016-01-28 05:22:45 -08002513int Channel::GetRTPStatistics(CallStatistics& stats) {
2514 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002515
kwiberg55b97fe2016-01-28 05:22:45 -08002516 // The jitter statistics is updated for each received RTP packet and is
2517 // based on received packets.
2518 RtcpStatistics statistics;
2519 StreamStatistician* statistician =
2520 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002521 if (statistician) {
2522 statistician->GetStatistics(&statistics,
2523 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002524 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002525
kwiberg55b97fe2016-01-28 05:22:45 -08002526 stats.fractionLost = statistics.fraction_lost;
2527 stats.cumulativeLost = statistics.cumulative_lost;
2528 stats.extendedMax = statistics.extended_max_sequence_number;
2529 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002530
kwiberg55b97fe2016-01-28 05:22:45 -08002531 // --- RTT
2532 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002533
kwiberg55b97fe2016-01-28 05:22:45 -08002534 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002535
kwiberg55b97fe2016-01-28 05:22:45 -08002536 size_t bytesSent(0);
2537 uint32_t packetsSent(0);
2538 size_t bytesReceived(0);
2539 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002540
kwiberg55b97fe2016-01-28 05:22:45 -08002541 if (statistician) {
2542 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2543 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002544
kwiberg55b97fe2016-01-28 05:22:45 -08002545 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2546 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2547 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2548 " output will not be complete");
2549 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002550
kwiberg55b97fe2016-01-28 05:22:45 -08002551 stats.bytesSent = bytesSent;
2552 stats.packetsSent = packetsSent;
2553 stats.bytesReceived = bytesReceived;
2554 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002555
kwiberg55b97fe2016-01-28 05:22:45 -08002556 // --- Timestamps
2557 {
2558 rtc::CritScope lock(&ts_stats_lock_);
2559 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2560 }
2561 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002562}
2563
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002564int Channel::SetCodecFECStatus(bool enable) {
2565 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2566 "Channel::SetCodecFECStatus()");
2567
kwibergc8d071e2016-04-06 12:22:38 -07002568 if (!codec_manager_.SetCodecFEC(enable) ||
2569 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002570 _engineStatisticsPtr->SetLastError(
2571 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2572 "SetCodecFECStatus() failed to set FEC state");
2573 return -1;
2574 }
2575 return 0;
2576}
2577
2578bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002579 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002580}
2581
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002582void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2583 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002584 // If pacing is enabled we always store packets.
2585 if (!pacing_enabled_)
2586 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002587 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002588 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002589 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002590 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002591 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002592}
2593
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002594// Called when we are missing one or more packets.
2595int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002596 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2597}
2598
kwiberg55b97fe2016-01-28 05:22:45 -08002599uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2600 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2601 "Channel::Demultiplex()");
2602 _audioFrame.CopyFrom(audioFrame);
2603 _audioFrame.id_ = _channelId;
2604 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002605}
2606
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002607void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002608 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002609 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002610 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002611 CodecInst codec;
2612 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002613
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002614 // Never upsample or upmix the capture signal here. This should be done at the
2615 // end of the send chain.
2616 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2617 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2618 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2619 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002620}
2621
kwiberg55b97fe2016-01-28 05:22:45 -08002622uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2623 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2624 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002625
kwiberg55b97fe2016-01-28 05:22:45 -08002626 if (_audioFrame.samples_per_channel_ == 0) {
2627 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2628 "Channel::PrepareEncodeAndSend() invalid audio frame");
2629 return 0xFFFFFFFF;
2630 }
2631
2632 if (channel_state_.Get().input_file_playing) {
2633 MixOrReplaceAudioWithFile(mixingFrequency);
2634 }
2635
solenberg1c2af8e2016-03-24 10:36:00 -07002636 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2637 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002638
kwiberg55b97fe2016-01-28 05:22:45 -08002639 if (_includeAudioLevelIndication) {
2640 size_t length =
2641 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002642 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002643 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002644 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002645 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002646 rms_level_.Analyze(
2647 rtc::ArrayView<const int16_t>(_audioFrame.data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002648 }
kwiberg55b97fe2016-01-28 05:22:45 -08002649 }
solenberg1c2af8e2016-03-24 10:36:00 -07002650 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002651
kwiberg55b97fe2016-01-28 05:22:45 -08002652 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002653}
2654
kwiberg55b97fe2016-01-28 05:22:45 -08002655uint32_t Channel::EncodeAndSend() {
2656 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2657 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002658
kwiberg55b97fe2016-01-28 05:22:45 -08002659 assert(_audioFrame.num_channels_ <= 2);
2660 if (_audioFrame.samples_per_channel_ == 0) {
2661 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2662 "Channel::EncodeAndSend() invalid audio frame");
2663 return 0xFFFFFFFF;
2664 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002665
kwiberg55b97fe2016-01-28 05:22:45 -08002666 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002667
kwiberg55b97fe2016-01-28 05:22:45 -08002668 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002669
kwiberg55b97fe2016-01-28 05:22:45 -08002670 // The ACM resamples internally.
2671 _audioFrame.timestamp_ = _timeStamp;
2672 // This call will trigger AudioPacketizationCallback::SendData if encoding
2673 // is done and payload is ready for packetization and transmission.
2674 // Otherwise, it will return without invoking the callback.
2675 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2676 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2677 "Channel::EncodeAndSend() ACM encoding failed");
2678 return 0xFFFFFFFF;
2679 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002680
kwiberg55b97fe2016-01-28 05:22:45 -08002681 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2682 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002683}
2684
solenberg7602aab2016-11-14 11:30:07 -08002685void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2686 RTC_DCHECK(!channel.channel() ||
2687 channel.channel()->ChannelId() != _channelId);
2688 rtc::CritScope lock(&assoc_send_channel_lock_);
2689 associate_send_channel_ = channel;
2690}
2691
Minyue2013aec2015-05-13 14:14:42 +02002692void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002693 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002694 Channel* channel = associate_send_channel_.channel();
2695 if (channel && channel->ChannelId() == channel_id) {
2696 // If this channel is associated with a send channel of the specified
2697 // Channel ID, disassociate with it.
2698 ChannelOwner ref(NULL);
2699 associate_send_channel_ = ref;
2700 }
2701}
2702
ivoc14d5dbe2016-07-04 07:06:55 -07002703void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2704 event_log_proxy_->SetEventLog(event_log);
2705}
2706
michaelt9332b7d2016-11-30 07:51:13 -08002707void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2708 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2709}
2710
nisse284542b2017-01-10 08:58:32 -08002711void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08002712 size_t overhead_per_packet =
2713 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08002714 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2715 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08002716 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08002717 }
2718 });
2719}
2720
2721void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002722 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002723 transport_overhead_per_packet_ = transport_overhead_per_packet;
2724 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002725}
2726
hbos3fd31fe2017-02-28 05:43:16 -08002727// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08002728void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002729 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002730 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2731 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002732}
2733
kwiberg55b97fe2016-01-28 05:22:45 -08002734int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2735 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002736}
2737
wu@webrtc.org24301a62013-12-13 19:17:43 +00002738void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2739 audio_coding_->GetDecodingCallStatistics(stats);
2740}
2741
solenberg358057b2015-11-27 10:46:42 -08002742uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002743 rtc::CritScope lock(&video_sync_lock_);
2744 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002745}
2746
kwiberg55b97fe2016-01-28 05:22:45 -08002747int Channel::SetMinimumPlayoutDelay(int delayMs) {
2748 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2749 "Channel::SetMinimumPlayoutDelay()");
2750 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2751 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2752 _engineStatisticsPtr->SetLastError(
2753 VE_INVALID_ARGUMENT, kTraceError,
2754 "SetMinimumPlayoutDelay() invalid min delay");
2755 return -1;
2756 }
2757 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2758 _engineStatisticsPtr->SetLastError(
2759 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2760 "SetMinimumPlayoutDelay() failed to set min playout delay");
2761 return -1;
2762 }
2763 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002764}
2765
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002766int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002767 uint32_t playout_timestamp_rtp = 0;
2768 {
tommi31fc21f2016-01-21 10:37:37 -08002769 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002770 playout_timestamp_rtp = playout_timestamp_rtp_;
2771 }
kwiberg55b97fe2016-01-28 05:22:45 -08002772 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002773 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002774 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002775 "GetPlayoutTimestamp() failed to retrieve timestamp");
2776 return -1;
2777 }
deadbeef74375882015-08-13 12:09:10 -07002778 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002779 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002780}
2781
kwiberg55b97fe2016-01-28 05:22:45 -08002782int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2783 RtpReceiver** rtp_receiver) const {
2784 *rtpRtcpModule = _rtpRtcpModule.get();
2785 *rtp_receiver = rtp_receiver_.get();
2786 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002787}
2788
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002789// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2790// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002791int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002792 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002793 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002794
kwiberg55b97fe2016-01-28 05:22:45 -08002795 {
2796 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002797
kwiberg5a25d952016-08-17 07:31:12 -07002798 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002799 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2800 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2801 " doesnt exist");
2802 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002803 }
2804
kwiberg4ec01d92016-08-22 08:43:54 -07002805 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002806 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002807 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2808 "Channel::MixOrReplaceAudioWithFile() file mixing "
2809 "failed");
2810 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002811 }
kwiberg55b97fe2016-01-28 05:22:45 -08002812 if (fileSamples == 0) {
2813 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2814 "Channel::MixOrReplaceAudioWithFile() file is ended");
2815 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002816 }
kwiberg55b97fe2016-01-28 05:22:45 -08002817 }
2818
2819 assert(_audioFrame.samples_per_channel_ == fileSamples);
2820
2821 if (_mixFileWithMicrophone) {
2822 // Currently file stream is always mono.
2823 // TODO(xians): Change the code when FilePlayer supports real stereo.
2824 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
2825 1, fileSamples);
2826 } else {
2827 // Replace ACM audio with file.
2828 // Currently file stream is always mono.
2829 // TODO(xians): Change the code when FilePlayer supports real stereo.
2830 _audioFrame.UpdateFrame(
2831 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2832 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2833 }
2834 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002835}
2836
kwiberg55b97fe2016-01-28 05:22:45 -08002837int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2838 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002839
kwibergb7f89d62016-02-17 10:04:18 -08002840 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002841 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002842
kwiberg55b97fe2016-01-28 05:22:45 -08002843 {
2844 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002845
kwiberg5a25d952016-08-17 07:31:12 -07002846 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002847 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2848 "Channel::MixAudioWithFile() file mixing failed");
2849 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002850 }
2851
kwiberg55b97fe2016-01-28 05:22:45 -08002852 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002853 if (output_file_player_->Get10msAudioFromFile(
2854 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002855 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2856 "Channel::MixAudioWithFile() file mixing failed");
2857 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002858 }
kwiberg55b97fe2016-01-28 05:22:45 -08002859 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002860
kwiberg55b97fe2016-01-28 05:22:45 -08002861 if (audioFrame.samples_per_channel_ == fileSamples) {
2862 // Currently file stream is always mono.
2863 // TODO(xians): Change the code when FilePlayer supports real stereo.
2864 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
2865 fileSamples);
2866 } else {
2867 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2868 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2869 ") != "
2870 "fileSamples(%" PRIuS ")",
2871 audioFrame.samples_per_channel_, fileSamples);
2872 return -1;
2873 }
2874
2875 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002876}
2877
deadbeef74375882015-08-13 12:09:10 -07002878void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002879 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002880
henrik.lundin96bd5022016-04-06 04:13:56 -07002881 if (!jitter_buffer_playout_timestamp_) {
2882 // This can happen if this channel has not received any RTP packets. In
2883 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07002884 return;
2885 }
2886
2887 uint16_t delay_ms = 0;
2888 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002889 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002890 "Channel::UpdatePlayoutTimestamp() failed to read playout"
2891 " delay from the ADM");
2892 _engineStatisticsPtr->SetLastError(
2893 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
2894 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
2895 return;
2896 }
2897
henrik.lundin96bd5022016-04-06 04:13:56 -07002898 RTC_DCHECK(jitter_buffer_playout_timestamp_);
2899 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07002900
2901 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07002902 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07002903
kwiberg55b97fe2016-01-28 05:22:45 -08002904 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002905 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07002906 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07002907
2908 {
tommi31fc21f2016-01-21 10:37:37 -08002909 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08002910 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002911 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07002912 }
2913 playout_delay_ms_ = delay_ms;
2914 }
2915}
2916
kwiberg55b97fe2016-01-28 05:22:45 -08002917void Channel::RegisterReceiveCodecsToRTPModule() {
2918 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2919 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002920
kwiberg55b97fe2016-01-28 05:22:45 -08002921 CodecInst codec;
2922 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002923
kwiberg55b97fe2016-01-28 05:22:45 -08002924 for (int idx = 0; idx < nSupportedCodecs; idx++) {
2925 // Open up the RTP/RTCP receiver for all supported codecs
2926 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08002927 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08002928 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2929 "Channel::RegisterReceiveCodecsToRTPModule() unable"
2930 " to register %s (%d/%d/%" PRIuS
2931 "/%d) to RTP/RTCP "
2932 "receiver",
2933 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2934 codec.rate);
2935 } else {
2936 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2937 "Channel::RegisterReceiveCodecsToRTPModule() %s "
2938 "(%d/%d/%" PRIuS
2939 "/%d) has been added to the RTP/RTCP "
2940 "receiver",
2941 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2942 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00002943 }
kwiberg55b97fe2016-01-28 05:22:45 -08002944 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002945}
2946
kwiberg55b97fe2016-01-28 05:22:45 -08002947int Channel::SetSendRtpHeaderExtension(bool enable,
2948 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002949 unsigned char id) {
2950 int error = 0;
2951 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
2952 if (enable) {
2953 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
2954 }
2955 return error;
2956}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002957
ossue280cde2016-10-12 11:04:10 -07002958int Channel::GetRtpTimestampRateHz() const {
2959 const auto format = audio_coding_->ReceiveFormat();
2960 // Default to the playout frequency if we've not gotten any packets yet.
2961 // TODO(ossu): Zero clockrate can only happen if we've added an external
2962 // decoder for a format we don't support internally. Remove once that way of
2963 // adding decoders is gone!
2964 return (format && format->clockrate_hz != 0)
2965 ? format->clockrate_hz
2966 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00002967}
2968
Minyue2013aec2015-05-13 14:14:42 +02002969int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07002970 RtcpMode method = _rtpRtcpModule->RTCP();
2971 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002972 return 0;
2973 }
2974 std::vector<RTCPReportBlock> report_blocks;
2975 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02002976
2977 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002978 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02002979 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08002980 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002981 Channel* channel = associate_send_channel_.channel();
2982 // Tries to get RTT from an associated channel. This is important for
2983 // receive-only channels.
2984 if (channel) {
2985 // To prevent infinite recursion and deadlock, calling GetRTT of
2986 // associate channel should always use "false" for argument:
2987 // |allow_associate_channel|.
2988 rtt = channel->GetRTT(false);
2989 }
2990 }
2991 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002992 }
2993
2994 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2995 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
2996 for (; it != report_blocks.end(); ++it) {
2997 if (it->remoteSSRC == remoteSSRC)
2998 break;
2999 }
3000 if (it == report_blocks.end()) {
3001 // We have not received packets with SSRC matching the report blocks.
3002 // To calculate RTT we try with the SSRC of the first report block.
3003 // This is very important for send-only channels where we don't know
3004 // the SSRC of the other end.
3005 remoteSSRC = report_blocks[0].remoteSSRC;
3006 }
Minyue2013aec2015-05-13 14:14:42 +02003007
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003008 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003009 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003010 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003011 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3012 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003013 return 0;
3014 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003015 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003016}
3017
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003018} // namespace voe
3019} // namespace webrtc