1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef HDI_DAUDIO_VOLUME_INTERNAL_H
17 #define HDI_DAUDIO_VOLUME_INTERNAL_H
18
19 #include "audio_types.h"
20
21 #include "daudio_errorcode.h"
22
23 #undef DH_LOG_TAG
24 #define DH_LOG_TAG "AudioVolumeInternal"
25
26 namespace OHOS {
27 namespace DistributedHardware {
28 template<typename T>
29 class AudioVolumeInternal final {
30 public:
31 static int32_t SetMute(AudioHandle handle, bool mute);
32 static int32_t GetMute(AudioHandle handle, bool *mute);
33 static int32_t SetVolume(AudioHandle handle, float volume);
34 static int32_t GetVolume(AudioHandle handle, float *volume);
35 static int32_t GetGainThreshold(AudioHandle handle, float *min, float *max);
36 static int32_t SetGain(AudioHandle handle, float gain);
37 static int32_t GetGain(AudioHandle handle, float *gain);
38 };
39
40 template<typename T>
SetMute(AudioHandle handle,bool mute)41 int32_t AudioVolumeInternal<T>::SetMute(AudioHandle handle, bool mute)
42 {
43 if (handle == nullptr) {
44 DHLOGE("The parameter is empty.");
45 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
46 }
47
48 T *context = reinterpret_cast<T *>(handle);
49 return (context == nullptr || context->proxy_ == nullptr) ?
50 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->SetMute(mute);
51 }
52
53 template<typename T>
GetMute(AudioHandle handle,bool * mute)54 int32_t AudioVolumeInternal<T>::GetMute(AudioHandle handle, bool *mute)
55 {
56 if (handle == nullptr || mute == nullptr) {
57 DHLOGE("The parameter is empty.");
58 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
59 }
60
61 T *context = reinterpret_cast<T *>(handle);
62 return (context == nullptr || context->proxy_ == nullptr) ?
63 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->GetMute(*mute);
64 }
65
66 template<typename T>
SetVolume(AudioHandle handle,float volume)67 int32_t AudioVolumeInternal<T>::SetVolume(AudioHandle handle, float volume)
68 {
69 if (handle == nullptr) {
70 DHLOGE("The parameter is empty.");
71 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
72 }
73
74 T *context = reinterpret_cast<T *>(handle);
75 return (context == nullptr || context->proxy_ == nullptr) ?
76 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->SetVolume(volume);
77 }
78
79 template<typename T>
GetVolume(AudioHandle handle,float * volume)80 int32_t AudioVolumeInternal<T>::GetVolume(AudioHandle handle, float *volume)
81 {
82 if (handle == nullptr || volume == nullptr) {
83 DHLOGE("The parameter is empty.");
84 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
85 }
86
87 T *context = reinterpret_cast<T *>(handle);
88 return (context == nullptr || context->proxy_ == nullptr) ?
89 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->GetVolume(*volume);
90 }
91
92 template<typename T>
GetGainThreshold(AudioHandle handle,float * min,float * max)93 int32_t AudioVolumeInternal<T>::GetGainThreshold(AudioHandle handle, float *min, float *max)
94 {
95 if (handle == nullptr || min == nullptr || max == nullptr) {
96 DHLOGE("The parameter is empty.");
97 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
98 }
99
100 T *context = reinterpret_cast<T *>(handle);
101 return (context == nullptr || context->proxy_ == nullptr) ?
102 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->GetGainThreshold(*min, *max);
103 }
104
105 template<typename T>
SetGain(AudioHandle handle,float gain)106 int32_t AudioVolumeInternal<T>::SetGain(AudioHandle handle, float gain)
107 {
108 if (handle == nullptr) {
109 DHLOGE("The parameter is empty.");
110 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
111 }
112
113 T *context = reinterpret_cast<T *>(handle);
114 return (context == nullptr || context->proxy_ == nullptr) ?
115 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->SetGain(gain);
116 }
117
118 template<typename T>
GetGain(AudioHandle handle,float * gain)119 int32_t AudioVolumeInternal<T>::GetGain(AudioHandle handle, float *gain)
120 {
121 if (handle == nullptr || gain == nullptr) {
122 DHLOGE("The parameter is empty.");
123 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
124 }
125
126 T *context = reinterpret_cast<T *>(handle);
127 return (context == nullptr || context->proxy_ == nullptr) ?
128 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->GetGain(*gain);
129 }
130 } // DistributedHardware
131 } // OHOS
132 #endif // HDI_DAUDIO_VOLUME_INTERNAL_H
133