jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 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 | */ |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 16 | |
jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 17 | #pragma once |
| 18 | |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 22 | #include <binder/Parcel.h> |
| 23 | #include <binder/Parcelable.h> |
jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 24 | #include <media/AudioContainers.h> |
| 25 | #include <system/audio.h> |
| 26 | #include <utils/RefBase.h> |
jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 30 | class AudioProfile final : public RefBase, public Parcelable |
jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 31 | { |
| 32 | public: |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 33 | static sp<AudioProfile> createFullDynamic(audio_format_t dynamicFormat = AUDIO_FORMAT_DEFAULT); |
jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 34 | |
| 35 | AudioProfile(audio_format_t format, audio_channel_mask_t channelMasks, uint32_t samplingRate); |
| 36 | AudioProfile(audio_format_t format, |
| 37 | const ChannelMaskSet &channelMasks, |
| 38 | const SampleRateSet &samplingRateCollection); |
| 39 | |
| 40 | audio_format_t getFormat() const { return mFormat; } |
| 41 | const ChannelMaskSet &getChannels() const { return mChannelMasks; } |
| 42 | const SampleRateSet &getSampleRates() const { return mSamplingRates; } |
| 43 | void setChannels(const ChannelMaskSet &channelMasks); |
| 44 | void setSampleRates(const SampleRateSet &sampleRates); |
| 45 | |
| 46 | void clear(); |
| 47 | bool isValid() const { return hasValidFormat() && hasValidRates() && hasValidChannels(); } |
| 48 | bool supportsChannels(audio_channel_mask_t channels) const |
| 49 | { |
| 50 | return mChannelMasks.count(channels) != 0; |
| 51 | } |
| 52 | bool supportsRate(uint32_t rate) const { return mSamplingRates.count(rate) != 0; } |
| 53 | |
jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 54 | bool hasValidFormat() const { return mFormat != AUDIO_FORMAT_DEFAULT; } |
| 55 | bool hasValidRates() const { return !mSamplingRates.empty(); } |
| 56 | bool hasValidChannels() const { return !mChannelMasks.empty(); } |
| 57 | |
| 58 | void setDynamicChannels(bool dynamic) { mIsDynamicChannels = dynamic; } |
| 59 | bool isDynamicChannels() const { return mIsDynamicChannels; } |
| 60 | |
| 61 | void setDynamicRate(bool dynamic) { mIsDynamicRate = dynamic; } |
| 62 | bool isDynamicRate() const { return mIsDynamicRate; } |
| 63 | |
| 64 | void setDynamicFormat(bool dynamic) { mIsDynamicFormat = dynamic; } |
| 65 | bool isDynamicFormat() const { return mIsDynamicFormat; } |
| 66 | |
| 67 | bool isDynamic() { return mIsDynamicFormat || mIsDynamicChannels || mIsDynamicRate; } |
| 68 | |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 69 | void dump(std::string *dst, int spaces) const; |
jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 70 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 71 | bool equals(const sp<AudioProfile>& other) const; |
| 72 | |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 73 | status_t writeToParcel(Parcel* parcel) const override; |
| 74 | status_t readFromParcel(const Parcel* parcel) override; |
| 75 | |
jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 76 | private: |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 77 | std::string mName; |
| 78 | audio_format_t mFormat; // The format for an audio profile should only be set when initialized. |
jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 79 | ChannelMaskSet mChannelMasks; |
| 80 | SampleRateSet mSamplingRates; |
| 81 | |
| 82 | bool mIsDynamicFormat = false; |
| 83 | bool mIsDynamicChannels = false; |
| 84 | bool mIsDynamicRate = false; |
| 85 | }; |
| 86 | |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 87 | class AudioProfileVector : public std::vector<sp<AudioProfile>>, public Parcelable |
jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 88 | { |
| 89 | public: |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 90 | virtual ~AudioProfileVector() = default; |
jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 91 | |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 92 | virtual ssize_t add(const sp<AudioProfile> &profile); |
| 93 | |
| 94 | // If the profile is dynamic format and has valid format, it will be removed when doing |
| 95 | // clearProfiles(). Otherwise, AudioProfile::clear() will be called. |
| 96 | virtual void clearProfiles(); |
jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 97 | |
| 98 | sp<AudioProfile> getFirstValidProfile() const; |
| 99 | sp<AudioProfile> getFirstValidProfileFor(audio_format_t format) const; |
| 100 | bool hasValidProfile() const { return getFirstValidProfile() != 0; } |
| 101 | |
| 102 | FormatVector getSupportedFormats() const; |
| 103 | bool hasDynamicChannelsFor(audio_format_t format) const; |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 104 | bool hasDynamicFormat() const; |
jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 105 | bool hasDynamicProfile() const; |
| 106 | bool hasDynamicRateFor(audio_format_t format) const; |
| 107 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame^] | 108 | bool contains(const sp<AudioProfile>& profile) const; |
| 109 | |
jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 110 | virtual void dump(std::string *dst, int spaces) const; |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 111 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 112 | bool equals(const AudioProfileVector& other) const; |
| 113 | |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 114 | status_t writeToParcel(Parcel* parcel) const override; |
| 115 | status_t readFromParcel(const Parcel* parcel) override; |
jiabin | e46870e | 2019-08-13 15:17:08 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | bool operator == (const AudioProfile &left, const AudioProfile &right); |
| 119 | |
| 120 | } // namespace android |