blob: 0c509623a766e96e8951b07b18b1ea3433568c95 [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öller7d76a312018-10-26 12:57:07 +020023#include "api/media_transport_interface.h"
Niels Möller530ead42018-10-04 14:28:39 +020024#include "api/rtpreceiverinterface.h"
25#include "audio/audio_level.h"
26#include "call/syncable.h"
27#include "common_types.h" // NOLINT(build/include)
28#include "modules/audio_coding/include/audio_coding_module.h"
29#include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
30#include "modules/rtp_rtcp/include/rtp_header_parser.h"
31#include "modules/rtp_rtcp/include/rtp_rtcp.h"
32#include "modules/rtp_rtcp/source/contributing_sources.h"
33#include "rtc_base/criticalsection.h"
34#include "rtc_base/thread_checker.h"
35
36// TODO(solenberg, nisse): This file contains a few NOLINT marks, to silence
37// warnings about use of unsigned short, and non-const reference arguments.
38// These need cleanup, in a separate cl.
39
40namespace rtc {
41class TimestampWrapAroundHandler;
42}
43
44namespace webrtc {
45
46class AudioDeviceModule;
Benjamin Wright84583f62018-10-04 14:22:34 -070047class FrameDecryptorInterface;
Niels Möller530ead42018-10-04 14:28:39 +020048class PacketRouter;
49class ProcessThread;
50class RateLimiter;
51class ReceiveStatistics;
52class RtcEventLog;
53class RtpPacketReceived;
54class RtpRtcp;
55
56struct CallReceiveStatistics {
57 unsigned short fractionLost; // NOLINT
58 unsigned int cumulativeLost;
59 unsigned int extendedMax;
60 unsigned int jitterSamples;
61 int64_t rttMs;
62 size_t bytesReceived;
63 int packetsReceived;
64 // The capture ntp time (in local timebase) of the first played out audio
65 // frame.
66 int64_t capture_start_ntp_time_ms_;
67};
68
69namespace voe {
70
71class ChannelSend;
72
73// Helper class to simplify locking scheme for members that are accessed from
74// multiple threads.
75// Example: a member can be set on thread T1 and read by an internal audio
76// thread T2. Accessing the member via this class ensures that we are
77// safe and also avoid TSan v2 warnings.
78class ChannelReceiveState {
79 public:
80 struct State {
81 bool playing = false;
82 };
83
84 ChannelReceiveState() {}
85 virtual ~ChannelReceiveState() {}
86
87 void Reset() {
88 rtc::CritScope lock(&lock_);
89 state_ = State();
90 }
91
92 State Get() const {
93 rtc::CritScope lock(&lock_);
94 return state_;
95 }
96
97 void SetPlaying(bool enable) {
98 rtc::CritScope lock(&lock_);
99 state_.playing = enable;
100 }
101
102 private:
103 rtc::CriticalSection lock_;
104 State state_;
105};
106
Niels Möller7d76a312018-10-26 12:57:07 +0200107class ChannelReceive : public RtpData, public MediaTransportAudioSinkInterface {
Niels Möller530ead42018-10-04 14:28:39 +0200108 public:
109 // Used for receive streams.
110 ChannelReceive(ProcessThread* module_process_thread,
111 AudioDeviceModule* audio_device_module,
Niels Möller7d76a312018-10-26 12:57:07 +0200112 MediaTransportInterface* media_transport,
Niels Möllerae4237e2018-10-05 11:28:38 +0200113 Transport* rtcp_send_transport,
Niels Möller530ead42018-10-04 14:28:39 +0200114 RtcEventLog* rtc_event_log,
115 uint32_t remote_ssrc,
116 size_t jitter_buffer_max_packets,
117 bool jitter_buffer_fast_playout,
118 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
Benjamin Wright84583f62018-10-04 14:22:34 -0700119 absl::optional<AudioCodecPairId> codec_pair_id,
Benjamin Wright78410ad2018-10-25 09:52:57 -0700120 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor,
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700121 const webrtc::CryptoOptions& crypto_options);
Niels Möller530ead42018-10-04 14:28:39 +0200122 virtual ~ChannelReceive();
123
124 void SetSink(AudioSinkInterface* sink);
125
126 void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs);
127
128 // API methods
129
130 // VoEBase
131 int32_t StartPlayout();
132 int32_t StopPlayout();
133
134 // Codecs
135 int32_t GetRecCodec(CodecInst& codec); // NOLINT
136
Niels Möller530ead42018-10-04 14:28:39 +0200137 // TODO(nisse, solenberg): Delete when VoENetwork is deleted.
138 int32_t ReceivedRTCPPacket(const uint8_t* data, size_t length);
139 void OnRtpPacket(const RtpPacketReceived& packet);
140
141 // Muting, Volume and Level.
142 void SetChannelOutputVolumeScaling(float scaling);
143 int GetSpeechOutputLevelFullRange() const;
144 // See description of "totalAudioEnergy" in the WebRTC stats spec:
145 // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
146 double GetTotalOutputEnergy() const;
147 double GetTotalOutputDuration() const;
148
149 // Stats.
150 int GetNetworkStatistics(NetworkStatistics& stats); // NOLINT
151 void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const;
152
153 // Audio+Video Sync.
154 uint32_t GetDelayEstimate() const;
155 int SetMinimumPlayoutDelay(int delayMs);
156 int GetPlayoutTimestamp(unsigned int& timestamp); // NOLINT
157
158 // Produces the transport-related timestamps; current_delay_ms is left unset.
159 absl::optional<Syncable::Info> GetSyncInfo() const;
160
161 // RTP+RTCP
162 int SetLocalSSRC(unsigned int ssrc);
163
164 void RegisterReceiverCongestionControlObjects(PacketRouter* packet_router);
165 void ResetReceiverCongestionControlObjects();
166
167 int GetRTPStatistics(CallReceiveStatistics& stats); // NOLINT
168 void SetNACKStatus(bool enable, int maxNumberOfPackets);
169
Niels Möller7d76a312018-10-26 12:57:07 +0200170 // MediaTransportAudioSinkInterface override;
171 void OnData(uint64_t channel_id,
172 MediaTransportEncodedAudioFrame frame) override;
173
Niels Möller530ead42018-10-04 14:28:39 +0200174 // From RtpData in the RTP/RTCP module
175 int32_t OnReceivedPayloadData(const uint8_t* payloadData,
176 size_t payloadSize,
177 const WebRtcRTPHeader* rtpHeader) override;
178
Niels Möller530ead42018-10-04 14:28:39 +0200179 // 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;
Niels Möller530ead42018-10-04 14:28:39 +0200257 float _outputGain RTC_GUARDED_BY(volume_settings_critsect_);
258
259 // An associated send channel.
260 rtc::CriticalSection assoc_send_channel_lock_;
261 ChannelSend* associated_send_channel_
262 RTC_GUARDED_BY(assoc_send_channel_lock_);
263
264 PacketRouter* packet_router_ = nullptr;
265
266 rtc::ThreadChecker construction_thread_;
Benjamin Wright84583f62018-10-04 14:22:34 -0700267
Niels Möller7d76a312018-10-26 12:57:07 +0200268 MediaTransportInterface* const media_transport_;
269
Benjamin Wright84583f62018-10-04 14:22:34 -0700270 // E2EE Audio Frame Decryption
Benjamin Wright78410ad2018-10-25 09:52:57 -0700271 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_;
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700272 webrtc::CryptoOptions crypto_options_;
Niels Möller530ead42018-10-04 14:28:39 +0200273};
274
275} // namespace voe
276} // namespace webrtc
277
278#endif // AUDIO_CHANNEL_RECEIVE_H_