blob: a8345435f90e7d3c65ccb2e5bae2a8ad5bfa1c99 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrika@webrtc.org2919e952012-01-31 08:45:03 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000011#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Henrik Lundin64dad832015-05-11 12:44:23 +020013#include <algorithm>
Tommif888bb52015-12-12 01:37:01 +010014#include <utility>
Henrik Lundin64dad832015-05-11 12:44:23 +020015
aleloi6321b492016-12-05 01:46:09 -080016#include "webrtc/audio/utility/audio_frame_operations.h"
henrik.lundin50499422016-11-29 04:26:24 -080017#include "webrtc/base/array_view.h"
Ivo Creusenae856f22015-09-17 16:30:16 +020018#include "webrtc/base/checks.h"
tommi31fc21f2016-01-21 10:37:37 -080019#include "webrtc/base/criticalsection.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000020#include "webrtc/base/format_macros.h"
pbosad856222015-11-27 09:48:36 -080021#include "webrtc/base/logging.h"
Erik Språng737336d2016-07-29 12:59:36 +020022#include "webrtc/base/rate_limiter.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010023#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000024#include "webrtc/base/timeutils.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020025#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070026#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070027#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000028#include "webrtc/modules/audio_device/include/audio_device.h"
29#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010031#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010032#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
33#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
34#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000035#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010036#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010037#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000038#include "webrtc/voice_engine/include/voe_external_media.h"
39#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
40#include "webrtc/voice_engine/output_mixer.h"
41#include "webrtc/voice_engine/statistics.h"
42#include "webrtc/voice_engine/transmit_mixer.h"
43#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000044
andrew@webrtc.org50419b02012-11-14 19:07:54 +000045namespace webrtc {
46namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000047
kwibergc8d071e2016-04-06 12:22:38 -070048namespace {
49
Erik Språng737336d2016-07-29 12:59:36 +020050constexpr int64_t kMaxRetransmissionWindowMs = 1000;
51constexpr int64_t kMinRetransmissionWindowMs = 30;
52
kwibergc8d071e2016-04-06 12:22:38 -070053} // namespace
54
solenberg8842c3e2016-03-11 03:06:41 -080055const int kTelephoneEventAttenuationdB = 10;
56
ivoc14d5dbe2016-07-04 07:06:55 -070057class RtcEventLogProxy final : public webrtc::RtcEventLog {
58 public:
59 RtcEventLogProxy() : event_log_(nullptr) {}
60
61 bool StartLogging(const std::string& file_name,
62 int64_t max_size_bytes) override {
63 RTC_NOTREACHED();
64 return false;
65 }
66
67 bool StartLogging(rtc::PlatformFile log_file,
68 int64_t max_size_bytes) override {
69 RTC_NOTREACHED();
70 return false;
71 }
72
73 void StopLogging() override { RTC_NOTREACHED(); }
74
75 void LogVideoReceiveStreamConfig(
76 const webrtc::VideoReceiveStream::Config& config) override {
77 rtc::CritScope lock(&crit_);
78 if (event_log_) {
79 event_log_->LogVideoReceiveStreamConfig(config);
80 }
81 }
82
83 void LogVideoSendStreamConfig(
84 const webrtc::VideoSendStream::Config& config) override {
85 rtc::CritScope lock(&crit_);
86 if (event_log_) {
87 event_log_->LogVideoSendStreamConfig(config);
88 }
89 }
90
ivoce0928d82016-10-10 05:12:51 -070091 void LogAudioReceiveStreamConfig(
92 const webrtc::AudioReceiveStream::Config& config) override {
93 rtc::CritScope lock(&crit_);
94 if (event_log_) {
95 event_log_->LogAudioReceiveStreamConfig(config);
96 }
97 }
98
99 void LogAudioSendStreamConfig(
100 const webrtc::AudioSendStream::Config& config) override {
101 rtc::CritScope lock(&crit_);
102 if (event_log_) {
103 event_log_->LogAudioSendStreamConfig(config);
104 }
105 }
106
ivoc14d5dbe2016-07-04 07:06:55 -0700107 void LogRtpHeader(webrtc::PacketDirection direction,
108 webrtc::MediaType media_type,
109 const uint8_t* header,
110 size_t packet_length) override {
111 rtc::CritScope lock(&crit_);
112 if (event_log_) {
113 event_log_->LogRtpHeader(direction, media_type, header, packet_length);
114 }
115 }
116
117 void LogRtcpPacket(webrtc::PacketDirection direction,
118 webrtc::MediaType media_type,
119 const uint8_t* packet,
120 size_t length) override {
121 rtc::CritScope lock(&crit_);
122 if (event_log_) {
123 event_log_->LogRtcpPacket(direction, media_type, packet, length);
124 }
125 }
126
127 void LogAudioPlayout(uint32_t ssrc) override {
128 rtc::CritScope lock(&crit_);
129 if (event_log_) {
130 event_log_->LogAudioPlayout(ssrc);
131 }
132 }
133
134 void LogBwePacketLossEvent(int32_t bitrate,
135 uint8_t fraction_loss,
136 int32_t total_packets) override {
137 rtc::CritScope lock(&crit_);
138 if (event_log_) {
139 event_log_->LogBwePacketLossEvent(bitrate, fraction_loss, total_packets);
140 }
141 }
142
minyue4b7c9522017-01-24 04:54:59 -0800143 void LogAudioNetworkAdaptation(
144 const AudioNetworkAdaptor::EncoderRuntimeConfig& config) override {
145 rtc::CritScope lock(&crit_);
146 if (event_log_) {
147 event_log_->LogAudioNetworkAdaptation(config);
148 }
149 }
150
ivoc14d5dbe2016-07-04 07:06:55 -0700151 void SetEventLog(RtcEventLog* event_log) {
152 rtc::CritScope lock(&crit_);
153 event_log_ = event_log;
154 }
155
156 private:
157 rtc::CriticalSection crit_;
158 RtcEventLog* event_log_ GUARDED_BY(crit_);
159 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
160};
161
michaelt9332b7d2016-11-30 07:51:13 -0800162class RtcpRttStatsProxy final : public RtcpRttStats {
163 public:
164 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
165
166 void OnRttUpdate(int64_t rtt) override {
167 rtc::CritScope lock(&crit_);
168 if (rtcp_rtt_stats_)
169 rtcp_rtt_stats_->OnRttUpdate(rtt);
170 }
171
172 int64_t LastProcessedRtt() const override {
173 rtc::CritScope lock(&crit_);
174 if (!rtcp_rtt_stats_)
175 return 0;
176 return rtcp_rtt_stats_->LastProcessedRtt();
177 }
178
179 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
180 rtc::CritScope lock(&crit_);
181 rtcp_rtt_stats_ = rtcp_rtt_stats;
182 }
183
184 private:
185 rtc::CriticalSection crit_;
186 RtcpRttStats* rtcp_rtt_stats_ GUARDED_BY(crit_);
187 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
188};
189
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100190class TransportFeedbackProxy : public TransportFeedbackObserver {
191 public:
192 TransportFeedbackProxy() : feedback_observer_(nullptr) {
193 pacer_thread_.DetachFromThread();
194 network_thread_.DetachFromThread();
195 }
196
197 void SetTransportFeedbackObserver(
198 TransportFeedbackObserver* feedback_observer) {
199 RTC_DCHECK(thread_checker_.CalledOnValidThread());
200 rtc::CritScope lock(&crit_);
201 feedback_observer_ = feedback_observer;
202 }
203
204 // Implements TransportFeedbackObserver.
205 void AddPacket(uint16_t sequence_number,
206 size_t length,
philipela1ed0b32016-06-01 06:31:17 -0700207 int probe_cluster_id) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100208 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
209 rtc::CritScope lock(&crit_);
210 if (feedback_observer_)
pbos2169d8b2016-06-20 11:53:02 -0700211 feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100212 }
213 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
214 RTC_DCHECK(network_thread_.CalledOnValidThread());
215 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700216 if (feedback_observer_)
217 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200218 }
219 std::vector<PacketInfo> GetTransportFeedbackVector() const override {
220 RTC_NOTREACHED();
221 return std::vector<PacketInfo>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100222 }
223
224 private:
225 rtc::CriticalSection crit_;
226 rtc::ThreadChecker thread_checker_;
227 rtc::ThreadChecker pacer_thread_;
228 rtc::ThreadChecker network_thread_;
229 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
230};
231
232class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
233 public:
234 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
235 pacer_thread_.DetachFromThread();
236 }
237
238 void SetSequenceNumberAllocator(
239 TransportSequenceNumberAllocator* seq_num_allocator) {
240 RTC_DCHECK(thread_checker_.CalledOnValidThread());
241 rtc::CritScope lock(&crit_);
242 seq_num_allocator_ = seq_num_allocator;
243 }
244
245 // Implements TransportSequenceNumberAllocator.
246 uint16_t AllocateSequenceNumber() override {
247 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
248 rtc::CritScope lock(&crit_);
249 if (!seq_num_allocator_)
250 return 0;
251 return seq_num_allocator_->AllocateSequenceNumber();
252 }
253
254 private:
255 rtc::CriticalSection crit_;
256 rtc::ThreadChecker thread_checker_;
257 rtc::ThreadChecker pacer_thread_;
258 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
259};
260
261class RtpPacketSenderProxy : public RtpPacketSender {
262 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800263 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100264
265 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
266 RTC_DCHECK(thread_checker_.CalledOnValidThread());
267 rtc::CritScope lock(&crit_);
268 rtp_packet_sender_ = rtp_packet_sender;
269 }
270
271 // Implements RtpPacketSender.
272 void InsertPacket(Priority priority,
273 uint32_t ssrc,
274 uint16_t sequence_number,
275 int64_t capture_time_ms,
276 size_t bytes,
277 bool retransmission) override {
278 rtc::CritScope lock(&crit_);
279 if (rtp_packet_sender_) {
280 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
281 capture_time_ms, bytes, retransmission);
282 }
283 }
284
285 private:
286 rtc::ThreadChecker thread_checker_;
287 rtc::CriticalSection crit_;
288 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
289};
290
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000291class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000292 public:
stefan7de8d642017-02-07 07:14:08 -0800293 explicit VoERtcpObserver(Channel* owner)
294 : owner_(owner), bandwidth_observer_(nullptr) {}
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000295 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000296
stefan7de8d642017-02-07 07:14:08 -0800297 void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
298 rtc::CritScope lock(&crit_);
299 bandwidth_observer_ = bandwidth_observer;
300 }
301
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000302 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
stefan7de8d642017-02-07 07:14:08 -0800303 rtc::CritScope lock(&crit_);
304 if (bandwidth_observer_) {
305 bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
306 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000307 }
308
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000309 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
310 int64_t rtt,
311 int64_t now_ms) override {
stefan7de8d642017-02-07 07:14:08 -0800312 {
313 rtc::CritScope lock(&crit_);
314 if (bandwidth_observer_) {
315 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
316 now_ms);
317 }
318 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000319 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
320 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
321 // report for VoiceEngine?
322 if (report_blocks.empty())
323 return;
324
325 int fraction_lost_aggregate = 0;
326 int total_number_of_packets = 0;
327
328 // If receiving multiple report blocks, calculate the weighted average based
329 // on the number of packets a report refers to.
330 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
331 block_it != report_blocks.end(); ++block_it) {
332 // Find the previous extended high sequence number for this remote SSRC,
333 // to calculate the number of RTP packets this report refers to. Ignore if
334 // we haven't seen this SSRC before.
335 std::map<uint32_t, uint32_t>::iterator seq_num_it =
336 extended_max_sequence_number_.find(block_it->sourceSSRC);
337 int number_of_packets = 0;
338 if (seq_num_it != extended_max_sequence_number_.end()) {
339 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
340 }
341 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
342 total_number_of_packets += number_of_packets;
343
344 extended_max_sequence_number_[block_it->sourceSSRC] =
345 block_it->extendedHighSeqNum;
346 }
347 int weighted_fraction_lost = 0;
348 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800349 weighted_fraction_lost =
350 (fraction_lost_aggregate + total_number_of_packets / 2) /
351 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000352 }
353 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000354 }
355
356 private:
357 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000358 // Maps remote side ssrc to extended highest sequence number received.
359 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800360 rtc::CriticalSection crit_;
361 RtcpBandwidthObserver* bandwidth_observer_ GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000362};
363
kwiberg55b97fe2016-01-28 05:22:45 -0800364int32_t Channel::SendData(FrameType frameType,
365 uint8_t payloadType,
366 uint32_t timeStamp,
367 const uint8_t* payloadData,
368 size_t payloadSize,
369 const RTPFragmentationHeader* fragmentation) {
370 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
371 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
372 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
373 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000374
kwiberg55b97fe2016-01-28 05:22:45 -0800375 if (_includeAudioLevelIndication) {
376 // Store current audio level in the RTP/RTCP module.
377 // The level will be used in combination with voice-activity state
378 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800379 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800380 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000381
kwiberg55b97fe2016-01-28 05:22:45 -0800382 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
383 // packetization.
384 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700385 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800386 (FrameType&)frameType, payloadType, timeStamp,
387 // Leaving the time when this frame was
388 // received from the capture device as
389 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700390 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800391 _engineStatisticsPtr->SetLastError(
392 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
393 "Channel::SendData() failed to send data to RTP/RTCP module");
394 return -1;
395 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000396
kwiberg55b97fe2016-01-28 05:22:45 -0800397 _lastLocalTimeStamp = timeStamp;
398 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000399
kwiberg55b97fe2016-01-28 05:22:45 -0800400 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000401}
402
kwiberg55b97fe2016-01-28 05:22:45 -0800403int32_t Channel::InFrameType(FrameType frame_type) {
404 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
405 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000406
kwiberg55b97fe2016-01-28 05:22:45 -0800407 rtc::CritScope cs(&_callbackCritSect);
408 _sendFrameType = (frame_type == kAudioFrameSpeech);
409 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000410}
411
stefan1d8a5062015-10-02 03:39:33 -0700412bool Channel::SendRtp(const uint8_t* data,
413 size_t len,
414 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800415 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
416 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000417
kwiberg55b97fe2016-01-28 05:22:45 -0800418 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000419
kwiberg55b97fe2016-01-28 05:22:45 -0800420 if (_transportPtr == NULL) {
421 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
422 "Channel::SendPacket() failed to send RTP packet due to"
423 " invalid transport object");
424 return false;
425 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000426
kwiberg55b97fe2016-01-28 05:22:45 -0800427 uint8_t* bufferToSendPtr = (uint8_t*)data;
428 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000429
kwiberg55b97fe2016-01-28 05:22:45 -0800430 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
431 std::string transport_name =
432 _externalTransport ? "external transport" : "WebRtc sockets";
433 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
434 "Channel::SendPacket() RTP transmission using %s failed",
435 transport_name.c_str());
436 return false;
437 }
438 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000439}
440
kwiberg55b97fe2016-01-28 05:22:45 -0800441bool Channel::SendRtcp(const uint8_t* data, size_t len) {
442 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
443 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000444
kwiberg55b97fe2016-01-28 05:22:45 -0800445 rtc::CritScope cs(&_callbackCritSect);
446 if (_transportPtr == NULL) {
447 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
448 "Channel::SendRtcp() failed to send RTCP packet"
449 " due to invalid transport object");
450 return false;
451 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000452
kwiberg55b97fe2016-01-28 05:22:45 -0800453 uint8_t* bufferToSendPtr = (uint8_t*)data;
454 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000455
kwiberg55b97fe2016-01-28 05:22:45 -0800456 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
457 if (n < 0) {
458 std::string transport_name =
459 _externalTransport ? "external transport" : "WebRtc sockets";
460 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
461 "Channel::SendRtcp() transmission using %s failed",
462 transport_name.c_str());
463 return false;
464 }
465 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000466}
467
kwiberg55b97fe2016-01-28 05:22:45 -0800468void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
469 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
470 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000471
kwiberg55b97fe2016-01-28 05:22:45 -0800472 // Update ssrc so that NTP for AV sync can be updated.
473 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000474}
475
Peter Boströmac547a62015-09-17 23:03:57 +0200476void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
477 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
478 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
479 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000480}
481
Peter Boströmac547a62015-09-17 23:03:57 +0200482int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000483 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000484 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000485 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800486 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200487 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800488 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
489 "Channel::OnInitializeDecoder(payloadType=%d, "
490 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
491 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
kwiberg55b97fe2016-01-28 05:22:45 -0800493 CodecInst receiveCodec = {0};
494 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000495
kwiberg55b97fe2016-01-28 05:22:45 -0800496 receiveCodec.pltype = payloadType;
497 receiveCodec.plfreq = frequency;
498 receiveCodec.channels = channels;
499 receiveCodec.rate = rate;
500 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000501
kwiberg55b97fe2016-01-28 05:22:45 -0800502 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
503 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000504
kwiberg55b97fe2016-01-28 05:22:45 -0800505 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700506 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
507 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800508 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
509 "Channel::OnInitializeDecoder() invalid codec ("
510 "pt=%d, name=%s) received - 1",
511 payloadType, payloadName);
512 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
513 return -1;
514 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000515
kwiberg55b97fe2016-01-28 05:22:45 -0800516 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000517}
518
kwiberg55b97fe2016-01-28 05:22:45 -0800519int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
520 size_t payloadSize,
521 const WebRtcRTPHeader* rtpHeader) {
522 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
523 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
524 ","
525 " payloadType=%u, audioChannel=%" PRIuS ")",
526 payloadSize, rtpHeader->header.payloadType,
527 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000528
kwiberg55b97fe2016-01-28 05:22:45 -0800529 if (!channel_state_.Get().playing) {
530 // Avoid inserting into NetEQ when we are not playing. Count the
531 // packet as discarded.
532 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
533 "received packet is discarded since playing is not"
534 " activated");
niklase@google.com470e71d2011-07-07 08:21:25 +0000535 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800536 }
537
538 // Push the incoming payload (parsed and ready for decoding) into the ACM
539 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
540 0) {
541 _engineStatisticsPtr->SetLastError(
542 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
543 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
544 return -1;
545 }
546
kwiberg55b97fe2016-01-28 05:22:45 -0800547 int64_t round_trip_time = 0;
548 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
549 NULL);
550
551 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
552 if (!nack_list.empty()) {
553 // Can't use nack_list.data() since it's not supported by all
554 // compilers.
555 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
556 }
557 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000558}
559
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000560bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000561 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000562 RTPHeader header;
563 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
564 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
565 "IncomingPacket invalid RTP header");
566 return false;
567 }
568 header.payload_type_frequency =
569 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
570 if (header.payload_type_frequency < 0)
571 return false;
572 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
573}
574
henrik.lundin42dda502016-05-18 05:36:01 -0700575MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
576 int32_t id,
577 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700578 unsigned int ssrc;
579 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
580 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800581 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700582 bool muted;
583 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
584 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800585 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
586 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
587 // In all likelihood, the audio in this frame is garbage. We return an
588 // error so that the audio mixer module doesn't add it to the mix. As
589 // a result, it won't be played out and the actions skipped here are
590 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700591 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800592 }
henrik.lundina89ab962016-05-18 08:52:45 -0700593
594 if (muted) {
595 // TODO(henrik.lundin): We should be able to do better than this. But we
596 // will have to go through all the cases below where the audio samples may
597 // be used, and handle the muted case in some way.
aleloi6321b492016-12-05 01:46:09 -0800598 AudioFrameOperations::Mute(audioFrame);
henrik.lundina89ab962016-05-18 08:52:45 -0700599 }
kwiberg55b97fe2016-01-28 05:22:45 -0800600
kwiberg55b97fe2016-01-28 05:22:45 -0800601 // Convert module ID to internal VoE channel ID
602 audioFrame->id_ = VoEChannelId(audioFrame->id_);
603 // Store speech type for dead-or-alive detection
604 _outputSpeechType = audioFrame->speech_type_;
605
606 ChannelState::State state = channel_state_.Get();
607
kwiberg55b97fe2016-01-28 05:22:45 -0800608 {
609 // Pass the audio buffers to an optional sink callback, before applying
610 // scaling/panning, as that applies to the mix operation.
611 // External recipients of the audio (e.g. via AudioTrack), will do their
612 // own mixing/dynamic processing.
613 rtc::CritScope cs(&_callbackCritSect);
614 if (audio_sink_) {
615 AudioSinkInterface::Data data(
616 &audioFrame->data_[0], audioFrame->samples_per_channel_,
617 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
618 audioFrame->timestamp_);
619 audio_sink_->OnData(data);
620 }
621 }
622
623 float output_gain = 1.0f;
624 float left_pan = 1.0f;
625 float right_pan = 1.0f;
626 {
627 rtc::CritScope cs(&volume_settings_critsect_);
628 output_gain = _outputGain;
629 left_pan = _panLeft;
630 right_pan = _panRight;
631 }
632
633 // Output volume scaling
634 if (output_gain < 0.99f || output_gain > 1.01f) {
635 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
636 }
637
638 // Scale left and/or right channel(s) if stereo and master balance is
639 // active
640
641 if (left_pan != 1.0f || right_pan != 1.0f) {
642 if (audioFrame->num_channels_ == 1) {
643 // Emulate stereo mode since panning is active.
644 // The mono signal is copied to both left and right channels here.
645 AudioFrameOperations::MonoToStereo(audioFrame);
646 }
647 // For true stereo mode (when we are receiving a stereo signal), no
648 // action is needed.
649
650 // Do the panning operation (the audio frame contains stereo at this
651 // stage)
652 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
653 }
654
655 // Mix decoded PCM output with file if file mixing is enabled
656 if (state.output_file_playing) {
657 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700658 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800659 }
660
661 // External media
662 if (_outputExternalMedia) {
663 rtc::CritScope cs(&_callbackCritSect);
664 const bool isStereo = (audioFrame->num_channels_ == 2);
665 if (_outputExternalMediaCallbackPtr) {
666 _outputExternalMediaCallbackPtr->Process(
667 _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_,
668 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
669 isStereo);
670 }
671 }
672
673 // Record playout if enabled
674 {
675 rtc::CritScope cs(&_fileCritSect);
676
kwiberg5a25d952016-08-17 07:31:12 -0700677 if (_outputFileRecording && output_file_recorder_) {
678 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800679 }
680 }
681
682 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700683 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800684 _outputAudioLevel.ComputeLevel(*audioFrame);
685
686 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
687 // The first frame with a valid rtp timestamp.
688 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
689 }
690
691 if (capture_start_rtp_time_stamp_ >= 0) {
692 // audioFrame.timestamp_ should be valid from now on.
693
694 // Compute elapsed time.
695 int64_t unwrap_timestamp =
696 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
697 audioFrame->elapsed_time_ms_ =
698 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700699 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800700
niklase@google.com470e71d2011-07-07 08:21:25 +0000701 {
kwiberg55b97fe2016-01-28 05:22:45 -0800702 rtc::CritScope lock(&ts_stats_lock_);
703 // Compute ntp time.
704 audioFrame->ntp_time_ms_ =
705 ntp_estimator_.Estimate(audioFrame->timestamp_);
706 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
707 if (audioFrame->ntp_time_ms_ > 0) {
708 // Compute |capture_start_ntp_time_ms_| so that
709 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
710 capture_start_ntp_time_ms_ =
711 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000712 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000713 }
kwiberg55b97fe2016-01-28 05:22:45 -0800714 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000715
henrik.lundin42dda502016-05-18 05:36:01 -0700716 return muted ? MixerParticipant::AudioFrameInfo::kMuted
717 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000718}
719
aleloi6c278492016-10-20 14:24:39 -0700720AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
721 int sample_rate_hz,
722 AudioFrame* audio_frame) {
723 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700724
aleloi6c278492016-10-20 14:24:39 -0700725 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700726
727 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
728 FrameInfo new_audio_frame_info = FrameInfo::kError;
729 switch (frame_info) {
730 case MixerParticipant::AudioFrameInfo::kNormal:
731 new_audio_frame_info = FrameInfo::kNormal;
732 break;
733 case MixerParticipant::AudioFrameInfo::kMuted:
734 new_audio_frame_info = FrameInfo::kMuted;
735 break;
736 case MixerParticipant::AudioFrameInfo::kError:
737 new_audio_frame_info = FrameInfo::kError;
738 break;
739 }
aleloi6c278492016-10-20 14:24:39 -0700740 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700741}
742
kwiberg55b97fe2016-01-28 05:22:45 -0800743int32_t Channel::NeededFrequency(int32_t id) const {
744 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
745 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000746
kwiberg55b97fe2016-01-28 05:22:45 -0800747 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000748
kwiberg55b97fe2016-01-28 05:22:45 -0800749 // Determine highest needed receive frequency
750 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000751
kwiberg55b97fe2016-01-28 05:22:45 -0800752 // Return the bigger of playout and receive frequency in the ACM.
753 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
754 highestNeeded = audio_coding_->PlayoutFrequency();
755 } else {
756 highestNeeded = receiveFrequency;
757 }
758
759 // Special case, if we're playing a file on the playout side
760 // we take that frequency into consideration as well
761 // This is not needed on sending side, since the codec will
762 // limit the spectrum anyway.
763 if (channel_state_.Get().output_file_playing) {
764 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700765 if (output_file_player_) {
766 if (output_file_player_->Frequency() > highestNeeded) {
767 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800768 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000769 }
kwiberg55b97fe2016-01-28 05:22:45 -0800770 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000771
kwiberg55b97fe2016-01-28 05:22:45 -0800772 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000773}
774
ossu5f7cfa52016-05-30 08:11:28 -0700775int32_t Channel::CreateChannel(
776 Channel*& channel,
777 int32_t channelId,
778 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700779 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800780 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
781 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
782 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000783
solenberg88499ec2016-09-07 07:34:41 -0700784 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800785 if (channel == NULL) {
786 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
787 "Channel::CreateChannel() unable to allocate memory for"
788 " channel");
789 return -1;
790 }
791 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000792}
793
kwiberg55b97fe2016-01-28 05:22:45 -0800794void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
795 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
796 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
797 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000798
kwiberg55b97fe2016-01-28 05:22:45 -0800799 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000800}
801
kwiberg55b97fe2016-01-28 05:22:45 -0800802void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
803 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
804 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
805 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000806
kwiberg55b97fe2016-01-28 05:22:45 -0800807 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000808}
809
kwiberg55b97fe2016-01-28 05:22:45 -0800810void Channel::PlayFileEnded(int32_t id) {
811 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
812 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000813
kwiberg55b97fe2016-01-28 05:22:45 -0800814 if (id == _inputFilePlayerId) {
815 channel_state_.SetInputFilePlaying(false);
816 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
817 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000818 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800819 } else if (id == _outputFilePlayerId) {
820 channel_state_.SetOutputFilePlaying(false);
821 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
822 "Channel::PlayFileEnded() => output file player module is"
823 " shutdown");
824 }
825}
826
827void Channel::RecordFileEnded(int32_t id) {
828 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
829 "Channel::RecordFileEnded(id=%d)", id);
830
831 assert(id == _outputFileRecorderId);
832
833 rtc::CritScope cs(&_fileCritSect);
834
835 _outputFileRecording = false;
836 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
837 "Channel::RecordFileEnded() => output file recorder module is"
838 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000839}
840
pbos@webrtc.org92135212013-05-14 08:31:39 +0000841Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000842 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700843 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800844 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100845 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700846 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800847 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100848 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800849 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100850 rtp_receive_statistics_(
851 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
852 rtp_receiver_(
853 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100854 this,
855 this,
856 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700857 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100858 _outputAudioLevel(),
859 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100860 // Avoid conflict with other channels by adding 1024 - 1026,
861 // won't use as much as 1024 channels.
862 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
863 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
864 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
865 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100866 _outputExternalMedia(false),
867 _inputExternalMediaCallbackPtr(NULL),
868 _outputExternalMediaCallbackPtr(NULL),
869 _timeStamp(0), // This is just an offset, RTP module will add it's own
870 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100871 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100872 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100873 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100874 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100875 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
876 capture_start_rtp_time_stamp_(-1),
877 capture_start_ntp_time_ms_(-1),
878 _engineStatisticsPtr(NULL),
879 _outputMixerPtr(NULL),
880 _transmitMixerPtr(NULL),
881 _moduleProcessThreadPtr(NULL),
882 _audioDeviceModulePtr(NULL),
883 _voiceEngineObserverPtr(NULL),
884 _callbackCritSectPtr(NULL),
885 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100886 _sendFrameType(0),
887 _externalMixing(false),
888 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700889 input_mute_(false),
890 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100891 _panLeft(1.0f),
892 _panRight(1.0f),
893 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100894 _lastLocalTimeStamp(0),
895 _lastPayloadType(0),
896 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800897 transport_overhead_per_packet_(0),
898 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100899 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100900 restored_packet_in_use_(false),
901 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100902 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700903 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800904 feedback_observer_proxy_(new TransportFeedbackProxy()),
905 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700906 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200907 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
908 kMaxRetransmissionWindowMs)),
michaelt566d8202017-01-12 10:17:38 -0800909 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800910 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
911 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700912 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800913 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700914 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800915 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200916
kwiberg55b97fe2016-01-28 05:22:45 -0800917 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000918
kwiberg55b97fe2016-01-28 05:22:45 -0800919 RtpRtcp::Configuration configuration;
920 configuration.audio = true;
921 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800922 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800923 configuration.receive_statistics = rtp_receive_statistics_.get();
924 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800925 if (pacing_enabled_) {
926 configuration.paced_sender = rtp_packet_sender_proxy_.get();
927 configuration.transport_sequence_number_allocator =
928 seq_num_allocator_proxy_.get();
929 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
930 }
ivoc14d5dbe2016-07-04 07:06:55 -0700931 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800932 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200933 configuration.retransmission_rate_limiter =
934 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000935
kwiberg55b97fe2016-01-28 05:22:45 -0800936 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100937 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000938}
939
kwiberg55b97fe2016-01-28 05:22:45 -0800940Channel::~Channel() {
941 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
942 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
943 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000944
kwiberg55b97fe2016-01-28 05:22:45 -0800945 if (_outputExternalMedia) {
946 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
947 }
948 if (channel_state_.Get().input_external_media) {
949 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
950 }
951 StopSend();
952 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000953
kwiberg55b97fe2016-01-28 05:22:45 -0800954 {
955 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700956 if (input_file_player_) {
957 input_file_player_->RegisterModuleFileCallback(NULL);
958 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000959 }
kwiberg5a25d952016-08-17 07:31:12 -0700960 if (output_file_player_) {
961 output_file_player_->RegisterModuleFileCallback(NULL);
962 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800963 }
kwiberg5a25d952016-08-17 07:31:12 -0700964 if (output_file_recorder_) {
965 output_file_recorder_->RegisterModuleFileCallback(NULL);
966 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800967 }
968 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000969
kwiberg55b97fe2016-01-28 05:22:45 -0800970 // The order to safely shutdown modules in a channel is:
971 // 1. De-register callbacks in modules
972 // 2. De-register modules in process thread
973 // 3. Destroy modules
974 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
975 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
976 "~Channel() failed to de-register transport callback"
977 " (Audio coding module)");
978 }
979 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
980 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
981 "~Channel() failed to de-register VAD callback"
982 " (Audio coding module)");
983 }
984 // De-register modules in process thread
985 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000986
kwiberg55b97fe2016-01-28 05:22:45 -0800987 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000988}
989
kwiberg55b97fe2016-01-28 05:22:45 -0800990int32_t Channel::Init() {
991 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
992 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000993
kwiberg55b97fe2016-01-28 05:22:45 -0800994 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000995
kwiberg55b97fe2016-01-28 05:22:45 -0800996 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000997
kwiberg55b97fe2016-01-28 05:22:45 -0800998 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
999 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1000 "Channel::Init() must call SetEngineInformation() first");
1001 return -1;
1002 }
1003
1004 // --- Add modules to process thread (for periodic schedulation)
1005
1006 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
1007
1008 // --- ACM initialization
1009
1010 if (audio_coding_->InitializeReceiver() == -1) {
1011 _engineStatisticsPtr->SetLastError(
1012 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1013 "Channel::Init() unable to initialize the ACM - 1");
1014 return -1;
1015 }
1016
1017 // --- RTP/RTCP module initialization
1018
1019 // Ensure that RTCP is enabled by default for the created channel.
1020 // Note that, the module will keep generating RTCP until it is explicitly
1021 // disabled by the user.
1022 // After StopListen (when no sockets exists), RTCP packets will no longer
1023 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001024 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001025 // RTCP is enabled by default.
1026 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1027 // --- Register all permanent callbacks
1028 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1029 (audio_coding_->RegisterVADCallback(this) == -1);
1030
1031 if (fail) {
1032 _engineStatisticsPtr->SetLastError(
1033 VE_CANNOT_INIT_CHANNEL, kTraceError,
1034 "Channel::Init() callbacks not registered");
1035 return -1;
1036 }
1037
1038 // --- Register all supported codecs to the receiving side of the
1039 // RTP/RTCP module
1040
1041 CodecInst codec;
1042 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1043
1044 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1045 // Open up the RTP/RTCP receiver for all supported codecs
1046 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08001047 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001048 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1049 "Channel::Init() unable to register %s "
1050 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1051 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1052 codec.rate);
1053 } else {
1054 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1055 "Channel::Init() %s (%d/%d/%" PRIuS
1056 "/%d) has been "
1057 "added to the RTP/RTCP receiver",
1058 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1059 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001060 }
1061
kwiberg55b97fe2016-01-28 05:22:45 -08001062 // Ensure that PCMU is used as default codec on the sending side
1063 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1064 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001065 }
1066
kwiberg55b97fe2016-01-28 05:22:45 -08001067 // Register default PT for outband 'telephone-event'
1068 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001069 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001070 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1071 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001072 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1073 "Channel::Init() failed to register outband "
1074 "'telephone-event' (%d/%d) correctly",
1075 codec.pltype, codec.plfreq);
1076 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001077 }
1078
kwiberg55b97fe2016-01-28 05:22:45 -08001079 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001080 if (!codec_manager_.RegisterEncoder(codec) ||
1081 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001082 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1083 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001084 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001085 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1086 "Channel::Init() failed to register CN (%d/%d) "
1087 "correctly - 1",
1088 codec.pltype, codec.plfreq);
1089 }
1090 }
kwiberg55b97fe2016-01-28 05:22:45 -08001091 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001092
kwiberg55b97fe2016-01-28 05:22:45 -08001093 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001094}
1095
kwiberg55b97fe2016-01-28 05:22:45 -08001096int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1097 OutputMixer& outputMixer,
1098 voe::TransmitMixer& transmitMixer,
1099 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;
1107 _transmitMixerPtr = &transmitMixer,
1108 _moduleProcessThreadPtr = &moduleProcessThread;
1109 _audioDeviceModulePtr = &audioDeviceModule;
1110 _voiceEngineObserverPtr = voiceEngineObserver;
1111 _callbackCritSectPtr = callbackCritSect;
1112 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001113}
1114
kwiberg55b97fe2016-01-28 05:22:45 -08001115int32_t Channel::UpdateLocalTimeStamp() {
1116 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1117 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001118}
1119
kwibergb7f89d62016-02-17 10:04:18 -08001120void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001121 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001122 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001123}
1124
ossu29b1a8d2016-06-13 07:34:51 -07001125const rtc::scoped_refptr<AudioDecoderFactory>&
1126Channel::GetAudioDecoderFactory() const {
1127 return decoder_factory_;
1128}
1129
kwiberg55b97fe2016-01-28 05:22:45 -08001130int32_t Channel::StartPlayout() {
1131 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1132 "Channel::StartPlayout()");
1133 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001134 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001135 }
1136
1137 if (!_externalMixing) {
1138 // Add participant as candidates for mixing.
1139 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1140 _engineStatisticsPtr->SetLastError(
1141 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1142 "StartPlayout() failed to add participant to mixer");
1143 return -1;
1144 }
1145 }
1146
1147 channel_state_.SetPlaying(true);
1148 if (RegisterFilePlayingToMixer() != 0)
1149 return -1;
1150
1151 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001152}
1153
kwiberg55b97fe2016-01-28 05:22:45 -08001154int32_t Channel::StopPlayout() {
1155 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1156 "Channel::StopPlayout()");
1157 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001158 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001159 }
1160
1161 if (!_externalMixing) {
1162 // Remove participant as candidates for mixing
1163 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1164 _engineStatisticsPtr->SetLastError(
1165 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1166 "StopPlayout() failed to remove participant from mixer");
1167 return -1;
1168 }
1169 }
1170
1171 channel_state_.SetPlaying(false);
1172 _outputAudioLevel.Clear();
1173
1174 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001175}
1176
kwiberg55b97fe2016-01-28 05:22:45 -08001177int32_t Channel::StartSend() {
1178 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1179 "Channel::StartSend()");
1180 // Resume the previous sequence number which was reset by StopSend().
1181 // This needs to be done before |sending| is set to true.
1182 if (send_sequence_number_)
1183 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001184
kwiberg55b97fe2016-01-28 05:22:45 -08001185 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001186 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001187 }
1188 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001189
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001190 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001191 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1192 _engineStatisticsPtr->SetLastError(
1193 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1194 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001195 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001196 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001197 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001198 return -1;
1199 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001200
kwiberg55b97fe2016-01-28 05:22:45 -08001201 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001202}
1203
kwiberg55b97fe2016-01-28 05:22:45 -08001204int32_t Channel::StopSend() {
1205 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1206 "Channel::StopSend()");
1207 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001208 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001209 }
1210 channel_state_.SetSending(false);
1211
1212 // Store the sequence number to be able to pick up the same sequence for
1213 // the next StartSend(). This is needed for restarting device, otherwise
1214 // it might cause libSRTP to complain about packets being replayed.
1215 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1216 // CL is landed. See issue
1217 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1218 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1219
1220 // Reset sending SSRC and sequence number and triggers direct transmission
1221 // of RTCP BYE
1222 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1223 _engineStatisticsPtr->SetLastError(
1224 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1225 "StartSend() RTP/RTCP failed to stop sending");
1226 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001227 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001228
1229 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001230}
1231
kwiberg55b97fe2016-01-28 05:22:45 -08001232int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1233 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1234 "Channel::RegisterVoiceEngineObserver()");
1235 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001236
kwiberg55b97fe2016-01-28 05:22:45 -08001237 if (_voiceEngineObserverPtr) {
1238 _engineStatisticsPtr->SetLastError(
1239 VE_INVALID_OPERATION, kTraceError,
1240 "RegisterVoiceEngineObserver() observer already enabled");
1241 return -1;
1242 }
1243 _voiceEngineObserverPtr = &observer;
1244 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001245}
1246
kwiberg55b97fe2016-01-28 05:22:45 -08001247int32_t Channel::DeRegisterVoiceEngineObserver() {
1248 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1249 "Channel::DeRegisterVoiceEngineObserver()");
1250 rtc::CritScope cs(&_callbackCritSect);
1251
1252 if (!_voiceEngineObserverPtr) {
1253 _engineStatisticsPtr->SetLastError(
1254 VE_INVALID_OPERATION, kTraceWarning,
1255 "DeRegisterVoiceEngineObserver() observer already disabled");
1256 return 0;
1257 }
1258 _voiceEngineObserverPtr = NULL;
1259 return 0;
1260}
1261
1262int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001263 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001264 if (send_codec) {
1265 codec = *send_codec;
1266 return 0;
1267 }
1268 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001269}
1270
kwiberg55b97fe2016-01-28 05:22:45 -08001271int32_t Channel::GetRecCodec(CodecInst& codec) {
1272 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001273}
1274
kwiberg55b97fe2016-01-28 05:22:45 -08001275int32_t Channel::SetSendCodec(const CodecInst& codec) {
1276 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1277 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001278
kwibergc8d071e2016-04-06 12:22:38 -07001279 if (!codec_manager_.RegisterEncoder(codec) ||
1280 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001281 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1282 "SetSendCodec() failed to register codec to ACM");
1283 return -1;
1284 }
1285
1286 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1287 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1288 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1289 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1290 "SetSendCodec() failed to register codec to"
1291 " RTP/RTCP module");
1292 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001293 }
kwiberg55b97fe2016-01-28 05:22:45 -08001294 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001295
kwiberg55b97fe2016-01-28 05:22:45 -08001296 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001297}
1298
minyue78b4d562016-11-30 04:47:39 -08001299void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001300 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1301 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001302 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001303 if (*encoder) {
1304 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001305 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001306 }
1307 });
michaelt566d8202017-01-12 10:17:38 -08001308 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001309}
1310
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001311void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001312 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1313 if (*encoder)
1314 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1315 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001316}
1317
kwiberg55b97fe2016-01-28 05:22:45 -08001318int32_t Channel::SetVADStatus(bool enableVAD,
1319 ACMVADMode mode,
1320 bool disableDTX) {
1321 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1322 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001323 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1324 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1325 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001326 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1327 kTraceError,
1328 "SetVADStatus() failed to set VAD");
1329 return -1;
1330 }
1331 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001332}
1333
kwiberg55b97fe2016-01-28 05:22:45 -08001334int32_t Channel::GetVADStatus(bool& enabledVAD,
1335 ACMVADMode& mode,
1336 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001337 const auto* params = codec_manager_.GetStackParams();
1338 enabledVAD = params->use_cng;
1339 mode = params->vad_mode;
1340 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001341 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001342}
1343
kwiberg55b97fe2016-01-28 05:22:45 -08001344int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001345 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1346}
1347
1348int32_t Channel::SetRecPayloadType(int payload_type,
1349 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001350 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1351 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001352
kwiberg55b97fe2016-01-28 05:22:45 -08001353 if (channel_state_.Get().playing) {
1354 _engineStatisticsPtr->SetLastError(
1355 VE_ALREADY_PLAYING, kTraceError,
1356 "SetRecPayloadType() unable to set PT while playing");
1357 return -1;
1358 }
kwiberg55b97fe2016-01-28 05:22:45 -08001359
kwibergd32bf752017-01-19 07:03:59 -08001360 const CodecInst codec = [&] {
1361 CodecInst c = SdpToCodecInst(payload_type, format);
1362
1363 // Bug 6986: Emulate an old bug that caused us to always choose to decode
1364 // Opus in stereo. To be able to remove this, we first need to fix the
1365 // other half of bug 6986, which is about losing the Opus "stereo"
1366 // parameter.
1367 // TODO(kwiberg): Remove this special case, a.k.a. fix bug 6986.
1368 if (STR_CASE_CMP(codec.plname, "opus") == 0) {
1369 c.channels = 2;
1370 }
1371
1372 return c;
1373 }();
1374
1375 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001376 // De-register the selected codec (RTP/RTCP module and ACM)
1377
1378 int8_t pltype(-1);
1379 CodecInst rxCodec = codec;
1380
1381 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001382 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001383 rxCodec.pltype = pltype;
1384
1385 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1386 _engineStatisticsPtr->SetLastError(
1387 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1388 "SetRecPayloadType() RTP/RTCP-module deregistration "
1389 "failed");
1390 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001391 }
kwiberg55b97fe2016-01-28 05:22:45 -08001392 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1393 _engineStatisticsPtr->SetLastError(
1394 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1395 "SetRecPayloadType() ACM deregistration failed - 1");
1396 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001397 }
kwiberg55b97fe2016-01-28 05:22:45 -08001398 return 0;
1399 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001400
magjed56124bd2016-11-24 09:34:46 -08001401 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001402 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001403 // TODO(kwiberg): Retrying is probably not necessary, since
1404 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001405 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001406 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001407 _engineStatisticsPtr->SetLastError(
1408 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1409 "SetRecPayloadType() RTP/RTCP-module registration failed");
1410 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001411 }
kwiberg55b97fe2016-01-28 05:22:45 -08001412 }
kwibergd32bf752017-01-19 07:03:59 -08001413 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1414 audio_coding_->UnregisterReceiveCodec(payload_type);
1415 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001416 _engineStatisticsPtr->SetLastError(
1417 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1418 "SetRecPayloadType() ACM registration failed - 1");
1419 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001420 }
kwiberg55b97fe2016-01-28 05:22:45 -08001421 }
1422 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001423}
1424
kwiberg55b97fe2016-01-28 05:22:45 -08001425int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1426 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001427 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001428 _engineStatisticsPtr->SetLastError(
1429 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1430 "GetRecPayloadType() failed to retrieve RX payload type");
1431 return -1;
1432 }
1433 codec.pltype = payloadType;
1434 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001435}
1436
kwiberg55b97fe2016-01-28 05:22:45 -08001437int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1438 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1439 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001440
kwiberg55b97fe2016-01-28 05:22:45 -08001441 CodecInst codec;
1442 int32_t samplingFreqHz(-1);
1443 const size_t kMono = 1;
1444 if (frequency == kFreq32000Hz)
1445 samplingFreqHz = 32000;
1446 else if (frequency == kFreq16000Hz)
1447 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001448
kwiberg55b97fe2016-01-28 05:22:45 -08001449 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1450 _engineStatisticsPtr->SetLastError(
1451 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1452 "SetSendCNPayloadType() failed to retrieve default CN codec "
1453 "settings");
1454 return -1;
1455 }
1456
1457 // Modify the payload type (must be set to dynamic range)
1458 codec.pltype = type;
1459
kwibergc8d071e2016-04-06 12:22:38 -07001460 if (!codec_manager_.RegisterEncoder(codec) ||
1461 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001462 _engineStatisticsPtr->SetLastError(
1463 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1464 "SetSendCNPayloadType() failed to register CN to ACM");
1465 return -1;
1466 }
1467
1468 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1469 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1470 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1471 _engineStatisticsPtr->SetLastError(
1472 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1473 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1474 "module");
1475 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001476 }
kwiberg55b97fe2016-01-28 05:22:45 -08001477 }
1478 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001479}
1480
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001481int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001482 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001483 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001484
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001485 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001486 _engineStatisticsPtr->SetLastError(
1487 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001488 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001489 return -1;
1490 }
1491 return 0;
1492}
1493
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001494int Channel::SetOpusDtx(bool enable_dtx) {
1495 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1496 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001497 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001498 : audio_coding_->DisableOpusDtx();
1499 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001500 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1501 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001502 return -1;
1503 }
1504 return 0;
1505}
1506
ivoc85228d62016-07-27 04:53:47 -07001507int Channel::GetOpusDtx(bool* enabled) {
1508 int success = -1;
1509 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1510 if (encoder) {
1511 *enabled = encoder->GetDtx();
1512 success = 0;
1513 }
1514 });
1515 return success;
1516}
1517
minyue7e304322016-10-12 05:00:55 -07001518bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1519 bool success = false;
1520 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1521 if (*encoder) {
1522 success = (*encoder)->EnableAudioNetworkAdaptor(
michaeltbf279fc2017-01-13 06:02:29 -08001523 config_string, event_log_proxy_.get(), Clock::GetRealTimeClock());
minyue7e304322016-10-12 05:00:55 -07001524 }
1525 });
1526 return success;
1527}
1528
1529void Channel::DisableAudioNetworkAdaptor() {
1530 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1531 if (*encoder)
1532 (*encoder)->DisableAudioNetworkAdaptor();
1533 });
1534}
1535
1536void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1537 int max_frame_length_ms) {
1538 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1539 if (*encoder) {
1540 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1541 max_frame_length_ms);
1542 }
1543 });
1544}
1545
mflodman3d7db262016-04-29 00:57:13 -07001546int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001547 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001548 "Channel::RegisterExternalTransport()");
1549
kwiberg55b97fe2016-01-28 05:22:45 -08001550 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001551 if (_externalTransport) {
1552 _engineStatisticsPtr->SetLastError(
1553 VE_INVALID_OPERATION, kTraceError,
1554 "RegisterExternalTransport() external transport already enabled");
1555 return -1;
1556 }
1557 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001558 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001559 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001560}
1561
kwiberg55b97fe2016-01-28 05:22:45 -08001562int32_t Channel::DeRegisterExternalTransport() {
1563 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1564 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001565
kwiberg55b97fe2016-01-28 05:22:45 -08001566 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001567 if (_transportPtr) {
1568 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1569 "DeRegisterExternalTransport() all transport is disabled");
1570 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001571 _engineStatisticsPtr->SetLastError(
1572 VE_INVALID_OPERATION, kTraceWarning,
1573 "DeRegisterExternalTransport() external transport already "
1574 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001575 }
1576 _externalTransport = false;
1577 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001578 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001579}
1580
mflodman3d7db262016-04-29 00:57:13 -07001581int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001582 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001583 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001584 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001585 "Channel::ReceivedRTPPacket()");
1586
1587 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001588 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001589
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001590 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001591 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1592 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1593 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001594 return -1;
1595 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001596 header.payload_type_frequency =
1597 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001598 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001599 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001600 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001601 rtp_receive_statistics_->IncomingPacket(
1602 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001603 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001604
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001605 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001606}
1607
1608bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001609 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001610 const RTPHeader& header,
1611 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001612 if (rtp_payload_registry_->IsRtx(header)) {
1613 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001614 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001615 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001616 assert(packet_length >= header.headerLength);
1617 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001618 PayloadUnion payload_specific;
1619 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001620 &payload_specific)) {
1621 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001622 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001623 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1624 payload_specific, in_order);
1625}
1626
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001627bool Channel::HandleRtxPacket(const uint8_t* packet,
1628 size_t packet_length,
1629 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001630 if (!rtp_payload_registry_->IsRtx(header))
1631 return false;
1632
1633 // Remove the RTX header and parse the original RTP header.
1634 if (packet_length < header.headerLength)
1635 return false;
1636 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1637 return false;
1638 if (restored_packet_in_use_) {
1639 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1640 "Multiple RTX headers detected, dropping packet");
1641 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001642 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001643 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001644 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1645 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001646 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1647 "Incoming RTX packet: invalid RTP header");
1648 return false;
1649 }
1650 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001651 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001652 restored_packet_in_use_ = false;
1653 return ret;
1654}
1655
1656bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1657 StreamStatistician* statistician =
1658 rtp_receive_statistics_->GetStatistician(header.ssrc);
1659 if (!statistician)
1660 return false;
1661 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001662}
1663
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001664bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1665 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001666 // Retransmissions are handled separately if RTX is enabled.
1667 if (rtp_payload_registry_->RtxEnabled())
1668 return false;
1669 StreamStatistician* statistician =
1670 rtp_receive_statistics_->GetStatistician(header.ssrc);
1671 if (!statistician)
1672 return false;
1673 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001674 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001675 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001676 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001677}
1678
mflodman3d7db262016-04-29 00:57:13 -07001679int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001680 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001681 "Channel::ReceivedRTCPPacket()");
1682 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001683 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001684
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001685 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001686 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001687 _engineStatisticsPtr->SetLastError(
1688 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1689 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1690 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001691
Minyue2013aec2015-05-13 14:14:42 +02001692 int64_t rtt = GetRTT(true);
1693 if (rtt == 0) {
1694 // Waiting for valid RTT.
1695 return 0;
1696 }
Erik Språng737336d2016-07-29 12:59:36 +02001697
1698 int64_t nack_window_ms = rtt;
1699 if (nack_window_ms < kMinRetransmissionWindowMs) {
1700 nack_window_ms = kMinRetransmissionWindowMs;
1701 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1702 nack_window_ms = kMaxRetransmissionWindowMs;
1703 }
1704 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1705
minyue7e304322016-10-12 05:00:55 -07001706 // Invoke audio encoders OnReceivedRtt().
1707 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1708 if (*encoder)
1709 (*encoder)->OnReceivedRtt(rtt);
1710 });
1711
Minyue2013aec2015-05-13 14:14:42 +02001712 uint32_t ntp_secs = 0;
1713 uint32_t ntp_frac = 0;
1714 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001715 if (0 !=
1716 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1717 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001718 // Waiting for RTCP.
1719 return 0;
1720 }
1721
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001722 {
tommi31fc21f2016-01-21 10:37:37 -08001723 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001724 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001725 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001726 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001727}
1728
niklase@google.com470e71d2011-07-07 08:21:25 +00001729int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001730 bool loop,
1731 FileFormats format,
1732 int startPosition,
1733 float volumeScaling,
1734 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001735 const CodecInst* codecInst) {
1736 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1737 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1738 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1739 "stopPosition=%d)",
1740 fileName, loop, format, volumeScaling, startPosition,
1741 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001742
kwiberg55b97fe2016-01-28 05:22:45 -08001743 if (channel_state_.Get().output_file_playing) {
1744 _engineStatisticsPtr->SetLastError(
1745 VE_ALREADY_PLAYING, kTraceError,
1746 "StartPlayingFileLocally() is already playing");
1747 return -1;
1748 }
1749
1750 {
1751 rtc::CritScope cs(&_fileCritSect);
1752
kwiberg5a25d952016-08-17 07:31:12 -07001753 if (output_file_player_) {
1754 output_file_player_->RegisterModuleFileCallback(NULL);
1755 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001756 }
1757
kwiberg5b356f42016-09-08 04:32:33 -07001758 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001759 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001760
kwiberg5a25d952016-08-17 07:31:12 -07001761 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001762 _engineStatisticsPtr->SetLastError(
1763 VE_INVALID_ARGUMENT, kTraceError,
1764 "StartPlayingFileLocally() filePlayer format is not correct");
1765 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001766 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001767
kwiberg55b97fe2016-01-28 05:22:45 -08001768 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001769
kwiberg5a25d952016-08-17 07:31:12 -07001770 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001771 fileName, loop, startPosition, volumeScaling, notificationTime,
1772 stopPosition, (const CodecInst*)codecInst) != 0) {
1773 _engineStatisticsPtr->SetLastError(
1774 VE_BAD_FILE, kTraceError,
1775 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001776 output_file_player_->StopPlayingFile();
1777 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001778 return -1;
1779 }
kwiberg5a25d952016-08-17 07:31:12 -07001780 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001781 channel_state_.SetOutputFilePlaying(true);
1782 }
1783
1784 if (RegisterFilePlayingToMixer() != 0)
1785 return -1;
1786
1787 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001788}
1789
1790int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001791 FileFormats format,
1792 int startPosition,
1793 float volumeScaling,
1794 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001795 const CodecInst* codecInst) {
1796 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1797 "Channel::StartPlayingFileLocally(format=%d,"
1798 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1799 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001800
kwiberg55b97fe2016-01-28 05:22:45 -08001801 if (stream == NULL) {
1802 _engineStatisticsPtr->SetLastError(
1803 VE_BAD_FILE, kTraceError,
1804 "StartPlayingFileLocally() NULL as input stream");
1805 return -1;
1806 }
1807
1808 if (channel_state_.Get().output_file_playing) {
1809 _engineStatisticsPtr->SetLastError(
1810 VE_ALREADY_PLAYING, kTraceError,
1811 "StartPlayingFileLocally() is already playing");
1812 return -1;
1813 }
1814
1815 {
1816 rtc::CritScope cs(&_fileCritSect);
1817
1818 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001819 if (output_file_player_) {
1820 output_file_player_->RegisterModuleFileCallback(NULL);
1821 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001822 }
1823
kwiberg55b97fe2016-01-28 05:22:45 -08001824 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001825 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001826 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001827
kwiberg5a25d952016-08-17 07:31:12 -07001828 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001829 _engineStatisticsPtr->SetLastError(
1830 VE_INVALID_ARGUMENT, kTraceError,
1831 "StartPlayingFileLocally() filePlayer format isnot correct");
1832 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001833 }
1834
kwiberg55b97fe2016-01-28 05:22:45 -08001835 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001836
kwiberg4ec01d92016-08-22 08:43:54 -07001837 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001838 volumeScaling, notificationTime,
1839 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001840 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1841 "StartPlayingFile() failed to "
1842 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001843 output_file_player_->StopPlayingFile();
1844 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001845 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001846 }
kwiberg5a25d952016-08-17 07:31:12 -07001847 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001848 channel_state_.SetOutputFilePlaying(true);
1849 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001850
kwiberg55b97fe2016-01-28 05:22:45 -08001851 if (RegisterFilePlayingToMixer() != 0)
1852 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001853
kwiberg55b97fe2016-01-28 05:22:45 -08001854 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001855}
1856
kwiberg55b97fe2016-01-28 05:22:45 -08001857int Channel::StopPlayingFileLocally() {
1858 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1859 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001860
kwiberg55b97fe2016-01-28 05:22:45 -08001861 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001862 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001863 }
1864
1865 {
1866 rtc::CritScope cs(&_fileCritSect);
1867
kwiberg5a25d952016-08-17 07:31:12 -07001868 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001869 _engineStatisticsPtr->SetLastError(
1870 VE_STOP_RECORDING_FAILED, kTraceError,
1871 "StopPlayingFile() could not stop playing");
1872 return -1;
1873 }
kwiberg5a25d952016-08-17 07:31:12 -07001874 output_file_player_->RegisterModuleFileCallback(NULL);
1875 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001876 channel_state_.SetOutputFilePlaying(false);
1877 }
1878 // _fileCritSect cannot be taken while calling
1879 // SetAnonymousMixibilityStatus. Refer to comments in
1880 // StartPlayingFileLocally(const char* ...) for more details.
1881 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1882 _engineStatisticsPtr->SetLastError(
1883 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1884 "StopPlayingFile() failed to stop participant from playing as"
1885 "file in the mixer");
1886 return -1;
1887 }
1888
1889 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001890}
1891
kwiberg55b97fe2016-01-28 05:22:45 -08001892int Channel::IsPlayingFileLocally() const {
1893 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001894}
1895
kwiberg55b97fe2016-01-28 05:22:45 -08001896int Channel::RegisterFilePlayingToMixer() {
1897 // Return success for not registering for file playing to mixer if:
1898 // 1. playing file before playout is started on that channel.
1899 // 2. starting playout without file playing on that channel.
1900 if (!channel_state_.Get().playing ||
1901 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001902 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001903 }
1904
1905 // |_fileCritSect| cannot be taken while calling
1906 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1907 // frames can be pulled by the mixer. Since the frames are generated from
1908 // the file, _fileCritSect will be taken. This would result in a deadlock.
1909 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1910 channel_state_.SetOutputFilePlaying(false);
1911 rtc::CritScope cs(&_fileCritSect);
1912 _engineStatisticsPtr->SetLastError(
1913 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1914 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001915 output_file_player_->StopPlayingFile();
1916 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001917 return -1;
1918 }
1919
1920 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001921}
1922
niklase@google.com470e71d2011-07-07 08:21:25 +00001923int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001924 bool loop,
1925 FileFormats format,
1926 int startPosition,
1927 float volumeScaling,
1928 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001929 const CodecInst* codecInst) {
1930 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1931 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1932 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1933 "stopPosition=%d)",
1934 fileName, loop, format, volumeScaling, startPosition,
1935 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001936
kwiberg55b97fe2016-01-28 05:22:45 -08001937 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001938
kwiberg55b97fe2016-01-28 05:22:45 -08001939 if (channel_state_.Get().input_file_playing) {
1940 _engineStatisticsPtr->SetLastError(
1941 VE_ALREADY_PLAYING, kTraceWarning,
1942 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001943 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001944 }
1945
1946 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001947 if (input_file_player_) {
1948 input_file_player_->RegisterModuleFileCallback(NULL);
1949 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001950 }
1951
1952 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001953 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001954 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001955
kwiberg5a25d952016-08-17 07:31:12 -07001956 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001957 _engineStatisticsPtr->SetLastError(
1958 VE_INVALID_ARGUMENT, kTraceError,
1959 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1960 return -1;
1961 }
1962
1963 const uint32_t notificationTime(0);
1964
kwiberg5a25d952016-08-17 07:31:12 -07001965 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001966 fileName, loop, startPosition, volumeScaling, notificationTime,
1967 stopPosition, (const CodecInst*)codecInst) != 0) {
1968 _engineStatisticsPtr->SetLastError(
1969 VE_BAD_FILE, kTraceError,
1970 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001971 input_file_player_->StopPlayingFile();
1972 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001973 return -1;
1974 }
kwiberg5a25d952016-08-17 07:31:12 -07001975 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001976 channel_state_.SetInputFilePlaying(true);
1977
1978 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001979}
1980
1981int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001982 FileFormats format,
1983 int startPosition,
1984 float volumeScaling,
1985 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001986 const CodecInst* codecInst) {
1987 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1988 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1989 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1990 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001991
kwiberg55b97fe2016-01-28 05:22:45 -08001992 if (stream == NULL) {
1993 _engineStatisticsPtr->SetLastError(
1994 VE_BAD_FILE, kTraceError,
1995 "StartPlayingFileAsMicrophone NULL as input stream");
1996 return -1;
1997 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001998
kwiberg55b97fe2016-01-28 05:22:45 -08001999 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002000
kwiberg55b97fe2016-01-28 05:22:45 -08002001 if (channel_state_.Get().input_file_playing) {
2002 _engineStatisticsPtr->SetLastError(
2003 VE_ALREADY_PLAYING, kTraceWarning,
2004 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002005 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002006 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002007
kwiberg55b97fe2016-01-28 05:22:45 -08002008 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002009 if (input_file_player_) {
2010 input_file_player_->RegisterModuleFileCallback(NULL);
2011 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002012 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002013
kwiberg55b97fe2016-01-28 05:22:45 -08002014 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002015 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002016 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002017
kwiberg5a25d952016-08-17 07:31:12 -07002018 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002019 _engineStatisticsPtr->SetLastError(
2020 VE_INVALID_ARGUMENT, kTraceError,
2021 "StartPlayingInputFile() filePlayer format isnot correct");
2022 return -1;
2023 }
2024
2025 const uint32_t notificationTime(0);
2026
kwiberg4ec01d92016-08-22 08:43:54 -07002027 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2028 notificationTime, stopPosition,
2029 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002030 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2031 "StartPlayingFile() failed to start "
2032 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002033 input_file_player_->StopPlayingFile();
2034 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002035 return -1;
2036 }
2037
kwiberg5a25d952016-08-17 07:31:12 -07002038 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002039 channel_state_.SetInputFilePlaying(true);
2040
2041 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002042}
2043
kwiberg55b97fe2016-01-28 05:22:45 -08002044int Channel::StopPlayingFileAsMicrophone() {
2045 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2046 "Channel::StopPlayingFileAsMicrophone()");
2047
2048 rtc::CritScope cs(&_fileCritSect);
2049
2050 if (!channel_state_.Get().input_file_playing) {
2051 return 0;
2052 }
2053
kwiberg5a25d952016-08-17 07:31:12 -07002054 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002055 _engineStatisticsPtr->SetLastError(
2056 VE_STOP_RECORDING_FAILED, kTraceError,
2057 "StopPlayingFile() could not stop playing");
2058 return -1;
2059 }
kwiberg5a25d952016-08-17 07:31:12 -07002060 input_file_player_->RegisterModuleFileCallback(NULL);
2061 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002062 channel_state_.SetInputFilePlaying(false);
2063
2064 return 0;
2065}
2066
2067int Channel::IsPlayingFileAsMicrophone() const {
2068 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002069}
2070
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002071int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002072 const CodecInst* codecInst) {
2073 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2074 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002075
kwiberg55b97fe2016-01-28 05:22:45 -08002076 if (_outputFileRecording) {
2077 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2078 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002079 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002080 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002081
kwiberg55b97fe2016-01-28 05:22:45 -08002082 FileFormats format;
2083 const uint32_t notificationTime(0); // Not supported in VoE
2084 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002085
kwiberg55b97fe2016-01-28 05:22:45 -08002086 if ((codecInst != NULL) &&
2087 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2088 _engineStatisticsPtr->SetLastError(
2089 VE_BAD_ARGUMENT, kTraceError,
2090 "StartRecordingPlayout() invalid compression");
2091 return (-1);
2092 }
2093 if (codecInst == NULL) {
2094 format = kFileFormatPcm16kHzFile;
2095 codecInst = &dummyCodec;
2096 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2097 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2098 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2099 format = kFileFormatWavFile;
2100 } else {
2101 format = kFileFormatCompressedFile;
2102 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002103
kwiberg55b97fe2016-01-28 05:22:45 -08002104 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002105
kwiberg55b97fe2016-01-28 05:22:45 -08002106 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002107 if (output_file_recorder_) {
2108 output_file_recorder_->RegisterModuleFileCallback(NULL);
2109 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002110 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002111
kwiberg5a25d952016-08-17 07:31:12 -07002112 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002113 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002114 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002115 _engineStatisticsPtr->SetLastError(
2116 VE_INVALID_ARGUMENT, kTraceError,
2117 "StartRecordingPlayout() fileRecorder format isnot correct");
2118 return -1;
2119 }
2120
kwiberg5a25d952016-08-17 07:31:12 -07002121 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002122 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2123 _engineStatisticsPtr->SetLastError(
2124 VE_BAD_FILE, kTraceError,
2125 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002126 output_file_recorder_->StopRecording();
2127 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002128 return -1;
2129 }
kwiberg5a25d952016-08-17 07:31:12 -07002130 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002131 _outputFileRecording = true;
2132
2133 return 0;
2134}
2135
2136int Channel::StartRecordingPlayout(OutStream* stream,
2137 const CodecInst* codecInst) {
2138 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2139 "Channel::StartRecordingPlayout()");
2140
2141 if (_outputFileRecording) {
2142 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2143 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002144 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002145 }
2146
2147 FileFormats format;
2148 const uint32_t notificationTime(0); // Not supported in VoE
2149 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2150
2151 if (codecInst != NULL && codecInst->channels != 1) {
2152 _engineStatisticsPtr->SetLastError(
2153 VE_BAD_ARGUMENT, kTraceError,
2154 "StartRecordingPlayout() invalid compression");
2155 return (-1);
2156 }
2157 if (codecInst == NULL) {
2158 format = kFileFormatPcm16kHzFile;
2159 codecInst = &dummyCodec;
2160 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2161 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2162 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2163 format = kFileFormatWavFile;
2164 } else {
2165 format = kFileFormatCompressedFile;
2166 }
2167
2168 rtc::CritScope cs(&_fileCritSect);
2169
2170 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002171 if (output_file_recorder_) {
2172 output_file_recorder_->RegisterModuleFileCallback(NULL);
2173 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002174 }
2175
kwiberg5a25d952016-08-17 07:31:12 -07002176 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002177 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002178 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002179 _engineStatisticsPtr->SetLastError(
2180 VE_INVALID_ARGUMENT, kTraceError,
2181 "StartRecordingPlayout() fileRecorder format isnot correct");
2182 return -1;
2183 }
2184
kwiberg4ec01d92016-08-22 08:43:54 -07002185 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002186 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002187 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2188 "StartRecordingPlayout() failed to "
2189 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002190 output_file_recorder_->StopRecording();
2191 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002192 return -1;
2193 }
2194
kwiberg5a25d952016-08-17 07:31:12 -07002195 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002196 _outputFileRecording = true;
2197
2198 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002199}
2200
kwiberg55b97fe2016-01-28 05:22:45 -08002201int Channel::StopRecordingPlayout() {
2202 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2203 "Channel::StopRecordingPlayout()");
2204
2205 if (!_outputFileRecording) {
2206 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2207 "StopRecordingPlayout() isnot recording");
2208 return -1;
2209 }
2210
2211 rtc::CritScope cs(&_fileCritSect);
2212
kwiberg5a25d952016-08-17 07:31:12 -07002213 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002214 _engineStatisticsPtr->SetLastError(
2215 VE_STOP_RECORDING_FAILED, kTraceError,
2216 "StopRecording() could not stop recording");
2217 return (-1);
2218 }
kwiberg5a25d952016-08-17 07:31:12 -07002219 output_file_recorder_->RegisterModuleFileCallback(NULL);
2220 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002221 _outputFileRecording = false;
2222
2223 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002224}
2225
kwiberg55b97fe2016-01-28 05:22:45 -08002226void Channel::SetMixWithMicStatus(bool mix) {
2227 rtc::CritScope cs(&_fileCritSect);
2228 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002229}
2230
kwiberg55b97fe2016-01-28 05:22:45 -08002231int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2232 int8_t currentLevel = _outputAudioLevel.Level();
2233 level = static_cast<int32_t>(currentLevel);
2234 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002235}
2236
kwiberg55b97fe2016-01-28 05:22:45 -08002237int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2238 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2239 level = static_cast<int32_t>(currentLevel);
2240 return 0;
2241}
2242
solenberg1c2af8e2016-03-24 10:36:00 -07002243int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002244 rtc::CritScope cs(&volume_settings_critsect_);
2245 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002246 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002247 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002248 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002249}
2250
solenberg1c2af8e2016-03-24 10:36:00 -07002251bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002252 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002253 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002254}
2255
kwiberg55b97fe2016-01-28 05:22:45 -08002256int Channel::SetOutputVolumePan(float left, float right) {
2257 rtc::CritScope cs(&volume_settings_critsect_);
2258 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002259 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002260 _panLeft = left;
2261 _panRight = right;
2262 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002263}
2264
kwiberg55b97fe2016-01-28 05:22:45 -08002265int Channel::GetOutputVolumePan(float& left, float& right) const {
2266 rtc::CritScope cs(&volume_settings_critsect_);
2267 left = _panLeft;
2268 right = _panRight;
2269 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002270}
2271
kwiberg55b97fe2016-01-28 05:22:45 -08002272int Channel::SetChannelOutputVolumeScaling(float scaling) {
2273 rtc::CritScope cs(&volume_settings_critsect_);
2274 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002275 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002276 _outputGain = scaling;
2277 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002278}
2279
kwiberg55b97fe2016-01-28 05:22:45 -08002280int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2281 rtc::CritScope cs(&volume_settings_critsect_);
2282 scaling = _outputGain;
2283 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002284}
2285
solenberg8842c3e2016-03-11 03:06:41 -08002286int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002287 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002288 "Channel::SendTelephoneEventOutband(...)");
2289 RTC_DCHECK_LE(0, event);
2290 RTC_DCHECK_GE(255, event);
2291 RTC_DCHECK_LE(0, duration_ms);
2292 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002293 if (!Sending()) {
2294 return -1;
2295 }
solenberg8842c3e2016-03-11 03:06:41 -08002296 if (_rtpRtcpModule->SendTelephoneEventOutband(
2297 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002298 _engineStatisticsPtr->SetLastError(
2299 VE_SEND_DTMF_FAILED, kTraceWarning,
2300 "SendTelephoneEventOutband() failed to send event");
2301 return -1;
2302 }
2303 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002304}
2305
solenbergffbbcac2016-11-17 05:25:37 -08002306int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2307 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002308 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002309 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002310 RTC_DCHECK_LE(0, payload_type);
2311 RTC_DCHECK_GE(127, payload_type);
2312 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002313 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002314 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002315 memcpy(codec.plname, "telephone-event", 16);
2316 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2317 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2318 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2319 _engineStatisticsPtr->SetLastError(
2320 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2321 "SetSendTelephoneEventPayloadType() failed to register send"
2322 "payload type");
2323 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002324 }
kwiberg55b97fe2016-01-28 05:22:45 -08002325 }
kwiberg55b97fe2016-01-28 05:22:45 -08002326 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002327}
2328
kwiberg55b97fe2016-01-28 05:22:45 -08002329int Channel::VoiceActivityIndicator(int& activity) {
2330 activity = _sendFrameType;
2331 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002332}
2333
kwiberg55b97fe2016-01-28 05:22:45 -08002334int Channel::SetLocalSSRC(unsigned int ssrc) {
2335 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2336 "Channel::SetLocalSSRC()");
2337 if (channel_state_.Get().sending) {
2338 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2339 "SetLocalSSRC() already sending");
2340 return -1;
2341 }
2342 _rtpRtcpModule->SetSSRC(ssrc);
2343 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002344}
2345
kwiberg55b97fe2016-01-28 05:22:45 -08002346int Channel::GetLocalSSRC(unsigned int& ssrc) {
2347 ssrc = _rtpRtcpModule->SSRC();
2348 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002349}
2350
kwiberg55b97fe2016-01-28 05:22:45 -08002351int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2352 ssrc = rtp_receiver_->SSRC();
2353 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002354}
2355
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002356int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002357 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002358 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002359}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002360
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002361int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2362 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002363 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2364 if (enable &&
2365 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2366 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002367 return -1;
2368 }
2369 return 0;
2370}
2371
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002372void Channel::EnableSendTransportSequenceNumber(int id) {
2373 int ret =
2374 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2375 RTC_DCHECK_EQ(0, ret);
2376}
2377
stefan3313ec92016-01-21 06:32:43 -08002378void Channel::EnableReceiveTransportSequenceNumber(int id) {
2379 rtp_header_parser_->DeregisterRtpHeaderExtension(
2380 kRtpExtensionTransportSequenceNumber);
2381 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2382 kRtpExtensionTransportSequenceNumber, id);
2383 RTC_DCHECK(ret);
2384}
2385
stefanbba9dec2016-02-01 04:39:55 -08002386void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002387 RtpPacketSender* rtp_packet_sender,
2388 TransportFeedbackObserver* transport_feedback_observer,
stefan7de8d642017-02-07 07:14:08 -08002389 PacketRouter* packet_router,
2390 RtcpBandwidthObserver* bandwidth_observer) {
stefanbba9dec2016-02-01 04:39:55 -08002391 RTC_DCHECK(rtp_packet_sender);
2392 RTC_DCHECK(transport_feedback_observer);
2393 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002394 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002395 feedback_observer_proxy_->SetTransportFeedbackObserver(
2396 transport_feedback_observer);
2397 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2398 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2399 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002400 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002401 packet_router_ = packet_router;
2402}
2403
stefanbba9dec2016-02-01 04:39:55 -08002404void Channel::RegisterReceiverCongestionControlObjects(
2405 PacketRouter* packet_router) {
2406 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002407 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002408 packet_router_ = packet_router;
2409}
2410
2411void Channel::ResetCongestionControlObjects() {
2412 RTC_DCHECK(packet_router_);
2413 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002414 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002415 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2416 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002417 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002418 packet_router_ = nullptr;
2419 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2420}
2421
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002422void Channel::SetRTCPStatus(bool enable) {
2423 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2424 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002425 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002426}
2427
kwiberg55b97fe2016-01-28 05:22:45 -08002428int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002429 RtcpMode method = _rtpRtcpModule->RTCP();
2430 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002431 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002432}
2433
kwiberg55b97fe2016-01-28 05:22:45 -08002434int Channel::SetRTCP_CNAME(const char cName[256]) {
2435 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2436 "Channel::SetRTCP_CNAME()");
2437 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2438 _engineStatisticsPtr->SetLastError(
2439 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2440 "SetRTCP_CNAME() failed to set RTCP CNAME");
2441 return -1;
2442 }
2443 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002444}
2445
kwiberg55b97fe2016-01-28 05:22:45 -08002446int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2447 if (cName == NULL) {
2448 _engineStatisticsPtr->SetLastError(
2449 VE_INVALID_ARGUMENT, kTraceError,
2450 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2451 return -1;
2452 }
2453 char cname[RTCP_CNAME_SIZE];
2454 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2455 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2456 _engineStatisticsPtr->SetLastError(
2457 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2458 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2459 return -1;
2460 }
2461 strcpy(cName, cname);
2462 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002463}
2464
kwiberg55b97fe2016-01-28 05:22:45 -08002465int Channel::SendApplicationDefinedRTCPPacket(
2466 unsigned char subType,
2467 unsigned int name,
2468 const char* data,
2469 unsigned short dataLengthInBytes) {
2470 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2471 "Channel::SendApplicationDefinedRTCPPacket()");
2472 if (!channel_state_.Get().sending) {
2473 _engineStatisticsPtr->SetLastError(
2474 VE_NOT_SENDING, kTraceError,
2475 "SendApplicationDefinedRTCPPacket() not sending");
2476 return -1;
2477 }
2478 if (NULL == data) {
2479 _engineStatisticsPtr->SetLastError(
2480 VE_INVALID_ARGUMENT, kTraceError,
2481 "SendApplicationDefinedRTCPPacket() invalid data value");
2482 return -1;
2483 }
2484 if (dataLengthInBytes % 4 != 0) {
2485 _engineStatisticsPtr->SetLastError(
2486 VE_INVALID_ARGUMENT, kTraceError,
2487 "SendApplicationDefinedRTCPPacket() invalid length value");
2488 return -1;
2489 }
2490 RtcpMode status = _rtpRtcpModule->RTCP();
2491 if (status == RtcpMode::kOff) {
2492 _engineStatisticsPtr->SetLastError(
2493 VE_RTCP_ERROR, kTraceError,
2494 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2495 return -1;
2496 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002497
kwiberg55b97fe2016-01-28 05:22:45 -08002498 // Create and schedule the RTCP APP packet for transmission
2499 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2500 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2501 _engineStatisticsPtr->SetLastError(
2502 VE_SEND_ERROR, kTraceError,
2503 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2504 return -1;
2505 }
2506 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002507}
2508
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002509int Channel::GetRemoteRTCPReportBlocks(
2510 std::vector<ReportBlock>* report_blocks) {
2511 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002512 _engineStatisticsPtr->SetLastError(
2513 VE_INVALID_ARGUMENT, kTraceError,
2514 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002515 return -1;
2516 }
2517
2518 // Get the report blocks from the latest received RTCP Sender or Receiver
2519 // Report. Each element in the vector contains the sender's SSRC and a
2520 // report block according to RFC 3550.
2521 std::vector<RTCPReportBlock> rtcp_report_blocks;
2522 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002523 return -1;
2524 }
2525
2526 if (rtcp_report_blocks.empty())
2527 return 0;
2528
2529 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2530 for (; it != rtcp_report_blocks.end(); ++it) {
2531 ReportBlock report_block;
2532 report_block.sender_SSRC = it->remoteSSRC;
2533 report_block.source_SSRC = it->sourceSSRC;
2534 report_block.fraction_lost = it->fractionLost;
2535 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2536 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2537 report_block.interarrival_jitter = it->jitter;
2538 report_block.last_SR_timestamp = it->lastSR;
2539 report_block.delay_since_last_SR = it->delaySinceLastSR;
2540 report_blocks->push_back(report_block);
2541 }
2542 return 0;
2543}
2544
kwiberg55b97fe2016-01-28 05:22:45 -08002545int Channel::GetRTPStatistics(CallStatistics& stats) {
2546 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002547
kwiberg55b97fe2016-01-28 05:22:45 -08002548 // The jitter statistics is updated for each received RTP packet and is
2549 // based on received packets.
2550 RtcpStatistics statistics;
2551 StreamStatistician* statistician =
2552 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002553 if (statistician) {
2554 statistician->GetStatistics(&statistics,
2555 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002556 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002557
kwiberg55b97fe2016-01-28 05:22:45 -08002558 stats.fractionLost = statistics.fraction_lost;
2559 stats.cumulativeLost = statistics.cumulative_lost;
2560 stats.extendedMax = statistics.extended_max_sequence_number;
2561 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002562
kwiberg55b97fe2016-01-28 05:22:45 -08002563 // --- RTT
2564 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002565
kwiberg55b97fe2016-01-28 05:22:45 -08002566 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002567
kwiberg55b97fe2016-01-28 05:22:45 -08002568 size_t bytesSent(0);
2569 uint32_t packetsSent(0);
2570 size_t bytesReceived(0);
2571 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002572
kwiberg55b97fe2016-01-28 05:22:45 -08002573 if (statistician) {
2574 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2575 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002576
kwiberg55b97fe2016-01-28 05:22:45 -08002577 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2578 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2579 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2580 " output will not be complete");
2581 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002582
kwiberg55b97fe2016-01-28 05:22:45 -08002583 stats.bytesSent = bytesSent;
2584 stats.packetsSent = packetsSent;
2585 stats.bytesReceived = bytesReceived;
2586 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002587
kwiberg55b97fe2016-01-28 05:22:45 -08002588 // --- Timestamps
2589 {
2590 rtc::CritScope lock(&ts_stats_lock_);
2591 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2592 }
2593 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002594}
2595
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002596int Channel::SetCodecFECStatus(bool enable) {
2597 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2598 "Channel::SetCodecFECStatus()");
2599
kwibergc8d071e2016-04-06 12:22:38 -07002600 if (!codec_manager_.SetCodecFEC(enable) ||
2601 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002602 _engineStatisticsPtr->SetLastError(
2603 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2604 "SetCodecFECStatus() failed to set FEC state");
2605 return -1;
2606 }
2607 return 0;
2608}
2609
2610bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002611 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002612}
2613
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002614void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2615 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002616 // If pacing is enabled we always store packets.
2617 if (!pacing_enabled_)
2618 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002619 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002620 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002621 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002622 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002623 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002624}
2625
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002626// Called when we are missing one or more packets.
2627int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002628 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2629}
2630
kwiberg55b97fe2016-01-28 05:22:45 -08002631uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2632 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2633 "Channel::Demultiplex()");
2634 _audioFrame.CopyFrom(audioFrame);
2635 _audioFrame.id_ = _channelId;
2636 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002637}
2638
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002639void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002640 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002641 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002642 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002643 CodecInst codec;
2644 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002645
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002646 // Never upsample or upmix the capture signal here. This should be done at the
2647 // end of the send chain.
2648 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2649 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2650 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2651 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002652}
2653
kwiberg55b97fe2016-01-28 05:22:45 -08002654uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2655 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2656 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002657
kwiberg55b97fe2016-01-28 05:22:45 -08002658 if (_audioFrame.samples_per_channel_ == 0) {
2659 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2660 "Channel::PrepareEncodeAndSend() invalid audio frame");
2661 return 0xFFFFFFFF;
2662 }
2663
2664 if (channel_state_.Get().input_file_playing) {
2665 MixOrReplaceAudioWithFile(mixingFrequency);
2666 }
2667
solenberg1c2af8e2016-03-24 10:36:00 -07002668 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2669 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002670
2671 if (channel_state_.Get().input_external_media) {
2672 rtc::CritScope cs(&_callbackCritSect);
2673 const bool isStereo = (_audioFrame.num_channels_ == 2);
2674 if (_inputExternalMediaCallbackPtr) {
2675 _inputExternalMediaCallbackPtr->Process(
2676 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
2677 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
2678 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00002679 }
kwiberg55b97fe2016-01-28 05:22:45 -08002680 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002681
kwiberg55b97fe2016-01-28 05:22:45 -08002682 if (_includeAudioLevelIndication) {
2683 size_t length =
2684 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002685 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002686 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002687 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002688 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002689 rms_level_.Analyze(
2690 rtc::ArrayView<const int16_t>(_audioFrame.data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002691 }
kwiberg55b97fe2016-01-28 05:22:45 -08002692 }
solenberg1c2af8e2016-03-24 10:36:00 -07002693 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002694
kwiberg55b97fe2016-01-28 05:22:45 -08002695 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002696}
2697
kwiberg55b97fe2016-01-28 05:22:45 -08002698uint32_t Channel::EncodeAndSend() {
2699 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2700 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002701
kwiberg55b97fe2016-01-28 05:22:45 -08002702 assert(_audioFrame.num_channels_ <= 2);
2703 if (_audioFrame.samples_per_channel_ == 0) {
2704 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2705 "Channel::EncodeAndSend() invalid audio frame");
2706 return 0xFFFFFFFF;
2707 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002708
kwiberg55b97fe2016-01-28 05:22:45 -08002709 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002710
kwiberg55b97fe2016-01-28 05:22:45 -08002711 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002712
kwiberg55b97fe2016-01-28 05:22:45 -08002713 // The ACM resamples internally.
2714 _audioFrame.timestamp_ = _timeStamp;
2715 // This call will trigger AudioPacketizationCallback::SendData if encoding
2716 // is done and payload is ready for packetization and transmission.
2717 // Otherwise, it will return without invoking the callback.
2718 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2719 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2720 "Channel::EncodeAndSend() ACM encoding failed");
2721 return 0xFFFFFFFF;
2722 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002723
kwiberg55b97fe2016-01-28 05:22:45 -08002724 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2725 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002726}
2727
solenberg7602aab2016-11-14 11:30:07 -08002728void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2729 RTC_DCHECK(!channel.channel() ||
2730 channel.channel()->ChannelId() != _channelId);
2731 rtc::CritScope lock(&assoc_send_channel_lock_);
2732 associate_send_channel_ = channel;
2733}
2734
Minyue2013aec2015-05-13 14:14:42 +02002735void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002736 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002737 Channel* channel = associate_send_channel_.channel();
2738 if (channel && channel->ChannelId() == channel_id) {
2739 // If this channel is associated with a send channel of the specified
2740 // Channel ID, disassociate with it.
2741 ChannelOwner ref(NULL);
2742 associate_send_channel_ = ref;
2743 }
2744}
2745
ivoc14d5dbe2016-07-04 07:06:55 -07002746void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2747 event_log_proxy_->SetEventLog(event_log);
2748}
2749
michaelt9332b7d2016-11-30 07:51:13 -08002750void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2751 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2752}
2753
nisse284542b2017-01-10 08:58:32 -08002754void Channel::UpdateOverheadForEncoder() {
2755 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2756 if (*encoder) {
2757 (*encoder)->OnReceivedOverhead(transport_overhead_per_packet_ +
2758 rtp_overhead_per_packet_);
2759 }
2760 });
2761}
2762
2763void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
2764 transport_overhead_per_packet_ = transport_overhead_per_packet;
2765 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002766}
2767
michaeltbf65be52016-12-15 06:24:49 -08002768void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
nisse284542b2017-01-10 08:58:32 -08002769 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2770 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002771}
2772
kwiberg55b97fe2016-01-28 05:22:45 -08002773int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
2774 VoEMediaProcess& processObject) {
2775 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2776 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002777
kwiberg55b97fe2016-01-28 05:22:45 -08002778 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002779
kwiberg55b97fe2016-01-28 05:22:45 -08002780 if (kPlaybackPerChannel == type) {
2781 if (_outputExternalMediaCallbackPtr) {
2782 _engineStatisticsPtr->SetLastError(
2783 VE_INVALID_OPERATION, kTraceError,
2784 "Channel::RegisterExternalMediaProcessing() "
2785 "output external media already enabled");
2786 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002787 }
kwiberg55b97fe2016-01-28 05:22:45 -08002788 _outputExternalMediaCallbackPtr = &processObject;
2789 _outputExternalMedia = true;
2790 } else if (kRecordingPerChannel == type) {
2791 if (_inputExternalMediaCallbackPtr) {
2792 _engineStatisticsPtr->SetLastError(
2793 VE_INVALID_OPERATION, kTraceError,
2794 "Channel::RegisterExternalMediaProcessing() "
2795 "output external media already enabled");
2796 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002797 }
kwiberg55b97fe2016-01-28 05:22:45 -08002798 _inputExternalMediaCallbackPtr = &processObject;
2799 channel_state_.SetInputExternalMedia(true);
2800 }
2801 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002802}
2803
kwiberg55b97fe2016-01-28 05:22:45 -08002804int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
2805 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2806 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002807
kwiberg55b97fe2016-01-28 05:22:45 -08002808 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002809
kwiberg55b97fe2016-01-28 05:22:45 -08002810 if (kPlaybackPerChannel == type) {
2811 if (!_outputExternalMediaCallbackPtr) {
2812 _engineStatisticsPtr->SetLastError(
2813 VE_INVALID_OPERATION, kTraceWarning,
2814 "Channel::DeRegisterExternalMediaProcessing() "
2815 "output external media already disabled");
2816 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002817 }
kwiberg55b97fe2016-01-28 05:22:45 -08002818 _outputExternalMedia = false;
2819 _outputExternalMediaCallbackPtr = NULL;
2820 } else if (kRecordingPerChannel == type) {
2821 if (!_inputExternalMediaCallbackPtr) {
2822 _engineStatisticsPtr->SetLastError(
2823 VE_INVALID_OPERATION, kTraceWarning,
2824 "Channel::DeRegisterExternalMediaProcessing() "
2825 "input external media already disabled");
2826 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002827 }
kwiberg55b97fe2016-01-28 05:22:45 -08002828 channel_state_.SetInputExternalMedia(false);
2829 _inputExternalMediaCallbackPtr = NULL;
2830 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002831
kwiberg55b97fe2016-01-28 05:22:45 -08002832 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002833}
2834
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002835int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08002836 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2837 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002838
kwiberg55b97fe2016-01-28 05:22:45 -08002839 if (channel_state_.Get().playing) {
2840 _engineStatisticsPtr->SetLastError(
2841 VE_INVALID_OPERATION, kTraceError,
2842 "Channel::SetExternalMixing() "
2843 "external mixing cannot be changed while playing.");
2844 return -1;
2845 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002846
kwiberg55b97fe2016-01-28 05:22:45 -08002847 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002848
kwiberg55b97fe2016-01-28 05:22:45 -08002849 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002850}
2851
kwiberg55b97fe2016-01-28 05:22:45 -08002852int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2853 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002854}
2855
wu@webrtc.org24301a62013-12-13 19:17:43 +00002856void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2857 audio_coding_->GetDecodingCallStatistics(stats);
2858}
2859
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002860bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
2861 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08002862 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07002863 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002864 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002865 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002866}
2867
solenberg358057b2015-11-27 10:46:42 -08002868uint32_t Channel::GetDelayEstimate() const {
2869 int jitter_buffer_delay_ms = 0;
2870 int playout_buffer_delay_ms = 0;
2871 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
2872 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
2873}
2874
deadbeef74375882015-08-13 12:09:10 -07002875int Channel::LeastRequiredDelayMs() const {
2876 return audio_coding_->LeastRequiredDelayMs();
2877}
2878
kwiberg55b97fe2016-01-28 05:22:45 -08002879int Channel::SetMinimumPlayoutDelay(int delayMs) {
2880 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2881 "Channel::SetMinimumPlayoutDelay()");
2882 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2883 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2884 _engineStatisticsPtr->SetLastError(
2885 VE_INVALID_ARGUMENT, kTraceError,
2886 "SetMinimumPlayoutDelay() invalid min delay");
2887 return -1;
2888 }
2889 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2890 _engineStatisticsPtr->SetLastError(
2891 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2892 "SetMinimumPlayoutDelay() failed to set min playout delay");
2893 return -1;
2894 }
2895 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002896}
2897
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002898int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002899 uint32_t playout_timestamp_rtp = 0;
2900 {
tommi31fc21f2016-01-21 10:37:37 -08002901 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002902 playout_timestamp_rtp = playout_timestamp_rtp_;
2903 }
kwiberg55b97fe2016-01-28 05:22:45 -08002904 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002905 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002906 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002907 "GetPlayoutTimestamp() failed to retrieve timestamp");
2908 return -1;
2909 }
deadbeef74375882015-08-13 12:09:10 -07002910 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002911 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002912}
2913
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002914int Channel::SetInitTimestamp(unsigned int timestamp) {
2915 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002916 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002917 if (channel_state_.Get().sending) {
2918 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
2919 "SetInitTimestamp() already sending");
2920 return -1;
2921 }
2922 _rtpRtcpModule->SetStartTimestamp(timestamp);
2923 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002924}
2925
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002926int Channel::SetInitSequenceNumber(short sequenceNumber) {
2927 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2928 "Channel::SetInitSequenceNumber()");
2929 if (channel_state_.Get().sending) {
2930 _engineStatisticsPtr->SetLastError(
2931 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
2932 return -1;
2933 }
2934 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
2935 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002936}
2937
kwiberg55b97fe2016-01-28 05:22:45 -08002938int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2939 RtpReceiver** rtp_receiver) const {
2940 *rtpRtcpModule = _rtpRtcpModule.get();
2941 *rtp_receiver = rtp_receiver_.get();
2942 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002943}
2944
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002945// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2946// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002947int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002948 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002949 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002950
kwiberg55b97fe2016-01-28 05:22:45 -08002951 {
2952 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002953
kwiberg5a25d952016-08-17 07:31:12 -07002954 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002955 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2956 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2957 " doesnt exist");
2958 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002959 }
2960
kwiberg4ec01d92016-08-22 08:43:54 -07002961 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002962 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002963 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2964 "Channel::MixOrReplaceAudioWithFile() file mixing "
2965 "failed");
2966 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002967 }
kwiberg55b97fe2016-01-28 05:22:45 -08002968 if (fileSamples == 0) {
2969 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2970 "Channel::MixOrReplaceAudioWithFile() file is ended");
2971 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002972 }
kwiberg55b97fe2016-01-28 05:22:45 -08002973 }
2974
2975 assert(_audioFrame.samples_per_channel_ == fileSamples);
2976
2977 if (_mixFileWithMicrophone) {
2978 // Currently file stream is always mono.
2979 // TODO(xians): Change the code when FilePlayer supports real stereo.
2980 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
2981 1, fileSamples);
2982 } else {
2983 // Replace ACM audio with file.
2984 // Currently file stream is always mono.
2985 // TODO(xians): Change the code when FilePlayer supports real stereo.
2986 _audioFrame.UpdateFrame(
2987 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2988 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2989 }
2990 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002991}
2992
kwiberg55b97fe2016-01-28 05:22:45 -08002993int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2994 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002995
kwibergb7f89d62016-02-17 10:04:18 -08002996 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002997 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002998
kwiberg55b97fe2016-01-28 05:22:45 -08002999 {
3000 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003001
kwiberg5a25d952016-08-17 07:31:12 -07003002 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003003 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3004 "Channel::MixAudioWithFile() file mixing failed");
3005 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003006 }
3007
kwiberg55b97fe2016-01-28 05:22:45 -08003008 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003009 if (output_file_player_->Get10msAudioFromFile(
3010 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003011 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3012 "Channel::MixAudioWithFile() file mixing failed");
3013 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003014 }
kwiberg55b97fe2016-01-28 05:22:45 -08003015 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003016
kwiberg55b97fe2016-01-28 05:22:45 -08003017 if (audioFrame.samples_per_channel_ == fileSamples) {
3018 // Currently file stream is always mono.
3019 // TODO(xians): Change the code when FilePlayer supports real stereo.
3020 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3021 fileSamples);
3022 } else {
3023 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3024 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3025 ") != "
3026 "fileSamples(%" PRIuS ")",
3027 audioFrame.samples_per_channel_, fileSamples);
3028 return -1;
3029 }
3030
3031 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003032}
3033
deadbeef74375882015-08-13 12:09:10 -07003034void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003035 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003036
henrik.lundin96bd5022016-04-06 04:13:56 -07003037 if (!jitter_buffer_playout_timestamp_) {
3038 // This can happen if this channel has not received any RTP packets. In
3039 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003040 return;
3041 }
3042
3043 uint16_t delay_ms = 0;
3044 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003045 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003046 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3047 " delay from the ADM");
3048 _engineStatisticsPtr->SetLastError(
3049 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3050 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3051 return;
3052 }
3053
henrik.lundin96bd5022016-04-06 04:13:56 -07003054 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3055 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003056
3057 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003058 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003059
kwiberg55b97fe2016-01-28 05:22:45 -08003060 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003061 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003062 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003063
3064 {
tommi31fc21f2016-01-21 10:37:37 -08003065 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08003066 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003067 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003068 }
3069 playout_delay_ms_ = delay_ms;
3070 }
3071}
3072
kwiberg55b97fe2016-01-28 05:22:45 -08003073void Channel::RegisterReceiveCodecsToRTPModule() {
3074 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3075 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003076
kwiberg55b97fe2016-01-28 05:22:45 -08003077 CodecInst codec;
3078 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003079
kwiberg55b97fe2016-01-28 05:22:45 -08003080 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3081 // Open up the RTP/RTCP receiver for all supported codecs
3082 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08003083 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08003084 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3085 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3086 " to register %s (%d/%d/%" PRIuS
3087 "/%d) to RTP/RTCP "
3088 "receiver",
3089 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3090 codec.rate);
3091 } else {
3092 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3093 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3094 "(%d/%d/%" PRIuS
3095 "/%d) has been added to the RTP/RTCP "
3096 "receiver",
3097 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3098 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003099 }
kwiberg55b97fe2016-01-28 05:22:45 -08003100 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003101}
3102
kwiberg55b97fe2016-01-28 05:22:45 -08003103int Channel::SetSendRtpHeaderExtension(bool enable,
3104 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003105 unsigned char id) {
3106 int error = 0;
3107 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3108 if (enable) {
3109 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3110 }
3111 return error;
3112}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003113
ossue280cde2016-10-12 11:04:10 -07003114int Channel::GetRtpTimestampRateHz() const {
3115 const auto format = audio_coding_->ReceiveFormat();
3116 // Default to the playout frequency if we've not gotten any packets yet.
3117 // TODO(ossu): Zero clockrate can only happen if we've added an external
3118 // decoder for a format we don't support internally. Remove once that way of
3119 // adding decoders is gone!
3120 return (format && format->clockrate_hz != 0)
3121 ? format->clockrate_hz
3122 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003123}
3124
Minyue2013aec2015-05-13 14:14:42 +02003125int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003126 RtcpMode method = _rtpRtcpModule->RTCP();
3127 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003128 return 0;
3129 }
3130 std::vector<RTCPReportBlock> report_blocks;
3131 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003132
3133 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003134 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003135 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003136 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003137 Channel* channel = associate_send_channel_.channel();
3138 // Tries to get RTT from an associated channel. This is important for
3139 // receive-only channels.
3140 if (channel) {
3141 // To prevent infinite recursion and deadlock, calling GetRTT of
3142 // associate channel should always use "false" for argument:
3143 // |allow_associate_channel|.
3144 rtt = channel->GetRTT(false);
3145 }
3146 }
3147 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003148 }
3149
3150 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3151 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3152 for (; it != report_blocks.end(); ++it) {
3153 if (it->remoteSSRC == remoteSSRC)
3154 break;
3155 }
3156 if (it == report_blocks.end()) {
3157 // We have not received packets with SSRC matching the report blocks.
3158 // To calculate RTT we try with the SSRC of the first report block.
3159 // This is very important for send-only channels where we don't know
3160 // the SSRC of the other end.
3161 remoteSSRC = report_blocks[0].remoteSSRC;
3162 }
Minyue2013aec2015-05-13 14:14:42 +02003163
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003164 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003165 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003166 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003167 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3168 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003169 return 0;
3170 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003171 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003172}
3173
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003174} // namespace voe
3175} // namespace webrtc