blob: a1cdac4ffc5dd34ed7c767f5bea82f8473bca2ec [file] [log] [blame]
Mikhail Naganov31d46652023-01-10 18:29:25 +00001/*
2 * Copyright (C) 2023 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#pragma once
18
Mikhail Naganov89a9f742023-01-30 12:33:18 -080019#include <atomic>
Mikhail Naganov31d46652023-01-10 18:29:25 +000020#include <memory>
Mikhail Naganov89a9f742023-01-30 12:33:18 -080021#include <mutex>
Mikhail Naganov31d46652023-01-10 18:29:25 +000022#include <string_view>
23
David Lia0ad9f02023-03-02 21:46:19 +080024#include <aidl/android/hardware/audio/common/AudioOffloadMetadata.h>
Mikhail Naganov31d46652023-01-10 18:29:25 +000025#include <aidl/android/hardware/audio/core/BpStreamCommon.h>
26#include <aidl/android/hardware/audio/core/BpStreamIn.h>
27#include <aidl/android/hardware/audio/core/BpStreamOut.h>
David Li9cf5e622023-03-21 00:51:10 +080028#include <aidl/android/hardware/audio/core/MmapBufferDescriptor.h>
Mikhail Naganove7a26ad2023-05-25 17:36:48 -070029#include <aidl/android/media/audio/IHalAdapterVendorExtension.h>
Mikhail Naganov31d46652023-01-10 18:29:25 +000030#include <fmq/AidlMessageQueue.h>
31#include <media/audiohal/EffectHalInterface.h>
32#include <media/audiohal/StreamHalInterface.h>
Mikhail Naganove7a26ad2023-05-25 17:36:48 -070033#include <media/AidlConversionUtil.h>
David Lia0ad9f02023-03-02 21:46:19 +080034#include <media/AudioParameter.h>
Mikhail Naganov23224a12024-03-28 02:15:21 +000035#include <mediautils/Synchronization.h>
Mikhail Naganov31d46652023-01-10 18:29:25 +000036
37#include "ConversionHelperAidl.h"
38#include "StreamPowerLog.h"
39
David Lia0ad9f02023-03-02 21:46:19 +080040using ::aidl::android::hardware::audio::common::AudioOffloadMetadata;
David Li9cf5e622023-03-21 00:51:10 +080041using ::aidl::android::hardware::audio::core::MmapBufferDescriptor;
David Lia0ad9f02023-03-02 21:46:19 +080042
Mikhail Naganov31d46652023-01-10 18:29:25 +000043namespace android {
44
Mikhail Naganov89a9f742023-01-30 12:33:18 -080045class StreamContextAidl {
46 public:
47 typedef AidlMessageQueue<::aidl::android::hardware::audio::core::StreamDescriptor::Command,
48 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> CommandMQ;
49 typedef AidlMessageQueue<::aidl::android::hardware::audio::core::StreamDescriptor::Reply,
50 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> ReplyMQ;
51 typedef AidlMessageQueue<int8_t,
52 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> DataMQ;
53
Mikhail Naganove7a26ad2023-05-25 17:36:48 -070054 StreamContextAidl(
David Li9cf5e622023-03-21 00:51:10 +080055 ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor,
Jaideep Sharma145313e2024-08-14 14:51:24 +053056 bool isAsynchronous, int ioHandle)
Mikhail Naganov89a9f742023-01-30 12:33:18 -080057 : mFrameSizeBytes(descriptor.frameSizeBytes),
58 mCommandMQ(new CommandMQ(descriptor.command)),
59 mReplyMQ(new ReplyMQ(descriptor.reply)),
60 mBufferSizeFrames(descriptor.bufferSizeFrames),
Mikhail Naganov712d71b2023-02-23 17:57:16 -080061 mDataMQ(maybeCreateDataMQ(descriptor)),
David Li9cf5e622023-03-21 00:51:10 +080062 mIsAsynchronous(isAsynchronous),
63 mIsMmapped(isMmapped(descriptor)),
Jaideep Sharma145313e2024-08-14 14:51:24 +053064 mMmapBufferDescriptor(maybeGetMmapBuffer(descriptor)),
65 mIoHandle(ioHandle) {}
Mikhail Naganov89a9f742023-01-30 12:33:18 -080066 StreamContextAidl(StreamContextAidl&& other) :
67 mFrameSizeBytes(other.mFrameSizeBytes),
68 mCommandMQ(std::move(other.mCommandMQ)),
69 mReplyMQ(std::move(other.mReplyMQ)),
70 mBufferSizeFrames(other.mBufferSizeFrames),
Mikhail Naganov712d71b2023-02-23 17:57:16 -080071 mDataMQ(std::move(other.mDataMQ)),
David Li9cf5e622023-03-21 00:51:10 +080072 mIsAsynchronous(other.mIsAsynchronous),
73 mIsMmapped(other.mIsMmapped),
Jaideep Sharma145313e2024-08-14 14:51:24 +053074 mMmapBufferDescriptor(std::move(other.mMmapBufferDescriptor)),
75 mIoHandle(other.mIoHandle) {}
Mikhail Naganov89a9f742023-01-30 12:33:18 -080076 StreamContextAidl& operator=(StreamContextAidl&& other) {
77 mFrameSizeBytes = other.mFrameSizeBytes;
78 mCommandMQ = std::move(other.mCommandMQ);
79 mReplyMQ = std::move(other.mReplyMQ);
80 mBufferSizeFrames = other.mBufferSizeFrames;
81 mDataMQ = std::move(other.mDataMQ);
Mikhail Naganov712d71b2023-02-23 17:57:16 -080082 mIsAsynchronous = other.mIsAsynchronous;
David Li9cf5e622023-03-21 00:51:10 +080083 mIsMmapped = other.mIsMmapped;
84 mMmapBufferDescriptor = std::move(other.mMmapBufferDescriptor);
Jaideep Sharma145313e2024-08-14 14:51:24 +053085 mIoHandle = other.mIoHandle;
Mikhail Naganov89a9f742023-01-30 12:33:18 -080086 return *this;
87 }
88 bool isValid() const {
89 return mFrameSizeBytes != 0 &&
90 mCommandMQ != nullptr && mCommandMQ->isValid() &&
91 mReplyMQ != nullptr && mReplyMQ->isValid() &&
David Li9cf5e622023-03-21 00:51:10 +080092 (mDataMQ == nullptr || (
Mikhail Naganov89a9f742023-01-30 12:33:18 -080093 mDataMQ->isValid() &&
94 mDataMQ->getQuantumCount() * mDataMQ->getQuantumSize() >=
David Li9cf5e622023-03-21 00:51:10 +080095 mFrameSizeBytes * mBufferSizeFrames)) &&
96 (!mIsMmapped || mMmapBufferDescriptor.sharedMemory.fd.get() >= 0);
Mikhail Naganov89a9f742023-01-30 12:33:18 -080097 }
98 size_t getBufferSizeBytes() const { return mFrameSizeBytes * mBufferSizeFrames; }
99 size_t getBufferSizeFrames() const { return mBufferSizeFrames; }
Mikhail Naganov23224a12024-03-28 02:15:21 +0000100 size_t getBufferDurationMs(int32_t sampleRate) const {
Jaideep Sharma0cb13872024-05-24 11:44:56 +0530101 auto bufferSize = mIsMmapped ? getMmapBurstSize() : mBufferSizeFrames;
102 return sampleRate != 0 ? bufferSize * MILLIS_PER_SECOND / sampleRate : 0;
Mikhail Naganov23224a12024-03-28 02:15:21 +0000103 }
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800104 CommandMQ* getCommandMQ() const { return mCommandMQ.get(); }
105 DataMQ* getDataMQ() const { return mDataMQ.get(); }
106 size_t getFrameSizeBytes() const { return mFrameSizeBytes; }
107 ReplyMQ* getReplyMQ() const { return mReplyMQ.get(); }
Mikhail Naganov712d71b2023-02-23 17:57:16 -0800108 bool isAsynchronous() const { return mIsAsynchronous; }
David Li9cf5e622023-03-21 00:51:10 +0800109 bool isMmapped() const { return mIsMmapped; }
110 const MmapBufferDescriptor& getMmapBufferDescriptor() const { return mMmapBufferDescriptor; }
Jaideep Sharma145313e2024-08-14 14:51:24 +0530111 size_t getMmapBurstSize() const { return mMmapBufferDescriptor.burstSizeFrames; }
112 int getIoHandle() const { return mIoHandle; }
113
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800114 private:
115 static std::unique_ptr<DataMQ> maybeCreateDataMQ(
116 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor) {
117 using Tag = ::aidl::android::hardware::audio::core::StreamDescriptor::AudioBuffer::Tag;
118 if (descriptor.audio.getTag() == Tag::fmq) {
119 return std::make_unique<DataMQ>(descriptor.audio.get<Tag::fmq>());
120 }
121 return nullptr;
122 }
David Li9cf5e622023-03-21 00:51:10 +0800123 static bool isMmapped(
124 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor) {
125 using Tag = ::aidl::android::hardware::audio::core::StreamDescriptor::AudioBuffer::Tag;
126 return descriptor.audio.getTag() == Tag::mmap;
127 }
128 static MmapBufferDescriptor maybeGetMmapBuffer(
129 ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor) {
130 using Tag = ::aidl::android::hardware::audio::core::StreamDescriptor::AudioBuffer::Tag;
131 if (descriptor.audio.getTag() == Tag::mmap) {
132 return std::move(descriptor.audio.get<Tag::mmap>());
133 }
134 return {};
135 }
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800136
137 size_t mFrameSizeBytes;
138 std::unique_ptr<CommandMQ> mCommandMQ;
139 std::unique_ptr<ReplyMQ> mReplyMQ;
140 size_t mBufferSizeFrames;
141 std::unique_ptr<DataMQ> mDataMQ;
Mikhail Naganov712d71b2023-02-23 17:57:16 -0800142 bool mIsAsynchronous;
David Li9cf5e622023-03-21 00:51:10 +0800143 bool mIsMmapped;
144 MmapBufferDescriptor mMmapBufferDescriptor;
Jaideep Sharma145313e2024-08-14 14:51:24 +0530145 int mIoHandle;
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800146};
Mikhail Naganov31d46652023-01-10 18:29:25 +0000147
148class StreamHalAidl : public virtual StreamHalInterface, public ConversionHelperAidl {
149 public:
150 // Return size of input/output buffer in bytes for this stream - eg. 4800.
151 status_t getBufferSize(size_t *size) override;
152
153 // Return the base configuration of the stream:
154 // - channel mask;
155 // - format - e.g. AUDIO_FORMAT_PCM_16_BIT;
156 // - sampling rate in Hz - eg. 44100.
157 status_t getAudioProperties(audio_config_base_t *configBase) override;
158
159 // Set audio stream parameters.
160 status_t setParameters(const String8& kvPairs) override;
161
162 // Get audio stream parameters.
163 status_t getParameters(const String8& keys, String8 *values) override;
164
165 // Return the frame size (number of bytes per sample) of a stream.
166 status_t getFrameSize(size_t *size) override;
167
168 // Add or remove the effect on the stream.
169 status_t addEffect(sp<EffectHalInterface> effect) override;
170 status_t removeEffect(sp<EffectHalInterface> effect) override;
171
172 // Put the audio hardware input/output into standby mode.
173 status_t standby() override;
174
175 status_t dump(int fd, const Vector<String16>& args) override;
176
177 // Start a stream operating in mmap mode.
178 status_t start() override;
179
180 // Stop a stream operating in mmap mode.
181 status_t stop() override;
182
183 // Retrieve information on the data buffer in mmap mode.
184 status_t createMmapBuffer(int32_t minSizeFrames,
185 struct audio_mmap_buffer_info *info) override;
186
187 // Get current read/write position in the mmap buffer
188 status_t getMmapPosition(struct audio_mmap_position *position) override;
189
190 // Set the priority of the thread that interacts with the HAL
191 // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
192 status_t setHalThreadPriority(int priority) override;
193
194 status_t legacyCreateAudioPatch(const struct audio_port_config& port,
195 std::optional<audio_source_t> source,
196 audio_devices_t type) override;
197
198 status_t legacyReleaseAudioPatch() override;
199
200 protected:
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700201 // For tests.
202 friend class sp<StreamHalAidl>;
203
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700204 struct StatePositions {
205 int64_t framesAtFlushOrDrain;
206 int64_t framesAtStandby;
207 };
208
Mikhail Naganovfab697c2023-01-11 19:33:13 +0000209 template<class T>
210 static std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> getStreamCommon(
211 const std::shared_ptr<T>& stream);
212
Mikhail Naganov31d46652023-01-10 18:29:25 +0000213 // Subclasses can not be constructed directly by clients.
214 StreamHalAidl(std::string_view className,
215 bool isInput,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800216 const audio_config& config,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800217 int32_t nominalLatency,
218 StreamContextAidl&& context,
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700219 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon>& stream,
220 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>& vext);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000221
222 ~StreamHalAidl() override;
223
Ajender Reddy5f95b4a2024-07-16 17:15:16 +0530224 ::aidl::android::hardware::audio::core::StreamDescriptor::State getState() {
225 std::lock_guard l(mLock);
226 return mLastReply.state;
227 }
228
Ajender Reddy3786f3d2024-07-31 20:08:14 +0530229 bool isInDrainedState(
230 const ::aidl::android::hardware::audio::core::StreamDescriptor::State state) {
231 if (state == ::aidl::android::hardware::audio::core::StreamDescriptor::State::IDLE ||
232 state == ::aidl::android::hardware::audio::core::StreamDescriptor::State::STANDBY) {
233 // drain equivalent states
234 return true;
235 }
236 return false;
237 }
238
Ajender Reddyd8075c82024-07-29 17:45:59 +0530239 bool isInPlayOrRecordState(
240 const ::aidl::android::hardware::audio::core::StreamDescriptor::State state) {
241 if (state == ::aidl::android::hardware::audio::core::StreamDescriptor::State::ACTIVE ||
242 state ==
243 ::aidl::android::hardware::audio::core::StreamDescriptor::State::TRANSFERRING ||
244 state == ::aidl::android::hardware::audio::core::StreamDescriptor::State::DRAINING) {
245 // play or record equivalent states
246 return true;
247 }
248 return false;
249 }
250
251 bool isInPausedState(
252 const ::aidl::android::hardware::audio::core::StreamDescriptor::State& state) {
253 if (state == ::aidl::android::hardware::audio::core::StreamDescriptor::State::PAUSED ||
254 state ==
255 ::aidl::android::hardware::audio::core::StreamDescriptor::State::DRAIN_PAUSED ||
256 state == ::aidl::android::hardware::audio::core::StreamDescriptor::State::
257 TRANSFER_PAUSED) {
258 // pause equivalent states
259 return true;
260 }
261 return false;
262 }
263
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800264 status_t getLatency(uint32_t *latency);
265
Mikhail Naganove2084702023-09-28 14:37:12 -0700266 // Always returns non-negative values.
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700267 status_t getObservablePosition(int64_t* frames, int64_t* timestamp,
268 StatePositions* statePositions = nullptr);
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800269
Mikhail Naganove2084702023-09-28 14:37:12 -0700270 // Always returns non-negative values.
David Li9cf5e622023-03-21 00:51:10 +0800271 status_t getHardwarePosition(int64_t *frames, int64_t *timestamp);
272
Mikhail Naganove2084702023-09-28 14:37:12 -0700273 // Always returns non-negative values.
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800274 status_t getXruns(int32_t *frames);
275
276 status_t transfer(void *buffer, size_t bytes, size_t *transferred);
277
278 status_t pause(
279 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
280
281 status_t resume(
282 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
283
284 status_t drain(bool earlyNotify,
285 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
286
287 status_t flush(
288 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
289
290 status_t exit();
291
Mikhail Naganov23224a12024-03-28 02:15:21 +0000292 void onAsyncTransferReady();
293 void onAsyncDrainReady();
294 void onAsyncError();
295
Mikhail Naganov31d46652023-01-10 18:29:25 +0000296 const bool mIsInput;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800297 const audio_config_base_t mConfig;
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800298 const StreamContextAidl mContext;
Mikhail Naganov503b1d82024-04-15 17:58:08 +0000299 // This lock is used to make sending of a command and receiving a reply an atomic
300 // operation. Otherwise, when two threads are trying to send a command, they may both advance to
301 // reading of the reply once the HAL has consumed the command from the MQ, and that creates a
302 // race condition between them.
303 //
304 // Note that only access to command and reply MQs needs to be protected because the data MQ is
305 // only accessed by the I/O thread. Also, there is no need to protect lookup operations on the
306 // queues as they are thread-safe, only send/receive operation must be protected.
307 std::mutex mCommandReplyLock;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000308
309 private:
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800310 static audio_config_base_t configToBase(const audio_config& config) {
311 audio_config_base_t result = AUDIO_CONFIG_BASE_INITIALIZER;
312 result.sample_rate = config.sample_rate;
313 result.channel_mask = config.channel_mask;
314 result.format = config.format;
315 return result;
316 }
Mikhail Naganov503b1d82024-04-15 17:58:08 +0000317 // Note: Since `sendCommand` takes mLock while holding mCommandReplyLock, never call
318 // it with `mLock` being held.
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800319 status_t sendCommand(
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700320 const ::aidl::android::hardware::audio::core::StreamDescriptor::Command& command,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800321 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr,
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700322 bool safeFromNonWorkerThread = false,
323 StatePositions* statePositions = nullptr);
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800324 status_t updateCountersIfNeeded(
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700325 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr,
326 StatePositions* statePositions = nullptr);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000327
Mikhail Naganov31d46652023-01-10 18:29:25 +0000328 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> mStream;
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700329 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> mVendorExt;
Mikhail Naganov23224a12024-03-28 02:15:21 +0000330 const int64_t mLastReplyLifeTimeNs;
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800331 std::mutex mLock;
332 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply mLastReply GUARDED_BY(mLock);
Mikhail Naganov23224a12024-03-28 02:15:21 +0000333 int64_t mLastReplyExpirationNs GUARDED_BY(mLock) = 0;
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700334 // Cached values of observable positions when the stream last entered certain state.
335 // Updated for output streams only.
336 StatePositions mStatePositions GUARDED_BY(mLock) = {};
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800337 // mStreamPowerLog is used for audio signal power logging.
338 StreamPowerLog mStreamPowerLog;
339 std::atomic<pid_t> mWorkerTid = -1;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000340};
341
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800342class CallbackBroker;
343
Mikhail Naganov23224a12024-03-28 02:15:21 +0000344class StreamOutHalAidl : public virtual StreamOutHalInterface,
345 public virtual StreamOutHalInterfaceCallback,
346 public StreamHalAidl {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000347 public:
David Lia0ad9f02023-03-02 21:46:19 +0800348 // Extract the output stream parameters and set by AIDL APIs.
349 status_t setParameters(const String8& kvPairs) override;
350
Mikhail Naganov31d46652023-01-10 18:29:25 +0000351 // Return the audio hardware driver estimated latency in milliseconds.
352 status_t getLatency(uint32_t *latency) override;
353
354 // Use this method in situations where audio mixing is done in the hardware.
355 status_t setVolume(float left, float right) override;
356
357 // Selects the audio presentation (if available).
358 status_t selectPresentation(int presentationId, int programId) override;
359
360 // Write audio buffer to driver.
361 status_t write(const void *buffer, size_t bytes, size_t *written) override;
362
363 // Return the number of audio frames written by the audio dsp to DAC since
364 // the output has exited standby.
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -0700365 status_t getRenderPosition(uint64_t *dspFrames) override;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000366
367 // Set the callback for notifying completion of non-blocking write and drain.
368 status_t setCallback(wp<StreamOutHalInterfaceCallback> callback) override;
369
370 // Returns whether pause and resume operations are supported.
371 status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume) override;
372
373 // Notifies to the audio driver to resume playback following a pause.
374 status_t pause() override;
375
376 // Notifies to the audio driver to resume playback following a pause.
377 status_t resume() override;
378
379 // Returns whether drain operation is supported.
380 status_t supportsDrain(bool *supportsDrain) override;
381
382 // Requests notification when data buffered by the driver/hardware has been played.
383 status_t drain(bool earlyNotify) override;
384
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700385 // Notifies to the audio driver to flush (that is, drop) the queued data. Stream must
386 // already be paused before calling 'flush'.
Mikhail Naganov31d46652023-01-10 18:29:25 +0000387 status_t flush() override;
388
389 // Return a recent count of the number of audio frames presented to an external observer.
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700390 // This excludes frames which have been written but are still in the pipeline. See the
391 // table at the start of the 'StreamOutHalInterface' for the specification of the frame
392 // count behavior w.r.t. 'flush', 'drain' and 'standby' operations.
Mikhail Naganov31d46652023-01-10 18:29:25 +0000393 status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp) override;
394
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -0700395 // Notifies the HAL layer that the framework considers the current playback as completed.
396 status_t presentationComplete() override;
397
Mikhail Naganov31d46652023-01-10 18:29:25 +0000398 // Called when the metadata of the stream's source has been changed.
399 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
400
401 // Returns the Dual Mono mode presentation setting.
402 status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override;
403
404 // Sets the Dual Mono mode presentation on the output device.
405 status_t setDualMonoMode(audio_dual_mono_mode_t mode) override;
406
407 // Returns the Audio Description Mix level in dB.
408 status_t getAudioDescriptionMixLevel(float* leveldB) override;
409
410 // Sets the Audio Description Mix level in dB.
411 status_t setAudioDescriptionMixLevel(float leveldB) override;
412
413 // Retrieves current playback rate parameters.
414 status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override;
415
416 // Sets the playback rate parameters that control playback behavior.
417 status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override;
418
419 status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override;
420
421 status_t setLatencyMode(audio_latency_mode_t mode) override;
422 status_t getRecommendedLatencyModes(std::vector<audio_latency_mode_t> *modes) override;
423 status_t setLatencyModeCallback(
424 const sp<StreamOutHalInterfaceLatencyModeCallback>& callback) override;
425
Mikhail Naganov31d46652023-01-10 18:29:25 +0000426 status_t exit() override;
427
Mikhail Naganov23224a12024-03-28 02:15:21 +0000428 // StreamOutHalInterfaceCallback
429 void onWriteReady() override;
430 void onDrainReady() override;
Mikhail Naganovf548cd32024-05-29 17:06:46 +0000431 void onError(bool isHardError) override;
Mikhail Naganov23224a12024-03-28 02:15:21 +0000432
Mikhail Naganov31d46652023-01-10 18:29:25 +0000433 private:
434 friend class sp<StreamOutHalAidl>;
435
Mikhail Naganov19418e32023-03-10 17:55:14 -0800436 static ConversionResult<::aidl::android::hardware::audio::common::SourceMetadata>
Mikhail Naganovb6e57752023-03-08 18:12:47 -0800437 legacy2aidl_SourceMetadata(const StreamOutHalInterface::SourceMetadata& legacy);
438
Mikhail Naganov31d46652023-01-10 18:29:25 +0000439 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut> mStream;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800440 const wp<CallbackBroker> mCallbackBroker;
Mikhail Naganov23224a12024-03-28 02:15:21 +0000441 mediautils::atomic_wp<StreamOutHalInterfaceCallback> mClientCallback;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000442
David Lia0ad9f02023-03-02 21:46:19 +0800443 AudioOffloadMetadata mOffloadMetadata;
444
Mikhail Naganov31d46652023-01-10 18:29:25 +0000445 // Can not be constructed directly by clients.
446 StreamOutHalAidl(
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800447 const audio_config& config, StreamContextAidl&& context, int32_t nominalLatency,
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800448 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut>& stream,
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700449 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>& vext,
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800450 const sp<CallbackBroker>& callbackBroker);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000451
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800452 ~StreamOutHalAidl() override;
David Lia0ad9f02023-03-02 21:46:19 +0800453
454 // Filter and update the offload metadata. The parameters which are related to the offload
455 // metadata will be removed after filtering.
456 status_t filterAndUpdateOffloadMetadata(AudioParameter &parameters);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000457};
458
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800459class MicrophoneInfoProvider;
460
Mikhail Naganov31d46652023-01-10 18:29:25 +0000461class StreamInHalAidl : public StreamInHalInterface, public StreamHalAidl {
462 public:
463 // Set the input gain for the audio driver.
464 status_t setGain(float gain) override;
465
466 // Read audio buffer in from driver.
467 status_t read(void *buffer, size_t bytes, size_t *read) override;
468
469 // Return the amount of input frames lost in the audio driver.
470 status_t getInputFramesLost(uint32_t *framesLost) override;
471
472 // Return a recent count of the number of audio frames received and
473 // the clock time associated with that frame count.
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700474 // The count must not reset to zero when a PCM input enters standby.
Mikhail Naganov31d46652023-01-10 18:29:25 +0000475 status_t getCapturePosition(int64_t *frames, int64_t *time) override;
476
477 // Get active microphones
Mikhail Naganov2a6a3012023-02-13 11:45:03 -0800478 status_t getActiveMicrophones(std::vector<media::MicrophoneInfoFw> *microphones) override;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000479
480 // Set microphone direction (for processing)
481 status_t setPreferredMicrophoneDirection(
482 audio_microphone_direction_t direction) override;
483
484 // Set microphone zoom (for processing)
485 status_t setPreferredMicrophoneFieldDimension(float zoom) override;
486
487 // Called when the metadata of the stream's sink has been changed.
488 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
489
490 private:
491 friend class sp<StreamInHalAidl>;
492
Mikhail Naganov19418e32023-03-10 17:55:14 -0800493 static ConversionResult<::aidl::android::hardware::audio::common::SinkMetadata>
Mikhail Naganovb6e57752023-03-08 18:12:47 -0800494 legacy2aidl_SinkMetadata(const StreamInHalInterface::SinkMetadata& legacy);
495
Mikhail Naganov31d46652023-01-10 18:29:25 +0000496 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn> mStream;
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800497 const wp<MicrophoneInfoProvider> mMicInfoProvider;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000498
499 // Can not be constructed directly by clients.
500 StreamInHalAidl(
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800501 const audio_config& config, StreamContextAidl&& context, int32_t nominalLatency,
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800502 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn>& stream,
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700503 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>& vext,
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800504 const sp<MicrophoneInfoProvider>& micInfoProvider);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000505
506 ~StreamInHalAidl() override = default;
507};
508
509} // namespace android