blob: 2dda0f8dcdd48c021cba3db6dfcd82016539919a [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 Rocard51e076a2018-02-28 14:36:53 -080017#ifndef ANDROID_HARDWARE_STREAM_HAL_HIDL_4_0_H
18#define ANDROID_HARDWARE_STREAM_HAL_HIDL_4_0_H
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080019
20#include <atomic>
21
Kevin Rocard51e076a2018-02-28 14:36:53 -080022#include <android/hardware/audio/4.0/IStream.h>
23#include <android/hardware/audio/4.0/IStreamIn.h>
24#include <android/hardware/audio/4.0/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>
28
29#include "ConversionHelperHidl.h"
30#include "StreamPowerLog.h"
31
Kevin Rocard51e076a2018-02-28 14:36:53 -080032using ::android::hardware::audio::V4_0::IStream;
33using ::android::hardware::audio::V4_0::IStreamIn;
34using ::android::hardware::audio::V4_0::IStreamOut;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080035using ::android::hardware::EventFlag;
36using ::android::hardware::MessageQueue;
37using ::android::hardware::Return;
Kevin Rocard51e076a2018-02-28 14:36:53 -080038using ReadParameters = ::android::hardware::audio::V4_0::IStreamIn::ReadParameters;
39using ReadStatus = ::android::hardware::audio::V4_0::IStreamIn::ReadStatus;
40using WriteCommand = ::android::hardware::audio::V4_0::IStreamOut::WriteCommand;
41using WriteStatus = ::android::hardware::audio::V4_0::IStreamOut::WriteStatus;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080042
43namespace android {
Kevin Rocard51e076a2018-02-28 14:36:53 -080044namespace V4_0 {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080045
46class DeviceHalHidl;
47
48class StreamHalHidl : public virtual StreamHalInterface, public ConversionHelperHidl
49{
50 public:
51 // Return the sampling rate in Hz - eg. 44100.
52 virtual status_t getSampleRate(uint32_t *rate);
53
54 // Return size of input/output buffer in bytes for this stream - eg. 4800.
55 virtual status_t getBufferSize(size_t *size);
56
57 // Return the channel mask.
58 virtual status_t getChannelMask(audio_channel_mask_t *mask);
59
60 // Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT.
61 virtual status_t getFormat(audio_format_t *format);
62
63 // Convenience method.
64 virtual status_t getAudioProperties(
65 uint32_t *sampleRate, audio_channel_mask_t *mask, audio_format_t *format);
66
67 // Set audio stream parameters.
68 virtual status_t setParameters(const String8& kvPairs);
69
70 // Get audio stream parameters.
71 virtual status_t getParameters(const String8& keys, String8 *values);
72
73 // Add or remove the effect on the stream.
74 virtual status_t addEffect(sp<EffectHalInterface> effect);
75 virtual status_t removeEffect(sp<EffectHalInterface> effect);
76
77 // Put the audio hardware input/output into standby mode.
78 virtual status_t standby();
79
80 virtual status_t dump(int fd);
81
82 // Start a stream operating in mmap mode.
83 virtual status_t start();
84
85 // Stop a stream operating in mmap mode.
86 virtual status_t stop();
87
88 // Retrieve information on the data buffer in mmap mode.
89 virtual status_t createMmapBuffer(int32_t minSizeFrames,
90 struct audio_mmap_buffer_info *info);
91
92 // Get current read/write position in the mmap buffer
93 virtual status_t getMmapPosition(struct audio_mmap_position *position);
94
95 // Set the priority of the thread that interacts with the HAL
96 // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
97 virtual status_t setHalThreadPriority(int priority);
98
99 protected:
100 // Subclasses can not be constructed directly by clients.
101 explicit StreamHalHidl(IStream *stream);
102
103 // The destructor automatically closes the stream.
104 virtual ~StreamHalHidl();
105
106 status_t getCachedBufferSize(size_t *size);
107
108 bool requestHalThreadPriority(pid_t threadPid, pid_t threadId);
109
110 // mStreamPowerLog is used for audio signal power logging.
111 StreamPowerLog mStreamPowerLog;
112
113 private:
114 const int HAL_THREAD_PRIORITY_DEFAULT = -1;
115 IStream *mStream;
116 int mHalThreadPriority;
117 size_t mCachedBufferSize;
118};
119
120class StreamOutHalHidl : public StreamOutHalInterface, public StreamHalHidl {
121 public:
122 // Return the frame size (number of bytes per sample) of a stream.
123 virtual status_t getFrameSize(size_t *size);
124
125 // Return the audio hardware driver estimated latency in milliseconds.
126 virtual status_t getLatency(uint32_t *latency);
127
128 // Use this method in situations where audio mixing is done in the hardware.
129 virtual status_t setVolume(float left, float right);
130
131 // Write audio buffer to driver.
132 virtual status_t write(const void *buffer, size_t bytes, size_t *written);
133
134 // Return the number of audio frames written by the audio dsp to DAC since
135 // the output has exited standby.
136 virtual status_t getRenderPosition(uint32_t *dspFrames);
137
138 // Get the local time at which the next write to the audio driver will be presented.
139 virtual status_t getNextWriteTimestamp(int64_t *timestamp);
140
141 // Set the callback for notifying completion of non-blocking write and drain.
142 virtual status_t setCallback(wp<StreamOutHalInterfaceCallback> callback);
143
144 // Returns whether pause and resume operations are supported.
145 virtual status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume);
146
147 // Notifies to the audio driver to resume playback following a pause.
148 virtual status_t pause();
149
150 // Notifies to the audio driver to resume playback following a pause.
151 virtual status_t resume();
152
153 // Returns whether drain operation is supported.
154 virtual status_t supportsDrain(bool *supportsDrain);
155
156 // Requests notification when data buffered by the driver/hardware has been played.
157 virtual status_t drain(bool earlyNotify);
158
159 // Notifies to the audio driver to flush the queued data.
160 virtual status_t flush();
161
162 // Return a recent count of the number of audio frames presented to an external observer.
163 virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp);
164
Kevin Rocarda8975a72018-03-27 10:16:52 -0700165 // Called when the metadata of the stream's source has been changed.
166 status_t updateSourceMetadata(const SourceMetadata& sourceMetadata) override;
167
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800168 // Methods used by StreamOutCallback (HIDL).
169 void onWriteReady();
170 void onDrainReady();
171 void onError();
172
173 private:
174 friend class DeviceHalHidl;
175 typedef MessageQueue<WriteCommand, hardware::kSynchronizedReadWrite> CommandMQ;
176 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
177 typedef MessageQueue<WriteStatus, hardware::kSynchronizedReadWrite> StatusMQ;
178
179 wp<StreamOutHalInterfaceCallback> mCallback;
180 sp<IStreamOut> mStream;
181 std::unique_ptr<CommandMQ> mCommandMQ;
182 std::unique_ptr<DataMQ> mDataMQ;
183 std::unique_ptr<StatusMQ> mStatusMQ;
184 std::atomic<pid_t> mWriterClient;
185 EventFlag* mEfGroup;
186
187 // Can not be constructed directly by clients.
188 StreamOutHalHidl(const sp<IStreamOut>& stream);
189
190 virtual ~StreamOutHalHidl();
191
192 using WriterCallback = std::function<void(const WriteStatus& writeStatus)>;
193 status_t callWriterThread(
194 WriteCommand cmd, const char* cmdName,
195 const uint8_t* data, size_t dataSize, WriterCallback callback);
196 status_t prepareForWriting(size_t bufferSize);
197};
198
199class StreamInHalHidl : public StreamInHalInterface, public StreamHalHidl {
200 public:
201 // Return the frame size (number of bytes per sample) of a stream.
202 virtual status_t getFrameSize(size_t *size);
203
204 // Set the input gain for the audio driver.
205 virtual status_t setGain(float gain);
206
207 // Read audio buffer in from driver.
208 virtual status_t read(void *buffer, size_t bytes, size_t *read);
209
210 // Return the amount of input frames lost in the audio driver.
211 virtual status_t getInputFramesLost(uint32_t *framesLost);
212
213 // Return a recent count of the number of audio frames received and
214 // the clock time associated with that frame count.
215 virtual status_t getCapturePosition(int64_t *frames, int64_t *time);
216
jiabin9ff780e2018-03-19 18:19:52 -0700217 // Get active microphones
218 virtual status_t getActiveMicrophones(std::vector<media::MicrophoneInfo> *microphones);
219
Kevin Rocarda8975a72018-03-27 10:16:52 -0700220 // Called when the metadata of the stream's sink has been changed.
221 status_t updateSinkMetadata(const SinkMetadata& sinkMetadata) override;
222
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800223 private:
224 friend class DeviceHalHidl;
225 typedef MessageQueue<ReadParameters, hardware::kSynchronizedReadWrite> CommandMQ;
226 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
227 typedef MessageQueue<ReadStatus, hardware::kSynchronizedReadWrite> StatusMQ;
228
229 sp<IStreamIn> mStream;
230 std::unique_ptr<CommandMQ> mCommandMQ;
231 std::unique_ptr<DataMQ> mDataMQ;
232 std::unique_ptr<StatusMQ> mStatusMQ;
233 std::atomic<pid_t> mReaderClient;
234 EventFlag* mEfGroup;
235
236 // Can not be constructed directly by clients.
237 StreamInHalHidl(const sp<IStreamIn>& stream);
238
239 virtual ~StreamInHalHidl();
240
241 using ReaderCallback = std::function<void(const ReadStatus& readStatus)>;
242 status_t callReaderThread(
243 const ReadParameters& params, const char* cmdName, ReaderCallback callback);
244 status_t prepareForReading(size_t bufferSize);
245};
246
Kevin Rocard51e076a2018-02-28 14:36:53 -0800247} // namespace V4_0
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800248} // namespace android
249
Kevin Rocard51e076a2018-02-28 14:36:53 -0800250#endif // ANDROID_HARDWARE_STREAM_HAL_HIDL_4_0_H