Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | #pragma once |
| 18 | |
Mikhail Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 19 | #include <atomic> |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 20 | #include <memory> |
Mikhail Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 21 | #include <mutex> |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 22 | #include <string_view> |
| 23 | |
| 24 | #include <aidl/android/hardware/audio/core/BpStreamCommon.h> |
| 25 | #include <aidl/android/hardware/audio/core/BpStreamIn.h> |
| 26 | #include <aidl/android/hardware/audio/core/BpStreamOut.h> |
| 27 | #include <fmq/AidlMessageQueue.h> |
| 28 | #include <media/audiohal/EffectHalInterface.h> |
| 29 | #include <media/audiohal/StreamHalInterface.h> |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 30 | |
| 31 | #include "ConversionHelperAidl.h" |
| 32 | #include "StreamPowerLog.h" |
| 33 | |
| 34 | namespace android { |
| 35 | |
Mikhail Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 36 | class StreamContextAidl { |
| 37 | public: |
| 38 | typedef AidlMessageQueue<::aidl::android::hardware::audio::core::StreamDescriptor::Command, |
| 39 | ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> CommandMQ; |
| 40 | typedef AidlMessageQueue<::aidl::android::hardware::audio::core::StreamDescriptor::Reply, |
| 41 | ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> ReplyMQ; |
| 42 | typedef AidlMessageQueue<int8_t, |
| 43 | ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> DataMQ; |
| 44 | |
| 45 | explicit StreamContextAidl( |
| 46 | const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor) |
| 47 | : mFrameSizeBytes(descriptor.frameSizeBytes), |
| 48 | mCommandMQ(new CommandMQ(descriptor.command)), |
| 49 | mReplyMQ(new ReplyMQ(descriptor.reply)), |
| 50 | mBufferSizeFrames(descriptor.bufferSizeFrames), |
| 51 | mDataMQ(maybeCreateDataMQ(descriptor)) {} |
| 52 | StreamContextAidl(StreamContextAidl&& other) : |
| 53 | mFrameSizeBytes(other.mFrameSizeBytes), |
| 54 | mCommandMQ(std::move(other.mCommandMQ)), |
| 55 | mReplyMQ(std::move(other.mReplyMQ)), |
| 56 | mBufferSizeFrames(other.mBufferSizeFrames), |
| 57 | mDataMQ(std::move(other.mDataMQ)) {} |
| 58 | StreamContextAidl& operator=(StreamContextAidl&& other) { |
| 59 | mFrameSizeBytes = other.mFrameSizeBytes; |
| 60 | mCommandMQ = std::move(other.mCommandMQ); |
| 61 | mReplyMQ = std::move(other.mReplyMQ); |
| 62 | mBufferSizeFrames = other.mBufferSizeFrames; |
| 63 | mDataMQ = std::move(other.mDataMQ); |
| 64 | return *this; |
| 65 | } |
| 66 | bool isValid() const { |
| 67 | return mFrameSizeBytes != 0 && |
| 68 | mCommandMQ != nullptr && mCommandMQ->isValid() && |
| 69 | mReplyMQ != nullptr && mReplyMQ->isValid() && |
| 70 | (mDataMQ != nullptr || ( |
| 71 | mDataMQ->isValid() && |
| 72 | mDataMQ->getQuantumCount() * mDataMQ->getQuantumSize() >= |
| 73 | mFrameSizeBytes * mBufferSizeFrames)); |
| 74 | } |
| 75 | size_t getBufferSizeBytes() const { return mFrameSizeBytes * mBufferSizeFrames; } |
| 76 | size_t getBufferSizeFrames() const { return mBufferSizeFrames; } |
| 77 | CommandMQ* getCommandMQ() const { return mCommandMQ.get(); } |
| 78 | DataMQ* getDataMQ() const { return mDataMQ.get(); } |
| 79 | size_t getFrameSizeBytes() const { return mFrameSizeBytes; } |
| 80 | ReplyMQ* getReplyMQ() const { return mReplyMQ.get(); } |
| 81 | |
| 82 | private: |
| 83 | static std::unique_ptr<DataMQ> maybeCreateDataMQ( |
| 84 | const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor) { |
| 85 | using Tag = ::aidl::android::hardware::audio::core::StreamDescriptor::AudioBuffer::Tag; |
| 86 | if (descriptor.audio.getTag() == Tag::fmq) { |
| 87 | return std::make_unique<DataMQ>(descriptor.audio.get<Tag::fmq>()); |
| 88 | } |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
| 92 | size_t mFrameSizeBytes; |
| 93 | std::unique_ptr<CommandMQ> mCommandMQ; |
| 94 | std::unique_ptr<ReplyMQ> mReplyMQ; |
| 95 | size_t mBufferSizeFrames; |
| 96 | std::unique_ptr<DataMQ> mDataMQ; |
| 97 | }; |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 98 | |
| 99 | class StreamHalAidl : public virtual StreamHalInterface, public ConversionHelperAidl { |
| 100 | public: |
| 101 | // Return size of input/output buffer in bytes for this stream - eg. 4800. |
| 102 | status_t getBufferSize(size_t *size) override; |
| 103 | |
| 104 | // Return the base configuration of the stream: |
| 105 | // - channel mask; |
| 106 | // - format - e.g. AUDIO_FORMAT_PCM_16_BIT; |
| 107 | // - sampling rate in Hz - eg. 44100. |
| 108 | status_t getAudioProperties(audio_config_base_t *configBase) override; |
| 109 | |
| 110 | // Set audio stream parameters. |
| 111 | status_t setParameters(const String8& kvPairs) override; |
| 112 | |
| 113 | // Get audio stream parameters. |
| 114 | status_t getParameters(const String8& keys, String8 *values) override; |
| 115 | |
| 116 | // Return the frame size (number of bytes per sample) of a stream. |
| 117 | status_t getFrameSize(size_t *size) override; |
| 118 | |
| 119 | // Add or remove the effect on the stream. |
| 120 | status_t addEffect(sp<EffectHalInterface> effect) override; |
| 121 | status_t removeEffect(sp<EffectHalInterface> effect) override; |
| 122 | |
| 123 | // Put the audio hardware input/output into standby mode. |
| 124 | status_t standby() override; |
| 125 | |
| 126 | status_t dump(int fd, const Vector<String16>& args) override; |
| 127 | |
| 128 | // Start a stream operating in mmap mode. |
| 129 | status_t start() override; |
| 130 | |
| 131 | // Stop a stream operating in mmap mode. |
| 132 | status_t stop() override; |
| 133 | |
| 134 | // Retrieve information on the data buffer in mmap mode. |
| 135 | status_t createMmapBuffer(int32_t minSizeFrames, |
| 136 | struct audio_mmap_buffer_info *info) override; |
| 137 | |
| 138 | // Get current read/write position in the mmap buffer |
| 139 | status_t getMmapPosition(struct audio_mmap_position *position) override; |
| 140 | |
| 141 | // Set the priority of the thread that interacts with the HAL |
| 142 | // (must match the priority of the audioflinger's thread that calls 'read' / 'write') |
| 143 | status_t setHalThreadPriority(int priority) override; |
| 144 | |
| 145 | status_t legacyCreateAudioPatch(const struct audio_port_config& port, |
| 146 | std::optional<audio_source_t> source, |
| 147 | audio_devices_t type) override; |
| 148 | |
| 149 | status_t legacyReleaseAudioPatch() override; |
| 150 | |
| 151 | protected: |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 152 | template<class T> |
| 153 | static std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> getStreamCommon( |
| 154 | const std::shared_ptr<T>& stream); |
| 155 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 156 | // Subclasses can not be constructed directly by clients. |
| 157 | StreamHalAidl(std::string_view className, |
| 158 | bool isInput, |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 159 | const audio_config& config, |
Mikhail Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 160 | int32_t nominalLatency, |
| 161 | StreamContextAidl&& context, |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 162 | const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon>& stream); |
| 163 | |
| 164 | ~StreamHalAidl() override; |
| 165 | |
| 166 | status_t getHalPid(pid_t *pid); |
| 167 | |
| 168 | bool requestHalThreadPriority(pid_t threadPid, pid_t threadId); |
| 169 | |
Mikhail Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 170 | status_t getLatency(uint32_t *latency); |
| 171 | |
| 172 | status_t getObservablePosition(int64_t *frames, int64_t *timestamp); |
| 173 | |
| 174 | status_t getXruns(int32_t *frames); |
| 175 | |
| 176 | status_t transfer(void *buffer, size_t bytes, size_t *transferred); |
| 177 | |
| 178 | status_t pause( |
| 179 | ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr); |
| 180 | |
| 181 | status_t resume( |
| 182 | ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr); |
| 183 | |
| 184 | status_t drain(bool earlyNotify, |
| 185 | ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr); |
| 186 | |
| 187 | status_t flush( |
| 188 | ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr); |
| 189 | |
| 190 | status_t exit(); |
| 191 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 192 | const bool mIsInput; |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 193 | const audio_config_base_t mConfig; |
Mikhail Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 194 | const StreamContextAidl mContext; |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 195 | |
| 196 | private: |
Mikhail Naganov | f56ce78 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 197 | static audio_config_base_t configToBase(const audio_config& config) { |
| 198 | audio_config_base_t result = AUDIO_CONFIG_BASE_INITIALIZER; |
| 199 | result.sample_rate = config.sample_rate; |
| 200 | result.channel_mask = config.channel_mask; |
| 201 | result.format = config.format; |
| 202 | return result; |
| 203 | } |
Mikhail Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 204 | ::aidl::android::hardware::audio::core::StreamDescriptor::State getState() { |
| 205 | std::lock_guard l(mLock); |
| 206 | return mLastReply.state; |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 207 | } |
Mikhail Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 208 | status_t sendCommand( |
| 209 | const ::aidl::android::hardware::audio::core::StreamDescriptor::Command &command, |
| 210 | ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr, |
| 211 | bool safeFromNonWorkerThread = false); |
| 212 | status_t updateCountersIfNeeded( |
| 213 | ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr); |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 214 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 215 | const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> mStream; |
Mikhail Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 216 | std::mutex mLock; |
| 217 | ::aidl::android::hardware::audio::core::StreamDescriptor::Reply mLastReply GUARDED_BY(mLock); |
| 218 | // mStreamPowerLog is used for audio signal power logging. |
| 219 | StreamPowerLog mStreamPowerLog; |
| 220 | std::atomic<pid_t> mWorkerTid = -1; |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 221 | }; |
| 222 | |
Mikhail Naganov | b0c5525 | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 223 | class CallbackBroker; |
| 224 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 225 | class StreamOutHalAidl : public StreamOutHalInterface, public StreamHalAidl { |
| 226 | public: |
| 227 | // Return the audio hardware driver estimated latency in milliseconds. |
| 228 | status_t getLatency(uint32_t *latency) override; |
| 229 | |
| 230 | // Use this method in situations where audio mixing is done in the hardware. |
| 231 | status_t setVolume(float left, float right) override; |
| 232 | |
| 233 | // Selects the audio presentation (if available). |
| 234 | status_t selectPresentation(int presentationId, int programId) override; |
| 235 | |
| 236 | // Write audio buffer to driver. |
| 237 | status_t write(const void *buffer, size_t bytes, size_t *written) override; |
| 238 | |
| 239 | // Return the number of audio frames written by the audio dsp to DAC since |
| 240 | // the output has exited standby. |
| 241 | status_t getRenderPosition(uint32_t *dspFrames) override; |
| 242 | |
| 243 | // Get the local time at which the next write to the audio driver will be presented. |
| 244 | status_t getNextWriteTimestamp(int64_t *timestamp) override; |
| 245 | |
| 246 | // Set the callback for notifying completion of non-blocking write and drain. |
| 247 | status_t setCallback(wp<StreamOutHalInterfaceCallback> callback) override; |
| 248 | |
| 249 | // Returns whether pause and resume operations are supported. |
| 250 | status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume) override; |
| 251 | |
| 252 | // Notifies to the audio driver to resume playback following a pause. |
| 253 | status_t pause() override; |
| 254 | |
| 255 | // Notifies to the audio driver to resume playback following a pause. |
| 256 | status_t resume() override; |
| 257 | |
| 258 | // Returns whether drain operation is supported. |
| 259 | status_t supportsDrain(bool *supportsDrain) override; |
| 260 | |
| 261 | // Requests notification when data buffered by the driver/hardware has been played. |
| 262 | status_t drain(bool earlyNotify) override; |
| 263 | |
| 264 | // Notifies to the audio driver to flush the queued data. |
| 265 | status_t flush() override; |
| 266 | |
| 267 | // Return a recent count of the number of audio frames presented to an external observer. |
| 268 | status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp) override; |
| 269 | |
| 270 | // Called when the metadata of the stream's source has been changed. |
| 271 | status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override; |
| 272 | |
| 273 | // Returns the Dual Mono mode presentation setting. |
| 274 | status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override; |
| 275 | |
| 276 | // Sets the Dual Mono mode presentation on the output device. |
| 277 | status_t setDualMonoMode(audio_dual_mono_mode_t mode) override; |
| 278 | |
| 279 | // Returns the Audio Description Mix level in dB. |
| 280 | status_t getAudioDescriptionMixLevel(float* leveldB) override; |
| 281 | |
| 282 | // Sets the Audio Description Mix level in dB. |
| 283 | status_t setAudioDescriptionMixLevel(float leveldB) override; |
| 284 | |
| 285 | // Retrieves current playback rate parameters. |
| 286 | status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override; |
| 287 | |
| 288 | // Sets the playback rate parameters that control playback behavior. |
| 289 | status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override; |
| 290 | |
| 291 | status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override; |
| 292 | |
| 293 | status_t setLatencyMode(audio_latency_mode_t mode) override; |
| 294 | status_t getRecommendedLatencyModes(std::vector<audio_latency_mode_t> *modes) override; |
| 295 | status_t setLatencyModeCallback( |
| 296 | const sp<StreamOutHalInterfaceLatencyModeCallback>& callback) override; |
| 297 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 298 | status_t exit() override; |
| 299 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 300 | private: |
| 301 | friend class sp<StreamOutHalAidl>; |
| 302 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 303 | const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut> mStream; |
Mikhail Naganov | b0c5525 | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 304 | const wp<CallbackBroker> mCallbackBroker; |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 305 | |
| 306 | // Can not be constructed directly by clients. |
| 307 | StreamOutHalAidl( |
Mikhail Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 308 | const audio_config& config, StreamContextAidl&& context, int32_t nominalLatency, |
Mikhail Naganov | b0c5525 | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 309 | const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut>& stream, |
| 310 | const sp<CallbackBroker>& callbackBroker); |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 311 | |
Mikhail Naganov | b0c5525 | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 312 | ~StreamOutHalAidl() override; |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 313 | }; |
| 314 | |
| 315 | class StreamInHalAidl : public StreamInHalInterface, public StreamHalAidl { |
| 316 | public: |
| 317 | // Set the input gain for the audio driver. |
| 318 | status_t setGain(float gain) override; |
| 319 | |
| 320 | // Read audio buffer in from driver. |
| 321 | status_t read(void *buffer, size_t bytes, size_t *read) override; |
| 322 | |
| 323 | // Return the amount of input frames lost in the audio driver. |
| 324 | status_t getInputFramesLost(uint32_t *framesLost) override; |
| 325 | |
| 326 | // Return a recent count of the number of audio frames received and |
| 327 | // the clock time associated with that frame count. |
| 328 | status_t getCapturePosition(int64_t *frames, int64_t *time) override; |
| 329 | |
| 330 | // Get active microphones |
| 331 | status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones) override; |
| 332 | |
| 333 | // Set microphone direction (for processing) |
| 334 | status_t setPreferredMicrophoneDirection( |
| 335 | audio_microphone_direction_t direction) override; |
| 336 | |
| 337 | // Set microphone zoom (for processing) |
| 338 | status_t setPreferredMicrophoneFieldDimension(float zoom) override; |
| 339 | |
| 340 | // Called when the metadata of the stream's sink has been changed. |
| 341 | status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override; |
| 342 | |
| 343 | private: |
| 344 | friend class sp<StreamInHalAidl>; |
| 345 | |
| 346 | const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn> mStream; |
| 347 | |
| 348 | // Can not be constructed directly by clients. |
| 349 | StreamInHalAidl( |
Mikhail Naganov | 8bd806e | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 350 | const audio_config& config, StreamContextAidl&& context, int32_t nominalLatency, |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 351 | const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn>& stream); |
| 352 | |
| 353 | ~StreamInHalAidl() override = default; |
| 354 | }; |
| 355 | |
| 356 | } // namespace android |