• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 #include <media/AudioValidator.h>
18 #include <cmath>
19 
20 namespace android {
21 
22 /** returns true if string is overflow */
23 template <size_t size>
checkStringOverflow(const char (& s)[size])24 bool checkStringOverflow(const char (&s)[size]) {
25     return strnlen(s, size) >= size;
26 }
27 
safetyNetLog(status_t status,std::string_view bugNumber)28 status_t safetyNetLog(status_t status, std::string_view bugNumber) {
29     if (status != NO_ERROR && !bugNumber.empty()) {
30         android_errorWriteLog(0x534e4554, bugNumber.data()); // SafetyNet logging
31     }
32     return status;
33 }
34 
validateAudioAttributes(const audio_attributes_t & attr,std::string_view bugNumber)35 status_t AudioValidator::validateAudioAttributes(
36         const audio_attributes_t& attr, std::string_view bugNumber)
37 {
38     status_t status = NO_ERROR;
39     const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
40     if (strnlen(attr.tags, tagsMaxSize) >= tagsMaxSize) {
41         status = BAD_VALUE;
42     }
43     return safetyNetLog(status, bugNumber);
44 }
45 
validateEffectDescriptor(const effect_descriptor_t & desc,std::string_view bugNumber)46 status_t AudioValidator::validateEffectDescriptor(
47         const effect_descriptor_t& desc, std::string_view bugNumber)
48 {
49     status_t status = NO_ERROR;
50     if (checkStringOverflow(desc.name) || checkStringOverflow(desc.implementor)) {
51         status = BAD_VALUE;
52     }
53     return safetyNetLog(status, bugNumber);
54 }
55 
validateAudioPortConfig(const struct audio_port_config & config,std::string_view bugNumber)56 status_t AudioValidator::validateAudioPortConfig(
57         const struct audio_port_config& config, std::string_view bugNumber)
58 {
59     status_t status = NO_ERROR;
60     if (config.type == AUDIO_PORT_TYPE_DEVICE &&
61         checkStringOverflow(config.ext.device.address)) {
62         status = BAD_VALUE;
63     }
64     return safetyNetLog(status, bugNumber);
65 }
66 
67 namespace {
68 
69 template <typename T, std::enable_if_t<std::is_same<T, struct audio_port>::value
70                                     || std::is_same<T, struct audio_port_v7>::value, int> = 0>
validateAudioPortInternal(const T & port,std::string_view bugNumber={})71 static status_t validateAudioPortInternal(const T& port, std::string_view bugNumber = {}) {
72     status_t status = NO_ERROR;
73     if (checkStringOverflow(port.name)) {
74         status = BAD_VALUE;
75     }
76     if (AudioValidator::validateAudioPortConfig(port.active_config) != NO_ERROR) {
77         status = BAD_VALUE;
78     }
79     if (port.type == AUDIO_PORT_TYPE_DEVICE &&
80         checkStringOverflow(port.ext.device.address)) {
81         status = BAD_VALUE;
82     }
83     return safetyNetLog(status, bugNumber);
84 }
85 
86 } // namespace
87 
validateAudioPort(const struct audio_port & port,std::string_view bugNumber)88 status_t AudioValidator::validateAudioPort(
89         const struct audio_port& port, std::string_view bugNumber)
90 {
91     return validateAudioPortInternal(port, bugNumber);
92 }
93 
validateAudioPort(const struct audio_port_v7 & port,std::string_view bugNumber)94 status_t AudioValidator::validateAudioPort(
95         const struct audio_port_v7& port, std::string_view bugNumber)
96 {
97     return validateAudioPortInternal(port, bugNumber);
98 }
99 
100 /** returns BAD_VALUE if sanitization was required. */
validateAudioPatch(const struct audio_patch & patch,std::string_view bugNumber)101 status_t AudioValidator::validateAudioPatch(
102         const struct audio_patch& patch, std::string_view bugNumber)
103 {
104     status_t status = NO_ERROR;
105     if (patch.num_sources > AUDIO_PATCH_PORTS_MAX) {
106         status = BAD_VALUE;
107     }
108     if (patch.num_sinks > AUDIO_PATCH_PORTS_MAX) {
109         status = BAD_VALUE;
110     }
111     for (size_t i = 0; i < patch.num_sources; i++) {
112         if (validateAudioPortConfig(patch.sources[i]) != NO_ERROR) {
113             status = BAD_VALUE;
114         }
115     }
116     for (size_t i = 0; i < patch.num_sinks; i++) {
117         if (validateAudioPortConfig(patch.sinks[i]) != NO_ERROR) {
118             status = BAD_VALUE;
119         }
120     }
121     return safetyNetLog(status, bugNumber);
122 }
123 
124 /* static */
validateAudioDescriptionMixLevel(float leveldB)125 status_t AudioValidator::validateAudioDescriptionMixLevel(float leveldB)
126 {
127     constexpr float MAX_AUDIO_DESCRIPTION_MIX_LEVEL = 48.f;
128     return std::isnan(leveldB) || leveldB > MAX_AUDIO_DESCRIPTION_MIX_LEVEL ? BAD_VALUE : OK;
129 }
130 
131 /* static */
validateDualMonoMode(audio_dual_mono_mode_t dualMonoMode)132 status_t AudioValidator::validateDualMonoMode(audio_dual_mono_mode_t dualMonoMode)
133 {
134     switch (dualMonoMode) {
135         case AUDIO_DUAL_MONO_MODE_OFF:
136         case AUDIO_DUAL_MONO_MODE_LR:
137         case AUDIO_DUAL_MONO_MODE_LL:
138         case AUDIO_DUAL_MONO_MODE_RR:
139         return OK;
140     }
141     return BAD_VALUE;
142 }
143 
144 /* static */
validatePlaybackRateFallbackMode(audio_timestretch_fallback_mode_t fallbackMode)145 status_t AudioValidator::validatePlaybackRateFallbackMode(
146         audio_timestretch_fallback_mode_t fallbackMode)
147 {
148     switch (fallbackMode) {
149         case AUDIO_TIMESTRETCH_FALLBACK_CUT_REPEAT:
150             // This is coarse sounding timestretching used for internal debugging,
151             // not intended for general use.
152             break; // warning if not listed.
153         case AUDIO_TIMESTRETCH_FALLBACK_DEFAULT:
154         case AUDIO_TIMESTRETCH_FALLBACK_MUTE:
155         case AUDIO_TIMESTRETCH_FALLBACK_FAIL:
156             return OK;
157     }
158     return BAD_VALUE;
159 }
160 
161 /* static */
validatePlaybackRateStretchMode(audio_timestretch_stretch_mode_t stretchMode)162 status_t AudioValidator::validatePlaybackRateStretchMode(
163         audio_timestretch_stretch_mode_t stretchMode)
164 {
165     switch (stretchMode) {
166         case AUDIO_TIMESTRETCH_STRETCH_DEFAULT:
167         case AUDIO_TIMESTRETCH_STRETCH_VOICE:
168             return OK;
169     }
170     return BAD_VALUE;
171 }
172 
173 /* static */
validatePlaybackRate(const audio_playback_rate_t & playbackRate)174 status_t AudioValidator::validatePlaybackRate(
175         const audio_playback_rate_t& playbackRate)
176 {
177     if (playbackRate.mSpeed < 0.f || playbackRate.mPitch < 0.f) return BAD_VALUE;
178     return validatePlaybackRateFallbackMode(playbackRate.mFallbackMode) ?:
179             validatePlaybackRateStretchMode(playbackRate.mStretchMode);
180 }
181 
182 }; // namespace android
183