blob: 7895e9b9e142a283c311730d953f03e06687c2db [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
Ivo Creusenae856f22015-09-17 16:30:16 +020016#include "webrtc/base/checks.h"
tommi31fc21f2016-01-21 10:37:37 -080017#include "webrtc/base/criticalsection.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000018#include "webrtc/base/format_macros.h"
pbosad856222015-11-27 09:48:36 -080019#include "webrtc/base/logging.h"
Erik Språng737336d2016-07-29 12:59:36 +020020#include "webrtc/base/rate_limiter.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010021#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000022#include "webrtc/base/timeutils.h"
ivoc14d5dbe2016-07-04 07:06:55 -070023#include "webrtc/call/rtc_event_log.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020024#include "webrtc/config.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000025#include "webrtc/modules/audio_device/include/audio_device.h"
26#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010027#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010028#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010029#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
30#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
31#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000032#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010033#include "webrtc/modules/utility/include/audio_frame_operations.h"
34#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010035#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000036#include "webrtc/voice_engine/include/voe_external_media.h"
37#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
38#include "webrtc/voice_engine/output_mixer.h"
39#include "webrtc/voice_engine/statistics.h"
40#include "webrtc/voice_engine/transmit_mixer.h"
41#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000042
andrew@webrtc.org50419b02012-11-14 19:07:54 +000043namespace webrtc {
44namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000045
kwibergc8d071e2016-04-06 12:22:38 -070046namespace {
47
Erik Språng737336d2016-07-29 12:59:36 +020048constexpr int64_t kMaxRetransmissionWindowMs = 1000;
49constexpr int64_t kMinRetransmissionWindowMs = 30;
50
kwibergc8d071e2016-04-06 12:22:38 -070051bool RegisterReceiveCodec(std::unique_ptr<AudioCodingModule>* acm,
52 acm2::RentACodec* rac,
53 const CodecInst& ci) {
kwibergabe95ba2016-06-02 02:58:59 -070054 const int result = (*acm)->RegisterReceiveCodec(
55 ci, [&] { return rac->RentIsacDecoder(ci.plfreq); });
kwibergc8d071e2016-04-06 12:22:38 -070056 return result == 0;
57}
58
59} // namespace
60
solenberg8842c3e2016-03-11 03:06:41 -080061const int kTelephoneEventAttenuationdB = 10;
62
ivoc14d5dbe2016-07-04 07:06:55 -070063class RtcEventLogProxy final : public webrtc::RtcEventLog {
64 public:
65 RtcEventLogProxy() : event_log_(nullptr) {}
66
67 bool StartLogging(const std::string& file_name,
68 int64_t max_size_bytes) override {
69 RTC_NOTREACHED();
70 return false;
71 }
72
73 bool StartLogging(rtc::PlatformFile log_file,
74 int64_t max_size_bytes) override {
75 RTC_NOTREACHED();
76 return false;
77 }
78
79 void StopLogging() override { RTC_NOTREACHED(); }
80
81 void LogVideoReceiveStreamConfig(
82 const webrtc::VideoReceiveStream::Config& config) override {
83 rtc::CritScope lock(&crit_);
84 if (event_log_) {
85 event_log_->LogVideoReceiveStreamConfig(config);
86 }
87 }
88
89 void LogVideoSendStreamConfig(
90 const webrtc::VideoSendStream::Config& config) override {
91 rtc::CritScope lock(&crit_);
92 if (event_log_) {
93 event_log_->LogVideoSendStreamConfig(config);
94 }
95 }
96
97 void LogRtpHeader(webrtc::PacketDirection direction,
98 webrtc::MediaType media_type,
99 const uint8_t* header,
100 size_t packet_length) override {
101 rtc::CritScope lock(&crit_);
102 if (event_log_) {
103 event_log_->LogRtpHeader(direction, media_type, header, packet_length);
104 }
105 }
106
107 void LogRtcpPacket(webrtc::PacketDirection direction,
108 webrtc::MediaType media_type,
109 const uint8_t* packet,
110 size_t length) override {
111 rtc::CritScope lock(&crit_);
112 if (event_log_) {
113 event_log_->LogRtcpPacket(direction, media_type, packet, length);
114 }
115 }
116
117 void LogAudioPlayout(uint32_t ssrc) override {
118 rtc::CritScope lock(&crit_);
119 if (event_log_) {
120 event_log_->LogAudioPlayout(ssrc);
121 }
122 }
123
124 void LogBwePacketLossEvent(int32_t bitrate,
125 uint8_t fraction_loss,
126 int32_t total_packets) override {
127 rtc::CritScope lock(&crit_);
128 if (event_log_) {
129 event_log_->LogBwePacketLossEvent(bitrate, fraction_loss, total_packets);
130 }
131 }
132
133 void SetEventLog(RtcEventLog* event_log) {
134 rtc::CritScope lock(&crit_);
135 event_log_ = event_log;
136 }
137
138 private:
139 rtc::CriticalSection crit_;
140 RtcEventLog* event_log_ GUARDED_BY(crit_);
141 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
142};
143
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100144class TransportFeedbackProxy : public TransportFeedbackObserver {
145 public:
146 TransportFeedbackProxy() : feedback_observer_(nullptr) {
147 pacer_thread_.DetachFromThread();
148 network_thread_.DetachFromThread();
149 }
150
151 void SetTransportFeedbackObserver(
152 TransportFeedbackObserver* feedback_observer) {
153 RTC_DCHECK(thread_checker_.CalledOnValidThread());
154 rtc::CritScope lock(&crit_);
155 feedback_observer_ = feedback_observer;
156 }
157
158 // Implements TransportFeedbackObserver.
159 void AddPacket(uint16_t sequence_number,
160 size_t length,
philipela1ed0b32016-06-01 06:31:17 -0700161 int probe_cluster_id) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100162 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
163 rtc::CritScope lock(&crit_);
164 if (feedback_observer_)
pbos2169d8b2016-06-20 11:53:02 -0700165 feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100166 }
167 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
168 RTC_DCHECK(network_thread_.CalledOnValidThread());
169 rtc::CritScope lock(&crit_);
Stefan Holmer60e43462016-09-07 09:58:20 +0200170 feedback_observer_->OnTransportFeedback(feedback);
171 }
172 std::vector<PacketInfo> GetTransportFeedbackVector() const override {
173 RTC_NOTREACHED();
174 return std::vector<PacketInfo>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100175 }
176
177 private:
178 rtc::CriticalSection crit_;
179 rtc::ThreadChecker thread_checker_;
180 rtc::ThreadChecker pacer_thread_;
181 rtc::ThreadChecker network_thread_;
182 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
183};
184
185class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
186 public:
187 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
188 pacer_thread_.DetachFromThread();
189 }
190
191 void SetSequenceNumberAllocator(
192 TransportSequenceNumberAllocator* seq_num_allocator) {
193 RTC_DCHECK(thread_checker_.CalledOnValidThread());
194 rtc::CritScope lock(&crit_);
195 seq_num_allocator_ = seq_num_allocator;
196 }
197
198 // Implements TransportSequenceNumberAllocator.
199 uint16_t AllocateSequenceNumber() override {
200 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
201 rtc::CritScope lock(&crit_);
202 if (!seq_num_allocator_)
203 return 0;
204 return seq_num_allocator_->AllocateSequenceNumber();
205 }
206
207 private:
208 rtc::CriticalSection crit_;
209 rtc::ThreadChecker thread_checker_;
210 rtc::ThreadChecker pacer_thread_;
211 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
212};
213
214class RtpPacketSenderProxy : public RtpPacketSender {
215 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800216 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100217
218 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
219 RTC_DCHECK(thread_checker_.CalledOnValidThread());
220 rtc::CritScope lock(&crit_);
221 rtp_packet_sender_ = rtp_packet_sender;
222 }
223
224 // Implements RtpPacketSender.
225 void InsertPacket(Priority priority,
226 uint32_t ssrc,
227 uint16_t sequence_number,
228 int64_t capture_time_ms,
229 size_t bytes,
230 bool retransmission) override {
231 rtc::CritScope lock(&crit_);
232 if (rtp_packet_sender_) {
233 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
234 capture_time_ms, bytes, retransmission);
235 }
236 }
237
238 private:
239 rtc::ThreadChecker thread_checker_;
240 rtc::CriticalSection crit_;
241 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
242};
243
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000244// Extend the default RTCP statistics struct with max_jitter, defined as the
245// maximum jitter value seen in an RTCP report block.
246struct ChannelStatistics : public RtcpStatistics {
247 ChannelStatistics() : rtcp(), max_jitter(0) {}
248
249 RtcpStatistics rtcp;
250 uint32_t max_jitter;
251};
252
253// Statistics callback, called at each generation of a new RTCP report block.
254class StatisticsProxy : public RtcpStatisticsCallback {
255 public:
tommi31fc21f2016-01-21 10:37:37 -0800256 StatisticsProxy(uint32_t ssrc) : ssrc_(ssrc) {}
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000257 virtual ~StatisticsProxy() {}
258
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000259 void StatisticsUpdated(const RtcpStatistics& statistics,
260 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000261 if (ssrc != ssrc_)
262 return;
263
tommi31fc21f2016-01-21 10:37:37 -0800264 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000265 stats_.rtcp = statistics;
266 if (statistics.jitter > stats_.max_jitter) {
267 stats_.max_jitter = statistics.jitter;
268 }
269 }
270
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000271 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000272
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000273 ChannelStatistics GetStats() {
tommi31fc21f2016-01-21 10:37:37 -0800274 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000275 return stats_;
276 }
277
278 private:
279 // StatisticsUpdated calls are triggered from threads in the RTP module,
280 // while GetStats calls can be triggered from the public voice engine API,
281 // hence synchronization is needed.
tommi31fc21f2016-01-21 10:37:37 -0800282 rtc::CriticalSection stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000283 const uint32_t ssrc_;
284 ChannelStatistics stats_;
285};
286
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000287class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000288 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000289 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
290 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000291
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000292 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
293 // Not used for Voice Engine.
294 }
295
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000296 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
297 int64_t rtt,
298 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000299 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
300 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
301 // report for VoiceEngine?
302 if (report_blocks.empty())
303 return;
304
305 int fraction_lost_aggregate = 0;
306 int total_number_of_packets = 0;
307
308 // If receiving multiple report blocks, calculate the weighted average based
309 // on the number of packets a report refers to.
310 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
311 block_it != report_blocks.end(); ++block_it) {
312 // Find the previous extended high sequence number for this remote SSRC,
313 // to calculate the number of RTP packets this report refers to. Ignore if
314 // we haven't seen this SSRC before.
315 std::map<uint32_t, uint32_t>::iterator seq_num_it =
316 extended_max_sequence_number_.find(block_it->sourceSSRC);
317 int number_of_packets = 0;
318 if (seq_num_it != extended_max_sequence_number_.end()) {
319 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
320 }
321 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
322 total_number_of_packets += number_of_packets;
323
324 extended_max_sequence_number_[block_it->sourceSSRC] =
325 block_it->extendedHighSeqNum;
326 }
327 int weighted_fraction_lost = 0;
328 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800329 weighted_fraction_lost =
330 (fraction_lost_aggregate + total_number_of_packets / 2) /
331 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000332 }
333 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000334 }
335
336 private:
337 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000338 // Maps remote side ssrc to extended highest sequence number received.
339 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000340};
341
kwiberg55b97fe2016-01-28 05:22:45 -0800342int32_t Channel::SendData(FrameType frameType,
343 uint8_t payloadType,
344 uint32_t timeStamp,
345 const uint8_t* payloadData,
346 size_t payloadSize,
347 const RTPFragmentationHeader* fragmentation) {
348 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
349 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
350 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
351 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000352
kwiberg55b97fe2016-01-28 05:22:45 -0800353 if (_includeAudioLevelIndication) {
354 // Store current audio level in the RTP/RTCP module.
355 // The level will be used in combination with voice-activity state
356 // (frameType) to add an RTP header extension
357 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
358 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000359
kwiberg55b97fe2016-01-28 05:22:45 -0800360 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
361 // packetization.
362 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700363 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800364 (FrameType&)frameType, payloadType, timeStamp,
365 // Leaving the time when this frame was
366 // received from the capture device as
367 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700368 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800369 _engineStatisticsPtr->SetLastError(
370 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
371 "Channel::SendData() failed to send data to RTP/RTCP module");
372 return -1;
373 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000374
kwiberg55b97fe2016-01-28 05:22:45 -0800375 _lastLocalTimeStamp = timeStamp;
376 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000377
kwiberg55b97fe2016-01-28 05:22:45 -0800378 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000379}
380
kwiberg55b97fe2016-01-28 05:22:45 -0800381int32_t Channel::InFrameType(FrameType frame_type) {
382 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
383 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000384
kwiberg55b97fe2016-01-28 05:22:45 -0800385 rtc::CritScope cs(&_callbackCritSect);
386 _sendFrameType = (frame_type == kAudioFrameSpeech);
387 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000388}
389
stefan1d8a5062015-10-02 03:39:33 -0700390bool Channel::SendRtp(const uint8_t* data,
391 size_t len,
392 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800393 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
394 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000395
kwiberg55b97fe2016-01-28 05:22:45 -0800396 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000397
kwiberg55b97fe2016-01-28 05:22:45 -0800398 if (_transportPtr == NULL) {
399 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
400 "Channel::SendPacket() failed to send RTP packet due to"
401 " invalid transport object");
402 return false;
403 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000404
kwiberg55b97fe2016-01-28 05:22:45 -0800405 uint8_t* bufferToSendPtr = (uint8_t*)data;
406 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000407
kwiberg55b97fe2016-01-28 05:22:45 -0800408 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
409 std::string transport_name =
410 _externalTransport ? "external transport" : "WebRtc sockets";
411 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
412 "Channel::SendPacket() RTP transmission using %s failed",
413 transport_name.c_str());
414 return false;
415 }
416 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000417}
418
kwiberg55b97fe2016-01-28 05:22:45 -0800419bool Channel::SendRtcp(const uint8_t* data, size_t len) {
420 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
421 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000422
kwiberg55b97fe2016-01-28 05:22:45 -0800423 rtc::CritScope cs(&_callbackCritSect);
424 if (_transportPtr == NULL) {
425 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
426 "Channel::SendRtcp() failed to send RTCP packet"
427 " due to invalid transport object");
428 return false;
429 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000430
kwiberg55b97fe2016-01-28 05:22:45 -0800431 uint8_t* bufferToSendPtr = (uint8_t*)data;
432 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000433
kwiberg55b97fe2016-01-28 05:22:45 -0800434 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
435 if (n < 0) {
436 std::string transport_name =
437 _externalTransport ? "external transport" : "WebRtc sockets";
438 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
439 "Channel::SendRtcp() transmission using %s failed",
440 transport_name.c_str());
441 return false;
442 }
443 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000444}
445
kwiberg55b97fe2016-01-28 05:22:45 -0800446void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
447 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
448 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000449
kwiberg55b97fe2016-01-28 05:22:45 -0800450 // Update ssrc so that NTP for AV sync can be updated.
451 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000452}
453
Peter Boströmac547a62015-09-17 23:03:57 +0200454void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
455 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
456 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
457 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000458}
459
Peter Boströmac547a62015-09-17 23:03:57 +0200460int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000461 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000462 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000463 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800464 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200465 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800466 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
467 "Channel::OnInitializeDecoder(payloadType=%d, "
468 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
469 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000470
kwiberg55b97fe2016-01-28 05:22:45 -0800471 CodecInst receiveCodec = {0};
472 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000473
kwiberg55b97fe2016-01-28 05:22:45 -0800474 receiveCodec.pltype = payloadType;
475 receiveCodec.plfreq = frequency;
476 receiveCodec.channels = channels;
477 receiveCodec.rate = rate;
478 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000479
kwiberg55b97fe2016-01-28 05:22:45 -0800480 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
481 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000482
kwiberg55b97fe2016-01-28 05:22:45 -0800483 // Register the new codec to the ACM
kwibergc8d071e2016-04-06 12:22:38 -0700484 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, receiveCodec)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800485 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
486 "Channel::OnInitializeDecoder() invalid codec ("
487 "pt=%d, name=%s) received - 1",
488 payloadType, payloadName);
489 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
490 return -1;
491 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
kwiberg55b97fe2016-01-28 05:22:45 -0800493 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000494}
495
kwiberg55b97fe2016-01-28 05:22:45 -0800496int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
497 size_t payloadSize,
498 const WebRtcRTPHeader* rtpHeader) {
499 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
500 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
501 ","
502 " payloadType=%u, audioChannel=%" PRIuS ")",
503 payloadSize, rtpHeader->header.payloadType,
504 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000505
kwiberg55b97fe2016-01-28 05:22:45 -0800506 if (!channel_state_.Get().playing) {
507 // Avoid inserting into NetEQ when we are not playing. Count the
508 // packet as discarded.
509 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
510 "received packet is discarded since playing is not"
511 " activated");
512 _numberOfDiscardedPackets++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000513 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800514 }
515
516 // Push the incoming payload (parsed and ready for decoding) into the ACM
517 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
518 0) {
519 _engineStatisticsPtr->SetLastError(
520 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
521 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
522 return -1;
523 }
524
kwiberg55b97fe2016-01-28 05:22:45 -0800525 int64_t round_trip_time = 0;
526 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
527 NULL);
528
529 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
530 if (!nack_list.empty()) {
531 // Can't use nack_list.data() since it's not supported by all
532 // compilers.
533 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
534 }
535 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000536}
537
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000538bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000539 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000540 RTPHeader header;
541 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
542 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
543 "IncomingPacket invalid RTP header");
544 return false;
545 }
546 header.payload_type_frequency =
547 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
548 if (header.payload_type_frequency < 0)
549 return false;
550 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
551}
552
henrik.lundin42dda502016-05-18 05:36:01 -0700553MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
554 int32_t id,
555 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700556 unsigned int ssrc;
557 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
558 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800559 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700560 bool muted;
561 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
562 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800563 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
564 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
565 // In all likelihood, the audio in this frame is garbage. We return an
566 // error so that the audio mixer module doesn't add it to the mix. As
567 // a result, it won't be played out and the actions skipped here are
568 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700569 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800570 }
henrik.lundina89ab962016-05-18 08:52:45 -0700571
572 if (muted) {
573 // TODO(henrik.lundin): We should be able to do better than this. But we
574 // will have to go through all the cases below where the audio samples may
575 // be used, and handle the muted case in some way.
576 audioFrame->Mute();
577 }
kwiberg55b97fe2016-01-28 05:22:45 -0800578
kwiberg55b97fe2016-01-28 05:22:45 -0800579 // Convert module ID to internal VoE channel ID
580 audioFrame->id_ = VoEChannelId(audioFrame->id_);
581 // Store speech type for dead-or-alive detection
582 _outputSpeechType = audioFrame->speech_type_;
583
584 ChannelState::State state = channel_state_.Get();
585
kwiberg55b97fe2016-01-28 05:22:45 -0800586 {
587 // Pass the audio buffers to an optional sink callback, before applying
588 // scaling/panning, as that applies to the mix operation.
589 // External recipients of the audio (e.g. via AudioTrack), will do their
590 // own mixing/dynamic processing.
591 rtc::CritScope cs(&_callbackCritSect);
592 if (audio_sink_) {
593 AudioSinkInterface::Data data(
594 &audioFrame->data_[0], audioFrame->samples_per_channel_,
595 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
596 audioFrame->timestamp_);
597 audio_sink_->OnData(data);
598 }
599 }
600
601 float output_gain = 1.0f;
602 float left_pan = 1.0f;
603 float right_pan = 1.0f;
604 {
605 rtc::CritScope cs(&volume_settings_critsect_);
606 output_gain = _outputGain;
607 left_pan = _panLeft;
608 right_pan = _panRight;
609 }
610
611 // Output volume scaling
612 if (output_gain < 0.99f || output_gain > 1.01f) {
613 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
614 }
615
616 // Scale left and/or right channel(s) if stereo and master balance is
617 // active
618
619 if (left_pan != 1.0f || right_pan != 1.0f) {
620 if (audioFrame->num_channels_ == 1) {
621 // Emulate stereo mode since panning is active.
622 // The mono signal is copied to both left and right channels here.
623 AudioFrameOperations::MonoToStereo(audioFrame);
624 }
625 // For true stereo mode (when we are receiving a stereo signal), no
626 // action is needed.
627
628 // Do the panning operation (the audio frame contains stereo at this
629 // stage)
630 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
631 }
632
633 // Mix decoded PCM output with file if file mixing is enabled
634 if (state.output_file_playing) {
635 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700636 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800637 }
638
639 // External media
640 if (_outputExternalMedia) {
641 rtc::CritScope cs(&_callbackCritSect);
642 const bool isStereo = (audioFrame->num_channels_ == 2);
643 if (_outputExternalMediaCallbackPtr) {
644 _outputExternalMediaCallbackPtr->Process(
645 _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_,
646 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
647 isStereo);
648 }
649 }
650
651 // Record playout if enabled
652 {
653 rtc::CritScope cs(&_fileCritSect);
654
kwiberg5a25d952016-08-17 07:31:12 -0700655 if (_outputFileRecording && output_file_recorder_) {
656 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800657 }
658 }
659
660 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700661 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800662 _outputAudioLevel.ComputeLevel(*audioFrame);
663
664 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
665 // The first frame with a valid rtp timestamp.
666 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
667 }
668
669 if (capture_start_rtp_time_stamp_ >= 0) {
670 // audioFrame.timestamp_ should be valid from now on.
671
672 // Compute elapsed time.
673 int64_t unwrap_timestamp =
674 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
675 audioFrame->elapsed_time_ms_ =
676 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
677 (GetPlayoutFrequency() / 1000);
678
niklase@google.com470e71d2011-07-07 08:21:25 +0000679 {
kwiberg55b97fe2016-01-28 05:22:45 -0800680 rtc::CritScope lock(&ts_stats_lock_);
681 // Compute ntp time.
682 audioFrame->ntp_time_ms_ =
683 ntp_estimator_.Estimate(audioFrame->timestamp_);
684 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
685 if (audioFrame->ntp_time_ms_ > 0) {
686 // Compute |capture_start_ntp_time_ms_| so that
687 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
688 capture_start_ntp_time_ms_ =
689 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000690 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000691 }
kwiberg55b97fe2016-01-28 05:22:45 -0800692 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000693
henrik.lundin42dda502016-05-18 05:36:01 -0700694 return muted ? MixerParticipant::AudioFrameInfo::kMuted
695 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000696}
697
kwiberg55b97fe2016-01-28 05:22:45 -0800698int32_t Channel::NeededFrequency(int32_t id) const {
699 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
700 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000701
kwiberg55b97fe2016-01-28 05:22:45 -0800702 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000703
kwiberg55b97fe2016-01-28 05:22:45 -0800704 // Determine highest needed receive frequency
705 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000706
kwiberg55b97fe2016-01-28 05:22:45 -0800707 // Return the bigger of playout and receive frequency in the ACM.
708 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
709 highestNeeded = audio_coding_->PlayoutFrequency();
710 } else {
711 highestNeeded = receiveFrequency;
712 }
713
714 // Special case, if we're playing a file on the playout side
715 // we take that frequency into consideration as well
716 // This is not needed on sending side, since the codec will
717 // limit the spectrum anyway.
718 if (channel_state_.Get().output_file_playing) {
719 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700720 if (output_file_player_) {
721 if (output_file_player_->Frequency() > highestNeeded) {
722 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800723 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000724 }
kwiberg55b97fe2016-01-28 05:22:45 -0800725 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000726
kwiberg55b97fe2016-01-28 05:22:45 -0800727 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000728}
729
ossu5f7cfa52016-05-30 08:11:28 -0700730int32_t Channel::CreateChannel(
731 Channel*& channel,
732 int32_t channelId,
733 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700734 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800735 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
736 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
737 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000738
solenberg88499ec2016-09-07 07:34:41 -0700739 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800740 if (channel == NULL) {
741 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
742 "Channel::CreateChannel() unable to allocate memory for"
743 " channel");
744 return -1;
745 }
746 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000747}
748
kwiberg55b97fe2016-01-28 05:22:45 -0800749void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
750 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
751 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
752 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000753
kwiberg55b97fe2016-01-28 05:22:45 -0800754 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000755}
756
kwiberg55b97fe2016-01-28 05:22:45 -0800757void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
758 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
759 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
760 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000761
kwiberg55b97fe2016-01-28 05:22:45 -0800762 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000763}
764
kwiberg55b97fe2016-01-28 05:22:45 -0800765void Channel::PlayFileEnded(int32_t id) {
766 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
767 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000768
kwiberg55b97fe2016-01-28 05:22:45 -0800769 if (id == _inputFilePlayerId) {
770 channel_state_.SetInputFilePlaying(false);
771 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
772 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000773 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800774 } else if (id == _outputFilePlayerId) {
775 channel_state_.SetOutputFilePlaying(false);
776 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
777 "Channel::PlayFileEnded() => output file player module is"
778 " shutdown");
779 }
780}
781
782void Channel::RecordFileEnded(int32_t id) {
783 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
784 "Channel::RecordFileEnded(id=%d)", id);
785
786 assert(id == _outputFileRecorderId);
787
788 rtc::CritScope cs(&_fileCritSect);
789
790 _outputFileRecording = false;
791 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
792 "Channel::RecordFileEnded() => output file recorder module is"
793 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000794}
795
pbos@webrtc.org92135212013-05-14 08:31:39 +0000796Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000797 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700798 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800799 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100800 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700801 event_log_proxy_(new RtcEventLogProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100802 rtp_header_parser_(RtpHeaderParser::Create()),
803 rtp_payload_registry_(
804 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
805 rtp_receive_statistics_(
806 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
807 rtp_receiver_(
808 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100809 this,
810 this,
811 rtp_payload_registry_.get())),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100812 _outputAudioLevel(),
813 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100814 // Avoid conflict with other channels by adding 1024 - 1026,
815 // won't use as much as 1024 channels.
816 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
817 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
818 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
819 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100820 _outputExternalMedia(false),
821 _inputExternalMediaCallbackPtr(NULL),
822 _outputExternalMediaCallbackPtr(NULL),
823 _timeStamp(0), // This is just an offset, RTP module will add it's own
824 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100825 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100826 playout_timestamp_rtp_(0),
827 playout_timestamp_rtcp_(0),
828 playout_delay_ms_(0),
829 _numberOfDiscardedPackets(0),
830 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100831 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
832 capture_start_rtp_time_stamp_(-1),
833 capture_start_ntp_time_ms_(-1),
834 _engineStatisticsPtr(NULL),
835 _outputMixerPtr(NULL),
836 _transmitMixerPtr(NULL),
837 _moduleProcessThreadPtr(NULL),
838 _audioDeviceModulePtr(NULL),
839 _voiceEngineObserverPtr(NULL),
840 _callbackCritSectPtr(NULL),
841 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100842 _sendFrameType(0),
843 _externalMixing(false),
844 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700845 input_mute_(false),
846 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100847 _panLeft(1.0f),
848 _panRight(1.0f),
849 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100850 _lastLocalTimeStamp(0),
851 _lastPayloadType(0),
852 _includeAudioLevelIndication(false),
853 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100854 restored_packet_in_use_(false),
855 rtcp_observer_(new VoERtcpObserver(this)),
856 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock())),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100857 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700858 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800859 feedback_observer_proxy_(new TransportFeedbackProxy()),
860 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700861 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200862 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
863 kMaxRetransmissionWindowMs)),
solenberg88499ec2016-09-07 07:34:41 -0700864 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800865 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
866 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700867 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800868 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700869 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800870 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200871
kwiberg55b97fe2016-01-28 05:22:45 -0800872 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000873
kwiberg55b97fe2016-01-28 05:22:45 -0800874 RtpRtcp::Configuration configuration;
875 configuration.audio = true;
876 configuration.outgoing_transport = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800877 configuration.receive_statistics = rtp_receive_statistics_.get();
878 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800879 if (pacing_enabled_) {
880 configuration.paced_sender = rtp_packet_sender_proxy_.get();
881 configuration.transport_sequence_number_allocator =
882 seq_num_allocator_proxy_.get();
883 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
884 }
ivoc14d5dbe2016-07-04 07:06:55 -0700885 configuration.event_log = &(*event_log_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200886 configuration.retransmission_rate_limiter =
887 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000888
kwiberg55b97fe2016-01-28 05:22:45 -0800889 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100890 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000891
kwiberg55b97fe2016-01-28 05:22:45 -0800892 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
893 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
894 statistics_proxy_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000895}
896
kwiberg55b97fe2016-01-28 05:22:45 -0800897Channel::~Channel() {
898 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
899 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
900 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000901
kwiberg55b97fe2016-01-28 05:22:45 -0800902 if (_outputExternalMedia) {
903 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
904 }
905 if (channel_state_.Get().input_external_media) {
906 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
907 }
908 StopSend();
909 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000910
kwiberg55b97fe2016-01-28 05:22:45 -0800911 {
912 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700913 if (input_file_player_) {
914 input_file_player_->RegisterModuleFileCallback(NULL);
915 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000916 }
kwiberg5a25d952016-08-17 07:31:12 -0700917 if (output_file_player_) {
918 output_file_player_->RegisterModuleFileCallback(NULL);
919 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800920 }
kwiberg5a25d952016-08-17 07:31:12 -0700921 if (output_file_recorder_) {
922 output_file_recorder_->RegisterModuleFileCallback(NULL);
923 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800924 }
925 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000926
kwiberg55b97fe2016-01-28 05:22:45 -0800927 // The order to safely shutdown modules in a channel is:
928 // 1. De-register callbacks in modules
929 // 2. De-register modules in process thread
930 // 3. Destroy modules
931 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
932 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
933 "~Channel() failed to de-register transport callback"
934 " (Audio coding module)");
935 }
936 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
937 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
938 "~Channel() failed to de-register VAD callback"
939 " (Audio coding module)");
940 }
941 // De-register modules in process thread
942 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000943
kwiberg55b97fe2016-01-28 05:22:45 -0800944 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000945}
946
kwiberg55b97fe2016-01-28 05:22:45 -0800947int32_t Channel::Init() {
948 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
949 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000950
kwiberg55b97fe2016-01-28 05:22:45 -0800951 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000952
kwiberg55b97fe2016-01-28 05:22:45 -0800953 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000954
kwiberg55b97fe2016-01-28 05:22:45 -0800955 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
956 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
957 "Channel::Init() must call SetEngineInformation() first");
958 return -1;
959 }
960
961 // --- Add modules to process thread (for periodic schedulation)
962
963 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
964
965 // --- ACM initialization
966
967 if (audio_coding_->InitializeReceiver() == -1) {
968 _engineStatisticsPtr->SetLastError(
969 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
970 "Channel::Init() unable to initialize the ACM - 1");
971 return -1;
972 }
973
974 // --- RTP/RTCP module initialization
975
976 // Ensure that RTCP is enabled by default for the created channel.
977 // Note that, the module will keep generating RTCP until it is explicitly
978 // disabled by the user.
979 // After StopListen (when no sockets exists), RTCP packets will no longer
980 // be transmitted since the Transport object will then be invalid.
kwiberg55b97fe2016-01-28 05:22:45 -0800981 // RTCP is enabled by default.
982 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
983 // --- Register all permanent callbacks
984 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
985 (audio_coding_->RegisterVADCallback(this) == -1);
986
987 if (fail) {
988 _engineStatisticsPtr->SetLastError(
989 VE_CANNOT_INIT_CHANNEL, kTraceError,
990 "Channel::Init() callbacks not registered");
991 return -1;
992 }
993
994 // --- Register all supported codecs to the receiving side of the
995 // RTP/RTCP module
996
997 CodecInst codec;
998 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
999
1000 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1001 // Open up the RTP/RTCP receiver for all supported codecs
1002 if ((audio_coding_->Codec(idx, &codec) == -1) ||
1003 (rtp_receiver_->RegisterReceivePayload(
1004 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1005 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
1006 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1007 "Channel::Init() unable to register %s "
1008 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1009 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1010 codec.rate);
1011 } else {
1012 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1013 "Channel::Init() %s (%d/%d/%" PRIuS
1014 "/%d) has been "
1015 "added to the RTP/RTCP receiver",
1016 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1017 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001018 }
1019
kwiberg55b97fe2016-01-28 05:22:45 -08001020 // Ensure that PCMU is used as default codec on the sending side
1021 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1022 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001023 }
1024
kwiberg55b97fe2016-01-28 05:22:45 -08001025 // Register default PT for outband 'telephone-event'
1026 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001027 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
1028 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001029 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1030 "Channel::Init() failed to register outband "
1031 "'telephone-event' (%d/%d) correctly",
1032 codec.pltype, codec.plfreq);
1033 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001034 }
1035
kwiberg55b97fe2016-01-28 05:22:45 -08001036 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001037 if (!codec_manager_.RegisterEncoder(codec) ||
1038 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
1039 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec) ||
1040 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001041 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1042 "Channel::Init() failed to register CN (%d/%d) "
1043 "correctly - 1",
1044 codec.pltype, codec.plfreq);
1045 }
1046 }
kwiberg55b97fe2016-01-28 05:22:45 -08001047 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001048
kwiberg55b97fe2016-01-28 05:22:45 -08001049 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001050}
1051
kwiberg55b97fe2016-01-28 05:22:45 -08001052int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1053 OutputMixer& outputMixer,
1054 voe::TransmitMixer& transmitMixer,
1055 ProcessThread& moduleProcessThread,
1056 AudioDeviceModule& audioDeviceModule,
1057 VoiceEngineObserver* voiceEngineObserver,
1058 rtc::CriticalSection* callbackCritSect) {
1059 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1060 "Channel::SetEngineInformation()");
1061 _engineStatisticsPtr = &engineStatistics;
1062 _outputMixerPtr = &outputMixer;
1063 _transmitMixerPtr = &transmitMixer,
1064 _moduleProcessThreadPtr = &moduleProcessThread;
1065 _audioDeviceModulePtr = &audioDeviceModule;
1066 _voiceEngineObserverPtr = voiceEngineObserver;
1067 _callbackCritSectPtr = callbackCritSect;
1068 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001069}
1070
kwiberg55b97fe2016-01-28 05:22:45 -08001071int32_t Channel::UpdateLocalTimeStamp() {
1072 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1073 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001074}
1075
kwibergb7f89d62016-02-17 10:04:18 -08001076void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001077 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001078 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001079}
1080
ossu29b1a8d2016-06-13 07:34:51 -07001081const rtc::scoped_refptr<AudioDecoderFactory>&
1082Channel::GetAudioDecoderFactory() const {
1083 return decoder_factory_;
1084}
1085
kwiberg55b97fe2016-01-28 05:22:45 -08001086int32_t Channel::StartPlayout() {
1087 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1088 "Channel::StartPlayout()");
1089 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001090 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001091 }
1092
1093 if (!_externalMixing) {
1094 // Add participant as candidates for mixing.
1095 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1096 _engineStatisticsPtr->SetLastError(
1097 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1098 "StartPlayout() failed to add participant to mixer");
1099 return -1;
1100 }
1101 }
1102
1103 channel_state_.SetPlaying(true);
1104 if (RegisterFilePlayingToMixer() != 0)
1105 return -1;
1106
1107 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001108}
1109
kwiberg55b97fe2016-01-28 05:22:45 -08001110int32_t Channel::StopPlayout() {
1111 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1112 "Channel::StopPlayout()");
1113 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001114 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001115 }
1116
1117 if (!_externalMixing) {
1118 // Remove participant as candidates for mixing
1119 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1120 _engineStatisticsPtr->SetLastError(
1121 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1122 "StopPlayout() failed to remove participant from mixer");
1123 return -1;
1124 }
1125 }
1126
1127 channel_state_.SetPlaying(false);
1128 _outputAudioLevel.Clear();
1129
1130 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001131}
1132
kwiberg55b97fe2016-01-28 05:22:45 -08001133int32_t Channel::StartSend() {
1134 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1135 "Channel::StartSend()");
1136 // Resume the previous sequence number which was reset by StopSend().
1137 // This needs to be done before |sending| is set to true.
1138 if (send_sequence_number_)
1139 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001140
kwiberg55b97fe2016-01-28 05:22:45 -08001141 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001142 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001143 }
1144 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001145
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001146 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001147 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1148 _engineStatisticsPtr->SetLastError(
1149 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1150 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001151 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001152 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001153 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001154 return -1;
1155 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001156
kwiberg55b97fe2016-01-28 05:22:45 -08001157 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001158}
1159
kwiberg55b97fe2016-01-28 05:22:45 -08001160int32_t Channel::StopSend() {
1161 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1162 "Channel::StopSend()");
1163 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001164 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001165 }
1166 channel_state_.SetSending(false);
1167
1168 // Store the sequence number to be able to pick up the same sequence for
1169 // the next StartSend(). This is needed for restarting device, otherwise
1170 // it might cause libSRTP to complain about packets being replayed.
1171 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1172 // CL is landed. See issue
1173 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1174 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1175
1176 // Reset sending SSRC and sequence number and triggers direct transmission
1177 // of RTCP BYE
1178 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1179 _engineStatisticsPtr->SetLastError(
1180 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1181 "StartSend() RTP/RTCP failed to stop sending");
1182 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001183 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001184
1185 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001186}
1187
kwiberg55b97fe2016-01-28 05:22:45 -08001188int32_t Channel::StartReceiving() {
1189 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1190 "Channel::StartReceiving()");
1191 if (channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001192 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001193 }
1194 channel_state_.SetReceiving(true);
1195 _numberOfDiscardedPackets = 0;
1196 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001197}
1198
kwiberg55b97fe2016-01-28 05:22:45 -08001199int32_t Channel::StopReceiving() {
1200 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1201 "Channel::StopReceiving()");
1202 if (!channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001203 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001204 }
1205
1206 channel_state_.SetReceiving(false);
1207 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001208}
1209
kwiberg55b97fe2016-01-28 05:22:45 -08001210int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1211 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1212 "Channel::RegisterVoiceEngineObserver()");
1213 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001214
kwiberg55b97fe2016-01-28 05:22:45 -08001215 if (_voiceEngineObserverPtr) {
1216 _engineStatisticsPtr->SetLastError(
1217 VE_INVALID_OPERATION, kTraceError,
1218 "RegisterVoiceEngineObserver() observer already enabled");
1219 return -1;
1220 }
1221 _voiceEngineObserverPtr = &observer;
1222 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001223}
1224
kwiberg55b97fe2016-01-28 05:22:45 -08001225int32_t Channel::DeRegisterVoiceEngineObserver() {
1226 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1227 "Channel::DeRegisterVoiceEngineObserver()");
1228 rtc::CritScope cs(&_callbackCritSect);
1229
1230 if (!_voiceEngineObserverPtr) {
1231 _engineStatisticsPtr->SetLastError(
1232 VE_INVALID_OPERATION, kTraceWarning,
1233 "DeRegisterVoiceEngineObserver() observer already disabled");
1234 return 0;
1235 }
1236 _voiceEngineObserverPtr = NULL;
1237 return 0;
1238}
1239
1240int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001241 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001242 if (send_codec) {
1243 codec = *send_codec;
1244 return 0;
1245 }
1246 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001247}
1248
kwiberg55b97fe2016-01-28 05:22:45 -08001249int32_t Channel::GetRecCodec(CodecInst& codec) {
1250 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001251}
1252
kwiberg55b97fe2016-01-28 05:22:45 -08001253int32_t Channel::SetSendCodec(const CodecInst& codec) {
1254 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1255 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001256
kwibergc8d071e2016-04-06 12:22:38 -07001257 if (!codec_manager_.RegisterEncoder(codec) ||
1258 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001259 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1260 "SetSendCodec() failed to register codec to ACM");
1261 return -1;
1262 }
1263
1264 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1265 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1266 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1267 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1268 "SetSendCodec() failed to register codec to"
1269 " RTP/RTCP module");
1270 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001271 }
kwiberg55b97fe2016-01-28 05:22:45 -08001272 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001273
kwiberg55b97fe2016-01-28 05:22:45 -08001274 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) {
1275 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1276 "SetSendCodec() failed to set audio packet size");
1277 return -1;
1278 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001279
kwiberg55b97fe2016-01-28 05:22:45 -08001280 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001281}
1282
Ivo Creusenadf89b72015-04-29 16:03:33 +02001283void Channel::SetBitRate(int bitrate_bps) {
1284 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1285 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
1286 audio_coding_->SetBitRate(bitrate_bps);
Erik Språng737336d2016-07-29 12:59:36 +02001287 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001288}
1289
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001290void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001291 network_predictor_->UpdatePacketLossRate(fraction_lost);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001292 uint8_t average_fraction_loss = network_predictor_->GetLossRate();
1293
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001294 // Normalizes rate to 0 - 100.
kwiberg55b97fe2016-01-28 05:22:45 -08001295 if (audio_coding_->SetPacketLossRate(100 * average_fraction_loss / 255) !=
1296 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001297 assert(false); // This should not happen.
1298 }
1299}
1300
kwiberg55b97fe2016-01-28 05:22:45 -08001301int32_t Channel::SetVADStatus(bool enableVAD,
1302 ACMVADMode mode,
1303 bool disableDTX) {
1304 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1305 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001306 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1307 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1308 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001309 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1310 kTraceError,
1311 "SetVADStatus() failed to set VAD");
1312 return -1;
1313 }
1314 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001315}
1316
kwiberg55b97fe2016-01-28 05:22:45 -08001317int32_t Channel::GetVADStatus(bool& enabledVAD,
1318 ACMVADMode& mode,
1319 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001320 const auto* params = codec_manager_.GetStackParams();
1321 enabledVAD = params->use_cng;
1322 mode = params->vad_mode;
1323 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001324 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001325}
1326
kwiberg55b97fe2016-01-28 05:22:45 -08001327int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1328 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1329 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001330
kwiberg55b97fe2016-01-28 05:22:45 -08001331 if (channel_state_.Get().playing) {
1332 _engineStatisticsPtr->SetLastError(
1333 VE_ALREADY_PLAYING, kTraceError,
1334 "SetRecPayloadType() unable to set PT while playing");
1335 return -1;
1336 }
1337 if (channel_state_.Get().receiving) {
1338 _engineStatisticsPtr->SetLastError(
1339 VE_ALREADY_LISTENING, kTraceError,
1340 "SetRecPayloadType() unable to set PT while listening");
1341 return -1;
1342 }
1343
1344 if (codec.pltype == -1) {
1345 // De-register the selected codec (RTP/RTCP module and ACM)
1346
1347 int8_t pltype(-1);
1348 CodecInst rxCodec = codec;
1349
1350 // Get payload type for the given codec
1351 rtp_payload_registry_->ReceivePayloadType(
1352 rxCodec.plname, rxCodec.plfreq, rxCodec.channels,
1353 (rxCodec.rate < 0) ? 0 : rxCodec.rate, &pltype);
1354 rxCodec.pltype = pltype;
1355
1356 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1357 _engineStatisticsPtr->SetLastError(
1358 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1359 "SetRecPayloadType() RTP/RTCP-module deregistration "
1360 "failed");
1361 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001362 }
kwiberg55b97fe2016-01-28 05:22:45 -08001363 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1364 _engineStatisticsPtr->SetLastError(
1365 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1366 "SetRecPayloadType() ACM deregistration failed - 1");
1367 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001368 }
kwiberg55b97fe2016-01-28 05:22:45 -08001369 return 0;
1370 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001371
kwiberg55b97fe2016-01-28 05:22:45 -08001372 if (rtp_receiver_->RegisterReceivePayload(
1373 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1374 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1375 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001376 // TODO(kwiberg): Retrying is probably not necessary, since
1377 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001378 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001379 if (rtp_receiver_->RegisterReceivePayload(
kwiberg55b97fe2016-01-28 05:22:45 -08001380 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1381 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1382 _engineStatisticsPtr->SetLastError(
1383 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1384 "SetRecPayloadType() RTP/RTCP-module registration failed");
1385 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001386 }
kwiberg55b97fe2016-01-28 05:22:45 -08001387 }
kwibergc8d071e2016-04-06 12:22:38 -07001388 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001389 audio_coding_->UnregisterReceiveCodec(codec.pltype);
kwibergc8d071e2016-04-06 12:22:38 -07001390 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001391 _engineStatisticsPtr->SetLastError(
1392 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1393 "SetRecPayloadType() ACM registration failed - 1");
1394 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001395 }
kwiberg55b97fe2016-01-28 05:22:45 -08001396 }
1397 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001398}
1399
kwiberg55b97fe2016-01-28 05:22:45 -08001400int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1401 int8_t payloadType(-1);
1402 if (rtp_payload_registry_->ReceivePayloadType(
1403 codec.plname, codec.plfreq, codec.channels,
1404 (codec.rate < 0) ? 0 : codec.rate, &payloadType) != 0) {
1405 _engineStatisticsPtr->SetLastError(
1406 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1407 "GetRecPayloadType() failed to retrieve RX payload type");
1408 return -1;
1409 }
1410 codec.pltype = payloadType;
1411 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001412}
1413
kwiberg55b97fe2016-01-28 05:22:45 -08001414int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1415 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1416 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001417
kwiberg55b97fe2016-01-28 05:22:45 -08001418 CodecInst codec;
1419 int32_t samplingFreqHz(-1);
1420 const size_t kMono = 1;
1421 if (frequency == kFreq32000Hz)
1422 samplingFreqHz = 32000;
1423 else if (frequency == kFreq16000Hz)
1424 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001425
kwiberg55b97fe2016-01-28 05:22:45 -08001426 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1427 _engineStatisticsPtr->SetLastError(
1428 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1429 "SetSendCNPayloadType() failed to retrieve default CN codec "
1430 "settings");
1431 return -1;
1432 }
1433
1434 // Modify the payload type (must be set to dynamic range)
1435 codec.pltype = type;
1436
kwibergc8d071e2016-04-06 12:22:38 -07001437 if (!codec_manager_.RegisterEncoder(codec) ||
1438 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001439 _engineStatisticsPtr->SetLastError(
1440 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1441 "SetSendCNPayloadType() failed to register CN to ACM");
1442 return -1;
1443 }
1444
1445 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1446 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1447 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1448 _engineStatisticsPtr->SetLastError(
1449 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1450 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1451 "module");
1452 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001453 }
kwiberg55b97fe2016-01-28 05:22:45 -08001454 }
1455 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001456}
1457
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001458int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001459 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001460 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001461
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001462 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001463 _engineStatisticsPtr->SetLastError(
1464 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001465 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001466 return -1;
1467 }
1468 return 0;
1469}
1470
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001471int Channel::SetOpusDtx(bool enable_dtx) {
1472 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1473 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001474 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001475 : audio_coding_->DisableOpusDtx();
1476 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001477 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1478 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001479 return -1;
1480 }
1481 return 0;
1482}
1483
ivoc85228d62016-07-27 04:53:47 -07001484int Channel::GetOpusDtx(bool* enabled) {
1485 int success = -1;
1486 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1487 if (encoder) {
1488 *enabled = encoder->GetDtx();
1489 success = 0;
1490 }
1491 });
1492 return success;
1493}
1494
mflodman3d7db262016-04-29 00:57:13 -07001495int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001496 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001497 "Channel::RegisterExternalTransport()");
1498
kwiberg55b97fe2016-01-28 05:22:45 -08001499 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001500 if (_externalTransport) {
1501 _engineStatisticsPtr->SetLastError(
1502 VE_INVALID_OPERATION, kTraceError,
1503 "RegisterExternalTransport() external transport already enabled");
1504 return -1;
1505 }
1506 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001507 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001508 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001509}
1510
kwiberg55b97fe2016-01-28 05:22:45 -08001511int32_t Channel::DeRegisterExternalTransport() {
1512 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1513 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001514
kwiberg55b97fe2016-01-28 05:22:45 -08001515 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001516 if (_transportPtr) {
1517 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1518 "DeRegisterExternalTransport() all transport is disabled");
1519 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001520 _engineStatisticsPtr->SetLastError(
1521 VE_INVALID_OPERATION, kTraceWarning,
1522 "DeRegisterExternalTransport() external transport already "
1523 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001524 }
1525 _externalTransport = false;
1526 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001527 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001528}
1529
mflodman3d7db262016-04-29 00:57:13 -07001530int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001531 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001532 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001533 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001534 "Channel::ReceivedRTPPacket()");
1535
1536 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001537 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001538
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001539 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001540 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1541 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1542 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001543 return -1;
1544 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001545 header.payload_type_frequency =
1546 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001547 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001548 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001549 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001550 rtp_receive_statistics_->IncomingPacket(
1551 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001552 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001553
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001554 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001555}
1556
1557bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001558 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001559 const RTPHeader& header,
1560 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001561 if (rtp_payload_registry_->IsRtx(header)) {
1562 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001563 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001564 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001565 assert(packet_length >= header.headerLength);
1566 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001567 PayloadUnion payload_specific;
1568 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001569 &payload_specific)) {
1570 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001571 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001572 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1573 payload_specific, in_order);
1574}
1575
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001576bool Channel::HandleRtxPacket(const uint8_t* packet,
1577 size_t packet_length,
1578 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001579 if (!rtp_payload_registry_->IsRtx(header))
1580 return false;
1581
1582 // Remove the RTX header and parse the original RTP header.
1583 if (packet_length < header.headerLength)
1584 return false;
1585 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1586 return false;
1587 if (restored_packet_in_use_) {
1588 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1589 "Multiple RTX headers detected, dropping packet");
1590 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001591 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001592 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001593 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1594 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001595 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1596 "Incoming RTX packet: invalid RTP header");
1597 return false;
1598 }
1599 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001600 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001601 restored_packet_in_use_ = false;
1602 return ret;
1603}
1604
1605bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1606 StreamStatistician* statistician =
1607 rtp_receive_statistics_->GetStatistician(header.ssrc);
1608 if (!statistician)
1609 return false;
1610 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001611}
1612
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001613bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1614 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001615 // Retransmissions are handled separately if RTX is enabled.
1616 if (rtp_payload_registry_->RtxEnabled())
1617 return false;
1618 StreamStatistician* statistician =
1619 rtp_receive_statistics_->GetStatistician(header.ssrc);
1620 if (!statistician)
1621 return false;
1622 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001623 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001624 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001625 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001626}
1627
mflodman3d7db262016-04-29 00:57:13 -07001628int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001629 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001630 "Channel::ReceivedRTCPPacket()");
1631 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001632 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001633
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001634 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001635 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001636 _engineStatisticsPtr->SetLastError(
1637 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1638 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1639 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001640
Minyue2013aec2015-05-13 14:14:42 +02001641 int64_t rtt = GetRTT(true);
1642 if (rtt == 0) {
1643 // Waiting for valid RTT.
1644 return 0;
1645 }
Erik Språng737336d2016-07-29 12:59:36 +02001646
1647 int64_t nack_window_ms = rtt;
1648 if (nack_window_ms < kMinRetransmissionWindowMs) {
1649 nack_window_ms = kMinRetransmissionWindowMs;
1650 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1651 nack_window_ms = kMaxRetransmissionWindowMs;
1652 }
1653 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1654
Minyue2013aec2015-05-13 14:14:42 +02001655 uint32_t ntp_secs = 0;
1656 uint32_t ntp_frac = 0;
1657 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001658 if (0 !=
1659 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1660 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001661 // Waiting for RTCP.
1662 return 0;
1663 }
1664
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001665 {
tommi31fc21f2016-01-21 10:37:37 -08001666 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001667 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001668 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001669 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001670}
1671
niklase@google.com470e71d2011-07-07 08:21:25 +00001672int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001673 bool loop,
1674 FileFormats format,
1675 int startPosition,
1676 float volumeScaling,
1677 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001678 const CodecInst* codecInst) {
1679 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1680 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1681 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1682 "stopPosition=%d)",
1683 fileName, loop, format, volumeScaling, startPosition,
1684 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001685
kwiberg55b97fe2016-01-28 05:22:45 -08001686 if (channel_state_.Get().output_file_playing) {
1687 _engineStatisticsPtr->SetLastError(
1688 VE_ALREADY_PLAYING, kTraceError,
1689 "StartPlayingFileLocally() is already playing");
1690 return -1;
1691 }
1692
1693 {
1694 rtc::CritScope cs(&_fileCritSect);
1695
kwiberg5a25d952016-08-17 07:31:12 -07001696 if (output_file_player_) {
1697 output_file_player_->RegisterModuleFileCallback(NULL);
1698 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001699 }
1700
kwiberg5b356f42016-09-08 04:32:33 -07001701 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001702 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001703
kwiberg5a25d952016-08-17 07:31:12 -07001704 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001705 _engineStatisticsPtr->SetLastError(
1706 VE_INVALID_ARGUMENT, kTraceError,
1707 "StartPlayingFileLocally() filePlayer format is not correct");
1708 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001709 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001710
kwiberg55b97fe2016-01-28 05:22:45 -08001711 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001712
kwiberg5a25d952016-08-17 07:31:12 -07001713 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001714 fileName, loop, startPosition, volumeScaling, notificationTime,
1715 stopPosition, (const CodecInst*)codecInst) != 0) {
1716 _engineStatisticsPtr->SetLastError(
1717 VE_BAD_FILE, kTraceError,
1718 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001719 output_file_player_->StopPlayingFile();
1720 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001721 return -1;
1722 }
kwiberg5a25d952016-08-17 07:31:12 -07001723 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001724 channel_state_.SetOutputFilePlaying(true);
1725 }
1726
1727 if (RegisterFilePlayingToMixer() != 0)
1728 return -1;
1729
1730 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001731}
1732
1733int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001734 FileFormats format,
1735 int startPosition,
1736 float volumeScaling,
1737 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001738 const CodecInst* codecInst) {
1739 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1740 "Channel::StartPlayingFileLocally(format=%d,"
1741 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1742 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001743
kwiberg55b97fe2016-01-28 05:22:45 -08001744 if (stream == NULL) {
1745 _engineStatisticsPtr->SetLastError(
1746 VE_BAD_FILE, kTraceError,
1747 "StartPlayingFileLocally() NULL as input stream");
1748 return -1;
1749 }
1750
1751 if (channel_state_.Get().output_file_playing) {
1752 _engineStatisticsPtr->SetLastError(
1753 VE_ALREADY_PLAYING, kTraceError,
1754 "StartPlayingFileLocally() is already playing");
1755 return -1;
1756 }
1757
1758 {
1759 rtc::CritScope cs(&_fileCritSect);
1760
1761 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001762 if (output_file_player_) {
1763 output_file_player_->RegisterModuleFileCallback(NULL);
1764 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001765 }
1766
kwiberg55b97fe2016-01-28 05:22:45 -08001767 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001768 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001769 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001770
kwiberg5a25d952016-08-17 07:31:12 -07001771 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001772 _engineStatisticsPtr->SetLastError(
1773 VE_INVALID_ARGUMENT, kTraceError,
1774 "StartPlayingFileLocally() filePlayer format isnot correct");
1775 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001776 }
1777
kwiberg55b97fe2016-01-28 05:22:45 -08001778 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001779
kwiberg4ec01d92016-08-22 08:43:54 -07001780 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001781 volumeScaling, notificationTime,
1782 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001783 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1784 "StartPlayingFile() failed to "
1785 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001786 output_file_player_->StopPlayingFile();
1787 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001788 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001789 }
kwiberg5a25d952016-08-17 07:31:12 -07001790 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001791 channel_state_.SetOutputFilePlaying(true);
1792 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001793
kwiberg55b97fe2016-01-28 05:22:45 -08001794 if (RegisterFilePlayingToMixer() != 0)
1795 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001796
kwiberg55b97fe2016-01-28 05:22:45 -08001797 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001798}
1799
kwiberg55b97fe2016-01-28 05:22:45 -08001800int Channel::StopPlayingFileLocally() {
1801 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1802 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001803
kwiberg55b97fe2016-01-28 05:22:45 -08001804 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001805 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001806 }
1807
1808 {
1809 rtc::CritScope cs(&_fileCritSect);
1810
kwiberg5a25d952016-08-17 07:31:12 -07001811 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001812 _engineStatisticsPtr->SetLastError(
1813 VE_STOP_RECORDING_FAILED, kTraceError,
1814 "StopPlayingFile() could not stop playing");
1815 return -1;
1816 }
kwiberg5a25d952016-08-17 07:31:12 -07001817 output_file_player_->RegisterModuleFileCallback(NULL);
1818 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001819 channel_state_.SetOutputFilePlaying(false);
1820 }
1821 // _fileCritSect cannot be taken while calling
1822 // SetAnonymousMixibilityStatus. Refer to comments in
1823 // StartPlayingFileLocally(const char* ...) for more details.
1824 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1825 _engineStatisticsPtr->SetLastError(
1826 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1827 "StopPlayingFile() failed to stop participant from playing as"
1828 "file in the mixer");
1829 return -1;
1830 }
1831
1832 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001833}
1834
kwiberg55b97fe2016-01-28 05:22:45 -08001835int Channel::IsPlayingFileLocally() const {
1836 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001837}
1838
kwiberg55b97fe2016-01-28 05:22:45 -08001839int Channel::RegisterFilePlayingToMixer() {
1840 // Return success for not registering for file playing to mixer if:
1841 // 1. playing file before playout is started on that channel.
1842 // 2. starting playout without file playing on that channel.
1843 if (!channel_state_.Get().playing ||
1844 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001845 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001846 }
1847
1848 // |_fileCritSect| cannot be taken while calling
1849 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1850 // frames can be pulled by the mixer. Since the frames are generated from
1851 // the file, _fileCritSect will be taken. This would result in a deadlock.
1852 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1853 channel_state_.SetOutputFilePlaying(false);
1854 rtc::CritScope cs(&_fileCritSect);
1855 _engineStatisticsPtr->SetLastError(
1856 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1857 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001858 output_file_player_->StopPlayingFile();
1859 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001860 return -1;
1861 }
1862
1863 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001864}
1865
niklase@google.com470e71d2011-07-07 08:21:25 +00001866int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001867 bool loop,
1868 FileFormats format,
1869 int startPosition,
1870 float volumeScaling,
1871 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001872 const CodecInst* codecInst) {
1873 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1874 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1875 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1876 "stopPosition=%d)",
1877 fileName, loop, format, volumeScaling, startPosition,
1878 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001879
kwiberg55b97fe2016-01-28 05:22:45 -08001880 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001881
kwiberg55b97fe2016-01-28 05:22:45 -08001882 if (channel_state_.Get().input_file_playing) {
1883 _engineStatisticsPtr->SetLastError(
1884 VE_ALREADY_PLAYING, kTraceWarning,
1885 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001886 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001887 }
1888
1889 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001890 if (input_file_player_) {
1891 input_file_player_->RegisterModuleFileCallback(NULL);
1892 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001893 }
1894
1895 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001896 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001897 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001898
kwiberg5a25d952016-08-17 07:31:12 -07001899 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001900 _engineStatisticsPtr->SetLastError(
1901 VE_INVALID_ARGUMENT, kTraceError,
1902 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1903 return -1;
1904 }
1905
1906 const uint32_t notificationTime(0);
1907
kwiberg5a25d952016-08-17 07:31:12 -07001908 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001909 fileName, loop, startPosition, volumeScaling, notificationTime,
1910 stopPosition, (const CodecInst*)codecInst) != 0) {
1911 _engineStatisticsPtr->SetLastError(
1912 VE_BAD_FILE, kTraceError,
1913 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001914 input_file_player_->StopPlayingFile();
1915 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001916 return -1;
1917 }
kwiberg5a25d952016-08-17 07:31:12 -07001918 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001919 channel_state_.SetInputFilePlaying(true);
1920
1921 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001922}
1923
1924int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001925 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(format=%d, "
1932 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1933 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001934
kwiberg55b97fe2016-01-28 05:22:45 -08001935 if (stream == NULL) {
1936 _engineStatisticsPtr->SetLastError(
1937 VE_BAD_FILE, kTraceError,
1938 "StartPlayingFileAsMicrophone NULL as input stream");
1939 return -1;
1940 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001941
kwiberg55b97fe2016-01-28 05:22:45 -08001942 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001943
kwiberg55b97fe2016-01-28 05:22:45 -08001944 if (channel_state_.Get().input_file_playing) {
1945 _engineStatisticsPtr->SetLastError(
1946 VE_ALREADY_PLAYING, kTraceWarning,
1947 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001948 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001949 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001950
kwiberg55b97fe2016-01-28 05:22:45 -08001951 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001952 if (input_file_player_) {
1953 input_file_player_->RegisterModuleFileCallback(NULL);
1954 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001955 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001956
kwiberg55b97fe2016-01-28 05:22:45 -08001957 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001958 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001959 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001960
kwiberg5a25d952016-08-17 07:31:12 -07001961 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001962 _engineStatisticsPtr->SetLastError(
1963 VE_INVALID_ARGUMENT, kTraceError,
1964 "StartPlayingInputFile() filePlayer format isnot correct");
1965 return -1;
1966 }
1967
1968 const uint32_t notificationTime(0);
1969
kwiberg4ec01d92016-08-22 08:43:54 -07001970 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
1971 notificationTime, stopPosition,
1972 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001973 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1974 "StartPlayingFile() failed to start "
1975 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001976 input_file_player_->StopPlayingFile();
1977 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001978 return -1;
1979 }
1980
kwiberg5a25d952016-08-17 07:31:12 -07001981 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001982 channel_state_.SetInputFilePlaying(true);
1983
1984 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001985}
1986
kwiberg55b97fe2016-01-28 05:22:45 -08001987int Channel::StopPlayingFileAsMicrophone() {
1988 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1989 "Channel::StopPlayingFileAsMicrophone()");
1990
1991 rtc::CritScope cs(&_fileCritSect);
1992
1993 if (!channel_state_.Get().input_file_playing) {
1994 return 0;
1995 }
1996
kwiberg5a25d952016-08-17 07:31:12 -07001997 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001998 _engineStatisticsPtr->SetLastError(
1999 VE_STOP_RECORDING_FAILED, kTraceError,
2000 "StopPlayingFile() could not stop playing");
2001 return -1;
2002 }
kwiberg5a25d952016-08-17 07:31:12 -07002003 input_file_player_->RegisterModuleFileCallback(NULL);
2004 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002005 channel_state_.SetInputFilePlaying(false);
2006
2007 return 0;
2008}
2009
2010int Channel::IsPlayingFileAsMicrophone() const {
2011 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002012}
2013
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002014int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002015 const CodecInst* codecInst) {
2016 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2017 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002018
kwiberg55b97fe2016-01-28 05:22:45 -08002019 if (_outputFileRecording) {
2020 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2021 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002022 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002023 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002024
kwiberg55b97fe2016-01-28 05:22:45 -08002025 FileFormats format;
2026 const uint32_t notificationTime(0); // Not supported in VoE
2027 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002028
kwiberg55b97fe2016-01-28 05:22:45 -08002029 if ((codecInst != NULL) &&
2030 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2031 _engineStatisticsPtr->SetLastError(
2032 VE_BAD_ARGUMENT, kTraceError,
2033 "StartRecordingPlayout() invalid compression");
2034 return (-1);
2035 }
2036 if (codecInst == NULL) {
2037 format = kFileFormatPcm16kHzFile;
2038 codecInst = &dummyCodec;
2039 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2040 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2041 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2042 format = kFileFormatWavFile;
2043 } else {
2044 format = kFileFormatCompressedFile;
2045 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002046
kwiberg55b97fe2016-01-28 05:22:45 -08002047 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002048
kwiberg55b97fe2016-01-28 05:22:45 -08002049 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002050 if (output_file_recorder_) {
2051 output_file_recorder_->RegisterModuleFileCallback(NULL);
2052 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002053 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002054
kwiberg5a25d952016-08-17 07:31:12 -07002055 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002056 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002057 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002058 _engineStatisticsPtr->SetLastError(
2059 VE_INVALID_ARGUMENT, kTraceError,
2060 "StartRecordingPlayout() fileRecorder format isnot correct");
2061 return -1;
2062 }
2063
kwiberg5a25d952016-08-17 07:31:12 -07002064 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002065 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2066 _engineStatisticsPtr->SetLastError(
2067 VE_BAD_FILE, kTraceError,
2068 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002069 output_file_recorder_->StopRecording();
2070 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002071 return -1;
2072 }
kwiberg5a25d952016-08-17 07:31:12 -07002073 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002074 _outputFileRecording = true;
2075
2076 return 0;
2077}
2078
2079int Channel::StartRecordingPlayout(OutStream* stream,
2080 const CodecInst* codecInst) {
2081 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2082 "Channel::StartRecordingPlayout()");
2083
2084 if (_outputFileRecording) {
2085 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2086 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002087 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002088 }
2089
2090 FileFormats format;
2091 const uint32_t notificationTime(0); // Not supported in VoE
2092 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2093
2094 if (codecInst != NULL && codecInst->channels != 1) {
2095 _engineStatisticsPtr->SetLastError(
2096 VE_BAD_ARGUMENT, kTraceError,
2097 "StartRecordingPlayout() invalid compression");
2098 return (-1);
2099 }
2100 if (codecInst == NULL) {
2101 format = kFileFormatPcm16kHzFile;
2102 codecInst = &dummyCodec;
2103 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2104 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2105 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2106 format = kFileFormatWavFile;
2107 } else {
2108 format = kFileFormatCompressedFile;
2109 }
2110
2111 rtc::CritScope cs(&_fileCritSect);
2112
2113 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002114 if (output_file_recorder_) {
2115 output_file_recorder_->RegisterModuleFileCallback(NULL);
2116 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002117 }
2118
kwiberg5a25d952016-08-17 07:31:12 -07002119 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002120 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002121 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002122 _engineStatisticsPtr->SetLastError(
2123 VE_INVALID_ARGUMENT, kTraceError,
2124 "StartRecordingPlayout() fileRecorder format isnot correct");
2125 return -1;
2126 }
2127
kwiberg4ec01d92016-08-22 08:43:54 -07002128 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002129 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002130 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2131 "StartRecordingPlayout() failed to "
2132 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002133 output_file_recorder_->StopRecording();
2134 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002135 return -1;
2136 }
2137
kwiberg5a25d952016-08-17 07:31:12 -07002138 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002139 _outputFileRecording = true;
2140
2141 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002142}
2143
kwiberg55b97fe2016-01-28 05:22:45 -08002144int Channel::StopRecordingPlayout() {
2145 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2146 "Channel::StopRecordingPlayout()");
2147
2148 if (!_outputFileRecording) {
2149 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2150 "StopRecordingPlayout() isnot recording");
2151 return -1;
2152 }
2153
2154 rtc::CritScope cs(&_fileCritSect);
2155
kwiberg5a25d952016-08-17 07:31:12 -07002156 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002157 _engineStatisticsPtr->SetLastError(
2158 VE_STOP_RECORDING_FAILED, kTraceError,
2159 "StopRecording() could not stop recording");
2160 return (-1);
2161 }
kwiberg5a25d952016-08-17 07:31:12 -07002162 output_file_recorder_->RegisterModuleFileCallback(NULL);
2163 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002164 _outputFileRecording = false;
2165
2166 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002167}
2168
kwiberg55b97fe2016-01-28 05:22:45 -08002169void Channel::SetMixWithMicStatus(bool mix) {
2170 rtc::CritScope cs(&_fileCritSect);
2171 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002172}
2173
kwiberg55b97fe2016-01-28 05:22:45 -08002174int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2175 int8_t currentLevel = _outputAudioLevel.Level();
2176 level = static_cast<int32_t>(currentLevel);
2177 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002178}
2179
kwiberg55b97fe2016-01-28 05:22:45 -08002180int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2181 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2182 level = static_cast<int32_t>(currentLevel);
2183 return 0;
2184}
2185
solenberg1c2af8e2016-03-24 10:36:00 -07002186int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002187 rtc::CritScope cs(&volume_settings_critsect_);
2188 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002189 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002190 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002191 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002192}
2193
solenberg1c2af8e2016-03-24 10:36:00 -07002194bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002195 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002196 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002197}
2198
kwiberg55b97fe2016-01-28 05:22:45 -08002199int Channel::SetOutputVolumePan(float left, float right) {
2200 rtc::CritScope cs(&volume_settings_critsect_);
2201 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002202 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002203 _panLeft = left;
2204 _panRight = right;
2205 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002206}
2207
kwiberg55b97fe2016-01-28 05:22:45 -08002208int Channel::GetOutputVolumePan(float& left, float& right) const {
2209 rtc::CritScope cs(&volume_settings_critsect_);
2210 left = _panLeft;
2211 right = _panRight;
2212 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002213}
2214
kwiberg55b97fe2016-01-28 05:22:45 -08002215int Channel::SetChannelOutputVolumeScaling(float scaling) {
2216 rtc::CritScope cs(&volume_settings_critsect_);
2217 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002218 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002219 _outputGain = scaling;
2220 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002221}
2222
kwiberg55b97fe2016-01-28 05:22:45 -08002223int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2224 rtc::CritScope cs(&volume_settings_critsect_);
2225 scaling = _outputGain;
2226 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002227}
2228
solenberg8842c3e2016-03-11 03:06:41 -08002229int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002230 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002231 "Channel::SendTelephoneEventOutband(...)");
2232 RTC_DCHECK_LE(0, event);
2233 RTC_DCHECK_GE(255, event);
2234 RTC_DCHECK_LE(0, duration_ms);
2235 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002236 if (!Sending()) {
2237 return -1;
2238 }
solenberg8842c3e2016-03-11 03:06:41 -08002239 if (_rtpRtcpModule->SendTelephoneEventOutband(
2240 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002241 _engineStatisticsPtr->SetLastError(
2242 VE_SEND_DTMF_FAILED, kTraceWarning,
2243 "SendTelephoneEventOutband() failed to send event");
2244 return -1;
2245 }
2246 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002247}
2248
solenberg31642aa2016-03-14 08:00:37 -07002249int Channel::SetSendTelephoneEventPayloadType(int payload_type) {
kwiberg55b97fe2016-01-28 05:22:45 -08002250 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002251 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002252 RTC_DCHECK_LE(0, payload_type);
2253 RTC_DCHECK_GE(127, payload_type);
2254 CodecInst codec = {0};
kwiberg55b97fe2016-01-28 05:22:45 -08002255 codec.plfreq = 8000;
solenberg31642aa2016-03-14 08:00:37 -07002256 codec.pltype = payload_type;
kwiberg55b97fe2016-01-28 05:22:45 -08002257 memcpy(codec.plname, "telephone-event", 16);
2258 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2259 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2260 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2261 _engineStatisticsPtr->SetLastError(
2262 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2263 "SetSendTelephoneEventPayloadType() failed to register send"
2264 "payload type");
2265 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002266 }
kwiberg55b97fe2016-01-28 05:22:45 -08002267 }
kwiberg55b97fe2016-01-28 05:22:45 -08002268 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002269}
2270
kwiberg55b97fe2016-01-28 05:22:45 -08002271int Channel::VoiceActivityIndicator(int& activity) {
2272 activity = _sendFrameType;
2273 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002274}
2275
kwiberg55b97fe2016-01-28 05:22:45 -08002276int Channel::SetLocalSSRC(unsigned int ssrc) {
2277 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2278 "Channel::SetLocalSSRC()");
2279 if (channel_state_.Get().sending) {
2280 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2281 "SetLocalSSRC() already sending");
2282 return -1;
2283 }
2284 _rtpRtcpModule->SetSSRC(ssrc);
2285 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002286}
2287
kwiberg55b97fe2016-01-28 05:22:45 -08002288int Channel::GetLocalSSRC(unsigned int& ssrc) {
2289 ssrc = _rtpRtcpModule->SSRC();
2290 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002291}
2292
kwiberg55b97fe2016-01-28 05:22:45 -08002293int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2294 ssrc = rtp_receiver_->SSRC();
2295 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002296}
2297
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002298int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002299 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002300 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002301}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002302
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002303int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2304 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002305 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2306 if (enable &&
2307 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2308 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002309 return -1;
2310 }
2311 return 0;
2312}
2313
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002314int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2315 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2316}
2317
2318int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2319 rtp_header_parser_->DeregisterRtpHeaderExtension(
2320 kRtpExtensionAbsoluteSendTime);
kwiberg55b97fe2016-01-28 05:22:45 -08002321 if (enable &&
2322 !rtp_header_parser_->RegisterRtpHeaderExtension(
2323 kRtpExtensionAbsoluteSendTime, id)) {
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002324 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002325 }
2326 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002327}
2328
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002329void Channel::EnableSendTransportSequenceNumber(int id) {
2330 int ret =
2331 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2332 RTC_DCHECK_EQ(0, ret);
2333}
2334
stefan3313ec92016-01-21 06:32:43 -08002335void Channel::EnableReceiveTransportSequenceNumber(int id) {
2336 rtp_header_parser_->DeregisterRtpHeaderExtension(
2337 kRtpExtensionTransportSequenceNumber);
2338 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2339 kRtpExtensionTransportSequenceNumber, id);
2340 RTC_DCHECK(ret);
2341}
2342
stefanbba9dec2016-02-01 04:39:55 -08002343void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002344 RtpPacketSender* rtp_packet_sender,
2345 TransportFeedbackObserver* transport_feedback_observer,
2346 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002347 RTC_DCHECK(rtp_packet_sender);
2348 RTC_DCHECK(transport_feedback_observer);
2349 RTC_DCHECK(packet_router && !packet_router_);
2350 feedback_observer_proxy_->SetTransportFeedbackObserver(
2351 transport_feedback_observer);
2352 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2353 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2354 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002355 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002356 packet_router_ = packet_router;
2357}
2358
stefanbba9dec2016-02-01 04:39:55 -08002359void Channel::RegisterReceiverCongestionControlObjects(
2360 PacketRouter* packet_router) {
2361 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002362 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002363 packet_router_ = packet_router;
2364}
2365
2366void Channel::ResetCongestionControlObjects() {
2367 RTC_DCHECK(packet_router_);
2368 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2369 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2370 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002371 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002372 packet_router_ = nullptr;
2373 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2374}
2375
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002376void Channel::SetRTCPStatus(bool enable) {
2377 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2378 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002379 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002380}
2381
kwiberg55b97fe2016-01-28 05:22:45 -08002382int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002383 RtcpMode method = _rtpRtcpModule->RTCP();
2384 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002385 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002386}
2387
kwiberg55b97fe2016-01-28 05:22:45 -08002388int Channel::SetRTCP_CNAME(const char cName[256]) {
2389 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2390 "Channel::SetRTCP_CNAME()");
2391 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2392 _engineStatisticsPtr->SetLastError(
2393 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2394 "SetRTCP_CNAME() failed to set RTCP CNAME");
2395 return -1;
2396 }
2397 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002398}
2399
kwiberg55b97fe2016-01-28 05:22:45 -08002400int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2401 if (cName == NULL) {
2402 _engineStatisticsPtr->SetLastError(
2403 VE_INVALID_ARGUMENT, kTraceError,
2404 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2405 return -1;
2406 }
2407 char cname[RTCP_CNAME_SIZE];
2408 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2409 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2410 _engineStatisticsPtr->SetLastError(
2411 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2412 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2413 return -1;
2414 }
2415 strcpy(cName, cname);
2416 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002417}
2418
kwiberg55b97fe2016-01-28 05:22:45 -08002419int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2420 unsigned int& NTPLow,
2421 unsigned int& timestamp,
2422 unsigned int& playoutTimestamp,
2423 unsigned int* jitter,
2424 unsigned short* fractionLost) {
2425 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002426
kwiberg55b97fe2016-01-28 05:22:45 -08002427 RTCPSenderInfo senderInfo;
2428 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2429 _engineStatisticsPtr->SetLastError(
2430 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2431 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2432 "side");
2433 return -1;
2434 }
2435
2436 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2437 // and octet count)
2438 NTPHigh = senderInfo.NTPseconds;
2439 NTPLow = senderInfo.NTPfraction;
2440 timestamp = senderInfo.RTPtimeStamp;
2441
2442 // --- Locally derived information
2443
2444 // This value is updated on each incoming RTCP packet (0 when no packet
2445 // has been received)
2446 playoutTimestamp = playout_timestamp_rtcp_;
2447
2448 if (NULL != jitter || NULL != fractionLost) {
2449 // Get all RTCP receiver report blocks that have been received on this
2450 // channel. If we receive RTP packets from a remote source we know the
2451 // remote SSRC and use the report block from him.
2452 // Otherwise use the first report block.
2453 std::vector<RTCPReportBlock> remote_stats;
2454 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2455 remote_stats.empty()) {
2456 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2457 "GetRemoteRTCPData() failed to measure statistics due"
2458 " to lack of received RTP and/or RTCP packets");
2459 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002460 }
2461
kwiberg55b97fe2016-01-28 05:22:45 -08002462 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2463 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2464 for (; it != remote_stats.end(); ++it) {
2465 if (it->remoteSSRC == remoteSSRC)
2466 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002467 }
kwiberg55b97fe2016-01-28 05:22:45 -08002468
2469 if (it == remote_stats.end()) {
2470 // If we have not received any RTCP packets from this SSRC it probably
2471 // means that we have not received any RTP packets.
2472 // Use the first received report block instead.
2473 it = remote_stats.begin();
2474 remoteSSRC = it->remoteSSRC;
2475 }
2476
2477 if (jitter) {
2478 *jitter = it->jitter;
2479 }
2480
2481 if (fractionLost) {
2482 *fractionLost = it->fractionLost;
2483 }
2484 }
2485 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002486}
2487
kwiberg55b97fe2016-01-28 05:22:45 -08002488int Channel::SendApplicationDefinedRTCPPacket(
2489 unsigned char subType,
2490 unsigned int name,
2491 const char* data,
2492 unsigned short dataLengthInBytes) {
2493 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2494 "Channel::SendApplicationDefinedRTCPPacket()");
2495 if (!channel_state_.Get().sending) {
2496 _engineStatisticsPtr->SetLastError(
2497 VE_NOT_SENDING, kTraceError,
2498 "SendApplicationDefinedRTCPPacket() not sending");
2499 return -1;
2500 }
2501 if (NULL == data) {
2502 _engineStatisticsPtr->SetLastError(
2503 VE_INVALID_ARGUMENT, kTraceError,
2504 "SendApplicationDefinedRTCPPacket() invalid data value");
2505 return -1;
2506 }
2507 if (dataLengthInBytes % 4 != 0) {
2508 _engineStatisticsPtr->SetLastError(
2509 VE_INVALID_ARGUMENT, kTraceError,
2510 "SendApplicationDefinedRTCPPacket() invalid length value");
2511 return -1;
2512 }
2513 RtcpMode status = _rtpRtcpModule->RTCP();
2514 if (status == RtcpMode::kOff) {
2515 _engineStatisticsPtr->SetLastError(
2516 VE_RTCP_ERROR, kTraceError,
2517 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2518 return -1;
2519 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002520
kwiberg55b97fe2016-01-28 05:22:45 -08002521 // Create and schedule the RTCP APP packet for transmission
2522 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2523 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2524 _engineStatisticsPtr->SetLastError(
2525 VE_SEND_ERROR, kTraceError,
2526 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2527 return -1;
2528 }
2529 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002530}
2531
kwiberg55b97fe2016-01-28 05:22:45 -08002532int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2533 unsigned int& maxJitterMs,
2534 unsigned int& discardedPackets) {
2535 // The jitter statistics is updated for each received RTP packet and is
2536 // based on received packets.
2537 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2538 // If RTCP is off, there is no timed thread in the RTCP module regularly
2539 // generating new stats, trigger the update manually here instead.
2540 StreamStatistician* statistician =
2541 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2542 if (statistician) {
2543 // Don't use returned statistics, use data from proxy instead so that
2544 // max jitter can be fetched atomically.
2545 RtcpStatistics s;
2546 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002547 }
kwiberg55b97fe2016-01-28 05:22:45 -08002548 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002549
kwiberg55b97fe2016-01-28 05:22:45 -08002550 ChannelStatistics stats = statistics_proxy_->GetStats();
2551 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2552 if (playoutFrequency > 0) {
2553 // Scale RTP statistics given the current playout frequency
2554 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2555 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2556 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002557
kwiberg55b97fe2016-01-28 05:22:45 -08002558 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002559
kwiberg55b97fe2016-01-28 05:22:45 -08002560 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002561}
2562
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002563int Channel::GetRemoteRTCPReportBlocks(
2564 std::vector<ReportBlock>* report_blocks) {
2565 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002566 _engineStatisticsPtr->SetLastError(
2567 VE_INVALID_ARGUMENT, kTraceError,
2568 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002569 return -1;
2570 }
2571
2572 // Get the report blocks from the latest received RTCP Sender or Receiver
2573 // Report. Each element in the vector contains the sender's SSRC and a
2574 // report block according to RFC 3550.
2575 std::vector<RTCPReportBlock> rtcp_report_blocks;
2576 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002577 return -1;
2578 }
2579
2580 if (rtcp_report_blocks.empty())
2581 return 0;
2582
2583 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2584 for (; it != rtcp_report_blocks.end(); ++it) {
2585 ReportBlock report_block;
2586 report_block.sender_SSRC = it->remoteSSRC;
2587 report_block.source_SSRC = it->sourceSSRC;
2588 report_block.fraction_lost = it->fractionLost;
2589 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2590 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2591 report_block.interarrival_jitter = it->jitter;
2592 report_block.last_SR_timestamp = it->lastSR;
2593 report_block.delay_since_last_SR = it->delaySinceLastSR;
2594 report_blocks->push_back(report_block);
2595 }
2596 return 0;
2597}
2598
kwiberg55b97fe2016-01-28 05:22:45 -08002599int Channel::GetRTPStatistics(CallStatistics& stats) {
2600 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002601
kwiberg55b97fe2016-01-28 05:22:45 -08002602 // The jitter statistics is updated for each received RTP packet and is
2603 // based on received packets.
2604 RtcpStatistics statistics;
2605 StreamStatistician* statistician =
2606 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002607 if (statistician) {
2608 statistician->GetStatistics(&statistics,
2609 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002610 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002611
kwiberg55b97fe2016-01-28 05:22:45 -08002612 stats.fractionLost = statistics.fraction_lost;
2613 stats.cumulativeLost = statistics.cumulative_lost;
2614 stats.extendedMax = statistics.extended_max_sequence_number;
2615 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002616
kwiberg55b97fe2016-01-28 05:22:45 -08002617 // --- RTT
2618 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002619
kwiberg55b97fe2016-01-28 05:22:45 -08002620 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002621
kwiberg55b97fe2016-01-28 05:22:45 -08002622 size_t bytesSent(0);
2623 uint32_t packetsSent(0);
2624 size_t bytesReceived(0);
2625 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002626
kwiberg55b97fe2016-01-28 05:22:45 -08002627 if (statistician) {
2628 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2629 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002630
kwiberg55b97fe2016-01-28 05:22:45 -08002631 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2632 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2633 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2634 " output will not be complete");
2635 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002636
kwiberg55b97fe2016-01-28 05:22:45 -08002637 stats.bytesSent = bytesSent;
2638 stats.packetsSent = packetsSent;
2639 stats.bytesReceived = bytesReceived;
2640 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002641
kwiberg55b97fe2016-01-28 05:22:45 -08002642 // --- Timestamps
2643 {
2644 rtc::CritScope lock(&ts_stats_lock_);
2645 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2646 }
2647 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002648}
2649
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002650int Channel::SetCodecFECStatus(bool enable) {
2651 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2652 "Channel::SetCodecFECStatus()");
2653
kwibergc8d071e2016-04-06 12:22:38 -07002654 if (!codec_manager_.SetCodecFEC(enable) ||
2655 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002656 _engineStatisticsPtr->SetLastError(
2657 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2658 "SetCodecFECStatus() failed to set FEC state");
2659 return -1;
2660 }
2661 return 0;
2662}
2663
2664bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002665 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002666}
2667
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002668void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2669 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002670 // If pacing is enabled we always store packets.
2671 if (!pacing_enabled_)
2672 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002673 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002674 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002675 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002676 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002677 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002678}
2679
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002680// Called when we are missing one or more packets.
2681int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002682 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2683}
2684
kwiberg55b97fe2016-01-28 05:22:45 -08002685uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2686 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2687 "Channel::Demultiplex()");
2688 _audioFrame.CopyFrom(audioFrame);
2689 _audioFrame.id_ = _channelId;
2690 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002691}
2692
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002693void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002694 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002695 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002696 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002697 CodecInst codec;
2698 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002699
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002700 // Never upsample or upmix the capture signal here. This should be done at the
2701 // end of the send chain.
2702 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2703 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2704 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2705 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002706}
2707
kwiberg55b97fe2016-01-28 05:22:45 -08002708uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2709 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2710 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002711
kwiberg55b97fe2016-01-28 05:22:45 -08002712 if (_audioFrame.samples_per_channel_ == 0) {
2713 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2714 "Channel::PrepareEncodeAndSend() invalid audio frame");
2715 return 0xFFFFFFFF;
2716 }
2717
2718 if (channel_state_.Get().input_file_playing) {
2719 MixOrReplaceAudioWithFile(mixingFrequency);
2720 }
2721
solenberg1c2af8e2016-03-24 10:36:00 -07002722 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2723 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002724
2725 if (channel_state_.Get().input_external_media) {
2726 rtc::CritScope cs(&_callbackCritSect);
2727 const bool isStereo = (_audioFrame.num_channels_ == 2);
2728 if (_inputExternalMediaCallbackPtr) {
2729 _inputExternalMediaCallbackPtr->Process(
2730 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
2731 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
2732 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00002733 }
kwiberg55b97fe2016-01-28 05:22:45 -08002734 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002735
kwiberg55b97fe2016-01-28 05:22:45 -08002736 if (_includeAudioLevelIndication) {
2737 size_t length =
2738 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002739 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002740 if (is_muted && previous_frame_muted_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002741 rms_level_.ProcessMuted(length);
2742 } else {
2743 rms_level_.Process(_audioFrame.data_, length);
niklase@google.com470e71d2011-07-07 08:21:25 +00002744 }
kwiberg55b97fe2016-01-28 05:22:45 -08002745 }
solenberg1c2af8e2016-03-24 10:36:00 -07002746 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002747
kwiberg55b97fe2016-01-28 05:22:45 -08002748 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002749}
2750
kwiberg55b97fe2016-01-28 05:22:45 -08002751uint32_t Channel::EncodeAndSend() {
2752 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2753 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002754
kwiberg55b97fe2016-01-28 05:22:45 -08002755 assert(_audioFrame.num_channels_ <= 2);
2756 if (_audioFrame.samples_per_channel_ == 0) {
2757 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2758 "Channel::EncodeAndSend() invalid audio frame");
2759 return 0xFFFFFFFF;
2760 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002761
kwiberg55b97fe2016-01-28 05:22:45 -08002762 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002763
kwiberg55b97fe2016-01-28 05:22:45 -08002764 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002765
kwiberg55b97fe2016-01-28 05:22:45 -08002766 // The ACM resamples internally.
2767 _audioFrame.timestamp_ = _timeStamp;
2768 // This call will trigger AudioPacketizationCallback::SendData if encoding
2769 // is done and payload is ready for packetization and transmission.
2770 // Otherwise, it will return without invoking the callback.
2771 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2772 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2773 "Channel::EncodeAndSend() ACM encoding failed");
2774 return 0xFFFFFFFF;
2775 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002776
kwiberg55b97fe2016-01-28 05:22:45 -08002777 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2778 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002779}
2780
Minyue2013aec2015-05-13 14:14:42 +02002781void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002782 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002783 Channel* channel = associate_send_channel_.channel();
2784 if (channel && channel->ChannelId() == channel_id) {
2785 // If this channel is associated with a send channel of the specified
2786 // Channel ID, disassociate with it.
2787 ChannelOwner ref(NULL);
2788 associate_send_channel_ = ref;
2789 }
2790}
2791
ivoc14d5dbe2016-07-04 07:06:55 -07002792void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2793 event_log_proxy_->SetEventLog(event_log);
2794}
2795
kwiberg55b97fe2016-01-28 05:22:45 -08002796int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
2797 VoEMediaProcess& processObject) {
2798 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2799 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002800
kwiberg55b97fe2016-01-28 05:22:45 -08002801 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002802
kwiberg55b97fe2016-01-28 05:22:45 -08002803 if (kPlaybackPerChannel == type) {
2804 if (_outputExternalMediaCallbackPtr) {
2805 _engineStatisticsPtr->SetLastError(
2806 VE_INVALID_OPERATION, kTraceError,
2807 "Channel::RegisterExternalMediaProcessing() "
2808 "output external media already enabled");
2809 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002810 }
kwiberg55b97fe2016-01-28 05:22:45 -08002811 _outputExternalMediaCallbackPtr = &processObject;
2812 _outputExternalMedia = true;
2813 } else if (kRecordingPerChannel == type) {
2814 if (_inputExternalMediaCallbackPtr) {
2815 _engineStatisticsPtr->SetLastError(
2816 VE_INVALID_OPERATION, kTraceError,
2817 "Channel::RegisterExternalMediaProcessing() "
2818 "output external media already enabled");
2819 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002820 }
kwiberg55b97fe2016-01-28 05:22:45 -08002821 _inputExternalMediaCallbackPtr = &processObject;
2822 channel_state_.SetInputExternalMedia(true);
2823 }
2824 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002825}
2826
kwiberg55b97fe2016-01-28 05:22:45 -08002827int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
2828 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2829 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002830
kwiberg55b97fe2016-01-28 05:22:45 -08002831 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002832
kwiberg55b97fe2016-01-28 05:22:45 -08002833 if (kPlaybackPerChannel == type) {
2834 if (!_outputExternalMediaCallbackPtr) {
2835 _engineStatisticsPtr->SetLastError(
2836 VE_INVALID_OPERATION, kTraceWarning,
2837 "Channel::DeRegisterExternalMediaProcessing() "
2838 "output external media already disabled");
2839 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002840 }
kwiberg55b97fe2016-01-28 05:22:45 -08002841 _outputExternalMedia = false;
2842 _outputExternalMediaCallbackPtr = NULL;
2843 } else if (kRecordingPerChannel == type) {
2844 if (!_inputExternalMediaCallbackPtr) {
2845 _engineStatisticsPtr->SetLastError(
2846 VE_INVALID_OPERATION, kTraceWarning,
2847 "Channel::DeRegisterExternalMediaProcessing() "
2848 "input external media already disabled");
2849 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002850 }
kwiberg55b97fe2016-01-28 05:22:45 -08002851 channel_state_.SetInputExternalMedia(false);
2852 _inputExternalMediaCallbackPtr = NULL;
2853 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002854
kwiberg55b97fe2016-01-28 05:22:45 -08002855 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002856}
2857
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002858int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08002859 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2860 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002861
kwiberg55b97fe2016-01-28 05:22:45 -08002862 if (channel_state_.Get().playing) {
2863 _engineStatisticsPtr->SetLastError(
2864 VE_INVALID_OPERATION, kTraceError,
2865 "Channel::SetExternalMixing() "
2866 "external mixing cannot be changed while playing.");
2867 return -1;
2868 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002869
kwiberg55b97fe2016-01-28 05:22:45 -08002870 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002871
kwiberg55b97fe2016-01-28 05:22:45 -08002872 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002873}
2874
kwiberg55b97fe2016-01-28 05:22:45 -08002875int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2876 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002877}
2878
wu@webrtc.org24301a62013-12-13 19:17:43 +00002879void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2880 audio_coding_->GetDecodingCallStatistics(stats);
2881}
2882
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002883bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
2884 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08002885 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07002886 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002887 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002888 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002889}
2890
solenberg358057b2015-11-27 10:46:42 -08002891uint32_t Channel::GetDelayEstimate() const {
2892 int jitter_buffer_delay_ms = 0;
2893 int playout_buffer_delay_ms = 0;
2894 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
2895 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
2896}
2897
deadbeef74375882015-08-13 12:09:10 -07002898int Channel::LeastRequiredDelayMs() const {
2899 return audio_coding_->LeastRequiredDelayMs();
2900}
2901
kwiberg55b97fe2016-01-28 05:22:45 -08002902int Channel::SetMinimumPlayoutDelay(int delayMs) {
2903 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2904 "Channel::SetMinimumPlayoutDelay()");
2905 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2906 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2907 _engineStatisticsPtr->SetLastError(
2908 VE_INVALID_ARGUMENT, kTraceError,
2909 "SetMinimumPlayoutDelay() invalid min delay");
2910 return -1;
2911 }
2912 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2913 _engineStatisticsPtr->SetLastError(
2914 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2915 "SetMinimumPlayoutDelay() failed to set min playout delay");
2916 return -1;
2917 }
2918 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002919}
2920
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002921int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002922 uint32_t playout_timestamp_rtp = 0;
2923 {
tommi31fc21f2016-01-21 10:37:37 -08002924 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002925 playout_timestamp_rtp = playout_timestamp_rtp_;
2926 }
kwiberg55b97fe2016-01-28 05:22:45 -08002927 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002928 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002929 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002930 "GetPlayoutTimestamp() failed to retrieve timestamp");
2931 return -1;
2932 }
deadbeef74375882015-08-13 12:09:10 -07002933 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002934 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002935}
2936
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002937int Channel::SetInitTimestamp(unsigned int timestamp) {
2938 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002939 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002940 if (channel_state_.Get().sending) {
2941 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
2942 "SetInitTimestamp() already sending");
2943 return -1;
2944 }
2945 _rtpRtcpModule->SetStartTimestamp(timestamp);
2946 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002947}
2948
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002949int Channel::SetInitSequenceNumber(short sequenceNumber) {
2950 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2951 "Channel::SetInitSequenceNumber()");
2952 if (channel_state_.Get().sending) {
2953 _engineStatisticsPtr->SetLastError(
2954 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
2955 return -1;
2956 }
2957 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
2958 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002959}
2960
kwiberg55b97fe2016-01-28 05:22:45 -08002961int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2962 RtpReceiver** rtp_receiver) const {
2963 *rtpRtcpModule = _rtpRtcpModule.get();
2964 *rtp_receiver = rtp_receiver_.get();
2965 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002966}
2967
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002968// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2969// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002970int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002971 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002972 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002973
kwiberg55b97fe2016-01-28 05:22:45 -08002974 {
2975 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002976
kwiberg5a25d952016-08-17 07:31:12 -07002977 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002978 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2979 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2980 " doesnt exist");
2981 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002982 }
2983
kwiberg4ec01d92016-08-22 08:43:54 -07002984 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002985 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002986 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2987 "Channel::MixOrReplaceAudioWithFile() file mixing "
2988 "failed");
2989 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002990 }
kwiberg55b97fe2016-01-28 05:22:45 -08002991 if (fileSamples == 0) {
2992 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2993 "Channel::MixOrReplaceAudioWithFile() file is ended");
2994 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002995 }
kwiberg55b97fe2016-01-28 05:22:45 -08002996 }
2997
2998 assert(_audioFrame.samples_per_channel_ == fileSamples);
2999
3000 if (_mixFileWithMicrophone) {
3001 // Currently file stream is always mono.
3002 // TODO(xians): Change the code when FilePlayer supports real stereo.
3003 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3004 1, fileSamples);
3005 } else {
3006 // Replace ACM audio with file.
3007 // Currently file stream is always mono.
3008 // TODO(xians): Change the code when FilePlayer supports real stereo.
3009 _audioFrame.UpdateFrame(
3010 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3011 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3012 }
3013 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003014}
3015
kwiberg55b97fe2016-01-28 05:22:45 -08003016int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3017 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003018
kwibergb7f89d62016-02-17 10:04:18 -08003019 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003020 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003021
kwiberg55b97fe2016-01-28 05:22:45 -08003022 {
3023 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003024
kwiberg5a25d952016-08-17 07:31:12 -07003025 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003026 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3027 "Channel::MixAudioWithFile() file mixing failed");
3028 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003029 }
3030
kwiberg55b97fe2016-01-28 05:22:45 -08003031 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003032 if (output_file_player_->Get10msAudioFromFile(
3033 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003034 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3035 "Channel::MixAudioWithFile() file mixing failed");
3036 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003037 }
kwiberg55b97fe2016-01-28 05:22:45 -08003038 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003039
kwiberg55b97fe2016-01-28 05:22:45 -08003040 if (audioFrame.samples_per_channel_ == fileSamples) {
3041 // Currently file stream is always mono.
3042 // TODO(xians): Change the code when FilePlayer supports real stereo.
3043 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3044 fileSamples);
3045 } else {
3046 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3047 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3048 ") != "
3049 "fileSamples(%" PRIuS ")",
3050 audioFrame.samples_per_channel_, fileSamples);
3051 return -1;
3052 }
3053
3054 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003055}
3056
deadbeef74375882015-08-13 12:09:10 -07003057void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003058 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003059
henrik.lundin96bd5022016-04-06 04:13:56 -07003060 if (!jitter_buffer_playout_timestamp_) {
3061 // This can happen if this channel has not received any RTP packets. In
3062 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003063 return;
3064 }
3065
3066 uint16_t delay_ms = 0;
3067 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003068 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003069 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3070 " delay from the ADM");
3071 _engineStatisticsPtr->SetLastError(
3072 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3073 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3074 return;
3075 }
3076
henrik.lundin96bd5022016-04-06 04:13:56 -07003077 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3078 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003079
3080 // Remove the playout delay.
henrik.lundin96bd5022016-04-06 04:13:56 -07003081 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003082
kwiberg55b97fe2016-01-28 05:22:45 -08003083 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003084 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003085 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003086
3087 {
tommi31fc21f2016-01-21 10:37:37 -08003088 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003089 if (rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003090 playout_timestamp_rtcp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003091 } else {
henrik.lundin96bd5022016-04-06 04:13:56 -07003092 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003093 }
3094 playout_delay_ms_ = delay_ms;
3095 }
3096}
3097
kwiberg55b97fe2016-01-28 05:22:45 -08003098void Channel::RegisterReceiveCodecsToRTPModule() {
3099 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3100 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003101
kwiberg55b97fe2016-01-28 05:22:45 -08003102 CodecInst codec;
3103 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003104
kwiberg55b97fe2016-01-28 05:22:45 -08003105 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3106 // Open up the RTP/RTCP receiver for all supported codecs
3107 if ((audio_coding_->Codec(idx, &codec) == -1) ||
3108 (rtp_receiver_->RegisterReceivePayload(
3109 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3110 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
3111 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3112 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3113 " to register %s (%d/%d/%" PRIuS
3114 "/%d) to RTP/RTCP "
3115 "receiver",
3116 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3117 codec.rate);
3118 } else {
3119 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3120 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3121 "(%d/%d/%" PRIuS
3122 "/%d) has been added to the RTP/RTCP "
3123 "receiver",
3124 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3125 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003126 }
kwiberg55b97fe2016-01-28 05:22:45 -08003127 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003128}
3129
kwiberg55b97fe2016-01-28 05:22:45 -08003130int Channel::SetSendRtpHeaderExtension(bool enable,
3131 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003132 unsigned char id) {
3133 int error = 0;
3134 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3135 if (enable) {
3136 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3137 }
3138 return error;
3139}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003140
henrik.lundinb3e30012016-08-31 14:09:51 -07003141int32_t Channel::GetPlayoutFrequency() const {
wu@webrtc.org94454b72014-06-05 20:34:08 +00003142 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
3143 CodecInst current_recive_codec;
3144 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
3145 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
3146 // Even though the actual sampling rate for G.722 audio is
3147 // 16,000 Hz, the RTP clock rate for the G722 payload format is
3148 // 8,000 Hz because that value was erroneously assigned in
3149 // RFC 1890 and must remain unchanged for backward compatibility.
3150 playout_frequency = 8000;
3151 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
3152 // We are resampling Opus internally to 32,000 Hz until all our
3153 // DSP routines can operate at 48,000 Hz, but the RTP clock
3154 // rate for the Opus payload format is standardized to 48,000 Hz,
3155 // because that is the maximum supported decoding sampling rate.
3156 playout_frequency = 48000;
3157 }
3158 }
3159 return playout_frequency;
3160}
3161
Minyue2013aec2015-05-13 14:14:42 +02003162int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003163 RtcpMode method = _rtpRtcpModule->RTCP();
3164 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003165 return 0;
3166 }
3167 std::vector<RTCPReportBlock> report_blocks;
3168 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003169
3170 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003171 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003172 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003173 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003174 Channel* channel = associate_send_channel_.channel();
3175 // Tries to get RTT from an associated channel. This is important for
3176 // receive-only channels.
3177 if (channel) {
3178 // To prevent infinite recursion and deadlock, calling GetRTT of
3179 // associate channel should always use "false" for argument:
3180 // |allow_associate_channel|.
3181 rtt = channel->GetRTT(false);
3182 }
3183 }
3184 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003185 }
3186
3187 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3188 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3189 for (; it != report_blocks.end(); ++it) {
3190 if (it->remoteSSRC == remoteSSRC)
3191 break;
3192 }
3193 if (it == report_blocks.end()) {
3194 // We have not received packets with SSRC matching the report blocks.
3195 // To calculate RTT we try with the SSRC of the first report block.
3196 // This is very important for send-only channels where we don't know
3197 // the SSRC of the other end.
3198 remoteSSRC = report_blocks[0].remoteSSRC;
3199 }
Minyue2013aec2015-05-13 14:14:42 +02003200
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003201 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003202 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003203 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003204 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3205 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003206 return 0;
3207 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003208 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003209}
3210
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003211} // namespace voe
3212} // namespace webrtc