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