blob: 2e728f8e1caa4f7164e554b9c20efce4a913ac94 [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 {
35
36class AudioProcessing;
37class ProcessThread;
niklase@google.com470e71d2011-07-07 08:21:25 +000038
39namespace voe {
40
41class ChannelManager;
42class MixedAudio;
43class Statistics;
44
tommib1175bb2017-02-28 01:16:48 -080045class TransmitMixer : public FileCallback {
niklase@google.com470e71d2011-07-07 08:21:25 +000046public:
pbos@webrtc.org92135212013-05-14 08:31:39 +000047 static int32_t Create(TransmitMixer*& mixer, uint32_t instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +000048
49 static void Destroy(TransmitMixer*& mixer);
50
pbos@webrtc.org6141e132013-04-09 10:09:10 +000051 int32_t SetEngineInformation(ProcessThread& processThread,
52 Statistics& engineStatistics,
53 ChannelManager& channelManager);
niklase@google.com470e71d2011-07-07 08:21:25 +000054
pbos@webrtc.org6141e132013-04-09 10:09:10 +000055 int32_t SetAudioProcessingModule(
niklase@google.com470e71d2011-07-07 08:21:25 +000056 AudioProcessing* audioProcessingModule);
57
pbos@webrtc.org6141e132013-04-09 10:09:10 +000058 int32_t PrepareDemux(const void* audioSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -070059 size_t nSamples,
Peter Kasting69558702016-01-12 16:26:35 -080060 size_t nChannels,
pbos@webrtc.org92135212013-05-14 08:31:39 +000061 uint32_t samplesPerSec,
62 uint16_t totalDelayMS,
63 int32_t clockDrift,
64 uint16_t currentMicLevel,
65 bool keyPressed);
niklase@google.com470e71d2011-07-07 08:21:25 +000066
67
pbos@webrtc.org6141e132013-04-09 10:09:10 +000068 int32_t DemuxAndMix();
xians@webrtc.org2f84afa2013-07-31 16:23:37 +000069 // Used by the Chrome to pass the recording data to the specific VoE
70 // channels for demux.
Peter Kasting69558702016-01-12 16:26:35 -080071 void DemuxAndMix(const int voe_channels[], size_t number_of_voe_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000072
pbos@webrtc.org6141e132013-04-09 10:09:10 +000073 int32_t EncodeAndSend();
xians@webrtc.org2f84afa2013-07-31 16:23:37 +000074 // Used by the Chrome to pass the recording data to the specific VoE
75 // channels for encoding and sending to the network.
Peter Kasting69558702016-01-12 16:26:35 -080076 void EncodeAndSend(const int voe_channels[], size_t number_of_voe_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000077
andrew@webrtc.org023cc5a2014-01-11 01:25:53 +000078 // Must be called on the same thread as PrepareDemux().
pbos@webrtc.org6141e132013-04-09 10:09:10 +000079 uint32_t CaptureLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000080
pbos@webrtc.org6141e132013-04-09 10:09:10 +000081 int32_t StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +000082
solenberg8d73f8c2017-03-08 01:52:20 -080083 // TODO(solenberg): Remove, once AudioMonitor is gone.
pbos@webrtc.org6141e132013-04-09 10:09:10 +000084 int8_t AudioLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000085
solenberg796b8f92017-03-01 17:02:23 -080086 // 'virtual' to allow mocking.
87 virtual int16_t AudioLevelFullRange() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000088
89 bool IsRecordingCall();
90
91 bool IsRecordingMic();
92
93 int StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +000094 bool loop,
95 FileFormats format,
96 int startPosition,
97 float volumeScaling,
98 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +000099 const CodecInst* codecInst);
100
101 int StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000102 FileFormats format,
103 int startPosition,
104 float volumeScaling,
105 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +0000106 const CodecInst* codecInst);
107
108 int StopPlayingFileAsMicrophone();
109
110 int IsPlayingFileAsMicrophone() const;
111
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 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.org6141e132013-04-09 10:09:10 +0000128 int32_t RegisterVoiceEngineObserver(VoiceEngineObserver& observer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
130 virtual ~TransmitMixer();
131
tommiba08a142017-02-28 08:25:11 -0800132#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
tommib1175bb2017-02-28 01:16:48 -0800133 // Periodic callback from the MonitorModule.
niklase@google.com470e71d2011-07-07 08:21:25 +0000134 void OnPeriodicProcess();
tommiba08a142017-02-28 08:25:11 -0800135#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000136
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000137 // FileCallback
solenberg76377c52017-02-21 00:54:31 -0800138 void PlayNotification(const int32_t id,
139 const uint32_t durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000140
solenberg76377c52017-02-21 00:54:31 -0800141 void RecordNotification(const int32_t id,
142 const uint32_t durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000143
solenberg76377c52017-02-21 00:54:31 -0800144 void PlayFileEnded(const int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
solenberg76377c52017-02-21 00:54:31 -0800146 void RecordFileEnded(const int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000147
solenberg76377c52017-02-21 00:54:31 -0800148 // Virtual to allow mocking.
149 virtual void EnableStereoChannelSwapping(bool enable);
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000150 bool IsStereoChannelSwappingEnabled();
151
solenberg76377c52017-02-21 00:54:31 -0800152protected:
tommiba08a142017-02-28 08:25:11 -0800153#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
tommib1175bb2017-02-28 01:16:48 -0800154 TransmitMixer() : _monitorModule(this) {}
tommiba08a142017-02-28 08:25:11 -0800155#else
156 TransmitMixer() = default;
157#endif
solenberg76377c52017-02-21 00:54:31 -0800158
niklase@google.com470e71d2011-07-07 08:21:25 +0000159private:
pbos@webrtc.org92135212013-05-14 08:31:39 +0000160 TransmitMixer(uint32_t instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
andrew@webrtc.org24120852013-03-02 00:14:46 +0000162 // Gets the maximum sample rate and number of channels over all currently
163 // sending codecs.
Peter Kasting69558702016-01-12 16:26:35 -0800164 void GetSendCodecInfo(int* max_sample_rate, size_t* max_channels);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000165
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000166 void GenerateAudioFrame(const int16_t audioSamples[],
Peter Kastingdce40cf2015-08-24 14:52:23 -0700167 size_t nSamples,
Peter Kasting69558702016-01-12 16:26:35 -0800168 size_t nChannels,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000169 int samplesPerSec);
pbos@webrtc.org92135212013-05-14 08:31:39 +0000170 int32_t RecordAudioToFile(uint32_t mixingFrequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000172 int32_t MixOrReplaceAudioWithFile(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000173 int mixingFrequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000174
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000175 void ProcessAudio(int delay_ms, int clock_drift, int current_mic_level,
176 bool key_pressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
henrik.lundinf00082d2016-12-05 02:22:12 -0800178#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000179 void TypingDetection(bool keyPressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000180#endif
181
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000182 // uses
solenberg76377c52017-02-21 00:54:31 -0800183 Statistics* _engineStatisticsPtr = nullptr;
184 ChannelManager* _channelManagerPtr = nullptr;
185 AudioProcessing* audioproc_ = nullptr;
186 VoiceEngineObserver* _voiceEngineObserverPtr = nullptr;
187 ProcessThread* _processThreadPtr = nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000188
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000189 // owns
niklase@google.com470e71d2011-07-07 08:21:25 +0000190 AudioFrame _audioFrame;
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000191 PushResampler<int16_t> resampler_; // ADM sample rate -> mixing rate
kwiberg5a25d952016-08-17 07:31:12 -0700192 std::unique_ptr<FilePlayer> file_player_;
193 std::unique_ptr<FileRecorder> file_recorder_;
194 std::unique_ptr<FileRecorder> file_call_recorder_;
solenberg76377c52017-02-21 00:54:31 -0800195 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.com470e71d2011-07-07 08:21:25 +0000201 voe::AudioLevel _audioLevel;
202 // 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