blob: 502a8d08d510aaa34d8d7fd0ee8e05786c624a68 [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 }
230 status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS,
231 &mChannelMasks);
232 if (status != NO_ERROR) {
233 return status;
234 }
235 return NO_ERROR;
236}
237
238/** Gets the channel mask, sampling rate and format set for the spatializer input. */
239audio_config_base_t Spatializer::getAudioInConfig() const {
240 std::lock_guard lock(mLock);
241 audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER;
242 // For now use highest supported channel count
243 uint32_t maxCount = 0;
244 for ( auto mask : mChannelMasks) {
245 if (audio_channel_count_from_out_mask(mask) > maxCount) {
246 config.channel_mask = mask;
247 }
248 }
249 return config;
250}
251
Eric Laurent6d607012021-07-05 11:54:40 +0200252status_t Spatializer::registerCallback(
253 const sp<media::INativeSpatializerCallback>& callback) {
Eric Laurent2be8b292021-08-23 09:44:33 -0700254 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200255 if (callback == nullptr) {
256 return BAD_VALUE;
257 }
258
259 sp<IBinder> binder = IInterface::asBinder(callback);
260 status_t status = binder->linkToDeath(this);
261 if (status == NO_ERROR) {
262 mSpatializerCallback = callback;
263 }
264 ALOGV("%s status %d", __func__, status);
265 return status;
266}
267
268// IBinder::DeathRecipient
269void Spatializer::binderDied(__unused const wp<IBinder> &who) {
270 {
Eric Laurent2be8b292021-08-23 09:44:33 -0700271 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200272 mLevel = SpatializationLevel::NONE;
273 mSpatializerCallback.clear();
274 }
275 ALOGV("%s", __func__);
276 mPolicyCallback->onCheckSpatializer();
277}
278
279// ISpatializer
280Status Spatializer::getSupportedLevels(std::vector<SpatializationLevel> *levels) {
281 ALOGV("%s", __func__);
282 if (levels == nullptr) {
283 return binderStatusFromStatusT(BAD_VALUE);
284 }
Eric Laurent6d607012021-07-05 11:54:40 +0200285 levels->push_back(SpatializationLevel::NONE);
Eric Laurent2be8b292021-08-23 09:44:33 -0700286 levels->insert(levels->end(), mLevels.begin(), mLevels.end());
Eric Laurent6d607012021-07-05 11:54:40 +0200287 return Status::ok();
288}
289
Eric Laurent2be8b292021-08-23 09:44:33 -0700290Status Spatializer::setLevel(SpatializationLevel level) {
Eric Laurent6d607012021-07-05 11:54:40 +0200291 ALOGV("%s level %d", __func__, (int)level);
292 if (level != SpatializationLevel::NONE
Eric Laurent2be8b292021-08-23 09:44:33 -0700293 && std::find(mLevels.begin(), mLevels.end(), level) == mLevels.end()) {
Eric Laurent6d607012021-07-05 11:54:40 +0200294 return binderStatusFromStatusT(BAD_VALUE);
295 }
296 sp<media::INativeSpatializerCallback> callback;
297 bool levelChanged = false;
298 {
Eric Laurent2be8b292021-08-23 09:44:33 -0700299 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200300 levelChanged = mLevel != level;
301 mLevel = level;
302 callback = mSpatializerCallback;
Eric Laurent2be8b292021-08-23 09:44:33 -0700303
304 if (levelChanged && mEngine != nullptr) {
305 setEffectParameter_l(SPATIALIZER_PARAM_LEVEL, std::vector<SpatializationLevel>{level});
306 }
Eric Laurent6d607012021-07-05 11:54:40 +0200307 }
308
309 if (levelChanged) {
310 mPolicyCallback->onCheckSpatializer();
311 if (callback != nullptr) {
312 callback->onLevelChanged(level);
313 }
314 }
315 return Status::ok();
316}
317
Eric Laurent2be8b292021-08-23 09:44:33 -0700318Status Spatializer::getLevel(SpatializationLevel *level) {
Eric Laurent6d607012021-07-05 11:54:40 +0200319 if (level == nullptr) {
320 return binderStatusFromStatusT(BAD_VALUE);
321 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700322 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200323 *level = mLevel;
324 ALOGV("%s level %d", __func__, (int)*level);
325 return Status::ok();
326}
327
Eric Laurentc87402b2021-09-17 16:49:42 +0200328Status Spatializer::isHeadTrackingSupported(bool *supports) {
329 ALOGV("%s mSupportsHeadTracking %d", __func__, mSupportsHeadTracking);
330 if (supports == nullptr) {
331 return binderStatusFromStatusT(BAD_VALUE);
332 }
333 std::lock_guard lock(mLock);
334 *supports = mSupportsHeadTracking;
335 return Status::ok();
336}
337
Eric Laurent6d607012021-07-05 11:54:40 +0200338Status Spatializer::getSupportedHeadTrackingModes(
Eric Laurent2be8b292021-08-23 09:44:33 -0700339 std::vector<SpatializerHeadTrackingMode>* modes) {
340 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200341 ALOGV("%s", __func__);
342 if (modes == nullptr) {
343 return binderStatusFromStatusT(BAD_VALUE);
344 }
Eric Laurent6d607012021-07-05 11:54:40 +0200345
Eric Laurent2be8b292021-08-23 09:44:33 -0700346 modes->push_back(SpatializerHeadTrackingMode::DISABLED);
347 if (mSupportsHeadTracking) {
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700348 if (mHeadSensor != SpatializerPoseController::INVALID_SENSOR) {
Eric Laurent2be8b292021-08-23 09:44:33 -0700349 modes->push_back(SpatializerHeadTrackingMode::RELATIVE_WORLD);
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700350 if (mScreenSensor != SpatializerPoseController::INVALID_SENSOR) {
Eric Laurent2be8b292021-08-23 09:44:33 -0700351 modes->push_back(SpatializerHeadTrackingMode::RELATIVE_SCREEN);
352 }
353 }
Eric Laurent6d607012021-07-05 11:54:40 +0200354 }
355 return Status::ok();
356}
357
Eric Laurent2be8b292021-08-23 09:44:33 -0700358Status Spatializer::setDesiredHeadTrackingMode(SpatializerHeadTrackingMode mode) {
359 ALOGV("%s mode %d", __func__, (int)mode);
360
361 if (!mSupportsHeadTracking) {
362 return binderStatusFromStatusT(INVALID_OPERATION);
363 }
364 std::lock_guard lock(mLock);
365 switch (mode) {
366 case SpatializerHeadTrackingMode::OTHER:
367 return binderStatusFromStatusT(BAD_VALUE);
368 case SpatializerHeadTrackingMode::DISABLED:
369 mDesiredHeadTrackingMode = HeadTrackingMode::STATIC;
370 break;
371 case SpatializerHeadTrackingMode::RELATIVE_WORLD:
372 mDesiredHeadTrackingMode = HeadTrackingMode::WORLD_RELATIVE;
373 break;
374 case SpatializerHeadTrackingMode::RELATIVE_SCREEN:
375 mDesiredHeadTrackingMode = HeadTrackingMode::SCREEN_RELATIVE;
376 break;
377 }
378
379 if (mPoseController != nullptr) {
380 mPoseController->setDesiredMode(mDesiredHeadTrackingMode);
381 }
382
383 return Status::ok();
384}
385
386Status Spatializer::getActualHeadTrackingMode(SpatializerHeadTrackingMode *mode) {
Eric Laurent6d607012021-07-05 11:54:40 +0200387 if (mode == nullptr) {
388 return binderStatusFromStatusT(BAD_VALUE);
389 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700390 std::lock_guard lock(mLock);
391 *mode = mActualHeadTrackingMode;
Eric Laurent6d607012021-07-05 11:54:40 +0200392 ALOGV("%s mode %d", __func__, (int)*mode);
393 return Status::ok();
394}
395
Ytai Ben-Tsvia16a9df2021-08-05 08:57:06 -0700396Status Spatializer::recenterHeadTracker() {
Eric Laurent780be4a2021-09-16 10:44:24 +0200397 if (!mSupportsHeadTracking) {
398 return binderStatusFromStatusT(INVALID_OPERATION);
399 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700400 std::lock_guard lock(mLock);
401 if (mPoseController != nullptr) {
402 mPoseController->recenter();
403 }
Eric Laurent6d607012021-07-05 11:54:40 +0200404 return Status::ok();
405}
406
407Status Spatializer::setGlobalTransform(const std::vector<float>& screenToStage) {
Eric Laurent6d607012021-07-05 11:54:40 +0200408 ALOGV("%s", __func__);
Eric Laurent780be4a2021-09-16 10:44:24 +0200409 if (!mSupportsHeadTracking) {
410 return binderStatusFromStatusT(INVALID_OPERATION);
411 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700412 std::optional<Pose3f> maybePose = Pose3f::fromVector(screenToStage);
413 if (!maybePose.has_value()) {
414 ALOGW("Invalid screenToStage vector.");
415 return binderStatusFromStatusT(BAD_VALUE);
416 }
417 std::lock_guard lock(mLock);
418 if (mPoseController != nullptr) {
419 mPoseController->setScreenToStagePose(maybePose.value());
420 }
Eric Laurent6d607012021-07-05 11:54:40 +0200421 return Status::ok();
422}
423
424Status Spatializer::release() {
425 ALOGV("%s", __func__);
426 bool levelChanged = false;
427 {
Eric Laurent2be8b292021-08-23 09:44:33 -0700428 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200429 if (mSpatializerCallback == nullptr) {
430 return binderStatusFromStatusT(INVALID_OPERATION);
431 }
432
433 sp<IBinder> binder = IInterface::asBinder(mSpatializerCallback);
434 binder->unlinkToDeath(this);
435 mSpatializerCallback.clear();
436
437 levelChanged = mLevel != SpatializationLevel::NONE;
438 mLevel = SpatializationLevel::NONE;
439 }
440
441 if (levelChanged) {
442 mPolicyCallback->onCheckSpatializer();
443 }
444 return Status::ok();
445}
446
Eric Laurent2be8b292021-08-23 09:44:33 -0700447Status Spatializer::setHeadSensor(int sensorHandle) {
448 ALOGV("%s sensorHandle %d", __func__, sensorHandle);
Eric Laurent780be4a2021-09-16 10:44:24 +0200449 if (!mSupportsHeadTracking) {
450 return binderStatusFromStatusT(INVALID_OPERATION);
451 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700452 std::lock_guard lock(mLock);
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700453 mHeadSensor = sensorHandle;
Eric Laurent2be8b292021-08-23 09:44:33 -0700454 if (mPoseController != nullptr) {
455 mPoseController->setHeadSensor(mHeadSensor);
456 }
457 return Status::ok();
458}
459
460Status Spatializer::setScreenSensor(int sensorHandle) {
461 ALOGV("%s sensorHandle %d", __func__, sensorHandle);
Eric Laurent780be4a2021-09-16 10:44:24 +0200462 if (!mSupportsHeadTracking) {
463 return binderStatusFromStatusT(INVALID_OPERATION);
464 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700465 std::lock_guard lock(mLock);
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700466 mScreenSensor = sensorHandle;
Eric Laurent2be8b292021-08-23 09:44:33 -0700467 if (mPoseController != nullptr) {
468 mPoseController->setScreenSensor(mScreenSensor);
469 }
470 return Status::ok();
471}
472
473Status Spatializer::setDisplayOrientation(float physicalToLogicalAngle) {
474 ALOGV("%s physicalToLogicalAngle %f", __func__, physicalToLogicalAngle);
Eric Laurent780be4a2021-09-16 10:44:24 +0200475 if (!mSupportsHeadTracking) {
476 return binderStatusFromStatusT(INVALID_OPERATION);
477 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700478 std::lock_guard lock(mLock);
479 mDisplayOrientation = physicalToLogicalAngle;
480 if (mPoseController != nullptr) {
481 mPoseController->setDisplayOrientation(mDisplayOrientation);
482 }
Eric Laurent16ddaf42021-09-17 15:00:35 +0200483 if (mEngine != nullptr) {
484 setEffectParameter_l(
485 SPATIALIZER_PARAM_DISPLAY_ORIENTATION, std::vector<float>{physicalToLogicalAngle});
486 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700487 return Status::ok();
488}
489
490Status Spatializer::setHingeAngle(float hingeAngle) {
491 std::lock_guard lock(mLock);
492 ALOGV("%s hingeAngle %f", __func__, hingeAngle);
493 if (mEngine != nullptr) {
494 setEffectParameter_l(SPATIALIZER_PARAM_HINGE_ANGLE, std::vector<float>{hingeAngle});
495 }
496 return Status::ok();
497}
498
499Status Spatializer::getSupportedModes(std::vector<SpatializationMode> *modes) {
500 ALOGV("%s", __func__);
501 if (modes == nullptr) {
502 return binderStatusFromStatusT(BAD_VALUE);
503 }
504 *modes = mSpatializationModes;
505 return Status::ok();
506}
507
Eric Laurent67816e32021-09-16 15:18:40 +0200508Status Spatializer::registerHeadTrackingCallback(
509 const sp<media::ISpatializerHeadTrackingCallback>& callback) {
510 ALOGV("%s callback %p", __func__, callback.get());
511 std::lock_guard lock(mLock);
512 if (!mSupportsHeadTracking) {
513 return binderStatusFromStatusT(INVALID_OPERATION);
514 }
515 mHeadTrackingCallback = callback;
516 return Status::ok();
517}
518
Eric Laurentc87402b2021-09-17 16:49:42 +0200519Status Spatializer::setParameter(int key, const std::vector<unsigned char>& value) {
520 ALOGV("%s key %d", __func__, key);
521 std::lock_guard lock(mLock);
522 status_t status = INVALID_OPERATION;
523 if (mEngine != nullptr) {
524 status = setEffectParameter_l(key, value);
525 }
526 return binderStatusFromStatusT(status);
527}
528
529Status Spatializer::getParameter(int key, std::vector<unsigned char> *value) {
Greg Kaiserf7249f82021-09-21 07:10:12 -0700530 ALOGV("%s key %d value size %d", __func__, key,
531 (value != nullptr ? (int)value->size() : -1));
Eric Laurentc87402b2021-09-17 16:49:42 +0200532 if (value == nullptr) {
George Burgess IV22386222021-09-22 12:09:31 -0700533 return binderStatusFromStatusT(BAD_VALUE);
Eric Laurentc87402b2021-09-17 16:49:42 +0200534 }
535 std::lock_guard lock(mLock);
536 status_t status = INVALID_OPERATION;
537 if (mEngine != nullptr) {
538 ALOGV("%s key %d mEngine %p", __func__, key, mEngine.get());
539 status = getEffectParameter_l(key, value);
540 }
541 return binderStatusFromStatusT(status);
542}
543
544Status Spatializer::getOutput(int *output) {
545 ALOGV("%s", __func__);
546 if (output == nullptr) {
547 binderStatusFromStatusT(BAD_VALUE);
548 }
549 std::lock_guard lock(mLock);
550 *output = VALUE_OR_RETURN_BINDER_STATUS(legacy2aidl_audio_io_handle_t_int32_t(mOutput));
551 ALOGV("%s got output %d", __func__, *output);
552 return Status::ok();
553}
554
Eric Laurent2be8b292021-08-23 09:44:33 -0700555// SpatializerPoseController::Listener
556void Spatializer::onHeadToStagePose(const Pose3f& headToStage) {
557 ALOGV("%s", __func__);
Eric Laurent780be4a2021-09-16 10:44:24 +0200558 LOG_ALWAYS_FATAL_IF(!mSupportsHeadTracking,
559 "onHeadToStagePose() called with no head tracking support!");
560
Eric Laurent2be8b292021-08-23 09:44:33 -0700561 auto vec = headToStage.toVector();
Eric Laurent8a4259f2021-09-14 16:04:00 +0200562 LOG_ALWAYS_FATAL_IF(vec.size() != sHeadPoseKeys.size(),
563 "%s invalid head to stage vector size %zu", __func__, vec.size());
564
565 sp<AMessage> msg =
566 new AMessage(EngineCallbackHandler::kWhatOnHeadToStagePose, mHandler);
567 for (size_t i = 0 ; i < sHeadPoseKeys.size(); i++) {
568 msg->setFloat(sHeadPoseKeys[i], vec[i]);
569 }
570 msg->post();
571}
572
573void Spatializer::onHeadToStagePoseMsg(const std::vector<float>& headToStage) {
574 ALOGV("%s", __func__);
Eric Laurent67816e32021-09-16 15:18:40 +0200575 sp<media::ISpatializerHeadTrackingCallback> callback;
Eric Laurent2be8b292021-08-23 09:44:33 -0700576 {
577 std::lock_guard lock(mLock);
Eric Laurent67816e32021-09-16 15:18:40 +0200578 callback = mHeadTrackingCallback;
Eric Laurent2be8b292021-08-23 09:44:33 -0700579 if (mEngine != nullptr) {
Eric Laurent8a4259f2021-09-14 16:04:00 +0200580 setEffectParameter_l(SPATIALIZER_PARAM_HEAD_TO_STAGE, headToStage);
Eric Laurent2be8b292021-08-23 09:44:33 -0700581 }
582 }
583
584 if (callback != nullptr) {
Eric Laurent8a4259f2021-09-14 16:04:00 +0200585 callback->onHeadToSoundStagePoseUpdated(headToStage);
Eric Laurent2be8b292021-08-23 09:44:33 -0700586 }
587}
588
589void Spatializer::onActualModeChange(HeadTrackingMode mode) {
Eric Laurent8a4259f2021-09-14 16:04:00 +0200590 ALOGV("%s(%d)", __func__, (int)mode);
591 sp<AMessage> msg =
592 new AMessage(EngineCallbackHandler::kWhatOnActualModeChange, mHandler);
593 msg->setInt32(EngineCallbackHandler::kModeKey, static_cast<int>(mode));
594 msg->post();
595}
596
597void Spatializer::onActualModeChangeMsg(HeadTrackingMode mode) {
598 ALOGV("%s(%d)", __func__, (int) mode);
Eric Laurent67816e32021-09-16 15:18:40 +0200599 sp<media::ISpatializerHeadTrackingCallback> callback;
Eric Laurent2be8b292021-08-23 09:44:33 -0700600 SpatializerHeadTrackingMode spatializerMode;
601 {
602 std::lock_guard lock(mLock);
603 if (!mSupportsHeadTracking) {
604 spatializerMode = SpatializerHeadTrackingMode::DISABLED;
605 } else {
606 switch (mode) {
607 case HeadTrackingMode::STATIC:
608 spatializerMode = SpatializerHeadTrackingMode::DISABLED;
609 break;
610 case HeadTrackingMode::WORLD_RELATIVE:
611 spatializerMode = SpatializerHeadTrackingMode::RELATIVE_WORLD;
612 break;
613 case HeadTrackingMode::SCREEN_RELATIVE:
614 spatializerMode = SpatializerHeadTrackingMode::RELATIVE_SCREEN;
615 break;
616 default:
617 LOG_ALWAYS_FATAL("Unknown mode: %d", mode);
618 }
619 }
620 mActualHeadTrackingMode = spatializerMode;
Eric Laurent67816e32021-09-16 15:18:40 +0200621 callback = mHeadTrackingCallback;
Eric Laurent2be8b292021-08-23 09:44:33 -0700622 }
623 if (callback != nullptr) {
624 callback->onHeadTrackingModeChanged(spatializerMode);
625 }
626}
627
Eric Laurent6d607012021-07-05 11:54:40 +0200628status_t Spatializer::attachOutput(audio_io_handle_t output) {
Eric Laurent2be8b292021-08-23 09:44:33 -0700629 std::shared_ptr<SpatializerPoseController> poseController;
630 {
631 std::lock_guard lock(mLock);
632 ALOGV("%s output %d mOutput %d", __func__, (int)output, (int)mOutput);
633 if (mOutput != AUDIO_IO_HANDLE_NONE) {
634 LOG_ALWAYS_FATAL_IF(mEngine == nullptr, "%s output set without FX engine", __func__);
635 // remove FX instance
636 mEngine->setEnabled(false);
637 mEngine.clear();
638 }
639 // create FX instance on output
640 AttributionSourceState attributionSource = AttributionSourceState();
641 mEngine = new AudioEffect(attributionSource);
642 mEngine->set(nullptr, &mEngineDescriptor.uuid, 0, Spatializer::engineCallback /* cbf */,
643 this /* user */, AUDIO_SESSION_OUTPUT_STAGE, output, {} /* device */,
644 false /* probe */, true /* notifyFramesProcessed */);
645 status_t status = mEngine->initCheck();
646 ALOGV("%s mEngine create status %d", __func__, (int)status);
647 if (status != NO_ERROR) {
648 return status;
649 }
650
651 setEffectParameter_l(SPATIALIZER_PARAM_LEVEL,
652 std::vector<SpatializationLevel>{mLevel});
653 setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_MODE,
654 std::vector<SpatializerHeadTrackingMode>{mActualHeadTrackingMode});
655
656 mEngine->setEnabled(true);
657 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 Laurent6d607012021-07-05 11:54:40 +0200671 }
Eric Laurent780be4a2021-09-16 10:44:24 +0200672 if (poseController != nullptr) {
673 poseController->waitUntilCalculated();
674 }
Eric Laurent6d607012021-07-05 11:54:40 +0200675 return NO_ERROR;
676}
677
678audio_io_handle_t Spatializer::detachOutput() {
Eric Laurent2be8b292021-08-23 09:44:33 -0700679 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200680 ALOGV("%s mOutput %d", __func__, (int)mOutput);
Eric Laurent2be8b292021-08-23 09:44:33 -0700681 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent6d607012021-07-05 11:54:40 +0200682 if (mOutput == AUDIO_IO_HANDLE_NONE) {
Eric Laurent2be8b292021-08-23 09:44:33 -0700683 return output;
Eric Laurent6d607012021-07-05 11:54:40 +0200684 }
685 // remove FX instance
686 mEngine->setEnabled(false);
687 mEngine.clear();
Eric Laurent2be8b292021-08-23 09:44:33 -0700688 output = mOutput;
Eric Laurent6d607012021-07-05 11:54:40 +0200689 mOutput = AUDIO_IO_HANDLE_NONE;
Eric Laurent2be8b292021-08-23 09:44:33 -0700690 mPoseController.reset();
Eric Laurent6d607012021-07-05 11:54:40 +0200691 return output;
692}
693
Eric Laurent2be8b292021-08-23 09:44:33 -0700694void Spatializer::calculateHeadPose() {
695 ALOGV("%s", __func__);
696 std::lock_guard lock(mLock);
697 if (mPoseController != nullptr) {
698 mPoseController->calculateAsync();
699 }
700}
Eric Laurent6d607012021-07-05 11:54:40 +0200701
Eric Laurent2be8b292021-08-23 09:44:33 -0700702void Spatializer::engineCallback(int32_t event, void *user, void *info) {
Eric Laurent6d607012021-07-05 11:54:40 +0200703 if (user == nullptr) {
704 return;
705 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700706 Spatializer* const me = reinterpret_cast<Spatializer *>(user);
Eric Laurent6d607012021-07-05 11:54:40 +0200707 switch (event) {
708 case AudioEffect::EVENT_FRAMES_PROCESSED: {
Eric Laurent2be8b292021-08-23 09:44:33 -0700709 int frames = info == nullptr ? 0 : *(int*)info;
Eric Laurent6d607012021-07-05 11:54:40 +0200710 ALOGD("%s frames processed %d for me %p", __func__, frames, me);
Eric Laurent8a4259f2021-09-14 16:04:00 +0200711 me->postFramesProcessedMsg(frames);
Eric Laurent2be8b292021-08-23 09:44:33 -0700712 } break;
Eric Laurent6d607012021-07-05 11:54:40 +0200713 default:
714 ALOGD("%s event %d", __func__, event);
715 break;
716 }
717}
718
Eric Laurent8a4259f2021-09-14 16:04:00 +0200719void Spatializer::postFramesProcessedMsg(int frames) {
720 sp<AMessage> msg =
721 new AMessage(EngineCallbackHandler::kWhatOnFramesProcessed, mHandler);
722 msg->setInt32(EngineCallbackHandler::kNumFramesKey, frames);
723 msg->post();
724}
725
Eric Laurent6d607012021-07-05 11:54:40 +0200726} // namespace android