blob: 280306dc15ee1c84e2d8746e040231d809975c94 [file] [log] [blame]
Shunkai Yao51202502022-12-12 06:11:46 +00001/*
2 * Copyright (C) 2022 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 "DeviceHalAidl"
Vlad Popa03bd5bc2023-01-17 16:16:51 +010018// #define LOG_NDEBUG 0
Shunkai Yao51202502022-12-12 06:11:46 +000019
Mikhail Naganovfab697c2023-01-11 19:33:13 +000020#include <aidl/android/hardware/audio/core/StreamDescriptor.h>
21#include <error/expected_utils.h>
22#include <media/AidlConversionCppNdk.h>
23#include <media/AidlConversionUtil.h>
Mikhail Naganov31d46652023-01-10 18:29:25 +000024#include <mediautils/TimeCheck.h>
25#include <utils/Log.h>
Shunkai Yao51202502022-12-12 06:11:46 +000026
Mikhail Naganov31d46652023-01-10 18:29:25 +000027#include "DeviceHalAidl.h"
28#include "StreamHalAidl.h"
29
Mikhail Naganovfab697c2023-01-11 19:33:13 +000030using aidl::android::aidl_utils::statusTFromBinderStatus;
31using aidl::android::media::audio::common::AudioMode;
32using aidl::android::media::audio::common::Float;
33using aidl::android::hardware::audio::core::IModule;
34using aidl::android::hardware::audio::core::ITelephony;
35using aidl::android::hardware::audio::core::StreamDescriptor;
Vlad Popa03bd5bc2023-01-17 16:16:51 +010036using aidl::android::hardware::audio::core::sounddose::ISoundDose;
Mikhail Naganov31d46652023-01-10 18:29:25 +000037
38namespace android {
39
40status_t DeviceHalAidl::getSupportedDevices(uint32_t*) {
41 // Obsolete.
42 return INVALID_OPERATION;
Shunkai Yao51202502022-12-12 06:11:46 +000043}
44
45status_t DeviceHalAidl::initCheck() {
Mikhail Naganov31d46652023-01-10 18:29:25 +000046 if (mModule == nullptr) return NO_INIT;
47 // HAL modules are already initialized by the time they are published to the SM.
Shunkai Yao51202502022-12-12 06:11:46 +000048 return OK;
49}
50
51status_t DeviceHalAidl::setVoiceVolume(float volume) {
Mikhail Naganov31d46652023-01-10 18:29:25 +000052 TIME_CHECK();
Mikhail Naganovfab697c2023-01-11 19:33:13 +000053 if (!mModule) return NO_INIT;
54 std::shared_ptr<ITelephony> telephony;
55 if (ndk::ScopedAStatus status = mModule->getTelephony(&telephony);
56 status.isOk() && telephony != nullptr) {
57 ITelephony::TelecomConfig inConfig{ .voiceVolume = Float{volume} }, outConfig;
58 RETURN_STATUS_IF_ERROR(
59 statusTFromBinderStatus(telephony->setTelecomConfig(inConfig, &outConfig)));
60 ALOGW_IF(outConfig.voiceVolume.has_value() && volume != outConfig.voiceVolume.value().value,
61 "%s: the resulting voice volume %f is not the same as requested %f",
62 __func__, outConfig.voiceVolume.value().value, volume);
63 }
64 return INVALID_OPERATION;
Shunkai Yao51202502022-12-12 06:11:46 +000065}
66
67status_t DeviceHalAidl::setMasterVolume(float volume) {
Mikhail Naganov31d46652023-01-10 18:29:25 +000068 TIME_CHECK();
Mikhail Naganovfab697c2023-01-11 19:33:13 +000069 if (!mModule) return NO_INIT;
70 return statusTFromBinderStatus(mModule->setMasterVolume(volume));
Shunkai Yao51202502022-12-12 06:11:46 +000071}
72
73status_t DeviceHalAidl::getMasterVolume(float *volume) {
Mikhail Naganov31d46652023-01-10 18:29:25 +000074 TIME_CHECK();
Mikhail Naganovfab697c2023-01-11 19:33:13 +000075 if (!mModule) return NO_INIT;
76 return statusTFromBinderStatus(mModule->getMasterVolume(volume));
Shunkai Yao51202502022-12-12 06:11:46 +000077}
78
Mikhail Naganovfab697c2023-01-11 19:33:13 +000079status_t DeviceHalAidl::setMode(audio_mode_t mode) {
Mikhail Naganov31d46652023-01-10 18:29:25 +000080 TIME_CHECK();
81 if (!mModule) return NO_INIT;
Mikhail Naganovfab697c2023-01-11 19:33:13 +000082 AudioMode audioMode = VALUE_OR_FATAL(::aidl::android::legacy2aidl_audio_mode_t_AudioMode(mode));
83 std::shared_ptr<ITelephony> telephony;
84 if (ndk::ScopedAStatus status = mModule->getTelephony(&telephony);
85 status.isOk() && telephony != nullptr) {
86 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(telephony->switchAudioMode(audioMode)));
87 }
88 return statusTFromBinderStatus(mModule->updateAudioMode(audioMode));
Shunkai Yao51202502022-12-12 06:11:46 +000089}
90
91status_t DeviceHalAidl::setMicMute(bool state) {
Mikhail Naganov31d46652023-01-10 18:29:25 +000092 TIME_CHECK();
Mikhail Naganovfab697c2023-01-11 19:33:13 +000093 if (!mModule) return NO_INIT;
94 return statusTFromBinderStatus(mModule->setMicMute(state));
Shunkai Yao51202502022-12-12 06:11:46 +000095}
Mikhail Naganov31d46652023-01-10 18:29:25 +000096
Shunkai Yao51202502022-12-12 06:11:46 +000097status_t DeviceHalAidl::getMicMute(bool *state) {
Mikhail Naganov31d46652023-01-10 18:29:25 +000098 TIME_CHECK();
Mikhail Naganovfab697c2023-01-11 19:33:13 +000099 if (!mModule) return NO_INIT;
100 return statusTFromBinderStatus(mModule->getMicMute(state));
Shunkai Yao51202502022-12-12 06:11:46 +0000101}
Mikhail Naganov31d46652023-01-10 18:29:25 +0000102
Shunkai Yao51202502022-12-12 06:11:46 +0000103status_t DeviceHalAidl::setMasterMute(bool state) {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000104 TIME_CHECK();
Mikhail Naganovfab697c2023-01-11 19:33:13 +0000105 if (!mModule) return NO_INIT;
106 return statusTFromBinderStatus(mModule->setMasterMute(state));
Shunkai Yao51202502022-12-12 06:11:46 +0000107}
Mikhail Naganov31d46652023-01-10 18:29:25 +0000108
Shunkai Yao51202502022-12-12 06:11:46 +0000109status_t DeviceHalAidl::getMasterMute(bool *state) {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000110 TIME_CHECK();
Mikhail Naganovfab697c2023-01-11 19:33:13 +0000111 if (!mModule) return NO_INIT;
112 return statusTFromBinderStatus(mModule->getMasterMute(state));
Shunkai Yao51202502022-12-12 06:11:46 +0000113}
114
Mikhail Naganov31d46652023-01-10 18:29:25 +0000115status_t DeviceHalAidl::setParameters(const String8& kvPairs __unused) {
116 TIME_CHECK();
117 if (!mModule) return NO_INIT;
118 ALOGE("%s not implemented yet", __func__);
Shunkai Yao51202502022-12-12 06:11:46 +0000119 return OK;
120}
121
Mikhail Naganov31d46652023-01-10 18:29:25 +0000122status_t DeviceHalAidl::getParameters(const String8& keys __unused, String8 *values) {
123 TIME_CHECK();
124 values->clear();
125 if (!mModule) return NO_INIT;
126 ALOGE("%s not implemented yet", __func__);
Shunkai Yao51202502022-12-12 06:11:46 +0000127 return OK;
128}
129
Mikhail Naganov31d46652023-01-10 18:29:25 +0000130status_t DeviceHalAidl::getInputBufferSize(
131 const struct audio_config* config __unused, size_t* size __unused) {
132 TIME_CHECK();
133 if (!mModule) return NO_INIT;
134 ALOGE("%s not implemented yet", __func__);
Shunkai Yao51202502022-12-12 06:11:46 +0000135 return OK;
136}
137
Mikhail Naganov31d46652023-01-10 18:29:25 +0000138status_t DeviceHalAidl::openOutputStream(
139 audio_io_handle_t handle __unused, audio_devices_t devices __unused,
140 audio_output_flags_t flags __unused, struct audio_config* config,
141 const char* address __unused,
142 sp<StreamOutHalInterface>* outStream) {
143 if (!outStream || !config) {
144 return BAD_VALUE;
145 }
146 TIME_CHECK();
147 if (!mModule) return NO_INIT;
148 config->sample_rate = 48000;
149 config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
150 config->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
151 StreamDescriptor descriptor;
152 descriptor.frameSizeBytes = audio_bytes_per_sample(config->format) *
153 audio_channel_count_from_out_mask(config->channel_mask);
154 descriptor.bufferSizeFrames = 600;
155 *outStream = sp<StreamOutHalAidl>::make(descriptor, nullptr);
Shunkai Yao51202502022-12-12 06:11:46 +0000156 return OK;
157}
158
Mikhail Naganov31d46652023-01-10 18:29:25 +0000159status_t DeviceHalAidl::openInputStream(
160 audio_io_handle_t handle __unused, audio_devices_t devices __unused,
161 struct audio_config* config, audio_input_flags_t flags __unused,
162 const char* address __unused, audio_source_t source __unused,
163 audio_devices_t outputDevice __unused,
164 const char* outputDeviceAddress __unused,
165 sp<StreamInHalInterface>* inStream) {
166 if (!inStream || !config) {
167 return BAD_VALUE;
168 }
169 TIME_CHECK();
170 if (!mModule) return NO_INIT;
171 config->sample_rate = 48000;
172 config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
173 config->channel_mask = AUDIO_CHANNEL_IN_STEREO;
174 StreamDescriptor descriptor;
175 descriptor.frameSizeBytes = audio_bytes_per_sample(config->format) *
176 audio_channel_count_from_out_mask(config->channel_mask);
177 descriptor.bufferSizeFrames = 600;
178 *inStream = sp<StreamInHalAidl>::make(descriptor, nullptr);
Shunkai Yao51202502022-12-12 06:11:46 +0000179 return OK;
180}
181
182status_t DeviceHalAidl::supportsAudioPatches(bool* supportsPatches) {
183 *supportsPatches = true;
184 return OK;
185}
186
Mikhail Naganov31d46652023-01-10 18:29:25 +0000187status_t DeviceHalAidl::createAudioPatch(unsigned int num_sources __unused,
188 const struct audio_port_config* sources __unused,
189 unsigned int num_sinks __unused,
190 const struct audio_port_config* sinks __unused,
191 audio_patch_handle_t* patch __unused) {
192 TIME_CHECK();
193 if (!mModule) return NO_INIT;
194 ALOGE("%s not implemented yet", __func__);
Shunkai Yao51202502022-12-12 06:11:46 +0000195 return OK;
196}
197
Mikhail Naganov31d46652023-01-10 18:29:25 +0000198status_t DeviceHalAidl::releaseAudioPatch(audio_patch_handle_t patch __unused) {
199 TIME_CHECK();
200 if (!mModule) return NO_INIT;
201 ALOGE("%s not implemented yet", __func__);
Shunkai Yao51202502022-12-12 06:11:46 +0000202 return OK;
203}
204
Mikhail Naganov31d46652023-01-10 18:29:25 +0000205status_t DeviceHalAidl::getAudioPort(struct audio_port* port __unused) {
206 TIME_CHECK();
207 ALOGE("%s not implemented yet", __func__);
208 return INVALID_OPERATION;
209}
210
211status_t DeviceHalAidl::getAudioPort(struct audio_port_v7 *port __unused) {
212 TIME_CHECK();
213 ALOGE("%s not implemented yet", __func__);
214 return INVALID_OPERATION;
215}
216
217status_t DeviceHalAidl::setAudioPortConfig(const struct audio_port_config* config __unused) {
218 TIME_CHECK();
219 if (!mModule) return NO_INIT;
220 ALOGE("%s not implemented yet", __func__);
Shunkai Yao51202502022-12-12 06:11:46 +0000221 return OK;
222}
223
224status_t DeviceHalAidl::getMicrophones(
Mikhail Naganov31d46652023-01-10 18:29:25 +0000225 std::vector<audio_microphone_characteristic_t>* microphones __unused) {
226 TIME_CHECK();
227 if (!mModule) return NO_INIT;
228 ALOGE("%s not implemented yet", __func__);
Shunkai Yao51202502022-12-12 06:11:46 +0000229 return OK;
230}
231
Mikhail Naganov31d46652023-01-10 18:29:25 +0000232status_t DeviceHalAidl::addDeviceEffect(audio_port_handle_t device __unused,
233 sp<EffectHalInterface> effect) {
Shunkai Yao51202502022-12-12 06:11:46 +0000234 if (!effect) {
235 return BAD_VALUE;
236 }
Mikhail Naganov31d46652023-01-10 18:29:25 +0000237 TIME_CHECK();
238 if (!mModule) return NO_INIT;
239 ALOGE("%s not implemented yet", __func__);
Shunkai Yao51202502022-12-12 06:11:46 +0000240 return OK;
241}
Mikhail Naganov31d46652023-01-10 18:29:25 +0000242status_t DeviceHalAidl::removeDeviceEffect(audio_port_handle_t device __unused,
Shunkai Yao51202502022-12-12 06:11:46 +0000243 sp<EffectHalInterface> effect) {
244 if (!effect) {
245 return BAD_VALUE;
246 }
Mikhail Naganov31d46652023-01-10 18:29:25 +0000247 TIME_CHECK();
248 if (!mModule) return NO_INIT;
249 ALOGE("%s not implemented yet", __func__);
Shunkai Yao51202502022-12-12 06:11:46 +0000250 return OK;
251}
252
253status_t DeviceHalAidl::getMmapPolicyInfos(
254 media::audio::common::AudioMMapPolicyType policyType __unused,
255 std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos __unused) {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000256 TIME_CHECK();
Shunkai Yao51202502022-12-12 06:11:46 +0000257 ALOGE("%s not implemented yet", __func__);
258 return OK;
259}
260
261int32_t DeviceHalAidl::getAAudioMixerBurstCount() {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000262 TIME_CHECK();
Shunkai Yao51202502022-12-12 06:11:46 +0000263 ALOGE("%s not implemented yet", __func__);
264 return OK;
265}
266
267int32_t DeviceHalAidl::getAAudioHardwareBurstMinUsec() {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000268 TIME_CHECK();
Shunkai Yao51202502022-12-12 06:11:46 +0000269 ALOGE("%s not implemented yet", __func__);
270 return OK;
271}
272
273error::Result<audio_hw_sync_t> DeviceHalAidl::getHwAvSync() {
Mikhail Naganov31d46652023-01-10 18:29:25 +0000274 TIME_CHECK();
Shunkai Yao51202502022-12-12 06:11:46 +0000275 ALOGE("%s not implemented yet", __func__);
276 return base::unexpected(INVALID_OPERATION);
277}
278
Mikhail Naganovfab697c2023-01-11 19:33:13 +0000279status_t DeviceHalAidl::dump(int fd, const Vector<String16>& args) {
280 TIME_CHECK();
281 if (!mModule) return NO_INIT;
282 return mModule->dump(fd, Args(args).args(), args.size());
Shunkai Yao51202502022-12-12 06:11:46 +0000283};
284
Mikhail Naganov31d46652023-01-10 18:29:25 +0000285int32_t DeviceHalAidl::supportsBluetoothVariableLatency(bool* supports __unused) {
286 TIME_CHECK();
Shunkai Yao51202502022-12-12 06:11:46 +0000287 ALOGE("%s not implemented yet", __func__);
288 return INVALID_OPERATION;
289}
Mikhail Naganov31d46652023-01-10 18:29:25 +0000290
Vlad Popa03bd5bc2023-01-17 16:16:51 +0100291status_t DeviceHalAidl::getSoundDoseInterface(const std::string& module,
292 ::ndk::SpAIBinder* soundDoseBinder) {
293 TIME_CHECK();
294 if (!mModule) return NO_INIT;
295 if (mSoundDose == nullptr) {
296 ndk::ScopedAStatus status = mModule->getSoundDose(&mSoundDose);
297 if (!status.isOk()) {
298 ALOGE("%s failed to return the sound dose interface for module %s: %s",
299 __func__,
300 module.c_str(),
301 status.getDescription().c_str());
302 return BAD_VALUE;
303 }
304 }
305 *soundDoseBinder = mSoundDose->asBinder();
306 ALOGI("%s using audio AIDL HAL sound dose interface", __func__);
307
308 return OK;
309}
310
Mikhail Naganov31d46652023-01-10 18:29:25 +0000311} // namespace android