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   virtual bool IsMuted() OVERRIDE;
     44 
     45   static bool RegisterAudioRecordInput(JNIEnv* env);
     46 
     47   // Called from Java when data is available.
     48   void OnData(JNIEnv* env, jobject obj, jint size, jint hardware_delay_bytes);
     49 
     50   // Called from Java so that we can cache the address of the Java-managed
     51   // |byte_buffer| in |direct_buffer_address_|.
     52   void CacheDirectBufferAddress(JNIEnv* env, jobject obj, jobject byte_buffer);
     53 
     54  private:
     55   base::ThreadChecker thread_checker_;
     56   AudioManagerAndroid* audio_manager_;
     57 
     58   // Java AudioRecordInput instance.
     59   base::android::ScopedJavaGlobalRef<jobject> j_audio_record_;
     60 
     61   // This is the only member accessed by both the Audio Manager and Java
     62   // threads. Explanations for why we do not require explicit synchronization
     63   // are given in the implementation.
     64   AudioInputCallback* callback_;
     65 
     66   // Owned by j_audio_record_.
     67   uint8* direct_buffer_address_;
     68 
     69   scoped_ptr<media::AudioBus> audio_bus_;
     70   int bytes_per_sample_;
     71 
     72   DISALLOW_COPY_AND_ASSIGN(AudioRecordInputStream);
     73 };
     74 
     75 }  // namespace media
     76 
     77 #endif  // MEDIA_AUDIO_ANDROID_AUDIO_RECORD_INPUT_H_
     78