blob: 225c335d91525bae955ccf990a42bcfb70212eb3 [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
Niels Möller80c67622018-11-12 13:22:47 +0100130 void StartPlayout();
131 void StopPlayout();
Niels Möller530ead42018-10-04 14:28:39 +0200132
133 // Codecs
Niels Möller80c67622018-11-12 13:22:47 +0100134 bool GetRecCodec(CodecInst* codec);
Niels Möller530ead42018-10-04 14:28:39 +0200135
Niels Möller530ead42018-10-04 14:28:39 +0200136 // TODO(nisse, solenberg): Delete when VoENetwork is deleted.
Niels Möller80c67622018-11-12 13:22:47 +0100137 bool ReceivedRTCPPacket(const uint8_t* data, size_t length);
Niels Möller530ead42018-10-04 14:28:39 +0200138 void OnRtpPacket(const RtpPacketReceived& packet);
139
140 // Muting, Volume and Level.
141 void SetChannelOutputVolumeScaling(float scaling);
142 int GetSpeechOutputLevelFullRange() const;
143 // See description of "totalAudioEnergy" in the WebRTC stats spec:
144 // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
145 double GetTotalOutputEnergy() const;
146 double GetTotalOutputDuration() const;
147
148 // Stats.
Niels Möller80c67622018-11-12 13:22:47 +0100149 NetworkStatistics GetNetworkStatistics() const;
150 AudioDecodingCallStats GetDecodingCallStatistics() const;
Niels Möller530ead42018-10-04 14:28:39 +0200151
152 // Audio+Video Sync.
153 uint32_t GetDelayEstimate() const;
Niels Möller80c67622018-11-12 13:22:47 +0100154 void SetMinimumPlayoutDelay(int delayMs);
155 uint32_t GetPlayoutTimestamp();
Niels Möller530ead42018-10-04 14:28:39 +0200156
157 // Produces the transport-related timestamps; current_delay_ms is left unset.
158 absl::optional<Syncable::Info> GetSyncInfo() const;
159
160 // RTP+RTCP
Niels Möller80c67622018-11-12 13:22:47 +0100161 void SetLocalSSRC(unsigned int ssrc);
Niels Möller530ead42018-10-04 14:28:39 +0200162
163 void RegisterReceiverCongestionControlObjects(PacketRouter* packet_router);
164 void ResetReceiverCongestionControlObjects();
165
Niels Möller80c67622018-11-12 13:22:47 +0100166 CallReceiveStatistics GetRTCPStatistics();
Niels Möller530ead42018-10-04 14:28:39 +0200167 void SetNACKStatus(bool enable, int maxNumberOfPackets);
168
Niels Möller530ead42018-10-04 14:28:39 +0200169 // From AudioMixer::Source.
170 AudioMixer::Source::AudioFrameInfo GetAudioFrameWithInfo(
171 int sample_rate_hz,
172 AudioFrame* audio_frame);
173
174 int PreferredSampleRate() const;
175
176 // Associate to a send channel.
177 // Used for obtaining RTT for a receive-only channel.
178 void SetAssociatedSendChannel(ChannelSend* channel);
179
180 std::vector<RtpSource> GetSources() const;
181
182 private:
183 void Init();
184 void Terminate();
185
186 int GetRemoteSSRC(unsigned int& ssrc); // NOLINT
187
188 bool ReceivePacket(const uint8_t* packet,
189 size_t packet_length,
190 const RTPHeader& header);
191 int ResendPackets(const uint16_t* sequence_numbers, int length);
192 void UpdatePlayoutTimestamp(bool rtcp);
193
194 int GetRtpTimestampRateHz() const;
195 int64_t GetRTT() const;
196
Niels Möller80c67622018-11-12 13:22:47 +0100197 // MediaTransportAudioSinkInterface override;
198 void OnData(uint64_t channel_id,
199 MediaTransportEncodedAudioFrame frame) override;
200
201 // From RtpData in the RTP/RTCP module
202 int32_t OnReceivedPayloadData(const uint8_t* payloadData,
203 size_t payloadSize,
204 const WebRtcRTPHeader* rtpHeader) override;
205
Niels Möller530ead42018-10-04 14:28:39 +0200206 rtc::CriticalSection _callbackCritSect;
207 rtc::CriticalSection volume_settings_critsect_;
208
209 ChannelReceiveState channel_state_;
210
211 RtcEventLog* const event_log_;
212
213 // Indexed by payload type.
214 std::map<uint8_t, int> payload_type_frequencies_;
215
216 std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_;
217 std::unique_ptr<RtpRtcp> _rtpRtcpModule;
218 const uint32_t remote_ssrc_;
219
220 // Info for GetSources and GetSyncInfo is updated on network or worker thread,
221 // queried on the worker thread.
222 rtc::CriticalSection rtp_sources_lock_;
223 ContributingSources contributing_sources_ RTC_GUARDED_BY(&rtp_sources_lock_);
224 absl::optional<uint32_t> last_received_rtp_timestamp_
225 RTC_GUARDED_BY(&rtp_sources_lock_);
226 absl::optional<int64_t> last_received_rtp_system_time_ms_
227 RTC_GUARDED_BY(&rtp_sources_lock_);
228 absl::optional<uint8_t> last_received_rtp_audio_level_
229 RTC_GUARDED_BY(&rtp_sources_lock_);
230
231 std::unique_ptr<AudioCodingModule> audio_coding_;
232 AudioSinkInterface* audio_sink_ = nullptr;
233 AudioLevel _outputAudioLevel;
234
235 RemoteNtpTimeEstimator ntp_estimator_ RTC_GUARDED_BY(ts_stats_lock_);
236
237 // Timestamp of the audio pulled from NetEq.
238 absl::optional<uint32_t> jitter_buffer_playout_timestamp_;
239
240 rtc::CriticalSection video_sync_lock_;
241 uint32_t playout_timestamp_rtp_ RTC_GUARDED_BY(video_sync_lock_);
242 uint32_t playout_delay_ms_ RTC_GUARDED_BY(video_sync_lock_);
243
244 rtc::CriticalSection ts_stats_lock_;
245
246 std::unique_ptr<rtc::TimestampWrapAroundHandler> rtp_ts_wraparound_handler_;
247 // The rtp timestamp of the first played out audio frame.
248 int64_t capture_start_rtp_time_stamp_;
249 // The capture ntp time (in local timebase) of the first played out audio
250 // frame.
251 int64_t capture_start_ntp_time_ms_ RTC_GUARDED_BY(ts_stats_lock_);
252
253 // uses
254 ProcessThread* _moduleProcessThreadPtr;
255 AudioDeviceModule* _audioDeviceModulePtr;
Niels Möller530ead42018-10-04 14:28:39 +0200256 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_;
Benjamin Wright84583f62018-10-04 14:22:34 -0700266
Niels Möller7d76a312018-10-26 12:57:07 +0200267 MediaTransportInterface* const media_transport_;
268
Benjamin Wright84583f62018-10-04 14:22:34 -0700269 // E2EE Audio Frame Decryption
Benjamin Wright78410ad2018-10-25 09:52:57 -0700270 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_;
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700271 webrtc::CryptoOptions crypto_options_;
Niels Möller530ead42018-10-04 14:28:39 +0200272};
273
274} // namespace voe
275} // namespace webrtc
276
277#endif // AUDIO_CHANNEL_RECEIVE_H_