1 /*
2 * Copyright (C) 2018 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"
18
19 #include "core/default/Device.h"
20 #include <HidlUtils.h>
21 #include "common/all-versions/default/EffectMap.h"
22 #include "core/default/Conversions.h"
23 #include "core/default/StreamIn.h"
24 #include "core/default/StreamOut.h"
25 #include "core/default/Util.h"
26
27 //#define LOG_NDEBUG 0
28
29 #include <inttypes.h>
30 #include <memory.h>
31 #include <string.h>
32 #include <algorithm>
33
34 #include <android/log.h>
35
36 namespace android {
37 namespace hardware {
38 namespace audio {
39 namespace CPP_VERSION {
40 namespace implementation {
41
42 using ::android::hardware::audio::common::CPP_VERSION::implementation::HidlUtils;
43
Device(audio_hw_device_t * device)44 Device::Device(audio_hw_device_t* device) : mIsClosed(false), mDevice(device) {}
45
~Device()46 Device::~Device() {
47 (void)doClose();
48 mDevice = nullptr;
49 }
50
analyzeStatus(const char * funcName,int status,const std::vector<int> & ignoreErrors)51 Result Device::analyzeStatus(const char* funcName, int status,
52 const std::vector<int>& ignoreErrors) {
53 return util::analyzeStatus("Device", funcName, status, ignoreErrors);
54 }
55
closeInputStream(audio_stream_in_t * stream)56 void Device::closeInputStream(audio_stream_in_t* stream) {
57 mDevice->close_input_stream(mDevice, stream);
58 LOG_ALWAYS_FATAL_IF(mOpenedStreamsCount == 0, "mOpenedStreamsCount is already 0");
59 --mOpenedStreamsCount;
60 }
61
closeOutputStream(audio_stream_out_t * stream)62 void Device::closeOutputStream(audio_stream_out_t* stream) {
63 mDevice->close_output_stream(mDevice, stream);
64 LOG_ALWAYS_FATAL_IF(mOpenedStreamsCount == 0, "mOpenedStreamsCount is already 0");
65 --mOpenedStreamsCount;
66 }
67
halGetParameters(const char * keys)68 char* Device::halGetParameters(const char* keys) {
69 return mDevice->get_parameters(mDevice, keys);
70 }
71
halSetParameters(const char * keysAndValues)72 int Device::halSetParameters(const char* keysAndValues) {
73 return mDevice->set_parameters(mDevice, keysAndValues);
74 }
75
76 // Methods from ::android::hardware::audio::CPP_VERSION::IDevice follow.
initCheck()77 Return<Result> Device::initCheck() {
78 return analyzeStatus("init_check", mDevice->init_check(mDevice));
79 }
80
setMasterVolume(float volume)81 Return<Result> Device::setMasterVolume(float volume) {
82 if (mDevice->set_master_volume == NULL) {
83 return Result::NOT_SUPPORTED;
84 }
85 if (!isGainNormalized(volume)) {
86 ALOGW("Can not set a master volume (%f) outside [0,1]", volume);
87 return Result::INVALID_ARGUMENTS;
88 }
89 return analyzeStatus("set_master_volume", mDevice->set_master_volume(mDevice, volume),
90 {ENOSYS} /*ignore*/);
91 }
92
getMasterVolume(getMasterVolume_cb _hidl_cb)93 Return<void> Device::getMasterVolume(getMasterVolume_cb _hidl_cb) {
94 Result retval(Result::NOT_SUPPORTED);
95 float volume = 0;
96 if (mDevice->get_master_volume != NULL) {
97 retval = analyzeStatus("get_master_volume", mDevice->get_master_volume(mDevice, &volume),
98 {ENOSYS} /*ignore*/);
99 }
100 _hidl_cb(retval, volume);
101 return Void();
102 }
103
setMicMute(bool mute)104 Return<Result> Device::setMicMute(bool mute) {
105 return analyzeStatus("set_mic_mute", mDevice->set_mic_mute(mDevice, mute), {ENOSYS} /*ignore*/);
106 }
107
getMicMute(getMicMute_cb _hidl_cb)108 Return<void> Device::getMicMute(getMicMute_cb _hidl_cb) {
109 bool mute = false;
110 Result retval = analyzeStatus("get_mic_mute", mDevice->get_mic_mute(mDevice, &mute),
111 {ENOSYS} /*ignore*/);
112 _hidl_cb(retval, mute);
113 return Void();
114 }
115
setMasterMute(bool mute)116 Return<Result> Device::setMasterMute(bool mute) {
117 Result retval(Result::NOT_SUPPORTED);
118 if (mDevice->set_master_mute != NULL) {
119 retval = analyzeStatus("set_master_mute", mDevice->set_master_mute(mDevice, mute),
120 {ENOSYS} /*ignore*/);
121 }
122 return retval;
123 }
124
getMasterMute(getMasterMute_cb _hidl_cb)125 Return<void> Device::getMasterMute(getMasterMute_cb _hidl_cb) {
126 Result retval(Result::NOT_SUPPORTED);
127 bool mute = false;
128 if (mDevice->get_master_mute != NULL) {
129 retval = analyzeStatus("get_master_mute", mDevice->get_master_mute(mDevice, &mute),
130 {ENOSYS} /*ignore*/);
131 }
132 _hidl_cb(retval, mute);
133 return Void();
134 }
135
getInputBufferSize(const AudioConfig & config,getInputBufferSize_cb _hidl_cb)136 Return<void> Device::getInputBufferSize(const AudioConfig& config, getInputBufferSize_cb _hidl_cb) {
137 audio_config_t halConfig;
138 HidlUtils::audioConfigToHal(config, &halConfig);
139 size_t halBufferSize = mDevice->get_input_buffer_size(mDevice, &halConfig);
140 Result retval(Result::INVALID_ARGUMENTS);
141 uint64_t bufferSize = 0;
142 if (halBufferSize != 0) {
143 retval = Result::OK;
144 bufferSize = halBufferSize;
145 }
146 _hidl_cb(retval, bufferSize);
147 return Void();
148 }
149
openOutputStreamImpl(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioOutputFlagBitfield flags,AudioConfig * suggestedConfig)150 std::tuple<Result, sp<IStreamOut>> Device::openOutputStreamImpl(int32_t ioHandle,
151 const DeviceAddress& device,
152 const AudioConfig& config,
153 AudioOutputFlagBitfield flags,
154 AudioConfig* suggestedConfig) {
155 audio_config_t halConfig;
156 HidlUtils::audioConfigToHal(config, &halConfig);
157 audio_stream_out_t* halStream;
158 ALOGV(
159 "open_output_stream handle: %d devices: %x flags: %#x "
160 "srate: %d format %#x channels %x address %s",
161 ioHandle, static_cast<audio_devices_t>(device.device),
162 static_cast<audio_output_flags_t>(flags), halConfig.sample_rate, halConfig.format,
163 halConfig.channel_mask, deviceAddressToHal(device).c_str());
164 int status =
165 mDevice->open_output_stream(mDevice, ioHandle, static_cast<audio_devices_t>(device.device),
166 static_cast<audio_output_flags_t>(flags), &halConfig,
167 &halStream, deviceAddressToHal(device).c_str());
168 ALOGV("open_output_stream status %d stream %p", status, halStream);
169 sp<IStreamOut> streamOut;
170 if (status == OK) {
171 streamOut = new StreamOut(this, halStream);
172 ++mOpenedStreamsCount;
173 }
174 status_t convertStatus = HidlUtils::audioConfigFromHal(halConfig, suggestedConfig);
175 ALOGW_IF(convertStatus != OK, "%s: suggested config with incompatible fields", __func__);
176 return {analyzeStatus("open_output_stream", status, {EINVAL} /*ignore*/), streamOut};
177 }
178
openInputStreamImpl(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioInputFlagBitfield flags,AudioSource source,AudioConfig * suggestedConfig)179 std::tuple<Result, sp<IStreamIn>> Device::openInputStreamImpl(
180 int32_t ioHandle, const DeviceAddress& device, const AudioConfig& config,
181 AudioInputFlagBitfield flags, AudioSource source, AudioConfig* suggestedConfig) {
182 audio_config_t halConfig;
183 HidlUtils::audioConfigToHal(config, &halConfig);
184 audio_stream_in_t* halStream;
185 ALOGV(
186 "open_input_stream handle: %d devices: %x flags: %#x "
187 "srate: %d format %#x channels %x address %s source %d",
188 ioHandle, static_cast<audio_devices_t>(device.device),
189 static_cast<audio_input_flags_t>(flags), halConfig.sample_rate, halConfig.format,
190 halConfig.channel_mask, deviceAddressToHal(device).c_str(),
191 static_cast<audio_source_t>(source));
192 int status = mDevice->open_input_stream(
193 mDevice, ioHandle, static_cast<audio_devices_t>(device.device), &halConfig, &halStream,
194 static_cast<audio_input_flags_t>(flags), 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;
198 if (status == OK) {
199 streamIn = new StreamIn(this, halStream);
200 ++mOpenedStreamsCount;
201 }
202 status_t convertStatus = HidlUtils::audioConfigFromHal(halConfig, suggestedConfig);
203 ALOGW_IF(convertStatus != OK, "%s: suggested config with incompatible fields", __func__);
204 return {analyzeStatus("open_input_stream", status, {EINVAL} /*ignore*/), streamIn};
205 }
206
207 #if MAJOR_VERSION == 2
openOutputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioOutputFlagBitfield flags,openOutputStream_cb _hidl_cb)208 Return<void> Device::openOutputStream(int32_t ioHandle, const DeviceAddress& device,
209 const AudioConfig& config, AudioOutputFlagBitfield flags,
210 openOutputStream_cb _hidl_cb) {
211 AudioConfig suggestedConfig;
212 auto [result, streamOut] =
213 openOutputStreamImpl(ioHandle, device, config, flags, &suggestedConfig);
214 _hidl_cb(result, streamOut, suggestedConfig);
215 return Void();
216 }
217
openInputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioInputFlagBitfield flags,AudioSource source,openInputStream_cb _hidl_cb)218 Return<void> Device::openInputStream(int32_t ioHandle, const DeviceAddress& device,
219 const AudioConfig& config, AudioInputFlagBitfield flags,
220 AudioSource source, openInputStream_cb _hidl_cb) {
221 AudioConfig suggestedConfig;
222 auto [result, streamIn] =
223 openInputStreamImpl(ioHandle, device, config, flags, source, &suggestedConfig);
224 _hidl_cb(result, streamIn, suggestedConfig);
225 return Void();
226 }
227
228 #elif MAJOR_VERSION >= 4
openOutputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioOutputFlagBitfield flags,const SourceMetadata & sourceMetadata,openOutputStream_cb _hidl_cb)229 Return<void> Device::openOutputStream(int32_t ioHandle, const DeviceAddress& device,
230 const AudioConfig& config, AudioOutputFlagBitfield flags,
231 const SourceMetadata& sourceMetadata,
232 openOutputStream_cb _hidl_cb) {
233 AudioConfig suggestedConfig;
234 auto [result, streamOut] =
235 openOutputStreamImpl(ioHandle, device, config, flags, &suggestedConfig);
236 if (streamOut) {
237 streamOut->updateSourceMetadata(sourceMetadata);
238 }
239 _hidl_cb(result, streamOut, suggestedConfig);
240 return Void();
241 }
242
openInputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioInputFlagBitfield flags,const SinkMetadata & sinkMetadata,openInputStream_cb _hidl_cb)243 Return<void> Device::openInputStream(int32_t ioHandle, const DeviceAddress& device,
244 const AudioConfig& config, AudioInputFlagBitfield flags,
245 const SinkMetadata& sinkMetadata,
246 openInputStream_cb _hidl_cb) {
247 if (sinkMetadata.tracks.size() == 0) {
248 // This should never happen, the framework must not create as stream
249 // if there is no client
250 ALOGE("openInputStream called without tracks connected");
251 _hidl_cb(Result::INVALID_ARGUMENTS, nullptr, AudioConfig());
252 return Void();
253 }
254 // Pick the first one as the main.
255 AudioSource source = sinkMetadata.tracks[0].source;
256 AudioConfig suggestedConfig;
257 auto [result, streamIn] =
258 openInputStreamImpl(ioHandle, device, config, flags, source, &suggestedConfig);
259 if (streamIn) {
260 streamIn->updateSinkMetadata(sinkMetadata);
261 }
262 _hidl_cb(result, streamIn, suggestedConfig);
263 return Void();
264 }
265 #endif /* MAJOR_VERSION */
266
supportsAudioPatches()267 Return<bool> Device::supportsAudioPatches() {
268 return version() >= AUDIO_DEVICE_API_VERSION_3_0;
269 }
270
createAudioPatch(const hidl_vec<AudioPortConfig> & sources,const hidl_vec<AudioPortConfig> & sinks,createAudioPatch_cb _hidl_cb)271 Return<void> Device::createAudioPatch(const hidl_vec<AudioPortConfig>& sources,
272 const hidl_vec<AudioPortConfig>& sinks,
273 createAudioPatch_cb _hidl_cb) {
274 auto [retval, patch] = createOrUpdateAudioPatch(
275 static_cast<AudioPatchHandle>(AudioHandleConsts::AUDIO_PATCH_HANDLE_NONE), sources,
276 sinks);
277 _hidl_cb(retval, patch);
278 return Void();
279 }
280
createOrUpdateAudioPatch(AudioPatchHandle patch,const hidl_vec<AudioPortConfig> & sources,const hidl_vec<AudioPortConfig> & sinks)281 std::tuple<Result, AudioPatchHandle> Device::createOrUpdateAudioPatch(
282 AudioPatchHandle patch, const hidl_vec<AudioPortConfig>& sources,
283 const hidl_vec<AudioPortConfig>& sinks) {
284 Result retval(Result::NOT_SUPPORTED);
285 if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
286 std::unique_ptr<audio_port_config[]> halSources(HidlUtils::audioPortConfigsToHal(sources));
287 std::unique_ptr<audio_port_config[]> halSinks(HidlUtils::audioPortConfigsToHal(sinks));
288 audio_patch_handle_t halPatch = static_cast<audio_patch_handle_t>(patch);
289 retval = analyzeStatus("create_audio_patch",
290 mDevice->create_audio_patch(mDevice, sources.size(), &halSources[0],
291 sinks.size(), &halSinks[0], &halPatch));
292 if (retval == Result::OK) {
293 patch = static_cast<AudioPatchHandle>(halPatch);
294 }
295 }
296 return {retval, patch};
297 }
298
releaseAudioPatch(int32_t patch)299 Return<Result> Device::releaseAudioPatch(int32_t patch) {
300 if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
301 return analyzeStatus(
302 "release_audio_patch",
303 mDevice->release_audio_patch(mDevice, static_cast<audio_patch_handle_t>(patch)));
304 }
305 return Result::NOT_SUPPORTED;
306 }
307
getAudioPort(const AudioPort & port,getAudioPort_cb _hidl_cb)308 Return<void> Device::getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb) {
309 audio_port halPort;
310 HidlUtils::audioPortToHal(port, &halPort);
311 Result retval = analyzeStatus("get_audio_port", mDevice->get_audio_port(mDevice, &halPort));
312 AudioPort resultPort = port;
313 if (retval == Result::OK) {
314 HidlUtils::audioPortFromHal(halPort, &resultPort);
315 }
316 _hidl_cb(retval, resultPort);
317 return Void();
318 }
319
setAudioPortConfig(const AudioPortConfig & config)320 Return<Result> Device::setAudioPortConfig(const AudioPortConfig& config) {
321 if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
322 struct audio_port_config halPortConfig;
323 HidlUtils::audioPortConfigToHal(config, &halPortConfig);
324 return analyzeStatus("set_audio_port_config",
325 mDevice->set_audio_port_config(mDevice, &halPortConfig));
326 }
327 return Result::NOT_SUPPORTED;
328 }
329
330 #if MAJOR_VERSION == 2
getHwAvSync()331 Return<AudioHwSync> Device::getHwAvSync() {
332 int halHwAvSync;
333 Result retval = getParam(AudioParameter::keyHwAvSync, &halHwAvSync);
334 return retval == Result::OK ? halHwAvSync : AUDIO_HW_SYNC_INVALID;
335 }
336 #elif MAJOR_VERSION >= 4
getHwAvSync(getHwAvSync_cb _hidl_cb)337 Return<void> Device::getHwAvSync(getHwAvSync_cb _hidl_cb) {
338 int halHwAvSync;
339 Result retval = getParam(AudioParameter::keyHwAvSync, &halHwAvSync);
340 _hidl_cb(retval, halHwAvSync);
341 return Void();
342 }
343 #endif
344
setScreenState(bool turnedOn)345 Return<Result> Device::setScreenState(bool turnedOn) {
346 return setParam(AudioParameter::keyScreenState, turnedOn);
347 }
348
349 #if MAJOR_VERSION == 2
getParameters(const hidl_vec<hidl_string> & keys,getParameters_cb _hidl_cb)350 Return<void> Device::getParameters(const hidl_vec<hidl_string>& keys, getParameters_cb _hidl_cb) {
351 getParametersImpl({}, keys, _hidl_cb);
352 return Void();
353 }
354
setParameters(const hidl_vec<ParameterValue> & parameters)355 Return<Result> Device::setParameters(const hidl_vec<ParameterValue>& parameters) {
356 return setParametersImpl({} /* context */, parameters);
357 }
358 #elif MAJOR_VERSION >= 4
getParameters(const hidl_vec<ParameterValue> & context,const hidl_vec<hidl_string> & keys,getParameters_cb _hidl_cb)359 Return<void> Device::getParameters(const hidl_vec<ParameterValue>& context,
360 const hidl_vec<hidl_string>& keys, getParameters_cb _hidl_cb) {
361 getParametersImpl(context, keys, _hidl_cb);
362 return Void();
363 }
setParameters(const hidl_vec<ParameterValue> & context,const hidl_vec<ParameterValue> & parameters)364 Return<Result> Device::setParameters(const hidl_vec<ParameterValue>& context,
365 const hidl_vec<ParameterValue>& parameters) {
366 return setParametersImpl(context, parameters);
367 }
368 #endif
369
370 #if MAJOR_VERSION == 2
debugDump(const hidl_handle & fd)371 Return<void> Device::debugDump(const hidl_handle& fd) {
372 return debug(fd, {});
373 }
374 #endif
375
debug(const hidl_handle & fd,const hidl_vec<hidl_string> &)376 Return<void> Device::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& /* options */) {
377 if (fd.getNativeHandle() != nullptr && fd->numFds == 1) {
378 analyzeStatus("dump", mDevice->dump(mDevice, fd->data[0]));
379 }
380 return Void();
381 }
382
383 #if MAJOR_VERSION >= 4
getMicrophones(getMicrophones_cb _hidl_cb)384 Return<void> Device::getMicrophones(getMicrophones_cb _hidl_cb) {
385 Result retval = Result::NOT_SUPPORTED;
386 size_t actual_mics = AUDIO_MICROPHONE_MAX_COUNT;
387 audio_microphone_characteristic_t mic_array[AUDIO_MICROPHONE_MAX_COUNT];
388
389 hidl_vec<MicrophoneInfo> microphones;
390 if (mDevice->get_microphones != NULL &&
391 mDevice->get_microphones(mDevice, &mic_array[0], &actual_mics) == 0) {
392 microphones.resize(actual_mics);
393 for (size_t i = 0; i < actual_mics; ++i) {
394 halToMicrophoneCharacteristics(µphones[i], mic_array[i]);
395 }
396 retval = Result::OK;
397 }
398 _hidl_cb(retval, microphones);
399 return Void();
400 }
401
setConnectedState(const DeviceAddress & address,bool connected)402 Return<Result> Device::setConnectedState(const DeviceAddress& address, bool connected) {
403 auto key = connected ? AudioParameter::keyDeviceConnect : AudioParameter::keyDeviceDisconnect;
404 return setParam(key, address);
405 }
406 #endif
407
doClose()408 Result Device::doClose() {
409 if (mIsClosed || mOpenedStreamsCount != 0) return Result::INVALID_STATE;
410 mIsClosed = true;
411 return analyzeStatus("close", audio_hw_device_close(mDevice));
412 }
413
414 #if MAJOR_VERSION >= 6
close()415 Return<Result> Device::close() {
416 return doClose();
417 }
418
addDeviceEffect(AudioPortHandle device,uint64_t effectId)419 Return<Result> Device::addDeviceEffect(AudioPortHandle device, uint64_t effectId) {
420 if (version() < AUDIO_DEVICE_API_VERSION_3_1 || mDevice->add_device_effect == nullptr) {
421 return Result::NOT_SUPPORTED;
422 }
423
424 effect_handle_t halEffect = EffectMap::getInstance().get(effectId);
425 if (halEffect != NULL) {
426 return analyzeStatus("add_device_effect",
427 mDevice->add_device_effect(
428 mDevice, static_cast<audio_port_handle_t>(device), halEffect));
429 } else {
430 ALOGW("%s Invalid effect ID passed from client: %" PRIu64 "", __func__, effectId);
431 return Result::INVALID_ARGUMENTS;
432 }
433 }
434
removeDeviceEffect(AudioPortHandle device,uint64_t effectId)435 Return<Result> Device::removeDeviceEffect(AudioPortHandle device, uint64_t effectId) {
436 if (version() < AUDIO_DEVICE_API_VERSION_3_1 || mDevice->remove_device_effect == nullptr) {
437 return Result::NOT_SUPPORTED;
438 }
439
440 effect_handle_t halEffect = EffectMap::getInstance().get(effectId);
441 if (halEffect != NULL) {
442 return analyzeStatus("remove_device_effect",
443 mDevice->remove_device_effect(
444 mDevice, static_cast<audio_port_handle_t>(device), halEffect));
445 } else {
446 ALOGW("%s Invalid effect ID passed from client: %" PRIu64 "", __func__, effectId);
447 return Result::INVALID_ARGUMENTS;
448 }
449 }
450
updateAudioPatch(int32_t previousPatch,const hidl_vec<AudioPortConfig> & sources,const hidl_vec<AudioPortConfig> & sinks,createAudioPatch_cb _hidl_cb)451 Return<void> Device::updateAudioPatch(int32_t previousPatch,
452 const hidl_vec<AudioPortConfig>& sources,
453 const hidl_vec<AudioPortConfig>& sinks,
454 createAudioPatch_cb _hidl_cb) {
455 if (previousPatch != static_cast<int32_t>(AudioHandleConsts::AUDIO_PATCH_HANDLE_NONE)) {
456 auto [retval, patch] = createOrUpdateAudioPatch(previousPatch, sources, sinks);
457 _hidl_cb(retval, patch);
458 } else {
459 _hidl_cb(Result::INVALID_ARGUMENTS, previousPatch);
460 }
461 return Void();
462 }
463
464 #endif
465
466 } // namespace implementation
467 } // namespace CPP_VERSION
468 } // namespace audio
469 } // namespace hardware
470 } // namespace android
471