blob: 3147c1036f250809ac9ff6667fc97135e9dcc56c [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"
kwiberg9d7eb132016-08-16 04:08:30 -070021#include "webrtc/modules/utility/include/file_player.h"
22#include "webrtc/modules/utility/include/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;
38class VoEExternalMedia;
39class VoEMediaProcess;
40
41namespace voe {
42
43class ChannelManager;
44class MixedAudio;
45class Statistics;
46
47class TransmitMixer : public MonitorObserver,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000048 public FileCallback {
niklase@google.com470e71d2011-07-07 08:21:25 +000049public:
pbos@webrtc.org92135212013-05-14 08:31:39 +000050 static int32_t Create(TransmitMixer*& mixer, uint32_t instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +000051
52 static void Destroy(TransmitMixer*& mixer);
53
pbos@webrtc.org6141e132013-04-09 10:09:10 +000054 int32_t SetEngineInformation(ProcessThread& processThread,
55 Statistics& engineStatistics,
56 ChannelManager& channelManager);
niklase@google.com470e71d2011-07-07 08:21:25 +000057
pbos@webrtc.org6141e132013-04-09 10:09:10 +000058 int32_t SetAudioProcessingModule(
niklase@google.com470e71d2011-07-07 08:21:25 +000059 AudioProcessing* audioProcessingModule);
60
pbos@webrtc.org6141e132013-04-09 10:09:10 +000061 int32_t PrepareDemux(const void* audioSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -070062 size_t nSamples,
Peter Kasting69558702016-01-12 16:26:35 -080063 size_t nChannels,
pbos@webrtc.org92135212013-05-14 08:31:39 +000064 uint32_t samplesPerSec,
65 uint16_t totalDelayMS,
66 int32_t clockDrift,
67 uint16_t currentMicLevel,
68 bool keyPressed);
niklase@google.com470e71d2011-07-07 08:21:25 +000069
70
pbos@webrtc.org6141e132013-04-09 10:09:10 +000071 int32_t DemuxAndMix();
xians@webrtc.org2f84afa2013-07-31 16:23:37 +000072 // Used by the Chrome to pass the recording data to the specific VoE
73 // channels for demux.
Peter Kasting69558702016-01-12 16:26:35 -080074 void DemuxAndMix(const int voe_channels[], size_t number_of_voe_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000075
pbos@webrtc.org6141e132013-04-09 10:09:10 +000076 int32_t EncodeAndSend();
xians@webrtc.org2f84afa2013-07-31 16:23:37 +000077 // Used by the Chrome to pass the recording data to the specific VoE
78 // channels for encoding and sending to the network.
Peter Kasting69558702016-01-12 16:26:35 -080079 void EncodeAndSend(const int voe_channels[], size_t number_of_voe_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000080
andrew@webrtc.org023cc5a2014-01-11 01:25:53 +000081 // Must be called on the same thread as PrepareDemux().
pbos@webrtc.org6141e132013-04-09 10:09:10 +000082 uint32_t CaptureLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000083
pbos@webrtc.org6141e132013-04-09 10:09:10 +000084 int32_t StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +000085
niklase@google.com470e71d2011-07-07 08:21:25 +000086 // VoEExternalMedia
andrew@webrtc.org21ab3ba2012-10-19 17:30:56 +000087 int RegisterExternalMediaProcessing(VoEMediaProcess* object,
88 ProcessingTypes type);
89 int DeRegisterExternalMediaProcessing(ProcessingTypes type);
niklase@google.com470e71d2011-07-07 08:21:25 +000090
xians@google.com0b0665a2011-08-08 08:18:44 +000091 int GetMixingFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +000092
93 // VoEVolumeControl
pbos@webrtc.org92135212013-05-14 08:31:39 +000094 int SetMute(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000095
96 bool Mute() const;
97
pbos@webrtc.org6141e132013-04-09 10:09:10 +000098 int8_t AudioLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000099
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000100 int16_t AudioLevelFullRange() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000101
102 bool IsRecordingCall();
103
104 bool IsRecordingMic();
105
106 int StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000107 bool loop,
108 FileFormats format,
109 int startPosition,
110 float volumeScaling,
111 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 const CodecInst* codecInst);
113
114 int StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000115 FileFormats format,
116 int startPosition,
117 float volumeScaling,
118 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +0000119 const CodecInst* codecInst);
120
121 int StopPlayingFileAsMicrophone();
122
123 int IsPlayingFileAsMicrophone() const;
124
niklase@google.com470e71d2011-07-07 08:21:25 +0000125 int StartRecordingMicrophone(const char* fileName,
126 const CodecInst* codecInst);
127
128 int StartRecordingMicrophone(OutStream* stream,
129 const CodecInst* codecInst);
130
131 int StopRecordingMicrophone();
132
133 int StartRecordingCall(const char* fileName, const CodecInst* codecInst);
134
135 int StartRecordingCall(OutStream* stream, const CodecInst* codecInst);
136
137 int StopRecordingCall();
138
139 void SetMixWithMicStatus(bool mix);
140
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000141 int32_t RegisterVoiceEngineObserver(VoiceEngineObserver& observer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000142
143 virtual ~TransmitMixer();
144
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000145 // MonitorObserver
niklase@google.com470e71d2011-07-07 08:21:25 +0000146 void OnPeriodicProcess();
147
148
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000149 // FileCallback
pbos@webrtc.org92135212013-05-14 08:31:39 +0000150 void PlayNotification(int32_t id,
151 uint32_t durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000152
pbos@webrtc.org92135212013-05-14 08:31:39 +0000153 void RecordNotification(int32_t id,
154 uint32_t durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000155
pbos@webrtc.org92135212013-05-14 08:31:39 +0000156 void PlayFileEnded(int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
pbos@webrtc.org92135212013-05-14 08:31:39 +0000158 void RecordFileEnded(int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000159
henrik.lundinf00082d2016-12-05 02:22:12 -0800160#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000161 // Typing detection
niklas.enbom@webrtc.org3dc88652012-03-30 09:53:54 +0000162 int TimeSinceLastTyping(int &seconds);
niklas.enbom@webrtc.org06e722a2012-04-04 07:44:27 +0000163 int SetTypingDetectionParameters(int timeWindow,
164 int costPerTyping,
165 int reportingThreshold,
niklas.enbom@webrtc.orgf6edfef2012-05-09 13:16:12 +0000166 int penaltyDecay,
167 int typeEventDelay);
niklas.enbom@webrtc.org3dc88652012-03-30 09:53:54 +0000168#endif
169
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000170 void EnableStereoChannelSwapping(bool enable);
171 bool IsStereoChannelSwappingEnabled();
172
niklase@google.com470e71d2011-07-07 08:21:25 +0000173private:
pbos@webrtc.org92135212013-05-14 08:31:39 +0000174 TransmitMixer(uint32_t instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000175
andrew@webrtc.org24120852013-03-02 00:14:46 +0000176 // Gets the maximum sample rate and number of channels over all currently
177 // sending codecs.
Peter Kasting69558702016-01-12 16:26:35 -0800178 void GetSendCodecInfo(int* max_sample_rate, size_t* max_channels);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000179
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000180 void GenerateAudioFrame(const int16_t audioSamples[],
Peter Kastingdce40cf2015-08-24 14:52:23 -0700181 size_t nSamples,
Peter Kasting69558702016-01-12 16:26:35 -0800182 size_t nChannels,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000183 int samplesPerSec);
pbos@webrtc.org92135212013-05-14 08:31:39 +0000184 int32_t RecordAudioToFile(uint32_t mixingFrequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000185
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000186 int32_t MixOrReplaceAudioWithFile(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000187 int mixingFrequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000188
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000189 void ProcessAudio(int delay_ms, int clock_drift, int current_mic_level,
190 bool key_pressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000191
henrik.lundinf00082d2016-12-05 02:22:12 -0800192#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000193 void TypingDetection(bool keyPressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000194#endif
195
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000196 // uses
niklase@google.com470e71d2011-07-07 08:21:25 +0000197 Statistics* _engineStatisticsPtr;
198 ChannelManager* _channelManagerPtr;
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000199 AudioProcessing* audioproc_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000200 VoiceEngineObserver* _voiceEngineObserverPtr;
201 ProcessThread* _processThreadPtr;
202
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000203 // owns
niklase@google.com470e71d2011-07-07 08:21:25 +0000204 MonitorModule _monitorModule;
205 AudioFrame _audioFrame;
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000206 PushResampler<int16_t> resampler_; // ADM sample rate -> mixing rate
kwiberg5a25d952016-08-17 07:31:12 -0700207 std::unique_ptr<FilePlayer> file_player_;
208 std::unique_ptr<FileRecorder> file_recorder_;
209 std::unique_ptr<FileRecorder> file_call_recorder_;
xians@google.com0b0665a2011-08-08 08:18:44 +0000210 int _filePlayerId;
211 int _fileRecorderId;
212 int _fileCallRecorderId;
niklase@google.com470e71d2011-07-07 08:21:25 +0000213 bool _filePlaying;
214 bool _fileRecording;
215 bool _fileCallRecording;
216 voe::AudioLevel _audioLevel;
217 // protect file instances and their variables in MixedParticipants()
tommi31fc21f2016-01-21 10:37:37 -0800218 rtc::CriticalSection _critSect;
219 rtc::CriticalSection _callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000220
henrik.lundinf00082d2016-12-05 02:22:12 -0800221#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000222 webrtc::TypingDetection _typingDetection;
jiayl@webrtc.orgbf007402013-09-17 18:09:20 +0000223 bool _typingNoiseWarningPending;
224 bool _typingNoiseDetected;
niklase@google.com470e71d2011-07-07 08:21:25 +0000225#endif
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000226 bool _saturationWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000227
xians@google.com0b0665a2011-08-08 08:18:44 +0000228 int _instanceId;
niklase@google.com470e71d2011-07-07 08:21:25 +0000229 bool _mixFileWithMicrophone;
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000230 uint32_t _captureLevel;
andrew@webrtc.org21ab3ba2012-10-19 17:30:56 +0000231 VoEMediaProcess* external_postproc_ptr_;
232 VoEMediaProcess* external_preproc_ptr_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000233 bool _mute;
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000234 bool stereo_codec_;
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000235 bool swap_stereo_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000236};
237
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000238} // namespace voe
niklase@google.com470e71d2011-07-07 08:21:25 +0000239
240} // namespace webrtc
braveyao@webrtc.orga7cfa672013-12-24 03:39:10 +0000241
braveyao@webrtc.org0062a6d2013-12-24 03:58:51 +0000242#endif // WEBRTC_VOICE_ENGINE_TRANSMIT_MIXER_H