blob: ce9770e8a461b68fa67c61ae9e17979b8199035c [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"
Henrik Lundin64dad832015-05-11 12:44:23 +020023#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070024#include "webrtc/logging/rtc_event_log/rtc_event_log.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())),
danilchap799a9d02016-09-22 03:36:27 -0700812 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100813 _outputAudioLevel(),
814 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100815 // Avoid conflict with other channels by adding 1024 - 1026,
816 // won't use as much as 1024 channels.
817 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
818 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
819 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
820 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100821 _outputExternalMedia(false),
822 _inputExternalMediaCallbackPtr(NULL),
823 _outputExternalMediaCallbackPtr(NULL),
824 _timeStamp(0), // This is just an offset, RTP module will add it's own
825 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100826 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100827 playout_timestamp_rtp_(0),
828 playout_timestamp_rtcp_(0),
829 playout_delay_ms_(0),
830 _numberOfDiscardedPackets(0),
831 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100832 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
833 capture_start_rtp_time_stamp_(-1),
834 capture_start_ntp_time_ms_(-1),
835 _engineStatisticsPtr(NULL),
836 _outputMixerPtr(NULL),
837 _transmitMixerPtr(NULL),
838 _moduleProcessThreadPtr(NULL),
839 _audioDeviceModulePtr(NULL),
840 _voiceEngineObserverPtr(NULL),
841 _callbackCritSectPtr(NULL),
842 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100843 _sendFrameType(0),
844 _externalMixing(false),
845 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700846 input_mute_(false),
847 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100848 _panLeft(1.0f),
849 _panRight(1.0f),
850 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100851 _lastLocalTimeStamp(0),
852 _lastPayloadType(0),
853 _includeAudioLevelIndication(false),
854 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100855 restored_packet_in_use_(false),
856 rtcp_observer_(new VoERtcpObserver(this)),
857 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock())),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100858 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700859 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800860 feedback_observer_proxy_(new TransportFeedbackProxy()),
861 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700862 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200863 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
864 kMaxRetransmissionWindowMs)),
solenberg88499ec2016-09-07 07:34:41 -0700865 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800866 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
867 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700868 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800869 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700870 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800871 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200872
kwiberg55b97fe2016-01-28 05:22:45 -0800873 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000874
kwiberg55b97fe2016-01-28 05:22:45 -0800875 RtpRtcp::Configuration configuration;
876 configuration.audio = true;
877 configuration.outgoing_transport = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800878 configuration.receive_statistics = rtp_receive_statistics_.get();
879 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800880 if (pacing_enabled_) {
881 configuration.paced_sender = rtp_packet_sender_proxy_.get();
882 configuration.transport_sequence_number_allocator =
883 seq_num_allocator_proxy_.get();
884 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
885 }
ivoc14d5dbe2016-07-04 07:06:55 -0700886 configuration.event_log = &(*event_log_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200887 configuration.retransmission_rate_limiter =
888 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000889
kwiberg55b97fe2016-01-28 05:22:45 -0800890 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100891 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000892
kwiberg55b97fe2016-01-28 05:22:45 -0800893 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
894 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
895 statistics_proxy_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000896}
897
kwiberg55b97fe2016-01-28 05:22:45 -0800898Channel::~Channel() {
899 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
900 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
901 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000902
kwiberg55b97fe2016-01-28 05:22:45 -0800903 if (_outputExternalMedia) {
904 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
905 }
906 if (channel_state_.Get().input_external_media) {
907 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
908 }
909 StopSend();
910 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000911
kwiberg55b97fe2016-01-28 05:22:45 -0800912 {
913 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700914 if (input_file_player_) {
915 input_file_player_->RegisterModuleFileCallback(NULL);
916 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000917 }
kwiberg5a25d952016-08-17 07:31:12 -0700918 if (output_file_player_) {
919 output_file_player_->RegisterModuleFileCallback(NULL);
920 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800921 }
kwiberg5a25d952016-08-17 07:31:12 -0700922 if (output_file_recorder_) {
923 output_file_recorder_->RegisterModuleFileCallback(NULL);
924 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800925 }
926 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000927
kwiberg55b97fe2016-01-28 05:22:45 -0800928 // The order to safely shutdown modules in a channel is:
929 // 1. De-register callbacks in modules
930 // 2. De-register modules in process thread
931 // 3. Destroy modules
932 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
933 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
934 "~Channel() failed to de-register transport callback"
935 " (Audio coding module)");
936 }
937 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
938 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
939 "~Channel() failed to de-register VAD callback"
940 " (Audio coding module)");
941 }
942 // De-register modules in process thread
943 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000944
kwiberg55b97fe2016-01-28 05:22:45 -0800945 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000946}
947
kwiberg55b97fe2016-01-28 05:22:45 -0800948int32_t Channel::Init() {
949 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
950 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000951
kwiberg55b97fe2016-01-28 05:22:45 -0800952 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000953
kwiberg55b97fe2016-01-28 05:22:45 -0800954 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000955
kwiberg55b97fe2016-01-28 05:22:45 -0800956 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
957 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
958 "Channel::Init() must call SetEngineInformation() first");
959 return -1;
960 }
961
962 // --- Add modules to process thread (for periodic schedulation)
963
964 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
965
966 // --- ACM initialization
967
968 if (audio_coding_->InitializeReceiver() == -1) {
969 _engineStatisticsPtr->SetLastError(
970 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
971 "Channel::Init() unable to initialize the ACM - 1");
972 return -1;
973 }
974
975 // --- RTP/RTCP module initialization
976
977 // Ensure that RTCP is enabled by default for the created channel.
978 // Note that, the module will keep generating RTCP until it is explicitly
979 // disabled by the user.
980 // After StopListen (when no sockets exists), RTCP packets will no longer
981 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -0700982 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800983 // RTCP is enabled by default.
984 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
985 // --- Register all permanent callbacks
986 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
987 (audio_coding_->RegisterVADCallback(this) == -1);
988
989 if (fail) {
990 _engineStatisticsPtr->SetLastError(
991 VE_CANNOT_INIT_CHANNEL, kTraceError,
992 "Channel::Init() callbacks not registered");
993 return -1;
994 }
995
996 // --- Register all supported codecs to the receiving side of the
997 // RTP/RTCP module
998
999 CodecInst codec;
1000 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1001
1002 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1003 // Open up the RTP/RTCP receiver for all supported codecs
1004 if ((audio_coding_->Codec(idx, &codec) == -1) ||
1005 (rtp_receiver_->RegisterReceivePayload(
1006 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1007 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
1008 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1009 "Channel::Init() unable to register %s "
1010 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1011 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1012 codec.rate);
1013 } else {
1014 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1015 "Channel::Init() %s (%d/%d/%" PRIuS
1016 "/%d) has been "
1017 "added to the RTP/RTCP receiver",
1018 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1019 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001020 }
1021
kwiberg55b97fe2016-01-28 05:22:45 -08001022 // Ensure that PCMU is used as default codec on the sending side
1023 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1024 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001025 }
1026
kwiberg55b97fe2016-01-28 05:22:45 -08001027 // Register default PT for outband 'telephone-event'
1028 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001029 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
1030 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001031 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1032 "Channel::Init() failed to register outband "
1033 "'telephone-event' (%d/%d) correctly",
1034 codec.pltype, codec.plfreq);
1035 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001036 }
1037
kwiberg55b97fe2016-01-28 05:22:45 -08001038 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001039 if (!codec_manager_.RegisterEncoder(codec) ||
1040 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
1041 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec) ||
1042 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001043 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1044 "Channel::Init() failed to register CN (%d/%d) "
1045 "correctly - 1",
1046 codec.pltype, codec.plfreq);
1047 }
1048 }
kwiberg55b97fe2016-01-28 05:22:45 -08001049 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001050
kwiberg55b97fe2016-01-28 05:22:45 -08001051 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001052}
1053
kwiberg55b97fe2016-01-28 05:22:45 -08001054int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1055 OutputMixer& outputMixer,
1056 voe::TransmitMixer& transmitMixer,
1057 ProcessThread& moduleProcessThread,
1058 AudioDeviceModule& audioDeviceModule,
1059 VoiceEngineObserver* voiceEngineObserver,
1060 rtc::CriticalSection* callbackCritSect) {
1061 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1062 "Channel::SetEngineInformation()");
1063 _engineStatisticsPtr = &engineStatistics;
1064 _outputMixerPtr = &outputMixer;
1065 _transmitMixerPtr = &transmitMixer,
1066 _moduleProcessThreadPtr = &moduleProcessThread;
1067 _audioDeviceModulePtr = &audioDeviceModule;
1068 _voiceEngineObserverPtr = voiceEngineObserver;
1069 _callbackCritSectPtr = callbackCritSect;
1070 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001071}
1072
kwiberg55b97fe2016-01-28 05:22:45 -08001073int32_t Channel::UpdateLocalTimeStamp() {
1074 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1075 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001076}
1077
kwibergb7f89d62016-02-17 10:04:18 -08001078void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001079 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001080 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001081}
1082
ossu29b1a8d2016-06-13 07:34:51 -07001083const rtc::scoped_refptr<AudioDecoderFactory>&
1084Channel::GetAudioDecoderFactory() const {
1085 return decoder_factory_;
1086}
1087
kwiberg55b97fe2016-01-28 05:22:45 -08001088int32_t Channel::StartPlayout() {
1089 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1090 "Channel::StartPlayout()");
1091 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001092 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001093 }
1094
1095 if (!_externalMixing) {
1096 // Add participant as candidates for mixing.
1097 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1098 _engineStatisticsPtr->SetLastError(
1099 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1100 "StartPlayout() failed to add participant to mixer");
1101 return -1;
1102 }
1103 }
1104
1105 channel_state_.SetPlaying(true);
1106 if (RegisterFilePlayingToMixer() != 0)
1107 return -1;
1108
1109 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001110}
1111
kwiberg55b97fe2016-01-28 05:22:45 -08001112int32_t Channel::StopPlayout() {
1113 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1114 "Channel::StopPlayout()");
1115 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001116 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001117 }
1118
1119 if (!_externalMixing) {
1120 // Remove participant as candidates for mixing
1121 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1122 _engineStatisticsPtr->SetLastError(
1123 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1124 "StopPlayout() failed to remove participant from mixer");
1125 return -1;
1126 }
1127 }
1128
1129 channel_state_.SetPlaying(false);
1130 _outputAudioLevel.Clear();
1131
1132 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001133}
1134
kwiberg55b97fe2016-01-28 05:22:45 -08001135int32_t Channel::StartSend() {
1136 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1137 "Channel::StartSend()");
1138 // Resume the previous sequence number which was reset by StopSend().
1139 // This needs to be done before |sending| is set to true.
1140 if (send_sequence_number_)
1141 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001142
kwiberg55b97fe2016-01-28 05:22:45 -08001143 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001144 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001145 }
1146 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001147
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001148 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001149 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1150 _engineStatisticsPtr->SetLastError(
1151 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1152 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001153 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001154 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001155 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001156 return -1;
1157 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001158
kwiberg55b97fe2016-01-28 05:22:45 -08001159 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001160}
1161
kwiberg55b97fe2016-01-28 05:22:45 -08001162int32_t Channel::StopSend() {
1163 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1164 "Channel::StopSend()");
1165 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001166 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001167 }
1168 channel_state_.SetSending(false);
1169
1170 // Store the sequence number to be able to pick up the same sequence for
1171 // the next StartSend(). This is needed for restarting device, otherwise
1172 // it might cause libSRTP to complain about packets being replayed.
1173 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1174 // CL is landed. See issue
1175 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1176 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1177
1178 // Reset sending SSRC and sequence number and triggers direct transmission
1179 // of RTCP BYE
1180 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1181 _engineStatisticsPtr->SetLastError(
1182 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1183 "StartSend() RTP/RTCP failed to stop sending");
1184 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001185 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001186
1187 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001188}
1189
kwiberg55b97fe2016-01-28 05:22:45 -08001190int32_t Channel::StartReceiving() {
1191 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1192 "Channel::StartReceiving()");
1193 if (channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001194 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001195 }
1196 channel_state_.SetReceiving(true);
1197 _numberOfDiscardedPackets = 0;
1198 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001199}
1200
kwiberg55b97fe2016-01-28 05:22:45 -08001201int32_t Channel::StopReceiving() {
1202 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1203 "Channel::StopReceiving()");
1204 if (!channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001205 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001206 }
1207
1208 channel_state_.SetReceiving(false);
1209 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001210}
1211
kwiberg55b97fe2016-01-28 05:22:45 -08001212int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1213 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1214 "Channel::RegisterVoiceEngineObserver()");
1215 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001216
kwiberg55b97fe2016-01-28 05:22:45 -08001217 if (_voiceEngineObserverPtr) {
1218 _engineStatisticsPtr->SetLastError(
1219 VE_INVALID_OPERATION, kTraceError,
1220 "RegisterVoiceEngineObserver() observer already enabled");
1221 return -1;
1222 }
1223 _voiceEngineObserverPtr = &observer;
1224 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001225}
1226
kwiberg55b97fe2016-01-28 05:22:45 -08001227int32_t Channel::DeRegisterVoiceEngineObserver() {
1228 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1229 "Channel::DeRegisterVoiceEngineObserver()");
1230 rtc::CritScope cs(&_callbackCritSect);
1231
1232 if (!_voiceEngineObserverPtr) {
1233 _engineStatisticsPtr->SetLastError(
1234 VE_INVALID_OPERATION, kTraceWarning,
1235 "DeRegisterVoiceEngineObserver() observer already disabled");
1236 return 0;
1237 }
1238 _voiceEngineObserverPtr = NULL;
1239 return 0;
1240}
1241
1242int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001243 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001244 if (send_codec) {
1245 codec = *send_codec;
1246 return 0;
1247 }
1248 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001249}
1250
kwiberg55b97fe2016-01-28 05:22:45 -08001251int32_t Channel::GetRecCodec(CodecInst& codec) {
1252 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001253}
1254
kwiberg55b97fe2016-01-28 05:22:45 -08001255int32_t Channel::SetSendCodec(const CodecInst& codec) {
1256 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1257 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001258
kwibergc8d071e2016-04-06 12:22:38 -07001259 if (!codec_manager_.RegisterEncoder(codec) ||
1260 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001261 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1262 "SetSendCodec() failed to register codec to ACM");
1263 return -1;
1264 }
1265
1266 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1267 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1268 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1269 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1270 "SetSendCodec() failed to register codec to"
1271 " RTP/RTCP module");
1272 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001273 }
kwiberg55b97fe2016-01-28 05:22:45 -08001274 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001275
kwiberg55b97fe2016-01-28 05:22:45 -08001276 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) {
1277 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1278 "SetSendCodec() failed to set audio packet size");
1279 return -1;
1280 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001281
kwiberg55b97fe2016-01-28 05:22:45 -08001282 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001283}
1284
Ivo Creusenadf89b72015-04-29 16:03:33 +02001285void Channel::SetBitRate(int bitrate_bps) {
1286 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1287 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
1288 audio_coding_->SetBitRate(bitrate_bps);
Erik Språng737336d2016-07-29 12:59:36 +02001289 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001290}
1291
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001292void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001293 network_predictor_->UpdatePacketLossRate(fraction_lost);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001294 uint8_t average_fraction_loss = network_predictor_->GetLossRate();
1295
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001296 // Normalizes rate to 0 - 100.
kwiberg55b97fe2016-01-28 05:22:45 -08001297 if (audio_coding_->SetPacketLossRate(100 * average_fraction_loss / 255) !=
1298 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001299 assert(false); // This should not happen.
1300 }
1301}
1302
kwiberg55b97fe2016-01-28 05:22:45 -08001303int32_t Channel::SetVADStatus(bool enableVAD,
1304 ACMVADMode mode,
1305 bool disableDTX) {
1306 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1307 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001308 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1309 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1310 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001311 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1312 kTraceError,
1313 "SetVADStatus() failed to set VAD");
1314 return -1;
1315 }
1316 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001317}
1318
kwiberg55b97fe2016-01-28 05:22:45 -08001319int32_t Channel::GetVADStatus(bool& enabledVAD,
1320 ACMVADMode& mode,
1321 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001322 const auto* params = codec_manager_.GetStackParams();
1323 enabledVAD = params->use_cng;
1324 mode = params->vad_mode;
1325 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001326 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001327}
1328
kwiberg55b97fe2016-01-28 05:22:45 -08001329int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1330 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1331 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001332
kwiberg55b97fe2016-01-28 05:22:45 -08001333 if (channel_state_.Get().playing) {
1334 _engineStatisticsPtr->SetLastError(
1335 VE_ALREADY_PLAYING, kTraceError,
1336 "SetRecPayloadType() unable to set PT while playing");
1337 return -1;
1338 }
1339 if (channel_state_.Get().receiving) {
1340 _engineStatisticsPtr->SetLastError(
1341 VE_ALREADY_LISTENING, kTraceError,
1342 "SetRecPayloadType() unable to set PT while listening");
1343 return -1;
1344 }
1345
1346 if (codec.pltype == -1) {
1347 // De-register the selected codec (RTP/RTCP module and ACM)
1348
1349 int8_t pltype(-1);
1350 CodecInst rxCodec = codec;
1351
1352 // Get payload type for the given codec
1353 rtp_payload_registry_->ReceivePayloadType(
1354 rxCodec.plname, rxCodec.plfreq, rxCodec.channels,
1355 (rxCodec.rate < 0) ? 0 : rxCodec.rate, &pltype);
1356 rxCodec.pltype = pltype;
1357
1358 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1359 _engineStatisticsPtr->SetLastError(
1360 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1361 "SetRecPayloadType() RTP/RTCP-module deregistration "
1362 "failed");
1363 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001364 }
kwiberg55b97fe2016-01-28 05:22:45 -08001365 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1366 _engineStatisticsPtr->SetLastError(
1367 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1368 "SetRecPayloadType() ACM deregistration failed - 1");
1369 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001370 }
kwiberg55b97fe2016-01-28 05:22:45 -08001371 return 0;
1372 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001373
kwiberg55b97fe2016-01-28 05:22:45 -08001374 if (rtp_receiver_->RegisterReceivePayload(
1375 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1376 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1377 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001378 // TODO(kwiberg): Retrying is probably not necessary, since
1379 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001380 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001381 if (rtp_receiver_->RegisterReceivePayload(
kwiberg55b97fe2016-01-28 05:22:45 -08001382 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1383 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1384 _engineStatisticsPtr->SetLastError(
1385 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1386 "SetRecPayloadType() RTP/RTCP-module registration failed");
1387 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001388 }
kwiberg55b97fe2016-01-28 05:22:45 -08001389 }
kwibergc8d071e2016-04-06 12:22:38 -07001390 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001391 audio_coding_->UnregisterReceiveCodec(codec.pltype);
kwibergc8d071e2016-04-06 12:22:38 -07001392 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001393 _engineStatisticsPtr->SetLastError(
1394 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1395 "SetRecPayloadType() ACM registration failed - 1");
1396 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001397 }
kwiberg55b97fe2016-01-28 05:22:45 -08001398 }
1399 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001400}
1401
kwiberg55b97fe2016-01-28 05:22:45 -08001402int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1403 int8_t payloadType(-1);
1404 if (rtp_payload_registry_->ReceivePayloadType(
1405 codec.plname, codec.plfreq, codec.channels,
1406 (codec.rate < 0) ? 0 : codec.rate, &payloadType) != 0) {
1407 _engineStatisticsPtr->SetLastError(
1408 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1409 "GetRecPayloadType() failed to retrieve RX payload type");
1410 return -1;
1411 }
1412 codec.pltype = payloadType;
1413 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001414}
1415
kwiberg55b97fe2016-01-28 05:22:45 -08001416int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1417 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1418 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001419
kwiberg55b97fe2016-01-28 05:22:45 -08001420 CodecInst codec;
1421 int32_t samplingFreqHz(-1);
1422 const size_t kMono = 1;
1423 if (frequency == kFreq32000Hz)
1424 samplingFreqHz = 32000;
1425 else if (frequency == kFreq16000Hz)
1426 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001427
kwiberg55b97fe2016-01-28 05:22:45 -08001428 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1429 _engineStatisticsPtr->SetLastError(
1430 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1431 "SetSendCNPayloadType() failed to retrieve default CN codec "
1432 "settings");
1433 return -1;
1434 }
1435
1436 // Modify the payload type (must be set to dynamic range)
1437 codec.pltype = type;
1438
kwibergc8d071e2016-04-06 12:22:38 -07001439 if (!codec_manager_.RegisterEncoder(codec) ||
1440 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001441 _engineStatisticsPtr->SetLastError(
1442 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1443 "SetSendCNPayloadType() failed to register CN to ACM");
1444 return -1;
1445 }
1446
1447 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1448 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1449 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1450 _engineStatisticsPtr->SetLastError(
1451 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1452 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1453 "module");
1454 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001455 }
kwiberg55b97fe2016-01-28 05:22:45 -08001456 }
1457 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001458}
1459
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001460int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001461 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001462 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001463
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001464 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001465 _engineStatisticsPtr->SetLastError(
1466 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001467 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001468 return -1;
1469 }
1470 return 0;
1471}
1472
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001473int Channel::SetOpusDtx(bool enable_dtx) {
1474 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1475 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001476 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001477 : audio_coding_->DisableOpusDtx();
1478 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001479 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1480 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001481 return -1;
1482 }
1483 return 0;
1484}
1485
ivoc85228d62016-07-27 04:53:47 -07001486int Channel::GetOpusDtx(bool* enabled) {
1487 int success = -1;
1488 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1489 if (encoder) {
1490 *enabled = encoder->GetDtx();
1491 success = 0;
1492 }
1493 });
1494 return success;
1495}
1496
mflodman3d7db262016-04-29 00:57:13 -07001497int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001498 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001499 "Channel::RegisterExternalTransport()");
1500
kwiberg55b97fe2016-01-28 05:22:45 -08001501 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001502 if (_externalTransport) {
1503 _engineStatisticsPtr->SetLastError(
1504 VE_INVALID_OPERATION, kTraceError,
1505 "RegisterExternalTransport() external transport already enabled");
1506 return -1;
1507 }
1508 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001509 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001510 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001511}
1512
kwiberg55b97fe2016-01-28 05:22:45 -08001513int32_t Channel::DeRegisterExternalTransport() {
1514 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1515 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001516
kwiberg55b97fe2016-01-28 05:22:45 -08001517 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001518 if (_transportPtr) {
1519 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1520 "DeRegisterExternalTransport() all transport is disabled");
1521 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001522 _engineStatisticsPtr->SetLastError(
1523 VE_INVALID_OPERATION, kTraceWarning,
1524 "DeRegisterExternalTransport() external transport already "
1525 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001526 }
1527 _externalTransport = false;
1528 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001529 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001530}
1531
mflodman3d7db262016-04-29 00:57:13 -07001532int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001533 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001534 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001535 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001536 "Channel::ReceivedRTPPacket()");
1537
1538 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001539 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001540
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001541 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001542 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1543 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1544 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001545 return -1;
1546 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001547 header.payload_type_frequency =
1548 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001549 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001550 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001551 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001552 rtp_receive_statistics_->IncomingPacket(
1553 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001554 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001555
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001556 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001557}
1558
1559bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001560 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001561 const RTPHeader& header,
1562 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001563 if (rtp_payload_registry_->IsRtx(header)) {
1564 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001565 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001566 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001567 assert(packet_length >= header.headerLength);
1568 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001569 PayloadUnion payload_specific;
1570 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001571 &payload_specific)) {
1572 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001573 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001574 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1575 payload_specific, in_order);
1576}
1577
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001578bool Channel::HandleRtxPacket(const uint8_t* packet,
1579 size_t packet_length,
1580 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001581 if (!rtp_payload_registry_->IsRtx(header))
1582 return false;
1583
1584 // Remove the RTX header and parse the original RTP header.
1585 if (packet_length < header.headerLength)
1586 return false;
1587 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1588 return false;
1589 if (restored_packet_in_use_) {
1590 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1591 "Multiple RTX headers detected, dropping packet");
1592 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001593 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001594 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001595 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1596 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001597 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1598 "Incoming RTX packet: invalid RTP header");
1599 return false;
1600 }
1601 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001602 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001603 restored_packet_in_use_ = false;
1604 return ret;
1605}
1606
1607bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1608 StreamStatistician* statistician =
1609 rtp_receive_statistics_->GetStatistician(header.ssrc);
1610 if (!statistician)
1611 return false;
1612 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001613}
1614
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001615bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1616 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001617 // Retransmissions are handled separately if RTX is enabled.
1618 if (rtp_payload_registry_->RtxEnabled())
1619 return false;
1620 StreamStatistician* statistician =
1621 rtp_receive_statistics_->GetStatistician(header.ssrc);
1622 if (!statistician)
1623 return false;
1624 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001625 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001626 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001627 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001628}
1629
mflodman3d7db262016-04-29 00:57:13 -07001630int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001631 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001632 "Channel::ReceivedRTCPPacket()");
1633 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001634 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001635
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001636 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001637 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001638 _engineStatisticsPtr->SetLastError(
1639 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1640 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1641 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001642
Minyue2013aec2015-05-13 14:14:42 +02001643 int64_t rtt = GetRTT(true);
1644 if (rtt == 0) {
1645 // Waiting for valid RTT.
1646 return 0;
1647 }
Erik Språng737336d2016-07-29 12:59:36 +02001648
1649 int64_t nack_window_ms = rtt;
1650 if (nack_window_ms < kMinRetransmissionWindowMs) {
1651 nack_window_ms = kMinRetransmissionWindowMs;
1652 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1653 nack_window_ms = kMaxRetransmissionWindowMs;
1654 }
1655 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1656
Minyue2013aec2015-05-13 14:14:42 +02001657 uint32_t ntp_secs = 0;
1658 uint32_t ntp_frac = 0;
1659 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001660 if (0 !=
1661 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1662 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001663 // Waiting for RTCP.
1664 return 0;
1665 }
1666
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001667 {
tommi31fc21f2016-01-21 10:37:37 -08001668 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001669 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001670 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001671 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001672}
1673
niklase@google.com470e71d2011-07-07 08:21:25 +00001674int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001675 bool loop,
1676 FileFormats format,
1677 int startPosition,
1678 float volumeScaling,
1679 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001680 const CodecInst* codecInst) {
1681 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1682 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1683 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1684 "stopPosition=%d)",
1685 fileName, loop, format, volumeScaling, startPosition,
1686 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001687
kwiberg55b97fe2016-01-28 05:22:45 -08001688 if (channel_state_.Get().output_file_playing) {
1689 _engineStatisticsPtr->SetLastError(
1690 VE_ALREADY_PLAYING, kTraceError,
1691 "StartPlayingFileLocally() is already playing");
1692 return -1;
1693 }
1694
1695 {
1696 rtc::CritScope cs(&_fileCritSect);
1697
kwiberg5a25d952016-08-17 07:31:12 -07001698 if (output_file_player_) {
1699 output_file_player_->RegisterModuleFileCallback(NULL);
1700 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001701 }
1702
kwiberg5b356f42016-09-08 04:32:33 -07001703 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001704 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001705
kwiberg5a25d952016-08-17 07:31:12 -07001706 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001707 _engineStatisticsPtr->SetLastError(
1708 VE_INVALID_ARGUMENT, kTraceError,
1709 "StartPlayingFileLocally() filePlayer format is not correct");
1710 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001711 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001712
kwiberg55b97fe2016-01-28 05:22:45 -08001713 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001714
kwiberg5a25d952016-08-17 07:31:12 -07001715 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001716 fileName, loop, startPosition, volumeScaling, notificationTime,
1717 stopPosition, (const CodecInst*)codecInst) != 0) {
1718 _engineStatisticsPtr->SetLastError(
1719 VE_BAD_FILE, kTraceError,
1720 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001721 output_file_player_->StopPlayingFile();
1722 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001723 return -1;
1724 }
kwiberg5a25d952016-08-17 07:31:12 -07001725 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001726 channel_state_.SetOutputFilePlaying(true);
1727 }
1728
1729 if (RegisterFilePlayingToMixer() != 0)
1730 return -1;
1731
1732 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001733}
1734
1735int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001736 FileFormats format,
1737 int startPosition,
1738 float volumeScaling,
1739 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001740 const CodecInst* codecInst) {
1741 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1742 "Channel::StartPlayingFileLocally(format=%d,"
1743 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1744 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001745
kwiberg55b97fe2016-01-28 05:22:45 -08001746 if (stream == NULL) {
1747 _engineStatisticsPtr->SetLastError(
1748 VE_BAD_FILE, kTraceError,
1749 "StartPlayingFileLocally() NULL as input stream");
1750 return -1;
1751 }
1752
1753 if (channel_state_.Get().output_file_playing) {
1754 _engineStatisticsPtr->SetLastError(
1755 VE_ALREADY_PLAYING, kTraceError,
1756 "StartPlayingFileLocally() is already playing");
1757 return -1;
1758 }
1759
1760 {
1761 rtc::CritScope cs(&_fileCritSect);
1762
1763 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001764 if (output_file_player_) {
1765 output_file_player_->RegisterModuleFileCallback(NULL);
1766 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001767 }
1768
kwiberg55b97fe2016-01-28 05:22:45 -08001769 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001770 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001771 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001772
kwiberg5a25d952016-08-17 07:31:12 -07001773 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001774 _engineStatisticsPtr->SetLastError(
1775 VE_INVALID_ARGUMENT, kTraceError,
1776 "StartPlayingFileLocally() filePlayer format isnot correct");
1777 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001778 }
1779
kwiberg55b97fe2016-01-28 05:22:45 -08001780 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001781
kwiberg4ec01d92016-08-22 08:43:54 -07001782 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001783 volumeScaling, notificationTime,
1784 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001785 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1786 "StartPlayingFile() failed to "
1787 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001788 output_file_player_->StopPlayingFile();
1789 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001790 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001791 }
kwiberg5a25d952016-08-17 07:31:12 -07001792 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001793 channel_state_.SetOutputFilePlaying(true);
1794 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001795
kwiberg55b97fe2016-01-28 05:22:45 -08001796 if (RegisterFilePlayingToMixer() != 0)
1797 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001798
kwiberg55b97fe2016-01-28 05:22:45 -08001799 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001800}
1801
kwiberg55b97fe2016-01-28 05:22:45 -08001802int Channel::StopPlayingFileLocally() {
1803 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1804 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001805
kwiberg55b97fe2016-01-28 05:22:45 -08001806 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001807 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001808 }
1809
1810 {
1811 rtc::CritScope cs(&_fileCritSect);
1812
kwiberg5a25d952016-08-17 07:31:12 -07001813 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001814 _engineStatisticsPtr->SetLastError(
1815 VE_STOP_RECORDING_FAILED, kTraceError,
1816 "StopPlayingFile() could not stop playing");
1817 return -1;
1818 }
kwiberg5a25d952016-08-17 07:31:12 -07001819 output_file_player_->RegisterModuleFileCallback(NULL);
1820 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001821 channel_state_.SetOutputFilePlaying(false);
1822 }
1823 // _fileCritSect cannot be taken while calling
1824 // SetAnonymousMixibilityStatus. Refer to comments in
1825 // StartPlayingFileLocally(const char* ...) for more details.
1826 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1827 _engineStatisticsPtr->SetLastError(
1828 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1829 "StopPlayingFile() failed to stop participant from playing as"
1830 "file in the mixer");
1831 return -1;
1832 }
1833
1834 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001835}
1836
kwiberg55b97fe2016-01-28 05:22:45 -08001837int Channel::IsPlayingFileLocally() const {
1838 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001839}
1840
kwiberg55b97fe2016-01-28 05:22:45 -08001841int Channel::RegisterFilePlayingToMixer() {
1842 // Return success for not registering for file playing to mixer if:
1843 // 1. playing file before playout is started on that channel.
1844 // 2. starting playout without file playing on that channel.
1845 if (!channel_state_.Get().playing ||
1846 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001847 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001848 }
1849
1850 // |_fileCritSect| cannot be taken while calling
1851 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1852 // frames can be pulled by the mixer. Since the frames are generated from
1853 // the file, _fileCritSect will be taken. This would result in a deadlock.
1854 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1855 channel_state_.SetOutputFilePlaying(false);
1856 rtc::CritScope cs(&_fileCritSect);
1857 _engineStatisticsPtr->SetLastError(
1858 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1859 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001860 output_file_player_->StopPlayingFile();
1861 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001862 return -1;
1863 }
1864
1865 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001866}
1867
niklase@google.com470e71d2011-07-07 08:21:25 +00001868int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001869 bool loop,
1870 FileFormats format,
1871 int startPosition,
1872 float volumeScaling,
1873 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001874 const CodecInst* codecInst) {
1875 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1876 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1877 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1878 "stopPosition=%d)",
1879 fileName, loop, format, volumeScaling, startPosition,
1880 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001881
kwiberg55b97fe2016-01-28 05:22:45 -08001882 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001883
kwiberg55b97fe2016-01-28 05:22:45 -08001884 if (channel_state_.Get().input_file_playing) {
1885 _engineStatisticsPtr->SetLastError(
1886 VE_ALREADY_PLAYING, kTraceWarning,
1887 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001888 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001889 }
1890
1891 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001892 if (input_file_player_) {
1893 input_file_player_->RegisterModuleFileCallback(NULL);
1894 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001895 }
1896
1897 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001898 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001899 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001900
kwiberg5a25d952016-08-17 07:31:12 -07001901 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001902 _engineStatisticsPtr->SetLastError(
1903 VE_INVALID_ARGUMENT, kTraceError,
1904 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1905 return -1;
1906 }
1907
1908 const uint32_t notificationTime(0);
1909
kwiberg5a25d952016-08-17 07:31:12 -07001910 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001911 fileName, loop, startPosition, volumeScaling, notificationTime,
1912 stopPosition, (const CodecInst*)codecInst) != 0) {
1913 _engineStatisticsPtr->SetLastError(
1914 VE_BAD_FILE, kTraceError,
1915 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001916 input_file_player_->StopPlayingFile();
1917 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001918 return -1;
1919 }
kwiberg5a25d952016-08-17 07:31:12 -07001920 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001921 channel_state_.SetInputFilePlaying(true);
1922
1923 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001924}
1925
1926int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001927 FileFormats format,
1928 int startPosition,
1929 float volumeScaling,
1930 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001931 const CodecInst* codecInst) {
1932 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1933 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1934 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1935 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001936
kwiberg55b97fe2016-01-28 05:22:45 -08001937 if (stream == NULL) {
1938 _engineStatisticsPtr->SetLastError(
1939 VE_BAD_FILE, kTraceError,
1940 "StartPlayingFileAsMicrophone NULL as input stream");
1941 return -1;
1942 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001943
kwiberg55b97fe2016-01-28 05:22:45 -08001944 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001945
kwiberg55b97fe2016-01-28 05:22:45 -08001946 if (channel_state_.Get().input_file_playing) {
1947 _engineStatisticsPtr->SetLastError(
1948 VE_ALREADY_PLAYING, kTraceWarning,
1949 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001950 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001951 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001952
kwiberg55b97fe2016-01-28 05:22:45 -08001953 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001954 if (input_file_player_) {
1955 input_file_player_->RegisterModuleFileCallback(NULL);
1956 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001957 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001958
kwiberg55b97fe2016-01-28 05:22:45 -08001959 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001960 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001961 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001962
kwiberg5a25d952016-08-17 07:31:12 -07001963 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001964 _engineStatisticsPtr->SetLastError(
1965 VE_INVALID_ARGUMENT, kTraceError,
1966 "StartPlayingInputFile() filePlayer format isnot correct");
1967 return -1;
1968 }
1969
1970 const uint32_t notificationTime(0);
1971
kwiberg4ec01d92016-08-22 08:43:54 -07001972 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
1973 notificationTime, stopPosition,
1974 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001975 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1976 "StartPlayingFile() failed to start "
1977 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001978 input_file_player_->StopPlayingFile();
1979 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001980 return -1;
1981 }
1982
kwiberg5a25d952016-08-17 07:31:12 -07001983 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001984 channel_state_.SetInputFilePlaying(true);
1985
1986 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001987}
1988
kwiberg55b97fe2016-01-28 05:22:45 -08001989int Channel::StopPlayingFileAsMicrophone() {
1990 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1991 "Channel::StopPlayingFileAsMicrophone()");
1992
1993 rtc::CritScope cs(&_fileCritSect);
1994
1995 if (!channel_state_.Get().input_file_playing) {
1996 return 0;
1997 }
1998
kwiberg5a25d952016-08-17 07:31:12 -07001999 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002000 _engineStatisticsPtr->SetLastError(
2001 VE_STOP_RECORDING_FAILED, kTraceError,
2002 "StopPlayingFile() could not stop playing");
2003 return -1;
2004 }
kwiberg5a25d952016-08-17 07:31:12 -07002005 input_file_player_->RegisterModuleFileCallback(NULL);
2006 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002007 channel_state_.SetInputFilePlaying(false);
2008
2009 return 0;
2010}
2011
2012int Channel::IsPlayingFileAsMicrophone() const {
2013 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002014}
2015
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002016int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002017 const CodecInst* codecInst) {
2018 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2019 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002020
kwiberg55b97fe2016-01-28 05:22:45 -08002021 if (_outputFileRecording) {
2022 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2023 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002024 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002025 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002026
kwiberg55b97fe2016-01-28 05:22:45 -08002027 FileFormats format;
2028 const uint32_t notificationTime(0); // Not supported in VoE
2029 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002030
kwiberg55b97fe2016-01-28 05:22:45 -08002031 if ((codecInst != NULL) &&
2032 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2033 _engineStatisticsPtr->SetLastError(
2034 VE_BAD_ARGUMENT, kTraceError,
2035 "StartRecordingPlayout() invalid compression");
2036 return (-1);
2037 }
2038 if (codecInst == NULL) {
2039 format = kFileFormatPcm16kHzFile;
2040 codecInst = &dummyCodec;
2041 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2042 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2043 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2044 format = kFileFormatWavFile;
2045 } else {
2046 format = kFileFormatCompressedFile;
2047 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002048
kwiberg55b97fe2016-01-28 05:22:45 -08002049 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002050
kwiberg55b97fe2016-01-28 05:22:45 -08002051 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002052 if (output_file_recorder_) {
2053 output_file_recorder_->RegisterModuleFileCallback(NULL);
2054 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002055 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002056
kwiberg5a25d952016-08-17 07:31:12 -07002057 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002058 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002059 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002060 _engineStatisticsPtr->SetLastError(
2061 VE_INVALID_ARGUMENT, kTraceError,
2062 "StartRecordingPlayout() fileRecorder format isnot correct");
2063 return -1;
2064 }
2065
kwiberg5a25d952016-08-17 07:31:12 -07002066 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002067 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2068 _engineStatisticsPtr->SetLastError(
2069 VE_BAD_FILE, kTraceError,
2070 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002071 output_file_recorder_->StopRecording();
2072 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002073 return -1;
2074 }
kwiberg5a25d952016-08-17 07:31:12 -07002075 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002076 _outputFileRecording = true;
2077
2078 return 0;
2079}
2080
2081int Channel::StartRecordingPlayout(OutStream* stream,
2082 const CodecInst* codecInst) {
2083 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2084 "Channel::StartRecordingPlayout()");
2085
2086 if (_outputFileRecording) {
2087 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2088 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002089 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002090 }
2091
2092 FileFormats format;
2093 const uint32_t notificationTime(0); // Not supported in VoE
2094 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2095
2096 if (codecInst != NULL && codecInst->channels != 1) {
2097 _engineStatisticsPtr->SetLastError(
2098 VE_BAD_ARGUMENT, kTraceError,
2099 "StartRecordingPlayout() invalid compression");
2100 return (-1);
2101 }
2102 if (codecInst == NULL) {
2103 format = kFileFormatPcm16kHzFile;
2104 codecInst = &dummyCodec;
2105 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2106 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2107 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2108 format = kFileFormatWavFile;
2109 } else {
2110 format = kFileFormatCompressedFile;
2111 }
2112
2113 rtc::CritScope cs(&_fileCritSect);
2114
2115 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002116 if (output_file_recorder_) {
2117 output_file_recorder_->RegisterModuleFileCallback(NULL);
2118 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002119 }
2120
kwiberg5a25d952016-08-17 07:31:12 -07002121 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002122 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002123 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002124 _engineStatisticsPtr->SetLastError(
2125 VE_INVALID_ARGUMENT, kTraceError,
2126 "StartRecordingPlayout() fileRecorder format isnot correct");
2127 return -1;
2128 }
2129
kwiberg4ec01d92016-08-22 08:43:54 -07002130 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002131 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002132 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2133 "StartRecordingPlayout() failed to "
2134 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002135 output_file_recorder_->StopRecording();
2136 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002137 return -1;
2138 }
2139
kwiberg5a25d952016-08-17 07:31:12 -07002140 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002141 _outputFileRecording = true;
2142
2143 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002144}
2145
kwiberg55b97fe2016-01-28 05:22:45 -08002146int Channel::StopRecordingPlayout() {
2147 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2148 "Channel::StopRecordingPlayout()");
2149
2150 if (!_outputFileRecording) {
2151 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2152 "StopRecordingPlayout() isnot recording");
2153 return -1;
2154 }
2155
2156 rtc::CritScope cs(&_fileCritSect);
2157
kwiberg5a25d952016-08-17 07:31:12 -07002158 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002159 _engineStatisticsPtr->SetLastError(
2160 VE_STOP_RECORDING_FAILED, kTraceError,
2161 "StopRecording() could not stop recording");
2162 return (-1);
2163 }
kwiberg5a25d952016-08-17 07:31:12 -07002164 output_file_recorder_->RegisterModuleFileCallback(NULL);
2165 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002166 _outputFileRecording = false;
2167
2168 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002169}
2170
kwiberg55b97fe2016-01-28 05:22:45 -08002171void Channel::SetMixWithMicStatus(bool mix) {
2172 rtc::CritScope cs(&_fileCritSect);
2173 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002174}
2175
kwiberg55b97fe2016-01-28 05:22:45 -08002176int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2177 int8_t currentLevel = _outputAudioLevel.Level();
2178 level = static_cast<int32_t>(currentLevel);
2179 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002180}
2181
kwiberg55b97fe2016-01-28 05:22:45 -08002182int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2183 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2184 level = static_cast<int32_t>(currentLevel);
2185 return 0;
2186}
2187
solenberg1c2af8e2016-03-24 10:36:00 -07002188int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002189 rtc::CritScope cs(&volume_settings_critsect_);
2190 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002191 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002192 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002193 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002194}
2195
solenberg1c2af8e2016-03-24 10:36:00 -07002196bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002197 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002198 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002199}
2200
kwiberg55b97fe2016-01-28 05:22:45 -08002201int Channel::SetOutputVolumePan(float left, float right) {
2202 rtc::CritScope cs(&volume_settings_critsect_);
2203 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002204 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002205 _panLeft = left;
2206 _panRight = right;
2207 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002208}
2209
kwiberg55b97fe2016-01-28 05:22:45 -08002210int Channel::GetOutputVolumePan(float& left, float& right) const {
2211 rtc::CritScope cs(&volume_settings_critsect_);
2212 left = _panLeft;
2213 right = _panRight;
2214 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002215}
2216
kwiberg55b97fe2016-01-28 05:22:45 -08002217int Channel::SetChannelOutputVolumeScaling(float scaling) {
2218 rtc::CritScope cs(&volume_settings_critsect_);
2219 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002220 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002221 _outputGain = scaling;
2222 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002223}
2224
kwiberg55b97fe2016-01-28 05:22:45 -08002225int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2226 rtc::CritScope cs(&volume_settings_critsect_);
2227 scaling = _outputGain;
2228 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002229}
2230
solenberg8842c3e2016-03-11 03:06:41 -08002231int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002232 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002233 "Channel::SendTelephoneEventOutband(...)");
2234 RTC_DCHECK_LE(0, event);
2235 RTC_DCHECK_GE(255, event);
2236 RTC_DCHECK_LE(0, duration_ms);
2237 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002238 if (!Sending()) {
2239 return -1;
2240 }
solenberg8842c3e2016-03-11 03:06:41 -08002241 if (_rtpRtcpModule->SendTelephoneEventOutband(
2242 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002243 _engineStatisticsPtr->SetLastError(
2244 VE_SEND_DTMF_FAILED, kTraceWarning,
2245 "SendTelephoneEventOutband() failed to send event");
2246 return -1;
2247 }
2248 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002249}
2250
solenberg31642aa2016-03-14 08:00:37 -07002251int Channel::SetSendTelephoneEventPayloadType(int payload_type) {
kwiberg55b97fe2016-01-28 05:22:45 -08002252 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002253 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002254 RTC_DCHECK_LE(0, payload_type);
2255 RTC_DCHECK_GE(127, payload_type);
2256 CodecInst codec = {0};
kwiberg55b97fe2016-01-28 05:22:45 -08002257 codec.plfreq = 8000;
solenberg31642aa2016-03-14 08:00:37 -07002258 codec.pltype = payload_type;
kwiberg55b97fe2016-01-28 05:22:45 -08002259 memcpy(codec.plname, "telephone-event", 16);
2260 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2261 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2262 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2263 _engineStatisticsPtr->SetLastError(
2264 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2265 "SetSendTelephoneEventPayloadType() failed to register send"
2266 "payload type");
2267 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002268 }
kwiberg55b97fe2016-01-28 05:22:45 -08002269 }
kwiberg55b97fe2016-01-28 05:22:45 -08002270 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002271}
2272
kwiberg55b97fe2016-01-28 05:22:45 -08002273int Channel::VoiceActivityIndicator(int& activity) {
2274 activity = _sendFrameType;
2275 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002276}
2277
kwiberg55b97fe2016-01-28 05:22:45 -08002278int Channel::SetLocalSSRC(unsigned int ssrc) {
2279 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2280 "Channel::SetLocalSSRC()");
2281 if (channel_state_.Get().sending) {
2282 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2283 "SetLocalSSRC() already sending");
2284 return -1;
2285 }
2286 _rtpRtcpModule->SetSSRC(ssrc);
2287 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002288}
2289
kwiberg55b97fe2016-01-28 05:22:45 -08002290int Channel::GetLocalSSRC(unsigned int& ssrc) {
2291 ssrc = _rtpRtcpModule->SSRC();
2292 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002293}
2294
kwiberg55b97fe2016-01-28 05:22:45 -08002295int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2296 ssrc = rtp_receiver_->SSRC();
2297 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002298}
2299
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002300int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002301 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002302 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002303}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002304
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002305int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2306 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002307 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2308 if (enable &&
2309 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2310 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002311 return -1;
2312 }
2313 return 0;
2314}
2315
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002316int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2317 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2318}
2319
2320int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2321 rtp_header_parser_->DeregisterRtpHeaderExtension(
2322 kRtpExtensionAbsoluteSendTime);
kwiberg55b97fe2016-01-28 05:22:45 -08002323 if (enable &&
2324 !rtp_header_parser_->RegisterRtpHeaderExtension(
2325 kRtpExtensionAbsoluteSendTime, id)) {
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002326 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002327 }
2328 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002329}
2330
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002331void Channel::EnableSendTransportSequenceNumber(int id) {
2332 int ret =
2333 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2334 RTC_DCHECK_EQ(0, ret);
2335}
2336
stefan3313ec92016-01-21 06:32:43 -08002337void Channel::EnableReceiveTransportSequenceNumber(int id) {
2338 rtp_header_parser_->DeregisterRtpHeaderExtension(
2339 kRtpExtensionTransportSequenceNumber);
2340 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2341 kRtpExtensionTransportSequenceNumber, id);
2342 RTC_DCHECK(ret);
2343}
2344
stefanbba9dec2016-02-01 04:39:55 -08002345void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002346 RtpPacketSender* rtp_packet_sender,
2347 TransportFeedbackObserver* transport_feedback_observer,
2348 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002349 RTC_DCHECK(rtp_packet_sender);
2350 RTC_DCHECK(transport_feedback_observer);
2351 RTC_DCHECK(packet_router && !packet_router_);
2352 feedback_observer_proxy_->SetTransportFeedbackObserver(
2353 transport_feedback_observer);
2354 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2355 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2356 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002357 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002358 packet_router_ = packet_router;
2359}
2360
stefanbba9dec2016-02-01 04:39:55 -08002361void Channel::RegisterReceiverCongestionControlObjects(
2362 PacketRouter* packet_router) {
2363 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002364 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002365 packet_router_ = packet_router;
2366}
2367
2368void Channel::ResetCongestionControlObjects() {
2369 RTC_DCHECK(packet_router_);
2370 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2371 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2372 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002373 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002374 packet_router_ = nullptr;
2375 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2376}
2377
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002378void Channel::SetRTCPStatus(bool enable) {
2379 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2380 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002381 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002382}
2383
kwiberg55b97fe2016-01-28 05:22:45 -08002384int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002385 RtcpMode method = _rtpRtcpModule->RTCP();
2386 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002387 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002388}
2389
kwiberg55b97fe2016-01-28 05:22:45 -08002390int Channel::SetRTCP_CNAME(const char cName[256]) {
2391 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2392 "Channel::SetRTCP_CNAME()");
2393 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2394 _engineStatisticsPtr->SetLastError(
2395 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2396 "SetRTCP_CNAME() failed to set RTCP CNAME");
2397 return -1;
2398 }
2399 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002400}
2401
kwiberg55b97fe2016-01-28 05:22:45 -08002402int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2403 if (cName == NULL) {
2404 _engineStatisticsPtr->SetLastError(
2405 VE_INVALID_ARGUMENT, kTraceError,
2406 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2407 return -1;
2408 }
2409 char cname[RTCP_CNAME_SIZE];
2410 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2411 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2412 _engineStatisticsPtr->SetLastError(
2413 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2414 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2415 return -1;
2416 }
2417 strcpy(cName, cname);
2418 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002419}
2420
kwiberg55b97fe2016-01-28 05:22:45 -08002421int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2422 unsigned int& NTPLow,
2423 unsigned int& timestamp,
2424 unsigned int& playoutTimestamp,
2425 unsigned int* jitter,
2426 unsigned short* fractionLost) {
2427 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002428
kwiberg55b97fe2016-01-28 05:22:45 -08002429 RTCPSenderInfo senderInfo;
2430 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2431 _engineStatisticsPtr->SetLastError(
2432 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2433 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2434 "side");
2435 return -1;
2436 }
2437
2438 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2439 // and octet count)
2440 NTPHigh = senderInfo.NTPseconds;
2441 NTPLow = senderInfo.NTPfraction;
2442 timestamp = senderInfo.RTPtimeStamp;
2443
2444 // --- Locally derived information
2445
2446 // This value is updated on each incoming RTCP packet (0 when no packet
2447 // has been received)
2448 playoutTimestamp = playout_timestamp_rtcp_;
2449
2450 if (NULL != jitter || NULL != fractionLost) {
2451 // Get all RTCP receiver report blocks that have been received on this
2452 // channel. If we receive RTP packets from a remote source we know the
2453 // remote SSRC and use the report block from him.
2454 // Otherwise use the first report block.
2455 std::vector<RTCPReportBlock> remote_stats;
2456 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2457 remote_stats.empty()) {
2458 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2459 "GetRemoteRTCPData() failed to measure statistics due"
2460 " to lack of received RTP and/or RTCP packets");
2461 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002462 }
2463
kwiberg55b97fe2016-01-28 05:22:45 -08002464 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2465 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2466 for (; it != remote_stats.end(); ++it) {
2467 if (it->remoteSSRC == remoteSSRC)
2468 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002469 }
kwiberg55b97fe2016-01-28 05:22:45 -08002470
2471 if (it == remote_stats.end()) {
2472 // If we have not received any RTCP packets from this SSRC it probably
2473 // means that we have not received any RTP packets.
2474 // Use the first received report block instead.
2475 it = remote_stats.begin();
2476 remoteSSRC = it->remoteSSRC;
2477 }
2478
2479 if (jitter) {
2480 *jitter = it->jitter;
2481 }
2482
2483 if (fractionLost) {
2484 *fractionLost = it->fractionLost;
2485 }
2486 }
2487 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002488}
2489
kwiberg55b97fe2016-01-28 05:22:45 -08002490int Channel::SendApplicationDefinedRTCPPacket(
2491 unsigned char subType,
2492 unsigned int name,
2493 const char* data,
2494 unsigned short dataLengthInBytes) {
2495 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2496 "Channel::SendApplicationDefinedRTCPPacket()");
2497 if (!channel_state_.Get().sending) {
2498 _engineStatisticsPtr->SetLastError(
2499 VE_NOT_SENDING, kTraceError,
2500 "SendApplicationDefinedRTCPPacket() not sending");
2501 return -1;
2502 }
2503 if (NULL == data) {
2504 _engineStatisticsPtr->SetLastError(
2505 VE_INVALID_ARGUMENT, kTraceError,
2506 "SendApplicationDefinedRTCPPacket() invalid data value");
2507 return -1;
2508 }
2509 if (dataLengthInBytes % 4 != 0) {
2510 _engineStatisticsPtr->SetLastError(
2511 VE_INVALID_ARGUMENT, kTraceError,
2512 "SendApplicationDefinedRTCPPacket() invalid length value");
2513 return -1;
2514 }
2515 RtcpMode status = _rtpRtcpModule->RTCP();
2516 if (status == RtcpMode::kOff) {
2517 _engineStatisticsPtr->SetLastError(
2518 VE_RTCP_ERROR, kTraceError,
2519 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2520 return -1;
2521 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002522
kwiberg55b97fe2016-01-28 05:22:45 -08002523 // Create and schedule the RTCP APP packet for transmission
2524 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2525 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2526 _engineStatisticsPtr->SetLastError(
2527 VE_SEND_ERROR, kTraceError,
2528 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2529 return -1;
2530 }
2531 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002532}
2533
kwiberg55b97fe2016-01-28 05:22:45 -08002534int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2535 unsigned int& maxJitterMs,
2536 unsigned int& discardedPackets) {
2537 // The jitter statistics is updated for each received RTP packet and is
2538 // based on received packets.
2539 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2540 // If RTCP is off, there is no timed thread in the RTCP module regularly
2541 // generating new stats, trigger the update manually here instead.
2542 StreamStatistician* statistician =
2543 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2544 if (statistician) {
2545 // Don't use returned statistics, use data from proxy instead so that
2546 // max jitter can be fetched atomically.
2547 RtcpStatistics s;
2548 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002549 }
kwiberg55b97fe2016-01-28 05:22:45 -08002550 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002551
kwiberg55b97fe2016-01-28 05:22:45 -08002552 ChannelStatistics stats = statistics_proxy_->GetStats();
2553 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2554 if (playoutFrequency > 0) {
2555 // Scale RTP statistics given the current playout frequency
2556 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2557 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2558 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002559
kwiberg55b97fe2016-01-28 05:22:45 -08002560 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002561
kwiberg55b97fe2016-01-28 05:22:45 -08002562 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002563}
2564
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002565int Channel::GetRemoteRTCPReportBlocks(
2566 std::vector<ReportBlock>* report_blocks) {
2567 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002568 _engineStatisticsPtr->SetLastError(
2569 VE_INVALID_ARGUMENT, kTraceError,
2570 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002571 return -1;
2572 }
2573
2574 // Get the report blocks from the latest received RTCP Sender or Receiver
2575 // Report. Each element in the vector contains the sender's SSRC and a
2576 // report block according to RFC 3550.
2577 std::vector<RTCPReportBlock> rtcp_report_blocks;
2578 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002579 return -1;
2580 }
2581
2582 if (rtcp_report_blocks.empty())
2583 return 0;
2584
2585 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2586 for (; it != rtcp_report_blocks.end(); ++it) {
2587 ReportBlock report_block;
2588 report_block.sender_SSRC = it->remoteSSRC;
2589 report_block.source_SSRC = it->sourceSSRC;
2590 report_block.fraction_lost = it->fractionLost;
2591 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2592 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2593 report_block.interarrival_jitter = it->jitter;
2594 report_block.last_SR_timestamp = it->lastSR;
2595 report_block.delay_since_last_SR = it->delaySinceLastSR;
2596 report_blocks->push_back(report_block);
2597 }
2598 return 0;
2599}
2600
kwiberg55b97fe2016-01-28 05:22:45 -08002601int Channel::GetRTPStatistics(CallStatistics& stats) {
2602 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002603
kwiberg55b97fe2016-01-28 05:22:45 -08002604 // The jitter statistics is updated for each received RTP packet and is
2605 // based on received packets.
2606 RtcpStatistics statistics;
2607 StreamStatistician* statistician =
2608 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002609 if (statistician) {
2610 statistician->GetStatistics(&statistics,
2611 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002612 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002613
kwiberg55b97fe2016-01-28 05:22:45 -08002614 stats.fractionLost = statistics.fraction_lost;
2615 stats.cumulativeLost = statistics.cumulative_lost;
2616 stats.extendedMax = statistics.extended_max_sequence_number;
2617 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002618
kwiberg55b97fe2016-01-28 05:22:45 -08002619 // --- RTT
2620 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002621
kwiberg55b97fe2016-01-28 05:22:45 -08002622 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002623
kwiberg55b97fe2016-01-28 05:22:45 -08002624 size_t bytesSent(0);
2625 uint32_t packetsSent(0);
2626 size_t bytesReceived(0);
2627 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002628
kwiberg55b97fe2016-01-28 05:22:45 -08002629 if (statistician) {
2630 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2631 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002632
kwiberg55b97fe2016-01-28 05:22:45 -08002633 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2634 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2635 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2636 " output will not be complete");
2637 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002638
kwiberg55b97fe2016-01-28 05:22:45 -08002639 stats.bytesSent = bytesSent;
2640 stats.packetsSent = packetsSent;
2641 stats.bytesReceived = bytesReceived;
2642 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002643
kwiberg55b97fe2016-01-28 05:22:45 -08002644 // --- Timestamps
2645 {
2646 rtc::CritScope lock(&ts_stats_lock_);
2647 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2648 }
2649 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002650}
2651
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002652int Channel::SetCodecFECStatus(bool enable) {
2653 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2654 "Channel::SetCodecFECStatus()");
2655
kwibergc8d071e2016-04-06 12:22:38 -07002656 if (!codec_manager_.SetCodecFEC(enable) ||
2657 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002658 _engineStatisticsPtr->SetLastError(
2659 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2660 "SetCodecFECStatus() failed to set FEC state");
2661 return -1;
2662 }
2663 return 0;
2664}
2665
2666bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002667 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002668}
2669
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002670void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2671 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002672 // If pacing is enabled we always store packets.
2673 if (!pacing_enabled_)
2674 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002675 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002676 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002677 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002678 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002679 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002680}
2681
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002682// Called when we are missing one or more packets.
2683int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002684 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2685}
2686
kwiberg55b97fe2016-01-28 05:22:45 -08002687uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2688 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2689 "Channel::Demultiplex()");
2690 _audioFrame.CopyFrom(audioFrame);
2691 _audioFrame.id_ = _channelId;
2692 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002693}
2694
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002695void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002696 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002697 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002698 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002699 CodecInst codec;
2700 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002701
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002702 // Never upsample or upmix the capture signal here. This should be done at the
2703 // end of the send chain.
2704 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2705 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2706 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2707 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002708}
2709
kwiberg55b97fe2016-01-28 05:22:45 -08002710uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2711 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2712 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002713
kwiberg55b97fe2016-01-28 05:22:45 -08002714 if (_audioFrame.samples_per_channel_ == 0) {
2715 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2716 "Channel::PrepareEncodeAndSend() invalid audio frame");
2717 return 0xFFFFFFFF;
2718 }
2719
2720 if (channel_state_.Get().input_file_playing) {
2721 MixOrReplaceAudioWithFile(mixingFrequency);
2722 }
2723
solenberg1c2af8e2016-03-24 10:36:00 -07002724 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2725 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002726
2727 if (channel_state_.Get().input_external_media) {
2728 rtc::CritScope cs(&_callbackCritSect);
2729 const bool isStereo = (_audioFrame.num_channels_ == 2);
2730 if (_inputExternalMediaCallbackPtr) {
2731 _inputExternalMediaCallbackPtr->Process(
2732 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
2733 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
2734 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00002735 }
kwiberg55b97fe2016-01-28 05:22:45 -08002736 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002737
kwiberg55b97fe2016-01-28 05:22:45 -08002738 if (_includeAudioLevelIndication) {
2739 size_t length =
2740 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002741 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002742 if (is_muted && previous_frame_muted_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002743 rms_level_.ProcessMuted(length);
2744 } else {
2745 rms_level_.Process(_audioFrame.data_, length);
niklase@google.com470e71d2011-07-07 08:21:25 +00002746 }
kwiberg55b97fe2016-01-28 05:22:45 -08002747 }
solenberg1c2af8e2016-03-24 10:36:00 -07002748 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002749
kwiberg55b97fe2016-01-28 05:22:45 -08002750 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002751}
2752
kwiberg55b97fe2016-01-28 05:22:45 -08002753uint32_t Channel::EncodeAndSend() {
2754 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2755 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002756
kwiberg55b97fe2016-01-28 05:22:45 -08002757 assert(_audioFrame.num_channels_ <= 2);
2758 if (_audioFrame.samples_per_channel_ == 0) {
2759 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2760 "Channel::EncodeAndSend() invalid audio frame");
2761 return 0xFFFFFFFF;
2762 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002763
kwiberg55b97fe2016-01-28 05:22:45 -08002764 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002765
kwiberg55b97fe2016-01-28 05:22:45 -08002766 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002767
kwiberg55b97fe2016-01-28 05:22:45 -08002768 // The ACM resamples internally.
2769 _audioFrame.timestamp_ = _timeStamp;
2770 // This call will trigger AudioPacketizationCallback::SendData if encoding
2771 // is done and payload is ready for packetization and transmission.
2772 // Otherwise, it will return without invoking the callback.
2773 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2774 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2775 "Channel::EncodeAndSend() ACM encoding failed");
2776 return 0xFFFFFFFF;
2777 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002778
kwiberg55b97fe2016-01-28 05:22:45 -08002779 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2780 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002781}
2782
Minyue2013aec2015-05-13 14:14:42 +02002783void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002784 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002785 Channel* channel = associate_send_channel_.channel();
2786 if (channel && channel->ChannelId() == channel_id) {
2787 // If this channel is associated with a send channel of the specified
2788 // Channel ID, disassociate with it.
2789 ChannelOwner ref(NULL);
2790 associate_send_channel_ = ref;
2791 }
2792}
2793
ivoc14d5dbe2016-07-04 07:06:55 -07002794void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2795 event_log_proxy_->SetEventLog(event_log);
2796}
2797
kwiberg55b97fe2016-01-28 05:22:45 -08002798int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
2799 VoEMediaProcess& processObject) {
2800 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2801 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002802
kwiberg55b97fe2016-01-28 05:22:45 -08002803 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002804
kwiberg55b97fe2016-01-28 05:22:45 -08002805 if (kPlaybackPerChannel == type) {
2806 if (_outputExternalMediaCallbackPtr) {
2807 _engineStatisticsPtr->SetLastError(
2808 VE_INVALID_OPERATION, kTraceError,
2809 "Channel::RegisterExternalMediaProcessing() "
2810 "output external media already enabled");
2811 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002812 }
kwiberg55b97fe2016-01-28 05:22:45 -08002813 _outputExternalMediaCallbackPtr = &processObject;
2814 _outputExternalMedia = true;
2815 } else if (kRecordingPerChannel == type) {
2816 if (_inputExternalMediaCallbackPtr) {
2817 _engineStatisticsPtr->SetLastError(
2818 VE_INVALID_OPERATION, kTraceError,
2819 "Channel::RegisterExternalMediaProcessing() "
2820 "output external media already enabled");
2821 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002822 }
kwiberg55b97fe2016-01-28 05:22:45 -08002823 _inputExternalMediaCallbackPtr = &processObject;
2824 channel_state_.SetInputExternalMedia(true);
2825 }
2826 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002827}
2828
kwiberg55b97fe2016-01-28 05:22:45 -08002829int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
2830 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2831 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002832
kwiberg55b97fe2016-01-28 05:22:45 -08002833 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002834
kwiberg55b97fe2016-01-28 05:22:45 -08002835 if (kPlaybackPerChannel == type) {
2836 if (!_outputExternalMediaCallbackPtr) {
2837 _engineStatisticsPtr->SetLastError(
2838 VE_INVALID_OPERATION, kTraceWarning,
2839 "Channel::DeRegisterExternalMediaProcessing() "
2840 "output external media already disabled");
2841 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002842 }
kwiberg55b97fe2016-01-28 05:22:45 -08002843 _outputExternalMedia = false;
2844 _outputExternalMediaCallbackPtr = NULL;
2845 } else if (kRecordingPerChannel == type) {
2846 if (!_inputExternalMediaCallbackPtr) {
2847 _engineStatisticsPtr->SetLastError(
2848 VE_INVALID_OPERATION, kTraceWarning,
2849 "Channel::DeRegisterExternalMediaProcessing() "
2850 "input external media already disabled");
2851 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002852 }
kwiberg55b97fe2016-01-28 05:22:45 -08002853 channel_state_.SetInputExternalMedia(false);
2854 _inputExternalMediaCallbackPtr = NULL;
2855 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002856
kwiberg55b97fe2016-01-28 05:22:45 -08002857 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002858}
2859
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002860int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08002861 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2862 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002863
kwiberg55b97fe2016-01-28 05:22:45 -08002864 if (channel_state_.Get().playing) {
2865 _engineStatisticsPtr->SetLastError(
2866 VE_INVALID_OPERATION, kTraceError,
2867 "Channel::SetExternalMixing() "
2868 "external mixing cannot be changed while playing.");
2869 return -1;
2870 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002871
kwiberg55b97fe2016-01-28 05:22:45 -08002872 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002873
kwiberg55b97fe2016-01-28 05:22:45 -08002874 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002875}
2876
kwiberg55b97fe2016-01-28 05:22:45 -08002877int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2878 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002879}
2880
wu@webrtc.org24301a62013-12-13 19:17:43 +00002881void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2882 audio_coding_->GetDecodingCallStatistics(stats);
2883}
2884
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002885bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
2886 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08002887 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07002888 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002889 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002890 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002891}
2892
solenberg358057b2015-11-27 10:46:42 -08002893uint32_t Channel::GetDelayEstimate() const {
2894 int jitter_buffer_delay_ms = 0;
2895 int playout_buffer_delay_ms = 0;
2896 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
2897 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
2898}
2899
deadbeef74375882015-08-13 12:09:10 -07002900int Channel::LeastRequiredDelayMs() const {
2901 return audio_coding_->LeastRequiredDelayMs();
2902}
2903
kwiberg55b97fe2016-01-28 05:22:45 -08002904int Channel::SetMinimumPlayoutDelay(int delayMs) {
2905 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2906 "Channel::SetMinimumPlayoutDelay()");
2907 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2908 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2909 _engineStatisticsPtr->SetLastError(
2910 VE_INVALID_ARGUMENT, kTraceError,
2911 "SetMinimumPlayoutDelay() invalid min delay");
2912 return -1;
2913 }
2914 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2915 _engineStatisticsPtr->SetLastError(
2916 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2917 "SetMinimumPlayoutDelay() failed to set min playout delay");
2918 return -1;
2919 }
2920 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002921}
2922
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002923int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002924 uint32_t playout_timestamp_rtp = 0;
2925 {
tommi31fc21f2016-01-21 10:37:37 -08002926 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002927 playout_timestamp_rtp = playout_timestamp_rtp_;
2928 }
kwiberg55b97fe2016-01-28 05:22:45 -08002929 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002930 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002931 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002932 "GetPlayoutTimestamp() failed to retrieve timestamp");
2933 return -1;
2934 }
deadbeef74375882015-08-13 12:09:10 -07002935 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002936 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002937}
2938
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002939int Channel::SetInitTimestamp(unsigned int timestamp) {
2940 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002941 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002942 if (channel_state_.Get().sending) {
2943 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
2944 "SetInitTimestamp() already sending");
2945 return -1;
2946 }
2947 _rtpRtcpModule->SetStartTimestamp(timestamp);
2948 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002949}
2950
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002951int Channel::SetInitSequenceNumber(short sequenceNumber) {
2952 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2953 "Channel::SetInitSequenceNumber()");
2954 if (channel_state_.Get().sending) {
2955 _engineStatisticsPtr->SetLastError(
2956 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
2957 return -1;
2958 }
2959 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
2960 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002961}
2962
kwiberg55b97fe2016-01-28 05:22:45 -08002963int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2964 RtpReceiver** rtp_receiver) const {
2965 *rtpRtcpModule = _rtpRtcpModule.get();
2966 *rtp_receiver = rtp_receiver_.get();
2967 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002968}
2969
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002970// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2971// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002972int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002973 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002974 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002975
kwiberg55b97fe2016-01-28 05:22:45 -08002976 {
2977 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002978
kwiberg5a25d952016-08-17 07:31:12 -07002979 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002980 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2981 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2982 " doesnt exist");
2983 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002984 }
2985
kwiberg4ec01d92016-08-22 08:43:54 -07002986 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002987 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002988 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2989 "Channel::MixOrReplaceAudioWithFile() file mixing "
2990 "failed");
2991 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002992 }
kwiberg55b97fe2016-01-28 05:22:45 -08002993 if (fileSamples == 0) {
2994 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2995 "Channel::MixOrReplaceAudioWithFile() file is ended");
2996 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002997 }
kwiberg55b97fe2016-01-28 05:22:45 -08002998 }
2999
3000 assert(_audioFrame.samples_per_channel_ == fileSamples);
3001
3002 if (_mixFileWithMicrophone) {
3003 // Currently file stream is always mono.
3004 // TODO(xians): Change the code when FilePlayer supports real stereo.
3005 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3006 1, fileSamples);
3007 } else {
3008 // Replace ACM audio with file.
3009 // Currently file stream is always mono.
3010 // TODO(xians): Change the code when FilePlayer supports real stereo.
3011 _audioFrame.UpdateFrame(
3012 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3013 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3014 }
3015 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003016}
3017
kwiberg55b97fe2016-01-28 05:22:45 -08003018int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3019 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003020
kwibergb7f89d62016-02-17 10:04:18 -08003021 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003022 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003023
kwiberg55b97fe2016-01-28 05:22:45 -08003024 {
3025 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003026
kwiberg5a25d952016-08-17 07:31:12 -07003027 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003028 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3029 "Channel::MixAudioWithFile() file mixing failed");
3030 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003031 }
3032
kwiberg55b97fe2016-01-28 05:22:45 -08003033 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003034 if (output_file_player_->Get10msAudioFromFile(
3035 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003036 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3037 "Channel::MixAudioWithFile() file mixing failed");
3038 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003039 }
kwiberg55b97fe2016-01-28 05:22:45 -08003040 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003041
kwiberg55b97fe2016-01-28 05:22:45 -08003042 if (audioFrame.samples_per_channel_ == fileSamples) {
3043 // Currently file stream is always mono.
3044 // TODO(xians): Change the code when FilePlayer supports real stereo.
3045 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3046 fileSamples);
3047 } else {
3048 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3049 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3050 ") != "
3051 "fileSamples(%" PRIuS ")",
3052 audioFrame.samples_per_channel_, fileSamples);
3053 return -1;
3054 }
3055
3056 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003057}
3058
deadbeef74375882015-08-13 12:09:10 -07003059void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003060 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003061
henrik.lundin96bd5022016-04-06 04:13:56 -07003062 if (!jitter_buffer_playout_timestamp_) {
3063 // This can happen if this channel has not received any RTP packets. In
3064 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003065 return;
3066 }
3067
3068 uint16_t delay_ms = 0;
3069 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003070 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003071 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3072 " delay from the ADM");
3073 _engineStatisticsPtr->SetLastError(
3074 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3075 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3076 return;
3077 }
3078
henrik.lundin96bd5022016-04-06 04:13:56 -07003079 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3080 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003081
3082 // Remove the playout delay.
henrik.lundin96bd5022016-04-06 04:13:56 -07003083 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003084
kwiberg55b97fe2016-01-28 05:22:45 -08003085 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003086 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003087 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003088
3089 {
tommi31fc21f2016-01-21 10:37:37 -08003090 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003091 if (rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003092 playout_timestamp_rtcp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003093 } else {
henrik.lundin96bd5022016-04-06 04:13:56 -07003094 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003095 }
3096 playout_delay_ms_ = delay_ms;
3097 }
3098}
3099
kwiberg55b97fe2016-01-28 05:22:45 -08003100void Channel::RegisterReceiveCodecsToRTPModule() {
3101 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3102 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003103
kwiberg55b97fe2016-01-28 05:22:45 -08003104 CodecInst codec;
3105 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003106
kwiberg55b97fe2016-01-28 05:22:45 -08003107 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3108 // Open up the RTP/RTCP receiver for all supported codecs
3109 if ((audio_coding_->Codec(idx, &codec) == -1) ||
3110 (rtp_receiver_->RegisterReceivePayload(
3111 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3112 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
3113 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3114 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3115 " to register %s (%d/%d/%" PRIuS
3116 "/%d) to RTP/RTCP "
3117 "receiver",
3118 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3119 codec.rate);
3120 } else {
3121 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3122 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3123 "(%d/%d/%" PRIuS
3124 "/%d) has been added to the RTP/RTCP "
3125 "receiver",
3126 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3127 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003128 }
kwiberg55b97fe2016-01-28 05:22:45 -08003129 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003130}
3131
kwiberg55b97fe2016-01-28 05:22:45 -08003132int Channel::SetSendRtpHeaderExtension(bool enable,
3133 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003134 unsigned char id) {
3135 int error = 0;
3136 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3137 if (enable) {
3138 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3139 }
3140 return error;
3141}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003142
henrik.lundinb3e30012016-08-31 14:09:51 -07003143int32_t Channel::GetPlayoutFrequency() const {
wu@webrtc.org94454b72014-06-05 20:34:08 +00003144 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
3145 CodecInst current_recive_codec;
3146 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
3147 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
3148 // Even though the actual sampling rate for G.722 audio is
3149 // 16,000 Hz, the RTP clock rate for the G722 payload format is
3150 // 8,000 Hz because that value was erroneously assigned in
3151 // RFC 1890 and must remain unchanged for backward compatibility.
3152 playout_frequency = 8000;
3153 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
3154 // We are resampling Opus internally to 32,000 Hz until all our
3155 // DSP routines can operate at 48,000 Hz, but the RTP clock
3156 // rate for the Opus payload format is standardized to 48,000 Hz,
3157 // because that is the maximum supported decoding sampling rate.
3158 playout_frequency = 48000;
3159 }
3160 }
3161 return playout_frequency;
3162}
3163
Minyue2013aec2015-05-13 14:14:42 +02003164int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003165 RtcpMode method = _rtpRtcpModule->RTCP();
3166 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003167 return 0;
3168 }
3169 std::vector<RTCPReportBlock> report_blocks;
3170 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003171
3172 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003173 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003174 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003175 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003176 Channel* channel = associate_send_channel_.channel();
3177 // Tries to get RTT from an associated channel. This is important for
3178 // receive-only channels.
3179 if (channel) {
3180 // To prevent infinite recursion and deadlock, calling GetRTT of
3181 // associate channel should always use "false" for argument:
3182 // |allow_associate_channel|.
3183 rtt = channel->GetRTT(false);
3184 }
3185 }
3186 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003187 }
3188
3189 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3190 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3191 for (; it != report_blocks.end(); ++it) {
3192 if (it->remoteSSRC == remoteSSRC)
3193 break;
3194 }
3195 if (it == report_blocks.end()) {
3196 // We have not received packets with SSRC matching the report blocks.
3197 // To calculate RTT we try with the SSRC of the first report block.
3198 // This is very important for send-only channels where we don't know
3199 // the SSRC of the other end.
3200 remoteSSRC = report_blocks[0].remoteSSRC;
3201 }
Minyue2013aec2015-05-13 14:14:42 +02003202
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003203 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003204 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003205 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003206 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3207 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003208 return 0;
3209 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003210 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003211}
3212
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003213} // namespace voe
3214} // namespace webrtc