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