blob: 6d053323da591c62e404d2f155466f7124c88ed0 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrika@webrtc.org2919e952012-01-31 08:45:03 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000011#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Henrik Lundin64dad832015-05-11 12:44:23 +020013#include <algorithm>
Tommif888bb52015-12-12 01:37:01 +010014#include <utility>
Henrik Lundin64dad832015-05-11 12:44:23 +020015
Ivo Creusenae856f22015-09-17 16:30:16 +020016#include "webrtc/base/checks.h"
tommi31fc21f2016-01-21 10:37:37 -080017#include "webrtc/base/criticalsection.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000018#include "webrtc/base/format_macros.h"
pbosad856222015-11-27 09:48:36 -080019#include "webrtc/base/logging.h"
Erik Språng737336d2016-07-29 12:59:36 +020020#include "webrtc/base/rate_limiter.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010021#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000022#include "webrtc/base/timeutils.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020023#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070024#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000025#include "webrtc/modules/audio_device/include/audio_device.h"
26#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010027#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010028#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010029#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
30#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
31#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000032#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010033#include "webrtc/modules/utility/include/audio_frame_operations.h"
34#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010035#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000036#include "webrtc/voice_engine/include/voe_external_media.h"
37#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
38#include "webrtc/voice_engine/output_mixer.h"
39#include "webrtc/voice_engine/statistics.h"
40#include "webrtc/voice_engine/transmit_mixer.h"
41#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000042
andrew@webrtc.org50419b02012-11-14 19:07:54 +000043namespace webrtc {
44namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000045
kwibergc8d071e2016-04-06 12:22:38 -070046namespace {
47
Erik Språng737336d2016-07-29 12:59:36 +020048constexpr int64_t kMaxRetransmissionWindowMs = 1000;
49constexpr int64_t kMinRetransmissionWindowMs = 30;
50
kwibergc8d071e2016-04-06 12:22:38 -070051bool RegisterReceiveCodec(std::unique_ptr<AudioCodingModule>* acm,
52 acm2::RentACodec* rac,
53 const CodecInst& ci) {
kwibergabe95ba2016-06-02 02:58:59 -070054 const int result = (*acm)->RegisterReceiveCodec(
55 ci, [&] { return rac->RentIsacDecoder(ci.plfreq); });
kwibergc8d071e2016-04-06 12:22:38 -070056 return result == 0;
57}
58
59} // namespace
60
solenberg8842c3e2016-03-11 03:06:41 -080061const int kTelephoneEventAttenuationdB = 10;
62
ivoc14d5dbe2016-07-04 07:06:55 -070063class RtcEventLogProxy final : public webrtc::RtcEventLog {
64 public:
65 RtcEventLogProxy() : event_log_(nullptr) {}
66
67 bool StartLogging(const std::string& file_name,
68 int64_t max_size_bytes) override {
69 RTC_NOTREACHED();
70 return false;
71 }
72
73 bool StartLogging(rtc::PlatformFile log_file,
74 int64_t max_size_bytes) override {
75 RTC_NOTREACHED();
76 return false;
77 }
78
79 void StopLogging() override { RTC_NOTREACHED(); }
80
81 void LogVideoReceiveStreamConfig(
82 const webrtc::VideoReceiveStream::Config& config) override {
83 rtc::CritScope lock(&crit_);
84 if (event_log_) {
85 event_log_->LogVideoReceiveStreamConfig(config);
86 }
87 }
88
89 void LogVideoSendStreamConfig(
90 const webrtc::VideoSendStream::Config& config) override {
91 rtc::CritScope lock(&crit_);
92 if (event_log_) {
93 event_log_->LogVideoSendStreamConfig(config);
94 }
95 }
96
ivoce0928d82016-10-10 05:12:51 -070097 void LogAudioReceiveStreamConfig(
98 const webrtc::AudioReceiveStream::Config& config) override {
99 rtc::CritScope lock(&crit_);
100 if (event_log_) {
101 event_log_->LogAudioReceiveStreamConfig(config);
102 }
103 }
104
105 void LogAudioSendStreamConfig(
106 const webrtc::AudioSendStream::Config& config) override {
107 rtc::CritScope lock(&crit_);
108 if (event_log_) {
109 event_log_->LogAudioSendStreamConfig(config);
110 }
111 }
112
ivoc14d5dbe2016-07-04 07:06:55 -0700113 void LogRtpHeader(webrtc::PacketDirection direction,
114 webrtc::MediaType media_type,
115 const uint8_t* header,
116 size_t packet_length) override {
117 rtc::CritScope lock(&crit_);
118 if (event_log_) {
119 event_log_->LogRtpHeader(direction, media_type, header, packet_length);
120 }
121 }
122
123 void LogRtcpPacket(webrtc::PacketDirection direction,
124 webrtc::MediaType media_type,
125 const uint8_t* packet,
126 size_t length) override {
127 rtc::CritScope lock(&crit_);
128 if (event_log_) {
129 event_log_->LogRtcpPacket(direction, media_type, packet, length);
130 }
131 }
132
133 void LogAudioPlayout(uint32_t ssrc) override {
134 rtc::CritScope lock(&crit_);
135 if (event_log_) {
136 event_log_->LogAudioPlayout(ssrc);
137 }
138 }
139
140 void LogBwePacketLossEvent(int32_t bitrate,
141 uint8_t fraction_loss,
142 int32_t total_packets) override {
143 rtc::CritScope lock(&crit_);
144 if (event_log_) {
145 event_log_->LogBwePacketLossEvent(bitrate, fraction_loss, total_packets);
146 }
147 }
148
149 void SetEventLog(RtcEventLog* event_log) {
150 rtc::CritScope lock(&crit_);
151 event_log_ = event_log;
152 }
153
154 private:
155 rtc::CriticalSection crit_;
156 RtcEventLog* event_log_ GUARDED_BY(crit_);
157 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
158};
159
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100160class TransportFeedbackProxy : public TransportFeedbackObserver {
161 public:
162 TransportFeedbackProxy() : feedback_observer_(nullptr) {
163 pacer_thread_.DetachFromThread();
164 network_thread_.DetachFromThread();
165 }
166
167 void SetTransportFeedbackObserver(
168 TransportFeedbackObserver* feedback_observer) {
169 RTC_DCHECK(thread_checker_.CalledOnValidThread());
170 rtc::CritScope lock(&crit_);
171 feedback_observer_ = feedback_observer;
172 }
173
174 // Implements TransportFeedbackObserver.
175 void AddPacket(uint16_t sequence_number,
176 size_t length,
philipela1ed0b32016-06-01 06:31:17 -0700177 int probe_cluster_id) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100178 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
179 rtc::CritScope lock(&crit_);
180 if (feedback_observer_)
pbos2169d8b2016-06-20 11:53:02 -0700181 feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100182 }
183 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
184 RTC_DCHECK(network_thread_.CalledOnValidThread());
185 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700186 if (feedback_observer_)
187 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200188 }
189 std::vector<PacketInfo> GetTransportFeedbackVector() const override {
190 RTC_NOTREACHED();
191 return std::vector<PacketInfo>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100192 }
193
194 private:
195 rtc::CriticalSection crit_;
196 rtc::ThreadChecker thread_checker_;
197 rtc::ThreadChecker pacer_thread_;
198 rtc::ThreadChecker network_thread_;
199 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
200};
201
202class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
203 public:
204 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
205 pacer_thread_.DetachFromThread();
206 }
207
208 void SetSequenceNumberAllocator(
209 TransportSequenceNumberAllocator* seq_num_allocator) {
210 RTC_DCHECK(thread_checker_.CalledOnValidThread());
211 rtc::CritScope lock(&crit_);
212 seq_num_allocator_ = seq_num_allocator;
213 }
214
215 // Implements TransportSequenceNumberAllocator.
216 uint16_t AllocateSequenceNumber() override {
217 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
218 rtc::CritScope lock(&crit_);
219 if (!seq_num_allocator_)
220 return 0;
221 return seq_num_allocator_->AllocateSequenceNumber();
222 }
223
224 private:
225 rtc::CriticalSection crit_;
226 rtc::ThreadChecker thread_checker_;
227 rtc::ThreadChecker pacer_thread_;
228 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
229};
230
231class RtpPacketSenderProxy : public RtpPacketSender {
232 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800233 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100234
235 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
236 RTC_DCHECK(thread_checker_.CalledOnValidThread());
237 rtc::CritScope lock(&crit_);
238 rtp_packet_sender_ = rtp_packet_sender;
239 }
240
241 // Implements RtpPacketSender.
242 void InsertPacket(Priority priority,
243 uint32_t ssrc,
244 uint16_t sequence_number,
245 int64_t capture_time_ms,
246 size_t bytes,
247 bool retransmission) override {
248 rtc::CritScope lock(&crit_);
249 if (rtp_packet_sender_) {
250 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
251 capture_time_ms, bytes, retransmission);
252 }
253 }
254
255 private:
256 rtc::ThreadChecker thread_checker_;
257 rtc::CriticalSection crit_;
258 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
259};
260
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000261// Extend the default RTCP statistics struct with max_jitter, defined as the
262// maximum jitter value seen in an RTCP report block.
263struct ChannelStatistics : public RtcpStatistics {
264 ChannelStatistics() : rtcp(), max_jitter(0) {}
265
266 RtcpStatistics rtcp;
267 uint32_t max_jitter;
268};
269
270// Statistics callback, called at each generation of a new RTCP report block.
271class StatisticsProxy : public RtcpStatisticsCallback {
272 public:
tommi31fc21f2016-01-21 10:37:37 -0800273 StatisticsProxy(uint32_t ssrc) : ssrc_(ssrc) {}
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000274 virtual ~StatisticsProxy() {}
275
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000276 void StatisticsUpdated(const RtcpStatistics& statistics,
277 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000278 if (ssrc != ssrc_)
279 return;
280
tommi31fc21f2016-01-21 10:37:37 -0800281 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000282 stats_.rtcp = statistics;
283 if (statistics.jitter > stats_.max_jitter) {
284 stats_.max_jitter = statistics.jitter;
285 }
286 }
287
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000288 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000289
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000290 ChannelStatistics GetStats() {
tommi31fc21f2016-01-21 10:37:37 -0800291 rtc::CritScope cs(&stats_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000292 return stats_;
293 }
294
295 private:
296 // StatisticsUpdated calls are triggered from threads in the RTP module,
297 // while GetStats calls can be triggered from the public voice engine API,
298 // hence synchronization is needed.
tommi31fc21f2016-01-21 10:37:37 -0800299 rtc::CriticalSection stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000300 const uint32_t ssrc_;
301 ChannelStatistics stats_;
302};
303
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000304class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000305 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000306 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
307 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000308
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000309 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
310 // Not used for Voice Engine.
311 }
312
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000313 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
314 int64_t rtt,
315 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000316 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
317 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
318 // report for VoiceEngine?
319 if (report_blocks.empty())
320 return;
321
322 int fraction_lost_aggregate = 0;
323 int total_number_of_packets = 0;
324
325 // If receiving multiple report blocks, calculate the weighted average based
326 // on the number of packets a report refers to.
327 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
328 block_it != report_blocks.end(); ++block_it) {
329 // Find the previous extended high sequence number for this remote SSRC,
330 // to calculate the number of RTP packets this report refers to. Ignore if
331 // we haven't seen this SSRC before.
332 std::map<uint32_t, uint32_t>::iterator seq_num_it =
333 extended_max_sequence_number_.find(block_it->sourceSSRC);
334 int number_of_packets = 0;
335 if (seq_num_it != extended_max_sequence_number_.end()) {
336 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
337 }
338 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
339 total_number_of_packets += number_of_packets;
340
341 extended_max_sequence_number_[block_it->sourceSSRC] =
342 block_it->extendedHighSeqNum;
343 }
344 int weighted_fraction_lost = 0;
345 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800346 weighted_fraction_lost =
347 (fraction_lost_aggregate + total_number_of_packets / 2) /
348 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000349 }
350 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000351 }
352
353 private:
354 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000355 // Maps remote side ssrc to extended highest sequence number received.
356 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000357};
358
kwiberg55b97fe2016-01-28 05:22:45 -0800359int32_t Channel::SendData(FrameType frameType,
360 uint8_t payloadType,
361 uint32_t timeStamp,
362 const uint8_t* payloadData,
363 size_t payloadSize,
364 const RTPFragmentationHeader* fragmentation) {
365 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
366 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
367 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
368 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000369
kwiberg55b97fe2016-01-28 05:22:45 -0800370 if (_includeAudioLevelIndication) {
371 // Store current audio level in the RTP/RTCP module.
372 // The level will be used in combination with voice-activity state
373 // (frameType) to add an RTP header extension
374 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
375 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000376
kwiberg55b97fe2016-01-28 05:22:45 -0800377 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
378 // packetization.
379 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700380 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800381 (FrameType&)frameType, payloadType, timeStamp,
382 // Leaving the time when this frame was
383 // received from the capture device as
384 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700385 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800386 _engineStatisticsPtr->SetLastError(
387 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
388 "Channel::SendData() failed to send data to RTP/RTCP module");
389 return -1;
390 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000391
kwiberg55b97fe2016-01-28 05:22:45 -0800392 _lastLocalTimeStamp = timeStamp;
393 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000394
kwiberg55b97fe2016-01-28 05:22:45 -0800395 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000396}
397
kwiberg55b97fe2016-01-28 05:22:45 -0800398int32_t Channel::InFrameType(FrameType frame_type) {
399 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
400 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000401
kwiberg55b97fe2016-01-28 05:22:45 -0800402 rtc::CritScope cs(&_callbackCritSect);
403 _sendFrameType = (frame_type == kAudioFrameSpeech);
404 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000405}
406
stefan1d8a5062015-10-02 03:39:33 -0700407bool Channel::SendRtp(const uint8_t* data,
408 size_t len,
409 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800410 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
411 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000412
kwiberg55b97fe2016-01-28 05:22:45 -0800413 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000414
kwiberg55b97fe2016-01-28 05:22:45 -0800415 if (_transportPtr == NULL) {
416 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
417 "Channel::SendPacket() failed to send RTP packet due to"
418 " invalid transport object");
419 return false;
420 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000421
kwiberg55b97fe2016-01-28 05:22:45 -0800422 uint8_t* bufferToSendPtr = (uint8_t*)data;
423 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000424
kwiberg55b97fe2016-01-28 05:22:45 -0800425 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
426 std::string transport_name =
427 _externalTransport ? "external transport" : "WebRtc sockets";
428 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
429 "Channel::SendPacket() RTP transmission using %s failed",
430 transport_name.c_str());
431 return false;
432 }
433 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000434}
435
kwiberg55b97fe2016-01-28 05:22:45 -0800436bool Channel::SendRtcp(const uint8_t* data, size_t len) {
437 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
438 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
kwiberg55b97fe2016-01-28 05:22:45 -0800440 rtc::CritScope cs(&_callbackCritSect);
441 if (_transportPtr == NULL) {
442 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
443 "Channel::SendRtcp() failed to send RTCP packet"
444 " due to invalid transport object");
445 return false;
446 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000447
kwiberg55b97fe2016-01-28 05:22:45 -0800448 uint8_t* bufferToSendPtr = (uint8_t*)data;
449 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000450
kwiberg55b97fe2016-01-28 05:22:45 -0800451 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
452 if (n < 0) {
453 std::string transport_name =
454 _externalTransport ? "external transport" : "WebRtc sockets";
455 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
456 "Channel::SendRtcp() 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 -0800463void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
464 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
465 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000466
kwiberg55b97fe2016-01-28 05:22:45 -0800467 // Update ssrc so that NTP for AV sync can be updated.
468 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000469}
470
Peter Boströmac547a62015-09-17 23:03:57 +0200471void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
472 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
473 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
474 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000475}
476
Peter Boströmac547a62015-09-17 23:03:57 +0200477int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000478 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000479 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000480 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800481 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200482 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800483 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
484 "Channel::OnInitializeDecoder(payloadType=%d, "
485 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
486 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000487
kwiberg55b97fe2016-01-28 05:22:45 -0800488 CodecInst receiveCodec = {0};
489 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000490
kwiberg55b97fe2016-01-28 05:22:45 -0800491 receiveCodec.pltype = payloadType;
492 receiveCodec.plfreq = frequency;
493 receiveCodec.channels = channels;
494 receiveCodec.rate = rate;
495 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000496
kwiberg55b97fe2016-01-28 05:22:45 -0800497 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
498 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000499
kwiberg55b97fe2016-01-28 05:22:45 -0800500 // Register the new codec to the ACM
kwibergc8d071e2016-04-06 12:22:38 -0700501 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, receiveCodec)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800502 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
503 "Channel::OnInitializeDecoder() invalid codec ("
504 "pt=%d, name=%s) received - 1",
505 payloadType, payloadName);
506 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
507 return -1;
508 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000509
kwiberg55b97fe2016-01-28 05:22:45 -0800510 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000511}
512
kwiberg55b97fe2016-01-28 05:22:45 -0800513int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
514 size_t payloadSize,
515 const WebRtcRTPHeader* rtpHeader) {
516 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
517 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
518 ","
519 " payloadType=%u, audioChannel=%" PRIuS ")",
520 payloadSize, rtpHeader->header.payloadType,
521 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000522
kwiberg55b97fe2016-01-28 05:22:45 -0800523 if (!channel_state_.Get().playing) {
524 // Avoid inserting into NetEQ when we are not playing. Count the
525 // packet as discarded.
526 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
527 "received packet is discarded since playing is not"
528 " activated");
529 _numberOfDiscardedPackets++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000530 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800531 }
532
533 // Push the incoming payload (parsed and ready for decoding) into the ACM
534 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
535 0) {
536 _engineStatisticsPtr->SetLastError(
537 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
538 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
539 return -1;
540 }
541
kwiberg55b97fe2016-01-28 05:22:45 -0800542 int64_t round_trip_time = 0;
543 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
544 NULL);
545
546 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
547 if (!nack_list.empty()) {
548 // Can't use nack_list.data() since it's not supported by all
549 // compilers.
550 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
551 }
552 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000553}
554
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000555bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000556 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000557 RTPHeader header;
558 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
559 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
560 "IncomingPacket invalid RTP header");
561 return false;
562 }
563 header.payload_type_frequency =
564 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
565 if (header.payload_type_frequency < 0)
566 return false;
567 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
568}
569
henrik.lundin42dda502016-05-18 05:36:01 -0700570MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
571 int32_t id,
572 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700573 unsigned int ssrc;
574 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
575 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800576 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700577 bool muted;
578 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
579 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800580 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
581 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
582 // In all likelihood, the audio in this frame is garbage. We return an
583 // error so that the audio mixer module doesn't add it to the mix. As
584 // a result, it won't be played out and the actions skipped here are
585 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700586 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800587 }
henrik.lundina89ab962016-05-18 08:52:45 -0700588
589 if (muted) {
590 // TODO(henrik.lundin): We should be able to do better than this. But we
591 // will have to go through all the cases below where the audio samples may
592 // be used, and handle the muted case in some way.
593 audioFrame->Mute();
594 }
kwiberg55b97fe2016-01-28 05:22:45 -0800595
kwiberg55b97fe2016-01-28 05:22:45 -0800596 // Convert module ID to internal VoE channel ID
597 audioFrame->id_ = VoEChannelId(audioFrame->id_);
598 // Store speech type for dead-or-alive detection
599 _outputSpeechType = audioFrame->speech_type_;
600
601 ChannelState::State state = channel_state_.Get();
602
kwiberg55b97fe2016-01-28 05:22:45 -0800603 {
604 // Pass the audio buffers to an optional sink callback, before applying
605 // scaling/panning, as that applies to the mix operation.
606 // External recipients of the audio (e.g. via AudioTrack), will do their
607 // own mixing/dynamic processing.
608 rtc::CritScope cs(&_callbackCritSect);
609 if (audio_sink_) {
610 AudioSinkInterface::Data data(
611 &audioFrame->data_[0], audioFrame->samples_per_channel_,
612 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
613 audioFrame->timestamp_);
614 audio_sink_->OnData(data);
615 }
616 }
617
618 float output_gain = 1.0f;
619 float left_pan = 1.0f;
620 float right_pan = 1.0f;
621 {
622 rtc::CritScope cs(&volume_settings_critsect_);
623 output_gain = _outputGain;
624 left_pan = _panLeft;
625 right_pan = _panRight;
626 }
627
628 // Output volume scaling
629 if (output_gain < 0.99f || output_gain > 1.01f) {
630 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
631 }
632
633 // Scale left and/or right channel(s) if stereo and master balance is
634 // active
635
636 if (left_pan != 1.0f || right_pan != 1.0f) {
637 if (audioFrame->num_channels_ == 1) {
638 // Emulate stereo mode since panning is active.
639 // The mono signal is copied to both left and right channels here.
640 AudioFrameOperations::MonoToStereo(audioFrame);
641 }
642 // For true stereo mode (when we are receiving a stereo signal), no
643 // action is needed.
644
645 // Do the panning operation (the audio frame contains stereo at this
646 // stage)
647 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
648 }
649
650 // Mix decoded PCM output with file if file mixing is enabled
651 if (state.output_file_playing) {
652 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700653 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800654 }
655
656 // External media
657 if (_outputExternalMedia) {
658 rtc::CritScope cs(&_callbackCritSect);
659 const bool isStereo = (audioFrame->num_channels_ == 2);
660 if (_outputExternalMediaCallbackPtr) {
661 _outputExternalMediaCallbackPtr->Process(
662 _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_,
663 audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_,
664 isStereo);
665 }
666 }
667
668 // Record playout if enabled
669 {
670 rtc::CritScope cs(&_fileCritSect);
671
kwiberg5a25d952016-08-17 07:31:12 -0700672 if (_outputFileRecording && output_file_recorder_) {
673 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800674 }
675 }
676
677 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700678 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800679 _outputAudioLevel.ComputeLevel(*audioFrame);
680
681 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
682 // The first frame with a valid rtp timestamp.
683 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
684 }
685
686 if (capture_start_rtp_time_stamp_ >= 0) {
687 // audioFrame.timestamp_ should be valid from now on.
688
689 // Compute elapsed time.
690 int64_t unwrap_timestamp =
691 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
692 audioFrame->elapsed_time_ms_ =
693 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700694 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800695
niklase@google.com470e71d2011-07-07 08:21:25 +0000696 {
kwiberg55b97fe2016-01-28 05:22:45 -0800697 rtc::CritScope lock(&ts_stats_lock_);
698 // Compute ntp time.
699 audioFrame->ntp_time_ms_ =
700 ntp_estimator_.Estimate(audioFrame->timestamp_);
701 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
702 if (audioFrame->ntp_time_ms_ > 0) {
703 // Compute |capture_start_ntp_time_ms_| so that
704 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
705 capture_start_ntp_time_ms_ =
706 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000707 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000708 }
kwiberg55b97fe2016-01-28 05:22:45 -0800709 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000710
henrik.lundin42dda502016-05-18 05:36:01 -0700711 return muted ? MixerParticipant::AudioFrameInfo::kMuted
712 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000713}
714
aleloi6c278492016-10-20 14:24:39 -0700715AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
716 int sample_rate_hz,
717 AudioFrame* audio_frame) {
718 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700719
aleloi6c278492016-10-20 14:24:39 -0700720 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700721
722 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
723 FrameInfo new_audio_frame_info = FrameInfo::kError;
724 switch (frame_info) {
725 case MixerParticipant::AudioFrameInfo::kNormal:
726 new_audio_frame_info = FrameInfo::kNormal;
727 break;
728 case MixerParticipant::AudioFrameInfo::kMuted:
729 new_audio_frame_info = FrameInfo::kMuted;
730 break;
731 case MixerParticipant::AudioFrameInfo::kError:
732 new_audio_frame_info = FrameInfo::kError;
733 break;
734 }
aleloi6c278492016-10-20 14:24:39 -0700735 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700736}
737
kwiberg55b97fe2016-01-28 05:22:45 -0800738int32_t Channel::NeededFrequency(int32_t id) const {
739 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
740 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000741
kwiberg55b97fe2016-01-28 05:22:45 -0800742 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000743
kwiberg55b97fe2016-01-28 05:22:45 -0800744 // Determine highest needed receive frequency
745 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000746
kwiberg55b97fe2016-01-28 05:22:45 -0800747 // Return the bigger of playout and receive frequency in the ACM.
748 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
749 highestNeeded = audio_coding_->PlayoutFrequency();
750 } else {
751 highestNeeded = receiveFrequency;
752 }
753
754 // Special case, if we're playing a file on the playout side
755 // we take that frequency into consideration as well
756 // This is not needed on sending side, since the codec will
757 // limit the spectrum anyway.
758 if (channel_state_.Get().output_file_playing) {
759 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700760 if (output_file_player_) {
761 if (output_file_player_->Frequency() > highestNeeded) {
762 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800763 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000764 }
kwiberg55b97fe2016-01-28 05:22:45 -0800765 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000766
kwiberg55b97fe2016-01-28 05:22:45 -0800767 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000768}
769
ossu5f7cfa52016-05-30 08:11:28 -0700770int32_t Channel::CreateChannel(
771 Channel*& channel,
772 int32_t channelId,
773 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700774 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800775 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
776 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
777 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000778
solenberg88499ec2016-09-07 07:34:41 -0700779 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800780 if (channel == NULL) {
781 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
782 "Channel::CreateChannel() unable to allocate memory for"
783 " channel");
784 return -1;
785 }
786 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000787}
788
kwiberg55b97fe2016-01-28 05:22:45 -0800789void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
790 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
791 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
792 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000793
kwiberg55b97fe2016-01-28 05:22:45 -0800794 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000795}
796
kwiberg55b97fe2016-01-28 05:22:45 -0800797void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
798 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
799 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
800 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000801
kwiberg55b97fe2016-01-28 05:22:45 -0800802 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000803}
804
kwiberg55b97fe2016-01-28 05:22:45 -0800805void Channel::PlayFileEnded(int32_t id) {
806 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
807 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000808
kwiberg55b97fe2016-01-28 05:22:45 -0800809 if (id == _inputFilePlayerId) {
810 channel_state_.SetInputFilePlaying(false);
811 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
812 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000813 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800814 } else if (id == _outputFilePlayerId) {
815 channel_state_.SetOutputFilePlaying(false);
816 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
817 "Channel::PlayFileEnded() => output file player module is"
818 " shutdown");
819 }
820}
821
822void Channel::RecordFileEnded(int32_t id) {
823 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
824 "Channel::RecordFileEnded(id=%d)", id);
825
826 assert(id == _outputFileRecorderId);
827
828 rtc::CritScope cs(&_fileCritSect);
829
830 _outputFileRecording = false;
831 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
832 "Channel::RecordFileEnded() => output file recorder module is"
833 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000834}
835
pbos@webrtc.org92135212013-05-14 08:31:39 +0000836Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000837 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700838 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800839 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100840 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700841 event_log_proxy_(new RtcEventLogProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100842 rtp_header_parser_(RtpHeaderParser::Create()),
843 rtp_payload_registry_(
844 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
845 rtp_receive_statistics_(
846 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
847 rtp_receiver_(
848 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100849 this,
850 this,
851 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700852 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100853 _outputAudioLevel(),
854 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100855 // Avoid conflict with other channels by adding 1024 - 1026,
856 // won't use as much as 1024 channels.
857 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
858 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
859 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
860 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100861 _outputExternalMedia(false),
862 _inputExternalMediaCallbackPtr(NULL),
863 _outputExternalMediaCallbackPtr(NULL),
864 _timeStamp(0), // This is just an offset, RTP module will add it's own
865 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100866 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100867 playout_timestamp_rtp_(0),
868 playout_timestamp_rtcp_(0),
869 playout_delay_ms_(0),
870 _numberOfDiscardedPackets(0),
871 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100872 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
873 capture_start_rtp_time_stamp_(-1),
874 capture_start_ntp_time_ms_(-1),
875 _engineStatisticsPtr(NULL),
876 _outputMixerPtr(NULL),
877 _transmitMixerPtr(NULL),
878 _moduleProcessThreadPtr(NULL),
879 _audioDeviceModulePtr(NULL),
880 _voiceEngineObserverPtr(NULL),
881 _callbackCritSectPtr(NULL),
882 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100883 _sendFrameType(0),
884 _externalMixing(false),
885 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700886 input_mute_(false),
887 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100888 _panLeft(1.0f),
889 _panRight(1.0f),
890 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100891 _lastLocalTimeStamp(0),
892 _lastPayloadType(0),
893 _includeAudioLevelIndication(false),
894 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100895 restored_packet_in_use_(false),
896 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100897 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700898 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800899 feedback_observer_proxy_(new TransportFeedbackProxy()),
900 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700901 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200902 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
903 kMaxRetransmissionWindowMs)),
solenberg88499ec2016-09-07 07:34:41 -0700904 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800905 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
906 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700907 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800908 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700909 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800910 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200911
kwiberg55b97fe2016-01-28 05:22:45 -0800912 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000913
kwiberg55b97fe2016-01-28 05:22:45 -0800914 RtpRtcp::Configuration configuration;
915 configuration.audio = true;
916 configuration.outgoing_transport = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800917 configuration.receive_statistics = rtp_receive_statistics_.get();
918 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800919 if (pacing_enabled_) {
920 configuration.paced_sender = rtp_packet_sender_proxy_.get();
921 configuration.transport_sequence_number_allocator =
922 seq_num_allocator_proxy_.get();
923 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
924 }
ivoc14d5dbe2016-07-04 07:06:55 -0700925 configuration.event_log = &(*event_log_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200926 configuration.retransmission_rate_limiter =
927 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000928
kwiberg55b97fe2016-01-28 05:22:45 -0800929 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100930 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000931
kwiberg55b97fe2016-01-28 05:22:45 -0800932 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
933 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
934 statistics_proxy_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000935}
936
kwiberg55b97fe2016-01-28 05:22:45 -0800937Channel::~Channel() {
938 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
939 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
940 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000941
kwiberg55b97fe2016-01-28 05:22:45 -0800942 if (_outputExternalMedia) {
943 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
944 }
945 if (channel_state_.Get().input_external_media) {
946 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
947 }
948 StopSend();
949 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000950
kwiberg55b97fe2016-01-28 05:22:45 -0800951 {
952 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700953 if (input_file_player_) {
954 input_file_player_->RegisterModuleFileCallback(NULL);
955 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000956 }
kwiberg5a25d952016-08-17 07:31:12 -0700957 if (output_file_player_) {
958 output_file_player_->RegisterModuleFileCallback(NULL);
959 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800960 }
kwiberg5a25d952016-08-17 07:31:12 -0700961 if (output_file_recorder_) {
962 output_file_recorder_->RegisterModuleFileCallback(NULL);
963 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800964 }
965 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000966
kwiberg55b97fe2016-01-28 05:22:45 -0800967 // The order to safely shutdown modules in a channel is:
968 // 1. De-register callbacks in modules
969 // 2. De-register modules in process thread
970 // 3. Destroy modules
971 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
972 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
973 "~Channel() failed to de-register transport callback"
974 " (Audio coding module)");
975 }
976 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
977 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
978 "~Channel() failed to de-register VAD callback"
979 " (Audio coding module)");
980 }
981 // De-register modules in process thread
982 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000983
kwiberg55b97fe2016-01-28 05:22:45 -0800984 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000985}
986
kwiberg55b97fe2016-01-28 05:22:45 -0800987int32_t Channel::Init() {
988 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
989 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000990
kwiberg55b97fe2016-01-28 05:22:45 -0800991 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000992
kwiberg55b97fe2016-01-28 05:22:45 -0800993 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000994
kwiberg55b97fe2016-01-28 05:22:45 -0800995 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
996 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
997 "Channel::Init() must call SetEngineInformation() first");
998 return -1;
999 }
1000
1001 // --- Add modules to process thread (for periodic schedulation)
1002
1003 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
1004
1005 // --- ACM initialization
1006
1007 if (audio_coding_->InitializeReceiver() == -1) {
1008 _engineStatisticsPtr->SetLastError(
1009 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1010 "Channel::Init() unable to initialize the ACM - 1");
1011 return -1;
1012 }
1013
1014 // --- RTP/RTCP module initialization
1015
1016 // Ensure that RTCP is enabled by default for the created channel.
1017 // Note that, the module will keep generating RTCP until it is explicitly
1018 // disabled by the user.
1019 // After StopListen (when no sockets exists), RTCP packets will no longer
1020 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001021 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001022 // RTCP is enabled by default.
1023 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1024 // --- Register all permanent callbacks
1025 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1026 (audio_coding_->RegisterVADCallback(this) == -1);
1027
1028 if (fail) {
1029 _engineStatisticsPtr->SetLastError(
1030 VE_CANNOT_INIT_CHANNEL, kTraceError,
1031 "Channel::Init() callbacks not registered");
1032 return -1;
1033 }
1034
1035 // --- Register all supported codecs to the receiving side of the
1036 // RTP/RTCP module
1037
1038 CodecInst codec;
1039 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1040
1041 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1042 // Open up the RTP/RTCP receiver for all supported codecs
1043 if ((audio_coding_->Codec(idx, &codec) == -1) ||
1044 (rtp_receiver_->RegisterReceivePayload(
1045 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1046 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
1047 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1048 "Channel::Init() unable to register %s "
1049 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1050 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1051 codec.rate);
1052 } else {
1053 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1054 "Channel::Init() %s (%d/%d/%" PRIuS
1055 "/%d) has been "
1056 "added to the RTP/RTCP receiver",
1057 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1058 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001059 }
1060
kwiberg55b97fe2016-01-28 05:22:45 -08001061 // Ensure that PCMU is used as default codec on the sending side
1062 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1063 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001064 }
1065
kwiberg55b97fe2016-01-28 05:22:45 -08001066 // Register default PT for outband 'telephone-event'
1067 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001068 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
1069 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001070 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1071 "Channel::Init() failed to register outband "
1072 "'telephone-event' (%d/%d) correctly",
1073 codec.pltype, codec.plfreq);
1074 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001075 }
1076
kwiberg55b97fe2016-01-28 05:22:45 -08001077 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001078 if (!codec_manager_.RegisterEncoder(codec) ||
1079 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
1080 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec) ||
1081 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001082 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1083 "Channel::Init() failed to register CN (%d/%d) "
1084 "correctly - 1",
1085 codec.pltype, codec.plfreq);
1086 }
1087 }
kwiberg55b97fe2016-01-28 05:22:45 -08001088 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001089
kwiberg55b97fe2016-01-28 05:22:45 -08001090 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001091}
1092
kwiberg55b97fe2016-01-28 05:22:45 -08001093int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1094 OutputMixer& outputMixer,
1095 voe::TransmitMixer& transmitMixer,
1096 ProcessThread& moduleProcessThread,
1097 AudioDeviceModule& audioDeviceModule,
1098 VoiceEngineObserver* voiceEngineObserver,
1099 rtc::CriticalSection* callbackCritSect) {
1100 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1101 "Channel::SetEngineInformation()");
1102 _engineStatisticsPtr = &engineStatistics;
1103 _outputMixerPtr = &outputMixer;
1104 _transmitMixerPtr = &transmitMixer,
1105 _moduleProcessThreadPtr = &moduleProcessThread;
1106 _audioDeviceModulePtr = &audioDeviceModule;
1107 _voiceEngineObserverPtr = voiceEngineObserver;
1108 _callbackCritSectPtr = callbackCritSect;
1109 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001110}
1111
kwiberg55b97fe2016-01-28 05:22:45 -08001112int32_t Channel::UpdateLocalTimeStamp() {
1113 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1114 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001115}
1116
kwibergb7f89d62016-02-17 10:04:18 -08001117void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001118 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001119 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001120}
1121
ossu29b1a8d2016-06-13 07:34:51 -07001122const rtc::scoped_refptr<AudioDecoderFactory>&
1123Channel::GetAudioDecoderFactory() const {
1124 return decoder_factory_;
1125}
1126
kwiberg55b97fe2016-01-28 05:22:45 -08001127int32_t Channel::StartPlayout() {
1128 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1129 "Channel::StartPlayout()");
1130 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001131 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001132 }
1133
1134 if (!_externalMixing) {
1135 // Add participant as candidates for mixing.
1136 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1137 _engineStatisticsPtr->SetLastError(
1138 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1139 "StartPlayout() failed to add participant to mixer");
1140 return -1;
1141 }
1142 }
1143
1144 channel_state_.SetPlaying(true);
1145 if (RegisterFilePlayingToMixer() != 0)
1146 return -1;
1147
1148 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001149}
1150
kwiberg55b97fe2016-01-28 05:22:45 -08001151int32_t Channel::StopPlayout() {
1152 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1153 "Channel::StopPlayout()");
1154 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001155 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001156 }
1157
1158 if (!_externalMixing) {
1159 // Remove participant as candidates for mixing
1160 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1161 _engineStatisticsPtr->SetLastError(
1162 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1163 "StopPlayout() failed to remove participant from mixer");
1164 return -1;
1165 }
1166 }
1167
1168 channel_state_.SetPlaying(false);
1169 _outputAudioLevel.Clear();
1170
1171 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001172}
1173
kwiberg55b97fe2016-01-28 05:22:45 -08001174int32_t Channel::StartSend() {
1175 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1176 "Channel::StartSend()");
1177 // Resume the previous sequence number which was reset by StopSend().
1178 // This needs to be done before |sending| is set to true.
1179 if (send_sequence_number_)
1180 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001181
kwiberg55b97fe2016-01-28 05:22:45 -08001182 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001183 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001184 }
1185 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001186
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001187 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001188 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1189 _engineStatisticsPtr->SetLastError(
1190 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1191 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001192 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001193 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001194 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001195 return -1;
1196 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001197
kwiberg55b97fe2016-01-28 05:22:45 -08001198 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001199}
1200
kwiberg55b97fe2016-01-28 05:22:45 -08001201int32_t Channel::StopSend() {
1202 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1203 "Channel::StopSend()");
1204 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001205 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001206 }
1207 channel_state_.SetSending(false);
1208
1209 // Store the sequence number to be able to pick up the same sequence for
1210 // the next StartSend(). This is needed for restarting device, otherwise
1211 // it might cause libSRTP to complain about packets being replayed.
1212 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1213 // CL is landed. See issue
1214 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1215 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1216
1217 // Reset sending SSRC and sequence number and triggers direct transmission
1218 // of RTCP BYE
1219 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1220 _engineStatisticsPtr->SetLastError(
1221 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1222 "StartSend() RTP/RTCP failed to stop sending");
1223 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001224 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001225
1226 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001227}
1228
kwiberg55b97fe2016-01-28 05:22:45 -08001229int32_t Channel::StartReceiving() {
1230 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1231 "Channel::StartReceiving()");
1232 if (channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001233 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001234 }
1235 channel_state_.SetReceiving(true);
1236 _numberOfDiscardedPackets = 0;
1237 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001238}
1239
kwiberg55b97fe2016-01-28 05:22:45 -08001240int32_t Channel::StopReceiving() {
1241 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1242 "Channel::StopReceiving()");
1243 if (!channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001244 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001245 }
1246
1247 channel_state_.SetReceiving(false);
1248 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001249}
1250
kwiberg55b97fe2016-01-28 05:22:45 -08001251int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1252 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1253 "Channel::RegisterVoiceEngineObserver()");
1254 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001255
kwiberg55b97fe2016-01-28 05:22:45 -08001256 if (_voiceEngineObserverPtr) {
1257 _engineStatisticsPtr->SetLastError(
1258 VE_INVALID_OPERATION, kTraceError,
1259 "RegisterVoiceEngineObserver() observer already enabled");
1260 return -1;
1261 }
1262 _voiceEngineObserverPtr = &observer;
1263 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001264}
1265
kwiberg55b97fe2016-01-28 05:22:45 -08001266int32_t Channel::DeRegisterVoiceEngineObserver() {
1267 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1268 "Channel::DeRegisterVoiceEngineObserver()");
1269 rtc::CritScope cs(&_callbackCritSect);
1270
1271 if (!_voiceEngineObserverPtr) {
1272 _engineStatisticsPtr->SetLastError(
1273 VE_INVALID_OPERATION, kTraceWarning,
1274 "DeRegisterVoiceEngineObserver() observer already disabled");
1275 return 0;
1276 }
1277 _voiceEngineObserverPtr = NULL;
1278 return 0;
1279}
1280
1281int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001282 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001283 if (send_codec) {
1284 codec = *send_codec;
1285 return 0;
1286 }
1287 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001288}
1289
kwiberg55b97fe2016-01-28 05:22:45 -08001290int32_t Channel::GetRecCodec(CodecInst& codec) {
1291 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001292}
1293
kwiberg55b97fe2016-01-28 05:22:45 -08001294int32_t Channel::SetSendCodec(const CodecInst& codec) {
1295 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1296 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001297
kwibergc8d071e2016-04-06 12:22:38 -07001298 if (!codec_manager_.RegisterEncoder(codec) ||
1299 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001300 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1301 "SetSendCodec() failed to register codec to ACM");
1302 return -1;
1303 }
1304
1305 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1306 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1307 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1308 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1309 "SetSendCodec() failed to register codec to"
1310 " RTP/RTCP module");
1311 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001312 }
kwiberg55b97fe2016-01-28 05:22:45 -08001313 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001314
kwiberg55b97fe2016-01-28 05:22:45 -08001315 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) {
1316 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1317 "SetSendCodec() failed to set audio packet size");
1318 return -1;
1319 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001320
kwiberg55b97fe2016-01-28 05:22:45 -08001321 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001322}
1323
Ivo Creusenadf89b72015-04-29 16:03:33 +02001324void Channel::SetBitRate(int bitrate_bps) {
1325 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1326 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001327 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1328 if (*encoder)
1329 (*encoder)->OnReceivedTargetAudioBitrate(bitrate_bps);
1330 });
Erik Språng737336d2016-07-29 12:59:36 +02001331 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001332}
1333
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001334void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001335 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1336 if (*encoder)
1337 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1338 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001339}
1340
kwiberg55b97fe2016-01-28 05:22:45 -08001341int32_t Channel::SetVADStatus(bool enableVAD,
1342 ACMVADMode mode,
1343 bool disableDTX) {
1344 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1345 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001346 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1347 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1348 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001349 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1350 kTraceError,
1351 "SetVADStatus() failed to set VAD");
1352 return -1;
1353 }
1354 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001355}
1356
kwiberg55b97fe2016-01-28 05:22:45 -08001357int32_t Channel::GetVADStatus(bool& enabledVAD,
1358 ACMVADMode& mode,
1359 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001360 const auto* params = codec_manager_.GetStackParams();
1361 enabledVAD = params->use_cng;
1362 mode = params->vad_mode;
1363 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001364 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001365}
1366
kwiberg55b97fe2016-01-28 05:22:45 -08001367int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1368 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1369 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001370
kwiberg55b97fe2016-01-28 05:22:45 -08001371 if (channel_state_.Get().playing) {
1372 _engineStatisticsPtr->SetLastError(
1373 VE_ALREADY_PLAYING, kTraceError,
1374 "SetRecPayloadType() unable to set PT while playing");
1375 return -1;
1376 }
1377 if (channel_state_.Get().receiving) {
1378 _engineStatisticsPtr->SetLastError(
1379 VE_ALREADY_LISTENING, kTraceError,
1380 "SetRecPayloadType() unable to set PT while listening");
1381 return -1;
1382 }
1383
1384 if (codec.pltype == -1) {
1385 // De-register the selected codec (RTP/RTCP module and ACM)
1386
1387 int8_t pltype(-1);
1388 CodecInst rxCodec = codec;
1389
1390 // Get payload type for the given codec
1391 rtp_payload_registry_->ReceivePayloadType(
1392 rxCodec.plname, rxCodec.plfreq, rxCodec.channels,
1393 (rxCodec.rate < 0) ? 0 : rxCodec.rate, &pltype);
1394 rxCodec.pltype = pltype;
1395
1396 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1397 _engineStatisticsPtr->SetLastError(
1398 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1399 "SetRecPayloadType() RTP/RTCP-module deregistration "
1400 "failed");
1401 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001402 }
kwiberg55b97fe2016-01-28 05:22:45 -08001403 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1404 _engineStatisticsPtr->SetLastError(
1405 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1406 "SetRecPayloadType() ACM deregistration failed - 1");
1407 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001408 }
kwiberg55b97fe2016-01-28 05:22:45 -08001409 return 0;
1410 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001411
kwiberg55b97fe2016-01-28 05:22:45 -08001412 if (rtp_receiver_->RegisterReceivePayload(
1413 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1414 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1415 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001416 // TODO(kwiberg): Retrying is probably not necessary, since
1417 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001418 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001419 if (rtp_receiver_->RegisterReceivePayload(
kwiberg55b97fe2016-01-28 05:22:45 -08001420 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1421 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1422 _engineStatisticsPtr->SetLastError(
1423 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1424 "SetRecPayloadType() RTP/RTCP-module registration failed");
1425 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001426 }
kwiberg55b97fe2016-01-28 05:22:45 -08001427 }
kwibergc8d071e2016-04-06 12:22:38 -07001428 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001429 audio_coding_->UnregisterReceiveCodec(codec.pltype);
kwibergc8d071e2016-04-06 12:22:38 -07001430 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001431 _engineStatisticsPtr->SetLastError(
1432 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1433 "SetRecPayloadType() ACM registration failed - 1");
1434 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001435 }
kwiberg55b97fe2016-01-28 05:22:45 -08001436 }
1437 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001438}
1439
kwiberg55b97fe2016-01-28 05:22:45 -08001440int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1441 int8_t payloadType(-1);
1442 if (rtp_payload_registry_->ReceivePayloadType(
1443 codec.plname, codec.plfreq, codec.channels,
1444 (codec.rate < 0) ? 0 : codec.rate, &payloadType) != 0) {
1445 _engineStatisticsPtr->SetLastError(
1446 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1447 "GetRecPayloadType() failed to retrieve RX payload type");
1448 return -1;
1449 }
1450 codec.pltype = payloadType;
1451 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001452}
1453
kwiberg55b97fe2016-01-28 05:22:45 -08001454int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1455 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1456 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001457
kwiberg55b97fe2016-01-28 05:22:45 -08001458 CodecInst codec;
1459 int32_t samplingFreqHz(-1);
1460 const size_t kMono = 1;
1461 if (frequency == kFreq32000Hz)
1462 samplingFreqHz = 32000;
1463 else if (frequency == kFreq16000Hz)
1464 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001465
kwiberg55b97fe2016-01-28 05:22:45 -08001466 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1467 _engineStatisticsPtr->SetLastError(
1468 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1469 "SetSendCNPayloadType() failed to retrieve default CN codec "
1470 "settings");
1471 return -1;
1472 }
1473
1474 // Modify the payload type (must be set to dynamic range)
1475 codec.pltype = type;
1476
kwibergc8d071e2016-04-06 12:22:38 -07001477 if (!codec_manager_.RegisterEncoder(codec) ||
1478 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001479 _engineStatisticsPtr->SetLastError(
1480 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1481 "SetSendCNPayloadType() failed to register CN to ACM");
1482 return -1;
1483 }
1484
1485 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1486 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1487 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1488 _engineStatisticsPtr->SetLastError(
1489 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1490 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1491 "module");
1492 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001493 }
kwiberg55b97fe2016-01-28 05:22:45 -08001494 }
1495 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001496}
1497
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001498int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001499 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001500 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001501
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001502 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001503 _engineStatisticsPtr->SetLastError(
1504 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001505 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001506 return -1;
1507 }
1508 return 0;
1509}
1510
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001511int Channel::SetOpusDtx(bool enable_dtx) {
1512 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1513 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001514 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001515 : audio_coding_->DisableOpusDtx();
1516 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001517 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1518 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001519 return -1;
1520 }
1521 return 0;
1522}
1523
ivoc85228d62016-07-27 04:53:47 -07001524int Channel::GetOpusDtx(bool* enabled) {
1525 int success = -1;
1526 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1527 if (encoder) {
1528 *enabled = encoder->GetDtx();
1529 success = 0;
1530 }
1531 });
1532 return success;
1533}
1534
minyue7e304322016-10-12 05:00:55 -07001535bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1536 bool success = false;
1537 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1538 if (*encoder) {
1539 success = (*encoder)->EnableAudioNetworkAdaptor(
1540 config_string, Clock::GetRealTimeClock());
1541 }
1542 });
1543 return success;
1544}
1545
1546void Channel::DisableAudioNetworkAdaptor() {
1547 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1548 if (*encoder)
1549 (*encoder)->DisableAudioNetworkAdaptor();
1550 });
1551}
1552
1553void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1554 int max_frame_length_ms) {
1555 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1556 if (*encoder) {
1557 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1558 max_frame_length_ms);
1559 }
1560 });
1561}
1562
mflodman3d7db262016-04-29 00:57:13 -07001563int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001564 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001565 "Channel::RegisterExternalTransport()");
1566
kwiberg55b97fe2016-01-28 05:22:45 -08001567 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001568 if (_externalTransport) {
1569 _engineStatisticsPtr->SetLastError(
1570 VE_INVALID_OPERATION, kTraceError,
1571 "RegisterExternalTransport() external transport already enabled");
1572 return -1;
1573 }
1574 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001575 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001576 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001577}
1578
kwiberg55b97fe2016-01-28 05:22:45 -08001579int32_t Channel::DeRegisterExternalTransport() {
1580 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1581 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001582
kwiberg55b97fe2016-01-28 05:22:45 -08001583 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001584 if (_transportPtr) {
1585 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1586 "DeRegisterExternalTransport() all transport is disabled");
1587 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001588 _engineStatisticsPtr->SetLastError(
1589 VE_INVALID_OPERATION, kTraceWarning,
1590 "DeRegisterExternalTransport() external transport already "
1591 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001592 }
1593 _externalTransport = false;
1594 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001595 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001596}
1597
mflodman3d7db262016-04-29 00:57:13 -07001598int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001599 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001600 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001601 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001602 "Channel::ReceivedRTPPacket()");
1603
1604 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001605 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001606
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001607 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001608 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1609 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1610 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001611 return -1;
1612 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001613 header.payload_type_frequency =
1614 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001615 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001616 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001617 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001618 rtp_receive_statistics_->IncomingPacket(
1619 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001620 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001621
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001622 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001623}
1624
1625bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001626 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001627 const RTPHeader& header,
1628 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001629 if (rtp_payload_registry_->IsRtx(header)) {
1630 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001631 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001632 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001633 assert(packet_length >= header.headerLength);
1634 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001635 PayloadUnion payload_specific;
1636 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001637 &payload_specific)) {
1638 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001639 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001640 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1641 payload_specific, in_order);
1642}
1643
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001644bool Channel::HandleRtxPacket(const uint8_t* packet,
1645 size_t packet_length,
1646 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001647 if (!rtp_payload_registry_->IsRtx(header))
1648 return false;
1649
1650 // Remove the RTX header and parse the original RTP header.
1651 if (packet_length < header.headerLength)
1652 return false;
1653 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1654 return false;
1655 if (restored_packet_in_use_) {
1656 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1657 "Multiple RTX headers detected, dropping packet");
1658 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001659 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001660 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001661 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1662 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001663 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1664 "Incoming RTX packet: invalid RTP header");
1665 return false;
1666 }
1667 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001668 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001669 restored_packet_in_use_ = false;
1670 return ret;
1671}
1672
1673bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1674 StreamStatistician* statistician =
1675 rtp_receive_statistics_->GetStatistician(header.ssrc);
1676 if (!statistician)
1677 return false;
1678 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001679}
1680
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001681bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1682 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001683 // Retransmissions are handled separately if RTX is enabled.
1684 if (rtp_payload_registry_->RtxEnabled())
1685 return false;
1686 StreamStatistician* statistician =
1687 rtp_receive_statistics_->GetStatistician(header.ssrc);
1688 if (!statistician)
1689 return false;
1690 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001691 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001692 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001693 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001694}
1695
mflodman3d7db262016-04-29 00:57:13 -07001696int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001697 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001698 "Channel::ReceivedRTCPPacket()");
1699 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001700 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001701
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001702 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001703 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001704 _engineStatisticsPtr->SetLastError(
1705 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1706 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1707 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001708
Minyue2013aec2015-05-13 14:14:42 +02001709 int64_t rtt = GetRTT(true);
1710 if (rtt == 0) {
1711 // Waiting for valid RTT.
1712 return 0;
1713 }
Erik Språng737336d2016-07-29 12:59:36 +02001714
1715 int64_t nack_window_ms = rtt;
1716 if (nack_window_ms < kMinRetransmissionWindowMs) {
1717 nack_window_ms = kMinRetransmissionWindowMs;
1718 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1719 nack_window_ms = kMaxRetransmissionWindowMs;
1720 }
1721 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1722
minyue7e304322016-10-12 05:00:55 -07001723 // Invoke audio encoders OnReceivedRtt().
1724 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1725 if (*encoder)
1726 (*encoder)->OnReceivedRtt(rtt);
1727 });
1728
Minyue2013aec2015-05-13 14:14:42 +02001729 uint32_t ntp_secs = 0;
1730 uint32_t ntp_frac = 0;
1731 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001732 if (0 !=
1733 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1734 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001735 // Waiting for RTCP.
1736 return 0;
1737 }
1738
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001739 {
tommi31fc21f2016-01-21 10:37:37 -08001740 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001741 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001742 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001743 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001744}
1745
niklase@google.com470e71d2011-07-07 08:21:25 +00001746int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001747 bool loop,
1748 FileFormats format,
1749 int startPosition,
1750 float volumeScaling,
1751 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001752 const CodecInst* codecInst) {
1753 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1754 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1755 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1756 "stopPosition=%d)",
1757 fileName, loop, format, volumeScaling, startPosition,
1758 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001759
kwiberg55b97fe2016-01-28 05:22:45 -08001760 if (channel_state_.Get().output_file_playing) {
1761 _engineStatisticsPtr->SetLastError(
1762 VE_ALREADY_PLAYING, kTraceError,
1763 "StartPlayingFileLocally() is already playing");
1764 return -1;
1765 }
1766
1767 {
1768 rtc::CritScope cs(&_fileCritSect);
1769
kwiberg5a25d952016-08-17 07:31:12 -07001770 if (output_file_player_) {
1771 output_file_player_->RegisterModuleFileCallback(NULL);
1772 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001773 }
1774
kwiberg5b356f42016-09-08 04:32:33 -07001775 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001776 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001777
kwiberg5a25d952016-08-17 07:31:12 -07001778 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001779 _engineStatisticsPtr->SetLastError(
1780 VE_INVALID_ARGUMENT, kTraceError,
1781 "StartPlayingFileLocally() filePlayer format is not correct");
1782 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001783 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001784
kwiberg55b97fe2016-01-28 05:22:45 -08001785 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001786
kwiberg5a25d952016-08-17 07:31:12 -07001787 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001788 fileName, loop, startPosition, volumeScaling, notificationTime,
1789 stopPosition, (const CodecInst*)codecInst) != 0) {
1790 _engineStatisticsPtr->SetLastError(
1791 VE_BAD_FILE, kTraceError,
1792 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001793 output_file_player_->StopPlayingFile();
1794 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001795 return -1;
1796 }
kwiberg5a25d952016-08-17 07:31:12 -07001797 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001798 channel_state_.SetOutputFilePlaying(true);
1799 }
1800
1801 if (RegisterFilePlayingToMixer() != 0)
1802 return -1;
1803
1804 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001805}
1806
1807int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001808 FileFormats format,
1809 int startPosition,
1810 float volumeScaling,
1811 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001812 const CodecInst* codecInst) {
1813 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1814 "Channel::StartPlayingFileLocally(format=%d,"
1815 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1816 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001817
kwiberg55b97fe2016-01-28 05:22:45 -08001818 if (stream == NULL) {
1819 _engineStatisticsPtr->SetLastError(
1820 VE_BAD_FILE, kTraceError,
1821 "StartPlayingFileLocally() NULL as input stream");
1822 return -1;
1823 }
1824
1825 if (channel_state_.Get().output_file_playing) {
1826 _engineStatisticsPtr->SetLastError(
1827 VE_ALREADY_PLAYING, kTraceError,
1828 "StartPlayingFileLocally() is already playing");
1829 return -1;
1830 }
1831
1832 {
1833 rtc::CritScope cs(&_fileCritSect);
1834
1835 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001836 if (output_file_player_) {
1837 output_file_player_->RegisterModuleFileCallback(NULL);
1838 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001839 }
1840
kwiberg55b97fe2016-01-28 05:22:45 -08001841 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001842 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001843 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001844
kwiberg5a25d952016-08-17 07:31:12 -07001845 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001846 _engineStatisticsPtr->SetLastError(
1847 VE_INVALID_ARGUMENT, kTraceError,
1848 "StartPlayingFileLocally() filePlayer format isnot correct");
1849 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001850 }
1851
kwiberg55b97fe2016-01-28 05:22:45 -08001852 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001853
kwiberg4ec01d92016-08-22 08:43:54 -07001854 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001855 volumeScaling, notificationTime,
1856 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001857 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1858 "StartPlayingFile() failed to "
1859 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001860 output_file_player_->StopPlayingFile();
1861 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001862 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001863 }
kwiberg5a25d952016-08-17 07:31:12 -07001864 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001865 channel_state_.SetOutputFilePlaying(true);
1866 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001867
kwiberg55b97fe2016-01-28 05:22:45 -08001868 if (RegisterFilePlayingToMixer() != 0)
1869 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001870
kwiberg55b97fe2016-01-28 05:22:45 -08001871 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001872}
1873
kwiberg55b97fe2016-01-28 05:22:45 -08001874int Channel::StopPlayingFileLocally() {
1875 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1876 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001877
kwiberg55b97fe2016-01-28 05:22:45 -08001878 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001879 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001880 }
1881
1882 {
1883 rtc::CritScope cs(&_fileCritSect);
1884
kwiberg5a25d952016-08-17 07:31:12 -07001885 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001886 _engineStatisticsPtr->SetLastError(
1887 VE_STOP_RECORDING_FAILED, kTraceError,
1888 "StopPlayingFile() could not stop playing");
1889 return -1;
1890 }
kwiberg5a25d952016-08-17 07:31:12 -07001891 output_file_player_->RegisterModuleFileCallback(NULL);
1892 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001893 channel_state_.SetOutputFilePlaying(false);
1894 }
1895 // _fileCritSect cannot be taken while calling
1896 // SetAnonymousMixibilityStatus. Refer to comments in
1897 // StartPlayingFileLocally(const char* ...) for more details.
1898 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1899 _engineStatisticsPtr->SetLastError(
1900 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1901 "StopPlayingFile() failed to stop participant from playing as"
1902 "file in the mixer");
1903 return -1;
1904 }
1905
1906 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001907}
1908
kwiberg55b97fe2016-01-28 05:22:45 -08001909int Channel::IsPlayingFileLocally() const {
1910 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001911}
1912
kwiberg55b97fe2016-01-28 05:22:45 -08001913int Channel::RegisterFilePlayingToMixer() {
1914 // Return success for not registering for file playing to mixer if:
1915 // 1. playing file before playout is started on that channel.
1916 // 2. starting playout without file playing on that channel.
1917 if (!channel_state_.Get().playing ||
1918 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001919 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001920 }
1921
1922 // |_fileCritSect| cannot be taken while calling
1923 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1924 // frames can be pulled by the mixer. Since the frames are generated from
1925 // the file, _fileCritSect will be taken. This would result in a deadlock.
1926 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1927 channel_state_.SetOutputFilePlaying(false);
1928 rtc::CritScope cs(&_fileCritSect);
1929 _engineStatisticsPtr->SetLastError(
1930 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1931 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001932 output_file_player_->StopPlayingFile();
1933 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001934 return -1;
1935 }
1936
1937 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001938}
1939
niklase@google.com470e71d2011-07-07 08:21:25 +00001940int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001941 bool loop,
1942 FileFormats format,
1943 int startPosition,
1944 float volumeScaling,
1945 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001946 const CodecInst* codecInst) {
1947 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1948 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1949 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1950 "stopPosition=%d)",
1951 fileName, loop, format, volumeScaling, startPosition,
1952 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001953
kwiberg55b97fe2016-01-28 05:22:45 -08001954 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001955
kwiberg55b97fe2016-01-28 05:22:45 -08001956 if (channel_state_.Get().input_file_playing) {
1957 _engineStatisticsPtr->SetLastError(
1958 VE_ALREADY_PLAYING, kTraceWarning,
1959 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001960 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001961 }
1962
1963 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001964 if (input_file_player_) {
1965 input_file_player_->RegisterModuleFileCallback(NULL);
1966 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001967 }
1968
1969 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001970 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001971 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001972
kwiberg5a25d952016-08-17 07:31:12 -07001973 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001974 _engineStatisticsPtr->SetLastError(
1975 VE_INVALID_ARGUMENT, kTraceError,
1976 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1977 return -1;
1978 }
1979
1980 const uint32_t notificationTime(0);
1981
kwiberg5a25d952016-08-17 07:31:12 -07001982 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001983 fileName, loop, startPosition, volumeScaling, notificationTime,
1984 stopPosition, (const CodecInst*)codecInst) != 0) {
1985 _engineStatisticsPtr->SetLastError(
1986 VE_BAD_FILE, kTraceError,
1987 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001988 input_file_player_->StopPlayingFile();
1989 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001990 return -1;
1991 }
kwiberg5a25d952016-08-17 07:31:12 -07001992 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001993 channel_state_.SetInputFilePlaying(true);
1994
1995 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001996}
1997
1998int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001999 FileFormats format,
2000 int startPosition,
2001 float volumeScaling,
2002 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002003 const CodecInst* codecInst) {
2004 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2005 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2006 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2007 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002008
kwiberg55b97fe2016-01-28 05:22:45 -08002009 if (stream == NULL) {
2010 _engineStatisticsPtr->SetLastError(
2011 VE_BAD_FILE, kTraceError,
2012 "StartPlayingFileAsMicrophone NULL as input stream");
2013 return -1;
2014 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002015
kwiberg55b97fe2016-01-28 05:22:45 -08002016 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002017
kwiberg55b97fe2016-01-28 05:22:45 -08002018 if (channel_state_.Get().input_file_playing) {
2019 _engineStatisticsPtr->SetLastError(
2020 VE_ALREADY_PLAYING, kTraceWarning,
2021 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002022 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002023 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002024
kwiberg55b97fe2016-01-28 05:22:45 -08002025 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002026 if (input_file_player_) {
2027 input_file_player_->RegisterModuleFileCallback(NULL);
2028 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002029 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002030
kwiberg55b97fe2016-01-28 05:22:45 -08002031 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002032 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002033 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002034
kwiberg5a25d952016-08-17 07:31:12 -07002035 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002036 _engineStatisticsPtr->SetLastError(
2037 VE_INVALID_ARGUMENT, kTraceError,
2038 "StartPlayingInputFile() filePlayer format isnot correct");
2039 return -1;
2040 }
2041
2042 const uint32_t notificationTime(0);
2043
kwiberg4ec01d92016-08-22 08:43:54 -07002044 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2045 notificationTime, stopPosition,
2046 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002047 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2048 "StartPlayingFile() failed to start "
2049 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002050 input_file_player_->StopPlayingFile();
2051 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002052 return -1;
2053 }
2054
kwiberg5a25d952016-08-17 07:31:12 -07002055 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002056 channel_state_.SetInputFilePlaying(true);
2057
2058 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002059}
2060
kwiberg55b97fe2016-01-28 05:22:45 -08002061int Channel::StopPlayingFileAsMicrophone() {
2062 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2063 "Channel::StopPlayingFileAsMicrophone()");
2064
2065 rtc::CritScope cs(&_fileCritSect);
2066
2067 if (!channel_state_.Get().input_file_playing) {
2068 return 0;
2069 }
2070
kwiberg5a25d952016-08-17 07:31:12 -07002071 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002072 _engineStatisticsPtr->SetLastError(
2073 VE_STOP_RECORDING_FAILED, kTraceError,
2074 "StopPlayingFile() could not stop playing");
2075 return -1;
2076 }
kwiberg5a25d952016-08-17 07:31:12 -07002077 input_file_player_->RegisterModuleFileCallback(NULL);
2078 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002079 channel_state_.SetInputFilePlaying(false);
2080
2081 return 0;
2082}
2083
2084int Channel::IsPlayingFileAsMicrophone() const {
2085 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002086}
2087
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002088int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002089 const CodecInst* codecInst) {
2090 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2091 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002092
kwiberg55b97fe2016-01-28 05:22:45 -08002093 if (_outputFileRecording) {
2094 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2095 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002096 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002097 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002098
kwiberg55b97fe2016-01-28 05:22:45 -08002099 FileFormats format;
2100 const uint32_t notificationTime(0); // Not supported in VoE
2101 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002102
kwiberg55b97fe2016-01-28 05:22:45 -08002103 if ((codecInst != NULL) &&
2104 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2105 _engineStatisticsPtr->SetLastError(
2106 VE_BAD_ARGUMENT, kTraceError,
2107 "StartRecordingPlayout() invalid compression");
2108 return (-1);
2109 }
2110 if (codecInst == NULL) {
2111 format = kFileFormatPcm16kHzFile;
2112 codecInst = &dummyCodec;
2113 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2114 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2115 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2116 format = kFileFormatWavFile;
2117 } else {
2118 format = kFileFormatCompressedFile;
2119 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002120
kwiberg55b97fe2016-01-28 05:22:45 -08002121 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002122
kwiberg55b97fe2016-01-28 05:22:45 -08002123 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002124 if (output_file_recorder_) {
2125 output_file_recorder_->RegisterModuleFileCallback(NULL);
2126 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002127 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002128
kwiberg5a25d952016-08-17 07:31:12 -07002129 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002130 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002131 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002132 _engineStatisticsPtr->SetLastError(
2133 VE_INVALID_ARGUMENT, kTraceError,
2134 "StartRecordingPlayout() fileRecorder format isnot correct");
2135 return -1;
2136 }
2137
kwiberg5a25d952016-08-17 07:31:12 -07002138 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002139 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2140 _engineStatisticsPtr->SetLastError(
2141 VE_BAD_FILE, kTraceError,
2142 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002143 output_file_recorder_->StopRecording();
2144 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002145 return -1;
2146 }
kwiberg5a25d952016-08-17 07:31:12 -07002147 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002148 _outputFileRecording = true;
2149
2150 return 0;
2151}
2152
2153int Channel::StartRecordingPlayout(OutStream* stream,
2154 const CodecInst* codecInst) {
2155 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2156 "Channel::StartRecordingPlayout()");
2157
2158 if (_outputFileRecording) {
2159 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2160 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002161 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002162 }
2163
2164 FileFormats format;
2165 const uint32_t notificationTime(0); // Not supported in VoE
2166 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2167
2168 if (codecInst != NULL && codecInst->channels != 1) {
2169 _engineStatisticsPtr->SetLastError(
2170 VE_BAD_ARGUMENT, kTraceError,
2171 "StartRecordingPlayout() invalid compression");
2172 return (-1);
2173 }
2174 if (codecInst == NULL) {
2175 format = kFileFormatPcm16kHzFile;
2176 codecInst = &dummyCodec;
2177 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2178 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2179 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2180 format = kFileFormatWavFile;
2181 } else {
2182 format = kFileFormatCompressedFile;
2183 }
2184
2185 rtc::CritScope cs(&_fileCritSect);
2186
2187 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002188 if (output_file_recorder_) {
2189 output_file_recorder_->RegisterModuleFileCallback(NULL);
2190 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002191 }
2192
kwiberg5a25d952016-08-17 07:31:12 -07002193 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002194 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002195 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002196 _engineStatisticsPtr->SetLastError(
2197 VE_INVALID_ARGUMENT, kTraceError,
2198 "StartRecordingPlayout() fileRecorder format isnot correct");
2199 return -1;
2200 }
2201
kwiberg4ec01d92016-08-22 08:43:54 -07002202 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002203 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002204 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2205 "StartRecordingPlayout() failed to "
2206 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002207 output_file_recorder_->StopRecording();
2208 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002209 return -1;
2210 }
2211
kwiberg5a25d952016-08-17 07:31:12 -07002212 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002213 _outputFileRecording = true;
2214
2215 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002216}
2217
kwiberg55b97fe2016-01-28 05:22:45 -08002218int Channel::StopRecordingPlayout() {
2219 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2220 "Channel::StopRecordingPlayout()");
2221
2222 if (!_outputFileRecording) {
2223 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2224 "StopRecordingPlayout() isnot recording");
2225 return -1;
2226 }
2227
2228 rtc::CritScope cs(&_fileCritSect);
2229
kwiberg5a25d952016-08-17 07:31:12 -07002230 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002231 _engineStatisticsPtr->SetLastError(
2232 VE_STOP_RECORDING_FAILED, kTraceError,
2233 "StopRecording() could not stop recording");
2234 return (-1);
2235 }
kwiberg5a25d952016-08-17 07:31:12 -07002236 output_file_recorder_->RegisterModuleFileCallback(NULL);
2237 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002238 _outputFileRecording = false;
2239
2240 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002241}
2242
kwiberg55b97fe2016-01-28 05:22:45 -08002243void Channel::SetMixWithMicStatus(bool mix) {
2244 rtc::CritScope cs(&_fileCritSect);
2245 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002246}
2247
kwiberg55b97fe2016-01-28 05:22:45 -08002248int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2249 int8_t currentLevel = _outputAudioLevel.Level();
2250 level = static_cast<int32_t>(currentLevel);
2251 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002252}
2253
kwiberg55b97fe2016-01-28 05:22:45 -08002254int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2255 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2256 level = static_cast<int32_t>(currentLevel);
2257 return 0;
2258}
2259
solenberg1c2af8e2016-03-24 10:36:00 -07002260int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002261 rtc::CritScope cs(&volume_settings_critsect_);
2262 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002263 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002264 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002265 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002266}
2267
solenberg1c2af8e2016-03-24 10:36:00 -07002268bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002269 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002270 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002271}
2272
kwiberg55b97fe2016-01-28 05:22:45 -08002273int Channel::SetOutputVolumePan(float left, float right) {
2274 rtc::CritScope cs(&volume_settings_critsect_);
2275 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002276 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002277 _panLeft = left;
2278 _panRight = right;
2279 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002280}
2281
kwiberg55b97fe2016-01-28 05:22:45 -08002282int Channel::GetOutputVolumePan(float& left, float& right) const {
2283 rtc::CritScope cs(&volume_settings_critsect_);
2284 left = _panLeft;
2285 right = _panRight;
2286 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002287}
2288
kwiberg55b97fe2016-01-28 05:22:45 -08002289int Channel::SetChannelOutputVolumeScaling(float scaling) {
2290 rtc::CritScope cs(&volume_settings_critsect_);
2291 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002292 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002293 _outputGain = scaling;
2294 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002295}
2296
kwiberg55b97fe2016-01-28 05:22:45 -08002297int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2298 rtc::CritScope cs(&volume_settings_critsect_);
2299 scaling = _outputGain;
2300 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002301}
2302
solenberg8842c3e2016-03-11 03:06:41 -08002303int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002304 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002305 "Channel::SendTelephoneEventOutband(...)");
2306 RTC_DCHECK_LE(0, event);
2307 RTC_DCHECK_GE(255, event);
2308 RTC_DCHECK_LE(0, duration_ms);
2309 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002310 if (!Sending()) {
2311 return -1;
2312 }
solenberg8842c3e2016-03-11 03:06:41 -08002313 if (_rtpRtcpModule->SendTelephoneEventOutband(
2314 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002315 _engineStatisticsPtr->SetLastError(
2316 VE_SEND_DTMF_FAILED, kTraceWarning,
2317 "SendTelephoneEventOutband() failed to send event");
2318 return -1;
2319 }
2320 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002321}
2322
solenberg31642aa2016-03-14 08:00:37 -07002323int Channel::SetSendTelephoneEventPayloadType(int payload_type) {
kwiberg55b97fe2016-01-28 05:22:45 -08002324 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002325 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002326 RTC_DCHECK_LE(0, payload_type);
2327 RTC_DCHECK_GE(127, payload_type);
2328 CodecInst codec = {0};
kwiberg55b97fe2016-01-28 05:22:45 -08002329 codec.plfreq = 8000;
solenberg31642aa2016-03-14 08:00:37 -07002330 codec.pltype = payload_type;
kwiberg55b97fe2016-01-28 05:22:45 -08002331 memcpy(codec.plname, "telephone-event", 16);
2332 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2333 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2334 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2335 _engineStatisticsPtr->SetLastError(
2336 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2337 "SetSendTelephoneEventPayloadType() failed to register send"
2338 "payload type");
2339 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002340 }
kwiberg55b97fe2016-01-28 05:22:45 -08002341 }
kwiberg55b97fe2016-01-28 05:22:45 -08002342 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002343}
2344
kwiberg55b97fe2016-01-28 05:22:45 -08002345int Channel::VoiceActivityIndicator(int& activity) {
2346 activity = _sendFrameType;
2347 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002348}
2349
kwiberg55b97fe2016-01-28 05:22:45 -08002350int Channel::SetLocalSSRC(unsigned int ssrc) {
2351 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2352 "Channel::SetLocalSSRC()");
2353 if (channel_state_.Get().sending) {
2354 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2355 "SetLocalSSRC() already sending");
2356 return -1;
2357 }
2358 _rtpRtcpModule->SetSSRC(ssrc);
2359 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002360}
2361
kwiberg55b97fe2016-01-28 05:22:45 -08002362int Channel::GetLocalSSRC(unsigned int& ssrc) {
2363 ssrc = _rtpRtcpModule->SSRC();
2364 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002365}
2366
kwiberg55b97fe2016-01-28 05:22:45 -08002367int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2368 ssrc = rtp_receiver_->SSRC();
2369 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002370}
2371
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002372int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002373 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002374 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002375}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002376
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002377int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2378 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002379 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2380 if (enable &&
2381 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2382 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002383 return -1;
2384 }
2385 return 0;
2386}
2387
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002388int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2389 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2390}
2391
2392int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2393 rtp_header_parser_->DeregisterRtpHeaderExtension(
2394 kRtpExtensionAbsoluteSendTime);
kwiberg55b97fe2016-01-28 05:22:45 -08002395 if (enable &&
2396 !rtp_header_parser_->RegisterRtpHeaderExtension(
2397 kRtpExtensionAbsoluteSendTime, id)) {
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002398 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002399 }
2400 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002401}
2402
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002403void Channel::EnableSendTransportSequenceNumber(int id) {
2404 int ret =
2405 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2406 RTC_DCHECK_EQ(0, ret);
2407}
2408
stefan3313ec92016-01-21 06:32:43 -08002409void Channel::EnableReceiveTransportSequenceNumber(int id) {
2410 rtp_header_parser_->DeregisterRtpHeaderExtension(
2411 kRtpExtensionTransportSequenceNumber);
2412 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2413 kRtpExtensionTransportSequenceNumber, id);
2414 RTC_DCHECK(ret);
2415}
2416
stefanbba9dec2016-02-01 04:39:55 -08002417void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002418 RtpPacketSender* rtp_packet_sender,
2419 TransportFeedbackObserver* transport_feedback_observer,
2420 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002421 RTC_DCHECK(rtp_packet_sender);
2422 RTC_DCHECK(transport_feedback_observer);
2423 RTC_DCHECK(packet_router && !packet_router_);
2424 feedback_observer_proxy_->SetTransportFeedbackObserver(
2425 transport_feedback_observer);
2426 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2427 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2428 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002429 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002430 packet_router_ = packet_router;
2431}
2432
stefanbba9dec2016-02-01 04:39:55 -08002433void Channel::RegisterReceiverCongestionControlObjects(
2434 PacketRouter* packet_router) {
2435 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002436 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002437 packet_router_ = packet_router;
2438}
2439
2440void Channel::ResetCongestionControlObjects() {
2441 RTC_DCHECK(packet_router_);
2442 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2443 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2444 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002445 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002446 packet_router_ = nullptr;
2447 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2448}
2449
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002450void Channel::SetRTCPStatus(bool enable) {
2451 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2452 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002453 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002454}
2455
kwiberg55b97fe2016-01-28 05:22:45 -08002456int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002457 RtcpMode method = _rtpRtcpModule->RTCP();
2458 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002459 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002460}
2461
kwiberg55b97fe2016-01-28 05:22:45 -08002462int Channel::SetRTCP_CNAME(const char cName[256]) {
2463 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2464 "Channel::SetRTCP_CNAME()");
2465 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2466 _engineStatisticsPtr->SetLastError(
2467 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2468 "SetRTCP_CNAME() failed to set RTCP CNAME");
2469 return -1;
2470 }
2471 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002472}
2473
kwiberg55b97fe2016-01-28 05:22:45 -08002474int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2475 if (cName == NULL) {
2476 _engineStatisticsPtr->SetLastError(
2477 VE_INVALID_ARGUMENT, kTraceError,
2478 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2479 return -1;
2480 }
2481 char cname[RTCP_CNAME_SIZE];
2482 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2483 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2484 _engineStatisticsPtr->SetLastError(
2485 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2486 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2487 return -1;
2488 }
2489 strcpy(cName, cname);
2490 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002491}
2492
kwiberg55b97fe2016-01-28 05:22:45 -08002493int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2494 unsigned int& NTPLow,
2495 unsigned int& timestamp,
2496 unsigned int& playoutTimestamp,
2497 unsigned int* jitter,
2498 unsigned short* fractionLost) {
2499 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002500
kwiberg55b97fe2016-01-28 05:22:45 -08002501 RTCPSenderInfo senderInfo;
2502 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2503 _engineStatisticsPtr->SetLastError(
2504 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2505 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2506 "side");
2507 return -1;
2508 }
2509
2510 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2511 // and octet count)
2512 NTPHigh = senderInfo.NTPseconds;
2513 NTPLow = senderInfo.NTPfraction;
2514 timestamp = senderInfo.RTPtimeStamp;
2515
2516 // --- Locally derived information
2517
2518 // This value is updated on each incoming RTCP packet (0 when no packet
2519 // has been received)
2520 playoutTimestamp = playout_timestamp_rtcp_;
2521
2522 if (NULL != jitter || NULL != fractionLost) {
2523 // Get all RTCP receiver report blocks that have been received on this
2524 // channel. If we receive RTP packets from a remote source we know the
2525 // remote SSRC and use the report block from him.
2526 // Otherwise use the first report block.
2527 std::vector<RTCPReportBlock> remote_stats;
2528 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2529 remote_stats.empty()) {
2530 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2531 "GetRemoteRTCPData() failed to measure statistics due"
2532 " to lack of received RTP and/or RTCP packets");
2533 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002534 }
2535
kwiberg55b97fe2016-01-28 05:22:45 -08002536 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2537 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2538 for (; it != remote_stats.end(); ++it) {
2539 if (it->remoteSSRC == remoteSSRC)
2540 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002541 }
kwiberg55b97fe2016-01-28 05:22:45 -08002542
2543 if (it == remote_stats.end()) {
2544 // If we have not received any RTCP packets from this SSRC it probably
2545 // means that we have not received any RTP packets.
2546 // Use the first received report block instead.
2547 it = remote_stats.begin();
2548 remoteSSRC = it->remoteSSRC;
2549 }
2550
2551 if (jitter) {
2552 *jitter = it->jitter;
2553 }
2554
2555 if (fractionLost) {
2556 *fractionLost = it->fractionLost;
2557 }
2558 }
2559 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002560}
2561
kwiberg55b97fe2016-01-28 05:22:45 -08002562int Channel::SendApplicationDefinedRTCPPacket(
2563 unsigned char subType,
2564 unsigned int name,
2565 const char* data,
2566 unsigned short dataLengthInBytes) {
2567 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2568 "Channel::SendApplicationDefinedRTCPPacket()");
2569 if (!channel_state_.Get().sending) {
2570 _engineStatisticsPtr->SetLastError(
2571 VE_NOT_SENDING, kTraceError,
2572 "SendApplicationDefinedRTCPPacket() not sending");
2573 return -1;
2574 }
2575 if (NULL == data) {
2576 _engineStatisticsPtr->SetLastError(
2577 VE_INVALID_ARGUMENT, kTraceError,
2578 "SendApplicationDefinedRTCPPacket() invalid data value");
2579 return -1;
2580 }
2581 if (dataLengthInBytes % 4 != 0) {
2582 _engineStatisticsPtr->SetLastError(
2583 VE_INVALID_ARGUMENT, kTraceError,
2584 "SendApplicationDefinedRTCPPacket() invalid length value");
2585 return -1;
2586 }
2587 RtcpMode status = _rtpRtcpModule->RTCP();
2588 if (status == RtcpMode::kOff) {
2589 _engineStatisticsPtr->SetLastError(
2590 VE_RTCP_ERROR, kTraceError,
2591 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2592 return -1;
2593 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002594
kwiberg55b97fe2016-01-28 05:22:45 -08002595 // Create and schedule the RTCP APP packet for transmission
2596 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2597 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2598 _engineStatisticsPtr->SetLastError(
2599 VE_SEND_ERROR, kTraceError,
2600 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2601 return -1;
2602 }
2603 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002604}
2605
kwiberg55b97fe2016-01-28 05:22:45 -08002606int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2607 unsigned int& maxJitterMs,
2608 unsigned int& discardedPackets) {
2609 // The jitter statistics is updated for each received RTP packet and is
2610 // based on received packets.
2611 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2612 // If RTCP is off, there is no timed thread in the RTCP module regularly
2613 // generating new stats, trigger the update manually here instead.
2614 StreamStatistician* statistician =
2615 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2616 if (statistician) {
2617 // Don't use returned statistics, use data from proxy instead so that
2618 // max jitter can be fetched atomically.
2619 RtcpStatistics s;
2620 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002621 }
kwiberg55b97fe2016-01-28 05:22:45 -08002622 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002623
kwiberg55b97fe2016-01-28 05:22:45 -08002624 ChannelStatistics stats = statistics_proxy_->GetStats();
2625 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2626 if (playoutFrequency > 0) {
2627 // Scale RTP statistics given the current playout frequency
2628 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2629 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2630 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002631
kwiberg55b97fe2016-01-28 05:22:45 -08002632 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002633
kwiberg55b97fe2016-01-28 05:22:45 -08002634 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002635}
2636
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002637int Channel::GetRemoteRTCPReportBlocks(
2638 std::vector<ReportBlock>* report_blocks) {
2639 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002640 _engineStatisticsPtr->SetLastError(
2641 VE_INVALID_ARGUMENT, kTraceError,
2642 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002643 return -1;
2644 }
2645
2646 // Get the report blocks from the latest received RTCP Sender or Receiver
2647 // Report. Each element in the vector contains the sender's SSRC and a
2648 // report block according to RFC 3550.
2649 std::vector<RTCPReportBlock> rtcp_report_blocks;
2650 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002651 return -1;
2652 }
2653
2654 if (rtcp_report_blocks.empty())
2655 return 0;
2656
2657 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2658 for (; it != rtcp_report_blocks.end(); ++it) {
2659 ReportBlock report_block;
2660 report_block.sender_SSRC = it->remoteSSRC;
2661 report_block.source_SSRC = it->sourceSSRC;
2662 report_block.fraction_lost = it->fractionLost;
2663 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2664 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2665 report_block.interarrival_jitter = it->jitter;
2666 report_block.last_SR_timestamp = it->lastSR;
2667 report_block.delay_since_last_SR = it->delaySinceLastSR;
2668 report_blocks->push_back(report_block);
2669 }
2670 return 0;
2671}
2672
kwiberg55b97fe2016-01-28 05:22:45 -08002673int Channel::GetRTPStatistics(CallStatistics& stats) {
2674 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002675
kwiberg55b97fe2016-01-28 05:22:45 -08002676 // The jitter statistics is updated for each received RTP packet and is
2677 // based on received packets.
2678 RtcpStatistics statistics;
2679 StreamStatistician* statistician =
2680 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002681 if (statistician) {
2682 statistician->GetStatistics(&statistics,
2683 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002684 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002685
kwiberg55b97fe2016-01-28 05:22:45 -08002686 stats.fractionLost = statistics.fraction_lost;
2687 stats.cumulativeLost = statistics.cumulative_lost;
2688 stats.extendedMax = statistics.extended_max_sequence_number;
2689 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002690
kwiberg55b97fe2016-01-28 05:22:45 -08002691 // --- RTT
2692 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002693
kwiberg55b97fe2016-01-28 05:22:45 -08002694 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002695
kwiberg55b97fe2016-01-28 05:22:45 -08002696 size_t bytesSent(0);
2697 uint32_t packetsSent(0);
2698 size_t bytesReceived(0);
2699 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002700
kwiberg55b97fe2016-01-28 05:22:45 -08002701 if (statistician) {
2702 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2703 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002704
kwiberg55b97fe2016-01-28 05:22:45 -08002705 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2706 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2707 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2708 " output will not be complete");
2709 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002710
kwiberg55b97fe2016-01-28 05:22:45 -08002711 stats.bytesSent = bytesSent;
2712 stats.packetsSent = packetsSent;
2713 stats.bytesReceived = bytesReceived;
2714 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002715
kwiberg55b97fe2016-01-28 05:22:45 -08002716 // --- Timestamps
2717 {
2718 rtc::CritScope lock(&ts_stats_lock_);
2719 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2720 }
2721 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002722}
2723
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002724int Channel::SetCodecFECStatus(bool enable) {
2725 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2726 "Channel::SetCodecFECStatus()");
2727
kwibergc8d071e2016-04-06 12:22:38 -07002728 if (!codec_manager_.SetCodecFEC(enable) ||
2729 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002730 _engineStatisticsPtr->SetLastError(
2731 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2732 "SetCodecFECStatus() failed to set FEC state");
2733 return -1;
2734 }
2735 return 0;
2736}
2737
2738bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002739 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002740}
2741
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002742void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2743 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002744 // If pacing is enabled we always store packets.
2745 if (!pacing_enabled_)
2746 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002747 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002748 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002749 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002750 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002751 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002752}
2753
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002754// Called when we are missing one or more packets.
2755int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002756 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2757}
2758
kwiberg55b97fe2016-01-28 05:22:45 -08002759uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2760 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2761 "Channel::Demultiplex()");
2762 _audioFrame.CopyFrom(audioFrame);
2763 _audioFrame.id_ = _channelId;
2764 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002765}
2766
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002767void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002768 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002769 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002770 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002771 CodecInst codec;
2772 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002773
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002774 // Never upsample or upmix the capture signal here. This should be done at the
2775 // end of the send chain.
2776 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2777 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2778 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2779 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002780}
2781
kwiberg55b97fe2016-01-28 05:22:45 -08002782uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2783 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2784 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002785
kwiberg55b97fe2016-01-28 05:22:45 -08002786 if (_audioFrame.samples_per_channel_ == 0) {
2787 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2788 "Channel::PrepareEncodeAndSend() invalid audio frame");
2789 return 0xFFFFFFFF;
2790 }
2791
2792 if (channel_state_.Get().input_file_playing) {
2793 MixOrReplaceAudioWithFile(mixingFrequency);
2794 }
2795
solenberg1c2af8e2016-03-24 10:36:00 -07002796 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2797 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002798
2799 if (channel_state_.Get().input_external_media) {
2800 rtc::CritScope cs(&_callbackCritSect);
2801 const bool isStereo = (_audioFrame.num_channels_ == 2);
2802 if (_inputExternalMediaCallbackPtr) {
2803 _inputExternalMediaCallbackPtr->Process(
2804 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
2805 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
2806 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00002807 }
kwiberg55b97fe2016-01-28 05:22:45 -08002808 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002809
kwiberg55b97fe2016-01-28 05:22:45 -08002810 if (_includeAudioLevelIndication) {
2811 size_t length =
2812 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002813 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002814 if (is_muted && previous_frame_muted_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002815 rms_level_.ProcessMuted(length);
2816 } else {
2817 rms_level_.Process(_audioFrame.data_, length);
niklase@google.com470e71d2011-07-07 08:21:25 +00002818 }
kwiberg55b97fe2016-01-28 05:22:45 -08002819 }
solenberg1c2af8e2016-03-24 10:36:00 -07002820 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002821
kwiberg55b97fe2016-01-28 05:22:45 -08002822 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002823}
2824
kwiberg55b97fe2016-01-28 05:22:45 -08002825uint32_t Channel::EncodeAndSend() {
2826 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2827 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002828
kwiberg55b97fe2016-01-28 05:22:45 -08002829 assert(_audioFrame.num_channels_ <= 2);
2830 if (_audioFrame.samples_per_channel_ == 0) {
2831 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2832 "Channel::EncodeAndSend() invalid audio frame");
2833 return 0xFFFFFFFF;
2834 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002835
kwiberg55b97fe2016-01-28 05:22:45 -08002836 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002837
kwiberg55b97fe2016-01-28 05:22:45 -08002838 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002839
kwiberg55b97fe2016-01-28 05:22:45 -08002840 // The ACM resamples internally.
2841 _audioFrame.timestamp_ = _timeStamp;
2842 // This call will trigger AudioPacketizationCallback::SendData if encoding
2843 // is done and payload is ready for packetization and transmission.
2844 // Otherwise, it will return without invoking the callback.
2845 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2846 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2847 "Channel::EncodeAndSend() ACM encoding failed");
2848 return 0xFFFFFFFF;
2849 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002850
kwiberg55b97fe2016-01-28 05:22:45 -08002851 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2852 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002853}
2854
Minyue2013aec2015-05-13 14:14:42 +02002855void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002856 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002857 Channel* channel = associate_send_channel_.channel();
2858 if (channel && channel->ChannelId() == channel_id) {
2859 // If this channel is associated with a send channel of the specified
2860 // Channel ID, disassociate with it.
2861 ChannelOwner ref(NULL);
2862 associate_send_channel_ = ref;
2863 }
2864}
2865
ivoc14d5dbe2016-07-04 07:06:55 -07002866void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2867 event_log_proxy_->SetEventLog(event_log);
2868}
2869
kwiberg55b97fe2016-01-28 05:22:45 -08002870int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
2871 VoEMediaProcess& processObject) {
2872 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2873 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002874
kwiberg55b97fe2016-01-28 05:22:45 -08002875 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002876
kwiberg55b97fe2016-01-28 05:22:45 -08002877 if (kPlaybackPerChannel == type) {
2878 if (_outputExternalMediaCallbackPtr) {
2879 _engineStatisticsPtr->SetLastError(
2880 VE_INVALID_OPERATION, kTraceError,
2881 "Channel::RegisterExternalMediaProcessing() "
2882 "output external media already enabled");
2883 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002884 }
kwiberg55b97fe2016-01-28 05:22:45 -08002885 _outputExternalMediaCallbackPtr = &processObject;
2886 _outputExternalMedia = true;
2887 } else if (kRecordingPerChannel == type) {
2888 if (_inputExternalMediaCallbackPtr) {
2889 _engineStatisticsPtr->SetLastError(
2890 VE_INVALID_OPERATION, kTraceError,
2891 "Channel::RegisterExternalMediaProcessing() "
2892 "output external media already enabled");
2893 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002894 }
kwiberg55b97fe2016-01-28 05:22:45 -08002895 _inputExternalMediaCallbackPtr = &processObject;
2896 channel_state_.SetInputExternalMedia(true);
2897 }
2898 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002899}
2900
kwiberg55b97fe2016-01-28 05:22:45 -08002901int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
2902 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2903 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002904
kwiberg55b97fe2016-01-28 05:22:45 -08002905 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002906
kwiberg55b97fe2016-01-28 05:22:45 -08002907 if (kPlaybackPerChannel == type) {
2908 if (!_outputExternalMediaCallbackPtr) {
2909 _engineStatisticsPtr->SetLastError(
2910 VE_INVALID_OPERATION, kTraceWarning,
2911 "Channel::DeRegisterExternalMediaProcessing() "
2912 "output external media already disabled");
2913 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002914 }
kwiberg55b97fe2016-01-28 05:22:45 -08002915 _outputExternalMedia = false;
2916 _outputExternalMediaCallbackPtr = NULL;
2917 } else if (kRecordingPerChannel == type) {
2918 if (!_inputExternalMediaCallbackPtr) {
2919 _engineStatisticsPtr->SetLastError(
2920 VE_INVALID_OPERATION, kTraceWarning,
2921 "Channel::DeRegisterExternalMediaProcessing() "
2922 "input external media already disabled");
2923 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002924 }
kwiberg55b97fe2016-01-28 05:22:45 -08002925 channel_state_.SetInputExternalMedia(false);
2926 _inputExternalMediaCallbackPtr = NULL;
2927 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002928
kwiberg55b97fe2016-01-28 05:22:45 -08002929 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002930}
2931
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002932int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08002933 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2934 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002935
kwiberg55b97fe2016-01-28 05:22:45 -08002936 if (channel_state_.Get().playing) {
2937 _engineStatisticsPtr->SetLastError(
2938 VE_INVALID_OPERATION, kTraceError,
2939 "Channel::SetExternalMixing() "
2940 "external mixing cannot be changed while playing.");
2941 return -1;
2942 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002943
kwiberg55b97fe2016-01-28 05:22:45 -08002944 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002945
kwiberg55b97fe2016-01-28 05:22:45 -08002946 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002947}
2948
kwiberg55b97fe2016-01-28 05:22:45 -08002949int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2950 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002951}
2952
wu@webrtc.org24301a62013-12-13 19:17:43 +00002953void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2954 audio_coding_->GetDecodingCallStatistics(stats);
2955}
2956
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002957bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
2958 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08002959 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07002960 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002961 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002962 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002963}
2964
solenberg358057b2015-11-27 10:46:42 -08002965uint32_t Channel::GetDelayEstimate() const {
2966 int jitter_buffer_delay_ms = 0;
2967 int playout_buffer_delay_ms = 0;
2968 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
2969 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
2970}
2971
deadbeef74375882015-08-13 12:09:10 -07002972int Channel::LeastRequiredDelayMs() const {
2973 return audio_coding_->LeastRequiredDelayMs();
2974}
2975
kwiberg55b97fe2016-01-28 05:22:45 -08002976int Channel::SetMinimumPlayoutDelay(int delayMs) {
2977 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2978 "Channel::SetMinimumPlayoutDelay()");
2979 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2980 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2981 _engineStatisticsPtr->SetLastError(
2982 VE_INVALID_ARGUMENT, kTraceError,
2983 "SetMinimumPlayoutDelay() invalid min delay");
2984 return -1;
2985 }
2986 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2987 _engineStatisticsPtr->SetLastError(
2988 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2989 "SetMinimumPlayoutDelay() failed to set min playout delay");
2990 return -1;
2991 }
2992 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002993}
2994
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002995int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002996 uint32_t playout_timestamp_rtp = 0;
2997 {
tommi31fc21f2016-01-21 10:37:37 -08002998 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002999 playout_timestamp_rtp = playout_timestamp_rtp_;
3000 }
kwiberg55b97fe2016-01-28 05:22:45 -08003001 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003002 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07003003 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003004 "GetPlayoutTimestamp() failed to retrieve timestamp");
3005 return -1;
3006 }
deadbeef74375882015-08-13 12:09:10 -07003007 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003008 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003009}
3010
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003011int Channel::SetInitTimestamp(unsigned int timestamp) {
3012 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003013 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003014 if (channel_state_.Get().sending) {
3015 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3016 "SetInitTimestamp() already sending");
3017 return -1;
3018 }
3019 _rtpRtcpModule->SetStartTimestamp(timestamp);
3020 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003021}
3022
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003023int Channel::SetInitSequenceNumber(short sequenceNumber) {
3024 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3025 "Channel::SetInitSequenceNumber()");
3026 if (channel_state_.Get().sending) {
3027 _engineStatisticsPtr->SetLastError(
3028 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3029 return -1;
3030 }
3031 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3032 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003033}
3034
kwiberg55b97fe2016-01-28 05:22:45 -08003035int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
3036 RtpReceiver** rtp_receiver) const {
3037 *rtpRtcpModule = _rtpRtcpModule.get();
3038 *rtp_receiver = rtp_receiver_.get();
3039 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003040}
3041
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003042// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3043// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08003044int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08003045 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08003046 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003047
kwiberg55b97fe2016-01-28 05:22:45 -08003048 {
3049 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003050
kwiberg5a25d952016-08-17 07:31:12 -07003051 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003052 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3053 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3054 " doesnt exist");
3055 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003056 }
3057
kwiberg4ec01d92016-08-22 08:43:54 -07003058 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07003059 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003060 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3061 "Channel::MixOrReplaceAudioWithFile() file mixing "
3062 "failed");
3063 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003064 }
kwiberg55b97fe2016-01-28 05:22:45 -08003065 if (fileSamples == 0) {
3066 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3067 "Channel::MixOrReplaceAudioWithFile() file is ended");
3068 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003069 }
kwiberg55b97fe2016-01-28 05:22:45 -08003070 }
3071
3072 assert(_audioFrame.samples_per_channel_ == fileSamples);
3073
3074 if (_mixFileWithMicrophone) {
3075 // Currently file stream is always mono.
3076 // TODO(xians): Change the code when FilePlayer supports real stereo.
3077 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3078 1, fileSamples);
3079 } else {
3080 // Replace ACM audio with file.
3081 // Currently file stream is always mono.
3082 // TODO(xians): Change the code when FilePlayer supports real stereo.
3083 _audioFrame.UpdateFrame(
3084 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3085 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3086 }
3087 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003088}
3089
kwiberg55b97fe2016-01-28 05:22:45 -08003090int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3091 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003092
kwibergb7f89d62016-02-17 10:04:18 -08003093 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003094 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003095
kwiberg55b97fe2016-01-28 05:22:45 -08003096 {
3097 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003098
kwiberg5a25d952016-08-17 07:31:12 -07003099 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003100 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3101 "Channel::MixAudioWithFile() file mixing failed");
3102 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003103 }
3104
kwiberg55b97fe2016-01-28 05:22:45 -08003105 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003106 if (output_file_player_->Get10msAudioFromFile(
3107 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003108 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3109 "Channel::MixAudioWithFile() file mixing failed");
3110 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003111 }
kwiberg55b97fe2016-01-28 05:22:45 -08003112 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003113
kwiberg55b97fe2016-01-28 05:22:45 -08003114 if (audioFrame.samples_per_channel_ == fileSamples) {
3115 // Currently file stream is always mono.
3116 // TODO(xians): Change the code when FilePlayer supports real stereo.
3117 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3118 fileSamples);
3119 } else {
3120 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3121 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3122 ") != "
3123 "fileSamples(%" PRIuS ")",
3124 audioFrame.samples_per_channel_, fileSamples);
3125 return -1;
3126 }
3127
3128 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003129}
3130
deadbeef74375882015-08-13 12:09:10 -07003131void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003132 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003133
henrik.lundin96bd5022016-04-06 04:13:56 -07003134 if (!jitter_buffer_playout_timestamp_) {
3135 // This can happen if this channel has not received any RTP packets. In
3136 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003137 return;
3138 }
3139
3140 uint16_t delay_ms = 0;
3141 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003142 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003143 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3144 " delay from the ADM");
3145 _engineStatisticsPtr->SetLastError(
3146 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3147 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3148 return;
3149 }
3150
henrik.lundin96bd5022016-04-06 04:13:56 -07003151 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3152 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003153
3154 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003155 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003156
kwiberg55b97fe2016-01-28 05:22:45 -08003157 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003158 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003159 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003160
3161 {
tommi31fc21f2016-01-21 10:37:37 -08003162 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003163 if (rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003164 playout_timestamp_rtcp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003165 } else {
henrik.lundin96bd5022016-04-06 04:13:56 -07003166 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003167 }
3168 playout_delay_ms_ = delay_ms;
3169 }
3170}
3171
kwiberg55b97fe2016-01-28 05:22:45 -08003172void Channel::RegisterReceiveCodecsToRTPModule() {
3173 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3174 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003175
kwiberg55b97fe2016-01-28 05:22:45 -08003176 CodecInst codec;
3177 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003178
kwiberg55b97fe2016-01-28 05:22:45 -08003179 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3180 // Open up the RTP/RTCP receiver for all supported codecs
3181 if ((audio_coding_->Codec(idx, &codec) == -1) ||
3182 (rtp_receiver_->RegisterReceivePayload(
3183 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3184 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
3185 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3186 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3187 " to register %s (%d/%d/%" PRIuS
3188 "/%d) to RTP/RTCP "
3189 "receiver",
3190 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3191 codec.rate);
3192 } else {
3193 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3194 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3195 "(%d/%d/%" PRIuS
3196 "/%d) has been added to the RTP/RTCP "
3197 "receiver",
3198 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3199 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003200 }
kwiberg55b97fe2016-01-28 05:22:45 -08003201 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003202}
3203
kwiberg55b97fe2016-01-28 05:22:45 -08003204int Channel::SetSendRtpHeaderExtension(bool enable,
3205 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003206 unsigned char id) {
3207 int error = 0;
3208 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3209 if (enable) {
3210 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3211 }
3212 return error;
3213}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003214
ossue280cde2016-10-12 11:04:10 -07003215int Channel::GetRtpTimestampRateHz() const {
3216 const auto format = audio_coding_->ReceiveFormat();
3217 // Default to the playout frequency if we've not gotten any packets yet.
3218 // TODO(ossu): Zero clockrate can only happen if we've added an external
3219 // decoder for a format we don't support internally. Remove once that way of
3220 // adding decoders is gone!
3221 return (format && format->clockrate_hz != 0)
3222 ? format->clockrate_hz
3223 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003224}
3225
Minyue2013aec2015-05-13 14:14:42 +02003226int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003227 RtcpMode method = _rtpRtcpModule->RTCP();
3228 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003229 return 0;
3230 }
3231 std::vector<RTCPReportBlock> report_blocks;
3232 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003233
3234 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003235 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003236 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003237 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003238 Channel* channel = associate_send_channel_.channel();
3239 // Tries to get RTT from an associated channel. This is important for
3240 // receive-only channels.
3241 if (channel) {
3242 // To prevent infinite recursion and deadlock, calling GetRTT of
3243 // associate channel should always use "false" for argument:
3244 // |allow_associate_channel|.
3245 rtt = channel->GetRTT(false);
3246 }
3247 }
3248 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003249 }
3250
3251 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3252 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3253 for (; it != report_blocks.end(); ++it) {
3254 if (it->remoteSSRC == remoteSSRC)
3255 break;
3256 }
3257 if (it == report_blocks.end()) {
3258 // We have not received packets with SSRC matching the report blocks.
3259 // To calculate RTT we try with the SSRC of the first report block.
3260 // This is very important for send-only channels where we don't know
3261 // the SSRC of the other end.
3262 remoteSSRC = report_blocks[0].remoteSSRC;
3263 }
Minyue2013aec2015-05-13 14:14:42 +02003264
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003265 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003266 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003267 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003268 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3269 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003270 return 0;
3271 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003272 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003273}
3274
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003275} // namespace voe
3276} // namespace webrtc