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 #include <type_traits> 21 22 #include <android/media/AudioPort.h> 23 #include <android/media/AudioPortConfig.h> 24 #include <android/media/ExtraAudioDescriptor.h> 25 #include <binder/Parcel.h> 26 #include <binder/Parcelable.h> 27 #include <media/AudioGain.h> 28 #include <media/AudioProfile.h> 29 #include <utils/Errors.h> 30 #include <utils/RefBase.h> 31 #include <system/audio.h> 32 #include <cutils/config_utils.h> 33 34 namespace android { 35 36 class AudioPort : public virtual RefBase, public virtual Parcelable 37 { 38 public: AudioPort(const std::string & name,audio_port_type_t type,audio_port_role_t role)39 AudioPort(const std::string& name, audio_port_type_t type, audio_port_role_t role) : 40 mName(name), mType(type), mRole(role) {} 41 42 virtual ~AudioPort() = default; 43 setName(const std::string & name)44 void setName(const std::string &name) { mName = name; } getName()45 const std::string &getName() const { return mName; } 46 getType()47 audio_port_type_t getType() const { return mType; } getRole()48 audio_port_role_t getRole() const { return mRole; } 49 setGains(const AudioGains & gains)50 void setGains(const AudioGains &gains) { mGains = gains; } getGains()51 const AudioGains &getGains() const { return mGains; } 52 53 virtual void toAudioPort(struct audio_port *port) const; 54 55 virtual void toAudioPort(struct audio_port_v7 *port) const; 56 addAudioProfile(const sp<AudioProfile> & profile)57 virtual void addAudioProfile(const sp<AudioProfile> &profile) { 58 mProfiles.add(profile); 59 } clearAudioProfiles()60 virtual void clearAudioProfiles() { 61 mProfiles.clearProfiles(); 62 } 63 hasValidAudioProfile()64 bool hasValidAudioProfile() const { return mProfiles.hasValidProfile(); } 65 hasDynamicAudioProfile()66 bool hasDynamicAudioProfile() const { return mProfiles.hasDynamicProfile(); } 67 setAudioProfiles(const AudioProfileVector & profiles)68 void setAudioProfiles(const AudioProfileVector &profiles) { mProfiles = profiles; } getAudioProfiles()69 AudioProfileVector &getAudioProfiles() { return mProfiles; } 70 setExtraAudioDescriptors(const std::vector<media::ExtraAudioDescriptor> extraAudioDescriptors)71 void setExtraAudioDescriptors( 72 const std::vector<media::ExtraAudioDescriptor> extraAudioDescriptors) { 73 mExtraAudioDescriptors = extraAudioDescriptors; 74 } getExtraAudioDescriptors()75 std::vector<media::ExtraAudioDescriptor> &getExtraAudioDescriptors() { 76 return mExtraAudioDescriptors; 77 } 78 79 virtual void importAudioPort(const sp<AudioPort>& port, bool force = false); 80 81 virtual void importAudioPort(const audio_port_v7& port); 82 checkGain(const struct audio_gain_config * gainConfig,int index)83 status_t checkGain(const struct audio_gain_config *gainConfig, int index) const { 84 if (index < 0 || (size_t)index >= mGains.size()) { 85 return BAD_VALUE; 86 } 87 return mGains[index]->checkConfig(gainConfig); 88 } 89 useInputChannelMask()90 bool useInputChannelMask() const 91 { 92 return ((mType == AUDIO_PORT_TYPE_DEVICE) && (mRole == AUDIO_PORT_ROLE_SOURCE)) || 93 ((mType == AUDIO_PORT_TYPE_MIX) && (mRole == AUDIO_PORT_ROLE_SINK)); 94 } 95 96 void dump(std::string *dst, int spaces, bool verbose = true) const; 97 98 void log(const char* indent) const; 99 100 bool equals(const sp<AudioPort>& other) const; 101 102 status_t writeToParcel(Parcel* parcel) const override; 103 status_t readFromParcel(const Parcel* parcel) override; 104 105 status_t writeToParcelable(media::AudioPort* parcelable) const; 106 status_t readFromParcelable(const media::AudioPort& parcelable); 107 108 AudioGains mGains; // gain controllers 109 protected: 110 std::string mName; 111 audio_port_type_t mType; 112 audio_port_role_t mRole; 113 AudioProfileVector mProfiles; // AudioProfiles supported by this port (format, Rates, Channels) 114 115 // Audio capabilities that are defined by hardware descriptors when the format is unrecognized 116 // by the platform, e.g. short audio descriptor in EDID for HDMI. 117 std::vector<media::ExtraAudioDescriptor> mExtraAudioDescriptors; 118 private: 119 template <typename T, std::enable_if_t<std::is_same<T, struct audio_port>::value 120 || std::is_same<T, struct audio_port_v7>::value, int> = 0> toAudioPortBase(T * port)121 void toAudioPortBase(T* port) const { 122 port->role = mRole; 123 port->type = mType; 124 strlcpy(port->name, mName.c_str(), AUDIO_PORT_MAX_NAME_LEN); 125 port->num_gains = std::min(mGains.size(), (size_t) AUDIO_PORT_MAX_GAINS); 126 for (size_t i = 0; i < port->num_gains; i++) { 127 port->gains[i] = mGains[i]->getGain(); 128 } 129 } 130 }; 131 132 133 class AudioPortConfig : public virtual RefBase, public virtual Parcelable 134 { 135 public: 136 virtual ~AudioPortConfig() = default; 137 138 virtual sp<AudioPort> getAudioPort() const = 0; 139 140 virtual status_t applyAudioPortConfig(const struct audio_port_config *config, 141 struct audio_port_config *backupConfig = NULL); 142 143 virtual void toAudioPortConfig(struct audio_port_config *dstConfig, 144 const struct audio_port_config *srcConfig = NULL) const; 145 getSamplingRate()146 unsigned int getSamplingRate() const { return mSamplingRate; } getFormat()147 audio_format_t getFormat() const { return mFormat; } getChannelMask()148 audio_channel_mask_t getChannelMask() const { return mChannelMask; } getId()149 audio_port_handle_t getId() const { return mId; } 150 151 bool hasGainController(bool canUseForVolume = false) const; 152 153 bool equals(const sp<AudioPortConfig>& other) const; 154 155 status_t writeToParcel(Parcel* parcel) const override; 156 status_t readFromParcel(const Parcel* parcel) override; 157 status_t writeToParcelable(media::AudioPortConfig* parcelable) const; 158 status_t readFromParcelable(const media::AudioPortConfig& parcelable); 159 160 protected: 161 unsigned int mSamplingRate = 0u; 162 audio_format_t mFormat = AUDIO_FORMAT_INVALID; 163 audio_channel_mask_t mChannelMask = AUDIO_CHANNEL_NONE; 164 audio_port_handle_t mId = AUDIO_PORT_HANDLE_NONE; 165 struct audio_gain_config mGain = { .index = -1 }; 166 }; 167 168 } // namespace android 169