Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 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 | #define LOG_TAG "DeviceHAL" |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 19 | |
| 20 | #include <algorithm> |
| 21 | #include <memory.h> |
| 22 | #include <string.h> |
| 23 | |
Yifan Hong | f9d3034 | 2016-11-30 13:45:34 -0800 | [diff] [blame] | 24 | #include <android/log.h> |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 25 | |
| 26 | #include "Conversions.h" |
| 27 | #include "Device.h" |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 28 | #include "HidlUtils.h" |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 29 | #include "StreamIn.h" |
| 30 | #include "StreamOut.h" |
| 31 | |
| 32 | namespace android { |
| 33 | namespace hardware { |
| 34 | namespace audio { |
| 35 | namespace V2_0 { |
| 36 | namespace implementation { |
| 37 | |
| 38 | Device::Device(audio_hw_device_t* device) |
| 39 | : mDevice(device) { |
| 40 | } |
| 41 | |
| 42 | Device::~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 Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 48 | Result 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 Naganov | 936279e | 2017-03-29 09:31:18 -0700 | [diff] [blame^] | 62 | void Device::closeInputStream(audio_stream_in_t* stream) { |
| 63 | mDevice->close_input_stream(mDevice, stream); |
| 64 | } |
| 65 | |
| 66 | void Device::closeOutputStream(audio_stream_out_t* stream) { |
| 67 | mDevice->close_output_stream(mDevice, stream); |
| 68 | } |
| 69 | |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 70 | char* Device::halGetParameters(const char* keys) { |
| 71 | return mDevice->get_parameters(mDevice, keys); |
| 72 | } |
| 73 | |
| 74 | int Device::halSetParameters(const char* keysAndValues) { |
| 75 | return mDevice->set_parameters(mDevice, keysAndValues); |
| 76 | } |
| 77 | |
| 78 | // Methods from ::android::hardware::audio::V2_0::IDevice follow. |
| 79 | Return<Result> Device::initCheck() { |
| 80 | return analyzeStatus("init_check", mDevice->init_check(mDevice)); |
| 81 | } |
| 82 | |
| 83 | Return<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 | |
| 91 | Return<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 | |
| 101 | Return<Result> Device::setMicMute(bool mute) { |
| 102 | return analyzeStatus("set_mic_mute", mDevice->set_mic_mute(mDevice, mute)); |
| 103 | } |
| 104 | |
| 105 | Return<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 | |
| 112 | Return<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 | |
| 120 | Return<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 | |
| 130 | Return<void> Device::getInputBufferSize( |
| 131 | const AudioConfig& config, getInputBufferSize_cb _hidl_cb) { |
| 132 | audio_config_t halConfig; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 133 | HidlUtils::audioConfigToHal(config, &halConfig); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 134 | 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 | |
| 145 | Return<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 Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 152 | HidlUtils::audioConfigToHal(config, &halConfig); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 153 | audio_stream_out_t *halStream; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 154 | 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 Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 160 | 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 Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 168 | ALOGV("open_output_stream status %d stream %p", status, halStream); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 169 | sp<IStreamOut> streamOut; |
| 170 | if (status == OK) { |
Mikhail Naganov | 936279e | 2017-03-29 09:31:18 -0700 | [diff] [blame^] | 171 | streamOut = new StreamOut(this, halStream); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 172 | } |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 173 | AudioConfig suggestedConfig; |
| 174 | HidlUtils::audioConfigFromHal(halConfig, &suggestedConfig); |
| 175 | _hidl_cb(analyzeStatus("open_output_stream", status), streamOut, suggestedConfig); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 176 | return Void(); |
| 177 | } |
| 178 | |
| 179 | Return<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 Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 187 | HidlUtils::audioConfigToHal(config, &halConfig); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 188 | audio_stream_in_t *halStream; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 189 | 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 Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 195 | 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 Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 204 | ALOGV("open_input_stream status %d stream %p", status, halStream); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 205 | sp<IStreamIn> streamIn; |
| 206 | if (status == OK) { |
Mikhail Naganov | 936279e | 2017-03-29 09:31:18 -0700 | [diff] [blame^] | 207 | streamIn = new StreamIn(this, halStream); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 208 | } |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 209 | AudioConfig suggestedConfig; |
| 210 | HidlUtils::audioConfigFromHal(halConfig, &suggestedConfig); |
| 211 | _hidl_cb(analyzeStatus("open_input_stream", status), streamIn, suggestedConfig); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 212 | return Void(); |
| 213 | } |
| 214 | |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 215 | Return<bool> Device::supportsAudioPatches() { |
| 216 | return version() >= AUDIO_DEVICE_API_VERSION_3_0; |
| 217 | } |
| 218 | |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 219 | Return<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 Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 226 | std::unique_ptr<audio_port_config[]> halSources(HidlUtils::audioPortConfigsToHal(sources)); |
| 227 | std::unique_ptr<audio_port_config[]> halSinks(HidlUtils::audioPortConfigsToHal(sinks)); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 228 | 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 | |
| 244 | Return<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 | |
| 253 | Return<void> Device::getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb) { |
| 254 | audio_port halPort; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 255 | HidlUtils::audioPortToHal(port, &halPort); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 256 | Result retval = analyzeStatus("get_audio_port", mDevice->get_audio_port(mDevice, &halPort)); |
| 257 | AudioPort resultPort = port; |
| 258 | if (retval == Result::OK) { |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 259 | HidlUtils::audioPortFromHal(halPort, &resultPort); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 260 | } |
| 261 | _hidl_cb(retval, resultPort); |
| 262 | return Void(); |
| 263 | } |
| 264 | |
| 265 | Return<Result> Device::setAudioPortConfig(const AudioPortConfig& config) { |
| 266 | if (version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 267 | struct audio_port_config halPortConfig; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 268 | HidlUtils::audioPortConfigToHal(config, &halPortConfig); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 269 | return analyzeStatus( |
| 270 | "set_audio_port_config", mDevice->set_audio_port_config(mDevice, &halPortConfig)); |
| 271 | } |
| 272 | return Result::NOT_SUPPORTED; |
| 273 | } |
| 274 | |
| 275 | Return<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 | |
| 281 | Return<Result> Device::setScreenState(bool turnedOn) { |
| 282 | return setParam(AudioParameter::keyScreenState, turnedOn); |
| 283 | } |
| 284 | |
| 285 | Return<void> Device::getParameters(const hidl_vec<hidl_string>& keys, getParameters_cb _hidl_cb) { |
| 286 | getParametersImpl(keys, _hidl_cb); |
| 287 | return Void(); |
| 288 | } |
| 289 | |
| 290 | Return<Result> Device::setParameters(const hidl_vec<ParameterValue>& parameters) { |
| 291 | return setParametersImpl(parameters); |
| 292 | } |
| 293 | |
Martijn Coenen | 70b9a15 | 2016-11-18 15:29:32 +0100 | [diff] [blame] | 294 | Return<void> Device::debugDump(const hidl_handle& fd) { |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 295 | 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 |