niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
henrika@webrtc.org | 2919e95 | 2012-01-31 08:45:03 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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.org | 6388c3e | 2013-02-12 21:42:18 +0000 | [diff] [blame] | 11 | #include "webrtc/voice_engine/channel.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
Henrik Lundin | 64dad83 | 2015-05-11 12:44:23 +0200 | [diff] [blame] | 13 | #include <algorithm> |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 14 | #include <utility> |
Henrik Lundin | 64dad83 | 2015-05-11 12:44:23 +0200 | [diff] [blame] | 15 | |
Ivo Creusen | ae856f2 | 2015-09-17 16:30:16 +0200 | [diff] [blame] | 16 | #include "webrtc/base/checks.h" |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 17 | #include "webrtc/base/criticalsection.h" |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 18 | #include "webrtc/base/format_macros.h" |
pbos | ad85622 | 2015-11-27 09:48:36 -0800 | [diff] [blame] | 19 | #include "webrtc/base/logging.h" |
Erik Språng | 737336d | 2016-07-29 12:59:36 +0200 | [diff] [blame] | 20 | #include "webrtc/base/rate_limiter.h" |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 21 | #include "webrtc/base/thread_checker.h" |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 22 | #include "webrtc/base/timeutils.h" |
Henrik Lundin | 64dad83 | 2015-05-11 12:44:23 +0200 | [diff] [blame] | 23 | #include "webrtc/config.h" |
skvlad | cc91d28 | 2016-10-03 18:31:22 -0700 | [diff] [blame] | 24 | #include "webrtc/logging/rtc_event_log/rtc_event_log.h" |
turaj@webrtc.org | 6388c3e | 2013-02-12 21:42:18 +0000 | [diff] [blame] | 25 | #include "webrtc/modules/audio_device/include/audio_device.h" |
| 26 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 27 | #include "webrtc/modules/include/module_common_types.h" |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 28 | #include "webrtc/modules/pacing/packet_router.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 29 | #include "webrtc/modules/rtp_rtcp/include/receive_statistics.h" |
| 30 | #include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h" |
| 31 | #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 32 | #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 33 | #include "webrtc/modules/utility/include/audio_frame_operations.h" |
| 34 | #include "webrtc/modules/utility/include/process_thread.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 35 | #include "webrtc/system_wrappers/include/trace.h" |
turaj@webrtc.org | 6388c3e | 2013-02-12 21:42:18 +0000 | [diff] [blame] | 36 | #include "webrtc/voice_engine/include/voe_external_media.h" |
| 37 | #include "webrtc/voice_engine/include/voe_rtp_rtcp.h" |
| 38 | #include "webrtc/voice_engine/output_mixer.h" |
| 39 | #include "webrtc/voice_engine/statistics.h" |
| 40 | #include "webrtc/voice_engine/transmit_mixer.h" |
| 41 | #include "webrtc/voice_engine/utility.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 43 | namespace webrtc { |
| 44 | namespace voe { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 46 | namespace { |
| 47 | |
Erik Språng | 737336d | 2016-07-29 12:59:36 +0200 | [diff] [blame] | 48 | constexpr int64_t kMaxRetransmissionWindowMs = 1000; |
| 49 | constexpr int64_t kMinRetransmissionWindowMs = 30; |
| 50 | |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 51 | bool RegisterReceiveCodec(std::unique_ptr<AudioCodingModule>* acm, |
| 52 | acm2::RentACodec* rac, |
| 53 | const CodecInst& ci) { |
kwiberg | abe95ba | 2016-06-02 02:58:59 -0700 | [diff] [blame] | 54 | const int result = (*acm)->RegisterReceiveCodec( |
| 55 | ci, [&] { return rac->RentIsacDecoder(ci.plfreq); }); |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 56 | return result == 0; |
| 57 | } |
| 58 | |
| 59 | } // namespace |
| 60 | |
solenberg | 8842c3e | 2016-03-11 03:06:41 -0800 | [diff] [blame] | 61 | const int kTelephoneEventAttenuationdB = 10; |
| 62 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 63 | class RtcEventLogProxy final : public webrtc::RtcEventLog { |
| 64 | public: |
| 65 | RtcEventLogProxy() : event_log_(nullptr) {} |
| 66 | |
| 67 | bool StartLogging(const std::string& file_name, |
| 68 | int64_t max_size_bytes) override { |
| 69 | RTC_NOTREACHED(); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | bool StartLogging(rtc::PlatformFile log_file, |
| 74 | int64_t max_size_bytes) override { |
| 75 | RTC_NOTREACHED(); |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | void StopLogging() override { RTC_NOTREACHED(); } |
| 80 | |
| 81 | void LogVideoReceiveStreamConfig( |
| 82 | const webrtc::VideoReceiveStream::Config& config) override { |
| 83 | rtc::CritScope lock(&crit_); |
| 84 | if (event_log_) { |
| 85 | event_log_->LogVideoReceiveStreamConfig(config); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void LogVideoSendStreamConfig( |
| 90 | const webrtc::VideoSendStream::Config& config) override { |
| 91 | rtc::CritScope lock(&crit_); |
| 92 | if (event_log_) { |
| 93 | event_log_->LogVideoSendStreamConfig(config); |
| 94 | } |
| 95 | } |
| 96 | |
ivoc | e0928d8 | 2016-10-10 05:12:51 -0700 | [diff] [blame^] | 97 | void LogAudioReceiveStreamConfig( |
| 98 | const webrtc::AudioReceiveStream::Config& config) override { |
| 99 | rtc::CritScope lock(&crit_); |
| 100 | if (event_log_) { |
| 101 | event_log_->LogAudioReceiveStreamConfig(config); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void LogAudioSendStreamConfig( |
| 106 | const webrtc::AudioSendStream::Config& config) override { |
| 107 | rtc::CritScope lock(&crit_); |
| 108 | if (event_log_) { |
| 109 | event_log_->LogAudioSendStreamConfig(config); |
| 110 | } |
| 111 | } |
| 112 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 113 | void LogRtpHeader(webrtc::PacketDirection direction, |
| 114 | webrtc::MediaType media_type, |
| 115 | const uint8_t* header, |
| 116 | size_t packet_length) override { |
| 117 | rtc::CritScope lock(&crit_); |
| 118 | if (event_log_) { |
| 119 | event_log_->LogRtpHeader(direction, media_type, header, packet_length); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void LogRtcpPacket(webrtc::PacketDirection direction, |
| 124 | webrtc::MediaType media_type, |
| 125 | const uint8_t* packet, |
| 126 | size_t length) override { |
| 127 | rtc::CritScope lock(&crit_); |
| 128 | if (event_log_) { |
| 129 | event_log_->LogRtcpPacket(direction, media_type, packet, length); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void LogAudioPlayout(uint32_t ssrc) override { |
| 134 | rtc::CritScope lock(&crit_); |
| 135 | if (event_log_) { |
| 136 | event_log_->LogAudioPlayout(ssrc); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void LogBwePacketLossEvent(int32_t bitrate, |
| 141 | uint8_t fraction_loss, |
| 142 | int32_t total_packets) override { |
| 143 | rtc::CritScope lock(&crit_); |
| 144 | if (event_log_) { |
| 145 | event_log_->LogBwePacketLossEvent(bitrate, fraction_loss, total_packets); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void SetEventLog(RtcEventLog* event_log) { |
| 150 | rtc::CritScope lock(&crit_); |
| 151 | event_log_ = event_log; |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | rtc::CriticalSection crit_; |
| 156 | RtcEventLog* event_log_ GUARDED_BY(crit_); |
| 157 | RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogProxy); |
| 158 | }; |
| 159 | |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 160 | class TransportFeedbackProxy : public TransportFeedbackObserver { |
| 161 | public: |
| 162 | TransportFeedbackProxy() : feedback_observer_(nullptr) { |
| 163 | pacer_thread_.DetachFromThread(); |
| 164 | network_thread_.DetachFromThread(); |
| 165 | } |
| 166 | |
| 167 | void SetTransportFeedbackObserver( |
| 168 | TransportFeedbackObserver* feedback_observer) { |
| 169 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 170 | rtc::CritScope lock(&crit_); |
| 171 | feedback_observer_ = feedback_observer; |
| 172 | } |
| 173 | |
| 174 | // Implements TransportFeedbackObserver. |
| 175 | void AddPacket(uint16_t sequence_number, |
| 176 | size_t length, |
philipel | a1ed0b3 | 2016-06-01 06:31:17 -0700 | [diff] [blame] | 177 | int probe_cluster_id) override { |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 178 | RTC_DCHECK(pacer_thread_.CalledOnValidThread()); |
| 179 | rtc::CritScope lock(&crit_); |
| 180 | if (feedback_observer_) |
pbos | 2169d8b | 2016-06-20 11:53:02 -0700 | [diff] [blame] | 181 | feedback_observer_->AddPacket(sequence_number, length, probe_cluster_id); |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 182 | } |
| 183 | void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override { |
| 184 | RTC_DCHECK(network_thread_.CalledOnValidThread()); |
| 185 | rtc::CritScope lock(&crit_); |
Stefan Holmer | 60e4346 | 2016-09-07 09:58:20 +0200 | [diff] [blame] | 186 | feedback_observer_->OnTransportFeedback(feedback); |
| 187 | } |
| 188 | std::vector<PacketInfo> GetTransportFeedbackVector() const override { |
| 189 | RTC_NOTREACHED(); |
| 190 | return std::vector<PacketInfo>(); |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | private: |
| 194 | rtc::CriticalSection crit_; |
| 195 | rtc::ThreadChecker thread_checker_; |
| 196 | rtc::ThreadChecker pacer_thread_; |
| 197 | rtc::ThreadChecker network_thread_; |
| 198 | TransportFeedbackObserver* feedback_observer_ GUARDED_BY(&crit_); |
| 199 | }; |
| 200 | |
| 201 | class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator { |
| 202 | public: |
| 203 | TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) { |
| 204 | pacer_thread_.DetachFromThread(); |
| 205 | } |
| 206 | |
| 207 | void SetSequenceNumberAllocator( |
| 208 | TransportSequenceNumberAllocator* seq_num_allocator) { |
| 209 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 210 | rtc::CritScope lock(&crit_); |
| 211 | seq_num_allocator_ = seq_num_allocator; |
| 212 | } |
| 213 | |
| 214 | // Implements TransportSequenceNumberAllocator. |
| 215 | uint16_t AllocateSequenceNumber() override { |
| 216 | RTC_DCHECK(pacer_thread_.CalledOnValidThread()); |
| 217 | rtc::CritScope lock(&crit_); |
| 218 | if (!seq_num_allocator_) |
| 219 | return 0; |
| 220 | return seq_num_allocator_->AllocateSequenceNumber(); |
| 221 | } |
| 222 | |
| 223 | private: |
| 224 | rtc::CriticalSection crit_; |
| 225 | rtc::ThreadChecker thread_checker_; |
| 226 | rtc::ThreadChecker pacer_thread_; |
| 227 | TransportSequenceNumberAllocator* seq_num_allocator_ GUARDED_BY(&crit_); |
| 228 | }; |
| 229 | |
| 230 | class RtpPacketSenderProxy : public RtpPacketSender { |
| 231 | public: |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 232 | RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {} |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 233 | |
| 234 | void SetPacketSender(RtpPacketSender* rtp_packet_sender) { |
| 235 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 236 | rtc::CritScope lock(&crit_); |
| 237 | rtp_packet_sender_ = rtp_packet_sender; |
| 238 | } |
| 239 | |
| 240 | // Implements RtpPacketSender. |
| 241 | void InsertPacket(Priority priority, |
| 242 | uint32_t ssrc, |
| 243 | uint16_t sequence_number, |
| 244 | int64_t capture_time_ms, |
| 245 | size_t bytes, |
| 246 | bool retransmission) override { |
| 247 | rtc::CritScope lock(&crit_); |
| 248 | if (rtp_packet_sender_) { |
| 249 | rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number, |
| 250 | capture_time_ms, bytes, retransmission); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | private: |
| 255 | rtc::ThreadChecker thread_checker_; |
| 256 | rtc::CriticalSection crit_; |
| 257 | RtpPacketSender* rtp_packet_sender_ GUARDED_BY(&crit_); |
| 258 | }; |
| 259 | |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 260 | // Extend the default RTCP statistics struct with max_jitter, defined as the |
| 261 | // maximum jitter value seen in an RTCP report block. |
| 262 | struct ChannelStatistics : public RtcpStatistics { |
| 263 | ChannelStatistics() : rtcp(), max_jitter(0) {} |
| 264 | |
| 265 | RtcpStatistics rtcp; |
| 266 | uint32_t max_jitter; |
| 267 | }; |
| 268 | |
| 269 | // Statistics callback, called at each generation of a new RTCP report block. |
| 270 | class StatisticsProxy : public RtcpStatisticsCallback { |
| 271 | public: |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 272 | StatisticsProxy(uint32_t ssrc) : ssrc_(ssrc) {} |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 273 | virtual ~StatisticsProxy() {} |
| 274 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 275 | void StatisticsUpdated(const RtcpStatistics& statistics, |
| 276 | uint32_t ssrc) override { |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 277 | if (ssrc != ssrc_) |
| 278 | return; |
| 279 | |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 280 | rtc::CritScope cs(&stats_lock_); |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 281 | stats_.rtcp = statistics; |
| 282 | if (statistics.jitter > stats_.max_jitter) { |
| 283 | stats_.max_jitter = statistics.jitter; |
| 284 | } |
| 285 | } |
| 286 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 287 | void CNameChanged(const char* cname, uint32_t ssrc) override {} |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 288 | |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 289 | ChannelStatistics GetStats() { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 290 | rtc::CritScope cs(&stats_lock_); |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 291 | return stats_; |
| 292 | } |
| 293 | |
| 294 | private: |
| 295 | // StatisticsUpdated calls are triggered from threads in the RTP module, |
| 296 | // while GetStats calls can be triggered from the public voice engine API, |
| 297 | // hence synchronization is needed. |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 298 | rtc::CriticalSection stats_lock_; |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 299 | const uint32_t ssrc_; |
| 300 | ChannelStatistics stats_; |
| 301 | }; |
| 302 | |
mflodman@webrtc.org | 0a7d4ee | 2015-02-17 12:57:14 +0000 | [diff] [blame] | 303 | class VoERtcpObserver : public RtcpBandwidthObserver { |
minyue@webrtc.org | c1a40a7 | 2014-05-28 09:52:06 +0000 | [diff] [blame] | 304 | public: |
mflodman@webrtc.org | 0a7d4ee | 2015-02-17 12:57:14 +0000 | [diff] [blame] | 305 | explicit VoERtcpObserver(Channel* owner) : owner_(owner) {} |
| 306 | virtual ~VoERtcpObserver() {} |
minyue@webrtc.org | c1a40a7 | 2014-05-28 09:52:06 +0000 | [diff] [blame] | 307 | |
mflodman@webrtc.org | 0a7d4ee | 2015-02-17 12:57:14 +0000 | [diff] [blame] | 308 | void OnReceivedEstimatedBitrate(uint32_t bitrate) override { |
| 309 | // Not used for Voice Engine. |
| 310 | } |
| 311 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 312 | void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks, |
| 313 | int64_t rtt, |
| 314 | int64_t now_ms) override { |
mflodman@webrtc.org | 0a7d4ee | 2015-02-17 12:57:14 +0000 | [diff] [blame] | 315 | // TODO(mflodman): Do we need to aggregate reports here or can we jut send |
| 316 | // what we get? I.e. do we ever get multiple reports bundled into one RTCP |
| 317 | // report for VoiceEngine? |
| 318 | if (report_blocks.empty()) |
| 319 | return; |
| 320 | |
| 321 | int fraction_lost_aggregate = 0; |
| 322 | int total_number_of_packets = 0; |
| 323 | |
| 324 | // If receiving multiple report blocks, calculate the weighted average based |
| 325 | // on the number of packets a report refers to. |
| 326 | for (ReportBlockList::const_iterator block_it = report_blocks.begin(); |
| 327 | block_it != report_blocks.end(); ++block_it) { |
| 328 | // Find the previous extended high sequence number for this remote SSRC, |
| 329 | // to calculate the number of RTP packets this report refers to. Ignore if |
| 330 | // we haven't seen this SSRC before. |
| 331 | std::map<uint32_t, uint32_t>::iterator seq_num_it = |
| 332 | extended_max_sequence_number_.find(block_it->sourceSSRC); |
| 333 | int number_of_packets = 0; |
| 334 | if (seq_num_it != extended_max_sequence_number_.end()) { |
| 335 | number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second; |
| 336 | } |
| 337 | fraction_lost_aggregate += number_of_packets * block_it->fractionLost; |
| 338 | total_number_of_packets += number_of_packets; |
| 339 | |
| 340 | extended_max_sequence_number_[block_it->sourceSSRC] = |
| 341 | block_it->extendedHighSeqNum; |
| 342 | } |
| 343 | int weighted_fraction_lost = 0; |
| 344 | if (total_number_of_packets > 0) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 345 | weighted_fraction_lost = |
| 346 | (fraction_lost_aggregate + total_number_of_packets / 2) / |
| 347 | total_number_of_packets; |
mflodman@webrtc.org | 0a7d4ee | 2015-02-17 12:57:14 +0000 | [diff] [blame] | 348 | } |
| 349 | owner_->OnIncomingFractionLoss(weighted_fraction_lost); |
minyue@webrtc.org | c1a40a7 | 2014-05-28 09:52:06 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | private: |
| 353 | Channel* owner_; |
mflodman@webrtc.org | 0a7d4ee | 2015-02-17 12:57:14 +0000 | [diff] [blame] | 354 | // Maps remote side ssrc to extended highest sequence number received. |
| 355 | std::map<uint32_t, uint32_t> extended_max_sequence_number_; |
minyue@webrtc.org | c1a40a7 | 2014-05-28 09:52:06 +0000 | [diff] [blame] | 356 | }; |
| 357 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 358 | int32_t Channel::SendData(FrameType frameType, |
| 359 | uint8_t payloadType, |
| 360 | uint32_t timeStamp, |
| 361 | const uint8_t* payloadData, |
| 362 | size_t payloadSize, |
| 363 | const RTPFragmentationHeader* fragmentation) { |
| 364 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
| 365 | "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u," |
| 366 | " payloadSize=%" PRIuS ", fragmentation=0x%x)", |
| 367 | frameType, payloadType, timeStamp, payloadSize, fragmentation); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 368 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 369 | if (_includeAudioLevelIndication) { |
| 370 | // Store current audio level in the RTP/RTCP module. |
| 371 | // The level will be used in combination with voice-activity state |
| 372 | // (frameType) to add an RTP header extension |
| 373 | _rtpRtcpModule->SetAudioLevel(rms_level_.RMS()); |
| 374 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 375 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 376 | // Push data from ACM to RTP/RTCP-module to deliver audio frame for |
| 377 | // packetization. |
| 378 | // This call will trigger Transport::SendPacket() from the RTP/RTCP module. |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 379 | if (!_rtpRtcpModule->SendOutgoingData( |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 380 | (FrameType&)frameType, payloadType, timeStamp, |
| 381 | // Leaving the time when this frame was |
| 382 | // received from the capture device as |
| 383 | // undefined for voice for now. |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 384 | -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 385 | _engineStatisticsPtr->SetLastError( |
| 386 | VE_RTP_RTCP_MODULE_ERROR, kTraceWarning, |
| 387 | "Channel::SendData() failed to send data to RTP/RTCP module"); |
| 388 | return -1; |
| 389 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 390 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 391 | _lastLocalTimeStamp = timeStamp; |
| 392 | _lastPayloadType = payloadType; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 393 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 394 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 395 | } |
| 396 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 397 | int32_t Channel::InFrameType(FrameType frame_type) { |
| 398 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 399 | "Channel::InFrameType(frame_type=%d)", frame_type); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 400 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 401 | rtc::CritScope cs(&_callbackCritSect); |
| 402 | _sendFrameType = (frame_type == kAudioFrameSpeech); |
| 403 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 404 | } |
| 405 | |
stefan | 1d8a506 | 2015-10-02 03:39:33 -0700 | [diff] [blame] | 406 | bool Channel::SendRtp(const uint8_t* data, |
| 407 | size_t len, |
| 408 | const PacketOptions& options) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 409 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
| 410 | "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 411 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 412 | rtc::CritScope cs(&_callbackCritSect); |
wu@webrtc.org | fb648da | 2013-10-18 21:10:51 +0000 | [diff] [blame] | 413 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 414 | if (_transportPtr == NULL) { |
| 415 | WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId), |
| 416 | "Channel::SendPacket() failed to send RTP packet due to" |
| 417 | " invalid transport object"); |
| 418 | return false; |
| 419 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 420 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 421 | uint8_t* bufferToSendPtr = (uint8_t*)data; |
| 422 | size_t bufferLength = len; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 423 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 424 | if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength, options)) { |
| 425 | std::string transport_name = |
| 426 | _externalTransport ? "external transport" : "WebRtc sockets"; |
| 427 | WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId), |
| 428 | "Channel::SendPacket() RTP transmission using %s failed", |
| 429 | transport_name.c_str()); |
| 430 | return false; |
| 431 | } |
| 432 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 433 | } |
| 434 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 435 | bool Channel::SendRtcp(const uint8_t* data, size_t len) { |
| 436 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
| 437 | "Channel::SendRtcp(len=%" PRIuS ")", len); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 438 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 439 | rtc::CritScope cs(&_callbackCritSect); |
| 440 | if (_transportPtr == NULL) { |
| 441 | WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId), |
| 442 | "Channel::SendRtcp() failed to send RTCP packet" |
| 443 | " due to invalid transport object"); |
| 444 | return false; |
| 445 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 446 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 447 | uint8_t* bufferToSendPtr = (uint8_t*)data; |
| 448 | size_t bufferLength = len; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 449 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 450 | int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength); |
| 451 | if (n < 0) { |
| 452 | std::string transport_name = |
| 453 | _externalTransport ? "external transport" : "WebRtc sockets"; |
| 454 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 455 | "Channel::SendRtcp() transmission using %s failed", |
| 456 | transport_name.c_str()); |
| 457 | return false; |
| 458 | } |
| 459 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 460 | } |
| 461 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 462 | void Channel::OnIncomingSSRCChanged(uint32_t ssrc) { |
| 463 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 464 | "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 465 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 466 | // Update ssrc so that NTP for AV sync can be updated. |
| 467 | _rtpRtcpModule->SetRemoteSSRC(ssrc); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Peter Boström | ac547a6 | 2015-09-17 23:03:57 +0200 | [diff] [blame] | 470 | void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) { |
| 471 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 472 | "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC, |
| 473 | added); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Peter Boström | ac547a6 | 2015-09-17 23:03:57 +0200 | [diff] [blame] | 476 | int32_t Channel::OnInitializeDecoder( |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 477 | int8_t payloadType, |
leozwang@webrtc.org | 813e4b0 | 2012-03-01 18:34:25 +0000 | [diff] [blame] | 478 | const char payloadName[RTP_PAYLOAD_NAME_SIZE], |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 479 | int frequency, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 480 | size_t channels, |
Peter Boström | ac547a6 | 2015-09-17 23:03:57 +0200 | [diff] [blame] | 481 | uint32_t rate) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 482 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 483 | "Channel::OnInitializeDecoder(payloadType=%d, " |
| 484 | "payloadName=%s, frequency=%u, channels=%" PRIuS ", rate=%u)", |
| 485 | payloadType, payloadName, frequency, channels, rate); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 486 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 487 | CodecInst receiveCodec = {0}; |
| 488 | CodecInst dummyCodec = {0}; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 489 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 490 | receiveCodec.pltype = payloadType; |
| 491 | receiveCodec.plfreq = frequency; |
| 492 | receiveCodec.channels = channels; |
| 493 | receiveCodec.rate = rate; |
| 494 | strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1); |
andrew@webrtc.org | ae1a58b | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 495 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 496 | audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels); |
| 497 | receiveCodec.pacsize = dummyCodec.pacsize; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 498 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 499 | // Register the new codec to the ACM |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 500 | if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, receiveCodec)) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 501 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 502 | "Channel::OnInitializeDecoder() invalid codec (" |
| 503 | "pt=%d, name=%s) received - 1", |
| 504 | payloadType, payloadName); |
| 505 | _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR); |
| 506 | return -1; |
| 507 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 508 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 509 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 510 | } |
| 511 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 512 | int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData, |
| 513 | size_t payloadSize, |
| 514 | const WebRtcRTPHeader* rtpHeader) { |
| 515 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
| 516 | "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS |
| 517 | "," |
| 518 | " payloadType=%u, audioChannel=%" PRIuS ")", |
| 519 | payloadSize, rtpHeader->header.payloadType, |
| 520 | rtpHeader->type.Audio.channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 521 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 522 | if (!channel_state_.Get().playing) { |
| 523 | // Avoid inserting into NetEQ when we are not playing. Count the |
| 524 | // packet as discarded. |
| 525 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
| 526 | "received packet is discarded since playing is not" |
| 527 | " activated"); |
| 528 | _numberOfDiscardedPackets++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 529 | return 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | // Push the incoming payload (parsed and ready for decoding) into the ACM |
| 533 | if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) != |
| 534 | 0) { |
| 535 | _engineStatisticsPtr->SetLastError( |
| 536 | VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning, |
| 537 | "Channel::OnReceivedPayloadData() unable to push data to the ACM"); |
| 538 | return -1; |
| 539 | } |
| 540 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 541 | int64_t round_trip_time = 0; |
| 542 | _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time, NULL, NULL, |
| 543 | NULL); |
| 544 | |
| 545 | std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time); |
| 546 | if (!nack_list.empty()) { |
| 547 | // Can't use nack_list.data() since it's not supported by all |
| 548 | // compilers. |
| 549 | ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size())); |
| 550 | } |
| 551 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 552 | } |
| 553 | |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 554 | bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 555 | size_t rtp_packet_length) { |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 556 | RTPHeader header; |
| 557 | if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) { |
| 558 | WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId, |
| 559 | "IncomingPacket invalid RTP header"); |
| 560 | return false; |
| 561 | } |
| 562 | header.payload_type_frequency = |
| 563 | rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType); |
| 564 | if (header.payload_type_frequency < 0) |
| 565 | return false; |
| 566 | return ReceivePacket(rtp_packet, rtp_packet_length, header, false); |
| 567 | } |
| 568 | |
henrik.lundin | 42dda50 | 2016-05-18 05:36:01 -0700 | [diff] [blame] | 569 | MixerParticipant::AudioFrameInfo Channel::GetAudioFrameWithMuted( |
| 570 | int32_t id, |
| 571 | AudioFrame* audioFrame) { |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 572 | unsigned int ssrc; |
| 573 | RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0); |
| 574 | event_log_proxy_->LogAudioPlayout(ssrc); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 575 | // Get 10ms raw PCM data from the ACM (mixer limits output frequency) |
henrik.lundin | d4ccb00 | 2016-05-17 12:21:55 -0700 | [diff] [blame] | 576 | bool muted; |
| 577 | if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_, audioFrame, |
| 578 | &muted) == -1) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 579 | WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId), |
| 580 | "Channel::GetAudioFrame() PlayoutData10Ms() failed!"); |
| 581 | // In all likelihood, the audio in this frame is garbage. We return an |
| 582 | // error so that the audio mixer module doesn't add it to the mix. As |
| 583 | // a result, it won't be played out and the actions skipped here are |
| 584 | // irrelevant. |
henrik.lundin | 42dda50 | 2016-05-18 05:36:01 -0700 | [diff] [blame] | 585 | return MixerParticipant::AudioFrameInfo::kError; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 586 | } |
henrik.lundin | a89ab96 | 2016-05-18 08:52:45 -0700 | [diff] [blame] | 587 | |
| 588 | if (muted) { |
| 589 | // TODO(henrik.lundin): We should be able to do better than this. But we |
| 590 | // will have to go through all the cases below where the audio samples may |
| 591 | // be used, and handle the muted case in some way. |
| 592 | audioFrame->Mute(); |
| 593 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 594 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 595 | // Convert module ID to internal VoE channel ID |
| 596 | audioFrame->id_ = VoEChannelId(audioFrame->id_); |
| 597 | // Store speech type for dead-or-alive detection |
| 598 | _outputSpeechType = audioFrame->speech_type_; |
| 599 | |
| 600 | ChannelState::State state = channel_state_.Get(); |
| 601 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 602 | { |
| 603 | // Pass the audio buffers to an optional sink callback, before applying |
| 604 | // scaling/panning, as that applies to the mix operation. |
| 605 | // External recipients of the audio (e.g. via AudioTrack), will do their |
| 606 | // own mixing/dynamic processing. |
| 607 | rtc::CritScope cs(&_callbackCritSect); |
| 608 | if (audio_sink_) { |
| 609 | AudioSinkInterface::Data data( |
| 610 | &audioFrame->data_[0], audioFrame->samples_per_channel_, |
| 611 | audioFrame->sample_rate_hz_, audioFrame->num_channels_, |
| 612 | audioFrame->timestamp_); |
| 613 | audio_sink_->OnData(data); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | float output_gain = 1.0f; |
| 618 | float left_pan = 1.0f; |
| 619 | float right_pan = 1.0f; |
| 620 | { |
| 621 | rtc::CritScope cs(&volume_settings_critsect_); |
| 622 | output_gain = _outputGain; |
| 623 | left_pan = _panLeft; |
| 624 | right_pan = _panRight; |
| 625 | } |
| 626 | |
| 627 | // Output volume scaling |
| 628 | if (output_gain < 0.99f || output_gain > 1.01f) { |
| 629 | AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame); |
| 630 | } |
| 631 | |
| 632 | // Scale left and/or right channel(s) if stereo and master balance is |
| 633 | // active |
| 634 | |
| 635 | if (left_pan != 1.0f || right_pan != 1.0f) { |
| 636 | if (audioFrame->num_channels_ == 1) { |
| 637 | // Emulate stereo mode since panning is active. |
| 638 | // The mono signal is copied to both left and right channels here. |
| 639 | AudioFrameOperations::MonoToStereo(audioFrame); |
| 640 | } |
| 641 | // For true stereo mode (when we are receiving a stereo signal), no |
| 642 | // action is needed. |
| 643 | |
| 644 | // Do the panning operation (the audio frame contains stereo at this |
| 645 | // stage) |
| 646 | AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame); |
| 647 | } |
| 648 | |
| 649 | // Mix decoded PCM output with file if file mixing is enabled |
| 650 | if (state.output_file_playing) { |
| 651 | MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_); |
henrik.lundin | a89ab96 | 2016-05-18 08:52:45 -0700 | [diff] [blame] | 652 | muted = false; // We may have added non-zero samples. |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | // External media |
| 656 | if (_outputExternalMedia) { |
| 657 | rtc::CritScope cs(&_callbackCritSect); |
| 658 | const bool isStereo = (audioFrame->num_channels_ == 2); |
| 659 | if (_outputExternalMediaCallbackPtr) { |
| 660 | _outputExternalMediaCallbackPtr->Process( |
| 661 | _channelId, kPlaybackPerChannel, (int16_t*)audioFrame->data_, |
| 662 | audioFrame->samples_per_channel_, audioFrame->sample_rate_hz_, |
| 663 | isStereo); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | // Record playout if enabled |
| 668 | { |
| 669 | rtc::CritScope cs(&_fileCritSect); |
| 670 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 671 | if (_outputFileRecording && output_file_recorder_) { |
| 672 | output_file_recorder_->RecordAudioToFile(*audioFrame); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
| 676 | // Measure audio level (0-9) |
henrik.lundin | a89ab96 | 2016-05-18 08:52:45 -0700 | [diff] [blame] | 677 | // TODO(henrik.lundin) Use the |muted| information here too. |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 678 | _outputAudioLevel.ComputeLevel(*audioFrame); |
| 679 | |
| 680 | if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) { |
| 681 | // The first frame with a valid rtp timestamp. |
| 682 | capture_start_rtp_time_stamp_ = audioFrame->timestamp_; |
| 683 | } |
| 684 | |
| 685 | if (capture_start_rtp_time_stamp_ >= 0) { |
| 686 | // audioFrame.timestamp_ should be valid from now on. |
| 687 | |
| 688 | // Compute elapsed time. |
| 689 | int64_t unwrap_timestamp = |
| 690 | rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_); |
| 691 | audioFrame->elapsed_time_ms_ = |
| 692 | (unwrap_timestamp - capture_start_rtp_time_stamp_) / |
| 693 | (GetPlayoutFrequency() / 1000); |
| 694 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 695 | { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 696 | rtc::CritScope lock(&ts_stats_lock_); |
| 697 | // Compute ntp time. |
| 698 | audioFrame->ntp_time_ms_ = |
| 699 | ntp_estimator_.Estimate(audioFrame->timestamp_); |
| 700 | // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received. |
| 701 | if (audioFrame->ntp_time_ms_ > 0) { |
| 702 | // Compute |capture_start_ntp_time_ms_| so that |
| 703 | // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_| |
| 704 | capture_start_ntp_time_ms_ = |
| 705 | audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_; |
andrew@webrtc.org | 60730cf | 2014-01-07 17:45:09 +0000 | [diff] [blame] | 706 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 707 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 708 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 709 | |
henrik.lundin | 42dda50 | 2016-05-18 05:36:01 -0700 | [diff] [blame] | 710 | return muted ? MixerParticipant::AudioFrameInfo::kMuted |
| 711 | : MixerParticipant::AudioFrameInfo::kNormal; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 712 | } |
| 713 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 714 | int32_t Channel::NeededFrequency(int32_t id) const { |
| 715 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
| 716 | "Channel::NeededFrequency(id=%d)", id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 717 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 718 | int highestNeeded = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 719 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 720 | // Determine highest needed receive frequency |
| 721 | int32_t receiveFrequency = audio_coding_->ReceiveFrequency(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 722 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 723 | // Return the bigger of playout and receive frequency in the ACM. |
| 724 | if (audio_coding_->PlayoutFrequency() > receiveFrequency) { |
| 725 | highestNeeded = audio_coding_->PlayoutFrequency(); |
| 726 | } else { |
| 727 | highestNeeded = receiveFrequency; |
| 728 | } |
| 729 | |
| 730 | // Special case, if we're playing a file on the playout side |
| 731 | // we take that frequency into consideration as well |
| 732 | // This is not needed on sending side, since the codec will |
| 733 | // limit the spectrum anyway. |
| 734 | if (channel_state_.Get().output_file_playing) { |
| 735 | rtc::CritScope cs(&_fileCritSect); |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 736 | if (output_file_player_) { |
| 737 | if (output_file_player_->Frequency() > highestNeeded) { |
| 738 | highestNeeded = output_file_player_->Frequency(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 739 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 740 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 741 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 742 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 743 | return (highestNeeded); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 744 | } |
| 745 | |
ossu | 5f7cfa5 | 2016-05-30 08:11:28 -0700 | [diff] [blame] | 746 | int32_t Channel::CreateChannel( |
| 747 | Channel*& channel, |
| 748 | int32_t channelId, |
| 749 | uint32_t instanceId, |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 750 | const VoEBase::ChannelConfig& config) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 751 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId), |
| 752 | "Channel::CreateChannel(channelId=%d, instanceId=%d)", channelId, |
| 753 | instanceId); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 754 | |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 755 | channel = new Channel(channelId, instanceId, config); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 756 | if (channel == NULL) { |
| 757 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId, channelId), |
| 758 | "Channel::CreateChannel() unable to allocate memory for" |
| 759 | " channel"); |
| 760 | return -1; |
| 761 | } |
| 762 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 763 | } |
| 764 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 765 | void Channel::PlayNotification(int32_t id, uint32_t durationMs) { |
| 766 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
| 767 | "Channel::PlayNotification(id=%d, durationMs=%d)", id, |
| 768 | durationMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 769 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 770 | // Not implement yet |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 771 | } |
| 772 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 773 | void Channel::RecordNotification(int32_t id, uint32_t durationMs) { |
| 774 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
| 775 | "Channel::RecordNotification(id=%d, durationMs=%d)", id, |
| 776 | durationMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 777 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 778 | // Not implement yet |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 779 | } |
| 780 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 781 | void Channel::PlayFileEnded(int32_t id) { |
| 782 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
| 783 | "Channel::PlayFileEnded(id=%d)", id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 784 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 785 | if (id == _inputFilePlayerId) { |
| 786 | channel_state_.SetInputFilePlaying(false); |
| 787 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 788 | "Channel::PlayFileEnded() => input file player module is" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 789 | " shutdown"); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 790 | } else if (id == _outputFilePlayerId) { |
| 791 | channel_state_.SetOutputFilePlaying(false); |
| 792 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 793 | "Channel::PlayFileEnded() => output file player module is" |
| 794 | " shutdown"); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | void Channel::RecordFileEnded(int32_t id) { |
| 799 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
| 800 | "Channel::RecordFileEnded(id=%d)", id); |
| 801 | |
| 802 | assert(id == _outputFileRecorderId); |
| 803 | |
| 804 | rtc::CritScope cs(&_fileCritSect); |
| 805 | |
| 806 | _outputFileRecording = false; |
| 807 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 808 | "Channel::RecordFileEnded() => output file recorder module is" |
| 809 | " shutdown"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 810 | } |
| 811 | |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 812 | Channel::Channel(int32_t channelId, |
minyue@webrtc.org | e509f94 | 2013-09-12 17:03:00 +0000 | [diff] [blame] | 813 | uint32_t instanceId, |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 814 | const VoEBase::ChannelConfig& config) |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 815 | : _instanceId(instanceId), |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 816 | _channelId(channelId), |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 817 | event_log_proxy_(new RtcEventLogProxy()), |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 818 | rtp_header_parser_(RtpHeaderParser::Create()), |
| 819 | rtp_payload_registry_( |
| 820 | new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))), |
| 821 | rtp_receive_statistics_( |
| 822 | ReceiveStatistics::Create(Clock::GetRealTimeClock())), |
| 823 | rtp_receiver_( |
| 824 | RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(), |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 825 | this, |
| 826 | this, |
| 827 | rtp_payload_registry_.get())), |
danilchap | 799a9d0 | 2016-09-22 03:36:27 -0700 | [diff] [blame] | 828 | telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()), |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 829 | _outputAudioLevel(), |
| 830 | _externalTransport(false), |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 831 | // Avoid conflict with other channels by adding 1024 - 1026, |
| 832 | // won't use as much as 1024 channels. |
| 833 | _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024), |
| 834 | _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025), |
| 835 | _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026), |
| 836 | _outputFileRecording(false), |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 837 | _outputExternalMedia(false), |
| 838 | _inputExternalMediaCallbackPtr(NULL), |
| 839 | _outputExternalMediaCallbackPtr(NULL), |
| 840 | _timeStamp(0), // This is just an offset, RTP module will add it's own |
| 841 | // random offset |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 842 | ntp_estimator_(Clock::GetRealTimeClock()), |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 843 | playout_timestamp_rtp_(0), |
| 844 | playout_timestamp_rtcp_(0), |
| 845 | playout_delay_ms_(0), |
| 846 | _numberOfDiscardedPackets(0), |
| 847 | send_sequence_number_(0), |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 848 | rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()), |
| 849 | capture_start_rtp_time_stamp_(-1), |
| 850 | capture_start_ntp_time_ms_(-1), |
| 851 | _engineStatisticsPtr(NULL), |
| 852 | _outputMixerPtr(NULL), |
| 853 | _transmitMixerPtr(NULL), |
| 854 | _moduleProcessThreadPtr(NULL), |
| 855 | _audioDeviceModulePtr(NULL), |
| 856 | _voiceEngineObserverPtr(NULL), |
| 857 | _callbackCritSectPtr(NULL), |
| 858 | _transportPtr(NULL), |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 859 | _sendFrameType(0), |
| 860 | _externalMixing(false), |
| 861 | _mixFileWithMicrophone(false), |
solenberg | 1c2af8e | 2016-03-24 10:36:00 -0700 | [diff] [blame] | 862 | input_mute_(false), |
| 863 | previous_frame_muted_(false), |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 864 | _panLeft(1.0f), |
| 865 | _panRight(1.0f), |
| 866 | _outputGain(1.0f), |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 867 | _lastLocalTimeStamp(0), |
| 868 | _lastPayloadType(0), |
| 869 | _includeAudioLevelIndication(false), |
| 870 | _outputSpeechType(AudioFrame::kNormalSpeech), |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 871 | restored_packet_in_use_(false), |
| 872 | rtcp_observer_(new VoERtcpObserver(this)), |
| 873 | network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock())), |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 874 | associate_send_channel_(ChannelOwner(nullptr)), |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 875 | pacing_enabled_(config.enable_voice_pacing), |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 876 | feedback_observer_proxy_(new TransportFeedbackProxy()), |
| 877 | seq_num_allocator_proxy_(new TransportSequenceNumberProxy()), |
ossu | 29b1a8d | 2016-06-13 07:34:51 -0700 | [diff] [blame] | 878 | rtp_packet_sender_proxy_(new RtpPacketSenderProxy()), |
Erik Språng | 737336d | 2016-07-29 12:59:36 +0200 | [diff] [blame] | 879 | retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(), |
| 880 | kMaxRetransmissionWindowMs)), |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 881 | decoder_factory_(config.acm_config.decoder_factory) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 882 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId), |
| 883 | "Channel::Channel() - ctor"); |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 884 | AudioCodingModule::Config acm_config(config.acm_config); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 885 | acm_config.id = VoEModuleId(instanceId, channelId); |
henrik.lundin | a89ab96 | 2016-05-18 08:52:45 -0700 | [diff] [blame] | 886 | acm_config.neteq_config.enable_muted_state = true; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 887 | audio_coding_.reset(AudioCodingModule::Create(acm_config)); |
Henrik Lundin | 64dad83 | 2015-05-11 12:44:23 +0200 | [diff] [blame] | 888 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 889 | _outputAudioLevel.Clear(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 890 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 891 | RtpRtcp::Configuration configuration; |
| 892 | configuration.audio = true; |
| 893 | configuration.outgoing_transport = this; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 894 | configuration.receive_statistics = rtp_receive_statistics_.get(); |
| 895 | configuration.bandwidth_callback = rtcp_observer_.get(); |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 896 | if (pacing_enabled_) { |
| 897 | configuration.paced_sender = rtp_packet_sender_proxy_.get(); |
| 898 | configuration.transport_sequence_number_allocator = |
| 899 | seq_num_allocator_proxy_.get(); |
| 900 | configuration.transport_feedback_callback = feedback_observer_proxy_.get(); |
| 901 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 902 | configuration.event_log = &(*event_log_proxy_); |
Erik Språng | 737336d | 2016-07-29 12:59:36 +0200 | [diff] [blame] | 903 | configuration.retransmission_rate_limiter = |
| 904 | retransmission_rate_limiter_.get(); |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 905 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 906 | _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration)); |
Peter Boström | 3dd5d1d | 2016-02-25 16:56:48 +0100 | [diff] [blame] | 907 | _rtpRtcpModule->SetSendingMediaStatus(false); |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 908 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 909 | statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC())); |
| 910 | rtp_receive_statistics_->RegisterRtcpStatisticsCallback( |
| 911 | statistics_proxy_.get()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 912 | } |
| 913 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 914 | Channel::~Channel() { |
| 915 | rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL); |
| 916 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId, _channelId), |
| 917 | "Channel::~Channel() - dtor"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 918 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 919 | if (_outputExternalMedia) { |
| 920 | DeRegisterExternalMediaProcessing(kPlaybackPerChannel); |
| 921 | } |
| 922 | if (channel_state_.Get().input_external_media) { |
| 923 | DeRegisterExternalMediaProcessing(kRecordingPerChannel); |
| 924 | } |
| 925 | StopSend(); |
| 926 | StopPlayout(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 927 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 928 | { |
| 929 | rtc::CritScope cs(&_fileCritSect); |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 930 | if (input_file_player_) { |
| 931 | input_file_player_->RegisterModuleFileCallback(NULL); |
| 932 | input_file_player_->StopPlayingFile(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 933 | } |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 934 | if (output_file_player_) { |
| 935 | output_file_player_->RegisterModuleFileCallback(NULL); |
| 936 | output_file_player_->StopPlayingFile(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 937 | } |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 938 | if (output_file_recorder_) { |
| 939 | output_file_recorder_->RegisterModuleFileCallback(NULL); |
| 940 | output_file_recorder_->StopRecording(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 941 | } |
| 942 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 943 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 944 | // The order to safely shutdown modules in a channel is: |
| 945 | // 1. De-register callbacks in modules |
| 946 | // 2. De-register modules in process thread |
| 947 | // 3. Destroy modules |
| 948 | if (audio_coding_->RegisterTransportCallback(NULL) == -1) { |
| 949 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 950 | "~Channel() failed to de-register transport callback" |
| 951 | " (Audio coding module)"); |
| 952 | } |
| 953 | if (audio_coding_->RegisterVADCallback(NULL) == -1) { |
| 954 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 955 | "~Channel() failed to de-register VAD callback" |
| 956 | " (Audio coding module)"); |
| 957 | } |
| 958 | // De-register modules in process thread |
| 959 | _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 960 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 961 | // End of modules shutdown |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 962 | } |
| 963 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 964 | int32_t Channel::Init() { |
| 965 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 966 | "Channel::Init()"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 967 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 968 | channel_state_.Reset(); |
henrika@webrtc.org | 944cbeb | 2014-03-18 10:32:33 +0000 | [diff] [blame] | 969 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 970 | // --- Initial sanity |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 971 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 972 | if ((_engineStatisticsPtr == NULL) || (_moduleProcessThreadPtr == NULL)) { |
| 973 | WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId), |
| 974 | "Channel::Init() must call SetEngineInformation() first"); |
| 975 | return -1; |
| 976 | } |
| 977 | |
| 978 | // --- Add modules to process thread (for periodic schedulation) |
| 979 | |
| 980 | _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get()); |
| 981 | |
| 982 | // --- ACM initialization |
| 983 | |
| 984 | if (audio_coding_->InitializeReceiver() == -1) { |
| 985 | _engineStatisticsPtr->SetLastError( |
| 986 | VE_AUDIO_CODING_MODULE_ERROR, kTraceError, |
| 987 | "Channel::Init() unable to initialize the ACM - 1"); |
| 988 | return -1; |
| 989 | } |
| 990 | |
| 991 | // --- RTP/RTCP module initialization |
| 992 | |
| 993 | // Ensure that RTCP is enabled by default for the created channel. |
| 994 | // Note that, the module will keep generating RTCP until it is explicitly |
| 995 | // disabled by the user. |
| 996 | // After StopListen (when no sockets exists), RTCP packets will no longer |
| 997 | // be transmitted since the Transport object will then be invalid. |
danilchap | 799a9d0 | 2016-09-22 03:36:27 -0700 | [diff] [blame] | 998 | telephone_event_handler_->SetTelephoneEventForwardToDecoder(true); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 999 | // RTCP is enabled by default. |
| 1000 | _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound); |
| 1001 | // --- Register all permanent callbacks |
| 1002 | const bool fail = (audio_coding_->RegisterTransportCallback(this) == -1) || |
| 1003 | (audio_coding_->RegisterVADCallback(this) == -1); |
| 1004 | |
| 1005 | if (fail) { |
| 1006 | _engineStatisticsPtr->SetLastError( |
| 1007 | VE_CANNOT_INIT_CHANNEL, kTraceError, |
| 1008 | "Channel::Init() callbacks not registered"); |
| 1009 | return -1; |
| 1010 | } |
| 1011 | |
| 1012 | // --- Register all supported codecs to the receiving side of the |
| 1013 | // RTP/RTCP module |
| 1014 | |
| 1015 | CodecInst codec; |
| 1016 | const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs(); |
| 1017 | |
| 1018 | for (int idx = 0; idx < nSupportedCodecs; idx++) { |
| 1019 | // Open up the RTP/RTCP receiver for all supported codecs |
| 1020 | if ((audio_coding_->Codec(idx, &codec) == -1) || |
| 1021 | (rtp_receiver_->RegisterReceivePayload( |
| 1022 | codec.plname, codec.pltype, codec.plfreq, codec.channels, |
| 1023 | (codec.rate < 0) ? 0 : codec.rate) == -1)) { |
| 1024 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1025 | "Channel::Init() unable to register %s " |
| 1026 | "(%d/%d/%" PRIuS "/%d) to RTP/RTCP receiver", |
| 1027 | codec.plname, codec.pltype, codec.plfreq, codec.channels, |
| 1028 | codec.rate); |
| 1029 | } else { |
| 1030 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1031 | "Channel::Init() %s (%d/%d/%" PRIuS |
| 1032 | "/%d) has been " |
| 1033 | "added to the RTP/RTCP receiver", |
| 1034 | codec.plname, codec.pltype, codec.plfreq, codec.channels, |
| 1035 | codec.rate); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1038 | // Ensure that PCMU is used as default codec on the sending side |
| 1039 | if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1)) { |
| 1040 | SetSendCodec(codec); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1043 | // Register default PT for outband 'telephone-event' |
| 1044 | if (!STR_CASE_CMP(codec.plname, "telephone-event")) { |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 1045 | if (_rtpRtcpModule->RegisterSendPayload(codec) == -1 || |
| 1046 | !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1047 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1048 | "Channel::Init() failed to register outband " |
| 1049 | "'telephone-event' (%d/%d) correctly", |
| 1050 | codec.pltype, codec.plfreq); |
| 1051 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1054 | if (!STR_CASE_CMP(codec.plname, "CN")) { |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 1055 | if (!codec_manager_.RegisterEncoder(codec) || |
| 1056 | !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get()) || |
| 1057 | !RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec) || |
| 1058 | _rtpRtcpModule->RegisterSendPayload(codec) == -1) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1059 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1060 | "Channel::Init() failed to register CN (%d/%d) " |
| 1061 | "correctly - 1", |
| 1062 | codec.pltype, codec.plfreq); |
| 1063 | } |
| 1064 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1065 | } |
pwestin@webrtc.org | 684f057 | 2013-03-13 23:20:57 +0000 | [diff] [blame] | 1066 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1067 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1070 | int32_t Channel::SetEngineInformation(Statistics& engineStatistics, |
| 1071 | OutputMixer& outputMixer, |
| 1072 | voe::TransmitMixer& transmitMixer, |
| 1073 | ProcessThread& moduleProcessThread, |
| 1074 | AudioDeviceModule& audioDeviceModule, |
| 1075 | VoiceEngineObserver* voiceEngineObserver, |
| 1076 | rtc::CriticalSection* callbackCritSect) { |
| 1077 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1078 | "Channel::SetEngineInformation()"); |
| 1079 | _engineStatisticsPtr = &engineStatistics; |
| 1080 | _outputMixerPtr = &outputMixer; |
| 1081 | _transmitMixerPtr = &transmitMixer, |
| 1082 | _moduleProcessThreadPtr = &moduleProcessThread; |
| 1083 | _audioDeviceModulePtr = &audioDeviceModule; |
| 1084 | _voiceEngineObserverPtr = voiceEngineObserver; |
| 1085 | _callbackCritSectPtr = callbackCritSect; |
| 1086 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1089 | int32_t Channel::UpdateLocalTimeStamp() { |
| 1090 | _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_); |
| 1091 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
kwiberg | b7f89d6 | 2016-02-17 10:04:18 -0800 | [diff] [blame] | 1094 | void Channel::SetSink(std::unique_ptr<AudioSinkInterface> sink) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 1095 | rtc::CritScope cs(&_callbackCritSect); |
deadbeef | 2d110be | 2016-01-13 12:00:26 -0800 | [diff] [blame] | 1096 | audio_sink_ = std::move(sink); |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 1097 | } |
| 1098 | |
ossu | 29b1a8d | 2016-06-13 07:34:51 -0700 | [diff] [blame] | 1099 | const rtc::scoped_refptr<AudioDecoderFactory>& |
| 1100 | Channel::GetAudioDecoderFactory() const { |
| 1101 | return decoder_factory_; |
| 1102 | } |
| 1103 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1104 | int32_t Channel::StartPlayout() { |
| 1105 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1106 | "Channel::StartPlayout()"); |
| 1107 | if (channel_state_.Get().playing) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1108 | return 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | if (!_externalMixing) { |
| 1112 | // Add participant as candidates for mixing. |
| 1113 | if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0) { |
| 1114 | _engineStatisticsPtr->SetLastError( |
| 1115 | VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError, |
| 1116 | "StartPlayout() failed to add participant to mixer"); |
| 1117 | return -1; |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | channel_state_.SetPlaying(true); |
| 1122 | if (RegisterFilePlayingToMixer() != 0) |
| 1123 | return -1; |
| 1124 | |
| 1125 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1128 | int32_t Channel::StopPlayout() { |
| 1129 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1130 | "Channel::StopPlayout()"); |
| 1131 | if (!channel_state_.Get().playing) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1132 | return 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | if (!_externalMixing) { |
| 1136 | // Remove participant as candidates for mixing |
| 1137 | if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0) { |
| 1138 | _engineStatisticsPtr->SetLastError( |
| 1139 | VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError, |
| 1140 | "StopPlayout() failed to remove participant from mixer"); |
| 1141 | return -1; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | channel_state_.SetPlaying(false); |
| 1146 | _outputAudioLevel.Clear(); |
| 1147 | |
| 1148 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1151 | int32_t Channel::StartSend() { |
| 1152 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1153 | "Channel::StartSend()"); |
| 1154 | // Resume the previous sequence number which was reset by StopSend(). |
| 1155 | // This needs to be done before |sending| is set to true. |
| 1156 | if (send_sequence_number_) |
| 1157 | SetInitSequenceNumber(send_sequence_number_); |
xians@webrtc.org | 09e8c47 | 2013-07-31 16:30:19 +0000 | [diff] [blame] | 1158 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1159 | if (channel_state_.Get().sending) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1160 | return 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1161 | } |
| 1162 | channel_state_.SetSending(true); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1163 | |
Peter Boström | 3dd5d1d | 2016-02-25 16:56:48 +0100 | [diff] [blame] | 1164 | _rtpRtcpModule->SetSendingMediaStatus(true); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1165 | if (_rtpRtcpModule->SetSendingStatus(true) != 0) { |
| 1166 | _engineStatisticsPtr->SetLastError( |
| 1167 | VE_RTP_RTCP_MODULE_ERROR, kTraceError, |
| 1168 | "StartSend() RTP/RTCP failed to start sending"); |
Peter Boström | 3dd5d1d | 2016-02-25 16:56:48 +0100 | [diff] [blame] | 1169 | _rtpRtcpModule->SetSendingMediaStatus(false); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1170 | rtc::CritScope cs(&_callbackCritSect); |
henrika@webrtc.org | 944cbeb | 2014-03-18 10:32:33 +0000 | [diff] [blame] | 1171 | channel_state_.SetSending(false); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1172 | return -1; |
| 1173 | } |
xians@webrtc.org | e07247a | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 1174 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1175 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1178 | int32_t Channel::StopSend() { |
| 1179 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1180 | "Channel::StopSend()"); |
| 1181 | if (!channel_state_.Get().sending) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1182 | return 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1183 | } |
| 1184 | channel_state_.SetSending(false); |
| 1185 | |
| 1186 | // Store the sequence number to be able to pick up the same sequence for |
| 1187 | // the next StartSend(). This is needed for restarting device, otherwise |
| 1188 | // it might cause libSRTP to complain about packets being replayed. |
| 1189 | // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring |
| 1190 | // CL is landed. See issue |
| 1191 | // https://code.google.com/p/webrtc/issues/detail?id=2111 . |
| 1192 | send_sequence_number_ = _rtpRtcpModule->SequenceNumber(); |
| 1193 | |
| 1194 | // Reset sending SSRC and sequence number and triggers direct transmission |
| 1195 | // of RTCP BYE |
| 1196 | if (_rtpRtcpModule->SetSendingStatus(false) == -1) { |
| 1197 | _engineStatisticsPtr->SetLastError( |
| 1198 | VE_RTP_RTCP_MODULE_ERROR, kTraceWarning, |
| 1199 | "StartSend() RTP/RTCP failed to stop sending"); |
| 1200 | } |
Peter Boström | 3dd5d1d | 2016-02-25 16:56:48 +0100 | [diff] [blame] | 1201 | _rtpRtcpModule->SetSendingMediaStatus(false); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1202 | |
| 1203 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1206 | int32_t Channel::StartReceiving() { |
| 1207 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1208 | "Channel::StartReceiving()"); |
| 1209 | if (channel_state_.Get().receiving) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1210 | return 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1211 | } |
| 1212 | channel_state_.SetReceiving(true); |
| 1213 | _numberOfDiscardedPackets = 0; |
| 1214 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1217 | int32_t Channel::StopReceiving() { |
| 1218 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1219 | "Channel::StopReceiving()"); |
| 1220 | if (!channel_state_.Get().receiving) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1221 | return 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | channel_state_.SetReceiving(false); |
| 1225 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1228 | int32_t Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer) { |
| 1229 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1230 | "Channel::RegisterVoiceEngineObserver()"); |
| 1231 | rtc::CritScope cs(&_callbackCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1232 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1233 | if (_voiceEngineObserverPtr) { |
| 1234 | _engineStatisticsPtr->SetLastError( |
| 1235 | VE_INVALID_OPERATION, kTraceError, |
| 1236 | "RegisterVoiceEngineObserver() observer already enabled"); |
| 1237 | return -1; |
| 1238 | } |
| 1239 | _voiceEngineObserverPtr = &observer; |
| 1240 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1243 | int32_t Channel::DeRegisterVoiceEngineObserver() { |
| 1244 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1245 | "Channel::DeRegisterVoiceEngineObserver()"); |
| 1246 | rtc::CritScope cs(&_callbackCritSect); |
| 1247 | |
| 1248 | if (!_voiceEngineObserverPtr) { |
| 1249 | _engineStatisticsPtr->SetLastError( |
| 1250 | VE_INVALID_OPERATION, kTraceWarning, |
| 1251 | "DeRegisterVoiceEngineObserver() observer already disabled"); |
| 1252 | return 0; |
| 1253 | } |
| 1254 | _voiceEngineObserverPtr = NULL; |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
| 1258 | int32_t Channel::GetSendCodec(CodecInst& codec) { |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 1259 | auto send_codec = codec_manager_.GetCodecInst(); |
kwiberg | 1fd4a4a | 2015-11-03 11:20:50 -0800 | [diff] [blame] | 1260 | if (send_codec) { |
| 1261 | codec = *send_codec; |
| 1262 | return 0; |
| 1263 | } |
| 1264 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1267 | int32_t Channel::GetRecCodec(CodecInst& codec) { |
| 1268 | return (audio_coding_->ReceiveCodec(&codec)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1271 | int32_t Channel::SetSendCodec(const CodecInst& codec) { |
| 1272 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1273 | "Channel::SetSendCodec()"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1274 | |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 1275 | if (!codec_manager_.RegisterEncoder(codec) || |
| 1276 | !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1277 | WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1278 | "SetSendCodec() failed to register codec to ACM"); |
| 1279 | return -1; |
| 1280 | } |
| 1281 | |
| 1282 | if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) { |
| 1283 | _rtpRtcpModule->DeRegisterSendPayload(codec.pltype); |
| 1284 | if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) { |
| 1285 | WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1286 | "SetSendCodec() failed to register codec to" |
| 1287 | " RTP/RTCP module"); |
| 1288 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1289 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1290 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1291 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1292 | if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0) { |
| 1293 | WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1294 | "SetSendCodec() failed to set audio packet size"); |
| 1295 | return -1; |
| 1296 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1297 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1298 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Ivo Creusen | adf89b7 | 2015-04-29 16:03:33 +0200 | [diff] [blame] | 1301 | void Channel::SetBitRate(int bitrate_bps) { |
| 1302 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1303 | "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps); |
| 1304 | audio_coding_->SetBitRate(bitrate_bps); |
Erik Språng | 737336d | 2016-07-29 12:59:36 +0200 | [diff] [blame] | 1305 | retransmission_rate_limiter_->SetMaxRate(bitrate_bps); |
Ivo Creusen | adf89b7 | 2015-04-29 16:03:33 +0200 | [diff] [blame] | 1306 | } |
| 1307 | |
mflodman@webrtc.org | 0a7d4ee | 2015-02-17 12:57:14 +0000 | [diff] [blame] | 1308 | void Channel::OnIncomingFractionLoss(int fraction_lost) { |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 1309 | network_predictor_->UpdatePacketLossRate(fraction_lost); |
mflodman@webrtc.org | 0a7d4ee | 2015-02-17 12:57:14 +0000 | [diff] [blame] | 1310 | uint8_t average_fraction_loss = network_predictor_->GetLossRate(); |
| 1311 | |
minyue@webrtc.org | c1a40a7 | 2014-05-28 09:52:06 +0000 | [diff] [blame] | 1312 | // Normalizes rate to 0 - 100. |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1313 | if (audio_coding_->SetPacketLossRate(100 * average_fraction_loss / 255) != |
| 1314 | 0) { |
minyue@webrtc.org | c1a40a7 | 2014-05-28 09:52:06 +0000 | [diff] [blame] | 1315 | assert(false); // This should not happen. |
| 1316 | } |
| 1317 | } |
| 1318 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1319 | int32_t Channel::SetVADStatus(bool enableVAD, |
| 1320 | ACMVADMode mode, |
| 1321 | bool disableDTX) { |
| 1322 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1323 | "Channel::SetVADStatus(mode=%d)", mode); |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 1324 | RTC_DCHECK(!(disableDTX && enableVAD)); // disableDTX mode is deprecated. |
| 1325 | if (!codec_manager_.SetVAD(enableVAD, mode) || |
| 1326 | !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1327 | _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR, |
| 1328 | kTraceError, |
| 1329 | "SetVADStatus() failed to set VAD"); |
| 1330 | return -1; |
| 1331 | } |
| 1332 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1335 | int32_t Channel::GetVADStatus(bool& enabledVAD, |
| 1336 | ACMVADMode& mode, |
| 1337 | bool& disabledDTX) { |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 1338 | const auto* params = codec_manager_.GetStackParams(); |
| 1339 | enabledVAD = params->use_cng; |
| 1340 | mode = params->vad_mode; |
| 1341 | disabledDTX = !params->use_cng; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1342 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1345 | int32_t Channel::SetRecPayloadType(const CodecInst& codec) { |
| 1346 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1347 | "Channel::SetRecPayloadType()"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1348 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1349 | if (channel_state_.Get().playing) { |
| 1350 | _engineStatisticsPtr->SetLastError( |
| 1351 | VE_ALREADY_PLAYING, kTraceError, |
| 1352 | "SetRecPayloadType() unable to set PT while playing"); |
| 1353 | return -1; |
| 1354 | } |
| 1355 | if (channel_state_.Get().receiving) { |
| 1356 | _engineStatisticsPtr->SetLastError( |
| 1357 | VE_ALREADY_LISTENING, kTraceError, |
| 1358 | "SetRecPayloadType() unable to set PT while listening"); |
| 1359 | return -1; |
| 1360 | } |
| 1361 | |
| 1362 | if (codec.pltype == -1) { |
| 1363 | // De-register the selected codec (RTP/RTCP module and ACM) |
| 1364 | |
| 1365 | int8_t pltype(-1); |
| 1366 | CodecInst rxCodec = codec; |
| 1367 | |
| 1368 | // Get payload type for the given codec |
| 1369 | rtp_payload_registry_->ReceivePayloadType( |
| 1370 | rxCodec.plname, rxCodec.plfreq, rxCodec.channels, |
| 1371 | (rxCodec.rate < 0) ? 0 : rxCodec.rate, &pltype); |
| 1372 | rxCodec.pltype = pltype; |
| 1373 | |
| 1374 | if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0) { |
| 1375 | _engineStatisticsPtr->SetLastError( |
| 1376 | VE_RTP_RTCP_MODULE_ERROR, kTraceError, |
| 1377 | "SetRecPayloadType() RTP/RTCP-module deregistration " |
| 1378 | "failed"); |
| 1379 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1380 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1381 | if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0) { |
| 1382 | _engineStatisticsPtr->SetLastError( |
| 1383 | VE_AUDIO_CODING_MODULE_ERROR, kTraceError, |
| 1384 | "SetRecPayloadType() ACM deregistration failed - 1"); |
| 1385 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1386 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1387 | return 0; |
| 1388 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1389 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1390 | if (rtp_receiver_->RegisterReceivePayload( |
| 1391 | codec.plname, codec.pltype, codec.plfreq, codec.channels, |
| 1392 | (codec.rate < 0) ? 0 : codec.rate) != 0) { |
| 1393 | // First attempt to register failed => de-register and try again |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 1394 | // TODO(kwiberg): Retrying is probably not necessary, since |
| 1395 | // AcmReceiver::AddCodec also retries. |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1396 | rtp_receiver_->DeRegisterReceivePayload(codec.pltype); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 1397 | if (rtp_receiver_->RegisterReceivePayload( |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1398 | codec.plname, codec.pltype, codec.plfreq, codec.channels, |
| 1399 | (codec.rate < 0) ? 0 : codec.rate) != 0) { |
| 1400 | _engineStatisticsPtr->SetLastError( |
| 1401 | VE_RTP_RTCP_MODULE_ERROR, kTraceError, |
| 1402 | "SetRecPayloadType() RTP/RTCP-module registration failed"); |
| 1403 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1404 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1405 | } |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 1406 | if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1407 | audio_coding_->UnregisterReceiveCodec(codec.pltype); |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 1408 | if (!RegisterReceiveCodec(&audio_coding_, &rent_a_codec_, codec)) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1409 | _engineStatisticsPtr->SetLastError( |
| 1410 | VE_AUDIO_CODING_MODULE_ERROR, kTraceError, |
| 1411 | "SetRecPayloadType() ACM registration failed - 1"); |
| 1412 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1413 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1414 | } |
| 1415 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1418 | int32_t Channel::GetRecPayloadType(CodecInst& codec) { |
| 1419 | int8_t payloadType(-1); |
| 1420 | if (rtp_payload_registry_->ReceivePayloadType( |
| 1421 | codec.plname, codec.plfreq, codec.channels, |
| 1422 | (codec.rate < 0) ? 0 : codec.rate, &payloadType) != 0) { |
| 1423 | _engineStatisticsPtr->SetLastError( |
| 1424 | VE_RTP_RTCP_MODULE_ERROR, kTraceWarning, |
| 1425 | "GetRecPayloadType() failed to retrieve RX payload type"); |
| 1426 | return -1; |
| 1427 | } |
| 1428 | codec.pltype = payloadType; |
| 1429 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1432 | int32_t Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency) { |
| 1433 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1434 | "Channel::SetSendCNPayloadType()"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1435 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1436 | CodecInst codec; |
| 1437 | int32_t samplingFreqHz(-1); |
| 1438 | const size_t kMono = 1; |
| 1439 | if (frequency == kFreq32000Hz) |
| 1440 | samplingFreqHz = 32000; |
| 1441 | else if (frequency == kFreq16000Hz) |
| 1442 | samplingFreqHz = 16000; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1443 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1444 | if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1) { |
| 1445 | _engineStatisticsPtr->SetLastError( |
| 1446 | VE_AUDIO_CODING_MODULE_ERROR, kTraceError, |
| 1447 | "SetSendCNPayloadType() failed to retrieve default CN codec " |
| 1448 | "settings"); |
| 1449 | return -1; |
| 1450 | } |
| 1451 | |
| 1452 | // Modify the payload type (must be set to dynamic range) |
| 1453 | codec.pltype = type; |
| 1454 | |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 1455 | if (!codec_manager_.RegisterEncoder(codec) || |
| 1456 | !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1457 | _engineStatisticsPtr->SetLastError( |
| 1458 | VE_AUDIO_CODING_MODULE_ERROR, kTraceError, |
| 1459 | "SetSendCNPayloadType() failed to register CN to ACM"); |
| 1460 | return -1; |
| 1461 | } |
| 1462 | |
| 1463 | if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) { |
| 1464 | _rtpRtcpModule->DeRegisterSendPayload(codec.pltype); |
| 1465 | if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) { |
| 1466 | _engineStatisticsPtr->SetLastError( |
| 1467 | VE_RTP_RTCP_MODULE_ERROR, kTraceError, |
| 1468 | "SetSendCNPayloadType() failed to register CN to RTP/RTCP " |
| 1469 | "module"); |
| 1470 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1471 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1472 | } |
| 1473 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
minyue@webrtc.org | adee8f9 | 2014-09-03 12:28:06 +0000 | [diff] [blame] | 1476 | int Channel::SetOpusMaxPlaybackRate(int frequency_hz) { |
minyue@webrtc.org | 6aac93b | 2014-08-12 08:13:33 +0000 | [diff] [blame] | 1477 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
minyue@webrtc.org | adee8f9 | 2014-09-03 12:28:06 +0000 | [diff] [blame] | 1478 | "Channel::SetOpusMaxPlaybackRate()"); |
minyue@webrtc.org | 6aac93b | 2014-08-12 08:13:33 +0000 | [diff] [blame] | 1479 | |
minyue@webrtc.org | adee8f9 | 2014-09-03 12:28:06 +0000 | [diff] [blame] | 1480 | if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) { |
minyue@webrtc.org | 6aac93b | 2014-08-12 08:13:33 +0000 | [diff] [blame] | 1481 | _engineStatisticsPtr->SetLastError( |
| 1482 | VE_AUDIO_CODING_MODULE_ERROR, kTraceError, |
minyue@webrtc.org | adee8f9 | 2014-09-03 12:28:06 +0000 | [diff] [blame] | 1483 | "SetOpusMaxPlaybackRate() failed to set maximum playback rate"); |
minyue@webrtc.org | 6aac93b | 2014-08-12 08:13:33 +0000 | [diff] [blame] | 1484 | return -1; |
| 1485 | } |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
minyue@webrtc.org | 9b2e114 | 2015-03-13 09:38:07 +0000 | [diff] [blame] | 1489 | int Channel::SetOpusDtx(bool enable_dtx) { |
| 1490 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1491 | "Channel::SetOpusDtx(%d)", enable_dtx); |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 1492 | int ret = enable_dtx ? audio_coding_->EnableOpusDtx() |
minyue@webrtc.org | 9b2e114 | 2015-03-13 09:38:07 +0000 | [diff] [blame] | 1493 | : audio_coding_->DisableOpusDtx(); |
| 1494 | if (ret != 0) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1495 | _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR, |
| 1496 | kTraceError, "SetOpusDtx() failed"); |
minyue@webrtc.org | 9b2e114 | 2015-03-13 09:38:07 +0000 | [diff] [blame] | 1497 | return -1; |
| 1498 | } |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
ivoc | 85228d6 | 2016-07-27 04:53:47 -0700 | [diff] [blame] | 1502 | int Channel::GetOpusDtx(bool* enabled) { |
| 1503 | int success = -1; |
| 1504 | audio_coding_->QueryEncoder([&](AudioEncoder const* encoder) { |
| 1505 | if (encoder) { |
| 1506 | *enabled = encoder->GetDtx(); |
| 1507 | success = 0; |
| 1508 | } |
| 1509 | }); |
| 1510 | return success; |
| 1511 | } |
| 1512 | |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 1513 | int32_t Channel::RegisterExternalTransport(Transport* transport) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1514 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1515 | "Channel::RegisterExternalTransport()"); |
| 1516 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1517 | rtc::CritScope cs(&_callbackCritSect); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1518 | if (_externalTransport) { |
| 1519 | _engineStatisticsPtr->SetLastError( |
| 1520 | VE_INVALID_OPERATION, kTraceError, |
| 1521 | "RegisterExternalTransport() external transport already enabled"); |
| 1522 | return -1; |
| 1523 | } |
| 1524 | _externalTransport = true; |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 1525 | _transportPtr = transport; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1526 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1529 | int32_t Channel::DeRegisterExternalTransport() { |
| 1530 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1531 | "Channel::DeRegisterExternalTransport()"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1532 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1533 | rtc::CritScope cs(&_callbackCritSect); |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 1534 | if (_transportPtr) { |
| 1535 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1536 | "DeRegisterExternalTransport() all transport is disabled"); |
| 1537 | } else { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1538 | _engineStatisticsPtr->SetLastError( |
| 1539 | VE_INVALID_OPERATION, kTraceWarning, |
| 1540 | "DeRegisterExternalTransport() external transport already " |
| 1541 | "disabled"); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1542 | } |
| 1543 | _externalTransport = false; |
| 1544 | _transportPtr = NULL; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1545 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 1548 | int32_t Channel::ReceivedRTPPacket(const uint8_t* received_packet, |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1549 | size_t length, |
solenberg@webrtc.org | b1f5010 | 2014-03-24 10:38:25 +0000 | [diff] [blame] | 1550 | const PacketTime& packet_time) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1551 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
pwestin@webrtc.org | 0c45957 | 2013-04-03 15:43:57 +0000 | [diff] [blame] | 1552 | "Channel::ReceivedRTPPacket()"); |
| 1553 | |
| 1554 | // Store playout timestamp for the received RTP packet |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 1555 | UpdatePlayoutTimestamp(false); |
pwestin@webrtc.org | 0c45957 | 2013-04-03 15:43:57 +0000 | [diff] [blame] | 1556 | |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1557 | RTPHeader header; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 1558 | if (!rtp_header_parser_->Parse(received_packet, length, &header)) { |
| 1559 | WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId, |
| 1560 | "Incoming packet: invalid RTP header"); |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1561 | return -1; |
| 1562 | } |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 1563 | header.payload_type_frequency = |
| 1564 | rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 1565 | if (header.payload_type_frequency < 0) |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 1566 | return -1; |
stefan@webrtc.org | 48df381 | 2013-11-08 15:18:52 +0000 | [diff] [blame] | 1567 | bool in_order = IsPacketInOrder(header); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1568 | rtp_receive_statistics_->IncomingPacket( |
| 1569 | header, length, IsPacketRetransmitted(header, in_order)); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 1570 | rtp_payload_registry_->SetIncomingPayloadType(header); |
solenberg@webrtc.org | b1f5010 | 2014-03-24 10:38:25 +0000 | [diff] [blame] | 1571 | |
stefan@webrtc.org | 48df381 | 2013-11-08 15:18:52 +0000 | [diff] [blame] | 1572 | return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | bool Channel::ReceivePacket(const uint8_t* packet, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1576 | size_t packet_length, |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 1577 | const RTPHeader& header, |
| 1578 | bool in_order) { |
minyue@webrtc.org | 456f014 | 2015-01-23 11:58:42 +0000 | [diff] [blame] | 1579 | if (rtp_payload_registry_->IsRtx(header)) { |
| 1580 | return HandleRtxPacket(packet, packet_length, header); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 1581 | } |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 1582 | const uint8_t* payload = packet + header.headerLength; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1583 | assert(packet_length >= header.headerLength); |
| 1584 | size_t payload_length = packet_length - header.headerLength; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 1585 | PayloadUnion payload_specific; |
| 1586 | if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType, |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 1587 | &payload_specific)) { |
| 1588 | return false; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 1589 | } |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 1590 | return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length, |
| 1591 | payload_specific, in_order); |
| 1592 | } |
| 1593 | |
minyue@webrtc.org | 456f014 | 2015-01-23 11:58:42 +0000 | [diff] [blame] | 1594 | bool Channel::HandleRtxPacket(const uint8_t* packet, |
| 1595 | size_t packet_length, |
| 1596 | const RTPHeader& header) { |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 1597 | if (!rtp_payload_registry_->IsRtx(header)) |
| 1598 | return false; |
| 1599 | |
| 1600 | // Remove the RTX header and parse the original RTP header. |
| 1601 | if (packet_length < header.headerLength) |
| 1602 | return false; |
| 1603 | if (packet_length > kVoiceEngineMaxIpPacketSizeBytes) |
| 1604 | return false; |
| 1605 | if (restored_packet_in_use_) { |
| 1606 | WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId, |
| 1607 | "Multiple RTX headers detected, dropping packet"); |
| 1608 | return false; |
pwestin@webrtc.org | 0c45957 | 2013-04-03 15:43:57 +0000 | [diff] [blame] | 1609 | } |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 1610 | if (!rtp_payload_registry_->RestoreOriginalPacket( |
noahric | 65220a7 | 2015-10-14 11:29:49 -0700 | [diff] [blame] | 1611 | restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(), |
| 1612 | header)) { |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 1613 | WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId, |
| 1614 | "Incoming RTX packet: invalid RTP header"); |
| 1615 | return false; |
| 1616 | } |
| 1617 | restored_packet_in_use_ = true; |
noahric | 65220a7 | 2015-10-14 11:29:49 -0700 | [diff] [blame] | 1618 | bool ret = OnRecoveredPacket(restored_packet_, packet_length); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 1619 | restored_packet_in_use_ = false; |
| 1620 | return ret; |
| 1621 | } |
| 1622 | |
| 1623 | bool Channel::IsPacketInOrder(const RTPHeader& header) const { |
| 1624 | StreamStatistician* statistician = |
| 1625 | rtp_receive_statistics_->GetStatistician(header.ssrc); |
| 1626 | if (!statistician) |
| 1627 | return false; |
| 1628 | return statistician->IsPacketInOrder(header.sequenceNumber); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
stefan@webrtc.org | 48df381 | 2013-11-08 15:18:52 +0000 | [diff] [blame] | 1631 | bool Channel::IsPacketRetransmitted(const RTPHeader& header, |
| 1632 | bool in_order) const { |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 1633 | // Retransmissions are handled separately if RTX is enabled. |
| 1634 | if (rtp_payload_registry_->RtxEnabled()) |
| 1635 | return false; |
| 1636 | StreamStatistician* statistician = |
| 1637 | rtp_receive_statistics_->GetStatistician(header.ssrc); |
| 1638 | if (!statistician) |
| 1639 | return false; |
| 1640 | // Check if this is a retransmission. |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 1641 | int64_t min_rtt = 0; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 1642 | _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1643 | return !in_order && statistician->IsRetransmitOfOldPacket(header, min_rtt); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 1646 | int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1647 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
pwestin@webrtc.org | 0c45957 | 2013-04-03 15:43:57 +0000 | [diff] [blame] | 1648 | "Channel::ReceivedRTCPPacket()"); |
| 1649 | // Store playout timestamp for the received RTCP packet |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 1650 | UpdatePlayoutTimestamp(true); |
pwestin@webrtc.org | 0c45957 | 2013-04-03 15:43:57 +0000 | [diff] [blame] | 1651 | |
pwestin@webrtc.org | 0c45957 | 2013-04-03 15:43:57 +0000 | [diff] [blame] | 1652 | // Deliver RTCP packet to RTP/RTCP module for parsing |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 1653 | if (_rtpRtcpModule->IncomingRtcpPacket(data, length) == -1) { |
pwestin@webrtc.org | 0c45957 | 2013-04-03 15:43:57 +0000 | [diff] [blame] | 1654 | _engineStatisticsPtr->SetLastError( |
| 1655 | VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning, |
| 1656 | "Channel::IncomingRTPPacket() RTCP packet is invalid"); |
| 1657 | } |
wu@webrtc.org | 82c4b85 | 2014-05-20 22:55:01 +0000 | [diff] [blame] | 1658 | |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 1659 | int64_t rtt = GetRTT(true); |
| 1660 | if (rtt == 0) { |
| 1661 | // Waiting for valid RTT. |
| 1662 | return 0; |
| 1663 | } |
Erik Språng | 737336d | 2016-07-29 12:59:36 +0200 | [diff] [blame] | 1664 | |
| 1665 | int64_t nack_window_ms = rtt; |
| 1666 | if (nack_window_ms < kMinRetransmissionWindowMs) { |
| 1667 | nack_window_ms = kMinRetransmissionWindowMs; |
| 1668 | } else if (nack_window_ms > kMaxRetransmissionWindowMs) { |
| 1669 | nack_window_ms = kMaxRetransmissionWindowMs; |
| 1670 | } |
| 1671 | retransmission_rate_limiter_->SetWindowSize(nack_window_ms); |
| 1672 | |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 1673 | uint32_t ntp_secs = 0; |
| 1674 | uint32_t ntp_frac = 0; |
| 1675 | uint32_t rtp_timestamp = 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1676 | if (0 != |
| 1677 | _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL, |
| 1678 | &rtp_timestamp)) { |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 1679 | // Waiting for RTCP. |
| 1680 | return 0; |
| 1681 | } |
| 1682 | |
stefan@webrtc.org | 8e24d87 | 2014-09-02 18:58:24 +0000 | [diff] [blame] | 1683 | { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 1684 | rtc::CritScope lock(&ts_stats_lock_); |
minyue@webrtc.org | 2c0cdbc | 2014-10-09 10:52:43 +0000 | [diff] [blame] | 1685 | ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp); |
stefan@webrtc.org | 8e24d87 | 2014-09-02 18:58:24 +0000 | [diff] [blame] | 1686 | } |
pwestin@webrtc.org | 0c45957 | 2013-04-03 15:43:57 +0000 | [diff] [blame] | 1687 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1690 | int Channel::StartPlayingFileLocally(const char* fileName, |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 1691 | bool loop, |
| 1692 | FileFormats format, |
| 1693 | int startPosition, |
| 1694 | float volumeScaling, |
| 1695 | int stopPosition, |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1696 | const CodecInst* codecInst) { |
| 1697 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1698 | "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d," |
| 1699 | " format=%d, volumeScaling=%5.3f, startPosition=%d, " |
| 1700 | "stopPosition=%d)", |
| 1701 | fileName, loop, format, volumeScaling, startPosition, |
| 1702 | stopPosition); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1703 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1704 | if (channel_state_.Get().output_file_playing) { |
| 1705 | _engineStatisticsPtr->SetLastError( |
| 1706 | VE_ALREADY_PLAYING, kTraceError, |
| 1707 | "StartPlayingFileLocally() is already playing"); |
| 1708 | return -1; |
| 1709 | } |
| 1710 | |
| 1711 | { |
| 1712 | rtc::CritScope cs(&_fileCritSect); |
| 1713 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1714 | if (output_file_player_) { |
| 1715 | output_file_player_->RegisterModuleFileCallback(NULL); |
| 1716 | output_file_player_.reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
kwiberg | 5b356f4 | 2016-09-08 04:32:33 -0700 | [diff] [blame] | 1719 | output_file_player_ = FilePlayer::CreateFilePlayer( |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1720 | _outputFilePlayerId, (const FileFormats)format); |
henrike@webrtc.org | b37c628 | 2011-10-31 23:53:04 +0000 | [diff] [blame] | 1721 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1722 | if (!output_file_player_) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1723 | _engineStatisticsPtr->SetLastError( |
| 1724 | VE_INVALID_ARGUMENT, kTraceError, |
| 1725 | "StartPlayingFileLocally() filePlayer format is not correct"); |
| 1726 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1727 | } |
braveyao@webrtc.org | ab12990 | 2012-06-04 03:26:39 +0000 | [diff] [blame] | 1728 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1729 | const uint32_t notificationTime(0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1730 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1731 | if (output_file_player_->StartPlayingFile( |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1732 | fileName, loop, startPosition, volumeScaling, notificationTime, |
| 1733 | stopPosition, (const CodecInst*)codecInst) != 0) { |
| 1734 | _engineStatisticsPtr->SetLastError( |
| 1735 | VE_BAD_FILE, kTraceError, |
| 1736 | "StartPlayingFile() failed to start file playout"); |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1737 | output_file_player_->StopPlayingFile(); |
| 1738 | output_file_player_.reset(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1739 | return -1; |
| 1740 | } |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1741 | output_file_player_->RegisterModuleFileCallback(this); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1742 | channel_state_.SetOutputFilePlaying(true); |
| 1743 | } |
| 1744 | |
| 1745 | if (RegisterFilePlayingToMixer() != 0) |
| 1746 | return -1; |
| 1747 | |
| 1748 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | int Channel::StartPlayingFileLocally(InStream* stream, |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 1752 | FileFormats format, |
| 1753 | int startPosition, |
| 1754 | float volumeScaling, |
| 1755 | int stopPosition, |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1756 | const CodecInst* codecInst) { |
| 1757 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1758 | "Channel::StartPlayingFileLocally(format=%d," |
| 1759 | " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)", |
| 1760 | format, volumeScaling, startPosition, stopPosition); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1761 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1762 | if (stream == NULL) { |
| 1763 | _engineStatisticsPtr->SetLastError( |
| 1764 | VE_BAD_FILE, kTraceError, |
| 1765 | "StartPlayingFileLocally() NULL as input stream"); |
| 1766 | return -1; |
| 1767 | } |
| 1768 | |
| 1769 | if (channel_state_.Get().output_file_playing) { |
| 1770 | _engineStatisticsPtr->SetLastError( |
| 1771 | VE_ALREADY_PLAYING, kTraceError, |
| 1772 | "StartPlayingFileLocally() is already playing"); |
| 1773 | return -1; |
| 1774 | } |
| 1775 | |
| 1776 | { |
| 1777 | rtc::CritScope cs(&_fileCritSect); |
| 1778 | |
| 1779 | // Destroy the old instance |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1780 | if (output_file_player_) { |
| 1781 | output_file_player_->RegisterModuleFileCallback(NULL); |
| 1782 | output_file_player_.reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1785 | // Create the instance |
kwiberg | 5b356f4 | 2016-09-08 04:32:33 -0700 | [diff] [blame] | 1786 | output_file_player_ = FilePlayer::CreateFilePlayer( |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1787 | _outputFilePlayerId, (const FileFormats)format); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1788 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1789 | if (!output_file_player_) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1790 | _engineStatisticsPtr->SetLastError( |
| 1791 | VE_INVALID_ARGUMENT, kTraceError, |
| 1792 | "StartPlayingFileLocally() filePlayer format isnot correct"); |
| 1793 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1796 | const uint32_t notificationTime(0); |
henrike@webrtc.org | b37c628 | 2011-10-31 23:53:04 +0000 | [diff] [blame] | 1797 | |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 1798 | if (output_file_player_->StartPlayingFile(stream, startPosition, |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1799 | volumeScaling, notificationTime, |
| 1800 | stopPosition, codecInst) != 0) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1801 | _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError, |
| 1802 | "StartPlayingFile() failed to " |
| 1803 | "start file playout"); |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1804 | output_file_player_->StopPlayingFile(); |
| 1805 | output_file_player_.reset(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1806 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1807 | } |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1808 | output_file_player_->RegisterModuleFileCallback(this); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1809 | channel_state_.SetOutputFilePlaying(true); |
| 1810 | } |
braveyao@webrtc.org | ab12990 | 2012-06-04 03:26:39 +0000 | [diff] [blame] | 1811 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1812 | if (RegisterFilePlayingToMixer() != 0) |
| 1813 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1814 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1815 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1818 | int Channel::StopPlayingFileLocally() { |
| 1819 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1820 | "Channel::StopPlayingFileLocally()"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1821 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1822 | if (!channel_state_.Get().output_file_playing) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1823 | return 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | { |
| 1827 | rtc::CritScope cs(&_fileCritSect); |
| 1828 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1829 | if (output_file_player_->StopPlayingFile() != 0) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1830 | _engineStatisticsPtr->SetLastError( |
| 1831 | VE_STOP_RECORDING_FAILED, kTraceError, |
| 1832 | "StopPlayingFile() could not stop playing"); |
| 1833 | return -1; |
| 1834 | } |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1835 | output_file_player_->RegisterModuleFileCallback(NULL); |
| 1836 | output_file_player_.reset(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1837 | channel_state_.SetOutputFilePlaying(false); |
| 1838 | } |
| 1839 | // _fileCritSect cannot be taken while calling |
| 1840 | // SetAnonymousMixibilityStatus. Refer to comments in |
| 1841 | // StartPlayingFileLocally(const char* ...) for more details. |
| 1842 | if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0) { |
| 1843 | _engineStatisticsPtr->SetLastError( |
| 1844 | VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError, |
| 1845 | "StopPlayingFile() failed to stop participant from playing as" |
| 1846 | "file in the mixer"); |
| 1847 | return -1; |
| 1848 | } |
| 1849 | |
| 1850 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1853 | int Channel::IsPlayingFileLocally() const { |
| 1854 | return channel_state_.Get().output_file_playing; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1857 | int Channel::RegisterFilePlayingToMixer() { |
| 1858 | // Return success for not registering for file playing to mixer if: |
| 1859 | // 1. playing file before playout is started on that channel. |
| 1860 | // 2. starting playout without file playing on that channel. |
| 1861 | if (!channel_state_.Get().playing || |
| 1862 | !channel_state_.Get().output_file_playing) { |
braveyao@webrtc.org | ab12990 | 2012-06-04 03:26:39 +0000 | [diff] [blame] | 1863 | return 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | // |_fileCritSect| cannot be taken while calling |
| 1867 | // SetAnonymousMixabilityStatus() since as soon as the participant is added |
| 1868 | // frames can be pulled by the mixer. Since the frames are generated from |
| 1869 | // the file, _fileCritSect will be taken. This would result in a deadlock. |
| 1870 | if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0) { |
| 1871 | channel_state_.SetOutputFilePlaying(false); |
| 1872 | rtc::CritScope cs(&_fileCritSect); |
| 1873 | _engineStatisticsPtr->SetLastError( |
| 1874 | VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError, |
| 1875 | "StartPlayingFile() failed to add participant as file to mixer"); |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1876 | output_file_player_->StopPlayingFile(); |
| 1877 | output_file_player_.reset(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1878 | return -1; |
| 1879 | } |
| 1880 | |
| 1881 | return 0; |
braveyao@webrtc.org | ab12990 | 2012-06-04 03:26:39 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1884 | int Channel::StartPlayingFileAsMicrophone(const char* fileName, |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 1885 | bool loop, |
| 1886 | FileFormats format, |
| 1887 | int startPosition, |
| 1888 | float volumeScaling, |
| 1889 | int stopPosition, |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1890 | const CodecInst* codecInst) { |
| 1891 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1892 | "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, " |
| 1893 | "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, " |
| 1894 | "stopPosition=%d)", |
| 1895 | fileName, loop, format, volumeScaling, startPosition, |
| 1896 | stopPosition); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1897 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1898 | rtc::CritScope cs(&_fileCritSect); |
henrika@webrtc.org | 944cbeb | 2014-03-18 10:32:33 +0000 | [diff] [blame] | 1899 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1900 | if (channel_state_.Get().input_file_playing) { |
| 1901 | _engineStatisticsPtr->SetLastError( |
| 1902 | VE_ALREADY_PLAYING, kTraceWarning, |
| 1903 | "StartPlayingFileAsMicrophone() filePlayer is playing"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1904 | return 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | // Destroy the old instance |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1908 | if (input_file_player_) { |
| 1909 | input_file_player_->RegisterModuleFileCallback(NULL); |
| 1910 | input_file_player_.reset(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | // Create the instance |
kwiberg | 5b356f4 | 2016-09-08 04:32:33 -0700 | [diff] [blame] | 1914 | input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId, |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1915 | (const FileFormats)format); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1916 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1917 | if (!input_file_player_) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1918 | _engineStatisticsPtr->SetLastError( |
| 1919 | VE_INVALID_ARGUMENT, kTraceError, |
| 1920 | "StartPlayingFileAsMicrophone() filePlayer format isnot correct"); |
| 1921 | return -1; |
| 1922 | } |
| 1923 | |
| 1924 | const uint32_t notificationTime(0); |
| 1925 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1926 | if (input_file_player_->StartPlayingFile( |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1927 | fileName, loop, startPosition, volumeScaling, notificationTime, |
| 1928 | stopPosition, (const CodecInst*)codecInst) != 0) { |
| 1929 | _engineStatisticsPtr->SetLastError( |
| 1930 | VE_BAD_FILE, kTraceError, |
| 1931 | "StartPlayingFile() failed to start file playout"); |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1932 | input_file_player_->StopPlayingFile(); |
| 1933 | input_file_player_.reset(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1934 | return -1; |
| 1935 | } |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1936 | input_file_player_->RegisterModuleFileCallback(this); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1937 | channel_state_.SetInputFilePlaying(true); |
| 1938 | |
| 1939 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | int Channel::StartPlayingFileAsMicrophone(InStream* stream, |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 1943 | FileFormats format, |
| 1944 | int startPosition, |
| 1945 | float volumeScaling, |
| 1946 | int stopPosition, |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1947 | const CodecInst* codecInst) { |
| 1948 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 1949 | "Channel::StartPlayingFileAsMicrophone(format=%d, " |
| 1950 | "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)", |
| 1951 | format, volumeScaling, startPosition, stopPosition); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1952 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1953 | if (stream == NULL) { |
| 1954 | _engineStatisticsPtr->SetLastError( |
| 1955 | VE_BAD_FILE, kTraceError, |
| 1956 | "StartPlayingFileAsMicrophone NULL as input stream"); |
| 1957 | return -1; |
| 1958 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1959 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1960 | rtc::CritScope cs(&_fileCritSect); |
henrika@webrtc.org | 944cbeb | 2014-03-18 10:32:33 +0000 | [diff] [blame] | 1961 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1962 | if (channel_state_.Get().input_file_playing) { |
| 1963 | _engineStatisticsPtr->SetLastError( |
| 1964 | VE_ALREADY_PLAYING, kTraceWarning, |
| 1965 | "StartPlayingFileAsMicrophone() is playing"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1966 | return 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1967 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1968 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1969 | // Destroy the old instance |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1970 | if (input_file_player_) { |
| 1971 | input_file_player_->RegisterModuleFileCallback(NULL); |
| 1972 | input_file_player_.reset(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1973 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1974 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1975 | // Create the instance |
kwiberg | 5b356f4 | 2016-09-08 04:32:33 -0700 | [diff] [blame] | 1976 | input_file_player_ = FilePlayer::CreateFilePlayer(_inputFilePlayerId, |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1977 | (const FileFormats)format); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1978 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1979 | if (!input_file_player_) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1980 | _engineStatisticsPtr->SetLastError( |
| 1981 | VE_INVALID_ARGUMENT, kTraceError, |
| 1982 | "StartPlayingInputFile() filePlayer format isnot correct"); |
| 1983 | return -1; |
| 1984 | } |
| 1985 | |
| 1986 | const uint32_t notificationTime(0); |
| 1987 | |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 1988 | if (input_file_player_->StartPlayingFile(stream, startPosition, volumeScaling, |
| 1989 | notificationTime, stopPosition, |
| 1990 | codecInst) != 0) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1991 | _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError, |
| 1992 | "StartPlayingFile() failed to start " |
| 1993 | "file playout"); |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1994 | input_file_player_->StopPlayingFile(); |
| 1995 | input_file_player_.reset(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 1996 | return -1; |
| 1997 | } |
| 1998 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 1999 | input_file_player_->RegisterModuleFileCallback(this); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2000 | channel_state_.SetInputFilePlaying(true); |
| 2001 | |
| 2002 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2005 | int Channel::StopPlayingFileAsMicrophone() { |
| 2006 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2007 | "Channel::StopPlayingFileAsMicrophone()"); |
| 2008 | |
| 2009 | rtc::CritScope cs(&_fileCritSect); |
| 2010 | |
| 2011 | if (!channel_state_.Get().input_file_playing) { |
| 2012 | return 0; |
| 2013 | } |
| 2014 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2015 | if (input_file_player_->StopPlayingFile() != 0) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2016 | _engineStatisticsPtr->SetLastError( |
| 2017 | VE_STOP_RECORDING_FAILED, kTraceError, |
| 2018 | "StopPlayingFile() could not stop playing"); |
| 2019 | return -1; |
| 2020 | } |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2021 | input_file_player_->RegisterModuleFileCallback(NULL); |
| 2022 | input_file_player_.reset(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2023 | channel_state_.SetInputFilePlaying(false); |
| 2024 | |
| 2025 | return 0; |
| 2026 | } |
| 2027 | |
| 2028 | int Channel::IsPlayingFileAsMicrophone() const { |
| 2029 | return channel_state_.Get().input_file_playing; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
leozwang@webrtc.org | 813e4b0 | 2012-03-01 18:34:25 +0000 | [diff] [blame] | 2032 | int Channel::StartRecordingPlayout(const char* fileName, |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2033 | const CodecInst* codecInst) { |
| 2034 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2035 | "Channel::StartRecordingPlayout(fileName=%s)", fileName); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2036 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2037 | if (_outputFileRecording) { |
| 2038 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1), |
| 2039 | "StartRecordingPlayout() is already recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2040 | return 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2041 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2042 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2043 | FileFormats format; |
| 2044 | const uint32_t notificationTime(0); // Not supported in VoE |
| 2045 | CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000}; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2046 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2047 | if ((codecInst != NULL) && |
| 2048 | ((codecInst->channels < 1) || (codecInst->channels > 2))) { |
| 2049 | _engineStatisticsPtr->SetLastError( |
| 2050 | VE_BAD_ARGUMENT, kTraceError, |
| 2051 | "StartRecordingPlayout() invalid compression"); |
| 2052 | return (-1); |
| 2053 | } |
| 2054 | if (codecInst == NULL) { |
| 2055 | format = kFileFormatPcm16kHzFile; |
| 2056 | codecInst = &dummyCodec; |
| 2057 | } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) || |
| 2058 | (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) || |
| 2059 | (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) { |
| 2060 | format = kFileFormatWavFile; |
| 2061 | } else { |
| 2062 | format = kFileFormatCompressedFile; |
| 2063 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2064 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2065 | rtc::CritScope cs(&_fileCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2066 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2067 | // Destroy the old instance |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2068 | if (output_file_recorder_) { |
| 2069 | output_file_recorder_->RegisterModuleFileCallback(NULL); |
| 2070 | output_file_recorder_.reset(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2071 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2072 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2073 | output_file_recorder_ = FileRecorder::CreateFileRecorder( |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2074 | _outputFileRecorderId, (const FileFormats)format); |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2075 | if (!output_file_recorder_) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2076 | _engineStatisticsPtr->SetLastError( |
| 2077 | VE_INVALID_ARGUMENT, kTraceError, |
| 2078 | "StartRecordingPlayout() fileRecorder format isnot correct"); |
| 2079 | return -1; |
| 2080 | } |
| 2081 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2082 | if (output_file_recorder_->StartRecordingAudioFile( |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2083 | fileName, (const CodecInst&)*codecInst, notificationTime) != 0) { |
| 2084 | _engineStatisticsPtr->SetLastError( |
| 2085 | VE_BAD_FILE, kTraceError, |
| 2086 | "StartRecordingAudioFile() failed to start file recording"); |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2087 | output_file_recorder_->StopRecording(); |
| 2088 | output_file_recorder_.reset(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2089 | return -1; |
| 2090 | } |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2091 | output_file_recorder_->RegisterModuleFileCallback(this); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2092 | _outputFileRecording = true; |
| 2093 | |
| 2094 | return 0; |
| 2095 | } |
| 2096 | |
| 2097 | int Channel::StartRecordingPlayout(OutStream* stream, |
| 2098 | const CodecInst* codecInst) { |
| 2099 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2100 | "Channel::StartRecordingPlayout()"); |
| 2101 | |
| 2102 | if (_outputFileRecording) { |
| 2103 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1), |
| 2104 | "StartRecordingPlayout() is already recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2105 | return 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | FileFormats format; |
| 2109 | const uint32_t notificationTime(0); // Not supported in VoE |
| 2110 | CodecInst dummyCodec = {100, "L16", 16000, 320, 1, 320000}; |
| 2111 | |
| 2112 | if (codecInst != NULL && codecInst->channels != 1) { |
| 2113 | _engineStatisticsPtr->SetLastError( |
| 2114 | VE_BAD_ARGUMENT, kTraceError, |
| 2115 | "StartRecordingPlayout() invalid compression"); |
| 2116 | return (-1); |
| 2117 | } |
| 2118 | if (codecInst == NULL) { |
| 2119 | format = kFileFormatPcm16kHzFile; |
| 2120 | codecInst = &dummyCodec; |
| 2121 | } else if ((STR_CASE_CMP(codecInst->plname, "L16") == 0) || |
| 2122 | (STR_CASE_CMP(codecInst->plname, "PCMU") == 0) || |
| 2123 | (STR_CASE_CMP(codecInst->plname, "PCMA") == 0)) { |
| 2124 | format = kFileFormatWavFile; |
| 2125 | } else { |
| 2126 | format = kFileFormatCompressedFile; |
| 2127 | } |
| 2128 | |
| 2129 | rtc::CritScope cs(&_fileCritSect); |
| 2130 | |
| 2131 | // Destroy the old instance |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2132 | if (output_file_recorder_) { |
| 2133 | output_file_recorder_->RegisterModuleFileCallback(NULL); |
| 2134 | output_file_recorder_.reset(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2135 | } |
| 2136 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2137 | output_file_recorder_ = FileRecorder::CreateFileRecorder( |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2138 | _outputFileRecorderId, (const FileFormats)format); |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2139 | if (!output_file_recorder_) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2140 | _engineStatisticsPtr->SetLastError( |
| 2141 | VE_INVALID_ARGUMENT, kTraceError, |
| 2142 | "StartRecordingPlayout() fileRecorder format isnot correct"); |
| 2143 | return -1; |
| 2144 | } |
| 2145 | |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 2146 | if (output_file_recorder_->StartRecordingAudioFile(stream, *codecInst, |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2147 | notificationTime) != 0) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2148 | _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError, |
| 2149 | "StartRecordingPlayout() failed to " |
| 2150 | "start file recording"); |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2151 | output_file_recorder_->StopRecording(); |
| 2152 | output_file_recorder_.reset(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2153 | return -1; |
| 2154 | } |
| 2155 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2156 | output_file_recorder_->RegisterModuleFileCallback(this); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2157 | _outputFileRecording = true; |
| 2158 | |
| 2159 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2162 | int Channel::StopRecordingPlayout() { |
| 2163 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1), |
| 2164 | "Channel::StopRecordingPlayout()"); |
| 2165 | |
| 2166 | if (!_outputFileRecording) { |
| 2167 | WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, -1), |
| 2168 | "StopRecordingPlayout() isnot recording"); |
| 2169 | return -1; |
| 2170 | } |
| 2171 | |
| 2172 | rtc::CritScope cs(&_fileCritSect); |
| 2173 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2174 | if (output_file_recorder_->StopRecording() != 0) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2175 | _engineStatisticsPtr->SetLastError( |
| 2176 | VE_STOP_RECORDING_FAILED, kTraceError, |
| 2177 | "StopRecording() could not stop recording"); |
| 2178 | return (-1); |
| 2179 | } |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2180 | output_file_recorder_->RegisterModuleFileCallback(NULL); |
| 2181 | output_file_recorder_.reset(); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2182 | _outputFileRecording = false; |
| 2183 | |
| 2184 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2187 | void Channel::SetMixWithMicStatus(bool mix) { |
| 2188 | rtc::CritScope cs(&_fileCritSect); |
| 2189 | _mixFileWithMicrophone = mix; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2192 | int Channel::GetSpeechOutputLevel(uint32_t& level) const { |
| 2193 | int8_t currentLevel = _outputAudioLevel.Level(); |
| 2194 | level = static_cast<int32_t>(currentLevel); |
| 2195 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2198 | int Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const { |
| 2199 | int16_t currentLevel = _outputAudioLevel.LevelFullRange(); |
| 2200 | level = static_cast<int32_t>(currentLevel); |
| 2201 | return 0; |
| 2202 | } |
| 2203 | |
solenberg | 1c2af8e | 2016-03-24 10:36:00 -0700 | [diff] [blame] | 2204 | int Channel::SetInputMute(bool enable) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2205 | rtc::CritScope cs(&volume_settings_critsect_); |
| 2206 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2207 | "Channel::SetMute(enable=%d)", enable); |
solenberg | 1c2af8e | 2016-03-24 10:36:00 -0700 | [diff] [blame] | 2208 | input_mute_ = enable; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2209 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
solenberg | 1c2af8e | 2016-03-24 10:36:00 -0700 | [diff] [blame] | 2212 | bool Channel::InputMute() const { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2213 | rtc::CritScope cs(&volume_settings_critsect_); |
solenberg | 1c2af8e | 2016-03-24 10:36:00 -0700 | [diff] [blame] | 2214 | return input_mute_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2217 | int Channel::SetOutputVolumePan(float left, float right) { |
| 2218 | rtc::CritScope cs(&volume_settings_critsect_); |
| 2219 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2220 | "Channel::SetOutputVolumePan()"); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2221 | _panLeft = left; |
| 2222 | _panRight = right; |
| 2223 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2226 | int Channel::GetOutputVolumePan(float& left, float& right) const { |
| 2227 | rtc::CritScope cs(&volume_settings_critsect_); |
| 2228 | left = _panLeft; |
| 2229 | right = _panRight; |
| 2230 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2233 | int Channel::SetChannelOutputVolumeScaling(float scaling) { |
| 2234 | rtc::CritScope cs(&volume_settings_critsect_); |
| 2235 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2236 | "Channel::SetChannelOutputVolumeScaling()"); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2237 | _outputGain = scaling; |
| 2238 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2241 | int Channel::GetChannelOutputVolumeScaling(float& scaling) const { |
| 2242 | rtc::CritScope cs(&volume_settings_critsect_); |
| 2243 | scaling = _outputGain; |
| 2244 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
solenberg | 8842c3e | 2016-03-11 03:06:41 -0800 | [diff] [blame] | 2247 | int Channel::SendTelephoneEventOutband(int event, int duration_ms) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2248 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
solenberg | 8842c3e | 2016-03-11 03:06:41 -0800 | [diff] [blame] | 2249 | "Channel::SendTelephoneEventOutband(...)"); |
| 2250 | RTC_DCHECK_LE(0, event); |
| 2251 | RTC_DCHECK_GE(255, event); |
| 2252 | RTC_DCHECK_LE(0, duration_ms); |
| 2253 | RTC_DCHECK_GE(65535, duration_ms); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2254 | if (!Sending()) { |
| 2255 | return -1; |
| 2256 | } |
solenberg | 8842c3e | 2016-03-11 03:06:41 -0800 | [diff] [blame] | 2257 | if (_rtpRtcpModule->SendTelephoneEventOutband( |
| 2258 | event, duration_ms, kTelephoneEventAttenuationdB) != 0) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2259 | _engineStatisticsPtr->SetLastError( |
| 2260 | VE_SEND_DTMF_FAILED, kTraceWarning, |
| 2261 | "SendTelephoneEventOutband() failed to send event"); |
| 2262 | return -1; |
| 2263 | } |
| 2264 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
solenberg | 31642aa | 2016-03-14 08:00:37 -0700 | [diff] [blame] | 2267 | int Channel::SetSendTelephoneEventPayloadType(int payload_type) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2268 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2269 | "Channel::SetSendTelephoneEventPayloadType()"); |
solenberg | 31642aa | 2016-03-14 08:00:37 -0700 | [diff] [blame] | 2270 | RTC_DCHECK_LE(0, payload_type); |
| 2271 | RTC_DCHECK_GE(127, payload_type); |
| 2272 | CodecInst codec = {0}; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2273 | codec.plfreq = 8000; |
solenberg | 31642aa | 2016-03-14 08:00:37 -0700 | [diff] [blame] | 2274 | codec.pltype = payload_type; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2275 | memcpy(codec.plname, "telephone-event", 16); |
| 2276 | if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) { |
| 2277 | _rtpRtcpModule->DeRegisterSendPayload(codec.pltype); |
| 2278 | if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) { |
| 2279 | _engineStatisticsPtr->SetLastError( |
| 2280 | VE_RTP_RTCP_MODULE_ERROR, kTraceError, |
| 2281 | "SetSendTelephoneEventPayloadType() failed to register send" |
| 2282 | "payload type"); |
| 2283 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2284 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2285 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2286 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2289 | int Channel::VoiceActivityIndicator(int& activity) { |
| 2290 | activity = _sendFrameType; |
| 2291 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2294 | int Channel::SetLocalSSRC(unsigned int ssrc) { |
| 2295 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2296 | "Channel::SetLocalSSRC()"); |
| 2297 | if (channel_state_.Get().sending) { |
| 2298 | _engineStatisticsPtr->SetLastError(VE_ALREADY_SENDING, kTraceError, |
| 2299 | "SetLocalSSRC() already sending"); |
| 2300 | return -1; |
| 2301 | } |
| 2302 | _rtpRtcpModule->SetSSRC(ssrc); |
| 2303 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2306 | int Channel::GetLocalSSRC(unsigned int& ssrc) { |
| 2307 | ssrc = _rtpRtcpModule->SSRC(); |
| 2308 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2311 | int Channel::GetRemoteSSRC(unsigned int& ssrc) { |
| 2312 | ssrc = rtp_receiver_->SSRC(); |
| 2313 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
wu@webrtc.org | ebdb0e3 | 2014-03-06 23:49:08 +0000 | [diff] [blame] | 2316 | int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) { |
andrew@webrtc.org | f3930e9 | 2013-09-18 22:37:32 +0000 | [diff] [blame] | 2317 | _includeAudioLevelIndication = enable; |
wu@webrtc.org | ebdb0e3 | 2014-03-06 23:49:08 +0000 | [diff] [blame] | 2318 | return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2319 | } |
andrew@webrtc.org | f3930e9 | 2013-09-18 22:37:32 +0000 | [diff] [blame] | 2320 | |
wu@webrtc.org | 93fd25c | 2014-04-24 20:33:08 +0000 | [diff] [blame] | 2321 | int Channel::SetReceiveAudioLevelIndicationStatus(bool enable, |
| 2322 | unsigned char id) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2323 | rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel); |
| 2324 | if (enable && |
| 2325 | !rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel, |
| 2326 | id)) { |
wu@webrtc.org | 93fd25c | 2014-04-24 20:33:08 +0000 | [diff] [blame] | 2327 | return -1; |
| 2328 | } |
| 2329 | return 0; |
| 2330 | } |
| 2331 | |
wu@webrtc.org | ebdb0e3 | 2014-03-06 23:49:08 +0000 | [diff] [blame] | 2332 | int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) { |
| 2333 | return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id); |
| 2334 | } |
| 2335 | |
| 2336 | int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) { |
| 2337 | rtp_header_parser_->DeregisterRtpHeaderExtension( |
| 2338 | kRtpExtensionAbsoluteSendTime); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2339 | if (enable && |
| 2340 | !rtp_header_parser_->RegisterRtpHeaderExtension( |
| 2341 | kRtpExtensionAbsoluteSendTime, id)) { |
solenberg@webrtc.org | b1f5010 | 2014-03-24 10:38:25 +0000 | [diff] [blame] | 2342 | return -1; |
wu@webrtc.org | ebdb0e3 | 2014-03-06 23:49:08 +0000 | [diff] [blame] | 2343 | } |
| 2344 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 2347 | void Channel::EnableSendTransportSequenceNumber(int id) { |
| 2348 | int ret = |
| 2349 | SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id); |
| 2350 | RTC_DCHECK_EQ(0, ret); |
| 2351 | } |
| 2352 | |
stefan | 3313ec9 | 2016-01-21 06:32:43 -0800 | [diff] [blame] | 2353 | void Channel::EnableReceiveTransportSequenceNumber(int id) { |
| 2354 | rtp_header_parser_->DeregisterRtpHeaderExtension( |
| 2355 | kRtpExtensionTransportSequenceNumber); |
| 2356 | bool ret = rtp_header_parser_->RegisterRtpHeaderExtension( |
| 2357 | kRtpExtensionTransportSequenceNumber, id); |
| 2358 | RTC_DCHECK(ret); |
| 2359 | } |
| 2360 | |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 2361 | void Channel::RegisterSenderCongestionControlObjects( |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 2362 | RtpPacketSender* rtp_packet_sender, |
| 2363 | TransportFeedbackObserver* transport_feedback_observer, |
| 2364 | PacketRouter* packet_router) { |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 2365 | RTC_DCHECK(rtp_packet_sender); |
| 2366 | RTC_DCHECK(transport_feedback_observer); |
| 2367 | RTC_DCHECK(packet_router && !packet_router_); |
| 2368 | feedback_observer_proxy_->SetTransportFeedbackObserver( |
| 2369 | transport_feedback_observer); |
| 2370 | seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router); |
| 2371 | rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender); |
| 2372 | _rtpRtcpModule->SetStorePacketsStatus(true, 600); |
Peter Boström | 3dd5d1d | 2016-02-25 16:56:48 +0100 | [diff] [blame] | 2373 | packet_router->AddRtpModule(_rtpRtcpModule.get()); |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 2374 | packet_router_ = packet_router; |
| 2375 | } |
| 2376 | |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 2377 | void Channel::RegisterReceiverCongestionControlObjects( |
| 2378 | PacketRouter* packet_router) { |
| 2379 | RTC_DCHECK(packet_router && !packet_router_); |
Peter Boström | 3dd5d1d | 2016-02-25 16:56:48 +0100 | [diff] [blame] | 2380 | packet_router->AddRtpModule(_rtpRtcpModule.get()); |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 2381 | packet_router_ = packet_router; |
| 2382 | } |
| 2383 | |
| 2384 | void Channel::ResetCongestionControlObjects() { |
| 2385 | RTC_DCHECK(packet_router_); |
| 2386 | _rtpRtcpModule->SetStorePacketsStatus(false, 600); |
| 2387 | feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr); |
| 2388 | seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr); |
Peter Boström | 3dd5d1d | 2016-02-25 16:56:48 +0100 | [diff] [blame] | 2389 | packet_router_->RemoveRtpModule(_rtpRtcpModule.get()); |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 2390 | packet_router_ = nullptr; |
| 2391 | rtp_packet_sender_proxy_->SetPacketSender(nullptr); |
| 2392 | } |
| 2393 | |
pbos@webrtc.org | d16e839 | 2014-12-19 13:49:55 +0000 | [diff] [blame] | 2394 | void Channel::SetRTCPStatus(bool enable) { |
| 2395 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2396 | "Channel::SetRTCPStatus()"); |
pbos | da903ea | 2015-10-02 02:36:56 -0700 | [diff] [blame] | 2397 | _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2400 | int Channel::GetRTCPStatus(bool& enabled) { |
pbos | da903ea | 2015-10-02 02:36:56 -0700 | [diff] [blame] | 2401 | RtcpMode method = _rtpRtcpModule->RTCP(); |
| 2402 | enabled = (method != RtcpMode::kOff); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2403 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2406 | int Channel::SetRTCP_CNAME(const char cName[256]) { |
| 2407 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2408 | "Channel::SetRTCP_CNAME()"); |
| 2409 | if (_rtpRtcpModule->SetCNAME(cName) != 0) { |
| 2410 | _engineStatisticsPtr->SetLastError( |
| 2411 | VE_RTP_RTCP_MODULE_ERROR, kTraceError, |
| 2412 | "SetRTCP_CNAME() failed to set RTCP CNAME"); |
| 2413 | return -1; |
| 2414 | } |
| 2415 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2418 | int Channel::GetRemoteRTCP_CNAME(char cName[256]) { |
| 2419 | if (cName == NULL) { |
| 2420 | _engineStatisticsPtr->SetLastError( |
| 2421 | VE_INVALID_ARGUMENT, kTraceError, |
| 2422 | "GetRemoteRTCP_CNAME() invalid CNAME input buffer"); |
| 2423 | return -1; |
| 2424 | } |
| 2425 | char cname[RTCP_CNAME_SIZE]; |
| 2426 | const uint32_t remoteSSRC = rtp_receiver_->SSRC(); |
| 2427 | if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0) { |
| 2428 | _engineStatisticsPtr->SetLastError( |
| 2429 | VE_CANNOT_RETRIEVE_CNAME, kTraceError, |
| 2430 | "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME"); |
| 2431 | return -1; |
| 2432 | } |
| 2433 | strcpy(cName, cname); |
| 2434 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2437 | int Channel::GetRemoteRTCPData(unsigned int& NTPHigh, |
| 2438 | unsigned int& NTPLow, |
| 2439 | unsigned int& timestamp, |
| 2440 | unsigned int& playoutTimestamp, |
| 2441 | unsigned int* jitter, |
| 2442 | unsigned short* fractionLost) { |
| 2443 | // --- Information from sender info in received Sender Reports |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2444 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2445 | RTCPSenderInfo senderInfo; |
| 2446 | if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0) { |
| 2447 | _engineStatisticsPtr->SetLastError( |
| 2448 | VE_RTP_RTCP_MODULE_ERROR, kTraceError, |
| 2449 | "GetRemoteRTCPData() failed to retrieve sender info for remote " |
| 2450 | "side"); |
| 2451 | return -1; |
| 2452 | } |
| 2453 | |
| 2454 | // We only utilize 12 out of 20 bytes in the sender info (ignores packet |
| 2455 | // and octet count) |
| 2456 | NTPHigh = senderInfo.NTPseconds; |
| 2457 | NTPLow = senderInfo.NTPfraction; |
| 2458 | timestamp = senderInfo.RTPtimeStamp; |
| 2459 | |
| 2460 | // --- Locally derived information |
| 2461 | |
| 2462 | // This value is updated on each incoming RTCP packet (0 when no packet |
| 2463 | // has been received) |
| 2464 | playoutTimestamp = playout_timestamp_rtcp_; |
| 2465 | |
| 2466 | if (NULL != jitter || NULL != fractionLost) { |
| 2467 | // Get all RTCP receiver report blocks that have been received on this |
| 2468 | // channel. If we receive RTP packets from a remote source we know the |
| 2469 | // remote SSRC and use the report block from him. |
| 2470 | // Otherwise use the first report block. |
| 2471 | std::vector<RTCPReportBlock> remote_stats; |
| 2472 | if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 || |
| 2473 | remote_stats.empty()) { |
| 2474 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2475 | "GetRemoteRTCPData() failed to measure statistics due" |
| 2476 | " to lack of received RTP and/or RTCP packets"); |
| 2477 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2480 | uint32_t remoteSSRC = rtp_receiver_->SSRC(); |
| 2481 | std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin(); |
| 2482 | for (; it != remote_stats.end(); ++it) { |
| 2483 | if (it->remoteSSRC == remoteSSRC) |
| 2484 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2485 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2486 | |
| 2487 | if (it == remote_stats.end()) { |
| 2488 | // If we have not received any RTCP packets from this SSRC it probably |
| 2489 | // means that we have not received any RTP packets. |
| 2490 | // Use the first received report block instead. |
| 2491 | it = remote_stats.begin(); |
| 2492 | remoteSSRC = it->remoteSSRC; |
| 2493 | } |
| 2494 | |
| 2495 | if (jitter) { |
| 2496 | *jitter = it->jitter; |
| 2497 | } |
| 2498 | |
| 2499 | if (fractionLost) { |
| 2500 | *fractionLost = it->fractionLost; |
| 2501 | } |
| 2502 | } |
| 2503 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2506 | int Channel::SendApplicationDefinedRTCPPacket( |
| 2507 | unsigned char subType, |
| 2508 | unsigned int name, |
| 2509 | const char* data, |
| 2510 | unsigned short dataLengthInBytes) { |
| 2511 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2512 | "Channel::SendApplicationDefinedRTCPPacket()"); |
| 2513 | if (!channel_state_.Get().sending) { |
| 2514 | _engineStatisticsPtr->SetLastError( |
| 2515 | VE_NOT_SENDING, kTraceError, |
| 2516 | "SendApplicationDefinedRTCPPacket() not sending"); |
| 2517 | return -1; |
| 2518 | } |
| 2519 | if (NULL == data) { |
| 2520 | _engineStatisticsPtr->SetLastError( |
| 2521 | VE_INVALID_ARGUMENT, kTraceError, |
| 2522 | "SendApplicationDefinedRTCPPacket() invalid data value"); |
| 2523 | return -1; |
| 2524 | } |
| 2525 | if (dataLengthInBytes % 4 != 0) { |
| 2526 | _engineStatisticsPtr->SetLastError( |
| 2527 | VE_INVALID_ARGUMENT, kTraceError, |
| 2528 | "SendApplicationDefinedRTCPPacket() invalid length value"); |
| 2529 | return -1; |
| 2530 | } |
| 2531 | RtcpMode status = _rtpRtcpModule->RTCP(); |
| 2532 | if (status == RtcpMode::kOff) { |
| 2533 | _engineStatisticsPtr->SetLastError( |
| 2534 | VE_RTCP_ERROR, kTraceError, |
| 2535 | "SendApplicationDefinedRTCPPacket() RTCP is disabled"); |
| 2536 | return -1; |
| 2537 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2538 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2539 | // Create and schedule the RTCP APP packet for transmission |
| 2540 | if (_rtpRtcpModule->SetRTCPApplicationSpecificData( |
| 2541 | subType, name, (const unsigned char*)data, dataLengthInBytes) != 0) { |
| 2542 | _engineStatisticsPtr->SetLastError( |
| 2543 | VE_SEND_ERROR, kTraceError, |
| 2544 | "SendApplicationDefinedRTCPPacket() failed to send RTCP packet"); |
| 2545 | return -1; |
| 2546 | } |
| 2547 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2550 | int Channel::GetRTPStatistics(unsigned int& averageJitterMs, |
| 2551 | unsigned int& maxJitterMs, |
| 2552 | unsigned int& discardedPackets) { |
| 2553 | // The jitter statistics is updated for each received RTP packet and is |
| 2554 | // based on received packets. |
| 2555 | if (_rtpRtcpModule->RTCP() == RtcpMode::kOff) { |
| 2556 | // If RTCP is off, there is no timed thread in the RTCP module regularly |
| 2557 | // generating new stats, trigger the update manually here instead. |
| 2558 | StreamStatistician* statistician = |
| 2559 | rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC()); |
| 2560 | if (statistician) { |
| 2561 | // Don't use returned statistics, use data from proxy instead so that |
| 2562 | // max jitter can be fetched atomically. |
| 2563 | RtcpStatistics s; |
| 2564 | statistician->GetStatistics(&s, true); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2565 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2566 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2567 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2568 | ChannelStatistics stats = statistics_proxy_->GetStats(); |
| 2569 | const int32_t playoutFrequency = audio_coding_->PlayoutFrequency(); |
| 2570 | if (playoutFrequency > 0) { |
| 2571 | // Scale RTP statistics given the current playout frequency |
| 2572 | maxJitterMs = stats.max_jitter / (playoutFrequency / 1000); |
| 2573 | averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000); |
| 2574 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2575 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2576 | discardedPackets = _numberOfDiscardedPackets; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2577 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2578 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
henrika@webrtc.org | 8a2fc88 | 2012-08-22 08:53:55 +0000 | [diff] [blame] | 2581 | int Channel::GetRemoteRTCPReportBlocks( |
| 2582 | std::vector<ReportBlock>* report_blocks) { |
| 2583 | if (report_blocks == NULL) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2584 | _engineStatisticsPtr->SetLastError( |
| 2585 | VE_INVALID_ARGUMENT, kTraceError, |
| 2586 | "GetRemoteRTCPReportBlock()s invalid report_blocks."); |
henrika@webrtc.org | 8a2fc88 | 2012-08-22 08:53:55 +0000 | [diff] [blame] | 2587 | return -1; |
| 2588 | } |
| 2589 | |
| 2590 | // Get the report blocks from the latest received RTCP Sender or Receiver |
| 2591 | // Report. Each element in the vector contains the sender's SSRC and a |
| 2592 | // report block according to RFC 3550. |
| 2593 | std::vector<RTCPReportBlock> rtcp_report_blocks; |
| 2594 | if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) { |
henrika@webrtc.org | 8a2fc88 | 2012-08-22 08:53:55 +0000 | [diff] [blame] | 2595 | return -1; |
| 2596 | } |
| 2597 | |
| 2598 | if (rtcp_report_blocks.empty()) |
| 2599 | return 0; |
| 2600 | |
| 2601 | std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin(); |
| 2602 | for (; it != rtcp_report_blocks.end(); ++it) { |
| 2603 | ReportBlock report_block; |
| 2604 | report_block.sender_SSRC = it->remoteSSRC; |
| 2605 | report_block.source_SSRC = it->sourceSSRC; |
| 2606 | report_block.fraction_lost = it->fractionLost; |
| 2607 | report_block.cumulative_num_packets_lost = it->cumulativeLost; |
| 2608 | report_block.extended_highest_sequence_number = it->extendedHighSeqNum; |
| 2609 | report_block.interarrival_jitter = it->jitter; |
| 2610 | report_block.last_SR_timestamp = it->lastSR; |
| 2611 | report_block.delay_since_last_SR = it->delaySinceLastSR; |
| 2612 | report_blocks->push_back(report_block); |
| 2613 | } |
| 2614 | return 0; |
| 2615 | } |
| 2616 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2617 | int Channel::GetRTPStatistics(CallStatistics& stats) { |
| 2618 | // --- RtcpStatistics |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2619 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2620 | // The jitter statistics is updated for each received RTP packet and is |
| 2621 | // based on received packets. |
| 2622 | RtcpStatistics statistics; |
| 2623 | StreamStatistician* statistician = |
| 2624 | rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC()); |
Peter Boström | 59013bc | 2016-02-12 11:35:08 +0100 | [diff] [blame] | 2625 | if (statistician) { |
| 2626 | statistician->GetStatistics(&statistics, |
| 2627 | _rtpRtcpModule->RTCP() == RtcpMode::kOff); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2628 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2629 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2630 | stats.fractionLost = statistics.fraction_lost; |
| 2631 | stats.cumulativeLost = statistics.cumulative_lost; |
| 2632 | stats.extendedMax = statistics.extended_max_sequence_number; |
| 2633 | stats.jitterSamples = statistics.jitter; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2634 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2635 | // --- RTT |
| 2636 | stats.rttMs = GetRTT(true); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2637 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2638 | // --- Data counters |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2639 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2640 | size_t bytesSent(0); |
| 2641 | uint32_t packetsSent(0); |
| 2642 | size_t bytesReceived(0); |
| 2643 | uint32_t packetsReceived(0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2644 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2645 | if (statistician) { |
| 2646 | statistician->GetDataCounters(&bytesReceived, &packetsReceived); |
| 2647 | } |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 2648 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2649 | if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) { |
| 2650 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2651 | "GetRTPStatistics() failed to retrieve RTP datacounters =>" |
| 2652 | " output will not be complete"); |
| 2653 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2654 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2655 | stats.bytesSent = bytesSent; |
| 2656 | stats.packetsSent = packetsSent; |
| 2657 | stats.bytesReceived = bytesReceived; |
| 2658 | stats.packetsReceived = packetsReceived; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2659 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2660 | // --- Timestamps |
| 2661 | { |
| 2662 | rtc::CritScope lock(&ts_stats_lock_); |
| 2663 | stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_; |
| 2664 | } |
| 2665 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
minyue@webrtc.org | c1a40a7 | 2014-05-28 09:52:06 +0000 | [diff] [blame] | 2668 | int Channel::SetCodecFECStatus(bool enable) { |
| 2669 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2670 | "Channel::SetCodecFECStatus()"); |
| 2671 | |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 2672 | if (!codec_manager_.SetCodecFEC(enable) || |
| 2673 | !codec_manager_.MakeEncoder(&rent_a_codec_, audio_coding_.get())) { |
minyue@webrtc.org | c1a40a7 | 2014-05-28 09:52:06 +0000 | [diff] [blame] | 2674 | _engineStatisticsPtr->SetLastError( |
| 2675 | VE_AUDIO_CODING_MODULE_ERROR, kTraceError, |
| 2676 | "SetCodecFECStatus() failed to set FEC state"); |
| 2677 | return -1; |
| 2678 | } |
| 2679 | return 0; |
| 2680 | } |
| 2681 | |
| 2682 | bool Channel::GetCodecFECStatus() { |
kwiberg | c8d071e | 2016-04-06 12:22:38 -0700 | [diff] [blame] | 2683 | return codec_manager_.GetStackParams()->use_codec_fec; |
minyue@webrtc.org | c1a40a7 | 2014-05-28 09:52:06 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
pwestin@webrtc.org | db24995 | 2013-06-05 15:33:20 +0000 | [diff] [blame] | 2686 | void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) { |
| 2687 | // None of these functions can fail. |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 2688 | // If pacing is enabled we always store packets. |
| 2689 | if (!pacing_enabled_) |
| 2690 | _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 2691 | rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets); |
pwestin@webrtc.org | d30859e | 2013-06-06 21:09:01 +0000 | [diff] [blame] | 2692 | if (enable) |
andrew@webrtc.org | eb524d9 | 2013-09-23 23:02:24 +0000 | [diff] [blame] | 2693 | audio_coding_->EnableNack(maxNumberOfPackets); |
pwestin@webrtc.org | d30859e | 2013-06-06 21:09:01 +0000 | [diff] [blame] | 2694 | else |
andrew@webrtc.org | eb524d9 | 2013-09-23 23:02:24 +0000 | [diff] [blame] | 2695 | audio_coding_->DisableNack(); |
pwestin@webrtc.org | db24995 | 2013-06-05 15:33:20 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
pwestin@webrtc.org | d30859e | 2013-06-06 21:09:01 +0000 | [diff] [blame] | 2698 | // Called when we are missing one or more packets. |
| 2699 | int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) { |
pwestin@webrtc.org | db24995 | 2013-06-05 15:33:20 +0000 | [diff] [blame] | 2700 | return _rtpRtcpModule->SendNACK(sequence_numbers, length); |
| 2701 | } |
| 2702 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2703 | uint32_t Channel::Demultiplex(const AudioFrame& audioFrame) { |
| 2704 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2705 | "Channel::Demultiplex()"); |
| 2706 | _audioFrame.CopyFrom(audioFrame); |
| 2707 | _audioFrame.id_ = _channelId; |
| 2708 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
xians@webrtc.org | 2f84afa | 2013-07-31 16:23:37 +0000 | [diff] [blame] | 2711 | void Channel::Demultiplex(const int16_t* audio_data, |
xians@webrtc.org | 8fff1f0 | 2013-07-31 16:27:42 +0000 | [diff] [blame] | 2712 | int sample_rate, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 2713 | size_t number_of_frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 2714 | size_t number_of_channels) { |
xians@webrtc.org | 2f84afa | 2013-07-31 16:23:37 +0000 | [diff] [blame] | 2715 | CodecInst codec; |
| 2716 | GetSendCodec(codec); |
xians@webrtc.org | 2f84afa | 2013-07-31 16:23:37 +0000 | [diff] [blame] | 2717 | |
Alejandro Luebs | cdfe20b | 2015-09-23 12:49:12 -0700 | [diff] [blame] | 2718 | // Never upsample or upmix the capture signal here. This should be done at the |
| 2719 | // end of the send chain. |
| 2720 | _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate); |
| 2721 | _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels); |
| 2722 | RemixAndResample(audio_data, number_of_frames, number_of_channels, |
| 2723 | sample_rate, &input_resampler_, &_audioFrame); |
xians@webrtc.org | 2f84afa | 2013-07-31 16:23:37 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2726 | uint32_t Channel::PrepareEncodeAndSend(int mixingFrequency) { |
| 2727 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2728 | "Channel::PrepareEncodeAndSend()"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2729 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2730 | if (_audioFrame.samples_per_channel_ == 0) { |
| 2731 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2732 | "Channel::PrepareEncodeAndSend() invalid audio frame"); |
| 2733 | return 0xFFFFFFFF; |
| 2734 | } |
| 2735 | |
| 2736 | if (channel_state_.Get().input_file_playing) { |
| 2737 | MixOrReplaceAudioWithFile(mixingFrequency); |
| 2738 | } |
| 2739 | |
solenberg | 1c2af8e | 2016-03-24 10:36:00 -0700 | [diff] [blame] | 2740 | bool is_muted = InputMute(); // Cache locally as InputMute() takes a lock. |
| 2741 | AudioFrameOperations::Mute(&_audioFrame, previous_frame_muted_, is_muted); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2742 | |
| 2743 | if (channel_state_.Get().input_external_media) { |
| 2744 | rtc::CritScope cs(&_callbackCritSect); |
| 2745 | const bool isStereo = (_audioFrame.num_channels_ == 2); |
| 2746 | if (_inputExternalMediaCallbackPtr) { |
| 2747 | _inputExternalMediaCallbackPtr->Process( |
| 2748 | _channelId, kRecordingPerChannel, (int16_t*)_audioFrame.data_, |
| 2749 | _audioFrame.samples_per_channel_, _audioFrame.sample_rate_hz_, |
| 2750 | isStereo); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2751 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2752 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2753 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2754 | if (_includeAudioLevelIndication) { |
| 2755 | size_t length = |
| 2756 | _audioFrame.samples_per_channel_ * _audioFrame.num_channels_; |
Tommi | 60c4e0a | 2016-05-26 21:35:27 +0200 | [diff] [blame] | 2757 | RTC_CHECK_LE(length, sizeof(_audioFrame.data_)); |
solenberg | 1c2af8e | 2016-03-24 10:36:00 -0700 | [diff] [blame] | 2758 | if (is_muted && previous_frame_muted_) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2759 | rms_level_.ProcessMuted(length); |
| 2760 | } else { |
| 2761 | rms_level_.Process(_audioFrame.data_, length); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2762 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2763 | } |
solenberg | 1c2af8e | 2016-03-24 10:36:00 -0700 | [diff] [blame] | 2764 | previous_frame_muted_ = is_muted; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2765 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2766 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2769 | uint32_t Channel::EncodeAndSend() { |
| 2770 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2771 | "Channel::EncodeAndSend()"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2772 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2773 | assert(_audioFrame.num_channels_ <= 2); |
| 2774 | if (_audioFrame.samples_per_channel_ == 0) { |
| 2775 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2776 | "Channel::EncodeAndSend() invalid audio frame"); |
| 2777 | return 0xFFFFFFFF; |
| 2778 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2779 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2780 | _audioFrame.id_ = _channelId; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2781 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2782 | // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2783 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2784 | // The ACM resamples internally. |
| 2785 | _audioFrame.timestamp_ = _timeStamp; |
| 2786 | // This call will trigger AudioPacketizationCallback::SendData if encoding |
| 2787 | // is done and payload is ready for packetization and transmission. |
| 2788 | // Otherwise, it will return without invoking the callback. |
| 2789 | if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0) { |
| 2790 | WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2791 | "Channel::EncodeAndSend() ACM encoding failed"); |
| 2792 | return 0xFFFFFFFF; |
| 2793 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2794 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2795 | _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_); |
| 2796 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 2799 | void Channel::DisassociateSendChannel(int channel_id) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 2800 | rtc::CritScope lock(&assoc_send_channel_lock_); |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 2801 | Channel* channel = associate_send_channel_.channel(); |
| 2802 | if (channel && channel->ChannelId() == channel_id) { |
| 2803 | // If this channel is associated with a send channel of the specified |
| 2804 | // Channel ID, disassociate with it. |
| 2805 | ChannelOwner ref(NULL); |
| 2806 | associate_send_channel_ = ref; |
| 2807 | } |
| 2808 | } |
| 2809 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 2810 | void Channel::SetRtcEventLog(RtcEventLog* event_log) { |
| 2811 | event_log_proxy_->SetEventLog(event_log); |
| 2812 | } |
| 2813 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2814 | int Channel::RegisterExternalMediaProcessing(ProcessingTypes type, |
| 2815 | VoEMediaProcess& processObject) { |
| 2816 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2817 | "Channel::RegisterExternalMediaProcessing()"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2818 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2819 | rtc::CritScope cs(&_callbackCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2820 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2821 | if (kPlaybackPerChannel == type) { |
| 2822 | if (_outputExternalMediaCallbackPtr) { |
| 2823 | _engineStatisticsPtr->SetLastError( |
| 2824 | VE_INVALID_OPERATION, kTraceError, |
| 2825 | "Channel::RegisterExternalMediaProcessing() " |
| 2826 | "output external media already enabled"); |
| 2827 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2828 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2829 | _outputExternalMediaCallbackPtr = &processObject; |
| 2830 | _outputExternalMedia = true; |
| 2831 | } else if (kRecordingPerChannel == type) { |
| 2832 | if (_inputExternalMediaCallbackPtr) { |
| 2833 | _engineStatisticsPtr->SetLastError( |
| 2834 | VE_INVALID_OPERATION, kTraceError, |
| 2835 | "Channel::RegisterExternalMediaProcessing() " |
| 2836 | "output external media already enabled"); |
| 2837 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2838 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2839 | _inputExternalMediaCallbackPtr = &processObject; |
| 2840 | channel_state_.SetInputExternalMedia(true); |
| 2841 | } |
| 2842 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2845 | int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type) { |
| 2846 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2847 | "Channel::DeRegisterExternalMediaProcessing()"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2848 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2849 | rtc::CritScope cs(&_callbackCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2850 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2851 | if (kPlaybackPerChannel == type) { |
| 2852 | if (!_outputExternalMediaCallbackPtr) { |
| 2853 | _engineStatisticsPtr->SetLastError( |
| 2854 | VE_INVALID_OPERATION, kTraceWarning, |
| 2855 | "Channel::DeRegisterExternalMediaProcessing() " |
| 2856 | "output external media already disabled"); |
| 2857 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2858 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2859 | _outputExternalMedia = false; |
| 2860 | _outputExternalMediaCallbackPtr = NULL; |
| 2861 | } else if (kRecordingPerChannel == type) { |
| 2862 | if (!_inputExternalMediaCallbackPtr) { |
| 2863 | _engineStatisticsPtr->SetLastError( |
| 2864 | VE_INVALID_OPERATION, kTraceWarning, |
| 2865 | "Channel::DeRegisterExternalMediaProcessing() " |
| 2866 | "input external media already disabled"); |
| 2867 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2868 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2869 | channel_state_.SetInputExternalMedia(false); |
| 2870 | _inputExternalMediaCallbackPtr = NULL; |
| 2871 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2872 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2873 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
roosa@google.com | 1b60ceb | 2012-12-12 23:00:29 +0000 | [diff] [blame] | 2876 | int Channel::SetExternalMixing(bool enabled) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2877 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2878 | "Channel::SetExternalMixing(enabled=%d)", enabled); |
roosa@google.com | 1b60ceb | 2012-12-12 23:00:29 +0000 | [diff] [blame] | 2879 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2880 | if (channel_state_.Get().playing) { |
| 2881 | _engineStatisticsPtr->SetLastError( |
| 2882 | VE_INVALID_OPERATION, kTraceError, |
| 2883 | "Channel::SetExternalMixing() " |
| 2884 | "external mixing cannot be changed while playing."); |
| 2885 | return -1; |
| 2886 | } |
roosa@google.com | 1b60ceb | 2012-12-12 23:00:29 +0000 | [diff] [blame] | 2887 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2888 | _externalMixing = enabled; |
roosa@google.com | 1b60ceb | 2012-12-12 23:00:29 +0000 | [diff] [blame] | 2889 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2890 | return 0; |
roosa@google.com | 1b60ceb | 2012-12-12 23:00:29 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2893 | int Channel::GetNetworkStatistics(NetworkStatistics& stats) { |
| 2894 | return audio_coding_->GetNetworkStatistics(&stats); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
wu@webrtc.org | 24301a6 | 2013-12-13 19:17:43 +0000 | [diff] [blame] | 2897 | void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const { |
| 2898 | audio_coding_->GetDecodingCallStatistics(stats); |
| 2899 | } |
| 2900 | |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 2901 | bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms, |
| 2902 | int* playout_buffer_delay_ms) const { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 2903 | rtc::CritScope lock(&video_sync_lock_); |
henrik.lundin | b3f1c5d | 2016-08-22 15:39:53 -0700 | [diff] [blame] | 2904 | *jitter_buffer_delay_ms = audio_coding_->FilteredCurrentDelayMs(); |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 2905 | *playout_buffer_delay_ms = playout_delay_ms_; |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 2906 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
solenberg | 358057b | 2015-11-27 10:46:42 -0800 | [diff] [blame] | 2909 | uint32_t Channel::GetDelayEstimate() const { |
| 2910 | int jitter_buffer_delay_ms = 0; |
| 2911 | int playout_buffer_delay_ms = 0; |
| 2912 | GetDelayEstimate(&jitter_buffer_delay_ms, &playout_buffer_delay_ms); |
| 2913 | return jitter_buffer_delay_ms + playout_buffer_delay_ms; |
| 2914 | } |
| 2915 | |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 2916 | int Channel::LeastRequiredDelayMs() const { |
| 2917 | return audio_coding_->LeastRequiredDelayMs(); |
| 2918 | } |
| 2919 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2920 | int Channel::SetMinimumPlayoutDelay(int delayMs) { |
| 2921 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2922 | "Channel::SetMinimumPlayoutDelay()"); |
| 2923 | if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) || |
| 2924 | (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) { |
| 2925 | _engineStatisticsPtr->SetLastError( |
| 2926 | VE_INVALID_ARGUMENT, kTraceError, |
| 2927 | "SetMinimumPlayoutDelay() invalid min delay"); |
| 2928 | return -1; |
| 2929 | } |
| 2930 | if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) { |
| 2931 | _engineStatisticsPtr->SetLastError( |
| 2932 | VE_AUDIO_CODING_MODULE_ERROR, kTraceError, |
| 2933 | "SetMinimumPlayoutDelay() failed to set min playout delay"); |
| 2934 | return -1; |
| 2935 | } |
| 2936 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 2939 | int Channel::GetPlayoutTimestamp(unsigned int& timestamp) { |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 2940 | uint32_t playout_timestamp_rtp = 0; |
| 2941 | { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 2942 | rtc::CritScope lock(&video_sync_lock_); |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 2943 | playout_timestamp_rtp = playout_timestamp_rtp_; |
| 2944 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2945 | if (playout_timestamp_rtp == 0) { |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 2946 | _engineStatisticsPtr->SetLastError( |
skvlad | 4c0536b | 2016-07-07 13:06:26 -0700 | [diff] [blame] | 2947 | VE_CANNOT_RETRIEVE_VALUE, kTraceStateInfo, |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 2948 | "GetPlayoutTimestamp() failed to retrieve timestamp"); |
| 2949 | return -1; |
| 2950 | } |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 2951 | timestamp = playout_timestamp_rtp; |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 2952 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
pbos@webrtc.org | d16e839 | 2014-12-19 13:49:55 +0000 | [diff] [blame] | 2955 | int Channel::SetInitTimestamp(unsigned int timestamp) { |
| 2956 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2957 | "Channel::SetInitTimestamp()"); |
pbos@webrtc.org | d16e839 | 2014-12-19 13:49:55 +0000 | [diff] [blame] | 2958 | if (channel_state_.Get().sending) { |
| 2959 | _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError, |
| 2960 | "SetInitTimestamp() already sending"); |
| 2961 | return -1; |
| 2962 | } |
| 2963 | _rtpRtcpModule->SetStartTimestamp(timestamp); |
| 2964 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
pbos@webrtc.org | d16e839 | 2014-12-19 13:49:55 +0000 | [diff] [blame] | 2967 | int Channel::SetInitSequenceNumber(short sequenceNumber) { |
| 2968 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2969 | "Channel::SetInitSequenceNumber()"); |
| 2970 | if (channel_state_.Get().sending) { |
| 2971 | _engineStatisticsPtr->SetLastError( |
| 2972 | VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending"); |
| 2973 | return -1; |
| 2974 | } |
| 2975 | _rtpRtcpModule->SetSequenceNumber(sequenceNumber); |
| 2976 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2979 | int Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule, |
| 2980 | RtpReceiver** rtp_receiver) const { |
| 2981 | *rtpRtcpModule = _rtpRtcpModule.get(); |
| 2982 | *rtp_receiver = rtp_receiver_.get(); |
| 2983 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
andrew@webrtc.org | e59a0ac | 2012-05-08 17:12:40 +0000 | [diff] [blame] | 2986 | // TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use |
| 2987 | // a shared helper. |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2988 | int32_t Channel::MixOrReplaceAudioWithFile(int mixingFrequency) { |
kwiberg | b7f89d6 | 2016-02-17 10:04:18 -0800 | [diff] [blame] | 2989 | std::unique_ptr<int16_t[]> fileBuffer(new int16_t[640]); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2990 | size_t fileSamples(0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2991 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2992 | { |
| 2993 | rtc::CritScope cs(&_fileCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 2994 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 2995 | if (!input_file_player_) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 2996 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 2997 | "Channel::MixOrReplaceAudioWithFile() fileplayer" |
| 2998 | " doesnt exist"); |
| 2999 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 3002 | if (input_file_player_->Get10msAudioFromFile(fileBuffer.get(), &fileSamples, |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 3003 | mixingFrequency) == -1) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3004 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 3005 | "Channel::MixOrReplaceAudioWithFile() file mixing " |
| 3006 | "failed"); |
| 3007 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3008 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3009 | if (fileSamples == 0) { |
| 3010 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 3011 | "Channel::MixOrReplaceAudioWithFile() file is ended"); |
| 3012 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3013 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | assert(_audioFrame.samples_per_channel_ == fileSamples); |
| 3017 | |
| 3018 | if (_mixFileWithMicrophone) { |
| 3019 | // Currently file stream is always mono. |
| 3020 | // TODO(xians): Change the code when FilePlayer supports real stereo. |
| 3021 | MixWithSat(_audioFrame.data_, _audioFrame.num_channels_, fileBuffer.get(), |
| 3022 | 1, fileSamples); |
| 3023 | } else { |
| 3024 | // Replace ACM audio with file. |
| 3025 | // Currently file stream is always mono. |
| 3026 | // TODO(xians): Change the code when FilePlayer supports real stereo. |
| 3027 | _audioFrame.UpdateFrame( |
| 3028 | _channelId, 0xFFFFFFFF, fileBuffer.get(), fileSamples, mixingFrequency, |
| 3029 | AudioFrame::kNormalSpeech, AudioFrame::kVadUnknown, 1); |
| 3030 | } |
| 3031 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3034 | int32_t Channel::MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency) { |
| 3035 | assert(mixingFrequency <= 48000); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3036 | |
kwiberg | b7f89d6 | 2016-02-17 10:04:18 -0800 | [diff] [blame] | 3037 | std::unique_ptr<int16_t[]> fileBuffer(new int16_t[960]); |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3038 | size_t fileSamples(0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3039 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3040 | { |
| 3041 | rtc::CritScope cs(&_fileCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3042 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 3043 | if (!output_file_player_) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3044 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 3045 | "Channel::MixAudioWithFile() file mixing failed"); |
| 3046 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3047 | } |
| 3048 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3049 | // We should get the frequency we ask for. |
kwiberg | 4ec01d9 | 2016-08-22 08:43:54 -0700 | [diff] [blame] | 3050 | if (output_file_player_->Get10msAudioFromFile( |
| 3051 | fileBuffer.get(), &fileSamples, mixingFrequency) == -1) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3052 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 3053 | "Channel::MixAudioWithFile() file mixing failed"); |
| 3054 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3055 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3056 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3057 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3058 | if (audioFrame.samples_per_channel_ == fileSamples) { |
| 3059 | // Currently file stream is always mono. |
| 3060 | // TODO(xians): Change the code when FilePlayer supports real stereo. |
| 3061 | MixWithSat(audioFrame.data_, audioFrame.num_channels_, fileBuffer.get(), 1, |
| 3062 | fileSamples); |
| 3063 | } else { |
| 3064 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 3065 | "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS |
| 3066 | ") != " |
| 3067 | "fileSamples(%" PRIuS ")", |
| 3068 | audioFrame.samples_per_channel_, fileSamples); |
| 3069 | return -1; |
| 3070 | } |
| 3071 | |
| 3072 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 3075 | void Channel::UpdatePlayoutTimestamp(bool rtcp) { |
henrik.lundin | 96bd502 | 2016-04-06 04:13:56 -0700 | [diff] [blame] | 3076 | jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp(); |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 3077 | |
henrik.lundin | 96bd502 | 2016-04-06 04:13:56 -0700 | [diff] [blame] | 3078 | if (!jitter_buffer_playout_timestamp_) { |
| 3079 | // This can happen if this channel has not received any RTP packets. In |
| 3080 | // this case, NetEq is not capable of computing a playout timestamp. |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 3081 | return; |
| 3082 | } |
| 3083 | |
| 3084 | uint16_t delay_ms = 0; |
| 3085 | if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) { |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3086 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 3087 | "Channel::UpdatePlayoutTimestamp() failed to read playout" |
| 3088 | " delay from the ADM"); |
| 3089 | _engineStatisticsPtr->SetLastError( |
| 3090 | VE_CANNOT_RETRIEVE_VALUE, kTraceError, |
| 3091 | "UpdatePlayoutTimestamp() failed to retrieve playout delay"); |
| 3092 | return; |
| 3093 | } |
| 3094 | |
henrik.lundin | 96bd502 | 2016-04-06 04:13:56 -0700 | [diff] [blame] | 3095 | RTC_DCHECK(jitter_buffer_playout_timestamp_); |
| 3096 | uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_; |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 3097 | |
| 3098 | // Remove the playout delay. |
henrik.lundin | 96bd502 | 2016-04-06 04:13:56 -0700 | [diff] [blame] | 3099 | playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000)); |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 3100 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3101 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId, _channelId), |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 3102 | "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu", |
henrik.lundin | 96bd502 | 2016-04-06 04:13:56 -0700 | [diff] [blame] | 3103 | playout_timestamp); |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 3104 | |
| 3105 | { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 3106 | rtc::CritScope lock(&video_sync_lock_); |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 3107 | if (rtcp) { |
henrik.lundin | 96bd502 | 2016-04-06 04:13:56 -0700 | [diff] [blame] | 3108 | playout_timestamp_rtcp_ = playout_timestamp; |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 3109 | } else { |
henrik.lundin | 96bd502 | 2016-04-06 04:13:56 -0700 | [diff] [blame] | 3110 | playout_timestamp_rtp_ = playout_timestamp; |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 3111 | } |
| 3112 | playout_delay_ms_ = delay_ms; |
| 3113 | } |
| 3114 | } |
| 3115 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3116 | void Channel::RegisterReceiveCodecsToRTPModule() { |
| 3117 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 3118 | "Channel::RegisterReceiveCodecsToRTPModule()"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3119 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3120 | CodecInst codec; |
| 3121 | const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3122 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3123 | for (int idx = 0; idx < nSupportedCodecs; idx++) { |
| 3124 | // Open up the RTP/RTCP receiver for all supported codecs |
| 3125 | if ((audio_coding_->Codec(idx, &codec) == -1) || |
| 3126 | (rtp_receiver_->RegisterReceivePayload( |
| 3127 | codec.plname, codec.pltype, codec.plfreq, codec.channels, |
| 3128 | (codec.rate < 0) ? 0 : codec.rate) == -1)) { |
| 3129 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId), |
| 3130 | "Channel::RegisterReceiveCodecsToRTPModule() unable" |
| 3131 | " to register %s (%d/%d/%" PRIuS |
| 3132 | "/%d) to RTP/RTCP " |
| 3133 | "receiver", |
| 3134 | codec.plname, codec.pltype, codec.plfreq, codec.channels, |
| 3135 | codec.rate); |
| 3136 | } else { |
| 3137 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), |
| 3138 | "Channel::RegisterReceiveCodecsToRTPModule() %s " |
| 3139 | "(%d/%d/%" PRIuS |
| 3140 | "/%d) has been added to the RTP/RTCP " |
| 3141 | "receiver", |
| 3142 | codec.plname, codec.pltype, codec.plfreq, codec.channels, |
| 3143 | codec.rate); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3144 | } |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3145 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3148 | int Channel::SetSendRtpHeaderExtension(bool enable, |
| 3149 | RTPExtensionType type, |
wu@webrtc.org | ebdb0e3 | 2014-03-06 23:49:08 +0000 | [diff] [blame] | 3150 | unsigned char id) { |
| 3151 | int error = 0; |
| 3152 | _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type); |
| 3153 | if (enable) { |
| 3154 | error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id); |
| 3155 | } |
| 3156 | return error; |
| 3157 | } |
minyue@webrtc.org | c1a40a7 | 2014-05-28 09:52:06 +0000 | [diff] [blame] | 3158 | |
henrik.lundin | b3e3001 | 2016-08-31 14:09:51 -0700 | [diff] [blame] | 3159 | int32_t Channel::GetPlayoutFrequency() const { |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 3160 | int32_t playout_frequency = audio_coding_->PlayoutFrequency(); |
| 3161 | CodecInst current_recive_codec; |
| 3162 | if (audio_coding_->ReceiveCodec(¤t_recive_codec) == 0) { |
| 3163 | if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) { |
| 3164 | // Even though the actual sampling rate for G.722 audio is |
| 3165 | // 16,000 Hz, the RTP clock rate for the G722 payload format is |
| 3166 | // 8,000 Hz because that value was erroneously assigned in |
| 3167 | // RFC 1890 and must remain unchanged for backward compatibility. |
| 3168 | playout_frequency = 8000; |
| 3169 | } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) { |
| 3170 | // We are resampling Opus internally to 32,000 Hz until all our |
| 3171 | // DSP routines can operate at 48,000 Hz, but the RTP clock |
| 3172 | // rate for the Opus payload format is standardized to 48,000 Hz, |
| 3173 | // because that is the maximum supported decoding sampling rate. |
| 3174 | playout_frequency = 48000; |
| 3175 | } |
| 3176 | } |
| 3177 | return playout_frequency; |
| 3178 | } |
| 3179 | |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 3180 | int64_t Channel::GetRTT(bool allow_associate_channel) const { |
pbos | da903ea | 2015-10-02 02:36:56 -0700 | [diff] [blame] | 3181 | RtcpMode method = _rtpRtcpModule->RTCP(); |
| 3182 | if (method == RtcpMode::kOff) { |
minyue@webrtc.org | 2b58a44 | 2014-09-11 07:51:53 +0000 | [diff] [blame] | 3183 | return 0; |
| 3184 | } |
| 3185 | std::vector<RTCPReportBlock> report_blocks; |
| 3186 | _rtpRtcpModule->RemoteRTCPStat(&report_blocks); |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 3187 | |
| 3188 | int64_t rtt = 0; |
minyue@webrtc.org | 2b58a44 | 2014-09-11 07:51:53 +0000 | [diff] [blame] | 3189 | if (report_blocks.empty()) { |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 3190 | if (allow_associate_channel) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 3191 | rtc::CritScope lock(&assoc_send_channel_lock_); |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 3192 | Channel* channel = associate_send_channel_.channel(); |
| 3193 | // Tries to get RTT from an associated channel. This is important for |
| 3194 | // receive-only channels. |
| 3195 | if (channel) { |
| 3196 | // To prevent infinite recursion and deadlock, calling GetRTT of |
| 3197 | // associate channel should always use "false" for argument: |
| 3198 | // |allow_associate_channel|. |
| 3199 | rtt = channel->GetRTT(false); |
| 3200 | } |
| 3201 | } |
| 3202 | return rtt; |
minyue@webrtc.org | 2b58a44 | 2014-09-11 07:51:53 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | uint32_t remoteSSRC = rtp_receiver_->SSRC(); |
| 3206 | std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin(); |
| 3207 | for (; it != report_blocks.end(); ++it) { |
| 3208 | if (it->remoteSSRC == remoteSSRC) |
| 3209 | break; |
| 3210 | } |
| 3211 | if (it == report_blocks.end()) { |
| 3212 | // We have not received packets with SSRC matching the report blocks. |
| 3213 | // To calculate RTT we try with the SSRC of the first report block. |
| 3214 | // This is very important for send-only channels where we don't know |
| 3215 | // the SSRC of the other end. |
| 3216 | remoteSSRC = report_blocks[0].remoteSSRC; |
| 3217 | } |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 3218 | |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 3219 | int64_t avg_rtt = 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3220 | int64_t max_rtt = 0; |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 3221 | int64_t min_rtt = 0; |
kwiberg | 55b97fe | 2016-01-28 05:22:45 -0800 | [diff] [blame] | 3222 | if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt) != |
| 3223 | 0) { |
minyue@webrtc.org | 2b58a44 | 2014-09-11 07:51:53 +0000 | [diff] [blame] | 3224 | return 0; |
| 3225 | } |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 3226 | return rtt; |
minyue@webrtc.org | 2b58a44 | 2014-09-11 07:51:53 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 3229 | } // namespace voe |
| 3230 | } // namespace webrtc |