Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 20 | #include <android-base/stringprintf.h> |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 21 | #include <android/media/BnEffect.h> |
| 22 | #include <android/media/BnSpatializer.h> |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 23 | #include <android/media/SpatializationLevel.h> |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 24 | #include <android/media/SpatializationMode.h> |
| 25 | #include <android/media/SpatializerHeadTrackingMode.h> |
Andy Hung | 5d8618d | 2022-11-17 17:21:45 -0800 | [diff] [blame] | 26 | #include <android/media/audio/common/AudioLatencyMode.h> |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 27 | #include <audio_utils/SimpleLog.h> |
| 28 | #include <math.h> |
| 29 | #include <media/AudioEffect.h> |
Shunkai Yao | e3e403d | 2023-04-18 23:14:25 +0000 | [diff] [blame^] | 30 | #include <media/audiohal/EffectsFactoryHalInterface.h> |
Andy Hung | 82a4eab | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 31 | #include <media/VectorRecorder.h> |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 32 | #include <media/audiohal/EffectHalInterface.h> |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 33 | #include <media/stagefright/foundation/ALooper.h> |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 34 | #include <system/audio_effects/effect_spatializer.h> |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 35 | #include <string> |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 36 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 37 | #include "SpatializerPoseController.h" |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | |
| 41 | |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | |
| 44 | /** |
| 45 | * A callback interface from the Spatializer object or its parent AudioPolicyService. |
| 46 | * This is implemented by the audio policy service hosting the Spatializer to perform |
| 47 | * actions needed when a state change inside the Spatializer requires some audio system |
| 48 | * changes that cannot be performed by the Spatializer. For instance opening or closing a |
| 49 | * spatializer output stream when the spatializer is enabled or disabled |
| 50 | */ |
| 51 | class SpatializerPolicyCallback { |
| 52 | public: |
| 53 | /** Called when a stage change occurs that requires the parent audio policy service to take |
| 54 | * some action. |
| 55 | */ |
| 56 | virtual void onCheckSpatializer() = 0; |
| 57 | |
| 58 | virtual ~SpatializerPolicyCallback() = default; |
| 59 | }; |
| 60 | /** |
| 61 | * The Spatializer class implements all functional controlling the multichannel spatializer |
| 62 | * with head tracking implementation in the native audio service: audio policy and audio flinger. |
| 63 | * It presents an AIDL interface available to the java audio service to discover the availability |
| 64 | * of the feature and options, control its state and register an active head tracking sensor. |
| 65 | * It maintains the current state of the platform spatializer and applies the stored parameters |
| 66 | * when the spatializer engine is created and enabled. |
| 67 | * Based on the requested spatializer level, it will request the creation of a specialized output |
| 68 | * mixer to the audio policy service which will in turn notify the Spatializer of the output |
| 69 | * stream on which a spatializer engine should be created, configured and enabled. |
| 70 | * The spatializer also hosts the head tracking management logic. This logic receives the |
| 71 | * desired head tracking mode and selected head tracking sensor, registers a sensor event listener |
| 72 | * and derives the compounded head pose information to the spatializer engine. |
| 73 | * |
| 74 | * Workflow: |
| 75 | * - Initialization: when the audio policy service starts, it checks if a spatializer effect |
| 76 | * engine exists and if the audio policy manager reports a dedicated spatializer output profile. |
| 77 | * If both conditions are met, a Spatializer object is created |
| 78 | * - Capabilities discovery: AudioService will call AudioSystem::canBeSpatialized() and if true, |
| 79 | * acquire an ISpatializer interface with AudioSystem::getSpatializer(). This interface |
| 80 | * will be used to query the implementation capabilities and configure the spatializer. |
| 81 | * - Enabling: when ISpatializer::setLevel() sets a level different from NONE the spatializer |
| 82 | * is considered enabled. The audio policy callback onCheckSpatializer() is called. This |
| 83 | * triggers a request to audio policy manager to open a spatialization output stream and a |
| 84 | * spatializer mixer is created in audio flinger. When an output is returned by audio policy |
| 85 | * manager, Spatializer::attachOutput() is called which creates and enables the spatializer |
| 86 | * stage engine on the specified output. |
| 87 | * - Disabling: when the spatialization level is set to NONE, the spatializer is considered |
| 88 | * disabled. The audio policy callback onCheckSpatializer() is called. This triggers a call |
| 89 | * to Spatializer::detachOutput() and the spatializer engine is released. Then a request is |
| 90 | * made to audio policy manager to release and close the spatializer output stream and the |
| 91 | * spatializer mixer thread is destroyed. |
| 92 | */ |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 93 | class Spatializer : public media::BnSpatializer, |
| 94 | public IBinder::DeathRecipient, |
Eric Laurent | b387fb4 | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 95 | private SpatializerPoseController::Listener, |
| 96 | public virtual AudioSystem::SupportedLatencyModesCallback { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 97 | public: |
Shunkai Yao | e3e403d | 2023-04-18 23:14:25 +0000 | [diff] [blame^] | 98 | static sp<Spatializer> create(SpatializerPolicyCallback* callback, |
| 99 | const sp<EffectsFactoryHalInterface>& effectsFactoryHal); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 100 | |
| 101 | ~Spatializer() override; |
| 102 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 103 | /** RefBase */ |
| 104 | void onFirstRef(); |
| 105 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 106 | /** ISpatializer, see ISpatializer.aidl */ |
| 107 | binder::Status release() override; |
| 108 | binder::Status getSupportedLevels(std::vector<media::SpatializationLevel>* levels) override; |
| 109 | binder::Status setLevel(media::SpatializationLevel level) override; |
| 110 | binder::Status getLevel(media::SpatializationLevel *level) override; |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 111 | binder::Status isHeadTrackingSupported(bool *supports); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 112 | binder::Status getSupportedHeadTrackingModes( |
Ytai Ben-Tsvi | a16a9df | 2021-08-05 08:57:06 -0700 | [diff] [blame] | 113 | std::vector<media::SpatializerHeadTrackingMode>* modes) override; |
| 114 | binder::Status setDesiredHeadTrackingMode( |
| 115 | media::SpatializerHeadTrackingMode mode) override; |
| 116 | binder::Status getActualHeadTrackingMode( |
| 117 | media::SpatializerHeadTrackingMode* mode) override; |
| 118 | binder::Status recenterHeadTracker() override; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 119 | binder::Status setGlobalTransform(const std::vector<float>& screenToStage) override; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 120 | binder::Status setHeadSensor(int sensorHandle) override; |
| 121 | binder::Status setScreenSensor(int sensorHandle) override; |
| 122 | binder::Status setDisplayOrientation(float physicalToLogicalAngle) override; |
| 123 | binder::Status setHingeAngle(float hingeAngle) override; |
Andy Hung | cd6c106 | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 124 | binder::Status setFoldState(bool folded) override; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 125 | binder::Status getSupportedModes(std::vector<media::SpatializationMode>* modes) override; |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 126 | binder::Status registerHeadTrackingCallback( |
| 127 | const sp<media::ISpatializerHeadTrackingCallback>& callback) override; |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 128 | binder::Status setParameter(int key, const std::vector<unsigned char>& value) override; |
| 129 | binder::Status getParameter(int key, std::vector<unsigned char> *value) override; |
| 130 | binder::Status getOutput(int *output); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 131 | |
| 132 | /** IBinder::DeathRecipient. Listen to the death of the INativeSpatializerCallback. */ |
| 133 | virtual void binderDied(const wp<IBinder>& who); |
| 134 | |
Eric Laurent | b387fb4 | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 135 | /** SupportedLatencyModesCallback */ |
| 136 | void onSupportedLatencyModesChanged( |
| 137 | audio_io_handle_t output, const std::vector<audio_latency_mode_t>& modes) override; |
| 138 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 139 | /** Registers a INativeSpatializerCallback when a client is attached to this Spatializer |
| 140 | * by audio policy service. |
| 141 | */ |
| 142 | status_t registerCallback(const sp<media::INativeSpatializerCallback>& callback); |
| 143 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 144 | status_t loadEngineConfiguration(sp<EffectHalInterface> effect); |
| 145 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 146 | /** Level getter for use by local classes. */ |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 147 | media::SpatializationLevel getLevel() const { std::lock_guard lock(mLock); return mLevel; } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 148 | |
| 149 | /** Called by audio policy service when the special output mixer dedicated to spatialization |
| 150 | * is opened and the spatializer engine must be created. |
| 151 | */ |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 152 | status_t attachOutput(audio_io_handle_t output, size_t numActiveTracks); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 153 | /** Called by audio policy service when the special output mixer dedicated to spatialization |
| 154 | * is closed and the spatializer engine must be release. |
| 155 | */ |
| 156 | audio_io_handle_t detachOutput(); |
| 157 | /** Returns the output stream the spatializer is attached to. */ |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 158 | audio_io_handle_t getOutput() const { std::lock_guard lock(mLock); return mOutput; } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 159 | |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 160 | void updateActiveTracks(size_t numActiveTracks); |
| 161 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 162 | /** Gets the channel mask, sampling rate and format set for the spatializer input. */ |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 163 | audio_config_base_t getAudioInConfig() const; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 164 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 165 | void calculateHeadPose(); |
| 166 | |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 167 | /** Convert fields in Spatializer and sub-modules to a string. Disable thread-safety-analysis |
| 168 | * here because we want to dump mutex guarded members even try_lock failed to provide as much |
| 169 | * information as possible for debugging purpose. */ |
| 170 | std::string toString(unsigned level) const NO_THREAD_SAFETY_ANALYSIS; |
| 171 | |
| 172 | static std::string toString(audio_latency_mode_t mode) { |
Andy Hung | 5d8618d | 2022-11-17 17:21:45 -0800 | [diff] [blame] | 173 | // We convert to the AIDL type to print (eventually the legacy type will be removed). |
| 174 | const auto result = legacy2aidl_audio_latency_mode_t_AudioLatencyMode(mode); |
| 175 | return result.has_value() ? |
| 176 | media::audio::common::toString(*result) : "unknown_latency_mode"; |
| 177 | } |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 178 | |
Andy Hung | 757bc81 | 2022-09-13 18:53:06 -0700 | [diff] [blame] | 179 | // If the Spatializer is not created, we send the status for metrics purposes. |
| 180 | // OK: Spatializer not expected to be created. |
| 181 | // NO_INIT: Spatializer creation failed. |
| 182 | static void sendEmptyCreateSpatializerMetricWithStatus(status_t status); |
| 183 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 184 | private: |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 185 | Spatializer(effect_descriptor_t engineDescriptor, |
| 186 | SpatializerPolicyCallback *callback); |
| 187 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 188 | static void engineCallback(int32_t event, void* user, void *info); |
| 189 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 190 | // From VirtualizerStageController::Listener |
| 191 | void onHeadToStagePose(const media::Pose3f& headToStage) override; |
| 192 | void onActualModeChange(media::HeadTrackingMode mode) override; |
| 193 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 194 | void onHeadToStagePoseMsg(const std::vector<float>& headToStage); |
| 195 | void onActualModeChangeMsg(media::HeadTrackingMode mode); |
Eric Laurent | df2ece4 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 196 | void onSupportedLatencyModesChangedMsg( |
| 197 | audio_io_handle_t output, std::vector<audio_latency_mode_t>&& modes); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 198 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 199 | static constexpr int kMaxEffectParamValues = 10; |
| 200 | /** |
| 201 | * Get a parameter from spatializer engine by calling the effect HAL command method directly. |
| 202 | * To be used when the engine instance mEngine is not yet created in the effect framework. |
| 203 | * When MULTI_VALUES is false, the expected reply is only one value of type T. |
| 204 | * When MULTI_VALUES is true, the expected reply is made of a number (of type T) indicating |
| 205 | * how many values are returned, followed by this number for values of type T. |
| 206 | */ |
| 207 | template<bool MULTI_VALUES, typename T> |
| 208 | status_t getHalParameter(sp<EffectHalInterface> effect, uint32_t type, |
| 209 | std::vector<T> *values) { |
| 210 | static_assert(sizeof(T) <= sizeof(uint32_t), "The size of T must less than 32 bits"); |
| 211 | |
| 212 | uint32_t cmd[sizeof(effect_param_t) / sizeof(uint32_t) + 1]; |
| 213 | uint32_t reply[sizeof(effect_param_t) / sizeof(uint32_t) + 2 + kMaxEffectParamValues]; |
| 214 | |
| 215 | effect_param_t *p = (effect_param_t *)cmd; |
| 216 | p->psize = sizeof(uint32_t); |
| 217 | if (MULTI_VALUES) { |
| 218 | p->vsize = (kMaxEffectParamValues + 1) * sizeof(T); |
| 219 | } else { |
| 220 | p->vsize = sizeof(T); |
| 221 | } |
| 222 | *(uint32_t *)p->data = type; |
| 223 | uint32_t replySize = sizeof(effect_param_t) + p->psize + p->vsize; |
| 224 | |
| 225 | status_t status = effect->command(EFFECT_CMD_GET_PARAM, |
| 226 | sizeof(effect_param_t) + sizeof(uint32_t), cmd, |
| 227 | &replySize, reply); |
| 228 | if (status != NO_ERROR) { |
| 229 | return status; |
| 230 | } |
| 231 | if (p->status != NO_ERROR) { |
| 232 | return p->status; |
| 233 | } |
| 234 | if (replySize < |
| 235 | sizeof(effect_param_t) + sizeof(uint32_t) + (MULTI_VALUES ? 2 : 1) * sizeof(T)) { |
| 236 | return BAD_VALUE; |
| 237 | } |
| 238 | |
| 239 | T *params = (T *)((uint8_t *)reply + sizeof(effect_param_t) + sizeof(uint32_t)); |
| 240 | int numParams = 1; |
| 241 | if (MULTI_VALUES) { |
| 242 | numParams = (int)*params++; |
| 243 | } |
| 244 | if (numParams > kMaxEffectParamValues) { |
| 245 | return BAD_VALUE; |
| 246 | } |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 247 | (*values).clear(); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 248 | std::copy(¶ms[0], ¶ms[numParams], back_inserter(*values)); |
| 249 | return NO_ERROR; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Set a parameter to spatializer engine by calling setParameter on mEngine AudioEffect object. |
| 254 | * It is possible to pass more than one value of type T according to the parameter type |
| 255 | * according to values vector size. |
| 256 | */ |
| 257 | template<typename T> |
| 258 | status_t setEffectParameter_l(uint32_t type, const 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 | memcpy((uint32_t *)p->data + 1, values.data(), sizeof(T) * values.size()); |
| 267 | |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 268 | status_t status = mEngine->setParameter(p); |
| 269 | if (status != NO_ERROR) { |
| 270 | return status; |
| 271 | } |
| 272 | if (p->status != NO_ERROR) { |
| 273 | return p->status; |
| 274 | } |
| 275 | return NO_ERROR; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Get a parameter from spatializer engine by calling getParameter on AudioEffect object. |
| 280 | * It is possible to read more than one value of type T according to the parameter type |
| 281 | * by specifying values vector size. |
| 282 | */ |
| 283 | template<typename T> |
| 284 | status_t getEffectParameter_l(uint32_t type, std::vector<T> *values) REQUIRES(mLock) { |
| 285 | static_assert(sizeof(T) <= sizeof(uint32_t), "The size of T must less than 32 bits"); |
| 286 | |
| 287 | uint32_t cmd[sizeof(effect_param_t) / sizeof(uint32_t) + 1 + values->size()]; |
| 288 | effect_param_t *p = (effect_param_t *)cmd; |
| 289 | p->psize = sizeof(uint32_t); |
| 290 | p->vsize = sizeof(T) * values->size(); |
| 291 | *(uint32_t *)p->data = type; |
| 292 | |
| 293 | status_t status = mEngine->getParameter(p); |
| 294 | |
| 295 | if (status != NO_ERROR) { |
| 296 | return status; |
| 297 | } |
| 298 | if (p->status != NO_ERROR) { |
| 299 | return p->status; |
| 300 | } |
| 301 | |
| 302 | int numValues = std::min(p->vsize / sizeof(T), values->size()); |
| 303 | (*values).clear(); |
| 304 | T *retValues = (T *)((uint8_t *)p->data + sizeof(uint32_t)); |
| 305 | std::copy(&retValues[0], &retValues[numValues], back_inserter(*values)); |
| 306 | |
| 307 | return NO_ERROR; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 310 | void postFramesProcessedMsg(int frames); |
| 311 | |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 312 | /** |
| 313 | * Checks if head and screen sensors must be actively monitored based on |
| 314 | * spatializer state and playback activity and configures the pose controller |
| 315 | * accordingly. |
| 316 | */ |
| 317 | void checkSensorsState_l() REQUIRES(mLock); |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 318 | |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 319 | /** |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 320 | * Checks if the head pose controller should be created or destroyed according |
| 321 | * to desired head tracking mode. |
| 322 | */ |
| 323 | void checkPoseController_l() REQUIRES(mLock); |
| 324 | |
| 325 | /** |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 326 | * Checks if the spatializer effect should be enabled based on |
| 327 | * playback activity and requested level. |
| 328 | */ |
| 329 | void checkEngineState_l() REQUIRES(mLock); |
| 330 | |
Eric Laurent | 3c48ad9 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 331 | /** |
| 332 | * Reset head tracking mode and recenter pose in engine: Called when the head tracking |
| 333 | * is disabled. |
| 334 | */ |
| 335 | void resetEngineHeadPose_l() REQUIRES(mLock); |
| 336 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 337 | /** Effect engine descriptor */ |
| 338 | const effect_descriptor_t mEngineDescriptor; |
| 339 | /** Callback interface to parent audio policy service */ |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 340 | SpatializerPolicyCallback* const mPolicyCallback; |
| 341 | |
| 342 | /** Currently there is only one version of the spatializer running */ |
Andy Hung | 757bc81 | 2022-09-13 18:53:06 -0700 | [diff] [blame] | 343 | static constexpr const char* kDefaultMetricsId = |
| 344 | AMEDIAMETRICS_KEY_PREFIX_AUDIO_SPATIALIZER "0"; |
| 345 | const std::string mMetricsId = kDefaultMetricsId; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 346 | |
| 347 | /** Mutex protecting internal state */ |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 348 | mutable std::mutex mLock; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 349 | |
| 350 | /** Client AudioEffect for the engine */ |
| 351 | sp<AudioEffect> mEngine GUARDED_BY(mLock); |
| 352 | /** Output stream the spatializer mixer thread is attached to */ |
| 353 | audio_io_handle_t mOutput GUARDED_BY(mLock) = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 354 | |
| 355 | /** Callback interface to the client (AudioService) controlling this`Spatializer */ |
| 356 | sp<media::INativeSpatializerCallback> mSpatializerCallback GUARDED_BY(mLock); |
| 357 | |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 358 | /** Callback interface for head tracking */ |
| 359 | sp<media::ISpatializerHeadTrackingCallback> mHeadTrackingCallback GUARDED_BY(mLock); |
| 360 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 361 | /** Requested spatialization level */ |
| 362 | media::SpatializationLevel mLevel GUARDED_BY(mLock) = media::SpatializationLevel::NONE; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 363 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 364 | /** Control logic for head-tracking, etc. */ |
| 365 | std::shared_ptr<SpatializerPoseController> mPoseController GUARDED_BY(mLock); |
| 366 | |
| 367 | /** Last requested head tracking mode */ |
| 368 | media::HeadTrackingMode mDesiredHeadTrackingMode GUARDED_BY(mLock) |
| 369 | = media::HeadTrackingMode::STATIC; |
| 370 | |
| 371 | /** Last-reported actual head-tracking mode. */ |
| 372 | media::SpatializerHeadTrackingMode mActualHeadTrackingMode GUARDED_BY(mLock) |
| 373 | = media::SpatializerHeadTrackingMode::DISABLED; |
| 374 | |
| 375 | /** Selected Head pose sensor */ |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 376 | int32_t mHeadSensor GUARDED_BY(mLock) = SpatializerPoseController::INVALID_SENSOR; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 377 | |
| 378 | /** Selected Screen pose sensor */ |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 379 | int32_t mScreenSensor GUARDED_BY(mLock) = SpatializerPoseController::INVALID_SENSOR; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 380 | |
| 381 | /** Last display orientation received */ |
Andy Hung | cd6c106 | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 382 | float mDisplayOrientation GUARDED_BY(mLock) = 0.f; // aligned to natural up orientation. |
| 383 | |
| 384 | /** Last folded state */ |
| 385 | bool mFoldedState GUARDED_BY(mLock) = false; // foldable: true means folded. |
| 386 | |
| 387 | /** Last hinge angle */ |
| 388 | float mHingeAngle GUARDED_BY(mLock) = 0.f; // foldable: 0.f is closed, M_PI flat open. |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 389 | |
| 390 | std::vector<media::SpatializationLevel> mLevels; |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 391 | std::vector<media::SpatializerHeadTrackingMode> mHeadTrackingModes; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 392 | std::vector<media::SpatializationMode> mSpatializationModes; |
| 393 | std::vector<audio_channel_mask_t> mChannelMasks; |
| 394 | bool mSupportsHeadTracking; |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 395 | |
| 396 | // Looper thread for mEngine callbacks |
| 397 | class EngineCallbackHandler; |
| 398 | |
| 399 | sp<ALooper> mLooper; |
| 400 | sp<EngineCallbackHandler> mHandler; |
| 401 | |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 402 | size_t mNumActiveTracks GUARDED_BY(mLock) = 0; |
Eric Laurent | b387fb4 | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 403 | std::vector<audio_latency_mode_t> mSupportedLatencyModes GUARDED_BY(mLock); |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 404 | |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 405 | static const std::vector<const char*> sHeadPoseKeys; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 406 | |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 407 | // Local log for command messages. |
| 408 | static constexpr int mMaxLocalLogLine = 10; |
| 409 | SimpleLog mLocalLog{mMaxLocalLogLine}; |
| 410 | |
| 411 | /** |
| 412 | * @brief Calculate and record sensor data. |
| 413 | * Dump to local log with max/average pose angle every mPoseRecordThreshold. |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 414 | */ |
Shunkai Yao | 7de4038 | 2022-08-25 00:44:04 +0000 | [diff] [blame] | 415 | // Record one log line per second (up to mMaxLocalLogLine) to capture most recent sensor data. |
Andy Hung | 82a4eab | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 416 | media::VectorRecorder mPoseRecorder GUARDED_BY(mLock) { |
Andy Hung | 9bcf425 | 2023-02-27 13:36:23 -0800 | [diff] [blame] | 417 | 6 /* vectorSize */, std::chrono::seconds(1), mMaxLocalLogLine, { 3 } /* delimiterIdx */}; |
Shunkai Yao | 7de4038 | 2022-08-25 00:44:04 +0000 | [diff] [blame] | 418 | // Record one log line per minute (up to mMaxLocalLogLine) to capture durable sensor data. |
Andy Hung | 82a4eab | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 419 | media::VectorRecorder mPoseDurableRecorder GUARDED_BY(mLock) { |
Andy Hung | 9bcf425 | 2023-02-27 13:36:23 -0800 | [diff] [blame] | 420 | 6 /* vectorSize */, std::chrono::minutes(1), mMaxLocalLogLine, { 3 } /* delimiterIdx */}; |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 421 | }; // Spatializer |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 422 | |
| 423 | }; // namespace android |
| 424 | |
| 425 | #endif // ANDROID_MEDIA_SPATIALIZER_H |