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