OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_ | 5 #ifndef MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_ |
6 #define MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_ | 6 #define MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_ |
7 | 7 |
8 #include <SLES/OpenSLES.h> | 8 #include <SLES/OpenSLES.h> |
9 #include <SLES/OpenSLES_Android.h> | 9 #include <SLES/OpenSLES_Android.h> |
10 | 10 |
11 #include "base/compiler_specific.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" |
12 #include "media/audio/audio_io.h" | 15 #include "media/audio/audio_io.h" |
13 #include "media/audio/audio_parameters.h" | 16 #include "media/audio/audio_parameters.h" |
14 #include "media/audio/android/opensles_util.h" | |
15 | 17 |
16 namespace media { | 18 namespace media { |
17 | 19 |
18 class AudioManagerAndroid; | 20 class AudioManagerAndroid; |
19 | 21 |
20 // Implements PCM audio input support for Android using the OpenSLES API. | 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. |
21 class OpenSLESInputStream : public AudioInputStream { | 26 class OpenSLESInputStream : public AudioInputStream { |
22 public: | 27 public: |
23 static const int kNumOfQueuesInBuffer = 2; | 28 static const int kMaxNumOfBuffersInQueue = 2; |
24 | 29 |
25 OpenSLESInputStream(AudioManagerAndroid* manager, | 30 OpenSLESInputStream(AudioManagerAndroid* manager, |
26 const AudioParameters& params); | 31 const AudioParameters& params); |
27 | 32 |
28 virtual ~OpenSLESInputStream(); | 33 virtual ~OpenSLESInputStream(); |
29 | 34 |
30 // Implementation of AudioInputStream. | 35 // Implementation of AudioInputStream. |
31 virtual bool Open() OVERRIDE; | 36 virtual bool Open() OVERRIDE; |
32 virtual void Start(AudioInputCallback* callback) OVERRIDE; | 37 virtual void Start(AudioInputCallback* callback) OVERRIDE; |
33 virtual void Stop() OVERRIDE; | 38 virtual void Stop() OVERRIDE; |
34 virtual void Close() OVERRIDE; | 39 virtual void Close() OVERRIDE; |
35 virtual double GetMaxVolume() OVERRIDE; | 40 virtual double GetMaxVolume() OVERRIDE; |
36 virtual void SetVolume(double volume) OVERRIDE; | 41 virtual void SetVolume(double volume) OVERRIDE; |
37 virtual double GetVolume() OVERRIDE; | 42 virtual double GetVolume() OVERRIDE; |
38 virtual void SetAutomaticGainControl(bool enabled) OVERRIDE; | 43 virtual void SetAutomaticGainControl(bool enabled) OVERRIDE; |
39 virtual bool GetAutomaticGainControl() OVERRIDE; | 44 virtual bool GetAutomaticGainControl() OVERRIDE; |
40 | 45 |
41 private: | 46 private: |
42 bool CreateRecorder(); | 47 bool CreateRecorder(); |
43 | 48 |
| 49 // Called from OpenSLES specific audio worker thread. |
44 static void SimpleBufferQueueCallback( | 50 static void SimpleBufferQueueCallback( |
45 SLAndroidSimpleBufferQueueItf buffer_queue, void* instance); | 51 SLAndroidSimpleBufferQueueItf buffer_queue, |
| 52 void* instance); |
46 | 53 |
| 54 // Called from OpenSLES specific audio worker thread. |
47 void ReadBufferQueue(); | 55 void ReadBufferQueue(); |
48 | 56 |
49 // Called in Open(); | 57 // Called in Open(); |
50 void SetupAudioBuffer(); | 58 void SetupAudioBuffer(); |
51 | 59 |
52 // Called in Close(); | 60 // Called in Close(); |
53 void ReleaseAudioBuffer(); | 61 void ReleaseAudioBuffer(); |
54 | 62 |
55 // If OpenSLES reports an error this function handles it and passes it to | 63 // If OpenSLES reports an error this function handles it and passes it to |
56 // the attached AudioInputCallback::OnError(). | 64 // the attached AudioInputCallback::OnError(). |
57 void HandleError(SLresult error); | 65 void HandleError(SLresult error); |
58 | 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 |
59 AudioManagerAndroid* audio_manager_; | 73 AudioManagerAndroid* audio_manager_; |
60 | 74 |
61 AudioInputCallback* callback_; | 75 AudioInputCallback* callback_; |
62 | 76 |
63 // Shared engine interfaces for the app. | 77 // Shared engine interfaces for the app. |
64 media::ScopedSLObjectItf recorder_object_; | 78 media::ScopedSLObjectItf recorder_object_; |
65 media::ScopedSLObjectItf engine_object_; | 79 media::ScopedSLObjectItf engine_object_; |
66 | 80 |
67 SLRecordItf recorder_; | 81 SLRecordItf recorder_; |
68 | 82 |
69 // Buffer queue recorder interface. | 83 // Buffer queue recorder interface. |
70 SLAndroidSimpleBufferQueueItf simple_buffer_queue_; | 84 SLAndroidSimpleBufferQueueItf simple_buffer_queue_; |
71 | 85 |
72 SLDataFormat_PCM format_; | 86 SLDataFormat_PCM format_; |
73 | 87 |
74 // Audio buffers that are allocated in the constructor based on | 88 // Audio buffers that are allocated in the constructor based on |
75 // info from audio parameters. | 89 // info from audio parameters. |
76 uint8* audio_data_[kNumOfQueuesInBuffer]; | 90 uint8* audio_data_[kMaxNumOfBuffersInQueue]; |
77 | 91 |
78 int active_queue_; | 92 int active_buffer_index_; |
79 int buffer_size_bytes_; | 93 int buffer_size_bytes_; |
80 | 94 |
81 bool started_; | 95 bool started_; |
82 | 96 |
83 DISALLOW_COPY_AND_ASSIGN(OpenSLESInputStream); | 97 DISALLOW_COPY_AND_ASSIGN(OpenSLESInputStream); |
84 }; | 98 }; |
85 | 99 |
86 } // namespace media | 100 } // namespace media |
87 | 101 |
88 #endif // MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_ | 102 #endif // MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_ |
OLD | NEW |