blob: 3b562f91561c83927ff973b13e51622ea10a1166 [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
kwiberg1724cfb2017-03-24 03:16:04 -0700990 return 0;
991}
kwiberg55b97fe2016-01-28 05:22:45 -0800992
kwiberg1724cfb2017-03-24 03:16:04 -0700993void Channel::RegisterLegacyCodecs() {
kwiberg55b97fe2016-01-28 05:22:45 -0800994 CodecInst codec;
995 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
996
997 for (int idx = 0; idx < nSupportedCodecs; idx++) {
998 // Open up the RTP/RTCP receiver for all supported codecs
999 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08001000 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001001 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1002 "Channel::Init() unable to register %s "
1003 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1004 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1005 codec.rate);
1006 } else {
1007 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1008 "Channel::Init() %s (%d/%d/%" PRIuS
1009 "/%d) has been "
1010 "added to the RTP/RTCP receiver",
1011 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1012 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001013 }
1014
kwiberg55b97fe2016-01-28 05:22:45 -08001015 // Ensure that PCMU is used as default codec on the sending side
1016 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1017 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001018 }
1019
kwiberg55b97fe2016-01-28 05:22:45 -08001020 // Register default PT for outband 'telephone-event'
1021 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001022 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001023 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1024 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001025 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1026 "Channel::Init() failed to register outband "
1027 "'telephone-event' (%d/%d) correctly",
1028 codec.pltype, codec.plfreq);
1029 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001030 }
1031
kwiberg55b97fe2016-01-28 05:22:45 -08001032 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001033 if (!codec_manager_.RegisterEncoder(codec) ||
1034 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001035 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1036 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001037 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001038 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1039 "Channel::Init() failed to register CN (%d/%d) "
1040 "correctly - 1",
1041 codec.pltype, codec.plfreq);
1042 }
1043 }
kwiberg55b97fe2016-01-28 05:22:45 -08001044 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001045}
1046
tommi0a2391f2017-03-21 02:31:51 -07001047void Channel::Terminate() {
1048 RTC_DCHECK(construction_thread_.CalledOnValidThread());
1049 // Must be called on the same thread as Init().
1050 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
1051 "Channel::Terminate");
1052
1053 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
1054
1055 StopSend();
1056 StopPlayout();
1057
1058 {
1059 rtc::CritScope cs(&_fileCritSect);
1060 if (input_file_player_) {
1061 input_file_player_->RegisterModuleFileCallback(NULL);
1062 input_file_player_->StopPlayingFile();
1063 }
1064 if (output_file_player_) {
1065 output_file_player_->RegisterModuleFileCallback(NULL);
1066 output_file_player_->StopPlayingFile();
1067 }
1068 if (output_file_recorder_) {
1069 output_file_recorder_->RegisterModuleFileCallback(NULL);
1070 output_file_recorder_->StopRecording();
1071 }
1072 }
1073
1074 // The order to safely shutdown modules in a channel is:
1075 // 1. De-register callbacks in modules
1076 // 2. De-register modules in process thread
1077 // 3. Destroy modules
1078 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
1079 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1080 "Terminate() failed to de-register transport callback"
1081 " (Audio coding module)");
1082 }
1083
1084 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
1085 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1086 "Terminate() failed to de-register VAD callback"
1087 " (Audio coding module)");
1088 }
1089
1090 // De-register modules in process thread
1091 if (_moduleProcessThreadPtr)
1092 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
1093
1094 // End of modules shutdown
1095}
1096
kwiberg55b97fe2016-01-28 05:22:45 -08001097int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1098 OutputMixer& outputMixer,
kwiberg55b97fe2016-01-28 05:22:45 -08001099 ProcessThread& moduleProcessThread,
1100 AudioDeviceModule& audioDeviceModule,
1101 VoiceEngineObserver* voiceEngineObserver,
1102 rtc::CriticalSection* callbackCritSect) {
1103 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1104 "Channel::SetEngineInformation()");
1105 _engineStatisticsPtr = &engineStatistics;
1106 _outputMixerPtr = &outputMixer;
kwiberg55b97fe2016-01-28 05:22:45 -08001107 _moduleProcessThreadPtr = &moduleProcessThread;
1108 _audioDeviceModulePtr = &audioDeviceModule;
1109 _voiceEngineObserverPtr = voiceEngineObserver;
1110 _callbackCritSectPtr = callbackCritSect;
1111 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001112}
1113
kwiberg55b97fe2016-01-28 05:22:45 -08001114int32_t Channel::UpdateLocalTimeStamp() {
1115 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1116 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001117}
1118
kwibergb7f89d62016-02-17 10:04:18 -08001119void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001120 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001121 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001122}
1123
ossu29b1a8d2016-06-13 07:34:51 -07001124const rtc::scoped_refptr<AudioDecoderFactory>&
1125Channel::GetAudioDecoderFactory() const {
1126 return decoder_factory_;
1127}
1128
kwiberg55b97fe2016-01-28 05:22:45 -08001129int32_t Channel::StartPlayout() {
1130 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1131 "Channel::StartPlayout()");
1132 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001133 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001134 }
1135
solenberge374e012017-02-14 04:55:00 -08001136 // Add participant as candidates for mixing.
1137 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1138 _engineStatisticsPtr->SetLastError(
1139 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1140 "StartPlayout() failed to add participant to mixer");
1141 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001142 }
1143
1144 channel_state_.SetPlaying(true);
1145 if (RegisterFilePlayingToMixer() != 0)
1146 return -1;
1147
1148 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001149}
1150
kwiberg55b97fe2016-01-28 05:22:45 -08001151int32_t Channel::StopPlayout() {
1152 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1153 "Channel::StopPlayout()");
1154 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001155 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001156 }
1157
solenberge374e012017-02-14 04:55:00 -08001158 // Remove participant as candidates for mixing
1159 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1160 _engineStatisticsPtr->SetLastError(
1161 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1162 "StopPlayout() failed to remove participant from mixer");
1163 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001164 }
1165
1166 channel_state_.SetPlaying(false);
1167 _outputAudioLevel.Clear();
1168
1169 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001170}
1171
kwiberg55b97fe2016-01-28 05:22:45 -08001172int32_t Channel::StartSend() {
1173 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1174 "Channel::StartSend()");
kwiberg55b97fe2016-01-28 05:22:45 -08001175 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001176 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001177 }
1178 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001179
solenberg08b19df2017-02-15 00:42:31 -08001180 // Resume the previous sequence number which was reset by StopSend(). This
1181 // needs to be done before |sending| is set to true on the RTP/RTCP module.
1182 if (send_sequence_number_) {
1183 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
1184 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001185 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001186 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1187 _engineStatisticsPtr->SetLastError(
1188 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1189 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001190 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001191 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001192 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001193 return -1;
1194 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001195
kwiberg55b97fe2016-01-28 05:22:45 -08001196 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001197}
1198
kwiberg55b97fe2016-01-28 05:22:45 -08001199int32_t Channel::StopSend() {
1200 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1201 "Channel::StopSend()");
1202 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001203 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001204 }
1205 channel_state_.SetSending(false);
1206
1207 // Store the sequence number to be able to pick up the same sequence for
1208 // the next StartSend(). This is needed for restarting device, otherwise
1209 // it might cause libSRTP to complain about packets being replayed.
1210 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1211 // CL is landed. See issue
1212 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1213 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1214
1215 // Reset sending SSRC and sequence number and triggers direct transmission
1216 // of RTCP BYE
1217 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1218 _engineStatisticsPtr->SetLastError(
1219 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1220 "StartSend() RTP/RTCP failed to stop sending");
1221 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001222 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001223
1224 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001225}
1226
kwiberg55b97fe2016-01-28 05:22:45 -08001227int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1228 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1229 "Channel::RegisterVoiceEngineObserver()");
1230 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001231
kwiberg55b97fe2016-01-28 05:22:45 -08001232 if (_voiceEngineObserverPtr) {
1233 _engineStatisticsPtr->SetLastError(
1234 VE_INVALID_OPERATION, kTraceError,
1235 "RegisterVoiceEngineObserver() observer already enabled");
1236 return -1;
1237 }
1238 _voiceEngineObserverPtr = &observer;
1239 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001240}
1241
kwiberg55b97fe2016-01-28 05:22:45 -08001242int32_t Channel::DeRegisterVoiceEngineObserver() {
1243 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1244 "Channel::DeRegisterVoiceEngineObserver()");
1245 rtc::CritScope cs(&_callbackCritSect);
1246
1247 if (!_voiceEngineObserverPtr) {
1248 _engineStatisticsPtr->SetLastError(
1249 VE_INVALID_OPERATION, kTraceWarning,
1250 "DeRegisterVoiceEngineObserver() observer already disabled");
1251 return 0;
1252 }
1253 _voiceEngineObserverPtr = NULL;
1254 return 0;
1255}
1256
1257int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001258 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001259 if (send_codec) {
1260 codec = *send_codec;
1261 return 0;
1262 }
1263 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001264}
1265
kwiberg55b97fe2016-01-28 05:22:45 -08001266int32_t Channel::GetRecCodec(CodecInst& codec) {
1267 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001268}
1269
kwiberg55b97fe2016-01-28 05:22:45 -08001270int32_t Channel::SetSendCodec(const CodecInst& codec) {
1271 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1272 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001273
kwibergc8d071e2016-04-06 12:22:38 -07001274 if (!codec_manager_.RegisterEncoder(codec) ||
1275 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001276 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1277 "SetSendCodec() failed to register codec to ACM");
1278 return -1;
1279 }
1280
1281 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1282 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1283 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1284 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1285 "SetSendCodec() failed to register codec to"
1286 " RTP/RTCP module");
1287 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001288 }
kwiberg55b97fe2016-01-28 05:22:45 -08001289 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001290
kwiberg55b97fe2016-01-28 05:22:45 -08001291 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001292}
1293
minyue78b4d562016-11-30 04:47:39 -08001294void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001295 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1296 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001297 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001298 if (*encoder) {
1299 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001300 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001301 }
1302 });
michaelt566d8202017-01-12 10:17:38 -08001303 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001304}
1305
elad.alond12a8e12017-03-23 11:04:48 -07001306void Channel::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
1307 if (!use_twcc_plr_for_ana_)
1308 return;
minyue7e304322016-10-12 05:00:55 -07001309 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
elad.alond12a8e12017-03-23 11:04:48 -07001310 if (*encoder) {
1311 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1312 }
1313 });
1314}
1315
elad.alondadb4dc2017-03-23 15:29:50 -07001316void Channel::OnRecoverableUplinkPacketLossRate(
1317 float recoverable_packet_loss_rate) {
1318 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1319 if (*encoder) {
1320 (*encoder)->OnReceivedUplinkRecoverablePacketLossFraction(
1321 recoverable_packet_loss_rate);
1322 }
1323 });
1324}
1325
elad.alond12a8e12017-03-23 11:04:48 -07001326void Channel::OnUplinkPacketLossRate(float packet_loss_rate) {
1327 if (use_twcc_plr_for_ana_)
1328 return;
1329 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1330 if (*encoder) {
1331 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1332 }
minyue7e304322016-10-12 05:00:55 -07001333 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001334}
1335
kwiberg55b97fe2016-01-28 05:22:45 -08001336int32_t Channel::SetVADStatus(bool enableVAD,
1337 ACMVADMode mode,
1338 bool disableDTX) {
1339 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1340 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001341 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1342 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1343 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001344 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1345 kTraceError,
1346 "SetVADStatus() failed to set VAD");
1347 return -1;
1348 }
1349 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001350}
1351
kwiberg55b97fe2016-01-28 05:22:45 -08001352int32_t Channel::GetVADStatus(bool& enabledVAD,
1353 ACMVADMode& mode,
1354 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001355 const auto* params = codec_manager_.GetStackParams();
1356 enabledVAD = params->use_cng;
1357 mode = params->vad_mode;
1358 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001359 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001360}
1361
kwiberg1724cfb2017-03-24 03:16:04 -07001362void Channel::SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) {
1363 rtp_payload_registry_->SetAudioReceivePayloads(codecs);
1364 audio_coding_->SetReceiveCodecs(codecs);
1365}
1366
kwiberg55b97fe2016-01-28 05:22:45 -08001367int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001368 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1369}
1370
1371int32_t Channel::SetRecPayloadType(int payload_type,
1372 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001373 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1374 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001375
kwiberg55b97fe2016-01-28 05:22:45 -08001376 if (channel_state_.Get().playing) {
1377 _engineStatisticsPtr->SetLastError(
1378 VE_ALREADY_PLAYING, kTraceError,
1379 "SetRecPayloadType() unable to set PT while playing");
1380 return -1;
1381 }
kwiberg55b97fe2016-01-28 05:22:45 -08001382
kwiberg09f090c2017-03-01 01:57:11 -08001383 const CodecInst codec = SdpToCodecInst(payload_type, format);
kwibergd32bf752017-01-19 07:03:59 -08001384
1385 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001386 // De-register the selected codec (RTP/RTCP module and ACM)
1387
1388 int8_t pltype(-1);
1389 CodecInst rxCodec = codec;
1390
1391 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001392 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001393 rxCodec.pltype = pltype;
1394
1395 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1396 _engineStatisticsPtr->SetLastError(
1397 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1398 "SetRecPayloadType() RTP/RTCP-module deregistration "
1399 "failed");
1400 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001401 }
kwiberg55b97fe2016-01-28 05:22:45 -08001402 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1403 _engineStatisticsPtr->SetLastError(
1404 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1405 "SetRecPayloadType() ACM deregistration failed - 1");
1406 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001407 }
kwiberg55b97fe2016-01-28 05:22:45 -08001408 return 0;
1409 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001410
magjed56124bd2016-11-24 09:34:46 -08001411 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001412 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001413 // TODO(kwiberg): Retrying is probably not necessary, since
1414 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001415 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001416 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001417 _engineStatisticsPtr->SetLastError(
1418 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1419 "SetRecPayloadType() RTP/RTCP-module registration failed");
1420 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001421 }
kwiberg55b97fe2016-01-28 05:22:45 -08001422 }
kwibergd32bf752017-01-19 07:03:59 -08001423 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1424 audio_coding_->UnregisterReceiveCodec(payload_type);
1425 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001426 _engineStatisticsPtr->SetLastError(
1427 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1428 "SetRecPayloadType() ACM registration failed - 1");
1429 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001430 }
kwiberg55b97fe2016-01-28 05:22:45 -08001431 }
1432 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001433}
1434
kwiberg55b97fe2016-01-28 05:22:45 -08001435int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1436 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001437 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001438 _engineStatisticsPtr->SetLastError(
1439 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1440 "GetRecPayloadType() failed to retrieve RX payload type");
1441 return -1;
1442 }
1443 codec.pltype = payloadType;
1444 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001445}
1446
kwiberg55b97fe2016-01-28 05:22:45 -08001447int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1448 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1449 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001450
kwiberg55b97fe2016-01-28 05:22:45 -08001451 CodecInst codec;
1452 int32_t samplingFreqHz(-1);
1453 const size_t kMono = 1;
1454 if (frequency == kFreq32000Hz)
1455 samplingFreqHz = 32000;
1456 else if (frequency == kFreq16000Hz)
1457 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001458
kwiberg55b97fe2016-01-28 05:22:45 -08001459 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1460 _engineStatisticsPtr->SetLastError(
1461 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1462 "SetSendCNPayloadType() failed to retrieve default CN codec "
1463 "settings");
1464 return -1;
1465 }
1466
1467 // Modify the payload type (must be set to dynamic range)
1468 codec.pltype = type;
1469
kwibergc8d071e2016-04-06 12:22:38 -07001470 if (!codec_manager_.RegisterEncoder(codec) ||
1471 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001472 _engineStatisticsPtr->SetLastError(
1473 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1474 "SetSendCNPayloadType() failed to register CN to ACM");
1475 return -1;
1476 }
1477
1478 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1479 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1480 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1481 _engineStatisticsPtr->SetLastError(
1482 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1483 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1484 "module");
1485 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001486 }
kwiberg55b97fe2016-01-28 05:22:45 -08001487 }
1488 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001489}
1490
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001491int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001492 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001493 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001494
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001495 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001496 _engineStatisticsPtr->SetLastError(
1497 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001498 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001499 return -1;
1500 }
1501 return 0;
1502}
1503
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001504int Channel::SetOpusDtx(bool enable_dtx) {
1505 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1506 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001507 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001508 : audio_coding_->DisableOpusDtx();
1509 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001510 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1511 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001512 return -1;
1513 }
1514 return 0;
1515}
1516
ivoc85228d62016-07-27 04:53:47 -07001517int Channel::GetOpusDtx(bool* enabled) {
1518 int success = -1;
1519 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1520 if (encoder) {
1521 *enabled = encoder->GetDtx();
1522 success = 0;
1523 }
1524 });
1525 return success;
1526}
1527
minyue7e304322016-10-12 05:00:55 -07001528bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1529 bool success = false;
1530 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1531 if (*encoder) {
1532 success = (*encoder)->EnableAudioNetworkAdaptor(
michaeltbf279fc2017-01-13 06:02:29 -08001533 config_string, event_log_proxy_.get(), Clock::GetRealTimeClock());
minyue7e304322016-10-12 05:00:55 -07001534 }
1535 });
1536 return success;
1537}
1538
1539void Channel::DisableAudioNetworkAdaptor() {
1540 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1541 if (*encoder)
1542 (*encoder)->DisableAudioNetworkAdaptor();
1543 });
1544}
1545
1546void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1547 int max_frame_length_ms) {
1548 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1549 if (*encoder) {
1550 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1551 max_frame_length_ms);
1552 }
1553 });
1554}
1555
mflodman3d7db262016-04-29 00:57:13 -07001556int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001557 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001558 "Channel::RegisterExternalTransport()");
1559
kwiberg55b97fe2016-01-28 05:22:45 -08001560 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001561 if (_externalTransport) {
1562 _engineStatisticsPtr->SetLastError(
1563 VE_INVALID_OPERATION, kTraceError,
1564 "RegisterExternalTransport() external transport already enabled");
1565 return -1;
1566 }
1567 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001568 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001569 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001570}
1571
kwiberg55b97fe2016-01-28 05:22:45 -08001572int32_t Channel::DeRegisterExternalTransport() {
1573 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1574 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001575
kwiberg55b97fe2016-01-28 05:22:45 -08001576 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001577 if (_transportPtr) {
1578 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1579 "DeRegisterExternalTransport() all transport is disabled");
1580 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001581 _engineStatisticsPtr->SetLastError(
1582 VE_INVALID_OPERATION, kTraceWarning,
1583 "DeRegisterExternalTransport() external transport already "
1584 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001585 }
1586 _externalTransport = false;
1587 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001588 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001589}
1590
nisse657bab22017-02-21 06:28:10 -08001591// TODO(nisse): Delete this method together with ReceivedRTPPacket.
1592// It's a temporary hack to support both ReceivedRTPPacket and
1593// OnRtpPacket interfaces without too much code duplication.
1594bool Channel::OnRtpPacketWithHeader(const uint8_t* received_packet,
1595 size_t length,
1596 RTPHeader *header) {
1597 // Store playout timestamp for the received RTP packet
1598 UpdatePlayoutTimestamp(false);
1599
1600 header->payload_type_frequency =
1601 rtp_payload_registry_->GetPayloadTypeFrequency(header->payloadType);
1602 if (header->payload_type_frequency < 0)
1603 return false;
1604 bool in_order = IsPacketInOrder(*header);
1605 rtp_receive_statistics_->IncomingPacket(
1606 *header, length, IsPacketRetransmitted(*header, in_order));
1607 rtp_payload_registry_->SetIncomingPayloadType(*header);
1608
1609 return ReceivePacket(received_packet, length, *header, in_order);
1610}
1611
mflodman3d7db262016-04-29 00:57:13 -07001612int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001613 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001614 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001615 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001616 "Channel::ReceivedRTPPacket()");
1617
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001618 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001619 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1620 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1621 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001622 return -1;
1623 }
nisse657bab22017-02-21 06:28:10 -08001624 return OnRtpPacketWithHeader(received_packet, length, &header) ? 0 : -1;
1625}
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001626
nisse657bab22017-02-21 06:28:10 -08001627void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
1628 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
1629 "Channel::ReceivedRTPPacket()");
1630
1631 RTPHeader header;
1632 packet.GetHeader(&header);
1633 OnRtpPacketWithHeader(packet.data(), packet.size(), &header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001634}
1635
1636bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001637 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001638 const RTPHeader& header,
1639 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001640 if (rtp_payload_registry_->IsRtx(header)) {
1641 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001642 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001643 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001644 assert(packet_length >= header.headerLength);
1645 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001646 PayloadUnion payload_specific;
1647 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001648 &payload_specific)) {
1649 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001650 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001651 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1652 payload_specific, in_order);
1653}
1654
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001655bool Channel::HandleRtxPacket(const uint8_t* packet,
1656 size_t packet_length,
1657 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001658 if (!rtp_payload_registry_->IsRtx(header))
1659 return false;
1660
1661 // Remove the RTX header and parse the original RTP header.
1662 if (packet_length < header.headerLength)
1663 return false;
1664 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1665 return false;
1666 if (restored_packet_in_use_) {
1667 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1668 "Multiple RTX headers detected, dropping packet");
1669 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001670 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001671 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001672 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1673 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001674 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1675 "Incoming RTX packet: invalid RTP header");
1676 return false;
1677 }
1678 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001679 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001680 restored_packet_in_use_ = false;
1681 return ret;
1682}
1683
1684bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1685 StreamStatistician* statistician =
1686 rtp_receive_statistics_->GetStatistician(header.ssrc);
1687 if (!statistician)
1688 return false;
1689 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001690}
1691
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001692bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1693 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001694 // Retransmissions are handled separately if RTX is enabled.
1695 if (rtp_payload_registry_->RtxEnabled())
1696 return false;
1697 StreamStatistician* statistician =
1698 rtp_receive_statistics_->GetStatistician(header.ssrc);
1699 if (!statistician)
1700 return false;
1701 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001702 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001703 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001704 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001705}
1706
mflodman3d7db262016-04-29 00:57:13 -07001707int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001708 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001709 "Channel::ReceivedRTCPPacket()");
1710 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001711 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001712
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001713 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001714 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001715 _engineStatisticsPtr->SetLastError(
1716 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1717 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1718 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001719
Minyue2013aec2015-05-13 14:14:42 +02001720 int64_t rtt = GetRTT(true);
1721 if (rtt == 0) {
1722 // Waiting for valid RTT.
1723 return 0;
1724 }
Erik Språng737336d2016-07-29 12:59:36 +02001725
1726 int64_t nack_window_ms = rtt;
1727 if (nack_window_ms < kMinRetransmissionWindowMs) {
1728 nack_window_ms = kMinRetransmissionWindowMs;
1729 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1730 nack_window_ms = kMaxRetransmissionWindowMs;
1731 }
1732 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1733
minyue7e304322016-10-12 05:00:55 -07001734 // Invoke audio encoders OnReceivedRtt().
1735 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1736 if (*encoder)
1737 (*encoder)->OnReceivedRtt(rtt);
1738 });
1739
Minyue2013aec2015-05-13 14:14:42 +02001740 uint32_t ntp_secs = 0;
1741 uint32_t ntp_frac = 0;
1742 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001743 if (0 !=
1744 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1745 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001746 // Waiting for RTCP.
1747 return 0;
1748 }
1749
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001750 {
tommi31fc21f2016-01-21 10:37:37 -08001751 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001752 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001753 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001754 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001755}
1756
niklase@google.com470e71d2011-07-07 08:21:25 +00001757int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001758 bool loop,
1759 FileFormats format,
1760 int startPosition,
1761 float volumeScaling,
1762 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001763 const CodecInst* codecInst) {
1764 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1765 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1766 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1767 "stopPosition=%d)",
1768 fileName, loop, format, volumeScaling, startPosition,
1769 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001770
kwiberg55b97fe2016-01-28 05:22:45 -08001771 if (channel_state_.Get().output_file_playing) {
1772 _engineStatisticsPtr->SetLastError(
1773 VE_ALREADY_PLAYING, kTraceError,
1774 "StartPlayingFileLocally() is already playing");
1775 return -1;
1776 }
1777
1778 {
1779 rtc::CritScope cs(&_fileCritSect);
1780
kwiberg5a25d952016-08-17 07:31:12 -07001781 if (output_file_player_) {
1782 output_file_player_->RegisterModuleFileCallback(NULL);
1783 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001784 }
1785
kwiberg5b356f42016-09-08 04:32:33 -07001786 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001787 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001788
kwiberg5a25d952016-08-17 07:31:12 -07001789 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001790 _engineStatisticsPtr->SetLastError(
1791 VE_INVALID_ARGUMENT, kTraceError,
1792 "StartPlayingFileLocally() filePlayer format is not correct");
1793 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001794 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001795
kwiberg55b97fe2016-01-28 05:22:45 -08001796 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001797
kwiberg5a25d952016-08-17 07:31:12 -07001798 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001799 fileName, loop, startPosition, volumeScaling, notificationTime,
1800 stopPosition, (const CodecInst*)codecInst) != 0) {
1801 _engineStatisticsPtr->SetLastError(
1802 VE_BAD_FILE, kTraceError,
1803 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001804 output_file_player_->StopPlayingFile();
1805 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001806 return -1;
1807 }
kwiberg5a25d952016-08-17 07:31:12 -07001808 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001809 channel_state_.SetOutputFilePlaying(true);
1810 }
1811
1812 if (RegisterFilePlayingToMixer() != 0)
1813 return -1;
1814
1815 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001816}
1817
1818int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001819 FileFormats format,
1820 int startPosition,
1821 float volumeScaling,
1822 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001823 const CodecInst* codecInst) {
1824 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1825 "Channel::StartPlayingFileLocally(format=%d,"
1826 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1827 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001828
kwiberg55b97fe2016-01-28 05:22:45 -08001829 if (stream == NULL) {
1830 _engineStatisticsPtr->SetLastError(
1831 VE_BAD_FILE, kTraceError,
1832 "StartPlayingFileLocally() NULL as input stream");
1833 return -1;
1834 }
1835
1836 if (channel_state_.Get().output_file_playing) {
1837 _engineStatisticsPtr->SetLastError(
1838 VE_ALREADY_PLAYING, kTraceError,
1839 "StartPlayingFileLocally() is already playing");
1840 return -1;
1841 }
1842
1843 {
1844 rtc::CritScope cs(&_fileCritSect);
1845
1846 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001847 if (output_file_player_) {
1848 output_file_player_->RegisterModuleFileCallback(NULL);
1849 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001850 }
1851
kwiberg55b97fe2016-01-28 05:22:45 -08001852 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001853 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001854 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001855
kwiberg5a25d952016-08-17 07:31:12 -07001856 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001857 _engineStatisticsPtr->SetLastError(
1858 VE_INVALID_ARGUMENT, kTraceError,
1859 "StartPlayingFileLocally() filePlayer format isnot correct");
1860 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001861 }
1862
kwiberg55b97fe2016-01-28 05:22:45 -08001863 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001864
kwiberg4ec01d92016-08-22 08:43:54 -07001865 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001866 volumeScaling, notificationTime,
1867 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001868 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1869 "StartPlayingFile() failed to "
1870 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001871 output_file_player_->StopPlayingFile();
1872 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001873 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001874 }
kwiberg5a25d952016-08-17 07:31:12 -07001875 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001876 channel_state_.SetOutputFilePlaying(true);
1877 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001878
kwiberg55b97fe2016-01-28 05:22:45 -08001879 if (RegisterFilePlayingToMixer() != 0)
1880 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001881
kwiberg55b97fe2016-01-28 05:22:45 -08001882 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001883}
1884
kwiberg55b97fe2016-01-28 05:22:45 -08001885int Channel::StopPlayingFileLocally() {
1886 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1887 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001888
kwiberg55b97fe2016-01-28 05:22:45 -08001889 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001890 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001891 }
1892
1893 {
1894 rtc::CritScope cs(&_fileCritSect);
1895
kwiberg5a25d952016-08-17 07:31:12 -07001896 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001897 _engineStatisticsPtr->SetLastError(
1898 VE_STOP_RECORDING_FAILED, kTraceError,
1899 "StopPlayingFile() could not stop playing");
1900 return -1;
1901 }
kwiberg5a25d952016-08-17 07:31:12 -07001902 output_file_player_->RegisterModuleFileCallback(NULL);
1903 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001904 channel_state_.SetOutputFilePlaying(false);
1905 }
1906 // _fileCritSect cannot be taken while calling
1907 // SetAnonymousMixibilityStatus. Refer to comments in
1908 // StartPlayingFileLocally(const char* ...) for more details.
1909 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1910 _engineStatisticsPtr->SetLastError(
1911 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1912 "StopPlayingFile() failed to stop participant from playing as"
1913 "file in the mixer");
1914 return -1;
1915 }
1916
1917 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001918}
1919
kwiberg55b97fe2016-01-28 05:22:45 -08001920int Channel::IsPlayingFileLocally() const {
1921 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001922}
1923
kwiberg55b97fe2016-01-28 05:22:45 -08001924int Channel::RegisterFilePlayingToMixer() {
1925 // Return success for not registering for file playing to mixer if:
1926 // 1. playing file before playout is started on that channel.
1927 // 2. starting playout without file playing on that channel.
1928 if (!channel_state_.Get().playing ||
1929 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001930 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001931 }
1932
1933 // |_fileCritSect| cannot be taken while calling
1934 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1935 // frames can be pulled by the mixer. Since the frames are generated from
1936 // the file, _fileCritSect will be taken. This would result in a deadlock.
1937 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1938 channel_state_.SetOutputFilePlaying(false);
1939 rtc::CritScope cs(&_fileCritSect);
1940 _engineStatisticsPtr->SetLastError(
1941 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1942 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001943 output_file_player_->StopPlayingFile();
1944 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001945 return -1;
1946 }
1947
1948 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001949}
1950
niklase@google.com470e71d2011-07-07 08:21:25 +00001951int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001952 bool loop,
1953 FileFormats format,
1954 int startPosition,
1955 float volumeScaling,
1956 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001957 const CodecInst* codecInst) {
1958 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1959 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1960 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1961 "stopPosition=%d)",
1962 fileName, loop, format, volumeScaling, startPosition,
1963 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001964
kwiberg55b97fe2016-01-28 05:22:45 -08001965 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001966
kwiberg55b97fe2016-01-28 05:22:45 -08001967 if (channel_state_.Get().input_file_playing) {
1968 _engineStatisticsPtr->SetLastError(
1969 VE_ALREADY_PLAYING, kTraceWarning,
1970 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001971 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001972 }
1973
1974 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001975 if (input_file_player_) {
1976 input_file_player_->RegisterModuleFileCallback(NULL);
1977 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001978 }
1979
1980 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001981 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001982 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001983
kwiberg5a25d952016-08-17 07:31:12 -07001984 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001985 _engineStatisticsPtr->SetLastError(
1986 VE_INVALID_ARGUMENT, kTraceError,
1987 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1988 return -1;
1989 }
1990
1991 const uint32_t notificationTime(0);
1992
kwiberg5a25d952016-08-17 07:31:12 -07001993 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001994 fileName, loop, startPosition, volumeScaling, notificationTime,
1995 stopPosition, (const CodecInst*)codecInst) != 0) {
1996 _engineStatisticsPtr->SetLastError(
1997 VE_BAD_FILE, kTraceError,
1998 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001999 input_file_player_->StopPlayingFile();
2000 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002001 return -1;
2002 }
kwiberg5a25d952016-08-17 07:31:12 -07002003 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002004 channel_state_.SetInputFilePlaying(true);
2005
2006 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002007}
2008
2009int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002010 FileFormats format,
2011 int startPosition,
2012 float volumeScaling,
2013 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002014 const CodecInst* codecInst) {
2015 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2016 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2017 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2018 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002019
kwiberg55b97fe2016-01-28 05:22:45 -08002020 if (stream == NULL) {
2021 _engineStatisticsPtr->SetLastError(
2022 VE_BAD_FILE, kTraceError,
2023 "StartPlayingFileAsMicrophone NULL as input stream");
2024 return -1;
2025 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002026
kwiberg55b97fe2016-01-28 05:22:45 -08002027 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002028
kwiberg55b97fe2016-01-28 05:22:45 -08002029 if (channel_state_.Get().input_file_playing) {
2030 _engineStatisticsPtr->SetLastError(
2031 VE_ALREADY_PLAYING, kTraceWarning,
2032 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002033 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002034 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002035
kwiberg55b97fe2016-01-28 05:22:45 -08002036 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002037 if (input_file_player_) {
2038 input_file_player_->RegisterModuleFileCallback(NULL);
2039 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002040 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002041
kwiberg55b97fe2016-01-28 05:22:45 -08002042 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002043 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002044 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002045
kwiberg5a25d952016-08-17 07:31:12 -07002046 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002047 _engineStatisticsPtr->SetLastError(
2048 VE_INVALID_ARGUMENT, kTraceError,
2049 "StartPlayingInputFile() filePlayer format isnot correct");
2050 return -1;
2051 }
2052
2053 const uint32_t notificationTime(0);
2054
kwiberg4ec01d92016-08-22 08:43:54 -07002055 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2056 notificationTime, stopPosition,
2057 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002058 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2059 "StartPlayingFile() failed to start "
2060 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002061 input_file_player_->StopPlayingFile();
2062 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002063 return -1;
2064 }
2065
kwiberg5a25d952016-08-17 07:31:12 -07002066 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002067 channel_state_.SetInputFilePlaying(true);
2068
2069 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002070}
2071
kwiberg55b97fe2016-01-28 05:22:45 -08002072int Channel::StopPlayingFileAsMicrophone() {
2073 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2074 "Channel::StopPlayingFileAsMicrophone()");
2075
2076 rtc::CritScope cs(&_fileCritSect);
2077
2078 if (!channel_state_.Get().input_file_playing) {
2079 return 0;
2080 }
2081
kwiberg5a25d952016-08-17 07:31:12 -07002082 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002083 _engineStatisticsPtr->SetLastError(
2084 VE_STOP_RECORDING_FAILED, kTraceError,
2085 "StopPlayingFile() could not stop playing");
2086 return -1;
2087 }
kwiberg5a25d952016-08-17 07:31:12 -07002088 input_file_player_->RegisterModuleFileCallback(NULL);
2089 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002090 channel_state_.SetInputFilePlaying(false);
2091
2092 return 0;
2093}
2094
2095int Channel::IsPlayingFileAsMicrophone() const {
2096 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002097}
2098
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002099int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002100 const CodecInst* codecInst) {
2101 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2102 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002103
kwiberg55b97fe2016-01-28 05:22:45 -08002104 if (_outputFileRecording) {
2105 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2106 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002107 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002108 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002109
kwiberg55b97fe2016-01-28 05:22:45 -08002110 FileFormats format;
2111 const uint32_t notificationTime(0); // Not supported in VoE
2112 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002113
kwiberg55b97fe2016-01-28 05:22:45 -08002114 if ((codecInst != NULL) &&
2115 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2116 _engineStatisticsPtr->SetLastError(
2117 VE_BAD_ARGUMENT, kTraceError,
2118 "StartRecordingPlayout() invalid compression");
2119 return (-1);
2120 }
2121 if (codecInst == NULL) {
2122 format = kFileFormatPcm16kHzFile;
2123 codecInst = &dummyCodec;
2124 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2125 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2126 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2127 format = kFileFormatWavFile;
2128 } else {
2129 format = kFileFormatCompressedFile;
2130 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002131
kwiberg55b97fe2016-01-28 05:22:45 -08002132 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002133
kwiberg55b97fe2016-01-28 05:22:45 -08002134 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002135 if (output_file_recorder_) {
2136 output_file_recorder_->RegisterModuleFileCallback(NULL);
2137 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002138 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002139
kwiberg5a25d952016-08-17 07:31:12 -07002140 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002141 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002142 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002143 _engineStatisticsPtr->SetLastError(
2144 VE_INVALID_ARGUMENT, kTraceError,
2145 "StartRecordingPlayout() fileRecorder format isnot correct");
2146 return -1;
2147 }
2148
kwiberg5a25d952016-08-17 07:31:12 -07002149 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002150 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2151 _engineStatisticsPtr->SetLastError(
2152 VE_BAD_FILE, kTraceError,
2153 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002154 output_file_recorder_->StopRecording();
2155 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002156 return -1;
2157 }
kwiberg5a25d952016-08-17 07:31:12 -07002158 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002159 _outputFileRecording = true;
2160
2161 return 0;
2162}
2163
2164int Channel::StartRecordingPlayout(OutStream* stream,
2165 const CodecInst* codecInst) {
2166 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2167 "Channel::StartRecordingPlayout()");
2168
2169 if (_outputFileRecording) {
2170 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2171 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002172 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002173 }
2174
2175 FileFormats format;
2176 const uint32_t notificationTime(0); // Not supported in VoE
2177 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2178
2179 if (codecInst != NULL && codecInst->channels != 1) {
2180 _engineStatisticsPtr->SetLastError(
2181 VE_BAD_ARGUMENT, kTraceError,
2182 "StartRecordingPlayout() invalid compression");
2183 return (-1);
2184 }
2185 if (codecInst == NULL) {
2186 format = kFileFormatPcm16kHzFile;
2187 codecInst = &dummyCodec;
2188 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2189 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2190 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2191 format = kFileFormatWavFile;
2192 } else {
2193 format = kFileFormatCompressedFile;
2194 }
2195
2196 rtc::CritScope cs(&_fileCritSect);
2197
2198 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002199 if (output_file_recorder_) {
2200 output_file_recorder_->RegisterModuleFileCallback(NULL);
2201 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002202 }
2203
kwiberg5a25d952016-08-17 07:31:12 -07002204 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002205 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002206 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002207 _engineStatisticsPtr->SetLastError(
2208 VE_INVALID_ARGUMENT, kTraceError,
2209 "StartRecordingPlayout() fileRecorder format isnot correct");
2210 return -1;
2211 }
2212
kwiberg4ec01d92016-08-22 08:43:54 -07002213 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002214 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002215 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2216 "StartRecordingPlayout() failed to "
2217 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002218 output_file_recorder_->StopRecording();
2219 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002220 return -1;
2221 }
2222
kwiberg5a25d952016-08-17 07:31:12 -07002223 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002224 _outputFileRecording = true;
2225
2226 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002227}
2228
kwiberg55b97fe2016-01-28 05:22:45 -08002229int Channel::StopRecordingPlayout() {
2230 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2231 "Channel::StopRecordingPlayout()");
2232
2233 if (!_outputFileRecording) {
2234 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2235 "StopRecordingPlayout() isnot recording");
2236 return -1;
2237 }
2238
2239 rtc::CritScope cs(&_fileCritSect);
2240
kwiberg5a25d952016-08-17 07:31:12 -07002241 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002242 _engineStatisticsPtr->SetLastError(
2243 VE_STOP_RECORDING_FAILED, kTraceError,
2244 "StopRecording() could not stop recording");
2245 return (-1);
2246 }
kwiberg5a25d952016-08-17 07:31:12 -07002247 output_file_recorder_->RegisterModuleFileCallback(NULL);
2248 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002249 _outputFileRecording = false;
2250
2251 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002252}
2253
kwiberg55b97fe2016-01-28 05:22:45 -08002254void Channel::SetMixWithMicStatus(bool mix) {
2255 rtc::CritScope cs(&_fileCritSect);
2256 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002257}
2258
solenberg8d73f8c2017-03-08 01:52:20 -08002259int Channel::GetSpeechOutputLevel() const {
2260 return _outputAudioLevel.Level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002261}
2262
solenberg8d73f8c2017-03-08 01:52:20 -08002263int Channel::GetSpeechOutputLevelFullRange() const {
2264 return _outputAudioLevel.LevelFullRange();
kwiberg55b97fe2016-01-28 05:22:45 -08002265}
2266
solenberg8d73f8c2017-03-08 01:52:20 -08002267void Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002268 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002269 input_mute_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002270}
2271
solenberg1c2af8e2016-03-24 10:36:00 -07002272bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002273 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002274 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002275}
2276
solenberg8d73f8c2017-03-08 01:52:20 -08002277void Channel::SetChannelOutputVolumeScaling(float scaling) {
kwiberg55b97fe2016-01-28 05:22:45 -08002278 rtc::CritScope cs(&volume_settings_critsect_);
kwiberg55b97fe2016-01-28 05:22:45 -08002279 _outputGain = scaling;
niklase@google.com470e71d2011-07-07 08:21:25 +00002280}
2281
solenberg8842c3e2016-03-11 03:06:41 -08002282int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002283 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002284 "Channel::SendTelephoneEventOutband(...)");
2285 RTC_DCHECK_LE(0, event);
2286 RTC_DCHECK_GE(255, event);
2287 RTC_DCHECK_LE(0, duration_ms);
2288 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002289 if (!Sending()) {
2290 return -1;
2291 }
solenberg8842c3e2016-03-11 03:06:41 -08002292 if (_rtpRtcpModule->SendTelephoneEventOutband(
2293 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002294 _engineStatisticsPtr->SetLastError(
2295 VE_SEND_DTMF_FAILED, kTraceWarning,
2296 "SendTelephoneEventOutband() failed to send event");
2297 return -1;
2298 }
2299 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002300}
2301
solenbergffbbcac2016-11-17 05:25:37 -08002302int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2303 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002304 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002305 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002306 RTC_DCHECK_LE(0, payload_type);
2307 RTC_DCHECK_GE(127, payload_type);
2308 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002309 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002310 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002311 memcpy(codec.plname, "telephone-event", 16);
2312 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2313 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2314 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2315 _engineStatisticsPtr->SetLastError(
2316 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2317 "SetSendTelephoneEventPayloadType() failed to register send"
2318 "payload type");
2319 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002320 }
kwiberg55b97fe2016-01-28 05:22:45 -08002321 }
kwiberg55b97fe2016-01-28 05:22:45 -08002322 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002323}
2324
kwiberg55b97fe2016-01-28 05:22:45 -08002325int Channel::SetLocalSSRC(unsigned int ssrc) {
2326 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2327 "Channel::SetLocalSSRC()");
2328 if (channel_state_.Get().sending) {
2329 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2330 "SetLocalSSRC() already sending");
2331 return -1;
2332 }
2333 _rtpRtcpModule->SetSSRC(ssrc);
2334 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002335}
2336
kwiberg55b97fe2016-01-28 05:22:45 -08002337int Channel::GetLocalSSRC(unsigned int& ssrc) {
2338 ssrc = _rtpRtcpModule->SSRC();
2339 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002340}
2341
kwiberg55b97fe2016-01-28 05:22:45 -08002342int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2343 ssrc = rtp_receiver_->SSRC();
2344 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002345}
2346
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002347int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002348 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002349 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002350}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002351
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002352int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2353 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002354 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2355 if (enable &&
2356 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2357 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002358 return -1;
2359 }
2360 return 0;
2361}
2362
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002363void Channel::EnableSendTransportSequenceNumber(int id) {
2364 int ret =
2365 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2366 RTC_DCHECK_EQ(0, ret);
2367}
2368
stefan3313ec92016-01-21 06:32:43 -08002369void Channel::EnableReceiveTransportSequenceNumber(int id) {
2370 rtp_header_parser_->DeregisterRtpHeaderExtension(
2371 kRtpExtensionTransportSequenceNumber);
2372 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2373 kRtpExtensionTransportSequenceNumber, id);
2374 RTC_DCHECK(ret);
2375}
2376
stefanbba9dec2016-02-01 04:39:55 -08002377void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002378 RtpPacketSender* rtp_packet_sender,
2379 TransportFeedbackObserver* transport_feedback_observer,
stefan7de8d642017-02-07 07:14:08 -08002380 PacketRouter* packet_router,
2381 RtcpBandwidthObserver* bandwidth_observer) {
stefanbba9dec2016-02-01 04:39:55 -08002382 RTC_DCHECK(rtp_packet_sender);
2383 RTC_DCHECK(transport_feedback_observer);
2384 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002385 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002386 feedback_observer_proxy_->SetTransportFeedbackObserver(
2387 transport_feedback_observer);
2388 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2389 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2390 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002391 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002392 packet_router_ = packet_router;
2393}
2394
stefanbba9dec2016-02-01 04:39:55 -08002395void Channel::RegisterReceiverCongestionControlObjects(
2396 PacketRouter* packet_router) {
2397 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002398 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002399 packet_router_ = packet_router;
2400}
2401
2402void Channel::ResetCongestionControlObjects() {
2403 RTC_DCHECK(packet_router_);
2404 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002405 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002406 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2407 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002408 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002409 packet_router_ = nullptr;
2410 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2411}
2412
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002413void Channel::SetRTCPStatus(bool enable) {
2414 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2415 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002416 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002417}
2418
kwiberg55b97fe2016-01-28 05:22:45 -08002419int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002420 RtcpMode method = _rtpRtcpModule->RTCP();
2421 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002422 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002423}
2424
kwiberg55b97fe2016-01-28 05:22:45 -08002425int Channel::SetRTCP_CNAME(const char cName[256]) {
2426 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2427 "Channel::SetRTCP_CNAME()");
2428 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2429 _engineStatisticsPtr->SetLastError(
2430 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2431 "SetRTCP_CNAME() failed to set RTCP CNAME");
2432 return -1;
2433 }
2434 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002435}
2436
kwiberg55b97fe2016-01-28 05:22:45 -08002437int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2438 if (cName == NULL) {
2439 _engineStatisticsPtr->SetLastError(
2440 VE_INVALID_ARGUMENT, kTraceError,
2441 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2442 return -1;
2443 }
2444 char cname[RTCP_CNAME_SIZE];
2445 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2446 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2447 _engineStatisticsPtr->SetLastError(
2448 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2449 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2450 return -1;
2451 }
2452 strcpy(cName, cname);
2453 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002454}
2455
kwiberg55b97fe2016-01-28 05:22:45 -08002456int Channel::SendApplicationDefinedRTCPPacket(
2457 unsigned char subType,
2458 unsigned int name,
2459 const char* data,
2460 unsigned short dataLengthInBytes) {
2461 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2462 "Channel::SendApplicationDefinedRTCPPacket()");
2463 if (!channel_state_.Get().sending) {
2464 _engineStatisticsPtr->SetLastError(
2465 VE_NOT_SENDING, kTraceError,
2466 "SendApplicationDefinedRTCPPacket() not sending");
2467 return -1;
2468 }
2469 if (NULL == data) {
2470 _engineStatisticsPtr->SetLastError(
2471 VE_INVALID_ARGUMENT, kTraceError,
2472 "SendApplicationDefinedRTCPPacket() invalid data value");
2473 return -1;
2474 }
2475 if (dataLengthInBytes % 4 != 0) {
2476 _engineStatisticsPtr->SetLastError(
2477 VE_INVALID_ARGUMENT, kTraceError,
2478 "SendApplicationDefinedRTCPPacket() invalid length value");
2479 return -1;
2480 }
2481 RtcpMode status = _rtpRtcpModule->RTCP();
2482 if (status == RtcpMode::kOff) {
2483 _engineStatisticsPtr->SetLastError(
2484 VE_RTCP_ERROR, kTraceError,
2485 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2486 return -1;
2487 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002488
kwiberg55b97fe2016-01-28 05:22:45 -08002489 // Create and schedule the RTCP APP packet for transmission
2490 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2491 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2492 _engineStatisticsPtr->SetLastError(
2493 VE_SEND_ERROR, kTraceError,
2494 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2495 return -1;
2496 }
2497 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002498}
2499
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002500int Channel::GetRemoteRTCPReportBlocks(
2501 std::vector<ReportBlock>* report_blocks) {
2502 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002503 _engineStatisticsPtr->SetLastError(
2504 VE_INVALID_ARGUMENT, kTraceError,
2505 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002506 return -1;
2507 }
2508
2509 // Get the report blocks from the latest received RTCP Sender or Receiver
2510 // Report. Each element in the vector contains the sender's SSRC and a
2511 // report block according to RFC 3550.
2512 std::vector<RTCPReportBlock> rtcp_report_blocks;
2513 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002514 return -1;
2515 }
2516
2517 if (rtcp_report_blocks.empty())
2518 return 0;
2519
2520 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2521 for (; it != rtcp_report_blocks.end(); ++it) {
2522 ReportBlock report_block;
2523 report_block.sender_SSRC = it->remoteSSRC;
2524 report_block.source_SSRC = it->sourceSSRC;
2525 report_block.fraction_lost = it->fractionLost;
2526 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2527 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2528 report_block.interarrival_jitter = it->jitter;
2529 report_block.last_SR_timestamp = it->lastSR;
2530 report_block.delay_since_last_SR = it->delaySinceLastSR;
2531 report_blocks->push_back(report_block);
2532 }
2533 return 0;
2534}
2535
kwiberg55b97fe2016-01-28 05:22:45 -08002536int Channel::GetRTPStatistics(CallStatistics& stats) {
2537 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002538
kwiberg55b97fe2016-01-28 05:22:45 -08002539 // The jitter statistics is updated for each received RTP packet and is
2540 // based on received packets.
2541 RtcpStatistics statistics;
2542 StreamStatistician* statistician =
2543 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002544 if (statistician) {
2545 statistician->GetStatistics(&statistics,
2546 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002547 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002548
kwiberg55b97fe2016-01-28 05:22:45 -08002549 stats.fractionLost = statistics.fraction_lost;
2550 stats.cumulativeLost = statistics.cumulative_lost;
2551 stats.extendedMax = statistics.extended_max_sequence_number;
2552 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002553
kwiberg55b97fe2016-01-28 05:22:45 -08002554 // --- RTT
2555 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002556
kwiberg55b97fe2016-01-28 05:22:45 -08002557 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002558
kwiberg55b97fe2016-01-28 05:22:45 -08002559 size_t bytesSent(0);
2560 uint32_t packetsSent(0);
2561 size_t bytesReceived(0);
2562 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002563
kwiberg55b97fe2016-01-28 05:22:45 -08002564 if (statistician) {
2565 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2566 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002567
kwiberg55b97fe2016-01-28 05:22:45 -08002568 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2569 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2570 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2571 " output will not be complete");
2572 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002573
kwiberg55b97fe2016-01-28 05:22:45 -08002574 stats.bytesSent = bytesSent;
2575 stats.packetsSent = packetsSent;
2576 stats.bytesReceived = bytesReceived;
2577 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002578
kwiberg55b97fe2016-01-28 05:22:45 -08002579 // --- Timestamps
2580 {
2581 rtc::CritScope lock(&ts_stats_lock_);
2582 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2583 }
2584 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002585}
2586
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002587int Channel::SetCodecFECStatus(bool enable) {
2588 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2589 "Channel::SetCodecFECStatus()");
2590
kwibergc8d071e2016-04-06 12:22:38 -07002591 if (!codec_manager_.SetCodecFEC(enable) ||
2592 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002593 _engineStatisticsPtr->SetLastError(
2594 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2595 "SetCodecFECStatus() failed to set FEC state");
2596 return -1;
2597 }
2598 return 0;
2599}
2600
2601bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002602 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002603}
2604
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002605void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2606 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002607 // If pacing is enabled we always store packets.
2608 if (!pacing_enabled_)
2609 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002610 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002611 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002612 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002613 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002614 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002615}
2616
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002617// Called when we are missing one or more packets.
2618int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002619 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2620}
2621
kwiberg55b97fe2016-01-28 05:22:45 -08002622uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2623 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2624 "Channel::Demultiplex()");
2625 _audioFrame.CopyFrom(audioFrame);
2626 _audioFrame.id_ = _channelId;
2627 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002628}
2629
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002630void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002631 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002632 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002633 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002634 CodecInst codec;
2635 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002636
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002637 // Never upsample or upmix the capture signal here. This should be done at the
2638 // end of the send chain.
2639 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2640 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2641 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2642 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002643}
2644
kwiberg55b97fe2016-01-28 05:22:45 -08002645uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2646 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2647 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002648
kwiberg55b97fe2016-01-28 05:22:45 -08002649 if (_audioFrame.samples_per_channel_ == 0) {
2650 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2651 "Channel::PrepareEncodeAndSend() invalid audio frame");
2652 return 0xFFFFFFFF;
2653 }
2654
2655 if (channel_state_.Get().input_file_playing) {
2656 MixOrReplaceAudioWithFile(mixingFrequency);
2657 }
2658
solenberg1c2af8e2016-03-24 10:36:00 -07002659 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2660 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002661
kwiberg55b97fe2016-01-28 05:22:45 -08002662 if (_includeAudioLevelIndication) {
2663 size_t length =
2664 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002665 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002666 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002667 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002668 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002669 rms_level_.Analyze(
2670 rtc::ArrayView<const int16_t>(_audioFrame.data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002671 }
kwiberg55b97fe2016-01-28 05:22:45 -08002672 }
solenberg1c2af8e2016-03-24 10:36:00 -07002673 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002674
kwiberg55b97fe2016-01-28 05:22:45 -08002675 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002676}
2677
kwiberg55b97fe2016-01-28 05:22:45 -08002678uint32_t Channel::EncodeAndSend() {
2679 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2680 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002681
kwiberg55b97fe2016-01-28 05:22:45 -08002682 assert(_audioFrame.num_channels_ <= 2);
2683 if (_audioFrame.samples_per_channel_ == 0) {
2684 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2685 "Channel::EncodeAndSend() invalid audio frame");
2686 return 0xFFFFFFFF;
2687 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002688
kwiberg55b97fe2016-01-28 05:22:45 -08002689 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002690
kwiberg55b97fe2016-01-28 05:22:45 -08002691 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002692
kwiberg55b97fe2016-01-28 05:22:45 -08002693 // The ACM resamples internally.
2694 _audioFrame.timestamp_ = _timeStamp;
2695 // This call will trigger AudioPacketizationCallback::SendData if encoding
2696 // is done and payload is ready for packetization and transmission.
2697 // Otherwise, it will return without invoking the callback.
2698 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2699 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2700 "Channel::EncodeAndSend() ACM encoding failed");
2701 return 0xFFFFFFFF;
2702 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002703
kwiberg55b97fe2016-01-28 05:22:45 -08002704 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2705 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002706}
2707
solenberg7602aab2016-11-14 11:30:07 -08002708void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2709 RTC_DCHECK(!channel.channel() ||
2710 channel.channel()->ChannelId() != _channelId);
2711 rtc::CritScope lock(&assoc_send_channel_lock_);
2712 associate_send_channel_ = channel;
2713}
2714
Minyue2013aec2015-05-13 14:14:42 +02002715void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002716 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002717 Channel* channel = associate_send_channel_.channel();
2718 if (channel && channel->ChannelId() == channel_id) {
2719 // If this channel is associated with a send channel of the specified
2720 // Channel ID, disassociate with it.
2721 ChannelOwner ref(NULL);
2722 associate_send_channel_ = ref;
2723 }
2724}
2725
ivoc14d5dbe2016-07-04 07:06:55 -07002726void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2727 event_log_proxy_->SetEventLog(event_log);
2728}
2729
michaelt9332b7d2016-11-30 07:51:13 -08002730void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2731 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2732}
2733
nisse284542b2017-01-10 08:58:32 -08002734void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08002735 size_t overhead_per_packet =
2736 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08002737 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2738 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08002739 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08002740 }
2741 });
2742}
2743
2744void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002745 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002746 transport_overhead_per_packet_ = transport_overhead_per_packet;
2747 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002748}
2749
hbos3fd31fe2017-02-28 05:43:16 -08002750// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08002751void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002752 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002753 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2754 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002755}
2756
kwiberg55b97fe2016-01-28 05:22:45 -08002757int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2758 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002759}
2760
wu@webrtc.org24301a62013-12-13 19:17:43 +00002761void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2762 audio_coding_->GetDecodingCallStatistics(stats);
2763}
2764
solenberg358057b2015-11-27 10:46:42 -08002765uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002766 rtc::CritScope lock(&video_sync_lock_);
2767 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002768}
2769
kwiberg55b97fe2016-01-28 05:22:45 -08002770int Channel::SetMinimumPlayoutDelay(int delayMs) {
2771 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2772 "Channel::SetMinimumPlayoutDelay()");
2773 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2774 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2775 _engineStatisticsPtr->SetLastError(
2776 VE_INVALID_ARGUMENT, kTraceError,
2777 "SetMinimumPlayoutDelay() invalid min delay");
2778 return -1;
2779 }
2780 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2781 _engineStatisticsPtr->SetLastError(
2782 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2783 "SetMinimumPlayoutDelay() failed to set min playout delay");
2784 return -1;
2785 }
2786 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002787}
2788
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002789int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002790 uint32_t playout_timestamp_rtp = 0;
2791 {
tommi31fc21f2016-01-21 10:37:37 -08002792 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002793 playout_timestamp_rtp = playout_timestamp_rtp_;
2794 }
kwiberg55b97fe2016-01-28 05:22:45 -08002795 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002796 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002797 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002798 "GetPlayoutTimestamp() failed to retrieve timestamp");
2799 return -1;
2800 }
deadbeef74375882015-08-13 12:09:10 -07002801 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002802 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002803}
2804
kwiberg55b97fe2016-01-28 05:22:45 -08002805int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2806 RtpReceiver** rtp_receiver) const {
2807 *rtpRtcpModule = _rtpRtcpModule.get();
2808 *rtp_receiver = rtp_receiver_.get();
2809 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002810}
2811
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002812// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2813// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002814int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002815 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002816 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002817
kwiberg55b97fe2016-01-28 05:22:45 -08002818 {
2819 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002820
kwiberg5a25d952016-08-17 07:31:12 -07002821 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002822 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2823 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2824 " doesnt exist");
2825 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002826 }
2827
kwiberg4ec01d92016-08-22 08:43:54 -07002828 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002829 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002830 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2831 "Channel::MixOrReplaceAudioWithFile() file mixing "
2832 "failed");
2833 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002834 }
kwiberg55b97fe2016-01-28 05:22:45 -08002835 if (fileSamples == 0) {
2836 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2837 "Channel::MixOrReplaceAudioWithFile() file is ended");
2838 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002839 }
kwiberg55b97fe2016-01-28 05:22:45 -08002840 }
2841
2842 assert(_audioFrame.samples_per_channel_ == fileSamples);
2843
2844 if (_mixFileWithMicrophone) {
2845 // Currently file stream is always mono.
2846 // TODO(xians): Change the code when FilePlayer supports real stereo.
2847 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
2848 1, fileSamples);
2849 } else {
2850 // Replace ACM audio with file.
2851 // Currently file stream is always mono.
2852 // TODO(xians): Change the code when FilePlayer supports real stereo.
2853 _audioFrame.UpdateFrame(
2854 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2855 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2856 }
2857 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002858}
2859
kwiberg55b97fe2016-01-28 05:22:45 -08002860int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2861 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002862
kwibergb7f89d62016-02-17 10:04:18 -08002863 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002864 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002865
kwiberg55b97fe2016-01-28 05:22:45 -08002866 {
2867 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002868
kwiberg5a25d952016-08-17 07:31:12 -07002869 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002870 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2871 "Channel::MixAudioWithFile() file mixing failed");
2872 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002873 }
2874
kwiberg55b97fe2016-01-28 05:22:45 -08002875 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002876 if (output_file_player_->Get10msAudioFromFile(
2877 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002878 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2879 "Channel::MixAudioWithFile() file mixing failed");
2880 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002881 }
kwiberg55b97fe2016-01-28 05:22:45 -08002882 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002883
kwiberg55b97fe2016-01-28 05:22:45 -08002884 if (audioFrame.samples_per_channel_ == fileSamples) {
2885 // Currently file stream is always mono.
2886 // TODO(xians): Change the code when FilePlayer supports real stereo.
2887 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
2888 fileSamples);
2889 } else {
2890 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2891 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2892 ") != "
2893 "fileSamples(%" PRIuS ")",
2894 audioFrame.samples_per_channel_, fileSamples);
2895 return -1;
2896 }
2897
2898 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002899}
2900
deadbeef74375882015-08-13 12:09:10 -07002901void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002902 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002903
henrik.lundin96bd5022016-04-06 04:13:56 -07002904 if (!jitter_buffer_playout_timestamp_) {
2905 // This can happen if this channel has not received any RTP packets. In
2906 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07002907 return;
2908 }
2909
2910 uint16_t delay_ms = 0;
2911 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002912 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002913 "Channel::UpdatePlayoutTimestamp() failed to read playout"
2914 " delay from the ADM");
2915 _engineStatisticsPtr->SetLastError(
2916 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
2917 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
2918 return;
2919 }
2920
henrik.lundin96bd5022016-04-06 04:13:56 -07002921 RTC_DCHECK(jitter_buffer_playout_timestamp_);
2922 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07002923
2924 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07002925 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07002926
kwiberg55b97fe2016-01-28 05:22:45 -08002927 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002928 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07002929 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07002930
2931 {
tommi31fc21f2016-01-21 10:37:37 -08002932 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08002933 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002934 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07002935 }
2936 playout_delay_ms_ = delay_ms;
2937 }
2938}
2939
kwiberg55b97fe2016-01-28 05:22:45 -08002940void Channel::RegisterReceiveCodecsToRTPModule() {
2941 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2942 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002943
kwiberg55b97fe2016-01-28 05:22:45 -08002944 CodecInst codec;
2945 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002946
kwiberg55b97fe2016-01-28 05:22:45 -08002947 for (int idx = 0; idx < nSupportedCodecs; idx++) {
2948 // Open up the RTP/RTCP receiver for all supported codecs
2949 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08002950 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08002951 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2952 "Channel::RegisterReceiveCodecsToRTPModule() unable"
2953 " to register %s (%d/%d/%" PRIuS
2954 "/%d) to RTP/RTCP "
2955 "receiver",
2956 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2957 codec.rate);
2958 } else {
2959 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2960 "Channel::RegisterReceiveCodecsToRTPModule() %s "
2961 "(%d/%d/%" PRIuS
2962 "/%d) has been added to the RTP/RTCP "
2963 "receiver",
2964 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2965 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00002966 }
kwiberg55b97fe2016-01-28 05:22:45 -08002967 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002968}
2969
kwiberg55b97fe2016-01-28 05:22:45 -08002970int Channel::SetSendRtpHeaderExtension(bool enable,
2971 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002972 unsigned char id) {
2973 int error = 0;
2974 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
2975 if (enable) {
2976 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
2977 }
2978 return error;
2979}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002980
ossue280cde2016-10-12 11:04:10 -07002981int Channel::GetRtpTimestampRateHz() const {
2982 const auto format = audio_coding_->ReceiveFormat();
2983 // Default to the playout frequency if we've not gotten any packets yet.
2984 // TODO(ossu): Zero clockrate can only happen if we've added an external
2985 // decoder for a format we don't support internally. Remove once that way of
2986 // adding decoders is gone!
2987 return (format && format->clockrate_hz != 0)
2988 ? format->clockrate_hz
2989 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00002990}
2991
Minyue2013aec2015-05-13 14:14:42 +02002992int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07002993 RtcpMode method = _rtpRtcpModule->RTCP();
2994 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002995 return 0;
2996 }
2997 std::vector<RTCPReportBlock> report_blocks;
2998 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02002999
3000 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003001 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003002 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003003 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003004 Channel* channel = associate_send_channel_.channel();
3005 // Tries to get RTT from an associated channel. This is important for
3006 // receive-only channels.
3007 if (channel) {
3008 // To prevent infinite recursion and deadlock, calling GetRTT of
3009 // associate channel should always use "false" for argument:
3010 // |allow_associate_channel|.
3011 rtt = channel->GetRTT(false);
3012 }
3013 }
3014 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003015 }
3016
3017 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3018 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3019 for (; it != report_blocks.end(); ++it) {
3020 if (it->remoteSSRC == remoteSSRC)
3021 break;
3022 }
3023 if (it == report_blocks.end()) {
3024 // We have not received packets with SSRC matching the report blocks.
3025 // To calculate RTT we try with the SSRC of the first report block.
3026 // This is very important for send-only channels where we don't know
3027 // the SSRC of the other end.
3028 remoteSSRC = report_blocks[0].remoteSSRC;
3029 }
Minyue2013aec2015-05-13 14:14:42 +02003030
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003031 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003032 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003033 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003034 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3035 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003036 return 0;
3037 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003038 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003039}
3040
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003041} // namespace voe
3042} // namespace webrtc