blob: f56d1d822c84f24d1534791c38f02cc3fa74f4fd [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"
nissecae45d02017-04-24 05:53:20 -070027#include "webrtc/call/rtp_transport_controller_send_interface.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));
ossu20a4b3f2017-04-27 02:08:52 -07001306 codec_manager_.UnsetCodecInst();
ossu1ffbd6c2017-04-06 12:05:04 -07001307 return true;
1308}
1309
ossu20a4b3f2017-04-27 02:08:52 -07001310void Channel::ModifyEncoder(
1311 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
1312 audio_coding_->ModifyEncoder(modifier);
1313}
1314
kwiberg55b97fe2016-01-28 05:22:45 -08001315int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1316 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1317 "Channel::RegisterVoiceEngineObserver()");
1318 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001319
kwiberg55b97fe2016-01-28 05:22:45 -08001320 if (_voiceEngineObserverPtr) {
1321 _engineStatisticsPtr->SetLastError(
1322 VE_INVALID_OPERATION, kTraceError,
1323 "RegisterVoiceEngineObserver() observer already enabled");
1324 return -1;
1325 }
1326 _voiceEngineObserverPtr = &observer;
1327 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001328}
1329
kwiberg55b97fe2016-01-28 05:22:45 -08001330int32_t Channel::DeRegisterVoiceEngineObserver() {
1331 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1332 "Channel::DeRegisterVoiceEngineObserver()");
1333 rtc::CritScope cs(&_callbackCritSect);
1334
1335 if (!_voiceEngineObserverPtr) {
1336 _engineStatisticsPtr->SetLastError(
1337 VE_INVALID_OPERATION, kTraceWarning,
1338 "DeRegisterVoiceEngineObserver() observer already disabled");
1339 return 0;
1340 }
1341 _voiceEngineObserverPtr = NULL;
1342 return 0;
1343}
1344
1345int32_t Channel::GetSendCodec(CodecInst& codec) {
ossu20a4b3f2017-04-27 02:08:52 -07001346 {
1347 const CodecInst* send_codec = codec_manager_.GetCodecInst();
1348 if (send_codec) {
1349 codec = *send_codec;
1350 return 0;
1351 }
1352 }
1353 rtc::Optional<CodecInst> acm_send_codec = audio_coding_->SendCodec();
1354 if (acm_send_codec) {
1355 codec = *acm_send_codec;
kwiberg1fd4a4a2015-11-03 11:20:50 -08001356 return 0;
1357 }
1358 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001359}
1360
kwiberg55b97fe2016-01-28 05:22:45 -08001361int32_t Channel::GetRecCodec(CodecInst& codec) {
1362 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001363}
1364
kwiberg55b97fe2016-01-28 05:22:45 -08001365int32_t Channel::SetSendCodec(const CodecInst& codec) {
1366 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1367 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001368
kwibergc8d071e2016-04-06 12:22:38 -07001369 if (!codec_manager_.RegisterEncoder(codec) ||
1370 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001371 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1372 "SetSendCodec() failed to register codec to ACM");
1373 return -1;
1374 }
1375
1376 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1377 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1378 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1379 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1380 "SetSendCodec() failed to register codec to"
1381 " RTP/RTCP module");
1382 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001383 }
kwiberg55b97fe2016-01-28 05:22:45 -08001384 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001385
kwiberg55b97fe2016-01-28 05:22:45 -08001386 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001387}
1388
minyue78b4d562016-11-30 04:47:39 -08001389void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001390 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1391 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001392 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001393 if (*encoder) {
1394 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001395 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001396 }
1397 });
michaelt566d8202017-01-12 10:17:38 -08001398 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001399}
1400
elad.alond12a8e12017-03-23 11:04:48 -07001401void Channel::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
1402 if (!use_twcc_plr_for_ana_)
1403 return;
minyue7e304322016-10-12 05:00:55 -07001404 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
elad.alond12a8e12017-03-23 11:04:48 -07001405 if (*encoder) {
1406 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1407 }
1408 });
1409}
1410
elad.alondadb4dc2017-03-23 15:29:50 -07001411void Channel::OnRecoverableUplinkPacketLossRate(
1412 float recoverable_packet_loss_rate) {
1413 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1414 if (*encoder) {
1415 (*encoder)->OnReceivedUplinkRecoverablePacketLossFraction(
1416 recoverable_packet_loss_rate);
1417 }
1418 });
1419}
1420
elad.alond12a8e12017-03-23 11:04:48 -07001421void Channel::OnUplinkPacketLossRate(float packet_loss_rate) {
1422 if (use_twcc_plr_for_ana_)
1423 return;
1424 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1425 if (*encoder) {
1426 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1427 }
minyue7e304322016-10-12 05:00:55 -07001428 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001429}
1430
kwiberg55b97fe2016-01-28 05:22:45 -08001431int32_t Channel::SetVADStatus(bool enableVAD,
1432 ACMVADMode mode,
1433 bool disableDTX) {
1434 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1435 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001436 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1437 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1438 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001439 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1440 kTraceError,
1441 "SetVADStatus() failed to set VAD");
1442 return -1;
1443 }
1444 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001445}
1446
kwiberg55b97fe2016-01-28 05:22:45 -08001447int32_t Channel::GetVADStatus(bool& enabledVAD,
1448 ACMVADMode& mode,
1449 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001450 const auto* params = codec_manager_.GetStackParams();
1451 enabledVAD = params->use_cng;
1452 mode = params->vad_mode;
1453 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001454 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001455}
1456
kwiberg1c07c702017-03-27 07:15:49 -07001457void Channel::SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) {
1458 rtp_payload_registry_->SetAudioReceivePayloads(codecs);
1459 audio_coding_->SetReceiveCodecs(codecs);
1460}
1461
kwiberg55b97fe2016-01-28 05:22:45 -08001462int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001463 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1464}
1465
1466int32_t Channel::SetRecPayloadType(int payload_type,
1467 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001468 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1469 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001470
kwiberg55b97fe2016-01-28 05:22:45 -08001471 if (channel_state_.Get().playing) {
1472 _engineStatisticsPtr->SetLastError(
1473 VE_ALREADY_PLAYING, kTraceError,
1474 "SetRecPayloadType() unable to set PT while playing");
1475 return -1;
1476 }
kwiberg55b97fe2016-01-28 05:22:45 -08001477
kwiberg09f090c2017-03-01 01:57:11 -08001478 const CodecInst codec = SdpToCodecInst(payload_type, format);
kwibergd32bf752017-01-19 07:03:59 -08001479
1480 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001481 // De-register the selected codec (RTP/RTCP module and ACM)
1482
1483 int8_t pltype(-1);
1484 CodecInst rxCodec = codec;
1485
1486 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001487 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001488 rxCodec.pltype = pltype;
1489
1490 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1491 _engineStatisticsPtr->SetLastError(
1492 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1493 "SetRecPayloadType() RTP/RTCP-module deregistration "
1494 "failed");
1495 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001496 }
kwiberg55b97fe2016-01-28 05:22:45 -08001497 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1498 _engineStatisticsPtr->SetLastError(
1499 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1500 "SetRecPayloadType() ACM deregistration failed - 1");
1501 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001502 }
kwiberg55b97fe2016-01-28 05:22:45 -08001503 return 0;
1504 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001505
magjed56124bd2016-11-24 09:34:46 -08001506 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001507 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001508 // TODO(kwiberg): Retrying is probably not necessary, since
1509 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001510 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001511 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001512 _engineStatisticsPtr->SetLastError(
1513 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1514 "SetRecPayloadType() RTP/RTCP-module registration failed");
1515 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001516 }
kwiberg55b97fe2016-01-28 05:22:45 -08001517 }
kwibergd32bf752017-01-19 07:03:59 -08001518 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1519 audio_coding_->UnregisterReceiveCodec(payload_type);
1520 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001521 _engineStatisticsPtr->SetLastError(
1522 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1523 "SetRecPayloadType() ACM registration failed - 1");
1524 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001525 }
kwiberg55b97fe2016-01-28 05:22:45 -08001526 }
1527 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001528}
1529
kwiberg55b97fe2016-01-28 05:22:45 -08001530int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1531 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001532 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001533 _engineStatisticsPtr->SetLastError(
1534 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1535 "GetRecPayloadType() failed to retrieve RX payload type");
1536 return -1;
1537 }
1538 codec.pltype = payloadType;
1539 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001540}
1541
kwiberg55b97fe2016-01-28 05:22:45 -08001542int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1543 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1544 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001545
kwiberg55b97fe2016-01-28 05:22:45 -08001546 CodecInst codec;
1547 int32_t samplingFreqHz(-1);
1548 const size_t kMono = 1;
1549 if (frequency == kFreq32000Hz)
1550 samplingFreqHz = 32000;
1551 else if (frequency == kFreq16000Hz)
1552 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001553
kwiberg55b97fe2016-01-28 05:22:45 -08001554 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1555 _engineStatisticsPtr->SetLastError(
1556 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1557 "SetSendCNPayloadType() failed to retrieve default CN codec "
1558 "settings");
1559 return -1;
1560 }
1561
1562 // Modify the payload type (must be set to dynamic range)
1563 codec.pltype = type;
1564
kwibergc8d071e2016-04-06 12:22:38 -07001565 if (!codec_manager_.RegisterEncoder(codec) ||
1566 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001567 _engineStatisticsPtr->SetLastError(
1568 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1569 "SetSendCNPayloadType() failed to register CN to ACM");
1570 return -1;
1571 }
1572
1573 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1574 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1575 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1576 _engineStatisticsPtr->SetLastError(
1577 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1578 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1579 "module");
1580 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001581 }
kwiberg55b97fe2016-01-28 05:22:45 -08001582 }
1583 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001584}
1585
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001586int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001587 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001588 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001589
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001590 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001591 _engineStatisticsPtr->SetLastError(
1592 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001593 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001594 return -1;
1595 }
1596 return 0;
1597}
1598
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001599int Channel::SetOpusDtx(bool enable_dtx) {
1600 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1601 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001602 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001603 : audio_coding_->DisableOpusDtx();
1604 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001605 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1606 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001607 return -1;
1608 }
1609 return 0;
1610}
1611
ivoc85228d62016-07-27 04:53:47 -07001612int Channel::GetOpusDtx(bool* enabled) {
1613 int success = -1;
1614 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1615 if (encoder) {
1616 *enabled = encoder->GetDtx();
1617 success = 0;
1618 }
1619 });
1620 return success;
1621}
1622
minyue7e304322016-10-12 05:00:55 -07001623bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1624 bool success = false;
1625 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1626 if (*encoder) {
michaelt92aef172017-04-18 00:11:48 -07001627 success = (*encoder)->EnableAudioNetworkAdaptor(config_string,
1628 event_log_proxy_.get());
minyue7e304322016-10-12 05:00:55 -07001629 }
1630 });
1631 return success;
1632}
1633
1634void Channel::DisableAudioNetworkAdaptor() {
1635 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1636 if (*encoder)
1637 (*encoder)->DisableAudioNetworkAdaptor();
1638 });
1639}
1640
1641void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1642 int max_frame_length_ms) {
1643 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1644 if (*encoder) {
1645 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1646 max_frame_length_ms);
1647 }
1648 });
1649}
1650
mflodman3d7db262016-04-29 00:57:13 -07001651int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001652 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001653 "Channel::RegisterExternalTransport()");
1654
kwiberg55b97fe2016-01-28 05:22:45 -08001655 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001656 if (_externalTransport) {
1657 _engineStatisticsPtr->SetLastError(
1658 VE_INVALID_OPERATION, kTraceError,
1659 "RegisterExternalTransport() external transport already enabled");
1660 return -1;
1661 }
1662 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001663 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001664 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001665}
1666
kwiberg55b97fe2016-01-28 05:22:45 -08001667int32_t Channel::DeRegisterExternalTransport() {
1668 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1669 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001670
kwiberg55b97fe2016-01-28 05:22:45 -08001671 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001672 if (_transportPtr) {
1673 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1674 "DeRegisterExternalTransport() all transport is disabled");
1675 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001676 _engineStatisticsPtr->SetLastError(
1677 VE_INVALID_OPERATION, kTraceWarning,
1678 "DeRegisterExternalTransport() external transport already "
1679 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001680 }
1681 _externalTransport = false;
1682 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001683 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001684}
1685
nisse657bab22017-02-21 06:28:10 -08001686// TODO(nisse): Delete this method together with ReceivedRTPPacket.
1687// It's a temporary hack to support both ReceivedRTPPacket and
1688// OnRtpPacket interfaces without too much code duplication.
1689bool Channel::OnRtpPacketWithHeader(const uint8_t* received_packet,
1690 size_t length,
1691 RTPHeader *header) {
1692 // Store playout timestamp for the received RTP packet
1693 UpdatePlayoutTimestamp(false);
1694
1695 header->payload_type_frequency =
1696 rtp_payload_registry_->GetPayloadTypeFrequency(header->payloadType);
1697 if (header->payload_type_frequency < 0)
1698 return false;
1699 bool in_order = IsPacketInOrder(*header);
1700 rtp_receive_statistics_->IncomingPacket(
1701 *header, length, IsPacketRetransmitted(*header, in_order));
1702 rtp_payload_registry_->SetIncomingPayloadType(*header);
1703
1704 return ReceivePacket(received_packet, length, *header, in_order);
1705}
1706
mflodman3d7db262016-04-29 00:57:13 -07001707int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001708 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001709 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001710 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001711 "Channel::ReceivedRTPPacket()");
1712
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001713 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001714 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1715 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1716 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001717 return -1;
1718 }
nisse657bab22017-02-21 06:28:10 -08001719 return OnRtpPacketWithHeader(received_packet, length, &header) ? 0 : -1;
1720}
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001721
nisse657bab22017-02-21 06:28:10 -08001722void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
1723 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
1724 "Channel::ReceivedRTPPacket()");
1725
1726 RTPHeader header;
1727 packet.GetHeader(&header);
1728 OnRtpPacketWithHeader(packet.data(), packet.size(), &header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001729}
1730
1731bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001732 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001733 const RTPHeader& header,
1734 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001735 if (rtp_payload_registry_->IsRtx(header)) {
1736 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001737 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001738 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001739 assert(packet_length >= header.headerLength);
1740 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001741 PayloadUnion payload_specific;
1742 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001743 &payload_specific)) {
1744 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001745 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001746 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1747 payload_specific, in_order);
1748}
1749
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001750bool Channel::HandleRtxPacket(const uint8_t* packet,
1751 size_t packet_length,
1752 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001753 if (!rtp_payload_registry_->IsRtx(header))
1754 return false;
1755
1756 // Remove the RTX header and parse the original RTP header.
1757 if (packet_length < header.headerLength)
1758 return false;
1759 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1760 return false;
1761 if (restored_packet_in_use_) {
1762 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1763 "Multiple RTX headers detected, dropping packet");
1764 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001765 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001766 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001767 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1768 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001769 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1770 "Incoming RTX packet: invalid RTP header");
1771 return false;
1772 }
1773 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001774 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001775 restored_packet_in_use_ = false;
1776 return ret;
1777}
1778
1779bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1780 StreamStatistician* statistician =
1781 rtp_receive_statistics_->GetStatistician(header.ssrc);
1782 if (!statistician)
1783 return false;
1784 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001785}
1786
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001787bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1788 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001789 // Retransmissions are handled separately if RTX is enabled.
1790 if (rtp_payload_registry_->RtxEnabled())
1791 return false;
1792 StreamStatistician* statistician =
1793 rtp_receive_statistics_->GetStatistician(header.ssrc);
1794 if (!statistician)
1795 return false;
1796 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001797 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001798 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001799 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001800}
1801
mflodman3d7db262016-04-29 00:57:13 -07001802int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001803 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001804 "Channel::ReceivedRTCPPacket()");
1805 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001806 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001807
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001808 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001809 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001810 _engineStatisticsPtr->SetLastError(
1811 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1812 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1813 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001814
Minyue2013aec2015-05-13 14:14:42 +02001815 int64_t rtt = GetRTT(true);
1816 if (rtt == 0) {
1817 // Waiting for valid RTT.
1818 return 0;
1819 }
Erik Språng737336d2016-07-29 12:59:36 +02001820
1821 int64_t nack_window_ms = rtt;
1822 if (nack_window_ms < kMinRetransmissionWindowMs) {
1823 nack_window_ms = kMinRetransmissionWindowMs;
1824 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1825 nack_window_ms = kMaxRetransmissionWindowMs;
1826 }
1827 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1828
minyue7e304322016-10-12 05:00:55 -07001829 // Invoke audio encoders OnReceivedRtt().
1830 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1831 if (*encoder)
1832 (*encoder)->OnReceivedRtt(rtt);
1833 });
1834
Minyue2013aec2015-05-13 14:14:42 +02001835 uint32_t ntp_secs = 0;
1836 uint32_t ntp_frac = 0;
1837 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001838 if (0 !=
1839 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1840 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001841 // Waiting for RTCP.
1842 return 0;
1843 }
1844
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001845 {
tommi31fc21f2016-01-21 10:37:37 -08001846 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001847 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001848 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001849 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001850}
1851
niklase@google.com470e71d2011-07-07 08:21:25 +00001852int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001853 bool loop,
1854 FileFormats format,
1855 int startPosition,
1856 float volumeScaling,
1857 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001858 const CodecInst* codecInst) {
1859 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1860 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1861 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1862 "stopPosition=%d)",
1863 fileName, loop, format, volumeScaling, startPosition,
1864 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001865
kwiberg55b97fe2016-01-28 05:22:45 -08001866 if (channel_state_.Get().output_file_playing) {
1867 _engineStatisticsPtr->SetLastError(
1868 VE_ALREADY_PLAYING, kTraceError,
1869 "StartPlayingFileLocally() is already playing");
1870 return -1;
1871 }
1872
1873 {
1874 rtc::CritScope cs(&_fileCritSect);
1875
kwiberg5a25d952016-08-17 07:31:12 -07001876 if (output_file_player_) {
1877 output_file_player_->RegisterModuleFileCallback(NULL);
1878 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001879 }
1880
kwiberg5b356f42016-09-08 04:32:33 -07001881 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001882 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001883
kwiberg5a25d952016-08-17 07:31:12 -07001884 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001885 _engineStatisticsPtr->SetLastError(
1886 VE_INVALID_ARGUMENT, kTraceError,
1887 "StartPlayingFileLocally() filePlayer format is not correct");
1888 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001889 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001890
kwiberg55b97fe2016-01-28 05:22:45 -08001891 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001892
kwiberg5a25d952016-08-17 07:31:12 -07001893 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001894 fileName, loop, startPosition, volumeScaling, notificationTime,
1895 stopPosition, (const CodecInst*)codecInst) != 0) {
1896 _engineStatisticsPtr->SetLastError(
1897 VE_BAD_FILE, kTraceError,
1898 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001899 output_file_player_->StopPlayingFile();
1900 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001901 return -1;
1902 }
kwiberg5a25d952016-08-17 07:31:12 -07001903 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001904 channel_state_.SetOutputFilePlaying(true);
1905 }
1906
1907 if (RegisterFilePlayingToMixer() != 0)
1908 return -1;
1909
1910 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001911}
1912
1913int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001914 FileFormats format,
1915 int startPosition,
1916 float volumeScaling,
1917 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001918 const CodecInst* codecInst) {
1919 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1920 "Channel::StartPlayingFileLocally(format=%d,"
1921 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1922 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001923
kwiberg55b97fe2016-01-28 05:22:45 -08001924 if (stream == NULL) {
1925 _engineStatisticsPtr->SetLastError(
1926 VE_BAD_FILE, kTraceError,
1927 "StartPlayingFileLocally() NULL as input stream");
1928 return -1;
1929 }
1930
1931 if (channel_state_.Get().output_file_playing) {
1932 _engineStatisticsPtr->SetLastError(
1933 VE_ALREADY_PLAYING, kTraceError,
1934 "StartPlayingFileLocally() is already playing");
1935 return -1;
1936 }
1937
1938 {
1939 rtc::CritScope cs(&_fileCritSect);
1940
1941 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001942 if (output_file_player_) {
1943 output_file_player_->RegisterModuleFileCallback(NULL);
1944 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001945 }
1946
kwiberg55b97fe2016-01-28 05:22:45 -08001947 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001948 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001949 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001950
kwiberg5a25d952016-08-17 07:31:12 -07001951 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001952 _engineStatisticsPtr->SetLastError(
1953 VE_INVALID_ARGUMENT, kTraceError,
1954 "StartPlayingFileLocally() filePlayer format isnot correct");
1955 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001956 }
1957
kwiberg55b97fe2016-01-28 05:22:45 -08001958 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001959
kwiberg4ec01d92016-08-22 08:43:54 -07001960 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001961 volumeScaling, notificationTime,
1962 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001963 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1964 "StartPlayingFile() failed to "
1965 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001966 output_file_player_->StopPlayingFile();
1967 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001968 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001969 }
kwiberg5a25d952016-08-17 07:31:12 -07001970 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001971 channel_state_.SetOutputFilePlaying(true);
1972 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001973
kwiberg55b97fe2016-01-28 05:22:45 -08001974 if (RegisterFilePlayingToMixer() != 0)
1975 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001976
kwiberg55b97fe2016-01-28 05:22:45 -08001977 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001978}
1979
kwiberg55b97fe2016-01-28 05:22:45 -08001980int Channel::StopPlayingFileLocally() {
1981 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1982 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001983
kwiberg55b97fe2016-01-28 05:22:45 -08001984 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001985 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001986 }
1987
1988 {
1989 rtc::CritScope cs(&_fileCritSect);
1990
kwiberg5a25d952016-08-17 07:31:12 -07001991 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001992 _engineStatisticsPtr->SetLastError(
1993 VE_STOP_RECORDING_FAILED, kTraceError,
1994 "StopPlayingFile() could not stop playing");
1995 return -1;
1996 }
kwiberg5a25d952016-08-17 07:31:12 -07001997 output_file_player_->RegisterModuleFileCallback(NULL);
1998 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001999 channel_state_.SetOutputFilePlaying(false);
2000 }
2001 // _fileCritSect cannot be taken while calling
2002 // SetAnonymousMixibilityStatus. Refer to comments in
2003 // StartPlayingFileLocally(const char* ...) for more details.
2004 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
2005 _engineStatisticsPtr->SetLastError(
2006 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2007 "StopPlayingFile() failed to stop participant from playing as"
2008 "file in the mixer");
2009 return -1;
2010 }
2011
2012 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002013}
2014
kwiberg55b97fe2016-01-28 05:22:45 -08002015int Channel::IsPlayingFileLocally() const {
2016 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002017}
2018
kwiberg55b97fe2016-01-28 05:22:45 -08002019int Channel::RegisterFilePlayingToMixer() {
2020 // Return success for not registering for file playing to mixer if:
2021 // 1. playing file before playout is started on that channel.
2022 // 2. starting playout without file playing on that channel.
2023 if (!channel_state_.Get().playing ||
2024 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002025 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002026 }
2027
2028 // |_fileCritSect| cannot be taken while calling
2029 // SetAnonymousMixabilityStatus() since as soon as the participant is added
2030 // frames can be pulled by the mixer. Since the frames are generated from
2031 // the file, _fileCritSect will be taken. This would result in a deadlock.
2032 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
2033 channel_state_.SetOutputFilePlaying(false);
2034 rtc::CritScope cs(&_fileCritSect);
2035 _engineStatisticsPtr->SetLastError(
2036 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2037 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07002038 output_file_player_->StopPlayingFile();
2039 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002040 return -1;
2041 }
2042
2043 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002044}
2045
niklase@google.com470e71d2011-07-07 08:21:25 +00002046int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002047 bool loop,
2048 FileFormats format,
2049 int startPosition,
2050 float volumeScaling,
2051 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002052 const CodecInst* codecInst) {
2053 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2054 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2055 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2056 "stopPosition=%d)",
2057 fileName, loop, format, volumeScaling, startPosition,
2058 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002059
kwiberg55b97fe2016-01-28 05:22:45 -08002060 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002061
kwiberg55b97fe2016-01-28 05:22:45 -08002062 if (channel_state_.Get().input_file_playing) {
2063 _engineStatisticsPtr->SetLastError(
2064 VE_ALREADY_PLAYING, kTraceWarning,
2065 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002066 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002067 }
2068
2069 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002070 if (input_file_player_) {
2071 input_file_player_->RegisterModuleFileCallback(NULL);
2072 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002073 }
2074
2075 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002076 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002077 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002078
kwiberg5a25d952016-08-17 07:31:12 -07002079 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002080 _engineStatisticsPtr->SetLastError(
2081 VE_INVALID_ARGUMENT, kTraceError,
2082 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2083 return -1;
2084 }
2085
2086 const uint32_t notificationTime(0);
2087
kwiberg5a25d952016-08-17 07:31:12 -07002088 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002089 fileName, loop, startPosition, volumeScaling, notificationTime,
2090 stopPosition, (const CodecInst*)codecInst) != 0) {
2091 _engineStatisticsPtr->SetLastError(
2092 VE_BAD_FILE, kTraceError,
2093 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002094 input_file_player_->StopPlayingFile();
2095 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002096 return -1;
2097 }
kwiberg5a25d952016-08-17 07:31:12 -07002098 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002099 channel_state_.SetInputFilePlaying(true);
2100
2101 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002102}
2103
2104int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002105 FileFormats format,
2106 int startPosition,
2107 float volumeScaling,
2108 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002109 const CodecInst* codecInst) {
2110 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2111 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2112 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2113 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002114
kwiberg55b97fe2016-01-28 05:22:45 -08002115 if (stream == NULL) {
2116 _engineStatisticsPtr->SetLastError(
2117 VE_BAD_FILE, kTraceError,
2118 "StartPlayingFileAsMicrophone NULL as input stream");
2119 return -1;
2120 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002121
kwiberg55b97fe2016-01-28 05:22:45 -08002122 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002123
kwiberg55b97fe2016-01-28 05:22:45 -08002124 if (channel_state_.Get().input_file_playing) {
2125 _engineStatisticsPtr->SetLastError(
2126 VE_ALREADY_PLAYING, kTraceWarning,
2127 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002128 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002129 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002130
kwiberg55b97fe2016-01-28 05:22:45 -08002131 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002132 if (input_file_player_) {
2133 input_file_player_->RegisterModuleFileCallback(NULL);
2134 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002135 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002136
kwiberg55b97fe2016-01-28 05:22:45 -08002137 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002138 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002139 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002140
kwiberg5a25d952016-08-17 07:31:12 -07002141 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002142 _engineStatisticsPtr->SetLastError(
2143 VE_INVALID_ARGUMENT, kTraceError,
2144 "StartPlayingInputFile() filePlayer format isnot correct");
2145 return -1;
2146 }
2147
2148 const uint32_t notificationTime(0);
2149
kwiberg4ec01d92016-08-22 08:43:54 -07002150 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2151 notificationTime, stopPosition,
2152 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002153 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2154 "StartPlayingFile() failed to start "
2155 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002156 input_file_player_->StopPlayingFile();
2157 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002158 return -1;
2159 }
2160
kwiberg5a25d952016-08-17 07:31:12 -07002161 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002162 channel_state_.SetInputFilePlaying(true);
2163
2164 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002165}
2166
kwiberg55b97fe2016-01-28 05:22:45 -08002167int Channel::StopPlayingFileAsMicrophone() {
2168 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2169 "Channel::StopPlayingFileAsMicrophone()");
2170
2171 rtc::CritScope cs(&_fileCritSect);
2172
2173 if (!channel_state_.Get().input_file_playing) {
2174 return 0;
2175 }
2176
kwiberg5a25d952016-08-17 07:31:12 -07002177 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002178 _engineStatisticsPtr->SetLastError(
2179 VE_STOP_RECORDING_FAILED, kTraceError,
2180 "StopPlayingFile() could not stop playing");
2181 return -1;
2182 }
kwiberg5a25d952016-08-17 07:31:12 -07002183 input_file_player_->RegisterModuleFileCallback(NULL);
2184 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002185 channel_state_.SetInputFilePlaying(false);
2186
2187 return 0;
2188}
2189
2190int Channel::IsPlayingFileAsMicrophone() const {
2191 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002192}
2193
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002194int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002195 const CodecInst* codecInst) {
2196 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2197 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002198
kwiberg55b97fe2016-01-28 05:22:45 -08002199 if (_outputFileRecording) {
2200 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2201 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002202 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002203 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002204
kwiberg55b97fe2016-01-28 05:22:45 -08002205 FileFormats format;
2206 const uint32_t notificationTime(0); // Not supported in VoE
2207 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002208
kwiberg55b97fe2016-01-28 05:22:45 -08002209 if ((codecInst != NULL) &&
2210 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2211 _engineStatisticsPtr->SetLastError(
2212 VE_BAD_ARGUMENT, kTraceError,
2213 "StartRecordingPlayout() invalid compression");
2214 return (-1);
2215 }
2216 if (codecInst == NULL) {
2217 format = kFileFormatPcm16kHzFile;
2218 codecInst = &dummyCodec;
2219 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2220 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2221 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2222 format = kFileFormatWavFile;
2223 } else {
2224 format = kFileFormatCompressedFile;
2225 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002226
kwiberg55b97fe2016-01-28 05:22:45 -08002227 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002228
kwiberg55b97fe2016-01-28 05:22:45 -08002229 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002230 if (output_file_recorder_) {
2231 output_file_recorder_->RegisterModuleFileCallback(NULL);
2232 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002233 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002234
kwiberg5a25d952016-08-17 07:31:12 -07002235 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002236 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002237 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002238 _engineStatisticsPtr->SetLastError(
2239 VE_INVALID_ARGUMENT, kTraceError,
2240 "StartRecordingPlayout() fileRecorder format isnot correct");
2241 return -1;
2242 }
2243
kwiberg5a25d952016-08-17 07:31:12 -07002244 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002245 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2246 _engineStatisticsPtr->SetLastError(
2247 VE_BAD_FILE, kTraceError,
2248 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002249 output_file_recorder_->StopRecording();
2250 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002251 return -1;
2252 }
kwiberg5a25d952016-08-17 07:31:12 -07002253 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002254 _outputFileRecording = true;
2255
2256 return 0;
2257}
2258
2259int Channel::StartRecordingPlayout(OutStream* stream,
2260 const CodecInst* codecInst) {
2261 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2262 "Channel::StartRecordingPlayout()");
2263
2264 if (_outputFileRecording) {
2265 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2266 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002267 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002268 }
2269
2270 FileFormats format;
2271 const uint32_t notificationTime(0); // Not supported in VoE
2272 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2273
2274 if (codecInst != NULL && codecInst->channels != 1) {
2275 _engineStatisticsPtr->SetLastError(
2276 VE_BAD_ARGUMENT, kTraceError,
2277 "StartRecordingPlayout() invalid compression");
2278 return (-1);
2279 }
2280 if (codecInst == NULL) {
2281 format = kFileFormatPcm16kHzFile;
2282 codecInst = &dummyCodec;
2283 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2284 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2285 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2286 format = kFileFormatWavFile;
2287 } else {
2288 format = kFileFormatCompressedFile;
2289 }
2290
2291 rtc::CritScope cs(&_fileCritSect);
2292
2293 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002294 if (output_file_recorder_) {
2295 output_file_recorder_->RegisterModuleFileCallback(NULL);
2296 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002297 }
2298
kwiberg5a25d952016-08-17 07:31:12 -07002299 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002300 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002301 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002302 _engineStatisticsPtr->SetLastError(
2303 VE_INVALID_ARGUMENT, kTraceError,
2304 "StartRecordingPlayout() fileRecorder format isnot correct");
2305 return -1;
2306 }
2307
kwiberg4ec01d92016-08-22 08:43:54 -07002308 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002309 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002310 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2311 "StartRecordingPlayout() failed to "
2312 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002313 output_file_recorder_->StopRecording();
2314 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002315 return -1;
2316 }
2317
kwiberg5a25d952016-08-17 07:31:12 -07002318 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002319 _outputFileRecording = true;
2320
2321 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002322}
2323
kwiberg55b97fe2016-01-28 05:22:45 -08002324int Channel::StopRecordingPlayout() {
2325 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2326 "Channel::StopRecordingPlayout()");
2327
2328 if (!_outputFileRecording) {
2329 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2330 "StopRecordingPlayout() isnot recording");
2331 return -1;
2332 }
2333
2334 rtc::CritScope cs(&_fileCritSect);
2335
kwiberg5a25d952016-08-17 07:31:12 -07002336 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002337 _engineStatisticsPtr->SetLastError(
2338 VE_STOP_RECORDING_FAILED, kTraceError,
2339 "StopRecording() could not stop recording");
2340 return (-1);
2341 }
kwiberg5a25d952016-08-17 07:31:12 -07002342 output_file_recorder_->RegisterModuleFileCallback(NULL);
2343 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002344 _outputFileRecording = false;
2345
2346 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002347}
2348
kwiberg55b97fe2016-01-28 05:22:45 -08002349void Channel::SetMixWithMicStatus(bool mix) {
2350 rtc::CritScope cs(&_fileCritSect);
2351 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002352}
2353
solenberg8d73f8c2017-03-08 01:52:20 -08002354int Channel::GetSpeechOutputLevel() const {
2355 return _outputAudioLevel.Level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002356}
2357
solenberg8d73f8c2017-03-08 01:52:20 -08002358int Channel::GetSpeechOutputLevelFullRange() const {
2359 return _outputAudioLevel.LevelFullRange();
kwiberg55b97fe2016-01-28 05:22:45 -08002360}
2361
solenberg8d73f8c2017-03-08 01:52:20 -08002362void Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002363 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002364 input_mute_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002365}
2366
solenberg1c2af8e2016-03-24 10:36:00 -07002367bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002368 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002369 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002370}
2371
solenberg8d73f8c2017-03-08 01:52:20 -08002372void Channel::SetChannelOutputVolumeScaling(float scaling) {
kwiberg55b97fe2016-01-28 05:22:45 -08002373 rtc::CritScope cs(&volume_settings_critsect_);
kwiberg55b97fe2016-01-28 05:22:45 -08002374 _outputGain = scaling;
niklase@google.com470e71d2011-07-07 08:21:25 +00002375}
2376
solenberg8842c3e2016-03-11 03:06:41 -08002377int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002378 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002379 "Channel::SendTelephoneEventOutband(...)");
2380 RTC_DCHECK_LE(0, event);
2381 RTC_DCHECK_GE(255, event);
2382 RTC_DCHECK_LE(0, duration_ms);
2383 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002384 if (!Sending()) {
2385 return -1;
2386 }
solenberg8842c3e2016-03-11 03:06:41 -08002387 if (_rtpRtcpModule->SendTelephoneEventOutband(
2388 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002389 _engineStatisticsPtr->SetLastError(
2390 VE_SEND_DTMF_FAILED, kTraceWarning,
2391 "SendTelephoneEventOutband() failed to send event");
2392 return -1;
2393 }
2394 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002395}
2396
solenbergffbbcac2016-11-17 05:25:37 -08002397int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2398 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002399 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002400 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002401 RTC_DCHECK_LE(0, payload_type);
2402 RTC_DCHECK_GE(127, payload_type);
2403 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002404 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002405 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002406 memcpy(codec.plname, "telephone-event", 16);
2407 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2408 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2409 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2410 _engineStatisticsPtr->SetLastError(
2411 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2412 "SetSendTelephoneEventPayloadType() failed to register send"
2413 "payload type");
2414 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002415 }
kwiberg55b97fe2016-01-28 05:22:45 -08002416 }
kwiberg55b97fe2016-01-28 05:22:45 -08002417 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002418}
2419
kwiberg55b97fe2016-01-28 05:22:45 -08002420int Channel::SetLocalSSRC(unsigned int ssrc) {
2421 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2422 "Channel::SetLocalSSRC()");
2423 if (channel_state_.Get().sending) {
2424 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2425 "SetLocalSSRC() already sending");
2426 return -1;
2427 }
2428 _rtpRtcpModule->SetSSRC(ssrc);
2429 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002430}
2431
kwiberg55b97fe2016-01-28 05:22:45 -08002432int Channel::GetLocalSSRC(unsigned int& ssrc) {
2433 ssrc = _rtpRtcpModule->SSRC();
2434 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002435}
2436
kwiberg55b97fe2016-01-28 05:22:45 -08002437int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2438 ssrc = rtp_receiver_->SSRC();
2439 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002440}
2441
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002442int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002443 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002444 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002445}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002446
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002447int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2448 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002449 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2450 if (enable &&
2451 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2452 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002453 return -1;
2454 }
2455 return 0;
2456}
2457
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002458void Channel::EnableSendTransportSequenceNumber(int id) {
2459 int ret =
2460 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2461 RTC_DCHECK_EQ(0, ret);
2462}
2463
stefan3313ec92016-01-21 06:32:43 -08002464void Channel::EnableReceiveTransportSequenceNumber(int id) {
2465 rtp_header_parser_->DeregisterRtpHeaderExtension(
2466 kRtpExtensionTransportSequenceNumber);
2467 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2468 kRtpExtensionTransportSequenceNumber, id);
2469 RTC_DCHECK(ret);
2470}
2471
stefanbba9dec2016-02-01 04:39:55 -08002472void Channel::RegisterSenderCongestionControlObjects(
nisseb8f9a322017-03-27 05:36:15 -07002473 RtpTransportControllerSendInterface* transport,
stefan7de8d642017-02-07 07:14:08 -08002474 RtcpBandwidthObserver* bandwidth_observer) {
nisseb8f9a322017-03-27 05:36:15 -07002475 RtpPacketSender* rtp_packet_sender = transport->packet_sender();
2476 TransportFeedbackObserver* transport_feedback_observer =
2477 transport->transport_feedback_observer();
2478 PacketRouter* packet_router = transport->packet_router();
2479
stefanbba9dec2016-02-01 04:39:55 -08002480 RTC_DCHECK(rtp_packet_sender);
2481 RTC_DCHECK(transport_feedback_observer);
2482 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002483 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002484 feedback_observer_proxy_->SetTransportFeedbackObserver(
2485 transport_feedback_observer);
2486 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2487 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2488 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
nissefdbfdc92017-03-31 05:44:52 -07002489 packet_router->AddSendRtpModule(_rtpRtcpModule.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002490 packet_router_ = packet_router;
2491}
2492
stefanbba9dec2016-02-01 04:39:55 -08002493void Channel::RegisterReceiverCongestionControlObjects(
2494 PacketRouter* packet_router) {
2495 RTC_DCHECK(packet_router && !packet_router_);
nissefdbfdc92017-03-31 05:44:52 -07002496 packet_router->AddReceiveRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002497 packet_router_ = packet_router;
2498}
2499
nissefdbfdc92017-03-31 05:44:52 -07002500void Channel::ResetSenderCongestionControlObjects() {
stefanbba9dec2016-02-01 04:39:55 -08002501 RTC_DCHECK(packet_router_);
2502 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002503 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002504 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2505 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
nissefdbfdc92017-03-31 05:44:52 -07002506 packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002507 packet_router_ = nullptr;
2508 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2509}
2510
nissefdbfdc92017-03-31 05:44:52 -07002511void Channel::ResetReceiverCongestionControlObjects() {
2512 RTC_DCHECK(packet_router_);
2513 packet_router_->RemoveReceiveRtpModule(_rtpRtcpModule.get());
2514 packet_router_ = nullptr;
2515}
2516
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002517void Channel::SetRTCPStatus(bool enable) {
2518 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2519 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002520 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002521}
2522
kwiberg55b97fe2016-01-28 05:22:45 -08002523int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002524 RtcpMode method = _rtpRtcpModule->RTCP();
2525 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002526 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002527}
2528
kwiberg55b97fe2016-01-28 05:22:45 -08002529int Channel::SetRTCP_CNAME(const char cName[256]) {
2530 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2531 "Channel::SetRTCP_CNAME()");
2532 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2533 _engineStatisticsPtr->SetLastError(
2534 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2535 "SetRTCP_CNAME() failed to set RTCP CNAME");
2536 return -1;
2537 }
2538 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002539}
2540
kwiberg55b97fe2016-01-28 05:22:45 -08002541int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2542 if (cName == NULL) {
2543 _engineStatisticsPtr->SetLastError(
2544 VE_INVALID_ARGUMENT, kTraceError,
2545 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2546 return -1;
2547 }
2548 char cname[RTCP_CNAME_SIZE];
2549 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2550 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2551 _engineStatisticsPtr->SetLastError(
2552 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2553 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2554 return -1;
2555 }
2556 strcpy(cName, cname);
2557 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002558}
2559
kwiberg55b97fe2016-01-28 05:22:45 -08002560int Channel::SendApplicationDefinedRTCPPacket(
2561 unsigned char subType,
2562 unsigned int name,
2563 const char* data,
2564 unsigned short dataLengthInBytes) {
2565 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2566 "Channel::SendApplicationDefinedRTCPPacket()");
2567 if (!channel_state_.Get().sending) {
2568 _engineStatisticsPtr->SetLastError(
2569 VE_NOT_SENDING, kTraceError,
2570 "SendApplicationDefinedRTCPPacket() not sending");
2571 return -1;
2572 }
2573 if (NULL == data) {
2574 _engineStatisticsPtr->SetLastError(
2575 VE_INVALID_ARGUMENT, kTraceError,
2576 "SendApplicationDefinedRTCPPacket() invalid data value");
2577 return -1;
2578 }
2579 if (dataLengthInBytes % 4 != 0) {
2580 _engineStatisticsPtr->SetLastError(
2581 VE_INVALID_ARGUMENT, kTraceError,
2582 "SendApplicationDefinedRTCPPacket() invalid length value");
2583 return -1;
2584 }
2585 RtcpMode status = _rtpRtcpModule->RTCP();
2586 if (status == RtcpMode::kOff) {
2587 _engineStatisticsPtr->SetLastError(
2588 VE_RTCP_ERROR, kTraceError,
2589 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2590 return -1;
2591 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002592
kwiberg55b97fe2016-01-28 05:22:45 -08002593 // Create and schedule the RTCP APP packet for transmission
2594 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2595 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2596 _engineStatisticsPtr->SetLastError(
2597 VE_SEND_ERROR, kTraceError,
2598 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2599 return -1;
2600 }
2601 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002602}
2603
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002604int Channel::GetRemoteRTCPReportBlocks(
2605 std::vector<ReportBlock>* report_blocks) {
2606 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002607 _engineStatisticsPtr->SetLastError(
2608 VE_INVALID_ARGUMENT, kTraceError,
2609 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002610 return -1;
2611 }
2612
2613 // Get the report blocks from the latest received RTCP Sender or Receiver
2614 // Report. Each element in the vector contains the sender's SSRC and a
2615 // report block according to RFC 3550.
2616 std::vector<RTCPReportBlock> rtcp_report_blocks;
2617 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002618 return -1;
2619 }
2620
2621 if (rtcp_report_blocks.empty())
2622 return 0;
2623
2624 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2625 for (; it != rtcp_report_blocks.end(); ++it) {
2626 ReportBlock report_block;
2627 report_block.sender_SSRC = it->remoteSSRC;
2628 report_block.source_SSRC = it->sourceSSRC;
2629 report_block.fraction_lost = it->fractionLost;
2630 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2631 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2632 report_block.interarrival_jitter = it->jitter;
2633 report_block.last_SR_timestamp = it->lastSR;
2634 report_block.delay_since_last_SR = it->delaySinceLastSR;
2635 report_blocks->push_back(report_block);
2636 }
2637 return 0;
2638}
2639
kwiberg55b97fe2016-01-28 05:22:45 -08002640int Channel::GetRTPStatistics(CallStatistics& stats) {
2641 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002642
kwiberg55b97fe2016-01-28 05:22:45 -08002643 // The jitter statistics is updated for each received RTP packet and is
2644 // based on received packets.
2645 RtcpStatistics statistics;
2646 StreamStatistician* statistician =
2647 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002648 if (statistician) {
2649 statistician->GetStatistics(&statistics,
2650 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002651 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002652
kwiberg55b97fe2016-01-28 05:22:45 -08002653 stats.fractionLost = statistics.fraction_lost;
2654 stats.cumulativeLost = statistics.cumulative_lost;
2655 stats.extendedMax = statistics.extended_max_sequence_number;
2656 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002657
kwiberg55b97fe2016-01-28 05:22:45 -08002658 // --- RTT
2659 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002660
kwiberg55b97fe2016-01-28 05:22:45 -08002661 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002662
kwiberg55b97fe2016-01-28 05:22:45 -08002663 size_t bytesSent(0);
2664 uint32_t packetsSent(0);
2665 size_t bytesReceived(0);
2666 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002667
kwiberg55b97fe2016-01-28 05:22:45 -08002668 if (statistician) {
2669 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2670 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002671
kwiberg55b97fe2016-01-28 05:22:45 -08002672 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2673 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2674 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2675 " output will not be complete");
2676 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002677
kwiberg55b97fe2016-01-28 05:22:45 -08002678 stats.bytesSent = bytesSent;
2679 stats.packetsSent = packetsSent;
2680 stats.bytesReceived = bytesReceived;
2681 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002682
kwiberg55b97fe2016-01-28 05:22:45 -08002683 // --- Timestamps
2684 {
2685 rtc::CritScope lock(&ts_stats_lock_);
2686 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2687 }
2688 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002689}
2690
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002691int Channel::SetCodecFECStatus(bool enable) {
2692 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2693 "Channel::SetCodecFECStatus()");
2694
kwibergc8d071e2016-04-06 12:22:38 -07002695 if (!codec_manager_.SetCodecFEC(enable) ||
2696 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002697 _engineStatisticsPtr->SetLastError(
2698 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2699 "SetCodecFECStatus() failed to set FEC state");
2700 return -1;
2701 }
2702 return 0;
2703}
2704
2705bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002706 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002707}
2708
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002709void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2710 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002711 // If pacing is enabled we always store packets.
2712 if (!pacing_enabled_)
2713 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002714 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002715 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002716 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002717 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002718 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002719}
2720
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002721// Called when we are missing one or more packets.
2722int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002723 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2724}
2725
henrikaec6fbd22017-03-31 05:43:36 -07002726void Channel::ProcessAndEncodeAudio(const AudioFrame& audio_input) {
2727 RTC_DCHECK(channel_state_.Get().sending);
2728 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
2729 // TODO(henrika): try to avoid copying by moving ownership of audio frame
2730 // either into pool of frames or into the task itself.
2731 audio_frame->CopyFrom(audio_input);
2732 audio_frame->id_ = ChannelId();
2733 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
2734 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
niklase@google.com470e71d2011-07-07 08:21:25 +00002735}
2736
henrikaec6fbd22017-03-31 05:43:36 -07002737void Channel::ProcessAndEncodeAudio(const int16_t* audio_data,
2738 int sample_rate,
2739 size_t number_of_frames,
2740 size_t number_of_channels) {
2741 RTC_DCHECK(channel_state_.Get().sending);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002742 CodecInst codec;
2743 GetSendCodec(codec);
henrikaec6fbd22017-03-31 05:43:36 -07002744 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
2745 audio_frame->id_ = ChannelId();
2746 audio_frame->sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2747 audio_frame->num_channels_ = std::min(number_of_channels, codec.channels);
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002748 RemixAndResample(audio_data, number_of_frames, number_of_channels,
henrikaec6fbd22017-03-31 05:43:36 -07002749 sample_rate, &input_resampler_, audio_frame.get());
2750 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
2751 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002752}
2753
henrikaec6fbd22017-03-31 05:43:36 -07002754void Channel::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
2755 RTC_DCHECK_RUN_ON(encoder_queue_);
2756 RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
2757 RTC_DCHECK_LE(audio_input->num_channels_, 2);
2758 RTC_DCHECK_EQ(audio_input->id_, ChannelId());
kwiberg55b97fe2016-01-28 05:22:45 -08002759
2760 if (channel_state_.Get().input_file_playing) {
henrikaec6fbd22017-03-31 05:43:36 -07002761 MixOrReplaceAudioWithFile(audio_input);
kwiberg55b97fe2016-01-28 05:22:45 -08002762 }
2763
henrikaec6fbd22017-03-31 05:43:36 -07002764 bool is_muted = InputMute();
2765 AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002766
kwiberg55b97fe2016-01-28 05:22:45 -08002767 if (_includeAudioLevelIndication) {
2768 size_t length =
henrikaec6fbd22017-03-31 05:43:36 -07002769 audio_input->samples_per_channel_ * audio_input->num_channels_;
2770 RTC_CHECK_LE(length, sizeof(audio_input->data_));
solenberg1c2af8e2016-03-24 10:36:00 -07002771 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002772 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002773 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002774 rms_level_.Analyze(
henrikaec6fbd22017-03-31 05:43:36 -07002775 rtc::ArrayView<const int16_t>(audio_input->data_, length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002776 }
kwiberg55b97fe2016-01-28 05:22:45 -08002777 }
solenberg1c2af8e2016-03-24 10:36:00 -07002778 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002779
henrikaec6fbd22017-03-31 05:43:36 -07002780 // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002781
kwiberg55b97fe2016-01-28 05:22:45 -08002782 // The ACM resamples internally.
henrikaec6fbd22017-03-31 05:43:36 -07002783 audio_input->timestamp_ = _timeStamp;
kwiberg55b97fe2016-01-28 05:22:45 -08002784 // This call will trigger AudioPacketizationCallback::SendData if encoding
2785 // is done and payload is ready for packetization and transmission.
2786 // Otherwise, it will return without invoking the callback.
henrikaec6fbd22017-03-31 05:43:36 -07002787 if (audio_coding_->Add10MsData(*audio_input) < 0) {
2788 LOG(LS_ERROR) << "ACM::Add10MsData() failed for channel " << _channelId;
2789 return;
kwiberg55b97fe2016-01-28 05:22:45 -08002790 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002791
henrikaec6fbd22017-03-31 05:43:36 -07002792 _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002793}
2794
solenberg7602aab2016-11-14 11:30:07 -08002795void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2796 RTC_DCHECK(!channel.channel() ||
2797 channel.channel()->ChannelId() != _channelId);
2798 rtc::CritScope lock(&assoc_send_channel_lock_);
2799 associate_send_channel_ = channel;
2800}
2801
Minyue2013aec2015-05-13 14:14:42 +02002802void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002803 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002804 Channel* channel = associate_send_channel_.channel();
2805 if (channel && channel->ChannelId() == channel_id) {
2806 // If this channel is associated with a send channel of the specified
2807 // Channel ID, disassociate with it.
2808 ChannelOwner ref(NULL);
2809 associate_send_channel_ = ref;
2810 }
2811}
2812
ivoc14d5dbe2016-07-04 07:06:55 -07002813void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2814 event_log_proxy_->SetEventLog(event_log);
2815}
2816
michaelt9332b7d2016-11-30 07:51:13 -08002817void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2818 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2819}
2820
nisse284542b2017-01-10 08:58:32 -08002821void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08002822 size_t overhead_per_packet =
2823 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08002824 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2825 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08002826 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08002827 }
2828 });
2829}
2830
2831void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002832 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002833 transport_overhead_per_packet_ = transport_overhead_per_packet;
2834 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002835}
2836
hbos3fd31fe2017-02-28 05:43:16 -08002837// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08002838void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002839 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002840 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2841 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002842}
2843
kwiberg55b97fe2016-01-28 05:22:45 -08002844int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2845 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002846}
2847
wu@webrtc.org24301a62013-12-13 19:17:43 +00002848void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2849 audio_coding_->GetDecodingCallStatistics(stats);
2850}
2851
solenberg358057b2015-11-27 10:46:42 -08002852uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002853 rtc::CritScope lock(&video_sync_lock_);
2854 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002855}
2856
kwiberg55b97fe2016-01-28 05:22:45 -08002857int Channel::SetMinimumPlayoutDelay(int delayMs) {
2858 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2859 "Channel::SetMinimumPlayoutDelay()");
2860 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2861 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2862 _engineStatisticsPtr->SetLastError(
2863 VE_INVALID_ARGUMENT, kTraceError,
2864 "SetMinimumPlayoutDelay() invalid min delay");
2865 return -1;
2866 }
2867 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2868 _engineStatisticsPtr->SetLastError(
2869 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2870 "SetMinimumPlayoutDelay() failed to set min playout delay");
2871 return -1;
2872 }
2873 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002874}
2875
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002876int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002877 uint32_t playout_timestamp_rtp = 0;
2878 {
tommi31fc21f2016-01-21 10:37:37 -08002879 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002880 playout_timestamp_rtp = playout_timestamp_rtp_;
2881 }
kwiberg55b97fe2016-01-28 05:22:45 -08002882 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002883 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002884 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002885 "GetPlayoutTimestamp() failed to retrieve timestamp");
2886 return -1;
2887 }
deadbeef74375882015-08-13 12:09:10 -07002888 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002889 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002890}
2891
kwiberg55b97fe2016-01-28 05:22:45 -08002892int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2893 RtpReceiver** rtp_receiver) const {
2894 *rtpRtcpModule = _rtpRtcpModule.get();
2895 *rtp_receiver = rtp_receiver_.get();
2896 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002897}
2898
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002899// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2900// a shared helper.
henrikaec6fbd22017-03-31 05:43:36 -07002901int32_t Channel::MixOrReplaceAudioWithFile(AudioFrame* audio_input) {
2902 RTC_DCHECK_RUN_ON(encoder_queue_);
kwibergb7f89d62016-02-17 10:04:18 -08002903 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002904 size_t fileSamples(0);
henrikaec6fbd22017-03-31 05:43:36 -07002905 const int mixingFrequency = audio_input->sample_rate_hz_;
kwiberg55b97fe2016-01-28 05:22:45 -08002906 {
2907 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002908
kwiberg5a25d952016-08-17 07:31:12 -07002909 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002910 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2911 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2912 " doesnt exist");
2913 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002914 }
2915
kwiberg4ec01d92016-08-22 08:43:54 -07002916 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002917 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002918 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2919 "Channel::MixOrReplaceAudioWithFile() file mixing "
2920 "failed");
2921 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002922 }
kwiberg55b97fe2016-01-28 05:22:45 -08002923 if (fileSamples == 0) {
2924 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2925 "Channel::MixOrReplaceAudioWithFile() file is ended");
2926 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002927 }
kwiberg55b97fe2016-01-28 05:22:45 -08002928 }
2929
henrikaec6fbd22017-03-31 05:43:36 -07002930 RTC_DCHECK_EQ(audio_input->samples_per_channel_, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08002931
2932 if (_mixFileWithMicrophone) {
2933 // Currently file stream is always mono.
2934 // TODO(xians): Change the code when FilePlayer supports real stereo.
henrikaec6fbd22017-03-31 05:43:36 -07002935 MixWithSat(audio_input->data_, audio_input->num_channels_, fileBuffer.get(),
kwiberg55b97fe2016-01-28 05:22:45 -08002936 1, fileSamples);
2937 } else {
2938 // Replace ACM audio with file.
2939 // Currently file stream is always mono.
2940 // TODO(xians): Change the code when FilePlayer supports real stereo.
henrikaec6fbd22017-03-31 05:43:36 -07002941 audio_input->UpdateFrame(
kwiberg55b97fe2016-01-28 05:22:45 -08002942 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2943 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2944 }
2945 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002946}
2947
kwiberg55b97fe2016-01-28 05:22:45 -08002948int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2949 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002950
kwibergb7f89d62016-02-17 10:04:18 -08002951 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002952 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002953
kwiberg55b97fe2016-01-28 05:22:45 -08002954 {
2955 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002956
kwiberg5a25d952016-08-17 07:31:12 -07002957 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002958 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2959 "Channel::MixAudioWithFile() file mixing failed");
2960 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002961 }
2962
kwiberg55b97fe2016-01-28 05:22:45 -08002963 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07002964 if (output_file_player_->Get10msAudioFromFile(
2965 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002966 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2967 "Channel::MixAudioWithFile() file mixing failed");
2968 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002969 }
kwiberg55b97fe2016-01-28 05:22:45 -08002970 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002971
kwiberg55b97fe2016-01-28 05:22:45 -08002972 if (audioFrame.samples_per_channel_ == fileSamples) {
2973 // Currently file stream is always mono.
2974 // TODO(xians): Change the code when FilePlayer supports real stereo.
2975 MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1,
2976 fileSamples);
2977 } else {
2978 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2979 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
2980 ") != "
2981 "fileSamples(%" PRIuS ")",
2982 audioFrame.samples_per_channel_, fileSamples);
2983 return -1;
2984 }
2985
2986 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002987}
2988
deadbeef74375882015-08-13 12:09:10 -07002989void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07002990 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07002991
henrik.lundin96bd5022016-04-06 04:13:56 -07002992 if (!jitter_buffer_playout_timestamp_) {
2993 // This can happen if this channel has not received any RTP packets. In
2994 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07002995 return;
2996 }
2997
2998 uint16_t delay_ms = 0;
2999 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003000 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003001 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3002 " delay from the ADM");
3003 _engineStatisticsPtr->SetLastError(
3004 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3005 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3006 return;
3007 }
3008
henrik.lundin96bd5022016-04-06 04:13:56 -07003009 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3010 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003011
3012 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003013 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003014
kwiberg55b97fe2016-01-28 05:22:45 -08003015 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003016 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003017 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003018
3019 {
tommi31fc21f2016-01-21 10:37:37 -08003020 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08003021 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003022 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003023 }
3024 playout_delay_ms_ = delay_ms;
3025 }
3026}
3027
kwiberg55b97fe2016-01-28 05:22:45 -08003028void Channel::RegisterReceiveCodecsToRTPModule() {
3029 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3030 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003031
kwiberg55b97fe2016-01-28 05:22:45 -08003032 CodecInst codec;
3033 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003034
kwiberg55b97fe2016-01-28 05:22:45 -08003035 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3036 // Open up the RTP/RTCP receiver for all supported codecs
3037 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08003038 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08003039 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3040 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3041 " to register %s (%d/%d/%" PRIuS
3042 "/%d) to RTP/RTCP "
3043 "receiver",
3044 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3045 codec.rate);
3046 } else {
3047 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3048 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3049 "(%d/%d/%" PRIuS
3050 "/%d) has been added to the RTP/RTCP "
3051 "receiver",
3052 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3053 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003054 }
kwiberg55b97fe2016-01-28 05:22:45 -08003055 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003056}
3057
kwiberg55b97fe2016-01-28 05:22:45 -08003058int Channel::SetSendRtpHeaderExtension(bool enable,
3059 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003060 unsigned char id) {
3061 int error = 0;
3062 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3063 if (enable) {
3064 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3065 }
3066 return error;
3067}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003068
ossue280cde2016-10-12 11:04:10 -07003069int Channel::GetRtpTimestampRateHz() const {
3070 const auto format = audio_coding_->ReceiveFormat();
3071 // Default to the playout frequency if we've not gotten any packets yet.
3072 // TODO(ossu): Zero clockrate can only happen if we've added an external
3073 // decoder for a format we don't support internally. Remove once that way of
3074 // adding decoders is gone!
3075 return (format && format->clockrate_hz != 0)
3076 ? format->clockrate_hz
3077 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003078}
3079
Minyue2013aec2015-05-13 14:14:42 +02003080int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003081 RtcpMode method = _rtpRtcpModule->RTCP();
3082 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003083 return 0;
3084 }
3085 std::vector<RTCPReportBlock> report_blocks;
3086 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003087
3088 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003089 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003090 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003091 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003092 Channel* channel = associate_send_channel_.channel();
3093 // Tries to get RTT from an associated channel. This is important for
3094 // receive-only channels.
3095 if (channel) {
3096 // To prevent infinite recursion and deadlock, calling GetRTT of
3097 // associate channel should always use "false" for argument:
3098 // |allow_associate_channel|.
3099 rtt = channel->GetRTT(false);
3100 }
3101 }
3102 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003103 }
3104
3105 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3106 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3107 for (; it != report_blocks.end(); ++it) {
3108 if (it->remoteSSRC == remoteSSRC)
3109 break;
3110 }
3111 if (it == report_blocks.end()) {
3112 // We have not received packets with SSRC matching the report blocks.
3113 // To calculate RTT we try with the SSRC of the first report block.
3114 // This is very important for send-only channels where we don't know
3115 // the SSRC of the other end.
3116 remoteSSRC = report_blocks[0].remoteSSRC;
3117 }
Minyue2013aec2015-05-13 14:14:42 +02003118
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003119 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003120 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003121 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003122 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3123 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003124 return 0;
3125 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003126 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003127}
3128
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003129} // namespace voe
3130} // namespace webrtc