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