blob: c6db6d6cd8ab88d0df9aa654c1ff9087ecfec713 [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
Andy Hungacb5b982021-01-20 10:12:00 -0800104 ~StreamHalHidl() override;
105
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800106 status_t getCachedBufferSize(size_t *size);
107
Mikhail Naganov247b5f92021-01-15 19:16:12 +0000108 status_t getHalPid(pid_t *pid);
109
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800110 bool requestHalThreadPriority(pid_t threadPid, pid_t threadId);
111
112 // mStreamPowerLog is used for audio signal power logging.
113 StreamPowerLog mStreamPowerLog;
114
115 private:
116 const int HAL_THREAD_PRIORITY_DEFAULT = -1;
Andy Hung638f45b2021-01-18 20:02:56 -0800117 IStream * const mStream;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800118 int mHalThreadPriority;
119 size_t mCachedBufferSize;
120};
121
122class StreamOutHalHidl : public StreamOutHalInterface, public StreamHalHidl {
123 public:
124 // Return the frame size (number of bytes per sample) of a stream.
125 virtual status_t getFrameSize(size_t *size);
126
127 // Return the audio hardware driver estimated latency in milliseconds.
128 virtual status_t getLatency(uint32_t *latency);
129
130 // Use this method in situations where audio mixing is done in the hardware.
131 virtual status_t setVolume(float left, float right);
132
Mikhail Naganovac917ac2018-11-28 14:03:52 -0800133 // Selects the audio presentation (if available).
134 virtual status_t selectPresentation(int presentationId, int programId);
135
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800136 // Write audio buffer to driver.
137 virtual status_t write(const void *buffer, size_t bytes, size_t *written);
138
139 // Return the number of audio frames written by the audio dsp to DAC since
140 // the output has exited standby.
141 virtual status_t getRenderPosition(uint32_t *dspFrames);
142
143 // Get the local time at which the next write to the audio driver will be presented.
144 virtual status_t getNextWriteTimestamp(int64_t *timestamp);
145
146 // Set the callback for notifying completion of non-blocking write and drain.
147 virtual status_t setCallback(wp<StreamOutHalInterfaceCallback> callback);
148
149 // Returns whether pause and resume operations are supported.
150 virtual status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume);
151
152 // Notifies to the audio driver to resume playback following a pause.
153 virtual status_t pause();
154
155 // Notifies to the audio driver to resume playback following a pause.
156 virtual status_t resume();
157
158 // Returns whether drain operation is supported.
159 virtual status_t supportsDrain(bool *supportsDrain);
160
161 // Requests notification when data buffered by the driver/hardware has been played.
162 virtual status_t drain(bool earlyNotify);
163
164 // Notifies to the audio driver to flush the queued data.
165 virtual status_t flush();
166
167 // Return a recent count of the number of audio frames presented to an external observer.
168 virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp);
169
Kevin Rocarda8975a72018-03-27 10:16:52 -0700170 // Called when the metadata of the stream's source has been changed.
171 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
172
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800173 // Methods used by StreamOutCallback (HIDL).
174 void onWriteReady();
175 void onDrainReady();
176 void onError();
177
Kuowei Lid4adbdb2020-08-13 14:44:25 +0800178 // Returns the Dual Mono mode presentation setting.
179 status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override;
180
181 // Sets the Dual Mono mode presentation on the output device.
182 status_t setDualMonoMode(audio_dual_mono_mode_t mode) override;
183
184 // Returns the Audio Description Mix level in dB.
185 status_t getAudioDescriptionMixLevel(float* leveldB) override;
186
187 // Sets the Audio Description Mix level in dB.
188 status_t setAudioDescriptionMixLevel(float leveldB) override;
189
190 // Retrieves current playback rate parameters.
191 status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override;
192
193 // Sets the playback rate parameters that control playback behavior.
194 status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override;
195
jiabinf6eb4c32020-02-25 14:06:25 -0800196 status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override;
197
198 // Methods used by StreamCodecFormatCallback (HIDL).
199 void onCodecFormatChanged(const std::basic_string<uint8_t>& metadataBs);
200
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800201 private:
202 friend class DeviceHalHidl;
203 typedef MessageQueue<WriteCommand, hardware::kSynchronizedReadWrite> CommandMQ;
204 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
205 typedef MessageQueue<WriteStatus, hardware::kSynchronizedReadWrite> StatusMQ;
206
Andy Hung638f45b2021-01-18 20:02:56 -0800207 mediautils::atomic_wp<StreamOutHalInterfaceCallback> mCallback;
208 mediautils::atomic_wp<StreamOutHalInterfaceEventCallback> mEventCallback;
209 const sp<IStreamOut> mStream;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800210 std::unique_ptr<CommandMQ> mCommandMQ;
211 std::unique_ptr<DataMQ> mDataMQ;
212 std::unique_ptr<StatusMQ> mStatusMQ;
213 std::atomic<pid_t> mWriterClient;
214 EventFlag* mEfGroup;
215
216 // Can not be constructed directly by clients.
217 StreamOutHalHidl(const sp<IStreamOut>& stream);
218
219 virtual ~StreamOutHalHidl();
220
221 using WriterCallback = std::function<void(const WriteStatus& writeStatus)>;
222 status_t callWriterThread(
223 WriteCommand cmd, const char* cmdName,
224 const uint8_t* data, size_t dataSize, WriterCallback callback);
225 status_t prepareForWriting(size_t bufferSize);
226};
227
228class StreamInHalHidl : public StreamInHalInterface, public StreamHalHidl {
229 public:
230 // Return the frame size (number of bytes per sample) of a stream.
231 virtual status_t getFrameSize(size_t *size);
232
233 // Set the input gain for the audio driver.
234 virtual status_t setGain(float gain);
235
236 // Read audio buffer in from driver.
237 virtual status_t read(void *buffer, size_t bytes, size_t *read);
238
239 // Return the amount of input frames lost in the audio driver.
240 virtual status_t getInputFramesLost(uint32_t *framesLost);
241
242 // Return a recent count of the number of audio frames received and
243 // the clock time associated with that frame count.
244 virtual status_t getCapturePosition(int64_t *frames, int64_t *time);
245
jiabin9ff780e2018-03-19 18:19:52 -0700246 // Get active microphones
247 virtual status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones);
248
Paul McLean03a6e6a2018-12-04 10:54:13 -0700249 // Set microphone direction (for processing)
Paul McLean12340082019-03-19 09:35:05 -0600250 virtual status_t setPreferredMicrophoneDirection(
251 audio_microphone_direction_t direction) override;
Paul McLean03a6e6a2018-12-04 10:54:13 -0700252
253 // Set microphone zoom (for processing)
Paul McLean12340082019-03-19 09:35:05 -0600254 virtual status_t setPreferredMicrophoneFieldDimension(float zoom) override;
Paul McLean03a6e6a2018-12-04 10:54:13 -0700255
Kevin Rocarda8975a72018-03-27 10:16:52 -0700256 // Called when the metadata of the stream's sink has been changed.
257 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
258
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800259 private:
260 friend class DeviceHalHidl;
261 typedef MessageQueue<ReadParameters, hardware::kSynchronizedReadWrite> CommandMQ;
262 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
263 typedef MessageQueue<ReadStatus, hardware::kSynchronizedReadWrite> StatusMQ;
264
Andy Hung638f45b2021-01-18 20:02:56 -0800265 const sp<IStreamIn> mStream;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800266 std::unique_ptr<CommandMQ> mCommandMQ;
267 std::unique_ptr<DataMQ> mDataMQ;
268 std::unique_ptr<StatusMQ> mStatusMQ;
269 std::atomic<pid_t> mReaderClient;
270 EventFlag* mEfGroup;
271
272 // Can not be constructed directly by clients.
273 StreamInHalHidl(const sp<IStreamIn>& stream);
274
275 virtual ~StreamInHalHidl();
276
277 using ReaderCallback = std::function<void(const ReadStatus& readStatus)>;
278 status_t callReaderThread(
279 const ReadParameters& params, const char* cmdName, ReaderCallback callback);
280 status_t prepareForReading(size_t bufferSize);
281};
282
Kevin Rocard070e7512018-05-22 09:29:13 -0700283} // namespace CPP_VERSION
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800284} // namespace android
285
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700286#endif // ANDROID_HARDWARE_STREAM_HAL_HIDL_H