Home | History | Annotate | Download | only in android
      1 // Copyright (c) 2012 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_OPENSLES_INPUT_H_
      6 #define MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_
      7 
      8 #include <SLES/OpenSLES.h>
      9 #include <SLES/OpenSLES_Android.h>
     10 
     11 #include "base/compiler_specific.h"
     12 #include "base/synchronization/lock.h"
     13 #include "base/threading/thread_checker.h"
     14 #include "media/audio/android/opensles_util.h"
     15 #include "media/audio/audio_io.h"
     16 #include "media/audio/audio_parameters.h"
     17 
     18 namespace media {
     19 
     20 class AudioBus;
     21 class AudioManagerAndroid;
     22 
     23 // Implements PCM audio input support for Android using the OpenSLES API.
     24 // This class is created and lives on the Audio Manager thread but recorded
     25 // audio buffers are delivered on an internal OpenSLES audio thread. All public
     26 // methods should be called on the Audio Manager thread.
     27 class OpenSLESInputStream : public AudioInputStream {
     28  public:
     29   static const int kMaxNumOfBuffersInQueue = 2;
     30 
     31   OpenSLESInputStream(AudioManagerAndroid* manager,
     32                       const AudioParameters& params);
     33 
     34   virtual ~OpenSLESInputStream();
     35 
     36   // Implementation of AudioInputStream.
     37   virtual bool Open() OVERRIDE;
     38   virtual void Start(AudioInputCallback* callback) OVERRIDE;
     39   virtual void Stop() OVERRIDE;
     40   virtual void Close() OVERRIDE;
     41   virtual double GetMaxVolume() OVERRIDE;
     42   virtual void SetVolume(double volume) OVERRIDE;
     43   virtual double GetVolume() OVERRIDE;
     44   virtual void SetAutomaticGainControl(bool enabled) OVERRIDE;
     45   virtual bool GetAutomaticGainControl() OVERRIDE;
     46   virtual bool IsMuted() OVERRIDE;
     47 
     48  private:
     49   bool CreateRecorder();
     50 
     51   // Called from OpenSLES specific audio worker thread.
     52   static void SimpleBufferQueueCallback(
     53       SLAndroidSimpleBufferQueueItf buffer_queue,
     54       void* instance);
     55 
     56   // Called from OpenSLES specific audio worker thread.
     57   void ReadBufferQueue();
     58 
     59   // Called in Open();
     60   void SetupAudioBuffer();
     61 
     62   // Called in Close();
     63   void ReleaseAudioBuffer();
     64 
     65   // If OpenSLES reports an error this function handles it and passes it to
     66   // the attached AudioInputCallback::OnError().
     67   void HandleError(SLresult error);
     68 
     69   base::ThreadChecker thread_checker_;
     70 
     71   // Protects |callback_|, |active_buffer_index_|, |audio_data_|,
     72   // |buffer_size_bytes_| and |simple_buffer_queue_|.
     73   base::Lock lock_;
     74 
     75   AudioManagerAndroid* audio_manager_;
     76 
     77   AudioInputCallback* callback_;
     78 
     79   // Shared engine interfaces for the app.
     80   media::ScopedSLObjectItf recorder_object_;
     81   media::ScopedSLObjectItf engine_object_;
     82 
     83   SLRecordItf recorder_;
     84 
     85   // Buffer queue recorder interface.
     86   SLAndroidSimpleBufferQueueItf simple_buffer_queue_;
     87 
     88   SLDataFormat_PCM format_;
     89 
     90   // Audio buffers that are allocated in the constructor based on
     91   // info from audio parameters.
     92   uint8* audio_data_[kMaxNumOfBuffersInQueue];
     93 
     94   int active_buffer_index_;
     95   int buffer_size_bytes_;
     96 
     97   bool started_;
     98 
     99   scoped_ptr<media::AudioBus> audio_bus_;
    100 
    101   DISALLOW_COPY_AND_ASSIGN(OpenSLESInputStream);
    102 };
    103 
    104 }  // namespace media
    105 
    106 #endif  // MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_
    107