Home | History | Annotate | Download | only in android
      1 /*
      2  *  Copyright (c) 2013 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_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_RECORD_JNI_H_
     12 #define WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_RECORD_JNI_H_
     13 
     14 #include <jni.h>
     15 
     16 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
     17 #include "webrtc/modules/audio_device/include/audio_device_defines.h"
     18 #include "webrtc/modules/audio_device/audio_device_generic.h"
     19 
     20 namespace webrtc {
     21 
     22 class EventWrapper;
     23 class ThreadWrapper;
     24 class PlayoutDelayProvider;
     25 
     26 const uint32_t N_REC_SAMPLES_PER_SEC = 16000; // Default is 16 kHz
     27 const uint32_t N_REC_CHANNELS = 1; // default is mono recording
     28 const uint32_t REC_BUF_SIZE_IN_SAMPLES = 480; // Handle max 10 ms @ 48 kHz
     29 
     30 class AudioRecordJni {
     31  public:
     32   static int32_t SetAndroidAudioDeviceObjects(void* javaVM, void* env,
     33                                               void* context);
     34   static void ClearAndroidAudioDeviceObjects();
     35 
     36   AudioRecordJni(const int32_t id, PlayoutDelayProvider* delay_provider);
     37   ~AudioRecordJni();
     38 
     39   // Main initializaton and termination
     40   int32_t Init();
     41   int32_t Terminate();
     42   bool Initialized() const { return _initialized; }
     43 
     44   // Device enumeration
     45   int16_t RecordingDevices() { return 1; }  // There is one device only
     46   int32_t RecordingDeviceName(uint16_t index,
     47                               char name[kAdmMaxDeviceNameSize],
     48                               char guid[kAdmMaxGuidSize]);
     49 
     50   // Device selection
     51   int32_t SetRecordingDevice(uint16_t index);
     52   int32_t SetRecordingDevice(
     53       AudioDeviceModule::WindowsDeviceType device);
     54 
     55   // Audio transport initialization
     56   int32_t RecordingIsAvailable(bool& available);  // NOLINT
     57   int32_t InitRecording();
     58   bool RecordingIsInitialized() const { return _recIsInitialized; }
     59 
     60   // Audio transport control
     61   int32_t StartRecording();
     62   int32_t StopRecording();
     63   bool Recording() const { return _recording; }
     64 
     65   // Microphone Automatic Gain Control (AGC)
     66   int32_t SetAGC(bool enable);
     67   bool AGC() const { return _AGC; }
     68 
     69   // Audio mixer initialization
     70   int32_t InitMicrophone();
     71   bool MicrophoneIsInitialized() const { return _micIsInitialized; }
     72 
     73   // Microphone volume controls
     74   int32_t MicrophoneVolumeIsAvailable(bool& available);  // NOLINT
     75   // TODO(leozwang): Add microphone volume control when OpenSL APIs
     76   // are available.
     77   int32_t SetMicrophoneVolume(uint32_t volume);
     78   int32_t MicrophoneVolume(uint32_t& volume) const;  // NOLINT
     79   int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const;  // NOLINT
     80   int32_t MinMicrophoneVolume(uint32_t& minVolume) const;  // NOLINT
     81   int32_t MicrophoneVolumeStepSize(
     82       uint16_t& stepSize) const;  // NOLINT
     83 
     84   // Microphone mute control
     85   int32_t MicrophoneMuteIsAvailable(bool& available);  // NOLINT
     86   int32_t SetMicrophoneMute(bool enable);
     87   int32_t MicrophoneMute(bool& enabled) const;  // NOLINT
     88 
     89   // Microphone boost control
     90   int32_t MicrophoneBoostIsAvailable(bool& available);  // NOLINT
     91   int32_t SetMicrophoneBoost(bool enable);
     92   int32_t MicrophoneBoost(bool& enabled) const;  // NOLINT
     93 
     94   // Stereo support
     95   int32_t StereoRecordingIsAvailable(bool& available);  // NOLINT
     96   int32_t SetStereoRecording(bool enable);
     97   int32_t StereoRecording(bool& enabled) const;  // NOLINT
     98 
     99   // Delay information and control
    100   int32_t RecordingDelay(uint16_t& delayMS) const;  // NOLINT
    101 
    102   bool RecordingWarning() const;
    103   bool RecordingError() const;
    104   void ClearRecordingWarning();
    105   void ClearRecordingError();
    106 
    107   // Attach audio buffer
    108   void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer);
    109 
    110   int32_t SetRecordingSampleRate(const uint32_t samplesPerSec);
    111 
    112  private:
    113   void Lock() EXCLUSIVE_LOCK_FUNCTION(_critSect) {
    114     _critSect.Enter();
    115   }
    116   void UnLock() UNLOCK_FUNCTION(_critSect) {
    117     _critSect.Leave();
    118   }
    119 
    120   int32_t InitJavaResources();
    121   int32_t InitSampleRate();
    122 
    123   static bool RecThreadFunc(void*);
    124   bool RecThreadProcess();
    125 
    126   // TODO(leozwang): Android holds only one JVM, all these jni handling
    127   // will be consolidated into a single place to make it consistant and
    128   // reliable. Chromium has a good example at base/android.
    129   static JavaVM* globalJvm;
    130   static JNIEnv* globalJNIEnv;
    131   static jobject globalContext;
    132   static jclass globalScClass;
    133 
    134   JavaVM* _javaVM; // denotes a Java VM
    135   JNIEnv* _jniEnvRec; // The JNI env for recording thread
    136   jclass _javaScClass; // AudioDeviceAndroid class
    137   jobject _javaScObj; // AudioDeviceAndroid object
    138   jobject _javaRecBuffer;
    139   void* _javaDirectRecBuffer; // Direct buffer pointer to rec buffer
    140   jmethodID _javaMidRecAudio; // Method ID of rec in AudioDeviceAndroid
    141 
    142   AudioDeviceBuffer* _ptrAudioBuffer;
    143   CriticalSectionWrapper& _critSect;
    144   int32_t _id;
    145   PlayoutDelayProvider* _delay_provider;
    146   bool _initialized;
    147 
    148   EventWrapper& _timeEventRec;
    149   EventWrapper& _recStartStopEvent;
    150   ThreadWrapper* _ptrThreadRec;
    151   uint32_t _recThreadID;
    152   bool _recThreadIsInitialized;
    153   bool _shutdownRecThread;
    154 
    155   int8_t _recBuffer[2 * REC_BUF_SIZE_IN_SAMPLES];
    156   bool _recordingDeviceIsSpecified;
    157 
    158   bool _recording;
    159   bool _recIsInitialized;
    160   bool _micIsInitialized;
    161 
    162   bool _startRec;
    163 
    164   uint16_t _recWarning;
    165   uint16_t _recError;
    166 
    167   uint16_t _delayRecording;
    168 
    169   bool _AGC;
    170 
    171   uint16_t _samplingFreqIn; // Sampling frequency for Mic
    172   int _recAudioSource;
    173 
    174 };
    175 
    176 }  // namespace webrtc
    177 
    178 #endif  // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_RECORD_JNI_H_
    179