1 /*
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_AUDIO_V2_0_STREAM_H
18 #define ANDROID_HARDWARE_AUDIO_V2_0_STREAM_H
19 
20 #include <vector>
21 
22 #include <android/hardware/audio/2.0/IStream.h>
23 #include <hardware/audio.h>
24 #include <hidl/Status.h>
25 
26 #include <hidl/MQDescriptor.h>
27 
28 #include "ParametersUtil.h"
29 
30 namespace android {
31 namespace hardware {
32 namespace audio {
33 namespace V2_0 {
34 namespace implementation {
35 
36 using ::android::hardware::audio::common::V2_0::AudioChannelMask;
37 using ::android::hardware::audio::common::V2_0::AudioDevice;
38 using ::android::hardware::audio::common::V2_0::AudioFormat;
39 using ::android::hardware::audio::V2_0::DeviceAddress;
40 using ::android::hardware::audio::V2_0::IStream;
41 using ::android::hardware::audio::V2_0::ParameterValue;
42 using ::android::hardware::audio::V2_0::Result;
43 using ::android::hardware::Return;
44 using ::android::hardware::Void;
45 using ::android::hardware::hidl_vec;
46 using ::android::hardware::hidl_string;
47 using ::android::sp;
48 
49 struct Stream : public IStream, public ParametersUtil {
50     explicit Stream(audio_stream_t* stream);
51 
52     // Methods from ::android::hardware::audio::V2_0::IStream follow.
53     Return<uint64_t> getFrameSize()  override;
54     Return<uint64_t> getFrameCount()  override;
55     Return<uint64_t> getBufferSize()  override;
56     Return<uint32_t> getSampleRate()  override;
57     Return<void> getSupportedSampleRates(getSupportedSampleRates_cb _hidl_cb)  override;
58     Return<Result> setSampleRate(uint32_t sampleRateHz)  override;
59     Return<AudioChannelMask> getChannelMask()  override;
60     Return<void> getSupportedChannelMasks(getSupportedChannelMasks_cb _hidl_cb)  override;
61     Return<Result> setChannelMask(AudioChannelMask mask)  override;
62     Return<AudioFormat> getFormat()  override;
63     Return<void> getSupportedFormats(getSupportedFormats_cb _hidl_cb)  override;
64     Return<Result> setFormat(AudioFormat format)  override;
65     Return<void> getAudioProperties(getAudioProperties_cb _hidl_cb)  override;
66     Return<Result> addEffect(uint64_t effectId)  override;
67     Return<Result> removeEffect(uint64_t effectId)  override;
68     Return<Result> standby()  override;
69     Return<AudioDevice> getDevice()  override;
70     Return<Result> setDevice(const DeviceAddress& address)  override;
71     Return<Result> setConnectedState(const DeviceAddress& address, bool connected)  override;
72     Return<Result> setHwAvSync(uint32_t hwAvSync)  override;
73     Return<void> getParameters(
74             const hidl_vec<hidl_string>& keys, getParameters_cb _hidl_cb)  override;
75     Return<Result> setParameters(const hidl_vec<ParameterValue>& parameters)  override;
76     Return<void> debugDump(const hidl_handle& fd)  override;
77     Return<Result> start() override;
78     Return<Result> stop() override;
79     Return<void> createMmapBuffer(int32_t minSizeFrames, createMmapBuffer_cb _hidl_cb) override;
80     Return<void> getMmapPosition(getMmapPosition_cb _hidl_cb) override;
81     Return<Result> close()  override;
82 
83     // Utility methods for extending interfaces.
84     static Result analyzeStatus(const char* funcName, int status);
85     static Result analyzeStatus(const char* funcName, int status,
86                                 const std::vector<int>& ignoreErrors);
87 
88    private:
89     audio_stream_t *mStream;
90 
91     virtual ~Stream();
92 
93     // Methods from ParametersUtil.
94     char* halGetParameters(const char* keys) override;
95     int halSetParameters(const char* keysAndValues) override;
96 };
97 
98 
99 template <typename T>
100 struct StreamMmap : public RefBase {
StreamMmapStreamMmap101     explicit StreamMmap(T* stream) : mStream(stream) {}
102 
103     Return<Result> start();
104     Return<Result> stop();
105     Return<void> createMmapBuffer(
106             int32_t minSizeFrames, size_t frameSize, IStream::createMmapBuffer_cb _hidl_cb);
107     Return<void> getMmapPosition(IStream::getMmapPosition_cb _hidl_cb);
108 
109  private:
StreamMmapStreamMmap110    StreamMmap() {}
111 
112    T *mStream;
113 };
114 
115 template <typename T>
start()116 Return<Result> StreamMmap<T>::start() {
117     if (mStream->start == NULL) return Result::NOT_SUPPORTED;
118     int result = mStream->start(mStream);
119     return Stream::analyzeStatus("start", result);
120 }
121 
122 template <typename T>
stop()123 Return<Result> StreamMmap<T>::stop() {
124     if (mStream->stop == NULL) return Result::NOT_SUPPORTED;
125     int result = mStream->stop(mStream);
126     return Stream::analyzeStatus("stop", result);
127 }
128 
129 template <typename T>
createMmapBuffer(int32_t minSizeFrames,size_t frameSize,IStream::createMmapBuffer_cb _hidl_cb)130 Return<void> StreamMmap<T>::createMmapBuffer(int32_t minSizeFrames, size_t frameSize,
131                                              IStream::createMmapBuffer_cb _hidl_cb) {
132     Result retval(Result::NOT_SUPPORTED);
133     MmapBufferInfo info;
134     native_handle_t* hidlHandle = nullptr;
135 
136     if (mStream->create_mmap_buffer != NULL) {
137         struct audio_mmap_buffer_info halInfo;
138         retval = Stream::analyzeStatus(
139                 "create_mmap_buffer",
140                 mStream->create_mmap_buffer(mStream, minSizeFrames, &halInfo));
141         if (retval == Result::OK) {
142             hidlHandle = native_handle_create(1, 0);
143             hidlHandle->data[0] = halInfo.shared_memory_fd;
144             info.sharedMemory = hidl_memory("audio_buffer", hidlHandle,
145                                             frameSize *halInfo.buffer_size_frames);
146             info.bufferSizeFrames = halInfo.buffer_size_frames;
147             info.burstSizeFrames = halInfo.burst_size_frames;
148         }
149     }
150     _hidl_cb(retval, info);
151     if (hidlHandle != nullptr) {
152         native_handle_delete(hidlHandle);
153     }
154     return Void();
155 }
156 
157 template <typename T>
getMmapPosition(IStream::getMmapPosition_cb _hidl_cb)158 Return<void> StreamMmap<T>::getMmapPosition(IStream::getMmapPosition_cb _hidl_cb) {
159     Result retval(Result::NOT_SUPPORTED);
160     MmapPosition position;
161 
162     if (mStream->get_mmap_position != NULL) {
163         struct audio_mmap_position halPosition;
164         retval = Stream::analyzeStatus(
165                 "get_mmap_position",
166                 mStream->get_mmap_position(mStream, &halPosition));
167         if (retval == Result::OK) {
168             position.timeNanoseconds = halPosition.time_nanoseconds;
169             position.positionFrames = halPosition.position_frames;
170         }
171     }
172     _hidl_cb(retval, position);
173     return Void();
174 }
175 
176 }  // namespace implementation
177 }  // namespace V2_0
178 }  // namespace audio
179 }  // namespace hardware
180 }  // namespace android
181 
182 #endif  // ANDROID_HARDWARE_AUDIO_V2_0_STREAM_H
183