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"
20 class AudioManagerAndroid
;
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 delivered on an internal OpenSLES audio thread. All public
25 // methods should be called on the Audio Manager thread.
26 class OpenSLESInputStream
: public AudioInputStream
{
28 static const int kMaxNumOfBuffersInQueue
= 2;
30 OpenSLESInputStream(AudioManagerAndroid
* manager
,
31 const AudioParameters
& params
);
33 virtual ~OpenSLESInputStream();
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
;
47 bool CreateRecorder();
49 // Called from OpenSLES specific audio worker thread.
50 static void SimpleBufferQueueCallback(
51 SLAndroidSimpleBufferQueueItf buffer_queue
,
54 // Called from OpenSLES specific audio worker thread.
55 void ReadBufferQueue();
58 void SetupAudioBuffer();
61 void ReleaseAudioBuffer();
63 // If OpenSLES reports an error this function handles it and passes it to
64 // the attached AudioInputCallback::OnError().
65 void HandleError(SLresult error
);
67 base::ThreadChecker thread_checker_
;
69 // Protects |callback_|, |active_buffer_index_|, |audio_data_|,
70 // |buffer_size_bytes_| and |simple_buffer_queue_|.
73 AudioManagerAndroid
* audio_manager_
;
75 AudioInputCallback
* callback_
;
77 // Shared engine interfaces for the app.
78 media::ScopedSLObjectItf recorder_object_
;
79 media::ScopedSLObjectItf engine_object_
;
81 SLRecordItf recorder_
;
83 // Buffer queue recorder interface.
84 SLAndroidSimpleBufferQueueItf simple_buffer_queue_
;
86 SLDataFormat_PCM format_
;
88 // Audio buffers that are allocated in the constructor based on
89 // info from audio parameters.
90 uint8
* audio_data_
[kMaxNumOfBuffersInQueue
];
92 int active_buffer_index_
;
93 int buffer_size_bytes_
;
97 DISALLOW_COPY_AND_ASSIGN(OpenSLESInputStream
);
102 #endif // MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_