blob: 136a4672ff6e816d463efbbc95065e4883e0c15b [file] [log] [blame]
Eric Laurent6d607012021-07-05 11:54:40 +02001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_MEDIA_SPATIALIZER_H
18#define ANDROID_MEDIA_SPATIALIZER_H
19
20#include <android/media/BnEffect.h>
21#include <android/media/BnSpatializer.h>
Eric Laurent6d607012021-07-05 11:54:40 +020022#include <android/media/SpatializationLevel.h>
Eric Laurent2be8b292021-08-23 09:44:33 -070023#include <android/media/SpatializationMode.h>
24#include <android/media/SpatializerHeadTrackingMode.h>
25#include <android/sensor.h>
26#include <media/audiohal/EffectHalInterface.h>
Eric Laurent8a4259f2021-09-14 16:04:00 +020027#include <media/stagefright/foundation/ALooper.h>
Eric Laurent6d607012021-07-05 11:54:40 +020028#include <media/AudioEffect.h>
Eric Laurent1c5e2e32021-08-18 18:50:28 +020029#include <system/audio_effects/effect_spatializer.h>
Eric Laurent6d607012021-07-05 11:54:40 +020030
Eric Laurent2be8b292021-08-23 09:44:33 -070031#include "SpatializerPoseController.h"
Eric Laurent6d607012021-07-05 11:54:40 +020032
33namespace android {
34
35
36// ----------------------------------------------------------------------------
37
38/**
39 * A callback interface from the Spatializer object or its parent AudioPolicyService.
40 * This is implemented by the audio policy service hosting the Spatializer to perform
41 * actions needed when a state change inside the Spatializer requires some audio system
42 * changes that cannot be performed by the Spatializer. For instance opening or closing a
43 * spatializer output stream when the spatializer is enabled or disabled
44 */
45class SpatializerPolicyCallback {
46public:
47 /** Called when a stage change occurs that requires the parent audio policy service to take
48 * some action.
49 */
50 virtual void onCheckSpatializer() = 0;
51
52 virtual ~SpatializerPolicyCallback() = default;
53};
54/**
55 * The Spatializer class implements all functional controlling the multichannel spatializer
56 * with head tracking implementation in the native audio service: audio policy and audio flinger.
57 * It presents an AIDL interface available to the java audio service to discover the availability
58 * of the feature and options, control its state and register an active head tracking sensor.
59 * It maintains the current state of the platform spatializer and applies the stored parameters
60 * when the spatializer engine is created and enabled.
61 * Based on the requested spatializer level, it will request the creation of a specialized output
62 * mixer to the audio policy service which will in turn notify the Spatializer of the output
63 * stream on which a spatializer engine should be created, configured and enabled.
64 * The spatializer also hosts the head tracking management logic. This logic receives the
65 * desired head tracking mode and selected head tracking sensor, registers a sensor event listener
66 * and derives the compounded head pose information to the spatializer engine.
67 *
68 * Workflow:
69 * - Initialization: when the audio policy service starts, it checks if a spatializer effect
70 * engine exists and if the audio policy manager reports a dedicated spatializer output profile.
71 * If both conditions are met, a Spatializer object is created
72 * - Capabilities discovery: AudioService will call AudioSystem::canBeSpatialized() and if true,
73 * acquire an ISpatializer interface with AudioSystem::getSpatializer(). This interface
74 * will be used to query the implementation capabilities and configure the spatializer.
75 * - Enabling: when ISpatializer::setLevel() sets a level different from NONE the spatializer
76 * is considered enabled. The audio policy callback onCheckSpatializer() is called. This
77 * triggers a request to audio policy manager to open a spatialization output stream and a
78 * spatializer mixer is created in audio flinger. When an output is returned by audio policy
79 * manager, Spatializer::attachOutput() is called which creates and enables the spatializer
80 * stage engine on the specified output.
81 * - Disabling: when the spatialization level is set to NONE, the spatializer is considered
82 * disabled. The audio policy callback onCheckSpatializer() is called. This triggers a call
83 * to Spatializer::detachOutput() and the spatializer engine is released. Then a request is
84 * made to audio policy manager to release and close the spatializer output stream and the
85 * spatializer mixer thread is destroyed.
86 */
Eric Laurent2be8b292021-08-23 09:44:33 -070087class Spatializer : public media::BnSpatializer,
88 public IBinder::DeathRecipient,
89 private SpatializerPoseController::Listener {
90 public:
Eric Laurent6d607012021-07-05 11:54:40 +020091 static sp<Spatializer> create(SpatializerPolicyCallback *callback);
92
93 ~Spatializer() override;
94
Eric Laurent8a4259f2021-09-14 16:04:00 +020095 /** RefBase */
96 void onFirstRef();
97
Eric Laurent6d607012021-07-05 11:54:40 +020098 /** ISpatializer, see ISpatializer.aidl */
99 binder::Status release() override;
100 binder::Status getSupportedLevels(std::vector<media::SpatializationLevel>* levels) override;
101 binder::Status setLevel(media::SpatializationLevel level) override;
102 binder::Status getLevel(media::SpatializationLevel *level) override;
Eric Laurentc87402b2021-09-17 16:49:42 +0200103 binder::Status isHeadTrackingSupported(bool *supports);
Eric Laurent6d607012021-07-05 11:54:40 +0200104 binder::Status getSupportedHeadTrackingModes(
Ytai Ben-Tsvia16a9df2021-08-05 08:57:06 -0700105 std::vector<media::SpatializerHeadTrackingMode>* modes) override;
106 binder::Status setDesiredHeadTrackingMode(
107 media::SpatializerHeadTrackingMode mode) override;
108 binder::Status getActualHeadTrackingMode(
109 media::SpatializerHeadTrackingMode* mode) override;
110 binder::Status recenterHeadTracker() override;
Eric Laurent6d607012021-07-05 11:54:40 +0200111 binder::Status setGlobalTransform(const std::vector<float>& screenToStage) override;
Eric Laurent2be8b292021-08-23 09:44:33 -0700112 binder::Status setHeadSensor(int sensorHandle) override;
113 binder::Status setScreenSensor(int sensorHandle) override;
114 binder::Status setDisplayOrientation(float physicalToLogicalAngle) override;
115 binder::Status setHingeAngle(float hingeAngle) override;
116 binder::Status getSupportedModes(std::vector<media::SpatializationMode>* modes) override;
Eric Laurent67816e32021-09-16 15:18:40 +0200117 binder::Status registerHeadTrackingCallback(
118 const sp<media::ISpatializerHeadTrackingCallback>& callback) override;
Eric Laurentc87402b2021-09-17 16:49:42 +0200119 binder::Status setParameter(int key, const std::vector<unsigned char>& value) override;
120 binder::Status getParameter(int key, std::vector<unsigned char> *value) override;
121 binder::Status getOutput(int *output);
Eric Laurent6d607012021-07-05 11:54:40 +0200122
123 /** IBinder::DeathRecipient. Listen to the death of the INativeSpatializerCallback. */
124 virtual void binderDied(const wp<IBinder>& who);
125
126 /** Registers a INativeSpatializerCallback when a client is attached to this Spatializer
127 * by audio policy service.
128 */
129 status_t registerCallback(const sp<media::INativeSpatializerCallback>& callback);
130
Eric Laurent2be8b292021-08-23 09:44:33 -0700131 status_t loadEngineConfiguration(sp<EffectHalInterface> effect);
132
Eric Laurent6d607012021-07-05 11:54:40 +0200133 /** Level getter for use by local classes. */
Eric Laurent2be8b292021-08-23 09:44:33 -0700134 media::SpatializationLevel getLevel() const { std::lock_guard lock(mLock); return mLevel; }
Eric Laurent6d607012021-07-05 11:54:40 +0200135
136 /** Called by audio policy service when the special output mixer dedicated to spatialization
137 * is opened and the spatializer engine must be created.
138 */
139 status_t attachOutput(audio_io_handle_t output);
140 /** Called by audio policy service when the special output mixer dedicated to spatialization
141 * is closed and the spatializer engine must be release.
142 */
143 audio_io_handle_t detachOutput();
144 /** Returns the output stream the spatializer is attached to. */
Eric Laurent2be8b292021-08-23 09:44:33 -0700145 audio_io_handle_t getOutput() const { std::lock_guard lock(mLock); return mOutput; }
Eric Laurent6d607012021-07-05 11:54:40 +0200146
147 /** Gets the channel mask, sampling rate and format set for the spatializer input. */
Eric Laurent2be8b292021-08-23 09:44:33 -0700148 audio_config_base_t getAudioInConfig() const;
Eric Laurent6d607012021-07-05 11:54:40 +0200149
Eric Laurent8a4259f2021-09-14 16:04:00 +0200150 void calculateHeadPose();
151
Eric Laurent6d607012021-07-05 11:54:40 +0200152private:
Eric Laurent6d607012021-07-05 11:54:40 +0200153 Spatializer(effect_descriptor_t engineDescriptor,
154 SpatializerPolicyCallback *callback);
155
Eric Laurent6d607012021-07-05 11:54:40 +0200156 static void engineCallback(int32_t event, void* user, void *info);
157
Eric Laurent2be8b292021-08-23 09:44:33 -0700158 // From VirtualizerStageController::Listener
159 void onHeadToStagePose(const media::Pose3f& headToStage) override;
160 void onActualModeChange(media::HeadTrackingMode mode) override;
161
Eric Laurent8a4259f2021-09-14 16:04:00 +0200162 void onHeadToStagePoseMsg(const std::vector<float>& headToStage);
163 void onActualModeChangeMsg(media::HeadTrackingMode mode);
164
Eric Laurent2be8b292021-08-23 09:44:33 -0700165
166 static ConversionResult<ASensorRef> getSensorFromHandle(int handle);
167
168 static constexpr int kMaxEffectParamValues = 10;
169 /**
170 * Get a parameter from spatializer engine by calling the effect HAL command method directly.
171 * To be used when the engine instance mEngine is not yet created in the effect framework.
172 * When MULTI_VALUES is false, the expected reply is only one value of type T.
173 * When MULTI_VALUES is true, the expected reply is made of a number (of type T) indicating
174 * how many values are returned, followed by this number for values of type T.
175 */
176 template<bool MULTI_VALUES, typename T>
177 status_t getHalParameter(sp<EffectHalInterface> effect, uint32_t type,
178 std::vector<T> *values) {
179 static_assert(sizeof(T) <= sizeof(uint32_t), "The size of T must less than 32 bits");
180
181 uint32_t cmd[sizeof(effect_param_t) / sizeof(uint32_t) + 1];
182 uint32_t reply[sizeof(effect_param_t) / sizeof(uint32_t) + 2 + kMaxEffectParamValues];
183
184 effect_param_t *p = (effect_param_t *)cmd;
185 p->psize = sizeof(uint32_t);
186 if (MULTI_VALUES) {
187 p->vsize = (kMaxEffectParamValues + 1) * sizeof(T);
188 } else {
189 p->vsize = sizeof(T);
190 }
191 *(uint32_t *)p->data = type;
192 uint32_t replySize = sizeof(effect_param_t) + p->psize + p->vsize;
193
194 status_t status = effect->command(EFFECT_CMD_GET_PARAM,
195 sizeof(effect_param_t) + sizeof(uint32_t), cmd,
196 &replySize, reply);
197 if (status != NO_ERROR) {
198 return status;
199 }
200 if (p->status != NO_ERROR) {
201 return p->status;
202 }
203 if (replySize <
204 sizeof(effect_param_t) + sizeof(uint32_t) + (MULTI_VALUES ? 2 : 1) * sizeof(T)) {
205 return BAD_VALUE;
206 }
207
208 T *params = (T *)((uint8_t *)reply + sizeof(effect_param_t) + sizeof(uint32_t));
209 int numParams = 1;
210 if (MULTI_VALUES) {
211 numParams = (int)*params++;
212 }
213 if (numParams > kMaxEffectParamValues) {
214 return BAD_VALUE;
215 }
Eric Laurentc87402b2021-09-17 16:49:42 +0200216 (*values).clear();
Eric Laurent2be8b292021-08-23 09:44:33 -0700217 std::copy(&params[0], &params[numParams], back_inserter(*values));
218 return NO_ERROR;
219 }
220
221 /**
222 * Set a parameter to spatializer engine by calling setParameter on mEngine AudioEffect object.
223 * It is possible to pass more than one value of type T according to the parameter type
224 * according to values vector size.
225 */
226 template<typename T>
227 status_t setEffectParameter_l(uint32_t type, const std::vector<T>& values) REQUIRES(mLock) {
228 static_assert(sizeof(T) <= sizeof(uint32_t), "The size of T must less than 32 bits");
229
230 uint32_t cmd[sizeof(effect_param_t) / sizeof(uint32_t) + 1 + values.size()];
231 effect_param_t *p = (effect_param_t *)cmd;
232 p->psize = sizeof(uint32_t);
233 p->vsize = sizeof(T) * values.size();
234 *(uint32_t *)p->data = type;
235 memcpy((uint32_t *)p->data + 1, values.data(), sizeof(T) * values.size());
236
Eric Laurentc87402b2021-09-17 16:49:42 +0200237 status_t status = mEngine->setParameter(p);
238 if (status != NO_ERROR) {
239 return status;
240 }
241 if (p->status != NO_ERROR) {
242 return p->status;
243 }
244 return NO_ERROR;
245 }
246
247 /**
248 * Get a parameter from spatializer engine by calling getParameter on AudioEffect object.
249 * It is possible to read more than one value of type T according to the parameter type
250 * by specifying values vector size.
251 */
252 template<typename T>
253 status_t getEffectParameter_l(uint32_t type, std::vector<T> *values) REQUIRES(mLock) {
254 static_assert(sizeof(T) <= sizeof(uint32_t), "The size of T must less than 32 bits");
255
256 uint32_t cmd[sizeof(effect_param_t) / sizeof(uint32_t) + 1 + values->size()];
257 effect_param_t *p = (effect_param_t *)cmd;
258 p->psize = sizeof(uint32_t);
259 p->vsize = sizeof(T) * values->size();
260 *(uint32_t *)p->data = type;
261
262 status_t status = mEngine->getParameter(p);
263
264 if (status != NO_ERROR) {
265 return status;
266 }
267 if (p->status != NO_ERROR) {
268 return p->status;
269 }
270
271 int numValues = std::min(p->vsize / sizeof(T), values->size());
272 (*values).clear();
273 T *retValues = (T *)((uint8_t *)p->data + sizeof(uint32_t));
274 std::copy(&retValues[0], &retValues[numValues], back_inserter(*values));
275
276 return NO_ERROR;
Eric Laurent2be8b292021-08-23 09:44:33 -0700277 }
278
Eric Laurent8a4259f2021-09-14 16:04:00 +0200279 void postFramesProcessedMsg(int frames);
280
Eric Laurent6d607012021-07-05 11:54:40 +0200281 /** Effect engine descriptor */
282 const effect_descriptor_t mEngineDescriptor;
283 /** Callback interface to parent audio policy service */
284 SpatializerPolicyCallback* mPolicyCallback;
285
286 /** Mutex protecting internal state */
Eric Laurent2be8b292021-08-23 09:44:33 -0700287 mutable std::mutex mLock;
Eric Laurent6d607012021-07-05 11:54:40 +0200288
289 /** Client AudioEffect for the engine */
290 sp<AudioEffect> mEngine GUARDED_BY(mLock);
291 /** Output stream the spatializer mixer thread is attached to */
292 audio_io_handle_t mOutput GUARDED_BY(mLock) = AUDIO_IO_HANDLE_NONE;
Eric Laurent6d607012021-07-05 11:54:40 +0200293
294 /** Callback interface to the client (AudioService) controlling this`Spatializer */
295 sp<media::INativeSpatializerCallback> mSpatializerCallback GUARDED_BY(mLock);
296
Eric Laurent67816e32021-09-16 15:18:40 +0200297 /** Callback interface for head tracking */
298 sp<media::ISpatializerHeadTrackingCallback> mHeadTrackingCallback GUARDED_BY(mLock);
299
Eric Laurent6d607012021-07-05 11:54:40 +0200300 /** Requested spatialization level */
301 media::SpatializationLevel mLevel GUARDED_BY(mLock) = media::SpatializationLevel::NONE;
Eric Laurent6d607012021-07-05 11:54:40 +0200302
Eric Laurent2be8b292021-08-23 09:44:33 -0700303 /** Control logic for head-tracking, etc. */
304 std::shared_ptr<SpatializerPoseController> mPoseController GUARDED_BY(mLock);
305
306 /** Last requested head tracking mode */
307 media::HeadTrackingMode mDesiredHeadTrackingMode GUARDED_BY(mLock)
308 = media::HeadTrackingMode::STATIC;
309
310 /** Last-reported actual head-tracking mode. */
311 media::SpatializerHeadTrackingMode mActualHeadTrackingMode GUARDED_BY(mLock)
312 = media::SpatializerHeadTrackingMode::DISABLED;
313
314 /** Selected Head pose sensor */
315 ASensorRef mHeadSensor GUARDED_BY(mLock) = nullptr;
316
317 /** Selected Screen pose sensor */
318 ASensorRef mScreenSensor GUARDED_BY(mLock) = nullptr;
319
320 /** Last display orientation received */
321 static constexpr float kDisplayOrientationInvalid = 1000;
322 float mDisplayOrientation GUARDED_BY(mLock) = kDisplayOrientationInvalid;
323
324 std::vector<media::SpatializationLevel> mLevels;
325 std::vector<media::SpatializationMode> mSpatializationModes;
326 std::vector<audio_channel_mask_t> mChannelMasks;
327 bool mSupportsHeadTracking;
Eric Laurent8a4259f2021-09-14 16:04:00 +0200328
329 // Looper thread for mEngine callbacks
330 class EngineCallbackHandler;
331
332 sp<ALooper> mLooper;
333 sp<EngineCallbackHandler> mHandler;
334
335 static const std::vector<const char *> sHeadPoseKeys;
Eric Laurent6d607012021-07-05 11:54:40 +0200336};
337
338
339}; // namespace android
340
341#endif // ANDROID_MEDIA_SPATIALIZER_H