blob: c56d5e3b28ddf8f8a8b54999288a5435eeced8c5 [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
Mikhail Naganovfab697c2023-01-11 19:33:13 +000097 template<class T>
98 static std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> getStreamCommon(
99 const std::shared_ptr<T>& stream);
100
Mikhail Naganov31d46652023-01-10 18:29:25 +0000101 // Subclasses can not be constructed directly by clients.
102 StreamHalAidl(std::string_view className,
103 bool isInput,
104 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor,
105 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon>& stream);
106
107 ~StreamHalAidl() override;
108
109 status_t getHalPid(pid_t *pid);
110
111 bool requestHalThreadPriority(pid_t threadPid, pid_t threadId);
112
113 const bool mIsInput;
114 const size_t mFrameSizeBytes;
115 const size_t mBufferSizeFrames;
116 const std::unique_ptr<CommandMQ> mCommandMQ;
117 const std::unique_ptr<ReplyMQ> mReplyMQ;
118 const std::unique_ptr<DataMQ> mDataMQ;
119 // mStreamPowerLog is used for audio signal power logging.
120 StreamPowerLog mStreamPowerLog;
121
122 private:
123 static std::unique_ptr<DataMQ> maybeCreateDataMQ(
124 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor) {
125 using Tag = ::aidl::android::hardware::audio::core::StreamDescriptor::AudioBuffer::Tag;
126 if (descriptor.audio.getTag() == Tag::fmq) {
127 return std::make_unique<DataMQ>(descriptor.audio.get<Tag::fmq>());
128 }
129 return nullptr;
130 }
131
132 const int HAL_THREAD_PRIORITY_DEFAULT = -1;
133 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon> mStream;
134 int mHalThreadPriority = HAL_THREAD_PRIORITY_DEFAULT;
135};
136
137class StreamOutHalAidl : public StreamOutHalInterface, public StreamHalAidl {
138 public:
139 // Return the audio hardware driver estimated latency in milliseconds.
140 status_t getLatency(uint32_t *latency) override;
141
142 // Use this method in situations where audio mixing is done in the hardware.
143 status_t setVolume(float left, float right) override;
144
145 // Selects the audio presentation (if available).
146 status_t selectPresentation(int presentationId, int programId) override;
147
148 // Write audio buffer to driver.
149 status_t write(const void *buffer, size_t bytes, size_t *written) override;
150
151 // Return the number of audio frames written by the audio dsp to DAC since
152 // the output has exited standby.
153 status_t getRenderPosition(uint32_t *dspFrames) override;
154
155 // Get the local time at which the next write to the audio driver will be presented.
156 status_t getNextWriteTimestamp(int64_t *timestamp) override;
157
158 // Set the callback for notifying completion of non-blocking write and drain.
159 status_t setCallback(wp<StreamOutHalInterfaceCallback> callback) override;
160
161 // Returns whether pause and resume operations are supported.
162 status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume) override;
163
164 // Notifies to the audio driver to resume playback following a pause.
165 status_t pause() override;
166
167 // Notifies to the audio driver to resume playback following a pause.
168 status_t resume() override;
169
170 // Returns whether drain operation is supported.
171 status_t supportsDrain(bool *supportsDrain) override;
172
173 // Requests notification when data buffered by the driver/hardware has been played.
174 status_t drain(bool earlyNotify) override;
175
176 // Notifies to the audio driver to flush the queued data.
177 status_t flush() override;
178
179 // Return a recent count of the number of audio frames presented to an external observer.
180 status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp) override;
181
182 // Called when the metadata of the stream's source has been changed.
183 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
184
185 // Returns the Dual Mono mode presentation setting.
186 status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override;
187
188 // Sets the Dual Mono mode presentation on the output device.
189 status_t setDualMonoMode(audio_dual_mono_mode_t mode) override;
190
191 // Returns the Audio Description Mix level in dB.
192 status_t getAudioDescriptionMixLevel(float* leveldB) override;
193
194 // Sets the Audio Description Mix level in dB.
195 status_t setAudioDescriptionMixLevel(float leveldB) override;
196
197 // Retrieves current playback rate parameters.
198 status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override;
199
200 // Sets the playback rate parameters that control playback behavior.
201 status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override;
202
203 status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override;
204
205 status_t setLatencyMode(audio_latency_mode_t mode) override;
206 status_t getRecommendedLatencyModes(std::vector<audio_latency_mode_t> *modes) override;
207 status_t setLatencyModeCallback(
208 const sp<StreamOutHalInterfaceLatencyModeCallback>& callback) override;
209
210 void onRecommendedLatencyModeChanged(const std::vector<audio_latency_mode_t>& modes);
211
212 status_t exit() override;
213
214 void onCodecFormatChanged(const std::basic_string<uint8_t>& metadataBs);
215
216 // Methods used by StreamOutCallback ().
217 // FIXME: Consider the required visibility.
218 void onWriteReady();
219 void onDrainReady();
220 void onError();
221
222 private:
223 friend class sp<StreamOutHalAidl>;
224
225 mediautils::atomic_wp<StreamOutHalInterfaceCallback> mCallback;
226 mediautils::atomic_wp<StreamOutHalInterfaceEventCallback> mEventCallback;
227 mediautils::atomic_wp<StreamOutHalInterfaceLatencyModeCallback> mLatencyModeCallback;
228
229 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut> mStream;
230
231 // Can not be constructed directly by clients.
232 StreamOutHalAidl(
233 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor,
234 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamOut>& stream);
235
236 ~StreamOutHalAidl() override = default;
237};
238
239class StreamInHalAidl : public StreamInHalInterface, public StreamHalAidl {
240 public:
241 // Set the input gain for the audio driver.
242 status_t setGain(float gain) override;
243
244 // Read audio buffer in from driver.
245 status_t read(void *buffer, size_t bytes, size_t *read) override;
246
247 // Return the amount of input frames lost in the audio driver.
248 status_t getInputFramesLost(uint32_t *framesLost) override;
249
250 // Return a recent count of the number of audio frames received and
251 // the clock time associated with that frame count.
252 status_t getCapturePosition(int64_t *frames, int64_t *time) override;
253
254 // Get active microphones
255 status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones) override;
256
257 // Set microphone direction (for processing)
258 status_t setPreferredMicrophoneDirection(
259 audio_microphone_direction_t direction) override;
260
261 // Set microphone zoom (for processing)
262 status_t setPreferredMicrophoneFieldDimension(float zoom) override;
263
264 // Called when the metadata of the stream's sink has been changed.
265 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
266
267 private:
268 friend class sp<StreamInHalAidl>;
269
270 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn> mStream;
271
272 // Can not be constructed directly by clients.
273 StreamInHalAidl(
274 const ::aidl::android::hardware::audio::core::StreamDescriptor& descriptor,
275 const std::shared_ptr<::aidl::android::hardware::audio::core::IStreamIn>& stream);
276
277 ~StreamInHalAidl() override = default;
278};
279
280} // namespace android