blob: 3d194a7ecde498fbb47774753813d003a823266e [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_SEND_H_
12#define AUDIO_CHANNEL_SEND_H_
13
14#include <map>
15#include <memory>
16#include <string>
17#include <vector>
18
19#include "api/audio/audio_frame.h"
20#include "api/audio_codecs/audio_encoder.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 "common_types.h" // NOLINT(build/include)
25#include "modules/audio_coding/include/audio_coding_module.h"
26#include "modules/audio_processing/rms_level.h"
27#include "modules/rtp_rtcp/include/rtp_rtcp.h"
28#include "rtc_base/criticalsection.h"
29#include "rtc_base/task_queue.h"
30#include "rtc_base/thread_checker.h"
31
32// TODO(solenberg, nisse): This file contains a few NOLINT marks, to silence
33// warnings about use of unsigned short, and non-const reference arguments.
34// These need cleanup, in a separate cl.
35
36namespace rtc {
37class TimestampWrapAroundHandler;
38}
39
40namespace webrtc {
41
Benjamin Wright84583f62018-10-04 14:22:34 -070042class FrameEncryptorInterface;
Niels Möller530ead42018-10-04 14:28:39 +020043class PacketRouter;
44class ProcessThread;
45class RateLimiter;
46class RtcEventLog;
47class RtpRtcp;
48class RtpTransportControllerSendInterface;
49
50struct SenderInfo;
51
52struct CallSendStatistics {
53 int64_t rttMs;
54 size_t bytesSent;
55 int packetsSent;
56};
57
58// See section 6.4.2 in http://www.ietf.org/rfc/rfc3550.txt for details.
59struct ReportBlock {
60 uint32_t sender_SSRC; // SSRC of sender
61 uint32_t source_SSRC;
62 uint8_t fraction_lost;
63 int32_t cumulative_num_packets_lost;
64 uint32_t extended_highest_sequence_number;
65 uint32_t interarrival_jitter;
66 uint32_t last_SR_timestamp;
67 uint32_t delay_since_last_SR;
68};
69
70namespace voe {
71
72class RtpPacketSenderProxy;
73class TransportFeedbackProxy;
74class TransportSequenceNumberProxy;
75class VoERtcpObserver;
76
77// Helper class to simplify locking scheme for members that are accessed from
78// multiple threads.
79// Example: a member can be set on thread T1 and read by an internal audio
80// thread T2. Accessing the member via this class ensures that we are
81// safe and also avoid TSan v2 warnings.
82class ChannelSendState {
83 public:
84 struct State {
85 bool sending = false;
86 };
87
88 ChannelSendState() {}
89 virtual ~ChannelSendState() {}
90
91 void Reset() {
92 rtc::CritScope lock(&lock_);
93 state_ = State();
94 }
95
96 State Get() const {
97 rtc::CritScope lock(&lock_);
98 return state_;
99 }
100
101 void SetSending(bool enable) {
102 rtc::CritScope lock(&lock_);
103 state_.sending = enable;
104 }
105
106 private:
107 rtc::CriticalSection lock_;
108 State state_;
109};
110
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800111class ChannelSend : public Transport,
112 public AudioPacketizationCallback, // receive encoded
113 // packets from the ACM
114 public OverheadObserver,
115 public TargetTransferRateObserver {
Niels Möller530ead42018-10-04 14:28:39 +0200116 public:
117 // TODO(nisse): Make OnUplinkPacketLossRate public, and delete friend
118 // declaration.
119 friend class VoERtcpObserver;
120
121 ChannelSend(rtc::TaskQueue* encoder_queue,
122 ProcessThread* module_process_thread,
Niels Möller7d76a312018-10-26 12:57:07 +0200123 MediaTransportInterface* media_transport,
Niels Möller530ead42018-10-04 14:28:39 +0200124 RtcpRttStats* rtcp_rtt_stats,
Benjamin Wright84583f62018-10-04 14:22:34 -0700125 RtcEventLog* rtc_event_log,
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700126 FrameEncryptorInterface* frame_encryptor,
Johannes Kron9190b822018-10-29 11:22:05 +0100127 const webrtc::CryptoOptions& crypto_options,
Jiawei Ou55718122018-11-09 13:17:39 -0800128 bool extmap_allow_mixed,
129 int rtcp_report_interval_ms);
Niels Möller530ead42018-10-04 14:28:39 +0200130
131 virtual ~ChannelSend();
132
133 // Send using this encoder, with this payload type.
134 bool SetEncoder(int payload_type, std::unique_ptr<AudioEncoder> encoder);
135 void ModifyEncoder(
136 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier);
137
138 // API methods
139
Niels Möller26815232018-11-16 09:32:40 +0100140 void StartSend();
Niels Möller530ead42018-10-04 14:28:39 +0200141 void StopSend();
142
143 // Codecs
144 void SetBitRate(int bitrate_bps, int64_t probing_interval_ms);
Sebastian Jansson359d60a2018-10-25 16:22:02 +0200145 int GetBitRate() const;
Niels Möller530ead42018-10-04 14:28:39 +0200146
147 // Network
148 void RegisterTransport(Transport* transport);
Niels Möller26815232018-11-16 09:32:40 +0100149
150 bool ReceivedRTCPPacket(const uint8_t* data, size_t length);
Niels Möller530ead42018-10-04 14:28:39 +0200151
152 // Muting, Volume and Level.
153 void SetInputMute(bool enable);
154
155 // Stats.
156 ANAStats GetANAStatistics() const;
157
158 // Used by AudioSendStream.
159 RtpRtcp* GetRtpRtcp() const;
160
161 // DTMF.
Niels Möller26815232018-11-16 09:32:40 +0100162 bool SendTelephoneEventOutband(int event, int duration_ms);
163 bool SetSendTelephoneEventPayloadType(int payload_type,
164 int payload_frequency);
Niels Möller530ead42018-10-04 14:28:39 +0200165
166 // RTP+RTCP
Niels Möller26815232018-11-16 09:32:40 +0100167 void SetLocalSSRC(uint32_t ssrc);
Niels Möller530ead42018-10-04 14:28:39 +0200168
169 void SetMid(const std::string& mid, int extension_id);
Johannes Kron9190b822018-10-29 11:22:05 +0100170 void SetExtmapAllowMixed(bool extmap_allow_mixed);
Niels Möller26815232018-11-16 09:32:40 +0100171 void SetSendAudioLevelIndicationStatus(bool enable, int id);
Niels Möller530ead42018-10-04 14:28:39 +0200172 void EnableSendTransportSequenceNumber(int id);
173
174 void RegisterSenderCongestionControlObjects(
175 RtpTransportControllerSendInterface* transport,
176 RtcpBandwidthObserver* bandwidth_observer);
177 void ResetSenderCongestionControlObjects();
Niels Möller26815232018-11-16 09:32:40 +0100178 void SetRTCP_CNAME(absl::string_view c_name);
179 std::vector<ReportBlock> GetRemoteRTCPReportBlocks() const;
180 CallSendStatistics GetRTCPStatistics() const;
Niels Möller530ead42018-10-04 14:28:39 +0200181 void SetNACKStatus(bool enable, int maxNumberOfPackets);
182
Niels Möller530ead42018-10-04 14:28:39 +0200183 // ProcessAndEncodeAudio() posts a task on the shared encoder task queue,
184 // which in turn calls (on the queue) ProcessAndEncodeAudioOnTaskQueue() where
185 // the actual processing of the audio takes place. The processing mainly
186 // consists of encoding and preparing the result for sending by adding it to a
187 // send queue.
188 // The main reason for using a task queue here is to release the native,
189 // OS-specific, audio capture thread as soon as possible to ensure that it
190 // can go back to sleep and be prepared to deliver an new captured audio
191 // packet.
192 void ProcessAndEncodeAudio(std::unique_ptr<AudioFrame> audio_frame);
193
Niels Möller26815232018-11-16 09:32:40 +0100194 public:
Niels Möller530ead42018-10-04 14:28:39 +0200195 void SetTransportOverhead(size_t transport_overhead_per_packet);
196
Niels Möller530ead42018-10-04 14:28:39 +0200197 // The existence of this function alongside OnUplinkPacketLossRate is
198 // a compromise. We want the encoder to be agnostic of the PLR source, but
199 // we also don't want it to receive conflicting information from TWCC and
200 // from RTCP-XR.
201 void OnTwccBasedUplinkPacketLossRate(float packet_loss_rate);
202
203 void OnRecoverableUplinkPacketLossRate(float recoverable_packet_loss_rate);
204
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800205 // Returns the RTT in milliseconds.
Niels Möller530ead42018-10-04 14:28:39 +0200206 int64_t GetRTT() const;
207
Benjamin Wright84583f62018-10-04 14:22:34 -0700208 // E2EE Custom Audio Frame Encryption
Benjamin Wright78410ad2018-10-25 09:52:57 -0700209 void SetFrameEncryptor(
210 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor);
Benjamin Wright84583f62018-10-04 14:22:34 -0700211
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800212 // In RTP we currently rely on RTCP packets (|ReceivedRTCPPacket|) to inform
213 // about RTT.
214 // In media transport we rely on the TargetTransferRateObserver instead.
215 // In other words, if you are using RTP, you should expect
216 // |ReceivedRTCPPacket| to be called, if you are using media transport,
217 // |OnTargetTransferRate| will be called.
218 //
219 // In future, RTP media will move to the media transport implementation and
220 // these conditions will be removed.
221 void OnTargetTransferRate(TargetTransferRate rate) override;
222
Niels Möller530ead42018-10-04 14:28:39 +0200223 private:
224 class ProcessAndEncodeAudioTask;
225
Niels Möller26815232018-11-16 09:32:40 +0100226 // From AudioPacketizationCallback in the ACM
227 int32_t SendData(FrameType frameType,
228 uint8_t payloadType,
229 uint32_t timeStamp,
230 const uint8_t* payloadData,
231 size_t payloadSize,
232 const RTPFragmentationHeader* fragmentation) override;
233
234 // From Transport (called by the RTP/RTCP module)
235 bool SendRtp(const uint8_t* data,
236 size_t len,
237 const PacketOptions& packet_options) override;
238 bool SendRtcp(const uint8_t* data, size_t len) override;
239
Niels Möller26815232018-11-16 09:32:40 +0100240 bool Sending() const { return channel_state_.Get().sending; }
241
242 // From OverheadObserver in the RTP/RTCP module
243 void OnOverheadChanged(size_t overhead_bytes_per_packet) override;
244
Niels Möller530ead42018-10-04 14:28:39 +0200245 void OnUplinkPacketLossRate(float packet_loss_rate);
246 bool InputMute() const;
247
248 int ResendPackets(const uint16_t* sequence_numbers, int length);
249
Niels Möller26815232018-11-16 09:32:40 +0100250 int SetSendRtpHeaderExtension(bool enable, RTPExtensionType type, int id);
Niels Möller530ead42018-10-04 14:28:39 +0200251
252 void UpdateOverheadForEncoder()
253 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
254
255 int GetRtpTimestampRateHz() const;
256
Niels Möller7d76a312018-10-26 12:57:07 +0200257 int32_t SendRtpAudio(FrameType frameType,
258 uint8_t payloadType,
259 uint32_t timeStamp,
260 rtc::ArrayView<const uint8_t> payload,
261 const RTPFragmentationHeader* fragmentation);
262
263 int32_t SendMediaTransportAudio(FrameType frameType,
264 uint8_t payloadType,
265 uint32_t timeStamp,
266 rtc::ArrayView<const uint8_t> payload,
267 const RTPFragmentationHeader* fragmentation);
268
269 // Return media transport or nullptr if using RTP.
270 MediaTransportInterface* media_transport() { return media_transport_; }
271
Niels Möller530ead42018-10-04 14:28:39 +0200272 // Called on the encoder task queue when a new input audio frame is ready
273 // for encoding.
274 void ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input);
275
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800276 void OnReceivedRtt(int64_t rtt_ms);
277
Niels Möller530ead42018-10-04 14:28:39 +0200278 rtc::CriticalSection _callbackCritSect;
279 rtc::CriticalSection volume_settings_critsect_;
280
281 ChannelSendState channel_state_;
282
283 RtcEventLog* const event_log_;
284
285 std::unique_ptr<RtpRtcp> _rtpRtcpModule;
286
287 std::unique_ptr<AudioCodingModule> audio_coding_;
288 uint32_t _timeStamp RTC_GUARDED_BY(encoder_queue_);
289
290 uint16_t send_sequence_number_;
291
292 // uses
293 ProcessThread* _moduleProcessThreadPtr;
294 Transport* _transportPtr; // WebRtc socket or external transport
295 RmsLevel rms_level_ RTC_GUARDED_BY(encoder_queue_);
296 bool input_mute_ RTC_GUARDED_BY(volume_settings_critsect_);
297 bool previous_frame_muted_ RTC_GUARDED_BY(encoder_queue_);
298 // VoeRTP_RTCP
299 // TODO(henrika): can today be accessed on the main thread and on the
300 // task queue; hence potential race.
301 bool _includeAudioLevelIndication;
302 size_t transport_overhead_per_packet_
303 RTC_GUARDED_BY(overhead_per_packet_lock_);
304 size_t rtp_overhead_per_packet_ RTC_GUARDED_BY(overhead_per_packet_lock_);
305 rtc::CriticalSection overhead_per_packet_lock_;
306 // RtcpBandwidthObserver
307 std::unique_ptr<VoERtcpObserver> rtcp_observer_;
308
309 PacketRouter* packet_router_ = nullptr;
310 std::unique_ptr<TransportFeedbackProxy> feedback_observer_proxy_;
311 std::unique_ptr<TransportSequenceNumberProxy> seq_num_allocator_proxy_;
312 std::unique_ptr<RtpPacketSenderProxy> rtp_packet_sender_proxy_;
313 std::unique_ptr<RateLimiter> retransmission_rate_limiter_;
314
315 rtc::ThreadChecker construction_thread_;
316
317 const bool use_twcc_plr_for_ana_;
318
319 rtc::CriticalSection encoder_queue_lock_;
320 bool encoder_queue_is_active_ RTC_GUARDED_BY(encoder_queue_lock_) = false;
321 rtc::TaskQueue* encoder_queue_ = nullptr;
Benjamin Wright84583f62018-10-04 14:22:34 -0700322
Niels Möller7d76a312018-10-26 12:57:07 +0200323 MediaTransportInterface* const media_transport_;
324 int media_transport_sequence_number_ RTC_GUARDED_BY(encoder_queue_) = 0;
325
326 rtc::CriticalSection media_transport_lock_;
327 // Currently set by SetLocalSSRC.
328 uint64_t media_transport_channel_id_ RTC_GUARDED_BY(&media_transport_lock_) =
329 0;
330 // Cache payload type and sampling frequency from most recent call to
331 // SetEncoder. Needed to set MediaTransportEncodedAudioFrame metadata, and
332 // invalidate on encoder change.
333 int media_transport_payload_type_ RTC_GUARDED_BY(&media_transport_lock_);
334 int media_transport_sampling_frequency_
335 RTC_GUARDED_BY(&media_transport_lock_);
336
Benjamin Wright84583f62018-10-04 14:22:34 -0700337 // E2EE Audio Frame Encryption
Benjamin Wright78410ad2018-10-25 09:52:57 -0700338 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_;
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700339 // E2EE Frame Encryption Options
340 webrtc::CryptoOptions crypto_options_;
Piotr (Peter) Slatala1eebec92018-11-16 09:03:35 -0800341
342 rtc::CriticalSection bitrate_crit_section_;
343 int configured_bitrate_bps_ RTC_GUARDED_BY(bitrate_crit_section_) = 0;
Niels Möller530ead42018-10-04 14:28:39 +0200344};
345
346} // namespace voe
347} // namespace webrtc
348
349#endif // AUDIO_CHANNEL_SEND_H_