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