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 #pragma once
18
19 #include "DeviceDescriptor.h"
20
21 namespace android {
22
23 /**
24 * Interface for I/O descriptors to implement so information about their context
25 * can be queried and updated.
26 */
27 class AudioIODescriptorInterface
28 {
29 public:
~AudioIODescriptorInterface()30 virtual ~AudioIODescriptorInterface() {};
31
32 virtual audio_config_base_t getConfig() const = 0;
33
34 virtual audio_patch_handle_t getPatchHandle() const = 0;
35
36 virtual void setPatchHandle(audio_patch_handle_t handle) = 0;
37
38 virtual bool isMmap() = 0;
39 };
40
41 template <class IoDescriptor, class Filter>
findPreferredDevice(IoDescriptor & desc,Filter filter,bool & active,const DeviceVector & devices)42 sp<DeviceDescriptor> findPreferredDevice(
43 IoDescriptor& desc, Filter filter, bool& active, const DeviceVector& devices)
44 {
45 auto activeClients = desc->clientsList(true /*activeOnly*/);
46 active = activeClients.size() > 0;
47
48 if (active) {
49 // On MMAP IOs, the preferred device is selected by the first client (virtual client
50 // created when the mmap stream is opened). This client is never active and we only
51 // consider the Filter criteria, not the active state.
52 // On non MMAP IOs, the preferred device is honored only if all active clients have
53 // a preferred device in which case the first client drives the selection.
54 if (desc->isMmap()) {
55 auto matchingClients = desc->clientsList(
56 false /*activeOnly*/, filter, false /*preferredDevice*/);
57 if (matchingClients.empty()) {
58 return nullptr;
59 }
60 return devices.getDeviceFromId(matchingClients[0]->preferredDeviceId());
61 } else {
62 auto activeClientsWithRoute =
63 desc->clientsList(true /*activeOnly*/, filter, true /*preferredDevice*/);
64 if (activeClients.size() == activeClientsWithRoute.size()) {
65 return devices.getDeviceFromId(activeClientsWithRoute[0]->preferredDeviceId());
66 }
67 if (activeClientsWithRoute.size() == 0) {
68 return nullptr;
69 }
70 uid_t uniqueUid = activeClients[0]->uid();
71 for (const auto &client : activeClients) {
72 if (uniqueUid != client->uid()) {
73 return nullptr;
74 }
75 }
76 for (const auto &client : activeClientsWithRoute) {
77 if (uniqueUid != client->uid()) {
78 return nullptr;
79 }
80 }
81 return devices.getDeviceFromId(activeClientsWithRoute[0]->preferredDeviceId());
82 }
83 }
84 return nullptr;
85 }
86
87 template <class IoCollection, class Filter>
findPreferredDevice(IoCollection & ioCollection,Filter filter,const DeviceVector & devices)88 sp<DeviceDescriptor> findPreferredDevice(
89 IoCollection& ioCollection, Filter filter, const DeviceVector& devices)
90 {
91 sp<DeviceDescriptor> device;
92 for (size_t i = 0; i < ioCollection.size(); i++) {
93 auto desc = ioCollection.valueAt(i);
94 bool active;
95 sp<DeviceDescriptor> curDevice = findPreferredDevice(desc, filter, active, devices);
96 if (active && curDevice == nullptr) {
97 return nullptr;
98 } else if (curDevice != nullptr) {
99 device = curDevice;
100 }
101 }
102 return device;
103 }
104
105 } // namespace android
106