blob: 0f0831c8b1f8cbd737a7b919e4a277b04cb87ff1 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrika@webrtc.org2919e952012-01-31 08:45:03 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000011#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Henrik Lundin64dad832015-05-11 12:44:23 +020013#include <algorithm>
Tommif888bb52015-12-12 01:37:01 +010014#include <utility>
Henrik Lundin64dad832015-05-11 12:44:23 +020015
aleloi6321b492016-12-05 01:46:09 -080016#include "webrtc/audio/utility/audio_frame_operations.h"
henrik.lundin50499422016-11-29 04:26:24 -080017#include "webrtc/base/array_view.h"
Ivo Creusenae856f22015-09-17 16:30:16 +020018#include "webrtc/base/checks.h"
tommi31fc21f2016-01-21 10:37:37 -080019#include "webrtc/base/criticalsection.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000020#include "webrtc/base/format_macros.h"
tommidea489f2017-03-03 03:20:24 -080021#include "webrtc/base/location.h"
pbosad856222015-11-27 09:48:36 -080022#include "webrtc/base/logging.h"
Erik Språng737336d2016-07-29 12:59:36 +020023#include "webrtc/base/rate_limiter.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000024#include "webrtc/base/timeutils.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020025#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070026#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070027#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000028#include "webrtc/modules/audio_device/include/audio_device.h"
29#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010031#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010032#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
33#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
34#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
nisse657bab22017-02-21 06:28:10 -080035#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000036#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010037#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010038#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000039#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
40#include "webrtc/voice_engine/output_mixer.h"
41#include "webrtc/voice_engine/statistics.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000042#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000043
andrew@webrtc.org50419b02012-11-14 19:07:54 +000044namespace webrtc {
45namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000046
kwibergc8d071e2016-04-06 12:22:38 -070047namespace {
48
Erik Språng737336d2016-07-29 12:59:36 +020049constexpr int64_t kMaxRetransmissionWindowMs = 1000;
50constexpr int64_t kMinRetransmissionWindowMs = 30;
51
kwibergc8d071e2016-04-06 12:22:38 -070052} // namespace
53
solenberg8842c3e2016-03-11 03:06:41 -080054const int kTelephoneEventAttenuationdB = 10;
55
ivoc14d5dbe2016-07-04 07:06:55 -070056class RtcEventLogProxy final : public webrtc::RtcEventLog {
57 public:
58 RtcEventLogProxy() : event_log_(nullptr) {}
59
60 bool StartLogging(const std::string& file_name,
61 int64_t max_size_bytes) override {
62 RTC_NOTREACHED();
63 return false;
64 }
65
66 bool StartLogging(rtc::PlatformFile log_file,
67 int64_t max_size_bytes) override {
68 RTC_NOTREACHED();
69 return false;
70 }
71
72 void StopLogging() override { RTC_NOTREACHED(); }
73
74 void LogVideoReceiveStreamConfig(
75 const webrtc::VideoReceiveStream::Config& config) override {
76 rtc::CritScope lock(&crit_);
77 if (event_log_) {
78 event_log_->LogVideoReceiveStreamConfig(config);
79 }
80 }
81
82 void LogVideoSendStreamConfig(
83 const webrtc::VideoSendStream::Config& config) override {
84 rtc::CritScope lock(&crit_);
85 if (event_log_) {
86 event_log_->LogVideoSendStreamConfig(config);
87 }
88 }
89
ivoce0928d82016-10-10 05:12:51 -070090 void LogAudioReceiveStreamConfig(
91 const webrtc::AudioReceiveStream::Config& config) override {
92 rtc::CritScope lock(&crit_);
93 if (event_log_) {
94 event_log_->LogAudioReceiveStreamConfig(config);
95 }
96 }
97
98 void LogAudioSendStreamConfig(
99 const webrtc::AudioSendStream::Config& config) override {
100 rtc::CritScope lock(&crit_);
101 if (event_log_) {
102 event_log_->LogAudioSendStreamConfig(config);
103 }
104 }
105
ivoc14d5dbe2016-07-04 07:06:55 -0700106 void LogRtpHeader(webrtc::PacketDirection direction,
107 webrtc::MediaType media_type,
108 const uint8_t* header,
109 size_t packet_length) override {
philipel32d00102017-02-27 02:18:46 -0800110 LogRtpHeader(direction, media_type, header, packet_length,
111 PacedPacketInfo::kNotAProbe);
112 }
113
114 void LogRtpHeader(webrtc::PacketDirection direction,
115 webrtc::MediaType media_type,
116 const uint8_t* header,
117 size_t packet_length,
118 int probe_cluster_id) override {
ivoc14d5dbe2016-07-04 07:06:55 -0700119 rtc::CritScope lock(&crit_);
120 if (event_log_) {
philipel32d00102017-02-27 02:18:46 -0800121 event_log_->LogRtpHeader(direction, media_type, header, packet_length,
122 probe_cluster_id);
ivoc14d5dbe2016-07-04 07:06:55 -0700123 }
124 }
125
126 void LogRtcpPacket(webrtc::PacketDirection direction,
127 webrtc::MediaType media_type,
128 const uint8_t* packet,
129 size_t length) override {
130 rtc::CritScope lock(&crit_);
131 if (event_log_) {
132 event_log_->LogRtcpPacket(direction, media_type, packet, length);
133 }
134 }
135
136 void LogAudioPlayout(uint32_t ssrc) override {
137 rtc::CritScope lock(&crit_);
138 if (event_log_) {
139 event_log_->LogAudioPlayout(ssrc);
140 }
141 }
142
terelius424e6cf2017-02-20 05:14:41 -0800143 void LogLossBasedBweUpdate(int32_t bitrate_bps,
ivoc14d5dbe2016-07-04 07:06:55 -0700144 uint8_t fraction_loss,
145 int32_t total_packets) override {
146 rtc::CritScope lock(&crit_);
147 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800148 event_log_->LogLossBasedBweUpdate(bitrate_bps, fraction_loss,
149 total_packets);
ivoc14d5dbe2016-07-04 07:06:55 -0700150 }
151 }
152
terelius424e6cf2017-02-20 05:14:41 -0800153 void LogDelayBasedBweUpdate(int32_t bitrate_bps,
terelius0baf55d2017-02-17 03:38:28 -0800154 BandwidthUsage detector_state) override {
155 rtc::CritScope lock(&crit_);
156 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800157 event_log_->LogDelayBasedBweUpdate(bitrate_bps, detector_state);
terelius0baf55d2017-02-17 03:38:28 -0800158 }
159 }
160
minyue4b7c9522017-01-24 04:54:59 -0800161 void LogAudioNetworkAdaptation(
162 const AudioNetworkAdaptor::EncoderRuntimeConfig& config) override {
163 rtc::CritScope lock(&crit_);
164 if (event_log_) {
165 event_log_->LogAudioNetworkAdaptation(config);
166 }
167 }
168
philipel32d00102017-02-27 02:18:46 -0800169 void LogProbeClusterCreated(int id,
170 int bitrate_bps,
171 int min_probes,
172 int min_bytes) override {
173 rtc::CritScope lock(&crit_);
174 if (event_log_) {
175 event_log_->LogProbeClusterCreated(id, bitrate_bps, min_probes,
176 min_bytes);
177 }
178 };
179
180 void LogProbeResultSuccess(int id, int bitrate_bps) override {
181 rtc::CritScope lock(&crit_);
182 if (event_log_) {
183 event_log_->LogProbeResultSuccess(id, bitrate_bps);
184 }
185 };
186
187 void LogProbeResultFailure(int id,
188 ProbeFailureReason failure_reason) override {
189 rtc::CritScope lock(&crit_);
190 if (event_log_) {
191 event_log_->LogProbeResultFailure(id, failure_reason);
192 }
193 };
194
ivoc14d5dbe2016-07-04 07:06:55 -0700195 void SetEventLog(RtcEventLog* event_log) {
196 rtc::CritScope lock(&crit_);
197 event_log_ = event_log;
198 }
199
200 private:
201 rtc::CriticalSection crit_;
202 RtcEventLog* event_log_ GUARDED_BY(crit_);
203 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
204};
205
michaelt9332b7d2016-11-30 07:51:13 -0800206class RtcpRttStatsProxy final : public RtcpRttStats {
207 public:
208 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
209
210 void OnRttUpdate(int64_t rtt) override {
211 rtc::CritScope lock(&crit_);
212 if (rtcp_rtt_stats_)
213 rtcp_rtt_stats_->OnRttUpdate(rtt);
214 }
215
216 int64_t LastProcessedRtt() const override {
217 rtc::CritScope lock(&crit_);
218 if (!rtcp_rtt_stats_)
219 return 0;
220 return rtcp_rtt_stats_->LastProcessedRtt();
221 }
222
223 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
224 rtc::CritScope lock(&crit_);
225 rtcp_rtt_stats_ = rtcp_rtt_stats;
226 }
227
228 private:
229 rtc::CriticalSection crit_;
230 RtcpRttStats* rtcp_rtt_stats_ GUARDED_BY(crit_);
231 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
232};
233
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100234class TransportFeedbackProxy : public TransportFeedbackObserver {
235 public:
236 TransportFeedbackProxy() : feedback_observer_(nullptr) {
237 pacer_thread_.DetachFromThread();
238 network_thread_.DetachFromThread();
239 }
240
241 void SetTransportFeedbackObserver(
242 TransportFeedbackObserver* feedback_observer) {
243 RTC_DCHECK(thread_checker_.CalledOnValidThread());
244 rtc::CritScope lock(&crit_);
245 feedback_observer_ = feedback_observer;
246 }
247
248 // Implements TransportFeedbackObserver.
249 void AddPacket(uint16_t sequence_number,
250 size_t length,
philipel8aadd502017-02-23 02:56:13 -0800251 const PacedPacketInfo& pacing_info) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100252 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
253 rtc::CritScope lock(&crit_);
254 if (feedback_observer_)
philipel8aadd502017-02-23 02:56:13 -0800255 feedback_observer_->AddPacket(sequence_number, length, pacing_info);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100256 }
philipel8aadd502017-02-23 02:56:13 -0800257
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100258 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
259 RTC_DCHECK(network_thread_.CalledOnValidThread());
260 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700261 if (feedback_observer_)
262 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200263 }
elad.alonf9490002017-03-06 05:32:21 -0800264 std::vector<PacketFeedback> GetTransportFeedbackVector() const override {
Stefan Holmer60e43462016-09-07 09:58:20 +0200265 RTC_NOTREACHED();
elad.alonf9490002017-03-06 05:32:21 -0800266 return std::vector<PacketFeedback>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100267 }
268
269 private:
270 rtc::CriticalSection crit_;
271 rtc::ThreadChecker thread_checker_;
272 rtc::ThreadChecker pacer_thread_;
273 rtc::ThreadChecker network_thread_;
274 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
275};
276
277class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
278 public:
279 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
280 pacer_thread_.DetachFromThread();
281 }
282
283 void SetSequenceNumberAllocator(
284 TransportSequenceNumberAllocator* seq_num_allocator) {
285 RTC_DCHECK(thread_checker_.CalledOnValidThread());
286 rtc::CritScope lock(&crit_);
287 seq_num_allocator_ = seq_num_allocator;
288 }
289
290 // Implements TransportSequenceNumberAllocator.
291 uint16_t AllocateSequenceNumber() override {
292 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
293 rtc::CritScope lock(&crit_);
294 if (!seq_num_allocator_)
295 return 0;
296 return seq_num_allocator_->AllocateSequenceNumber();
297 }
298
299 private:
300 rtc::CriticalSection crit_;
301 rtc::ThreadChecker thread_checker_;
302 rtc::ThreadChecker pacer_thread_;
303 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
304};
305
306class RtpPacketSenderProxy : public RtpPacketSender {
307 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800308 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100309
310 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
311 RTC_DCHECK(thread_checker_.CalledOnValidThread());
312 rtc::CritScope lock(&crit_);
313 rtp_packet_sender_ = rtp_packet_sender;
314 }
315
316 // Implements RtpPacketSender.
317 void InsertPacket(Priority priority,
318 uint32_t ssrc,
319 uint16_t sequence_number,
320 int64_t capture_time_ms,
321 size_t bytes,
322 bool retransmission) override {
323 rtc::CritScope lock(&crit_);
324 if (rtp_packet_sender_) {
325 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
326 capture_time_ms, bytes, retransmission);
327 }
328 }
329
330 private:
331 rtc::ThreadChecker thread_checker_;
332 rtc::CriticalSection crit_;
333 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
334};
335
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000336class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000337 public:
stefan7de8d642017-02-07 07:14:08 -0800338 explicit VoERtcpObserver(Channel* owner)
339 : owner_(owner), bandwidth_observer_(nullptr) {}
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000340 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000341
stefan7de8d642017-02-07 07:14:08 -0800342 void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
343 rtc::CritScope lock(&crit_);
344 bandwidth_observer_ = bandwidth_observer;
345 }
346
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000347 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
stefan7de8d642017-02-07 07:14:08 -0800348 rtc::CritScope lock(&crit_);
349 if (bandwidth_observer_) {
350 bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
351 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000352 }
353
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000354 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
355 int64_t rtt,
356 int64_t now_ms) override {
stefan7de8d642017-02-07 07:14:08 -0800357 {
358 rtc::CritScope lock(&crit_);
359 if (bandwidth_observer_) {
360 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
361 now_ms);
362 }
363 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000364 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
365 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
366 // report for VoiceEngine?
367 if (report_blocks.empty())
368 return;
369
370 int fraction_lost_aggregate = 0;
371 int total_number_of_packets = 0;
372
373 // If receiving multiple report blocks, calculate the weighted average based
374 // on the number of packets a report refers to.
375 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
376 block_it != report_blocks.end(); ++block_it) {
377 // Find the previous extended high sequence number for this remote SSRC,
378 // to calculate the number of RTP packets this report refers to. Ignore if
379 // we haven't seen this SSRC before.
380 std::map<uint32_t, uint32_t>::iterator seq_num_it =
381 extended_max_sequence_number_.find(block_it->sourceSSRC);
382 int number_of_packets = 0;
383 if (seq_num_it != extended_max_sequence_number_.end()) {
384 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
385 }
386 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
387 total_number_of_packets += number_of_packets;
388
389 extended_max_sequence_number_[block_it->sourceSSRC] =
390 block_it->extendedHighSeqNum;
391 }
392 int weighted_fraction_lost = 0;
393 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800394 weighted_fraction_lost =
395 (fraction_lost_aggregate + total_number_of_packets / 2) /
396 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000397 }
398 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000399 }
400
401 private:
402 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000403 // Maps remote side ssrc to extended highest sequence number received.
404 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800405 rtc::CriticalSection crit_;
406 RtcpBandwidthObserver* bandwidth_observer_ GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000407};
408
kwiberg55b97fe2016-01-28 05:22:45 -0800409int32_t Channel::SendData(FrameType frameType,
410 uint8_t payloadType,
411 uint32_t timeStamp,
412 const uint8_t* payloadData,
413 size_t payloadSize,
414 const RTPFragmentationHeader* fragmentation) {
415 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
416 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
417 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
418 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000419
kwiberg55b97fe2016-01-28 05:22:45 -0800420 if (_includeAudioLevelIndication) {
421 // Store current audio level in the RTP/RTCP module.
422 // The level will be used in combination with voice-activity state
423 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800424 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800425 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000426
kwiberg55b97fe2016-01-28 05:22:45 -0800427 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
428 // packetization.
429 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700430 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800431 (FrameType&)frameType, payloadType, timeStamp,
432 // Leaving the time when this frame was
433 // received from the capture device as
434 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700435 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800436 _engineStatisticsPtr->SetLastError(
437 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
438 "Channel::SendData() failed to send data to RTP/RTCP module");
439 return -1;
440 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000441
kwiberg55b97fe2016-01-28 05:22:45 -0800442 _lastLocalTimeStamp = timeStamp;
443 _lastPayloadType = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000444
kwiberg55b97fe2016-01-28 05:22:45 -0800445 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000446}
447
stefan1d8a5062015-10-02 03:39:33 -0700448bool Channel::SendRtp(const uint8_t* data,
449 size_t len,
450 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800451 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
452 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000453
kwiberg55b97fe2016-01-28 05:22:45 -0800454 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000455
kwiberg55b97fe2016-01-28 05:22:45 -0800456 if (_transportPtr == NULL) {
457 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
458 "Channel::SendPacket() failed to send RTP packet due to"
459 " invalid transport object");
460 return false;
461 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000462
kwiberg55b97fe2016-01-28 05:22:45 -0800463 uint8_t* bufferToSendPtr = (uint8_t*)data;
464 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000465
kwiberg55b97fe2016-01-28 05:22:45 -0800466 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
467 std::string transport_name =
468 _externalTransport ? "external transport" : "WebRtc sockets";
469 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
470 "Channel::SendPacket() RTP transmission using %s failed",
471 transport_name.c_str());
472 return false;
473 }
474 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000475}
476
kwiberg55b97fe2016-01-28 05:22:45 -0800477bool Channel::SendRtcp(const uint8_t* data, size_t len) {
478 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
479 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000480
kwiberg55b97fe2016-01-28 05:22:45 -0800481 rtc::CritScope cs(&_callbackCritSect);
482 if (_transportPtr == NULL) {
483 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
484 "Channel::SendRtcp() failed to send RTCP packet"
485 " due to invalid transport object");
486 return false;
487 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000488
kwiberg55b97fe2016-01-28 05:22:45 -0800489 uint8_t* bufferToSendPtr = (uint8_t*)data;
490 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000491
kwiberg55b97fe2016-01-28 05:22:45 -0800492 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
493 if (n < 0) {
494 std::string transport_name =
495 _externalTransport ? "external transport" : "WebRtc sockets";
496 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
497 "Channel::SendRtcp() transmission using %s failed",
498 transport_name.c_str());
499 return false;
500 }
501 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000502}
503
kwiberg55b97fe2016-01-28 05:22:45 -0800504void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
505 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
506 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000507
kwiberg55b97fe2016-01-28 05:22:45 -0800508 // Update ssrc so that NTP for AV sync can be updated.
509 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000510}
511
Peter Boströmac547a62015-09-17 23:03:57 +0200512void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
513 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
514 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
515 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000516}
517
Peter Boströmac547a62015-09-17 23:03:57 +0200518int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000519 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000520 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000521 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800522 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200523 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800524 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
525 "Channel::OnInitializeDecoder(payloadType=%d, "
526 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
527 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000528
kwiberg55b97fe2016-01-28 05:22:45 -0800529 CodecInst receiveCodec = {0};
530 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000531
kwiberg55b97fe2016-01-28 05:22:45 -0800532 receiveCodec.pltype = payloadType;
533 receiveCodec.plfreq = frequency;
534 receiveCodec.channels = channels;
535 receiveCodec.rate = rate;
536 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000537
kwiberg55b97fe2016-01-28 05:22:45 -0800538 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
539 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000540
kwiberg55b97fe2016-01-28 05:22:45 -0800541 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700542 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
543 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800544 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
545 "Channel::OnInitializeDecoder() invalid codec ("
546 "pt=%d, name=%s) received - 1",
547 payloadType, payloadName);
548 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
549 return -1;
550 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000551
kwiberg55b97fe2016-01-28 05:22:45 -0800552 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000553}
554
kwiberg55b97fe2016-01-28 05:22:45 -0800555int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
556 size_t payloadSize,
557 const WebRtcRTPHeader* rtpHeader) {
558 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
559 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
560 ","
561 " payloadType=%u, audioChannel=%" PRIuS ")",
562 payloadSize, rtpHeader->header.payloadType,
563 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000564
kwiberg55b97fe2016-01-28 05:22:45 -0800565 if (!channel_state_.Get().playing) {
566 // Avoid inserting into NetEQ when we are not playing. Count the
567 // packet as discarded.
568 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
569 "received packet is discarded since playing is not"
570 " activated");
niklase@google.com470e71d2011-07-07 08:21:25 +0000571 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800572 }
573
574 // Push the incoming payload (parsed and ready for decoding) into the ACM
575 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
576 0) {
577 _engineStatisticsPtr->SetLastError(
578 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
579 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
580 return -1;
581 }
582
kwiberg55b97fe2016-01-28 05:22:45 -0800583 int64_t round_trip_time = 0;
584 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
585 NULL);
586
587 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
588 if (!nack_list.empty()) {
589 // Can't use nack_list.data() since it's not supported by all
590 // compilers.
591 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
592 }
593 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000594}
595
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000596bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000597 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000598 RTPHeader header;
599 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
600 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
601 "IncomingPacket invalid RTP header");
602 return false;
603 }
604 header.payload_type_frequency =
605 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
606 if (header.payload_type_frequency < 0)
607 return false;
608 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
609}
610
henrik.lundin42dda502016-05-18 05:36:01 -0700611MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
612 int32_t id,
613 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700614 unsigned int ssrc;
nisse7d59f6b2017-02-21 03:40:24 -0800615 RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
ivoc14d5dbe2016-07-04 07:06:55 -0700616 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800617 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700618 bool muted;
619 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
620 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800621 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
622 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
623 // In all likelihood, the audio in this frame is garbage. We return an
624 // error so that the audio mixer module doesn't add it to the mix. As
625 // a result, it won't be played out and the actions skipped here are
626 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700627 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800628 }
henrik.lundina89ab962016-05-18 08:52:45 -0700629
630 if (muted) {
631 // TODO(henrik.lundin): We should be able to do better than this. But we
632 // will have to go through all the cases below where the audio samples may
633 // be used, and handle the muted case in some way.
aleloi6321b492016-12-05 01:46:09 -0800634 AudioFrameOperations::Mute(audioFrame);
henrik.lundina89ab962016-05-18 08:52:45 -0700635 }
kwiberg55b97fe2016-01-28 05:22:45 -0800636
kwiberg55b97fe2016-01-28 05:22:45 -0800637 // Convert module ID to internal VoE channel ID
638 audioFrame->id_ = VoEChannelId(audioFrame->id_);
639 // Store speech type for dead-or-alive detection
640 _outputSpeechType = audioFrame->speech_type_;
641
642 ChannelState::State state = channel_state_.Get();
643
kwiberg55b97fe2016-01-28 05:22:45 -0800644 {
645 // Pass the audio buffers to an optional sink callback, before applying
646 // scaling/panning, as that applies to the mix operation.
647 // External recipients of the audio (e.g. via AudioTrack), will do their
648 // own mixing/dynamic processing.
649 rtc::CritScope cs(&_callbackCritSect);
650 if (audio_sink_) {
651 AudioSinkInterface::Data data(
652 &audioFrame->data_[0], audioFrame->samples_per_channel_,
653 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
654 audioFrame->timestamp_);
655 audio_sink_->OnData(data);
656 }
657 }
658
659 float output_gain = 1.0f;
kwiberg55b97fe2016-01-28 05:22:45 -0800660 {
661 rtc::CritScope cs(&volume_settings_critsect_);
662 output_gain = _outputGain;
kwiberg55b97fe2016-01-28 05:22:45 -0800663 }
664
665 // Output volume scaling
666 if (output_gain < 0.99f || output_gain > 1.01f) {
solenberg8d73f8c2017-03-08 01:52:20 -0800667 // TODO(solenberg): Combine with mute state - this can cause clicks!
oprypin67fdb802017-03-09 06:25:06 -0800668 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800669 }
670
kwiberg55b97fe2016-01-28 05:22:45 -0800671 // Mix decoded PCM output with file if file mixing is enabled
672 if (state.output_file_playing) {
673 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700674 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800675 }
676
kwiberg55b97fe2016-01-28 05:22:45 -0800677 // Record playout if enabled
678 {
679 rtc::CritScope cs(&_fileCritSect);
680
kwiberg5a25d952016-08-17 07:31:12 -0700681 if (_outputFileRecording && output_file_recorder_) {
682 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800683 }
684 }
685
686 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700687 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800688 _outputAudioLevel.ComputeLevel(*audioFrame);
689
690 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
691 // The first frame with a valid rtp timestamp.
692 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
693 }
694
695 if (capture_start_rtp_time_stamp_ >= 0) {
696 // audioFrame.timestamp_ should be valid from now on.
697
698 // Compute elapsed time.
699 int64_t unwrap_timestamp =
700 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
701 audioFrame->elapsed_time_ms_ =
702 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700703 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800704
niklase@google.com470e71d2011-07-07 08:21:25 +0000705 {
kwiberg55b97fe2016-01-28 05:22:45 -0800706 rtc::CritScope lock(&ts_stats_lock_);
707 // Compute ntp time.
708 audioFrame->ntp_time_ms_ =
709 ntp_estimator_.Estimate(audioFrame->timestamp_);
710 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
711 if (audioFrame->ntp_time_ms_ > 0) {
712 // Compute |capture_start_ntp_time_ms_| so that
713 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
714 capture_start_ntp_time_ms_ =
715 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000716 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000717 }
kwiberg55b97fe2016-01-28 05:22:45 -0800718 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000719
henrik.lundin42dda502016-05-18 05:36:01 -0700720 return muted ? MixerParticipant::AudioFrameInfo::kMuted
721 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000722}
723
aleloi6c278492016-10-20 14:24:39 -0700724AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
725 int sample_rate_hz,
726 AudioFrame* audio_frame) {
727 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700728
aleloi6c278492016-10-20 14:24:39 -0700729 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700730
731 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
732 FrameInfo new_audio_frame_info = FrameInfo::kError;
733 switch (frame_info) {
734 case MixerParticipant::AudioFrameInfo::kNormal:
735 new_audio_frame_info = FrameInfo::kNormal;
736 break;
737 case MixerParticipant::AudioFrameInfo::kMuted:
738 new_audio_frame_info = FrameInfo::kMuted;
739 break;
740 case MixerParticipant::AudioFrameInfo::kError:
741 new_audio_frame_info = FrameInfo::kError;
742 break;
743 }
aleloi6c278492016-10-20 14:24:39 -0700744 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700745}
746
kwiberg55b97fe2016-01-28 05:22:45 -0800747int32_t Channel::NeededFrequency(int32_t id) const {
748 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
749 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000750
kwiberg55b97fe2016-01-28 05:22:45 -0800751 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000752
kwiberg55b97fe2016-01-28 05:22:45 -0800753 // Determine highest needed receive frequency
754 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000755
kwiberg55b97fe2016-01-28 05:22:45 -0800756 // Return the bigger of playout and receive frequency in the ACM.
757 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
758 highestNeeded = audio_coding_->PlayoutFrequency();
759 } else {
760 highestNeeded = receiveFrequency;
761 }
762
763 // Special case, if we're playing a file on the playout side
764 // we take that frequency into consideration as well
765 // This is not needed on sending side, since the codec will
766 // limit the spectrum anyway.
767 if (channel_state_.Get().output_file_playing) {
768 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700769 if (output_file_player_) {
770 if (output_file_player_->Frequency() > highestNeeded) {
771 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800772 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000773 }
kwiberg55b97fe2016-01-28 05:22:45 -0800774 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000775
kwiberg55b97fe2016-01-28 05:22:45 -0800776 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000777}
778
ossu5f7cfa52016-05-30 08:11:28 -0700779int32_t Channel::CreateChannel(
780 Channel*& channel,
781 int32_t channelId,
782 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700783 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800784 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
785 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
786 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000787
solenberg88499ec2016-09-07 07:34:41 -0700788 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800789 if (channel == NULL) {
790 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
791 "Channel::CreateChannel() unable to allocate memory for"
792 " channel");
793 return -1;
794 }
795 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000796}
797
kwiberg55b97fe2016-01-28 05:22:45 -0800798void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
799 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
800 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
801 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000802
kwiberg55b97fe2016-01-28 05:22:45 -0800803 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000804}
805
kwiberg55b97fe2016-01-28 05:22:45 -0800806void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
807 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
808 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
809 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000810
kwiberg55b97fe2016-01-28 05:22:45 -0800811 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000812}
813
kwiberg55b97fe2016-01-28 05:22:45 -0800814void Channel::PlayFileEnded(int32_t id) {
815 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
816 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000817
kwiberg55b97fe2016-01-28 05:22:45 -0800818 if (id == _inputFilePlayerId) {
819 channel_state_.SetInputFilePlaying(false);
820 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
821 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000822 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800823 } else if (id == _outputFilePlayerId) {
824 channel_state_.SetOutputFilePlaying(false);
825 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
826 "Channel::PlayFileEnded() => output file player module is"
827 " shutdown");
828 }
829}
830
831void Channel::RecordFileEnded(int32_t id) {
832 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
833 "Channel::RecordFileEnded(id=%d)", id);
834
835 assert(id == _outputFileRecorderId);
836
837 rtc::CritScope cs(&_fileCritSect);
838
839 _outputFileRecording = false;
840 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
841 "Channel::RecordFileEnded() => output file recorder module is"
842 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000843}
844
pbos@webrtc.org92135212013-05-14 08:31:39 +0000845Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000846 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700847 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800848 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100849 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700850 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800851 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100852 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800853 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100854 rtp_receive_statistics_(
855 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
856 rtp_receiver_(
857 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100858 this,
859 this,
860 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700861 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100862 _outputAudioLevel(),
863 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100864 // Avoid conflict with other channels by adding 1024 - 1026,
865 // won't use as much as 1024 channels.
866 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
867 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
868 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
869 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100870 _timeStamp(0), // This is just an offset, RTP module will add it's own
871 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100872 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100873 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100874 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100875 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100876 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
877 capture_start_rtp_time_stamp_(-1),
878 capture_start_ntp_time_ms_(-1),
879 _engineStatisticsPtr(NULL),
880 _outputMixerPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100881 _moduleProcessThreadPtr(NULL),
882 _audioDeviceModulePtr(NULL),
883 _voiceEngineObserverPtr(NULL),
884 _callbackCritSectPtr(NULL),
885 _transportPtr(NULL),
solenberg1c2af8e2016-03-24 10:36:00 -0700886 input_mute_(false),
887 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100888 _outputGain(1.0f),
solenberg8d73f8c2017-03-08 01:52:20 -0800889 _mixFileWithMicrophone(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100890 _lastLocalTimeStamp(0),
891 _lastPayloadType(0),
892 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800893 transport_overhead_per_packet_(0),
894 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100895 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100896 restored_packet_in_use_(false),
897 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100898 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700899 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800900 feedback_observer_proxy_(new TransportFeedbackProxy()),
901 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700902 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200903 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
904 kMaxRetransmissionWindowMs)),
michaelt566d8202017-01-12 10:17:38 -0800905 decoder_factory_(config.acm_config.decoder_factory) {
kwiberg55b97fe2016-01-28 05:22:45 -0800906 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
907 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700908 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800909 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700910 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800911 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200912
kwiberg55b97fe2016-01-28 05:22:45 -0800913 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000914
kwiberg55b97fe2016-01-28 05:22:45 -0800915 RtpRtcp::Configuration configuration;
916 configuration.audio = true;
917 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800918 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800919 configuration.receive_statistics = rtp_receive_statistics_.get();
920 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800921 if (pacing_enabled_) {
922 configuration.paced_sender = rtp_packet_sender_proxy_.get();
923 configuration.transport_sequence_number_allocator =
924 seq_num_allocator_proxy_.get();
925 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
926 }
ivoc14d5dbe2016-07-04 07:06:55 -0700927 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800928 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200929 configuration.retransmission_rate_limiter =
930 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000931
kwiberg55b97fe2016-01-28 05:22:45 -0800932 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100933 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000934}
935
kwiberg55b97fe2016-01-28 05:22:45 -0800936Channel::~Channel() {
tommi0a2391f2017-03-21 02:31:51 -0700937 RTC_DCHECK(!channel_state_.Get().sending);
938 RTC_DCHECK(!channel_state_.Get().playing);
niklase@google.com470e71d2011-07-07 08:21:25 +0000939}
940
kwiberg55b97fe2016-01-28 05:22:45 -0800941int32_t Channel::Init() {
tommi0a2391f2017-03-21 02:31:51 -0700942 RTC_DCHECK(construction_thread_.CalledOnValidThread());
kwiberg55b97fe2016-01-28 05:22:45 -0800943 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
944 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000945
kwiberg55b97fe2016-01-28 05:22:45 -0800946 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000947
kwiberg55b97fe2016-01-28 05:22:45 -0800948 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000949
kwiberg55b97fe2016-01-28 05:22:45 -0800950 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
951 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
952 "Channel::Init() must call SetEngineInformation() first");
953 return -1;
954 }
955
956 // --- Add modules to process thread (for periodic schedulation)
957
tommidea489f2017-03-03 03:20:24 -0800958 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
kwiberg55b97fe2016-01-28 05:22:45 -0800959
960 // --- ACM initialization
961
962 if (audio_coding_->InitializeReceiver() == -1) {
963 _engineStatisticsPtr->SetLastError(
964 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
965 "Channel::Init() unable to initialize the ACM - 1");
966 return -1;
967 }
968
969 // --- RTP/RTCP module initialization
970
971 // Ensure that RTCP is enabled by default for the created channel.
972 // Note that, the module will keep generating RTCP until it is explicitly
973 // disabled by the user.
974 // After StopListen (when no sockets exists), RTCP packets will no longer
975 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -0700976 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800977 // RTCP is enabled by default.
978 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
979 // --- Register all permanent callbacks
solenbergfe7dd6d2017-03-11 08:10:43 -0800980 if (audio_coding_->RegisterTransportCallback(this) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800981 _engineStatisticsPtr->SetLastError(
982 VE_CANNOT_INIT_CHANNEL, kTraceError,
983 "Channel::Init() callbacks not registered");
984 return -1;
985 }
986
987 // --- Register all supported codecs to the receiving side of the
988 // RTP/RTCP module
989
990 CodecInst codec;
991 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
992
993 for (int idx = 0; idx < nSupportedCodecs; idx++) {
994 // Open up the RTP/RTCP receiver for all supported codecs
995 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -0800996 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800997 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
998 "Channel::Init() unable to register %s "
999 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1000 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1001 codec.rate);
1002 } else {
1003 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1004 "Channel::Init() %s (%d/%d/%" PRIuS
1005 "/%d) has been "
1006 "added to the RTP/RTCP receiver",
1007 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1008 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001009 }
1010
kwiberg55b97fe2016-01-28 05:22:45 -08001011 // Ensure that PCMU is used as default codec on the sending side
1012 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) {
1013 SetSendCodec(codec);
niklase@google.com470e71d2011-07-07 08:21:25 +00001014 }
1015
kwiberg55b97fe2016-01-28 05:22:45 -08001016 // Register default PT for outband 'telephone-event'
1017 if (!STR_CASE_CMP(codec.plname, "telephone-event")) {
kwibergc8d071e2016-04-06 12:22:38 -07001018 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 ||
kwibergda2bf4e2016-10-24 13:47:09 -07001019 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1020 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001021 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1022 "Channel::Init() failed to register outband "
1023 "'telephone-event' (%d/%d) correctly",
1024 codec.pltype, codec.plfreq);
1025 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001026 }
1027
kwiberg55b97fe2016-01-28 05:22:45 -08001028 if (!STR_CASE_CMP(codec.plname, "CN")) {
kwibergc8d071e2016-04-06 12:22:38 -07001029 if (!codec_manager_.RegisterEncoder(codec) ||
1030 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
kwibergda2bf4e2016-10-24 13:47:09 -07001031 !audio_coding_->RegisterReceiveCodec(codec.pltype,
1032 CodecInstToSdp(codec)) ||
kwibergc8d071e2016-04-06 12:22:38 -07001033 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001034 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1035 "Channel::Init() failed to register CN (%d/%d) "
1036 "correctly - 1",
1037 codec.pltype, codec.plfreq);
1038 }
1039 }
kwiberg55b97fe2016-01-28 05:22:45 -08001040 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001041
kwiberg55b97fe2016-01-28 05:22:45 -08001042 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001043}
1044
tommi0a2391f2017-03-21 02:31:51 -07001045void Channel::Terminate() {
1046 RTC_DCHECK(construction_thread_.CalledOnValidThread());
1047 // Must be called on the same thread as Init().
1048 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
1049 "Channel::Terminate");
1050
1051 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
1052
1053 StopSend();
1054 StopPlayout();
1055
1056 {
1057 rtc::CritScope cs(&_fileCritSect);
1058 if (input_file_player_) {
1059 input_file_player_->RegisterModuleFileCallback(NULL);
1060 input_file_player_->StopPlayingFile();
1061 }
1062 if (output_file_player_) {
1063 output_file_player_->RegisterModuleFileCallback(NULL);
1064 output_file_player_->StopPlayingFile();
1065 }
1066 if (output_file_recorder_) {
1067 output_file_recorder_->RegisterModuleFileCallback(NULL);
1068 output_file_recorder_->StopRecording();
1069 }
1070 }
1071
1072 // The order to safely shutdown modules in a channel is:
1073 // 1. De-register callbacks in modules
1074 // 2. De-register modules in process thread
1075 // 3. Destroy modules
1076 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
1077 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1078 "Terminate() failed to de-register transport callback"
1079 " (Audio coding module)");
1080 }
1081
1082 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
1083 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1084 "Terminate() failed to de-register VAD callback"
1085 " (Audio coding module)");
1086 }
1087
1088 // De-register modules in process thread
1089 if (_moduleProcessThreadPtr)
1090 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
1091
1092 // End of modules shutdown
1093}
1094
kwiberg55b97fe2016-01-28 05:22:45 -08001095int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1096 OutputMixer& outputMixer,
kwiberg55b97fe2016-01-28 05:22:45 -08001097 ProcessThread& moduleProcessThread,
1098 AudioDeviceModule& audioDeviceModule,
1099 VoiceEngineObserver* voiceEngineObserver,
1100 rtc::CriticalSection* callbackCritSect) {
1101 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1102 "Channel::SetEngineInformation()");
1103 _engineStatisticsPtr = &engineStatistics;
1104 _outputMixerPtr = &outputMixer;
kwiberg55b97fe2016-01-28 05:22:45 -08001105 _moduleProcessThreadPtr = &moduleProcessThread;
1106 _audioDeviceModulePtr = &audioDeviceModule;
1107 _voiceEngineObserverPtr = voiceEngineObserver;
1108 _callbackCritSectPtr = callbackCritSect;
1109 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001110}
1111
kwiberg55b97fe2016-01-28 05:22:45 -08001112int32_t Channel::UpdateLocalTimeStamp() {
1113 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
1114 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001115}
1116
kwibergb7f89d62016-02-17 10:04:18 -08001117void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001118 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001119 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001120}
1121
ossu29b1a8d2016-06-13 07:34:51 -07001122const rtc::scoped_refptr<AudioDecoderFactory>&
1123Channel::GetAudioDecoderFactory() const {
1124 return decoder_factory_;
1125}
1126
kwiberg55b97fe2016-01-28 05:22:45 -08001127int32_t Channel::StartPlayout() {
1128 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1129 "Channel::StartPlayout()");
1130 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001131 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001132 }
1133
solenberge374e012017-02-14 04:55:00 -08001134 // 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;
kwiberg55b97fe2016-01-28 05:22:45 -08001140 }
1141
1142 channel_state_.SetPlaying(true);
1143 if (RegisterFilePlayingToMixer() != 0)
1144 return -1;
1145
1146 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001147}
1148
kwiberg55b97fe2016-01-28 05:22:45 -08001149int32_t Channel::StopPlayout() {
1150 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1151 "Channel::StopPlayout()");
1152 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001153 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001154 }
1155
solenberge374e012017-02-14 04:55:00 -08001156 // Remove participant as candidates for mixing
1157 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1158 _engineStatisticsPtr->SetLastError(
1159 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1160 "StopPlayout() failed to remove participant from mixer");
1161 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001162 }
1163
1164 channel_state_.SetPlaying(false);
1165 _outputAudioLevel.Clear();
1166
1167 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001168}
1169
kwiberg55b97fe2016-01-28 05:22:45 -08001170int32_t Channel::StartSend() {
1171 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1172 "Channel::StartSend()");
kwiberg55b97fe2016-01-28 05:22:45 -08001173 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001174 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001175 }
1176 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001177
solenberg08b19df2017-02-15 00:42:31 -08001178 // Resume the previous sequence number which was reset by StopSend(). This
1179 // needs to be done before |sending| is set to true on the RTP/RTCP module.
1180 if (send_sequence_number_) {
1181 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
1182 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001183 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001184 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1185 _engineStatisticsPtr->SetLastError(
1186 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1187 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001188 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001189 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001190 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001191 return -1;
1192 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001193
kwiberg55b97fe2016-01-28 05:22:45 -08001194 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001195}
1196
kwiberg55b97fe2016-01-28 05:22:45 -08001197int32_t Channel::StopSend() {
1198 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1199 "Channel::StopSend()");
1200 if (!channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001201 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001202 }
1203 channel_state_.SetSending(false);
1204
1205 // Store the sequence number to be able to pick up the same sequence for
1206 // the next StartSend(). This is needed for restarting device, otherwise
1207 // it might cause libSRTP to complain about packets being replayed.
1208 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1209 // CL is landed. See issue
1210 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1211 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1212
1213 // Reset sending SSRC and sequence number and triggers direct transmission
1214 // of RTCP BYE
1215 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1216 _engineStatisticsPtr->SetLastError(
1217 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1218 "StartSend() RTP/RTCP failed to stop sending");
1219 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001220 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001221
1222 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001223}
1224
kwiberg55b97fe2016-01-28 05:22:45 -08001225int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1226 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1227 "Channel::RegisterVoiceEngineObserver()");
1228 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001229
kwiberg55b97fe2016-01-28 05:22:45 -08001230 if (_voiceEngineObserverPtr) {
1231 _engineStatisticsPtr->SetLastError(
1232 VE_INVALID_OPERATION, kTraceError,
1233 "RegisterVoiceEngineObserver() observer already enabled");
1234 return -1;
1235 }
1236 _voiceEngineObserverPtr = &observer;
1237 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001238}
1239
kwiberg55b97fe2016-01-28 05:22:45 -08001240int32_t Channel::DeRegisterVoiceEngineObserver() {
1241 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1242 "Channel::DeRegisterVoiceEngineObserver()");
1243 rtc::CritScope cs(&_callbackCritSect);
1244
1245 if (!_voiceEngineObserverPtr) {
1246 _engineStatisticsPtr->SetLastError(
1247 VE_INVALID_OPERATION, kTraceWarning,
1248 "DeRegisterVoiceEngineObserver() observer already disabled");
1249 return 0;
1250 }
1251 _voiceEngineObserverPtr = NULL;
1252 return 0;
1253}
1254
1255int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001256 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001257 if (send_codec) {
1258 codec = *send_codec;
1259 return 0;
1260 }
1261 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001262}
1263
kwiberg55b97fe2016-01-28 05:22:45 -08001264int32_t Channel::GetRecCodec(CodecInst& codec) {
1265 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001266}
1267
kwiberg55b97fe2016-01-28 05:22:45 -08001268int32_t Channel::SetSendCodec(const CodecInst& codec) {
1269 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1270 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001271
kwibergc8d071e2016-04-06 12:22:38 -07001272 if (!codec_manager_.RegisterEncoder(codec) ||
1273 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001274 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1275 "SetSendCodec() failed to register codec to ACM");
1276 return -1;
1277 }
1278
1279 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1280 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1281 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1282 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1283 "SetSendCodec() failed to register codec to"
1284 " RTP/RTCP module");
1285 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001286 }
kwiberg55b97fe2016-01-28 05:22:45 -08001287 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001288
kwiberg55b97fe2016-01-28 05:22:45 -08001289 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001290}
1291
minyue78b4d562016-11-30 04:47:39 -08001292void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001293 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1294 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001295 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001296 if (*encoder) {
1297 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001298 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001299 }
1300 });
michaelt566d8202017-01-12 10:17:38 -08001301 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001302}
1303
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001304void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue7e304322016-10-12 05:00:55 -07001305 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1306 if (*encoder)
1307 (*encoder)->OnReceivedUplinkPacketLossFraction(fraction_lost / 255.0f);
1308 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001309}
1310
kwiberg55b97fe2016-01-28 05:22:45 -08001311int32_t Channel::SetVADStatus(bool enableVAD,
1312 ACMVADMode mode,
1313 bool disableDTX) {
1314 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1315 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001316 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1317 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1318 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001319 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1320 kTraceError,
1321 "SetVADStatus() failed to set VAD");
1322 return -1;
1323 }
1324 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001325}
1326
kwiberg55b97fe2016-01-28 05:22:45 -08001327int32_t Channel::GetVADStatus(bool& enabledVAD,
1328 ACMVADMode& mode,
1329 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001330 const auto* params = codec_manager_.GetStackParams();
1331 enabledVAD = params->use_cng;
1332 mode = params->vad_mode;
1333 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001334 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001335}
1336
kwiberg55b97fe2016-01-28 05:22:45 -08001337int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001338 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1339}
1340
1341int32_t Channel::SetRecPayloadType(int payload_type,
1342 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001343 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1344 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001345
kwiberg55b97fe2016-01-28 05:22:45 -08001346 if (channel_state_.Get().playing) {
1347 _engineStatisticsPtr->SetLastError(
1348 VE_ALREADY_PLAYING, kTraceError,
1349 "SetRecPayloadType() unable to set PT while playing");
1350 return -1;
1351 }
kwiberg55b97fe2016-01-28 05:22:45 -08001352
kwiberg09f090c2017-03-01 01:57:11 -08001353 const CodecInst codec = SdpToCodecInst(payload_type, format);
kwibergd32bf752017-01-19 07:03:59 -08001354
1355 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001356 // De-register the selected codec (RTP/RTCP module and ACM)
1357
1358 int8_t pltype(-1);
1359 CodecInst rxCodec = codec;
1360
1361 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001362 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001363 rxCodec.pltype = pltype;
1364
1365 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1366 _engineStatisticsPtr->SetLastError(
1367 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1368 "SetRecPayloadType() RTP/RTCP-module deregistration "
1369 "failed");
1370 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001371 }
kwiberg55b97fe2016-01-28 05:22:45 -08001372 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1373 _engineStatisticsPtr->SetLastError(
1374 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1375 "SetRecPayloadType() ACM deregistration failed - 1");
1376 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001377 }
kwiberg55b97fe2016-01-28 05:22:45 -08001378 return 0;
1379 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001380
magjed56124bd2016-11-24 09:34:46 -08001381 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001382 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001383 // TODO(kwiberg): Retrying is probably not necessary, since
1384 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001385 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001386 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001387 _engineStatisticsPtr->SetLastError(
1388 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1389 "SetRecPayloadType() RTP/RTCP-module registration failed");
1390 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001391 }
kwiberg55b97fe2016-01-28 05:22:45 -08001392 }
kwibergd32bf752017-01-19 07:03:59 -08001393 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1394 audio_coding_->UnregisterReceiveCodec(payload_type);
1395 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001396 _engineStatisticsPtr->SetLastError(
1397 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1398 "SetRecPayloadType() ACM registration failed - 1");
1399 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001400 }
kwiberg55b97fe2016-01-28 05:22:45 -08001401 }
1402 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001403}
1404
kwiberg55b97fe2016-01-28 05:22:45 -08001405int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1406 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001407 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001408 _engineStatisticsPtr->SetLastError(
1409 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1410 "GetRecPayloadType() failed to retrieve RX payload type");
1411 return -1;
1412 }
1413 codec.pltype = payloadType;
1414 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001415}
1416
kwiberg55b97fe2016-01-28 05:22:45 -08001417int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1418 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1419 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001420
kwiberg55b97fe2016-01-28 05:22:45 -08001421 CodecInst codec;
1422 int32_t samplingFreqHz(-1);
1423 const size_t kMono = 1;
1424 if (frequency == kFreq32000Hz)
1425 samplingFreqHz = 32000;
1426 else if (frequency == kFreq16000Hz)
1427 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001428
kwiberg55b97fe2016-01-28 05:22:45 -08001429 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1430 _engineStatisticsPtr->SetLastError(
1431 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1432 "SetSendCNPayloadType() failed to retrieve default CN codec "
1433 "settings");
1434 return -1;
1435 }
1436
1437 // Modify the payload type (must be set to dynamic range)
1438 codec.pltype = type;
1439
kwibergc8d071e2016-04-06 12:22:38 -07001440 if (!codec_manager_.RegisterEncoder(codec) ||
1441 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001442 _engineStatisticsPtr->SetLastError(
1443 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1444 "SetSendCNPayloadType() failed to register CN to ACM");
1445 return -1;
1446 }
1447
1448 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1449 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1450 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1451 _engineStatisticsPtr->SetLastError(
1452 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1453 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1454 "module");
1455 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001456 }
kwiberg55b97fe2016-01-28 05:22:45 -08001457 }
1458 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001459}
1460
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001461int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001462 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001463 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001464
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001465 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001466 _engineStatisticsPtr->SetLastError(
1467 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001468 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001469 return -1;
1470 }
1471 return 0;
1472}
1473
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001474int Channel::SetOpusDtx(bool enable_dtx) {
1475 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1476 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001477 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001478 : audio_coding_->DisableOpusDtx();
1479 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001480 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1481 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001482 return -1;
1483 }
1484 return 0;
1485}
1486
ivoc85228d62016-07-27 04:53:47 -07001487int Channel::GetOpusDtx(bool* enabled) {
1488 int success = -1;
1489 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1490 if (encoder) {
1491 *enabled = encoder->GetDtx();
1492 success = 0;
1493 }
1494 });
1495 return success;
1496}
1497
minyue7e304322016-10-12 05:00:55 -07001498bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1499 bool success = false;
1500 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1501 if (*encoder) {
1502 success = (*encoder)->EnableAudioNetworkAdaptor(
michaeltbf279fc2017-01-13 06:02:29 -08001503 config_string, event_log_proxy_.get(), Clock::GetRealTimeClock());
minyue7e304322016-10-12 05:00:55 -07001504 }
1505 });
1506 return success;
1507}
1508
1509void Channel::DisableAudioNetworkAdaptor() {
1510 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1511 if (*encoder)
1512 (*encoder)->DisableAudioNetworkAdaptor();
1513 });
1514}
1515
1516void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1517 int max_frame_length_ms) {
1518 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1519 if (*encoder) {
1520 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1521 max_frame_length_ms);
1522 }
1523 });
1524}
1525
mflodman3d7db262016-04-29 00:57:13 -07001526int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001527 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001528 "Channel::RegisterExternalTransport()");
1529
kwiberg55b97fe2016-01-28 05:22:45 -08001530 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001531 if (_externalTransport) {
1532 _engineStatisticsPtr->SetLastError(
1533 VE_INVALID_OPERATION, kTraceError,
1534 "RegisterExternalTransport() external transport already enabled");
1535 return -1;
1536 }
1537 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001538 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001539 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001540}
1541
kwiberg55b97fe2016-01-28 05:22:45 -08001542int32_t Channel::DeRegisterExternalTransport() {
1543 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1544 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001545
kwiberg55b97fe2016-01-28 05:22:45 -08001546 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001547 if (_transportPtr) {
1548 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1549 "DeRegisterExternalTransport() all transport is disabled");
1550 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001551 _engineStatisticsPtr->SetLastError(
1552 VE_INVALID_OPERATION, kTraceWarning,
1553 "DeRegisterExternalTransport() external transport already "
1554 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001555 }
1556 _externalTransport = false;
1557 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001558 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001559}
1560
nisse657bab22017-02-21 06:28:10 -08001561// TODO(nisse): Delete this method together with ReceivedRTPPacket.
1562// It's a temporary hack to support both ReceivedRTPPacket and
1563// OnRtpPacket interfaces without too much code duplication.
1564bool Channel::OnRtpPacketWithHeader(const uint8_t* received_packet,
1565 size_t length,
1566 RTPHeader *header) {
1567 // Store playout timestamp for the received RTP packet
1568 UpdatePlayoutTimestamp(false);
1569
1570 header->payload_type_frequency =
1571 rtp_payload_registry_->GetPayloadTypeFrequency(header->payloadType);
1572 if (header->payload_type_frequency < 0)
1573 return false;
1574 bool in_order = IsPacketInOrder(*header);
1575 rtp_receive_statistics_->IncomingPacket(
1576 *header, length, IsPacketRetransmitted(*header, in_order));
1577 rtp_payload_registry_->SetIncomingPayloadType(*header);
1578
1579 return ReceivePacket(received_packet, length, *header, in_order);
1580}
1581
mflodman3d7db262016-04-29 00:57:13 -07001582int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001583 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001584 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001585 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001586 "Channel::ReceivedRTPPacket()");
1587
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001588 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001589 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1590 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1591 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001592 return -1;
1593 }
nisse657bab22017-02-21 06:28:10 -08001594 return OnRtpPacketWithHeader(received_packet, length, &header) ? 0 : -1;
1595}
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001596
nisse657bab22017-02-21 06:28:10 -08001597void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
1598 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
1599 "Channel::ReceivedRTPPacket()");
1600
1601 RTPHeader header;
1602 packet.GetHeader(&header);
1603 OnRtpPacketWithHeader(packet.data(), packet.size(), &header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001604}
1605
1606bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001607 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001608 const RTPHeader& header,
1609 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001610 if (rtp_payload_registry_->IsRtx(header)) {
1611 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001612 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001613 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001614 assert(packet_length >= header.headerLength);
1615 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001616 PayloadUnion payload_specific;
1617 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001618 &payload_specific)) {
1619 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001620 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001621 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1622 payload_specific, in_order);
1623}
1624
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001625bool Channel::HandleRtxPacket(const uint8_t* packet,
1626 size_t packet_length,
1627 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001628 if (!rtp_payload_registry_->IsRtx(header))
1629 return false;
1630
1631 // Remove the RTX header and parse the original RTP header.
1632 if (packet_length < header.headerLength)
1633 return false;
1634 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1635 return false;
1636 if (restored_packet_in_use_) {
1637 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1638 "Multiple RTX headers detected, dropping packet");
1639 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001640 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001641 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001642 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1643 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001644 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1645 "Incoming RTX packet: invalid RTP header");
1646 return false;
1647 }
1648 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001649 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001650 restored_packet_in_use_ = false;
1651 return ret;
1652}
1653
1654bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1655 StreamStatistician* statistician =
1656 rtp_receive_statistics_->GetStatistician(header.ssrc);
1657 if (!statistician)
1658 return false;
1659 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001660}
1661
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001662bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1663 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001664 // Retransmissions are handled separately if RTX is enabled.
1665 if (rtp_payload_registry_->RtxEnabled())
1666 return false;
1667 StreamStatistician* statistician =
1668 rtp_receive_statistics_->GetStatistician(header.ssrc);
1669 if (!statistician)
1670 return false;
1671 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001672 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001673 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001674 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001675}
1676
mflodman3d7db262016-04-29 00:57:13 -07001677int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001678 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001679 "Channel::ReceivedRTCPPacket()");
1680 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001681 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001682
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001683 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001684 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001685 _engineStatisticsPtr->SetLastError(
1686 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1687 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1688 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001689
Minyue2013aec2015-05-13 14:14:42 +02001690 int64_t rtt = GetRTT(true);
1691 if (rtt == 0) {
1692 // Waiting for valid RTT.
1693 return 0;
1694 }
Erik Språng737336d2016-07-29 12:59:36 +02001695
1696 int64_t nack_window_ms = rtt;
1697 if (nack_window_ms < kMinRetransmissionWindowMs) {
1698 nack_window_ms = kMinRetransmissionWindowMs;
1699 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1700 nack_window_ms = kMaxRetransmissionWindowMs;
1701 }
1702 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1703
minyue7e304322016-10-12 05:00:55 -07001704 // Invoke audio encoders OnReceivedRtt().
1705 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1706 if (*encoder)
1707 (*encoder)->OnReceivedRtt(rtt);
1708 });
1709
Minyue2013aec2015-05-13 14:14:42 +02001710 uint32_t ntp_secs = 0;
1711 uint32_t ntp_frac = 0;
1712 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001713 if (0 !=
1714 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1715 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001716 // Waiting for RTCP.
1717 return 0;
1718 }
1719
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001720 {
tommi31fc21f2016-01-21 10:37:37 -08001721 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001722 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001723 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001724 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001725}
1726
niklase@google.com470e71d2011-07-07 08:21:25 +00001727int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001728 bool loop,
1729 FileFormats format,
1730 int startPosition,
1731 float volumeScaling,
1732 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001733 const CodecInst* codecInst) {
1734 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1735 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1736 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1737 "stopPosition=%d)",
1738 fileName, loop, format, volumeScaling, startPosition,
1739 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001740
kwiberg55b97fe2016-01-28 05:22:45 -08001741 if (channel_state_.Get().output_file_playing) {
1742 _engineStatisticsPtr->SetLastError(
1743 VE_ALREADY_PLAYING, kTraceError,
1744 "StartPlayingFileLocally() is already playing");
1745 return -1;
1746 }
1747
1748 {
1749 rtc::CritScope cs(&_fileCritSect);
1750
kwiberg5a25d952016-08-17 07:31:12 -07001751 if (output_file_player_) {
1752 output_file_player_->RegisterModuleFileCallback(NULL);
1753 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001754 }
1755
kwiberg5b356f42016-09-08 04:32:33 -07001756 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001757 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001758
kwiberg5a25d952016-08-17 07:31:12 -07001759 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001760 _engineStatisticsPtr->SetLastError(
1761 VE_INVALID_ARGUMENT, kTraceError,
1762 "StartPlayingFileLocally() filePlayer format is not correct");
1763 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001764 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001765
kwiberg55b97fe2016-01-28 05:22:45 -08001766 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001767
kwiberg5a25d952016-08-17 07:31:12 -07001768 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001769 fileName, loop, startPosition, volumeScaling, notificationTime,
1770 stopPosition, (const CodecInst*)codecInst) != 0) {
1771 _engineStatisticsPtr->SetLastError(
1772 VE_BAD_FILE, kTraceError,
1773 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001774 output_file_player_->StopPlayingFile();
1775 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001776 return -1;
1777 }
kwiberg5a25d952016-08-17 07:31:12 -07001778 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001779 channel_state_.SetOutputFilePlaying(true);
1780 }
1781
1782 if (RegisterFilePlayingToMixer() != 0)
1783 return -1;
1784
1785 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001786}
1787
1788int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001789 FileFormats format,
1790 int startPosition,
1791 float volumeScaling,
1792 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001793 const CodecInst* codecInst) {
1794 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1795 "Channel::StartPlayingFileLocally(format=%d,"
1796 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1797 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001798
kwiberg55b97fe2016-01-28 05:22:45 -08001799 if (stream == NULL) {
1800 _engineStatisticsPtr->SetLastError(
1801 VE_BAD_FILE, kTraceError,
1802 "StartPlayingFileLocally() NULL as input stream");
1803 return -1;
1804 }
1805
1806 if (channel_state_.Get().output_file_playing) {
1807 _engineStatisticsPtr->SetLastError(
1808 VE_ALREADY_PLAYING, kTraceError,
1809 "StartPlayingFileLocally() is already playing");
1810 return -1;
1811 }
1812
1813 {
1814 rtc::CritScope cs(&_fileCritSect);
1815
1816 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001817 if (output_file_player_) {
1818 output_file_player_->RegisterModuleFileCallback(NULL);
1819 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001820 }
1821
kwiberg55b97fe2016-01-28 05:22:45 -08001822 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001823 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001824 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001825
kwiberg5a25d952016-08-17 07:31:12 -07001826 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001827 _engineStatisticsPtr->SetLastError(
1828 VE_INVALID_ARGUMENT, kTraceError,
1829 "StartPlayingFileLocally() filePlayer format isnot correct");
1830 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001831 }
1832
kwiberg55b97fe2016-01-28 05:22:45 -08001833 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001834
kwiberg4ec01d92016-08-22 08:43:54 -07001835 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001836 volumeScaling, notificationTime,
1837 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001838 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1839 "StartPlayingFile() failed to "
1840 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001841 output_file_player_->StopPlayingFile();
1842 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001843 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001844 }
kwiberg5a25d952016-08-17 07:31:12 -07001845 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001846 channel_state_.SetOutputFilePlaying(true);
1847 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001848
kwiberg55b97fe2016-01-28 05:22:45 -08001849 if (RegisterFilePlayingToMixer() != 0)
1850 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001851
kwiberg55b97fe2016-01-28 05:22:45 -08001852 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001853}
1854
kwiberg55b97fe2016-01-28 05:22:45 -08001855int Channel::StopPlayingFileLocally() {
1856 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1857 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001858
kwiberg55b97fe2016-01-28 05:22:45 -08001859 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001860 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001861 }
1862
1863 {
1864 rtc::CritScope cs(&_fileCritSect);
1865
kwiberg5a25d952016-08-17 07:31:12 -07001866 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001867 _engineStatisticsPtr->SetLastError(
1868 VE_STOP_RECORDING_FAILED, kTraceError,
1869 "StopPlayingFile() could not stop playing");
1870 return -1;
1871 }
kwiberg5a25d952016-08-17 07:31:12 -07001872 output_file_player_->RegisterModuleFileCallback(NULL);
1873 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001874 channel_state_.SetOutputFilePlaying(false);
1875 }
1876 // _fileCritSect cannot be taken while calling
1877 // SetAnonymousMixibilityStatus. Refer to comments in
1878 // StartPlayingFileLocally(const char* ...) for more details.
1879 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1880 _engineStatisticsPtr->SetLastError(
1881 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1882 "StopPlayingFile() failed to stop participant from playing as"
1883 "file in the mixer");
1884 return -1;
1885 }
1886
1887 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001888}
1889
kwiberg55b97fe2016-01-28 05:22:45 -08001890int Channel::IsPlayingFileLocally() const {
1891 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001892}
1893
kwiberg55b97fe2016-01-28 05:22:45 -08001894int Channel::RegisterFilePlayingToMixer() {
1895 // Return success for not registering for file playing to mixer if:
1896 // 1. playing file before playout is started on that channel.
1897 // 2. starting playout without file playing on that channel.
1898 if (!channel_state_.Get().playing ||
1899 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001900 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001901 }
1902
1903 // |_fileCritSect| cannot be taken while calling
1904 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1905 // frames can be pulled by the mixer. Since the frames are generated from
1906 // the file, _fileCritSect will be taken. This would result in a deadlock.
1907 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
1908 channel_state_.SetOutputFilePlaying(false);
1909 rtc::CritScope cs(&_fileCritSect);
1910 _engineStatisticsPtr->SetLastError(
1911 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1912 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07001913 output_file_player_->StopPlayingFile();
1914 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001915 return -1;
1916 }
1917
1918 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001919}
1920
niklase@google.com470e71d2011-07-07 08:21:25 +00001921int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001922 bool loop,
1923 FileFormats format,
1924 int startPosition,
1925 float volumeScaling,
1926 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001927 const CodecInst* codecInst) {
1928 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1929 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1930 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1931 "stopPosition=%d)",
1932 fileName, loop, format, volumeScaling, startPosition,
1933 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001934
kwiberg55b97fe2016-01-28 05:22:45 -08001935 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001936
kwiberg55b97fe2016-01-28 05:22:45 -08001937 if (channel_state_.Get().input_file_playing) {
1938 _engineStatisticsPtr->SetLastError(
1939 VE_ALREADY_PLAYING, kTraceWarning,
1940 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00001941 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001942 }
1943
1944 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001945 if (input_file_player_) {
1946 input_file_player_->RegisterModuleFileCallback(NULL);
1947 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001948 }
1949
1950 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001951 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07001952 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08001953
kwiberg5a25d952016-08-17 07:31:12 -07001954 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001955 _engineStatisticsPtr->SetLastError(
1956 VE_INVALID_ARGUMENT, kTraceError,
1957 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1958 return -1;
1959 }
1960
1961 const uint32_t notificationTime(0);
1962
kwiberg5a25d952016-08-17 07:31:12 -07001963 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001964 fileName, loop, startPosition, volumeScaling, notificationTime,
1965 stopPosition, (const CodecInst*)codecInst) != 0) {
1966 _engineStatisticsPtr->SetLastError(
1967 VE_BAD_FILE, kTraceError,
1968 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001969 input_file_player_->StopPlayingFile();
1970 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001971 return -1;
1972 }
kwiberg5a25d952016-08-17 07:31:12 -07001973 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001974 channel_state_.SetInputFilePlaying(true);
1975
1976 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001977}
1978
1979int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001980 FileFormats format,
1981 int startPosition,
1982 float volumeScaling,
1983 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001984 const CodecInst* codecInst) {
1985 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1986 "Channel::StartPlayingFileAsMicrophone(format=%d, "
1987 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1988 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001989
kwiberg55b97fe2016-01-28 05:22:45 -08001990 if (stream == NULL) {
1991 _engineStatisticsPtr->SetLastError(
1992 VE_BAD_FILE, kTraceError,
1993 "StartPlayingFileAsMicrophone NULL as input stream");
1994 return -1;
1995 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001996
kwiberg55b97fe2016-01-28 05:22:45 -08001997 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001998
kwiberg55b97fe2016-01-28 05:22:45 -08001999 if (channel_state_.Get().input_file_playing) {
2000 _engineStatisticsPtr->SetLastError(
2001 VE_ALREADY_PLAYING, kTraceWarning,
2002 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002003 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002004 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002005
kwiberg55b97fe2016-01-28 05:22:45 -08002006 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002007 if (input_file_player_) {
2008 input_file_player_->RegisterModuleFileCallback(NULL);
2009 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002010 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002011
kwiberg55b97fe2016-01-28 05:22:45 -08002012 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002013 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002014 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002015
kwiberg5a25d952016-08-17 07:31:12 -07002016 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002017 _engineStatisticsPtr->SetLastError(
2018 VE_INVALID_ARGUMENT, kTraceError,
2019 "StartPlayingInputFile() filePlayer format isnot correct");
2020 return -1;
2021 }
2022
2023 const uint32_t notificationTime(0);
2024
kwiberg4ec01d92016-08-22 08:43:54 -07002025 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2026 notificationTime, stopPosition,
2027 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002028 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2029 "StartPlayingFile() failed to start "
2030 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002031 input_file_player_->StopPlayingFile();
2032 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002033 return -1;
2034 }
2035
kwiberg5a25d952016-08-17 07:31:12 -07002036 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002037 channel_state_.SetInputFilePlaying(true);
2038
2039 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002040}
2041
kwiberg55b97fe2016-01-28 05:22:45 -08002042int Channel::StopPlayingFileAsMicrophone() {
2043 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2044 "Channel::StopPlayingFileAsMicrophone()");
2045
2046 rtc::CritScope cs(&_fileCritSect);
2047
2048 if (!channel_state_.Get().input_file_playing) {
2049 return 0;
2050 }
2051
kwiberg5a25d952016-08-17 07:31:12 -07002052 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002053 _engineStatisticsPtr->SetLastError(
2054 VE_STOP_RECORDING_FAILED, kTraceError,
2055 "StopPlayingFile() could not stop playing");
2056 return -1;
2057 }
kwiberg5a25d952016-08-17 07:31:12 -07002058 input_file_player_->RegisterModuleFileCallback(NULL);
2059 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002060 channel_state_.SetInputFilePlaying(false);
2061
2062 return 0;
2063}
2064
2065int Channel::IsPlayingFileAsMicrophone() const {
2066 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002067}
2068
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002069int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002070 const CodecInst* codecInst) {
2071 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2072 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002073
kwiberg55b97fe2016-01-28 05:22:45 -08002074 if (_outputFileRecording) {
2075 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2076 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002077 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002078 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002079
kwiberg55b97fe2016-01-28 05:22:45 -08002080 FileFormats format;
2081 const uint32_t notificationTime(0); // Not supported in VoE
2082 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002083
kwiberg55b97fe2016-01-28 05:22:45 -08002084 if ((codecInst != NULL) &&
2085 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2086 _engineStatisticsPtr->SetLastError(
2087 VE_BAD_ARGUMENT, kTraceError,
2088 "StartRecordingPlayout() invalid compression");
2089 return (-1);
2090 }
2091 if (codecInst == NULL) {
2092 format = kFileFormatPcm16kHzFile;
2093 codecInst = &dummyCodec;
2094 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2095 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2096 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2097 format = kFileFormatWavFile;
2098 } else {
2099 format = kFileFormatCompressedFile;
2100 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002101
kwiberg55b97fe2016-01-28 05:22:45 -08002102 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002103
kwiberg55b97fe2016-01-28 05:22:45 -08002104 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002105 if (output_file_recorder_) {
2106 output_file_recorder_->RegisterModuleFileCallback(NULL);
2107 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002108 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002109
kwiberg5a25d952016-08-17 07:31:12 -07002110 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002111 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002112 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002113 _engineStatisticsPtr->SetLastError(
2114 VE_INVALID_ARGUMENT, kTraceError,
2115 "StartRecordingPlayout() fileRecorder format isnot correct");
2116 return -1;
2117 }
2118
kwiberg5a25d952016-08-17 07:31:12 -07002119 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002120 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2121 _engineStatisticsPtr->SetLastError(
2122 VE_BAD_FILE, kTraceError,
2123 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002124 output_file_recorder_->StopRecording();
2125 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002126 return -1;
2127 }
kwiberg5a25d952016-08-17 07:31:12 -07002128 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002129 _outputFileRecording = true;
2130
2131 return 0;
2132}
2133
2134int Channel::StartRecordingPlayout(OutStream* stream,
2135 const CodecInst* codecInst) {
2136 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2137 "Channel::StartRecordingPlayout()");
2138
2139 if (_outputFileRecording) {
2140 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2141 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002142 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002143 }
2144
2145 FileFormats format;
2146 const uint32_t notificationTime(0); // Not supported in VoE
2147 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2148
2149 if (codecInst != NULL && codecInst->channels != 1) {
2150 _engineStatisticsPtr->SetLastError(
2151 VE_BAD_ARGUMENT, kTraceError,
2152 "StartRecordingPlayout() invalid compression");
2153 return (-1);
2154 }
2155 if (codecInst == NULL) {
2156 format = kFileFormatPcm16kHzFile;
2157 codecInst = &dummyCodec;
2158 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2159 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2160 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2161 format = kFileFormatWavFile;
2162 } else {
2163 format = kFileFormatCompressedFile;
2164 }
2165
2166 rtc::CritScope cs(&_fileCritSect);
2167
2168 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002169 if (output_file_recorder_) {
2170 output_file_recorder_->RegisterModuleFileCallback(NULL);
2171 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002172 }
2173
kwiberg5a25d952016-08-17 07:31:12 -07002174 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002175 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002176 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002177 _engineStatisticsPtr->SetLastError(
2178 VE_INVALID_ARGUMENT, kTraceError,
2179 "StartRecordingPlayout() fileRecorder format isnot correct");
2180 return -1;
2181 }
2182
kwiberg4ec01d92016-08-22 08:43:54 -07002183 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002184 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002185 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2186 "StartRecordingPlayout() failed to "
2187 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002188 output_file_recorder_->StopRecording();
2189 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002190 return -1;
2191 }
2192
kwiberg5a25d952016-08-17 07:31:12 -07002193 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002194 _outputFileRecording = true;
2195
2196 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002197}
2198
kwiberg55b97fe2016-01-28 05:22:45 -08002199int Channel::StopRecordingPlayout() {
2200 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2201 "Channel::StopRecordingPlayout()");
2202
2203 if (!_outputFileRecording) {
2204 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2205 "StopRecordingPlayout() isnot recording");
2206 return -1;
2207 }
2208
2209 rtc::CritScope cs(&_fileCritSect);
2210
kwiberg5a25d952016-08-17 07:31:12 -07002211 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002212 _engineStatisticsPtr->SetLastError(
2213 VE_STOP_RECORDING_FAILED, kTraceError,
2214 "StopRecording() could not stop recording");
2215 return (-1);
2216 }
kwiberg5a25d952016-08-17 07:31:12 -07002217 output_file_recorder_->RegisterModuleFileCallback(NULL);
2218 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002219 _outputFileRecording = false;
2220
2221 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002222}
2223
kwiberg55b97fe2016-01-28 05:22:45 -08002224void Channel::SetMixWithMicStatus(bool mix) {
2225 rtc::CritScope cs(&_fileCritSect);
2226 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002227}
2228
solenberg8d73f8c2017-03-08 01:52:20 -08002229int Channel::GetSpeechOutputLevel() const {
2230 return _outputAudioLevel.Level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002231}
2232
solenberg8d73f8c2017-03-08 01:52:20 -08002233int Channel::GetSpeechOutputLevelFullRange() const {
2234 return _outputAudioLevel.LevelFullRange();
kwiberg55b97fe2016-01-28 05:22:45 -08002235}
2236
solenberg8d73f8c2017-03-08 01:52:20 -08002237void Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002238 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002239 input_mute_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002240}
2241
solenberg1c2af8e2016-03-24 10:36:00 -07002242bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002243 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002244 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002245}
2246
solenberg8d73f8c2017-03-08 01:52:20 -08002247void Channel::SetChannelOutputVolumeScaling(float scaling) {
kwiberg55b97fe2016-01-28 05:22:45 -08002248 rtc::CritScope cs(&volume_settings_critsect_);
kwiberg55b97fe2016-01-28 05:22:45 -08002249 _outputGain = scaling;
niklase@google.com470e71d2011-07-07 08:21:25 +00002250}
2251
solenberg8842c3e2016-03-11 03:06:41 -08002252int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002253 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002254 "Channel::SendTelephoneEventOutband(...)");
2255 RTC_DCHECK_LE(0, event);
2256 RTC_DCHECK_GE(255, event);
2257 RTC_DCHECK_LE(0, duration_ms);
2258 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002259 if (!Sending()) {
2260 return -1;
2261 }
solenberg8842c3e2016-03-11 03:06:41 -08002262 if (_rtpRtcpModule->SendTelephoneEventOutband(
2263 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002264 _engineStatisticsPtr->SetLastError(
2265 VE_SEND_DTMF_FAILED, kTraceWarning,
2266 "SendTelephoneEventOutband() failed to send event");
2267 return -1;
2268 }
2269 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002270}
2271
solenbergffbbcac2016-11-17 05:25:37 -08002272int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2273 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002274 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002275 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002276 RTC_DCHECK_LE(0, payload_type);
2277 RTC_DCHECK_GE(127, payload_type);
2278 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002279 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002280 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002281 memcpy(codec.plname, "telephone-event", 16);
2282 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2283 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2284 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2285 _engineStatisticsPtr->SetLastError(
2286 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2287 "SetSendTelephoneEventPayloadType() failed to register send"
2288 "payload type");
2289 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002290 }
kwiberg55b97fe2016-01-28 05:22:45 -08002291 }
kwiberg55b97fe2016-01-28 05:22:45 -08002292 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002293}
2294
kwiberg55b97fe2016-01-28 05:22:45 -08002295int Channel::SetLocalSSRC(unsigned int ssrc) {
2296 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2297 "Channel::SetLocalSSRC()");
2298 if (channel_state_.Get().sending) {
2299 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2300 "SetLocalSSRC() already sending");
2301 return -1;
2302 }
2303 _rtpRtcpModule->SetSSRC(ssrc);
2304 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002305}
2306
kwiberg55b97fe2016-01-28 05:22:45 -08002307int Channel::GetLocalSSRC(unsigned int& ssrc) {
2308 ssrc = _rtpRtcpModule->SSRC();
2309 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002310}
2311
kwiberg55b97fe2016-01-28 05:22:45 -08002312int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2313 ssrc = rtp_receiver_->SSRC();
2314 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002315}
2316
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002317int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002318 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002319 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002320}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002321
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002322int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2323 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002324 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2325 if (enable &&
2326 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2327 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002328 return -1;
2329 }
2330 return 0;
2331}
2332
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002333void Channel::EnableSendTransportSequenceNumber(int id) {
2334 int ret =
2335 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2336 RTC_DCHECK_EQ(0, ret);
2337}
2338
stefan3313ec92016-01-21 06:32:43 -08002339void Channel::EnableReceiveTransportSequenceNumber(int id) {
2340 rtp_header_parser_->DeregisterRtpHeaderExtension(
2341 kRtpExtensionTransportSequenceNumber);
2342 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2343 kRtpExtensionTransportSequenceNumber, id);
2344 RTC_DCHECK(ret);
2345}
2346
stefanbba9dec2016-02-01 04:39:55 -08002347void Channel::RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002348 RtpPacketSender* rtp_packet_sender,
2349 TransportFeedbackObserver* transport_feedback_observer,
stefan7de8d642017-02-07 07:14:08 -08002350 PacketRouter* packet_router,
2351 RtcpBandwidthObserver* bandwidth_observer) {
stefanbba9dec2016-02-01 04:39:55 -08002352 RTC_DCHECK(rtp_packet_sender);
2353 RTC_DCHECK(transport_feedback_observer);
2354 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002355 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002356 feedback_observer_proxy_->SetTransportFeedbackObserver(
2357 transport_feedback_observer);
2358 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2359 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2360 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002361 packet_router->AddRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002362 packet_router_ = packet_router;
2363}
2364
stefanbba9dec2016-02-01 04:39:55 -08002365void Channel::RegisterReceiverCongestionControlObjects(
2366 PacketRouter* packet_router) {
2367 RTC_DCHECK(packet_router && !packet_router_);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002368 packet_router->AddRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002369 packet_router_ = packet_router;
2370}
2371
2372void Channel::ResetCongestionControlObjects() {
2373 RTC_DCHECK(packet_router_);
2374 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002375 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002376 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2377 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
Peter Boström3dd5d1d2016-02-25 16:56:48 +01002378 packet_router_->RemoveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002379 packet_router_ = nullptr;
2380 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2381}
2382
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002383void Channel::SetRTCPStatus(bool enable) {
2384 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2385 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002386 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002387}
2388
kwiberg55b97fe2016-01-28 05:22:45 -08002389int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002390 RtcpMode method = _rtpRtcpModule->RTCP();
2391 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002392 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002393}
2394
kwiberg55b97fe2016-01-28 05:22:45 -08002395int Channel::SetRTCP_CNAME(const char cName[256]) {
2396 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2397 "Channel::SetRTCP_CNAME()");
2398 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2399 _engineStatisticsPtr->SetLastError(
2400 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2401 "SetRTCP_CNAME() failed to set RTCP CNAME");
2402 return -1;
2403 }
2404 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002405}
2406
kwiberg55b97fe2016-01-28 05:22:45 -08002407int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2408 if (cName == NULL) {
2409 _engineStatisticsPtr->SetLastError(
2410 VE_INVALID_ARGUMENT, kTraceError,
2411 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2412 return -1;
2413 }
2414 char cname[RTCP_CNAME_SIZE];
2415 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2416 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2417 _engineStatisticsPtr->SetLastError(
2418 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2419 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2420 return -1;
2421 }
2422 strcpy(cName, cname);
2423 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002424}
2425
kwiberg55b97fe2016-01-28 05:22:45 -08002426int Channel::SendApplicationDefinedRTCPPacket(
2427 unsigned char subType,
2428 unsigned int name,
2429 const char* data,
2430 unsigned short dataLengthInBytes) {
2431 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2432 "Channel::SendApplicationDefinedRTCPPacket()");
2433 if (!channel_state_.Get().sending) {
2434 _engineStatisticsPtr->SetLastError(
2435 VE_NOT_SENDING, kTraceError,
2436 "SendApplicationDefinedRTCPPacket() not sending");
2437 return -1;
2438 }
2439 if (NULL == data) {
2440 _engineStatisticsPtr->SetLastError(
2441 VE_INVALID_ARGUMENT, kTraceError,
2442 "SendApplicationDefinedRTCPPacket() invalid data value");
2443 return -1;
2444 }
2445 if (dataLengthInBytes % 4 != 0) {
2446 _engineStatisticsPtr->SetLastError(
2447 VE_INVALID_ARGUMENT, kTraceError,
2448 "SendApplicationDefinedRTCPPacket() invalid length value");
2449 return -1;
2450 }
2451 RtcpMode status = _rtpRtcpModule->RTCP();
2452 if (status == RtcpMode::kOff) {
2453 _engineStatisticsPtr->SetLastError(
2454 VE_RTCP_ERROR, kTraceError,
2455 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2456 return -1;
2457 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002458
kwiberg55b97fe2016-01-28 05:22:45 -08002459 // Create and schedule the RTCP APP packet for transmission
2460 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2461 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2462 _engineStatisticsPtr->SetLastError(
2463 VE_SEND_ERROR, kTraceError,
2464 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2465 return -1;
2466 }
2467 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002468}
2469
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002470int Channel::GetRemoteRTCPReportBlocks(
2471 std::vector<ReportBlock>* report_blocks) {
2472 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002473 _engineStatisticsPtr->SetLastError(
2474 VE_INVALID_ARGUMENT, kTraceError,
2475 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002476 return -1;
2477 }
2478
2479 // Get the report blocks from the latest received RTCP Sender or Receiver
2480 // Report. Each element in the vector contains the sender's SSRC and a
2481 // report block according to RFC 3550.
2482 std::vector<RTCPReportBlock> rtcp_report_blocks;
2483 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002484 return -1;
2485 }
2486
2487 if (rtcp_report_blocks.empty())
2488 return 0;
2489
2490 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2491 for (; it != rtcp_report_blocks.end(); ++it) {
2492 ReportBlock report_block;
2493 report_block.sender_SSRC = it->remoteSSRC;
2494 report_block.source_SSRC = it->sourceSSRC;
2495 report_block.fraction_lost = it->fractionLost;
2496 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2497 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2498 report_block.interarrival_jitter = it->jitter;
2499 report_block.last_SR_timestamp = it->lastSR;
2500 report_block.delay_since_last_SR = it->delaySinceLastSR;
2501 report_blocks->push_back(report_block);
2502 }
2503 return 0;
2504}
2505
kwiberg55b97fe2016-01-28 05:22:45 -08002506int Channel::GetRTPStatistics(CallStatistics& stats) {
2507 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002508
kwiberg55b97fe2016-01-28 05:22:45 -08002509 // The jitter statistics is updated for each received RTP packet and is
2510 // based on received packets.
2511 RtcpStatistics statistics;
2512 StreamStatistician* statistician =
2513 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002514 if (statistician) {
2515 statistician->GetStatistics(&statistics,
2516 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002517 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002518
kwiberg55b97fe2016-01-28 05:22:45 -08002519 stats.fractionLost = statistics.fraction_lost;
2520 stats.cumulativeLost = statistics.cumulative_lost;
2521 stats.extendedMax = statistics.extended_max_sequence_number;
2522 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002523
kwiberg55b97fe2016-01-28 05:22:45 -08002524 // --- RTT
2525 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002526
kwiberg55b97fe2016-01-28 05:22:45 -08002527 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002528
kwiberg55b97fe2016-01-28 05:22:45 -08002529 size_t bytesSent(0);
2530 uint32_t packetsSent(0);
2531 size_t bytesReceived(0);
2532 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002533
kwiberg55b97fe2016-01-28 05:22:45 -08002534 if (statistician) {
2535 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2536 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002537
kwiberg55b97fe2016-01-28 05:22:45 -08002538 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2539 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2540 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2541 " output will not be complete");
2542 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002543
kwiberg55b97fe2016-01-28 05:22:45 -08002544 stats.bytesSent = bytesSent;
2545 stats.packetsSent = packetsSent;
2546 stats.bytesReceived = bytesReceived;
2547 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002548
kwiberg55b97fe2016-01-28 05:22:45 -08002549 // --- Timestamps
2550 {
2551 rtc::CritScope lock(&ts_stats_lock_);
2552 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2553 }
2554 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002555}
2556
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002557int Channel::SetCodecFECStatus(bool enable) {
2558 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2559 "Channel::SetCodecFECStatus()");
2560
kwibergc8d071e2016-04-06 12:22:38 -07002561 if (!codec_manager_.SetCodecFEC(enable) ||
2562 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002563 _engineStatisticsPtr->SetLastError(
2564 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2565 "SetCodecFECStatus() failed to set FEC state");
2566 return -1;
2567 }
2568 return 0;
2569}
2570
2571bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002572 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002573}
2574
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002575void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2576 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002577 // If pacing is enabled we always store packets.
2578 if (!pacing_enabled_)
2579 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002580 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002581 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002582 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002583 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002584 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002585}
2586
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002587// Called when we are missing one or more packets.
2588int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002589 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2590}
2591
kwiberg55b97fe2016-01-28 05:22:45 -08002592uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) {
2593 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2594 "Channel::Demultiplex()");
2595 _audioFrame.CopyFrom(audioFrame);
2596 _audioFrame.id_ = _channelId;
2597 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002598}
2599
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002600void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00002601 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07002602 size_t number_of_frames,
Peter Kasting69558702016-01-12 16:26:35 -08002603 size_t number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002604 CodecInst codec;
2605 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002606
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002607 // Never upsample or upmix the capture signal here. This should be done at the
2608 // end of the send chain.
2609 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2610 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
2611 RemixAndResample(audio_data, number_of_frames, number_of_channels,
2612 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002613}
2614
kwiberg55b97fe2016-01-28 05:22:45 -08002615uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) {
2616 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2617 "Channel::PrepareEncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002618
kwiberg55b97fe2016-01-28 05:22:45 -08002619 if (_audioFrame.samples_per_channel_ == 0) {
2620 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2621 "Channel::PrepareEncodeAndSend() invalid audio frame");
2622 return 0xFFFFFFFF;
2623 }
2624
2625 if (channel_state_.Get().input_file_playing) {
2626 MixOrReplaceAudioWithFile(mixingFrequency);
2627 }
2628
solenberg1c2af8e2016-03-24 10:36:00 -07002629 bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock.
2630 AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002631
kwiberg55b97fe2016-01-28 05:22:45 -08002632 if (_includeAudioLevelIndication) {
2633 size_t length =
2634 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
Tommi60c4e0a2016-05-26 21:35:27 +02002635 RTC_CHECK_LE(length, sizeof(_audioFrame.data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002636 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002637 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002638 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002639 rms_level_.Analyze(
2640 rtc::ArrayView<const int16_t>(_audioFrame.data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002641 }
kwiberg55b97fe2016-01-28 05:22:45 -08002642 }
solenberg1c2af8e2016-03-24 10:36:00 -07002643 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002644
kwiberg55b97fe2016-01-28 05:22:45 -08002645 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002646}
2647
kwiberg55b97fe2016-01-28 05:22:45 -08002648uint32_t Channel::EncodeAndSend() {
2649 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
2650 "Channel::EncodeAndSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002651
kwiberg55b97fe2016-01-28 05:22:45 -08002652 assert(_audioFrame.num_channels_ <= 2);
2653 if (_audioFrame.samples_per_channel_ == 0) {
2654 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2655 "Channel::EncodeAndSend() invalid audio frame");
2656 return 0xFFFFFFFF;
2657 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002658
kwiberg55b97fe2016-01-28 05:22:45 -08002659 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00002660
kwiberg55b97fe2016-01-28 05:22:45 -08002661 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002662
kwiberg55b97fe2016-01-28 05:22:45 -08002663 // The ACM resamples internally.
2664 _audioFrame.timestamp_ = _timeStamp;
2665 // This call will trigger AudioPacketizationCallback::SendData if encoding
2666 // is done and payload is ready for packetization and transmission.
2667 // Otherwise, it will return without invoking the callback.
2668 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) {
2669 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
2670 "Channel::EncodeAndSend() ACM encoding failed");
2671 return 0xFFFFFFFF;
2672 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002673
kwiberg55b97fe2016-01-28 05:22:45 -08002674 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
2675 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002676}
2677
solenberg7602aab2016-11-14 11:30:07 -08002678void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2679 RTC_DCHECK(!channel.channel() ||
2680 channel.channel()->ChannelId() != _channelId);
2681 rtc::CritScope lock(&assoc_send_channel_lock_);
2682 associate_send_channel_ = channel;
2683}
2684
Minyue2013aec2015-05-13 14:14:42 +02002685void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002686 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002687 Channel* channel = associate_send_channel_.channel();
2688 if (channel && channel->ChannelId() == channel_id) {
2689 // If this channel is associated with a send channel of the specified
2690 // Channel ID, disassociate with it.
2691 ChannelOwner ref(NULL);
2692 associate_send_channel_ = ref;
2693 }
2694}
2695
ivoc14d5dbe2016-07-04 07:06:55 -07002696void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2697 event_log_proxy_->SetEventLog(event_log);
2698}
2699
michaelt9332b7d2016-11-30 07:51:13 -08002700void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2701 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2702}
2703
nisse284542b2017-01-10 08:58:32 -08002704void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08002705 size_t overhead_per_packet =
2706 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08002707 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2708 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08002709 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08002710 }
2711 });
2712}
2713
2714void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002715 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002716 transport_overhead_per_packet_ = transport_overhead_per_packet;
2717 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002718}
2719
hbos3fd31fe2017-02-28 05:43:16 -08002720// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08002721void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002722 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002723 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2724 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002725}
2726
kwiberg55b97fe2016-01-28 05:22:45 -08002727int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2728 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002729}
2730
wu@webrtc.org24301a62013-12-13 19:17:43 +00002731void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2732 audio_coding_->GetDecodingCallStatistics(stats);
2733}
2734
solenberg358057b2015-11-27 10:46:42 -08002735uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002736 rtc::CritScope lock(&video_sync_lock_);
2737 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002738}
2739
kwiberg55b97fe2016-01-28 05:22:45 -08002740int Channel::SetMinimumPlayoutDelay(int delayMs) {
2741 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2742 "Channel::SetMinimumPlayoutDelay()");
2743 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2744 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2745 _engineStatisticsPtr->SetLastError(
2746 VE_INVALID_ARGUMENT, kTraceError,
2747 "SetMinimumPlayoutDelay() invalid min delay");
2748 return -1;
2749 }
2750 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2751 _engineStatisticsPtr->SetLastError(
2752 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2753 "SetMinimumPlayoutDelay() failed to set min playout delay");
2754 return -1;
2755 }
2756 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002757}
2758
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002759int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002760 uint32_t playout_timestamp_rtp = 0;
2761 {
tommi31fc21f2016-01-21 10:37:37 -08002762 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002763 playout_timestamp_rtp = playout_timestamp_rtp_;
2764 }
kwiberg55b97fe2016-01-28 05:22:45 -08002765 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002766 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002767 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002768 "GetPlayoutTimestamp() failed to retrieve timestamp");
2769 return -1;
2770 }
deadbeef74375882015-08-13 12:09:10 -07002771 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002772 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002773}
2774
kwiberg55b97fe2016-01-28 05:22:45 -08002775int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2776 RtpReceiver** rtp_receiver) const {
2777 *rtpRtcpModule = _rtpRtcpModule.get();
2778 *rtp_receiver = rtp_receiver_.get();
2779 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002780}
2781
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002782// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2783// a shared helper.
kwiberg55b97fe2016-01-28 05:22:45 -08002784int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) {
kwibergb7f89d62016-02-17 10:04:18 -08002785 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002786 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002787
kwiberg55b97fe2016-01-28 05:22:45 -08002788 {
2789 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002790
kwiberg5a25d952016-08-17 07:31:12 -07002791 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002792 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2793 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2794 " doesnt exist");
2795 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002796 }
2797
kwiberg4ec01d92016-08-22 08:43:54 -07002798 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002799 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002800 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2801 "Channel::MixOrReplaceAudioWithFile() file mixing "
2802 "failed");
2803 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002804 }
kwiberg55b97fe2016-01-28 05:22:45 -08002805 if (fileSamples == 0) {
2806 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2807 "Channel::MixOrReplaceAudioWithFile() file is ended");
2808 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002809 }
kwiberg55b97fe2016-01-28 05:22:45 -08002810 }
2811
2812 assert(_audioFrame.samples_per_channel_ == fileSamples);
2813
2814 if (_mixFileWithMicrophone) {
2815 // Currently file stream is always mono.
2816 // TODO(xians): Change the code when FilePlayer supports real stereo.
2817 MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(),
2818 1, fileSamples);
2819 } else {
2820 // Replace ACM audio with file.
2821 // Currently file stream is always mono.
2822 // TODO(xians): Change the code when FilePlayer supports real stereo.
2823 _audioFrame.UpdateFrame(
2824 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2825 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2826 }
2827 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002828}
2829
kwiberg55b97fe2016-01-28 05:22:45 -08002830int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2831 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002832
kwibergb7f89d62016-02-17 10:04:18 -08002833 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002834 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002835
kwiberg55b97fe2016-01-28 05:22:45 -08002836 {
2837 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002838
kwiberg5a25d952016-08-17 07:31:12 -07002839 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002840 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2841 "Channel::MixAudioWithFile() file mixing failed");
2842 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002843 }
2844
kwiberg55b97fe2016-01-28 05:22:45 -08002845 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002846 if (output_file_player_->Get10msAudioFromFile(
2847 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002848 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2849 "Channel::MixAudioWithFile() file mixing failed");
2850 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002851 }
kwiberg55b97fe2016-01-28 05:22:45 -08002852 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002853
kwiberg55b97fe2016-01-28 05:22:45 -08002854 if (audioFrame.samples_per_channel_ == fileSamples) {
2855 // Currently file stream is always mono.
2856 // TODO(xians): Change the code when FilePlayer supports real stereo.
2857 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
2858 fileSamples);
2859 } else {
2860 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2861 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2862 ") != "
2863 "fileSamples(%" PRIuS ")",
2864 audioFrame.samples_per_channel_, fileSamples);
2865 return -1;
2866 }
2867
2868 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002869}
2870
deadbeef74375882015-08-13 12:09:10 -07002871void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002872 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002873
henrik.lundin96bd5022016-04-06 04:13:56 -07002874 if (!jitter_buffer_playout_timestamp_) {
2875 // This can happen if this channel has not received any RTP packets. In
2876 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07002877 return;
2878 }
2879
2880 uint16_t delay_ms = 0;
2881 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002882 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002883 "Channel::UpdatePlayoutTimestamp() failed to read playout"
2884 " delay from the ADM");
2885 _engineStatisticsPtr->SetLastError(
2886 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
2887 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
2888 return;
2889 }
2890
henrik.lundin96bd5022016-04-06 04:13:56 -07002891 RTC_DCHECK(jitter_buffer_playout_timestamp_);
2892 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07002893
2894 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07002895 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07002896
kwiberg55b97fe2016-01-28 05:22:45 -08002897 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002898 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07002899 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07002900
2901 {
tommi31fc21f2016-01-21 10:37:37 -08002902 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08002903 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002904 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07002905 }
2906 playout_delay_ms_ = delay_ms;
2907 }
2908}
2909
kwiberg55b97fe2016-01-28 05:22:45 -08002910void Channel::RegisterReceiveCodecsToRTPModule() {
2911 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2912 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00002913
kwiberg55b97fe2016-01-28 05:22:45 -08002914 CodecInst codec;
2915 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002916
kwiberg55b97fe2016-01-28 05:22:45 -08002917 for (int idx = 0; idx < nSupportedCodecs; idx++) {
2918 // Open up the RTP/RTCP receiver for all supported codecs
2919 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08002920 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08002921 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2922 "Channel::RegisterReceiveCodecsToRTPModule() unable"
2923 " to register %s (%d/%d/%" PRIuS
2924 "/%d) to RTP/RTCP "
2925 "receiver",
2926 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2927 codec.rate);
2928 } else {
2929 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2930 "Channel::RegisterReceiveCodecsToRTPModule() %s "
2931 "(%d/%d/%" PRIuS
2932 "/%d) has been added to the RTP/RTCP "
2933 "receiver",
2934 codec.plname, codec.pltype, codec.plfreq, codec.channels,
2935 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00002936 }
kwiberg55b97fe2016-01-28 05:22:45 -08002937 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002938}
2939
kwiberg55b97fe2016-01-28 05:22:45 -08002940int Channel::SetSendRtpHeaderExtension(bool enable,
2941 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002942 unsigned char id) {
2943 int error = 0;
2944 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
2945 if (enable) {
2946 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
2947 }
2948 return error;
2949}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002950
ossue280cde2016-10-12 11:04:10 -07002951int Channel::GetRtpTimestampRateHz() const {
2952 const auto format = audio_coding_->ReceiveFormat();
2953 // Default to the playout frequency if we've not gotten any packets yet.
2954 // TODO(ossu): Zero clockrate can only happen if we've added an external
2955 // decoder for a format we don't support internally. Remove once that way of
2956 // adding decoders is gone!
2957 return (format && format->clockrate_hz != 0)
2958 ? format->clockrate_hz
2959 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00002960}
2961
Minyue2013aec2015-05-13 14:14:42 +02002962int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07002963 RtcpMode method = _rtpRtcpModule->RTCP();
2964 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002965 return 0;
2966 }
2967 std::vector<RTCPReportBlock> report_blocks;
2968 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02002969
2970 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002971 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02002972 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08002973 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002974 Channel* channel = associate_send_channel_.channel();
2975 // Tries to get RTT from an associated channel. This is important for
2976 // receive-only channels.
2977 if (channel) {
2978 // To prevent infinite recursion and deadlock, calling GetRTT of
2979 // associate channel should always use "false" for argument:
2980 // |allow_associate_channel|.
2981 rtt = channel->GetRTT(false);
2982 }
2983 }
2984 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00002985 }
2986
2987 uint32_t remoteSSRC = rtp_receiver_->SSRC();
2988 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
2989 for (; it != report_blocks.end(); ++it) {
2990 if (it->remoteSSRC == remoteSSRC)
2991 break;
2992 }
2993 if (it == report_blocks.end()) {
2994 // We have not received packets with SSRC matching the report blocks.
2995 // To calculate RTT we try with the SSRC of the first report block.
2996 // This is very important for send-only channels where we don't know
2997 // the SSRC of the other end.
2998 remoteSSRC = report_blocks[0].remoteSSRC;
2999 }
Minyue2013aec2015-05-13 14:14:42 +02003000
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003001 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003002 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003003 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003004 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3005 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003006 return 0;
3007 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003008 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003009}
3010
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003011} // namespace voe
3012} // namespace webrtc