blob: ebe855e4df75bd13b3172a2886157977d1b47e24 [file] [log] [blame]
jiabine46870e2019-08-13 15:17:08 -07001/*
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 */
jiabin9bb3a1e2019-08-19 10:10:17 -070016
jiabine46870e2019-08-13 15:17:08 -070017#pragma once
18
jiabin9bb3a1e2019-08-19 10:10:17 -070019#include <string>
20#include <vector>
21
jiabin17058fa2019-10-08 17:33:38 -070022#include <binder/Parcel.h>
23#include <binder/Parcelable.h>
jiabine46870e2019-08-13 15:17:08 -070024#include <media/AudioContainers.h>
25#include <system/audio.h>
26#include <utils/RefBase.h>
jiabine46870e2019-08-13 15:17:08 -070027
28namespace android {
29
jiabin17058fa2019-10-08 17:33:38 -070030class AudioProfile final : public RefBase, public Parcelable
jiabine46870e2019-08-13 15:17:08 -070031{
32public:
jiabin9bb3a1e2019-08-19 10:10:17 -070033 static sp<AudioProfile> createFullDynamic(audio_format_t dynamicFormat = AUDIO_FORMAT_DEFAULT);
jiabine46870e2019-08-13 15:17:08 -070034
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
jiabine46870e2019-08-13 15:17:08 -070054 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
jiabin9bb3a1e2019-08-19 10:10:17 -070069 void dump(std::string *dst, int spaces) const;
jiabine46870e2019-08-13 15:17:08 -070070
jiabin49e69a12019-10-15 16:04:13 -070071 bool equals(const sp<AudioProfile>& other) const;
72
jiabin17058fa2019-10-08 17:33:38 -070073 status_t writeToParcel(Parcel* parcel) const override;
74 status_t readFromParcel(const Parcel* parcel) override;
75
jiabine46870e2019-08-13 15:17:08 -070076private:
jiabin9bb3a1e2019-08-19 10:10:17 -070077 std::string mName;
78 audio_format_t mFormat; // The format for an audio profile should only be set when initialized.
jiabine46870e2019-08-13 15:17:08 -070079 ChannelMaskSet mChannelMasks;
80 SampleRateSet mSamplingRates;
81
82 bool mIsDynamicFormat = false;
83 bool mIsDynamicChannels = false;
84 bool mIsDynamicRate = false;
85};
86
jiabin17058fa2019-10-08 17:33:38 -070087class AudioProfileVector : public std::vector<sp<AudioProfile>>, public Parcelable
jiabine46870e2019-08-13 15:17:08 -070088{
89public:
jiabin3e277cc2019-09-10 14:27:34 -070090 virtual ~AudioProfileVector() = default;
jiabine46870e2019-08-13 15:17:08 -070091
jiabin9bb3a1e2019-08-19 10:10:17 -070092 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();
jiabine46870e2019-08-13 15:17:08 -070097
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;
jiabin9bb3a1e2019-08-19 10:10:17 -0700104 bool hasDynamicFormat() const;
jiabine46870e2019-08-13 15:17:08 -0700105 bool hasDynamicProfile() const;
106 bool hasDynamicRateFor(audio_format_t format) const;
107
jiabinb4fed192020-09-22 14:45:40 -0700108 bool contains(const sp<AudioProfile>& profile) const;
109
jiabin9bb3a1e2019-08-19 10:10:17 -0700110 virtual void dump(std::string *dst, int spaces) const;
jiabin17058fa2019-10-08 17:33:38 -0700111
jiabin49e69a12019-10-15 16:04:13 -0700112 bool equals(const AudioProfileVector& other) const;
113
jiabin17058fa2019-10-08 17:33:38 -0700114 status_t writeToParcel(Parcel* parcel) const override;
115 status_t readFromParcel(const Parcel* parcel) override;
jiabine46870e2019-08-13 15:17:08 -0700116};
117
118bool operator == (const AudioProfile &left, const AudioProfile &right);
119
120} // namespace android