blob: 6fcb86ea6f45012b62ec11f7b950192a18abc732 [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
andrew@webrtc.org28e82bf2013-05-02 00:30:36 +000016#include "webrtc/common_audio/resampler/include/push_resampler.h"
17#include "webrtc/common_types.h"
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +000018#include "webrtc/modules/audio_processing/typing_detection.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010019#include "webrtc/modules/include/module_common_types.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020020#include "webrtc/rtc_base/criticalsection.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
zsteine76bd3a2017-07-14 12:17:49 -070079 // See description of "totalAudioEnergy" in the WebRTC stats spec:
80 // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
81 // 'virtual' to allow mocking.
82 virtual double GetTotalInputEnergy() const;
83
84 // 'virtual' to allow mocking.
85 virtual double GetTotalInputDuration() const;
86
niklase@google.com470e71d2011-07-07 08:21:25 +000087 bool IsRecordingCall();
88
89 bool IsRecordingMic();
90
91 int StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +000092 bool loop,
93 FileFormats format,
94 int startPosition,
95 float volumeScaling,
96 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +000097 const CodecInst* codecInst);
98
99 int StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000100 FileFormats format,
101 int startPosition,
102 float volumeScaling,
103 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +0000104 const CodecInst* codecInst);
105
106 int StopPlayingFileAsMicrophone();
107
108 int IsPlayingFileAsMicrophone() const;
109
niklase@google.com470e71d2011-07-07 08:21:25 +0000110 int StartRecordingMicrophone(const char* fileName,
111 const CodecInst* codecInst);
112
113 int StartRecordingMicrophone(OutStream* stream,
114 const CodecInst* codecInst);
115
116 int StopRecordingMicrophone();
117
118 int StartRecordingCall(const char* fileName, const CodecInst* codecInst);
119
120 int StartRecordingCall(OutStream* stream, const CodecInst* codecInst);
121
122 int StopRecordingCall();
123
124 void SetMixWithMicStatus(bool mix);
125
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000126 int32_t RegisterVoiceEngineObserver(VoiceEngineObserver& observer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
128 virtual ~TransmitMixer();
129
tommiba08a142017-02-28 08:25:11 -0800130#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
tommib1175bb2017-02-28 01:16:48 -0800131 // Periodic callback from the MonitorModule.
niklase@google.com470e71d2011-07-07 08:21:25 +0000132 void OnPeriodicProcess();
tommiba08a142017-02-28 08:25:11 -0800133#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000134
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000135 // FileCallback
solenberg76377c52017-02-21 00:54:31 -0800136 void PlayNotification(const int32_t id,
137 const uint32_t durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
solenberg76377c52017-02-21 00:54:31 -0800139 void RecordNotification(const int32_t id,
140 const uint32_t durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000141
solenberg76377c52017-02-21 00:54:31 -0800142 void PlayFileEnded(const int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000143
solenberg76377c52017-02-21 00:54:31 -0800144 void RecordFileEnded(const int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
solenberg76377c52017-02-21 00:54:31 -0800146 // Virtual to allow mocking.
147 virtual void EnableStereoChannelSwapping(bool enable);
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000148 bool IsStereoChannelSwappingEnabled();
149
solenberg76377c52017-02-21 00:54:31 -0800150protected:
tommiba08a142017-02-28 08:25:11 -0800151#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
tommib1175bb2017-02-28 01:16:48 -0800152 TransmitMixer() : _monitorModule(this) {}
tommiba08a142017-02-28 08:25:11 -0800153#else
154 TransmitMixer() = default;
155#endif
solenberg76377c52017-02-21 00:54:31 -0800156
niklase@google.com470e71d2011-07-07 08:21:25 +0000157private:
pbos@webrtc.org92135212013-05-14 08:31:39 +0000158 TransmitMixer(uint32_t instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000159
andrew@webrtc.org24120852013-03-02 00:14:46 +0000160 // Gets the maximum sample rate and number of channels over all currently
161 // sending codecs.
Peter Kasting69558702016-01-12 16:26:35 -0800162 void GetSendCodecInfo(int* max_sample_rate, size_t* max_channels);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000163
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000164 void GenerateAudioFrame(const int16_t audioSamples[],
Peter Kastingdce40cf2015-08-24 14:52:23 -0700165 size_t nSamples,
Peter Kasting69558702016-01-12 16:26:35 -0800166 size_t nChannels,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000167 int samplesPerSec);
pbos@webrtc.org92135212013-05-14 08:31:39 +0000168 int32_t RecordAudioToFile(uint32_t mixingFrequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000169
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000170 int32_t MixOrReplaceAudioWithFile(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000171 int mixingFrequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000172
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000173 void ProcessAudio(int delay_ms, int clock_drift, int current_mic_level,
174 bool key_pressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000175
henrik.lundinf00082d2016-12-05 02:22:12 -0800176#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000177 void TypingDetection(bool keyPressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000178#endif
179
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000180 // uses
solenberg76377c52017-02-21 00:54:31 -0800181 Statistics* _engineStatisticsPtr = nullptr;
182 ChannelManager* _channelManagerPtr = nullptr;
183 AudioProcessing* audioproc_ = nullptr;
184 VoiceEngineObserver* _voiceEngineObserverPtr = nullptr;
185 ProcessThread* _processThreadPtr = nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000186
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000187 // owns
niklase@google.com470e71d2011-07-07 08:21:25 +0000188 AudioFrame _audioFrame;
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000189 PushResampler<int16_t> resampler_; // ADM sample rate -> mixing rate
kwiberg5a25d952016-08-17 07:31:12 -0700190 std::unique_ptr<FilePlayer> file_player_;
191 std::unique_ptr<FileRecorder> file_recorder_;
192 std::unique_ptr<FileRecorder> file_call_recorder_;
solenberg76377c52017-02-21 00:54:31 -0800193 int _filePlayerId = 0;
194 int _fileRecorderId = 0;
195 int _fileCallRecorderId = 0;
196 bool _filePlaying = false;
197 bool _fileRecording = false;
198 bool _fileCallRecording = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000199 voe::AudioLevel _audioLevel;
zsteine76bd3a2017-07-14 12:17:49 -0700200 double totalInputEnergy_ = 0.0;
201 double totalInputDuration_ = 0.0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000202 // protect file instances and their variables in MixedParticipants()
tommi31fc21f2016-01-21 10:37:37 -0800203 rtc::CriticalSection _critSect;
204 rtc::CriticalSection _callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
henrik.lundinf00082d2016-12-05 02:22:12 -0800206#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
tommiba08a142017-02-28 08:25:11 -0800207 MonitorModule<TransmitMixer> _monitorModule;
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000208 webrtc::TypingDetection _typingDetection;
solenberg76377c52017-02-21 00:54:31 -0800209 bool _typingNoiseWarningPending = false;
210 bool _typingNoiseDetected = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000212
solenberg76377c52017-02-21 00:54:31 -0800213 int _instanceId = 0;
214 bool _mixFileWithMicrophone = false;
215 uint32_t _captureLevel = 0;
solenberg76377c52017-02-21 00:54:31 -0800216 bool stereo_codec_ = false;
217 bool swap_stereo_channels_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000218};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000219} // namespace voe
niklase@google.com470e71d2011-07-07 08:21:25 +0000220} // namespace webrtc
braveyao@webrtc.orga7cfa672013-12-24 03:39:10 +0000221
braveyao@webrtc.org0062a6d2013-12-24 03:58:51 +0000222#endif // WEBRTC_VOICE_ENGINE_TRANSMIT_MIXER_H