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 #ifndef ANDROID_AUDIO_VALIDATOR_H_
18 #define ANDROID_AUDIO_VALIDATOR_H_
19 
20 #include <system/audio.h>
21 #include <system/audio_effect.h>
22 #include <utils/Errors.h>
23 #include <utils/Log.h>
24 
25 #include <string_view>
26 
27 namespace android {
28 
29 /**
30  * AudioValidator is a class to validate audio data in binder call. NO_ERROR will be returned only
31  * when there is no error with the data.
32  */
33 class AudioValidator {
34 public:
35     /**
36      * Return NO_ERROR only when there is no error with the given audio attributes.
37      * Otherwise, return BAD_VALUE.
38      */
39     static status_t validateAudioAttributes(
40             const audio_attributes_t& attr, std::string_view bugNumber = {});
41 
42     /**
43      * Return NO_ERROR only when there is no error with the given effect descriptor.
44      * Otherwise, return BAD_VALUE.
45      */
46     static status_t validateEffectDescriptor(
47             const effect_descriptor_t& desc, std::string_view bugNumber = {});
48 
49     /**
50      * Return NO_ERROR only when there is no error with the given audio port config.
51      * Otherwise, return BAD_VALUE.
52      */
53     static status_t validateAudioPortConfig(
54             const struct audio_port_config& config, std::string_view bugNumber = {});
55 
56     /**
57      * Return NO_ERROR only when there is no error with the given audio port.
58      * Otherwise, return BAD_VALUE.
59      */
60     static status_t validateAudioPort(
61             const struct audio_port& port, std::string_view bugNumber = {});
62 
63     /**
64      * Return NO_ERROR only when there is no error with the given audio_port_v7.
65      * Otherwise, return BAD_VALUE.
66      */
67     static status_t validateAudioPort(
68             const struct audio_port_v7& port, std::string_view bugNumber = {});
69 
70     /**
71      * Return NO_ERROR only when there is no error with the given audio patch.
72      * Otherwise, return BAD_VALUE.
73      */
74     static status_t validateAudioPatch(
75             const struct audio_patch& patch, std::string_view bugNumber = {});
76 
77     /**
78      * Return NO_ERROR if leveldB is acceptable, otherwise BAD_VALUE.
79      */
80     static status_t validateAudioDescriptionMixLevel(float leveldB);
81 
82     /**
83      * Return NO_ERROR if dualMonoMode is one of the enum values, otherwise BAD_VALUE.
84      */
85     static status_t validateDualMonoMode(audio_dual_mono_mode_t dualMonoMode);
86 
87     /**
88      * Return NO_ERROR if fallbackMode is one of the enum values, otherwise BAD_VALUE.
89      */
90     static status_t validatePlaybackRateFallbackMode(
91             audio_timestretch_fallback_mode_t fallbackMode);
92 
93     /**
94      * Return NO_ERROR if fallbackMode is one of the enum values, otherwise BAD_VALUE.
95      */
96     static status_t validatePlaybackRateStretchMode(audio_timestretch_stretch_mode_t stretchMode);
97 
98     /**
99      * Return NO_ERROR if playbackRate is acceptable - the enums are correct and the
100      * rate and speed non-negative, otherwise BAD_VALUE.
101      *
102      * This is a basic bounds check - the system might have stricter requirements for
103      * playbackRate on a particular stream / device.
104      */
105     static status_t validatePlaybackRate(const audio_playback_rate_t& playbackRate);
106 };
107 
108 }; // namespace android
109 
110 #endif  /*ANDROID_AUDIO_VALIDATOR_H_*/
111