blob: 82f05a77b41bd8d608fe498e7b7a8ef34cf36d68 [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
Mikhail Naganov96a3a192017-05-19 16:46:28 -070020#include <vector>
21
Mikhail Naganov10548292016-10-31 10:39:47 -070022#include <android/hardware/audio/2.0/IStream.h>
Eric Laurent7deb7da2016-12-15 19:15:45 -080023#include <hardware/audio.h>
Mikhail Naganov10548292016-10-31 10:39:47 -070024#include <hidl/Status.h>
25
26#include <hidl/MQDescriptor.h>
27
28#include "ParametersUtil.h"
29
30namespace android {
31namespace hardware {
32namespace audio {
33namespace V2_0 {
34namespace implementation {
35
36using ::android::hardware::audio::common::V2_0::AudioChannelMask;
37using ::android::hardware::audio::common::V2_0::AudioDevice;
38using ::android::hardware::audio::common::V2_0::AudioFormat;
39using ::android::hardware::audio::V2_0::DeviceAddress;
40using ::android::hardware::audio::V2_0::IStream;
41using ::android::hardware::audio::V2_0::ParameterValue;
42using ::android::hardware::audio::V2_0::Result;
43using ::android::hardware::Return;
44using ::android::hardware::Void;
45using ::android::hardware::hidl_vec;
46using ::android::hardware::hidl_string;
47using ::android::sp;
48
49struct 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;
Martijn Coenen70b9a152016-11-18 15:29:32 +010076 Return<void> debugDump(const hidl_handle& fd) override;
Eric Laurent7deb7da2016-12-15 19:15:45 -080077 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;
Mikhail Naganovb29438e2016-12-22 09:21:34 -080081 Return<Result> close() override;
Mikhail Naganov10548292016-10-31 10:39:47 -070082
83 // Utility methods for extending interfaces.
Mikhail Naganov96a3a192017-05-19 16:46:28 -070084 static Result analyzeStatus(const char* funcName, int status);
85 static Result analyzeStatus(const char* funcName, int status,
86 const std::vector<int>& ignoreErrors);
Mikhail Naganov10548292016-10-31 10:39:47 -070087
Mikhail Naganov96a3a192017-05-19 16:46:28 -070088 private:
Mikhail Naganov10548292016-10-31 10:39:47 -070089 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
Eric Laurent7deb7da2016-12-15 19:15:45 -080098
99template <typename T>
100struct StreamMmap : public RefBase {
101 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:
110 StreamMmap() {}
111
112 T *mStream;
113};
114
115template <typename T>
116Return<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
122template <typename T>
123Return<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
129template <typename T>
130Return<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;
Martijn Coenen45d4d302017-01-23 13:00:19 +0100134 native_handle_t* hidlHandle = nullptr;
Eric Laurent7deb7da2016-12-15 19:15:45 -0800135
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) {
Martijn Coenen45d4d302017-01-23 13:00:19 +0100142 hidlHandle = native_handle_create(1, 0);
Eric Laurent7deb7da2016-12-15 19:15:45 -0800143 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);
Martijn Coenen45d4d302017-01-23 13:00:19 +0100151 if (hidlHandle != nullptr) {
Martijn Coenen45d4d302017-01-23 13:00:19 +0100152 native_handle_delete(hidlHandle);
153 }
Eric Laurent7deb7da2016-12-15 19:15:45 -0800154 return Void();
155}
156
157template <typename T>
158Return<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
Mikhail Naganov10548292016-10-31 10:39:47 -0700176} // namespace implementation
177} // namespace V2_0
178} // namespace audio
179} // namespace hardware
180} // namespace android
181
Steven Morelandd6e4f032016-11-28 18:37:07 -0800182#endif // ANDROID_HARDWARE_AUDIO_V2_0_STREAM_H