blob: a7df276ee0bbed107fc74660345a26c4279f1f5e [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"
30
31using ::android::hardware::audio::V2_0::IStream;
32using ::android::hardware::audio::V2_0::IStreamIn;
33using ::android::hardware::audio::V2_0::IStreamOut;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -080034using ::android::hardware::EventFlag;
35using ::android::hardware::MessageQueue;
Mikhail Naganovf558e022016-11-14 17:45:17 -080036using ::android::hardware::Return;
Mikhail Naganovc8381902017-01-31 13:56:25 -080037using ReadParameters = ::android::hardware::audio::V2_0::IStreamIn::ReadParameters;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -080038using ReadStatus = ::android::hardware::audio::V2_0::IStreamIn::ReadStatus;
Mikhail Naganovc8381902017-01-31 13:56:25 -080039using WriteCommand = ::android::hardware::audio::V2_0::IStreamOut::WriteCommand;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -080040using WriteStatus = ::android::hardware::audio::V2_0::IStreamOut::WriteStatus;
Mikhail Naganovf558e022016-11-14 17:45:17 -080041
42namespace android {
43
44class DeviceHalHidl;
45
46class StreamHalHidl : public virtual StreamHalInterface, public ConversionHelperHidl
47{
48 public:
49 // Return the sampling rate in Hz - eg. 44100.
50 virtual status_t getSampleRate(uint32_t *rate);
51
52 // Return size of input/output buffer in bytes for this stream - eg. 4800.
53 virtual status_t getBufferSize(size_t *size);
54
55 // Return the channel mask.
56 virtual status_t getChannelMask(audio_channel_mask_t *mask);
57
58 // Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT.
59 virtual status_t getFormat(audio_format_t *format);
60
61 // Convenience method.
62 virtual status_t getAudioProperties(
63 uint32_t *sampleRate, audio_channel_mask_t *mask, audio_format_t *format);
64
65 // Set audio stream parameters.
66 virtual status_t setParameters(const String8& kvPairs);
67
68 // Get audio stream parameters.
69 virtual status_t getParameters(const String8& keys, String8 *values);
70
71 // Add or remove the effect on the stream.
72 virtual status_t addEffect(sp<EffectHalInterface> effect);
73 virtual status_t removeEffect(sp<EffectHalInterface> effect);
74
75 // Put the audio hardware input/output into standby mode.
76 virtual status_t standby();
77
78 virtual status_t dump(int fd);
79
Eric Laurentaf35aad2016-12-15 14:25:36 -080080 // Start a stream operating in mmap mode.
81 virtual status_t start();
82
83 // Stop a stream operating in mmap mode.
84 virtual status_t stop();
85
86 // Retrieve information on the data buffer in mmap mode.
87 virtual status_t createMmapBuffer(int32_t minSizeFrames,
88 struct audio_mmap_buffer_info *info);
89
90 // Get current read/write position in the mmap buffer
91 virtual status_t getMmapPosition(struct audio_mmap_position *position);
92
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -080093 // Set the priority of the thread that interacts with the HAL
94 // (must match the priority of the audioflinger's thread that calls 'read' / 'write')
95 virtual status_t setHalThreadPriority(int priority);
96
Mikhail Naganovf558e022016-11-14 17:45:17 -080097 protected:
98 // Subclasses can not be constructed directly by clients.
99 explicit StreamHalHidl(IStream *stream);
100
101 // The destructor automatically closes the stream.
102 virtual ~StreamHalHidl();
103
Mikhail Naganov83f04272017-02-07 10:45:09 -0800104 bool requestHalThreadPriority(pid_t threadPid, pid_t threadId);
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800105
Mikhail Naganovf558e022016-11-14 17:45:17 -0800106 private:
Mikhail Naganov83f04272017-02-07 10:45:09 -0800107 const int HAL_THREAD_PRIORITY_DEFAULT = -1;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800108 IStream *mStream;
Mikhail Naganov83f04272017-02-07 10:45:09 -0800109 int mHalThreadPriority;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800110};
111
112class StreamOutHalHidl : public StreamOutHalInterface, public StreamHalHidl {
113 public:
114 // Return the frame size (number of bytes per sample) of a stream.
115 virtual status_t getFrameSize(size_t *size);
116
117 // Return the audio hardware driver estimated latency in milliseconds.
118 virtual status_t getLatency(uint32_t *latency);
119
120 // Use this method in situations where audio mixing is done in the hardware.
121 virtual status_t setVolume(float left, float right);
122
123 // Write audio buffer to driver.
124 virtual status_t write(const void *buffer, size_t bytes, size_t *written);
125
126 // Return the number of audio frames written by the audio dsp to DAC since
127 // the output has exited standby.
128 virtual status_t getRenderPosition(uint32_t *dspFrames);
129
130 // Get the local time at which the next write to the audio driver will be presented.
131 virtual status_t getNextWriteTimestamp(int64_t *timestamp);
132
133 // Set the callback for notifying completion of non-blocking write and drain.
134 virtual status_t setCallback(wp<StreamOutHalInterfaceCallback> callback);
135
136 // Returns whether pause and resume operations are supported.
137 virtual status_t supportsPauseAndResume(bool *supportsPause, bool *supportsResume);
138
139 // Notifies to the audio driver to resume playback following a pause.
140 virtual status_t pause();
141
142 // Notifies to the audio driver to resume playback following a pause.
143 virtual status_t resume();
144
145 // Returns whether drain operation is supported.
146 virtual status_t supportsDrain(bool *supportsDrain);
147
148 // Requests notification when data buffered by the driver/hardware has been played.
149 virtual status_t drain(bool earlyNotify);
150
151 // Notifies to the audio driver to flush the queued data.
152 virtual status_t flush();
153
154 // Return a recent count of the number of audio frames presented to an external observer.
155 virtual status_t getPresentationPosition(uint64_t *frames, struct timespec *timestamp);
156
157 // Methods used by StreamOutCallback (HIDL).
158 void onWriteReady();
159 void onDrainReady();
160 void onError();
161
162 private:
163 friend class DeviceHalHidl;
Mikhail Naganovc8381902017-01-31 13:56:25 -0800164 typedef MessageQueue<WriteCommand, hardware::kSynchronizedReadWrite> CommandMQ;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800165 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
166 typedef MessageQueue<WriteStatus, hardware::kSynchronizedReadWrite> StatusMQ;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800167
168 wp<StreamOutHalInterfaceCallback> mCallback;
169 sp<IStreamOut> mStream;
Mikhail Naganovc8381902017-01-31 13:56:25 -0800170 std::unique_ptr<CommandMQ> mCommandMQ;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800171 std::unique_ptr<DataMQ> mDataMQ;
172 std::unique_ptr<StatusMQ> mStatusMQ;
Mikhail Naganovc8381902017-01-31 13:56:25 -0800173 std::atomic<pid_t> mWriterClient;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800174 EventFlag* mEfGroup;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800175
176 // Can not be constructed directly by clients.
177 StreamOutHalHidl(const sp<IStreamOut>& stream);
178
179 virtual ~StreamOutHalHidl();
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800180
Mikhail Naganovc8381902017-01-31 13:56:25 -0800181 using WriterCallback = std::function<void(const WriteStatus& writeStatus)>;
182 status_t callWriterThread(
183 WriteCommand cmd, const char* cmdName,
184 const uint8_t* data, size_t dataSize, WriterCallback callback);
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800185 status_t prepareForWriting(size_t bufferSize);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800186};
187
188class StreamInHalHidl : public StreamInHalInterface, public StreamHalHidl {
189 public:
190 // Return the frame size (number of bytes per sample) of a stream.
191 virtual status_t getFrameSize(size_t *size);
192
193 // Set the input gain for the audio driver.
194 virtual status_t setGain(float gain);
195
196 // Read audio buffer in from driver.
197 virtual status_t read(void *buffer, size_t bytes, size_t *read);
198
199 // Return the amount of input frames lost in the audio driver.
200 virtual status_t getInputFramesLost(uint32_t *framesLost);
201
202 // Return a recent count of the number of audio frames received and
203 // the clock time associated with that frame count.
204 virtual status_t getCapturePosition(int64_t *frames, int64_t *time);
205
206 private:
207 friend class DeviceHalHidl;
Mikhail Naganovc8381902017-01-31 13:56:25 -0800208 typedef MessageQueue<ReadParameters, hardware::kSynchronizedReadWrite> CommandMQ;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800209 typedef MessageQueue<uint8_t, hardware::kSynchronizedReadWrite> DataMQ;
210 typedef MessageQueue<ReadStatus, hardware::kSynchronizedReadWrite> StatusMQ;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800211
212 sp<IStreamIn> mStream;
Mikhail Naganovc8381902017-01-31 13:56:25 -0800213 std::unique_ptr<CommandMQ> mCommandMQ;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800214 std::unique_ptr<DataMQ> mDataMQ;
215 std::unique_ptr<StatusMQ> mStatusMQ;
Mikhail Naganovc8381902017-01-31 13:56:25 -0800216 std::atomic<pid_t> mReaderClient;
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800217 EventFlag* mEfGroup;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800218
219 // Can not be constructed directly by clients.
220 StreamInHalHidl(const sp<IStreamIn>& stream);
221
222 virtual ~StreamInHalHidl();
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800223
Mikhail Naganovc8381902017-01-31 13:56:25 -0800224 using ReaderCallback = std::function<void(const ReadStatus& readStatus)>;
225 status_t callReaderThread(
226 const ReadParameters& params, const char* cmdName, ReaderCallback callback);
Mikhail Naganove1c4b5d2016-12-22 09:22:45 -0800227 status_t prepareForReading(size_t bufferSize);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800228};
229
230} // namespace android
231
232#endif // ANDROID_HARDWARE_STREAM_HAL_HIDL_H