blob: d630575cbcb292d5787326a9c364095fa5891d1e [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"
nissecae45d02017-04-24 05:53:20 -070017#include "webrtc/call/rtp_transport_controller_send_interface.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020018#include "webrtc/config.h"
skvladcc91d282016-10-03 18:31:22 -070019#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kwibergda2bf4e2016-10-24 13:47:09 -070020#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000021#include "webrtc/modules/audio_device/include/audio_device.h"
22#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010023#include "webrtc/modules/include/module_common_types.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010024#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010025#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
26#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
27#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
nisse657bab22017-02-21 06:28:10 -080028#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000029#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/utility/include/process_thread.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020031#include "webrtc/rtc_base/array_view.h"
32#include "webrtc/rtc_base/checks.h"
33#include "webrtc/rtc_base/criticalsection.h"
34#include "webrtc/rtc_base/format_macros.h"
35#include "webrtc/rtc_base/location.h"
36#include "webrtc/rtc_base/logging.h"
37#include "webrtc/rtc_base/rate_limiter.h"
38#include "webrtc/rtc_base/task_queue.h"
39#include "webrtc/rtc_base/thread_checker.h"
40#include "webrtc/rtc_base/timeutils.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
zsteine76bd3a2017-07-14 12:17:49 -070053constexpr double kAudioSampleDurationSeconds = 0.01;
Erik Språng737336d2016-07-29 12:59:36 +020054constexpr int64_t kMaxRetransmissionWindowMs = 1000;
55constexpr int64_t kMinRetransmissionWindowMs = 30;
56
kwibergc8d071e2016-04-06 12:22:38 -070057} // namespace
58
solenberg8842c3e2016-03-11 03:06:41 -080059const int kTelephoneEventAttenuationdB = 10;
60
ivoc14d5dbe2016-07-04 07:06:55 -070061class RtcEventLogProxy final : public webrtc::RtcEventLog {
62 public:
63 RtcEventLogProxy() : event_log_(nullptr) {}
64
65 bool StartLogging(const std::string& file_name,
66 int64_t max_size_bytes) override {
67 RTC_NOTREACHED();
68 return false;
69 }
70
71 bool StartLogging(rtc::PlatformFile log_file,
72 int64_t max_size_bytes) override {
73 RTC_NOTREACHED();
74 return false;
75 }
76
77 void StopLogging() override { RTC_NOTREACHED(); }
78
79 void LogVideoReceiveStreamConfig(
perkj09e71da2017-05-22 03:26:49 -070080 const webrtc::rtclog::StreamConfig&) override {
81 RTC_NOTREACHED();
ivoc14d5dbe2016-07-04 07:06:55 -070082 }
83
perkjc0876aa2017-05-22 04:08:28 -070084 void LogVideoSendStreamConfig(const webrtc::rtclog::StreamConfig&) override {
85 RTC_NOTREACHED();
ivoc14d5dbe2016-07-04 07:06:55 -070086 }
87
ivoce0928d82016-10-10 05:12:51 -070088 void LogAudioReceiveStreamConfig(
perkjac8f52d2017-05-22 09:36:28 -070089 const webrtc::rtclog::StreamConfig& config) override {
ivoce0928d82016-10-10 05:12:51 -070090 rtc::CritScope lock(&crit_);
91 if (event_log_) {
92 event_log_->LogAudioReceiveStreamConfig(config);
93 }
94 }
95
96 void LogAudioSendStreamConfig(
perkjf4726992017-05-22 10:12:26 -070097 const webrtc::rtclog::StreamConfig& config) override {
ivoce0928d82016-10-10 05:12:51 -070098 rtc::CritScope lock(&crit_);
99 if (event_log_) {
100 event_log_->LogAudioSendStreamConfig(config);
101 }
102 }
103
ivoc14d5dbe2016-07-04 07:06:55 -0700104 void LogRtpHeader(webrtc::PacketDirection direction,
ivoc14d5dbe2016-07-04 07:06:55 -0700105 const uint8_t* header,
106 size_t packet_length) override {
perkj77cd58e2017-05-30 03:52:10 -0700107 LogRtpHeader(direction, header, packet_length, PacedPacketInfo::kNotAProbe);
philipel32d00102017-02-27 02:18:46 -0800108 }
109
110 void LogRtpHeader(webrtc::PacketDirection direction,
philipel32d00102017-02-27 02:18:46 -0800111 const uint8_t* header,
112 size_t packet_length,
113 int probe_cluster_id) override {
ivoc14d5dbe2016-07-04 07:06:55 -0700114 rtc::CritScope lock(&crit_);
115 if (event_log_) {
perkj77cd58e2017-05-30 03:52:10 -0700116 event_log_->LogRtpHeader(direction, header, packet_length,
philipel32d00102017-02-27 02:18:46 -0800117 probe_cluster_id);
ivoc14d5dbe2016-07-04 07:06:55 -0700118 }
119 }
120
121 void LogRtcpPacket(webrtc::PacketDirection direction,
ivoc14d5dbe2016-07-04 07:06:55 -0700122 const uint8_t* packet,
123 size_t length) override {
124 rtc::CritScope lock(&crit_);
125 if (event_log_) {
perkj77cd58e2017-05-30 03:52:10 -0700126 event_log_->LogRtcpPacket(direction, packet, length);
ivoc14d5dbe2016-07-04 07:06:55 -0700127 }
128 }
129
130 void LogAudioPlayout(uint32_t ssrc) override {
131 rtc::CritScope lock(&crit_);
132 if (event_log_) {
133 event_log_->LogAudioPlayout(ssrc);
134 }
135 }
136
terelius424e6cf2017-02-20 05:14:41 -0800137 void LogLossBasedBweUpdate(int32_t bitrate_bps,
ivoc14d5dbe2016-07-04 07:06:55 -0700138 uint8_t fraction_loss,
139 int32_t total_packets) override {
140 rtc::CritScope lock(&crit_);
141 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800142 event_log_->LogLossBasedBweUpdate(bitrate_bps, fraction_loss,
143 total_packets);
ivoc14d5dbe2016-07-04 07:06:55 -0700144 }
145 }
146
terelius424e6cf2017-02-20 05:14:41 -0800147 void LogDelayBasedBweUpdate(int32_t bitrate_bps,
terelius0baf55d2017-02-17 03:38:28 -0800148 BandwidthUsage detector_state) override {
149 rtc::CritScope lock(&crit_);
150 if (event_log_) {
terelius424e6cf2017-02-20 05:14:41 -0800151 event_log_->LogDelayBasedBweUpdate(bitrate_bps, detector_state);
terelius0baf55d2017-02-17 03:38:28 -0800152 }
153 }
154
minyue4b7c9522017-01-24 04:54:59 -0800155 void LogAudioNetworkAdaptation(
michaeltcde46b72017-04-06 05:59:10 -0700156 const AudioEncoderRuntimeConfig& config) override {
minyue4b7c9522017-01-24 04:54:59 -0800157 rtc::CritScope lock(&crit_);
158 if (event_log_) {
159 event_log_->LogAudioNetworkAdaptation(config);
160 }
161 }
162
philipel32d00102017-02-27 02:18:46 -0800163 void LogProbeClusterCreated(int id,
164 int bitrate_bps,
165 int min_probes,
166 int min_bytes) override {
167 rtc::CritScope lock(&crit_);
168 if (event_log_) {
169 event_log_->LogProbeClusterCreated(id, bitrate_bps, min_probes,
170 min_bytes);
171 }
172 };
173
174 void LogProbeResultSuccess(int id, int bitrate_bps) override {
175 rtc::CritScope lock(&crit_);
176 if (event_log_) {
177 event_log_->LogProbeResultSuccess(id, bitrate_bps);
178 }
179 };
180
181 void LogProbeResultFailure(int id,
182 ProbeFailureReason failure_reason) override {
183 rtc::CritScope lock(&crit_);
184 if (event_log_) {
185 event_log_->LogProbeResultFailure(id, failure_reason);
186 }
187 };
188
ivoc14d5dbe2016-07-04 07:06:55 -0700189 void SetEventLog(RtcEventLog* event_log) {
190 rtc::CritScope lock(&crit_);
191 event_log_ = event_log;
192 }
193
194 private:
195 rtc::CriticalSection crit_;
196 RtcEventLog* event_log_ GUARDED_BY(crit_);
197 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy);
198};
199
michaelt9332b7d2016-11-30 07:51:13 -0800200class RtcpRttStatsProxy final : public RtcpRttStats {
201 public:
202 RtcpRttStatsProxy() : rtcp_rtt_stats_(nullptr) {}
203
204 void OnRttUpdate(int64_t rtt) override {
205 rtc::CritScope lock(&crit_);
206 if (rtcp_rtt_stats_)
207 rtcp_rtt_stats_->OnRttUpdate(rtt);
208 }
209
210 int64_t LastProcessedRtt() const override {
211 rtc::CritScope lock(&crit_);
212 if (!rtcp_rtt_stats_)
213 return 0;
214 return rtcp_rtt_stats_->LastProcessedRtt();
215 }
216
217 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
218 rtc::CritScope lock(&crit_);
219 rtcp_rtt_stats_ = rtcp_rtt_stats;
220 }
221
222 private:
223 rtc::CriticalSection crit_;
224 RtcpRttStats* rtcp_rtt_stats_ GUARDED_BY(crit_);
225 RTC_DISALLOW_COPY_AND_ASSIGN(RtcpRttStatsProxy);
226};
227
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100228class TransportFeedbackProxy : public TransportFeedbackObserver {
229 public:
230 TransportFeedbackProxy() : feedback_observer_(nullptr) {
231 pacer_thread_.DetachFromThread();
232 network_thread_.DetachFromThread();
233 }
234
235 void SetTransportFeedbackObserver(
236 TransportFeedbackObserver* feedback_observer) {
237 RTC_DCHECK(thread_checker_.CalledOnValidThread());
238 rtc::CritScope lock(&crit_);
239 feedback_observer_ = feedback_observer;
240 }
241
242 // Implements TransportFeedbackObserver.
elad.alond12a8e12017-03-23 11:04:48 -0700243 void AddPacket(uint32_t ssrc,
244 uint16_t sequence_number,
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100245 size_t length,
philipel8aadd502017-02-23 02:56:13 -0800246 const PacedPacketInfo& pacing_info) override {
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100247 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
248 rtc::CritScope lock(&crit_);
249 if (feedback_observer_)
elad.alond12a8e12017-03-23 11:04:48 -0700250 feedback_observer_->AddPacket(ssrc, sequence_number, length, pacing_info);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100251 }
philipel8aadd502017-02-23 02:56:13 -0800252
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100253 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
254 RTC_DCHECK(network_thread_.CalledOnValidThread());
255 rtc::CritScope lock(&crit_);
michaelt9960bb12016-10-18 09:40:34 -0700256 if (feedback_observer_)
257 feedback_observer_->OnTransportFeedback(feedback);
Stefan Holmer60e43462016-09-07 09:58:20 +0200258 }
elad.alonf9490002017-03-06 05:32:21 -0800259 std::vector<PacketFeedback> GetTransportFeedbackVector() const override {
Stefan Holmer60e43462016-09-07 09:58:20 +0200260 RTC_NOTREACHED();
elad.alonf9490002017-03-06 05:32:21 -0800261 return std::vector<PacketFeedback>();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100262 }
263
264 private:
265 rtc::CriticalSection crit_;
266 rtc::ThreadChecker thread_checker_;
267 rtc::ThreadChecker pacer_thread_;
268 rtc::ThreadChecker network_thread_;
269 TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_);
270};
271
272class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
273 public:
274 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
275 pacer_thread_.DetachFromThread();
276 }
277
278 void SetSequenceNumberAllocator(
279 TransportSequenceNumberAllocator* seq_num_allocator) {
280 RTC_DCHECK(thread_checker_.CalledOnValidThread());
281 rtc::CritScope lock(&crit_);
282 seq_num_allocator_ = seq_num_allocator;
283 }
284
285 // Implements TransportSequenceNumberAllocator.
286 uint16_t AllocateSequenceNumber() override {
287 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
288 rtc::CritScope lock(&crit_);
289 if (!seq_num_allocator_)
290 return 0;
291 return seq_num_allocator_->AllocateSequenceNumber();
292 }
293
294 private:
295 rtc::CriticalSection crit_;
296 rtc::ThreadChecker thread_checker_;
297 rtc::ThreadChecker pacer_thread_;
298 TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_);
299};
300
301class RtpPacketSenderProxy : public RtpPacketSender {
302 public:
kwiberg55b97fe2016-01-28 05:22:45 -0800303 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100304
305 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
306 RTC_DCHECK(thread_checker_.CalledOnValidThread());
307 rtc::CritScope lock(&crit_);
308 rtp_packet_sender_ = rtp_packet_sender;
309 }
310
311 // Implements RtpPacketSender.
312 void InsertPacket(Priority priority,
313 uint32_t ssrc,
314 uint16_t sequence_number,
315 int64_t capture_time_ms,
316 size_t bytes,
317 bool retransmission) override {
318 rtc::CritScope lock(&crit_);
319 if (rtp_packet_sender_) {
320 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
321 capture_time_ms, bytes, retransmission);
322 }
323 }
324
325 private:
326 rtc::ThreadChecker thread_checker_;
327 rtc::CriticalSection crit_;
328 RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_);
329};
330
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000331class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000332 public:
stefan7de8d642017-02-07 07:14:08 -0800333 explicit VoERtcpObserver(Channel* owner)
334 : owner_(owner), bandwidth_observer_(nullptr) {}
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000335 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000336
stefan7de8d642017-02-07 07:14:08 -0800337 void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
338 rtc::CritScope lock(&crit_);
339 bandwidth_observer_ = bandwidth_observer;
340 }
341
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000342 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
stefan7de8d642017-02-07 07:14:08 -0800343 rtc::CritScope lock(&crit_);
344 if (bandwidth_observer_) {
345 bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
346 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000347 }
348
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000349 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
350 int64_t rtt,
351 int64_t now_ms) override {
stefan7de8d642017-02-07 07:14:08 -0800352 {
353 rtc::CritScope lock(&crit_);
354 if (bandwidth_observer_) {
355 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
356 now_ms);
357 }
358 }
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000359 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
360 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
361 // report for VoiceEngine?
362 if (report_blocks.empty())
363 return;
364
365 int fraction_lost_aggregate = 0;
366 int total_number_of_packets = 0;
367
368 // If receiving multiple report blocks, calculate the weighted average based
369 // on the number of packets a report refers to.
370 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
371 block_it != report_blocks.end(); ++block_it) {
372 // Find the previous extended high sequence number for this remote SSRC,
373 // to calculate the number of RTP packets this report refers to. Ignore if
374 // we haven't seen this SSRC before.
375 std::map<uint32_t, uint32_t>::iterator seq_num_it =
376 extended_max_sequence_number_.find(block_it->sourceSSRC);
377 int number_of_packets = 0;
378 if (seq_num_it != extended_max_sequence_number_.end()) {
379 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
380 }
381 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
382 total_number_of_packets += number_of_packets;
383
384 extended_max_sequence_number_[block_it->sourceSSRC] =
385 block_it->extendedHighSeqNum;
386 }
387 int weighted_fraction_lost = 0;
388 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800389 weighted_fraction_lost =
390 (fraction_lost_aggregate + total_number_of_packets / 2) /
391 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000392 }
elad.alond12a8e12017-03-23 11:04:48 -0700393 owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000394 }
395
396 private:
397 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000398 // Maps remote side ssrc to extended highest sequence number received.
399 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800400 rtc::CriticalSection crit_;
401 RtcpBandwidthObserver* bandwidth_observer_ GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000402};
403
henrikaec6fbd22017-03-31 05:43:36 -0700404class Channel::ProcessAndEncodeAudioTask : public rtc::QueuedTask {
405 public:
406 ProcessAndEncodeAudioTask(std::unique_ptr<AudioFrame> audio_frame,
407 Channel* channel)
408 : audio_frame_(std::move(audio_frame)), channel_(channel) {
409 RTC_DCHECK(channel_);
410 }
411
412 private:
413 bool Run() override {
414 RTC_DCHECK_RUN_ON(channel_->encoder_queue_);
415 channel_->ProcessAndEncodeAudioOnTaskQueue(audio_frame_.get());
416 return true;
417 }
418
419 std::unique_ptr<AudioFrame> audio_frame_;
420 Channel* const channel_;
421};
422
kwiberg55b97fe2016-01-28 05:22:45 -0800423int32_t Channel::SendData(FrameType frameType,
424 uint8_t payloadType,
425 uint32_t timeStamp,
426 const uint8_t* payloadData,
427 size_t payloadSize,
428 const RTPFragmentationHeader* fragmentation) {
henrikaec6fbd22017-03-31 05:43:36 -0700429 RTC_DCHECK_RUN_ON(encoder_queue_);
kwiberg55b97fe2016-01-28 05:22:45 -0800430 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
431 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
432 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
433 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000434
kwiberg55b97fe2016-01-28 05:22:45 -0800435 if (_includeAudioLevelIndication) {
436 // Store current audio level in the RTP/RTCP module.
437 // The level will be used in combination with voice-activity state
438 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800439 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800440 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000441
kwiberg55b97fe2016-01-28 05:22:45 -0800442 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
443 // packetization.
444 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700445 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800446 (FrameType&)frameType, payloadType, timeStamp,
447 // Leaving the time when this frame was
448 // received from the capture device as
449 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700450 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800451 _engineStatisticsPtr->SetLastError(
452 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
453 "Channel::SendData() failed to send data to RTP/RTCP module");
454 return -1;
455 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000456
kwiberg55b97fe2016-01-28 05:22:45 -0800457 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000458}
459
stefan1d8a5062015-10-02 03:39:33 -0700460bool Channel::SendRtp(const uint8_t* data,
461 size_t len,
462 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800463 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
464 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000465
kwiberg55b97fe2016-01-28 05:22:45 -0800466 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000467
kwiberg55b97fe2016-01-28 05:22:45 -0800468 if (_transportPtr == NULL) {
469 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
470 "Channel::SendPacket() failed to send RTP packet due to"
471 " invalid transport object");
472 return false;
473 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000474
kwiberg55b97fe2016-01-28 05:22:45 -0800475 uint8_t* bufferToSendPtr = (uint8_t*)data;
476 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000477
kwiberg55b97fe2016-01-28 05:22:45 -0800478 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
479 std::string transport_name =
480 _externalTransport ? "external transport" : "WebRtc sockets";
481 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
482 "Channel::SendPacket() RTP transmission using %s failed",
483 transport_name.c_str());
484 return false;
485 }
486 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000487}
488
kwiberg55b97fe2016-01-28 05:22:45 -0800489bool Channel::SendRtcp(const uint8_t* data, size_t len) {
490 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
491 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
kwiberg55b97fe2016-01-28 05:22:45 -0800493 rtc::CritScope cs(&_callbackCritSect);
494 if (_transportPtr == NULL) {
495 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
496 "Channel::SendRtcp() failed to send RTCP packet"
497 " due to invalid transport object");
498 return false;
499 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000500
kwiberg55b97fe2016-01-28 05:22:45 -0800501 uint8_t* bufferToSendPtr = (uint8_t*)data;
502 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000503
kwiberg55b97fe2016-01-28 05:22:45 -0800504 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
505 if (n < 0) {
506 std::string transport_name =
507 _externalTransport ? "external transport" : "WebRtc sockets";
508 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
509 "Channel::SendRtcp() transmission using %s failed",
510 transport_name.c_str());
511 return false;
512 }
513 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000514}
515
kwiberg55b97fe2016-01-28 05:22:45 -0800516void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
517 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
518 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000519
kwiberg55b97fe2016-01-28 05:22:45 -0800520 // Update ssrc so that NTP for AV sync can be updated.
521 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000522}
523
Peter Boströmac547a62015-09-17 23:03:57 +0200524void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
525 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
526 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
527 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000528}
529
Peter Boströmac547a62015-09-17 23:03:57 +0200530int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000531 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000532 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000533 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800534 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200535 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800536 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
537 "Channel::OnInitializeDecoder(payloadType=%d, "
538 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
539 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000540
kwiberg55b97fe2016-01-28 05:22:45 -0800541 CodecInst receiveCodec = {0};
542 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000543
kwiberg55b97fe2016-01-28 05:22:45 -0800544 receiveCodec.pltype = payloadType;
545 receiveCodec.plfreq = frequency;
546 receiveCodec.channels = channels;
547 receiveCodec.rate = rate;
548 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000549
kwiberg55b97fe2016-01-28 05:22:45 -0800550 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
551 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000552
kwiberg55b97fe2016-01-28 05:22:45 -0800553 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700554 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
555 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800556 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
557 "Channel::OnInitializeDecoder() invalid codec ("
558 "pt=%d, name=%s) received - 1",
559 payloadType, payloadName);
560 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
561 return -1;
562 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000563
kwiberg55b97fe2016-01-28 05:22:45 -0800564 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000565}
566
kwiberg55b97fe2016-01-28 05:22:45 -0800567int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
568 size_t payloadSize,
569 const WebRtcRTPHeader* rtpHeader) {
570 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
571 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
572 ","
573 " payloadType=%u, audioChannel=%" PRIuS ")",
574 payloadSize, rtpHeader->header.payloadType,
575 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000576
kwiberg55b97fe2016-01-28 05:22:45 -0800577 if (!channel_state_.Get().playing) {
578 // Avoid inserting into NetEQ when we are not playing. Count the
579 // packet as discarded.
580 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
581 "received packet is discarded since playing is not"
582 " activated");
niklase@google.com470e71d2011-07-07 08:21:25 +0000583 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800584 }
585
586 // Push the incoming payload (parsed and ready for decoding) into the ACM
587 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
588 0) {
589 _engineStatisticsPtr->SetLastError(
590 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
591 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
592 return -1;
593 }
594
kwiberg55b97fe2016-01-28 05:22:45 -0800595 int64_t round_trip_time = 0;
596 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
597 NULL);
598
599 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
600 if (!nack_list.empty()) {
601 // Can't use nack_list.data() since it's not supported by all
602 // compilers.
603 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
604 }
605 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000606}
607
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000608bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000609 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000610 RTPHeader header;
611 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
612 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
613 "IncomingPacket invalid RTP header");
614 return false;
615 }
616 header.payload_type_frequency =
617 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
618 if (header.payload_type_frequency < 0)
619 return false;
620 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
621}
622
henrik.lundin42dda502016-05-18 05:36:01 -0700623MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
624 int32_t id,
625 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700626 unsigned int ssrc;
nisse7d59f6b2017-02-21 03:40:24 -0800627 RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
ivoc14d5dbe2016-07-04 07:06:55 -0700628 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800629 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700630 bool muted;
631 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
632 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800633 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
634 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
635 // In all likelihood, the audio in this frame is garbage. We return an
636 // error so that the audio mixer module doesn't add it to the mix. As
637 // a result, it won't be played out and the actions skipped here are
638 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700639 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800640 }
henrik.lundina89ab962016-05-18 08:52:45 -0700641
642 if (muted) {
643 // TODO(henrik.lundin): We should be able to do better than this. But we
644 // will have to go through all the cases below where the audio samples may
645 // be used, and handle the muted case in some way.
aleloi6321b492016-12-05 01:46:09 -0800646 AudioFrameOperations::Mute(audioFrame);
henrik.lundina89ab962016-05-18 08:52:45 -0700647 }
kwiberg55b97fe2016-01-28 05:22:45 -0800648
kwiberg55b97fe2016-01-28 05:22:45 -0800649 // Convert module ID to internal VoE channel ID
650 audioFrame->id_ = VoEChannelId(audioFrame->id_);
651 // Store speech type for dead-or-alive detection
652 _outputSpeechType = audioFrame->speech_type_;
653
654 ChannelState::State state = channel_state_.Get();
655
kwiberg55b97fe2016-01-28 05:22:45 -0800656 {
657 // Pass the audio buffers to an optional sink callback, before applying
658 // scaling/panning, as that applies to the mix operation.
659 // External recipients of the audio (e.g. via AudioTrack), will do their
660 // own mixing/dynamic processing.
661 rtc::CritScope cs(&_callbackCritSect);
662 if (audio_sink_) {
663 AudioSinkInterface::Data data(
yujo36b1a5f2017-06-12 12:45:32 -0700664 audioFrame->data(), audioFrame->samples_per_channel_,
kwiberg55b97fe2016-01-28 05:22:45 -0800665 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
666 audioFrame->timestamp_);
667 audio_sink_->OnData(data);
668 }
669 }
670
671 float output_gain = 1.0f;
kwiberg55b97fe2016-01-28 05:22:45 -0800672 {
673 rtc::CritScope cs(&volume_settings_critsect_);
674 output_gain = _outputGain;
kwiberg55b97fe2016-01-28 05:22:45 -0800675 }
676
677 // Output volume scaling
678 if (output_gain < 0.99f || output_gain > 1.01f) {
solenberg8d73f8c2017-03-08 01:52:20 -0800679 // TODO(solenberg): Combine with mute state - this can cause clicks!
oprypin67fdb802017-03-09 06:25:06 -0800680 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800681 }
682
kwiberg55b97fe2016-01-28 05:22:45 -0800683 // Mix decoded PCM output with file if file mixing is enabled
684 if (state.output_file_playing) {
685 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700686 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800687 }
688
kwiberg55b97fe2016-01-28 05:22:45 -0800689 // Record playout if enabled
690 {
691 rtc::CritScope cs(&_fileCritSect);
692
kwiberg5a25d952016-08-17 07:31:12 -0700693 if (_outputFileRecording && output_file_recorder_) {
694 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800695 }
696 }
697
698 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700699 // TODO(henrik.lundin) Use the |muted| information here too.
zstein3c451862017-07-20 09:57:42 -0700700 // TODO(deadbeef): Use RmsLevel for |_outputAudioLevel| (see
zsteine76bd3a2017-07-14 12:17:49 -0700701 // https://crbug.com/webrtc/7517).
zstein3c451862017-07-20 09:57:42 -0700702 _outputAudioLevel.ComputeLevel(*audioFrame, kAudioSampleDurationSeconds);
kwiberg55b97fe2016-01-28 05:22:45 -0800703
704 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
705 // The first frame with a valid rtp timestamp.
706 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
707 }
708
709 if (capture_start_rtp_time_stamp_ >= 0) {
710 // audioFrame.timestamp_ should be valid from now on.
711
712 // Compute elapsed time.
713 int64_t unwrap_timestamp =
714 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
715 audioFrame->elapsed_time_ms_ =
716 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700717 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800718
niklase@google.com470e71d2011-07-07 08:21:25 +0000719 {
kwiberg55b97fe2016-01-28 05:22:45 -0800720 rtc::CritScope lock(&ts_stats_lock_);
721 // Compute ntp time.
722 audioFrame->ntp_time_ms_ =
723 ntp_estimator_.Estimate(audioFrame->timestamp_);
724 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
725 if (audioFrame->ntp_time_ms_ > 0) {
726 // Compute |capture_start_ntp_time_ms_| so that
727 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
728 capture_start_ntp_time_ms_ =
729 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000730 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000731 }
kwiberg55b97fe2016-01-28 05:22:45 -0800732 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000733
henrik.lundin42dda502016-05-18 05:36:01 -0700734 return muted ? MixerParticipant::AudioFrameInfo::kMuted
735 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000736}
737
aleloi6c278492016-10-20 14:24:39 -0700738AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
739 int sample_rate_hz,
740 AudioFrame* audio_frame) {
741 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700742
aleloi6c278492016-10-20 14:24:39 -0700743 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700744
745 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
746 FrameInfo new_audio_frame_info = FrameInfo::kError;
747 switch (frame_info) {
748 case MixerParticipant::AudioFrameInfo::kNormal:
749 new_audio_frame_info = FrameInfo::kNormal;
750 break;
751 case MixerParticipant::AudioFrameInfo::kMuted:
752 new_audio_frame_info = FrameInfo::kMuted;
753 break;
754 case MixerParticipant::AudioFrameInfo::kError:
755 new_audio_frame_info = FrameInfo::kError;
756 break;
757 }
aleloi6c278492016-10-20 14:24:39 -0700758 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700759}
760
kwiberg55b97fe2016-01-28 05:22:45 -0800761int32_t Channel::NeededFrequency(int32_t id) const {
762 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
763 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000764
kwiberg55b97fe2016-01-28 05:22:45 -0800765 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000766
kwiberg55b97fe2016-01-28 05:22:45 -0800767 // Determine highest needed receive frequency
768 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000769
kwiberg55b97fe2016-01-28 05:22:45 -0800770 // Return the bigger of playout and receive frequency in the ACM.
771 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
772 highestNeeded = audio_coding_->PlayoutFrequency();
773 } else {
774 highestNeeded = receiveFrequency;
775 }
776
777 // Special case, if we're playing a file on the playout side
778 // we take that frequency into consideration as well
779 // This is not needed on sending side, since the codec will
780 // limit the spectrum anyway.
781 if (channel_state_.Get().output_file_playing) {
782 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700783 if (output_file_player_) {
784 if (output_file_player_->Frequency() > highestNeeded) {
785 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800786 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000787 }
kwiberg55b97fe2016-01-28 05:22:45 -0800788 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000789
kwiberg55b97fe2016-01-28 05:22:45 -0800790 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000791}
792
henrikaec6fbd22017-03-31 05:43:36 -0700793int32_t Channel::CreateChannel(Channel*& channel,
794 int32_t channelId,
795 uint32_t instanceId,
796 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800797 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
798 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
799 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000800
solenberg88499ec2016-09-07 07:34:41 -0700801 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800802 if (channel == NULL) {
803 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
804 "Channel::CreateChannel() unable to allocate memory for"
805 " channel");
806 return -1;
807 }
808 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000809}
810
kwiberg55b97fe2016-01-28 05:22:45 -0800811void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
812 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
813 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
814 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000815
kwiberg55b97fe2016-01-28 05:22:45 -0800816 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000817}
818
kwiberg55b97fe2016-01-28 05:22:45 -0800819void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
820 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
821 "Channel::RecordNotification(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::PlayFileEnded(int32_t id) {
828 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
829 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000830
kwiberg55b97fe2016-01-28 05:22:45 -0800831 if (id == _inputFilePlayerId) {
832 channel_state_.SetInputFilePlaying(false);
833 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
834 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000835 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800836 } else if (id == _outputFilePlayerId) {
837 channel_state_.SetOutputFilePlaying(false);
838 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
839 "Channel::PlayFileEnded() => output file player module is"
840 " shutdown");
841 }
842}
843
844void Channel::RecordFileEnded(int32_t id) {
845 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
846 "Channel::RecordFileEnded(id=%d)", id);
847
848 assert(id == _outputFileRecorderId);
849
850 rtc::CritScope cs(&_fileCritSect);
851
852 _outputFileRecording = false;
853 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
854 "Channel::RecordFileEnded() => output file recorder module is"
855 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000856}
857
pbos@webrtc.org92135212013-05-14 08:31:39 +0000858Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000859 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700860 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800861 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100862 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700863 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800864 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100865 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800866 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100867 rtp_receive_statistics_(
868 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
869 rtp_receiver_(
870 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100871 this,
872 this,
873 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700874 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100875 _outputAudioLevel(),
876 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100877 // Avoid conflict with other channels by adding 1024 - 1026,
878 // won't use as much as 1024 channels.
879 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
880 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
881 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
882 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100883 _timeStamp(0), // This is just an offset, RTP module will add it's own
884 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100885 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100886 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100887 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100888 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100889 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
890 capture_start_rtp_time_stamp_(-1),
891 capture_start_ntp_time_ms_(-1),
892 _engineStatisticsPtr(NULL),
893 _outputMixerPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100894 _moduleProcessThreadPtr(NULL),
895 _audioDeviceModulePtr(NULL),
896 _voiceEngineObserverPtr(NULL),
897 _callbackCritSectPtr(NULL),
898 _transportPtr(NULL),
solenberg1c2af8e2016-03-24 10:36:00 -0700899 input_mute_(false),
900 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100901 _outputGain(1.0f),
solenberg8d73f8c2017-03-08 01:52:20 -0800902 _mixFileWithMicrophone(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100903 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800904 transport_overhead_per_packet_(0),
905 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100906 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100907 restored_packet_in_use_(false),
908 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100909 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700910 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800911 feedback_observer_proxy_(new TransportFeedbackProxy()),
912 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700913 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200914 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
915 kMaxRetransmissionWindowMs)),
elad.alond12a8e12017-03-23 11:04:48 -0700916 decoder_factory_(config.acm_config.decoder_factory),
elad.alon28770482017-03-28 05:03:55 -0700917 use_twcc_plr_for_ana_(
918 webrtc::field_trial::FindFullName("UseTwccPlrForAna") == "Enabled") {
kwiberg55b97fe2016-01-28 05:22:45 -0800919 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
920 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700921 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800922 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700923 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800924 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200925
kwiberg55b97fe2016-01-28 05:22:45 -0800926 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000927
kwiberg55b97fe2016-01-28 05:22:45 -0800928 RtpRtcp::Configuration configuration;
929 configuration.audio = true;
930 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800931 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800932 configuration.receive_statistics = rtp_receive_statistics_.get();
933 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800934 if (pacing_enabled_) {
935 configuration.paced_sender = rtp_packet_sender_proxy_.get();
936 configuration.transport_sequence_number_allocator =
937 seq_num_allocator_proxy_.get();
938 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
939 }
ivoc14d5dbe2016-07-04 07:06:55 -0700940 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800941 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200942 configuration.retransmission_rate_limiter =
943 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000944
kwiberg55b97fe2016-01-28 05:22:45 -0800945 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100946 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000947}
948
kwiberg55b97fe2016-01-28 05:22:45 -0800949Channel::~Channel() {
tommi0a2391f2017-03-21 02:31:51 -0700950 RTC_DCHECK(!channel_state_.Get().sending);
951 RTC_DCHECK(!channel_state_.Get().playing);
niklase@google.com470e71d2011-07-07 08:21:25 +0000952}
953
kwiberg55b97fe2016-01-28 05:22:45 -0800954int32_t Channel::Init() {
tommi0a2391f2017-03-21 02:31:51 -0700955 RTC_DCHECK(construction_thread_.CalledOnValidThread());
kwiberg55b97fe2016-01-28 05:22:45 -0800956 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
957 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000958
kwiberg55b97fe2016-01-28 05:22:45 -0800959 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000960
kwiberg55b97fe2016-01-28 05:22:45 -0800961 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000962
kwiberg55b97fe2016-01-28 05:22:45 -0800963 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
964 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
965 "Channel::Init() must call SetEngineInformation() first");
966 return -1;
967 }
968
969 // --- Add modules to process thread (for periodic schedulation)
970
tommidea489f2017-03-03 03:20:24 -0800971 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
kwiberg55b97fe2016-01-28 05:22:45 -0800972
973 // --- ACM initialization
974
975 if (audio_coding_->InitializeReceiver() == -1) {
976 _engineStatisticsPtr->SetLastError(
977 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
978 "Channel::Init() unable to initialize the ACM - 1");
979 return -1;
980 }
981
982 // --- RTP/RTCP module initialization
983
984 // Ensure that RTCP is enabled by default for the created channel.
985 // Note that, the module will keep generating RTCP until it is explicitly
986 // disabled by the user.
987 // After StopListen (when no sockets exists), RTCP packets will no longer
988 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -0700989 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800990 // RTCP is enabled by default.
991 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
992 // --- Register all permanent callbacks
solenbergfe7dd6d2017-03-11 08:10:43 -0800993 if (audio_coding_->RegisterTransportCallback(this) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800994 _engineStatisticsPtr->SetLastError(
995 VE_CANNOT_INIT_CHANNEL, kTraceError,
996 "Channel::Init() callbacks not registered");
997 return -1;
998 }
999
kwiberg1c07c702017-03-27 07:15:49 -07001000 // Register a default set of send codecs.
1001 const int nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
kwiberg55b97fe2016-01-28 05:22:45 -08001002 for (int idx = 0; idx < nSupportedCodecs; idx++) {
kwiberg1c07c702017-03-27 07:15:49 -07001003 CodecInst codec;
1004 RTC_CHECK_EQ(0, audio_coding_->Codec(idx, &codec));
1005
1006 // Ensure that PCMU is used as default send codec.
1007 if (STR_CASE_CMP(codec.plname, "PCMU") == 0 && codec.channels == 1) {
1008 SetSendCodec(codec);
1009 }
1010
1011 // Register default PT for 'telephone-event'
1012 if (STR_CASE_CMP(codec.plname, "telephone-event") == 0) {
1013 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1) {
1014 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1015 "Channel::Init() failed to register outband "
1016 "'telephone-event' (%d/%d) correctly",
1017 codec.pltype, codec.plfreq);
1018 }
1019 }
1020
1021 if (STR_CASE_CMP(codec.plname, "CN") == 0) {
1022 if (!codec_manager_.RegisterEncoder(codec) ||
1023 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
1024 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
1025 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1026 "Channel::Init() failed to register CN (%d/%d) "
1027 "correctly - 1",
1028 codec.pltype, codec.plfreq);
1029 }
1030 }
1031 }
1032
1033 return 0;
1034}
1035
1036void Channel::RegisterLegacyReceiveCodecs() {
1037 const int nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1038 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1039 CodecInst codec;
1040 RTC_CHECK_EQ(0, audio_coding_->Codec(idx, &codec));
1041
kwiberg55b97fe2016-01-28 05:22:45 -08001042 // Open up the RTP/RTCP receiver for all supported codecs
kwiberg1c07c702017-03-27 07:15:49 -07001043 if (rtp_receiver_->RegisterReceivePayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001044 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1045 "Channel::Init() unable to register %s "
1046 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1047 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1048 codec.rate);
1049 } else {
1050 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1051 "Channel::Init() %s (%d/%d/%" PRIuS
1052 "/%d) has been "
1053 "added to the RTP/RTCP receiver",
1054 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1055 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001056 }
1057
kwiberg1c07c702017-03-27 07:15:49 -07001058 // Register default PT for 'telephone-event'
1059 if (STR_CASE_CMP(codec.plname, "telephone-event") == 0) {
1060 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
kwibergda2bf4e2016-10-24 13:47:09 -07001061 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001062 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
kwiberg1c07c702017-03-27 07:15:49 -07001063 "Channel::Init() failed to register inband "
kwiberg55b97fe2016-01-28 05:22:45 -08001064 "'telephone-event' (%d/%d) correctly",
1065 codec.pltype, codec.plfreq);
1066 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001067 }
1068
kwiberg1c07c702017-03-27 07:15:49 -07001069 if (STR_CASE_CMP(codec.plname, "CN") == 0) {
1070 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1071 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001072 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1073 "Channel::Init() failed to register CN (%d/%d) "
1074 "correctly - 1",
1075 codec.pltype, codec.plfreq);
1076 }
1077 }
kwiberg55b97fe2016-01-28 05:22:45 -08001078 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001079}
1080
tommi0a2391f2017-03-21 02:31:51 -07001081void Channel::Terminate() {
1082 RTC_DCHECK(construction_thread_.CalledOnValidThread());
1083 // Must be called on the same thread as Init().
1084 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
1085 "Channel::Terminate");
1086
1087 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
1088
1089 StopSend();
1090 StopPlayout();
1091
1092 {
1093 rtc::CritScope cs(&_fileCritSect);
1094 if (input_file_player_) {
1095 input_file_player_->RegisterModuleFileCallback(NULL);
1096 input_file_player_->StopPlayingFile();
1097 }
1098 if (output_file_player_) {
1099 output_file_player_->RegisterModuleFileCallback(NULL);
1100 output_file_player_->StopPlayingFile();
1101 }
1102 if (output_file_recorder_) {
1103 output_file_recorder_->RegisterModuleFileCallback(NULL);
1104 output_file_recorder_->StopRecording();
1105 }
1106 }
1107
1108 // The order to safely shutdown modules in a channel is:
1109 // 1. De-register callbacks in modules
1110 // 2. De-register modules in process thread
1111 // 3. Destroy modules
1112 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
1113 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1114 "Terminate() failed to de-register transport callback"
1115 " (Audio coding module)");
1116 }
1117
1118 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
1119 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1120 "Terminate() failed to de-register VAD callback"
1121 " (Audio coding module)");
1122 }
1123
1124 // De-register modules in process thread
1125 if (_moduleProcessThreadPtr)
1126 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
1127
1128 // End of modules shutdown
1129}
1130
kwiberg55b97fe2016-01-28 05:22:45 -08001131int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1132 OutputMixer& outputMixer,
kwiberg55b97fe2016-01-28 05:22:45 -08001133 ProcessThread& moduleProcessThread,
1134 AudioDeviceModule& audioDeviceModule,
1135 VoiceEngineObserver* voiceEngineObserver,
henrikaec6fbd22017-03-31 05:43:36 -07001136 rtc::CriticalSection* callbackCritSect,
1137 rtc::TaskQueue* encoder_queue) {
1138 RTC_DCHECK(encoder_queue);
1139 RTC_DCHECK(!encoder_queue_);
kwiberg55b97fe2016-01-28 05:22:45 -08001140 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1141 "Channel::SetEngineInformation()");
1142 _engineStatisticsPtr = &engineStatistics;
1143 _outputMixerPtr = &outputMixer;
kwiberg55b97fe2016-01-28 05:22:45 -08001144 _moduleProcessThreadPtr = &moduleProcessThread;
1145 _audioDeviceModulePtr = &audioDeviceModule;
1146 _voiceEngineObserverPtr = voiceEngineObserver;
1147 _callbackCritSectPtr = callbackCritSect;
henrikaec6fbd22017-03-31 05:43:36 -07001148 encoder_queue_ = encoder_queue;
kwiberg55b97fe2016-01-28 05:22:45 -08001149 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001150}
1151
kwibergb7f89d62016-02-17 10:04:18 -08001152void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001153 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001154 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001155}
1156
ossu29b1a8d2016-06-13 07:34:51 -07001157const rtc::scoped_refptr<AudioDecoderFactory>&
1158Channel::GetAudioDecoderFactory() const {
1159 return decoder_factory_;
1160}
1161
kwiberg55b97fe2016-01-28 05:22:45 -08001162int32_t Channel::StartPlayout() {
1163 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1164 "Channel::StartPlayout()");
1165 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001166 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001167 }
1168
solenberge374e012017-02-14 04:55:00 -08001169 // Add participant as candidates for mixing.
1170 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1171 _engineStatisticsPtr->SetLastError(
1172 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1173 "StartPlayout() failed to add participant to mixer");
1174 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001175 }
1176
1177 channel_state_.SetPlaying(true);
1178 if (RegisterFilePlayingToMixer() != 0)
1179 return -1;
1180
1181 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001182}
1183
kwiberg55b97fe2016-01-28 05:22:45 -08001184int32_t Channel::StopPlayout() {
1185 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1186 "Channel::StopPlayout()");
1187 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001188 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001189 }
1190
solenberge374e012017-02-14 04:55:00 -08001191 // Remove participant as candidates for mixing
1192 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1193 _engineStatisticsPtr->SetLastError(
1194 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1195 "StopPlayout() failed to remove participant from mixer");
1196 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001197 }
1198
1199 channel_state_.SetPlaying(false);
1200 _outputAudioLevel.Clear();
1201
1202 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001203}
1204
kwiberg55b97fe2016-01-28 05:22:45 -08001205int32_t Channel::StartSend() {
1206 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1207 "Channel::StartSend()");
kwiberg55b97fe2016-01-28 05:22:45 -08001208 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001209 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001210 }
1211 channel_state_.SetSending(true);
henrika4515fa02017-05-03 08:30:15 -07001212 {
1213 // It is now OK to start posting tasks to the encoder task queue.
1214 rtc::CritScope cs(&encoder_queue_lock_);
1215 encoder_queue_is_active_ = true;
1216 }
solenberg08b19df2017-02-15 00:42:31 -08001217 // Resume the previous sequence number which was reset by StopSend(). This
1218 // needs to be done before |sending| is set to true on the RTP/RTCP module.
1219 if (send_sequence_number_) {
1220 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
1221 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001222 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001223 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1224 _engineStatisticsPtr->SetLastError(
1225 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1226 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001227 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001228 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001229 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001230 return -1;
1231 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001232
kwiberg55b97fe2016-01-28 05:22:45 -08001233 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001234}
1235
henrikaec6fbd22017-03-31 05:43:36 -07001236void Channel::StopSend() {
kwiberg55b97fe2016-01-28 05:22:45 -08001237 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1238 "Channel::StopSend()");
1239 if (!channel_state_.Get().sending) {
henrikaec6fbd22017-03-31 05:43:36 -07001240 return;
kwiberg55b97fe2016-01-28 05:22:45 -08001241 }
1242 channel_state_.SetSending(false);
1243
henrikaec6fbd22017-03-31 05:43:36 -07001244 // Post a task to the encoder thread which sets an event when the task is
1245 // executed. We know that no more encoding tasks will be added to the task
1246 // queue for this channel since sending is now deactivated. It means that,
1247 // if we wait for the event to bet set, we know that no more pending tasks
1248 // exists and it is therfore guaranteed that the task queue will never try
1249 // to acccess and invalid channel object.
1250 RTC_DCHECK(encoder_queue_);
henrika4515fa02017-05-03 08:30:15 -07001251
henrikaec6fbd22017-03-31 05:43:36 -07001252 rtc::Event flush(false, false);
henrika4515fa02017-05-03 08:30:15 -07001253 {
1254 // Clear |encoder_queue_is_active_| under lock to prevent any other tasks
1255 // than this final "flush task" to be posted on the queue.
1256 rtc::CritScope cs(&encoder_queue_lock_);
1257 encoder_queue_is_active_ = false;
1258 encoder_queue_->PostTask([&flush]() { flush.Set(); });
1259 }
henrikaec6fbd22017-03-31 05:43:36 -07001260 flush.Wait(rtc::Event::kForever);
1261
kwiberg55b97fe2016-01-28 05:22:45 -08001262 // Store the sequence number to be able to pick up the same sequence for
1263 // the next StartSend(). This is needed for restarting device, otherwise
1264 // it might cause libSRTP to complain about packets being replayed.
1265 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1266 // CL is landed. See issue
1267 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1268 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1269
1270 // Reset sending SSRC and sequence number and triggers direct transmission
1271 // of RTCP BYE
1272 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1273 _engineStatisticsPtr->SetLastError(
1274 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1275 "StartSend() RTP/RTCP failed to stop sending");
1276 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001277 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001278}
1279
ossu1ffbd6c2017-04-06 12:05:04 -07001280bool Channel::SetEncoder(int payload_type,
1281 std::unique_ptr<AudioEncoder> encoder) {
1282 RTC_DCHECK_GE(payload_type, 0);
1283 RTC_DCHECK_LE(payload_type, 127);
ossu76d29f92017-06-09 07:30:13 -07001284 // TODO(ossu): Make CodecInsts up, for now: one for the RTP/RTCP module and
1285 // one for for us to keep track of sample rate and number of channels, etc.
1286
1287 // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate)
1288 // as well as some other things, so we collect this info and send it along.
1289 CodecInst rtp_codec;
1290 rtp_codec.pltype = payload_type;
1291 strncpy(rtp_codec.plname, "audio", sizeof(rtp_codec.plname));
1292 rtp_codec.plname[sizeof(rtp_codec.plname) - 1] = 0;
ossu1ffbd6c2017-04-06 12:05:04 -07001293 // Seems unclear if it should be clock rate or sample rate. CodecInst
1294 // supposedly carries the sample rate, but only clock rate seems sensible to
1295 // send to the RTP/RTCP module.
ossu76d29f92017-06-09 07:30:13 -07001296 rtp_codec.plfreq = encoder->RtpTimestampRateHz();
1297 rtp_codec.pacsize = rtc::CheckedDivExact(
1298 static_cast<int>(encoder->Max10MsFramesInAPacket() * rtp_codec.plfreq),
1299 100);
1300 rtp_codec.channels = encoder->NumChannels();
1301 rtp_codec.rate = 0;
ossu1ffbd6c2017-04-06 12:05:04 -07001302
ossu76d29f92017-06-09 07:30:13 -07001303 // For audio encoding we need, instead, the actual sample rate of the codec.
1304 // The rest of the information should be the same.
1305 CodecInst send_codec = rtp_codec;
1306 send_codec.plfreq = encoder->SampleRateHz();
1307 cached_send_codec_.emplace(send_codec);
1308
1309 if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) {
ossu1ffbd6c2017-04-06 12:05:04 -07001310 _rtpRtcpModule->DeRegisterSendPayload(payload_type);
ossu76d29f92017-06-09 07:30:13 -07001311 if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) {
ossu1ffbd6c2017-04-06 12:05:04 -07001312 WEBRTC_TRACE(
1313 kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1314 "SetEncoder() failed to register codec to RTP/RTCP module");
1315 return false;
1316 }
1317 }
1318
1319 audio_coding_->SetEncoder(std::move(encoder));
ossu20a4b3f2017-04-27 02:08:52 -07001320 codec_manager_.UnsetCodecInst();
ossu1ffbd6c2017-04-06 12:05:04 -07001321 return true;
1322}
1323
ossu20a4b3f2017-04-27 02:08:52 -07001324void Channel::ModifyEncoder(
1325 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
1326 audio_coding_->ModifyEncoder(modifier);
1327}
1328
kwiberg55b97fe2016-01-28 05:22:45 -08001329int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1330 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1331 "Channel::RegisterVoiceEngineObserver()");
1332 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001333
kwiberg55b97fe2016-01-28 05:22:45 -08001334 if (_voiceEngineObserverPtr) {
1335 _engineStatisticsPtr->SetLastError(
1336 VE_INVALID_OPERATION, kTraceError,
1337 "RegisterVoiceEngineObserver() observer already enabled");
1338 return -1;
1339 }
1340 _voiceEngineObserverPtr = &observer;
1341 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001342}
1343
kwiberg55b97fe2016-01-28 05:22:45 -08001344int32_t Channel::DeRegisterVoiceEngineObserver() {
1345 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1346 "Channel::DeRegisterVoiceEngineObserver()");
1347 rtc::CritScope cs(&_callbackCritSect);
1348
1349 if (!_voiceEngineObserverPtr) {
1350 _engineStatisticsPtr->SetLastError(
1351 VE_INVALID_OPERATION, kTraceWarning,
1352 "DeRegisterVoiceEngineObserver() observer already disabled");
1353 return 0;
1354 }
1355 _voiceEngineObserverPtr = NULL;
1356 return 0;
1357}
1358
1359int32_t Channel::GetSendCodec(CodecInst& codec) {
ossu76d29f92017-06-09 07:30:13 -07001360 if (cached_send_codec_) {
1361 codec = *cached_send_codec_;
1362 return 0;
1363 } else {
ossu20a4b3f2017-04-27 02:08:52 -07001364 const CodecInst* send_codec = codec_manager_.GetCodecInst();
1365 if (send_codec) {
1366 codec = *send_codec;
1367 return 0;
1368 }
1369 }
kwiberg1fd4a4a2015-11-03 11:20:50 -08001370 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001371}
1372
kwiberg55b97fe2016-01-28 05:22:45 -08001373int32_t Channel::GetRecCodec(CodecInst& codec) {
1374 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001375}
1376
kwiberg55b97fe2016-01-28 05:22:45 -08001377int32_t Channel::SetSendCodec(const CodecInst& codec) {
1378 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1379 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001380
kwibergc8d071e2016-04-06 12:22:38 -07001381 if (!codec_manager_.RegisterEncoder(codec) ||
1382 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001383 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1384 "SetSendCodec() failed to register codec to ACM");
1385 return -1;
1386 }
1387
1388 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1389 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1390 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1391 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1392 "SetSendCodec() failed to register codec to"
1393 " RTP/RTCP module");
1394 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001395 }
kwiberg55b97fe2016-01-28 05:22:45 -08001396 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001397
ossu76d29f92017-06-09 07:30:13 -07001398 cached_send_codec_.reset();
1399
kwiberg55b97fe2016-01-28 05:22:45 -08001400 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001401}
1402
minyue78b4d562016-11-30 04:47:39 -08001403void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001404 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1405 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001406 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001407 if (*encoder) {
1408 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001409 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001410 }
1411 });
michaelt566d8202017-01-12 10:17:38 -08001412 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001413}
1414
elad.alond12a8e12017-03-23 11:04:48 -07001415void Channel::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
1416 if (!use_twcc_plr_for_ana_)
1417 return;
minyue7e304322016-10-12 05:00:55 -07001418 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
elad.alond12a8e12017-03-23 11:04:48 -07001419 if (*encoder) {
1420 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1421 }
1422 });
1423}
1424
elad.alondadb4dc2017-03-23 15:29:50 -07001425void Channel::OnRecoverableUplinkPacketLossRate(
1426 float recoverable_packet_loss_rate) {
1427 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1428 if (*encoder) {
1429 (*encoder)->OnReceivedUplinkRecoverablePacketLossFraction(
1430 recoverable_packet_loss_rate);
1431 }
1432 });
1433}
1434
elad.alond12a8e12017-03-23 11:04:48 -07001435void Channel::OnUplinkPacketLossRate(float packet_loss_rate) {
1436 if (use_twcc_plr_for_ana_)
1437 return;
1438 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1439 if (*encoder) {
1440 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1441 }
minyue7e304322016-10-12 05:00:55 -07001442 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001443}
1444
kwiberg55b97fe2016-01-28 05:22:45 -08001445int32_t Channel::SetVADStatus(bool enableVAD,
1446 ACMVADMode mode,
1447 bool disableDTX) {
1448 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1449 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001450 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1451 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1452 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001453 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1454 kTraceError,
1455 "SetVADStatus() failed to set VAD");
1456 return -1;
1457 }
1458 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001459}
1460
kwiberg55b97fe2016-01-28 05:22:45 -08001461int32_t Channel::GetVADStatus(bool& enabledVAD,
1462 ACMVADMode& mode,
1463 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001464 const auto* params = codec_manager_.GetStackParams();
1465 enabledVAD = params->use_cng;
1466 mode = params->vad_mode;
1467 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001468 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001469}
1470
kwiberg1c07c702017-03-27 07:15:49 -07001471void Channel::SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) {
1472 rtp_payload_registry_->SetAudioReceivePayloads(codecs);
1473 audio_coding_->SetReceiveCodecs(codecs);
1474}
1475
kwiberg55b97fe2016-01-28 05:22:45 -08001476int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001477 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1478}
1479
1480int32_t Channel::SetRecPayloadType(int payload_type,
1481 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001482 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1483 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001484
kwiberg55b97fe2016-01-28 05:22:45 -08001485 if (channel_state_.Get().playing) {
1486 _engineStatisticsPtr->SetLastError(
1487 VE_ALREADY_PLAYING, kTraceError,
1488 "SetRecPayloadType() unable to set PT while playing");
1489 return -1;
1490 }
kwiberg55b97fe2016-01-28 05:22:45 -08001491
kwiberg09f090c2017-03-01 01:57:11 -08001492 const CodecInst codec = SdpToCodecInst(payload_type, format);
kwibergd32bf752017-01-19 07:03:59 -08001493
1494 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001495 // De-register the selected codec (RTP/RTCP module and ACM)
1496
1497 int8_t pltype(-1);
1498 CodecInst rxCodec = codec;
1499
1500 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001501 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001502 rxCodec.pltype = pltype;
1503
1504 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1505 _engineStatisticsPtr->SetLastError(
1506 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1507 "SetRecPayloadType() RTP/RTCP-module deregistration "
1508 "failed");
1509 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001510 }
kwiberg55b97fe2016-01-28 05:22:45 -08001511 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1512 _engineStatisticsPtr->SetLastError(
1513 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1514 "SetRecPayloadType() ACM deregistration failed - 1");
1515 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001516 }
kwiberg55b97fe2016-01-28 05:22:45 -08001517 return 0;
1518 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001519
magjed56124bd2016-11-24 09:34:46 -08001520 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001521 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001522 // TODO(kwiberg): Retrying is probably not necessary, since
1523 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001524 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001525 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001526 _engineStatisticsPtr->SetLastError(
1527 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1528 "SetRecPayloadType() RTP/RTCP-module registration failed");
1529 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001530 }
kwiberg55b97fe2016-01-28 05:22:45 -08001531 }
kwibergd32bf752017-01-19 07:03:59 -08001532 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1533 audio_coding_->UnregisterReceiveCodec(payload_type);
1534 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001535 _engineStatisticsPtr->SetLastError(
1536 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1537 "SetRecPayloadType() ACM registration failed - 1");
1538 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001539 }
kwiberg55b97fe2016-01-28 05:22:45 -08001540 }
1541 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001542}
1543
kwiberg55b97fe2016-01-28 05:22:45 -08001544int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1545 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001546 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001547 _engineStatisticsPtr->SetLastError(
1548 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1549 "GetRecPayloadType() failed to retrieve RX payload type");
1550 return -1;
1551 }
1552 codec.pltype = payloadType;
1553 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001554}
1555
kwiberg55b97fe2016-01-28 05:22:45 -08001556int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1557 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1558 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001559
kwiberg55b97fe2016-01-28 05:22:45 -08001560 CodecInst codec;
1561 int32_t samplingFreqHz(-1);
1562 const size_t kMono = 1;
1563 if (frequency == kFreq32000Hz)
1564 samplingFreqHz = 32000;
1565 else if (frequency == kFreq16000Hz)
1566 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001567
kwiberg55b97fe2016-01-28 05:22:45 -08001568 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1569 _engineStatisticsPtr->SetLastError(
1570 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1571 "SetSendCNPayloadType() failed to retrieve default CN codec "
1572 "settings");
1573 return -1;
1574 }
1575
1576 // Modify the payload type (must be set to dynamic range)
1577 codec.pltype = type;
1578
kwibergc8d071e2016-04-06 12:22:38 -07001579 if (!codec_manager_.RegisterEncoder(codec) ||
1580 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001581 _engineStatisticsPtr->SetLastError(
1582 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1583 "SetSendCNPayloadType() failed to register CN to ACM");
1584 return -1;
1585 }
1586
1587 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1588 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1589 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1590 _engineStatisticsPtr->SetLastError(
1591 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1592 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1593 "module");
1594 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001595 }
kwiberg55b97fe2016-01-28 05:22:45 -08001596 }
1597 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001598}
1599
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001600int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001601 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001602 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001603
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001604 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001605 _engineStatisticsPtr->SetLastError(
1606 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001607 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001608 return -1;
1609 }
1610 return 0;
1611}
1612
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001613int Channel::SetOpusDtx(bool enable_dtx) {
1614 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1615 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001616 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001617 : audio_coding_->DisableOpusDtx();
1618 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001619 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1620 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001621 return -1;
1622 }
1623 return 0;
1624}
1625
ivoc85228d62016-07-27 04:53:47 -07001626int Channel::GetOpusDtx(bool* enabled) {
1627 int success = -1;
1628 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1629 if (encoder) {
1630 *enabled = encoder->GetDtx();
1631 success = 0;
1632 }
1633 });
1634 return success;
1635}
1636
minyue7e304322016-10-12 05:00:55 -07001637bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1638 bool success = false;
1639 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1640 if (*encoder) {
michaelt92aef172017-04-18 00:11:48 -07001641 success = (*encoder)->EnableAudioNetworkAdaptor(config_string,
1642 event_log_proxy_.get());
minyue7e304322016-10-12 05:00:55 -07001643 }
1644 });
1645 return success;
1646}
1647
1648void Channel::DisableAudioNetworkAdaptor() {
1649 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1650 if (*encoder)
1651 (*encoder)->DisableAudioNetworkAdaptor();
1652 });
1653}
1654
1655void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1656 int max_frame_length_ms) {
1657 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1658 if (*encoder) {
1659 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1660 max_frame_length_ms);
1661 }
1662 });
1663}
1664
mflodman3d7db262016-04-29 00:57:13 -07001665int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001666 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001667 "Channel::RegisterExternalTransport()");
1668
kwiberg55b97fe2016-01-28 05:22:45 -08001669 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001670 if (_externalTransport) {
1671 _engineStatisticsPtr->SetLastError(
1672 VE_INVALID_OPERATION, kTraceError,
1673 "RegisterExternalTransport() external transport already enabled");
1674 return -1;
1675 }
1676 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001677 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001678 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001679}
1680
kwiberg55b97fe2016-01-28 05:22:45 -08001681int32_t Channel::DeRegisterExternalTransport() {
1682 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1683 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001684
kwiberg55b97fe2016-01-28 05:22:45 -08001685 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001686 if (_transportPtr) {
1687 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1688 "DeRegisterExternalTransport() all transport is disabled");
1689 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001690 _engineStatisticsPtr->SetLastError(
1691 VE_INVALID_OPERATION, kTraceWarning,
1692 "DeRegisterExternalTransport() external transport already "
1693 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001694 }
1695 _externalTransport = false;
1696 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001697 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001698}
1699
nisse657bab22017-02-21 06:28:10 -08001700// TODO(nisse): Delete this method together with ReceivedRTPPacket.
1701// It's a temporary hack to support both ReceivedRTPPacket and
1702// OnRtpPacket interfaces without too much code duplication.
1703bool Channel::OnRtpPacketWithHeader(const uint8_t* received_packet,
1704 size_t length,
1705 RTPHeader *header) {
1706 // Store playout timestamp for the received RTP packet
1707 UpdatePlayoutTimestamp(false);
1708
1709 header->payload_type_frequency =
1710 rtp_payload_registry_->GetPayloadTypeFrequency(header->payloadType);
1711 if (header->payload_type_frequency < 0)
1712 return false;
1713 bool in_order = IsPacketInOrder(*header);
1714 rtp_receive_statistics_->IncomingPacket(
1715 *header, length, IsPacketRetransmitted(*header, in_order));
1716 rtp_payload_registry_->SetIncomingPayloadType(*header);
1717
1718 return ReceivePacket(received_packet, length, *header, in_order);
1719}
1720
mflodman3d7db262016-04-29 00:57:13 -07001721int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001722 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001723 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001724 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001725 "Channel::ReceivedRTPPacket()");
1726
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001727 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001728 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1729 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1730 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001731 return -1;
1732 }
nisse657bab22017-02-21 06:28:10 -08001733 return OnRtpPacketWithHeader(received_packet, length, &header) ? 0 : -1;
1734}
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001735
nisse657bab22017-02-21 06:28:10 -08001736void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
1737 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
1738 "Channel::ReceivedRTPPacket()");
1739
1740 RTPHeader header;
1741 packet.GetHeader(&header);
1742 OnRtpPacketWithHeader(packet.data(), packet.size(), &header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001743}
1744
1745bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001746 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001747 const RTPHeader& header,
1748 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001749 if (rtp_payload_registry_->IsRtx(header)) {
1750 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001751 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001752 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001753 assert(packet_length >= header.headerLength);
1754 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001755 PayloadUnion payload_specific;
1756 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001757 &payload_specific)) {
1758 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001759 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001760 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1761 payload_specific, in_order);
1762}
1763
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001764bool Channel::HandleRtxPacket(const uint8_t* packet,
1765 size_t packet_length,
1766 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001767 if (!rtp_payload_registry_->IsRtx(header))
1768 return false;
1769
1770 // Remove the RTX header and parse the original RTP header.
1771 if (packet_length < header.headerLength)
1772 return false;
1773 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1774 return false;
1775 if (restored_packet_in_use_) {
1776 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1777 "Multiple RTX headers detected, dropping packet");
1778 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001779 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001780 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001781 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1782 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001783 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1784 "Incoming RTX packet: invalid RTP header");
1785 return false;
1786 }
1787 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001788 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001789 restored_packet_in_use_ = false;
1790 return ret;
1791}
1792
1793bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1794 StreamStatistician* statistician =
1795 rtp_receive_statistics_->GetStatistician(header.ssrc);
1796 if (!statistician)
1797 return false;
1798 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001799}
1800
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001801bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1802 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001803 // Retransmissions are handled separately if RTX is enabled.
1804 if (rtp_payload_registry_->RtxEnabled())
1805 return false;
1806 StreamStatistician* statistician =
1807 rtp_receive_statistics_->GetStatistician(header.ssrc);
1808 if (!statistician)
1809 return false;
1810 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001811 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001812 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001813 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001814}
1815
mflodman3d7db262016-04-29 00:57:13 -07001816int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001817 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001818 "Channel::ReceivedRTCPPacket()");
1819 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001820 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001821
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001822 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001823 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001824 _engineStatisticsPtr->SetLastError(
1825 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1826 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1827 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001828
Minyue2013aec2015-05-13 14:14:42 +02001829 int64_t rtt = GetRTT(true);
1830 if (rtt == 0) {
1831 // Waiting for valid RTT.
1832 return 0;
1833 }
Erik Språng737336d2016-07-29 12:59:36 +02001834
1835 int64_t nack_window_ms = rtt;
1836 if (nack_window_ms < kMinRetransmissionWindowMs) {
1837 nack_window_ms = kMinRetransmissionWindowMs;
1838 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1839 nack_window_ms = kMaxRetransmissionWindowMs;
1840 }
1841 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1842
minyue7e304322016-10-12 05:00:55 -07001843 // Invoke audio encoders OnReceivedRtt().
1844 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1845 if (*encoder)
1846 (*encoder)->OnReceivedRtt(rtt);
1847 });
1848
Minyue2013aec2015-05-13 14:14:42 +02001849 uint32_t ntp_secs = 0;
1850 uint32_t ntp_frac = 0;
1851 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001852 if (0 !=
1853 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1854 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001855 // Waiting for RTCP.
1856 return 0;
1857 }
1858
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001859 {
tommi31fc21f2016-01-21 10:37:37 -08001860 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001861 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001862 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001863 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001864}
1865
niklase@google.com470e71d2011-07-07 08:21:25 +00001866int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001867 bool loop,
1868 FileFormats format,
1869 int startPosition,
1870 float volumeScaling,
1871 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001872 const CodecInst* codecInst) {
1873 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1874 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1875 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1876 "stopPosition=%d)",
1877 fileName, loop, format, volumeScaling, startPosition,
1878 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001879
kwiberg55b97fe2016-01-28 05:22:45 -08001880 if (channel_state_.Get().output_file_playing) {
1881 _engineStatisticsPtr->SetLastError(
1882 VE_ALREADY_PLAYING, kTraceError,
1883 "StartPlayingFileLocally() is already playing");
1884 return -1;
1885 }
1886
1887 {
1888 rtc::CritScope cs(&_fileCritSect);
1889
kwiberg5a25d952016-08-17 07:31:12 -07001890 if (output_file_player_) {
1891 output_file_player_->RegisterModuleFileCallback(NULL);
1892 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001893 }
1894
kwiberg5b356f42016-09-08 04:32:33 -07001895 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001896 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001897
kwiberg5a25d952016-08-17 07:31:12 -07001898 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001899 _engineStatisticsPtr->SetLastError(
1900 VE_INVALID_ARGUMENT, kTraceError,
1901 "StartPlayingFileLocally() filePlayer format is not correct");
1902 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001903 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001904
kwiberg55b97fe2016-01-28 05:22:45 -08001905 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001906
kwiberg5a25d952016-08-17 07:31:12 -07001907 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001908 fileName, loop, startPosition, volumeScaling, notificationTime,
1909 stopPosition, (const CodecInst*)codecInst) != 0) {
1910 _engineStatisticsPtr->SetLastError(
1911 VE_BAD_FILE, kTraceError,
1912 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001913 output_file_player_->StopPlayingFile();
1914 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001915 return -1;
1916 }
kwiberg5a25d952016-08-17 07:31:12 -07001917 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001918 channel_state_.SetOutputFilePlaying(true);
1919 }
1920
1921 if (RegisterFilePlayingToMixer() != 0)
1922 return -1;
1923
1924 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001925}
1926
1927int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001928 FileFormats format,
1929 int startPosition,
1930 float volumeScaling,
1931 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001932 const CodecInst* codecInst) {
1933 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1934 "Channel::StartPlayingFileLocally(format=%d,"
1935 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1936 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001937
kwiberg55b97fe2016-01-28 05:22:45 -08001938 if (stream == NULL) {
1939 _engineStatisticsPtr->SetLastError(
1940 VE_BAD_FILE, kTraceError,
1941 "StartPlayingFileLocally() NULL as input stream");
1942 return -1;
1943 }
1944
1945 if (channel_state_.Get().output_file_playing) {
1946 _engineStatisticsPtr->SetLastError(
1947 VE_ALREADY_PLAYING, kTraceError,
1948 "StartPlayingFileLocally() is already playing");
1949 return -1;
1950 }
1951
1952 {
1953 rtc::CritScope cs(&_fileCritSect);
1954
1955 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001956 if (output_file_player_) {
1957 output_file_player_->RegisterModuleFileCallback(NULL);
1958 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001959 }
1960
kwiberg55b97fe2016-01-28 05:22:45 -08001961 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001962 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001963 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001964
kwiberg5a25d952016-08-17 07:31:12 -07001965 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001966 _engineStatisticsPtr->SetLastError(
1967 VE_INVALID_ARGUMENT, kTraceError,
1968 "StartPlayingFileLocally() filePlayer format isnot correct");
1969 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001970 }
1971
kwiberg55b97fe2016-01-28 05:22:45 -08001972 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001973
kwiberg4ec01d92016-08-22 08:43:54 -07001974 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001975 volumeScaling, notificationTime,
1976 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001977 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1978 "StartPlayingFile() failed to "
1979 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001980 output_file_player_->StopPlayingFile();
1981 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001982 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001983 }
kwiberg5a25d952016-08-17 07:31:12 -07001984 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001985 channel_state_.SetOutputFilePlaying(true);
1986 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001987
kwiberg55b97fe2016-01-28 05:22:45 -08001988 if (RegisterFilePlayingToMixer() != 0)
1989 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001990
kwiberg55b97fe2016-01-28 05:22:45 -08001991 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001992}
1993
kwiberg55b97fe2016-01-28 05:22:45 -08001994int Channel::StopPlayingFileLocally() {
1995 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1996 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001997
kwiberg55b97fe2016-01-28 05:22:45 -08001998 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001999 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002000 }
2001
2002 {
2003 rtc::CritScope cs(&_fileCritSect);
2004
kwiberg5a25d952016-08-17 07:31:12 -07002005 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002006 _engineStatisticsPtr->SetLastError(
2007 VE_STOP_RECORDING_FAILED, kTraceError,
2008 "StopPlayingFile() could not stop playing");
2009 return -1;
2010 }
kwiberg5a25d952016-08-17 07:31:12 -07002011 output_file_player_->RegisterModuleFileCallback(NULL);
2012 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002013 channel_state_.SetOutputFilePlaying(false);
2014 }
2015 // _fileCritSect cannot be taken while calling
2016 // SetAnonymousMixibilityStatus. Refer to comments in
2017 // StartPlayingFileLocally(const char* ...) for more details.
2018 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
2019 _engineStatisticsPtr->SetLastError(
2020 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2021 "StopPlayingFile() failed to stop participant from playing as"
2022 "file in the mixer");
2023 return -1;
2024 }
2025
2026 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002027}
2028
kwiberg55b97fe2016-01-28 05:22:45 -08002029int Channel::IsPlayingFileLocally() const {
2030 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002031}
2032
kwiberg55b97fe2016-01-28 05:22:45 -08002033int Channel::RegisterFilePlayingToMixer() {
2034 // Return success for not registering for file playing to mixer if:
2035 // 1. playing file before playout is started on that channel.
2036 // 2. starting playout without file playing on that channel.
2037 if (!channel_state_.Get().playing ||
2038 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002039 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002040 }
2041
2042 // |_fileCritSect| cannot be taken while calling
2043 // SetAnonymousMixabilityStatus() since as soon as the participant is added
2044 // frames can be pulled by the mixer. Since the frames are generated from
2045 // the file, _fileCritSect will be taken. This would result in a deadlock.
2046 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
2047 channel_state_.SetOutputFilePlaying(false);
2048 rtc::CritScope cs(&_fileCritSect);
2049 _engineStatisticsPtr->SetLastError(
2050 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2051 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07002052 output_file_player_->StopPlayingFile();
2053 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002054 return -1;
2055 }
2056
2057 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002058}
2059
niklase@google.com470e71d2011-07-07 08:21:25 +00002060int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002061 bool loop,
2062 FileFormats format,
2063 int startPosition,
2064 float volumeScaling,
2065 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002066 const CodecInst* codecInst) {
2067 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2068 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2069 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2070 "stopPosition=%d)",
2071 fileName, loop, format, volumeScaling, startPosition,
2072 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002073
kwiberg55b97fe2016-01-28 05:22:45 -08002074 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002075
kwiberg55b97fe2016-01-28 05:22:45 -08002076 if (channel_state_.Get().input_file_playing) {
2077 _engineStatisticsPtr->SetLastError(
2078 VE_ALREADY_PLAYING, kTraceWarning,
2079 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002080 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002081 }
2082
2083 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002084 if (input_file_player_) {
2085 input_file_player_->RegisterModuleFileCallback(NULL);
2086 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002087 }
2088
2089 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002090 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002091 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002092
kwiberg5a25d952016-08-17 07:31:12 -07002093 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002094 _engineStatisticsPtr->SetLastError(
2095 VE_INVALID_ARGUMENT, kTraceError,
2096 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2097 return -1;
2098 }
2099
2100 const uint32_t notificationTime(0);
2101
kwiberg5a25d952016-08-17 07:31:12 -07002102 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002103 fileName, loop, startPosition, volumeScaling, notificationTime,
2104 stopPosition, (const CodecInst*)codecInst) != 0) {
2105 _engineStatisticsPtr->SetLastError(
2106 VE_BAD_FILE, kTraceError,
2107 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002108 input_file_player_->StopPlayingFile();
2109 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002110 return -1;
2111 }
kwiberg5a25d952016-08-17 07:31:12 -07002112 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002113 channel_state_.SetInputFilePlaying(true);
2114
2115 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002116}
2117
2118int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002119 FileFormats format,
2120 int startPosition,
2121 float volumeScaling,
2122 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002123 const CodecInst* codecInst) {
2124 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2125 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2126 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2127 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002128
kwiberg55b97fe2016-01-28 05:22:45 -08002129 if (stream == NULL) {
2130 _engineStatisticsPtr->SetLastError(
2131 VE_BAD_FILE, kTraceError,
2132 "StartPlayingFileAsMicrophone NULL as input stream");
2133 return -1;
2134 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002135
kwiberg55b97fe2016-01-28 05:22:45 -08002136 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002137
kwiberg55b97fe2016-01-28 05:22:45 -08002138 if (channel_state_.Get().input_file_playing) {
2139 _engineStatisticsPtr->SetLastError(
2140 VE_ALREADY_PLAYING, kTraceWarning,
2141 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002142 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002143 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002144
kwiberg55b97fe2016-01-28 05:22:45 -08002145 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002146 if (input_file_player_) {
2147 input_file_player_->RegisterModuleFileCallback(NULL);
2148 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002149 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002150
kwiberg55b97fe2016-01-28 05:22:45 -08002151 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002152 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002153 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002154
kwiberg5a25d952016-08-17 07:31:12 -07002155 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002156 _engineStatisticsPtr->SetLastError(
2157 VE_INVALID_ARGUMENT, kTraceError,
2158 "StartPlayingInputFile() filePlayer format isnot correct");
2159 return -1;
2160 }
2161
2162 const uint32_t notificationTime(0);
2163
kwiberg4ec01d92016-08-22 08:43:54 -07002164 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2165 notificationTime, stopPosition,
2166 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002167 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2168 "StartPlayingFile() failed to start "
2169 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002170 input_file_player_->StopPlayingFile();
2171 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002172 return -1;
2173 }
2174
kwiberg5a25d952016-08-17 07:31:12 -07002175 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002176 channel_state_.SetInputFilePlaying(true);
2177
2178 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002179}
2180
kwiberg55b97fe2016-01-28 05:22:45 -08002181int Channel::StopPlayingFileAsMicrophone() {
2182 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2183 "Channel::StopPlayingFileAsMicrophone()");
2184
2185 rtc::CritScope cs(&_fileCritSect);
2186
2187 if (!channel_state_.Get().input_file_playing) {
2188 return 0;
2189 }
2190
kwiberg5a25d952016-08-17 07:31:12 -07002191 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002192 _engineStatisticsPtr->SetLastError(
2193 VE_STOP_RECORDING_FAILED, kTraceError,
2194 "StopPlayingFile() could not stop playing");
2195 return -1;
2196 }
kwiberg5a25d952016-08-17 07:31:12 -07002197 input_file_player_->RegisterModuleFileCallback(NULL);
2198 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002199 channel_state_.SetInputFilePlaying(false);
2200
2201 return 0;
2202}
2203
2204int Channel::IsPlayingFileAsMicrophone() const {
2205 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002206}
2207
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002208int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002209 const CodecInst* codecInst) {
2210 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2211 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002212
kwiberg55b97fe2016-01-28 05:22:45 -08002213 if (_outputFileRecording) {
2214 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2215 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002216 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002217 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002218
kwiberg55b97fe2016-01-28 05:22:45 -08002219 FileFormats format;
2220 const uint32_t notificationTime(0); // Not supported in VoE
2221 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002222
kwiberg55b97fe2016-01-28 05:22:45 -08002223 if ((codecInst != NULL) &&
2224 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2225 _engineStatisticsPtr->SetLastError(
2226 VE_BAD_ARGUMENT, kTraceError,
2227 "StartRecordingPlayout() invalid compression");
2228 return (-1);
2229 }
2230 if (codecInst == NULL) {
2231 format = kFileFormatPcm16kHzFile;
2232 codecInst = &dummyCodec;
2233 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2234 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2235 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2236 format = kFileFormatWavFile;
2237 } else {
2238 format = kFileFormatCompressedFile;
2239 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002240
kwiberg55b97fe2016-01-28 05:22:45 -08002241 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002242
kwiberg55b97fe2016-01-28 05:22:45 -08002243 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002244 if (output_file_recorder_) {
2245 output_file_recorder_->RegisterModuleFileCallback(NULL);
2246 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002247 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002248
kwiberg5a25d952016-08-17 07:31:12 -07002249 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002250 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002251 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002252 _engineStatisticsPtr->SetLastError(
2253 VE_INVALID_ARGUMENT, kTraceError,
2254 "StartRecordingPlayout() fileRecorder format isnot correct");
2255 return -1;
2256 }
2257
kwiberg5a25d952016-08-17 07:31:12 -07002258 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002259 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2260 _engineStatisticsPtr->SetLastError(
2261 VE_BAD_FILE, kTraceError,
2262 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002263 output_file_recorder_->StopRecording();
2264 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002265 return -1;
2266 }
kwiberg5a25d952016-08-17 07:31:12 -07002267 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002268 _outputFileRecording = true;
2269
2270 return 0;
2271}
2272
2273int Channel::StartRecordingPlayout(OutStream* stream,
2274 const CodecInst* codecInst) {
2275 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2276 "Channel::StartRecordingPlayout()");
2277
2278 if (_outputFileRecording) {
2279 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2280 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002281 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002282 }
2283
2284 FileFormats format;
2285 const uint32_t notificationTime(0); // Not supported in VoE
2286 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2287
2288 if (codecInst != NULL && codecInst->channels != 1) {
2289 _engineStatisticsPtr->SetLastError(
2290 VE_BAD_ARGUMENT, kTraceError,
2291 "StartRecordingPlayout() invalid compression");
2292 return (-1);
2293 }
2294 if (codecInst == NULL) {
2295 format = kFileFormatPcm16kHzFile;
2296 codecInst = &dummyCodec;
2297 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2298 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2299 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2300 format = kFileFormatWavFile;
2301 } else {
2302 format = kFileFormatCompressedFile;
2303 }
2304
2305 rtc::CritScope cs(&_fileCritSect);
2306
2307 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002308 if (output_file_recorder_) {
2309 output_file_recorder_->RegisterModuleFileCallback(NULL);
2310 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002311 }
2312
kwiberg5a25d952016-08-17 07:31:12 -07002313 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002314 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002315 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002316 _engineStatisticsPtr->SetLastError(
2317 VE_INVALID_ARGUMENT, kTraceError,
2318 "StartRecordingPlayout() fileRecorder format isnot correct");
2319 return -1;
2320 }
2321
kwiberg4ec01d92016-08-22 08:43:54 -07002322 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002323 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002324 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2325 "StartRecordingPlayout() failed to "
2326 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002327 output_file_recorder_->StopRecording();
2328 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002329 return -1;
2330 }
2331
kwiberg5a25d952016-08-17 07:31:12 -07002332 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002333 _outputFileRecording = true;
2334
2335 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002336}
2337
kwiberg55b97fe2016-01-28 05:22:45 -08002338int Channel::StopRecordingPlayout() {
2339 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2340 "Channel::StopRecordingPlayout()");
2341
2342 if (!_outputFileRecording) {
2343 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2344 "StopRecordingPlayout() isnot recording");
2345 return -1;
2346 }
2347
2348 rtc::CritScope cs(&_fileCritSect);
2349
kwiberg5a25d952016-08-17 07:31:12 -07002350 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002351 _engineStatisticsPtr->SetLastError(
2352 VE_STOP_RECORDING_FAILED, kTraceError,
2353 "StopRecording() could not stop recording");
2354 return (-1);
2355 }
kwiberg5a25d952016-08-17 07:31:12 -07002356 output_file_recorder_->RegisterModuleFileCallback(NULL);
2357 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002358 _outputFileRecording = false;
2359
2360 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002361}
2362
kwiberg55b97fe2016-01-28 05:22:45 -08002363void Channel::SetMixWithMicStatus(bool mix) {
2364 rtc::CritScope cs(&_fileCritSect);
2365 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002366}
2367
solenberg8d73f8c2017-03-08 01:52:20 -08002368int Channel::GetSpeechOutputLevel() const {
2369 return _outputAudioLevel.Level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002370}
2371
solenberg8d73f8c2017-03-08 01:52:20 -08002372int Channel::GetSpeechOutputLevelFullRange() const {
2373 return _outputAudioLevel.LevelFullRange();
kwiberg55b97fe2016-01-28 05:22:45 -08002374}
2375
zsteine76bd3a2017-07-14 12:17:49 -07002376double Channel::GetTotalOutputEnergy() const {
zstein3c451862017-07-20 09:57:42 -07002377 return _outputAudioLevel.TotalEnergy();
zsteine76bd3a2017-07-14 12:17:49 -07002378}
2379
2380double Channel::GetTotalOutputDuration() const {
zstein3c451862017-07-20 09:57:42 -07002381 return _outputAudioLevel.TotalDuration();
zsteine76bd3a2017-07-14 12:17:49 -07002382}
2383
solenberg8d73f8c2017-03-08 01:52:20 -08002384void Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002385 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002386 input_mute_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002387}
2388
solenberg1c2af8e2016-03-24 10:36:00 -07002389bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002390 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002391 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002392}
2393
solenberg8d73f8c2017-03-08 01:52:20 -08002394void Channel::SetChannelOutputVolumeScaling(float scaling) {
kwiberg55b97fe2016-01-28 05:22:45 -08002395 rtc::CritScope cs(&volume_settings_critsect_);
kwiberg55b97fe2016-01-28 05:22:45 -08002396 _outputGain = scaling;
niklase@google.com470e71d2011-07-07 08:21:25 +00002397}
2398
solenberg8842c3e2016-03-11 03:06:41 -08002399int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002400 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002401 "Channel::SendTelephoneEventOutband(...)");
2402 RTC_DCHECK_LE(0, event);
2403 RTC_DCHECK_GE(255, event);
2404 RTC_DCHECK_LE(0, duration_ms);
2405 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002406 if (!Sending()) {
2407 return -1;
2408 }
solenberg8842c3e2016-03-11 03:06:41 -08002409 if (_rtpRtcpModule->SendTelephoneEventOutband(
2410 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002411 _engineStatisticsPtr->SetLastError(
2412 VE_SEND_DTMF_FAILED, kTraceWarning,
2413 "SendTelephoneEventOutband() failed to send event");
2414 return -1;
2415 }
2416 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002417}
2418
solenbergffbbcac2016-11-17 05:25:37 -08002419int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2420 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002421 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002422 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002423 RTC_DCHECK_LE(0, payload_type);
2424 RTC_DCHECK_GE(127, payload_type);
2425 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002426 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002427 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002428 memcpy(codec.plname, "telephone-event", 16);
2429 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2430 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2431 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2432 _engineStatisticsPtr->SetLastError(
2433 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2434 "SetSendTelephoneEventPayloadType() failed to register send"
2435 "payload type");
2436 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002437 }
kwiberg55b97fe2016-01-28 05:22:45 -08002438 }
kwiberg55b97fe2016-01-28 05:22:45 -08002439 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002440}
2441
kwiberg55b97fe2016-01-28 05:22:45 -08002442int Channel::SetLocalSSRC(unsigned int ssrc) {
2443 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2444 "Channel::SetLocalSSRC()");
2445 if (channel_state_.Get().sending) {
2446 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2447 "SetLocalSSRC() already sending");
2448 return -1;
2449 }
2450 _rtpRtcpModule->SetSSRC(ssrc);
2451 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002452}
2453
kwiberg55b97fe2016-01-28 05:22:45 -08002454int Channel::GetLocalSSRC(unsigned int& ssrc) {
2455 ssrc = _rtpRtcpModule->SSRC();
2456 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002457}
2458
kwiberg55b97fe2016-01-28 05:22:45 -08002459int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2460 ssrc = rtp_receiver_->SSRC();
2461 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002462}
2463
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002464int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002465 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002466 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002467}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002468
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002469int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2470 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002471 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2472 if (enable &&
2473 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2474 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002475 return -1;
2476 }
2477 return 0;
2478}
2479
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002480void Channel::EnableSendTransportSequenceNumber(int id) {
2481 int ret =
2482 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2483 RTC_DCHECK_EQ(0, ret);
2484}
2485
stefan3313ec92016-01-21 06:32:43 -08002486void Channel::EnableReceiveTransportSequenceNumber(int id) {
2487 rtp_header_parser_->DeregisterRtpHeaderExtension(
2488 kRtpExtensionTransportSequenceNumber);
2489 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2490 kRtpExtensionTransportSequenceNumber, id);
2491 RTC_DCHECK(ret);
2492}
2493
stefanbba9dec2016-02-01 04:39:55 -08002494void Channel::RegisterSenderCongestionControlObjects(
nisseb8f9a322017-03-27 05:36:15 -07002495 RtpTransportControllerSendInterface* transport,
stefan7de8d642017-02-07 07:14:08 -08002496 RtcpBandwidthObserver* bandwidth_observer) {
nisseb8f9a322017-03-27 05:36:15 -07002497 RtpPacketSender* rtp_packet_sender = transport->packet_sender();
2498 TransportFeedbackObserver* transport_feedback_observer =
2499 transport->transport_feedback_observer();
2500 PacketRouter* packet_router = transport->packet_router();
2501
stefanbba9dec2016-02-01 04:39:55 -08002502 RTC_DCHECK(rtp_packet_sender);
2503 RTC_DCHECK(transport_feedback_observer);
2504 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002505 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002506 feedback_observer_proxy_->SetTransportFeedbackObserver(
2507 transport_feedback_observer);
2508 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2509 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2510 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
eladalon822ff2b2017-08-01 06:30:28 -07002511 constexpr bool remb_candidate = false;
2512 packet_router->AddSendRtpModule(_rtpRtcpModule.get(), remb_candidate);
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002513 packet_router_ = packet_router;
2514}
2515
stefanbba9dec2016-02-01 04:39:55 -08002516void Channel::RegisterReceiverCongestionControlObjects(
2517 PacketRouter* packet_router) {
2518 RTC_DCHECK(packet_router && !packet_router_);
eladalon822ff2b2017-08-01 06:30:28 -07002519 constexpr bool remb_candidate = false;
2520 packet_router->AddReceiveRtpModule(_rtpRtcpModule.get(), remb_candidate);
stefanbba9dec2016-02-01 04:39:55 -08002521 packet_router_ = packet_router;
2522}
2523
nissefdbfdc92017-03-31 05:44:52 -07002524void Channel::ResetSenderCongestionControlObjects() {
stefanbba9dec2016-02-01 04:39:55 -08002525 RTC_DCHECK(packet_router_);
2526 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002527 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002528 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2529 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
nissefdbfdc92017-03-31 05:44:52 -07002530 packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002531 packet_router_ = nullptr;
2532 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2533}
2534
nissefdbfdc92017-03-31 05:44:52 -07002535void Channel::ResetReceiverCongestionControlObjects() {
2536 RTC_DCHECK(packet_router_);
2537 packet_router_->RemoveReceiveRtpModule(_rtpRtcpModule.get());
2538 packet_router_ = nullptr;
2539}
2540
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002541void Channel::SetRTCPStatus(bool enable) {
2542 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2543 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002544 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002545}
2546
kwiberg55b97fe2016-01-28 05:22:45 -08002547int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002548 RtcpMode method = _rtpRtcpModule->RTCP();
2549 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002550 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002551}
2552
kwiberg55b97fe2016-01-28 05:22:45 -08002553int Channel::SetRTCP_CNAME(const char cName[256]) {
2554 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2555 "Channel::SetRTCP_CNAME()");
2556 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2557 _engineStatisticsPtr->SetLastError(
2558 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2559 "SetRTCP_CNAME() failed to set RTCP CNAME");
2560 return -1;
2561 }
2562 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002563}
2564
kwiberg55b97fe2016-01-28 05:22:45 -08002565int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2566 if (cName == NULL) {
2567 _engineStatisticsPtr->SetLastError(
2568 VE_INVALID_ARGUMENT, kTraceError,
2569 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2570 return -1;
2571 }
2572 char cname[RTCP_CNAME_SIZE];
2573 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2574 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2575 _engineStatisticsPtr->SetLastError(
2576 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2577 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2578 return -1;
2579 }
2580 strcpy(cName, cname);
2581 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002582}
2583
kwiberg55b97fe2016-01-28 05:22:45 -08002584int Channel::SendApplicationDefinedRTCPPacket(
2585 unsigned char subType,
2586 unsigned int name,
2587 const char* data,
2588 unsigned short dataLengthInBytes) {
2589 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2590 "Channel::SendApplicationDefinedRTCPPacket()");
2591 if (!channel_state_.Get().sending) {
2592 _engineStatisticsPtr->SetLastError(
2593 VE_NOT_SENDING, kTraceError,
2594 "SendApplicationDefinedRTCPPacket() not sending");
2595 return -1;
2596 }
2597 if (NULL == data) {
2598 _engineStatisticsPtr->SetLastError(
2599 VE_INVALID_ARGUMENT, kTraceError,
2600 "SendApplicationDefinedRTCPPacket() invalid data value");
2601 return -1;
2602 }
2603 if (dataLengthInBytes % 4 != 0) {
2604 _engineStatisticsPtr->SetLastError(
2605 VE_INVALID_ARGUMENT, kTraceError,
2606 "SendApplicationDefinedRTCPPacket() invalid length value");
2607 return -1;
2608 }
2609 RtcpMode status = _rtpRtcpModule->RTCP();
2610 if (status == RtcpMode::kOff) {
2611 _engineStatisticsPtr->SetLastError(
2612 VE_RTCP_ERROR, kTraceError,
2613 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2614 return -1;
2615 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002616
kwiberg55b97fe2016-01-28 05:22:45 -08002617 // Create and schedule the RTCP APP packet for transmission
2618 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2619 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2620 _engineStatisticsPtr->SetLastError(
2621 VE_SEND_ERROR, kTraceError,
2622 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2623 return -1;
2624 }
2625 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002626}
2627
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002628int Channel::GetRemoteRTCPReportBlocks(
2629 std::vector<ReportBlock>* report_blocks) {
2630 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002631 _engineStatisticsPtr->SetLastError(
2632 VE_INVALID_ARGUMENT, kTraceError,
2633 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002634 return -1;
2635 }
2636
2637 // Get the report blocks from the latest received RTCP Sender or Receiver
2638 // Report. Each element in the vector contains the sender's SSRC and a
2639 // report block according to RFC 3550.
2640 std::vector<RTCPReportBlock> rtcp_report_blocks;
2641 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002642 return -1;
2643 }
2644
2645 if (rtcp_report_blocks.empty())
2646 return 0;
2647
2648 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2649 for (; it != rtcp_report_blocks.end(); ++it) {
2650 ReportBlock report_block;
2651 report_block.sender_SSRC = it->remoteSSRC;
2652 report_block.source_SSRC = it->sourceSSRC;
2653 report_block.fraction_lost = it->fractionLost;
2654 report_block.cumulative_num_packets_lost = it->cumulativeLost;
2655 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
2656 report_block.interarrival_jitter = it->jitter;
2657 report_block.last_SR_timestamp = it->lastSR;
2658 report_block.delay_since_last_SR = it->delaySinceLastSR;
2659 report_blocks->push_back(report_block);
2660 }
2661 return 0;
2662}
2663
kwiberg55b97fe2016-01-28 05:22:45 -08002664int Channel::GetRTPStatistics(CallStatistics& stats) {
2665 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002666
kwiberg55b97fe2016-01-28 05:22:45 -08002667 // The jitter statistics is updated for each received RTP packet and is
2668 // based on received packets.
2669 RtcpStatistics statistics;
2670 StreamStatistician* statistician =
2671 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002672 if (statistician) {
2673 statistician->GetStatistics(&statistics,
2674 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002675 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002676
kwiberg55b97fe2016-01-28 05:22:45 -08002677 stats.fractionLost = statistics.fraction_lost;
srte186d9c32017-08-04 05:03:53 -07002678 stats.cumulativeLost = statistics.packets_lost;
2679 stats.extendedMax = statistics.extended_highest_sequence_number;
kwiberg55b97fe2016-01-28 05:22:45 -08002680 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002681
kwiberg55b97fe2016-01-28 05:22:45 -08002682 // --- RTT
2683 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002684
kwiberg55b97fe2016-01-28 05:22:45 -08002685 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002686
kwiberg55b97fe2016-01-28 05:22:45 -08002687 size_t bytesSent(0);
2688 uint32_t packetsSent(0);
2689 size_t bytesReceived(0);
2690 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002691
kwiberg55b97fe2016-01-28 05:22:45 -08002692 if (statistician) {
2693 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2694 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002695
kwiberg55b97fe2016-01-28 05:22:45 -08002696 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2697 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2698 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2699 " output will not be complete");
2700 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002701
kwiberg55b97fe2016-01-28 05:22:45 -08002702 stats.bytesSent = bytesSent;
2703 stats.packetsSent = packetsSent;
2704 stats.bytesReceived = bytesReceived;
2705 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002706
kwiberg55b97fe2016-01-28 05:22:45 -08002707 // --- Timestamps
2708 {
2709 rtc::CritScope lock(&ts_stats_lock_);
2710 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2711 }
2712 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002713}
2714
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002715int Channel::SetCodecFECStatus(bool enable) {
2716 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2717 "Channel::SetCodecFECStatus()");
2718
kwibergc8d071e2016-04-06 12:22:38 -07002719 if (!codec_manager_.SetCodecFEC(enable) ||
2720 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002721 _engineStatisticsPtr->SetLastError(
2722 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2723 "SetCodecFECStatus() failed to set FEC state");
2724 return -1;
2725 }
2726 return 0;
2727}
2728
2729bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002730 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002731}
2732
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002733void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2734 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002735 // If pacing is enabled we always store packets.
2736 if (!pacing_enabled_)
2737 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002738 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002739 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002740 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002741 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002742 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002743}
2744
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002745// Called when we are missing one or more packets.
2746int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002747 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2748}
2749
henrikaec6fbd22017-03-31 05:43:36 -07002750void Channel::ProcessAndEncodeAudio(const AudioFrame& audio_input) {
henrika4515fa02017-05-03 08:30:15 -07002751 // Avoid posting any new tasks if sending was already stopped in StopSend().
2752 rtc::CritScope cs(&encoder_queue_lock_);
2753 if (!encoder_queue_is_active_) {
2754 return;
2755 }
henrikaec6fbd22017-03-31 05:43:36 -07002756 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
2757 // TODO(henrika): try to avoid copying by moving ownership of audio frame
2758 // either into pool of frames or into the task itself.
2759 audio_frame->CopyFrom(audio_input);
2760 audio_frame->id_ = ChannelId();
2761 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
2762 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
niklase@google.com470e71d2011-07-07 08:21:25 +00002763}
2764
henrikaec6fbd22017-03-31 05:43:36 -07002765void Channel::ProcessAndEncodeAudio(const int16_t* audio_data,
2766 int sample_rate,
2767 size_t number_of_frames,
2768 size_t number_of_channels) {
henrika4515fa02017-05-03 08:30:15 -07002769 // Avoid posting as new task if sending was already stopped in StopSend().
2770 rtc::CritScope cs(&encoder_queue_lock_);
2771 if (!encoder_queue_is_active_) {
2772 return;
2773 }
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002774 CodecInst codec;
ossu950c1c92017-07-11 08:19:31 -07002775 const int result = GetSendCodec(codec);
henrikaec6fbd22017-03-31 05:43:36 -07002776 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
2777 audio_frame->id_ = ChannelId();
ossu950c1c92017-07-11 08:19:31 -07002778 // TODO(ossu): Investigate how this could happen. b/62909493
2779 if (result == 0) {
2780 audio_frame->sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2781 audio_frame->num_channels_ = std::min(number_of_channels, codec.channels);
2782 } else {
2783 audio_frame->sample_rate_hz_ = sample_rate;
2784 audio_frame->num_channels_ = number_of_channels;
2785 LOG(LS_WARNING) << "Unable to get send codec for channel " << ChannelId();
2786 RTC_NOTREACHED();
2787 }
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002788 RemixAndResample(audio_data, number_of_frames, number_of_channels,
henrikaec6fbd22017-03-31 05:43:36 -07002789 sample_rate, &input_resampler_, audio_frame.get());
2790 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
2791 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002792}
2793
henrikaec6fbd22017-03-31 05:43:36 -07002794void Channel::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
2795 RTC_DCHECK_RUN_ON(encoder_queue_);
2796 RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
2797 RTC_DCHECK_LE(audio_input->num_channels_, 2);
2798 RTC_DCHECK_EQ(audio_input->id_, ChannelId());
kwiberg55b97fe2016-01-28 05:22:45 -08002799
2800 if (channel_state_.Get().input_file_playing) {
henrikaec6fbd22017-03-31 05:43:36 -07002801 MixOrReplaceAudioWithFile(audio_input);
kwiberg55b97fe2016-01-28 05:22:45 -08002802 }
2803
henrikaec6fbd22017-03-31 05:43:36 -07002804 bool is_muted = InputMute();
2805 AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002806
kwiberg55b97fe2016-01-28 05:22:45 -08002807 if (_includeAudioLevelIndication) {
2808 size_t length =
henrikaec6fbd22017-03-31 05:43:36 -07002809 audio_input->samples_per_channel_ * audio_input->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002810 RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes);
solenberg1c2af8e2016-03-24 10:36:00 -07002811 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002812 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002813 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002814 rms_level_.Analyze(
yujo36b1a5f2017-06-12 12:45:32 -07002815 rtc::ArrayView<const int16_t>(audio_input->data(), length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002816 }
kwiberg55b97fe2016-01-28 05:22:45 -08002817 }
solenberg1c2af8e2016-03-24 10:36:00 -07002818 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002819
henrikaec6fbd22017-03-31 05:43:36 -07002820 // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002821
kwiberg55b97fe2016-01-28 05:22:45 -08002822 // The ACM resamples internally.
henrikaec6fbd22017-03-31 05:43:36 -07002823 audio_input->timestamp_ = _timeStamp;
kwiberg55b97fe2016-01-28 05:22:45 -08002824 // This call will trigger AudioPacketizationCallback::SendData if encoding
2825 // is done and payload is ready for packetization and transmission.
2826 // Otherwise, it will return without invoking the callback.
henrikaec6fbd22017-03-31 05:43:36 -07002827 if (audio_coding_->Add10MsData(*audio_input) < 0) {
2828 LOG(LS_ERROR) << "ACM::Add10MsData() failed for channel " << _channelId;
2829 return;
kwiberg55b97fe2016-01-28 05:22:45 -08002830 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002831
henrikaec6fbd22017-03-31 05:43:36 -07002832 _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002833}
2834
solenberg7602aab2016-11-14 11:30:07 -08002835void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2836 RTC_DCHECK(!channel.channel() ||
2837 channel.channel()->ChannelId() != _channelId);
2838 rtc::CritScope lock(&assoc_send_channel_lock_);
2839 associate_send_channel_ = channel;
2840}
2841
Minyue2013aec2015-05-13 14:14:42 +02002842void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002843 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002844 Channel* channel = associate_send_channel_.channel();
2845 if (channel && channel->ChannelId() == channel_id) {
2846 // If this channel is associated with a send channel of the specified
2847 // Channel ID, disassociate with it.
2848 ChannelOwner ref(NULL);
2849 associate_send_channel_ = ref;
2850 }
2851}
2852
ivoc14d5dbe2016-07-04 07:06:55 -07002853void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2854 event_log_proxy_->SetEventLog(event_log);
2855}
2856
michaelt9332b7d2016-11-30 07:51:13 -08002857void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2858 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2859}
2860
nisse284542b2017-01-10 08:58:32 -08002861void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08002862 size_t overhead_per_packet =
2863 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08002864 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2865 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08002866 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08002867 }
2868 });
2869}
2870
2871void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002872 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002873 transport_overhead_per_packet_ = transport_overhead_per_packet;
2874 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002875}
2876
hbos3fd31fe2017-02-28 05:43:16 -08002877// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08002878void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002879 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002880 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2881 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002882}
2883
kwiberg55b97fe2016-01-28 05:22:45 -08002884int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2885 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002886}
2887
wu@webrtc.org24301a62013-12-13 19:17:43 +00002888void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2889 audio_coding_->GetDecodingCallStatistics(stats);
2890}
2891
solenberg358057b2015-11-27 10:46:42 -08002892uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002893 rtc::CritScope lock(&video_sync_lock_);
2894 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002895}
2896
kwiberg55b97fe2016-01-28 05:22:45 -08002897int Channel::SetMinimumPlayoutDelay(int delayMs) {
2898 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2899 "Channel::SetMinimumPlayoutDelay()");
2900 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2901 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2902 _engineStatisticsPtr->SetLastError(
2903 VE_INVALID_ARGUMENT, kTraceError,
2904 "SetMinimumPlayoutDelay() invalid min delay");
2905 return -1;
2906 }
2907 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2908 _engineStatisticsPtr->SetLastError(
2909 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2910 "SetMinimumPlayoutDelay() failed to set min playout delay");
2911 return -1;
2912 }
2913 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002914}
2915
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002916int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002917 uint32_t playout_timestamp_rtp = 0;
2918 {
tommi31fc21f2016-01-21 10:37:37 -08002919 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002920 playout_timestamp_rtp = playout_timestamp_rtp_;
2921 }
kwiberg55b97fe2016-01-28 05:22:45 -08002922 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002923 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002924 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002925 "GetPlayoutTimestamp() failed to retrieve timestamp");
2926 return -1;
2927 }
deadbeef74375882015-08-13 12:09:10 -07002928 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002929 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002930}
2931
kwiberg55b97fe2016-01-28 05:22:45 -08002932int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2933 RtpReceiver** rtp_receiver) const {
2934 *rtpRtcpModule = _rtpRtcpModule.get();
2935 *rtp_receiver = rtp_receiver_.get();
2936 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002937}
2938
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002939// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2940// a shared helper.
henrikaec6fbd22017-03-31 05:43:36 -07002941int32_t Channel::MixOrReplaceAudioWithFile(AudioFrame* audio_input) {
2942 RTC_DCHECK_RUN_ON(encoder_queue_);
kwibergb7f89d62016-02-17 10:04:18 -08002943 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002944 size_t fileSamples(0);
henrikaec6fbd22017-03-31 05:43:36 -07002945 const int mixingFrequency = audio_input->sample_rate_hz_;
kwiberg55b97fe2016-01-28 05:22:45 -08002946 {
2947 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002948
kwiberg5a25d952016-08-17 07:31:12 -07002949 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002950 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2951 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2952 " doesnt exist");
2953 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002954 }
2955
kwiberg4ec01d92016-08-22 08:43:54 -07002956 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002957 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002958 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2959 "Channel::MixOrReplaceAudioWithFile() file mixing "
2960 "failed");
2961 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002962 }
kwiberg55b97fe2016-01-28 05:22:45 -08002963 if (fileSamples == 0) {
2964 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2965 "Channel::MixOrReplaceAudioWithFile() file is ended");
2966 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002967 }
kwiberg55b97fe2016-01-28 05:22:45 -08002968 }
2969
henrikaec6fbd22017-03-31 05:43:36 -07002970 RTC_DCHECK_EQ(audio_input->samples_per_channel_, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08002971
2972 if (_mixFileWithMicrophone) {
2973 // Currently file stream is always mono.
2974 // TODO(xians): Change the code when FilePlayer supports real stereo.
yujo36b1a5f2017-06-12 12:45:32 -07002975 MixWithSat(audio_input->mutable_data(), audio_input->num_channels_,
2976 fileBuffer.get(), 1, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08002977 } else {
2978 // Replace ACM audio with file.
2979 // Currently file stream is always mono.
2980 // TODO(xians): Change the code when FilePlayer supports real stereo.
henrikaec6fbd22017-03-31 05:43:36 -07002981 audio_input->UpdateFrame(
kwiberg55b97fe2016-01-28 05:22:45 -08002982 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2983 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2984 }
2985 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002986}
2987
kwiberg55b97fe2016-01-28 05:22:45 -08002988int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2989 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002990
kwibergb7f89d62016-02-17 10:04:18 -08002991 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002992 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002993
kwiberg55b97fe2016-01-28 05:22:45 -08002994 {
2995 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002996
kwiberg5a25d952016-08-17 07:31:12 -07002997 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002998 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2999 "Channel::MixAudioWithFile() file mixing failed");
3000 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003001 }
3002
kwiberg55b97fe2016-01-28 05:22:45 -08003003 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003004 if (output_file_player_->Get10msAudioFromFile(
3005 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003006 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3007 "Channel::MixAudioWithFile() file mixing failed");
3008 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003009 }
kwiberg55b97fe2016-01-28 05:22:45 -08003010 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003011
kwiberg55b97fe2016-01-28 05:22:45 -08003012 if (audioFrame.samples_per_channel_ == fileSamples) {
3013 // Currently file stream is always mono.
3014 // TODO(xians): Change the code when FilePlayer supports real stereo.
yujo36b1a5f2017-06-12 12:45:32 -07003015 MixWithSat(audioFrame.mutable_data(), audioFrame.num_channels_,
3016 fileBuffer.get(), 1, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08003017 } else {
3018 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3019 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3020 ") != "
3021 "fileSamples(%" PRIuS ")",
3022 audioFrame.samples_per_channel_, fileSamples);
3023 return -1;
3024 }
3025
3026 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003027}
3028
deadbeef74375882015-08-13 12:09:10 -07003029void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003030 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003031
henrik.lundin96bd5022016-04-06 04:13:56 -07003032 if (!jitter_buffer_playout_timestamp_) {
3033 // This can happen if this channel has not received any RTP packets. In
3034 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003035 return;
3036 }
3037
3038 uint16_t delay_ms = 0;
3039 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003040 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003041 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3042 " delay from the ADM");
3043 _engineStatisticsPtr->SetLastError(
3044 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3045 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3046 return;
3047 }
3048
henrik.lundin96bd5022016-04-06 04:13:56 -07003049 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3050 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003051
3052 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003053 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003054
kwiberg55b97fe2016-01-28 05:22:45 -08003055 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003056 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003057 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003058
3059 {
tommi31fc21f2016-01-21 10:37:37 -08003060 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08003061 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003062 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003063 }
3064 playout_delay_ms_ = delay_ms;
3065 }
3066}
3067
kwiberg55b97fe2016-01-28 05:22:45 -08003068void Channel::RegisterReceiveCodecsToRTPModule() {
3069 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3070 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003071
kwiberg55b97fe2016-01-28 05:22:45 -08003072 CodecInst codec;
3073 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003074
kwiberg55b97fe2016-01-28 05:22:45 -08003075 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3076 // Open up the RTP/RTCP receiver for all supported codecs
3077 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08003078 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08003079 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3080 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3081 " to register %s (%d/%d/%" PRIuS
3082 "/%d) to RTP/RTCP "
3083 "receiver",
3084 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3085 codec.rate);
3086 } else {
3087 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3088 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3089 "(%d/%d/%" PRIuS
3090 "/%d) has been added to the RTP/RTCP "
3091 "receiver",
3092 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3093 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003094 }
kwiberg55b97fe2016-01-28 05:22:45 -08003095 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003096}
3097
kwiberg55b97fe2016-01-28 05:22:45 -08003098int Channel::SetSendRtpHeaderExtension(bool enable,
3099 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003100 unsigned char id) {
3101 int error = 0;
3102 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3103 if (enable) {
3104 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3105 }
3106 return error;
3107}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003108
ossue280cde2016-10-12 11:04:10 -07003109int Channel::GetRtpTimestampRateHz() const {
3110 const auto format = audio_coding_->ReceiveFormat();
3111 // Default to the playout frequency if we've not gotten any packets yet.
3112 // TODO(ossu): Zero clockrate can only happen if we've added an external
3113 // decoder for a format we don't support internally. Remove once that way of
3114 // adding decoders is gone!
3115 return (format && format->clockrate_hz != 0)
3116 ? format->clockrate_hz
3117 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003118}
3119
Minyue2013aec2015-05-13 14:14:42 +02003120int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003121 RtcpMode method = _rtpRtcpModule->RTCP();
3122 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003123 return 0;
3124 }
3125 std::vector<RTCPReportBlock> report_blocks;
3126 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003127
3128 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003129 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003130 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003131 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003132 Channel* channel = associate_send_channel_.channel();
3133 // Tries to get RTT from an associated channel. This is important for
3134 // receive-only channels.
3135 if (channel) {
3136 // To prevent infinite recursion and deadlock, calling GetRTT of
3137 // associate channel should always use "false" for argument:
3138 // |allow_associate_channel|.
3139 rtt = channel->GetRTT(false);
3140 }
3141 }
3142 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003143 }
3144
3145 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3146 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3147 for (; it != report_blocks.end(); ++it) {
3148 if (it->remoteSSRC == remoteSSRC)
3149 break;
3150 }
3151 if (it == report_blocks.end()) {
3152 // We have not received packets with SSRC matching the report blocks.
3153 // To calculate RTT we try with the SSRC of the first report block.
3154 // This is very important for send-only channels where we don't know
3155 // the SSRC of the other end.
3156 remoteSSRC = report_blocks[0].remoteSSRC;
3157 }
Minyue2013aec2015-05-13 14:14:42 +02003158
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003159 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003160 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003161 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003162 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3163 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003164 return 0;
3165 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003166 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003167}
3168
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003169} // namespace voe
3170} // namespace webrtc