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 <binder/Parcel.h>
23 #include <binder/Parcelable.h>
24 #include <media/AudioContainers.h>
25 #include <system/audio.h>
26 #include <utils/RefBase.h>
27 
28 namespace android {
29 
30 class AudioProfile final : public RefBase, public Parcelable
31 {
32 public:
33     static sp<AudioProfile> createFullDynamic(audio_format_t dynamicFormat = AUDIO_FORMAT_DEFAULT);
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 
getFormat()40     audio_format_t getFormat() const { return mFormat; }
getChannels()41     const ChannelMaskSet &getChannels() const { return mChannelMasks; }
getSampleRates()42     const SampleRateSet &getSampleRates() const { return mSamplingRates; }
43     void setChannels(const ChannelMaskSet &channelMasks);
44     void setSampleRates(const SampleRateSet &sampleRates);
45 
46     void clear();
isValid()47     bool isValid() const { return hasValidFormat() && hasValidRates() && hasValidChannels(); }
supportsChannels(audio_channel_mask_t channels)48     bool supportsChannels(audio_channel_mask_t channels) const
49     {
50         return mChannelMasks.count(channels) != 0;
51     }
supportsRate(uint32_t rate)52     bool supportsRate(uint32_t rate) const { return mSamplingRates.count(rate) != 0; }
53 
hasValidFormat()54     bool hasValidFormat() const { return mFormat != AUDIO_FORMAT_DEFAULT; }
hasValidRates()55     bool hasValidRates() const { return !mSamplingRates.empty(); }
hasValidChannels()56     bool hasValidChannels() const { return !mChannelMasks.empty(); }
57 
setDynamicChannels(bool dynamic)58     void setDynamicChannels(bool dynamic) { mIsDynamicChannels = dynamic; }
isDynamicChannels()59     bool isDynamicChannels() const { return mIsDynamicChannels; }
60 
setDynamicRate(bool dynamic)61     void setDynamicRate(bool dynamic) { mIsDynamicRate = dynamic; }
isDynamicRate()62     bool isDynamicRate() const { return mIsDynamicRate; }
63 
setDynamicFormat(bool dynamic)64     void setDynamicFormat(bool dynamic) { mIsDynamicFormat = dynamic; }
isDynamicFormat()65     bool isDynamicFormat() const { return mIsDynamicFormat; }
66 
isDynamic()67     bool isDynamic() { return mIsDynamicFormat || mIsDynamicChannels || mIsDynamicRate; }
68 
69     void dump(std::string *dst, int spaces) const;
70 
71     bool equals(const sp<AudioProfile>& other) const;
72 
73     status_t writeToParcel(Parcel* parcel) const override;
74     status_t readFromParcel(const Parcel* parcel) override;
75 
76 private:
77     std::string  mName;
78     audio_format_t mFormat; // The format for an audio profile should only be set when initialized.
79     ChannelMaskSet mChannelMasks;
80     SampleRateSet mSamplingRates;
81 
82     bool mIsDynamicFormat = false;
83     bool mIsDynamicChannels = false;
84     bool mIsDynamicRate = false;
85 };
86 
87 class AudioProfileVector : public std::vector<sp<AudioProfile>>, public Parcelable
88 {
89 public:
90     virtual ~AudioProfileVector() = default;
91 
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();
97 
98     sp<AudioProfile> getFirstValidProfile() const;
99     sp<AudioProfile> getFirstValidProfileFor(audio_format_t format) const;
hasValidProfile()100     bool hasValidProfile() const { return getFirstValidProfile() != 0; }
101 
102     FormatVector getSupportedFormats() const;
103     bool hasDynamicChannelsFor(audio_format_t format) const;
104     bool hasDynamicFormat() const;
105     bool hasDynamicProfile() const;
106     bool hasDynamicRateFor(audio_format_t format) const;
107 
108     virtual void dump(std::string *dst, int spaces) const;
109 
110     bool equals(const AudioProfileVector& other) const;
111 
112     status_t writeToParcel(Parcel* parcel) const override;
113     status_t readFromParcel(const Parcel* parcel) override;
114 };
115 
116 bool operator == (const AudioProfile &left, const AudioProfile &right);
117 
118 } // namespace android
119