blob: b5e1cad73b62c767e949f2b08821410ec617f384 [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"
henrikaec6fbd22017-03-31 05:43:36 -070024#include "webrtc/base/task_queue.h"
25#include "webrtc/base/thread_checker.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000026#include "webrtc/base/timeutils.h"
nisseb8f9a322017-03-27 05:36:15 -070027#include "webrtc/call/rtp_transport_controller_send.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020028#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070029#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070030#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000031#include "webrtc/modules/audio_device/include/audio_device.h"
32#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010033#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010034#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010035#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
36#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
37#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
nisse657bab22017-02-21 06:28:10 -080038#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000039#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010040#include "webrtc/modules/utility/include/process_thread.h"
elad.alon28770482017-03-28 05:03:55 -070041#include "webrtc/system_wrappers/include/field_trial.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010042#include "webrtc/system_wrappers/include/trace.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000043#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
44#include "webrtc/voice_engine/output_mixer.h"
45#include "webrtc/voice_engine/statistics.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000046#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000047
andrew@webrtc.org50419b02012-11-14 19:07:54 +000048namespace webrtc {
49namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000050
kwibergc8d071e2016-04-06 12:22:38 -070051namespace {
52
Erik Språng737336d2016-07-29 12:59:36 +020053constexpr int64_t kMaxRetransmissionWindowMs = 1000;
54constexpr int64_t kMinRetransmissionWindowMs = 30;
55
kwibergc8d071e2016-04-06 12:22:38 -070056} // namespace
57
solenberg8842c3e2016-03-11 03:06:41 -080058const int kTelephoneEventAttenuationdB = 10;
59
ivoc14d5dbe2016-07-04 07:06:55 -070060class RtcEventLogProxy final : public webrtc::RtcEventLog {
61 public:
62 RtcEventLogProxy() : event_log_(nullptr) {}
63
64 bool StartLogging(const std::string& file_name,
65 int64_t max_size_bytes) override {
66 RTC_NOTREACHED();
67 return false;
68 }
69
70 bool StartLogging(rtc::PlatformFile log_file,
71 int64_t max_size_bytes) override {
72 RTC_NOTREACHED();
73 return false;
74 }
75
76 void StopLogging() override { RTC_NOTREACHED(); }
77
78 void LogVideoReceiveStreamConfig(
79 const webrtc::VideoReceiveStream::Config& config) override {
80 rtc::CritScope lock(&crit_);
81 if (event_log_) {
82 event_log_->LogVideoReceiveStreamConfig(config);
83 }
84 }
85
86 void LogVideoSendStreamConfig(
87 const webrtc::VideoSendStream::Config& config) override {
88 rtc::CritScope lock(&crit_);
89 if (event_log_) {
90 event_log_->LogVideoSendStreamConfig(config);
91 }
92 }
93
ivoce0928d82016-10-10 05:12:51 -070094 void LogAudioReceiveStreamConfig(
95 const webrtc::AudioReceiveStream::Config& config) override {
96 rtc::CritScope lock(&crit_);
97 if (event_log_) {
98 event_log_->LogAudioReceiveStreamConfig(config);
99 }
100 }
101
102 void LogAudioSendStreamConfig(
103 const webrtc::AudioSendStream::Config& config) override {
104 rtc::CritScope lock(&crit_);
105 if (event_log_) {
106 event_log_->LogAudioSendStreamConfig(config);
107 }
108 }
109
ivoc14d5dbe2016-07-04 07:06:55 -0700110 void LogRtpHeader(webrtc::PacketDirection direction,
111 webrtc::MediaType media_type,
112 const uint8_t* header,
113 size_t packet_length) override {
philipel32d00102017-02-27 02:18:46 -0800114 LogRtpHeader(direction, media_type, header, packet_length,
115 PacedPacketInfo::kNotAProbe);
116 }
117
118 void LogRtpHeader(webrtc::PacketDirection direction,
119 webrtc::MediaType media_type,
120 const uint8_t* header,
121 size_t packet_length,
122 int probe_cluster_id) override {
ivoc14d5dbe2016-07-04 07:06:55 -0700123 rtc::CritScope lock(&crit_);
124 if (event_log_) {
philipel32d00102017-02-27 02:18:46 -0800125 event_log_->LogRtpHeader(direction, media_type, header, packet_length,
126 probe_cluster_id);
ivoc14d5dbe2016-07-04 07:06:55 -0700127 }
128 }
129
130 void LogRtcpPacket(webrtc::PacketDirection direction,
131 webrtc::MediaType media_type,
132 const uint8_t* packet,
133 size_t length) override {
134 rtc::CritScope lock(&crit_);
135 if (event_log_) {
136 event_log_->LogRtcpPacket(direction, media_type, packet, length);
137 }
138 }
139
140 void LogAudioPlayout(uint32_t ssrc) override {
141 rtc::CritScope lock(&crit_);
142 if (event_log_) {
143 event_log_->LogAudioPlayout(ssrc);
144 }
145 }
146
terelius424e6cf2017-02-20 05:14:41 -0800147 void LogLossBasedBweUpdate(int32_t bitrate_bps,
ivoc14d5dbe2016-07-04 07:06:55 -0700148 uint8_t fraction_loss,
149 int32_t total_packets) override {
150 rtc::CritScope lock(&crit_);
151 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800152 event_log_->LogLossBasedBweUpdate(bitrate_bps, fraction_loss,
153 total_packets);
ivoc14d5dbe2016-07-04 07:06:55 -0700154 }
155 }
156
terelius424e6cf2017-02-20 05:14:41 -0800157 void LogDelayBasedBweUpdate(int32_t bitrate_bps,
terelius0baf55d2017-02-17 03:38:28 -0800158 BandwidthUsage detector_state) override {
159 rtc::CritScope lock(&crit_);
160 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800161 event_log_->LogDelayBasedBweUpdate(bitrate_bps, detector_state);
terelius0baf55d2017-02-17 03:38:28 -0800162 }
163 }
164
minyue4b7c9522017-01-24 04:54:59 -0800165 void LogAudioNetworkAdaptation(
michaeltcde46b72017-04-06 05:59:10 -0700166 const AudioEncoderRuntimeConfig& config) override {
minyue4b7c9522017-01-24 04:54:59 -0800167 rtc::CritScope lock(&crit_);
168 if (event_log_) {
169 event_log_->LogAudioNetworkAdaptation(config);
170 }
171 }
172
philipel32d00102017-02-27 02:18:46 -0800173 void LogProbeClusterCreated(int id,
174 int bitrate_bps,
175 int min_probes,
176 int min_bytes) override {
177 rtc::CritScope lock(&crit_);
178 if (event_log_) {
179 event_log_->LogProbeClusterCreated(id, bitrate_bps, min_probes,
180 min_bytes);
181 }
182 };
183
184 void LogProbeResultSuccess(int id, int bitrate_bps) override {
185 rtc::CritScope lock(&crit_);
186 if (event_log_) {
187 event_log_->LogProbeResultSuccess(id, bitrate_bps);
188 }
189 };
190
191 void LogProbeResultFailure(int id,
192 ProbeFailureReason failure_reason) override {
193 rtc::CritScope lock(&crit_);
194 if (event_log_) {
195 event_log_->LogProbeResultFailure(id, failure_reason);
196 }
197 };
198
ivoc14d5dbe2016-07-04 07:06:55 -0700199 void SetEventLog(RtcEventLog* event_log) {
200 rtc::CritScope lock(&crit_);
201 event_log_ = event_log;
202 }
203
204 private:
205 rtc::CriticalSection crit_;
206 RtcEventLog* event_log_ GUARDED_BY(crit_);
207 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
208};
209
michaelt9332b7d2016-11-30 07:51:13 -0800210class RtcpRttStatsProxy final : public RtcpRttStats {
211 public:
212 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
213
214 void OnRttUpdate(int64_t rtt) override {
215 rtc::CritScope lock(&crit_);
216 if (rtcp_rtt_stats_)
217 rtcp_rtt_stats_->OnRttUpdate(rtt);
218 }
219
220 int64_t LastProcessedRtt() const override {
221 rtc::CritScope lock(&crit_);
222 if (!rtcp_rtt_stats_)
223 return 0;
224 return rtcp_rtt_stats_->LastProcessedRtt();
225 }
226
227 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
228 rtc::CritScope lock(&crit_);
229 rtcp_rtt_stats_ = rtcp_rtt_stats;
230 }
231
232 private:
233 rtc::CriticalSection crit_;
234 RtcpRttStats* rtcp_rtt_stats_ GUARDED_BY(crit_);
235 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
236};
237
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100238class TransportFeedbackProxy : public TransportFeedbackObserver {
239 public:
240 TransportFeedbackProxy() : feedback_observer_(nullptr) {
241 pacer_thread_.DetachFromThread();
242 network_thread_.DetachFromThread();
243 }
244
245 void SetTransportFeedbackObserver(
246 TransportFeedbackObserver* feedback_observer) {
247 RTC_DCHECK(thread_checker_.CalledOnValidThread());
248 rtc::CritScope lock(&crit_);
249 feedback_observer_ = feedback_observer;
250 }
251
252 // Implements TransportFeedbackObserver.
elad.alond12a8e12017-03-23 11:04:48 -0700253 void AddPacket(uint32_t ssrc,
254 uint16_t sequence_number,
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100255 size_t length,
philipel8aadd502017-02-23 02:56:13 -0800256 const PacedPacketInfo& pacing_info) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100257 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
258 rtc::CritScope lock(&crit_);
259 if (feedback_observer_)
elad.alond12a8e12017-03-23 11:04:48 -0700260 feedback_observer_->AddPacket(ssrc, sequence_number, length, pacing_info);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100261 }
philipel8aadd502017-02-23 02:56:13 -0800262
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100263 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
264 RTC_DCHECK(network_thread_.CalledOnValidThread());
265 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700266 if (feedback_observer_)
267 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200268 }
elad.alonf9490002017-03-06 05:32:21 -0800269 std::vector<PacketFeedback> GetTransportFeedbackVector() const override {
Stefan Holmer60e43462016-09-07 09:58:20 +0200270 RTC_NOTREACHED();
elad.alonf9490002017-03-06 05:32:21 -0800271 return std::vector<PacketFeedback>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100272 }
273
274 private:
275 rtc::CriticalSection crit_;
276 rtc::ThreadChecker thread_checker_;
277 rtc::ThreadChecker pacer_thread_;
278 rtc::ThreadChecker network_thread_;
279 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
280};
281
282class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
283 public:
284 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
285 pacer_thread_.DetachFromThread();
286 }
287
288 void SetSequenceNumberAllocator(
289 TransportSequenceNumberAllocator* seq_num_allocator) {
290 RTC_DCHECK(thread_checker_.CalledOnValidThread());
291 rtc::CritScope lock(&crit_);
292 seq_num_allocator_ = seq_num_allocator;
293 }
294
295 // Implements TransportSequenceNumberAllocator.
296 uint16_t AllocateSequenceNumber() override {
297 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
298 rtc::CritScope lock(&crit_);
299 if (!seq_num_allocator_)
300 return 0;
301 return seq_num_allocator_->AllocateSequenceNumber();
302 }
303
304 private:
305 rtc::CriticalSection crit_;
306 rtc::ThreadChecker thread_checker_;
307 rtc::ThreadChecker pacer_thread_;
308 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
309};
310
311class RtpPacketSenderProxy : public RtpPacketSender {
312 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800313 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100314
315 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
316 RTC_DCHECK(thread_checker_.CalledOnValidThread());
317 rtc::CritScope lock(&crit_);
318 rtp_packet_sender_ = rtp_packet_sender;
319 }
320
321 // Implements RtpPacketSender.
322 void InsertPacket(Priority priority,
323 uint32_t ssrc,
324 uint16_t sequence_number,
325 int64_t capture_time_ms,
326 size_t bytes,
327 bool retransmission) override {
328 rtc::CritScope lock(&crit_);
329 if (rtp_packet_sender_) {
330 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
331 capture_time_ms, bytes, retransmission);
332 }
333 }
334
335 private:
336 rtc::ThreadChecker thread_checker_;
337 rtc::CriticalSection crit_;
338 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
339};
340
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000341class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000342 public:
stefan7de8d642017-02-07 07:14:08 -0800343 explicit VoERtcpObserver(Channel* owner)
344 : owner_(owner), bandwidth_observer_(nullptr) {}
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000345 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000346
stefan7de8d642017-02-07 07:14:08 -0800347 void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
348 rtc::CritScope lock(&crit_);
349 bandwidth_observer_ = bandwidth_observer;
350 }
351
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000352 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
stefan7de8d642017-02-07 07:14:08 -0800353 rtc::CritScope lock(&crit_);
354 if (bandwidth_observer_) {
355 bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
356 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000357 }
358
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000359 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
360 int64_t rtt,
361 int64_t now_ms) override {
stefan7de8d642017-02-07 07:14:08 -0800362 {
363 rtc::CritScope lock(&crit_);
364 if (bandwidth_observer_) {
365 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
366 now_ms);
367 }
368 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000369 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
370 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
371 // report for VoiceEngine?
372 if (report_blocks.empty())
373 return;
374
375 int fraction_lost_aggregate = 0;
376 int total_number_of_packets = 0;
377
378 // If receiving multiple report blocks, calculate the weighted average based
379 // on the number of packets a report refers to.
380 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
381 block_it != report_blocks.end(); ++block_it) {
382 // Find the previous extended high sequence number for this remote SSRC,
383 // to calculate the number of RTP packets this report refers to. Ignore if
384 // we haven't seen this SSRC before.
385 std::map<uint32_t, uint32_t>::iterator seq_num_it =
386 extended_max_sequence_number_.find(block_it->sourceSSRC);
387 int number_of_packets = 0;
388 if (seq_num_it != extended_max_sequence_number_.end()) {
389 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
390 }
391 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
392 total_number_of_packets += number_of_packets;
393
394 extended_max_sequence_number_[block_it->sourceSSRC] =
395 block_it->extendedHighSeqNum;
396 }
397 int weighted_fraction_lost = 0;
398 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800399 weighted_fraction_lost =
400 (fraction_lost_aggregate + total_number_of_packets / 2) /
401 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000402 }
elad.alond12a8e12017-03-23 11:04:48 -0700403 owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000404 }
405
406 private:
407 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000408 // Maps remote side ssrc to extended highest sequence number received.
409 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800410 rtc::CriticalSection crit_;
411 RtcpBandwidthObserver* bandwidth_observer_ GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000412};
413
henrikaec6fbd22017-03-31 05:43:36 -0700414class Channel::ProcessAndEncodeAudioTask : public rtc::QueuedTask {
415 public:
416 ProcessAndEncodeAudioTask(std::unique_ptr<AudioFrame> audio_frame,
417 Channel* channel)
418 : audio_frame_(std::move(audio_frame)), channel_(channel) {
419 RTC_DCHECK(channel_);
420 }
421
422 private:
423 bool Run() override {
424 RTC_DCHECK_RUN_ON(channel_->encoder_queue_);
425 channel_->ProcessAndEncodeAudioOnTaskQueue(audio_frame_.get());
426 return true;
427 }
428
429 std::unique_ptr<AudioFrame> audio_frame_;
430 Channel* const channel_;
431};
432
kwiberg55b97fe2016-01-28 05:22:45 -0800433int32_t Channel::SendData(FrameType frameType,
434 uint8_t payloadType,
435 uint32_t timeStamp,
436 const uint8_t* payloadData,
437 size_t payloadSize,
438 const RTPFragmentationHeader* fragmentation) {
henrikaec6fbd22017-03-31 05:43:36 -0700439 RTC_DCHECK_RUN_ON(encoder_queue_);
kwiberg55b97fe2016-01-28 05:22:45 -0800440 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
441 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
442 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
443 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000444
kwiberg55b97fe2016-01-28 05:22:45 -0800445 if (_includeAudioLevelIndication) {
446 // Store current audio level in the RTP/RTCP module.
447 // The level will be used in combination with voice-activity state
448 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800449 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800450 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000451
kwiberg55b97fe2016-01-28 05:22:45 -0800452 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
453 // packetization.
454 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700455 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800456 (FrameType&)frameType, payloadType, timeStamp,
457 // Leaving the time when this frame was
458 // received from the capture device as
459 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700460 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800461 _engineStatisticsPtr->SetLastError(
462 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
463 "Channel::SendData() failed to send data to RTP/RTCP module");
464 return -1;
465 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000466
kwiberg55b97fe2016-01-28 05:22:45 -0800467 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000468}
469
stefan1d8a5062015-10-02 03:39:33 -0700470bool Channel::SendRtp(const uint8_t* data,
471 size_t len,
472 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800473 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
474 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000475
kwiberg55b97fe2016-01-28 05:22:45 -0800476 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000477
kwiberg55b97fe2016-01-28 05:22:45 -0800478 if (_transportPtr == NULL) {
479 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
480 "Channel::SendPacket() failed to send RTP packet due to"
481 " invalid transport object");
482 return false;
483 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000484
kwiberg55b97fe2016-01-28 05:22:45 -0800485 uint8_t* bufferToSendPtr = (uint8_t*)data;
486 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000487
kwiberg55b97fe2016-01-28 05:22:45 -0800488 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
489 std::string transport_name =
490 _externalTransport ? "external transport" : "WebRtc sockets";
491 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
492 "Channel::SendPacket() RTP transmission using %s failed",
493 transport_name.c_str());
494 return false;
495 }
496 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000497}
498
kwiberg55b97fe2016-01-28 05:22:45 -0800499bool Channel::SendRtcp(const uint8_t* data, size_t len) {
500 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
501 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000502
kwiberg55b97fe2016-01-28 05:22:45 -0800503 rtc::CritScope cs(&_callbackCritSect);
504 if (_transportPtr == NULL) {
505 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
506 "Channel::SendRtcp() failed to send RTCP packet"
507 " due to invalid transport object");
508 return false;
509 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000510
kwiberg55b97fe2016-01-28 05:22:45 -0800511 uint8_t* bufferToSendPtr = (uint8_t*)data;
512 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000513
kwiberg55b97fe2016-01-28 05:22:45 -0800514 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
515 if (n < 0) {
516 std::string transport_name =
517 _externalTransport ? "external transport" : "WebRtc sockets";
518 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
519 "Channel::SendRtcp() transmission using %s failed",
520 transport_name.c_str());
521 return false;
522 }
523 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000524}
525
kwiberg55b97fe2016-01-28 05:22:45 -0800526void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
527 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
528 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000529
kwiberg55b97fe2016-01-28 05:22:45 -0800530 // Update ssrc so that NTP for AV sync can be updated.
531 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000532}
533
Peter Boströmac547a62015-09-17 23:03:57 +0200534void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
535 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
536 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
537 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000538}
539
Peter Boströmac547a62015-09-17 23:03:57 +0200540int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000541 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000542 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000543 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800544 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200545 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800546 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
547 "Channel::OnInitializeDecoder(payloadType=%d, "
548 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
549 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000550
kwiberg55b97fe2016-01-28 05:22:45 -0800551 CodecInst receiveCodec = {0};
552 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000553
kwiberg55b97fe2016-01-28 05:22:45 -0800554 receiveCodec.pltype = payloadType;
555 receiveCodec.plfreq = frequency;
556 receiveCodec.channels = channels;
557 receiveCodec.rate = rate;
558 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000559
kwiberg55b97fe2016-01-28 05:22:45 -0800560 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
561 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000562
kwiberg55b97fe2016-01-28 05:22:45 -0800563 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700564 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
565 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800566 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
567 "Channel::OnInitializeDecoder() invalid codec ("
568 "pt=%d, name=%s) received - 1",
569 payloadType, payloadName);
570 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
571 return -1;
572 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000573
kwiberg55b97fe2016-01-28 05:22:45 -0800574 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000575}
576
kwiberg55b97fe2016-01-28 05:22:45 -0800577int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
578 size_t payloadSize,
579 const WebRtcRTPHeader* rtpHeader) {
580 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
581 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
582 ","
583 " payloadType=%u, audioChannel=%" PRIuS ")",
584 payloadSize, rtpHeader->header.payloadType,
585 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000586
kwiberg55b97fe2016-01-28 05:22:45 -0800587 if (!channel_state_.Get().playing) {
588 // Avoid inserting into NetEQ when we are not playing. Count the
589 // packet as discarded.
590 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
591 "received packet is discarded since playing is not"
592 " activated");
niklase@google.com470e71d2011-07-07 08:21:25 +0000593 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800594 }
595
596 // Push the incoming payload (parsed and ready for decoding) into the ACM
597 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
598 0) {
599 _engineStatisticsPtr->SetLastError(
600 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
601 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
602 return -1;
603 }
604
kwiberg55b97fe2016-01-28 05:22:45 -0800605 int64_t round_trip_time = 0;
606 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
607 NULL);
608
609 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
610 if (!nack_list.empty()) {
611 // Can't use nack_list.data() since it's not supported by all
612 // compilers.
613 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
614 }
615 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000616}
617
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000618bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000619 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000620 RTPHeader header;
621 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
622 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
623 "IncomingPacket invalid RTP header");
624 return false;
625 }
626 header.payload_type_frequency =
627 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
628 if (header.payload_type_frequency < 0)
629 return false;
630 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
631}
632
henrik.lundin42dda502016-05-18 05:36:01 -0700633MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
634 int32_t id,
635 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700636 unsigned int ssrc;
nisse7d59f6b2017-02-21 03:40:24 -0800637 RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
ivoc14d5dbe2016-07-04 07:06:55 -0700638 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800639 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700640 bool muted;
641 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
642 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800643 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
644 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
645 // In all likelihood, the audio in this frame is garbage. We return an
646 // error so that the audio mixer module doesn't add it to the mix. As
647 // a result, it won't be played out and the actions skipped here are
648 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700649 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800650 }
henrik.lundina89ab962016-05-18 08:52:45 -0700651
652 if (muted) {
653 // TODO(henrik.lundin): We should be able to do better than this. But we
654 // will have to go through all the cases below where the audio samples may
655 // be used, and handle the muted case in some way.
aleloi6321b492016-12-05 01:46:09 -0800656 AudioFrameOperations::Mute(audioFrame);
henrik.lundina89ab962016-05-18 08:52:45 -0700657 }
kwiberg55b97fe2016-01-28 05:22:45 -0800658
kwiberg55b97fe2016-01-28 05:22:45 -0800659 // Convert module ID to internal VoE channel ID
660 audioFrame->id_ = VoEChannelId(audioFrame->id_);
661 // Store speech type for dead-or-alive detection
662 _outputSpeechType = audioFrame->speech_type_;
663
664 ChannelState::State state = channel_state_.Get();
665
kwiberg55b97fe2016-01-28 05:22:45 -0800666 {
667 // Pass the audio buffers to an optional sink callback, before applying
668 // scaling/panning, as that applies to the mix operation.
669 // External recipients of the audio (e.g. via AudioTrack), will do their
670 // own mixing/dynamic processing.
671 rtc::CritScope cs(&_callbackCritSect);
672 if (audio_sink_) {
673 AudioSinkInterface::Data data(
674 &audioFrame->data_[0], audioFrame->samples_per_channel_,
675 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
676 audioFrame->timestamp_);
677 audio_sink_->OnData(data);
678 }
679 }
680
681 float output_gain = 1.0f;
kwiberg55b97fe2016-01-28 05:22:45 -0800682 {
683 rtc::CritScope cs(&volume_settings_critsect_);
684 output_gain = _outputGain;
kwiberg55b97fe2016-01-28 05:22:45 -0800685 }
686
687 // Output volume scaling
688 if (output_gain < 0.99f || output_gain > 1.01f) {
solenberg8d73f8c2017-03-08 01:52:20 -0800689 // TODO(solenberg): Combine with mute state - this can cause clicks!
oprypin67fdb802017-03-09 06:25:06 -0800690 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800691 }
692
kwiberg55b97fe2016-01-28 05:22:45 -0800693 // Mix decoded PCM output with file if file mixing is enabled
694 if (state.output_file_playing) {
695 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700696 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800697 }
698
kwiberg55b97fe2016-01-28 05:22:45 -0800699 // Record playout if enabled
700 {
701 rtc::CritScope cs(&_fileCritSect);
702
kwiberg5a25d952016-08-17 07:31:12 -0700703 if (_outputFileRecording && output_file_recorder_) {
704 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800705 }
706 }
707
708 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700709 // TODO(henrik.lundin) Use the |muted| information here too.
kwiberg55b97fe2016-01-28 05:22:45 -0800710 _outputAudioLevel.ComputeLevel(*audioFrame);
711
712 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
713 // The first frame with a valid rtp timestamp.
714 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
715 }
716
717 if (capture_start_rtp_time_stamp_ >= 0) {
718 // audioFrame.timestamp_ should be valid from now on.
719
720 // Compute elapsed time.
721 int64_t unwrap_timestamp =
722 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
723 audioFrame->elapsed_time_ms_ =
724 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700725 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800726
niklase@google.com470e71d2011-07-07 08:21:25 +0000727 {
kwiberg55b97fe2016-01-28 05:22:45 -0800728 rtc::CritScope lock(&ts_stats_lock_);
729 // Compute ntp time.
730 audioFrame->ntp_time_ms_ =
731 ntp_estimator_.Estimate(audioFrame->timestamp_);
732 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
733 if (audioFrame->ntp_time_ms_ > 0) {
734 // Compute |capture_start_ntp_time_ms_| so that
735 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
736 capture_start_ntp_time_ms_ =
737 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000738 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000739 }
kwiberg55b97fe2016-01-28 05:22:45 -0800740 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000741
henrik.lundin42dda502016-05-18 05:36:01 -0700742 return muted ? MixerParticipant::AudioFrameInfo::kMuted
743 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000744}
745
aleloi6c278492016-10-20 14:24:39 -0700746AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
747 int sample_rate_hz,
748 AudioFrame* audio_frame) {
749 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700750
aleloi6c278492016-10-20 14:24:39 -0700751 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700752
753 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
754 FrameInfo new_audio_frame_info = FrameInfo::kError;
755 switch (frame_info) {
756 case MixerParticipant::AudioFrameInfo::kNormal:
757 new_audio_frame_info = FrameInfo::kNormal;
758 break;
759 case MixerParticipant::AudioFrameInfo::kMuted:
760 new_audio_frame_info = FrameInfo::kMuted;
761 break;
762 case MixerParticipant::AudioFrameInfo::kError:
763 new_audio_frame_info = FrameInfo::kError;
764 break;
765 }
aleloi6c278492016-10-20 14:24:39 -0700766 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700767}
768
kwiberg55b97fe2016-01-28 05:22:45 -0800769int32_t Channel::NeededFrequency(int32_t id) const {
770 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
771 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000772
kwiberg55b97fe2016-01-28 05:22:45 -0800773 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000774
kwiberg55b97fe2016-01-28 05:22:45 -0800775 // Determine highest needed receive frequency
776 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000777
kwiberg55b97fe2016-01-28 05:22:45 -0800778 // Return the bigger of playout and receive frequency in the ACM.
779 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
780 highestNeeded = audio_coding_->PlayoutFrequency();
781 } else {
782 highestNeeded = receiveFrequency;
783 }
784
785 // Special case, if we're playing a file on the playout side
786 // we take that frequency into consideration as well
787 // This is not needed on sending side, since the codec will
788 // limit the spectrum anyway.
789 if (channel_state_.Get().output_file_playing) {
790 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700791 if (output_file_player_) {
792 if (output_file_player_->Frequency() > highestNeeded) {
793 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800794 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000795 }
kwiberg55b97fe2016-01-28 05:22:45 -0800796 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000797
kwiberg55b97fe2016-01-28 05:22:45 -0800798 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000799}
800
henrikaec6fbd22017-03-31 05:43:36 -0700801int32_t Channel::CreateChannel(Channel*& channel,
802 int32_t channelId,
803 uint32_t instanceId,
804 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800805 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
806 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
807 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000808
solenberg88499ec2016-09-07 07:34:41 -0700809 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800810 if (channel == NULL) {
811 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
812 "Channel::CreateChannel() unable to allocate memory for"
813 " channel");
814 return -1;
815 }
816 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000817}
818
kwiberg55b97fe2016-01-28 05:22:45 -0800819void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
820 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
821 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
822 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000823
kwiberg55b97fe2016-01-28 05:22:45 -0800824 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000825}
826
kwiberg55b97fe2016-01-28 05:22:45 -0800827void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
828 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
829 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
830 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000831
kwiberg55b97fe2016-01-28 05:22:45 -0800832 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000833}
834
kwiberg55b97fe2016-01-28 05:22:45 -0800835void Channel::PlayFileEnded(int32_t id) {
836 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
837 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000838
kwiberg55b97fe2016-01-28 05:22:45 -0800839 if (id == _inputFilePlayerId) {
840 channel_state_.SetInputFilePlaying(false);
841 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
842 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000843 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800844 } else if (id == _outputFilePlayerId) {
845 channel_state_.SetOutputFilePlaying(false);
846 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
847 "Channel::PlayFileEnded() => output file player module is"
848 " shutdown");
849 }
850}
851
852void Channel::RecordFileEnded(int32_t id) {
853 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
854 "Channel::RecordFileEnded(id=%d)", id);
855
856 assert(id == _outputFileRecorderId);
857
858 rtc::CritScope cs(&_fileCritSect);
859
860 _outputFileRecording = false;
861 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
862 "Channel::RecordFileEnded() => output file recorder module is"
863 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000864}
865
pbos@webrtc.org92135212013-05-14 08:31:39 +0000866Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000867 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700868 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800869 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100870 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700871 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800872 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100873 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800874 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100875 rtp_receive_statistics_(
876 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
877 rtp_receiver_(
878 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100879 this,
880 this,
881 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700882 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100883 _outputAudioLevel(),
884 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100885 // Avoid conflict with other channels by adding 1024 - 1026,
886 // won't use as much as 1024 channels.
887 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
888 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
889 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
890 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100891 _timeStamp(0), // This is just an offset, RTP module will add it's own
892 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100893 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100894 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100895 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100896 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100897 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
898 capture_start_rtp_time_stamp_(-1),
899 capture_start_ntp_time_ms_(-1),
900 _engineStatisticsPtr(NULL),
901 _outputMixerPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100902 _moduleProcessThreadPtr(NULL),
903 _audioDeviceModulePtr(NULL),
904 _voiceEngineObserverPtr(NULL),
905 _callbackCritSectPtr(NULL),
906 _transportPtr(NULL),
solenberg1c2af8e2016-03-24 10:36:00 -0700907 input_mute_(false),
908 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100909 _outputGain(1.0f),
solenberg8d73f8c2017-03-08 01:52:20 -0800910 _mixFileWithMicrophone(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100911 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800912 transport_overhead_per_packet_(0),
913 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100914 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100915 restored_packet_in_use_(false),
916 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100917 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700918 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800919 feedback_observer_proxy_(new TransportFeedbackProxy()),
920 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700921 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200922 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
923 kMaxRetransmissionWindowMs)),
elad.alond12a8e12017-03-23 11:04:48 -0700924 decoder_factory_(config.acm_config.decoder_factory),
elad.alon28770482017-03-28 05:03:55 -0700925 use_twcc_plr_for_ana_(
926 webrtc::field_trial::FindFullName("UseTwccPlrForAna") == "Enabled") {
kwiberg55b97fe2016-01-28 05:22:45 -0800927 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
928 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700929 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800930 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700931 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800932 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200933
kwiberg55b97fe2016-01-28 05:22:45 -0800934 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000935
kwiberg55b97fe2016-01-28 05:22:45 -0800936 RtpRtcp::Configuration configuration;
937 configuration.audio = true;
938 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800939 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800940 configuration.receive_statistics = rtp_receive_statistics_.get();
941 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800942 if (pacing_enabled_) {
943 configuration.paced_sender = rtp_packet_sender_proxy_.get();
944 configuration.transport_sequence_number_allocator =
945 seq_num_allocator_proxy_.get();
946 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
947 }
ivoc14d5dbe2016-07-04 07:06:55 -0700948 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800949 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200950 configuration.retransmission_rate_limiter =
951 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000952
kwiberg55b97fe2016-01-28 05:22:45 -0800953 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100954 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000955}
956
kwiberg55b97fe2016-01-28 05:22:45 -0800957Channel::~Channel() {
tommi0a2391f2017-03-21 02:31:51 -0700958 RTC_DCHECK(!channel_state_.Get().sending);
959 RTC_DCHECK(!channel_state_.Get().playing);
niklase@google.com470e71d2011-07-07 08:21:25 +0000960}
961
kwiberg55b97fe2016-01-28 05:22:45 -0800962int32_t Channel::Init() {
tommi0a2391f2017-03-21 02:31:51 -0700963 RTC_DCHECK(construction_thread_.CalledOnValidThread());
kwiberg55b97fe2016-01-28 05:22:45 -0800964 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
965 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000966
kwiberg55b97fe2016-01-28 05:22:45 -0800967 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000968
kwiberg55b97fe2016-01-28 05:22:45 -0800969 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000970
kwiberg55b97fe2016-01-28 05:22:45 -0800971 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
972 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
973 "Channel::Init() must call SetEngineInformation() first");
974 return -1;
975 }
976
977 // --- Add modules to process thread (for periodic schedulation)
978
tommidea489f2017-03-03 03:20:24 -0800979 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
kwiberg55b97fe2016-01-28 05:22:45 -0800980
981 // --- ACM initialization
982
983 if (audio_coding_->InitializeReceiver() == -1) {
984 _engineStatisticsPtr->SetLastError(
985 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
986 "Channel::Init() unable to initialize the ACM - 1");
987 return -1;
988 }
989
990 // --- RTP/RTCP module initialization
991
992 // Ensure that RTCP is enabled by default for the created channel.
993 // Note that, the module will keep generating RTCP until it is explicitly
994 // disabled by the user.
995 // After StopListen (when no sockets exists), RTCP packets will no longer
996 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -0700997 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800998 // RTCP is enabled by default.
999 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
1000 // --- Register all permanent callbacks
solenbergfe7dd6d2017-03-11 08:10:43 -08001001 if (audio_coding_->RegisterTransportCallback(this) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001002 _engineStatisticsPtr->SetLastError(
1003 VE_CANNOT_INIT_CHANNEL, kTraceError,
1004 "Channel::Init() callbacks not registered");
1005 return -1;
1006 }
1007
kwiberg1c07c702017-03-27 07:15:49 -07001008 // Register a default set of send codecs.
1009 const int nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
kwiberg55b97fe2016-01-28 05:22:45 -08001010 for (int idx = 0; idx < nSupportedCodecs; idx++) {
kwiberg1c07c702017-03-27 07:15:49 -07001011 CodecInst codec;
1012 RTC_CHECK_EQ(0, audio_coding_->Codec(idx, &codec));
1013
1014 // Ensure that PCMU is used as default send codec.
1015 if (STR_CASE_CMP(codec.plname, "PCMU") == 0 && codec.channels == 1) {
1016 SetSendCodec(codec);
1017 }
1018
1019 // Register default PT for 'telephone-event'
1020 if (STR_CASE_CMP(codec.plname, "telephone-event") == 0) {
1021 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1) {
1022 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1023 "Channel::Init() failed to register outband "
1024 "'telephone-event' (%d/%d) correctly",
1025 codec.pltype, codec.plfreq);
1026 }
1027 }
1028
1029 if (STR_CASE_CMP(codec.plname, "CN") == 0) {
1030 if (!codec_manager_.RegisterEncoder(codec) ||
1031 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
1032 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
1033 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1034 "Channel::Init() failed to register CN (%d/%d) "
1035 "correctly - 1",
1036 codec.pltype, codec.plfreq);
1037 }
1038 }
1039 }
1040
1041 return 0;
1042}
1043
1044void Channel::RegisterLegacyReceiveCodecs() {
1045 const int nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1046 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1047 CodecInst codec;
1048 RTC_CHECK_EQ(0, audio_coding_->Codec(idx, &codec));
1049
kwiberg55b97fe2016-01-28 05:22:45 -08001050 // Open up the RTP/RTCP receiver for all supported codecs
kwiberg1c07c702017-03-27 07:15:49 -07001051 if (rtp_receiver_->RegisterReceivePayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001052 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1053 "Channel::Init() unable to register %s "
1054 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1055 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1056 codec.rate);
1057 } else {
1058 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1059 "Channel::Init() %s (%d/%d/%" PRIuS
1060 "/%d) has been "
1061 "added to the RTP/RTCP receiver",
1062 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1063 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001064 }
1065
kwiberg1c07c702017-03-27 07:15:49 -07001066 // Register default PT for 'telephone-event'
1067 if (STR_CASE_CMP(codec.plname, "telephone-event") == 0) {
1068 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
kwibergda2bf4e2016-10-24 13:47:09 -07001069 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001070 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
kwiberg1c07c702017-03-27 07:15:49 -07001071 "Channel::Init() failed to register inband "
kwiberg55b97fe2016-01-28 05:22:45 -08001072 "'telephone-event' (%d/%d) correctly",
1073 codec.pltype, codec.plfreq);
1074 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001075 }
1076
kwiberg1c07c702017-03-27 07:15:49 -07001077 if (STR_CASE_CMP(codec.plname, "CN") == 0) {
1078 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1079 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001080 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1081 "Channel::Init() failed to register CN (%d/%d) "
1082 "correctly - 1",
1083 codec.pltype, codec.plfreq);
1084 }
1085 }
kwiberg55b97fe2016-01-28 05:22:45 -08001086 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001087}
1088
tommi0a2391f2017-03-21 02:31:51 -07001089void Channel::Terminate() {
1090 RTC_DCHECK(construction_thread_.CalledOnValidThread());
1091 // Must be called on the same thread as Init().
1092 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
1093 "Channel::Terminate");
1094
1095 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
1096
1097 StopSend();
1098 StopPlayout();
1099
1100 {
1101 rtc::CritScope cs(&_fileCritSect);
1102 if (input_file_player_) {
1103 input_file_player_->RegisterModuleFileCallback(NULL);
1104 input_file_player_->StopPlayingFile();
1105 }
1106 if (output_file_player_) {
1107 output_file_player_->RegisterModuleFileCallback(NULL);
1108 output_file_player_->StopPlayingFile();
1109 }
1110 if (output_file_recorder_) {
1111 output_file_recorder_->RegisterModuleFileCallback(NULL);
1112 output_file_recorder_->StopRecording();
1113 }
1114 }
1115
1116 // The order to safely shutdown modules in a channel is:
1117 // 1. De-register callbacks in modules
1118 // 2. De-register modules in process thread
1119 // 3. Destroy modules
1120 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
1121 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1122 "Terminate() failed to de-register transport callback"
1123 " (Audio coding module)");
1124 }
1125
1126 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
1127 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1128 "Terminate() failed to de-register VAD callback"
1129 " (Audio coding module)");
1130 }
1131
1132 // De-register modules in process thread
1133 if (_moduleProcessThreadPtr)
1134 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
1135
1136 // End of modules shutdown
1137}
1138
kwiberg55b97fe2016-01-28 05:22:45 -08001139int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1140 OutputMixer& outputMixer,
kwiberg55b97fe2016-01-28 05:22:45 -08001141 ProcessThread& moduleProcessThread,
1142 AudioDeviceModule& audioDeviceModule,
1143 VoiceEngineObserver* voiceEngineObserver,
henrikaec6fbd22017-03-31 05:43:36 -07001144 rtc::CriticalSection* callbackCritSect,
1145 rtc::TaskQueue* encoder_queue) {
1146 RTC_DCHECK(encoder_queue);
1147 RTC_DCHECK(!encoder_queue_);
kwiberg55b97fe2016-01-28 05:22:45 -08001148 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1149 "Channel::SetEngineInformation()");
1150 _engineStatisticsPtr = &engineStatistics;
1151 _outputMixerPtr = &outputMixer;
kwiberg55b97fe2016-01-28 05:22:45 -08001152 _moduleProcessThreadPtr = &moduleProcessThread;
1153 _audioDeviceModulePtr = &audioDeviceModule;
1154 _voiceEngineObserverPtr = voiceEngineObserver;
1155 _callbackCritSectPtr = callbackCritSect;
henrikaec6fbd22017-03-31 05:43:36 -07001156 encoder_queue_ = encoder_queue;
kwiberg55b97fe2016-01-28 05:22:45 -08001157 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001158}
1159
kwibergb7f89d62016-02-17 10:04:18 -08001160void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001161 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001162 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001163}
1164
ossu29b1a8d2016-06-13 07:34:51 -07001165const rtc::scoped_refptr<AudioDecoderFactory>&
1166Channel::GetAudioDecoderFactory() const {
1167 return decoder_factory_;
1168}
1169
kwiberg55b97fe2016-01-28 05:22:45 -08001170int32_t Channel::StartPlayout() {
1171 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1172 "Channel::StartPlayout()");
1173 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001174 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001175 }
1176
solenberge374e012017-02-14 04:55:00 -08001177 // Add participant as candidates for mixing.
1178 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1179 _engineStatisticsPtr->SetLastError(
1180 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1181 "StartPlayout() failed to add participant to mixer");
1182 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001183 }
1184
1185 channel_state_.SetPlaying(true);
1186 if (RegisterFilePlayingToMixer() != 0)
1187 return -1;
1188
1189 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001190}
1191
kwiberg55b97fe2016-01-28 05:22:45 -08001192int32_t Channel::StopPlayout() {
1193 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1194 "Channel::StopPlayout()");
1195 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001196 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001197 }
1198
solenberge374e012017-02-14 04:55:00 -08001199 // Remove participant as candidates for mixing
1200 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1201 _engineStatisticsPtr->SetLastError(
1202 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1203 "StopPlayout() failed to remove participant from mixer");
1204 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001205 }
1206
1207 channel_state_.SetPlaying(false);
1208 _outputAudioLevel.Clear();
1209
1210 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001211}
1212
kwiberg55b97fe2016-01-28 05:22:45 -08001213int32_t Channel::StartSend() {
1214 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1215 "Channel::StartSend()");
kwiberg55b97fe2016-01-28 05:22:45 -08001216 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001217 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001218 }
1219 channel_state_.SetSending(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001220
solenberg08b19df2017-02-15 00:42:31 -08001221 // Resume the previous sequence number which was reset by StopSend(). This
1222 // needs to be done before |sending| is set to true on the RTP/RTCP module.
1223 if (send_sequence_number_) {
1224 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
1225 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001226 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001227 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1228 _engineStatisticsPtr->SetLastError(
1229 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1230 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001231 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001232 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001233 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001234 return -1;
1235 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001236
kwiberg55b97fe2016-01-28 05:22:45 -08001237 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001238}
1239
henrikaec6fbd22017-03-31 05:43:36 -07001240void Channel::StopSend() {
kwiberg55b97fe2016-01-28 05:22:45 -08001241 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1242 "Channel::StopSend()");
1243 if (!channel_state_.Get().sending) {
henrikaec6fbd22017-03-31 05:43:36 -07001244 return;
kwiberg55b97fe2016-01-28 05:22:45 -08001245 }
1246 channel_state_.SetSending(false);
1247
henrikaec6fbd22017-03-31 05:43:36 -07001248 // Post a task to the encoder thread which sets an event when the task is
1249 // executed. We know that no more encoding tasks will be added to the task
1250 // queue for this channel since sending is now deactivated. It means that,
1251 // if we wait for the event to bet set, we know that no more pending tasks
1252 // exists and it is therfore guaranteed that the task queue will never try
1253 // to acccess and invalid channel object.
1254 RTC_DCHECK(encoder_queue_);
1255 rtc::Event flush(false, false);
1256 encoder_queue_->PostTask([&flush]() { flush.Set(); });
1257 flush.Wait(rtc::Event::kForever);
1258
kwiberg55b97fe2016-01-28 05:22:45 -08001259 // Store the sequence number to be able to pick up the same sequence for
1260 // the next StartSend(). This is needed for restarting device, otherwise
1261 // it might cause libSRTP to complain about packets being replayed.
1262 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1263 // CL is landed. See issue
1264 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1265 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1266
1267 // Reset sending SSRC and sequence number and triggers direct transmission
1268 // of RTCP BYE
1269 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1270 _engineStatisticsPtr->SetLastError(
1271 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1272 "StartSend() RTP/RTCP failed to stop sending");
1273 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001274 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001275}
1276
ossu1ffbd6c2017-04-06 12:05:04 -07001277bool Channel::SetEncoder(int payload_type,
1278 std::unique_ptr<AudioEncoder> encoder) {
1279 RTC_DCHECK_GE(payload_type, 0);
1280 RTC_DCHECK_LE(payload_type, 127);
1281 // TODO(ossu): Make a CodecInst up for now. It seems like very little of this
1282 // information is actually used, possibly only payload type and clock rate.
1283 CodecInst lies;
1284 lies.pltype = payload_type;
1285 strncpy(lies.plname, "audio", sizeof(lies.plname));
1286 lies.plname[sizeof(lies.plname) - 1] = 0;
1287 // Seems unclear if it should be clock rate or sample rate. CodecInst
1288 // supposedly carries the sample rate, but only clock rate seems sensible to
1289 // send to the RTP/RTCP module.
1290 lies.plfreq = encoder->RtpTimestampRateHz();
1291 lies.pacsize = 0;
1292 lies.channels = encoder->NumChannels();
1293 lies.rate = 0;
1294
1295 if (_rtpRtcpModule->RegisterSendPayload(lies) != 0) {
1296 _rtpRtcpModule->DeRegisterSendPayload(payload_type);
1297 if (_rtpRtcpModule->RegisterSendPayload(lies) != 0) {
1298 WEBRTC_TRACE(
1299 kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1300 "SetEncoder() failed to register codec to RTP/RTCP module");
1301 return false;
1302 }
1303 }
1304
1305 audio_coding_->SetEncoder(std::move(encoder));
1306 return true;
1307}
1308
kwiberg55b97fe2016-01-28 05:22:45 -08001309int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1310 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1311 "Channel::RegisterVoiceEngineObserver()");
1312 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001313
kwiberg55b97fe2016-01-28 05:22:45 -08001314 if (_voiceEngineObserverPtr) {
1315 _engineStatisticsPtr->SetLastError(
1316 VE_INVALID_OPERATION, kTraceError,
1317 "RegisterVoiceEngineObserver() observer already enabled");
1318 return -1;
1319 }
1320 _voiceEngineObserverPtr = &observer;
1321 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001322}
1323
kwiberg55b97fe2016-01-28 05:22:45 -08001324int32_t Channel::DeRegisterVoiceEngineObserver() {
1325 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1326 "Channel::DeRegisterVoiceEngineObserver()");
1327 rtc::CritScope cs(&_callbackCritSect);
1328
1329 if (!_voiceEngineObserverPtr) {
1330 _engineStatisticsPtr->SetLastError(
1331 VE_INVALID_OPERATION, kTraceWarning,
1332 "DeRegisterVoiceEngineObserver() observer already disabled");
1333 return 0;
1334 }
1335 _voiceEngineObserverPtr = NULL;
1336 return 0;
1337}
1338
1339int32_t Channel::GetSendCodec(CodecInst& codec) {
kwibergc8d071e2016-04-06 12:22:38 -07001340 auto send_codec = codec_manager_.GetCodecInst();
kwiberg1fd4a4a2015-11-03 11:20:50 -08001341 if (send_codec) {
1342 codec = *send_codec;
1343 return 0;
1344 }
1345 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001346}
1347
kwiberg55b97fe2016-01-28 05:22:45 -08001348int32_t Channel::GetRecCodec(CodecInst& codec) {
1349 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001350}
1351
kwiberg55b97fe2016-01-28 05:22:45 -08001352int32_t Channel::SetSendCodec(const CodecInst& codec) {
1353 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1354 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001355
kwibergc8d071e2016-04-06 12:22:38 -07001356 if (!codec_manager_.RegisterEncoder(codec) ||
1357 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001358 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1359 "SetSendCodec() failed to register codec to ACM");
1360 return -1;
1361 }
1362
1363 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1364 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1365 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1366 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1367 "SetSendCodec() failed to register codec to"
1368 " RTP/RTCP module");
1369 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001370 }
kwiberg55b97fe2016-01-28 05:22:45 -08001371 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001372
kwiberg55b97fe2016-01-28 05:22:45 -08001373 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001374}
1375
minyue78b4d562016-11-30 04:47:39 -08001376void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001377 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1378 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001379 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001380 if (*encoder) {
1381 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001382 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001383 }
1384 });
michaelt566d8202017-01-12 10:17:38 -08001385 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001386}
1387
elad.alond12a8e12017-03-23 11:04:48 -07001388void Channel::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
1389 if (!use_twcc_plr_for_ana_)
1390 return;
minyue7e304322016-10-12 05:00:55 -07001391 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
elad.alond12a8e12017-03-23 11:04:48 -07001392 if (*encoder) {
1393 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1394 }
1395 });
1396}
1397
elad.alondadb4dc2017-03-23 15:29:50 -07001398void Channel::OnRecoverableUplinkPacketLossRate(
1399 float recoverable_packet_loss_rate) {
1400 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1401 if (*encoder) {
1402 (*encoder)->OnReceivedUplinkRecoverablePacketLossFraction(
1403 recoverable_packet_loss_rate);
1404 }
1405 });
1406}
1407
elad.alond12a8e12017-03-23 11:04:48 -07001408void Channel::OnUplinkPacketLossRate(float packet_loss_rate) {
1409 if (use_twcc_plr_for_ana_)
1410 return;
1411 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1412 if (*encoder) {
1413 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1414 }
minyue7e304322016-10-12 05:00:55 -07001415 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001416}
1417
kwiberg55b97fe2016-01-28 05:22:45 -08001418int32_t Channel::SetVADStatus(bool enableVAD,
1419 ACMVADMode mode,
1420 bool disableDTX) {
1421 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1422 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001423 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1424 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1425 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001426 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1427 kTraceError,
1428 "SetVADStatus() failed to set VAD");
1429 return -1;
1430 }
1431 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001432}
1433
kwiberg55b97fe2016-01-28 05:22:45 -08001434int32_t Channel::GetVADStatus(bool& enabledVAD,
1435 ACMVADMode& mode,
1436 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001437 const auto* params = codec_manager_.GetStackParams();
1438 enabledVAD = params->use_cng;
1439 mode = params->vad_mode;
1440 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001441 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001442}
1443
kwiberg1c07c702017-03-27 07:15:49 -07001444void Channel::SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) {
1445 rtp_payload_registry_->SetAudioReceivePayloads(codecs);
1446 audio_coding_->SetReceiveCodecs(codecs);
1447}
1448
kwiberg55b97fe2016-01-28 05:22:45 -08001449int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001450 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1451}
1452
1453int32_t Channel::SetRecPayloadType(int payload_type,
1454 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001455 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1456 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001457
kwiberg55b97fe2016-01-28 05:22:45 -08001458 if (channel_state_.Get().playing) {
1459 _engineStatisticsPtr->SetLastError(
1460 VE_ALREADY_PLAYING, kTraceError,
1461 "SetRecPayloadType() unable to set PT while playing");
1462 return -1;
1463 }
kwiberg55b97fe2016-01-28 05:22:45 -08001464
kwiberg09f090c2017-03-01 01:57:11 -08001465 const CodecInst codec = SdpToCodecInst(payload_type, format);
kwibergd32bf752017-01-19 07:03:59 -08001466
1467 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001468 // De-register the selected codec (RTP/RTCP module and ACM)
1469
1470 int8_t pltype(-1);
1471 CodecInst rxCodec = codec;
1472
1473 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001474 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001475 rxCodec.pltype = pltype;
1476
1477 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1478 _engineStatisticsPtr->SetLastError(
1479 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1480 "SetRecPayloadType() RTP/RTCP-module deregistration "
1481 "failed");
1482 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001483 }
kwiberg55b97fe2016-01-28 05:22:45 -08001484 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1485 _engineStatisticsPtr->SetLastError(
1486 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1487 "SetRecPayloadType() ACM deregistration failed - 1");
1488 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001489 }
kwiberg55b97fe2016-01-28 05:22:45 -08001490 return 0;
1491 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001492
magjed56124bd2016-11-24 09:34:46 -08001493 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001494 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001495 // TODO(kwiberg): Retrying is probably not necessary, since
1496 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001497 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001498 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001499 _engineStatisticsPtr->SetLastError(
1500 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1501 "SetRecPayloadType() RTP/RTCP-module registration failed");
1502 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001503 }
kwiberg55b97fe2016-01-28 05:22:45 -08001504 }
kwibergd32bf752017-01-19 07:03:59 -08001505 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1506 audio_coding_->UnregisterReceiveCodec(payload_type);
1507 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001508 _engineStatisticsPtr->SetLastError(
1509 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1510 "SetRecPayloadType() ACM registration failed - 1");
1511 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001512 }
kwiberg55b97fe2016-01-28 05:22:45 -08001513 }
1514 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001515}
1516
kwiberg55b97fe2016-01-28 05:22:45 -08001517int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1518 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001519 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001520 _engineStatisticsPtr->SetLastError(
1521 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1522 "GetRecPayloadType() failed to retrieve RX payload type");
1523 return -1;
1524 }
1525 codec.pltype = payloadType;
1526 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001527}
1528
kwiberg55b97fe2016-01-28 05:22:45 -08001529int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1530 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1531 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001532
kwiberg55b97fe2016-01-28 05:22:45 -08001533 CodecInst codec;
1534 int32_t samplingFreqHz(-1);
1535 const size_t kMono = 1;
1536 if (frequency == kFreq32000Hz)
1537 samplingFreqHz = 32000;
1538 else if (frequency == kFreq16000Hz)
1539 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001540
kwiberg55b97fe2016-01-28 05:22:45 -08001541 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1542 _engineStatisticsPtr->SetLastError(
1543 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1544 "SetSendCNPayloadType() failed to retrieve default CN codec "
1545 "settings");
1546 return -1;
1547 }
1548
1549 // Modify the payload type (must be set to dynamic range)
1550 codec.pltype = type;
1551
kwibergc8d071e2016-04-06 12:22:38 -07001552 if (!codec_manager_.RegisterEncoder(codec) ||
1553 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001554 _engineStatisticsPtr->SetLastError(
1555 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1556 "SetSendCNPayloadType() failed to register CN to ACM");
1557 return -1;
1558 }
1559
1560 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1561 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1562 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1563 _engineStatisticsPtr->SetLastError(
1564 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1565 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1566 "module");
1567 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001568 }
kwiberg55b97fe2016-01-28 05:22:45 -08001569 }
1570 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001571}
1572
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001573int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001574 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001575 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001576
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001577 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001578 _engineStatisticsPtr->SetLastError(
1579 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001580 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001581 return -1;
1582 }
1583 return 0;
1584}
1585
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001586int Channel::SetOpusDtx(bool enable_dtx) {
1587 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1588 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001589 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001590 : audio_coding_->DisableOpusDtx();
1591 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001592 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1593 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001594 return -1;
1595 }
1596 return 0;
1597}
1598
ivoc85228d62016-07-27 04:53:47 -07001599int Channel::GetOpusDtx(bool* enabled) {
1600 int success = -1;
1601 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1602 if (encoder) {
1603 *enabled = encoder->GetDtx();
1604 success = 0;
1605 }
1606 });
1607 return success;
1608}
1609
minyue7e304322016-10-12 05:00:55 -07001610bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1611 bool success = false;
1612 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1613 if (*encoder) {
michaelt92aef172017-04-18 00:11:48 -07001614 success = (*encoder)->EnableAudioNetworkAdaptor(config_string,
1615 event_log_proxy_.get());
minyue7e304322016-10-12 05:00:55 -07001616 }
1617 });
1618 return success;
1619}
1620
1621void Channel::DisableAudioNetworkAdaptor() {
1622 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1623 if (*encoder)
1624 (*encoder)->DisableAudioNetworkAdaptor();
1625 });
1626}
1627
1628void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1629 int max_frame_length_ms) {
1630 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1631 if (*encoder) {
1632 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1633 max_frame_length_ms);
1634 }
1635 });
1636}
1637
mflodman3d7db262016-04-29 00:57:13 -07001638int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001639 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001640 "Channel::RegisterExternalTransport()");
1641
kwiberg55b97fe2016-01-28 05:22:45 -08001642 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001643 if (_externalTransport) {
1644 _engineStatisticsPtr->SetLastError(
1645 VE_INVALID_OPERATION, kTraceError,
1646 "RegisterExternalTransport() external transport already enabled");
1647 return -1;
1648 }
1649 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001650 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001651 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001652}
1653
kwiberg55b97fe2016-01-28 05:22:45 -08001654int32_t Channel::DeRegisterExternalTransport() {
1655 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1656 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001657
kwiberg55b97fe2016-01-28 05:22:45 -08001658 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001659 if (_transportPtr) {
1660 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1661 "DeRegisterExternalTransport() all transport is disabled");
1662 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001663 _engineStatisticsPtr->SetLastError(
1664 VE_INVALID_OPERATION, kTraceWarning,
1665 "DeRegisterExternalTransport() external transport already "
1666 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001667 }
1668 _externalTransport = false;
1669 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001670 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001671}
1672
nisse657bab22017-02-21 06:28:10 -08001673// TODO(nisse): Delete this method together with ReceivedRTPPacket.
1674// It's a temporary hack to support both ReceivedRTPPacket and
1675// OnRtpPacket interfaces without too much code duplication.
1676bool Channel::OnRtpPacketWithHeader(const uint8_t* received_packet,
1677 size_t length,
1678 RTPHeader *header) {
1679 // Store playout timestamp for the received RTP packet
1680 UpdatePlayoutTimestamp(false);
1681
1682 header->payload_type_frequency =
1683 rtp_payload_registry_->GetPayloadTypeFrequency(header->payloadType);
1684 if (header->payload_type_frequency < 0)
1685 return false;
1686 bool in_order = IsPacketInOrder(*header);
1687 rtp_receive_statistics_->IncomingPacket(
1688 *header, length, IsPacketRetransmitted(*header, in_order));
1689 rtp_payload_registry_->SetIncomingPayloadType(*header);
1690
1691 return ReceivePacket(received_packet, length, *header, in_order);
1692}
1693
mflodman3d7db262016-04-29 00:57:13 -07001694int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001695 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001696 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001697 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001698 "Channel::ReceivedRTPPacket()");
1699
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001700 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001701 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1702 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1703 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001704 return -1;
1705 }
nisse657bab22017-02-21 06:28:10 -08001706 return OnRtpPacketWithHeader(received_packet, length, &header) ? 0 : -1;
1707}
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001708
nisse657bab22017-02-21 06:28:10 -08001709void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
1710 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
1711 "Channel::ReceivedRTPPacket()");
1712
1713 RTPHeader header;
1714 packet.GetHeader(&header);
1715 OnRtpPacketWithHeader(packet.data(), packet.size(), &header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001716}
1717
1718bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001719 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001720 const RTPHeader& header,
1721 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001722 if (rtp_payload_registry_->IsRtx(header)) {
1723 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001724 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001725 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001726 assert(packet_length >= header.headerLength);
1727 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001728 PayloadUnion payload_specific;
1729 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001730 &payload_specific)) {
1731 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001732 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001733 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1734 payload_specific, in_order);
1735}
1736
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001737bool Channel::HandleRtxPacket(const uint8_t* packet,
1738 size_t packet_length,
1739 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001740 if (!rtp_payload_registry_->IsRtx(header))
1741 return false;
1742
1743 // Remove the RTX header and parse the original RTP header.
1744 if (packet_length < header.headerLength)
1745 return false;
1746 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1747 return false;
1748 if (restored_packet_in_use_) {
1749 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1750 "Multiple RTX headers detected, dropping packet");
1751 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001752 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001753 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001754 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1755 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001756 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1757 "Incoming RTX packet: invalid RTP header");
1758 return false;
1759 }
1760 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001761 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001762 restored_packet_in_use_ = false;
1763 return ret;
1764}
1765
1766bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1767 StreamStatistician* statistician =
1768 rtp_receive_statistics_->GetStatistician(header.ssrc);
1769 if (!statistician)
1770 return false;
1771 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001772}
1773
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001774bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1775 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001776 // Retransmissions are handled separately if RTX is enabled.
1777 if (rtp_payload_registry_->RtxEnabled())
1778 return false;
1779 StreamStatistician* statistician =
1780 rtp_receive_statistics_->GetStatistician(header.ssrc);
1781 if (!statistician)
1782 return false;
1783 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001784 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001785 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001786 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001787}
1788
mflodman3d7db262016-04-29 00:57:13 -07001789int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001790 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001791 "Channel::ReceivedRTCPPacket()");
1792 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001793 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001794
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001795 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001796 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001797 _engineStatisticsPtr->SetLastError(
1798 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1799 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1800 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001801
Minyue2013aec2015-05-13 14:14:42 +02001802 int64_t rtt = GetRTT(true);
1803 if (rtt == 0) {
1804 // Waiting for valid RTT.
1805 return 0;
1806 }
Erik Språng737336d2016-07-29 12:59:36 +02001807
1808 int64_t nack_window_ms = rtt;
1809 if (nack_window_ms < kMinRetransmissionWindowMs) {
1810 nack_window_ms = kMinRetransmissionWindowMs;
1811 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1812 nack_window_ms = kMaxRetransmissionWindowMs;
1813 }
1814 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1815
minyue7e304322016-10-12 05:00:55 -07001816 // Invoke audio encoders OnReceivedRtt().
1817 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1818 if (*encoder)
1819 (*encoder)->OnReceivedRtt(rtt);
1820 });
1821
Minyue2013aec2015-05-13 14:14:42 +02001822 uint32_t ntp_secs = 0;
1823 uint32_t ntp_frac = 0;
1824 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001825 if (0 !=
1826 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1827 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001828 // Waiting for RTCP.
1829 return 0;
1830 }
1831
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001832 {
tommi31fc21f2016-01-21 10:37:37 -08001833 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001834 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001835 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001836 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001837}
1838
niklase@google.com470e71d2011-07-07 08:21:25 +00001839int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001840 bool loop,
1841 FileFormats format,
1842 int startPosition,
1843 float volumeScaling,
1844 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001845 const CodecInst* codecInst) {
1846 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1847 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1848 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1849 "stopPosition=%d)",
1850 fileName, loop, format, volumeScaling, startPosition,
1851 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001852
kwiberg55b97fe2016-01-28 05:22:45 -08001853 if (channel_state_.Get().output_file_playing) {
1854 _engineStatisticsPtr->SetLastError(
1855 VE_ALREADY_PLAYING, kTraceError,
1856 "StartPlayingFileLocally() is already playing");
1857 return -1;
1858 }
1859
1860 {
1861 rtc::CritScope cs(&_fileCritSect);
1862
kwiberg5a25d952016-08-17 07:31:12 -07001863 if (output_file_player_) {
1864 output_file_player_->RegisterModuleFileCallback(NULL);
1865 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001866 }
1867
kwiberg5b356f42016-09-08 04:32:33 -07001868 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001869 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001870
kwiberg5a25d952016-08-17 07:31:12 -07001871 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001872 _engineStatisticsPtr->SetLastError(
1873 VE_INVALID_ARGUMENT, kTraceError,
1874 "StartPlayingFileLocally() filePlayer format is not correct");
1875 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001876 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001877
kwiberg55b97fe2016-01-28 05:22:45 -08001878 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001879
kwiberg5a25d952016-08-17 07:31:12 -07001880 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001881 fileName, loop, startPosition, volumeScaling, notificationTime,
1882 stopPosition, (const CodecInst*)codecInst) != 0) {
1883 _engineStatisticsPtr->SetLastError(
1884 VE_BAD_FILE, kTraceError,
1885 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001886 output_file_player_->StopPlayingFile();
1887 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001888 return -1;
1889 }
kwiberg5a25d952016-08-17 07:31:12 -07001890 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001891 channel_state_.SetOutputFilePlaying(true);
1892 }
1893
1894 if (RegisterFilePlayingToMixer() != 0)
1895 return -1;
1896
1897 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001898}
1899
1900int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001901 FileFormats format,
1902 int startPosition,
1903 float volumeScaling,
1904 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001905 const CodecInst* codecInst) {
1906 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1907 "Channel::StartPlayingFileLocally(format=%d,"
1908 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1909 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001910
kwiberg55b97fe2016-01-28 05:22:45 -08001911 if (stream == NULL) {
1912 _engineStatisticsPtr->SetLastError(
1913 VE_BAD_FILE, kTraceError,
1914 "StartPlayingFileLocally() NULL as input stream");
1915 return -1;
1916 }
1917
1918 if (channel_state_.Get().output_file_playing) {
1919 _engineStatisticsPtr->SetLastError(
1920 VE_ALREADY_PLAYING, kTraceError,
1921 "StartPlayingFileLocally() is already playing");
1922 return -1;
1923 }
1924
1925 {
1926 rtc::CritScope cs(&_fileCritSect);
1927
1928 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001929 if (output_file_player_) {
1930 output_file_player_->RegisterModuleFileCallback(NULL);
1931 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001932 }
1933
kwiberg55b97fe2016-01-28 05:22:45 -08001934 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001935 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001936 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001937
kwiberg5a25d952016-08-17 07:31:12 -07001938 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001939 _engineStatisticsPtr->SetLastError(
1940 VE_INVALID_ARGUMENT, kTraceError,
1941 "StartPlayingFileLocally() filePlayer format isnot correct");
1942 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001943 }
1944
kwiberg55b97fe2016-01-28 05:22:45 -08001945 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001946
kwiberg4ec01d92016-08-22 08:43:54 -07001947 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001948 volumeScaling, notificationTime,
1949 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001950 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1951 "StartPlayingFile() failed to "
1952 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001953 output_file_player_->StopPlayingFile();
1954 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001955 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001956 }
kwiberg5a25d952016-08-17 07:31:12 -07001957 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001958 channel_state_.SetOutputFilePlaying(true);
1959 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001960
kwiberg55b97fe2016-01-28 05:22:45 -08001961 if (RegisterFilePlayingToMixer() != 0)
1962 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001963
kwiberg55b97fe2016-01-28 05:22:45 -08001964 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001965}
1966
kwiberg55b97fe2016-01-28 05:22:45 -08001967int Channel::StopPlayingFileLocally() {
1968 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1969 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001970
kwiberg55b97fe2016-01-28 05:22:45 -08001971 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001972 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001973 }
1974
1975 {
1976 rtc::CritScope cs(&_fileCritSect);
1977
kwiberg5a25d952016-08-17 07:31:12 -07001978 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001979 _engineStatisticsPtr->SetLastError(
1980 VE_STOP_RECORDING_FAILED, kTraceError,
1981 "StopPlayingFile() could not stop playing");
1982 return -1;
1983 }
kwiberg5a25d952016-08-17 07:31:12 -07001984 output_file_player_->RegisterModuleFileCallback(NULL);
1985 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001986 channel_state_.SetOutputFilePlaying(false);
1987 }
1988 // _fileCritSect cannot be taken while calling
1989 // SetAnonymousMixibilityStatus. Refer to comments in
1990 // StartPlayingFileLocally(const char* ...) for more details.
1991 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
1992 _engineStatisticsPtr->SetLastError(
1993 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1994 "StopPlayingFile() failed to stop participant from playing as"
1995 "file in the mixer");
1996 return -1;
1997 }
1998
1999 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002000}
2001
kwiberg55b97fe2016-01-28 05:22:45 -08002002int Channel::IsPlayingFileLocally() const {
2003 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002004}
2005
kwiberg55b97fe2016-01-28 05:22:45 -08002006int Channel::RegisterFilePlayingToMixer() {
2007 // Return success for not registering for file playing to mixer if:
2008 // 1. playing file before playout is started on that channel.
2009 // 2. starting playout without file playing on that channel.
2010 if (!channel_state_.Get().playing ||
2011 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002012 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002013 }
2014
2015 // |_fileCritSect| cannot be taken while calling
2016 // SetAnonymousMixabilityStatus() since as soon as the participant is added
2017 // frames can be pulled by the mixer. Since the frames are generated from
2018 // the file, _fileCritSect will be taken. This would result in a deadlock.
2019 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
2020 channel_state_.SetOutputFilePlaying(false);
2021 rtc::CritScope cs(&_fileCritSect);
2022 _engineStatisticsPtr->SetLastError(
2023 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2024 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07002025 output_file_player_->StopPlayingFile();
2026 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002027 return -1;
2028 }
2029
2030 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002031}
2032
niklase@google.com470e71d2011-07-07 08:21:25 +00002033int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002034 bool loop,
2035 FileFormats format,
2036 int startPosition,
2037 float volumeScaling,
2038 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002039 const CodecInst* codecInst) {
2040 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2041 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2042 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2043 "stopPosition=%d)",
2044 fileName, loop, format, volumeScaling, startPosition,
2045 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002046
kwiberg55b97fe2016-01-28 05:22:45 -08002047 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002048
kwiberg55b97fe2016-01-28 05:22:45 -08002049 if (channel_state_.Get().input_file_playing) {
2050 _engineStatisticsPtr->SetLastError(
2051 VE_ALREADY_PLAYING, kTraceWarning,
2052 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002053 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002054 }
2055
2056 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002057 if (input_file_player_) {
2058 input_file_player_->RegisterModuleFileCallback(NULL);
2059 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002060 }
2061
2062 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002063 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002064 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002065
kwiberg5a25d952016-08-17 07:31:12 -07002066 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002067 _engineStatisticsPtr->SetLastError(
2068 VE_INVALID_ARGUMENT, kTraceError,
2069 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2070 return -1;
2071 }
2072
2073 const uint32_t notificationTime(0);
2074
kwiberg5a25d952016-08-17 07:31:12 -07002075 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002076 fileName, loop, startPosition, volumeScaling, notificationTime,
2077 stopPosition, (const CodecInst*)codecInst) != 0) {
2078 _engineStatisticsPtr->SetLastError(
2079 VE_BAD_FILE, kTraceError,
2080 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002081 input_file_player_->StopPlayingFile();
2082 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002083 return -1;
2084 }
kwiberg5a25d952016-08-17 07:31:12 -07002085 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002086 channel_state_.SetInputFilePlaying(true);
2087
2088 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002089}
2090
2091int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002092 FileFormats format,
2093 int startPosition,
2094 float volumeScaling,
2095 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002096 const CodecInst* codecInst) {
2097 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2098 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2099 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2100 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002101
kwiberg55b97fe2016-01-28 05:22:45 -08002102 if (stream == NULL) {
2103 _engineStatisticsPtr->SetLastError(
2104 VE_BAD_FILE, kTraceError,
2105 "StartPlayingFileAsMicrophone NULL as input stream");
2106 return -1;
2107 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002108
kwiberg55b97fe2016-01-28 05:22:45 -08002109 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002110
kwiberg55b97fe2016-01-28 05:22:45 -08002111 if (channel_state_.Get().input_file_playing) {
2112 _engineStatisticsPtr->SetLastError(
2113 VE_ALREADY_PLAYING, kTraceWarning,
2114 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002115 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002116 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002117
kwiberg55b97fe2016-01-28 05:22:45 -08002118 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002119 if (input_file_player_) {
2120 input_file_player_->RegisterModuleFileCallback(NULL);
2121 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002122 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002123
kwiberg55b97fe2016-01-28 05:22:45 -08002124 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002125 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002126 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002127
kwiberg5a25d952016-08-17 07:31:12 -07002128 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002129 _engineStatisticsPtr->SetLastError(
2130 VE_INVALID_ARGUMENT, kTraceError,
2131 "StartPlayingInputFile() filePlayer format isnot correct");
2132 return -1;
2133 }
2134
2135 const uint32_t notificationTime(0);
2136
kwiberg4ec01d92016-08-22 08:43:54 -07002137 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2138 notificationTime, stopPosition,
2139 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002140 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2141 "StartPlayingFile() failed to start "
2142 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002143 input_file_player_->StopPlayingFile();
2144 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002145 return -1;
2146 }
2147
kwiberg5a25d952016-08-17 07:31:12 -07002148 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002149 channel_state_.SetInputFilePlaying(true);
2150
2151 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002152}
2153
kwiberg55b97fe2016-01-28 05:22:45 -08002154int Channel::StopPlayingFileAsMicrophone() {
2155 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2156 "Channel::StopPlayingFileAsMicrophone()");
2157
2158 rtc::CritScope cs(&_fileCritSect);
2159
2160 if (!channel_state_.Get().input_file_playing) {
2161 return 0;
2162 }
2163
kwiberg5a25d952016-08-17 07:31:12 -07002164 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002165 _engineStatisticsPtr->SetLastError(
2166 VE_STOP_RECORDING_FAILED, kTraceError,
2167 "StopPlayingFile() could not stop playing");
2168 return -1;
2169 }
kwiberg5a25d952016-08-17 07:31:12 -07002170 input_file_player_->RegisterModuleFileCallback(NULL);
2171 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002172 channel_state_.SetInputFilePlaying(false);
2173
2174 return 0;
2175}
2176
2177int Channel::IsPlayingFileAsMicrophone() const {
2178 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002179}
2180
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002181int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002182 const CodecInst* codecInst) {
2183 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2184 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002185
kwiberg55b97fe2016-01-28 05:22:45 -08002186 if (_outputFileRecording) {
2187 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2188 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002189 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002190 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002191
kwiberg55b97fe2016-01-28 05:22:45 -08002192 FileFormats format;
2193 const uint32_t notificationTime(0); // Not supported in VoE
2194 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002195
kwiberg55b97fe2016-01-28 05:22:45 -08002196 if ((codecInst != NULL) &&
2197 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2198 _engineStatisticsPtr->SetLastError(
2199 VE_BAD_ARGUMENT, kTraceError,
2200 "StartRecordingPlayout() invalid compression");
2201 return (-1);
2202 }
2203 if (codecInst == NULL) {
2204 format = kFileFormatPcm16kHzFile;
2205 codecInst = &dummyCodec;
2206 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2207 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2208 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2209 format = kFileFormatWavFile;
2210 } else {
2211 format = kFileFormatCompressedFile;
2212 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002213
kwiberg55b97fe2016-01-28 05:22:45 -08002214 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002215
kwiberg55b97fe2016-01-28 05:22:45 -08002216 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002217 if (output_file_recorder_) {
2218 output_file_recorder_->RegisterModuleFileCallback(NULL);
2219 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002220 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002221
kwiberg5a25d952016-08-17 07:31:12 -07002222 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002223 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002224 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002225 _engineStatisticsPtr->SetLastError(
2226 VE_INVALID_ARGUMENT, kTraceError,
2227 "StartRecordingPlayout() fileRecorder format isnot correct");
2228 return -1;
2229 }
2230
kwiberg5a25d952016-08-17 07:31:12 -07002231 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002232 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2233 _engineStatisticsPtr->SetLastError(
2234 VE_BAD_FILE, kTraceError,
2235 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002236 output_file_recorder_->StopRecording();
2237 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002238 return -1;
2239 }
kwiberg5a25d952016-08-17 07:31:12 -07002240 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002241 _outputFileRecording = true;
2242
2243 return 0;
2244}
2245
2246int Channel::StartRecordingPlayout(OutStream* stream,
2247 const CodecInst* codecInst) {
2248 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2249 "Channel::StartRecordingPlayout()");
2250
2251 if (_outputFileRecording) {
2252 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2253 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002254 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002255 }
2256
2257 FileFormats format;
2258 const uint32_t notificationTime(0); // Not supported in VoE
2259 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2260
2261 if (codecInst != NULL && codecInst->channels != 1) {
2262 _engineStatisticsPtr->SetLastError(
2263 VE_BAD_ARGUMENT, kTraceError,
2264 "StartRecordingPlayout() invalid compression");
2265 return (-1);
2266 }
2267 if (codecInst == NULL) {
2268 format = kFileFormatPcm16kHzFile;
2269 codecInst = &dummyCodec;
2270 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2271 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2272 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2273 format = kFileFormatWavFile;
2274 } else {
2275 format = kFileFormatCompressedFile;
2276 }
2277
2278 rtc::CritScope cs(&_fileCritSect);
2279
2280 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002281 if (output_file_recorder_) {
2282 output_file_recorder_->RegisterModuleFileCallback(NULL);
2283 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002284 }
2285
kwiberg5a25d952016-08-17 07:31:12 -07002286 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002287 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002288 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002289 _engineStatisticsPtr->SetLastError(
2290 VE_INVALID_ARGUMENT, kTraceError,
2291 "StartRecordingPlayout() fileRecorder format isnot correct");
2292 return -1;
2293 }
2294
kwiberg4ec01d92016-08-22 08:43:54 -07002295 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002296 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002297 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2298 "StartRecordingPlayout() failed to "
2299 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002300 output_file_recorder_->StopRecording();
2301 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002302 return -1;
2303 }
2304
kwiberg5a25d952016-08-17 07:31:12 -07002305 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002306 _outputFileRecording = true;
2307
2308 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002309}
2310
kwiberg55b97fe2016-01-28 05:22:45 -08002311int Channel::StopRecordingPlayout() {
2312 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2313 "Channel::StopRecordingPlayout()");
2314
2315 if (!_outputFileRecording) {
2316 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2317 "StopRecordingPlayout() isnot recording");
2318 return -1;
2319 }
2320
2321 rtc::CritScope cs(&_fileCritSect);
2322
kwiberg5a25d952016-08-17 07:31:12 -07002323 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002324 _engineStatisticsPtr->SetLastError(
2325 VE_STOP_RECORDING_FAILED, kTraceError,
2326 "StopRecording() could not stop recording");
2327 return (-1);
2328 }
kwiberg5a25d952016-08-17 07:31:12 -07002329 output_file_recorder_->RegisterModuleFileCallback(NULL);
2330 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002331 _outputFileRecording = false;
2332
2333 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002334}
2335
kwiberg55b97fe2016-01-28 05:22:45 -08002336void Channel::SetMixWithMicStatus(bool mix) {
2337 rtc::CritScope cs(&_fileCritSect);
2338 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002339}
2340
solenberg8d73f8c2017-03-08 01:52:20 -08002341int Channel::GetSpeechOutputLevel() const {
2342 return _outputAudioLevel.Level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002343}
2344
solenberg8d73f8c2017-03-08 01:52:20 -08002345int Channel::GetSpeechOutputLevelFullRange() const {
2346 return _outputAudioLevel.LevelFullRange();
kwiberg55b97fe2016-01-28 05:22:45 -08002347}
2348
solenberg8d73f8c2017-03-08 01:52:20 -08002349void Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002350 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002351 input_mute_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002352}
2353
solenberg1c2af8e2016-03-24 10:36:00 -07002354bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002355 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002356 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002357}
2358
solenberg8d73f8c2017-03-08 01:52:20 -08002359void Channel::SetChannelOutputVolumeScaling(float scaling) {
kwiberg55b97fe2016-01-28 05:22:45 -08002360 rtc::CritScope cs(&volume_settings_critsect_);
kwiberg55b97fe2016-01-28 05:22:45 -08002361 _outputGain = scaling;
niklase@google.com470e71d2011-07-07 08:21:25 +00002362}
2363
solenberg8842c3e2016-03-11 03:06:41 -08002364int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002365 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002366 "Channel::SendTelephoneEventOutband(...)");
2367 RTC_DCHECK_LE(0, event);
2368 RTC_DCHECK_GE(255, event);
2369 RTC_DCHECK_LE(0, duration_ms);
2370 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002371 if (!Sending()) {
2372 return -1;
2373 }
solenberg8842c3e2016-03-11 03:06:41 -08002374 if (_rtpRtcpModule->SendTelephoneEventOutband(
2375 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002376 _engineStatisticsPtr->SetLastError(
2377 VE_SEND_DTMF_FAILED, kTraceWarning,
2378 "SendTelephoneEventOutband() failed to send event");
2379 return -1;
2380 }
2381 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002382}
2383
solenbergffbbcac2016-11-17 05:25:37 -08002384int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2385 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002386 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002387 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002388 RTC_DCHECK_LE(0, payload_type);
2389 RTC_DCHECK_GE(127, payload_type);
2390 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002391 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002392 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002393 memcpy(codec.plname, "telephone-event", 16);
2394 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2395 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2396 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2397 _engineStatisticsPtr->SetLastError(
2398 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2399 "SetSendTelephoneEventPayloadType() failed to register send"
2400 "payload type");
2401 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002402 }
kwiberg55b97fe2016-01-28 05:22:45 -08002403 }
kwiberg55b97fe2016-01-28 05:22:45 -08002404 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002405}
2406
kwiberg55b97fe2016-01-28 05:22:45 -08002407int Channel::SetLocalSSRC(unsigned int ssrc) {
2408 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2409 "Channel::SetLocalSSRC()");
2410 if (channel_state_.Get().sending) {
2411 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2412 "SetLocalSSRC() already sending");
2413 return -1;
2414 }
2415 _rtpRtcpModule->SetSSRC(ssrc);
2416 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002417}
2418
kwiberg55b97fe2016-01-28 05:22:45 -08002419int Channel::GetLocalSSRC(unsigned int& ssrc) {
2420 ssrc = _rtpRtcpModule->SSRC();
2421 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002422}
2423
kwiberg55b97fe2016-01-28 05:22:45 -08002424int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2425 ssrc = rtp_receiver_->SSRC();
2426 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002427}
2428
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002429int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002430 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002431 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002432}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002433
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002434int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2435 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002436 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2437 if (enable &&
2438 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2439 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002440 return -1;
2441 }
2442 return 0;
2443}
2444
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002445void Channel::EnableSendTransportSequenceNumber(int id) {
2446 int ret =
2447 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2448 RTC_DCHECK_EQ(0, ret);
2449}
2450
stefan3313ec92016-01-21 06:32:43 -08002451void Channel::EnableReceiveTransportSequenceNumber(int id) {
2452 rtp_header_parser_->DeregisterRtpHeaderExtension(
2453 kRtpExtensionTransportSequenceNumber);
2454 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2455 kRtpExtensionTransportSequenceNumber, id);
2456 RTC_DCHECK(ret);
2457}
2458
stefanbba9dec2016-02-01 04:39:55 -08002459void Channel::RegisterSenderCongestionControlObjects(
nisseb8f9a322017-03-27 05:36:15 -07002460 RtpTransportControllerSendInterface* transport,
stefan7de8d642017-02-07 07:14:08 -08002461 RtcpBandwidthObserver* bandwidth_observer) {
nisseb8f9a322017-03-27 05:36:15 -07002462 RtpPacketSender* rtp_packet_sender = transport->packet_sender();
2463 TransportFeedbackObserver* transport_feedback_observer =
2464 transport->transport_feedback_observer();
2465 PacketRouter* packet_router = transport->packet_router();
2466
stefanbba9dec2016-02-01 04:39:55 -08002467 RTC_DCHECK(rtp_packet_sender);
2468 RTC_DCHECK(transport_feedback_observer);
2469 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002470 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002471 feedback_observer_proxy_->SetTransportFeedbackObserver(
2472 transport_feedback_observer);
2473 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2474 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2475 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
nissefdbfdc92017-03-31 05:44:52 -07002476 packet_router->AddSendRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002477 packet_router_ = packet_router;
2478}
2479
stefanbba9dec2016-02-01 04:39:55 -08002480void Channel::RegisterReceiverCongestionControlObjects(
2481 PacketRouter* packet_router) {
2482 RTC_DCHECK(packet_router && !packet_router_);
nissefdbfdc92017-03-31 05:44:52 -07002483 packet_router->AddReceiveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002484 packet_router_ = packet_router;
2485}
2486
nissefdbfdc92017-03-31 05:44:52 -07002487void Channel::ResetSenderCongestionControlObjects() {
stefanbba9dec2016-02-01 04:39:55 -08002488 RTC_DCHECK(packet_router_);
2489 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002490 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002491 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2492 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
nissefdbfdc92017-03-31 05:44:52 -07002493 packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002494 packet_router_ = nullptr;
2495 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2496}
2497
nissefdbfdc92017-03-31 05:44:52 -07002498void Channel::ResetReceiverCongestionControlObjects() {
2499 RTC_DCHECK(packet_router_);
2500 packet_router_->RemoveReceiveRtpModule(_rtpRtcpModule.get());
2501 packet_router_ = nullptr;
2502}
2503
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002504void Channel::SetRTCPStatus(bool enable) {
2505 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2506 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002507 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002508}
2509
kwiberg55b97fe2016-01-28 05:22:45 -08002510int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002511 RtcpMode method = _rtpRtcpModule->RTCP();
2512 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002513 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002514}
2515
kwiberg55b97fe2016-01-28 05:22:45 -08002516int Channel::SetRTCP_CNAME(const char cName[256]) {
2517 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2518 "Channel::SetRTCP_CNAME()");
2519 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2520 _engineStatisticsPtr->SetLastError(
2521 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2522 "SetRTCP_CNAME() failed to set RTCP CNAME");
2523 return -1;
2524 }
2525 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002526}
2527
kwiberg55b97fe2016-01-28 05:22:45 -08002528int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2529 if (cName == NULL) {
2530 _engineStatisticsPtr->SetLastError(
2531 VE_INVALID_ARGUMENT, kTraceError,
2532 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2533 return -1;
2534 }
2535 char cname[RTCP_CNAME_SIZE];
2536 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2537 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2538 _engineStatisticsPtr->SetLastError(
2539 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2540 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2541 return -1;
2542 }
2543 strcpy(cName, cname);
2544 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002545}
2546
kwiberg55b97fe2016-01-28 05:22:45 -08002547int Channel::SendApplicationDefinedRTCPPacket(
2548 unsigned char subType,
2549 unsigned int name,
2550 const char* data,
2551 unsigned short dataLengthInBytes) {
2552 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2553 "Channel::SendApplicationDefinedRTCPPacket()");
2554 if (!channel_state_.Get().sending) {
2555 _engineStatisticsPtr->SetLastError(
2556 VE_NOT_SENDING, kTraceError,
2557 "SendApplicationDefinedRTCPPacket() not sending");
2558 return -1;
2559 }
2560 if (NULL == data) {
2561 _engineStatisticsPtr->SetLastError(
2562 VE_INVALID_ARGUMENT, kTraceError,
2563 "SendApplicationDefinedRTCPPacket() invalid data value");
2564 return -1;
2565 }
2566 if (dataLengthInBytes % 4 != 0) {
2567 _engineStatisticsPtr->SetLastError(
2568 VE_INVALID_ARGUMENT, kTraceError,
2569 "SendApplicationDefinedRTCPPacket() invalid length value");
2570 return -1;
2571 }
2572 RtcpMode status = _rtpRtcpModule->RTCP();
2573 if (status == RtcpMode::kOff) {
2574 _engineStatisticsPtr->SetLastError(
2575 VE_RTCP_ERROR, kTraceError,
2576 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2577 return -1;
2578 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002579
kwiberg55b97fe2016-01-28 05:22:45 -08002580 // Create and schedule the RTCP APP packet for transmission
2581 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2582 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2583 _engineStatisticsPtr->SetLastError(
2584 VE_SEND_ERROR, kTraceError,
2585 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2586 return -1;
2587 }
2588 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002589}
2590
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002591int Channel::GetRemoteRTCPReportBlocks(
2592 std::vector<ReportBlock>* report_blocks) {
2593 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002594 _engineStatisticsPtr->SetLastError(
2595 VE_INVALID_ARGUMENT, kTraceError,
2596 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002597 return -1;
2598 }
2599
2600 // Get the report blocks from the latest received RTCP Sender or Receiver
2601 // Report. Each element in the vector contains the sender's SSRC and a
2602 // report block according to RFC 3550.
2603 std::vector<RTCPReportBlock> rtcp_report_blocks;
2604 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002605 return -1;
2606 }
2607
2608 if (rtcp_report_blocks.empty())
2609 return 0;
2610
2611 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2612 for (; it != rtcp_report_blocks.end(); ++it) {
2613 ReportBlock report_block;
2614 report_block.sender_SSRC = it->remoteSSRC;
2615 report_block.source_SSRC = it->sourceSSRC;
2616 report_block.fraction_lost = it->fractionLost;
2617 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2618 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2619 report_block.interarrival_jitter = it->jitter;
2620 report_block.last_SR_timestamp = it->lastSR;
2621 report_block.delay_since_last_SR = it->delaySinceLastSR;
2622 report_blocks->push_back(report_block);
2623 }
2624 return 0;
2625}
2626
kwiberg55b97fe2016-01-28 05:22:45 -08002627int Channel::GetRTPStatistics(CallStatistics& stats) {
2628 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002629
kwiberg55b97fe2016-01-28 05:22:45 -08002630 // The jitter statistics is updated for each received RTP packet and is
2631 // based on received packets.
2632 RtcpStatistics statistics;
2633 StreamStatistician* statistician =
2634 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002635 if (statistician) {
2636 statistician->GetStatistics(&statistics,
2637 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002638 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002639
kwiberg55b97fe2016-01-28 05:22:45 -08002640 stats.fractionLost = statistics.fraction_lost;
2641 stats.cumulativeLost = statistics.cumulative_lost;
2642 stats.extendedMax = statistics.extended_max_sequence_number;
2643 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002644
kwiberg55b97fe2016-01-28 05:22:45 -08002645 // --- RTT
2646 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002647
kwiberg55b97fe2016-01-28 05:22:45 -08002648 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002649
kwiberg55b97fe2016-01-28 05:22:45 -08002650 size_t bytesSent(0);
2651 uint32_t packetsSent(0);
2652 size_t bytesReceived(0);
2653 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002654
kwiberg55b97fe2016-01-28 05:22:45 -08002655 if (statistician) {
2656 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2657 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002658
kwiberg55b97fe2016-01-28 05:22:45 -08002659 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2660 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2661 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2662 " output will not be complete");
2663 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002664
kwiberg55b97fe2016-01-28 05:22:45 -08002665 stats.bytesSent = bytesSent;
2666 stats.packetsSent = packetsSent;
2667 stats.bytesReceived = bytesReceived;
2668 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002669
kwiberg55b97fe2016-01-28 05:22:45 -08002670 // --- Timestamps
2671 {
2672 rtc::CritScope lock(&ts_stats_lock_);
2673 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2674 }
2675 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002676}
2677
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002678int Channel::SetCodecFECStatus(bool enable) {
2679 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2680 "Channel::SetCodecFECStatus()");
2681
kwibergc8d071e2016-04-06 12:22:38 -07002682 if (!codec_manager_.SetCodecFEC(enable) ||
2683 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002684 _engineStatisticsPtr->SetLastError(
2685 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2686 "SetCodecFECStatus() failed to set FEC state");
2687 return -1;
2688 }
2689 return 0;
2690}
2691
2692bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002693 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002694}
2695
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002696void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2697 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002698 // If pacing is enabled we always store packets.
2699 if (!pacing_enabled_)
2700 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002701 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002702 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002703 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002704 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002705 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002706}
2707
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002708// Called when we are missing one or more packets.
2709int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002710 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2711}
2712
henrikaec6fbd22017-03-31 05:43:36 -07002713void Channel::ProcessAndEncodeAudio(const AudioFrame& audio_input) {
2714 RTC_DCHECK(channel_state_.Get().sending);
2715 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
2716 // TODO(henrika): try to avoid copying by moving ownership of audio frame
2717 // either into pool of frames or into the task itself.
2718 audio_frame->CopyFrom(audio_input);
2719 audio_frame->id_ = ChannelId();
2720 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
2721 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
niklase@google.com470e71d2011-07-07 08:21:25 +00002722}
2723
henrikaec6fbd22017-03-31 05:43:36 -07002724void Channel::ProcessAndEncodeAudio(const int16_t* audio_data,
2725 int sample_rate,
2726 size_t number_of_frames,
2727 size_t number_of_channels) {
2728 RTC_DCHECK(channel_state_.Get().sending);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002729 CodecInst codec;
2730 GetSendCodec(codec);
henrikaec6fbd22017-03-31 05:43:36 -07002731 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
2732 audio_frame->id_ = ChannelId();
2733 audio_frame->sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2734 audio_frame->num_channels_ = std::min(number_of_channels, codec.channels);
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002735 RemixAndResample(audio_data, number_of_frames, number_of_channels,
henrikaec6fbd22017-03-31 05:43:36 -07002736 sample_rate, &input_resampler_, audio_frame.get());
2737 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
2738 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002739}
2740
henrikaec6fbd22017-03-31 05:43:36 -07002741void Channel::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
2742 RTC_DCHECK_RUN_ON(encoder_queue_);
2743 RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
2744 RTC_DCHECK_LE(audio_input->num_channels_, 2);
2745 RTC_DCHECK_EQ(audio_input->id_, ChannelId());
kwiberg55b97fe2016-01-28 05:22:45 -08002746
2747 if (channel_state_.Get().input_file_playing) {
henrikaec6fbd22017-03-31 05:43:36 -07002748 MixOrReplaceAudioWithFile(audio_input);
kwiberg55b97fe2016-01-28 05:22:45 -08002749 }
2750
henrikaec6fbd22017-03-31 05:43:36 -07002751 bool is_muted = InputMute();
2752 AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002753
kwiberg55b97fe2016-01-28 05:22:45 -08002754 if (_includeAudioLevelIndication) {
2755 size_t length =
henrikaec6fbd22017-03-31 05:43:36 -07002756 audio_input->samples_per_channel_ * audio_input->num_channels_;
2757 RTC_CHECK_LE(length, sizeof(audio_input->data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002758 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002759 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002760 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002761 rms_level_.Analyze(
henrikaec6fbd22017-03-31 05:43:36 -07002762 rtc::ArrayView<const int16_t>(audio_input->data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002763 }
kwiberg55b97fe2016-01-28 05:22:45 -08002764 }
solenberg1c2af8e2016-03-24 10:36:00 -07002765 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002766
henrikaec6fbd22017-03-31 05:43:36 -07002767 // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002768
kwiberg55b97fe2016-01-28 05:22:45 -08002769 // The ACM resamples internally.
henrikaec6fbd22017-03-31 05:43:36 -07002770 audio_input->timestamp_ = _timeStamp;
kwiberg55b97fe2016-01-28 05:22:45 -08002771 // This call will trigger AudioPacketizationCallback::SendData if encoding
2772 // is done and payload is ready for packetization and transmission.
2773 // Otherwise, it will return without invoking the callback.
henrikaec6fbd22017-03-31 05:43:36 -07002774 if (audio_coding_->Add10MsData(*audio_input) < 0) {
2775 LOG(LS_ERROR) << "ACM::Add10MsData() failed for channel " << _channelId;
2776 return;
kwiberg55b97fe2016-01-28 05:22:45 -08002777 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002778
henrikaec6fbd22017-03-31 05:43:36 -07002779 _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002780}
2781
solenberg7602aab2016-11-14 11:30:07 -08002782void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2783 RTC_DCHECK(!channel.channel() ||
2784 channel.channel()->ChannelId() != _channelId);
2785 rtc::CritScope lock(&assoc_send_channel_lock_);
2786 associate_send_channel_ = channel;
2787}
2788
Minyue2013aec2015-05-13 14:14:42 +02002789void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002790 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002791 Channel* channel = associate_send_channel_.channel();
2792 if (channel && channel->ChannelId() == channel_id) {
2793 // If this channel is associated with a send channel of the specified
2794 // Channel ID, disassociate with it.
2795 ChannelOwner ref(NULL);
2796 associate_send_channel_ = ref;
2797 }
2798}
2799
ivoc14d5dbe2016-07-04 07:06:55 -07002800void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2801 event_log_proxy_->SetEventLog(event_log);
2802}
2803
michaelt9332b7d2016-11-30 07:51:13 -08002804void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2805 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2806}
2807
nisse284542b2017-01-10 08:58:32 -08002808void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08002809 size_t overhead_per_packet =
2810 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08002811 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2812 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08002813 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08002814 }
2815 });
2816}
2817
2818void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002819 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002820 transport_overhead_per_packet_ = transport_overhead_per_packet;
2821 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002822}
2823
hbos3fd31fe2017-02-28 05:43:16 -08002824// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08002825void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002826 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002827 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2828 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002829}
2830
kwiberg55b97fe2016-01-28 05:22:45 -08002831int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2832 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002833}
2834
wu@webrtc.org24301a62013-12-13 19:17:43 +00002835void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2836 audio_coding_->GetDecodingCallStatistics(stats);
2837}
2838
solenberg358057b2015-11-27 10:46:42 -08002839uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002840 rtc::CritScope lock(&video_sync_lock_);
2841 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002842}
2843
kwiberg55b97fe2016-01-28 05:22:45 -08002844int Channel::SetMinimumPlayoutDelay(int delayMs) {
2845 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2846 "Channel::SetMinimumPlayoutDelay()");
2847 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2848 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2849 _engineStatisticsPtr->SetLastError(
2850 VE_INVALID_ARGUMENT, kTraceError,
2851 "SetMinimumPlayoutDelay() invalid min delay");
2852 return -1;
2853 }
2854 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2855 _engineStatisticsPtr->SetLastError(
2856 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2857 "SetMinimumPlayoutDelay() failed to set min playout delay");
2858 return -1;
2859 }
2860 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002861}
2862
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002863int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002864 uint32_t playout_timestamp_rtp = 0;
2865 {
tommi31fc21f2016-01-21 10:37:37 -08002866 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002867 playout_timestamp_rtp = playout_timestamp_rtp_;
2868 }
kwiberg55b97fe2016-01-28 05:22:45 -08002869 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002870 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002871 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002872 "GetPlayoutTimestamp() failed to retrieve timestamp");
2873 return -1;
2874 }
deadbeef74375882015-08-13 12:09:10 -07002875 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002876 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002877}
2878
kwiberg55b97fe2016-01-28 05:22:45 -08002879int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2880 RtpReceiver** rtp_receiver) const {
2881 *rtpRtcpModule = _rtpRtcpModule.get();
2882 *rtp_receiver = rtp_receiver_.get();
2883 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002884}
2885
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002886// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2887// a shared helper.
henrikaec6fbd22017-03-31 05:43:36 -07002888int32_t Channel::MixOrReplaceAudioWithFile(AudioFrame* audio_input) {
2889 RTC_DCHECK_RUN_ON(encoder_queue_);
kwibergb7f89d62016-02-17 10:04:18 -08002890 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002891 size_t fileSamples(0);
henrikaec6fbd22017-03-31 05:43:36 -07002892 const int mixingFrequency = audio_input->sample_rate_hz_;
kwiberg55b97fe2016-01-28 05:22:45 -08002893 {
2894 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002895
kwiberg5a25d952016-08-17 07:31:12 -07002896 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002897 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2898 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2899 " doesnt exist");
2900 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002901 }
2902
kwiberg4ec01d92016-08-22 08:43:54 -07002903 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002904 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002905 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2906 "Channel::MixOrReplaceAudioWithFile() file mixing "
2907 "failed");
2908 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002909 }
kwiberg55b97fe2016-01-28 05:22:45 -08002910 if (fileSamples == 0) {
2911 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2912 "Channel::MixOrReplaceAudioWithFile() file is ended");
2913 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002914 }
kwiberg55b97fe2016-01-28 05:22:45 -08002915 }
2916
henrikaec6fbd22017-03-31 05:43:36 -07002917 RTC_DCHECK_EQ(audio_input->samples_per_channel_, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08002918
2919 if (_mixFileWithMicrophone) {
2920 // Currently file stream is always mono.
2921 // TODO(xians): Change the code when FilePlayer supports real stereo.
henrikaec6fbd22017-03-31 05:43:36 -07002922 MixWithSat(audio_input->data_, audio_input->num_channels_, fileBuffer.get(),
kwiberg55b97fe2016-01-28 05:22:45 -08002923 1, fileSamples);
2924 } else {
2925 // Replace ACM audio with file.
2926 // Currently file stream is always mono.
2927 // TODO(xians): Change the code when FilePlayer supports real stereo.
henrikaec6fbd22017-03-31 05:43:36 -07002928 audio_input->UpdateFrame(
kwiberg55b97fe2016-01-28 05:22:45 -08002929 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2930 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2931 }
2932 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002933}
2934
kwiberg55b97fe2016-01-28 05:22:45 -08002935int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2936 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002937
kwibergb7f89d62016-02-17 10:04:18 -08002938 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002939 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002940
kwiberg55b97fe2016-01-28 05:22:45 -08002941 {
2942 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002943
kwiberg5a25d952016-08-17 07:31:12 -07002944 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002945 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2946 "Channel::MixAudioWithFile() file mixing failed");
2947 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002948 }
2949
kwiberg55b97fe2016-01-28 05:22:45 -08002950 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002951 if (output_file_player_->Get10msAudioFromFile(
2952 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002953 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2954 "Channel::MixAudioWithFile() file mixing failed");
2955 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002956 }
kwiberg55b97fe2016-01-28 05:22:45 -08002957 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002958
kwiberg55b97fe2016-01-28 05:22:45 -08002959 if (audioFrame.samples_per_channel_ == fileSamples) {
2960 // Currently file stream is always mono.
2961 // TODO(xians): Change the code when FilePlayer supports real stereo.
2962 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
2963 fileSamples);
2964 } else {
2965 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2966 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2967 ") != "
2968 "fileSamples(%" PRIuS ")",
2969 audioFrame.samples_per_channel_, fileSamples);
2970 return -1;
2971 }
2972
2973 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002974}
2975
deadbeef74375882015-08-13 12:09:10 -07002976void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002977 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002978
henrik.lundin96bd5022016-04-06 04:13:56 -07002979 if (!jitter_buffer_playout_timestamp_) {
2980 // This can happen if this channel has not received any RTP packets. In
2981 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07002982 return;
2983 }
2984
2985 uint16_t delay_ms = 0;
2986 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002987 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07002988 "Channel::UpdatePlayoutTimestamp() failed to read playout"
2989 " delay from the ADM");
2990 _engineStatisticsPtr->SetLastError(
2991 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
2992 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
2993 return;
2994 }
2995
henrik.lundin96bd5022016-04-06 04:13:56 -07002996 RTC_DCHECK(jitter_buffer_playout_timestamp_);
2997 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07002998
2999 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003000 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003001
kwiberg55b97fe2016-01-28 05:22:45 -08003002 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003003 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003004 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003005
3006 {
tommi31fc21f2016-01-21 10:37:37 -08003007 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08003008 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003009 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003010 }
3011 playout_delay_ms_ = delay_ms;
3012 }
3013}
3014
kwiberg55b97fe2016-01-28 05:22:45 -08003015void Channel::RegisterReceiveCodecsToRTPModule() {
3016 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3017 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003018
kwiberg55b97fe2016-01-28 05:22:45 -08003019 CodecInst codec;
3020 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003021
kwiberg55b97fe2016-01-28 05:22:45 -08003022 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3023 // Open up the RTP/RTCP receiver for all supported codecs
3024 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08003025 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08003026 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3027 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3028 " to register %s (%d/%d/%" PRIuS
3029 "/%d) to RTP/RTCP "
3030 "receiver",
3031 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3032 codec.rate);
3033 } else {
3034 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3035 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3036 "(%d/%d/%" PRIuS
3037 "/%d) has been added to the RTP/RTCP "
3038 "receiver",
3039 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3040 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003041 }
kwiberg55b97fe2016-01-28 05:22:45 -08003042 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003043}
3044
kwiberg55b97fe2016-01-28 05:22:45 -08003045int Channel::SetSendRtpHeaderExtension(bool enable,
3046 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003047 unsigned char id) {
3048 int error = 0;
3049 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3050 if (enable) {
3051 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3052 }
3053 return error;
3054}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003055
ossue280cde2016-10-12 11:04:10 -07003056int Channel::GetRtpTimestampRateHz() const {
3057 const auto format = audio_coding_->ReceiveFormat();
3058 // Default to the playout frequency if we've not gotten any packets yet.
3059 // TODO(ossu): Zero clockrate can only happen if we've added an external
3060 // decoder for a format we don't support internally. Remove once that way of
3061 // adding decoders is gone!
3062 return (format && format->clockrate_hz != 0)
3063 ? format->clockrate_hz
3064 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003065}
3066
Minyue2013aec2015-05-13 14:14:42 +02003067int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003068 RtcpMode method = _rtpRtcpModule->RTCP();
3069 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003070 return 0;
3071 }
3072 std::vector<RTCPReportBlock> report_blocks;
3073 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003074
3075 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003076 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003077 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003078 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003079 Channel* channel = associate_send_channel_.channel();
3080 // Tries to get RTT from an associated channel. This is important for
3081 // receive-only channels.
3082 if (channel) {
3083 // To prevent infinite recursion and deadlock, calling GetRTT of
3084 // associate channel should always use "false" for argument:
3085 // |allow_associate_channel|.
3086 rtt = channel->GetRTT(false);
3087 }
3088 }
3089 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003090 }
3091
3092 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3093 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3094 for (; it != report_blocks.end(); ++it) {
3095 if (it->remoteSSRC == remoteSSRC)
3096 break;
3097 }
3098 if (it == report_blocks.end()) {
3099 // We have not received packets with SSRC matching the report blocks.
3100 // To calculate RTT we try with the SSRC of the first report block.
3101 // This is very important for send-only channels where we don't know
3102 // the SSRC of the other end.
3103 remoteSSRC = report_blocks[0].remoteSSRC;
3104 }
Minyue2013aec2015-05-13 14:14:42 +02003105
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003106 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003107 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003108 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003109 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3110 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003111 return 0;
3112 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003113 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003114}
3115
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003116} // namespace voe
3117} // namespace webrtc