blob: 306d6a8b3e56d60e459f9069305d25af12389e13 [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öller530ead42018-10-04 14:28:39 +020023#include "common_types.h" // NOLINT(build/include)
24#include "modules/audio_coding/include/audio_coding_module.h"
25#include "modules/audio_processing/rms_level.h"
26#include "modules/rtp_rtcp/include/rtp_rtcp.h"
27#include "rtc_base/criticalsection.h"
28#include "rtc_base/task_queue.h"
29#include "rtc_base/thread_checker.h"
30
31// TODO(solenberg, nisse): This file contains a few NOLINT marks, to silence
32// warnings about use of unsigned short, and non-const reference arguments.
33// These need cleanup, in a separate cl.
34
35namespace rtc {
36class TimestampWrapAroundHandler;
37}
38
39namespace webrtc {
40
Benjamin Wright84583f62018-10-04 14:22:34 -070041class FrameEncryptorInterface;
Niels Möller530ead42018-10-04 14:28:39 +020042class PacketRouter;
43class ProcessThread;
44class RateLimiter;
45class RtcEventLog;
46class RtpRtcp;
47class RtpTransportControllerSendInterface;
48
49struct SenderInfo;
50
51struct CallSendStatistics {
52 int64_t rttMs;
53 size_t bytesSent;
54 int packetsSent;
55};
56
57// See section 6.4.2 in http://www.ietf.org/rfc/rfc3550.txt for details.
58struct ReportBlock {
59 uint32_t sender_SSRC; // SSRC of sender
60 uint32_t source_SSRC;
61 uint8_t fraction_lost;
62 int32_t cumulative_num_packets_lost;
63 uint32_t extended_highest_sequence_number;
64 uint32_t interarrival_jitter;
65 uint32_t last_SR_timestamp;
66 uint32_t delay_since_last_SR;
67};
68
69namespace voe {
70
71class RtpPacketSenderProxy;
72class TransportFeedbackProxy;
73class TransportSequenceNumberProxy;
74class VoERtcpObserver;
75
76// Helper class to simplify locking scheme for members that are accessed from
77// multiple threads.
78// Example: a member can be set on thread T1 and read by an internal audio
79// thread T2. Accessing the member via this class ensures that we are
80// safe and also avoid TSan v2 warnings.
81class ChannelSendState {
82 public:
83 struct State {
84 bool sending = false;
85 };
86
87 ChannelSendState() {}
88 virtual ~ChannelSendState() {}
89
90 void Reset() {
91 rtc::CritScope lock(&lock_);
92 state_ = State();
93 }
94
95 State Get() const {
96 rtc::CritScope lock(&lock_);
97 return state_;
98 }
99
100 void SetSending(bool enable) {
101 rtc::CritScope lock(&lock_);
102 state_.sending = enable;
103 }
104
105 private:
106 rtc::CriticalSection lock_;
107 State state_;
108};
109
110class ChannelSend
111 : public Transport,
112 public AudioPacketizationCallback, // receive encoded packets from the
113 // ACM
114 public OverheadObserver {
115 public:
116 // TODO(nisse): Make OnUplinkPacketLossRate public, and delete friend
117 // declaration.
118 friend class VoERtcpObserver;
119
120 ChannelSend(rtc::TaskQueue* encoder_queue,
121 ProcessThread* module_process_thread,
122 RtcpRttStats* rtcp_rtt_stats,
Benjamin Wright84583f62018-10-04 14:22:34 -0700123 RtcEventLog* rtc_event_log,
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700124 FrameEncryptorInterface* frame_encryptor,
125 const webrtc::CryptoOptions& crypto_options);
Niels Möller530ead42018-10-04 14:28:39 +0200126
127 virtual ~ChannelSend();
128
129 // Send using this encoder, with this payload type.
130 bool SetEncoder(int payload_type, std::unique_ptr<AudioEncoder> encoder);
131 void ModifyEncoder(
132 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier);
133
134 // API methods
135
136 // VoEBase
137 int32_t StartSend();
138 void StopSend();
139
140 // Codecs
141 void SetBitRate(int bitrate_bps, int64_t probing_interval_ms);
142 bool EnableAudioNetworkAdaptor(const std::string& config_string);
143 void DisableAudioNetworkAdaptor();
144
145 // TODO(nisse): Modifies decoder, but not used?
146 void SetReceiverFrameLengthRange(int min_frame_length_ms,
147 int max_frame_length_ms);
148
149 // Network
150 void RegisterTransport(Transport* transport);
151 // TODO(nisse, solenberg): Delete when VoENetwork is deleted.
152 int32_t ReceivedRTCPPacket(const uint8_t* data, size_t length);
153
154 // Muting, Volume and Level.
155 void SetInputMute(bool enable);
156
157 // Stats.
158 ANAStats GetANAStatistics() const;
159
160 // Used by AudioSendStream.
161 RtpRtcp* GetRtpRtcp() const;
162
163 // DTMF.
164 int SendTelephoneEventOutband(int event, int duration_ms);
165 int SetSendTelephoneEventPayloadType(int payload_type, int payload_frequency);
166
167 // RTP+RTCP
168 int SetLocalSSRC(unsigned int ssrc);
169
170 void SetMid(const std::string& mid, int extension_id);
171 int SetSendAudioLevelIndicationStatus(bool enable, unsigned char id);
172 void EnableSendTransportSequenceNumber(int id);
173
174 void RegisterSenderCongestionControlObjects(
175 RtpTransportControllerSendInterface* transport,
176 RtcpBandwidthObserver* bandwidth_observer);
177 void ResetSenderCongestionControlObjects();
178 void SetRTCPStatus(bool enable);
179 int SetRTCP_CNAME(const char cName[256]);
180 int GetRemoteRTCPReportBlocks(std::vector<ReportBlock>* report_blocks);
181 int GetRTPStatistics(CallSendStatistics& stats); // NOLINT
182 void SetNACKStatus(bool enable, int maxNumberOfPackets);
183
184 // From AudioPacketizationCallback in the ACM
185 int32_t SendData(FrameType frameType,
186 uint8_t payloadType,
187 uint32_t timeStamp,
188 const uint8_t* payloadData,
189 size_t payloadSize,
190 const RTPFragmentationHeader* fragmentation) override;
191
192 // From Transport (called by the RTP/RTCP module)
193 bool SendRtp(const uint8_t* data,
194 size_t len,
195 const PacketOptions& packet_options) override;
196 bool SendRtcp(const uint8_t* data, size_t len) override;
197
198 int PreferredSampleRate() const;
199
200 bool Sending() const { return channel_state_.Get().sending; }
201 RtpRtcp* RtpRtcpModulePtr() const { return _rtpRtcpModule.get(); }
202
203 // ProcessAndEncodeAudio() posts a task on the shared encoder task queue,
204 // which in turn calls (on the queue) ProcessAndEncodeAudioOnTaskQueue() where
205 // the actual processing of the audio takes place. The processing mainly
206 // consists of encoding and preparing the result for sending by adding it to a
207 // send queue.
208 // The main reason for using a task queue here is to release the native,
209 // OS-specific, audio capture thread as soon as possible to ensure that it
210 // can go back to sleep and be prepared to deliver an new captured audio
211 // packet.
212 void ProcessAndEncodeAudio(std::unique_ptr<AudioFrame> audio_frame);
213
214 void SetTransportOverhead(size_t transport_overhead_per_packet);
215
216 // From OverheadObserver in the RTP/RTCP module
217 void OnOverheadChanged(size_t overhead_bytes_per_packet) override;
218
219 // The existence of this function alongside OnUplinkPacketLossRate is
220 // a compromise. We want the encoder to be agnostic of the PLR source, but
221 // we also don't want it to receive conflicting information from TWCC and
222 // from RTCP-XR.
223 void OnTwccBasedUplinkPacketLossRate(float packet_loss_rate);
224
225 void OnRecoverableUplinkPacketLossRate(float recoverable_packet_loss_rate);
226
227 int64_t GetRTT() const;
228
Benjamin Wright84583f62018-10-04 14:22:34 -0700229 // E2EE Custom Audio Frame Encryption
230 void SetFrameEncryptor(FrameEncryptorInterface* frame_encryptor);
231
Niels Möller530ead42018-10-04 14:28:39 +0200232 private:
233 class ProcessAndEncodeAudioTask;
234
235 void Init();
236 void Terminate();
237
238 void OnUplinkPacketLossRate(float packet_loss_rate);
239 bool InputMute() const;
240
241 int ResendPackets(const uint16_t* sequence_numbers, int length);
242
243 int SetSendRtpHeaderExtension(bool enable,
244 RTPExtensionType type,
245 unsigned char id);
246
247 void UpdateOverheadForEncoder()
248 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
249
250 int GetRtpTimestampRateHz() const;
251
252 // Called on the encoder task queue when a new input audio frame is ready
253 // for encoding.
254 void ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input);
255
256 rtc::CriticalSection _callbackCritSect;
257 rtc::CriticalSection volume_settings_critsect_;
258
259 ChannelSendState channel_state_;
260
261 RtcEventLog* const event_log_;
262
263 std::unique_ptr<RtpRtcp> _rtpRtcpModule;
264
265 std::unique_ptr<AudioCodingModule> audio_coding_;
266 uint32_t _timeStamp RTC_GUARDED_BY(encoder_queue_);
267
268 uint16_t send_sequence_number_;
269
270 // uses
271 ProcessThread* _moduleProcessThreadPtr;
272 Transport* _transportPtr; // WebRtc socket or external transport
273 RmsLevel rms_level_ RTC_GUARDED_BY(encoder_queue_);
274 bool input_mute_ RTC_GUARDED_BY(volume_settings_critsect_);
275 bool previous_frame_muted_ RTC_GUARDED_BY(encoder_queue_);
276 // VoeRTP_RTCP
277 // TODO(henrika): can today be accessed on the main thread and on the
278 // task queue; hence potential race.
279 bool _includeAudioLevelIndication;
280 size_t transport_overhead_per_packet_
281 RTC_GUARDED_BY(overhead_per_packet_lock_);
282 size_t rtp_overhead_per_packet_ RTC_GUARDED_BY(overhead_per_packet_lock_);
283 rtc::CriticalSection overhead_per_packet_lock_;
284 // RtcpBandwidthObserver
285 std::unique_ptr<VoERtcpObserver> rtcp_observer_;
286
287 PacketRouter* packet_router_ = nullptr;
288 std::unique_ptr<TransportFeedbackProxy> feedback_observer_proxy_;
289 std::unique_ptr<TransportSequenceNumberProxy> seq_num_allocator_proxy_;
290 std::unique_ptr<RtpPacketSenderProxy> rtp_packet_sender_proxy_;
291 std::unique_ptr<RateLimiter> retransmission_rate_limiter_;
292
293 rtc::ThreadChecker construction_thread_;
294
295 const bool use_twcc_plr_for_ana_;
296
297 rtc::CriticalSection encoder_queue_lock_;
298 bool encoder_queue_is_active_ RTC_GUARDED_BY(encoder_queue_lock_) = false;
299 rtc::TaskQueue* encoder_queue_ = nullptr;
Benjamin Wright84583f62018-10-04 14:22:34 -0700300
301 // E2EE Audio Frame Encryption
302 FrameEncryptorInterface* frame_encryptor_ = nullptr;
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700303 // E2EE Frame Encryption Options
304 webrtc::CryptoOptions crypto_options_;
Niels Möller530ead42018-10-04 14:28:39 +0200305};
306
307} // namespace voe
308} // namespace webrtc
309
310#endif // AUDIO_CHANNEL_SEND_H_