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