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