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_OUTPUT_H_
6 #define MEDIA_AUDIO_ANDROID_OPENSLES_OUTPUT_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 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.
26 class OpenSLESOutputStream
: public AudioOutputStream
{
28 static const int kMaxNumOfBuffersInQueue
= 2;
30 OpenSLESOutputStream(AudioManagerAndroid
* manager
,
31 const AudioParameters
& params
,
34 ~OpenSLESOutputStream() override
;
36 // Implementation of AudioOutputStream.
38 void Close() override
;
39 void Start(AudioSourceCallback
* callback
) override
;
41 void SetVolume(double volume
) override
;
42 void GetVolume(double* volume
) override
;
44 // Set the value of |muted_|. It does not affect |volume_| which can be
45 // got by calling GetVolume(). See comments for |muted_| below.
46 void SetMute(bool muted
);
51 // Called from OpenSLES specific audio worker thread.
52 static void SimpleBufferQueueCallback(
53 SLAndroidSimpleBufferQueueItf buffer_queue
,
56 // Fills up one buffer by asking the registered source for data.
57 // Called from OpenSLES specific audio worker thread.
58 void FillBufferQueue();
60 // Called from the audio manager thread.
61 void FillBufferQueueNoLock();
64 void SetupAudioBuffer();
67 void ReleaseAudioBuffer();
69 // If OpenSLES reports an error this function handles it and passes it to
70 // the attached AudioOutputCallback::OnError().
71 void HandleError(SLresult error
);
73 base::ThreadChecker thread_checker_
;
75 // Protects |callback_|, |active_buffer_index_|, |audio_data_|,
76 // |buffer_size_bytes_| and |simple_buffer_queue_|.
79 AudioManagerAndroid
* audio_manager_
;
81 // Audio playback stream type.
82 // See SLES/OpenSLES_Android.h for details.
85 AudioSourceCallback
* callback_
;
87 // Shared engine interfaces for the app.
88 media::ScopedSLObjectItf engine_object_
;
89 media::ScopedSLObjectItf player_object_
;
90 media::ScopedSLObjectItf output_mixer_
;
94 // Buffer queue recorder interface.
95 SLAndroidSimpleBufferQueueItf simple_buffer_queue_
;
97 SLDataFormat_PCM format_
;
99 // Audio buffers that are allocated in the constructor based on
100 // info from audio parameters.
101 uint8
* audio_data_
[kMaxNumOfBuffersInQueue
];
103 int active_buffer_index_
;
104 size_t buffer_size_bytes_
;
108 // Volume control coming from hardware. It overrides |volume_| when it's
109 // true. Otherwise, use |volume_| for scaling.
110 // This is needed because platform voice volume never goes to zero in
111 // COMMUNICATION mode on Android.
114 // Volume level from 0 to 1.
117 // Container for retrieving data from AudioSourceCallback::OnMoreData().
118 scoped_ptr
<AudioBus
> audio_bus_
;
120 DISALLOW_COPY_AND_ASSIGN(OpenSLESOutputStream
);
125 #endif // MEDIA_AUDIO_ANDROID_OPENSLES_OUTPUT_H_