blob: 8a51cd7d51d04a01901dada1eb9cd2e1c352f6a2 [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
17#define LOG_TAG "DeviceHAL"
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -080018//#define LOG_NDEBUG 0
Mikhail Naganov10548292016-10-31 10:39:47 -070019
20#include <algorithm>
21#include <memory.h>
22#include <string.h>
23
Yifan Hongf9d30342016-11-30 13:45:34 -080024#include <android/log.h>
Mikhail Naganov10548292016-10-31 10:39:47 -070025
26#include "Conversions.h"
27#include "Device.h"
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -080028#include "HidlUtils.h"
Mikhail Naganov10548292016-10-31 10:39:47 -070029#include "StreamIn.h"
30#include "StreamOut.h"
31
32namespace android {
33namespace hardware {
34namespace audio {
35namespace V2_0 {
36namespace implementation {
37
38Device::Device(audio_hw_device_t* device)
39 : mDevice(device) {
40}
41
42Device::~Device() {
43 int status = audio_hw_device_close(mDevice);
44 ALOGW_IF(status, "Error closing audio hw device %p: %s", mDevice, strerror(-status));
45 mDevice = nullptr;
46}
47
Mikhail Naganov10548292016-10-31 10:39:47 -070048Result Device::analyzeStatus(const char* funcName, int status) {
49 if (status != 0) {
50 ALOGW("Device %p %s: %s", mDevice, funcName, strerror(-status));
51 }
52 switch (status) {
53 case 0: return Result::OK;
54 case -EINVAL: return Result::INVALID_ARGUMENTS;
55 case -ENODATA: return Result::INVALID_STATE;
56 case -ENODEV: return Result::NOT_INITIALIZED;
57 case -ENOSYS: return Result::NOT_SUPPORTED;
58 default: return Result::INVALID_STATE;
59 }
60}
61
62char* Device::halGetParameters(const char* keys) {
63 return mDevice->get_parameters(mDevice, keys);
64}
65
66int Device::halSetParameters(const char* keysAndValues) {
67 return mDevice->set_parameters(mDevice, keysAndValues);
68}
69
70// Methods from ::android::hardware::audio::V2_0::IDevice follow.
71Return<Result> Device::initCheck() {
72 return analyzeStatus("init_check", mDevice->init_check(mDevice));
73}
74
75Return<Result> Device::setMasterVolume(float volume) {
76 Result retval(Result::NOT_SUPPORTED);
77 if (mDevice->set_master_volume != NULL) {
78 retval = analyzeStatus("set_master_volume", mDevice->set_master_volume(mDevice, volume));
79 }
80 return retval;
81}
82
83Return<void> Device::getMasterVolume(getMasterVolume_cb _hidl_cb) {
84 Result retval(Result::NOT_SUPPORTED);
85 float volume = 0;
86 if (mDevice->get_master_volume != NULL) {
87 retval = analyzeStatus("get_master_volume", mDevice->get_master_volume(mDevice, &volume));
88 }
89 _hidl_cb(retval, volume);
90 return Void();
91}
92
93Return<Result> Device::setMicMute(bool mute) {
94 return analyzeStatus("set_mic_mute", mDevice->set_mic_mute(mDevice, mute));
95}
96
97Return<void> Device::getMicMute(getMicMute_cb _hidl_cb) {
98 bool mute = false;
99 Result retval = analyzeStatus("get_mic_mute", mDevice->get_mic_mute(mDevice, &mute));
100 _hidl_cb(retval, mute);
101 return Void();
102}
103
104Return<Result> Device::setMasterMute(bool mute) {
105 Result retval(Result::NOT_SUPPORTED);
106 if (mDevice->set_master_mute != NULL) {
107 retval = analyzeStatus("set_master_mute", mDevice->set_master_mute(mDevice, mute));
108 }
109 return retval;
110}
111
112Return<void> Device::getMasterMute(getMasterMute_cb _hidl_cb) {
113 Result retval(Result::NOT_SUPPORTED);
114 bool mute = false;
115 if (mDevice->get_master_mute != NULL) {
116 retval = analyzeStatus("get_master_mute", mDevice->get_master_mute(mDevice, &mute));
117 }
118 _hidl_cb(retval, mute);
119 return Void();
120}
121
122Return<void> Device::getInputBufferSize(
123 const AudioConfig& config, getInputBufferSize_cb _hidl_cb) {
124 audio_config_t halConfig;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800125 HidlUtils::audioConfigToHal(config, &halConfig);
Mikhail Naganov10548292016-10-31 10:39:47 -0700126 size_t halBufferSize = mDevice->get_input_buffer_size(mDevice, &halConfig);
127 Result retval(Result::INVALID_ARGUMENTS);
128 uint64_t bufferSize = 0;
129 if (halBufferSize != 0) {
130 retval = Result::OK;
131 bufferSize = halBufferSize;
132 }
133 _hidl_cb(retval, bufferSize);
134 return Void();
135}
136
137Return<void> Device::openOutputStream(
138 int32_t ioHandle,
139 const DeviceAddress& device,
140 const AudioConfig& config,
141 AudioOutputFlag flags,
142 openOutputStream_cb _hidl_cb) {
143 audio_config_t halConfig;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800144 HidlUtils::audioConfigToHal(config, &halConfig);
Mikhail Naganov10548292016-10-31 10:39:47 -0700145 audio_stream_out_t *halStream;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800146 ALOGV("open_output_stream handle: %d devices: %x flags: %#x "
147 "srate: %d format %#x channels %x address %s",
148 ioHandle,
149 static_cast<audio_devices_t>(device.device), static_cast<audio_output_flags_t>(flags),
150 halConfig.sample_rate, halConfig.format, halConfig.channel_mask,
151 deviceAddressToHal(device).c_str());
Mikhail Naganov10548292016-10-31 10:39:47 -0700152 int status = mDevice->open_output_stream(
153 mDevice,
154 ioHandle,
155 static_cast<audio_devices_t>(device.device),
156 static_cast<audio_output_flags_t>(flags),
157 &halConfig,
158 &halStream,
159 deviceAddressToHal(device).c_str());
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800160 ALOGV("open_output_stream status %d stream %p", status, halStream);
Mikhail Naganov10548292016-10-31 10:39:47 -0700161 sp<IStreamOut> streamOut;
162 if (status == OK) {
163 streamOut = new StreamOut(mDevice, halStream);
164 }
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800165 AudioConfig suggestedConfig;
166 HidlUtils::audioConfigFromHal(halConfig, &suggestedConfig);
167 _hidl_cb(analyzeStatus("open_output_stream", status), streamOut, suggestedConfig);
Mikhail Naganov10548292016-10-31 10:39:47 -0700168 return Void();
169}
170
171Return<void> Device::openInputStream(
172 int32_t ioHandle,
173 const DeviceAddress& device,
174 const AudioConfig& config,
175 AudioInputFlag flags,
176 AudioSource source,
177 openInputStream_cb _hidl_cb) {
178 audio_config_t halConfig;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800179 HidlUtils::audioConfigToHal(config, &halConfig);
Mikhail Naganov10548292016-10-31 10:39:47 -0700180 audio_stream_in_t *halStream;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800181 ALOGV("open_input_stream handle: %d devices: %x flags: %#x "
182 "srate: %d format %#x channels %x address %s source %d",
183 ioHandle,
184 static_cast<audio_devices_t>(device.device), static_cast<audio_input_flags_t>(flags),
185 halConfig.sample_rate, halConfig.format, halConfig.channel_mask,
186 deviceAddressToHal(device).c_str(), static_cast<audio_source_t>(source));
Mikhail Naganov10548292016-10-31 10:39:47 -0700187 int status = mDevice->open_input_stream(
188 mDevice,
189 ioHandle,
190 static_cast<audio_devices_t>(device.device),
191 &halConfig,
192 &halStream,
193 static_cast<audio_input_flags_t>(flags),
194 deviceAddressToHal(device).c_str(),
195 static_cast<audio_source_t>(source));
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800196 ALOGV("open_input_stream status %d stream %p", status, halStream);
Mikhail Naganov10548292016-10-31 10:39:47 -0700197 sp<IStreamIn> streamIn;
198 if (status == OK) {
199 streamIn = new StreamIn(mDevice, halStream);
200 }
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800201 AudioConfig suggestedConfig;
202 HidlUtils::audioConfigFromHal(halConfig, &suggestedConfig);
203 _hidl_cb(analyzeStatus("open_input_stream", status), streamIn, suggestedConfig);
Mikhail Naganov10548292016-10-31 10:39:47 -0700204 return Void();
205}
206
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800207Return<bool> Device::supportsAudioPatches() {
208 return version() >= AUDIO_DEVICE_API_VERSION_3_0;
209}
210
Mikhail Naganov10548292016-10-31 10:39:47 -0700211Return<void> Device::createAudioPatch(
212 const hidl_vec<AudioPortConfig>& sources,
213 const hidl_vec<AudioPortConfig>& sinks,
214 createAudioPatch_cb _hidl_cb) {
215 Result retval(Result::NOT_SUPPORTED);
216 AudioPatchHandle patch = 0;
217 if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800218 std::unique_ptr<audio_port_config[]> halSources(HidlUtils::audioPortConfigsToHal(sources));
219 std::unique_ptr<audio_port_config[]> halSinks(HidlUtils::audioPortConfigsToHal(sinks));
Mikhail Naganov10548292016-10-31 10:39:47 -0700220 audio_patch_handle_t halPatch;
221 retval = analyzeStatus(
222 "create_audio_patch",
223 mDevice->create_audio_patch(
224 mDevice,
225 sources.size(), &halSources[0],
226 sinks.size(), &halSinks[0],
227 &halPatch));
228 if (retval == Result::OK) {
229 patch = static_cast<AudioPatchHandle>(halPatch);
230 }
231 }
232 _hidl_cb(retval, patch);
233 return Void();
234}
235
236Return<Result> Device::releaseAudioPatch(int32_t patch) {
237 if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
238 return analyzeStatus(
239 "release_audio_patch",
240 mDevice->release_audio_patch(mDevice, static_cast<audio_patch_handle_t>(patch)));
241 }
242 return Result::NOT_SUPPORTED;
243}
244
245Return<void> Device::getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb) {
246 audio_port halPort;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800247 HidlUtils::audioPortToHal(port, &halPort);
Mikhail Naganov10548292016-10-31 10:39:47 -0700248 Result retval = analyzeStatus("get_audio_port", mDevice->get_audio_port(mDevice, &halPort));
249 AudioPort resultPort = port;
250 if (retval == Result::OK) {
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800251 HidlUtils::audioPortFromHal(halPort, &resultPort);
Mikhail Naganov10548292016-10-31 10:39:47 -0700252 }
253 _hidl_cb(retval, resultPort);
254 return Void();
255}
256
257Return<Result> Device::setAudioPortConfig(const AudioPortConfig& config) {
258 if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
259 struct audio_port_config halPortConfig;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800260 HidlUtils::audioPortConfigToHal(config, &halPortConfig);
Mikhail Naganov10548292016-10-31 10:39:47 -0700261 return analyzeStatus(
262 "set_audio_port_config", mDevice->set_audio_port_config(mDevice, &halPortConfig));
263 }
264 return Result::NOT_SUPPORTED;
265}
266
267Return<AudioHwSync> Device::getHwAvSync() {
268 int halHwAvSync;
269 Result retval = getParam(AudioParameter::keyHwAvSync, &halHwAvSync);
270 return retval == Result::OK ? halHwAvSync : AUDIO_HW_SYNC_INVALID;
271}
272
273Return<Result> Device::setScreenState(bool turnedOn) {
274 return setParam(AudioParameter::keyScreenState, turnedOn);
275}
276
277Return<void> Device::getParameters(const hidl_vec<hidl_string>& keys, getParameters_cb _hidl_cb) {
278 getParametersImpl(keys, _hidl_cb);
279 return Void();
280}
281
282Return<Result> Device::setParameters(const hidl_vec<ParameterValue>& parameters) {
283 return setParametersImpl(parameters);
284}
285
Martijn Coenen70b9a152016-11-18 15:29:32 +0100286Return<void> Device::debugDump(const hidl_handle& fd) {
Mikhail Naganov10548292016-10-31 10:39:47 -0700287 if (fd->numFds == 1) {
288 analyzeStatus("dump", mDevice->dump(mDevice, fd->data[0]));
289 }
290 return Void();
291}
292
293} // namespace implementation
294} // namespace V2_0
295} // namespace audio
296} // namespace hardware
297} // namespace android