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 bool SetAutomaticGainControl(bool enabled
) override
;
45 virtual bool GetAutomaticGainControl() override
;
46 virtual bool IsMuted() override
;
49 bool CreateRecorder();
51 // Called from OpenSLES specific audio worker thread.
52 static void SimpleBufferQueueCallback(
53 SLAndroidSimpleBufferQueueItf buffer_queue
,
56 // Called from OpenSLES specific audio worker thread.
57 void ReadBufferQueue();
60 void SetupAudioBuffer();
63 void ReleaseAudioBuffer();
65 // If OpenSLES reports an error this function handles it and passes it to
66 // the attached AudioInputCallback::OnError().
67 void HandleError(SLresult error
);
69 base::ThreadChecker thread_checker_
;
71 // Protects |callback_|, |active_buffer_index_|, |audio_data_|,
72 // |buffer_size_bytes_| and |simple_buffer_queue_|.
75 AudioManagerAndroid
* audio_manager_
;
77 AudioInputCallback
* callback_
;
79 // Shared engine interfaces for the app.
80 media::ScopedSLObjectItf recorder_object_
;
81 media::ScopedSLObjectItf engine_object_
;
83 SLRecordItf recorder_
;
85 // Buffer queue recorder interface.
86 SLAndroidSimpleBufferQueueItf simple_buffer_queue_
;
88 SLDataFormat_PCM format_
;
90 // Audio buffers that are allocated in the constructor based on
91 // info from audio parameters.
92 uint8
* audio_data_
[kMaxNumOfBuffersInQueue
];
94 int active_buffer_index_
;
95 int buffer_size_bytes_
;
99 scoped_ptr
<media::AudioBus
> audio_bus_
;
101 DISALLOW_COPY_AND_ASSIGN(OpenSLESInputStream
);
106 #endif // MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_