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 | |
| 62 | char* Device::halGetParameters(const char* keys) { |
| 63 | return mDevice->get_parameters(mDevice, keys); |
| 64 | } |
| 65 | |
| 66 | int Device::halSetParameters(const char* keysAndValues) { |
| 67 | return mDevice->set_parameters(mDevice, keysAndValues); |
| 68 | } |
| 69 | |
| 70 | // Methods from ::android::hardware::audio::V2_0::IDevice follow. |
| 71 | Return<Result> Device::initCheck() { |
| 72 | return analyzeStatus("init_check", mDevice->init_check(mDevice)); |
| 73 | } |
| 74 | |
| 75 | Return<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 | |
| 83 | Return<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 | |
| 93 | Return<Result> Device::setMicMute(bool mute) { |
| 94 | return analyzeStatus("set_mic_mute", mDevice->set_mic_mute(mDevice, mute)); |
| 95 | } |
| 96 | |
| 97 | Return<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 | |
| 104 | Return<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 | |
| 112 | Return<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 | |
| 122 | Return<void> Device::getInputBufferSize( |
| 123 | const AudioConfig& config, getInputBufferSize_cb _hidl_cb) { |
| 124 | audio_config_t halConfig; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 125 | HidlUtils::audioConfigToHal(config, &halConfig); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 126 | 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 | |
| 137 | Return<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 Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 144 | HidlUtils::audioConfigToHal(config, &halConfig); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 145 | audio_stream_out_t *halStream; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 146 | 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 Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 152 | 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 Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 160 | ALOGV("open_output_stream status %d stream %p", status, halStream); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 161 | sp<IStreamOut> streamOut; |
| 162 | if (status == OK) { |
| 163 | streamOut = new StreamOut(mDevice, halStream); |
| 164 | } |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 165 | AudioConfig suggestedConfig; |
| 166 | HidlUtils::audioConfigFromHal(halConfig, &suggestedConfig); |
| 167 | _hidl_cb(analyzeStatus("open_output_stream", status), streamOut, suggestedConfig); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 168 | return Void(); |
| 169 | } |
| 170 | |
| 171 | Return<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 Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 179 | HidlUtils::audioConfigToHal(config, &halConfig); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 180 | audio_stream_in_t *halStream; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 181 | 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 Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 187 | 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 Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 196 | ALOGV("open_input_stream status %d stream %p", status, halStream); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 197 | sp<IStreamIn> streamIn; |
| 198 | if (status == OK) { |
| 199 | streamIn = new StreamIn(mDevice, halStream); |
| 200 | } |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 201 | AudioConfig suggestedConfig; |
| 202 | HidlUtils::audioConfigFromHal(halConfig, &suggestedConfig); |
| 203 | _hidl_cb(analyzeStatus("open_input_stream", status), streamIn, suggestedConfig); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 204 | return Void(); |
| 205 | } |
| 206 | |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 207 | Return<bool> Device::supportsAudioPatches() { |
| 208 | return version() >= AUDIO_DEVICE_API_VERSION_3_0; |
| 209 | } |
| 210 | |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 211 | Return<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 Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 218 | std::unique_ptr<audio_port_config[]> halSources(HidlUtils::audioPortConfigsToHal(sources)); |
| 219 | std::unique_ptr<audio_port_config[]> halSinks(HidlUtils::audioPortConfigsToHal(sinks)); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 220 | 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 | |
| 236 | Return<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 | |
| 245 | Return<void> Device::getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb) { |
| 246 | audio_port halPort; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 247 | HidlUtils::audioPortToHal(port, &halPort); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 248 | Result retval = analyzeStatus("get_audio_port", mDevice->get_audio_port(mDevice, &halPort)); |
| 249 | AudioPort resultPort = port; |
| 250 | if (retval == Result::OK) { |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 251 | HidlUtils::audioPortFromHal(halPort, &resultPort); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 252 | } |
| 253 | _hidl_cb(retval, resultPort); |
| 254 | return Void(); |
| 255 | } |
| 256 | |
| 257 | Return<Result> Device::setAudioPortConfig(const AudioPortConfig& config) { |
| 258 | if (version() >= AUDIO_DEVICE_API_VERSION_3_0) { |
| 259 | struct audio_port_config halPortConfig; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 260 | HidlUtils::audioPortConfigToHal(config, &halPortConfig); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 261 | return analyzeStatus( |
| 262 | "set_audio_port_config", mDevice->set_audio_port_config(mDevice, &halPortConfig)); |
| 263 | } |
| 264 | return Result::NOT_SUPPORTED; |
| 265 | } |
| 266 | |
| 267 | Return<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 | |
| 273 | Return<Result> Device::setScreenState(bool turnedOn) { |
| 274 | return setParam(AudioParameter::keyScreenState, turnedOn); |
| 275 | } |
| 276 | |
| 277 | Return<void> Device::getParameters(const hidl_vec<hidl_string>& keys, getParameters_cb _hidl_cb) { |
| 278 | getParametersImpl(keys, _hidl_cb); |
| 279 | return Void(); |
| 280 | } |
| 281 | |
| 282 | Return<Result> Device::setParameters(const hidl_vec<ParameterValue>& parameters) { |
| 283 | return setParametersImpl(parameters); |
| 284 | } |
| 285 | |
Martijn Coenen | 70b9a15 | 2016-11-18 15:29:32 +0100 | [diff] [blame] | 286 | Return<void> Device::debugDump(const hidl_handle& fd) { |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 287 | 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 |