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" | |
12 #include "media/audio/audio_io.h" | 14 #include "media/audio/audio_io.h" |
13 #include "media/audio/audio_parameters.h" | 15 #include "media/audio/audio_parameters.h" |
14 #include "media/audio/android/opensles_util.h" | 16 #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, void* instance); |
46 | 52 |
53 // Called from OpenSLES specific audio worker thread. | |
47 void ReadBufferQueue(); | 54 void ReadBufferQueue(); |
48 | 55 |
49 // Called in Open(); | 56 // Called in Open(); |
50 void SetupAudioBuffer(); | 57 void SetupAudioBuffer(); |
51 | 58 |
52 // Called in Close(); | 59 // Called in Close(); |
53 void ReleaseAudioBuffer(); | 60 void ReleaseAudioBuffer(); |
54 | 61 |
55 // If OpenSLES reports an error this function handles it and passes it to | 62 // If OpenSLES reports an error this function handles it and passes it to |
56 // the attached AudioInputCallback::OnError(). | 63 // the attached AudioInputCallback::OnError(). |
57 void HandleError(SLresult error); | 64 void HandleError(SLresult error); |
58 | 65 |
66 base::ThreadChecker thread_checker_; | |
67 | |
68 // Protects |callback_|, |active_queue_|, |audio_data_|, | |
69 // |buffer_size_bytes_| and |simple_buffer_queue_|. | |
70 base::Lock lock_; | |
71 | |
59 AudioManagerAndroid* audio_manager_; | 72 AudioManagerAndroid* audio_manager_; |
60 | 73 |
61 AudioInputCallback* callback_; | 74 AudioInputCallback* callback_; |
62 | 75 |
63 // Shared engine interfaces for the app. | 76 // Shared engine interfaces for the app. |
64 media::ScopedSLObjectItf recorder_object_; | 77 media::ScopedSLObjectItf recorder_object_; |
65 media::ScopedSLObjectItf engine_object_; | 78 media::ScopedSLObjectItf engine_object_; |
66 | 79 |
67 SLRecordItf recorder_; | 80 SLRecordItf recorder_; |
68 | 81 |
69 // Buffer queue recorder interface. | 82 // Buffer queue recorder interface. |
70 SLAndroidSimpleBufferQueueItf simple_buffer_queue_; | 83 SLAndroidSimpleBufferQueueItf simple_buffer_queue_; |
71 | 84 |
72 SLDataFormat_PCM format_; | 85 SLDataFormat_PCM format_; |
73 | 86 |
74 // Audio buffers that are allocated in the constructor based on | 87 // Audio buffers that are allocated in the constructor based on |
75 // info from audio parameters. | 88 // info from audio parameters. |
76 uint8* audio_data_[kNumOfQueuesInBuffer]; | 89 uint8* audio_data_[kMaxNumOfBuffersInQueue]; |
77 | 90 |
78 int active_queue_; | 91 int active_buffer_; |
tommi (sloooow) - chröme
2013/09/03 12:15:09
nit: active_buffer_index_
henrika (OOO until Aug 14)
2013/09/03 12:48:32
Done.
| |
79 int buffer_size_bytes_; | 92 int buffer_size_bytes_; |
80 | 93 |
81 bool started_; | 94 bool started_; |
82 | 95 |
83 DISALLOW_COPY_AND_ASSIGN(OpenSLESInputStream); | 96 DISALLOW_COPY_AND_ASSIGN(OpenSLESInputStream); |
84 }; | 97 }; |
85 | 98 |
86 } // namespace media | 99 } // namespace media |
87 | 100 |
88 #endif // MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_ | 101 #endif // MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_ |
OLD | NEW |