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