blob: deea05772716abfaabadcf0725ece599ace6cb36 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrika@webrtc.org2919e952012-01-31 08:45:03 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000011#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Henrik Lundin64dad832015-05-11 12:44:23 +020013#include <algorithm>
Tommif888bb52015-12-12 01:37:01 +010014#include <utility>
Henrik Lundin64dad832015-05-11 12:44:23 +020015
Ivo Creusenae856f22015-09-17 16:30:16 +020016#include "webrtc/base/checks.h"
tommi31fc21f2016-01-21 10:37:37 -080017#include "webrtc/base/criticalsection.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000018#include "webrtc/base/format_macros.h"
pbosad856222015-11-27 09:48:36 -080019#include "webrtc/base/logging.h"
Erik Språng737336d2016-07-29 12:59:36 +020020#include "webrtc/base/rate_limiter.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010021#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000022#include "webrtc/base/timeutils.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020023#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070024#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070025#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000026#include "webrtc/modules/audio_device/include/audio_device.h"
27#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010028#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010029#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
31#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
32#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000033#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010034#include "webrtc/modules/utility/include/audio_frame_operations.h"
35#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010036#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000037#include "webrtc/voice_engine/include/voe_external_media.h"
38#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
39#include "webrtc/voice_engine/output_mixer.h"
40#include "webrtc/voice_engine/statistics.h"
41#include "webrtc/voice_engine/transmit_mixer.h"
42#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000043
andrew@webrtc.org50419b02012-11-14 19:07:54 +000044namespace webrtc {
45namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000046
kwibergc8d071e2016-04-06 12:22:38 -070047namespace {
48
Erik Språng737336d2016-07-29 12:59:36 +020049constexpr int64_t kMaxRetransmissionWindowMs = 1000;
50constexpr int64_t kMinRetransmissionWindowMs = 30;
51
kwibergc8d071e2016-04-06 12:22:38 -070052} // namespace
53
solenberg8842c3e2016-03-11 03:06:41 -080054const int kTelephoneEventAttenuationdB = 10;
55
ivoc14d5dbe2016-07-04 07:06:55 -070056class RtcEventLogProxy final : public webrtc::RtcEventLog {
57 public:
58 RtcEventLogProxy() : event_log_(nullptr) {}
59
60 bool StartLogging(const std::string& file_name,
61 int64_t max_size_bytes) override {
62 RTC_NOTREACHED();
63 return false;
64 }
65
66 bool StartLogging(rtc::PlatformFile log_file,
67 int64_t max_size_bytes) override {
68 RTC_NOTREACHED();
69 return false;
70 }
71
72 void StopLogging() override { RTC_NOTREACHED(); }
73
74 void LogVideoReceiveStreamConfig(
75 const webrtc::VideoReceiveStream::Config& config) override {
76 rtc::CritScope lock(&crit_);
77 if (event_log_) {
78 event_log_->LogVideoReceiveStreamConfig(config);
79 }
80 }
81
82 void LogVideoSendStreamConfig(
83 const webrtc::VideoSendStream::Config& config) override {
84 rtc::CritScope lock(&crit_);
85 if (event_log_) {
86 event_log_->LogVideoSendStreamConfig(config);
87 }
88 }
89
ivoce0928d82016-10-10 05:12:51 -070090 void LogAudioReceiveStreamConfig(
91 const webrtc::AudioReceiveStream::Config& config) override {
92 rtc::CritScope lock(&crit_);
93 if (event_log_) {
94 event_log_->LogAudioReceiveStreamConfig(config);
95 }
96 }
97
98 void LogAudioSendStreamConfig(
99 const webrtc::AudioSendStream::Config& config) override {
100 rtc::CritScope lock(&crit_);
101 if (event_log_) {
102 event_log_->LogAudioSendStreamConfig(config);
103 }
104 }
105
ivoc14d5dbe2016-07-04 07:06:55 -0700106 void LogRtpHeader(webrtc::PacketDirection direction,
107 webrtc::MediaType media_type,
108 const uint8_t* header,
109 size_t packet_length) override {
110 rtc::CritScope lock(&crit_);
111 if (event_log_) {
112 event_log_->LogRtpHeader(direction, media_type, header, packet_length);
113 }
114 }
115
116 void LogRtcpPacket(webrtc::PacketDirection direction,
117 webrtc::MediaType media_type,
118 const uint8_t* packet,
119 size_t length) override {
120 rtc::CritScope lock(&crit_);
121 if (event_log_) {
122 event_log_->LogRtcpPacket(direction, media_type, packet, length);
123 }
124 }
125
126 void LogAudioPlayout(uint32_t ssrc) override {
127 rtc::CritScope lock(&crit_);
128 if (event_log_) {
129 event_log_->LogAudioPlayout(ssrc);
130 }
131 }
132
133 void LogBwePacketLossEvent(int32_t bitrate,
134 uint8_t fraction_loss,
135 int32_t total_packets) override {
136 rtc::CritScope lock(&crit_);
137 if (event_log_) {
138 event_log_->LogBwePacketLossEvent(bitrate, fraction_loss, total_packets);
139 }
140 }
141
142 void SetEventLog(RtcEventLog* event_log) {
143 rtc::CritScope lock(&crit_);
144 event_log_ = event_log;
145 }
146
147 private:
148 rtc::CriticalSection crit_;
149 RtcEventLog* event_log_ GUARDED_BY(crit_);
150 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
151};
152
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100153class TransportFeedbackProxy : public TransportFeedbackObserver {
154 public:
155 TransportFeedbackProxy() : feedback_observer_(nullptr) {
156 pacer_thread_.DetachFromThread();
157 network_thread_.DetachFromThread();
158 }
159
160 void SetTransportFeedbackObserver(
161 TransportFeedbackObserver* feedback_observer) {
162 RTC_DCHECK(thread_checker_.CalledOnValidThread());
163 rtc::CritScope lock(&crit_);
164 feedback_observer_ = feedback_observer;
165 }
166
167 // Implements TransportFeedbackObserver.
168 void AddPacket(uint16_t sequence_number,
169 size_t length,
philipela1ed0b32016-06-01 06:31:17 -0700170 int probe_cluster_id) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100171 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
172 rtc::CritScope lock(&crit_);
173 if (feedback_observer_)
pbos2169d8b2016-06-20 11:53:02 -0700174 feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100175 }
176 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
177 RTC_DCHECK(network_thread_.CalledOnValidThread());
178 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700179 if (feedback_observer_)
180 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200181 }
182 std::vector<PacketInfo> GetTransportFeedbackVector() const override {
183 RTC_NOTREACHED();
184 return std::vector<PacketInfo>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100185 }
186
187 private:
188 rtc::CriticalSection crit_;
189 rtc::ThreadChecker thread_checker_;
190 rtc::ThreadChecker pacer_thread_;
191 rtc::ThreadChecker network_thread_;
192 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
193};
194
195class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
196 public:
197 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
198 pacer_thread_.DetachFromThread();
199 }
200
201 void SetSequenceNumberAllocator(
202 TransportSequenceNumberAllocator* seq_num_allocator) {
203 RTC_DCHECK(thread_checker_.CalledOnValidThread());
204 rtc::CritScope lock(&crit_);
205 seq_num_allocator_ = seq_num_allocator;
206 }
207
208 // Implements TransportSequenceNumberAllocator.
209 uint16_t AllocateSequenceNumber() override {
210 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
211 rtc::CritScope lock(&crit_);
212 if (!seq_num_allocator_)
213 return 0;
214 return seq_num_allocator_->AllocateSequenceNumber();
215 }
216
217 private:
218 rtc::CriticalSection crit_;
219 rtc::ThreadChecker thread_checker_;
220 rtc::ThreadChecker pacer_thread_;
221 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
222};
223
224class RtpPacketSenderProxy : public RtpPacketSender {
225 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800226 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100227
228 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
229 RTC_DCHECK(thread_checker_.CalledOnValidThread());
230 rtc::CritScope lock(&crit_);
231 rtp_packet_sender_ = rtp_packet_sender;
232 }
233
234 // Implements RtpPacketSender.
235 void InsertPacket(Priority priority,
236 uint32_t ssrc,
237 uint16_t sequence_number,
238 int64_t capture_time_ms,
239 size_t bytes,
240 bool retransmission) override {
241 rtc::CritScope lock(&crit_);
242 if (rtp_packet_sender_) {
243 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
244 capture_time_ms, bytes, retransmission);
245 }
246 }
247
248 private:
249 rtc::ThreadChecker thread_checker_;
250 rtc::CriticalSection crit_;
251 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
252};
253
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000254// Extend the default RTCP statistics struct with max_jitter, defined as the
255// maximum jitter value seen in an RTCP report block.
256struct ChannelStatistics : public RtcpStatistics {
257 ChannelStatistics() : rtcp(), max_jitter(0) {}
258
259 RtcpStatistics rtcp;
260 uint32_t max_jitter;
261};
262
263// Statistics callback, called at each generation of a new RTCP report block.
264class StatisticsProxy : public RtcpStatisticsCallback {
265 public:
tommi31fc21f2016-01-21 10:37:37 -0800266 StatisticsProxy(uint32_t ssrc) : ssrc_(ssrc) {}
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000267 virtual ~StatisticsProxy() {}
268
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000269 void StatisticsUpdated(const RtcpStatistics& statistics,
270 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000271 if (ssrc != ssrc_)
272 return;
273
tommi31fc21f2016-01-21 10:37:37 -0800274 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000275 stats_.rtcp = statistics;
276 if (statistics.jitter > stats_.max_jitter) {
277 stats_.max_jitter = statistics.jitter;
278 }
279 }
280
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000281 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000282
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000283 ChannelStatistics GetStats() {
tommi31fc21f2016-01-21 10:37:37 -0800284 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000285 return stats_;
286 }
287
288 private:
289 // StatisticsUpdated calls are triggered from threads in the RTP module,
290 // while GetStats calls can be triggered from the public voice engine API,
291 // hence synchronization is needed.
tommi31fc21f2016-01-21 10:37:37 -0800292 rtc::CriticalSection stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000293 const uint32_t ssrc_;
294 ChannelStatistics stats_;
295};
296
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000297class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000298 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000299 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
300 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000301
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000302 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
303 // Not used for Voice Engine.
304 }
305
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000306 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
307 int64_t rtt,
308 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000309 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
310 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
311 // report for VoiceEngine?
312 if (report_blocks.empty())
313 return;
314
315 int fraction_lost_aggregate = 0;
316 int total_number_of_packets = 0;
317
318 // If receiving multiple report blocks, calculate the weighted average based
319 // on the number of packets a report refers to.
320 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
321 block_it != report_blocks.end(); ++block_it) {
322 // Find the previous extended high sequence number for this remote SSRC,
323 // to calculate the number of RTP packets this report refers to. Ignore if
324 // we haven't seen this SSRC before.
325 std::map<uint32_t, uint32_t>::iterator seq_num_it =
326 extended_max_sequence_number_.find(block_it->sourceSSRC);
327 int number_of_packets = 0;
328 if (seq_num_it != extended_max_sequence_number_.end()) {
329 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
330 }
331 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
332 total_number_of_packets += number_of_packets;
333
334 extended_max_sequence_number_[block_it->sourceSSRC] =
335 block_it->extendedHighSeqNum;
336 }
337 int weighted_fraction_lost = 0;
338 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800339 weighted_fraction_lost =
340 (fraction_lost_aggregate + total_number_of_packets / 2) /
341 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000342 }
343 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000344 }
345
346 private:
347 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000348 // Maps remote side ssrc to extended highest sequence number received.
349 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000350};
351
kwiberg55b97fe2016-01-28 05:22:45 -0800352int32_t Channel::SendData(FrameType frameType,
353 uint8_t payloadType,
354 uint32_t timeStamp,
355 const uint8_t* payloadData,
356 size_t payloadSize,
357 const RTPFragmentationHeader* fragmentation) {
358 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
359 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
360 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
361 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000362
kwiberg55b97fe2016-01-28 05:22:45 -0800363 if (_includeAudioLevelIndication) {
364 // Store current audio level in the RTP/RTCP module.
365 // The level will be used in combination with voice-activity state
366 // (frameType) to add an RTP header extension
367 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
368 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000369
kwiberg55b97fe2016-01-28 05:22:45 -0800370 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
371 // packetization.
372 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700373 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800374 (FrameType&)frameType, payloadType, timeStamp,
375 // Leaving the time when this frame was
376 // received from the capture device as
377 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700378 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800379 _engineStatisticsPtr->SetLastError(
380 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
381 "Channel::SendData() failed to send data to RTP/RTCP module");
382 return -1;
383 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000384
kwiberg55b97fe2016-01-28 05:22:45 -0800385 _lastLocalTimeStamp = timeStamp;
386 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000387
kwiberg55b97fe2016-01-28 05:22:45 -0800388 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000389}
390
kwiberg55b97fe2016-01-28 05:22:45 -0800391int32_t Channel::InFrameType(FrameType frame_type) {
392 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
393 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000394
kwiberg55b97fe2016-01-28 05:22:45 -0800395 rtc::CritScope cs(&_callbackCritSect);
396 _sendFrameType = (frame_type == kAudioFrameSpeech);
397 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000398}
399
stefan1d8a5062015-10-02 03:39:33 -0700400bool Channel::SendRtp(const uint8_t* data,
401 size_t len,
402 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800403 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
404 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
kwiberg55b97fe2016-01-28 05:22:45 -0800406 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000407
kwiberg55b97fe2016-01-28 05:22:45 -0800408 if (_transportPtr == NULL) {
409 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
410 "Channel::SendPacket() failed to send RTP packet due to"
411 " invalid transport object");
412 return false;
413 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
kwiberg55b97fe2016-01-28 05:22:45 -0800415 uint8_t* bufferToSendPtr = (uint8_t*)data;
416 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000417
kwiberg55b97fe2016-01-28 05:22:45 -0800418 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
419 std::string transport_name =
420 _externalTransport ? "external transport" : "WebRtc sockets";
421 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
422 "Channel::SendPacket() RTP transmission using %s failed",
423 transport_name.c_str());
424 return false;
425 }
426 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000427}
428
kwiberg55b97fe2016-01-28 05:22:45 -0800429bool Channel::SendRtcp(const uint8_t* data, size_t len) {
430 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
431 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000432
kwiberg55b97fe2016-01-28 05:22:45 -0800433 rtc::CritScope cs(&_callbackCritSect);
434 if (_transportPtr == NULL) {
435 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
436 "Channel::SendRtcp() failed to send RTCP packet"
437 " due to invalid transport object");
438 return false;
439 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000440
kwiberg55b97fe2016-01-28 05:22:45 -0800441 uint8_t* bufferToSendPtr = (uint8_t*)data;
442 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000443
kwiberg55b97fe2016-01-28 05:22:45 -0800444 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
445 if (n < 0) {
446 std::string transport_name =
447 _externalTransport ? "external transport" : "WebRtc sockets";
448 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
449 "Channel::SendRtcp() transmission using %s failed",
450 transport_name.c_str());
451 return false;
452 }
453 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000454}
455
kwiberg55b97fe2016-01-28 05:22:45 -0800456void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
457 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
458 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000459
kwiberg55b97fe2016-01-28 05:22:45 -0800460 // Update ssrc so that NTP for AV sync can be updated.
461 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000462}
463
Peter Boströmac547a62015-09-17 23:03:57 +0200464void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
465 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
466 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
467 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000468}
469
Peter Boströmac547a62015-09-17 23:03:57 +0200470int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000471 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000472 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000473 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800474 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200475 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800476 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
477 "Channel::OnInitializeDecoder(payloadType=%d, "
478 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
479 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000480
kwiberg55b97fe2016-01-28 05:22:45 -0800481 CodecInst receiveCodec = {0};
482 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000483
kwiberg55b97fe2016-01-28 05:22:45 -0800484 receiveCodec.pltype = payloadType;
485 receiveCodec.plfreq = frequency;
486 receiveCodec.channels = channels;
487 receiveCodec.rate = rate;
488 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000489
kwiberg55b97fe2016-01-28 05:22:45 -0800490 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
491 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
kwiberg55b97fe2016-01-28 05:22:45 -0800493 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700494 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
495 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800496 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
497 "Channel::OnInitializeDecoder() invalid codec ("
498 "pt=%d, name=%s) received - 1",
499 payloadType, payloadName);
500 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
501 return -1;
502 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000503
kwiberg55b97fe2016-01-28 05:22:45 -0800504 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000505}
506
kwiberg55b97fe2016-01-28 05:22:45 -0800507int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
508 size_t payloadSize,
509 const WebRtcRTPHeader* rtpHeader) {
510 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
511 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
512 ","
513 " payloadType=%u, audioChannel=%" PRIuS ")",
514 payloadSize, rtpHeader->header.payloadType,
515 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000516
kwiberg55b97fe2016-01-28 05:22:45 -0800517 if (!channel_state_.Get().playing) {
518 // Avoid inserting into NetEQ when we are not playing. Count the
519 // packet as discarded.
520 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
521 "received packet is discarded since playing is not"
522 " activated");
523 _numberOfDiscardedPackets++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000524 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800525 }
526
527 // Push the incoming payload (parsed and ready for decoding) into the ACM
528 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
529 0) {
530 _engineStatisticsPtr->SetLastError(
531 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
532 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
533 return -1;
534 }
535
kwiberg55b97fe2016-01-28 05:22:45 -0800536 int64_t round_trip_time = 0;
537 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
538 NULL);
539
540 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
541 if (!nack_list.empty()) {
542 // Can't use nack_list.data() since it's not supported by all
543 // compilers.
544 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
545 }
546 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000547}
548
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000549bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000550 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000551 RTPHeader header;
552 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
553 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
554 "IncomingPacket invalid RTP header");
555 return false;
556 }
557 header.payload_type_frequency =
558 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
559 if (header.payload_type_frequency < 0)
560 return false;
561 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
562}
563
henrik.lundin42dda502016-05-18 05:36:01 -0700564MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
565 int32_t id,
566 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700567 unsigned int ssrc;
568 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
569 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800570 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700571 bool muted;
572 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
573 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800574 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
575 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
576 // In all likelihood, the audio in this frame is garbage. We return an
577 // error so that the audio mixer module doesn't add it to the mix. As
578 // a result, it won't be played out and the actions skipped here are
579 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700580 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800581 }
henrik.lundina89ab962016-05-18 08:52:45 -0700582
583 if (muted) {
584 // TODO(henrik.lundin): We should be able to do better than this. But we
585 // will have to go through all the cases below where the audio samples may
586 // be used, and handle the muted case in some way.
587 audioFrame->Mute();
588 }
kwiberg55b97fe2016-01-28 05:22:45 -0800589
kwiberg55b97fe2016-01-28 05:22:45 -0800590 // Convert module ID to internal VoE channel ID
591 audioFrame->id_ = VoEChannelId(audioFrame->id_);
592 // Store speech type for dead-or-alive detection
593 _outputSpeechType = audioFrame->speech_type_;
594
595 ChannelState::State state = channel_state_.Get();
596
kwiberg55b97fe2016-01-28 05:22:45 -0800597 {
598 // Pass the audio buffers to an optional sink callback, before applying
599 // scaling/panning, as that applies to the mix operation.
600 // External recipients of the audio (e.g. via AudioTrack), will do their
601 // own mixing/dynamic processing.
602 rtc::CritScope cs(&_callbackCritSect);
603 if (audio_sink_) {
604 AudioSinkInterface::Data data(
605 &audioFrame->data_[0], audioFrame->samples_per_channel_,
606 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
607 audioFrame->timestamp_);
608 audio_sink_->OnData(data);
609 }
610 }
611
612 float output_gain = 1.0f;
613 float left_pan = 1.0f;
614 float right_pan = 1.0f;
615 {
616 rtc::CritScope cs(&volume_settings_critsect_);
617 output_gain = _outputGain;
618 left_pan = _panLeft;
619 right_pan = _panRight;
620 }
621
622 // Output volume scaling
623 if (output_gain < 0.99f || output_gain > 1.01f) {
624 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
625 }
626
627 // Scale left and/or right channel(s) if stereo and master balance is
628 // active
629
630 if (left_pan != 1.0f || right_pan != 1.0f) {
631 if (audioFrame->num_channels_ == 1) {
632 // Emulate stereo mode since panning is active.
633 // The mono signal is copied to both left and right channels here.
634 AudioFrameOperations::MonoToStereo(audioFrame);
635 }
636 // For true stereo mode (when we are receiving a stereo signal), no
637 // action is needed.
638
639 // Do the panning operation (the audio frame contains stereo at this
640 // stage)
641 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
642 }
643
644 // Mix decoded PCM output with file if file mixing is enabled
645 if (state.output_file_playing) {
646 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700647 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800648 }
649
650 // External media
651 if (_outputExternalMedia) {
652 rtc::CritScope cs(&_callbackCritSect);
653 const bool isStereo = (audioFrame->num_channels_ == 2);
654 if (_outputExternalMediaCallbackPtr) {
655 _outputExternalMediaCallbackPtr->Process(
656 _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_,
657 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
658 isStereo);
659 }
660 }
661
662 // Record playout if enabled
663 {
664 rtc::CritScope cs(&_fileCritSect);
665
kwiberg5a25d952016-08-17 07:31:12 -0700666 if (_outputFileRecording && output_file_recorder_) {
667 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800668 }
669 }
670
671 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700672 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800673 _outputAudioLevel.ComputeLevel(*audioFrame);
674
675 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
676 // The first frame with a valid rtp timestamp.
677 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
678 }
679
680 if (capture_start_rtp_time_stamp_ >= 0) {
681 // audioFrame.timestamp_ should be valid from now on.
682
683 // Compute elapsed time.
684 int64_t unwrap_timestamp =
685 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
686 audioFrame->elapsed_time_ms_ =
687 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700688 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800689
niklase@google.com470e71d2011-07-07 08:21:25 +0000690 {
kwiberg55b97fe2016-01-28 05:22:45 -0800691 rtc::CritScope lock(&ts_stats_lock_);
692 // Compute ntp time.
693 audioFrame->ntp_time_ms_ =
694 ntp_estimator_.Estimate(audioFrame->timestamp_);
695 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
696 if (audioFrame->ntp_time_ms_ > 0) {
697 // Compute |capture_start_ntp_time_ms_| so that
698 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
699 capture_start_ntp_time_ms_ =
700 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000701 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000702 }
kwiberg55b97fe2016-01-28 05:22:45 -0800703 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000704
henrik.lundin42dda502016-05-18 05:36:01 -0700705 return muted ? MixerParticipant::AudioFrameInfo::kMuted
706 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000707}
708
aleloi6c278492016-10-20 14:24:39 -0700709AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
710 int sample_rate_hz,
711 AudioFrame* audio_frame) {
712 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700713
aleloi6c278492016-10-20 14:24:39 -0700714 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700715
716 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
717 FrameInfo new_audio_frame_info = FrameInfo::kError;
718 switch (frame_info) {
719 case MixerParticipant::AudioFrameInfo::kNormal:
720 new_audio_frame_info = FrameInfo::kNormal;
721 break;
722 case MixerParticipant::AudioFrameInfo::kMuted:
723 new_audio_frame_info = FrameInfo::kMuted;
724 break;
725 case MixerParticipant::AudioFrameInfo::kError:
726 new_audio_frame_info = FrameInfo::kError;
727 break;
728 }
aleloi6c278492016-10-20 14:24:39 -0700729 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700730}
731
kwiberg55b97fe2016-01-28 05:22:45 -0800732int32_t Channel::NeededFrequency(int32_t id) const {
733 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
734 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000735
kwiberg55b97fe2016-01-28 05:22:45 -0800736 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000737
kwiberg55b97fe2016-01-28 05:22:45 -0800738 // Determine highest needed receive frequency
739 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000740
kwiberg55b97fe2016-01-28 05:22:45 -0800741 // Return the bigger of playout and receive frequency in the ACM.
742 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
743 highestNeeded = audio_coding_->PlayoutFrequency();
744 } else {
745 highestNeeded = receiveFrequency;
746 }
747
748 // Special case, if we're playing a file on the playout side
749 // we take that frequency into consideration as well
750 // This is not needed on sending side, since the codec will
751 // limit the spectrum anyway.
752 if (channel_state_.Get().output_file_playing) {
753 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700754 if (output_file_player_) {
755 if (output_file_player_->Frequency() > highestNeeded) {
756 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800757 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000758 }
kwiberg55b97fe2016-01-28 05:22:45 -0800759 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000760
kwiberg55b97fe2016-01-28 05:22:45 -0800761 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000762}
763
ossu5f7cfa52016-05-30 08:11:28 -0700764int32_t Channel::CreateChannel(
765 Channel*& channel,
766 int32_t channelId,
767 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700768 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800769 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
770 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
771 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000772
solenberg88499ec2016-09-07 07:34:41 -0700773 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800774 if (channel == NULL) {
775 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
776 "Channel::CreateChannel() unable to allocate memory for"
777 " channel");
778 return -1;
779 }
780 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000781}
782
kwiberg55b97fe2016-01-28 05:22:45 -0800783void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
784 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
785 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
786 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000787
kwiberg55b97fe2016-01-28 05:22:45 -0800788 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000789}
790
kwiberg55b97fe2016-01-28 05:22:45 -0800791void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
792 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
793 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
794 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000795
kwiberg55b97fe2016-01-28 05:22:45 -0800796 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000797}
798
kwiberg55b97fe2016-01-28 05:22:45 -0800799void Channel::PlayFileEnded(int32_t id) {
800 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
801 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000802
kwiberg55b97fe2016-01-28 05:22:45 -0800803 if (id == _inputFilePlayerId) {
804 channel_state_.SetInputFilePlaying(false);
805 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
806 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000807 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800808 } else if (id == _outputFilePlayerId) {
809 channel_state_.SetOutputFilePlaying(false);
810 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
811 "Channel::PlayFileEnded() => output file player module is"
812 " shutdown");
813 }
814}
815
816void Channel::RecordFileEnded(int32_t id) {
817 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
818 "Channel::RecordFileEnded(id=%d)", id);
819
820 assert(id == _outputFileRecorderId);
821
822 rtc::CritScope cs(&_fileCritSect);
823
824 _outputFileRecording = false;
825 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
826 "Channel::RecordFileEnded() => output file recorder module is"
827 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000828}
829
pbos@webrtc.org92135212013-05-14 08:31:39 +0000830Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000831 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700832 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800833 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100834 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700835 event_log_proxy_(new RtcEventLogProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100836 rtp_header_parser_(RtpHeaderParser::Create()),
837 rtp_payload_registry_(
838 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
839 rtp_receive_statistics_(
840 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
841 rtp_receiver_(
842 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100843 this,
844 this,
845 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700846 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100847 _outputAudioLevel(),
848 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100849 // Avoid conflict with other channels by adding 1024 - 1026,
850 // won't use as much as 1024 channels.
851 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
852 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
853 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
854 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100855 _outputExternalMedia(false),
856 _inputExternalMediaCallbackPtr(NULL),
857 _outputExternalMediaCallbackPtr(NULL),
858 _timeStamp(0), // This is just an offset, RTP module will add it's own
859 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100860 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100861 playout_timestamp_rtp_(0),
862 playout_timestamp_rtcp_(0),
863 playout_delay_ms_(0),
864 _numberOfDiscardedPackets(0),
865 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100866 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
867 capture_start_rtp_time_stamp_(-1),
868 capture_start_ntp_time_ms_(-1),
869 _engineStatisticsPtr(NULL),
870 _outputMixerPtr(NULL),
871 _transmitMixerPtr(NULL),
872 _moduleProcessThreadPtr(NULL),
873 _audioDeviceModulePtr(NULL),
874 _voiceEngineObserverPtr(NULL),
875 _callbackCritSectPtr(NULL),
876 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100877 _sendFrameType(0),
878 _externalMixing(false),
879 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700880 input_mute_(false),
881 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100882 _panLeft(1.0f),
883 _panRight(1.0f),
884 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100885 _lastLocalTimeStamp(0),
886 _lastPayloadType(0),
887 _includeAudioLevelIndication(false),
888 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100889 restored_packet_in_use_(false),
890 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100891 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700892 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800893 feedback_observer_proxy_(new TransportFeedbackProxy()),
894 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700895 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200896 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
897 kMaxRetransmissionWindowMs)),
solenberg88499ec2016-09-07 07:34:41 -0700898 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800899 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
900 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700901 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800902 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700903 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800904 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200905
kwiberg55b97fe2016-01-28 05:22:45 -0800906 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000907
kwiberg55b97fe2016-01-28 05:22:45 -0800908 RtpRtcp::Configuration configuration;
909 configuration.audio = true;
910 configuration.outgoing_transport = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800911 configuration.receive_statistics = rtp_receive_statistics_.get();
912 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800913 if (pacing_enabled_) {
914 configuration.paced_sender = rtp_packet_sender_proxy_.get();
915 configuration.transport_sequence_number_allocator =
916 seq_num_allocator_proxy_.get();
917 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
918 }
ivoc14d5dbe2016-07-04 07:06:55 -0700919 configuration.event_log = &(*event_log_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200920 configuration.retransmission_rate_limiter =
921 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000922
kwiberg55b97fe2016-01-28 05:22:45 -0800923 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100924 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000925
kwiberg55b97fe2016-01-28 05:22:45 -0800926 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
927 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
928 statistics_proxy_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000929}
930
kwiberg55b97fe2016-01-28 05:22:45 -0800931Channel::~Channel() {
932 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
933 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
934 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000935
kwiberg55b97fe2016-01-28 05:22:45 -0800936 if (_outputExternalMedia) {
937 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
938 }
939 if (channel_state_.Get().input_external_media) {
940 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
941 }
942 StopSend();
943 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000944
kwiberg55b97fe2016-01-28 05:22:45 -0800945 {
946 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700947 if (input_file_player_) {
948 input_file_player_->RegisterModuleFileCallback(NULL);
949 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000950 }
kwiberg5a25d952016-08-17 07:31:12 -0700951 if (output_file_player_) {
952 output_file_player_->RegisterModuleFileCallback(NULL);
953 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800954 }
kwiberg5a25d952016-08-17 07:31:12 -0700955 if (output_file_recorder_) {
956 output_file_recorder_->RegisterModuleFileCallback(NULL);
957 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800958 }
959 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000960
kwiberg55b97fe2016-01-28 05:22:45 -0800961 // The order to safely shutdown modules in a channel is:
962 // 1. De-register callbacks in modules
963 // 2. De-register modules in process thread
964 // 3. Destroy modules
965 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
966 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
967 "~Channel() failed to de-register transport callback"
968 " (Audio coding module)");
969 }
970 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
971 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
972 "~Channel() failed to de-register VAD callback"
973 " (Audio coding module)");
974 }
975 // De-register modules in process thread
976 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000977
kwiberg55b97fe2016-01-28 05:22:45 -0800978 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000979}
980
kwiberg55b97fe2016-01-28 05:22:45 -0800981int32_t Channel::Init() {
982 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
983 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000984
kwiberg55b97fe2016-01-28 05:22:45 -0800985 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000986
kwiberg55b97fe2016-01-28 05:22:45 -0800987 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000988
kwiberg55b97fe2016-01-28 05:22:45 -0800989 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
990 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
991 "Channel::Init() must call SetEngineInformation() first");
992 return -1;
993 }
994
995 // --- Add modules to process thread (for periodic schedulation)
996
997 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
998
999 // --- ACM initialization
1000
1001 if (audio_coding_->InitializeReceiver() == -1) {
1002 _engineStatisticsPtr->SetLastError(
1003 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1004 "Channel::Init() unable to initialize the ACM - 1");
1005 return -1;
1006 }
1007
1008 // --- RTP/RTCP module initialization
1009
1010 // Ensure that RTCP is enabled by default for the created channel.
1011 // Note that, the module will keep generating RTCP until it is explicitly
1012 // disabled by the user.
1013 // After StopListen (when no sockets exists), RTCP packets will no longer
1014 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001015 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001016 // RTCP is enabled by default.
1017 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1018 // --- Register all permanent callbacks
1019 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1020 (audio_coding_->RegisterVADCallback(this) == -1);
1021
1022 if (fail) {
1023 _engineStatisticsPtr->SetLastError(
1024 VE_CANNOT_INIT_CHANNEL, kTraceError,
1025 "Channel::Init() callbacks not registered");
1026 return -1;
1027 }
1028
1029 // --- Register all supported codecs to the receiving side of the
1030 // RTP/RTCP module
1031
1032 CodecInst codec;
1033 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1034
1035 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1036 // Open up the RTP/RTCP receiver for all supported codecs
1037 if ((audio_coding_->Codec(idx, &codec) == -1) ||
1038 (rtp_receiver_->RegisterReceivePayload(
1039 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1040 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
1041 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1042 "Channel::Init() unable to register %s "
1043 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1044 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1045 codec.rate);
1046 } else {
1047 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1048 "Channel::Init() %s (%d/%d/%" PRIuS
1049 "/%d) has been "
1050 "added to the RTP/RTCP receiver",
1051 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1052 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001053 }
1054
kwiberg55b97fe2016-01-28 05:22:45 -08001055 // Ensure that PCMU is used as default codec on the sending side
1056 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1057 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001058 }
1059
kwiberg55b97fe2016-01-28 05:22:45 -08001060 // Register default PT for outband 'telephone-event'
1061 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001062 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001063 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1064 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001065 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1066 "Channel::Init() failed to register outband "
1067 "'telephone-event' (%d/%d) correctly",
1068 codec.pltype, codec.plfreq);
1069 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001070 }
1071
kwiberg55b97fe2016-01-28 05:22:45 -08001072 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001073 if (!codec_manager_.RegisterEncoder(codec) ||
1074 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001075 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1076 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001077 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001078 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1079 "Channel::Init() failed to register CN (%d/%d) "
1080 "correctly - 1",
1081 codec.pltype, codec.plfreq);
1082 }
1083 }
kwiberg55b97fe2016-01-28 05:22:45 -08001084 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001085
kwiberg55b97fe2016-01-28 05:22:45 -08001086 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001087}
1088
kwiberg55b97fe2016-01-28 05:22:45 -08001089int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1090 OutputMixer& outputMixer,
1091 voe::TransmitMixer& transmitMixer,
1092 ProcessThread& moduleProcessThread,
1093 AudioDeviceModule& audioDeviceModule,
1094 VoiceEngineObserver* voiceEngineObserver,
1095 rtc::CriticalSection* callbackCritSect) {
1096 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1097 "Channel::SetEngineInformation()");
1098 _engineStatisticsPtr = &engineStatistics;
1099 _outputMixerPtr = &outputMixer;
1100 _transmitMixerPtr = &transmitMixer,
1101 _moduleProcessThreadPtr = &moduleProcessThread;
1102 _audioDeviceModulePtr = &audioDeviceModule;
1103 _voiceEngineObserverPtr = voiceEngineObserver;
1104 _callbackCritSectPtr = callbackCritSect;
1105 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001106}
1107
kwiberg55b97fe2016-01-28 05:22:45 -08001108int32_t Channel::UpdateLocalTimeStamp() {
1109 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1110 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001111}
1112
kwibergb7f89d62016-02-17 10:04:18 -08001113void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001114 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001115 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001116}
1117
ossu29b1a8d2016-06-13 07:34:51 -07001118const rtc::scoped_refptr<AudioDecoderFactory>&
1119Channel::GetAudioDecoderFactory() const {
1120 return decoder_factory_;
1121}
1122
kwiberg55b97fe2016-01-28 05:22:45 -08001123int32_t Channel::StartPlayout() {
1124 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1125 "Channel::StartPlayout()");
1126 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001127 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001128 }
1129
1130 if (!_externalMixing) {
1131 // Add participant as candidates for mixing.
1132 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1133 _engineStatisticsPtr->SetLastError(
1134 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1135 "StartPlayout() failed to add participant to mixer");
1136 return -1;
1137 }
1138 }
1139
1140 channel_state_.SetPlaying(true);
1141 if (RegisterFilePlayingToMixer() != 0)
1142 return -1;
1143
1144 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001145}
1146
kwiberg55b97fe2016-01-28 05:22:45 -08001147int32_t Channel::StopPlayout() {
1148 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1149 "Channel::StopPlayout()");
1150 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001151 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001152 }
1153
1154 if (!_externalMixing) {
1155 // Remove participant as candidates for mixing
1156 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1157 _engineStatisticsPtr->SetLastError(
1158 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1159 "StopPlayout() failed to remove participant from mixer");
1160 return -1;
1161 }
1162 }
1163
1164 channel_state_.SetPlaying(false);
1165 _outputAudioLevel.Clear();
1166
1167 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001168}
1169
kwiberg55b97fe2016-01-28 05:22:45 -08001170int32_t Channel::StartSend() {
1171 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1172 "Channel::StartSend()");
1173 // Resume the previous sequence number which was reset by StopSend().
1174 // This needs to be done before |sending| is set to true.
1175 if (send_sequence_number_)
1176 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001177
kwiberg55b97fe2016-01-28 05:22:45 -08001178 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001179 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001180 }
1181 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001182
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001183 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001184 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1185 _engineStatisticsPtr->SetLastError(
1186 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1187 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001188 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001189 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001190 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001191 return -1;
1192 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001193
kwiberg55b97fe2016-01-28 05:22:45 -08001194 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001195}
1196
kwiberg55b97fe2016-01-28 05:22:45 -08001197int32_t Channel::StopSend() {
1198 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1199 "Channel::StopSend()");
1200 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001201 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001202 }
1203 channel_state_.SetSending(false);
1204
1205 // Store the sequence number to be able to pick up the same sequence for
1206 // the next StartSend(). This is needed for restarting device, otherwise
1207 // it might cause libSRTP to complain about packets being replayed.
1208 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1209 // CL is landed. See issue
1210 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1211 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1212
1213 // Reset sending SSRC and sequence number and triggers direct transmission
1214 // of RTCP BYE
1215 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1216 _engineStatisticsPtr->SetLastError(
1217 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1218 "StartSend() RTP/RTCP failed to stop sending");
1219 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001220 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001221
1222 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001223}
1224
kwiberg55b97fe2016-01-28 05:22:45 -08001225int32_t Channel::StartReceiving() {
1226 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1227 "Channel::StartReceiving()");
1228 if (channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001229 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001230 }
1231 channel_state_.SetReceiving(true);
1232 _numberOfDiscardedPackets = 0;
1233 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001234}
1235
kwiberg55b97fe2016-01-28 05:22:45 -08001236int32_t Channel::StopReceiving() {
1237 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1238 "Channel::StopReceiving()");
1239 if (!channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001240 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001241 }
1242
1243 channel_state_.SetReceiving(false);
1244 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001245}
1246
kwiberg55b97fe2016-01-28 05:22:45 -08001247int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1248 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1249 "Channel::RegisterVoiceEngineObserver()");
1250 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001251
kwiberg55b97fe2016-01-28 05:22:45 -08001252 if (_voiceEngineObserverPtr) {
1253 _engineStatisticsPtr->SetLastError(
1254 VE_INVALID_OPERATION, kTraceError,
1255 "RegisterVoiceEngineObserver() observer already enabled");
1256 return -1;
1257 }
1258 _voiceEngineObserverPtr = &observer;
1259 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001260}
1261
kwiberg55b97fe2016-01-28 05:22:45 -08001262int32_t Channel::DeRegisterVoiceEngineObserver() {
1263 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1264 "Channel::DeRegisterVoiceEngineObserver()");
1265 rtc::CritScope cs(&_callbackCritSect);
1266
1267 if (!_voiceEngineObserverPtr) {
1268 _engineStatisticsPtr->SetLastError(
1269 VE_INVALID_OPERATION, kTraceWarning,
1270 "DeRegisterVoiceEngineObserver() observer already disabled");
1271 return 0;
1272 }
1273 _voiceEngineObserverPtr = NULL;
1274 return 0;
1275}
1276
1277int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001278 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001279 if (send_codec) {
1280 codec = *send_codec;
1281 return 0;
1282 }
1283 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001284}
1285
kwiberg55b97fe2016-01-28 05:22:45 -08001286int32_t Channel::GetRecCodec(CodecInst& codec) {
1287 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001288}
1289
kwiberg55b97fe2016-01-28 05:22:45 -08001290int32_t Channel::SetSendCodec(const CodecInst& codec) {
1291 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1292 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001293
kwibergc8d071e2016-04-06 12:22:38 -07001294 if (!codec_manager_.RegisterEncoder(codec) ||
1295 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001296 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1297 "SetSendCodec() failed to register codec to ACM");
1298 return -1;
1299 }
1300
1301 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1302 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1303 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1304 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1305 "SetSendCodec() failed to register codec to"
1306 " RTP/RTCP module");
1307 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001308 }
kwiberg55b97fe2016-01-28 05:22:45 -08001309 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001310
kwiberg55b97fe2016-01-28 05:22:45 -08001311 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) {
1312 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1313 "SetSendCodec() failed to set audio packet size");
1314 return -1;
1315 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001316
kwiberg55b97fe2016-01-28 05:22:45 -08001317 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001318}
1319
Ivo Creusenadf89b72015-04-29 16:03:33 +02001320void Channel::SetBitRate(int bitrate_bps) {
1321 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1322 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001323 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1324 if (*encoder)
1325 (*encoder)->OnReceivedTargetAudioBitrate(bitrate_bps);
1326 });
Erik Språng737336d2016-07-29 12:59:36 +02001327 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001328}
1329
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001330void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001331 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1332 if (*encoder)
1333 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1334 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001335}
1336
kwiberg55b97fe2016-01-28 05:22:45 -08001337int32_t Channel::SetVADStatus(bool enableVAD,
1338 ACMVADMode mode,
1339 bool disableDTX) {
1340 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1341 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001342 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1343 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1344 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001345 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1346 kTraceError,
1347 "SetVADStatus() failed to set VAD");
1348 return -1;
1349 }
1350 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001351}
1352
kwiberg55b97fe2016-01-28 05:22:45 -08001353int32_t Channel::GetVADStatus(bool& enabledVAD,
1354 ACMVADMode& mode,
1355 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001356 const auto* params = codec_manager_.GetStackParams();
1357 enabledVAD = params->use_cng;
1358 mode = params->vad_mode;
1359 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001360 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001361}
1362
kwiberg55b97fe2016-01-28 05:22:45 -08001363int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1364 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1365 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001366
kwiberg55b97fe2016-01-28 05:22:45 -08001367 if (channel_state_.Get().playing) {
1368 _engineStatisticsPtr->SetLastError(
1369 VE_ALREADY_PLAYING, kTraceError,
1370 "SetRecPayloadType() unable to set PT while playing");
1371 return -1;
1372 }
1373 if (channel_state_.Get().receiving) {
1374 _engineStatisticsPtr->SetLastError(
1375 VE_ALREADY_LISTENING, kTraceError,
1376 "SetRecPayloadType() unable to set PT while listening");
1377 return -1;
1378 }
1379
1380 if (codec.pltype == -1) {
1381 // De-register the selected codec (RTP/RTCP module and ACM)
1382
1383 int8_t pltype(-1);
1384 CodecInst rxCodec = codec;
1385
1386 // Get payload type for the given codec
1387 rtp_payload_registry_->ReceivePayloadType(
1388 rxCodec.plname, rxCodec.plfreq, rxCodec.channels,
1389 (rxCodec.rate < 0) ? 0 : rxCodec.rate, &pltype);
1390 rxCodec.pltype = pltype;
1391
1392 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1393 _engineStatisticsPtr->SetLastError(
1394 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1395 "SetRecPayloadType() RTP/RTCP-module deregistration "
1396 "failed");
1397 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001398 }
kwiberg55b97fe2016-01-28 05:22:45 -08001399 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1400 _engineStatisticsPtr->SetLastError(
1401 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1402 "SetRecPayloadType() ACM deregistration failed - 1");
1403 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001404 }
kwiberg55b97fe2016-01-28 05:22:45 -08001405 return 0;
1406 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001407
kwiberg55b97fe2016-01-28 05:22:45 -08001408 if (rtp_receiver_->RegisterReceivePayload(
1409 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1410 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1411 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001412 // TODO(kwiberg): Retrying is probably not necessary, since
1413 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001414 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001415 if (rtp_receiver_->RegisterReceivePayload(
kwiberg55b97fe2016-01-28 05:22:45 -08001416 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1417 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1418 _engineStatisticsPtr->SetLastError(
1419 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1420 "SetRecPayloadType() RTP/RTCP-module registration failed");
1421 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001422 }
kwiberg55b97fe2016-01-28 05:22:45 -08001423 }
kwibergda2bf4e2016-10-24 13:47:09 -07001424 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1425 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001426 audio_coding_->UnregisterReceiveCodec(codec.pltype);
kwibergda2bf4e2016-10-24 13:47:09 -07001427 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1428 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001429 _engineStatisticsPtr->SetLastError(
1430 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1431 "SetRecPayloadType() ACM registration failed - 1");
1432 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001433 }
kwiberg55b97fe2016-01-28 05:22:45 -08001434 }
1435 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001436}
1437
kwiberg55b97fe2016-01-28 05:22:45 -08001438int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1439 int8_t payloadType(-1);
1440 if (rtp_payload_registry_->ReceivePayloadType(
1441 codec.plname, codec.plfreq, codec.channels,
1442 (codec.rate < 0) ? 0 : codec.rate, &payloadType) != 0) {
1443 _engineStatisticsPtr->SetLastError(
1444 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1445 "GetRecPayloadType() failed to retrieve RX payload type");
1446 return -1;
1447 }
1448 codec.pltype = payloadType;
1449 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001450}
1451
kwiberg55b97fe2016-01-28 05:22:45 -08001452int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1453 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1454 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001455
kwiberg55b97fe2016-01-28 05:22:45 -08001456 CodecInst codec;
1457 int32_t samplingFreqHz(-1);
1458 const size_t kMono = 1;
1459 if (frequency == kFreq32000Hz)
1460 samplingFreqHz = 32000;
1461 else if (frequency == kFreq16000Hz)
1462 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001463
kwiberg55b97fe2016-01-28 05:22:45 -08001464 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1465 _engineStatisticsPtr->SetLastError(
1466 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1467 "SetSendCNPayloadType() failed to retrieve default CN codec "
1468 "settings");
1469 return -1;
1470 }
1471
1472 // Modify the payload type (must be set to dynamic range)
1473 codec.pltype = type;
1474
kwibergc8d071e2016-04-06 12:22:38 -07001475 if (!codec_manager_.RegisterEncoder(codec) ||
1476 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001477 _engineStatisticsPtr->SetLastError(
1478 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1479 "SetSendCNPayloadType() failed to register CN to ACM");
1480 return -1;
1481 }
1482
1483 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1484 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1485 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1486 _engineStatisticsPtr->SetLastError(
1487 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1488 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1489 "module");
1490 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001491 }
kwiberg55b97fe2016-01-28 05:22:45 -08001492 }
1493 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001494}
1495
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001496int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001497 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001498 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001499
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001500 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001501 _engineStatisticsPtr->SetLastError(
1502 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001503 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001504 return -1;
1505 }
1506 return 0;
1507}
1508
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001509int Channel::SetOpusDtx(bool enable_dtx) {
1510 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1511 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001512 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001513 : audio_coding_->DisableOpusDtx();
1514 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001515 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1516 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001517 return -1;
1518 }
1519 return 0;
1520}
1521
ivoc85228d62016-07-27 04:53:47 -07001522int Channel::GetOpusDtx(bool* enabled) {
1523 int success = -1;
1524 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1525 if (encoder) {
1526 *enabled = encoder->GetDtx();
1527 success = 0;
1528 }
1529 });
1530 return success;
1531}
1532
minyue7e304322016-10-12 05:00:55 -07001533bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1534 bool success = false;
1535 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1536 if (*encoder) {
1537 success = (*encoder)->EnableAudioNetworkAdaptor(
1538 config_string, Clock::GetRealTimeClock());
1539 }
1540 });
1541 return success;
1542}
1543
1544void Channel::DisableAudioNetworkAdaptor() {
1545 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1546 if (*encoder)
1547 (*encoder)->DisableAudioNetworkAdaptor();
1548 });
1549}
1550
1551void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1552 int max_frame_length_ms) {
1553 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1554 if (*encoder) {
1555 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1556 max_frame_length_ms);
1557 }
1558 });
1559}
1560
mflodman3d7db262016-04-29 00:57:13 -07001561int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001562 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001563 "Channel::RegisterExternalTransport()");
1564
kwiberg55b97fe2016-01-28 05:22:45 -08001565 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001566 if (_externalTransport) {
1567 _engineStatisticsPtr->SetLastError(
1568 VE_INVALID_OPERATION, kTraceError,
1569 "RegisterExternalTransport() external transport already enabled");
1570 return -1;
1571 }
1572 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001573 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001574 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001575}
1576
kwiberg55b97fe2016-01-28 05:22:45 -08001577int32_t Channel::DeRegisterExternalTransport() {
1578 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1579 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001580
kwiberg55b97fe2016-01-28 05:22:45 -08001581 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001582 if (_transportPtr) {
1583 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1584 "DeRegisterExternalTransport() all transport is disabled");
1585 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001586 _engineStatisticsPtr->SetLastError(
1587 VE_INVALID_OPERATION, kTraceWarning,
1588 "DeRegisterExternalTransport() external transport already "
1589 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001590 }
1591 _externalTransport = false;
1592 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001593 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001594}
1595
mflodman3d7db262016-04-29 00:57:13 -07001596int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001597 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001598 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001599 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001600 "Channel::ReceivedRTPPacket()");
1601
1602 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001603 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001604
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001605 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001606 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1607 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1608 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001609 return -1;
1610 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001611 header.payload_type_frequency =
1612 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001613 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001614 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001615 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001616 rtp_receive_statistics_->IncomingPacket(
1617 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001618 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001619
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001620 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001621}
1622
1623bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001624 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001625 const RTPHeader& header,
1626 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001627 if (rtp_payload_registry_->IsRtx(header)) {
1628 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001629 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001630 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001631 assert(packet_length >= header.headerLength);
1632 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001633 PayloadUnion payload_specific;
1634 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001635 &payload_specific)) {
1636 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001637 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001638 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1639 payload_specific, in_order);
1640}
1641
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001642bool Channel::HandleRtxPacket(const uint8_t* packet,
1643 size_t packet_length,
1644 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001645 if (!rtp_payload_registry_->IsRtx(header))
1646 return false;
1647
1648 // Remove the RTX header and parse the original RTP header.
1649 if (packet_length < header.headerLength)
1650 return false;
1651 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1652 return false;
1653 if (restored_packet_in_use_) {
1654 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1655 "Multiple RTX headers detected, dropping packet");
1656 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001657 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001658 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001659 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1660 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001661 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1662 "Incoming RTX packet: invalid RTP header");
1663 return false;
1664 }
1665 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001666 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001667 restored_packet_in_use_ = false;
1668 return ret;
1669}
1670
1671bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1672 StreamStatistician* statistician =
1673 rtp_receive_statistics_->GetStatistician(header.ssrc);
1674 if (!statistician)
1675 return false;
1676 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001677}
1678
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001679bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1680 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001681 // Retransmissions are handled separately if RTX is enabled.
1682 if (rtp_payload_registry_->RtxEnabled())
1683 return false;
1684 StreamStatistician* statistician =
1685 rtp_receive_statistics_->GetStatistician(header.ssrc);
1686 if (!statistician)
1687 return false;
1688 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001689 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001690 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001691 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001692}
1693
mflodman3d7db262016-04-29 00:57:13 -07001694int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001695 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001696 "Channel::ReceivedRTCPPacket()");
1697 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001698 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001699
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001700 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001701 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001702 _engineStatisticsPtr->SetLastError(
1703 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1704 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1705 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001706
Minyue2013aec2015-05-13 14:14:42 +02001707 int64_t rtt = GetRTT(true);
1708 if (rtt == 0) {
1709 // Waiting for valid RTT.
1710 return 0;
1711 }
Erik Språng737336d2016-07-29 12:59:36 +02001712
1713 int64_t nack_window_ms = rtt;
1714 if (nack_window_ms < kMinRetransmissionWindowMs) {
1715 nack_window_ms = kMinRetransmissionWindowMs;
1716 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1717 nack_window_ms = kMaxRetransmissionWindowMs;
1718 }
1719 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1720
minyue7e304322016-10-12 05:00:55 -07001721 // Invoke audio encoders OnReceivedRtt().
1722 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1723 if (*encoder)
1724 (*encoder)->OnReceivedRtt(rtt);
1725 });
1726
Minyue2013aec2015-05-13 14:14:42 +02001727 uint32_t ntp_secs = 0;
1728 uint32_t ntp_frac = 0;
1729 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001730 if (0 !=
1731 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1732 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001733 // Waiting for RTCP.
1734 return 0;
1735 }
1736
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001737 {
tommi31fc21f2016-01-21 10:37:37 -08001738 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001739 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001740 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001741 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001742}
1743
niklase@google.com470e71d2011-07-07 08:21:25 +00001744int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001745 bool loop,
1746 FileFormats format,
1747 int startPosition,
1748 float volumeScaling,
1749 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001750 const CodecInst* codecInst) {
1751 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1752 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1753 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1754 "stopPosition=%d)",
1755 fileName, loop, format, volumeScaling, startPosition,
1756 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001757
kwiberg55b97fe2016-01-28 05:22:45 -08001758 if (channel_state_.Get().output_file_playing) {
1759 _engineStatisticsPtr->SetLastError(
1760 VE_ALREADY_PLAYING, kTraceError,
1761 "StartPlayingFileLocally() is already playing");
1762 return -1;
1763 }
1764
1765 {
1766 rtc::CritScope cs(&_fileCritSect);
1767
kwiberg5a25d952016-08-17 07:31:12 -07001768 if (output_file_player_) {
1769 output_file_player_->RegisterModuleFileCallback(NULL);
1770 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001771 }
1772
kwiberg5b356f42016-09-08 04:32:33 -07001773 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001774 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001775
kwiberg5a25d952016-08-17 07:31:12 -07001776 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001777 _engineStatisticsPtr->SetLastError(
1778 VE_INVALID_ARGUMENT, kTraceError,
1779 "StartPlayingFileLocally() filePlayer format is not correct");
1780 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001781 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001782
kwiberg55b97fe2016-01-28 05:22:45 -08001783 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001784
kwiberg5a25d952016-08-17 07:31:12 -07001785 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001786 fileName, loop, startPosition, volumeScaling, notificationTime,
1787 stopPosition, (const CodecInst*)codecInst) != 0) {
1788 _engineStatisticsPtr->SetLastError(
1789 VE_BAD_FILE, kTraceError,
1790 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001791 output_file_player_->StopPlayingFile();
1792 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001793 return -1;
1794 }
kwiberg5a25d952016-08-17 07:31:12 -07001795 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001796 channel_state_.SetOutputFilePlaying(true);
1797 }
1798
1799 if (RegisterFilePlayingToMixer() != 0)
1800 return -1;
1801
1802 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001803}
1804
1805int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001806 FileFormats format,
1807 int startPosition,
1808 float volumeScaling,
1809 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001810 const CodecInst* codecInst) {
1811 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1812 "Channel::StartPlayingFileLocally(format=%d,"
1813 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1814 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001815
kwiberg55b97fe2016-01-28 05:22:45 -08001816 if (stream == NULL) {
1817 _engineStatisticsPtr->SetLastError(
1818 VE_BAD_FILE, kTraceError,
1819 "StartPlayingFileLocally() NULL as input stream");
1820 return -1;
1821 }
1822
1823 if (channel_state_.Get().output_file_playing) {
1824 _engineStatisticsPtr->SetLastError(
1825 VE_ALREADY_PLAYING, kTraceError,
1826 "StartPlayingFileLocally() is already playing");
1827 return -1;
1828 }
1829
1830 {
1831 rtc::CritScope cs(&_fileCritSect);
1832
1833 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001834 if (output_file_player_) {
1835 output_file_player_->RegisterModuleFileCallback(NULL);
1836 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001837 }
1838
kwiberg55b97fe2016-01-28 05:22:45 -08001839 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001840 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001841 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001842
kwiberg5a25d952016-08-17 07:31:12 -07001843 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001844 _engineStatisticsPtr->SetLastError(
1845 VE_INVALID_ARGUMENT, kTraceError,
1846 "StartPlayingFileLocally() filePlayer format isnot correct");
1847 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001848 }
1849
kwiberg55b97fe2016-01-28 05:22:45 -08001850 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001851
kwiberg4ec01d92016-08-22 08:43:54 -07001852 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001853 volumeScaling, notificationTime,
1854 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001855 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1856 "StartPlayingFile() failed to "
1857 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001858 output_file_player_->StopPlayingFile();
1859 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001860 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001861 }
kwiberg5a25d952016-08-17 07:31:12 -07001862 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001863 channel_state_.SetOutputFilePlaying(true);
1864 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001865
kwiberg55b97fe2016-01-28 05:22:45 -08001866 if (RegisterFilePlayingToMixer() != 0)
1867 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001868
kwiberg55b97fe2016-01-28 05:22:45 -08001869 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001870}
1871
kwiberg55b97fe2016-01-28 05:22:45 -08001872int Channel::StopPlayingFileLocally() {
1873 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1874 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001875
kwiberg55b97fe2016-01-28 05:22:45 -08001876 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001877 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001878 }
1879
1880 {
1881 rtc::CritScope cs(&_fileCritSect);
1882
kwiberg5a25d952016-08-17 07:31:12 -07001883 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001884 _engineStatisticsPtr->SetLastError(
1885 VE_STOP_RECORDING_FAILED, kTraceError,
1886 "StopPlayingFile() could not stop playing");
1887 return -1;
1888 }
kwiberg5a25d952016-08-17 07:31:12 -07001889 output_file_player_->RegisterModuleFileCallback(NULL);
1890 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001891 channel_state_.SetOutputFilePlaying(false);
1892 }
1893 // _fileCritSect cannot be taken while calling
1894 // SetAnonymousMixibilityStatus. Refer to comments in
1895 // StartPlayingFileLocally(const char* ...) for more details.
1896 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1897 _engineStatisticsPtr->SetLastError(
1898 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1899 "StopPlayingFile() failed to stop participant from playing as"
1900 "file in the mixer");
1901 return -1;
1902 }
1903
1904 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001905}
1906
kwiberg55b97fe2016-01-28 05:22:45 -08001907int Channel::IsPlayingFileLocally() const {
1908 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001909}
1910
kwiberg55b97fe2016-01-28 05:22:45 -08001911int Channel::RegisterFilePlayingToMixer() {
1912 // Return success for not registering for file playing to mixer if:
1913 // 1. playing file before playout is started on that channel.
1914 // 2. starting playout without file playing on that channel.
1915 if (!channel_state_.Get().playing ||
1916 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001917 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001918 }
1919
1920 // |_fileCritSect| cannot be taken while calling
1921 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1922 // frames can be pulled by the mixer. Since the frames are generated from
1923 // the file, _fileCritSect will be taken. This would result in a deadlock.
1924 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1925 channel_state_.SetOutputFilePlaying(false);
1926 rtc::CritScope cs(&_fileCritSect);
1927 _engineStatisticsPtr->SetLastError(
1928 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1929 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001930 output_file_player_->StopPlayingFile();
1931 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001932 return -1;
1933 }
1934
1935 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001936}
1937
niklase@google.com470e71d2011-07-07 08:21:25 +00001938int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001939 bool loop,
1940 FileFormats format,
1941 int startPosition,
1942 float volumeScaling,
1943 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001944 const CodecInst* codecInst) {
1945 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1946 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1947 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1948 "stopPosition=%d)",
1949 fileName, loop, format, volumeScaling, startPosition,
1950 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001951
kwiberg55b97fe2016-01-28 05:22:45 -08001952 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001953
kwiberg55b97fe2016-01-28 05:22:45 -08001954 if (channel_state_.Get().input_file_playing) {
1955 _engineStatisticsPtr->SetLastError(
1956 VE_ALREADY_PLAYING, kTraceWarning,
1957 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001958 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001959 }
1960
1961 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001962 if (input_file_player_) {
1963 input_file_player_->RegisterModuleFileCallback(NULL);
1964 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001965 }
1966
1967 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001968 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001969 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001970
kwiberg5a25d952016-08-17 07:31:12 -07001971 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001972 _engineStatisticsPtr->SetLastError(
1973 VE_INVALID_ARGUMENT, kTraceError,
1974 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1975 return -1;
1976 }
1977
1978 const uint32_t notificationTime(0);
1979
kwiberg5a25d952016-08-17 07:31:12 -07001980 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001981 fileName, loop, startPosition, volumeScaling, notificationTime,
1982 stopPosition, (const CodecInst*)codecInst) != 0) {
1983 _engineStatisticsPtr->SetLastError(
1984 VE_BAD_FILE, kTraceError,
1985 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001986 input_file_player_->StopPlayingFile();
1987 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001988 return -1;
1989 }
kwiberg5a25d952016-08-17 07:31:12 -07001990 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001991 channel_state_.SetInputFilePlaying(true);
1992
1993 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001994}
1995
1996int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001997 FileFormats format,
1998 int startPosition,
1999 float volumeScaling,
2000 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002001 const CodecInst* codecInst) {
2002 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2003 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2004 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2005 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002006
kwiberg55b97fe2016-01-28 05:22:45 -08002007 if (stream == NULL) {
2008 _engineStatisticsPtr->SetLastError(
2009 VE_BAD_FILE, kTraceError,
2010 "StartPlayingFileAsMicrophone NULL as input stream");
2011 return -1;
2012 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002013
kwiberg55b97fe2016-01-28 05:22:45 -08002014 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002015
kwiberg55b97fe2016-01-28 05:22:45 -08002016 if (channel_state_.Get().input_file_playing) {
2017 _engineStatisticsPtr->SetLastError(
2018 VE_ALREADY_PLAYING, kTraceWarning,
2019 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002020 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002021 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002022
kwiberg55b97fe2016-01-28 05:22:45 -08002023 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002024 if (input_file_player_) {
2025 input_file_player_->RegisterModuleFileCallback(NULL);
2026 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002027 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002028
kwiberg55b97fe2016-01-28 05:22:45 -08002029 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002030 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002031 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002032
kwiberg5a25d952016-08-17 07:31:12 -07002033 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002034 _engineStatisticsPtr->SetLastError(
2035 VE_INVALID_ARGUMENT, kTraceError,
2036 "StartPlayingInputFile() filePlayer format isnot correct");
2037 return -1;
2038 }
2039
2040 const uint32_t notificationTime(0);
2041
kwiberg4ec01d92016-08-22 08:43:54 -07002042 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2043 notificationTime, stopPosition,
2044 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002045 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2046 "StartPlayingFile() failed to start "
2047 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002048 input_file_player_->StopPlayingFile();
2049 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002050 return -1;
2051 }
2052
kwiberg5a25d952016-08-17 07:31:12 -07002053 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002054 channel_state_.SetInputFilePlaying(true);
2055
2056 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002057}
2058
kwiberg55b97fe2016-01-28 05:22:45 -08002059int Channel::StopPlayingFileAsMicrophone() {
2060 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2061 "Channel::StopPlayingFileAsMicrophone()");
2062
2063 rtc::CritScope cs(&_fileCritSect);
2064
2065 if (!channel_state_.Get().input_file_playing) {
2066 return 0;
2067 }
2068
kwiberg5a25d952016-08-17 07:31:12 -07002069 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002070 _engineStatisticsPtr->SetLastError(
2071 VE_STOP_RECORDING_FAILED, kTraceError,
2072 "StopPlayingFile() could not stop playing");
2073 return -1;
2074 }
kwiberg5a25d952016-08-17 07:31:12 -07002075 input_file_player_->RegisterModuleFileCallback(NULL);
2076 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002077 channel_state_.SetInputFilePlaying(false);
2078
2079 return 0;
2080}
2081
2082int Channel::IsPlayingFileAsMicrophone() const {
2083 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002084}
2085
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002086int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002087 const CodecInst* codecInst) {
2088 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2089 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002090
kwiberg55b97fe2016-01-28 05:22:45 -08002091 if (_outputFileRecording) {
2092 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2093 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002094 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002095 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002096
kwiberg55b97fe2016-01-28 05:22:45 -08002097 FileFormats format;
2098 const uint32_t notificationTime(0); // Not supported in VoE
2099 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002100
kwiberg55b97fe2016-01-28 05:22:45 -08002101 if ((codecInst != NULL) &&
2102 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2103 _engineStatisticsPtr->SetLastError(
2104 VE_BAD_ARGUMENT, kTraceError,
2105 "StartRecordingPlayout() invalid compression");
2106 return (-1);
2107 }
2108 if (codecInst == NULL) {
2109 format = kFileFormatPcm16kHzFile;
2110 codecInst = &dummyCodec;
2111 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2112 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2113 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2114 format = kFileFormatWavFile;
2115 } else {
2116 format = kFileFormatCompressedFile;
2117 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002118
kwiberg55b97fe2016-01-28 05:22:45 -08002119 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002120
kwiberg55b97fe2016-01-28 05:22:45 -08002121 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002122 if (output_file_recorder_) {
2123 output_file_recorder_->RegisterModuleFileCallback(NULL);
2124 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002125 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002126
kwiberg5a25d952016-08-17 07:31:12 -07002127 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002128 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002129 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002130 _engineStatisticsPtr->SetLastError(
2131 VE_INVALID_ARGUMENT, kTraceError,
2132 "StartRecordingPlayout() fileRecorder format isnot correct");
2133 return -1;
2134 }
2135
kwiberg5a25d952016-08-17 07:31:12 -07002136 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002137 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2138 _engineStatisticsPtr->SetLastError(
2139 VE_BAD_FILE, kTraceError,
2140 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002141 output_file_recorder_->StopRecording();
2142 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002143 return -1;
2144 }
kwiberg5a25d952016-08-17 07:31:12 -07002145 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002146 _outputFileRecording = true;
2147
2148 return 0;
2149}
2150
2151int Channel::StartRecordingPlayout(OutStream* stream,
2152 const CodecInst* codecInst) {
2153 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2154 "Channel::StartRecordingPlayout()");
2155
2156 if (_outputFileRecording) {
2157 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2158 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002159 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002160 }
2161
2162 FileFormats format;
2163 const uint32_t notificationTime(0); // Not supported in VoE
2164 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2165
2166 if (codecInst != NULL && codecInst->channels != 1) {
2167 _engineStatisticsPtr->SetLastError(
2168 VE_BAD_ARGUMENT, kTraceError,
2169 "StartRecordingPlayout() invalid compression");
2170 return (-1);
2171 }
2172 if (codecInst == NULL) {
2173 format = kFileFormatPcm16kHzFile;
2174 codecInst = &dummyCodec;
2175 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2176 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2177 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2178 format = kFileFormatWavFile;
2179 } else {
2180 format = kFileFormatCompressedFile;
2181 }
2182
2183 rtc::CritScope cs(&_fileCritSect);
2184
2185 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002186 if (output_file_recorder_) {
2187 output_file_recorder_->RegisterModuleFileCallback(NULL);
2188 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002189 }
2190
kwiberg5a25d952016-08-17 07:31:12 -07002191 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002192 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002193 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002194 _engineStatisticsPtr->SetLastError(
2195 VE_INVALID_ARGUMENT, kTraceError,
2196 "StartRecordingPlayout() fileRecorder format isnot correct");
2197 return -1;
2198 }
2199
kwiberg4ec01d92016-08-22 08:43:54 -07002200 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002201 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002202 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2203 "StartRecordingPlayout() failed to "
2204 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002205 output_file_recorder_->StopRecording();
2206 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002207 return -1;
2208 }
2209
kwiberg5a25d952016-08-17 07:31:12 -07002210 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002211 _outputFileRecording = true;
2212
2213 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002214}
2215
kwiberg55b97fe2016-01-28 05:22:45 -08002216int Channel::StopRecordingPlayout() {
2217 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2218 "Channel::StopRecordingPlayout()");
2219
2220 if (!_outputFileRecording) {
2221 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2222 "StopRecordingPlayout() isnot recording");
2223 return -1;
2224 }
2225
2226 rtc::CritScope cs(&_fileCritSect);
2227
kwiberg5a25d952016-08-17 07:31:12 -07002228 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002229 _engineStatisticsPtr->SetLastError(
2230 VE_STOP_RECORDING_FAILED, kTraceError,
2231 "StopRecording() could not stop recording");
2232 return (-1);
2233 }
kwiberg5a25d952016-08-17 07:31:12 -07002234 output_file_recorder_->RegisterModuleFileCallback(NULL);
2235 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002236 _outputFileRecording = false;
2237
2238 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002239}
2240
kwiberg55b97fe2016-01-28 05:22:45 -08002241void Channel::SetMixWithMicStatus(bool mix) {
2242 rtc::CritScope cs(&_fileCritSect);
2243 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002244}
2245
kwiberg55b97fe2016-01-28 05:22:45 -08002246int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2247 int8_t currentLevel = _outputAudioLevel.Level();
2248 level = static_cast<int32_t>(currentLevel);
2249 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002250}
2251
kwiberg55b97fe2016-01-28 05:22:45 -08002252int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2253 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2254 level = static_cast<int32_t>(currentLevel);
2255 return 0;
2256}
2257
solenberg1c2af8e2016-03-24 10:36:00 -07002258int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002259 rtc::CritScope cs(&volume_settings_critsect_);
2260 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002261 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002262 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002263 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002264}
2265
solenberg1c2af8e2016-03-24 10:36:00 -07002266bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002267 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002268 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002269}
2270
kwiberg55b97fe2016-01-28 05:22:45 -08002271int Channel::SetOutputVolumePan(float left, float right) {
2272 rtc::CritScope cs(&volume_settings_critsect_);
2273 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002274 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002275 _panLeft = left;
2276 _panRight = right;
2277 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002278}
2279
kwiberg55b97fe2016-01-28 05:22:45 -08002280int Channel::GetOutputVolumePan(float& left, float& right) const {
2281 rtc::CritScope cs(&volume_settings_critsect_);
2282 left = _panLeft;
2283 right = _panRight;
2284 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002285}
2286
kwiberg55b97fe2016-01-28 05:22:45 -08002287int Channel::SetChannelOutputVolumeScaling(float scaling) {
2288 rtc::CritScope cs(&volume_settings_critsect_);
2289 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002290 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002291 _outputGain = scaling;
2292 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002293}
2294
kwiberg55b97fe2016-01-28 05:22:45 -08002295int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2296 rtc::CritScope cs(&volume_settings_critsect_);
2297 scaling = _outputGain;
2298 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002299}
2300
solenberg8842c3e2016-03-11 03:06:41 -08002301int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002302 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002303 "Channel::SendTelephoneEventOutband(...)");
2304 RTC_DCHECK_LE(0, event);
2305 RTC_DCHECK_GE(255, event);
2306 RTC_DCHECK_LE(0, duration_ms);
2307 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002308 if (!Sending()) {
2309 return -1;
2310 }
solenberg8842c3e2016-03-11 03:06:41 -08002311 if (_rtpRtcpModule->SendTelephoneEventOutband(
2312 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002313 _engineStatisticsPtr->SetLastError(
2314 VE_SEND_DTMF_FAILED, kTraceWarning,
2315 "SendTelephoneEventOutband() failed to send event");
2316 return -1;
2317 }
2318 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002319}
2320
solenberg31642aa2016-03-14 08:00:37 -07002321int Channel::SetSendTelephoneEventPayloadType(int payload_type) {
kwiberg55b97fe2016-01-28 05:22:45 -08002322 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002323 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002324 RTC_DCHECK_LE(0, payload_type);
2325 RTC_DCHECK_GE(127, payload_type);
2326 CodecInst codec = {0};
kwiberg55b97fe2016-01-28 05:22:45 -08002327 codec.plfreq = 8000;
solenberg31642aa2016-03-14 08:00:37 -07002328 codec.pltype = payload_type;
kwiberg55b97fe2016-01-28 05:22:45 -08002329 memcpy(codec.plname, "telephone-event", 16);
2330 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2331 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2332 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2333 _engineStatisticsPtr->SetLastError(
2334 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2335 "SetSendTelephoneEventPayloadType() failed to register send"
2336 "payload type");
2337 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002338 }
kwiberg55b97fe2016-01-28 05:22:45 -08002339 }
kwiberg55b97fe2016-01-28 05:22:45 -08002340 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002341}
2342
kwiberg55b97fe2016-01-28 05:22:45 -08002343int Channel::VoiceActivityIndicator(int& activity) {
2344 activity = _sendFrameType;
2345 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002346}
2347
kwiberg55b97fe2016-01-28 05:22:45 -08002348int Channel::SetLocalSSRC(unsigned int ssrc) {
2349 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2350 "Channel::SetLocalSSRC()");
2351 if (channel_state_.Get().sending) {
2352 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2353 "SetLocalSSRC() already sending");
2354 return -1;
2355 }
2356 _rtpRtcpModule->SetSSRC(ssrc);
2357 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002358}
2359
kwiberg55b97fe2016-01-28 05:22:45 -08002360int Channel::GetLocalSSRC(unsigned int& ssrc) {
2361 ssrc = _rtpRtcpModule->SSRC();
2362 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002363}
2364
kwiberg55b97fe2016-01-28 05:22:45 -08002365int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2366 ssrc = rtp_receiver_->SSRC();
2367 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002368}
2369
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002370int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002371 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002372 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002373}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002374
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002375int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2376 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002377 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2378 if (enable &&
2379 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2380 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002381 return -1;
2382 }
2383 return 0;
2384}
2385
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002386int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2387 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2388}
2389
2390int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2391 rtp_header_parser_->DeregisterRtpHeaderExtension(
2392 kRtpExtensionAbsoluteSendTime);
kwiberg55b97fe2016-01-28 05:22:45 -08002393 if (enable &&
2394 !rtp_header_parser_->RegisterRtpHeaderExtension(
2395 kRtpExtensionAbsoluteSendTime, id)) {
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002396 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002397 }
2398 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002399}
2400
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002401void Channel::EnableSendTransportSequenceNumber(int id) {
2402 int ret =
2403 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2404 RTC_DCHECK_EQ(0, ret);
2405}
2406
stefan3313ec92016-01-21 06:32:43 -08002407void Channel::EnableReceiveTransportSequenceNumber(int id) {
2408 rtp_header_parser_->DeregisterRtpHeaderExtension(
2409 kRtpExtensionTransportSequenceNumber);
2410 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2411 kRtpExtensionTransportSequenceNumber, id);
2412 RTC_DCHECK(ret);
2413}
2414
stefanbba9dec2016-02-01 04:39:55 -08002415void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002416 RtpPacketSender* rtp_packet_sender,
2417 TransportFeedbackObserver* transport_feedback_observer,
2418 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002419 RTC_DCHECK(rtp_packet_sender);
2420 RTC_DCHECK(transport_feedback_observer);
2421 RTC_DCHECK(packet_router && !packet_router_);
2422 feedback_observer_proxy_->SetTransportFeedbackObserver(
2423 transport_feedback_observer);
2424 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2425 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2426 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002427 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002428 packet_router_ = packet_router;
2429}
2430
stefanbba9dec2016-02-01 04:39:55 -08002431void Channel::RegisterReceiverCongestionControlObjects(
2432 PacketRouter* packet_router) {
2433 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002434 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002435 packet_router_ = packet_router;
2436}
2437
2438void Channel::ResetCongestionControlObjects() {
2439 RTC_DCHECK(packet_router_);
2440 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2441 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2442 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002443 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002444 packet_router_ = nullptr;
2445 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2446}
2447
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002448void Channel::SetRTCPStatus(bool enable) {
2449 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2450 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002451 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002452}
2453
kwiberg55b97fe2016-01-28 05:22:45 -08002454int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002455 RtcpMode method = _rtpRtcpModule->RTCP();
2456 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002457 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002458}
2459
kwiberg55b97fe2016-01-28 05:22:45 -08002460int Channel::SetRTCP_CNAME(const char cName[256]) {
2461 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2462 "Channel::SetRTCP_CNAME()");
2463 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2464 _engineStatisticsPtr->SetLastError(
2465 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2466 "SetRTCP_CNAME() failed to set RTCP CNAME");
2467 return -1;
2468 }
2469 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002470}
2471
kwiberg55b97fe2016-01-28 05:22:45 -08002472int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2473 if (cName == NULL) {
2474 _engineStatisticsPtr->SetLastError(
2475 VE_INVALID_ARGUMENT, kTraceError,
2476 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2477 return -1;
2478 }
2479 char cname[RTCP_CNAME_SIZE];
2480 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2481 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2482 _engineStatisticsPtr->SetLastError(
2483 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2484 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2485 return -1;
2486 }
2487 strcpy(cName, cname);
2488 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002489}
2490
kwiberg55b97fe2016-01-28 05:22:45 -08002491int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2492 unsigned int& NTPLow,
2493 unsigned int& timestamp,
2494 unsigned int& playoutTimestamp,
2495 unsigned int* jitter,
2496 unsigned short* fractionLost) {
2497 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002498
kwiberg55b97fe2016-01-28 05:22:45 -08002499 RTCPSenderInfo senderInfo;
2500 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2501 _engineStatisticsPtr->SetLastError(
2502 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2503 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2504 "side");
2505 return -1;
2506 }
2507
2508 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2509 // and octet count)
2510 NTPHigh = senderInfo.NTPseconds;
2511 NTPLow = senderInfo.NTPfraction;
2512 timestamp = senderInfo.RTPtimeStamp;
2513
2514 // --- Locally derived information
2515
2516 // This value is updated on each incoming RTCP packet (0 when no packet
2517 // has been received)
2518 playoutTimestamp = playout_timestamp_rtcp_;
2519
2520 if (NULL != jitter || NULL != fractionLost) {
2521 // Get all RTCP receiver report blocks that have been received on this
2522 // channel. If we receive RTP packets from a remote source we know the
2523 // remote SSRC and use the report block from him.
2524 // Otherwise use the first report block.
2525 std::vector<RTCPReportBlock> remote_stats;
2526 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2527 remote_stats.empty()) {
2528 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2529 "GetRemoteRTCPData() failed to measure statistics due"
2530 " to lack of received RTP and/or RTCP packets");
2531 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002532 }
2533
kwiberg55b97fe2016-01-28 05:22:45 -08002534 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2535 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2536 for (; it != remote_stats.end(); ++it) {
2537 if (it->remoteSSRC == remoteSSRC)
2538 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002539 }
kwiberg55b97fe2016-01-28 05:22:45 -08002540
2541 if (it == remote_stats.end()) {
2542 // If we have not received any RTCP packets from this SSRC it probably
2543 // means that we have not received any RTP packets.
2544 // Use the first received report block instead.
2545 it = remote_stats.begin();
2546 remoteSSRC = it->remoteSSRC;
2547 }
2548
2549 if (jitter) {
2550 *jitter = it->jitter;
2551 }
2552
2553 if (fractionLost) {
2554 *fractionLost = it->fractionLost;
2555 }
2556 }
2557 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002558}
2559
kwiberg55b97fe2016-01-28 05:22:45 -08002560int Channel::SendApplicationDefinedRTCPPacket(
2561 unsigned char subType,
2562 unsigned int name,
2563 const char* data,
2564 unsigned short dataLengthInBytes) {
2565 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2566 "Channel::SendApplicationDefinedRTCPPacket()");
2567 if (!channel_state_.Get().sending) {
2568 _engineStatisticsPtr->SetLastError(
2569 VE_NOT_SENDING, kTraceError,
2570 "SendApplicationDefinedRTCPPacket() not sending");
2571 return -1;
2572 }
2573 if (NULL == data) {
2574 _engineStatisticsPtr->SetLastError(
2575 VE_INVALID_ARGUMENT, kTraceError,
2576 "SendApplicationDefinedRTCPPacket() invalid data value");
2577 return -1;
2578 }
2579 if (dataLengthInBytes % 4 != 0) {
2580 _engineStatisticsPtr->SetLastError(
2581 VE_INVALID_ARGUMENT, kTraceError,
2582 "SendApplicationDefinedRTCPPacket() invalid length value");
2583 return -1;
2584 }
2585 RtcpMode status = _rtpRtcpModule->RTCP();
2586 if (status == RtcpMode::kOff) {
2587 _engineStatisticsPtr->SetLastError(
2588 VE_RTCP_ERROR, kTraceError,
2589 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2590 return -1;
2591 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002592
kwiberg55b97fe2016-01-28 05:22:45 -08002593 // Create and schedule the RTCP APP packet for transmission
2594 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2595 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2596 _engineStatisticsPtr->SetLastError(
2597 VE_SEND_ERROR, kTraceError,
2598 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2599 return -1;
2600 }
2601 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002602}
2603
kwiberg55b97fe2016-01-28 05:22:45 -08002604int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2605 unsigned int& maxJitterMs,
2606 unsigned int& discardedPackets) {
2607 // The jitter statistics is updated for each received RTP packet and is
2608 // based on received packets.
2609 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2610 // If RTCP is off, there is no timed thread in the RTCP module regularly
2611 // generating new stats, trigger the update manually here instead.
2612 StreamStatistician* statistician =
2613 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2614 if (statistician) {
2615 // Don't use returned statistics, use data from proxy instead so that
2616 // max jitter can be fetched atomically.
2617 RtcpStatistics s;
2618 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002619 }
kwiberg55b97fe2016-01-28 05:22:45 -08002620 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002621
kwiberg55b97fe2016-01-28 05:22:45 -08002622 ChannelStatistics stats = statistics_proxy_->GetStats();
2623 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2624 if (playoutFrequency > 0) {
2625 // Scale RTP statistics given the current playout frequency
2626 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2627 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2628 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002629
kwiberg55b97fe2016-01-28 05:22:45 -08002630 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002631
kwiberg55b97fe2016-01-28 05:22:45 -08002632 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002633}
2634
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002635int Channel::GetRemoteRTCPReportBlocks(
2636 std::vector<ReportBlock>* report_blocks) {
2637 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002638 _engineStatisticsPtr->SetLastError(
2639 VE_INVALID_ARGUMENT, kTraceError,
2640 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002641 return -1;
2642 }
2643
2644 // Get the report blocks from the latest received RTCP Sender or Receiver
2645 // Report. Each element in the vector contains the sender's SSRC and a
2646 // report block according to RFC 3550.
2647 std::vector<RTCPReportBlock> rtcp_report_blocks;
2648 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002649 return -1;
2650 }
2651
2652 if (rtcp_report_blocks.empty())
2653 return 0;
2654
2655 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2656 for (; it != rtcp_report_blocks.end(); ++it) {
2657 ReportBlock report_block;
2658 report_block.sender_SSRC = it->remoteSSRC;
2659 report_block.source_SSRC = it->sourceSSRC;
2660 report_block.fraction_lost = it->fractionLost;
2661 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2662 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2663 report_block.interarrival_jitter = it->jitter;
2664 report_block.last_SR_timestamp = it->lastSR;
2665 report_block.delay_since_last_SR = it->delaySinceLastSR;
2666 report_blocks->push_back(report_block);
2667 }
2668 return 0;
2669}
2670
kwiberg55b97fe2016-01-28 05:22:45 -08002671int Channel::GetRTPStatistics(CallStatistics& stats) {
2672 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002673
kwiberg55b97fe2016-01-28 05:22:45 -08002674 // The jitter statistics is updated for each received RTP packet and is
2675 // based on received packets.
2676 RtcpStatistics statistics;
2677 StreamStatistician* statistician =
2678 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002679 if (statistician) {
2680 statistician->GetStatistics(&statistics,
2681 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002682 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002683
kwiberg55b97fe2016-01-28 05:22:45 -08002684 stats.fractionLost = statistics.fraction_lost;
2685 stats.cumulativeLost = statistics.cumulative_lost;
2686 stats.extendedMax = statistics.extended_max_sequence_number;
2687 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002688
kwiberg55b97fe2016-01-28 05:22:45 -08002689 // --- RTT
2690 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002691
kwiberg55b97fe2016-01-28 05:22:45 -08002692 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002693
kwiberg55b97fe2016-01-28 05:22:45 -08002694 size_t bytesSent(0);
2695 uint32_t packetsSent(0);
2696 size_t bytesReceived(0);
2697 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002698
kwiberg55b97fe2016-01-28 05:22:45 -08002699 if (statistician) {
2700 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2701 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002702
kwiberg55b97fe2016-01-28 05:22:45 -08002703 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2704 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2705 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2706 " output will not be complete");
2707 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002708
kwiberg55b97fe2016-01-28 05:22:45 -08002709 stats.bytesSent = bytesSent;
2710 stats.packetsSent = packetsSent;
2711 stats.bytesReceived = bytesReceived;
2712 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002713
kwiberg55b97fe2016-01-28 05:22:45 -08002714 // --- Timestamps
2715 {
2716 rtc::CritScope lock(&ts_stats_lock_);
2717 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2718 }
2719 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002720}
2721
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002722int Channel::SetCodecFECStatus(bool enable) {
2723 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2724 "Channel::SetCodecFECStatus()");
2725
kwibergc8d071e2016-04-06 12:22:38 -07002726 if (!codec_manager_.SetCodecFEC(enable) ||
2727 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002728 _engineStatisticsPtr->SetLastError(
2729 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2730 "SetCodecFECStatus() failed to set FEC state");
2731 return -1;
2732 }
2733 return 0;
2734}
2735
2736bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002737 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002738}
2739
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002740void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2741 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002742 // If pacing is enabled we always store packets.
2743 if (!pacing_enabled_)
2744 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002745 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002746 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002747 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002748 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002749 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002750}
2751
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002752// Called when we are missing one or more packets.
2753int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002754 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2755}
2756
kwiberg55b97fe2016-01-28 05:22:45 -08002757uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2758 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2759 "Channel::Demultiplex()");
2760 _audioFrame.CopyFrom(audioFrame);
2761 _audioFrame.id_ = _channelId;
2762 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002763}
2764
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002765void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002766 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002767 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002768 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002769 CodecInst codec;
2770 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002771
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002772 // Never upsample or upmix the capture signal here. This should be done at the
2773 // end of the send chain.
2774 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2775 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2776 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2777 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002778}
2779
kwiberg55b97fe2016-01-28 05:22:45 -08002780uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2781 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2782 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002783
kwiberg55b97fe2016-01-28 05:22:45 -08002784 if (_audioFrame.samples_per_channel_ == 0) {
2785 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2786 "Channel::PrepareEncodeAndSend() invalid audio frame");
2787 return 0xFFFFFFFF;
2788 }
2789
2790 if (channel_state_.Get().input_file_playing) {
2791 MixOrReplaceAudioWithFile(mixingFrequency);
2792 }
2793
solenberg1c2af8e2016-03-24 10:36:00 -07002794 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2795 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002796
2797 if (channel_state_.Get().input_external_media) {
2798 rtc::CritScope cs(&_callbackCritSect);
2799 const bool isStereo = (_audioFrame.num_channels_ == 2);
2800 if (_inputExternalMediaCallbackPtr) {
2801 _inputExternalMediaCallbackPtr->Process(
2802 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
2803 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
2804 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00002805 }
kwiberg55b97fe2016-01-28 05:22:45 -08002806 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002807
kwiberg55b97fe2016-01-28 05:22:45 -08002808 if (_includeAudioLevelIndication) {
2809 size_t length =
2810 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002811 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002812 if (is_muted && previous_frame_muted_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002813 rms_level_.ProcessMuted(length);
2814 } else {
2815 rms_level_.Process(_audioFrame.data_, length);
niklase@google.com470e71d2011-07-07 08:21:25 +00002816 }
kwiberg55b97fe2016-01-28 05:22:45 -08002817 }
solenberg1c2af8e2016-03-24 10:36:00 -07002818 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002819
kwiberg55b97fe2016-01-28 05:22:45 -08002820 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002821}
2822
kwiberg55b97fe2016-01-28 05:22:45 -08002823uint32_t Channel::EncodeAndSend() {
2824 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2825 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002826
kwiberg55b97fe2016-01-28 05:22:45 -08002827 assert(_audioFrame.num_channels_ <= 2);
2828 if (_audioFrame.samples_per_channel_ == 0) {
2829 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2830 "Channel::EncodeAndSend() invalid audio frame");
2831 return 0xFFFFFFFF;
2832 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002833
kwiberg55b97fe2016-01-28 05:22:45 -08002834 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002835
kwiberg55b97fe2016-01-28 05:22:45 -08002836 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002837
kwiberg55b97fe2016-01-28 05:22:45 -08002838 // The ACM resamples internally.
2839 _audioFrame.timestamp_ = _timeStamp;
2840 // This call will trigger AudioPacketizationCallback::SendData if encoding
2841 // is done and payload is ready for packetization and transmission.
2842 // Otherwise, it will return without invoking the callback.
2843 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2844 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2845 "Channel::EncodeAndSend() ACM encoding failed");
2846 return 0xFFFFFFFF;
2847 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002848
kwiberg55b97fe2016-01-28 05:22:45 -08002849 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2850 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002851}
2852
Minyue2013aec2015-05-13 14:14:42 +02002853void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002854 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002855 Channel* channel = associate_send_channel_.channel();
2856 if (channel && channel->ChannelId() == channel_id) {
2857 // If this channel is associated with a send channel of the specified
2858 // Channel ID, disassociate with it.
2859 ChannelOwner ref(NULL);
2860 associate_send_channel_ = ref;
2861 }
2862}
2863
ivoc14d5dbe2016-07-04 07:06:55 -07002864void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2865 event_log_proxy_->SetEventLog(event_log);
2866}
2867
kwiberg55b97fe2016-01-28 05:22:45 -08002868int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
2869 VoEMediaProcess& processObject) {
2870 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2871 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002872
kwiberg55b97fe2016-01-28 05:22:45 -08002873 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002874
kwiberg55b97fe2016-01-28 05:22:45 -08002875 if (kPlaybackPerChannel == type) {
2876 if (_outputExternalMediaCallbackPtr) {
2877 _engineStatisticsPtr->SetLastError(
2878 VE_INVALID_OPERATION, kTraceError,
2879 "Channel::RegisterExternalMediaProcessing() "
2880 "output external media already enabled");
2881 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002882 }
kwiberg55b97fe2016-01-28 05:22:45 -08002883 _outputExternalMediaCallbackPtr = &processObject;
2884 _outputExternalMedia = true;
2885 } else if (kRecordingPerChannel == type) {
2886 if (_inputExternalMediaCallbackPtr) {
2887 _engineStatisticsPtr->SetLastError(
2888 VE_INVALID_OPERATION, kTraceError,
2889 "Channel::RegisterExternalMediaProcessing() "
2890 "output external media already enabled");
2891 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002892 }
kwiberg55b97fe2016-01-28 05:22:45 -08002893 _inputExternalMediaCallbackPtr = &processObject;
2894 channel_state_.SetInputExternalMedia(true);
2895 }
2896 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002897}
2898
kwiberg55b97fe2016-01-28 05:22:45 -08002899int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
2900 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2901 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002902
kwiberg55b97fe2016-01-28 05:22:45 -08002903 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002904
kwiberg55b97fe2016-01-28 05:22:45 -08002905 if (kPlaybackPerChannel == type) {
2906 if (!_outputExternalMediaCallbackPtr) {
2907 _engineStatisticsPtr->SetLastError(
2908 VE_INVALID_OPERATION, kTraceWarning,
2909 "Channel::DeRegisterExternalMediaProcessing() "
2910 "output external media already disabled");
2911 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002912 }
kwiberg55b97fe2016-01-28 05:22:45 -08002913 _outputExternalMedia = false;
2914 _outputExternalMediaCallbackPtr = NULL;
2915 } else if (kRecordingPerChannel == type) {
2916 if (!_inputExternalMediaCallbackPtr) {
2917 _engineStatisticsPtr->SetLastError(
2918 VE_INVALID_OPERATION, kTraceWarning,
2919 "Channel::DeRegisterExternalMediaProcessing() "
2920 "input external media already disabled");
2921 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002922 }
kwiberg55b97fe2016-01-28 05:22:45 -08002923 channel_state_.SetInputExternalMedia(false);
2924 _inputExternalMediaCallbackPtr = NULL;
2925 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002926
kwiberg55b97fe2016-01-28 05:22:45 -08002927 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002928}
2929
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002930int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08002931 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2932 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002933
kwiberg55b97fe2016-01-28 05:22:45 -08002934 if (channel_state_.Get().playing) {
2935 _engineStatisticsPtr->SetLastError(
2936 VE_INVALID_OPERATION, kTraceError,
2937 "Channel::SetExternalMixing() "
2938 "external mixing cannot be changed while playing.");
2939 return -1;
2940 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002941
kwiberg55b97fe2016-01-28 05:22:45 -08002942 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002943
kwiberg55b97fe2016-01-28 05:22:45 -08002944 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002945}
2946
kwiberg55b97fe2016-01-28 05:22:45 -08002947int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2948 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002949}
2950
wu@webrtc.org24301a62013-12-13 19:17:43 +00002951void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2952 audio_coding_->GetDecodingCallStatistics(stats);
2953}
2954
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002955bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
2956 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08002957 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07002958 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002959 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002960 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002961}
2962
solenberg358057b2015-11-27 10:46:42 -08002963uint32_t Channel::GetDelayEstimate() const {
2964 int jitter_buffer_delay_ms = 0;
2965 int playout_buffer_delay_ms = 0;
2966 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
2967 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
2968}
2969
deadbeef74375882015-08-13 12:09:10 -07002970int Channel::LeastRequiredDelayMs() const {
2971 return audio_coding_->LeastRequiredDelayMs();
2972}
2973
kwiberg55b97fe2016-01-28 05:22:45 -08002974int Channel::SetMinimumPlayoutDelay(int delayMs) {
2975 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2976 "Channel::SetMinimumPlayoutDelay()");
2977 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2978 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2979 _engineStatisticsPtr->SetLastError(
2980 VE_INVALID_ARGUMENT, kTraceError,
2981 "SetMinimumPlayoutDelay() invalid min delay");
2982 return -1;
2983 }
2984 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2985 _engineStatisticsPtr->SetLastError(
2986 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2987 "SetMinimumPlayoutDelay() failed to set min playout delay");
2988 return -1;
2989 }
2990 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002991}
2992
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002993int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002994 uint32_t playout_timestamp_rtp = 0;
2995 {
tommi31fc21f2016-01-21 10:37:37 -08002996 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002997 playout_timestamp_rtp = playout_timestamp_rtp_;
2998 }
kwiberg55b97fe2016-01-28 05:22:45 -08002999 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003000 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07003001 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003002 "GetPlayoutTimestamp() failed to retrieve timestamp");
3003 return -1;
3004 }
deadbeef74375882015-08-13 12:09:10 -07003005 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003006 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003007}
3008
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003009int Channel::SetInitTimestamp(unsigned int timestamp) {
3010 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003011 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003012 if (channel_state_.Get().sending) {
3013 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3014 "SetInitTimestamp() already sending");
3015 return -1;
3016 }
3017 _rtpRtcpModule->SetStartTimestamp(timestamp);
3018 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003019}
3020
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003021int Channel::SetInitSequenceNumber(short sequenceNumber) {
3022 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3023 "Channel::SetInitSequenceNumber()");
3024 if (channel_state_.Get().sending) {
3025 _engineStatisticsPtr->SetLastError(
3026 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3027 return -1;
3028 }
3029 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3030 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003031}
3032
kwiberg55b97fe2016-01-28 05:22:45 -08003033int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
3034 RtpReceiver** rtp_receiver) const {
3035 *rtpRtcpModule = _rtpRtcpModule.get();
3036 *rtp_receiver = rtp_receiver_.get();
3037 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003038}
3039
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003040// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3041// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08003042int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08003043 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08003044 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003045
kwiberg55b97fe2016-01-28 05:22:45 -08003046 {
3047 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003048
kwiberg5a25d952016-08-17 07:31:12 -07003049 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003050 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3051 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3052 " doesnt exist");
3053 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003054 }
3055
kwiberg4ec01d92016-08-22 08:43:54 -07003056 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07003057 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003058 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3059 "Channel::MixOrReplaceAudioWithFile() file mixing "
3060 "failed");
3061 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003062 }
kwiberg55b97fe2016-01-28 05:22:45 -08003063 if (fileSamples == 0) {
3064 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3065 "Channel::MixOrReplaceAudioWithFile() file is ended");
3066 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003067 }
kwiberg55b97fe2016-01-28 05:22:45 -08003068 }
3069
3070 assert(_audioFrame.samples_per_channel_ == fileSamples);
3071
3072 if (_mixFileWithMicrophone) {
3073 // Currently file stream is always mono.
3074 // TODO(xians): Change the code when FilePlayer supports real stereo.
3075 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3076 1, fileSamples);
3077 } else {
3078 // Replace ACM audio with file.
3079 // Currently file stream is always mono.
3080 // TODO(xians): Change the code when FilePlayer supports real stereo.
3081 _audioFrame.UpdateFrame(
3082 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3083 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3084 }
3085 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003086}
3087
kwiberg55b97fe2016-01-28 05:22:45 -08003088int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3089 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003090
kwibergb7f89d62016-02-17 10:04:18 -08003091 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003092 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003093
kwiberg55b97fe2016-01-28 05:22:45 -08003094 {
3095 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003096
kwiberg5a25d952016-08-17 07:31:12 -07003097 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003098 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3099 "Channel::MixAudioWithFile() file mixing failed");
3100 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003101 }
3102
kwiberg55b97fe2016-01-28 05:22:45 -08003103 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003104 if (output_file_player_->Get10msAudioFromFile(
3105 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003106 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3107 "Channel::MixAudioWithFile() file mixing failed");
3108 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003109 }
kwiberg55b97fe2016-01-28 05:22:45 -08003110 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003111
kwiberg55b97fe2016-01-28 05:22:45 -08003112 if (audioFrame.samples_per_channel_ == fileSamples) {
3113 // Currently file stream is always mono.
3114 // TODO(xians): Change the code when FilePlayer supports real stereo.
3115 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3116 fileSamples);
3117 } else {
3118 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3119 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3120 ") != "
3121 "fileSamples(%" PRIuS ")",
3122 audioFrame.samples_per_channel_, fileSamples);
3123 return -1;
3124 }
3125
3126 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003127}
3128
deadbeef74375882015-08-13 12:09:10 -07003129void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003130 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003131
henrik.lundin96bd5022016-04-06 04:13:56 -07003132 if (!jitter_buffer_playout_timestamp_) {
3133 // This can happen if this channel has not received any RTP packets. In
3134 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003135 return;
3136 }
3137
3138 uint16_t delay_ms = 0;
3139 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003140 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003141 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3142 " delay from the ADM");
3143 _engineStatisticsPtr->SetLastError(
3144 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3145 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3146 return;
3147 }
3148
henrik.lundin96bd5022016-04-06 04:13:56 -07003149 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3150 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003151
3152 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003153 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003154
kwiberg55b97fe2016-01-28 05:22:45 -08003155 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003156 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003157 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003158
3159 {
tommi31fc21f2016-01-21 10:37:37 -08003160 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003161 if (rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003162 playout_timestamp_rtcp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003163 } else {
henrik.lundin96bd5022016-04-06 04:13:56 -07003164 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003165 }
3166 playout_delay_ms_ = delay_ms;
3167 }
3168}
3169
kwiberg55b97fe2016-01-28 05:22:45 -08003170void Channel::RegisterReceiveCodecsToRTPModule() {
3171 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3172 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003173
kwiberg55b97fe2016-01-28 05:22:45 -08003174 CodecInst codec;
3175 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003176
kwiberg55b97fe2016-01-28 05:22:45 -08003177 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3178 // Open up the RTP/RTCP receiver for all supported codecs
3179 if ((audio_coding_->Codec(idx, &codec) == -1) ||
3180 (rtp_receiver_->RegisterReceivePayload(
3181 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3182 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
3183 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3184 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3185 " to register %s (%d/%d/%" PRIuS
3186 "/%d) to RTP/RTCP "
3187 "receiver",
3188 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3189 codec.rate);
3190 } else {
3191 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3192 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3193 "(%d/%d/%" PRIuS
3194 "/%d) has been added to the RTP/RTCP "
3195 "receiver",
3196 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3197 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003198 }
kwiberg55b97fe2016-01-28 05:22:45 -08003199 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003200}
3201
kwiberg55b97fe2016-01-28 05:22:45 -08003202int Channel::SetSendRtpHeaderExtension(bool enable,
3203 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003204 unsigned char id) {
3205 int error = 0;
3206 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3207 if (enable) {
3208 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3209 }
3210 return error;
3211}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003212
ossue280cde2016-10-12 11:04:10 -07003213int Channel::GetRtpTimestampRateHz() const {
3214 const auto format = audio_coding_->ReceiveFormat();
3215 // Default to the playout frequency if we've not gotten any packets yet.
3216 // TODO(ossu): Zero clockrate can only happen if we've added an external
3217 // decoder for a format we don't support internally. Remove once that way of
3218 // adding decoders is gone!
3219 return (format && format->clockrate_hz != 0)
3220 ? format->clockrate_hz
3221 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003222}
3223
Minyue2013aec2015-05-13 14:14:42 +02003224int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003225 RtcpMode method = _rtpRtcpModule->RTCP();
3226 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003227 return 0;
3228 }
3229 std::vector<RTCPReportBlock> report_blocks;
3230 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003231
3232 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003233 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003234 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003235 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003236 Channel* channel = associate_send_channel_.channel();
3237 // Tries to get RTT from an associated channel. This is important for
3238 // receive-only channels.
3239 if (channel) {
3240 // To prevent infinite recursion and deadlock, calling GetRTT of
3241 // associate channel should always use "false" for argument:
3242 // |allow_associate_channel|.
3243 rtt = channel->GetRTT(false);
3244 }
3245 }
3246 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003247 }
3248
3249 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3250 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3251 for (; it != report_blocks.end(); ++it) {
3252 if (it->remoteSSRC == remoteSSRC)
3253 break;
3254 }
3255 if (it == report_blocks.end()) {
3256 // We have not received packets with SSRC matching the report blocks.
3257 // To calculate RTT we try with the SSRC of the first report block.
3258 // This is very important for send-only channels where we don't know
3259 // the SSRC of the other end.
3260 remoteSSRC = report_blocks[0].remoteSSRC;
3261 }
Minyue2013aec2015-05-13 14:14:42 +02003262
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003263 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003264 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003265 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003266 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3267 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003268 return 0;
3269 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003270 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003271}
3272
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003273} // namespace voe
3274} // namespace webrtc