blob: 8f196c6d3148b1838ebdeb8231eed21d88bf09e1 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrika@webrtc.org2919e952012-01-31 08:45:03 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000011#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Henrik Lundin64dad832015-05-11 12:44:23 +020013#include <algorithm>
Tommif888bb52015-12-12 01:37:01 +010014#include <utility>
Henrik Lundin64dad832015-05-11 12:44:23 +020015
Ivo Creusenae856f22015-09-17 16:30:16 +020016#include "webrtc/base/checks.h"
tommi31fc21f2016-01-21 10:37:37 -080017#include "webrtc/base/criticalsection.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000018#include "webrtc/base/format_macros.h"
pbosad856222015-11-27 09:48:36 -080019#include "webrtc/base/logging.h"
Erik Språng737336d2016-07-29 12:59:36 +020020#include "webrtc/base/rate_limiter.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010021#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000022#include "webrtc/base/timeutils.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020023#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070024#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000025#include "webrtc/modules/audio_device/include/audio_device.h"
26#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010027#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010028#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010029#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
30#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
31#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000032#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010033#include "webrtc/modules/utility/include/audio_frame_operations.h"
34#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010035#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000036#include "webrtc/voice_engine/include/voe_external_media.h"
37#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
38#include "webrtc/voice_engine/output_mixer.h"
39#include "webrtc/voice_engine/statistics.h"
40#include "webrtc/voice_engine/transmit_mixer.h"
41#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000042
andrew@webrtc.org50419b02012-11-14 19:07:54 +000043namespace webrtc {
44namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000045
kwibergc8d071e2016-04-06 12:22:38 -070046namespace {
47
Erik Språng737336d2016-07-29 12:59:36 +020048constexpr int64_t kMaxRetransmissionWindowMs = 1000;
49constexpr int64_t kMinRetransmissionWindowMs = 30;
50
kwibergc8d071e2016-04-06 12:22:38 -070051bool RegisterReceiveCodec(std::unique_ptr<AudioCodingModule>* acm,
52 acm2::RentACodec* rac,
53 const CodecInst& ci) {
kwibergabe95ba2016-06-02 02:58:59 -070054 const int result = (*acm)->RegisterReceiveCodec(
55 ci, [&] { return rac->RentIsacDecoder(ci.plfreq); });
kwibergc8d071e2016-04-06 12:22:38 -070056 return result == 0;
57}
58
59} // namespace
60
solenberg8842c3e2016-03-11 03:06:41 -080061const int kTelephoneEventAttenuationdB = 10;
62
ivoc14d5dbe2016-07-04 07:06:55 -070063class RtcEventLogProxy final : public webrtc::RtcEventLog {
64 public:
65 RtcEventLogProxy() : event_log_(nullptr) {}
66
67 bool StartLogging(const std::string& file_name,
68 int64_t max_size_bytes) override {
69 RTC_NOTREACHED();
70 return false;
71 }
72
73 bool StartLogging(rtc::PlatformFile log_file,
74 int64_t max_size_bytes) override {
75 RTC_NOTREACHED();
76 return false;
77 }
78
79 void StopLogging() override { RTC_NOTREACHED(); }
80
81 void LogVideoReceiveStreamConfig(
82 const webrtc::VideoReceiveStream::Config& config) override {
83 rtc::CritScope lock(&crit_);
84 if (event_log_) {
85 event_log_->LogVideoReceiveStreamConfig(config);
86 }
87 }
88
89 void LogVideoSendStreamConfig(
90 const webrtc::VideoSendStream::Config& config) override {
91 rtc::CritScope lock(&crit_);
92 if (event_log_) {
93 event_log_->LogVideoSendStreamConfig(config);
94 }
95 }
96
ivoce0928d82016-10-10 05:12:51 -070097 void LogAudioReceiveStreamConfig(
98 const webrtc::AudioReceiveStream::Config& config) override {
99 rtc::CritScope lock(&crit_);
100 if (event_log_) {
101 event_log_->LogAudioReceiveStreamConfig(config);
102 }
103 }
104
105 void LogAudioSendStreamConfig(
106 const webrtc::AudioSendStream::Config& config) override {
107 rtc::CritScope lock(&crit_);
108 if (event_log_) {
109 event_log_->LogAudioSendStreamConfig(config);
110 }
111 }
112
ivoc14d5dbe2016-07-04 07:06:55 -0700113 void LogRtpHeader(webrtc::PacketDirection direction,
114 webrtc::MediaType media_type,
115 const uint8_t* header,
116 size_t packet_length) override {
117 rtc::CritScope lock(&crit_);
118 if (event_log_) {
119 event_log_->LogRtpHeader(direction, media_type, header, packet_length);
120 }
121 }
122
123 void LogRtcpPacket(webrtc::PacketDirection direction,
124 webrtc::MediaType media_type,
125 const uint8_t* packet,
126 size_t length) override {
127 rtc::CritScope lock(&crit_);
128 if (event_log_) {
129 event_log_->LogRtcpPacket(direction, media_type, packet, length);
130 }
131 }
132
133 void LogAudioPlayout(uint32_t ssrc) override {
134 rtc::CritScope lock(&crit_);
135 if (event_log_) {
136 event_log_->LogAudioPlayout(ssrc);
137 }
138 }
139
140 void LogBwePacketLossEvent(int32_t bitrate,
141 uint8_t fraction_loss,
142 int32_t total_packets) override {
143 rtc::CritScope lock(&crit_);
144 if (event_log_) {
145 event_log_->LogBwePacketLossEvent(bitrate, fraction_loss, total_packets);
146 }
147 }
148
149 void SetEventLog(RtcEventLog* event_log) {
150 rtc::CritScope lock(&crit_);
151 event_log_ = event_log;
152 }
153
154 private:
155 rtc::CriticalSection crit_;
156 RtcEventLog* event_log_ GUARDED_BY(crit_);
157 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
158};
159
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100160class TransportFeedbackProxy : public TransportFeedbackObserver {
161 public:
162 TransportFeedbackProxy() : feedback_observer_(nullptr) {
163 pacer_thread_.DetachFromThread();
164 network_thread_.DetachFromThread();
165 }
166
167 void SetTransportFeedbackObserver(
168 TransportFeedbackObserver* feedback_observer) {
169 RTC_DCHECK(thread_checker_.CalledOnValidThread());
170 rtc::CritScope lock(&crit_);
171 feedback_observer_ = feedback_observer;
172 }
173
174 // Implements TransportFeedbackObserver.
175 void AddPacket(uint16_t sequence_number,
176 size_t length,
philipela1ed0b32016-06-01 06:31:17 -0700177 int probe_cluster_id) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100178 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
179 rtc::CritScope lock(&crit_);
180 if (feedback_observer_)
pbos2169d8b2016-06-20 11:53:02 -0700181 feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100182 }
183 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
184 RTC_DCHECK(network_thread_.CalledOnValidThread());
185 rtc::CritScope lock(&crit_);
Stefan Holmer60e43462016-09-07 09:58:20 +0200186 feedback_observer_->OnTransportFeedback(feedback);
187 }
188 std::vector<PacketInfo> GetTransportFeedbackVector() const override {
189 RTC_NOTREACHED();
190 return std::vector<PacketInfo>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100191 }
192
193 private:
194 rtc::CriticalSection crit_;
195 rtc::ThreadChecker thread_checker_;
196 rtc::ThreadChecker pacer_thread_;
197 rtc::ThreadChecker network_thread_;
198 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
199};
200
201class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
202 public:
203 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
204 pacer_thread_.DetachFromThread();
205 }
206
207 void SetSequenceNumberAllocator(
208 TransportSequenceNumberAllocator* seq_num_allocator) {
209 RTC_DCHECK(thread_checker_.CalledOnValidThread());
210 rtc::CritScope lock(&crit_);
211 seq_num_allocator_ = seq_num_allocator;
212 }
213
214 // Implements TransportSequenceNumberAllocator.
215 uint16_t AllocateSequenceNumber() override {
216 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
217 rtc::CritScope lock(&crit_);
218 if (!seq_num_allocator_)
219 return 0;
220 return seq_num_allocator_->AllocateSequenceNumber();
221 }
222
223 private:
224 rtc::CriticalSection crit_;
225 rtc::ThreadChecker thread_checker_;
226 rtc::ThreadChecker pacer_thread_;
227 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
228};
229
230class RtpPacketSenderProxy : public RtpPacketSender {
231 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800232 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100233
234 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
235 RTC_DCHECK(thread_checker_.CalledOnValidThread());
236 rtc::CritScope lock(&crit_);
237 rtp_packet_sender_ = rtp_packet_sender;
238 }
239
240 // Implements RtpPacketSender.
241 void InsertPacket(Priority priority,
242 uint32_t ssrc,
243 uint16_t sequence_number,
244 int64_t capture_time_ms,
245 size_t bytes,
246 bool retransmission) override {
247 rtc::CritScope lock(&crit_);
248 if (rtp_packet_sender_) {
249 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
250 capture_time_ms, bytes, retransmission);
251 }
252 }
253
254 private:
255 rtc::ThreadChecker thread_checker_;
256 rtc::CriticalSection crit_;
257 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
258};
259
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000260// Extend the default RTCP statistics struct with max_jitter, defined as the
261// maximum jitter value seen in an RTCP report block.
262struct ChannelStatistics : public RtcpStatistics {
263 ChannelStatistics() : rtcp(), max_jitter(0) {}
264
265 RtcpStatistics rtcp;
266 uint32_t max_jitter;
267};
268
269// Statistics callback, called at each generation of a new RTCP report block.
270class StatisticsProxy : public RtcpStatisticsCallback {
271 public:
tommi31fc21f2016-01-21 10:37:37 -0800272 StatisticsProxy(uint32_t ssrc) : ssrc_(ssrc) {}
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000273 virtual ~StatisticsProxy() {}
274
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000275 void StatisticsUpdated(const RtcpStatistics& statistics,
276 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000277 if (ssrc != ssrc_)
278 return;
279
tommi31fc21f2016-01-21 10:37:37 -0800280 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000281 stats_.rtcp = statistics;
282 if (statistics.jitter > stats_.max_jitter) {
283 stats_.max_jitter = statistics.jitter;
284 }
285 }
286
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000287 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000288
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000289 ChannelStatistics GetStats() {
tommi31fc21f2016-01-21 10:37:37 -0800290 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000291 return stats_;
292 }
293
294 private:
295 // StatisticsUpdated calls are triggered from threads in the RTP module,
296 // while GetStats calls can be triggered from the public voice engine API,
297 // hence synchronization is needed.
tommi31fc21f2016-01-21 10:37:37 -0800298 rtc::CriticalSection stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000299 const uint32_t ssrc_;
300 ChannelStatistics stats_;
301};
302
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000303class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000304 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000305 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
306 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000307
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000308 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
309 // Not used for Voice Engine.
310 }
311
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000312 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
313 int64_t rtt,
314 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000315 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
316 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
317 // report for VoiceEngine?
318 if (report_blocks.empty())
319 return;
320
321 int fraction_lost_aggregate = 0;
322 int total_number_of_packets = 0;
323
324 // If receiving multiple report blocks, calculate the weighted average based
325 // on the number of packets a report refers to.
326 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
327 block_it != report_blocks.end(); ++block_it) {
328 // Find the previous extended high sequence number for this remote SSRC,
329 // to calculate the number of RTP packets this report refers to. Ignore if
330 // we haven't seen this SSRC before.
331 std::map<uint32_t, uint32_t>::iterator seq_num_it =
332 extended_max_sequence_number_.find(block_it->sourceSSRC);
333 int number_of_packets = 0;
334 if (seq_num_it != extended_max_sequence_number_.end()) {
335 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
336 }
337 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
338 total_number_of_packets += number_of_packets;
339
340 extended_max_sequence_number_[block_it->sourceSSRC] =
341 block_it->extendedHighSeqNum;
342 }
343 int weighted_fraction_lost = 0;
344 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800345 weighted_fraction_lost =
346 (fraction_lost_aggregate + total_number_of_packets / 2) /
347 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000348 }
349 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000350 }
351
352 private:
353 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000354 // Maps remote side ssrc to extended highest sequence number received.
355 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000356};
357
kwiberg55b97fe2016-01-28 05:22:45 -0800358int32_t Channel::SendData(FrameType frameType,
359 uint8_t payloadType,
360 uint32_t timeStamp,
361 const uint8_t* payloadData,
362 size_t payloadSize,
363 const RTPFragmentationHeader* fragmentation) {
364 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
365 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
366 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
367 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000368
kwiberg55b97fe2016-01-28 05:22:45 -0800369 if (_includeAudioLevelIndication) {
370 // Store current audio level in the RTP/RTCP module.
371 // The level will be used in combination with voice-activity state
372 // (frameType) to add an RTP header extension
373 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
374 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000375
kwiberg55b97fe2016-01-28 05:22:45 -0800376 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
377 // packetization.
378 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700379 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800380 (FrameType&)frameType, payloadType, timeStamp,
381 // Leaving the time when this frame was
382 // received from the capture device as
383 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700384 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800385 _engineStatisticsPtr->SetLastError(
386 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
387 "Channel::SendData() failed to send data to RTP/RTCP module");
388 return -1;
389 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000390
kwiberg55b97fe2016-01-28 05:22:45 -0800391 _lastLocalTimeStamp = timeStamp;
392 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000393
kwiberg55b97fe2016-01-28 05:22:45 -0800394 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000395}
396
kwiberg55b97fe2016-01-28 05:22:45 -0800397int32_t Channel::InFrameType(FrameType frame_type) {
398 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
399 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000400
kwiberg55b97fe2016-01-28 05:22:45 -0800401 rtc::CritScope cs(&_callbackCritSect);
402 _sendFrameType = (frame_type == kAudioFrameSpeech);
403 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000404}
405
stefan1d8a5062015-10-02 03:39:33 -0700406bool Channel::SendRtp(const uint8_t* data,
407 size_t len,
408 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800409 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
410 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000411
kwiberg55b97fe2016-01-28 05:22:45 -0800412 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000413
kwiberg55b97fe2016-01-28 05:22:45 -0800414 if (_transportPtr == NULL) {
415 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
416 "Channel::SendPacket() failed to send RTP packet due to"
417 " invalid transport object");
418 return false;
419 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000420
kwiberg55b97fe2016-01-28 05:22:45 -0800421 uint8_t* bufferToSendPtr = (uint8_t*)data;
422 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000423
kwiberg55b97fe2016-01-28 05:22:45 -0800424 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
425 std::string transport_name =
426 _externalTransport ? "external transport" : "WebRtc sockets";
427 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
428 "Channel::SendPacket() RTP transmission using %s failed",
429 transport_name.c_str());
430 return false;
431 }
432 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000433}
434
kwiberg55b97fe2016-01-28 05:22:45 -0800435bool Channel::SendRtcp(const uint8_t* data, size_t len) {
436 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
437 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000438
kwiberg55b97fe2016-01-28 05:22:45 -0800439 rtc::CritScope cs(&_callbackCritSect);
440 if (_transportPtr == NULL) {
441 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
442 "Channel::SendRtcp() failed to send RTCP packet"
443 " due to invalid transport object");
444 return false;
445 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000446
kwiberg55b97fe2016-01-28 05:22:45 -0800447 uint8_t* bufferToSendPtr = (uint8_t*)data;
448 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000449
kwiberg55b97fe2016-01-28 05:22:45 -0800450 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
451 if (n < 0) {
452 std::string transport_name =
453 _externalTransport ? "external transport" : "WebRtc sockets";
454 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
455 "Channel::SendRtcp() transmission using %s failed",
456 transport_name.c_str());
457 return false;
458 }
459 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000460}
461
kwiberg55b97fe2016-01-28 05:22:45 -0800462void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
463 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
464 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000465
kwiberg55b97fe2016-01-28 05:22:45 -0800466 // Update ssrc so that NTP for AV sync can be updated.
467 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000468}
469
Peter Boströmac547a62015-09-17 23:03:57 +0200470void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
471 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
472 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
473 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000474}
475
Peter Boströmac547a62015-09-17 23:03:57 +0200476int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000477 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000478 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000479 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800480 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200481 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800482 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
483 "Channel::OnInitializeDecoder(payloadType=%d, "
484 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
485 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000486
kwiberg55b97fe2016-01-28 05:22:45 -0800487 CodecInst receiveCodec = {0};
488 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000489
kwiberg55b97fe2016-01-28 05:22:45 -0800490 receiveCodec.pltype = payloadType;
491 receiveCodec.plfreq = frequency;
492 receiveCodec.channels = channels;
493 receiveCodec.rate = rate;
494 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000495
kwiberg55b97fe2016-01-28 05:22:45 -0800496 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
497 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000498
kwiberg55b97fe2016-01-28 05:22:45 -0800499 // Register the new codec to the ACM
kwibergc8d071e2016-04-06 12:22:38 -0700500 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, receiveCodec)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800501 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
502 "Channel::OnInitializeDecoder() invalid codec ("
503 "pt=%d, name=%s) received - 1",
504 payloadType, payloadName);
505 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
506 return -1;
507 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000508
kwiberg55b97fe2016-01-28 05:22:45 -0800509 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000510}
511
kwiberg55b97fe2016-01-28 05:22:45 -0800512int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
513 size_t payloadSize,
514 const WebRtcRTPHeader* rtpHeader) {
515 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
516 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
517 ","
518 " payloadType=%u, audioChannel=%" PRIuS ")",
519 payloadSize, rtpHeader->header.payloadType,
520 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000521
kwiberg55b97fe2016-01-28 05:22:45 -0800522 if (!channel_state_.Get().playing) {
523 // Avoid inserting into NetEQ when we are not playing. Count the
524 // packet as discarded.
525 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
526 "received packet is discarded since playing is not"
527 " activated");
528 _numberOfDiscardedPackets++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000529 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800530 }
531
532 // Push the incoming payload (parsed and ready for decoding) into the ACM
533 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
534 0) {
535 _engineStatisticsPtr->SetLastError(
536 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
537 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
538 return -1;
539 }
540
kwiberg55b97fe2016-01-28 05:22:45 -0800541 int64_t round_trip_time = 0;
542 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
543 NULL);
544
545 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
546 if (!nack_list.empty()) {
547 // Can't use nack_list.data() since it's not supported by all
548 // compilers.
549 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
550 }
551 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000552}
553
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000554bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000555 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000556 RTPHeader header;
557 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
558 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
559 "IncomingPacket invalid RTP header");
560 return false;
561 }
562 header.payload_type_frequency =
563 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
564 if (header.payload_type_frequency < 0)
565 return false;
566 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
567}
568
henrik.lundin42dda502016-05-18 05:36:01 -0700569MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
570 int32_t id,
571 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700572 unsigned int ssrc;
573 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
574 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800575 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700576 bool muted;
577 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
578 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800579 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
580 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
581 // In all likelihood, the audio in this frame is garbage. We return an
582 // error so that the audio mixer module doesn't add it to the mix. As
583 // a result, it won't be played out and the actions skipped here are
584 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700585 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800586 }
henrik.lundina89ab962016-05-18 08:52:45 -0700587
588 if (muted) {
589 // TODO(henrik.lundin): We should be able to do better than this. But we
590 // will have to go through all the cases below where the audio samples may
591 // be used, and handle the muted case in some way.
592 audioFrame->Mute();
593 }
kwiberg55b97fe2016-01-28 05:22:45 -0800594
kwiberg55b97fe2016-01-28 05:22:45 -0800595 // Convert module ID to internal VoE channel ID
596 audioFrame->id_ = VoEChannelId(audioFrame->id_);
597 // Store speech type for dead-or-alive detection
598 _outputSpeechType = audioFrame->speech_type_;
599
600 ChannelState::State state = channel_state_.Get();
601
kwiberg55b97fe2016-01-28 05:22:45 -0800602 {
603 // Pass the audio buffers to an optional sink callback, before applying
604 // scaling/panning, as that applies to the mix operation.
605 // External recipients of the audio (e.g. via AudioTrack), will do their
606 // own mixing/dynamic processing.
607 rtc::CritScope cs(&_callbackCritSect);
608 if (audio_sink_) {
609 AudioSinkInterface::Data data(
610 &audioFrame->data_[0], audioFrame->samples_per_channel_,
611 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
612 audioFrame->timestamp_);
613 audio_sink_->OnData(data);
614 }
615 }
616
617 float output_gain = 1.0f;
618 float left_pan = 1.0f;
619 float right_pan = 1.0f;
620 {
621 rtc::CritScope cs(&volume_settings_critsect_);
622 output_gain = _outputGain;
623 left_pan = _panLeft;
624 right_pan = _panRight;
625 }
626
627 // Output volume scaling
628 if (output_gain < 0.99f || output_gain > 1.01f) {
629 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
630 }
631
632 // Scale left and/or right channel(s) if stereo and master balance is
633 // active
634
635 if (left_pan != 1.0f || right_pan != 1.0f) {
636 if (audioFrame->num_channels_ == 1) {
637 // Emulate stereo mode since panning is active.
638 // The mono signal is copied to both left and right channels here.
639 AudioFrameOperations::MonoToStereo(audioFrame);
640 }
641 // For true stereo mode (when we are receiving a stereo signal), no
642 // action is needed.
643
644 // Do the panning operation (the audio frame contains stereo at this
645 // stage)
646 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
647 }
648
649 // Mix decoded PCM output with file if file mixing is enabled
650 if (state.output_file_playing) {
651 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700652 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800653 }
654
655 // External media
656 if (_outputExternalMedia) {
657 rtc::CritScope cs(&_callbackCritSect);
658 const bool isStereo = (audioFrame->num_channels_ == 2);
659 if (_outputExternalMediaCallbackPtr) {
660 _outputExternalMediaCallbackPtr->Process(
661 _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_,
662 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
663 isStereo);
664 }
665 }
666
667 // Record playout if enabled
668 {
669 rtc::CritScope cs(&_fileCritSect);
670
kwiberg5a25d952016-08-17 07:31:12 -0700671 if (_outputFileRecording && output_file_recorder_) {
672 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800673 }
674 }
675
676 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700677 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800678 _outputAudioLevel.ComputeLevel(*audioFrame);
679
680 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
681 // The first frame with a valid rtp timestamp.
682 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
683 }
684
685 if (capture_start_rtp_time_stamp_ >= 0) {
686 // audioFrame.timestamp_ should be valid from now on.
687
688 // Compute elapsed time.
689 int64_t unwrap_timestamp =
690 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
691 audioFrame->elapsed_time_ms_ =
692 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
693 (GetPlayoutFrequency() / 1000);
694
niklase@google.com470e71d2011-07-07 08:21:25 +0000695 {
kwiberg55b97fe2016-01-28 05:22:45 -0800696 rtc::CritScope lock(&ts_stats_lock_);
697 // Compute ntp time.
698 audioFrame->ntp_time_ms_ =
699 ntp_estimator_.Estimate(audioFrame->timestamp_);
700 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
701 if (audioFrame->ntp_time_ms_ > 0) {
702 // Compute |capture_start_ntp_time_ms_| so that
703 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
704 capture_start_ntp_time_ms_ =
705 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000706 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000707 }
kwiberg55b97fe2016-01-28 05:22:45 -0800708 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000709
henrik.lundin42dda502016-05-18 05:36:01 -0700710 return muted ? MixerParticipant::AudioFrameInfo::kMuted
711 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000712}
713
kwiberg55b97fe2016-01-28 05:22:45 -0800714int32_t Channel::NeededFrequency(int32_t id) const {
715 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
716 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000717
kwiberg55b97fe2016-01-28 05:22:45 -0800718 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000719
kwiberg55b97fe2016-01-28 05:22:45 -0800720 // Determine highest needed receive frequency
721 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000722
kwiberg55b97fe2016-01-28 05:22:45 -0800723 // Return the bigger of playout and receive frequency in the ACM.
724 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
725 highestNeeded = audio_coding_->PlayoutFrequency();
726 } else {
727 highestNeeded = receiveFrequency;
728 }
729
730 // Special case, if we're playing a file on the playout side
731 // we take that frequency into consideration as well
732 // This is not needed on sending side, since the codec will
733 // limit the spectrum anyway.
734 if (channel_state_.Get().output_file_playing) {
735 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700736 if (output_file_player_) {
737 if (output_file_player_->Frequency() > highestNeeded) {
738 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800739 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000740 }
kwiberg55b97fe2016-01-28 05:22:45 -0800741 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000742
kwiberg55b97fe2016-01-28 05:22:45 -0800743 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000744}
745
ossu5f7cfa52016-05-30 08:11:28 -0700746int32_t Channel::CreateChannel(
747 Channel*& channel,
748 int32_t channelId,
749 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700750 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800751 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
752 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
753 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000754
solenberg88499ec2016-09-07 07:34:41 -0700755 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800756 if (channel == NULL) {
757 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
758 "Channel::CreateChannel() unable to allocate memory for"
759 " channel");
760 return -1;
761 }
762 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000763}
764
kwiberg55b97fe2016-01-28 05:22:45 -0800765void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
766 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
767 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
768 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000769
kwiberg55b97fe2016-01-28 05:22:45 -0800770 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000771}
772
kwiberg55b97fe2016-01-28 05:22:45 -0800773void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
774 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
775 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
776 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000777
kwiberg55b97fe2016-01-28 05:22:45 -0800778 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000779}
780
kwiberg55b97fe2016-01-28 05:22:45 -0800781void Channel::PlayFileEnded(int32_t id) {
782 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
783 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000784
kwiberg55b97fe2016-01-28 05:22:45 -0800785 if (id == _inputFilePlayerId) {
786 channel_state_.SetInputFilePlaying(false);
787 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
788 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000789 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800790 } else if (id == _outputFilePlayerId) {
791 channel_state_.SetOutputFilePlaying(false);
792 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
793 "Channel::PlayFileEnded() => output file player module is"
794 " shutdown");
795 }
796}
797
798void Channel::RecordFileEnded(int32_t id) {
799 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
800 "Channel::RecordFileEnded(id=%d)", id);
801
802 assert(id == _outputFileRecorderId);
803
804 rtc::CritScope cs(&_fileCritSect);
805
806 _outputFileRecording = false;
807 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
808 "Channel::RecordFileEnded() => output file recorder module is"
809 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000810}
811
pbos@webrtc.org92135212013-05-14 08:31:39 +0000812Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000813 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700814 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800815 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100816 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700817 event_log_proxy_(new RtcEventLogProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100818 rtp_header_parser_(RtpHeaderParser::Create()),
819 rtp_payload_registry_(
820 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
821 rtp_receive_statistics_(
822 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
823 rtp_receiver_(
824 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100825 this,
826 this,
827 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700828 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100829 _outputAudioLevel(),
830 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100831 // Avoid conflict with other channels by adding 1024 - 1026,
832 // won't use as much as 1024 channels.
833 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
834 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
835 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
836 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100837 _outputExternalMedia(false),
838 _inputExternalMediaCallbackPtr(NULL),
839 _outputExternalMediaCallbackPtr(NULL),
840 _timeStamp(0), // This is just an offset, RTP module will add it's own
841 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100842 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100843 playout_timestamp_rtp_(0),
844 playout_timestamp_rtcp_(0),
845 playout_delay_ms_(0),
846 _numberOfDiscardedPackets(0),
847 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100848 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
849 capture_start_rtp_time_stamp_(-1),
850 capture_start_ntp_time_ms_(-1),
851 _engineStatisticsPtr(NULL),
852 _outputMixerPtr(NULL),
853 _transmitMixerPtr(NULL),
854 _moduleProcessThreadPtr(NULL),
855 _audioDeviceModulePtr(NULL),
856 _voiceEngineObserverPtr(NULL),
857 _callbackCritSectPtr(NULL),
858 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100859 _sendFrameType(0),
860 _externalMixing(false),
861 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700862 input_mute_(false),
863 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100864 _panLeft(1.0f),
865 _panRight(1.0f),
866 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100867 _lastLocalTimeStamp(0),
868 _lastPayloadType(0),
869 _includeAudioLevelIndication(false),
870 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100871 restored_packet_in_use_(false),
872 rtcp_observer_(new VoERtcpObserver(this)),
873 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock())),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100874 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700875 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800876 feedback_observer_proxy_(new TransportFeedbackProxy()),
877 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700878 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200879 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
880 kMaxRetransmissionWindowMs)),
solenberg88499ec2016-09-07 07:34:41 -0700881 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800882 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
883 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700884 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800885 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700886 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800887 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200888
kwiberg55b97fe2016-01-28 05:22:45 -0800889 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000890
kwiberg55b97fe2016-01-28 05:22:45 -0800891 RtpRtcp::Configuration configuration;
892 configuration.audio = true;
893 configuration.outgoing_transport = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800894 configuration.receive_statistics = rtp_receive_statistics_.get();
895 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800896 if (pacing_enabled_) {
897 configuration.paced_sender = rtp_packet_sender_proxy_.get();
898 configuration.transport_sequence_number_allocator =
899 seq_num_allocator_proxy_.get();
900 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
901 }
ivoc14d5dbe2016-07-04 07:06:55 -0700902 configuration.event_log = &(*event_log_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200903 configuration.retransmission_rate_limiter =
904 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000905
kwiberg55b97fe2016-01-28 05:22:45 -0800906 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100907 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000908
kwiberg55b97fe2016-01-28 05:22:45 -0800909 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
910 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
911 statistics_proxy_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000912}
913
kwiberg55b97fe2016-01-28 05:22:45 -0800914Channel::~Channel() {
915 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
916 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
917 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000918
kwiberg55b97fe2016-01-28 05:22:45 -0800919 if (_outputExternalMedia) {
920 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
921 }
922 if (channel_state_.Get().input_external_media) {
923 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
924 }
925 StopSend();
926 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000927
kwiberg55b97fe2016-01-28 05:22:45 -0800928 {
929 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700930 if (input_file_player_) {
931 input_file_player_->RegisterModuleFileCallback(NULL);
932 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000933 }
kwiberg5a25d952016-08-17 07:31:12 -0700934 if (output_file_player_) {
935 output_file_player_->RegisterModuleFileCallback(NULL);
936 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800937 }
kwiberg5a25d952016-08-17 07:31:12 -0700938 if (output_file_recorder_) {
939 output_file_recorder_->RegisterModuleFileCallback(NULL);
940 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800941 }
942 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000943
kwiberg55b97fe2016-01-28 05:22:45 -0800944 // The order to safely shutdown modules in a channel is:
945 // 1. De-register callbacks in modules
946 // 2. De-register modules in process thread
947 // 3. Destroy modules
948 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
949 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
950 "~Channel() failed to de-register transport callback"
951 " (Audio coding module)");
952 }
953 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
954 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
955 "~Channel() failed to de-register VAD callback"
956 " (Audio coding module)");
957 }
958 // De-register modules in process thread
959 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000960
kwiberg55b97fe2016-01-28 05:22:45 -0800961 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000962}
963
kwiberg55b97fe2016-01-28 05:22:45 -0800964int32_t Channel::Init() {
965 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
966 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000967
kwiberg55b97fe2016-01-28 05:22:45 -0800968 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000969
kwiberg55b97fe2016-01-28 05:22:45 -0800970 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000971
kwiberg55b97fe2016-01-28 05:22:45 -0800972 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
973 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
974 "Channel::Init() must call SetEngineInformation() first");
975 return -1;
976 }
977
978 // --- Add modules to process thread (for periodic schedulation)
979
980 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
981
982 // --- ACM initialization
983
984 if (audio_coding_->InitializeReceiver() == -1) {
985 _engineStatisticsPtr->SetLastError(
986 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
987 "Channel::Init() unable to initialize the ACM - 1");
988 return -1;
989 }
990
991 // --- RTP/RTCP module initialization
992
993 // Ensure that RTCP is enabled by default for the created channel.
994 // Note that, the module will keep generating RTCP until it is explicitly
995 // disabled by the user.
996 // After StopListen (when no sockets exists), RTCP packets will no longer
997 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -0700998 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800999 // RTCP is enabled by default.
1000 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1001 // --- Register all permanent callbacks
1002 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1003 (audio_coding_->RegisterVADCallback(this) == -1);
1004
1005 if (fail) {
1006 _engineStatisticsPtr->SetLastError(
1007 VE_CANNOT_INIT_CHANNEL, kTraceError,
1008 "Channel::Init() callbacks not registered");
1009 return -1;
1010 }
1011
1012 // --- Register all supported codecs to the receiving side of the
1013 // RTP/RTCP module
1014
1015 CodecInst codec;
1016 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1017
1018 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1019 // Open up the RTP/RTCP receiver for all supported codecs
1020 if ((audio_coding_->Codec(idx, &codec) == -1) ||
1021 (rtp_receiver_->RegisterReceivePayload(
1022 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1023 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
1024 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1025 "Channel::Init() unable to register %s "
1026 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1027 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1028 codec.rate);
1029 } else {
1030 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1031 "Channel::Init() %s (%d/%d/%" PRIuS
1032 "/%d) has been "
1033 "added to the RTP/RTCP receiver",
1034 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1035 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001036 }
1037
kwiberg55b97fe2016-01-28 05:22:45 -08001038 // Ensure that PCMU is used as default codec on the sending side
1039 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1040 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001041 }
1042
kwiberg55b97fe2016-01-28 05:22:45 -08001043 // Register default PT for outband 'telephone-event'
1044 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001045 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
1046 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001047 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1048 "Channel::Init() failed to register outband "
1049 "'telephone-event' (%d/%d) correctly",
1050 codec.pltype, codec.plfreq);
1051 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001052 }
1053
kwiberg55b97fe2016-01-28 05:22:45 -08001054 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001055 if (!codec_manager_.RegisterEncoder(codec) ||
1056 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
1057 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec) ||
1058 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001059 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1060 "Channel::Init() failed to register CN (%d/%d) "
1061 "correctly - 1",
1062 codec.pltype, codec.plfreq);
1063 }
1064 }
kwiberg55b97fe2016-01-28 05:22:45 -08001065 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001066
kwiberg55b97fe2016-01-28 05:22:45 -08001067 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001068}
1069
kwiberg55b97fe2016-01-28 05:22:45 -08001070int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1071 OutputMixer& outputMixer,
1072 voe::TransmitMixer& transmitMixer,
1073 ProcessThread& moduleProcessThread,
1074 AudioDeviceModule& audioDeviceModule,
1075 VoiceEngineObserver* voiceEngineObserver,
1076 rtc::CriticalSection* callbackCritSect) {
1077 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1078 "Channel::SetEngineInformation()");
1079 _engineStatisticsPtr = &engineStatistics;
1080 _outputMixerPtr = &outputMixer;
1081 _transmitMixerPtr = &transmitMixer,
1082 _moduleProcessThreadPtr = &moduleProcessThread;
1083 _audioDeviceModulePtr = &audioDeviceModule;
1084 _voiceEngineObserverPtr = voiceEngineObserver;
1085 _callbackCritSectPtr = callbackCritSect;
1086 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001087}
1088
kwiberg55b97fe2016-01-28 05:22:45 -08001089int32_t Channel::UpdateLocalTimeStamp() {
1090 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1091 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001092}
1093
kwibergb7f89d62016-02-17 10:04:18 -08001094void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001095 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001096 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001097}
1098
ossu29b1a8d2016-06-13 07:34:51 -07001099const rtc::scoped_refptr<AudioDecoderFactory>&
1100Channel::GetAudioDecoderFactory() const {
1101 return decoder_factory_;
1102}
1103
kwiberg55b97fe2016-01-28 05:22:45 -08001104int32_t Channel::StartPlayout() {
1105 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1106 "Channel::StartPlayout()");
1107 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001108 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001109 }
1110
1111 if (!_externalMixing) {
1112 // Add participant as candidates for mixing.
1113 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1114 _engineStatisticsPtr->SetLastError(
1115 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1116 "StartPlayout() failed to add participant to mixer");
1117 return -1;
1118 }
1119 }
1120
1121 channel_state_.SetPlaying(true);
1122 if (RegisterFilePlayingToMixer() != 0)
1123 return -1;
1124
1125 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001126}
1127
kwiberg55b97fe2016-01-28 05:22:45 -08001128int32_t Channel::StopPlayout() {
1129 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1130 "Channel::StopPlayout()");
1131 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001132 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001133 }
1134
1135 if (!_externalMixing) {
1136 // Remove participant as candidates for mixing
1137 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1138 _engineStatisticsPtr->SetLastError(
1139 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1140 "StopPlayout() failed to remove participant from mixer");
1141 return -1;
1142 }
1143 }
1144
1145 channel_state_.SetPlaying(false);
1146 _outputAudioLevel.Clear();
1147
1148 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001149}
1150
kwiberg55b97fe2016-01-28 05:22:45 -08001151int32_t Channel::StartSend() {
1152 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1153 "Channel::StartSend()");
1154 // Resume the previous sequence number which was reset by StopSend().
1155 // This needs to be done before |sending| is set to true.
1156 if (send_sequence_number_)
1157 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001158
kwiberg55b97fe2016-01-28 05:22:45 -08001159 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001160 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001161 }
1162 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001163
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001164 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001165 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1166 _engineStatisticsPtr->SetLastError(
1167 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1168 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001169 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001170 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001171 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001172 return -1;
1173 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001174
kwiberg55b97fe2016-01-28 05:22:45 -08001175 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001176}
1177
kwiberg55b97fe2016-01-28 05:22:45 -08001178int32_t Channel::StopSend() {
1179 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1180 "Channel::StopSend()");
1181 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001182 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001183 }
1184 channel_state_.SetSending(false);
1185
1186 // Store the sequence number to be able to pick up the same sequence for
1187 // the next StartSend(). This is needed for restarting device, otherwise
1188 // it might cause libSRTP to complain about packets being replayed.
1189 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1190 // CL is landed. See issue
1191 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1192 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1193
1194 // Reset sending SSRC and sequence number and triggers direct transmission
1195 // of RTCP BYE
1196 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1197 _engineStatisticsPtr->SetLastError(
1198 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1199 "StartSend() RTP/RTCP failed to stop sending");
1200 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001201 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001202
1203 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001204}
1205
kwiberg55b97fe2016-01-28 05:22:45 -08001206int32_t Channel::StartReceiving() {
1207 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1208 "Channel::StartReceiving()");
1209 if (channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001210 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001211 }
1212 channel_state_.SetReceiving(true);
1213 _numberOfDiscardedPackets = 0;
1214 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001215}
1216
kwiberg55b97fe2016-01-28 05:22:45 -08001217int32_t Channel::StopReceiving() {
1218 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1219 "Channel::StopReceiving()");
1220 if (!channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001221 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001222 }
1223
1224 channel_state_.SetReceiving(false);
1225 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001226}
1227
kwiberg55b97fe2016-01-28 05:22:45 -08001228int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1229 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1230 "Channel::RegisterVoiceEngineObserver()");
1231 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001232
kwiberg55b97fe2016-01-28 05:22:45 -08001233 if (_voiceEngineObserverPtr) {
1234 _engineStatisticsPtr->SetLastError(
1235 VE_INVALID_OPERATION, kTraceError,
1236 "RegisterVoiceEngineObserver() observer already enabled");
1237 return -1;
1238 }
1239 _voiceEngineObserverPtr = &observer;
1240 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001241}
1242
kwiberg55b97fe2016-01-28 05:22:45 -08001243int32_t Channel::DeRegisterVoiceEngineObserver() {
1244 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1245 "Channel::DeRegisterVoiceEngineObserver()");
1246 rtc::CritScope cs(&_callbackCritSect);
1247
1248 if (!_voiceEngineObserverPtr) {
1249 _engineStatisticsPtr->SetLastError(
1250 VE_INVALID_OPERATION, kTraceWarning,
1251 "DeRegisterVoiceEngineObserver() observer already disabled");
1252 return 0;
1253 }
1254 _voiceEngineObserverPtr = NULL;
1255 return 0;
1256}
1257
1258int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001259 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001260 if (send_codec) {
1261 codec = *send_codec;
1262 return 0;
1263 }
1264 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001265}
1266
kwiberg55b97fe2016-01-28 05:22:45 -08001267int32_t Channel::GetRecCodec(CodecInst& codec) {
1268 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001269}
1270
kwiberg55b97fe2016-01-28 05:22:45 -08001271int32_t Channel::SetSendCodec(const CodecInst& codec) {
1272 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1273 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001274
kwibergc8d071e2016-04-06 12:22:38 -07001275 if (!codec_manager_.RegisterEncoder(codec) ||
1276 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001277 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1278 "SetSendCodec() failed to register codec to ACM");
1279 return -1;
1280 }
1281
1282 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1283 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1284 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1285 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1286 "SetSendCodec() failed to register codec to"
1287 " RTP/RTCP module");
1288 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001289 }
kwiberg55b97fe2016-01-28 05:22:45 -08001290 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001291
kwiberg55b97fe2016-01-28 05:22:45 -08001292 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) {
1293 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1294 "SetSendCodec() failed to set audio packet size");
1295 return -1;
1296 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001297
kwiberg55b97fe2016-01-28 05:22:45 -08001298 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001299}
1300
Ivo Creusenadf89b72015-04-29 16:03:33 +02001301void Channel::SetBitRate(int bitrate_bps) {
1302 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1303 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
1304 audio_coding_->SetBitRate(bitrate_bps);
Erik Språng737336d2016-07-29 12:59:36 +02001305 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001306}
1307
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001308void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001309 network_predictor_->UpdatePacketLossRate(fraction_lost);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001310 uint8_t average_fraction_loss = network_predictor_->GetLossRate();
1311
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001312 // Normalizes rate to 0 - 100.
kwiberg55b97fe2016-01-28 05:22:45 -08001313 if (audio_coding_->SetPacketLossRate(100 * average_fraction_loss / 255) !=
1314 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001315 assert(false); // This should not happen.
1316 }
1317}
1318
kwiberg55b97fe2016-01-28 05:22:45 -08001319int32_t Channel::SetVADStatus(bool enableVAD,
1320 ACMVADMode mode,
1321 bool disableDTX) {
1322 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1323 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001324 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1325 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1326 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001327 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1328 kTraceError,
1329 "SetVADStatus() failed to set VAD");
1330 return -1;
1331 }
1332 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001333}
1334
kwiberg55b97fe2016-01-28 05:22:45 -08001335int32_t Channel::GetVADStatus(bool& enabledVAD,
1336 ACMVADMode& mode,
1337 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001338 const auto* params = codec_manager_.GetStackParams();
1339 enabledVAD = params->use_cng;
1340 mode = params->vad_mode;
1341 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001342 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001343}
1344
kwiberg55b97fe2016-01-28 05:22:45 -08001345int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1346 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1347 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001348
kwiberg55b97fe2016-01-28 05:22:45 -08001349 if (channel_state_.Get().playing) {
1350 _engineStatisticsPtr->SetLastError(
1351 VE_ALREADY_PLAYING, kTraceError,
1352 "SetRecPayloadType() unable to set PT while playing");
1353 return -1;
1354 }
1355 if (channel_state_.Get().receiving) {
1356 _engineStatisticsPtr->SetLastError(
1357 VE_ALREADY_LISTENING, kTraceError,
1358 "SetRecPayloadType() unable to set PT while listening");
1359 return -1;
1360 }
1361
1362 if (codec.pltype == -1) {
1363 // De-register the selected codec (RTP/RTCP module and ACM)
1364
1365 int8_t pltype(-1);
1366 CodecInst rxCodec = codec;
1367
1368 // Get payload type for the given codec
1369 rtp_payload_registry_->ReceivePayloadType(
1370 rxCodec.plname, rxCodec.plfreq, rxCodec.channels,
1371 (rxCodec.rate < 0) ? 0 : rxCodec.rate, &pltype);
1372 rxCodec.pltype = pltype;
1373
1374 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1375 _engineStatisticsPtr->SetLastError(
1376 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1377 "SetRecPayloadType() RTP/RTCP-module deregistration "
1378 "failed");
1379 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001380 }
kwiberg55b97fe2016-01-28 05:22:45 -08001381 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1382 _engineStatisticsPtr->SetLastError(
1383 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1384 "SetRecPayloadType() ACM deregistration failed - 1");
1385 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001386 }
kwiberg55b97fe2016-01-28 05:22:45 -08001387 return 0;
1388 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001389
kwiberg55b97fe2016-01-28 05:22:45 -08001390 if (rtp_receiver_->RegisterReceivePayload(
1391 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1392 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1393 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001394 // TODO(kwiberg): Retrying is probably not necessary, since
1395 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001396 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001397 if (rtp_receiver_->RegisterReceivePayload(
kwiberg55b97fe2016-01-28 05:22:45 -08001398 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1399 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1400 _engineStatisticsPtr->SetLastError(
1401 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1402 "SetRecPayloadType() RTP/RTCP-module registration failed");
1403 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001404 }
kwiberg55b97fe2016-01-28 05:22:45 -08001405 }
kwibergc8d071e2016-04-06 12:22:38 -07001406 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001407 audio_coding_->UnregisterReceiveCodec(codec.pltype);
kwibergc8d071e2016-04-06 12:22:38 -07001408 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001409 _engineStatisticsPtr->SetLastError(
1410 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1411 "SetRecPayloadType() ACM registration failed - 1");
1412 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001413 }
kwiberg55b97fe2016-01-28 05:22:45 -08001414 }
1415 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001416}
1417
kwiberg55b97fe2016-01-28 05:22:45 -08001418int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1419 int8_t payloadType(-1);
1420 if (rtp_payload_registry_->ReceivePayloadType(
1421 codec.plname, codec.plfreq, codec.channels,
1422 (codec.rate < 0) ? 0 : codec.rate, &payloadType) != 0) {
1423 _engineStatisticsPtr->SetLastError(
1424 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1425 "GetRecPayloadType() failed to retrieve RX payload type");
1426 return -1;
1427 }
1428 codec.pltype = payloadType;
1429 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001430}
1431
kwiberg55b97fe2016-01-28 05:22:45 -08001432int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1433 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1434 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001435
kwiberg55b97fe2016-01-28 05:22:45 -08001436 CodecInst codec;
1437 int32_t samplingFreqHz(-1);
1438 const size_t kMono = 1;
1439 if (frequency == kFreq32000Hz)
1440 samplingFreqHz = 32000;
1441 else if (frequency == kFreq16000Hz)
1442 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001443
kwiberg55b97fe2016-01-28 05:22:45 -08001444 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1445 _engineStatisticsPtr->SetLastError(
1446 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1447 "SetSendCNPayloadType() failed to retrieve default CN codec "
1448 "settings");
1449 return -1;
1450 }
1451
1452 // Modify the payload type (must be set to dynamic range)
1453 codec.pltype = type;
1454
kwibergc8d071e2016-04-06 12:22:38 -07001455 if (!codec_manager_.RegisterEncoder(codec) ||
1456 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001457 _engineStatisticsPtr->SetLastError(
1458 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1459 "SetSendCNPayloadType() failed to register CN to ACM");
1460 return -1;
1461 }
1462
1463 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1464 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1465 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1466 _engineStatisticsPtr->SetLastError(
1467 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1468 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1469 "module");
1470 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001471 }
kwiberg55b97fe2016-01-28 05:22:45 -08001472 }
1473 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001474}
1475
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001476int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001477 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001478 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001479
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001480 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001481 _engineStatisticsPtr->SetLastError(
1482 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001483 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001484 return -1;
1485 }
1486 return 0;
1487}
1488
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001489int Channel::SetOpusDtx(bool enable_dtx) {
1490 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1491 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001492 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001493 : audio_coding_->DisableOpusDtx();
1494 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001495 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1496 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001497 return -1;
1498 }
1499 return 0;
1500}
1501
ivoc85228d62016-07-27 04:53:47 -07001502int Channel::GetOpusDtx(bool* enabled) {
1503 int success = -1;
1504 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1505 if (encoder) {
1506 *enabled = encoder->GetDtx();
1507 success = 0;
1508 }
1509 });
1510 return success;
1511}
1512
mflodman3d7db262016-04-29 00:57:13 -07001513int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001514 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001515 "Channel::RegisterExternalTransport()");
1516
kwiberg55b97fe2016-01-28 05:22:45 -08001517 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001518 if (_externalTransport) {
1519 _engineStatisticsPtr->SetLastError(
1520 VE_INVALID_OPERATION, kTraceError,
1521 "RegisterExternalTransport() external transport already enabled");
1522 return -1;
1523 }
1524 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001525 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001526 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001527}
1528
kwiberg55b97fe2016-01-28 05:22:45 -08001529int32_t Channel::DeRegisterExternalTransport() {
1530 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1531 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001532
kwiberg55b97fe2016-01-28 05:22:45 -08001533 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001534 if (_transportPtr) {
1535 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1536 "DeRegisterExternalTransport() all transport is disabled");
1537 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001538 _engineStatisticsPtr->SetLastError(
1539 VE_INVALID_OPERATION, kTraceWarning,
1540 "DeRegisterExternalTransport() external transport already "
1541 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001542 }
1543 _externalTransport = false;
1544 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001545 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001546}
1547
mflodman3d7db262016-04-29 00:57:13 -07001548int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001549 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001550 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001551 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001552 "Channel::ReceivedRTPPacket()");
1553
1554 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001555 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001556
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001557 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001558 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1559 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1560 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001561 return -1;
1562 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001563 header.payload_type_frequency =
1564 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001565 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001566 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001567 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001568 rtp_receive_statistics_->IncomingPacket(
1569 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001570 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001571
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001572 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001573}
1574
1575bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001576 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001577 const RTPHeader& header,
1578 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001579 if (rtp_payload_registry_->IsRtx(header)) {
1580 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001581 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001582 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001583 assert(packet_length >= header.headerLength);
1584 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001585 PayloadUnion payload_specific;
1586 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001587 &payload_specific)) {
1588 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001589 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001590 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1591 payload_specific, in_order);
1592}
1593
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001594bool Channel::HandleRtxPacket(const uint8_t* packet,
1595 size_t packet_length,
1596 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001597 if (!rtp_payload_registry_->IsRtx(header))
1598 return false;
1599
1600 // Remove the RTX header and parse the original RTP header.
1601 if (packet_length < header.headerLength)
1602 return false;
1603 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1604 return false;
1605 if (restored_packet_in_use_) {
1606 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1607 "Multiple RTX headers detected, dropping packet");
1608 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001609 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001610 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001611 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1612 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001613 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1614 "Incoming RTX packet: invalid RTP header");
1615 return false;
1616 }
1617 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001618 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001619 restored_packet_in_use_ = false;
1620 return ret;
1621}
1622
1623bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1624 StreamStatistician* statistician =
1625 rtp_receive_statistics_->GetStatistician(header.ssrc);
1626 if (!statistician)
1627 return false;
1628 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001629}
1630
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001631bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1632 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001633 // Retransmissions are handled separately if RTX is enabled.
1634 if (rtp_payload_registry_->RtxEnabled())
1635 return false;
1636 StreamStatistician* statistician =
1637 rtp_receive_statistics_->GetStatistician(header.ssrc);
1638 if (!statistician)
1639 return false;
1640 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001641 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001642 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001643 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001644}
1645
mflodman3d7db262016-04-29 00:57:13 -07001646int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001647 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001648 "Channel::ReceivedRTCPPacket()");
1649 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001650 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001651
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001652 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001653 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001654 _engineStatisticsPtr->SetLastError(
1655 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1656 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1657 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001658
Minyue2013aec2015-05-13 14:14:42 +02001659 int64_t rtt = GetRTT(true);
1660 if (rtt == 0) {
1661 // Waiting for valid RTT.
1662 return 0;
1663 }
Erik Språng737336d2016-07-29 12:59:36 +02001664
1665 int64_t nack_window_ms = rtt;
1666 if (nack_window_ms < kMinRetransmissionWindowMs) {
1667 nack_window_ms = kMinRetransmissionWindowMs;
1668 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1669 nack_window_ms = kMaxRetransmissionWindowMs;
1670 }
1671 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1672
Minyue2013aec2015-05-13 14:14:42 +02001673 uint32_t ntp_secs = 0;
1674 uint32_t ntp_frac = 0;
1675 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001676 if (0 !=
1677 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1678 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001679 // Waiting for RTCP.
1680 return 0;
1681 }
1682
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001683 {
tommi31fc21f2016-01-21 10:37:37 -08001684 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001685 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001686 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001687 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001688}
1689
niklase@google.com470e71d2011-07-07 08:21:25 +00001690int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001691 bool loop,
1692 FileFormats format,
1693 int startPosition,
1694 float volumeScaling,
1695 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001696 const CodecInst* codecInst) {
1697 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1698 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1699 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1700 "stopPosition=%d)",
1701 fileName, loop, format, volumeScaling, startPosition,
1702 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001703
kwiberg55b97fe2016-01-28 05:22:45 -08001704 if (channel_state_.Get().output_file_playing) {
1705 _engineStatisticsPtr->SetLastError(
1706 VE_ALREADY_PLAYING, kTraceError,
1707 "StartPlayingFileLocally() is already playing");
1708 return -1;
1709 }
1710
1711 {
1712 rtc::CritScope cs(&_fileCritSect);
1713
kwiberg5a25d952016-08-17 07:31:12 -07001714 if (output_file_player_) {
1715 output_file_player_->RegisterModuleFileCallback(NULL);
1716 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001717 }
1718
kwiberg5b356f42016-09-08 04:32:33 -07001719 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001720 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001721
kwiberg5a25d952016-08-17 07:31:12 -07001722 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001723 _engineStatisticsPtr->SetLastError(
1724 VE_INVALID_ARGUMENT, kTraceError,
1725 "StartPlayingFileLocally() filePlayer format is not correct");
1726 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001727 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001728
kwiberg55b97fe2016-01-28 05:22:45 -08001729 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001730
kwiberg5a25d952016-08-17 07:31:12 -07001731 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001732 fileName, loop, startPosition, volumeScaling, notificationTime,
1733 stopPosition, (const CodecInst*)codecInst) != 0) {
1734 _engineStatisticsPtr->SetLastError(
1735 VE_BAD_FILE, kTraceError,
1736 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001737 output_file_player_->StopPlayingFile();
1738 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001739 return -1;
1740 }
kwiberg5a25d952016-08-17 07:31:12 -07001741 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001742 channel_state_.SetOutputFilePlaying(true);
1743 }
1744
1745 if (RegisterFilePlayingToMixer() != 0)
1746 return -1;
1747
1748 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001749}
1750
1751int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001752 FileFormats format,
1753 int startPosition,
1754 float volumeScaling,
1755 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001756 const CodecInst* codecInst) {
1757 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1758 "Channel::StartPlayingFileLocally(format=%d,"
1759 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1760 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001761
kwiberg55b97fe2016-01-28 05:22:45 -08001762 if (stream == NULL) {
1763 _engineStatisticsPtr->SetLastError(
1764 VE_BAD_FILE, kTraceError,
1765 "StartPlayingFileLocally() NULL as input stream");
1766 return -1;
1767 }
1768
1769 if (channel_state_.Get().output_file_playing) {
1770 _engineStatisticsPtr->SetLastError(
1771 VE_ALREADY_PLAYING, kTraceError,
1772 "StartPlayingFileLocally() is already playing");
1773 return -1;
1774 }
1775
1776 {
1777 rtc::CritScope cs(&_fileCritSect);
1778
1779 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001780 if (output_file_player_) {
1781 output_file_player_->RegisterModuleFileCallback(NULL);
1782 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001783 }
1784
kwiberg55b97fe2016-01-28 05:22:45 -08001785 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001786 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001787 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001788
kwiberg5a25d952016-08-17 07:31:12 -07001789 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001790 _engineStatisticsPtr->SetLastError(
1791 VE_INVALID_ARGUMENT, kTraceError,
1792 "StartPlayingFileLocally() filePlayer format isnot correct");
1793 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001794 }
1795
kwiberg55b97fe2016-01-28 05:22:45 -08001796 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001797
kwiberg4ec01d92016-08-22 08:43:54 -07001798 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001799 volumeScaling, notificationTime,
1800 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001801 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1802 "StartPlayingFile() failed to "
1803 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001804 output_file_player_->StopPlayingFile();
1805 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001806 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001807 }
kwiberg5a25d952016-08-17 07:31:12 -07001808 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001809 channel_state_.SetOutputFilePlaying(true);
1810 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001811
kwiberg55b97fe2016-01-28 05:22:45 -08001812 if (RegisterFilePlayingToMixer() != 0)
1813 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001814
kwiberg55b97fe2016-01-28 05:22:45 -08001815 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001816}
1817
kwiberg55b97fe2016-01-28 05:22:45 -08001818int Channel::StopPlayingFileLocally() {
1819 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1820 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001821
kwiberg55b97fe2016-01-28 05:22:45 -08001822 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001823 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001824 }
1825
1826 {
1827 rtc::CritScope cs(&_fileCritSect);
1828
kwiberg5a25d952016-08-17 07:31:12 -07001829 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001830 _engineStatisticsPtr->SetLastError(
1831 VE_STOP_RECORDING_FAILED, kTraceError,
1832 "StopPlayingFile() could not stop playing");
1833 return -1;
1834 }
kwiberg5a25d952016-08-17 07:31:12 -07001835 output_file_player_->RegisterModuleFileCallback(NULL);
1836 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001837 channel_state_.SetOutputFilePlaying(false);
1838 }
1839 // _fileCritSect cannot be taken while calling
1840 // SetAnonymousMixibilityStatus. Refer to comments in
1841 // StartPlayingFileLocally(const char* ...) for more details.
1842 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1843 _engineStatisticsPtr->SetLastError(
1844 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1845 "StopPlayingFile() failed to stop participant from playing as"
1846 "file in the mixer");
1847 return -1;
1848 }
1849
1850 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001851}
1852
kwiberg55b97fe2016-01-28 05:22:45 -08001853int Channel::IsPlayingFileLocally() const {
1854 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001855}
1856
kwiberg55b97fe2016-01-28 05:22:45 -08001857int Channel::RegisterFilePlayingToMixer() {
1858 // Return success for not registering for file playing to mixer if:
1859 // 1. playing file before playout is started on that channel.
1860 // 2. starting playout without file playing on that channel.
1861 if (!channel_state_.Get().playing ||
1862 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001863 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001864 }
1865
1866 // |_fileCritSect| cannot be taken while calling
1867 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1868 // frames can be pulled by the mixer. Since the frames are generated from
1869 // the file, _fileCritSect will be taken. This would result in a deadlock.
1870 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1871 channel_state_.SetOutputFilePlaying(false);
1872 rtc::CritScope cs(&_fileCritSect);
1873 _engineStatisticsPtr->SetLastError(
1874 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1875 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001876 output_file_player_->StopPlayingFile();
1877 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001878 return -1;
1879 }
1880
1881 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001882}
1883
niklase@google.com470e71d2011-07-07 08:21:25 +00001884int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001885 bool loop,
1886 FileFormats format,
1887 int startPosition,
1888 float volumeScaling,
1889 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001890 const CodecInst* codecInst) {
1891 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1892 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1893 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1894 "stopPosition=%d)",
1895 fileName, loop, format, volumeScaling, startPosition,
1896 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001897
kwiberg55b97fe2016-01-28 05:22:45 -08001898 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001899
kwiberg55b97fe2016-01-28 05:22:45 -08001900 if (channel_state_.Get().input_file_playing) {
1901 _engineStatisticsPtr->SetLastError(
1902 VE_ALREADY_PLAYING, kTraceWarning,
1903 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001904 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001905 }
1906
1907 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001908 if (input_file_player_) {
1909 input_file_player_->RegisterModuleFileCallback(NULL);
1910 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001911 }
1912
1913 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001914 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001915 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001916
kwiberg5a25d952016-08-17 07:31:12 -07001917 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001918 _engineStatisticsPtr->SetLastError(
1919 VE_INVALID_ARGUMENT, kTraceError,
1920 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1921 return -1;
1922 }
1923
1924 const uint32_t notificationTime(0);
1925
kwiberg5a25d952016-08-17 07:31:12 -07001926 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001927 fileName, loop, startPosition, volumeScaling, notificationTime,
1928 stopPosition, (const CodecInst*)codecInst) != 0) {
1929 _engineStatisticsPtr->SetLastError(
1930 VE_BAD_FILE, kTraceError,
1931 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001932 input_file_player_->StopPlayingFile();
1933 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001934 return -1;
1935 }
kwiberg5a25d952016-08-17 07:31:12 -07001936 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001937 channel_state_.SetInputFilePlaying(true);
1938
1939 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001940}
1941
1942int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001943 FileFormats format,
1944 int startPosition,
1945 float volumeScaling,
1946 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001947 const CodecInst* codecInst) {
1948 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1949 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1950 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1951 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001952
kwiberg55b97fe2016-01-28 05:22:45 -08001953 if (stream == NULL) {
1954 _engineStatisticsPtr->SetLastError(
1955 VE_BAD_FILE, kTraceError,
1956 "StartPlayingFileAsMicrophone NULL as input stream");
1957 return -1;
1958 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001959
kwiberg55b97fe2016-01-28 05:22:45 -08001960 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001961
kwiberg55b97fe2016-01-28 05:22:45 -08001962 if (channel_state_.Get().input_file_playing) {
1963 _engineStatisticsPtr->SetLastError(
1964 VE_ALREADY_PLAYING, kTraceWarning,
1965 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001966 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001967 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001968
kwiberg55b97fe2016-01-28 05:22:45 -08001969 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001970 if (input_file_player_) {
1971 input_file_player_->RegisterModuleFileCallback(NULL);
1972 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001973 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001974
kwiberg55b97fe2016-01-28 05:22:45 -08001975 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001976 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001977 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001978
kwiberg5a25d952016-08-17 07:31:12 -07001979 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001980 _engineStatisticsPtr->SetLastError(
1981 VE_INVALID_ARGUMENT, kTraceError,
1982 "StartPlayingInputFile() filePlayer format isnot correct");
1983 return -1;
1984 }
1985
1986 const uint32_t notificationTime(0);
1987
kwiberg4ec01d92016-08-22 08:43:54 -07001988 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
1989 notificationTime, stopPosition,
1990 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001991 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1992 "StartPlayingFile() failed to start "
1993 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001994 input_file_player_->StopPlayingFile();
1995 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001996 return -1;
1997 }
1998
kwiberg5a25d952016-08-17 07:31:12 -07001999 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002000 channel_state_.SetInputFilePlaying(true);
2001
2002 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002003}
2004
kwiberg55b97fe2016-01-28 05:22:45 -08002005int Channel::StopPlayingFileAsMicrophone() {
2006 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2007 "Channel::StopPlayingFileAsMicrophone()");
2008
2009 rtc::CritScope cs(&_fileCritSect);
2010
2011 if (!channel_state_.Get().input_file_playing) {
2012 return 0;
2013 }
2014
kwiberg5a25d952016-08-17 07:31:12 -07002015 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002016 _engineStatisticsPtr->SetLastError(
2017 VE_STOP_RECORDING_FAILED, kTraceError,
2018 "StopPlayingFile() could not stop playing");
2019 return -1;
2020 }
kwiberg5a25d952016-08-17 07:31:12 -07002021 input_file_player_->RegisterModuleFileCallback(NULL);
2022 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002023 channel_state_.SetInputFilePlaying(false);
2024
2025 return 0;
2026}
2027
2028int Channel::IsPlayingFileAsMicrophone() const {
2029 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002030}
2031
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002032int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002033 const CodecInst* codecInst) {
2034 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2035 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002036
kwiberg55b97fe2016-01-28 05:22:45 -08002037 if (_outputFileRecording) {
2038 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2039 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002040 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002041 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002042
kwiberg55b97fe2016-01-28 05:22:45 -08002043 FileFormats format;
2044 const uint32_t notificationTime(0); // Not supported in VoE
2045 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002046
kwiberg55b97fe2016-01-28 05:22:45 -08002047 if ((codecInst != NULL) &&
2048 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2049 _engineStatisticsPtr->SetLastError(
2050 VE_BAD_ARGUMENT, kTraceError,
2051 "StartRecordingPlayout() invalid compression");
2052 return (-1);
2053 }
2054 if (codecInst == NULL) {
2055 format = kFileFormatPcm16kHzFile;
2056 codecInst = &dummyCodec;
2057 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2058 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2059 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2060 format = kFileFormatWavFile;
2061 } else {
2062 format = kFileFormatCompressedFile;
2063 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002064
kwiberg55b97fe2016-01-28 05:22:45 -08002065 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002066
kwiberg55b97fe2016-01-28 05:22:45 -08002067 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002068 if (output_file_recorder_) {
2069 output_file_recorder_->RegisterModuleFileCallback(NULL);
2070 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002071 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002072
kwiberg5a25d952016-08-17 07:31:12 -07002073 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002074 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002075 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002076 _engineStatisticsPtr->SetLastError(
2077 VE_INVALID_ARGUMENT, kTraceError,
2078 "StartRecordingPlayout() fileRecorder format isnot correct");
2079 return -1;
2080 }
2081
kwiberg5a25d952016-08-17 07:31:12 -07002082 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002083 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2084 _engineStatisticsPtr->SetLastError(
2085 VE_BAD_FILE, kTraceError,
2086 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002087 output_file_recorder_->StopRecording();
2088 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002089 return -1;
2090 }
kwiberg5a25d952016-08-17 07:31:12 -07002091 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002092 _outputFileRecording = true;
2093
2094 return 0;
2095}
2096
2097int Channel::StartRecordingPlayout(OutStream* stream,
2098 const CodecInst* codecInst) {
2099 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2100 "Channel::StartRecordingPlayout()");
2101
2102 if (_outputFileRecording) {
2103 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2104 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002105 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002106 }
2107
2108 FileFormats format;
2109 const uint32_t notificationTime(0); // Not supported in VoE
2110 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2111
2112 if (codecInst != NULL && codecInst->channels != 1) {
2113 _engineStatisticsPtr->SetLastError(
2114 VE_BAD_ARGUMENT, kTraceError,
2115 "StartRecordingPlayout() invalid compression");
2116 return (-1);
2117 }
2118 if (codecInst == NULL) {
2119 format = kFileFormatPcm16kHzFile;
2120 codecInst = &dummyCodec;
2121 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2122 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2123 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2124 format = kFileFormatWavFile;
2125 } else {
2126 format = kFileFormatCompressedFile;
2127 }
2128
2129 rtc::CritScope cs(&_fileCritSect);
2130
2131 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002132 if (output_file_recorder_) {
2133 output_file_recorder_->RegisterModuleFileCallback(NULL);
2134 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002135 }
2136
kwiberg5a25d952016-08-17 07:31:12 -07002137 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002138 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002139 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002140 _engineStatisticsPtr->SetLastError(
2141 VE_INVALID_ARGUMENT, kTraceError,
2142 "StartRecordingPlayout() fileRecorder format isnot correct");
2143 return -1;
2144 }
2145
kwiberg4ec01d92016-08-22 08:43:54 -07002146 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002147 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002148 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2149 "StartRecordingPlayout() failed to "
2150 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002151 output_file_recorder_->StopRecording();
2152 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002153 return -1;
2154 }
2155
kwiberg5a25d952016-08-17 07:31:12 -07002156 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002157 _outputFileRecording = true;
2158
2159 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002160}
2161
kwiberg55b97fe2016-01-28 05:22:45 -08002162int Channel::StopRecordingPlayout() {
2163 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2164 "Channel::StopRecordingPlayout()");
2165
2166 if (!_outputFileRecording) {
2167 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2168 "StopRecordingPlayout() isnot recording");
2169 return -1;
2170 }
2171
2172 rtc::CritScope cs(&_fileCritSect);
2173
kwiberg5a25d952016-08-17 07:31:12 -07002174 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002175 _engineStatisticsPtr->SetLastError(
2176 VE_STOP_RECORDING_FAILED, kTraceError,
2177 "StopRecording() could not stop recording");
2178 return (-1);
2179 }
kwiberg5a25d952016-08-17 07:31:12 -07002180 output_file_recorder_->RegisterModuleFileCallback(NULL);
2181 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002182 _outputFileRecording = false;
2183
2184 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002185}
2186
kwiberg55b97fe2016-01-28 05:22:45 -08002187void Channel::SetMixWithMicStatus(bool mix) {
2188 rtc::CritScope cs(&_fileCritSect);
2189 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002190}
2191
kwiberg55b97fe2016-01-28 05:22:45 -08002192int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2193 int8_t currentLevel = _outputAudioLevel.Level();
2194 level = static_cast<int32_t>(currentLevel);
2195 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002196}
2197
kwiberg55b97fe2016-01-28 05:22:45 -08002198int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2199 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2200 level = static_cast<int32_t>(currentLevel);
2201 return 0;
2202}
2203
solenberg1c2af8e2016-03-24 10:36:00 -07002204int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002205 rtc::CritScope cs(&volume_settings_critsect_);
2206 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002207 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002208 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002209 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002210}
2211
solenberg1c2af8e2016-03-24 10:36:00 -07002212bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002213 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002214 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002215}
2216
kwiberg55b97fe2016-01-28 05:22:45 -08002217int Channel::SetOutputVolumePan(float left, float right) {
2218 rtc::CritScope cs(&volume_settings_critsect_);
2219 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002220 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002221 _panLeft = left;
2222 _panRight = right;
2223 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002224}
2225
kwiberg55b97fe2016-01-28 05:22:45 -08002226int Channel::GetOutputVolumePan(float& left, float& right) const {
2227 rtc::CritScope cs(&volume_settings_critsect_);
2228 left = _panLeft;
2229 right = _panRight;
2230 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002231}
2232
kwiberg55b97fe2016-01-28 05:22:45 -08002233int Channel::SetChannelOutputVolumeScaling(float scaling) {
2234 rtc::CritScope cs(&volume_settings_critsect_);
2235 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002236 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002237 _outputGain = scaling;
2238 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002239}
2240
kwiberg55b97fe2016-01-28 05:22:45 -08002241int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2242 rtc::CritScope cs(&volume_settings_critsect_);
2243 scaling = _outputGain;
2244 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002245}
2246
solenberg8842c3e2016-03-11 03:06:41 -08002247int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002248 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002249 "Channel::SendTelephoneEventOutband(...)");
2250 RTC_DCHECK_LE(0, event);
2251 RTC_DCHECK_GE(255, event);
2252 RTC_DCHECK_LE(0, duration_ms);
2253 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002254 if (!Sending()) {
2255 return -1;
2256 }
solenberg8842c3e2016-03-11 03:06:41 -08002257 if (_rtpRtcpModule->SendTelephoneEventOutband(
2258 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002259 _engineStatisticsPtr->SetLastError(
2260 VE_SEND_DTMF_FAILED, kTraceWarning,
2261 "SendTelephoneEventOutband() failed to send event");
2262 return -1;
2263 }
2264 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002265}
2266
solenberg31642aa2016-03-14 08:00:37 -07002267int Channel::SetSendTelephoneEventPayloadType(int payload_type) {
kwiberg55b97fe2016-01-28 05:22:45 -08002268 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002269 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002270 RTC_DCHECK_LE(0, payload_type);
2271 RTC_DCHECK_GE(127, payload_type);
2272 CodecInst codec = {0};
kwiberg55b97fe2016-01-28 05:22:45 -08002273 codec.plfreq = 8000;
solenberg31642aa2016-03-14 08:00:37 -07002274 codec.pltype = payload_type;
kwiberg55b97fe2016-01-28 05:22:45 -08002275 memcpy(codec.plname, "telephone-event", 16);
2276 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2277 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2278 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2279 _engineStatisticsPtr->SetLastError(
2280 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2281 "SetSendTelephoneEventPayloadType() failed to register send"
2282 "payload type");
2283 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002284 }
kwiberg55b97fe2016-01-28 05:22:45 -08002285 }
kwiberg55b97fe2016-01-28 05:22:45 -08002286 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002287}
2288
kwiberg55b97fe2016-01-28 05:22:45 -08002289int Channel::VoiceActivityIndicator(int& activity) {
2290 activity = _sendFrameType;
2291 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002292}
2293
kwiberg55b97fe2016-01-28 05:22:45 -08002294int Channel::SetLocalSSRC(unsigned int ssrc) {
2295 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2296 "Channel::SetLocalSSRC()");
2297 if (channel_state_.Get().sending) {
2298 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2299 "SetLocalSSRC() already sending");
2300 return -1;
2301 }
2302 _rtpRtcpModule->SetSSRC(ssrc);
2303 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002304}
2305
kwiberg55b97fe2016-01-28 05:22:45 -08002306int Channel::GetLocalSSRC(unsigned int& ssrc) {
2307 ssrc = _rtpRtcpModule->SSRC();
2308 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002309}
2310
kwiberg55b97fe2016-01-28 05:22:45 -08002311int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2312 ssrc = rtp_receiver_->SSRC();
2313 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002314}
2315
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002316int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002317 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002318 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002319}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002320
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002321int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2322 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002323 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2324 if (enable &&
2325 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2326 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002327 return -1;
2328 }
2329 return 0;
2330}
2331
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002332int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2333 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2334}
2335
2336int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2337 rtp_header_parser_->DeregisterRtpHeaderExtension(
2338 kRtpExtensionAbsoluteSendTime);
kwiberg55b97fe2016-01-28 05:22:45 -08002339 if (enable &&
2340 !rtp_header_parser_->RegisterRtpHeaderExtension(
2341 kRtpExtensionAbsoluteSendTime, id)) {
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002342 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002343 }
2344 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002345}
2346
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002347void Channel::EnableSendTransportSequenceNumber(int id) {
2348 int ret =
2349 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2350 RTC_DCHECK_EQ(0, ret);
2351}
2352
stefan3313ec92016-01-21 06:32:43 -08002353void Channel::EnableReceiveTransportSequenceNumber(int id) {
2354 rtp_header_parser_->DeregisterRtpHeaderExtension(
2355 kRtpExtensionTransportSequenceNumber);
2356 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2357 kRtpExtensionTransportSequenceNumber, id);
2358 RTC_DCHECK(ret);
2359}
2360
stefanbba9dec2016-02-01 04:39:55 -08002361void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002362 RtpPacketSender* rtp_packet_sender,
2363 TransportFeedbackObserver* transport_feedback_observer,
2364 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002365 RTC_DCHECK(rtp_packet_sender);
2366 RTC_DCHECK(transport_feedback_observer);
2367 RTC_DCHECK(packet_router && !packet_router_);
2368 feedback_observer_proxy_->SetTransportFeedbackObserver(
2369 transport_feedback_observer);
2370 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2371 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2372 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002373 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002374 packet_router_ = packet_router;
2375}
2376
stefanbba9dec2016-02-01 04:39:55 -08002377void Channel::RegisterReceiverCongestionControlObjects(
2378 PacketRouter* packet_router) {
2379 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002380 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002381 packet_router_ = packet_router;
2382}
2383
2384void Channel::ResetCongestionControlObjects() {
2385 RTC_DCHECK(packet_router_);
2386 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2387 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2388 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002389 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002390 packet_router_ = nullptr;
2391 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2392}
2393
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002394void Channel::SetRTCPStatus(bool enable) {
2395 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2396 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002397 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002398}
2399
kwiberg55b97fe2016-01-28 05:22:45 -08002400int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002401 RtcpMode method = _rtpRtcpModule->RTCP();
2402 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002403 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002404}
2405
kwiberg55b97fe2016-01-28 05:22:45 -08002406int Channel::SetRTCP_CNAME(const char cName[256]) {
2407 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2408 "Channel::SetRTCP_CNAME()");
2409 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2410 _engineStatisticsPtr->SetLastError(
2411 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2412 "SetRTCP_CNAME() failed to set RTCP CNAME");
2413 return -1;
2414 }
2415 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002416}
2417
kwiberg55b97fe2016-01-28 05:22:45 -08002418int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2419 if (cName == NULL) {
2420 _engineStatisticsPtr->SetLastError(
2421 VE_INVALID_ARGUMENT, kTraceError,
2422 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2423 return -1;
2424 }
2425 char cname[RTCP_CNAME_SIZE];
2426 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2427 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2428 _engineStatisticsPtr->SetLastError(
2429 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2430 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2431 return -1;
2432 }
2433 strcpy(cName, cname);
2434 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002435}
2436
kwiberg55b97fe2016-01-28 05:22:45 -08002437int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2438 unsigned int& NTPLow,
2439 unsigned int& timestamp,
2440 unsigned int& playoutTimestamp,
2441 unsigned int* jitter,
2442 unsigned short* fractionLost) {
2443 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002444
kwiberg55b97fe2016-01-28 05:22:45 -08002445 RTCPSenderInfo senderInfo;
2446 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2447 _engineStatisticsPtr->SetLastError(
2448 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2449 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2450 "side");
2451 return -1;
2452 }
2453
2454 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2455 // and octet count)
2456 NTPHigh = senderInfo.NTPseconds;
2457 NTPLow = senderInfo.NTPfraction;
2458 timestamp = senderInfo.RTPtimeStamp;
2459
2460 // --- Locally derived information
2461
2462 // This value is updated on each incoming RTCP packet (0 when no packet
2463 // has been received)
2464 playoutTimestamp = playout_timestamp_rtcp_;
2465
2466 if (NULL != jitter || NULL != fractionLost) {
2467 // Get all RTCP receiver report blocks that have been received on this
2468 // channel. If we receive RTP packets from a remote source we know the
2469 // remote SSRC and use the report block from him.
2470 // Otherwise use the first report block.
2471 std::vector<RTCPReportBlock> remote_stats;
2472 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2473 remote_stats.empty()) {
2474 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2475 "GetRemoteRTCPData() failed to measure statistics due"
2476 " to lack of received RTP and/or RTCP packets");
2477 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002478 }
2479
kwiberg55b97fe2016-01-28 05:22:45 -08002480 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2481 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2482 for (; it != remote_stats.end(); ++it) {
2483 if (it->remoteSSRC == remoteSSRC)
2484 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002485 }
kwiberg55b97fe2016-01-28 05:22:45 -08002486
2487 if (it == remote_stats.end()) {
2488 // If we have not received any RTCP packets from this SSRC it probably
2489 // means that we have not received any RTP packets.
2490 // Use the first received report block instead.
2491 it = remote_stats.begin();
2492 remoteSSRC = it->remoteSSRC;
2493 }
2494
2495 if (jitter) {
2496 *jitter = it->jitter;
2497 }
2498
2499 if (fractionLost) {
2500 *fractionLost = it->fractionLost;
2501 }
2502 }
2503 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002504}
2505
kwiberg55b97fe2016-01-28 05:22:45 -08002506int Channel::SendApplicationDefinedRTCPPacket(
2507 unsigned char subType,
2508 unsigned int name,
2509 const char* data,
2510 unsigned short dataLengthInBytes) {
2511 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2512 "Channel::SendApplicationDefinedRTCPPacket()");
2513 if (!channel_state_.Get().sending) {
2514 _engineStatisticsPtr->SetLastError(
2515 VE_NOT_SENDING, kTraceError,
2516 "SendApplicationDefinedRTCPPacket() not sending");
2517 return -1;
2518 }
2519 if (NULL == data) {
2520 _engineStatisticsPtr->SetLastError(
2521 VE_INVALID_ARGUMENT, kTraceError,
2522 "SendApplicationDefinedRTCPPacket() invalid data value");
2523 return -1;
2524 }
2525 if (dataLengthInBytes % 4 != 0) {
2526 _engineStatisticsPtr->SetLastError(
2527 VE_INVALID_ARGUMENT, kTraceError,
2528 "SendApplicationDefinedRTCPPacket() invalid length value");
2529 return -1;
2530 }
2531 RtcpMode status = _rtpRtcpModule->RTCP();
2532 if (status == RtcpMode::kOff) {
2533 _engineStatisticsPtr->SetLastError(
2534 VE_RTCP_ERROR, kTraceError,
2535 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2536 return -1;
2537 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002538
kwiberg55b97fe2016-01-28 05:22:45 -08002539 // Create and schedule the RTCP APP packet for transmission
2540 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2541 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2542 _engineStatisticsPtr->SetLastError(
2543 VE_SEND_ERROR, kTraceError,
2544 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2545 return -1;
2546 }
2547 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002548}
2549
kwiberg55b97fe2016-01-28 05:22:45 -08002550int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2551 unsigned int& maxJitterMs,
2552 unsigned int& discardedPackets) {
2553 // The jitter statistics is updated for each received RTP packet and is
2554 // based on received packets.
2555 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2556 // If RTCP is off, there is no timed thread in the RTCP module regularly
2557 // generating new stats, trigger the update manually here instead.
2558 StreamStatistician* statistician =
2559 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2560 if (statistician) {
2561 // Don't use returned statistics, use data from proxy instead so that
2562 // max jitter can be fetched atomically.
2563 RtcpStatistics s;
2564 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002565 }
kwiberg55b97fe2016-01-28 05:22:45 -08002566 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002567
kwiberg55b97fe2016-01-28 05:22:45 -08002568 ChannelStatistics stats = statistics_proxy_->GetStats();
2569 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2570 if (playoutFrequency > 0) {
2571 // Scale RTP statistics given the current playout frequency
2572 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2573 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2574 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002575
kwiberg55b97fe2016-01-28 05:22:45 -08002576 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002577
kwiberg55b97fe2016-01-28 05:22:45 -08002578 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002579}
2580
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002581int Channel::GetRemoteRTCPReportBlocks(
2582 std::vector<ReportBlock>* report_blocks) {
2583 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002584 _engineStatisticsPtr->SetLastError(
2585 VE_INVALID_ARGUMENT, kTraceError,
2586 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002587 return -1;
2588 }
2589
2590 // Get the report blocks from the latest received RTCP Sender or Receiver
2591 // Report. Each element in the vector contains the sender's SSRC and a
2592 // report block according to RFC 3550.
2593 std::vector<RTCPReportBlock> rtcp_report_blocks;
2594 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002595 return -1;
2596 }
2597
2598 if (rtcp_report_blocks.empty())
2599 return 0;
2600
2601 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2602 for (; it != rtcp_report_blocks.end(); ++it) {
2603 ReportBlock report_block;
2604 report_block.sender_SSRC = it->remoteSSRC;
2605 report_block.source_SSRC = it->sourceSSRC;
2606 report_block.fraction_lost = it->fractionLost;
2607 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2608 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2609 report_block.interarrival_jitter = it->jitter;
2610 report_block.last_SR_timestamp = it->lastSR;
2611 report_block.delay_since_last_SR = it->delaySinceLastSR;
2612 report_blocks->push_back(report_block);
2613 }
2614 return 0;
2615}
2616
kwiberg55b97fe2016-01-28 05:22:45 -08002617int Channel::GetRTPStatistics(CallStatistics& stats) {
2618 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002619
kwiberg55b97fe2016-01-28 05:22:45 -08002620 // The jitter statistics is updated for each received RTP packet and is
2621 // based on received packets.
2622 RtcpStatistics statistics;
2623 StreamStatistician* statistician =
2624 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002625 if (statistician) {
2626 statistician->GetStatistics(&statistics,
2627 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002628 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002629
kwiberg55b97fe2016-01-28 05:22:45 -08002630 stats.fractionLost = statistics.fraction_lost;
2631 stats.cumulativeLost = statistics.cumulative_lost;
2632 stats.extendedMax = statistics.extended_max_sequence_number;
2633 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002634
kwiberg55b97fe2016-01-28 05:22:45 -08002635 // --- RTT
2636 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002637
kwiberg55b97fe2016-01-28 05:22:45 -08002638 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002639
kwiberg55b97fe2016-01-28 05:22:45 -08002640 size_t bytesSent(0);
2641 uint32_t packetsSent(0);
2642 size_t bytesReceived(0);
2643 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002644
kwiberg55b97fe2016-01-28 05:22:45 -08002645 if (statistician) {
2646 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2647 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002648
kwiberg55b97fe2016-01-28 05:22:45 -08002649 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2650 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2651 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2652 " output will not be complete");
2653 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002654
kwiberg55b97fe2016-01-28 05:22:45 -08002655 stats.bytesSent = bytesSent;
2656 stats.packetsSent = packetsSent;
2657 stats.bytesReceived = bytesReceived;
2658 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002659
kwiberg55b97fe2016-01-28 05:22:45 -08002660 // --- Timestamps
2661 {
2662 rtc::CritScope lock(&ts_stats_lock_);
2663 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2664 }
2665 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002666}
2667
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002668int Channel::SetCodecFECStatus(bool enable) {
2669 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2670 "Channel::SetCodecFECStatus()");
2671
kwibergc8d071e2016-04-06 12:22:38 -07002672 if (!codec_manager_.SetCodecFEC(enable) ||
2673 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002674 _engineStatisticsPtr->SetLastError(
2675 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2676 "SetCodecFECStatus() failed to set FEC state");
2677 return -1;
2678 }
2679 return 0;
2680}
2681
2682bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002683 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002684}
2685
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002686void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2687 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002688 // If pacing is enabled we always store packets.
2689 if (!pacing_enabled_)
2690 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002691 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002692 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002693 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002694 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002695 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002696}
2697
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002698// Called when we are missing one or more packets.
2699int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002700 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2701}
2702
kwiberg55b97fe2016-01-28 05:22:45 -08002703uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2704 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2705 "Channel::Demultiplex()");
2706 _audioFrame.CopyFrom(audioFrame);
2707 _audioFrame.id_ = _channelId;
2708 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002709}
2710
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002711void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002712 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002713 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002714 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002715 CodecInst codec;
2716 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002717
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002718 // Never upsample or upmix the capture signal here. This should be done at the
2719 // end of the send chain.
2720 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2721 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2722 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2723 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002724}
2725
kwiberg55b97fe2016-01-28 05:22:45 -08002726uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2727 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2728 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002729
kwiberg55b97fe2016-01-28 05:22:45 -08002730 if (_audioFrame.samples_per_channel_ == 0) {
2731 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2732 "Channel::PrepareEncodeAndSend() invalid audio frame");
2733 return 0xFFFFFFFF;
2734 }
2735
2736 if (channel_state_.Get().input_file_playing) {
2737 MixOrReplaceAudioWithFile(mixingFrequency);
2738 }
2739
solenberg1c2af8e2016-03-24 10:36:00 -07002740 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2741 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002742
2743 if (channel_state_.Get().input_external_media) {
2744 rtc::CritScope cs(&_callbackCritSect);
2745 const bool isStereo = (_audioFrame.num_channels_ == 2);
2746 if (_inputExternalMediaCallbackPtr) {
2747 _inputExternalMediaCallbackPtr->Process(
2748 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
2749 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
2750 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00002751 }
kwiberg55b97fe2016-01-28 05:22:45 -08002752 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002753
kwiberg55b97fe2016-01-28 05:22:45 -08002754 if (_includeAudioLevelIndication) {
2755 size_t length =
2756 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002757 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002758 if (is_muted && previous_frame_muted_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002759 rms_level_.ProcessMuted(length);
2760 } else {
2761 rms_level_.Process(_audioFrame.data_, length);
niklase@google.com470e71d2011-07-07 08:21:25 +00002762 }
kwiberg55b97fe2016-01-28 05:22:45 -08002763 }
solenberg1c2af8e2016-03-24 10:36:00 -07002764 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002765
kwiberg55b97fe2016-01-28 05:22:45 -08002766 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002767}
2768
kwiberg55b97fe2016-01-28 05:22:45 -08002769uint32_t Channel::EncodeAndSend() {
2770 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2771 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002772
kwiberg55b97fe2016-01-28 05:22:45 -08002773 assert(_audioFrame.num_channels_ <= 2);
2774 if (_audioFrame.samples_per_channel_ == 0) {
2775 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2776 "Channel::EncodeAndSend() invalid audio frame");
2777 return 0xFFFFFFFF;
2778 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002779
kwiberg55b97fe2016-01-28 05:22:45 -08002780 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002781
kwiberg55b97fe2016-01-28 05:22:45 -08002782 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002783
kwiberg55b97fe2016-01-28 05:22:45 -08002784 // The ACM resamples internally.
2785 _audioFrame.timestamp_ = _timeStamp;
2786 // This call will trigger AudioPacketizationCallback::SendData if encoding
2787 // is done and payload is ready for packetization and transmission.
2788 // Otherwise, it will return without invoking the callback.
2789 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2790 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2791 "Channel::EncodeAndSend() ACM encoding failed");
2792 return 0xFFFFFFFF;
2793 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002794
kwiberg55b97fe2016-01-28 05:22:45 -08002795 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2796 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002797}
2798
Minyue2013aec2015-05-13 14:14:42 +02002799void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002800 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002801 Channel* channel = associate_send_channel_.channel();
2802 if (channel && channel->ChannelId() == channel_id) {
2803 // If this channel is associated with a send channel of the specified
2804 // Channel ID, disassociate with it.
2805 ChannelOwner ref(NULL);
2806 associate_send_channel_ = ref;
2807 }
2808}
2809
ivoc14d5dbe2016-07-04 07:06:55 -07002810void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2811 event_log_proxy_->SetEventLog(event_log);
2812}
2813
kwiberg55b97fe2016-01-28 05:22:45 -08002814int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
2815 VoEMediaProcess& processObject) {
2816 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2817 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002818
kwiberg55b97fe2016-01-28 05:22:45 -08002819 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002820
kwiberg55b97fe2016-01-28 05:22:45 -08002821 if (kPlaybackPerChannel == type) {
2822 if (_outputExternalMediaCallbackPtr) {
2823 _engineStatisticsPtr->SetLastError(
2824 VE_INVALID_OPERATION, kTraceError,
2825 "Channel::RegisterExternalMediaProcessing() "
2826 "output external media already enabled");
2827 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002828 }
kwiberg55b97fe2016-01-28 05:22:45 -08002829 _outputExternalMediaCallbackPtr = &processObject;
2830 _outputExternalMedia = true;
2831 } else if (kRecordingPerChannel == type) {
2832 if (_inputExternalMediaCallbackPtr) {
2833 _engineStatisticsPtr->SetLastError(
2834 VE_INVALID_OPERATION, kTraceError,
2835 "Channel::RegisterExternalMediaProcessing() "
2836 "output external media already enabled");
2837 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002838 }
kwiberg55b97fe2016-01-28 05:22:45 -08002839 _inputExternalMediaCallbackPtr = &processObject;
2840 channel_state_.SetInputExternalMedia(true);
2841 }
2842 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002843}
2844
kwiberg55b97fe2016-01-28 05:22:45 -08002845int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
2846 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2847 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002848
kwiberg55b97fe2016-01-28 05:22:45 -08002849 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002850
kwiberg55b97fe2016-01-28 05:22:45 -08002851 if (kPlaybackPerChannel == type) {
2852 if (!_outputExternalMediaCallbackPtr) {
2853 _engineStatisticsPtr->SetLastError(
2854 VE_INVALID_OPERATION, kTraceWarning,
2855 "Channel::DeRegisterExternalMediaProcessing() "
2856 "output external media already disabled");
2857 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002858 }
kwiberg55b97fe2016-01-28 05:22:45 -08002859 _outputExternalMedia = false;
2860 _outputExternalMediaCallbackPtr = NULL;
2861 } else if (kRecordingPerChannel == type) {
2862 if (!_inputExternalMediaCallbackPtr) {
2863 _engineStatisticsPtr->SetLastError(
2864 VE_INVALID_OPERATION, kTraceWarning,
2865 "Channel::DeRegisterExternalMediaProcessing() "
2866 "input external media already disabled");
2867 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002868 }
kwiberg55b97fe2016-01-28 05:22:45 -08002869 channel_state_.SetInputExternalMedia(false);
2870 _inputExternalMediaCallbackPtr = NULL;
2871 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002872
kwiberg55b97fe2016-01-28 05:22:45 -08002873 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002874}
2875
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002876int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08002877 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2878 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002879
kwiberg55b97fe2016-01-28 05:22:45 -08002880 if (channel_state_.Get().playing) {
2881 _engineStatisticsPtr->SetLastError(
2882 VE_INVALID_OPERATION, kTraceError,
2883 "Channel::SetExternalMixing() "
2884 "external mixing cannot be changed while playing.");
2885 return -1;
2886 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002887
kwiberg55b97fe2016-01-28 05:22:45 -08002888 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002889
kwiberg55b97fe2016-01-28 05:22:45 -08002890 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002891}
2892
kwiberg55b97fe2016-01-28 05:22:45 -08002893int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2894 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002895}
2896
wu@webrtc.org24301a62013-12-13 19:17:43 +00002897void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2898 audio_coding_->GetDecodingCallStatistics(stats);
2899}
2900
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002901bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
2902 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08002903 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07002904 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002905 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002906 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002907}
2908
solenberg358057b2015-11-27 10:46:42 -08002909uint32_t Channel::GetDelayEstimate() const {
2910 int jitter_buffer_delay_ms = 0;
2911 int playout_buffer_delay_ms = 0;
2912 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
2913 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
2914}
2915
deadbeef74375882015-08-13 12:09:10 -07002916int Channel::LeastRequiredDelayMs() const {
2917 return audio_coding_->LeastRequiredDelayMs();
2918}
2919
kwiberg55b97fe2016-01-28 05:22:45 -08002920int Channel::SetMinimumPlayoutDelay(int delayMs) {
2921 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2922 "Channel::SetMinimumPlayoutDelay()");
2923 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2924 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2925 _engineStatisticsPtr->SetLastError(
2926 VE_INVALID_ARGUMENT, kTraceError,
2927 "SetMinimumPlayoutDelay() invalid min delay");
2928 return -1;
2929 }
2930 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2931 _engineStatisticsPtr->SetLastError(
2932 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2933 "SetMinimumPlayoutDelay() failed to set min playout delay");
2934 return -1;
2935 }
2936 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002937}
2938
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002939int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002940 uint32_t playout_timestamp_rtp = 0;
2941 {
tommi31fc21f2016-01-21 10:37:37 -08002942 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002943 playout_timestamp_rtp = playout_timestamp_rtp_;
2944 }
kwiberg55b97fe2016-01-28 05:22:45 -08002945 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002946 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002947 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002948 "GetPlayoutTimestamp() failed to retrieve timestamp");
2949 return -1;
2950 }
deadbeef74375882015-08-13 12:09:10 -07002951 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002952 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002953}
2954
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002955int Channel::SetInitTimestamp(unsigned int timestamp) {
2956 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002957 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002958 if (channel_state_.Get().sending) {
2959 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
2960 "SetInitTimestamp() already sending");
2961 return -1;
2962 }
2963 _rtpRtcpModule->SetStartTimestamp(timestamp);
2964 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002965}
2966
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002967int Channel::SetInitSequenceNumber(short sequenceNumber) {
2968 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2969 "Channel::SetInitSequenceNumber()");
2970 if (channel_state_.Get().sending) {
2971 _engineStatisticsPtr->SetLastError(
2972 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
2973 return -1;
2974 }
2975 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
2976 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002977}
2978
kwiberg55b97fe2016-01-28 05:22:45 -08002979int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2980 RtpReceiver** rtp_receiver) const {
2981 *rtpRtcpModule = _rtpRtcpModule.get();
2982 *rtp_receiver = rtp_receiver_.get();
2983 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002984}
2985
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002986// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2987// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002988int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002989 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002990 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002991
kwiberg55b97fe2016-01-28 05:22:45 -08002992 {
2993 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002994
kwiberg5a25d952016-08-17 07:31:12 -07002995 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002996 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2997 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2998 " doesnt exist");
2999 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003000 }
3001
kwiberg4ec01d92016-08-22 08:43:54 -07003002 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07003003 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003004 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3005 "Channel::MixOrReplaceAudioWithFile() file mixing "
3006 "failed");
3007 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003008 }
kwiberg55b97fe2016-01-28 05:22:45 -08003009 if (fileSamples == 0) {
3010 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3011 "Channel::MixOrReplaceAudioWithFile() file is ended");
3012 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003013 }
kwiberg55b97fe2016-01-28 05:22:45 -08003014 }
3015
3016 assert(_audioFrame.samples_per_channel_ == fileSamples);
3017
3018 if (_mixFileWithMicrophone) {
3019 // Currently file stream is always mono.
3020 // TODO(xians): Change the code when FilePlayer supports real stereo.
3021 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3022 1, fileSamples);
3023 } else {
3024 // Replace ACM audio with file.
3025 // Currently file stream is always mono.
3026 // TODO(xians): Change the code when FilePlayer supports real stereo.
3027 _audioFrame.UpdateFrame(
3028 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3029 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3030 }
3031 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003032}
3033
kwiberg55b97fe2016-01-28 05:22:45 -08003034int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3035 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003036
kwibergb7f89d62016-02-17 10:04:18 -08003037 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003038 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003039
kwiberg55b97fe2016-01-28 05:22:45 -08003040 {
3041 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003042
kwiberg5a25d952016-08-17 07:31:12 -07003043 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003044 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3045 "Channel::MixAudioWithFile() file mixing failed");
3046 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003047 }
3048
kwiberg55b97fe2016-01-28 05:22:45 -08003049 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003050 if (output_file_player_->Get10msAudioFromFile(
3051 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003052 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3053 "Channel::MixAudioWithFile() file mixing failed");
3054 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003055 }
kwiberg55b97fe2016-01-28 05:22:45 -08003056 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003057
kwiberg55b97fe2016-01-28 05:22:45 -08003058 if (audioFrame.samples_per_channel_ == fileSamples) {
3059 // Currently file stream is always mono.
3060 // TODO(xians): Change the code when FilePlayer supports real stereo.
3061 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3062 fileSamples);
3063 } else {
3064 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3065 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3066 ") != "
3067 "fileSamples(%" PRIuS ")",
3068 audioFrame.samples_per_channel_, fileSamples);
3069 return -1;
3070 }
3071
3072 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003073}
3074
deadbeef74375882015-08-13 12:09:10 -07003075void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003076 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003077
henrik.lundin96bd5022016-04-06 04:13:56 -07003078 if (!jitter_buffer_playout_timestamp_) {
3079 // This can happen if this channel has not received any RTP packets. In
3080 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003081 return;
3082 }
3083
3084 uint16_t delay_ms = 0;
3085 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003086 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003087 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3088 " delay from the ADM");
3089 _engineStatisticsPtr->SetLastError(
3090 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3091 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3092 return;
3093 }
3094
henrik.lundin96bd5022016-04-06 04:13:56 -07003095 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3096 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003097
3098 // Remove the playout delay.
henrik.lundin96bd5022016-04-06 04:13:56 -07003099 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003100
kwiberg55b97fe2016-01-28 05:22:45 -08003101 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003102 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003103 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003104
3105 {
tommi31fc21f2016-01-21 10:37:37 -08003106 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003107 if (rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003108 playout_timestamp_rtcp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003109 } else {
henrik.lundin96bd5022016-04-06 04:13:56 -07003110 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003111 }
3112 playout_delay_ms_ = delay_ms;
3113 }
3114}
3115
kwiberg55b97fe2016-01-28 05:22:45 -08003116void Channel::RegisterReceiveCodecsToRTPModule() {
3117 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3118 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003119
kwiberg55b97fe2016-01-28 05:22:45 -08003120 CodecInst codec;
3121 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003122
kwiberg55b97fe2016-01-28 05:22:45 -08003123 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3124 // Open up the RTP/RTCP receiver for all supported codecs
3125 if ((audio_coding_->Codec(idx, &codec) == -1) ||
3126 (rtp_receiver_->RegisterReceivePayload(
3127 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3128 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
3129 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3130 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3131 " to register %s (%d/%d/%" PRIuS
3132 "/%d) to RTP/RTCP "
3133 "receiver",
3134 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3135 codec.rate);
3136 } else {
3137 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3138 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3139 "(%d/%d/%" PRIuS
3140 "/%d) has been added to the RTP/RTCP "
3141 "receiver",
3142 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3143 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003144 }
kwiberg55b97fe2016-01-28 05:22:45 -08003145 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003146}
3147
kwiberg55b97fe2016-01-28 05:22:45 -08003148int Channel::SetSendRtpHeaderExtension(bool enable,
3149 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003150 unsigned char id) {
3151 int error = 0;
3152 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3153 if (enable) {
3154 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3155 }
3156 return error;
3157}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003158
henrik.lundinb3e30012016-08-31 14:09:51 -07003159int32_t Channel::GetPlayoutFrequency() const {
wu@webrtc.org94454b72014-06-05 20:34:08 +00003160 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
3161 CodecInst current_recive_codec;
3162 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
3163 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
3164 // Even though the actual sampling rate for G.722 audio is
3165 // 16,000 Hz, the RTP clock rate for the G722 payload format is
3166 // 8,000 Hz because that value was erroneously assigned in
3167 // RFC 1890 and must remain unchanged for backward compatibility.
3168 playout_frequency = 8000;
3169 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
3170 // We are resampling Opus internally to 32,000 Hz until all our
3171 // DSP routines can operate at 48,000 Hz, but the RTP clock
3172 // rate for the Opus payload format is standardized to 48,000 Hz,
3173 // because that is the maximum supported decoding sampling rate.
3174 playout_frequency = 48000;
3175 }
3176 }
3177 return playout_frequency;
3178}
3179
Minyue2013aec2015-05-13 14:14:42 +02003180int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003181 RtcpMode method = _rtpRtcpModule->RTCP();
3182 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003183 return 0;
3184 }
3185 std::vector<RTCPReportBlock> report_blocks;
3186 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003187
3188 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003189 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003190 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003191 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003192 Channel* channel = associate_send_channel_.channel();
3193 // Tries to get RTT from an associated channel. This is important for
3194 // receive-only channels.
3195 if (channel) {
3196 // To prevent infinite recursion and deadlock, calling GetRTT of
3197 // associate channel should always use "false" for argument:
3198 // |allow_associate_channel|.
3199 rtt = channel->GetRTT(false);
3200 }
3201 }
3202 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003203 }
3204
3205 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3206 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3207 for (; it != report_blocks.end(); ++it) {
3208 if (it->remoteSSRC == remoteSSRC)
3209 break;
3210 }
3211 if (it == report_blocks.end()) {
3212 // We have not received packets with SSRC matching the report blocks.
3213 // To calculate RTT we try with the SSRC of the first report block.
3214 // This is very important for send-only channels where we don't know
3215 // the SSRC of the other end.
3216 remoteSSRC = report_blocks[0].remoteSSRC;
3217 }
Minyue2013aec2015-05-13 14:14:42 +02003218
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003219 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003220 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003221 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003222 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3223 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003224 return 0;
3225 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003226 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003227}
3228
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003229} // namespace voe
3230} // namespace webrtc