blob: 66f14bd46326dd994b9f422e43dec84832820ead [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"
kwibergda2bf4e2016-10-24 13:47:09 -070025#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000026#include "webrtc/modules/audio_device/include/audio_device.h"
27#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010028#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010029#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
31#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
32#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000033#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010034#include "webrtc/modules/utility/include/audio_frame_operations.h"
35#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010036#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000037#include "webrtc/voice_engine/include/voe_external_media.h"
38#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
39#include "webrtc/voice_engine/output_mixer.h"
40#include "webrtc/voice_engine/statistics.h"
41#include "webrtc/voice_engine/transmit_mixer.h"
42#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000043
andrew@webrtc.org50419b02012-11-14 19:07:54 +000044namespace webrtc {
45namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000046
kwibergc8d071e2016-04-06 12:22:38 -070047namespace {
48
Erik Språng737336d2016-07-29 12:59:36 +020049constexpr int64_t kMaxRetransmissionWindowMs = 1000;
50constexpr int64_t kMinRetransmissionWindowMs = 30;
51
kwibergc8d071e2016-04-06 12:22:38 -070052} // namespace
53
solenberg8842c3e2016-03-11 03:06:41 -080054const int kTelephoneEventAttenuationdB = 10;
55
ivoc14d5dbe2016-07-04 07:06:55 -070056class RtcEventLogProxy final : public webrtc::RtcEventLog {
57 public:
58 RtcEventLogProxy() : event_log_(nullptr) {}
59
60 bool StartLogging(const std::string& file_name,
61 int64_t max_size_bytes) override {
62 RTC_NOTREACHED();
63 return false;
64 }
65
66 bool StartLogging(rtc::PlatformFile log_file,
67 int64_t max_size_bytes) override {
68 RTC_NOTREACHED();
69 return false;
70 }
71
72 void StopLogging() override { RTC_NOTREACHED(); }
73
74 void LogVideoReceiveStreamConfig(
75 const webrtc::VideoReceiveStream::Config& config) override {
76 rtc::CritScope lock(&crit_);
77 if (event_log_) {
78 event_log_->LogVideoReceiveStreamConfig(config);
79 }
80 }
81
82 void LogVideoSendStreamConfig(
83 const webrtc::VideoSendStream::Config& config) override {
84 rtc::CritScope lock(&crit_);
85 if (event_log_) {
86 event_log_->LogVideoSendStreamConfig(config);
87 }
88 }
89
ivoce0928d82016-10-10 05:12:51 -070090 void LogAudioReceiveStreamConfig(
91 const webrtc::AudioReceiveStream::Config& config) override {
92 rtc::CritScope lock(&crit_);
93 if (event_log_) {
94 event_log_->LogAudioReceiveStreamConfig(config);
95 }
96 }
97
98 void LogAudioSendStreamConfig(
99 const webrtc::AudioSendStream::Config& config) override {
100 rtc::CritScope lock(&crit_);
101 if (event_log_) {
102 event_log_->LogAudioSendStreamConfig(config);
103 }
104 }
105
ivoc14d5dbe2016-07-04 07:06:55 -0700106 void LogRtpHeader(webrtc::PacketDirection direction,
107 webrtc::MediaType media_type,
108 const uint8_t* header,
109 size_t packet_length) override {
110 rtc::CritScope lock(&crit_);
111 if (event_log_) {
112 event_log_->LogRtpHeader(direction, media_type, header, packet_length);
113 }
114 }
115
116 void LogRtcpPacket(webrtc::PacketDirection direction,
117 webrtc::MediaType media_type,
118 const uint8_t* packet,
119 size_t length) override {
120 rtc::CritScope lock(&crit_);
121 if (event_log_) {
122 event_log_->LogRtcpPacket(direction, media_type, packet, length);
123 }
124 }
125
126 void LogAudioPlayout(uint32_t ssrc) override {
127 rtc::CritScope lock(&crit_);
128 if (event_log_) {
129 event_log_->LogAudioPlayout(ssrc);
130 }
131 }
132
133 void LogBwePacketLossEvent(int32_t bitrate,
134 uint8_t fraction_loss,
135 int32_t total_packets) override {
136 rtc::CritScope lock(&crit_);
137 if (event_log_) {
138 event_log_->LogBwePacketLossEvent(bitrate, fraction_loss, total_packets);
139 }
140 }
141
142 void SetEventLog(RtcEventLog* event_log) {
143 rtc::CritScope lock(&crit_);
144 event_log_ = event_log;
145 }
146
147 private:
148 rtc::CriticalSection crit_;
149 RtcEventLog* event_log_ GUARDED_BY(crit_);
150 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
151};
152
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100153class TransportFeedbackProxy : public TransportFeedbackObserver {
154 public:
155 TransportFeedbackProxy() : feedback_observer_(nullptr) {
156 pacer_thread_.DetachFromThread();
157 network_thread_.DetachFromThread();
158 }
159
160 void SetTransportFeedbackObserver(
161 TransportFeedbackObserver* feedback_observer) {
162 RTC_DCHECK(thread_checker_.CalledOnValidThread());
163 rtc::CritScope lock(&crit_);
164 feedback_observer_ = feedback_observer;
165 }
166
167 // Implements TransportFeedbackObserver.
168 void AddPacket(uint16_t sequence_number,
169 size_t length,
philipela1ed0b32016-06-01 06:31:17 -0700170 int probe_cluster_id) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100171 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
172 rtc::CritScope lock(&crit_);
173 if (feedback_observer_)
pbos2169d8b2016-06-20 11:53:02 -0700174 feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100175 }
176 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
177 RTC_DCHECK(network_thread_.CalledOnValidThread());
178 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700179 if (feedback_observer_)
180 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200181 }
182 std::vector<PacketInfo> GetTransportFeedbackVector() const override {
183 RTC_NOTREACHED();
184 return std::vector<PacketInfo>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100185 }
186
187 private:
188 rtc::CriticalSection crit_;
189 rtc::ThreadChecker thread_checker_;
190 rtc::ThreadChecker pacer_thread_;
191 rtc::ThreadChecker network_thread_;
192 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
193};
194
195class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
196 public:
197 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
198 pacer_thread_.DetachFromThread();
199 }
200
201 void SetSequenceNumberAllocator(
202 TransportSequenceNumberAllocator* seq_num_allocator) {
203 RTC_DCHECK(thread_checker_.CalledOnValidThread());
204 rtc::CritScope lock(&crit_);
205 seq_num_allocator_ = seq_num_allocator;
206 }
207
208 // Implements TransportSequenceNumberAllocator.
209 uint16_t AllocateSequenceNumber() override {
210 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
211 rtc::CritScope lock(&crit_);
212 if (!seq_num_allocator_)
213 return 0;
214 return seq_num_allocator_->AllocateSequenceNumber();
215 }
216
217 private:
218 rtc::CriticalSection crit_;
219 rtc::ThreadChecker thread_checker_;
220 rtc::ThreadChecker pacer_thread_;
221 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
222};
223
224class RtpPacketSenderProxy : public RtpPacketSender {
225 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800226 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100227
228 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
229 RTC_DCHECK(thread_checker_.CalledOnValidThread());
230 rtc::CritScope lock(&crit_);
231 rtp_packet_sender_ = rtp_packet_sender;
232 }
233
234 // Implements RtpPacketSender.
235 void InsertPacket(Priority priority,
236 uint32_t ssrc,
237 uint16_t sequence_number,
238 int64_t capture_time_ms,
239 size_t bytes,
240 bool retransmission) override {
241 rtc::CritScope lock(&crit_);
242 if (rtp_packet_sender_) {
243 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
244 capture_time_ms, bytes, retransmission);
245 }
246 }
247
248 private:
249 rtc::ThreadChecker thread_checker_;
250 rtc::CriticalSection crit_;
251 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
252};
253
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000254// Extend the default RTCP statistics struct with max_jitter, defined as the
255// maximum jitter value seen in an RTCP report block.
256struct ChannelStatistics : public RtcpStatistics {
257 ChannelStatistics() : rtcp(), max_jitter(0) {}
258
259 RtcpStatistics rtcp;
260 uint32_t max_jitter;
261};
262
263// Statistics callback, called at each generation of a new RTCP report block.
264class StatisticsProxy : public RtcpStatisticsCallback {
265 public:
tommi31fc21f2016-01-21 10:37:37 -0800266 StatisticsProxy(uint32_t ssrc) : ssrc_(ssrc) {}
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000267 virtual ~StatisticsProxy() {}
268
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000269 void StatisticsUpdated(const RtcpStatistics& statistics,
270 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000271 if (ssrc != ssrc_)
272 return;
273
tommi31fc21f2016-01-21 10:37:37 -0800274 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000275 stats_.rtcp = statistics;
276 if (statistics.jitter > stats_.max_jitter) {
277 stats_.max_jitter = statistics.jitter;
278 }
279 }
280
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000281 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000282
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000283 ChannelStatistics GetStats() {
tommi31fc21f2016-01-21 10:37:37 -0800284 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000285 return stats_;
286 }
287
288 private:
289 // StatisticsUpdated calls are triggered from threads in the RTP module,
290 // while GetStats calls can be triggered from the public voice engine API,
291 // hence synchronization is needed.
tommi31fc21f2016-01-21 10:37:37 -0800292 rtc::CriticalSection stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000293 const uint32_t ssrc_;
294 ChannelStatistics stats_;
295};
296
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000297class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000298 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000299 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
300 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000301
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000302 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
303 // Not used for Voice Engine.
304 }
305
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000306 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
307 int64_t rtt,
308 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000309 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
310 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
311 // report for VoiceEngine?
312 if (report_blocks.empty())
313 return;
314
315 int fraction_lost_aggregate = 0;
316 int total_number_of_packets = 0;
317
318 // If receiving multiple report blocks, calculate the weighted average based
319 // on the number of packets a report refers to.
320 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
321 block_it != report_blocks.end(); ++block_it) {
322 // Find the previous extended high sequence number for this remote SSRC,
323 // to calculate the number of RTP packets this report refers to. Ignore if
324 // we haven't seen this SSRC before.
325 std::map<uint32_t, uint32_t>::iterator seq_num_it =
326 extended_max_sequence_number_.find(block_it->sourceSSRC);
327 int number_of_packets = 0;
328 if (seq_num_it != extended_max_sequence_number_.end()) {
329 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
330 }
331 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
332 total_number_of_packets += number_of_packets;
333
334 extended_max_sequence_number_[block_it->sourceSSRC] =
335 block_it->extendedHighSeqNum;
336 }
337 int weighted_fraction_lost = 0;
338 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800339 weighted_fraction_lost =
340 (fraction_lost_aggregate + total_number_of_packets / 2) /
341 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000342 }
343 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000344 }
345
346 private:
347 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000348 // Maps remote side ssrc to extended highest sequence number received.
349 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000350};
351
kwiberg55b97fe2016-01-28 05:22:45 -0800352int32_t Channel::SendData(FrameType frameType,
353 uint8_t payloadType,
354 uint32_t timeStamp,
355 const uint8_t* payloadData,
356 size_t payloadSize,
357 const RTPFragmentationHeader* fragmentation) {
358 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
359 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
360 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
361 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000362
kwiberg55b97fe2016-01-28 05:22:45 -0800363 if (_includeAudioLevelIndication) {
364 // Store current audio level in the RTP/RTCP module.
365 // The level will be used in combination with voice-activity state
366 // (frameType) to add an RTP header extension
367 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
368 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000369
kwiberg55b97fe2016-01-28 05:22:45 -0800370 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
371 // packetization.
372 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700373 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800374 (FrameType&)frameType, payloadType, timeStamp,
375 // Leaving the time when this frame was
376 // received from the capture device as
377 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700378 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800379 _engineStatisticsPtr->SetLastError(
380 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
381 "Channel::SendData() failed to send data to RTP/RTCP module");
382 return -1;
383 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000384
kwiberg55b97fe2016-01-28 05:22:45 -0800385 _lastLocalTimeStamp = timeStamp;
386 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000387
kwiberg55b97fe2016-01-28 05:22:45 -0800388 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000389}
390
kwiberg55b97fe2016-01-28 05:22:45 -0800391int32_t Channel::InFrameType(FrameType frame_type) {
392 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
393 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000394
kwiberg55b97fe2016-01-28 05:22:45 -0800395 rtc::CritScope cs(&_callbackCritSect);
396 _sendFrameType = (frame_type == kAudioFrameSpeech);
397 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000398}
399
stefan1d8a5062015-10-02 03:39:33 -0700400bool Channel::SendRtp(const uint8_t* data,
401 size_t len,
402 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800403 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
404 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
kwiberg55b97fe2016-01-28 05:22:45 -0800406 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000407
kwiberg55b97fe2016-01-28 05:22:45 -0800408 if (_transportPtr == NULL) {
409 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
410 "Channel::SendPacket() failed to send RTP packet due to"
411 " invalid transport object");
412 return false;
413 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
kwiberg55b97fe2016-01-28 05:22:45 -0800415 uint8_t* bufferToSendPtr = (uint8_t*)data;
416 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000417
kwiberg55b97fe2016-01-28 05:22:45 -0800418 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
419 std::string transport_name =
420 _externalTransport ? "external transport" : "WebRtc sockets";
421 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
422 "Channel::SendPacket() RTP transmission using %s failed",
423 transport_name.c_str());
424 return false;
425 }
426 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000427}
428
kwiberg55b97fe2016-01-28 05:22:45 -0800429bool Channel::SendRtcp(const uint8_t* data, size_t len) {
430 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
431 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000432
kwiberg55b97fe2016-01-28 05:22:45 -0800433 rtc::CritScope cs(&_callbackCritSect);
434 if (_transportPtr == NULL) {
435 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
436 "Channel::SendRtcp() failed to send RTCP packet"
437 " due to invalid transport object");
438 return false;
439 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000440
kwiberg55b97fe2016-01-28 05:22:45 -0800441 uint8_t* bufferToSendPtr = (uint8_t*)data;
442 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000443
kwiberg55b97fe2016-01-28 05:22:45 -0800444 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
445 if (n < 0) {
446 std::string transport_name =
447 _externalTransport ? "external transport" : "WebRtc sockets";
448 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
449 "Channel::SendRtcp() transmission using %s failed",
450 transport_name.c_str());
451 return false;
452 }
453 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000454}
455
kwiberg55b97fe2016-01-28 05:22:45 -0800456void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
457 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
458 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000459
kwiberg55b97fe2016-01-28 05:22:45 -0800460 // Update ssrc so that NTP for AV sync can be updated.
461 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000462}
463
Peter Boströmac547a62015-09-17 23:03:57 +0200464void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
465 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
466 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
467 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000468}
469
Peter Boströmac547a62015-09-17 23:03:57 +0200470int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000471 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000472 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000473 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800474 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200475 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800476 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
477 "Channel::OnInitializeDecoder(payloadType=%d, "
478 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
479 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000480
kwiberg55b97fe2016-01-28 05:22:45 -0800481 CodecInst receiveCodec = {0};
482 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000483
kwiberg55b97fe2016-01-28 05:22:45 -0800484 receiveCodec.pltype = payloadType;
485 receiveCodec.plfreq = frequency;
486 receiveCodec.channels = channels;
487 receiveCodec.rate = rate;
488 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000489
kwiberg55b97fe2016-01-28 05:22:45 -0800490 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
491 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
kwiberg55b97fe2016-01-28 05:22:45 -0800493 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700494 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
495 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800496 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
497 "Channel::OnInitializeDecoder() invalid codec ("
498 "pt=%d, name=%s) received - 1",
499 payloadType, payloadName);
500 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
501 return -1;
502 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000503
kwiberg55b97fe2016-01-28 05:22:45 -0800504 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000505}
506
kwiberg55b97fe2016-01-28 05:22:45 -0800507int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
508 size_t payloadSize,
509 const WebRtcRTPHeader* rtpHeader) {
510 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
511 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
512 ","
513 " payloadType=%u, audioChannel=%" PRIuS ")",
514 payloadSize, rtpHeader->header.payloadType,
515 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000516
kwiberg55b97fe2016-01-28 05:22:45 -0800517 if (!channel_state_.Get().playing) {
518 // Avoid inserting into NetEQ when we are not playing. Count the
519 // packet as discarded.
520 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
521 "received packet is discarded since playing is not"
522 " activated");
523 _numberOfDiscardedPackets++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000524 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800525 }
526
527 // Push the incoming payload (parsed and ready for decoding) into the ACM
528 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
529 0) {
530 _engineStatisticsPtr->SetLastError(
531 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
532 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
533 return -1;
534 }
535
kwiberg55b97fe2016-01-28 05:22:45 -0800536 int64_t round_trip_time = 0;
537 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
538 NULL);
539
540 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
541 if (!nack_list.empty()) {
542 // Can't use nack_list.data() since it's not supported by all
543 // compilers.
544 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
545 }
546 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000547}
548
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000549bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000550 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000551 RTPHeader header;
552 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
553 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
554 "IncomingPacket invalid RTP header");
555 return false;
556 }
557 header.payload_type_frequency =
558 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
559 if (header.payload_type_frequency < 0)
560 return false;
561 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
562}
563
henrik.lundin42dda502016-05-18 05:36:01 -0700564MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
565 int32_t id,
566 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700567 unsigned int ssrc;
568 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
569 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800570 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700571 bool muted;
572 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
573 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800574 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
575 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
576 // In all likelihood, the audio in this frame is garbage. We return an
577 // error so that the audio mixer module doesn't add it to the mix. As
578 // a result, it won't be played out and the actions skipped here are
579 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700580 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800581 }
henrik.lundina89ab962016-05-18 08:52:45 -0700582
583 if (muted) {
584 // TODO(henrik.lundin): We should be able to do better than this. But we
585 // will have to go through all the cases below where the audio samples may
586 // be used, and handle the muted case in some way.
587 audioFrame->Mute();
588 }
kwiberg55b97fe2016-01-28 05:22:45 -0800589
kwiberg55b97fe2016-01-28 05:22:45 -0800590 // Convert module ID to internal VoE channel ID
591 audioFrame->id_ = VoEChannelId(audioFrame->id_);
592 // Store speech type for dead-or-alive detection
593 _outputSpeechType = audioFrame->speech_type_;
594
595 ChannelState::State state = channel_state_.Get();
596
kwiberg55b97fe2016-01-28 05:22:45 -0800597 {
598 // Pass the audio buffers to an optional sink callback, before applying
599 // scaling/panning, as that applies to the mix operation.
600 // External recipients of the audio (e.g. via AudioTrack), will do their
601 // own mixing/dynamic processing.
602 rtc::CritScope cs(&_callbackCritSect);
603 if (audio_sink_) {
604 AudioSinkInterface::Data data(
605 &audioFrame->data_[0], audioFrame->samples_per_channel_,
606 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
607 audioFrame->timestamp_);
608 audio_sink_->OnData(data);
609 }
610 }
611
612 float output_gain = 1.0f;
613 float left_pan = 1.0f;
614 float right_pan = 1.0f;
615 {
616 rtc::CritScope cs(&volume_settings_critsect_);
617 output_gain = _outputGain;
618 left_pan = _panLeft;
619 right_pan = _panRight;
620 }
621
622 // Output volume scaling
623 if (output_gain < 0.99f || output_gain > 1.01f) {
624 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
625 }
626
627 // Scale left and/or right channel(s) if stereo and master balance is
628 // active
629
630 if (left_pan != 1.0f || right_pan != 1.0f) {
631 if (audioFrame->num_channels_ == 1) {
632 // Emulate stereo mode since panning is active.
633 // The mono signal is copied to both left and right channels here.
634 AudioFrameOperations::MonoToStereo(audioFrame);
635 }
636 // For true stereo mode (when we are receiving a stereo signal), no
637 // action is needed.
638
639 // Do the panning operation (the audio frame contains stereo at this
640 // stage)
641 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
642 }
643
644 // Mix decoded PCM output with file if file mixing is enabled
645 if (state.output_file_playing) {
646 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700647 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800648 }
649
650 // External media
651 if (_outputExternalMedia) {
652 rtc::CritScope cs(&_callbackCritSect);
653 const bool isStereo = (audioFrame->num_channels_ == 2);
654 if (_outputExternalMediaCallbackPtr) {
655 _outputExternalMediaCallbackPtr->Process(
656 _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_,
657 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
658 isStereo);
659 }
660 }
661
662 // Record playout if enabled
663 {
664 rtc::CritScope cs(&_fileCritSect);
665
kwiberg5a25d952016-08-17 07:31:12 -0700666 if (_outputFileRecording && output_file_recorder_) {
667 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800668 }
669 }
670
671 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700672 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800673 _outputAudioLevel.ComputeLevel(*audioFrame);
674
675 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
676 // The first frame with a valid rtp timestamp.
677 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
678 }
679
680 if (capture_start_rtp_time_stamp_ >= 0) {
681 // audioFrame.timestamp_ should be valid from now on.
682
683 // Compute elapsed time.
684 int64_t unwrap_timestamp =
685 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
686 audioFrame->elapsed_time_ms_ =
687 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700688 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800689
niklase@google.com470e71d2011-07-07 08:21:25 +0000690 {
kwiberg55b97fe2016-01-28 05:22:45 -0800691 rtc::CritScope lock(&ts_stats_lock_);
692 // Compute ntp time.
693 audioFrame->ntp_time_ms_ =
694 ntp_estimator_.Estimate(audioFrame->timestamp_);
695 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
696 if (audioFrame->ntp_time_ms_ > 0) {
697 // Compute |capture_start_ntp_time_ms_| so that
698 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
699 capture_start_ntp_time_ms_ =
700 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000701 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000702 }
kwiberg55b97fe2016-01-28 05:22:45 -0800703 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000704
henrik.lundin42dda502016-05-18 05:36:01 -0700705 return muted ? MixerParticipant::AudioFrameInfo::kMuted
706 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000707}
708
aleloi6c278492016-10-20 14:24:39 -0700709AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
710 int sample_rate_hz,
711 AudioFrame* audio_frame) {
712 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700713
aleloi6c278492016-10-20 14:24:39 -0700714 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700715
716 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
717 FrameInfo new_audio_frame_info = FrameInfo::kError;
718 switch (frame_info) {
719 case MixerParticipant::AudioFrameInfo::kNormal:
720 new_audio_frame_info = FrameInfo::kNormal;
721 break;
722 case MixerParticipant::AudioFrameInfo::kMuted:
723 new_audio_frame_info = FrameInfo::kMuted;
724 break;
725 case MixerParticipant::AudioFrameInfo::kError:
726 new_audio_frame_info = FrameInfo::kError;
727 break;
728 }
aleloi6c278492016-10-20 14:24:39 -0700729 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700730}
731
kwiberg55b97fe2016-01-28 05:22:45 -0800732int32_t Channel::NeededFrequency(int32_t id) const {
733 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
734 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000735
kwiberg55b97fe2016-01-28 05:22:45 -0800736 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000737
kwiberg55b97fe2016-01-28 05:22:45 -0800738 // Determine highest needed receive frequency
739 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000740
kwiberg55b97fe2016-01-28 05:22:45 -0800741 // Return the bigger of playout and receive frequency in the ACM.
742 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
743 highestNeeded = audio_coding_->PlayoutFrequency();
744 } else {
745 highestNeeded = receiveFrequency;
746 }
747
748 // Special case, if we're playing a file on the playout side
749 // we take that frequency into consideration as well
750 // This is not needed on sending side, since the codec will
751 // limit the spectrum anyway.
752 if (channel_state_.Get().output_file_playing) {
753 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700754 if (output_file_player_) {
755 if (output_file_player_->Frequency() > highestNeeded) {
756 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800757 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000758 }
kwiberg55b97fe2016-01-28 05:22:45 -0800759 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000760
kwiberg55b97fe2016-01-28 05:22:45 -0800761 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000762}
763
ossu5f7cfa52016-05-30 08:11:28 -0700764int32_t Channel::CreateChannel(
765 Channel*& channel,
766 int32_t channelId,
767 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700768 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800769 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
770 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
771 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000772
solenberg88499ec2016-09-07 07:34:41 -0700773 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800774 if (channel == NULL) {
775 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
776 "Channel::CreateChannel() unable to allocate memory for"
777 " channel");
778 return -1;
779 }
780 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000781}
782
kwiberg55b97fe2016-01-28 05:22:45 -0800783void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
784 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
785 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
786 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000787
kwiberg55b97fe2016-01-28 05:22:45 -0800788 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000789}
790
kwiberg55b97fe2016-01-28 05:22:45 -0800791void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
792 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
793 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
794 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000795
kwiberg55b97fe2016-01-28 05:22:45 -0800796 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000797}
798
kwiberg55b97fe2016-01-28 05:22:45 -0800799void Channel::PlayFileEnded(int32_t id) {
800 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
801 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000802
kwiberg55b97fe2016-01-28 05:22:45 -0800803 if (id == _inputFilePlayerId) {
804 channel_state_.SetInputFilePlaying(false);
805 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
806 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000807 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800808 } else if (id == _outputFilePlayerId) {
809 channel_state_.SetOutputFilePlaying(false);
810 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
811 "Channel::PlayFileEnded() => output file player module is"
812 " shutdown");
813 }
814}
815
816void Channel::RecordFileEnded(int32_t id) {
817 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
818 "Channel::RecordFileEnded(id=%d)", id);
819
820 assert(id == _outputFileRecorderId);
821
822 rtc::CritScope cs(&_fileCritSect);
823
824 _outputFileRecording = false;
825 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
826 "Channel::RecordFileEnded() => output file recorder module is"
827 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000828}
829
pbos@webrtc.org92135212013-05-14 08:31:39 +0000830Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000831 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700832 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800833 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100834 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700835 event_log_proxy_(new RtcEventLogProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100836 rtp_header_parser_(RtpHeaderParser::Create()),
837 rtp_payload_registry_(
838 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
839 rtp_receive_statistics_(
840 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
841 rtp_receiver_(
842 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100843 this,
844 this,
845 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700846 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100847 _outputAudioLevel(),
848 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100849 // Avoid conflict with other channels by adding 1024 - 1026,
850 // won't use as much as 1024 channels.
851 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
852 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
853 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
854 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100855 _outputExternalMedia(false),
856 _inputExternalMediaCallbackPtr(NULL),
857 _outputExternalMediaCallbackPtr(NULL),
858 _timeStamp(0), // This is just an offset, RTP module will add it's own
859 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100860 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100861 playout_timestamp_rtp_(0),
862 playout_timestamp_rtcp_(0),
863 playout_delay_ms_(0),
864 _numberOfDiscardedPackets(0),
865 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100866 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
867 capture_start_rtp_time_stamp_(-1),
868 capture_start_ntp_time_ms_(-1),
869 _engineStatisticsPtr(NULL),
870 _outputMixerPtr(NULL),
871 _transmitMixerPtr(NULL),
872 _moduleProcessThreadPtr(NULL),
873 _audioDeviceModulePtr(NULL),
874 _voiceEngineObserverPtr(NULL),
875 _callbackCritSectPtr(NULL),
876 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100877 _sendFrameType(0),
878 _externalMixing(false),
879 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700880 input_mute_(false),
881 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100882 _panLeft(1.0f),
883 _panRight(1.0f),
884 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100885 _lastLocalTimeStamp(0),
886 _lastPayloadType(0),
887 _includeAudioLevelIndication(false),
888 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100889 restored_packet_in_use_(false),
890 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100891 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700892 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800893 feedback_observer_proxy_(new TransportFeedbackProxy()),
894 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700895 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200896 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
897 kMaxRetransmissionWindowMs)),
solenberg88499ec2016-09-07 07:34:41 -0700898 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800899 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
900 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700901 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800902 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700903 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800904 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200905
kwiberg55b97fe2016-01-28 05:22:45 -0800906 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000907
kwiberg55b97fe2016-01-28 05:22:45 -0800908 RtpRtcp::Configuration configuration;
909 configuration.audio = true;
910 configuration.outgoing_transport = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800911 configuration.receive_statistics = rtp_receive_statistics_.get();
912 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800913 if (pacing_enabled_) {
914 configuration.paced_sender = rtp_packet_sender_proxy_.get();
915 configuration.transport_sequence_number_allocator =
916 seq_num_allocator_proxy_.get();
917 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
918 }
ivoc14d5dbe2016-07-04 07:06:55 -0700919 configuration.event_log = &(*event_log_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200920 configuration.retransmission_rate_limiter =
921 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000922
kwiberg55b97fe2016-01-28 05:22:45 -0800923 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100924 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000925
kwiberg55b97fe2016-01-28 05:22:45 -0800926 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
927 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
928 statistics_proxy_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000929}
930
kwiberg55b97fe2016-01-28 05:22:45 -0800931Channel::~Channel() {
932 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
933 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
934 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000935
kwiberg55b97fe2016-01-28 05:22:45 -0800936 if (_outputExternalMedia) {
937 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
938 }
939 if (channel_state_.Get().input_external_media) {
940 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
941 }
942 StopSend();
943 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000944
kwiberg55b97fe2016-01-28 05:22:45 -0800945 {
946 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700947 if (input_file_player_) {
948 input_file_player_->RegisterModuleFileCallback(NULL);
949 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000950 }
kwiberg5a25d952016-08-17 07:31:12 -0700951 if (output_file_player_) {
952 output_file_player_->RegisterModuleFileCallback(NULL);
953 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800954 }
kwiberg5a25d952016-08-17 07:31:12 -0700955 if (output_file_recorder_) {
956 output_file_recorder_->RegisterModuleFileCallback(NULL);
957 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800958 }
959 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000960
kwiberg55b97fe2016-01-28 05:22:45 -0800961 // The order to safely shutdown modules in a channel is:
962 // 1. De-register callbacks in modules
963 // 2. De-register modules in process thread
964 // 3. Destroy modules
965 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
966 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
967 "~Channel() failed to de-register transport callback"
968 " (Audio coding module)");
969 }
970 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
971 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
972 "~Channel() failed to de-register VAD callback"
973 " (Audio coding module)");
974 }
975 // De-register modules in process thread
976 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000977
kwiberg55b97fe2016-01-28 05:22:45 -0800978 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000979}
980
kwiberg55b97fe2016-01-28 05:22:45 -0800981int32_t Channel::Init() {
982 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
983 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000984
kwiberg55b97fe2016-01-28 05:22:45 -0800985 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000986
kwiberg55b97fe2016-01-28 05:22:45 -0800987 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000988
kwiberg55b97fe2016-01-28 05:22:45 -0800989 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
990 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
991 "Channel::Init() must call SetEngineInformation() first");
992 return -1;
993 }
994
995 // --- Add modules to process thread (for periodic schedulation)
996
997 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
998
999 // --- ACM initialization
1000
1001 if (audio_coding_->InitializeReceiver() == -1) {
1002 _engineStatisticsPtr->SetLastError(
1003 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1004 "Channel::Init() unable to initialize the ACM - 1");
1005 return -1;
1006 }
1007
1008 // --- RTP/RTCP module initialization
1009
1010 // Ensure that RTCP is enabled by default for the created channel.
1011 // Note that, the module will keep generating RTCP until it is explicitly
1012 // disabled by the user.
1013 // After StopListen (when no sockets exists), RTCP packets will no longer
1014 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001015 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001016 // RTCP is enabled by default.
1017 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1018 // --- Register all permanent callbacks
1019 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1020 (audio_coding_->RegisterVADCallback(this) == -1);
1021
1022 if (fail) {
1023 _engineStatisticsPtr->SetLastError(
1024 VE_CANNOT_INIT_CHANNEL, kTraceError,
1025 "Channel::Init() callbacks not registered");
1026 return -1;
1027 }
1028
1029 // --- Register all supported codecs to the receiving side of the
1030 // RTP/RTCP module
1031
1032 CodecInst codec;
1033 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1034
1035 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1036 // Open up the RTP/RTCP receiver for all supported codecs
1037 if ((audio_coding_->Codec(idx, &codec) == -1) ||
1038 (rtp_receiver_->RegisterReceivePayload(
1039 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1040 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
1041 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1042 "Channel::Init() unable to register %s "
1043 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1044 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1045 codec.rate);
1046 } else {
1047 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1048 "Channel::Init() %s (%d/%d/%" PRIuS
1049 "/%d) has been "
1050 "added to the RTP/RTCP receiver",
1051 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1052 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001053 }
1054
kwiberg55b97fe2016-01-28 05:22:45 -08001055 // Ensure that PCMU is used as default codec on the sending side
1056 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1057 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001058 }
1059
kwiberg55b97fe2016-01-28 05:22:45 -08001060 // Register default PT for outband 'telephone-event'
1061 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001062 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001063 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1064 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001065 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1066 "Channel::Init() failed to register outband "
1067 "'telephone-event' (%d/%d) correctly",
1068 codec.pltype, codec.plfreq);
1069 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001070 }
1071
kwiberg55b97fe2016-01-28 05:22:45 -08001072 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001073 if (!codec_manager_.RegisterEncoder(codec) ||
1074 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001075 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1076 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001077 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001078 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1079 "Channel::Init() failed to register CN (%d/%d) "
1080 "correctly - 1",
1081 codec.pltype, codec.plfreq);
1082 }
1083 }
kwiberg55b97fe2016-01-28 05:22:45 -08001084 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001085
kwiberg55b97fe2016-01-28 05:22:45 -08001086 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001087}
1088
kwiberg55b97fe2016-01-28 05:22:45 -08001089int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1090 OutputMixer& outputMixer,
1091 voe::TransmitMixer& transmitMixer,
1092 ProcessThread& moduleProcessThread,
1093 AudioDeviceModule& audioDeviceModule,
1094 VoiceEngineObserver* voiceEngineObserver,
1095 rtc::CriticalSection* callbackCritSect) {
1096 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1097 "Channel::SetEngineInformation()");
1098 _engineStatisticsPtr = &engineStatistics;
1099 _outputMixerPtr = &outputMixer;
1100 _transmitMixerPtr = &transmitMixer,
1101 _moduleProcessThreadPtr = &moduleProcessThread;
1102 _audioDeviceModulePtr = &audioDeviceModule;
1103 _voiceEngineObserverPtr = voiceEngineObserver;
1104 _callbackCritSectPtr = callbackCritSect;
1105 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001106}
1107
kwiberg55b97fe2016-01-28 05:22:45 -08001108int32_t Channel::UpdateLocalTimeStamp() {
1109 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1110 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001111}
1112
kwibergb7f89d62016-02-17 10:04:18 -08001113void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001114 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001115 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001116}
1117
ossu29b1a8d2016-06-13 07:34:51 -07001118const rtc::scoped_refptr<AudioDecoderFactory>&
1119Channel::GetAudioDecoderFactory() const {
1120 return decoder_factory_;
1121}
1122
kwiberg55b97fe2016-01-28 05:22:45 -08001123int32_t Channel::StartPlayout() {
1124 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1125 "Channel::StartPlayout()");
1126 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001127 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001128 }
1129
1130 if (!_externalMixing) {
1131 // Add participant as candidates for mixing.
1132 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1133 _engineStatisticsPtr->SetLastError(
1134 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1135 "StartPlayout() failed to add participant to mixer");
1136 return -1;
1137 }
1138 }
1139
1140 channel_state_.SetPlaying(true);
1141 if (RegisterFilePlayingToMixer() != 0)
1142 return -1;
1143
1144 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001145}
1146
kwiberg55b97fe2016-01-28 05:22:45 -08001147int32_t Channel::StopPlayout() {
1148 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1149 "Channel::StopPlayout()");
1150 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001151 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001152 }
1153
1154 if (!_externalMixing) {
1155 // Remove participant as candidates for mixing
1156 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1157 _engineStatisticsPtr->SetLastError(
1158 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1159 "StopPlayout() failed to remove participant from mixer");
1160 return -1;
1161 }
1162 }
1163
1164 channel_state_.SetPlaying(false);
1165 _outputAudioLevel.Clear();
1166
1167 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001168}
1169
kwiberg55b97fe2016-01-28 05:22:45 -08001170int32_t Channel::StartSend() {
1171 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1172 "Channel::StartSend()");
1173 // Resume the previous sequence number which was reset by StopSend().
1174 // This needs to be done before |sending| is set to true.
1175 if (send_sequence_number_)
1176 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001177
kwiberg55b97fe2016-01-28 05:22:45 -08001178 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001179 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001180 }
1181 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001182
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001183 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001184 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1185 _engineStatisticsPtr->SetLastError(
1186 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1187 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001188 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001189 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001190 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001191 return -1;
1192 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001193
kwiberg55b97fe2016-01-28 05:22:45 -08001194 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001195}
1196
kwiberg55b97fe2016-01-28 05:22:45 -08001197int32_t Channel::StopSend() {
1198 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1199 "Channel::StopSend()");
1200 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001201 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001202 }
1203 channel_state_.SetSending(false);
1204
1205 // Store the sequence number to be able to pick up the same sequence for
1206 // the next StartSend(). This is needed for restarting device, otherwise
1207 // it might cause libSRTP to complain about packets being replayed.
1208 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1209 // CL is landed. See issue
1210 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1211 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1212
1213 // Reset sending SSRC and sequence number and triggers direct transmission
1214 // of RTCP BYE
1215 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1216 _engineStatisticsPtr->SetLastError(
1217 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1218 "StartSend() RTP/RTCP failed to stop sending");
1219 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001220 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001221
1222 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001223}
1224
solenberge566ac72016-10-31 12:52:33 -07001225void Channel::ResetDiscardedPacketCount() {
kwiberg55b97fe2016-01-28 05:22:45 -08001226 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberge566ac72016-10-31 12:52:33 -07001227 "Channel::ResetDiscardedPacketCount()");
kwiberg55b97fe2016-01-28 05:22:45 -08001228 _numberOfDiscardedPackets = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001229}
1230
kwiberg55b97fe2016-01-28 05:22:45 -08001231int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1232 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1233 "Channel::RegisterVoiceEngineObserver()");
1234 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001235
kwiberg55b97fe2016-01-28 05:22:45 -08001236 if (_voiceEngineObserverPtr) {
1237 _engineStatisticsPtr->SetLastError(
1238 VE_INVALID_OPERATION, kTraceError,
1239 "RegisterVoiceEngineObserver() observer already enabled");
1240 return -1;
1241 }
1242 _voiceEngineObserverPtr = &observer;
1243 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001244}
1245
kwiberg55b97fe2016-01-28 05:22:45 -08001246int32_t Channel::DeRegisterVoiceEngineObserver() {
1247 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1248 "Channel::DeRegisterVoiceEngineObserver()");
1249 rtc::CritScope cs(&_callbackCritSect);
1250
1251 if (!_voiceEngineObserverPtr) {
1252 _engineStatisticsPtr->SetLastError(
1253 VE_INVALID_OPERATION, kTraceWarning,
1254 "DeRegisterVoiceEngineObserver() observer already disabled");
1255 return 0;
1256 }
1257 _voiceEngineObserverPtr = NULL;
1258 return 0;
1259}
1260
1261int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001262 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001263 if (send_codec) {
1264 codec = *send_codec;
1265 return 0;
1266 }
1267 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001268}
1269
kwiberg55b97fe2016-01-28 05:22:45 -08001270int32_t Channel::GetRecCodec(CodecInst& codec) {
1271 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001272}
1273
kwiberg55b97fe2016-01-28 05:22:45 -08001274int32_t Channel::SetSendCodec(const CodecInst& codec) {
1275 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1276 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001277
kwibergc8d071e2016-04-06 12:22:38 -07001278 if (!codec_manager_.RegisterEncoder(codec) ||
1279 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001280 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1281 "SetSendCodec() failed to register codec to ACM");
1282 return -1;
1283 }
1284
1285 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1286 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1287 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1288 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1289 "SetSendCodec() failed to register codec to"
1290 " RTP/RTCP module");
1291 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001292 }
kwiberg55b97fe2016-01-28 05:22:45 -08001293 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001294
kwiberg55b97fe2016-01-28 05:22:45 -08001295 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) {
1296 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1297 "SetSendCodec() failed to set audio packet size");
1298 return -1;
1299 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001300
kwiberg55b97fe2016-01-28 05:22:45 -08001301 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001302}
1303
Ivo Creusenadf89b72015-04-29 16:03:33 +02001304void Channel::SetBitRate(int bitrate_bps) {
1305 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1306 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001307 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1308 if (*encoder)
1309 (*encoder)->OnReceivedTargetAudioBitrate(bitrate_bps);
1310 });
Erik Språng737336d2016-07-29 12:59:36 +02001311 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001312}
1313
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001314void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001315 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1316 if (*encoder)
1317 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1318 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001319}
1320
kwiberg55b97fe2016-01-28 05:22:45 -08001321int32_t Channel::SetVADStatus(bool enableVAD,
1322 ACMVADMode mode,
1323 bool disableDTX) {
1324 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1325 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001326 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1327 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1328 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001329 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1330 kTraceError,
1331 "SetVADStatus() failed to set VAD");
1332 return -1;
1333 }
1334 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001335}
1336
kwiberg55b97fe2016-01-28 05:22:45 -08001337int32_t Channel::GetVADStatus(bool& enabledVAD,
1338 ACMVADMode& mode,
1339 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001340 const auto* params = codec_manager_.GetStackParams();
1341 enabledVAD = params->use_cng;
1342 mode = params->vad_mode;
1343 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001344 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001345}
1346
kwiberg55b97fe2016-01-28 05:22:45 -08001347int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1348 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1349 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001350
kwiberg55b97fe2016-01-28 05:22:45 -08001351 if (channel_state_.Get().playing) {
1352 _engineStatisticsPtr->SetLastError(
1353 VE_ALREADY_PLAYING, kTraceError,
1354 "SetRecPayloadType() unable to set PT while playing");
1355 return -1;
1356 }
kwiberg55b97fe2016-01-28 05:22:45 -08001357
1358 if (codec.pltype == -1) {
1359 // De-register the selected codec (RTP/RTCP module and ACM)
1360
1361 int8_t pltype(-1);
1362 CodecInst rxCodec = codec;
1363
1364 // Get payload type for the given codec
1365 rtp_payload_registry_->ReceivePayloadType(
1366 rxCodec.plname, rxCodec.plfreq, rxCodec.channels,
1367 (rxCodec.rate < 0) ? 0 : rxCodec.rate, &pltype);
1368 rxCodec.pltype = pltype;
1369
1370 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1371 _engineStatisticsPtr->SetLastError(
1372 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1373 "SetRecPayloadType() RTP/RTCP-module deregistration "
1374 "failed");
1375 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001376 }
kwiberg55b97fe2016-01-28 05:22:45 -08001377 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1378 _engineStatisticsPtr->SetLastError(
1379 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1380 "SetRecPayloadType() ACM deregistration failed - 1");
1381 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001382 }
kwiberg55b97fe2016-01-28 05:22:45 -08001383 return 0;
1384 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001385
kwiberg55b97fe2016-01-28 05:22:45 -08001386 if (rtp_receiver_->RegisterReceivePayload(
1387 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1388 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1389 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001390 // TODO(kwiberg): Retrying is probably not necessary, since
1391 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001392 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001393 if (rtp_receiver_->RegisterReceivePayload(
kwiberg55b97fe2016-01-28 05:22:45 -08001394 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1395 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1396 _engineStatisticsPtr->SetLastError(
1397 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1398 "SetRecPayloadType() RTP/RTCP-module registration failed");
1399 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001400 }
kwiberg55b97fe2016-01-28 05:22:45 -08001401 }
kwibergda2bf4e2016-10-24 13:47:09 -07001402 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1403 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001404 audio_coding_->UnregisterReceiveCodec(codec.pltype);
kwibergda2bf4e2016-10-24 13:47:09 -07001405 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1406 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001407 _engineStatisticsPtr->SetLastError(
1408 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1409 "SetRecPayloadType() ACM registration failed - 1");
1410 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001411 }
kwiberg55b97fe2016-01-28 05:22:45 -08001412 }
1413 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001414}
1415
kwiberg55b97fe2016-01-28 05:22:45 -08001416int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1417 int8_t payloadType(-1);
1418 if (rtp_payload_registry_->ReceivePayloadType(
1419 codec.plname, codec.plfreq, codec.channels,
1420 (codec.rate < 0) ? 0 : codec.rate, &payloadType) != 0) {
1421 _engineStatisticsPtr->SetLastError(
1422 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1423 "GetRecPayloadType() failed to retrieve RX payload type");
1424 return -1;
1425 }
1426 codec.pltype = payloadType;
1427 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001428}
1429
kwiberg55b97fe2016-01-28 05:22:45 -08001430int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1431 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1432 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001433
kwiberg55b97fe2016-01-28 05:22:45 -08001434 CodecInst codec;
1435 int32_t samplingFreqHz(-1);
1436 const size_t kMono = 1;
1437 if (frequency == kFreq32000Hz)
1438 samplingFreqHz = 32000;
1439 else if (frequency == kFreq16000Hz)
1440 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001441
kwiberg55b97fe2016-01-28 05:22:45 -08001442 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1443 _engineStatisticsPtr->SetLastError(
1444 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1445 "SetSendCNPayloadType() failed to retrieve default CN codec "
1446 "settings");
1447 return -1;
1448 }
1449
1450 // Modify the payload type (must be set to dynamic range)
1451 codec.pltype = type;
1452
kwibergc8d071e2016-04-06 12:22:38 -07001453 if (!codec_manager_.RegisterEncoder(codec) ||
1454 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001455 _engineStatisticsPtr->SetLastError(
1456 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1457 "SetSendCNPayloadType() failed to register CN to ACM");
1458 return -1;
1459 }
1460
1461 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1462 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1463 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1464 _engineStatisticsPtr->SetLastError(
1465 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1466 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1467 "module");
1468 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001469 }
kwiberg55b97fe2016-01-28 05:22:45 -08001470 }
1471 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001472}
1473
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001474int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001475 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001476 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001477
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001478 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001479 _engineStatisticsPtr->SetLastError(
1480 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001481 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001482 return -1;
1483 }
1484 return 0;
1485}
1486
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001487int Channel::SetOpusDtx(bool enable_dtx) {
1488 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1489 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001490 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001491 : audio_coding_->DisableOpusDtx();
1492 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001493 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1494 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001495 return -1;
1496 }
1497 return 0;
1498}
1499
ivoc85228d62016-07-27 04:53:47 -07001500int Channel::GetOpusDtx(bool* enabled) {
1501 int success = -1;
1502 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1503 if (encoder) {
1504 *enabled = encoder->GetDtx();
1505 success = 0;
1506 }
1507 });
1508 return success;
1509}
1510
minyue7e304322016-10-12 05:00:55 -07001511bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1512 bool success = false;
1513 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1514 if (*encoder) {
1515 success = (*encoder)->EnableAudioNetworkAdaptor(
1516 config_string, Clock::GetRealTimeClock());
1517 }
1518 });
1519 return success;
1520}
1521
1522void Channel::DisableAudioNetworkAdaptor() {
1523 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1524 if (*encoder)
1525 (*encoder)->DisableAudioNetworkAdaptor();
1526 });
1527}
1528
1529void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1530 int max_frame_length_ms) {
1531 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1532 if (*encoder) {
1533 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1534 max_frame_length_ms);
1535 }
1536 });
1537}
1538
mflodman3d7db262016-04-29 00:57:13 -07001539int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001540 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001541 "Channel::RegisterExternalTransport()");
1542
kwiberg55b97fe2016-01-28 05:22:45 -08001543 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001544 if (_externalTransport) {
1545 _engineStatisticsPtr->SetLastError(
1546 VE_INVALID_OPERATION, kTraceError,
1547 "RegisterExternalTransport() external transport already enabled");
1548 return -1;
1549 }
1550 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001551 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001552 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001553}
1554
kwiberg55b97fe2016-01-28 05:22:45 -08001555int32_t Channel::DeRegisterExternalTransport() {
1556 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1557 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001558
kwiberg55b97fe2016-01-28 05:22:45 -08001559 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001560 if (_transportPtr) {
1561 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1562 "DeRegisterExternalTransport() all transport is disabled");
1563 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001564 _engineStatisticsPtr->SetLastError(
1565 VE_INVALID_OPERATION, kTraceWarning,
1566 "DeRegisterExternalTransport() external transport already "
1567 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001568 }
1569 _externalTransport = false;
1570 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001571 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001572}
1573
mflodman3d7db262016-04-29 00:57:13 -07001574int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001575 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001576 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001577 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001578 "Channel::ReceivedRTPPacket()");
1579
1580 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001581 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001582
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001583 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001584 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1585 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1586 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001587 return -1;
1588 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001589 header.payload_type_frequency =
1590 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001591 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001592 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001593 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001594 rtp_receive_statistics_->IncomingPacket(
1595 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001596 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001597
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001598 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001599}
1600
1601bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001602 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001603 const RTPHeader& header,
1604 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001605 if (rtp_payload_registry_->IsRtx(header)) {
1606 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001607 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001608 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001609 assert(packet_length >= header.headerLength);
1610 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001611 PayloadUnion payload_specific;
1612 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001613 &payload_specific)) {
1614 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001615 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001616 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1617 payload_specific, in_order);
1618}
1619
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001620bool Channel::HandleRtxPacket(const uint8_t* packet,
1621 size_t packet_length,
1622 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001623 if (!rtp_payload_registry_->IsRtx(header))
1624 return false;
1625
1626 // Remove the RTX header and parse the original RTP header.
1627 if (packet_length < header.headerLength)
1628 return false;
1629 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1630 return false;
1631 if (restored_packet_in_use_) {
1632 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1633 "Multiple RTX headers detected, dropping packet");
1634 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001635 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001636 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001637 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1638 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001639 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1640 "Incoming RTX packet: invalid RTP header");
1641 return false;
1642 }
1643 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001644 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001645 restored_packet_in_use_ = false;
1646 return ret;
1647}
1648
1649bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1650 StreamStatistician* statistician =
1651 rtp_receive_statistics_->GetStatistician(header.ssrc);
1652 if (!statistician)
1653 return false;
1654 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001655}
1656
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001657bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1658 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001659 // Retransmissions are handled separately if RTX is enabled.
1660 if (rtp_payload_registry_->RtxEnabled())
1661 return false;
1662 StreamStatistician* statistician =
1663 rtp_receive_statistics_->GetStatistician(header.ssrc);
1664 if (!statistician)
1665 return false;
1666 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001667 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001668 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001669 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001670}
1671
mflodman3d7db262016-04-29 00:57:13 -07001672int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001673 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001674 "Channel::ReceivedRTCPPacket()");
1675 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001676 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001677
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001678 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001679 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001680 _engineStatisticsPtr->SetLastError(
1681 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1682 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1683 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001684
Minyue2013aec2015-05-13 14:14:42 +02001685 int64_t rtt = GetRTT(true);
1686 if (rtt == 0) {
1687 // Waiting for valid RTT.
1688 return 0;
1689 }
Erik Språng737336d2016-07-29 12:59:36 +02001690
1691 int64_t nack_window_ms = rtt;
1692 if (nack_window_ms < kMinRetransmissionWindowMs) {
1693 nack_window_ms = kMinRetransmissionWindowMs;
1694 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1695 nack_window_ms = kMaxRetransmissionWindowMs;
1696 }
1697 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1698
minyue7e304322016-10-12 05:00:55 -07001699 // Invoke audio encoders OnReceivedRtt().
1700 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1701 if (*encoder)
1702 (*encoder)->OnReceivedRtt(rtt);
1703 });
1704
Minyue2013aec2015-05-13 14:14:42 +02001705 uint32_t ntp_secs = 0;
1706 uint32_t ntp_frac = 0;
1707 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001708 if (0 !=
1709 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1710 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001711 // Waiting for RTCP.
1712 return 0;
1713 }
1714
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001715 {
tommi31fc21f2016-01-21 10:37:37 -08001716 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001717 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001718 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001719 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001720}
1721
niklase@google.com470e71d2011-07-07 08:21:25 +00001722int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001723 bool loop,
1724 FileFormats format,
1725 int startPosition,
1726 float volumeScaling,
1727 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001728 const CodecInst* codecInst) {
1729 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1730 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1731 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1732 "stopPosition=%d)",
1733 fileName, loop, format, volumeScaling, startPosition,
1734 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001735
kwiberg55b97fe2016-01-28 05:22:45 -08001736 if (channel_state_.Get().output_file_playing) {
1737 _engineStatisticsPtr->SetLastError(
1738 VE_ALREADY_PLAYING, kTraceError,
1739 "StartPlayingFileLocally() is already playing");
1740 return -1;
1741 }
1742
1743 {
1744 rtc::CritScope cs(&_fileCritSect);
1745
kwiberg5a25d952016-08-17 07:31:12 -07001746 if (output_file_player_) {
1747 output_file_player_->RegisterModuleFileCallback(NULL);
1748 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001749 }
1750
kwiberg5b356f42016-09-08 04:32:33 -07001751 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001752 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001753
kwiberg5a25d952016-08-17 07:31:12 -07001754 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001755 _engineStatisticsPtr->SetLastError(
1756 VE_INVALID_ARGUMENT, kTraceError,
1757 "StartPlayingFileLocally() filePlayer format is not correct");
1758 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001759 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001760
kwiberg55b97fe2016-01-28 05:22:45 -08001761 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001762
kwiberg5a25d952016-08-17 07:31:12 -07001763 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001764 fileName, loop, startPosition, volumeScaling, notificationTime,
1765 stopPosition, (const CodecInst*)codecInst) != 0) {
1766 _engineStatisticsPtr->SetLastError(
1767 VE_BAD_FILE, kTraceError,
1768 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001769 output_file_player_->StopPlayingFile();
1770 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001771 return -1;
1772 }
kwiberg5a25d952016-08-17 07:31:12 -07001773 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001774 channel_state_.SetOutputFilePlaying(true);
1775 }
1776
1777 if (RegisterFilePlayingToMixer() != 0)
1778 return -1;
1779
1780 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001781}
1782
1783int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001784 FileFormats format,
1785 int startPosition,
1786 float volumeScaling,
1787 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001788 const CodecInst* codecInst) {
1789 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1790 "Channel::StartPlayingFileLocally(format=%d,"
1791 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1792 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001793
kwiberg55b97fe2016-01-28 05:22:45 -08001794 if (stream == NULL) {
1795 _engineStatisticsPtr->SetLastError(
1796 VE_BAD_FILE, kTraceError,
1797 "StartPlayingFileLocally() NULL as input stream");
1798 return -1;
1799 }
1800
1801 if (channel_state_.Get().output_file_playing) {
1802 _engineStatisticsPtr->SetLastError(
1803 VE_ALREADY_PLAYING, kTraceError,
1804 "StartPlayingFileLocally() is already playing");
1805 return -1;
1806 }
1807
1808 {
1809 rtc::CritScope cs(&_fileCritSect);
1810
1811 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001812 if (output_file_player_) {
1813 output_file_player_->RegisterModuleFileCallback(NULL);
1814 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001815 }
1816
kwiberg55b97fe2016-01-28 05:22:45 -08001817 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001818 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001819 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001820
kwiberg5a25d952016-08-17 07:31:12 -07001821 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001822 _engineStatisticsPtr->SetLastError(
1823 VE_INVALID_ARGUMENT, kTraceError,
1824 "StartPlayingFileLocally() filePlayer format isnot correct");
1825 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001826 }
1827
kwiberg55b97fe2016-01-28 05:22:45 -08001828 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001829
kwiberg4ec01d92016-08-22 08:43:54 -07001830 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001831 volumeScaling, notificationTime,
1832 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001833 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1834 "StartPlayingFile() failed to "
1835 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001836 output_file_player_->StopPlayingFile();
1837 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001838 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001839 }
kwiberg5a25d952016-08-17 07:31:12 -07001840 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001841 channel_state_.SetOutputFilePlaying(true);
1842 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001843
kwiberg55b97fe2016-01-28 05:22:45 -08001844 if (RegisterFilePlayingToMixer() != 0)
1845 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001846
kwiberg55b97fe2016-01-28 05:22:45 -08001847 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001848}
1849
kwiberg55b97fe2016-01-28 05:22:45 -08001850int Channel::StopPlayingFileLocally() {
1851 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1852 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001853
kwiberg55b97fe2016-01-28 05:22:45 -08001854 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001855 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001856 }
1857
1858 {
1859 rtc::CritScope cs(&_fileCritSect);
1860
kwiberg5a25d952016-08-17 07:31:12 -07001861 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001862 _engineStatisticsPtr->SetLastError(
1863 VE_STOP_RECORDING_FAILED, kTraceError,
1864 "StopPlayingFile() could not stop playing");
1865 return -1;
1866 }
kwiberg5a25d952016-08-17 07:31:12 -07001867 output_file_player_->RegisterModuleFileCallback(NULL);
1868 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001869 channel_state_.SetOutputFilePlaying(false);
1870 }
1871 // _fileCritSect cannot be taken while calling
1872 // SetAnonymousMixibilityStatus. Refer to comments in
1873 // StartPlayingFileLocally(const char* ...) for more details.
1874 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1875 _engineStatisticsPtr->SetLastError(
1876 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1877 "StopPlayingFile() failed to stop participant from playing as"
1878 "file in the mixer");
1879 return -1;
1880 }
1881
1882 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001883}
1884
kwiberg55b97fe2016-01-28 05:22:45 -08001885int Channel::IsPlayingFileLocally() const {
1886 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001887}
1888
kwiberg55b97fe2016-01-28 05:22:45 -08001889int Channel::RegisterFilePlayingToMixer() {
1890 // Return success for not registering for file playing to mixer if:
1891 // 1. playing file before playout is started on that channel.
1892 // 2. starting playout without file playing on that channel.
1893 if (!channel_state_.Get().playing ||
1894 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001895 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001896 }
1897
1898 // |_fileCritSect| cannot be taken while calling
1899 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1900 // frames can be pulled by the mixer. Since the frames are generated from
1901 // the file, _fileCritSect will be taken. This would result in a deadlock.
1902 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1903 channel_state_.SetOutputFilePlaying(false);
1904 rtc::CritScope cs(&_fileCritSect);
1905 _engineStatisticsPtr->SetLastError(
1906 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1907 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001908 output_file_player_->StopPlayingFile();
1909 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001910 return -1;
1911 }
1912
1913 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001914}
1915
niklase@google.com470e71d2011-07-07 08:21:25 +00001916int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001917 bool loop,
1918 FileFormats format,
1919 int startPosition,
1920 float volumeScaling,
1921 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001922 const CodecInst* codecInst) {
1923 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1924 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1925 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1926 "stopPosition=%d)",
1927 fileName, loop, format, volumeScaling, startPosition,
1928 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001929
kwiberg55b97fe2016-01-28 05:22:45 -08001930 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001931
kwiberg55b97fe2016-01-28 05:22:45 -08001932 if (channel_state_.Get().input_file_playing) {
1933 _engineStatisticsPtr->SetLastError(
1934 VE_ALREADY_PLAYING, kTraceWarning,
1935 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001936 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001937 }
1938
1939 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001940 if (input_file_player_) {
1941 input_file_player_->RegisterModuleFileCallback(NULL);
1942 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001943 }
1944
1945 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001946 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001947 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001948
kwiberg5a25d952016-08-17 07:31:12 -07001949 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001950 _engineStatisticsPtr->SetLastError(
1951 VE_INVALID_ARGUMENT, kTraceError,
1952 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1953 return -1;
1954 }
1955
1956 const uint32_t notificationTime(0);
1957
kwiberg5a25d952016-08-17 07:31:12 -07001958 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001959 fileName, loop, startPosition, volumeScaling, notificationTime,
1960 stopPosition, (const CodecInst*)codecInst) != 0) {
1961 _engineStatisticsPtr->SetLastError(
1962 VE_BAD_FILE, kTraceError,
1963 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001964 input_file_player_->StopPlayingFile();
1965 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001966 return -1;
1967 }
kwiberg5a25d952016-08-17 07:31:12 -07001968 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001969 channel_state_.SetInputFilePlaying(true);
1970
1971 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001972}
1973
1974int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001975 FileFormats format,
1976 int startPosition,
1977 float volumeScaling,
1978 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001979 const CodecInst* codecInst) {
1980 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1981 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1982 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1983 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001984
kwiberg55b97fe2016-01-28 05:22:45 -08001985 if (stream == NULL) {
1986 _engineStatisticsPtr->SetLastError(
1987 VE_BAD_FILE, kTraceError,
1988 "StartPlayingFileAsMicrophone NULL as input stream");
1989 return -1;
1990 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001991
kwiberg55b97fe2016-01-28 05:22:45 -08001992 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001993
kwiberg55b97fe2016-01-28 05:22:45 -08001994 if (channel_state_.Get().input_file_playing) {
1995 _engineStatisticsPtr->SetLastError(
1996 VE_ALREADY_PLAYING, kTraceWarning,
1997 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001998 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001999 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002000
kwiberg55b97fe2016-01-28 05:22:45 -08002001 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002002 if (input_file_player_) {
2003 input_file_player_->RegisterModuleFileCallback(NULL);
2004 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002005 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002006
kwiberg55b97fe2016-01-28 05:22:45 -08002007 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002008 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002009 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002010
kwiberg5a25d952016-08-17 07:31:12 -07002011 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002012 _engineStatisticsPtr->SetLastError(
2013 VE_INVALID_ARGUMENT, kTraceError,
2014 "StartPlayingInputFile() filePlayer format isnot correct");
2015 return -1;
2016 }
2017
2018 const uint32_t notificationTime(0);
2019
kwiberg4ec01d92016-08-22 08:43:54 -07002020 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2021 notificationTime, stopPosition,
2022 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002023 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2024 "StartPlayingFile() failed to start "
2025 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002026 input_file_player_->StopPlayingFile();
2027 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002028 return -1;
2029 }
2030
kwiberg5a25d952016-08-17 07:31:12 -07002031 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002032 channel_state_.SetInputFilePlaying(true);
2033
2034 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002035}
2036
kwiberg55b97fe2016-01-28 05:22:45 -08002037int Channel::StopPlayingFileAsMicrophone() {
2038 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2039 "Channel::StopPlayingFileAsMicrophone()");
2040
2041 rtc::CritScope cs(&_fileCritSect);
2042
2043 if (!channel_state_.Get().input_file_playing) {
2044 return 0;
2045 }
2046
kwiberg5a25d952016-08-17 07:31:12 -07002047 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002048 _engineStatisticsPtr->SetLastError(
2049 VE_STOP_RECORDING_FAILED, kTraceError,
2050 "StopPlayingFile() could not stop playing");
2051 return -1;
2052 }
kwiberg5a25d952016-08-17 07:31:12 -07002053 input_file_player_->RegisterModuleFileCallback(NULL);
2054 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002055 channel_state_.SetInputFilePlaying(false);
2056
2057 return 0;
2058}
2059
2060int Channel::IsPlayingFileAsMicrophone() const {
2061 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002062}
2063
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002064int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002065 const CodecInst* codecInst) {
2066 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2067 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002068
kwiberg55b97fe2016-01-28 05:22:45 -08002069 if (_outputFileRecording) {
2070 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2071 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002072 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002073 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002074
kwiberg55b97fe2016-01-28 05:22:45 -08002075 FileFormats format;
2076 const uint32_t notificationTime(0); // Not supported in VoE
2077 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002078
kwiberg55b97fe2016-01-28 05:22:45 -08002079 if ((codecInst != NULL) &&
2080 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2081 _engineStatisticsPtr->SetLastError(
2082 VE_BAD_ARGUMENT, kTraceError,
2083 "StartRecordingPlayout() invalid compression");
2084 return (-1);
2085 }
2086 if (codecInst == NULL) {
2087 format = kFileFormatPcm16kHzFile;
2088 codecInst = &dummyCodec;
2089 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2090 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2091 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2092 format = kFileFormatWavFile;
2093 } else {
2094 format = kFileFormatCompressedFile;
2095 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002096
kwiberg55b97fe2016-01-28 05:22:45 -08002097 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002098
kwiberg55b97fe2016-01-28 05:22:45 -08002099 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002100 if (output_file_recorder_) {
2101 output_file_recorder_->RegisterModuleFileCallback(NULL);
2102 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002103 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002104
kwiberg5a25d952016-08-17 07:31:12 -07002105 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002106 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002107 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002108 _engineStatisticsPtr->SetLastError(
2109 VE_INVALID_ARGUMENT, kTraceError,
2110 "StartRecordingPlayout() fileRecorder format isnot correct");
2111 return -1;
2112 }
2113
kwiberg5a25d952016-08-17 07:31:12 -07002114 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002115 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2116 _engineStatisticsPtr->SetLastError(
2117 VE_BAD_FILE, kTraceError,
2118 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002119 output_file_recorder_->StopRecording();
2120 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002121 return -1;
2122 }
kwiberg5a25d952016-08-17 07:31:12 -07002123 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002124 _outputFileRecording = true;
2125
2126 return 0;
2127}
2128
2129int Channel::StartRecordingPlayout(OutStream* stream,
2130 const CodecInst* codecInst) {
2131 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2132 "Channel::StartRecordingPlayout()");
2133
2134 if (_outputFileRecording) {
2135 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2136 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002137 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002138 }
2139
2140 FileFormats format;
2141 const uint32_t notificationTime(0); // Not supported in VoE
2142 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2143
2144 if (codecInst != NULL && codecInst->channels != 1) {
2145 _engineStatisticsPtr->SetLastError(
2146 VE_BAD_ARGUMENT, kTraceError,
2147 "StartRecordingPlayout() invalid compression");
2148 return (-1);
2149 }
2150 if (codecInst == NULL) {
2151 format = kFileFormatPcm16kHzFile;
2152 codecInst = &dummyCodec;
2153 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2154 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2155 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2156 format = kFileFormatWavFile;
2157 } else {
2158 format = kFileFormatCompressedFile;
2159 }
2160
2161 rtc::CritScope cs(&_fileCritSect);
2162
2163 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002164 if (output_file_recorder_) {
2165 output_file_recorder_->RegisterModuleFileCallback(NULL);
2166 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002167 }
2168
kwiberg5a25d952016-08-17 07:31:12 -07002169 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002170 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002171 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002172 _engineStatisticsPtr->SetLastError(
2173 VE_INVALID_ARGUMENT, kTraceError,
2174 "StartRecordingPlayout() fileRecorder format isnot correct");
2175 return -1;
2176 }
2177
kwiberg4ec01d92016-08-22 08:43:54 -07002178 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002179 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002180 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2181 "StartRecordingPlayout() failed to "
2182 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002183 output_file_recorder_->StopRecording();
2184 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002185 return -1;
2186 }
2187
kwiberg5a25d952016-08-17 07:31:12 -07002188 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002189 _outputFileRecording = true;
2190
2191 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002192}
2193
kwiberg55b97fe2016-01-28 05:22:45 -08002194int Channel::StopRecordingPlayout() {
2195 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2196 "Channel::StopRecordingPlayout()");
2197
2198 if (!_outputFileRecording) {
2199 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2200 "StopRecordingPlayout() isnot recording");
2201 return -1;
2202 }
2203
2204 rtc::CritScope cs(&_fileCritSect);
2205
kwiberg5a25d952016-08-17 07:31:12 -07002206 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002207 _engineStatisticsPtr->SetLastError(
2208 VE_STOP_RECORDING_FAILED, kTraceError,
2209 "StopRecording() could not stop recording");
2210 return (-1);
2211 }
kwiberg5a25d952016-08-17 07:31:12 -07002212 output_file_recorder_->RegisterModuleFileCallback(NULL);
2213 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002214 _outputFileRecording = false;
2215
2216 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002217}
2218
kwiberg55b97fe2016-01-28 05:22:45 -08002219void Channel::SetMixWithMicStatus(bool mix) {
2220 rtc::CritScope cs(&_fileCritSect);
2221 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002222}
2223
kwiberg55b97fe2016-01-28 05:22:45 -08002224int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2225 int8_t currentLevel = _outputAudioLevel.Level();
2226 level = static_cast<int32_t>(currentLevel);
2227 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002228}
2229
kwiberg55b97fe2016-01-28 05:22:45 -08002230int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2231 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2232 level = static_cast<int32_t>(currentLevel);
2233 return 0;
2234}
2235
solenberg1c2af8e2016-03-24 10:36:00 -07002236int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002237 rtc::CritScope cs(&volume_settings_critsect_);
2238 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002239 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002240 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002241 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002242}
2243
solenberg1c2af8e2016-03-24 10:36:00 -07002244bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002245 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002246 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002247}
2248
kwiberg55b97fe2016-01-28 05:22:45 -08002249int Channel::SetOutputVolumePan(float left, float right) {
2250 rtc::CritScope cs(&volume_settings_critsect_);
2251 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002252 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002253 _panLeft = left;
2254 _panRight = right;
2255 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002256}
2257
kwiberg55b97fe2016-01-28 05:22:45 -08002258int Channel::GetOutputVolumePan(float& left, float& right) const {
2259 rtc::CritScope cs(&volume_settings_critsect_);
2260 left = _panLeft;
2261 right = _panRight;
2262 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002263}
2264
kwiberg55b97fe2016-01-28 05:22:45 -08002265int Channel::SetChannelOutputVolumeScaling(float scaling) {
2266 rtc::CritScope cs(&volume_settings_critsect_);
2267 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002268 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002269 _outputGain = scaling;
2270 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002271}
2272
kwiberg55b97fe2016-01-28 05:22:45 -08002273int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2274 rtc::CritScope cs(&volume_settings_critsect_);
2275 scaling = _outputGain;
2276 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002277}
2278
solenberg8842c3e2016-03-11 03:06:41 -08002279int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002280 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002281 "Channel::SendTelephoneEventOutband(...)");
2282 RTC_DCHECK_LE(0, event);
2283 RTC_DCHECK_GE(255, event);
2284 RTC_DCHECK_LE(0, duration_ms);
2285 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002286 if (!Sending()) {
2287 return -1;
2288 }
solenberg8842c3e2016-03-11 03:06:41 -08002289 if (_rtpRtcpModule->SendTelephoneEventOutband(
2290 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002291 _engineStatisticsPtr->SetLastError(
2292 VE_SEND_DTMF_FAILED, kTraceWarning,
2293 "SendTelephoneEventOutband() failed to send event");
2294 return -1;
2295 }
2296 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002297}
2298
solenberg31642aa2016-03-14 08:00:37 -07002299int Channel::SetSendTelephoneEventPayloadType(int payload_type) {
kwiberg55b97fe2016-01-28 05:22:45 -08002300 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002301 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002302 RTC_DCHECK_LE(0, payload_type);
2303 RTC_DCHECK_GE(127, payload_type);
2304 CodecInst codec = {0};
kwiberg55b97fe2016-01-28 05:22:45 -08002305 codec.plfreq = 8000;
solenberg31642aa2016-03-14 08:00:37 -07002306 codec.pltype = payload_type;
kwiberg55b97fe2016-01-28 05:22:45 -08002307 memcpy(codec.plname, "telephone-event", 16);
2308 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2309 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2310 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2311 _engineStatisticsPtr->SetLastError(
2312 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2313 "SetSendTelephoneEventPayloadType() failed to register send"
2314 "payload type");
2315 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002316 }
kwiberg55b97fe2016-01-28 05:22:45 -08002317 }
kwiberg55b97fe2016-01-28 05:22:45 -08002318 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002319}
2320
kwiberg55b97fe2016-01-28 05:22:45 -08002321int Channel::VoiceActivityIndicator(int& activity) {
2322 activity = _sendFrameType;
2323 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002324}
2325
kwiberg55b97fe2016-01-28 05:22:45 -08002326int Channel::SetLocalSSRC(unsigned int ssrc) {
2327 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2328 "Channel::SetLocalSSRC()");
2329 if (channel_state_.Get().sending) {
2330 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2331 "SetLocalSSRC() already sending");
2332 return -1;
2333 }
2334 _rtpRtcpModule->SetSSRC(ssrc);
2335 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002336}
2337
kwiberg55b97fe2016-01-28 05:22:45 -08002338int Channel::GetLocalSSRC(unsigned int& ssrc) {
2339 ssrc = _rtpRtcpModule->SSRC();
2340 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002341}
2342
kwiberg55b97fe2016-01-28 05:22:45 -08002343int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2344 ssrc = rtp_receiver_->SSRC();
2345 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002346}
2347
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002348int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002349 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002350 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002351}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002352
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002353int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2354 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002355 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2356 if (enable &&
2357 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2358 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002359 return -1;
2360 }
2361 return 0;
2362}
2363
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002364void Channel::EnableSendTransportSequenceNumber(int id) {
2365 int ret =
2366 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2367 RTC_DCHECK_EQ(0, ret);
2368}
2369
stefan3313ec92016-01-21 06:32:43 -08002370void Channel::EnableReceiveTransportSequenceNumber(int id) {
2371 rtp_header_parser_->DeregisterRtpHeaderExtension(
2372 kRtpExtensionTransportSequenceNumber);
2373 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2374 kRtpExtensionTransportSequenceNumber, id);
2375 RTC_DCHECK(ret);
2376}
2377
stefanbba9dec2016-02-01 04:39:55 -08002378void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002379 RtpPacketSender* rtp_packet_sender,
2380 TransportFeedbackObserver* transport_feedback_observer,
2381 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002382 RTC_DCHECK(rtp_packet_sender);
2383 RTC_DCHECK(transport_feedback_observer);
2384 RTC_DCHECK(packet_router && !packet_router_);
2385 feedback_observer_proxy_->SetTransportFeedbackObserver(
2386 transport_feedback_observer);
2387 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2388 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2389 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002390 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002391 packet_router_ = packet_router;
2392}
2393
stefanbba9dec2016-02-01 04:39:55 -08002394void Channel::RegisterReceiverCongestionControlObjects(
2395 PacketRouter* packet_router) {
2396 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002397 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002398 packet_router_ = packet_router;
2399}
2400
2401void Channel::ResetCongestionControlObjects() {
2402 RTC_DCHECK(packet_router_);
2403 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2404 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2405 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002406 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002407 packet_router_ = nullptr;
2408 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2409}
2410
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002411void Channel::SetRTCPStatus(bool enable) {
2412 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2413 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002414 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002415}
2416
kwiberg55b97fe2016-01-28 05:22:45 -08002417int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002418 RtcpMode method = _rtpRtcpModule->RTCP();
2419 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002420 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002421}
2422
kwiberg55b97fe2016-01-28 05:22:45 -08002423int Channel::SetRTCP_CNAME(const char cName[256]) {
2424 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2425 "Channel::SetRTCP_CNAME()");
2426 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2427 _engineStatisticsPtr->SetLastError(
2428 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2429 "SetRTCP_CNAME() failed to set RTCP CNAME");
2430 return -1;
2431 }
2432 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002433}
2434
kwiberg55b97fe2016-01-28 05:22:45 -08002435int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2436 if (cName == NULL) {
2437 _engineStatisticsPtr->SetLastError(
2438 VE_INVALID_ARGUMENT, kTraceError,
2439 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2440 return -1;
2441 }
2442 char cname[RTCP_CNAME_SIZE];
2443 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2444 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2445 _engineStatisticsPtr->SetLastError(
2446 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2447 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2448 return -1;
2449 }
2450 strcpy(cName, cname);
2451 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002452}
2453
kwiberg55b97fe2016-01-28 05:22:45 -08002454int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2455 unsigned int& NTPLow,
2456 unsigned int& timestamp,
2457 unsigned int& playoutTimestamp,
2458 unsigned int* jitter,
2459 unsigned short* fractionLost) {
2460 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002461
kwiberg55b97fe2016-01-28 05:22:45 -08002462 RTCPSenderInfo senderInfo;
2463 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2464 _engineStatisticsPtr->SetLastError(
2465 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2466 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2467 "side");
2468 return -1;
2469 }
2470
2471 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2472 // and octet count)
2473 NTPHigh = senderInfo.NTPseconds;
2474 NTPLow = senderInfo.NTPfraction;
2475 timestamp = senderInfo.RTPtimeStamp;
2476
2477 // --- Locally derived information
2478
2479 // This value is updated on each incoming RTCP packet (0 when no packet
2480 // has been received)
2481 playoutTimestamp = playout_timestamp_rtcp_;
2482
2483 if (NULL != jitter || NULL != fractionLost) {
2484 // Get all RTCP receiver report blocks that have been received on this
2485 // channel. If we receive RTP packets from a remote source we know the
2486 // remote SSRC and use the report block from him.
2487 // Otherwise use the first report block.
2488 std::vector<RTCPReportBlock> remote_stats;
2489 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2490 remote_stats.empty()) {
2491 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2492 "GetRemoteRTCPData() failed to measure statistics due"
2493 " to lack of received RTP and/or RTCP packets");
2494 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002495 }
2496
kwiberg55b97fe2016-01-28 05:22:45 -08002497 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2498 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2499 for (; it != remote_stats.end(); ++it) {
2500 if (it->remoteSSRC == remoteSSRC)
2501 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002502 }
kwiberg55b97fe2016-01-28 05:22:45 -08002503
2504 if (it == remote_stats.end()) {
2505 // If we have not received any RTCP packets from this SSRC it probably
2506 // means that we have not received any RTP packets.
2507 // Use the first received report block instead.
2508 it = remote_stats.begin();
2509 remoteSSRC = it->remoteSSRC;
2510 }
2511
2512 if (jitter) {
2513 *jitter = it->jitter;
2514 }
2515
2516 if (fractionLost) {
2517 *fractionLost = it->fractionLost;
2518 }
2519 }
2520 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002521}
2522
kwiberg55b97fe2016-01-28 05:22:45 -08002523int Channel::SendApplicationDefinedRTCPPacket(
2524 unsigned char subType,
2525 unsigned int name,
2526 const char* data,
2527 unsigned short dataLengthInBytes) {
2528 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2529 "Channel::SendApplicationDefinedRTCPPacket()");
2530 if (!channel_state_.Get().sending) {
2531 _engineStatisticsPtr->SetLastError(
2532 VE_NOT_SENDING, kTraceError,
2533 "SendApplicationDefinedRTCPPacket() not sending");
2534 return -1;
2535 }
2536 if (NULL == data) {
2537 _engineStatisticsPtr->SetLastError(
2538 VE_INVALID_ARGUMENT, kTraceError,
2539 "SendApplicationDefinedRTCPPacket() invalid data value");
2540 return -1;
2541 }
2542 if (dataLengthInBytes % 4 != 0) {
2543 _engineStatisticsPtr->SetLastError(
2544 VE_INVALID_ARGUMENT, kTraceError,
2545 "SendApplicationDefinedRTCPPacket() invalid length value");
2546 return -1;
2547 }
2548 RtcpMode status = _rtpRtcpModule->RTCP();
2549 if (status == RtcpMode::kOff) {
2550 _engineStatisticsPtr->SetLastError(
2551 VE_RTCP_ERROR, kTraceError,
2552 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2553 return -1;
2554 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002555
kwiberg55b97fe2016-01-28 05:22:45 -08002556 // Create and schedule the RTCP APP packet for transmission
2557 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2558 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2559 _engineStatisticsPtr->SetLastError(
2560 VE_SEND_ERROR, kTraceError,
2561 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2562 return -1;
2563 }
2564 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002565}
2566
kwiberg55b97fe2016-01-28 05:22:45 -08002567int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2568 unsigned int& maxJitterMs,
2569 unsigned int& discardedPackets) {
2570 // The jitter statistics is updated for each received RTP packet and is
2571 // based on received packets.
2572 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2573 // If RTCP is off, there is no timed thread in the RTCP module regularly
2574 // generating new stats, trigger the update manually here instead.
2575 StreamStatistician* statistician =
2576 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2577 if (statistician) {
2578 // Don't use returned statistics, use data from proxy instead so that
2579 // max jitter can be fetched atomically.
2580 RtcpStatistics s;
2581 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002582 }
kwiberg55b97fe2016-01-28 05:22:45 -08002583 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002584
kwiberg55b97fe2016-01-28 05:22:45 -08002585 ChannelStatistics stats = statistics_proxy_->GetStats();
2586 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2587 if (playoutFrequency > 0) {
2588 // Scale RTP statistics given the current playout frequency
2589 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2590 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2591 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002592
kwiberg55b97fe2016-01-28 05:22:45 -08002593 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002594
kwiberg55b97fe2016-01-28 05:22:45 -08002595 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002596}
2597
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002598int Channel::GetRemoteRTCPReportBlocks(
2599 std::vector<ReportBlock>* report_blocks) {
2600 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002601 _engineStatisticsPtr->SetLastError(
2602 VE_INVALID_ARGUMENT, kTraceError,
2603 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002604 return -1;
2605 }
2606
2607 // Get the report blocks from the latest received RTCP Sender or Receiver
2608 // Report. Each element in the vector contains the sender's SSRC and a
2609 // report block according to RFC 3550.
2610 std::vector<RTCPReportBlock> rtcp_report_blocks;
2611 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002612 return -1;
2613 }
2614
2615 if (rtcp_report_blocks.empty())
2616 return 0;
2617
2618 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2619 for (; it != rtcp_report_blocks.end(); ++it) {
2620 ReportBlock report_block;
2621 report_block.sender_SSRC = it->remoteSSRC;
2622 report_block.source_SSRC = it->sourceSSRC;
2623 report_block.fraction_lost = it->fractionLost;
2624 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2625 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2626 report_block.interarrival_jitter = it->jitter;
2627 report_block.last_SR_timestamp = it->lastSR;
2628 report_block.delay_since_last_SR = it->delaySinceLastSR;
2629 report_blocks->push_back(report_block);
2630 }
2631 return 0;
2632}
2633
kwiberg55b97fe2016-01-28 05:22:45 -08002634int Channel::GetRTPStatistics(CallStatistics& stats) {
2635 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002636
kwiberg55b97fe2016-01-28 05:22:45 -08002637 // The jitter statistics is updated for each received RTP packet and is
2638 // based on received packets.
2639 RtcpStatistics statistics;
2640 StreamStatistician* statistician =
2641 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002642 if (statistician) {
2643 statistician->GetStatistics(&statistics,
2644 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002645 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002646
kwiberg55b97fe2016-01-28 05:22:45 -08002647 stats.fractionLost = statistics.fraction_lost;
2648 stats.cumulativeLost = statistics.cumulative_lost;
2649 stats.extendedMax = statistics.extended_max_sequence_number;
2650 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002651
kwiberg55b97fe2016-01-28 05:22:45 -08002652 // --- RTT
2653 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002654
kwiberg55b97fe2016-01-28 05:22:45 -08002655 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002656
kwiberg55b97fe2016-01-28 05:22:45 -08002657 size_t bytesSent(0);
2658 uint32_t packetsSent(0);
2659 size_t bytesReceived(0);
2660 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002661
kwiberg55b97fe2016-01-28 05:22:45 -08002662 if (statistician) {
2663 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2664 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002665
kwiberg55b97fe2016-01-28 05:22:45 -08002666 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2667 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2668 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2669 " output will not be complete");
2670 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002671
kwiberg55b97fe2016-01-28 05:22:45 -08002672 stats.bytesSent = bytesSent;
2673 stats.packetsSent = packetsSent;
2674 stats.bytesReceived = bytesReceived;
2675 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002676
kwiberg55b97fe2016-01-28 05:22:45 -08002677 // --- Timestamps
2678 {
2679 rtc::CritScope lock(&ts_stats_lock_);
2680 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2681 }
2682 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002683}
2684
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002685int Channel::SetCodecFECStatus(bool enable) {
2686 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2687 "Channel::SetCodecFECStatus()");
2688
kwibergc8d071e2016-04-06 12:22:38 -07002689 if (!codec_manager_.SetCodecFEC(enable) ||
2690 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002691 _engineStatisticsPtr->SetLastError(
2692 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2693 "SetCodecFECStatus() failed to set FEC state");
2694 return -1;
2695 }
2696 return 0;
2697}
2698
2699bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002700 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002701}
2702
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002703void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2704 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002705 // If pacing is enabled we always store packets.
2706 if (!pacing_enabled_)
2707 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002708 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002709 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002710 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002711 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002712 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002713}
2714
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002715// Called when we are missing one or more packets.
2716int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002717 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2718}
2719
kwiberg55b97fe2016-01-28 05:22:45 -08002720uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2721 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2722 "Channel::Demultiplex()");
2723 _audioFrame.CopyFrom(audioFrame);
2724 _audioFrame.id_ = _channelId;
2725 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002726}
2727
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002728void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002729 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002730 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002731 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002732 CodecInst codec;
2733 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002734
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002735 // Never upsample or upmix the capture signal here. This should be done at the
2736 // end of the send chain.
2737 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2738 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2739 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2740 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002741}
2742
kwiberg55b97fe2016-01-28 05:22:45 -08002743uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2744 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2745 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002746
kwiberg55b97fe2016-01-28 05:22:45 -08002747 if (_audioFrame.samples_per_channel_ == 0) {
2748 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2749 "Channel::PrepareEncodeAndSend() invalid audio frame");
2750 return 0xFFFFFFFF;
2751 }
2752
2753 if (channel_state_.Get().input_file_playing) {
2754 MixOrReplaceAudioWithFile(mixingFrequency);
2755 }
2756
solenberg1c2af8e2016-03-24 10:36:00 -07002757 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2758 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002759
2760 if (channel_state_.Get().input_external_media) {
2761 rtc::CritScope cs(&_callbackCritSect);
2762 const bool isStereo = (_audioFrame.num_channels_ == 2);
2763 if (_inputExternalMediaCallbackPtr) {
2764 _inputExternalMediaCallbackPtr->Process(
2765 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
2766 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
2767 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00002768 }
kwiberg55b97fe2016-01-28 05:22:45 -08002769 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002770
kwiberg55b97fe2016-01-28 05:22:45 -08002771 if (_includeAudioLevelIndication) {
2772 size_t length =
2773 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002774 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002775 if (is_muted && previous_frame_muted_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002776 rms_level_.ProcessMuted(length);
2777 } else {
2778 rms_level_.Process(_audioFrame.data_, length);
niklase@google.com470e71d2011-07-07 08:21:25 +00002779 }
kwiberg55b97fe2016-01-28 05:22:45 -08002780 }
solenberg1c2af8e2016-03-24 10:36:00 -07002781 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002782
kwiberg55b97fe2016-01-28 05:22:45 -08002783 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002784}
2785
kwiberg55b97fe2016-01-28 05:22:45 -08002786uint32_t Channel::EncodeAndSend() {
2787 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2788 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002789
kwiberg55b97fe2016-01-28 05:22:45 -08002790 assert(_audioFrame.num_channels_ <= 2);
2791 if (_audioFrame.samples_per_channel_ == 0) {
2792 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2793 "Channel::EncodeAndSend() invalid audio frame");
2794 return 0xFFFFFFFF;
2795 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002796
kwiberg55b97fe2016-01-28 05:22:45 -08002797 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002798
kwiberg55b97fe2016-01-28 05:22:45 -08002799 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002800
kwiberg55b97fe2016-01-28 05:22:45 -08002801 // The ACM resamples internally.
2802 _audioFrame.timestamp_ = _timeStamp;
2803 // This call will trigger AudioPacketizationCallback::SendData if encoding
2804 // is done and payload is ready for packetization and transmission.
2805 // Otherwise, it will return without invoking the callback.
2806 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2807 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2808 "Channel::EncodeAndSend() ACM encoding failed");
2809 return 0xFFFFFFFF;
2810 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002811
kwiberg55b97fe2016-01-28 05:22:45 -08002812 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2813 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002814}
2815
Minyue2013aec2015-05-13 14:14:42 +02002816void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002817 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002818 Channel* channel = associate_send_channel_.channel();
2819 if (channel && channel->ChannelId() == channel_id) {
2820 // If this channel is associated with a send channel of the specified
2821 // Channel ID, disassociate with it.
2822 ChannelOwner ref(NULL);
2823 associate_send_channel_ = ref;
2824 }
2825}
2826
ivoc14d5dbe2016-07-04 07:06:55 -07002827void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2828 event_log_proxy_->SetEventLog(event_log);
2829}
2830
michaelt79e05882016-11-08 02:50:09 -08002831void Channel::SetTransportOverhead(int transport_overhead_per_packet) {
2832 _rtpRtcpModule->SetTransportOverhead(transport_overhead_per_packet);
2833}
2834
kwiberg55b97fe2016-01-28 05:22:45 -08002835int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
2836 VoEMediaProcess& processObject) {
2837 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2838 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002839
kwiberg55b97fe2016-01-28 05:22:45 -08002840 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002841
kwiberg55b97fe2016-01-28 05:22:45 -08002842 if (kPlaybackPerChannel == type) {
2843 if (_outputExternalMediaCallbackPtr) {
2844 _engineStatisticsPtr->SetLastError(
2845 VE_INVALID_OPERATION, kTraceError,
2846 "Channel::RegisterExternalMediaProcessing() "
2847 "output external media already enabled");
2848 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002849 }
kwiberg55b97fe2016-01-28 05:22:45 -08002850 _outputExternalMediaCallbackPtr = &processObject;
2851 _outputExternalMedia = true;
2852 } else if (kRecordingPerChannel == type) {
2853 if (_inputExternalMediaCallbackPtr) {
2854 _engineStatisticsPtr->SetLastError(
2855 VE_INVALID_OPERATION, kTraceError,
2856 "Channel::RegisterExternalMediaProcessing() "
2857 "output external media already enabled");
2858 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002859 }
kwiberg55b97fe2016-01-28 05:22:45 -08002860 _inputExternalMediaCallbackPtr = &processObject;
2861 channel_state_.SetInputExternalMedia(true);
2862 }
2863 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002864}
2865
kwiberg55b97fe2016-01-28 05:22:45 -08002866int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
2867 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2868 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002869
kwiberg55b97fe2016-01-28 05:22:45 -08002870 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002871
kwiberg55b97fe2016-01-28 05:22:45 -08002872 if (kPlaybackPerChannel == type) {
2873 if (!_outputExternalMediaCallbackPtr) {
2874 _engineStatisticsPtr->SetLastError(
2875 VE_INVALID_OPERATION, kTraceWarning,
2876 "Channel::DeRegisterExternalMediaProcessing() "
2877 "output external media already disabled");
2878 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002879 }
kwiberg55b97fe2016-01-28 05:22:45 -08002880 _outputExternalMedia = false;
2881 _outputExternalMediaCallbackPtr = NULL;
2882 } else if (kRecordingPerChannel == type) {
2883 if (!_inputExternalMediaCallbackPtr) {
2884 _engineStatisticsPtr->SetLastError(
2885 VE_INVALID_OPERATION, kTraceWarning,
2886 "Channel::DeRegisterExternalMediaProcessing() "
2887 "input external media already disabled");
2888 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002889 }
kwiberg55b97fe2016-01-28 05:22:45 -08002890 channel_state_.SetInputExternalMedia(false);
2891 _inputExternalMediaCallbackPtr = NULL;
2892 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002893
kwiberg55b97fe2016-01-28 05:22:45 -08002894 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002895}
2896
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002897int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08002898 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2899 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002900
kwiberg55b97fe2016-01-28 05:22:45 -08002901 if (channel_state_.Get().playing) {
2902 _engineStatisticsPtr->SetLastError(
2903 VE_INVALID_OPERATION, kTraceError,
2904 "Channel::SetExternalMixing() "
2905 "external mixing cannot be changed while playing.");
2906 return -1;
2907 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002908
kwiberg55b97fe2016-01-28 05:22:45 -08002909 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002910
kwiberg55b97fe2016-01-28 05:22:45 -08002911 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002912}
2913
kwiberg55b97fe2016-01-28 05:22:45 -08002914int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2915 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002916}
2917
wu@webrtc.org24301a62013-12-13 19:17:43 +00002918void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2919 audio_coding_->GetDecodingCallStatistics(stats);
2920}
2921
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002922bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
2923 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08002924 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07002925 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002926 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002927 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002928}
2929
solenberg358057b2015-11-27 10:46:42 -08002930uint32_t Channel::GetDelayEstimate() const {
2931 int jitter_buffer_delay_ms = 0;
2932 int playout_buffer_delay_ms = 0;
2933 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
2934 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
2935}
2936
deadbeef74375882015-08-13 12:09:10 -07002937int Channel::LeastRequiredDelayMs() const {
2938 return audio_coding_->LeastRequiredDelayMs();
2939}
2940
kwiberg55b97fe2016-01-28 05:22:45 -08002941int Channel::SetMinimumPlayoutDelay(int delayMs) {
2942 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2943 "Channel::SetMinimumPlayoutDelay()");
2944 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2945 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2946 _engineStatisticsPtr->SetLastError(
2947 VE_INVALID_ARGUMENT, kTraceError,
2948 "SetMinimumPlayoutDelay() invalid min delay");
2949 return -1;
2950 }
2951 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2952 _engineStatisticsPtr->SetLastError(
2953 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2954 "SetMinimumPlayoutDelay() failed to set min playout delay");
2955 return -1;
2956 }
2957 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002958}
2959
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002960int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002961 uint32_t playout_timestamp_rtp = 0;
2962 {
tommi31fc21f2016-01-21 10:37:37 -08002963 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002964 playout_timestamp_rtp = playout_timestamp_rtp_;
2965 }
kwiberg55b97fe2016-01-28 05:22:45 -08002966 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002967 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002968 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002969 "GetPlayoutTimestamp() failed to retrieve timestamp");
2970 return -1;
2971 }
deadbeef74375882015-08-13 12:09:10 -07002972 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002973 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002974}
2975
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002976int Channel::SetInitTimestamp(unsigned int timestamp) {
2977 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002978 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002979 if (channel_state_.Get().sending) {
2980 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
2981 "SetInitTimestamp() already sending");
2982 return -1;
2983 }
2984 _rtpRtcpModule->SetStartTimestamp(timestamp);
2985 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002986}
2987
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002988int Channel::SetInitSequenceNumber(short sequenceNumber) {
2989 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2990 "Channel::SetInitSequenceNumber()");
2991 if (channel_state_.Get().sending) {
2992 _engineStatisticsPtr->SetLastError(
2993 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
2994 return -1;
2995 }
2996 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
2997 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002998}
2999
kwiberg55b97fe2016-01-28 05:22:45 -08003000int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
3001 RtpReceiver** rtp_receiver) const {
3002 *rtpRtcpModule = _rtpRtcpModule.get();
3003 *rtp_receiver = rtp_receiver_.get();
3004 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003005}
3006
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003007// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3008// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08003009int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08003010 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08003011 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003012
kwiberg55b97fe2016-01-28 05:22:45 -08003013 {
3014 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003015
kwiberg5a25d952016-08-17 07:31:12 -07003016 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003017 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3018 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3019 " doesnt exist");
3020 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003021 }
3022
kwiberg4ec01d92016-08-22 08:43:54 -07003023 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07003024 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003025 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3026 "Channel::MixOrReplaceAudioWithFile() file mixing "
3027 "failed");
3028 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003029 }
kwiberg55b97fe2016-01-28 05:22:45 -08003030 if (fileSamples == 0) {
3031 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3032 "Channel::MixOrReplaceAudioWithFile() file is ended");
3033 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003034 }
kwiberg55b97fe2016-01-28 05:22:45 -08003035 }
3036
3037 assert(_audioFrame.samples_per_channel_ == fileSamples);
3038
3039 if (_mixFileWithMicrophone) {
3040 // Currently file stream is always mono.
3041 // TODO(xians): Change the code when FilePlayer supports real stereo.
3042 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3043 1, fileSamples);
3044 } else {
3045 // Replace ACM audio with file.
3046 // Currently file stream is always mono.
3047 // TODO(xians): Change the code when FilePlayer supports real stereo.
3048 _audioFrame.UpdateFrame(
3049 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3050 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3051 }
3052 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003053}
3054
kwiberg55b97fe2016-01-28 05:22:45 -08003055int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3056 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003057
kwibergb7f89d62016-02-17 10:04:18 -08003058 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003059 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003060
kwiberg55b97fe2016-01-28 05:22:45 -08003061 {
3062 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003063
kwiberg5a25d952016-08-17 07:31:12 -07003064 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003065 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3066 "Channel::MixAudioWithFile() file mixing failed");
3067 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003068 }
3069
kwiberg55b97fe2016-01-28 05:22:45 -08003070 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003071 if (output_file_player_->Get10msAudioFromFile(
3072 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003073 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3074 "Channel::MixAudioWithFile() file mixing failed");
3075 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003076 }
kwiberg55b97fe2016-01-28 05:22:45 -08003077 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003078
kwiberg55b97fe2016-01-28 05:22:45 -08003079 if (audioFrame.samples_per_channel_ == fileSamples) {
3080 // Currently file stream is always mono.
3081 // TODO(xians): Change the code when FilePlayer supports real stereo.
3082 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3083 fileSamples);
3084 } else {
3085 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3086 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3087 ") != "
3088 "fileSamples(%" PRIuS ")",
3089 audioFrame.samples_per_channel_, fileSamples);
3090 return -1;
3091 }
3092
3093 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003094}
3095
deadbeef74375882015-08-13 12:09:10 -07003096void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003097 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003098
henrik.lundin96bd5022016-04-06 04:13:56 -07003099 if (!jitter_buffer_playout_timestamp_) {
3100 // This can happen if this channel has not received any RTP packets. In
3101 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003102 return;
3103 }
3104
3105 uint16_t delay_ms = 0;
3106 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003107 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003108 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3109 " delay from the ADM");
3110 _engineStatisticsPtr->SetLastError(
3111 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3112 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3113 return;
3114 }
3115
henrik.lundin96bd5022016-04-06 04:13:56 -07003116 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3117 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003118
3119 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003120 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003121
kwiberg55b97fe2016-01-28 05:22:45 -08003122 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003123 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003124 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003125
3126 {
tommi31fc21f2016-01-21 10:37:37 -08003127 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003128 if (rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003129 playout_timestamp_rtcp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003130 } else {
henrik.lundin96bd5022016-04-06 04:13:56 -07003131 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003132 }
3133 playout_delay_ms_ = delay_ms;
3134 }
3135}
3136
kwiberg55b97fe2016-01-28 05:22:45 -08003137void Channel::RegisterReceiveCodecsToRTPModule() {
3138 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3139 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003140
kwiberg55b97fe2016-01-28 05:22:45 -08003141 CodecInst codec;
3142 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003143
kwiberg55b97fe2016-01-28 05:22:45 -08003144 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3145 // Open up the RTP/RTCP receiver for all supported codecs
3146 if ((audio_coding_->Codec(idx, &codec) == -1) ||
3147 (rtp_receiver_->RegisterReceivePayload(
3148 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3149 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
3150 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3151 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3152 " to register %s (%d/%d/%" PRIuS
3153 "/%d) to RTP/RTCP "
3154 "receiver",
3155 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3156 codec.rate);
3157 } else {
3158 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3159 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3160 "(%d/%d/%" PRIuS
3161 "/%d) has been added to the RTP/RTCP "
3162 "receiver",
3163 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3164 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003165 }
kwiberg55b97fe2016-01-28 05:22:45 -08003166 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003167}
3168
kwiberg55b97fe2016-01-28 05:22:45 -08003169int Channel::SetSendRtpHeaderExtension(bool enable,
3170 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003171 unsigned char id) {
3172 int error = 0;
3173 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3174 if (enable) {
3175 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3176 }
3177 return error;
3178}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003179
ossue280cde2016-10-12 11:04:10 -07003180int Channel::GetRtpTimestampRateHz() const {
3181 const auto format = audio_coding_->ReceiveFormat();
3182 // Default to the playout frequency if we've not gotten any packets yet.
3183 // TODO(ossu): Zero clockrate can only happen if we've added an external
3184 // decoder for a format we don't support internally. Remove once that way of
3185 // adding decoders is gone!
3186 return (format && format->clockrate_hz != 0)
3187 ? format->clockrate_hz
3188 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003189}
3190
Minyue2013aec2015-05-13 14:14:42 +02003191int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003192 RtcpMode method = _rtpRtcpModule->RTCP();
3193 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003194 return 0;
3195 }
3196 std::vector<RTCPReportBlock> report_blocks;
3197 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003198
3199 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003200 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003201 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003202 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003203 Channel* channel = associate_send_channel_.channel();
3204 // Tries to get RTT from an associated channel. This is important for
3205 // receive-only channels.
3206 if (channel) {
3207 // To prevent infinite recursion and deadlock, calling GetRTT of
3208 // associate channel should always use "false" for argument:
3209 // |allow_associate_channel|.
3210 rtt = channel->GetRTT(false);
3211 }
3212 }
3213 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003214 }
3215
3216 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3217 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3218 for (; it != report_blocks.end(); ++it) {
3219 if (it->remoteSSRC == remoteSSRC)
3220 break;
3221 }
3222 if (it == report_blocks.end()) {
3223 // We have not received packets with SSRC matching the report blocks.
3224 // To calculate RTT we try with the SSRC of the first report block.
3225 // This is very important for send-only channels where we don't know
3226 // the SSRC of the other end.
3227 remoteSSRC = report_blocks[0].remoteSSRC;
3228 }
Minyue2013aec2015-05-13 14:14:42 +02003229
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003230 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003231 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003232 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003233 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3234 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003235 return 0;
3236 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003237 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003238}
3239
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003240} // namespace voe
3241} // namespace webrtc