blob: a7d9f32e45c6ed5bccf92feb53b95d0da068c618 [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>
Eric Laurent2be8b292021-08-23 09:44:33 -070025#include <media/audiohal/EffectHalInterface.h>
Eric Laurent8a4259f2021-09-14 16:04:00 +020026#include <media/stagefright/foundation/ALooper.h>
Eric Laurent6d607012021-07-05 11:54:40 +020027#include <media/AudioEffect.h>
Eric Laurent1c5e2e32021-08-18 18:50:28 +020028#include <system/audio_effects/effect_spatializer.h>
Eric Laurent6d607012021-07-05 11:54:40 +020029
Eric Laurent2be8b292021-08-23 09:44:33 -070030#include "SpatializerPoseController.h"
Eric Laurent6d607012021-07-05 11:54:40 +020031
32namespace android {
33
34
35// ----------------------------------------------------------------------------
36
37/**
38 * A callback interface from the Spatializer object or its parent AudioPolicyService.
39 * This is implemented by the audio policy service hosting the Spatializer to perform
40 * actions needed when a state change inside the Spatializer requires some audio system
41 * changes that cannot be performed by the Spatializer. For instance opening or closing a
42 * spatializer output stream when the spatializer is enabled or disabled
43 */
44class SpatializerPolicyCallback {
45public:
46 /** Called when a stage change occurs that requires the parent audio policy service to take
47 * some action.
48 */
49 virtual void onCheckSpatializer() = 0;
50
51 virtual ~SpatializerPolicyCallback() = default;
52};
53/**
54 * The Spatializer class implements all functional controlling the multichannel spatializer
55 * with head tracking implementation in the native audio service: audio policy and audio flinger.
56 * It presents an AIDL interface available to the java audio service to discover the availability
57 * of the feature and options, control its state and register an active head tracking sensor.
58 * It maintains the current state of the platform spatializer and applies the stored parameters
59 * when the spatializer engine is created and enabled.
60 * Based on the requested spatializer level, it will request the creation of a specialized output
61 * mixer to the audio policy service which will in turn notify the Spatializer of the output
62 * stream on which a spatializer engine should be created, configured and enabled.
63 * The spatializer also hosts the head tracking management logic. This logic receives the
64 * desired head tracking mode and selected head tracking sensor, registers a sensor event listener
65 * and derives the compounded head pose information to the spatializer engine.
66 *
67 * Workflow:
68 * - Initialization: when the audio policy service starts, it checks if a spatializer effect
69 * engine exists and if the audio policy manager reports a dedicated spatializer output profile.
70 * If both conditions are met, a Spatializer object is created
71 * - Capabilities discovery: AudioService will call AudioSystem::canBeSpatialized() and if true,
72 * acquire an ISpatializer interface with AudioSystem::getSpatializer(). This interface
73 * will be used to query the implementation capabilities and configure the spatializer.
74 * - Enabling: when ISpatializer::setLevel() sets a level different from NONE the spatializer
75 * is considered enabled. The audio policy callback onCheckSpatializer() is called. This
76 * triggers a request to audio policy manager to open a spatialization output stream and a
77 * spatializer mixer is created in audio flinger. When an output is returned by audio policy
78 * manager, Spatializer::attachOutput() is called which creates and enables the spatializer
79 * stage engine on the specified output.
80 * - Disabling: when the spatialization level is set to NONE, the spatializer is considered
81 * disabled. The audio policy callback onCheckSpatializer() is called. This triggers a call
82 * to Spatializer::detachOutput() and the spatializer engine is released. Then a request is
83 * made to audio policy manager to release and close the spatializer output stream and the
84 * spatializer mixer thread is destroyed.
85 */
Eric Laurent2be8b292021-08-23 09:44:33 -070086class Spatializer : public media::BnSpatializer,
87 public IBinder::DeathRecipient,
Eric Laurentee398ad2022-05-03 18:19:35 +020088 private SpatializerPoseController::Listener,
89 public virtual AudioSystem::SupportedLatencyModesCallback {
Eric Laurent2be8b292021-08-23 09:44:33 -070090 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
Eric Laurentee398ad2022-05-03 18:19:35 +0200126 /** SupportedLatencyModesCallback */
127 void onSupportedLatencyModesChanged(
128 audio_io_handle_t output, const std::vector<audio_latency_mode_t>& modes) override;
129
Eric Laurent6d607012021-07-05 11:54:40 +0200130 /** Registers a INativeSpatializerCallback when a client is attached to this Spatializer
131 * by audio policy service.
132 */
133 status_t registerCallback(const sp<media::INativeSpatializerCallback>& callback);
134
Eric Laurent2be8b292021-08-23 09:44:33 -0700135 status_t loadEngineConfiguration(sp<EffectHalInterface> effect);
136
Eric Laurent6d607012021-07-05 11:54:40 +0200137 /** Level getter for use by local classes. */
Eric Laurent2be8b292021-08-23 09:44:33 -0700138 media::SpatializationLevel getLevel() const { std::lock_guard lock(mLock); return mLevel; }
Eric Laurent6d607012021-07-05 11:54:40 +0200139
140 /** Called by audio policy service when the special output mixer dedicated to spatialization
141 * is opened and the spatializer engine must be created.
142 */
Eric Laurent15903592022-02-24 20:44:36 +0100143 status_t attachOutput(audio_io_handle_t output, size_t numActiveTracks);
Eric Laurent6d607012021-07-05 11:54:40 +0200144 /** Called by audio policy service when the special output mixer dedicated to spatialization
145 * is closed and the spatializer engine must be release.
146 */
147 audio_io_handle_t detachOutput();
148 /** Returns the output stream the spatializer is attached to. */
Eric Laurent2be8b292021-08-23 09:44:33 -0700149 audio_io_handle_t getOutput() const { std::lock_guard lock(mLock); return mOutput; }
Eric Laurent6d607012021-07-05 11:54:40 +0200150
Eric Laurent15903592022-02-24 20:44:36 +0100151 void updateActiveTracks(size_t numActiveTracks);
152
Eric Laurent6d607012021-07-05 11:54:40 +0200153 /** Gets the channel mask, sampling rate and format set for the spatializer input. */
Eric Laurent2be8b292021-08-23 09:44:33 -0700154 audio_config_base_t getAudioInConfig() const;
Eric Laurent6d607012021-07-05 11:54:40 +0200155
Eric Laurent8a4259f2021-09-14 16:04:00 +0200156 void calculateHeadPose();
157
Eric Laurent6d607012021-07-05 11:54:40 +0200158private:
Eric Laurent6d607012021-07-05 11:54:40 +0200159 Spatializer(effect_descriptor_t engineDescriptor,
160 SpatializerPolicyCallback *callback);
161
Eric Laurent6d607012021-07-05 11:54:40 +0200162 static void engineCallback(int32_t event, void* user, void *info);
163
Eric Laurent2be8b292021-08-23 09:44:33 -0700164 // From VirtualizerStageController::Listener
165 void onHeadToStagePose(const media::Pose3f& headToStage) override;
166 void onActualModeChange(media::HeadTrackingMode mode) override;
167
Eric Laurent8a4259f2021-09-14 16:04:00 +0200168 void onHeadToStagePoseMsg(const std::vector<float>& headToStage);
169 void onActualModeChangeMsg(media::HeadTrackingMode mode);
Eric Laurent9c04de92022-07-20 13:49:47 +0200170 void onSupportedLatencyModesChangedMsg(
171 audio_io_handle_t output, std::vector<audio_latency_mode_t>&& modes);
Eric Laurent8a4259f2021-09-14 16:04:00 +0200172
Eric Laurent2be8b292021-08-23 09:44:33 -0700173 static constexpr int kMaxEffectParamValues = 10;
174 /**
175 * Get a parameter from spatializer engine by calling the effect HAL command method directly.
176 * To be used when the engine instance mEngine is not yet created in the effect framework.
177 * When MULTI_VALUES is false, the expected reply is only one value of type T.
178 * When MULTI_VALUES is true, the expected reply is made of a number (of type T) indicating
179 * how many values are returned, followed by this number for values of type T.
180 */
181 template<bool MULTI_VALUES, typename T>
182 status_t getHalParameter(sp<EffectHalInterface> effect, uint32_t type,
183 std::vector<T> *values) {
184 static_assert(sizeof(T) <= sizeof(uint32_t), "The size of T must less than 32 bits");
185
186 uint32_t cmd[sizeof(effect_param_t) / sizeof(uint32_t) + 1];
187 uint32_t reply[sizeof(effect_param_t) / sizeof(uint32_t) + 2 + kMaxEffectParamValues];
188
189 effect_param_t *p = (effect_param_t *)cmd;
190 p->psize = sizeof(uint32_t);
191 if (MULTI_VALUES) {
192 p->vsize = (kMaxEffectParamValues + 1) * sizeof(T);
193 } else {
194 p->vsize = sizeof(T);
195 }
196 *(uint32_t *)p->data = type;
197 uint32_t replySize = sizeof(effect_param_t) + p->psize + p->vsize;
198
199 status_t status = effect->command(EFFECT_CMD_GET_PARAM,
200 sizeof(effect_param_t) + sizeof(uint32_t), cmd,
201 &replySize, reply);
202 if (status != NO_ERROR) {
203 return status;
204 }
205 if (p->status != NO_ERROR) {
206 return p->status;
207 }
208 if (replySize <
209 sizeof(effect_param_t) + sizeof(uint32_t) + (MULTI_VALUES ? 2 : 1) * sizeof(T)) {
210 return BAD_VALUE;
211 }
212
213 T *params = (T *)((uint8_t *)reply + sizeof(effect_param_t) + sizeof(uint32_t));
214 int numParams = 1;
215 if (MULTI_VALUES) {
216 numParams = (int)*params++;
217 }
218 if (numParams > kMaxEffectParamValues) {
219 return BAD_VALUE;
220 }
Eric Laurentc87402b2021-09-17 16:49:42 +0200221 (*values).clear();
Eric Laurent2be8b292021-08-23 09:44:33 -0700222 std::copy(&params[0], &params[numParams], back_inserter(*values));
223 return NO_ERROR;
224 }
225
226 /**
227 * Set a parameter to spatializer engine by calling setParameter on mEngine AudioEffect object.
228 * It is possible to pass more than one value of type T according to the parameter type
229 * according to values vector size.
230 */
231 template<typename T>
232 status_t setEffectParameter_l(uint32_t type, const std::vector<T>& values) REQUIRES(mLock) {
233 static_assert(sizeof(T) <= sizeof(uint32_t), "The size of T must less than 32 bits");
234
235 uint32_t cmd[sizeof(effect_param_t) / sizeof(uint32_t) + 1 + values.size()];
236 effect_param_t *p = (effect_param_t *)cmd;
237 p->psize = sizeof(uint32_t);
238 p->vsize = sizeof(T) * values.size();
239 *(uint32_t *)p->data = type;
240 memcpy((uint32_t *)p->data + 1, values.data(), sizeof(T) * values.size());
241
Eric Laurentc87402b2021-09-17 16:49:42 +0200242 status_t status = mEngine->setParameter(p);
243 if (status != NO_ERROR) {
244 return status;
245 }
246 if (p->status != NO_ERROR) {
247 return p->status;
248 }
249 return NO_ERROR;
250 }
251
252 /**
253 * Get a parameter from spatializer engine by calling getParameter on AudioEffect object.
254 * It is possible to read more than one value of type T according to the parameter type
255 * by specifying values vector size.
256 */
257 template<typename T>
258 status_t getEffectParameter_l(uint32_t type, std::vector<T> *values) REQUIRES(mLock) {
259 static_assert(sizeof(T) <= sizeof(uint32_t), "The size of T must less than 32 bits");
260
261 uint32_t cmd[sizeof(effect_param_t) / sizeof(uint32_t) + 1 + values->size()];
262 effect_param_t *p = (effect_param_t *)cmd;
263 p->psize = sizeof(uint32_t);
264 p->vsize = sizeof(T) * values->size();
265 *(uint32_t *)p->data = type;
266
267 status_t status = mEngine->getParameter(p);
268
269 if (status != NO_ERROR) {
270 return status;
271 }
272 if (p->status != NO_ERROR) {
273 return p->status;
274 }
275
276 int numValues = std::min(p->vsize / sizeof(T), values->size());
277 (*values).clear();
278 T *retValues = (T *)((uint8_t *)p->data + sizeof(uint32_t));
279 std::copy(&retValues[0], &retValues[numValues], back_inserter(*values));
280
281 return NO_ERROR;
Eric Laurent2be8b292021-08-23 09:44:33 -0700282 }
283
Eric Laurent8a4259f2021-09-14 16:04:00 +0200284 void postFramesProcessedMsg(int frames);
285
Eric Laurent9249d342022-03-18 11:55:56 +0100286 /**
287 * Checks if head and screen sensors must be actively monitored based on
288 * spatializer state and playback activity and configures the pose controller
289 * accordingly.
290 */
291 void checkSensorsState_l() REQUIRES(mLock);
Eric Laurent15903592022-02-24 20:44:36 +0100292
Eric Laurent7ea0d1b2022-04-01 14:23:44 +0200293 /**
Eric Laurent11094172022-04-05 18:27:42 +0200294 * Checks if the head pose controller should be created or destroyed according
295 * to desired head tracking mode.
296 */
297 void checkPoseController_l() REQUIRES(mLock);
298
299 /**
Eric Laurent7ea0d1b2022-04-01 14:23:44 +0200300 * Checks if the spatializer effect should be enabled based on
301 * playback activity and requested level.
302 */
303 void checkEngineState_l() REQUIRES(mLock);
304
Eric Laurent6d607012021-07-05 11:54:40 +0200305 /** Effect engine descriptor */
306 const effect_descriptor_t mEngineDescriptor;
307 /** Callback interface to parent audio policy service */
Andy Hunga461a002022-05-17 10:36:02 -0700308 SpatializerPolicyCallback* const mPolicyCallback;
309
310 /** Currently there is only one version of the spatializer running */
311 const std::string mMetricsId = AMEDIAMETRICS_KEY_PREFIX_AUDIO_SPATIALIZER "0";
Eric Laurent6d607012021-07-05 11:54:40 +0200312
313 /** Mutex protecting internal state */
Eric Laurent2be8b292021-08-23 09:44:33 -0700314 mutable std::mutex mLock;
Eric Laurent6d607012021-07-05 11:54:40 +0200315
316 /** Client AudioEffect for the engine */
317 sp<AudioEffect> mEngine GUARDED_BY(mLock);
318 /** Output stream the spatializer mixer thread is attached to */
319 audio_io_handle_t mOutput GUARDED_BY(mLock) = AUDIO_IO_HANDLE_NONE;
Eric Laurent6d607012021-07-05 11:54:40 +0200320
321 /** Callback interface to the client (AudioService) controlling this`Spatializer */
322 sp<media::INativeSpatializerCallback> mSpatializerCallback GUARDED_BY(mLock);
323
Eric Laurent67816e32021-09-16 15:18:40 +0200324 /** Callback interface for head tracking */
325 sp<media::ISpatializerHeadTrackingCallback> mHeadTrackingCallback GUARDED_BY(mLock);
326
Eric Laurent6d607012021-07-05 11:54:40 +0200327 /** Requested spatialization level */
328 media::SpatializationLevel mLevel GUARDED_BY(mLock) = media::SpatializationLevel::NONE;
Eric Laurent6d607012021-07-05 11:54:40 +0200329
Eric Laurent2be8b292021-08-23 09:44:33 -0700330 /** Control logic for head-tracking, etc. */
331 std::shared_ptr<SpatializerPoseController> mPoseController GUARDED_BY(mLock);
332
333 /** Last requested head tracking mode */
334 media::HeadTrackingMode mDesiredHeadTrackingMode GUARDED_BY(mLock)
335 = media::HeadTrackingMode::STATIC;
336
337 /** Last-reported actual head-tracking mode. */
338 media::SpatializerHeadTrackingMode mActualHeadTrackingMode GUARDED_BY(mLock)
339 = media::SpatializerHeadTrackingMode::DISABLED;
340
341 /** Selected Head pose sensor */
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700342 int32_t mHeadSensor GUARDED_BY(mLock) = SpatializerPoseController::INVALID_SENSOR;
Eric Laurent2be8b292021-08-23 09:44:33 -0700343
344 /** Selected Screen pose sensor */
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700345 int32_t mScreenSensor GUARDED_BY(mLock) = SpatializerPoseController::INVALID_SENSOR;
Eric Laurent2be8b292021-08-23 09:44:33 -0700346
347 /** Last display orientation received */
348 static constexpr float kDisplayOrientationInvalid = 1000;
349 float mDisplayOrientation GUARDED_BY(mLock) = kDisplayOrientationInvalid;
350
351 std::vector<media::SpatializationLevel> mLevels;
Andy Hunga461a002022-05-17 10:36:02 -0700352 std::vector<media::SpatializerHeadTrackingMode> mHeadTrackingModes;
Eric Laurent2be8b292021-08-23 09:44:33 -0700353 std::vector<media::SpatializationMode> mSpatializationModes;
354 std::vector<audio_channel_mask_t> mChannelMasks;
355 bool mSupportsHeadTracking;
Eric Laurent8a4259f2021-09-14 16:04:00 +0200356
357 // Looper thread for mEngine callbacks
358 class EngineCallbackHandler;
359
360 sp<ALooper> mLooper;
361 sp<EngineCallbackHandler> mHandler;
362
Eric Laurent15903592022-02-24 20:44:36 +0100363 size_t mNumActiveTracks GUARDED_BY(mLock) = 0;
Eric Laurentee398ad2022-05-03 18:19:35 +0200364 std::vector<audio_latency_mode_t> mSupportedLatencyModes GUARDED_BY(mLock);
Eric Laurent15903592022-02-24 20:44:36 +0100365
Eric Laurent8a4259f2021-09-14 16:04:00 +0200366 static const std::vector<const char *> sHeadPoseKeys;
Eric Laurent6d607012021-07-05 11:54:40 +0200367};
368
369
370}; // namespace android
371
372#endif // ANDROID_MEDIA_SPATIALIZER_H