blob: a04f92b6bfc6428ffea511842ae6918092d7c68d [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
niklas.enbom@webrtc.org3dc88652012-03-30 09:53:54 +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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef VOICE_ENGINE_TRANSMIT_MIXER_H_
12#define VOICE_ENGINE_TRANSMIT_MIXER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
kwiberg5a25d952016-08-17 07:31:12 -070014#include <memory>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "common_audio/resampler/include/push_resampler.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020017#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_processing/typing_detection.h"
19#include "modules/include/module_common_types.h"
20#include "rtc_base/criticalsection.h"
21#include "voice_engine/audio_level.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "voice_engine/include/voe_base.h"
23#include "voice_engine/monitor_module.h"
24#include "voice_engine/voice_engine_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000025
henrik.lundinf00082d2016-12-05 02:22:12 -080026#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
27#define WEBRTC_VOICE_ENGINE_TYPING_DETECTION 1
28#else
29#define WEBRTC_VOICE_ENGINE_TYPING_DETECTION 0
30#endif
31
niklase@google.com470e71d2011-07-07 08:21:25 +000032namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000033class AudioProcessing;
34class ProcessThread;
niklase@google.com470e71d2011-07-07 08:21:25 +000035
36namespace voe {
37
38class ChannelManager;
39class MixedAudio;
40class Statistics;
41
solenbergb63310a2017-09-18 03:04:12 -070042class TransmitMixer {
niklase@google.com470e71d2011-07-07 08:21:25 +000043public:
pbos@webrtc.org92135212013-05-14 08:31:39 +000044 static int32_t Create(TransmitMixer*& mixer, uint32_t instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +000045
46 static void Destroy(TransmitMixer*& mixer);
47
pbos@webrtc.org6141e132013-04-09 10:09:10 +000048 int32_t SetEngineInformation(ProcessThread& processThread,
49 Statistics& engineStatistics,
50 ChannelManager& channelManager);
niklase@google.com470e71d2011-07-07 08:21:25 +000051
pbos@webrtc.org6141e132013-04-09 10:09:10 +000052 int32_t SetAudioProcessingModule(
niklase@google.com470e71d2011-07-07 08:21:25 +000053 AudioProcessing* audioProcessingModule);
54
pbos@webrtc.org6141e132013-04-09 10:09:10 +000055 int32_t PrepareDemux(const void* audioSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -070056 size_t nSamples,
Peter Kasting69558702016-01-12 16:26:35 -080057 size_t nChannels,
pbos@webrtc.org92135212013-05-14 08:31:39 +000058 uint32_t samplesPerSec,
59 uint16_t totalDelayMS,
60 int32_t clockDrift,
61 uint16_t currentMicLevel,
62 bool keyPressed);
niklase@google.com470e71d2011-07-07 08:21:25 +000063
henrikaec6fbd22017-03-31 05:43:36 -070064 void ProcessAndEncodeAudio();
niklase@google.com470e71d2011-07-07 08:21:25 +000065
andrew@webrtc.org023cc5a2014-01-11 01:25:53 +000066 // Must be called on the same thread as PrepareDemux().
pbos@webrtc.org6141e132013-04-09 10:09:10 +000067 uint32_t CaptureLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000068
pbos@webrtc.org6141e132013-04-09 10:09:10 +000069 int32_t StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +000070
solenberg8d73f8c2017-03-08 01:52:20 -080071 // TODO(solenberg): Remove, once AudioMonitor is gone.
pbos@webrtc.org6141e132013-04-09 10:09:10 +000072 int8_t AudioLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000073
solenberg796b8f92017-03-01 17:02:23 -080074 // 'virtual' to allow mocking.
75 virtual int16_t AudioLevelFullRange() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000076
zsteine76bd3a2017-07-14 12:17:49 -070077 // See description of "totalAudioEnergy" in the WebRTC stats spec:
78 // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
79 // 'virtual' to allow mocking.
80 virtual double GetTotalInputEnergy() const;
81
82 // 'virtual' to allow mocking.
83 virtual double GetTotalInputDuration() const;
84
pbos@webrtc.org6141e132013-04-09 10:09:10 +000085 int32_t RegisterVoiceEngineObserver(VoiceEngineObserver& observer);
niklase@google.com470e71d2011-07-07 08:21:25 +000086
87 virtual ~TransmitMixer();
88
tommiba08a142017-02-28 08:25:11 -080089#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
tommib1175bb2017-02-28 01:16:48 -080090 // Periodic callback from the MonitorModule.
niklase@google.com470e71d2011-07-07 08:21:25 +000091 void OnPeriodicProcess();
tommiba08a142017-02-28 08:25:11 -080092#endif
niklase@google.com470e71d2011-07-07 08:21:25 +000093
solenberg76377c52017-02-21 00:54:31 -080094 // Virtual to allow mocking.
95 virtual void EnableStereoChannelSwapping(bool enable);
andrew@webrtc.org02d71742012-04-24 19:47:00 +000096 bool IsStereoChannelSwappingEnabled();
97
solenberg76377c52017-02-21 00:54:31 -080098protected:
tommiba08a142017-02-28 08:25:11 -080099#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
tommib1175bb2017-02-28 01:16:48 -0800100 TransmitMixer() : _monitorModule(this) {}
tommiba08a142017-02-28 08:25:11 -0800101#else
102 TransmitMixer() = default;
103#endif
solenberg76377c52017-02-21 00:54:31 -0800104
niklase@google.com470e71d2011-07-07 08:21:25 +0000105private:
pbos@webrtc.org92135212013-05-14 08:31:39 +0000106 TransmitMixer(uint32_t instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
andrew@webrtc.org24120852013-03-02 00:14:46 +0000108 // Gets the maximum sample rate and number of channels over all currently
109 // sending codecs.
Peter Kasting69558702016-01-12 16:26:35 -0800110 void GetSendCodecInfo(int* max_sample_rate, size_t* max_channels);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000111
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000112 void GenerateAudioFrame(const int16_t audioSamples[],
Peter Kastingdce40cf2015-08-24 14:52:23 -0700113 size_t nSamples,
Peter Kasting69558702016-01-12 16:26:35 -0800114 size_t nChannels,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000115 int samplesPerSec);
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000117 void ProcessAudio(int delay_ms, int clock_drift, int current_mic_level,
118 bool key_pressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000119
henrik.lundinf00082d2016-12-05 02:22:12 -0800120#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000121 void TypingDetection(bool keyPressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000122#endif
123
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000124 // uses
solenberg76377c52017-02-21 00:54:31 -0800125 Statistics* _engineStatisticsPtr = nullptr;
126 ChannelManager* _channelManagerPtr = nullptr;
127 AudioProcessing* audioproc_ = nullptr;
128 VoiceEngineObserver* _voiceEngineObserverPtr = nullptr;
129 ProcessThread* _processThreadPtr = nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000131 // owns
niklase@google.com470e71d2011-07-07 08:21:25 +0000132 AudioFrame _audioFrame;
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000133 PushResampler<int16_t> resampler_; // ADM sample rate -> mixing rate
niklase@google.com470e71d2011-07-07 08:21:25 +0000134 voe::AudioLevel _audioLevel;
135 // protect file instances and their variables in MixedParticipants()
tommi31fc21f2016-01-21 10:37:37 -0800136 rtc::CriticalSection _critSect;
137 rtc::CriticalSection _callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
henrik.lundinf00082d2016-12-05 02:22:12 -0800139#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
tommiba08a142017-02-28 08:25:11 -0800140 MonitorModule<TransmitMixer> _monitorModule;
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000141 webrtc::TypingDetection _typingDetection;
solenberg76377c52017-02-21 00:54:31 -0800142 bool _typingNoiseWarningPending = false;
143 bool _typingNoiseDetected = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000144#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
solenberg76377c52017-02-21 00:54:31 -0800146 int _instanceId = 0;
solenberg76377c52017-02-21 00:54:31 -0800147 uint32_t _captureLevel = 0;
solenberg76377c52017-02-21 00:54:31 -0800148 bool stereo_codec_ = false;
149 bool swap_stereo_channels_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000150};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000151} // namespace voe
niklase@google.com470e71d2011-07-07 08:21:25 +0000152} // namespace webrtc
braveyao@webrtc.orga7cfa672013-12-24 03:39:10 +0000153
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200154#endif // VOICE_ENGINE_TRANSMIT_MIXER_H_