blob: 8c9d623b19f623efe034866c538886eb89300950 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000011#ifndef WEBRTC_VOICE_ENGINE_CHANNEL_H_
12#define WEBRTC_VOICE_ENGINE_CHANNEL_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
kwibergb7f89d62016-02-17 10:04:18 -080014#include <memory>
15
aleloiaed581a2016-10-20 06:32:39 -070016#include "webrtc/api/audio/audio_mixer.h"
ossueb1fde42017-05-02 06:46:30 -070017#include "webrtc/api/audio_codecs/audio_encoder.h"
kjellandera69d9732016-08-31 07:33:05 -070018#include "webrtc/api/call/audio_sink.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010019#include "webrtc/base/criticalsection.h"
henrikaec6fbd22017-03-31 05:43:36 -070020#include "webrtc/base/event.h"
henrik.lundin96bd5022016-04-06 04:13:56 -070021#include "webrtc/base/optional.h"
tommi0a2391f2017-03-21 02:31:51 -070022#include "webrtc/base/thread_checker.h"
xians@webrtc.org2f84afa2013-07-31 16:23:37 +000023#include "webrtc/common_audio/resampler/include/push_resampler.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000024#include "webrtc/common_types.h"
kwibergc8d071e2016-04-06 12:22:38 -070025#include "webrtc/modules/audio_coding/acm2/codec_manager.h"
26#include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
kjellander3e6db232015-11-26 04:44:54 -080027#include "webrtc/modules/audio_coding/include/audio_coding_module.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010028#include "webrtc/modules/audio_conference_mixer/include/audio_conference_mixer_defines.h"
andrew@webrtc.org382c0c22014-05-05 18:22:21 +000029#include "webrtc/modules/audio_processing/rms_level.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010030#include "webrtc/modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
31#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
hbos8d609f62017-04-10 07:39:05 -070032#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010033#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
henrik.lundin92a7a182017-03-07 01:58:55 -080034#include "webrtc/voice_engine/audio_level.h"
kwiberg97744472017-01-10 01:12:51 -080035#include "webrtc/voice_engine/file_player.h"
36#include "webrtc/voice_engine/file_recorder.h"
solenberg88499ec2016-09-07 07:34:41 -070037#include "webrtc/voice_engine/include/voe_base.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000038#include "webrtc/voice_engine/include/voe_network.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000039#include "webrtc/voice_engine/shared_data.h"
40#include "webrtc/voice_engine/voice_engine_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000041
wu@webrtc.org94454b72014-06-05 20:34:08 +000042namespace rtc {
wu@webrtc.org94454b72014-06-05 20:34:08 +000043class TimestampWrapAroundHandler;
44}
45
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +000046namespace webrtc {
47
tnakamura@webrtc.orgaa4d96a2013-07-16 19:25:04 +000048class AudioDeviceModule;
tnakamura@webrtc.orgaa4d96a2013-07-16 19:25:04 +000049class FileWrapper;
Stefan Holmerb86d4e42015-12-07 10:26:18 +010050class PacketRouter;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000051class ProcessThread;
Erik Språng737336d2016-07-29 12:59:36 +020052class RateLimiter;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000053class ReceiveStatistics;
wu@webrtc.org82c4b852014-05-20 22:55:01 +000054class RemoteNtpTimeEstimator;
ivocb04965c2015-09-09 00:09:43 -070055class RtcEventLog;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000056class RTPPayloadRegistry;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000057class RTPReceiverAudio;
nisse657bab22017-02-21 06:28:10 -080058class RtpPacketReceived;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000059class RtpRtcp;
nisseb8f9a322017-03-27 05:36:15 -070060class RtpTransportControllerSendInterface;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000061class TelephoneEventHandler;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000062class VoERTPObserver;
63class VoiceEngineObserver;
niklase@google.com470e71d2011-07-07 08:21:25 +000064
65struct CallStatistics;
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +000066struct ReportBlock;
67struct SenderInfo;
niklase@google.com470e71d2011-07-07 08:21:25 +000068
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +000069namespace voe {
70
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +000071class OutputMixer;
ivoc14d5dbe2016-07-04 07:06:55 -070072class RtcEventLogProxy;
michaelt9332b7d2016-11-30 07:51:13 -080073class RtcpRttStatsProxy;
Stefan Holmerb86d4e42015-12-07 10:26:18 +010074class RtpPacketSenderProxy;
niklase@google.com470e71d2011-07-07 08:21:25 +000075class Statistics;
Stefan Holmerb86d4e42015-12-07 10:26:18 +010076class TransportFeedbackProxy;
Stefan Holmerb86d4e42015-12-07 10:26:18 +010077class TransportSequenceNumberProxy;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +000078class VoERtcpObserver;
niklase@google.com470e71d2011-07-07 08:21:25 +000079
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +000080// Helper class to simplify locking scheme for members that are accessed from
81// multiple threads.
82// Example: a member can be set on thread T1 and read by an internal audio
83// thread T2. Accessing the member via this class ensures that we are
84// safe and also avoid TSan v2 warnings.
85class ChannelState {
86 public:
kwiberg55b97fe2016-01-28 05:22:45 -080087 struct State {
solenberg11ace152016-09-15 04:29:13 -070088 bool output_file_playing = false;
89 bool input_file_playing = false;
90 bool playing = false;
91 bool sending = false;
kwiberg55b97fe2016-01-28 05:22:45 -080092 };
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +000093
kwiberg55b97fe2016-01-28 05:22:45 -080094 ChannelState() {}
95 virtual ~ChannelState() {}
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +000096
kwiberg55b97fe2016-01-28 05:22:45 -080097 void Reset() {
98 rtc::CritScope lock(&lock_);
99 state_ = State();
100 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000101
kwiberg55b97fe2016-01-28 05:22:45 -0800102 State Get() const {
103 rtc::CritScope lock(&lock_);
104 return state_;
105 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000106
kwiberg55b97fe2016-01-28 05:22:45 -0800107 void SetOutputFilePlaying(bool enable) {
108 rtc::CritScope lock(&lock_);
109 state_.output_file_playing = enable;
110 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000111
kwiberg55b97fe2016-01-28 05:22:45 -0800112 void SetInputFilePlaying(bool enable) {
113 rtc::CritScope lock(&lock_);
114 state_.input_file_playing = enable;
115 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000116
kwiberg55b97fe2016-01-28 05:22:45 -0800117 void SetPlaying(bool enable) {
118 rtc::CritScope lock(&lock_);
119 state_.playing = enable;
120 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000121
kwiberg55b97fe2016-01-28 05:22:45 -0800122 void SetSending(bool enable) {
123 rtc::CritScope lock(&lock_);
124 state_.sending = enable;
125 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000126
kwiberg55b97fe2016-01-28 05:22:45 -0800127 private:
pbosd8de1152016-02-01 09:00:51 -0800128 rtc::CriticalSection lock_;
kwiberg55b97fe2016-01-28 05:22:45 -0800129 State state_;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000130};
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
kwiberg55b97fe2016-01-28 05:22:45 -0800132class Channel
133 : public RtpData,
134 public RtpFeedback,
135 public FileCallback, // receiving notification from file player &
136 // recorder
137 public Transport,
kwiberg55b97fe2016-01-28 05:22:45 -0800138 public AudioPacketizationCallback, // receive encoded packets from the
139 // ACM
michaeltbf65be52016-12-15 06:24:49 -0800140 public MixerParticipant, // supplies output mixer with audio frames
141 public OverheadObserver {
kwiberg55b97fe2016-01-28 05:22:45 -0800142 public:
143 friend class VoERtcpObserver;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000144
kwiberg55b97fe2016-01-28 05:22:45 -0800145 enum { KNumSocketThreads = 1 };
146 enum { KNumberOfSocketBuffers = 8 };
147 virtual ~Channel();
henrikaec6fbd22017-03-31 05:43:36 -0700148 static int32_t CreateChannel(Channel*& channel,
149 int32_t channelId,
150 uint32_t instanceId,
151 const VoEBase::ChannelConfig& config);
kwiberg55b97fe2016-01-28 05:22:45 -0800152 Channel(int32_t channelId,
153 uint32_t instanceId,
solenberg88499ec2016-09-07 07:34:41 -0700154 const VoEBase::ChannelConfig& config);
kwiberg55b97fe2016-01-28 05:22:45 -0800155 int32_t Init();
kwiberg1c07c702017-03-27 07:15:49 -0700156 void RegisterLegacyReceiveCodecs();
tommi0a2391f2017-03-21 02:31:51 -0700157 void Terminate();
kwiberg55b97fe2016-01-28 05:22:45 -0800158 int32_t SetEngineInformation(Statistics& engineStatistics,
159 OutputMixer& outputMixer,
kwiberg55b97fe2016-01-28 05:22:45 -0800160 ProcessThread& moduleProcessThread,
161 AudioDeviceModule& audioDeviceModule,
162 VoiceEngineObserver* voiceEngineObserver,
henrikaec6fbd22017-03-31 05:43:36 -0700163 rtc::CriticalSection* callbackCritSect,
164 rtc::TaskQueue* encoder_queue);
niklase@google.com470e71d2011-07-07 08:21:25 +0000165
kwibergb7f89d62016-02-17 10:04:18 -0800166 void SetSink(std::unique_ptr<AudioSinkInterface> sink);
Tommif888bb52015-12-12 01:37:01 +0100167
ossu29b1a8d2016-06-13 07:34:51 -0700168 // TODO(ossu): Don't use! It's only here to confirm that the decoder factory
169 // passed into AudioReceiveStream is the same as the one set when creating the
170 // ADM. Once Channel creation is moved into Audio{Send,Receive}Stream this can
171 // go.
172 const rtc::scoped_refptr<AudioDecoderFactory>& GetAudioDecoderFactory() const;
173
kwiberg1c07c702017-03-27 07:15:49 -0700174 void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs);
175
ossu1ffbd6c2017-04-06 12:05:04 -0700176 // Send using this encoder, with this payload type.
177 bool SetEncoder(int payload_type, std::unique_ptr<AudioEncoder> encoder);
ossu20a4b3f2017-04-27 02:08:52 -0700178 void ModifyEncoder(
179 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier);
ossu1ffbd6c2017-04-06 12:05:04 -0700180
kwiberg55b97fe2016-01-28 05:22:45 -0800181 // API methods
niklase@google.com470e71d2011-07-07 08:21:25 +0000182
kwiberg55b97fe2016-01-28 05:22:45 -0800183 // VoEBase
184 int32_t StartPlayout();
185 int32_t StopPlayout();
186 int32_t StartSend();
henrikaec6fbd22017-03-31 05:43:36 -0700187 void StopSend();
kwiberg55b97fe2016-01-28 05:22:45 -0800188 int32_t RegisterVoiceEngineObserver(VoiceEngineObserver& observer);
189 int32_t DeRegisterVoiceEngineObserver();
niklase@google.com470e71d2011-07-07 08:21:25 +0000190
kwiberg55b97fe2016-01-28 05:22:45 -0800191 // VoECodec
192 int32_t GetSendCodec(CodecInst& codec);
193 int32_t GetRecCodec(CodecInst& codec);
194 int32_t SetSendCodec(const CodecInst& codec);
minyue78b4d562016-11-30 04:47:39 -0800195 void SetBitRate(int bitrate_bps, int64_t probing_interval_ms);
kwiberg55b97fe2016-01-28 05:22:45 -0800196 int32_t SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX);
197 int32_t GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX);
198 int32_t SetRecPayloadType(const CodecInst& codec);
kwibergd32bf752017-01-19 07:03:59 -0800199 int32_t SetRecPayloadType(int payload_type, const SdpAudioFormat& format);
kwiberg55b97fe2016-01-28 05:22:45 -0800200 int32_t GetRecPayloadType(CodecInst& codec);
201 int32_t SetSendCNPayloadType(int type, PayloadFrequencies frequency);
202 int SetOpusMaxPlaybackRate(int frequency_hz);
203 int SetOpusDtx(bool enable_dtx);
ivoc85228d62016-07-27 04:53:47 -0700204 int GetOpusDtx(bool* enabled);
minyue7e304322016-10-12 05:00:55 -0700205 bool EnableAudioNetworkAdaptor(const std::string& config_string);
206 void DisableAudioNetworkAdaptor();
207 void SetReceiverFrameLengthRange(int min_frame_length_ms,
208 int max_frame_length_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000209
kwiberg55b97fe2016-01-28 05:22:45 -0800210 // VoENetwork
mflodman3d7db262016-04-29 00:57:13 -0700211 int32_t RegisterExternalTransport(Transport* transport);
kwiberg55b97fe2016-01-28 05:22:45 -0800212 int32_t DeRegisterExternalTransport();
mflodman3d7db262016-04-29 00:57:13 -0700213 int32_t ReceivedRTPPacket(const uint8_t* received_packet,
kwiberg55b97fe2016-01-28 05:22:45 -0800214 size_t length,
215 const PacketTime& packet_time);
nisse657bab22017-02-21 06:28:10 -0800216 // TODO(nisse, solenberg): Delete when VoENetwork is deleted.
mflodman3d7db262016-04-29 00:57:13 -0700217 int32_t ReceivedRTCPPacket(const uint8_t* data, size_t length);
nisse657bab22017-02-21 06:28:10 -0800218 void OnRtpPacket(const RtpPacketReceived& packet);
pwestin@webrtc.org684f0572013-03-13 23:20:57 +0000219
kwiberg55b97fe2016-01-28 05:22:45 -0800220 // VoEFile
221 int StartPlayingFileLocally(const char* fileName,
222 bool loop,
223 FileFormats format,
224 int startPosition,
225 float volumeScaling,
226 int stopPosition,
227 const CodecInst* codecInst);
228 int StartPlayingFileLocally(InStream* stream,
229 FileFormats format,
230 int startPosition,
231 float volumeScaling,
232 int stopPosition,
233 const CodecInst* codecInst);
234 int StopPlayingFileLocally();
235 int IsPlayingFileLocally() const;
236 int RegisterFilePlayingToMixer();
237 int StartPlayingFileAsMicrophone(const char* fileName,
238 bool loop,
239 FileFormats format,
240 int startPosition,
241 float volumeScaling,
242 int stopPosition,
243 const CodecInst* codecInst);
244 int StartPlayingFileAsMicrophone(InStream* stream,
245 FileFormats format,
246 int startPosition,
247 float volumeScaling,
248 int stopPosition,
249 const CodecInst* codecInst);
250 int StopPlayingFileAsMicrophone();
251 int IsPlayingFileAsMicrophone() const;
252 int StartRecordingPlayout(const char* fileName, const CodecInst* codecInst);
253 int StartRecordingPlayout(OutStream* stream, const CodecInst* codecInst);
254 int StopRecordingPlayout();
niklase@google.com470e71d2011-07-07 08:21:25 +0000255
kwiberg55b97fe2016-01-28 05:22:45 -0800256 void SetMixWithMicStatus(bool mix);
niklase@google.com470e71d2011-07-07 08:21:25 +0000257
solenberg8d73f8c2017-03-08 01:52:20 -0800258 // Muting, Volume and Level.
259 void SetInputMute(bool enable);
260 void SetChannelOutputVolumeScaling(float scaling);
261 int GetSpeechOutputLevel() const;
262 int GetSpeechOutputLevelFullRange() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000263
solenbergc6192a92017-03-13 02:36:19 -0700264 // Stats.
kwiberg55b97fe2016-01-28 05:22:45 -0800265 int GetNetworkStatistics(NetworkStatistics& stats);
266 void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000267
solenbergc6192a92017-03-13 02:36:19 -0700268 // Audio+Video Sync.
kwiberg55b97fe2016-01-28 05:22:45 -0800269 uint32_t GetDelayEstimate() const;
kwiberg55b97fe2016-01-28 05:22:45 -0800270 int SetMinimumPlayoutDelay(int delayMs);
271 int GetPlayoutTimestamp(unsigned int& timestamp);
kwiberg55b97fe2016-01-28 05:22:45 -0800272 int GetRtpRtcp(RtpRtcp** rtpRtcpModule, RtpReceiver** rtp_receiver) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000273
solenbergc6192a92017-03-13 02:36:19 -0700274 // DTMF.
solenberg8842c3e2016-03-11 03:06:41 -0800275 int SendTelephoneEventOutband(int event, int duration_ms);
solenbergffbbcac2016-11-17 05:25:37 -0800276 int SetSendTelephoneEventPayloadType(int payload_type, int payload_frequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000277
kwiberg55b97fe2016-01-28 05:22:45 -0800278 // VoERTP_RTCP
279 int SetLocalSSRC(unsigned int ssrc);
280 int GetLocalSSRC(unsigned int& ssrc);
281 int GetRemoteSSRC(unsigned int& ssrc);
282 int SetSendAudioLevelIndicationStatus(bool enable, unsigned char id);
283 int SetReceiveAudioLevelIndicationStatus(bool enable, unsigned char id);
kwiberg55b97fe2016-01-28 05:22:45 -0800284 void EnableSendTransportSequenceNumber(int id);
285 void EnableReceiveTransportSequenceNumber(int id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100286
stefan7de8d642017-02-07 07:14:08 -0800287 void RegisterSenderCongestionControlObjects(
nisseb8f9a322017-03-27 05:36:15 -0700288 RtpTransportControllerSendInterface* transport,
stefan7de8d642017-02-07 07:14:08 -0800289 RtcpBandwidthObserver* bandwidth_observer);
290 void RegisterReceiverCongestionControlObjects(PacketRouter* packet_router);
nissefdbfdc92017-03-31 05:44:52 -0700291 void ResetSenderCongestionControlObjects();
292 void ResetReceiverCongestionControlObjects();
kwiberg55b97fe2016-01-28 05:22:45 -0800293 void SetRTCPStatus(bool enable);
294 int GetRTCPStatus(bool& enabled);
295 int SetRTCP_CNAME(const char cName[256]);
296 int GetRemoteRTCP_CNAME(char cName[256]);
kwiberg55b97fe2016-01-28 05:22:45 -0800297 int SendApplicationDefinedRTCPPacket(unsigned char subType,
298 unsigned int name,
299 const char* data,
300 unsigned short dataLengthInBytes);
kwiberg55b97fe2016-01-28 05:22:45 -0800301 int GetRemoteRTCPReportBlocks(std::vector<ReportBlock>* report_blocks);
302 int GetRTPStatistics(CallStatistics& stats);
kwiberg55b97fe2016-01-28 05:22:45 -0800303 int SetCodecFECStatus(bool enable);
304 bool GetCodecFECStatus();
305 void SetNACKStatus(bool enable, int maxNumberOfPackets);
niklase@google.com470e71d2011-07-07 08:21:25 +0000306
kwiberg55b97fe2016-01-28 05:22:45 -0800307 // From AudioPacketizationCallback in the ACM
308 int32_t SendData(FrameType frameType,
309 uint8_t payloadType,
310 uint32_t timeStamp,
311 const uint8_t* payloadData,
312 size_t payloadSize,
313 const RTPFragmentationHeader* fragmentation) override;
xians@webrtc.org3cefbc92014-10-10 09:42:53 +0000314
kwiberg55b97fe2016-01-28 05:22:45 -0800315 // From RtpData in the RTP/RTCP module
316 int32_t OnReceivedPayloadData(const uint8_t* payloadData,
317 size_t payloadSize,
318 const WebRtcRTPHeader* rtpHeader) override;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000319
kwiberg55b97fe2016-01-28 05:22:45 -0800320 // From RtpFeedback in the RTP/RTCP module
321 int32_t OnInitializeDecoder(int8_t payloadType,
322 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
323 int frequency,
324 size_t channels,
325 uint32_t rate) override;
326 void OnIncomingSSRCChanged(uint32_t ssrc) override;
327 void OnIncomingCSRCChanged(uint32_t CSRC, bool added) override;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000328
kwiberg55b97fe2016-01-28 05:22:45 -0800329 // From Transport (called by the RTP/RTCP module)
330 bool SendRtp(const uint8_t* data,
331 size_t len,
332 const PacketOptions& packet_options) override;
333 bool SendRtcp(const uint8_t* data, size_t len) override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000334
kwiberg55b97fe2016-01-28 05:22:45 -0800335 // From MixerParticipant
henrik.lundin42dda502016-05-18 05:36:01 -0700336 MixerParticipant::AudioFrameInfo GetAudioFrameWithMuted(
337 int32_t id,
338 AudioFrame* audioFrame) override;
kwiberg55b97fe2016-01-28 05:22:45 -0800339 int32_t NeededFrequency(int32_t id) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000340
aleloiaed581a2016-10-20 06:32:39 -0700341 // From AudioMixer::Source.
aleloi6c278492016-10-20 14:24:39 -0700342 AudioMixer::Source::AudioFrameInfo GetAudioFrameWithInfo(
343 int sample_rate_hz,
344 AudioFrame* audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700345
kwiberg55b97fe2016-01-28 05:22:45 -0800346 // From FileCallback
347 void PlayNotification(int32_t id, uint32_t durationMs) override;
348 void RecordNotification(int32_t id, uint32_t durationMs) override;
349 void PlayFileEnded(int32_t id) override;
350 void RecordFileEnded(int32_t id) override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000351
kwiberg55b97fe2016-01-28 05:22:45 -0800352 uint32_t InstanceId() const { return _instanceId; }
353 int32_t ChannelId() const { return _channelId; }
354 bool Playing() const { return channel_state_.Get().playing; }
355 bool Sending() const { return channel_state_.Get().sending; }
kwiberg55b97fe2016-01-28 05:22:45 -0800356 bool ExternalTransport() const {
357 rtc::CritScope cs(&_callbackCritSect);
358 return _externalTransport;
359 }
kwiberg55b97fe2016-01-28 05:22:45 -0800360 RtpRtcp* RtpRtcpModulePtr() const { return _rtpRtcpModule.get(); }
361 int8_t OutputEnergyLevel() const { return _outputAudioLevel.Level(); }
henrikaec6fbd22017-03-31 05:43:36 -0700362
363 // ProcessAndEncodeAudio() creates an audio frame copy and posts a task
364 // on the shared encoder task queue, wich in turn calls (on the queue)
365 // ProcessAndEncodeAudioOnTaskQueue() where the actual processing of the
366 // audio takes place. The processing mainly consists of encoding and preparing
367 // the result for sending by adding it to a send queue.
368 // The main reason for using a task queue here is to release the native,
369 // OS-specific, audio capture thread as soon as possible to ensure that it
370 // can go back to sleep and be prepared to deliver an new captured audio
371 // packet.
372 void ProcessAndEncodeAudio(const AudioFrame& audio_input);
373
374 // This version of ProcessAndEncodeAudio() is used by PushCaptureData() in
375 // VoEBase and the audio in |audio_data| has not been subject to any APM
376 // processing. Some extra steps are therfore needed when building up the
377 // audio frame copy before using the same task as in the default call to
378 // ProcessAndEncodeAudio(const AudioFrame& audio_input).
379 void ProcessAndEncodeAudio(const int16_t* audio_data,
380 int sample_rate,
381 size_t number_of_frames,
382 size_t number_of_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000383
kwiberg55b97fe2016-01-28 05:22:45 -0800384 // Associate to a send channel.
385 // Used for obtaining RTT for a receive-only channel.
solenberg7602aab2016-11-14 11:30:07 -0800386 void set_associate_send_channel(const ChannelOwner& channel);
kwiberg55b97fe2016-01-28 05:22:45 -0800387 // Disassociate a send channel if it was associated.
388 void DisassociateSendChannel(int channel_id);
Minyue2013aec2015-05-13 14:14:42 +0200389
ivoc14d5dbe2016-07-04 07:06:55 -0700390 // Set a RtcEventLog logging object.
391 void SetRtcEventLog(RtcEventLog* event_log);
392
michaelt9332b7d2016-11-30 07:51:13 -0800393 void SetRtcpRttStats(RtcpRttStats* rtcp_rtt_stats);
nisse284542b2017-01-10 08:58:32 -0800394 void SetTransportOverhead(size_t transport_overhead_per_packet);
michaelt79e05882016-11-08 02:50:09 -0800395
michaeltbf65be52016-12-15 06:24:49 -0800396 // From OverheadObserver in the RTP/RTCP module
397 void OnOverheadChanged(size_t overhead_bytes_per_packet) override;
398
elad.alond12a8e12017-03-23 11:04:48 -0700399 // The existence of this function alongside OnUplinkPacketLossRate is
400 // a compromise. We want the encoder to be agnostic of the PLR source, but
401 // we also don't want it to receive conflicting information from TWCC and
402 // from RTCP-XR.
403 void OnTwccBasedUplinkPacketLossRate(float packet_loss_rate);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000404
elad.alondadb4dc2017-03-23 15:29:50 -0700405 void OnRecoverableUplinkPacketLossRate(float recoverable_packet_loss_rate);
406
hbos8d609f62017-04-10 07:39:05 -0700407 std::vector<RtpSource> GetSources() const {
408 return rtp_receiver_->GetSources();
409 }
410
kwiberg55b97fe2016-01-28 05:22:45 -0800411 private:
henrikaec6fbd22017-03-31 05:43:36 -0700412 class ProcessAndEncodeAudioTask;
elad.alond12a8e12017-03-23 11:04:48 -0700413
henrikaec6fbd22017-03-31 05:43:36 -0700414 void OnUplinkPacketLossRate(float packet_loss_rate);
solenberg8d73f8c2017-03-08 01:52:20 -0800415 bool InputMute() const;
nisse657bab22017-02-21 06:28:10 -0800416 bool OnRtpPacketWithHeader(const uint8_t* received_packet,
417 size_t length,
418 RTPHeader *header);
nisse30e89312017-05-29 08:16:37 -0700419 bool OnRecoveredPacket(const uint8_t* packet, size_t packet_length);
420
kwiberg55b97fe2016-01-28 05:22:45 -0800421 bool ReceivePacket(const uint8_t* packet,
422 size_t packet_length,
423 const RTPHeader& header,
424 bool in_order);
425 bool HandleRtxPacket(const uint8_t* packet,
426 size_t packet_length,
427 const RTPHeader& header);
428 bool IsPacketInOrder(const RTPHeader& header) const;
429 bool IsPacketRetransmitted(const RTPHeader& header, bool in_order) const;
430 int ResendPackets(const uint16_t* sequence_numbers, int length);
henrikaec6fbd22017-03-31 05:43:36 -0700431 int32_t MixOrReplaceAudioWithFile(AudioFrame* audio_frame);
kwiberg55b97fe2016-01-28 05:22:45 -0800432 int32_t MixAudioWithFile(AudioFrame& audioFrame, int mixingFrequency);
433 void UpdatePlayoutTimestamp(bool rtcp);
kwiberg55b97fe2016-01-28 05:22:45 -0800434 void RegisterReceiveCodecsToRTPModule();
niklase@google.com470e71d2011-07-07 08:21:25 +0000435
kwiberg55b97fe2016-01-28 05:22:45 -0800436 int SetSendRtpHeaderExtension(bool enable,
437 RTPExtensionType type,
438 unsigned char id);
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000439
hbos3fd31fe2017-02-28 05:43:16 -0800440 void UpdateOverheadForEncoder()
441 EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
nisse284542b2017-01-10 08:58:32 -0800442
ossue280cde2016-10-12 11:04:10 -0700443 int GetRtpTimestampRateHz() const;
kwiberg55b97fe2016-01-28 05:22:45 -0800444 int64_t GetRTT(bool allow_associate_channel) const;
wu@webrtc.org94454b72014-06-05 20:34:08 +0000445
henrikaec6fbd22017-03-31 05:43:36 -0700446 // Called on the encoder task queue when a new input audio frame is ready
447 // for encoding.
448 void ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input);
449
450 uint32_t _instanceId;
451 int32_t _channelId;
452
pbosd8de1152016-02-01 09:00:51 -0800453 rtc::CriticalSection _fileCritSect;
454 rtc::CriticalSection _callbackCritSect;
455 rtc::CriticalSection volume_settings_critsect_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000456
kwiberg55b97fe2016-01-28 05:22:45 -0800457 ChannelState channel_state_;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000458
ivoc14d5dbe2016-07-04 07:06:55 -0700459 std::unique_ptr<voe::RtcEventLogProxy> event_log_proxy_;
michaelt9332b7d2016-11-30 07:51:13 -0800460 std::unique_ptr<voe::RtcpRttStatsProxy> rtcp_rtt_stats_proxy_;
Ivo Creusenae856f22015-09-17 16:30:16 +0200461
kwibergb7f89d62016-02-17 10:04:18 -0800462 std::unique_ptr<RtpHeaderParser> rtp_header_parser_;
463 std::unique_ptr<RTPPayloadRegistry> rtp_payload_registry_;
464 std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_;
kwibergb7f89d62016-02-17 10:04:18 -0800465 std::unique_ptr<RtpReceiver> rtp_receiver_;
danilchap799a9d02016-09-22 03:36:27 -0700466 TelephoneEventHandler* telephone_event_handler_;
kwibergb7f89d62016-02-17 10:04:18 -0800467 std::unique_ptr<RtpRtcp> _rtpRtcpModule;
468 std::unique_ptr<AudioCodingModule> audio_coding_;
kwibergc8d071e2016-04-06 12:22:38 -0700469 acm2::CodecManager codec_manager_;
470 acm2::RentACodec rent_a_codec_;
kwibergb7f89d62016-02-17 10:04:18 -0800471 std::unique_ptr<AudioSinkInterface> audio_sink_;
kwiberg55b97fe2016-01-28 05:22:45 -0800472 AudioLevel _outputAudioLevel;
473 bool _externalTransport;
kwiberg55b97fe2016-01-28 05:22:45 -0800474 // Downsamples to the codec rate if necessary.
475 PushResampler<int16_t> input_resampler_;
kwiberg5a25d952016-08-17 07:31:12 -0700476 std::unique_ptr<FilePlayer> input_file_player_;
477 std::unique_ptr<FilePlayer> output_file_player_;
478 std::unique_ptr<FileRecorder> output_file_recorder_;
kwiberg55b97fe2016-01-28 05:22:45 -0800479 int _inputFilePlayerId;
480 int _outputFilePlayerId;
481 int _outputFileRecorderId;
482 bool _outputFileRecording;
henrikaec6fbd22017-03-31 05:43:36 -0700483 uint32_t _timeStamp ACCESS_ON(encoder_queue_);
turaj@webrtc.org167b6df2013-12-13 21:05:07 +0000484
kwiberg55b97fe2016-01-28 05:22:45 -0800485 RemoteNtpTimeEstimator ntp_estimator_ GUARDED_BY(ts_stats_lock_);
wu@webrtc.org82c4b852014-05-20 22:55:01 +0000486
kwiberg55b97fe2016-01-28 05:22:45 -0800487 // Timestamp of the audio pulled from NetEq.
henrik.lundin96bd5022016-04-06 04:13:56 -0700488 rtc::Optional<uint32_t> jitter_buffer_playout_timestamp_;
solenbergfe7dd6d2017-03-11 08:10:43 -0800489
490 rtc::CriticalSection video_sync_lock_;
kwiberg55b97fe2016-01-28 05:22:45 -0800491 uint32_t playout_timestamp_rtp_ GUARDED_BY(video_sync_lock_);
kwiberg55b97fe2016-01-28 05:22:45 -0800492 uint32_t playout_delay_ms_ GUARDED_BY(video_sync_lock_);
kwiberg55b97fe2016-01-28 05:22:45 -0800493 uint16_t send_sequence_number_;
494 uint8_t restored_packet_[kVoiceEngineMaxIpPacketSizeBytes];
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000495
pbosd8de1152016-02-01 09:00:51 -0800496 rtc::CriticalSection ts_stats_lock_;
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000497
kwibergb7f89d62016-02-17 10:04:18 -0800498 std::unique_ptr<rtc::TimestampWrapAroundHandler> rtp_ts_wraparound_handler_;
kwiberg55b97fe2016-01-28 05:22:45 -0800499 // The rtp timestamp of the first played out audio frame.
500 int64_t capture_start_rtp_time_stamp_;
501 // The capture ntp time (in local timebase) of the first played out audio
502 // frame.
503 int64_t capture_start_ntp_time_ms_ GUARDED_BY(ts_stats_lock_);
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000504
kwiberg55b97fe2016-01-28 05:22:45 -0800505 // uses
506 Statistics* _engineStatisticsPtr;
507 OutputMixer* _outputMixerPtr;
kwiberg55b97fe2016-01-28 05:22:45 -0800508 ProcessThread* _moduleProcessThreadPtr;
509 AudioDeviceModule* _audioDeviceModulePtr;
510 VoiceEngineObserver* _voiceEngineObserverPtr; // owned by base
511 rtc::CriticalSection* _callbackCritSectPtr; // owned by base
512 Transport* _transportPtr; // WebRtc socket or external transport
henrikaec6fbd22017-03-31 05:43:36 -0700513 RmsLevel rms_level_ ACCESS_ON(encoder_queue_);
solenberg1c2af8e2016-03-24 10:36:00 -0700514 bool input_mute_ GUARDED_BY(volume_settings_critsect_);
henrikaec6fbd22017-03-31 05:43:36 -0700515 bool previous_frame_muted_ ACCESS_ON(encoder_queue_);
solenberg1c2af8e2016-03-24 10:36:00 -0700516 float _outputGain GUARDED_BY(volume_settings_critsect_);
solenberg8d73f8c2017-03-08 01:52:20 -0800517 // VoEBase
518 bool _mixFileWithMicrophone;
kwiberg55b97fe2016-01-28 05:22:45 -0800519 // VoeRTP_RTCP
henrikaec6fbd22017-03-31 05:43:36 -0700520 // TODO(henrika): can today be accessed on the main thread and on the
521 // task queue; hence potential race.
kwiberg55b97fe2016-01-28 05:22:45 -0800522 bool _includeAudioLevelIndication;
hbos3fd31fe2017-02-28 05:43:16 -0800523 size_t transport_overhead_per_packet_ GUARDED_BY(overhead_per_packet_lock_);
524 size_t rtp_overhead_per_packet_ GUARDED_BY(overhead_per_packet_lock_);
525 rtc::CriticalSection overhead_per_packet_lock_;
kwiberg55b97fe2016-01-28 05:22:45 -0800526 // VoENetwork
527 AudioFrame::SpeechType _outputSpeechType;
solenbergfe7dd6d2017-03-11 08:10:43 -0800528 // DTX.
kwiberg55b97fe2016-01-28 05:22:45 -0800529 bool restored_packet_in_use_;
530 // RtcpBandwidthObserver
kwibergb7f89d62016-02-17 10:04:18 -0800531 std::unique_ptr<VoERtcpObserver> rtcp_observer_;
kwiberg55b97fe2016-01-28 05:22:45 -0800532 // An associated send channel.
pbosd8de1152016-02-01 09:00:51 -0800533 rtc::CriticalSection assoc_send_channel_lock_;
kwiberg55b97fe2016-01-28 05:22:45 -0800534 ChannelOwner associate_send_channel_ GUARDED_BY(assoc_send_channel_lock_);
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100535
kwiberg55b97fe2016-01-28 05:22:45 -0800536 bool pacing_enabled_;
537 PacketRouter* packet_router_ = nullptr;
kwibergb7f89d62016-02-17 10:04:18 -0800538 std::unique_ptr<TransportFeedbackProxy> feedback_observer_proxy_;
539 std::unique_ptr<TransportSequenceNumberProxy> seq_num_allocator_proxy_;
540 std::unique_ptr<RtpPacketSenderProxy> rtp_packet_sender_proxy_;
Erik Språng737336d2016-07-29 12:59:36 +0200541 std::unique_ptr<RateLimiter> retransmission_rate_limiter_;
ossu29b1a8d2016-06-13 07:34:51 -0700542
543 // TODO(ossu): Remove once GetAudioDecoderFactory() is no longer needed.
544 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
tommi0a2391f2017-03-21 02:31:51 -0700545
546 rtc::ThreadChecker construction_thread_;
elad.alond12a8e12017-03-23 11:04:48 -0700547
548 const bool use_twcc_plr_for_ana_;
henrikaec6fbd22017-03-31 05:43:36 -0700549
henrika4515fa02017-05-03 08:30:15 -0700550 rtc::CriticalSection encoder_queue_lock_;
551
552 bool encoder_queue_is_active_ GUARDED_BY(encoder_queue_lock_) = false;
553
henrikaec6fbd22017-03-31 05:43:36 -0700554 rtc::TaskQueue* encoder_queue_ = nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000555};
556
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000557} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000558} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000559
andrew@webrtc.org382c0c22014-05-05 18:22:21 +0000560#endif // WEBRTC_VOICE_ENGINE_CHANNEL_H_