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 17 #ifndef ANDROID_HARDWARE_STREAM_HAL_HIDL_H 18 #define ANDROID_HARDWARE_STREAM_HAL_HIDL_H 19 20 #include <atomic> 21 22 #include PATH(android/hardware/audio/CORE_TYPES_FILE_VERSION/IStream.h) 23 #include PATH(android/hardware/audio/CORE_TYPES_FILE_VERSION/IStreamIn.h) 24 #include PATH(android/hardware/audio/FILE_VERSION/IStreamOut.h) 25 #include <fmq/EventFlag.h> 26 #include <fmq/MessageQueue.h> 27 #include <media/audiohal/EffectHalInterface.h> 28 #include <media/audiohal/StreamHalInterface.h> 29 #include <mediautils/Synchronization.h> 30 31 #include "CoreConversionHelperHidl.h" 32 #include "StreamPowerLog.h" 33 34 using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStream; 35 using ::android::hardware::EventFlag; 36 using ::android::hardware::MessageQueue; 37 using ::android::hardware::Return; 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; 41 using WriteCommand = ::android::hardware::audio::CPP_VERSION::IStreamOut::WriteCommand; 42 using WriteStatus = ::android::hardware::audio::CPP_VERSION::IStreamOut::WriteStatus; 43 44 namespace android { 45 46 class DeviceHalHidl; 47 48 class StreamHalHidl : public virtual StreamHalInterface, public CoreConversionHelperHidl 49 { 50 public: 51 // Return size of input/output buffer in bytes for this stream - eg. 4800. 52 virtual status_t getBufferSize(size_t *size); 53 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); 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 73 virtual status_t dump(int fd, const Vector<String16>& args) override; 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 status_t legacyCreateAudioPatch(const struct audio_port_config& port, 93 std::optional<audio_source_t> source, 94 audio_devices_t type) override; 95 96 status_t legacyReleaseAudioPatch() override; 97 98 protected: 99 // Subclasses can not be constructed directly by clients. 100 StreamHalHidl(std::string_view className, IStream *stream); 101 102 ~StreamHalHidl() override; 103 104 status_t getCachedBufferSize(size_t *size); 105 106 status_t getHalPid(pid_t *pid); 107 108 bool requestHalThreadPriority(pid_t threadPid, pid_t threadId); 109 110 // mStreamPowerLog is used for audio signal power logging. 111 StreamPowerLog mStreamPowerLog; 112 113 private: 114 const int HAL_THREAD_PRIORITY_DEFAULT = -1; 115 IStream * const mStream; 116 int mHalThreadPriority; 117 size_t mCachedBufferSize; 118 }; 119 120 class StreamOutHalHidl : public StreamOutHalInterface, public StreamHalHidl { 121 public: 122 // Return the frame size (number of bytes per sample) of a stream. 123 virtual status_t getFrameSize(size_t *size); 124 125 // Return the audio hardware driver estimated latency in milliseconds. 126 virtual status_t getLatency(uint32_t *latency); 127 128 // Use this method in situations where audio mixing is done in the hardware. 129 virtual status_t setVolume(float left, float right); 130 131 // Selects the audio presentation (if available). 132 virtual status_t selectPresentation(int presentationId, int programId); 133 134 // Write audio buffer to driver. 135 virtual status_t write(const void *buffer, size_t bytes, size_t *written); 136 137 // Return the number of audio frames written by the audio dsp to DAC since 138 // the output has exited standby. 139 virtual status_t getRenderPosition(uint32_t *dspFrames); 140 141 // Get the local time at which the next write to the audio driver will be presented. 142 virtual status_t getNextWriteTimestamp(int64_t *timestamp); 143 144 // Set the callback for notifying completion of non-blocking write and drain. 145 virtual status_t setCallback(wp<StreamOutHalInterfaceCallback> callback); 146 147 // Returns whether pause and resume operations are supported. 148 virtual status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume); 149 150 // Notifies to the audio driver to resume playback following a pause. 151 virtual status_t pause(); 152 153 // Notifies to the audio driver to resume playback following a pause. 154 virtual status_t resume(); 155 156 // Returns whether drain operation is supported. 157 virtual status_t supportsDrain(bool *supportsDrain); 158 159 // Requests notification when data buffered by the driver/hardware has been played. 160 virtual status_t drain(bool earlyNotify); 161 162 // Notifies to the audio driver to flush the queued data. 163 virtual status_t flush(); 164 165 // Return a recent count of the number of audio frames presented to an external observer. 166 virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp); 167 168 // Called when the metadata of the stream's source has been changed. 169 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override; 170 171 // Methods used by StreamOutCallback (HIDL). 172 void onWriteReady(); 173 void onDrainReady(); 174 void onError(); 175 176 // Returns the Dual Mono mode presentation setting. 177 status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override; 178 179 // Sets the Dual Mono mode presentation on the output device. 180 status_t setDualMonoMode(audio_dual_mono_mode_t mode) override; 181 182 // Returns the Audio Description Mix level in dB. 183 status_t getAudioDescriptionMixLevel(float* leveldB) override; 184 185 // Sets the Audio Description Mix level in dB. 186 status_t setAudioDescriptionMixLevel(float leveldB) override; 187 188 // Retrieves current playback rate parameters. 189 status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override; 190 191 // Sets the playback rate parameters that control playback behavior. 192 status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override; 193 194 status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override; 195 196 // Methods used by StreamCodecFormatCallback (HIDL). 197 void onCodecFormatChanged(const std::basic_string<uint8_t>& metadataBs); 198 199 status_t setLatencyMode(audio_latency_mode_t mode) override; 200 status_t getRecommendedLatencyModes(std::vector<audio_latency_mode_t> *modes) override; 201 status_t setLatencyModeCallback( 202 const sp<StreamOutHalInterfaceLatencyModeCallback>& callback) override; 203 204 void onRecommendedLatencyModeChanged(const std::vector<audio_latency_mode_t>& modes); 205 206 status_t exit() override; 207 208 private: 209 friend class DeviceHalHidl; 210 typedef MessageQueue<WriteCommand, hardware::kSynchronizedReadWrite> CommandMQ; 211 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ; 212 typedef MessageQueue<WriteStatus, hardware::kSynchronizedReadWrite> StatusMQ; 213 214 mediautils::atomic_wp<StreamOutHalInterfaceCallback> mCallback; 215 mediautils::atomic_wp<StreamOutHalInterfaceEventCallback> mEventCallback; 216 mediautils::atomic_wp<StreamOutHalInterfaceLatencyModeCallback> mLatencyModeCallback; 217 218 const sp<::android::hardware::audio::CPP_VERSION::IStreamOut> mStream; 219 std::unique_ptr<CommandMQ> mCommandMQ; 220 std::unique_ptr<DataMQ> mDataMQ; 221 std::unique_ptr<StatusMQ> mStatusMQ; 222 std::atomic<pid_t> mWriterClient; 223 EventFlag* mEfGroup; 224 225 // Can not be constructed directly by clients. 226 StreamOutHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IStreamOut>& stream); 227 228 virtual ~StreamOutHalHidl(); 229 230 using WriterCallback = std::function<void(const WriteStatus& writeStatus)>; 231 status_t callWriterThread( 232 WriteCommand cmd, const char* cmdName, 233 const uint8_t* data, size_t dataSize, WriterCallback callback); 234 status_t prepareForWriting(size_t bufferSize); 235 }; 236 237 class StreamInHalHidl : public StreamInHalInterface, public StreamHalHidl { 238 public: 239 // Return the frame size (number of bytes per sample) of a stream. 240 virtual status_t getFrameSize(size_t *size); 241 242 // Set the input gain for the audio driver. 243 virtual status_t setGain(float gain); 244 245 // Read audio buffer in from driver. 246 virtual status_t read(void *buffer, size_t bytes, size_t *read); 247 248 // Return the amount of input frames lost in the audio driver. 249 virtual status_t getInputFramesLost(uint32_t *framesLost); 250 251 // Return a recent count of the number of audio frames received and 252 // the clock time associated with that frame count. 253 virtual status_t getCapturePosition(int64_t *frames, int64_t *time); 254 255 // Get active microphones 256 virtual status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones); 257 258 // Set microphone direction (for processing) 259 virtual status_t setPreferredMicrophoneDirection( 260 audio_microphone_direction_t direction) override; 261 262 // Set microphone zoom (for processing) 263 virtual status_t setPreferredMicrophoneFieldDimension(float zoom) override; 264 265 // Called when the metadata of the stream's sink has been changed. 266 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override; 267 268 private: 269 friend class DeviceHalHidl; 270 typedef MessageQueue<ReadParameters, hardware::kSynchronizedReadWrite> CommandMQ; 271 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ; 272 typedef MessageQueue<ReadStatus, hardware::kSynchronizedReadWrite> StatusMQ; 273 274 const sp<::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn> mStream; 275 std::unique_ptr<CommandMQ> mCommandMQ; 276 std::unique_ptr<DataMQ> mDataMQ; 277 std::unique_ptr<StatusMQ> mStatusMQ; 278 std::atomic<pid_t> mReaderClient; 279 EventFlag* mEfGroup; 280 281 // Can not be constructed directly by clients. 282 StreamInHalHidl( 283 const sp<::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn>& stream); 284 285 virtual ~StreamInHalHidl(); 286 287 using ReaderCallback = std::function<void(const ReadStatus& readStatus)>; 288 status_t callReaderThread( 289 const ReadParameters& params, const char* cmdName, ReaderCallback callback); 290 status_t prepareForReading(size_t bufferSize); 291 }; 292 293 } // namespace android 294 295 #endif // ANDROID_HARDWARE_STREAM_HAL_HIDL_H 296