blob: c12d87c5bf77cfc8411e6c45e4e03efd8643ba8b [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
aleloi6321b492016-12-05 01:46:09 -080016#include "webrtc/audio/utility/audio_frame_operations.h"
henrik.lundin50499422016-11-29 04:26:24 -080017#include "webrtc/base/array_view.h"
Ivo Creusenae856f22015-09-17 16:30:16 +020018#include "webrtc/base/checks.h"
tommi31fc21f2016-01-21 10:37:37 -080019#include "webrtc/base/criticalsection.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000020#include "webrtc/base/format_macros.h"
pbosad856222015-11-27 09:48:36 -080021#include "webrtc/base/logging.h"
Erik Språng737336d2016-07-29 12:59:36 +020022#include "webrtc/base/rate_limiter.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010023#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000024#include "webrtc/base/timeutils.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020025#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070026#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070027#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000028#include "webrtc/modules/audio_device/include/audio_device.h"
29#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010031#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010032#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
33#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
34#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000035#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010036#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010037#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000038#include "webrtc/voice_engine/include/voe_external_media.h"
39#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
40#include "webrtc/voice_engine/output_mixer.h"
41#include "webrtc/voice_engine/statistics.h"
42#include "webrtc/voice_engine/transmit_mixer.h"
43#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000044
andrew@webrtc.org50419b02012-11-14 19:07:54 +000045namespace webrtc {
46namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000047
kwibergc8d071e2016-04-06 12:22:38 -070048namespace {
49
Erik Språng737336d2016-07-29 12:59:36 +020050constexpr int64_t kMaxRetransmissionWindowMs = 1000;
51constexpr int64_t kMinRetransmissionWindowMs = 30;
52
kwibergc8d071e2016-04-06 12:22:38 -070053} // namespace
54
solenberg8842c3e2016-03-11 03:06:41 -080055const int kTelephoneEventAttenuationdB = 10;
56
ivoc14d5dbe2016-07-04 07:06:55 -070057class RtcEventLogProxy final : public webrtc::RtcEventLog {
58 public:
59 RtcEventLogProxy() : event_log_(nullptr) {}
60
61 bool StartLogging(const std::string& file_name,
62 int64_t max_size_bytes) override {
63 RTC_NOTREACHED();
64 return false;
65 }
66
67 bool StartLogging(rtc::PlatformFile log_file,
68 int64_t max_size_bytes) override {
69 RTC_NOTREACHED();
70 return false;
71 }
72
73 void StopLogging() override { RTC_NOTREACHED(); }
74
75 void LogVideoReceiveStreamConfig(
76 const webrtc::VideoReceiveStream::Config& config) override {
77 rtc::CritScope lock(&crit_);
78 if (event_log_) {
79 event_log_->LogVideoReceiveStreamConfig(config);
80 }
81 }
82
83 void LogVideoSendStreamConfig(
84 const webrtc::VideoSendStream::Config& config) override {
85 rtc::CritScope lock(&crit_);
86 if (event_log_) {
87 event_log_->LogVideoSendStreamConfig(config);
88 }
89 }
90
ivoce0928d82016-10-10 05:12:51 -070091 void LogAudioReceiveStreamConfig(
92 const webrtc::AudioReceiveStream::Config& config) override {
93 rtc::CritScope lock(&crit_);
94 if (event_log_) {
95 event_log_->LogAudioReceiveStreamConfig(config);
96 }
97 }
98
99 void LogAudioSendStreamConfig(
100 const webrtc::AudioSendStream::Config& config) override {
101 rtc::CritScope lock(&crit_);
102 if (event_log_) {
103 event_log_->LogAudioSendStreamConfig(config);
104 }
105 }
106
ivoc14d5dbe2016-07-04 07:06:55 -0700107 void LogRtpHeader(webrtc::PacketDirection direction,
108 webrtc::MediaType media_type,
109 const uint8_t* header,
110 size_t packet_length) override {
111 rtc::CritScope lock(&crit_);
112 if (event_log_) {
113 event_log_->LogRtpHeader(direction, media_type, header, packet_length);
114 }
115 }
116
117 void LogRtcpPacket(webrtc::PacketDirection direction,
118 webrtc::MediaType media_type,
119 const uint8_t* packet,
120 size_t length) override {
121 rtc::CritScope lock(&crit_);
122 if (event_log_) {
123 event_log_->LogRtcpPacket(direction, media_type, packet, length);
124 }
125 }
126
127 void LogAudioPlayout(uint32_t ssrc) override {
128 rtc::CritScope lock(&crit_);
129 if (event_log_) {
130 event_log_->LogAudioPlayout(ssrc);
131 }
132 }
133
134 void LogBwePacketLossEvent(int32_t bitrate,
135 uint8_t fraction_loss,
136 int32_t total_packets) override {
137 rtc::CritScope lock(&crit_);
138 if (event_log_) {
139 event_log_->LogBwePacketLossEvent(bitrate, fraction_loss, total_packets);
140 }
141 }
142
143 void SetEventLog(RtcEventLog* event_log) {
144 rtc::CritScope lock(&crit_);
145 event_log_ = event_log;
146 }
147
148 private:
149 rtc::CriticalSection crit_;
150 RtcEventLog* event_log_ GUARDED_BY(crit_);
151 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
152};
153
michaelt9332b7d2016-11-30 07:51:13 -0800154class RtcpRttStatsProxy final : public RtcpRttStats {
155 public:
156 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
157
158 void OnRttUpdate(int64_t rtt) override {
159 rtc::CritScope lock(&crit_);
160 if (rtcp_rtt_stats_)
161 rtcp_rtt_stats_->OnRttUpdate(rtt);
162 }
163
164 int64_t LastProcessedRtt() const override {
165 rtc::CritScope lock(&crit_);
166 if (!rtcp_rtt_stats_)
167 return 0;
168 return rtcp_rtt_stats_->LastProcessedRtt();
169 }
170
171 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
172 rtc::CritScope lock(&crit_);
173 rtcp_rtt_stats_ = rtcp_rtt_stats;
174 }
175
176 private:
177 rtc::CriticalSection crit_;
178 RtcpRttStats* rtcp_rtt_stats_ GUARDED_BY(crit_);
179 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
180};
181
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100182class TransportFeedbackProxy : public TransportFeedbackObserver {
183 public:
184 TransportFeedbackProxy() : feedback_observer_(nullptr) {
185 pacer_thread_.DetachFromThread();
186 network_thread_.DetachFromThread();
187 }
188
189 void SetTransportFeedbackObserver(
190 TransportFeedbackObserver* feedback_observer) {
191 RTC_DCHECK(thread_checker_.CalledOnValidThread());
192 rtc::CritScope lock(&crit_);
193 feedback_observer_ = feedback_observer;
194 }
195
196 // Implements TransportFeedbackObserver.
197 void AddPacket(uint16_t sequence_number,
198 size_t length,
philipela1ed0b32016-06-01 06:31:17 -0700199 int probe_cluster_id) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100200 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
201 rtc::CritScope lock(&crit_);
202 if (feedback_observer_)
pbos2169d8b2016-06-20 11:53:02 -0700203 feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100204 }
205 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
206 RTC_DCHECK(network_thread_.CalledOnValidThread());
207 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700208 if (feedback_observer_)
209 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200210 }
211 std::vector<PacketInfo> GetTransportFeedbackVector() const override {
212 RTC_NOTREACHED();
213 return std::vector<PacketInfo>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100214 }
215
216 private:
217 rtc::CriticalSection crit_;
218 rtc::ThreadChecker thread_checker_;
219 rtc::ThreadChecker pacer_thread_;
220 rtc::ThreadChecker network_thread_;
221 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
222};
223
224class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
225 public:
226 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
227 pacer_thread_.DetachFromThread();
228 }
229
230 void SetSequenceNumberAllocator(
231 TransportSequenceNumberAllocator* seq_num_allocator) {
232 RTC_DCHECK(thread_checker_.CalledOnValidThread());
233 rtc::CritScope lock(&crit_);
234 seq_num_allocator_ = seq_num_allocator;
235 }
236
237 // Implements TransportSequenceNumberAllocator.
238 uint16_t AllocateSequenceNumber() override {
239 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
240 rtc::CritScope lock(&crit_);
241 if (!seq_num_allocator_)
242 return 0;
243 return seq_num_allocator_->AllocateSequenceNumber();
244 }
245
246 private:
247 rtc::CriticalSection crit_;
248 rtc::ThreadChecker thread_checker_;
249 rtc::ThreadChecker pacer_thread_;
250 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
251};
252
253class RtpPacketSenderProxy : public RtpPacketSender {
254 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800255 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100256
257 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
258 RTC_DCHECK(thread_checker_.CalledOnValidThread());
259 rtc::CritScope lock(&crit_);
260 rtp_packet_sender_ = rtp_packet_sender;
261 }
262
263 // Implements RtpPacketSender.
264 void InsertPacket(Priority priority,
265 uint32_t ssrc,
266 uint16_t sequence_number,
267 int64_t capture_time_ms,
268 size_t bytes,
269 bool retransmission) override {
270 rtc::CritScope lock(&crit_);
271 if (rtp_packet_sender_) {
272 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
273 capture_time_ms, bytes, retransmission);
274 }
275 }
276
277 private:
278 rtc::ThreadChecker thread_checker_;
279 rtc::CriticalSection crit_;
280 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
281};
282
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000283// Extend the default RTCP statistics struct with max_jitter, defined as the
284// maximum jitter value seen in an RTCP report block.
285struct ChannelStatistics : public RtcpStatistics {
286 ChannelStatistics() : rtcp(), max_jitter(0) {}
287
288 RtcpStatistics rtcp;
289 uint32_t max_jitter;
290};
291
292// Statistics callback, called at each generation of a new RTCP report block.
293class StatisticsProxy : public RtcpStatisticsCallback {
294 public:
tommi31fc21f2016-01-21 10:37:37 -0800295 StatisticsProxy(uint32_t ssrc) : ssrc_(ssrc) {}
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000296 virtual ~StatisticsProxy() {}
297
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000298 void StatisticsUpdated(const RtcpStatistics& statistics,
299 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000300 if (ssrc != ssrc_)
301 return;
302
tommi31fc21f2016-01-21 10:37:37 -0800303 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000304 stats_.rtcp = statistics;
305 if (statistics.jitter > stats_.max_jitter) {
306 stats_.max_jitter = statistics.jitter;
307 }
308 }
309
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000310 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000311
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000312 ChannelStatistics GetStats() {
tommi31fc21f2016-01-21 10:37:37 -0800313 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000314 return stats_;
315 }
316
317 private:
318 // StatisticsUpdated calls are triggered from threads in the RTP module,
319 // while GetStats calls can be triggered from the public voice engine API,
320 // hence synchronization is needed.
tommi31fc21f2016-01-21 10:37:37 -0800321 rtc::CriticalSection stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000322 const uint32_t ssrc_;
323 ChannelStatistics stats_;
324};
325
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000326class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000327 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000328 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
329 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000330
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000331 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
332 // Not used for Voice Engine.
333 }
334
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000335 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
336 int64_t rtt,
337 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000338 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
339 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
340 // report for VoiceEngine?
341 if (report_blocks.empty())
342 return;
343
344 int fraction_lost_aggregate = 0;
345 int total_number_of_packets = 0;
346
347 // If receiving multiple report blocks, calculate the weighted average based
348 // on the number of packets a report refers to.
349 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
350 block_it != report_blocks.end(); ++block_it) {
351 // Find the previous extended high sequence number for this remote SSRC,
352 // to calculate the number of RTP packets this report refers to. Ignore if
353 // we haven't seen this SSRC before.
354 std::map<uint32_t, uint32_t>::iterator seq_num_it =
355 extended_max_sequence_number_.find(block_it->sourceSSRC);
356 int number_of_packets = 0;
357 if (seq_num_it != extended_max_sequence_number_.end()) {
358 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
359 }
360 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
361 total_number_of_packets += number_of_packets;
362
363 extended_max_sequence_number_[block_it->sourceSSRC] =
364 block_it->extendedHighSeqNum;
365 }
366 int weighted_fraction_lost = 0;
367 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800368 weighted_fraction_lost =
369 (fraction_lost_aggregate + total_number_of_packets / 2) /
370 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000371 }
372 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000373 }
374
375 private:
376 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000377 // Maps remote side ssrc to extended highest sequence number received.
378 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000379};
380
kwiberg55b97fe2016-01-28 05:22:45 -0800381int32_t Channel::SendData(FrameType frameType,
382 uint8_t payloadType,
383 uint32_t timeStamp,
384 const uint8_t* payloadData,
385 size_t payloadSize,
386 const RTPFragmentationHeader* fragmentation) {
387 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
388 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
389 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
390 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000391
kwiberg55b97fe2016-01-28 05:22:45 -0800392 if (_includeAudioLevelIndication) {
393 // Store current audio level in the RTP/RTCP module.
394 // The level will be used in combination with voice-activity state
395 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800396 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800397 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000398
kwiberg55b97fe2016-01-28 05:22:45 -0800399 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
400 // packetization.
401 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700402 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800403 (FrameType&)frameType, payloadType, timeStamp,
404 // Leaving the time when this frame was
405 // received from the capture device as
406 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700407 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800408 _engineStatisticsPtr->SetLastError(
409 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
410 "Channel::SendData() failed to send data to RTP/RTCP module");
411 return -1;
412 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000413
kwiberg55b97fe2016-01-28 05:22:45 -0800414 _lastLocalTimeStamp = timeStamp;
415 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000416
kwiberg55b97fe2016-01-28 05:22:45 -0800417 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000418}
419
kwiberg55b97fe2016-01-28 05:22:45 -0800420int32_t Channel::InFrameType(FrameType frame_type) {
421 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
422 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000423
kwiberg55b97fe2016-01-28 05:22:45 -0800424 rtc::CritScope cs(&_callbackCritSect);
425 _sendFrameType = (frame_type == kAudioFrameSpeech);
426 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000427}
428
stefan1d8a5062015-10-02 03:39:33 -0700429bool Channel::SendRtp(const uint8_t* data,
430 size_t len,
431 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800432 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
433 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000434
kwiberg55b97fe2016-01-28 05:22:45 -0800435 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000436
kwiberg55b97fe2016-01-28 05:22:45 -0800437 if (_transportPtr == NULL) {
438 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
439 "Channel::SendPacket() failed to send RTP packet due to"
440 " invalid transport object");
441 return false;
442 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000443
kwiberg55b97fe2016-01-28 05:22:45 -0800444 uint8_t* bufferToSendPtr = (uint8_t*)data;
445 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000446
kwiberg55b97fe2016-01-28 05:22:45 -0800447 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
448 std::string transport_name =
449 _externalTransport ? "external transport" : "WebRtc sockets";
450 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
451 "Channel::SendPacket() RTP transmission using %s failed",
452 transport_name.c_str());
453 return false;
454 }
455 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000456}
457
kwiberg55b97fe2016-01-28 05:22:45 -0800458bool Channel::SendRtcp(const uint8_t* data, size_t len) {
459 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
460 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000461
kwiberg55b97fe2016-01-28 05:22:45 -0800462 rtc::CritScope cs(&_callbackCritSect);
463 if (_transportPtr == NULL) {
464 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
465 "Channel::SendRtcp() failed to send RTCP packet"
466 " due to invalid transport object");
467 return false;
468 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000469
kwiberg55b97fe2016-01-28 05:22:45 -0800470 uint8_t* bufferToSendPtr = (uint8_t*)data;
471 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000472
kwiberg55b97fe2016-01-28 05:22:45 -0800473 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
474 if (n < 0) {
475 std::string transport_name =
476 _externalTransport ? "external transport" : "WebRtc sockets";
477 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
478 "Channel::SendRtcp() transmission using %s failed",
479 transport_name.c_str());
480 return false;
481 }
482 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000483}
484
kwiberg55b97fe2016-01-28 05:22:45 -0800485void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
486 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
487 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000488
kwiberg55b97fe2016-01-28 05:22:45 -0800489 // Update ssrc so that NTP for AV sync can be updated.
490 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000491}
492
Peter Boströmac547a62015-09-17 23:03:57 +0200493void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
494 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
495 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
496 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000497}
498
Peter Boströmac547a62015-09-17 23:03:57 +0200499int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000500 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000501 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000502 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800503 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200504 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800505 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
506 "Channel::OnInitializeDecoder(payloadType=%d, "
507 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
508 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000509
kwiberg55b97fe2016-01-28 05:22:45 -0800510 CodecInst receiveCodec = {0};
511 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000512
kwiberg55b97fe2016-01-28 05:22:45 -0800513 receiveCodec.pltype = payloadType;
514 receiveCodec.plfreq = frequency;
515 receiveCodec.channels = channels;
516 receiveCodec.rate = rate;
517 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000518
kwiberg55b97fe2016-01-28 05:22:45 -0800519 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
520 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000521
kwiberg55b97fe2016-01-28 05:22:45 -0800522 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700523 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
524 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800525 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
526 "Channel::OnInitializeDecoder() invalid codec ("
527 "pt=%d, name=%s) received - 1",
528 payloadType, payloadName);
529 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
530 return -1;
531 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000532
kwiberg55b97fe2016-01-28 05:22:45 -0800533 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000534}
535
kwiberg55b97fe2016-01-28 05:22:45 -0800536int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
537 size_t payloadSize,
538 const WebRtcRTPHeader* rtpHeader) {
539 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
540 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
541 ","
542 " payloadType=%u, audioChannel=%" PRIuS ")",
543 payloadSize, rtpHeader->header.payloadType,
544 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000545
kwiberg55b97fe2016-01-28 05:22:45 -0800546 if (!channel_state_.Get().playing) {
547 // Avoid inserting into NetEQ when we are not playing. Count the
548 // packet as discarded.
549 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
550 "received packet is discarded since playing is not"
551 " activated");
552 _numberOfDiscardedPackets++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000553 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800554 }
555
556 // Push the incoming payload (parsed and ready for decoding) into the ACM
557 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
558 0) {
559 _engineStatisticsPtr->SetLastError(
560 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
561 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
562 return -1;
563 }
564
kwiberg55b97fe2016-01-28 05:22:45 -0800565 int64_t round_trip_time = 0;
566 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
567 NULL);
568
569 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
570 if (!nack_list.empty()) {
571 // Can't use nack_list.data() since it's not supported by all
572 // compilers.
573 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
574 }
575 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000576}
577
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000578bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000579 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000580 RTPHeader header;
581 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
582 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
583 "IncomingPacket invalid RTP header");
584 return false;
585 }
586 header.payload_type_frequency =
587 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
588 if (header.payload_type_frequency < 0)
589 return false;
590 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
591}
592
henrik.lundin42dda502016-05-18 05:36:01 -0700593MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
594 int32_t id,
595 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700596 unsigned int ssrc;
597 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
598 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800599 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700600 bool muted;
601 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
602 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800603 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
604 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
605 // In all likelihood, the audio in this frame is garbage. We return an
606 // error so that the audio mixer module doesn't add it to the mix. As
607 // a result, it won't be played out and the actions skipped here are
608 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700609 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800610 }
henrik.lundina89ab962016-05-18 08:52:45 -0700611
612 if (muted) {
613 // TODO(henrik.lundin): We should be able to do better than this. But we
614 // will have to go through all the cases below where the audio samples may
615 // be used, and handle the muted case in some way.
aleloi6321b492016-12-05 01:46:09 -0800616 AudioFrameOperations::Mute(audioFrame);
henrik.lundina89ab962016-05-18 08:52:45 -0700617 }
kwiberg55b97fe2016-01-28 05:22:45 -0800618
kwiberg55b97fe2016-01-28 05:22:45 -0800619 // Convert module ID to internal VoE channel ID
620 audioFrame->id_ = VoEChannelId(audioFrame->id_);
621 // Store speech type for dead-or-alive detection
622 _outputSpeechType = audioFrame->speech_type_;
623
624 ChannelState::State state = channel_state_.Get();
625
kwiberg55b97fe2016-01-28 05:22:45 -0800626 {
627 // Pass the audio buffers to an optional sink callback, before applying
628 // scaling/panning, as that applies to the mix operation.
629 // External recipients of the audio (e.g. via AudioTrack), will do their
630 // own mixing/dynamic processing.
631 rtc::CritScope cs(&_callbackCritSect);
632 if (audio_sink_) {
633 AudioSinkInterface::Data data(
634 &audioFrame->data_[0], audioFrame->samples_per_channel_,
635 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
636 audioFrame->timestamp_);
637 audio_sink_->OnData(data);
638 }
639 }
640
641 float output_gain = 1.0f;
642 float left_pan = 1.0f;
643 float right_pan = 1.0f;
644 {
645 rtc::CritScope cs(&volume_settings_critsect_);
646 output_gain = _outputGain;
647 left_pan = _panLeft;
648 right_pan = _panRight;
649 }
650
651 // Output volume scaling
652 if (output_gain < 0.99f || output_gain > 1.01f) {
653 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
654 }
655
656 // Scale left and/or right channel(s) if stereo and master balance is
657 // active
658
659 if (left_pan != 1.0f || right_pan != 1.0f) {
660 if (audioFrame->num_channels_ == 1) {
661 // Emulate stereo mode since panning is active.
662 // The mono signal is copied to both left and right channels here.
663 AudioFrameOperations::MonoToStereo(audioFrame);
664 }
665 // For true stereo mode (when we are receiving a stereo signal), no
666 // action is needed.
667
668 // Do the panning operation (the audio frame contains stereo at this
669 // stage)
670 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
671 }
672
673 // Mix decoded PCM output with file if file mixing is enabled
674 if (state.output_file_playing) {
675 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700676 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800677 }
678
679 // External media
680 if (_outputExternalMedia) {
681 rtc::CritScope cs(&_callbackCritSect);
682 const bool isStereo = (audioFrame->num_channels_ == 2);
683 if (_outputExternalMediaCallbackPtr) {
684 _outputExternalMediaCallbackPtr->Process(
685 _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_,
686 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
687 isStereo);
688 }
689 }
690
691 // Record playout if enabled
692 {
693 rtc::CritScope cs(&_fileCritSect);
694
kwiberg5a25d952016-08-17 07:31:12 -0700695 if (_outputFileRecording && output_file_recorder_) {
696 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800697 }
698 }
699
700 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700701 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800702 _outputAudioLevel.ComputeLevel(*audioFrame);
703
704 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
705 // The first frame with a valid rtp timestamp.
706 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
707 }
708
709 if (capture_start_rtp_time_stamp_ >= 0) {
710 // audioFrame.timestamp_ should be valid from now on.
711
712 // Compute elapsed time.
713 int64_t unwrap_timestamp =
714 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
715 audioFrame->elapsed_time_ms_ =
716 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700717 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800718
niklase@google.com470e71d2011-07-07 08:21:25 +0000719 {
kwiberg55b97fe2016-01-28 05:22:45 -0800720 rtc::CritScope lock(&ts_stats_lock_);
721 // Compute ntp time.
722 audioFrame->ntp_time_ms_ =
723 ntp_estimator_.Estimate(audioFrame->timestamp_);
724 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
725 if (audioFrame->ntp_time_ms_ > 0) {
726 // Compute |capture_start_ntp_time_ms_| so that
727 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
728 capture_start_ntp_time_ms_ =
729 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000730 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000731 }
kwiberg55b97fe2016-01-28 05:22:45 -0800732 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000733
henrik.lundin42dda502016-05-18 05:36:01 -0700734 return muted ? MixerParticipant::AudioFrameInfo::kMuted
735 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000736}
737
aleloi6c278492016-10-20 14:24:39 -0700738AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
739 int sample_rate_hz,
740 AudioFrame* audio_frame) {
741 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700742
aleloi6c278492016-10-20 14:24:39 -0700743 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700744
745 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
746 FrameInfo new_audio_frame_info = FrameInfo::kError;
747 switch (frame_info) {
748 case MixerParticipant::AudioFrameInfo::kNormal:
749 new_audio_frame_info = FrameInfo::kNormal;
750 break;
751 case MixerParticipant::AudioFrameInfo::kMuted:
752 new_audio_frame_info = FrameInfo::kMuted;
753 break;
754 case MixerParticipant::AudioFrameInfo::kError:
755 new_audio_frame_info = FrameInfo::kError;
756 break;
757 }
aleloi6c278492016-10-20 14:24:39 -0700758 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700759}
760
kwiberg55b97fe2016-01-28 05:22:45 -0800761int32_t Channel::NeededFrequency(int32_t id) const {
762 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
763 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000764
kwiberg55b97fe2016-01-28 05:22:45 -0800765 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000766
kwiberg55b97fe2016-01-28 05:22:45 -0800767 // Determine highest needed receive frequency
768 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000769
kwiberg55b97fe2016-01-28 05:22:45 -0800770 // Return the bigger of playout and receive frequency in the ACM.
771 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
772 highestNeeded = audio_coding_->PlayoutFrequency();
773 } else {
774 highestNeeded = receiveFrequency;
775 }
776
777 // Special case, if we're playing a file on the playout side
778 // we take that frequency into consideration as well
779 // This is not needed on sending side, since the codec will
780 // limit the spectrum anyway.
781 if (channel_state_.Get().output_file_playing) {
782 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700783 if (output_file_player_) {
784 if (output_file_player_->Frequency() > highestNeeded) {
785 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800786 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000787 }
kwiberg55b97fe2016-01-28 05:22:45 -0800788 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000789
kwiberg55b97fe2016-01-28 05:22:45 -0800790 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000791}
792
ossu5f7cfa52016-05-30 08:11:28 -0700793int32_t Channel::CreateChannel(
794 Channel*& channel,
795 int32_t channelId,
796 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700797 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800798 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
799 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
800 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000801
solenberg88499ec2016-09-07 07:34:41 -0700802 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800803 if (channel == NULL) {
804 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
805 "Channel::CreateChannel() unable to allocate memory for"
806 " channel");
807 return -1;
808 }
809 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000810}
811
kwiberg55b97fe2016-01-28 05:22:45 -0800812void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
813 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
814 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
815 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000816
kwiberg55b97fe2016-01-28 05:22:45 -0800817 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000818}
819
kwiberg55b97fe2016-01-28 05:22:45 -0800820void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
821 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
822 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
823 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000824
kwiberg55b97fe2016-01-28 05:22:45 -0800825 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000826}
827
kwiberg55b97fe2016-01-28 05:22:45 -0800828void Channel::PlayFileEnded(int32_t id) {
829 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
830 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000831
kwiberg55b97fe2016-01-28 05:22:45 -0800832 if (id == _inputFilePlayerId) {
833 channel_state_.SetInputFilePlaying(false);
834 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
835 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000836 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800837 } else if (id == _outputFilePlayerId) {
838 channel_state_.SetOutputFilePlaying(false);
839 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
840 "Channel::PlayFileEnded() => output file player module is"
841 " shutdown");
842 }
843}
844
845void Channel::RecordFileEnded(int32_t id) {
846 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
847 "Channel::RecordFileEnded(id=%d)", id);
848
849 assert(id == _outputFileRecorderId);
850
851 rtc::CritScope cs(&_fileCritSect);
852
853 _outputFileRecording = false;
854 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
855 "Channel::RecordFileEnded() => output file recorder module is"
856 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000857}
858
pbos@webrtc.org92135212013-05-14 08:31:39 +0000859Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000860 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700861 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800862 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100863 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700864 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800865 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100866 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800867 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100868 rtp_receive_statistics_(
869 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
870 rtp_receiver_(
871 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100872 this,
873 this,
874 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700875 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100876 _outputAudioLevel(),
877 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100878 // Avoid conflict with other channels by adding 1024 - 1026,
879 // won't use as much as 1024 channels.
880 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
881 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
882 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
883 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100884 _outputExternalMedia(false),
885 _inputExternalMediaCallbackPtr(NULL),
886 _outputExternalMediaCallbackPtr(NULL),
887 _timeStamp(0), // This is just an offset, RTP module will add it's own
888 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100889 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100890 playout_timestamp_rtp_(0),
891 playout_timestamp_rtcp_(0),
892 playout_delay_ms_(0),
893 _numberOfDiscardedPackets(0),
894 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100895 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
896 capture_start_rtp_time_stamp_(-1),
897 capture_start_ntp_time_ms_(-1),
898 _engineStatisticsPtr(NULL),
899 _outputMixerPtr(NULL),
900 _transmitMixerPtr(NULL),
901 _moduleProcessThreadPtr(NULL),
902 _audioDeviceModulePtr(NULL),
903 _voiceEngineObserverPtr(NULL),
904 _callbackCritSectPtr(NULL),
905 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100906 _sendFrameType(0),
907 _externalMixing(false),
908 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700909 input_mute_(false),
910 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100911 _panLeft(1.0f),
912 _panRight(1.0f),
913 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100914 _lastLocalTimeStamp(0),
915 _lastPayloadType(0),
916 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800917 transport_overhead_per_packet_(0),
918 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100919 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100920 restored_packet_in_use_(false),
921 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100922 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700923 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800924 feedback_observer_proxy_(new TransportFeedbackProxy()),
925 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700926 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200927 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
928 kMaxRetransmissionWindowMs)),
michaelt566d8202017-01-12 10:17:38 -0800929 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800930 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
931 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700932 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800933 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700934 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800935 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200936
kwiberg55b97fe2016-01-28 05:22:45 -0800937 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000938
kwiberg55b97fe2016-01-28 05:22:45 -0800939 RtpRtcp::Configuration configuration;
940 configuration.audio = true;
941 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800942 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800943 configuration.receive_statistics = rtp_receive_statistics_.get();
944 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800945 if (pacing_enabled_) {
946 configuration.paced_sender = rtp_packet_sender_proxy_.get();
947 configuration.transport_sequence_number_allocator =
948 seq_num_allocator_proxy_.get();
949 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
950 }
ivoc14d5dbe2016-07-04 07:06:55 -0700951 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800952 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200953 configuration.retransmission_rate_limiter =
954 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000955
kwiberg55b97fe2016-01-28 05:22:45 -0800956 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100957 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000958
kwiberg55b97fe2016-01-28 05:22:45 -0800959 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
960 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
961 statistics_proxy_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000962}
963
kwiberg55b97fe2016-01-28 05:22:45 -0800964Channel::~Channel() {
965 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
966 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
967 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000968
kwiberg55b97fe2016-01-28 05:22:45 -0800969 if (_outputExternalMedia) {
970 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
971 }
972 if (channel_state_.Get().input_external_media) {
973 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
974 }
975 StopSend();
976 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000977
kwiberg55b97fe2016-01-28 05:22:45 -0800978 {
979 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700980 if (input_file_player_) {
981 input_file_player_->RegisterModuleFileCallback(NULL);
982 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000983 }
kwiberg5a25d952016-08-17 07:31:12 -0700984 if (output_file_player_) {
985 output_file_player_->RegisterModuleFileCallback(NULL);
986 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800987 }
kwiberg5a25d952016-08-17 07:31:12 -0700988 if (output_file_recorder_) {
989 output_file_recorder_->RegisterModuleFileCallback(NULL);
990 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800991 }
992 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000993
kwiberg55b97fe2016-01-28 05:22:45 -0800994 // The order to safely shutdown modules in a channel is:
995 // 1. De-register callbacks in modules
996 // 2. De-register modules in process thread
997 // 3. Destroy modules
998 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
999 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1000 "~Channel() failed to de-register transport callback"
1001 " (Audio coding module)");
1002 }
1003 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
1004 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1005 "~Channel() failed to de-register VAD callback"
1006 " (Audio coding module)");
1007 }
1008 // De-register modules in process thread
1009 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +00001010
kwiberg55b97fe2016-01-28 05:22:45 -08001011 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +00001012}
1013
kwiberg55b97fe2016-01-28 05:22:45 -08001014int32_t Channel::Init() {
1015 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1016 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001017
kwiberg55b97fe2016-01-28 05:22:45 -08001018 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001019
kwiberg55b97fe2016-01-28 05:22:45 -08001020 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +00001021
kwiberg55b97fe2016-01-28 05:22:45 -08001022 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
1023 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1024 "Channel::Init() must call SetEngineInformation() first");
1025 return -1;
1026 }
1027
1028 // --- Add modules to process thread (for periodic schedulation)
1029
1030 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
1031
1032 // --- ACM initialization
1033
1034 if (audio_coding_->InitializeReceiver() == -1) {
1035 _engineStatisticsPtr->SetLastError(
1036 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1037 "Channel::Init() unable to initialize the ACM - 1");
1038 return -1;
1039 }
1040
1041 // --- RTP/RTCP module initialization
1042
1043 // Ensure that RTCP is enabled by default for the created channel.
1044 // Note that, the module will keep generating RTCP until it is explicitly
1045 // disabled by the user.
1046 // After StopListen (when no sockets exists), RTCP packets will no longer
1047 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001048 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001049 // RTCP is enabled by default.
1050 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1051 // --- Register all permanent callbacks
1052 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1053 (audio_coding_->RegisterVADCallback(this) == -1);
1054
1055 if (fail) {
1056 _engineStatisticsPtr->SetLastError(
1057 VE_CANNOT_INIT_CHANNEL, kTraceError,
1058 "Channel::Init() callbacks not registered");
1059 return -1;
1060 }
1061
1062 // --- Register all supported codecs to the receiving side of the
1063 // RTP/RTCP module
1064
1065 CodecInst codec;
1066 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1067
1068 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1069 // Open up the RTP/RTCP receiver for all supported codecs
1070 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08001071 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001072 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1073 "Channel::Init() unable to register %s "
1074 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1075 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1076 codec.rate);
1077 } else {
1078 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1079 "Channel::Init() %s (%d/%d/%" PRIuS
1080 "/%d) has been "
1081 "added to the RTP/RTCP receiver",
1082 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1083 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001084 }
1085
kwiberg55b97fe2016-01-28 05:22:45 -08001086 // Ensure that PCMU is used as default codec on the sending side
1087 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1088 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001089 }
1090
kwiberg55b97fe2016-01-28 05:22:45 -08001091 // Register default PT for outband 'telephone-event'
1092 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001093 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001094 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1095 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001096 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1097 "Channel::Init() failed to register outband "
1098 "'telephone-event' (%d/%d) correctly",
1099 codec.pltype, codec.plfreq);
1100 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001101 }
1102
kwiberg55b97fe2016-01-28 05:22:45 -08001103 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001104 if (!codec_manager_.RegisterEncoder(codec) ||
1105 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001106 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1107 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001108 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001109 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1110 "Channel::Init() failed to register CN (%d/%d) "
1111 "correctly - 1",
1112 codec.pltype, codec.plfreq);
1113 }
1114 }
kwiberg55b97fe2016-01-28 05:22:45 -08001115 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001116
kwiberg55b97fe2016-01-28 05:22:45 -08001117 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001118}
1119
kwiberg55b97fe2016-01-28 05:22:45 -08001120int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1121 OutputMixer& outputMixer,
1122 voe::TransmitMixer& transmitMixer,
1123 ProcessThread& moduleProcessThread,
1124 AudioDeviceModule& audioDeviceModule,
1125 VoiceEngineObserver* voiceEngineObserver,
1126 rtc::CriticalSection* callbackCritSect) {
1127 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1128 "Channel::SetEngineInformation()");
1129 _engineStatisticsPtr = &engineStatistics;
1130 _outputMixerPtr = &outputMixer;
1131 _transmitMixerPtr = &transmitMixer,
1132 _moduleProcessThreadPtr = &moduleProcessThread;
1133 _audioDeviceModulePtr = &audioDeviceModule;
1134 _voiceEngineObserverPtr = voiceEngineObserver;
1135 _callbackCritSectPtr = callbackCritSect;
1136 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001137}
1138
kwiberg55b97fe2016-01-28 05:22:45 -08001139int32_t Channel::UpdateLocalTimeStamp() {
1140 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1141 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001142}
1143
kwibergb7f89d62016-02-17 10:04:18 -08001144void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001145 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001146 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001147}
1148
ossu29b1a8d2016-06-13 07:34:51 -07001149const rtc::scoped_refptr<AudioDecoderFactory>&
1150Channel::GetAudioDecoderFactory() const {
1151 return decoder_factory_;
1152}
1153
kwiberg55b97fe2016-01-28 05:22:45 -08001154int32_t Channel::StartPlayout() {
1155 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1156 "Channel::StartPlayout()");
1157 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001158 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001159 }
1160
1161 if (!_externalMixing) {
1162 // Add participant as candidates for mixing.
1163 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1164 _engineStatisticsPtr->SetLastError(
1165 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1166 "StartPlayout() failed to add participant to mixer");
1167 return -1;
1168 }
1169 }
1170
1171 channel_state_.SetPlaying(true);
1172 if (RegisterFilePlayingToMixer() != 0)
1173 return -1;
1174
1175 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001176}
1177
kwiberg55b97fe2016-01-28 05:22:45 -08001178int32_t Channel::StopPlayout() {
1179 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1180 "Channel::StopPlayout()");
1181 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001182 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001183 }
1184
1185 if (!_externalMixing) {
1186 // Remove participant as candidates for mixing
1187 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1188 _engineStatisticsPtr->SetLastError(
1189 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1190 "StopPlayout() failed to remove participant from mixer");
1191 return -1;
1192 }
1193 }
1194
1195 channel_state_.SetPlaying(false);
1196 _outputAudioLevel.Clear();
1197
1198 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001199}
1200
kwiberg55b97fe2016-01-28 05:22:45 -08001201int32_t Channel::StartSend() {
1202 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1203 "Channel::StartSend()");
1204 // Resume the previous sequence number which was reset by StopSend().
1205 // This needs to be done before |sending| is set to true.
1206 if (send_sequence_number_)
1207 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001208
kwiberg55b97fe2016-01-28 05:22:45 -08001209 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001210 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001211 }
1212 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001213
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001214 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001215 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1216 _engineStatisticsPtr->SetLastError(
1217 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1218 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001219 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001220 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001221 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001222 return -1;
1223 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001224
kwiberg55b97fe2016-01-28 05:22:45 -08001225 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001226}
1227
kwiberg55b97fe2016-01-28 05:22:45 -08001228int32_t Channel::StopSend() {
1229 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1230 "Channel::StopSend()");
1231 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001232 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001233 }
1234 channel_state_.SetSending(false);
1235
1236 // Store the sequence number to be able to pick up the same sequence for
1237 // the next StartSend(). This is needed for restarting device, otherwise
1238 // it might cause libSRTP to complain about packets being replayed.
1239 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1240 // CL is landed. See issue
1241 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1242 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1243
1244 // Reset sending SSRC and sequence number and triggers direct transmission
1245 // of RTCP BYE
1246 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1247 _engineStatisticsPtr->SetLastError(
1248 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1249 "StartSend() RTP/RTCP failed to stop sending");
1250 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001251 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001252
1253 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001254}
1255
solenberge566ac72016-10-31 12:52:33 -07001256void Channel::ResetDiscardedPacketCount() {
kwiberg55b97fe2016-01-28 05:22:45 -08001257 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberge566ac72016-10-31 12:52:33 -07001258 "Channel::ResetDiscardedPacketCount()");
kwiberg55b97fe2016-01-28 05:22:45 -08001259 _numberOfDiscardedPackets = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001260}
1261
kwiberg55b97fe2016-01-28 05:22:45 -08001262int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1263 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1264 "Channel::RegisterVoiceEngineObserver()");
1265 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001266
kwiberg55b97fe2016-01-28 05:22:45 -08001267 if (_voiceEngineObserverPtr) {
1268 _engineStatisticsPtr->SetLastError(
1269 VE_INVALID_OPERATION, kTraceError,
1270 "RegisterVoiceEngineObserver() observer already enabled");
1271 return -1;
1272 }
1273 _voiceEngineObserverPtr = &observer;
1274 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001275}
1276
kwiberg55b97fe2016-01-28 05:22:45 -08001277int32_t Channel::DeRegisterVoiceEngineObserver() {
1278 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1279 "Channel::DeRegisterVoiceEngineObserver()");
1280 rtc::CritScope cs(&_callbackCritSect);
1281
1282 if (!_voiceEngineObserverPtr) {
1283 _engineStatisticsPtr->SetLastError(
1284 VE_INVALID_OPERATION, kTraceWarning,
1285 "DeRegisterVoiceEngineObserver() observer already disabled");
1286 return 0;
1287 }
1288 _voiceEngineObserverPtr = NULL;
1289 return 0;
1290}
1291
1292int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001293 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001294 if (send_codec) {
1295 codec = *send_codec;
1296 return 0;
1297 }
1298 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001299}
1300
kwiberg55b97fe2016-01-28 05:22:45 -08001301int32_t Channel::GetRecCodec(CodecInst& codec) {
1302 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001303}
1304
kwiberg55b97fe2016-01-28 05:22:45 -08001305int32_t Channel::SetSendCodec(const CodecInst& codec) {
1306 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1307 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001308
kwibergc8d071e2016-04-06 12:22:38 -07001309 if (!codec_manager_.RegisterEncoder(codec) ||
1310 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001311 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1312 "SetSendCodec() failed to register codec to ACM");
1313 return -1;
1314 }
1315
1316 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1317 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1318 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1319 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1320 "SetSendCodec() failed to register codec to"
1321 " RTP/RTCP module");
1322 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001323 }
kwiberg55b97fe2016-01-28 05:22:45 -08001324 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001325
kwiberg55b97fe2016-01-28 05:22:45 -08001326 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001327}
1328
minyue78b4d562016-11-30 04:47:39 -08001329void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001330 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1331 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001332 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001333 if (*encoder) {
1334 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001335 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001336 }
1337 });
michaelt566d8202017-01-12 10:17:38 -08001338 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001339}
1340
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001341void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001342 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1343 if (*encoder)
1344 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1345 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001346}
1347
kwiberg55b97fe2016-01-28 05:22:45 -08001348int32_t Channel::SetVADStatus(bool enableVAD,
1349 ACMVADMode mode,
1350 bool disableDTX) {
1351 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1352 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001353 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1354 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1355 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001356 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1357 kTraceError,
1358 "SetVADStatus() failed to set VAD");
1359 return -1;
1360 }
1361 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001362}
1363
kwiberg55b97fe2016-01-28 05:22:45 -08001364int32_t Channel::GetVADStatus(bool& enabledVAD,
1365 ACMVADMode& mode,
1366 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001367 const auto* params = codec_manager_.GetStackParams();
1368 enabledVAD = params->use_cng;
1369 mode = params->vad_mode;
1370 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001371 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001372}
1373
kwiberg55b97fe2016-01-28 05:22:45 -08001374int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1375 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1376 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001377
kwiberg55b97fe2016-01-28 05:22:45 -08001378 if (channel_state_.Get().playing) {
1379 _engineStatisticsPtr->SetLastError(
1380 VE_ALREADY_PLAYING, kTraceError,
1381 "SetRecPayloadType() unable to set PT while playing");
1382 return -1;
1383 }
kwiberg55b97fe2016-01-28 05:22:45 -08001384
1385 if (codec.pltype == -1) {
1386 // De-register the selected codec (RTP/RTCP module and ACM)
1387
1388 int8_t pltype(-1);
1389 CodecInst rxCodec = codec;
1390
1391 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001392 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001393 rxCodec.pltype = pltype;
1394
1395 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1396 _engineStatisticsPtr->SetLastError(
1397 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1398 "SetRecPayloadType() RTP/RTCP-module deregistration "
1399 "failed");
1400 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001401 }
kwiberg55b97fe2016-01-28 05:22:45 -08001402 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1403 _engineStatisticsPtr->SetLastError(
1404 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1405 "SetRecPayloadType() ACM deregistration failed - 1");
1406 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001407 }
kwiberg55b97fe2016-01-28 05:22:45 -08001408 return 0;
1409 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001410
magjed56124bd2016-11-24 09:34:46 -08001411 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001412 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001413 // TODO(kwiberg): Retrying is probably not necessary, since
1414 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001415 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001416 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001417 _engineStatisticsPtr->SetLastError(
1418 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1419 "SetRecPayloadType() RTP/RTCP-module registration failed");
1420 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001421 }
kwiberg55b97fe2016-01-28 05:22:45 -08001422 }
kwibergda2bf4e2016-10-24 13:47:09 -07001423 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1424 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001425 audio_coding_->UnregisterReceiveCodec(codec.pltype);
kwibergda2bf4e2016-10-24 13:47:09 -07001426 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1427 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001428 _engineStatisticsPtr->SetLastError(
1429 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1430 "SetRecPayloadType() ACM registration failed - 1");
1431 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001432 }
kwiberg55b97fe2016-01-28 05:22:45 -08001433 }
1434 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001435}
1436
kwiberg55b97fe2016-01-28 05:22:45 -08001437int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1438 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001439 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001440 _engineStatisticsPtr->SetLastError(
1441 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1442 "GetRecPayloadType() failed to retrieve RX payload type");
1443 return -1;
1444 }
1445 codec.pltype = payloadType;
1446 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001447}
1448
kwiberg55b97fe2016-01-28 05:22:45 -08001449int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1450 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1451 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001452
kwiberg55b97fe2016-01-28 05:22:45 -08001453 CodecInst codec;
1454 int32_t samplingFreqHz(-1);
1455 const size_t kMono = 1;
1456 if (frequency == kFreq32000Hz)
1457 samplingFreqHz = 32000;
1458 else if (frequency == kFreq16000Hz)
1459 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001460
kwiberg55b97fe2016-01-28 05:22:45 -08001461 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1462 _engineStatisticsPtr->SetLastError(
1463 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1464 "SetSendCNPayloadType() failed to retrieve default CN codec "
1465 "settings");
1466 return -1;
1467 }
1468
1469 // Modify the payload type (must be set to dynamic range)
1470 codec.pltype = type;
1471
kwibergc8d071e2016-04-06 12:22:38 -07001472 if (!codec_manager_.RegisterEncoder(codec) ||
1473 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001474 _engineStatisticsPtr->SetLastError(
1475 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1476 "SetSendCNPayloadType() failed to register CN to ACM");
1477 return -1;
1478 }
1479
1480 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1481 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1482 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1483 _engineStatisticsPtr->SetLastError(
1484 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1485 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1486 "module");
1487 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001488 }
kwiberg55b97fe2016-01-28 05:22:45 -08001489 }
1490 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001491}
1492
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001493int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001494 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001495 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001496
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001497 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001498 _engineStatisticsPtr->SetLastError(
1499 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001500 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001501 return -1;
1502 }
1503 return 0;
1504}
1505
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001506int Channel::SetOpusDtx(bool enable_dtx) {
1507 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1508 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001509 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001510 : audio_coding_->DisableOpusDtx();
1511 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001512 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1513 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001514 return -1;
1515 }
1516 return 0;
1517}
1518
ivoc85228d62016-07-27 04:53:47 -07001519int Channel::GetOpusDtx(bool* enabled) {
1520 int success = -1;
1521 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1522 if (encoder) {
1523 *enabled = encoder->GetDtx();
1524 success = 0;
1525 }
1526 });
1527 return success;
1528}
1529
minyue7e304322016-10-12 05:00:55 -07001530bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1531 bool success = false;
1532 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1533 if (*encoder) {
1534 success = (*encoder)->EnableAudioNetworkAdaptor(
1535 config_string, Clock::GetRealTimeClock());
1536 }
1537 });
1538 return success;
1539}
1540
1541void Channel::DisableAudioNetworkAdaptor() {
1542 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1543 if (*encoder)
1544 (*encoder)->DisableAudioNetworkAdaptor();
1545 });
1546}
1547
1548void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1549 int max_frame_length_ms) {
1550 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1551 if (*encoder) {
1552 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1553 max_frame_length_ms);
1554 }
1555 });
1556}
1557
mflodman3d7db262016-04-29 00:57:13 -07001558int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001559 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001560 "Channel::RegisterExternalTransport()");
1561
kwiberg55b97fe2016-01-28 05:22:45 -08001562 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001563 if (_externalTransport) {
1564 _engineStatisticsPtr->SetLastError(
1565 VE_INVALID_OPERATION, kTraceError,
1566 "RegisterExternalTransport() external transport already enabled");
1567 return -1;
1568 }
1569 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001570 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001571 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001572}
1573
kwiberg55b97fe2016-01-28 05:22:45 -08001574int32_t Channel::DeRegisterExternalTransport() {
1575 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1576 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001577
kwiberg55b97fe2016-01-28 05:22:45 -08001578 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001579 if (_transportPtr) {
1580 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1581 "DeRegisterExternalTransport() all transport is disabled");
1582 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001583 _engineStatisticsPtr->SetLastError(
1584 VE_INVALID_OPERATION, kTraceWarning,
1585 "DeRegisterExternalTransport() external transport already "
1586 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001587 }
1588 _externalTransport = false;
1589 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001590 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001591}
1592
mflodman3d7db262016-04-29 00:57:13 -07001593int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001594 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001595 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001596 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001597 "Channel::ReceivedRTPPacket()");
1598
1599 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001600 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001601
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001602 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001603 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1604 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1605 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001606 return -1;
1607 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001608 header.payload_type_frequency =
1609 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001610 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001611 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001612 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001613 rtp_receive_statistics_->IncomingPacket(
1614 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001615 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001616
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001617 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001618}
1619
1620bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001621 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001622 const RTPHeader& header,
1623 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001624 if (rtp_payload_registry_->IsRtx(header)) {
1625 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001626 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001627 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001628 assert(packet_length >= header.headerLength);
1629 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001630 PayloadUnion payload_specific;
1631 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001632 &payload_specific)) {
1633 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001634 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001635 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1636 payload_specific, in_order);
1637}
1638
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001639bool Channel::HandleRtxPacket(const uint8_t* packet,
1640 size_t packet_length,
1641 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001642 if (!rtp_payload_registry_->IsRtx(header))
1643 return false;
1644
1645 // Remove the RTX header and parse the original RTP header.
1646 if (packet_length < header.headerLength)
1647 return false;
1648 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1649 return false;
1650 if (restored_packet_in_use_) {
1651 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1652 "Multiple RTX headers detected, dropping packet");
1653 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001654 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001655 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001656 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1657 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001658 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1659 "Incoming RTX packet: invalid RTP header");
1660 return false;
1661 }
1662 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001663 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001664 restored_packet_in_use_ = false;
1665 return ret;
1666}
1667
1668bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1669 StreamStatistician* statistician =
1670 rtp_receive_statistics_->GetStatistician(header.ssrc);
1671 if (!statistician)
1672 return false;
1673 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001674}
1675
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001676bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1677 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001678 // Retransmissions are handled separately if RTX is enabled.
1679 if (rtp_payload_registry_->RtxEnabled())
1680 return false;
1681 StreamStatistician* statistician =
1682 rtp_receive_statistics_->GetStatistician(header.ssrc);
1683 if (!statistician)
1684 return false;
1685 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001686 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001687 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001688 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001689}
1690
mflodman3d7db262016-04-29 00:57:13 -07001691int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001692 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001693 "Channel::ReceivedRTCPPacket()");
1694 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001695 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001696
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001697 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001698 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001699 _engineStatisticsPtr->SetLastError(
1700 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1701 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1702 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001703
Minyue2013aec2015-05-13 14:14:42 +02001704 int64_t rtt = GetRTT(true);
1705 if (rtt == 0) {
1706 // Waiting for valid RTT.
1707 return 0;
1708 }
Erik Språng737336d2016-07-29 12:59:36 +02001709
1710 int64_t nack_window_ms = rtt;
1711 if (nack_window_ms < kMinRetransmissionWindowMs) {
1712 nack_window_ms = kMinRetransmissionWindowMs;
1713 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1714 nack_window_ms = kMaxRetransmissionWindowMs;
1715 }
1716 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1717
minyue7e304322016-10-12 05:00:55 -07001718 // Invoke audio encoders OnReceivedRtt().
1719 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1720 if (*encoder)
1721 (*encoder)->OnReceivedRtt(rtt);
1722 });
1723
Minyue2013aec2015-05-13 14:14:42 +02001724 uint32_t ntp_secs = 0;
1725 uint32_t ntp_frac = 0;
1726 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001727 if (0 !=
1728 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1729 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001730 // Waiting for RTCP.
1731 return 0;
1732 }
1733
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001734 {
tommi31fc21f2016-01-21 10:37:37 -08001735 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001736 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001737 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001738 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001739}
1740
niklase@google.com470e71d2011-07-07 08:21:25 +00001741int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001742 bool loop,
1743 FileFormats format,
1744 int startPosition,
1745 float volumeScaling,
1746 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001747 const CodecInst* codecInst) {
1748 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1749 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1750 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1751 "stopPosition=%d)",
1752 fileName, loop, format, volumeScaling, startPosition,
1753 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001754
kwiberg55b97fe2016-01-28 05:22:45 -08001755 if (channel_state_.Get().output_file_playing) {
1756 _engineStatisticsPtr->SetLastError(
1757 VE_ALREADY_PLAYING, kTraceError,
1758 "StartPlayingFileLocally() is already playing");
1759 return -1;
1760 }
1761
1762 {
1763 rtc::CritScope cs(&_fileCritSect);
1764
kwiberg5a25d952016-08-17 07:31:12 -07001765 if (output_file_player_) {
1766 output_file_player_->RegisterModuleFileCallback(NULL);
1767 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001768 }
1769
kwiberg5b356f42016-09-08 04:32:33 -07001770 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001771 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001772
kwiberg5a25d952016-08-17 07:31:12 -07001773 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001774 _engineStatisticsPtr->SetLastError(
1775 VE_INVALID_ARGUMENT, kTraceError,
1776 "StartPlayingFileLocally() filePlayer format is not correct");
1777 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001778 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001779
kwiberg55b97fe2016-01-28 05:22:45 -08001780 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001781
kwiberg5a25d952016-08-17 07:31:12 -07001782 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001783 fileName, loop, startPosition, volumeScaling, notificationTime,
1784 stopPosition, (const CodecInst*)codecInst) != 0) {
1785 _engineStatisticsPtr->SetLastError(
1786 VE_BAD_FILE, kTraceError,
1787 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001788 output_file_player_->StopPlayingFile();
1789 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001790 return -1;
1791 }
kwiberg5a25d952016-08-17 07:31:12 -07001792 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001793 channel_state_.SetOutputFilePlaying(true);
1794 }
1795
1796 if (RegisterFilePlayingToMixer() != 0)
1797 return -1;
1798
1799 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001800}
1801
1802int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001803 FileFormats format,
1804 int startPosition,
1805 float volumeScaling,
1806 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001807 const CodecInst* codecInst) {
1808 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1809 "Channel::StartPlayingFileLocally(format=%d,"
1810 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1811 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001812
kwiberg55b97fe2016-01-28 05:22:45 -08001813 if (stream == NULL) {
1814 _engineStatisticsPtr->SetLastError(
1815 VE_BAD_FILE, kTraceError,
1816 "StartPlayingFileLocally() NULL as input stream");
1817 return -1;
1818 }
1819
1820 if (channel_state_.Get().output_file_playing) {
1821 _engineStatisticsPtr->SetLastError(
1822 VE_ALREADY_PLAYING, kTraceError,
1823 "StartPlayingFileLocally() is already playing");
1824 return -1;
1825 }
1826
1827 {
1828 rtc::CritScope cs(&_fileCritSect);
1829
1830 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001831 if (output_file_player_) {
1832 output_file_player_->RegisterModuleFileCallback(NULL);
1833 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001834 }
1835
kwiberg55b97fe2016-01-28 05:22:45 -08001836 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001837 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001838 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001839
kwiberg5a25d952016-08-17 07:31:12 -07001840 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001841 _engineStatisticsPtr->SetLastError(
1842 VE_INVALID_ARGUMENT, kTraceError,
1843 "StartPlayingFileLocally() filePlayer format isnot correct");
1844 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001845 }
1846
kwiberg55b97fe2016-01-28 05:22:45 -08001847 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001848
kwiberg4ec01d92016-08-22 08:43:54 -07001849 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001850 volumeScaling, notificationTime,
1851 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001852 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1853 "StartPlayingFile() failed to "
1854 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001855 output_file_player_->StopPlayingFile();
1856 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001857 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001858 }
kwiberg5a25d952016-08-17 07:31:12 -07001859 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001860 channel_state_.SetOutputFilePlaying(true);
1861 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001862
kwiberg55b97fe2016-01-28 05:22:45 -08001863 if (RegisterFilePlayingToMixer() != 0)
1864 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001865
kwiberg55b97fe2016-01-28 05:22:45 -08001866 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001867}
1868
kwiberg55b97fe2016-01-28 05:22:45 -08001869int Channel::StopPlayingFileLocally() {
1870 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1871 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001872
kwiberg55b97fe2016-01-28 05:22:45 -08001873 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001874 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001875 }
1876
1877 {
1878 rtc::CritScope cs(&_fileCritSect);
1879
kwiberg5a25d952016-08-17 07:31:12 -07001880 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001881 _engineStatisticsPtr->SetLastError(
1882 VE_STOP_RECORDING_FAILED, kTraceError,
1883 "StopPlayingFile() could not stop playing");
1884 return -1;
1885 }
kwiberg5a25d952016-08-17 07:31:12 -07001886 output_file_player_->RegisterModuleFileCallback(NULL);
1887 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001888 channel_state_.SetOutputFilePlaying(false);
1889 }
1890 // _fileCritSect cannot be taken while calling
1891 // SetAnonymousMixibilityStatus. Refer to comments in
1892 // StartPlayingFileLocally(const char* ...) for more details.
1893 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1894 _engineStatisticsPtr->SetLastError(
1895 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1896 "StopPlayingFile() failed to stop participant from playing as"
1897 "file in the mixer");
1898 return -1;
1899 }
1900
1901 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001902}
1903
kwiberg55b97fe2016-01-28 05:22:45 -08001904int Channel::IsPlayingFileLocally() const {
1905 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001906}
1907
kwiberg55b97fe2016-01-28 05:22:45 -08001908int Channel::RegisterFilePlayingToMixer() {
1909 // Return success for not registering for file playing to mixer if:
1910 // 1. playing file before playout is started on that channel.
1911 // 2. starting playout without file playing on that channel.
1912 if (!channel_state_.Get().playing ||
1913 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001914 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001915 }
1916
1917 // |_fileCritSect| cannot be taken while calling
1918 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1919 // frames can be pulled by the mixer. Since the frames are generated from
1920 // the file, _fileCritSect will be taken. This would result in a deadlock.
1921 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1922 channel_state_.SetOutputFilePlaying(false);
1923 rtc::CritScope cs(&_fileCritSect);
1924 _engineStatisticsPtr->SetLastError(
1925 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1926 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001927 output_file_player_->StopPlayingFile();
1928 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001929 return -1;
1930 }
1931
1932 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001933}
1934
niklase@google.com470e71d2011-07-07 08:21:25 +00001935int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001936 bool loop,
1937 FileFormats format,
1938 int startPosition,
1939 float volumeScaling,
1940 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001941 const CodecInst* codecInst) {
1942 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1943 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1944 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1945 "stopPosition=%d)",
1946 fileName, loop, format, volumeScaling, startPosition,
1947 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001948
kwiberg55b97fe2016-01-28 05:22:45 -08001949 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001950
kwiberg55b97fe2016-01-28 05:22:45 -08001951 if (channel_state_.Get().input_file_playing) {
1952 _engineStatisticsPtr->SetLastError(
1953 VE_ALREADY_PLAYING, kTraceWarning,
1954 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001955 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001956 }
1957
1958 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001959 if (input_file_player_) {
1960 input_file_player_->RegisterModuleFileCallback(NULL);
1961 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001962 }
1963
1964 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001965 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001966 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001967
kwiberg5a25d952016-08-17 07:31:12 -07001968 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001969 _engineStatisticsPtr->SetLastError(
1970 VE_INVALID_ARGUMENT, kTraceError,
1971 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1972 return -1;
1973 }
1974
1975 const uint32_t notificationTime(0);
1976
kwiberg5a25d952016-08-17 07:31:12 -07001977 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001978 fileName, loop, startPosition, volumeScaling, notificationTime,
1979 stopPosition, (const CodecInst*)codecInst) != 0) {
1980 _engineStatisticsPtr->SetLastError(
1981 VE_BAD_FILE, kTraceError,
1982 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001983 input_file_player_->StopPlayingFile();
1984 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001985 return -1;
1986 }
kwiberg5a25d952016-08-17 07:31:12 -07001987 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001988 channel_state_.SetInputFilePlaying(true);
1989
1990 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001991}
1992
1993int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001994 FileFormats format,
1995 int startPosition,
1996 float volumeScaling,
1997 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001998 const CodecInst* codecInst) {
1999 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2000 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2001 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2002 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002003
kwiberg55b97fe2016-01-28 05:22:45 -08002004 if (stream == NULL) {
2005 _engineStatisticsPtr->SetLastError(
2006 VE_BAD_FILE, kTraceError,
2007 "StartPlayingFileAsMicrophone NULL as input stream");
2008 return -1;
2009 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002010
kwiberg55b97fe2016-01-28 05:22:45 -08002011 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002012
kwiberg55b97fe2016-01-28 05:22:45 -08002013 if (channel_state_.Get().input_file_playing) {
2014 _engineStatisticsPtr->SetLastError(
2015 VE_ALREADY_PLAYING, kTraceWarning,
2016 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002017 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002018 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002019
kwiberg55b97fe2016-01-28 05:22:45 -08002020 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002021 if (input_file_player_) {
2022 input_file_player_->RegisterModuleFileCallback(NULL);
2023 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002024 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002025
kwiberg55b97fe2016-01-28 05:22:45 -08002026 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002027 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002028 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002029
kwiberg5a25d952016-08-17 07:31:12 -07002030 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002031 _engineStatisticsPtr->SetLastError(
2032 VE_INVALID_ARGUMENT, kTraceError,
2033 "StartPlayingInputFile() filePlayer format isnot correct");
2034 return -1;
2035 }
2036
2037 const uint32_t notificationTime(0);
2038
kwiberg4ec01d92016-08-22 08:43:54 -07002039 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2040 notificationTime, stopPosition,
2041 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002042 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2043 "StartPlayingFile() failed to start "
2044 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002045 input_file_player_->StopPlayingFile();
2046 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002047 return -1;
2048 }
2049
kwiberg5a25d952016-08-17 07:31:12 -07002050 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002051 channel_state_.SetInputFilePlaying(true);
2052
2053 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002054}
2055
kwiberg55b97fe2016-01-28 05:22:45 -08002056int Channel::StopPlayingFileAsMicrophone() {
2057 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2058 "Channel::StopPlayingFileAsMicrophone()");
2059
2060 rtc::CritScope cs(&_fileCritSect);
2061
2062 if (!channel_state_.Get().input_file_playing) {
2063 return 0;
2064 }
2065
kwiberg5a25d952016-08-17 07:31:12 -07002066 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002067 _engineStatisticsPtr->SetLastError(
2068 VE_STOP_RECORDING_FAILED, kTraceError,
2069 "StopPlayingFile() could not stop playing");
2070 return -1;
2071 }
kwiberg5a25d952016-08-17 07:31:12 -07002072 input_file_player_->RegisterModuleFileCallback(NULL);
2073 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002074 channel_state_.SetInputFilePlaying(false);
2075
2076 return 0;
2077}
2078
2079int Channel::IsPlayingFileAsMicrophone() const {
2080 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002081}
2082
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002083int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002084 const CodecInst* codecInst) {
2085 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2086 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002087
kwiberg55b97fe2016-01-28 05:22:45 -08002088 if (_outputFileRecording) {
2089 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2090 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002091 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002092 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002093
kwiberg55b97fe2016-01-28 05:22:45 -08002094 FileFormats format;
2095 const uint32_t notificationTime(0); // Not supported in VoE
2096 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002097
kwiberg55b97fe2016-01-28 05:22:45 -08002098 if ((codecInst != NULL) &&
2099 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2100 _engineStatisticsPtr->SetLastError(
2101 VE_BAD_ARGUMENT, kTraceError,
2102 "StartRecordingPlayout() invalid compression");
2103 return (-1);
2104 }
2105 if (codecInst == NULL) {
2106 format = kFileFormatPcm16kHzFile;
2107 codecInst = &dummyCodec;
2108 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2109 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2110 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2111 format = kFileFormatWavFile;
2112 } else {
2113 format = kFileFormatCompressedFile;
2114 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002115
kwiberg55b97fe2016-01-28 05:22:45 -08002116 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002117
kwiberg55b97fe2016-01-28 05:22:45 -08002118 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002119 if (output_file_recorder_) {
2120 output_file_recorder_->RegisterModuleFileCallback(NULL);
2121 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002122 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002123
kwiberg5a25d952016-08-17 07:31:12 -07002124 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002125 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002126 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002127 _engineStatisticsPtr->SetLastError(
2128 VE_INVALID_ARGUMENT, kTraceError,
2129 "StartRecordingPlayout() fileRecorder format isnot correct");
2130 return -1;
2131 }
2132
kwiberg5a25d952016-08-17 07:31:12 -07002133 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002134 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2135 _engineStatisticsPtr->SetLastError(
2136 VE_BAD_FILE, kTraceError,
2137 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002138 output_file_recorder_->StopRecording();
2139 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002140 return -1;
2141 }
kwiberg5a25d952016-08-17 07:31:12 -07002142 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002143 _outputFileRecording = true;
2144
2145 return 0;
2146}
2147
2148int Channel::StartRecordingPlayout(OutStream* stream,
2149 const CodecInst* codecInst) {
2150 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2151 "Channel::StartRecordingPlayout()");
2152
2153 if (_outputFileRecording) {
2154 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2155 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002156 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002157 }
2158
2159 FileFormats format;
2160 const uint32_t notificationTime(0); // Not supported in VoE
2161 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2162
2163 if (codecInst != NULL && codecInst->channels != 1) {
2164 _engineStatisticsPtr->SetLastError(
2165 VE_BAD_ARGUMENT, kTraceError,
2166 "StartRecordingPlayout() invalid compression");
2167 return (-1);
2168 }
2169 if (codecInst == NULL) {
2170 format = kFileFormatPcm16kHzFile;
2171 codecInst = &dummyCodec;
2172 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2173 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2174 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2175 format = kFileFormatWavFile;
2176 } else {
2177 format = kFileFormatCompressedFile;
2178 }
2179
2180 rtc::CritScope cs(&_fileCritSect);
2181
2182 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002183 if (output_file_recorder_) {
2184 output_file_recorder_->RegisterModuleFileCallback(NULL);
2185 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002186 }
2187
kwiberg5a25d952016-08-17 07:31:12 -07002188 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002189 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002190 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002191 _engineStatisticsPtr->SetLastError(
2192 VE_INVALID_ARGUMENT, kTraceError,
2193 "StartRecordingPlayout() fileRecorder format isnot correct");
2194 return -1;
2195 }
2196
kwiberg4ec01d92016-08-22 08:43:54 -07002197 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002198 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002199 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2200 "StartRecordingPlayout() failed to "
2201 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002202 output_file_recorder_->StopRecording();
2203 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002204 return -1;
2205 }
2206
kwiberg5a25d952016-08-17 07:31:12 -07002207 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002208 _outputFileRecording = true;
2209
2210 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002211}
2212
kwiberg55b97fe2016-01-28 05:22:45 -08002213int Channel::StopRecordingPlayout() {
2214 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2215 "Channel::StopRecordingPlayout()");
2216
2217 if (!_outputFileRecording) {
2218 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2219 "StopRecordingPlayout() isnot recording");
2220 return -1;
2221 }
2222
2223 rtc::CritScope cs(&_fileCritSect);
2224
kwiberg5a25d952016-08-17 07:31:12 -07002225 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002226 _engineStatisticsPtr->SetLastError(
2227 VE_STOP_RECORDING_FAILED, kTraceError,
2228 "StopRecording() could not stop recording");
2229 return (-1);
2230 }
kwiberg5a25d952016-08-17 07:31:12 -07002231 output_file_recorder_->RegisterModuleFileCallback(NULL);
2232 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002233 _outputFileRecording = false;
2234
2235 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002236}
2237
kwiberg55b97fe2016-01-28 05:22:45 -08002238void Channel::SetMixWithMicStatus(bool mix) {
2239 rtc::CritScope cs(&_fileCritSect);
2240 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002241}
2242
kwiberg55b97fe2016-01-28 05:22:45 -08002243int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2244 int8_t currentLevel = _outputAudioLevel.Level();
2245 level = static_cast<int32_t>(currentLevel);
2246 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002247}
2248
kwiberg55b97fe2016-01-28 05:22:45 -08002249int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2250 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2251 level = static_cast<int32_t>(currentLevel);
2252 return 0;
2253}
2254
solenberg1c2af8e2016-03-24 10:36:00 -07002255int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002256 rtc::CritScope cs(&volume_settings_critsect_);
2257 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002258 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002259 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002260 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002261}
2262
solenberg1c2af8e2016-03-24 10:36:00 -07002263bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002264 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002265 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002266}
2267
kwiberg55b97fe2016-01-28 05:22:45 -08002268int Channel::SetOutputVolumePan(float left, float right) {
2269 rtc::CritScope cs(&volume_settings_critsect_);
2270 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002271 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002272 _panLeft = left;
2273 _panRight = right;
2274 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002275}
2276
kwiberg55b97fe2016-01-28 05:22:45 -08002277int Channel::GetOutputVolumePan(float& left, float& right) const {
2278 rtc::CritScope cs(&volume_settings_critsect_);
2279 left = _panLeft;
2280 right = _panRight;
2281 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002282}
2283
kwiberg55b97fe2016-01-28 05:22:45 -08002284int Channel::SetChannelOutputVolumeScaling(float scaling) {
2285 rtc::CritScope cs(&volume_settings_critsect_);
2286 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002287 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002288 _outputGain = scaling;
2289 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002290}
2291
kwiberg55b97fe2016-01-28 05:22:45 -08002292int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2293 rtc::CritScope cs(&volume_settings_critsect_);
2294 scaling = _outputGain;
2295 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002296}
2297
solenberg8842c3e2016-03-11 03:06:41 -08002298int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002299 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002300 "Channel::SendTelephoneEventOutband(...)");
2301 RTC_DCHECK_LE(0, event);
2302 RTC_DCHECK_GE(255, event);
2303 RTC_DCHECK_LE(0, duration_ms);
2304 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002305 if (!Sending()) {
2306 return -1;
2307 }
solenberg8842c3e2016-03-11 03:06:41 -08002308 if (_rtpRtcpModule->SendTelephoneEventOutband(
2309 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002310 _engineStatisticsPtr->SetLastError(
2311 VE_SEND_DTMF_FAILED, kTraceWarning,
2312 "SendTelephoneEventOutband() failed to send event");
2313 return -1;
2314 }
2315 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002316}
2317
solenbergffbbcac2016-11-17 05:25:37 -08002318int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2319 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002320 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002321 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002322 RTC_DCHECK_LE(0, payload_type);
2323 RTC_DCHECK_GE(127, payload_type);
2324 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002325 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002326 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002327 memcpy(codec.plname, "telephone-event", 16);
2328 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2329 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2330 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2331 _engineStatisticsPtr->SetLastError(
2332 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2333 "SetSendTelephoneEventPayloadType() failed to register send"
2334 "payload type");
2335 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002336 }
kwiberg55b97fe2016-01-28 05:22:45 -08002337 }
kwiberg55b97fe2016-01-28 05:22:45 -08002338 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002339}
2340
kwiberg55b97fe2016-01-28 05:22:45 -08002341int Channel::VoiceActivityIndicator(int& activity) {
2342 activity = _sendFrameType;
2343 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002344}
2345
kwiberg55b97fe2016-01-28 05:22:45 -08002346int Channel::SetLocalSSRC(unsigned int ssrc) {
2347 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2348 "Channel::SetLocalSSRC()");
2349 if (channel_state_.Get().sending) {
2350 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2351 "SetLocalSSRC() already sending");
2352 return -1;
2353 }
2354 _rtpRtcpModule->SetSSRC(ssrc);
2355 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002356}
2357
kwiberg55b97fe2016-01-28 05:22:45 -08002358int Channel::GetLocalSSRC(unsigned int& ssrc) {
2359 ssrc = _rtpRtcpModule->SSRC();
2360 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002361}
2362
kwiberg55b97fe2016-01-28 05:22:45 -08002363int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2364 ssrc = rtp_receiver_->SSRC();
2365 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002366}
2367
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002368int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002369 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002370 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002371}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002372
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002373int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2374 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002375 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2376 if (enable &&
2377 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2378 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002379 return -1;
2380 }
2381 return 0;
2382}
2383
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002384void Channel::EnableSendTransportSequenceNumber(int id) {
2385 int ret =
2386 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2387 RTC_DCHECK_EQ(0, ret);
2388}
2389
stefan3313ec92016-01-21 06:32:43 -08002390void Channel::EnableReceiveTransportSequenceNumber(int id) {
2391 rtp_header_parser_->DeregisterRtpHeaderExtension(
2392 kRtpExtensionTransportSequenceNumber);
2393 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2394 kRtpExtensionTransportSequenceNumber, id);
2395 RTC_DCHECK(ret);
2396}
2397
stefanbba9dec2016-02-01 04:39:55 -08002398void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002399 RtpPacketSender* rtp_packet_sender,
2400 TransportFeedbackObserver* transport_feedback_observer,
2401 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002402 RTC_DCHECK(rtp_packet_sender);
2403 RTC_DCHECK(transport_feedback_observer);
2404 RTC_DCHECK(packet_router && !packet_router_);
2405 feedback_observer_proxy_->SetTransportFeedbackObserver(
2406 transport_feedback_observer);
2407 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2408 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2409 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002410 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002411 packet_router_ = packet_router;
2412}
2413
stefanbba9dec2016-02-01 04:39:55 -08002414void Channel::RegisterReceiverCongestionControlObjects(
2415 PacketRouter* packet_router) {
2416 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002417 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002418 packet_router_ = packet_router;
2419}
2420
2421void Channel::ResetCongestionControlObjects() {
2422 RTC_DCHECK(packet_router_);
2423 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2424 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2425 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002426 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002427 packet_router_ = nullptr;
2428 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2429}
2430
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002431void Channel::SetRTCPStatus(bool enable) {
2432 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2433 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002434 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002435}
2436
kwiberg55b97fe2016-01-28 05:22:45 -08002437int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002438 RtcpMode method = _rtpRtcpModule->RTCP();
2439 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002440 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002441}
2442
kwiberg55b97fe2016-01-28 05:22:45 -08002443int Channel::SetRTCP_CNAME(const char cName[256]) {
2444 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2445 "Channel::SetRTCP_CNAME()");
2446 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2447 _engineStatisticsPtr->SetLastError(
2448 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2449 "SetRTCP_CNAME() failed to set RTCP CNAME");
2450 return -1;
2451 }
2452 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002453}
2454
kwiberg55b97fe2016-01-28 05:22:45 -08002455int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2456 if (cName == NULL) {
2457 _engineStatisticsPtr->SetLastError(
2458 VE_INVALID_ARGUMENT, kTraceError,
2459 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2460 return -1;
2461 }
2462 char cname[RTCP_CNAME_SIZE];
2463 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2464 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2465 _engineStatisticsPtr->SetLastError(
2466 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2467 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2468 return -1;
2469 }
2470 strcpy(cName, cname);
2471 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002472}
2473
kwiberg55b97fe2016-01-28 05:22:45 -08002474int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2475 unsigned int& NTPLow,
2476 unsigned int& timestamp,
2477 unsigned int& playoutTimestamp,
2478 unsigned int* jitter,
2479 unsigned short* fractionLost) {
2480 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002481
kwiberg55b97fe2016-01-28 05:22:45 -08002482 RTCPSenderInfo senderInfo;
2483 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2484 _engineStatisticsPtr->SetLastError(
2485 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2486 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2487 "side");
2488 return -1;
2489 }
2490
2491 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2492 // and octet count)
2493 NTPHigh = senderInfo.NTPseconds;
2494 NTPLow = senderInfo.NTPfraction;
2495 timestamp = senderInfo.RTPtimeStamp;
2496
2497 // --- Locally derived information
2498
2499 // This value is updated on each incoming RTCP packet (0 when no packet
2500 // has been received)
2501 playoutTimestamp = playout_timestamp_rtcp_;
2502
2503 if (NULL != jitter || NULL != fractionLost) {
2504 // Get all RTCP receiver report blocks that have been received on this
2505 // channel. If we receive RTP packets from a remote source we know the
2506 // remote SSRC and use the report block from him.
2507 // Otherwise use the first report block.
2508 std::vector<RTCPReportBlock> remote_stats;
2509 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2510 remote_stats.empty()) {
2511 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2512 "GetRemoteRTCPData() failed to measure statistics due"
2513 " to lack of received RTP and/or RTCP packets");
2514 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002515 }
2516
kwiberg55b97fe2016-01-28 05:22:45 -08002517 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2518 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2519 for (; it != remote_stats.end(); ++it) {
2520 if (it->remoteSSRC == remoteSSRC)
2521 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002522 }
kwiberg55b97fe2016-01-28 05:22:45 -08002523
2524 if (it == remote_stats.end()) {
2525 // If we have not received any RTCP packets from this SSRC it probably
2526 // means that we have not received any RTP packets.
2527 // Use the first received report block instead.
2528 it = remote_stats.begin();
2529 remoteSSRC = it->remoteSSRC;
2530 }
2531
2532 if (jitter) {
2533 *jitter = it->jitter;
2534 }
2535
2536 if (fractionLost) {
2537 *fractionLost = it->fractionLost;
2538 }
2539 }
2540 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002541}
2542
kwiberg55b97fe2016-01-28 05:22:45 -08002543int Channel::SendApplicationDefinedRTCPPacket(
2544 unsigned char subType,
2545 unsigned int name,
2546 const char* data,
2547 unsigned short dataLengthInBytes) {
2548 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2549 "Channel::SendApplicationDefinedRTCPPacket()");
2550 if (!channel_state_.Get().sending) {
2551 _engineStatisticsPtr->SetLastError(
2552 VE_NOT_SENDING, kTraceError,
2553 "SendApplicationDefinedRTCPPacket() not sending");
2554 return -1;
2555 }
2556 if (NULL == data) {
2557 _engineStatisticsPtr->SetLastError(
2558 VE_INVALID_ARGUMENT, kTraceError,
2559 "SendApplicationDefinedRTCPPacket() invalid data value");
2560 return -1;
2561 }
2562 if (dataLengthInBytes % 4 != 0) {
2563 _engineStatisticsPtr->SetLastError(
2564 VE_INVALID_ARGUMENT, kTraceError,
2565 "SendApplicationDefinedRTCPPacket() invalid length value");
2566 return -1;
2567 }
2568 RtcpMode status = _rtpRtcpModule->RTCP();
2569 if (status == RtcpMode::kOff) {
2570 _engineStatisticsPtr->SetLastError(
2571 VE_RTCP_ERROR, kTraceError,
2572 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2573 return -1;
2574 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002575
kwiberg55b97fe2016-01-28 05:22:45 -08002576 // Create and schedule the RTCP APP packet for transmission
2577 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2578 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2579 _engineStatisticsPtr->SetLastError(
2580 VE_SEND_ERROR, kTraceError,
2581 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2582 return -1;
2583 }
2584 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002585}
2586
kwiberg55b97fe2016-01-28 05:22:45 -08002587int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2588 unsigned int& maxJitterMs,
2589 unsigned int& discardedPackets) {
2590 // The jitter statistics is updated for each received RTP packet and is
2591 // based on received packets.
2592 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2593 // If RTCP is off, there is no timed thread in the RTCP module regularly
2594 // generating new stats, trigger the update manually here instead.
2595 StreamStatistician* statistician =
2596 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2597 if (statistician) {
2598 // Don't use returned statistics, use data from proxy instead so that
2599 // max jitter can be fetched atomically.
2600 RtcpStatistics s;
2601 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002602 }
kwiberg55b97fe2016-01-28 05:22:45 -08002603 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002604
kwiberg55b97fe2016-01-28 05:22:45 -08002605 ChannelStatistics stats = statistics_proxy_->GetStats();
2606 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2607 if (playoutFrequency > 0) {
2608 // Scale RTP statistics given the current playout frequency
2609 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2610 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2611 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002612
kwiberg55b97fe2016-01-28 05:22:45 -08002613 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002614
kwiberg55b97fe2016-01-28 05:22:45 -08002615 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002616}
2617
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002618int Channel::GetRemoteRTCPReportBlocks(
2619 std::vector<ReportBlock>* report_blocks) {
2620 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002621 _engineStatisticsPtr->SetLastError(
2622 VE_INVALID_ARGUMENT, kTraceError,
2623 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002624 return -1;
2625 }
2626
2627 // Get the report blocks from the latest received RTCP Sender or Receiver
2628 // Report. Each element in the vector contains the sender's SSRC and a
2629 // report block according to RFC 3550.
2630 std::vector<RTCPReportBlock> rtcp_report_blocks;
2631 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002632 return -1;
2633 }
2634
2635 if (rtcp_report_blocks.empty())
2636 return 0;
2637
2638 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2639 for (; it != rtcp_report_blocks.end(); ++it) {
2640 ReportBlock report_block;
2641 report_block.sender_SSRC = it->remoteSSRC;
2642 report_block.source_SSRC = it->sourceSSRC;
2643 report_block.fraction_lost = it->fractionLost;
2644 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2645 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2646 report_block.interarrival_jitter = it->jitter;
2647 report_block.last_SR_timestamp = it->lastSR;
2648 report_block.delay_since_last_SR = it->delaySinceLastSR;
2649 report_blocks->push_back(report_block);
2650 }
2651 return 0;
2652}
2653
kwiberg55b97fe2016-01-28 05:22:45 -08002654int Channel::GetRTPStatistics(CallStatistics& stats) {
2655 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002656
kwiberg55b97fe2016-01-28 05:22:45 -08002657 // The jitter statistics is updated for each received RTP packet and is
2658 // based on received packets.
2659 RtcpStatistics statistics;
2660 StreamStatistician* statistician =
2661 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002662 if (statistician) {
2663 statistician->GetStatistics(&statistics,
2664 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002665 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002666
kwiberg55b97fe2016-01-28 05:22:45 -08002667 stats.fractionLost = statistics.fraction_lost;
2668 stats.cumulativeLost = statistics.cumulative_lost;
2669 stats.extendedMax = statistics.extended_max_sequence_number;
2670 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002671
kwiberg55b97fe2016-01-28 05:22:45 -08002672 // --- RTT
2673 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002674
kwiberg55b97fe2016-01-28 05:22:45 -08002675 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002676
kwiberg55b97fe2016-01-28 05:22:45 -08002677 size_t bytesSent(0);
2678 uint32_t packetsSent(0);
2679 size_t bytesReceived(0);
2680 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002681
kwiberg55b97fe2016-01-28 05:22:45 -08002682 if (statistician) {
2683 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2684 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002685
kwiberg55b97fe2016-01-28 05:22:45 -08002686 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2687 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2688 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2689 " output will not be complete");
2690 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002691
kwiberg55b97fe2016-01-28 05:22:45 -08002692 stats.bytesSent = bytesSent;
2693 stats.packetsSent = packetsSent;
2694 stats.bytesReceived = bytesReceived;
2695 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002696
kwiberg55b97fe2016-01-28 05:22:45 -08002697 // --- Timestamps
2698 {
2699 rtc::CritScope lock(&ts_stats_lock_);
2700 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2701 }
2702 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002703}
2704
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002705int Channel::SetCodecFECStatus(bool enable) {
2706 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2707 "Channel::SetCodecFECStatus()");
2708
kwibergc8d071e2016-04-06 12:22:38 -07002709 if (!codec_manager_.SetCodecFEC(enable) ||
2710 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002711 _engineStatisticsPtr->SetLastError(
2712 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2713 "SetCodecFECStatus() failed to set FEC state");
2714 return -1;
2715 }
2716 return 0;
2717}
2718
2719bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002720 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002721}
2722
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002723void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2724 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002725 // If pacing is enabled we always store packets.
2726 if (!pacing_enabled_)
2727 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002728 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002729 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002730 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002731 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002732 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002733}
2734
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002735// Called when we are missing one or more packets.
2736int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002737 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2738}
2739
kwiberg55b97fe2016-01-28 05:22:45 -08002740uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2741 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2742 "Channel::Demultiplex()");
2743 _audioFrame.CopyFrom(audioFrame);
2744 _audioFrame.id_ = _channelId;
2745 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002746}
2747
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002748void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002749 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002750 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002751 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002752 CodecInst codec;
2753 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002754
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002755 // Never upsample or upmix the capture signal here. This should be done at the
2756 // end of the send chain.
2757 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2758 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2759 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2760 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002761}
2762
kwiberg55b97fe2016-01-28 05:22:45 -08002763uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2764 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2765 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002766
kwiberg55b97fe2016-01-28 05:22:45 -08002767 if (_audioFrame.samples_per_channel_ == 0) {
2768 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2769 "Channel::PrepareEncodeAndSend() invalid audio frame");
2770 return 0xFFFFFFFF;
2771 }
2772
2773 if (channel_state_.Get().input_file_playing) {
2774 MixOrReplaceAudioWithFile(mixingFrequency);
2775 }
2776
solenberg1c2af8e2016-03-24 10:36:00 -07002777 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2778 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002779
2780 if (channel_state_.Get().input_external_media) {
2781 rtc::CritScope cs(&_callbackCritSect);
2782 const bool isStereo = (_audioFrame.num_channels_ == 2);
2783 if (_inputExternalMediaCallbackPtr) {
2784 _inputExternalMediaCallbackPtr->Process(
2785 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
2786 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
2787 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00002788 }
kwiberg55b97fe2016-01-28 05:22:45 -08002789 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002790
kwiberg55b97fe2016-01-28 05:22:45 -08002791 if (_includeAudioLevelIndication) {
2792 size_t length =
2793 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002794 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002795 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002796 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002797 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002798 rms_level_.Analyze(
2799 rtc::ArrayView<const int16_t>(_audioFrame.data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002800 }
kwiberg55b97fe2016-01-28 05:22:45 -08002801 }
solenberg1c2af8e2016-03-24 10:36:00 -07002802 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002803
kwiberg55b97fe2016-01-28 05:22:45 -08002804 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002805}
2806
kwiberg55b97fe2016-01-28 05:22:45 -08002807uint32_t Channel::EncodeAndSend() {
2808 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2809 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002810
kwiberg55b97fe2016-01-28 05:22:45 -08002811 assert(_audioFrame.num_channels_ <= 2);
2812 if (_audioFrame.samples_per_channel_ == 0) {
2813 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2814 "Channel::EncodeAndSend() invalid audio frame");
2815 return 0xFFFFFFFF;
2816 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002817
kwiberg55b97fe2016-01-28 05:22:45 -08002818 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002819
kwiberg55b97fe2016-01-28 05:22:45 -08002820 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002821
kwiberg55b97fe2016-01-28 05:22:45 -08002822 // The ACM resamples internally.
2823 _audioFrame.timestamp_ = _timeStamp;
2824 // This call will trigger AudioPacketizationCallback::SendData if encoding
2825 // is done and payload is ready for packetization and transmission.
2826 // Otherwise, it will return without invoking the callback.
2827 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2828 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2829 "Channel::EncodeAndSend() ACM encoding failed");
2830 return 0xFFFFFFFF;
2831 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002832
kwiberg55b97fe2016-01-28 05:22:45 -08002833 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2834 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002835}
2836
solenberg7602aab2016-11-14 11:30:07 -08002837void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2838 RTC_DCHECK(!channel.channel() ||
2839 channel.channel()->ChannelId() != _channelId);
2840 rtc::CritScope lock(&assoc_send_channel_lock_);
2841 associate_send_channel_ = channel;
2842}
2843
Minyue2013aec2015-05-13 14:14:42 +02002844void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002845 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002846 Channel* channel = associate_send_channel_.channel();
2847 if (channel && channel->ChannelId() == channel_id) {
2848 // If this channel is associated with a send channel of the specified
2849 // Channel ID, disassociate with it.
2850 ChannelOwner ref(NULL);
2851 associate_send_channel_ = ref;
2852 }
2853}
2854
ivoc14d5dbe2016-07-04 07:06:55 -07002855void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2856 event_log_proxy_->SetEventLog(event_log);
2857}
2858
michaelt9332b7d2016-11-30 07:51:13 -08002859void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2860 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2861}
2862
nisse284542b2017-01-10 08:58:32 -08002863void Channel::UpdateOverheadForEncoder() {
2864 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2865 if (*encoder) {
2866 (*encoder)->OnReceivedOverhead(transport_overhead_per_packet_ +
2867 rtp_overhead_per_packet_);
2868 }
2869 });
2870}
2871
2872void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
2873 transport_overhead_per_packet_ = transport_overhead_per_packet;
2874 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002875}
2876
michaeltbf65be52016-12-15 06:24:49 -08002877void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
nisse284542b2017-01-10 08:58:32 -08002878 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2879 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002880}
2881
kwiberg55b97fe2016-01-28 05:22:45 -08002882int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
2883 VoEMediaProcess& processObject) {
2884 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2885 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002886
kwiberg55b97fe2016-01-28 05:22:45 -08002887 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002888
kwiberg55b97fe2016-01-28 05:22:45 -08002889 if (kPlaybackPerChannel == type) {
2890 if (_outputExternalMediaCallbackPtr) {
2891 _engineStatisticsPtr->SetLastError(
2892 VE_INVALID_OPERATION, kTraceError,
2893 "Channel::RegisterExternalMediaProcessing() "
2894 "output external media already enabled");
2895 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002896 }
kwiberg55b97fe2016-01-28 05:22:45 -08002897 _outputExternalMediaCallbackPtr = &processObject;
2898 _outputExternalMedia = true;
2899 } else if (kRecordingPerChannel == type) {
2900 if (_inputExternalMediaCallbackPtr) {
2901 _engineStatisticsPtr->SetLastError(
2902 VE_INVALID_OPERATION, kTraceError,
2903 "Channel::RegisterExternalMediaProcessing() "
2904 "output external media already enabled");
2905 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002906 }
kwiberg55b97fe2016-01-28 05:22:45 -08002907 _inputExternalMediaCallbackPtr = &processObject;
2908 channel_state_.SetInputExternalMedia(true);
2909 }
2910 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002911}
2912
kwiberg55b97fe2016-01-28 05:22:45 -08002913int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
2914 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2915 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002916
kwiberg55b97fe2016-01-28 05:22:45 -08002917 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002918
kwiberg55b97fe2016-01-28 05:22:45 -08002919 if (kPlaybackPerChannel == type) {
2920 if (!_outputExternalMediaCallbackPtr) {
2921 _engineStatisticsPtr->SetLastError(
2922 VE_INVALID_OPERATION, kTraceWarning,
2923 "Channel::DeRegisterExternalMediaProcessing() "
2924 "output external media already disabled");
2925 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002926 }
kwiberg55b97fe2016-01-28 05:22:45 -08002927 _outputExternalMedia = false;
2928 _outputExternalMediaCallbackPtr = NULL;
2929 } else if (kRecordingPerChannel == type) {
2930 if (!_inputExternalMediaCallbackPtr) {
2931 _engineStatisticsPtr->SetLastError(
2932 VE_INVALID_OPERATION, kTraceWarning,
2933 "Channel::DeRegisterExternalMediaProcessing() "
2934 "input external media already disabled");
2935 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002936 }
kwiberg55b97fe2016-01-28 05:22:45 -08002937 channel_state_.SetInputExternalMedia(false);
2938 _inputExternalMediaCallbackPtr = NULL;
2939 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002940
kwiberg55b97fe2016-01-28 05:22:45 -08002941 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002942}
2943
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002944int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08002945 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2946 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002947
kwiberg55b97fe2016-01-28 05:22:45 -08002948 if (channel_state_.Get().playing) {
2949 _engineStatisticsPtr->SetLastError(
2950 VE_INVALID_OPERATION, kTraceError,
2951 "Channel::SetExternalMixing() "
2952 "external mixing cannot be changed while playing.");
2953 return -1;
2954 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002955
kwiberg55b97fe2016-01-28 05:22:45 -08002956 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002957
kwiberg55b97fe2016-01-28 05:22:45 -08002958 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002959}
2960
kwiberg55b97fe2016-01-28 05:22:45 -08002961int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2962 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002963}
2964
wu@webrtc.org24301a62013-12-13 19:17:43 +00002965void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2966 audio_coding_->GetDecodingCallStatistics(stats);
2967}
2968
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002969bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
2970 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08002971 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07002972 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002973 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002974 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002975}
2976
solenberg358057b2015-11-27 10:46:42 -08002977uint32_t Channel::GetDelayEstimate() const {
2978 int jitter_buffer_delay_ms = 0;
2979 int playout_buffer_delay_ms = 0;
2980 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
2981 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
2982}
2983
deadbeef74375882015-08-13 12:09:10 -07002984int Channel::LeastRequiredDelayMs() const {
2985 return audio_coding_->LeastRequiredDelayMs();
2986}
2987
kwiberg55b97fe2016-01-28 05:22:45 -08002988int Channel::SetMinimumPlayoutDelay(int delayMs) {
2989 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2990 "Channel::SetMinimumPlayoutDelay()");
2991 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2992 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2993 _engineStatisticsPtr->SetLastError(
2994 VE_INVALID_ARGUMENT, kTraceError,
2995 "SetMinimumPlayoutDelay() invalid min delay");
2996 return -1;
2997 }
2998 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2999 _engineStatisticsPtr->SetLastError(
3000 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3001 "SetMinimumPlayoutDelay() failed to set min playout delay");
3002 return -1;
3003 }
3004 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003005}
3006
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003007int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07003008 uint32_t playout_timestamp_rtp = 0;
3009 {
tommi31fc21f2016-01-21 10:37:37 -08003010 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003011 playout_timestamp_rtp = playout_timestamp_rtp_;
3012 }
kwiberg55b97fe2016-01-28 05:22:45 -08003013 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003014 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07003015 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003016 "GetPlayoutTimestamp() failed to retrieve timestamp");
3017 return -1;
3018 }
deadbeef74375882015-08-13 12:09:10 -07003019 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003020 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003021}
3022
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003023int Channel::SetInitTimestamp(unsigned int timestamp) {
3024 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003025 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003026 if (channel_state_.Get().sending) {
3027 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3028 "SetInitTimestamp() already sending");
3029 return -1;
3030 }
3031 _rtpRtcpModule->SetStartTimestamp(timestamp);
3032 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003033}
3034
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003035int Channel::SetInitSequenceNumber(short sequenceNumber) {
3036 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3037 "Channel::SetInitSequenceNumber()");
3038 if (channel_state_.Get().sending) {
3039 _engineStatisticsPtr->SetLastError(
3040 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3041 return -1;
3042 }
3043 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3044 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003045}
3046
kwiberg55b97fe2016-01-28 05:22:45 -08003047int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
3048 RtpReceiver** rtp_receiver) const {
3049 *rtpRtcpModule = _rtpRtcpModule.get();
3050 *rtp_receiver = rtp_receiver_.get();
3051 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003052}
3053
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003054// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3055// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08003056int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08003057 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08003058 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003059
kwiberg55b97fe2016-01-28 05:22:45 -08003060 {
3061 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003062
kwiberg5a25d952016-08-17 07:31:12 -07003063 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003064 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3065 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3066 " doesnt exist");
3067 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003068 }
3069
kwiberg4ec01d92016-08-22 08:43:54 -07003070 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07003071 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003072 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3073 "Channel::MixOrReplaceAudioWithFile() file mixing "
3074 "failed");
3075 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003076 }
kwiberg55b97fe2016-01-28 05:22:45 -08003077 if (fileSamples == 0) {
3078 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3079 "Channel::MixOrReplaceAudioWithFile() file is ended");
3080 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003081 }
kwiberg55b97fe2016-01-28 05:22:45 -08003082 }
3083
3084 assert(_audioFrame.samples_per_channel_ == fileSamples);
3085
3086 if (_mixFileWithMicrophone) {
3087 // Currently file stream is always mono.
3088 // TODO(xians): Change the code when FilePlayer supports real stereo.
3089 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3090 1, fileSamples);
3091 } else {
3092 // Replace ACM audio with file.
3093 // Currently file stream is always mono.
3094 // TODO(xians): Change the code when FilePlayer supports real stereo.
3095 _audioFrame.UpdateFrame(
3096 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3097 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3098 }
3099 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003100}
3101
kwiberg55b97fe2016-01-28 05:22:45 -08003102int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3103 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003104
kwibergb7f89d62016-02-17 10:04:18 -08003105 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003106 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003107
kwiberg55b97fe2016-01-28 05:22:45 -08003108 {
3109 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003110
kwiberg5a25d952016-08-17 07:31:12 -07003111 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003112 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3113 "Channel::MixAudioWithFile() file mixing failed");
3114 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003115 }
3116
kwiberg55b97fe2016-01-28 05:22:45 -08003117 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003118 if (output_file_player_->Get10msAudioFromFile(
3119 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003120 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3121 "Channel::MixAudioWithFile() file mixing failed");
3122 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003123 }
kwiberg55b97fe2016-01-28 05:22:45 -08003124 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003125
kwiberg55b97fe2016-01-28 05:22:45 -08003126 if (audioFrame.samples_per_channel_ == fileSamples) {
3127 // Currently file stream is always mono.
3128 // TODO(xians): Change the code when FilePlayer supports real stereo.
3129 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3130 fileSamples);
3131 } else {
3132 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3133 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3134 ") != "
3135 "fileSamples(%" PRIuS ")",
3136 audioFrame.samples_per_channel_, fileSamples);
3137 return -1;
3138 }
3139
3140 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003141}
3142
deadbeef74375882015-08-13 12:09:10 -07003143void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003144 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003145
henrik.lundin96bd5022016-04-06 04:13:56 -07003146 if (!jitter_buffer_playout_timestamp_) {
3147 // This can happen if this channel has not received any RTP packets. In
3148 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003149 return;
3150 }
3151
3152 uint16_t delay_ms = 0;
3153 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003154 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003155 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3156 " delay from the ADM");
3157 _engineStatisticsPtr->SetLastError(
3158 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3159 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3160 return;
3161 }
3162
henrik.lundin96bd5022016-04-06 04:13:56 -07003163 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3164 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003165
3166 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003167 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003168
kwiberg55b97fe2016-01-28 05:22:45 -08003169 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003170 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003171 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003172
3173 {
tommi31fc21f2016-01-21 10:37:37 -08003174 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003175 if (rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003176 playout_timestamp_rtcp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003177 } else {
henrik.lundin96bd5022016-04-06 04:13:56 -07003178 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003179 }
3180 playout_delay_ms_ = delay_ms;
3181 }
3182}
3183
kwiberg55b97fe2016-01-28 05:22:45 -08003184void Channel::RegisterReceiveCodecsToRTPModule() {
3185 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3186 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003187
kwiberg55b97fe2016-01-28 05:22:45 -08003188 CodecInst codec;
3189 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003190
kwiberg55b97fe2016-01-28 05:22:45 -08003191 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3192 // Open up the RTP/RTCP receiver for all supported codecs
3193 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08003194 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08003195 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3196 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3197 " to register %s (%d/%d/%" PRIuS
3198 "/%d) to RTP/RTCP "
3199 "receiver",
3200 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3201 codec.rate);
3202 } else {
3203 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3204 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3205 "(%d/%d/%" PRIuS
3206 "/%d) has been added to the RTP/RTCP "
3207 "receiver",
3208 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3209 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003210 }
kwiberg55b97fe2016-01-28 05:22:45 -08003211 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003212}
3213
kwiberg55b97fe2016-01-28 05:22:45 -08003214int Channel::SetSendRtpHeaderExtension(bool enable,
3215 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003216 unsigned char id) {
3217 int error = 0;
3218 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3219 if (enable) {
3220 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3221 }
3222 return error;
3223}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003224
ossue280cde2016-10-12 11:04:10 -07003225int Channel::GetRtpTimestampRateHz() const {
3226 const auto format = audio_coding_->ReceiveFormat();
3227 // Default to the playout frequency if we've not gotten any packets yet.
3228 // TODO(ossu): Zero clockrate can only happen if we've added an external
3229 // decoder for a format we don't support internally. Remove once that way of
3230 // adding decoders is gone!
3231 return (format && format->clockrate_hz != 0)
3232 ? format->clockrate_hz
3233 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003234}
3235
Minyue2013aec2015-05-13 14:14:42 +02003236int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003237 RtcpMode method = _rtpRtcpModule->RTCP();
3238 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003239 return 0;
3240 }
3241 std::vector<RTCPReportBlock> report_blocks;
3242 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003243
3244 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003245 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003246 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003247 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003248 Channel* channel = associate_send_channel_.channel();
3249 // Tries to get RTT from an associated channel. This is important for
3250 // receive-only channels.
3251 if (channel) {
3252 // To prevent infinite recursion and deadlock, calling GetRTT of
3253 // associate channel should always use "false" for argument:
3254 // |allow_associate_channel|.
3255 rtt = channel->GetRTT(false);
3256 }
3257 }
3258 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003259 }
3260
3261 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3262 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3263 for (; it != report_blocks.end(); ++it) {
3264 if (it->remoteSSRC == remoteSSRC)
3265 break;
3266 }
3267 if (it == report_blocks.end()) {
3268 // We have not received packets with SSRC matching the report blocks.
3269 // To calculate RTT we try with the SSRC of the first report block.
3270 // This is very important for send-only channels where we don't know
3271 // the SSRC of the other end.
3272 remoteSSRC = report_blocks[0].remoteSSRC;
3273 }
Minyue2013aec2015-05-13 14:14:42 +02003274
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003275 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003276 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003277 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003278 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3279 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003280 return 0;
3281 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003282 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003283}
3284
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003285} // namespace voe
3286} // namespace webrtc