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