blob: 54d9094e018392cb9723d7da98663658b3cc850b [file] [log] [blame]
Eric Laurent6d607012021-07-05 11:54:40 +02001/*
2**
3** Copyright 2021, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "Spatializer"
20//#define LOG_NDEBUG 0
21#include <utils/Log.h>
22
23#include <limits.h>
24#include <stdint.h>
25#include <sys/types.h>
26
27#include <android/content/AttributionSourceState.h>
28#include <audio_utils/fixedfft.h>
29#include <cutils/bitops.h>
Eric Laurent2be8b292021-08-23 09:44:33 -070030#include <hardware/sensors.h>
Eric Laurent6d607012021-07-05 11:54:40 +020031#include <media/audiohal/EffectsFactoryHalInterface.h>
Eric Laurent8a4259f2021-09-14 16:04:00 +020032#include <media/stagefright/foundation/AHandler.h>
33#include <media/stagefright/foundation/AMessage.h>
34#include <media/ShmemCompat.h>
Eric Laurent6d607012021-07-05 11:54:40 +020035#include <mediautils/ServiceUtilities.h>
36#include <utils/Thread.h>
37
38#include "Spatializer.h"
39
40namespace android {
41
42using aidl_utils::statusTFromBinderStatus;
43using aidl_utils::binderStatusFromStatusT;
44using android::content::AttributionSourceState;
45using binder::Status;
Eric Laurent2be8b292021-08-23 09:44:33 -070046using media::HeadTrackingMode;
47using media::Pose3f;
Eric Laurent6d607012021-07-05 11:54:40 +020048using media::SpatializationLevel;
Eric Laurent2be8b292021-08-23 09:44:33 -070049using media::SpatializationMode;
Ytai Ben-Tsvia16a9df2021-08-05 08:57:06 -070050using media::SpatializerHeadTrackingMode;
Eric Laurent2be8b292021-08-23 09:44:33 -070051using media::SensorPoseProvider;
52
Eric Laurent2be8b292021-08-23 09:44:33 -070053using namespace std::chrono_literals;
Eric Laurent6d607012021-07-05 11:54:40 +020054
55#define VALUE_OR_RETURN_BINDER_STATUS(x) \
56 ({ auto _tmp = (x); \
57 if (!_tmp.ok()) return aidl_utils::binderStatusFromStatusT(_tmp.error()); \
58 std::move(_tmp.value()); })
59
Eric Laurent6d607012021-07-05 11:54:40 +020060// ---------------------------------------------------------------------------
61
Eric Laurent8a4259f2021-09-14 16:04:00 +020062class Spatializer::EngineCallbackHandler : public AHandler {
63public:
64 EngineCallbackHandler(wp<Spatializer> spatializer)
65 : mSpatializer(spatializer) {
66 }
67
68 enum {
69 // Device state callbacks
70 kWhatOnFramesProcessed, // AudioEffect::EVENT_FRAMES_PROCESSED
71 kWhatOnHeadToStagePose, // SpatializerPoseController::Listener::onHeadToStagePose
72 kWhatOnActualModeChange, // SpatializerPoseController::Listener::onActualModeChange
73 };
74 static constexpr const char *kNumFramesKey = "numFrames";
75 static constexpr const char *kModeKey = "mode";
76 static constexpr const char *kTranslation0Key = "translation0";
77 static constexpr const char *kTranslation1Key = "translation1";
78 static constexpr const char *kTranslation2Key = "translation2";
79 static constexpr const char *kRotation0Key = "rotation0";
80 static constexpr const char *kRotation1Key = "rotation1";
81 static constexpr const char *kRotation2Key = "rotation2";
82
83 void onMessageReceived(const sp<AMessage> &msg) override {
84 switch (msg->what()) {
85 case kWhatOnFramesProcessed: {
86 sp<Spatializer> spatializer = mSpatializer.promote();
87 if (spatializer == nullptr) {
88 ALOGW("%s: Cannot promote spatializer", __func__);
89 return;
90 }
91 int numFrames;
92 if (!msg->findInt32(kNumFramesKey, &numFrames)) {
93 ALOGE("%s: Cannot find num frames!", __func__);
94 return;
95 }
96 if (numFrames > 0) {
97 spatializer->calculateHeadPose();
98 }
99 } break;
100 case kWhatOnHeadToStagePose: {
101 sp<Spatializer> spatializer = mSpatializer.promote();
102 if (spatializer == nullptr) {
103 ALOGW("%s: Cannot promote spatializer", __func__);
104 return;
105 }
106 std::vector<float> headToStage(sHeadPoseKeys.size());
107 for (size_t i = 0 ; i < sHeadPoseKeys.size(); i++) {
108 if (!msg->findFloat(sHeadPoseKeys[i], &headToStage[i])) {
109 ALOGE("%s: Cannot find kTranslation0Key!", __func__);
110 return;
111 }
112 }
113 spatializer->onHeadToStagePoseMsg(headToStage);
114 } break;
115 case kWhatOnActualModeChange: {
116 sp<Spatializer> spatializer = mSpatializer.promote();
117 if (spatializer == nullptr) {
118 ALOGW("%s: Cannot promote spatializer", __func__);
119 return;
120 }
121 int mode;
122 if (!msg->findInt32(EngineCallbackHandler::kModeKey, &mode)) {
123 ALOGE("%s: Cannot find actualMode!", __func__);
124 return;
125 }
126 spatializer->onActualModeChangeMsg(static_cast<HeadTrackingMode>(mode));
127 } break;
128 default:
129 LOG_ALWAYS_FATAL("Invalid callback message %d", msg->what());
130 }
131 }
132private:
133 wp<Spatializer> mSpatializer;
134};
135
136const std::vector<const char *> Spatializer::sHeadPoseKeys = {
137 Spatializer::EngineCallbackHandler::kTranslation0Key,
138 Spatializer::EngineCallbackHandler::kTranslation1Key,
139 Spatializer::EngineCallbackHandler::kTranslation2Key,
140 Spatializer::EngineCallbackHandler::kRotation0Key,
141 Spatializer::EngineCallbackHandler::kRotation1Key,
142 Spatializer::EngineCallbackHandler::kRotation2Key,
143};
144
145// ---------------------------------------------------------------------------
Eric Laurent6d607012021-07-05 11:54:40 +0200146sp<Spatializer> Spatializer::create(SpatializerPolicyCallback *callback) {
147 sp<Spatializer> spatializer;
148
149 sp<EffectsFactoryHalInterface> effectsFactoryHal = EffectsFactoryHalInterface::create();
150 if (effectsFactoryHal == nullptr) {
151 ALOGW("%s failed to create effect factory interface", __func__);
152 return spatializer;
153 }
154
155 std::vector<effect_descriptor_t> descriptors;
156 status_t status =
Eric Laurent1c5e2e32021-08-18 18:50:28 +0200157 effectsFactoryHal->getDescriptors(FX_IID_SPATIALIZER, &descriptors);
Eric Laurent6d607012021-07-05 11:54:40 +0200158 if (status != NO_ERROR) {
159 ALOGW("%s failed to get spatializer descriptor, error %d", __func__, status);
160 return spatializer;
161 }
162 ALOG_ASSERT(!descriptors.empty(),
163 "%s getDescriptors() returned no error but empty list", __func__);
164
165 //TODO: get supported spatialization modes from FX engine or descriptor
166
167 sp<EffectHalInterface> effect;
168 status = effectsFactoryHal->createEffect(&descriptors[0].uuid, AUDIO_SESSION_OUTPUT_STAGE,
169 AUDIO_IO_HANDLE_NONE, AUDIO_PORT_HANDLE_NONE, &effect);
170 ALOGI("%s FX create status %d effect %p", __func__, status, effect.get());
171
172 if (status == NO_ERROR && effect != nullptr) {
173 spatializer = new Spatializer(descriptors[0], callback);
Eric Laurent2be8b292021-08-23 09:44:33 -0700174 if (spatializer->loadEngineConfiguration(effect) != NO_ERROR) {
175 spatializer.clear();
176 }
Eric Laurent6d607012021-07-05 11:54:40 +0200177 }
178
179 return spatializer;
180}
181
Eric Laurent2be8b292021-08-23 09:44:33 -0700182Spatializer::Spatializer(effect_descriptor_t engineDescriptor, SpatializerPolicyCallback* callback)
183 : mEngineDescriptor(engineDescriptor),
184 mPolicyCallback(callback) {
Eric Laurent6d607012021-07-05 11:54:40 +0200185 ALOGV("%s", __func__);
186}
187
Eric Laurent8a4259f2021-09-14 16:04:00 +0200188void Spatializer::onFirstRef() {
189 mLooper = new ALooper;
190 mLooper->setName("Spatializer-looper");
191 mLooper->start(
192 /*runOnCallingThread*/false,
193 /*canCallJava*/ false,
194 PRIORITY_AUDIO);
195
196 mHandler = new EngineCallbackHandler(this);
197 mLooper->registerHandler(mHandler);
198}
199
Eric Laurent6d607012021-07-05 11:54:40 +0200200Spatializer::~Spatializer() {
201 ALOGV("%s", __func__);
Eric Laurent8a4259f2021-09-14 16:04:00 +0200202 if (mLooper != nullptr) {
203 mLooper->stop();
204 mLooper->unregisterHandler(mHandler->id());
205 }
206 mLooper.clear();
207 mHandler.clear();
Eric Laurent6d607012021-07-05 11:54:40 +0200208}
209
Eric Laurent2be8b292021-08-23 09:44:33 -0700210status_t Spatializer::loadEngineConfiguration(sp<EffectHalInterface> effect) {
211 ALOGV("%s", __func__);
212
213 std::vector<bool> supportsHeadTracking;
214 status_t status = getHalParameter<false>(effect, SPATIALIZER_PARAM_HEADTRACKING_SUPPORTED,
215 &supportsHeadTracking);
216 if (status != NO_ERROR) {
217 return status;
218 }
219 mSupportsHeadTracking = supportsHeadTracking[0];
220
221 status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_LEVELS, &mLevels);
222 if (status != NO_ERROR) {
223 return status;
224 }
225 status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES,
226 &mSpatializationModes);
227 if (status != NO_ERROR) {
228 return status;
229 }
Eric Laurentb4f42a92022-01-17 17:37:31 +0100230 return getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS,
Eric Laurent2be8b292021-08-23 09:44:33 -0700231 &mChannelMasks);
Eric Laurent2be8b292021-08-23 09:44:33 -0700232}
233
234/** Gets the channel mask, sampling rate and format set for the spatializer input. */
235audio_config_base_t Spatializer::getAudioInConfig() const {
236 std::lock_guard lock(mLock);
237 audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER;
238 // For now use highest supported channel count
239 uint32_t maxCount = 0;
240 for ( auto mask : mChannelMasks) {
241 if (audio_channel_count_from_out_mask(mask) > maxCount) {
242 config.channel_mask = mask;
243 }
244 }
245 return config;
246}
247
Eric Laurent6d607012021-07-05 11:54:40 +0200248status_t Spatializer::registerCallback(
249 const sp<media::INativeSpatializerCallback>& callback) {
Eric Laurent2be8b292021-08-23 09:44:33 -0700250 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200251 if (callback == nullptr) {
252 return BAD_VALUE;
253 }
254
255 sp<IBinder> binder = IInterface::asBinder(callback);
256 status_t status = binder->linkToDeath(this);
257 if (status == NO_ERROR) {
258 mSpatializerCallback = callback;
259 }
260 ALOGV("%s status %d", __func__, status);
261 return status;
262}
263
264// IBinder::DeathRecipient
265void Spatializer::binderDied(__unused const wp<IBinder> &who) {
266 {
Eric Laurent2be8b292021-08-23 09:44:33 -0700267 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200268 mLevel = SpatializationLevel::NONE;
269 mSpatializerCallback.clear();
270 }
271 ALOGV("%s", __func__);
272 mPolicyCallback->onCheckSpatializer();
273}
274
275// ISpatializer
276Status Spatializer::getSupportedLevels(std::vector<SpatializationLevel> *levels) {
277 ALOGV("%s", __func__);
278 if (levels == nullptr) {
279 return binderStatusFromStatusT(BAD_VALUE);
280 }
Eric Laurent6d607012021-07-05 11:54:40 +0200281 levels->push_back(SpatializationLevel::NONE);
Eric Laurent2be8b292021-08-23 09:44:33 -0700282 levels->insert(levels->end(), mLevels.begin(), mLevels.end());
Eric Laurent6d607012021-07-05 11:54:40 +0200283 return Status::ok();
284}
285
Eric Laurent2be8b292021-08-23 09:44:33 -0700286Status Spatializer::setLevel(SpatializationLevel level) {
Eric Laurent6d607012021-07-05 11:54:40 +0200287 ALOGV("%s level %d", __func__, (int)level);
288 if (level != SpatializationLevel::NONE
Eric Laurent2be8b292021-08-23 09:44:33 -0700289 && std::find(mLevels.begin(), mLevels.end(), level) == mLevels.end()) {
Eric Laurent6d607012021-07-05 11:54:40 +0200290 return binderStatusFromStatusT(BAD_VALUE);
291 }
292 sp<media::INativeSpatializerCallback> callback;
293 bool levelChanged = false;
294 {
Eric Laurent2be8b292021-08-23 09:44:33 -0700295 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200296 levelChanged = mLevel != level;
297 mLevel = level;
298 callback = mSpatializerCallback;
Eric Laurent2be8b292021-08-23 09:44:33 -0700299
300 if (levelChanged && mEngine != nullptr) {
301 setEffectParameter_l(SPATIALIZER_PARAM_LEVEL, std::vector<SpatializationLevel>{level});
302 }
Eric Laurent6d607012021-07-05 11:54:40 +0200303 }
304
305 if (levelChanged) {
306 mPolicyCallback->onCheckSpatializer();
307 if (callback != nullptr) {
308 callback->onLevelChanged(level);
309 }
310 }
311 return Status::ok();
312}
313
Eric Laurent2be8b292021-08-23 09:44:33 -0700314Status Spatializer::getLevel(SpatializationLevel *level) {
Eric Laurent6d607012021-07-05 11:54:40 +0200315 if (level == nullptr) {
316 return binderStatusFromStatusT(BAD_VALUE);
317 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700318 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200319 *level = mLevel;
320 ALOGV("%s level %d", __func__, (int)*level);
321 return Status::ok();
322}
323
Eric Laurentc87402b2021-09-17 16:49:42 +0200324Status Spatializer::isHeadTrackingSupported(bool *supports) {
325 ALOGV("%s mSupportsHeadTracking %d", __func__, mSupportsHeadTracking);
326 if (supports == nullptr) {
327 return binderStatusFromStatusT(BAD_VALUE);
328 }
329 std::lock_guard lock(mLock);
330 *supports = mSupportsHeadTracking;
331 return Status::ok();
332}
333
Eric Laurent6d607012021-07-05 11:54:40 +0200334Status Spatializer::getSupportedHeadTrackingModes(
Eric Laurent2be8b292021-08-23 09:44:33 -0700335 std::vector<SpatializerHeadTrackingMode>* modes) {
336 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200337 ALOGV("%s", __func__);
338 if (modes == nullptr) {
339 return binderStatusFromStatusT(BAD_VALUE);
340 }
Eric Laurent6d607012021-07-05 11:54:40 +0200341
Eric Laurent2be8b292021-08-23 09:44:33 -0700342 modes->push_back(SpatializerHeadTrackingMode::DISABLED);
343 if (mSupportsHeadTracking) {
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700344 if (mHeadSensor != SpatializerPoseController::INVALID_SENSOR) {
Eric Laurent2be8b292021-08-23 09:44:33 -0700345 modes->push_back(SpatializerHeadTrackingMode::RELATIVE_WORLD);
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700346 if (mScreenSensor != SpatializerPoseController::INVALID_SENSOR) {
Eric Laurent2be8b292021-08-23 09:44:33 -0700347 modes->push_back(SpatializerHeadTrackingMode::RELATIVE_SCREEN);
348 }
349 }
Eric Laurent6d607012021-07-05 11:54:40 +0200350 }
351 return Status::ok();
352}
353
Eric Laurent2be8b292021-08-23 09:44:33 -0700354Status Spatializer::setDesiredHeadTrackingMode(SpatializerHeadTrackingMode mode) {
355 ALOGV("%s mode %d", __func__, (int)mode);
356
357 if (!mSupportsHeadTracking) {
358 return binderStatusFromStatusT(INVALID_OPERATION);
359 }
360 std::lock_guard lock(mLock);
361 switch (mode) {
362 case SpatializerHeadTrackingMode::OTHER:
363 return binderStatusFromStatusT(BAD_VALUE);
364 case SpatializerHeadTrackingMode::DISABLED:
365 mDesiredHeadTrackingMode = HeadTrackingMode::STATIC;
366 break;
367 case SpatializerHeadTrackingMode::RELATIVE_WORLD:
368 mDesiredHeadTrackingMode = HeadTrackingMode::WORLD_RELATIVE;
369 break;
370 case SpatializerHeadTrackingMode::RELATIVE_SCREEN:
371 mDesiredHeadTrackingMode = HeadTrackingMode::SCREEN_RELATIVE;
372 break;
373 }
374
375 if (mPoseController != nullptr) {
376 mPoseController->setDesiredMode(mDesiredHeadTrackingMode);
377 }
378
379 return Status::ok();
380}
381
382Status Spatializer::getActualHeadTrackingMode(SpatializerHeadTrackingMode *mode) {
Eric Laurent6d607012021-07-05 11:54:40 +0200383 if (mode == nullptr) {
384 return binderStatusFromStatusT(BAD_VALUE);
385 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700386 std::lock_guard lock(mLock);
387 *mode = mActualHeadTrackingMode;
Eric Laurent6d607012021-07-05 11:54:40 +0200388 ALOGV("%s mode %d", __func__, (int)*mode);
389 return Status::ok();
390}
391
Ytai Ben-Tsvia16a9df2021-08-05 08:57:06 -0700392Status Spatializer::recenterHeadTracker() {
Eric Laurent780be4a2021-09-16 10:44:24 +0200393 if (!mSupportsHeadTracking) {
394 return binderStatusFromStatusT(INVALID_OPERATION);
395 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700396 std::lock_guard lock(mLock);
397 if (mPoseController != nullptr) {
398 mPoseController->recenter();
399 }
Eric Laurent6d607012021-07-05 11:54:40 +0200400 return Status::ok();
401}
402
403Status Spatializer::setGlobalTransform(const std::vector<float>& screenToStage) {
Eric Laurent6d607012021-07-05 11:54:40 +0200404 ALOGV("%s", __func__);
Eric Laurent780be4a2021-09-16 10:44:24 +0200405 if (!mSupportsHeadTracking) {
406 return binderStatusFromStatusT(INVALID_OPERATION);
407 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700408 std::optional<Pose3f> maybePose = Pose3f::fromVector(screenToStage);
409 if (!maybePose.has_value()) {
410 ALOGW("Invalid screenToStage vector.");
411 return binderStatusFromStatusT(BAD_VALUE);
412 }
413 std::lock_guard lock(mLock);
414 if (mPoseController != nullptr) {
415 mPoseController->setScreenToStagePose(maybePose.value());
416 }
Eric Laurent6d607012021-07-05 11:54:40 +0200417 return Status::ok();
418}
419
420Status Spatializer::release() {
421 ALOGV("%s", __func__);
422 bool levelChanged = false;
423 {
Eric Laurent2be8b292021-08-23 09:44:33 -0700424 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200425 if (mSpatializerCallback == nullptr) {
426 return binderStatusFromStatusT(INVALID_OPERATION);
427 }
428
429 sp<IBinder> binder = IInterface::asBinder(mSpatializerCallback);
430 binder->unlinkToDeath(this);
431 mSpatializerCallback.clear();
432
433 levelChanged = mLevel != SpatializationLevel::NONE;
434 mLevel = SpatializationLevel::NONE;
435 }
436
437 if (levelChanged) {
438 mPolicyCallback->onCheckSpatializer();
439 }
440 return Status::ok();
441}
442
Eric Laurent2be8b292021-08-23 09:44:33 -0700443Status Spatializer::setHeadSensor(int sensorHandle) {
444 ALOGV("%s sensorHandle %d", __func__, sensorHandle);
Eric Laurent780be4a2021-09-16 10:44:24 +0200445 if (!mSupportsHeadTracking) {
446 return binderStatusFromStatusT(INVALID_OPERATION);
447 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700448 std::lock_guard lock(mLock);
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700449 mHeadSensor = sensorHandle;
Eric Laurent2be8b292021-08-23 09:44:33 -0700450 if (mPoseController != nullptr) {
451 mPoseController->setHeadSensor(mHeadSensor);
452 }
453 return Status::ok();
454}
455
456Status Spatializer::setScreenSensor(int sensorHandle) {
457 ALOGV("%s sensorHandle %d", __func__, sensorHandle);
Eric Laurent780be4a2021-09-16 10:44:24 +0200458 if (!mSupportsHeadTracking) {
459 return binderStatusFromStatusT(INVALID_OPERATION);
460 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700461 std::lock_guard lock(mLock);
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700462 mScreenSensor = sensorHandle;
Eric Laurent2be8b292021-08-23 09:44:33 -0700463 if (mPoseController != nullptr) {
464 mPoseController->setScreenSensor(mScreenSensor);
465 }
466 return Status::ok();
467}
468
469Status Spatializer::setDisplayOrientation(float physicalToLogicalAngle) {
470 ALOGV("%s physicalToLogicalAngle %f", __func__, physicalToLogicalAngle);
Eric Laurent780be4a2021-09-16 10:44:24 +0200471 if (!mSupportsHeadTracking) {
472 return binderStatusFromStatusT(INVALID_OPERATION);
473 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700474 std::lock_guard lock(mLock);
475 mDisplayOrientation = physicalToLogicalAngle;
476 if (mPoseController != nullptr) {
477 mPoseController->setDisplayOrientation(mDisplayOrientation);
478 }
Eric Laurent16ddaf42021-09-17 15:00:35 +0200479 if (mEngine != nullptr) {
480 setEffectParameter_l(
481 SPATIALIZER_PARAM_DISPLAY_ORIENTATION, std::vector<float>{physicalToLogicalAngle});
482 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700483 return Status::ok();
484}
485
486Status Spatializer::setHingeAngle(float hingeAngle) {
487 std::lock_guard lock(mLock);
488 ALOGV("%s hingeAngle %f", __func__, hingeAngle);
489 if (mEngine != nullptr) {
490 setEffectParameter_l(SPATIALIZER_PARAM_HINGE_ANGLE, std::vector<float>{hingeAngle});
491 }
492 return Status::ok();
493}
494
495Status Spatializer::getSupportedModes(std::vector<SpatializationMode> *modes) {
496 ALOGV("%s", __func__);
497 if (modes == nullptr) {
498 return binderStatusFromStatusT(BAD_VALUE);
499 }
500 *modes = mSpatializationModes;
501 return Status::ok();
502}
503
Eric Laurent67816e32021-09-16 15:18:40 +0200504Status Spatializer::registerHeadTrackingCallback(
505 const sp<media::ISpatializerHeadTrackingCallback>& callback) {
506 ALOGV("%s callback %p", __func__, callback.get());
507 std::lock_guard lock(mLock);
508 if (!mSupportsHeadTracking) {
509 return binderStatusFromStatusT(INVALID_OPERATION);
510 }
511 mHeadTrackingCallback = callback;
512 return Status::ok();
513}
514
Eric Laurentc87402b2021-09-17 16:49:42 +0200515Status Spatializer::setParameter(int key, const std::vector<unsigned char>& value) {
516 ALOGV("%s key %d", __func__, key);
517 std::lock_guard lock(mLock);
518 status_t status = INVALID_OPERATION;
519 if (mEngine != nullptr) {
520 status = setEffectParameter_l(key, value);
521 }
522 return binderStatusFromStatusT(status);
523}
524
525Status Spatializer::getParameter(int key, std::vector<unsigned char> *value) {
Greg Kaiserf7249f82021-09-21 07:10:12 -0700526 ALOGV("%s key %d value size %d", __func__, key,
527 (value != nullptr ? (int)value->size() : -1));
Eric Laurentc87402b2021-09-17 16:49:42 +0200528 if (value == nullptr) {
George Burgess IV22386222021-09-22 12:09:31 -0700529 return binderStatusFromStatusT(BAD_VALUE);
Eric Laurentc87402b2021-09-17 16:49:42 +0200530 }
531 std::lock_guard lock(mLock);
532 status_t status = INVALID_OPERATION;
533 if (mEngine != nullptr) {
534 ALOGV("%s key %d mEngine %p", __func__, key, mEngine.get());
535 status = getEffectParameter_l(key, value);
536 }
537 return binderStatusFromStatusT(status);
538}
539
540Status Spatializer::getOutput(int *output) {
541 ALOGV("%s", __func__);
542 if (output == nullptr) {
543 binderStatusFromStatusT(BAD_VALUE);
544 }
545 std::lock_guard lock(mLock);
546 *output = VALUE_OR_RETURN_BINDER_STATUS(legacy2aidl_audio_io_handle_t_int32_t(mOutput));
547 ALOGV("%s got output %d", __func__, *output);
548 return Status::ok();
549}
550
Eric Laurent2be8b292021-08-23 09:44:33 -0700551// SpatializerPoseController::Listener
552void Spatializer::onHeadToStagePose(const Pose3f& headToStage) {
553 ALOGV("%s", __func__);
Eric Laurent780be4a2021-09-16 10:44:24 +0200554 LOG_ALWAYS_FATAL_IF(!mSupportsHeadTracking,
555 "onHeadToStagePose() called with no head tracking support!");
556
Eric Laurent2be8b292021-08-23 09:44:33 -0700557 auto vec = headToStage.toVector();
Eric Laurent8a4259f2021-09-14 16:04:00 +0200558 LOG_ALWAYS_FATAL_IF(vec.size() != sHeadPoseKeys.size(),
559 "%s invalid head to stage vector size %zu", __func__, vec.size());
560
561 sp<AMessage> msg =
562 new AMessage(EngineCallbackHandler::kWhatOnHeadToStagePose, mHandler);
563 for (size_t i = 0 ; i < sHeadPoseKeys.size(); i++) {
564 msg->setFloat(sHeadPoseKeys[i], vec[i]);
565 }
566 msg->post();
567}
568
569void Spatializer::onHeadToStagePoseMsg(const std::vector<float>& headToStage) {
570 ALOGV("%s", __func__);
Eric Laurent67816e32021-09-16 15:18:40 +0200571 sp<media::ISpatializerHeadTrackingCallback> callback;
Eric Laurent2be8b292021-08-23 09:44:33 -0700572 {
573 std::lock_guard lock(mLock);
Eric Laurent67816e32021-09-16 15:18:40 +0200574 callback = mHeadTrackingCallback;
Eric Laurent2be8b292021-08-23 09:44:33 -0700575 if (mEngine != nullptr) {
Eric Laurent8a4259f2021-09-14 16:04:00 +0200576 setEffectParameter_l(SPATIALIZER_PARAM_HEAD_TO_STAGE, headToStage);
Eric Laurent2be8b292021-08-23 09:44:33 -0700577 }
578 }
579
580 if (callback != nullptr) {
Eric Laurent8a4259f2021-09-14 16:04:00 +0200581 callback->onHeadToSoundStagePoseUpdated(headToStage);
Eric Laurent2be8b292021-08-23 09:44:33 -0700582 }
583}
584
585void Spatializer::onActualModeChange(HeadTrackingMode mode) {
Eric Laurent8a4259f2021-09-14 16:04:00 +0200586 ALOGV("%s(%d)", __func__, (int)mode);
587 sp<AMessage> msg =
588 new AMessage(EngineCallbackHandler::kWhatOnActualModeChange, mHandler);
589 msg->setInt32(EngineCallbackHandler::kModeKey, static_cast<int>(mode));
590 msg->post();
591}
592
593void Spatializer::onActualModeChangeMsg(HeadTrackingMode mode) {
594 ALOGV("%s(%d)", __func__, (int) mode);
Eric Laurent67816e32021-09-16 15:18:40 +0200595 sp<media::ISpatializerHeadTrackingCallback> callback;
Eric Laurent2be8b292021-08-23 09:44:33 -0700596 SpatializerHeadTrackingMode spatializerMode;
597 {
598 std::lock_guard lock(mLock);
599 if (!mSupportsHeadTracking) {
600 spatializerMode = SpatializerHeadTrackingMode::DISABLED;
601 } else {
602 switch (mode) {
603 case HeadTrackingMode::STATIC:
604 spatializerMode = SpatializerHeadTrackingMode::DISABLED;
605 break;
606 case HeadTrackingMode::WORLD_RELATIVE:
607 spatializerMode = SpatializerHeadTrackingMode::RELATIVE_WORLD;
608 break;
609 case HeadTrackingMode::SCREEN_RELATIVE:
610 spatializerMode = SpatializerHeadTrackingMode::RELATIVE_SCREEN;
611 break;
612 default:
613 LOG_ALWAYS_FATAL("Unknown mode: %d", mode);
614 }
615 }
616 mActualHeadTrackingMode = spatializerMode;
Eric Laurent67816e32021-09-16 15:18:40 +0200617 callback = mHeadTrackingCallback;
Eric Laurent2be8b292021-08-23 09:44:33 -0700618 }
619 if (callback != nullptr) {
620 callback->onHeadTrackingModeChanged(spatializerMode);
621 }
622}
623
Eric Laurent6d607012021-07-05 11:54:40 +0200624status_t Spatializer::attachOutput(audio_io_handle_t output) {
Eric Laurent2be8b292021-08-23 09:44:33 -0700625 std::shared_ptr<SpatializerPoseController> poseController;
Eric Laurent4a872862021-10-11 17:06:47 +0200626 bool outputChanged = false;
627 sp<media::INativeSpatializerCallback> callback;
628
Eric Laurent2be8b292021-08-23 09:44:33 -0700629 {
630 std::lock_guard lock(mLock);
631 ALOGV("%s output %d mOutput %d", __func__, (int)output, (int)mOutput);
632 if (mOutput != AUDIO_IO_HANDLE_NONE) {
633 LOG_ALWAYS_FATAL_IF(mEngine == nullptr, "%s output set without FX engine", __func__);
634 // remove FX instance
635 mEngine->setEnabled(false);
636 mEngine.clear();
637 }
638 // create FX instance on output
639 AttributionSourceState attributionSource = AttributionSourceState();
640 mEngine = new AudioEffect(attributionSource);
641 mEngine->set(nullptr, &mEngineDescriptor.uuid, 0, Spatializer::engineCallback /* cbf */,
642 this /* user */, AUDIO_SESSION_OUTPUT_STAGE, output, {} /* device */,
643 false /* probe */, true /* notifyFramesProcessed */);
644 status_t status = mEngine->initCheck();
645 ALOGV("%s mEngine create status %d", __func__, (int)status);
646 if (status != NO_ERROR) {
647 return status;
648 }
649
650 setEffectParameter_l(SPATIALIZER_PARAM_LEVEL,
651 std::vector<SpatializationLevel>{mLevel});
652 setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_MODE,
653 std::vector<SpatializerHeadTrackingMode>{mActualHeadTrackingMode});
654
655 mEngine->setEnabled(true);
Eric Laurent4a872862021-10-11 17:06:47 +0200656 outputChanged = mOutput != output;
Eric Laurent2be8b292021-08-23 09:44:33 -0700657 mOutput = output;
658
Eric Laurent780be4a2021-09-16 10:44:24 +0200659 if (mSupportsHeadTracking) {
660 mPoseController = std::make_shared<SpatializerPoseController>(
661 static_cast<SpatializerPoseController::Listener*>(this), 10ms, 50ms);
662 LOG_ALWAYS_FATAL_IF(mPoseController == nullptr,
663 "%s could not allocate pose controller", __func__);
Eric Laurent2be8b292021-08-23 09:44:33 -0700664
Eric Laurent780be4a2021-09-16 10:44:24 +0200665 mPoseController->setDesiredMode(mDesiredHeadTrackingMode);
666 mPoseController->setHeadSensor(mHeadSensor);
667 mPoseController->setScreenSensor(mScreenSensor);
668 mPoseController->setDisplayOrientation(mDisplayOrientation);
669 poseController = mPoseController;
670 }
Eric Laurent4a872862021-10-11 17:06:47 +0200671 callback = mSpatializerCallback;
Eric Laurent6d607012021-07-05 11:54:40 +0200672 }
Eric Laurent780be4a2021-09-16 10:44:24 +0200673 if (poseController != nullptr) {
674 poseController->waitUntilCalculated();
675 }
Eric Laurent4a872862021-10-11 17:06:47 +0200676
677 if (outputChanged && callback != nullptr) {
678 callback->onOutputChanged(output);
679 }
680
Eric Laurent6d607012021-07-05 11:54:40 +0200681 return NO_ERROR;
682}
683
684audio_io_handle_t Spatializer::detachOutput() {
Eric Laurent2be8b292021-08-23 09:44:33 -0700685 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent4a872862021-10-11 17:06:47 +0200686 sp<media::INativeSpatializerCallback> callback;
687
688 {
689 std::lock_guard lock(mLock);
690 ALOGV("%s mOutput %d", __func__, (int)mOutput);
691 if (mOutput == AUDIO_IO_HANDLE_NONE) {
692 return output;
693 }
694 // remove FX instance
695 mEngine->setEnabled(false);
696 mEngine.clear();
697 output = mOutput;
698 mOutput = AUDIO_IO_HANDLE_NONE;
699 mPoseController.reset();
700
701 callback = mSpatializerCallback;
Eric Laurent6d607012021-07-05 11:54:40 +0200702 }
Eric Laurent4a872862021-10-11 17:06:47 +0200703
704 if (callback != nullptr) {
705 callback->onOutputChanged(AUDIO_IO_HANDLE_NONE);
706 }
Eric Laurent6d607012021-07-05 11:54:40 +0200707 return output;
708}
709
Eric Laurent2be8b292021-08-23 09:44:33 -0700710void Spatializer::calculateHeadPose() {
711 ALOGV("%s", __func__);
712 std::lock_guard lock(mLock);
713 if (mPoseController != nullptr) {
714 mPoseController->calculateAsync();
715 }
716}
Eric Laurent6d607012021-07-05 11:54:40 +0200717
Eric Laurent2be8b292021-08-23 09:44:33 -0700718void Spatializer::engineCallback(int32_t event, void *user, void *info) {
Eric Laurent6d607012021-07-05 11:54:40 +0200719 if (user == nullptr) {
720 return;
721 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700722 Spatializer* const me = reinterpret_cast<Spatializer *>(user);
Eric Laurent6d607012021-07-05 11:54:40 +0200723 switch (event) {
724 case AudioEffect::EVENT_FRAMES_PROCESSED: {
Eric Laurent2be8b292021-08-23 09:44:33 -0700725 int frames = info == nullptr ? 0 : *(int*)info;
Eric Laurent6d607012021-07-05 11:54:40 +0200726 ALOGD("%s frames processed %d for me %p", __func__, frames, me);
Eric Laurent8a4259f2021-09-14 16:04:00 +0200727 me->postFramesProcessedMsg(frames);
Eric Laurent2be8b292021-08-23 09:44:33 -0700728 } break;
Eric Laurent6d607012021-07-05 11:54:40 +0200729 default:
730 ALOGD("%s event %d", __func__, event);
731 break;
732 }
733}
734
Eric Laurent8a4259f2021-09-14 16:04:00 +0200735void Spatializer::postFramesProcessedMsg(int frames) {
736 sp<AMessage> msg =
737 new AMessage(EngineCallbackHandler::kWhatOnFramesProcessed, mHandler);
738 msg->setInt32(EngineCallbackHandler::kNumFramesKey, frames);
739 msg->post();
740}
741
Eric Laurent6d607012021-07-05 11:54:40 +0200742} // namespace android