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