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 47 private: 48 bool CreateRecorder(); 49 50 // Called from OpenSLES specific audio worker thread. 51 static void SimpleBufferQueueCallback( 52 SLAndroidSimpleBufferQueueItf buffer_queue, 53 void* instance); 54 55 // Called from OpenSLES specific audio worker thread. 56 void ReadBufferQueue(); 57 58 // Called in Open(); 59 void SetupAudioBuffer(); 60 61 // Called in Close(); 62 void ReleaseAudioBuffer(); 63 64 // If OpenSLES reports an error this function handles it and passes it to 65 // the attached AudioInputCallback::OnError(). 66 void HandleError(SLresult error); 67 68 base::ThreadChecker thread_checker_; 69 70 // Protects |callback_|, |active_buffer_index_|, |audio_data_|, 71 // |buffer_size_bytes_| and |simple_buffer_queue_|. 72 base::Lock lock_; 73 74 AudioManagerAndroid* audio_manager_; 75 76 AudioInputCallback* callback_; 77 78 // Shared engine interfaces for the app. 79 media::ScopedSLObjectItf recorder_object_; 80 media::ScopedSLObjectItf engine_object_; 81 82 SLRecordItf recorder_; 83 84 // Buffer queue recorder interface. 85 SLAndroidSimpleBufferQueueItf simple_buffer_queue_; 86 87 SLDataFormat_PCM format_; 88 89 // Audio buffers that are allocated in the constructor based on 90 // info from audio parameters. 91 uint8* audio_data_[kMaxNumOfBuffersInQueue]; 92 93 int active_buffer_index_; 94 int buffer_size_bytes_; 95 96 bool started_; 97 98 scoped_ptr<media::AudioBus> audio_bus_; 99 100 DISALLOW_COPY_AND_ASSIGN(OpenSLESInputStream); 101 }; 102 103 } // namespace media 104 105 #endif // MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_ 106