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 #pragma once
18 
19 #include "DeviceDescriptor.h"
20 #include <utils/RefBase.h>
21 #include <media/AudioDeviceTypeAddr.h>
22 #include <media/AudioPolicy.h>
23 #include <utils/Vector.h>
24 #include <system/audio.h>
25 #include <utils/String8.h>
26 
27 #include <DeviceDescriptor.h>
28 #include <AudioOutputDescriptor.h>
29 
30 namespace android {
31 
32 /**
33  * custom mix entry in mPolicyMixes
34  */
35 class AudioPolicyMix : public AudioMix, public RefBase {
36 public:
AudioPolicyMix(const AudioMix & mix)37     AudioPolicyMix(const AudioMix &mix) : AudioMix(mix) {}
38     AudioPolicyMix(const AudioPolicyMix&) = delete;
39     AudioPolicyMix& operator=(const AudioPolicyMix&) = delete;
40 
getOutput()41     const sp<SwAudioOutputDescriptor> &getOutput() const { return mOutput; }
setOutput(const sp<SwAudioOutputDescriptor> & output)42     void setOutput(const sp<SwAudioOutputDescriptor> &output) { mOutput = output; }
clearOutput()43     void clearOutput() { mOutput.clear(); }
44 
45     void dump(String8 *dst, int spaces, int index) const;
46 
47 private:
48     sp<SwAudioOutputDescriptor> mOutput;  // Corresponding output stream
49 };
50 
51 
52 class AudioPolicyMixCollection : public Vector<sp<AudioPolicyMix>>
53 {
54 public:
55     status_t getAudioPolicyMix(audio_devices_t deviceType,
56             const String8& address, sp<AudioPolicyMix> &policyMix) const;
57 
58     status_t registerMix(AudioMix mix, sp<SwAudioOutputDescriptor> desc);
59 
60     status_t unregisterMix(const AudioMix& mix);
61 
62     void closeOutput(sp<SwAudioOutputDescriptor> &desc);
63 
64     /**
65      * Try to find an output descriptor for the given attributes.
66      *
67      * @param[in] attributes to consider fowr the research of output descriptor.
68      * @param[out] desc to return if an primary output could be found.
69      * @param[out] secondaryDesc other desc that the audio should be routed to.
70      * @return OK if the request is valid
71      *         otherwise if the request is not supported
72      */
73     status_t getOutputForAttr(const audio_attributes_t& attributes, uid_t uid,
74                               audio_output_flags_t flags,
75                               sp<SwAudioOutputDescriptor> &primaryDesc,
76                               std::vector<sp<SwAudioOutputDescriptor>> *secondaryDescs);
77 
78     sp<DeviceDescriptor> getDeviceAndMixForInputSource(audio_source_t inputSource,
79                                                        const DeviceVector &availableDeviceTypes,
80                                                        sp<AudioPolicyMix> *policyMix) const;
81 
82     /**
83      * @brief try to find a matching mix for a given output descriptor and returns the associated
84      * output device.
85      * @param output to be considered
86      * @param availableOutputDevices list of output devices currently reachable
87      * @return device selected from the mix attached to the output, null pointer otherwise
88      */
89     sp<DeviceDescriptor> getDeviceAndMixForOutput(const sp<SwAudioOutputDescriptor> &output,
90                                                   const DeviceVector &availableOutputDevices);
91 
92     status_t getInputMixForAttr(audio_attributes_t attr, sp<AudioPolicyMix> *policyMix);
93 
94     /**
95      * Updates the mix rules in order to make streams associated with the given uid
96      * be routed to the given audio devices.
97      * @param uid the uid for which the device affinity is set
98      * @param devices the vector of devices that this uid may be routed to. A typical
99      *    use is to pass the devices associated with a given zone in a multi-zone setup.
100      * @return NO_ERROR if the update was successful, INVALID_OPERATION otherwise.
101      *    An example of failure is when there are already rules in place to restrict
102      *    a mix to the given uid (i.e. when a MATCH_UID rule was set for it).
103      */
104     status_t setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices);
105     status_t removeUidDeviceAffinities(uid_t uid);
106     status_t getDevicesForUid(uid_t uid, Vector<AudioDeviceTypeAddr>& devices) const;
107 
108     void dump(String8 *dst) const;
109 
110 private:
111     enum class MixMatchStatus { MATCH, NO_MATCH, INVALID_MIX };
112     MixMatchStatus mixMatch(const AudioMix* mix, size_t mixIndex,
113                             const audio_attributes_t& attributes, uid_t uid);
114 };
115 
116 } // namespace android
117