blob: ce6c31c0a11114d6e8e153bc7c7ace2d924bb05d [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 Naganov8bd806e2023-01-30 12:33:18 -080019#include <atomic>
Mikhail Naganov31d46652023-01-10 18:29:25 +000020#include <memory>
Mikhail Naganov8bd806e2023-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>
30#include <mediautils/Synchronization.h>
31
32#include "ConversionHelperAidl.h"
33#include "StreamPowerLog.h"
34
35namespace android {
36
Mikhail Naganov8bd806e2023-01-30 12:33:18 -080037class StreamContextAidl {
38 public:
39 typedef AidlMessageQueue<::aidl::android::hardware::audio::core::StreamDescriptor::Command,
40 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> CommandMQ;
41 typedef AidlMessageQueue<::aidl::android::hardware::audio::core::StreamDescriptor::Reply,
42 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> ReplyMQ;
43 typedef AidlMessageQueue<int8_t,
44 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> DataMQ;
45
46 explicit StreamContextAidl(
47 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor)
48 : mFrameSizeBytes(descriptor.frameSizeBytes),
49 mCommandMQ(new CommandMQ(descriptor.command)),
50 mReplyMQ(new ReplyMQ(descriptor.reply)),
51 mBufferSizeFrames(descriptor.bufferSizeFrames),
52 mDataMQ(maybeCreateDataMQ(descriptor)) {}
53 StreamContextAidl(StreamContextAidl&& other) :
54 mFrameSizeBytes(other.mFrameSizeBytes),
55 mCommandMQ(std::move(other.mCommandMQ)),
56 mReplyMQ(std::move(other.mReplyMQ)),
57 mBufferSizeFrames(other.mBufferSizeFrames),
58 mDataMQ(std::move(other.mDataMQ)) {}
59 StreamContextAidl& operator=(StreamContextAidl&& other) {
60 mFrameSizeBytes = other.mFrameSizeBytes;
61 mCommandMQ = std::move(other.mCommandMQ);
62 mReplyMQ = std::move(other.mReplyMQ);
63 mBufferSizeFrames = other.mBufferSizeFrames;
64 mDataMQ = std::move(other.mDataMQ);
65 return *this;
66 }
67 bool isValid() const {
68 return mFrameSizeBytes != 0 &&
69 mCommandMQ != nullptr && mCommandMQ->isValid() &&
70 mReplyMQ != nullptr && mReplyMQ->isValid() &&
71 (mDataMQ != nullptr || (
72 mDataMQ->isValid() &&
73 mDataMQ->getQuantumCount() * mDataMQ->getQuantumSize() >=
74 mFrameSizeBytes * mBufferSizeFrames));
75 }
76 size_t getBufferSizeBytes() const { return mFrameSizeBytes * mBufferSizeFrames; }
77 size_t getBufferSizeFrames() const { return mBufferSizeFrames; }
78 CommandMQ* getCommandMQ() const { return mCommandMQ.get(); }
79 DataMQ* getDataMQ() const { return mDataMQ.get(); }
80 size_t getFrameSizeBytes() const { return mFrameSizeBytes; }
81 ReplyMQ* getReplyMQ() const { return mReplyMQ.get(); }
82
83 private:
84 static std::unique_ptr<DataMQ> maybeCreateDataMQ(
85 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor) {
86 using Tag = ::aidl::android::hardware::audio::core::StreamDescriptor::AudioBuffer::Tag;
87 if (descriptor.audio.getTag() == Tag::fmq) {
88 return std::make_unique<DataMQ>(descriptor.audio.get<Tag::fmq>());
89 }
90 return nullptr;
91 }
92
93 size_t mFrameSizeBytes;
94 std::unique_ptr<CommandMQ> mCommandMQ;
95 std::unique_ptr<ReplyMQ> mReplyMQ;
96 size_t mBufferSizeFrames;
97 std::unique_ptr<DataMQ> mDataMQ;
98};
Mikhail Naganov31d46652023-01-10 18:29:25 +000099
100class StreamHalAidl : public virtual StreamHalInterface, public ConversionHelperAidl {
101 public:
102 // Return size of input/output buffer in bytes for this stream - eg. 4800.
103 status_t getBufferSize(size_t *size) override;
104
105 // Return the base configuration of the stream:
106 // - channel mask;
107 // - format - e.g. AUDIO_FORMAT_PCM_16_BIT;
108 // - sampling rate in Hz - eg. 44100.
109 status_t getAudioProperties(audio_config_base_t *configBase) override;
110
111 // Set audio stream parameters.
112 status_t setParameters(const String8& kvPairs) override;
113
114 // Get audio stream parameters.
115 status_t getParameters(const String8& keys, String8 *values) override;
116
117 // Return the frame size (number of bytes per sample) of a stream.
118 status_t getFrameSize(size_t *size) override;
119
120 // Add or remove the effect on the stream.
121 status_t addEffect(sp<EffectHalInterface> effect) override;
122 status_t removeEffect(sp<EffectHalInterface> effect) override;
123
124 // Put the audio hardware input/output into standby mode.
125 status_t standby() override;
126
127 status_t dump(int fd, const Vector<String16>& args) override;
128
129 // Start a stream operating in mmap mode.
130 status_t start() override;
131
132 // Stop a stream operating in mmap mode.
133 status_t stop() override;
134
135 // Retrieve information on the data buffer in mmap mode.
136 status_t createMmapBuffer(int32_t minSizeFrames,
137 struct audio_mmap_buffer_info *info) override;
138
139 // Get current read/write position in the mmap buffer
140 status_t getMmapPosition(struct audio_mmap_position *position) override;
141
142 // Set the priority of the thread that interacts with the HAL
143 // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
144 status_t setHalThreadPriority(int priority) override;
145
146 status_t legacyCreateAudioPatch(const struct audio_port_config& port,
147 std::optional<audio_source_t> source,
148 audio_devices_t type) override;
149
150 status_t legacyReleaseAudioPatch() override;
151
152 protected:
Mikhail Naganovfab697c2023-01-11 19:33:13 +0000153 template<class T>
154 static std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> getStreamCommon(
155 const std::shared_ptr<T>& stream);
156
Mikhail Naganov31d46652023-01-10 18:29:25 +0000157 // Subclasses can not be constructed directly by clients.
158 StreamHalAidl(std::string_view className,
159 bool isInput,
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800160 const audio_config& config,
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800161 int32_t nominalLatency,
162 StreamContextAidl&& context,
Mikhail Naganov31d46652023-01-10 18:29:25 +0000163 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon>& stream);
164
165 ~StreamHalAidl() override;
166
167 status_t getHalPid(pid_t *pid);
168
169 bool requestHalThreadPriority(pid_t threadPid, pid_t threadId);
170
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800171 status_t getLatency(uint32_t *latency);
172
173 status_t getObservablePosition(int64_t *frames, int64_t *timestamp);
174
175 status_t getXruns(int32_t *frames);
176
177 status_t transfer(void *buffer, size_t bytes, size_t *transferred);
178
179 status_t pause(
180 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
181
182 status_t resume(
183 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
184
185 status_t drain(bool earlyNotify,
186 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
187
188 status_t flush(
189 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
190
191 status_t exit();
192
Mikhail Naganov31d46652023-01-10 18:29:25 +0000193 const bool mIsInput;
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800194 const audio_config_base_t mConfig;
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800195 const StreamContextAidl mContext;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000196
197 private:
Mikhail Naganovf56ce782023-01-25 11:29:11 -0800198 static audio_config_base_t configToBase(const audio_config& config) {
199 audio_config_base_t result = AUDIO_CONFIG_BASE_INITIALIZER;
200 result.sample_rate = config.sample_rate;
201 result.channel_mask = config.channel_mask;
202 result.format = config.format;
203 return result;
204 }
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800205 ::aidl::android::hardware::audio::core::StreamDescriptor::State getState() {
206 std::lock_guard l(mLock);
207 return mLastReply.state;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000208 }
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800209 status_t sendCommand(
210 const ::aidl::android::hardware::audio::core::StreamDescriptor::Command &command,
211 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr,
212 bool safeFromNonWorkerThread = false);
213 status_t updateCountersIfNeeded(
214 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply* reply = nullptr);
Mikhail Naganov31d46652023-01-10 18:29:25 +0000215
Mikhail Naganov31d46652023-01-10 18:29:25 +0000216 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> mStream;
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800217 std::mutex mLock;
218 ::aidl::android::hardware::audio::core::StreamDescriptor::Reply mLastReply GUARDED_BY(mLock);
219 // mStreamPowerLog is used for audio signal power logging.
220 StreamPowerLog mStreamPowerLog;
221 std::atomic<pid_t> mWorkerTid = -1;
Mikhail Naganov31d46652023-01-10 18:29:25 +0000222};
223
224class StreamOutHalAidl : public StreamOutHalInterface, public StreamHalAidl {
225 public:
226 // Return the audio hardware driver estimated latency in milliseconds.
227 status_t getLatency(uint32_t *latency) override;
228
229 // Use this method in situations where audio mixing is done in the hardware.
230 status_t setVolume(float left, float right) override;
231
232 // Selects the audio presentation (if available).
233 status_t selectPresentation(int presentationId, int programId) override;
234
235 // Write audio buffer to driver.
236 status_t write(const void *buffer, size_t bytes, size_t *written) override;
237
238 // Return the number of audio frames written by the audio dsp to DAC since
239 // the output has exited standby.
240 status_t getRenderPosition(uint32_t *dspFrames) override;
241
242 // Get the local time at which the next write to the audio driver will be presented.
243 status_t getNextWriteTimestamp(int64_t *timestamp) override;
244
245 // Set the callback for notifying completion of non-blocking write and drain.
246 status_t setCallback(wp<StreamOutHalInterfaceCallback> callback) override;
247
248 // Returns whether pause and resume operations are supported.
249 status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume) override;
250
251 // Notifies to the audio driver to resume playback following a pause.
252 status_t pause() override;
253
254 // Notifies to the audio driver to resume playback following a pause.
255 status_t resume() override;
256
257 // Returns whether drain operation is supported.
258 status_t supportsDrain(bool *supportsDrain) override;
259
260 // Requests notification when data buffered by the driver/hardware has been played.
261 status_t drain(bool earlyNotify) override;
262
263 // Notifies to the audio driver to flush the queued data.
264 status_t flush() override;
265
266 // Return a recent count of the number of audio frames presented to an external observer.
267 status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp) override;
268
269 // Called when the metadata of the stream's source has been changed.
270 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
271
272 // Returns the Dual Mono mode presentation setting.
273 status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override;
274
275 // Sets the Dual Mono mode presentation on the output device.
276 status_t setDualMonoMode(audio_dual_mono_mode_t mode) override;
277
278 // Returns the Audio Description Mix level in dB.
279 status_t getAudioDescriptionMixLevel(float* leveldB) override;
280
281 // Sets the Audio Description Mix level in dB.
282 status_t setAudioDescriptionMixLevel(float leveldB) override;
283
284 // Retrieves current playback rate parameters.
285 status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override;
286
287 // Sets the playback rate parameters that control playback behavior.
288 status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override;
289
290 status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override;
291
292 status_t setLatencyMode(audio_latency_mode_t mode) override;
293 status_t getRecommendedLatencyModes(std::vector<audio_latency_mode_t> *modes) override;
294 status_t setLatencyModeCallback(
295 const sp<StreamOutHalInterfaceLatencyModeCallback>& callback) override;
296
297 void onRecommendedLatencyModeChanged(const std::vector<audio_latency_mode_t>& modes);
298
299 status_t exit() override;
300
301 void onCodecFormatChanged(const std::basic_string<uint8_t>& metadataBs);
302
303 // Methods used by StreamOutCallback ().
304 // FIXME: Consider the required visibility.
305 void onWriteReady();
306 void onDrainReady();
307 void onError();
308
309 private:
310 friend class sp<StreamOutHalAidl>;
311
312 mediautils::atomic_wp<StreamOutHalInterfaceCallback> mCallback;
313 mediautils::atomic_wp<StreamOutHalInterfaceEventCallback> mEventCallback;
314 mediautils::atomic_wp<StreamOutHalInterfaceLatencyModeCallback> mLatencyModeCallback;
315
316 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut> mStream;
317
318 // Can not be constructed directly by clients.
319 StreamOutHalAidl(
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800320 const audio_config& config, StreamContextAidl&& context, int32_t nominalLatency,
Mikhail Naganov31d46652023-01-10 18:29:25 +0000321 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut>& stream);
322
323 ~StreamOutHalAidl() override = default;
324};
325
326class StreamInHalAidl : public StreamInHalInterface, public StreamHalAidl {
327 public:
328 // Set the input gain for the audio driver.
329 status_t setGain(float gain) override;
330
331 // Read audio buffer in from driver.
332 status_t read(void *buffer, size_t bytes, size_t *read) override;
333
334 // Return the amount of input frames lost in the audio driver.
335 status_t getInputFramesLost(uint32_t *framesLost) override;
336
337 // Return a recent count of the number of audio frames received and
338 // the clock time associated with that frame count.
339 status_t getCapturePosition(int64_t *frames, int64_t *time) override;
340
341 // Get active microphones
342 status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones) override;
343
344 // Set microphone direction (for processing)
345 status_t setPreferredMicrophoneDirection(
346 audio_microphone_direction_t direction) override;
347
348 // Set microphone zoom (for processing)
349 status_t setPreferredMicrophoneFieldDimension(float zoom) override;
350
351 // Called when the metadata of the stream's sink has been changed.
352 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
353
354 private:
355 friend class sp<StreamInHalAidl>;
356
357 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn> mStream;
358
359 // Can not be constructed directly by clients.
360 StreamInHalAidl(
Mikhail Naganov8bd806e2023-01-30 12:33:18 -0800361 const audio_config& config, StreamContextAidl&& context, int32_t nominalLatency,
Mikhail Naganov31d46652023-01-10 18:29:25 +0000362 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn>& stream);
363
364 ~StreamInHalAidl() override = default;
365};
366
367} // namespace android