blob: 57592bca60b44ef80266fa5c8ce73d7fab62e082 [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
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -080022#include <android/media/AudioProfile.h>
jiabin17058fa2019-10-08 17:33:38 -070023#include <binder/Parcel.h>
24#include <binder/Parcelable.h>
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -080025#include <media/AidlConversion.h>
jiabine46870e2019-08-13 15:17:08 -070026#include <media/AudioContainers.h>
27#include <system/audio.h>
28#include <utils/RefBase.h>
jiabine46870e2019-08-13 15:17:08 -070029
30namespace android {
31
jiabin17058fa2019-10-08 17:33:38 -070032class AudioProfile final : public RefBase, public Parcelable
jiabine46870e2019-08-13 15:17:08 -070033{
34public:
jiabin9bb3a1e2019-08-19 10:10:17 -070035 static sp<AudioProfile> createFullDynamic(audio_format_t dynamicFormat = AUDIO_FORMAT_DEFAULT);
jiabine46870e2019-08-13 15:17:08 -070036
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
jiabine46870e2019-08-13 15:17:08 -070056 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
jiabin9bb3a1e2019-08-19 10:10:17 -070071 void dump(std::string *dst, int spaces) const;
jiabine46870e2019-08-13 15:17:08 -070072
jiabin49e69a12019-10-15 16:04:13 -070073 bool equals(const sp<AudioProfile>& other) const;
74
jiabin17058fa2019-10-08 17:33:38 -070075 status_t writeToParcel(Parcel* parcel) const override;
76 status_t readFromParcel(const Parcel* parcel) override;
77
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -080078 ConversionResult<media::AudioProfile> toParcelable() const;
79 static ConversionResult<sp<AudioProfile>> fromParcelable(const media::AudioProfile& parcelable);
80
jiabine46870e2019-08-13 15:17:08 -070081private:
jiabin9bb3a1e2019-08-19 10:10:17 -070082 std::string mName;
83 audio_format_t mFormat; // The format for an audio profile should only be set when initialized.
jiabine46870e2019-08-13 15:17:08 -070084 ChannelMaskSet mChannelMasks;
85 SampleRateSet mSamplingRates;
86
87 bool mIsDynamicFormat = false;
88 bool mIsDynamicChannels = false;
89 bool mIsDynamicRate = false;
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -080090
91 AudioProfile() = default;
92 AudioProfile& operator=(const AudioProfile& other);
jiabine46870e2019-08-13 15:17:08 -070093};
94
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -080095// Conversion routines, according to AidlConversion.h conventions.
96ConversionResult<sp<AudioProfile>>
97aidl2legacy_AudioProfile(const media::AudioProfile& aidl);
98ConversionResult<media::AudioProfile>
99legacy2aidl_AudioProfile(const sp<AudioProfile>& legacy);
100
jiabin17058fa2019-10-08 17:33:38 -0700101class AudioProfileVector : public std::vector<sp<AudioProfile>>, public Parcelable
jiabine46870e2019-08-13 15:17:08 -0700102{
103public:
jiabin3e277cc2019-09-10 14:27:34 -0700104 virtual ~AudioProfileVector() = default;
jiabine46870e2019-08-13 15:17:08 -0700105
jiabin9bb3a1e2019-08-19 10:10:17 -0700106 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();
jiabine46870e2019-08-13 15:17:08 -0700111
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;
jiabin9bb3a1e2019-08-19 10:10:17 -0700118 bool hasDynamicFormat() const;
jiabine46870e2019-08-13 15:17:08 -0700119 bool hasDynamicProfile() const;
120 bool hasDynamicRateFor(audio_format_t format) const;
121
jiabinb4fed192020-09-22 14:45:40 -0700122 bool contains(const sp<AudioProfile>& profile) const;
123
jiabin9bb3a1e2019-08-19 10:10:17 -0700124 virtual void dump(std::string *dst, int spaces) const;
jiabin17058fa2019-10-08 17:33:38 -0700125
jiabin49e69a12019-10-15 16:04:13 -0700126 bool equals(const AudioProfileVector& other) const;
127
jiabin17058fa2019-10-08 17:33:38 -0700128 status_t writeToParcel(Parcel* parcel) const override;
129 status_t readFromParcel(const Parcel* parcel) override;
jiabine46870e2019-08-13 15:17:08 -0700130};
131
132bool operator == (const AudioProfile &left, const AudioProfile &right);
133
Ytai Ben-Tsvi50e016a2020-11-12 14:26:12 -0800134// Conversion routines, according to AidlConversion.h conventions.
135ConversionResult<AudioProfileVector>
136aidl2legacy_AudioProfileVector(const std::vector<media::AudioProfile>& aidl);
137ConversionResult<std::vector<media::AudioProfile>>
138legacy2aidl_AudioProfileVector(const AudioProfileVector& legacy);
139
140
jiabine46870e2019-08-13 15:17:08 -0700141} // namespace android