blob: b408614ae9859c475c22baea5222addb0f510998 [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
andrew@webrtc.org28e82bf2013-05-02 00:30:36 +000014#include "webrtc/common_audio/resampler/include/push_resampler.h"
15#include "webrtc/common_types.h"
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +000016#include "webrtc/modules/audio_processing/typing_detection.h"
andrew@webrtc.org28e82bf2013-05-02 00:30:36 +000017#include "webrtc/modules/interface/module_common_types.h"
18#include "webrtc/modules/utility/interface/file_player.h"
19#include "webrtc/modules/utility/interface/file_recorder.h"
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000020#include "webrtc/system_wrappers/interface/scoped_ptr.h"
andrew@webrtc.org28e82bf2013-05-02 00:30:36 +000021#include "webrtc/voice_engine/include/voe_base.h"
22#include "webrtc/voice_engine/level_indicator.h"
23#include "webrtc/voice_engine/monitor_module.h"
24#include "webrtc/voice_engine/voice_engine_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000025
26namespace webrtc {
27
28class AudioProcessing;
29class ProcessThread;
30class VoEExternalMedia;
31class VoEMediaProcess;
32
33namespace voe {
34
35class ChannelManager;
36class MixedAudio;
37class Statistics;
38
39class TransmitMixer : public MonitorObserver,
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +000040 public FileCallback {
niklase@google.com470e71d2011-07-07 08:21:25 +000041public:
pbos@webrtc.org92135212013-05-14 08:31:39 +000042 static int32_t Create(TransmitMixer*& mixer, uint32_t instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +000043
44 static void Destroy(TransmitMixer*& mixer);
45
pbos@webrtc.org6141e132013-04-09 10:09:10 +000046 int32_t SetEngineInformation(ProcessThread& processThread,
47 Statistics& engineStatistics,
48 ChannelManager& channelManager);
niklase@google.com470e71d2011-07-07 08:21:25 +000049
pbos@webrtc.org6141e132013-04-09 10:09:10 +000050 int32_t SetAudioProcessingModule(
niklase@google.com470e71d2011-07-07 08:21:25 +000051 AudioProcessing* audioProcessingModule);
52
pbos@webrtc.org6141e132013-04-09 10:09:10 +000053 int32_t PrepareDemux(const void* audioSamples,
pbos@webrtc.org92135212013-05-14 08:31:39 +000054 uint32_t nSamples,
55 uint8_t nChannels,
56 uint32_t samplesPerSec,
57 uint16_t totalDelayMS,
58 int32_t clockDrift,
59 uint16_t currentMicLevel,
60 bool keyPressed);
niklase@google.com470e71d2011-07-07 08:21:25 +000061
62
pbos@webrtc.org6141e132013-04-09 10:09:10 +000063 int32_t DemuxAndMix();
xians@webrtc.org2f84afa2013-07-31 16:23:37 +000064 // Used by the Chrome to pass the recording data to the specific VoE
65 // channels for demux.
xians@webrtc.org8fff1f02013-07-31 16:27:42 +000066 void DemuxAndMix(const int voe_channels[], int number_of_voe_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000067
pbos@webrtc.org6141e132013-04-09 10:09:10 +000068 int32_t EncodeAndSend();
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 encoding and sending to the network.
xians@webrtc.org8fff1f02013-07-31 16:27:42 +000071 void EncodeAndSend(const int voe_channels[], int number_of_voe_channels);
niklase@google.com470e71d2011-07-07 08:21:25 +000072
andrew@webrtc.org023cc5a2014-01-11 01:25:53 +000073 // Must be called on the same thread as PrepareDemux().
pbos@webrtc.org6141e132013-04-09 10:09:10 +000074 uint32_t CaptureLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000075
pbos@webrtc.org6141e132013-04-09 10:09:10 +000076 int32_t StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +000077
niklase@google.com470e71d2011-07-07 08:21:25 +000078 // VoEDtmf
pbos@webrtc.org92135212013-05-14 08:31:39 +000079 void UpdateMuteMicrophoneTime(uint32_t lengthMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000080
81 // VoEExternalMedia
andrew@webrtc.org21ab3ba2012-10-19 17:30:56 +000082 int RegisterExternalMediaProcessing(VoEMediaProcess* object,
83 ProcessingTypes type);
84 int DeRegisterExternalMediaProcessing(ProcessingTypes type);
niklase@google.com470e71d2011-07-07 08:21:25 +000085
xians@google.com0b0665a2011-08-08 08:18:44 +000086 int GetMixingFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +000087
88 // VoEVolumeControl
pbos@webrtc.org92135212013-05-14 08:31:39 +000089 int SetMute(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000090
91 bool Mute() const;
92
pbos@webrtc.org6141e132013-04-09 10:09:10 +000093 int8_t AudioLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000094
pbos@webrtc.org6141e132013-04-09 10:09:10 +000095 int16_t AudioLevelFullRange() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000096
97 bool IsRecordingCall();
98
99 bool IsRecordingMic();
100
101 int StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000102 bool loop,
103 FileFormats format,
104 int startPosition,
105 float volumeScaling,
106 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 const CodecInst* codecInst);
108
109 int StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000110 FileFormats format,
111 int startPosition,
112 float volumeScaling,
113 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +0000114 const CodecInst* codecInst);
115
116 int StopPlayingFileAsMicrophone();
117
118 int IsPlayingFileAsMicrophone() const;
119
pbos@webrtc.org92135212013-05-14 08:31:39 +0000120 int ScaleFileAsMicrophonePlayout(float scale);
niklase@google.com470e71d2011-07-07 08:21:25 +0000121
122 int StartRecordingMicrophone(const char* fileName,
123 const CodecInst* codecInst);
124
125 int StartRecordingMicrophone(OutStream* stream,
126 const CodecInst* codecInst);
127
128 int StopRecordingMicrophone();
129
130 int StartRecordingCall(const char* fileName, const CodecInst* codecInst);
131
132 int StartRecordingCall(OutStream* stream, const CodecInst* codecInst);
133
134 int StopRecordingCall();
135
136 void SetMixWithMicStatus(bool mix);
137
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000138 int32_t RegisterVoiceEngineObserver(VoiceEngineObserver& observer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000139
140 virtual ~TransmitMixer();
141
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000142 // MonitorObserver
niklase@google.com470e71d2011-07-07 08:21:25 +0000143 void OnPeriodicProcess();
144
145
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000146 // FileCallback
pbos@webrtc.org92135212013-05-14 08:31:39 +0000147 void PlayNotification(int32_t id,
148 uint32_t durationMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000149
pbos@webrtc.org92135212013-05-14 08:31:39 +0000150 void RecordNotification(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 PlayFileEnded(int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
pbos@webrtc.org92135212013-05-14 08:31:39 +0000155 void RecordFileEnded(int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000156
niklas.enbom@webrtc.org3dc88652012-03-30 09:53:54 +0000157#ifdef WEBRTC_VOICE_ENGINE_TYPING_DETECTION
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000158 // Typing detection
niklas.enbom@webrtc.org3dc88652012-03-30 09:53:54 +0000159 int TimeSinceLastTyping(int &seconds);
niklas.enbom@webrtc.org06e722a2012-04-04 07:44:27 +0000160 int SetTypingDetectionParameters(int timeWindow,
161 int costPerTyping,
162 int reportingThreshold,
niklas.enbom@webrtc.orgf6edfef2012-05-09 13:16:12 +0000163 int penaltyDecay,
164 int typeEventDelay);
niklas.enbom@webrtc.org3dc88652012-03-30 09:53:54 +0000165#endif
166
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000167 void EnableStereoChannelSwapping(bool enable);
168 bool IsStereoChannelSwappingEnabled();
169
niklase@google.com470e71d2011-07-07 08:21:25 +0000170private:
pbos@webrtc.org92135212013-05-14 08:31:39 +0000171 TransmitMixer(uint32_t instanceId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000172
andrew@webrtc.org24120852013-03-02 00:14:46 +0000173 // Gets the maximum sample rate and number of channels over all currently
174 // sending codecs.
175 void GetSendCodecInfo(int* max_sample_rate, int* max_channels);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000176
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000177 void GenerateAudioFrame(const int16_t audioSamples[],
178 int nSamples,
179 int nChannels,
180 int samplesPerSec);
pbos@webrtc.org92135212013-05-14 08:31:39 +0000181 int32_t RecordAudioToFile(uint32_t mixingFrequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000182
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000183 int32_t MixOrReplaceAudioWithFile(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000184 int mixingFrequency);
niklase@google.com470e71d2011-07-07 08:21:25 +0000185
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000186 void ProcessAudio(int delay_ms, int clock_drift, int current_mic_level,
187 bool key_pressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000188
189#ifdef WEBRTC_VOICE_ENGINE_TYPING_DETECTION
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000190 void TypingDetection(bool keyPressed);
niklase@google.com470e71d2011-07-07 08:21:25 +0000191#endif
192
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000193 // uses
niklase@google.com470e71d2011-07-07 08:21:25 +0000194 Statistics* _engineStatisticsPtr;
195 ChannelManager* _channelManagerPtr;
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000196 AudioProcessing* audioproc_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000197 VoiceEngineObserver* _voiceEngineObserverPtr;
198 ProcessThread* _processThreadPtr;
199
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000200 // owns
niklase@google.com470e71d2011-07-07 08:21:25 +0000201 MonitorModule _monitorModule;
202 AudioFrame _audioFrame;
andrew@webrtc.org28e82bf2013-05-02 00:30:36 +0000203 PushResampler resampler_; // ADM sample rate -> mixing rate
andrew@webrtc.org21ab3ba2012-10-19 17:30:56 +0000204 FilePlayer* _filePlayerPtr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205 FileRecorder* _fileRecorderPtr;
206 FileRecorder* _fileCallRecorderPtr;
xians@google.com0b0665a2011-08-08 08:18:44 +0000207 int _filePlayerId;
208 int _fileRecorderId;
209 int _fileCallRecorderId;
niklase@google.com470e71d2011-07-07 08:21:25 +0000210 bool _filePlaying;
211 bool _fileRecording;
212 bool _fileCallRecording;
213 voe::AudioLevel _audioLevel;
214 // protect file instances and their variables in MixedParticipants()
215 CriticalSectionWrapper& _critSect;
216 CriticalSectionWrapper& _callbackCritSect;
217
218#ifdef WEBRTC_VOICE_ENGINE_TYPING_DETECTION
henrikg@webrtc.orgc6937042014-01-30 09:50:46 +0000219 webrtc::TypingDetection _typingDetection;
jiayl@webrtc.orgbf007402013-09-17 18:09:20 +0000220 bool _typingNoiseWarningPending;
221 bool _typingNoiseDetected;
niklase@google.com470e71d2011-07-07 08:21:25 +0000222#endif
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000223 bool _saturationWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224
xians@google.com0b0665a2011-08-08 08:18:44 +0000225 int _instanceId;
niklase@google.com470e71d2011-07-07 08:21:25 +0000226 bool _mixFileWithMicrophone;
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000227 uint32_t _captureLevel;
andrew@webrtc.org21ab3ba2012-10-19 17:30:56 +0000228 VoEMediaProcess* external_postproc_ptr_;
229 VoEMediaProcess* external_preproc_ptr_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000230 bool _mute;
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000231 int32_t _remainingMuteMicTimeMs;
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000232 bool stereo_codec_;
andrew@webrtc.org02d71742012-04-24 19:47:00 +0000233 bool swap_stereo_channels_;
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000234 scoped_ptr<int16_t[]> mono_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000235};
236
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000237} // namespace voe
niklase@google.com470e71d2011-07-07 08:21:25 +0000238
239} // namespace webrtc
braveyao@webrtc.orga7cfa672013-12-24 03:39:10 +0000240
braveyao@webrtc.org0062a6d2013-12-24 03:58:51 +0000241#endif // WEBRTC_VOICE_ENGINE_TRANSMIT_MIXER_H