blob: ac359bb62ffc4ffa9ef25d35a2659a9d006edf5f [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"
Benjamin Wrightbfb444c2018-10-15 10:20:24 -070022#include "api/crypto/cryptooptions.h"
Niels Möller530ead42018-10-04 14:28:39 +020023#include "api/rtpreceiverinterface.h"
24#include "audio/audio_level.h"
25#include "call/syncable.h"
26#include "common_types.h" // NOLINT(build/include)
27#include "modules/audio_coding/include/audio_coding_module.h"
28#include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
29#include "modules/rtp_rtcp/include/rtp_header_parser.h"
30#include "modules/rtp_rtcp/include/rtp_rtcp.h"
31#include "modules/rtp_rtcp/source/contributing_sources.h"
32#include "rtc_base/criticalsection.h"
33#include "rtc_base/thread_checker.h"
34
35// TODO(solenberg, nisse): This file contains a few NOLINT marks, to silence
36// warnings about use of unsigned short, and non-const reference arguments.
37// These need cleanup, in a separate cl.
38
39namespace rtc {
40class TimestampWrapAroundHandler;
41}
42
43namespace webrtc {
44
45class AudioDeviceModule;
Benjamin Wright84583f62018-10-04 14:22:34 -070046class FrameDecryptorInterface;
Niels Möller530ead42018-10-04 14:28:39 +020047class PacketRouter;
48class ProcessThread;
49class RateLimiter;
50class ReceiveStatistics;
51class RtcEventLog;
52class RtpPacketReceived;
53class RtpRtcp;
54
55struct CallReceiveStatistics {
56 unsigned short fractionLost; // NOLINT
57 unsigned int cumulativeLost;
58 unsigned int extendedMax;
59 unsigned int jitterSamples;
60 int64_t rttMs;
61 size_t bytesReceived;
62 int packetsReceived;
63 // The capture ntp time (in local timebase) of the first played out audio
64 // frame.
65 int64_t capture_start_ntp_time_ms_;
66};
67
68namespace voe {
69
70class ChannelSend;
71
72// Helper class to simplify locking scheme for members that are accessed from
73// multiple threads.
74// Example: a member can be set on thread T1 and read by an internal audio
75// thread T2. Accessing the member via this class ensures that we are
76// safe and also avoid TSan v2 warnings.
77class ChannelReceiveState {
78 public:
79 struct State {
80 bool playing = false;
81 };
82
83 ChannelReceiveState() {}
84 virtual ~ChannelReceiveState() {}
85
86 void Reset() {
87 rtc::CritScope lock(&lock_);
88 state_ = State();
89 }
90
91 State Get() const {
92 rtc::CritScope lock(&lock_);
93 return state_;
94 }
95
96 void SetPlaying(bool enable) {
97 rtc::CritScope lock(&lock_);
98 state_.playing = enable;
99 }
100
101 private:
102 rtc::CriticalSection lock_;
103 State state_;
104};
105
Niels Möllerae4237e2018-10-05 11:28:38 +0200106class ChannelReceive : public RtpData {
Niels Möller530ead42018-10-04 14:28:39 +0200107 public:
108 // Used for receive streams.
109 ChannelReceive(ProcessThread* module_process_thread,
110 AudioDeviceModule* audio_device_module,
Niels Möllerae4237e2018-10-05 11:28:38 +0200111 Transport* rtcp_send_transport,
Niels Möller530ead42018-10-04 14:28:39 +0200112 RtcEventLog* rtc_event_log,
113 uint32_t remote_ssrc,
114 size_t jitter_buffer_max_packets,
115 bool jitter_buffer_fast_playout,
116 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
Benjamin Wright84583f62018-10-04 14:22:34 -0700117 absl::optional<AudioCodecPairId> codec_pair_id,
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700118 FrameDecryptorInterface* frame_decryptor,
119 const webrtc::CryptoOptions& crypto_options);
Niels Möller530ead42018-10-04 14:28:39 +0200120 virtual ~ChannelReceive();
121
122 void SetSink(AudioSinkInterface* sink);
123
124 void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs);
125
126 // API methods
127
128 // VoEBase
129 int32_t StartPlayout();
130 int32_t StopPlayout();
131
132 // Codecs
133 int32_t GetRecCodec(CodecInst& codec); // NOLINT
134
Niels Möller530ead42018-10-04 14:28:39 +0200135 // 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
Niels Möller530ead42018-10-04 14:28:39 +0200173 // From AudioMixer::Source.
174 AudioMixer::Source::AudioFrameInfo GetAudioFrameWithInfo(
175 int sample_rate_hz,
176 AudioFrame* audio_frame);
177
178 int PreferredSampleRate() const;
179
180 // Associate to a send channel.
181 // Used for obtaining RTT for a receive-only channel.
182 void SetAssociatedSendChannel(ChannelSend* channel);
183
184 std::vector<RtpSource> GetSources() const;
185
186 private:
187 void Init();
188 void Terminate();
189
190 int GetRemoteSSRC(unsigned int& ssrc); // NOLINT
191
192 bool ReceivePacket(const uint8_t* packet,
193 size_t packet_length,
194 const RTPHeader& header);
195 int ResendPackets(const uint16_t* sequence_numbers, int length);
196 void UpdatePlayoutTimestamp(bool rtcp);
197
198 int GetRtpTimestampRateHz() const;
199 int64_t GetRTT() const;
200
201 rtc::CriticalSection _callbackCritSect;
202 rtc::CriticalSection volume_settings_critsect_;
203
204 ChannelReceiveState channel_state_;
205
206 RtcEventLog* const event_log_;
207
208 // Indexed by payload type.
209 std::map<uint8_t, int> payload_type_frequencies_;
210
211 std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_;
212 std::unique_ptr<RtpRtcp> _rtpRtcpModule;
213 const uint32_t remote_ssrc_;
214
215 // Info for GetSources and GetSyncInfo is updated on network or worker thread,
216 // queried on the worker thread.
217 rtc::CriticalSection rtp_sources_lock_;
218 ContributingSources contributing_sources_ RTC_GUARDED_BY(&rtp_sources_lock_);
219 absl::optional<uint32_t> last_received_rtp_timestamp_
220 RTC_GUARDED_BY(&rtp_sources_lock_);
221 absl::optional<int64_t> last_received_rtp_system_time_ms_
222 RTC_GUARDED_BY(&rtp_sources_lock_);
223 absl::optional<uint8_t> last_received_rtp_audio_level_
224 RTC_GUARDED_BY(&rtp_sources_lock_);
225
226 std::unique_ptr<AudioCodingModule> audio_coding_;
227 AudioSinkInterface* audio_sink_ = nullptr;
228 AudioLevel _outputAudioLevel;
229
230 RemoteNtpTimeEstimator ntp_estimator_ RTC_GUARDED_BY(ts_stats_lock_);
231
232 // Timestamp of the audio pulled from NetEq.
233 absl::optional<uint32_t> jitter_buffer_playout_timestamp_;
234
235 rtc::CriticalSection video_sync_lock_;
236 uint32_t playout_timestamp_rtp_ RTC_GUARDED_BY(video_sync_lock_);
237 uint32_t playout_delay_ms_ RTC_GUARDED_BY(video_sync_lock_);
238
239 rtc::CriticalSection ts_stats_lock_;
240
241 std::unique_ptr<rtc::TimestampWrapAroundHandler> rtp_ts_wraparound_handler_;
242 // The rtp timestamp of the first played out audio frame.
243 int64_t capture_start_rtp_time_stamp_;
244 // The capture ntp time (in local timebase) of the first played out audio
245 // frame.
246 int64_t capture_start_ntp_time_ms_ RTC_GUARDED_BY(ts_stats_lock_);
247
248 // uses
249 ProcessThread* _moduleProcessThreadPtr;
250 AudioDeviceModule* _audioDeviceModulePtr;
Niels Möller530ead42018-10-04 14:28:39 +0200251 float _outputGain RTC_GUARDED_BY(volume_settings_critsect_);
252
253 // An associated send channel.
254 rtc::CriticalSection assoc_send_channel_lock_;
255 ChannelSend* associated_send_channel_
256 RTC_GUARDED_BY(assoc_send_channel_lock_);
257
258 PacketRouter* packet_router_ = nullptr;
259
260 rtc::ThreadChecker construction_thread_;
Benjamin Wright84583f62018-10-04 14:22:34 -0700261
262 // E2EE Audio Frame Decryption
263 FrameDecryptorInterface* frame_decryptor_ = nullptr;
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700264 webrtc::CryptoOptions crypto_options_;
Niels Möller530ead42018-10-04 14:28:39 +0200265};
266
267} // namespace voe
268} // namespace webrtc
269
270#endif // AUDIO_CHANNEL_RECEIVE_H_