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 32 using ::android::hardware::audio::V2_0::IStream; 33 using ::android::hardware::audio::V2_0::IStreamIn; 34 using ::android::hardware::audio::V2_0::IStreamOut; 35 using ::android::hardware::EventFlag; 36 using ::android::hardware::MessageQueue; 37 using ::android::hardware::Return; 38 using ReadParameters = ::android::hardware::audio::V2_0::IStreamIn::ReadParameters; 39 using ReadStatus = ::android::hardware::audio::V2_0::IStreamIn::ReadStatus; 40 using WriteCommand = ::android::hardware::audio::V2_0::IStreamOut::WriteCommand; 41 using WriteStatus = ::android::hardware::audio::V2_0::IStreamOut::WriteStatus; 42 43 namespace android { 44 45 class DeviceHalHidl; 46 47 class 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 119 class 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 // Methods used by StreamOutCallback (HIDL). 165 void onWriteReady(); 166 void onDrainReady(); 167 void onError(); 168 169 private: 170 friend class DeviceHalHidl; 171 typedef MessageQueue<WriteCommand, hardware::kSynchronizedReadWrite> CommandMQ; 172 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ; 173 typedef MessageQueue<WriteStatus, hardware::kSynchronizedReadWrite> StatusMQ; 174 175 wp<StreamOutHalInterfaceCallback> mCallback; 176 sp<IStreamOut> mStream; 177 std::unique_ptr<CommandMQ> mCommandMQ; 178 std::unique_ptr<DataMQ> mDataMQ; 179 std::unique_ptr<StatusMQ> mStatusMQ; 180 std::atomic<pid_t> mWriterClient; 181 EventFlag* mEfGroup; 182 183 // Can not be constructed directly by clients. 184 StreamOutHalHidl(const sp<IStreamOut>& stream); 185 186 virtual ~StreamOutHalHidl(); 187 188 using WriterCallback = std::function<void(const WriteStatus& writeStatus)>; 189 status_t callWriterThread( 190 WriteCommand cmd, const char* cmdName, 191 const uint8_t* data, size_t dataSize, WriterCallback callback); 192 status_t prepareForWriting(size_t bufferSize); 193 }; 194 195 class StreamInHalHidl : public StreamInHalInterface, public StreamHalHidl { 196 public: 197 // Return the frame size (number of bytes per sample) of a stream. 198 virtual status_t getFrameSize(size_t *size); 199 200 // Set the input gain for the audio driver. 201 virtual status_t setGain(float gain); 202 203 // Read audio buffer in from driver. 204 virtual status_t read(void *buffer, size_t bytes, size_t *read); 205 206 // Return the amount of input frames lost in the audio driver. 207 virtual status_t getInputFramesLost(uint32_t *framesLost); 208 209 // Return a recent count of the number of audio frames received and 210 // the clock time associated with that frame count. 211 virtual status_t getCapturePosition(int64_t *frames, int64_t *time); 212 213 private: 214 friend class DeviceHalHidl; 215 typedef MessageQueue<ReadParameters, hardware::kSynchronizedReadWrite> CommandMQ; 216 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ; 217 typedef MessageQueue<ReadStatus, hardware::kSynchronizedReadWrite> StatusMQ; 218 219 sp<IStreamIn> mStream; 220 std::unique_ptr<CommandMQ> mCommandMQ; 221 std::unique_ptr<DataMQ> mDataMQ; 222 std::unique_ptr<StatusMQ> mStatusMQ; 223 std::atomic<pid_t> mReaderClient; 224 EventFlag* mEfGroup; 225 226 // Can not be constructed directly by clients. 227 StreamInHalHidl(const sp<IStreamIn>& stream); 228 229 virtual ~StreamInHalHidl(); 230 231 using ReaderCallback = std::function<void(const ReadStatus& readStatus)>; 232 status_t callReaderThread( 233 const ReadParameters& params, const char* cmdName, ReaderCallback callback); 234 status_t prepareForReading(size_t bufferSize); 235 }; 236 237 } // namespace android 238 239 #endif // ANDROID_HARDWARE_STREAM_HAL_HIDL_H 240