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_OUTPUT_H_ | 5 #ifndef MEDIA_AUDIO_ANDROID_OPENSLES_OUTPUT_H_ |
6 #define MEDIA_AUDIO_ANDROID_OPENSLES_OUTPUT_H_ | 6 #define MEDIA_AUDIO_ANDROID_OPENSLES_OUTPUT_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/android/opensles_util.h" | 14 #include "media/audio/android/opensles_util.h" |
13 #include "media/audio/audio_io.h" | 15 #include "media/audio/audio_io.h" |
14 #include "media/audio/audio_parameters.h" | 16 #include "media/audio/audio_parameters.h" |
15 | 17 |
16 namespace media { | 18 namespace media { |
17 | 19 |
18 class AudioManagerAndroid; | 20 class AudioManagerAndroid; |
19 | 21 |
20 // Implements PCM audio output support for Android using the OpenSLES API. | 22 // Implements PCM audio output 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 OpenSLESOutputStream : public AudioOutputStream { | 26 class OpenSLESOutputStream : public AudioOutputStream { |
22 public: | 27 public: |
23 static const int kNumOfQueuesInBuffer = 2; | 28 static const int kMaxNumOfBuffersInQueue = 2; |
24 | 29 |
25 OpenSLESOutputStream(AudioManagerAndroid* manager, | 30 OpenSLESOutputStream(AudioManagerAndroid* manager, |
26 const AudioParameters& params); | 31 const AudioParameters& params); |
27 | 32 |
28 virtual ~OpenSLESOutputStream(); | 33 virtual ~OpenSLESOutputStream(); |
29 | 34 |
30 // Implementation of AudioOutputStream. | 35 // Implementation of AudioOutputStream. |
31 virtual bool Open() OVERRIDE; | 36 virtual bool Open() OVERRIDE; |
32 virtual void Close() OVERRIDE; | 37 virtual void Close() OVERRIDE; |
33 virtual void Start(AudioSourceCallback* callback) OVERRIDE; | 38 virtual void Start(AudioSourceCallback* callback) OVERRIDE; |
34 virtual void Stop() OVERRIDE; | 39 virtual void Stop() OVERRIDE; |
35 virtual void SetVolume(double volume) OVERRIDE; | 40 virtual void SetVolume(double volume) OVERRIDE; |
36 virtual void GetVolume(double* volume) OVERRIDE; | 41 virtual void GetVolume(double* volume) OVERRIDE; |
37 | 42 |
38 private: | 43 private: |
39 bool CreatePlayer(); | 44 bool CreatePlayer(); |
40 | 45 |
| 46 // Called from OpenSLES specific audio worker thread. |
41 static void SimpleBufferQueueCallback( | 47 static void SimpleBufferQueueCallback( |
42 SLAndroidSimpleBufferQueueItf buffer_queue, void* instance); | 48 SLAndroidSimpleBufferQueueItf buffer_queue, void* instance); |
43 | 49 |
| 50 // Fills up one buffer by asking the registered source for data. |
| 51 // Called from OpenSLES specific audio worker thread. |
44 void FillBufferQueue(); | 52 void FillBufferQueue(); |
45 | 53 |
| 54 // Called from the audio manager thread. |
| 55 void FillBufferQueueNoLock(); |
| 56 |
46 // Called in Open(); | 57 // Called in Open(); |
47 void SetupAudioBuffer(); | 58 void SetupAudioBuffer(); |
48 | 59 |
49 // Called in Close(); | 60 // Called in Close(); |
50 void ReleaseAudioBuffer(); | 61 void ReleaseAudioBuffer(); |
51 | 62 |
52 // 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 |
53 // the attached AudioOutputCallback::OnError(). | 64 // the attached AudioOutputCallback::OnError(). |
54 void HandleError(SLresult error); | 65 void HandleError(SLresult error); |
55 | 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 |
56 AudioManagerAndroid* audio_manager_; | 73 AudioManagerAndroid* audio_manager_; |
57 | 74 |
58 AudioSourceCallback* callback_; | 75 AudioSourceCallback* callback_; |
59 | 76 |
60 // Shared engine interfaces for the app. | 77 // Shared engine interfaces for the app. |
61 media::ScopedSLObjectItf engine_object_; | 78 media::ScopedSLObjectItf engine_object_; |
62 media::ScopedSLObjectItf player_object_; | 79 media::ScopedSLObjectItf player_object_; |
63 media::ScopedSLObjectItf output_mixer_; | 80 media::ScopedSLObjectItf output_mixer_; |
64 | 81 |
65 SLPlayItf player_; | 82 SLPlayItf player_; |
66 | 83 |
67 // Buffer queue recorder interface. | 84 // Buffer queue recorder interface. |
68 SLAndroidSimpleBufferQueueItf simple_buffer_queue_; | 85 SLAndroidSimpleBufferQueueItf simple_buffer_queue_; |
69 | 86 |
70 SLDataFormat_PCM format_; | 87 SLDataFormat_PCM format_; |
71 | 88 |
72 // Audio buffer arrays that are allocated in the constructor. | 89 // Audio buffers that are allocated in the constructor based on |
73 uint8* audio_data_[kNumOfQueuesInBuffer]; | 90 // info from audio parameters. |
| 91 uint8* audio_data_[kMaxNumOfBuffersInQueue]; |
74 | 92 |
75 int active_queue_; | 93 int active_buffer_index_; |
76 size_t buffer_size_bytes_; | 94 size_t buffer_size_bytes_; |
77 | 95 |
78 bool started_; | 96 bool started_; |
79 | 97 |
80 // Volume level from 0 to 1. | 98 // Volume level from 0 to 1. |
81 float volume_; | 99 float volume_; |
82 | 100 |
83 // Container for retrieving data from AudioSourceCallback::OnMoreData(). | 101 // Container for retrieving data from AudioSourceCallback::OnMoreData(). |
84 scoped_ptr<AudioBus> audio_bus_; | 102 scoped_ptr<AudioBus> audio_bus_; |
85 | 103 |
86 DISALLOW_COPY_AND_ASSIGN(OpenSLESOutputStream); | 104 DISALLOW_COPY_AND_ASSIGN(OpenSLESOutputStream); |
87 }; | 105 }; |
88 | 106 |
89 } // namespace media | 107 } // namespace media |
90 | 108 |
91 #endif // MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_ | 109 #endif // MEDIA_AUDIO_ANDROID_OPENSLES_OUTPUT_H_ |
OLD | NEW |