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