Home | History | Annotate | Download | only in android
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef MEDIA_AUDIO_ANDROID_AUDIO_RECORD_INPUT_H_
      6 #define MEDIA_AUDIO_ANDROID_AUDIO_RECORD_INPUT_H_
      7 
      8 #include "base/android/jni_android.h"
      9 #include "base/threading/thread_checker.h"
     10 #include "media/audio/audio_io.h"
     11 #include "media/audio/audio_parameters.h"
     12 
     13 namespace media {
     14 
     15 class AudioBus;
     16 class AudioManagerAndroid;
     17 
     18 // Implements PCM audio input support for Android using the Java AudioRecord
     19 // interface. Most of the work is done by its Java counterpart in
     20 // AudioRecordInput.java. This class is created and lives on the Audio Manager
     21 // thread but recorded audio buffers are delivered on a thread managed by
     22 // the Java class.
     23 //
     24 // The Java class makes use of AudioEffect features which are first available
     25 // in Jelly Bean. It should not be instantiated running against earlier SDKs.
     26 class MEDIA_EXPORT AudioRecordInputStream : public AudioInputStream {
     27  public:
     28   AudioRecordInputStream(AudioManagerAndroid* manager,
     29                          const AudioParameters& params);
     30 
     31   virtual ~AudioRecordInputStream();
     32 
     33   // Implementation of AudioInputStream.
     34   virtual bool Open() OVERRIDE;
     35   virtual void Start(AudioInputCallback* callback) OVERRIDE;
     36   virtual void Stop() OVERRIDE;
     37   virtual void Close() OVERRIDE;
     38   virtual double GetMaxVolume() OVERRIDE;
     39   virtual void SetVolume(double volume) OVERRIDE;
     40   virtual double GetVolume() OVERRIDE;
     41   virtual void SetAutomaticGainControl(bool enabled) OVERRIDE;
     42   virtual bool GetAutomaticGainControl() OVERRIDE;
     43 
     44   static bool RegisterAudioRecordInput(JNIEnv* env);
     45 
     46   // Called from Java when data is available.
     47   void OnData(JNIEnv* env, jobject obj, jint size, jint hardware_delay_bytes);
     48 
     49   // Called from Java so that we can cache the address of the Java-managed
     50   // |byte_buffer| in |direct_buffer_address_|.
     51   void CacheDirectBufferAddress(JNIEnv* env, jobject obj, jobject byte_buffer);
     52 
     53  private:
     54   base::ThreadChecker thread_checker_;
     55   AudioManagerAndroid* audio_manager_;
     56 
     57   // Java AudioRecordInput instance.
     58   base::android::ScopedJavaGlobalRef<jobject> j_audio_record_;
     59 
     60   // This is the only member accessed by both the Audio Manager and Java
     61   // threads. Explanations for why we do not require explicit synchronization
     62   // are given in the implementation.
     63   AudioInputCallback* callback_;
     64 
     65   // Owned by j_audio_record_.
     66   uint8* direct_buffer_address_;
     67 
     68   scoped_ptr<media::AudioBus> audio_bus_;
     69   int bytes_per_sample_;
     70 
     71   DISALLOW_COPY_AND_ASSIGN(AudioRecordInputStream);
     72 };
     73 
     74 }  // namespace media
     75 
     76 #endif  // MEDIA_AUDIO_ANDROID_AUDIO_RECORD_INPUT_H_
     77