blob: 7e49e321cfc2affe6041f05a2c446d7d2fbdf7c3 [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"
wu@webrtc.org94454b72014-06-05 20:34:08 +000024#include "webrtc/base/timeutils.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020025#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070026#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070027#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000028#include "webrtc/modules/audio_device/include/audio_device.h"
29#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010031#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010032#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
33#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
34#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
nisse657bab22017-02-21 06:28:10 -080035#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000036#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010037#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010038#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000039#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
40#include "webrtc/voice_engine/output_mixer.h"
41#include "webrtc/voice_engine/statistics.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000042#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000043
andrew@webrtc.org50419b02012-11-14 19:07:54 +000044namespace webrtc {
45namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000046
kwibergc8d071e2016-04-06 12:22:38 -070047namespace {
48
Erik Språng737336d2016-07-29 12:59:36 +020049constexpr int64_t kMaxRetransmissionWindowMs = 1000;
50constexpr int64_t kMinRetransmissionWindowMs = 30;
51
kwibergc8d071e2016-04-06 12:22:38 -070052} // namespace
53
solenberg8842c3e2016-03-11 03:06:41 -080054const int kTelephoneEventAttenuationdB = 10;
55
ivoc14d5dbe2016-07-04 07:06:55 -070056class RtcEventLogProxy final : public webrtc::RtcEventLog {
57 public:
58 RtcEventLogProxy() : event_log_(nullptr) {}
59
60 bool StartLogging(const std::string& file_name,
61 int64_t max_size_bytes) override {
62 RTC_NOTREACHED();
63 return false;
64 }
65
66 bool StartLogging(rtc::PlatformFile log_file,
67 int64_t max_size_bytes) override {
68 RTC_NOTREACHED();
69 return false;
70 }
71
72 void StopLogging() override { RTC_NOTREACHED(); }
73
74 void LogVideoReceiveStreamConfig(
75 const webrtc::VideoReceiveStream::Config& config) override {
76 rtc::CritScope lock(&crit_);
77 if (event_log_) {
78 event_log_->LogVideoReceiveStreamConfig(config);
79 }
80 }
81
82 void LogVideoSendStreamConfig(
83 const webrtc::VideoSendStream::Config& config) override {
84 rtc::CritScope lock(&crit_);
85 if (event_log_) {
86 event_log_->LogVideoSendStreamConfig(config);
87 }
88 }
89
ivoce0928d82016-10-10 05:12:51 -070090 void LogAudioReceiveStreamConfig(
91 const webrtc::AudioReceiveStream::Config& config) override {
92 rtc::CritScope lock(&crit_);
93 if (event_log_) {
94 event_log_->LogAudioReceiveStreamConfig(config);
95 }
96 }
97
98 void LogAudioSendStreamConfig(
99 const webrtc::AudioSendStream::Config& config) override {
100 rtc::CritScope lock(&crit_);
101 if (event_log_) {
102 event_log_->LogAudioSendStreamConfig(config);
103 }
104 }
105
ivoc14d5dbe2016-07-04 07:06:55 -0700106 void LogRtpHeader(webrtc::PacketDirection direction,
107 webrtc::MediaType media_type,
108 const uint8_t* header,
109 size_t packet_length) override {
philipel32d00102017-02-27 02:18:46 -0800110 LogRtpHeader(direction, media_type, header, packet_length,
111 PacedPacketInfo::kNotAProbe);
112 }
113
114 void LogRtpHeader(webrtc::PacketDirection direction,
115 webrtc::MediaType media_type,
116 const uint8_t* header,
117 size_t packet_length,
118 int probe_cluster_id) override {
ivoc14d5dbe2016-07-04 07:06:55 -0700119 rtc::CritScope lock(&crit_);
120 if (event_log_) {
philipel32d00102017-02-27 02:18:46 -0800121 event_log_->LogRtpHeader(direction, media_type, header, packet_length,
122 probe_cluster_id);
ivoc14d5dbe2016-07-04 07:06:55 -0700123 }
124 }
125
126 void LogRtcpPacket(webrtc::PacketDirection direction,
127 webrtc::MediaType media_type,
128 const uint8_t* packet,
129 size_t length) override {
130 rtc::CritScope lock(&crit_);
131 if (event_log_) {
132 event_log_->LogRtcpPacket(direction, media_type, packet, length);
133 }
134 }
135
136 void LogAudioPlayout(uint32_t ssrc) override {
137 rtc::CritScope lock(&crit_);
138 if (event_log_) {
139 event_log_->LogAudioPlayout(ssrc);
140 }
141 }
142
terelius424e6cf2017-02-20 05:14:41 -0800143 void LogLossBasedBweUpdate(int32_t bitrate_bps,
ivoc14d5dbe2016-07-04 07:06:55 -0700144 uint8_t fraction_loss,
145 int32_t total_packets) override {
146 rtc::CritScope lock(&crit_);
147 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800148 event_log_->LogLossBasedBweUpdate(bitrate_bps, fraction_loss,
149 total_packets);
ivoc14d5dbe2016-07-04 07:06:55 -0700150 }
151 }
152
terelius424e6cf2017-02-20 05:14:41 -0800153 void LogDelayBasedBweUpdate(int32_t bitrate_bps,
terelius0baf55d2017-02-17 03:38:28 -0800154 BandwidthUsage detector_state) override {
155 rtc::CritScope lock(&crit_);
156 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800157 event_log_->LogDelayBasedBweUpdate(bitrate_bps, detector_state);
terelius0baf55d2017-02-17 03:38:28 -0800158 }
159 }
160
minyue4b7c9522017-01-24 04:54:59 -0800161 void LogAudioNetworkAdaptation(
162 const AudioNetworkAdaptor::EncoderRuntimeConfig& config) override {
163 rtc::CritScope lock(&crit_);
164 if (event_log_) {
165 event_log_->LogAudioNetworkAdaptation(config);
166 }
167 }
168
philipel32d00102017-02-27 02:18:46 -0800169 void LogProbeClusterCreated(int id,
170 int bitrate_bps,
171 int min_probes,
172 int min_bytes) override {
173 rtc::CritScope lock(&crit_);
174 if (event_log_) {
175 event_log_->LogProbeClusterCreated(id, bitrate_bps, min_probes,
176 min_bytes);
177 }
178 };
179
180 void LogProbeResultSuccess(int id, int bitrate_bps) override {
181 rtc::CritScope lock(&crit_);
182 if (event_log_) {
183 event_log_->LogProbeResultSuccess(id, bitrate_bps);
184 }
185 };
186
187 void LogProbeResultFailure(int id,
188 ProbeFailureReason failure_reason) override {
189 rtc::CritScope lock(&crit_);
190 if (event_log_) {
191 event_log_->LogProbeResultFailure(id, failure_reason);
192 }
193 };
194
ivoc14d5dbe2016-07-04 07:06:55 -0700195 void SetEventLog(RtcEventLog* event_log) {
196 rtc::CritScope lock(&crit_);
197 event_log_ = event_log;
198 }
199
200 private:
201 rtc::CriticalSection crit_;
202 RtcEventLog* event_log_ GUARDED_BY(crit_);
203 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
204};
205
michaelt9332b7d2016-11-30 07:51:13 -0800206class RtcpRttStatsProxy final : public RtcpRttStats {
207 public:
208 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
209
210 void OnRttUpdate(int64_t rtt) override {
211 rtc::CritScope lock(&crit_);
212 if (rtcp_rtt_stats_)
213 rtcp_rtt_stats_->OnRttUpdate(rtt);
214 }
215
216 int64_t LastProcessedRtt() const override {
217 rtc::CritScope lock(&crit_);
218 if (!rtcp_rtt_stats_)
219 return 0;
220 return rtcp_rtt_stats_->LastProcessedRtt();
221 }
222
223 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
224 rtc::CritScope lock(&crit_);
225 rtcp_rtt_stats_ = rtcp_rtt_stats;
226 }
227
228 private:
229 rtc::CriticalSection crit_;
230 RtcpRttStats* rtcp_rtt_stats_ GUARDED_BY(crit_);
231 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
232};
233
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100234class TransportFeedbackProxy : public TransportFeedbackObserver {
235 public:
236 TransportFeedbackProxy() : feedback_observer_(nullptr) {
237 pacer_thread_.DetachFromThread();
238 network_thread_.DetachFromThread();
239 }
240
241 void SetTransportFeedbackObserver(
242 TransportFeedbackObserver* feedback_observer) {
243 RTC_DCHECK(thread_checker_.CalledOnValidThread());
244 rtc::CritScope lock(&crit_);
245 feedback_observer_ = feedback_observer;
246 }
247
248 // Implements TransportFeedbackObserver.
elad.alond12a8e12017-03-23 11:04:48 -0700249 void AddPacket(uint32_t ssrc,
250 uint16_t sequence_number,
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100251 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_)
elad.alond12a8e12017-03-23 11:04:48 -0700256 feedback_observer_->AddPacket(ssrc, 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 }
elad.alond12a8e12017-03-23 11:04:48 -0700399 owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000400 }
401
402 private:
403 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000404 // Maps remote side ssrc to extended highest sequence number received.
405 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800406 rtc::CriticalSection crit_;
407 RtcpBandwidthObserver* bandwidth_observer_ GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000408};
409
kwiberg55b97fe2016-01-28 05:22:45 -0800410int32_t Channel::SendData(FrameType frameType,
411 uint8_t payloadType,
412 uint32_t timeStamp,
413 const uint8_t* payloadData,
414 size_t payloadSize,
415 const RTPFragmentationHeader* fragmentation) {
416 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
417 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
418 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
419 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000420
kwiberg55b97fe2016-01-28 05:22:45 -0800421 if (_includeAudioLevelIndication) {
422 // Store current audio level in the RTP/RTCP module.
423 // The level will be used in combination with voice-activity state
424 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800425 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800426 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000427
kwiberg55b97fe2016-01-28 05:22:45 -0800428 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
429 // packetization.
430 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700431 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800432 (FrameType&)frameType, payloadType, timeStamp,
433 // Leaving the time when this frame was
434 // received from the capture device as
435 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700436 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800437 _engineStatisticsPtr->SetLastError(
438 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
439 "Channel::SendData() failed to send data to RTP/RTCP module");
440 return -1;
441 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000442
kwiberg55b97fe2016-01-28 05:22:45 -0800443 _lastLocalTimeStamp = timeStamp;
444 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000445
kwiberg55b97fe2016-01-28 05:22:45 -0800446 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000447}
448
stefan1d8a5062015-10-02 03:39:33 -0700449bool Channel::SendRtp(const uint8_t* data,
450 size_t len,
451 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800452 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
453 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000454
kwiberg55b97fe2016-01-28 05:22:45 -0800455 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000456
kwiberg55b97fe2016-01-28 05:22:45 -0800457 if (_transportPtr == NULL) {
458 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
459 "Channel::SendPacket() failed to send RTP packet due to"
460 " invalid transport object");
461 return false;
462 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000463
kwiberg55b97fe2016-01-28 05:22:45 -0800464 uint8_t* bufferToSendPtr = (uint8_t*)data;
465 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000466
kwiberg55b97fe2016-01-28 05:22:45 -0800467 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
468 std::string transport_name =
469 _externalTransport ? "external transport" : "WebRtc sockets";
470 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
471 "Channel::SendPacket() RTP transmission using %s failed",
472 transport_name.c_str());
473 return false;
474 }
475 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000476}
477
kwiberg55b97fe2016-01-28 05:22:45 -0800478bool Channel::SendRtcp(const uint8_t* data, size_t len) {
479 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
480 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000481
kwiberg55b97fe2016-01-28 05:22:45 -0800482 rtc::CritScope cs(&_callbackCritSect);
483 if (_transportPtr == NULL) {
484 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
485 "Channel::SendRtcp() failed to send RTCP packet"
486 " due to invalid transport object");
487 return false;
488 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000489
kwiberg55b97fe2016-01-28 05:22:45 -0800490 uint8_t* bufferToSendPtr = (uint8_t*)data;
491 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
kwiberg55b97fe2016-01-28 05:22:45 -0800493 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
494 if (n < 0) {
495 std::string transport_name =
496 _externalTransport ? "external transport" : "WebRtc sockets";
497 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
498 "Channel::SendRtcp() transmission using %s failed",
499 transport_name.c_str());
500 return false;
501 }
502 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000503}
504
kwiberg55b97fe2016-01-28 05:22:45 -0800505void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
506 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
507 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000508
kwiberg55b97fe2016-01-28 05:22:45 -0800509 // Update ssrc so that NTP for AV sync can be updated.
510 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000511}
512
Peter Boströmac547a62015-09-17 23:03:57 +0200513void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
514 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
515 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
516 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000517}
518
Peter Boströmac547a62015-09-17 23:03:57 +0200519int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000520 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000521 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000522 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800523 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200524 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800525 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
526 "Channel::OnInitializeDecoder(payloadType=%d, "
527 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
528 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000529
kwiberg55b97fe2016-01-28 05:22:45 -0800530 CodecInst receiveCodec = {0};
531 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000532
kwiberg55b97fe2016-01-28 05:22:45 -0800533 receiveCodec.pltype = payloadType;
534 receiveCodec.plfreq = frequency;
535 receiveCodec.channels = channels;
536 receiveCodec.rate = rate;
537 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000538
kwiberg55b97fe2016-01-28 05:22:45 -0800539 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
540 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000541
kwiberg55b97fe2016-01-28 05:22:45 -0800542 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700543 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
544 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800545 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
546 "Channel::OnInitializeDecoder() invalid codec ("
547 "pt=%d, name=%s) received - 1",
548 payloadType, payloadName);
549 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
550 return -1;
551 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000552
kwiberg55b97fe2016-01-28 05:22:45 -0800553 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000554}
555
kwiberg55b97fe2016-01-28 05:22:45 -0800556int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
557 size_t payloadSize,
558 const WebRtcRTPHeader* rtpHeader) {
559 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
560 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
561 ","
562 " payloadType=%u, audioChannel=%" PRIuS ")",
563 payloadSize, rtpHeader->header.payloadType,
564 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000565
kwiberg55b97fe2016-01-28 05:22:45 -0800566 if (!channel_state_.Get().playing) {
567 // Avoid inserting into NetEQ when we are not playing. Count the
568 // packet as discarded.
569 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
570 "received packet is discarded since playing is not"
571 " activated");
niklase@google.com470e71d2011-07-07 08:21:25 +0000572 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800573 }
574
575 // Push the incoming payload (parsed and ready for decoding) into the ACM
576 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
577 0) {
578 _engineStatisticsPtr->SetLastError(
579 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
580 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
581 return -1;
582 }
583
kwiberg55b97fe2016-01-28 05:22:45 -0800584 int64_t round_trip_time = 0;
585 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
586 NULL);
587
588 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
589 if (!nack_list.empty()) {
590 // Can't use nack_list.data() since it's not supported by all
591 // compilers.
592 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
593 }
594 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000595}
596
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000597bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000598 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000599 RTPHeader header;
600 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
601 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
602 "IncomingPacket invalid RTP header");
603 return false;
604 }
605 header.payload_type_frequency =
606 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
607 if (header.payload_type_frequency < 0)
608 return false;
609 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
610}
611
henrik.lundin42dda502016-05-18 05:36:01 -0700612MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
613 int32_t id,
614 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700615 unsigned int ssrc;
nisse7d59f6b2017-02-21 03:40:24 -0800616 RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
ivoc14d5dbe2016-07-04 07:06:55 -0700617 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800618 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700619 bool muted;
620 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
621 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800622 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
623 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
624 // In all likelihood, the audio in this frame is garbage. We return an
625 // error so that the audio mixer module doesn't add it to the mix. As
626 // a result, it won't be played out and the actions skipped here are
627 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700628 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800629 }
henrik.lundina89ab962016-05-18 08:52:45 -0700630
631 if (muted) {
632 // TODO(henrik.lundin): We should be able to do better than this. But we
633 // will have to go through all the cases below where the audio samples may
634 // be used, and handle the muted case in some way.
aleloi6321b492016-12-05 01:46:09 -0800635 AudioFrameOperations::Mute(audioFrame);
henrik.lundina89ab962016-05-18 08:52:45 -0700636 }
kwiberg55b97fe2016-01-28 05:22:45 -0800637
kwiberg55b97fe2016-01-28 05:22:45 -0800638 // Convert module ID to internal VoE channel ID
639 audioFrame->id_ = VoEChannelId(audioFrame->id_);
640 // Store speech type for dead-or-alive detection
641 _outputSpeechType = audioFrame->speech_type_;
642
643 ChannelState::State state = channel_state_.Get();
644
kwiberg55b97fe2016-01-28 05:22:45 -0800645 {
646 // Pass the audio buffers to an optional sink callback, before applying
647 // scaling/panning, as that applies to the mix operation.
648 // External recipients of the audio (e.g. via AudioTrack), will do their
649 // own mixing/dynamic processing.
650 rtc::CritScope cs(&_callbackCritSect);
651 if (audio_sink_) {
652 AudioSinkInterface::Data data(
653 &audioFrame->data_[0], audioFrame->samples_per_channel_,
654 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
655 audioFrame->timestamp_);
656 audio_sink_->OnData(data);
657 }
658 }
659
660 float output_gain = 1.0f;
kwiberg55b97fe2016-01-28 05:22:45 -0800661 {
662 rtc::CritScope cs(&volume_settings_critsect_);
663 output_gain = _outputGain;
kwiberg55b97fe2016-01-28 05:22:45 -0800664 }
665
666 // Output volume scaling
667 if (output_gain < 0.99f || output_gain > 1.01f) {
solenberg8d73f8c2017-03-08 01:52:20 -0800668 // TODO(solenberg): Combine with mute state - this can cause clicks!
oprypin67fdb802017-03-09 06:25:06 -0800669 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800670 }
671
kwiberg55b97fe2016-01-28 05:22:45 -0800672 // Mix decoded PCM output with file if file mixing is enabled
673 if (state.output_file_playing) {
674 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700675 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800676 }
677
kwiberg55b97fe2016-01-28 05:22:45 -0800678 // Record playout if enabled
679 {
680 rtc::CritScope cs(&_fileCritSect);
681
kwiberg5a25d952016-08-17 07:31:12 -0700682 if (_outputFileRecording && output_file_recorder_) {
683 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800684 }
685 }
686
687 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700688 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800689 _outputAudioLevel.ComputeLevel(*audioFrame);
690
691 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
692 // The first frame with a valid rtp timestamp.
693 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
694 }
695
696 if (capture_start_rtp_time_stamp_ >= 0) {
697 // audioFrame.timestamp_ should be valid from now on.
698
699 // Compute elapsed time.
700 int64_t unwrap_timestamp =
701 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
702 audioFrame->elapsed_time_ms_ =
703 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700704 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800705
niklase@google.com470e71d2011-07-07 08:21:25 +0000706 {
kwiberg55b97fe2016-01-28 05:22:45 -0800707 rtc::CritScope lock(&ts_stats_lock_);
708 // Compute ntp time.
709 audioFrame->ntp_time_ms_ =
710 ntp_estimator_.Estimate(audioFrame->timestamp_);
711 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
712 if (audioFrame->ntp_time_ms_ > 0) {
713 // Compute |capture_start_ntp_time_ms_| so that
714 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
715 capture_start_ntp_time_ms_ =
716 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000717 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000718 }
kwiberg55b97fe2016-01-28 05:22:45 -0800719 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000720
henrik.lundin42dda502016-05-18 05:36:01 -0700721 return muted ? MixerParticipant::AudioFrameInfo::kMuted
722 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000723}
724
aleloi6c278492016-10-20 14:24:39 -0700725AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
726 int sample_rate_hz,
727 AudioFrame* audio_frame) {
728 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700729
aleloi6c278492016-10-20 14:24:39 -0700730 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700731
732 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
733 FrameInfo new_audio_frame_info = FrameInfo::kError;
734 switch (frame_info) {
735 case MixerParticipant::AudioFrameInfo::kNormal:
736 new_audio_frame_info = FrameInfo::kNormal;
737 break;
738 case MixerParticipant::AudioFrameInfo::kMuted:
739 new_audio_frame_info = FrameInfo::kMuted;
740 break;
741 case MixerParticipant::AudioFrameInfo::kError:
742 new_audio_frame_info = FrameInfo::kError;
743 break;
744 }
aleloi6c278492016-10-20 14:24:39 -0700745 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700746}
747
kwiberg55b97fe2016-01-28 05:22:45 -0800748int32_t Channel::NeededFrequency(int32_t id) const {
749 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
750 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000751
kwiberg55b97fe2016-01-28 05:22:45 -0800752 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000753
kwiberg55b97fe2016-01-28 05:22:45 -0800754 // Determine highest needed receive frequency
755 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000756
kwiberg55b97fe2016-01-28 05:22:45 -0800757 // Return the bigger of playout and receive frequency in the ACM.
758 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
759 highestNeeded = audio_coding_->PlayoutFrequency();
760 } else {
761 highestNeeded = receiveFrequency;
762 }
763
764 // Special case, if we're playing a file on the playout side
765 // we take that frequency into consideration as well
766 // This is not needed on sending side, since the codec will
767 // limit the spectrum anyway.
768 if (channel_state_.Get().output_file_playing) {
769 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700770 if (output_file_player_) {
771 if (output_file_player_->Frequency() > highestNeeded) {
772 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800773 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000774 }
kwiberg55b97fe2016-01-28 05:22:45 -0800775 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000776
kwiberg55b97fe2016-01-28 05:22:45 -0800777 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000778}
779
ossu5f7cfa52016-05-30 08:11:28 -0700780int32_t Channel::CreateChannel(
781 Channel*& channel,
782 int32_t channelId,
783 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700784 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800785 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
786 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
787 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000788
solenberg88499ec2016-09-07 07:34:41 -0700789 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800790 if (channel == NULL) {
791 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
792 "Channel::CreateChannel() unable to allocate memory for"
793 " channel");
794 return -1;
795 }
796 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000797}
798
kwiberg55b97fe2016-01-28 05:22:45 -0800799void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
800 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
801 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
802 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000803
kwiberg55b97fe2016-01-28 05:22:45 -0800804 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000805}
806
kwiberg55b97fe2016-01-28 05:22:45 -0800807void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
808 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
809 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
810 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000811
kwiberg55b97fe2016-01-28 05:22:45 -0800812 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000813}
814
kwiberg55b97fe2016-01-28 05:22:45 -0800815void Channel::PlayFileEnded(int32_t id) {
816 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
817 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000818
kwiberg55b97fe2016-01-28 05:22:45 -0800819 if (id == _inputFilePlayerId) {
820 channel_state_.SetInputFilePlaying(false);
821 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
822 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000823 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800824 } else if (id == _outputFilePlayerId) {
825 channel_state_.SetOutputFilePlaying(false);
826 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
827 "Channel::PlayFileEnded() => output file player module is"
828 " shutdown");
829 }
830}
831
832void Channel::RecordFileEnded(int32_t id) {
833 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
834 "Channel::RecordFileEnded(id=%d)", id);
835
836 assert(id == _outputFileRecorderId);
837
838 rtc::CritScope cs(&_fileCritSect);
839
840 _outputFileRecording = false;
841 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
842 "Channel::RecordFileEnded() => output file recorder module is"
843 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000844}
845
pbos@webrtc.org92135212013-05-14 08:31:39 +0000846Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000847 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700848 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800849 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100850 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700851 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800852 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100853 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800854 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100855 rtp_receive_statistics_(
856 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
857 rtp_receiver_(
858 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100859 this,
860 this,
861 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700862 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100863 _outputAudioLevel(),
864 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100865 // Avoid conflict with other channels by adding 1024 - 1026,
866 // won't use as much as 1024 channels.
867 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
868 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
869 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
870 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100871 _timeStamp(0), // This is just an offset, RTP module will add it's own
872 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100873 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100874 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100875 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100876 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100877 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
878 capture_start_rtp_time_stamp_(-1),
879 capture_start_ntp_time_ms_(-1),
880 _engineStatisticsPtr(NULL),
881 _outputMixerPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100882 _moduleProcessThreadPtr(NULL),
883 _audioDeviceModulePtr(NULL),
884 _voiceEngineObserverPtr(NULL),
885 _callbackCritSectPtr(NULL),
886 _transportPtr(NULL),
solenberg1c2af8e2016-03-24 10:36:00 -0700887 input_mute_(false),
888 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100889 _outputGain(1.0f),
solenberg8d73f8c2017-03-08 01:52:20 -0800890 _mixFileWithMicrophone(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100891 _lastLocalTimeStamp(0),
892 _lastPayloadType(0),
893 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800894 transport_overhead_per_packet_(0),
895 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100896 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100897 restored_packet_in_use_(false),
898 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100899 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700900 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800901 feedback_observer_proxy_(new TransportFeedbackProxy()),
902 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700903 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200904 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
905 kMaxRetransmissionWindowMs)),
elad.alond12a8e12017-03-23 11:04:48 -0700906 decoder_factory_(config.acm_config.decoder_factory),
907 // TODO(elad.alon): Subsequent CL experiments with PLR source.
908 use_twcc_plr_for_ana_(false) {
kwiberg55b97fe2016-01-28 05:22:45 -0800909 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
910 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700911 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800912 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700913 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800914 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200915
kwiberg55b97fe2016-01-28 05:22:45 -0800916 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000917
kwiberg55b97fe2016-01-28 05:22:45 -0800918 RtpRtcp::Configuration configuration;
919 configuration.audio = true;
920 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800921 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800922 configuration.receive_statistics = rtp_receive_statistics_.get();
923 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800924 if (pacing_enabled_) {
925 configuration.paced_sender = rtp_packet_sender_proxy_.get();
926 configuration.transport_sequence_number_allocator =
927 seq_num_allocator_proxy_.get();
928 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
929 }
ivoc14d5dbe2016-07-04 07:06:55 -0700930 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800931 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200932 configuration.retransmission_rate_limiter =
933 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000934
kwiberg55b97fe2016-01-28 05:22:45 -0800935 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100936 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000937}
938
kwiberg55b97fe2016-01-28 05:22:45 -0800939Channel::~Channel() {
tommi0a2391f2017-03-21 02:31:51 -0700940 RTC_DCHECK(!channel_state_.Get().sending);
941 RTC_DCHECK(!channel_state_.Get().playing);
niklase@google.com470e71d2011-07-07 08:21:25 +0000942}
943
kwiberg55b97fe2016-01-28 05:22:45 -0800944int32_t Channel::Init() {
tommi0a2391f2017-03-21 02:31:51 -0700945 RTC_DCHECK(construction_thread_.CalledOnValidThread());
kwiberg55b97fe2016-01-28 05:22:45 -0800946 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
947 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000948
kwiberg55b97fe2016-01-28 05:22:45 -0800949 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000950
kwiberg55b97fe2016-01-28 05:22:45 -0800951 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000952
kwiberg55b97fe2016-01-28 05:22:45 -0800953 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
954 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
955 "Channel::Init() must call SetEngineInformation() first");
956 return -1;
957 }
958
959 // --- Add modules to process thread (for periodic schedulation)
960
tommidea489f2017-03-03 03:20:24 -0800961 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
kwiberg55b97fe2016-01-28 05:22:45 -0800962
963 // --- ACM initialization
964
965 if (audio_coding_->InitializeReceiver() == -1) {
966 _engineStatisticsPtr->SetLastError(
967 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
968 "Channel::Init() unable to initialize the ACM - 1");
969 return -1;
970 }
971
972 // --- RTP/RTCP module initialization
973
974 // Ensure that RTCP is enabled by default for the created channel.
975 // Note that, the module will keep generating RTCP until it is explicitly
976 // disabled by the user.
977 // After StopListen (when no sockets exists), RTCP packets will no longer
978 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -0700979 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800980 // RTCP is enabled by default.
981 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
982 // --- Register all permanent callbacks
solenbergfe7dd6d2017-03-11 08:10:43 -0800983 if (audio_coding_->RegisterTransportCallback(this) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800984 _engineStatisticsPtr->SetLastError(
985 VE_CANNOT_INIT_CHANNEL, kTraceError,
986 "Channel::Init() callbacks not registered");
987 return -1;
988 }
989
990 // --- Register all supported codecs to the receiving side of the
991 // RTP/RTCP module
992
993 CodecInst codec;
994 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
995
996 for (int idx = 0; idx < nSupportedCodecs; idx++) {
997 // Open up the RTP/RTCP receiver for all supported codecs
998 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -0800999 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001000 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1001 "Channel::Init() unable to register %s "
1002 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1003 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1004 codec.rate);
1005 } else {
1006 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1007 "Channel::Init() %s (%d/%d/%" PRIuS
1008 "/%d) has been "
1009 "added to the RTP/RTCP receiver",
1010 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1011 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001012 }
1013
kwiberg55b97fe2016-01-28 05:22:45 -08001014 // Ensure that PCMU is used as default codec on the sending side
1015 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1016 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001017 }
1018
kwiberg55b97fe2016-01-28 05:22:45 -08001019 // Register default PT for outband 'telephone-event'
1020 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001021 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001022 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1023 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001024 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1025 "Channel::Init() failed to register outband "
1026 "'telephone-event' (%d/%d) correctly",
1027 codec.pltype, codec.plfreq);
1028 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001029 }
1030
kwiberg55b97fe2016-01-28 05:22:45 -08001031 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001032 if (!codec_manager_.RegisterEncoder(codec) ||
1033 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001034 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1035 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001036 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001037 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1038 "Channel::Init() failed to register CN (%d/%d) "
1039 "correctly - 1",
1040 codec.pltype, codec.plfreq);
1041 }
1042 }
kwiberg55b97fe2016-01-28 05:22:45 -08001043 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001044
kwiberg55b97fe2016-01-28 05:22:45 -08001045 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001046}
1047
tommi0a2391f2017-03-21 02:31:51 -07001048void Channel::Terminate() {
1049 RTC_DCHECK(construction_thread_.CalledOnValidThread());
1050 // Must be called on the same thread as Init().
1051 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
1052 "Channel::Terminate");
1053
1054 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
1055
1056 StopSend();
1057 StopPlayout();
1058
1059 {
1060 rtc::CritScope cs(&_fileCritSect);
1061 if (input_file_player_) {
1062 input_file_player_->RegisterModuleFileCallback(NULL);
1063 input_file_player_->StopPlayingFile();
1064 }
1065 if (output_file_player_) {
1066 output_file_player_->RegisterModuleFileCallback(NULL);
1067 output_file_player_->StopPlayingFile();
1068 }
1069 if (output_file_recorder_) {
1070 output_file_recorder_->RegisterModuleFileCallback(NULL);
1071 output_file_recorder_->StopRecording();
1072 }
1073 }
1074
1075 // The order to safely shutdown modules in a channel is:
1076 // 1. De-register callbacks in modules
1077 // 2. De-register modules in process thread
1078 // 3. Destroy modules
1079 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
1080 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1081 "Terminate() failed to de-register transport callback"
1082 " (Audio coding module)");
1083 }
1084
1085 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
1086 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1087 "Terminate() failed to de-register VAD callback"
1088 " (Audio coding module)");
1089 }
1090
1091 // De-register modules in process thread
1092 if (_moduleProcessThreadPtr)
1093 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
1094
1095 // End of modules shutdown
1096}
1097
kwiberg55b97fe2016-01-28 05:22:45 -08001098int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1099 OutputMixer& outputMixer,
kwiberg55b97fe2016-01-28 05:22:45 -08001100 ProcessThread& moduleProcessThread,
1101 AudioDeviceModule& audioDeviceModule,
1102 VoiceEngineObserver* voiceEngineObserver,
1103 rtc::CriticalSection* callbackCritSect) {
1104 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1105 "Channel::SetEngineInformation()");
1106 _engineStatisticsPtr = &engineStatistics;
1107 _outputMixerPtr = &outputMixer;
kwiberg55b97fe2016-01-28 05:22:45 -08001108 _moduleProcessThreadPtr = &moduleProcessThread;
1109 _audioDeviceModulePtr = &audioDeviceModule;
1110 _voiceEngineObserverPtr = voiceEngineObserver;
1111 _callbackCritSectPtr = callbackCritSect;
1112 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001113}
1114
kwiberg55b97fe2016-01-28 05:22:45 -08001115int32_t Channel::UpdateLocalTimeStamp() {
1116 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1117 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001118}
1119
kwibergb7f89d62016-02-17 10:04:18 -08001120void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001121 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001122 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001123}
1124
ossu29b1a8d2016-06-13 07:34:51 -07001125const rtc::scoped_refptr<AudioDecoderFactory>&
1126Channel::GetAudioDecoderFactory() const {
1127 return decoder_factory_;
1128}
1129
kwiberg55b97fe2016-01-28 05:22:45 -08001130int32_t Channel::StartPlayout() {
1131 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1132 "Channel::StartPlayout()");
1133 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001134 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001135 }
1136
solenberge374e012017-02-14 04:55:00 -08001137 // Add participant as candidates for mixing.
1138 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1139 _engineStatisticsPtr->SetLastError(
1140 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1141 "StartPlayout() failed to add participant to mixer");
1142 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001143 }
1144
1145 channel_state_.SetPlaying(true);
1146 if (RegisterFilePlayingToMixer() != 0)
1147 return -1;
1148
1149 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001150}
1151
kwiberg55b97fe2016-01-28 05:22:45 -08001152int32_t Channel::StopPlayout() {
1153 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1154 "Channel::StopPlayout()");
1155 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001156 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001157 }
1158
solenberge374e012017-02-14 04:55:00 -08001159 // Remove participant as candidates for mixing
1160 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1161 _engineStatisticsPtr->SetLastError(
1162 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1163 "StopPlayout() failed to remove participant from mixer");
1164 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001165 }
1166
1167 channel_state_.SetPlaying(false);
1168 _outputAudioLevel.Clear();
1169
1170 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001171}
1172
kwiberg55b97fe2016-01-28 05:22:45 -08001173int32_t Channel::StartSend() {
1174 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1175 "Channel::StartSend()");
kwiberg55b97fe2016-01-28 05:22:45 -08001176 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001177 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001178 }
1179 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001180
solenberg08b19df2017-02-15 00:42:31 -08001181 // Resume the previous sequence number which was reset by StopSend(). This
1182 // needs to be done before |sending| is set to true on the RTP/RTCP module.
1183 if (send_sequence_number_) {
1184 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
1185 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001186 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001187 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1188 _engineStatisticsPtr->SetLastError(
1189 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1190 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001191 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001192 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001193 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001194 return -1;
1195 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001196
kwiberg55b97fe2016-01-28 05:22:45 -08001197 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001198}
1199
kwiberg55b97fe2016-01-28 05:22:45 -08001200int32_t Channel::StopSend() {
1201 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1202 "Channel::StopSend()");
1203 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001204 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001205 }
1206 channel_state_.SetSending(false);
1207
1208 // Store the sequence number to be able to pick up the same sequence for
1209 // the next StartSend(). This is needed for restarting device, otherwise
1210 // it might cause libSRTP to complain about packets being replayed.
1211 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1212 // CL is landed. See issue
1213 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1214 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1215
1216 // Reset sending SSRC and sequence number and triggers direct transmission
1217 // of RTCP BYE
1218 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1219 _engineStatisticsPtr->SetLastError(
1220 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1221 "StartSend() RTP/RTCP failed to stop sending");
1222 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001223 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001224
1225 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001226}
1227
kwiberg55b97fe2016-01-28 05:22:45 -08001228int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1229 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1230 "Channel::RegisterVoiceEngineObserver()");
1231 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001232
kwiberg55b97fe2016-01-28 05:22:45 -08001233 if (_voiceEngineObserverPtr) {
1234 _engineStatisticsPtr->SetLastError(
1235 VE_INVALID_OPERATION, kTraceError,
1236 "RegisterVoiceEngineObserver() observer already enabled");
1237 return -1;
1238 }
1239 _voiceEngineObserverPtr = &observer;
1240 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001241}
1242
kwiberg55b97fe2016-01-28 05:22:45 -08001243int32_t Channel::DeRegisterVoiceEngineObserver() {
1244 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1245 "Channel::DeRegisterVoiceEngineObserver()");
1246 rtc::CritScope cs(&_callbackCritSect);
1247
1248 if (!_voiceEngineObserverPtr) {
1249 _engineStatisticsPtr->SetLastError(
1250 VE_INVALID_OPERATION, kTraceWarning,
1251 "DeRegisterVoiceEngineObserver() observer already disabled");
1252 return 0;
1253 }
1254 _voiceEngineObserverPtr = NULL;
1255 return 0;
1256}
1257
1258int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001259 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001260 if (send_codec) {
1261 codec = *send_codec;
1262 return 0;
1263 }
1264 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001265}
1266
kwiberg55b97fe2016-01-28 05:22:45 -08001267int32_t Channel::GetRecCodec(CodecInst& codec) {
1268 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001269}
1270
kwiberg55b97fe2016-01-28 05:22:45 -08001271int32_t Channel::SetSendCodec(const CodecInst& codec) {
1272 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1273 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001274
kwibergc8d071e2016-04-06 12:22:38 -07001275 if (!codec_manager_.RegisterEncoder(codec) ||
1276 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001277 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1278 "SetSendCodec() failed to register codec to ACM");
1279 return -1;
1280 }
1281
1282 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1283 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1284 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1285 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1286 "SetSendCodec() failed to register codec to"
1287 " RTP/RTCP module");
1288 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001289 }
kwiberg55b97fe2016-01-28 05:22:45 -08001290 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001291
kwiberg55b97fe2016-01-28 05:22:45 -08001292 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001293}
1294
minyue78b4d562016-11-30 04:47:39 -08001295void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001296 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1297 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001298 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001299 if (*encoder) {
1300 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001301 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001302 }
1303 });
michaelt566d8202017-01-12 10:17:38 -08001304 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001305}
1306
elad.alond12a8e12017-03-23 11:04:48 -07001307void Channel::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
1308 if (!use_twcc_plr_for_ana_)
1309 return;
minyue7e304322016-10-12 05:00:55 -07001310 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
elad.alond12a8e12017-03-23 11:04:48 -07001311 if (*encoder) {
1312 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1313 }
1314 });
1315}
1316
1317void Channel::OnUplinkPacketLossRate(float packet_loss_rate) {
1318 if (use_twcc_plr_for_ana_)
1319 return;
1320 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1321 if (*encoder) {
1322 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1323 }
minyue7e304322016-10-12 05:00:55 -07001324 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001325}
1326
kwiberg55b97fe2016-01-28 05:22:45 -08001327int32_t Channel::SetVADStatus(bool enableVAD,
1328 ACMVADMode mode,
1329 bool disableDTX) {
1330 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1331 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001332 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1333 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1334 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001335 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1336 kTraceError,
1337 "SetVADStatus() failed to set VAD");
1338 return -1;
1339 }
1340 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001341}
1342
kwiberg55b97fe2016-01-28 05:22:45 -08001343int32_t Channel::GetVADStatus(bool& enabledVAD,
1344 ACMVADMode& mode,
1345 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001346 const auto* params = codec_manager_.GetStackParams();
1347 enabledVAD = params->use_cng;
1348 mode = params->vad_mode;
1349 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001350 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001351}
1352
kwiberg55b97fe2016-01-28 05:22:45 -08001353int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001354 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1355}
1356
1357int32_t Channel::SetRecPayloadType(int payload_type,
1358 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001359 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1360 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001361
kwiberg55b97fe2016-01-28 05:22:45 -08001362 if (channel_state_.Get().playing) {
1363 _engineStatisticsPtr->SetLastError(
1364 VE_ALREADY_PLAYING, kTraceError,
1365 "SetRecPayloadType() unable to set PT while playing");
1366 return -1;
1367 }
kwiberg55b97fe2016-01-28 05:22:45 -08001368
kwiberg09f090c2017-03-01 01:57:11 -08001369 const CodecInst codec = SdpToCodecInst(payload_type, format);
kwibergd32bf752017-01-19 07:03:59 -08001370
1371 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001372 // De-register the selected codec (RTP/RTCP module and ACM)
1373
1374 int8_t pltype(-1);
1375 CodecInst rxCodec = codec;
1376
1377 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001378 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001379 rxCodec.pltype = pltype;
1380
1381 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1382 _engineStatisticsPtr->SetLastError(
1383 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1384 "SetRecPayloadType() RTP/RTCP-module deregistration "
1385 "failed");
1386 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001387 }
kwiberg55b97fe2016-01-28 05:22:45 -08001388 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1389 _engineStatisticsPtr->SetLastError(
1390 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1391 "SetRecPayloadType() ACM deregistration failed - 1");
1392 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001393 }
kwiberg55b97fe2016-01-28 05:22:45 -08001394 return 0;
1395 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001396
magjed56124bd2016-11-24 09:34:46 -08001397 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001398 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001399 // TODO(kwiberg): Retrying is probably not necessary, since
1400 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001401 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001402 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001403 _engineStatisticsPtr->SetLastError(
1404 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1405 "SetRecPayloadType() RTP/RTCP-module registration failed");
1406 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001407 }
kwiberg55b97fe2016-01-28 05:22:45 -08001408 }
kwibergd32bf752017-01-19 07:03:59 -08001409 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1410 audio_coding_->UnregisterReceiveCodec(payload_type);
1411 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001412 _engineStatisticsPtr->SetLastError(
1413 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1414 "SetRecPayloadType() ACM registration failed - 1");
1415 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001416 }
kwiberg55b97fe2016-01-28 05:22:45 -08001417 }
1418 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001419}
1420
kwiberg55b97fe2016-01-28 05:22:45 -08001421int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1422 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001423 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001424 _engineStatisticsPtr->SetLastError(
1425 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1426 "GetRecPayloadType() failed to retrieve RX payload type");
1427 return -1;
1428 }
1429 codec.pltype = payloadType;
1430 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001431}
1432
kwiberg55b97fe2016-01-28 05:22:45 -08001433int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1434 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1435 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001436
kwiberg55b97fe2016-01-28 05:22:45 -08001437 CodecInst codec;
1438 int32_t samplingFreqHz(-1);
1439 const size_t kMono = 1;
1440 if (frequency == kFreq32000Hz)
1441 samplingFreqHz = 32000;
1442 else if (frequency == kFreq16000Hz)
1443 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001444
kwiberg55b97fe2016-01-28 05:22:45 -08001445 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1446 _engineStatisticsPtr->SetLastError(
1447 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1448 "SetSendCNPayloadType() failed to retrieve default CN codec "
1449 "settings");
1450 return -1;
1451 }
1452
1453 // Modify the payload type (must be set to dynamic range)
1454 codec.pltype = type;
1455
kwibergc8d071e2016-04-06 12:22:38 -07001456 if (!codec_manager_.RegisterEncoder(codec) ||
1457 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001458 _engineStatisticsPtr->SetLastError(
1459 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1460 "SetSendCNPayloadType() failed to register CN to ACM");
1461 return -1;
1462 }
1463
1464 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1465 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1466 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1467 _engineStatisticsPtr->SetLastError(
1468 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1469 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1470 "module");
1471 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001472 }
kwiberg55b97fe2016-01-28 05:22:45 -08001473 }
1474 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001475}
1476
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001477int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001478 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001479 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001480
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001481 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001482 _engineStatisticsPtr->SetLastError(
1483 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001484 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001485 return -1;
1486 }
1487 return 0;
1488}
1489
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001490int Channel::SetOpusDtx(bool enable_dtx) {
1491 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1492 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001493 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001494 : audio_coding_->DisableOpusDtx();
1495 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001496 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1497 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001498 return -1;
1499 }
1500 return 0;
1501}
1502
ivoc85228d62016-07-27 04:53:47 -07001503int Channel::GetOpusDtx(bool* enabled) {
1504 int success = -1;
1505 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1506 if (encoder) {
1507 *enabled = encoder->GetDtx();
1508 success = 0;
1509 }
1510 });
1511 return success;
1512}
1513
minyue7e304322016-10-12 05:00:55 -07001514bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1515 bool success = false;
1516 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1517 if (*encoder) {
1518 success = (*encoder)->EnableAudioNetworkAdaptor(
michaeltbf279fc2017-01-13 06:02:29 -08001519 config_string, event_log_proxy_.get(), Clock::GetRealTimeClock());
minyue7e304322016-10-12 05:00:55 -07001520 }
1521 });
1522 return success;
1523}
1524
1525void Channel::DisableAudioNetworkAdaptor() {
1526 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1527 if (*encoder)
1528 (*encoder)->DisableAudioNetworkAdaptor();
1529 });
1530}
1531
1532void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1533 int max_frame_length_ms) {
1534 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1535 if (*encoder) {
1536 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1537 max_frame_length_ms);
1538 }
1539 });
1540}
1541
mflodman3d7db262016-04-29 00:57:13 -07001542int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001543 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001544 "Channel::RegisterExternalTransport()");
1545
kwiberg55b97fe2016-01-28 05:22:45 -08001546 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001547 if (_externalTransport) {
1548 _engineStatisticsPtr->SetLastError(
1549 VE_INVALID_OPERATION, kTraceError,
1550 "RegisterExternalTransport() external transport already enabled");
1551 return -1;
1552 }
1553 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001554 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001555 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001556}
1557
kwiberg55b97fe2016-01-28 05:22:45 -08001558int32_t Channel::DeRegisterExternalTransport() {
1559 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1560 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001561
kwiberg55b97fe2016-01-28 05:22:45 -08001562 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001563 if (_transportPtr) {
1564 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1565 "DeRegisterExternalTransport() all transport is disabled");
1566 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001567 _engineStatisticsPtr->SetLastError(
1568 VE_INVALID_OPERATION, kTraceWarning,
1569 "DeRegisterExternalTransport() external transport already "
1570 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001571 }
1572 _externalTransport = false;
1573 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001574 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001575}
1576
nisse657bab22017-02-21 06:28:10 -08001577// TODO(nisse): Delete this method together with ReceivedRTPPacket.
1578// It's a temporary hack to support both ReceivedRTPPacket and
1579// OnRtpPacket interfaces without too much code duplication.
1580bool Channel::OnRtpPacketWithHeader(const uint8_t* received_packet,
1581 size_t length,
1582 RTPHeader *header) {
1583 // Store playout timestamp for the received RTP packet
1584 UpdatePlayoutTimestamp(false);
1585
1586 header->payload_type_frequency =
1587 rtp_payload_registry_->GetPayloadTypeFrequency(header->payloadType);
1588 if (header->payload_type_frequency < 0)
1589 return false;
1590 bool in_order = IsPacketInOrder(*header);
1591 rtp_receive_statistics_->IncomingPacket(
1592 *header, length, IsPacketRetransmitted(*header, in_order));
1593 rtp_payload_registry_->SetIncomingPayloadType(*header);
1594
1595 return ReceivePacket(received_packet, length, *header, in_order);
1596}
1597
mflodman3d7db262016-04-29 00:57:13 -07001598int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001599 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001600 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001601 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001602 "Channel::ReceivedRTPPacket()");
1603
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001604 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001605 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1606 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1607 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001608 return -1;
1609 }
nisse657bab22017-02-21 06:28:10 -08001610 return OnRtpPacketWithHeader(received_packet, length, &header) ? 0 : -1;
1611}
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001612
nisse657bab22017-02-21 06:28:10 -08001613void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
1614 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
1615 "Channel::ReceivedRTPPacket()");
1616
1617 RTPHeader header;
1618 packet.GetHeader(&header);
1619 OnRtpPacketWithHeader(packet.data(), packet.size(), &header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001620}
1621
1622bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001623 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001624 const RTPHeader& header,
1625 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001626 if (rtp_payload_registry_->IsRtx(header)) {
1627 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001628 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001629 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001630 assert(packet_length >= header.headerLength);
1631 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001632 PayloadUnion payload_specific;
1633 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001634 &payload_specific)) {
1635 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001636 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001637 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1638 payload_specific, in_order);
1639}
1640
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001641bool Channel::HandleRtxPacket(const uint8_t* packet,
1642 size_t packet_length,
1643 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001644 if (!rtp_payload_registry_->IsRtx(header))
1645 return false;
1646
1647 // Remove the RTX header and parse the original RTP header.
1648 if (packet_length < header.headerLength)
1649 return false;
1650 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1651 return false;
1652 if (restored_packet_in_use_) {
1653 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1654 "Multiple RTX headers detected, dropping packet");
1655 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001656 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001657 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001658 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1659 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001660 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1661 "Incoming RTX packet: invalid RTP header");
1662 return false;
1663 }
1664 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001665 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001666 restored_packet_in_use_ = false;
1667 return ret;
1668}
1669
1670bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1671 StreamStatistician* statistician =
1672 rtp_receive_statistics_->GetStatistician(header.ssrc);
1673 if (!statistician)
1674 return false;
1675 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001676}
1677
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001678bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1679 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001680 // Retransmissions are handled separately if RTX is enabled.
1681 if (rtp_payload_registry_->RtxEnabled())
1682 return false;
1683 StreamStatistician* statistician =
1684 rtp_receive_statistics_->GetStatistician(header.ssrc);
1685 if (!statistician)
1686 return false;
1687 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001688 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001689 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001690 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001691}
1692
mflodman3d7db262016-04-29 00:57:13 -07001693int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001694 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001695 "Channel::ReceivedRTCPPacket()");
1696 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001697 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001698
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001699 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001700 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001701 _engineStatisticsPtr->SetLastError(
1702 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1703 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1704 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001705
Minyue2013aec2015-05-13 14:14:42 +02001706 int64_t rtt = GetRTT(true);
1707 if (rtt == 0) {
1708 // Waiting for valid RTT.
1709 return 0;
1710 }
Erik Språng737336d2016-07-29 12:59:36 +02001711
1712 int64_t nack_window_ms = rtt;
1713 if (nack_window_ms < kMinRetransmissionWindowMs) {
1714 nack_window_ms = kMinRetransmissionWindowMs;
1715 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1716 nack_window_ms = kMaxRetransmissionWindowMs;
1717 }
1718 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1719
minyue7e304322016-10-12 05:00:55 -07001720 // Invoke audio encoders OnReceivedRtt().
1721 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1722 if (*encoder)
1723 (*encoder)->OnReceivedRtt(rtt);
1724 });
1725
Minyue2013aec2015-05-13 14:14:42 +02001726 uint32_t ntp_secs = 0;
1727 uint32_t ntp_frac = 0;
1728 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001729 if (0 !=
1730 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1731 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001732 // Waiting for RTCP.
1733 return 0;
1734 }
1735
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001736 {
tommi31fc21f2016-01-21 10:37:37 -08001737 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001738 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001739 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001740 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001741}
1742
niklase@google.com470e71d2011-07-07 08:21:25 +00001743int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001744 bool loop,
1745 FileFormats format,
1746 int startPosition,
1747 float volumeScaling,
1748 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001749 const CodecInst* codecInst) {
1750 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1751 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1752 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1753 "stopPosition=%d)",
1754 fileName, loop, format, volumeScaling, startPosition,
1755 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001756
kwiberg55b97fe2016-01-28 05:22:45 -08001757 if (channel_state_.Get().output_file_playing) {
1758 _engineStatisticsPtr->SetLastError(
1759 VE_ALREADY_PLAYING, kTraceError,
1760 "StartPlayingFileLocally() is already playing");
1761 return -1;
1762 }
1763
1764 {
1765 rtc::CritScope cs(&_fileCritSect);
1766
kwiberg5a25d952016-08-17 07:31:12 -07001767 if (output_file_player_) {
1768 output_file_player_->RegisterModuleFileCallback(NULL);
1769 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001770 }
1771
kwiberg5b356f42016-09-08 04:32:33 -07001772 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001773 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001774
kwiberg5a25d952016-08-17 07:31:12 -07001775 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001776 _engineStatisticsPtr->SetLastError(
1777 VE_INVALID_ARGUMENT, kTraceError,
1778 "StartPlayingFileLocally() filePlayer format is not correct");
1779 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001780 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001781
kwiberg55b97fe2016-01-28 05:22:45 -08001782 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001783
kwiberg5a25d952016-08-17 07:31:12 -07001784 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001785 fileName, loop, startPosition, volumeScaling, notificationTime,
1786 stopPosition, (const CodecInst*)codecInst) != 0) {
1787 _engineStatisticsPtr->SetLastError(
1788 VE_BAD_FILE, kTraceError,
1789 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001790 output_file_player_->StopPlayingFile();
1791 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001792 return -1;
1793 }
kwiberg5a25d952016-08-17 07:31:12 -07001794 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001795 channel_state_.SetOutputFilePlaying(true);
1796 }
1797
1798 if (RegisterFilePlayingToMixer() != 0)
1799 return -1;
1800
1801 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001802}
1803
1804int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001805 FileFormats format,
1806 int startPosition,
1807 float volumeScaling,
1808 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001809 const CodecInst* codecInst) {
1810 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1811 "Channel::StartPlayingFileLocally(format=%d,"
1812 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1813 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001814
kwiberg55b97fe2016-01-28 05:22:45 -08001815 if (stream == NULL) {
1816 _engineStatisticsPtr->SetLastError(
1817 VE_BAD_FILE, kTraceError,
1818 "StartPlayingFileLocally() NULL as input stream");
1819 return -1;
1820 }
1821
1822 if (channel_state_.Get().output_file_playing) {
1823 _engineStatisticsPtr->SetLastError(
1824 VE_ALREADY_PLAYING, kTraceError,
1825 "StartPlayingFileLocally() is already playing");
1826 return -1;
1827 }
1828
1829 {
1830 rtc::CritScope cs(&_fileCritSect);
1831
1832 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001833 if (output_file_player_) {
1834 output_file_player_->RegisterModuleFileCallback(NULL);
1835 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001836 }
1837
kwiberg55b97fe2016-01-28 05:22:45 -08001838 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001839 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001840 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001841
kwiberg5a25d952016-08-17 07:31:12 -07001842 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001843 _engineStatisticsPtr->SetLastError(
1844 VE_INVALID_ARGUMENT, kTraceError,
1845 "StartPlayingFileLocally() filePlayer format isnot correct");
1846 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001847 }
1848
kwiberg55b97fe2016-01-28 05:22:45 -08001849 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001850
kwiberg4ec01d92016-08-22 08:43:54 -07001851 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001852 volumeScaling, notificationTime,
1853 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001854 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1855 "StartPlayingFile() failed to "
1856 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001857 output_file_player_->StopPlayingFile();
1858 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001859 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001860 }
kwiberg5a25d952016-08-17 07:31:12 -07001861 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001862 channel_state_.SetOutputFilePlaying(true);
1863 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001864
kwiberg55b97fe2016-01-28 05:22:45 -08001865 if (RegisterFilePlayingToMixer() != 0)
1866 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001867
kwiberg55b97fe2016-01-28 05:22:45 -08001868 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001869}
1870
kwiberg55b97fe2016-01-28 05:22:45 -08001871int Channel::StopPlayingFileLocally() {
1872 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1873 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001874
kwiberg55b97fe2016-01-28 05:22:45 -08001875 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001876 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001877 }
1878
1879 {
1880 rtc::CritScope cs(&_fileCritSect);
1881
kwiberg5a25d952016-08-17 07:31:12 -07001882 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001883 _engineStatisticsPtr->SetLastError(
1884 VE_STOP_RECORDING_FAILED, kTraceError,
1885 "StopPlayingFile() could not stop playing");
1886 return -1;
1887 }
kwiberg5a25d952016-08-17 07:31:12 -07001888 output_file_player_->RegisterModuleFileCallback(NULL);
1889 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001890 channel_state_.SetOutputFilePlaying(false);
1891 }
1892 // _fileCritSect cannot be taken while calling
1893 // SetAnonymousMixibilityStatus. Refer to comments in
1894 // StartPlayingFileLocally(const char* ...) for more details.
1895 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1896 _engineStatisticsPtr->SetLastError(
1897 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1898 "StopPlayingFile() failed to stop participant from playing as"
1899 "file in the mixer");
1900 return -1;
1901 }
1902
1903 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001904}
1905
kwiberg55b97fe2016-01-28 05:22:45 -08001906int Channel::IsPlayingFileLocally() const {
1907 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001908}
1909
kwiberg55b97fe2016-01-28 05:22:45 -08001910int Channel::RegisterFilePlayingToMixer() {
1911 // Return success for not registering for file playing to mixer if:
1912 // 1. playing file before playout is started on that channel.
1913 // 2. starting playout without file playing on that channel.
1914 if (!channel_state_.Get().playing ||
1915 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001916 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001917 }
1918
1919 // |_fileCritSect| cannot be taken while calling
1920 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1921 // frames can be pulled by the mixer. Since the frames are generated from
1922 // the file, _fileCritSect will be taken. This would result in a deadlock.
1923 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1924 channel_state_.SetOutputFilePlaying(false);
1925 rtc::CritScope cs(&_fileCritSect);
1926 _engineStatisticsPtr->SetLastError(
1927 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1928 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001929 output_file_player_->StopPlayingFile();
1930 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001931 return -1;
1932 }
1933
1934 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001935}
1936
niklase@google.com470e71d2011-07-07 08:21:25 +00001937int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001938 bool loop,
1939 FileFormats format,
1940 int startPosition,
1941 float volumeScaling,
1942 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001943 const CodecInst* codecInst) {
1944 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1945 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1946 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1947 "stopPosition=%d)",
1948 fileName, loop, format, volumeScaling, startPosition,
1949 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001950
kwiberg55b97fe2016-01-28 05:22:45 -08001951 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001952
kwiberg55b97fe2016-01-28 05:22:45 -08001953 if (channel_state_.Get().input_file_playing) {
1954 _engineStatisticsPtr->SetLastError(
1955 VE_ALREADY_PLAYING, kTraceWarning,
1956 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001957 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001958 }
1959
1960 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001961 if (input_file_player_) {
1962 input_file_player_->RegisterModuleFileCallback(NULL);
1963 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001964 }
1965
1966 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001967 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001968 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001969
kwiberg5a25d952016-08-17 07:31:12 -07001970 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001971 _engineStatisticsPtr->SetLastError(
1972 VE_INVALID_ARGUMENT, kTraceError,
1973 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1974 return -1;
1975 }
1976
1977 const uint32_t notificationTime(0);
1978
kwiberg5a25d952016-08-17 07:31:12 -07001979 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001980 fileName, loop, startPosition, volumeScaling, notificationTime,
1981 stopPosition, (const CodecInst*)codecInst) != 0) {
1982 _engineStatisticsPtr->SetLastError(
1983 VE_BAD_FILE, kTraceError,
1984 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001985 input_file_player_->StopPlayingFile();
1986 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001987 return -1;
1988 }
kwiberg5a25d952016-08-17 07:31:12 -07001989 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001990 channel_state_.SetInputFilePlaying(true);
1991
1992 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001993}
1994
1995int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001996 FileFormats format,
1997 int startPosition,
1998 float volumeScaling,
1999 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002000 const CodecInst* codecInst) {
2001 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2002 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2003 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2004 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002005
kwiberg55b97fe2016-01-28 05:22:45 -08002006 if (stream == NULL) {
2007 _engineStatisticsPtr->SetLastError(
2008 VE_BAD_FILE, kTraceError,
2009 "StartPlayingFileAsMicrophone NULL as input stream");
2010 return -1;
2011 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002012
kwiberg55b97fe2016-01-28 05:22:45 -08002013 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002014
kwiberg55b97fe2016-01-28 05:22:45 -08002015 if (channel_state_.Get().input_file_playing) {
2016 _engineStatisticsPtr->SetLastError(
2017 VE_ALREADY_PLAYING, kTraceWarning,
2018 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002019 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002020 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002021
kwiberg55b97fe2016-01-28 05:22:45 -08002022 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002023 if (input_file_player_) {
2024 input_file_player_->RegisterModuleFileCallback(NULL);
2025 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002026 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002027
kwiberg55b97fe2016-01-28 05:22:45 -08002028 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002029 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002030 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002031
kwiberg5a25d952016-08-17 07:31:12 -07002032 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002033 _engineStatisticsPtr->SetLastError(
2034 VE_INVALID_ARGUMENT, kTraceError,
2035 "StartPlayingInputFile() filePlayer format isnot correct");
2036 return -1;
2037 }
2038
2039 const uint32_t notificationTime(0);
2040
kwiberg4ec01d92016-08-22 08:43:54 -07002041 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2042 notificationTime, stopPosition,
2043 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002044 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2045 "StartPlayingFile() failed to start "
2046 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002047 input_file_player_->StopPlayingFile();
2048 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002049 return -1;
2050 }
2051
kwiberg5a25d952016-08-17 07:31:12 -07002052 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002053 channel_state_.SetInputFilePlaying(true);
2054
2055 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002056}
2057
kwiberg55b97fe2016-01-28 05:22:45 -08002058int Channel::StopPlayingFileAsMicrophone() {
2059 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2060 "Channel::StopPlayingFileAsMicrophone()");
2061
2062 rtc::CritScope cs(&_fileCritSect);
2063
2064 if (!channel_state_.Get().input_file_playing) {
2065 return 0;
2066 }
2067
kwiberg5a25d952016-08-17 07:31:12 -07002068 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002069 _engineStatisticsPtr->SetLastError(
2070 VE_STOP_RECORDING_FAILED, kTraceError,
2071 "StopPlayingFile() could not stop playing");
2072 return -1;
2073 }
kwiberg5a25d952016-08-17 07:31:12 -07002074 input_file_player_->RegisterModuleFileCallback(NULL);
2075 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002076 channel_state_.SetInputFilePlaying(false);
2077
2078 return 0;
2079}
2080
2081int Channel::IsPlayingFileAsMicrophone() const {
2082 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002083}
2084
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002085int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002086 const CodecInst* codecInst) {
2087 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2088 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002089
kwiberg55b97fe2016-01-28 05:22:45 -08002090 if (_outputFileRecording) {
2091 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2092 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002093 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002094 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002095
kwiberg55b97fe2016-01-28 05:22:45 -08002096 FileFormats format;
2097 const uint32_t notificationTime(0); // Not supported in VoE
2098 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002099
kwiberg55b97fe2016-01-28 05:22:45 -08002100 if ((codecInst != NULL) &&
2101 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2102 _engineStatisticsPtr->SetLastError(
2103 VE_BAD_ARGUMENT, kTraceError,
2104 "StartRecordingPlayout() invalid compression");
2105 return (-1);
2106 }
2107 if (codecInst == NULL) {
2108 format = kFileFormatPcm16kHzFile;
2109 codecInst = &dummyCodec;
2110 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2111 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2112 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2113 format = kFileFormatWavFile;
2114 } else {
2115 format = kFileFormatCompressedFile;
2116 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002117
kwiberg55b97fe2016-01-28 05:22:45 -08002118 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002119
kwiberg55b97fe2016-01-28 05:22:45 -08002120 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002121 if (output_file_recorder_) {
2122 output_file_recorder_->RegisterModuleFileCallback(NULL);
2123 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002124 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002125
kwiberg5a25d952016-08-17 07:31:12 -07002126 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002127 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002128 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002129 _engineStatisticsPtr->SetLastError(
2130 VE_INVALID_ARGUMENT, kTraceError,
2131 "StartRecordingPlayout() fileRecorder format isnot correct");
2132 return -1;
2133 }
2134
kwiberg5a25d952016-08-17 07:31:12 -07002135 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002136 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2137 _engineStatisticsPtr->SetLastError(
2138 VE_BAD_FILE, kTraceError,
2139 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002140 output_file_recorder_->StopRecording();
2141 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002142 return -1;
2143 }
kwiberg5a25d952016-08-17 07:31:12 -07002144 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002145 _outputFileRecording = true;
2146
2147 return 0;
2148}
2149
2150int Channel::StartRecordingPlayout(OutStream* stream,
2151 const CodecInst* codecInst) {
2152 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2153 "Channel::StartRecordingPlayout()");
2154
2155 if (_outputFileRecording) {
2156 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2157 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002158 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002159 }
2160
2161 FileFormats format;
2162 const uint32_t notificationTime(0); // Not supported in VoE
2163 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2164
2165 if (codecInst != NULL && codecInst->channels != 1) {
2166 _engineStatisticsPtr->SetLastError(
2167 VE_BAD_ARGUMENT, kTraceError,
2168 "StartRecordingPlayout() invalid compression");
2169 return (-1);
2170 }
2171 if (codecInst == NULL) {
2172 format = kFileFormatPcm16kHzFile;
2173 codecInst = &dummyCodec;
2174 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2175 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2176 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2177 format = kFileFormatWavFile;
2178 } else {
2179 format = kFileFormatCompressedFile;
2180 }
2181
2182 rtc::CritScope cs(&_fileCritSect);
2183
2184 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002185 if (output_file_recorder_) {
2186 output_file_recorder_->RegisterModuleFileCallback(NULL);
2187 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002188 }
2189
kwiberg5a25d952016-08-17 07:31:12 -07002190 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002191 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002192 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002193 _engineStatisticsPtr->SetLastError(
2194 VE_INVALID_ARGUMENT, kTraceError,
2195 "StartRecordingPlayout() fileRecorder format isnot correct");
2196 return -1;
2197 }
2198
kwiberg4ec01d92016-08-22 08:43:54 -07002199 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002200 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002201 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2202 "StartRecordingPlayout() failed to "
2203 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002204 output_file_recorder_->StopRecording();
2205 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002206 return -1;
2207 }
2208
kwiberg5a25d952016-08-17 07:31:12 -07002209 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002210 _outputFileRecording = true;
2211
2212 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002213}
2214
kwiberg55b97fe2016-01-28 05:22:45 -08002215int Channel::StopRecordingPlayout() {
2216 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2217 "Channel::StopRecordingPlayout()");
2218
2219 if (!_outputFileRecording) {
2220 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2221 "StopRecordingPlayout() isnot recording");
2222 return -1;
2223 }
2224
2225 rtc::CritScope cs(&_fileCritSect);
2226
kwiberg5a25d952016-08-17 07:31:12 -07002227 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002228 _engineStatisticsPtr->SetLastError(
2229 VE_STOP_RECORDING_FAILED, kTraceError,
2230 "StopRecording() could not stop recording");
2231 return (-1);
2232 }
kwiberg5a25d952016-08-17 07:31:12 -07002233 output_file_recorder_->RegisterModuleFileCallback(NULL);
2234 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002235 _outputFileRecording = false;
2236
2237 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002238}
2239
kwiberg55b97fe2016-01-28 05:22:45 -08002240void Channel::SetMixWithMicStatus(bool mix) {
2241 rtc::CritScope cs(&_fileCritSect);
2242 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002243}
2244
solenberg8d73f8c2017-03-08 01:52:20 -08002245int Channel::GetSpeechOutputLevel() const {
2246 return _outputAudioLevel.Level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002247}
2248
solenberg8d73f8c2017-03-08 01:52:20 -08002249int Channel::GetSpeechOutputLevelFullRange() const {
2250 return _outputAudioLevel.LevelFullRange();
kwiberg55b97fe2016-01-28 05:22:45 -08002251}
2252
solenberg8d73f8c2017-03-08 01:52:20 -08002253void Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002254 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002255 input_mute_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002256}
2257
solenberg1c2af8e2016-03-24 10:36:00 -07002258bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002259 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002260 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002261}
2262
solenberg8d73f8c2017-03-08 01:52:20 -08002263void Channel::SetChannelOutputVolumeScaling(float scaling) {
kwiberg55b97fe2016-01-28 05:22:45 -08002264 rtc::CritScope cs(&volume_settings_critsect_);
kwiberg55b97fe2016-01-28 05:22:45 -08002265 _outputGain = scaling;
niklase@google.com470e71d2011-07-07 08:21:25 +00002266}
2267
solenberg8842c3e2016-03-11 03:06:41 -08002268int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002269 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002270 "Channel::SendTelephoneEventOutband(...)");
2271 RTC_DCHECK_LE(0, event);
2272 RTC_DCHECK_GE(255, event);
2273 RTC_DCHECK_LE(0, duration_ms);
2274 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002275 if (!Sending()) {
2276 return -1;
2277 }
solenberg8842c3e2016-03-11 03:06:41 -08002278 if (_rtpRtcpModule->SendTelephoneEventOutband(
2279 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002280 _engineStatisticsPtr->SetLastError(
2281 VE_SEND_DTMF_FAILED, kTraceWarning,
2282 "SendTelephoneEventOutband() failed to send event");
2283 return -1;
2284 }
2285 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002286}
2287
solenbergffbbcac2016-11-17 05:25:37 -08002288int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2289 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002290 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002291 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002292 RTC_DCHECK_LE(0, payload_type);
2293 RTC_DCHECK_GE(127, payload_type);
2294 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002295 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002296 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002297 memcpy(codec.plname, "telephone-event", 16);
2298 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2299 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2300 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2301 _engineStatisticsPtr->SetLastError(
2302 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2303 "SetSendTelephoneEventPayloadType() failed to register send"
2304 "payload type");
2305 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002306 }
kwiberg55b97fe2016-01-28 05:22:45 -08002307 }
kwiberg55b97fe2016-01-28 05:22:45 -08002308 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002309}
2310
kwiberg55b97fe2016-01-28 05:22:45 -08002311int Channel::SetLocalSSRC(unsigned int ssrc) {
2312 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2313 "Channel::SetLocalSSRC()");
2314 if (channel_state_.Get().sending) {
2315 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2316 "SetLocalSSRC() already sending");
2317 return -1;
2318 }
2319 _rtpRtcpModule->SetSSRC(ssrc);
2320 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002321}
2322
kwiberg55b97fe2016-01-28 05:22:45 -08002323int Channel::GetLocalSSRC(unsigned int& ssrc) {
2324 ssrc = _rtpRtcpModule->SSRC();
2325 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002326}
2327
kwiberg55b97fe2016-01-28 05:22:45 -08002328int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2329 ssrc = rtp_receiver_->SSRC();
2330 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002331}
2332
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002333int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002334 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002335 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002336}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002337
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002338int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2339 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002340 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2341 if (enable &&
2342 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2343 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002344 return -1;
2345 }
2346 return 0;
2347}
2348
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002349void Channel::EnableSendTransportSequenceNumber(int id) {
2350 int ret =
2351 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2352 RTC_DCHECK_EQ(0, ret);
2353}
2354
stefan3313ec92016-01-21 06:32:43 -08002355void Channel::EnableReceiveTransportSequenceNumber(int id) {
2356 rtp_header_parser_->DeregisterRtpHeaderExtension(
2357 kRtpExtensionTransportSequenceNumber);
2358 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2359 kRtpExtensionTransportSequenceNumber, id);
2360 RTC_DCHECK(ret);
2361}
2362
stefanbba9dec2016-02-01 04:39:55 -08002363void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002364 RtpPacketSender* rtp_packet_sender,
2365 TransportFeedbackObserver* transport_feedback_observer,
stefan7de8d642017-02-07 07:14:08 -08002366 PacketRouter* packet_router,
2367 RtcpBandwidthObserver* bandwidth_observer) {
stefanbba9dec2016-02-01 04:39:55 -08002368 RTC_DCHECK(rtp_packet_sender);
2369 RTC_DCHECK(transport_feedback_observer);
2370 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002371 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002372 feedback_observer_proxy_->SetTransportFeedbackObserver(
2373 transport_feedback_observer);
2374 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2375 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2376 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002377 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002378 packet_router_ = packet_router;
2379}
2380
stefanbba9dec2016-02-01 04:39:55 -08002381void Channel::RegisterReceiverCongestionControlObjects(
2382 PacketRouter* packet_router) {
2383 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002384 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002385 packet_router_ = packet_router;
2386}
2387
2388void Channel::ResetCongestionControlObjects() {
2389 RTC_DCHECK(packet_router_);
2390 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002391 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002392 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2393 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002394 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002395 packet_router_ = nullptr;
2396 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2397}
2398
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002399void Channel::SetRTCPStatus(bool enable) {
2400 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2401 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002402 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002403}
2404
kwiberg55b97fe2016-01-28 05:22:45 -08002405int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002406 RtcpMode method = _rtpRtcpModule->RTCP();
2407 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002408 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002409}
2410
kwiberg55b97fe2016-01-28 05:22:45 -08002411int Channel::SetRTCP_CNAME(const char cName[256]) {
2412 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2413 "Channel::SetRTCP_CNAME()");
2414 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2415 _engineStatisticsPtr->SetLastError(
2416 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2417 "SetRTCP_CNAME() failed to set RTCP CNAME");
2418 return -1;
2419 }
2420 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002421}
2422
kwiberg55b97fe2016-01-28 05:22:45 -08002423int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2424 if (cName == NULL) {
2425 _engineStatisticsPtr->SetLastError(
2426 VE_INVALID_ARGUMENT, kTraceError,
2427 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2428 return -1;
2429 }
2430 char cname[RTCP_CNAME_SIZE];
2431 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2432 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2433 _engineStatisticsPtr->SetLastError(
2434 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2435 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2436 return -1;
2437 }
2438 strcpy(cName, cname);
2439 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002440}
2441
kwiberg55b97fe2016-01-28 05:22:45 -08002442int Channel::SendApplicationDefinedRTCPPacket(
2443 unsigned char subType,
2444 unsigned int name,
2445 const char* data,
2446 unsigned short dataLengthInBytes) {
2447 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2448 "Channel::SendApplicationDefinedRTCPPacket()");
2449 if (!channel_state_.Get().sending) {
2450 _engineStatisticsPtr->SetLastError(
2451 VE_NOT_SENDING, kTraceError,
2452 "SendApplicationDefinedRTCPPacket() not sending");
2453 return -1;
2454 }
2455 if (NULL == data) {
2456 _engineStatisticsPtr->SetLastError(
2457 VE_INVALID_ARGUMENT, kTraceError,
2458 "SendApplicationDefinedRTCPPacket() invalid data value");
2459 return -1;
2460 }
2461 if (dataLengthInBytes % 4 != 0) {
2462 _engineStatisticsPtr->SetLastError(
2463 VE_INVALID_ARGUMENT, kTraceError,
2464 "SendApplicationDefinedRTCPPacket() invalid length value");
2465 return -1;
2466 }
2467 RtcpMode status = _rtpRtcpModule->RTCP();
2468 if (status == RtcpMode::kOff) {
2469 _engineStatisticsPtr->SetLastError(
2470 VE_RTCP_ERROR, kTraceError,
2471 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2472 return -1;
2473 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002474
kwiberg55b97fe2016-01-28 05:22:45 -08002475 // Create and schedule the RTCP APP packet for transmission
2476 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2477 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2478 _engineStatisticsPtr->SetLastError(
2479 VE_SEND_ERROR, kTraceError,
2480 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2481 return -1;
2482 }
2483 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002484}
2485
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002486int Channel::GetRemoteRTCPReportBlocks(
2487 std::vector<ReportBlock>* report_blocks) {
2488 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002489 _engineStatisticsPtr->SetLastError(
2490 VE_INVALID_ARGUMENT, kTraceError,
2491 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002492 return -1;
2493 }
2494
2495 // Get the report blocks from the latest received RTCP Sender or Receiver
2496 // Report. Each element in the vector contains the sender's SSRC and a
2497 // report block according to RFC 3550.
2498 std::vector<RTCPReportBlock> rtcp_report_blocks;
2499 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002500 return -1;
2501 }
2502
2503 if (rtcp_report_blocks.empty())
2504 return 0;
2505
2506 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2507 for (; it != rtcp_report_blocks.end(); ++it) {
2508 ReportBlock report_block;
2509 report_block.sender_SSRC = it->remoteSSRC;
2510 report_block.source_SSRC = it->sourceSSRC;
2511 report_block.fraction_lost = it->fractionLost;
2512 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2513 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2514 report_block.interarrival_jitter = it->jitter;
2515 report_block.last_SR_timestamp = it->lastSR;
2516 report_block.delay_since_last_SR = it->delaySinceLastSR;
2517 report_blocks->push_back(report_block);
2518 }
2519 return 0;
2520}
2521
kwiberg55b97fe2016-01-28 05:22:45 -08002522int Channel::GetRTPStatistics(CallStatistics& stats) {
2523 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002524
kwiberg55b97fe2016-01-28 05:22:45 -08002525 // The jitter statistics is updated for each received RTP packet and is
2526 // based on received packets.
2527 RtcpStatistics statistics;
2528 StreamStatistician* statistician =
2529 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002530 if (statistician) {
2531 statistician->GetStatistics(&statistics,
2532 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002533 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002534
kwiberg55b97fe2016-01-28 05:22:45 -08002535 stats.fractionLost = statistics.fraction_lost;
2536 stats.cumulativeLost = statistics.cumulative_lost;
2537 stats.extendedMax = statistics.extended_max_sequence_number;
2538 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002539
kwiberg55b97fe2016-01-28 05:22:45 -08002540 // --- RTT
2541 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002542
kwiberg55b97fe2016-01-28 05:22:45 -08002543 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002544
kwiberg55b97fe2016-01-28 05:22:45 -08002545 size_t bytesSent(0);
2546 uint32_t packetsSent(0);
2547 size_t bytesReceived(0);
2548 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002549
kwiberg55b97fe2016-01-28 05:22:45 -08002550 if (statistician) {
2551 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2552 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002553
kwiberg55b97fe2016-01-28 05:22:45 -08002554 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2555 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2556 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2557 " output will not be complete");
2558 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002559
kwiberg55b97fe2016-01-28 05:22:45 -08002560 stats.bytesSent = bytesSent;
2561 stats.packetsSent = packetsSent;
2562 stats.bytesReceived = bytesReceived;
2563 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002564
kwiberg55b97fe2016-01-28 05:22:45 -08002565 // --- Timestamps
2566 {
2567 rtc::CritScope lock(&ts_stats_lock_);
2568 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2569 }
2570 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002571}
2572
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002573int Channel::SetCodecFECStatus(bool enable) {
2574 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2575 "Channel::SetCodecFECStatus()");
2576
kwibergc8d071e2016-04-06 12:22:38 -07002577 if (!codec_manager_.SetCodecFEC(enable) ||
2578 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002579 _engineStatisticsPtr->SetLastError(
2580 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2581 "SetCodecFECStatus() failed to set FEC state");
2582 return -1;
2583 }
2584 return 0;
2585}
2586
2587bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002588 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002589}
2590
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002591void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2592 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002593 // If pacing is enabled we always store packets.
2594 if (!pacing_enabled_)
2595 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002596 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002597 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002598 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002599 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002600 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002601}
2602
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002603// Called when we are missing one or more packets.
2604int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002605 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2606}
2607
kwiberg55b97fe2016-01-28 05:22:45 -08002608uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2609 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2610 "Channel::Demultiplex()");
2611 _audioFrame.CopyFrom(audioFrame);
2612 _audioFrame.id_ = _channelId;
2613 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002614}
2615
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002616void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002617 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002618 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002619 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002620 CodecInst codec;
2621 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002622
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002623 // Never upsample or upmix the capture signal here. This should be done at the
2624 // end of the send chain.
2625 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2626 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2627 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2628 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002629}
2630
kwiberg55b97fe2016-01-28 05:22:45 -08002631uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2632 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2633 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002634
kwiberg55b97fe2016-01-28 05:22:45 -08002635 if (_audioFrame.samples_per_channel_ == 0) {
2636 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2637 "Channel::PrepareEncodeAndSend() invalid audio frame");
2638 return 0xFFFFFFFF;
2639 }
2640
2641 if (channel_state_.Get().input_file_playing) {
2642 MixOrReplaceAudioWithFile(mixingFrequency);
2643 }
2644
solenberg1c2af8e2016-03-24 10:36:00 -07002645 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2646 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002647
kwiberg55b97fe2016-01-28 05:22:45 -08002648 if (_includeAudioLevelIndication) {
2649 size_t length =
2650 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002651 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002652 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002653 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002654 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002655 rms_level_.Analyze(
2656 rtc::ArrayView<const int16_t>(_audioFrame.data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002657 }
kwiberg55b97fe2016-01-28 05:22:45 -08002658 }
solenberg1c2af8e2016-03-24 10:36:00 -07002659 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002660
kwiberg55b97fe2016-01-28 05:22:45 -08002661 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002662}
2663
kwiberg55b97fe2016-01-28 05:22:45 -08002664uint32_t Channel::EncodeAndSend() {
2665 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2666 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002667
kwiberg55b97fe2016-01-28 05:22:45 -08002668 assert(_audioFrame.num_channels_ <= 2);
2669 if (_audioFrame.samples_per_channel_ == 0) {
2670 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2671 "Channel::EncodeAndSend() invalid audio frame");
2672 return 0xFFFFFFFF;
2673 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002674
kwiberg55b97fe2016-01-28 05:22:45 -08002675 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002676
kwiberg55b97fe2016-01-28 05:22:45 -08002677 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002678
kwiberg55b97fe2016-01-28 05:22:45 -08002679 // The ACM resamples internally.
2680 _audioFrame.timestamp_ = _timeStamp;
2681 // This call will trigger AudioPacketizationCallback::SendData if encoding
2682 // is done and payload is ready for packetization and transmission.
2683 // Otherwise, it will return without invoking the callback.
2684 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2685 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2686 "Channel::EncodeAndSend() ACM encoding failed");
2687 return 0xFFFFFFFF;
2688 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002689
kwiberg55b97fe2016-01-28 05:22:45 -08002690 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2691 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002692}
2693
solenberg7602aab2016-11-14 11:30:07 -08002694void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2695 RTC_DCHECK(!channel.channel() ||
2696 channel.channel()->ChannelId() != _channelId);
2697 rtc::CritScope lock(&assoc_send_channel_lock_);
2698 associate_send_channel_ = channel;
2699}
2700
Minyue2013aec2015-05-13 14:14:42 +02002701void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002702 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002703 Channel* channel = associate_send_channel_.channel();
2704 if (channel && channel->ChannelId() == channel_id) {
2705 // If this channel is associated with a send channel of the specified
2706 // Channel ID, disassociate with it.
2707 ChannelOwner ref(NULL);
2708 associate_send_channel_ = ref;
2709 }
2710}
2711
ivoc14d5dbe2016-07-04 07:06:55 -07002712void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2713 event_log_proxy_->SetEventLog(event_log);
2714}
2715
michaelt9332b7d2016-11-30 07:51:13 -08002716void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2717 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2718}
2719
nisse284542b2017-01-10 08:58:32 -08002720void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08002721 size_t overhead_per_packet =
2722 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08002723 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2724 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08002725 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08002726 }
2727 });
2728}
2729
2730void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002731 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002732 transport_overhead_per_packet_ = transport_overhead_per_packet;
2733 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002734}
2735
hbos3fd31fe2017-02-28 05:43:16 -08002736// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08002737void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002738 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002739 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2740 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002741}
2742
kwiberg55b97fe2016-01-28 05:22:45 -08002743int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2744 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002745}
2746
wu@webrtc.org24301a62013-12-13 19:17:43 +00002747void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2748 audio_coding_->GetDecodingCallStatistics(stats);
2749}
2750
solenberg358057b2015-11-27 10:46:42 -08002751uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002752 rtc::CritScope lock(&video_sync_lock_);
2753 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002754}
2755
kwiberg55b97fe2016-01-28 05:22:45 -08002756int Channel::SetMinimumPlayoutDelay(int delayMs) {
2757 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2758 "Channel::SetMinimumPlayoutDelay()");
2759 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2760 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2761 _engineStatisticsPtr->SetLastError(
2762 VE_INVALID_ARGUMENT, kTraceError,
2763 "SetMinimumPlayoutDelay() invalid min delay");
2764 return -1;
2765 }
2766 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2767 _engineStatisticsPtr->SetLastError(
2768 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2769 "SetMinimumPlayoutDelay() failed to set min playout delay");
2770 return -1;
2771 }
2772 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002773}
2774
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002775int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002776 uint32_t playout_timestamp_rtp = 0;
2777 {
tommi31fc21f2016-01-21 10:37:37 -08002778 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002779 playout_timestamp_rtp = playout_timestamp_rtp_;
2780 }
kwiberg55b97fe2016-01-28 05:22:45 -08002781 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002782 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002783 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002784 "GetPlayoutTimestamp() failed to retrieve timestamp");
2785 return -1;
2786 }
deadbeef74375882015-08-13 12:09:10 -07002787 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002788 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002789}
2790
kwiberg55b97fe2016-01-28 05:22:45 -08002791int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2792 RtpReceiver** rtp_receiver) const {
2793 *rtpRtcpModule = _rtpRtcpModule.get();
2794 *rtp_receiver = rtp_receiver_.get();
2795 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002796}
2797
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002798// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2799// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002800int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002801 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002802 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002803
kwiberg55b97fe2016-01-28 05:22:45 -08002804 {
2805 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002806
kwiberg5a25d952016-08-17 07:31:12 -07002807 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002808 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2809 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2810 " doesnt exist");
2811 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002812 }
2813
kwiberg4ec01d92016-08-22 08:43:54 -07002814 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002815 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002816 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2817 "Channel::MixOrReplaceAudioWithFile() file mixing "
2818 "failed");
2819 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002820 }
kwiberg55b97fe2016-01-28 05:22:45 -08002821 if (fileSamples == 0) {
2822 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2823 "Channel::MixOrReplaceAudioWithFile() file is ended");
2824 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002825 }
kwiberg55b97fe2016-01-28 05:22:45 -08002826 }
2827
2828 assert(_audioFrame.samples_per_channel_ == fileSamples);
2829
2830 if (_mixFileWithMicrophone) {
2831 // Currently file stream is always mono.
2832 // TODO(xians): Change the code when FilePlayer supports real stereo.
2833 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
2834 1, fileSamples);
2835 } else {
2836 // Replace ACM audio with file.
2837 // Currently file stream is always mono.
2838 // TODO(xians): Change the code when FilePlayer supports real stereo.
2839 _audioFrame.UpdateFrame(
2840 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2841 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2842 }
2843 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002844}
2845
kwiberg55b97fe2016-01-28 05:22:45 -08002846int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2847 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002848
kwibergb7f89d62016-02-17 10:04:18 -08002849 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002850 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002851
kwiberg55b97fe2016-01-28 05:22:45 -08002852 {
2853 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002854
kwiberg5a25d952016-08-17 07:31:12 -07002855 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002856 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2857 "Channel::MixAudioWithFile() file mixing failed");
2858 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002859 }
2860
kwiberg55b97fe2016-01-28 05:22:45 -08002861 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002862 if (output_file_player_->Get10msAudioFromFile(
2863 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002864 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2865 "Channel::MixAudioWithFile() file mixing failed");
2866 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002867 }
kwiberg55b97fe2016-01-28 05:22:45 -08002868 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002869
kwiberg55b97fe2016-01-28 05:22:45 -08002870 if (audioFrame.samples_per_channel_ == fileSamples) {
2871 // Currently file stream is always mono.
2872 // TODO(xians): Change the code when FilePlayer supports real stereo.
2873 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
2874 fileSamples);
2875 } else {
2876 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2877 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2878 ") != "
2879 "fileSamples(%" PRIuS ")",
2880 audioFrame.samples_per_channel_, fileSamples);
2881 return -1;
2882 }
2883
2884 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002885}
2886
deadbeef74375882015-08-13 12:09:10 -07002887void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002888 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002889
henrik.lundin96bd5022016-04-06 04:13:56 -07002890 if (!jitter_buffer_playout_timestamp_) {
2891 // This can happen if this channel has not received any RTP packets. In
2892 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07002893 return;
2894 }
2895
2896 uint16_t delay_ms = 0;
2897 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002898 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002899 "Channel::UpdatePlayoutTimestamp() failed to read playout"
2900 " delay from the ADM");
2901 _engineStatisticsPtr->SetLastError(
2902 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
2903 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
2904 return;
2905 }
2906
henrik.lundin96bd5022016-04-06 04:13:56 -07002907 RTC_DCHECK(jitter_buffer_playout_timestamp_);
2908 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07002909
2910 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07002911 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07002912
kwiberg55b97fe2016-01-28 05:22:45 -08002913 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002914 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07002915 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07002916
2917 {
tommi31fc21f2016-01-21 10:37:37 -08002918 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08002919 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002920 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07002921 }
2922 playout_delay_ms_ = delay_ms;
2923 }
2924}
2925
kwiberg55b97fe2016-01-28 05:22:45 -08002926void Channel::RegisterReceiveCodecsToRTPModule() {
2927 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2928 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002929
kwiberg55b97fe2016-01-28 05:22:45 -08002930 CodecInst codec;
2931 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002932
kwiberg55b97fe2016-01-28 05:22:45 -08002933 for (int idx = 0; idx < nSupportedCodecs; idx++) {
2934 // Open up the RTP/RTCP receiver for all supported codecs
2935 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08002936 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08002937 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2938 "Channel::RegisterReceiveCodecsToRTPModule() unable"
2939 " to register %s (%d/%d/%" PRIuS
2940 "/%d) to RTP/RTCP "
2941 "receiver",
2942 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2943 codec.rate);
2944 } else {
2945 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2946 "Channel::RegisterReceiveCodecsToRTPModule() %s "
2947 "(%d/%d/%" PRIuS
2948 "/%d) has been added to the RTP/RTCP "
2949 "receiver",
2950 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2951 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00002952 }
kwiberg55b97fe2016-01-28 05:22:45 -08002953 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002954}
2955
kwiberg55b97fe2016-01-28 05:22:45 -08002956int Channel::SetSendRtpHeaderExtension(bool enable,
2957 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002958 unsigned char id) {
2959 int error = 0;
2960 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
2961 if (enable) {
2962 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
2963 }
2964 return error;
2965}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002966
ossue280cde2016-10-12 11:04:10 -07002967int Channel::GetRtpTimestampRateHz() const {
2968 const auto format = audio_coding_->ReceiveFormat();
2969 // Default to the playout frequency if we've not gotten any packets yet.
2970 // TODO(ossu): Zero clockrate can only happen if we've added an external
2971 // decoder for a format we don't support internally. Remove once that way of
2972 // adding decoders is gone!
2973 return (format && format->clockrate_hz != 0)
2974 ? format->clockrate_hz
2975 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00002976}
2977
Minyue2013aec2015-05-13 14:14:42 +02002978int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07002979 RtcpMode method = _rtpRtcpModule->RTCP();
2980 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002981 return 0;
2982 }
2983 std::vector<RTCPReportBlock> report_blocks;
2984 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02002985
2986 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002987 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02002988 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08002989 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002990 Channel* channel = associate_send_channel_.channel();
2991 // Tries to get RTT from an associated channel. This is important for
2992 // receive-only channels.
2993 if (channel) {
2994 // To prevent infinite recursion and deadlock, calling GetRTT of
2995 // associate channel should always use "false" for argument:
2996 // |allow_associate_channel|.
2997 rtt = channel->GetRTT(false);
2998 }
2999 }
3000 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003001 }
3002
3003 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3004 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3005 for (; it != report_blocks.end(); ++it) {
3006 if (it->remoteSSRC == remoteSSRC)
3007 break;
3008 }
3009 if (it == report_blocks.end()) {
3010 // We have not received packets with SSRC matching the report blocks.
3011 // To calculate RTT we try with the SSRC of the first report block.
3012 // This is very important for send-only channels where we don't know
3013 // the SSRC of the other end.
3014 remoteSSRC = report_blocks[0].remoteSSRC;
3015 }
Minyue2013aec2015-05-13 14:14:42 +02003016
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003017 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003018 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003019 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003020 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3021 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003022 return 0;
3023 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003024 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003025}
3026
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003027} // namespace voe
3028} // namespace webrtc