1 /*
2 * Copyright (C) 2018 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_AUDIO_STREAM_H
18 #define ANDROID_HARDWARE_AUDIO_STREAM_H
19
20 #include PATH(android/hardware/audio/FILE_VERSION/IStream.h)
21
22 #include "ParametersUtil.h"
23
24 #include <vector>
25
26 #include <hardware/audio.h>
27 #include <hidl/Status.h>
28
29 #include <hidl/MQDescriptor.h>
30
31 #include <VersionUtils.h>
32
33 namespace android {
34 namespace hardware {
35 namespace audio {
36 namespace CPP_VERSION {
37 namespace implementation {
38
39 using ::android::sp;
40 using ::android::hardware::hidl_string;
41 using ::android::hardware::hidl_vec;
42 using ::android::hardware::Return;
43 using ::android::hardware::Void;
44 using ::android::hardware::audio::common::CPP_VERSION::implementation::AudioChannelBitfield;
45 using namespace ::android::hardware::audio::common::CPP_VERSION;
46 using namespace ::android::hardware::audio::CPP_VERSION;
47
48 struct Stream : public IStream, public ParametersUtil {
49 explicit Stream(audio_stream_t* stream);
50
51 /** 1GiB is the maximum buffer size the HAL client is allowed to request.
52 * This value has been chosen to be under SIZE_MAX and still big enough
53 * for all audio use case.
54 * Keep private for 2.0, put in .hal in 2.1
55 */
56 static constexpr uint32_t MAX_BUFFER_SIZE = 2 << 30 /* == 1GiB */;
57
58 // Methods from ::android::hardware::audio::CPP_VERSION::IStream follow.
59 Return<uint64_t> getFrameSize() override;
60 Return<uint64_t> getFrameCount() override;
61 Return<uint64_t> getBufferSize() override;
62 Return<uint32_t> getSampleRate() override;
63 #if MAJOR_VERSION == 2
64 Return<void> getSupportedSampleRates(getSupportedSampleRates_cb _hidl_cb) override;
65 Return<void> getSupportedChannelMasks(getSupportedChannelMasks_cb _hidl_cb) override;
66 #endif
67 Return<void> getSupportedSampleRates(AudioFormat format, getSupportedSampleRates_cb _hidl_cb);
68 Return<void> getSupportedChannelMasks(AudioFormat format, getSupportedChannelMasks_cb _hidl_cb);
69 Return<Result> setSampleRate(uint32_t sampleRateHz) override;
70 Return<AudioChannelBitfield> getChannelMask() override;
71 Return<Result> setChannelMask(AudioChannelBitfield mask) override;
72 Return<AudioFormat> getFormat() override;
73 Return<void> getSupportedFormats(getSupportedFormats_cb _hidl_cb) override;
74 Return<Result> setFormat(AudioFormat format) override;
75 Return<void> getAudioProperties(getAudioProperties_cb _hidl_cb) override;
76 Return<Result> addEffect(uint64_t effectId) override;
77 Return<Result> removeEffect(uint64_t effectId) override;
78 Return<Result> standby() override;
79 #if MAJOR_VERSION == 2
80 Return<AudioDevice> getDevice() override;
81 Return<Result> setDevice(const DeviceAddress& address) override;
82 Return<void> getParameters(const hidl_vec<hidl_string>& keys,
83 getParameters_cb _hidl_cb) override;
84 Return<Result> setParameters(const hidl_vec<ParameterValue>& parameters) override;
85 Return<Result> setConnectedState(const DeviceAddress& address, bool connected) override;
86 #elif MAJOR_VERSION >= 4
87 Return<void> getDevices(getDevices_cb _hidl_cb) override;
88 Return<Result> setDevices(const hidl_vec<DeviceAddress>& devices) override;
89 Return<void> getParameters(const hidl_vec<ParameterValue>& context,
90 const hidl_vec<hidl_string>& keys,
91 getParameters_cb _hidl_cb) override;
92 Return<Result> setParameters(const hidl_vec<ParameterValue>& context,
93 const hidl_vec<ParameterValue>& parameters) override;
94 #endif
95 Return<Result> setHwAvSync(uint32_t hwAvSync) override;
96 Return<Result> start() override;
97 Return<Result> stop() override;
98 Return<void> createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) override;
99 Return<void> getMmapPosition(getMmapPosition_cb _hidl_cb) override;
100 Return<Result> close() override;
101
102 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override;
103 #if MAJOR_VERSION == 2
104 Return<void> debugDump(const hidl_handle& fd) override;
105 #endif
106
107 // Utility methods for extending interfaces.
108 static Result analyzeStatus(const char* funcName, int status);
109 static Result analyzeStatus(const char* funcName, int status,
110 const std::vector<int>& ignoreErrors);
111
112 private:
113 audio_stream_t* mStream;
114
115 virtual ~Stream();
116
117 // Methods from ParametersUtil.
118 char* halGetParameters(const char* keys) override;
119 int halSetParameters(const char* keysAndValues) override;
120 };
121
122 template <typename T>
123 struct StreamMmap : public RefBase {
StreamMmapStreamMmap124 explicit StreamMmap(T* stream) : mStream(stream) {}
125
126 Return<Result> start();
127 Return<Result> stop();
128 Return<void> createMmapBuffer(int32_t minSizeFrames, size_t frameSize,
129 IStream::createMmapBuffer_cb _hidl_cb);
130 Return<void> getMmapPosition(IStream::getMmapPosition_cb _hidl_cb);
131
132 private:
StreamMmapStreamMmap133 StreamMmap() {}
134
135 T* mStream;
136 };
137
138 template <typename T>
start()139 Return<Result> StreamMmap<T>::start() {
140 if (mStream->start == NULL) return Result::NOT_SUPPORTED;
141 int result = mStream->start(mStream);
142 return Stream::analyzeStatus("start", result);
143 }
144
145 template <typename T>
stop()146 Return<Result> StreamMmap<T>::stop() {
147 if (mStream->stop == NULL) return Result::NOT_SUPPORTED;
148 int result = mStream->stop(mStream);
149 return Stream::analyzeStatus("stop", result);
150 }
151
152 template <typename T>
createMmapBuffer(int32_t minSizeFrames,size_t frameSize,IStream::createMmapBuffer_cb _hidl_cb)153 Return<void> StreamMmap<T>::createMmapBuffer(int32_t minSizeFrames, size_t frameSize,
154 IStream::createMmapBuffer_cb _hidl_cb) {
155 Result retval(Result::NOT_SUPPORTED);
156 MmapBufferInfo info;
157 native_handle_t* hidlHandle = nullptr;
158
159 if (mStream->create_mmap_buffer != NULL) {
160 if (minSizeFrames <= 0) {
161 retval = Result::INVALID_ARGUMENTS;
162 goto exit;
163 }
164 struct audio_mmap_buffer_info halInfo;
165 retval = Stream::analyzeStatus(
166 "create_mmap_buffer", mStream->create_mmap_buffer(mStream, minSizeFrames, &halInfo));
167 if (retval == Result::OK) {
168 hidlHandle = native_handle_create(1, 0);
169 hidlHandle->data[0] = halInfo.shared_memory_fd;
170
171 // Negative buffer size frame is a legacy hack to indicate that the buffer
172 // is shareable to applications before the relevant flag was introduced
173 bool applicationShareable =
174 halInfo.flags & AUDIO_MMAP_APPLICATION_SHAREABLE || halInfo.buffer_size_frames < 0;
175 halInfo.buffer_size_frames = abs(halInfo.buffer_size_frames);
176 info.sharedMemory = // hidl_memory size must always be positive
177 hidl_memory("audio_buffer", hidlHandle, frameSize * halInfo.buffer_size_frames);
178 #if MAJOR_VERSION == 2
179 if (applicationShareable) {
180 halInfo.buffer_size_frames *= -1;
181 }
182 #else
183 info.flags =
184 halInfo.flags | (applicationShareable ? MmapBufferFlag::APPLICATION_SHAREABLE
185 : MmapBufferFlag::NONE);
186 #endif
187 info.bufferSizeFrames = halInfo.buffer_size_frames;
188 info.burstSizeFrames = halInfo.burst_size_frames;
189 }
190 }
191 exit:
192 _hidl_cb(retval, info);
193 if (hidlHandle != nullptr) {
194 native_handle_delete(hidlHandle);
195 }
196 return Void();
197 }
198
199 template <typename T>
getMmapPosition(IStream::getMmapPosition_cb _hidl_cb)200 Return<void> StreamMmap<T>::getMmapPosition(IStream::getMmapPosition_cb _hidl_cb) {
201 Result retval(Result::NOT_SUPPORTED);
202 MmapPosition position;
203
204 if (mStream->get_mmap_position != NULL) {
205 struct audio_mmap_position halPosition;
206 retval = Stream::analyzeStatus("get_mmap_position",
207 mStream->get_mmap_position(mStream, &halPosition));
208 if (retval == Result::OK) {
209 position.timeNanoseconds = halPosition.time_nanoseconds;
210 position.positionFrames = halPosition.position_frames;
211 }
212 }
213 _hidl_cb(retval, position);
214 return Void();
215 }
216
217 } // namespace implementation
218 } // namespace CPP_VERSION
219 } // namespace audio
220 } // namespace hardware
221 } // namespace android
222
223 #endif // ANDROID_HARDWARE_AUDIO_STREAM_H
224