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 template<typename T>
SetMute(AudioHandle handle,bool mute)40 int32_t AudioVolumeInternal<T>::SetMute(AudioHandle handle, bool mute)
41 {
42 if (handle == nullptr) {
43 DHLOGE("The parameter is empty.");
44 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
45 }
46
47 T *context = reinterpret_cast<T *>(handle);
48 return (context == nullptr || context->proxy_ == nullptr) ?
49 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->SetMute(mute);
50 }
51
52 template<typename T>
GetMute(AudioHandle handle,bool * mute)53 int32_t AudioVolumeInternal<T>::GetMute(AudioHandle handle, bool *mute)
54 {
55 if (handle == nullptr || mute == nullptr) {
56 DHLOGE("The parameter is empty.");
57 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
58 }
59
60 T *context = reinterpret_cast<T *>(handle);
61 return (context == nullptr || context->proxy_ == nullptr) ?
62 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->GetMute(*mute);
63 }
64
65 template<typename T>
SetVolume(AudioHandle handle,float volume)66 int32_t AudioVolumeInternal<T>::SetVolume(AudioHandle handle, float volume)
67 {
68 if (handle == nullptr) {
69 DHLOGE("The parameter is empty.");
70 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
71 }
72
73 T *context = reinterpret_cast<T *>(handle);
74 return (context == nullptr || context->proxy_ == nullptr) ?
75 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->SetVolume(volume);
76 }
77
78 template<typename T>
GetVolume(AudioHandle handle,float * volume)79 int32_t AudioVolumeInternal<T>::GetVolume(AudioHandle handle, float *volume)
80 {
81 if (handle == nullptr || volume == nullptr) {
82 DHLOGE("The parameter is empty.");
83 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
84 }
85
86 T *context = reinterpret_cast<T *>(handle);
87 return (context == nullptr || context->proxy_ == nullptr) ?
88 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->GetVolume(*volume);
89 }
90
91 template<typename T>
GetGainThreshold(AudioHandle handle,float * min,float * max)92 int32_t AudioVolumeInternal<T>::GetGainThreshold(AudioHandle handle, float *min, float *max)
93 {
94 if (handle == nullptr || min == nullptr || max == nullptr) {
95 DHLOGE("The parameter is empty.");
96 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
97 }
98
99 T *context = reinterpret_cast<T *>(handle);
100 return (context == nullptr || context->proxy_ == nullptr) ?
101 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->GetGainThreshold(*min, *max);
102 }
103
104 template<typename T>
SetGain(AudioHandle handle,float gain)105 int32_t AudioVolumeInternal<T>::SetGain(AudioHandle handle, float gain)
106 {
107 if (handle == nullptr) {
108 DHLOGE("The parameter is empty.");
109 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
110 }
111
112 T *context = reinterpret_cast<T *>(handle);
113 return (context == nullptr || context->proxy_ == nullptr) ?
114 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->SetGain(gain);
115 }
116
117 template<typename T>
GetGain(AudioHandle handle,float * gain)118 int32_t AudioVolumeInternal<T>::GetGain(AudioHandle handle, float *gain)
119 {
120 if (handle == nullptr || gain == nullptr) {
121 DHLOGE("The parameter is empty.");
122 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
123 }
124
125 T *context = reinterpret_cast<T *>(handle);
126 return (context == nullptr || context->proxy_ == nullptr) ?
127 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->GetGain(*gain);
128 }
129 } // DistributedHardware
130 } // OHOS
131 #endif // HDI_DAUDIO_VOLUME_INTERNAL_H
132