2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "DeviceHAL"
18 //#define LOG_NDEBUG 0
24 #include <android/log.h>
26 #include "Conversions.h"
28 #include "HidlUtils.h"
30 #include "StreamOut.h"
36 namespace implementation {
38 Device::Device(audio_hw_device_t* device)
43 int status = audio_hw_device_close(mDevice);
44 ALOGW_IF(status, "Error closing audio hw device %p: %s", mDevice, strerror(-status));
48 Result Device::analyzeStatus(const char* funcName, int status) {
50 ALOGW("Device %p %s: %s", mDevice, funcName, strerror(-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;
62 char* Device::halGetParameters(const char* keys) {
63 return mDevice->get_parameters(mDevice, keys);
66 int Device::halSetParameters(const char* keysAndValues) {
67 return mDevice->set_parameters(mDevice, keysAndValues);
70 // Methods from ::android::hardware::audio::V2_0::IDevice follow.
71 Return<Result> Device::initCheck() {
72 return analyzeStatus("init_check", mDevice->init_check(mDevice));
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));
83 Return<void> Device::getMasterVolume(getMasterVolume_cb _hidl_cb) {
84 Result retval(Result::NOT_SUPPORTED);
86 if (mDevice->get_master_volume != NULL) {
87 retval = analyzeStatus("get_master_volume", mDevice->get_master_volume(mDevice, &volume));
89 _hidl_cb(retval, volume);
93 Return<Result> Device::setMicMute(bool mute) {
94 return analyzeStatus("set_mic_mute", mDevice->set_mic_mute(mDevice, mute));
97 Return<void> Device::getMicMute(getMicMute_cb _hidl_cb) {
99 Result retval = analyzeStatus("get_mic_mute", mDevice->get_mic_mute(mDevice, &mute));
100 _hidl_cb(retval, mute);
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));
112 Return<void> Device::getMasterMute(getMasterMute_cb _hidl_cb) {
113 Result retval(Result::NOT_SUPPORTED);
115 if (mDevice->get_master_mute != NULL) {
116 retval = analyzeStatus("get_master_mute", mDevice->get_master_mute(mDevice, &mute));
118 _hidl_cb(retval, mute);
122 Return<void> Device::getInputBufferSize(
123 const AudioConfig& config, getInputBufferSize_cb _hidl_cb) {
124 audio_config_t halConfig;
125 HidlUtils::audioConfigToHal(config, &halConfig);
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) {
131 bufferSize = halBufferSize;
133 _hidl_cb(retval, bufferSize);
137 Return<void> Device::openOutputStream(
139 const DeviceAddress& device,
140 const AudioConfig& config,
141 AudioOutputFlag flags,
142 openOutputStream_cb _hidl_cb) {
143 audio_config_t halConfig;
144 HidlUtils::audioConfigToHal(config, &halConfig);
145 audio_stream_out_t *halStream;
146 ALOGV("open_output_stream handle: %d devices: %x flags: %#x "
147 "srate: %d format %#x channels %x address %s",
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());
152 int status = mDevice->open_output_stream(
155 static_cast<audio_devices_t>(device.device),
156 static_cast<audio_output_flags_t>(flags),
159 deviceAddressToHal(device).c_str());
160 ALOGV("open_output_stream status %d stream %p", status, halStream);
161 sp<IStreamOut> streamOut;
163 streamOut = new StreamOut(mDevice, halStream);
165 AudioConfig suggestedConfig;
166 HidlUtils::audioConfigFromHal(halConfig, &suggestedConfig);
167 _hidl_cb(analyzeStatus("open_output_stream", status), streamOut, suggestedConfig);
171 Return<void> Device::openInputStream(
173 const DeviceAddress& device,
174 const AudioConfig& config,
175 AudioInputFlag flags,
177 openInputStream_cb _hidl_cb) {
178 audio_config_t halConfig;
179 HidlUtils::audioConfigToHal(config, &halConfig);
180 audio_stream_in_t *halStream;
181 ALOGV("open_input_stream handle: %d devices: %x flags: %#x "
182 "srate: %d format %#x channels %x address %s source %d",
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));
187 int status = mDevice->open_input_stream(
190 static_cast<audio_devices_t>(device.device),
193 static_cast<audio_input_flags_t>(flags),
194 deviceAddressToHal(device).c_str(),
195 static_cast<audio_source_t>(source));
196 ALOGV("open_input_stream status %d stream %p", status, halStream);
197 sp<IStreamIn> streamIn;
199 streamIn = new StreamIn(mDevice, halStream);
201 AudioConfig suggestedConfig;
202 HidlUtils::audioConfigFromHal(halConfig, &suggestedConfig);
203 _hidl_cb(analyzeStatus("open_input_stream", status), streamIn, suggestedConfig);
207 Return<bool> Device::supportsAudioPatches() {
208 return version() >= AUDIO_DEVICE_API_VERSION_3_0;
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) {
218 std::unique_ptr<audio_port_config[]> halSources(HidlUtils::audioPortConfigsToHal(sources));
219 std::unique_ptr<audio_port_config[]> halSinks(HidlUtils::audioPortConfigsToHal(sinks));
220 audio_patch_handle_t halPatch;
221 retval = analyzeStatus(
222 "create_audio_patch",
223 mDevice->create_audio_patch(
225 sources.size(), &halSources[0],
226 sinks.size(), &halSinks[0],
228 if (retval == Result::OK) {
229 patch = static_cast<AudioPatchHandle>(halPatch);
232 _hidl_cb(retval, patch);
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)));
242 return Result::NOT_SUPPORTED;
245 Return<void> Device::getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb) {
247 HidlUtils::audioPortToHal(port, &halPort);
248 Result retval = analyzeStatus("get_audio_port", mDevice->get_audio_port(mDevice, &halPort));
249 AudioPort resultPort = port;
250 if (retval == Result::OK) {
251 HidlUtils::audioPortFromHal(halPort, &resultPort);
253 _hidl_cb(retval, resultPort);
257 Return<Result> Device::setAudioPortConfig(const AudioPortConfig& config) {
258 if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
259 struct audio_port_config halPortConfig;
260 HidlUtils::audioPortConfigToHal(config, &halPortConfig);
261 return analyzeStatus(
262 "set_audio_port_config", mDevice->set_audio_port_config(mDevice, &halPortConfig));
264 return Result::NOT_SUPPORTED;
267 Return<AudioHwSync> Device::getHwAvSync() {
269 Result retval = getParam(AudioParameter::keyHwAvSync, &halHwAvSync);
270 return retval == Result::OK ? halHwAvSync : AUDIO_HW_SYNC_INVALID;
273 Return<Result> Device::setScreenState(bool turnedOn) {
274 return setParam(AudioParameter::keyScreenState, turnedOn);
277 Return<void> Device::getParameters(const hidl_vec<hidl_string>& keys, getParameters_cb _hidl_cb) {
278 getParametersImpl(keys, _hidl_cb);
282 Return<Result> Device::setParameters(const hidl_vec<ParameterValue>& parameters) {
283 return setParametersImpl(parameters);
286 Return<void> Device::debugDump(const hidl_handle& fd) {
287 if (fd->numFds == 1) {
288 analyzeStatus("dump", mDevice->dump(mDevice, fd->data[0]));
293 } // namespace implementation
296 } // namespace hardware
297 } // namespace android