blob: 970903b1ce7a22827b3ec99d151384bff1c7fd28 [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
Kevin Rocard95213bf2018-11-08 17:16:57 -080022#include PATH(android/hardware/audio/FILE_VERSION/IStream.h)
23#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>
27#include <media/audiohal/StreamHalInterface.h>
Andy Hung638f45b2021-01-18 20:02:56 -080028#include <mediautils/Synchronization.h>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080029
30#include "ConversionHelperHidl.h"
31#include "StreamPowerLog.h"
32
Kevin Rocard070e7512018-05-22 09:29:13 -070033using ::android::hardware::audio::CPP_VERSION::IStream;
34using ::android::hardware::audio::CPP_VERSION::IStreamIn;
35using ::android::hardware::audio::CPP_VERSION::IStreamOut;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080036using ::android::hardware::EventFlag;
37using ::android::hardware::MessageQueue;
38using ::android::hardware::Return;
Kevin Rocard070e7512018-05-22 09:29:13 -070039using ReadParameters = ::android::hardware::audio::CPP_VERSION::IStreamIn::ReadParameters;
40using ReadStatus = ::android::hardware::audio::CPP_VERSION::IStreamIn::ReadStatus;
41using WriteCommand = ::android::hardware::audio::CPP_VERSION::IStreamOut::WriteCommand;
42using WriteStatus = ::android::hardware::audio::CPP_VERSION::IStreamOut::WriteStatus;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080043
44namespace android {
Kevin Rocard070e7512018-05-22 09:29:13 -070045namespace CPP_VERSION {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080046
47class DeviceHalHidl;
48
49class StreamHalHidl : public virtual StreamHalInterface, public ConversionHelperHidl
50{
51 public:
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080052 // Return size of input/output buffer in bytes for this stream - eg. 4800.
53 virtual status_t getBufferSize(size_t *size);
54
Mikhail Naganov560637e2021-03-31 22:40:13 +000055 // Return the base configuration of the stream:
56 // - channel mask;
57 // - format - e.g. AUDIO_FORMAT_PCM_16_BIT;
58 // - sampling rate in Hz - eg. 44100.
59 virtual status_t getAudioProperties(audio_config_base_t *configBase);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080060
61 // Set audio stream parameters.
62 virtual status_t setParameters(const String8& kvPairs);
63
64 // Get audio stream parameters.
65 virtual status_t getParameters(const String8& keys, String8 *values);
66
67 // Add or remove the effect on the stream.
68 virtual status_t addEffect(sp<EffectHalInterface> effect);
69 virtual status_t removeEffect(sp<EffectHalInterface> effect);
70
71 // Put the audio hardware input/output into standby mode.
72 virtual status_t standby();
73
74 virtual status_t dump(int fd);
75
76 // Start a stream operating in mmap mode.
77 virtual status_t start();
78
79 // Stop a stream operating in mmap mode.
80 virtual status_t stop();
81
82 // Retrieve information on the data buffer in mmap mode.
83 virtual status_t createMmapBuffer(int32_t minSizeFrames,
84 struct audio_mmap_buffer_info *info);
85
86 // Get current read/write position in the mmap buffer
87 virtual status_t getMmapPosition(struct audio_mmap_position *position);
88
89 // Set the priority of the thread that interacts with the HAL
90 // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
91 virtual status_t setHalThreadPriority(int priority);
92
93 protected:
94 // Subclasses can not be constructed directly by clients.
95 explicit StreamHalHidl(IStream *stream);
96
Andy Hungacb5b982021-01-20 10:12:00 -080097 ~StreamHalHidl() override;
98
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080099 status_t getCachedBufferSize(size_t *size);
100
Mikhail Naganov247b5f92021-01-15 19:16:12 +0000101 status_t getHalPid(pid_t *pid);
102
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800103 bool requestHalThreadPriority(pid_t threadPid, pid_t threadId);
104
105 // mStreamPowerLog is used for audio signal power logging.
106 StreamPowerLog mStreamPowerLog;
107
108 private:
109 const int HAL_THREAD_PRIORITY_DEFAULT = -1;
Andy Hung638f45b2021-01-18 20:02:56 -0800110 IStream * const mStream;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800111 int mHalThreadPriority;
112 size_t mCachedBufferSize;
113};
114
115class StreamOutHalHidl : public StreamOutHalInterface, public StreamHalHidl {
116 public:
117 // Return the frame size (number of bytes per sample) of a stream.
118 virtual status_t getFrameSize(size_t *size);
119
120 // Return the audio hardware driver estimated latency in milliseconds.
121 virtual status_t getLatency(uint32_t *latency);
122
123 // Use this method in situations where audio mixing is done in the hardware.
124 virtual status_t setVolume(float left, float right);
125
Mikhail Naganovac917ac2018-11-28 14:03:52 -0800126 // Selects the audio presentation (if available).
127 virtual status_t selectPresentation(int presentationId, int programId);
128
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800129 // Write audio buffer to driver.
130 virtual status_t write(const void *buffer, size_t bytes, size_t *written);
131
132 // Return the number of audio frames written by the audio dsp to DAC since
133 // the output has exited standby.
134 virtual status_t getRenderPosition(uint32_t *dspFrames);
135
136 // Get the local time at which the next write to the audio driver will be presented.
137 virtual status_t getNextWriteTimestamp(int64_t *timestamp);
138
139 // Set the callback for notifying completion of non-blocking write and drain.
140 virtual status_t setCallback(wp<StreamOutHalInterfaceCallback> callback);
141
142 // Returns whether pause and resume operations are supported.
143 virtual status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume);
144
145 // Notifies to the audio driver to resume playback following a pause.
146 virtual status_t pause();
147
148 // Notifies to the audio driver to resume playback following a pause.
149 virtual status_t resume();
150
151 // Returns whether drain operation is supported.
152 virtual status_t supportsDrain(bool *supportsDrain);
153
154 // Requests notification when data buffered by the driver/hardware has been played.
155 virtual status_t drain(bool earlyNotify);
156
157 // Notifies to the audio driver to flush the queued data.
158 virtual status_t flush();
159
160 // Return a recent count of the number of audio frames presented to an external observer.
161 virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp);
162
Kevin Rocarda8975a72018-03-27 10:16:52 -0700163 // Called when the metadata of the stream's source has been changed.
164 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
165
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800166 // Methods used by StreamOutCallback (HIDL).
167 void onWriteReady();
168 void onDrainReady();
169 void onError();
170
Kuowei Lid4adbdb2020-08-13 14:44:25 +0800171 // Returns the Dual Mono mode presentation setting.
172 status_t getDualMonoMode(audio_dual_mono_mode_t* mode) override;
173
174 // Sets the Dual Mono mode presentation on the output device.
175 status_t setDualMonoMode(audio_dual_mono_mode_t mode) override;
176
177 // Returns the Audio Description Mix level in dB.
178 status_t getAudioDescriptionMixLevel(float* leveldB) override;
179
180 // Sets the Audio Description Mix level in dB.
181 status_t setAudioDescriptionMixLevel(float leveldB) override;
182
183 // Retrieves current playback rate parameters.
184 status_t getPlaybackRateParameters(audio_playback_rate_t* playbackRate) override;
185
186 // Sets the playback rate parameters that control playback behavior.
187 status_t setPlaybackRateParameters(const audio_playback_rate_t& playbackRate) override;
188
jiabinf6eb4c32020-02-25 14:06:25 -0800189 status_t setEventCallback(const sp<StreamOutHalInterfaceEventCallback>& callback) override;
190
191 // Methods used by StreamCodecFormatCallback (HIDL).
192 void onCodecFormatChanged(const std::basic_string<uint8_t>& metadataBs);
193
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800194 private:
195 friend class DeviceHalHidl;
196 typedef MessageQueue<WriteCommand, hardware::kSynchronizedReadWrite> CommandMQ;
197 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
198 typedef MessageQueue<WriteStatus, hardware::kSynchronizedReadWrite> StatusMQ;
199
Andy Hung638f45b2021-01-18 20:02:56 -0800200 mediautils::atomic_wp<StreamOutHalInterfaceCallback> mCallback;
201 mediautils::atomic_wp<StreamOutHalInterfaceEventCallback> mEventCallback;
202 const sp<IStreamOut> mStream;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800203 std::unique_ptr<CommandMQ> mCommandMQ;
204 std::unique_ptr<DataMQ> mDataMQ;
205 std::unique_ptr<StatusMQ> mStatusMQ;
206 std::atomic<pid_t> mWriterClient;
207 EventFlag* mEfGroup;
208
209 // Can not be constructed directly by clients.
210 StreamOutHalHidl(const sp<IStreamOut>& stream);
211
212 virtual ~StreamOutHalHidl();
213
214 using WriterCallback = std::function<void(const WriteStatus& writeStatus)>;
215 status_t callWriterThread(
216 WriteCommand cmd, const char* cmdName,
217 const uint8_t* data, size_t dataSize, WriterCallback callback);
218 status_t prepareForWriting(size_t bufferSize);
219};
220
221class StreamInHalHidl : public StreamInHalInterface, public StreamHalHidl {
222 public:
223 // Return the frame size (number of bytes per sample) of a stream.
224 virtual status_t getFrameSize(size_t *size);
225
226 // Set the input gain for the audio driver.
227 virtual status_t setGain(float gain);
228
229 // Read audio buffer in from driver.
230 virtual status_t read(void *buffer, size_t bytes, size_t *read);
231
232 // Return the amount of input frames lost in the audio driver.
233 virtual status_t getInputFramesLost(uint32_t *framesLost);
234
235 // Return a recent count of the number of audio frames received and
236 // the clock time associated with that frame count.
237 virtual status_t getCapturePosition(int64_t *frames, int64_t *time);
238
jiabin9ff780e2018-03-19 18:19:52 -0700239 // Get active microphones
240 virtual status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones);
241
Paul McLean03a6e6a2018-12-04 10:54:13 -0700242 // Set microphone direction (for processing)
Paul McLean12340082019-03-19 09:35:05 -0600243 virtual status_t setPreferredMicrophoneDirection(
244 audio_microphone_direction_t direction) override;
Paul McLean03a6e6a2018-12-04 10:54:13 -0700245
246 // Set microphone zoom (for processing)
Paul McLean12340082019-03-19 09:35:05 -0600247 virtual status_t setPreferredMicrophoneFieldDimension(float zoom) override;
Paul McLean03a6e6a2018-12-04 10:54:13 -0700248
Kevin Rocarda8975a72018-03-27 10:16:52 -0700249 // Called when the metadata of the stream's sink has been changed.
250 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
251
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800252 private:
253 friend class DeviceHalHidl;
254 typedef MessageQueue<ReadParameters, hardware::kSynchronizedReadWrite> CommandMQ;
255 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
256 typedef MessageQueue<ReadStatus, hardware::kSynchronizedReadWrite> StatusMQ;
257
Andy Hung638f45b2021-01-18 20:02:56 -0800258 const sp<IStreamIn> mStream;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800259 std::unique_ptr<CommandMQ> mCommandMQ;
260 std::unique_ptr<DataMQ> mDataMQ;
261 std::unique_ptr<StatusMQ> mStatusMQ;
262 std::atomic<pid_t> mReaderClient;
263 EventFlag* mEfGroup;
264
265 // Can not be constructed directly by clients.
266 StreamInHalHidl(const sp<IStreamIn>& stream);
267
268 virtual ~StreamInHalHidl();
269
270 using ReaderCallback = std::function<void(const ReadStatus& readStatus)>;
271 status_t callReaderThread(
272 const ReadParameters& params, const char* cmdName, ReaderCallback callback);
273 status_t prepareForReading(size_t bufferSize);
274};
275
Kevin Rocard070e7512018-05-22 09:29:13 -0700276} // namespace CPP_VERSION
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800277} // namespace android
278
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700279#endif // ANDROID_HARDWARE_STREAM_HAL_HIDL_H