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 
39 template <class IoDescriptor, class Filter>
findPreferredDevice(IoDescriptor & desc,Filter filter,bool & active,const DeviceVector & devices)40 sp<DeviceDescriptor> findPreferredDevice(
41         IoDescriptor& desc, Filter filter, bool& active, const DeviceVector& devices)
42 {
43     auto activeClients = desc->clientsList(true /*activeOnly*/);
44     auto activeClientsWithRoute =
45         desc->clientsList(true /*activeOnly*/, filter, true /*preferredDevice*/);
46     active = activeClients.size() > 0;
47     if (active && activeClients.size() == activeClientsWithRoute.size()) {
48         return devices.getDeviceFromId(activeClientsWithRoute[0]->preferredDeviceId());
49     }
50     return nullptr;
51 }
52 
53 template <class IoCollection, class Filter>
findPreferredDevice(IoCollection & ioCollection,Filter filter,const DeviceVector & devices)54 sp<DeviceDescriptor> findPreferredDevice(
55         IoCollection& ioCollection, Filter filter, const DeviceVector& devices)
56 {
57     sp<DeviceDescriptor> device;
58     for (size_t i = 0; i < ioCollection.size(); i++) {
59         auto desc = ioCollection.valueAt(i);
60         bool active;
61         sp<DeviceDescriptor> curDevice = findPreferredDevice(desc, filter, active, devices);
62         if (active && curDevice == nullptr) {
63             return nullptr;
64         } else if (curDevice != nullptr) {
65             device = curDevice;
66         }
67     }
68     return device;
69 }
70 
71 } // namespace android
72