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