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 <string> 20 #include <vector> 21 22 #include <android/media/AudioProfile.h> 23 #include <binder/Parcel.h> 24 #include <binder/Parcelable.h> 25 #include <media/AidlConversion.h> 26 #include <media/AudioContainers.h> 27 #include <system/audio.h> 28 #include <utils/RefBase.h> 29 30 namespace android { 31 32 class AudioProfile final : public RefBase, public Parcelable 33 { 34 public: 35 static sp<AudioProfile> createFullDynamic(audio_format_t dynamicFormat = AUDIO_FORMAT_DEFAULT); 36 37 AudioProfile(audio_format_t format, audio_channel_mask_t channelMasks, uint32_t samplingRate); 38 AudioProfile(audio_format_t format, 39 const ChannelMaskSet &channelMasks, 40 const SampleRateSet &samplingRateCollection); 41 AudioProfile(audio_format_t format, 42 const ChannelMaskSet &channelMasks, 43 const SampleRateSet &samplingRateCollection, 44 audio_encapsulation_type_t encapsulationType); 45 getFormat()46 audio_format_t getFormat() const { return mFormat; } getChannels()47 const ChannelMaskSet &getChannels() const { return mChannelMasks; } getSampleRates()48 const SampleRateSet &getSampleRates() const { return mSamplingRates; } 49 void setChannels(const ChannelMaskSet &channelMasks); 50 void setSampleRates(const SampleRateSet &sampleRates); 51 52 void clear(); isValid()53 bool isValid() const { return hasValidFormat() && hasValidRates() && hasValidChannels(); } supportsChannels(audio_channel_mask_t channels)54 bool supportsChannels(audio_channel_mask_t channels) const 55 { 56 return mChannelMasks.count(channels) != 0; 57 } supportsRate(uint32_t rate)58 bool supportsRate(uint32_t rate) const { return mSamplingRates.count(rate) != 0; } 59 hasValidFormat()60 bool hasValidFormat() const { return mFormat != AUDIO_FORMAT_DEFAULT; } hasValidRates()61 bool hasValidRates() const { return !mSamplingRates.empty(); } hasValidChannels()62 bool hasValidChannels() const { return !mChannelMasks.empty(); } 63 setDynamicChannels(bool dynamic)64 void setDynamicChannels(bool dynamic) { mIsDynamicChannels = dynamic; } isDynamicChannels()65 bool isDynamicChannels() const { return mIsDynamicChannels; } 66 setDynamicRate(bool dynamic)67 void setDynamicRate(bool dynamic) { mIsDynamicRate = dynamic; } isDynamicRate()68 bool isDynamicRate() const { return mIsDynamicRate; } 69 setDynamicFormat(bool dynamic)70 void setDynamicFormat(bool dynamic) { mIsDynamicFormat = dynamic; } isDynamicFormat()71 bool isDynamicFormat() const { return mIsDynamicFormat; } 72 isDynamic()73 bool isDynamic() { return mIsDynamicFormat || mIsDynamicChannels || mIsDynamicRate; } 74 getEncapsulationType()75 audio_encapsulation_type_t getEncapsulationType() const { return mEncapsulationType; } setEncapsulationType(audio_encapsulation_type_t encapsulationType)76 void setEncapsulationType(audio_encapsulation_type_t encapsulationType) { 77 mEncapsulationType = encapsulationType; 78 } 79 80 void dump(std::string *dst, int spaces) const; 81 82 bool equals(const sp<AudioProfile>& other) const; 83 84 status_t writeToParcel(Parcel* parcel) const override; 85 status_t readFromParcel(const Parcel* parcel) override; 86 87 ConversionResult<media::AudioProfile> toParcelable() const; 88 static ConversionResult<sp<AudioProfile>> fromParcelable(const media::AudioProfile& parcelable); 89 90 private: 91 92 std::string mName; 93 audio_format_t mFormat; // The format for an audio profile should only be set when initialized. 94 ChannelMaskSet mChannelMasks; 95 SampleRateSet mSamplingRates; 96 97 bool mIsDynamicFormat = false; 98 bool mIsDynamicChannels = false; 99 bool mIsDynamicRate = false; 100 101 audio_encapsulation_type_t mEncapsulationType = AUDIO_ENCAPSULATION_TYPE_NONE; 102 103 AudioProfile() = default; 104 AudioProfile& operator=(const AudioProfile& other); 105 }; 106 107 // Conversion routines, according to AidlConversion.h conventions. 108 ConversionResult<sp<AudioProfile>> 109 aidl2legacy_AudioProfile(const media::AudioProfile& aidl); 110 ConversionResult<media::AudioProfile> 111 legacy2aidl_AudioProfile(const sp<AudioProfile>& legacy); 112 113 class AudioProfileVector : public std::vector<sp<AudioProfile>>, public Parcelable 114 { 115 public: 116 virtual ~AudioProfileVector() = default; 117 118 virtual ssize_t add(const sp<AudioProfile> &profile); 119 120 // If the profile is dynamic format and has valid format, it will be removed when doing 121 // clearProfiles(). Otherwise, AudioProfile::clear() will be called. 122 virtual void clearProfiles(); 123 124 sp<AudioProfile> getFirstValidProfile() const; 125 sp<AudioProfile> getFirstValidProfileFor(audio_format_t format) const; hasValidProfile()126 bool hasValidProfile() const { return getFirstValidProfile() != 0; } 127 128 FormatVector getSupportedFormats() const; 129 bool hasDynamicChannelsFor(audio_format_t format) const; 130 bool hasDynamicFormat() const; 131 bool hasDynamicProfile() const; 132 bool hasDynamicRateFor(audio_format_t format) const; 133 134 bool contains(const sp<AudioProfile>& profile) const; 135 136 virtual void dump(std::string *dst, int spaces) const; 137 138 bool equals(const AudioProfileVector& other) const; 139 140 status_t writeToParcel(Parcel* parcel) const override; 141 status_t readFromParcel(const Parcel* parcel) override; 142 }; 143 144 bool operator == (const AudioProfile &left, const AudioProfile &right); 145 146 // Conversion routines, according to AidlConversion.h conventions. 147 ConversionResult<AudioProfileVector> 148 aidl2legacy_AudioProfileVector(const std::vector<media::AudioProfile>& aidl); 149 ConversionResult<std::vector<media::AudioProfile>> 150 legacy2aidl_AudioProfileVector(const AudioProfileVector& legacy); 151 152 AudioProfileVector intersectAudioProfiles(const AudioProfileVector& profiles1, 153 const AudioProfileVector& profiles2); 154 155 } // namespace android 156