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