blob: a69cce8120584a5ca02af582b6275d937b98d897 [file] [log] [blame]
Mikhail Naganovf558e022016-11-14 17:45:17 -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
17#ifndef ANDROID_HARDWARE_STREAM_HAL_HIDL_H
18#define ANDROID_HARDWARE_STREAM_HAL_HIDL_H
19
Mikhail Naganovc8381902017-01-31 13:56:25 -080020#include <atomic>
21
Mikhail Naganovf558e022016-11-14 17:45:17 -080022#include <android/hardware/audio/2.0/IStream.h>
23#include <android/hardware/audio/2.0/IStreamIn.h>
24#include <android/hardware/audio/2.0/IStreamOut.h>
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -080025#include <fmq/EventFlag.h>
26#include <fmq/MessageQueue.h>
Mikhail Naganovf558e022016-11-14 17:45:17 -080027#include <media/audiohal/StreamHalInterface.h>
28
29#include "ConversionHelperHidl.h"
Andy Hung953608f2017-06-13 15:21:49 -070030#include "StreamPowerLog.h"
Mikhail Naganovf558e022016-11-14 17:45:17 -080031
32using ::android::hardware::audio::V2_0::IStream;
33using ::android::hardware::audio::V2_0::IStreamIn;
34using ::android::hardware::audio::V2_0::IStreamOut;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -080035using ::android::hardware::EventFlag;
36using ::android::hardware::MessageQueue;
Mikhail Naganovf558e022016-11-14 17:45:17 -080037using ::android::hardware::Return;
Mikhail Naganovc8381902017-01-31 13:56:25 -080038using ReadParameters = ::android::hardware::audio::V2_0::IStreamIn::ReadParameters;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -080039using ReadStatus = ::android::hardware::audio::V2_0::IStreamIn::ReadStatus;
Mikhail Naganovc8381902017-01-31 13:56:25 -080040using WriteCommand = ::android::hardware::audio::V2_0::IStreamOut::WriteCommand;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -080041using WriteStatus = ::android::hardware::audio::V2_0::IStreamOut::WriteStatus;
Mikhail Naganovf558e022016-11-14 17:45:17 -080042
43namespace android {
44
45class DeviceHalHidl;
46
47class StreamHalHidl : public virtual StreamHalInterface, public ConversionHelperHidl
48{
49 public:
50 // Return the sampling rate in Hz - eg. 44100.
51 virtual status_t getSampleRate(uint32_t *rate);
52
53 // Return size of input/output buffer in bytes for this stream - eg. 4800.
54 virtual status_t getBufferSize(size_t *size);
55
56 // Return the channel mask.
57 virtual status_t getChannelMask(audio_channel_mask_t *mask);
58
59 // Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT.
60 virtual status_t getFormat(audio_format_t *format);
61
62 // Convenience method.
63 virtual status_t getAudioProperties(
64 uint32_t *sampleRate, audio_channel_mask_t *mask, audio_format_t *format);
65
66 // Set audio stream parameters.
67 virtual status_t setParameters(const String8& kvPairs);
68
69 // Get audio stream parameters.
70 virtual status_t getParameters(const String8& keys, String8 *values);
71
72 // Add or remove the effect on the stream.
73 virtual status_t addEffect(sp<EffectHalInterface> effect);
74 virtual status_t removeEffect(sp<EffectHalInterface> effect);
75
76 // Put the audio hardware input/output into standby mode.
77 virtual status_t standby();
78
79 virtual status_t dump(int fd);
80
Eric Laurentaf35aad2016-12-15 14:25:36 -080081 // Start a stream operating in mmap mode.
82 virtual status_t start();
83
84 // Stop a stream operating in mmap mode.
85 virtual status_t stop();
86
87 // Retrieve information on the data buffer in mmap mode.
88 virtual status_t createMmapBuffer(int32_t minSizeFrames,
89 struct audio_mmap_buffer_info *info);
90
91 // Get current read/write position in the mmap buffer
92 virtual status_t getMmapPosition(struct audio_mmap_position *position);
93
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -080094 // Set the priority of the thread that interacts with the HAL
95 // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
96 virtual status_t setHalThreadPriority(int priority);
97
Mikhail Naganovf558e022016-11-14 17:45:17 -080098 protected:
99 // Subclasses can not be constructed directly by clients.
100 explicit StreamHalHidl(IStream *stream);
101
102 // The destructor automatically closes the stream.
103 virtual ~StreamHalHidl();
104
Mikhail Naganov83f04272017-02-07 10:45:09 -0800105 bool requestHalThreadPriority(pid_t threadPid, pid_t threadId);
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800106
Andy Hung953608f2017-06-13 15:21:49 -0700107 // mStreamPowerLog is used for audio signal power logging.
108 StreamPowerLog mStreamPowerLog;
109
Mikhail Naganovf558e022016-11-14 17:45:17 -0800110 private:
Mikhail Naganov83f04272017-02-07 10:45:09 -0800111 const int HAL_THREAD_PRIORITY_DEFAULT = -1;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800112 IStream *mStream;
Mikhail Naganov83f04272017-02-07 10:45:09 -0800113 int mHalThreadPriority;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800114};
115
116class StreamOutHalHidl : public StreamOutHalInterface, public StreamHalHidl {
117 public:
118 // Return the frame size (number of bytes per sample) of a stream.
119 virtual status_t getFrameSize(size_t *size);
120
121 // Return the audio hardware driver estimated latency in milliseconds.
122 virtual status_t getLatency(uint32_t *latency);
123
124 // Use this method in situations where audio mixing is done in the hardware.
125 virtual status_t setVolume(float left, float right);
126
127 // 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
161 // Methods used by StreamOutCallback (HIDL).
162 void onWriteReady();
163 void onDrainReady();
164 void onError();
165
166 private:
167 friend class DeviceHalHidl;
Mikhail Naganovc8381902017-01-31 13:56:25 -0800168 typedef MessageQueue<WriteCommand, hardware::kSynchronizedReadWrite> CommandMQ;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800169 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
170 typedef MessageQueue<WriteStatus, hardware::kSynchronizedReadWrite> StatusMQ;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800171
172 wp<StreamOutHalInterfaceCallback> mCallback;
173 sp<IStreamOut> mStream;
Mikhail Naganovc8381902017-01-31 13:56:25 -0800174 std::unique_ptr<CommandMQ> mCommandMQ;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800175 std::unique_ptr<DataMQ> mDataMQ;
176 std::unique_ptr<StatusMQ> mStatusMQ;
Mikhail Naganovc8381902017-01-31 13:56:25 -0800177 std::atomic<pid_t> mWriterClient;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800178 EventFlag* mEfGroup;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800179
180 // Can not be constructed directly by clients.
181 StreamOutHalHidl(const sp<IStreamOut>& stream);
182
183 virtual ~StreamOutHalHidl();
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800184
Mikhail Naganovc8381902017-01-31 13:56:25 -0800185 using WriterCallback = std::function<void(const WriteStatus& writeStatus)>;
186 status_t callWriterThread(
187 WriteCommand cmd, const char* cmdName,
188 const uint8_t* data, size_t dataSize, WriterCallback callback);
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800189 status_t prepareForWriting(size_t bufferSize);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800190};
191
192class StreamInHalHidl : public StreamInHalInterface, public StreamHalHidl {
193 public:
194 // Return the frame size (number of bytes per sample) of a stream.
195 virtual status_t getFrameSize(size_t *size);
196
197 // Set the input gain for the audio driver.
198 virtual status_t setGain(float gain);
199
200 // Read audio buffer in from driver.
201 virtual status_t read(void *buffer, size_t bytes, size_t *read);
202
203 // Return the amount of input frames lost in the audio driver.
204 virtual status_t getInputFramesLost(uint32_t *framesLost);
205
206 // Return a recent count of the number of audio frames received and
207 // the clock time associated with that frame count.
208 virtual status_t getCapturePosition(int64_t *frames, int64_t *time);
209
210 private:
211 friend class DeviceHalHidl;
Mikhail Naganovc8381902017-01-31 13:56:25 -0800212 typedef MessageQueue<ReadParameters, hardware::kSynchronizedReadWrite> CommandMQ;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800213 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
214 typedef MessageQueue<ReadStatus, hardware::kSynchronizedReadWrite> StatusMQ;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800215
216 sp<IStreamIn> mStream;
Mikhail Naganovc8381902017-01-31 13:56:25 -0800217 std::unique_ptr<CommandMQ> mCommandMQ;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800218 std::unique_ptr<DataMQ> mDataMQ;
219 std::unique_ptr<StatusMQ> mStatusMQ;
Mikhail Naganovc8381902017-01-31 13:56:25 -0800220 std::atomic<pid_t> mReaderClient;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800221 EventFlag* mEfGroup;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800222
223 // Can not be constructed directly by clients.
224 StreamInHalHidl(const sp<IStreamIn>& stream);
225
226 virtual ~StreamInHalHidl();
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800227
Mikhail Naganovc8381902017-01-31 13:56:25 -0800228 using ReaderCallback = std::function<void(const ReadStatus& readStatus)>;
229 status_t callReaderThread(
230 const ReadParameters& params, const char* cmdName, ReaderCallback callback);
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800231 status_t prepareForReading(size_t bufferSize);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800232};
233
234} // namespace android
235
236#endif // ANDROID_HARDWARE_STREAM_HAL_HIDL_H