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 <android/hardware/audio/2.0/IStream.h> 23#include <android/hardware/audio/2.0/IStreamIn.h> 24#include <android/hardware/audio/2.0/IStreamOut.h> 25#include <fmq/EventFlag.h> 26#include <fmq/MessageQueue.h> 27#include <media/audiohal/StreamHalInterface.h> 28 29#include "ConversionHelperHidl.h" 30#include "StreamPowerLog.h" 31 32using ::android::hardware::audio::V2_0::IStream; 33using ::android::hardware::audio::V2_0::IStreamIn; 34using ::android::hardware::audio::V2_0::IStreamOut; 35using ::android::hardware::EventFlag; 36using ::android::hardware::MessageQueue; 37using ::android::hardware::Return; 38using ReadParameters = ::android::hardware::audio::V2_0::IStreamIn::ReadParameters; 39using ReadStatus = ::android::hardware::audio::V2_0::IStreamIn::ReadStatus; 40using WriteCommand = ::android::hardware::audio::V2_0::IStreamOut::WriteCommand; 41using WriteStatus = ::android::hardware::audio::V2_0::IStreamOut::WriteStatus; 42 43namespace android { 44 45class DeviceHalHidl; 46 47class StreamHalHidl : public virtual StreamHalInterface, public ConversionHelperHidl 48{ 49 public: 50 // Return the sampling rate in Hz - eg. 44100. 51 virtual status_t getSampleRate(uint32_t *rate); 52 53 // Return size of input/output buffer in bytes for this stream - eg. 4800. 54 virtual status_t getBufferSize(size_t *size); 55 56 // Return the channel mask. 57 virtual status_t getChannelMask(audio_channel_mask_t *mask); 58 59 // Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT. 60 virtual status_t getFormat(audio_format_t *format); 61 62 // Convenience method. 63 virtual status_t getAudioProperties( 64 uint32_t *sampleRate, audio_channel_mask_t *mask, audio_format_t *format); 65 66 // Set audio stream parameters. 67 virtual status_t setParameters(const String8& kvPairs); 68 69 // Get audio stream parameters. 70 virtual status_t getParameters(const String8& keys, String8 *values); 71 72 // Add or remove the effect on the stream. 73 virtual status_t addEffect(sp<EffectHalInterface> effect); 74 virtual status_t removeEffect(sp<EffectHalInterface> effect); 75 76 // Put the audio hardware input/output into standby mode. 77 virtual status_t standby(); 78 79 virtual status_t dump(int fd); 80 81 // Start a stream operating in mmap mode. 82 virtual status_t start(); 83 84 // Stop a stream operating in mmap mode. 85 virtual status_t stop(); 86 87 // Retrieve information on the data buffer in mmap mode. 88 virtual status_t createMmapBuffer(int32_t minSizeFrames, 89 struct audio_mmap_buffer_info *info); 90 91 // Get current read/write position in the mmap buffer 92 virtual status_t getMmapPosition(struct audio_mmap_position *position); 93 94 // Set the priority of the thread that interacts with the HAL 95 // (must match the priority of the audioflinger's thread that calls 'read' / 'write') 96 virtual status_t setHalThreadPriority(int priority); 97 98 protected: 99 // Subclasses can not be constructed directly by clients. 100 explicit StreamHalHidl(IStream *stream); 101 102 // The destructor automatically closes the stream. 103 virtual ~StreamHalHidl(); 104 105 status_t getCachedBufferSize(size_t *size); 106 107 bool requestHalThreadPriority(pid_t threadPid, pid_t threadId); 108 109 // mStreamPowerLog is used for audio signal power logging. 110 StreamPowerLog mStreamPowerLog; 111 112 private: 113 const int HAL_THREAD_PRIORITY_DEFAULT = -1; 114 IStream *mStream; 115 int mHalThreadPriority; 116 size_t mCachedBufferSize; 117}; 118 119class StreamOutHalHidl : public StreamOutHalInterface, public StreamHalHidl { 120 public: 121 // Return the frame size (number of bytes per sample) of a stream. 122 virtual status_t getFrameSize(size_t *size); 123 124 // Return the audio hardware driver estimated latency in milliseconds. 125 virtual status_t getLatency(uint32_t *latency); 126 127 // Use this method in situations where audio mixing is done in the hardware. 128 virtual status_t setVolume(float left, float right); 129 130 // Write audio buffer to driver. 131 virtual status_t write(const void *buffer, size_t bytes, size_t *written); 132 133 // Return the number of audio frames written by the audio dsp to DAC since 134 // the output has exited standby. 135 virtual status_t getRenderPosition(uint32_t *dspFrames); 136 137 // Get the local time at which the next write to the audio driver will be presented. 138 virtual status_t getNextWriteTimestamp(int64_t *timestamp); 139 140 // Set the callback for notifying completion of non-blocking write and drain. 141 virtual status_t setCallback(wp<StreamOutHalInterfaceCallback> callback); 142 143 // Returns whether pause and resume operations are supported. 144 virtual status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume); 145 146 // Notifies to the audio driver to resume playback following a pause. 147 virtual status_t pause(); 148 149 // Notifies to the audio driver to resume playback following a pause. 150 virtual status_t resume(); 151 152 // Returns whether drain operation is supported. 153 virtual status_t supportsDrain(bool *supportsDrain); 154 155 // Requests notification when data buffered by the driver/hardware has been played. 156 virtual status_t drain(bool earlyNotify); 157 158 // Notifies to the audio driver to flush the queued data. 159 virtual status_t flush(); 160 161 // Return a recent count of the number of audio frames presented to an external observer. 162 virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp); 163 164 // Called when the metadata of the stream's source has been changed. 165 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override; 166 167 // Methods used by StreamOutCallback (HIDL). 168 void onWriteReady(); 169 void onDrainReady(); 170 void onError(); 171 172 private: 173 friend class DeviceHalHidl; 174 typedef MessageQueue<WriteCommand, hardware::kSynchronizedReadWrite> CommandMQ; 175 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ; 176 typedef MessageQueue<WriteStatus, hardware::kSynchronizedReadWrite> StatusMQ; 177 178 wp<StreamOutHalInterfaceCallback> mCallback; 179 sp<IStreamOut> mStream; 180 std::unique_ptr<CommandMQ> mCommandMQ; 181 std::unique_ptr<DataMQ> mDataMQ; 182 std::unique_ptr<StatusMQ> mStatusMQ; 183 std::atomic<pid_t> mWriterClient; 184 EventFlag* mEfGroup; 185 186 // Can not be constructed directly by clients. 187 StreamOutHalHidl(const sp<IStreamOut>& stream); 188 189 virtual ~StreamOutHalHidl(); 190 191 using WriterCallback = std::function<void(const WriteStatus& writeStatus)>; 192 status_t callWriterThread( 193 WriteCommand cmd, const char* cmdName, 194 const uint8_t* data, size_t dataSize, WriterCallback callback); 195 status_t prepareForWriting(size_t bufferSize); 196}; 197 198class StreamInHalHidl : public StreamInHalInterface, public StreamHalHidl { 199 public: 200 // Return the frame size (number of bytes per sample) of a stream. 201 virtual status_t getFrameSize(size_t *size); 202 203 // Set the input gain for the audio driver. 204 virtual status_t setGain(float gain); 205 206 // Read audio buffer in from driver. 207 virtual status_t read(void *buffer, size_t bytes, size_t *read); 208 209 // Return the amount of input frames lost in the audio driver. 210 virtual status_t getInputFramesLost(uint32_t *framesLost); 211 212 // Return a recent count of the number of audio frames received and 213 // the clock time associated with that frame count. 214 virtual status_t getCapturePosition(int64_t *frames, int64_t *time); 215 216 // Get active microphones 217 virtual status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones); 218 219 // Called when the metadata of the stream's sink has been changed. 220 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override; 221 222 private: 223 friend class DeviceHalHidl; 224 typedef MessageQueue<ReadParameters, hardware::kSynchronizedReadWrite> CommandMQ; 225 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ; 226 typedef MessageQueue<ReadStatus, hardware::kSynchronizedReadWrite> StatusMQ; 227 228 sp<IStreamIn> mStream; 229 std::unique_ptr<CommandMQ> mCommandMQ; 230 std::unique_ptr<DataMQ> mDataMQ; 231 std::unique_ptr<StatusMQ> mStatusMQ; 232 std::atomic<pid_t> mReaderClient; 233 EventFlag* mEfGroup; 234 235 // Can not be constructed directly by clients. 236 StreamInHalHidl(const sp<IStreamIn>& stream); 237 238 virtual ~StreamInHalHidl(); 239 240 using ReaderCallback = std::function<void(const ReadStatus& readStatus)>; 241 status_t callReaderThread( 242 const ReadParameters& params, const char* cmdName, ReaderCallback callback); 243 status_t prepareForReading(size_t bufferSize); 244}; 245 246} // namespace android 247 248#endif // ANDROID_HARDWARE_STREAM_HAL_HIDL_H 249