blob: 80379d059da126869a81e3b15f765217ea797627 [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>
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -070021#include <mutex>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080022
Mikhail Naganov6718c392022-01-27 22:17:21 +000023#include PATH(android/hardware/audio/CORE_TYPES_FILE_VERSION/IStream.h)
Mikhail Naganovaccbe8a2022-02-03 23:45:36 +000024#include PATH(android/hardware/audio/CORE_TYPES_FILE_VERSION/IStreamIn.h)
Kevin Rocard95213bf2018-11-08 17:16:57 -080025#include PATH(android/hardware/audio/FILE_VERSION/IStreamOut.h)
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -070026#include <android-base/thread_annotations.h>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080027#include <fmq/EventFlag.h>
28#include <fmq/MessageQueue.h>
Mikhail Naganov6718c392022-01-27 22:17:21 +000029#include <media/audiohal/EffectHalInterface.h>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080030#include <media/audiohal/StreamHalInterface.h>
Andy Hung638f45b2021-01-18 20:02:56 -080031#include <mediautils/Synchronization.h>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080032
Mikhail Naganov288a3432022-03-25 00:29:56 +000033#include "CoreConversionHelperHidl.h"
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080034#include "StreamPowerLog.h"
35
Mikhail Naganov6718c392022-01-27 22:17:21 +000036using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStream;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080037using ::android::hardware::EventFlag;
38using ::android::hardware::MessageQueue;
39using ::android::hardware::Return;
Mikhail Naganovaccbe8a2022-02-03 23:45:36 +000040using ReadParameters =
41 ::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn::ReadParameters;
42using ReadStatus = ::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn::ReadStatus;
Kevin Rocard070e7512018-05-22 09:29:13 -070043using WriteCommand = ::android::hardware::audio::CPP_VERSION::IStreamOut::WriteCommand;
44using WriteStatus = ::android::hardware::audio::CPP_VERSION::IStreamOut::WriteStatus;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080045
46namespace android {
47
48class DeviceHalHidl;
49
Mikhail Naganov288a3432022-03-25 00:29:56 +000050class StreamHalHidl : public virtual StreamHalInterface, public CoreConversionHelperHidl
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080051{
52 public:
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080053 // Return size of input/output buffer in bytes for this stream - eg. 4800.
54 virtual status_t getBufferSize(size_t *size);
55
Mikhail Naganov560637e2021-03-31 22:40:13 +000056 // Return the base configuration of the stream:
57 // - channel mask;
58 // - format - e.g. AUDIO_FORMAT_PCM_16_BIT;
59 // - sampling rate in Hz - eg. 44100.
60 virtual status_t getAudioProperties(audio_config_base_t *configBase);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080061
62 // Set audio stream parameters.
63 virtual status_t setParameters(const String8& kvPairs);
64
65 // Get audio stream parameters.
66 virtual status_t getParameters(const String8& keys, String8 *values);
67
68 // Add or remove the effect on the stream.
69 virtual status_t addEffect(sp<EffectHalInterface> effect);
70 virtual status_t removeEffect(sp<EffectHalInterface> effect);
71
72 // Put the audio hardware input/output into standby mode.
73 virtual status_t standby();
74
Andy Hung61589a42021-06-16 09:37:53 -070075 virtual status_t dump(int fd, const Vector<String16>& args) override;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080076
77 // Start a stream operating in mmap mode.
78 virtual status_t start();
79
80 // Stop a stream operating in mmap mode.
81 virtual status_t stop();
82
83 // Retrieve information on the data buffer in mmap mode.
84 virtual status_t createMmapBuffer(int32_t minSizeFrames,
85 struct audio_mmap_buffer_info *info);
86
87 // Get current read/write position in the mmap buffer
88 virtual status_t getMmapPosition(struct audio_mmap_position *position);
89
90 // Set the priority of the thread that interacts with the HAL
91 // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
92 virtual status_t setHalThreadPriority(int priority);
93
Ytai Ben-Tsvif997ffe2022-02-03 16:38:16 -080094 status_t legacyCreateAudioPatch(const struct audio_port_config& port,
95 std::optional<audio_source_t> source,
96 audio_devices_t type) override;
97
98 status_t legacyReleaseAudioPatch() override;
99
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800100 protected:
101 // Subclasses can not be constructed directly by clients.
Andy Hung224f82f2022-03-22 00:00:49 -0700102 StreamHalHidl(std::string_view className, IStream *stream);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800103
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:
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -0700124 // Put the audio hardware input/output into standby mode (from StreamHalInterface).
125 status_t standby() override;
126
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800127 // Return the frame size (number of bytes per sample) of a stream.
128 virtual status_t getFrameSize(size_t *size);
129
130 // Return the audio hardware driver estimated latency in milliseconds.
131 virtual status_t getLatency(uint32_t *latency);
132
133 // Use this method in situations where audio mixing is done in the hardware.
134 virtual status_t setVolume(float left, float right);
135
Mikhail Naganovac917ac2018-11-28 14:03:52 -0800136 // Selects the audio presentation (if available).
137 virtual status_t selectPresentation(int presentationId, int programId);
138
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800139 // Write audio buffer to driver.
140 virtual status_t write(const void *buffer, size_t bytes, size_t *written);
141
142 // Return the number of audio frames written by the audio dsp to DAC since
143 // the output has exited standby.
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -0700144 virtual status_t getRenderPosition(uint64_t *dspFrames);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800145
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
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -0700170 // Notifies the HAL layer that the framework considers the current playback as completed.
171 status_t presentationComplete() override;
172
Kevin Rocarda8975a72018-03-27 10:16:52 -0700173 // Called when the metadata of the stream's source has been changed.
174 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
175
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800176 // Methods used by StreamOutCallback (HIDL).
177 void onWriteReady();
178 void onDrainReady();
179 void onError();
180
Kuowei Lid4adbdb2020-08-13 14:44:25 +0800181 // Returns the Dual Mono mode presentation setting.
182 status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override;
183
184 // Sets the Dual Mono mode presentation on the output device.
185 status_t setDualMonoMode(audio_dual_mono_mode_t mode) override;
186
187 // Returns the Audio Description Mix level in dB.
188 status_t getAudioDescriptionMixLevel(float* leveldB) override;
189
190 // Sets the Audio Description Mix level in dB.
191 status_t setAudioDescriptionMixLevel(float leveldB) override;
192
193 // Retrieves current playback rate parameters.
194 status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override;
195
196 // Sets the playback rate parameters that control playback behavior.
197 status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override;
198
jiabinf6eb4c32020-02-25 14:06:25 -0800199 status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override;
200
201 // Methods used by StreamCodecFormatCallback (HIDL).
Ryan Prichard78c5e452024-02-08 16:16:57 -0800202 void onCodecFormatChanged(const std::vector<uint8_t>& metadataBs);
jiabinf6eb4c32020-02-25 14:06:25 -0800203
Eric Laurentafa586b2022-01-27 21:10:55 +0100204 status_t setLatencyMode(audio_latency_mode_t mode) override;
205 status_t getRecommendedLatencyModes(std::vector<audio_latency_mode_t> *modes) override;
206 status_t setLatencyModeCallback(
207 const sp<StreamOutHalInterfaceLatencyModeCallback>& callback) override;
208
209 void onRecommendedLatencyModeChanged(const std::vector<audio_latency_mode_t>& modes);
210
Ytai Ben-Tsvi7e0183f2022-02-04 10:49:54 -0800211 status_t exit() override;
212
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800213 private:
214 friend class DeviceHalHidl;
215 typedef MessageQueue<WriteCommand, hardware::kSynchronizedReadWrite> CommandMQ;
216 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
217 typedef MessageQueue<WriteStatus, hardware::kSynchronizedReadWrite> StatusMQ;
218
Andy Hung638f45b2021-01-18 20:02:56 -0800219 mediautils::atomic_wp<StreamOutHalInterfaceCallback> mCallback;
220 mediautils::atomic_wp<StreamOutHalInterfaceEventCallback> mEventCallback;
Eric Laurentafa586b2022-01-27 21:10:55 +0100221 mediautils::atomic_wp<StreamOutHalInterfaceLatencyModeCallback> mLatencyModeCallback;
222
Mikhail Naganov6718c392022-01-27 22:17:21 +0000223 const sp<::android::hardware::audio::CPP_VERSION::IStreamOut> mStream;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800224 std::unique_ptr<CommandMQ> mCommandMQ;
225 std::unique_ptr<DataMQ> mDataMQ;
226 std::unique_ptr<StatusMQ> mStatusMQ;
227 std::atomic<pid_t> mWriterClient;
228 EventFlag* mEfGroup;
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -0700229 std::mutex mPositionMutex;
230 // Used to expand correctly the 32-bit position from the HAL.
231 uint64_t mRenderPosition GUARDED_BY(mPositionMutex) = 0;
232 bool mExpectRetrograde GUARDED_BY(mPositionMutex) = false; // See 'presentationComplete'.
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800233
234 // Can not be constructed directly by clients.
Mikhail Naganov6718c392022-01-27 22:17:21 +0000235 StreamOutHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IStreamOut>& stream);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800236
237 virtual ~StreamOutHalHidl();
238
239 using WriterCallback = std::function<void(const WriteStatus& writeStatus)>;
240 status_t callWriterThread(
241 WriteCommand cmd, const char* cmdName,
242 const uint8_t* data, size_t dataSize, WriterCallback callback);
243 status_t prepareForWriting(size_t bufferSize);
244};
245
246class StreamInHalHidl : public StreamInHalInterface, public StreamHalHidl {
247 public:
248 // Return the frame size (number of bytes per sample) of a stream.
249 virtual status_t getFrameSize(size_t *size);
250
251 // Set the input gain for the audio driver.
252 virtual status_t setGain(float gain);
253
254 // Read audio buffer in from driver.
255 virtual status_t read(void *buffer, size_t bytes, size_t *read);
256
257 // Return the amount of input frames lost in the audio driver.
258 virtual status_t getInputFramesLost(uint32_t *framesLost);
259
260 // Return a recent count of the number of audio frames received and
261 // the clock time associated with that frame count.
262 virtual status_t getCapturePosition(int64_t *frames, int64_t *time);
263
jiabin9ff780e2018-03-19 18:19:52 -0700264 // Get active microphones
Mikhail Naganov2a6a3012023-02-13 11:45:03 -0800265 status_t getActiveMicrophones(std::vector<media::MicrophoneInfoFw> *microphones) override;
jiabin9ff780e2018-03-19 18:19:52 -0700266
Paul McLean03a6e6a2018-12-04 10:54:13 -0700267 // Set microphone direction (for processing)
Paul McLean12340082019-03-19 09:35:05 -0600268 virtual status_t setPreferredMicrophoneDirection(
269 audio_microphone_direction_t direction) override;
Paul McLean03a6e6a2018-12-04 10:54:13 -0700270
271 // Set microphone zoom (for processing)
Paul McLean12340082019-03-19 09:35:05 -0600272 virtual status_t setPreferredMicrophoneFieldDimension(float zoom) override;
Paul McLean03a6e6a2018-12-04 10:54:13 -0700273
Kevin Rocarda8975a72018-03-27 10:16:52 -0700274 // Called when the metadata of the stream's sink has been changed.
275 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
276
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800277 private:
278 friend class DeviceHalHidl;
279 typedef MessageQueue<ReadParameters, hardware::kSynchronizedReadWrite> CommandMQ;
280 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
281 typedef MessageQueue<ReadStatus, hardware::kSynchronizedReadWrite> StatusMQ;
282
Mikhail Naganovaccbe8a2022-02-03 23:45:36 +0000283 const sp<::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn> mStream;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800284 std::unique_ptr<CommandMQ> mCommandMQ;
285 std::unique_ptr<DataMQ> mDataMQ;
286 std::unique_ptr<StatusMQ> mStatusMQ;
287 std::atomic<pid_t> mReaderClient;
288 EventFlag* mEfGroup;
289
290 // Can not be constructed directly by clients.
Mikhail Naganovaccbe8a2022-02-03 23:45:36 +0000291 StreamInHalHidl(
292 const sp<::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn>& stream);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800293
294 virtual ~StreamInHalHidl();
295
296 using ReaderCallback = std::function<void(const ReadStatus& readStatus)>;
297 status_t callReaderThread(
298 const ReadParameters& params, const char* cmdName, ReaderCallback callback);
299 status_t prepareForReading(size_t bufferSize);
300};
301
302} // namespace android
303
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700304#endif // ANDROID_HARDWARE_STREAM_HAL_HIDL_H