blob: de2ae2aef0eac3884c1d6b49e729404c4e3400be [file] [log] [blame]
Niels Möller530ead42018-10-04 12:28:391/*
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 17:20:2422#include "api/crypto/cryptooptions.h"
Niels Möller7d76a312018-10-26 10:57:0723#include "api/media_transport_interface.h"
Niels Möller530ead42018-10-04 12:28:3924#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 21:22:3447class FrameDecryptorInterface;
Niels Möller530ead42018-10-04 12:28:3948class 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öller8fb57462018-11-13 09:08:33107class ChannelReceive : public MediaTransportAudioSinkInterface {
Niels Möller530ead42018-10-04 12:28:39108 public:
109 // Used for receive streams.
110 ChannelReceive(ProcessThread* module_process_thread,
111 AudioDeviceModule* audio_device_module,
Niels Möller7d76a312018-10-26 10:57:07112 MediaTransportInterface* media_transport,
Niels Möllerae4237e2018-10-05 09:28:38113 Transport* rtcp_send_transport,
Niels Möller530ead42018-10-04 12:28:39114 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 21:22:34119 absl::optional<AudioCodecPairId> codec_pair_id,
Benjamin Wright78410ad2018-10-25 16:52:57120 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor,
Benjamin Wrightbfb444c2018-10-15 17:20:24121 const webrtc::CryptoOptions& crypto_options);
Niels Möller530ead42018-10-04 12:28:39122 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 12:22:47130 void StartPlayout();
131 void StopPlayout();
Niels Möller530ead42018-10-04 12:28:39132
133 // Codecs
Niels Möller80c67622018-11-12 12:22:47134 bool GetRecCodec(CodecInst* codec);
Niels Möller530ead42018-10-04 12:28:39135
Niels Möller530ead42018-10-04 12:28:39136 // TODO(nisse, solenberg): Delete when VoENetwork is deleted.
Niels Möller80c67622018-11-12 12:22:47137 bool ReceivedRTCPPacket(const uint8_t* data, size_t length);
Niels Möller530ead42018-10-04 12:28:39138 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 12:22:47149 NetworkStatistics GetNetworkStatistics() const;
150 AudioDecodingCallStats GetDecodingCallStatistics() const;
Niels Möller530ead42018-10-04 12:28:39151
152 // Audio+Video Sync.
153 uint32_t GetDelayEstimate() const;
Niels Möller80c67622018-11-12 12:22:47154 void SetMinimumPlayoutDelay(int delayMs);
155 uint32_t GetPlayoutTimestamp();
Niels Möller530ead42018-10-04 12:28:39156
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 12:22:47161 void SetLocalSSRC(unsigned int ssrc);
Niels Möller530ead42018-10-04 12:28:39162
163 void RegisterReceiverCongestionControlObjects(PacketRouter* packet_router);
164 void ResetReceiverCongestionControlObjects();
165
Niels Möller80c67622018-11-12 12:22:47166 CallReceiveStatistics GetRTCPStatistics();
Niels Möller530ead42018-10-04 12:28:39167 void SetNACKStatus(bool enable, int maxNumberOfPackets);
168
Niels Möller530ead42018-10-04 12:28:39169 // 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 12:22:47197 // MediaTransportAudioSinkInterface override;
198 void OnData(uint64_t channel_id,
199 MediaTransportEncodedAudioFrame frame) override;
200
Niels Möller80c67622018-11-12 12:22:47201 int32_t OnReceivedPayloadData(const uint8_t* payloadData,
202 size_t payloadSize,
Niels Möller8fb57462018-11-13 09:08:33203 const WebRtcRTPHeader* rtpHeader);
Niels Möller530ead42018-10-04 12:28:39204 rtc::CriticalSection _callbackCritSect;
205 rtc::CriticalSection volume_settings_critsect_;
206
207 ChannelReceiveState channel_state_;
208
209 RtcEventLog* const event_log_;
210
211 // Indexed by payload type.
212 std::map<uint8_t, int> payload_type_frequencies_;
213
214 std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_;
215 std::unique_ptr<RtpRtcp> _rtpRtcpModule;
216 const uint32_t remote_ssrc_;
217
218 // Info for GetSources and GetSyncInfo is updated on network or worker thread,
219 // queried on the worker thread.
220 rtc::CriticalSection rtp_sources_lock_;
221 ContributingSources contributing_sources_ RTC_GUARDED_BY(&rtp_sources_lock_);
222 absl::optional<uint32_t> last_received_rtp_timestamp_
223 RTC_GUARDED_BY(&rtp_sources_lock_);
224 absl::optional<int64_t> last_received_rtp_system_time_ms_
225 RTC_GUARDED_BY(&rtp_sources_lock_);
226 absl::optional<uint8_t> last_received_rtp_audio_level_
227 RTC_GUARDED_BY(&rtp_sources_lock_);
228
229 std::unique_ptr<AudioCodingModule> audio_coding_;
230 AudioSinkInterface* audio_sink_ = nullptr;
231 AudioLevel _outputAudioLevel;
232
233 RemoteNtpTimeEstimator ntp_estimator_ RTC_GUARDED_BY(ts_stats_lock_);
234
235 // Timestamp of the audio pulled from NetEq.
236 absl::optional<uint32_t> jitter_buffer_playout_timestamp_;
237
238 rtc::CriticalSection video_sync_lock_;
239 uint32_t playout_timestamp_rtp_ RTC_GUARDED_BY(video_sync_lock_);
240 uint32_t playout_delay_ms_ RTC_GUARDED_BY(video_sync_lock_);
241
242 rtc::CriticalSection ts_stats_lock_;
243
244 std::unique_ptr<rtc::TimestampWrapAroundHandler> rtp_ts_wraparound_handler_;
245 // The rtp timestamp of the first played out audio frame.
246 int64_t capture_start_rtp_time_stamp_;
247 // The capture ntp time (in local timebase) of the first played out audio
248 // frame.
249 int64_t capture_start_ntp_time_ms_ RTC_GUARDED_BY(ts_stats_lock_);
250
251 // uses
252 ProcessThread* _moduleProcessThreadPtr;
253 AudioDeviceModule* _audioDeviceModulePtr;
Niels Möller530ead42018-10-04 12:28:39254 float _outputGain RTC_GUARDED_BY(volume_settings_critsect_);
255
256 // An associated send channel.
257 rtc::CriticalSection assoc_send_channel_lock_;
258 ChannelSend* associated_send_channel_
259 RTC_GUARDED_BY(assoc_send_channel_lock_);
260
261 PacketRouter* packet_router_ = nullptr;
262
263 rtc::ThreadChecker construction_thread_;
Benjamin Wright84583f62018-10-04 21:22:34264
Niels Möller7d76a312018-10-26 10:57:07265 MediaTransportInterface* const media_transport_;
266
Benjamin Wright84583f62018-10-04 21:22:34267 // E2EE Audio Frame Decryption
Benjamin Wright78410ad2018-10-25 16:52:57268 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_;
Benjamin Wrightbfb444c2018-10-15 17:20:24269 webrtc::CryptoOptions crypto_options_;
Niels Möller530ead42018-10-04 12:28:39270};
271
272} // namespace voe
273} // namespace webrtc
274
275#endif // AUDIO_CHANNEL_RECEIVE_H_