blob: a7df1f2176ce5ade86669c8295c461758f9a9b2b [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrika@webrtc.org2919e952012-01-31 08:45:03 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000011#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Henrik Lundin64dad832015-05-11 12:44:23 +020013#include <algorithm>
Tommif888bb52015-12-12 01:37:01 +010014#include <utility>
Henrik Lundin64dad832015-05-11 12:44:23 +020015
Ivo Creusenae856f22015-09-17 16:30:16 +020016#include "webrtc/base/checks.h"
tommi31fc21f2016-01-21 10:37:37 -080017#include "webrtc/base/criticalsection.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000018#include "webrtc/base/format_macros.h"
pbosad856222015-11-27 09:48:36 -080019#include "webrtc/base/logging.h"
Erik Språng737336d2016-07-29 12:59:36 +020020#include "webrtc/base/rate_limiter.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010021#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000022#include "webrtc/base/timeutils.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020023#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070024#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070025#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000026#include "webrtc/modules/audio_device/include/audio_device.h"
27#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010028#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010029#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
31#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
32#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000033#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010034#include "webrtc/modules/utility/include/audio_frame_operations.h"
35#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010036#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000037#include "webrtc/voice_engine/include/voe_external_media.h"
38#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
39#include "webrtc/voice_engine/output_mixer.h"
40#include "webrtc/voice_engine/statistics.h"
41#include "webrtc/voice_engine/transmit_mixer.h"
42#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000043
andrew@webrtc.org50419b02012-11-14 19:07:54 +000044namespace webrtc {
45namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000046
kwibergc8d071e2016-04-06 12:22:38 -070047namespace {
48
Erik Språng737336d2016-07-29 12:59:36 +020049constexpr int64_t kMaxRetransmissionWindowMs = 1000;
50constexpr int64_t kMinRetransmissionWindowMs = 30;
51
kwibergc8d071e2016-04-06 12:22:38 -070052} // namespace
53
solenberg8842c3e2016-03-11 03:06:41 -080054const int kTelephoneEventAttenuationdB = 10;
55
ivoc14d5dbe2016-07-04 07:06:55 -070056class RtcEventLogProxy final : public webrtc::RtcEventLog {
57 public:
58 RtcEventLogProxy() : event_log_(nullptr) {}
59
60 bool StartLogging(const std::string& file_name,
61 int64_t max_size_bytes) override {
62 RTC_NOTREACHED();
63 return false;
64 }
65
66 bool StartLogging(rtc::PlatformFile log_file,
67 int64_t max_size_bytes) override {
68 RTC_NOTREACHED();
69 return false;
70 }
71
72 void StopLogging() override { RTC_NOTREACHED(); }
73
74 void LogVideoReceiveStreamConfig(
75 const webrtc::VideoReceiveStream::Config& config) override {
76 rtc::CritScope lock(&crit_);
77 if (event_log_) {
78 event_log_->LogVideoReceiveStreamConfig(config);
79 }
80 }
81
82 void LogVideoSendStreamConfig(
83 const webrtc::VideoSendStream::Config& config) override {
84 rtc::CritScope lock(&crit_);
85 if (event_log_) {
86 event_log_->LogVideoSendStreamConfig(config);
87 }
88 }
89
ivoce0928d82016-10-10 05:12:51 -070090 void LogAudioReceiveStreamConfig(
91 const webrtc::AudioReceiveStream::Config& config) override {
92 rtc::CritScope lock(&crit_);
93 if (event_log_) {
94 event_log_->LogAudioReceiveStreamConfig(config);
95 }
96 }
97
98 void LogAudioSendStreamConfig(
99 const webrtc::AudioSendStream::Config& config) override {
100 rtc::CritScope lock(&crit_);
101 if (event_log_) {
102 event_log_->LogAudioSendStreamConfig(config);
103 }
104 }
105
ivoc14d5dbe2016-07-04 07:06:55 -0700106 void LogRtpHeader(webrtc::PacketDirection direction,
107 webrtc::MediaType media_type,
108 const uint8_t* header,
109 size_t packet_length) override {
110 rtc::CritScope lock(&crit_);
111 if (event_log_) {
112 event_log_->LogRtpHeader(direction, media_type, header, packet_length);
113 }
114 }
115
116 void LogRtcpPacket(webrtc::PacketDirection direction,
117 webrtc::MediaType media_type,
118 const uint8_t* packet,
119 size_t length) override {
120 rtc::CritScope lock(&crit_);
121 if (event_log_) {
122 event_log_->LogRtcpPacket(direction, media_type, packet, length);
123 }
124 }
125
126 void LogAudioPlayout(uint32_t ssrc) override {
127 rtc::CritScope lock(&crit_);
128 if (event_log_) {
129 event_log_->LogAudioPlayout(ssrc);
130 }
131 }
132
133 void LogBwePacketLossEvent(int32_t bitrate,
134 uint8_t fraction_loss,
135 int32_t total_packets) override {
136 rtc::CritScope lock(&crit_);
137 if (event_log_) {
138 event_log_->LogBwePacketLossEvent(bitrate, fraction_loss, total_packets);
139 }
140 }
141
142 void SetEventLog(RtcEventLog* event_log) {
143 rtc::CritScope lock(&crit_);
144 event_log_ = event_log;
145 }
146
147 private:
148 rtc::CriticalSection crit_;
149 RtcEventLog* event_log_ GUARDED_BY(crit_);
150 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
151};
152
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100153class TransportFeedbackProxy : public TransportFeedbackObserver {
154 public:
155 TransportFeedbackProxy() : feedback_observer_(nullptr) {
156 pacer_thread_.DetachFromThread();
157 network_thread_.DetachFromThread();
158 }
159
160 void SetTransportFeedbackObserver(
161 TransportFeedbackObserver* feedback_observer) {
162 RTC_DCHECK(thread_checker_.CalledOnValidThread());
163 rtc::CritScope lock(&crit_);
164 feedback_observer_ = feedback_observer;
165 }
166
167 // Implements TransportFeedbackObserver.
168 void AddPacket(uint16_t sequence_number,
169 size_t length,
philipela1ed0b32016-06-01 06:31:17 -0700170 int probe_cluster_id) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100171 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
172 rtc::CritScope lock(&crit_);
173 if (feedback_observer_)
pbos2169d8b2016-06-20 11:53:02 -0700174 feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100175 }
176 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
177 RTC_DCHECK(network_thread_.CalledOnValidThread());
178 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700179 if (feedback_observer_)
180 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200181 }
182 std::vector<PacketInfo> GetTransportFeedbackVector() const override {
183 RTC_NOTREACHED();
184 return std::vector<PacketInfo>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100185 }
186
187 private:
188 rtc::CriticalSection crit_;
189 rtc::ThreadChecker thread_checker_;
190 rtc::ThreadChecker pacer_thread_;
191 rtc::ThreadChecker network_thread_;
192 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
193};
194
195class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
196 public:
197 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
198 pacer_thread_.DetachFromThread();
199 }
200
201 void SetSequenceNumberAllocator(
202 TransportSequenceNumberAllocator* seq_num_allocator) {
203 RTC_DCHECK(thread_checker_.CalledOnValidThread());
204 rtc::CritScope lock(&crit_);
205 seq_num_allocator_ = seq_num_allocator;
206 }
207
208 // Implements TransportSequenceNumberAllocator.
209 uint16_t AllocateSequenceNumber() override {
210 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
211 rtc::CritScope lock(&crit_);
212 if (!seq_num_allocator_)
213 return 0;
214 return seq_num_allocator_->AllocateSequenceNumber();
215 }
216
217 private:
218 rtc::CriticalSection crit_;
219 rtc::ThreadChecker thread_checker_;
220 rtc::ThreadChecker pacer_thread_;
221 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
222};
223
224class RtpPacketSenderProxy : public RtpPacketSender {
225 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800226 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100227
228 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
229 RTC_DCHECK(thread_checker_.CalledOnValidThread());
230 rtc::CritScope lock(&crit_);
231 rtp_packet_sender_ = rtp_packet_sender;
232 }
233
234 // Implements RtpPacketSender.
235 void InsertPacket(Priority priority,
236 uint32_t ssrc,
237 uint16_t sequence_number,
238 int64_t capture_time_ms,
239 size_t bytes,
240 bool retransmission) override {
241 rtc::CritScope lock(&crit_);
242 if (rtp_packet_sender_) {
243 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
244 capture_time_ms, bytes, retransmission);
245 }
246 }
247
248 private:
249 rtc::ThreadChecker thread_checker_;
250 rtc::CriticalSection crit_;
251 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
252};
253
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000254// Extend the default RTCP statistics struct with max_jitter, defined as the
255// maximum jitter value seen in an RTCP report block.
256struct ChannelStatistics : public RtcpStatistics {
257 ChannelStatistics() : rtcp(), max_jitter(0) {}
258
259 RtcpStatistics rtcp;
260 uint32_t max_jitter;
261};
262
263// Statistics callback, called at each generation of a new RTCP report block.
264class StatisticsProxy : public RtcpStatisticsCallback {
265 public:
tommi31fc21f2016-01-21 10:37:37 -0800266 StatisticsProxy(uint32_t ssrc) : ssrc_(ssrc) {}
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000267 virtual ~StatisticsProxy() {}
268
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000269 void StatisticsUpdated(const RtcpStatistics& statistics,
270 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000271 if (ssrc != ssrc_)
272 return;
273
tommi31fc21f2016-01-21 10:37:37 -0800274 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000275 stats_.rtcp = statistics;
276 if (statistics.jitter > stats_.max_jitter) {
277 stats_.max_jitter = statistics.jitter;
278 }
279 }
280
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000281 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000282
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000283 ChannelStatistics GetStats() {
tommi31fc21f2016-01-21 10:37:37 -0800284 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000285 return stats_;
286 }
287
288 private:
289 // StatisticsUpdated calls are triggered from threads in the RTP module,
290 // while GetStats calls can be triggered from the public voice engine API,
291 // hence synchronization is needed.
tommi31fc21f2016-01-21 10:37:37 -0800292 rtc::CriticalSection stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000293 const uint32_t ssrc_;
294 ChannelStatistics stats_;
295};
296
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000297class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000298 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000299 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
300 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000301
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000302 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
303 // Not used for Voice Engine.
304 }
305
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000306 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
307 int64_t rtt,
308 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000309 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
310 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
311 // report for VoiceEngine?
312 if (report_blocks.empty())
313 return;
314
315 int fraction_lost_aggregate = 0;
316 int total_number_of_packets = 0;
317
318 // If receiving multiple report blocks, calculate the weighted average based
319 // on the number of packets a report refers to.
320 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
321 block_it != report_blocks.end(); ++block_it) {
322 // Find the previous extended high sequence number for this remote SSRC,
323 // to calculate the number of RTP packets this report refers to. Ignore if
324 // we haven't seen this SSRC before.
325 std::map<uint32_t, uint32_t>::iterator seq_num_it =
326 extended_max_sequence_number_.find(block_it->sourceSSRC);
327 int number_of_packets = 0;
328 if (seq_num_it != extended_max_sequence_number_.end()) {
329 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
330 }
331 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
332 total_number_of_packets += number_of_packets;
333
334 extended_max_sequence_number_[block_it->sourceSSRC] =
335 block_it->extendedHighSeqNum;
336 }
337 int weighted_fraction_lost = 0;
338 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800339 weighted_fraction_lost =
340 (fraction_lost_aggregate + total_number_of_packets / 2) /
341 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000342 }
343 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000344 }
345
346 private:
347 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000348 // Maps remote side ssrc to extended highest sequence number received.
349 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000350};
351
kwiberg55b97fe2016-01-28 05:22:45 -0800352int32_t Channel::SendData(FrameType frameType,
353 uint8_t payloadType,
354 uint32_t timeStamp,
355 const uint8_t* payloadData,
356 size_t payloadSize,
357 const RTPFragmentationHeader* fragmentation) {
358 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
359 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
360 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
361 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000362
kwiberg55b97fe2016-01-28 05:22:45 -0800363 if (_includeAudioLevelIndication) {
364 // Store current audio level in the RTP/RTCP module.
365 // The level will be used in combination with voice-activity state
366 // (frameType) to add an RTP header extension
367 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
368 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000369
kwiberg55b97fe2016-01-28 05:22:45 -0800370 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
371 // packetization.
372 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700373 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800374 (FrameType&)frameType, payloadType, timeStamp,
375 // Leaving the time when this frame was
376 // received from the capture device as
377 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700378 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800379 _engineStatisticsPtr->SetLastError(
380 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
381 "Channel::SendData() failed to send data to RTP/RTCP module");
382 return -1;
383 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000384
kwiberg55b97fe2016-01-28 05:22:45 -0800385 _lastLocalTimeStamp = timeStamp;
386 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000387
kwiberg55b97fe2016-01-28 05:22:45 -0800388 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000389}
390
kwiberg55b97fe2016-01-28 05:22:45 -0800391int32_t Channel::InFrameType(FrameType frame_type) {
392 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
393 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000394
kwiberg55b97fe2016-01-28 05:22:45 -0800395 rtc::CritScope cs(&_callbackCritSect);
396 _sendFrameType = (frame_type == kAudioFrameSpeech);
397 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000398}
399
stefan1d8a5062015-10-02 03:39:33 -0700400bool Channel::SendRtp(const uint8_t* data,
401 size_t len,
402 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800403 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
404 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
kwiberg55b97fe2016-01-28 05:22:45 -0800406 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000407
kwiberg55b97fe2016-01-28 05:22:45 -0800408 if (_transportPtr == NULL) {
409 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
410 "Channel::SendPacket() failed to send RTP packet due to"
411 " invalid transport object");
412 return false;
413 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
kwiberg55b97fe2016-01-28 05:22:45 -0800415 uint8_t* bufferToSendPtr = (uint8_t*)data;
416 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000417
kwiberg55b97fe2016-01-28 05:22:45 -0800418 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
419 std::string transport_name =
420 _externalTransport ? "external transport" : "WebRtc sockets";
421 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
422 "Channel::SendPacket() RTP transmission using %s failed",
423 transport_name.c_str());
424 return false;
425 }
426 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000427}
428
kwiberg55b97fe2016-01-28 05:22:45 -0800429bool Channel::SendRtcp(const uint8_t* data, size_t len) {
430 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
431 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000432
kwiberg55b97fe2016-01-28 05:22:45 -0800433 rtc::CritScope cs(&_callbackCritSect);
434 if (_transportPtr == NULL) {
435 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
436 "Channel::SendRtcp() failed to send RTCP packet"
437 " due to invalid transport object");
438 return false;
439 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000440
kwiberg55b97fe2016-01-28 05:22:45 -0800441 uint8_t* bufferToSendPtr = (uint8_t*)data;
442 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000443
kwiberg55b97fe2016-01-28 05:22:45 -0800444 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
445 if (n < 0) {
446 std::string transport_name =
447 _externalTransport ? "external transport" : "WebRtc sockets";
448 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
449 "Channel::SendRtcp() transmission using %s failed",
450 transport_name.c_str());
451 return false;
452 }
453 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000454}
455
kwiberg55b97fe2016-01-28 05:22:45 -0800456void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
457 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
458 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000459
kwiberg55b97fe2016-01-28 05:22:45 -0800460 // Update ssrc so that NTP for AV sync can be updated.
461 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000462}
463
Peter Boströmac547a62015-09-17 23:03:57 +0200464void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
465 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
466 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
467 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000468}
469
Peter Boströmac547a62015-09-17 23:03:57 +0200470int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000471 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000472 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000473 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800474 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200475 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800476 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
477 "Channel::OnInitializeDecoder(payloadType=%d, "
478 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
479 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000480
kwiberg55b97fe2016-01-28 05:22:45 -0800481 CodecInst receiveCodec = {0};
482 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000483
kwiberg55b97fe2016-01-28 05:22:45 -0800484 receiveCodec.pltype = payloadType;
485 receiveCodec.plfreq = frequency;
486 receiveCodec.channels = channels;
487 receiveCodec.rate = rate;
488 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000489
kwiberg55b97fe2016-01-28 05:22:45 -0800490 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
491 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
kwiberg55b97fe2016-01-28 05:22:45 -0800493 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700494 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
495 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800496 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
497 "Channel::OnInitializeDecoder() invalid codec ("
498 "pt=%d, name=%s) received - 1",
499 payloadType, payloadName);
500 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
501 return -1;
502 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000503
kwiberg55b97fe2016-01-28 05:22:45 -0800504 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000505}
506
kwiberg55b97fe2016-01-28 05:22:45 -0800507int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
508 size_t payloadSize,
509 const WebRtcRTPHeader* rtpHeader) {
510 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
511 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
512 ","
513 " payloadType=%u, audioChannel=%" PRIuS ")",
514 payloadSize, rtpHeader->header.payloadType,
515 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000516
kwiberg55b97fe2016-01-28 05:22:45 -0800517 if (!channel_state_.Get().playing) {
518 // Avoid inserting into NetEQ when we are not playing. Count the
519 // packet as discarded.
520 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
521 "received packet is discarded since playing is not"
522 " activated");
523 _numberOfDiscardedPackets++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000524 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800525 }
526
527 // Push the incoming payload (parsed and ready for decoding) into the ACM
528 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
529 0) {
530 _engineStatisticsPtr->SetLastError(
531 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
532 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
533 return -1;
534 }
535
kwiberg55b97fe2016-01-28 05:22:45 -0800536 int64_t round_trip_time = 0;
537 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
538 NULL);
539
540 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
541 if (!nack_list.empty()) {
542 // Can't use nack_list.data() since it's not supported by all
543 // compilers.
544 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
545 }
546 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000547}
548
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000549bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000550 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000551 RTPHeader header;
552 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
553 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
554 "IncomingPacket invalid RTP header");
555 return false;
556 }
557 header.payload_type_frequency =
558 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
559 if (header.payload_type_frequency < 0)
560 return false;
561 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
562}
563
henrik.lundin42dda502016-05-18 05:36:01 -0700564MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
565 int32_t id,
566 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700567 unsigned int ssrc;
568 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
569 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800570 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700571 bool muted;
572 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
573 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800574 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
575 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
576 // In all likelihood, the audio in this frame is garbage. We return an
577 // error so that the audio mixer module doesn't add it to the mix. As
578 // a result, it won't be played out and the actions skipped here are
579 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700580 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800581 }
henrik.lundina89ab962016-05-18 08:52:45 -0700582
583 if (muted) {
584 // TODO(henrik.lundin): We should be able to do better than this. But we
585 // will have to go through all the cases below where the audio samples may
586 // be used, and handle the muted case in some way.
587 audioFrame->Mute();
588 }
kwiberg55b97fe2016-01-28 05:22:45 -0800589
kwiberg55b97fe2016-01-28 05:22:45 -0800590 // Convert module ID to internal VoE channel ID
591 audioFrame->id_ = VoEChannelId(audioFrame->id_);
592 // Store speech type for dead-or-alive detection
593 _outputSpeechType = audioFrame->speech_type_;
594
595 ChannelState::State state = channel_state_.Get();
596
kwiberg55b97fe2016-01-28 05:22:45 -0800597 {
598 // Pass the audio buffers to an optional sink callback, before applying
599 // scaling/panning, as that applies to the mix operation.
600 // External recipients of the audio (e.g. via AudioTrack), will do their
601 // own mixing/dynamic processing.
602 rtc::CritScope cs(&_callbackCritSect);
603 if (audio_sink_) {
604 AudioSinkInterface::Data data(
605 &audioFrame->data_[0], audioFrame->samples_per_channel_,
606 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
607 audioFrame->timestamp_);
608 audio_sink_->OnData(data);
609 }
610 }
611
612 float output_gain = 1.0f;
613 float left_pan = 1.0f;
614 float right_pan = 1.0f;
615 {
616 rtc::CritScope cs(&volume_settings_critsect_);
617 output_gain = _outputGain;
618 left_pan = _panLeft;
619 right_pan = _panRight;
620 }
621
622 // Output volume scaling
623 if (output_gain < 0.99f || output_gain > 1.01f) {
624 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
625 }
626
627 // Scale left and/or right channel(s) if stereo and master balance is
628 // active
629
630 if (left_pan != 1.0f || right_pan != 1.0f) {
631 if (audioFrame->num_channels_ == 1) {
632 // Emulate stereo mode since panning is active.
633 // The mono signal is copied to both left and right channels here.
634 AudioFrameOperations::MonoToStereo(audioFrame);
635 }
636 // For true stereo mode (when we are receiving a stereo signal), no
637 // action is needed.
638
639 // Do the panning operation (the audio frame contains stereo at this
640 // stage)
641 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
642 }
643
644 // Mix decoded PCM output with file if file mixing is enabled
645 if (state.output_file_playing) {
646 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700647 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800648 }
649
650 // External media
651 if (_outputExternalMedia) {
652 rtc::CritScope cs(&_callbackCritSect);
653 const bool isStereo = (audioFrame->num_channels_ == 2);
654 if (_outputExternalMediaCallbackPtr) {
655 _outputExternalMediaCallbackPtr->Process(
656 _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_,
657 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
658 isStereo);
659 }
660 }
661
662 // Record playout if enabled
663 {
664 rtc::CritScope cs(&_fileCritSect);
665
kwiberg5a25d952016-08-17 07:31:12 -0700666 if (_outputFileRecording && output_file_recorder_) {
667 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800668 }
669 }
670
671 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700672 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800673 _outputAudioLevel.ComputeLevel(*audioFrame);
674
675 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
676 // The first frame with a valid rtp timestamp.
677 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
678 }
679
680 if (capture_start_rtp_time_stamp_ >= 0) {
681 // audioFrame.timestamp_ should be valid from now on.
682
683 // Compute elapsed time.
684 int64_t unwrap_timestamp =
685 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
686 audioFrame->elapsed_time_ms_ =
687 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700688 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800689
niklase@google.com470e71d2011-07-07 08:21:25 +0000690 {
kwiberg55b97fe2016-01-28 05:22:45 -0800691 rtc::CritScope lock(&ts_stats_lock_);
692 // Compute ntp time.
693 audioFrame->ntp_time_ms_ =
694 ntp_estimator_.Estimate(audioFrame->timestamp_);
695 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
696 if (audioFrame->ntp_time_ms_ > 0) {
697 // Compute |capture_start_ntp_time_ms_| so that
698 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
699 capture_start_ntp_time_ms_ =
700 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000701 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000702 }
kwiberg55b97fe2016-01-28 05:22:45 -0800703 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000704
henrik.lundin42dda502016-05-18 05:36:01 -0700705 return muted ? MixerParticipant::AudioFrameInfo::kMuted
706 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000707}
708
aleloi6c278492016-10-20 14:24:39 -0700709AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
710 int sample_rate_hz,
711 AudioFrame* audio_frame) {
712 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700713
aleloi6c278492016-10-20 14:24:39 -0700714 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700715
716 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
717 FrameInfo new_audio_frame_info = FrameInfo::kError;
718 switch (frame_info) {
719 case MixerParticipant::AudioFrameInfo::kNormal:
720 new_audio_frame_info = FrameInfo::kNormal;
721 break;
722 case MixerParticipant::AudioFrameInfo::kMuted:
723 new_audio_frame_info = FrameInfo::kMuted;
724 break;
725 case MixerParticipant::AudioFrameInfo::kError:
726 new_audio_frame_info = FrameInfo::kError;
727 break;
728 }
aleloi6c278492016-10-20 14:24:39 -0700729 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700730}
731
kwiberg55b97fe2016-01-28 05:22:45 -0800732int32_t Channel::NeededFrequency(int32_t id) const {
733 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
734 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000735
kwiberg55b97fe2016-01-28 05:22:45 -0800736 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000737
kwiberg55b97fe2016-01-28 05:22:45 -0800738 // Determine highest needed receive frequency
739 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000740
kwiberg55b97fe2016-01-28 05:22:45 -0800741 // Return the bigger of playout and receive frequency in the ACM.
742 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
743 highestNeeded = audio_coding_->PlayoutFrequency();
744 } else {
745 highestNeeded = receiveFrequency;
746 }
747
748 // Special case, if we're playing a file on the playout side
749 // we take that frequency into consideration as well
750 // This is not needed on sending side, since the codec will
751 // limit the spectrum anyway.
752 if (channel_state_.Get().output_file_playing) {
753 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700754 if (output_file_player_) {
755 if (output_file_player_->Frequency() > highestNeeded) {
756 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800757 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000758 }
kwiberg55b97fe2016-01-28 05:22:45 -0800759 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000760
kwiberg55b97fe2016-01-28 05:22:45 -0800761 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000762}
763
ossu5f7cfa52016-05-30 08:11:28 -0700764int32_t Channel::CreateChannel(
765 Channel*& channel,
766 int32_t channelId,
767 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700768 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800769 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
770 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
771 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000772
solenberg88499ec2016-09-07 07:34:41 -0700773 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800774 if (channel == NULL) {
775 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
776 "Channel::CreateChannel() unable to allocate memory for"
777 " channel");
778 return -1;
779 }
780 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000781}
782
kwiberg55b97fe2016-01-28 05:22:45 -0800783void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
784 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
785 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
786 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000787
kwiberg55b97fe2016-01-28 05:22:45 -0800788 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000789}
790
kwiberg55b97fe2016-01-28 05:22:45 -0800791void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
792 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
793 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
794 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000795
kwiberg55b97fe2016-01-28 05:22:45 -0800796 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000797}
798
kwiberg55b97fe2016-01-28 05:22:45 -0800799void Channel::PlayFileEnded(int32_t id) {
800 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
801 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000802
kwiberg55b97fe2016-01-28 05:22:45 -0800803 if (id == _inputFilePlayerId) {
804 channel_state_.SetInputFilePlaying(false);
805 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
806 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000807 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800808 } else if (id == _outputFilePlayerId) {
809 channel_state_.SetOutputFilePlaying(false);
810 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
811 "Channel::PlayFileEnded() => output file player module is"
812 " shutdown");
813 }
814}
815
816void Channel::RecordFileEnded(int32_t id) {
817 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
818 "Channel::RecordFileEnded(id=%d)", id);
819
820 assert(id == _outputFileRecorderId);
821
822 rtc::CritScope cs(&_fileCritSect);
823
824 _outputFileRecording = false;
825 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
826 "Channel::RecordFileEnded() => output file recorder module is"
827 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000828}
829
pbos@webrtc.org92135212013-05-14 08:31:39 +0000830Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000831 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700832 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800833 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100834 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700835 event_log_proxy_(new RtcEventLogProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100836 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800837 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100838 rtp_receive_statistics_(
839 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
840 rtp_receiver_(
841 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100842 this,
843 this,
844 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700845 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100846 _outputAudioLevel(),
847 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100848 // Avoid conflict with other channels by adding 1024 - 1026,
849 // won't use as much as 1024 channels.
850 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
851 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
852 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
853 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100854 _outputExternalMedia(false),
855 _inputExternalMediaCallbackPtr(NULL),
856 _outputExternalMediaCallbackPtr(NULL),
857 _timeStamp(0), // This is just an offset, RTP module will add it's own
858 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100859 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100860 playout_timestamp_rtp_(0),
861 playout_timestamp_rtcp_(0),
862 playout_delay_ms_(0),
863 _numberOfDiscardedPackets(0),
864 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100865 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
866 capture_start_rtp_time_stamp_(-1),
867 capture_start_ntp_time_ms_(-1),
868 _engineStatisticsPtr(NULL),
869 _outputMixerPtr(NULL),
870 _transmitMixerPtr(NULL),
871 _moduleProcessThreadPtr(NULL),
872 _audioDeviceModulePtr(NULL),
873 _voiceEngineObserverPtr(NULL),
874 _callbackCritSectPtr(NULL),
875 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100876 _sendFrameType(0),
877 _externalMixing(false),
878 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700879 input_mute_(false),
880 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100881 _panLeft(1.0f),
882 _panRight(1.0f),
883 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100884 _lastLocalTimeStamp(0),
885 _lastPayloadType(0),
886 _includeAudioLevelIndication(false),
887 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100888 restored_packet_in_use_(false),
889 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100890 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700891 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800892 feedback_observer_proxy_(new TransportFeedbackProxy()),
893 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700894 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200895 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
896 kMaxRetransmissionWindowMs)),
michaelt2fedf9c2016-11-28 02:34:18 -0800897 decoder_factory_(config.acm_config.decoder_factory),
898 // Bitrate smoother can be initialized with arbitrary time constant
899 // (0 used here). The actual time constant will be set in SetBitRate.
900 bitrate_smoother_(0, Clock::GetRealTimeClock()) {
kwiberg55b97fe2016-01-28 05:22:45 -0800901 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
902 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700903 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800904 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700905 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800906 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200907
kwiberg55b97fe2016-01-28 05:22:45 -0800908 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000909
kwiberg55b97fe2016-01-28 05:22:45 -0800910 RtpRtcp::Configuration configuration;
911 configuration.audio = true;
912 configuration.outgoing_transport = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800913 configuration.receive_statistics = rtp_receive_statistics_.get();
914 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800915 if (pacing_enabled_) {
916 configuration.paced_sender = rtp_packet_sender_proxy_.get();
917 configuration.transport_sequence_number_allocator =
918 seq_num_allocator_proxy_.get();
919 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
920 }
ivoc14d5dbe2016-07-04 07:06:55 -0700921 configuration.event_log = &(*event_log_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200922 configuration.retransmission_rate_limiter =
923 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000924
kwiberg55b97fe2016-01-28 05:22:45 -0800925 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100926 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000927
kwiberg55b97fe2016-01-28 05:22:45 -0800928 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
929 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
930 statistics_proxy_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000931}
932
kwiberg55b97fe2016-01-28 05:22:45 -0800933Channel::~Channel() {
934 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
935 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
936 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000937
kwiberg55b97fe2016-01-28 05:22:45 -0800938 if (_outputExternalMedia) {
939 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
940 }
941 if (channel_state_.Get().input_external_media) {
942 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
943 }
944 StopSend();
945 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000946
kwiberg55b97fe2016-01-28 05:22:45 -0800947 {
948 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700949 if (input_file_player_) {
950 input_file_player_->RegisterModuleFileCallback(NULL);
951 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000952 }
kwiberg5a25d952016-08-17 07:31:12 -0700953 if (output_file_player_) {
954 output_file_player_->RegisterModuleFileCallback(NULL);
955 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800956 }
kwiberg5a25d952016-08-17 07:31:12 -0700957 if (output_file_recorder_) {
958 output_file_recorder_->RegisterModuleFileCallback(NULL);
959 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800960 }
961 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000962
kwiberg55b97fe2016-01-28 05:22:45 -0800963 // The order to safely shutdown modules in a channel is:
964 // 1. De-register callbacks in modules
965 // 2. De-register modules in process thread
966 // 3. Destroy modules
967 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
968 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
969 "~Channel() failed to de-register transport callback"
970 " (Audio coding module)");
971 }
972 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
973 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
974 "~Channel() failed to de-register VAD callback"
975 " (Audio coding module)");
976 }
977 // De-register modules in process thread
978 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000979
kwiberg55b97fe2016-01-28 05:22:45 -0800980 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000981}
982
kwiberg55b97fe2016-01-28 05:22:45 -0800983int32_t Channel::Init() {
984 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
985 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000986
kwiberg55b97fe2016-01-28 05:22:45 -0800987 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000988
kwiberg55b97fe2016-01-28 05:22:45 -0800989 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000990
kwiberg55b97fe2016-01-28 05:22:45 -0800991 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
992 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
993 "Channel::Init() must call SetEngineInformation() first");
994 return -1;
995 }
996
997 // --- Add modules to process thread (for periodic schedulation)
998
999 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
1000
1001 // --- ACM initialization
1002
1003 if (audio_coding_->InitializeReceiver() == -1) {
1004 _engineStatisticsPtr->SetLastError(
1005 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1006 "Channel::Init() unable to initialize the ACM - 1");
1007 return -1;
1008 }
1009
1010 // --- RTP/RTCP module initialization
1011
1012 // Ensure that RTCP is enabled by default for the created channel.
1013 // Note that, the module will keep generating RTCP until it is explicitly
1014 // disabled by the user.
1015 // After StopListen (when no sockets exists), RTCP packets will no longer
1016 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001017 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001018 // RTCP is enabled by default.
1019 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1020 // --- Register all permanent callbacks
1021 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1022 (audio_coding_->RegisterVADCallback(this) == -1);
1023
1024 if (fail) {
1025 _engineStatisticsPtr->SetLastError(
1026 VE_CANNOT_INIT_CHANNEL, kTraceError,
1027 "Channel::Init() callbacks not registered");
1028 return -1;
1029 }
1030
1031 // --- Register all supported codecs to the receiving side of the
1032 // RTP/RTCP module
1033
1034 CodecInst codec;
1035 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1036
1037 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1038 // Open up the RTP/RTCP receiver for all supported codecs
1039 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08001040 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001041 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1042 "Channel::Init() unable to register %s "
1043 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1044 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1045 codec.rate);
1046 } else {
1047 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1048 "Channel::Init() %s (%d/%d/%" PRIuS
1049 "/%d) has been "
1050 "added to the RTP/RTCP receiver",
1051 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1052 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001053 }
1054
kwiberg55b97fe2016-01-28 05:22:45 -08001055 // Ensure that PCMU is used as default codec on the sending side
1056 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1057 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001058 }
1059
kwiberg55b97fe2016-01-28 05:22:45 -08001060 // Register default PT for outband 'telephone-event'
1061 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001062 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001063 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1064 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001065 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1066 "Channel::Init() failed to register outband "
1067 "'telephone-event' (%d/%d) correctly",
1068 codec.pltype, codec.plfreq);
1069 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001070 }
1071
kwiberg55b97fe2016-01-28 05:22:45 -08001072 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001073 if (!codec_manager_.RegisterEncoder(codec) ||
1074 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001075 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1076 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001077 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001078 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1079 "Channel::Init() failed to register CN (%d/%d) "
1080 "correctly - 1",
1081 codec.pltype, codec.plfreq);
1082 }
1083 }
kwiberg55b97fe2016-01-28 05:22:45 -08001084 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001085
kwiberg55b97fe2016-01-28 05:22:45 -08001086 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001087}
1088
kwiberg55b97fe2016-01-28 05:22:45 -08001089int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1090 OutputMixer& outputMixer,
1091 voe::TransmitMixer& transmitMixer,
1092 ProcessThread& moduleProcessThread,
1093 AudioDeviceModule& audioDeviceModule,
1094 VoiceEngineObserver* voiceEngineObserver,
1095 rtc::CriticalSection* callbackCritSect) {
1096 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1097 "Channel::SetEngineInformation()");
1098 _engineStatisticsPtr = &engineStatistics;
1099 _outputMixerPtr = &outputMixer;
1100 _transmitMixerPtr = &transmitMixer,
1101 _moduleProcessThreadPtr = &moduleProcessThread;
1102 _audioDeviceModulePtr = &audioDeviceModule;
1103 _voiceEngineObserverPtr = voiceEngineObserver;
1104 _callbackCritSectPtr = callbackCritSect;
1105 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001106}
1107
kwiberg55b97fe2016-01-28 05:22:45 -08001108int32_t Channel::UpdateLocalTimeStamp() {
1109 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1110 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001111}
1112
kwibergb7f89d62016-02-17 10:04:18 -08001113void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001114 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001115 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001116}
1117
ossu29b1a8d2016-06-13 07:34:51 -07001118const rtc::scoped_refptr<AudioDecoderFactory>&
1119Channel::GetAudioDecoderFactory() const {
1120 return decoder_factory_;
1121}
1122
kwiberg55b97fe2016-01-28 05:22:45 -08001123int32_t Channel::StartPlayout() {
1124 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1125 "Channel::StartPlayout()");
1126 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001127 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001128 }
1129
1130 if (!_externalMixing) {
1131 // Add participant as candidates for mixing.
1132 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1133 _engineStatisticsPtr->SetLastError(
1134 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1135 "StartPlayout() failed to add participant to mixer");
1136 return -1;
1137 }
1138 }
1139
1140 channel_state_.SetPlaying(true);
1141 if (RegisterFilePlayingToMixer() != 0)
1142 return -1;
1143
1144 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001145}
1146
kwiberg55b97fe2016-01-28 05:22:45 -08001147int32_t Channel::StopPlayout() {
1148 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1149 "Channel::StopPlayout()");
1150 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001151 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001152 }
1153
1154 if (!_externalMixing) {
1155 // Remove participant as candidates for mixing
1156 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1157 _engineStatisticsPtr->SetLastError(
1158 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1159 "StopPlayout() failed to remove participant from mixer");
1160 return -1;
1161 }
1162 }
1163
1164 channel_state_.SetPlaying(false);
1165 _outputAudioLevel.Clear();
1166
1167 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001168}
1169
kwiberg55b97fe2016-01-28 05:22:45 -08001170int32_t Channel::StartSend() {
1171 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1172 "Channel::StartSend()");
1173 // Resume the previous sequence number which was reset by StopSend().
1174 // This needs to be done before |sending| is set to true.
1175 if (send_sequence_number_)
1176 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001177
kwiberg55b97fe2016-01-28 05:22:45 -08001178 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001179 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001180 }
1181 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001182
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001183 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001184 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1185 _engineStatisticsPtr->SetLastError(
1186 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1187 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001188 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001189 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001190 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001191 return -1;
1192 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001193
kwiberg55b97fe2016-01-28 05:22:45 -08001194 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001195}
1196
kwiberg55b97fe2016-01-28 05:22:45 -08001197int32_t Channel::StopSend() {
1198 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1199 "Channel::StopSend()");
1200 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001201 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001202 }
1203 channel_state_.SetSending(false);
1204
1205 // Store the sequence number to be able to pick up the same sequence for
1206 // the next StartSend(). This is needed for restarting device, otherwise
1207 // it might cause libSRTP to complain about packets being replayed.
1208 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1209 // CL is landed. See issue
1210 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1211 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1212
1213 // Reset sending SSRC and sequence number and triggers direct transmission
1214 // of RTCP BYE
1215 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1216 _engineStatisticsPtr->SetLastError(
1217 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1218 "StartSend() RTP/RTCP failed to stop sending");
1219 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001220 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001221
1222 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001223}
1224
solenberge566ac72016-10-31 12:52:33 -07001225void Channel::ResetDiscardedPacketCount() {
kwiberg55b97fe2016-01-28 05:22:45 -08001226 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberge566ac72016-10-31 12:52:33 -07001227 "Channel::ResetDiscardedPacketCount()");
kwiberg55b97fe2016-01-28 05:22:45 -08001228 _numberOfDiscardedPackets = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001229}
1230
kwiberg55b97fe2016-01-28 05:22:45 -08001231int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1232 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1233 "Channel::RegisterVoiceEngineObserver()");
1234 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001235
kwiberg55b97fe2016-01-28 05:22:45 -08001236 if (_voiceEngineObserverPtr) {
1237 _engineStatisticsPtr->SetLastError(
1238 VE_INVALID_OPERATION, kTraceError,
1239 "RegisterVoiceEngineObserver() observer already enabled");
1240 return -1;
1241 }
1242 _voiceEngineObserverPtr = &observer;
1243 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001244}
1245
kwiberg55b97fe2016-01-28 05:22:45 -08001246int32_t Channel::DeRegisterVoiceEngineObserver() {
1247 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1248 "Channel::DeRegisterVoiceEngineObserver()");
1249 rtc::CritScope cs(&_callbackCritSect);
1250
1251 if (!_voiceEngineObserverPtr) {
1252 _engineStatisticsPtr->SetLastError(
1253 VE_INVALID_OPERATION, kTraceWarning,
1254 "DeRegisterVoiceEngineObserver() observer already disabled");
1255 return 0;
1256 }
1257 _voiceEngineObserverPtr = NULL;
1258 return 0;
1259}
1260
1261int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001262 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001263 if (send_codec) {
1264 codec = *send_codec;
1265 return 0;
1266 }
1267 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001268}
1269
kwiberg55b97fe2016-01-28 05:22:45 -08001270int32_t Channel::GetRecCodec(CodecInst& codec) {
1271 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001272}
1273
kwiberg55b97fe2016-01-28 05:22:45 -08001274int32_t Channel::SetSendCodec(const CodecInst& codec) {
1275 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1276 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001277
kwibergc8d071e2016-04-06 12:22:38 -07001278 if (!codec_manager_.RegisterEncoder(codec) ||
1279 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001280 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1281 "SetSendCodec() failed to register codec to ACM");
1282 return -1;
1283 }
1284
1285 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1286 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1287 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1288 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1289 "SetSendCodec() failed to register codec to"
1290 " RTP/RTCP module");
1291 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001292 }
kwiberg55b97fe2016-01-28 05:22:45 -08001293 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001294
kwiberg55b97fe2016-01-28 05:22:45 -08001295 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) {
1296 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1297 "SetSendCodec() failed to set audio packet size");
1298 return -1;
1299 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001300
kwiberg55b97fe2016-01-28 05:22:45 -08001301 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001302}
1303
Ivo Creusenadf89b72015-04-29 16:03:33 +02001304void Channel::SetBitRate(int bitrate_bps) {
1305 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1306 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001307 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1308 if (*encoder)
1309 (*encoder)->OnReceivedTargetAudioBitrate(bitrate_bps);
1310 });
Erik Språng737336d2016-07-29 12:59:36 +02001311 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
michaelt2fedf9c2016-11-28 02:34:18 -08001312
1313 // We give smoothed bitrate allocation to audio network adaptor as
1314 // the uplink bandwidth.
1315 // TODO(michaelt) : Remove kDefaultBitrateSmoothingTimeConstantMs as soon as
1316 // we pass the probing interval to this function.
1317 constexpr int64_t kDefaultBitrateSmoothingTimeConstantMs = 20000;
1318 bitrate_smoother_.SetTimeConstantMs(kDefaultBitrateSmoothingTimeConstantMs);
1319 bitrate_smoother_.AddSample(bitrate_bps);
1320 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1321 if (*encoder) {
1322 (*encoder)->OnReceivedUplinkBandwidth(
1323 static_cast<int>(*bitrate_smoother_.GetAverage()));
1324 }
1325 });
Ivo Creusenadf89b72015-04-29 16:03:33 +02001326}
1327
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001328void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001329 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1330 if (*encoder)
1331 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1332 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001333}
1334
kwiberg55b97fe2016-01-28 05:22:45 -08001335int32_t Channel::SetVADStatus(bool enableVAD,
1336 ACMVADMode mode,
1337 bool disableDTX) {
1338 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1339 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001340 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1341 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1342 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001343 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1344 kTraceError,
1345 "SetVADStatus() failed to set VAD");
1346 return -1;
1347 }
1348 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001349}
1350
kwiberg55b97fe2016-01-28 05:22:45 -08001351int32_t Channel::GetVADStatus(bool& enabledVAD,
1352 ACMVADMode& mode,
1353 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001354 const auto* params = codec_manager_.GetStackParams();
1355 enabledVAD = params->use_cng;
1356 mode = params->vad_mode;
1357 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001358 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001359}
1360
kwiberg55b97fe2016-01-28 05:22:45 -08001361int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1362 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1363 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001364
kwiberg55b97fe2016-01-28 05:22:45 -08001365 if (channel_state_.Get().playing) {
1366 _engineStatisticsPtr->SetLastError(
1367 VE_ALREADY_PLAYING, kTraceError,
1368 "SetRecPayloadType() unable to set PT while playing");
1369 return -1;
1370 }
kwiberg55b97fe2016-01-28 05:22:45 -08001371
1372 if (codec.pltype == -1) {
1373 // De-register the selected codec (RTP/RTCP module and ACM)
1374
1375 int8_t pltype(-1);
1376 CodecInst rxCodec = codec;
1377
1378 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001379 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001380 rxCodec.pltype = pltype;
1381
1382 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1383 _engineStatisticsPtr->SetLastError(
1384 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1385 "SetRecPayloadType() RTP/RTCP-module deregistration "
1386 "failed");
1387 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001388 }
kwiberg55b97fe2016-01-28 05:22:45 -08001389 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1390 _engineStatisticsPtr->SetLastError(
1391 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1392 "SetRecPayloadType() ACM deregistration failed - 1");
1393 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001394 }
kwiberg55b97fe2016-01-28 05:22:45 -08001395 return 0;
1396 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001397
magjed56124bd2016-11-24 09:34:46 -08001398 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001399 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001400 // TODO(kwiberg): Retrying is probably not necessary, since
1401 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001402 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001403 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001404 _engineStatisticsPtr->SetLastError(
1405 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1406 "SetRecPayloadType() RTP/RTCP-module registration failed");
1407 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001408 }
kwiberg55b97fe2016-01-28 05:22:45 -08001409 }
kwibergda2bf4e2016-10-24 13:47:09 -07001410 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1411 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001412 audio_coding_->UnregisterReceiveCodec(codec.pltype);
kwibergda2bf4e2016-10-24 13:47:09 -07001413 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1414 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001415 _engineStatisticsPtr->SetLastError(
1416 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1417 "SetRecPayloadType() ACM registration failed - 1");
1418 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001419 }
kwiberg55b97fe2016-01-28 05:22:45 -08001420 }
1421 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001422}
1423
kwiberg55b97fe2016-01-28 05:22:45 -08001424int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1425 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001426 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001427 _engineStatisticsPtr->SetLastError(
1428 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1429 "GetRecPayloadType() failed to retrieve RX payload type");
1430 return -1;
1431 }
1432 codec.pltype = payloadType;
1433 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001434}
1435
kwiberg55b97fe2016-01-28 05:22:45 -08001436int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1437 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1438 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001439
kwiberg55b97fe2016-01-28 05:22:45 -08001440 CodecInst codec;
1441 int32_t samplingFreqHz(-1);
1442 const size_t kMono = 1;
1443 if (frequency == kFreq32000Hz)
1444 samplingFreqHz = 32000;
1445 else if (frequency == kFreq16000Hz)
1446 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001447
kwiberg55b97fe2016-01-28 05:22:45 -08001448 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1449 _engineStatisticsPtr->SetLastError(
1450 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1451 "SetSendCNPayloadType() failed to retrieve default CN codec "
1452 "settings");
1453 return -1;
1454 }
1455
1456 // Modify the payload type (must be set to dynamic range)
1457 codec.pltype = type;
1458
kwibergc8d071e2016-04-06 12:22:38 -07001459 if (!codec_manager_.RegisterEncoder(codec) ||
1460 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001461 _engineStatisticsPtr->SetLastError(
1462 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1463 "SetSendCNPayloadType() failed to register CN to ACM");
1464 return -1;
1465 }
1466
1467 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1468 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1469 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1470 _engineStatisticsPtr->SetLastError(
1471 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1472 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1473 "module");
1474 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001475 }
kwiberg55b97fe2016-01-28 05:22:45 -08001476 }
1477 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001478}
1479
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001480int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001481 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001482 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001483
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001484 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001485 _engineStatisticsPtr->SetLastError(
1486 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001487 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001488 return -1;
1489 }
1490 return 0;
1491}
1492
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001493int Channel::SetOpusDtx(bool enable_dtx) {
1494 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1495 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001496 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001497 : audio_coding_->DisableOpusDtx();
1498 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001499 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1500 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001501 return -1;
1502 }
1503 return 0;
1504}
1505
ivoc85228d62016-07-27 04:53:47 -07001506int Channel::GetOpusDtx(bool* enabled) {
1507 int success = -1;
1508 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1509 if (encoder) {
1510 *enabled = encoder->GetDtx();
1511 success = 0;
1512 }
1513 });
1514 return success;
1515}
1516
minyue7e304322016-10-12 05:00:55 -07001517bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1518 bool success = false;
1519 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1520 if (*encoder) {
1521 success = (*encoder)->EnableAudioNetworkAdaptor(
1522 config_string, Clock::GetRealTimeClock());
1523 }
1524 });
1525 return success;
1526}
1527
1528void Channel::DisableAudioNetworkAdaptor() {
1529 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1530 if (*encoder)
1531 (*encoder)->DisableAudioNetworkAdaptor();
1532 });
1533}
1534
1535void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1536 int max_frame_length_ms) {
1537 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1538 if (*encoder) {
1539 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1540 max_frame_length_ms);
1541 }
1542 });
1543}
1544
mflodman3d7db262016-04-29 00:57:13 -07001545int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001546 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001547 "Channel::RegisterExternalTransport()");
1548
kwiberg55b97fe2016-01-28 05:22:45 -08001549 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001550 if (_externalTransport) {
1551 _engineStatisticsPtr->SetLastError(
1552 VE_INVALID_OPERATION, kTraceError,
1553 "RegisterExternalTransport() external transport already enabled");
1554 return -1;
1555 }
1556 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001557 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001558 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001559}
1560
kwiberg55b97fe2016-01-28 05:22:45 -08001561int32_t Channel::DeRegisterExternalTransport() {
1562 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1563 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001564
kwiberg55b97fe2016-01-28 05:22:45 -08001565 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001566 if (_transportPtr) {
1567 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1568 "DeRegisterExternalTransport() all transport is disabled");
1569 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001570 _engineStatisticsPtr->SetLastError(
1571 VE_INVALID_OPERATION, kTraceWarning,
1572 "DeRegisterExternalTransport() external transport already "
1573 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001574 }
1575 _externalTransport = false;
1576 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001577 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001578}
1579
mflodman3d7db262016-04-29 00:57:13 -07001580int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001581 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001582 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001583 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001584 "Channel::ReceivedRTPPacket()");
1585
1586 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001587 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001588
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001589 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001590 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1591 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1592 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001593 return -1;
1594 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001595 header.payload_type_frequency =
1596 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001597 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001598 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001599 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001600 rtp_receive_statistics_->IncomingPacket(
1601 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001602 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001603
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001604 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001605}
1606
1607bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001608 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001609 const RTPHeader& header,
1610 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001611 if (rtp_payload_registry_->IsRtx(header)) {
1612 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001613 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001614 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001615 assert(packet_length >= header.headerLength);
1616 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001617 PayloadUnion payload_specific;
1618 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001619 &payload_specific)) {
1620 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001621 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001622 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1623 payload_specific, in_order);
1624}
1625
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001626bool Channel::HandleRtxPacket(const uint8_t* packet,
1627 size_t packet_length,
1628 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001629 if (!rtp_payload_registry_->IsRtx(header))
1630 return false;
1631
1632 // Remove the RTX header and parse the original RTP header.
1633 if (packet_length < header.headerLength)
1634 return false;
1635 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1636 return false;
1637 if (restored_packet_in_use_) {
1638 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1639 "Multiple RTX headers detected, dropping packet");
1640 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001641 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001642 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001643 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1644 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001645 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1646 "Incoming RTX packet: invalid RTP header");
1647 return false;
1648 }
1649 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001650 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001651 restored_packet_in_use_ = false;
1652 return ret;
1653}
1654
1655bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1656 StreamStatistician* statistician =
1657 rtp_receive_statistics_->GetStatistician(header.ssrc);
1658 if (!statistician)
1659 return false;
1660 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001661}
1662
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001663bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1664 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001665 // Retransmissions are handled separately if RTX is enabled.
1666 if (rtp_payload_registry_->RtxEnabled())
1667 return false;
1668 StreamStatistician* statistician =
1669 rtp_receive_statistics_->GetStatistician(header.ssrc);
1670 if (!statistician)
1671 return false;
1672 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001673 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001674 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001675 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001676}
1677
mflodman3d7db262016-04-29 00:57:13 -07001678int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001679 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001680 "Channel::ReceivedRTCPPacket()");
1681 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001682 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001683
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001684 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001685 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001686 _engineStatisticsPtr->SetLastError(
1687 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1688 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1689 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001690
Minyue2013aec2015-05-13 14:14:42 +02001691 int64_t rtt = GetRTT(true);
1692 if (rtt == 0) {
1693 // Waiting for valid RTT.
1694 return 0;
1695 }
Erik Språng737336d2016-07-29 12:59:36 +02001696
1697 int64_t nack_window_ms = rtt;
1698 if (nack_window_ms < kMinRetransmissionWindowMs) {
1699 nack_window_ms = kMinRetransmissionWindowMs;
1700 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1701 nack_window_ms = kMaxRetransmissionWindowMs;
1702 }
1703 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1704
minyue7e304322016-10-12 05:00:55 -07001705 // Invoke audio encoders OnReceivedRtt().
1706 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1707 if (*encoder)
1708 (*encoder)->OnReceivedRtt(rtt);
1709 });
1710
Minyue2013aec2015-05-13 14:14:42 +02001711 uint32_t ntp_secs = 0;
1712 uint32_t ntp_frac = 0;
1713 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001714 if (0 !=
1715 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1716 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001717 // Waiting for RTCP.
1718 return 0;
1719 }
1720
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001721 {
tommi31fc21f2016-01-21 10:37:37 -08001722 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001723 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001724 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001725 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001726}
1727
niklase@google.com470e71d2011-07-07 08:21:25 +00001728int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001729 bool loop,
1730 FileFormats format,
1731 int startPosition,
1732 float volumeScaling,
1733 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001734 const CodecInst* codecInst) {
1735 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1736 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1737 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1738 "stopPosition=%d)",
1739 fileName, loop, format, volumeScaling, startPosition,
1740 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001741
kwiberg55b97fe2016-01-28 05:22:45 -08001742 if (channel_state_.Get().output_file_playing) {
1743 _engineStatisticsPtr->SetLastError(
1744 VE_ALREADY_PLAYING, kTraceError,
1745 "StartPlayingFileLocally() is already playing");
1746 return -1;
1747 }
1748
1749 {
1750 rtc::CritScope cs(&_fileCritSect);
1751
kwiberg5a25d952016-08-17 07:31:12 -07001752 if (output_file_player_) {
1753 output_file_player_->RegisterModuleFileCallback(NULL);
1754 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001755 }
1756
kwiberg5b356f42016-09-08 04:32:33 -07001757 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001758 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001759
kwiberg5a25d952016-08-17 07:31:12 -07001760 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001761 _engineStatisticsPtr->SetLastError(
1762 VE_INVALID_ARGUMENT, kTraceError,
1763 "StartPlayingFileLocally() filePlayer format is not correct");
1764 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001765 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001766
kwiberg55b97fe2016-01-28 05:22:45 -08001767 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001768
kwiberg5a25d952016-08-17 07:31:12 -07001769 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001770 fileName, loop, startPosition, volumeScaling, notificationTime,
1771 stopPosition, (const CodecInst*)codecInst) != 0) {
1772 _engineStatisticsPtr->SetLastError(
1773 VE_BAD_FILE, kTraceError,
1774 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001775 output_file_player_->StopPlayingFile();
1776 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001777 return -1;
1778 }
kwiberg5a25d952016-08-17 07:31:12 -07001779 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001780 channel_state_.SetOutputFilePlaying(true);
1781 }
1782
1783 if (RegisterFilePlayingToMixer() != 0)
1784 return -1;
1785
1786 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001787}
1788
1789int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001790 FileFormats format,
1791 int startPosition,
1792 float volumeScaling,
1793 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001794 const CodecInst* codecInst) {
1795 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1796 "Channel::StartPlayingFileLocally(format=%d,"
1797 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1798 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001799
kwiberg55b97fe2016-01-28 05:22:45 -08001800 if (stream == NULL) {
1801 _engineStatisticsPtr->SetLastError(
1802 VE_BAD_FILE, kTraceError,
1803 "StartPlayingFileLocally() NULL as input stream");
1804 return -1;
1805 }
1806
1807 if (channel_state_.Get().output_file_playing) {
1808 _engineStatisticsPtr->SetLastError(
1809 VE_ALREADY_PLAYING, kTraceError,
1810 "StartPlayingFileLocally() is already playing");
1811 return -1;
1812 }
1813
1814 {
1815 rtc::CritScope cs(&_fileCritSect);
1816
1817 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001818 if (output_file_player_) {
1819 output_file_player_->RegisterModuleFileCallback(NULL);
1820 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001821 }
1822
kwiberg55b97fe2016-01-28 05:22:45 -08001823 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001824 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001825 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001826
kwiberg5a25d952016-08-17 07:31:12 -07001827 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001828 _engineStatisticsPtr->SetLastError(
1829 VE_INVALID_ARGUMENT, kTraceError,
1830 "StartPlayingFileLocally() filePlayer format isnot correct");
1831 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001832 }
1833
kwiberg55b97fe2016-01-28 05:22:45 -08001834 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001835
kwiberg4ec01d92016-08-22 08:43:54 -07001836 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001837 volumeScaling, notificationTime,
1838 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001839 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1840 "StartPlayingFile() failed to "
1841 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001842 output_file_player_->StopPlayingFile();
1843 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001844 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001845 }
kwiberg5a25d952016-08-17 07:31:12 -07001846 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001847 channel_state_.SetOutputFilePlaying(true);
1848 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001849
kwiberg55b97fe2016-01-28 05:22:45 -08001850 if (RegisterFilePlayingToMixer() != 0)
1851 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001852
kwiberg55b97fe2016-01-28 05:22:45 -08001853 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001854}
1855
kwiberg55b97fe2016-01-28 05:22:45 -08001856int Channel::StopPlayingFileLocally() {
1857 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1858 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001859
kwiberg55b97fe2016-01-28 05:22:45 -08001860 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001861 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001862 }
1863
1864 {
1865 rtc::CritScope cs(&_fileCritSect);
1866
kwiberg5a25d952016-08-17 07:31:12 -07001867 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001868 _engineStatisticsPtr->SetLastError(
1869 VE_STOP_RECORDING_FAILED, kTraceError,
1870 "StopPlayingFile() could not stop playing");
1871 return -1;
1872 }
kwiberg5a25d952016-08-17 07:31:12 -07001873 output_file_player_->RegisterModuleFileCallback(NULL);
1874 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001875 channel_state_.SetOutputFilePlaying(false);
1876 }
1877 // _fileCritSect cannot be taken while calling
1878 // SetAnonymousMixibilityStatus. Refer to comments in
1879 // StartPlayingFileLocally(const char* ...) for more details.
1880 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1881 _engineStatisticsPtr->SetLastError(
1882 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1883 "StopPlayingFile() failed to stop participant from playing as"
1884 "file in the mixer");
1885 return -1;
1886 }
1887
1888 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001889}
1890
kwiberg55b97fe2016-01-28 05:22:45 -08001891int Channel::IsPlayingFileLocally() const {
1892 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001893}
1894
kwiberg55b97fe2016-01-28 05:22:45 -08001895int Channel::RegisterFilePlayingToMixer() {
1896 // Return success for not registering for file playing to mixer if:
1897 // 1. playing file before playout is started on that channel.
1898 // 2. starting playout without file playing on that channel.
1899 if (!channel_state_.Get().playing ||
1900 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001901 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001902 }
1903
1904 // |_fileCritSect| cannot be taken while calling
1905 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1906 // frames can be pulled by the mixer. Since the frames are generated from
1907 // the file, _fileCritSect will be taken. This would result in a deadlock.
1908 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1909 channel_state_.SetOutputFilePlaying(false);
1910 rtc::CritScope cs(&_fileCritSect);
1911 _engineStatisticsPtr->SetLastError(
1912 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1913 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001914 output_file_player_->StopPlayingFile();
1915 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001916 return -1;
1917 }
1918
1919 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001920}
1921
niklase@google.com470e71d2011-07-07 08:21:25 +00001922int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001923 bool loop,
1924 FileFormats format,
1925 int startPosition,
1926 float volumeScaling,
1927 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001928 const CodecInst* codecInst) {
1929 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1930 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1931 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1932 "stopPosition=%d)",
1933 fileName, loop, format, volumeScaling, startPosition,
1934 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001935
kwiberg55b97fe2016-01-28 05:22:45 -08001936 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001937
kwiberg55b97fe2016-01-28 05:22:45 -08001938 if (channel_state_.Get().input_file_playing) {
1939 _engineStatisticsPtr->SetLastError(
1940 VE_ALREADY_PLAYING, kTraceWarning,
1941 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001942 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001943 }
1944
1945 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001946 if (input_file_player_) {
1947 input_file_player_->RegisterModuleFileCallback(NULL);
1948 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001949 }
1950
1951 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001952 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001953 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001954
kwiberg5a25d952016-08-17 07:31:12 -07001955 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001956 _engineStatisticsPtr->SetLastError(
1957 VE_INVALID_ARGUMENT, kTraceError,
1958 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1959 return -1;
1960 }
1961
1962 const uint32_t notificationTime(0);
1963
kwiberg5a25d952016-08-17 07:31:12 -07001964 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001965 fileName, loop, startPosition, volumeScaling, notificationTime,
1966 stopPosition, (const CodecInst*)codecInst) != 0) {
1967 _engineStatisticsPtr->SetLastError(
1968 VE_BAD_FILE, kTraceError,
1969 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001970 input_file_player_->StopPlayingFile();
1971 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001972 return -1;
1973 }
kwiberg5a25d952016-08-17 07:31:12 -07001974 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001975 channel_state_.SetInputFilePlaying(true);
1976
1977 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001978}
1979
1980int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001981 FileFormats format,
1982 int startPosition,
1983 float volumeScaling,
1984 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001985 const CodecInst* codecInst) {
1986 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1987 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1988 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1989 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001990
kwiberg55b97fe2016-01-28 05:22:45 -08001991 if (stream == NULL) {
1992 _engineStatisticsPtr->SetLastError(
1993 VE_BAD_FILE, kTraceError,
1994 "StartPlayingFileAsMicrophone NULL as input stream");
1995 return -1;
1996 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001997
kwiberg55b97fe2016-01-28 05:22:45 -08001998 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001999
kwiberg55b97fe2016-01-28 05:22:45 -08002000 if (channel_state_.Get().input_file_playing) {
2001 _engineStatisticsPtr->SetLastError(
2002 VE_ALREADY_PLAYING, kTraceWarning,
2003 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002004 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002005 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002006
kwiberg55b97fe2016-01-28 05:22:45 -08002007 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002008 if (input_file_player_) {
2009 input_file_player_->RegisterModuleFileCallback(NULL);
2010 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002011 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002012
kwiberg55b97fe2016-01-28 05:22:45 -08002013 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002014 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002015 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002016
kwiberg5a25d952016-08-17 07:31:12 -07002017 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002018 _engineStatisticsPtr->SetLastError(
2019 VE_INVALID_ARGUMENT, kTraceError,
2020 "StartPlayingInputFile() filePlayer format isnot correct");
2021 return -1;
2022 }
2023
2024 const uint32_t notificationTime(0);
2025
kwiberg4ec01d92016-08-22 08:43:54 -07002026 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2027 notificationTime, stopPosition,
2028 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002029 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2030 "StartPlayingFile() failed to start "
2031 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002032 input_file_player_->StopPlayingFile();
2033 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002034 return -1;
2035 }
2036
kwiberg5a25d952016-08-17 07:31:12 -07002037 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002038 channel_state_.SetInputFilePlaying(true);
2039
2040 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002041}
2042
kwiberg55b97fe2016-01-28 05:22:45 -08002043int Channel::StopPlayingFileAsMicrophone() {
2044 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2045 "Channel::StopPlayingFileAsMicrophone()");
2046
2047 rtc::CritScope cs(&_fileCritSect);
2048
2049 if (!channel_state_.Get().input_file_playing) {
2050 return 0;
2051 }
2052
kwiberg5a25d952016-08-17 07:31:12 -07002053 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002054 _engineStatisticsPtr->SetLastError(
2055 VE_STOP_RECORDING_FAILED, kTraceError,
2056 "StopPlayingFile() could not stop playing");
2057 return -1;
2058 }
kwiberg5a25d952016-08-17 07:31:12 -07002059 input_file_player_->RegisterModuleFileCallback(NULL);
2060 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002061 channel_state_.SetInputFilePlaying(false);
2062
2063 return 0;
2064}
2065
2066int Channel::IsPlayingFileAsMicrophone() const {
2067 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002068}
2069
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002070int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002071 const CodecInst* codecInst) {
2072 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2073 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002074
kwiberg55b97fe2016-01-28 05:22:45 -08002075 if (_outputFileRecording) {
2076 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2077 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002078 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002079 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002080
kwiberg55b97fe2016-01-28 05:22:45 -08002081 FileFormats format;
2082 const uint32_t notificationTime(0); // Not supported in VoE
2083 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002084
kwiberg55b97fe2016-01-28 05:22:45 -08002085 if ((codecInst != NULL) &&
2086 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2087 _engineStatisticsPtr->SetLastError(
2088 VE_BAD_ARGUMENT, kTraceError,
2089 "StartRecordingPlayout() invalid compression");
2090 return (-1);
2091 }
2092 if (codecInst == NULL) {
2093 format = kFileFormatPcm16kHzFile;
2094 codecInst = &dummyCodec;
2095 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2096 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2097 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2098 format = kFileFormatWavFile;
2099 } else {
2100 format = kFileFormatCompressedFile;
2101 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002102
kwiberg55b97fe2016-01-28 05:22:45 -08002103 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002104
kwiberg55b97fe2016-01-28 05:22:45 -08002105 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002106 if (output_file_recorder_) {
2107 output_file_recorder_->RegisterModuleFileCallback(NULL);
2108 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002109 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002110
kwiberg5a25d952016-08-17 07:31:12 -07002111 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002112 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002113 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002114 _engineStatisticsPtr->SetLastError(
2115 VE_INVALID_ARGUMENT, kTraceError,
2116 "StartRecordingPlayout() fileRecorder format isnot correct");
2117 return -1;
2118 }
2119
kwiberg5a25d952016-08-17 07:31:12 -07002120 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002121 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2122 _engineStatisticsPtr->SetLastError(
2123 VE_BAD_FILE, kTraceError,
2124 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002125 output_file_recorder_->StopRecording();
2126 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002127 return -1;
2128 }
kwiberg5a25d952016-08-17 07:31:12 -07002129 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002130 _outputFileRecording = true;
2131
2132 return 0;
2133}
2134
2135int Channel::StartRecordingPlayout(OutStream* stream,
2136 const CodecInst* codecInst) {
2137 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2138 "Channel::StartRecordingPlayout()");
2139
2140 if (_outputFileRecording) {
2141 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2142 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002143 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002144 }
2145
2146 FileFormats format;
2147 const uint32_t notificationTime(0); // Not supported in VoE
2148 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2149
2150 if (codecInst != NULL && codecInst->channels != 1) {
2151 _engineStatisticsPtr->SetLastError(
2152 VE_BAD_ARGUMENT, kTraceError,
2153 "StartRecordingPlayout() invalid compression");
2154 return (-1);
2155 }
2156 if (codecInst == NULL) {
2157 format = kFileFormatPcm16kHzFile;
2158 codecInst = &dummyCodec;
2159 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2160 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2161 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2162 format = kFileFormatWavFile;
2163 } else {
2164 format = kFileFormatCompressedFile;
2165 }
2166
2167 rtc::CritScope cs(&_fileCritSect);
2168
2169 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002170 if (output_file_recorder_) {
2171 output_file_recorder_->RegisterModuleFileCallback(NULL);
2172 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002173 }
2174
kwiberg5a25d952016-08-17 07:31:12 -07002175 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002176 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002177 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002178 _engineStatisticsPtr->SetLastError(
2179 VE_INVALID_ARGUMENT, kTraceError,
2180 "StartRecordingPlayout() fileRecorder format isnot correct");
2181 return -1;
2182 }
2183
kwiberg4ec01d92016-08-22 08:43:54 -07002184 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002185 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002186 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2187 "StartRecordingPlayout() failed to "
2188 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002189 output_file_recorder_->StopRecording();
2190 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002191 return -1;
2192 }
2193
kwiberg5a25d952016-08-17 07:31:12 -07002194 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002195 _outputFileRecording = true;
2196
2197 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002198}
2199
kwiberg55b97fe2016-01-28 05:22:45 -08002200int Channel::StopRecordingPlayout() {
2201 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2202 "Channel::StopRecordingPlayout()");
2203
2204 if (!_outputFileRecording) {
2205 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2206 "StopRecordingPlayout() isnot recording");
2207 return -1;
2208 }
2209
2210 rtc::CritScope cs(&_fileCritSect);
2211
kwiberg5a25d952016-08-17 07:31:12 -07002212 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002213 _engineStatisticsPtr->SetLastError(
2214 VE_STOP_RECORDING_FAILED, kTraceError,
2215 "StopRecording() could not stop recording");
2216 return (-1);
2217 }
kwiberg5a25d952016-08-17 07:31:12 -07002218 output_file_recorder_->RegisterModuleFileCallback(NULL);
2219 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002220 _outputFileRecording = false;
2221
2222 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002223}
2224
kwiberg55b97fe2016-01-28 05:22:45 -08002225void Channel::SetMixWithMicStatus(bool mix) {
2226 rtc::CritScope cs(&_fileCritSect);
2227 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002228}
2229
kwiberg55b97fe2016-01-28 05:22:45 -08002230int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2231 int8_t currentLevel = _outputAudioLevel.Level();
2232 level = static_cast<int32_t>(currentLevel);
2233 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002234}
2235
kwiberg55b97fe2016-01-28 05:22:45 -08002236int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2237 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2238 level = static_cast<int32_t>(currentLevel);
2239 return 0;
2240}
2241
solenberg1c2af8e2016-03-24 10:36:00 -07002242int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002243 rtc::CritScope cs(&volume_settings_critsect_);
2244 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002245 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002246 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002247 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002248}
2249
solenberg1c2af8e2016-03-24 10:36:00 -07002250bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002251 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002252 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002253}
2254
kwiberg55b97fe2016-01-28 05:22:45 -08002255int Channel::SetOutputVolumePan(float left, float right) {
2256 rtc::CritScope cs(&volume_settings_critsect_);
2257 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002258 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002259 _panLeft = left;
2260 _panRight = right;
2261 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002262}
2263
kwiberg55b97fe2016-01-28 05:22:45 -08002264int Channel::GetOutputVolumePan(float& left, float& right) const {
2265 rtc::CritScope cs(&volume_settings_critsect_);
2266 left = _panLeft;
2267 right = _panRight;
2268 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002269}
2270
kwiberg55b97fe2016-01-28 05:22:45 -08002271int Channel::SetChannelOutputVolumeScaling(float scaling) {
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::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002275 _outputGain = scaling;
2276 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002277}
2278
kwiberg55b97fe2016-01-28 05:22:45 -08002279int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2280 rtc::CritScope cs(&volume_settings_critsect_);
2281 scaling = _outputGain;
2282 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002283}
2284
solenberg8842c3e2016-03-11 03:06:41 -08002285int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002286 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002287 "Channel::SendTelephoneEventOutband(...)");
2288 RTC_DCHECK_LE(0, event);
2289 RTC_DCHECK_GE(255, event);
2290 RTC_DCHECK_LE(0, duration_ms);
2291 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002292 if (!Sending()) {
2293 return -1;
2294 }
solenberg8842c3e2016-03-11 03:06:41 -08002295 if (_rtpRtcpModule->SendTelephoneEventOutband(
2296 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002297 _engineStatisticsPtr->SetLastError(
2298 VE_SEND_DTMF_FAILED, kTraceWarning,
2299 "SendTelephoneEventOutband() failed to send event");
2300 return -1;
2301 }
2302 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002303}
2304
solenbergffbbcac2016-11-17 05:25:37 -08002305int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2306 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002307 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002308 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002309 RTC_DCHECK_LE(0, payload_type);
2310 RTC_DCHECK_GE(127, payload_type);
2311 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002312 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002313 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002314 memcpy(codec.plname, "telephone-event", 16);
2315 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2316 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2317 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2318 _engineStatisticsPtr->SetLastError(
2319 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2320 "SetSendTelephoneEventPayloadType() failed to register send"
2321 "payload type");
2322 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002323 }
kwiberg55b97fe2016-01-28 05:22:45 -08002324 }
kwiberg55b97fe2016-01-28 05:22:45 -08002325 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002326}
2327
kwiberg55b97fe2016-01-28 05:22:45 -08002328int Channel::VoiceActivityIndicator(int& activity) {
2329 activity = _sendFrameType;
2330 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002331}
2332
kwiberg55b97fe2016-01-28 05:22:45 -08002333int Channel::SetLocalSSRC(unsigned int ssrc) {
2334 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2335 "Channel::SetLocalSSRC()");
2336 if (channel_state_.Get().sending) {
2337 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2338 "SetLocalSSRC() already sending");
2339 return -1;
2340 }
2341 _rtpRtcpModule->SetSSRC(ssrc);
2342 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002343}
2344
kwiberg55b97fe2016-01-28 05:22:45 -08002345int Channel::GetLocalSSRC(unsigned int& ssrc) {
2346 ssrc = _rtpRtcpModule->SSRC();
2347 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002348}
2349
kwiberg55b97fe2016-01-28 05:22:45 -08002350int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2351 ssrc = rtp_receiver_->SSRC();
2352 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002353}
2354
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002355int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002356 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002357 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002358}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002359
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002360int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2361 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002362 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2363 if (enable &&
2364 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2365 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002366 return -1;
2367 }
2368 return 0;
2369}
2370
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002371void Channel::EnableSendTransportSequenceNumber(int id) {
2372 int ret =
2373 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2374 RTC_DCHECK_EQ(0, ret);
2375}
2376
stefan3313ec92016-01-21 06:32:43 -08002377void Channel::EnableReceiveTransportSequenceNumber(int id) {
2378 rtp_header_parser_->DeregisterRtpHeaderExtension(
2379 kRtpExtensionTransportSequenceNumber);
2380 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2381 kRtpExtensionTransportSequenceNumber, id);
2382 RTC_DCHECK(ret);
2383}
2384
stefanbba9dec2016-02-01 04:39:55 -08002385void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002386 RtpPacketSender* rtp_packet_sender,
2387 TransportFeedbackObserver* transport_feedback_observer,
2388 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002389 RTC_DCHECK(rtp_packet_sender);
2390 RTC_DCHECK(transport_feedback_observer);
2391 RTC_DCHECK(packet_router && !packet_router_);
2392 feedback_observer_proxy_->SetTransportFeedbackObserver(
2393 transport_feedback_observer);
2394 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2395 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2396 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002397 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002398 packet_router_ = packet_router;
2399}
2400
stefanbba9dec2016-02-01 04:39:55 -08002401void Channel::RegisterReceiverCongestionControlObjects(
2402 PacketRouter* packet_router) {
2403 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002404 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002405 packet_router_ = packet_router;
2406}
2407
2408void Channel::ResetCongestionControlObjects() {
2409 RTC_DCHECK(packet_router_);
2410 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2411 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2412 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002413 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002414 packet_router_ = nullptr;
2415 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2416}
2417
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002418void Channel::SetRTCPStatus(bool enable) {
2419 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2420 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002421 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002422}
2423
kwiberg55b97fe2016-01-28 05:22:45 -08002424int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002425 RtcpMode method = _rtpRtcpModule->RTCP();
2426 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002427 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002428}
2429
kwiberg55b97fe2016-01-28 05:22:45 -08002430int Channel::SetRTCP_CNAME(const char cName[256]) {
2431 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2432 "Channel::SetRTCP_CNAME()");
2433 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2434 _engineStatisticsPtr->SetLastError(
2435 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2436 "SetRTCP_CNAME() failed to set RTCP CNAME");
2437 return -1;
2438 }
2439 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002440}
2441
kwiberg55b97fe2016-01-28 05:22:45 -08002442int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2443 if (cName == NULL) {
2444 _engineStatisticsPtr->SetLastError(
2445 VE_INVALID_ARGUMENT, kTraceError,
2446 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2447 return -1;
2448 }
2449 char cname[RTCP_CNAME_SIZE];
2450 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2451 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2452 _engineStatisticsPtr->SetLastError(
2453 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2454 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2455 return -1;
2456 }
2457 strcpy(cName, cname);
2458 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002459}
2460
kwiberg55b97fe2016-01-28 05:22:45 -08002461int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2462 unsigned int& NTPLow,
2463 unsigned int& timestamp,
2464 unsigned int& playoutTimestamp,
2465 unsigned int* jitter,
2466 unsigned short* fractionLost) {
2467 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002468
kwiberg55b97fe2016-01-28 05:22:45 -08002469 RTCPSenderInfo senderInfo;
2470 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2471 _engineStatisticsPtr->SetLastError(
2472 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2473 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2474 "side");
2475 return -1;
2476 }
2477
2478 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2479 // and octet count)
2480 NTPHigh = senderInfo.NTPseconds;
2481 NTPLow = senderInfo.NTPfraction;
2482 timestamp = senderInfo.RTPtimeStamp;
2483
2484 // --- Locally derived information
2485
2486 // This value is updated on each incoming RTCP packet (0 when no packet
2487 // has been received)
2488 playoutTimestamp = playout_timestamp_rtcp_;
2489
2490 if (NULL != jitter || NULL != fractionLost) {
2491 // Get all RTCP receiver report blocks that have been received on this
2492 // channel. If we receive RTP packets from a remote source we know the
2493 // remote SSRC and use the report block from him.
2494 // Otherwise use the first report block.
2495 std::vector<RTCPReportBlock> remote_stats;
2496 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2497 remote_stats.empty()) {
2498 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2499 "GetRemoteRTCPData() failed to measure statistics due"
2500 " to lack of received RTP and/or RTCP packets");
2501 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002502 }
2503
kwiberg55b97fe2016-01-28 05:22:45 -08002504 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2505 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2506 for (; it != remote_stats.end(); ++it) {
2507 if (it->remoteSSRC == remoteSSRC)
2508 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002509 }
kwiberg55b97fe2016-01-28 05:22:45 -08002510
2511 if (it == remote_stats.end()) {
2512 // If we have not received any RTCP packets from this SSRC it probably
2513 // means that we have not received any RTP packets.
2514 // Use the first received report block instead.
2515 it = remote_stats.begin();
2516 remoteSSRC = it->remoteSSRC;
2517 }
2518
2519 if (jitter) {
2520 *jitter = it->jitter;
2521 }
2522
2523 if (fractionLost) {
2524 *fractionLost = it->fractionLost;
2525 }
2526 }
2527 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002528}
2529
kwiberg55b97fe2016-01-28 05:22:45 -08002530int Channel::SendApplicationDefinedRTCPPacket(
2531 unsigned char subType,
2532 unsigned int name,
2533 const char* data,
2534 unsigned short dataLengthInBytes) {
2535 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2536 "Channel::SendApplicationDefinedRTCPPacket()");
2537 if (!channel_state_.Get().sending) {
2538 _engineStatisticsPtr->SetLastError(
2539 VE_NOT_SENDING, kTraceError,
2540 "SendApplicationDefinedRTCPPacket() not sending");
2541 return -1;
2542 }
2543 if (NULL == data) {
2544 _engineStatisticsPtr->SetLastError(
2545 VE_INVALID_ARGUMENT, kTraceError,
2546 "SendApplicationDefinedRTCPPacket() invalid data value");
2547 return -1;
2548 }
2549 if (dataLengthInBytes % 4 != 0) {
2550 _engineStatisticsPtr->SetLastError(
2551 VE_INVALID_ARGUMENT, kTraceError,
2552 "SendApplicationDefinedRTCPPacket() invalid length value");
2553 return -1;
2554 }
2555 RtcpMode status = _rtpRtcpModule->RTCP();
2556 if (status == RtcpMode::kOff) {
2557 _engineStatisticsPtr->SetLastError(
2558 VE_RTCP_ERROR, kTraceError,
2559 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2560 return -1;
2561 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002562
kwiberg55b97fe2016-01-28 05:22:45 -08002563 // Create and schedule the RTCP APP packet for transmission
2564 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2565 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2566 _engineStatisticsPtr->SetLastError(
2567 VE_SEND_ERROR, kTraceError,
2568 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2569 return -1;
2570 }
2571 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002572}
2573
kwiberg55b97fe2016-01-28 05:22:45 -08002574int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2575 unsigned int& maxJitterMs,
2576 unsigned int& discardedPackets) {
2577 // The jitter statistics is updated for each received RTP packet and is
2578 // based on received packets.
2579 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2580 // If RTCP is off, there is no timed thread in the RTCP module regularly
2581 // generating new stats, trigger the update manually here instead.
2582 StreamStatistician* statistician =
2583 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2584 if (statistician) {
2585 // Don't use returned statistics, use data from proxy instead so that
2586 // max jitter can be fetched atomically.
2587 RtcpStatistics s;
2588 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002589 }
kwiberg55b97fe2016-01-28 05:22:45 -08002590 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002591
kwiberg55b97fe2016-01-28 05:22:45 -08002592 ChannelStatistics stats = statistics_proxy_->GetStats();
2593 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2594 if (playoutFrequency > 0) {
2595 // Scale RTP statistics given the current playout frequency
2596 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2597 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2598 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002599
kwiberg55b97fe2016-01-28 05:22:45 -08002600 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002601
kwiberg55b97fe2016-01-28 05:22:45 -08002602 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002603}
2604
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002605int Channel::GetRemoteRTCPReportBlocks(
2606 std::vector<ReportBlock>* report_blocks) {
2607 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002608 _engineStatisticsPtr->SetLastError(
2609 VE_INVALID_ARGUMENT, kTraceError,
2610 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002611 return -1;
2612 }
2613
2614 // Get the report blocks from the latest received RTCP Sender or Receiver
2615 // Report. Each element in the vector contains the sender's SSRC and a
2616 // report block according to RFC 3550.
2617 std::vector<RTCPReportBlock> rtcp_report_blocks;
2618 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002619 return -1;
2620 }
2621
2622 if (rtcp_report_blocks.empty())
2623 return 0;
2624
2625 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2626 for (; it != rtcp_report_blocks.end(); ++it) {
2627 ReportBlock report_block;
2628 report_block.sender_SSRC = it->remoteSSRC;
2629 report_block.source_SSRC = it->sourceSSRC;
2630 report_block.fraction_lost = it->fractionLost;
2631 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2632 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2633 report_block.interarrival_jitter = it->jitter;
2634 report_block.last_SR_timestamp = it->lastSR;
2635 report_block.delay_since_last_SR = it->delaySinceLastSR;
2636 report_blocks->push_back(report_block);
2637 }
2638 return 0;
2639}
2640
kwiberg55b97fe2016-01-28 05:22:45 -08002641int Channel::GetRTPStatistics(CallStatistics& stats) {
2642 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002643
kwiberg55b97fe2016-01-28 05:22:45 -08002644 // The jitter statistics is updated for each received RTP packet and is
2645 // based on received packets.
2646 RtcpStatistics statistics;
2647 StreamStatistician* statistician =
2648 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002649 if (statistician) {
2650 statistician->GetStatistics(&statistics,
2651 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002652 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002653
kwiberg55b97fe2016-01-28 05:22:45 -08002654 stats.fractionLost = statistics.fraction_lost;
2655 stats.cumulativeLost = statistics.cumulative_lost;
2656 stats.extendedMax = statistics.extended_max_sequence_number;
2657 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002658
kwiberg55b97fe2016-01-28 05:22:45 -08002659 // --- RTT
2660 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002661
kwiberg55b97fe2016-01-28 05:22:45 -08002662 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002663
kwiberg55b97fe2016-01-28 05:22:45 -08002664 size_t bytesSent(0);
2665 uint32_t packetsSent(0);
2666 size_t bytesReceived(0);
2667 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002668
kwiberg55b97fe2016-01-28 05:22:45 -08002669 if (statistician) {
2670 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2671 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002672
kwiberg55b97fe2016-01-28 05:22:45 -08002673 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2674 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2675 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2676 " output will not be complete");
2677 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002678
kwiberg55b97fe2016-01-28 05:22:45 -08002679 stats.bytesSent = bytesSent;
2680 stats.packetsSent = packetsSent;
2681 stats.bytesReceived = bytesReceived;
2682 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002683
kwiberg55b97fe2016-01-28 05:22:45 -08002684 // --- Timestamps
2685 {
2686 rtc::CritScope lock(&ts_stats_lock_);
2687 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2688 }
2689 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002690}
2691
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002692int Channel::SetCodecFECStatus(bool enable) {
2693 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2694 "Channel::SetCodecFECStatus()");
2695
kwibergc8d071e2016-04-06 12:22:38 -07002696 if (!codec_manager_.SetCodecFEC(enable) ||
2697 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002698 _engineStatisticsPtr->SetLastError(
2699 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2700 "SetCodecFECStatus() failed to set FEC state");
2701 return -1;
2702 }
2703 return 0;
2704}
2705
2706bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002707 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002708}
2709
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002710void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2711 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002712 // If pacing is enabled we always store packets.
2713 if (!pacing_enabled_)
2714 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002715 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002716 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002717 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002718 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002719 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002720}
2721
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002722// Called when we are missing one or more packets.
2723int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002724 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2725}
2726
kwiberg55b97fe2016-01-28 05:22:45 -08002727uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2728 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2729 "Channel::Demultiplex()");
2730 _audioFrame.CopyFrom(audioFrame);
2731 _audioFrame.id_ = _channelId;
2732 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002733}
2734
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002735void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002736 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002737 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002738 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002739 CodecInst codec;
2740 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002741
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002742 // Never upsample or upmix the capture signal here. This should be done at the
2743 // end of the send chain.
2744 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2745 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2746 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2747 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002748}
2749
kwiberg55b97fe2016-01-28 05:22:45 -08002750uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2751 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2752 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002753
kwiberg55b97fe2016-01-28 05:22:45 -08002754 if (_audioFrame.samples_per_channel_ == 0) {
2755 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2756 "Channel::PrepareEncodeAndSend() invalid audio frame");
2757 return 0xFFFFFFFF;
2758 }
2759
2760 if (channel_state_.Get().input_file_playing) {
2761 MixOrReplaceAudioWithFile(mixingFrequency);
2762 }
2763
solenberg1c2af8e2016-03-24 10:36:00 -07002764 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2765 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002766
2767 if (channel_state_.Get().input_external_media) {
2768 rtc::CritScope cs(&_callbackCritSect);
2769 const bool isStereo = (_audioFrame.num_channels_ == 2);
2770 if (_inputExternalMediaCallbackPtr) {
2771 _inputExternalMediaCallbackPtr->Process(
2772 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
2773 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
2774 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00002775 }
kwiberg55b97fe2016-01-28 05:22:45 -08002776 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002777
kwiberg55b97fe2016-01-28 05:22:45 -08002778 if (_includeAudioLevelIndication) {
2779 size_t length =
2780 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002781 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002782 if (is_muted && previous_frame_muted_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002783 rms_level_.ProcessMuted(length);
2784 } else {
2785 rms_level_.Process(_audioFrame.data_, length);
niklase@google.com470e71d2011-07-07 08:21:25 +00002786 }
kwiberg55b97fe2016-01-28 05:22:45 -08002787 }
solenberg1c2af8e2016-03-24 10:36:00 -07002788 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002789
kwiberg55b97fe2016-01-28 05:22:45 -08002790 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002791}
2792
kwiberg55b97fe2016-01-28 05:22:45 -08002793uint32_t Channel::EncodeAndSend() {
2794 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2795 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002796
kwiberg55b97fe2016-01-28 05:22:45 -08002797 assert(_audioFrame.num_channels_ <= 2);
2798 if (_audioFrame.samples_per_channel_ == 0) {
2799 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2800 "Channel::EncodeAndSend() invalid audio frame");
2801 return 0xFFFFFFFF;
2802 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002803
kwiberg55b97fe2016-01-28 05:22:45 -08002804 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002805
kwiberg55b97fe2016-01-28 05:22:45 -08002806 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002807
kwiberg55b97fe2016-01-28 05:22:45 -08002808 // The ACM resamples internally.
2809 _audioFrame.timestamp_ = _timeStamp;
2810 // This call will trigger AudioPacketizationCallback::SendData if encoding
2811 // is done and payload is ready for packetization and transmission.
2812 // Otherwise, it will return without invoking the callback.
2813 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2814 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2815 "Channel::EncodeAndSend() ACM encoding failed");
2816 return 0xFFFFFFFF;
2817 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002818
kwiberg55b97fe2016-01-28 05:22:45 -08002819 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2820 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002821}
2822
solenberg7602aab2016-11-14 11:30:07 -08002823void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2824 RTC_DCHECK(!channel.channel() ||
2825 channel.channel()->ChannelId() != _channelId);
2826 rtc::CritScope lock(&assoc_send_channel_lock_);
2827 associate_send_channel_ = channel;
2828}
2829
Minyue2013aec2015-05-13 14:14:42 +02002830void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002831 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002832 Channel* channel = associate_send_channel_.channel();
2833 if (channel && channel->ChannelId() == channel_id) {
2834 // If this channel is associated with a send channel of the specified
2835 // Channel ID, disassociate with it.
2836 ChannelOwner ref(NULL);
2837 associate_send_channel_ = ref;
2838 }
2839}
2840
ivoc14d5dbe2016-07-04 07:06:55 -07002841void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2842 event_log_proxy_->SetEventLog(event_log);
2843}
2844
michaelt79e05882016-11-08 02:50:09 -08002845void Channel::SetTransportOverhead(int transport_overhead_per_packet) {
2846 _rtpRtcpModule->SetTransportOverhead(transport_overhead_per_packet);
2847}
2848
kwiberg55b97fe2016-01-28 05:22:45 -08002849int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
2850 VoEMediaProcess& processObject) {
2851 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2852 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002853
kwiberg55b97fe2016-01-28 05:22:45 -08002854 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002855
kwiberg55b97fe2016-01-28 05:22:45 -08002856 if (kPlaybackPerChannel == type) {
2857 if (_outputExternalMediaCallbackPtr) {
2858 _engineStatisticsPtr->SetLastError(
2859 VE_INVALID_OPERATION, kTraceError,
2860 "Channel::RegisterExternalMediaProcessing() "
2861 "output external media already enabled");
2862 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002863 }
kwiberg55b97fe2016-01-28 05:22:45 -08002864 _outputExternalMediaCallbackPtr = &processObject;
2865 _outputExternalMedia = true;
2866 } else if (kRecordingPerChannel == type) {
2867 if (_inputExternalMediaCallbackPtr) {
2868 _engineStatisticsPtr->SetLastError(
2869 VE_INVALID_OPERATION, kTraceError,
2870 "Channel::RegisterExternalMediaProcessing() "
2871 "output external media already enabled");
2872 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002873 }
kwiberg55b97fe2016-01-28 05:22:45 -08002874 _inputExternalMediaCallbackPtr = &processObject;
2875 channel_state_.SetInputExternalMedia(true);
2876 }
2877 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002878}
2879
kwiberg55b97fe2016-01-28 05:22:45 -08002880int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
2881 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2882 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002883
kwiberg55b97fe2016-01-28 05:22:45 -08002884 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002885
kwiberg55b97fe2016-01-28 05:22:45 -08002886 if (kPlaybackPerChannel == type) {
2887 if (!_outputExternalMediaCallbackPtr) {
2888 _engineStatisticsPtr->SetLastError(
2889 VE_INVALID_OPERATION, kTraceWarning,
2890 "Channel::DeRegisterExternalMediaProcessing() "
2891 "output external media already disabled");
2892 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002893 }
kwiberg55b97fe2016-01-28 05:22:45 -08002894 _outputExternalMedia = false;
2895 _outputExternalMediaCallbackPtr = NULL;
2896 } else if (kRecordingPerChannel == type) {
2897 if (!_inputExternalMediaCallbackPtr) {
2898 _engineStatisticsPtr->SetLastError(
2899 VE_INVALID_OPERATION, kTraceWarning,
2900 "Channel::DeRegisterExternalMediaProcessing() "
2901 "input external media already disabled");
2902 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002903 }
kwiberg55b97fe2016-01-28 05:22:45 -08002904 channel_state_.SetInputExternalMedia(false);
2905 _inputExternalMediaCallbackPtr = NULL;
2906 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002907
kwiberg55b97fe2016-01-28 05:22:45 -08002908 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002909}
2910
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002911int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08002912 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2913 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002914
kwiberg55b97fe2016-01-28 05:22:45 -08002915 if (channel_state_.Get().playing) {
2916 _engineStatisticsPtr->SetLastError(
2917 VE_INVALID_OPERATION, kTraceError,
2918 "Channel::SetExternalMixing() "
2919 "external mixing cannot be changed while playing.");
2920 return -1;
2921 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002922
kwiberg55b97fe2016-01-28 05:22:45 -08002923 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002924
kwiberg55b97fe2016-01-28 05:22:45 -08002925 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002926}
2927
kwiberg55b97fe2016-01-28 05:22:45 -08002928int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2929 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002930}
2931
wu@webrtc.org24301a62013-12-13 19:17:43 +00002932void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2933 audio_coding_->GetDecodingCallStatistics(stats);
2934}
2935
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002936bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
2937 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08002938 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07002939 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002940 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002941 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002942}
2943
solenberg358057b2015-11-27 10:46:42 -08002944uint32_t Channel::GetDelayEstimate() const {
2945 int jitter_buffer_delay_ms = 0;
2946 int playout_buffer_delay_ms = 0;
2947 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
2948 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
2949}
2950
deadbeef74375882015-08-13 12:09:10 -07002951int Channel::LeastRequiredDelayMs() const {
2952 return audio_coding_->LeastRequiredDelayMs();
2953}
2954
kwiberg55b97fe2016-01-28 05:22:45 -08002955int Channel::SetMinimumPlayoutDelay(int delayMs) {
2956 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2957 "Channel::SetMinimumPlayoutDelay()");
2958 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2959 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2960 _engineStatisticsPtr->SetLastError(
2961 VE_INVALID_ARGUMENT, kTraceError,
2962 "SetMinimumPlayoutDelay() invalid min delay");
2963 return -1;
2964 }
2965 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2966 _engineStatisticsPtr->SetLastError(
2967 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2968 "SetMinimumPlayoutDelay() failed to set min playout delay");
2969 return -1;
2970 }
2971 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002972}
2973
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002974int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002975 uint32_t playout_timestamp_rtp = 0;
2976 {
tommi31fc21f2016-01-21 10:37:37 -08002977 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002978 playout_timestamp_rtp = playout_timestamp_rtp_;
2979 }
kwiberg55b97fe2016-01-28 05:22:45 -08002980 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002981 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002982 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002983 "GetPlayoutTimestamp() failed to retrieve timestamp");
2984 return -1;
2985 }
deadbeef74375882015-08-13 12:09:10 -07002986 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002987 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002988}
2989
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002990int Channel::SetInitTimestamp(unsigned int timestamp) {
2991 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002992 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002993 if (channel_state_.Get().sending) {
2994 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
2995 "SetInitTimestamp() already sending");
2996 return -1;
2997 }
2998 _rtpRtcpModule->SetStartTimestamp(timestamp);
2999 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003000}
3001
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003002int Channel::SetInitSequenceNumber(short sequenceNumber) {
3003 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3004 "Channel::SetInitSequenceNumber()");
3005 if (channel_state_.Get().sending) {
3006 _engineStatisticsPtr->SetLastError(
3007 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3008 return -1;
3009 }
3010 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3011 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003012}
3013
kwiberg55b97fe2016-01-28 05:22:45 -08003014int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
3015 RtpReceiver** rtp_receiver) const {
3016 *rtpRtcpModule = _rtpRtcpModule.get();
3017 *rtp_receiver = rtp_receiver_.get();
3018 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003019}
3020
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003021// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3022// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08003023int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08003024 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08003025 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003026
kwiberg55b97fe2016-01-28 05:22:45 -08003027 {
3028 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003029
kwiberg5a25d952016-08-17 07:31:12 -07003030 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003031 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3032 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3033 " doesnt exist");
3034 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003035 }
3036
kwiberg4ec01d92016-08-22 08:43:54 -07003037 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07003038 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003039 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3040 "Channel::MixOrReplaceAudioWithFile() file mixing "
3041 "failed");
3042 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003043 }
kwiberg55b97fe2016-01-28 05:22:45 -08003044 if (fileSamples == 0) {
3045 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3046 "Channel::MixOrReplaceAudioWithFile() file is ended");
3047 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003048 }
kwiberg55b97fe2016-01-28 05:22:45 -08003049 }
3050
3051 assert(_audioFrame.samples_per_channel_ == fileSamples);
3052
3053 if (_mixFileWithMicrophone) {
3054 // Currently file stream is always mono.
3055 // TODO(xians): Change the code when FilePlayer supports real stereo.
3056 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3057 1, fileSamples);
3058 } else {
3059 // Replace ACM audio with file.
3060 // Currently file stream is always mono.
3061 // TODO(xians): Change the code when FilePlayer supports real stereo.
3062 _audioFrame.UpdateFrame(
3063 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3064 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3065 }
3066 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003067}
3068
kwiberg55b97fe2016-01-28 05:22:45 -08003069int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3070 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003071
kwibergb7f89d62016-02-17 10:04:18 -08003072 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003073 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003074
kwiberg55b97fe2016-01-28 05:22:45 -08003075 {
3076 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003077
kwiberg5a25d952016-08-17 07:31:12 -07003078 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003079 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3080 "Channel::MixAudioWithFile() file mixing failed");
3081 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003082 }
3083
kwiberg55b97fe2016-01-28 05:22:45 -08003084 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003085 if (output_file_player_->Get10msAudioFromFile(
3086 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003087 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3088 "Channel::MixAudioWithFile() file mixing failed");
3089 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003090 }
kwiberg55b97fe2016-01-28 05:22:45 -08003091 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003092
kwiberg55b97fe2016-01-28 05:22:45 -08003093 if (audioFrame.samples_per_channel_ == fileSamples) {
3094 // Currently file stream is always mono.
3095 // TODO(xians): Change the code when FilePlayer supports real stereo.
3096 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3097 fileSamples);
3098 } else {
3099 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3100 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3101 ") != "
3102 "fileSamples(%" PRIuS ")",
3103 audioFrame.samples_per_channel_, fileSamples);
3104 return -1;
3105 }
3106
3107 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003108}
3109
deadbeef74375882015-08-13 12:09:10 -07003110void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003111 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003112
henrik.lundin96bd5022016-04-06 04:13:56 -07003113 if (!jitter_buffer_playout_timestamp_) {
3114 // This can happen if this channel has not received any RTP packets. In
3115 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003116 return;
3117 }
3118
3119 uint16_t delay_ms = 0;
3120 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003121 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003122 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3123 " delay from the ADM");
3124 _engineStatisticsPtr->SetLastError(
3125 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3126 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3127 return;
3128 }
3129
henrik.lundin96bd5022016-04-06 04:13:56 -07003130 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3131 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003132
3133 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003134 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003135
kwiberg55b97fe2016-01-28 05:22:45 -08003136 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003137 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003138 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003139
3140 {
tommi31fc21f2016-01-21 10:37:37 -08003141 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003142 if (rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003143 playout_timestamp_rtcp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003144 } else {
henrik.lundin96bd5022016-04-06 04:13:56 -07003145 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003146 }
3147 playout_delay_ms_ = delay_ms;
3148 }
3149}
3150
kwiberg55b97fe2016-01-28 05:22:45 -08003151void Channel::RegisterReceiveCodecsToRTPModule() {
3152 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3153 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003154
kwiberg55b97fe2016-01-28 05:22:45 -08003155 CodecInst codec;
3156 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003157
kwiberg55b97fe2016-01-28 05:22:45 -08003158 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3159 // Open up the RTP/RTCP receiver for all supported codecs
3160 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08003161 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08003162 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3163 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3164 " to register %s (%d/%d/%" PRIuS
3165 "/%d) to RTP/RTCP "
3166 "receiver",
3167 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3168 codec.rate);
3169 } else {
3170 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3171 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3172 "(%d/%d/%" PRIuS
3173 "/%d) has been added to the RTP/RTCP "
3174 "receiver",
3175 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3176 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003177 }
kwiberg55b97fe2016-01-28 05:22:45 -08003178 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003179}
3180
kwiberg55b97fe2016-01-28 05:22:45 -08003181int Channel::SetSendRtpHeaderExtension(bool enable,
3182 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003183 unsigned char id) {
3184 int error = 0;
3185 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3186 if (enable) {
3187 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3188 }
3189 return error;
3190}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003191
ossue280cde2016-10-12 11:04:10 -07003192int Channel::GetRtpTimestampRateHz() const {
3193 const auto format = audio_coding_->ReceiveFormat();
3194 // Default to the playout frequency if we've not gotten any packets yet.
3195 // TODO(ossu): Zero clockrate can only happen if we've added an external
3196 // decoder for a format we don't support internally. Remove once that way of
3197 // adding decoders is gone!
3198 return (format && format->clockrate_hz != 0)
3199 ? format->clockrate_hz
3200 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003201}
3202
Minyue2013aec2015-05-13 14:14:42 +02003203int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003204 RtcpMode method = _rtpRtcpModule->RTCP();
3205 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003206 return 0;
3207 }
3208 std::vector<RTCPReportBlock> report_blocks;
3209 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003210
3211 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003212 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003213 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003214 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003215 Channel* channel = associate_send_channel_.channel();
3216 // Tries to get RTT from an associated channel. This is important for
3217 // receive-only channels.
3218 if (channel) {
3219 // To prevent infinite recursion and deadlock, calling GetRTT of
3220 // associate channel should always use "false" for argument:
3221 // |allow_associate_channel|.
3222 rtt = channel->GetRTT(false);
3223 }
3224 }
3225 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003226 }
3227
3228 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3229 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3230 for (; it != report_blocks.end(); ++it) {
3231 if (it->remoteSSRC == remoteSSRC)
3232 break;
3233 }
3234 if (it == report_blocks.end()) {
3235 // We have not received packets with SSRC matching the report blocks.
3236 // To calculate RTT we try with the SSRC of the first report block.
3237 // This is very important for send-only channels where we don't know
3238 // the SSRC of the other end.
3239 remoteSSRC = report_blocks[0].remoteSSRC;
3240 }
Minyue2013aec2015-05-13 14:14:42 +02003241
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003242 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003243 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003244 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003245 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3246 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003247 return 0;
3248 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003249 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003250}
3251
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003252} // namespace voe
3253} // namespace webrtc