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 <utils/String8.h>
20 #include <utils/Vector.h>
21 #include <utils/RefBase.h>
22 #include <utils/Errors.h>
23 #include <system/audio.h>
24 #include <cutils/config_utils.h>
25 
26 namespace android {
27 
28 class HwModule;
29 class AudioGain;
30 
31 class AudioPort : public virtual RefBase
32 {
33 public:
34     AudioPort(const String8& name, audio_port_type_t type,
35               audio_port_role_t role);
~AudioPort()36     virtual ~AudioPort() {}
37 
38     virtual void attach(const sp<HwModule>& module);
isAttached()39     bool isAttached() { return mModule != 0; }
40 
41     static audio_port_handle_t getNextUniqueId();
42 
43     virtual void toAudioPort(struct audio_port *port) const;
44 
45     virtual void importAudioPort(const sp<AudioPort> port);
46     void clearCapabilities();
47 
48     void loadSamplingRates(char *name);
49     void loadFormats(char *name);
50     void loadOutChannels(char *name);
51     void loadInChannels(char *name);
52 
53     audio_gain_mode_t loadGainMode(char *name);
54     void loadGain(cnode *root, int index);
55     virtual void loadGains(cnode *root);
56 
57     // searches for an exact match
58     status_t checkExactSamplingRate(uint32_t samplingRate) const;
59     // searches for a compatible match, and returns the best match via updatedSamplingRate
60     status_t checkCompatibleSamplingRate(uint32_t samplingRate,
61             uint32_t *updatedSamplingRate) const;
62     // searches for an exact match
63     status_t checkExactChannelMask(audio_channel_mask_t channelMask) const;
64     // searches for a compatible match, currently implemented for input channel masks only
65     status_t checkCompatibleChannelMask(audio_channel_mask_t channelMask,
66             audio_channel_mask_t *updatedChannelMask) const;
67 
68     status_t checkExactFormat(audio_format_t format) const;
69     // searches for a compatible match, currently implemented for input formats only
70     status_t checkCompatibleFormat(audio_format_t format, audio_format_t *updatedFormat) const;
71     status_t checkGain(const struct audio_gain_config *gainConfig, int index) const;
72 
73     uint32_t pickSamplingRate() const;
74     audio_channel_mask_t pickChannelMask() const;
75     audio_format_t pickFormat() const;
76 
77     static const audio_format_t sPcmFormatCompareTable[];
compareFormats(const audio_format_t * format1,const audio_format_t * format2)78     static int compareFormats(const audio_format_t *format1, const audio_format_t *format2) {
79         return compareFormats(*format1, *format2);
80     }
81     static int compareFormats(audio_format_t format1, audio_format_t format2);
82 
83     audio_module_handle_t getModuleHandle() const;
84     uint32_t getModuleVersion() const;
85     const char *getModuleName() const;
86 
87     void dump(int fd, int spaces) const;
88     void log(const char* indent) const;
89 
90     String8           mName;
91     audio_port_type_t mType;
92     audio_port_role_t mRole;
93     bool              mUseInChannelMask;
94     // by convention, "0' in the first entry in mSamplingRates, mChannelMasks or mFormats
95     // indicates the supported parameters should be read from the output stream
96     // after it is opened for the first time
97     Vector <uint32_t> mSamplingRates; // supported sampling rates
98     Vector <audio_channel_mask_t> mChannelMasks; // supported channel masks
99     Vector <audio_format_t> mFormats; // supported audio formats
100     Vector < sp<AudioGain> > mGains; // gain controllers
101     sp<HwModule> mModule;                 // audio HW module exposing this I/O stream
102     uint32_t mFlags; // attribute flags (e.g primary output,
103                      // direct output...).
104 
105 private:
106     static volatile int32_t mNextUniqueId;
107 };
108 
109 class AudioPortConfig : public virtual RefBase
110 {
111 public:
112     AudioPortConfig();
~AudioPortConfig()113     virtual ~AudioPortConfig() {}
114 
115     status_t applyAudioPortConfig(const struct audio_port_config *config,
116             struct audio_port_config *backupConfig = NULL);
117     virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
118             const struct audio_port_config *srcConfig = NULL) const = 0;
119     virtual sp<AudioPort> getAudioPort() const = 0;
120     uint32_t mSamplingRate;
121     audio_format_t mFormat;
122     audio_channel_mask_t mChannelMask;
123     struct audio_gain_config mGain;
124 };
125 
126 }; // namespace android
127