1 /*
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 
25 namespace android {
26 namespace CPP_VERSION {
27 
DeviceHalLocal(audio_hw_device_t * dev)28 DeviceHalLocal::DeviceHalLocal(audio_hw_device_t *dev)
29         : mDev(dev) {
30 }
31 
~DeviceHalLocal()32 DeviceHalLocal::~DeviceHalLocal() {
33     int status = audio_hw_device_close(mDev);
34     ALOGW_IF(status, "Error closing audio hw device %p: %s", mDev, strerror(-status));
35     mDev = 0;
36 }
37 
getSupportedDevices(uint32_t * devices)38 status_t DeviceHalLocal::getSupportedDevices(uint32_t *devices) {
39     if (mDev->get_supported_devices == NULL) return INVALID_OPERATION;
40     *devices = mDev->get_supported_devices(mDev);
41     return OK;
42 }
43 
initCheck()44 status_t DeviceHalLocal::initCheck() {
45     return mDev->init_check(mDev);
46 }
47 
setVoiceVolume(float volume)48 status_t DeviceHalLocal::setVoiceVolume(float volume) {
49     return mDev->set_voice_volume(mDev, volume);
50 }
51 
setMasterVolume(float volume)52 status_t DeviceHalLocal::setMasterVolume(float volume) {
53     if (mDev->set_master_volume == NULL) return INVALID_OPERATION;
54     return mDev->set_master_volume(mDev, volume);
55 }
56 
getMasterVolume(float * volume)57 status_t DeviceHalLocal::getMasterVolume(float *volume) {
58     if (mDev->get_master_volume == NULL) return INVALID_OPERATION;
59     return mDev->get_master_volume(mDev, volume);
60 }
61 
setMode(audio_mode_t mode)62 status_t DeviceHalLocal::setMode(audio_mode_t mode) {
63     return mDev->set_mode(mDev, mode);
64 }
65 
setMicMute(bool state)66 status_t DeviceHalLocal::setMicMute(bool state) {
67     return mDev->set_mic_mute(mDev, state);
68 }
69 
getMicMute(bool * state)70 status_t DeviceHalLocal::getMicMute(bool *state) {
71     return mDev->get_mic_mute(mDev, state);
72 }
73 
setMasterMute(bool state)74 status_t DeviceHalLocal::setMasterMute(bool state) {
75     if (mDev->set_master_mute == NULL) return INVALID_OPERATION;
76     return mDev->set_master_mute(mDev, state);
77 }
78 
getMasterMute(bool * state)79 status_t DeviceHalLocal::getMasterMute(bool *state) {
80     if (mDev->get_master_mute == NULL) return INVALID_OPERATION;
81     return mDev->get_master_mute(mDev, state);
82 }
83 
setParameters(const String8 & kvPairs)84 status_t DeviceHalLocal::setParameters(const String8& kvPairs) {
85     return mDev->set_parameters(mDev, kvPairs.string());
86 }
87 
getParameters(const String8 & keys,String8 * values)88 status_t DeviceHalLocal::getParameters(const String8& keys, String8 *values) {
89     char *halValues = mDev->get_parameters(mDev, keys.string());
90     if (halValues != NULL) {
91         values->setTo(halValues);
92         free(halValues);
93     } else {
94         values->clear();
95     }
96     return OK;
97 }
98 
getInputBufferSize(const struct audio_config * config,size_t * size)99 status_t DeviceHalLocal::getInputBufferSize(
100         const struct audio_config *config, size_t *size) {
101     *size = mDev->get_input_buffer_size(mDev, config);
102     return OK;
103 }
104 
openOutputStream(audio_io_handle_t handle,audio_devices_t deviceType,audio_output_flags_t flags,struct audio_config * config,const char * address,sp<StreamOutHalInterface> * outStream)105 status_t DeviceHalLocal::openOutputStream(
106         audio_io_handle_t handle,
107         audio_devices_t deviceType,
108         audio_output_flags_t flags,
109         struct audio_config *config,
110         const char *address,
111         sp<StreamOutHalInterface> *outStream) {
112     audio_stream_out_t *halStream;
113     ALOGV("open_output_stream handle: %d devices: %x flags: %#x"
114             "srate: %d format %#x channels %x address %s",
115             handle, deviceType, flags,
116             config->sample_rate, config->format, config->channel_mask,
117             address);
118     int openResut = mDev->open_output_stream(
119             mDev, handle, deviceType, flags, config, &halStream, address);
120     if (openResut == OK) {
121         *outStream = new StreamOutHalLocal(halStream, this);
122     }
123     ALOGV("open_output_stream status %d stream %p", openResut, halStream);
124     return openResut;
125 }
126 
openInputStream(audio_io_handle_t handle,audio_devices_t devices,struct audio_config * config,audio_input_flags_t flags,const char * address,audio_source_t source,audio_devices_t,const char *,sp<StreamInHalInterface> * inStream)127 status_t DeviceHalLocal::openInputStream(
128         audio_io_handle_t handle,
129         audio_devices_t devices,
130         struct audio_config *config,
131         audio_input_flags_t flags,
132         const char *address,
133         audio_source_t source,
134         audio_devices_t /*outputDevice*/,
135         const char */*outputDeviceAddress*/,
136         sp<StreamInHalInterface> *inStream) {
137     audio_stream_in_t *halStream;
138     ALOGV("open_input_stream handle: %d devices: %x flags: %#x "
139             "srate: %d format %#x channels %x address %s source %d",
140             handle, devices, flags,
141             config->sample_rate, config->format, config->channel_mask,
142             address, source);
143     int openResult = mDev->open_input_stream(
144             mDev, handle, devices, config, &halStream, flags, address, source);
145     if (openResult == OK) {
146         *inStream = new StreamInHalLocal(halStream, this);
147     }
148     ALOGV("open_input_stream status %d stream %p", openResult, inStream);
149     return openResult;
150 }
151 
supportsAudioPatches(bool * supportsPatches)152 status_t DeviceHalLocal::supportsAudioPatches(bool *supportsPatches) {
153     *supportsPatches = version() >= AUDIO_DEVICE_API_VERSION_3_0;
154     return OK;
155 }
156 
createAudioPatch(unsigned int num_sources,const struct audio_port_config * sources,unsigned int num_sinks,const struct audio_port_config * sinks,audio_patch_handle_t * patch)157 status_t DeviceHalLocal::createAudioPatch(
158         unsigned int num_sources,
159         const struct audio_port_config *sources,
160         unsigned int num_sinks,
161         const struct audio_port_config *sinks,
162         audio_patch_handle_t *patch) {
163     if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
164         return mDev->create_audio_patch(
165                 mDev, num_sources, sources, num_sinks, sinks, patch);
166     } else {
167         return INVALID_OPERATION;
168     }
169 }
170 
releaseAudioPatch(audio_patch_handle_t patch)171 status_t DeviceHalLocal::releaseAudioPatch(audio_patch_handle_t patch) {
172     if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
173         return mDev->release_audio_patch(mDev, patch);
174     } else {
175         return INVALID_OPERATION;
176     }
177 }
178 
getAudioPort(struct audio_port * port)179 status_t DeviceHalLocal::getAudioPort(struct audio_port *port) {
180     return mDev->get_audio_port(mDev, port);
181 }
182 
setAudioPortConfig(const struct audio_port_config * config)183 status_t DeviceHalLocal::setAudioPortConfig(const struct audio_port_config *config) {
184     if (version() >= AUDIO_DEVICE_API_VERSION_3_0)
185         return mDev->set_audio_port_config(mDev, config);
186     else
187         return INVALID_OPERATION;
188 }
189 
190 #if MAJOR_VERSION == 2
getMicrophones(std::vector<media::MicrophoneInfo> * microphones __unused)191 status_t DeviceHalLocal::getMicrophones(
192         std::vector<media::MicrophoneInfo> *microphones __unused) {
193     return INVALID_OPERATION;
194 }
195 #elif MAJOR_VERSION >= 4
getMicrophones(std::vector<media::MicrophoneInfo> * microphones)196 status_t DeviceHalLocal::getMicrophones(std::vector<media::MicrophoneInfo> *microphones) {
197     if (mDev->get_microphones == NULL) return INVALID_OPERATION;
198     size_t actual_mics = AUDIO_MICROPHONE_MAX_COUNT;
199     audio_microphone_characteristic_t mic_array[AUDIO_MICROPHONE_MAX_COUNT];
200     status_t status = mDev->get_microphones(mDev, &mic_array[0], &actual_mics);
201     for (size_t i = 0; i < actual_mics; i++) {
202         media::MicrophoneInfo microphoneInfo = media::MicrophoneInfo(mic_array[i]);
203         microphones->push_back(microphoneInfo);
204     }
205     return status;
206 }
207 #endif
208 
209 // Local HAL implementation does not support effects
addDeviceEffect(audio_port_handle_t device __unused,sp<EffectHalInterface> effect __unused)210 status_t DeviceHalLocal::addDeviceEffect(
211         audio_port_handle_t device __unused, sp<EffectHalInterface> effect __unused) {
212     return INVALID_OPERATION;
213 }
214 
removeDeviceEffect(audio_port_handle_t device __unused,sp<EffectHalInterface> effect __unused)215 status_t DeviceHalLocal::removeDeviceEffect(
216         audio_port_handle_t device __unused, sp<EffectHalInterface> effect __unused) {
217     return INVALID_OPERATION;
218 }
219 
dump(int fd)220 status_t DeviceHalLocal::dump(int fd) {
221     return mDev->dump(mDev, fd);
222 }
223 
closeOutputStream(struct audio_stream_out * stream_out)224 void DeviceHalLocal::closeOutputStream(struct audio_stream_out *stream_out) {
225     mDev->close_output_stream(mDev, stream_out);
226 }
227 
closeInputStream(struct audio_stream_in * stream_in)228 void DeviceHalLocal::closeInputStream(struct audio_stream_in *stream_in) {
229     mDev->close_input_stream(mDev, stream_in);
230 }
231 
232 } // namespace CPP_VERSION
233 } // namespace android
234