• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "alsa_snd_capture.h"
17 #include "common.h"
18 
19 #define HDF_LOG_TAG HDF_AUDIO_HAL_CAPTURE
20 
21 typedef struct CaptureData {
22     struct AlsaMixerCtlElement ctrlLeftVolume;
23     struct AlsaMixerCtlElement ctrlRightVolume;
24     long tempVolume;
25 } CaptureData;
26 
CaptureInitImpl(struct AlsaCapture * captureIns)27 static int32_t CaptureInitImpl(struct AlsaCapture* captureIns)
28 {
29     CHECK_NULL_PTR_RETURN_DEFAULT(captureIns);
30     if (captureIns->priData != NULL) {
31         return HDF_SUCCESS;
32     }
33 
34     CaptureData *priData = (CaptureData *)OsalMemCalloc(sizeof(CaptureData));
35     if (priData == NULL) {
36         AUDIO_FUNC_LOGE("Failed to allocate memory!");
37         return HDF_FAILURE;
38     }
39 
40     SndElementItemInit(&priData->ctrlLeftVolume);
41     SndElementItemInit(&priData->ctrlRightVolume);
42     priData->ctrlLeftVolume.numid = SND_NUMID_DACL_CAPTURE_VOL;
43     priData->ctrlLeftVolume.name = SND_ELEM_DACL_CAPTURE_VOL;
44     priData->ctrlRightVolume.numid = SND_NUMID_DACR_CAPTURE_VOL;
45     priData->ctrlRightVolume.name = SND_ELEM_DACR_CAPTURE_VOL;
46     CaptureSetPriData(captureIns, (CapturePriData)priData);
47 
48     return HDF_SUCCESS;
49 }
50 
CaptureSelectSceneImpl(struct AlsaCapture * captureIns,enum AudioPortPin descPins,const struct PathDeviceInfo * deviceInfo)51 static int32_t CaptureSelectSceneImpl(struct AlsaCapture *captureIns, enum AudioPortPin descPins,
52     const struct PathDeviceInfo *deviceInfo)
53 {
54     CHECK_NULL_PTR_RETURN_DEFAULT(captureIns);
55     captureIns->descPins = descPins;
56     return HDF_SUCCESS;
57 }
58 
CaptureGetVolThresholdImpl(struct AlsaCapture * captureIns,long * volMin,long * volMax)59 static int32_t CaptureGetVolThresholdImpl(struct AlsaCapture *captureIns, long *volMin, long *volMax)
60 {
61     CHECK_NULL_PTR_RETURN_DEFAULT(captureIns);
62     CHECK_NULL_PTR_RETURN_DEFAULT(volMin);
63     CHECK_NULL_PTR_RETURN_DEFAULT(volMax);
64     int32_t ret = HDF_SUCCESS;
65     long volMinTmp = 0;
66     long volMaxTmp = 0;
67     struct AlsaSoundCard *cardIns = (struct AlsaSoundCard *)captureIns;
68     CaptureData *priData = CaptureGetPriData(captureIns);
69     CHECK_NULL_PTR_RETURN_DEFAULT(cardIns);
70     CHECK_NULL_PTR_RETURN_DEFAULT(priData);
71 
72     ret = SndElementReadRange(cardIns, &priData->ctrlLeftVolume, &volMinTmp, &volMaxTmp);
73     if (ret != HDF_SUCCESS) {
74         AUDIO_FUNC_LOGE("SndElementReadRange fail!");
75         return HDF_FAILURE;
76     }
77     *volMin = volMinTmp;
78     *volMax = volMaxTmp;
79 
80     return HDF_SUCCESS;
81 }
82 
CaptureGetVolumeImpl(struct AlsaCapture * captureIns,long * volume)83 static int32_t CaptureGetVolumeImpl(struct AlsaCapture *captureIns, long *volume)
84 {
85     CHECK_NULL_PTR_RETURN_DEFAULT(captureIns);
86     CHECK_NULL_PTR_RETURN_DEFAULT(volume);
87     int32_t ret = HDF_SUCCESS;
88     long volLeft = 0;
89     long volRight = 0;
90     struct AlsaSoundCard *cardIns = (struct AlsaSoundCard *)captureIns;
91     CaptureData *priData = CaptureGetPriData(captureIns);
92     CHECK_NULL_PTR_RETURN_DEFAULT(cardIns);
93     CHECK_NULL_PTR_RETURN_DEFAULT(priData);
94 
95     ret = SndElementReadInt(cardIns, &priData->ctrlLeftVolume, &volLeft);
96     if (ret != HDF_SUCCESS) {
97         AUDIO_FUNC_LOGE("Read left volume fail!");
98         return HDF_FAILURE;
99     }
100     ret = SndElementReadInt(cardIns, &priData->ctrlRightVolume, &volRight);
101     if (ret != HDF_SUCCESS) {
102         AUDIO_FUNC_LOGE("Read right volume fail!");
103         return HDF_FAILURE;
104     }
105     *volume = (volLeft + volRight) >> 1;
106 
107     return HDF_SUCCESS;
108 }
109 
CaptureSetVolumeImpl(struct AlsaCapture * captureIns,long volume)110 static int32_t CaptureSetVolumeImpl(struct AlsaCapture *captureIns, long volume)
111 {
112     CHECK_NULL_PTR_RETURN_DEFAULT(captureIns);
113     int32_t ret = HDF_SUCCESS;
114     struct AlsaSoundCard *cardIns = (struct AlsaSoundCard *)captureIns;
115     CaptureData *priData = CaptureGetPriData(captureIns);
116     CHECK_NULL_PTR_RETURN_DEFAULT(cardIns);
117     CHECK_NULL_PTR_RETURN_DEFAULT(priData);
118     ret = SndElementWriteInt(cardIns, &priData->ctrlLeftVolume, volume);
119     if (ret != HDF_SUCCESS) {
120         AUDIO_FUNC_LOGE("Write left volume fail!");
121         return HDF_FAILURE;
122     }
123     ret = SndElementWriteInt(cardIns, &priData->ctrlRightVolume, volume);
124     if (ret != HDF_SUCCESS) {
125         AUDIO_FUNC_LOGE("Write right volume fail!");
126         return HDF_FAILURE;
127     }
128 
129     return HDF_SUCCESS;
130 }
131 
CaptureSetMuteImpl(struct AlsaCapture * captureIns,bool muteFlag)132 static int32_t CaptureSetMuteImpl(struct AlsaCapture *captureIns, bool muteFlag)
133 {
134     CHECK_NULL_PTR_RETURN_DEFAULT(captureIns);
135     int32_t ret = HDF_SUCCESS;
136     long vol = 0;
137     long setVol = 0;
138     CaptureData *priData = CaptureGetPriData(captureIns);
139     CHECK_NULL_PTR_RETURN_DEFAULT(captureIns);
140     CHECK_NULL_PTR_RETURN_DEFAULT(priData);
141     ret = captureIns->GetVolume(captureIns, &vol);
142     if (ret != HDF_SUCCESS) {
143         AUDIO_FUNC_LOGE("GetVolume failed!");
144         return HDF_FAILURE;
145     }
146 
147     if (muteFlag) {
148         priData->tempVolume = vol;
149         setVol = 0;
150     } else {
151         setVol = priData->tempVolume;
152     }
153     captureIns->SetVolume(captureIns, setVol);
154     if (ret != HDF_SUCCESS) {
155         AUDIO_FUNC_LOGE("SetVolume failed!");
156         return HDF_FAILURE;
157     }
158     captureIns->muteState = muteFlag;
159     return HDF_SUCCESS;
160 }
161 
CaptureStartImpl(struct AlsaCapture * captureIns)162 static int32_t CaptureStartImpl(struct AlsaCapture *captureIns)
163 {
164     return HDF_SUCCESS;
165 }
166 
CaptureStopImpl(struct AlsaCapture * captureIns)167 static int32_t CaptureStopImpl(struct AlsaCapture *captureIns)
168 {
169     CHECK_NULL_PTR_RETURN_DEFAULT(captureIns);
170     snd_pcm_drop(captureIns->soundCard.pcmHandle);
171     return HDF_SUCCESS;
172 }
173 
CaptureGetGainThresholdImpl(struct AlsaCapture * captureIns,float * gainMin,float * gainMax)174 static int32_t CaptureGetGainThresholdImpl(struct AlsaCapture *captureIns, float *gainMin, float *gainMax)
175 {
176     AUDIO_FUNC_LOGE("emulator not support gain operation");
177     return HDF_SUCCESS;
178 }
179 
CaptureGetGainImpl(struct AlsaCapture * captureIns,float * volume)180 static int32_t CaptureGetGainImpl(struct AlsaCapture *captureIns, float *volume)
181 {
182     AUDIO_FUNC_LOGE("emulator not support gain operation");
183     return HDF_SUCCESS;
184 }
185 
CaptureSetGainImpl(struct AlsaCapture * captureIns,float volume)186 static int32_t CaptureSetGainImpl(struct AlsaCapture *captureIns, float volume)
187 {
188     AUDIO_FUNC_LOGE("emulator not support gain operation");
189     return HDF_SUCCESS;
190 }
191 
CaptureGetMuteImpl(struct AlsaCapture * captureIns)192 static bool CaptureGetMuteImpl(struct AlsaCapture *captureIns)
193 {
194     CHECK_NULL_PTR_RETURN_DEFAULT(captureIns);
195     return captureIns->muteState;
196 }
197 
CaptureOverrideFunc(struct AlsaCapture * captureIns)198 int32_t CaptureOverrideFunc(struct AlsaCapture *captureIns)
199 {
200     if (captureIns == NULL) {
201         return HDF_FAILURE;
202     }
203     struct AlsaSoundCard *cardIns = (struct AlsaSoundCard *)captureIns;
204 
205     if (cardIns->cardType == SND_CARD_PRIMARY) {
206         captureIns->Init = CaptureInitImpl;
207         captureIns->SelectScene = CaptureSelectSceneImpl;
208         captureIns->Start = CaptureStartImpl;
209         captureIns->Stop = CaptureStopImpl;
210         captureIns->GetVolThreshold = CaptureGetVolThresholdImpl;
211         captureIns->GetVolume = CaptureGetVolumeImpl;
212         captureIns->SetVolume = CaptureSetVolumeImpl;
213         captureIns->GetGainThreshold = CaptureGetGainThresholdImpl;
214         captureIns->GetGain = CaptureGetGainImpl;
215         captureIns->SetGain = CaptureSetGainImpl;
216         captureIns->GetMute = CaptureGetMuteImpl;
217         captureIns->SetMute = CaptureSetMuteImpl;
218     }
219 
220     return HDF_SUCCESS;
221 }