• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "AudioCollections.h"
20 #include "AudioProfileVectorHelper.h"
21 #include "HandleGenerator.h"
22 #include <media/AudioGain.h>
23 #include <media/AudioPort.h>
24 #include <utils/String8.h>
25 #include <utils/Vector.h>
26 #include <utils/RefBase.h>
27 #include <utils/Errors.h>
28 #include <system/audio.h>
29 #include <cutils/config_utils.h>
30 
31 namespace android {
32 
33 class HwModule;
34 class AudioRoute;
35 
36 class PolicyAudioPort : public virtual RefBase, private HandleGenerator<audio_port_handle_t>
37 {
38 public:
39     PolicyAudioPort() = default;
40 
41     virtual ~PolicyAudioPort() = default;
42 
43     virtual const std::string getTagName() const = 0;
44 
equals(const sp<PolicyAudioPort> & right)45     bool equals(const sp<PolicyAudioPort> &right) const
46     {
47         return right != 0 && getTagName() == right->getTagName();
48     }
49 
50     virtual sp<AudioPort> asAudioPort() const = 0;
51 
52     virtual void attach(const sp<HwModule>& module);
53     virtual void detach();
isAttached()54     bool isAttached() { return mModule != 0; }
55 
56     // Audio port IDs are in a different namespace than AudioFlinger unique IDs
57     static audio_port_handle_t getNextUniqueId();
58 
59     // searches for an exact match, note that this method use `audio_formats_match` from policy.h,
60     // which will consider PCM formats match if their bytes per sample are greater than 2.
61     virtual status_t checkExactAudioProfile(const struct audio_port_config *config) const;
62 
63     // searches for an identical match, unlike `checkExactAudioProfile` above, this will also
64     // require the formats to be exactly the same.
65     virtual status_t checkIdenticalAudioProfile(const struct audio_port_config *config) const;
66 
67     // searches for a compatible match, currently implemented for input
68     // parameters are input|output, returned value is the best match.
checkCompatibleAudioProfile(uint32_t & samplingRate,audio_channel_mask_t & channelMask,audio_format_t & format)69     status_t checkCompatibleAudioProfile(uint32_t &samplingRate,
70                                          audio_channel_mask_t &channelMask,
71                                          audio_format_t &format) const
72     {
73         return checkCompatibleProfile(
74                 asAudioPort()->getAudioProfiles(), samplingRate, channelMask, format,
75                 asAudioPort()->getType(), asAudioPort()->getRole());
76     }
77 
78     void pickAudioProfile(uint32_t &samplingRate,
79                           audio_channel_mask_t &channelMask,
80                           audio_format_t &format) const;
81 
82     static const audio_format_t sPcmFormatCompareTable[];
83 
84     static int compareFormats(audio_format_t format1, audio_format_t format2);
85 
86     // Used to select an audio HAL output stream with a sample format providing the
87     // less degradation for a given AudioTrack sample format.
88     static bool isBetterFormatMatch(audio_format_t newFormat,
89                                     audio_format_t currentFormat,
90                                     audio_format_t targetFormat);
91     static uint32_t formatDistance(audio_format_t format1,
92                                    audio_format_t format2);
93     static const uint32_t kFormatDistanceMax = 4;
94 
95     audio_module_handle_t getModuleHandle() const;
96     uint32_t getModuleVersionMajor() const;
97     const char *getModuleName() const;
getModule()98     sp<HwModule> getModule() const { return mModule; }
99 
addRoute(const sp<AudioRoute> & route)100     void addRoute(const sp<AudioRoute> &route) { mRoutes.add(route); }
getRoutes()101     const AudioRouteVector &getRoutes() const { return mRoutes; }
102 
103 private:
104     void pickChannelMask(audio_channel_mask_t &channelMask,
105                          const ChannelMaskSet &channelMasks) const;
106     void pickSamplingRate(uint32_t &rate, const SampleRateSet &samplingRates) const;
107 
108     status_t checkAudioProfile(const struct audio_port_config *config,
109                                std::function<status_t(const AudioProfileVector&,
110                                                       const uint32_t samplingRate,
111                                                       audio_channel_mask_t,
112                                                       audio_format_t)> checkProfile) const;
113 
114     sp<HwModule> mModule;     // audio HW module exposing this I/O stream
115     AudioRouteVector mRoutes; // Routes involving this port
116 };
117 
118 class PolicyAudioPortConfig : public virtual RefBase
119 {
120 public:
121     virtual ~PolicyAudioPortConfig() = default;
122 
123     virtual sp<PolicyAudioPort> getPolicyAudioPort() const = 0;
124 
validationBeforeApplyConfig(const struct audio_port_config * config)125     status_t validationBeforeApplyConfig(const struct audio_port_config *config) const {
126         sp<PolicyAudioPort> policyAudioPort = getPolicyAudioPort();
127         return policyAudioPort ? policyAudioPort->checkExactAudioProfile(config) : NO_INIT;
128     }
129 
hasSameHwModuleAs(const sp<PolicyAudioPortConfig> & other)130     bool hasSameHwModuleAs(const sp<PolicyAudioPortConfig>& other) const {
131         return (other.get() != nullptr) && (other->getPolicyAudioPort().get() != nullptr) &&
132                 (getPolicyAudioPort().get() != nullptr) &&
133                 (other->getPolicyAudioPort()->getModuleHandle() ==
134                         getPolicyAudioPort()->getModuleHandle());
135     }
136 
137 };
138 
139 } // namespace android
140