Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 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 | |
| 11 | #include "audio/channel_receive.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <utility> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "absl/memory/memory.h" |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 21 | #include "audio/audio_level.h" |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 22 | #include "audio/channel_send.h" |
| 23 | #include "audio/utility/audio_frame_operations.h" |
| 24 | #include "logging/rtc_event_log/events/rtc_event_audio_playout.h" |
| 25 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 26 | #include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h" |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 27 | #include "modules/audio_coding/include/audio_coding_module.h" |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 28 | #include "modules/audio_device/include/audio_device.h" |
| 29 | #include "modules/pacing/packet_router.h" |
| 30 | #include "modules/rtp_rtcp/include/receive_statistics.h" |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 31 | #include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h" |
| 32 | #include "modules/rtp_rtcp/include/rtp_rtcp.h" |
| 33 | #include "modules/rtp_rtcp/source/contributing_sources.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 34 | #include "modules/rtp_rtcp/source/rtp_header_extensions.h" |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 35 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
Danil Chapovalov | 2a977cf | 2018-12-04 17:03:52 | [diff] [blame] | 36 | #include "modules/rtp_rtcp/source/rtp_rtcp_config.h" |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 37 | #include "modules/utility/include/process_thread.h" |
| 38 | #include "rtc_base/checks.h" |
| 39 | #include "rtc_base/criticalsection.h" |
| 40 | #include "rtc_base/format_macros.h" |
| 41 | #include "rtc_base/location.h" |
| 42 | #include "rtc_base/logging.h" |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 43 | #include "rtc_base/numerics/safe_minmax.h" |
| 44 | #include "rtc_base/race_checker.h" |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 45 | #include "rtc_base/thread_checker.h" |
| 46 | #include "rtc_base/timeutils.h" |
| 47 | #include "system_wrappers/include/metrics.h" |
| 48 | |
| 49 | namespace webrtc { |
| 50 | namespace voe { |
| 51 | |
| 52 | namespace { |
| 53 | |
| 54 | constexpr double kAudioSampleDurationSeconds = 0.01; |
| 55 | constexpr int64_t kMaxRetransmissionWindowMs = 1000; |
| 56 | constexpr int64_t kMinRetransmissionWindowMs = 30; |
| 57 | |
| 58 | // Video Sync. |
| 59 | constexpr int kVoiceEngineMinMinPlayoutDelayMs = 0; |
| 60 | constexpr int kVoiceEngineMaxMinPlayoutDelayMs = 10000; |
| 61 | |
Niels Möller | 7d76a31 | 2018-10-26 10:57:07 | [diff] [blame] | 62 | webrtc::FrameType WebrtcFrameTypeForMediaTransportFrameType( |
| 63 | MediaTransportEncodedAudioFrame::FrameType frame_type) { |
| 64 | switch (frame_type) { |
| 65 | case MediaTransportEncodedAudioFrame::FrameType::kSpeech: |
| 66 | return kAudioFrameSpeech; |
| 67 | break; |
| 68 | |
| 69 | case MediaTransportEncodedAudioFrame::FrameType:: |
| 70 | kDiscountinuousTransmission: |
| 71 | return kAudioFrameCN; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | WebRtcRTPHeader CreateWebrtcRTPHeaderForMediaTransportFrame( |
| 77 | const MediaTransportEncodedAudioFrame& frame, |
| 78 | uint64_t channel_id) { |
| 79 | webrtc::WebRtcRTPHeader webrtc_header = {}; |
| 80 | webrtc_header.header.payloadType = frame.payload_type(); |
| 81 | webrtc_header.header.payload_type_frequency = frame.sampling_rate_hz(); |
| 82 | webrtc_header.header.timestamp = frame.starting_sample_index(); |
| 83 | webrtc_header.header.sequenceNumber = frame.sequence_number(); |
| 84 | |
| 85 | webrtc_header.frameType = |
| 86 | WebrtcFrameTypeForMediaTransportFrameType(frame.frame_type()); |
| 87 | |
| 88 | webrtc_header.header.ssrc = static_cast<uint32_t>(channel_id); |
| 89 | |
| 90 | // The rest are initialized by the RTPHeader constructor. |
| 91 | return webrtc_header; |
| 92 | } |
| 93 | |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 94 | class ChannelReceive : public ChannelReceiveInterface, |
| 95 | public MediaTransportAudioSinkInterface { |
| 96 | public: |
| 97 | // Used for receive streams. |
| 98 | ChannelReceive(ProcessThread* module_process_thread, |
| 99 | AudioDeviceModule* audio_device_module, |
| 100 | MediaTransportInterface* media_transport, |
| 101 | Transport* rtcp_send_transport, |
| 102 | RtcEventLog* rtc_event_log, |
| 103 | uint32_t remote_ssrc, |
| 104 | size_t jitter_buffer_max_packets, |
| 105 | bool jitter_buffer_fast_playout, |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 106 | int jitter_buffer_min_delay_ms, |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 107 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory, |
| 108 | absl::optional<AudioCodecPairId> codec_pair_id, |
| 109 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor, |
| 110 | const webrtc::CryptoOptions& crypto_options); |
| 111 | ~ChannelReceive() override; |
| 112 | |
| 113 | void SetSink(AudioSinkInterface* sink) override; |
| 114 | |
| 115 | void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) override; |
| 116 | |
| 117 | // API methods |
| 118 | |
| 119 | void StartPlayout() override; |
| 120 | void StopPlayout() override; |
| 121 | |
| 122 | // Codecs |
Fredrik Solenberg | f693bfa | 2018-12-11 11:22:10 | [diff] [blame] | 123 | absl::optional<std::pair<int, SdpAudioFormat>> |
| 124 | GetReceiveCodec() const override; |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 125 | |
| 126 | bool ReceivedRTCPPacket(const uint8_t* data, size_t length) override; |
| 127 | |
| 128 | // RtpPacketSinkInterface. |
| 129 | void OnRtpPacket(const RtpPacketReceived& packet) override; |
| 130 | |
| 131 | // Muting, Volume and Level. |
| 132 | void SetChannelOutputVolumeScaling(float scaling) override; |
| 133 | int GetSpeechOutputLevelFullRange() const override; |
| 134 | // See description of "totalAudioEnergy" in the WebRTC stats spec: |
| 135 | // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy |
| 136 | double GetTotalOutputEnergy() const override; |
| 137 | double GetTotalOutputDuration() const override; |
| 138 | |
| 139 | // Stats. |
| 140 | NetworkStatistics GetNetworkStatistics() const override; |
| 141 | AudioDecodingCallStats GetDecodingCallStatistics() const override; |
| 142 | |
| 143 | // Audio+Video Sync. |
| 144 | uint32_t GetDelayEstimate() const override; |
| 145 | void SetMinimumPlayoutDelay(int delayMs) override; |
| 146 | uint32_t GetPlayoutTimestamp() const override; |
| 147 | |
| 148 | // Produces the transport-related timestamps; current_delay_ms is left unset. |
| 149 | absl::optional<Syncable::Info> GetSyncInfo() const override; |
| 150 | |
| 151 | // RTP+RTCP |
| 152 | void SetLocalSSRC(unsigned int ssrc) override; |
| 153 | |
| 154 | void RegisterReceiverCongestionControlObjects( |
| 155 | PacketRouter* packet_router) override; |
| 156 | void ResetReceiverCongestionControlObjects() override; |
| 157 | |
| 158 | CallReceiveStatistics GetRTCPStatistics() const override; |
| 159 | void SetNACKStatus(bool enable, int maxNumberOfPackets) override; |
| 160 | |
| 161 | AudioMixer::Source::AudioFrameInfo GetAudioFrameWithInfo( |
| 162 | int sample_rate_hz, |
| 163 | AudioFrame* audio_frame) override; |
| 164 | |
| 165 | int PreferredSampleRate() const override; |
| 166 | |
| 167 | // Associate to a send channel. |
| 168 | // Used for obtaining RTT for a receive-only channel. |
Niels Möller | dced9f6 | 2018-11-19 09:27:07 | [diff] [blame] | 169 | void SetAssociatedSendChannel(const ChannelSendInterface* channel) override; |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 170 | |
| 171 | std::vector<RtpSource> GetSources() const override; |
| 172 | |
| 173 | private: |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 174 | bool ReceivePacket(const uint8_t* packet, |
| 175 | size_t packet_length, |
| 176 | const RTPHeader& header); |
| 177 | int ResendPackets(const uint16_t* sequence_numbers, int length); |
| 178 | void UpdatePlayoutTimestamp(bool rtcp); |
| 179 | |
| 180 | int GetRtpTimestampRateHz() const; |
| 181 | int64_t GetRTT() const; |
| 182 | |
| 183 | // MediaTransportAudioSinkInterface override; |
| 184 | void OnData(uint64_t channel_id, |
| 185 | MediaTransportEncodedAudioFrame frame) override; |
| 186 | |
| 187 | int32_t OnReceivedPayloadData(const uint8_t* payloadData, |
| 188 | size_t payloadSize, |
| 189 | const WebRtcRTPHeader* rtpHeader); |
| 190 | |
Fredrik Solenberg | c5e8be3 | 2018-11-19 10:56:13 | [diff] [blame] | 191 | bool Playing() const { |
| 192 | rtc::CritScope lock(&playing_lock_); |
| 193 | return playing_; |
| 194 | } |
| 195 | |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 196 | // Thread checkers document and lock usage of some methods to specific threads |
| 197 | // we know about. The goal is to eventually split up voe::ChannelReceive into |
| 198 | // parts with single-threaded semantics, and thereby reduce the need for |
| 199 | // locks. |
| 200 | rtc::ThreadChecker worker_thread_checker_; |
| 201 | rtc::ThreadChecker module_process_thread_checker_; |
| 202 | // Methods accessed from audio and video threads are checked for sequential- |
| 203 | // only access. We don't necessarily own and control these threads, so thread |
| 204 | // checkers cannot be used. E.g. Chromium may transfer "ownership" from one |
| 205 | // audio thread to another, but access is still sequential. |
| 206 | rtc::RaceChecker audio_thread_race_checker_; |
| 207 | rtc::RaceChecker video_capture_thread_race_checker_; |
| 208 | rtc::CriticalSection _callbackCritSect; |
| 209 | rtc::CriticalSection volume_settings_critsect_; |
| 210 | |
Fredrik Solenberg | c5e8be3 | 2018-11-19 10:56:13 | [diff] [blame] | 211 | rtc::CriticalSection playing_lock_; |
| 212 | bool playing_ RTC_GUARDED_BY(&playing_lock_) = false; |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 213 | |
| 214 | RtcEventLog* const event_log_; |
| 215 | |
| 216 | // Indexed by payload type. |
| 217 | std::map<uint8_t, int> payload_type_frequencies_; |
| 218 | |
| 219 | std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_; |
| 220 | std::unique_ptr<RtpRtcp> _rtpRtcpModule; |
| 221 | const uint32_t remote_ssrc_; |
| 222 | |
| 223 | // Info for GetSources and GetSyncInfo is updated on network or worker thread, |
| 224 | // queried on the worker thread. |
| 225 | rtc::CriticalSection rtp_sources_lock_; |
| 226 | ContributingSources contributing_sources_ RTC_GUARDED_BY(&rtp_sources_lock_); |
| 227 | absl::optional<uint32_t> last_received_rtp_timestamp_ |
| 228 | RTC_GUARDED_BY(&rtp_sources_lock_); |
| 229 | absl::optional<int64_t> last_received_rtp_system_time_ms_ |
| 230 | RTC_GUARDED_BY(&rtp_sources_lock_); |
| 231 | absl::optional<uint8_t> last_received_rtp_audio_level_ |
| 232 | RTC_GUARDED_BY(&rtp_sources_lock_); |
| 233 | |
| 234 | std::unique_ptr<AudioCodingModule> audio_coding_; |
| 235 | AudioSinkInterface* audio_sink_ = nullptr; |
| 236 | AudioLevel _outputAudioLevel; |
| 237 | |
| 238 | RemoteNtpTimeEstimator ntp_estimator_ RTC_GUARDED_BY(ts_stats_lock_); |
| 239 | |
| 240 | // Timestamp of the audio pulled from NetEq. |
| 241 | absl::optional<uint32_t> jitter_buffer_playout_timestamp_; |
| 242 | |
| 243 | rtc::CriticalSection video_sync_lock_; |
| 244 | uint32_t playout_timestamp_rtp_ RTC_GUARDED_BY(video_sync_lock_); |
| 245 | uint32_t playout_delay_ms_ RTC_GUARDED_BY(video_sync_lock_); |
| 246 | |
| 247 | rtc::CriticalSection ts_stats_lock_; |
| 248 | |
| 249 | std::unique_ptr<rtc::TimestampWrapAroundHandler> rtp_ts_wraparound_handler_; |
| 250 | // The rtp timestamp of the first played out audio frame. |
| 251 | int64_t capture_start_rtp_time_stamp_; |
| 252 | // The capture ntp time (in local timebase) of the first played out audio |
| 253 | // frame. |
| 254 | int64_t capture_start_ntp_time_ms_ RTC_GUARDED_BY(ts_stats_lock_); |
| 255 | |
| 256 | // uses |
| 257 | ProcessThread* _moduleProcessThreadPtr; |
| 258 | AudioDeviceModule* _audioDeviceModulePtr; |
| 259 | float _outputGain RTC_GUARDED_BY(volume_settings_critsect_); |
| 260 | |
| 261 | // An associated send channel. |
| 262 | rtc::CriticalSection assoc_send_channel_lock_; |
Niels Möller | dced9f6 | 2018-11-19 09:27:07 | [diff] [blame] | 263 | const ChannelSendInterface* associated_send_channel_ |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 264 | RTC_GUARDED_BY(assoc_send_channel_lock_); |
| 265 | |
| 266 | PacketRouter* packet_router_ = nullptr; |
| 267 | |
| 268 | rtc::ThreadChecker construction_thread_; |
| 269 | |
| 270 | MediaTransportInterface* const media_transport_; |
| 271 | |
| 272 | // E2EE Audio Frame Decryption |
| 273 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_; |
| 274 | webrtc::CryptoOptions crypto_options_; |
| 275 | }; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 276 | |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 277 | int32_t ChannelReceive::OnReceivedPayloadData( |
| 278 | const uint8_t* payloadData, |
| 279 | size_t payloadSize, |
| 280 | const WebRtcRTPHeader* rtpHeader) { |
Niels Möller | 7d76a31 | 2018-10-26 10:57:07 | [diff] [blame] | 281 | // We should not be receiving any RTP packets if media_transport is set. |
| 282 | RTC_CHECK(!media_transport_); |
| 283 | |
Fredrik Solenberg | c5e8be3 | 2018-11-19 10:56:13 | [diff] [blame] | 284 | if (!Playing()) { |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 285 | // Avoid inserting into NetEQ when we are not playing. Count the |
| 286 | // packet as discarded. |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | // Push the incoming payload (parsed and ready for decoding) into the ACM |
| 291 | if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) != |
| 292 | 0) { |
| 293 | RTC_DLOG(LS_ERROR) << "ChannelReceive::OnReceivedPayloadData() unable to " |
| 294 | "push data to the ACM"; |
| 295 | return -1; |
| 296 | } |
| 297 | |
| 298 | int64_t round_trip_time = 0; |
| 299 | _rtpRtcpModule->RTT(remote_ssrc_, &round_trip_time, NULL, NULL, NULL); |
| 300 | |
| 301 | std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time); |
| 302 | if (!nack_list.empty()) { |
| 303 | // Can't use nack_list.data() since it's not supported by all |
| 304 | // compilers. |
| 305 | ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size())); |
| 306 | } |
| 307 | return 0; |
| 308 | } |
| 309 | |
Niels Möller | 7d76a31 | 2018-10-26 10:57:07 | [diff] [blame] | 310 | // MediaTransportAudioSinkInterface override. |
| 311 | void ChannelReceive::OnData(uint64_t channel_id, |
| 312 | MediaTransportEncodedAudioFrame frame) { |
| 313 | RTC_CHECK(media_transport_); |
| 314 | |
Fredrik Solenberg | c5e8be3 | 2018-11-19 10:56:13 | [diff] [blame] | 315 | if (!Playing()) { |
Niels Möller | 7d76a31 | 2018-10-26 10:57:07 | [diff] [blame] | 316 | // Avoid inserting into NetEQ when we are not playing. Count the |
| 317 | // packet as discarded. |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | // Send encoded audio frame to Decoder / NetEq. |
| 322 | if (audio_coding_->IncomingPacket( |
| 323 | frame.encoded_data().data(), frame.encoded_data().size(), |
| 324 | CreateWebrtcRTPHeaderForMediaTransportFrame(frame, channel_id)) != |
| 325 | 0) { |
| 326 | RTC_DLOG(LS_ERROR) << "ChannelReceive::OnData: unable to " |
| 327 | "push data to the ACM"; |
| 328 | } |
| 329 | } |
| 330 | |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 331 | AudioMixer::Source::AudioFrameInfo ChannelReceive::GetAudioFrameWithInfo( |
| 332 | int sample_rate_hz, |
| 333 | AudioFrame* audio_frame) { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 334 | RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 335 | audio_frame->sample_rate_hz_ = sample_rate_hz; |
| 336 | |
Fredrik Solenberg | c5e8be3 | 2018-11-19 10:56:13 | [diff] [blame] | 337 | event_log_->Log(absl::make_unique<RtcEventAudioPlayout>(remote_ssrc_)); |
| 338 | |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 339 | // Get 10ms raw PCM data from the ACM (mixer limits output frequency) |
| 340 | bool muted; |
| 341 | if (audio_coding_->PlayoutData10Ms(audio_frame->sample_rate_hz_, audio_frame, |
| 342 | &muted) == -1) { |
| 343 | RTC_DLOG(LS_ERROR) |
| 344 | << "ChannelReceive::GetAudioFrame() PlayoutData10Ms() failed!"; |
| 345 | // In all likelihood, the audio in this frame is garbage. We return an |
| 346 | // error so that the audio mixer module doesn't add it to the mix. As |
| 347 | // a result, it won't be played out and the actions skipped here are |
| 348 | // irrelevant. |
| 349 | return AudioMixer::Source::AudioFrameInfo::kError; |
| 350 | } |
| 351 | |
| 352 | if (muted) { |
| 353 | // TODO(henrik.lundin): We should be able to do better than this. But we |
| 354 | // will have to go through all the cases below where the audio samples may |
| 355 | // be used, and handle the muted case in some way. |
| 356 | AudioFrameOperations::Mute(audio_frame); |
| 357 | } |
| 358 | |
| 359 | { |
| 360 | // Pass the audio buffers to an optional sink callback, before applying |
| 361 | // scaling/panning, as that applies to the mix operation. |
| 362 | // External recipients of the audio (e.g. via AudioTrack), will do their |
| 363 | // own mixing/dynamic processing. |
| 364 | rtc::CritScope cs(&_callbackCritSect); |
| 365 | if (audio_sink_) { |
| 366 | AudioSinkInterface::Data data( |
| 367 | audio_frame->data(), audio_frame->samples_per_channel_, |
| 368 | audio_frame->sample_rate_hz_, audio_frame->num_channels_, |
| 369 | audio_frame->timestamp_); |
| 370 | audio_sink_->OnData(data); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | float output_gain = 1.0f; |
| 375 | { |
| 376 | rtc::CritScope cs(&volume_settings_critsect_); |
| 377 | output_gain = _outputGain; |
| 378 | } |
| 379 | |
| 380 | // Output volume scaling |
| 381 | if (output_gain < 0.99f || output_gain > 1.01f) { |
| 382 | // TODO(solenberg): Combine with mute state - this can cause clicks! |
| 383 | AudioFrameOperations::ScaleWithSat(output_gain, audio_frame); |
| 384 | } |
| 385 | |
| 386 | // Measure audio level (0-9) |
| 387 | // TODO(henrik.lundin) Use the |muted| information here too. |
| 388 | // TODO(deadbeef): Use RmsLevel for |_outputAudioLevel| (see |
| 389 | // https://crbug.com/webrtc/7517). |
| 390 | _outputAudioLevel.ComputeLevel(*audio_frame, kAudioSampleDurationSeconds); |
| 391 | |
| 392 | if (capture_start_rtp_time_stamp_ < 0 && audio_frame->timestamp_ != 0) { |
| 393 | // The first frame with a valid rtp timestamp. |
| 394 | capture_start_rtp_time_stamp_ = audio_frame->timestamp_; |
| 395 | } |
| 396 | |
| 397 | if (capture_start_rtp_time_stamp_ >= 0) { |
| 398 | // audio_frame.timestamp_ should be valid from now on. |
| 399 | |
| 400 | // Compute elapsed time. |
| 401 | int64_t unwrap_timestamp = |
| 402 | rtp_ts_wraparound_handler_->Unwrap(audio_frame->timestamp_); |
| 403 | audio_frame->elapsed_time_ms_ = |
| 404 | (unwrap_timestamp - capture_start_rtp_time_stamp_) / |
| 405 | (GetRtpTimestampRateHz() / 1000); |
| 406 | |
| 407 | { |
| 408 | rtc::CritScope lock(&ts_stats_lock_); |
| 409 | // Compute ntp time. |
| 410 | audio_frame->ntp_time_ms_ = |
| 411 | ntp_estimator_.Estimate(audio_frame->timestamp_); |
| 412 | // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received. |
| 413 | if (audio_frame->ntp_time_ms_ > 0) { |
| 414 | // Compute |capture_start_ntp_time_ms_| so that |
| 415 | // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_| |
| 416 | capture_start_ntp_time_ms_ = |
| 417 | audio_frame->ntp_time_ms_ - audio_frame->elapsed_time_ms_; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | { |
| 423 | RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.TargetJitterBufferDelayMs", |
| 424 | audio_coding_->TargetDelayMs()); |
| 425 | const int jitter_buffer_delay = audio_coding_->FilteredCurrentDelayMs(); |
| 426 | rtc::CritScope lock(&video_sync_lock_); |
| 427 | RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverDelayEstimateMs", |
| 428 | jitter_buffer_delay + playout_delay_ms_); |
| 429 | RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverJitterBufferDelayMs", |
| 430 | jitter_buffer_delay); |
| 431 | RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverDeviceDelayMs", |
| 432 | playout_delay_ms_); |
| 433 | } |
| 434 | |
| 435 | return muted ? AudioMixer::Source::AudioFrameInfo::kMuted |
| 436 | : AudioMixer::Source::AudioFrameInfo::kNormal; |
| 437 | } |
| 438 | |
| 439 | int ChannelReceive::PreferredSampleRate() const { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 440 | RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 441 | // Return the bigger of playout and receive frequency in the ACM. |
| 442 | return std::max(audio_coding_->ReceiveFrequency(), |
| 443 | audio_coding_->PlayoutFrequency()); |
| 444 | } |
| 445 | |
| 446 | ChannelReceive::ChannelReceive( |
| 447 | ProcessThread* module_process_thread, |
| 448 | AudioDeviceModule* audio_device_module, |
Niels Möller | 7d76a31 | 2018-10-26 10:57:07 | [diff] [blame] | 449 | MediaTransportInterface* media_transport, |
Niels Möller | ae4237e | 2018-10-05 09:28:38 | [diff] [blame] | 450 | Transport* rtcp_send_transport, |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 451 | RtcEventLog* rtc_event_log, |
| 452 | uint32_t remote_ssrc, |
| 453 | size_t jitter_buffer_max_packets, |
| 454 | bool jitter_buffer_fast_playout, |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 455 | int jitter_buffer_min_delay_ms, |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 456 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory, |
Benjamin Wright | 84583f6 | 2018-10-04 21:22:34 | [diff] [blame] | 457 | absl::optional<AudioCodecPairId> codec_pair_id, |
Benjamin Wright | 78410ad | 2018-10-25 16:52:57 | [diff] [blame] | 458 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor, |
Benjamin Wright | bfb444c | 2018-10-15 17:20:24 | [diff] [blame] | 459 | const webrtc::CryptoOptions& crypto_options) |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 460 | : event_log_(rtc_event_log), |
| 461 | rtp_receive_statistics_( |
| 462 | ReceiveStatistics::Create(Clock::GetRealTimeClock())), |
| 463 | remote_ssrc_(remote_ssrc), |
| 464 | _outputAudioLevel(), |
| 465 | ntp_estimator_(Clock::GetRealTimeClock()), |
| 466 | playout_timestamp_rtp_(0), |
| 467 | playout_delay_ms_(0), |
| 468 | rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()), |
| 469 | capture_start_rtp_time_stamp_(-1), |
| 470 | capture_start_ntp_time_ms_(-1), |
| 471 | _moduleProcessThreadPtr(module_process_thread), |
| 472 | _audioDeviceModulePtr(audio_device_module), |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 473 | _outputGain(1.0f), |
Benjamin Wright | 84583f6 | 2018-10-04 21:22:34 | [diff] [blame] | 474 | associated_send_channel_(nullptr), |
Niels Möller | 7d76a31 | 2018-10-26 10:57:07 | [diff] [blame] | 475 | media_transport_(media_transport), |
Benjamin Wright | bfb444c | 2018-10-15 17:20:24 | [diff] [blame] | 476 | frame_decryptor_(frame_decryptor), |
| 477 | crypto_options_(crypto_options) { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 478 | // TODO(nisse): Use _moduleProcessThreadPtr instead? |
| 479 | module_process_thread_checker_.DetachFromThread(); |
| 480 | |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 481 | RTC_DCHECK(module_process_thread); |
| 482 | RTC_DCHECK(audio_device_module); |
| 483 | AudioCodingModule::Config acm_config; |
| 484 | acm_config.decoder_factory = decoder_factory; |
| 485 | acm_config.neteq_config.codec_pair_id = codec_pair_id; |
| 486 | acm_config.neteq_config.max_packets_in_buffer = jitter_buffer_max_packets; |
| 487 | acm_config.neteq_config.enable_fast_accelerate = jitter_buffer_fast_playout; |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 488 | acm_config.neteq_config.min_delay_ms = jitter_buffer_min_delay_ms; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 489 | acm_config.neteq_config.enable_muted_state = true; |
| 490 | audio_coding_.reset(AudioCodingModule::Create(acm_config)); |
| 491 | |
| 492 | _outputAudioLevel.Clear(); |
| 493 | |
| 494 | rtp_receive_statistics_->EnableRetransmitDetection(remote_ssrc_, true); |
| 495 | RtpRtcp::Configuration configuration; |
| 496 | configuration.audio = true; |
Niels Möller | fd1a2fb | 2018-10-31 14:25:26 | [diff] [blame] | 497 | configuration.receiver_only = true; |
Niels Möller | ae4237e | 2018-10-05 09:28:38 | [diff] [blame] | 498 | configuration.outgoing_transport = rtcp_send_transport; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 499 | configuration.receive_statistics = rtp_receive_statistics_.get(); |
| 500 | |
| 501 | configuration.event_log = event_log_; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 502 | |
| 503 | _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration)); |
| 504 | _rtpRtcpModule->SetSendingMediaStatus(false); |
| 505 | _rtpRtcpModule->SetRemoteSSRC(remote_ssrc_); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 506 | |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 507 | _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE); |
| 508 | |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 509 | // Ensure that RTCP is enabled by default for the created channel. |
| 510 | // Note that, the module will keep generating RTCP until it is explicitly |
| 511 | // disabled by the user. |
| 512 | // After StopListen (when no sockets exists), RTCP packets will no longer |
| 513 | // be transmitted since the Transport object will then be invalid. |
| 514 | // RTCP is enabled by default. |
| 515 | _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound); |
Niels Möller | 7d76a31 | 2018-10-26 10:57:07 | [diff] [blame] | 516 | |
| 517 | if (media_transport_) { |
| 518 | media_transport_->SetReceiveAudioSink(this); |
| 519 | } |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 520 | } |
| 521 | |
Fredrik Solenberg | c5e8be3 | 2018-11-19 10:56:13 | [diff] [blame] | 522 | ChannelReceive::~ChannelReceive() { |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 523 | RTC_DCHECK(construction_thread_.CalledOnValidThread()); |
Niels Möller | 7d76a31 | 2018-10-26 10:57:07 | [diff] [blame] | 524 | |
| 525 | if (media_transport_) { |
| 526 | media_transport_->SetReceiveAudioSink(nullptr); |
| 527 | } |
| 528 | |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 529 | StopPlayout(); |
| 530 | |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 531 | int error = audio_coding_->RegisterTransportCallback(NULL); |
| 532 | RTC_DCHECK_EQ(0, error); |
| 533 | |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 534 | if (_moduleProcessThreadPtr) |
| 535 | _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | void ChannelReceive::SetSink(AudioSinkInterface* sink) { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 539 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 540 | rtc::CritScope cs(&_callbackCritSect); |
| 541 | audio_sink_ = sink; |
| 542 | } |
| 543 | |
Niels Möller | 80c6762 | 2018-11-12 12:22:47 | [diff] [blame] | 544 | void ChannelReceive::StartPlayout() { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 545 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Fredrik Solenberg | c5e8be3 | 2018-11-19 10:56:13 | [diff] [blame] | 546 | rtc::CritScope lock(&playing_lock_); |
| 547 | playing_ = true; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 548 | } |
| 549 | |
Niels Möller | 80c6762 | 2018-11-12 12:22:47 | [diff] [blame] | 550 | void ChannelReceive::StopPlayout() { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 551 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Fredrik Solenberg | c5e8be3 | 2018-11-19 10:56:13 | [diff] [blame] | 552 | rtc::CritScope lock(&playing_lock_); |
| 553 | playing_ = false; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 554 | _outputAudioLevel.Clear(); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 555 | } |
| 556 | |
Fredrik Solenberg | f693bfa | 2018-12-11 11:22:10 | [diff] [blame] | 557 | absl::optional<std::pair<int, SdpAudioFormat>> |
| 558 | ChannelReceive::GetReceiveCodec() const { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 559 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Fredrik Solenberg | f693bfa | 2018-12-11 11:22:10 | [diff] [blame] | 560 | return audio_coding_->ReceiveCodec(); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | std::vector<webrtc::RtpSource> ChannelReceive::GetSources() const { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 564 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 565 | int64_t now_ms = rtc::TimeMillis(); |
| 566 | std::vector<RtpSource> sources; |
| 567 | { |
| 568 | rtc::CritScope cs(&rtp_sources_lock_); |
| 569 | sources = contributing_sources_.GetSources(now_ms); |
| 570 | if (last_received_rtp_system_time_ms_ >= |
| 571 | now_ms - ContributingSources::kHistoryMs) { |
| 572 | sources.emplace_back(*last_received_rtp_system_time_ms_, remote_ssrc_, |
| 573 | RtpSourceType::SSRC); |
| 574 | sources.back().set_audio_level(last_received_rtp_audio_level_); |
| 575 | } |
| 576 | } |
| 577 | return sources; |
| 578 | } |
| 579 | |
| 580 | void ChannelReceive::SetReceiveCodecs( |
| 581 | const std::map<int, SdpAudioFormat>& codecs) { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 582 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 583 | for (const auto& kv : codecs) { |
| 584 | RTC_DCHECK_GE(kv.second.clockrate_hz, 1000); |
| 585 | payload_type_frequencies_[kv.first] = kv.second.clockrate_hz; |
| 586 | } |
| 587 | audio_coding_->SetReceiveCodecs(codecs); |
| 588 | } |
| 589 | |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 590 | // May be called on either worker thread or network thread. |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 591 | void ChannelReceive::OnRtpPacket(const RtpPacketReceived& packet) { |
| 592 | int64_t now_ms = rtc::TimeMillis(); |
| 593 | uint8_t audio_level; |
| 594 | bool voice_activity; |
| 595 | bool has_audio_level = |
| 596 | packet.GetExtension<::webrtc::AudioLevel>(&voice_activity, &audio_level); |
| 597 | |
| 598 | { |
| 599 | rtc::CritScope cs(&rtp_sources_lock_); |
| 600 | last_received_rtp_timestamp_ = packet.Timestamp(); |
| 601 | last_received_rtp_system_time_ms_ = now_ms; |
| 602 | if (has_audio_level) |
| 603 | last_received_rtp_audio_level_ = audio_level; |
| 604 | std::vector<uint32_t> csrcs = packet.Csrcs(); |
Jonas Oreland | 967f7d5 | 2018-11-06 06:35:06 | [diff] [blame] | 605 | contributing_sources_.Update( |
| 606 | now_ms, csrcs, |
| 607 | has_audio_level ? absl::optional<uint8_t>(audio_level) : absl::nullopt); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | // Store playout timestamp for the received RTP packet |
| 611 | UpdatePlayoutTimestamp(false); |
| 612 | |
| 613 | const auto& it = payload_type_frequencies_.find(packet.PayloadType()); |
| 614 | if (it == payload_type_frequencies_.end()) |
| 615 | return; |
| 616 | // TODO(nisse): Set payload_type_frequency earlier, when packet is parsed. |
| 617 | RtpPacketReceived packet_copy(packet); |
| 618 | packet_copy.set_payload_type_frequency(it->second); |
| 619 | |
| 620 | rtp_receive_statistics_->OnRtpPacket(packet_copy); |
| 621 | |
| 622 | RTPHeader header; |
| 623 | packet_copy.GetHeader(&header); |
| 624 | |
| 625 | ReceivePacket(packet_copy.data(), packet_copy.size(), header); |
| 626 | } |
| 627 | |
| 628 | bool ChannelReceive::ReceivePacket(const uint8_t* packet, |
| 629 | size_t packet_length, |
| 630 | const RTPHeader& header) { |
| 631 | const uint8_t* payload = packet + header.headerLength; |
| 632 | assert(packet_length >= header.headerLength); |
| 633 | size_t payload_length = packet_length - header.headerLength; |
| 634 | WebRtcRTPHeader webrtc_rtp_header = {}; |
| 635 | webrtc_rtp_header.header = header; |
| 636 | |
Benjamin Wright | 84583f6 | 2018-10-04 21:22:34 | [diff] [blame] | 637 | size_t payload_data_length = payload_length - header.paddingLength; |
| 638 | |
| 639 | // E2EE Custom Audio Frame Decryption (This is optional). |
| 640 | // Keep this buffer around for the lifetime of the OnReceivedPayloadData call. |
| 641 | rtc::Buffer decrypted_audio_payload; |
| 642 | if (frame_decryptor_ != nullptr) { |
| 643 | size_t max_plaintext_size = frame_decryptor_->GetMaxPlaintextByteSize( |
| 644 | cricket::MEDIA_TYPE_AUDIO, payload_length); |
| 645 | decrypted_audio_payload.SetSize(max_plaintext_size); |
| 646 | |
| 647 | size_t bytes_written = 0; |
| 648 | std::vector<uint32_t> csrcs(header.arrOfCSRCs, |
| 649 | header.arrOfCSRCs + header.numCSRCs); |
| 650 | int decrypt_status = frame_decryptor_->Decrypt( |
| 651 | cricket::MEDIA_TYPE_AUDIO, csrcs, |
| 652 | /*additional_data=*/nullptr, |
| 653 | rtc::ArrayView<const uint8_t>(payload, payload_data_length), |
| 654 | decrypted_audio_payload, &bytes_written); |
| 655 | |
| 656 | // In this case just interpret the failure as a silent frame. |
| 657 | if (decrypt_status != 0) { |
| 658 | bytes_written = 0; |
| 659 | } |
| 660 | |
| 661 | // Resize the decrypted audio payload to the number of bytes actually |
| 662 | // written. |
| 663 | decrypted_audio_payload.SetSize(bytes_written); |
| 664 | // Update the final payload. |
| 665 | payload = decrypted_audio_payload.data(); |
| 666 | payload_data_length = decrypted_audio_payload.size(); |
Benjamin Wright | bfb444c | 2018-10-15 17:20:24 | [diff] [blame] | 667 | } else if (crypto_options_.sframe.require_frame_encryption) { |
| 668 | RTC_DLOG(LS_ERROR) |
| 669 | << "FrameDecryptor required but not set, dropping packet"; |
| 670 | payload_data_length = 0; |
Benjamin Wright | 84583f6 | 2018-10-04 21:22:34 | [diff] [blame] | 671 | } |
| 672 | |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 673 | if (payload_data_length == 0) { |
| 674 | webrtc_rtp_header.frameType = kEmptyFrame; |
| 675 | return OnReceivedPayloadData(nullptr, 0, &webrtc_rtp_header); |
| 676 | } |
| 677 | return OnReceivedPayloadData(payload, payload_data_length, |
| 678 | &webrtc_rtp_header); |
| 679 | } |
| 680 | |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 681 | // May be called on either worker thread or network thread. |
Niels Möller | 80c6762 | 2018-11-12 12:22:47 | [diff] [blame] | 682 | // TODO(nisse): Drop always-true return value. |
| 683 | bool ChannelReceive::ReceivedRTCPPacket(const uint8_t* data, size_t length) { |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 684 | // Store playout timestamp for the received RTCP packet |
| 685 | UpdatePlayoutTimestamp(true); |
| 686 | |
| 687 | // Deliver RTCP packet to RTP/RTCP module for parsing |
| 688 | _rtpRtcpModule->IncomingRtcpPacket(data, length); |
| 689 | |
| 690 | int64_t rtt = GetRTT(); |
| 691 | if (rtt == 0) { |
| 692 | // Waiting for valid RTT. |
Niels Möller | 80c6762 | 2018-11-12 12:22:47 | [diff] [blame] | 693 | return true; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | int64_t nack_window_ms = rtt; |
| 697 | if (nack_window_ms < kMinRetransmissionWindowMs) { |
| 698 | nack_window_ms = kMinRetransmissionWindowMs; |
| 699 | } else if (nack_window_ms > kMaxRetransmissionWindowMs) { |
| 700 | nack_window_ms = kMaxRetransmissionWindowMs; |
| 701 | } |
| 702 | |
| 703 | uint32_t ntp_secs = 0; |
| 704 | uint32_t ntp_frac = 0; |
| 705 | uint32_t rtp_timestamp = 0; |
| 706 | if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL, |
| 707 | &rtp_timestamp)) { |
| 708 | // Waiting for RTCP. |
Niels Möller | 80c6762 | 2018-11-12 12:22:47 | [diff] [blame] | 709 | return true; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | { |
| 713 | rtc::CritScope lock(&ts_stats_lock_); |
| 714 | ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp); |
| 715 | } |
Niels Möller | 80c6762 | 2018-11-12 12:22:47 | [diff] [blame] | 716 | return true; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | int ChannelReceive::GetSpeechOutputLevelFullRange() const { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 720 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 721 | return _outputAudioLevel.LevelFullRange(); |
| 722 | } |
| 723 | |
| 724 | double ChannelReceive::GetTotalOutputEnergy() const { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 725 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 726 | return _outputAudioLevel.TotalEnergy(); |
| 727 | } |
| 728 | |
| 729 | double ChannelReceive::GetTotalOutputDuration() const { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 730 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 731 | return _outputAudioLevel.TotalDuration(); |
| 732 | } |
| 733 | |
| 734 | void ChannelReceive::SetChannelOutputVolumeScaling(float scaling) { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 735 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 736 | rtc::CritScope cs(&volume_settings_critsect_); |
| 737 | _outputGain = scaling; |
| 738 | } |
| 739 | |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 740 | void ChannelReceive::SetLocalSSRC(uint32_t ssrc) { |
| 741 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 742 | _rtpRtcpModule->SetSSRC(ssrc); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 743 | } |
| 744 | |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 745 | void ChannelReceive::RegisterReceiverCongestionControlObjects( |
| 746 | PacketRouter* packet_router) { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 747 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 748 | RTC_DCHECK(packet_router); |
| 749 | RTC_DCHECK(!packet_router_); |
| 750 | constexpr bool remb_candidate = false; |
| 751 | packet_router->AddReceiveRtpModule(_rtpRtcpModule.get(), remb_candidate); |
| 752 | packet_router_ = packet_router; |
| 753 | } |
| 754 | |
| 755 | void ChannelReceive::ResetReceiverCongestionControlObjects() { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 756 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 757 | RTC_DCHECK(packet_router_); |
| 758 | packet_router_->RemoveReceiveRtpModule(_rtpRtcpModule.get()); |
| 759 | packet_router_ = nullptr; |
| 760 | } |
| 761 | |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 762 | CallReceiveStatistics ChannelReceive::GetRTCPStatistics() const { |
| 763 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 764 | // --- RtcpStatistics |
Niels Möller | 80c6762 | 2018-11-12 12:22:47 | [diff] [blame] | 765 | CallReceiveStatistics stats; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 766 | |
| 767 | // The jitter statistics is updated for each received RTP packet and is |
| 768 | // based on received packets. |
| 769 | RtcpStatistics statistics; |
| 770 | StreamStatistician* statistician = |
| 771 | rtp_receive_statistics_->GetStatistician(remote_ssrc_); |
| 772 | if (statistician) { |
| 773 | statistician->GetStatistics(&statistics, |
| 774 | _rtpRtcpModule->RTCP() == RtcpMode::kOff); |
| 775 | } |
| 776 | |
| 777 | stats.fractionLost = statistics.fraction_lost; |
| 778 | stats.cumulativeLost = statistics.packets_lost; |
| 779 | stats.extendedMax = statistics.extended_highest_sequence_number; |
| 780 | stats.jitterSamples = statistics.jitter; |
| 781 | |
| 782 | // --- RTT |
| 783 | stats.rttMs = GetRTT(); |
| 784 | |
| 785 | // --- Data counters |
| 786 | |
| 787 | size_t bytesReceived(0); |
| 788 | uint32_t packetsReceived(0); |
| 789 | |
| 790 | if (statistician) { |
| 791 | statistician->GetDataCounters(&bytesReceived, &packetsReceived); |
| 792 | } |
| 793 | |
| 794 | stats.bytesReceived = bytesReceived; |
| 795 | stats.packetsReceived = packetsReceived; |
| 796 | |
| 797 | // --- Timestamps |
| 798 | { |
| 799 | rtc::CritScope lock(&ts_stats_lock_); |
| 800 | stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_; |
| 801 | } |
Niels Möller | 80c6762 | 2018-11-12 12:22:47 | [diff] [blame] | 802 | return stats; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 803 | } |
| 804 | |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 805 | void ChannelReceive::SetNACKStatus(bool enable, int max_packets) { |
| 806 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 807 | // None of these functions can fail. |
Danil Chapovalov | 2a977cf | 2018-12-04 17:03:52 | [diff] [blame] | 808 | if (enable) { |
| 809 | rtp_receive_statistics_->SetMaxReorderingThreshold(max_packets); |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 810 | audio_coding_->EnableNack(max_packets); |
Danil Chapovalov | 2a977cf | 2018-12-04 17:03:52 | [diff] [blame] | 811 | } else { |
| 812 | rtp_receive_statistics_->SetMaxReorderingThreshold( |
| 813 | kDefaultMaxReorderingThreshold); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 814 | audio_coding_->DisableNack(); |
Danil Chapovalov | 2a977cf | 2018-12-04 17:03:52 | [diff] [blame] | 815 | } |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | // Called when we are missing one or more packets. |
| 819 | int ChannelReceive::ResendPackets(const uint16_t* sequence_numbers, |
| 820 | int length) { |
| 821 | return _rtpRtcpModule->SendNACK(sequence_numbers, length); |
| 822 | } |
| 823 | |
Niels Möller | dced9f6 | 2018-11-19 09:27:07 | [diff] [blame] | 824 | void ChannelReceive::SetAssociatedSendChannel( |
| 825 | const ChannelSendInterface* channel) { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 826 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 827 | rtc::CritScope lock(&assoc_send_channel_lock_); |
| 828 | associated_send_channel_ = channel; |
| 829 | } |
| 830 | |
Niels Möller | 80c6762 | 2018-11-12 12:22:47 | [diff] [blame] | 831 | NetworkStatistics ChannelReceive::GetNetworkStatistics() const { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 832 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 80c6762 | 2018-11-12 12:22:47 | [diff] [blame] | 833 | NetworkStatistics stats; |
| 834 | int error = audio_coding_->GetNetworkStatistics(&stats); |
| 835 | RTC_DCHECK_EQ(0, error); |
| 836 | return stats; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 837 | } |
| 838 | |
Niels Möller | 80c6762 | 2018-11-12 12:22:47 | [diff] [blame] | 839 | AudioDecodingCallStats ChannelReceive::GetDecodingCallStatistics() const { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 840 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Niels Möller | 80c6762 | 2018-11-12 12:22:47 | [diff] [blame] | 841 | AudioDecodingCallStats stats; |
| 842 | audio_coding_->GetDecodingCallStatistics(&stats); |
| 843 | return stats; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | uint32_t ChannelReceive::GetDelayEstimate() const { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 847 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread() || |
| 848 | module_process_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 849 | rtc::CritScope lock(&video_sync_lock_); |
| 850 | return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_; |
| 851 | } |
| 852 | |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 853 | void ChannelReceive::SetMinimumPlayoutDelay(int delay_ms) { |
| 854 | RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); |
| 855 | // Limit to range accepted by both VoE and ACM, so we're at least getting as |
| 856 | // close as possible, instead of failing. |
| 857 | delay_ms = rtc::SafeClamp(delay_ms, 0, 10000); |
| 858 | if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) || |
| 859 | (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs)) { |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 860 | RTC_DLOG(LS_ERROR) << "SetMinimumPlayoutDelay() invalid min delay"; |
Niels Möller | 80c6762 | 2018-11-12 12:22:47 | [diff] [blame] | 861 | return; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 862 | } |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 863 | if (audio_coding_->SetMinimumPlayoutDelay(delay_ms) != 0) { |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 864 | RTC_DLOG(LS_ERROR) |
| 865 | << "SetMinimumPlayoutDelay() failed to set min playout delay"; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 866 | } |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 867 | } |
| 868 | |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 869 | uint32_t ChannelReceive::GetPlayoutTimestamp() const { |
| 870 | RTC_DCHECK_RUNS_SERIALIZED(&video_capture_thread_race_checker_); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 871 | { |
| 872 | rtc::CritScope lock(&video_sync_lock_); |
Niels Möller | 80c6762 | 2018-11-12 12:22:47 | [diff] [blame] | 873 | return playout_timestamp_rtp_; |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 874 | } |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | absl::optional<Syncable::Info> ChannelReceive::GetSyncInfo() const { |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 878 | RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 879 | Syncable::Info info; |
| 880 | if (_rtpRtcpModule->RemoteNTP(&info.capture_time_ntp_secs, |
| 881 | &info.capture_time_ntp_frac, nullptr, nullptr, |
| 882 | &info.capture_time_source_clock) != 0) { |
| 883 | return absl::nullopt; |
| 884 | } |
| 885 | { |
| 886 | rtc::CritScope cs(&rtp_sources_lock_); |
| 887 | if (!last_received_rtp_timestamp_ || !last_received_rtp_system_time_ms_) { |
| 888 | return absl::nullopt; |
| 889 | } |
| 890 | info.latest_received_capture_timestamp = *last_received_rtp_timestamp_; |
| 891 | info.latest_receive_time_ms = *last_received_rtp_system_time_ms_; |
| 892 | } |
| 893 | return info; |
| 894 | } |
| 895 | |
| 896 | void ChannelReceive::UpdatePlayoutTimestamp(bool rtcp) { |
| 897 | jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp(); |
| 898 | |
| 899 | if (!jitter_buffer_playout_timestamp_) { |
| 900 | // This can happen if this channel has not received any RTP packets. In |
| 901 | // this case, NetEq is not capable of computing a playout timestamp. |
| 902 | return; |
| 903 | } |
| 904 | |
| 905 | uint16_t delay_ms = 0; |
| 906 | if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) { |
| 907 | RTC_DLOG(LS_WARNING) |
| 908 | << "ChannelReceive::UpdatePlayoutTimestamp() failed to read" |
| 909 | << " playout delay from the ADM"; |
| 910 | return; |
| 911 | } |
| 912 | |
| 913 | RTC_DCHECK(jitter_buffer_playout_timestamp_); |
| 914 | uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_; |
| 915 | |
| 916 | // Remove the playout delay. |
| 917 | playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000)); |
| 918 | |
| 919 | { |
| 920 | rtc::CritScope lock(&video_sync_lock_); |
| 921 | if (!rtcp) { |
| 922 | playout_timestamp_rtp_ = playout_timestamp; |
| 923 | } |
| 924 | playout_delay_ms_ = delay_ms; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | int ChannelReceive::GetRtpTimestampRateHz() const { |
Fredrik Solenberg | f693bfa | 2018-12-11 11:22:10 | [diff] [blame] | 929 | const auto decoder = audio_coding_->ReceiveCodec(); |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 930 | // Default to the playout frequency if we've not gotten any packets yet. |
| 931 | // TODO(ossu): Zero clockrate can only happen if we've added an external |
| 932 | // decoder for a format we don't support internally. Remove once that way of |
| 933 | // adding decoders is gone! |
Fredrik Solenberg | f693bfa | 2018-12-11 11:22:10 | [diff] [blame] | 934 | return (decoder && decoder->second.clockrate_hz != 0) |
| 935 | ? decoder->second.clockrate_hz |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 936 | : audio_coding_->PlayoutFrequency(); |
| 937 | } |
| 938 | |
| 939 | int64_t ChannelReceive::GetRTT() const { |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 17:57:58 | [diff] [blame] | 940 | if (media_transport_) { |
| 941 | auto target_rate = media_transport_->GetLatestTargetTransferRate(); |
| 942 | if (target_rate.has_value()) { |
| 943 | return target_rate->network_estimate.round_trip_time.ms(); |
| 944 | } |
| 945 | |
| 946 | return 0; |
| 947 | } |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 948 | RtcpMode method = _rtpRtcpModule->RTCP(); |
| 949 | if (method == RtcpMode::kOff) { |
| 950 | return 0; |
| 951 | } |
| 952 | std::vector<RTCPReportBlock> report_blocks; |
| 953 | _rtpRtcpModule->RemoteRTCPStat(&report_blocks); |
| 954 | |
| 955 | // TODO(nisse): Could we check the return value from the ->RTT() call below, |
| 956 | // instead of checking if we have any report blocks? |
| 957 | if (report_blocks.empty()) { |
| 958 | rtc::CritScope lock(&assoc_send_channel_lock_); |
| 959 | // Tries to get RTT from an associated channel. |
| 960 | if (!associated_send_channel_) { |
| 961 | return 0; |
| 962 | } |
| 963 | return associated_send_channel_->GetRTT(); |
| 964 | } |
| 965 | |
| 966 | int64_t rtt = 0; |
| 967 | int64_t avg_rtt = 0; |
| 968 | int64_t max_rtt = 0; |
| 969 | int64_t min_rtt = 0; |
Niels Möller | fd1a2fb | 2018-10-31 14:25:26 | [diff] [blame] | 970 | // TODO(nisse): This method computes RTT based on sender reports, even though |
| 971 | // a receive stream is not supposed to do that. |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 972 | if (_rtpRtcpModule->RTT(remote_ssrc_, &rtt, &avg_rtt, &min_rtt, &max_rtt) != |
| 973 | 0) { |
| 974 | return 0; |
| 975 | } |
| 976 | return rtt; |
| 977 | } |
| 978 | |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 979 | } // namespace |
| 980 | |
| 981 | std::unique_ptr<ChannelReceiveInterface> CreateChannelReceive( |
| 982 | ProcessThread* module_process_thread, |
| 983 | AudioDeviceModule* audio_device_module, |
| 984 | MediaTransportInterface* media_transport, |
| 985 | Transport* rtcp_send_transport, |
| 986 | RtcEventLog* rtc_event_log, |
| 987 | uint32_t remote_ssrc, |
| 988 | size_t jitter_buffer_max_packets, |
| 989 | bool jitter_buffer_fast_playout, |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 990 | int jitter_buffer_min_delay_ms, |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 991 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory, |
| 992 | absl::optional<AudioCodecPairId> codec_pair_id, |
| 993 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor, |
| 994 | const webrtc::CryptoOptions& crypto_options) { |
| 995 | return absl::make_unique<ChannelReceive>( |
| 996 | module_process_thread, audio_device_module, media_transport, |
| 997 | rtcp_send_transport, rtc_event_log, remote_ssrc, |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 998 | jitter_buffer_max_packets, jitter_buffer_fast_playout, |
| 999 | jitter_buffer_min_delay_ms, decoder_factory, codec_pair_id, |
| 1000 | frame_decryptor, crypto_options); |
Niels Möller | 349ade3 | 2018-11-16 08:50:42 | [diff] [blame] | 1001 | } |
| 1002 | |
Niels Möller | 530ead4 | 2018-10-04 12:28:39 | [diff] [blame] | 1003 | } // namespace voe |
| 1004 | } // namespace webrtc |