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