OSDN Git Service

Merge "RenderScript VTS cleanup and fixes" am: 7b36175068 am: 1a2850fbc4 am: 643c439e89
[android-x86/hardware-interfaces.git] / audio / 2.0 / default / Device.cpp
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 "DeviceHAL"
18 //#define LOG_NDEBUG 0
19
20 #include <algorithm>
21 #include <memory.h>
22 #include <string.h>
23
24 #include <android/log.h>
25
26 #include "Conversions.h"
27 #include "Device.h"
28 #include "HidlUtils.h"
29 #include "StreamIn.h"
30 #include "StreamOut.h"
31
32 namespace android {
33 namespace hardware {
34 namespace audio {
35 namespace V2_0 {
36 namespace implementation {
37
38 Device::Device(audio_hw_device_t* device)
39         : mDevice(device) {
40 }
41
42 Device::~Device() {
43     int status = audio_hw_device_close(mDevice);
44     ALOGW_IF(status, "Error closing audio hw device %p: %s", mDevice, strerror(-status));
45     mDevice = nullptr;
46 }
47
48 Result Device::analyzeStatus(const char* funcName, int status) {
49     if (status != 0) {
50         ALOGW("Device %p %s: %s", mDevice, funcName, strerror(-status));
51     }
52     switch (status) {
53         case 0: return Result::OK;
54         case -EINVAL: return Result::INVALID_ARGUMENTS;
55         case -ENODATA: return Result::INVALID_STATE;
56         case -ENODEV: return Result::NOT_INITIALIZED;
57         case -ENOSYS: return Result::NOT_SUPPORTED;
58         default: return Result::INVALID_STATE;
59     }
60 }
61
62 char* Device::halGetParameters(const char* keys) {
63     return mDevice->get_parameters(mDevice, keys);
64 }
65
66 int Device::halSetParameters(const char* keysAndValues) {
67     return mDevice->set_parameters(mDevice, keysAndValues);
68 }
69
70 // Methods from ::android::hardware::audio::V2_0::IDevice follow.
71 Return<Result> Device::initCheck()  {
72     return analyzeStatus("init_check", mDevice->init_check(mDevice));
73 }
74
75 Return<Result> Device::setMasterVolume(float volume)  {
76     Result retval(Result::NOT_SUPPORTED);
77     if (mDevice->set_master_volume != NULL) {
78         retval = analyzeStatus("set_master_volume", mDevice->set_master_volume(mDevice, volume));
79     }
80     return retval;
81 }
82
83 Return<void> Device::getMasterVolume(getMasterVolume_cb _hidl_cb)  {
84     Result retval(Result::NOT_SUPPORTED);
85     float volume = 0;
86     if (mDevice->get_master_volume != NULL) {
87         retval = analyzeStatus("get_master_volume", mDevice->get_master_volume(mDevice, &volume));
88     }
89     _hidl_cb(retval, volume);
90     return Void();
91 }
92
93 Return<Result> Device::setMicMute(bool mute)  {
94     return analyzeStatus("set_mic_mute", mDevice->set_mic_mute(mDevice, mute));
95 }
96
97 Return<void> Device::getMicMute(getMicMute_cb _hidl_cb)  {
98     bool mute = false;
99     Result retval = analyzeStatus("get_mic_mute", mDevice->get_mic_mute(mDevice, &mute));
100     _hidl_cb(retval, mute);
101     return Void();
102 }
103
104 Return<Result> Device::setMasterMute(bool mute)  {
105     Result retval(Result::NOT_SUPPORTED);
106     if (mDevice->set_master_mute != NULL) {
107         retval = analyzeStatus("set_master_mute", mDevice->set_master_mute(mDevice, mute));
108     }
109     return retval;
110 }
111
112 Return<void> Device::getMasterMute(getMasterMute_cb _hidl_cb)  {
113     Result retval(Result::NOT_SUPPORTED);
114     bool mute = false;
115     if (mDevice->get_master_mute != NULL) {
116         retval = analyzeStatus("get_master_mute", mDevice->get_master_mute(mDevice, &mute));
117     }
118     _hidl_cb(retval, mute);
119     return Void();
120 }
121
122 Return<void> Device::getInputBufferSize(
123         const AudioConfig& config, getInputBufferSize_cb _hidl_cb)  {
124     audio_config_t halConfig;
125     HidlUtils::audioConfigToHal(config, &halConfig);
126     size_t halBufferSize = mDevice->get_input_buffer_size(mDevice, &halConfig);
127     Result retval(Result::INVALID_ARGUMENTS);
128     uint64_t bufferSize = 0;
129     if (halBufferSize != 0) {
130         retval = Result::OK;
131         bufferSize = halBufferSize;
132     }
133     _hidl_cb(retval, bufferSize);
134     return Void();
135 }
136
137 Return<void> Device::openOutputStream(
138         int32_t ioHandle,
139         const DeviceAddress& device,
140         const AudioConfig& config,
141         AudioOutputFlag flags,
142         openOutputStream_cb _hidl_cb)  {
143     audio_config_t halConfig;
144     HidlUtils::audioConfigToHal(config, &halConfig);
145     audio_stream_out_t *halStream;
146     ALOGV("open_output_stream handle: %d devices: %x flags: %#x "
147             "srate: %d format %#x channels %x address %s",
148             ioHandle,
149             static_cast<audio_devices_t>(device.device), static_cast<audio_output_flags_t>(flags),
150             halConfig.sample_rate, halConfig.format, halConfig.channel_mask,
151             deviceAddressToHal(device).c_str());
152     int status = mDevice->open_output_stream(
153             mDevice,
154             ioHandle,
155             static_cast<audio_devices_t>(device.device),
156             static_cast<audio_output_flags_t>(flags),
157             &halConfig,
158             &halStream,
159             deviceAddressToHal(device).c_str());
160     ALOGV("open_output_stream status %d stream %p", status, halStream);
161     sp<IStreamOut> streamOut;
162     if (status == OK) {
163         streamOut = new StreamOut(mDevice, halStream);
164     }
165     AudioConfig suggestedConfig;
166     HidlUtils::audioConfigFromHal(halConfig, &suggestedConfig);
167     _hidl_cb(analyzeStatus("open_output_stream", status), streamOut, suggestedConfig);
168     return Void();
169 }
170
171 Return<void> Device::openInputStream(
172         int32_t ioHandle,
173         const DeviceAddress& device,
174         const AudioConfig& config,
175         AudioInputFlag flags,
176         AudioSource source,
177         openInputStream_cb _hidl_cb)  {
178     audio_config_t halConfig;
179     HidlUtils::audioConfigToHal(config, &halConfig);
180     audio_stream_in_t *halStream;
181     ALOGV("open_input_stream handle: %d devices: %x flags: %#x "
182             "srate: %d format %#x channels %x address %s source %d",
183             ioHandle,
184             static_cast<audio_devices_t>(device.device), static_cast<audio_input_flags_t>(flags),
185             halConfig.sample_rate, halConfig.format, halConfig.channel_mask,
186             deviceAddressToHal(device).c_str(), static_cast<audio_source_t>(source));
187     int status = mDevice->open_input_stream(
188             mDevice,
189             ioHandle,
190             static_cast<audio_devices_t>(device.device),
191             &halConfig,
192             &halStream,
193             static_cast<audio_input_flags_t>(flags),
194             deviceAddressToHal(device).c_str(),
195             static_cast<audio_source_t>(source));
196     ALOGV("open_input_stream status %d stream %p", status, halStream);
197     sp<IStreamIn> streamIn;
198     if (status == OK) {
199         streamIn = new StreamIn(mDevice, halStream);
200     }
201     AudioConfig suggestedConfig;
202     HidlUtils::audioConfigFromHal(halConfig, &suggestedConfig);
203     _hidl_cb(analyzeStatus("open_input_stream", status), streamIn, suggestedConfig);
204     return Void();
205 }
206
207 Return<bool> Device::supportsAudioPatches() {
208     return version() >= AUDIO_DEVICE_API_VERSION_3_0;
209 }
210
211 Return<void> Device::createAudioPatch(
212         const hidl_vec<AudioPortConfig>& sources,
213         const hidl_vec<AudioPortConfig>& sinks,
214         createAudioPatch_cb _hidl_cb)  {
215     Result retval(Result::NOT_SUPPORTED);
216     AudioPatchHandle patch = 0;
217     if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
218         std::unique_ptr<audio_port_config[]> halSources(HidlUtils::audioPortConfigsToHal(sources));
219         std::unique_ptr<audio_port_config[]> halSinks(HidlUtils::audioPortConfigsToHal(sinks));
220         audio_patch_handle_t halPatch;
221         retval = analyzeStatus(
222                 "create_audio_patch",
223                 mDevice->create_audio_patch(
224                         mDevice,
225                         sources.size(), &halSources[0],
226                         sinks.size(), &halSinks[0],
227                         &halPatch));
228         if (retval == Result::OK) {
229             patch = static_cast<AudioPatchHandle>(halPatch);
230         }
231     }
232     _hidl_cb(retval, patch);
233     return Void();
234 }
235
236 Return<Result> Device::releaseAudioPatch(int32_t patch)  {
237     if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
238         return analyzeStatus(
239                 "release_audio_patch",
240                 mDevice->release_audio_patch(mDevice, static_cast<audio_patch_handle_t>(patch)));
241     }
242     return Result::NOT_SUPPORTED;
243 }
244
245 Return<void> Device::getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb)  {
246     audio_port halPort;
247     HidlUtils::audioPortToHal(port, &halPort);
248     Result retval = analyzeStatus("get_audio_port", mDevice->get_audio_port(mDevice, &halPort));
249     AudioPort resultPort = port;
250     if (retval == Result::OK) {
251         HidlUtils::audioPortFromHal(halPort, &resultPort);
252     }
253     _hidl_cb(retval, resultPort);
254     return Void();
255 }
256
257 Return<Result> Device::setAudioPortConfig(const AudioPortConfig& config)  {
258     if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
259         struct audio_port_config halPortConfig;
260         HidlUtils::audioPortConfigToHal(config, &halPortConfig);
261         return analyzeStatus(
262                 "set_audio_port_config", mDevice->set_audio_port_config(mDevice, &halPortConfig));
263     }
264     return Result::NOT_SUPPORTED;
265 }
266
267 Return<AudioHwSync> Device::getHwAvSync()  {
268     int halHwAvSync;
269     Result retval = getParam(AudioParameter::keyHwAvSync, &halHwAvSync);
270     return retval == Result::OK ? halHwAvSync : AUDIO_HW_SYNC_INVALID;
271 }
272
273 Return<Result> Device::setScreenState(bool turnedOn)  {
274     return setParam(AudioParameter::keyScreenState, turnedOn);
275 }
276
277 Return<void> Device::getParameters(const hidl_vec<hidl_string>& keys, getParameters_cb _hidl_cb)  {
278     getParametersImpl(keys, _hidl_cb);
279     return Void();
280 }
281
282 Return<Result> Device::setParameters(const hidl_vec<ParameterValue>& parameters)  {
283     return setParametersImpl(parameters);
284 }
285
286 Return<void> Device::debugDump(const hidl_handle& fd)  {
287     if (fd->numFds == 1) {
288         analyzeStatus("dump", mDevice->dump(mDevice, fd->data[0]));
289     }
290     return Void();
291 }
292
293 }  // namespace implementation
294 }  // namespace V2_0
295 }  // namespace audio
296 }  // namespace hardware
297 }  // namespace android