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