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 {
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,
106        audio_devices_t devices,
107        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",
114            handle, devices, flags,
115            config->sample_rate, config->format, config->channel_mask,
116            address);
117    int openResut = mDev->open_output_stream(
118            mDev, handle, devices, flags, config, &halStream, address);
119    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,
133        sp<StreamInHalInterface> *inStream) {
134    audio_stream_in_t *halStream;
135    ALOGV("open_input_stream handle: %d devices: %x flags: %#x "
136            "srate: %d format %#x channels %x address %s source %d",
137            handle, devices, flags,
138            config->sample_rate, config->format, config->channel_mask,
139            address, source);
140    int openResult = mDev->open_input_stream(
141            mDev, handle, devices, config, &halStream, flags, address, source);
142    if (openResult == OK) {
143        *inStream = new StreamInHalLocal(halStream, this);
144    }
145    ALOGV("open_input_stream status %d stream %p", openResult, inStream);
146    return openResult;
147}
148
149status_t DeviceHalLocal::supportsAudioPatches(bool *supportsPatches) {
150    *supportsPatches = version() >= AUDIO_DEVICE_API_VERSION_3_0;
151    return OK;
152}
153
154status_t DeviceHalLocal::createAudioPatch(
155        unsigned int num_sources,
156        const struct audio_port_config *sources,
157        unsigned int num_sinks,
158        const struct audio_port_config *sinks,
159        audio_patch_handle_t *patch) {
160    if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
161        return mDev->create_audio_patch(
162                mDev, num_sources, sources, num_sinks, sinks, patch);
163    } else {
164        return INVALID_OPERATION;
165    }
166}
167
168status_t DeviceHalLocal::releaseAudioPatch(audio_patch_handle_t patch) {
169    if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
170        return mDev->release_audio_patch(mDev, patch);
171    } else {
172        return INVALID_OPERATION;
173    }
174}
175
176status_t DeviceHalLocal::getAudioPort(struct audio_port *port) {
177    return mDev->get_audio_port(mDev, port);
178}
179
180status_t DeviceHalLocal::setAudioPortConfig(const struct audio_port_config *config) {
181    if (version() >= AUDIO_DEVICE_API_VERSION_3_0)
182        return mDev->set_audio_port_config(mDev, config);
183    else
184        return INVALID_OPERATION;
185}
186
187status_t DeviceHalLocal::getMicrophones(
188        std::vector<media::MicrophoneInfo> *microphones __unused) {
189    return INVALID_OPERATION;
190}
191
192status_t DeviceHalLocal::dump(int fd) {
193    return mDev->dump(mDev, fd);
194}
195
196void DeviceHalLocal::closeOutputStream(struct audio_stream_out *stream_out) {
197    mDev->close_output_stream(mDev, stream_out);
198}
199
200void DeviceHalLocal::closeInputStream(struct audio_stream_in *stream_in) {
201    mDev->close_input_stream(mDev, stream_in);
202}
203
204} // namespace android
205