blob: 97ad87dfc3d3d3e26b4ec8996b8b976a68ac5f8e [file] [log] [blame]
Kevin Rocard4bcd67f2018-02-28 14:33:38 -08001/*
2 * Copyright (C) 2016 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
Kevin Rocarddf9b4202018-05-10 19:56:08 -070017#ifndef ANDROID_HARDWARE_STREAM_HAL_HIDL_H
18#define ANDROID_HARDWARE_STREAM_HAL_HIDL_H
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080019
20#include <atomic>
21
Mikhail Naganov6718c392022-01-27 22:17:21 +000022#include PATH(android/hardware/audio/CORE_TYPES_FILE_VERSION/IStream.h)
Kevin Rocard95213bf2018-11-08 17:16:57 -080023#include PATH(android/hardware/audio/FILE_VERSION/IStreamIn.h)
24#include PATH(android/hardware/audio/FILE_VERSION/IStreamOut.h)
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080025#include <fmq/EventFlag.h>
26#include <fmq/MessageQueue.h>
Mikhail Naganov6718c392022-01-27 22:17:21 +000027#include <media/audiohal/EffectHalInterface.h>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080028#include <media/audiohal/StreamHalInterface.h>
Andy Hung638f45b2021-01-18 20:02:56 -080029#include <mediautils/Synchronization.h>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080030
31#include "ConversionHelperHidl.h"
32#include "StreamPowerLog.h"
33
Mikhail Naganov6718c392022-01-27 22:17:21 +000034using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStream;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080035using ::android::hardware::EventFlag;
36using ::android::hardware::MessageQueue;
37using ::android::hardware::Return;
Kevin Rocard070e7512018-05-22 09:29:13 -070038using ReadParameters = ::android::hardware::audio::CPP_VERSION::IStreamIn::ReadParameters;
39using ReadStatus = ::android::hardware::audio::CPP_VERSION::IStreamIn::ReadStatus;
40using WriteCommand = ::android::hardware::audio::CPP_VERSION::IStreamOut::WriteCommand;
41using WriteStatus = ::android::hardware::audio::CPP_VERSION::IStreamOut::WriteStatus;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080042
43namespace android {
44
45class DeviceHalHidl;
46
47class StreamHalHidl : public virtual StreamHalInterface, public ConversionHelperHidl
48{
49 public:
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080050 // Return size of input/output buffer in bytes for this stream - eg. 4800.
51 virtual status_t getBufferSize(size_t *size);
52
Mikhail Naganov560637e2021-03-31 22:40:13 +000053 // Return the base configuration of the stream:
54 // - channel mask;
55 // - format - e.g. AUDIO_FORMAT_PCM_16_BIT;
56 // - sampling rate in Hz - eg. 44100.
57 virtual status_t getAudioProperties(audio_config_base_t *configBase);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080058
59 // Set audio stream parameters.
60 virtual status_t setParameters(const String8& kvPairs);
61
62 // Get audio stream parameters.
63 virtual status_t getParameters(const String8& keys, String8 *values);
64
65 // Add or remove the effect on the stream.
66 virtual status_t addEffect(sp<EffectHalInterface> effect);
67 virtual status_t removeEffect(sp<EffectHalInterface> effect);
68
69 // Put the audio hardware input/output into standby mode.
70 virtual status_t standby();
71
Andy Hung61589a42021-06-16 09:37:53 -070072 virtual status_t dump(int fd, const Vector<String16>& args) override;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080073
74 // Start a stream operating in mmap mode.
75 virtual status_t start();
76
77 // Stop a stream operating in mmap mode.
78 virtual status_t stop();
79
80 // Retrieve information on the data buffer in mmap mode.
81 virtual status_t createMmapBuffer(int32_t minSizeFrames,
82 struct audio_mmap_buffer_info *info);
83
84 // Get current read/write position in the mmap buffer
85 virtual status_t getMmapPosition(struct audio_mmap_position *position);
86
87 // Set the priority of the thread that interacts with the HAL
88 // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
89 virtual status_t setHalThreadPriority(int priority);
90
91 protected:
92 // Subclasses can not be constructed directly by clients.
93 explicit StreamHalHidl(IStream *stream);
94
Andy Hungacb5b982021-01-20 10:12:00 -080095 ~StreamHalHidl() override;
96
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080097 status_t getCachedBufferSize(size_t *size);
98
Mikhail Naganov247b5f92021-01-15 19:16:12 +000099 status_t getHalPid(pid_t *pid);
100
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800101 bool requestHalThreadPriority(pid_t threadPid, pid_t threadId);
102
103 // mStreamPowerLog is used for audio signal power logging.
104 StreamPowerLog mStreamPowerLog;
105
106 private:
107 const int HAL_THREAD_PRIORITY_DEFAULT = -1;
Andy Hung638f45b2021-01-18 20:02:56 -0800108 IStream * const mStream;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800109 int mHalThreadPriority;
110 size_t mCachedBufferSize;
111};
112
113class StreamOutHalHidl : public StreamOutHalInterface, public StreamHalHidl {
114 public:
115 // Return the frame size (number of bytes per sample) of a stream.
116 virtual status_t getFrameSize(size_t *size);
117
118 // Return the audio hardware driver estimated latency in milliseconds.
119 virtual status_t getLatency(uint32_t *latency);
120
121 // Use this method in situations where audio mixing is done in the hardware.
122 virtual status_t setVolume(float left, float right);
123
Mikhail Naganovac917ac2018-11-28 14:03:52 -0800124 // Selects the audio presentation (if available).
125 virtual status_t selectPresentation(int presentationId, int programId);
126
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800127 // Write audio buffer to driver.
128 virtual status_t write(const void *buffer, size_t bytes, size_t *written);
129
130 // Return the number of audio frames written by the audio dsp to DAC since
131 // the output has exited standby.
132 virtual status_t getRenderPosition(uint32_t *dspFrames);
133
134 // Get the local time at which the next write to the audio driver will be presented.
135 virtual status_t getNextWriteTimestamp(int64_t *timestamp);
136
137 // Set the callback for notifying completion of non-blocking write and drain.
138 virtual status_t setCallback(wp<StreamOutHalInterfaceCallback> callback);
139
140 // Returns whether pause and resume operations are supported.
141 virtual status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume);
142
143 // Notifies to the audio driver to resume playback following a pause.
144 virtual status_t pause();
145
146 // Notifies to the audio driver to resume playback following a pause.
147 virtual status_t resume();
148
149 // Returns whether drain operation is supported.
150 virtual status_t supportsDrain(bool *supportsDrain);
151
152 // Requests notification when data buffered by the driver/hardware has been played.
153 virtual status_t drain(bool earlyNotify);
154
155 // Notifies to the audio driver to flush the queued data.
156 virtual status_t flush();
157
158 // Return a recent count of the number of audio frames presented to an external observer.
159 virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp);
160
Kevin Rocarda8975a72018-03-27 10:16:52 -0700161 // Called when the metadata of the stream's source has been changed.
162 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
163
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800164 // Methods used by StreamOutCallback (HIDL).
165 void onWriteReady();
166 void onDrainReady();
167 void onError();
168
Kuowei Lid4adbdb2020-08-13 14:44:25 +0800169 // Returns the Dual Mono mode presentation setting.
170 status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override;
171
172 // Sets the Dual Mono mode presentation on the output device.
173 status_t setDualMonoMode(audio_dual_mono_mode_t mode) override;
174
175 // Returns the Audio Description Mix level in dB.
176 status_t getAudioDescriptionMixLevel(float* leveldB) override;
177
178 // Sets the Audio Description Mix level in dB.
179 status_t setAudioDescriptionMixLevel(float leveldB) override;
180
181 // Retrieves current playback rate parameters.
182 status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override;
183
184 // Sets the playback rate parameters that control playback behavior.
185 status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override;
186
jiabinf6eb4c32020-02-25 14:06:25 -0800187 status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override;
188
189 // Methods used by StreamCodecFormatCallback (HIDL).
190 void onCodecFormatChanged(const std::basic_string<uint8_t>& metadataBs);
191
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800192 private:
193 friend class DeviceHalHidl;
194 typedef MessageQueue<WriteCommand, hardware::kSynchronizedReadWrite> CommandMQ;
195 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
196 typedef MessageQueue<WriteStatus, hardware::kSynchronizedReadWrite> StatusMQ;
197
Andy Hung638f45b2021-01-18 20:02:56 -0800198 mediautils::atomic_wp<StreamOutHalInterfaceCallback> mCallback;
199 mediautils::atomic_wp<StreamOutHalInterfaceEventCallback> mEventCallback;
Mikhail Naganov6718c392022-01-27 22:17:21 +0000200 const sp<::android::hardware::audio::CPP_VERSION::IStreamOut> mStream;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800201 std::unique_ptr<CommandMQ> mCommandMQ;
202 std::unique_ptr<DataMQ> mDataMQ;
203 std::unique_ptr<StatusMQ> mStatusMQ;
204 std::atomic<pid_t> mWriterClient;
205 EventFlag* mEfGroup;
206
207 // Can not be constructed directly by clients.
Mikhail Naganov6718c392022-01-27 22:17:21 +0000208 StreamOutHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IStreamOut>& stream);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800209
210 virtual ~StreamOutHalHidl();
211
212 using WriterCallback = std::function<void(const WriteStatus& writeStatus)>;
213 status_t callWriterThread(
214 WriteCommand cmd, const char* cmdName,
215 const uint8_t* data, size_t dataSize, WriterCallback callback);
216 status_t prepareForWriting(size_t bufferSize);
217};
218
219class StreamInHalHidl : public StreamInHalInterface, public StreamHalHidl {
220 public:
221 // Return the frame size (number of bytes per sample) of a stream.
222 virtual status_t getFrameSize(size_t *size);
223
224 // Set the input gain for the audio driver.
225 virtual status_t setGain(float gain);
226
227 // Read audio buffer in from driver.
228 virtual status_t read(void *buffer, size_t bytes, size_t *read);
229
230 // Return the amount of input frames lost in the audio driver.
231 virtual status_t getInputFramesLost(uint32_t *framesLost);
232
233 // Return a recent count of the number of audio frames received and
234 // the clock time associated with that frame count.
235 virtual status_t getCapturePosition(int64_t *frames, int64_t *time);
236
jiabin9ff780e2018-03-19 18:19:52 -0700237 // Get active microphones
238 virtual status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones);
239
Paul McLean03a6e6a2018-12-04 10:54:13 -0700240 // Set microphone direction (for processing)
Paul McLean12340082019-03-19 09:35:05 -0600241 virtual status_t setPreferredMicrophoneDirection(
242 audio_microphone_direction_t direction) override;
Paul McLean03a6e6a2018-12-04 10:54:13 -0700243
244 // Set microphone zoom (for processing)
Paul McLean12340082019-03-19 09:35:05 -0600245 virtual status_t setPreferredMicrophoneFieldDimension(float zoom) override;
Paul McLean03a6e6a2018-12-04 10:54:13 -0700246
Kevin Rocarda8975a72018-03-27 10:16:52 -0700247 // Called when the metadata of the stream's sink has been changed.
248 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
249
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800250 private:
251 friend class DeviceHalHidl;
252 typedef MessageQueue<ReadParameters, hardware::kSynchronizedReadWrite> CommandMQ;
253 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
254 typedef MessageQueue<ReadStatus, hardware::kSynchronizedReadWrite> StatusMQ;
255
Mikhail Naganov6718c392022-01-27 22:17:21 +0000256 const sp<::android::hardware::audio::CPP_VERSION::IStreamIn> mStream;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800257 std::unique_ptr<CommandMQ> mCommandMQ;
258 std::unique_ptr<DataMQ> mDataMQ;
259 std::unique_ptr<StatusMQ> mStatusMQ;
260 std::atomic<pid_t> mReaderClient;
261 EventFlag* mEfGroup;
262
263 // Can not be constructed directly by clients.
Mikhail Naganov6718c392022-01-27 22:17:21 +0000264 StreamInHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IStreamIn>& stream);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800265
266 virtual ~StreamInHalHidl();
267
268 using ReaderCallback = std::function<void(const ReadStatus& readStatus)>;
269 status_t callReaderThread(
270 const ReadParameters& params, const char* cmdName, ReaderCallback callback);
271 status_t prepareForReading(size_t bufferSize);
272};
273
274} // namespace android
275
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700276#endif // ANDROID_HARDWARE_STREAM_HAL_HIDL_H