blob: 97132995ba1fe8723d54240ee9d9892fa652b7b7 [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
michaelte0729c52016-10-11 00:29:27 -0700160class RtcpRttStatsProxy final : public RtcpRttStats {
161 public:
162 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
163
164 void OnRttUpdate(int64_t rtt) override {
165 rtc::CritScope lock(&crit_);
166 if (rtcp_rtt_stats_)
167 rtcp_rtt_stats_->OnRttUpdate(rtt);
168 }
169
170 int64_t LastProcessedRtt() const override {
171 rtc::CritScope lock(&crit_);
172 if (rtcp_rtt_stats_ == nullptr)
173 return 0;
174 return rtcp_rtt_stats_->LastProcessedRtt();
175 }
176
177 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
178 rtc::CritScope lock(&crit_);
179 rtcp_rtt_stats_ = rtcp_rtt_stats;
180 }
181
182 private:
183 rtc::CriticalSection crit_;
184 RtcpRttStats* rtcp_rtt_stats_ GUARDED_BY(crit_);
185 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
186};
187
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100188class TransportFeedbackProxy : public TransportFeedbackObserver {
189 public:
190 TransportFeedbackProxy() : feedback_observer_(nullptr) {
191 pacer_thread_.DetachFromThread();
192 network_thread_.DetachFromThread();
193 }
194
195 void SetTransportFeedbackObserver(
196 TransportFeedbackObserver* feedback_observer) {
197 RTC_DCHECK(thread_checker_.CalledOnValidThread());
198 rtc::CritScope lock(&crit_);
199 feedback_observer_ = feedback_observer;
200 }
201
202 // Implements TransportFeedbackObserver.
203 void AddPacket(uint16_t sequence_number,
204 size_t length,
philipela1ed0b32016-06-01 06:31:17 -0700205 int probe_cluster_id) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100206 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
207 rtc::CritScope lock(&crit_);
208 if (feedback_observer_)
pbos2169d8b2016-06-20 11:53:02 -0700209 feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100210 }
211 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
212 RTC_DCHECK(network_thread_.CalledOnValidThread());
213 rtc::CritScope lock(&crit_);
Stefan Holmer60e43462016-09-07 09:58:20 +0200214 feedback_observer_->OnTransportFeedback(feedback);
215 }
216 std::vector<PacketInfo> GetTransportFeedbackVector() const override {
217 RTC_NOTREACHED();
218 return std::vector<PacketInfo>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100219 }
220
221 private:
222 rtc::CriticalSection crit_;
223 rtc::ThreadChecker thread_checker_;
224 rtc::ThreadChecker pacer_thread_;
225 rtc::ThreadChecker network_thread_;
226 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
227};
228
229class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
230 public:
231 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
232 pacer_thread_.DetachFromThread();
233 }
234
235 void SetSequenceNumberAllocator(
236 TransportSequenceNumberAllocator* seq_num_allocator) {
237 RTC_DCHECK(thread_checker_.CalledOnValidThread());
238 rtc::CritScope lock(&crit_);
239 seq_num_allocator_ = seq_num_allocator;
240 }
241
242 // Implements TransportSequenceNumberAllocator.
243 uint16_t AllocateSequenceNumber() override {
244 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
245 rtc::CritScope lock(&crit_);
246 if (!seq_num_allocator_)
247 return 0;
248 return seq_num_allocator_->AllocateSequenceNumber();
249 }
250
251 private:
252 rtc::CriticalSection crit_;
253 rtc::ThreadChecker thread_checker_;
254 rtc::ThreadChecker pacer_thread_;
255 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
256};
257
258class RtpPacketSenderProxy : public RtpPacketSender {
259 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800260 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100261
262 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
263 RTC_DCHECK(thread_checker_.CalledOnValidThread());
264 rtc::CritScope lock(&crit_);
265 rtp_packet_sender_ = rtp_packet_sender;
266 }
267
268 // Implements RtpPacketSender.
269 void InsertPacket(Priority priority,
270 uint32_t ssrc,
271 uint16_t sequence_number,
272 int64_t capture_time_ms,
273 size_t bytes,
274 bool retransmission) override {
275 rtc::CritScope lock(&crit_);
276 if (rtp_packet_sender_) {
277 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
278 capture_time_ms, bytes, retransmission);
279 }
280 }
281
282 private:
283 rtc::ThreadChecker thread_checker_;
284 rtc::CriticalSection crit_;
285 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
286};
287
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000288// Extend the default RTCP statistics struct with max_jitter, defined as the
289// maximum jitter value seen in an RTCP report block.
290struct ChannelStatistics : public RtcpStatistics {
291 ChannelStatistics() : rtcp(), max_jitter(0) {}
292
293 RtcpStatistics rtcp;
294 uint32_t max_jitter;
295};
296
297// Statistics callback, called at each generation of a new RTCP report block.
298class StatisticsProxy : public RtcpStatisticsCallback {
299 public:
tommi31fc21f2016-01-21 10:37:37 -0800300 StatisticsProxy(uint32_t ssrc) : ssrc_(ssrc) {}
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000301 virtual ~StatisticsProxy() {}
302
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000303 void StatisticsUpdated(const RtcpStatistics& statistics,
304 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000305 if (ssrc != ssrc_)
306 return;
307
tommi31fc21f2016-01-21 10:37:37 -0800308 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000309 stats_.rtcp = statistics;
310 if (statistics.jitter > stats_.max_jitter) {
311 stats_.max_jitter = statistics.jitter;
312 }
313 }
314
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000315 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000316
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000317 ChannelStatistics GetStats() {
tommi31fc21f2016-01-21 10:37:37 -0800318 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000319 return stats_;
320 }
321
322 private:
323 // StatisticsUpdated calls are triggered from threads in the RTP module,
324 // while GetStats calls can be triggered from the public voice engine API,
325 // hence synchronization is needed.
tommi31fc21f2016-01-21 10:37:37 -0800326 rtc::CriticalSection stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000327 const uint32_t ssrc_;
328 ChannelStatistics stats_;
329};
330
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000331class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000332 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000333 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
334 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000335
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000336 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
337 // Not used for Voice Engine.
338 }
339
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000340 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
341 int64_t rtt,
342 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000343 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
344 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
345 // report for VoiceEngine?
346 if (report_blocks.empty())
347 return;
348
349 int fraction_lost_aggregate = 0;
350 int total_number_of_packets = 0;
351
352 // If receiving multiple report blocks, calculate the weighted average based
353 // on the number of packets a report refers to.
354 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
355 block_it != report_blocks.end(); ++block_it) {
356 // Find the previous extended high sequence number for this remote SSRC,
357 // to calculate the number of RTP packets this report refers to. Ignore if
358 // we haven't seen this SSRC before.
359 std::map<uint32_t, uint32_t>::iterator seq_num_it =
360 extended_max_sequence_number_.find(block_it->sourceSSRC);
361 int number_of_packets = 0;
362 if (seq_num_it != extended_max_sequence_number_.end()) {
363 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
364 }
365 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
366 total_number_of_packets += number_of_packets;
367
368 extended_max_sequence_number_[block_it->sourceSSRC] =
369 block_it->extendedHighSeqNum;
370 }
371 int weighted_fraction_lost = 0;
372 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800373 weighted_fraction_lost =
374 (fraction_lost_aggregate + total_number_of_packets / 2) /
375 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000376 }
377 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000378 }
379
380 private:
381 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000382 // Maps remote side ssrc to extended highest sequence number received.
383 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000384};
385
kwiberg55b97fe2016-01-28 05:22:45 -0800386int32_t Channel::SendData(FrameType frameType,
387 uint8_t payloadType,
388 uint32_t timeStamp,
389 const uint8_t* payloadData,
390 size_t payloadSize,
391 const RTPFragmentationHeader* fragmentation) {
392 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
393 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
394 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
395 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000396
kwiberg55b97fe2016-01-28 05:22:45 -0800397 if (_includeAudioLevelIndication) {
398 // Store current audio level in the RTP/RTCP module.
399 // The level will be used in combination with voice-activity state
400 // (frameType) to add an RTP header extension
401 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
402 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000403
kwiberg55b97fe2016-01-28 05:22:45 -0800404 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
405 // packetization.
406 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700407 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800408 (FrameType&)frameType, payloadType, timeStamp,
409 // Leaving the time when this frame was
410 // received from the capture device as
411 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700412 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800413 _engineStatisticsPtr->SetLastError(
414 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
415 "Channel::SendData() failed to send data to RTP/RTCP module");
416 return -1;
417 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000418
kwiberg55b97fe2016-01-28 05:22:45 -0800419 _lastLocalTimeStamp = timeStamp;
420 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000421
kwiberg55b97fe2016-01-28 05:22:45 -0800422 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000423}
424
kwiberg55b97fe2016-01-28 05:22:45 -0800425int32_t Channel::InFrameType(FrameType frame_type) {
426 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
427 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000428
kwiberg55b97fe2016-01-28 05:22:45 -0800429 rtc::CritScope cs(&_callbackCritSect);
430 _sendFrameType = (frame_type == kAudioFrameSpeech);
431 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000432}
433
stefan1d8a5062015-10-02 03:39:33 -0700434bool Channel::SendRtp(const uint8_t* data,
435 size_t len,
436 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800437 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
438 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
kwiberg55b97fe2016-01-28 05:22:45 -0800440 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000441
kwiberg55b97fe2016-01-28 05:22:45 -0800442 if (_transportPtr == NULL) {
443 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
444 "Channel::SendPacket() failed to send RTP packet due to"
445 " invalid transport object");
446 return false;
447 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000448
kwiberg55b97fe2016-01-28 05:22:45 -0800449 uint8_t* bufferToSendPtr = (uint8_t*)data;
450 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000451
kwiberg55b97fe2016-01-28 05:22:45 -0800452 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
453 std::string transport_name =
454 _externalTransport ? "external transport" : "WebRtc sockets";
455 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
456 "Channel::SendPacket() RTP transmission using %s failed",
457 transport_name.c_str());
458 return false;
459 }
460 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000461}
462
kwiberg55b97fe2016-01-28 05:22:45 -0800463bool Channel::SendRtcp(const uint8_t* data, size_t len) {
464 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
465 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000466
kwiberg55b97fe2016-01-28 05:22:45 -0800467 rtc::CritScope cs(&_callbackCritSect);
468 if (_transportPtr == NULL) {
469 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
470 "Channel::SendRtcp() failed to send RTCP packet"
471 " due to invalid transport object");
472 return false;
473 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000474
kwiberg55b97fe2016-01-28 05:22:45 -0800475 uint8_t* bufferToSendPtr = (uint8_t*)data;
476 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000477
kwiberg55b97fe2016-01-28 05:22:45 -0800478 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
479 if (n < 0) {
480 std::string transport_name =
481 _externalTransport ? "external transport" : "WebRtc sockets";
482 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
483 "Channel::SendRtcp() transmission using %s failed",
484 transport_name.c_str());
485 return false;
486 }
487 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000488}
489
kwiberg55b97fe2016-01-28 05:22:45 -0800490void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
491 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
492 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000493
kwiberg55b97fe2016-01-28 05:22:45 -0800494 // Update ssrc so that NTP for AV sync can be updated.
495 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000496}
497
Peter Boströmac547a62015-09-17 23:03:57 +0200498void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
499 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
500 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
501 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000502}
503
Peter Boströmac547a62015-09-17 23:03:57 +0200504int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000505 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000506 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000507 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800508 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200509 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800510 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
511 "Channel::OnInitializeDecoder(payloadType=%d, "
512 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
513 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000514
kwiberg55b97fe2016-01-28 05:22:45 -0800515 CodecInst receiveCodec = {0};
516 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000517
kwiberg55b97fe2016-01-28 05:22:45 -0800518 receiveCodec.pltype = payloadType;
519 receiveCodec.plfreq = frequency;
520 receiveCodec.channels = channels;
521 receiveCodec.rate = rate;
522 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000523
kwiberg55b97fe2016-01-28 05:22:45 -0800524 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
525 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000526
kwiberg55b97fe2016-01-28 05:22:45 -0800527 // Register the new codec to the ACM
kwibergc8d071e2016-04-06 12:22:38 -0700528 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, receiveCodec)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800529 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
530 "Channel::OnInitializeDecoder() invalid codec ("
531 "pt=%d, name=%s) received - 1",
532 payloadType, payloadName);
533 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
534 return -1;
535 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000536
kwiberg55b97fe2016-01-28 05:22:45 -0800537 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000538}
539
kwiberg55b97fe2016-01-28 05:22:45 -0800540int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
541 size_t payloadSize,
542 const WebRtcRTPHeader* rtpHeader) {
543 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
544 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
545 ","
546 " payloadType=%u, audioChannel=%" PRIuS ")",
547 payloadSize, rtpHeader->header.payloadType,
548 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000549
kwiberg55b97fe2016-01-28 05:22:45 -0800550 if (!channel_state_.Get().playing) {
551 // Avoid inserting into NetEQ when we are not playing. Count the
552 // packet as discarded.
553 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
554 "received packet is discarded since playing is not"
555 " activated");
556 _numberOfDiscardedPackets++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000557 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800558 }
559
560 // Push the incoming payload (parsed and ready for decoding) into the ACM
561 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
562 0) {
563 _engineStatisticsPtr->SetLastError(
564 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
565 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
566 return -1;
567 }
568
kwiberg55b97fe2016-01-28 05:22:45 -0800569 int64_t round_trip_time = 0;
570 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
571 NULL);
572
573 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
574 if (!nack_list.empty()) {
575 // Can't use nack_list.data() since it's not supported by all
576 // compilers.
577 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
578 }
579 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000580}
581
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000582bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000583 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000584 RTPHeader header;
585 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
586 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
587 "IncomingPacket invalid RTP header");
588 return false;
589 }
590 header.payload_type_frequency =
591 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
592 if (header.payload_type_frequency < 0)
593 return false;
594 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
595}
596
henrik.lundin42dda502016-05-18 05:36:01 -0700597MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
598 int32_t id,
599 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700600 unsigned int ssrc;
601 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
602 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800603 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700604 bool muted;
605 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
606 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800607 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
608 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
609 // In all likelihood, the audio in this frame is garbage. We return an
610 // error so that the audio mixer module doesn't add it to the mix. As
611 // a result, it won't be played out and the actions skipped here are
612 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700613 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800614 }
henrik.lundina89ab962016-05-18 08:52:45 -0700615
616 if (muted) {
617 // TODO(henrik.lundin): We should be able to do better than this. But we
618 // will have to go through all the cases below where the audio samples may
619 // be used, and handle the muted case in some way.
620 audioFrame->Mute();
621 }
kwiberg55b97fe2016-01-28 05:22:45 -0800622
kwiberg55b97fe2016-01-28 05:22:45 -0800623 // Convert module ID to internal VoE channel ID
624 audioFrame->id_ = VoEChannelId(audioFrame->id_);
625 // Store speech type for dead-or-alive detection
626 _outputSpeechType = audioFrame->speech_type_;
627
628 ChannelState::State state = channel_state_.Get();
629
kwiberg55b97fe2016-01-28 05:22:45 -0800630 {
631 // Pass the audio buffers to an optional sink callback, before applying
632 // scaling/panning, as that applies to the mix operation.
633 // External recipients of the audio (e.g. via AudioTrack), will do their
634 // own mixing/dynamic processing.
635 rtc::CritScope cs(&_callbackCritSect);
636 if (audio_sink_) {
637 AudioSinkInterface::Data data(
638 &audioFrame->data_[0], audioFrame->samples_per_channel_,
639 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
640 audioFrame->timestamp_);
641 audio_sink_->OnData(data);
642 }
643 }
644
645 float output_gain = 1.0f;
646 float left_pan = 1.0f;
647 float right_pan = 1.0f;
648 {
649 rtc::CritScope cs(&volume_settings_critsect_);
650 output_gain = _outputGain;
651 left_pan = _panLeft;
652 right_pan = _panRight;
653 }
654
655 // Output volume scaling
656 if (output_gain < 0.99f || output_gain > 1.01f) {
657 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
658 }
659
660 // Scale left and/or right channel(s) if stereo and master balance is
661 // active
662
663 if (left_pan != 1.0f || right_pan != 1.0f) {
664 if (audioFrame->num_channels_ == 1) {
665 // Emulate stereo mode since panning is active.
666 // The mono signal is copied to both left and right channels here.
667 AudioFrameOperations::MonoToStereo(audioFrame);
668 }
669 // For true stereo mode (when we are receiving a stereo signal), no
670 // action is needed.
671
672 // Do the panning operation (the audio frame contains stereo at this
673 // stage)
674 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
675 }
676
677 // Mix decoded PCM output with file if file mixing is enabled
678 if (state.output_file_playing) {
679 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700680 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800681 }
682
683 // External media
684 if (_outputExternalMedia) {
685 rtc::CritScope cs(&_callbackCritSect);
686 const bool isStereo = (audioFrame->num_channels_ == 2);
687 if (_outputExternalMediaCallbackPtr) {
688 _outputExternalMediaCallbackPtr->Process(
689 _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_,
690 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
691 isStereo);
692 }
693 }
694
695 // Record playout if enabled
696 {
697 rtc::CritScope cs(&_fileCritSect);
698
kwiberg5a25d952016-08-17 07:31:12 -0700699 if (_outputFileRecording && output_file_recorder_) {
700 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800701 }
702 }
703
704 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700705 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800706 _outputAudioLevel.ComputeLevel(*audioFrame);
707
708 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
709 // The first frame with a valid rtp timestamp.
710 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
711 }
712
713 if (capture_start_rtp_time_stamp_ >= 0) {
714 // audioFrame.timestamp_ should be valid from now on.
715
716 // Compute elapsed time.
717 int64_t unwrap_timestamp =
718 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
719 audioFrame->elapsed_time_ms_ =
720 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
721 (GetPlayoutFrequency() / 1000);
722
niklase@google.com470e71d2011-07-07 08:21:25 +0000723 {
kwiberg55b97fe2016-01-28 05:22:45 -0800724 rtc::CritScope lock(&ts_stats_lock_);
725 // Compute ntp time.
726 audioFrame->ntp_time_ms_ =
727 ntp_estimator_.Estimate(audioFrame->timestamp_);
728 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
729 if (audioFrame->ntp_time_ms_ > 0) {
730 // Compute |capture_start_ntp_time_ms_| so that
731 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
732 capture_start_ntp_time_ms_ =
733 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000734 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000735 }
kwiberg55b97fe2016-01-28 05:22:45 -0800736 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000737
henrik.lundin42dda502016-05-18 05:36:01 -0700738 return muted ? MixerParticipant::AudioFrameInfo::kMuted
739 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000740}
741
kwiberg55b97fe2016-01-28 05:22:45 -0800742int32_t Channel::NeededFrequency(int32_t id) const {
743 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
744 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000745
kwiberg55b97fe2016-01-28 05:22:45 -0800746 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000747
kwiberg55b97fe2016-01-28 05:22:45 -0800748 // Determine highest needed receive frequency
749 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000750
kwiberg55b97fe2016-01-28 05:22:45 -0800751 // Return the bigger of playout and receive frequency in the ACM.
752 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
753 highestNeeded = audio_coding_->PlayoutFrequency();
754 } else {
755 highestNeeded = receiveFrequency;
756 }
757
758 // Special case, if we're playing a file on the playout side
759 // we take that frequency into consideration as well
760 // This is not needed on sending side, since the codec will
761 // limit the spectrum anyway.
762 if (channel_state_.Get().output_file_playing) {
763 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700764 if (output_file_player_) {
765 if (output_file_player_->Frequency() > highestNeeded) {
766 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800767 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000768 }
kwiberg55b97fe2016-01-28 05:22:45 -0800769 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000770
kwiberg55b97fe2016-01-28 05:22:45 -0800771 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000772}
773
ossu5f7cfa52016-05-30 08:11:28 -0700774int32_t Channel::CreateChannel(
775 Channel*& channel,
776 int32_t channelId,
777 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700778 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800779 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
780 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
781 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000782
solenberg88499ec2016-09-07 07:34:41 -0700783 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800784 if (channel == NULL) {
785 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
786 "Channel::CreateChannel() unable to allocate memory for"
787 " channel");
788 return -1;
789 }
790 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000791}
792
kwiberg55b97fe2016-01-28 05:22:45 -0800793void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
794 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
795 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
796 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000797
kwiberg55b97fe2016-01-28 05:22:45 -0800798 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000799}
800
kwiberg55b97fe2016-01-28 05:22:45 -0800801void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
802 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
803 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
804 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000805
kwiberg55b97fe2016-01-28 05:22:45 -0800806 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000807}
808
kwiberg55b97fe2016-01-28 05:22:45 -0800809void Channel::PlayFileEnded(int32_t id) {
810 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
811 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000812
kwiberg55b97fe2016-01-28 05:22:45 -0800813 if (id == _inputFilePlayerId) {
814 channel_state_.SetInputFilePlaying(false);
815 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
816 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000817 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800818 } else if (id == _outputFilePlayerId) {
819 channel_state_.SetOutputFilePlaying(false);
820 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
821 "Channel::PlayFileEnded() => output file player module is"
822 " shutdown");
823 }
824}
825
826void Channel::RecordFileEnded(int32_t id) {
827 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
828 "Channel::RecordFileEnded(id=%d)", id);
829
830 assert(id == _outputFileRecorderId);
831
832 rtc::CritScope cs(&_fileCritSect);
833
834 _outputFileRecording = false;
835 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
836 "Channel::RecordFileEnded() => output file recorder module is"
837 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000838}
839
pbos@webrtc.org92135212013-05-14 08:31:39 +0000840Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000841 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700842 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800843 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100844 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700845 event_log_proxy_(new RtcEventLogProxy()),
michaelte0729c52016-10-11 00:29:27 -0700846 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100847 rtp_header_parser_(RtpHeaderParser::Create()),
848 rtp_payload_registry_(
849 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
850 rtp_receive_statistics_(
851 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
852 rtp_receiver_(
853 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100854 this,
855 this,
856 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700857 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100858 _outputAudioLevel(),
859 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100860 // Avoid conflict with other channels by adding 1024 - 1026,
861 // won't use as much as 1024 channels.
862 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
863 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
864 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
865 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100866 _outputExternalMedia(false),
867 _inputExternalMediaCallbackPtr(NULL),
868 _outputExternalMediaCallbackPtr(NULL),
869 _timeStamp(0), // This is just an offset, RTP module will add it's own
870 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100871 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100872 playout_timestamp_rtp_(0),
873 playout_timestamp_rtcp_(0),
874 playout_delay_ms_(0),
875 _numberOfDiscardedPackets(0),
876 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100877 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
878 capture_start_rtp_time_stamp_(-1),
879 capture_start_ntp_time_ms_(-1),
880 _engineStatisticsPtr(NULL),
881 _outputMixerPtr(NULL),
882 _transmitMixerPtr(NULL),
883 _moduleProcessThreadPtr(NULL),
884 _audioDeviceModulePtr(NULL),
885 _voiceEngineObserverPtr(NULL),
886 _callbackCritSectPtr(NULL),
887 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100888 _sendFrameType(0),
889 _externalMixing(false),
890 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700891 input_mute_(false),
892 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100893 _panLeft(1.0f),
894 _panRight(1.0f),
895 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100896 _lastLocalTimeStamp(0),
897 _lastPayloadType(0),
898 _includeAudioLevelIndication(false),
899 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100900 restored_packet_in_use_(false),
901 rtcp_observer_(new VoERtcpObserver(this)),
902 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock())),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100903 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700904 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800905 feedback_observer_proxy_(new TransportFeedbackProxy()),
906 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700907 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200908 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
909 kMaxRetransmissionWindowMs)),
solenberg88499ec2016-09-07 07:34:41 -0700910 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800911 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
912 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700913 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800914 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700915 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800916 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200917
kwiberg55b97fe2016-01-28 05:22:45 -0800918 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000919
kwiberg55b97fe2016-01-28 05:22:45 -0800920 RtpRtcp::Configuration configuration;
921 configuration.audio = true;
922 configuration.outgoing_transport = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800923 configuration.receive_statistics = rtp_receive_statistics_.get();
924 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800925 if (pacing_enabled_) {
926 configuration.paced_sender = rtp_packet_sender_proxy_.get();
927 configuration.transport_sequence_number_allocator =
928 seq_num_allocator_proxy_.get();
929 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
930 }
ivoc14d5dbe2016-07-04 07:06:55 -0700931 configuration.event_log = &(*event_log_proxy_);
michaelte0729c52016-10-11 00:29:27 -0700932 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200933 configuration.retransmission_rate_limiter =
934 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000935
kwiberg55b97fe2016-01-28 05:22:45 -0800936 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100937 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000938
kwiberg55b97fe2016-01-28 05:22:45 -0800939 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
940 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
941 statistics_proxy_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000942}
943
kwiberg55b97fe2016-01-28 05:22:45 -0800944Channel::~Channel() {
945 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
946 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
947 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000948
kwiberg55b97fe2016-01-28 05:22:45 -0800949 if (_outputExternalMedia) {
950 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
951 }
952 if (channel_state_.Get().input_external_media) {
953 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
954 }
955 StopSend();
956 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000957
kwiberg55b97fe2016-01-28 05:22:45 -0800958 {
959 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700960 if (input_file_player_) {
961 input_file_player_->RegisterModuleFileCallback(NULL);
962 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000963 }
kwiberg5a25d952016-08-17 07:31:12 -0700964 if (output_file_player_) {
965 output_file_player_->RegisterModuleFileCallback(NULL);
966 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800967 }
kwiberg5a25d952016-08-17 07:31:12 -0700968 if (output_file_recorder_) {
969 output_file_recorder_->RegisterModuleFileCallback(NULL);
970 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800971 }
972 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000973
kwiberg55b97fe2016-01-28 05:22:45 -0800974 // The order to safely shutdown modules in a channel is:
975 // 1. De-register callbacks in modules
976 // 2. De-register modules in process thread
977 // 3. Destroy modules
978 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
979 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
980 "~Channel() failed to de-register transport callback"
981 " (Audio coding module)");
982 }
983 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
984 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
985 "~Channel() failed to de-register VAD callback"
986 " (Audio coding module)");
987 }
988 // De-register modules in process thread
989 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000990
kwiberg55b97fe2016-01-28 05:22:45 -0800991 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000992}
993
kwiberg55b97fe2016-01-28 05:22:45 -0800994int32_t Channel::Init() {
995 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
996 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000997
kwiberg55b97fe2016-01-28 05:22:45 -0800998 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000999
kwiberg55b97fe2016-01-28 05:22:45 -08001000 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +00001001
kwiberg55b97fe2016-01-28 05:22:45 -08001002 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
1003 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1004 "Channel::Init() must call SetEngineInformation() first");
1005 return -1;
1006 }
1007
1008 // --- Add modules to process thread (for periodic schedulation)
1009
1010 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
1011
1012 // --- ACM initialization
1013
1014 if (audio_coding_->InitializeReceiver() == -1) {
1015 _engineStatisticsPtr->SetLastError(
1016 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1017 "Channel::Init() unable to initialize the ACM - 1");
1018 return -1;
1019 }
1020
1021 // --- RTP/RTCP module initialization
1022
1023 // Ensure that RTCP is enabled by default for the created channel.
1024 // Note that, the module will keep generating RTCP until it is explicitly
1025 // disabled by the user.
1026 // After StopListen (when no sockets exists), RTCP packets will no longer
1027 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001028 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001029 // RTCP is enabled by default.
1030 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1031 // --- Register all permanent callbacks
1032 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1033 (audio_coding_->RegisterVADCallback(this) == -1);
1034
1035 if (fail) {
1036 _engineStatisticsPtr->SetLastError(
1037 VE_CANNOT_INIT_CHANNEL, kTraceError,
1038 "Channel::Init() callbacks not registered");
1039 return -1;
1040 }
1041
1042 // --- Register all supported codecs to the receiving side of the
1043 // RTP/RTCP module
1044
1045 CodecInst codec;
1046 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1047
1048 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1049 // Open up the RTP/RTCP receiver for all supported codecs
1050 if ((audio_coding_->Codec(idx, &codec) == -1) ||
1051 (rtp_receiver_->RegisterReceivePayload(
1052 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1053 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
1054 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1055 "Channel::Init() unable to register %s "
1056 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1057 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1058 codec.rate);
1059 } else {
1060 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1061 "Channel::Init() %s (%d/%d/%" PRIuS
1062 "/%d) has been "
1063 "added to the RTP/RTCP receiver",
1064 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1065 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001066 }
1067
kwiberg55b97fe2016-01-28 05:22:45 -08001068 // Ensure that PCMU is used as default codec on the sending side
1069 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1070 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001071 }
1072
kwiberg55b97fe2016-01-28 05:22:45 -08001073 // Register default PT for outband 'telephone-event'
1074 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001075 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
1076 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001077 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1078 "Channel::Init() failed to register outband "
1079 "'telephone-event' (%d/%d) correctly",
1080 codec.pltype, codec.plfreq);
1081 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001082 }
1083
kwiberg55b97fe2016-01-28 05:22:45 -08001084 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001085 if (!codec_manager_.RegisterEncoder(codec) ||
1086 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
1087 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec) ||
1088 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001089 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1090 "Channel::Init() failed to register CN (%d/%d) "
1091 "correctly - 1",
1092 codec.pltype, codec.plfreq);
1093 }
1094 }
kwiberg55b97fe2016-01-28 05:22:45 -08001095 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001096
kwiberg55b97fe2016-01-28 05:22:45 -08001097 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001098}
1099
kwiberg55b97fe2016-01-28 05:22:45 -08001100int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1101 OutputMixer& outputMixer,
1102 voe::TransmitMixer& transmitMixer,
1103 ProcessThread& moduleProcessThread,
1104 AudioDeviceModule& audioDeviceModule,
1105 VoiceEngineObserver* voiceEngineObserver,
1106 rtc::CriticalSection* callbackCritSect) {
1107 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1108 "Channel::SetEngineInformation()");
1109 _engineStatisticsPtr = &engineStatistics;
1110 _outputMixerPtr = &outputMixer;
1111 _transmitMixerPtr = &transmitMixer,
1112 _moduleProcessThreadPtr = &moduleProcessThread;
1113 _audioDeviceModulePtr = &audioDeviceModule;
1114 _voiceEngineObserverPtr = voiceEngineObserver;
1115 _callbackCritSectPtr = callbackCritSect;
1116 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001117}
1118
kwiberg55b97fe2016-01-28 05:22:45 -08001119int32_t Channel::UpdateLocalTimeStamp() {
1120 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1121 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001122}
1123
kwibergb7f89d62016-02-17 10:04:18 -08001124void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001125 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001126 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001127}
1128
ossu29b1a8d2016-06-13 07:34:51 -07001129const rtc::scoped_refptr<AudioDecoderFactory>&
1130Channel::GetAudioDecoderFactory() const {
1131 return decoder_factory_;
1132}
1133
kwiberg55b97fe2016-01-28 05:22:45 -08001134int32_t Channel::StartPlayout() {
1135 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1136 "Channel::StartPlayout()");
1137 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001138 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001139 }
1140
1141 if (!_externalMixing) {
1142 // Add participant as candidates for mixing.
1143 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1144 _engineStatisticsPtr->SetLastError(
1145 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1146 "StartPlayout() failed to add participant to mixer");
1147 return -1;
1148 }
1149 }
1150
1151 channel_state_.SetPlaying(true);
1152 if (RegisterFilePlayingToMixer() != 0)
1153 return -1;
1154
1155 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001156}
1157
kwiberg55b97fe2016-01-28 05:22:45 -08001158int32_t Channel::StopPlayout() {
1159 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1160 "Channel::StopPlayout()");
1161 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001162 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001163 }
1164
1165 if (!_externalMixing) {
1166 // Remove participant as candidates for mixing
1167 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1168 _engineStatisticsPtr->SetLastError(
1169 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1170 "StopPlayout() failed to remove participant from mixer");
1171 return -1;
1172 }
1173 }
1174
1175 channel_state_.SetPlaying(false);
1176 _outputAudioLevel.Clear();
1177
1178 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001179}
1180
kwiberg55b97fe2016-01-28 05:22:45 -08001181int32_t Channel::StartSend() {
1182 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1183 "Channel::StartSend()");
1184 // Resume the previous sequence number which was reset by StopSend().
1185 // This needs to be done before |sending| is set to true.
1186 if (send_sequence_number_)
1187 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001188
kwiberg55b97fe2016-01-28 05:22:45 -08001189 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001190 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001191 }
1192 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001193
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001194 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001195 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1196 _engineStatisticsPtr->SetLastError(
1197 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1198 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001199 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001200 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001201 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001202 return -1;
1203 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001204
kwiberg55b97fe2016-01-28 05:22:45 -08001205 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001206}
1207
kwiberg55b97fe2016-01-28 05:22:45 -08001208int32_t Channel::StopSend() {
1209 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1210 "Channel::StopSend()");
1211 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001212 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001213 }
1214 channel_state_.SetSending(false);
1215
1216 // Store the sequence number to be able to pick up the same sequence for
1217 // the next StartSend(). This is needed for restarting device, otherwise
1218 // it might cause libSRTP to complain about packets being replayed.
1219 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1220 // CL is landed. See issue
1221 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1222 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1223
1224 // Reset sending SSRC and sequence number and triggers direct transmission
1225 // of RTCP BYE
1226 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1227 _engineStatisticsPtr->SetLastError(
1228 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1229 "StartSend() RTP/RTCP failed to stop sending");
1230 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001231 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001232
1233 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001234}
1235
kwiberg55b97fe2016-01-28 05:22:45 -08001236int32_t Channel::StartReceiving() {
1237 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1238 "Channel::StartReceiving()");
1239 if (channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001240 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001241 }
1242 channel_state_.SetReceiving(true);
1243 _numberOfDiscardedPackets = 0;
1244 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001245}
1246
kwiberg55b97fe2016-01-28 05:22:45 -08001247int32_t Channel::StopReceiving() {
1248 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1249 "Channel::StopReceiving()");
1250 if (!channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001251 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001252 }
1253
1254 channel_state_.SetReceiving(false);
1255 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001256}
1257
kwiberg55b97fe2016-01-28 05:22:45 -08001258int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1259 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1260 "Channel::RegisterVoiceEngineObserver()");
1261 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001262
kwiberg55b97fe2016-01-28 05:22:45 -08001263 if (_voiceEngineObserverPtr) {
1264 _engineStatisticsPtr->SetLastError(
1265 VE_INVALID_OPERATION, kTraceError,
1266 "RegisterVoiceEngineObserver() observer already enabled");
1267 return -1;
1268 }
1269 _voiceEngineObserverPtr = &observer;
1270 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001271}
1272
kwiberg55b97fe2016-01-28 05:22:45 -08001273int32_t Channel::DeRegisterVoiceEngineObserver() {
1274 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1275 "Channel::DeRegisterVoiceEngineObserver()");
1276 rtc::CritScope cs(&_callbackCritSect);
1277
1278 if (!_voiceEngineObserverPtr) {
1279 _engineStatisticsPtr->SetLastError(
1280 VE_INVALID_OPERATION, kTraceWarning,
1281 "DeRegisterVoiceEngineObserver() observer already disabled");
1282 return 0;
1283 }
1284 _voiceEngineObserverPtr = NULL;
1285 return 0;
1286}
1287
1288int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001289 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001290 if (send_codec) {
1291 codec = *send_codec;
1292 return 0;
1293 }
1294 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001295}
1296
kwiberg55b97fe2016-01-28 05:22:45 -08001297int32_t Channel::GetRecCodec(CodecInst& codec) {
1298 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001299}
1300
kwiberg55b97fe2016-01-28 05:22:45 -08001301int32_t Channel::SetSendCodec(const CodecInst& codec) {
1302 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1303 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001304
kwibergc8d071e2016-04-06 12:22:38 -07001305 if (!codec_manager_.RegisterEncoder(codec) ||
1306 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001307 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1308 "SetSendCodec() failed to register codec to ACM");
1309 return -1;
1310 }
1311
1312 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1313 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1314 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1315 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1316 "SetSendCodec() failed to register codec to"
1317 " RTP/RTCP module");
1318 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001319 }
kwiberg55b97fe2016-01-28 05:22:45 -08001320 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001321
kwiberg55b97fe2016-01-28 05:22:45 -08001322 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) {
1323 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1324 "SetSendCodec() failed to set audio packet size");
1325 return -1;
1326 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001327
kwiberg55b97fe2016-01-28 05:22:45 -08001328 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001329}
1330
Ivo Creusenadf89b72015-04-29 16:03:33 +02001331void Channel::SetBitRate(int bitrate_bps) {
1332 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1333 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
1334 audio_coding_->SetBitRate(bitrate_bps);
Erik Språng737336d2016-07-29 12:59:36 +02001335 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001336}
1337
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001338void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001339 network_predictor_->UpdatePacketLossRate(fraction_lost);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001340 uint8_t average_fraction_loss = network_predictor_->GetLossRate();
1341
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001342 // Normalizes rate to 0 - 100.
kwiberg55b97fe2016-01-28 05:22:45 -08001343 if (audio_coding_->SetPacketLossRate(100 * average_fraction_loss / 255) !=
1344 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001345 assert(false); // This should not happen.
1346 }
1347}
1348
kwiberg55b97fe2016-01-28 05:22:45 -08001349int32_t Channel::SetVADStatus(bool enableVAD,
1350 ACMVADMode mode,
1351 bool disableDTX) {
1352 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1353 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001354 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1355 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1356 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001357 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1358 kTraceError,
1359 "SetVADStatus() failed to set VAD");
1360 return -1;
1361 }
1362 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001363}
1364
kwiberg55b97fe2016-01-28 05:22:45 -08001365int32_t Channel::GetVADStatus(bool& enabledVAD,
1366 ACMVADMode& mode,
1367 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001368 const auto* params = codec_manager_.GetStackParams();
1369 enabledVAD = params->use_cng;
1370 mode = params->vad_mode;
1371 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001372 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001373}
1374
kwiberg55b97fe2016-01-28 05:22:45 -08001375int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1376 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1377 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001378
kwiberg55b97fe2016-01-28 05:22:45 -08001379 if (channel_state_.Get().playing) {
1380 _engineStatisticsPtr->SetLastError(
1381 VE_ALREADY_PLAYING, kTraceError,
1382 "SetRecPayloadType() unable to set PT while playing");
1383 return -1;
1384 }
1385 if (channel_state_.Get().receiving) {
1386 _engineStatisticsPtr->SetLastError(
1387 VE_ALREADY_LISTENING, kTraceError,
1388 "SetRecPayloadType() unable to set PT while listening");
1389 return -1;
1390 }
1391
1392 if (codec.pltype == -1) {
1393 // De-register the selected codec (RTP/RTCP module and ACM)
1394
1395 int8_t pltype(-1);
1396 CodecInst rxCodec = codec;
1397
1398 // Get payload type for the given codec
1399 rtp_payload_registry_->ReceivePayloadType(
1400 rxCodec.plname, rxCodec.plfreq, rxCodec.channels,
1401 (rxCodec.rate < 0) ? 0 : rxCodec.rate, &pltype);
1402 rxCodec.pltype = pltype;
1403
1404 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1405 _engineStatisticsPtr->SetLastError(
1406 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1407 "SetRecPayloadType() RTP/RTCP-module deregistration "
1408 "failed");
1409 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001410 }
kwiberg55b97fe2016-01-28 05:22:45 -08001411 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1412 _engineStatisticsPtr->SetLastError(
1413 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1414 "SetRecPayloadType() ACM deregistration failed - 1");
1415 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001416 }
kwiberg55b97fe2016-01-28 05:22:45 -08001417 return 0;
1418 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001419
kwiberg55b97fe2016-01-28 05:22:45 -08001420 if (rtp_receiver_->RegisterReceivePayload(
1421 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1422 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1423 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001424 // TODO(kwiberg): Retrying is probably not necessary, since
1425 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001426 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001427 if (rtp_receiver_->RegisterReceivePayload(
kwiberg55b97fe2016-01-28 05:22:45 -08001428 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1429 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1430 _engineStatisticsPtr->SetLastError(
1431 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1432 "SetRecPayloadType() RTP/RTCP-module registration failed");
1433 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001434 }
kwiberg55b97fe2016-01-28 05:22:45 -08001435 }
kwibergc8d071e2016-04-06 12:22:38 -07001436 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001437 audio_coding_->UnregisterReceiveCodec(codec.pltype);
kwibergc8d071e2016-04-06 12:22:38 -07001438 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001439 _engineStatisticsPtr->SetLastError(
1440 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1441 "SetRecPayloadType() ACM registration failed - 1");
1442 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001443 }
kwiberg55b97fe2016-01-28 05:22:45 -08001444 }
1445 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001446}
1447
kwiberg55b97fe2016-01-28 05:22:45 -08001448int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1449 int8_t payloadType(-1);
1450 if (rtp_payload_registry_->ReceivePayloadType(
1451 codec.plname, codec.plfreq, codec.channels,
1452 (codec.rate < 0) ? 0 : codec.rate, &payloadType) != 0) {
1453 _engineStatisticsPtr->SetLastError(
1454 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1455 "GetRecPayloadType() failed to retrieve RX payload type");
1456 return -1;
1457 }
1458 codec.pltype = payloadType;
1459 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001460}
1461
kwiberg55b97fe2016-01-28 05:22:45 -08001462int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1463 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1464 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001465
kwiberg55b97fe2016-01-28 05:22:45 -08001466 CodecInst codec;
1467 int32_t samplingFreqHz(-1);
1468 const size_t kMono = 1;
1469 if (frequency == kFreq32000Hz)
1470 samplingFreqHz = 32000;
1471 else if (frequency == kFreq16000Hz)
1472 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001473
kwiberg55b97fe2016-01-28 05:22:45 -08001474 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1475 _engineStatisticsPtr->SetLastError(
1476 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1477 "SetSendCNPayloadType() failed to retrieve default CN codec "
1478 "settings");
1479 return -1;
1480 }
1481
1482 // Modify the payload type (must be set to dynamic range)
1483 codec.pltype = type;
1484
kwibergc8d071e2016-04-06 12:22:38 -07001485 if (!codec_manager_.RegisterEncoder(codec) ||
1486 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001487 _engineStatisticsPtr->SetLastError(
1488 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1489 "SetSendCNPayloadType() failed to register CN to ACM");
1490 return -1;
1491 }
1492
1493 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1494 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1495 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1496 _engineStatisticsPtr->SetLastError(
1497 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1498 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1499 "module");
1500 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001501 }
kwiberg55b97fe2016-01-28 05:22:45 -08001502 }
1503 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001504}
1505
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001506int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001507 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001508 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001509
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001510 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001511 _engineStatisticsPtr->SetLastError(
1512 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001513 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001514 return -1;
1515 }
1516 return 0;
1517}
1518
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001519int Channel::SetOpusDtx(bool enable_dtx) {
1520 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1521 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001522 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001523 : audio_coding_->DisableOpusDtx();
1524 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001525 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1526 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001527 return -1;
1528 }
1529 return 0;
1530}
1531
ivoc85228d62016-07-27 04:53:47 -07001532int Channel::GetOpusDtx(bool* enabled) {
1533 int success = -1;
1534 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1535 if (encoder) {
1536 *enabled = encoder->GetDtx();
1537 success = 0;
1538 }
1539 });
1540 return success;
1541}
1542
mflodman3d7db262016-04-29 00:57:13 -07001543int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001544 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001545 "Channel::RegisterExternalTransport()");
1546
kwiberg55b97fe2016-01-28 05:22:45 -08001547 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001548 if (_externalTransport) {
1549 _engineStatisticsPtr->SetLastError(
1550 VE_INVALID_OPERATION, kTraceError,
1551 "RegisterExternalTransport() external transport already enabled");
1552 return -1;
1553 }
1554 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001555 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001556 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001557}
1558
kwiberg55b97fe2016-01-28 05:22:45 -08001559int32_t Channel::DeRegisterExternalTransport() {
1560 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1561 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001562
kwiberg55b97fe2016-01-28 05:22:45 -08001563 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001564 if (_transportPtr) {
1565 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1566 "DeRegisterExternalTransport() all transport is disabled");
1567 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001568 _engineStatisticsPtr->SetLastError(
1569 VE_INVALID_OPERATION, kTraceWarning,
1570 "DeRegisterExternalTransport() external transport already "
1571 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001572 }
1573 _externalTransport = false;
1574 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001575 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001576}
1577
mflodman3d7db262016-04-29 00:57:13 -07001578int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001579 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001580 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001581 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001582 "Channel::ReceivedRTPPacket()");
1583
1584 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001585 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001586
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001587 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001588 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1589 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1590 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001591 return -1;
1592 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001593 header.payload_type_frequency =
1594 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001595 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001596 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001597 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001598 rtp_receive_statistics_->IncomingPacket(
1599 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001600 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001601
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001602 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001603}
1604
1605bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001606 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001607 const RTPHeader& header,
1608 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001609 if (rtp_payload_registry_->IsRtx(header)) {
1610 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001611 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001612 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001613 assert(packet_length >= header.headerLength);
1614 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001615 PayloadUnion payload_specific;
1616 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001617 &payload_specific)) {
1618 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001619 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001620 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1621 payload_specific, in_order);
1622}
1623
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001624bool Channel::HandleRtxPacket(const uint8_t* packet,
1625 size_t packet_length,
1626 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001627 if (!rtp_payload_registry_->IsRtx(header))
1628 return false;
1629
1630 // Remove the RTX header and parse the original RTP header.
1631 if (packet_length < header.headerLength)
1632 return false;
1633 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1634 return false;
1635 if (restored_packet_in_use_) {
1636 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1637 "Multiple RTX headers detected, dropping packet");
1638 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001639 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001640 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001641 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1642 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001643 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1644 "Incoming RTX packet: invalid RTP header");
1645 return false;
1646 }
1647 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001648 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001649 restored_packet_in_use_ = false;
1650 return ret;
1651}
1652
1653bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1654 StreamStatistician* statistician =
1655 rtp_receive_statistics_->GetStatistician(header.ssrc);
1656 if (!statistician)
1657 return false;
1658 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001659}
1660
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001661bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1662 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001663 // Retransmissions are handled separately if RTX is enabled.
1664 if (rtp_payload_registry_->RtxEnabled())
1665 return false;
1666 StreamStatistician* statistician =
1667 rtp_receive_statistics_->GetStatistician(header.ssrc);
1668 if (!statistician)
1669 return false;
1670 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001671 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001672 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001673 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001674}
1675
mflodman3d7db262016-04-29 00:57:13 -07001676int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001677 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001678 "Channel::ReceivedRTCPPacket()");
1679 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001680 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001681
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001682 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001683 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001684 _engineStatisticsPtr->SetLastError(
1685 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1686 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1687 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001688
Minyue2013aec2015-05-13 14:14:42 +02001689 int64_t rtt = GetRTT(true);
1690 if (rtt == 0) {
1691 // Waiting for valid RTT.
1692 return 0;
1693 }
Erik Språng737336d2016-07-29 12:59:36 +02001694
1695 int64_t nack_window_ms = rtt;
1696 if (nack_window_ms < kMinRetransmissionWindowMs) {
1697 nack_window_ms = kMinRetransmissionWindowMs;
1698 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1699 nack_window_ms = kMaxRetransmissionWindowMs;
1700 }
1701 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1702
Minyue2013aec2015-05-13 14:14:42 +02001703 uint32_t ntp_secs = 0;
1704 uint32_t ntp_frac = 0;
1705 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001706 if (0 !=
1707 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1708 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001709 // Waiting for RTCP.
1710 return 0;
1711 }
1712
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001713 {
tommi31fc21f2016-01-21 10:37:37 -08001714 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001715 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001716 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001717 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001718}
1719
niklase@google.com470e71d2011-07-07 08:21:25 +00001720int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001721 bool loop,
1722 FileFormats format,
1723 int startPosition,
1724 float volumeScaling,
1725 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001726 const CodecInst* codecInst) {
1727 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1728 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1729 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1730 "stopPosition=%d)",
1731 fileName, loop, format, volumeScaling, startPosition,
1732 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001733
kwiberg55b97fe2016-01-28 05:22:45 -08001734 if (channel_state_.Get().output_file_playing) {
1735 _engineStatisticsPtr->SetLastError(
1736 VE_ALREADY_PLAYING, kTraceError,
1737 "StartPlayingFileLocally() is already playing");
1738 return -1;
1739 }
1740
1741 {
1742 rtc::CritScope cs(&_fileCritSect);
1743
kwiberg5a25d952016-08-17 07:31:12 -07001744 if (output_file_player_) {
1745 output_file_player_->RegisterModuleFileCallback(NULL);
1746 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001747 }
1748
kwiberg5b356f42016-09-08 04:32:33 -07001749 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001750 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001751
kwiberg5a25d952016-08-17 07:31:12 -07001752 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001753 _engineStatisticsPtr->SetLastError(
1754 VE_INVALID_ARGUMENT, kTraceError,
1755 "StartPlayingFileLocally() filePlayer format is not correct");
1756 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001757 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001758
kwiberg55b97fe2016-01-28 05:22:45 -08001759 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001760
kwiberg5a25d952016-08-17 07:31:12 -07001761 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001762 fileName, loop, startPosition, volumeScaling, notificationTime,
1763 stopPosition, (const CodecInst*)codecInst) != 0) {
1764 _engineStatisticsPtr->SetLastError(
1765 VE_BAD_FILE, kTraceError,
1766 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001767 output_file_player_->StopPlayingFile();
1768 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001769 return -1;
1770 }
kwiberg5a25d952016-08-17 07:31:12 -07001771 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001772 channel_state_.SetOutputFilePlaying(true);
1773 }
1774
1775 if (RegisterFilePlayingToMixer() != 0)
1776 return -1;
1777
1778 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001779}
1780
1781int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001782 FileFormats format,
1783 int startPosition,
1784 float volumeScaling,
1785 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001786 const CodecInst* codecInst) {
1787 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1788 "Channel::StartPlayingFileLocally(format=%d,"
1789 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1790 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001791
kwiberg55b97fe2016-01-28 05:22:45 -08001792 if (stream == NULL) {
1793 _engineStatisticsPtr->SetLastError(
1794 VE_BAD_FILE, kTraceError,
1795 "StartPlayingFileLocally() NULL as input stream");
1796 return -1;
1797 }
1798
1799 if (channel_state_.Get().output_file_playing) {
1800 _engineStatisticsPtr->SetLastError(
1801 VE_ALREADY_PLAYING, kTraceError,
1802 "StartPlayingFileLocally() is already playing");
1803 return -1;
1804 }
1805
1806 {
1807 rtc::CritScope cs(&_fileCritSect);
1808
1809 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001810 if (output_file_player_) {
1811 output_file_player_->RegisterModuleFileCallback(NULL);
1812 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001813 }
1814
kwiberg55b97fe2016-01-28 05:22:45 -08001815 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001816 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001817 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001818
kwiberg5a25d952016-08-17 07:31:12 -07001819 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001820 _engineStatisticsPtr->SetLastError(
1821 VE_INVALID_ARGUMENT, kTraceError,
1822 "StartPlayingFileLocally() filePlayer format isnot correct");
1823 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001824 }
1825
kwiberg55b97fe2016-01-28 05:22:45 -08001826 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001827
kwiberg4ec01d92016-08-22 08:43:54 -07001828 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001829 volumeScaling, notificationTime,
1830 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001831 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1832 "StartPlayingFile() failed to "
1833 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001834 output_file_player_->StopPlayingFile();
1835 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001836 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001837 }
kwiberg5a25d952016-08-17 07:31:12 -07001838 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001839 channel_state_.SetOutputFilePlaying(true);
1840 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001841
kwiberg55b97fe2016-01-28 05:22:45 -08001842 if (RegisterFilePlayingToMixer() != 0)
1843 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001844
kwiberg55b97fe2016-01-28 05:22:45 -08001845 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001846}
1847
kwiberg55b97fe2016-01-28 05:22:45 -08001848int Channel::StopPlayingFileLocally() {
1849 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1850 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001851
kwiberg55b97fe2016-01-28 05:22:45 -08001852 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001853 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001854 }
1855
1856 {
1857 rtc::CritScope cs(&_fileCritSect);
1858
kwiberg5a25d952016-08-17 07:31:12 -07001859 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001860 _engineStatisticsPtr->SetLastError(
1861 VE_STOP_RECORDING_FAILED, kTraceError,
1862 "StopPlayingFile() could not stop playing");
1863 return -1;
1864 }
kwiberg5a25d952016-08-17 07:31:12 -07001865 output_file_player_->RegisterModuleFileCallback(NULL);
1866 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001867 channel_state_.SetOutputFilePlaying(false);
1868 }
1869 // _fileCritSect cannot be taken while calling
1870 // SetAnonymousMixibilityStatus. Refer to comments in
1871 // StartPlayingFileLocally(const char* ...) for more details.
1872 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1873 _engineStatisticsPtr->SetLastError(
1874 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1875 "StopPlayingFile() failed to stop participant from playing as"
1876 "file in the mixer");
1877 return -1;
1878 }
1879
1880 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001881}
1882
kwiberg55b97fe2016-01-28 05:22:45 -08001883int Channel::IsPlayingFileLocally() const {
1884 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001885}
1886
kwiberg55b97fe2016-01-28 05:22:45 -08001887int Channel::RegisterFilePlayingToMixer() {
1888 // Return success for not registering for file playing to mixer if:
1889 // 1. playing file before playout is started on that channel.
1890 // 2. starting playout without file playing on that channel.
1891 if (!channel_state_.Get().playing ||
1892 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001893 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001894 }
1895
1896 // |_fileCritSect| cannot be taken while calling
1897 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1898 // frames can be pulled by the mixer. Since the frames are generated from
1899 // the file, _fileCritSect will be taken. This would result in a deadlock.
1900 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1901 channel_state_.SetOutputFilePlaying(false);
1902 rtc::CritScope cs(&_fileCritSect);
1903 _engineStatisticsPtr->SetLastError(
1904 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1905 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001906 output_file_player_->StopPlayingFile();
1907 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001908 return -1;
1909 }
1910
1911 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001912}
1913
niklase@google.com470e71d2011-07-07 08:21:25 +00001914int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001915 bool loop,
1916 FileFormats format,
1917 int startPosition,
1918 float volumeScaling,
1919 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001920 const CodecInst* codecInst) {
1921 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1922 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1923 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1924 "stopPosition=%d)",
1925 fileName, loop, format, volumeScaling, startPosition,
1926 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001927
kwiberg55b97fe2016-01-28 05:22:45 -08001928 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001929
kwiberg55b97fe2016-01-28 05:22:45 -08001930 if (channel_state_.Get().input_file_playing) {
1931 _engineStatisticsPtr->SetLastError(
1932 VE_ALREADY_PLAYING, kTraceWarning,
1933 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001934 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001935 }
1936
1937 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001938 if (input_file_player_) {
1939 input_file_player_->RegisterModuleFileCallback(NULL);
1940 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001941 }
1942
1943 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001944 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001945 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001946
kwiberg5a25d952016-08-17 07:31:12 -07001947 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001948 _engineStatisticsPtr->SetLastError(
1949 VE_INVALID_ARGUMENT, kTraceError,
1950 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1951 return -1;
1952 }
1953
1954 const uint32_t notificationTime(0);
1955
kwiberg5a25d952016-08-17 07:31:12 -07001956 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001957 fileName, loop, startPosition, volumeScaling, notificationTime,
1958 stopPosition, (const CodecInst*)codecInst) != 0) {
1959 _engineStatisticsPtr->SetLastError(
1960 VE_BAD_FILE, kTraceError,
1961 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001962 input_file_player_->StopPlayingFile();
1963 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001964 return -1;
1965 }
kwiberg5a25d952016-08-17 07:31:12 -07001966 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001967 channel_state_.SetInputFilePlaying(true);
1968
1969 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001970}
1971
1972int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001973 FileFormats format,
1974 int startPosition,
1975 float volumeScaling,
1976 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001977 const CodecInst* codecInst) {
1978 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1979 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1980 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1981 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001982
kwiberg55b97fe2016-01-28 05:22:45 -08001983 if (stream == NULL) {
1984 _engineStatisticsPtr->SetLastError(
1985 VE_BAD_FILE, kTraceError,
1986 "StartPlayingFileAsMicrophone NULL as input stream");
1987 return -1;
1988 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001989
kwiberg55b97fe2016-01-28 05:22:45 -08001990 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001991
kwiberg55b97fe2016-01-28 05:22:45 -08001992 if (channel_state_.Get().input_file_playing) {
1993 _engineStatisticsPtr->SetLastError(
1994 VE_ALREADY_PLAYING, kTraceWarning,
1995 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001996 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001997 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001998
kwiberg55b97fe2016-01-28 05:22:45 -08001999 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002000 if (input_file_player_) {
2001 input_file_player_->RegisterModuleFileCallback(NULL);
2002 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002003 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002004
kwiberg55b97fe2016-01-28 05:22:45 -08002005 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002006 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002007 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002008
kwiberg5a25d952016-08-17 07:31:12 -07002009 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002010 _engineStatisticsPtr->SetLastError(
2011 VE_INVALID_ARGUMENT, kTraceError,
2012 "StartPlayingInputFile() filePlayer format isnot correct");
2013 return -1;
2014 }
2015
2016 const uint32_t notificationTime(0);
2017
kwiberg4ec01d92016-08-22 08:43:54 -07002018 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2019 notificationTime, stopPosition,
2020 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002021 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2022 "StartPlayingFile() failed to start "
2023 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002024 input_file_player_->StopPlayingFile();
2025 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002026 return -1;
2027 }
2028
kwiberg5a25d952016-08-17 07:31:12 -07002029 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002030 channel_state_.SetInputFilePlaying(true);
2031
2032 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002033}
2034
kwiberg55b97fe2016-01-28 05:22:45 -08002035int Channel::StopPlayingFileAsMicrophone() {
2036 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2037 "Channel::StopPlayingFileAsMicrophone()");
2038
2039 rtc::CritScope cs(&_fileCritSect);
2040
2041 if (!channel_state_.Get().input_file_playing) {
2042 return 0;
2043 }
2044
kwiberg5a25d952016-08-17 07:31:12 -07002045 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002046 _engineStatisticsPtr->SetLastError(
2047 VE_STOP_RECORDING_FAILED, kTraceError,
2048 "StopPlayingFile() could not stop playing");
2049 return -1;
2050 }
kwiberg5a25d952016-08-17 07:31:12 -07002051 input_file_player_->RegisterModuleFileCallback(NULL);
2052 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002053 channel_state_.SetInputFilePlaying(false);
2054
2055 return 0;
2056}
2057
2058int Channel::IsPlayingFileAsMicrophone() const {
2059 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002060}
2061
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002062int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002063 const CodecInst* codecInst) {
2064 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2065 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002066
kwiberg55b97fe2016-01-28 05:22:45 -08002067 if (_outputFileRecording) {
2068 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2069 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002070 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002071 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002072
kwiberg55b97fe2016-01-28 05:22:45 -08002073 FileFormats format;
2074 const uint32_t notificationTime(0); // Not supported in VoE
2075 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002076
kwiberg55b97fe2016-01-28 05:22:45 -08002077 if ((codecInst != NULL) &&
2078 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2079 _engineStatisticsPtr->SetLastError(
2080 VE_BAD_ARGUMENT, kTraceError,
2081 "StartRecordingPlayout() invalid compression");
2082 return (-1);
2083 }
2084 if (codecInst == NULL) {
2085 format = kFileFormatPcm16kHzFile;
2086 codecInst = &dummyCodec;
2087 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2088 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2089 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2090 format = kFileFormatWavFile;
2091 } else {
2092 format = kFileFormatCompressedFile;
2093 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002094
kwiberg55b97fe2016-01-28 05:22:45 -08002095 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002096
kwiberg55b97fe2016-01-28 05:22:45 -08002097 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002098 if (output_file_recorder_) {
2099 output_file_recorder_->RegisterModuleFileCallback(NULL);
2100 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002101 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002102
kwiberg5a25d952016-08-17 07:31:12 -07002103 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002104 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002105 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002106 _engineStatisticsPtr->SetLastError(
2107 VE_INVALID_ARGUMENT, kTraceError,
2108 "StartRecordingPlayout() fileRecorder format isnot correct");
2109 return -1;
2110 }
2111
kwiberg5a25d952016-08-17 07:31:12 -07002112 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002113 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2114 _engineStatisticsPtr->SetLastError(
2115 VE_BAD_FILE, kTraceError,
2116 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002117 output_file_recorder_->StopRecording();
2118 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002119 return -1;
2120 }
kwiberg5a25d952016-08-17 07:31:12 -07002121 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002122 _outputFileRecording = true;
2123
2124 return 0;
2125}
2126
2127int Channel::StartRecordingPlayout(OutStream* stream,
2128 const CodecInst* codecInst) {
2129 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2130 "Channel::StartRecordingPlayout()");
2131
2132 if (_outputFileRecording) {
2133 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2134 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002135 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002136 }
2137
2138 FileFormats format;
2139 const uint32_t notificationTime(0); // Not supported in VoE
2140 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2141
2142 if (codecInst != NULL && codecInst->channels != 1) {
2143 _engineStatisticsPtr->SetLastError(
2144 VE_BAD_ARGUMENT, kTraceError,
2145 "StartRecordingPlayout() invalid compression");
2146 return (-1);
2147 }
2148 if (codecInst == NULL) {
2149 format = kFileFormatPcm16kHzFile;
2150 codecInst = &dummyCodec;
2151 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2152 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2153 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2154 format = kFileFormatWavFile;
2155 } else {
2156 format = kFileFormatCompressedFile;
2157 }
2158
2159 rtc::CritScope cs(&_fileCritSect);
2160
2161 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002162 if (output_file_recorder_) {
2163 output_file_recorder_->RegisterModuleFileCallback(NULL);
2164 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002165 }
2166
kwiberg5a25d952016-08-17 07:31:12 -07002167 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002168 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002169 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002170 _engineStatisticsPtr->SetLastError(
2171 VE_INVALID_ARGUMENT, kTraceError,
2172 "StartRecordingPlayout() fileRecorder format isnot correct");
2173 return -1;
2174 }
2175
kwiberg4ec01d92016-08-22 08:43:54 -07002176 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002177 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002178 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2179 "StartRecordingPlayout() failed to "
2180 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002181 output_file_recorder_->StopRecording();
2182 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002183 return -1;
2184 }
2185
kwiberg5a25d952016-08-17 07:31:12 -07002186 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002187 _outputFileRecording = true;
2188
2189 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002190}
2191
kwiberg55b97fe2016-01-28 05:22:45 -08002192int Channel::StopRecordingPlayout() {
2193 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2194 "Channel::StopRecordingPlayout()");
2195
2196 if (!_outputFileRecording) {
2197 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2198 "StopRecordingPlayout() isnot recording");
2199 return -1;
2200 }
2201
2202 rtc::CritScope cs(&_fileCritSect);
2203
kwiberg5a25d952016-08-17 07:31:12 -07002204 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002205 _engineStatisticsPtr->SetLastError(
2206 VE_STOP_RECORDING_FAILED, kTraceError,
2207 "StopRecording() could not stop recording");
2208 return (-1);
2209 }
kwiberg5a25d952016-08-17 07:31:12 -07002210 output_file_recorder_->RegisterModuleFileCallback(NULL);
2211 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002212 _outputFileRecording = false;
2213
2214 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002215}
2216
kwiberg55b97fe2016-01-28 05:22:45 -08002217void Channel::SetMixWithMicStatus(bool mix) {
2218 rtc::CritScope cs(&_fileCritSect);
2219 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002220}
2221
kwiberg55b97fe2016-01-28 05:22:45 -08002222int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2223 int8_t currentLevel = _outputAudioLevel.Level();
2224 level = static_cast<int32_t>(currentLevel);
2225 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002226}
2227
kwiberg55b97fe2016-01-28 05:22:45 -08002228int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2229 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2230 level = static_cast<int32_t>(currentLevel);
2231 return 0;
2232}
2233
solenberg1c2af8e2016-03-24 10:36:00 -07002234int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002235 rtc::CritScope cs(&volume_settings_critsect_);
2236 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002237 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002238 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002239 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002240}
2241
solenberg1c2af8e2016-03-24 10:36:00 -07002242bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002243 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002244 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002245}
2246
kwiberg55b97fe2016-01-28 05:22:45 -08002247int Channel::SetOutputVolumePan(float left, float right) {
2248 rtc::CritScope cs(&volume_settings_critsect_);
2249 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002250 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002251 _panLeft = left;
2252 _panRight = right;
2253 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002254}
2255
kwiberg55b97fe2016-01-28 05:22:45 -08002256int Channel::GetOutputVolumePan(float& left, float& right) const {
2257 rtc::CritScope cs(&volume_settings_critsect_);
2258 left = _panLeft;
2259 right = _panRight;
2260 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002261}
2262
kwiberg55b97fe2016-01-28 05:22:45 -08002263int Channel::SetChannelOutputVolumeScaling(float scaling) {
2264 rtc::CritScope cs(&volume_settings_critsect_);
2265 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002266 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002267 _outputGain = scaling;
2268 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002269}
2270
kwiberg55b97fe2016-01-28 05:22:45 -08002271int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2272 rtc::CritScope cs(&volume_settings_critsect_);
2273 scaling = _outputGain;
2274 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002275}
2276
solenberg8842c3e2016-03-11 03:06:41 -08002277int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002278 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002279 "Channel::SendTelephoneEventOutband(...)");
2280 RTC_DCHECK_LE(0, event);
2281 RTC_DCHECK_GE(255, event);
2282 RTC_DCHECK_LE(0, duration_ms);
2283 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002284 if (!Sending()) {
2285 return -1;
2286 }
solenberg8842c3e2016-03-11 03:06:41 -08002287 if (_rtpRtcpModule->SendTelephoneEventOutband(
2288 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002289 _engineStatisticsPtr->SetLastError(
2290 VE_SEND_DTMF_FAILED, kTraceWarning,
2291 "SendTelephoneEventOutband() failed to send event");
2292 return -1;
2293 }
2294 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002295}
2296
solenberg31642aa2016-03-14 08:00:37 -07002297int Channel::SetSendTelephoneEventPayloadType(int payload_type) {
kwiberg55b97fe2016-01-28 05:22:45 -08002298 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002299 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002300 RTC_DCHECK_LE(0, payload_type);
2301 RTC_DCHECK_GE(127, payload_type);
2302 CodecInst codec = {0};
kwiberg55b97fe2016-01-28 05:22:45 -08002303 codec.plfreq = 8000;
solenberg31642aa2016-03-14 08:00:37 -07002304 codec.pltype = payload_type;
kwiberg55b97fe2016-01-28 05:22:45 -08002305 memcpy(codec.plname, "telephone-event", 16);
2306 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2307 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2308 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2309 _engineStatisticsPtr->SetLastError(
2310 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2311 "SetSendTelephoneEventPayloadType() failed to register send"
2312 "payload type");
2313 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002314 }
kwiberg55b97fe2016-01-28 05:22:45 -08002315 }
kwiberg55b97fe2016-01-28 05:22:45 -08002316 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002317}
2318
kwiberg55b97fe2016-01-28 05:22:45 -08002319int Channel::VoiceActivityIndicator(int& activity) {
2320 activity = _sendFrameType;
2321 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002322}
2323
kwiberg55b97fe2016-01-28 05:22:45 -08002324int Channel::SetLocalSSRC(unsigned int ssrc) {
2325 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2326 "Channel::SetLocalSSRC()");
2327 if (channel_state_.Get().sending) {
2328 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2329 "SetLocalSSRC() already sending");
2330 return -1;
2331 }
2332 _rtpRtcpModule->SetSSRC(ssrc);
2333 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002334}
2335
kwiberg55b97fe2016-01-28 05:22:45 -08002336int Channel::GetLocalSSRC(unsigned int& ssrc) {
2337 ssrc = _rtpRtcpModule->SSRC();
2338 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002339}
2340
kwiberg55b97fe2016-01-28 05:22:45 -08002341int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2342 ssrc = rtp_receiver_->SSRC();
2343 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002344}
2345
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002346int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002347 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002348 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002349}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002350
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002351int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2352 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002353 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2354 if (enable &&
2355 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2356 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002357 return -1;
2358 }
2359 return 0;
2360}
2361
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002362int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2363 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2364}
2365
2366int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2367 rtp_header_parser_->DeregisterRtpHeaderExtension(
2368 kRtpExtensionAbsoluteSendTime);
kwiberg55b97fe2016-01-28 05:22:45 -08002369 if (enable &&
2370 !rtp_header_parser_->RegisterRtpHeaderExtension(
2371 kRtpExtensionAbsoluteSendTime, id)) {
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002372 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002373 }
2374 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002375}
2376
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002377void Channel::EnableSendTransportSequenceNumber(int id) {
2378 int ret =
2379 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2380 RTC_DCHECK_EQ(0, ret);
2381}
2382
stefan3313ec92016-01-21 06:32:43 -08002383void Channel::EnableReceiveTransportSequenceNumber(int id) {
2384 rtp_header_parser_->DeregisterRtpHeaderExtension(
2385 kRtpExtensionTransportSequenceNumber);
2386 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2387 kRtpExtensionTransportSequenceNumber, id);
2388 RTC_DCHECK(ret);
2389}
2390
stefanbba9dec2016-02-01 04:39:55 -08002391void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002392 RtpPacketSender* rtp_packet_sender,
2393 TransportFeedbackObserver* transport_feedback_observer,
2394 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002395 RTC_DCHECK(rtp_packet_sender);
2396 RTC_DCHECK(transport_feedback_observer);
2397 RTC_DCHECK(packet_router && !packet_router_);
2398 feedback_observer_proxy_->SetTransportFeedbackObserver(
2399 transport_feedback_observer);
2400 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2401 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2402 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002403 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002404 packet_router_ = packet_router;
2405}
2406
stefanbba9dec2016-02-01 04:39:55 -08002407void Channel::RegisterReceiverCongestionControlObjects(
2408 PacketRouter* packet_router) {
2409 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002410 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002411 packet_router_ = packet_router;
2412}
2413
2414void Channel::ResetCongestionControlObjects() {
2415 RTC_DCHECK(packet_router_);
2416 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2417 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2418 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002419 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002420 packet_router_ = nullptr;
2421 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2422}
2423
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002424void Channel::SetRTCPStatus(bool enable) {
2425 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2426 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002427 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002428}
2429
kwiberg55b97fe2016-01-28 05:22:45 -08002430int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002431 RtcpMode method = _rtpRtcpModule->RTCP();
2432 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002433 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002434}
2435
kwiberg55b97fe2016-01-28 05:22:45 -08002436int Channel::SetRTCP_CNAME(const char cName[256]) {
2437 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2438 "Channel::SetRTCP_CNAME()");
2439 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2440 _engineStatisticsPtr->SetLastError(
2441 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2442 "SetRTCP_CNAME() failed to set RTCP CNAME");
2443 return -1;
2444 }
2445 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002446}
2447
kwiberg55b97fe2016-01-28 05:22:45 -08002448int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2449 if (cName == NULL) {
2450 _engineStatisticsPtr->SetLastError(
2451 VE_INVALID_ARGUMENT, kTraceError,
2452 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2453 return -1;
2454 }
2455 char cname[RTCP_CNAME_SIZE];
2456 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2457 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2458 _engineStatisticsPtr->SetLastError(
2459 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2460 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2461 return -1;
2462 }
2463 strcpy(cName, cname);
2464 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002465}
2466
kwiberg55b97fe2016-01-28 05:22:45 -08002467int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2468 unsigned int& NTPLow,
2469 unsigned int& timestamp,
2470 unsigned int& playoutTimestamp,
2471 unsigned int* jitter,
2472 unsigned short* fractionLost) {
2473 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002474
kwiberg55b97fe2016-01-28 05:22:45 -08002475 RTCPSenderInfo senderInfo;
2476 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2477 _engineStatisticsPtr->SetLastError(
2478 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2479 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2480 "side");
2481 return -1;
2482 }
2483
2484 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2485 // and octet count)
2486 NTPHigh = senderInfo.NTPseconds;
2487 NTPLow = senderInfo.NTPfraction;
2488 timestamp = senderInfo.RTPtimeStamp;
2489
2490 // --- Locally derived information
2491
2492 // This value is updated on each incoming RTCP packet (0 when no packet
2493 // has been received)
2494 playoutTimestamp = playout_timestamp_rtcp_;
2495
2496 if (NULL != jitter || NULL != fractionLost) {
2497 // Get all RTCP receiver report blocks that have been received on this
2498 // channel. If we receive RTP packets from a remote source we know the
2499 // remote SSRC and use the report block from him.
2500 // Otherwise use the first report block.
2501 std::vector<RTCPReportBlock> remote_stats;
2502 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2503 remote_stats.empty()) {
2504 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2505 "GetRemoteRTCPData() failed to measure statistics due"
2506 " to lack of received RTP and/or RTCP packets");
2507 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002508 }
2509
kwiberg55b97fe2016-01-28 05:22:45 -08002510 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2511 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2512 for (; it != remote_stats.end(); ++it) {
2513 if (it->remoteSSRC == remoteSSRC)
2514 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002515 }
kwiberg55b97fe2016-01-28 05:22:45 -08002516
2517 if (it == remote_stats.end()) {
2518 // If we have not received any RTCP packets from this SSRC it probably
2519 // means that we have not received any RTP packets.
2520 // Use the first received report block instead.
2521 it = remote_stats.begin();
2522 remoteSSRC = it->remoteSSRC;
2523 }
2524
2525 if (jitter) {
2526 *jitter = it->jitter;
2527 }
2528
2529 if (fractionLost) {
2530 *fractionLost = it->fractionLost;
2531 }
2532 }
2533 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002534}
2535
kwiberg55b97fe2016-01-28 05:22:45 -08002536int Channel::SendApplicationDefinedRTCPPacket(
2537 unsigned char subType,
2538 unsigned int name,
2539 const char* data,
2540 unsigned short dataLengthInBytes) {
2541 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2542 "Channel::SendApplicationDefinedRTCPPacket()");
2543 if (!channel_state_.Get().sending) {
2544 _engineStatisticsPtr->SetLastError(
2545 VE_NOT_SENDING, kTraceError,
2546 "SendApplicationDefinedRTCPPacket() not sending");
2547 return -1;
2548 }
2549 if (NULL == data) {
2550 _engineStatisticsPtr->SetLastError(
2551 VE_INVALID_ARGUMENT, kTraceError,
2552 "SendApplicationDefinedRTCPPacket() invalid data value");
2553 return -1;
2554 }
2555 if (dataLengthInBytes % 4 != 0) {
2556 _engineStatisticsPtr->SetLastError(
2557 VE_INVALID_ARGUMENT, kTraceError,
2558 "SendApplicationDefinedRTCPPacket() invalid length value");
2559 return -1;
2560 }
2561 RtcpMode status = _rtpRtcpModule->RTCP();
2562 if (status == RtcpMode::kOff) {
2563 _engineStatisticsPtr->SetLastError(
2564 VE_RTCP_ERROR, kTraceError,
2565 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2566 return -1;
2567 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002568
kwiberg55b97fe2016-01-28 05:22:45 -08002569 // Create and schedule the RTCP APP packet for transmission
2570 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2571 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2572 _engineStatisticsPtr->SetLastError(
2573 VE_SEND_ERROR, kTraceError,
2574 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2575 return -1;
2576 }
2577 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002578}
2579
kwiberg55b97fe2016-01-28 05:22:45 -08002580int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2581 unsigned int& maxJitterMs,
2582 unsigned int& discardedPackets) {
2583 // The jitter statistics is updated for each received RTP packet and is
2584 // based on received packets.
2585 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2586 // If RTCP is off, there is no timed thread in the RTCP module regularly
2587 // generating new stats, trigger the update manually here instead.
2588 StreamStatistician* statistician =
2589 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2590 if (statistician) {
2591 // Don't use returned statistics, use data from proxy instead so that
2592 // max jitter can be fetched atomically.
2593 RtcpStatistics s;
2594 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002595 }
kwiberg55b97fe2016-01-28 05:22:45 -08002596 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002597
kwiberg55b97fe2016-01-28 05:22:45 -08002598 ChannelStatistics stats = statistics_proxy_->GetStats();
2599 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2600 if (playoutFrequency > 0) {
2601 // Scale RTP statistics given the current playout frequency
2602 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2603 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2604 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002605
kwiberg55b97fe2016-01-28 05:22:45 -08002606 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002607
kwiberg55b97fe2016-01-28 05:22:45 -08002608 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002609}
2610
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002611int Channel::GetRemoteRTCPReportBlocks(
2612 std::vector<ReportBlock>* report_blocks) {
2613 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002614 _engineStatisticsPtr->SetLastError(
2615 VE_INVALID_ARGUMENT, kTraceError,
2616 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002617 return -1;
2618 }
2619
2620 // Get the report blocks from the latest received RTCP Sender or Receiver
2621 // Report. Each element in the vector contains the sender's SSRC and a
2622 // report block according to RFC 3550.
2623 std::vector<RTCPReportBlock> rtcp_report_blocks;
2624 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002625 return -1;
2626 }
2627
2628 if (rtcp_report_blocks.empty())
2629 return 0;
2630
2631 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2632 for (; it != rtcp_report_blocks.end(); ++it) {
2633 ReportBlock report_block;
2634 report_block.sender_SSRC = it->remoteSSRC;
2635 report_block.source_SSRC = it->sourceSSRC;
2636 report_block.fraction_lost = it->fractionLost;
2637 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2638 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2639 report_block.interarrival_jitter = it->jitter;
2640 report_block.last_SR_timestamp = it->lastSR;
2641 report_block.delay_since_last_SR = it->delaySinceLastSR;
2642 report_blocks->push_back(report_block);
2643 }
2644 return 0;
2645}
2646
kwiberg55b97fe2016-01-28 05:22:45 -08002647int Channel::GetRTPStatistics(CallStatistics& stats) {
2648 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002649
kwiberg55b97fe2016-01-28 05:22:45 -08002650 // The jitter statistics is updated for each received RTP packet and is
2651 // based on received packets.
2652 RtcpStatistics statistics;
2653 StreamStatistician* statistician =
2654 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002655 if (statistician) {
2656 statistician->GetStatistics(&statistics,
2657 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002658 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002659
kwiberg55b97fe2016-01-28 05:22:45 -08002660 stats.fractionLost = statistics.fraction_lost;
2661 stats.cumulativeLost = statistics.cumulative_lost;
2662 stats.extendedMax = statistics.extended_max_sequence_number;
2663 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002664
kwiberg55b97fe2016-01-28 05:22:45 -08002665 // --- RTT
2666 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002667
kwiberg55b97fe2016-01-28 05:22:45 -08002668 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002669
kwiberg55b97fe2016-01-28 05:22:45 -08002670 size_t bytesSent(0);
2671 uint32_t packetsSent(0);
2672 size_t bytesReceived(0);
2673 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002674
kwiberg55b97fe2016-01-28 05:22:45 -08002675 if (statistician) {
2676 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2677 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002678
kwiberg55b97fe2016-01-28 05:22:45 -08002679 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2680 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2681 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2682 " output will not be complete");
2683 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002684
kwiberg55b97fe2016-01-28 05:22:45 -08002685 stats.bytesSent = bytesSent;
2686 stats.packetsSent = packetsSent;
2687 stats.bytesReceived = bytesReceived;
2688 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002689
kwiberg55b97fe2016-01-28 05:22:45 -08002690 // --- Timestamps
2691 {
2692 rtc::CritScope lock(&ts_stats_lock_);
2693 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2694 }
2695 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002696}
2697
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002698int Channel::SetCodecFECStatus(bool enable) {
2699 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2700 "Channel::SetCodecFECStatus()");
2701
kwibergc8d071e2016-04-06 12:22:38 -07002702 if (!codec_manager_.SetCodecFEC(enable) ||
2703 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002704 _engineStatisticsPtr->SetLastError(
2705 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2706 "SetCodecFECStatus() failed to set FEC state");
2707 return -1;
2708 }
2709 return 0;
2710}
2711
2712bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002713 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002714}
2715
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002716void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2717 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002718 // If pacing is enabled we always store packets.
2719 if (!pacing_enabled_)
2720 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002721 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002722 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002723 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002724 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002725 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002726}
2727
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002728// Called when we are missing one or more packets.
2729int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002730 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2731}
2732
kwiberg55b97fe2016-01-28 05:22:45 -08002733uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2734 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2735 "Channel::Demultiplex()");
2736 _audioFrame.CopyFrom(audioFrame);
2737 _audioFrame.id_ = _channelId;
2738 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002739}
2740
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002741void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002742 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002743 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002744 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002745 CodecInst codec;
2746 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002747
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002748 // Never upsample or upmix the capture signal here. This should be done at the
2749 // end of the send chain.
2750 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2751 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2752 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2753 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002754}
2755
kwiberg55b97fe2016-01-28 05:22:45 -08002756uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2757 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2758 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002759
kwiberg55b97fe2016-01-28 05:22:45 -08002760 if (_audioFrame.samples_per_channel_ == 0) {
2761 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2762 "Channel::PrepareEncodeAndSend() invalid audio frame");
2763 return 0xFFFFFFFF;
2764 }
2765
2766 if (channel_state_.Get().input_file_playing) {
2767 MixOrReplaceAudioWithFile(mixingFrequency);
2768 }
2769
solenberg1c2af8e2016-03-24 10:36:00 -07002770 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2771 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002772
2773 if (channel_state_.Get().input_external_media) {
2774 rtc::CritScope cs(&_callbackCritSect);
2775 const bool isStereo = (_audioFrame.num_channels_ == 2);
2776 if (_inputExternalMediaCallbackPtr) {
2777 _inputExternalMediaCallbackPtr->Process(
2778 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
2779 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
2780 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00002781 }
kwiberg55b97fe2016-01-28 05:22:45 -08002782 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002783
kwiberg55b97fe2016-01-28 05:22:45 -08002784 if (_includeAudioLevelIndication) {
2785 size_t length =
2786 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002787 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002788 if (is_muted && previous_frame_muted_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002789 rms_level_.ProcessMuted(length);
2790 } else {
2791 rms_level_.Process(_audioFrame.data_, length);
niklase@google.com470e71d2011-07-07 08:21:25 +00002792 }
kwiberg55b97fe2016-01-28 05:22:45 -08002793 }
solenberg1c2af8e2016-03-24 10:36:00 -07002794 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002795
kwiberg55b97fe2016-01-28 05:22:45 -08002796 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002797}
2798
kwiberg55b97fe2016-01-28 05:22:45 -08002799uint32_t Channel::EncodeAndSend() {
2800 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2801 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002802
kwiberg55b97fe2016-01-28 05:22:45 -08002803 assert(_audioFrame.num_channels_ <= 2);
2804 if (_audioFrame.samples_per_channel_ == 0) {
2805 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2806 "Channel::EncodeAndSend() invalid audio frame");
2807 return 0xFFFFFFFF;
2808 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002809
kwiberg55b97fe2016-01-28 05:22:45 -08002810 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002811
kwiberg55b97fe2016-01-28 05:22:45 -08002812 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002813
kwiberg55b97fe2016-01-28 05:22:45 -08002814 // The ACM resamples internally.
2815 _audioFrame.timestamp_ = _timeStamp;
2816 // This call will trigger AudioPacketizationCallback::SendData if encoding
2817 // is done and payload is ready for packetization and transmission.
2818 // Otherwise, it will return without invoking the callback.
2819 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2820 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2821 "Channel::EncodeAndSend() ACM encoding failed");
2822 return 0xFFFFFFFF;
2823 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002824
kwiberg55b97fe2016-01-28 05:22:45 -08002825 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2826 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002827}
2828
Minyue2013aec2015-05-13 14:14:42 +02002829void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002830 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002831 Channel* channel = associate_send_channel_.channel();
2832 if (channel && channel->ChannelId() == channel_id) {
2833 // If this channel is associated with a send channel of the specified
2834 // Channel ID, disassociate with it.
2835 ChannelOwner ref(NULL);
2836 associate_send_channel_ = ref;
2837 }
2838}
2839
ivoc14d5dbe2016-07-04 07:06:55 -07002840void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2841 event_log_proxy_->SetEventLog(event_log);
2842}
2843
michaelte0729c52016-10-11 00:29:27 -07002844void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2845 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2846}
2847
kwiberg55b97fe2016-01-28 05:22:45 -08002848int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
2849 VoEMediaProcess& processObject) {
2850 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2851 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002852
kwiberg55b97fe2016-01-28 05:22:45 -08002853 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002854
kwiberg55b97fe2016-01-28 05:22:45 -08002855 if (kPlaybackPerChannel == type) {
2856 if (_outputExternalMediaCallbackPtr) {
2857 _engineStatisticsPtr->SetLastError(
2858 VE_INVALID_OPERATION, kTraceError,
2859 "Channel::RegisterExternalMediaProcessing() "
2860 "output external media already enabled");
2861 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002862 }
kwiberg55b97fe2016-01-28 05:22:45 -08002863 _outputExternalMediaCallbackPtr = &processObject;
2864 _outputExternalMedia = true;
2865 } else if (kRecordingPerChannel == type) {
2866 if (_inputExternalMediaCallbackPtr) {
2867 _engineStatisticsPtr->SetLastError(
2868 VE_INVALID_OPERATION, kTraceError,
2869 "Channel::RegisterExternalMediaProcessing() "
2870 "output external media already enabled");
2871 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002872 }
kwiberg55b97fe2016-01-28 05:22:45 -08002873 _inputExternalMediaCallbackPtr = &processObject;
2874 channel_state_.SetInputExternalMedia(true);
2875 }
2876 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002877}
2878
kwiberg55b97fe2016-01-28 05:22:45 -08002879int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
2880 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2881 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002882
kwiberg55b97fe2016-01-28 05:22:45 -08002883 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002884
kwiberg55b97fe2016-01-28 05:22:45 -08002885 if (kPlaybackPerChannel == type) {
2886 if (!_outputExternalMediaCallbackPtr) {
2887 _engineStatisticsPtr->SetLastError(
2888 VE_INVALID_OPERATION, kTraceWarning,
2889 "Channel::DeRegisterExternalMediaProcessing() "
2890 "output external media already disabled");
2891 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002892 }
kwiberg55b97fe2016-01-28 05:22:45 -08002893 _outputExternalMedia = false;
2894 _outputExternalMediaCallbackPtr = NULL;
2895 } else if (kRecordingPerChannel == type) {
2896 if (!_inputExternalMediaCallbackPtr) {
2897 _engineStatisticsPtr->SetLastError(
2898 VE_INVALID_OPERATION, kTraceWarning,
2899 "Channel::DeRegisterExternalMediaProcessing() "
2900 "input external media already disabled");
2901 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002902 }
kwiberg55b97fe2016-01-28 05:22:45 -08002903 channel_state_.SetInputExternalMedia(false);
2904 _inputExternalMediaCallbackPtr = NULL;
2905 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002906
kwiberg55b97fe2016-01-28 05:22:45 -08002907 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002908}
2909
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002910int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08002911 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2912 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002913
kwiberg55b97fe2016-01-28 05:22:45 -08002914 if (channel_state_.Get().playing) {
2915 _engineStatisticsPtr->SetLastError(
2916 VE_INVALID_OPERATION, kTraceError,
2917 "Channel::SetExternalMixing() "
2918 "external mixing cannot be changed while playing.");
2919 return -1;
2920 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002921
kwiberg55b97fe2016-01-28 05:22:45 -08002922 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002923
kwiberg55b97fe2016-01-28 05:22:45 -08002924 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002925}
2926
kwiberg55b97fe2016-01-28 05:22:45 -08002927int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2928 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002929}
2930
wu@webrtc.org24301a62013-12-13 19:17:43 +00002931void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2932 audio_coding_->GetDecodingCallStatistics(stats);
2933}
2934
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002935bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
2936 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08002937 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07002938 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002939 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002940 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002941}
2942
solenberg358057b2015-11-27 10:46:42 -08002943uint32_t Channel::GetDelayEstimate() const {
2944 int jitter_buffer_delay_ms = 0;
2945 int playout_buffer_delay_ms = 0;
2946 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
2947 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
2948}
2949
deadbeef74375882015-08-13 12:09:10 -07002950int Channel::LeastRequiredDelayMs() const {
2951 return audio_coding_->LeastRequiredDelayMs();
2952}
2953
kwiberg55b97fe2016-01-28 05:22:45 -08002954int Channel::SetMinimumPlayoutDelay(int delayMs) {
2955 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2956 "Channel::SetMinimumPlayoutDelay()");
2957 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2958 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2959 _engineStatisticsPtr->SetLastError(
2960 VE_INVALID_ARGUMENT, kTraceError,
2961 "SetMinimumPlayoutDelay() invalid min delay");
2962 return -1;
2963 }
2964 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2965 _engineStatisticsPtr->SetLastError(
2966 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2967 "SetMinimumPlayoutDelay() failed to set min playout delay");
2968 return -1;
2969 }
2970 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002971}
2972
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002973int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002974 uint32_t playout_timestamp_rtp = 0;
2975 {
tommi31fc21f2016-01-21 10:37:37 -08002976 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002977 playout_timestamp_rtp = playout_timestamp_rtp_;
2978 }
kwiberg55b97fe2016-01-28 05:22:45 -08002979 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002980 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002981 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002982 "GetPlayoutTimestamp() failed to retrieve timestamp");
2983 return -1;
2984 }
deadbeef74375882015-08-13 12:09:10 -07002985 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002986 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002987}
2988
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002989int Channel::SetInitTimestamp(unsigned int timestamp) {
2990 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002991 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002992 if (channel_state_.Get().sending) {
2993 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
2994 "SetInitTimestamp() already sending");
2995 return -1;
2996 }
2997 _rtpRtcpModule->SetStartTimestamp(timestamp);
2998 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002999}
3000
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003001int Channel::SetInitSequenceNumber(short sequenceNumber) {
3002 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3003 "Channel::SetInitSequenceNumber()");
3004 if (channel_state_.Get().sending) {
3005 _engineStatisticsPtr->SetLastError(
3006 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3007 return -1;
3008 }
3009 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3010 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003011}
3012
kwiberg55b97fe2016-01-28 05:22:45 -08003013int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
3014 RtpReceiver** rtp_receiver) const {
3015 *rtpRtcpModule = _rtpRtcpModule.get();
3016 *rtp_receiver = rtp_receiver_.get();
3017 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003018}
3019
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003020// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3021// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08003022int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08003023 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08003024 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003025
kwiberg55b97fe2016-01-28 05:22:45 -08003026 {
3027 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003028
kwiberg5a25d952016-08-17 07:31:12 -07003029 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003030 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3031 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3032 " doesnt exist");
3033 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003034 }
3035
kwiberg4ec01d92016-08-22 08:43:54 -07003036 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07003037 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003038 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3039 "Channel::MixOrReplaceAudioWithFile() file mixing "
3040 "failed");
3041 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003042 }
kwiberg55b97fe2016-01-28 05:22:45 -08003043 if (fileSamples == 0) {
3044 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3045 "Channel::MixOrReplaceAudioWithFile() file is ended");
3046 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003047 }
kwiberg55b97fe2016-01-28 05:22:45 -08003048 }
3049
3050 assert(_audioFrame.samples_per_channel_ == fileSamples);
3051
3052 if (_mixFileWithMicrophone) {
3053 // Currently file stream is always mono.
3054 // TODO(xians): Change the code when FilePlayer supports real stereo.
3055 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3056 1, fileSamples);
3057 } else {
3058 // Replace ACM audio with file.
3059 // Currently file stream is always mono.
3060 // TODO(xians): Change the code when FilePlayer supports real stereo.
3061 _audioFrame.UpdateFrame(
3062 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3063 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3064 }
3065 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003066}
3067
kwiberg55b97fe2016-01-28 05:22:45 -08003068int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3069 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003070
kwibergb7f89d62016-02-17 10:04:18 -08003071 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003072 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003073
kwiberg55b97fe2016-01-28 05:22:45 -08003074 {
3075 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003076
kwiberg5a25d952016-08-17 07:31:12 -07003077 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003078 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3079 "Channel::MixAudioWithFile() file mixing failed");
3080 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003081 }
3082
kwiberg55b97fe2016-01-28 05:22:45 -08003083 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003084 if (output_file_player_->Get10msAudioFromFile(
3085 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003086 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3087 "Channel::MixAudioWithFile() file mixing failed");
3088 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003089 }
kwiberg55b97fe2016-01-28 05:22:45 -08003090 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003091
kwiberg55b97fe2016-01-28 05:22:45 -08003092 if (audioFrame.samples_per_channel_ == fileSamples) {
3093 // Currently file stream is always mono.
3094 // TODO(xians): Change the code when FilePlayer supports real stereo.
3095 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3096 fileSamples);
3097 } else {
3098 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3099 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3100 ") != "
3101 "fileSamples(%" PRIuS ")",
3102 audioFrame.samples_per_channel_, fileSamples);
3103 return -1;
3104 }
3105
3106 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003107}
3108
deadbeef74375882015-08-13 12:09:10 -07003109void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003110 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003111
henrik.lundin96bd5022016-04-06 04:13:56 -07003112 if (!jitter_buffer_playout_timestamp_) {
3113 // This can happen if this channel has not received any RTP packets. In
3114 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003115 return;
3116 }
3117
3118 uint16_t delay_ms = 0;
3119 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003120 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003121 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3122 " delay from the ADM");
3123 _engineStatisticsPtr->SetLastError(
3124 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3125 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3126 return;
3127 }
3128
henrik.lundin96bd5022016-04-06 04:13:56 -07003129 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3130 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003131
3132 // Remove the playout delay.
henrik.lundin96bd5022016-04-06 04:13:56 -07003133 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003134
kwiberg55b97fe2016-01-28 05:22:45 -08003135 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003136 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003137 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003138
3139 {
tommi31fc21f2016-01-21 10:37:37 -08003140 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003141 if (rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003142 playout_timestamp_rtcp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003143 } else {
henrik.lundin96bd5022016-04-06 04:13:56 -07003144 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003145 }
3146 playout_delay_ms_ = delay_ms;
3147 }
3148}
3149
kwiberg55b97fe2016-01-28 05:22:45 -08003150void Channel::RegisterReceiveCodecsToRTPModule() {
3151 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3152 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003153
kwiberg55b97fe2016-01-28 05:22:45 -08003154 CodecInst codec;
3155 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003156
kwiberg55b97fe2016-01-28 05:22:45 -08003157 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3158 // Open up the RTP/RTCP receiver for all supported codecs
3159 if ((audio_coding_->Codec(idx, &codec) == -1) ||
3160 (rtp_receiver_->RegisterReceivePayload(
3161 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3162 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
3163 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3164 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3165 " to register %s (%d/%d/%" PRIuS
3166 "/%d) to RTP/RTCP "
3167 "receiver",
3168 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3169 codec.rate);
3170 } else {
3171 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3172 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3173 "(%d/%d/%" PRIuS
3174 "/%d) has been added to the RTP/RTCP "
3175 "receiver",
3176 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3177 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003178 }
kwiberg55b97fe2016-01-28 05:22:45 -08003179 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003180}
3181
kwiberg55b97fe2016-01-28 05:22:45 -08003182int Channel::SetSendRtpHeaderExtension(bool enable,
3183 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003184 unsigned char id) {
3185 int error = 0;
3186 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3187 if (enable) {
3188 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3189 }
3190 return error;
3191}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003192
henrik.lundinb3e30012016-08-31 14:09:51 -07003193int32_t Channel::GetPlayoutFrequency() const {
wu@webrtc.org94454b72014-06-05 20:34:08 +00003194 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
3195 CodecInst current_recive_codec;
3196 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
3197 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
3198 // Even though the actual sampling rate for G.722 audio is
3199 // 16,000 Hz, the RTP clock rate for the G722 payload format is
3200 // 8,000 Hz because that value was erroneously assigned in
3201 // RFC 1890 and must remain unchanged for backward compatibility.
3202 playout_frequency = 8000;
3203 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
3204 // We are resampling Opus internally to 32,000 Hz until all our
3205 // DSP routines can operate at 48,000 Hz, but the RTP clock
3206 // rate for the Opus payload format is standardized to 48,000 Hz,
3207 // because that is the maximum supported decoding sampling rate.
3208 playout_frequency = 48000;
3209 }
3210 }
3211 return playout_frequency;
3212}
3213
Minyue2013aec2015-05-13 14:14:42 +02003214int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003215 RtcpMode method = _rtpRtcpModule->RTCP();
3216 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003217 return 0;
3218 }
3219 std::vector<RTCPReportBlock> report_blocks;
3220 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003221
3222 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003223 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003224 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003225 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003226 Channel* channel = associate_send_channel_.channel();
3227 // Tries to get RTT from an associated channel. This is important for
3228 // receive-only channels.
3229 if (channel) {
3230 // To prevent infinite recursion and deadlock, calling GetRTT of
3231 // associate channel should always use "false" for argument:
3232 // |allow_associate_channel|.
3233 rtt = channel->GetRTT(false);
3234 }
3235 }
3236 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003237 }
3238
3239 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3240 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3241 for (; it != report_blocks.end(); ++it) {
3242 if (it->remoteSSRC == remoteSSRC)
3243 break;
3244 }
3245 if (it == report_blocks.end()) {
3246 // We have not received packets with SSRC matching the report blocks.
3247 // To calculate RTT we try with the SSRC of the first report block.
3248 // This is very important for send-only channels where we don't know
3249 // the SSRC of the other end.
3250 remoteSSRC = report_blocks[0].remoteSSRC;
3251 }
Minyue2013aec2015-05-13 14:14:42 +02003252
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003253 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003254 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003255 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003256 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3257 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003258 return 0;
3259 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003260 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003261}
3262
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003263} // namespace voe
3264} // namespace webrtc