blob: 5ced0bcb0a8e0455032c4be83575ee8b3723c5c5 [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
Mikhail Naganov936279e2017-03-29 09:31:18 -070062void Device::closeInputStream(audio_stream_in_t* stream) {
63 mDevice->close_input_stream(mDevice, stream);
64}
65
66void Device::closeOutputStream(audio_stream_out_t* stream) {
67 mDevice->close_output_stream(mDevice, stream);
68}
69
Mikhail Naganov10548292016-10-31 10:39:47 -070070char* Device::halGetParameters(const char* keys) {
71 return mDevice->get_parameters(mDevice, keys);
72}
73
74int Device::halSetParameters(const char* keysAndValues) {
75 return mDevice->set_parameters(mDevice, keysAndValues);
76}
77
78// Methods from ::android::hardware::audio::V2_0::IDevice follow.
79Return<Result> Device::initCheck() {
80 return analyzeStatus("init_check", mDevice->init_check(mDevice));
81}
82
83Return<Result> Device::setMasterVolume(float volume) {
84 Result retval(Result::NOT_SUPPORTED);
85 if (mDevice->set_master_volume != NULL) {
86 retval = analyzeStatus("set_master_volume", mDevice->set_master_volume(mDevice, volume));
87 }
88 return retval;
89}
90
91Return<void> Device::getMasterVolume(getMasterVolume_cb _hidl_cb) {
92 Result retval(Result::NOT_SUPPORTED);
93 float volume = 0;
94 if (mDevice->get_master_volume != NULL) {
95 retval = analyzeStatus("get_master_volume", mDevice->get_master_volume(mDevice, &volume));
96 }
97 _hidl_cb(retval, volume);
98 return Void();
99}
100
101Return<Result> Device::setMicMute(bool mute) {
102 return analyzeStatus("set_mic_mute", mDevice->set_mic_mute(mDevice, mute));
103}
104
105Return<void> Device::getMicMute(getMicMute_cb _hidl_cb) {
106 bool mute = false;
107 Result retval = analyzeStatus("get_mic_mute", mDevice->get_mic_mute(mDevice, &mute));
108 _hidl_cb(retval, mute);
109 return Void();
110}
111
112Return<Result> Device::setMasterMute(bool mute) {
113 Result retval(Result::NOT_SUPPORTED);
114 if (mDevice->set_master_mute != NULL) {
115 retval = analyzeStatus("set_master_mute", mDevice->set_master_mute(mDevice, mute));
116 }
117 return retval;
118}
119
120Return<void> Device::getMasterMute(getMasterMute_cb _hidl_cb) {
121 Result retval(Result::NOT_SUPPORTED);
122 bool mute = false;
123 if (mDevice->get_master_mute != NULL) {
124 retval = analyzeStatus("get_master_mute", mDevice->get_master_mute(mDevice, &mute));
125 }
126 _hidl_cb(retval, mute);
127 return Void();
128}
129
130Return<void> Device::getInputBufferSize(
131 const AudioConfig& config, getInputBufferSize_cb _hidl_cb) {
132 audio_config_t halConfig;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800133 HidlUtils::audioConfigToHal(config, &halConfig);
Mikhail Naganov10548292016-10-31 10:39:47 -0700134 size_t halBufferSize = mDevice->get_input_buffer_size(mDevice, &halConfig);
135 Result retval(Result::INVALID_ARGUMENTS);
136 uint64_t bufferSize = 0;
137 if (halBufferSize != 0) {
138 retval = Result::OK;
139 bufferSize = halBufferSize;
140 }
141 _hidl_cb(retval, bufferSize);
142 return Void();
143}
144
145Return<void> Device::openOutputStream(
146 int32_t ioHandle,
147 const DeviceAddress& device,
148 const AudioConfig& config,
149 AudioOutputFlag flags,
150 openOutputStream_cb _hidl_cb) {
151 audio_config_t halConfig;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800152 HidlUtils::audioConfigToHal(config, &halConfig);
Mikhail Naganov10548292016-10-31 10:39:47 -0700153 audio_stream_out_t *halStream;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800154 ALOGV("open_output_stream handle: %d devices: %x flags: %#x "
155 "srate: %d format %#x channels %x address %s",
156 ioHandle,
157 static_cast<audio_devices_t>(device.device), static_cast<audio_output_flags_t>(flags),
158 halConfig.sample_rate, halConfig.format, halConfig.channel_mask,
159 deviceAddressToHal(device).c_str());
Mikhail Naganov10548292016-10-31 10:39:47 -0700160 int status = mDevice->open_output_stream(
161 mDevice,
162 ioHandle,
163 static_cast<audio_devices_t>(device.device),
164 static_cast<audio_output_flags_t>(flags),
165 &halConfig,
166 &halStream,
167 deviceAddressToHal(device).c_str());
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800168 ALOGV("open_output_stream status %d stream %p", status, halStream);
Mikhail Naganov10548292016-10-31 10:39:47 -0700169 sp<IStreamOut> streamOut;
170 if (status == OK) {
Mikhail Naganov936279e2017-03-29 09:31:18 -0700171 streamOut = new StreamOut(this, halStream);
Mikhail Naganov10548292016-10-31 10:39:47 -0700172 }
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800173 AudioConfig suggestedConfig;
174 HidlUtils::audioConfigFromHal(halConfig, &suggestedConfig);
175 _hidl_cb(analyzeStatus("open_output_stream", status), streamOut, suggestedConfig);
Mikhail Naganov10548292016-10-31 10:39:47 -0700176 return Void();
177}
178
179Return<void> Device::openInputStream(
180 int32_t ioHandle,
181 const DeviceAddress& device,
182 const AudioConfig& config,
183 AudioInputFlag flags,
184 AudioSource source,
185 openInputStream_cb _hidl_cb) {
186 audio_config_t halConfig;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800187 HidlUtils::audioConfigToHal(config, &halConfig);
Mikhail Naganov10548292016-10-31 10:39:47 -0700188 audio_stream_in_t *halStream;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800189 ALOGV("open_input_stream handle: %d devices: %x flags: %#x "
190 "srate: %d format %#x channels %x address %s source %d",
191 ioHandle,
192 static_cast<audio_devices_t>(device.device), static_cast<audio_input_flags_t>(flags),
193 halConfig.sample_rate, halConfig.format, halConfig.channel_mask,
194 deviceAddressToHal(device).c_str(), static_cast<audio_source_t>(source));
Mikhail Naganov10548292016-10-31 10:39:47 -0700195 int status = mDevice->open_input_stream(
196 mDevice,
197 ioHandle,
198 static_cast<audio_devices_t>(device.device),
199 &halConfig,
200 &halStream,
201 static_cast<audio_input_flags_t>(flags),
202 deviceAddressToHal(device).c_str(),
203 static_cast<audio_source_t>(source));
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800204 ALOGV("open_input_stream status %d stream %p", status, halStream);
Mikhail Naganov10548292016-10-31 10:39:47 -0700205 sp<IStreamIn> streamIn;
206 if (status == OK) {
Mikhail Naganov936279e2017-03-29 09:31:18 -0700207 streamIn = new StreamIn(this, halStream);
Mikhail Naganov10548292016-10-31 10:39:47 -0700208 }
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800209 AudioConfig suggestedConfig;
210 HidlUtils::audioConfigFromHal(halConfig, &suggestedConfig);
211 _hidl_cb(analyzeStatus("open_input_stream", status), streamIn, suggestedConfig);
Mikhail Naganov10548292016-10-31 10:39:47 -0700212 return Void();
213}
214
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800215Return<bool> Device::supportsAudioPatches() {
216 return version() >= AUDIO_DEVICE_API_VERSION_3_0;
217}
218
Mikhail Naganov10548292016-10-31 10:39:47 -0700219Return<void> Device::createAudioPatch(
220 const hidl_vec<AudioPortConfig>& sources,
221 const hidl_vec<AudioPortConfig>& sinks,
222 createAudioPatch_cb _hidl_cb) {
223 Result retval(Result::NOT_SUPPORTED);
224 AudioPatchHandle patch = 0;
225 if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800226 std::unique_ptr<audio_port_config[]> halSources(HidlUtils::audioPortConfigsToHal(sources));
227 std::unique_ptr<audio_port_config[]> halSinks(HidlUtils::audioPortConfigsToHal(sinks));
Mikhail Naganov10548292016-10-31 10:39:47 -0700228 audio_patch_handle_t halPatch;
229 retval = analyzeStatus(
230 "create_audio_patch",
231 mDevice->create_audio_patch(
232 mDevice,
233 sources.size(), &halSources[0],
234 sinks.size(), &halSinks[0],
235 &halPatch));
236 if (retval == Result::OK) {
237 patch = static_cast<AudioPatchHandle>(halPatch);
238 }
239 }
240 _hidl_cb(retval, patch);
241 return Void();
242}
243
244Return<Result> Device::releaseAudioPatch(int32_t patch) {
245 if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
246 return analyzeStatus(
247 "release_audio_patch",
248 mDevice->release_audio_patch(mDevice, static_cast<audio_patch_handle_t>(patch)));
249 }
250 return Result::NOT_SUPPORTED;
251}
252
253Return<void> Device::getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb) {
254 audio_port halPort;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800255 HidlUtils::audioPortToHal(port, &halPort);
Mikhail Naganov10548292016-10-31 10:39:47 -0700256 Result retval = analyzeStatus("get_audio_port", mDevice->get_audio_port(mDevice, &halPort));
257 AudioPort resultPort = port;
258 if (retval == Result::OK) {
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800259 HidlUtils::audioPortFromHal(halPort, &resultPort);
Mikhail Naganov10548292016-10-31 10:39:47 -0700260 }
261 _hidl_cb(retval, resultPort);
262 return Void();
263}
264
265Return<Result> Device::setAudioPortConfig(const AudioPortConfig& config) {
266 if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
267 struct audio_port_config halPortConfig;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800268 HidlUtils::audioPortConfigToHal(config, &halPortConfig);
Mikhail Naganov10548292016-10-31 10:39:47 -0700269 return analyzeStatus(
270 "set_audio_port_config", mDevice->set_audio_port_config(mDevice, &halPortConfig));
271 }
272 return Result::NOT_SUPPORTED;
273}
274
275Return<AudioHwSync> Device::getHwAvSync() {
276 int halHwAvSync;
277 Result retval = getParam(AudioParameter::keyHwAvSync, &halHwAvSync);
278 return retval == Result::OK ? halHwAvSync : AUDIO_HW_SYNC_INVALID;
279}
280
281Return<Result> Device::setScreenState(bool turnedOn) {
282 return setParam(AudioParameter::keyScreenState, turnedOn);
283}
284
285Return<void> Device::getParameters(const hidl_vec<hidl_string>& keys, getParameters_cb _hidl_cb) {
286 getParametersImpl(keys, _hidl_cb);
287 return Void();
288}
289
290Return<Result> Device::setParameters(const hidl_vec<ParameterValue>& parameters) {
291 return setParametersImpl(parameters);
292}
293
Martijn Coenen70b9a152016-11-18 15:29:32 +0100294Return<void> Device::debugDump(const hidl_handle& fd) {
Mikhail Naganov10548292016-10-31 10:39:47 -0700295 if (fd->numFds == 1) {
296 analyzeStatus("dump", mDevice->dump(mDevice, fd->data[0]));
297 }
298 return Void();
299}
300
301} // namespace implementation
302} // namespace V2_0
303} // namespace audio
304} // namespace hardware
305} // namespace android