blob: a045cf8322855abb0b7217e1b23c5e54eb80a00b [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
11#ifndef WEBRTC_VOICE_ENGINE_TRANSMIT_MIXER_H
12#define WEBRTC_VOICE_ENGINE_TRANSMIT_MIXER_H
13
kwiberg5a25d952016-08-17 07:31:12 -070014#include <memory>
15
tommi31fc21f2016-01-21 10:37:37 -080016#include "webrtc/base/criticalsection.h"
andrew@webrtc.org28e82bf2013-05-02 00:30:36 +000017#include "webrtc/common_audio/resampler/include/push_resampler.h"
18#include "webrtc/common_types.h"
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +000019#include "webrtc/modules/audio_processing/typing_detection.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010020#include "webrtc/modules/include/module_common_types.h"
henrik.lundin92a7a182017-03-07 01:58:55 -080021#include "webrtc/voice_engine/audio_level.h"
kwiberg97744472017-01-10 01:12:51 -080022#include "webrtc/voice_engine/file_player.h"
23#include "webrtc/voice_engine/file_recorder.h"
andrew@webrtc.org28e82bf2013-05-02 00:30:36 +000024#include "webrtc/voice_engine/include/voe_base.h"
andrew@webrtc.org28e82bf2013-05-02 00:30:36 +000025#include "webrtc/voice_engine/monitor_module.h"
26#include "webrtc/voice_engine/voice_engine_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000027
henrik.lundinf00082d2016-12-05 02:22:12 -080028#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.com470e71d2011-07-07 08:21:25 +000034namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000035class AudioProcessing;
36class ProcessThread;
niklase@google.com470e71d2011-07-07 08:21:25 +000037
38namespace voe {
39
40class ChannelManager;
41class MixedAudio;
42class Statistics;
43
tommib1175bb2017-02-28 01:16:48 -080044class TransmitMixer : public FileCallback {
niklase@google.com470e71d2011-07-07 08:21:25 +000045public:
pbos@webrtc.org92135212013-05-14 08:31:39 +000046 static int32_t Create(TransmitMixer*& mixer, uint32_t instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +000047
48 static void Destroy(TransmitMixer*& mixer);
49
pbos@webrtc.org6141e132013-04-09 10:09:10 +000050 int32_t SetEngineInformation(ProcessThread& processThread,
51 Statistics& engineStatistics,
52 ChannelManager& channelManager);
niklase@google.com470e71d2011-07-07 08:21:25 +000053
pbos@webrtc.org6141e132013-04-09 10:09:10 +000054 int32_t SetAudioProcessingModule(
niklase@google.com470e71d2011-07-07 08:21:25 +000055 AudioProcessing* audioProcessingModule);
56
pbos@webrtc.org6141e132013-04-09 10:09:10 +000057 int32_t PrepareDemux(const void* audioSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -070058 size_t nSamples,
Peter Kasting69558702016-01-12 16:26:35 -080059 size_t nChannels,
pbos@webrtc.org92135212013-05-14 08:31:39 +000060 uint32_t samplesPerSec,
61 uint16_t totalDelayMS,
62 int32_t clockDrift,
63 uint16_t currentMicLevel,
64 bool keyPressed);
niklase@google.com470e71d2011-07-07 08:21:25 +000065
henrikaec6fbd22017-03-31 05:43:36 -070066 void ProcessAndEncodeAudio();
niklase@google.com470e71d2011-07-07 08:21:25 +000067
andrew@webrtc.org023cc5a2014-01-11 01:25:53 +000068 // Must be called on the same thread as PrepareDemux().
pbos@webrtc.org6141e132013-04-09 10:09:10 +000069 uint32_t CaptureLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000070
pbos@webrtc.org6141e132013-04-09 10:09:10 +000071 int32_t StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +000072
solenberg8d73f8c2017-03-08 01:52:20 -080073 // TODO(solenberg): Remove, once AudioMonitor is gone.
pbos@webrtc.org6141e132013-04-09 10:09:10 +000074 int8_t AudioLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000075
solenberg796b8f92017-03-01 17:02:23 -080076 // 'virtual' to allow mocking.
77 virtual int16_t AudioLevelFullRange() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000078
79 bool IsRecordingCall();
80
81 bool IsRecordingMic();
82
83 int StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +000084 bool loop,
85 FileFormats format,
86 int startPosition,
87 float volumeScaling,
88 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +000089 const CodecInst* codecInst);
90
91 int StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +000092 FileFormats format,
93 int startPosition,
94 float volumeScaling,
95 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +000096 const CodecInst* codecInst);
97
98 int StopPlayingFileAsMicrophone();
99
100 int IsPlayingFileAsMicrophone() const;
101
niklase@google.com470e71d2011-07-07 08:21:25 +0000102 int StartRecordingMicrophone(const char* fileName,
103 const CodecInst* codecInst);
104
105 int StartRecordingMicrophone(OutStream* stream,
106 const CodecInst* codecInst);
107
108 int StopRecordingMicrophone();
109
110 int StartRecordingCall(const char* fileName, const CodecInst* codecInst);
111
112 int StartRecordingCall(OutStream* stream, const CodecInst* codecInst);
113
114 int StopRecordingCall();
115
116 void SetMixWithMicStatus(bool mix);
117
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000118 int32_t RegisterVoiceEngineObserver(VoiceEngineObserver& observer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000119
120 virtual ~TransmitMixer();
121
tommiba08a142017-02-28 08:25:11 -0800122#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
tommib1175bb2017-02-28 01:16:48 -0800123 // Periodic callback from the MonitorModule.
niklase@google.com470e71d2011-07-07 08:21:25 +0000124 void OnPeriodicProcess();
tommiba08a142017-02-28 08:25:11 -0800125#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000127 // FileCallback
solenberg76377c52017-02-21 00:54:31 -0800128 void PlayNotification(const int32_t id,
129 const uint32_t durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
solenberg76377c52017-02-21 00:54:31 -0800131 void RecordNotification(const int32_t id,
132 const uint32_t durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
solenberg76377c52017-02-21 00:54:31 -0800134 void PlayFileEnded(const int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
solenberg76377c52017-02-21 00:54:31 -0800136 void RecordFileEnded(const int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000137
solenberg76377c52017-02-21 00:54:31 -0800138 // Virtual to allow mocking.
139 virtual void EnableStereoChannelSwapping(bool enable);
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000140 bool IsStereoChannelSwappingEnabled();
141
solenberg76377c52017-02-21 00:54:31 -0800142protected:
tommiba08a142017-02-28 08:25:11 -0800143#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
tommib1175bb2017-02-28 01:16:48 -0800144 TransmitMixer() : _monitorModule(this) {}
tommiba08a142017-02-28 08:25:11 -0800145#else
146 TransmitMixer() = default;
147#endif
solenberg76377c52017-02-21 00:54:31 -0800148
niklase@google.com470e71d2011-07-07 08:21:25 +0000149private:
pbos@webrtc.org92135212013-05-14 08:31:39 +0000150 TransmitMixer(uint32_t instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000151
andrew@webrtc.org24120852013-03-02 00:14:46 +0000152 // Gets the maximum sample rate and number of channels over all currently
153 // sending codecs.
Peter Kasting69558702016-01-12 16:26:35 -0800154 void GetSendCodecInfo(int* max_sample_rate, size_t* max_channels);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000155
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000156 void GenerateAudioFrame(const int16_t audioSamples[],
Peter Kastingdce40cf2015-08-24 14:52:23 -0700157 size_t nSamples,
Peter Kasting69558702016-01-12 16:26:35 -0800158 size_t nChannels,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000159 int samplesPerSec);
pbos@webrtc.org92135212013-05-14 08:31:39 +0000160 int32_t RecordAudioToFile(uint32_t mixingFrequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000162 int32_t MixOrReplaceAudioWithFile(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000163 int mixingFrequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000164
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000165 void ProcessAudio(int delay_ms, int clock_drift, int current_mic_level,
166 bool key_pressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000167
henrik.lundinf00082d2016-12-05 02:22:12 -0800168#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000169 void TypingDetection(bool keyPressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000170#endif
171
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000172 // uses
solenberg76377c52017-02-21 00:54:31 -0800173 Statistics* _engineStatisticsPtr = nullptr;
174 ChannelManager* _channelManagerPtr = nullptr;
175 AudioProcessing* audioproc_ = nullptr;
176 VoiceEngineObserver* _voiceEngineObserverPtr = nullptr;
177 ProcessThread* _processThreadPtr = nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000178
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000179 // owns
niklase@google.com470e71d2011-07-07 08:21:25 +0000180 AudioFrame _audioFrame;
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000181 PushResampler<int16_t> resampler_; // ADM sample rate -> mixing rate
kwiberg5a25d952016-08-17 07:31:12 -0700182 std::unique_ptr<FilePlayer> file_player_;
183 std::unique_ptr<FileRecorder> file_recorder_;
184 std::unique_ptr<FileRecorder> file_call_recorder_;
solenberg76377c52017-02-21 00:54:31 -0800185 int _filePlayerId = 0;
186 int _fileRecorderId = 0;
187 int _fileCallRecorderId = 0;
188 bool _filePlaying = false;
189 bool _fileRecording = false;
190 bool _fileCallRecording = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191 voe::AudioLevel _audioLevel;
192 // protect file instances and their variables in MixedParticipants()
tommi31fc21f2016-01-21 10:37:37 -0800193 rtc::CriticalSection _critSect;
194 rtc::CriticalSection _callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
henrik.lundinf00082d2016-12-05 02:22:12 -0800196#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
tommiba08a142017-02-28 08:25:11 -0800197 MonitorModule<TransmitMixer> _monitorModule;
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000198 webrtc::TypingDetection _typingDetection;
solenberg76377c52017-02-21 00:54:31 -0800199 bool _typingNoiseWarningPending = false;
200 bool _typingNoiseDetected = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000201#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000202
solenberg76377c52017-02-21 00:54:31 -0800203 int _instanceId = 0;
204 bool _mixFileWithMicrophone = false;
205 uint32_t _captureLevel = 0;
solenberg76377c52017-02-21 00:54:31 -0800206 bool stereo_codec_ = false;
207 bool swap_stereo_channels_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000208};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000209} // namespace voe
niklase@google.com470e71d2011-07-07 08:21:25 +0000210} // namespace webrtc
braveyao@webrtc.orga7cfa672013-12-24 03:39:10 +0000211
braveyao@webrtc.org0062a6d2013-12-24 03:58:51 +0000212#endif // WEBRTC_VOICE_ENGINE_TRANSMIT_MIXER_H