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.
5 #ifndef MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_
6 #define MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_
8 #include <SLES/OpenSLES.h>
9 #include <SLES/OpenSLES_Android.h>
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"
21 class AudioManagerAndroid;
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 {
29 static const int kMaxNumOfBuffersInQueue = 2;
31 OpenSLESInputStream(AudioManagerAndroid* manager,
32 const AudioParameters& params);
34 virtual ~OpenSLESInputStream();
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;
48 bool CreateRecorder();
50 // Called from OpenSLES specific audio worker thread.
51 static void SimpleBufferQueueCallback(
52 SLAndroidSimpleBufferQueueItf buffer_queue,
55 // Called from OpenSLES specific audio worker thread.
56 void ReadBufferQueue();
59 void SetupAudioBuffer();
62 void ReleaseAudioBuffer();
64 // If OpenSLES reports an error this function handles it and passes it to
65 // the attached AudioInputCallback::OnError().
66 void HandleError(SLresult error);
68 base::ThreadChecker thread_checker_;
70 // Protects |callback_|, |active_buffer_index_|, |audio_data_|,
71 // |buffer_size_bytes_| and |simple_buffer_queue_|.
74 AudioManagerAndroid* audio_manager_;
76 AudioInputCallback* callback_;
78 // Shared engine interfaces for the app.
79 media::ScopedSLObjectItf recorder_object_;
80 media::ScopedSLObjectItf engine_object_;
82 SLRecordItf recorder_;
84 // Buffer queue recorder interface.
85 SLAndroidSimpleBufferQueueItf simple_buffer_queue_;
87 SLDataFormat_PCM format_;
89 // Audio buffers that are allocated in the constructor based on
90 // info from audio parameters.
91 uint8* audio_data_[kMaxNumOfBuffersInQueue];
93 int active_buffer_index_;
94 int buffer_size_bytes_;
98 scoped_ptr<media::AudioBus> audio_bus_;
100 DISALLOW_COPY_AND_ASSIGN(OpenSLESInputStream);
105 #endif // MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_