blob: 1b2b1d78fdbebfeb7b2811cfee50f0e1e92c7f17 [file] [log] [blame]
henrikab2619892015-05-18 16:49:16 +02001/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_OPENSLES_PLAYER_H_
12#define WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_OPENSLES_PLAYER_H_
13
14#include <SLES/OpenSLES.h>
15#include <SLES/OpenSLES_Android.h>
16#include <SLES/OpenSLES_AndroidConfiguration.h>
17
henrikab2619892015-05-18 16:49:16 +020018#include "webrtc/base/thread_checker.h"
19#include "webrtc/modules/audio_device/android/audio_common.h"
20#include "webrtc/modules/audio_device/android/audio_manager.h"
21#include "webrtc/modules/audio_device/android/opensles_common.h"
22#include "webrtc/modules/audio_device/include/audio_device_defines.h"
23#include "webrtc/modules/audio_device/audio_device_generic.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010024#include "webrtc/modules/utility/include/helpers_android.h"
henrikab2619892015-05-18 16:49:16 +020025
26namespace webrtc {
27
28class FineAudioBuffer;
29
30// Implements 16-bit mono PCM audio output support for Android using the
31// C based OpenSL ES API. No calls from C/C++ to Java using JNI is done.
32//
33// An instance must be created and destroyed on one and the same thread.
34// All public methods must also be called on the same thread. A thread checker
henrikg91d6ede2015-09-17 00:24:34 -070035// will RTC_DCHECK if any method is called on an invalid thread. Decoded audio
henrikab2619892015-05-18 16:49:16 +020036// buffers are requested on a dedicated internal thread managed by the OpenSL
37// ES layer.
38//
39// The existing design forces the user to call InitPlayout() after Stoplayout()
40// to be able to call StartPlayout() again. This is inline with how the Java-
41// based implementation works.
42//
43// OpenSL ES is a native C API which have no Dalvik-related overhead such as
44// garbage collection pauses and it supports reduced audio output latency.
45// If the device doesn't claim this feature but supports API level 9 (Android
46// platform version 2.3) or later, then we can still use the OpenSL ES APIs but
47// the output latency may be higher.
48class OpenSLESPlayer {
49 public:
henrika918b5542016-09-19 15:44:09 +020050 // Beginning with API level 17 (Android 4.2), a buffer count of 2 or more is
51 // required for lower latency. Beginning with API level 18 (Android 4.3), a
52 // buffer count of 1 is sufficient for lower latency. In addition, the buffer
53 // size and sample rate must be compatible with the device's native output
54 // configuration provided via the audio manager at construction.
55 // TODO(henrika): perhaps set this value dynamically based on OS version.
56 static const int kNumOfOpenSLESBuffers = 2;
henrikab2619892015-05-18 16:49:16 +020057
henrikab2619892015-05-18 16:49:16 +020058 explicit OpenSLESPlayer(AudioManager* audio_manager);
59 ~OpenSLESPlayer();
60
61 int Init();
62 int Terminate();
63
64 int InitPlayout();
65 bool PlayoutIsInitialized() const { return initialized_; }
66
67 int StartPlayout();
68 int StopPlayout();
69 bool Playing() const { return playing_; }
70
71 int SpeakerVolumeIsAvailable(bool& available);
72 int SetSpeakerVolume(uint32_t volume);
73 int SpeakerVolume(uint32_t& volume) const;
74 int MaxSpeakerVolume(uint32_t& maxVolume) const;
75 int MinSpeakerVolume(uint32_t& minVolume) const;
76
77 void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer);
78
79 private:
80 // These callback methods are called when data is required for playout.
81 // They are both called from an internal "OpenSL ES thread" which is not
82 // attached to the Dalvik VM.
83 static void SimpleBufferQueueCallback(SLAndroidSimpleBufferQueueItf caller,
84 void* context);
85 void FillBufferQueue();
86 // Reads audio data in PCM format using the AudioDeviceBuffer.
87 // Can be called both on the main thread (during Start()) and from the
88 // internal audio thread while output streaming is active.
89 void EnqueuePlayoutData();
90
henrikab2619892015-05-18 16:49:16 +020091 // Allocate memory for audio buffers which will be used to render audio
92 // via the SLAndroidSimpleBufferQueueItf interface.
93 void AllocateDataBuffers();
94
henrika521f7a82016-05-31 07:03:17 -070095 // Obtaines the SL Engine Interface from the existing global Engine object.
96 // The interface exposes creation methods of all the OpenSL ES object types.
97 // This method defines the |engine_| member variable.
98 bool ObtainEngineInterface();
henrikab2619892015-05-18 16:49:16 +020099
100 // Creates/destroys the output mix object.
101 bool CreateMix();
102 void DestroyMix();
103
104 // Creates/destroys the audio player and the simple-buffer object.
105 // Also creates the volume object.
106 bool CreateAudioPlayer();
107 void DestroyAudioPlayer();
108
109 SLuint32 GetPlayState() const;
110
111 // Ensures that methods are called from the same thread as this object is
112 // created on.
113 rtc::ThreadChecker thread_checker_;
114
115 // Stores thread ID in first call to SimpleBufferQueueCallback() from internal
116 // non-application thread which is not attached to the Dalvik JVM.
117 // Detached during construction of this object.
118 rtc::ThreadChecker thread_checker_opensles_;
119
henrika521f7a82016-05-31 07:03:17 -0700120 // Raw pointer to the audio manager injected at construction. Used to cache
121 // audio parameters and to access the global SL engine object needed by the
122 // ObtainEngineInterface() method. The audio manager outlives any instance of
123 // this class.
124 AudioManager* audio_manager_;
125
henrikab2619892015-05-18 16:49:16 +0200126 // Contains audio parameters provided to this class at construction by the
127 // AudioManager.
128 const AudioParameters audio_parameters_;
129
130 // Raw pointer handle provided to us in AttachAudioBuffer(). Owned by the
Peter Boström4adbbcf2016-05-03 15:51:26 -0400131 // AudioDeviceModuleImpl class and called by AudioDeviceModule::Create().
henrikab2619892015-05-18 16:49:16 +0200132 AudioDeviceBuffer* audio_device_buffer_;
133
134 bool initialized_;
135 bool playing_;
136
137 // PCM-type format definition.
138 // TODO(henrika): add support for SLAndroidDataFormat_PCM_EX (android-21) if
139 // 32-bit float representation is needed.
140 SLDataFormat_PCM pcm_format_;
141
henrikab2619892015-05-18 16:49:16 +0200142 // Queue of audio buffers to be used by the player object for rendering
143 // audio. They will be used in a Round-robin way and the size of each buffer
144 // is given by FineAudioBuffer::RequiredBufferSizeBytes().
kwibergf01633e2016-02-24 05:00:36 -0800145 std::unique_ptr<SLint8[]> audio_buffers_[kNumOfOpenSLESBuffers];
henrikab2619892015-05-18 16:49:16 +0200146
147 // FineAudioBuffer takes an AudioDeviceBuffer which delivers audio data
148 // in chunks of 10ms. It then allows for this data to be pulled in
149 // a finer or coarser granularity. I.e. interacting with this class instead
150 // of directly with the AudioDeviceBuffer one can ask for any number of
151 // audio data samples.
henrika918b5542016-09-19 15:44:09 +0200152 // Example: native buffer size can be 192 audio frames at 48kHz sample rate.
153 // WebRTC will provide 480 audio frames per 10ms but OpenSL ES asks for 192
154 // in each callback (one every 4th ms). This class can then ask for 192 and
155 // the FineAudioBuffer will ask WebRTC for new data approximately only every
156 // second callback and also cache non-utilized audio.
157 std::unique_ptr<FineAudioBuffer> fine_audio_buffer_;
henrikab2619892015-05-18 16:49:16 +0200158
159 // Keeps track of active audio buffer 'n' in the audio_buffers_[n] queue.
160 // Example (kNumOfOpenSLESBuffers = 2): counts 0, 1, 0, 1, ...
161 int buffer_index_;
162
henrikab2619892015-05-18 16:49:16 +0200163 // This interface exposes creation methods for all the OpenSL ES object types.
164 // It is the OpenSL ES API entry point.
165 SLEngineItf engine_;
166
167 // Output mix object to be used by the player object.
168 webrtc::ScopedSLObjectItf output_mix_;
169
170 // The audio player media object plays out audio to the speakers. It also
171 // supports volume control.
172 webrtc::ScopedSLObjectItf player_object_;
173
174 // This interface is supported on the audio player and it controls the state
175 // of the audio player.
176 SLPlayItf player_;
177
178 // The Android Simple Buffer Queue interface is supported on the audio player
179 // and it provides methods to send audio data from the source to the audio
180 // player for rendering.
181 SLAndroidSimpleBufferQueueItf simple_buffer_queue_;
182
183 // This interface exposes controls for manipulating the object’s audio volume
184 // properties. This interface is supported on the Audio Player object.
185 SLVolumeItf volume_;
henrikae71b24e2015-11-12 01:48:32 -0800186
187 // Last time the OpenSL ES layer asked for audio data to play out.
188 uint32_t last_play_time_;
henrikab2619892015-05-18 16:49:16 +0200189};
190
191} // namespace webrtc
192
193#endif // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_OPENSLES_PLAYER_H_