• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 
18 #pragma once
19 
20 #include <functional>
21 
22 #include <android/media/audio/common/AudioChannelLayout.h>
23 #include <android/media/audio/common/AudioDeviceDescription.h>
24 #include <android/media/audio/common/AudioFormatDescription.h>
25 #include <binder/Parcelable.h>
26 #include <system/audio.h>
27 #include <system/audio_policy.h>
28 
29 namespace {
30 // see boost::hash_combine
31 #if defined(__clang__)
32 __attribute__((no_sanitize("unsigned-integer-overflow")))
33 #endif
hash_combine(size_t seed,size_t v)34 static size_t hash_combine(size_t seed, size_t v) {
35     return std::hash<size_t>{}(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
36 }
37 }
38 
39 namespace std {
40 
41 // Note: when extending the types hashed below we need to account for the
42 // possibility of processing types belonging to different versions of the type,
43 // e.g. a HAL may be using a previous version of the AIDL interface.
44 
45 template<> struct hash<android::media::audio::common::AudioChannelLayout>
46 {
47     std::size_t operator()(
48             const android::media::audio::common::AudioChannelLayout& acl) const noexcept {
49         using Tag = android::media::audio::common::AudioChannelLayout::Tag;
50         const size_t seed = std::hash<Tag>{}(acl.getTag());
51         switch (acl.getTag()) {
52             case Tag::none:
53                 return hash_combine(seed, std::hash<int32_t>{}(acl.get<Tag::none>()));
54             case Tag::invalid:
55                 return hash_combine(seed, std::hash<int32_t>{}(acl.get<Tag::invalid>()));
56             case Tag::indexMask:
57                 return hash_combine(seed, std::hash<int32_t>{}(acl.get<Tag::indexMask>()));
58             case Tag::layoutMask:
59                 return hash_combine(seed, std::hash<int32_t>{}(acl.get<Tag::layoutMask>()));
60             case Tag::voiceMask:
61                 return hash_combine(seed, std::hash<int32_t>{}(acl.get<Tag::voiceMask>()));
62         }
63         return seed;
64     }
65 };
66 
67 template<> struct hash<android::media::audio::common::AudioDeviceDescription>
68 {
69     std::size_t operator()(
70             const android::media::audio::common::AudioDeviceDescription& add) const noexcept {
71         return hash_combine(
72                 std::hash<android::media::audio::common::AudioDeviceType>{}(add.type),
73                 std::hash<std::string>{}(add.connection));
74     }
75 };
76 
77 template<> struct hash<android::media::audio::common::AudioFormatDescription>
78 {
79     std::size_t operator()(
80             const android::media::audio::common::AudioFormatDescription& afd) const noexcept {
81         return hash_combine(
82                 std::hash<android::media::audio::common::AudioFormatType>{}(afd.type),
83                 hash_combine(
84                         std::hash<android::media::audio::common::PcmType>{}(afd.pcm),
85                         std::hash<std::string>{}(afd.encoding)));
86     }
87 };
88 }  // namespace std
89 
90 namespace android {
91 
92 enum product_strategy_t : uint32_t;
93 const product_strategy_t PRODUCT_STRATEGY_NONE = static_cast<product_strategy_t>(-1);
94 
95 using AttributesVector = std::vector<audio_attributes_t>;
96 using StreamTypeVector = std::vector<audio_stream_type_t>;
97 
98 using TrackSecondaryOutputsMap = std::map<audio_port_handle_t, std::vector<audio_io_handle_t>>;
99 
100 constexpr bool operator==(const audio_attributes_t &lhs, const audio_attributes_t &rhs)
101 {
102     return lhs.usage == rhs.usage && lhs.content_type == rhs.content_type &&
103             lhs.flags == rhs.flags && (std::strcmp(lhs.tags, rhs.tags) == 0);
104 }
105 constexpr bool operator!=(const audio_attributes_t &lhs, const audio_attributes_t &rhs)
106 {
107     return !(lhs==rhs);
108 }
109 
110 constexpr bool operator==(const audio_offload_info_t &lhs, const audio_offload_info_t &rhs)
111 {
112     return lhs.version == rhs.version && lhs.size == rhs.size &&
113            lhs.sample_rate == rhs.sample_rate && lhs.channel_mask == rhs.channel_mask &&
114            lhs.format == rhs.format && lhs.stream_type == rhs.stream_type &&
115            lhs.bit_rate == rhs.bit_rate && lhs.duration_us == rhs.duration_us &&
116            lhs.has_video == rhs.has_video && lhs.is_streaming == rhs.is_streaming &&
117            lhs.bit_width == rhs.bit_width && lhs.offload_buffer_size == rhs.offload_buffer_size &&
118            lhs.usage == rhs.usage && lhs.encapsulation_mode == rhs.encapsulation_mode &&
119            lhs.content_id == rhs.content_id && lhs.sync_id == rhs.sync_id;
120 }
121 constexpr bool operator!=(const audio_offload_info_t &lhs, const audio_offload_info_t &rhs)
122 {
123     return !(lhs==rhs);
124 }
125 
126 constexpr bool operator==(const audio_config_t &lhs, const audio_config_t &rhs)
127 {
128     return lhs.sample_rate == rhs.sample_rate && lhs.channel_mask == rhs.channel_mask &&
129            lhs.format == rhs.format && lhs.offload_info == rhs.offload_info;
130 }
131 constexpr bool operator!=(const audio_config_t &lhs, const audio_config_t &rhs)
132 {
133     return !(lhs==rhs);
134 }
135 
136 constexpr bool operator==(const audio_config_base_t &lhs, const audio_config_base_t &rhs)
137 {
138     return lhs.sample_rate == rhs.sample_rate && lhs.channel_mask == rhs.channel_mask &&
139            lhs.format == rhs.format;
140 }
141 constexpr bool operator!=(const audio_config_base_t &lhs, const audio_config_base_t &rhs)
142 {
143     return !(lhs==rhs);
144 }
145 
146 enum volume_group_t : uint32_t;
147 static const volume_group_t VOLUME_GROUP_NONE = static_cast<volume_group_t>(-1);
148 
149 } // namespace android
150