blob: 86f48f367e71efd6687594cac2cafe6cc79589ce [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
19#include <memory>
20#include <string_view>
21
22#include <aidl/android/hardware/audio/core/BpStreamCommon.h>
23#include <aidl/android/hardware/audio/core/BpStreamIn.h>
24#include <aidl/android/hardware/audio/core/BpStreamOut.h>
25#include <fmq/AidlMessageQueue.h>
26#include <media/audiohal/EffectHalInterface.h>
27#include <media/audiohal/StreamHalInterface.h>
28#include <mediautils/Synchronization.h>
29
30#include "ConversionHelperAidl.h"
31#include "StreamPowerLog.h"
32
33namespace android {
34
35class DeviceHalAidl;
36
37class StreamHalAidl : public virtual StreamHalInterface, public ConversionHelperAidl {
38 public:
39 // Return size of input/output buffer in bytes for this stream - eg. 4800.
40 status_t getBufferSize(size_t *size) override;
41
42 // Return the base configuration of the stream:
43 // - channel mask;
44 // - format - e.g. AUDIO_FORMAT_PCM_16_BIT;
45 // - sampling rate in Hz - eg. 44100.
46 status_t getAudioProperties(audio_config_base_t *configBase) override;
47
48 // Set audio stream parameters.
49 status_t setParameters(const String8& kvPairs) override;
50
51 // Get audio stream parameters.
52 status_t getParameters(const String8& keys, String8 *values) override;
53
54 // Return the frame size (number of bytes per sample) of a stream.
55 status_t getFrameSize(size_t *size) override;
56
57 // Add or remove the effect on the stream.
58 status_t addEffect(sp<EffectHalInterface> effect) override;
59 status_t removeEffect(sp<EffectHalInterface> effect) override;
60
61 // Put the audio hardware input/output into standby mode.
62 status_t standby() override;
63
64 status_t dump(int fd, const Vector<String16>& args) override;
65
66 // Start a stream operating in mmap mode.
67 status_t start() override;
68
69 // Stop a stream operating in mmap mode.
70 status_t stop() override;
71
72 // Retrieve information on the data buffer in mmap mode.
73 status_t createMmapBuffer(int32_t minSizeFrames,
74 struct audio_mmap_buffer_info *info) override;
75
76 // Get current read/write position in the mmap buffer
77 status_t getMmapPosition(struct audio_mmap_position *position) override;
78
79 // Set the priority of the thread that interacts with the HAL
80 // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
81 status_t setHalThreadPriority(int priority) override;
82
83 status_t legacyCreateAudioPatch(const struct audio_port_config& port,
84 std::optional<audio_source_t> source,
85 audio_devices_t type) override;
86
87 status_t legacyReleaseAudioPatch() override;
88
89 protected:
90 typedef AidlMessageQueue<::aidl::android::hardware::audio::core::StreamDescriptor::Command,
91 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> CommandMQ;
92 typedef AidlMessageQueue<::aidl::android::hardware::audio::core::StreamDescriptor::Reply,
93 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> ReplyMQ;
94 typedef AidlMessageQueue<int8_t,
95 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> DataMQ;
96
97 // Subclasses can not be constructed directly by clients.
98 StreamHalAidl(std::string_view className,
99 bool isInput,
100 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor,
101 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon>& stream);
102
103 ~StreamHalAidl() override;
104
105 status_t getHalPid(pid_t *pid);
106
107 bool requestHalThreadPriority(pid_t threadPid, pid_t threadId);
108
109 const bool mIsInput;
110 const size_t mFrameSizeBytes;
111 const size_t mBufferSizeFrames;
112 const std::unique_ptr<CommandMQ> mCommandMQ;
113 const std::unique_ptr<ReplyMQ> mReplyMQ;
114 const std::unique_ptr<DataMQ> mDataMQ;
115 // mStreamPowerLog is used for audio signal power logging.
116 StreamPowerLog mStreamPowerLog;
117
118 private:
119 static std::unique_ptr<DataMQ> maybeCreateDataMQ(
120 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor) {
121 using Tag = ::aidl::android::hardware::audio::core::StreamDescriptor::AudioBuffer::Tag;
122 if (descriptor.audio.getTag() == Tag::fmq) {
123 return std::make_unique<DataMQ>(descriptor.audio.get<Tag::fmq>());
124 }
125 return nullptr;
126 }
127
128 const int HAL_THREAD_PRIORITY_DEFAULT = -1;
129 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> mStream;
130 int mHalThreadPriority = HAL_THREAD_PRIORITY_DEFAULT;
131};
132
133class StreamOutHalAidl : public StreamOutHalInterface, public StreamHalAidl {
134 public:
135 // Return the audio hardware driver estimated latency in milliseconds.
136 status_t getLatency(uint32_t *latency) override;
137
138 // Use this method in situations where audio mixing is done in the hardware.
139 status_t setVolume(float left, float right) override;
140
141 // Selects the audio presentation (if available).
142 status_t selectPresentation(int presentationId, int programId) override;
143
144 // Write audio buffer to driver.
145 status_t write(const void *buffer, size_t bytes, size_t *written) override;
146
147 // Return the number of audio frames written by the audio dsp to DAC since
148 // the output has exited standby.
149 status_t getRenderPosition(uint32_t *dspFrames) override;
150
151 // Get the local time at which the next write to the audio driver will be presented.
152 status_t getNextWriteTimestamp(int64_t *timestamp) override;
153
154 // Set the callback for notifying completion of non-blocking write and drain.
155 status_t setCallback(wp<StreamOutHalInterfaceCallback> callback) override;
156
157 // Returns whether pause and resume operations are supported.
158 status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume) override;
159
160 // Notifies to the audio driver to resume playback following a pause.
161 status_t pause() override;
162
163 // Notifies to the audio driver to resume playback following a pause.
164 status_t resume() override;
165
166 // Returns whether drain operation is supported.
167 status_t supportsDrain(bool *supportsDrain) override;
168
169 // Requests notification when data buffered by the driver/hardware has been played.
170 status_t drain(bool earlyNotify) override;
171
172 // Notifies to the audio driver to flush the queued data.
173 status_t flush() override;
174
175 // Return a recent count of the number of audio frames presented to an external observer.
176 status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp) override;
177
178 // Called when the metadata of the stream's source has been changed.
179 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
180
181 // Returns the Dual Mono mode presentation setting.
182 status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override;
183
184 // Sets the Dual Mono mode presentation on the output device.
185 status_t setDualMonoMode(audio_dual_mono_mode_t mode) override;
186
187 // Returns the Audio Description Mix level in dB.
188 status_t getAudioDescriptionMixLevel(float* leveldB) override;
189
190 // Sets the Audio Description Mix level in dB.
191 status_t setAudioDescriptionMixLevel(float leveldB) override;
192
193 // Retrieves current playback rate parameters.
194 status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override;
195
196 // Sets the playback rate parameters that control playback behavior.
197 status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override;
198
199 status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override;
200
201 status_t setLatencyMode(audio_latency_mode_t mode) override;
202 status_t getRecommendedLatencyModes(std::vector<audio_latency_mode_t> *modes) override;
203 status_t setLatencyModeCallback(
204 const sp<StreamOutHalInterfaceLatencyModeCallback>& callback) override;
205
206 void onRecommendedLatencyModeChanged(const std::vector<audio_latency_mode_t>& modes);
207
208 status_t exit() override;
209
210 void onCodecFormatChanged(const std::basic_string<uint8_t>& metadataBs);
211
212 // Methods used by StreamOutCallback ().
213 // FIXME: Consider the required visibility.
214 void onWriteReady();
215 void onDrainReady();
216 void onError();
217
218 private:
219 friend class sp<StreamOutHalAidl>;
220
221 mediautils::atomic_wp<StreamOutHalInterfaceCallback> mCallback;
222 mediautils::atomic_wp<StreamOutHalInterfaceEventCallback> mEventCallback;
223 mediautils::atomic_wp<StreamOutHalInterfaceLatencyModeCallback> mLatencyModeCallback;
224
225 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut> mStream;
226
227 // Can not be constructed directly by clients.
228 StreamOutHalAidl(
229 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor,
230 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut>& stream);
231
232 ~StreamOutHalAidl() override = default;
233};
234
235class StreamInHalAidl : public StreamInHalInterface, public StreamHalAidl {
236 public:
237 // Set the input gain for the audio driver.
238 status_t setGain(float gain) override;
239
240 // Read audio buffer in from driver.
241 status_t read(void *buffer, size_t bytes, size_t *read) override;
242
243 // Return the amount of input frames lost in the audio driver.
244 status_t getInputFramesLost(uint32_t *framesLost) override;
245
246 // Return a recent count of the number of audio frames received and
247 // the clock time associated with that frame count.
248 status_t getCapturePosition(int64_t *frames, int64_t *time) override;
249
250 // Get active microphones
251 status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones) override;
252
253 // Set microphone direction (for processing)
254 status_t setPreferredMicrophoneDirection(
255 audio_microphone_direction_t direction) override;
256
257 // Set microphone zoom (for processing)
258 status_t setPreferredMicrophoneFieldDimension(float zoom) override;
259
260 // Called when the metadata of the stream's sink has been changed.
261 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
262
263 private:
264 friend class sp<StreamInHalAidl>;
265
266 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn> mStream;
267
268 // Can not be constructed directly by clients.
269 StreamInHalAidl(
270 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor,
271 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn>& stream);
272
273 ~StreamInHalAidl() override = default;
274};
275
276} // namespace android