1 /*
2  * Copyright (C) 2019 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 <string>
20 
21 #include <binder/Parcel.h>
22 #include <binder/Parcelable.h>
23 #include <media/AudioGain.h>
24 #include <media/AudioProfile.h>
25 #include <utils/Errors.h>
26 #include <utils/RefBase.h>
27 #include <system/audio.h>
28 #include <cutils/config_utils.h>
29 
30 namespace android {
31 
32 class AudioPort : public virtual RefBase, public virtual Parcelable
33 {
34 public:
AudioPort(const std::string & name,audio_port_type_t type,audio_port_role_t role)35     AudioPort(const std::string& name, audio_port_type_t type,  audio_port_role_t role) :
36             mName(name), mType(type), mRole(role) {}
37 
38     virtual ~AudioPort() = default;
39 
setName(const std::string & name)40     void setName(const std::string &name) { mName = name; }
getName()41     const std::string &getName() const { return mName; }
42 
getType()43     audio_port_type_t getType() const { return mType; }
getRole()44     audio_port_role_t getRole() const { return mRole; }
45 
setGains(const AudioGains & gains)46     void setGains(const AudioGains &gains) { mGains = gains; }
getGains()47     const AudioGains &getGains() const { return mGains; }
48 
49     virtual void toAudioPort(struct audio_port *port) const;
50 
addAudioProfile(const sp<AudioProfile> & profile)51     virtual void addAudioProfile(const sp<AudioProfile> &profile) {
52         mProfiles.add(profile);
53     }
clearAudioProfiles()54     virtual void clearAudioProfiles() {
55         mProfiles.clearProfiles();
56     }
57 
hasValidAudioProfile()58     bool hasValidAudioProfile() const { return mProfiles.hasValidProfile(); }
59 
hasDynamicAudioProfile()60     bool hasDynamicAudioProfile() const { return mProfiles.hasDynamicProfile(); }
61 
setAudioProfiles(const AudioProfileVector & profiles)62     void setAudioProfiles(const AudioProfileVector &profiles) { mProfiles = profiles; }
getAudioProfiles()63     AudioProfileVector &getAudioProfiles() { return mProfiles; }
64 
65     virtual void importAudioPort(const sp<AudioPort>& port, bool force = false);
66 
checkGain(const struct audio_gain_config * gainConfig,int index)67     status_t checkGain(const struct audio_gain_config *gainConfig, int index) const {
68         if (index < 0 || (size_t)index >= mGains.size()) {
69             return BAD_VALUE;
70         }
71         return mGains[index]->checkConfig(gainConfig);
72     }
73 
useInputChannelMask()74     bool useInputChannelMask() const
75     {
76         return ((mType == AUDIO_PORT_TYPE_DEVICE) && (mRole == AUDIO_PORT_ROLE_SOURCE)) ||
77                 ((mType == AUDIO_PORT_TYPE_MIX) && (mRole == AUDIO_PORT_ROLE_SINK));
78     }
79 
80     void dump(std::string *dst, int spaces, bool verbose = true) const;
81 
82     void log(const char* indent) const;
83 
84     bool equals(const sp<AudioPort>& other) const;
85 
86     status_t writeToParcel(Parcel* parcel) const override;
87     status_t readFromParcel(const Parcel* parcel) override;
88 
89     AudioGains mGains; // gain controllers
90 protected:
91     std::string  mName;
92     audio_port_type_t mType;
93     audio_port_role_t mRole;
94     AudioProfileVector mProfiles; // AudioProfiles supported by this port (format, Rates, Channels)
95 };
96 
97 
98 class AudioPortConfig : public virtual RefBase, public virtual Parcelable
99 {
100 public:
101     virtual ~AudioPortConfig() = default;
102 
103     virtual sp<AudioPort> getAudioPort() const = 0;
104 
105     virtual status_t applyAudioPortConfig(const struct audio_port_config *config,
106                                           struct audio_port_config *backupConfig = NULL);
107 
108     virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
109                                    const struct audio_port_config *srcConfig = NULL) const;
110 
getSamplingRate()111     unsigned int getSamplingRate() const { return mSamplingRate; }
getFormat()112     audio_format_t getFormat() const { return mFormat; }
getChannelMask()113     audio_channel_mask_t getChannelMask() const { return mChannelMask; }
getId()114     audio_port_handle_t getId() const { return mId; }
115 
116     bool hasGainController(bool canUseForVolume = false) const;
117 
118     bool equals(const sp<AudioPortConfig>& other) const;
119 
120     status_t writeToParcel(Parcel* parcel) const override;
121     status_t readFromParcel(const Parcel* parcel) override;
122 
123 protected:
124     unsigned int mSamplingRate = 0u;
125     audio_format_t mFormat = AUDIO_FORMAT_INVALID;
126     audio_channel_mask_t mChannelMask = AUDIO_CHANNEL_NONE;
127     audio_port_handle_t mId = AUDIO_PORT_HANDLE_NONE;
128     struct audio_gain_config mGain = { .index = -1 };
129 };
130 
131 } // namespace android
132