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
25namespace android {
26namespace V4_0 {
27
28DeviceHalLocal::DeviceHalLocal(audio_hw_device_t *dev)
29        : mDev(dev) {
30}
31
32DeviceHalLocal::~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
38status_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
44status_t DeviceHalLocal::initCheck() {
45    return mDev->init_check(mDev);
46}
47
48status_t DeviceHalLocal::setVoiceVolume(float volume) {
49    return mDev->set_voice_volume(mDev, volume);
50}
51
52status_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
57status_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
62status_t DeviceHalLocal::setMode(audio_mode_t mode) {
63    return mDev->set_mode(mDev, mode);
64}
65
66status_t DeviceHalLocal::setMicMute(bool state) {
67    return mDev->set_mic_mute(mDev, state);
68}
69
70status_t DeviceHalLocal::getMicMute(bool *state) {
71    return mDev->get_mic_mute(mDev, state);
72}
73
74status_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
79status_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
84status_t DeviceHalLocal::setParameters(const String8& kvPairs) {
85    return mDev->set_parameters(mDev, kvPairs.string());
86}
87
88status_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
99status_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
105status_t DeviceHalLocal::openOutputStream(
106        audio_io_handle_t handle,
107        audio_devices_t devices,
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, devices, flags,
116            config->sample_rate, config->format, config->channel_mask,
117            address);
118    int openResut = mDev->open_output_stream(
119            mDev, handle, devices, 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
127status_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        sp<StreamInHalInterface> *inStream) {
135    audio_stream_in_t *halStream;
136    ALOGV("open_input_stream handle: %d devices: %x flags: %#x "
137            "srate: %d format %#x channels %x address %s source %d",
138            handle, devices, flags,
139            config->sample_rate, config->format, config->channel_mask,
140            address, source);
141    int openResult = mDev->open_input_stream(
142            mDev, handle, devices, config, &halStream, flags, address, source);
143    if (openResult == OK) {
144        *inStream = new StreamInHalLocal(halStream, this);
145    }
146    ALOGV("open_input_stream status %d stream %p", openResult, inStream);
147    return openResult;
148}
149
150status_t DeviceHalLocal::supportsAudioPatches(bool *supportsPatches) {
151    *supportsPatches = version() >= AUDIO_DEVICE_API_VERSION_3_0;
152    return OK;
153}
154
155status_t DeviceHalLocal::createAudioPatch(
156        unsigned int num_sources,
157        const struct audio_port_config *sources,
158        unsigned int num_sinks,
159        const struct audio_port_config *sinks,
160        audio_patch_handle_t *patch) {
161    if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
162        return mDev->create_audio_patch(
163                mDev, num_sources, sources, num_sinks, sinks, patch);
164    } else {
165        return INVALID_OPERATION;
166    }
167}
168
169status_t DeviceHalLocal::releaseAudioPatch(audio_patch_handle_t patch) {
170    if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
171        return mDev->release_audio_patch(mDev, patch);
172    } else {
173        return INVALID_OPERATION;
174    }
175}
176
177status_t DeviceHalLocal::getAudioPort(struct audio_port *port) {
178    return mDev->get_audio_port(mDev, port);
179}
180
181status_t DeviceHalLocal::setAudioPortConfig(const struct audio_port_config *config) {
182    if (version() >= AUDIO_DEVICE_API_VERSION_3_0)
183        return mDev->set_audio_port_config(mDev, config);
184    else
185        return INVALID_OPERATION;
186}
187
188status_t DeviceHalLocal::getMicrophones(std::vector<media::MicrophoneInfo> *microphones) {
189    if (mDev->get_microphones == NULL) return INVALID_OPERATION;
190    size_t actual_mics = AUDIO_MICROPHONE_MAX_COUNT;
191    audio_microphone_characteristic_t mic_array[AUDIO_MICROPHONE_MAX_COUNT];
192    status_t status = mDev->get_microphones(mDev, &mic_array[0], &actual_mics);
193    for (size_t i = 0; i < actual_mics; i++) {
194        media::MicrophoneInfo microphoneInfo = media::MicrophoneInfo(mic_array[i]);
195        microphones->push_back(microphoneInfo);
196    }
197    return status;
198}
199
200status_t DeviceHalLocal::dump(int fd) {
201    return mDev->dump(mDev, fd);
202}
203
204void DeviceHalLocal::closeOutputStream(struct audio_stream_out *stream_out) {
205    mDev->close_output_stream(mDev, stream_out);
206}
207
208void DeviceHalLocal::closeInputStream(struct audio_stream_in *stream_in) {
209    mDev->close_input_stream(mDev, stream_in);
210}
211
212} // namespace V4_0
213} // namespace android
214