• 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 using PortHandleVector = std::vector<audio_port_handle_t>;
98 
99 using TrackSecondaryOutputsMap = std::map<audio_port_handle_t, std::vector<audio_io_handle_t>>;
100 
101 constexpr bool operator==(const audio_attributes_t &lhs, const audio_attributes_t &rhs)
102 {
103     return lhs.usage == rhs.usage && lhs.content_type == rhs.content_type &&
104             lhs.flags == rhs.flags && (std::strcmp(lhs.tags, rhs.tags) == 0);
105 }
106 constexpr bool operator!=(const audio_attributes_t &lhs, const audio_attributes_t &rhs)
107 {
108     return !(lhs==rhs);
109 }
110 
111 constexpr bool operator==(const audio_offload_info_t &lhs, const audio_offload_info_t &rhs)
112 {
113     return lhs.version == rhs.version && lhs.size == rhs.size &&
114            lhs.sample_rate == rhs.sample_rate && lhs.channel_mask == rhs.channel_mask &&
115            lhs.format == rhs.format && lhs.stream_type == rhs.stream_type &&
116            lhs.bit_rate == rhs.bit_rate && lhs.duration_us == rhs.duration_us &&
117            lhs.has_video == rhs.has_video && lhs.is_streaming == rhs.is_streaming &&
118            lhs.bit_width == rhs.bit_width && lhs.offload_buffer_size == rhs.offload_buffer_size &&
119            lhs.usage == rhs.usage && lhs.encapsulation_mode == rhs.encapsulation_mode &&
120            lhs.content_id == rhs.content_id && lhs.sync_id == rhs.sync_id;
121 }
122 constexpr bool operator!=(const audio_offload_info_t &lhs, const audio_offload_info_t &rhs)
123 {
124     return !(lhs==rhs);
125 }
126 
127 constexpr bool operator==(const audio_config_t &lhs, const audio_config_t &rhs)
128 {
129     return lhs.sample_rate == rhs.sample_rate && lhs.channel_mask == rhs.channel_mask &&
130            lhs.format == rhs.format && lhs.offload_info == rhs.offload_info;
131 }
132 constexpr bool operator!=(const audio_config_t &lhs, const audio_config_t &rhs)
133 {
134     return !(lhs==rhs);
135 }
136 
137 constexpr bool operator==(const audio_config_base_t &lhs, const audio_config_base_t &rhs)
138 {
139     return lhs.sample_rate == rhs.sample_rate && lhs.channel_mask == rhs.channel_mask &&
140            lhs.format == rhs.format;
141 }
142 constexpr bool operator!=(const audio_config_base_t &lhs, const audio_config_base_t &rhs)
143 {
144     return !(lhs==rhs);
145 }
146 
147 enum volume_group_t : uint32_t;
148 static const volume_group_t VOLUME_GROUP_NONE = static_cast<volume_group_t>(-1);
149 
150 } // namespace android
151