blob: 70550c0b2d89cce1df55bbff63642350792a4f05 [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;
670 float left_pan = 1.0f;
671 float right_pan = 1.0f;
672 {
673 rtc::CritScope cs(&volume_settings_critsect_);
674 output_gain = _outputGain;
675 left_pan = _panLeft;
676 right_pan = _panRight;
677 }
678
679 // Output volume scaling
680 if (output_gain < 0.99f || output_gain > 1.01f) {
681 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
682 }
683
684 // Scale left and/or right channel(s) if stereo and master balance is
685 // active
686
687 if (left_pan != 1.0f || right_pan != 1.0f) {
688 if (audioFrame->num_channels_ == 1) {
689 // Emulate stereo mode since panning is active.
690 // The mono signal is copied to both left and right channels here.
691 AudioFrameOperations::MonoToStereo(audioFrame);
692 }
693 // For true stereo mode (when we are receiving a stereo signal), no
694 // action is needed.
695
696 // Do the panning operation (the audio frame contains stereo at this
697 // stage)
698 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
699 }
700
701 // Mix decoded PCM output with file if file mixing is enabled
702 if (state.output_file_playing) {
703 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700704 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800705 }
706
kwiberg55b97fe2016-01-28 05:22:45 -0800707 // Record playout if enabled
708 {
709 rtc::CritScope cs(&_fileCritSect);
710
kwiberg5a25d952016-08-17 07:31:12 -0700711 if (_outputFileRecording && output_file_recorder_) {
712 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800713 }
714 }
715
716 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700717 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800718 _outputAudioLevel.ComputeLevel(*audioFrame);
719
720 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
721 // The first frame with a valid rtp timestamp.
722 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
723 }
724
725 if (capture_start_rtp_time_stamp_ >= 0) {
726 // audioFrame.timestamp_ should be valid from now on.
727
728 // Compute elapsed time.
729 int64_t unwrap_timestamp =
730 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
731 audioFrame->elapsed_time_ms_ =
732 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700733 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800734
niklase@google.com470e71d2011-07-07 08:21:25 +0000735 {
kwiberg55b97fe2016-01-28 05:22:45 -0800736 rtc::CritScope lock(&ts_stats_lock_);
737 // Compute ntp time.
738 audioFrame->ntp_time_ms_ =
739 ntp_estimator_.Estimate(audioFrame->timestamp_);
740 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
741 if (audioFrame->ntp_time_ms_ > 0) {
742 // Compute |capture_start_ntp_time_ms_| so that
743 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
744 capture_start_ntp_time_ms_ =
745 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000746 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000747 }
kwiberg55b97fe2016-01-28 05:22:45 -0800748 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000749
henrik.lundin42dda502016-05-18 05:36:01 -0700750 return muted ? MixerParticipant::AudioFrameInfo::kMuted
751 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000752}
753
aleloi6c278492016-10-20 14:24:39 -0700754AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
755 int sample_rate_hz,
756 AudioFrame* audio_frame) {
757 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700758
aleloi6c278492016-10-20 14:24:39 -0700759 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700760
761 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
762 FrameInfo new_audio_frame_info = FrameInfo::kError;
763 switch (frame_info) {
764 case MixerParticipant::AudioFrameInfo::kNormal:
765 new_audio_frame_info = FrameInfo::kNormal;
766 break;
767 case MixerParticipant::AudioFrameInfo::kMuted:
768 new_audio_frame_info = FrameInfo::kMuted;
769 break;
770 case MixerParticipant::AudioFrameInfo::kError:
771 new_audio_frame_info = FrameInfo::kError;
772 break;
773 }
aleloi6c278492016-10-20 14:24:39 -0700774 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700775}
776
kwiberg55b97fe2016-01-28 05:22:45 -0800777int32_t Channel::NeededFrequency(int32_t id) const {
778 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
779 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000780
kwiberg55b97fe2016-01-28 05:22:45 -0800781 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000782
kwiberg55b97fe2016-01-28 05:22:45 -0800783 // Determine highest needed receive frequency
784 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000785
kwiberg55b97fe2016-01-28 05:22:45 -0800786 // Return the bigger of playout and receive frequency in the ACM.
787 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
788 highestNeeded = audio_coding_->PlayoutFrequency();
789 } else {
790 highestNeeded = receiveFrequency;
791 }
792
793 // Special case, if we're playing a file on the playout side
794 // we take that frequency into consideration as well
795 // This is not needed on sending side, since the codec will
796 // limit the spectrum anyway.
797 if (channel_state_.Get().output_file_playing) {
798 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700799 if (output_file_player_) {
800 if (output_file_player_->Frequency() > highestNeeded) {
801 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800802 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000803 }
kwiberg55b97fe2016-01-28 05:22:45 -0800804 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000805
kwiberg55b97fe2016-01-28 05:22:45 -0800806 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000807}
808
ossu5f7cfa52016-05-30 08:11:28 -0700809int32_t Channel::CreateChannel(
810 Channel*& channel,
811 int32_t channelId,
812 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700813 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800814 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
815 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
816 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000817
solenberg88499ec2016-09-07 07:34:41 -0700818 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800819 if (channel == NULL) {
820 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
821 "Channel::CreateChannel() unable to allocate memory for"
822 " channel");
823 return -1;
824 }
825 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000826}
827
kwiberg55b97fe2016-01-28 05:22:45 -0800828void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
829 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
830 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
831 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000832
kwiberg55b97fe2016-01-28 05:22:45 -0800833 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000834}
835
kwiberg55b97fe2016-01-28 05:22:45 -0800836void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
837 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
838 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
839 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000840
kwiberg55b97fe2016-01-28 05:22:45 -0800841 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000842}
843
kwiberg55b97fe2016-01-28 05:22:45 -0800844void Channel::PlayFileEnded(int32_t id) {
845 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
846 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000847
kwiberg55b97fe2016-01-28 05:22:45 -0800848 if (id == _inputFilePlayerId) {
849 channel_state_.SetInputFilePlaying(false);
850 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
851 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000852 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800853 } else if (id == _outputFilePlayerId) {
854 channel_state_.SetOutputFilePlaying(false);
855 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
856 "Channel::PlayFileEnded() => output file player module is"
857 " shutdown");
858 }
859}
860
861void Channel::RecordFileEnded(int32_t id) {
862 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
863 "Channel::RecordFileEnded(id=%d)", id);
864
865 assert(id == _outputFileRecorderId);
866
867 rtc::CritScope cs(&_fileCritSect);
868
869 _outputFileRecording = false;
870 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
871 "Channel::RecordFileEnded() => output file recorder module is"
872 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000873}
874
pbos@webrtc.org92135212013-05-14 08:31:39 +0000875Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000876 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700877 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800878 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100879 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700880 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800881 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100882 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800883 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100884 rtp_receive_statistics_(
885 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
886 rtp_receiver_(
887 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100888 this,
889 this,
890 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700891 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100892 _outputAudioLevel(),
893 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100894 // Avoid conflict with other channels by adding 1024 - 1026,
895 // won't use as much as 1024 channels.
896 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
897 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
898 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
899 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100900 _timeStamp(0), // This is just an offset, RTP module will add it's own
901 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100902 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100903 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100904 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100905 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100906 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
907 capture_start_rtp_time_stamp_(-1),
908 capture_start_ntp_time_ms_(-1),
909 _engineStatisticsPtr(NULL),
910 _outputMixerPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100911 _moduleProcessThreadPtr(NULL),
912 _audioDeviceModulePtr(NULL),
913 _voiceEngineObserverPtr(NULL),
914 _callbackCritSectPtr(NULL),
915 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100916 _sendFrameType(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100917 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700918 input_mute_(false),
919 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100920 _panLeft(1.0f),
921 _panRight(1.0f),
922 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100923 _lastLocalTimeStamp(0),
924 _lastPayloadType(0),
925 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800926 transport_overhead_per_packet_(0),
927 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100928 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100929 restored_packet_in_use_(false),
930 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100931 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700932 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800933 feedback_observer_proxy_(new TransportFeedbackProxy()),
934 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700935 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200936 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
937 kMaxRetransmissionWindowMs)),
michaelt566d8202017-01-12 10:17:38 -0800938 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800939 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
940 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700941 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800942 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700943 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800944 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200945
kwiberg55b97fe2016-01-28 05:22:45 -0800946 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000947
kwiberg55b97fe2016-01-28 05:22:45 -0800948 RtpRtcp::Configuration configuration;
949 configuration.audio = true;
950 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800951 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800952 configuration.receive_statistics = rtp_receive_statistics_.get();
953 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800954 if (pacing_enabled_) {
955 configuration.paced_sender = rtp_packet_sender_proxy_.get();
956 configuration.transport_sequence_number_allocator =
957 seq_num_allocator_proxy_.get();
958 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
959 }
ivoc14d5dbe2016-07-04 07:06:55 -0700960 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800961 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200962 configuration.retransmission_rate_limiter =
963 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000964
kwiberg55b97fe2016-01-28 05:22:45 -0800965 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100966 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000967}
968
kwiberg55b97fe2016-01-28 05:22:45 -0800969Channel::~Channel() {
970 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
971 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
972 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000973
kwiberg55b97fe2016-01-28 05:22:45 -0800974 StopSend();
975 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000976
kwiberg55b97fe2016-01-28 05:22:45 -0800977 {
978 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700979 if (input_file_player_) {
980 input_file_player_->RegisterModuleFileCallback(NULL);
981 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000982 }
kwiberg5a25d952016-08-17 07:31:12 -0700983 if (output_file_player_) {
984 output_file_player_->RegisterModuleFileCallback(NULL);
985 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800986 }
kwiberg5a25d952016-08-17 07:31:12 -0700987 if (output_file_recorder_) {
988 output_file_recorder_->RegisterModuleFileCallback(NULL);
989 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800990 }
991 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000992
kwiberg55b97fe2016-01-28 05:22:45 -0800993 // The order to safely shutdown modules in a channel is:
994 // 1. De-register callbacks in modules
995 // 2. De-register modules in process thread
996 // 3. Destroy modules
997 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
998 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
999 "~Channel() failed to de-register transport callback"
1000 " (Audio coding module)");
1001 }
1002 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
1003 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1004 "~Channel() failed to de-register VAD callback"
1005 " (Audio coding module)");
1006 }
1007 // De-register modules in process thread
1008 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +00001009
kwiberg55b97fe2016-01-28 05:22:45 -08001010 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +00001011}
1012
kwiberg55b97fe2016-01-28 05:22:45 -08001013int32_t Channel::Init() {
1014 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1015 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001016
kwiberg55b97fe2016-01-28 05:22:45 -08001017 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001018
kwiberg55b97fe2016-01-28 05:22:45 -08001019 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +00001020
kwiberg55b97fe2016-01-28 05:22:45 -08001021 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
1022 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1023 "Channel::Init() must call SetEngineInformation() first");
1024 return -1;
1025 }
1026
1027 // --- Add modules to process thread (for periodic schedulation)
1028
tommidea489f2017-03-03 03:20:24 -08001029 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
kwiberg55b97fe2016-01-28 05:22:45 -08001030
1031 // --- ACM initialization
1032
1033 if (audio_coding_->InitializeReceiver() == -1) {
1034 _engineStatisticsPtr->SetLastError(
1035 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1036 "Channel::Init() unable to initialize the ACM - 1");
1037 return -1;
1038 }
1039
1040 // --- RTP/RTCP module initialization
1041
1042 // Ensure that RTCP is enabled by default for the created channel.
1043 // Note that, the module will keep generating RTCP until it is explicitly
1044 // disabled by the user.
1045 // After StopListen (when no sockets exists), RTCP packets will no longer
1046 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001047 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001048 // RTCP is enabled by default.
1049 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1050 // --- Register all permanent callbacks
1051 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1052 (audio_coding_->RegisterVADCallback(this) == -1);
1053
1054 if (fail) {
1055 _engineStatisticsPtr->SetLastError(
1056 VE_CANNOT_INIT_CHANNEL, kTraceError,
1057 "Channel::Init() callbacks not registered");
1058 return -1;
1059 }
1060
1061 // --- Register all supported codecs to the receiving side of the
1062 // RTP/RTCP module
1063
1064 CodecInst codec;
1065 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1066
1067 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1068 // Open up the RTP/RTCP receiver for all supported codecs
1069 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08001070 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001071 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1072 "Channel::Init() unable to register %s "
1073 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1074 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1075 codec.rate);
1076 } else {
1077 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1078 "Channel::Init() %s (%d/%d/%" PRIuS
1079 "/%d) has been "
1080 "added to the RTP/RTCP receiver",
1081 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1082 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001083 }
1084
kwiberg55b97fe2016-01-28 05:22:45 -08001085 // Ensure that PCMU is used as default codec on the sending side
1086 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1087 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001088 }
1089
kwiberg55b97fe2016-01-28 05:22:45 -08001090 // Register default PT for outband 'telephone-event'
1091 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001092 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001093 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1094 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001095 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1096 "Channel::Init() failed to register outband "
1097 "'telephone-event' (%d/%d) correctly",
1098 codec.pltype, codec.plfreq);
1099 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001100 }
1101
kwiberg55b97fe2016-01-28 05:22:45 -08001102 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001103 if (!codec_manager_.RegisterEncoder(codec) ||
1104 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001105 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1106 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001107 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001108 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1109 "Channel::Init() failed to register CN (%d/%d) "
1110 "correctly - 1",
1111 codec.pltype, codec.plfreq);
1112 }
1113 }
kwiberg55b97fe2016-01-28 05:22:45 -08001114 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001115
kwiberg55b97fe2016-01-28 05:22:45 -08001116 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001117}
1118
kwiberg55b97fe2016-01-28 05:22:45 -08001119int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1120 OutputMixer& outputMixer,
kwiberg55b97fe2016-01-28 05:22:45 -08001121 ProcessThread& moduleProcessThread,
1122 AudioDeviceModule& audioDeviceModule,
1123 VoiceEngineObserver* voiceEngineObserver,
1124 rtc::CriticalSection* callbackCritSect) {
1125 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1126 "Channel::SetEngineInformation()");
1127 _engineStatisticsPtr = &engineStatistics;
1128 _outputMixerPtr = &outputMixer;
kwiberg55b97fe2016-01-28 05:22:45 -08001129 _moduleProcessThreadPtr = &moduleProcessThread;
1130 _audioDeviceModulePtr = &audioDeviceModule;
1131 _voiceEngineObserverPtr = voiceEngineObserver;
1132 _callbackCritSectPtr = callbackCritSect;
1133 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001134}
1135
kwiberg55b97fe2016-01-28 05:22:45 -08001136int32_t Channel::UpdateLocalTimeStamp() {
1137 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1138 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001139}
1140
kwibergb7f89d62016-02-17 10:04:18 -08001141void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001142 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001143 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001144}
1145
ossu29b1a8d2016-06-13 07:34:51 -07001146const rtc::scoped_refptr<AudioDecoderFactory>&
1147Channel::GetAudioDecoderFactory() const {
1148 return decoder_factory_;
1149}
1150
kwiberg55b97fe2016-01-28 05:22:45 -08001151int32_t Channel::StartPlayout() {
1152 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1153 "Channel::StartPlayout()");
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 // Add participant as candidates for mixing.
1159 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1160 _engineStatisticsPtr->SetLastError(
1161 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1162 "StartPlayout() failed to add participant to mixer");
1163 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001164 }
1165
1166 channel_state_.SetPlaying(true);
1167 if (RegisterFilePlayingToMixer() != 0)
1168 return -1;
1169
1170 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001171}
1172
kwiberg55b97fe2016-01-28 05:22:45 -08001173int32_t Channel::StopPlayout() {
1174 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1175 "Channel::StopPlayout()");
1176 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001177 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001178 }
1179
solenberge374e012017-02-14 04:55:00 -08001180 // Remove participant as candidates for mixing
1181 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1182 _engineStatisticsPtr->SetLastError(
1183 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1184 "StopPlayout() failed to remove participant from mixer");
1185 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001186 }
1187
1188 channel_state_.SetPlaying(false);
1189 _outputAudioLevel.Clear();
1190
1191 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001192}
1193
kwiberg55b97fe2016-01-28 05:22:45 -08001194int32_t Channel::StartSend() {
1195 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1196 "Channel::StartSend()");
kwiberg55b97fe2016-01-28 05:22:45 -08001197 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001198 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001199 }
1200 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001201
solenberg08b19df2017-02-15 00:42:31 -08001202 // Resume the previous sequence number which was reset by StopSend(). This
1203 // needs to be done before |sending| is set to true on the RTP/RTCP module.
1204 if (send_sequence_number_) {
1205 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
1206 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001207 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001208 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1209 _engineStatisticsPtr->SetLastError(
1210 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1211 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001212 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001213 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001214 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001215 return -1;
1216 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001217
kwiberg55b97fe2016-01-28 05:22:45 -08001218 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001219}
1220
kwiberg55b97fe2016-01-28 05:22:45 -08001221int32_t Channel::StopSend() {
1222 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1223 "Channel::StopSend()");
1224 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001225 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001226 }
1227 channel_state_.SetSending(false);
1228
1229 // Store the sequence number to be able to pick up the same sequence for
1230 // the next StartSend(). This is needed for restarting device, otherwise
1231 // it might cause libSRTP to complain about packets being replayed.
1232 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1233 // CL is landed. See issue
1234 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1235 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1236
1237 // Reset sending SSRC and sequence number and triggers direct transmission
1238 // of RTCP BYE
1239 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1240 _engineStatisticsPtr->SetLastError(
1241 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1242 "StartSend() RTP/RTCP failed to stop sending");
1243 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001244 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001245
1246 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001247}
1248
kwiberg55b97fe2016-01-28 05:22:45 -08001249int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1250 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1251 "Channel::RegisterVoiceEngineObserver()");
1252 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001253
kwiberg55b97fe2016-01-28 05:22:45 -08001254 if (_voiceEngineObserverPtr) {
1255 _engineStatisticsPtr->SetLastError(
1256 VE_INVALID_OPERATION, kTraceError,
1257 "RegisterVoiceEngineObserver() observer already enabled");
1258 return -1;
1259 }
1260 _voiceEngineObserverPtr = &observer;
1261 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001262}
1263
kwiberg55b97fe2016-01-28 05:22:45 -08001264int32_t Channel::DeRegisterVoiceEngineObserver() {
1265 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1266 "Channel::DeRegisterVoiceEngineObserver()");
1267 rtc::CritScope cs(&_callbackCritSect);
1268
1269 if (!_voiceEngineObserverPtr) {
1270 _engineStatisticsPtr->SetLastError(
1271 VE_INVALID_OPERATION, kTraceWarning,
1272 "DeRegisterVoiceEngineObserver() observer already disabled");
1273 return 0;
1274 }
1275 _voiceEngineObserverPtr = NULL;
1276 return 0;
1277}
1278
1279int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001280 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001281 if (send_codec) {
1282 codec = *send_codec;
1283 return 0;
1284 }
1285 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001286}
1287
kwiberg55b97fe2016-01-28 05:22:45 -08001288int32_t Channel::GetRecCodec(CodecInst& codec) {
1289 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001290}
1291
kwiberg55b97fe2016-01-28 05:22:45 -08001292int32_t Channel::SetSendCodec(const CodecInst& codec) {
1293 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1294 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001295
kwibergc8d071e2016-04-06 12:22:38 -07001296 if (!codec_manager_.RegisterEncoder(codec) ||
1297 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001298 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1299 "SetSendCodec() failed to register codec to ACM");
1300 return -1;
1301 }
1302
1303 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1304 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1305 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1306 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1307 "SetSendCodec() failed to register codec to"
1308 " RTP/RTCP module");
1309 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001310 }
kwiberg55b97fe2016-01-28 05:22:45 -08001311 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001312
kwiberg55b97fe2016-01-28 05:22:45 -08001313 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001314}
1315
minyue78b4d562016-11-30 04:47:39 -08001316void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001317 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1318 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001319 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001320 if (*encoder) {
1321 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001322 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001323 }
1324 });
michaelt566d8202017-01-12 10:17:38 -08001325 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001326}
1327
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001328void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001329 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1330 if (*encoder)
1331 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1332 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001333}
1334
kwiberg55b97fe2016-01-28 05:22:45 -08001335int32_t Channel::SetVADStatus(bool enableVAD,
1336 ACMVADMode mode,
1337 bool disableDTX) {
1338 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1339 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001340 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1341 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1342 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001343 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1344 kTraceError,
1345 "SetVADStatus() failed to set VAD");
1346 return -1;
1347 }
1348 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001349}
1350
kwiberg55b97fe2016-01-28 05:22:45 -08001351int32_t Channel::GetVADStatus(bool& enabledVAD,
1352 ACMVADMode& mode,
1353 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001354 const auto* params = codec_manager_.GetStackParams();
1355 enabledVAD = params->use_cng;
1356 mode = params->vad_mode;
1357 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001358 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001359}
1360
kwiberg55b97fe2016-01-28 05:22:45 -08001361int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001362 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1363}
1364
1365int32_t Channel::SetRecPayloadType(int payload_type,
1366 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001367 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1368 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001369
kwiberg55b97fe2016-01-28 05:22:45 -08001370 if (channel_state_.Get().playing) {
1371 _engineStatisticsPtr->SetLastError(
1372 VE_ALREADY_PLAYING, kTraceError,
1373 "SetRecPayloadType() unable to set PT while playing");
1374 return -1;
1375 }
kwiberg55b97fe2016-01-28 05:22:45 -08001376
kwiberg09f090c2017-03-01 01:57:11 -08001377 const CodecInst codec = SdpToCodecInst(payload_type, format);
kwibergd32bf752017-01-19 07:03:59 -08001378
1379 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001380 // De-register the selected codec (RTP/RTCP module and ACM)
1381
1382 int8_t pltype(-1);
1383 CodecInst rxCodec = codec;
1384
1385 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001386 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001387 rxCodec.pltype = pltype;
1388
1389 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1390 _engineStatisticsPtr->SetLastError(
1391 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1392 "SetRecPayloadType() RTP/RTCP-module deregistration "
1393 "failed");
1394 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001395 }
kwiberg55b97fe2016-01-28 05:22:45 -08001396 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1397 _engineStatisticsPtr->SetLastError(
1398 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1399 "SetRecPayloadType() ACM deregistration failed - 1");
1400 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001401 }
kwiberg55b97fe2016-01-28 05:22:45 -08001402 return 0;
1403 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001404
magjed56124bd2016-11-24 09:34:46 -08001405 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001406 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001407 // TODO(kwiberg): Retrying is probably not necessary, since
1408 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001409 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001410 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001411 _engineStatisticsPtr->SetLastError(
1412 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1413 "SetRecPayloadType() RTP/RTCP-module registration failed");
1414 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001415 }
kwiberg55b97fe2016-01-28 05:22:45 -08001416 }
kwibergd32bf752017-01-19 07:03:59 -08001417 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1418 audio_coding_->UnregisterReceiveCodec(payload_type);
1419 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001420 _engineStatisticsPtr->SetLastError(
1421 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1422 "SetRecPayloadType() ACM registration failed - 1");
1423 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001424 }
kwiberg55b97fe2016-01-28 05:22:45 -08001425 }
1426 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001427}
1428
kwiberg55b97fe2016-01-28 05:22:45 -08001429int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1430 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001431 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001432 _engineStatisticsPtr->SetLastError(
1433 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1434 "GetRecPayloadType() failed to retrieve RX payload type");
1435 return -1;
1436 }
1437 codec.pltype = payloadType;
1438 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001439}
1440
kwiberg55b97fe2016-01-28 05:22:45 -08001441int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1442 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1443 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001444
kwiberg55b97fe2016-01-28 05:22:45 -08001445 CodecInst codec;
1446 int32_t samplingFreqHz(-1);
1447 const size_t kMono = 1;
1448 if (frequency == kFreq32000Hz)
1449 samplingFreqHz = 32000;
1450 else if (frequency == kFreq16000Hz)
1451 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001452
kwiberg55b97fe2016-01-28 05:22:45 -08001453 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1454 _engineStatisticsPtr->SetLastError(
1455 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1456 "SetSendCNPayloadType() failed to retrieve default CN codec "
1457 "settings");
1458 return -1;
1459 }
1460
1461 // Modify the payload type (must be set to dynamic range)
1462 codec.pltype = type;
1463
kwibergc8d071e2016-04-06 12:22:38 -07001464 if (!codec_manager_.RegisterEncoder(codec) ||
1465 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001466 _engineStatisticsPtr->SetLastError(
1467 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1468 "SetSendCNPayloadType() failed to register CN to ACM");
1469 return -1;
1470 }
1471
1472 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1473 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1474 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1475 _engineStatisticsPtr->SetLastError(
1476 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1477 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1478 "module");
1479 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001480 }
kwiberg55b97fe2016-01-28 05:22:45 -08001481 }
1482 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001483}
1484
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001485int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001486 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001487 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001488
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001489 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001490 _engineStatisticsPtr->SetLastError(
1491 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001492 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001493 return -1;
1494 }
1495 return 0;
1496}
1497
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001498int Channel::SetOpusDtx(bool enable_dtx) {
1499 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1500 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001501 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001502 : audio_coding_->DisableOpusDtx();
1503 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001504 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1505 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001506 return -1;
1507 }
1508 return 0;
1509}
1510
ivoc85228d62016-07-27 04:53:47 -07001511int Channel::GetOpusDtx(bool* enabled) {
1512 int success = -1;
1513 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1514 if (encoder) {
1515 *enabled = encoder->GetDtx();
1516 success = 0;
1517 }
1518 });
1519 return success;
1520}
1521
minyue7e304322016-10-12 05:00:55 -07001522bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1523 bool success = false;
1524 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1525 if (*encoder) {
1526 success = (*encoder)->EnableAudioNetworkAdaptor(
michaeltbf279fc2017-01-13 06:02:29 -08001527 config_string, event_log_proxy_.get(), Clock::GetRealTimeClock());
minyue7e304322016-10-12 05:00:55 -07001528 }
1529 });
1530 return success;
1531}
1532
1533void Channel::DisableAudioNetworkAdaptor() {
1534 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1535 if (*encoder)
1536 (*encoder)->DisableAudioNetworkAdaptor();
1537 });
1538}
1539
1540void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1541 int max_frame_length_ms) {
1542 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1543 if (*encoder) {
1544 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1545 max_frame_length_ms);
1546 }
1547 });
1548}
1549
mflodman3d7db262016-04-29 00:57:13 -07001550int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001551 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001552 "Channel::RegisterExternalTransport()");
1553
kwiberg55b97fe2016-01-28 05:22:45 -08001554 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001555 if (_externalTransport) {
1556 _engineStatisticsPtr->SetLastError(
1557 VE_INVALID_OPERATION, kTraceError,
1558 "RegisterExternalTransport() external transport already enabled");
1559 return -1;
1560 }
1561 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001562 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001563 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001564}
1565
kwiberg55b97fe2016-01-28 05:22:45 -08001566int32_t Channel::DeRegisterExternalTransport() {
1567 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1568 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001569
kwiberg55b97fe2016-01-28 05:22:45 -08001570 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001571 if (_transportPtr) {
1572 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1573 "DeRegisterExternalTransport() all transport is disabled");
1574 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001575 _engineStatisticsPtr->SetLastError(
1576 VE_INVALID_OPERATION, kTraceWarning,
1577 "DeRegisterExternalTransport() external transport already "
1578 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001579 }
1580 _externalTransport = false;
1581 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001582 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001583}
1584
nisse657bab22017-02-21 06:28:10 -08001585// TODO(nisse): Delete this method together with ReceivedRTPPacket.
1586// It's a temporary hack to support both ReceivedRTPPacket and
1587// OnRtpPacket interfaces without too much code duplication.
1588bool Channel::OnRtpPacketWithHeader(const uint8_t* received_packet,
1589 size_t length,
1590 RTPHeader *header) {
1591 // Store playout timestamp for the received RTP packet
1592 UpdatePlayoutTimestamp(false);
1593
1594 header->payload_type_frequency =
1595 rtp_payload_registry_->GetPayloadTypeFrequency(header->payloadType);
1596 if (header->payload_type_frequency < 0)
1597 return false;
1598 bool in_order = IsPacketInOrder(*header);
1599 rtp_receive_statistics_->IncomingPacket(
1600 *header, length, IsPacketRetransmitted(*header, in_order));
1601 rtp_payload_registry_->SetIncomingPayloadType(*header);
1602
1603 return ReceivePacket(received_packet, length, *header, in_order);
1604}
1605
mflodman3d7db262016-04-29 00:57:13 -07001606int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001607 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001608 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001609 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001610 "Channel::ReceivedRTPPacket()");
1611
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001612 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001613 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1614 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1615 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001616 return -1;
1617 }
nisse657bab22017-02-21 06:28:10 -08001618 return OnRtpPacketWithHeader(received_packet, length, &header) ? 0 : -1;
1619}
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001620
nisse657bab22017-02-21 06:28:10 -08001621void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
1622 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
1623 "Channel::ReceivedRTPPacket()");
1624
1625 RTPHeader header;
1626 packet.GetHeader(&header);
1627 OnRtpPacketWithHeader(packet.data(), packet.size(), &header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001628}
1629
1630bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001631 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001632 const RTPHeader& header,
1633 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001634 if (rtp_payload_registry_->IsRtx(header)) {
1635 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001636 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001637 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001638 assert(packet_length >= header.headerLength);
1639 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001640 PayloadUnion payload_specific;
1641 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001642 &payload_specific)) {
1643 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001644 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001645 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1646 payload_specific, in_order);
1647}
1648
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001649bool Channel::HandleRtxPacket(const uint8_t* packet,
1650 size_t packet_length,
1651 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001652 if (!rtp_payload_registry_->IsRtx(header))
1653 return false;
1654
1655 // Remove the RTX header and parse the original RTP header.
1656 if (packet_length < header.headerLength)
1657 return false;
1658 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1659 return false;
1660 if (restored_packet_in_use_) {
1661 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1662 "Multiple RTX headers detected, dropping packet");
1663 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001664 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001665 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001666 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1667 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001668 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1669 "Incoming RTX packet: invalid RTP header");
1670 return false;
1671 }
1672 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001673 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001674 restored_packet_in_use_ = false;
1675 return ret;
1676}
1677
1678bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1679 StreamStatistician* statistician =
1680 rtp_receive_statistics_->GetStatistician(header.ssrc);
1681 if (!statistician)
1682 return false;
1683 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001684}
1685
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001686bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1687 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001688 // Retransmissions are handled separately if RTX is enabled.
1689 if (rtp_payload_registry_->RtxEnabled())
1690 return false;
1691 StreamStatistician* statistician =
1692 rtp_receive_statistics_->GetStatistician(header.ssrc);
1693 if (!statistician)
1694 return false;
1695 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001696 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001697 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001698 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001699}
1700
mflodman3d7db262016-04-29 00:57:13 -07001701int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001702 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001703 "Channel::ReceivedRTCPPacket()");
1704 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001705 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001706
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001707 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001708 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001709 _engineStatisticsPtr->SetLastError(
1710 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1711 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1712 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001713
Minyue2013aec2015-05-13 14:14:42 +02001714 int64_t rtt = GetRTT(true);
1715 if (rtt == 0) {
1716 // Waiting for valid RTT.
1717 return 0;
1718 }
Erik Språng737336d2016-07-29 12:59:36 +02001719
1720 int64_t nack_window_ms = rtt;
1721 if (nack_window_ms < kMinRetransmissionWindowMs) {
1722 nack_window_ms = kMinRetransmissionWindowMs;
1723 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1724 nack_window_ms = kMaxRetransmissionWindowMs;
1725 }
1726 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1727
minyue7e304322016-10-12 05:00:55 -07001728 // Invoke audio encoders OnReceivedRtt().
1729 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1730 if (*encoder)
1731 (*encoder)->OnReceivedRtt(rtt);
1732 });
1733
Minyue2013aec2015-05-13 14:14:42 +02001734 uint32_t ntp_secs = 0;
1735 uint32_t ntp_frac = 0;
1736 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001737 if (0 !=
1738 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1739 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001740 // Waiting for RTCP.
1741 return 0;
1742 }
1743
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001744 {
tommi31fc21f2016-01-21 10:37:37 -08001745 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001746 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001747 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001748 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001749}
1750
niklase@google.com470e71d2011-07-07 08:21:25 +00001751int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001752 bool loop,
1753 FileFormats format,
1754 int startPosition,
1755 float volumeScaling,
1756 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001757 const CodecInst* codecInst) {
1758 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1759 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1760 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1761 "stopPosition=%d)",
1762 fileName, loop, format, volumeScaling, startPosition,
1763 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001764
kwiberg55b97fe2016-01-28 05:22:45 -08001765 if (channel_state_.Get().output_file_playing) {
1766 _engineStatisticsPtr->SetLastError(
1767 VE_ALREADY_PLAYING, kTraceError,
1768 "StartPlayingFileLocally() is already playing");
1769 return -1;
1770 }
1771
1772 {
1773 rtc::CritScope cs(&_fileCritSect);
1774
kwiberg5a25d952016-08-17 07:31:12 -07001775 if (output_file_player_) {
1776 output_file_player_->RegisterModuleFileCallback(NULL);
1777 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001778 }
1779
kwiberg5b356f42016-09-08 04:32:33 -07001780 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001781 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001782
kwiberg5a25d952016-08-17 07:31:12 -07001783 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001784 _engineStatisticsPtr->SetLastError(
1785 VE_INVALID_ARGUMENT, kTraceError,
1786 "StartPlayingFileLocally() filePlayer format is not correct");
1787 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001788 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001789
kwiberg55b97fe2016-01-28 05:22:45 -08001790 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001791
kwiberg5a25d952016-08-17 07:31:12 -07001792 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001793 fileName, loop, startPosition, volumeScaling, notificationTime,
1794 stopPosition, (const CodecInst*)codecInst) != 0) {
1795 _engineStatisticsPtr->SetLastError(
1796 VE_BAD_FILE, kTraceError,
1797 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001798 output_file_player_->StopPlayingFile();
1799 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001800 return -1;
1801 }
kwiberg5a25d952016-08-17 07:31:12 -07001802 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001803 channel_state_.SetOutputFilePlaying(true);
1804 }
1805
1806 if (RegisterFilePlayingToMixer() != 0)
1807 return -1;
1808
1809 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001810}
1811
1812int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001813 FileFormats format,
1814 int startPosition,
1815 float volumeScaling,
1816 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001817 const CodecInst* codecInst) {
1818 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1819 "Channel::StartPlayingFileLocally(format=%d,"
1820 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1821 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001822
kwiberg55b97fe2016-01-28 05:22:45 -08001823 if (stream == NULL) {
1824 _engineStatisticsPtr->SetLastError(
1825 VE_BAD_FILE, kTraceError,
1826 "StartPlayingFileLocally() NULL as input stream");
1827 return -1;
1828 }
1829
1830 if (channel_state_.Get().output_file_playing) {
1831 _engineStatisticsPtr->SetLastError(
1832 VE_ALREADY_PLAYING, kTraceError,
1833 "StartPlayingFileLocally() is already playing");
1834 return -1;
1835 }
1836
1837 {
1838 rtc::CritScope cs(&_fileCritSect);
1839
1840 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001841 if (output_file_player_) {
1842 output_file_player_->RegisterModuleFileCallback(NULL);
1843 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001844 }
1845
kwiberg55b97fe2016-01-28 05:22:45 -08001846 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001847 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001848 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001849
kwiberg5a25d952016-08-17 07:31:12 -07001850 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001851 _engineStatisticsPtr->SetLastError(
1852 VE_INVALID_ARGUMENT, kTraceError,
1853 "StartPlayingFileLocally() filePlayer format isnot correct");
1854 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001855 }
1856
kwiberg55b97fe2016-01-28 05:22:45 -08001857 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001858
kwiberg4ec01d92016-08-22 08:43:54 -07001859 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001860 volumeScaling, notificationTime,
1861 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001862 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1863 "StartPlayingFile() failed to "
1864 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001865 output_file_player_->StopPlayingFile();
1866 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001867 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001868 }
kwiberg5a25d952016-08-17 07:31:12 -07001869 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001870 channel_state_.SetOutputFilePlaying(true);
1871 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001872
kwiberg55b97fe2016-01-28 05:22:45 -08001873 if (RegisterFilePlayingToMixer() != 0)
1874 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001875
kwiberg55b97fe2016-01-28 05:22:45 -08001876 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001877}
1878
kwiberg55b97fe2016-01-28 05:22:45 -08001879int Channel::StopPlayingFileLocally() {
1880 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1881 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001882
kwiberg55b97fe2016-01-28 05:22:45 -08001883 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001884 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001885 }
1886
1887 {
1888 rtc::CritScope cs(&_fileCritSect);
1889
kwiberg5a25d952016-08-17 07:31:12 -07001890 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001891 _engineStatisticsPtr->SetLastError(
1892 VE_STOP_RECORDING_FAILED, kTraceError,
1893 "StopPlayingFile() could not stop playing");
1894 return -1;
1895 }
kwiberg5a25d952016-08-17 07:31:12 -07001896 output_file_player_->RegisterModuleFileCallback(NULL);
1897 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001898 channel_state_.SetOutputFilePlaying(false);
1899 }
1900 // _fileCritSect cannot be taken while calling
1901 // SetAnonymousMixibilityStatus. Refer to comments in
1902 // StartPlayingFileLocally(const char* ...) for more details.
1903 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1904 _engineStatisticsPtr->SetLastError(
1905 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1906 "StopPlayingFile() failed to stop participant from playing as"
1907 "file in the mixer");
1908 return -1;
1909 }
1910
1911 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001912}
1913
kwiberg55b97fe2016-01-28 05:22:45 -08001914int Channel::IsPlayingFileLocally() const {
1915 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001916}
1917
kwiberg55b97fe2016-01-28 05:22:45 -08001918int Channel::RegisterFilePlayingToMixer() {
1919 // Return success for not registering for file playing to mixer if:
1920 // 1. playing file before playout is started on that channel.
1921 // 2. starting playout without file playing on that channel.
1922 if (!channel_state_.Get().playing ||
1923 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001924 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001925 }
1926
1927 // |_fileCritSect| cannot be taken while calling
1928 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1929 // frames can be pulled by the mixer. Since the frames are generated from
1930 // the file, _fileCritSect will be taken. This would result in a deadlock.
1931 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1932 channel_state_.SetOutputFilePlaying(false);
1933 rtc::CritScope cs(&_fileCritSect);
1934 _engineStatisticsPtr->SetLastError(
1935 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1936 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001937 output_file_player_->StopPlayingFile();
1938 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001939 return -1;
1940 }
1941
1942 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001943}
1944
niklase@google.com470e71d2011-07-07 08:21:25 +00001945int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001946 bool loop,
1947 FileFormats format,
1948 int startPosition,
1949 float volumeScaling,
1950 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001951 const CodecInst* codecInst) {
1952 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1953 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1954 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1955 "stopPosition=%d)",
1956 fileName, loop, format, volumeScaling, startPosition,
1957 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001958
kwiberg55b97fe2016-01-28 05:22:45 -08001959 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001960
kwiberg55b97fe2016-01-28 05:22:45 -08001961 if (channel_state_.Get().input_file_playing) {
1962 _engineStatisticsPtr->SetLastError(
1963 VE_ALREADY_PLAYING, kTraceWarning,
1964 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001965 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001966 }
1967
1968 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001969 if (input_file_player_) {
1970 input_file_player_->RegisterModuleFileCallback(NULL);
1971 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001972 }
1973
1974 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001975 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001976 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001977
kwiberg5a25d952016-08-17 07:31:12 -07001978 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001979 _engineStatisticsPtr->SetLastError(
1980 VE_INVALID_ARGUMENT, kTraceError,
1981 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1982 return -1;
1983 }
1984
1985 const uint32_t notificationTime(0);
1986
kwiberg5a25d952016-08-17 07:31:12 -07001987 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001988 fileName, loop, startPosition, volumeScaling, notificationTime,
1989 stopPosition, (const CodecInst*)codecInst) != 0) {
1990 _engineStatisticsPtr->SetLastError(
1991 VE_BAD_FILE, kTraceError,
1992 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001993 input_file_player_->StopPlayingFile();
1994 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001995 return -1;
1996 }
kwiberg5a25d952016-08-17 07:31:12 -07001997 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001998 channel_state_.SetInputFilePlaying(true);
1999
2000 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002001}
2002
2003int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002004 FileFormats format,
2005 int startPosition,
2006 float volumeScaling,
2007 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002008 const CodecInst* codecInst) {
2009 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2010 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2011 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2012 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002013
kwiberg55b97fe2016-01-28 05:22:45 -08002014 if (stream == NULL) {
2015 _engineStatisticsPtr->SetLastError(
2016 VE_BAD_FILE, kTraceError,
2017 "StartPlayingFileAsMicrophone NULL as input stream");
2018 return -1;
2019 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002020
kwiberg55b97fe2016-01-28 05:22:45 -08002021 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002022
kwiberg55b97fe2016-01-28 05:22:45 -08002023 if (channel_state_.Get().input_file_playing) {
2024 _engineStatisticsPtr->SetLastError(
2025 VE_ALREADY_PLAYING, kTraceWarning,
2026 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002027 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002028 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002029
kwiberg55b97fe2016-01-28 05:22:45 -08002030 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002031 if (input_file_player_) {
2032 input_file_player_->RegisterModuleFileCallback(NULL);
2033 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002034 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002035
kwiberg55b97fe2016-01-28 05:22:45 -08002036 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002037 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002038 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002039
kwiberg5a25d952016-08-17 07:31:12 -07002040 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002041 _engineStatisticsPtr->SetLastError(
2042 VE_INVALID_ARGUMENT, kTraceError,
2043 "StartPlayingInputFile() filePlayer format isnot correct");
2044 return -1;
2045 }
2046
2047 const uint32_t notificationTime(0);
2048
kwiberg4ec01d92016-08-22 08:43:54 -07002049 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2050 notificationTime, stopPosition,
2051 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002052 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2053 "StartPlayingFile() failed to start "
2054 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002055 input_file_player_->StopPlayingFile();
2056 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002057 return -1;
2058 }
2059
kwiberg5a25d952016-08-17 07:31:12 -07002060 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002061 channel_state_.SetInputFilePlaying(true);
2062
2063 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002064}
2065
kwiberg55b97fe2016-01-28 05:22:45 -08002066int Channel::StopPlayingFileAsMicrophone() {
2067 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2068 "Channel::StopPlayingFileAsMicrophone()");
2069
2070 rtc::CritScope cs(&_fileCritSect);
2071
2072 if (!channel_state_.Get().input_file_playing) {
2073 return 0;
2074 }
2075
kwiberg5a25d952016-08-17 07:31:12 -07002076 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002077 _engineStatisticsPtr->SetLastError(
2078 VE_STOP_RECORDING_FAILED, kTraceError,
2079 "StopPlayingFile() could not stop playing");
2080 return -1;
2081 }
kwiberg5a25d952016-08-17 07:31:12 -07002082 input_file_player_->RegisterModuleFileCallback(NULL);
2083 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002084 channel_state_.SetInputFilePlaying(false);
2085
2086 return 0;
2087}
2088
2089int Channel::IsPlayingFileAsMicrophone() const {
2090 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002091}
2092
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002093int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002094 const CodecInst* codecInst) {
2095 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2096 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002097
kwiberg55b97fe2016-01-28 05:22:45 -08002098 if (_outputFileRecording) {
2099 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2100 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002101 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002102 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002103
kwiberg55b97fe2016-01-28 05:22:45 -08002104 FileFormats format;
2105 const uint32_t notificationTime(0); // Not supported in VoE
2106 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002107
kwiberg55b97fe2016-01-28 05:22:45 -08002108 if ((codecInst != NULL) &&
2109 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2110 _engineStatisticsPtr->SetLastError(
2111 VE_BAD_ARGUMENT, kTraceError,
2112 "StartRecordingPlayout() invalid compression");
2113 return (-1);
2114 }
2115 if (codecInst == NULL) {
2116 format = kFileFormatPcm16kHzFile;
2117 codecInst = &dummyCodec;
2118 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2119 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2120 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2121 format = kFileFormatWavFile;
2122 } else {
2123 format = kFileFormatCompressedFile;
2124 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002125
kwiberg55b97fe2016-01-28 05:22:45 -08002126 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002127
kwiberg55b97fe2016-01-28 05:22:45 -08002128 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002129 if (output_file_recorder_) {
2130 output_file_recorder_->RegisterModuleFileCallback(NULL);
2131 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002132 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002133
kwiberg5a25d952016-08-17 07:31:12 -07002134 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002135 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002136 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002137 _engineStatisticsPtr->SetLastError(
2138 VE_INVALID_ARGUMENT, kTraceError,
2139 "StartRecordingPlayout() fileRecorder format isnot correct");
2140 return -1;
2141 }
2142
kwiberg5a25d952016-08-17 07:31:12 -07002143 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002144 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2145 _engineStatisticsPtr->SetLastError(
2146 VE_BAD_FILE, kTraceError,
2147 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002148 output_file_recorder_->StopRecording();
2149 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002150 return -1;
2151 }
kwiberg5a25d952016-08-17 07:31:12 -07002152 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002153 _outputFileRecording = true;
2154
2155 return 0;
2156}
2157
2158int Channel::StartRecordingPlayout(OutStream* stream,
2159 const CodecInst* codecInst) {
2160 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2161 "Channel::StartRecordingPlayout()");
2162
2163 if (_outputFileRecording) {
2164 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2165 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002166 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002167 }
2168
2169 FileFormats format;
2170 const uint32_t notificationTime(0); // Not supported in VoE
2171 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2172
2173 if (codecInst != NULL && codecInst->channels != 1) {
2174 _engineStatisticsPtr->SetLastError(
2175 VE_BAD_ARGUMENT, kTraceError,
2176 "StartRecordingPlayout() invalid compression");
2177 return (-1);
2178 }
2179 if (codecInst == NULL) {
2180 format = kFileFormatPcm16kHzFile;
2181 codecInst = &dummyCodec;
2182 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2183 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2184 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2185 format = kFileFormatWavFile;
2186 } else {
2187 format = kFileFormatCompressedFile;
2188 }
2189
2190 rtc::CritScope cs(&_fileCritSect);
2191
2192 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002193 if (output_file_recorder_) {
2194 output_file_recorder_->RegisterModuleFileCallback(NULL);
2195 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002196 }
2197
kwiberg5a25d952016-08-17 07:31:12 -07002198 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002199 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002200 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002201 _engineStatisticsPtr->SetLastError(
2202 VE_INVALID_ARGUMENT, kTraceError,
2203 "StartRecordingPlayout() fileRecorder format isnot correct");
2204 return -1;
2205 }
2206
kwiberg4ec01d92016-08-22 08:43:54 -07002207 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002208 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002209 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2210 "StartRecordingPlayout() failed to "
2211 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002212 output_file_recorder_->StopRecording();
2213 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002214 return -1;
2215 }
2216
kwiberg5a25d952016-08-17 07:31:12 -07002217 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002218 _outputFileRecording = true;
2219
2220 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002221}
2222
kwiberg55b97fe2016-01-28 05:22:45 -08002223int Channel::StopRecordingPlayout() {
2224 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2225 "Channel::StopRecordingPlayout()");
2226
2227 if (!_outputFileRecording) {
2228 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2229 "StopRecordingPlayout() isnot recording");
2230 return -1;
2231 }
2232
2233 rtc::CritScope cs(&_fileCritSect);
2234
kwiberg5a25d952016-08-17 07:31:12 -07002235 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002236 _engineStatisticsPtr->SetLastError(
2237 VE_STOP_RECORDING_FAILED, kTraceError,
2238 "StopRecording() could not stop recording");
2239 return (-1);
2240 }
kwiberg5a25d952016-08-17 07:31:12 -07002241 output_file_recorder_->RegisterModuleFileCallback(NULL);
2242 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002243 _outputFileRecording = false;
2244
2245 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002246}
2247
kwiberg55b97fe2016-01-28 05:22:45 -08002248void Channel::SetMixWithMicStatus(bool mix) {
2249 rtc::CritScope cs(&_fileCritSect);
2250 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002251}
2252
kwiberg55b97fe2016-01-28 05:22:45 -08002253int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2254 int8_t currentLevel = _outputAudioLevel.Level();
2255 level = static_cast<int32_t>(currentLevel);
2256 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002257}
2258
kwiberg55b97fe2016-01-28 05:22:45 -08002259int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2260 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2261 level = static_cast<int32_t>(currentLevel);
2262 return 0;
2263}
2264
solenberg1c2af8e2016-03-24 10:36:00 -07002265int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002266 rtc::CritScope cs(&volume_settings_critsect_);
2267 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002268 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002269 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002270 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002271}
2272
solenberg1c2af8e2016-03-24 10:36:00 -07002273bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002274 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002275 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002276}
2277
kwiberg55b97fe2016-01-28 05:22:45 -08002278int Channel::SetOutputVolumePan(float left, float right) {
2279 rtc::CritScope cs(&volume_settings_critsect_);
2280 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002281 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002282 _panLeft = left;
2283 _panRight = right;
2284 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002285}
2286
kwiberg55b97fe2016-01-28 05:22:45 -08002287int Channel::GetOutputVolumePan(float& left, float& right) const {
2288 rtc::CritScope cs(&volume_settings_critsect_);
2289 left = _panLeft;
2290 right = _panRight;
2291 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002292}
2293
kwiberg55b97fe2016-01-28 05:22:45 -08002294int Channel::SetChannelOutputVolumeScaling(float scaling) {
2295 rtc::CritScope cs(&volume_settings_critsect_);
2296 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002297 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002298 _outputGain = scaling;
2299 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002300}
2301
kwiberg55b97fe2016-01-28 05:22:45 -08002302int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2303 rtc::CritScope cs(&volume_settings_critsect_);
2304 scaling = _outputGain;
2305 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002306}
2307
solenberg8842c3e2016-03-11 03:06:41 -08002308int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002309 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002310 "Channel::SendTelephoneEventOutband(...)");
2311 RTC_DCHECK_LE(0, event);
2312 RTC_DCHECK_GE(255, event);
2313 RTC_DCHECK_LE(0, duration_ms);
2314 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002315 if (!Sending()) {
2316 return -1;
2317 }
solenberg8842c3e2016-03-11 03:06:41 -08002318 if (_rtpRtcpModule->SendTelephoneEventOutband(
2319 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002320 _engineStatisticsPtr->SetLastError(
2321 VE_SEND_DTMF_FAILED, kTraceWarning,
2322 "SendTelephoneEventOutband() failed to send event");
2323 return -1;
2324 }
2325 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002326}
2327
solenbergffbbcac2016-11-17 05:25:37 -08002328int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2329 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002330 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002331 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002332 RTC_DCHECK_LE(0, payload_type);
2333 RTC_DCHECK_GE(127, payload_type);
2334 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002335 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002336 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002337 memcpy(codec.plname, "telephone-event", 16);
2338 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2339 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2340 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2341 _engineStatisticsPtr->SetLastError(
2342 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2343 "SetSendTelephoneEventPayloadType() failed to register send"
2344 "payload type");
2345 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002346 }
kwiberg55b97fe2016-01-28 05:22:45 -08002347 }
kwiberg55b97fe2016-01-28 05:22:45 -08002348 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002349}
2350
kwiberg55b97fe2016-01-28 05:22:45 -08002351int Channel::VoiceActivityIndicator(int& activity) {
2352 activity = _sendFrameType;
2353 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002354}
2355
kwiberg55b97fe2016-01-28 05:22:45 -08002356int Channel::SetLocalSSRC(unsigned int ssrc) {
2357 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2358 "Channel::SetLocalSSRC()");
2359 if (channel_state_.Get().sending) {
2360 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2361 "SetLocalSSRC() already sending");
2362 return -1;
2363 }
2364 _rtpRtcpModule->SetSSRC(ssrc);
2365 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002366}
2367
kwiberg55b97fe2016-01-28 05:22:45 -08002368int Channel::GetLocalSSRC(unsigned int& ssrc) {
2369 ssrc = _rtpRtcpModule->SSRC();
2370 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002371}
2372
kwiberg55b97fe2016-01-28 05:22:45 -08002373int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2374 ssrc = rtp_receiver_->SSRC();
2375 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002376}
2377
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002378int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002379 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002380 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002381}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002382
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002383int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2384 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002385 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2386 if (enable &&
2387 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2388 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002389 return -1;
2390 }
2391 return 0;
2392}
2393
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002394void Channel::EnableSendTransportSequenceNumber(int id) {
2395 int ret =
2396 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2397 RTC_DCHECK_EQ(0, ret);
2398}
2399
stefan3313ec92016-01-21 06:32:43 -08002400void Channel::EnableReceiveTransportSequenceNumber(int id) {
2401 rtp_header_parser_->DeregisterRtpHeaderExtension(
2402 kRtpExtensionTransportSequenceNumber);
2403 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2404 kRtpExtensionTransportSequenceNumber, id);
2405 RTC_DCHECK(ret);
2406}
2407
stefanbba9dec2016-02-01 04:39:55 -08002408void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002409 RtpPacketSender* rtp_packet_sender,
2410 TransportFeedbackObserver* transport_feedback_observer,
stefan7de8d642017-02-07 07:14:08 -08002411 PacketRouter* packet_router,
2412 RtcpBandwidthObserver* bandwidth_observer) {
stefanbba9dec2016-02-01 04:39:55 -08002413 RTC_DCHECK(rtp_packet_sender);
2414 RTC_DCHECK(transport_feedback_observer);
2415 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002416 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002417 feedback_observer_proxy_->SetTransportFeedbackObserver(
2418 transport_feedback_observer);
2419 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2420 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2421 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002422 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002423 packet_router_ = packet_router;
2424}
2425
stefanbba9dec2016-02-01 04:39:55 -08002426void Channel::RegisterReceiverCongestionControlObjects(
2427 PacketRouter* packet_router) {
2428 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002429 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002430 packet_router_ = packet_router;
2431}
2432
2433void Channel::ResetCongestionControlObjects() {
2434 RTC_DCHECK(packet_router_);
2435 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002436 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002437 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2438 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002439 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002440 packet_router_ = nullptr;
2441 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2442}
2443
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002444void Channel::SetRTCPStatus(bool enable) {
2445 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2446 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002447 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002448}
2449
kwiberg55b97fe2016-01-28 05:22:45 -08002450int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002451 RtcpMode method = _rtpRtcpModule->RTCP();
2452 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002453 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002454}
2455
kwiberg55b97fe2016-01-28 05:22:45 -08002456int Channel::SetRTCP_CNAME(const char cName[256]) {
2457 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2458 "Channel::SetRTCP_CNAME()");
2459 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2460 _engineStatisticsPtr->SetLastError(
2461 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2462 "SetRTCP_CNAME() failed to set RTCP CNAME");
2463 return -1;
2464 }
2465 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002466}
2467
kwiberg55b97fe2016-01-28 05:22:45 -08002468int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2469 if (cName == NULL) {
2470 _engineStatisticsPtr->SetLastError(
2471 VE_INVALID_ARGUMENT, kTraceError,
2472 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2473 return -1;
2474 }
2475 char cname[RTCP_CNAME_SIZE];
2476 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2477 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2478 _engineStatisticsPtr->SetLastError(
2479 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2480 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2481 return -1;
2482 }
2483 strcpy(cName, cname);
2484 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002485}
2486
kwiberg55b97fe2016-01-28 05:22:45 -08002487int Channel::SendApplicationDefinedRTCPPacket(
2488 unsigned char subType,
2489 unsigned int name,
2490 const char* data,
2491 unsigned short dataLengthInBytes) {
2492 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2493 "Channel::SendApplicationDefinedRTCPPacket()");
2494 if (!channel_state_.Get().sending) {
2495 _engineStatisticsPtr->SetLastError(
2496 VE_NOT_SENDING, kTraceError,
2497 "SendApplicationDefinedRTCPPacket() not sending");
2498 return -1;
2499 }
2500 if (NULL == data) {
2501 _engineStatisticsPtr->SetLastError(
2502 VE_INVALID_ARGUMENT, kTraceError,
2503 "SendApplicationDefinedRTCPPacket() invalid data value");
2504 return -1;
2505 }
2506 if (dataLengthInBytes % 4 != 0) {
2507 _engineStatisticsPtr->SetLastError(
2508 VE_INVALID_ARGUMENT, kTraceError,
2509 "SendApplicationDefinedRTCPPacket() invalid length value");
2510 return -1;
2511 }
2512 RtcpMode status = _rtpRtcpModule->RTCP();
2513 if (status == RtcpMode::kOff) {
2514 _engineStatisticsPtr->SetLastError(
2515 VE_RTCP_ERROR, kTraceError,
2516 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2517 return -1;
2518 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002519
kwiberg55b97fe2016-01-28 05:22:45 -08002520 // Create and schedule the RTCP APP packet for transmission
2521 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2522 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2523 _engineStatisticsPtr->SetLastError(
2524 VE_SEND_ERROR, kTraceError,
2525 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2526 return -1;
2527 }
2528 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002529}
2530
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002531int Channel::GetRemoteRTCPReportBlocks(
2532 std::vector<ReportBlock>* report_blocks) {
2533 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002534 _engineStatisticsPtr->SetLastError(
2535 VE_INVALID_ARGUMENT, kTraceError,
2536 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002537 return -1;
2538 }
2539
2540 // Get the report blocks from the latest received RTCP Sender or Receiver
2541 // Report. Each element in the vector contains the sender's SSRC and a
2542 // report block according to RFC 3550.
2543 std::vector<RTCPReportBlock> rtcp_report_blocks;
2544 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002545 return -1;
2546 }
2547
2548 if (rtcp_report_blocks.empty())
2549 return 0;
2550
2551 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2552 for (; it != rtcp_report_blocks.end(); ++it) {
2553 ReportBlock report_block;
2554 report_block.sender_SSRC = it->remoteSSRC;
2555 report_block.source_SSRC = it->sourceSSRC;
2556 report_block.fraction_lost = it->fractionLost;
2557 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2558 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2559 report_block.interarrival_jitter = it->jitter;
2560 report_block.last_SR_timestamp = it->lastSR;
2561 report_block.delay_since_last_SR = it->delaySinceLastSR;
2562 report_blocks->push_back(report_block);
2563 }
2564 return 0;
2565}
2566
kwiberg55b97fe2016-01-28 05:22:45 -08002567int Channel::GetRTPStatistics(CallStatistics& stats) {
2568 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002569
kwiberg55b97fe2016-01-28 05:22:45 -08002570 // The jitter statistics is updated for each received RTP packet and is
2571 // based on received packets.
2572 RtcpStatistics statistics;
2573 StreamStatistician* statistician =
2574 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002575 if (statistician) {
2576 statistician->GetStatistics(&statistics,
2577 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002578 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002579
kwiberg55b97fe2016-01-28 05:22:45 -08002580 stats.fractionLost = statistics.fraction_lost;
2581 stats.cumulativeLost = statistics.cumulative_lost;
2582 stats.extendedMax = statistics.extended_max_sequence_number;
2583 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002584
kwiberg55b97fe2016-01-28 05:22:45 -08002585 // --- RTT
2586 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002587
kwiberg55b97fe2016-01-28 05:22:45 -08002588 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002589
kwiberg55b97fe2016-01-28 05:22:45 -08002590 size_t bytesSent(0);
2591 uint32_t packetsSent(0);
2592 size_t bytesReceived(0);
2593 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002594
kwiberg55b97fe2016-01-28 05:22:45 -08002595 if (statistician) {
2596 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2597 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002598
kwiberg55b97fe2016-01-28 05:22:45 -08002599 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2600 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2601 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2602 " output will not be complete");
2603 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002604
kwiberg55b97fe2016-01-28 05:22:45 -08002605 stats.bytesSent = bytesSent;
2606 stats.packetsSent = packetsSent;
2607 stats.bytesReceived = bytesReceived;
2608 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002609
kwiberg55b97fe2016-01-28 05:22:45 -08002610 // --- Timestamps
2611 {
2612 rtc::CritScope lock(&ts_stats_lock_);
2613 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2614 }
2615 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002616}
2617
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002618int Channel::SetCodecFECStatus(bool enable) {
2619 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2620 "Channel::SetCodecFECStatus()");
2621
kwibergc8d071e2016-04-06 12:22:38 -07002622 if (!codec_manager_.SetCodecFEC(enable) ||
2623 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002624 _engineStatisticsPtr->SetLastError(
2625 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2626 "SetCodecFECStatus() failed to set FEC state");
2627 return -1;
2628 }
2629 return 0;
2630}
2631
2632bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002633 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002634}
2635
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002636void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2637 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002638 // If pacing is enabled we always store packets.
2639 if (!pacing_enabled_)
2640 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002641 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002642 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002643 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002644 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002645 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002646}
2647
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002648// Called when we are missing one or more packets.
2649int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002650 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2651}
2652
kwiberg55b97fe2016-01-28 05:22:45 -08002653uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2654 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2655 "Channel::Demultiplex()");
2656 _audioFrame.CopyFrom(audioFrame);
2657 _audioFrame.id_ = _channelId;
2658 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002659}
2660
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002661void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002662 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002663 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002664 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002665 CodecInst codec;
2666 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002667
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002668 // Never upsample or upmix the capture signal here. This should be done at the
2669 // end of the send chain.
2670 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2671 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2672 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2673 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002674}
2675
kwiberg55b97fe2016-01-28 05:22:45 -08002676uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2677 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2678 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002679
kwiberg55b97fe2016-01-28 05:22:45 -08002680 if (_audioFrame.samples_per_channel_ == 0) {
2681 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2682 "Channel::PrepareEncodeAndSend() invalid audio frame");
2683 return 0xFFFFFFFF;
2684 }
2685
2686 if (channel_state_.Get().input_file_playing) {
2687 MixOrReplaceAudioWithFile(mixingFrequency);
2688 }
2689
solenberg1c2af8e2016-03-24 10:36:00 -07002690 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2691 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002692
kwiberg55b97fe2016-01-28 05:22:45 -08002693 if (_includeAudioLevelIndication) {
2694 size_t length =
2695 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002696 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002697 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002698 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002699 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002700 rms_level_.Analyze(
2701 rtc::ArrayView<const int16_t>(_audioFrame.data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002702 }
kwiberg55b97fe2016-01-28 05:22:45 -08002703 }
solenberg1c2af8e2016-03-24 10:36:00 -07002704 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002705
kwiberg55b97fe2016-01-28 05:22:45 -08002706 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002707}
2708
kwiberg55b97fe2016-01-28 05:22:45 -08002709uint32_t Channel::EncodeAndSend() {
2710 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2711 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002712
kwiberg55b97fe2016-01-28 05:22:45 -08002713 assert(_audioFrame.num_channels_ <= 2);
2714 if (_audioFrame.samples_per_channel_ == 0) {
2715 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2716 "Channel::EncodeAndSend() invalid audio frame");
2717 return 0xFFFFFFFF;
2718 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002719
kwiberg55b97fe2016-01-28 05:22:45 -08002720 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002721
kwiberg55b97fe2016-01-28 05:22:45 -08002722 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002723
kwiberg55b97fe2016-01-28 05:22:45 -08002724 // The ACM resamples internally.
2725 _audioFrame.timestamp_ = _timeStamp;
2726 // This call will trigger AudioPacketizationCallback::SendData if encoding
2727 // is done and payload is ready for packetization and transmission.
2728 // Otherwise, it will return without invoking the callback.
2729 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2730 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2731 "Channel::EncodeAndSend() ACM encoding failed");
2732 return 0xFFFFFFFF;
2733 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002734
kwiberg55b97fe2016-01-28 05:22:45 -08002735 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2736 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002737}
2738
solenberg7602aab2016-11-14 11:30:07 -08002739void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2740 RTC_DCHECK(!channel.channel() ||
2741 channel.channel()->ChannelId() != _channelId);
2742 rtc::CritScope lock(&assoc_send_channel_lock_);
2743 associate_send_channel_ = channel;
2744}
2745
Minyue2013aec2015-05-13 14:14:42 +02002746void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002747 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002748 Channel* channel = associate_send_channel_.channel();
2749 if (channel && channel->ChannelId() == channel_id) {
2750 // If this channel is associated with a send channel of the specified
2751 // Channel ID, disassociate with it.
2752 ChannelOwner ref(NULL);
2753 associate_send_channel_ = ref;
2754 }
2755}
2756
ivoc14d5dbe2016-07-04 07:06:55 -07002757void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2758 event_log_proxy_->SetEventLog(event_log);
2759}
2760
michaelt9332b7d2016-11-30 07:51:13 -08002761void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2762 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2763}
2764
nisse284542b2017-01-10 08:58:32 -08002765void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08002766 size_t overhead_per_packet =
2767 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08002768 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2769 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08002770 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08002771 }
2772 });
2773}
2774
2775void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002776 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002777 transport_overhead_per_packet_ = transport_overhead_per_packet;
2778 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002779}
2780
hbos3fd31fe2017-02-28 05:43:16 -08002781// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08002782void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002783 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002784 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2785 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002786}
2787
kwiberg55b97fe2016-01-28 05:22:45 -08002788int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2789 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002790}
2791
wu@webrtc.org24301a62013-12-13 19:17:43 +00002792void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2793 audio_coding_->GetDecodingCallStatistics(stats);
2794}
2795
solenberg358057b2015-11-27 10:46:42 -08002796uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002797 rtc::CritScope lock(&video_sync_lock_);
2798 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002799}
2800
kwiberg55b97fe2016-01-28 05:22:45 -08002801int Channel::SetMinimumPlayoutDelay(int delayMs) {
2802 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2803 "Channel::SetMinimumPlayoutDelay()");
2804 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2805 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2806 _engineStatisticsPtr->SetLastError(
2807 VE_INVALID_ARGUMENT, kTraceError,
2808 "SetMinimumPlayoutDelay() invalid min delay");
2809 return -1;
2810 }
2811 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2812 _engineStatisticsPtr->SetLastError(
2813 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2814 "SetMinimumPlayoutDelay() failed to set min playout delay");
2815 return -1;
2816 }
2817 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002818}
2819
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002820int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002821 uint32_t playout_timestamp_rtp = 0;
2822 {
tommi31fc21f2016-01-21 10:37:37 -08002823 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002824 playout_timestamp_rtp = playout_timestamp_rtp_;
2825 }
kwiberg55b97fe2016-01-28 05:22:45 -08002826 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002827 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002828 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002829 "GetPlayoutTimestamp() failed to retrieve timestamp");
2830 return -1;
2831 }
deadbeef74375882015-08-13 12:09:10 -07002832 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002833 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002834}
2835
kwiberg55b97fe2016-01-28 05:22:45 -08002836int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2837 RtpReceiver** rtp_receiver) const {
2838 *rtpRtcpModule = _rtpRtcpModule.get();
2839 *rtp_receiver = rtp_receiver_.get();
2840 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002841}
2842
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002843// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2844// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002845int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002846 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002847 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002848
kwiberg55b97fe2016-01-28 05:22:45 -08002849 {
2850 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002851
kwiberg5a25d952016-08-17 07:31:12 -07002852 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002853 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2854 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2855 " doesnt exist");
2856 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002857 }
2858
kwiberg4ec01d92016-08-22 08:43:54 -07002859 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002860 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002861 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2862 "Channel::MixOrReplaceAudioWithFile() file mixing "
2863 "failed");
2864 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002865 }
kwiberg55b97fe2016-01-28 05:22:45 -08002866 if (fileSamples == 0) {
2867 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2868 "Channel::MixOrReplaceAudioWithFile() file is ended");
2869 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002870 }
kwiberg55b97fe2016-01-28 05:22:45 -08002871 }
2872
2873 assert(_audioFrame.samples_per_channel_ == fileSamples);
2874
2875 if (_mixFileWithMicrophone) {
2876 // Currently file stream is always mono.
2877 // TODO(xians): Change the code when FilePlayer supports real stereo.
2878 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
2879 1, fileSamples);
2880 } else {
2881 // Replace ACM audio with file.
2882 // Currently file stream is always mono.
2883 // TODO(xians): Change the code when FilePlayer supports real stereo.
2884 _audioFrame.UpdateFrame(
2885 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2886 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2887 }
2888 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002889}
2890
kwiberg55b97fe2016-01-28 05:22:45 -08002891int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2892 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002893
kwibergb7f89d62016-02-17 10:04:18 -08002894 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002895 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002896
kwiberg55b97fe2016-01-28 05:22:45 -08002897 {
2898 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002899
kwiberg5a25d952016-08-17 07:31:12 -07002900 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002901 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2902 "Channel::MixAudioWithFile() file mixing failed");
2903 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002904 }
2905
kwiberg55b97fe2016-01-28 05:22:45 -08002906 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002907 if (output_file_player_->Get10msAudioFromFile(
2908 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002909 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2910 "Channel::MixAudioWithFile() file mixing failed");
2911 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002912 }
kwiberg55b97fe2016-01-28 05:22:45 -08002913 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002914
kwiberg55b97fe2016-01-28 05:22:45 -08002915 if (audioFrame.samples_per_channel_ == fileSamples) {
2916 // Currently file stream is always mono.
2917 // TODO(xians): Change the code when FilePlayer supports real stereo.
2918 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
2919 fileSamples);
2920 } else {
2921 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2922 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2923 ") != "
2924 "fileSamples(%" PRIuS ")",
2925 audioFrame.samples_per_channel_, fileSamples);
2926 return -1;
2927 }
2928
2929 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002930}
2931
deadbeef74375882015-08-13 12:09:10 -07002932void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002933 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002934
henrik.lundin96bd5022016-04-06 04:13:56 -07002935 if (!jitter_buffer_playout_timestamp_) {
2936 // This can happen if this channel has not received any RTP packets. In
2937 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07002938 return;
2939 }
2940
2941 uint16_t delay_ms = 0;
2942 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002943 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002944 "Channel::UpdatePlayoutTimestamp() failed to read playout"
2945 " delay from the ADM");
2946 _engineStatisticsPtr->SetLastError(
2947 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
2948 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
2949 return;
2950 }
2951
henrik.lundin96bd5022016-04-06 04:13:56 -07002952 RTC_DCHECK(jitter_buffer_playout_timestamp_);
2953 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07002954
2955 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07002956 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07002957
kwiberg55b97fe2016-01-28 05:22:45 -08002958 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002959 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07002960 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07002961
2962 {
tommi31fc21f2016-01-21 10:37:37 -08002963 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08002964 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002965 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07002966 }
2967 playout_delay_ms_ = delay_ms;
2968 }
2969}
2970
kwiberg55b97fe2016-01-28 05:22:45 -08002971void Channel::RegisterReceiveCodecsToRTPModule() {
2972 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2973 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002974
kwiberg55b97fe2016-01-28 05:22:45 -08002975 CodecInst codec;
2976 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002977
kwiberg55b97fe2016-01-28 05:22:45 -08002978 for (int idx = 0; idx < nSupportedCodecs; idx++) {
2979 // Open up the RTP/RTCP receiver for all supported codecs
2980 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08002981 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08002982 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2983 "Channel::RegisterReceiveCodecsToRTPModule() unable"
2984 " to register %s (%d/%d/%" PRIuS
2985 "/%d) to RTP/RTCP "
2986 "receiver",
2987 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2988 codec.rate);
2989 } else {
2990 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2991 "Channel::RegisterReceiveCodecsToRTPModule() %s "
2992 "(%d/%d/%" PRIuS
2993 "/%d) has been added to the RTP/RTCP "
2994 "receiver",
2995 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2996 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00002997 }
kwiberg55b97fe2016-01-28 05:22:45 -08002998 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002999}
3000
kwiberg55b97fe2016-01-28 05:22:45 -08003001int Channel::SetSendRtpHeaderExtension(bool enable,
3002 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003003 unsigned char id) {
3004 int error = 0;
3005 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3006 if (enable) {
3007 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3008 }
3009 return error;
3010}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003011
ossue280cde2016-10-12 11:04:10 -07003012int Channel::GetRtpTimestampRateHz() const {
3013 const auto format = audio_coding_->ReceiveFormat();
3014 // Default to the playout frequency if we've not gotten any packets yet.
3015 // TODO(ossu): Zero clockrate can only happen if we've added an external
3016 // decoder for a format we don't support internally. Remove once that way of
3017 // adding decoders is gone!
3018 return (format && format->clockrate_hz != 0)
3019 ? format->clockrate_hz
3020 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003021}
3022
Minyue2013aec2015-05-13 14:14:42 +02003023int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003024 RtcpMode method = _rtpRtcpModule->RTCP();
3025 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003026 return 0;
3027 }
3028 std::vector<RTCPReportBlock> report_blocks;
3029 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003030
3031 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003032 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003033 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003034 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003035 Channel* channel = associate_send_channel_.channel();
3036 // Tries to get RTT from an associated channel. This is important for
3037 // receive-only channels.
3038 if (channel) {
3039 // To prevent infinite recursion and deadlock, calling GetRTT of
3040 // associate channel should always use "false" for argument:
3041 // |allow_associate_channel|.
3042 rtt = channel->GetRTT(false);
3043 }
3044 }
3045 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003046 }
3047
3048 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3049 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3050 for (; it != report_blocks.end(); ++it) {
3051 if (it->remoteSSRC == remoteSSRC)
3052 break;
3053 }
3054 if (it == report_blocks.end()) {
3055 // We have not received packets with SSRC matching the report blocks.
3056 // To calculate RTT we try with the SSRC of the first report block.
3057 // This is very important for send-only channels where we don't know
3058 // the SSRC of the other end.
3059 remoteSSRC = report_blocks[0].remoteSSRC;
3060 }
Minyue2013aec2015-05-13 14:14:42 +02003061
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003062 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003063 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003064 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003065 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3066 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003067 return 0;
3068 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003069 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003070}
3071
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003072} // namespace voe
3073} // namespace webrtc