blob: 1384c1e6f7171649fda97fde085af0557ddbecb6 [file] [log] [blame]
Kevin Rocard4bcd67f2018-02-28 14:33:38 -08001/*
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 "DeviceHalLocal"
18//#define LOG_NDEBUG 0
19
20#include <utils/Log.h>
21
22#include "DeviceHalLocal.h"
23#include "StreamHalLocal.h"
24
25namespace android {
26
27DeviceHalLocal::DeviceHalLocal(audio_hw_device_t *dev)
28 : mDev(dev) {
29}
30
31DeviceHalLocal::~DeviceHalLocal() {
32 int status = audio_hw_device_close(mDev);
33 ALOGW_IF(status, "Error closing audio hw device %p: %s", mDev, strerror(-status));
34 mDev = 0;
35}
36
37status_t DeviceHalLocal::getSupportedDevices(uint32_t *devices) {
38 if (mDev->get_supported_devices == NULL) return INVALID_OPERATION;
39 *devices = mDev->get_supported_devices(mDev);
40 return OK;
41}
42
43status_t DeviceHalLocal::initCheck() {
44 return mDev->init_check(mDev);
45}
46
47status_t DeviceHalLocal::setVoiceVolume(float volume) {
48 return mDev->set_voice_volume(mDev, volume);
49}
50
51status_t DeviceHalLocal::setMasterVolume(float volume) {
52 if (mDev->set_master_volume == NULL) return INVALID_OPERATION;
53 return mDev->set_master_volume(mDev, volume);
54}
55
56status_t DeviceHalLocal::getMasterVolume(float *volume) {
57 if (mDev->get_master_volume == NULL) return INVALID_OPERATION;
58 return mDev->get_master_volume(mDev, volume);
59}
60
61status_t DeviceHalLocal::setMode(audio_mode_t mode) {
62 return mDev->set_mode(mDev, mode);
63}
64
65status_t DeviceHalLocal::setMicMute(bool state) {
66 return mDev->set_mic_mute(mDev, state);
67}
68
69status_t DeviceHalLocal::getMicMute(bool *state) {
70 return mDev->get_mic_mute(mDev, state);
71}
72
73status_t DeviceHalLocal::setMasterMute(bool state) {
74 if (mDev->set_master_mute == NULL) return INVALID_OPERATION;
75 return mDev->set_master_mute(mDev, state);
76}
77
78status_t DeviceHalLocal::getMasterMute(bool *state) {
79 if (mDev->get_master_mute == NULL) return INVALID_OPERATION;
80 return mDev->get_master_mute(mDev, state);
81}
82
83status_t DeviceHalLocal::setParameters(const String8& kvPairs) {
84 return mDev->set_parameters(mDev, kvPairs.string());
85}
86
87status_t DeviceHalLocal::getParameters(const String8& keys, String8 *values) {
88 char *halValues = mDev->get_parameters(mDev, keys.string());
89 if (halValues != NULL) {
90 values->setTo(halValues);
91 free(halValues);
92 } else {
93 values->clear();
94 }
95 return OK;
96}
97
98status_t DeviceHalLocal::getInputBufferSize(
99 const struct audio_config *config, size_t *size) {
100 *size = mDev->get_input_buffer_size(mDev, config);
101 return OK;
102}
103
104status_t DeviceHalLocal::openOutputStream(
105 audio_io_handle_t handle,
jiabinc0106832019-10-24 14:58:31 -0700106 audio_devices_t deviceType,
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800107 audio_output_flags_t flags,
108 struct audio_config *config,
109 const char *address,
110 sp<StreamOutHalInterface> *outStream) {
111 audio_stream_out_t *halStream;
112 ALOGV("open_output_stream handle: %d devices: %x flags: %#x"
113 "srate: %d format %#x channels %x address %s",
jiabinc0106832019-10-24 14:58:31 -0700114 handle, deviceType, flags,
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800115 config->sample_rate, config->format, config->channel_mask,
116 address);
117 int openResut = mDev->open_output_stream(
jiabinc0106832019-10-24 14:58:31 -0700118 mDev, handle, deviceType, flags, config, &halStream, address);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800119 if (openResut == OK) {
120 *outStream = new StreamOutHalLocal(halStream, this);
121 }
122 ALOGV("open_output_stream status %d stream %p", openResut, halStream);
123 return openResut;
124}
125
126status_t DeviceHalLocal::openInputStream(
127 audio_io_handle_t handle,
128 audio_devices_t devices,
129 struct audio_config *config,
130 audio_input_flags_t flags,
131 const char *address,
132 audio_source_t source,
Mikhail Naganovb4e037e2019-01-14 15:56:33 -0800133 audio_devices_t /*outputDevice*/,
134 const char */*outputDeviceAddress*/,
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800135 sp<StreamInHalInterface> *inStream) {
136 audio_stream_in_t *halStream;
137 ALOGV("open_input_stream handle: %d devices: %x flags: %#x "
138 "srate: %d format %#x channels %x address %s source %d",
139 handle, devices, flags,
140 config->sample_rate, config->format, config->channel_mask,
141 address, source);
142 int openResult = mDev->open_input_stream(
143 mDev, handle, devices, config, &halStream, flags, address, source);
144 if (openResult == OK) {
145 *inStream = new StreamInHalLocal(halStream, this);
146 }
147 ALOGV("open_input_stream status %d stream %p", openResult, inStream);
148 return openResult;
149}
150
151status_t DeviceHalLocal::supportsAudioPatches(bool *supportsPatches) {
152 *supportsPatches = version() >= AUDIO_DEVICE_API_VERSION_3_0;
153 return OK;
154}
155
156status_t DeviceHalLocal::createAudioPatch(
157 unsigned int num_sources,
158 const struct audio_port_config *sources,
159 unsigned int num_sinks,
160 const struct audio_port_config *sinks,
161 audio_patch_handle_t *patch) {
162 if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
163 return mDev->create_audio_patch(
164 mDev, num_sources, sources, num_sinks, sinks, patch);
165 } else {
166 return INVALID_OPERATION;
167 }
168}
169
170status_t DeviceHalLocal::releaseAudioPatch(audio_patch_handle_t patch) {
171 if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
172 return mDev->release_audio_patch(mDev, patch);
173 } else {
174 return INVALID_OPERATION;
175 }
176}
177
178status_t DeviceHalLocal::getAudioPort(struct audio_port *port) {
179 return mDev->get_audio_port(mDev, port);
180}
181
Mikhail Naganov4f782d92021-03-31 18:35:29 +0000182status_t DeviceHalLocal::getAudioPort(struct audio_port_v7 *port) {
183#if MAJOR_VERSION >= 7
184 if (version() >= AUDIO_DEVICE_API_VERSION_3_2) {
185 // get_audio_port_v7 is mandatory if legacy HAL support this API version.
186 return mDev->get_audio_port_v7(mDev, port);
187 }
188#endif
189 struct audio_port audioPort = {};
190 audio_populate_audio_port(port, &audioPort);
191 status_t status = getAudioPort(&audioPort);
192 if (status == NO_ERROR) {
193 audio_populate_audio_port_v7(&audioPort, port);
194 }
195 return status;
196}
197
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800198status_t DeviceHalLocal::setAudioPortConfig(const struct audio_port_config *config) {
199 if (version() >= AUDIO_DEVICE_API_VERSION_3_0)
200 return mDev->set_audio_port_config(mDev, config);
201 else
202 return INVALID_OPERATION;
203}
204
Kevin Rocard070e7512018-05-22 09:29:13 -0700205#if MAJOR_VERSION == 2
206status_t DeviceHalLocal::getMicrophones(
207 std::vector<media::MicrophoneInfo> *microphones __unused) {
208 return INVALID_OPERATION;
209}
Kevin Rocard3d48dce2018-11-08 17:16:57 -0800210#elif MAJOR_VERSION >= 4
jiabin9ff780e2018-03-19 18:19:52 -0700211status_t DeviceHalLocal::getMicrophones(std::vector<media::MicrophoneInfo> *microphones) {
212 if (mDev->get_microphones == NULL) return INVALID_OPERATION;
213 size_t actual_mics = AUDIO_MICROPHONE_MAX_COUNT;
214 audio_microphone_characteristic_t mic_array[AUDIO_MICROPHONE_MAX_COUNT];
215 status_t status = mDev->get_microphones(mDev, &mic_array[0], &actual_mics);
216 for (size_t i = 0; i < actual_mics; i++) {
217 media::MicrophoneInfo microphoneInfo = media::MicrophoneInfo(mic_array[i]);
218 microphones->push_back(microphoneInfo);
219 }
220 return status;
221}
Kevin Rocard070e7512018-05-22 09:29:13 -0700222#endif
jiabin9ff780e2018-03-19 18:19:52 -0700223
Eric Laurent9b2064c2019-11-22 17:25:04 -0800224// Local HAL implementation does not support effects
225status_t DeviceHalLocal::addDeviceEffect(
226 audio_port_handle_t device __unused, sp<EffectHalInterface> effect __unused) {
227 return INVALID_OPERATION;
228}
229
230status_t DeviceHalLocal::removeDeviceEffect(
231 audio_port_handle_t device __unused, sp<EffectHalInterface> effect __unused) {
232 return INVALID_OPERATION;
233}
234
Andy Hung542ae9b2021-06-16 09:37:53 -0700235status_t DeviceHalLocal::dump(int fd, const Vector<String16>& /* args */) {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800236 return mDev->dump(mDev, fd);
237}
238
239void DeviceHalLocal::closeOutputStream(struct audio_stream_out *stream_out) {
240 mDev->close_output_stream(mDev, stream_out);
241}
242
243void DeviceHalLocal::closeInputStream(struct audio_stream_in *stream_in) {
244 mDev->close_input_stream(mDev, stream_in);
245}
246
Kevin Rocard4bcd67f2018-02-28 14:33:38 -0800247} // namespace android