DeviceDescriptor.cpp revision aa9811945f575614b3482d09e4d969792701cebb
1/*
2 * Copyright (C) 2015 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 "APM::Devices"
18//#define LOG_NDEBUG 0
19
20#include "DeviceDescriptor.h"
21#include "AudioGain.h"
22#include "HwModule.h"
23#include "ConfigParsingUtils.h"
24
25namespace android {
26
27String8 DeviceDescriptor::emptyNameStr = String8("");
28
29DeviceDescriptor::DeviceDescriptor(const String8& name, audio_devices_t type) :
30    AudioPort(name, AUDIO_PORT_TYPE_DEVICE,
31              audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK :
32                                             AUDIO_PORT_ROLE_SOURCE,
33              NULL),
34    mAddress(""), mDeviceType(type)
35{
36
37}
38
39bool DeviceDescriptor::equals(const sp<DeviceDescriptor>& other) const
40{
41    // Devices are considered equal if they:
42    // - are of the same type (a device type cannot be AUDIO_DEVICE_NONE)
43    // - have the same address or one device does not specify the address
44    // - have the same channel mask or one device does not specify the channel mask
45    return (mDeviceType == other->mDeviceType) &&
46           (mAddress == "" || other->mAddress == "" || mAddress == other->mAddress) &&
47           (mChannelMask == 0 || other->mChannelMask == 0 ||
48                mChannelMask == other->mChannelMask);
49}
50
51void DeviceDescriptor::loadGains(cnode *root)
52{
53    AudioPort::loadGains(root);
54    if (mGains.size() > 0) {
55        mGains[0]->getDefaultConfig(&mGain);
56    }
57}
58
59void DeviceVector::refreshTypes()
60{
61    mDeviceTypes = AUDIO_DEVICE_NONE;
62    for(size_t i = 0; i < size(); i++) {
63        mDeviceTypes |= itemAt(i)->type();
64    }
65    ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes);
66}
67
68ssize_t DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const
69{
70    for(size_t i = 0; i < size(); i++) {
71        if (item->equals(itemAt(i))) {
72            return i;
73        }
74    }
75    return -1;
76}
77
78ssize_t DeviceVector::add(const sp<DeviceDescriptor>& item)
79{
80    ssize_t ret = indexOf(item);
81
82    if (ret < 0) {
83        ret = SortedVector::add(item);
84        if (ret >= 0) {
85            refreshTypes();
86        }
87    } else {
88        ALOGW("DeviceVector::add device %08x already in", item->type());
89        ret = -1;
90    }
91    return ret;
92}
93
94ssize_t DeviceVector::remove(const sp<DeviceDescriptor>& item)
95{
96    size_t i;
97    ssize_t ret = indexOf(item);
98
99    if (ret < 0) {
100        ALOGW("DeviceVector::remove device %08x not in", item->type());
101    } else {
102        ret = SortedVector::removeAt(ret);
103        if (ret >= 0) {
104            refreshTypes();
105        }
106    }
107    return ret;
108}
109
110audio_devices_t DeviceVector::getDevicesFromHwModule(audio_module_handle_t moduleHandle) const
111{
112    audio_devices_t devices = AUDIO_DEVICE_NONE;
113    for (size_t i = 0; i < size(); i++) {
114        if (itemAt(i)->getModuleHandle() == moduleHandle) {
115            devices |= itemAt(i)->type();
116        }
117    }
118    return devices;
119}
120
121void DeviceVector::loadDevicesFromType(audio_devices_t types)
122{
123    DeviceVector deviceList;
124
125    uint32_t role_bit = AUDIO_DEVICE_BIT_IN & types;
126    types &= ~role_bit;
127
128    while (types) {
129        uint32_t i = 31 - __builtin_clz(types);
130        uint32_t type = 1 << i;
131        types &= ~type;
132        add(new DeviceDescriptor(String8("device_type"), type | role_bit));
133    }
134}
135
136void DeviceVector::loadDevicesFromName(char *name,
137                                       const DeviceVector& declaredDevices)
138{
139    char *devName = strtok(name, "|");
140    while (devName != NULL) {
141        if (strlen(devName) != 0) {
142            audio_devices_t type = ConfigParsingUtils::stringToEnum(sDeviceNameToEnumTable,
143                                 ARRAY_SIZE(sDeviceNameToEnumTable),
144                                 devName);
145            if (type != AUDIO_DEVICE_NONE) {
146                sp<DeviceDescriptor> dev = new DeviceDescriptor(String8(name), type);
147                if (type == AUDIO_DEVICE_IN_REMOTE_SUBMIX ||
148                        type == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ) {
149                    dev->mAddress = String8("0");
150                }
151                add(dev);
152            } else {
153                sp<DeviceDescriptor> deviceDesc =
154                        declaredDevices.getDeviceFromName(String8(devName));
155                if (deviceDesc != 0) {
156                    add(deviceDesc);
157                }
158            }
159         }
160         devName = strtok(NULL, "|");
161     }
162}
163
164sp<DeviceDescriptor> DeviceVector::getDevice(audio_devices_t type, String8 address) const
165{
166    sp<DeviceDescriptor> device;
167    for (size_t i = 0; i < size(); i++) {
168        if (itemAt(i)->type() == type) {
169            if (address == "" || itemAt(i)->mAddress == address) {
170                device = itemAt(i);
171                if (itemAt(i)->mAddress == address) {
172                    break;
173                }
174            }
175        }
176    }
177    ALOGV("DeviceVector::getDevice() for type %08x address %s found %p",
178          type, address.string(), device.get());
179    return device;
180}
181
182sp<DeviceDescriptor> DeviceVector::getDeviceFromId(audio_port_handle_t id) const
183{
184    sp<DeviceDescriptor> device;
185    for (size_t i = 0; i < size(); i++) {
186        if (itemAt(i)->getHandle() == id) {
187            device = itemAt(i);
188            break;
189        }
190    }
191    return device;
192}
193
194DeviceVector DeviceVector::getDevicesFromType(audio_devices_t type) const
195{
196    DeviceVector devices;
197    bool isOutput = audio_is_output_devices(type);
198    type &= ~AUDIO_DEVICE_BIT_IN;
199    for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) {
200        bool curIsOutput = audio_is_output_devices(itemAt(i)->mDeviceType);
201        audio_devices_t curType = itemAt(i)->mDeviceType & ~AUDIO_DEVICE_BIT_IN;
202        if ((isOutput == curIsOutput) && ((type & curType) != 0)) {
203            devices.add(itemAt(i));
204            type &= ~curType;
205            ALOGV("DeviceVector::getDevicesFromType() for type %x found %p",
206                  itemAt(i)->type(), itemAt(i).get());
207        }
208    }
209    return devices;
210}
211
212DeviceVector DeviceVector::getDevicesFromTypeAddr(
213        audio_devices_t type, String8 address) const
214{
215    DeviceVector devices;
216    for (size_t i = 0; i < size(); i++) {
217        if (itemAt(i)->type() == type) {
218            if (itemAt(i)->mAddress == address) {
219                devices.add(itemAt(i));
220            }
221        }
222    }
223    return devices;
224}
225
226sp<DeviceDescriptor> DeviceVector::getDeviceFromName(const String8& name) const
227{
228    sp<DeviceDescriptor> device;
229    for (size_t i = 0; i < size(); i++) {
230        if (itemAt(i)->mName == name) {
231            device = itemAt(i);
232            break;
233        }
234    }
235    return device;
236}
237
238
239status_t DeviceVector::dump(int fd, const String8 &direction) const
240{
241    const size_t SIZE = 256;
242    char buffer[SIZE];
243
244    snprintf(buffer, SIZE, "\n Available %s devices:\n", direction.string());
245    write(fd, buffer, strlen(buffer));
246    for (size_t i = 0; i < size(); i++) {
247        itemAt(i)->dump(fd, 2, i);
248    }
249    return NO_ERROR;
250}
251
252audio_policy_dev_state_t DeviceVector::getDeviceConnectionState(const sp<DeviceDescriptor> &devDesc) const
253{
254    ssize_t index = indexOf(devDesc);
255    return index >= 0 ? AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
256}
257
258void DeviceDescriptor::toAudioPortConfig(struct audio_port_config *dstConfig,
259                                         const struct audio_port_config *srcConfig) const
260{
261    dstConfig->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK|AUDIO_PORT_CONFIG_GAIN;
262    if (srcConfig != NULL) {
263        dstConfig->config_mask |= srcConfig->config_mask;
264    }
265
266    AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
267
268    dstConfig->id = mId;
269    dstConfig->role = audio_is_output_device(mDeviceType) ?
270                        AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE;
271    dstConfig->type = AUDIO_PORT_TYPE_DEVICE;
272    dstConfig->ext.device.type = mDeviceType;
273
274    //TODO Understand why this test is necessary. i.e. why at boot time does it crash
275    // without the test?
276    // This has been demonstrated to NOT be true (at start up)
277    // ALOG_ASSERT(mModule != NULL);
278    dstConfig->ext.device.hw_module = mModule != 0 ? mModule->mHandle : AUDIO_IO_HANDLE_NONE;
279    strncpy(dstConfig->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
280}
281
282void DeviceDescriptor::toAudioPort(struct audio_port *port) const
283{
284    ALOGV("DeviceDescriptor::toAudioPort() handle %d type %x", mId, mDeviceType);
285    AudioPort::toAudioPort(port);
286    port->id = mId;
287    toAudioPortConfig(&port->active_config);
288    port->ext.device.type = mDeviceType;
289    port->ext.device.hw_module = mModule->mHandle;
290    strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
291}
292
293status_t DeviceDescriptor::dump(int fd, int spaces, int index) const
294{
295    const size_t SIZE = 256;
296    char buffer[SIZE];
297    String8 result;
298
299    snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1);
300    result.append(buffer);
301    if (mId != 0) {
302        snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId);
303        result.append(buffer);
304    }
305    snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "",
306            ConfigParsingUtils::enumToString(sDeviceNameToEnumTable,
307                    ARRAY_SIZE(sDeviceNameToEnumTable),
308                    mDeviceType));
309    result.append(buffer);
310    if (mAddress.size() != 0) {
311        snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string());
312        result.append(buffer);
313    }
314    write(fd, result.string(), result.size());
315    AudioPort::dump(fd, spaces);
316
317    return NO_ERROR;
318}
319
320void DeviceDescriptor::log() const
321{
322    ALOGI("Device id:%d type:0x%X:%s, addr:%s",
323          mId,
324          mDeviceType,
325          ConfigParsingUtils::enumToString(
326             sDeviceNameToEnumTable, ARRAY_SIZE(sDeviceNameToEnumTable), mDeviceType),
327          mAddress.string());
328
329    AudioPort::log("  ");
330}
331
332}; // namespace android
333