blob: 8e5951326086a070a7a3fde186d3e4160a260637 [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>
28#include <fmq/AidlMessageQueue.h>
29#include <media/audiohal/EffectHalInterface.h>
30#include <media/audiohal/StreamHalInterface.h>
David Lia0ad9f02023-03-02 21:46:19 +080031#include <media/AudioParameter.h>
Mikhail Naganov31d46652023-01-10 18:29:25 +000032
33#include "ConversionHelperAidl.h"
34#include "StreamPowerLog.h"
35
David Lia0ad9f02023-03-02 21:46:19 +080036using ::aidl::android::hardware::audio::common::AudioOffloadMetadata;
37
Mikhail Naganov31d46652023-01-10 18:29:25 +000038namespace android {
39
Mikhail Naganov89a9f742023-01-30 12:33:18 -080040class StreamContextAidl {
41 public:
42 typedef AidlMessageQueue<::aidl::android::hardware::audio::core::StreamDescriptor::Command,
43 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> CommandMQ;
44 typedef AidlMessageQueue<::aidl::android::hardware::audio::core::StreamDescriptor::Reply,
45 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> ReplyMQ;
46 typedef AidlMessageQueue<int8_t,
47 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> DataMQ;
48
49 explicit StreamContextAidl(
Mikhail Naganov712d71b2023-02-23 17:57:16 -080050 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor,
51 bool isAsynchronous)
Mikhail Naganov89a9f742023-01-30 12:33:18 -080052 : mFrameSizeBytes(descriptor.frameSizeBytes),
53 mCommandMQ(new CommandMQ(descriptor.command)),
54 mReplyMQ(new ReplyMQ(descriptor.reply)),
55 mBufferSizeFrames(descriptor.bufferSizeFrames),
Mikhail Naganov712d71b2023-02-23 17:57:16 -080056 mDataMQ(maybeCreateDataMQ(descriptor)),
57 mIsAsynchronous(isAsynchronous) {}
Mikhail Naganov89a9f742023-01-30 12:33:18 -080058 StreamContextAidl(StreamContextAidl&& other) :
59 mFrameSizeBytes(other.mFrameSizeBytes),
60 mCommandMQ(std::move(other.mCommandMQ)),
61 mReplyMQ(std::move(other.mReplyMQ)),
62 mBufferSizeFrames(other.mBufferSizeFrames),
Mikhail Naganov712d71b2023-02-23 17:57:16 -080063 mDataMQ(std::move(other.mDataMQ)),
64 mIsAsynchronous(other.mIsAsynchronous) {}
Mikhail Naganov89a9f742023-01-30 12:33:18 -080065 StreamContextAidl& operator=(StreamContextAidl&& other) {
66 mFrameSizeBytes = other.mFrameSizeBytes;
67 mCommandMQ = std::move(other.mCommandMQ);
68 mReplyMQ = std::move(other.mReplyMQ);
69 mBufferSizeFrames = other.mBufferSizeFrames;
70 mDataMQ = std::move(other.mDataMQ);
Mikhail Naganov712d71b2023-02-23 17:57:16 -080071 mIsAsynchronous = other.mIsAsynchronous;
Mikhail Naganov89a9f742023-01-30 12:33:18 -080072 return *this;
73 }
74 bool isValid() const {
75 return mFrameSizeBytes != 0 &&
76 mCommandMQ != nullptr && mCommandMQ->isValid() &&
77 mReplyMQ != nullptr && mReplyMQ->isValid() &&
78 (mDataMQ != nullptr || (
79 mDataMQ->isValid() &&
80 mDataMQ->getQuantumCount() * mDataMQ->getQuantumSize() >=
81 mFrameSizeBytes * mBufferSizeFrames));
82 }
83 size_t getBufferSizeBytes() const { return mFrameSizeBytes * mBufferSizeFrames; }
84 size_t getBufferSizeFrames() const { return mBufferSizeFrames; }
85 CommandMQ* getCommandMQ() const { return mCommandMQ.get(); }
86 DataMQ* getDataMQ() const { return mDataMQ.get(); }
87 size_t getFrameSizeBytes() const { return mFrameSizeBytes; }
88 ReplyMQ* getReplyMQ() const { return mReplyMQ.get(); }
Mikhail Naganov712d71b2023-02-23 17:57:16 -080089 bool isAsynchronous() const { return mIsAsynchronous; }
Mikhail Naganov89a9f742023-01-30 12:33:18 -080090
91 private:
92 static std::unique_ptr<DataMQ> maybeCreateDataMQ(
93 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor) {
94 using Tag = ::aidl::android::hardware::audio::core::StreamDescriptor::AudioBuffer::Tag;
95 if (descriptor.audio.getTag() == Tag::fmq) {
96 return std::make_unique<DataMQ>(descriptor.audio.get<Tag::fmq>());
97 }
98 return nullptr;
99 }
100
101 size_t mFrameSizeBytes;
102 std::unique_ptr<CommandMQ> mCommandMQ;
103 std::unique_ptr<ReplyMQ> mReplyMQ;
104 size_t mBufferSizeFrames;
105 std::unique_ptr<DataMQ> mDataMQ;
Mikhail Naganov712d71b2023-02-23 17:57:16 -0800106 bool mIsAsynchronous;
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800107};
Mikhail Naganov31d46652023-01-10 18:29:25 +0000108
109class StreamHalAidl : public virtual StreamHalInterface, public ConversionHelperAidl {
110 public:
111 // Return size of input/output buffer in bytes for this stream - eg. 4800.
112 status_t getBufferSize(size_t *size) override;
113
114 // Return the base configuration of the stream:
115 // - channel mask;
116 // - format - e.g. AUDIO_FORMAT_PCM_16_BIT;
117 // - sampling rate in Hz - eg. 44100.
118 status_t getAudioProperties(audio_config_base_t *configBase) override;
119
120 // Set audio stream parameters.
121 status_t setParameters(const String8& kvPairs) override;
122
123 // Get audio stream parameters.
124 status_t getParameters(const String8& keys, String8 *values) override;
125
126 // Return the frame size (number of bytes per sample) of a stream.
127 status_t getFrameSize(size_t *size) override;
128
129 // Add or remove the effect on the stream.
130 status_t addEffect(sp<EffectHalInterface> effect) override;
131 status_t removeEffect(sp<EffectHalInterface> effect) override;
132
133 // Put the audio hardware input/output into standby mode.
134 status_t standby() override;
135
136 status_t dump(int fd, const Vector<String16>& args) override;
137
138 // Start a stream operating in mmap mode.
139 status_t start() override;
140
141 // Stop a stream operating in mmap mode.
142 status_t stop() override;
143
144 // Retrieve information on the data buffer in mmap mode.
145 status_t createMmapBuffer(int32_t minSizeFrames,
146 struct audio_mmap_buffer_info *info) override;
147
148 // Get current read/write position in the mmap buffer
149 status_t getMmapPosition(struct audio_mmap_position *position) override;
150
151 // Set the priority of the thread that interacts with the HAL
152 // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
153 status_t setHalThreadPriority(int priority) override;
154
155 status_t legacyCreateAudioPatch(const struct audio_port_config& port,
156 std::optional<audio_source_t> source,
157 audio_devices_t type) override;
158
159 status_t legacyReleaseAudioPatch() override;
160
161 protected:
Mikhail Naganovfab697c2023-01-11 19:33:13 +0000162 template<class T>
163 static std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> getStreamCommon(
164 const std::shared_ptr<T>& stream);
165
Mikhail Naganov31d46652023-01-10 18:29:25 +0000166 // Subclasses can not be constructed directly by clients.
167 StreamHalAidl(std::string_view className,
168 bool isInput,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800169 const audio_config& config,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800170 int32_t nominalLatency,
171 StreamContextAidl&& context,
Mikhail Naganov31d46652023-01-10 18:29:25 +0000172 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon>& stream);
173
174 ~StreamHalAidl() override;
175
176 status_t getHalPid(pid_t *pid);
177
178 bool requestHalThreadPriority(pid_t threadPid, pid_t threadId);
179
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800180 status_t getLatency(uint32_t *latency);
181
182 status_t getObservablePosition(int64_t *frames, int64_t *timestamp);
183
184 status_t getXruns(int32_t *frames);
185
186 status_t transfer(void *buffer, size_t bytes, size_t *transferred);
187
188 status_t pause(
189 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
190
191 status_t resume(
192 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
193
194 status_t drain(bool earlyNotify,
195 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
196
197 status_t flush(
198 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
199
200 status_t exit();
201
Mikhail Naganov31d46652023-01-10 18:29:25 +0000202 const bool mIsInput;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800203 const audio_config_base_t mConfig;
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800204 const StreamContextAidl mContext;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000205
206 private:
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800207 static audio_config_base_t configToBase(const audio_config& config) {
208 audio_config_base_t result = AUDIO_CONFIG_BASE_INITIALIZER;
209 result.sample_rate = config.sample_rate;
210 result.channel_mask = config.channel_mask;
211 result.format = config.format;
212 return result;
213 }
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800214 ::aidl::android::hardware::audio::core::StreamDescriptor::State getState() {
215 std::lock_guard l(mLock);
216 return mLastReply.state;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000217 }
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800218 status_t sendCommand(
219 const ::aidl::android::hardware::audio::core::StreamDescriptor::Command &command,
220 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr,
221 bool safeFromNonWorkerThread = false);
222 status_t updateCountersIfNeeded(
223 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000224
Mikhail Naganov31d46652023-01-10 18:29:25 +0000225 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> mStream;
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800226 std::mutex mLock;
227 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply mLastReply GUARDED_BY(mLock);
228 // mStreamPowerLog is used for audio signal power logging.
229 StreamPowerLog mStreamPowerLog;
230 std::atomic<pid_t> mWorkerTid = -1;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000231};
232
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800233class CallbackBroker;
234
Mikhail Naganov31d46652023-01-10 18:29:25 +0000235class StreamOutHalAidl : public StreamOutHalInterface, public StreamHalAidl {
236 public:
David Lia0ad9f02023-03-02 21:46:19 +0800237 // Extract the output stream parameters and set by AIDL APIs.
238 status_t setParameters(const String8& kvPairs) override;
239
Mikhail Naganov31d46652023-01-10 18:29:25 +0000240 // Return the audio hardware driver estimated latency in milliseconds.
241 status_t getLatency(uint32_t *latency) override;
242
243 // Use this method in situations where audio mixing is done in the hardware.
244 status_t setVolume(float left, float right) override;
245
246 // Selects the audio presentation (if available).
247 status_t selectPresentation(int presentationId, int programId) override;
248
249 // Write audio buffer to driver.
250 status_t write(const void *buffer, size_t bytes, size_t *written) override;
251
252 // Return the number of audio frames written by the audio dsp to DAC since
253 // the output has exited standby.
254 status_t getRenderPosition(uint32_t *dspFrames) override;
255
256 // Get the local time at which the next write to the audio driver will be presented.
257 status_t getNextWriteTimestamp(int64_t *timestamp) override;
258
259 // Set the callback for notifying completion of non-blocking write and drain.
260 status_t setCallback(wp<StreamOutHalInterfaceCallback> callback) override;
261
262 // Returns whether pause and resume operations are supported.
263 status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume) override;
264
265 // Notifies to the audio driver to resume playback following a pause.
266 status_t pause() override;
267
268 // Notifies to the audio driver to resume playback following a pause.
269 status_t resume() override;
270
271 // Returns whether drain operation is supported.
272 status_t supportsDrain(bool *supportsDrain) override;
273
274 // Requests notification when data buffered by the driver/hardware has been played.
275 status_t drain(bool earlyNotify) override;
276
277 // Notifies to the audio driver to flush the queued data.
278 status_t flush() override;
279
280 // Return a recent count of the number of audio frames presented to an external observer.
281 status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp) override;
282
283 // Called when the metadata of the stream's source has been changed.
284 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
285
286 // Returns the Dual Mono mode presentation setting.
287 status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override;
288
289 // Sets the Dual Mono mode presentation on the output device.
290 status_t setDualMonoMode(audio_dual_mono_mode_t mode) override;
291
292 // Returns the Audio Description Mix level in dB.
293 status_t getAudioDescriptionMixLevel(float* leveldB) override;
294
295 // Sets the Audio Description Mix level in dB.
296 status_t setAudioDescriptionMixLevel(float leveldB) override;
297
298 // Retrieves current playback rate parameters.
299 status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override;
300
301 // Sets the playback rate parameters that control playback behavior.
302 status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override;
303
304 status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override;
305
306 status_t setLatencyMode(audio_latency_mode_t mode) override;
307 status_t getRecommendedLatencyModes(std::vector<audio_latency_mode_t> *modes) override;
308 status_t setLatencyModeCallback(
309 const sp<StreamOutHalInterfaceLatencyModeCallback>& callback) override;
310
Mikhail Naganov31d46652023-01-10 18:29:25 +0000311 status_t exit() override;
312
Mikhail Naganov31d46652023-01-10 18:29:25 +0000313 private:
314 friend class sp<StreamOutHalAidl>;
315
Mikhail Naganov31d46652023-01-10 18:29:25 +0000316 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut> mStream;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800317 const wp<CallbackBroker> mCallbackBroker;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000318
David Lia0ad9f02023-03-02 21:46:19 +0800319 AudioOffloadMetadata mOffloadMetadata;
320
Mikhail Naganov31d46652023-01-10 18:29:25 +0000321 // Can not be constructed directly by clients.
322 StreamOutHalAidl(
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800323 const audio_config& config, StreamContextAidl&& context, int32_t nominalLatency,
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800324 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut>& stream,
325 const sp<CallbackBroker>& callbackBroker);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000326
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800327 ~StreamOutHalAidl() override;
David Lia0ad9f02023-03-02 21:46:19 +0800328
329 // Filter and update the offload metadata. The parameters which are related to the offload
330 // metadata will be removed after filtering.
331 status_t filterAndUpdateOffloadMetadata(AudioParameter &parameters);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000332};
333
334class StreamInHalAidl : public StreamInHalInterface, public StreamHalAidl {
335 public:
336 // Set the input gain for the audio driver.
337 status_t setGain(float gain) override;
338
339 // Read audio buffer in from driver.
340 status_t read(void *buffer, size_t bytes, size_t *read) override;
341
342 // Return the amount of input frames lost in the audio driver.
343 status_t getInputFramesLost(uint32_t *framesLost) override;
344
345 // Return a recent count of the number of audio frames received and
346 // the clock time associated with that frame count.
347 status_t getCapturePosition(int64_t *frames, int64_t *time) override;
348
349 // Get active microphones
Mikhail Naganov2a6a3012023-02-13 11:45:03 -0800350 status_t getActiveMicrophones(std::vector<media::MicrophoneInfoFw> *microphones) override;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000351
352 // Set microphone direction (for processing)
353 status_t setPreferredMicrophoneDirection(
354 audio_microphone_direction_t direction) override;
355
356 // Set microphone zoom (for processing)
357 status_t setPreferredMicrophoneFieldDimension(float zoom) override;
358
359 // Called when the metadata of the stream's sink has been changed.
360 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
361
362 private:
363 friend class sp<StreamInHalAidl>;
364
365 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn> mStream;
366
367 // Can not be constructed directly by clients.
368 StreamInHalAidl(
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800369 const audio_config& config, StreamContextAidl&& context, int32_t nominalLatency,
Mikhail Naganov31d46652023-01-10 18:29:25 +0000370 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn>& stream);
371
372 ~StreamInHalAidl() override = default;
373};
374
375} // namespace android