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.
51 // On non MMAP IOs, the preferred device is honored only if all active clients have
52 // a preferred device in which case the first client drives the selection.
53 if (desc->isMmap()) {
54 // The client list is never empty on a MMAP IO
55 return devices.getDeviceFromId(
56 desc->clientsList(false /*activeOnly*/)[0]->preferredDeviceId());
57 } else {
58 auto activeClientsWithRoute =
59 desc->clientsList(true /*activeOnly*/, filter, true /*preferredDevice*/);
60 if (activeClients.size() == activeClientsWithRoute.size()) {
61 return devices.getDeviceFromId(activeClientsWithRoute[0]->preferredDeviceId());
62 }
63 }
64 }
65 return nullptr;
66 }
67
68 template <class IoCollection, class Filter>
findPreferredDevice(IoCollection & ioCollection,Filter filter,const DeviceVector & devices)69 sp<DeviceDescriptor> findPreferredDevice(
70 IoCollection& ioCollection, Filter filter, const DeviceVector& devices)
71 {
72 sp<DeviceDescriptor> device;
73 for (size_t i = 0; i < ioCollection.size(); i++) {
74 auto desc = ioCollection.valueAt(i);
75 bool active;
76 sp<DeviceDescriptor> curDevice = findPreferredDevice(desc, filter, active, devices);
77 if (active && curDevice == nullptr) {
78 return nullptr;
79 } else if (curDevice != nullptr) {
80 device = curDevice;
81 }
82 }
83 return device;
84 }
85
86 } // namespace android
87