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 struct audio_mmap_buffer_info halInfo;
161 retval = Stream::analyzeStatus(
162 "create_mmap_buffer", mStream->create_mmap_buffer(mStream, minSizeFrames, &halInfo));
163 if (retval == Result::OK) {
164 hidlHandle = native_handle_create(1, 0);
165 hidlHandle->data[0] = halInfo.shared_memory_fd;
166
167 // Negative buffer size frame is a legacy hack to indicate that the buffer
168 // is shareable to applications before the relevant flag was introduced
169 bool applicationShareable =
170 halInfo.flags & AUDIO_MMAP_APPLICATION_SHAREABLE || halInfo.buffer_size_frames < 0;
171 halInfo.buffer_size_frames = abs(halInfo.buffer_size_frames);
172 info.sharedMemory = // hidl_memory size must always be positive
173 hidl_memory("audio_buffer", hidlHandle, frameSize * halInfo.buffer_size_frames);
174 #if MAJOR_VERSION == 2
175 if (applicationShareable) {
176 halInfo.buffer_size_frames *= -1;
177 }
178 #else
179 info.flags =
180 halInfo.flags | (applicationShareable ? MmapBufferFlag::APPLICATION_SHAREABLE
181 : MmapBufferFlag::NONE);
182 #endif
183 info.bufferSizeFrames = halInfo.buffer_size_frames;
184 info.burstSizeFrames = halInfo.burst_size_frames;
185 }
186 }
187 _hidl_cb(retval, info);
188 if (hidlHandle != nullptr) {
189 native_handle_delete(hidlHandle);
190 }
191 return Void();
192 }
193
194 template <typename T>
getMmapPosition(IStream::getMmapPosition_cb _hidl_cb)195 Return<void> StreamMmap<T>::getMmapPosition(IStream::getMmapPosition_cb _hidl_cb) {
196 Result retval(Result::NOT_SUPPORTED);
197 MmapPosition position;
198
199 if (mStream->get_mmap_position != NULL) {
200 struct audio_mmap_position halPosition;
201 retval = Stream::analyzeStatus("get_mmap_position",
202 mStream->get_mmap_position(mStream, &halPosition));
203 if (retval == Result::OK) {
204 position.timeNanoseconds = halPosition.time_nanoseconds;
205 position.positionFrames = halPosition.position_frames;
206 }
207 }
208 _hidl_cb(retval, position);
209 return Void();
210 }
211
212 } // namespace implementation
213 } // namespace CPP_VERSION
214 } // namespace audio
215 } // namespace hardware
216 } // namespace android
217
218 #endif // ANDROID_HARDWARE_AUDIO_STREAM_H
219