blob: 058764019d25c5bad5a524dd3c7210f12844c432 [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,
Mikhail Naganov712d71b2023-02-23 17:57:16 -080056 bool isAsynchronous)
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)),
64 mMmapBufferDescriptor(maybeGetMmapBuffer(descriptor)) {}
Mikhail Naganov89a9f742023-01-30 12:33:18 -080065 StreamContextAidl(StreamContextAidl&& other) :
66 mFrameSizeBytes(other.mFrameSizeBytes),
67 mCommandMQ(std::move(other.mCommandMQ)),
68 mReplyMQ(std::move(other.mReplyMQ)),
69 mBufferSizeFrames(other.mBufferSizeFrames),
Mikhail Naganov712d71b2023-02-23 17:57:16 -080070 mDataMQ(std::move(other.mDataMQ)),
David Li9cf5e622023-03-21 00:51:10 +080071 mIsAsynchronous(other.mIsAsynchronous),
72 mIsMmapped(other.mIsMmapped),
73 mMmapBufferDescriptor(std::move(other.mMmapBufferDescriptor)) {}
Mikhail Naganov89a9f742023-01-30 12:33:18 -080074 StreamContextAidl& operator=(StreamContextAidl&& other) {
75 mFrameSizeBytes = other.mFrameSizeBytes;
76 mCommandMQ = std::move(other.mCommandMQ);
77 mReplyMQ = std::move(other.mReplyMQ);
78 mBufferSizeFrames = other.mBufferSizeFrames;
79 mDataMQ = std::move(other.mDataMQ);
Mikhail Naganov712d71b2023-02-23 17:57:16 -080080 mIsAsynchronous = other.mIsAsynchronous;
David Li9cf5e622023-03-21 00:51:10 +080081 mIsMmapped = other.mIsMmapped;
82 mMmapBufferDescriptor = std::move(other.mMmapBufferDescriptor);
Mikhail Naganov89a9f742023-01-30 12:33:18 -080083 return *this;
84 }
85 bool isValid() const {
86 return mFrameSizeBytes != 0 &&
87 mCommandMQ != nullptr && mCommandMQ->isValid() &&
88 mReplyMQ != nullptr && mReplyMQ->isValid() &&
David Li9cf5e622023-03-21 00:51:10 +080089 (mDataMQ == nullptr || (
Mikhail Naganov89a9f742023-01-30 12:33:18 -080090 mDataMQ->isValid() &&
91 mDataMQ->getQuantumCount() * mDataMQ->getQuantumSize() >=
David Li9cf5e622023-03-21 00:51:10 +080092 mFrameSizeBytes * mBufferSizeFrames)) &&
93 (!mIsMmapped || mMmapBufferDescriptor.sharedMemory.fd.get() >= 0);
Mikhail Naganov89a9f742023-01-30 12:33:18 -080094 }
95 size_t getBufferSizeBytes() const { return mFrameSizeBytes * mBufferSizeFrames; }
96 size_t getBufferSizeFrames() const { return mBufferSizeFrames; }
Mikhail Naganov23224a12024-03-28 02:15:21 +000097 size_t getBufferDurationMs(int32_t sampleRate) const {
Jaideep Sharma0cb13872024-05-24 11:44:56 +053098 auto bufferSize = mIsMmapped ? getMmapBurstSize() : mBufferSizeFrames;
99 return sampleRate != 0 ? bufferSize * MILLIS_PER_SECOND / sampleRate : 0;
Mikhail Naganov23224a12024-03-28 02:15:21 +0000100 }
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800101 CommandMQ* getCommandMQ() const { return mCommandMQ.get(); }
102 DataMQ* getDataMQ() const { return mDataMQ.get(); }
103 size_t getFrameSizeBytes() const { return mFrameSizeBytes; }
104 ReplyMQ* getReplyMQ() const { return mReplyMQ.get(); }
Mikhail Naganov712d71b2023-02-23 17:57:16 -0800105 bool isAsynchronous() const { return mIsAsynchronous; }
David Li9cf5e622023-03-21 00:51:10 +0800106 bool isMmapped() const { return mIsMmapped; }
107 const MmapBufferDescriptor& getMmapBufferDescriptor() const { return mMmapBufferDescriptor; }
Jaideep Sharma0cb13872024-05-24 11:44:56 +0530108 size_t getMmapBurstSize() const { return mMmapBufferDescriptor.burstSizeFrames;}
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800109 private:
110 static std::unique_ptr<DataMQ> maybeCreateDataMQ(
111 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor) {
112 using Tag = ::aidl::android::hardware::audio::core::StreamDescriptor::AudioBuffer::Tag;
113 if (descriptor.audio.getTag() == Tag::fmq) {
114 return std::make_unique<DataMQ>(descriptor.audio.get<Tag::fmq>());
115 }
116 return nullptr;
117 }
David Li9cf5e622023-03-21 00:51:10 +0800118 static bool isMmapped(
119 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor) {
120 using Tag = ::aidl::android::hardware::audio::core::StreamDescriptor::AudioBuffer::Tag;
121 return descriptor.audio.getTag() == Tag::mmap;
122 }
123 static MmapBufferDescriptor maybeGetMmapBuffer(
124 ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor) {
125 using Tag = ::aidl::android::hardware::audio::core::StreamDescriptor::AudioBuffer::Tag;
126 if (descriptor.audio.getTag() == Tag::mmap) {
127 return std::move(descriptor.audio.get<Tag::mmap>());
128 }
129 return {};
130 }
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800131
132 size_t mFrameSizeBytes;
133 std::unique_ptr<CommandMQ> mCommandMQ;
134 std::unique_ptr<ReplyMQ> mReplyMQ;
135 size_t mBufferSizeFrames;
136 std::unique_ptr<DataMQ> mDataMQ;
Mikhail Naganov712d71b2023-02-23 17:57:16 -0800137 bool mIsAsynchronous;
David Li9cf5e622023-03-21 00:51:10 +0800138 bool mIsMmapped;
139 MmapBufferDescriptor mMmapBufferDescriptor;
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800140};
Mikhail Naganov31d46652023-01-10 18:29:25 +0000141
142class StreamHalAidl : public virtual StreamHalInterface, public ConversionHelperAidl {
143 public:
144 // Return size of input/output buffer in bytes for this stream - eg. 4800.
145 status_t getBufferSize(size_t *size) override;
146
147 // Return the base configuration of the stream:
148 // - channel mask;
149 // - format - e.g. AUDIO_FORMAT_PCM_16_BIT;
150 // - sampling rate in Hz - eg. 44100.
151 status_t getAudioProperties(audio_config_base_t *configBase) override;
152
153 // Set audio stream parameters.
154 status_t setParameters(const String8& kvPairs) override;
155
156 // Get audio stream parameters.
157 status_t getParameters(const String8& keys, String8 *values) override;
158
159 // Return the frame size (number of bytes per sample) of a stream.
160 status_t getFrameSize(size_t *size) override;
161
162 // Add or remove the effect on the stream.
163 status_t addEffect(sp<EffectHalInterface> effect) override;
164 status_t removeEffect(sp<EffectHalInterface> effect) override;
165
166 // Put the audio hardware input/output into standby mode.
167 status_t standby() override;
168
169 status_t dump(int fd, const Vector<String16>& args) override;
170
171 // Start a stream operating in mmap mode.
172 status_t start() override;
173
174 // Stop a stream operating in mmap mode.
175 status_t stop() override;
176
177 // Retrieve information on the data buffer in mmap mode.
178 status_t createMmapBuffer(int32_t minSizeFrames,
179 struct audio_mmap_buffer_info *info) override;
180
181 // Get current read/write position in the mmap buffer
182 status_t getMmapPosition(struct audio_mmap_position *position) override;
183
184 // Set the priority of the thread that interacts with the HAL
185 // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
186 status_t setHalThreadPriority(int priority) override;
187
188 status_t legacyCreateAudioPatch(const struct audio_port_config& port,
189 std::optional<audio_source_t> source,
190 audio_devices_t type) override;
191
192 status_t legacyReleaseAudioPatch() override;
193
194 protected:
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700195 // For tests.
196 friend class sp<StreamHalAidl>;
197
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700198 struct StatePositions {
199 int64_t framesAtFlushOrDrain;
200 int64_t framesAtStandby;
201 };
202
Mikhail Naganovfab697c2023-01-11 19:33:13 +0000203 template<class T>
204 static std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> getStreamCommon(
205 const std::shared_ptr<T>& stream);
206
Mikhail Naganov31d46652023-01-10 18:29:25 +0000207 // Subclasses can not be constructed directly by clients.
208 StreamHalAidl(std::string_view className,
209 bool isInput,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800210 const audio_config& config,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800211 int32_t nominalLatency,
212 StreamContextAidl&& context,
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700213 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon>& stream,
214 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>& vext);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000215
216 ~StreamHalAidl() override;
217
Ajender Reddy5f95b4a2024-07-16 17:15:16 +0530218 ::aidl::android::hardware::audio::core::StreamDescriptor::State getState() {
219 std::lock_guard l(mLock);
220 return mLastReply.state;
221 }
222
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800223 status_t getLatency(uint32_t *latency);
224
Mikhail Naganove2084702023-09-28 14:37:12 -0700225 // Always returns non-negative values.
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700226 status_t getObservablePosition(int64_t* frames, int64_t* timestamp,
227 StatePositions* statePositions = nullptr);
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800228
Mikhail Naganove2084702023-09-28 14:37:12 -0700229 // Always returns non-negative values.
David Li9cf5e622023-03-21 00:51:10 +0800230 status_t getHardwarePosition(int64_t *frames, int64_t *timestamp);
231
Mikhail Naganove2084702023-09-28 14:37:12 -0700232 // Always returns non-negative values.
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800233 status_t getXruns(int32_t *frames);
234
235 status_t transfer(void *buffer, size_t bytes, size_t *transferred);
236
237 status_t pause(
238 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
239
240 status_t resume(
241 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
242
243 status_t drain(bool earlyNotify,
244 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
245
246 status_t flush(
247 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
248
249 status_t exit();
250
Mikhail Naganov23224a12024-03-28 02:15:21 +0000251 void onAsyncTransferReady();
252 void onAsyncDrainReady();
253 void onAsyncError();
254
Mikhail Naganov31d46652023-01-10 18:29:25 +0000255 const bool mIsInput;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800256 const audio_config_base_t mConfig;
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800257 const StreamContextAidl mContext;
Mikhail Naganov503b1d82024-04-15 17:58:08 +0000258 // This lock is used to make sending of a command and receiving a reply an atomic
259 // operation. Otherwise, when two threads are trying to send a command, they may both advance to
260 // reading of the reply once the HAL has consumed the command from the MQ, and that creates a
261 // race condition between them.
262 //
263 // Note that only access to command and reply MQs needs to be protected because the data MQ is
264 // only accessed by the I/O thread. Also, there is no need to protect lookup operations on the
265 // queues as they are thread-safe, only send/receive operation must be protected.
266 std::mutex mCommandReplyLock;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000267
268 private:
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800269 static audio_config_base_t configToBase(const audio_config& config) {
270 audio_config_base_t result = AUDIO_CONFIG_BASE_INITIALIZER;
271 result.sample_rate = config.sample_rate;
272 result.channel_mask = config.channel_mask;
273 result.format = config.format;
274 return result;
275 }
Mikhail Naganov503b1d82024-04-15 17:58:08 +0000276 // Note: Since `sendCommand` takes mLock while holding mCommandReplyLock, never call
277 // it with `mLock` being held.
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800278 status_t sendCommand(
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700279 const ::aidl::android::hardware::audio::core::StreamDescriptor::Command& command,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800280 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr,
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700281 bool safeFromNonWorkerThread = false,
282 StatePositions* statePositions = nullptr);
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800283 status_t updateCountersIfNeeded(
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700284 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr,
285 StatePositions* statePositions = nullptr);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000286
Mikhail Naganov31d46652023-01-10 18:29:25 +0000287 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> mStream;
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700288 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> mVendorExt;
Mikhail Naganov23224a12024-03-28 02:15:21 +0000289 const int64_t mLastReplyLifeTimeNs;
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800290 std::mutex mLock;
291 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply mLastReply GUARDED_BY(mLock);
Mikhail Naganov23224a12024-03-28 02:15:21 +0000292 int64_t mLastReplyExpirationNs GUARDED_BY(mLock) = 0;
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700293 // Cached values of observable positions when the stream last entered certain state.
294 // Updated for output streams only.
295 StatePositions mStatePositions GUARDED_BY(mLock) = {};
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800296 // mStreamPowerLog is used for audio signal power logging.
297 StreamPowerLog mStreamPowerLog;
298 std::atomic<pid_t> mWorkerTid = -1;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000299};
300
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800301class CallbackBroker;
302
Mikhail Naganov23224a12024-03-28 02:15:21 +0000303class StreamOutHalAidl : public virtual StreamOutHalInterface,
304 public virtual StreamOutHalInterfaceCallback,
305 public StreamHalAidl {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000306 public:
David Lia0ad9f02023-03-02 21:46:19 +0800307 // Extract the output stream parameters and set by AIDL APIs.
308 status_t setParameters(const String8& kvPairs) override;
309
Mikhail Naganov31d46652023-01-10 18:29:25 +0000310 // Return the audio hardware driver estimated latency in milliseconds.
311 status_t getLatency(uint32_t *latency) override;
312
313 // Use this method in situations where audio mixing is done in the hardware.
314 status_t setVolume(float left, float right) override;
315
316 // Selects the audio presentation (if available).
317 status_t selectPresentation(int presentationId, int programId) override;
318
319 // Write audio buffer to driver.
320 status_t write(const void *buffer, size_t bytes, size_t *written) override;
321
322 // Return the number of audio frames written by the audio dsp to DAC since
323 // the output has exited standby.
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -0700324 status_t getRenderPosition(uint64_t *dspFrames) override;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000325
326 // Set the callback for notifying completion of non-blocking write and drain.
327 status_t setCallback(wp<StreamOutHalInterfaceCallback> callback) override;
328
329 // Returns whether pause and resume operations are supported.
330 status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume) override;
331
332 // Notifies to the audio driver to resume playback following a pause.
333 status_t pause() override;
334
335 // Notifies to the audio driver to resume playback following a pause.
336 status_t resume() override;
337
338 // Returns whether drain operation is supported.
339 status_t supportsDrain(bool *supportsDrain) override;
340
341 // Requests notification when data buffered by the driver/hardware has been played.
342 status_t drain(bool earlyNotify) override;
343
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700344 // Notifies to the audio driver to flush (that is, drop) the queued data. Stream must
345 // already be paused before calling 'flush'.
Mikhail Naganov31d46652023-01-10 18:29:25 +0000346 status_t flush() override;
347
348 // Return a recent count of the number of audio frames presented to an external observer.
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700349 // This excludes frames which have been written but are still in the pipeline. See the
350 // table at the start of the 'StreamOutHalInterface' for the specification of the frame
351 // count behavior w.r.t. 'flush', 'drain' and 'standby' operations.
Mikhail Naganov31d46652023-01-10 18:29:25 +0000352 status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp) override;
353
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -0700354 // Notifies the HAL layer that the framework considers the current playback as completed.
355 status_t presentationComplete() override;
356
Mikhail Naganov31d46652023-01-10 18:29:25 +0000357 // Called when the metadata of the stream's source has been changed.
358 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
359
360 // Returns the Dual Mono mode presentation setting.
361 status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override;
362
363 // Sets the Dual Mono mode presentation on the output device.
364 status_t setDualMonoMode(audio_dual_mono_mode_t mode) override;
365
366 // Returns the Audio Description Mix level in dB.
367 status_t getAudioDescriptionMixLevel(float* leveldB) override;
368
369 // Sets the Audio Description Mix level in dB.
370 status_t setAudioDescriptionMixLevel(float leveldB) override;
371
372 // Retrieves current playback rate parameters.
373 status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override;
374
375 // Sets the playback rate parameters that control playback behavior.
376 status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override;
377
378 status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override;
379
380 status_t setLatencyMode(audio_latency_mode_t mode) override;
381 status_t getRecommendedLatencyModes(std::vector<audio_latency_mode_t> *modes) override;
382 status_t setLatencyModeCallback(
383 const sp<StreamOutHalInterfaceLatencyModeCallback>& callback) override;
384
Mikhail Naganov31d46652023-01-10 18:29:25 +0000385 status_t exit() override;
386
Mikhail Naganov23224a12024-03-28 02:15:21 +0000387 // StreamOutHalInterfaceCallback
388 void onWriteReady() override;
389 void onDrainReady() override;
Mikhail Naganovf548cd32024-05-29 17:06:46 +0000390 void onError(bool isHardError) override;
Mikhail Naganov23224a12024-03-28 02:15:21 +0000391
Mikhail Naganov31d46652023-01-10 18:29:25 +0000392 private:
393 friend class sp<StreamOutHalAidl>;
394
Mikhail Naganov19418e32023-03-10 17:55:14 -0800395 static ConversionResult<::aidl::android::hardware::audio::common::SourceMetadata>
Mikhail Naganovb6e57752023-03-08 18:12:47 -0800396 legacy2aidl_SourceMetadata(const StreamOutHalInterface::SourceMetadata& legacy);
397
Mikhail Naganov31d46652023-01-10 18:29:25 +0000398 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut> mStream;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800399 const wp<CallbackBroker> mCallbackBroker;
Mikhail Naganov23224a12024-03-28 02:15:21 +0000400 mediautils::atomic_wp<StreamOutHalInterfaceCallback> mClientCallback;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000401
David Lia0ad9f02023-03-02 21:46:19 +0800402 AudioOffloadMetadata mOffloadMetadata;
403
Mikhail Naganov31d46652023-01-10 18:29:25 +0000404 // Can not be constructed directly by clients.
405 StreamOutHalAidl(
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800406 const audio_config& config, StreamContextAidl&& context, int32_t nominalLatency,
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800407 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut>& stream,
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700408 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>& vext,
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800409 const sp<CallbackBroker>& callbackBroker);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000410
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800411 ~StreamOutHalAidl() override;
David Lia0ad9f02023-03-02 21:46:19 +0800412
413 // Filter and update the offload metadata. The parameters which are related to the offload
414 // metadata will be removed after filtering.
415 status_t filterAndUpdateOffloadMetadata(AudioParameter &parameters);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000416};
417
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800418class MicrophoneInfoProvider;
419
Mikhail Naganov31d46652023-01-10 18:29:25 +0000420class StreamInHalAidl : public StreamInHalInterface, public StreamHalAidl {
421 public:
422 // Set the input gain for the audio driver.
423 status_t setGain(float gain) override;
424
425 // Read audio buffer in from driver.
426 status_t read(void *buffer, size_t bytes, size_t *read) override;
427
428 // Return the amount of input frames lost in the audio driver.
429 status_t getInputFramesLost(uint32_t *framesLost) override;
430
431 // Return a recent count of the number of audio frames received and
432 // the clock time associated with that frame count.
Mikhail Naganovf0a9fbc2024-05-14 14:05:16 -0700433 // The count must not reset to zero when a PCM input enters standby.
Mikhail Naganov31d46652023-01-10 18:29:25 +0000434 status_t getCapturePosition(int64_t *frames, int64_t *time) override;
435
436 // Get active microphones
Mikhail Naganov2a6a3012023-02-13 11:45:03 -0800437 status_t getActiveMicrophones(std::vector<media::MicrophoneInfoFw> *microphones) override;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000438
439 // Set microphone direction (for processing)
440 status_t setPreferredMicrophoneDirection(
441 audio_microphone_direction_t direction) override;
442
443 // Set microphone zoom (for processing)
444 status_t setPreferredMicrophoneFieldDimension(float zoom) override;
445
446 // Called when the metadata of the stream's sink has been changed.
447 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
448
449 private:
450 friend class sp<StreamInHalAidl>;
451
Mikhail Naganov19418e32023-03-10 17:55:14 -0800452 static ConversionResult<::aidl::android::hardware::audio::common::SinkMetadata>
Mikhail Naganovb6e57752023-03-08 18:12:47 -0800453 legacy2aidl_SinkMetadata(const StreamInHalInterface::SinkMetadata& legacy);
454
Mikhail Naganov31d46652023-01-10 18:29:25 +0000455 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn> mStream;
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800456 const wp<MicrophoneInfoProvider> mMicInfoProvider;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000457
458 // Can not be constructed directly by clients.
459 StreamInHalAidl(
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800460 const audio_config& config, StreamContextAidl&& context, int32_t nominalLatency,
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800461 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn>& stream,
Mikhail Naganove7a26ad2023-05-25 17:36:48 -0700462 const std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>& vext,
Mikhail Naganovcad0afe2023-03-10 14:25:57 -0800463 const sp<MicrophoneInfoProvider>& micInfoProvider);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000464
465 ~StreamInHalAidl() override = default;
466};
467
468} // namespace android