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 | #ifndef AUDIO_CHANNEL_RECEIVE_H_ |
| 12 | #define AUDIO_CHANNEL_RECEIVE_H_ |
| 13 | |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "absl/types/optional.h" |
| 19 | #include "api/audio/audio_mixer.h" |
| 20 | #include "api/call/audio_sink.h" |
| 21 | #include "api/call/transport.h" |
| 22 | #include "api/rtpreceiverinterface.h" |
| 23 | #include "audio/audio_level.h" |
| 24 | #include "call/syncable.h" |
| 25 | #include "common_types.h" // NOLINT(build/include) |
| 26 | #include "modules/audio_coding/include/audio_coding_module.h" |
| 27 | #include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h" |
| 28 | #include "modules/rtp_rtcp/include/rtp_header_parser.h" |
| 29 | #include "modules/rtp_rtcp/include/rtp_rtcp.h" |
| 30 | #include "modules/rtp_rtcp/source/contributing_sources.h" |
| 31 | #include "rtc_base/criticalsection.h" |
| 32 | #include "rtc_base/thread_checker.h" |
| 33 | |
| 34 | // TODO(solenberg, nisse): This file contains a few NOLINT marks, to silence |
| 35 | // warnings about use of unsigned short, and non-const reference arguments. |
| 36 | // These need cleanup, in a separate cl. |
| 37 | |
| 38 | namespace rtc { |
| 39 | class TimestampWrapAroundHandler; |
| 40 | } |
| 41 | |
| 42 | namespace webrtc { |
| 43 | |
| 44 | class AudioDeviceModule; |
| 45 | class PacketRouter; |
| 46 | class ProcessThread; |
| 47 | class RateLimiter; |
| 48 | class ReceiveStatistics; |
| 49 | class RtcEventLog; |
| 50 | class RtpPacketReceived; |
| 51 | class RtpRtcp; |
| 52 | |
| 53 | struct CallReceiveStatistics { |
| 54 | unsigned short fractionLost; // NOLINT |
| 55 | unsigned int cumulativeLost; |
| 56 | unsigned int extendedMax; |
| 57 | unsigned int jitterSamples; |
| 58 | int64_t rttMs; |
| 59 | size_t bytesReceived; |
| 60 | int packetsReceived; |
| 61 | // The capture ntp time (in local timebase) of the first played out audio |
| 62 | // frame. |
| 63 | int64_t capture_start_ntp_time_ms_; |
| 64 | }; |
| 65 | |
| 66 | namespace voe { |
| 67 | |
| 68 | class ChannelSend; |
| 69 | |
| 70 | // Helper class to simplify locking scheme for members that are accessed from |
| 71 | // multiple threads. |
| 72 | // Example: a member can be set on thread T1 and read by an internal audio |
| 73 | // thread T2. Accessing the member via this class ensures that we are |
| 74 | // safe and also avoid TSan v2 warnings. |
| 75 | class ChannelReceiveState { |
| 76 | public: |
| 77 | struct State { |
| 78 | bool playing = false; |
| 79 | }; |
| 80 | |
| 81 | ChannelReceiveState() {} |
| 82 | virtual ~ChannelReceiveState() {} |
| 83 | |
| 84 | void Reset() { |
| 85 | rtc::CritScope lock(&lock_); |
| 86 | state_ = State(); |
| 87 | } |
| 88 | |
| 89 | State Get() const { |
| 90 | rtc::CritScope lock(&lock_); |
| 91 | return state_; |
| 92 | } |
| 93 | |
| 94 | void SetPlaying(bool enable) { |
| 95 | rtc::CritScope lock(&lock_); |
| 96 | state_.playing = enable; |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | rtc::CriticalSection lock_; |
| 101 | State state_; |
| 102 | }; |
| 103 | |
| 104 | class ChannelReceive : public RtpData, public Transport { |
| 105 | public: |
| 106 | // Used for receive streams. |
| 107 | ChannelReceive(ProcessThread* module_process_thread, |
| 108 | AudioDeviceModule* audio_device_module, |
| 109 | RtcpRttStats* rtcp_rtt_stats, |
| 110 | RtcEventLog* rtc_event_log, |
| 111 | uint32_t remote_ssrc, |
| 112 | size_t jitter_buffer_max_packets, |
| 113 | bool jitter_buffer_fast_playout, |
| 114 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory, |
| 115 | absl::optional<AudioCodecPairId> codec_pair_id); |
| 116 | virtual ~ChannelReceive(); |
| 117 | |
| 118 | void SetSink(AudioSinkInterface* sink); |
| 119 | |
| 120 | void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs); |
| 121 | |
| 122 | // API methods |
| 123 | |
| 124 | // VoEBase |
| 125 | int32_t StartPlayout(); |
| 126 | int32_t StopPlayout(); |
| 127 | |
| 128 | // Codecs |
| 129 | int32_t GetRecCodec(CodecInst& codec); // NOLINT |
| 130 | |
| 131 | // Network |
| 132 | void RegisterTransport(Transport* transport); |
| 133 | // TODO(nisse, solenberg): Delete when VoENetwork is deleted. |
| 134 | int32_t ReceivedRTCPPacket(const uint8_t* data, size_t length); |
| 135 | void OnRtpPacket(const RtpPacketReceived& packet); |
| 136 | |
| 137 | // Muting, Volume and Level. |
| 138 | void SetChannelOutputVolumeScaling(float scaling); |
| 139 | int GetSpeechOutputLevelFullRange() const; |
| 140 | // See description of "totalAudioEnergy" in the WebRTC stats spec: |
| 141 | // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy |
| 142 | double GetTotalOutputEnergy() const; |
| 143 | double GetTotalOutputDuration() const; |
| 144 | |
| 145 | // Stats. |
| 146 | int GetNetworkStatistics(NetworkStatistics& stats); // NOLINT |
| 147 | void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const; |
| 148 | |
| 149 | // Audio+Video Sync. |
| 150 | uint32_t GetDelayEstimate() const; |
| 151 | int SetMinimumPlayoutDelay(int delayMs); |
| 152 | int GetPlayoutTimestamp(unsigned int& timestamp); // NOLINT |
| 153 | |
| 154 | // Produces the transport-related timestamps; current_delay_ms is left unset. |
| 155 | absl::optional<Syncable::Info> GetSyncInfo() const; |
| 156 | |
| 157 | // RTP+RTCP |
| 158 | int SetLocalSSRC(unsigned int ssrc); |
| 159 | |
| 160 | void RegisterReceiverCongestionControlObjects(PacketRouter* packet_router); |
| 161 | void ResetReceiverCongestionControlObjects(); |
| 162 | |
| 163 | int GetRTPStatistics(CallReceiveStatistics& stats); // NOLINT |
| 164 | void SetNACKStatus(bool enable, int maxNumberOfPackets); |
| 165 | |
| 166 | // From RtpData in the RTP/RTCP module |
| 167 | int32_t OnReceivedPayloadData(const uint8_t* payloadData, |
| 168 | size_t payloadSize, |
| 169 | const WebRtcRTPHeader* rtpHeader) override; |
| 170 | |
| 171 | // From Transport (called by the RTP/RTCP module) |
| 172 | bool SendRtp(const uint8_t* data, |
| 173 | size_t len, |
| 174 | const PacketOptions& packet_options) override; |
| 175 | bool SendRtcp(const uint8_t* data, size_t len) override; |
| 176 | |
| 177 | // From AudioMixer::Source. |
| 178 | AudioMixer::Source::AudioFrameInfo GetAudioFrameWithInfo( |
| 179 | int sample_rate_hz, |
| 180 | AudioFrame* audio_frame); |
| 181 | |
| 182 | int PreferredSampleRate() const; |
| 183 | |
| 184 | // Associate to a send channel. |
| 185 | // Used for obtaining RTT for a receive-only channel. |
| 186 | void SetAssociatedSendChannel(ChannelSend* channel); |
| 187 | |
| 188 | std::vector<RtpSource> GetSources() const; |
| 189 | |
| 190 | private: |
| 191 | void Init(); |
| 192 | void Terminate(); |
| 193 | |
| 194 | int GetRemoteSSRC(unsigned int& ssrc); // NOLINT |
| 195 | |
| 196 | bool ReceivePacket(const uint8_t* packet, |
| 197 | size_t packet_length, |
| 198 | const RTPHeader& header); |
| 199 | int ResendPackets(const uint16_t* sequence_numbers, int length); |
| 200 | void UpdatePlayoutTimestamp(bool rtcp); |
| 201 | |
| 202 | int GetRtpTimestampRateHz() const; |
| 203 | int64_t GetRTT() const; |
| 204 | |
| 205 | rtc::CriticalSection _callbackCritSect; |
| 206 | rtc::CriticalSection volume_settings_critsect_; |
| 207 | |
| 208 | ChannelReceiveState channel_state_; |
| 209 | |
| 210 | RtcEventLog* const event_log_; |
| 211 | |
| 212 | // Indexed by payload type. |
| 213 | std::map<uint8_t, int> payload_type_frequencies_; |
| 214 | |
| 215 | std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_; |
| 216 | std::unique_ptr<RtpRtcp> _rtpRtcpModule; |
| 217 | const uint32_t remote_ssrc_; |
| 218 | |
| 219 | // Info for GetSources and GetSyncInfo is updated on network or worker thread, |
| 220 | // queried on the worker thread. |
| 221 | rtc::CriticalSection rtp_sources_lock_; |
| 222 | ContributingSources contributing_sources_ RTC_GUARDED_BY(&rtp_sources_lock_); |
| 223 | absl::optional<uint32_t> last_received_rtp_timestamp_ |
| 224 | RTC_GUARDED_BY(&rtp_sources_lock_); |
| 225 | absl::optional<int64_t> last_received_rtp_system_time_ms_ |
| 226 | RTC_GUARDED_BY(&rtp_sources_lock_); |
| 227 | absl::optional<uint8_t> last_received_rtp_audio_level_ |
| 228 | RTC_GUARDED_BY(&rtp_sources_lock_); |
| 229 | |
| 230 | std::unique_ptr<AudioCodingModule> audio_coding_; |
| 231 | AudioSinkInterface* audio_sink_ = nullptr; |
| 232 | AudioLevel _outputAudioLevel; |
| 233 | |
| 234 | RemoteNtpTimeEstimator ntp_estimator_ RTC_GUARDED_BY(ts_stats_lock_); |
| 235 | |
| 236 | // Timestamp of the audio pulled from NetEq. |
| 237 | absl::optional<uint32_t> jitter_buffer_playout_timestamp_; |
| 238 | |
| 239 | rtc::CriticalSection video_sync_lock_; |
| 240 | uint32_t playout_timestamp_rtp_ RTC_GUARDED_BY(video_sync_lock_); |
| 241 | uint32_t playout_delay_ms_ RTC_GUARDED_BY(video_sync_lock_); |
| 242 | |
| 243 | rtc::CriticalSection ts_stats_lock_; |
| 244 | |
| 245 | std::unique_ptr<rtc::TimestampWrapAroundHandler> rtp_ts_wraparound_handler_; |
| 246 | // The rtp timestamp of the first played out audio frame. |
| 247 | int64_t capture_start_rtp_time_stamp_; |
| 248 | // The capture ntp time (in local timebase) of the first played out audio |
| 249 | // frame. |
| 250 | int64_t capture_start_ntp_time_ms_ RTC_GUARDED_BY(ts_stats_lock_); |
| 251 | |
| 252 | // uses |
| 253 | ProcessThread* _moduleProcessThreadPtr; |
| 254 | AudioDeviceModule* _audioDeviceModulePtr; |
| 255 | Transport* _transportPtr; // WebRtc socket or external transport |
| 256 | float _outputGain RTC_GUARDED_BY(volume_settings_critsect_); |
| 257 | |
| 258 | // An associated send channel. |
| 259 | rtc::CriticalSection assoc_send_channel_lock_; |
| 260 | ChannelSend* associated_send_channel_ |
| 261 | RTC_GUARDED_BY(assoc_send_channel_lock_); |
| 262 | |
| 263 | PacketRouter* packet_router_ = nullptr; |
| 264 | |
| 265 | rtc::ThreadChecker construction_thread_; |
| 266 | }; |
| 267 | |
| 268 | } // namespace voe |
| 269 | } // namespace webrtc |
| 270 | |
| 271 | #endif // AUDIO_CHANNEL_RECEIVE_H_ |