blob: 70939570e320be108182e6d6974f6aa1a81526cd [file] [log] [blame]
Eric Laurent6d607012021-07-05 11:54:40 +02001/*
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 Yao5a251df2022-07-22 18:42:27 +000020#include <android-base/stringprintf.h>
Eric Laurent6d607012021-07-05 11:54:40 +020021#include <android/media/BnEffect.h>
22#include <android/media/BnSpatializer.h>
Eric Laurent6d607012021-07-05 11:54:40 +020023#include <android/media/SpatializationLevel.h>
Eric Laurent2be8b292021-08-23 09:44:33 -070024#include <android/media/SpatializationMode.h>
25#include <android/media/SpatializerHeadTrackingMode.h>
Shunkai Yao5a251df2022-07-22 18:42:27 +000026#include <audio_utils/SimpleLog.h>
27#include <math.h>
28#include <media/AudioEffect.h>
Eric Laurent2be8b292021-08-23 09:44:33 -070029#include <media/audiohal/EffectHalInterface.h>
Eric Laurent8a4259f2021-09-14 16:04:00 +020030#include <media/stagefright/foundation/ALooper.h>
Eric Laurent1c5e2e32021-08-18 18:50:28 +020031#include <system/audio_effects/effect_spatializer.h>
Shunkai Yao5a251df2022-07-22 18:42:27 +000032#include <string>
Eric Laurent6d607012021-07-05 11:54:40 +020033
Eric Laurent2be8b292021-08-23 09:44:33 -070034#include "SpatializerPoseController.h"
Eric Laurent6d607012021-07-05 11:54:40 +020035
36namespace 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 */
48class SpatializerPolicyCallback {
49public:
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 Laurent2be8b292021-08-23 09:44:33 -070090class Spatializer : public media::BnSpatializer,
91 public IBinder::DeathRecipient,
92 private SpatializerPoseController::Listener {
93 public:
Eric Laurent6d607012021-07-05 11:54:40 +020094 static sp<Spatializer> create(SpatializerPolicyCallback *callback);
95
96 ~Spatializer() override;
97
Eric Laurent8a4259f2021-09-14 16:04:00 +020098 /** RefBase */
99 void onFirstRef();
100
Eric Laurent6d607012021-07-05 11:54:40 +0200101 /** ISpatializer, see ISpatializer.aidl */
102 binder::Status release() override;
103 binder::Status getSupportedLevels(std::vector<media::SpatializationLevel>* levels) override;
104 binder::Status setLevel(media::SpatializationLevel level) override;
105 binder::Status getLevel(media::SpatializationLevel *level) override;
Eric Laurentc87402b2021-09-17 16:49:42 +0200106 binder::Status isHeadTrackingSupported(bool *supports);
Eric Laurent6d607012021-07-05 11:54:40 +0200107 binder::Status getSupportedHeadTrackingModes(
Ytai Ben-Tsvia16a9df2021-08-05 08:57:06 -0700108 std::vector<media::SpatializerHeadTrackingMode>* modes) override;
109 binder::Status setDesiredHeadTrackingMode(
110 media::SpatializerHeadTrackingMode mode) override;
111 binder::Status getActualHeadTrackingMode(
112 media::SpatializerHeadTrackingMode* mode) override;
113 binder::Status recenterHeadTracker() override;
Eric Laurent6d607012021-07-05 11:54:40 +0200114 binder::Status setGlobalTransform(const std::vector<float>& screenToStage) override;
Eric Laurent2be8b292021-08-23 09:44:33 -0700115 binder::Status setHeadSensor(int sensorHandle) override;
116 binder::Status setScreenSensor(int sensorHandle) override;
117 binder::Status setDisplayOrientation(float physicalToLogicalAngle) override;
118 binder::Status setHingeAngle(float hingeAngle) override;
119 binder::Status getSupportedModes(std::vector<media::SpatializationMode>* modes) override;
Eric Laurent67816e32021-09-16 15:18:40 +0200120 binder::Status registerHeadTrackingCallback(
121 const sp<media::ISpatializerHeadTrackingCallback>& callback) override;
Eric Laurentc87402b2021-09-17 16:49:42 +0200122 binder::Status setParameter(int key, const std::vector<unsigned char>& value) override;
123 binder::Status getParameter(int key, std::vector<unsigned char> *value) override;
124 binder::Status getOutput(int *output);
Eric Laurent6d607012021-07-05 11:54:40 +0200125
126 /** IBinder::DeathRecipient. Listen to the death of the INativeSpatializerCallback. */
127 virtual void binderDied(const wp<IBinder>& who);
128
129 /** Registers a INativeSpatializerCallback when a client is attached to this Spatializer
130 * by audio policy service.
131 */
132 status_t registerCallback(const sp<media::INativeSpatializerCallback>& callback);
133
Eric Laurent2be8b292021-08-23 09:44:33 -0700134 status_t loadEngineConfiguration(sp<EffectHalInterface> effect);
135
Eric Laurent6d607012021-07-05 11:54:40 +0200136 /** Level getter for use by local classes. */
Eric Laurent2be8b292021-08-23 09:44:33 -0700137 media::SpatializationLevel getLevel() const { std::lock_guard lock(mLock); return mLevel; }
Eric Laurent6d607012021-07-05 11:54:40 +0200138
139 /** Called by audio policy service when the special output mixer dedicated to spatialization
140 * is opened and the spatializer engine must be created.
141 */
Eric Laurent15903592022-02-24 20:44:36 +0100142 status_t attachOutput(audio_io_handle_t output, size_t numActiveTracks);
Eric Laurent6d607012021-07-05 11:54:40 +0200143 /** Called by audio policy service when the special output mixer dedicated to spatialization
144 * is closed and the spatializer engine must be release.
145 */
146 audio_io_handle_t detachOutput();
147 /** Returns the output stream the spatializer is attached to. */
Eric Laurent2be8b292021-08-23 09:44:33 -0700148 audio_io_handle_t getOutput() const { std::lock_guard lock(mLock); return mOutput; }
Eric Laurent6d607012021-07-05 11:54:40 +0200149
Eric Laurent15903592022-02-24 20:44:36 +0100150 void updateActiveTracks(size_t numActiveTracks);
151
Eric Laurent6d607012021-07-05 11:54:40 +0200152 /** Gets the channel mask, sampling rate and format set for the spatializer input. */
Eric Laurent2be8b292021-08-23 09:44:33 -0700153 audio_config_base_t getAudioInConfig() const;
Eric Laurent6d607012021-07-05 11:54:40 +0200154
Eric Laurent8a4259f2021-09-14 16:04:00 +0200155 void calculateHeadPose();
156
Shunkai Yao5a251df2022-07-22 18:42:27 +0000157 /** Convert fields in Spatializer and sub-modules to a string. Disable thread-safety-analysis
158 * here because we want to dump mutex guarded members even try_lock failed to provide as much
159 * information as possible for debugging purpose. */
160 std::string toString(unsigned level) const NO_THREAD_SAFETY_ANALYSIS;
161
162 static std::string toString(audio_latency_mode_t mode) {
163 switch (mode) {
164 case AUDIO_LATENCY_MODE_FREE:
165 return "LATENCY_MODE_FREE";
166 case AUDIO_LATENCY_MODE_LOW:
167 return "LATENCY_MODE_LOW";
168 }
169 return "EnumNotImplemented";
170 };
171
172 /**
173 * Format head to stage vector to a string, [0.00, 0.00, 0.00, -1.29, -0.50, 15.27].
174 */
175 template <typename T>
176 static std::string toString(const std::vector<T>& vec, bool radianToDegree = false) {
177 if (vec.size() == 0) {
178 return "[]";
179 }
180
181 std::string ss = "[";
182 for (const auto& f : vec) {
183 if (radianToDegree) {
184 base::StringAppendF(&ss, "%0.2f, ",
185 HeadToStagePoseRecorder::getDegreeWithRadian(f));
186 } else {
187 base::StringAppendF(&ss, "%f, ", f);
188 }
189 }
190 ss.replace(ss.end() - 2, ss.end(), "]");
191 return ss;
192 };
193
Eric Laurent6d607012021-07-05 11:54:40 +0200194private:
Eric Laurent6d607012021-07-05 11:54:40 +0200195 Spatializer(effect_descriptor_t engineDescriptor,
196 SpatializerPolicyCallback *callback);
197
Eric Laurent6d607012021-07-05 11:54:40 +0200198 static void engineCallback(int32_t event, void* user, void *info);
199
Eric Laurent2be8b292021-08-23 09:44:33 -0700200 // From VirtualizerStageController::Listener
201 void onHeadToStagePose(const media::Pose3f& headToStage) override;
202 void onActualModeChange(media::HeadTrackingMode mode) override;
203
Eric Laurent8a4259f2021-09-14 16:04:00 +0200204 void onHeadToStagePoseMsg(const std::vector<float>& headToStage);
205 void onActualModeChangeMsg(media::HeadTrackingMode mode);
206
Eric Laurent2be8b292021-08-23 09:44:33 -0700207 static constexpr int kMaxEffectParamValues = 10;
208 /**
209 * Get a parameter from spatializer engine by calling the effect HAL command method directly.
210 * To be used when the engine instance mEngine is not yet created in the effect framework.
211 * When MULTI_VALUES is false, the expected reply is only one value of type T.
212 * When MULTI_VALUES is true, the expected reply is made of a number (of type T) indicating
213 * how many values are returned, followed by this number for values of type T.
214 */
215 template<bool MULTI_VALUES, typename T>
216 status_t getHalParameter(sp<EffectHalInterface> effect, uint32_t type,
217 std::vector<T> *values) {
218 static_assert(sizeof(T) <= sizeof(uint32_t), "The size of T must less than 32 bits");
219
220 uint32_t cmd[sizeof(effect_param_t) / sizeof(uint32_t) + 1];
221 uint32_t reply[sizeof(effect_param_t) / sizeof(uint32_t) + 2 + kMaxEffectParamValues];
222
223 effect_param_t *p = (effect_param_t *)cmd;
224 p->psize = sizeof(uint32_t);
225 if (MULTI_VALUES) {
226 p->vsize = (kMaxEffectParamValues + 1) * sizeof(T);
227 } else {
228 p->vsize = sizeof(T);
229 }
230 *(uint32_t *)p->data = type;
231 uint32_t replySize = sizeof(effect_param_t) + p->psize + p->vsize;
232
233 status_t status = effect->command(EFFECT_CMD_GET_PARAM,
234 sizeof(effect_param_t) + sizeof(uint32_t), cmd,
235 &replySize, reply);
236 if (status != NO_ERROR) {
237 return status;
238 }
239 if (p->status != NO_ERROR) {
240 return p->status;
241 }
242 if (replySize <
243 sizeof(effect_param_t) + sizeof(uint32_t) + (MULTI_VALUES ? 2 : 1) * sizeof(T)) {
244 return BAD_VALUE;
245 }
246
247 T *params = (T *)((uint8_t *)reply + sizeof(effect_param_t) + sizeof(uint32_t));
248 int numParams = 1;
249 if (MULTI_VALUES) {
250 numParams = (int)*params++;
251 }
252 if (numParams > kMaxEffectParamValues) {
253 return BAD_VALUE;
254 }
Eric Laurentc87402b2021-09-17 16:49:42 +0200255 (*values).clear();
Eric Laurent2be8b292021-08-23 09:44:33 -0700256 std::copy(&params[0], &params[numParams], back_inserter(*values));
257 return NO_ERROR;
258 }
259
260 /**
261 * Set a parameter to spatializer engine by calling setParameter on mEngine AudioEffect object.
262 * It is possible to pass more than one value of type T according to the parameter type
263 * according to values vector size.
264 */
265 template<typename T>
266 status_t setEffectParameter_l(uint32_t type, const std::vector<T>& values) REQUIRES(mLock) {
267 static_assert(sizeof(T) <= sizeof(uint32_t), "The size of T must less than 32 bits");
268
269 uint32_t cmd[sizeof(effect_param_t) / sizeof(uint32_t) + 1 + values.size()];
270 effect_param_t *p = (effect_param_t *)cmd;
271 p->psize = sizeof(uint32_t);
272 p->vsize = sizeof(T) * values.size();
273 *(uint32_t *)p->data = type;
274 memcpy((uint32_t *)p->data + 1, values.data(), sizeof(T) * values.size());
275
Eric Laurentc87402b2021-09-17 16:49:42 +0200276 status_t status = mEngine->setParameter(p);
277 if (status != NO_ERROR) {
278 return status;
279 }
280 if (p->status != NO_ERROR) {
281 return p->status;
282 }
283 return NO_ERROR;
284 }
285
286 /**
287 * Get a parameter from spatializer engine by calling getParameter on AudioEffect object.
288 * It is possible to read more than one value of type T according to the parameter type
289 * by specifying values vector size.
290 */
291 template<typename T>
292 status_t getEffectParameter_l(uint32_t type, std::vector<T> *values) REQUIRES(mLock) {
293 static_assert(sizeof(T) <= sizeof(uint32_t), "The size of T must less than 32 bits");
294
295 uint32_t cmd[sizeof(effect_param_t) / sizeof(uint32_t) + 1 + values->size()];
296 effect_param_t *p = (effect_param_t *)cmd;
297 p->psize = sizeof(uint32_t);
298 p->vsize = sizeof(T) * values->size();
299 *(uint32_t *)p->data = type;
300
301 status_t status = mEngine->getParameter(p);
302
303 if (status != NO_ERROR) {
304 return status;
305 }
306 if (p->status != NO_ERROR) {
307 return p->status;
308 }
309
310 int numValues = std::min(p->vsize / sizeof(T), values->size());
311 (*values).clear();
312 T *retValues = (T *)((uint8_t *)p->data + sizeof(uint32_t));
313 std::copy(&retValues[0], &retValues[numValues], back_inserter(*values));
314
315 return NO_ERROR;
Eric Laurent2be8b292021-08-23 09:44:33 -0700316 }
317
Eric Laurent8a4259f2021-09-14 16:04:00 +0200318 void postFramesProcessedMsg(int frames);
319
Eric Laurent9249d342022-03-18 11:55:56 +0100320 /**
321 * Checks if head and screen sensors must be actively monitored based on
322 * spatializer state and playback activity and configures the pose controller
323 * accordingly.
324 */
325 void checkSensorsState_l() REQUIRES(mLock);
Eric Laurent15903592022-02-24 20:44:36 +0100326
Eric Laurent7ea0d1b2022-04-01 14:23:44 +0200327 /**
Eric Laurent11094172022-04-05 18:27:42 +0200328 * Checks if the head pose controller should be created or destroyed according
329 * to desired head tracking mode.
330 */
331 void checkPoseController_l() REQUIRES(mLock);
332
333 /**
Eric Laurent7ea0d1b2022-04-01 14:23:44 +0200334 * Checks if the spatializer effect should be enabled based on
335 * playback activity and requested level.
336 */
337 void checkEngineState_l() REQUIRES(mLock);
338
Eric Laurent6d607012021-07-05 11:54:40 +0200339 /** Effect engine descriptor */
340 const effect_descriptor_t mEngineDescriptor;
341 /** Callback interface to parent audio policy service */
Andy Hunga461a002022-05-17 10:36:02 -0700342 SpatializerPolicyCallback* const mPolicyCallback;
343
344 /** Currently there is only one version of the spatializer running */
345 const std::string mMetricsId = AMEDIAMETRICS_KEY_PREFIX_AUDIO_SPATIALIZER "0";
Eric Laurent6d607012021-07-05 11:54:40 +0200346
347 /** Mutex protecting internal state */
Eric Laurent2be8b292021-08-23 09:44:33 -0700348 mutable std::mutex mLock;
Eric Laurent6d607012021-07-05 11:54:40 +0200349
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 Laurent6d607012021-07-05 11:54:40 +0200354
355 /** Callback interface to the client (AudioService) controlling this`Spatializer */
356 sp<media::INativeSpatializerCallback> mSpatializerCallback GUARDED_BY(mLock);
357
Eric Laurent67816e32021-09-16 15:18:40 +0200358 /** Callback interface for head tracking */
359 sp<media::ISpatializerHeadTrackingCallback> mHeadTrackingCallback GUARDED_BY(mLock);
360
Eric Laurent6d607012021-07-05 11:54:40 +0200361 /** Requested spatialization level */
362 media::SpatializationLevel mLevel GUARDED_BY(mLock) = media::SpatializationLevel::NONE;
Eric Laurent6d607012021-07-05 11:54:40 +0200363
Eric Laurent2be8b292021-08-23 09:44:33 -0700364 /** 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-Tsvi9f12f172021-09-23 16:47:25 -0700376 int32_t mHeadSensor GUARDED_BY(mLock) = SpatializerPoseController::INVALID_SENSOR;
Eric Laurent2be8b292021-08-23 09:44:33 -0700377
378 /** Selected Screen pose sensor */
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700379 int32_t mScreenSensor GUARDED_BY(mLock) = SpatializerPoseController::INVALID_SENSOR;
Eric Laurent2be8b292021-08-23 09:44:33 -0700380
381 /** Last display orientation received */
382 static constexpr float kDisplayOrientationInvalid = 1000;
383 float mDisplayOrientation GUARDED_BY(mLock) = kDisplayOrientationInvalid;
384
385 std::vector<media::SpatializationLevel> mLevels;
Andy Hunga461a002022-05-17 10:36:02 -0700386 std::vector<media::SpatializerHeadTrackingMode> mHeadTrackingModes;
Eric Laurent2be8b292021-08-23 09:44:33 -0700387 std::vector<media::SpatializationMode> mSpatializationModes;
388 std::vector<audio_channel_mask_t> mChannelMasks;
389 bool mSupportsHeadTracking;
Eric Laurent8a4259f2021-09-14 16:04:00 +0200390
391 // Looper thread for mEngine callbacks
392 class EngineCallbackHandler;
393
394 sp<ALooper> mLooper;
395 sp<EngineCallbackHandler> mHandler;
396
Eric Laurent15903592022-02-24 20:44:36 +0100397 size_t mNumActiveTracks GUARDED_BY(mLock) = 0;
398
Shunkai Yao5a251df2022-07-22 18:42:27 +0000399 static const std::vector<const char*> sHeadPoseKeys;
Eric Laurent6d607012021-07-05 11:54:40 +0200400
Shunkai Yao5a251df2022-07-22 18:42:27 +0000401 // Local log for command messages.
402 static constexpr int mMaxLocalLogLine = 10;
403 SimpleLog mLocalLog{mMaxLocalLogLine};
404
405 /**
406 * @brief Calculate and record sensor data.
407 * Dump to local log with max/average pose angle every mPoseRecordThreshold.
408 * TODO: log azimuth/elevation angles obtained for debugging actual orientation.
409 */
410 class HeadToStagePoseRecorder {
411 public:
412 /** Convert recorded sensor data to string with level indentation */
413 std::string toString_l(unsigned level) const;
414 /**
415 * @brief Calculate sensor data, record into local log when it is time.
416 *
417 * @param headToStage The vector from Pose3f::toVector().
418 */
419 void record_l(const std::vector<float>& headToStage);
420
421 static constexpr float getDegreeWithRadian(const float radian) {
422 float radianToDegreeRatio = (180 / PI);
423 return (radian * radianToDegreeRatio);
424 }
425
426 private:
427 static constexpr float PI = M_PI;
428 /**
429 * Pose recorder time threshold to record sensor data in local log.
430 * Sensor data will be recorded at least every mPoseRecordThreshold.
431 */
432 // TODO: add another history log to record longer period of sensor data.
433 static constexpr std::chrono::duration<double> mPoseRecordThreshold =
434 std::chrono::seconds(1);
435 /**
436 * According to frameworks/av/media/libheadtracking/include/media/Pose.h
437 * "The vector will have exactly 6 elements, where the first three are a translation vector
438 * and the last three are a rotation vector."
439 */
440 static constexpr size_t mPoseVectorSize = 6;
441 /**
442 * Timestamp of last sensor data record in local log.
443 */
444 std::chrono::time_point<std::chrono::steady_clock> mFirstSampleTimestamp;
445 // Last pose recorded, vector obtained from Pose3f::toVector().
446 std::vector<float> mLastPoseRecorded{mPoseVectorSize};
447 /**
448 * Number of sensor samples received since last record, sample rate is ~100Hz which produce
449 * ~6k samples/minute.
450 */
451 uint32_t mNumOfSampleSinceLastRecord = 0;
452 /* The sum of pose angle represented by radian since last dump, div
453 * mNumOfSampleSinceLastRecord to get arithmetic mean. Largest possible value: 2PI * 100Hz *
454 * mPoseRecordThreshold.
455 */
456 std::vector<double> mPoseRadianSum{mPoseVectorSize};
457 std::vector<float> mMaxPoseAngle{mPoseVectorSize};
458 std::vector<float> mMinPoseAngle{mPoseVectorSize};
459 // Local log for history sensor data.
460 SimpleLog mPoseRecordLog{mMaxLocalLogLine};
461
462 bool shouldRecordLog() const {
463 return std::chrono::duration_cast<std::chrono::seconds>(
464 std::chrono::steady_clock::now() - mFirstSampleTimestamp) >=
465 mPoseRecordThreshold;
466 }
467
468 void resetRecord(const std::vector<float>& headToStage) {
469 mPoseRadianSum.assign(mPoseVectorSize, 0);
470 mMaxPoseAngle.assign(mPoseVectorSize, 0);
471 mMinPoseAngle.assign(mPoseVectorSize, 0);
472 mNumOfSampleSinceLastRecord = 0;
473 mLastPoseRecorded = headToStage;
474 }
475
476 // Add each sample to sum and only calculate when record.
477 void poseSumToAverage() {
478 if (mNumOfSampleSinceLastRecord == 0) return;
479 for (auto& p : mPoseRadianSum) {
480 const float reciprocal = 1.f / mNumOfSampleSinceLastRecord;
481 p *= reciprocal;
482 }
483 }
484 }; // HeadToStagePoseRecorder
485 HeadToStagePoseRecorder mPoseRecorder;
486}; // Spatializer
Eric Laurent6d607012021-07-05 11:54:40 +0200487
488}; // namespace android
489
490#endif // ANDROID_MEDIA_SPATIALIZER_H