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 "AudioProfile.h" 21 #include "HandleGenerator.h" 22 #include <utils/String8.h> 23 #include <utils/Vector.h> 24 #include <utils/RefBase.h> 25 #include <utils/Errors.h> 26 #include <system/audio.h> 27 #include <cutils/config_utils.h> 28 29 namespace android { 30 31 class HwModule; 32 class AudioGain; 33 class AudioRoute; 34 typedef Vector<sp<AudioGain> > AudioGainCollection; 35 36 class AudioPort : public virtual RefBase, private HandleGenerator<audio_port_handle_t> 37 { 38 public: AudioPort(const String8 & name,audio_port_type_t type,audio_port_role_t role)39 AudioPort(const String8& name, audio_port_type_t type, audio_port_role_t role) : 40 mName(name), mType(type), mRole(role), mFlags(AUDIO_OUTPUT_FLAG_NONE) {} 41 ~AudioPort()42 virtual ~AudioPort() {} 43 setName(const String8 & name)44 void setName(const String8 &name) { mName = name; } getName()45 const String8 &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 50 virtual const String8 getTagName() const = 0; 51 setGains(const AudioGainCollection & gains)52 void setGains(const AudioGainCollection &gains) { mGains = gains; } getGains()53 const AudioGainCollection &getGains() const { return mGains; } 54 setFlags(uint32_t flags)55 virtual void setFlags(uint32_t flags) 56 { 57 //force direct flag if offload flag is set: offloading implies a direct output stream 58 // and all common behaviors are driven by checking only the direct flag 59 // this should normally be set appropriately in the policy configuration file 60 if (mRole == AUDIO_PORT_ROLE_SOURCE && (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { 61 flags |= AUDIO_OUTPUT_FLAG_DIRECT; 62 } 63 mFlags = flags; 64 } getFlags()65 uint32_t getFlags() const { return mFlags; } 66 67 virtual void attach(const sp<HwModule>& module); isAttached()68 bool isAttached() { return mModule != 0; } 69 70 // Audio port IDs are in a different namespace than AudioFlinger unique IDs 71 static audio_port_handle_t getNextUniqueId(); 72 73 virtual void toAudioPort(struct audio_port *port) const; 74 75 virtual void importAudioPort(const sp<AudioPort>& port, bool force = false); 76 addAudioProfile(const sp<AudioProfile> & profile)77 void addAudioProfile(const sp<AudioProfile> &profile) { mProfiles.add(profile); } 78 setAudioProfiles(const AudioProfileVector & profiles)79 void setAudioProfiles(const AudioProfileVector &profiles) { mProfiles = profiles; } getAudioProfiles()80 AudioProfileVector &getAudioProfiles() { return mProfiles; } 81 hasValidAudioProfile()82 bool hasValidAudioProfile() const { return mProfiles.hasValidProfile(); } 83 hasDynamicAudioProfile()84 bool hasDynamicAudioProfile() const { return mProfiles.hasDynamicProfile(); } 85 86 // searches for an exact match 87 virtual status_t checkExactAudioProfile(const struct audio_port_config *config) const; 88 89 // searches for a compatible match, currently implemented for input 90 // parameters are input|output, returned value is the best match. checkCompatibleAudioProfile(uint32_t & samplingRate,audio_channel_mask_t & channelMask,audio_format_t & format)91 status_t checkCompatibleAudioProfile(uint32_t &samplingRate, 92 audio_channel_mask_t &channelMask, 93 audio_format_t &format) const 94 { 95 return mProfiles.checkCompatibleProfile(samplingRate, channelMask, format, mType, mRole); 96 } 97 clearAudioProfiles()98 void clearAudioProfiles() { return mProfiles.clearProfiles(); } 99 100 status_t checkGain(const struct audio_gain_config *gainConfig, int index) const; 101 102 void pickAudioProfile(uint32_t &samplingRate, 103 audio_channel_mask_t &channelMask, 104 audio_format_t &format) const; 105 106 static const audio_format_t sPcmFormatCompareTable[]; 107 108 static int compareFormats(audio_format_t format1, audio_format_t format2); 109 110 // Used to select an audio HAL output stream with a sample format providing the 111 // less degradation for a given AudioTrack sample format. 112 static bool isBetterFormatMatch(audio_format_t newFormat, 113 audio_format_t currentFormat, 114 audio_format_t targetFormat); 115 116 audio_module_handle_t getModuleHandle() const; 117 uint32_t getModuleVersionMajor() const; 118 const char *getModuleName() const; 119 useInputChannelMask()120 bool useInputChannelMask() const 121 { 122 return ((mType == AUDIO_PORT_TYPE_DEVICE) && (mRole == AUDIO_PORT_ROLE_SOURCE)) || 123 ((mType == AUDIO_PORT_TYPE_MIX) && (mRole == AUDIO_PORT_ROLE_SINK)); 124 } 125 isDirectOutput()126 inline bool isDirectOutput() const 127 { 128 return (mType == AUDIO_PORT_TYPE_MIX) && (mRole == AUDIO_PORT_ROLE_SOURCE) && 129 (mFlags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)); 130 } 131 addRoute(const sp<AudioRoute> & route)132 void addRoute(const sp<AudioRoute> &route) { mRoutes.add(route); } getRoutes()133 const AudioRouteVector &getRoutes() const { return mRoutes; } 134 135 void dump(int fd, int spaces, bool verbose = true) const; 136 void log(const char* indent) const; 137 138 AudioGainCollection mGains; // gain controllers 139 sp<HwModule> mModule; // audio HW module exposing this I/O stream 140 141 private: 142 void pickChannelMask(audio_channel_mask_t &channelMask, const ChannelsVector &channelMasks) const; 143 void pickSamplingRate(uint32_t &rate,const SampleRateVector &samplingRates) const; 144 145 String8 mName; 146 audio_port_type_t mType; 147 audio_port_role_t mRole; 148 uint32_t mFlags; // attribute flags mask (e.g primary output, direct output...). 149 AudioProfileVector mProfiles; // AudioProfiles supported by this port (format, Rates, Channels) 150 AudioRouteVector mRoutes; // Routes involving this port 151 }; 152 153 class AudioPortConfig : public virtual RefBase 154 { 155 public: 156 AudioPortConfig(); ~AudioPortConfig()157 virtual ~AudioPortConfig() {} 158 159 status_t applyAudioPortConfig(const struct audio_port_config *config, 160 struct audio_port_config *backupConfig = NULL); 161 virtual void toAudioPortConfig(struct audio_port_config *dstConfig, 162 const struct audio_port_config *srcConfig = NULL) const = 0; 163 virtual sp<AudioPort> getAudioPort() const = 0; hasSameHwModuleAs(const sp<AudioPortConfig> & other)164 virtual bool hasSameHwModuleAs(const sp<AudioPortConfig>& other) const { 165 return (other != 0) && 166 (other->getAudioPort()->getModuleHandle() == getAudioPort()->getModuleHandle()); 167 } 168 uint32_t mSamplingRate; 169 audio_format_t mFormat; 170 audio_channel_mask_t mChannelMask; 171 struct audio_gain_config mGain; 172 }; 173 174 } // namespace android 175