blob: e0246cf4ceaa9595f6bf77a4d908dd20ab42e132 [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
tommi31fc21f2016-01-21 10:37:37 -080014#include "webrtc/base/criticalsection.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000015#include "webrtc/base/scoped_ptr.h"
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"
20#include "webrtc/modules/utility/include/file_player.h"
21#include "webrtc/modules/utility/include/file_recorder.h"
andrew@webrtc.org28e82bf2013-05-02 00:30:36 +000022#include "webrtc/voice_engine/include/voe_base.h"
23#include "webrtc/voice_engine/level_indicator.h"
24#include "webrtc/voice_engine/monitor_module.h"
25#include "webrtc/voice_engine/voice_engine_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000026
27namespace webrtc {
28
29class AudioProcessing;
30class ProcessThread;
31class VoEExternalMedia;
32class VoEMediaProcess;
33
34namespace voe {
35
36class ChannelManager;
37class MixedAudio;
38class Statistics;
39
40class TransmitMixer : public MonitorObserver,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000041 public FileCallback {
niklase@google.com470e71d2011-07-07 08:21:25 +000042public:
pbos@webrtc.org92135212013-05-14 08:31:39 +000043 static int32_t Create(TransmitMixer*& mixer, uint32_t instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +000044
45 static void Destroy(TransmitMixer*& mixer);
46
pbos@webrtc.org6141e132013-04-09 10:09:10 +000047 int32_t SetEngineInformation(ProcessThread& processThread,
48 Statistics& engineStatistics,
49 ChannelManager& channelManager);
niklase@google.com470e71d2011-07-07 08:21:25 +000050
pbos@webrtc.org6141e132013-04-09 10:09:10 +000051 int32_t SetAudioProcessingModule(
niklase@google.com470e71d2011-07-07 08:21:25 +000052 AudioProcessing* audioProcessingModule);
53
pbos@webrtc.org6141e132013-04-09 10:09:10 +000054 int32_t PrepareDemux(const void* audioSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -070055 size_t nSamples,
Peter Kasting69558702016-01-12 16:26:35 -080056 size_t nChannels,
pbos@webrtc.org92135212013-05-14 08:31:39 +000057 uint32_t samplesPerSec,
58 uint16_t totalDelayMS,
59 int32_t clockDrift,
60 uint16_t currentMicLevel,
61 bool keyPressed);
niklase@google.com470e71d2011-07-07 08:21:25 +000062
63
pbos@webrtc.org6141e132013-04-09 10:09:10 +000064 int32_t DemuxAndMix();
xians@webrtc.org2f84afa2013-07-31 16:23:37 +000065 // Used by the Chrome to pass the recording data to the specific VoE
66 // channels for demux.
Peter Kasting69558702016-01-12 16:26:35 -080067 void DemuxAndMix(const int voe_channels[], size_t number_of_voe_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000068
pbos@webrtc.org6141e132013-04-09 10:09:10 +000069 int32_t EncodeAndSend();
xians@webrtc.org2f84afa2013-07-31 16:23:37 +000070 // Used by the Chrome to pass the recording data to the specific VoE
71 // channels for encoding and sending to the network.
Peter Kasting69558702016-01-12 16:26:35 -080072 void EncodeAndSend(const int voe_channels[], size_t number_of_voe_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000073
andrew@webrtc.org023cc5a2014-01-11 01:25:53 +000074 // Must be called on the same thread as PrepareDemux().
pbos@webrtc.org6141e132013-04-09 10:09:10 +000075 uint32_t CaptureLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000076
pbos@webrtc.org6141e132013-04-09 10:09:10 +000077 int32_t StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +000078
niklase@google.com470e71d2011-07-07 08:21:25 +000079 // VoEDtmf
pbos@webrtc.org92135212013-05-14 08:31:39 +000080 void UpdateMuteMicrophoneTime(uint32_t lengthMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000081
82 // VoEExternalMedia
andrew@webrtc.org21ab3ba2012-10-19 17:30:56 +000083 int RegisterExternalMediaProcessing(VoEMediaProcess* object,
84 ProcessingTypes type);
85 int DeRegisterExternalMediaProcessing(ProcessingTypes type);
niklase@google.com470e71d2011-07-07 08:21:25 +000086
xians@google.com0b0665a2011-08-08 08:18:44 +000087 int GetMixingFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +000088
89 // VoEVolumeControl
pbos@webrtc.org92135212013-05-14 08:31:39 +000090 int SetMute(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000091
92 bool Mute() const;
93
pbos@webrtc.org6141e132013-04-09 10:09:10 +000094 int8_t AudioLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000095
pbos@webrtc.org6141e132013-04-09 10:09:10 +000096 int16_t AudioLevelFullRange() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000097
98 bool IsRecordingCall();
99
100 bool IsRecordingMic();
101
102 int StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000103 bool loop,
104 FileFormats format,
105 int startPosition,
106 float volumeScaling,
107 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +0000108 const CodecInst* codecInst);
109
110 int StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000111 FileFormats format,
112 int startPosition,
113 float volumeScaling,
114 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +0000115 const CodecInst* codecInst);
116
117 int StopPlayingFileAsMicrophone();
118
119 int IsPlayingFileAsMicrophone() const;
120
niklase@google.com470e71d2011-07-07 08:21:25 +0000121 int StartRecordingMicrophone(const char* fileName,
122 const CodecInst* codecInst);
123
124 int StartRecordingMicrophone(OutStream* stream,
125 const CodecInst* codecInst);
126
127 int StopRecordingMicrophone();
128
129 int StartRecordingCall(const char* fileName, const CodecInst* codecInst);
130
131 int StartRecordingCall(OutStream* stream, const CodecInst* codecInst);
132
133 int StopRecordingCall();
134
135 void SetMixWithMicStatus(bool mix);
136
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000137 int32_t RegisterVoiceEngineObserver(VoiceEngineObserver& observer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
139 virtual ~TransmitMixer();
140
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000141 // MonitorObserver
niklase@google.com470e71d2011-07-07 08:21:25 +0000142 void OnPeriodicProcess();
143
144
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000145 // FileCallback
pbos@webrtc.org92135212013-05-14 08:31:39 +0000146 void PlayNotification(int32_t id,
147 uint32_t durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000148
pbos@webrtc.org92135212013-05-14 08:31:39 +0000149 void RecordNotification(int32_t id,
150 uint32_t durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000151
pbos@webrtc.org92135212013-05-14 08:31:39 +0000152 void PlayFileEnded(int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000153
pbos@webrtc.org92135212013-05-14 08:31:39 +0000154 void RecordFileEnded(int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000155
niklas.enbom@webrtc.org3dc88652012-03-30 09:53:54 +0000156#ifdef WEBRTC_VOICE_ENGINE_TYPING_DETECTION
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000157 // Typing detection
niklas.enbom@webrtc.org3dc88652012-03-30 09:53:54 +0000158 int TimeSinceLastTyping(int &seconds);
niklas.enbom@webrtc.org06e722a2012-04-04 07:44:27 +0000159 int SetTypingDetectionParameters(int timeWindow,
160 int costPerTyping,
161 int reportingThreshold,
niklas.enbom@webrtc.orgf6edfef2012-05-09 13:16:12 +0000162 int penaltyDecay,
163 int typeEventDelay);
niklas.enbom@webrtc.org3dc88652012-03-30 09:53:54 +0000164#endif
165
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000166 void EnableStereoChannelSwapping(bool enable);
167 bool IsStereoChannelSwappingEnabled();
168
niklase@google.com470e71d2011-07-07 08:21:25 +0000169private:
pbos@webrtc.org92135212013-05-14 08:31:39 +0000170 TransmitMixer(uint32_t instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
andrew@webrtc.org24120852013-03-02 00:14:46 +0000172 // Gets the maximum sample rate and number of channels over all currently
173 // sending codecs.
Peter Kasting69558702016-01-12 16:26:35 -0800174 void GetSendCodecInfo(int* max_sample_rate, size_t* max_channels);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000175
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000176 void GenerateAudioFrame(const int16_t audioSamples[],
Peter Kastingdce40cf2015-08-24 14:52:23 -0700177 size_t nSamples,
Peter Kasting69558702016-01-12 16:26:35 -0800178 size_t nChannels,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000179 int samplesPerSec);
pbos@webrtc.org92135212013-05-14 08:31:39 +0000180 int32_t RecordAudioToFile(uint32_t mixingFrequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000181
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000182 int32_t MixOrReplaceAudioWithFile(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000183 int mixingFrequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000185 void ProcessAudio(int delay_ms, int clock_drift, int current_mic_level,
186 bool key_pressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000187
188#ifdef WEBRTC_VOICE_ENGINE_TYPING_DETECTION
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000189 void TypingDetection(bool keyPressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000190#endif
191
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000192 // uses
niklase@google.com470e71d2011-07-07 08:21:25 +0000193 Statistics* _engineStatisticsPtr;
194 ChannelManager* _channelManagerPtr;
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000195 AudioProcessing* audioproc_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196 VoiceEngineObserver* _voiceEngineObserverPtr;
197 ProcessThread* _processThreadPtr;
198
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000199 // owns
niklase@google.com470e71d2011-07-07 08:21:25 +0000200 MonitorModule _monitorModule;
201 AudioFrame _audioFrame;
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000202 PushResampler<int16_t> resampler_; // ADM sample rate -> mixing rate
andrew@webrtc.org21ab3ba2012-10-19 17:30:56 +0000203 FilePlayer* _filePlayerPtr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000204 FileRecorder* _fileRecorderPtr;
205 FileRecorder* _fileCallRecorderPtr;
xians@google.com0b0665a2011-08-08 08:18:44 +0000206 int _filePlayerId;
207 int _fileRecorderId;
208 int _fileCallRecorderId;
niklase@google.com470e71d2011-07-07 08:21:25 +0000209 bool _filePlaying;
210 bool _fileRecording;
211 bool _fileCallRecording;
212 voe::AudioLevel _audioLevel;
213 // protect file instances and their variables in MixedParticipants()
tommi31fc21f2016-01-21 10:37:37 -0800214 rtc::CriticalSection _critSect;
215 rtc::CriticalSection _callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000216
217#ifdef WEBRTC_VOICE_ENGINE_TYPING_DETECTION
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000218 webrtc::TypingDetection _typingDetection;
jiayl@webrtc.orgbf007402013-09-17 18:09:20 +0000219 bool _typingNoiseWarningPending;
220 bool _typingNoiseDetected;
niklase@google.com470e71d2011-07-07 08:21:25 +0000221#endif
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000222 bool _saturationWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000223
xians@google.com0b0665a2011-08-08 08:18:44 +0000224 int _instanceId;
niklase@google.com470e71d2011-07-07 08:21:25 +0000225 bool _mixFileWithMicrophone;
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000226 uint32_t _captureLevel;
andrew@webrtc.org21ab3ba2012-10-19 17:30:56 +0000227 VoEMediaProcess* external_postproc_ptr_;
228 VoEMediaProcess* external_preproc_ptr_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000229 bool _mute;
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000230 int32_t _remainingMuteMicTimeMs;
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000231 bool stereo_codec_;
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000232 bool swap_stereo_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000233};
234
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000235} // namespace voe
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
237} // namespace webrtc
braveyao@webrtc.orga7cfa672013-12-24 03:39:10 +0000238
braveyao@webrtc.org0062a6d2013-12-24 03:58:51 +0000239#endif // WEBRTC_VOICE_ENGINE_TRANSMIT_MIXER_H