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 "PrimaryDeviceHAL"
18
19 #include "core/default/PrimaryDevice.h"
20 #include "core/default/Util.h"
21
22 #if MAJOR_VERSION >= 4
23 #include <cmath>
24 #endif
25
26 namespace android {
27 namespace hardware {
28 namespace audio {
29 namespace CPP_VERSION {
30 namespace implementation {
31
PrimaryDevice(audio_hw_device_t * device)32 PrimaryDevice::PrimaryDevice(audio_hw_device_t* device) : mDevice(new Device(device)) {}
33
~PrimaryDevice()34 PrimaryDevice::~PrimaryDevice() {
35 // Do not call mDevice->close here. If there are any unclosed streams,
36 // they only hold IDevice instance, not IPrimaryDevice, thus IPrimaryDevice
37 // "part" of a device can be destroyed before the streams.
38 }
39
40 // Methods from ::android::hardware::audio::CPP_VERSION::IDevice follow.
initCheck()41 Return<Result> PrimaryDevice::initCheck() {
42 return mDevice->initCheck();
43 }
44
setMasterVolume(float volume)45 Return<Result> PrimaryDevice::setMasterVolume(float volume) {
46 return mDevice->setMasterVolume(volume);
47 }
48
getMasterVolume(getMasterVolume_cb _hidl_cb)49 Return<void> PrimaryDevice::getMasterVolume(getMasterVolume_cb _hidl_cb) {
50 return mDevice->getMasterVolume(_hidl_cb);
51 }
52
setMicMute(bool mute)53 Return<Result> PrimaryDevice::setMicMute(bool mute) {
54 return mDevice->setMicMute(mute);
55 }
56
getMicMute(getMicMute_cb _hidl_cb)57 Return<void> PrimaryDevice::getMicMute(getMicMute_cb _hidl_cb) {
58 return mDevice->getMicMute(_hidl_cb);
59 }
60
setMasterMute(bool mute)61 Return<Result> PrimaryDevice::setMasterMute(bool mute) {
62 return mDevice->setMasterMute(mute);
63 }
64
getMasterMute(getMasterMute_cb _hidl_cb)65 Return<void> PrimaryDevice::getMasterMute(getMasterMute_cb _hidl_cb) {
66 return mDevice->getMasterMute(_hidl_cb);
67 }
68
getInputBufferSize(const AudioConfig & config,getInputBufferSize_cb _hidl_cb)69 Return<void> PrimaryDevice::getInputBufferSize(const AudioConfig& config,
70 getInputBufferSize_cb _hidl_cb) {
71 return mDevice->getInputBufferSize(config, _hidl_cb);
72 }
73
74 #if MAJOR_VERSION == 2
openOutputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioOutputFlags flags,openOutputStream_cb _hidl_cb)75 Return<void> PrimaryDevice::openOutputStream(int32_t ioHandle, const DeviceAddress& device,
76 const AudioConfig& config, AudioOutputFlags flags,
77 openOutputStream_cb _hidl_cb) {
78 return mDevice->openOutputStream(ioHandle, device, config, flags, _hidl_cb);
79 }
80
openInputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioInputFlags flags,AudioSource source,openInputStream_cb _hidl_cb)81 Return<void> PrimaryDevice::openInputStream(int32_t ioHandle, const DeviceAddress& device,
82 const AudioConfig& config, AudioInputFlags flags,
83 AudioSource source, openInputStream_cb _hidl_cb) {
84 return mDevice->openInputStream(ioHandle, device, config, flags, source, _hidl_cb);
85 }
86 #elif MAJOR_VERSION >= 4
openOutputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioOutputFlags flags,const SourceMetadata & sourceMetadata,openOutputStream_cb _hidl_cb)87 Return<void> PrimaryDevice::openOutputStream(int32_t ioHandle, const DeviceAddress& device,
88 const AudioConfig& config,
89 #if MAJOR_VERSION <= 6
90 AudioOutputFlags flags,
91 #else
92 const AudioOutputFlags& flags,
93 #endif
94 const SourceMetadata& sourceMetadata,
95 openOutputStream_cb _hidl_cb) {
96 return mDevice->openOutputStream(ioHandle, device, config, flags, sourceMetadata, _hidl_cb);
97 }
98
openInputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioInputFlags flags,const SinkMetadata & sinkMetadata,openInputStream_cb _hidl_cb)99 Return<void> PrimaryDevice::openInputStream(int32_t ioHandle, const DeviceAddress& device,
100 const AudioConfig& config,
101 #if MAJOR_VERSION <= 6
102 AudioInputFlags flags,
103 #else
104 const AudioInputFlags& flags,
105 #endif
106 const SinkMetadata& sinkMetadata,
107 openInputStream_cb _hidl_cb) {
108 return mDevice->openInputStream(ioHandle, device, config, flags, sinkMetadata, _hidl_cb);
109 }
110 #endif
111
supportsAudioPatches()112 Return<bool> PrimaryDevice::supportsAudioPatches() {
113 return mDevice->supportsAudioPatches();
114 }
115
createAudioPatch(const hidl_vec<AudioPortConfig> & sources,const hidl_vec<AudioPortConfig> & sinks,createAudioPatch_cb _hidl_cb)116 Return<void> PrimaryDevice::createAudioPatch(const hidl_vec<AudioPortConfig>& sources,
117 const hidl_vec<AudioPortConfig>& sinks,
118 createAudioPatch_cb _hidl_cb) {
119 return mDevice->createAudioPatch(sources, sinks, _hidl_cb);
120 }
121
releaseAudioPatch(int32_t patch)122 Return<Result> PrimaryDevice::releaseAudioPatch(int32_t patch) {
123 return mDevice->releaseAudioPatch(patch);
124 }
125
getAudioPort(const AudioPort & port,getAudioPort_cb _hidl_cb)126 Return<void> PrimaryDevice::getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb) {
127 return mDevice->getAudioPort(port, _hidl_cb);
128 }
129
setAudioPortConfig(const AudioPortConfig & config)130 Return<Result> PrimaryDevice::setAudioPortConfig(const AudioPortConfig& config) {
131 return mDevice->setAudioPortConfig(config);
132 }
133
setScreenState(bool turnedOn)134 Return<Result> PrimaryDevice::setScreenState(bool turnedOn) {
135 return mDevice->setScreenState(turnedOn);
136 }
137
138 #if MAJOR_VERSION == 2
getHwAvSync()139 Return<AudioHwSync> PrimaryDevice::getHwAvSync() {
140 return mDevice->getHwAvSync();
141 }
142
getParameters(const hidl_vec<hidl_string> & keys,getParameters_cb _hidl_cb)143 Return<void> PrimaryDevice::getParameters(const hidl_vec<hidl_string>& keys,
144 getParameters_cb _hidl_cb) {
145 return mDevice->getParameters(keys, _hidl_cb);
146 }
147
setParameters(const hidl_vec<ParameterValue> & parameters)148 Return<Result> PrimaryDevice::setParameters(const hidl_vec<ParameterValue>& parameters) {
149 return mDevice->setParameters(parameters);
150 }
151
debugDump(const hidl_handle & fd)152 Return<void> PrimaryDevice::debugDump(const hidl_handle& fd) {
153 return mDevice->debugDump(fd);
154 }
155 #elif MAJOR_VERSION >= 4
getHwAvSync(getHwAvSync_cb _hidl_cb)156 Return<void> PrimaryDevice::getHwAvSync(getHwAvSync_cb _hidl_cb) {
157 return mDevice->getHwAvSync(_hidl_cb);
158 }
getParameters(const hidl_vec<ParameterValue> & context,const hidl_vec<hidl_string> & keys,getParameters_cb _hidl_cb)159 Return<void> PrimaryDevice::getParameters(const hidl_vec<ParameterValue>& context,
160 const hidl_vec<hidl_string>& keys,
161 getParameters_cb _hidl_cb) {
162 return mDevice->getParameters(context, keys, _hidl_cb);
163 }
setParameters(const hidl_vec<ParameterValue> & context,const hidl_vec<ParameterValue> & parameters)164 Return<Result> PrimaryDevice::setParameters(const hidl_vec<ParameterValue>& context,
165 const hidl_vec<ParameterValue>& parameters) {
166 return mDevice->setParameters(context, parameters);
167 }
getMicrophones(getMicrophones_cb _hidl_cb)168 Return<void> PrimaryDevice::getMicrophones(getMicrophones_cb _hidl_cb) {
169 return mDevice->getMicrophones(_hidl_cb);
170 }
setConnectedState(const DeviceAddress & address,bool connected)171 Return<Result> PrimaryDevice::setConnectedState(const DeviceAddress& address, bool connected) {
172 return mDevice->setConnectedState(address, connected);
173 }
174 #endif
175 #if MAJOR_VERSION >= 6
close()176 Return<Result> PrimaryDevice::close() {
177 return mDevice->close();
178 }
179
addDeviceEffect(AudioPortHandle device,uint64_t effectId)180 Return<Result> PrimaryDevice::addDeviceEffect(AudioPortHandle device, uint64_t effectId) {
181 return mDevice->addDeviceEffect(device, effectId);
182 }
183
removeDeviceEffect(AudioPortHandle device,uint64_t effectId)184 Return<Result> PrimaryDevice::removeDeviceEffect(AudioPortHandle device, uint64_t effectId) {
185 return mDevice->removeDeviceEffect(device, effectId);
186 }
187
updateAudioPatch(int32_t previousPatch,const hidl_vec<AudioPortConfig> & sources,const hidl_vec<AudioPortConfig> & sinks,updateAudioPatch_cb _hidl_cb)188 Return<void> PrimaryDevice::updateAudioPatch(int32_t previousPatch,
189 const hidl_vec<AudioPortConfig>& sources,
190 const hidl_vec<AudioPortConfig>& sinks,
191 updateAudioPatch_cb _hidl_cb) {
192 return mDevice->updateAudioPatch(previousPatch, sources, sinks, _hidl_cb);
193 }
194 #endif
195
196 // Methods from ::android::hardware::audio::CPP_VERSION::IPrimaryDevice follow.
setVoiceVolume(float volume)197 Return<Result> PrimaryDevice::setVoiceVolume(float volume) {
198 if (!isGainNormalized(volume)) {
199 ALOGW("Can not set a voice volume (%f) outside [0,1]", volume);
200 return Result::INVALID_ARGUMENTS;
201 }
202 return mDevice->analyzeStatus("set_voice_volume",
203 mDevice->device()->set_voice_volume(mDevice->device(), volume));
204 }
205
setMode(AudioMode mode)206 Return<Result> PrimaryDevice::setMode(AudioMode mode) {
207 // INVALID, CURRENT, CNT, MAX are reserved for internal use.
208 // TODO: remove the values from the HIDL interface
209 switch (mode) {
210 case AudioMode::NORMAL:
211 case AudioMode::RINGTONE:
212 case AudioMode::IN_CALL:
213 case AudioMode::IN_COMMUNICATION:
214 #if MAJOR_VERSION >= 6
215 case AudioMode::CALL_SCREEN:
216 #endif
217 break; // Valid values
218 default:
219 return Result::INVALID_ARGUMENTS;
220 };
221
222 return mDevice->analyzeStatus(
223 "set_mode",
224 mDevice->device()->set_mode(mDevice->device(), static_cast<audio_mode_t>(mode)));
225 }
226
getBtScoNrecEnabled(getBtScoNrecEnabled_cb _hidl_cb)227 Return<void> PrimaryDevice::getBtScoNrecEnabled(getBtScoNrecEnabled_cb _hidl_cb) {
228 bool enabled;
229 Result retval = mDevice->getParam(AudioParameter::keyBtNrec, &enabled);
230 _hidl_cb(retval, enabled);
231 return Void();
232 }
233
setBtScoNrecEnabled(bool enabled)234 Return<Result> PrimaryDevice::setBtScoNrecEnabled(bool enabled) {
235 return mDevice->setParam(AudioParameter::keyBtNrec, enabled);
236 }
237
getBtScoWidebandEnabled(getBtScoWidebandEnabled_cb _hidl_cb)238 Return<void> PrimaryDevice::getBtScoWidebandEnabled(getBtScoWidebandEnabled_cb _hidl_cb) {
239 bool enabled;
240 Result retval = mDevice->getParam(AUDIO_PARAMETER_KEY_BT_SCO_WB, &enabled);
241 _hidl_cb(retval, enabled);
242 return Void();
243 }
244
setBtScoWidebandEnabled(bool enabled)245 Return<Result> PrimaryDevice::setBtScoWidebandEnabled(bool enabled) {
246 return mDevice->setParam(AUDIO_PARAMETER_KEY_BT_SCO_WB, enabled);
247 }
248
convertTtyModeFromHIDL(IPrimaryDevice::TtyMode mode)249 static const char* convertTtyModeFromHIDL(IPrimaryDevice::TtyMode mode) {
250 switch (mode) {
251 case IPrimaryDevice::TtyMode::OFF:
252 return AUDIO_PARAMETER_VALUE_TTY_OFF;
253 case IPrimaryDevice::TtyMode::VCO:
254 return AUDIO_PARAMETER_VALUE_TTY_VCO;
255 case IPrimaryDevice::TtyMode::HCO:
256 return AUDIO_PARAMETER_VALUE_TTY_HCO;
257 case IPrimaryDevice::TtyMode::FULL:
258 return AUDIO_PARAMETER_VALUE_TTY_FULL;
259 default:
260 return nullptr;
261 }
262 }
convertTtyModeToHIDL(const char * halMode)263 static IPrimaryDevice::TtyMode convertTtyModeToHIDL(const char* halMode) {
264 if (strcmp(halMode, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
265 return IPrimaryDevice::TtyMode::OFF;
266 else if (strcmp(halMode, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
267 return IPrimaryDevice::TtyMode::VCO;
268 else if (strcmp(halMode, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
269 return IPrimaryDevice::TtyMode::HCO;
270 else if (strcmp(halMode, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
271 return IPrimaryDevice::TtyMode::FULL;
272 return IPrimaryDevice::TtyMode(-1);
273 }
274
getTtyMode(getTtyMode_cb _hidl_cb)275 Return<void> PrimaryDevice::getTtyMode(getTtyMode_cb _hidl_cb) {
276 String8 halMode;
277 Result retval = mDevice->getParam(AUDIO_PARAMETER_KEY_TTY_MODE, &halMode);
278 if (retval != Result::OK) {
279 _hidl_cb(retval, TtyMode::OFF);
280 return Void();
281 }
282 TtyMode mode = convertTtyModeToHIDL(halMode);
283 if (mode == TtyMode(-1)) {
284 ALOGE("HAL returned invalid TTY value: %s", halMode.c_str());
285 _hidl_cb(Result::INVALID_STATE, TtyMode::OFF);
286 return Void();
287 }
288 _hidl_cb(Result::OK, mode);
289 return Void();
290 }
291
setTtyMode(IPrimaryDevice::TtyMode mode)292 Return<Result> PrimaryDevice::setTtyMode(IPrimaryDevice::TtyMode mode) {
293 const char* modeStr = convertTtyModeFromHIDL(mode);
294 if (modeStr == nullptr) {
295 ALOGW("Can not set an invalid TTY value: %d", mode);
296 return Result::INVALID_ARGUMENTS;
297 }
298 return mDevice->setParam(AUDIO_PARAMETER_KEY_TTY_MODE, modeStr);
299 }
300
getHacEnabled(getHacEnabled_cb _hidl_cb)301 Return<void> PrimaryDevice::getHacEnabled(getHacEnabled_cb _hidl_cb) {
302 bool enabled;
303 Result retval = mDevice->getParam(AUDIO_PARAMETER_KEY_HAC, &enabled);
304 _hidl_cb(retval, enabled);
305 return Void();
306 }
307
setHacEnabled(bool enabled)308 Return<Result> PrimaryDevice::setHacEnabled(bool enabled) {
309 return mDevice->setParam(AUDIO_PARAMETER_KEY_HAC, enabled);
310 }
311
312 #if MAJOR_VERSION >= 4
setBtScoHeadsetDebugName(const hidl_string & name)313 Return<Result> PrimaryDevice::setBtScoHeadsetDebugName(const hidl_string& name) {
314 return mDevice->setParam(AUDIO_PARAMETER_KEY_BT_SCO_HEADSET_NAME, name.c_str());
315 }
getBtHfpEnabled(getBtHfpEnabled_cb _hidl_cb)316 Return<void> PrimaryDevice::getBtHfpEnabled(getBtHfpEnabled_cb _hidl_cb) {
317 bool enabled;
318 Result retval = mDevice->getParam(AUDIO_PARAMETER_KEY_HFP_ENABLE, &enabled);
319 _hidl_cb(retval, enabled);
320 return Void();
321 }
setBtHfpEnabled(bool enabled)322 Return<Result> PrimaryDevice::setBtHfpEnabled(bool enabled) {
323 return mDevice->setParam(AUDIO_PARAMETER_KEY_HFP_ENABLE, enabled);
324 }
setBtHfpSampleRate(uint32_t sampleRateHz)325 Return<Result> PrimaryDevice::setBtHfpSampleRate(uint32_t sampleRateHz) {
326 return mDevice->setParam(AUDIO_PARAMETER_KEY_HFP_SET_SAMPLING_RATE, int(sampleRateHz));
327 }
setBtHfpVolume(float volume)328 Return<Result> PrimaryDevice::setBtHfpVolume(float volume) {
329 if (!isGainNormalized(volume)) {
330 ALOGW("Can not set BT HFP volume (%f) outside [0,1]", volume);
331 return Result::INVALID_ARGUMENTS;
332 }
333 // Map the normalized volume onto the range of [0, 15]
334 return mDevice->setParam(AUDIO_PARAMETER_KEY_HFP_VOLUME,
335 static_cast<int>(std::round(volume * 15)));
336 }
updateRotation(IPrimaryDevice::Rotation rotation)337 Return<Result> PrimaryDevice::updateRotation(IPrimaryDevice::Rotation rotation) {
338 // legacy API expects the rotation in degree
339 return mDevice->setParam(AUDIO_PARAMETER_KEY_ROTATION, int(rotation) * 90);
340 }
341 #endif
342
debug(const hidl_handle & fd,const hidl_vec<hidl_string> & options)343 Return<void> PrimaryDevice::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) {
344 return mDevice->debug(fd, options);
345 }
346
347 } // namespace implementation
348 } // namespace CPP_VERSION
349 } // namespace audio
350 } // namespace hardware
351 } // namespace android
352