blob: 61daf011ee0e7db2bfd86a95a886979d0bdd44d2 [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
20#include <android/media/BnEffect.h>
21#include <android/media/BnSpatializer.h>
Eric Laurent6d607012021-07-05 11:54:40 +020022#include <android/media/SpatializationLevel.h>
Eric Laurent2be8b292021-08-23 09:44:33 -070023#include <android/media/SpatializationMode.h>
24#include <android/media/SpatializerHeadTrackingMode.h>
25#include <android/sensor.h>
26#include <media/audiohal/EffectHalInterface.h>
Eric Laurent8a4259f2021-09-14 16:04:00 +020027#include <media/stagefright/foundation/ALooper.h>
Eric Laurent6d607012021-07-05 11:54:40 +020028#include <media/AudioEffect.h>
Eric Laurent1c5e2e32021-08-18 18:50:28 +020029#include <system/audio_effects/effect_spatializer.h>
Eric Laurent6d607012021-07-05 11:54:40 +020030
Eric Laurent2be8b292021-08-23 09:44:33 -070031#include "SpatializerPoseController.h"
Eric Laurent6d607012021-07-05 11:54:40 +020032
33namespace android {
34
35
36// ----------------------------------------------------------------------------
37
38/**
39 * A callback interface from the Spatializer object or its parent AudioPolicyService.
40 * This is implemented by the audio policy service hosting the Spatializer to perform
41 * actions needed when a state change inside the Spatializer requires some audio system
42 * changes that cannot be performed by the Spatializer. For instance opening or closing a
43 * spatializer output stream when the spatializer is enabled or disabled
44 */
45class SpatializerPolicyCallback {
46public:
47 /** Called when a stage change occurs that requires the parent audio policy service to take
48 * some action.
49 */
50 virtual void onCheckSpatializer() = 0;
51
52 virtual ~SpatializerPolicyCallback() = default;
53};
54/**
55 * The Spatializer class implements all functional controlling the multichannel spatializer
56 * with head tracking implementation in the native audio service: audio policy and audio flinger.
57 * It presents an AIDL interface available to the java audio service to discover the availability
58 * of the feature and options, control its state and register an active head tracking sensor.
59 * It maintains the current state of the platform spatializer and applies the stored parameters
60 * when the spatializer engine is created and enabled.
61 * Based on the requested spatializer level, it will request the creation of a specialized output
62 * mixer to the audio policy service which will in turn notify the Spatializer of the output
63 * stream on which a spatializer engine should be created, configured and enabled.
64 * The spatializer also hosts the head tracking management logic. This logic receives the
65 * desired head tracking mode and selected head tracking sensor, registers a sensor event listener
66 * and derives the compounded head pose information to the spatializer engine.
67 *
68 * Workflow:
69 * - Initialization: when the audio policy service starts, it checks if a spatializer effect
70 * engine exists and if the audio policy manager reports a dedicated spatializer output profile.
71 * If both conditions are met, a Spatializer object is created
72 * - Capabilities discovery: AudioService will call AudioSystem::canBeSpatialized() and if true,
73 * acquire an ISpatializer interface with AudioSystem::getSpatializer(). This interface
74 * will be used to query the implementation capabilities and configure the spatializer.
75 * - Enabling: when ISpatializer::setLevel() sets a level different from NONE the spatializer
76 * is considered enabled. The audio policy callback onCheckSpatializer() is called. This
77 * triggers a request to audio policy manager to open a spatialization output stream and a
78 * spatializer mixer is created in audio flinger. When an output is returned by audio policy
79 * manager, Spatializer::attachOutput() is called which creates and enables the spatializer
80 * stage engine on the specified output.
81 * - Disabling: when the spatialization level is set to NONE, the spatializer is considered
82 * disabled. The audio policy callback onCheckSpatializer() is called. This triggers a call
83 * to Spatializer::detachOutput() and the spatializer engine is released. Then a request is
84 * made to audio policy manager to release and close the spatializer output stream and the
85 * spatializer mixer thread is destroyed.
86 */
Eric Laurent2be8b292021-08-23 09:44:33 -070087class Spatializer : public media::BnSpatializer,
88 public IBinder::DeathRecipient,
89 private SpatializerPoseController::Listener {
90 public:
Eric Laurent6d607012021-07-05 11:54:40 +020091 static sp<Spatializer> create(SpatializerPolicyCallback *callback);
92
93 ~Spatializer() override;
94
Eric Laurent8a4259f2021-09-14 16:04:00 +020095 /** RefBase */
96 void onFirstRef();
97
Eric Laurent6d607012021-07-05 11:54:40 +020098 /** ISpatializer, see ISpatializer.aidl */
99 binder::Status release() override;
100 binder::Status getSupportedLevels(std::vector<media::SpatializationLevel>* levels) override;
101 binder::Status setLevel(media::SpatializationLevel level) override;
102 binder::Status getLevel(media::SpatializationLevel *level) override;
103 binder::Status getSupportedHeadTrackingModes(
Ytai Ben-Tsvia16a9df2021-08-05 08:57:06 -0700104 std::vector<media::SpatializerHeadTrackingMode>* modes) override;
105 binder::Status setDesiredHeadTrackingMode(
106 media::SpatializerHeadTrackingMode mode) override;
107 binder::Status getActualHeadTrackingMode(
108 media::SpatializerHeadTrackingMode* mode) override;
109 binder::Status recenterHeadTracker() override;
Eric Laurent6d607012021-07-05 11:54:40 +0200110 binder::Status setGlobalTransform(const std::vector<float>& screenToStage) override;
Eric Laurent2be8b292021-08-23 09:44:33 -0700111 binder::Status setHeadSensor(int sensorHandle) override;
112 binder::Status setScreenSensor(int sensorHandle) override;
113 binder::Status setDisplayOrientation(float physicalToLogicalAngle) override;
114 binder::Status setHingeAngle(float hingeAngle) override;
115 binder::Status getSupportedModes(std::vector<media::SpatializationMode>* modes) override;
Eric Laurent67816e32021-09-16 15:18:40 +0200116 binder::Status registerHeadTrackingCallback(
117 const sp<media::ISpatializerHeadTrackingCallback>& callback) override;
Eric Laurent6d607012021-07-05 11:54:40 +0200118
119 /** IBinder::DeathRecipient. Listen to the death of the INativeSpatializerCallback. */
120 virtual void binderDied(const wp<IBinder>& who);
121
122 /** Registers a INativeSpatializerCallback when a client is attached to this Spatializer
123 * by audio policy service.
124 */
125 status_t registerCallback(const sp<media::INativeSpatializerCallback>& callback);
126
Eric Laurent2be8b292021-08-23 09:44:33 -0700127 status_t loadEngineConfiguration(sp<EffectHalInterface> effect);
128
Eric Laurent6d607012021-07-05 11:54:40 +0200129 /** Level getter for use by local classes. */
Eric Laurent2be8b292021-08-23 09:44:33 -0700130 media::SpatializationLevel getLevel() const { std::lock_guard lock(mLock); return mLevel; }
Eric Laurent6d607012021-07-05 11:54:40 +0200131
132 /** Called by audio policy service when the special output mixer dedicated to spatialization
133 * is opened and the spatializer engine must be created.
134 */
135 status_t attachOutput(audio_io_handle_t output);
136 /** Called by audio policy service when the special output mixer dedicated to spatialization
137 * is closed and the spatializer engine must be release.
138 */
139 audio_io_handle_t detachOutput();
140 /** Returns the output stream the spatializer is attached to. */
Eric Laurent2be8b292021-08-23 09:44:33 -0700141 audio_io_handle_t getOutput() const { std::lock_guard lock(mLock); return mOutput; }
Eric Laurent6d607012021-07-05 11:54:40 +0200142
143 /** Gets the channel mask, sampling rate and format set for the spatializer input. */
Eric Laurent2be8b292021-08-23 09:44:33 -0700144 audio_config_base_t getAudioInConfig() const;
Eric Laurent6d607012021-07-05 11:54:40 +0200145
Eric Laurent8a4259f2021-09-14 16:04:00 +0200146 void calculateHeadPose();
147
Eric Laurent6d607012021-07-05 11:54:40 +0200148private:
Eric Laurent6d607012021-07-05 11:54:40 +0200149 Spatializer(effect_descriptor_t engineDescriptor,
150 SpatializerPolicyCallback *callback);
151
Eric Laurent6d607012021-07-05 11:54:40 +0200152 static void engineCallback(int32_t event, void* user, void *info);
153
Eric Laurent2be8b292021-08-23 09:44:33 -0700154 // From VirtualizerStageController::Listener
155 void onHeadToStagePose(const media::Pose3f& headToStage) override;
156 void onActualModeChange(media::HeadTrackingMode mode) override;
157
Eric Laurent8a4259f2021-09-14 16:04:00 +0200158 void onHeadToStagePoseMsg(const std::vector<float>& headToStage);
159 void onActualModeChangeMsg(media::HeadTrackingMode mode);
160
Eric Laurent2be8b292021-08-23 09:44:33 -0700161
162 static ConversionResult<ASensorRef> getSensorFromHandle(int handle);
163
164 static constexpr int kMaxEffectParamValues = 10;
165 /**
166 * Get a parameter from spatializer engine by calling the effect HAL command method directly.
167 * To be used when the engine instance mEngine is not yet created in the effect framework.
168 * When MULTI_VALUES is false, the expected reply is only one value of type T.
169 * When MULTI_VALUES is true, the expected reply is made of a number (of type T) indicating
170 * how many values are returned, followed by this number for values of type T.
171 */
172 template<bool MULTI_VALUES, typename T>
173 status_t getHalParameter(sp<EffectHalInterface> effect, uint32_t type,
174 std::vector<T> *values) {
175 static_assert(sizeof(T) <= sizeof(uint32_t), "The size of T must less than 32 bits");
176
177 uint32_t cmd[sizeof(effect_param_t) / sizeof(uint32_t) + 1];
178 uint32_t reply[sizeof(effect_param_t) / sizeof(uint32_t) + 2 + kMaxEffectParamValues];
179
180 effect_param_t *p = (effect_param_t *)cmd;
181 p->psize = sizeof(uint32_t);
182 if (MULTI_VALUES) {
183 p->vsize = (kMaxEffectParamValues + 1) * sizeof(T);
184 } else {
185 p->vsize = sizeof(T);
186 }
187 *(uint32_t *)p->data = type;
188 uint32_t replySize = sizeof(effect_param_t) + p->psize + p->vsize;
189
190 status_t status = effect->command(EFFECT_CMD_GET_PARAM,
191 sizeof(effect_param_t) + sizeof(uint32_t), cmd,
192 &replySize, reply);
193 if (status != NO_ERROR) {
194 return status;
195 }
196 if (p->status != NO_ERROR) {
197 return p->status;
198 }
199 if (replySize <
200 sizeof(effect_param_t) + sizeof(uint32_t) + (MULTI_VALUES ? 2 : 1) * sizeof(T)) {
201 return BAD_VALUE;
202 }
203
204 T *params = (T *)((uint8_t *)reply + sizeof(effect_param_t) + sizeof(uint32_t));
205 int numParams = 1;
206 if (MULTI_VALUES) {
207 numParams = (int)*params++;
208 }
209 if (numParams > kMaxEffectParamValues) {
210 return BAD_VALUE;
211 }
212 std::copy(&params[0], &params[numParams], back_inserter(*values));
213 return NO_ERROR;
214 }
215
216 /**
217 * Set a parameter to spatializer engine by calling setParameter on mEngine AudioEffect object.
218 * It is possible to pass more than one value of type T according to the parameter type
219 * according to values vector size.
220 */
221 template<typename T>
222 status_t setEffectParameter_l(uint32_t type, const std::vector<T>& values) REQUIRES(mLock) {
223 static_assert(sizeof(T) <= sizeof(uint32_t), "The size of T must less than 32 bits");
224
225 uint32_t cmd[sizeof(effect_param_t) / sizeof(uint32_t) + 1 + values.size()];
226 effect_param_t *p = (effect_param_t *)cmd;
227 p->psize = sizeof(uint32_t);
228 p->vsize = sizeof(T) * values.size();
229 *(uint32_t *)p->data = type;
230 memcpy((uint32_t *)p->data + 1, values.data(), sizeof(T) * values.size());
231
232 return mEngine->setParameter(p);
233 }
234
Eric Laurent8a4259f2021-09-14 16:04:00 +0200235 void postFramesProcessedMsg(int frames);
236
Eric Laurent6d607012021-07-05 11:54:40 +0200237 /** Effect engine descriptor */
238 const effect_descriptor_t mEngineDescriptor;
239 /** Callback interface to parent audio policy service */
240 SpatializerPolicyCallback* mPolicyCallback;
241
242 /** Mutex protecting internal state */
Eric Laurent2be8b292021-08-23 09:44:33 -0700243 mutable std::mutex mLock;
Eric Laurent6d607012021-07-05 11:54:40 +0200244
245 /** Client AudioEffect for the engine */
246 sp<AudioEffect> mEngine GUARDED_BY(mLock);
247 /** Output stream the spatializer mixer thread is attached to */
248 audio_io_handle_t mOutput GUARDED_BY(mLock) = AUDIO_IO_HANDLE_NONE;
Eric Laurent6d607012021-07-05 11:54:40 +0200249
250 /** Callback interface to the client (AudioService) controlling this`Spatializer */
251 sp<media::INativeSpatializerCallback> mSpatializerCallback GUARDED_BY(mLock);
252
Eric Laurent67816e32021-09-16 15:18:40 +0200253 /** Callback interface for head tracking */
254 sp<media::ISpatializerHeadTrackingCallback> mHeadTrackingCallback GUARDED_BY(mLock);
255
Eric Laurent6d607012021-07-05 11:54:40 +0200256 /** Requested spatialization level */
257 media::SpatializationLevel mLevel GUARDED_BY(mLock) = media::SpatializationLevel::NONE;
Eric Laurent6d607012021-07-05 11:54:40 +0200258
Eric Laurent2be8b292021-08-23 09:44:33 -0700259 /** Control logic for head-tracking, etc. */
260 std::shared_ptr<SpatializerPoseController> mPoseController GUARDED_BY(mLock);
261
262 /** Last requested head tracking mode */
263 media::HeadTrackingMode mDesiredHeadTrackingMode GUARDED_BY(mLock)
264 = media::HeadTrackingMode::STATIC;
265
266 /** Last-reported actual head-tracking mode. */
267 media::SpatializerHeadTrackingMode mActualHeadTrackingMode GUARDED_BY(mLock)
268 = media::SpatializerHeadTrackingMode::DISABLED;
269
270 /** Selected Head pose sensor */
271 ASensorRef mHeadSensor GUARDED_BY(mLock) = nullptr;
272
273 /** Selected Screen pose sensor */
274 ASensorRef mScreenSensor GUARDED_BY(mLock) = nullptr;
275
276 /** Last display orientation received */
277 static constexpr float kDisplayOrientationInvalid = 1000;
278 float mDisplayOrientation GUARDED_BY(mLock) = kDisplayOrientationInvalid;
279
280 std::vector<media::SpatializationLevel> mLevels;
281 std::vector<media::SpatializationMode> mSpatializationModes;
282 std::vector<audio_channel_mask_t> mChannelMasks;
283 bool mSupportsHeadTracking;
Eric Laurent8a4259f2021-09-14 16:04:00 +0200284
285 // Looper thread for mEngine callbacks
286 class EngineCallbackHandler;
287
288 sp<ALooper> mLooper;
289 sp<EngineCallbackHandler> mHandler;
290
291 static const std::vector<const char *> sHeadPoseKeys;
Eric Laurent6d607012021-07-05 11:54:40 +0200292};
293
294
295}; // namespace android
296
297#endif // ANDROID_MEDIA_SPATIALIZER_H