niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
niklas.enbom@webrtc.org | 3dc8865 | 2012-03-30 09:53:54 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 WEBRTC_VOICE_ENGINE_TRANSMIT_MIXER_H |
| 12 | #define WEBRTC_VOICE_ENGINE_TRANSMIT_MIXER_H |
| 13 | |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 16 | #include "webrtc/base/criticalsection.h" |
andrew@webrtc.org | 28e82bf | 2013-05-02 00:30:36 +0000 | [diff] [blame] | 17 | #include "webrtc/common_audio/resampler/include/push_resampler.h" |
| 18 | #include "webrtc/common_types.h" |
henrikg@webrtc.org | c693704 | 2014-01-30 09:50:46 +0000 | [diff] [blame] | 19 | #include "webrtc/modules/audio_processing/typing_detection.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 20 | #include "webrtc/modules/include/module_common_types.h" |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 21 | #include "webrtc/voice_engine/audio_level.h" |
kwiberg | 9774447 | 2017-01-10 01:12:51 -0800 | [diff] [blame] | 22 | #include "webrtc/voice_engine/file_player.h" |
| 23 | #include "webrtc/voice_engine/file_recorder.h" |
andrew@webrtc.org | 28e82bf | 2013-05-02 00:30:36 +0000 | [diff] [blame] | 24 | #include "webrtc/voice_engine/include/voe_base.h" |
andrew@webrtc.org | 28e82bf | 2013-05-02 00:30:36 +0000 | [diff] [blame] | 25 | #include "webrtc/voice_engine/monitor_module.h" |
| 26 | #include "webrtc/voice_engine/voice_engine_defines.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
henrik.lundin | f00082d | 2016-12-05 02:22:12 -0800 | [diff] [blame] | 28 | #if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS) |
| 29 | #define WEBRTC_VOICE_ENGINE_TYPING_DETECTION 1 |
| 30 | #else |
| 31 | #define WEBRTC_VOICE_ENGINE_TYPING_DETECTION 0 |
| 32 | #endif |
| 33 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | namespace webrtc { |
| 35 | |
| 36 | class AudioProcessing; |
| 37 | class ProcessThread; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | |
| 39 | namespace voe { |
| 40 | |
| 41 | class ChannelManager; |
| 42 | class MixedAudio; |
| 43 | class Statistics; |
| 44 | |
tommi | b1175bb | 2017-02-28 01:16:48 -0800 | [diff] [blame] | 45 | class TransmitMixer : public FileCallback { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | public: |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 47 | static int32_t Create(TransmitMixer*& mixer, uint32_t instanceId); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | |
| 49 | static void Destroy(TransmitMixer*& mixer); |
| 50 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 51 | int32_t SetEngineInformation(ProcessThread& processThread, |
| 52 | Statistics& engineStatistics, |
| 53 | ChannelManager& channelManager); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 55 | int32_t SetAudioProcessingModule( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | AudioProcessing* audioProcessingModule); |
| 57 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 58 | int32_t PrepareDemux(const void* audioSamples, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 59 | size_t nSamples, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 60 | size_t nChannels, |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 61 | uint32_t samplesPerSec, |
| 62 | uint16_t totalDelayMS, |
| 63 | int32_t clockDrift, |
| 64 | uint16_t currentMicLevel, |
| 65 | bool keyPressed); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | |
| 67 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 68 | int32_t DemuxAndMix(); |
xians@webrtc.org | 2f84afa | 2013-07-31 16:23:37 +0000 | [diff] [blame] | 69 | // Used by the Chrome to pass the recording data to the specific VoE |
| 70 | // channels for demux. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 71 | void DemuxAndMix(const int voe_channels[], size_t number_of_voe_channels); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 72 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 73 | int32_t EncodeAndSend(); |
xians@webrtc.org | 2f84afa | 2013-07-31 16:23:37 +0000 | [diff] [blame] | 74 | // Used by the Chrome to pass the recording data to the specific VoE |
| 75 | // channels for encoding and sending to the network. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 76 | void EncodeAndSend(const int voe_channels[], size_t number_of_voe_channels); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | |
andrew@webrtc.org | 023cc5a | 2014-01-11 01:25:53 +0000 | [diff] [blame] | 78 | // Must be called on the same thread as PrepareDemux(). |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 79 | uint32_t CaptureLevel() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 81 | int32_t StopSend(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | |
solenberg | 8d73f8c | 2017-03-08 01:52:20 -0800 | [diff] [blame] | 83 | // TODO(solenberg): Remove, once AudioMonitor is gone. |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 84 | int8_t AudioLevel() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | |
solenberg | 796b8f9 | 2017-03-01 17:02:23 -0800 | [diff] [blame] | 86 | // 'virtual' to allow mocking. |
| 87 | virtual int16_t AudioLevelFullRange() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | |
| 89 | bool IsRecordingCall(); |
| 90 | |
| 91 | bool IsRecordingMic(); |
| 92 | |
| 93 | int StartPlayingFileAsMicrophone(const char* fileName, |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 94 | bool loop, |
| 95 | FileFormats format, |
| 96 | int startPosition, |
| 97 | float volumeScaling, |
| 98 | int stopPosition, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | const CodecInst* codecInst); |
| 100 | |
| 101 | int StartPlayingFileAsMicrophone(InStream* stream, |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 102 | FileFormats format, |
| 103 | int startPosition, |
| 104 | float volumeScaling, |
| 105 | int stopPosition, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | const CodecInst* codecInst); |
| 107 | |
| 108 | int StopPlayingFileAsMicrophone(); |
| 109 | |
| 110 | int IsPlayingFileAsMicrophone() const; |
| 111 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | int StartRecordingMicrophone(const char* fileName, |
| 113 | const CodecInst* codecInst); |
| 114 | |
| 115 | int StartRecordingMicrophone(OutStream* stream, |
| 116 | const CodecInst* codecInst); |
| 117 | |
| 118 | int StopRecordingMicrophone(); |
| 119 | |
| 120 | int StartRecordingCall(const char* fileName, const CodecInst* codecInst); |
| 121 | |
| 122 | int StartRecordingCall(OutStream* stream, const CodecInst* codecInst); |
| 123 | |
| 124 | int StopRecordingCall(); |
| 125 | |
| 126 | void SetMixWithMicStatus(bool mix); |
| 127 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 128 | int32_t RegisterVoiceEngineObserver(VoiceEngineObserver& observer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | |
| 130 | virtual ~TransmitMixer(); |
| 131 | |
tommi | ba08a14 | 2017-02-28 08:25:11 -0800 | [diff] [blame] | 132 | #if WEBRTC_VOICE_ENGINE_TYPING_DETECTION |
tommi | b1175bb | 2017-02-28 01:16:48 -0800 | [diff] [blame] | 133 | // Periodic callback from the MonitorModule. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | void OnPeriodicProcess(); |
tommi | ba08a14 | 2017-02-28 08:25:11 -0800 | [diff] [blame] | 135 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 136 | |
andrew@webrtc.org | 02d7174 | 2012-04-24 19:47:00 +0000 | [diff] [blame] | 137 | // FileCallback |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 138 | void PlayNotification(const int32_t id, |
| 139 | const uint32_t durationMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 140 | |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 141 | void RecordNotification(const int32_t id, |
| 142 | const uint32_t durationMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 144 | void PlayFileEnded(const int32_t id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 145 | |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 146 | void RecordFileEnded(const int32_t id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 148 | // Virtual to allow mocking. |
| 149 | virtual void EnableStereoChannelSwapping(bool enable); |
andrew@webrtc.org | 02d7174 | 2012-04-24 19:47:00 +0000 | [diff] [blame] | 150 | bool IsStereoChannelSwappingEnabled(); |
| 151 | |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 152 | protected: |
tommi | ba08a14 | 2017-02-28 08:25:11 -0800 | [diff] [blame] | 153 | #if WEBRTC_VOICE_ENGINE_TYPING_DETECTION |
tommi | b1175bb | 2017-02-28 01:16:48 -0800 | [diff] [blame] | 154 | TransmitMixer() : _monitorModule(this) {} |
tommi | ba08a14 | 2017-02-28 08:25:11 -0800 | [diff] [blame] | 155 | #else |
| 156 | TransmitMixer() = default; |
| 157 | #endif |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 158 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | private: |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 160 | TransmitMixer(uint32_t instanceId); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 161 | |
andrew@webrtc.org | 2412085 | 2013-03-02 00:14:46 +0000 | [diff] [blame] | 162 | // Gets the maximum sample rate and number of channels over all currently |
| 163 | // sending codecs. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 164 | void GetSendCodecInfo(int* max_sample_rate, size_t* max_channels); |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 165 | |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 166 | void GenerateAudioFrame(const int16_t audioSamples[], |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 167 | size_t nSamples, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 168 | size_t nChannels, |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 169 | int samplesPerSec); |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 170 | int32_t RecordAudioToFile(uint32_t mixingFrequency); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 172 | int32_t MixOrReplaceAudioWithFile( |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 173 | int mixingFrequency); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 174 | |
andrew@webrtc.org | 75dd288 | 2014-02-11 20:52:30 +0000 | [diff] [blame] | 175 | void ProcessAudio(int delay_ms, int clock_drift, int current_mic_level, |
| 176 | bool key_pressed); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 177 | |
henrik.lundin | f00082d | 2016-12-05 02:22:12 -0800 | [diff] [blame] | 178 | #if WEBRTC_VOICE_ENGINE_TYPING_DETECTION |
henrikg@webrtc.org | c693704 | 2014-01-30 09:50:46 +0000 | [diff] [blame] | 179 | void TypingDetection(bool keyPressed); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 180 | #endif |
| 181 | |
andrew@webrtc.org | 02d7174 | 2012-04-24 19:47:00 +0000 | [diff] [blame] | 182 | // uses |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 183 | Statistics* _engineStatisticsPtr = nullptr; |
| 184 | ChannelManager* _channelManagerPtr = nullptr; |
| 185 | AudioProcessing* audioproc_ = nullptr; |
| 186 | VoiceEngineObserver* _voiceEngineObserverPtr = nullptr; |
| 187 | ProcessThread* _processThreadPtr = nullptr; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | |
andrew@webrtc.org | 02d7174 | 2012-04-24 19:47:00 +0000 | [diff] [blame] | 189 | // owns |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 190 | AudioFrame _audioFrame; |
andrew@webrtc.org | f5a33f1 | 2014-04-19 00:32:07 +0000 | [diff] [blame] | 191 | PushResampler<int16_t> resampler_; // ADM sample rate -> mixing rate |
kwiberg | 5a25d95 | 2016-08-17 07:31:12 -0700 | [diff] [blame] | 192 | std::unique_ptr<FilePlayer> file_player_; |
| 193 | std::unique_ptr<FileRecorder> file_recorder_; |
| 194 | std::unique_ptr<FileRecorder> file_call_recorder_; |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 195 | int _filePlayerId = 0; |
| 196 | int _fileRecorderId = 0; |
| 197 | int _fileCallRecorderId = 0; |
| 198 | bool _filePlaying = false; |
| 199 | bool _fileRecording = false; |
| 200 | bool _fileCallRecording = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 201 | voe::AudioLevel _audioLevel; |
| 202 | // protect file instances and their variables in MixedParticipants() |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 203 | rtc::CriticalSection _critSect; |
| 204 | rtc::CriticalSection _callbackCritSect; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | |
henrik.lundin | f00082d | 2016-12-05 02:22:12 -0800 | [diff] [blame] | 206 | #if WEBRTC_VOICE_ENGINE_TYPING_DETECTION |
tommi | ba08a14 | 2017-02-28 08:25:11 -0800 | [diff] [blame] | 207 | MonitorModule<TransmitMixer> _monitorModule; |
henrikg@webrtc.org | c693704 | 2014-01-30 09:50:46 +0000 | [diff] [blame] | 208 | webrtc::TypingDetection _typingDetection; |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 209 | bool _typingNoiseWarningPending = false; |
| 210 | bool _typingNoiseDetected = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 211 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 212 | |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 213 | int _instanceId = 0; |
| 214 | bool _mixFileWithMicrophone = false; |
| 215 | uint32_t _captureLevel = 0; |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 216 | bool stereo_codec_ = false; |
| 217 | bool swap_stereo_channels_ = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 218 | }; |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 219 | } // namespace voe |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 220 | } // namespace webrtc |
braveyao@webrtc.org | a7cfa67 | 2013-12-24 03:39:10 +0000 | [diff] [blame] | 221 | |
braveyao@webrtc.org | 0062a6d | 2013-12-24 03:58:51 +0000 | [diff] [blame] | 222 | #endif // WEBRTC_VOICE_ENGINE_TRANSMIT_MIXER_H |