blob: d7ad473a1a33028e274694e92d774ae40fc33de4 [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"
kwiberg97744472017-01-10 01:12:51 -080021#include "webrtc/voice_engine/file_player.h"
22#include "webrtc/voice_engine/file_recorder.h"
andrew@webrtc.org28e82bf2013-05-02 00:30:36 +000023#include "webrtc/voice_engine/include/voe_base.h"
24#include "webrtc/voice_engine/level_indicator.h"
25#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
niklase@google.com470e71d2011-07-07 08:21:25 +000083 // VoEVolumeControl
pbos@webrtc.org92135212013-05-14 08:31:39 +000084 int SetMute(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000085
86 bool Mute() const;
87
pbos@webrtc.org6141e132013-04-09 10:09:10 +000088 int8_t AudioLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000089
pbos@webrtc.org6141e132013-04-09 10:09:10 +000090 int16_t AudioLevelFullRange() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000091
92 bool IsRecordingCall();
93
94 bool IsRecordingMic();
95
96 int StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +000097 bool loop,
98 FileFormats format,
99 int startPosition,
100 float volumeScaling,
101 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +0000102 const CodecInst* codecInst);
103
104 int StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000105 FileFormats format,
106 int startPosition,
107 float volumeScaling,
108 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +0000109 const CodecInst* codecInst);
110
111 int StopPlayingFileAsMicrophone();
112
113 int IsPlayingFileAsMicrophone() const;
114
niklase@google.com470e71d2011-07-07 08:21:25 +0000115 int StartRecordingMicrophone(const char* fileName,
116 const CodecInst* codecInst);
117
118 int StartRecordingMicrophone(OutStream* stream,
119 const CodecInst* codecInst);
120
121 int StopRecordingMicrophone();
122
123 int StartRecordingCall(const char* fileName, const CodecInst* codecInst);
124
125 int StartRecordingCall(OutStream* stream, const CodecInst* codecInst);
126
127 int StopRecordingCall();
128
129 void SetMixWithMicStatus(bool mix);
130
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000131 int32_t RegisterVoiceEngineObserver(VoiceEngineObserver& observer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
133 virtual ~TransmitMixer();
134
tommib1175bb2017-02-28 01:16:48 -0800135 // Periodic callback from the MonitorModule.
niklase@google.com470e71d2011-07-07 08:21:25 +0000136 void OnPeriodicProcess();
137
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000138 // FileCallback
solenberg76377c52017-02-21 00:54:31 -0800139 void PlayNotification(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 RecordNotification(const int32_t id,
143 const uint32_t durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
solenberg76377c52017-02-21 00:54:31 -0800145 void PlayFileEnded(const int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
solenberg76377c52017-02-21 00:54:31 -0800147 void RecordFileEnded(const int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000148
henrik.lundinf00082d2016-12-05 02:22:12 -0800149#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000150 // Typing detection
niklas.enbom@webrtc.org3dc88652012-03-30 09:53:54 +0000151 int TimeSinceLastTyping(int &seconds);
niklas.enbom@webrtc.org06e722a2012-04-04 07:44:27 +0000152 int SetTypingDetectionParameters(int timeWindow,
153 int costPerTyping,
154 int reportingThreshold,
niklas.enbom@webrtc.orgf6edfef2012-05-09 13:16:12 +0000155 int penaltyDecay,
156 int typeEventDelay);
niklas.enbom@webrtc.org3dc88652012-03-30 09:53:54 +0000157#endif
158
solenberg76377c52017-02-21 00:54:31 -0800159 // Virtual to allow mocking.
160 virtual void EnableStereoChannelSwapping(bool enable);
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000161 bool IsStereoChannelSwappingEnabled();
162
solenberg76377c52017-02-21 00:54:31 -0800163protected:
tommib1175bb2017-02-28 01:16:48 -0800164 TransmitMixer() : _monitorModule(this) {}
solenberg76377c52017-02-21 00:54:31 -0800165
niklase@google.com470e71d2011-07-07 08:21:25 +0000166private:
pbos@webrtc.org92135212013-05-14 08:31:39 +0000167 TransmitMixer(uint32_t instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000168
andrew@webrtc.org24120852013-03-02 00:14:46 +0000169 // Gets the maximum sample rate and number of channels over all currently
170 // sending codecs.
Peter Kasting69558702016-01-12 16:26:35 -0800171 void GetSendCodecInfo(int* max_sample_rate, size_t* max_channels);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000172
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000173 void GenerateAudioFrame(const int16_t audioSamples[],
Peter Kastingdce40cf2015-08-24 14:52:23 -0700174 size_t nSamples,
Peter Kasting69558702016-01-12 16:26:35 -0800175 size_t nChannels,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000176 int samplesPerSec);
pbos@webrtc.org92135212013-05-14 08:31:39 +0000177 int32_t RecordAudioToFile(uint32_t mixingFrequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000178
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000179 int32_t MixOrReplaceAudioWithFile(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000180 int mixingFrequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000181
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000182 void ProcessAudio(int delay_ms, int clock_drift, int current_mic_level,
183 bool key_pressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
henrik.lundinf00082d2016-12-05 02:22:12 -0800185#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000186 void TypingDetection(bool keyPressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000187#endif
188
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000189 // uses
solenberg76377c52017-02-21 00:54:31 -0800190 Statistics* _engineStatisticsPtr = nullptr;
191 ChannelManager* _channelManagerPtr = nullptr;
192 AudioProcessing* audioproc_ = nullptr;
193 VoiceEngineObserver* _voiceEngineObserverPtr = nullptr;
194 ProcessThread* _processThreadPtr = nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000196 // owns
tommib1175bb2017-02-28 01:16:48 -0800197 MonitorModule<TransmitMixer> _monitorModule;
niklase@google.com470e71d2011-07-07 08:21:25 +0000198 AudioFrame _audioFrame;
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000199 PushResampler<int16_t> resampler_; // ADM sample rate -> mixing rate
kwiberg5a25d952016-08-17 07:31:12 -0700200 std::unique_ptr<FilePlayer> file_player_;
201 std::unique_ptr<FileRecorder> file_recorder_;
202 std::unique_ptr<FileRecorder> file_call_recorder_;
solenberg76377c52017-02-21 00:54:31 -0800203 int _filePlayerId = 0;
204 int _fileRecorderId = 0;
205 int _fileCallRecorderId = 0;
206 bool _filePlaying = false;
207 bool _fileRecording = false;
208 bool _fileCallRecording = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000209 voe::AudioLevel _audioLevel;
210 // protect file instances and their variables in MixedParticipants()
tommi31fc21f2016-01-21 10:37:37 -0800211 rtc::CriticalSection _critSect;
212 rtc::CriticalSection _callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000213
henrik.lundinf00082d2016-12-05 02:22:12 -0800214#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000215 webrtc::TypingDetection _typingDetection;
solenberg76377c52017-02-21 00:54:31 -0800216 bool _typingNoiseWarningPending = false;
217 bool _typingNoiseDetected = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000218#endif
solenberg76377c52017-02-21 00:54:31 -0800219 bool _saturationWarning = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000220
solenberg76377c52017-02-21 00:54:31 -0800221 int _instanceId = 0;
222 bool _mixFileWithMicrophone = false;
223 uint32_t _captureLevel = 0;
224 bool _mute = false;
225 bool stereo_codec_ = false;
226 bool swap_stereo_channels_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000227};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000228} // namespace voe
niklase@google.com470e71d2011-07-07 08:21:25 +0000229} // namespace webrtc
braveyao@webrtc.orga7cfa672013-12-24 03:39:10 +0000230
braveyao@webrtc.org0062a6d2013-12-24 03:58:51 +0000231#endif // WEBRTC_VOICE_ENGINE_TRANSMIT_MIXER_H