blob: 9158d362c684104a913d2dde6e7c26877e98f926 [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 =
srte3e69e5c2017-08-09 06:13:45 -0700376 extended_max_sequence_number_.find(block_it->source_ssrc);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000377 int number_of_packets = 0;
378 if (seq_num_it != extended_max_sequence_number_.end()) {
srte3e69e5c2017-08-09 06:13:45 -0700379 number_of_packets =
380 block_it->extended_highest_sequence_number - seq_num_it->second;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000381 }
srte3e69e5c2017-08-09 06:13:45 -0700382 fraction_lost_aggregate += number_of_packets * block_it->fraction_lost;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000383 total_number_of_packets += number_of_packets;
384
srte3e69e5c2017-08-09 06:13:45 -0700385 extended_max_sequence_number_[block_it->source_ssrc] =
386 block_it->extended_highest_sequence_number;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000387 }
388 int weighted_fraction_lost = 0;
389 if (total_number_of_packets > 0) {
kwiberg55b97fe2016-01-28 05:22:45 -0800390 weighted_fraction_lost =
391 (fraction_lost_aggregate + total_number_of_packets / 2) /
392 total_number_of_packets;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000393 }
elad.alond12a8e12017-03-23 11:04:48 -0700394 owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000395 }
396
397 private:
398 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000399 // Maps remote side ssrc to extended highest sequence number received.
400 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
stefan7de8d642017-02-07 07:14:08 -0800401 rtc::CriticalSection crit_;
402 RtcpBandwidthObserver* bandwidth_observer_ GUARDED_BY(crit_);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000403};
404
henrikaec6fbd22017-03-31 05:43:36 -0700405class Channel::ProcessAndEncodeAudioTask : public rtc::QueuedTask {
406 public:
407 ProcessAndEncodeAudioTask(std::unique_ptr<AudioFrame> audio_frame,
408 Channel* channel)
409 : audio_frame_(std::move(audio_frame)), channel_(channel) {
410 RTC_DCHECK(channel_);
411 }
412
413 private:
414 bool Run() override {
415 RTC_DCHECK_RUN_ON(channel_->encoder_queue_);
416 channel_->ProcessAndEncodeAudioOnTaskQueue(audio_frame_.get());
417 return true;
418 }
419
420 std::unique_ptr<AudioFrame> audio_frame_;
421 Channel* const channel_;
422};
423
kwiberg55b97fe2016-01-28 05:22:45 -0800424int32_t Channel::SendData(FrameType frameType,
425 uint8_t payloadType,
426 uint32_t timeStamp,
427 const uint8_t* payloadData,
428 size_t payloadSize,
429 const RTPFragmentationHeader* fragmentation) {
henrikaec6fbd22017-03-31 05:43:36 -0700430 RTC_DCHECK_RUN_ON(encoder_queue_);
kwiberg55b97fe2016-01-28 05:22:45 -0800431 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
432 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
433 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
434 frameType, payloadType, timeStamp, payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000435
kwiberg55b97fe2016-01-28 05:22:45 -0800436 if (_includeAudioLevelIndication) {
437 // Store current audio level in the RTP/RTCP module.
438 // The level will be used in combination with voice-activity state
439 // (frameType) to add an RTP header extension
henrik.lundin50499422016-11-29 04:26:24 -0800440 _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
kwiberg55b97fe2016-01-28 05:22:45 -0800441 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000442
kwiberg55b97fe2016-01-28 05:22:45 -0800443 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
444 // packetization.
445 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700446 if (!_rtpRtcpModule->SendOutgoingData(
kwiberg55b97fe2016-01-28 05:22:45 -0800447 (FrameType&)frameType, payloadType, timeStamp,
448 // Leaving the time when this frame was
449 // received from the capture device as
450 // undefined for voice for now.
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700451 -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
kwiberg55b97fe2016-01-28 05:22:45 -0800452 _engineStatisticsPtr->SetLastError(
453 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
454 "Channel::SendData() failed to send data to RTP/RTCP module");
455 return -1;
456 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000457
kwiberg55b97fe2016-01-28 05:22:45 -0800458 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000459}
460
stefan1d8a5062015-10-02 03:39:33 -0700461bool Channel::SendRtp(const uint8_t* data,
462 size_t len,
463 const PacketOptions& options) {
kwiberg55b97fe2016-01-28 05:22:45 -0800464 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
465 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000466
kwiberg55b97fe2016-01-28 05:22:45 -0800467 rtc::CritScope cs(&_callbackCritSect);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000468
kwiberg55b97fe2016-01-28 05:22:45 -0800469 if (_transportPtr == NULL) {
470 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
471 "Channel::SendPacket() failed to send RTP packet due to"
472 " invalid transport object");
473 return false;
474 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000475
kwiberg55b97fe2016-01-28 05:22:45 -0800476 uint8_t* bufferToSendPtr = (uint8_t*)data;
477 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000478
kwiberg55b97fe2016-01-28 05:22:45 -0800479 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) {
480 std::string transport_name =
481 _externalTransport ? "external transport" : "WebRtc sockets";
482 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
483 "Channel::SendPacket() RTP transmission using %s failed",
484 transport_name.c_str());
485 return false;
486 }
487 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000488}
489
kwiberg55b97fe2016-01-28 05:22:45 -0800490bool Channel::SendRtcp(const uint8_t* data, size_t len) {
491 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
492 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000493
kwiberg55b97fe2016-01-28 05:22:45 -0800494 rtc::CritScope cs(&_callbackCritSect);
495 if (_transportPtr == NULL) {
496 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
497 "Channel::SendRtcp() failed to send RTCP packet"
498 " due to invalid transport object");
499 return false;
500 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000501
kwiberg55b97fe2016-01-28 05:22:45 -0800502 uint8_t* bufferToSendPtr = (uint8_t*)data;
503 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000504
kwiberg55b97fe2016-01-28 05:22:45 -0800505 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
506 if (n < 0) {
507 std::string transport_name =
508 _externalTransport ? "external transport" : "WebRtc sockets";
509 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
510 "Channel::SendRtcp() transmission using %s failed",
511 transport_name.c_str());
512 return false;
513 }
514 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000515}
516
kwiberg55b97fe2016-01-28 05:22:45 -0800517void Channel::OnIncomingSSRCChanged(uint32_t ssrc) {
518 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
519 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000520
kwiberg55b97fe2016-01-28 05:22:45 -0800521 // Update ssrc so that NTP for AV sync can be updated.
522 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000523}
524
Peter Boströmac547a62015-09-17 23:03:57 +0200525void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
526 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
527 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
528 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000529}
530
Peter Boströmac547a62015-09-17 23:03:57 +0200531int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000532 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000533 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000534 int frequency,
Peter Kasting69558702016-01-12 16:26:35 -0800535 size_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200536 uint32_t rate) {
kwiberg55b97fe2016-01-28 05:22:45 -0800537 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
538 "Channel::OnInitializeDecoder(payloadType=%d, "
539 "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)",
540 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000541
kwiberg55b97fe2016-01-28 05:22:45 -0800542 CodecInst receiveCodec = {0};
543 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000544
kwiberg55b97fe2016-01-28 05:22:45 -0800545 receiveCodec.pltype = payloadType;
546 receiveCodec.plfreq = frequency;
547 receiveCodec.channels = channels;
548 receiveCodec.rate = rate;
549 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000550
kwiberg55b97fe2016-01-28 05:22:45 -0800551 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
552 receiveCodec.pacsize = dummyCodec.pacsize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000553
kwiberg55b97fe2016-01-28 05:22:45 -0800554 // Register the new codec to the ACM
kwibergda2bf4e2016-10-24 13:47:09 -0700555 if (!audio_coding_->RegisterReceiveCodec(receiveCodec.pltype,
556 CodecInstToSdp(receiveCodec))) {
kwiberg55b97fe2016-01-28 05:22:45 -0800557 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
558 "Channel::OnInitializeDecoder() invalid codec ("
559 "pt=%d, name=%s) received - 1",
560 payloadType, payloadName);
561 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
562 return -1;
563 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000564
kwiberg55b97fe2016-01-28 05:22:45 -0800565 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000566}
567
kwiberg55b97fe2016-01-28 05:22:45 -0800568int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
569 size_t payloadSize,
570 const WebRtcRTPHeader* rtpHeader) {
571 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
572 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS
573 ","
574 " payloadType=%u, audioChannel=%" PRIuS ")",
575 payloadSize, rtpHeader->header.payloadType,
576 rtpHeader->type.Audio.channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000577
kwiberg55b97fe2016-01-28 05:22:45 -0800578 if (!channel_state_.Get().playing) {
579 // Avoid inserting into NetEQ when we are not playing. Count the
580 // packet as discarded.
581 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
582 "received packet is discarded since playing is not"
583 " activated");
niklase@google.com470e71d2011-07-07 08:21:25 +0000584 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -0800585 }
586
587 // Push the incoming payload (parsed and ready for decoding) into the ACM
588 if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
589 0) {
590 _engineStatisticsPtr->SetLastError(
591 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
592 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
593 return -1;
594 }
595
kwiberg55b97fe2016-01-28 05:22:45 -0800596 int64_t round_trip_time = 0;
597 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL,
598 NULL);
599
600 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
601 if (!nack_list.empty()) {
602 // Can't use nack_list.data() since it's not supported by all
603 // compilers.
604 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
605 }
606 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000607}
608
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000609bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000610 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000611 RTPHeader header;
612 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
613 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
614 "IncomingPacket invalid RTP header");
615 return false;
616 }
617 header.payload_type_frequency =
618 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
619 if (header.payload_type_frequency < 0)
620 return false;
621 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
622}
623
henrik.lundin42dda502016-05-18 05:36:01 -0700624MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted(
625 int32_t id,
626 AudioFrame* audioFrame) {
ivoc14d5dbe2016-07-04 07:06:55 -0700627 unsigned int ssrc;
nisse7d59f6b2017-02-21 03:40:24 -0800628 RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
ivoc14d5dbe2016-07-04 07:06:55 -0700629 event_log_proxy_->LogAudioPlayout(ssrc);
kwiberg55b97fe2016-01-28 05:22:45 -0800630 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
henrik.lundind4ccb002016-05-17 12:21:55 -0700631 bool muted;
632 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame,
633 &muted) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800634 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
635 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
636 // In all likelihood, the audio in this frame is garbage. We return an
637 // error so that the audio mixer module doesn't add it to the mix. As
638 // a result, it won't be played out and the actions skipped here are
639 // irrelevant.
henrik.lundin42dda502016-05-18 05:36:01 -0700640 return MixerParticipant::AudioFrameInfo::kError;
kwiberg55b97fe2016-01-28 05:22:45 -0800641 }
henrik.lundina89ab962016-05-18 08:52:45 -0700642
643 if (muted) {
644 // TODO(henrik.lundin): We should be able to do better than this. But we
645 // will have to go through all the cases below where the audio samples may
646 // be used, and handle the muted case in some way.
aleloi6321b492016-12-05 01:46:09 -0800647 AudioFrameOperations::Mute(audioFrame);
henrik.lundina89ab962016-05-18 08:52:45 -0700648 }
kwiberg55b97fe2016-01-28 05:22:45 -0800649
kwiberg55b97fe2016-01-28 05:22:45 -0800650 // Convert module ID to internal VoE channel ID
651 audioFrame->id_ = VoEChannelId(audioFrame->id_);
652 // Store speech type for dead-or-alive detection
653 _outputSpeechType = audioFrame->speech_type_;
654
655 ChannelState::State state = channel_state_.Get();
656
kwiberg55b97fe2016-01-28 05:22:45 -0800657 {
658 // Pass the audio buffers to an optional sink callback, before applying
659 // scaling/panning, as that applies to the mix operation.
660 // External recipients of the audio (e.g. via AudioTrack), will do their
661 // own mixing/dynamic processing.
662 rtc::CritScope cs(&_callbackCritSect);
663 if (audio_sink_) {
664 AudioSinkInterface::Data data(
yujo36b1a5f2017-06-12 12:45:32 -0700665 audioFrame->data(), audioFrame->samples_per_channel_,
kwiberg55b97fe2016-01-28 05:22:45 -0800666 audioFrame->sample_rate_hz_, audioFrame->num_channels_,
667 audioFrame->timestamp_);
668 audio_sink_->OnData(data);
669 }
670 }
671
672 float output_gain = 1.0f;
kwiberg55b97fe2016-01-28 05:22:45 -0800673 {
674 rtc::CritScope cs(&volume_settings_critsect_);
675 output_gain = _outputGain;
kwiberg55b97fe2016-01-28 05:22:45 -0800676 }
677
678 // Output volume scaling
679 if (output_gain < 0.99f || output_gain > 1.01f) {
solenberg8d73f8c2017-03-08 01:52:20 -0800680 // TODO(solenberg): Combine with mute state - this can cause clicks!
oprypin67fdb802017-03-09 06:25:06 -0800681 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800682 }
683
kwiberg55b97fe2016-01-28 05:22:45 -0800684 // Mix decoded PCM output with file if file mixing is enabled
685 if (state.output_file_playing) {
686 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
henrik.lundina89ab962016-05-18 08:52:45 -0700687 muted = false; // We may have added non-zero samples.
kwiberg55b97fe2016-01-28 05:22:45 -0800688 }
689
kwiberg55b97fe2016-01-28 05:22:45 -0800690 // Record playout if enabled
691 {
692 rtc::CritScope cs(&_fileCritSect);
693
kwiberg5a25d952016-08-17 07:31:12 -0700694 if (_outputFileRecording && output_file_recorder_) {
695 output_file_recorder_->RecordAudioToFile(*audioFrame);
kwiberg55b97fe2016-01-28 05:22:45 -0800696 }
697 }
698
699 // Measure audio level (0-9)
henrik.lundina89ab962016-05-18 08:52:45 -0700700 // TODO(henrik.lundin) Use the |muted| information here too.
zstein3c451862017-07-20 09:57:42 -0700701 // TODO(deadbeef): Use RmsLevel for |_outputAudioLevel| (see
zsteine76bd3a2017-07-14 12:17:49 -0700702 // https://crbug.com/webrtc/7517).
zstein3c451862017-07-20 09:57:42 -0700703 _outputAudioLevel.ComputeLevel(*audioFrame, kAudioSampleDurationSeconds);
kwiberg55b97fe2016-01-28 05:22:45 -0800704
705 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
706 // The first frame with a valid rtp timestamp.
707 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
708 }
709
710 if (capture_start_rtp_time_stamp_ >= 0) {
711 // audioFrame.timestamp_ should be valid from now on.
712
713 // Compute elapsed time.
714 int64_t unwrap_timestamp =
715 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
716 audioFrame->elapsed_time_ms_ =
717 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
ossue280cde2016-10-12 11:04:10 -0700718 (GetRtpTimestampRateHz() / 1000);
kwiberg55b97fe2016-01-28 05:22:45 -0800719
niklase@google.com470e71d2011-07-07 08:21:25 +0000720 {
kwiberg55b97fe2016-01-28 05:22:45 -0800721 rtc::CritScope lock(&ts_stats_lock_);
722 // Compute ntp time.
723 audioFrame->ntp_time_ms_ =
724 ntp_estimator_.Estimate(audioFrame->timestamp_);
725 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
726 if (audioFrame->ntp_time_ms_ > 0) {
727 // Compute |capture_start_ntp_time_ms_| so that
728 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
729 capture_start_ntp_time_ms_ =
730 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000731 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000732 }
kwiberg55b97fe2016-01-28 05:22:45 -0800733 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000734
henrik.lundin42dda502016-05-18 05:36:01 -0700735 return muted ? MixerParticipant::AudioFrameInfo::kMuted
736 : MixerParticipant::AudioFrameInfo::kNormal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000737}
738
aleloi6c278492016-10-20 14:24:39 -0700739AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
740 int sample_rate_hz,
741 AudioFrame* audio_frame) {
742 audio_frame->sample_rate_hz_ = sample_rate_hz;
aleloiaed581a2016-10-20 06:32:39 -0700743
aleloi6c278492016-10-20 14:24:39 -0700744 const auto frame_info = GetAudioFrameWithMuted(-1, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700745
746 using FrameInfo = AudioMixer::Source::AudioFrameInfo;
747 FrameInfo new_audio_frame_info = FrameInfo::kError;
748 switch (frame_info) {
749 case MixerParticipant::AudioFrameInfo::kNormal:
750 new_audio_frame_info = FrameInfo::kNormal;
751 break;
752 case MixerParticipant::AudioFrameInfo::kMuted:
753 new_audio_frame_info = FrameInfo::kMuted;
754 break;
755 case MixerParticipant::AudioFrameInfo::kError:
756 new_audio_frame_info = FrameInfo::kError;
757 break;
758 }
aleloi6c278492016-10-20 14:24:39 -0700759 return new_audio_frame_info;
aleloiaed581a2016-10-20 06:32:39 -0700760}
761
kwiberg55b97fe2016-01-28 05:22:45 -0800762int32_t Channel::NeededFrequency(int32_t id) const {
763 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
764 "Channel::NeededFrequency(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000765
kwiberg55b97fe2016-01-28 05:22:45 -0800766 int highestNeeded = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000767
kwiberg55b97fe2016-01-28 05:22:45 -0800768 // Determine highest needed receive frequency
769 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000770
kwiberg55b97fe2016-01-28 05:22:45 -0800771 // Return the bigger of playout and receive frequency in the ACM.
772 if (audio_coding_->PlayoutFrequency() > receiveFrequency) {
773 highestNeeded = audio_coding_->PlayoutFrequency();
774 } else {
775 highestNeeded = receiveFrequency;
776 }
777
778 // Special case, if we're playing a file on the playout side
779 // we take that frequency into consideration as well
780 // This is not needed on sending side, since the codec will
781 // limit the spectrum anyway.
782 if (channel_state_.Get().output_file_playing) {
783 rtc::CritScope cs(&_fileCritSect);
kwiberg5a25d952016-08-17 07:31:12 -0700784 if (output_file_player_) {
785 if (output_file_player_->Frequency() > highestNeeded) {
786 highestNeeded = output_file_player_->Frequency();
kwiberg55b97fe2016-01-28 05:22:45 -0800787 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000788 }
kwiberg55b97fe2016-01-28 05:22:45 -0800789 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000790
kwiberg55b97fe2016-01-28 05:22:45 -0800791 return (highestNeeded);
niklase@google.com470e71d2011-07-07 08:21:25 +0000792}
793
henrikaec6fbd22017-03-31 05:43:36 -0700794int32_t Channel::CreateChannel(Channel*& channel,
795 int32_t channelId,
796 uint32_t instanceId,
797 const VoEBase::ChannelConfig& config) {
kwiberg55b97fe2016-01-28 05:22:45 -0800798 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
799 "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId,
800 instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000801
solenberg88499ec2016-09-07 07:34:41 -0700802 channel = new Channel(channelId, instanceId, config);
kwiberg55b97fe2016-01-28 05:22:45 -0800803 if (channel == NULL) {
804 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId),
805 "Channel::CreateChannel() unable to allocate memory for"
806 " channel");
807 return -1;
808 }
809 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000810}
811
kwiberg55b97fe2016-01-28 05:22:45 -0800812void Channel::PlayNotification(int32_t id, uint32_t durationMs) {
813 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
814 "Channel::PlayNotification(id=%d, durationMs=%d)", id,
815 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000816
kwiberg55b97fe2016-01-28 05:22:45 -0800817 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000818}
819
kwiberg55b97fe2016-01-28 05:22:45 -0800820void Channel::RecordNotification(int32_t id, uint32_t durationMs) {
821 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
822 "Channel::RecordNotification(id=%d, durationMs=%d)", id,
823 durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000824
kwiberg55b97fe2016-01-28 05:22:45 -0800825 // Not implement yet
niklase@google.com470e71d2011-07-07 08:21:25 +0000826}
827
kwiberg55b97fe2016-01-28 05:22:45 -0800828void Channel::PlayFileEnded(int32_t id) {
829 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
830 "Channel::PlayFileEnded(id=%d)", id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000831
kwiberg55b97fe2016-01-28 05:22:45 -0800832 if (id == _inputFilePlayerId) {
833 channel_state_.SetInputFilePlaying(false);
834 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
835 "Channel::PlayFileEnded() => input file player module is"
niklase@google.com470e71d2011-07-07 08:21:25 +0000836 " shutdown");
kwiberg55b97fe2016-01-28 05:22:45 -0800837 } else if (id == _outputFilePlayerId) {
838 channel_state_.SetOutputFilePlaying(false);
839 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
840 "Channel::PlayFileEnded() => output file player module is"
841 " shutdown");
842 }
843}
844
845void Channel::RecordFileEnded(int32_t id) {
846 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
847 "Channel::RecordFileEnded(id=%d)", id);
848
849 assert(id == _outputFileRecorderId);
850
851 rtc::CritScope cs(&_fileCritSect);
852
853 _outputFileRecording = false;
854 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
855 "Channel::RecordFileEnded() => output file recorder module is"
856 " shutdown");
niklase@google.com470e71d2011-07-07 08:21:25 +0000857}
858
pbos@webrtc.org92135212013-05-14 08:31:39 +0000859Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000860 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700861 const VoEBase::ChannelConfig& config)
tommi31fc21f2016-01-21 10:37:37 -0800862 : _instanceId(instanceId),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100863 _channelId(channelId),
ivoc14d5dbe2016-07-04 07:06:55 -0700864 event_log_proxy_(new RtcEventLogProxy()),
michaelt9332b7d2016-11-30 07:51:13 -0800865 rtcp_rtt_stats_proxy_(new RtcpRttStatsProxy()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100866 rtp_header_parser_(RtpHeaderParser::Create()),
magjedf3feeff2016-11-25 06:40:25 -0800867 rtp_payload_registry_(new RTPPayloadRegistry()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100868 rtp_receive_statistics_(
869 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
870 rtp_receiver_(
871 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100872 this,
873 this,
874 rtp_payload_registry_.get())),
danilchap799a9d02016-09-22 03:36:27 -0700875 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100876 _outputAudioLevel(),
877 _externalTransport(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100878 // Avoid conflict with other channels by adding 1024 - 1026,
879 // won't use as much as 1024 channels.
880 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
881 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
882 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
883 _outputFileRecording(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100884 _timeStamp(0), // This is just an offset, RTP module will add it's own
885 // random offset
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100886 ntp_estimator_(Clock::GetRealTimeClock()),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100887 playout_timestamp_rtp_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100888 playout_delay_ms_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100889 send_sequence_number_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100890 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
891 capture_start_rtp_time_stamp_(-1),
892 capture_start_ntp_time_ms_(-1),
893 _engineStatisticsPtr(NULL),
894 _outputMixerPtr(NULL),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100895 _moduleProcessThreadPtr(NULL),
896 _audioDeviceModulePtr(NULL),
897 _voiceEngineObserverPtr(NULL),
898 _callbackCritSectPtr(NULL),
899 _transportPtr(NULL),
solenberg1c2af8e2016-03-24 10:36:00 -0700900 input_mute_(false),
901 previous_frame_muted_(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100902 _outputGain(1.0f),
solenberg8d73f8c2017-03-08 01:52:20 -0800903 _mixFileWithMicrophone(false),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100904 _includeAudioLevelIndication(false),
nisse284542b2017-01-10 08:58:32 -0800905 transport_overhead_per_packet_(0),
906 rtp_overhead_per_packet_(0),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100907 _outputSpeechType(AudioFrame::kNormalSpeech),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100908 restored_packet_in_use_(false),
909 rtcp_observer_(new VoERtcpObserver(this)),
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100910 associate_send_channel_(ChannelOwner(nullptr)),
solenberg88499ec2016-09-07 07:34:41 -0700911 pacing_enabled_(config.enable_voice_pacing),
stefanbba9dec2016-02-01 04:39:55 -0800912 feedback_observer_proxy_(new TransportFeedbackProxy()),
913 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
ossu29b1a8d2016-06-13 07:34:51 -0700914 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Erik Språng737336d2016-07-29 12:59:36 +0200915 retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
916 kMaxRetransmissionWindowMs)),
elad.alond12a8e12017-03-23 11:04:48 -0700917 decoder_factory_(config.acm_config.decoder_factory),
elad.alon28770482017-03-28 05:03:55 -0700918 use_twcc_plr_for_ana_(
919 webrtc::field_trial::FindFullName("UseTwccPlrForAna") == "Enabled") {
kwiberg55b97fe2016-01-28 05:22:45 -0800920 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
921 "Channel::Channel() - ctor");
solenberg88499ec2016-09-07 07:34:41 -0700922 AudioCodingModule::Config acm_config(config.acm_config);
kwiberg55b97fe2016-01-28 05:22:45 -0800923 acm_config.id = VoEModuleId(instanceId, channelId);
henrik.lundina89ab962016-05-18 08:52:45 -0700924 acm_config.neteq_config.enable_muted_state = true;
kwiberg55b97fe2016-01-28 05:22:45 -0800925 audio_coding_.reset(AudioCodingModule::Create(acm_config));
Henrik Lundin64dad832015-05-11 12:44:23 +0200926
kwiberg55b97fe2016-01-28 05:22:45 -0800927 _outputAudioLevel.Clear();
niklase@google.com470e71d2011-07-07 08:21:25 +0000928
kwiberg55b97fe2016-01-28 05:22:45 -0800929 RtpRtcp::Configuration configuration;
930 configuration.audio = true;
931 configuration.outgoing_transport = this;
michaeltbf65be52016-12-15 06:24:49 -0800932 configuration.overhead_observer = this;
kwiberg55b97fe2016-01-28 05:22:45 -0800933 configuration.receive_statistics = rtp_receive_statistics_.get();
934 configuration.bandwidth_callback = rtcp_observer_.get();
stefanbba9dec2016-02-01 04:39:55 -0800935 if (pacing_enabled_) {
936 configuration.paced_sender = rtp_packet_sender_proxy_.get();
937 configuration.transport_sequence_number_allocator =
938 seq_num_allocator_proxy_.get();
939 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
940 }
ivoc14d5dbe2016-07-04 07:06:55 -0700941 configuration.event_log = &(*event_log_proxy_);
michaelt9332b7d2016-11-30 07:51:13 -0800942 configuration.rtt_stats = &(*rtcp_rtt_stats_proxy_);
Erik Språng737336d2016-07-29 12:59:36 +0200943 configuration.retransmission_rate_limiter =
944 retransmission_rate_limiter_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000945
kwiberg55b97fe2016-01-28 05:22:45 -0800946 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100947 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000948}
949
kwiberg55b97fe2016-01-28 05:22:45 -0800950Channel::~Channel() {
tommi0a2391f2017-03-21 02:31:51 -0700951 RTC_DCHECK(!channel_state_.Get().sending);
952 RTC_DCHECK(!channel_state_.Get().playing);
niklase@google.com470e71d2011-07-07 08:21:25 +0000953}
954
kwiberg55b97fe2016-01-28 05:22:45 -0800955int32_t Channel::Init() {
tommi0a2391f2017-03-21 02:31:51 -0700956 RTC_DCHECK(construction_thread_.CalledOnValidThread());
kwiberg55b97fe2016-01-28 05:22:45 -0800957 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
958 "Channel::Init()");
niklase@google.com470e71d2011-07-07 08:21:25 +0000959
kwiberg55b97fe2016-01-28 05:22:45 -0800960 channel_state_.Reset();
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000961
kwiberg55b97fe2016-01-28 05:22:45 -0800962 // --- Initial sanity
niklase@google.com470e71d2011-07-07 08:21:25 +0000963
kwiberg55b97fe2016-01-28 05:22:45 -0800964 if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) {
965 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
966 "Channel::Init() must call SetEngineInformation() first");
967 return -1;
968 }
969
970 // --- Add modules to process thread (for periodic schedulation)
971
tommidea489f2017-03-03 03:20:24 -0800972 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
kwiberg55b97fe2016-01-28 05:22:45 -0800973
974 // --- ACM initialization
975
976 if (audio_coding_->InitializeReceiver() == -1) {
977 _engineStatisticsPtr->SetLastError(
978 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
979 "Channel::Init() unable to initialize the ACM - 1");
980 return -1;
981 }
982
983 // --- RTP/RTCP module initialization
984
985 // Ensure that RTCP is enabled by default for the created channel.
986 // Note that, the module will keep generating RTCP until it is explicitly
987 // disabled by the user.
988 // After StopListen (when no sockets exists), RTCP packets will no longer
989 // be transmitted since the Transport object will then be invalid.
danilchap799a9d02016-09-22 03:36:27 -0700990 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
kwiberg55b97fe2016-01-28 05:22:45 -0800991 // RTCP is enabled by default.
992 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
993 // --- Register all permanent callbacks
solenbergfe7dd6d2017-03-11 08:10:43 -0800994 if (audio_coding_->RegisterTransportCallback(this) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -0800995 _engineStatisticsPtr->SetLastError(
996 VE_CANNOT_INIT_CHANNEL, kTraceError,
997 "Channel::Init() callbacks not registered");
998 return -1;
999 }
1000
kwiberg1c07c702017-03-27 07:15:49 -07001001 // Register a default set of send codecs.
1002 const int nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
kwiberg55b97fe2016-01-28 05:22:45 -08001003 for (int idx = 0; idx < nSupportedCodecs; idx++) {
kwiberg1c07c702017-03-27 07:15:49 -07001004 CodecInst codec;
1005 RTC_CHECK_EQ(0, audio_coding_->Codec(idx, &codec));
1006
1007 // Ensure that PCMU is used as default send codec.
1008 if (STR_CASE_CMP(codec.plname, "PCMU") == 0 && codec.channels == 1) {
1009 SetSendCodec(codec);
1010 }
1011
1012 // Register default PT for 'telephone-event'
1013 if (STR_CASE_CMP(codec.plname, "telephone-event") == 0) {
1014 if (_rtpRtcpModule->RegisterSendPayload(codec) == -1) {
1015 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1016 "Channel::Init() failed to register outband "
1017 "'telephone-event' (%d/%d) correctly",
1018 codec.pltype, codec.plfreq);
1019 }
1020 }
1021
1022 if (STR_CASE_CMP(codec.plname, "CN") == 0) {
1023 if (!codec_manager_.RegisterEncoder(codec) ||
1024 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) ||
1025 _rtpRtcpModule->RegisterSendPayload(codec) == -1) {
1026 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1027 "Channel::Init() failed to register CN (%d/%d) "
1028 "correctly - 1",
1029 codec.pltype, codec.plfreq);
1030 }
1031 }
1032 }
1033
1034 return 0;
1035}
1036
1037void Channel::RegisterLegacyReceiveCodecs() {
1038 const int nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
1039 for (int idx = 0; idx < nSupportedCodecs; idx++) {
1040 CodecInst codec;
1041 RTC_CHECK_EQ(0, audio_coding_->Codec(idx, &codec));
1042
kwiberg55b97fe2016-01-28 05:22:45 -08001043 // Open up the RTP/RTCP receiver for all supported codecs
kwiberg1c07c702017-03-27 07:15:49 -07001044 if (rtp_receiver_->RegisterReceivePayload(codec) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001045 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1046 "Channel::Init() unable to register %s "
1047 "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver",
1048 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1049 codec.rate);
1050 } else {
1051 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1052 "Channel::Init() %s (%d/%d/%" PRIuS
1053 "/%d) has been "
1054 "added to the RTP/RTCP receiver",
1055 codec.plname, codec.pltype, codec.plfreq, codec.channels,
1056 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00001057 }
1058
kwiberg1c07c702017-03-27 07:15:49 -07001059 // Register default PT for 'telephone-event'
1060 if (STR_CASE_CMP(codec.plname, "telephone-event") == 0) {
1061 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
kwibergda2bf4e2016-10-24 13:47:09 -07001062 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001063 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
kwiberg1c07c702017-03-27 07:15:49 -07001064 "Channel::Init() failed to register inband "
kwiberg55b97fe2016-01-28 05:22:45 -08001065 "'telephone-event' (%d/%d) correctly",
1066 codec.pltype, codec.plfreq);
1067 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001068 }
1069
kwiberg1c07c702017-03-27 07:15:49 -07001070 if (STR_CASE_CMP(codec.plname, "CN") == 0) {
1071 if (!audio_coding_->RegisterReceiveCodec(codec.pltype,
1072 CodecInstToSdp(codec))) {
kwiberg55b97fe2016-01-28 05:22:45 -08001073 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1074 "Channel::Init() failed to register CN (%d/%d) "
1075 "correctly - 1",
1076 codec.pltype, codec.plfreq);
1077 }
1078 }
kwiberg55b97fe2016-01-28 05:22:45 -08001079 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001080}
1081
tommi0a2391f2017-03-21 02:31:51 -07001082void Channel::Terminate() {
1083 RTC_DCHECK(construction_thread_.CalledOnValidThread());
1084 // Must be called on the same thread as Init().
1085 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId),
1086 "Channel::Terminate");
1087
1088 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
1089
1090 StopSend();
1091 StopPlayout();
1092
1093 {
1094 rtc::CritScope cs(&_fileCritSect);
1095 if (input_file_player_) {
1096 input_file_player_->RegisterModuleFileCallback(NULL);
1097 input_file_player_->StopPlayingFile();
1098 }
1099 if (output_file_player_) {
1100 output_file_player_->RegisterModuleFileCallback(NULL);
1101 output_file_player_->StopPlayingFile();
1102 }
1103 if (output_file_recorder_) {
1104 output_file_recorder_->RegisterModuleFileCallback(NULL);
1105 output_file_recorder_->StopRecording();
1106 }
1107 }
1108
1109 // The order to safely shutdown modules in a channel is:
1110 // 1. De-register callbacks in modules
1111 // 2. De-register modules in process thread
1112 // 3. Destroy modules
1113 if (audio_coding_->RegisterTransportCallback(NULL) == -1) {
1114 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1115 "Terminate() failed to de-register transport callback"
1116 " (Audio coding module)");
1117 }
1118
1119 if (audio_coding_->RegisterVADCallback(NULL) == -1) {
1120 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
1121 "Terminate() failed to de-register VAD callback"
1122 " (Audio coding module)");
1123 }
1124
1125 // De-register modules in process thread
1126 if (_moduleProcessThreadPtr)
1127 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
1128
1129 // End of modules shutdown
1130}
1131
kwiberg55b97fe2016-01-28 05:22:45 -08001132int32_t Channel::SetEngineInformation(Statistics& engineStatistics,
1133 OutputMixer& outputMixer,
kwiberg55b97fe2016-01-28 05:22:45 -08001134 ProcessThread& moduleProcessThread,
1135 AudioDeviceModule& audioDeviceModule,
1136 VoiceEngineObserver* voiceEngineObserver,
henrikaec6fbd22017-03-31 05:43:36 -07001137 rtc::CriticalSection* callbackCritSect,
1138 rtc::TaskQueue* encoder_queue) {
1139 RTC_DCHECK(encoder_queue);
1140 RTC_DCHECK(!encoder_queue_);
kwiberg55b97fe2016-01-28 05:22:45 -08001141 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1142 "Channel::SetEngineInformation()");
1143 _engineStatisticsPtr = &engineStatistics;
1144 _outputMixerPtr = &outputMixer;
kwiberg55b97fe2016-01-28 05:22:45 -08001145 _moduleProcessThreadPtr = &moduleProcessThread;
1146 _audioDeviceModulePtr = &audioDeviceModule;
1147 _voiceEngineObserverPtr = voiceEngineObserver;
1148 _callbackCritSectPtr = callbackCritSect;
henrikaec6fbd22017-03-31 05:43:36 -07001149 encoder_queue_ = encoder_queue;
kwiberg55b97fe2016-01-28 05:22:45 -08001150 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001151}
1152
kwibergb7f89d62016-02-17 10:04:18 -08001153void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
tommi31fc21f2016-01-21 10:37:37 -08001154 rtc::CritScope cs(&_callbackCritSect);
deadbeef2d110be2016-01-13 12:00:26 -08001155 audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001156}
1157
ossu29b1a8d2016-06-13 07:34:51 -07001158const rtc::scoped_refptr<AudioDecoderFactory>&
1159Channel::GetAudioDecoderFactory() const {
1160 return decoder_factory_;
1161}
1162
kwiberg55b97fe2016-01-28 05:22:45 -08001163int32_t Channel::StartPlayout() {
1164 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1165 "Channel::StartPlayout()");
1166 if (channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001167 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001168 }
1169
solenberge374e012017-02-14 04:55:00 -08001170 // Add participant as candidates for mixing.
1171 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) {
1172 _engineStatisticsPtr->SetLastError(
1173 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1174 "StartPlayout() failed to add participant to mixer");
1175 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001176 }
1177
1178 channel_state_.SetPlaying(true);
1179 if (RegisterFilePlayingToMixer() != 0)
1180 return -1;
1181
1182 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001183}
1184
kwiberg55b97fe2016-01-28 05:22:45 -08001185int32_t Channel::StopPlayout() {
1186 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1187 "Channel::StopPlayout()");
1188 if (!channel_state_.Get().playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001189 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001190 }
1191
solenberge374e012017-02-14 04:55:00 -08001192 // Remove participant as candidates for mixing
1193 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) {
1194 _engineStatisticsPtr->SetLastError(
1195 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1196 "StopPlayout() failed to remove participant from mixer");
1197 return -1;
kwiberg55b97fe2016-01-28 05:22:45 -08001198 }
1199
1200 channel_state_.SetPlaying(false);
1201 _outputAudioLevel.Clear();
1202
1203 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001204}
1205
kwiberg55b97fe2016-01-28 05:22:45 -08001206int32_t Channel::StartSend() {
1207 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1208 "Channel::StartSend()");
kwiberg55b97fe2016-01-28 05:22:45 -08001209 if (channel_state_.Get().sending) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001210 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001211 }
1212 channel_state_.SetSending(true);
henrika4515fa02017-05-03 08:30:15 -07001213 {
1214 // It is now OK to start posting tasks to the encoder task queue.
1215 rtc::CritScope cs(&encoder_queue_lock_);
1216 encoder_queue_is_active_ = true;
1217 }
solenberg08b19df2017-02-15 00:42:31 -08001218 // Resume the previous sequence number which was reset by StopSend(). This
1219 // needs to be done before |sending| is set to true on the RTP/RTCP module.
1220 if (send_sequence_number_) {
1221 _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
1222 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001223 _rtpRtcpModule->SetSendingMediaStatus(true);
kwiberg55b97fe2016-01-28 05:22:45 -08001224 if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
1225 _engineStatisticsPtr->SetLastError(
1226 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1227 "StartSend() RTP/RTCP failed to start sending");
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001228 _rtpRtcpModule->SetSendingMediaStatus(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001229 rtc::CritScope cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001230 channel_state_.SetSending(false);
kwiberg55b97fe2016-01-28 05:22:45 -08001231 return -1;
1232 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001233
kwiberg55b97fe2016-01-28 05:22:45 -08001234 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001235}
1236
henrikaec6fbd22017-03-31 05:43:36 -07001237void Channel::StopSend() {
kwiberg55b97fe2016-01-28 05:22:45 -08001238 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1239 "Channel::StopSend()");
1240 if (!channel_state_.Get().sending) {
henrikaec6fbd22017-03-31 05:43:36 -07001241 return;
kwiberg55b97fe2016-01-28 05:22:45 -08001242 }
1243 channel_state_.SetSending(false);
1244
henrikaec6fbd22017-03-31 05:43:36 -07001245 // Post a task to the encoder thread which sets an event when the task is
1246 // executed. We know that no more encoding tasks will be added to the task
1247 // queue for this channel since sending is now deactivated. It means that,
1248 // if we wait for the event to bet set, we know that no more pending tasks
1249 // exists and it is therfore guaranteed that the task queue will never try
1250 // to acccess and invalid channel object.
1251 RTC_DCHECK(encoder_queue_);
henrika4515fa02017-05-03 08:30:15 -07001252
henrikaec6fbd22017-03-31 05:43:36 -07001253 rtc::Event flush(false, false);
henrika4515fa02017-05-03 08:30:15 -07001254 {
1255 // Clear |encoder_queue_is_active_| under lock to prevent any other tasks
1256 // than this final "flush task" to be posted on the queue.
1257 rtc::CritScope cs(&encoder_queue_lock_);
1258 encoder_queue_is_active_ = false;
1259 encoder_queue_->PostTask([&flush]() { flush.Set(); });
1260 }
henrikaec6fbd22017-03-31 05:43:36 -07001261 flush.Wait(rtc::Event::kForever);
1262
kwiberg55b97fe2016-01-28 05:22:45 -08001263 // Store the sequence number to be able to pick up the same sequence for
1264 // the next StartSend(). This is needed for restarting device, otherwise
1265 // it might cause libSRTP to complain about packets being replayed.
1266 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1267 // CL is landed. See issue
1268 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1269 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1270
1271 // Reset sending SSRC and sequence number and triggers direct transmission
1272 // of RTCP BYE
1273 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
1274 _engineStatisticsPtr->SetLastError(
1275 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1276 "StartSend() RTP/RTCP failed to stop sending");
1277 }
Peter Boström3dd5d1d2016-02-25 16:56:48 +01001278 _rtpRtcpModule->SetSendingMediaStatus(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001279}
1280
ossu1ffbd6c2017-04-06 12:05:04 -07001281bool Channel::SetEncoder(int payload_type,
1282 std::unique_ptr<AudioEncoder> encoder) {
1283 RTC_DCHECK_GE(payload_type, 0);
1284 RTC_DCHECK_LE(payload_type, 127);
ossu76d29f92017-06-09 07:30:13 -07001285 // TODO(ossu): Make CodecInsts up, for now: one for the RTP/RTCP module and
1286 // one for for us to keep track of sample rate and number of channels, etc.
1287
1288 // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate)
1289 // as well as some other things, so we collect this info and send it along.
1290 CodecInst rtp_codec;
1291 rtp_codec.pltype = payload_type;
1292 strncpy(rtp_codec.plname, "audio", sizeof(rtp_codec.plname));
1293 rtp_codec.plname[sizeof(rtp_codec.plname) - 1] = 0;
ossu1ffbd6c2017-04-06 12:05:04 -07001294 // Seems unclear if it should be clock rate or sample rate. CodecInst
1295 // supposedly carries the sample rate, but only clock rate seems sensible to
1296 // send to the RTP/RTCP module.
ossu76d29f92017-06-09 07:30:13 -07001297 rtp_codec.plfreq = encoder->RtpTimestampRateHz();
1298 rtp_codec.pacsize = rtc::CheckedDivExact(
1299 static_cast<int>(encoder->Max10MsFramesInAPacket() * rtp_codec.plfreq),
1300 100);
1301 rtp_codec.channels = encoder->NumChannels();
1302 rtp_codec.rate = 0;
ossu1ffbd6c2017-04-06 12:05:04 -07001303
ossu76d29f92017-06-09 07:30:13 -07001304 // For audio encoding we need, instead, the actual sample rate of the codec.
1305 // The rest of the information should be the same.
1306 CodecInst send_codec = rtp_codec;
1307 send_codec.plfreq = encoder->SampleRateHz();
1308 cached_send_codec_.emplace(send_codec);
1309
1310 if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) {
ossu1ffbd6c2017-04-06 12:05:04 -07001311 _rtpRtcpModule->DeRegisterSendPayload(payload_type);
ossu76d29f92017-06-09 07:30:13 -07001312 if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) {
ossu1ffbd6c2017-04-06 12:05:04 -07001313 WEBRTC_TRACE(
1314 kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1315 "SetEncoder() failed to register codec to RTP/RTCP module");
1316 return false;
1317 }
1318 }
1319
1320 audio_coding_->SetEncoder(std::move(encoder));
ossu20a4b3f2017-04-27 02:08:52 -07001321 codec_manager_.UnsetCodecInst();
ossu1ffbd6c2017-04-06 12:05:04 -07001322 return true;
1323}
1324
ossu20a4b3f2017-04-27 02:08:52 -07001325void Channel::ModifyEncoder(
1326 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
1327 audio_coding_->ModifyEncoder(modifier);
1328}
1329
kwiberg55b97fe2016-01-28 05:22:45 -08001330int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) {
1331 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1332 "Channel::RegisterVoiceEngineObserver()");
1333 rtc::CritScope cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001334
kwiberg55b97fe2016-01-28 05:22:45 -08001335 if (_voiceEngineObserverPtr) {
1336 _engineStatisticsPtr->SetLastError(
1337 VE_INVALID_OPERATION, kTraceError,
1338 "RegisterVoiceEngineObserver() observer already enabled");
1339 return -1;
1340 }
1341 _voiceEngineObserverPtr = &observer;
1342 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001343}
1344
kwiberg55b97fe2016-01-28 05:22:45 -08001345int32_t Channel::DeRegisterVoiceEngineObserver() {
1346 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1347 "Channel::DeRegisterVoiceEngineObserver()");
1348 rtc::CritScope cs(&_callbackCritSect);
1349
1350 if (!_voiceEngineObserverPtr) {
1351 _engineStatisticsPtr->SetLastError(
1352 VE_INVALID_OPERATION, kTraceWarning,
1353 "DeRegisterVoiceEngineObserver() observer already disabled");
1354 return 0;
1355 }
1356 _voiceEngineObserverPtr = NULL;
1357 return 0;
1358}
1359
1360int32_t Channel::GetSendCodec(CodecInst& codec) {
ossu76d29f92017-06-09 07:30:13 -07001361 if (cached_send_codec_) {
1362 codec = *cached_send_codec_;
1363 return 0;
1364 } else {
ossu20a4b3f2017-04-27 02:08:52 -07001365 const CodecInst* send_codec = codec_manager_.GetCodecInst();
1366 if (send_codec) {
1367 codec = *send_codec;
1368 return 0;
1369 }
1370 }
kwiberg1fd4a4a2015-11-03 11:20:50 -08001371 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001372}
1373
kwiberg55b97fe2016-01-28 05:22:45 -08001374int32_t Channel::GetRecCodec(CodecInst& codec) {
1375 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001376}
1377
kwiberg55b97fe2016-01-28 05:22:45 -08001378int32_t Channel::SetSendCodec(const CodecInst& codec) {
1379 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1380 "Channel::SetSendCodec()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001381
kwibergc8d071e2016-04-06 12:22:38 -07001382 if (!codec_manager_.RegisterEncoder(codec) ||
1383 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001384 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1385 "SetSendCodec() failed to register codec to ACM");
1386 return -1;
1387 }
1388
1389 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1390 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1391 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1392 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId),
1393 "SetSendCodec() failed to register codec to"
1394 " RTP/RTCP module");
1395 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001396 }
kwiberg55b97fe2016-01-28 05:22:45 -08001397 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001398
ossu76d29f92017-06-09 07:30:13 -07001399 cached_send_codec_.reset();
1400
kwiberg55b97fe2016-01-28 05:22:45 -08001401 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001402}
1403
minyue78b4d562016-11-30 04:47:39 -08001404void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
Ivo Creusenadf89b72015-04-29 16:03:33 +02001405 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1406 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
minyue7e304322016-10-12 05:00:55 -07001407 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
michaelt2fedf9c2016-11-28 02:34:18 -08001408 if (*encoder) {
1409 (*encoder)->OnReceivedUplinkBandwidth(
michaelt566d8202017-01-12 10:17:38 -08001410 bitrate_bps, rtc::Optional<int64_t>(probing_interval_ms));
michaelt2fedf9c2016-11-28 02:34:18 -08001411 }
1412 });
michaelt566d8202017-01-12 10:17:38 -08001413 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
Ivo Creusenadf89b72015-04-29 16:03:33 +02001414}
1415
elad.alond12a8e12017-03-23 11:04:48 -07001416void Channel::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
1417 if (!use_twcc_plr_for_ana_)
1418 return;
minyue7e304322016-10-12 05:00:55 -07001419 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
elad.alond12a8e12017-03-23 11:04:48 -07001420 if (*encoder) {
1421 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1422 }
1423 });
1424}
1425
elad.alondadb4dc2017-03-23 15:29:50 -07001426void Channel::OnRecoverableUplinkPacketLossRate(
1427 float recoverable_packet_loss_rate) {
1428 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1429 if (*encoder) {
1430 (*encoder)->OnReceivedUplinkRecoverablePacketLossFraction(
1431 recoverable_packet_loss_rate);
1432 }
1433 });
1434}
1435
elad.alond12a8e12017-03-23 11:04:48 -07001436void Channel::OnUplinkPacketLossRate(float packet_loss_rate) {
1437 if (use_twcc_plr_for_ana_)
1438 return;
1439 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1440 if (*encoder) {
1441 (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
1442 }
minyue7e304322016-10-12 05:00:55 -07001443 });
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001444}
1445
kwiberg55b97fe2016-01-28 05:22:45 -08001446int32_t Channel::SetVADStatus(bool enableVAD,
1447 ACMVADMode mode,
1448 bool disableDTX) {
1449 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1450 "Channel::SetVADStatus(mode=%d)", mode);
kwibergc8d071e2016-04-06 12:22:38 -07001451 RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
1452 if (!codec_manager_.SetVAD(enableVAD, mode) ||
1453 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001454 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1455 kTraceError,
1456 "SetVADStatus() failed to set VAD");
1457 return -1;
1458 }
1459 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001460}
1461
kwiberg55b97fe2016-01-28 05:22:45 -08001462int32_t Channel::GetVADStatus(bool& enabledVAD,
1463 ACMVADMode& mode,
1464 bool& disabledDTX) {
kwibergc8d071e2016-04-06 12:22:38 -07001465 const auto* params = codec_manager_.GetStackParams();
1466 enabledVAD = params->use_cng;
1467 mode = params->vad_mode;
1468 disabledDTX = !params->use_cng;
kwiberg55b97fe2016-01-28 05:22:45 -08001469 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001470}
1471
kwiberg1c07c702017-03-27 07:15:49 -07001472void Channel::SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) {
1473 rtp_payload_registry_->SetAudioReceivePayloads(codecs);
1474 audio_coding_->SetReceiveCodecs(codecs);
1475}
1476
kwiberg55b97fe2016-01-28 05:22:45 -08001477int32_t Channel::SetRecPayloadType(const CodecInst& codec) {
kwibergd32bf752017-01-19 07:03:59 -08001478 return SetRecPayloadType(codec.pltype, CodecInstToSdp(codec));
1479}
1480
1481int32_t Channel::SetRecPayloadType(int payload_type,
1482 const SdpAudioFormat& format) {
kwiberg55b97fe2016-01-28 05:22:45 -08001483 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1484 "Channel::SetRecPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001485
kwiberg55b97fe2016-01-28 05:22:45 -08001486 if (channel_state_.Get().playing) {
1487 _engineStatisticsPtr->SetLastError(
1488 VE_ALREADY_PLAYING, kTraceError,
1489 "SetRecPayloadType() unable to set PT while playing");
1490 return -1;
1491 }
kwiberg55b97fe2016-01-28 05:22:45 -08001492
kwiberg09f090c2017-03-01 01:57:11 -08001493 const CodecInst codec = SdpToCodecInst(payload_type, format);
kwibergd32bf752017-01-19 07:03:59 -08001494
1495 if (payload_type == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08001496 // De-register the selected codec (RTP/RTCP module and ACM)
1497
1498 int8_t pltype(-1);
1499 CodecInst rxCodec = codec;
1500
1501 // Get payload type for the given codec
magjed56124bd2016-11-24 09:34:46 -08001502 rtp_payload_registry_->ReceivePayloadType(rxCodec, &pltype);
kwiberg55b97fe2016-01-28 05:22:45 -08001503 rxCodec.pltype = pltype;
1504
1505 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) {
1506 _engineStatisticsPtr->SetLastError(
1507 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1508 "SetRecPayloadType() RTP/RTCP-module deregistration "
1509 "failed");
1510 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001511 }
kwiberg55b97fe2016-01-28 05:22:45 -08001512 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) {
1513 _engineStatisticsPtr->SetLastError(
1514 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1515 "SetRecPayloadType() ACM deregistration failed - 1");
1516 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001517 }
kwiberg55b97fe2016-01-28 05:22:45 -08001518 return 0;
1519 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001520
magjed56124bd2016-11-24 09:34:46 -08001521 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001522 // First attempt to register failed => de-register and try again
kwibergc8d071e2016-04-06 12:22:38 -07001523 // TODO(kwiberg): Retrying is probably not necessary, since
1524 // AcmReceiver::AddCodec also retries.
kwiberg55b97fe2016-01-28 05:22:45 -08001525 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
magjed56124bd2016-11-24 09:34:46 -08001526 if (rtp_receiver_->RegisterReceivePayload(codec) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001527 _engineStatisticsPtr->SetLastError(
1528 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1529 "SetRecPayloadType() RTP/RTCP-module registration failed");
1530 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001531 }
kwiberg55b97fe2016-01-28 05:22:45 -08001532 }
kwibergd32bf752017-01-19 07:03:59 -08001533 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
1534 audio_coding_->UnregisterReceiveCodec(payload_type);
1535 if (!audio_coding_->RegisterReceiveCodec(payload_type, format)) {
kwiberg55b97fe2016-01-28 05:22:45 -08001536 _engineStatisticsPtr->SetLastError(
1537 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1538 "SetRecPayloadType() ACM registration failed - 1");
1539 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001540 }
kwiberg55b97fe2016-01-28 05:22:45 -08001541 }
1542 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001543}
1544
kwiberg55b97fe2016-01-28 05:22:45 -08001545int32_t Channel::GetRecPayloadType(CodecInst& codec) {
1546 int8_t payloadType(-1);
magjed56124bd2016-11-24 09:34:46 -08001547 if (rtp_payload_registry_->ReceivePayloadType(codec, &payloadType) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001548 _engineStatisticsPtr->SetLastError(
1549 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1550 "GetRecPayloadType() failed to retrieve RX payload type");
1551 return -1;
1552 }
1553 codec.pltype = payloadType;
1554 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001555}
1556
kwiberg55b97fe2016-01-28 05:22:45 -08001557int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) {
1558 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1559 "Channel::SetSendCNPayloadType()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001560
kwiberg55b97fe2016-01-28 05:22:45 -08001561 CodecInst codec;
1562 int32_t samplingFreqHz(-1);
1563 const size_t kMono = 1;
1564 if (frequency == kFreq32000Hz)
1565 samplingFreqHz = 32000;
1566 else if (frequency == kFreq16000Hz)
1567 samplingFreqHz = 16000;
niklase@google.com470e71d2011-07-07 08:21:25 +00001568
kwiberg55b97fe2016-01-28 05:22:45 -08001569 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) {
1570 _engineStatisticsPtr->SetLastError(
1571 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1572 "SetSendCNPayloadType() failed to retrieve default CN codec "
1573 "settings");
1574 return -1;
1575 }
1576
1577 // Modify the payload type (must be set to dynamic range)
1578 codec.pltype = type;
1579
kwibergc8d071e2016-04-06 12:22:38 -07001580 if (!codec_manager_.RegisterEncoder(codec) ||
1581 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
kwiberg55b97fe2016-01-28 05:22:45 -08001582 _engineStatisticsPtr->SetLastError(
1583 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1584 "SetSendCNPayloadType() failed to register CN to ACM");
1585 return -1;
1586 }
1587
1588 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1589 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1590 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
1591 _engineStatisticsPtr->SetLastError(
1592 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1593 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1594 "module");
1595 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001596 }
kwiberg55b97fe2016-01-28 05:22:45 -08001597 }
1598 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001599}
1600
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001601int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001602 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001603 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001604
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001605 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001606 _engineStatisticsPtr->SetLastError(
1607 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001608 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001609 return -1;
1610 }
1611 return 0;
1612}
1613
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001614int Channel::SetOpusDtx(bool enable_dtx) {
1615 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1616 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001617 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001618 : audio_coding_->DisableOpusDtx();
1619 if (ret != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001620 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1621 kTraceError, "SetOpusDtx() failed");
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001622 return -1;
1623 }
1624 return 0;
1625}
1626
ivoc85228d62016-07-27 04:53:47 -07001627int Channel::GetOpusDtx(bool* enabled) {
1628 int success = -1;
1629 audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) {
1630 if (encoder) {
1631 *enabled = encoder->GetDtx();
1632 success = 0;
1633 }
1634 });
1635 return success;
1636}
1637
minyue7e304322016-10-12 05:00:55 -07001638bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
1639 bool success = false;
1640 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1641 if (*encoder) {
michaelt92aef172017-04-18 00:11:48 -07001642 success = (*encoder)->EnableAudioNetworkAdaptor(config_string,
1643 event_log_proxy_.get());
minyue7e304322016-10-12 05:00:55 -07001644 }
1645 });
1646 return success;
1647}
1648
1649void Channel::DisableAudioNetworkAdaptor() {
1650 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1651 if (*encoder)
1652 (*encoder)->DisableAudioNetworkAdaptor();
1653 });
1654}
1655
1656void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
1657 int max_frame_length_ms) {
1658 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1659 if (*encoder) {
1660 (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
1661 max_frame_length_ms);
1662 }
1663 });
1664}
1665
mflodman3d7db262016-04-29 00:57:13 -07001666int32_t Channel::RegisterExternalTransport(Transport* transport) {
kwiberg55b97fe2016-01-28 05:22:45 -08001667 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00001668 "Channel::RegisterExternalTransport()");
1669
kwiberg55b97fe2016-01-28 05:22:45 -08001670 rtc::CritScope cs(&_callbackCritSect);
kwiberg55b97fe2016-01-28 05:22:45 -08001671 if (_externalTransport) {
1672 _engineStatisticsPtr->SetLastError(
1673 VE_INVALID_OPERATION, kTraceError,
1674 "RegisterExternalTransport() external transport already enabled");
1675 return -1;
1676 }
1677 _externalTransport = true;
mflodman3d7db262016-04-29 00:57:13 -07001678 _transportPtr = transport;
kwiberg55b97fe2016-01-28 05:22:45 -08001679 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001680}
1681
kwiberg55b97fe2016-01-28 05:22:45 -08001682int32_t Channel::DeRegisterExternalTransport() {
1683 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1684 "Channel::DeRegisterExternalTransport()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001685
kwiberg55b97fe2016-01-28 05:22:45 -08001686 rtc::CritScope cs(&_callbackCritSect);
mflodman3d7db262016-04-29 00:57:13 -07001687 if (_transportPtr) {
1688 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1689 "DeRegisterExternalTransport() all transport is disabled");
1690 } else {
kwiberg55b97fe2016-01-28 05:22:45 -08001691 _engineStatisticsPtr->SetLastError(
1692 VE_INVALID_OPERATION, kTraceWarning,
1693 "DeRegisterExternalTransport() external transport already "
1694 "disabled");
kwiberg55b97fe2016-01-28 05:22:45 -08001695 }
1696 _externalTransport = false;
1697 _transportPtr = NULL;
kwiberg55b97fe2016-01-28 05:22:45 -08001698 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001699}
1700
nisse657bab22017-02-21 06:28:10 -08001701// TODO(nisse): Delete this method together with ReceivedRTPPacket.
1702// It's a temporary hack to support both ReceivedRTPPacket and
1703// OnRtpPacket interfaces without too much code duplication.
1704bool Channel::OnRtpPacketWithHeader(const uint8_t* received_packet,
1705 size_t length,
1706 RTPHeader *header) {
1707 // Store playout timestamp for the received RTP packet
1708 UpdatePlayoutTimestamp(false);
1709
1710 header->payload_type_frequency =
1711 rtp_payload_registry_->GetPayloadTypeFrequency(header->payloadType);
1712 if (header->payload_type_frequency < 0)
1713 return false;
1714 bool in_order = IsPacketInOrder(*header);
1715 rtp_receive_statistics_->IncomingPacket(
1716 *header, length, IsPacketRetransmitted(*header, in_order));
1717 rtp_payload_registry_->SetIncomingPayloadType(*header);
1718
1719 return ReceivePacket(received_packet, length, *header, in_order);
1720}
1721
mflodman3d7db262016-04-29 00:57:13 -07001722int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -08001723 size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001724 const PacketTime& packet_time) {
kwiberg55b97fe2016-01-28 05:22:45 -08001725 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001726 "Channel::ReceivedRTPPacket()");
1727
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001728 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001729 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1730 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1731 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001732 return -1;
1733 }
nisse657bab22017-02-21 06:28:10 -08001734 return OnRtpPacketWithHeader(received_packet, length, &header) ? 0 : -1;
1735}
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001736
nisse657bab22017-02-21 06:28:10 -08001737void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
1738 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
1739 "Channel::ReceivedRTPPacket()");
1740
1741 RTPHeader header;
1742 packet.GetHeader(&header);
1743 OnRtpPacketWithHeader(packet.data(), packet.size(), &header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001744}
1745
1746bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001747 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001748 const RTPHeader& header,
1749 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001750 if (rtp_payload_registry_->IsRtx(header)) {
1751 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001752 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001753 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001754 assert(packet_length >= header.headerLength);
1755 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001756 PayloadUnion payload_specific;
1757 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001758 &payload_specific)) {
1759 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001760 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001761 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1762 payload_specific, in_order);
1763}
1764
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001765bool Channel::HandleRtxPacket(const uint8_t* packet,
1766 size_t packet_length,
1767 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001768 if (!rtp_payload_registry_->IsRtx(header))
1769 return false;
1770
1771 // Remove the RTX header and parse the original RTP header.
1772 if (packet_length < header.headerLength)
1773 return false;
1774 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1775 return false;
1776 if (restored_packet_in_use_) {
1777 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1778 "Multiple RTX headers detected, dropping packet");
1779 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001780 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001781 if (!rtp_payload_registry_->RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -07001782 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
1783 header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001784 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1785 "Incoming RTX packet: invalid RTP header");
1786 return false;
1787 }
1788 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -07001789 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001790 restored_packet_in_use_ = false;
1791 return ret;
1792}
1793
1794bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1795 StreamStatistician* statistician =
1796 rtp_receive_statistics_->GetStatistician(header.ssrc);
1797 if (!statistician)
1798 return false;
1799 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001800}
1801
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001802bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1803 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001804 // Retransmissions are handled separately if RTX is enabled.
1805 if (rtp_payload_registry_->RtxEnabled())
1806 return false;
1807 StreamStatistician* statistician =
1808 rtp_receive_statistics_->GetStatistician(header.ssrc);
1809 if (!statistician)
1810 return false;
1811 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001812 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001813 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
kwiberg55b97fe2016-01-28 05:22:45 -08001814 return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001815}
1816
mflodman3d7db262016-04-29 00:57:13 -07001817int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
kwiberg55b97fe2016-01-28 05:22:45 -08001818 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001819 "Channel::ReceivedRTCPPacket()");
1820 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001821 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001822
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001823 // Deliver RTCP packet to RTP/RTCP module for parsing
mflodman3d7db262016-04-29 00:57:13 -07001824 if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001825 _engineStatisticsPtr->SetLastError(
1826 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1827 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1828 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001829
Minyue2013aec2015-05-13 14:14:42 +02001830 int64_t rtt = GetRTT(true);
1831 if (rtt == 0) {
1832 // Waiting for valid RTT.
1833 return 0;
1834 }
Erik Språng737336d2016-07-29 12:59:36 +02001835
1836 int64_t nack_window_ms = rtt;
1837 if (nack_window_ms < kMinRetransmissionWindowMs) {
1838 nack_window_ms = kMinRetransmissionWindowMs;
1839 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
1840 nack_window_ms = kMaxRetransmissionWindowMs;
1841 }
1842 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
1843
minyue7e304322016-10-12 05:00:55 -07001844 // Invoke audio encoders OnReceivedRtt().
1845 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
1846 if (*encoder)
1847 (*encoder)->OnReceivedRtt(rtt);
1848 });
1849
Minyue2013aec2015-05-13 14:14:42 +02001850 uint32_t ntp_secs = 0;
1851 uint32_t ntp_frac = 0;
1852 uint32_t rtp_timestamp = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08001853 if (0 !=
1854 _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1855 &rtp_timestamp)) {
Minyue2013aec2015-05-13 14:14:42 +02001856 // Waiting for RTCP.
1857 return 0;
1858 }
1859
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001860 {
tommi31fc21f2016-01-21 10:37:37 -08001861 rtc::CritScope lock(&ts_stats_lock_);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001862 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001863 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001864 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001865}
1866
niklase@google.com470e71d2011-07-07 08:21:25 +00001867int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001868 bool loop,
1869 FileFormats format,
1870 int startPosition,
1871 float volumeScaling,
1872 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001873 const CodecInst* codecInst) {
1874 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1875 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1876 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1877 "stopPosition=%d)",
1878 fileName, loop, format, volumeScaling, startPosition,
1879 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001880
kwiberg55b97fe2016-01-28 05:22:45 -08001881 if (channel_state_.Get().output_file_playing) {
1882 _engineStatisticsPtr->SetLastError(
1883 VE_ALREADY_PLAYING, kTraceError,
1884 "StartPlayingFileLocally() is already playing");
1885 return -1;
1886 }
1887
1888 {
1889 rtc::CritScope cs(&_fileCritSect);
1890
kwiberg5a25d952016-08-17 07:31:12 -07001891 if (output_file_player_) {
1892 output_file_player_->RegisterModuleFileCallback(NULL);
1893 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001894 }
1895
kwiberg5b356f42016-09-08 04:32:33 -07001896 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001897 _outputFilePlayerId, (const FileFormats)format);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001898
kwiberg5a25d952016-08-17 07:31:12 -07001899 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001900 _engineStatisticsPtr->SetLastError(
1901 VE_INVALID_ARGUMENT, kTraceError,
1902 "StartPlayingFileLocally() filePlayer format is not correct");
1903 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001904 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001905
kwiberg55b97fe2016-01-28 05:22:45 -08001906 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001907
kwiberg5a25d952016-08-17 07:31:12 -07001908 if (output_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08001909 fileName, loop, startPosition, volumeScaling, notificationTime,
1910 stopPosition, (const CodecInst*)codecInst) != 0) {
1911 _engineStatisticsPtr->SetLastError(
1912 VE_BAD_FILE, kTraceError,
1913 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001914 output_file_player_->StopPlayingFile();
1915 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001916 return -1;
1917 }
kwiberg5a25d952016-08-17 07:31:12 -07001918 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001919 channel_state_.SetOutputFilePlaying(true);
1920 }
1921
1922 if (RegisterFilePlayingToMixer() != 0)
1923 return -1;
1924
1925 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001926}
1927
1928int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001929 FileFormats format,
1930 int startPosition,
1931 float volumeScaling,
1932 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08001933 const CodecInst* codecInst) {
1934 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1935 "Channel::StartPlayingFileLocally(format=%d,"
1936 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1937 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00001938
kwiberg55b97fe2016-01-28 05:22:45 -08001939 if (stream == NULL) {
1940 _engineStatisticsPtr->SetLastError(
1941 VE_BAD_FILE, kTraceError,
1942 "StartPlayingFileLocally() NULL as input stream");
1943 return -1;
1944 }
1945
1946 if (channel_state_.Get().output_file_playing) {
1947 _engineStatisticsPtr->SetLastError(
1948 VE_ALREADY_PLAYING, kTraceError,
1949 "StartPlayingFileLocally() is already playing");
1950 return -1;
1951 }
1952
1953 {
1954 rtc::CritScope cs(&_fileCritSect);
1955
1956 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07001957 if (output_file_player_) {
1958 output_file_player_->RegisterModuleFileCallback(NULL);
1959 output_file_player_.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +00001960 }
1961
kwiberg55b97fe2016-01-28 05:22:45 -08001962 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07001963 output_file_player_ = FilePlayer::CreateFilePlayer(
kwiberg55b97fe2016-01-28 05:22:45 -08001964 _outputFilePlayerId, (const FileFormats)format);
niklase@google.com470e71d2011-07-07 08:21:25 +00001965
kwiberg5a25d952016-08-17 07:31:12 -07001966 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08001967 _engineStatisticsPtr->SetLastError(
1968 VE_INVALID_ARGUMENT, kTraceError,
1969 "StartPlayingFileLocally() filePlayer format isnot correct");
1970 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001971 }
1972
kwiberg55b97fe2016-01-28 05:22:45 -08001973 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001974
kwiberg4ec01d92016-08-22 08:43:54 -07001975 if (output_file_player_->StartPlayingFile(stream, startPosition,
kwiberg5a25d952016-08-17 07:31:12 -07001976 volumeScaling, notificationTime,
1977 stopPosition, codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08001978 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1979 "StartPlayingFile() failed to "
1980 "start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07001981 output_file_player_->StopPlayingFile();
1982 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08001983 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001984 }
kwiberg5a25d952016-08-17 07:31:12 -07001985 output_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08001986 channel_state_.SetOutputFilePlaying(true);
1987 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001988
kwiberg55b97fe2016-01-28 05:22:45 -08001989 if (RegisterFilePlayingToMixer() != 0)
1990 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001991
kwiberg55b97fe2016-01-28 05:22:45 -08001992 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001993}
1994
kwiberg55b97fe2016-01-28 05:22:45 -08001995int Channel::StopPlayingFileLocally() {
1996 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1997 "Channel::StopPlayingFileLocally()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001998
kwiberg55b97fe2016-01-28 05:22:45 -08001999 if (!channel_state_.Get().output_file_playing) {
niklase@google.com470e71d2011-07-07 08:21:25 +00002000 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002001 }
2002
2003 {
2004 rtc::CritScope cs(&_fileCritSect);
2005
kwiberg5a25d952016-08-17 07:31:12 -07002006 if (output_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002007 _engineStatisticsPtr->SetLastError(
2008 VE_STOP_RECORDING_FAILED, kTraceError,
2009 "StopPlayingFile() could not stop playing");
2010 return -1;
2011 }
kwiberg5a25d952016-08-17 07:31:12 -07002012 output_file_player_->RegisterModuleFileCallback(NULL);
2013 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002014 channel_state_.SetOutputFilePlaying(false);
2015 }
2016 // _fileCritSect cannot be taken while calling
2017 // SetAnonymousMixibilityStatus. Refer to comments in
2018 // StartPlayingFileLocally(const char* ...) for more details.
2019 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) {
2020 _engineStatisticsPtr->SetLastError(
2021 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2022 "StopPlayingFile() failed to stop participant from playing as"
2023 "file in the mixer");
2024 return -1;
2025 }
2026
2027 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002028}
2029
kwiberg55b97fe2016-01-28 05:22:45 -08002030int Channel::IsPlayingFileLocally() const {
2031 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002032}
2033
kwiberg55b97fe2016-01-28 05:22:45 -08002034int Channel::RegisterFilePlayingToMixer() {
2035 // Return success for not registering for file playing to mixer if:
2036 // 1. playing file before playout is started on that channel.
2037 // 2. starting playout without file playing on that channel.
2038 if (!channel_state_.Get().playing ||
2039 !channel_state_.Get().output_file_playing) {
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002040 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002041 }
2042
2043 // |_fileCritSect| cannot be taken while calling
2044 // SetAnonymousMixabilityStatus() since as soon as the participant is added
2045 // frames can be pulled by the mixer. Since the frames are generated from
2046 // the file, _fileCritSect will be taken. This would result in a deadlock.
2047 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) {
2048 channel_state_.SetOutputFilePlaying(false);
2049 rtc::CritScope cs(&_fileCritSect);
2050 _engineStatisticsPtr->SetLastError(
2051 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2052 "StartPlayingFile() failed to add participant as file to mixer");
kwiberg5a25d952016-08-17 07:31:12 -07002053 output_file_player_->StopPlayingFile();
2054 output_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002055 return -1;
2056 }
2057
2058 return 0;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002059}
2060
niklase@google.com470e71d2011-07-07 08:21:25 +00002061int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002062 bool loop,
2063 FileFormats format,
2064 int startPosition,
2065 float volumeScaling,
2066 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002067 const CodecInst* codecInst) {
2068 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2069 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2070 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2071 "stopPosition=%d)",
2072 fileName, loop, format, volumeScaling, startPosition,
2073 stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002074
kwiberg55b97fe2016-01-28 05:22:45 -08002075 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002076
kwiberg55b97fe2016-01-28 05:22:45 -08002077 if (channel_state_.Get().input_file_playing) {
2078 _engineStatisticsPtr->SetLastError(
2079 VE_ALREADY_PLAYING, kTraceWarning,
2080 "StartPlayingFileAsMicrophone() filePlayer is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002081 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002082 }
2083
2084 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002085 if (input_file_player_) {
2086 input_file_player_->RegisterModuleFileCallback(NULL);
2087 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002088 }
2089
2090 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002091 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002092 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002093
kwiberg5a25d952016-08-17 07:31:12 -07002094 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002095 _engineStatisticsPtr->SetLastError(
2096 VE_INVALID_ARGUMENT, kTraceError,
2097 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2098 return -1;
2099 }
2100
2101 const uint32_t notificationTime(0);
2102
kwiberg5a25d952016-08-17 07:31:12 -07002103 if (input_file_player_->StartPlayingFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002104 fileName, loop, startPosition, volumeScaling, notificationTime,
2105 stopPosition, (const CodecInst*)codecInst) != 0) {
2106 _engineStatisticsPtr->SetLastError(
2107 VE_BAD_FILE, kTraceError,
2108 "StartPlayingFile() failed to start file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002109 input_file_player_->StopPlayingFile();
2110 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002111 return -1;
2112 }
kwiberg5a25d952016-08-17 07:31:12 -07002113 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002114 channel_state_.SetInputFilePlaying(true);
2115
2116 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002117}
2118
2119int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002120 FileFormats format,
2121 int startPosition,
2122 float volumeScaling,
2123 int stopPosition,
kwiberg55b97fe2016-01-28 05:22:45 -08002124 const CodecInst* codecInst) {
2125 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2126 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2127 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2128 format, volumeScaling, startPosition, stopPosition);
niklase@google.com470e71d2011-07-07 08:21:25 +00002129
kwiberg55b97fe2016-01-28 05:22:45 -08002130 if (stream == NULL) {
2131 _engineStatisticsPtr->SetLastError(
2132 VE_BAD_FILE, kTraceError,
2133 "StartPlayingFileAsMicrophone NULL as input stream");
2134 return -1;
2135 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002136
kwiberg55b97fe2016-01-28 05:22:45 -08002137 rtc::CritScope cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002138
kwiberg55b97fe2016-01-28 05:22:45 -08002139 if (channel_state_.Get().input_file_playing) {
2140 _engineStatisticsPtr->SetLastError(
2141 VE_ALREADY_PLAYING, kTraceWarning,
2142 "StartPlayingFileAsMicrophone() is playing");
niklase@google.com470e71d2011-07-07 08:21:25 +00002143 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002144 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002145
kwiberg55b97fe2016-01-28 05:22:45 -08002146 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002147 if (input_file_player_) {
2148 input_file_player_->RegisterModuleFileCallback(NULL);
2149 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002150 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002151
kwiberg55b97fe2016-01-28 05:22:45 -08002152 // Create the instance
kwiberg5b356f42016-09-08 04:32:33 -07002153 input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId,
kwiberg5a25d952016-08-17 07:31:12 -07002154 (const FileFormats)format);
kwiberg55b97fe2016-01-28 05:22:45 -08002155
kwiberg5a25d952016-08-17 07:31:12 -07002156 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002157 _engineStatisticsPtr->SetLastError(
2158 VE_INVALID_ARGUMENT, kTraceError,
2159 "StartPlayingInputFile() filePlayer format isnot correct");
2160 return -1;
2161 }
2162
2163 const uint32_t notificationTime(0);
2164
kwiberg4ec01d92016-08-22 08:43:54 -07002165 if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling,
2166 notificationTime, stopPosition,
2167 codecInst) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002168 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2169 "StartPlayingFile() failed to start "
2170 "file playout");
kwiberg5a25d952016-08-17 07:31:12 -07002171 input_file_player_->StopPlayingFile();
2172 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002173 return -1;
2174 }
2175
kwiberg5a25d952016-08-17 07:31:12 -07002176 input_file_player_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002177 channel_state_.SetInputFilePlaying(true);
2178
2179 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002180}
2181
kwiberg55b97fe2016-01-28 05:22:45 -08002182int Channel::StopPlayingFileAsMicrophone() {
2183 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2184 "Channel::StopPlayingFileAsMicrophone()");
2185
2186 rtc::CritScope cs(&_fileCritSect);
2187
2188 if (!channel_state_.Get().input_file_playing) {
2189 return 0;
2190 }
2191
kwiberg5a25d952016-08-17 07:31:12 -07002192 if (input_file_player_->StopPlayingFile() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002193 _engineStatisticsPtr->SetLastError(
2194 VE_STOP_RECORDING_FAILED, kTraceError,
2195 "StopPlayingFile() could not stop playing");
2196 return -1;
2197 }
kwiberg5a25d952016-08-17 07:31:12 -07002198 input_file_player_->RegisterModuleFileCallback(NULL);
2199 input_file_player_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002200 channel_state_.SetInputFilePlaying(false);
2201
2202 return 0;
2203}
2204
2205int Channel::IsPlayingFileAsMicrophone() const {
2206 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002207}
2208
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002209int Channel::StartRecordingPlayout(const char* fileName,
kwiberg55b97fe2016-01-28 05:22:45 -08002210 const CodecInst* codecInst) {
2211 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2212 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
niklase@google.com470e71d2011-07-07 08:21:25 +00002213
kwiberg55b97fe2016-01-28 05:22:45 -08002214 if (_outputFileRecording) {
2215 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2216 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002217 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002218 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002219
kwiberg55b97fe2016-01-28 05:22:45 -08002220 FileFormats format;
2221 const uint32_t notificationTime(0); // Not supported in VoE
2222 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
niklase@google.com470e71d2011-07-07 08:21:25 +00002223
kwiberg55b97fe2016-01-28 05:22:45 -08002224 if ((codecInst != NULL) &&
2225 ((codecInst->channels < 1) || (codecInst->channels > 2))) {
2226 _engineStatisticsPtr->SetLastError(
2227 VE_BAD_ARGUMENT, kTraceError,
2228 "StartRecordingPlayout() invalid compression");
2229 return (-1);
2230 }
2231 if (codecInst == NULL) {
2232 format = kFileFormatPcm16kHzFile;
2233 codecInst = &dummyCodec;
2234 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2235 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2236 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2237 format = kFileFormatWavFile;
2238 } else {
2239 format = kFileFormatCompressedFile;
2240 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002241
kwiberg55b97fe2016-01-28 05:22:45 -08002242 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002243
kwiberg55b97fe2016-01-28 05:22:45 -08002244 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002245 if (output_file_recorder_) {
2246 output_file_recorder_->RegisterModuleFileCallback(NULL);
2247 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002248 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002249
kwiberg5a25d952016-08-17 07:31:12 -07002250 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002251 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002252 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002253 _engineStatisticsPtr->SetLastError(
2254 VE_INVALID_ARGUMENT, kTraceError,
2255 "StartRecordingPlayout() fileRecorder format isnot correct");
2256 return -1;
2257 }
2258
kwiberg5a25d952016-08-17 07:31:12 -07002259 if (output_file_recorder_->StartRecordingAudioFile(
kwiberg55b97fe2016-01-28 05:22:45 -08002260 fileName, (const CodecInst&)*codecInst, notificationTime) != 0) {
2261 _engineStatisticsPtr->SetLastError(
2262 VE_BAD_FILE, kTraceError,
2263 "StartRecordingAudioFile() failed to start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002264 output_file_recorder_->StopRecording();
2265 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002266 return -1;
2267 }
kwiberg5a25d952016-08-17 07:31:12 -07002268 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002269 _outputFileRecording = true;
2270
2271 return 0;
2272}
2273
2274int Channel::StartRecordingPlayout(OutStream* stream,
2275 const CodecInst* codecInst) {
2276 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2277 "Channel::StartRecordingPlayout()");
2278
2279 if (_outputFileRecording) {
2280 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
2281 "StartRecordingPlayout() is already recording");
niklase@google.com470e71d2011-07-07 08:21:25 +00002282 return 0;
kwiberg55b97fe2016-01-28 05:22:45 -08002283 }
2284
2285 FileFormats format;
2286 const uint32_t notificationTime(0); // Not supported in VoE
2287 CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000};
2288
2289 if (codecInst != NULL && codecInst->channels != 1) {
2290 _engineStatisticsPtr->SetLastError(
2291 VE_BAD_ARGUMENT, kTraceError,
2292 "StartRecordingPlayout() invalid compression");
2293 return (-1);
2294 }
2295 if (codecInst == NULL) {
2296 format = kFileFormatPcm16kHzFile;
2297 codecInst = &dummyCodec;
2298 } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) ||
2299 (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) ||
2300 (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) {
2301 format = kFileFormatWavFile;
2302 } else {
2303 format = kFileFormatCompressedFile;
2304 }
2305
2306 rtc::CritScope cs(&_fileCritSect);
2307
2308 // Destroy the old instance
kwiberg5a25d952016-08-17 07:31:12 -07002309 if (output_file_recorder_) {
2310 output_file_recorder_->RegisterModuleFileCallback(NULL);
2311 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002312 }
2313
kwiberg5a25d952016-08-17 07:31:12 -07002314 output_file_recorder_ = FileRecorder::CreateFileRecorder(
kwiberg55b97fe2016-01-28 05:22:45 -08002315 _outputFileRecorderId, (const FileFormats)format);
kwiberg5a25d952016-08-17 07:31:12 -07002316 if (!output_file_recorder_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002317 _engineStatisticsPtr->SetLastError(
2318 VE_INVALID_ARGUMENT, kTraceError,
2319 "StartRecordingPlayout() fileRecorder format isnot correct");
2320 return -1;
2321 }
2322
kwiberg4ec01d92016-08-22 08:43:54 -07002323 if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst,
kwiberg5a25d952016-08-17 07:31:12 -07002324 notificationTime) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002325 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2326 "StartRecordingPlayout() failed to "
2327 "start file recording");
kwiberg5a25d952016-08-17 07:31:12 -07002328 output_file_recorder_->StopRecording();
2329 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002330 return -1;
2331 }
2332
kwiberg5a25d952016-08-17 07:31:12 -07002333 output_file_recorder_->RegisterModuleFileCallback(this);
kwiberg55b97fe2016-01-28 05:22:45 -08002334 _outputFileRecording = true;
2335
2336 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002337}
2338
kwiberg55b97fe2016-01-28 05:22:45 -08002339int Channel::StopRecordingPlayout() {
2340 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1),
2341 "Channel::StopRecordingPlayout()");
2342
2343 if (!_outputFileRecording) {
2344 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1),
2345 "StopRecordingPlayout() isnot recording");
2346 return -1;
2347 }
2348
2349 rtc::CritScope cs(&_fileCritSect);
2350
kwiberg5a25d952016-08-17 07:31:12 -07002351 if (output_file_recorder_->StopRecording() != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002352 _engineStatisticsPtr->SetLastError(
2353 VE_STOP_RECORDING_FAILED, kTraceError,
2354 "StopRecording() could not stop recording");
2355 return (-1);
2356 }
kwiberg5a25d952016-08-17 07:31:12 -07002357 output_file_recorder_->RegisterModuleFileCallback(NULL);
2358 output_file_recorder_.reset();
kwiberg55b97fe2016-01-28 05:22:45 -08002359 _outputFileRecording = false;
2360
2361 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002362}
2363
kwiberg55b97fe2016-01-28 05:22:45 -08002364void Channel::SetMixWithMicStatus(bool mix) {
2365 rtc::CritScope cs(&_fileCritSect);
2366 _mixFileWithMicrophone = mix;
niklase@google.com470e71d2011-07-07 08:21:25 +00002367}
2368
solenberg8d73f8c2017-03-08 01:52:20 -08002369int Channel::GetSpeechOutputLevel() const {
2370 return _outputAudioLevel.Level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002371}
2372
solenberg8d73f8c2017-03-08 01:52:20 -08002373int Channel::GetSpeechOutputLevelFullRange() const {
2374 return _outputAudioLevel.LevelFullRange();
kwiberg55b97fe2016-01-28 05:22:45 -08002375}
2376
zsteine76bd3a2017-07-14 12:17:49 -07002377double Channel::GetTotalOutputEnergy() const {
zstein3c451862017-07-20 09:57:42 -07002378 return _outputAudioLevel.TotalEnergy();
zsteine76bd3a2017-07-14 12:17:49 -07002379}
2380
2381double Channel::GetTotalOutputDuration() const {
zstein3c451862017-07-20 09:57:42 -07002382 return _outputAudioLevel.TotalDuration();
zsteine76bd3a2017-07-14 12:17:49 -07002383}
2384
solenberg8d73f8c2017-03-08 01:52:20 -08002385void Channel::SetInputMute(bool enable) {
kwiberg55b97fe2016-01-28 05:22:45 -08002386 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002387 input_mute_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00002388}
2389
solenberg1c2af8e2016-03-24 10:36:00 -07002390bool Channel::InputMute() const {
kwiberg55b97fe2016-01-28 05:22:45 -08002391 rtc::CritScope cs(&volume_settings_critsect_);
solenberg1c2af8e2016-03-24 10:36:00 -07002392 return input_mute_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002393}
2394
solenberg8d73f8c2017-03-08 01:52:20 -08002395void Channel::SetChannelOutputVolumeScaling(float scaling) {
kwiberg55b97fe2016-01-28 05:22:45 -08002396 rtc::CritScope cs(&volume_settings_critsect_);
kwiberg55b97fe2016-01-28 05:22:45 -08002397 _outputGain = scaling;
niklase@google.com470e71d2011-07-07 08:21:25 +00002398}
2399
solenberg8842c3e2016-03-11 03:06:41 -08002400int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
kwiberg55b97fe2016-01-28 05:22:45 -08002401 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
solenberg8842c3e2016-03-11 03:06:41 -08002402 "Channel::SendTelephoneEventOutband(...)");
2403 RTC_DCHECK_LE(0, event);
2404 RTC_DCHECK_GE(255, event);
2405 RTC_DCHECK_LE(0, duration_ms);
2406 RTC_DCHECK_GE(65535, duration_ms);
kwiberg55b97fe2016-01-28 05:22:45 -08002407 if (!Sending()) {
2408 return -1;
2409 }
solenberg8842c3e2016-03-11 03:06:41 -08002410 if (_rtpRtcpModule->SendTelephoneEventOutband(
2411 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
kwiberg55b97fe2016-01-28 05:22:45 -08002412 _engineStatisticsPtr->SetLastError(
2413 VE_SEND_DTMF_FAILED, kTraceWarning,
2414 "SendTelephoneEventOutband() failed to send event");
2415 return -1;
2416 }
2417 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002418}
2419
solenbergffbbcac2016-11-17 05:25:37 -08002420int Channel::SetSendTelephoneEventPayloadType(int payload_type,
2421 int payload_frequency) {
kwiberg55b97fe2016-01-28 05:22:45 -08002422 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00002423 "Channel::SetSendTelephoneEventPayloadType()");
solenberg31642aa2016-03-14 08:00:37 -07002424 RTC_DCHECK_LE(0, payload_type);
2425 RTC_DCHECK_GE(127, payload_type);
2426 CodecInst codec = {0};
solenberg31642aa2016-03-14 08:00:37 -07002427 codec.pltype = payload_type;
solenbergffbbcac2016-11-17 05:25:37 -08002428 codec.plfreq = payload_frequency;
kwiberg55b97fe2016-01-28 05:22:45 -08002429 memcpy(codec.plname, "telephone-event", 16);
2430 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2431 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2432 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2433 _engineStatisticsPtr->SetLastError(
2434 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2435 "SetSendTelephoneEventPayloadType() failed to register send"
2436 "payload type");
2437 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002438 }
kwiberg55b97fe2016-01-28 05:22:45 -08002439 }
kwiberg55b97fe2016-01-28 05:22:45 -08002440 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002441}
2442
kwiberg55b97fe2016-01-28 05:22:45 -08002443int Channel::SetLocalSSRC(unsigned int ssrc) {
2444 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2445 "Channel::SetLocalSSRC()");
2446 if (channel_state_.Get().sending) {
2447 _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError,
2448 "SetLocalSSRC() already sending");
2449 return -1;
2450 }
2451 _rtpRtcpModule->SetSSRC(ssrc);
2452 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002453}
2454
kwiberg55b97fe2016-01-28 05:22:45 -08002455int Channel::GetLocalSSRC(unsigned int& ssrc) {
2456 ssrc = _rtpRtcpModule->SSRC();
2457 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002458}
2459
kwiberg55b97fe2016-01-28 05:22:45 -08002460int Channel::GetRemoteSSRC(unsigned int& ssrc) {
2461 ssrc = rtp_receiver_->SSRC();
2462 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002463}
2464
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002465int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002466 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002467 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002468}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002469
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002470int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2471 unsigned char id) {
kwiberg55b97fe2016-01-28 05:22:45 -08002472 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
2473 if (enable &&
2474 !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
2475 id)) {
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002476 return -1;
2477 }
2478 return 0;
2479}
2480
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002481void Channel::EnableSendTransportSequenceNumber(int id) {
2482 int ret =
2483 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
2484 RTC_DCHECK_EQ(0, ret);
2485}
2486
stefan3313ec92016-01-21 06:32:43 -08002487void Channel::EnableReceiveTransportSequenceNumber(int id) {
2488 rtp_header_parser_->DeregisterRtpHeaderExtension(
2489 kRtpExtensionTransportSequenceNumber);
2490 bool ret = rtp_header_parser_->RegisterRtpHeaderExtension(
2491 kRtpExtensionTransportSequenceNumber, id);
2492 RTC_DCHECK(ret);
2493}
2494
stefanbba9dec2016-02-01 04:39:55 -08002495void Channel::RegisterSenderCongestionControlObjects(
nisseb8f9a322017-03-27 05:36:15 -07002496 RtpTransportControllerSendInterface* transport,
stefan7de8d642017-02-07 07:14:08 -08002497 RtcpBandwidthObserver* bandwidth_observer) {
nisseb8f9a322017-03-27 05:36:15 -07002498 RtpPacketSender* rtp_packet_sender = transport->packet_sender();
2499 TransportFeedbackObserver* transport_feedback_observer =
2500 transport->transport_feedback_observer();
2501 PacketRouter* packet_router = transport->packet_router();
2502
stefanbba9dec2016-02-01 04:39:55 -08002503 RTC_DCHECK(rtp_packet_sender);
2504 RTC_DCHECK(transport_feedback_observer);
2505 RTC_DCHECK(packet_router && !packet_router_);
stefan7de8d642017-02-07 07:14:08 -08002506 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
stefanbba9dec2016-02-01 04:39:55 -08002507 feedback_observer_proxy_->SetTransportFeedbackObserver(
2508 transport_feedback_observer);
2509 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
2510 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
2511 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
eladalon822ff2b2017-08-01 06:30:28 -07002512 constexpr bool remb_candidate = false;
2513 packet_router->AddSendRtpModule(_rtpRtcpModule.get(), remb_candidate);
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002514 packet_router_ = packet_router;
2515}
2516
stefanbba9dec2016-02-01 04:39:55 -08002517void Channel::RegisterReceiverCongestionControlObjects(
2518 PacketRouter* packet_router) {
2519 RTC_DCHECK(packet_router && !packet_router_);
eladalon822ff2b2017-08-01 06:30:28 -07002520 constexpr bool remb_candidate = false;
2521 packet_router->AddReceiveRtpModule(_rtpRtcpModule.get(), remb_candidate);
stefanbba9dec2016-02-01 04:39:55 -08002522 packet_router_ = packet_router;
2523}
2524
nissefdbfdc92017-03-31 05:44:52 -07002525void Channel::ResetSenderCongestionControlObjects() {
stefanbba9dec2016-02-01 04:39:55 -08002526 RTC_DCHECK(packet_router_);
2527 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
stefan7de8d642017-02-07 07:14:08 -08002528 rtcp_observer_->SetBandwidthObserver(nullptr);
stefanbba9dec2016-02-01 04:39:55 -08002529 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
2530 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
nissefdbfdc92017-03-31 05:44:52 -07002531 packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get());
stefanbba9dec2016-02-01 04:39:55 -08002532 packet_router_ = nullptr;
2533 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
2534}
2535
nissefdbfdc92017-03-31 05:44:52 -07002536void Channel::ResetReceiverCongestionControlObjects() {
2537 RTC_DCHECK(packet_router_);
2538 packet_router_->RemoveReceiveRtpModule(_rtpRtcpModule.get());
2539 packet_router_ = nullptr;
2540}
2541
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002542void Channel::SetRTCPStatus(bool enable) {
2543 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2544 "Channel::SetRTCPStatus()");
pbosda903ea2015-10-02 02:36:56 -07002545 _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002546}
2547
kwiberg55b97fe2016-01-28 05:22:45 -08002548int Channel::GetRTCPStatus(bool& enabled) {
pbosda903ea2015-10-02 02:36:56 -07002549 RtcpMode method = _rtpRtcpModule->RTCP();
2550 enabled = (method != RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002551 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002552}
2553
kwiberg55b97fe2016-01-28 05:22:45 -08002554int Channel::SetRTCP_CNAME(const char cName[256]) {
2555 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2556 "Channel::SetRTCP_CNAME()");
2557 if (_rtpRtcpModule->SetCNAME(cName) != 0) {
2558 _engineStatisticsPtr->SetLastError(
2559 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2560 "SetRTCP_CNAME() failed to set RTCP CNAME");
2561 return -1;
2562 }
2563 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002564}
2565
kwiberg55b97fe2016-01-28 05:22:45 -08002566int Channel::GetRemoteRTCP_CNAME(char cName[256]) {
2567 if (cName == NULL) {
2568 _engineStatisticsPtr->SetLastError(
2569 VE_INVALID_ARGUMENT, kTraceError,
2570 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2571 return -1;
2572 }
2573 char cname[RTCP_CNAME_SIZE];
2574 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
2575 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) {
2576 _engineStatisticsPtr->SetLastError(
2577 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2578 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2579 return -1;
2580 }
2581 strcpy(cName, cname);
2582 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002583}
2584
kwiberg55b97fe2016-01-28 05:22:45 -08002585int Channel::SendApplicationDefinedRTCPPacket(
2586 unsigned char subType,
2587 unsigned int name,
2588 const char* data,
2589 unsigned short dataLengthInBytes) {
2590 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2591 "Channel::SendApplicationDefinedRTCPPacket()");
2592 if (!channel_state_.Get().sending) {
2593 _engineStatisticsPtr->SetLastError(
2594 VE_NOT_SENDING, kTraceError,
2595 "SendApplicationDefinedRTCPPacket() not sending");
2596 return -1;
2597 }
2598 if (NULL == data) {
2599 _engineStatisticsPtr->SetLastError(
2600 VE_INVALID_ARGUMENT, kTraceError,
2601 "SendApplicationDefinedRTCPPacket() invalid data value");
2602 return -1;
2603 }
2604 if (dataLengthInBytes % 4 != 0) {
2605 _engineStatisticsPtr->SetLastError(
2606 VE_INVALID_ARGUMENT, kTraceError,
2607 "SendApplicationDefinedRTCPPacket() invalid length value");
2608 return -1;
2609 }
2610 RtcpMode status = _rtpRtcpModule->RTCP();
2611 if (status == RtcpMode::kOff) {
2612 _engineStatisticsPtr->SetLastError(
2613 VE_RTCP_ERROR, kTraceError,
2614 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2615 return -1;
2616 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002617
kwiberg55b97fe2016-01-28 05:22:45 -08002618 // Create and schedule the RTCP APP packet for transmission
2619 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
2620 subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) {
2621 _engineStatisticsPtr->SetLastError(
2622 VE_SEND_ERROR, kTraceError,
2623 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2624 return -1;
2625 }
2626 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002627}
2628
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002629int Channel::GetRemoteRTCPReportBlocks(
2630 std::vector<ReportBlock>* report_blocks) {
2631 if (report_blocks == NULL) {
kwiberg55b97fe2016-01-28 05:22:45 -08002632 _engineStatisticsPtr->SetLastError(
2633 VE_INVALID_ARGUMENT, kTraceError,
2634 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002635 return -1;
2636 }
2637
2638 // Get the report blocks from the latest received RTCP Sender or Receiver
2639 // Report. Each element in the vector contains the sender's SSRC and a
2640 // report block according to RFC 3550.
2641 std::vector<RTCPReportBlock> rtcp_report_blocks;
2642 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002643 return -1;
2644 }
2645
2646 if (rtcp_report_blocks.empty())
2647 return 0;
2648
2649 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
2650 for (; it != rtcp_report_blocks.end(); ++it) {
2651 ReportBlock report_block;
srte3e69e5c2017-08-09 06:13:45 -07002652 report_block.sender_SSRC = it->sender_ssrc;
2653 report_block.source_SSRC = it->source_ssrc;
2654 report_block.fraction_lost = it->fraction_lost;
2655 report_block.cumulative_num_packets_lost = it->packets_lost;
2656 report_block.extended_highest_sequence_number =
2657 it->extended_highest_sequence_number;
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002658 report_block.interarrival_jitter = it->jitter;
srte3e69e5c2017-08-09 06:13:45 -07002659 report_block.last_SR_timestamp = it->last_sender_report_timestamp;
2660 report_block.delay_since_last_SR = it->delay_since_last_sender_report;
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002661 report_blocks->push_back(report_block);
2662 }
2663 return 0;
2664}
2665
kwiberg55b97fe2016-01-28 05:22:45 -08002666int Channel::GetRTPStatistics(CallStatistics& stats) {
2667 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00002668
kwiberg55b97fe2016-01-28 05:22:45 -08002669 // The jitter statistics is updated for each received RTP packet and is
2670 // based on received packets.
2671 RtcpStatistics statistics;
2672 StreamStatistician* statistician =
2673 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
Peter Boström59013bc2016-02-12 11:35:08 +01002674 if (statistician) {
2675 statistician->GetStatistics(&statistics,
2676 _rtpRtcpModule->RTCP() == RtcpMode::kOff);
kwiberg55b97fe2016-01-28 05:22:45 -08002677 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002678
kwiberg55b97fe2016-01-28 05:22:45 -08002679 stats.fractionLost = statistics.fraction_lost;
srte186d9c32017-08-04 05:03:53 -07002680 stats.cumulativeLost = statistics.packets_lost;
2681 stats.extendedMax = statistics.extended_highest_sequence_number;
kwiberg55b97fe2016-01-28 05:22:45 -08002682 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00002683
kwiberg55b97fe2016-01-28 05:22:45 -08002684 // --- RTT
2685 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002686
kwiberg55b97fe2016-01-28 05:22:45 -08002687 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00002688
kwiberg55b97fe2016-01-28 05:22:45 -08002689 size_t bytesSent(0);
2690 uint32_t packetsSent(0);
2691 size_t bytesReceived(0);
2692 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002693
kwiberg55b97fe2016-01-28 05:22:45 -08002694 if (statistician) {
2695 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
2696 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002697
kwiberg55b97fe2016-01-28 05:22:45 -08002698 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
2699 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2700 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
2701 " output will not be complete");
2702 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002703
kwiberg55b97fe2016-01-28 05:22:45 -08002704 stats.bytesSent = bytesSent;
2705 stats.packetsSent = packetsSent;
2706 stats.bytesReceived = bytesReceived;
2707 stats.packetsReceived = packetsReceived;
niklase@google.com470e71d2011-07-07 08:21:25 +00002708
kwiberg55b97fe2016-01-28 05:22:45 -08002709 // --- Timestamps
2710 {
2711 rtc::CritScope lock(&ts_stats_lock_);
2712 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
2713 }
2714 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002715}
2716
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002717int Channel::SetCodecFECStatus(bool enable) {
2718 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2719 "Channel::SetCodecFECStatus()");
2720
kwibergc8d071e2016-04-06 12:22:38 -07002721 if (!codec_manager_.SetCodecFEC(enable) ||
2722 !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002723 _engineStatisticsPtr->SetLastError(
2724 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2725 "SetCodecFECStatus() failed to set FEC state");
2726 return -1;
2727 }
2728 return 0;
2729}
2730
2731bool Channel::GetCodecFECStatus() {
kwibergc8d071e2016-04-06 12:22:38 -07002732 return codec_manager_.GetStackParams()->use_codec_fec;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00002733}
2734
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002735void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
2736 // None of these functions can fail.
Stefan Holmerb86d4e42015-12-07 10:26:18 +01002737 // If pacing is enabled we always store packets.
2738 if (!pacing_enabled_)
2739 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00002740 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002741 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002742 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002743 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002744 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002745}
2746
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00002747// Called when we are missing one or more packets.
2748int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00002749 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
2750}
2751
henrikaec6fbd22017-03-31 05:43:36 -07002752void Channel::ProcessAndEncodeAudio(const AudioFrame& audio_input) {
henrika4515fa02017-05-03 08:30:15 -07002753 // Avoid posting any new tasks if sending was already stopped in StopSend().
2754 rtc::CritScope cs(&encoder_queue_lock_);
2755 if (!encoder_queue_is_active_) {
2756 return;
2757 }
henrikaec6fbd22017-03-31 05:43:36 -07002758 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
2759 // TODO(henrika): try to avoid copying by moving ownership of audio frame
2760 // either into pool of frames or into the task itself.
2761 audio_frame->CopyFrom(audio_input);
2762 audio_frame->id_ = ChannelId();
2763 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
2764 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
niklase@google.com470e71d2011-07-07 08:21:25 +00002765}
2766
henrikaec6fbd22017-03-31 05:43:36 -07002767void Channel::ProcessAndEncodeAudio(const int16_t* audio_data,
2768 int sample_rate,
2769 size_t number_of_frames,
2770 size_t number_of_channels) {
henrika4515fa02017-05-03 08:30:15 -07002771 // Avoid posting as new task if sending was already stopped in StopSend().
2772 rtc::CritScope cs(&encoder_queue_lock_);
2773 if (!encoder_queue_is_active_) {
2774 return;
2775 }
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002776 CodecInst codec;
ossu950c1c92017-07-11 08:19:31 -07002777 const int result = GetSendCodec(codec);
henrikaec6fbd22017-03-31 05:43:36 -07002778 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
2779 audio_frame->id_ = ChannelId();
ossu950c1c92017-07-11 08:19:31 -07002780 // TODO(ossu): Investigate how this could happen. b/62909493
2781 if (result == 0) {
2782 audio_frame->sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
2783 audio_frame->num_channels_ = std::min(number_of_channels, codec.channels);
2784 } else {
2785 audio_frame->sample_rate_hz_ = sample_rate;
2786 audio_frame->num_channels_ = number_of_channels;
2787 LOG(LS_WARNING) << "Unable to get send codec for channel " << ChannelId();
2788 RTC_NOTREACHED();
2789 }
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07002790 RemixAndResample(audio_data, number_of_frames, number_of_channels,
henrikaec6fbd22017-03-31 05:43:36 -07002791 sample_rate, &input_resampler_, audio_frame.get());
2792 encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
2793 new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00002794}
2795
henrikaec6fbd22017-03-31 05:43:36 -07002796void Channel::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
2797 RTC_DCHECK_RUN_ON(encoder_queue_);
2798 RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
2799 RTC_DCHECK_LE(audio_input->num_channels_, 2);
2800 RTC_DCHECK_EQ(audio_input->id_, ChannelId());
kwiberg55b97fe2016-01-28 05:22:45 -08002801
2802 if (channel_state_.Get().input_file_playing) {
henrikaec6fbd22017-03-31 05:43:36 -07002803 MixOrReplaceAudioWithFile(audio_input);
kwiberg55b97fe2016-01-28 05:22:45 -08002804 }
2805
henrikaec6fbd22017-03-31 05:43:36 -07002806 bool is_muted = InputMute();
2807 AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
kwiberg55b97fe2016-01-28 05:22:45 -08002808
kwiberg55b97fe2016-01-28 05:22:45 -08002809 if (_includeAudioLevelIndication) {
2810 size_t length =
henrikaec6fbd22017-03-31 05:43:36 -07002811 audio_input->samples_per_channel_ * audio_input->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002812 RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes);
solenberg1c2af8e2016-03-24 10:36:00 -07002813 if (is_muted && previous_frame_muted_) {
henrik.lundin50499422016-11-29 04:26:24 -08002814 rms_level_.AnalyzeMuted(length);
kwiberg55b97fe2016-01-28 05:22:45 -08002815 } else {
henrik.lundin50499422016-11-29 04:26:24 -08002816 rms_level_.Analyze(
yujo36b1a5f2017-06-12 12:45:32 -07002817 rtc::ArrayView<const int16_t>(audio_input->data(), length));
niklase@google.com470e71d2011-07-07 08:21:25 +00002818 }
kwiberg55b97fe2016-01-28 05:22:45 -08002819 }
solenberg1c2af8e2016-03-24 10:36:00 -07002820 previous_frame_muted_ = is_muted;
niklase@google.com470e71d2011-07-07 08:21:25 +00002821
henrikaec6fbd22017-03-31 05:43:36 -07002822 // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
niklase@google.com470e71d2011-07-07 08:21:25 +00002823
kwiberg55b97fe2016-01-28 05:22:45 -08002824 // The ACM resamples internally.
henrikaec6fbd22017-03-31 05:43:36 -07002825 audio_input->timestamp_ = _timeStamp;
kwiberg55b97fe2016-01-28 05:22:45 -08002826 // This call will trigger AudioPacketizationCallback::SendData if encoding
2827 // is done and payload is ready for packetization and transmission.
2828 // Otherwise, it will return without invoking the callback.
henrikaec6fbd22017-03-31 05:43:36 -07002829 if (audio_coding_->Add10MsData(*audio_input) < 0) {
2830 LOG(LS_ERROR) << "ACM::Add10MsData() failed for channel " << _channelId;
2831 return;
kwiberg55b97fe2016-01-28 05:22:45 -08002832 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002833
henrikaec6fbd22017-03-31 05:43:36 -07002834 _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002835}
2836
solenberg7602aab2016-11-14 11:30:07 -08002837void Channel::set_associate_send_channel(const ChannelOwner& channel) {
2838 RTC_DCHECK(!channel.channel() ||
2839 channel.channel()->ChannelId() != _channelId);
2840 rtc::CritScope lock(&assoc_send_channel_lock_);
2841 associate_send_channel_ = channel;
2842}
2843
Minyue2013aec2015-05-13 14:14:42 +02002844void Channel::DisassociateSendChannel(int channel_id) {
tommi31fc21f2016-01-21 10:37:37 -08002845 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02002846 Channel* channel = associate_send_channel_.channel();
2847 if (channel && channel->ChannelId() == channel_id) {
2848 // If this channel is associated with a send channel of the specified
2849 // Channel ID, disassociate with it.
2850 ChannelOwner ref(NULL);
2851 associate_send_channel_ = ref;
2852 }
2853}
2854
ivoc14d5dbe2016-07-04 07:06:55 -07002855void Channel::SetRtcEventLog(RtcEventLog* event_log) {
2856 event_log_proxy_->SetEventLog(event_log);
2857}
2858
michaelt9332b7d2016-11-30 07:51:13 -08002859void Channel::SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats) {
2860 rtcp_rtt_stats_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
2861}
2862
nisse284542b2017-01-10 08:58:32 -08002863void Channel::UpdateOverheadForEncoder() {
hbos3fd31fe2017-02-28 05:43:16 -08002864 size_t overhead_per_packet =
2865 transport_overhead_per_packet_ + rtp_overhead_per_packet_;
nisse284542b2017-01-10 08:58:32 -08002866 audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
2867 if (*encoder) {
hbos3fd31fe2017-02-28 05:43:16 -08002868 (*encoder)->OnReceivedOverhead(overhead_per_packet);
nisse284542b2017-01-10 08:58:32 -08002869 }
2870 });
2871}
2872
2873void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002874 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002875 transport_overhead_per_packet_ = transport_overhead_per_packet;
2876 UpdateOverheadForEncoder();
michaelt79e05882016-11-08 02:50:09 -08002877}
2878
hbos3fd31fe2017-02-28 05:43:16 -08002879// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
michaeltbf65be52016-12-15 06:24:49 -08002880void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
hbos3fd31fe2017-02-28 05:43:16 -08002881 rtc::CritScope cs(&overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -08002882 rtp_overhead_per_packet_ = overhead_bytes_per_packet;
2883 UpdateOverheadForEncoder();
michaeltbf65be52016-12-15 06:24:49 -08002884}
2885
kwiberg55b97fe2016-01-28 05:22:45 -08002886int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
2887 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00002888}
2889
wu@webrtc.org24301a62013-12-13 19:17:43 +00002890void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
2891 audio_coding_->GetDecodingCallStatistics(stats);
2892}
2893
solenberg358057b2015-11-27 10:46:42 -08002894uint32_t Channel::GetDelayEstimate() const {
solenberg08b19df2017-02-15 00:42:31 -08002895 rtc::CritScope lock(&video_sync_lock_);
2896 return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
deadbeef74375882015-08-13 12:09:10 -07002897}
2898
kwiberg55b97fe2016-01-28 05:22:45 -08002899int Channel::SetMinimumPlayoutDelay(int delayMs) {
2900 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2901 "Channel::SetMinimumPlayoutDelay()");
2902 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
2903 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
2904 _engineStatisticsPtr->SetLastError(
2905 VE_INVALID_ARGUMENT, kTraceError,
2906 "SetMinimumPlayoutDelay() invalid min delay");
2907 return -1;
2908 }
2909 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
2910 _engineStatisticsPtr->SetLastError(
2911 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2912 "SetMinimumPlayoutDelay() failed to set min playout delay");
2913 return -1;
2914 }
2915 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002916}
2917
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002918int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07002919 uint32_t playout_timestamp_rtp = 0;
2920 {
tommi31fc21f2016-01-21 10:37:37 -08002921 rtc::CritScope lock(&video_sync_lock_);
deadbeef74375882015-08-13 12:09:10 -07002922 playout_timestamp_rtp = playout_timestamp_rtp_;
2923 }
kwiberg55b97fe2016-01-28 05:22:45 -08002924 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002925 _engineStatisticsPtr->SetLastError(
skvlad4c0536b2016-07-07 13:06:26 -07002926 VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo,
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002927 "GetPlayoutTimestamp() failed to retrieve timestamp");
2928 return -1;
2929 }
deadbeef74375882015-08-13 12:09:10 -07002930 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002931 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002932}
2933
kwiberg55b97fe2016-01-28 05:22:45 -08002934int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule,
2935 RtpReceiver** rtp_receiver) const {
2936 *rtpRtcpModule = _rtpRtcpModule.get();
2937 *rtp_receiver = rtp_receiver_.get();
2938 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002939}
2940
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00002941// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
2942// a shared helper.
henrikaec6fbd22017-03-31 05:43:36 -07002943int32_t Channel::MixOrReplaceAudioWithFile(AudioFrame* audio_input) {
2944 RTC_DCHECK_RUN_ON(encoder_queue_);
kwibergb7f89d62016-02-17 10:04:18 -08002945 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]);
kwiberg55b97fe2016-01-28 05:22:45 -08002946 size_t fileSamples(0);
henrikaec6fbd22017-03-31 05:43:36 -07002947 const int mixingFrequency = audio_input->sample_rate_hz_;
kwiberg55b97fe2016-01-28 05:22:45 -08002948 {
2949 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002950
kwiberg5a25d952016-08-17 07:31:12 -07002951 if (!input_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08002952 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2953 "Channel::MixOrReplaceAudioWithFile() fileplayer"
2954 " doesnt exist");
2955 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002956 }
2957
kwiberg4ec01d92016-08-22 08:43:54 -07002958 if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples,
kwiberg5a25d952016-08-17 07:31:12 -07002959 mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08002960 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2961 "Channel::MixOrReplaceAudioWithFile() file mixing "
2962 "failed");
2963 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002964 }
kwiberg55b97fe2016-01-28 05:22:45 -08002965 if (fileSamples == 0) {
2966 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
2967 "Channel::MixOrReplaceAudioWithFile() file is ended");
2968 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002969 }
kwiberg55b97fe2016-01-28 05:22:45 -08002970 }
2971
henrikaec6fbd22017-03-31 05:43:36 -07002972 RTC_DCHECK_EQ(audio_input->samples_per_channel_, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08002973
2974 if (_mixFileWithMicrophone) {
2975 // Currently file stream is always mono.
2976 // TODO(xians): Change the code when FilePlayer supports real stereo.
yujo36b1a5f2017-06-12 12:45:32 -07002977 MixWithSat(audio_input->mutable_data(), audio_input->num_channels_,
2978 fileBuffer.get(), 1, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08002979 } else {
2980 // Replace ACM audio with file.
2981 // Currently file stream is always mono.
2982 // TODO(xians): Change the code when FilePlayer supports real stereo.
henrikaec6fbd22017-03-31 05:43:36 -07002983 audio_input->UpdateFrame(
kwiberg55b97fe2016-01-28 05:22:45 -08002984 _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency,
2985 AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1);
2986 }
2987 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002988}
2989
kwiberg55b97fe2016-01-28 05:22:45 -08002990int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) {
2991 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002992
kwibergb7f89d62016-02-17 10:04:18 -08002993 std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]);
kwiberg55b97fe2016-01-28 05:22:45 -08002994 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002995
kwiberg55b97fe2016-01-28 05:22:45 -08002996 {
2997 rtc::CritScope cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002998
kwiberg5a25d952016-08-17 07:31:12 -07002999 if (!output_file_player_) {
kwiberg55b97fe2016-01-28 05:22:45 -08003000 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3001 "Channel::MixAudioWithFile() file mixing failed");
3002 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003003 }
3004
kwiberg55b97fe2016-01-28 05:22:45 -08003005 // We should get the frequency we ask for.
kwiberg4ec01d92016-08-22 08:43:54 -07003006 if (output_file_player_->Get10msAudioFromFile(
3007 fileBuffer.get(), &fileSamples, mixingFrequency) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003008 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3009 "Channel::MixAudioWithFile() file mixing failed");
3010 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003011 }
kwiberg55b97fe2016-01-28 05:22:45 -08003012 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003013
kwiberg55b97fe2016-01-28 05:22:45 -08003014 if (audioFrame.samples_per_channel_ == fileSamples) {
3015 // Currently file stream is always mono.
3016 // TODO(xians): Change the code when FilePlayer supports real stereo.
yujo36b1a5f2017-06-12 12:45:32 -07003017 MixWithSat(audioFrame.mutable_data(), audioFrame.num_channels_,
3018 fileBuffer.get(), 1, fileSamples);
kwiberg55b97fe2016-01-28 05:22:45 -08003019 } else {
3020 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3021 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS
3022 ") != "
3023 "fileSamples(%" PRIuS ")",
3024 audioFrame.samples_per_channel_, fileSamples);
3025 return -1;
3026 }
3027
3028 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003029}
3030
deadbeef74375882015-08-13 12:09:10 -07003031void Channel::UpdatePlayoutTimestamp(bool rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003032 jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
deadbeef74375882015-08-13 12:09:10 -07003033
henrik.lundin96bd5022016-04-06 04:13:56 -07003034 if (!jitter_buffer_playout_timestamp_) {
3035 // This can happen if this channel has not received any RTP packets. In
3036 // this case, NetEq is not capable of computing a playout timestamp.
deadbeef74375882015-08-13 12:09:10 -07003037 return;
3038 }
3039
3040 uint16_t delay_ms = 0;
3041 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
kwiberg55b97fe2016-01-28 05:22:45 -08003042 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003043 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3044 " delay from the ADM");
3045 _engineStatisticsPtr->SetLastError(
3046 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3047 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3048 return;
3049 }
3050
henrik.lundin96bd5022016-04-06 04:13:56 -07003051 RTC_DCHECK(jitter_buffer_playout_timestamp_);
3052 uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
deadbeef74375882015-08-13 12:09:10 -07003053
3054 // Remove the playout delay.
ossue280cde2016-10-12 11:04:10 -07003055 playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
deadbeef74375882015-08-13 12:09:10 -07003056
kwiberg55b97fe2016-01-28 05:22:45 -08003057 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId),
deadbeef74375882015-08-13 12:09:10 -07003058 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
henrik.lundin96bd5022016-04-06 04:13:56 -07003059 playout_timestamp);
deadbeef74375882015-08-13 12:09:10 -07003060
3061 {
tommi31fc21f2016-01-21 10:37:37 -08003062 rtc::CritScope lock(&video_sync_lock_);
solenberg81d93f32017-02-14 03:44:57 -08003063 if (!rtcp) {
henrik.lundin96bd5022016-04-06 04:13:56 -07003064 playout_timestamp_rtp_ = playout_timestamp;
deadbeef74375882015-08-13 12:09:10 -07003065 }
3066 playout_delay_ms_ = delay_ms;
3067 }
3068}
3069
kwiberg55b97fe2016-01-28 05:22:45 -08003070void Channel::RegisterReceiveCodecsToRTPModule() {
3071 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3072 "Channel::RegisterReceiveCodecsToRTPModule()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003073
kwiberg55b97fe2016-01-28 05:22:45 -08003074 CodecInst codec;
3075 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003076
kwiberg55b97fe2016-01-28 05:22:45 -08003077 for (int idx = 0; idx < nSupportedCodecs; idx++) {
3078 // Open up the RTP/RTCP receiver for all supported codecs
3079 if ((audio_coding_->Codec(idx, &codec) == -1) ||
magjed56124bd2016-11-24 09:34:46 -08003080 (rtp_receiver_->RegisterReceivePayload(codec) == -1)) {
kwiberg55b97fe2016-01-28 05:22:45 -08003081 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3082 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3083 " to register %s (%d/%d/%" PRIuS
3084 "/%d) to RTP/RTCP "
3085 "receiver",
3086 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3087 codec.rate);
3088 } else {
3089 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3090 "Channel::RegisterReceiveCodecsToRTPModule() %s "
3091 "(%d/%d/%" PRIuS
3092 "/%d) has been added to the RTP/RTCP "
3093 "receiver",
3094 codec.plname, codec.pltype, codec.plfreq, codec.channels,
3095 codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +00003096 }
kwiberg55b97fe2016-01-28 05:22:45 -08003097 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003098}
3099
kwiberg55b97fe2016-01-28 05:22:45 -08003100int Channel::SetSendRtpHeaderExtension(bool enable,
3101 RTPExtensionType type,
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003102 unsigned char id) {
3103 int error = 0;
3104 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3105 if (enable) {
3106 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3107 }
3108 return error;
3109}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003110
ossue280cde2016-10-12 11:04:10 -07003111int Channel::GetRtpTimestampRateHz() const {
3112 const auto format = audio_coding_->ReceiveFormat();
3113 // Default to the playout frequency if we've not gotten any packets yet.
3114 // TODO(ossu): Zero clockrate can only happen if we've added an external
3115 // decoder for a format we don't support internally. Remove once that way of
3116 // adding decoders is gone!
3117 return (format && format->clockrate_hz != 0)
3118 ? format->clockrate_hz
3119 : audio_coding_->PlayoutFrequency();
wu@webrtc.org94454b72014-06-05 20:34:08 +00003120}
3121
Minyue2013aec2015-05-13 14:14:42 +02003122int64_t Channel::GetRTT(bool allow_associate_channel) const {
pbosda903ea2015-10-02 02:36:56 -07003123 RtcpMode method = _rtpRtcpModule->RTCP();
3124 if (method == RtcpMode::kOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003125 return 0;
3126 }
3127 std::vector<RTCPReportBlock> report_blocks;
3128 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003129
3130 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003131 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003132 if (allow_associate_channel) {
tommi31fc21f2016-01-21 10:37:37 -08003133 rtc::CritScope lock(&assoc_send_channel_lock_);
Minyue2013aec2015-05-13 14:14:42 +02003134 Channel* channel = associate_send_channel_.channel();
3135 // Tries to get RTT from an associated channel. This is important for
3136 // receive-only channels.
3137 if (channel) {
3138 // To prevent infinite recursion and deadlock, calling GetRTT of
3139 // associate channel should always use "false" for argument:
3140 // |allow_associate_channel|.
3141 rtt = channel->GetRTT(false);
3142 }
3143 }
3144 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003145 }
3146
3147 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3148 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3149 for (; it != report_blocks.end(); ++it) {
srte3e69e5c2017-08-09 06:13:45 -07003150 if (it->sender_ssrc == remoteSSRC)
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003151 break;
3152 }
3153 if (it == report_blocks.end()) {
3154 // We have not received packets with SSRC matching the report blocks.
3155 // To calculate RTT we try with the SSRC of the first report block.
3156 // This is very important for send-only channels where we don't know
3157 // the SSRC of the other end.
srte3e69e5c2017-08-09 06:13:45 -07003158 remoteSSRC = report_blocks[0].sender_ssrc;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003159 }
Minyue2013aec2015-05-13 14:14:42 +02003160
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003161 int64_t avg_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003162 int64_t max_rtt = 0;
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003163 int64_t min_rtt = 0;
kwiberg55b97fe2016-01-28 05:22:45 -08003164 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
3165 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003166 return 0;
3167 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003168 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003169}
3170
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003171} // namespace voe
3172} // namespace webrtc