blob: d00774c4f9cf8a41e73d96101dfdb47c6e6f6671 [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
24#include <aidl/android/hardware/audio/core/BpStreamCommon.h>
25#include <aidl/android/hardware/audio/core/BpStreamIn.h>
26#include <aidl/android/hardware/audio/core/BpStreamOut.h>
27#include <fmq/AidlMessageQueue.h>
28#include <media/audiohal/EffectHalInterface.h>
29#include <media/audiohal/StreamHalInterface.h>
Mikhail Naganov31d46652023-01-10 18:29:25 +000030
31#include "ConversionHelperAidl.h"
32#include "StreamPowerLog.h"
33
34namespace android {
35
Mikhail Naganov89a9f742023-01-30 12:33:18 -080036class StreamContextAidl {
37 public:
38 typedef AidlMessageQueue<::aidl::android::hardware::audio::core::StreamDescriptor::Command,
39 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> CommandMQ;
40 typedef AidlMessageQueue<::aidl::android::hardware::audio::core::StreamDescriptor::Reply,
41 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> ReplyMQ;
42 typedef AidlMessageQueue<int8_t,
43 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> DataMQ;
44
45 explicit StreamContextAidl(
Mikhail Naganov712d71b2023-02-23 17:57:16 -080046 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor,
47 bool isAsynchronous)
Mikhail Naganov89a9f742023-01-30 12:33:18 -080048 : mFrameSizeBytes(descriptor.frameSizeBytes),
49 mCommandMQ(new CommandMQ(descriptor.command)),
50 mReplyMQ(new ReplyMQ(descriptor.reply)),
51 mBufferSizeFrames(descriptor.bufferSizeFrames),
Mikhail Naganov712d71b2023-02-23 17:57:16 -080052 mDataMQ(maybeCreateDataMQ(descriptor)),
53 mIsAsynchronous(isAsynchronous) {}
Mikhail Naganov89a9f742023-01-30 12:33:18 -080054 StreamContextAidl(StreamContextAidl&& other) :
55 mFrameSizeBytes(other.mFrameSizeBytes),
56 mCommandMQ(std::move(other.mCommandMQ)),
57 mReplyMQ(std::move(other.mReplyMQ)),
58 mBufferSizeFrames(other.mBufferSizeFrames),
Mikhail Naganov712d71b2023-02-23 17:57:16 -080059 mDataMQ(std::move(other.mDataMQ)),
60 mIsAsynchronous(other.mIsAsynchronous) {}
Mikhail Naganov89a9f742023-01-30 12:33:18 -080061 StreamContextAidl& operator=(StreamContextAidl&& other) {
62 mFrameSizeBytes = other.mFrameSizeBytes;
63 mCommandMQ = std::move(other.mCommandMQ);
64 mReplyMQ = std::move(other.mReplyMQ);
65 mBufferSizeFrames = other.mBufferSizeFrames;
66 mDataMQ = std::move(other.mDataMQ);
Mikhail Naganov712d71b2023-02-23 17:57:16 -080067 mIsAsynchronous = other.mIsAsynchronous;
Mikhail Naganov89a9f742023-01-30 12:33:18 -080068 return *this;
69 }
70 bool isValid() const {
71 return mFrameSizeBytes != 0 &&
72 mCommandMQ != nullptr && mCommandMQ->isValid() &&
73 mReplyMQ != nullptr && mReplyMQ->isValid() &&
74 (mDataMQ != nullptr || (
75 mDataMQ->isValid() &&
76 mDataMQ->getQuantumCount() * mDataMQ->getQuantumSize() >=
77 mFrameSizeBytes * mBufferSizeFrames));
78 }
79 size_t getBufferSizeBytes() const { return mFrameSizeBytes * mBufferSizeFrames; }
80 size_t getBufferSizeFrames() const { return mBufferSizeFrames; }
81 CommandMQ* getCommandMQ() const { return mCommandMQ.get(); }
82 DataMQ* getDataMQ() const { return mDataMQ.get(); }
83 size_t getFrameSizeBytes() const { return mFrameSizeBytes; }
84 ReplyMQ* getReplyMQ() const { return mReplyMQ.get(); }
Mikhail Naganov712d71b2023-02-23 17:57:16 -080085 bool isAsynchronous() const { return mIsAsynchronous; }
Mikhail Naganov89a9f742023-01-30 12:33:18 -080086
87 private:
88 static std::unique_ptr<DataMQ> maybeCreateDataMQ(
89 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor) {
90 using Tag = ::aidl::android::hardware::audio::core::StreamDescriptor::AudioBuffer::Tag;
91 if (descriptor.audio.getTag() == Tag::fmq) {
92 return std::make_unique<DataMQ>(descriptor.audio.get<Tag::fmq>());
93 }
94 return nullptr;
95 }
96
97 size_t mFrameSizeBytes;
98 std::unique_ptr<CommandMQ> mCommandMQ;
99 std::unique_ptr<ReplyMQ> mReplyMQ;
100 size_t mBufferSizeFrames;
101 std::unique_ptr<DataMQ> mDataMQ;
Mikhail Naganov712d71b2023-02-23 17:57:16 -0800102 bool mIsAsynchronous;
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800103};
Mikhail Naganov31d46652023-01-10 18:29:25 +0000104
105class StreamHalAidl : public virtual StreamHalInterface, public ConversionHelperAidl {
106 public:
107 // Return size of input/output buffer in bytes for this stream - eg. 4800.
108 status_t getBufferSize(size_t *size) override;
109
110 // Return the base configuration of the stream:
111 // - channel mask;
112 // - format - e.g. AUDIO_FORMAT_PCM_16_BIT;
113 // - sampling rate in Hz - eg. 44100.
114 status_t getAudioProperties(audio_config_base_t *configBase) override;
115
116 // Set audio stream parameters.
117 status_t setParameters(const String8& kvPairs) override;
118
119 // Get audio stream parameters.
120 status_t getParameters(const String8& keys, String8 *values) override;
121
122 // Return the frame size (number of bytes per sample) of a stream.
123 status_t getFrameSize(size_t *size) override;
124
125 // Add or remove the effect on the stream.
126 status_t addEffect(sp<EffectHalInterface> effect) override;
127 status_t removeEffect(sp<EffectHalInterface> effect) override;
128
129 // Put the audio hardware input/output into standby mode.
130 status_t standby() override;
131
132 status_t dump(int fd, const Vector<String16>& args) override;
133
134 // Start a stream operating in mmap mode.
135 status_t start() override;
136
137 // Stop a stream operating in mmap mode.
138 status_t stop() override;
139
140 // Retrieve information on the data buffer in mmap mode.
141 status_t createMmapBuffer(int32_t minSizeFrames,
142 struct audio_mmap_buffer_info *info) override;
143
144 // Get current read/write position in the mmap buffer
145 status_t getMmapPosition(struct audio_mmap_position *position) override;
146
147 // Set the priority of the thread that interacts with the HAL
148 // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
149 status_t setHalThreadPriority(int priority) override;
150
151 status_t legacyCreateAudioPatch(const struct audio_port_config& port,
152 std::optional<audio_source_t> source,
153 audio_devices_t type) override;
154
155 status_t legacyReleaseAudioPatch() override;
156
157 protected:
Mikhail Naganovfab697c2023-01-11 19:33:13 +0000158 template<class T>
159 static std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> getStreamCommon(
160 const std::shared_ptr<T>& stream);
161
Mikhail Naganov31d46652023-01-10 18:29:25 +0000162 // Subclasses can not be constructed directly by clients.
163 StreamHalAidl(std::string_view className,
164 bool isInput,
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800165 const audio_config& config,
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800166 int32_t nominalLatency,
167 StreamContextAidl&& context,
Mikhail Naganov31d46652023-01-10 18:29:25 +0000168 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon>& stream);
169
170 ~StreamHalAidl() override;
171
172 status_t getHalPid(pid_t *pid);
173
174 bool requestHalThreadPriority(pid_t threadPid, pid_t threadId);
175
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800176 status_t getLatency(uint32_t *latency);
177
178 status_t getObservablePosition(int64_t *frames, int64_t *timestamp);
179
180 status_t getXruns(int32_t *frames);
181
182 status_t transfer(void *buffer, size_t bytes, size_t *transferred);
183
184 status_t pause(
185 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
186
187 status_t resume(
188 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
189
190 status_t drain(bool earlyNotify,
191 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
192
193 status_t flush(
194 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
195
196 status_t exit();
197
Mikhail Naganov31d46652023-01-10 18:29:25 +0000198 const bool mIsInput;
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800199 const audio_config_base_t mConfig;
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800200 const StreamContextAidl mContext;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000201
202 private:
Mikhail Naganov5b1eed12023-01-25 11:29:11 -0800203 static audio_config_base_t configToBase(const audio_config& config) {
204 audio_config_base_t result = AUDIO_CONFIG_BASE_INITIALIZER;
205 result.sample_rate = config.sample_rate;
206 result.channel_mask = config.channel_mask;
207 result.format = config.format;
208 return result;
209 }
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800210 ::aidl::android::hardware::audio::core::StreamDescriptor::State getState() {
211 std::lock_guard l(mLock);
212 return mLastReply.state;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000213 }
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800214 status_t sendCommand(
215 const ::aidl::android::hardware::audio::core::StreamDescriptor::Command &command,
216 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr,
217 bool safeFromNonWorkerThread = false);
218 status_t updateCountersIfNeeded(
219 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000220
Mikhail Naganov31d46652023-01-10 18:29:25 +0000221 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> mStream;
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800222 std::mutex mLock;
223 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply mLastReply GUARDED_BY(mLock);
224 // mStreamPowerLog is used for audio signal power logging.
225 StreamPowerLog mStreamPowerLog;
226 std::atomic<pid_t> mWorkerTid = -1;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000227};
228
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800229class CallbackBroker;
230
Mikhail Naganov31d46652023-01-10 18:29:25 +0000231class StreamOutHalAidl : public StreamOutHalInterface, public StreamHalAidl {
232 public:
233 // Return the audio hardware driver estimated latency in milliseconds.
234 status_t getLatency(uint32_t *latency) override;
235
236 // Use this method in situations where audio mixing is done in the hardware.
237 status_t setVolume(float left, float right) override;
238
239 // Selects the audio presentation (if available).
240 status_t selectPresentation(int presentationId, int programId) override;
241
242 // Write audio buffer to driver.
243 status_t write(const void *buffer, size_t bytes, size_t *written) override;
244
245 // Return the number of audio frames written by the audio dsp to DAC since
246 // the output has exited standby.
247 status_t getRenderPosition(uint32_t *dspFrames) override;
248
249 // Get the local time at which the next write to the audio driver will be presented.
250 status_t getNextWriteTimestamp(int64_t *timestamp) override;
251
252 // Set the callback for notifying completion of non-blocking write and drain.
253 status_t setCallback(wp<StreamOutHalInterfaceCallback> callback) override;
254
255 // Returns whether pause and resume operations are supported.
256 status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume) override;
257
258 // Notifies to the audio driver to resume playback following a pause.
259 status_t pause() override;
260
261 // Notifies to the audio driver to resume playback following a pause.
262 status_t resume() override;
263
264 // Returns whether drain operation is supported.
265 status_t supportsDrain(bool *supportsDrain) override;
266
267 // Requests notification when data buffered by the driver/hardware has been played.
268 status_t drain(bool earlyNotify) override;
269
270 // Notifies to the audio driver to flush the queued data.
271 status_t flush() override;
272
273 // Return a recent count of the number of audio frames presented to an external observer.
274 status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp) override;
275
276 // Called when the metadata of the stream's source has been changed.
277 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
278
279 // Returns the Dual Mono mode presentation setting.
280 status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override;
281
282 // Sets the Dual Mono mode presentation on the output device.
283 status_t setDualMonoMode(audio_dual_mono_mode_t mode) override;
284
285 // Returns the Audio Description Mix level in dB.
286 status_t getAudioDescriptionMixLevel(float* leveldB) override;
287
288 // Sets the Audio Description Mix level in dB.
289 status_t setAudioDescriptionMixLevel(float leveldB) override;
290
291 // Retrieves current playback rate parameters.
292 status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override;
293
294 // Sets the playback rate parameters that control playback behavior.
295 status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override;
296
297 status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override;
298
299 status_t setLatencyMode(audio_latency_mode_t mode) override;
300 status_t getRecommendedLatencyModes(std::vector<audio_latency_mode_t> *modes) override;
301 status_t setLatencyModeCallback(
302 const sp<StreamOutHalInterfaceLatencyModeCallback>& callback) override;
303
Mikhail Naganov31d46652023-01-10 18:29:25 +0000304 status_t exit() override;
305
Mikhail Naganov31d46652023-01-10 18:29:25 +0000306 private:
307 friend class sp<StreamOutHalAidl>;
308
Mikhail Naganov31d46652023-01-10 18:29:25 +0000309 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut> mStream;
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800310 const wp<CallbackBroker> mCallbackBroker;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000311
312 // Can not be constructed directly by clients.
313 StreamOutHalAidl(
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800314 const audio_config& config, StreamContextAidl&& context, int32_t nominalLatency,
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800315 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut>& stream,
316 const sp<CallbackBroker>& callbackBroker);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000317
Mikhail Naganovdfd594e2023-02-08 16:59:41 -0800318 ~StreamOutHalAidl() override;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000319};
320
321class StreamInHalAidl : public StreamInHalInterface, public StreamHalAidl {
322 public:
323 // Set the input gain for the audio driver.
324 status_t setGain(float gain) override;
325
326 // Read audio buffer in from driver.
327 status_t read(void *buffer, size_t bytes, size_t *read) override;
328
329 // Return the amount of input frames lost in the audio driver.
330 status_t getInputFramesLost(uint32_t *framesLost) override;
331
332 // Return a recent count of the number of audio frames received and
333 // the clock time associated with that frame count.
334 status_t getCapturePosition(int64_t *frames, int64_t *time) override;
335
336 // Get active microphones
Mikhail Naganov2a6a3012023-02-13 11:45:03 -0800337 status_t getActiveMicrophones(std::vector<media::MicrophoneInfoFw> *microphones) override;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000338
339 // Set microphone direction (for processing)
340 status_t setPreferredMicrophoneDirection(
341 audio_microphone_direction_t direction) override;
342
343 // Set microphone zoom (for processing)
344 status_t setPreferredMicrophoneFieldDimension(float zoom) override;
345
346 // Called when the metadata of the stream's sink has been changed.
347 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
348
349 private:
350 friend class sp<StreamInHalAidl>;
351
352 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn> mStream;
353
354 // Can not be constructed directly by clients.
355 StreamInHalAidl(
Mikhail Naganov89a9f742023-01-30 12:33:18 -0800356 const audio_config& config, StreamContextAidl&& context, int32_t nominalLatency,
Mikhail Naganov31d46652023-01-10 18:29:25 +0000357 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn>& stream);
358
359 ~StreamInHalAidl() override = default;
360};
361
362} // namespace android