1 /*
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 VOICE_ENGINE_TRANSMIT_MIXER_H_
12 #define VOICE_ENGINE_TRANSMIT_MIXER_H_
13 
14 #include <memory>
15 
16 #include "common_audio/resampler/include/push_resampler.h"
17 #include "common_types.h"  // NOLINT(build/include)
18 #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"
22 #include "voice_engine/include/voe_base.h"
23 
24 #if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
25 #define WEBRTC_VOICE_ENGINE_TYPING_DETECTION 1
26 #else
27 #define WEBRTC_VOICE_ENGINE_TYPING_DETECTION 0
28 #endif
29 
30 namespace webrtc {
31 class AudioProcessing;
32 class ProcessThread;
33 
34 namespace voe {
35 
36 class ChannelManager;
37 class MixedAudio;
38 
39 class TransmitMixer {
40 public:
41     static int32_t Create(TransmitMixer*& mixer);
42 
43     static void Destroy(TransmitMixer*& mixer);
44 
45     void SetEngineInformation(ChannelManager* channelManager);
46 
47     int32_t SetAudioProcessingModule(AudioProcessing* audioProcessingModule);
48 
49     int32_t PrepareDemux(const void* audioSamples,
50                          size_t nSamples,
51                          size_t nChannels,
52                          uint32_t samplesPerSec,
53                          uint16_t totalDelayMS,
54                          int32_t  clockDrift,
55                          uint16_t currentMicLevel,
56                          bool keyPressed);
57 
58     void ProcessAndEncodeAudio();
59 
60     // Must be called on the same thread as PrepareDemux().
61     uint32_t CaptureLevel() const;
62 
63     int32_t StopSend();
64 
65     // TODO(solenberg): Remove, once AudioMonitor is gone.
66     int8_t AudioLevel() const;
67 
68     // 'virtual' to allow mocking.
69     virtual int16_t AudioLevelFullRange() const;
70 
71     // See description of "totalAudioEnergy" in the WebRTC stats spec:
72     // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
73     // 'virtual' to allow mocking.
74     virtual double GetTotalInputEnergy() const;
75 
76     // 'virtual' to allow mocking.
77     virtual double GetTotalInputDuration() const;
78 
79     virtual ~TransmitMixer();
80 
81   // Virtual to allow mocking.
82   virtual void EnableStereoChannelSwapping(bool enable);
83   bool IsStereoChannelSwappingEnabled();
84 
85   // Virtual to allow mocking.
86   virtual bool typing_noise_detected() const;
87 
88 protected:
89     TransmitMixer() = default;
90 
91 private:
92     // Gets the maximum sample rate and number of channels over all currently
93     // sending codecs.
94     void GetSendCodecInfo(int* max_sample_rate, size_t* max_channels);
95 
96     void GenerateAudioFrame(const int16_t audioSamples[],
97                             size_t nSamples,
98                             size_t nChannels,
99                             int samplesPerSec);
100 
101     void ProcessAudio(int delay_ms, int clock_drift, int current_mic_level,
102                       bool key_pressed);
103 
104 #if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
105     void TypingDetection(bool key_pressed);
106 #endif
107 
108     // uses
109     ChannelManager* _channelManagerPtr = nullptr;
110     AudioProcessing* audioproc_ = nullptr;
111 
112     // owns
113     AudioFrame _audioFrame;
114     PushResampler<int16_t> resampler_;  // ADM sample rate -> mixing rate
115     voe::AudioLevel _audioLevel;
116 
117 #if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
118     webrtc::TypingDetection typing_detection_;
119 #endif
120 
121     rtc::CriticalSection lock_;
122     bool typing_noise_detected_ RTC_GUARDED_BY(lock_) = false;
123 
124     uint32_t _captureLevel = 0;
125     bool stereo_codec_ = false;
126     bool swap_stereo_channels_ = false;
127 };
128 }  // namespace voe
129 }  // namespace webrtc
130 
131 #endif  // VOICE_ENGINE_TRANSMIT_MIXER_H_
132