Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / voice_engine / transmit_mixer.h
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
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
14 #include "webrtc/common_audio/resampler/include/push_resampler.h"
15 #include "webrtc/common_types.h"
16 #include "webrtc/modules/audio_processing/typing_detection.h"
17 #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"
20 #include "webrtc/voice_engine/include/voe_base.h"
21 #include "webrtc/voice_engine/level_indicator.h"
22 #include "webrtc/voice_engine/monitor_module.h"
23 #include "webrtc/voice_engine/voice_engine_defines.h"
24
25 namespace webrtc {
26
27 class AudioProcessing;
28 class ProcessThread;
29 class VoEExternalMedia;
30 class VoEMediaProcess;
31
32 namespace voe {
33
34 class ChannelManager;
35 class MixedAudio;
36 class Statistics;
37
38 class TransmitMixer : public MonitorObserver,
39                       public FileCallback
40
41 {
42 public:
43     static int32_t Create(TransmitMixer*& mixer, uint32_t instanceId);
44
45     static void Destroy(TransmitMixer*& mixer);
46
47     int32_t SetEngineInformation(ProcessThread& processThread,
48                                  Statistics& engineStatistics,
49                                  ChannelManager& channelManager);
50
51     int32_t SetAudioProcessingModule(
52         AudioProcessing* audioProcessingModule);
53
54     int32_t PrepareDemux(const void* audioSamples,
55                          uint32_t nSamples,
56                          uint8_t  nChannels,
57                          uint32_t samplesPerSec,
58                          uint16_t totalDelayMS,
59                          int32_t  clockDrift,
60                          uint16_t currentMicLevel,
61                          bool keyPressed);
62
63
64     int32_t DemuxAndMix();
65     // Used by the Chrome to pass the recording data to the specific VoE
66     // channels for demux.
67     void DemuxAndMix(const int voe_channels[], int number_of_voe_channels);
68
69     int32_t EncodeAndSend();
70     // Used by the Chrome to pass the recording data to the specific VoE
71     // channels for encoding and sending to the network.
72     void EncodeAndSend(const int voe_channels[], int number_of_voe_channels);
73
74     // Must be called on the same thread as PrepareDemux().
75     uint32_t CaptureLevel() const;
76
77     int32_t StopSend();
78
79     // VoEDtmf
80     void UpdateMuteMicrophoneTime(uint32_t lengthMs);
81
82     // VoEExternalMedia
83     int RegisterExternalMediaProcessing(VoEMediaProcess* object,
84                                         ProcessingTypes type);
85     int DeRegisterExternalMediaProcessing(ProcessingTypes type);
86
87     int GetMixingFrequency();
88
89     // VoEVolumeControl
90     int SetMute(bool enable);
91
92     bool Mute() const;
93
94     int8_t AudioLevel() const;
95
96     int16_t AudioLevelFullRange() const;
97
98     bool IsRecordingCall();
99
100     bool IsRecordingMic();
101
102     int StartPlayingFileAsMicrophone(const char* fileName,
103                                      bool loop,
104                                      FileFormats format,
105                                      int startPosition,
106                                      float volumeScaling,
107                                      int stopPosition,
108                                      const CodecInst* codecInst);
109
110     int StartPlayingFileAsMicrophone(InStream* stream,
111                                      FileFormats format,
112                                      int startPosition,
113                                      float volumeScaling,
114                                      int stopPosition,
115                                      const CodecInst* codecInst);
116
117     int StopPlayingFileAsMicrophone();
118
119     int IsPlayingFileAsMicrophone() const;
120
121     int ScaleFileAsMicrophonePlayout(float scale);
122
123     int StartRecordingMicrophone(const char* fileName,
124                                  const CodecInst* codecInst);
125
126     int StartRecordingMicrophone(OutStream* stream,
127                                  const CodecInst* codecInst);
128
129     int StopRecordingMicrophone();
130
131     int StartRecordingCall(const char* fileName, const CodecInst* codecInst);
132
133     int StartRecordingCall(OutStream* stream, const CodecInst* codecInst);
134
135     int StopRecordingCall();
136
137     void SetMixWithMicStatus(bool mix);
138
139     int32_t RegisterVoiceEngineObserver(VoiceEngineObserver& observer);
140
141     virtual ~TransmitMixer();
142
143     // MonitorObserver
144     void OnPeriodicProcess();
145
146
147     // FileCallback
148     void PlayNotification(int32_t id,
149                           uint32_t durationMs);
150
151     void RecordNotification(int32_t id,
152                             uint32_t durationMs);
153
154     void PlayFileEnded(int32_t id);
155
156     void RecordFileEnded(int32_t id);
157
158 #ifdef WEBRTC_VOICE_ENGINE_TYPING_DETECTION
159     // Typing detection
160     int TimeSinceLastTyping(int &seconds);
161     int SetTypingDetectionParameters(int timeWindow,
162                                      int costPerTyping,
163                                      int reportingThreshold,
164                                      int penaltyDecay,
165                                      int typeEventDelay);
166 #endif
167
168   void EnableStereoChannelSwapping(bool enable);
169   bool IsStereoChannelSwappingEnabled();
170
171 private:
172     TransmitMixer(uint32_t instanceId);
173
174     // Gets the maximum sample rate and number of channels over all currently
175     // sending codecs.
176     void GetSendCodecInfo(int* max_sample_rate, int* max_channels);
177
178     int GenerateAudioFrame(const int16_t audioSamples[],
179                            int nSamples,
180                            int nChannels,
181                            int samplesPerSec);
182     int32_t RecordAudioToFile(uint32_t mixingFrequency);
183
184     int32_t MixOrReplaceAudioWithFile(
185         int mixingFrequency);
186
187     void ProcessAudio(int delay_ms, int clock_drift, int current_mic_level,
188                       bool key_pressed);
189
190 #ifdef WEBRTC_VOICE_ENGINE_TYPING_DETECTION
191     void TypingDetection(bool keyPressed);
192 #endif
193
194     // uses
195     Statistics* _engineStatisticsPtr;
196     ChannelManager* _channelManagerPtr;
197     AudioProcessing* audioproc_;
198     VoiceEngineObserver* _voiceEngineObserverPtr;
199     ProcessThread* _processThreadPtr;
200
201     // owns
202     MonitorModule _monitorModule;
203     AudioFrame _audioFrame;
204     PushResampler resampler_;  // ADM sample rate -> mixing rate
205     FilePlayer* _filePlayerPtr;
206     FileRecorder* _fileRecorderPtr;
207     FileRecorder* _fileCallRecorderPtr;
208     int _filePlayerId;
209     int _fileRecorderId;
210     int _fileCallRecorderId;
211     bool _filePlaying;
212     bool _fileRecording;
213     bool _fileCallRecording;
214     voe::AudioLevel _audioLevel;
215     // protect file instances and their variables in MixedParticipants()
216     CriticalSectionWrapper& _critSect;
217     CriticalSectionWrapper& _callbackCritSect;
218
219 #ifdef WEBRTC_VOICE_ENGINE_TYPING_DETECTION
220     webrtc::TypingDetection _typingDetection;
221     bool _typingNoiseWarningPending;
222     bool _typingNoiseDetected;
223 #endif
224     bool _saturationWarning;
225
226     int _instanceId;
227     bool _mixFileWithMicrophone;
228     uint32_t _captureLevel;
229     VoEMediaProcess* external_postproc_ptr_;
230     VoEMediaProcess* external_preproc_ptr_;
231     bool _mute;
232     int32_t _remainingMuteMicTimeMs;
233     bool stereo_codec_;
234     bool swap_stereo_channels_;
235 };
236
237 }  // namespace voe
238
239 }  // namespace webrtc
240
241 #endif  // WEBRTC_VOICE_ENGINE_TRANSMIT_MIXER_H