Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HARDWARE_STREAM_HAL_HIDL_H |
| 18 | #define ANDROID_HARDWARE_STREAM_HAL_HIDL_H |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 19 | |
| 20 | #include <atomic> |
| 21 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 22 | #include PATH(android/hardware/audio/CORE_TYPES_FILE_VERSION/IStream.h) |
Mikhail Naganov | accbe8a | 2022-02-03 23:45:36 +0000 | [diff] [blame^] | 23 | #include PATH(android/hardware/audio/CORE_TYPES_FILE_VERSION/IStreamIn.h) |
Kevin Rocard | 95213bf | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 24 | #include PATH(android/hardware/audio/FILE_VERSION/IStreamOut.h) |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 25 | #include <fmq/EventFlag.h> |
| 26 | #include <fmq/MessageQueue.h> |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 27 | #include <media/audiohal/EffectHalInterface.h> |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 28 | #include <media/audiohal/StreamHalInterface.h> |
Andy Hung | 638f45b | 2021-01-18 20:02:56 -0800 | [diff] [blame] | 29 | #include <mediautils/Synchronization.h> |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 30 | |
| 31 | #include "ConversionHelperHidl.h" |
| 32 | #include "StreamPowerLog.h" |
| 33 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 34 | using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStream; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 35 | using ::android::hardware::EventFlag; |
| 36 | using ::android::hardware::MessageQueue; |
| 37 | using ::android::hardware::Return; |
Mikhail Naganov | accbe8a | 2022-02-03 23:45:36 +0000 | [diff] [blame^] | 38 | using ReadParameters = |
| 39 | ::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn::ReadParameters; |
| 40 | using ReadStatus = ::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn::ReadStatus; |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 41 | using WriteCommand = ::android::hardware::audio::CPP_VERSION::IStreamOut::WriteCommand; |
| 42 | using WriteStatus = ::android::hardware::audio::CPP_VERSION::IStreamOut::WriteStatus; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 43 | |
| 44 | namespace android { |
| 45 | |
| 46 | class DeviceHalHidl; |
| 47 | |
| 48 | class StreamHalHidl : public virtual StreamHalInterface, public ConversionHelperHidl |
| 49 | { |
| 50 | public: |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 51 | // Return size of input/output buffer in bytes for this stream - eg. 4800. |
| 52 | virtual status_t getBufferSize(size_t *size); |
| 53 | |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 54 | // Return the base configuration of the stream: |
| 55 | // - channel mask; |
| 56 | // - format - e.g. AUDIO_FORMAT_PCM_16_BIT; |
| 57 | // - sampling rate in Hz - eg. 44100. |
| 58 | virtual status_t getAudioProperties(audio_config_base_t *configBase); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 59 | |
| 60 | // Set audio stream parameters. |
| 61 | virtual status_t setParameters(const String8& kvPairs); |
| 62 | |
| 63 | // Get audio stream parameters. |
| 64 | virtual status_t getParameters(const String8& keys, String8 *values); |
| 65 | |
| 66 | // Add or remove the effect on the stream. |
| 67 | virtual status_t addEffect(sp<EffectHalInterface> effect); |
| 68 | virtual status_t removeEffect(sp<EffectHalInterface> effect); |
| 69 | |
| 70 | // Put the audio hardware input/output into standby mode. |
| 71 | virtual status_t standby(); |
| 72 | |
Andy Hung | 61589a4 | 2021-06-16 09:37:53 -0700 | [diff] [blame] | 73 | virtual status_t dump(int fd, const Vector<String16>& args) override; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 74 | |
| 75 | // Start a stream operating in mmap mode. |
| 76 | virtual status_t start(); |
| 77 | |
| 78 | // Stop a stream operating in mmap mode. |
| 79 | virtual status_t stop(); |
| 80 | |
| 81 | // Retrieve information on the data buffer in mmap mode. |
| 82 | virtual status_t createMmapBuffer(int32_t minSizeFrames, |
| 83 | struct audio_mmap_buffer_info *info); |
| 84 | |
| 85 | // Get current read/write position in the mmap buffer |
| 86 | virtual status_t getMmapPosition(struct audio_mmap_position *position); |
| 87 | |
| 88 | // Set the priority of the thread that interacts with the HAL |
| 89 | // (must match the priority of the audioflinger's thread that calls 'read' / 'write') |
| 90 | virtual status_t setHalThreadPriority(int priority); |
| 91 | |
| 92 | protected: |
| 93 | // Subclasses can not be constructed directly by clients. |
| 94 | explicit StreamHalHidl(IStream *stream); |
| 95 | |
Andy Hung | acb5b98 | 2021-01-20 10:12:00 -0800 | [diff] [blame] | 96 | ~StreamHalHidl() override; |
| 97 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 98 | status_t getCachedBufferSize(size_t *size); |
| 99 | |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 100 | status_t getHalPid(pid_t *pid); |
| 101 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 102 | bool requestHalThreadPriority(pid_t threadPid, pid_t threadId); |
| 103 | |
| 104 | // mStreamPowerLog is used for audio signal power logging. |
| 105 | StreamPowerLog mStreamPowerLog; |
| 106 | |
| 107 | private: |
| 108 | const int HAL_THREAD_PRIORITY_DEFAULT = -1; |
Andy Hung | 638f45b | 2021-01-18 20:02:56 -0800 | [diff] [blame] | 109 | IStream * const mStream; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 110 | int mHalThreadPriority; |
| 111 | size_t mCachedBufferSize; |
| 112 | }; |
| 113 | |
| 114 | class StreamOutHalHidl : public StreamOutHalInterface, public StreamHalHidl { |
| 115 | public: |
| 116 | // Return the frame size (number of bytes per sample) of a stream. |
| 117 | virtual status_t getFrameSize(size_t *size); |
| 118 | |
| 119 | // Return the audio hardware driver estimated latency in milliseconds. |
| 120 | virtual status_t getLatency(uint32_t *latency); |
| 121 | |
| 122 | // Use this method in situations where audio mixing is done in the hardware. |
| 123 | virtual status_t setVolume(float left, float right); |
| 124 | |
Mikhail Naganov | ac917ac | 2018-11-28 14:03:52 -0800 | [diff] [blame] | 125 | // Selects the audio presentation (if available). |
| 126 | virtual status_t selectPresentation(int presentationId, int programId); |
| 127 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 128 | // Write audio buffer to driver. |
| 129 | virtual status_t write(const void *buffer, size_t bytes, size_t *written); |
| 130 | |
| 131 | // Return the number of audio frames written by the audio dsp to DAC since |
| 132 | // the output has exited standby. |
| 133 | virtual status_t getRenderPosition(uint32_t *dspFrames); |
| 134 | |
| 135 | // Get the local time at which the next write to the audio driver will be presented. |
| 136 | virtual status_t getNextWriteTimestamp(int64_t *timestamp); |
| 137 | |
| 138 | // Set the callback for notifying completion of non-blocking write and drain. |
| 139 | virtual status_t setCallback(wp<StreamOutHalInterfaceCallback> callback); |
| 140 | |
| 141 | // Returns whether pause and resume operations are supported. |
| 142 | virtual status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume); |
| 143 | |
| 144 | // Notifies to the audio driver to resume playback following a pause. |
| 145 | virtual status_t pause(); |
| 146 | |
| 147 | // Notifies to the audio driver to resume playback following a pause. |
| 148 | virtual status_t resume(); |
| 149 | |
| 150 | // Returns whether drain operation is supported. |
| 151 | virtual status_t supportsDrain(bool *supportsDrain); |
| 152 | |
| 153 | // Requests notification when data buffered by the driver/hardware has been played. |
| 154 | virtual status_t drain(bool earlyNotify); |
| 155 | |
| 156 | // Notifies to the audio driver to flush the queued data. |
| 157 | virtual status_t flush(); |
| 158 | |
| 159 | // Return a recent count of the number of audio frames presented to an external observer. |
| 160 | virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp); |
| 161 | |
Kevin Rocard | a8975a7 | 2018-03-27 10:16:52 -0700 | [diff] [blame] | 162 | // Called when the metadata of the stream's source has been changed. |
| 163 | status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override; |
| 164 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 165 | // Methods used by StreamOutCallback (HIDL). |
| 166 | void onWriteReady(); |
| 167 | void onDrainReady(); |
| 168 | void onError(); |
| 169 | |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 170 | // Returns the Dual Mono mode presentation setting. |
| 171 | status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override; |
| 172 | |
| 173 | // Sets the Dual Mono mode presentation on the output device. |
| 174 | status_t setDualMonoMode(audio_dual_mono_mode_t mode) override; |
| 175 | |
| 176 | // Returns the Audio Description Mix level in dB. |
| 177 | status_t getAudioDescriptionMixLevel(float* leveldB) override; |
| 178 | |
| 179 | // Sets the Audio Description Mix level in dB. |
| 180 | status_t setAudioDescriptionMixLevel(float leveldB) override; |
| 181 | |
| 182 | // Retrieves current playback rate parameters. |
| 183 | status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override; |
| 184 | |
| 185 | // Sets the playback rate parameters that control playback behavior. |
| 186 | status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override; |
| 187 | |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 188 | status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override; |
| 189 | |
| 190 | // Methods used by StreamCodecFormatCallback (HIDL). |
| 191 | void onCodecFormatChanged(const std::basic_string<uint8_t>& metadataBs); |
| 192 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 193 | private: |
| 194 | friend class DeviceHalHidl; |
| 195 | typedef MessageQueue<WriteCommand, hardware::kSynchronizedReadWrite> CommandMQ; |
| 196 | typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ; |
| 197 | typedef MessageQueue<WriteStatus, hardware::kSynchronizedReadWrite> StatusMQ; |
| 198 | |
Andy Hung | 638f45b | 2021-01-18 20:02:56 -0800 | [diff] [blame] | 199 | mediautils::atomic_wp<StreamOutHalInterfaceCallback> mCallback; |
| 200 | mediautils::atomic_wp<StreamOutHalInterfaceEventCallback> mEventCallback; |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 201 | const sp<::android::hardware::audio::CPP_VERSION::IStreamOut> mStream; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 202 | std::unique_ptr<CommandMQ> mCommandMQ; |
| 203 | std::unique_ptr<DataMQ> mDataMQ; |
| 204 | std::unique_ptr<StatusMQ> mStatusMQ; |
| 205 | std::atomic<pid_t> mWriterClient; |
| 206 | EventFlag* mEfGroup; |
| 207 | |
| 208 | // Can not be constructed directly by clients. |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 209 | StreamOutHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IStreamOut>& stream); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 210 | |
| 211 | virtual ~StreamOutHalHidl(); |
| 212 | |
| 213 | using WriterCallback = std::function<void(const WriteStatus& writeStatus)>; |
| 214 | status_t callWriterThread( |
| 215 | WriteCommand cmd, const char* cmdName, |
| 216 | const uint8_t* data, size_t dataSize, WriterCallback callback); |
| 217 | status_t prepareForWriting(size_t bufferSize); |
| 218 | }; |
| 219 | |
| 220 | class StreamInHalHidl : public StreamInHalInterface, public StreamHalHidl { |
| 221 | public: |
| 222 | // Return the frame size (number of bytes per sample) of a stream. |
| 223 | virtual status_t getFrameSize(size_t *size); |
| 224 | |
| 225 | // Set the input gain for the audio driver. |
| 226 | virtual status_t setGain(float gain); |
| 227 | |
| 228 | // Read audio buffer in from driver. |
| 229 | virtual status_t read(void *buffer, size_t bytes, size_t *read); |
| 230 | |
| 231 | // Return the amount of input frames lost in the audio driver. |
| 232 | virtual status_t getInputFramesLost(uint32_t *framesLost); |
| 233 | |
| 234 | // Return a recent count of the number of audio frames received and |
| 235 | // the clock time associated with that frame count. |
| 236 | virtual status_t getCapturePosition(int64_t *frames, int64_t *time); |
| 237 | |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 238 | // Get active microphones |
| 239 | virtual status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones); |
| 240 | |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 241 | // Set microphone direction (for processing) |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 242 | virtual status_t setPreferredMicrophoneDirection( |
| 243 | audio_microphone_direction_t direction) override; |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 244 | |
| 245 | // Set microphone zoom (for processing) |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 246 | virtual status_t setPreferredMicrophoneFieldDimension(float zoom) override; |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 247 | |
Kevin Rocard | a8975a7 | 2018-03-27 10:16:52 -0700 | [diff] [blame] | 248 | // Called when the metadata of the stream's sink has been changed. |
| 249 | status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override; |
| 250 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 251 | private: |
| 252 | friend class DeviceHalHidl; |
| 253 | typedef MessageQueue<ReadParameters, hardware::kSynchronizedReadWrite> CommandMQ; |
| 254 | typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ; |
| 255 | typedef MessageQueue<ReadStatus, hardware::kSynchronizedReadWrite> StatusMQ; |
| 256 | |
Mikhail Naganov | accbe8a | 2022-02-03 23:45:36 +0000 | [diff] [blame^] | 257 | const sp<::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn> mStream; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 258 | std::unique_ptr<CommandMQ> mCommandMQ; |
| 259 | std::unique_ptr<DataMQ> mDataMQ; |
| 260 | std::unique_ptr<StatusMQ> mStatusMQ; |
| 261 | std::atomic<pid_t> mReaderClient; |
| 262 | EventFlag* mEfGroup; |
| 263 | |
| 264 | // Can not be constructed directly by clients. |
Mikhail Naganov | accbe8a | 2022-02-03 23:45:36 +0000 | [diff] [blame^] | 265 | StreamInHalHidl( |
| 266 | const sp<::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn>& stream); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 267 | |
| 268 | virtual ~StreamInHalHidl(); |
| 269 | |
| 270 | using ReaderCallback = std::function<void(const ReadStatus& readStatus)>; |
| 271 | status_t callReaderThread( |
| 272 | const ReadParameters& params, const char* cmdName, ReaderCallback callback); |
| 273 | status_t prepareForReading(size_t bufferSize); |
| 274 | }; |
| 275 | |
| 276 | } // namespace android |
| 277 | |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 278 | #endif // ANDROID_HARDWARE_STREAM_HAL_HIDL_H |