blob: b2fade87439870af781ce7f310836bdfcf781342 [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()),
magjedf3feeff2016-11-25 06:40:25 -0800837 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100838 rtp_receive_statistics_(
839 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
840 rtp_receiver_(
841 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100842 this,
843 this,
844 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700845 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100846 _outputAudioLevel(),
847 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100848 // Avoid conflict with other channels by adding 1024 - 1026,
849 // won't use as much as 1024 channels.
850 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
851 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
852 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
853 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100854 _outputExternalMedia(false),
855 _inputExternalMediaCallbackPtr(NULL),
856 _outputExternalMediaCallbackPtr(NULL),
857 _timeStamp(0), // This is just an offset, RTP module will add it's own
858 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100859 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100860 playout_timestamp_rtp_(0),
861 playout_timestamp_rtcp_(0),
862 playout_delay_ms_(0),
863 _numberOfDiscardedPackets(0),
864 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100865 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
866 capture_start_rtp_time_stamp_(-1),
867 capture_start_ntp_time_ms_(-1),
868 _engineStatisticsPtr(NULL),
869 _outputMixerPtr(NULL),
870 _transmitMixerPtr(NULL),
871 _moduleProcessThreadPtr(NULL),
872 _audioDeviceModulePtr(NULL),
873 _voiceEngineObserverPtr(NULL),
874 _callbackCritSectPtr(NULL),
875 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100876 _sendFrameType(0),
877 _externalMixing(false),
878 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700879 input_mute_(false),
880 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100881 _panLeft(1.0f),
882 _panRight(1.0f),
883 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100884 _lastLocalTimeStamp(0),
885 _lastPayloadType(0),
886 _includeAudioLevelIndication(false),
887 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100888 restored_packet_in_use_(false),
889 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100890 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700891 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800892 feedback_observer_proxy_(new TransportFeedbackProxy()),
893 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700894 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200895 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
896 kMaxRetransmissionWindowMs)),
solenberg88499ec2016-09-07 07:34:41 -0700897 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800898 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
899 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700900 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800901 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700902 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800903 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200904
kwiberg55b97fe2016-01-28 05:22:45 -0800905 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000906
kwiberg55b97fe2016-01-28 05:22:45 -0800907 RtpRtcp::Configuration configuration;
908 configuration.audio = true;
909 configuration.outgoing_transport = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800910 configuration.receive_statistics = rtp_receive_statistics_.get();
911 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800912 if (pacing_enabled_) {
913 configuration.paced_sender = rtp_packet_sender_proxy_.get();
914 configuration.transport_sequence_number_allocator =
915 seq_num_allocator_proxy_.get();
916 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
917 }
ivoc14d5dbe2016-07-04 07:06:55 -0700918 configuration.event_log = &(*event_log_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200919 configuration.retransmission_rate_limiter =
920 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000921
kwiberg55b97fe2016-01-28 05:22:45 -0800922 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100923 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000924
kwiberg55b97fe2016-01-28 05:22:45 -0800925 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
926 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
927 statistics_proxy_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000928}
929
kwiberg55b97fe2016-01-28 05:22:45 -0800930Channel::~Channel() {
931 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
932 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
933 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000934
kwiberg55b97fe2016-01-28 05:22:45 -0800935 if (_outputExternalMedia) {
936 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
937 }
938 if (channel_state_.Get().input_external_media) {
939 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
940 }
941 StopSend();
942 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000943
kwiberg55b97fe2016-01-28 05:22:45 -0800944 {
945 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700946 if (input_file_player_) {
947 input_file_player_->RegisterModuleFileCallback(NULL);
948 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000949 }
kwiberg5a25d952016-08-17 07:31:12 -0700950 if (output_file_player_) {
951 output_file_player_->RegisterModuleFileCallback(NULL);
952 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800953 }
kwiberg5a25d952016-08-17 07:31:12 -0700954 if (output_file_recorder_) {
955 output_file_recorder_->RegisterModuleFileCallback(NULL);
956 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800957 }
958 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000959
kwiberg55b97fe2016-01-28 05:22:45 -0800960 // The order to safely shutdown modules in a channel is:
961 // 1. De-register callbacks in modules
962 // 2. De-register modules in process thread
963 // 3. Destroy modules
964 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
965 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
966 "~Channel() failed to de-register transport callback"
967 " (Audio coding module)");
968 }
969 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
970 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
971 "~Channel() failed to de-register VAD callback"
972 " (Audio coding module)");
973 }
974 // De-register modules in process thread
975 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000976
kwiberg55b97fe2016-01-28 05:22:45 -0800977 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000978}
979
kwiberg55b97fe2016-01-28 05:22:45 -0800980int32_t Channel::Init() {
981 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
982 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000983
kwiberg55b97fe2016-01-28 05:22:45 -0800984 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000985
kwiberg55b97fe2016-01-28 05:22:45 -0800986 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000987
kwiberg55b97fe2016-01-28 05:22:45 -0800988 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
989 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
990 "Channel::Init() must call SetEngineInformation() first");
991 return -1;
992 }
993
994 // --- Add modules to process thread (for periodic schedulation)
995
996 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
997
998 // --- ACM initialization
999
1000 if (audio_coding_->InitializeReceiver() == -1) {
1001 _engineStatisticsPtr->SetLastError(
1002 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1003 "Channel::Init() unable to initialize the ACM - 1");
1004 return -1;
1005 }
1006
1007 // --- RTP/RTCP module initialization
1008
1009 // Ensure that RTCP is enabled by default for the created channel.
1010 // Note that, the module will keep generating RTCP until it is explicitly
1011 // disabled by the user.
1012 // After StopListen (when no sockets exists), RTCP packets will no longer
1013 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001014 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001015 // RTCP is enabled by default.
1016 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1017 // --- Register all permanent callbacks
1018 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1019 (audio_coding_->RegisterVADCallback(this) == -1);
1020
1021 if (fail) {
1022 _engineStatisticsPtr->SetLastError(
1023 VE_CANNOT_INIT_CHANNEL, kTraceError,
1024 "Channel::Init() callbacks not registered");
1025 return -1;
1026 }
1027
1028 // --- Register all supported codecs to the receiving side of the
1029 // RTP/RTCP module
1030
1031 CodecInst codec;
1032 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1033
1034 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1035 // Open up the RTP/RTCP receiver for all supported codecs
1036 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08001037 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001038 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1039 "Channel::Init() unable to register %s "
1040 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1041 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1042 codec.rate);
1043 } else {
1044 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1045 "Channel::Init() %s (%d/%d/%" PRIuS
1046 "/%d) has been "
1047 "added to the RTP/RTCP receiver",
1048 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1049 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001050 }
1051
kwiberg55b97fe2016-01-28 05:22:45 -08001052 // Ensure that PCMU is used as default codec on the sending side
1053 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1054 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001055 }
1056
kwiberg55b97fe2016-01-28 05:22:45 -08001057 // Register default PT for outband 'telephone-event'
1058 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001059 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001060 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1061 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001062 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1063 "Channel::Init() failed to register outband "
1064 "'telephone-event' (%d/%d) correctly",
1065 codec.pltype, codec.plfreq);
1066 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001067 }
1068
kwiberg55b97fe2016-01-28 05:22:45 -08001069 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001070 if (!codec_manager_.RegisterEncoder(codec) ||
1071 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001072 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1073 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001074 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001075 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1076 "Channel::Init() failed to register CN (%d/%d) "
1077 "correctly - 1",
1078 codec.pltype, codec.plfreq);
1079 }
1080 }
kwiberg55b97fe2016-01-28 05:22:45 -08001081 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001082
kwiberg55b97fe2016-01-28 05:22:45 -08001083 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001084}
1085
kwiberg55b97fe2016-01-28 05:22:45 -08001086int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1087 OutputMixer& outputMixer,
1088 voe::TransmitMixer& transmitMixer,
1089 ProcessThread& moduleProcessThread,
1090 AudioDeviceModule& audioDeviceModule,
1091 VoiceEngineObserver* voiceEngineObserver,
1092 rtc::CriticalSection* callbackCritSect) {
1093 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1094 "Channel::SetEngineInformation()");
1095 _engineStatisticsPtr = &engineStatistics;
1096 _outputMixerPtr = &outputMixer;
1097 _transmitMixerPtr = &transmitMixer,
1098 _moduleProcessThreadPtr = &moduleProcessThread;
1099 _audioDeviceModulePtr = &audioDeviceModule;
1100 _voiceEngineObserverPtr = voiceEngineObserver;
1101 _callbackCritSectPtr = callbackCritSect;
1102 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001103}
1104
kwiberg55b97fe2016-01-28 05:22:45 -08001105int32_t Channel::UpdateLocalTimeStamp() {
1106 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1107 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001108}
1109
kwibergb7f89d62016-02-17 10:04:18 -08001110void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001111 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001112 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001113}
1114
ossu29b1a8d2016-06-13 07:34:51 -07001115const rtc::scoped_refptr<AudioDecoderFactory>&
1116Channel::GetAudioDecoderFactory() const {
1117 return decoder_factory_;
1118}
1119
kwiberg55b97fe2016-01-28 05:22:45 -08001120int32_t Channel::StartPlayout() {
1121 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1122 "Channel::StartPlayout()");
1123 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001124 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001125 }
1126
1127 if (!_externalMixing) {
1128 // Add participant as candidates for mixing.
1129 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1130 _engineStatisticsPtr->SetLastError(
1131 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1132 "StartPlayout() failed to add participant to mixer");
1133 return -1;
1134 }
1135 }
1136
1137 channel_state_.SetPlaying(true);
1138 if (RegisterFilePlayingToMixer() != 0)
1139 return -1;
1140
1141 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001142}
1143
kwiberg55b97fe2016-01-28 05:22:45 -08001144int32_t Channel::StopPlayout() {
1145 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1146 "Channel::StopPlayout()");
1147 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001148 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001149 }
1150
1151 if (!_externalMixing) {
1152 // Remove participant as candidates for mixing
1153 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1154 _engineStatisticsPtr->SetLastError(
1155 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1156 "StopPlayout() failed to remove participant from mixer");
1157 return -1;
1158 }
1159 }
1160
1161 channel_state_.SetPlaying(false);
1162 _outputAudioLevel.Clear();
1163
1164 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001165}
1166
kwiberg55b97fe2016-01-28 05:22:45 -08001167int32_t Channel::StartSend() {
1168 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1169 "Channel::StartSend()");
1170 // Resume the previous sequence number which was reset by StopSend().
1171 // This needs to be done before |sending| is set to true.
1172 if (send_sequence_number_)
1173 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001174
kwiberg55b97fe2016-01-28 05:22:45 -08001175 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001176 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001177 }
1178 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001179
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001180 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001181 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1182 _engineStatisticsPtr->SetLastError(
1183 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1184 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001185 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001186 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001187 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001188 return -1;
1189 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001190
kwiberg55b97fe2016-01-28 05:22:45 -08001191 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001192}
1193
kwiberg55b97fe2016-01-28 05:22:45 -08001194int32_t Channel::StopSend() {
1195 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1196 "Channel::StopSend()");
1197 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001198 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001199 }
1200 channel_state_.SetSending(false);
1201
1202 // Store the sequence number to be able to pick up the same sequence for
1203 // the next StartSend(). This is needed for restarting device, otherwise
1204 // it might cause libSRTP to complain about packets being replayed.
1205 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1206 // CL is landed. See issue
1207 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1208 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1209
1210 // Reset sending SSRC and sequence number and triggers direct transmission
1211 // of RTCP BYE
1212 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1213 _engineStatisticsPtr->SetLastError(
1214 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1215 "StartSend() RTP/RTCP failed to stop sending");
1216 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001217 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001218
1219 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001220}
1221
solenberge566ac72016-10-31 12:52:33 -07001222void Channel::ResetDiscardedPacketCount() {
kwiberg55b97fe2016-01-28 05:22:45 -08001223 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberge566ac72016-10-31 12:52:33 -07001224 "Channel::ResetDiscardedPacketCount()");
kwiberg55b97fe2016-01-28 05:22:45 -08001225 _numberOfDiscardedPackets = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001226}
1227
kwiberg55b97fe2016-01-28 05:22:45 -08001228int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1229 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1230 "Channel::RegisterVoiceEngineObserver()");
1231 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001232
kwiberg55b97fe2016-01-28 05:22:45 -08001233 if (_voiceEngineObserverPtr) {
1234 _engineStatisticsPtr->SetLastError(
1235 VE_INVALID_OPERATION, kTraceError,
1236 "RegisterVoiceEngineObserver() observer already enabled");
1237 return -1;
1238 }
1239 _voiceEngineObserverPtr = &observer;
1240 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001241}
1242
kwiberg55b97fe2016-01-28 05:22:45 -08001243int32_t Channel::DeRegisterVoiceEngineObserver() {
1244 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1245 "Channel::DeRegisterVoiceEngineObserver()");
1246 rtc::CritScope cs(&_callbackCritSect);
1247
1248 if (!_voiceEngineObserverPtr) {
1249 _engineStatisticsPtr->SetLastError(
1250 VE_INVALID_OPERATION, kTraceWarning,
1251 "DeRegisterVoiceEngineObserver() observer already disabled");
1252 return 0;
1253 }
1254 _voiceEngineObserverPtr = NULL;
1255 return 0;
1256}
1257
1258int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001259 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001260 if (send_codec) {
1261 codec = *send_codec;
1262 return 0;
1263 }
1264 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001265}
1266
kwiberg55b97fe2016-01-28 05:22:45 -08001267int32_t Channel::GetRecCodec(CodecInst& codec) {
1268 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001269}
1270
kwiberg55b97fe2016-01-28 05:22:45 -08001271int32_t Channel::SetSendCodec(const CodecInst& codec) {
1272 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1273 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001274
kwibergc8d071e2016-04-06 12:22:38 -07001275 if (!codec_manager_.RegisterEncoder(codec) ||
1276 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001277 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1278 "SetSendCodec() failed to register codec to ACM");
1279 return -1;
1280 }
1281
1282 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1283 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1284 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1285 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1286 "SetSendCodec() failed to register codec to"
1287 " RTP/RTCP module");
1288 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001289 }
kwiberg55b97fe2016-01-28 05:22:45 -08001290 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001291
kwiberg55b97fe2016-01-28 05:22:45 -08001292 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) {
1293 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1294 "SetSendCodec() failed to set audio packet size");
1295 return -1;
1296 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001297
kwiberg55b97fe2016-01-28 05:22:45 -08001298 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001299}
1300
Ivo Creusenadf89b72015-04-29 16:03:33 +02001301void Channel::SetBitRate(int bitrate_bps) {
1302 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1303 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001304 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1305 if (*encoder)
1306 (*encoder)->OnReceivedTargetAudioBitrate(bitrate_bps);
1307 });
Erik Språng737336d2016-07-29 12:59:36 +02001308 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001309}
1310
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001311void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001312 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1313 if (*encoder)
1314 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1315 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001316}
1317
kwiberg55b97fe2016-01-28 05:22:45 -08001318int32_t Channel::SetVADStatus(bool enableVAD,
1319 ACMVADMode mode,
1320 bool disableDTX) {
1321 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1322 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001323 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1324 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1325 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001326 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1327 kTraceError,
1328 "SetVADStatus() failed to set VAD");
1329 return -1;
1330 }
1331 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001332}
1333
kwiberg55b97fe2016-01-28 05:22:45 -08001334int32_t Channel::GetVADStatus(bool& enabledVAD,
1335 ACMVADMode& mode,
1336 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001337 const auto* params = codec_manager_.GetStackParams();
1338 enabledVAD = params->use_cng;
1339 mode = params->vad_mode;
1340 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001341 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001342}
1343
kwiberg55b97fe2016-01-28 05:22:45 -08001344int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1345 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1346 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001347
kwiberg55b97fe2016-01-28 05:22:45 -08001348 if (channel_state_.Get().playing) {
1349 _engineStatisticsPtr->SetLastError(
1350 VE_ALREADY_PLAYING, kTraceError,
1351 "SetRecPayloadType() unable to set PT while playing");
1352 return -1;
1353 }
kwiberg55b97fe2016-01-28 05:22:45 -08001354
1355 if (codec.pltype == -1) {
1356 // De-register the selected codec (RTP/RTCP module and ACM)
1357
1358 int8_t pltype(-1);
1359 CodecInst rxCodec = codec;
1360
1361 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001362 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001363 rxCodec.pltype = pltype;
1364
1365 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1366 _engineStatisticsPtr->SetLastError(
1367 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1368 "SetRecPayloadType() RTP/RTCP-module deregistration "
1369 "failed");
1370 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001371 }
kwiberg55b97fe2016-01-28 05:22:45 -08001372 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1373 _engineStatisticsPtr->SetLastError(
1374 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1375 "SetRecPayloadType() ACM deregistration failed - 1");
1376 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001377 }
kwiberg55b97fe2016-01-28 05:22:45 -08001378 return 0;
1379 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001380
magjed56124bd2016-11-24 09:34:46 -08001381 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001382 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001383 // TODO(kwiberg): Retrying is probably not necessary, since
1384 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001385 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001386 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001387 _engineStatisticsPtr->SetLastError(
1388 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1389 "SetRecPayloadType() RTP/RTCP-module registration failed");
1390 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001391 }
kwiberg55b97fe2016-01-28 05:22:45 -08001392 }
kwibergda2bf4e2016-10-24 13:47:09 -07001393 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1394 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001395 audio_coding_->UnregisterReceiveCodec(codec.pltype);
kwibergda2bf4e2016-10-24 13:47:09 -07001396 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1397 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001398 _engineStatisticsPtr->SetLastError(
1399 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1400 "SetRecPayloadType() ACM registration failed - 1");
1401 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001402 }
kwiberg55b97fe2016-01-28 05:22:45 -08001403 }
1404 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001405}
1406
kwiberg55b97fe2016-01-28 05:22:45 -08001407int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1408 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001409 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001410 _engineStatisticsPtr->SetLastError(
1411 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1412 "GetRecPayloadType() failed to retrieve RX payload type");
1413 return -1;
1414 }
1415 codec.pltype = payloadType;
1416 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001417}
1418
kwiberg55b97fe2016-01-28 05:22:45 -08001419int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1420 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1421 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001422
kwiberg55b97fe2016-01-28 05:22:45 -08001423 CodecInst codec;
1424 int32_t samplingFreqHz(-1);
1425 const size_t kMono = 1;
1426 if (frequency == kFreq32000Hz)
1427 samplingFreqHz = 32000;
1428 else if (frequency == kFreq16000Hz)
1429 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001430
kwiberg55b97fe2016-01-28 05:22:45 -08001431 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1432 _engineStatisticsPtr->SetLastError(
1433 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1434 "SetSendCNPayloadType() failed to retrieve default CN codec "
1435 "settings");
1436 return -1;
1437 }
1438
1439 // Modify the payload type (must be set to dynamic range)
1440 codec.pltype = type;
1441
kwibergc8d071e2016-04-06 12:22:38 -07001442 if (!codec_manager_.RegisterEncoder(codec) ||
1443 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001444 _engineStatisticsPtr->SetLastError(
1445 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1446 "SetSendCNPayloadType() failed to register CN to ACM");
1447 return -1;
1448 }
1449
1450 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1451 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1452 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1453 _engineStatisticsPtr->SetLastError(
1454 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1455 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1456 "module");
1457 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001458 }
kwiberg55b97fe2016-01-28 05:22:45 -08001459 }
1460 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001461}
1462
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001463int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001464 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001465 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001466
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001467 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001468 _engineStatisticsPtr->SetLastError(
1469 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001470 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001471 return -1;
1472 }
1473 return 0;
1474}
1475
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001476int Channel::SetOpusDtx(bool enable_dtx) {
1477 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1478 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001479 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001480 : audio_coding_->DisableOpusDtx();
1481 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001482 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1483 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001484 return -1;
1485 }
1486 return 0;
1487}
1488
ivoc85228d62016-07-27 04:53:47 -07001489int Channel::GetOpusDtx(bool* enabled) {
1490 int success = -1;
1491 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1492 if (encoder) {
1493 *enabled = encoder->GetDtx();
1494 success = 0;
1495 }
1496 });
1497 return success;
1498}
1499
minyue7e304322016-10-12 05:00:55 -07001500bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1501 bool success = false;
1502 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1503 if (*encoder) {
1504 success = (*encoder)->EnableAudioNetworkAdaptor(
1505 config_string, Clock::GetRealTimeClock());
1506 }
1507 });
1508 return success;
1509}
1510
1511void Channel::DisableAudioNetworkAdaptor() {
1512 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1513 if (*encoder)
1514 (*encoder)->DisableAudioNetworkAdaptor();
1515 });
1516}
1517
1518void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1519 int max_frame_length_ms) {
1520 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1521 if (*encoder) {
1522 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1523 max_frame_length_ms);
1524 }
1525 });
1526}
1527
mflodman3d7db262016-04-29 00:57:13 -07001528int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001529 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001530 "Channel::RegisterExternalTransport()");
1531
kwiberg55b97fe2016-01-28 05:22:45 -08001532 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001533 if (_externalTransport) {
1534 _engineStatisticsPtr->SetLastError(
1535 VE_INVALID_OPERATION, kTraceError,
1536 "RegisterExternalTransport() external transport already enabled");
1537 return -1;
1538 }
1539 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001540 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001541 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001542}
1543
kwiberg55b97fe2016-01-28 05:22:45 -08001544int32_t Channel::DeRegisterExternalTransport() {
1545 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1546 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001547
kwiberg55b97fe2016-01-28 05:22:45 -08001548 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001549 if (_transportPtr) {
1550 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1551 "DeRegisterExternalTransport() all transport is disabled");
1552 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001553 _engineStatisticsPtr->SetLastError(
1554 VE_INVALID_OPERATION, kTraceWarning,
1555 "DeRegisterExternalTransport() external transport already "
1556 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001557 }
1558 _externalTransport = false;
1559 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001560 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001561}
1562
mflodman3d7db262016-04-29 00:57:13 -07001563int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001564 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001565 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001566 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001567 "Channel::ReceivedRTPPacket()");
1568
1569 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001570 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001571
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001572 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001573 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1574 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1575 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001576 return -1;
1577 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001578 header.payload_type_frequency =
1579 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001580 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001581 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001582 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001583 rtp_receive_statistics_->IncomingPacket(
1584 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001585 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001586
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001587 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001588}
1589
1590bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001591 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001592 const RTPHeader& header,
1593 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001594 if (rtp_payload_registry_->IsRtx(header)) {
1595 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001596 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001597 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001598 assert(packet_length >= header.headerLength);
1599 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001600 PayloadUnion payload_specific;
1601 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001602 &payload_specific)) {
1603 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001604 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001605 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1606 payload_specific, in_order);
1607}
1608
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001609bool Channel::HandleRtxPacket(const uint8_t* packet,
1610 size_t packet_length,
1611 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001612 if (!rtp_payload_registry_->IsRtx(header))
1613 return false;
1614
1615 // Remove the RTX header and parse the original RTP header.
1616 if (packet_length < header.headerLength)
1617 return false;
1618 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1619 return false;
1620 if (restored_packet_in_use_) {
1621 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1622 "Multiple RTX headers detected, dropping packet");
1623 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001624 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001625 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001626 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1627 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001628 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1629 "Incoming RTX packet: invalid RTP header");
1630 return false;
1631 }
1632 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001633 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001634 restored_packet_in_use_ = false;
1635 return ret;
1636}
1637
1638bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1639 StreamStatistician* statistician =
1640 rtp_receive_statistics_->GetStatistician(header.ssrc);
1641 if (!statistician)
1642 return false;
1643 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001644}
1645
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001646bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1647 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001648 // Retransmissions are handled separately if RTX is enabled.
1649 if (rtp_payload_registry_->RtxEnabled())
1650 return false;
1651 StreamStatistician* statistician =
1652 rtp_receive_statistics_->GetStatistician(header.ssrc);
1653 if (!statistician)
1654 return false;
1655 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001656 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001657 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001658 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001659}
1660
mflodman3d7db262016-04-29 00:57:13 -07001661int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001662 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001663 "Channel::ReceivedRTCPPacket()");
1664 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001665 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001666
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001667 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001668 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001669 _engineStatisticsPtr->SetLastError(
1670 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1671 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1672 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001673
Minyue2013aec2015-05-13 14:14:42 +02001674 int64_t rtt = GetRTT(true);
1675 if (rtt == 0) {
1676 // Waiting for valid RTT.
1677 return 0;
1678 }
Erik Språng737336d2016-07-29 12:59:36 +02001679
1680 int64_t nack_window_ms = rtt;
1681 if (nack_window_ms < kMinRetransmissionWindowMs) {
1682 nack_window_ms = kMinRetransmissionWindowMs;
1683 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1684 nack_window_ms = kMaxRetransmissionWindowMs;
1685 }
1686 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1687
minyue7e304322016-10-12 05:00:55 -07001688 // Invoke audio encoders OnReceivedRtt().
1689 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1690 if (*encoder)
1691 (*encoder)->OnReceivedRtt(rtt);
1692 });
1693
Minyue2013aec2015-05-13 14:14:42 +02001694 uint32_t ntp_secs = 0;
1695 uint32_t ntp_frac = 0;
1696 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001697 if (0 !=
1698 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1699 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001700 // Waiting for RTCP.
1701 return 0;
1702 }
1703
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001704 {
tommi31fc21f2016-01-21 10:37:37 -08001705 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001706 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001707 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001708 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001709}
1710
niklase@google.com470e71d2011-07-07 08:21:25 +00001711int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001712 bool loop,
1713 FileFormats format,
1714 int startPosition,
1715 float volumeScaling,
1716 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001717 const CodecInst* codecInst) {
1718 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1719 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1720 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1721 "stopPosition=%d)",
1722 fileName, loop, format, volumeScaling, startPosition,
1723 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001724
kwiberg55b97fe2016-01-28 05:22:45 -08001725 if (channel_state_.Get().output_file_playing) {
1726 _engineStatisticsPtr->SetLastError(
1727 VE_ALREADY_PLAYING, kTraceError,
1728 "StartPlayingFileLocally() is already playing");
1729 return -1;
1730 }
1731
1732 {
1733 rtc::CritScope cs(&_fileCritSect);
1734
kwiberg5a25d952016-08-17 07:31:12 -07001735 if (output_file_player_) {
1736 output_file_player_->RegisterModuleFileCallback(NULL);
1737 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001738 }
1739
kwiberg5b356f42016-09-08 04:32:33 -07001740 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001741 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001742
kwiberg5a25d952016-08-17 07:31:12 -07001743 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001744 _engineStatisticsPtr->SetLastError(
1745 VE_INVALID_ARGUMENT, kTraceError,
1746 "StartPlayingFileLocally() filePlayer format is not correct");
1747 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001748 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001749
kwiberg55b97fe2016-01-28 05:22:45 -08001750 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001751
kwiberg5a25d952016-08-17 07:31:12 -07001752 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001753 fileName, loop, startPosition, volumeScaling, notificationTime,
1754 stopPosition, (const CodecInst*)codecInst) != 0) {
1755 _engineStatisticsPtr->SetLastError(
1756 VE_BAD_FILE, kTraceError,
1757 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001758 output_file_player_->StopPlayingFile();
1759 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001760 return -1;
1761 }
kwiberg5a25d952016-08-17 07:31:12 -07001762 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001763 channel_state_.SetOutputFilePlaying(true);
1764 }
1765
1766 if (RegisterFilePlayingToMixer() != 0)
1767 return -1;
1768
1769 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001770}
1771
1772int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001773 FileFormats format,
1774 int startPosition,
1775 float volumeScaling,
1776 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001777 const CodecInst* codecInst) {
1778 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1779 "Channel::StartPlayingFileLocally(format=%d,"
1780 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1781 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001782
kwiberg55b97fe2016-01-28 05:22:45 -08001783 if (stream == NULL) {
1784 _engineStatisticsPtr->SetLastError(
1785 VE_BAD_FILE, kTraceError,
1786 "StartPlayingFileLocally() NULL as input stream");
1787 return -1;
1788 }
1789
1790 if (channel_state_.Get().output_file_playing) {
1791 _engineStatisticsPtr->SetLastError(
1792 VE_ALREADY_PLAYING, kTraceError,
1793 "StartPlayingFileLocally() is already playing");
1794 return -1;
1795 }
1796
1797 {
1798 rtc::CritScope cs(&_fileCritSect);
1799
1800 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001801 if (output_file_player_) {
1802 output_file_player_->RegisterModuleFileCallback(NULL);
1803 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001804 }
1805
kwiberg55b97fe2016-01-28 05:22:45 -08001806 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001807 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001808 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001809
kwiberg5a25d952016-08-17 07:31:12 -07001810 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001811 _engineStatisticsPtr->SetLastError(
1812 VE_INVALID_ARGUMENT, kTraceError,
1813 "StartPlayingFileLocally() filePlayer format isnot correct");
1814 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001815 }
1816
kwiberg55b97fe2016-01-28 05:22:45 -08001817 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001818
kwiberg4ec01d92016-08-22 08:43:54 -07001819 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001820 volumeScaling, notificationTime,
1821 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001822 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1823 "StartPlayingFile() failed to "
1824 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001825 output_file_player_->StopPlayingFile();
1826 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001827 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001828 }
kwiberg5a25d952016-08-17 07:31:12 -07001829 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001830 channel_state_.SetOutputFilePlaying(true);
1831 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001832
kwiberg55b97fe2016-01-28 05:22:45 -08001833 if (RegisterFilePlayingToMixer() != 0)
1834 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001835
kwiberg55b97fe2016-01-28 05:22:45 -08001836 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001837}
1838
kwiberg55b97fe2016-01-28 05:22:45 -08001839int Channel::StopPlayingFileLocally() {
1840 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1841 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001842
kwiberg55b97fe2016-01-28 05:22:45 -08001843 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001844 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001845 }
1846
1847 {
1848 rtc::CritScope cs(&_fileCritSect);
1849
kwiberg5a25d952016-08-17 07:31:12 -07001850 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001851 _engineStatisticsPtr->SetLastError(
1852 VE_STOP_RECORDING_FAILED, kTraceError,
1853 "StopPlayingFile() could not stop playing");
1854 return -1;
1855 }
kwiberg5a25d952016-08-17 07:31:12 -07001856 output_file_player_->RegisterModuleFileCallback(NULL);
1857 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001858 channel_state_.SetOutputFilePlaying(false);
1859 }
1860 // _fileCritSect cannot be taken while calling
1861 // SetAnonymousMixibilityStatus. Refer to comments in
1862 // StartPlayingFileLocally(const char* ...) for more details.
1863 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1864 _engineStatisticsPtr->SetLastError(
1865 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1866 "StopPlayingFile() failed to stop participant from playing as"
1867 "file in the mixer");
1868 return -1;
1869 }
1870
1871 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001872}
1873
kwiberg55b97fe2016-01-28 05:22:45 -08001874int Channel::IsPlayingFileLocally() const {
1875 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001876}
1877
kwiberg55b97fe2016-01-28 05:22:45 -08001878int Channel::RegisterFilePlayingToMixer() {
1879 // Return success for not registering for file playing to mixer if:
1880 // 1. playing file before playout is started on that channel.
1881 // 2. starting playout without file playing on that channel.
1882 if (!channel_state_.Get().playing ||
1883 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001884 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001885 }
1886
1887 // |_fileCritSect| cannot be taken while calling
1888 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1889 // frames can be pulled by the mixer. Since the frames are generated from
1890 // the file, _fileCritSect will be taken. This would result in a deadlock.
1891 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1892 channel_state_.SetOutputFilePlaying(false);
1893 rtc::CritScope cs(&_fileCritSect);
1894 _engineStatisticsPtr->SetLastError(
1895 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1896 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001897 output_file_player_->StopPlayingFile();
1898 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001899 return -1;
1900 }
1901
1902 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001903}
1904
niklase@google.com470e71d2011-07-07 08:21:25 +00001905int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001906 bool loop,
1907 FileFormats format,
1908 int startPosition,
1909 float volumeScaling,
1910 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001911 const CodecInst* codecInst) {
1912 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1913 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1914 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1915 "stopPosition=%d)",
1916 fileName, loop, format, volumeScaling, startPosition,
1917 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001918
kwiberg55b97fe2016-01-28 05:22:45 -08001919 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001920
kwiberg55b97fe2016-01-28 05:22:45 -08001921 if (channel_state_.Get().input_file_playing) {
1922 _engineStatisticsPtr->SetLastError(
1923 VE_ALREADY_PLAYING, kTraceWarning,
1924 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001925 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001926 }
1927
1928 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001929 if (input_file_player_) {
1930 input_file_player_->RegisterModuleFileCallback(NULL);
1931 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001932 }
1933
1934 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001935 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001936 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001937
kwiberg5a25d952016-08-17 07:31:12 -07001938 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001939 _engineStatisticsPtr->SetLastError(
1940 VE_INVALID_ARGUMENT, kTraceError,
1941 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1942 return -1;
1943 }
1944
1945 const uint32_t notificationTime(0);
1946
kwiberg5a25d952016-08-17 07:31:12 -07001947 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001948 fileName, loop, startPosition, volumeScaling, notificationTime,
1949 stopPosition, (const CodecInst*)codecInst) != 0) {
1950 _engineStatisticsPtr->SetLastError(
1951 VE_BAD_FILE, kTraceError,
1952 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001953 input_file_player_->StopPlayingFile();
1954 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001955 return -1;
1956 }
kwiberg5a25d952016-08-17 07:31:12 -07001957 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001958 channel_state_.SetInputFilePlaying(true);
1959
1960 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001961}
1962
1963int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001964 FileFormats format,
1965 int startPosition,
1966 float volumeScaling,
1967 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001968 const CodecInst* codecInst) {
1969 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1970 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1971 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1972 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001973
kwiberg55b97fe2016-01-28 05:22:45 -08001974 if (stream == NULL) {
1975 _engineStatisticsPtr->SetLastError(
1976 VE_BAD_FILE, kTraceError,
1977 "StartPlayingFileAsMicrophone NULL as input stream");
1978 return -1;
1979 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001980
kwiberg55b97fe2016-01-28 05:22:45 -08001981 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001982
kwiberg55b97fe2016-01-28 05:22:45 -08001983 if (channel_state_.Get().input_file_playing) {
1984 _engineStatisticsPtr->SetLastError(
1985 VE_ALREADY_PLAYING, kTraceWarning,
1986 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001987 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001988 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001989
kwiberg55b97fe2016-01-28 05:22:45 -08001990 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001991 if (input_file_player_) {
1992 input_file_player_->RegisterModuleFileCallback(NULL);
1993 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001994 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001995
kwiberg55b97fe2016-01-28 05:22:45 -08001996 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001997 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001998 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001999
kwiberg5a25d952016-08-17 07:31:12 -07002000 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002001 _engineStatisticsPtr->SetLastError(
2002 VE_INVALID_ARGUMENT, kTraceError,
2003 "StartPlayingInputFile() filePlayer format isnot correct");
2004 return -1;
2005 }
2006
2007 const uint32_t notificationTime(0);
2008
kwiberg4ec01d92016-08-22 08:43:54 -07002009 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2010 notificationTime, stopPosition,
2011 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002012 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2013 "StartPlayingFile() failed to start "
2014 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002015 input_file_player_->StopPlayingFile();
2016 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002017 return -1;
2018 }
2019
kwiberg5a25d952016-08-17 07:31:12 -07002020 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002021 channel_state_.SetInputFilePlaying(true);
2022
2023 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002024}
2025
kwiberg55b97fe2016-01-28 05:22:45 -08002026int Channel::StopPlayingFileAsMicrophone() {
2027 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2028 "Channel::StopPlayingFileAsMicrophone()");
2029
2030 rtc::CritScope cs(&_fileCritSect);
2031
2032 if (!channel_state_.Get().input_file_playing) {
2033 return 0;
2034 }
2035
kwiberg5a25d952016-08-17 07:31:12 -07002036 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002037 _engineStatisticsPtr->SetLastError(
2038 VE_STOP_RECORDING_FAILED, kTraceError,
2039 "StopPlayingFile() could not stop playing");
2040 return -1;
2041 }
kwiberg5a25d952016-08-17 07:31:12 -07002042 input_file_player_->RegisterModuleFileCallback(NULL);
2043 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002044 channel_state_.SetInputFilePlaying(false);
2045
2046 return 0;
2047}
2048
2049int Channel::IsPlayingFileAsMicrophone() const {
2050 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002051}
2052
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002053int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002054 const CodecInst* codecInst) {
2055 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2056 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002057
kwiberg55b97fe2016-01-28 05:22:45 -08002058 if (_outputFileRecording) {
2059 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2060 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002061 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002062 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002063
kwiberg55b97fe2016-01-28 05:22:45 -08002064 FileFormats format;
2065 const uint32_t notificationTime(0); // Not supported in VoE
2066 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002067
kwiberg55b97fe2016-01-28 05:22:45 -08002068 if ((codecInst != NULL) &&
2069 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2070 _engineStatisticsPtr->SetLastError(
2071 VE_BAD_ARGUMENT, kTraceError,
2072 "StartRecordingPlayout() invalid compression");
2073 return (-1);
2074 }
2075 if (codecInst == NULL) {
2076 format = kFileFormatPcm16kHzFile;
2077 codecInst = &dummyCodec;
2078 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2079 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2080 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2081 format = kFileFormatWavFile;
2082 } else {
2083 format = kFileFormatCompressedFile;
2084 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002085
kwiberg55b97fe2016-01-28 05:22:45 -08002086 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002087
kwiberg55b97fe2016-01-28 05:22:45 -08002088 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002089 if (output_file_recorder_) {
2090 output_file_recorder_->RegisterModuleFileCallback(NULL);
2091 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002092 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002093
kwiberg5a25d952016-08-17 07:31:12 -07002094 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002095 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002096 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002097 _engineStatisticsPtr->SetLastError(
2098 VE_INVALID_ARGUMENT, kTraceError,
2099 "StartRecordingPlayout() fileRecorder format isnot correct");
2100 return -1;
2101 }
2102
kwiberg5a25d952016-08-17 07:31:12 -07002103 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002104 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2105 _engineStatisticsPtr->SetLastError(
2106 VE_BAD_FILE, kTraceError,
2107 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002108 output_file_recorder_->StopRecording();
2109 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002110 return -1;
2111 }
kwiberg5a25d952016-08-17 07:31:12 -07002112 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002113 _outputFileRecording = true;
2114
2115 return 0;
2116}
2117
2118int Channel::StartRecordingPlayout(OutStream* stream,
2119 const CodecInst* codecInst) {
2120 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2121 "Channel::StartRecordingPlayout()");
2122
2123 if (_outputFileRecording) {
2124 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2125 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002126 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002127 }
2128
2129 FileFormats format;
2130 const uint32_t notificationTime(0); // Not supported in VoE
2131 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2132
2133 if (codecInst != NULL && codecInst->channels != 1) {
2134 _engineStatisticsPtr->SetLastError(
2135 VE_BAD_ARGUMENT, kTraceError,
2136 "StartRecordingPlayout() invalid compression");
2137 return (-1);
2138 }
2139 if (codecInst == NULL) {
2140 format = kFileFormatPcm16kHzFile;
2141 codecInst = &dummyCodec;
2142 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2143 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2144 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2145 format = kFileFormatWavFile;
2146 } else {
2147 format = kFileFormatCompressedFile;
2148 }
2149
2150 rtc::CritScope cs(&_fileCritSect);
2151
2152 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002153 if (output_file_recorder_) {
2154 output_file_recorder_->RegisterModuleFileCallback(NULL);
2155 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002156 }
2157
kwiberg5a25d952016-08-17 07:31:12 -07002158 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002159 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002160 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002161 _engineStatisticsPtr->SetLastError(
2162 VE_INVALID_ARGUMENT, kTraceError,
2163 "StartRecordingPlayout() fileRecorder format isnot correct");
2164 return -1;
2165 }
2166
kwiberg4ec01d92016-08-22 08:43:54 -07002167 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002168 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002169 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2170 "StartRecordingPlayout() failed to "
2171 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002172 output_file_recorder_->StopRecording();
2173 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002174 return -1;
2175 }
2176
kwiberg5a25d952016-08-17 07:31:12 -07002177 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002178 _outputFileRecording = true;
2179
2180 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002181}
2182
kwiberg55b97fe2016-01-28 05:22:45 -08002183int Channel::StopRecordingPlayout() {
2184 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2185 "Channel::StopRecordingPlayout()");
2186
2187 if (!_outputFileRecording) {
2188 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2189 "StopRecordingPlayout() isnot recording");
2190 return -1;
2191 }
2192
2193 rtc::CritScope cs(&_fileCritSect);
2194
kwiberg5a25d952016-08-17 07:31:12 -07002195 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002196 _engineStatisticsPtr->SetLastError(
2197 VE_STOP_RECORDING_FAILED, kTraceError,
2198 "StopRecording() could not stop recording");
2199 return (-1);
2200 }
kwiberg5a25d952016-08-17 07:31:12 -07002201 output_file_recorder_->RegisterModuleFileCallback(NULL);
2202 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002203 _outputFileRecording = false;
2204
2205 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002206}
2207
kwiberg55b97fe2016-01-28 05:22:45 -08002208void Channel::SetMixWithMicStatus(bool mix) {
2209 rtc::CritScope cs(&_fileCritSect);
2210 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002211}
2212
kwiberg55b97fe2016-01-28 05:22:45 -08002213int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2214 int8_t currentLevel = _outputAudioLevel.Level();
2215 level = static_cast<int32_t>(currentLevel);
2216 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002217}
2218
kwiberg55b97fe2016-01-28 05:22:45 -08002219int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2220 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2221 level = static_cast<int32_t>(currentLevel);
2222 return 0;
2223}
2224
solenberg1c2af8e2016-03-24 10:36:00 -07002225int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002226 rtc::CritScope cs(&volume_settings_critsect_);
2227 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002228 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002229 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002230 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002231}
2232
solenberg1c2af8e2016-03-24 10:36:00 -07002233bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002234 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002235 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002236}
2237
kwiberg55b97fe2016-01-28 05:22:45 -08002238int Channel::SetOutputVolumePan(float left, float right) {
2239 rtc::CritScope cs(&volume_settings_critsect_);
2240 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002241 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002242 _panLeft = left;
2243 _panRight = right;
2244 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002245}
2246
kwiberg55b97fe2016-01-28 05:22:45 -08002247int Channel::GetOutputVolumePan(float& left, float& right) const {
2248 rtc::CritScope cs(&volume_settings_critsect_);
2249 left = _panLeft;
2250 right = _panRight;
2251 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002252}
2253
kwiberg55b97fe2016-01-28 05:22:45 -08002254int Channel::SetChannelOutputVolumeScaling(float scaling) {
2255 rtc::CritScope cs(&volume_settings_critsect_);
2256 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002257 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002258 _outputGain = scaling;
2259 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002260}
2261
kwiberg55b97fe2016-01-28 05:22:45 -08002262int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2263 rtc::CritScope cs(&volume_settings_critsect_);
2264 scaling = _outputGain;
2265 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002266}
2267
solenberg8842c3e2016-03-11 03:06:41 -08002268int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002269 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002270 "Channel::SendTelephoneEventOutband(...)");
2271 RTC_DCHECK_LE(0, event);
2272 RTC_DCHECK_GE(255, event);
2273 RTC_DCHECK_LE(0, duration_ms);
2274 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002275 if (!Sending()) {
2276 return -1;
2277 }
solenberg8842c3e2016-03-11 03:06:41 -08002278 if (_rtpRtcpModule->SendTelephoneEventOutband(
2279 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002280 _engineStatisticsPtr->SetLastError(
2281 VE_SEND_DTMF_FAILED, kTraceWarning,
2282 "SendTelephoneEventOutband() failed to send event");
2283 return -1;
2284 }
2285 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002286}
2287
solenbergffbbcac2016-11-17 05:25:37 -08002288int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2289 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002290 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002291 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002292 RTC_DCHECK_LE(0, payload_type);
2293 RTC_DCHECK_GE(127, payload_type);
2294 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002295 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002296 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002297 memcpy(codec.plname, "telephone-event", 16);
2298 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2299 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2300 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2301 _engineStatisticsPtr->SetLastError(
2302 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2303 "SetSendTelephoneEventPayloadType() failed to register send"
2304 "payload type");
2305 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002306 }
kwiberg55b97fe2016-01-28 05:22:45 -08002307 }
kwiberg55b97fe2016-01-28 05:22:45 -08002308 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002309}
2310
kwiberg55b97fe2016-01-28 05:22:45 -08002311int Channel::VoiceActivityIndicator(int& activity) {
2312 activity = _sendFrameType;
2313 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002314}
2315
kwiberg55b97fe2016-01-28 05:22:45 -08002316int Channel::SetLocalSSRC(unsigned int ssrc) {
2317 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2318 "Channel::SetLocalSSRC()");
2319 if (channel_state_.Get().sending) {
2320 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2321 "SetLocalSSRC() already sending");
2322 return -1;
2323 }
2324 _rtpRtcpModule->SetSSRC(ssrc);
2325 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002326}
2327
kwiberg55b97fe2016-01-28 05:22:45 -08002328int Channel::GetLocalSSRC(unsigned int& ssrc) {
2329 ssrc = _rtpRtcpModule->SSRC();
2330 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002331}
2332
kwiberg55b97fe2016-01-28 05:22:45 -08002333int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2334 ssrc = rtp_receiver_->SSRC();
2335 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002336}
2337
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002338int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002339 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002340 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002341}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002342
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002343int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2344 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002345 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2346 if (enable &&
2347 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2348 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002349 return -1;
2350 }
2351 return 0;
2352}
2353
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002354void Channel::EnableSendTransportSequenceNumber(int id) {
2355 int ret =
2356 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2357 RTC_DCHECK_EQ(0, ret);
2358}
2359
stefan3313ec92016-01-21 06:32:43 -08002360void Channel::EnableReceiveTransportSequenceNumber(int id) {
2361 rtp_header_parser_->DeregisterRtpHeaderExtension(
2362 kRtpExtensionTransportSequenceNumber);
2363 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2364 kRtpExtensionTransportSequenceNumber, id);
2365 RTC_DCHECK(ret);
2366}
2367
stefanbba9dec2016-02-01 04:39:55 -08002368void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002369 RtpPacketSender* rtp_packet_sender,
2370 TransportFeedbackObserver* transport_feedback_observer,
2371 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002372 RTC_DCHECK(rtp_packet_sender);
2373 RTC_DCHECK(transport_feedback_observer);
2374 RTC_DCHECK(packet_router && !packet_router_);
2375 feedback_observer_proxy_->SetTransportFeedbackObserver(
2376 transport_feedback_observer);
2377 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2378 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2379 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002380 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002381 packet_router_ = packet_router;
2382}
2383
stefanbba9dec2016-02-01 04:39:55 -08002384void Channel::RegisterReceiverCongestionControlObjects(
2385 PacketRouter* packet_router) {
2386 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002387 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002388 packet_router_ = packet_router;
2389}
2390
2391void Channel::ResetCongestionControlObjects() {
2392 RTC_DCHECK(packet_router_);
2393 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2394 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2395 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002396 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002397 packet_router_ = nullptr;
2398 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2399}
2400
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002401void Channel::SetRTCPStatus(bool enable) {
2402 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2403 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002404 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002405}
2406
kwiberg55b97fe2016-01-28 05:22:45 -08002407int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002408 RtcpMode method = _rtpRtcpModule->RTCP();
2409 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002410 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002411}
2412
kwiberg55b97fe2016-01-28 05:22:45 -08002413int Channel::SetRTCP_CNAME(const char cName[256]) {
2414 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2415 "Channel::SetRTCP_CNAME()");
2416 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2417 _engineStatisticsPtr->SetLastError(
2418 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2419 "SetRTCP_CNAME() failed to set RTCP CNAME");
2420 return -1;
2421 }
2422 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002423}
2424
kwiberg55b97fe2016-01-28 05:22:45 -08002425int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2426 if (cName == NULL) {
2427 _engineStatisticsPtr->SetLastError(
2428 VE_INVALID_ARGUMENT, kTraceError,
2429 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2430 return -1;
2431 }
2432 char cname[RTCP_CNAME_SIZE];
2433 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2434 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2435 _engineStatisticsPtr->SetLastError(
2436 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2437 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2438 return -1;
2439 }
2440 strcpy(cName, cname);
2441 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002442}
2443
kwiberg55b97fe2016-01-28 05:22:45 -08002444int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2445 unsigned int& NTPLow,
2446 unsigned int& timestamp,
2447 unsigned int& playoutTimestamp,
2448 unsigned int* jitter,
2449 unsigned short* fractionLost) {
2450 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002451
kwiberg55b97fe2016-01-28 05:22:45 -08002452 RTCPSenderInfo senderInfo;
2453 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2454 _engineStatisticsPtr->SetLastError(
2455 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2456 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2457 "side");
2458 return -1;
2459 }
2460
2461 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2462 // and octet count)
2463 NTPHigh = senderInfo.NTPseconds;
2464 NTPLow = senderInfo.NTPfraction;
2465 timestamp = senderInfo.RTPtimeStamp;
2466
2467 // --- Locally derived information
2468
2469 // This value is updated on each incoming RTCP packet (0 when no packet
2470 // has been received)
2471 playoutTimestamp = playout_timestamp_rtcp_;
2472
2473 if (NULL != jitter || NULL != fractionLost) {
2474 // Get all RTCP receiver report blocks that have been received on this
2475 // channel. If we receive RTP packets from a remote source we know the
2476 // remote SSRC and use the report block from him.
2477 // Otherwise use the first report block.
2478 std::vector<RTCPReportBlock> remote_stats;
2479 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2480 remote_stats.empty()) {
2481 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2482 "GetRemoteRTCPData() failed to measure statistics due"
2483 " to lack of received RTP and/or RTCP packets");
2484 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002485 }
2486
kwiberg55b97fe2016-01-28 05:22:45 -08002487 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2488 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2489 for (; it != remote_stats.end(); ++it) {
2490 if (it->remoteSSRC == remoteSSRC)
2491 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002492 }
kwiberg55b97fe2016-01-28 05:22:45 -08002493
2494 if (it == remote_stats.end()) {
2495 // If we have not received any RTCP packets from this SSRC it probably
2496 // means that we have not received any RTP packets.
2497 // Use the first received report block instead.
2498 it = remote_stats.begin();
2499 remoteSSRC = it->remoteSSRC;
2500 }
2501
2502 if (jitter) {
2503 *jitter = it->jitter;
2504 }
2505
2506 if (fractionLost) {
2507 *fractionLost = it->fractionLost;
2508 }
2509 }
2510 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002511}
2512
kwiberg55b97fe2016-01-28 05:22:45 -08002513int Channel::SendApplicationDefinedRTCPPacket(
2514 unsigned char subType,
2515 unsigned int name,
2516 const char* data,
2517 unsigned short dataLengthInBytes) {
2518 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2519 "Channel::SendApplicationDefinedRTCPPacket()");
2520 if (!channel_state_.Get().sending) {
2521 _engineStatisticsPtr->SetLastError(
2522 VE_NOT_SENDING, kTraceError,
2523 "SendApplicationDefinedRTCPPacket() not sending");
2524 return -1;
2525 }
2526 if (NULL == data) {
2527 _engineStatisticsPtr->SetLastError(
2528 VE_INVALID_ARGUMENT, kTraceError,
2529 "SendApplicationDefinedRTCPPacket() invalid data value");
2530 return -1;
2531 }
2532 if (dataLengthInBytes % 4 != 0) {
2533 _engineStatisticsPtr->SetLastError(
2534 VE_INVALID_ARGUMENT, kTraceError,
2535 "SendApplicationDefinedRTCPPacket() invalid length value");
2536 return -1;
2537 }
2538 RtcpMode status = _rtpRtcpModule->RTCP();
2539 if (status == RtcpMode::kOff) {
2540 _engineStatisticsPtr->SetLastError(
2541 VE_RTCP_ERROR, kTraceError,
2542 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2543 return -1;
2544 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002545
kwiberg55b97fe2016-01-28 05:22:45 -08002546 // Create and schedule the RTCP APP packet for transmission
2547 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2548 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2549 _engineStatisticsPtr->SetLastError(
2550 VE_SEND_ERROR, kTraceError,
2551 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2552 return -1;
2553 }
2554 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002555}
2556
kwiberg55b97fe2016-01-28 05:22:45 -08002557int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2558 unsigned int& maxJitterMs,
2559 unsigned int& discardedPackets) {
2560 // The jitter statistics is updated for each received RTP packet and is
2561 // based on received packets.
2562 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2563 // If RTCP is off, there is no timed thread in the RTCP module regularly
2564 // generating new stats, trigger the update manually here instead.
2565 StreamStatistician* statistician =
2566 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2567 if (statistician) {
2568 // Don't use returned statistics, use data from proxy instead so that
2569 // max jitter can be fetched atomically.
2570 RtcpStatistics s;
2571 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002572 }
kwiberg55b97fe2016-01-28 05:22:45 -08002573 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002574
kwiberg55b97fe2016-01-28 05:22:45 -08002575 ChannelStatistics stats = statistics_proxy_->GetStats();
2576 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2577 if (playoutFrequency > 0) {
2578 // Scale RTP statistics given the current playout frequency
2579 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2580 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2581 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002582
kwiberg55b97fe2016-01-28 05:22:45 -08002583 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002584
kwiberg55b97fe2016-01-28 05:22:45 -08002585 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002586}
2587
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002588int Channel::GetRemoteRTCPReportBlocks(
2589 std::vector<ReportBlock>* report_blocks) {
2590 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002591 _engineStatisticsPtr->SetLastError(
2592 VE_INVALID_ARGUMENT, kTraceError,
2593 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002594 return -1;
2595 }
2596
2597 // Get the report blocks from the latest received RTCP Sender or Receiver
2598 // Report. Each element in the vector contains the sender's SSRC and a
2599 // report block according to RFC 3550.
2600 std::vector<RTCPReportBlock> rtcp_report_blocks;
2601 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002602 return -1;
2603 }
2604
2605 if (rtcp_report_blocks.empty())
2606 return 0;
2607
2608 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2609 for (; it != rtcp_report_blocks.end(); ++it) {
2610 ReportBlock report_block;
2611 report_block.sender_SSRC = it->remoteSSRC;
2612 report_block.source_SSRC = it->sourceSSRC;
2613 report_block.fraction_lost = it->fractionLost;
2614 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2615 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2616 report_block.interarrival_jitter = it->jitter;
2617 report_block.last_SR_timestamp = it->lastSR;
2618 report_block.delay_since_last_SR = it->delaySinceLastSR;
2619 report_blocks->push_back(report_block);
2620 }
2621 return 0;
2622}
2623
kwiberg55b97fe2016-01-28 05:22:45 -08002624int Channel::GetRTPStatistics(CallStatistics& stats) {
2625 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002626
kwiberg55b97fe2016-01-28 05:22:45 -08002627 // The jitter statistics is updated for each received RTP packet and is
2628 // based on received packets.
2629 RtcpStatistics statistics;
2630 StreamStatistician* statistician =
2631 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002632 if (statistician) {
2633 statistician->GetStatistics(&statistics,
2634 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002635 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002636
kwiberg55b97fe2016-01-28 05:22:45 -08002637 stats.fractionLost = statistics.fraction_lost;
2638 stats.cumulativeLost = statistics.cumulative_lost;
2639 stats.extendedMax = statistics.extended_max_sequence_number;
2640 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002641
kwiberg55b97fe2016-01-28 05:22:45 -08002642 // --- RTT
2643 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002644
kwiberg55b97fe2016-01-28 05:22:45 -08002645 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002646
kwiberg55b97fe2016-01-28 05:22:45 -08002647 size_t bytesSent(0);
2648 uint32_t packetsSent(0);
2649 size_t bytesReceived(0);
2650 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002651
kwiberg55b97fe2016-01-28 05:22:45 -08002652 if (statistician) {
2653 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2654 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002655
kwiberg55b97fe2016-01-28 05:22:45 -08002656 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2657 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2658 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2659 " output will not be complete");
2660 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002661
kwiberg55b97fe2016-01-28 05:22:45 -08002662 stats.bytesSent = bytesSent;
2663 stats.packetsSent = packetsSent;
2664 stats.bytesReceived = bytesReceived;
2665 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002666
kwiberg55b97fe2016-01-28 05:22:45 -08002667 // --- Timestamps
2668 {
2669 rtc::CritScope lock(&ts_stats_lock_);
2670 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2671 }
2672 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002673}
2674
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002675int Channel::SetCodecFECStatus(bool enable) {
2676 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2677 "Channel::SetCodecFECStatus()");
2678
kwibergc8d071e2016-04-06 12:22:38 -07002679 if (!codec_manager_.SetCodecFEC(enable) ||
2680 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002681 _engineStatisticsPtr->SetLastError(
2682 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2683 "SetCodecFECStatus() failed to set FEC state");
2684 return -1;
2685 }
2686 return 0;
2687}
2688
2689bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002690 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002691}
2692
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002693void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2694 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002695 // If pacing is enabled we always store packets.
2696 if (!pacing_enabled_)
2697 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002698 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002699 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002700 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002701 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002702 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002703}
2704
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002705// Called when we are missing one or more packets.
2706int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002707 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2708}
2709
kwiberg55b97fe2016-01-28 05:22:45 -08002710uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2711 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2712 "Channel::Demultiplex()");
2713 _audioFrame.CopyFrom(audioFrame);
2714 _audioFrame.id_ = _channelId;
2715 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002716}
2717
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002718void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002719 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002720 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002721 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002722 CodecInst codec;
2723 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002724
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002725 // Never upsample or upmix the capture signal here. This should be done at the
2726 // end of the send chain.
2727 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2728 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2729 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2730 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002731}
2732
kwiberg55b97fe2016-01-28 05:22:45 -08002733uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2734 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2735 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002736
kwiberg55b97fe2016-01-28 05:22:45 -08002737 if (_audioFrame.samples_per_channel_ == 0) {
2738 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2739 "Channel::PrepareEncodeAndSend() invalid audio frame");
2740 return 0xFFFFFFFF;
2741 }
2742
2743 if (channel_state_.Get().input_file_playing) {
2744 MixOrReplaceAudioWithFile(mixingFrequency);
2745 }
2746
solenberg1c2af8e2016-03-24 10:36:00 -07002747 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2748 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002749
2750 if (channel_state_.Get().input_external_media) {
2751 rtc::CritScope cs(&_callbackCritSect);
2752 const bool isStereo = (_audioFrame.num_channels_ == 2);
2753 if (_inputExternalMediaCallbackPtr) {
2754 _inputExternalMediaCallbackPtr->Process(
2755 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
2756 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
2757 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00002758 }
kwiberg55b97fe2016-01-28 05:22:45 -08002759 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002760
kwiberg55b97fe2016-01-28 05:22:45 -08002761 if (_includeAudioLevelIndication) {
2762 size_t length =
2763 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002764 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002765 if (is_muted && previous_frame_muted_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002766 rms_level_.ProcessMuted(length);
2767 } else {
2768 rms_level_.Process(_audioFrame.data_, length);
niklase@google.com470e71d2011-07-07 08:21:25 +00002769 }
kwiberg55b97fe2016-01-28 05:22:45 -08002770 }
solenberg1c2af8e2016-03-24 10:36:00 -07002771 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002772
kwiberg55b97fe2016-01-28 05:22:45 -08002773 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002774}
2775
kwiberg55b97fe2016-01-28 05:22:45 -08002776uint32_t Channel::EncodeAndSend() {
2777 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2778 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002779
kwiberg55b97fe2016-01-28 05:22:45 -08002780 assert(_audioFrame.num_channels_ <= 2);
2781 if (_audioFrame.samples_per_channel_ == 0) {
2782 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2783 "Channel::EncodeAndSend() invalid audio frame");
2784 return 0xFFFFFFFF;
2785 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002786
kwiberg55b97fe2016-01-28 05:22:45 -08002787 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002788
kwiberg55b97fe2016-01-28 05:22:45 -08002789 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002790
kwiberg55b97fe2016-01-28 05:22:45 -08002791 // The ACM resamples internally.
2792 _audioFrame.timestamp_ = _timeStamp;
2793 // This call will trigger AudioPacketizationCallback::SendData if encoding
2794 // is done and payload is ready for packetization and transmission.
2795 // Otherwise, it will return without invoking the callback.
2796 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2797 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2798 "Channel::EncodeAndSend() ACM encoding failed");
2799 return 0xFFFFFFFF;
2800 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002801
kwiberg55b97fe2016-01-28 05:22:45 -08002802 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2803 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002804}
2805
solenberg7602aab2016-11-14 11:30:07 -08002806void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2807 RTC_DCHECK(!channel.channel() ||
2808 channel.channel()->ChannelId() != _channelId);
2809 rtc::CritScope lock(&assoc_send_channel_lock_);
2810 associate_send_channel_ = channel;
2811}
2812
Minyue2013aec2015-05-13 14:14:42 +02002813void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002814 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002815 Channel* channel = associate_send_channel_.channel();
2816 if (channel && channel->ChannelId() == channel_id) {
2817 // If this channel is associated with a send channel of the specified
2818 // Channel ID, disassociate with it.
2819 ChannelOwner ref(NULL);
2820 associate_send_channel_ = ref;
2821 }
2822}
2823
ivoc14d5dbe2016-07-04 07:06:55 -07002824void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2825 event_log_proxy_->SetEventLog(event_log);
2826}
2827
michaelt79e05882016-11-08 02:50:09 -08002828void Channel::SetTransportOverhead(int transport_overhead_per_packet) {
2829 _rtpRtcpModule->SetTransportOverhead(transport_overhead_per_packet);
2830}
2831
kwiberg55b97fe2016-01-28 05:22:45 -08002832int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
2833 VoEMediaProcess& processObject) {
2834 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2835 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002836
kwiberg55b97fe2016-01-28 05:22:45 -08002837 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002838
kwiberg55b97fe2016-01-28 05:22:45 -08002839 if (kPlaybackPerChannel == type) {
2840 if (_outputExternalMediaCallbackPtr) {
2841 _engineStatisticsPtr->SetLastError(
2842 VE_INVALID_OPERATION, kTraceError,
2843 "Channel::RegisterExternalMediaProcessing() "
2844 "output external media already enabled");
2845 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002846 }
kwiberg55b97fe2016-01-28 05:22:45 -08002847 _outputExternalMediaCallbackPtr = &processObject;
2848 _outputExternalMedia = true;
2849 } else if (kRecordingPerChannel == type) {
2850 if (_inputExternalMediaCallbackPtr) {
2851 _engineStatisticsPtr->SetLastError(
2852 VE_INVALID_OPERATION, kTraceError,
2853 "Channel::RegisterExternalMediaProcessing() "
2854 "output external media already enabled");
2855 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002856 }
kwiberg55b97fe2016-01-28 05:22:45 -08002857 _inputExternalMediaCallbackPtr = &processObject;
2858 channel_state_.SetInputExternalMedia(true);
2859 }
2860 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002861}
2862
kwiberg55b97fe2016-01-28 05:22:45 -08002863int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
2864 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2865 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002866
kwiberg55b97fe2016-01-28 05:22:45 -08002867 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002868
kwiberg55b97fe2016-01-28 05:22:45 -08002869 if (kPlaybackPerChannel == type) {
2870 if (!_outputExternalMediaCallbackPtr) {
2871 _engineStatisticsPtr->SetLastError(
2872 VE_INVALID_OPERATION, kTraceWarning,
2873 "Channel::DeRegisterExternalMediaProcessing() "
2874 "output external media already disabled");
2875 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002876 }
kwiberg55b97fe2016-01-28 05:22:45 -08002877 _outputExternalMedia = false;
2878 _outputExternalMediaCallbackPtr = NULL;
2879 } else if (kRecordingPerChannel == type) {
2880 if (!_inputExternalMediaCallbackPtr) {
2881 _engineStatisticsPtr->SetLastError(
2882 VE_INVALID_OPERATION, kTraceWarning,
2883 "Channel::DeRegisterExternalMediaProcessing() "
2884 "input external media already disabled");
2885 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002886 }
kwiberg55b97fe2016-01-28 05:22:45 -08002887 channel_state_.SetInputExternalMedia(false);
2888 _inputExternalMediaCallbackPtr = NULL;
2889 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002890
kwiberg55b97fe2016-01-28 05:22:45 -08002891 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002892}
2893
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002894int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08002895 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2896 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002897
kwiberg55b97fe2016-01-28 05:22:45 -08002898 if (channel_state_.Get().playing) {
2899 _engineStatisticsPtr->SetLastError(
2900 VE_INVALID_OPERATION, kTraceError,
2901 "Channel::SetExternalMixing() "
2902 "external mixing cannot be changed while playing.");
2903 return -1;
2904 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002905
kwiberg55b97fe2016-01-28 05:22:45 -08002906 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002907
kwiberg55b97fe2016-01-28 05:22:45 -08002908 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002909}
2910
kwiberg55b97fe2016-01-28 05:22:45 -08002911int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2912 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002913}
2914
wu@webrtc.org24301a62013-12-13 19:17:43 +00002915void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2916 audio_coding_->GetDecodingCallStatistics(stats);
2917}
2918
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002919bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
2920 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08002921 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07002922 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002923 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002924 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002925}
2926
solenberg358057b2015-11-27 10:46:42 -08002927uint32_t Channel::GetDelayEstimate() const {
2928 int jitter_buffer_delay_ms = 0;
2929 int playout_buffer_delay_ms = 0;
2930 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
2931 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
2932}
2933
deadbeef74375882015-08-13 12:09:10 -07002934int Channel::LeastRequiredDelayMs() const {
2935 return audio_coding_->LeastRequiredDelayMs();
2936}
2937
kwiberg55b97fe2016-01-28 05:22:45 -08002938int Channel::SetMinimumPlayoutDelay(int delayMs) {
2939 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2940 "Channel::SetMinimumPlayoutDelay()");
2941 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2942 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2943 _engineStatisticsPtr->SetLastError(
2944 VE_INVALID_ARGUMENT, kTraceError,
2945 "SetMinimumPlayoutDelay() invalid min delay");
2946 return -1;
2947 }
2948 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2949 _engineStatisticsPtr->SetLastError(
2950 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2951 "SetMinimumPlayoutDelay() failed to set min playout delay");
2952 return -1;
2953 }
2954 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002955}
2956
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002957int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002958 uint32_t playout_timestamp_rtp = 0;
2959 {
tommi31fc21f2016-01-21 10:37:37 -08002960 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002961 playout_timestamp_rtp = playout_timestamp_rtp_;
2962 }
kwiberg55b97fe2016-01-28 05:22:45 -08002963 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002964 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002965 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002966 "GetPlayoutTimestamp() failed to retrieve timestamp");
2967 return -1;
2968 }
deadbeef74375882015-08-13 12:09:10 -07002969 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002970 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002971}
2972
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002973int Channel::SetInitTimestamp(unsigned int timestamp) {
2974 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002975 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002976 if (channel_state_.Get().sending) {
2977 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
2978 "SetInitTimestamp() already sending");
2979 return -1;
2980 }
2981 _rtpRtcpModule->SetStartTimestamp(timestamp);
2982 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002983}
2984
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002985int Channel::SetInitSequenceNumber(short sequenceNumber) {
2986 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2987 "Channel::SetInitSequenceNumber()");
2988 if (channel_state_.Get().sending) {
2989 _engineStatisticsPtr->SetLastError(
2990 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
2991 return -1;
2992 }
2993 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
2994 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002995}
2996
kwiberg55b97fe2016-01-28 05:22:45 -08002997int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2998 RtpReceiver** rtp_receiver) const {
2999 *rtpRtcpModule = _rtpRtcpModule.get();
3000 *rtp_receiver = rtp_receiver_.get();
3001 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003002}
3003
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003004// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3005// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08003006int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08003007 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08003008 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003009
kwiberg55b97fe2016-01-28 05:22:45 -08003010 {
3011 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003012
kwiberg5a25d952016-08-17 07:31:12 -07003013 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003014 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3015 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3016 " doesnt exist");
3017 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003018 }
3019
kwiberg4ec01d92016-08-22 08:43:54 -07003020 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07003021 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003022 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3023 "Channel::MixOrReplaceAudioWithFile() file mixing "
3024 "failed");
3025 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003026 }
kwiberg55b97fe2016-01-28 05:22:45 -08003027 if (fileSamples == 0) {
3028 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3029 "Channel::MixOrReplaceAudioWithFile() file is ended");
3030 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003031 }
kwiberg55b97fe2016-01-28 05:22:45 -08003032 }
3033
3034 assert(_audioFrame.samples_per_channel_ == fileSamples);
3035
3036 if (_mixFileWithMicrophone) {
3037 // Currently file stream is always mono.
3038 // TODO(xians): Change the code when FilePlayer supports real stereo.
3039 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3040 1, fileSamples);
3041 } else {
3042 // Replace ACM audio with file.
3043 // Currently file stream is always mono.
3044 // TODO(xians): Change the code when FilePlayer supports real stereo.
3045 _audioFrame.UpdateFrame(
3046 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3047 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3048 }
3049 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003050}
3051
kwiberg55b97fe2016-01-28 05:22:45 -08003052int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3053 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003054
kwibergb7f89d62016-02-17 10:04:18 -08003055 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003056 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003057
kwiberg55b97fe2016-01-28 05:22:45 -08003058 {
3059 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003060
kwiberg5a25d952016-08-17 07:31:12 -07003061 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003062 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3063 "Channel::MixAudioWithFile() file mixing failed");
3064 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003065 }
3066
kwiberg55b97fe2016-01-28 05:22:45 -08003067 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003068 if (output_file_player_->Get10msAudioFromFile(
3069 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003070 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3071 "Channel::MixAudioWithFile() file mixing failed");
3072 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003073 }
kwiberg55b97fe2016-01-28 05:22:45 -08003074 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003075
kwiberg55b97fe2016-01-28 05:22:45 -08003076 if (audioFrame.samples_per_channel_ == fileSamples) {
3077 // Currently file stream is always mono.
3078 // TODO(xians): Change the code when FilePlayer supports real stereo.
3079 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3080 fileSamples);
3081 } else {
3082 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3083 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3084 ") != "
3085 "fileSamples(%" PRIuS ")",
3086 audioFrame.samples_per_channel_, fileSamples);
3087 return -1;
3088 }
3089
3090 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003091}
3092
deadbeef74375882015-08-13 12:09:10 -07003093void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003094 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003095
henrik.lundin96bd5022016-04-06 04:13:56 -07003096 if (!jitter_buffer_playout_timestamp_) {
3097 // This can happen if this channel has not received any RTP packets. In
3098 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003099 return;
3100 }
3101
3102 uint16_t delay_ms = 0;
3103 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003104 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003105 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3106 " delay from the ADM");
3107 _engineStatisticsPtr->SetLastError(
3108 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3109 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3110 return;
3111 }
3112
henrik.lundin96bd5022016-04-06 04:13:56 -07003113 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3114 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003115
3116 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003117 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003118
kwiberg55b97fe2016-01-28 05:22:45 -08003119 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003120 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003121 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003122
3123 {
tommi31fc21f2016-01-21 10:37:37 -08003124 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003125 if (rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003126 playout_timestamp_rtcp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003127 } else {
henrik.lundin96bd5022016-04-06 04:13:56 -07003128 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003129 }
3130 playout_delay_ms_ = delay_ms;
3131 }
3132}
3133
kwiberg55b97fe2016-01-28 05:22:45 -08003134void Channel::RegisterReceiveCodecsToRTPModule() {
3135 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3136 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003137
kwiberg55b97fe2016-01-28 05:22:45 -08003138 CodecInst codec;
3139 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003140
kwiberg55b97fe2016-01-28 05:22:45 -08003141 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3142 // Open up the RTP/RTCP receiver for all supported codecs
3143 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08003144 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08003145 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3146 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3147 " to register %s (%d/%d/%" PRIuS
3148 "/%d) to RTP/RTCP "
3149 "receiver",
3150 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3151 codec.rate);
3152 } else {
3153 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3154 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3155 "(%d/%d/%" PRIuS
3156 "/%d) has been added to the RTP/RTCP "
3157 "receiver",
3158 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3159 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003160 }
kwiberg55b97fe2016-01-28 05:22:45 -08003161 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003162}
3163
kwiberg55b97fe2016-01-28 05:22:45 -08003164int Channel::SetSendRtpHeaderExtension(bool enable,
3165 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003166 unsigned char id) {
3167 int error = 0;
3168 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3169 if (enable) {
3170 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3171 }
3172 return error;
3173}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003174
ossue280cde2016-10-12 11:04:10 -07003175int Channel::GetRtpTimestampRateHz() const {
3176 const auto format = audio_coding_->ReceiveFormat();
3177 // Default to the playout frequency if we've not gotten any packets yet.
3178 // TODO(ossu): Zero clockrate can only happen if we've added an external
3179 // decoder for a format we don't support internally. Remove once that way of
3180 // adding decoders is gone!
3181 return (format && format->clockrate_hz != 0)
3182 ? format->clockrate_hz
3183 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003184}
3185
Minyue2013aec2015-05-13 14:14:42 +02003186int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003187 RtcpMode method = _rtpRtcpModule->RTCP();
3188 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003189 return 0;
3190 }
3191 std::vector<RTCPReportBlock> report_blocks;
3192 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003193
3194 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003195 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003196 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003197 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003198 Channel* channel = associate_send_channel_.channel();
3199 // Tries to get RTT from an associated channel. This is important for
3200 // receive-only channels.
3201 if (channel) {
3202 // To prevent infinite recursion and deadlock, calling GetRTT of
3203 // associate channel should always use "false" for argument:
3204 // |allow_associate_channel|.
3205 rtt = channel->GetRTT(false);
3206 }
3207 }
3208 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003209 }
3210
3211 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3212 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3213 for (; it != report_blocks.end(); ++it) {
3214 if (it->remoteSSRC == remoteSSRC)
3215 break;
3216 }
3217 if (it == report_blocks.end()) {
3218 // We have not received packets with SSRC matching the report blocks.
3219 // To calculate RTT we try with the SSRC of the first report block.
3220 // This is very important for send-only channels where we don't know
3221 // the SSRC of the other end.
3222 remoteSSRC = report_blocks[0].remoteSSRC;
3223 }
Minyue2013aec2015-05-13 14:14:42 +02003224
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003225 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003226 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003227 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003228 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3229 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003230 return 0;
3231 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003232 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003233}
3234
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003235} // namespace voe
3236} // namespace webrtc