blob: 8b1c3d312998f303421585b907cdc50d27756c6a [file] [log] [blame]
Kevin Rocard4bcd67f2018-02-28 14:33:38 -08001/*
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 Rocarddf9b4202018-05-10 19:56:08 -070017#ifndef ANDROID_HARDWARE_STREAM_HAL_HIDL_H
18#define ANDROID_HARDWARE_STREAM_HAL_HIDL_H
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080019
20#include <atomic>
21
Kevin Rocard95213bf2018-11-08 17:16:57 -080022#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 Rocard4bcd67f2018-02-28 14:33:38 -080025#include <fmq/EventFlag.h>
26#include <fmq/MessageQueue.h>
27#include <media/audiohal/StreamHalInterface.h>
Andy Hung638f45b2021-01-18 20:02:56 -080028#include <mediautils/Synchronization.h>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080029
30#include "ConversionHelperHidl.h"
31#include "StreamPowerLog.h"
32
Kevin Rocard070e7512018-05-22 09:29:13 -070033using ::android::hardware::audio::CPP_VERSION::IStream;
34using ::android::hardware::audio::CPP_VERSION::IStreamIn;
35using ::android::hardware::audio::CPP_VERSION::IStreamOut;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080036using ::android::hardware::EventFlag;
37using ::android::hardware::MessageQueue;
38using ::android::hardware::Return;
Kevin Rocard070e7512018-05-22 09:29:13 -070039using ReadParameters = ::android::hardware::audio::CPP_VERSION::IStreamIn::ReadParameters;
40using ReadStatus = ::android::hardware::audio::CPP_VERSION::IStreamIn::ReadStatus;
41using WriteCommand = ::android::hardware::audio::CPP_VERSION::IStreamOut::WriteCommand;
42using WriteStatus = ::android::hardware::audio::CPP_VERSION::IStreamOut::WriteStatus;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080043
44namespace android {
Kevin Rocard070e7512018-05-22 09:29:13 -070045namespace CPP_VERSION {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080046
47class DeviceHalHidl;
48
49class 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 Rocard4bcd67f2018-02-28 14:33:38 -0800104 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 Hung638f45b2021-01-18 20:02:56 -0800113 IStream * const mStream;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800114 int mHalThreadPriority;
115 size_t mCachedBufferSize;
116};
117
118class 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 Naganovac917ac2018-11-28 14:03:52 -0800129 // Selects the audio presentation (if available).
130 virtual status_t selectPresentation(int presentationId, int programId);
131
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800132 // 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 Rocarda8975a72018-03-27 10:16:52 -0700166 // Called when the metadata of the stream's source has been changed.
167 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
168
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800169 // Methods used by StreamOutCallback (HIDL).
170 void onWriteReady();
171 void onDrainReady();
172 void onError();
173
jiabinf6eb4c32020-02-25 14:06:25 -0800174 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 Rocard4bcd67f2018-02-28 14:33:38 -0800179 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 Hung638f45b2021-01-18 20:02:56 -0800185 // 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 Rocard4bcd67f2018-02-28 14:33:38 -0800195 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
213class 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
jiabin9ff780e2018-03-19 18:19:52 -0700231 // Get active microphones
232 virtual status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones);
233
Paul McLean03a6e6a2018-12-04 10:54:13 -0700234 // Set microphone direction (for processing)
Paul McLean12340082019-03-19 09:35:05 -0600235 virtual status_t setPreferredMicrophoneDirection(
236 audio_microphone_direction_t direction) override;
Paul McLean03a6e6a2018-12-04 10:54:13 -0700237
238 // Set microphone zoom (for processing)
Paul McLean12340082019-03-19 09:35:05 -0600239 virtual status_t setPreferredMicrophoneFieldDimension(float zoom) override;
Paul McLean03a6e6a2018-12-04 10:54:13 -0700240
Kevin Rocarda8975a72018-03-27 10:16:52 -0700241 // Called when the metadata of the stream's sink has been changed.
242 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
243
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800244 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 Hung638f45b2021-01-18 20:02:56 -0800250 // 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 Rocard4bcd67f2018-02-28 14:33:38 -0800258 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 Rocard070e7512018-05-22 09:29:13 -0700275} // namespace CPP_VERSION
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800276} // namespace android
277
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700278#endif // ANDROID_HARDWARE_STREAM_HAL_HIDL_H