• 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
60     virtual status_t checkExactAudioProfile(const struct audio_port_config *config) const;
61 
62     // searches for a compatible match, currently implemented for input
63     // parameters are input|output, returned value is the best match.
checkCompatibleAudioProfile(uint32_t & samplingRate,audio_channel_mask_t & channelMask,audio_format_t & format)64     status_t checkCompatibleAudioProfile(uint32_t &samplingRate,
65                                          audio_channel_mask_t &channelMask,
66                                          audio_format_t &format) const
67     {
68         return checkCompatibleProfile(
69                 asAudioPort()->getAudioProfiles(), samplingRate, channelMask, format,
70                 asAudioPort()->getType(), asAudioPort()->getRole());
71     }
72 
73     void pickAudioProfile(uint32_t &samplingRate,
74                           audio_channel_mask_t &channelMask,
75                           audio_format_t &format) const;
76 
77     static const audio_format_t sPcmFormatCompareTable[];
78 
79     static int compareFormats(audio_format_t format1, audio_format_t format2);
80 
81     // Used to select an audio HAL output stream with a sample format providing the
82     // less degradation for a given AudioTrack sample format.
83     static bool isBetterFormatMatch(audio_format_t newFormat,
84                                     audio_format_t currentFormat,
85                                     audio_format_t targetFormat);
86     static uint32_t formatDistance(audio_format_t format1,
87                                    audio_format_t format2);
88     static const uint32_t kFormatDistanceMax = 4;
89 
90     audio_module_handle_t getModuleHandle() const;
91     uint32_t getModuleVersionMajor() const;
92     const char *getModuleName() const;
getModule()93     sp<HwModule> getModule() const { return mModule; }
94 
addRoute(const sp<AudioRoute> & route)95     void addRoute(const sp<AudioRoute> &route) { mRoutes.add(route); }
getRoutes()96     const AudioRouteVector &getRoutes() const { return mRoutes; }
97 
98 private:
99     void pickChannelMask(audio_channel_mask_t &channelMask,
100                          const ChannelMaskSet &channelMasks) const;
101     void pickSamplingRate(uint32_t &rate, const SampleRateSet &samplingRates) const;
102 
103     sp<HwModule> mModule;     // audio HW module exposing this I/O stream
104     AudioRouteVector mRoutes; // Routes involving this port
105 };
106 
107 class PolicyAudioPortConfig : public virtual RefBase
108 {
109 public:
110     virtual ~PolicyAudioPortConfig() = default;
111 
112     virtual sp<PolicyAudioPort> getPolicyAudioPort() const = 0;
113 
validationBeforeApplyConfig(const struct audio_port_config * config)114     status_t validationBeforeApplyConfig(const struct audio_port_config *config) const {
115         sp<PolicyAudioPort> policyAudioPort = getPolicyAudioPort();
116         return policyAudioPort ? policyAudioPort->checkExactAudioProfile(config) : NO_INIT;
117     }
118 
hasSameHwModuleAs(const sp<PolicyAudioPortConfig> & other)119     bool hasSameHwModuleAs(const sp<PolicyAudioPortConfig>& other) const {
120         return (other.get() != nullptr) && (other->getPolicyAudioPort().get() != nullptr) &&
121                 (getPolicyAudioPort().get() != nullptr) &&
122                 (other->getPolicyAudioPort()->getModuleHandle() ==
123                         getPolicyAudioPort()->getModuleHandle());
124     }
125 
126 };
127 
128 } // namespace android
129