blob: 44a1c73b08c64624b9b6b54fd6a41ed91fa193f5 [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
aleloiaed581a2016-10-20 06:32:39 -0700715AudioMixer::Source::AudioFrameWithInfo Channel::GetAudioFrameWithInfo(
716 int sample_rate_hz) {
717 mix_audio_frame_.sample_rate_hz_ = sample_rate_hz;
718
719 const auto frame_info = GetAudioFrameWithMuted(-1, &mix_audio_frame_);
720
721 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
722 FrameInfo new_audio_frame_info = FrameInfo::kError;
723 switch (frame_info) {
724 case MixerParticipant::AudioFrameInfo::kNormal:
725 new_audio_frame_info = FrameInfo::kNormal;
726 break;
727 case MixerParticipant::AudioFrameInfo::kMuted:
728 new_audio_frame_info = FrameInfo::kMuted;
729 break;
730 case MixerParticipant::AudioFrameInfo::kError:
731 new_audio_frame_info = FrameInfo::kError;
732 break;
733 }
734 return {&mix_audio_frame_, new_audio_frame_info};
735}
736
kwiberg55b97fe2016-01-28 05:22:45 -0800737int32_t Channel::NeededFrequency(int32_t id) const {
738 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
739 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000740
kwiberg55b97fe2016-01-28 05:22:45 -0800741 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000742
kwiberg55b97fe2016-01-28 05:22:45 -0800743 // Determine highest needed receive frequency
744 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000745
kwiberg55b97fe2016-01-28 05:22:45 -0800746 // Return the bigger of playout and receive frequency in the ACM.
747 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
748 highestNeeded = audio_coding_->PlayoutFrequency();
749 } else {
750 highestNeeded = receiveFrequency;
751 }
752
753 // Special case, if we're playing a file on the playout side
754 // we take that frequency into consideration as well
755 // This is not needed on sending side, since the codec will
756 // limit the spectrum anyway.
757 if (channel_state_.Get().output_file_playing) {
758 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700759 if (output_file_player_) {
760 if (output_file_player_->Frequency() > highestNeeded) {
761 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800762 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000763 }
kwiberg55b97fe2016-01-28 05:22:45 -0800764 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000765
kwiberg55b97fe2016-01-28 05:22:45 -0800766 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000767}
768
ossu5f7cfa52016-05-30 08:11:28 -0700769int32_t Channel::CreateChannel(
770 Channel*& channel,
771 int32_t channelId,
772 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700773 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800774 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
775 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
776 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000777
solenberg88499ec2016-09-07 07:34:41 -0700778 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800779 if (channel == NULL) {
780 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
781 "Channel::CreateChannel() unable to allocate memory for"
782 " channel");
783 return -1;
784 }
785 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000786}
787
kwiberg55b97fe2016-01-28 05:22:45 -0800788void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
789 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
790 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
791 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000792
kwiberg55b97fe2016-01-28 05:22:45 -0800793 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000794}
795
kwiberg55b97fe2016-01-28 05:22:45 -0800796void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
797 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
798 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
799 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000800
kwiberg55b97fe2016-01-28 05:22:45 -0800801 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000802}
803
kwiberg55b97fe2016-01-28 05:22:45 -0800804void Channel::PlayFileEnded(int32_t id) {
805 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
806 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000807
kwiberg55b97fe2016-01-28 05:22:45 -0800808 if (id == _inputFilePlayerId) {
809 channel_state_.SetInputFilePlaying(false);
810 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
811 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000812 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800813 } else if (id == _outputFilePlayerId) {
814 channel_state_.SetOutputFilePlaying(false);
815 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
816 "Channel::PlayFileEnded() => output file player module is"
817 " shutdown");
818 }
819}
820
821void Channel::RecordFileEnded(int32_t id) {
822 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
823 "Channel::RecordFileEnded(id=%d)", id);
824
825 assert(id == _outputFileRecorderId);
826
827 rtc::CritScope cs(&_fileCritSect);
828
829 _outputFileRecording = false;
830 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
831 "Channel::RecordFileEnded() => output file recorder module is"
832 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000833}
834
pbos@webrtc.org92135212013-05-14 08:31:39 +0000835Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000836 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700837 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800838 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100839 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700840 event_log_proxy_(new RtcEventLogProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100841 rtp_header_parser_(RtpHeaderParser::Create()),
842 rtp_payload_registry_(
843 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
844 rtp_receive_statistics_(
845 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
846 rtp_receiver_(
847 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100848 this,
849 this,
850 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700851 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100852 _outputAudioLevel(),
853 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100854 // Avoid conflict with other channels by adding 1024 - 1026,
855 // won't use as much as 1024 channels.
856 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
857 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
858 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
859 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100860 _outputExternalMedia(false),
861 _inputExternalMediaCallbackPtr(NULL),
862 _outputExternalMediaCallbackPtr(NULL),
863 _timeStamp(0), // This is just an offset, RTP module will add it's own
864 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100865 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100866 playout_timestamp_rtp_(0),
867 playout_timestamp_rtcp_(0),
868 playout_delay_ms_(0),
869 _numberOfDiscardedPackets(0),
870 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100871 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
872 capture_start_rtp_time_stamp_(-1),
873 capture_start_ntp_time_ms_(-1),
874 _engineStatisticsPtr(NULL),
875 _outputMixerPtr(NULL),
876 _transmitMixerPtr(NULL),
877 _moduleProcessThreadPtr(NULL),
878 _audioDeviceModulePtr(NULL),
879 _voiceEngineObserverPtr(NULL),
880 _callbackCritSectPtr(NULL),
881 _transportPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100882 _sendFrameType(0),
883 _externalMixing(false),
884 _mixFileWithMicrophone(false),
solenberg1c2af8e2016-03-24 10:36:00 -0700885 input_mute_(false),
886 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100887 _panLeft(1.0f),
888 _panRight(1.0f),
889 _outputGain(1.0f),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100890 _lastLocalTimeStamp(0),
891 _lastPayloadType(0),
892 _includeAudioLevelIndication(false),
893 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100894 restored_packet_in_use_(false),
895 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100896 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700897 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800898 feedback_observer_proxy_(new TransportFeedbackProxy()),
899 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700900 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200901 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
902 kMaxRetransmissionWindowMs)),
solenberg88499ec2016-09-07 07:34:41 -0700903 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800904 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
905 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700906 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800907 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700908 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800909 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200910
kwiberg55b97fe2016-01-28 05:22:45 -0800911 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000912
kwiberg55b97fe2016-01-28 05:22:45 -0800913 RtpRtcp::Configuration configuration;
914 configuration.audio = true;
915 configuration.outgoing_transport = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800916 configuration.receive_statistics = rtp_receive_statistics_.get();
917 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800918 if (pacing_enabled_) {
919 configuration.paced_sender = rtp_packet_sender_proxy_.get();
920 configuration.transport_sequence_number_allocator =
921 seq_num_allocator_proxy_.get();
922 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
923 }
ivoc14d5dbe2016-07-04 07:06:55 -0700924 configuration.event_log = &(*event_log_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200925 configuration.retransmission_rate_limiter =
926 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000927
kwiberg55b97fe2016-01-28 05:22:45 -0800928 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100929 _rtpRtcpModule->SetSendingMediaStatus(false);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000930
kwiberg55b97fe2016-01-28 05:22:45 -0800931 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
932 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
933 statistics_proxy_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000934}
935
kwiberg55b97fe2016-01-28 05:22:45 -0800936Channel::~Channel() {
937 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
938 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
939 "Channel::~Channel() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +0000940
kwiberg55b97fe2016-01-28 05:22:45 -0800941 if (_outputExternalMedia) {
942 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
943 }
944 if (channel_state_.Get().input_external_media) {
945 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
946 }
947 StopSend();
948 StopPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000949
kwiberg55b97fe2016-01-28 05:22:45 -0800950 {
951 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700952 if (input_file_player_) {
953 input_file_player_->RegisterModuleFileCallback(NULL);
954 input_file_player_->StopPlayingFile();
niklase@google.com470e71d2011-07-07 08:21:25 +0000955 }
kwiberg5a25d952016-08-17 07:31:12 -0700956 if (output_file_player_) {
957 output_file_player_->RegisterModuleFileCallback(NULL);
958 output_file_player_->StopPlayingFile();
kwiberg55b97fe2016-01-28 05:22:45 -0800959 }
kwiberg5a25d952016-08-17 07:31:12 -0700960 if (output_file_recorder_) {
961 output_file_recorder_->RegisterModuleFileCallback(NULL);
962 output_file_recorder_->StopRecording();
kwiberg55b97fe2016-01-28 05:22:45 -0800963 }
964 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000965
kwiberg55b97fe2016-01-28 05:22:45 -0800966 // The order to safely shutdown modules in a channel is:
967 // 1. De-register callbacks in modules
968 // 2. De-register modules in process thread
969 // 3. Destroy modules
970 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
971 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
972 "~Channel() failed to de-register transport callback"
973 " (Audio coding module)");
974 }
975 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
976 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
977 "~Channel() failed to de-register VAD callback"
978 " (Audio coding module)");
979 }
980 // De-register modules in process thread
981 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000982
kwiberg55b97fe2016-01-28 05:22:45 -0800983 // End of modules shutdown
niklase@google.com470e71d2011-07-07 08:21:25 +0000984}
985
kwiberg55b97fe2016-01-28 05:22:45 -0800986int32_t Channel::Init() {
987 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
988 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000989
kwiberg55b97fe2016-01-28 05:22:45 -0800990 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000991
kwiberg55b97fe2016-01-28 05:22:45 -0800992 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000993
kwiberg55b97fe2016-01-28 05:22:45 -0800994 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
995 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
996 "Channel::Init() must call SetEngineInformation() first");
997 return -1;
998 }
999
1000 // --- Add modules to process thread (for periodic schedulation)
1001
1002 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
1003
1004 // --- ACM initialization
1005
1006 if (audio_coding_->InitializeReceiver() == -1) {
1007 _engineStatisticsPtr->SetLastError(
1008 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1009 "Channel::Init() unable to initialize the ACM - 1");
1010 return -1;
1011 }
1012
1013 // --- RTP/RTCP module initialization
1014
1015 // Ensure that RTCP is enabled by default for the created channel.
1016 // Note that, the module will keep generating RTCP until it is explicitly
1017 // disabled by the user.
1018 // After StopListen (when no sockets exists), RTCP packets will no longer
1019 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -07001020 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001021 // RTCP is enabled by default.
1022 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1023 // --- Register all permanent callbacks
1024 const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) ||
1025 (audio_coding_->RegisterVADCallback(this) == -1);
1026
1027 if (fail) {
1028 _engineStatisticsPtr->SetLastError(
1029 VE_CANNOT_INIT_CHANNEL, kTraceError,
1030 "Channel::Init() callbacks not registered");
1031 return -1;
1032 }
1033
1034 // --- Register all supported codecs to the receiving side of the
1035 // RTP/RTCP module
1036
1037 CodecInst codec;
1038 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1039
1040 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1041 // Open up the RTP/RTCP receiver for all supported codecs
1042 if ((audio_coding_->Codec(idx, &codec) == -1) ||
1043 (rtp_receiver_->RegisterReceivePayload(
1044 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1045 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
1046 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1047 "Channel::Init() unable to register %s "
1048 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1049 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1050 codec.rate);
1051 } else {
1052 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1053 "Channel::Init() %s (%d/%d/%" PRIuS
1054 "/%d) has been "
1055 "added to the RTP/RTCP receiver",
1056 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1057 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001058 }
1059
kwiberg55b97fe2016-01-28 05:22:45 -08001060 // Ensure that PCMU is used as default codec on the sending side
1061 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1062 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001063 }
1064
kwiberg55b97fe2016-01-28 05:22:45 -08001065 // Register default PT for outband 'telephone-event'
1066 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001067 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
1068 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001069 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1070 "Channel::Init() failed to register outband "
1071 "'telephone-event' (%d/%d) correctly",
1072 codec.pltype, codec.plfreq);
1073 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001074 }
1075
kwiberg55b97fe2016-01-28 05:22:45 -08001076 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001077 if (!codec_manager_.RegisterEncoder(codec) ||
1078 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
1079 !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec) ||
1080 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001081 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1082 "Channel::Init() failed to register CN (%d/%d) "
1083 "correctly - 1",
1084 codec.pltype, codec.plfreq);
1085 }
1086 }
kwiberg55b97fe2016-01-28 05:22:45 -08001087 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001088
kwiberg55b97fe2016-01-28 05:22:45 -08001089 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001090}
1091
kwiberg55b97fe2016-01-28 05:22:45 -08001092int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1093 OutputMixer& outputMixer,
1094 voe::TransmitMixer& transmitMixer,
1095 ProcessThread& moduleProcessThread,
1096 AudioDeviceModule& audioDeviceModule,
1097 VoiceEngineObserver* voiceEngineObserver,
1098 rtc::CriticalSection* callbackCritSect) {
1099 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1100 "Channel::SetEngineInformation()");
1101 _engineStatisticsPtr = &engineStatistics;
1102 _outputMixerPtr = &outputMixer;
1103 _transmitMixerPtr = &transmitMixer,
1104 _moduleProcessThreadPtr = &moduleProcessThread;
1105 _audioDeviceModulePtr = &audioDeviceModule;
1106 _voiceEngineObserverPtr = voiceEngineObserver;
1107 _callbackCritSectPtr = callbackCritSect;
1108 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001109}
1110
kwiberg55b97fe2016-01-28 05:22:45 -08001111int32_t Channel::UpdateLocalTimeStamp() {
1112 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1113 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001114}
1115
kwibergb7f89d62016-02-17 10:04:18 -08001116void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001117 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001118 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001119}
1120
ossu29b1a8d2016-06-13 07:34:51 -07001121const rtc::scoped_refptr<AudioDecoderFactory>&
1122Channel::GetAudioDecoderFactory() const {
1123 return decoder_factory_;
1124}
1125
kwiberg55b97fe2016-01-28 05:22:45 -08001126int32_t Channel::StartPlayout() {
1127 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1128 "Channel::StartPlayout()");
1129 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001130 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001131 }
1132
1133 if (!_externalMixing) {
1134 // Add participant as candidates for mixing.
1135 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1136 _engineStatisticsPtr->SetLastError(
1137 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1138 "StartPlayout() failed to add participant to mixer");
1139 return -1;
1140 }
1141 }
1142
1143 channel_state_.SetPlaying(true);
1144 if (RegisterFilePlayingToMixer() != 0)
1145 return -1;
1146
1147 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001148}
1149
kwiberg55b97fe2016-01-28 05:22:45 -08001150int32_t Channel::StopPlayout() {
1151 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1152 "Channel::StopPlayout()");
1153 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001154 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001155 }
1156
1157 if (!_externalMixing) {
1158 // Remove participant as candidates for mixing
1159 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1160 _engineStatisticsPtr->SetLastError(
1161 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1162 "StopPlayout() failed to remove participant from mixer");
1163 return -1;
1164 }
1165 }
1166
1167 channel_state_.SetPlaying(false);
1168 _outputAudioLevel.Clear();
1169
1170 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001171}
1172
kwiberg55b97fe2016-01-28 05:22:45 -08001173int32_t Channel::StartSend() {
1174 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1175 "Channel::StartSend()");
1176 // Resume the previous sequence number which was reset by StopSend().
1177 // This needs to be done before |sending| is set to true.
1178 if (send_sequence_number_)
1179 SetInitSequenceNumber(send_sequence_number_);
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001180
kwiberg55b97fe2016-01-28 05:22:45 -08001181 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001182 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001183 }
1184 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001185
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001186 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001187 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1188 _engineStatisticsPtr->SetLastError(
1189 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1190 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001191 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001192 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001193 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001194 return -1;
1195 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001196
kwiberg55b97fe2016-01-28 05:22:45 -08001197 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001198}
1199
kwiberg55b97fe2016-01-28 05:22:45 -08001200int32_t Channel::StopSend() {
1201 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1202 "Channel::StopSend()");
1203 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001204 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001205 }
1206 channel_state_.SetSending(false);
1207
1208 // Store the sequence number to be able to pick up the same sequence for
1209 // the next StartSend(). This is needed for restarting device, otherwise
1210 // it might cause libSRTP to complain about packets being replayed.
1211 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1212 // CL is landed. See issue
1213 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1214 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1215
1216 // Reset sending SSRC and sequence number and triggers direct transmission
1217 // of RTCP BYE
1218 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1219 _engineStatisticsPtr->SetLastError(
1220 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1221 "StartSend() RTP/RTCP failed to stop sending");
1222 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001223 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001224
1225 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001226}
1227
kwiberg55b97fe2016-01-28 05:22:45 -08001228int32_t Channel::StartReceiving() {
1229 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1230 "Channel::StartReceiving()");
1231 if (channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001232 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001233 }
1234 channel_state_.SetReceiving(true);
1235 _numberOfDiscardedPackets = 0;
1236 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001237}
1238
kwiberg55b97fe2016-01-28 05:22:45 -08001239int32_t Channel::StopReceiving() {
1240 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1241 "Channel::StopReceiving()");
1242 if (!channel_state_.Get().receiving) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001243 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001244 }
1245
1246 channel_state_.SetReceiving(false);
1247 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001248}
1249
kwiberg55b97fe2016-01-28 05:22:45 -08001250int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1251 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1252 "Channel::RegisterVoiceEngineObserver()");
1253 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001254
kwiberg55b97fe2016-01-28 05:22:45 -08001255 if (_voiceEngineObserverPtr) {
1256 _engineStatisticsPtr->SetLastError(
1257 VE_INVALID_OPERATION, kTraceError,
1258 "RegisterVoiceEngineObserver() observer already enabled");
1259 return -1;
1260 }
1261 _voiceEngineObserverPtr = &observer;
1262 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001263}
1264
kwiberg55b97fe2016-01-28 05:22:45 -08001265int32_t Channel::DeRegisterVoiceEngineObserver() {
1266 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1267 "Channel::DeRegisterVoiceEngineObserver()");
1268 rtc::CritScope cs(&_callbackCritSect);
1269
1270 if (!_voiceEngineObserverPtr) {
1271 _engineStatisticsPtr->SetLastError(
1272 VE_INVALID_OPERATION, kTraceWarning,
1273 "DeRegisterVoiceEngineObserver() observer already disabled");
1274 return 0;
1275 }
1276 _voiceEngineObserverPtr = NULL;
1277 return 0;
1278}
1279
1280int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001281 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001282 if (send_codec) {
1283 codec = *send_codec;
1284 return 0;
1285 }
1286 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001287}
1288
kwiberg55b97fe2016-01-28 05:22:45 -08001289int32_t Channel::GetRecCodec(CodecInst& codec) {
1290 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001291}
1292
kwiberg55b97fe2016-01-28 05:22:45 -08001293int32_t Channel::SetSendCodec(const CodecInst& codec) {
1294 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1295 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001296
kwibergc8d071e2016-04-06 12:22:38 -07001297 if (!codec_manager_.RegisterEncoder(codec) ||
1298 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001299 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1300 "SetSendCodec() failed to register codec to ACM");
1301 return -1;
1302 }
1303
1304 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1305 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1306 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1307 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1308 "SetSendCodec() failed to register codec to"
1309 " RTP/RTCP module");
1310 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001311 }
kwiberg55b97fe2016-01-28 05:22:45 -08001312 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001313
kwiberg55b97fe2016-01-28 05:22:45 -08001314 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) {
1315 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1316 "SetSendCodec() failed to set audio packet size");
1317 return -1;
1318 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001319
kwiberg55b97fe2016-01-28 05:22:45 -08001320 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001321}
1322
Ivo Creusenadf89b72015-04-29 16:03:33 +02001323void Channel::SetBitRate(int bitrate_bps) {
1324 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1325 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001326 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1327 if (*encoder)
1328 (*encoder)->OnReceivedTargetAudioBitrate(bitrate_bps);
1329 });
Erik Språng737336d2016-07-29 12:59:36 +02001330 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001331}
1332
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001333void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001334 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1335 if (*encoder)
1336 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1337 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001338}
1339
kwiberg55b97fe2016-01-28 05:22:45 -08001340int32_t Channel::SetVADStatus(bool enableVAD,
1341 ACMVADMode mode,
1342 bool disableDTX) {
1343 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1344 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001345 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1346 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1347 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001348 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1349 kTraceError,
1350 "SetVADStatus() failed to set VAD");
1351 return -1;
1352 }
1353 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001354}
1355
kwiberg55b97fe2016-01-28 05:22:45 -08001356int32_t Channel::GetVADStatus(bool& enabledVAD,
1357 ACMVADMode& mode,
1358 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001359 const auto* params = codec_manager_.GetStackParams();
1360 enabledVAD = params->use_cng;
1361 mode = params->vad_mode;
1362 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001363 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001364}
1365
kwiberg55b97fe2016-01-28 05:22:45 -08001366int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
1367 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1368 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001369
kwiberg55b97fe2016-01-28 05:22:45 -08001370 if (channel_state_.Get().playing) {
1371 _engineStatisticsPtr->SetLastError(
1372 VE_ALREADY_PLAYING, kTraceError,
1373 "SetRecPayloadType() unable to set PT while playing");
1374 return -1;
1375 }
1376 if (channel_state_.Get().receiving) {
1377 _engineStatisticsPtr->SetLastError(
1378 VE_ALREADY_LISTENING, kTraceError,
1379 "SetRecPayloadType() unable to set PT while listening");
1380 return -1;
1381 }
1382
1383 if (codec.pltype == -1) {
1384 // De-register the selected codec (RTP/RTCP module and ACM)
1385
1386 int8_t pltype(-1);
1387 CodecInst rxCodec = codec;
1388
1389 // Get payload type for the given codec
1390 rtp_payload_registry_->ReceivePayloadType(
1391 rxCodec.plname, rxCodec.plfreq, rxCodec.channels,
1392 (rxCodec.rate < 0) ? 0 : rxCodec.rate, &pltype);
1393 rxCodec.pltype = pltype;
1394
1395 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1396 _engineStatisticsPtr->SetLastError(
1397 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1398 "SetRecPayloadType() RTP/RTCP-module deregistration "
1399 "failed");
1400 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001401 }
kwiberg55b97fe2016-01-28 05:22:45 -08001402 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1403 _engineStatisticsPtr->SetLastError(
1404 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1405 "SetRecPayloadType() ACM deregistration failed - 1");
1406 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001407 }
kwiberg55b97fe2016-01-28 05:22:45 -08001408 return 0;
1409 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001410
kwiberg55b97fe2016-01-28 05:22:45 -08001411 if (rtp_receiver_->RegisterReceivePayload(
1412 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1413 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1414 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001415 // TODO(kwiberg): Retrying is probably not necessary, since
1416 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001417 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001418 if (rtp_receiver_->RegisterReceivePayload(
kwiberg55b97fe2016-01-28 05:22:45 -08001419 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1420 (codec.rate < 0) ? 0 : codec.rate) != 0) {
1421 _engineStatisticsPtr->SetLastError(
1422 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1423 "SetRecPayloadType() RTP/RTCP-module registration failed");
1424 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001425 }
kwiberg55b97fe2016-01-28 05:22:45 -08001426 }
kwibergc8d071e2016-04-06 12:22:38 -07001427 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001428 audio_coding_->UnregisterReceiveCodec(codec.pltype);
kwibergc8d071e2016-04-06 12:22:38 -07001429 if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001430 _engineStatisticsPtr->SetLastError(
1431 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1432 "SetRecPayloadType() ACM registration failed - 1");
1433 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001434 }
kwiberg55b97fe2016-01-28 05:22:45 -08001435 }
1436 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001437}
1438
kwiberg55b97fe2016-01-28 05:22:45 -08001439int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1440 int8_t payloadType(-1);
1441 if (rtp_payload_registry_->ReceivePayloadType(
1442 codec.plname, codec.plfreq, codec.channels,
1443 (codec.rate < 0) ? 0 : codec.rate, &payloadType) != 0) {
1444 _engineStatisticsPtr->SetLastError(
1445 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1446 "GetRecPayloadType() failed to retrieve RX payload type");
1447 return -1;
1448 }
1449 codec.pltype = payloadType;
1450 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001451}
1452
kwiberg55b97fe2016-01-28 05:22:45 -08001453int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1454 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1455 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001456
kwiberg55b97fe2016-01-28 05:22:45 -08001457 CodecInst codec;
1458 int32_t samplingFreqHz(-1);
1459 const size_t kMono = 1;
1460 if (frequency == kFreq32000Hz)
1461 samplingFreqHz = 32000;
1462 else if (frequency == kFreq16000Hz)
1463 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001464
kwiberg55b97fe2016-01-28 05:22:45 -08001465 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1466 _engineStatisticsPtr->SetLastError(
1467 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1468 "SetSendCNPayloadType() failed to retrieve default CN codec "
1469 "settings");
1470 return -1;
1471 }
1472
1473 // Modify the payload type (must be set to dynamic range)
1474 codec.pltype = type;
1475
kwibergc8d071e2016-04-06 12:22:38 -07001476 if (!codec_manager_.RegisterEncoder(codec) ||
1477 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001478 _engineStatisticsPtr->SetLastError(
1479 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1480 "SetSendCNPayloadType() failed to register CN to ACM");
1481 return -1;
1482 }
1483
1484 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1485 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1486 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1487 _engineStatisticsPtr->SetLastError(
1488 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1489 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1490 "module");
1491 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001492 }
kwiberg55b97fe2016-01-28 05:22:45 -08001493 }
1494 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001495}
1496
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001497int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001498 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001499 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001500
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001501 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001502 _engineStatisticsPtr->SetLastError(
1503 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001504 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001505 return -1;
1506 }
1507 return 0;
1508}
1509
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001510int Channel::SetOpusDtx(bool enable_dtx) {
1511 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1512 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001513 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001514 : audio_coding_->DisableOpusDtx();
1515 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001516 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1517 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001518 return -1;
1519 }
1520 return 0;
1521}
1522
ivoc85228d62016-07-27 04:53:47 -07001523int Channel::GetOpusDtx(bool* enabled) {
1524 int success = -1;
1525 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1526 if (encoder) {
1527 *enabled = encoder->GetDtx();
1528 success = 0;
1529 }
1530 });
1531 return success;
1532}
1533
minyue7e304322016-10-12 05:00:55 -07001534bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1535 bool success = false;
1536 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1537 if (*encoder) {
1538 success = (*encoder)->EnableAudioNetworkAdaptor(
1539 config_string, Clock::GetRealTimeClock());
1540 }
1541 });
1542 return success;
1543}
1544
1545void Channel::DisableAudioNetworkAdaptor() {
1546 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1547 if (*encoder)
1548 (*encoder)->DisableAudioNetworkAdaptor();
1549 });
1550}
1551
1552void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1553 int max_frame_length_ms) {
1554 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1555 if (*encoder) {
1556 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1557 max_frame_length_ms);
1558 }
1559 });
1560}
1561
mflodman3d7db262016-04-29 00:57:13 -07001562int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001563 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001564 "Channel::RegisterExternalTransport()");
1565
kwiberg55b97fe2016-01-28 05:22:45 -08001566 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001567 if (_externalTransport) {
1568 _engineStatisticsPtr->SetLastError(
1569 VE_INVALID_OPERATION, kTraceError,
1570 "RegisterExternalTransport() external transport already enabled");
1571 return -1;
1572 }
1573 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001574 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001575 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001576}
1577
kwiberg55b97fe2016-01-28 05:22:45 -08001578int32_t Channel::DeRegisterExternalTransport() {
1579 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1580 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001581
kwiberg55b97fe2016-01-28 05:22:45 -08001582 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001583 if (_transportPtr) {
1584 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1585 "DeRegisterExternalTransport() all transport is disabled");
1586 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001587 _engineStatisticsPtr->SetLastError(
1588 VE_INVALID_OPERATION, kTraceWarning,
1589 "DeRegisterExternalTransport() external transport already "
1590 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001591 }
1592 _externalTransport = false;
1593 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001594 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001595}
1596
mflodman3d7db262016-04-29 00:57:13 -07001597int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001598 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001599 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001600 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001601 "Channel::ReceivedRTPPacket()");
1602
1603 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001604 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001605
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001606 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001607 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1608 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1609 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001610 return -1;
1611 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001612 header.payload_type_frequency =
1613 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001614 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001615 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001616 bool in_order = IsPacketInOrder(header);
kwiberg55b97fe2016-01-28 05:22:45 -08001617 rtp_receive_statistics_->IncomingPacket(
1618 header, length, IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001619 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001620
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001621 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001622}
1623
1624bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001625 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001626 const RTPHeader& header,
1627 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001628 if (rtp_payload_registry_->IsRtx(header)) {
1629 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001630 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001631 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001632 assert(packet_length >= header.headerLength);
1633 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001634 PayloadUnion payload_specific;
1635 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001636 &payload_specific)) {
1637 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001638 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001639 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1640 payload_specific, in_order);
1641}
1642
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001643bool Channel::HandleRtxPacket(const uint8_t* packet,
1644 size_t packet_length,
1645 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001646 if (!rtp_payload_registry_->IsRtx(header))
1647 return false;
1648
1649 // Remove the RTX header and parse the original RTP header.
1650 if (packet_length < header.headerLength)
1651 return false;
1652 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1653 return false;
1654 if (restored_packet_in_use_) {
1655 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1656 "Multiple RTX headers detected, dropping packet");
1657 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001658 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001659 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001660 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1661 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001662 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1663 "Incoming RTX packet: invalid RTP header");
1664 return false;
1665 }
1666 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001667 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001668 restored_packet_in_use_ = false;
1669 return ret;
1670}
1671
1672bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1673 StreamStatistician* statistician =
1674 rtp_receive_statistics_->GetStatistician(header.ssrc);
1675 if (!statistician)
1676 return false;
1677 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001678}
1679
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001680bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1681 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001682 // Retransmissions are handled separately if RTX is enabled.
1683 if (rtp_payload_registry_->RtxEnabled())
1684 return false;
1685 StreamStatistician* statistician =
1686 rtp_receive_statistics_->GetStatistician(header.ssrc);
1687 if (!statistician)
1688 return false;
1689 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001690 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001691 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001692 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001693}
1694
mflodman3d7db262016-04-29 00:57:13 -07001695int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001696 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001697 "Channel::ReceivedRTCPPacket()");
1698 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001699 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001700
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001701 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001702 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001703 _engineStatisticsPtr->SetLastError(
1704 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1705 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1706 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001707
Minyue2013aec2015-05-13 14:14:42 +02001708 int64_t rtt = GetRTT(true);
1709 if (rtt == 0) {
1710 // Waiting for valid RTT.
1711 return 0;
1712 }
Erik Språng737336d2016-07-29 12:59:36 +02001713
1714 int64_t nack_window_ms = rtt;
1715 if (nack_window_ms < kMinRetransmissionWindowMs) {
1716 nack_window_ms = kMinRetransmissionWindowMs;
1717 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1718 nack_window_ms = kMaxRetransmissionWindowMs;
1719 }
1720 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1721
minyue7e304322016-10-12 05:00:55 -07001722 // Invoke audio encoders OnReceivedRtt().
1723 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1724 if (*encoder)
1725 (*encoder)->OnReceivedRtt(rtt);
1726 });
1727
Minyue2013aec2015-05-13 14:14:42 +02001728 uint32_t ntp_secs = 0;
1729 uint32_t ntp_frac = 0;
1730 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001731 if (0 !=
1732 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1733 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001734 // Waiting for RTCP.
1735 return 0;
1736 }
1737
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001738 {
tommi31fc21f2016-01-21 10:37:37 -08001739 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001740 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001741 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001742 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001743}
1744
niklase@google.com470e71d2011-07-07 08:21:25 +00001745int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001746 bool loop,
1747 FileFormats format,
1748 int startPosition,
1749 float volumeScaling,
1750 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001751 const CodecInst* codecInst) {
1752 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1753 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1754 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1755 "stopPosition=%d)",
1756 fileName, loop, format, volumeScaling, startPosition,
1757 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001758
kwiberg55b97fe2016-01-28 05:22:45 -08001759 if (channel_state_.Get().output_file_playing) {
1760 _engineStatisticsPtr->SetLastError(
1761 VE_ALREADY_PLAYING, kTraceError,
1762 "StartPlayingFileLocally() is already playing");
1763 return -1;
1764 }
1765
1766 {
1767 rtc::CritScope cs(&_fileCritSect);
1768
kwiberg5a25d952016-08-17 07:31:12 -07001769 if (output_file_player_) {
1770 output_file_player_->RegisterModuleFileCallback(NULL);
1771 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001772 }
1773
kwiberg5b356f42016-09-08 04:32:33 -07001774 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001775 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001776
kwiberg5a25d952016-08-17 07:31:12 -07001777 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001778 _engineStatisticsPtr->SetLastError(
1779 VE_INVALID_ARGUMENT, kTraceError,
1780 "StartPlayingFileLocally() filePlayer format is not correct");
1781 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001782 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001783
kwiberg55b97fe2016-01-28 05:22:45 -08001784 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001785
kwiberg5a25d952016-08-17 07:31:12 -07001786 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001787 fileName, loop, startPosition, volumeScaling, notificationTime,
1788 stopPosition, (const CodecInst*)codecInst) != 0) {
1789 _engineStatisticsPtr->SetLastError(
1790 VE_BAD_FILE, kTraceError,
1791 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001792 output_file_player_->StopPlayingFile();
1793 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001794 return -1;
1795 }
kwiberg5a25d952016-08-17 07:31:12 -07001796 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001797 channel_state_.SetOutputFilePlaying(true);
1798 }
1799
1800 if (RegisterFilePlayingToMixer() != 0)
1801 return -1;
1802
1803 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001804}
1805
1806int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001807 FileFormats format,
1808 int startPosition,
1809 float volumeScaling,
1810 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001811 const CodecInst* codecInst) {
1812 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1813 "Channel::StartPlayingFileLocally(format=%d,"
1814 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1815 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001816
kwiberg55b97fe2016-01-28 05:22:45 -08001817 if (stream == NULL) {
1818 _engineStatisticsPtr->SetLastError(
1819 VE_BAD_FILE, kTraceError,
1820 "StartPlayingFileLocally() NULL as input stream");
1821 return -1;
1822 }
1823
1824 if (channel_state_.Get().output_file_playing) {
1825 _engineStatisticsPtr->SetLastError(
1826 VE_ALREADY_PLAYING, kTraceError,
1827 "StartPlayingFileLocally() is already playing");
1828 return -1;
1829 }
1830
1831 {
1832 rtc::CritScope cs(&_fileCritSect);
1833
1834 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001835 if (output_file_player_) {
1836 output_file_player_->RegisterModuleFileCallback(NULL);
1837 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001838 }
1839
kwiberg55b97fe2016-01-28 05:22:45 -08001840 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001841 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001842 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001843
kwiberg5a25d952016-08-17 07:31:12 -07001844 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001845 _engineStatisticsPtr->SetLastError(
1846 VE_INVALID_ARGUMENT, kTraceError,
1847 "StartPlayingFileLocally() filePlayer format isnot correct");
1848 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001849 }
1850
kwiberg55b97fe2016-01-28 05:22:45 -08001851 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001852
kwiberg4ec01d92016-08-22 08:43:54 -07001853 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001854 volumeScaling, notificationTime,
1855 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001856 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1857 "StartPlayingFile() failed to "
1858 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001859 output_file_player_->StopPlayingFile();
1860 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001861 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001862 }
kwiberg5a25d952016-08-17 07:31:12 -07001863 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001864 channel_state_.SetOutputFilePlaying(true);
1865 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001866
kwiberg55b97fe2016-01-28 05:22:45 -08001867 if (RegisterFilePlayingToMixer() != 0)
1868 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001869
kwiberg55b97fe2016-01-28 05:22:45 -08001870 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001871}
1872
kwiberg55b97fe2016-01-28 05:22:45 -08001873int Channel::StopPlayingFileLocally() {
1874 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1875 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001876
kwiberg55b97fe2016-01-28 05:22:45 -08001877 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001878 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001879 }
1880
1881 {
1882 rtc::CritScope cs(&_fileCritSect);
1883
kwiberg5a25d952016-08-17 07:31:12 -07001884 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001885 _engineStatisticsPtr->SetLastError(
1886 VE_STOP_RECORDING_FAILED, kTraceError,
1887 "StopPlayingFile() could not stop playing");
1888 return -1;
1889 }
kwiberg5a25d952016-08-17 07:31:12 -07001890 output_file_player_->RegisterModuleFileCallback(NULL);
1891 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001892 channel_state_.SetOutputFilePlaying(false);
1893 }
1894 // _fileCritSect cannot be taken while calling
1895 // SetAnonymousMixibilityStatus. Refer to comments in
1896 // StartPlayingFileLocally(const char* ...) for more details.
1897 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1898 _engineStatisticsPtr->SetLastError(
1899 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1900 "StopPlayingFile() failed to stop participant from playing as"
1901 "file in the mixer");
1902 return -1;
1903 }
1904
1905 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001906}
1907
kwiberg55b97fe2016-01-28 05:22:45 -08001908int Channel::IsPlayingFileLocally() const {
1909 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001910}
1911
kwiberg55b97fe2016-01-28 05:22:45 -08001912int Channel::RegisterFilePlayingToMixer() {
1913 // Return success for not registering for file playing to mixer if:
1914 // 1. playing file before playout is started on that channel.
1915 // 2. starting playout without file playing on that channel.
1916 if (!channel_state_.Get().playing ||
1917 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001918 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001919 }
1920
1921 // |_fileCritSect| cannot be taken while calling
1922 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1923 // frames can be pulled by the mixer. Since the frames are generated from
1924 // the file, _fileCritSect will be taken. This would result in a deadlock.
1925 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1926 channel_state_.SetOutputFilePlaying(false);
1927 rtc::CritScope cs(&_fileCritSect);
1928 _engineStatisticsPtr->SetLastError(
1929 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1930 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001931 output_file_player_->StopPlayingFile();
1932 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001933 return -1;
1934 }
1935
1936 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001937}
1938
niklase@google.com470e71d2011-07-07 08:21:25 +00001939int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001940 bool loop,
1941 FileFormats format,
1942 int startPosition,
1943 float volumeScaling,
1944 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001945 const CodecInst* codecInst) {
1946 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1947 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1948 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1949 "stopPosition=%d)",
1950 fileName, loop, format, volumeScaling, startPosition,
1951 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001952
kwiberg55b97fe2016-01-28 05:22:45 -08001953 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001954
kwiberg55b97fe2016-01-28 05:22:45 -08001955 if (channel_state_.Get().input_file_playing) {
1956 _engineStatisticsPtr->SetLastError(
1957 VE_ALREADY_PLAYING, kTraceWarning,
1958 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001959 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001960 }
1961
1962 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001963 if (input_file_player_) {
1964 input_file_player_->RegisterModuleFileCallback(NULL);
1965 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001966 }
1967
1968 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001969 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001970 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001971
kwiberg5a25d952016-08-17 07:31:12 -07001972 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001973 _engineStatisticsPtr->SetLastError(
1974 VE_INVALID_ARGUMENT, kTraceError,
1975 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1976 return -1;
1977 }
1978
1979 const uint32_t notificationTime(0);
1980
kwiberg5a25d952016-08-17 07:31:12 -07001981 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001982 fileName, loop, startPosition, volumeScaling, notificationTime,
1983 stopPosition, (const CodecInst*)codecInst) != 0) {
1984 _engineStatisticsPtr->SetLastError(
1985 VE_BAD_FILE, kTraceError,
1986 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001987 input_file_player_->StopPlayingFile();
1988 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001989 return -1;
1990 }
kwiberg5a25d952016-08-17 07:31:12 -07001991 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001992 channel_state_.SetInputFilePlaying(true);
1993
1994 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001995}
1996
1997int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001998 FileFormats format,
1999 int startPosition,
2000 float volumeScaling,
2001 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002002 const CodecInst* codecInst) {
2003 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2004 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2005 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2006 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002007
kwiberg55b97fe2016-01-28 05:22:45 -08002008 if (stream == NULL) {
2009 _engineStatisticsPtr->SetLastError(
2010 VE_BAD_FILE, kTraceError,
2011 "StartPlayingFileAsMicrophone NULL as input stream");
2012 return -1;
2013 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002014
kwiberg55b97fe2016-01-28 05:22:45 -08002015 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002016
kwiberg55b97fe2016-01-28 05:22:45 -08002017 if (channel_state_.Get().input_file_playing) {
2018 _engineStatisticsPtr->SetLastError(
2019 VE_ALREADY_PLAYING, kTraceWarning,
2020 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002021 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002022 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002023
kwiberg55b97fe2016-01-28 05:22:45 -08002024 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002025 if (input_file_player_) {
2026 input_file_player_->RegisterModuleFileCallback(NULL);
2027 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002028 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002029
kwiberg55b97fe2016-01-28 05:22:45 -08002030 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002031 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002032 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002033
kwiberg5a25d952016-08-17 07:31:12 -07002034 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002035 _engineStatisticsPtr->SetLastError(
2036 VE_INVALID_ARGUMENT, kTraceError,
2037 "StartPlayingInputFile() filePlayer format isnot correct");
2038 return -1;
2039 }
2040
2041 const uint32_t notificationTime(0);
2042
kwiberg4ec01d92016-08-22 08:43:54 -07002043 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2044 notificationTime, stopPosition,
2045 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002046 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2047 "StartPlayingFile() failed to start "
2048 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002049 input_file_player_->StopPlayingFile();
2050 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002051 return -1;
2052 }
2053
kwiberg5a25d952016-08-17 07:31:12 -07002054 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002055 channel_state_.SetInputFilePlaying(true);
2056
2057 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002058}
2059
kwiberg55b97fe2016-01-28 05:22:45 -08002060int Channel::StopPlayingFileAsMicrophone() {
2061 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2062 "Channel::StopPlayingFileAsMicrophone()");
2063
2064 rtc::CritScope cs(&_fileCritSect);
2065
2066 if (!channel_state_.Get().input_file_playing) {
2067 return 0;
2068 }
2069
kwiberg5a25d952016-08-17 07:31:12 -07002070 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002071 _engineStatisticsPtr->SetLastError(
2072 VE_STOP_RECORDING_FAILED, kTraceError,
2073 "StopPlayingFile() could not stop playing");
2074 return -1;
2075 }
kwiberg5a25d952016-08-17 07:31:12 -07002076 input_file_player_->RegisterModuleFileCallback(NULL);
2077 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002078 channel_state_.SetInputFilePlaying(false);
2079
2080 return 0;
2081}
2082
2083int Channel::IsPlayingFileAsMicrophone() const {
2084 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002085}
2086
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002087int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002088 const CodecInst* codecInst) {
2089 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2090 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002091
kwiberg55b97fe2016-01-28 05:22:45 -08002092 if (_outputFileRecording) {
2093 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2094 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002095 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002096 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002097
kwiberg55b97fe2016-01-28 05:22:45 -08002098 FileFormats format;
2099 const uint32_t notificationTime(0); // Not supported in VoE
2100 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002101
kwiberg55b97fe2016-01-28 05:22:45 -08002102 if ((codecInst != NULL) &&
2103 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2104 _engineStatisticsPtr->SetLastError(
2105 VE_BAD_ARGUMENT, kTraceError,
2106 "StartRecordingPlayout() invalid compression");
2107 return (-1);
2108 }
2109 if (codecInst == NULL) {
2110 format = kFileFormatPcm16kHzFile;
2111 codecInst = &dummyCodec;
2112 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2113 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2114 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2115 format = kFileFormatWavFile;
2116 } else {
2117 format = kFileFormatCompressedFile;
2118 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002119
kwiberg55b97fe2016-01-28 05:22:45 -08002120 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002121
kwiberg55b97fe2016-01-28 05:22:45 -08002122 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002123 if (output_file_recorder_) {
2124 output_file_recorder_->RegisterModuleFileCallback(NULL);
2125 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002126 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002127
kwiberg5a25d952016-08-17 07:31:12 -07002128 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002129 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002130 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002131 _engineStatisticsPtr->SetLastError(
2132 VE_INVALID_ARGUMENT, kTraceError,
2133 "StartRecordingPlayout() fileRecorder format isnot correct");
2134 return -1;
2135 }
2136
kwiberg5a25d952016-08-17 07:31:12 -07002137 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002138 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2139 _engineStatisticsPtr->SetLastError(
2140 VE_BAD_FILE, kTraceError,
2141 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002142 output_file_recorder_->StopRecording();
2143 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002144 return -1;
2145 }
kwiberg5a25d952016-08-17 07:31:12 -07002146 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002147 _outputFileRecording = true;
2148
2149 return 0;
2150}
2151
2152int Channel::StartRecordingPlayout(OutStream* stream,
2153 const CodecInst* codecInst) {
2154 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2155 "Channel::StartRecordingPlayout()");
2156
2157 if (_outputFileRecording) {
2158 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2159 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002160 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002161 }
2162
2163 FileFormats format;
2164 const uint32_t notificationTime(0); // Not supported in VoE
2165 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2166
2167 if (codecInst != NULL && codecInst->channels != 1) {
2168 _engineStatisticsPtr->SetLastError(
2169 VE_BAD_ARGUMENT, kTraceError,
2170 "StartRecordingPlayout() invalid compression");
2171 return (-1);
2172 }
2173 if (codecInst == NULL) {
2174 format = kFileFormatPcm16kHzFile;
2175 codecInst = &dummyCodec;
2176 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2177 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2178 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2179 format = kFileFormatWavFile;
2180 } else {
2181 format = kFileFormatCompressedFile;
2182 }
2183
2184 rtc::CritScope cs(&_fileCritSect);
2185
2186 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002187 if (output_file_recorder_) {
2188 output_file_recorder_->RegisterModuleFileCallback(NULL);
2189 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002190 }
2191
kwiberg5a25d952016-08-17 07:31:12 -07002192 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002193 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002194 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002195 _engineStatisticsPtr->SetLastError(
2196 VE_INVALID_ARGUMENT, kTraceError,
2197 "StartRecordingPlayout() fileRecorder format isnot correct");
2198 return -1;
2199 }
2200
kwiberg4ec01d92016-08-22 08:43:54 -07002201 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002202 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002203 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2204 "StartRecordingPlayout() failed to "
2205 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002206 output_file_recorder_->StopRecording();
2207 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002208 return -1;
2209 }
2210
kwiberg5a25d952016-08-17 07:31:12 -07002211 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002212 _outputFileRecording = true;
2213
2214 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002215}
2216
kwiberg55b97fe2016-01-28 05:22:45 -08002217int Channel::StopRecordingPlayout() {
2218 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2219 "Channel::StopRecordingPlayout()");
2220
2221 if (!_outputFileRecording) {
2222 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2223 "StopRecordingPlayout() isnot recording");
2224 return -1;
2225 }
2226
2227 rtc::CritScope cs(&_fileCritSect);
2228
kwiberg5a25d952016-08-17 07:31:12 -07002229 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002230 _engineStatisticsPtr->SetLastError(
2231 VE_STOP_RECORDING_FAILED, kTraceError,
2232 "StopRecording() could not stop recording");
2233 return (-1);
2234 }
kwiberg5a25d952016-08-17 07:31:12 -07002235 output_file_recorder_->RegisterModuleFileCallback(NULL);
2236 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002237 _outputFileRecording = false;
2238
2239 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002240}
2241
kwiberg55b97fe2016-01-28 05:22:45 -08002242void Channel::SetMixWithMicStatus(bool mix) {
2243 rtc::CritScope cs(&_fileCritSect);
2244 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002245}
2246
kwiberg55b97fe2016-01-28 05:22:45 -08002247int Channel::GetSpeechOutputLevel(uint32_t& level) const {
2248 int8_t currentLevel = _outputAudioLevel.Level();
2249 level = static_cast<int32_t>(currentLevel);
2250 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002251}
2252
kwiberg55b97fe2016-01-28 05:22:45 -08002253int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const {
2254 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2255 level = static_cast<int32_t>(currentLevel);
2256 return 0;
2257}
2258
solenberg1c2af8e2016-03-24 10:36:00 -07002259int Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002260 rtc::CritScope cs(&volume_settings_critsect_);
2261 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002262 "Channel::SetMute(enable=%d)", enable);
solenberg1c2af8e2016-03-24 10:36:00 -07002263 input_mute_ = enable;
kwiberg55b97fe2016-01-28 05:22:45 -08002264 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002265}
2266
solenberg1c2af8e2016-03-24 10:36:00 -07002267bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002268 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002269 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002270}
2271
kwiberg55b97fe2016-01-28 05:22:45 -08002272int Channel::SetOutputVolumePan(float left, float right) {
2273 rtc::CritScope cs(&volume_settings_critsect_);
2274 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002275 "Channel::SetOutputVolumePan()");
kwiberg55b97fe2016-01-28 05:22:45 -08002276 _panLeft = left;
2277 _panRight = right;
2278 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002279}
2280
kwiberg55b97fe2016-01-28 05:22:45 -08002281int Channel::GetOutputVolumePan(float& left, float& right) const {
2282 rtc::CritScope cs(&volume_settings_critsect_);
2283 left = _panLeft;
2284 right = _panRight;
2285 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002286}
2287
kwiberg55b97fe2016-01-28 05:22:45 -08002288int Channel::SetChannelOutputVolumeScaling(float scaling) {
2289 rtc::CritScope cs(&volume_settings_critsect_);
2290 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002291 "Channel::SetChannelOutputVolumeScaling()");
kwiberg55b97fe2016-01-28 05:22:45 -08002292 _outputGain = scaling;
2293 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002294}
2295
kwiberg55b97fe2016-01-28 05:22:45 -08002296int Channel::GetChannelOutputVolumeScaling(float& scaling) const {
2297 rtc::CritScope cs(&volume_settings_critsect_);
2298 scaling = _outputGain;
2299 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002300}
2301
solenberg8842c3e2016-03-11 03:06:41 -08002302int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002303 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002304 "Channel::SendTelephoneEventOutband(...)");
2305 RTC_DCHECK_LE(0, event);
2306 RTC_DCHECK_GE(255, event);
2307 RTC_DCHECK_LE(0, duration_ms);
2308 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002309 if (!Sending()) {
2310 return -1;
2311 }
solenberg8842c3e2016-03-11 03:06:41 -08002312 if (_rtpRtcpModule->SendTelephoneEventOutband(
2313 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002314 _engineStatisticsPtr->SetLastError(
2315 VE_SEND_DTMF_FAILED, kTraceWarning,
2316 "SendTelephoneEventOutband() failed to send event");
2317 return -1;
2318 }
2319 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002320}
2321
solenberg31642aa2016-03-14 08:00:37 -07002322int Channel::SetSendTelephoneEventPayloadType(int payload_type) {
kwiberg55b97fe2016-01-28 05:22:45 -08002323 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002324 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002325 RTC_DCHECK_LE(0, payload_type);
2326 RTC_DCHECK_GE(127, payload_type);
2327 CodecInst codec = {0};
kwiberg55b97fe2016-01-28 05:22:45 -08002328 codec.plfreq = 8000;
solenberg31642aa2016-03-14 08:00:37 -07002329 codec.pltype = payload_type;
kwiberg55b97fe2016-01-28 05:22:45 -08002330 memcpy(codec.plname, "telephone-event", 16);
2331 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2332 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2333 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2334 _engineStatisticsPtr->SetLastError(
2335 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2336 "SetSendTelephoneEventPayloadType() failed to register send"
2337 "payload type");
2338 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002339 }
kwiberg55b97fe2016-01-28 05:22:45 -08002340 }
kwiberg55b97fe2016-01-28 05:22:45 -08002341 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002342}
2343
kwiberg55b97fe2016-01-28 05:22:45 -08002344int Channel::VoiceActivityIndicator(int& activity) {
2345 activity = _sendFrameType;
2346 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002347}
2348
kwiberg55b97fe2016-01-28 05:22:45 -08002349int Channel::SetLocalSSRC(unsigned int ssrc) {
2350 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2351 "Channel::SetLocalSSRC()");
2352 if (channel_state_.Get().sending) {
2353 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2354 "SetLocalSSRC() already sending");
2355 return -1;
2356 }
2357 _rtpRtcpModule->SetSSRC(ssrc);
2358 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002359}
2360
kwiberg55b97fe2016-01-28 05:22:45 -08002361int Channel::GetLocalSSRC(unsigned int& ssrc) {
2362 ssrc = _rtpRtcpModule->SSRC();
2363 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002364}
2365
kwiberg55b97fe2016-01-28 05:22:45 -08002366int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2367 ssrc = rtp_receiver_->SSRC();
2368 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002369}
2370
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002371int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002372 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002373 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002374}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002375
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002376int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2377 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002378 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2379 if (enable &&
2380 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2381 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002382 return -1;
2383 }
2384 return 0;
2385}
2386
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002387int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2388 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2389}
2390
2391int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2392 rtp_header_parser_->DeregisterRtpHeaderExtension(
2393 kRtpExtensionAbsoluteSendTime);
kwiberg55b97fe2016-01-28 05:22:45 -08002394 if (enable &&
2395 !rtp_header_parser_->RegisterRtpHeaderExtension(
2396 kRtpExtensionAbsoluteSendTime, id)) {
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002397 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002398 }
2399 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002400}
2401
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002402void Channel::EnableSendTransportSequenceNumber(int id) {
2403 int ret =
2404 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2405 RTC_DCHECK_EQ(0, ret);
2406}
2407
stefan3313ec92016-01-21 06:32:43 -08002408void Channel::EnableReceiveTransportSequenceNumber(int id) {
2409 rtp_header_parser_->DeregisterRtpHeaderExtension(
2410 kRtpExtensionTransportSequenceNumber);
2411 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2412 kRtpExtensionTransportSequenceNumber, id);
2413 RTC_DCHECK(ret);
2414}
2415
stefanbba9dec2016-02-01 04:39:55 -08002416void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002417 RtpPacketSender* rtp_packet_sender,
2418 TransportFeedbackObserver* transport_feedback_observer,
2419 PacketRouter* packet_router) {
stefanbba9dec2016-02-01 04:39:55 -08002420 RTC_DCHECK(rtp_packet_sender);
2421 RTC_DCHECK(transport_feedback_observer);
2422 RTC_DCHECK(packet_router && !packet_router_);
2423 feedback_observer_proxy_->SetTransportFeedbackObserver(
2424 transport_feedback_observer);
2425 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2426 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2427 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002428 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002429 packet_router_ = packet_router;
2430}
2431
stefanbba9dec2016-02-01 04:39:55 -08002432void Channel::RegisterReceiverCongestionControlObjects(
2433 PacketRouter* packet_router) {
2434 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002435 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002436 packet_router_ = packet_router;
2437}
2438
2439void Channel::ResetCongestionControlObjects() {
2440 RTC_DCHECK(packet_router_);
2441 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
2442 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2443 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002444 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002445 packet_router_ = nullptr;
2446 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2447}
2448
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002449void Channel::SetRTCPStatus(bool enable) {
2450 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2451 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002452 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002453}
2454
kwiberg55b97fe2016-01-28 05:22:45 -08002455int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002456 RtcpMode method = _rtpRtcpModule->RTCP();
2457 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002458 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002459}
2460
kwiberg55b97fe2016-01-28 05:22:45 -08002461int Channel::SetRTCP_CNAME(const char cName[256]) {
2462 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2463 "Channel::SetRTCP_CNAME()");
2464 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2465 _engineStatisticsPtr->SetLastError(
2466 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2467 "SetRTCP_CNAME() failed to set RTCP CNAME");
2468 return -1;
2469 }
2470 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002471}
2472
kwiberg55b97fe2016-01-28 05:22:45 -08002473int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2474 if (cName == NULL) {
2475 _engineStatisticsPtr->SetLastError(
2476 VE_INVALID_ARGUMENT, kTraceError,
2477 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2478 return -1;
2479 }
2480 char cname[RTCP_CNAME_SIZE];
2481 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2482 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2483 _engineStatisticsPtr->SetLastError(
2484 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2485 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2486 return -1;
2487 }
2488 strcpy(cName, cname);
2489 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002490}
2491
kwiberg55b97fe2016-01-28 05:22:45 -08002492int Channel::GetRemoteRTCPData(unsigned int& NTPHigh,
2493 unsigned int& NTPLow,
2494 unsigned int& timestamp,
2495 unsigned int& playoutTimestamp,
2496 unsigned int* jitter,
2497 unsigned short* fractionLost) {
2498 // --- Information from sender info in received Sender Reports
niklase@google.com470e71d2011-07-07 08:21:25 +00002499
kwiberg55b97fe2016-01-28 05:22:45 -08002500 RTCPSenderInfo senderInfo;
2501 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) {
2502 _engineStatisticsPtr->SetLastError(
2503 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2504 "GetRemoteRTCPData() failed to retrieve sender info for remote "
2505 "side");
2506 return -1;
2507 }
2508
2509 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2510 // and octet count)
2511 NTPHigh = senderInfo.NTPseconds;
2512 NTPLow = senderInfo.NTPfraction;
2513 timestamp = senderInfo.RTPtimeStamp;
2514
2515 // --- Locally derived information
2516
2517 // This value is updated on each incoming RTCP packet (0 when no packet
2518 // has been received)
2519 playoutTimestamp = playout_timestamp_rtcp_;
2520
2521 if (NULL != jitter || NULL != fractionLost) {
2522 // Get all RTCP receiver report blocks that have been received on this
2523 // channel. If we receive RTP packets from a remote source we know the
2524 // remote SSRC and use the report block from him.
2525 // Otherwise use the first report block.
2526 std::vector<RTCPReportBlock> remote_stats;
2527 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
2528 remote_stats.empty()) {
2529 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2530 "GetRemoteRTCPData() failed to measure statistics due"
2531 " to lack of received RTP and/or RTCP packets");
2532 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002533 }
2534
kwiberg55b97fe2016-01-28 05:22:45 -08002535 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2536 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2537 for (; it != remote_stats.end(); ++it) {
2538 if (it->remoteSSRC == remoteSSRC)
2539 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002540 }
kwiberg55b97fe2016-01-28 05:22:45 -08002541
2542 if (it == remote_stats.end()) {
2543 // If we have not received any RTCP packets from this SSRC it probably
2544 // means that we have not received any RTP packets.
2545 // Use the first received report block instead.
2546 it = remote_stats.begin();
2547 remoteSSRC = it->remoteSSRC;
2548 }
2549
2550 if (jitter) {
2551 *jitter = it->jitter;
2552 }
2553
2554 if (fractionLost) {
2555 *fractionLost = it->fractionLost;
2556 }
2557 }
2558 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002559}
2560
kwiberg55b97fe2016-01-28 05:22:45 -08002561int Channel::SendApplicationDefinedRTCPPacket(
2562 unsigned char subType,
2563 unsigned int name,
2564 const char* data,
2565 unsigned short dataLengthInBytes) {
2566 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2567 "Channel::SendApplicationDefinedRTCPPacket()");
2568 if (!channel_state_.Get().sending) {
2569 _engineStatisticsPtr->SetLastError(
2570 VE_NOT_SENDING, kTraceError,
2571 "SendApplicationDefinedRTCPPacket() not sending");
2572 return -1;
2573 }
2574 if (NULL == data) {
2575 _engineStatisticsPtr->SetLastError(
2576 VE_INVALID_ARGUMENT, kTraceError,
2577 "SendApplicationDefinedRTCPPacket() invalid data value");
2578 return -1;
2579 }
2580 if (dataLengthInBytes % 4 != 0) {
2581 _engineStatisticsPtr->SetLastError(
2582 VE_INVALID_ARGUMENT, kTraceError,
2583 "SendApplicationDefinedRTCPPacket() invalid length value");
2584 return -1;
2585 }
2586 RtcpMode status = _rtpRtcpModule->RTCP();
2587 if (status == RtcpMode::kOff) {
2588 _engineStatisticsPtr->SetLastError(
2589 VE_RTCP_ERROR, kTraceError,
2590 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2591 return -1;
2592 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002593
kwiberg55b97fe2016-01-28 05:22:45 -08002594 // Create and schedule the RTCP APP packet for transmission
2595 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2596 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2597 _engineStatisticsPtr->SetLastError(
2598 VE_SEND_ERROR, kTraceError,
2599 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2600 return -1;
2601 }
2602 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002603}
2604
kwiberg55b97fe2016-01-28 05:22:45 -08002605int Channel::GetRTPStatistics(unsigned int& averageJitterMs,
2606 unsigned int& maxJitterMs,
2607 unsigned int& discardedPackets) {
2608 // The jitter statistics is updated for each received RTP packet and is
2609 // based on received packets.
2610 if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) {
2611 // If RTCP is off, there is no timed thread in the RTCP module regularly
2612 // generating new stats, trigger the update manually here instead.
2613 StreamStatistician* statistician =
2614 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2615 if (statistician) {
2616 // Don't use returned statistics, use data from proxy instead so that
2617 // max jitter can be fetched atomically.
2618 RtcpStatistics s;
2619 statistician->GetStatistics(&s, true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002620 }
kwiberg55b97fe2016-01-28 05:22:45 -08002621 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002622
kwiberg55b97fe2016-01-28 05:22:45 -08002623 ChannelStatistics stats = statistics_proxy_->GetStats();
2624 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
2625 if (playoutFrequency > 0) {
2626 // Scale RTP statistics given the current playout frequency
2627 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2628 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
2629 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002630
kwiberg55b97fe2016-01-28 05:22:45 -08002631 discardedPackets = _numberOfDiscardedPackets;
niklase@google.com470e71d2011-07-07 08:21:25 +00002632
kwiberg55b97fe2016-01-28 05:22:45 -08002633 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002634}
2635
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002636int Channel::GetRemoteRTCPReportBlocks(
2637 std::vector<ReportBlock>* report_blocks) {
2638 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002639 _engineStatisticsPtr->SetLastError(
2640 VE_INVALID_ARGUMENT, kTraceError,
2641 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002642 return -1;
2643 }
2644
2645 // Get the report blocks from the latest received RTCP Sender or Receiver
2646 // Report. Each element in the vector contains the sender's SSRC and a
2647 // report block according to RFC 3550.
2648 std::vector<RTCPReportBlock> rtcp_report_blocks;
2649 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002650 return -1;
2651 }
2652
2653 if (rtcp_report_blocks.empty())
2654 return 0;
2655
2656 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2657 for (; it != rtcp_report_blocks.end(); ++it) {
2658 ReportBlock report_block;
2659 report_block.sender_SSRC = it->remoteSSRC;
2660 report_block.source_SSRC = it->sourceSSRC;
2661 report_block.fraction_lost = it->fractionLost;
2662 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2663 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2664 report_block.interarrival_jitter = it->jitter;
2665 report_block.last_SR_timestamp = it->lastSR;
2666 report_block.delay_since_last_SR = it->delaySinceLastSR;
2667 report_blocks->push_back(report_block);
2668 }
2669 return 0;
2670}
2671
kwiberg55b97fe2016-01-28 05:22:45 -08002672int Channel::GetRTPStatistics(CallStatistics& stats) {
2673 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002674
kwiberg55b97fe2016-01-28 05:22:45 -08002675 // The jitter statistics is updated for each received RTP packet and is
2676 // based on received packets.
2677 RtcpStatistics statistics;
2678 StreamStatistician* statistician =
2679 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002680 if (statistician) {
2681 statistician->GetStatistics(&statistics,
2682 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002683 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002684
kwiberg55b97fe2016-01-28 05:22:45 -08002685 stats.fractionLost = statistics.fraction_lost;
2686 stats.cumulativeLost = statistics.cumulative_lost;
2687 stats.extendedMax = statistics.extended_max_sequence_number;
2688 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002689
kwiberg55b97fe2016-01-28 05:22:45 -08002690 // --- RTT
2691 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002692
kwiberg55b97fe2016-01-28 05:22:45 -08002693 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002694
kwiberg55b97fe2016-01-28 05:22:45 -08002695 size_t bytesSent(0);
2696 uint32_t packetsSent(0);
2697 size_t bytesReceived(0);
2698 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002699
kwiberg55b97fe2016-01-28 05:22:45 -08002700 if (statistician) {
2701 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2702 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002703
kwiberg55b97fe2016-01-28 05:22:45 -08002704 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2705 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2706 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2707 " output will not be complete");
2708 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002709
kwiberg55b97fe2016-01-28 05:22:45 -08002710 stats.bytesSent = bytesSent;
2711 stats.packetsSent = packetsSent;
2712 stats.bytesReceived = bytesReceived;
2713 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002714
kwiberg55b97fe2016-01-28 05:22:45 -08002715 // --- Timestamps
2716 {
2717 rtc::CritScope lock(&ts_stats_lock_);
2718 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2719 }
2720 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002721}
2722
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002723int Channel::SetCodecFECStatus(bool enable) {
2724 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2725 "Channel::SetCodecFECStatus()");
2726
kwibergc8d071e2016-04-06 12:22:38 -07002727 if (!codec_manager_.SetCodecFEC(enable) ||
2728 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002729 _engineStatisticsPtr->SetLastError(
2730 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2731 "SetCodecFECStatus() failed to set FEC state");
2732 return -1;
2733 }
2734 return 0;
2735}
2736
2737bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002738 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002739}
2740
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002741void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2742 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002743 // If pacing is enabled we always store packets.
2744 if (!pacing_enabled_)
2745 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002746 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002747 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002748 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002749 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002750 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002751}
2752
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002753// Called when we are missing one or more packets.
2754int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002755 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2756}
2757
kwiberg55b97fe2016-01-28 05:22:45 -08002758uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2759 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2760 "Channel::Demultiplex()");
2761 _audioFrame.CopyFrom(audioFrame);
2762 _audioFrame.id_ = _channelId;
2763 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002764}
2765
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002766void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002767 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002768 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002769 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002770 CodecInst codec;
2771 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002772
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002773 // Never upsample or upmix the capture signal here. This should be done at the
2774 // end of the send chain.
2775 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2776 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2777 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2778 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002779}
2780
kwiberg55b97fe2016-01-28 05:22:45 -08002781uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2782 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2783 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002784
kwiberg55b97fe2016-01-28 05:22:45 -08002785 if (_audioFrame.samples_per_channel_ == 0) {
2786 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2787 "Channel::PrepareEncodeAndSend() invalid audio frame");
2788 return 0xFFFFFFFF;
2789 }
2790
2791 if (channel_state_.Get().input_file_playing) {
2792 MixOrReplaceAudioWithFile(mixingFrequency);
2793 }
2794
solenberg1c2af8e2016-03-24 10:36:00 -07002795 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2796 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002797
2798 if (channel_state_.Get().input_external_media) {
2799 rtc::CritScope cs(&_callbackCritSect);
2800 const bool isStereo = (_audioFrame.num_channels_ == 2);
2801 if (_inputExternalMediaCallbackPtr) {
2802 _inputExternalMediaCallbackPtr->Process(
2803 _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_,
2804 _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_,
2805 isStereo);
niklase@google.com470e71d2011-07-07 08:21:25 +00002806 }
kwiberg55b97fe2016-01-28 05:22:45 -08002807 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002808
kwiberg55b97fe2016-01-28 05:22:45 -08002809 if (_includeAudioLevelIndication) {
2810 size_t length =
2811 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002812 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002813 if (is_muted && previous_frame_muted_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002814 rms_level_.ProcessMuted(length);
2815 } else {
2816 rms_level_.Process(_audioFrame.data_, length);
niklase@google.com470e71d2011-07-07 08:21:25 +00002817 }
kwiberg55b97fe2016-01-28 05:22:45 -08002818 }
solenberg1c2af8e2016-03-24 10:36:00 -07002819 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002820
kwiberg55b97fe2016-01-28 05:22:45 -08002821 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002822}
2823
kwiberg55b97fe2016-01-28 05:22:45 -08002824uint32_t Channel::EncodeAndSend() {
2825 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2826 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002827
kwiberg55b97fe2016-01-28 05:22:45 -08002828 assert(_audioFrame.num_channels_ <= 2);
2829 if (_audioFrame.samples_per_channel_ == 0) {
2830 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2831 "Channel::EncodeAndSend() invalid audio frame");
2832 return 0xFFFFFFFF;
2833 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002834
kwiberg55b97fe2016-01-28 05:22:45 -08002835 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002836
kwiberg55b97fe2016-01-28 05:22:45 -08002837 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002838
kwiberg55b97fe2016-01-28 05:22:45 -08002839 // The ACM resamples internally.
2840 _audioFrame.timestamp_ = _timeStamp;
2841 // This call will trigger AudioPacketizationCallback::SendData if encoding
2842 // is done and payload is ready for packetization and transmission.
2843 // Otherwise, it will return without invoking the callback.
2844 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2845 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2846 "Channel::EncodeAndSend() ACM encoding failed");
2847 return 0xFFFFFFFF;
2848 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002849
kwiberg55b97fe2016-01-28 05:22:45 -08002850 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2851 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002852}
2853
Minyue2013aec2015-05-13 14:14:42 +02002854void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002855 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002856 Channel* channel = associate_send_channel_.channel();
2857 if (channel && channel->ChannelId() == channel_id) {
2858 // If this channel is associated with a send channel of the specified
2859 // Channel ID, disassociate with it.
2860 ChannelOwner ref(NULL);
2861 associate_send_channel_ = ref;
2862 }
2863}
2864
ivoc14d5dbe2016-07-04 07:06:55 -07002865void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2866 event_log_proxy_->SetEventLog(event_log);
2867}
2868
kwiberg55b97fe2016-01-28 05:22:45 -08002869int Channel::RegisterExternalMediaProcessing(ProcessingTypes type,
2870 VoEMediaProcess& processObject) {
2871 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2872 "Channel::RegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002873
kwiberg55b97fe2016-01-28 05:22:45 -08002874 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002875
kwiberg55b97fe2016-01-28 05:22:45 -08002876 if (kPlaybackPerChannel == type) {
2877 if (_outputExternalMediaCallbackPtr) {
2878 _engineStatisticsPtr->SetLastError(
2879 VE_INVALID_OPERATION, kTraceError,
2880 "Channel::RegisterExternalMediaProcessing() "
2881 "output external media already enabled");
2882 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002883 }
kwiberg55b97fe2016-01-28 05:22:45 -08002884 _outputExternalMediaCallbackPtr = &processObject;
2885 _outputExternalMedia = true;
2886 } else if (kRecordingPerChannel == type) {
2887 if (_inputExternalMediaCallbackPtr) {
2888 _engineStatisticsPtr->SetLastError(
2889 VE_INVALID_OPERATION, kTraceError,
2890 "Channel::RegisterExternalMediaProcessing() "
2891 "output external media already enabled");
2892 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002893 }
kwiberg55b97fe2016-01-28 05:22:45 -08002894 _inputExternalMediaCallbackPtr = &processObject;
2895 channel_state_.SetInputExternalMedia(true);
2896 }
2897 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002898}
2899
kwiberg55b97fe2016-01-28 05:22:45 -08002900int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) {
2901 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2902 "Channel::DeRegisterExternalMediaProcessing()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002903
kwiberg55b97fe2016-01-28 05:22:45 -08002904 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002905
kwiberg55b97fe2016-01-28 05:22:45 -08002906 if (kPlaybackPerChannel == type) {
2907 if (!_outputExternalMediaCallbackPtr) {
2908 _engineStatisticsPtr->SetLastError(
2909 VE_INVALID_OPERATION, kTraceWarning,
2910 "Channel::DeRegisterExternalMediaProcessing() "
2911 "output external media already disabled");
2912 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002913 }
kwiberg55b97fe2016-01-28 05:22:45 -08002914 _outputExternalMedia = false;
2915 _outputExternalMediaCallbackPtr = NULL;
2916 } else if (kRecordingPerChannel == type) {
2917 if (!_inputExternalMediaCallbackPtr) {
2918 _engineStatisticsPtr->SetLastError(
2919 VE_INVALID_OPERATION, kTraceWarning,
2920 "Channel::DeRegisterExternalMediaProcessing() "
2921 "input external media already disabled");
2922 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002923 }
kwiberg55b97fe2016-01-28 05:22:45 -08002924 channel_state_.SetInputExternalMedia(false);
2925 _inputExternalMediaCallbackPtr = NULL;
2926 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002927
kwiberg55b97fe2016-01-28 05:22:45 -08002928 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002929}
2930
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002931int Channel::SetExternalMixing(bool enabled) {
kwiberg55b97fe2016-01-28 05:22:45 -08002932 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2933 "Channel::SetExternalMixing(enabled=%d)", enabled);
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002934
kwiberg55b97fe2016-01-28 05:22:45 -08002935 if (channel_state_.Get().playing) {
2936 _engineStatisticsPtr->SetLastError(
2937 VE_INVALID_OPERATION, kTraceError,
2938 "Channel::SetExternalMixing() "
2939 "external mixing cannot be changed while playing.");
2940 return -1;
2941 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002942
kwiberg55b97fe2016-01-28 05:22:45 -08002943 _externalMixing = enabled;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002944
kwiberg55b97fe2016-01-28 05:22:45 -08002945 return 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +00002946}
2947
kwiberg55b97fe2016-01-28 05:22:45 -08002948int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2949 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002950}
2951
wu@webrtc.org24301a62013-12-13 19:17:43 +00002952void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2953 audio_coding_->GetDecodingCallStatistics(stats);
2954}
2955
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002956bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
2957 int* playout_buffer_delay_ms) const {
tommi31fc21f2016-01-21 10:37:37 -08002958 rtc::CritScope lock(&video_sync_lock_);
henrik.lundinb3f1c5d2016-08-22 15:39:53 -07002959 *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002960 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002961 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002962}
2963
solenberg358057b2015-11-27 10:46:42 -08002964uint32_t Channel::GetDelayEstimate() const {
2965 int jitter_buffer_delay_ms = 0;
2966 int playout_buffer_delay_ms = 0;
2967 GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms);
2968 return jitter_buffer_delay_ms + playout_buffer_delay_ms;
2969}
2970
deadbeef74375882015-08-13 12:09:10 -07002971int Channel::LeastRequiredDelayMs() const {
2972 return audio_coding_->LeastRequiredDelayMs();
2973}
2974
kwiberg55b97fe2016-01-28 05:22:45 -08002975int Channel::SetMinimumPlayoutDelay(int delayMs) {
2976 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2977 "Channel::SetMinimumPlayoutDelay()");
2978 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2979 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2980 _engineStatisticsPtr->SetLastError(
2981 VE_INVALID_ARGUMENT, kTraceError,
2982 "SetMinimumPlayoutDelay() invalid min delay");
2983 return -1;
2984 }
2985 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2986 _engineStatisticsPtr->SetLastError(
2987 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2988 "SetMinimumPlayoutDelay() failed to set min playout delay");
2989 return -1;
2990 }
2991 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002992}
2993
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002994int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002995 uint32_t playout_timestamp_rtp = 0;
2996 {
tommi31fc21f2016-01-21 10:37:37 -08002997 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002998 playout_timestamp_rtp = playout_timestamp_rtp_;
2999 }
kwiberg55b97fe2016-01-28 05:22:45 -08003000 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003001 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07003002 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003003 "GetPlayoutTimestamp() failed to retrieve timestamp");
3004 return -1;
3005 }
deadbeef74375882015-08-13 12:09:10 -07003006 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003007 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003008}
3009
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003010int Channel::SetInitTimestamp(unsigned int timestamp) {
3011 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003012 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003013 if (channel_state_.Get().sending) {
3014 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3015 "SetInitTimestamp() already sending");
3016 return -1;
3017 }
3018 _rtpRtcpModule->SetStartTimestamp(timestamp);
3019 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003020}
3021
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003022int Channel::SetInitSequenceNumber(short sequenceNumber) {
3023 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3024 "Channel::SetInitSequenceNumber()");
3025 if (channel_state_.Get().sending) {
3026 _engineStatisticsPtr->SetLastError(
3027 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3028 return -1;
3029 }
3030 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3031 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003032}
3033
kwiberg55b97fe2016-01-28 05:22:45 -08003034int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
3035 RtpReceiver** rtp_receiver) const {
3036 *rtpRtcpModule = _rtpRtcpModule.get();
3037 *rtp_receiver = rtp_receiver_.get();
3038 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003039}
3040
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003041// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3042// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08003043int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08003044 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08003045 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003046
kwiberg55b97fe2016-01-28 05:22:45 -08003047 {
3048 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003049
kwiberg5a25d952016-08-17 07:31:12 -07003050 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003051 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3052 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3053 " doesnt exist");
3054 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003055 }
3056
kwiberg4ec01d92016-08-22 08:43:54 -07003057 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07003058 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003059 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3060 "Channel::MixOrReplaceAudioWithFile() file mixing "
3061 "failed");
3062 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003063 }
kwiberg55b97fe2016-01-28 05:22:45 -08003064 if (fileSamples == 0) {
3065 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3066 "Channel::MixOrReplaceAudioWithFile() file is ended");
3067 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003068 }
kwiberg55b97fe2016-01-28 05:22:45 -08003069 }
3070
3071 assert(_audioFrame.samples_per_channel_ == fileSamples);
3072
3073 if (_mixFileWithMicrophone) {
3074 // Currently file stream is always mono.
3075 // TODO(xians): Change the code when FilePlayer supports real stereo.
3076 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
3077 1, fileSamples);
3078 } else {
3079 // Replace ACM audio with file.
3080 // Currently file stream is always mono.
3081 // TODO(xians): Change the code when FilePlayer supports real stereo.
3082 _audioFrame.UpdateFrame(
3083 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
3084 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
3085 }
3086 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003087}
3088
kwiberg55b97fe2016-01-28 05:22:45 -08003089int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
3090 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003091
kwibergb7f89d62016-02-17 10:04:18 -08003092 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08003093 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003094
kwiberg55b97fe2016-01-28 05:22:45 -08003095 {
3096 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003097
kwiberg5a25d952016-08-17 07:31:12 -07003098 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003099 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3100 "Channel::MixAudioWithFile() file mixing failed");
3101 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003102 }
3103
kwiberg55b97fe2016-01-28 05:22:45 -08003104 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003105 if (output_file_player_->Get10msAudioFromFile(
3106 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003107 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3108 "Channel::MixAudioWithFile() file mixing failed");
3109 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003110 }
kwiberg55b97fe2016-01-28 05:22:45 -08003111 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003112
kwiberg55b97fe2016-01-28 05:22:45 -08003113 if (audioFrame.samples_per_channel_ == fileSamples) {
3114 // Currently file stream is always mono.
3115 // TODO(xians): Change the code when FilePlayer supports real stereo.
3116 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
3117 fileSamples);
3118 } else {
3119 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3120 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3121 ") != "
3122 "fileSamples(%" PRIuS ")",
3123 audioFrame.samples_per_channel_, fileSamples);
3124 return -1;
3125 }
3126
3127 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003128}
3129
deadbeef74375882015-08-13 12:09:10 -07003130void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003131 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003132
henrik.lundin96bd5022016-04-06 04:13:56 -07003133 if (!jitter_buffer_playout_timestamp_) {
3134 // This can happen if this channel has not received any RTP packets. In
3135 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003136 return;
3137 }
3138
3139 uint16_t delay_ms = 0;
3140 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003141 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003142 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3143 " delay from the ADM");
3144 _engineStatisticsPtr->SetLastError(
3145 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3146 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3147 return;
3148 }
3149
henrik.lundin96bd5022016-04-06 04:13:56 -07003150 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3151 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003152
3153 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003154 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003155
kwiberg55b97fe2016-01-28 05:22:45 -08003156 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003157 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003158 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003159
3160 {
tommi31fc21f2016-01-21 10:37:37 -08003161 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07003162 if (rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003163 playout_timestamp_rtcp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003164 } else {
henrik.lundin96bd5022016-04-06 04:13:56 -07003165 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003166 }
3167 playout_delay_ms_ = delay_ms;
3168 }
3169}
3170
kwiberg55b97fe2016-01-28 05:22:45 -08003171void Channel::RegisterReceiveCodecsToRTPModule() {
3172 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3173 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003174
kwiberg55b97fe2016-01-28 05:22:45 -08003175 CodecInst codec;
3176 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003177
kwiberg55b97fe2016-01-28 05:22:45 -08003178 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3179 // Open up the RTP/RTCP receiver for all supported codecs
3180 if ((audio_coding_->Codec(idx, &codec) == -1) ||
3181 (rtp_receiver_->RegisterReceivePayload(
3182 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3183 (codec.rate < 0) ? 0 : codec.rate) == -1)) {
3184 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3185 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3186 " to register %s (%d/%d/%" PRIuS
3187 "/%d) to RTP/RTCP "
3188 "receiver",
3189 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3190 codec.rate);
3191 } else {
3192 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3193 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3194 "(%d/%d/%" PRIuS
3195 "/%d) has been added to the RTP/RTCP "
3196 "receiver",
3197 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3198 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003199 }
kwiberg55b97fe2016-01-28 05:22:45 -08003200 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003201}
3202
kwiberg55b97fe2016-01-28 05:22:45 -08003203int Channel::SetSendRtpHeaderExtension(bool enable,
3204 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003205 unsigned char id) {
3206 int error = 0;
3207 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3208 if (enable) {
3209 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3210 }
3211 return error;
3212}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003213
ossue280cde2016-10-12 11:04:10 -07003214int Channel::GetRtpTimestampRateHz() const {
3215 const auto format = audio_coding_->ReceiveFormat();
3216 // Default to the playout frequency if we've not gotten any packets yet.
3217 // TODO(ossu): Zero clockrate can only happen if we've added an external
3218 // decoder for a format we don't support internally. Remove once that way of
3219 // adding decoders is gone!
3220 return (format && format->clockrate_hz != 0)
3221 ? format->clockrate_hz
3222 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003223}
3224
Minyue2013aec2015-05-13 14:14:42 +02003225int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003226 RtcpMode method = _rtpRtcpModule->RTCP();
3227 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003228 return 0;
3229 }
3230 std::vector<RTCPReportBlock> report_blocks;
3231 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003232
3233 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003234 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003235 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003236 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003237 Channel* channel = associate_send_channel_.channel();
3238 // Tries to get RTT from an associated channel. This is important for
3239 // receive-only channels.
3240 if (channel) {
3241 // To prevent infinite recursion and deadlock, calling GetRTT of
3242 // associate channel should always use "false" for argument:
3243 // |allow_associate_channel|.
3244 rtt = channel->GetRTT(false);
3245 }
3246 }
3247 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003248 }
3249
3250 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3251 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3252 for (; it != report_blocks.end(); ++it) {
3253 if (it->remoteSSRC == remoteSSRC)
3254 break;
3255 }
3256 if (it == report_blocks.end()) {
3257 // We have not received packets with SSRC matching the report blocks.
3258 // To calculate RTT we try with the SSRC of the first report block.
3259 // This is very important for send-only channels where we don't know
3260 // the SSRC of the other end.
3261 remoteSSRC = report_blocks[0].remoteSSRC;
3262 }
Minyue2013aec2015-05-13 14:14:42 +02003263
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003264 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003265 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003266 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003267 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3268 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003269 return 0;
3270 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003271 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003272}
3273
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003274} // namespace voe
3275} // namespace webrtc