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 #include <cinttypes>
16 #include <hdf_log.h>
17 #include "audio_proxy_common.h"
18
19 namespace OHOS::HDI::Audio_Bluetooth {
AudioProxyCaptureCtrl(int cmId,AudioHandle handle)20 int32_t AudioProxyCaptureCtrl(int cmId, AudioHandle handle)
21 {
22 struct HdfSBuf *data = nullptr;
23 struct HdfSBuf *reply = nullptr;
24 struct AudioHwCapture *hwCapture = reinterpret_cast<struct AudioHwCapture *>(handle);
25 if (hwCapture == nullptr || hwCapture->proxyRemoteHandle == nullptr) {
26 HDF_LOGE("The hwCapture parameter is null");
27 return AUDIO_HAL_ERR_INVALID_PARAM;
28 }
29 if (AudioProxyPreprocessCapture(hwCapture, &data, &reply) < 0) {
30 return AUDIO_HAL_ERR_INTERNAL;
31 }
32 int32_t ret = AudioProxyDispatchCall(hwCapture->proxyRemoteHandle, cmId, data, reply);
33 AudioProxyBufReplyRecycle(data, reply);
34 return ret;
35 }
36
AudioProxyCaptureStart(AudioHandle handle)37 int32_t AudioProxyCaptureStart(AudioHandle handle)
38 {
39 HDF_LOGI("%{public}s", __func__);
40 return AudioProxyCaptureCtrl(AUDIO_HDI_CAPTURE_START, handle);
41 }
42
AudioProxyCaptureStop(AudioHandle handle)43 int32_t AudioProxyCaptureStop(AudioHandle handle)
44 {
45 HDF_LOGI("%{public}s", __func__);
46 return AudioProxyCaptureCtrl(AUDIO_HDI_CAPTURE_STOP, handle);
47 }
48
AudioProxyCapturePause(AudioHandle handle)49 int32_t AudioProxyCapturePause(AudioHandle handle)
50 {
51 HDF_LOGI("%{public}s", __func__);
52 return AudioProxyCaptureCtrl(AUDIO_HDI_CAPTURE_PAUSE, handle);
53 }
54
AudioProxyCaptureResume(AudioHandle handle)55 int32_t AudioProxyCaptureResume(AudioHandle handle)
56 {
57 HDF_LOGI("%{public}s", __func__);
58 return AudioProxyCaptureCtrl(AUDIO_HDI_CAPTURE_RESUME, handle);
59 }
60
AudioProxyCaptureFlush(AudioHandle handle)61 int32_t AudioProxyCaptureFlush(AudioHandle handle)
62 {
63 struct AudioHwCapture *hwCapture = reinterpret_cast<struct AudioHwCapture *>(handle);
64 if (hwCapture == nullptr) {
65 return AUDIO_HAL_ERR_INVALID_PARAM;
66 }
67 return AUDIO_HAL_ERR_NOT_SUPPORT;
68 }
69
AudioProxyCaptureSetMute(const AudioHandle handle,bool mute)70 int32_t AudioProxyCaptureSetMute(const AudioHandle handle, bool mute)
71 {
72 (void)mute;
73 struct HdfSBuf *data = nullptr;
74 struct HdfSBuf *reply = nullptr;
75 struct AudioHwCapture *hwCapture = reinterpret_cast<struct AudioHwCapture *>(handle);
76 if (hwCapture == nullptr || hwCapture->proxyRemoteHandle == nullptr) {
77 HDF_LOGE("The pointer is null");
78 return AUDIO_HAL_ERR_INVALID_PARAM;
79 }
80 if (AudioProxyPreprocessCapture(hwCapture, &data, &reply) < 0) {
81 return AUDIO_HAL_ERR_INTERNAL;
82 }
83 auto tempMute = static_cast<uint32_t>(mute);
84 if (!HdfSbufWriteUint32(data, tempMute)) {
85 AudioProxyBufReplyRecycle(data, reply);
86 return AUDIO_HAL_ERR_INTERNAL;
87 }
88 int32_t ret = AudioProxyDispatchCall(hwCapture->proxyRemoteHandle, AUDIO_HDI_CAPTURE_SET_MUTE, data, reply);
89 AudioProxyBufReplyRecycle(data, reply);
90 return ret;
91 }
92
AudioProxyCaptureGetMute(const AudioHandle handle,bool * mute)93 int32_t AudioProxyCaptureGetMute(const AudioHandle handle, bool *mute)
94 {
95 if (mute == nullptr) {
96 return AUDIO_HAL_ERR_INVALID_PARAM;
97 }
98 struct HdfSBuf *data = nullptr;
99 struct HdfSBuf *reply = nullptr;
100 struct AudioHwCapture *hwCapture = reinterpret_cast<struct AudioHwCapture *>(handle);
101 if (hwCapture == nullptr || hwCapture->proxyRemoteHandle == nullptr) {
102 HDF_LOGE("The parameter is null");
103 return AUDIO_HAL_ERR_INVALID_PARAM;
104 }
105 if (AudioProxyPreprocessCapture(hwCapture, &data, &reply) < 0) {
106 return AUDIO_HAL_ERR_INTERNAL;
107 }
108 int32_t ret = AudioProxyDispatchCall(hwCapture->proxyRemoteHandle, AUDIO_HDI_CAPTURE_GET_MUTE, data, reply);
109 uint32_t tempMute = 0;
110 if (!HdfSbufReadUint32(reply, &tempMute)) {
111 AudioProxyBufReplyRecycle(data, reply);
112 return AUDIO_HAL_ERR_INTERNAL;
113 }
114 *mute = static_cast<bool>(tempMute);
115 AudioProxyBufReplyRecycle(data, reply);
116 LOG_PARA_INFO("GetMute SUCCESS!");
117 return ret;
118 }
119
ReadReplyBuffer(struct HdfSBuf * reply,uint64_t replyBytes,void * frame,uint64_t requestBytes)120 bool ReadReplyBuffer(struct HdfSBuf *reply, uint64_t replyBytes, void *frame, uint64_t requestBytes)
121 {
122 const void *rBuf = nullptr;
123 uint32_t rLen;
124 if (HdfSbufReadBuffer(reply, &rBuf, &rLen) && rLen == replyBytes) {
125 if (memcpy_s(frame, requestBytes, rBuf, rLen) == EOK) {
126 return true;
127 }
128 }
129 return false;
130 }
131
AudioProxyCaptureCaptureFrame(struct AudioCapture * capture,void * frame,uint64_t requestBytes,uint64_t * replyBytes)132 int32_t AudioProxyCaptureCaptureFrame(struct AudioCapture *capture, void *frame,
133 uint64_t requestBytes, uint64_t *replyBytes)
134 {
135 HDF_LOGD("%{public}s", __func__);
136 if (frame == nullptr || replyBytes == nullptr) {
137 HDF_LOGE("Capture Frame Paras is NULL!");
138 return AUDIO_HAL_ERR_INVALID_PARAM;
139 }
140 struct HdfSBuf *data = nullptr;
141 struct HdfSBuf *reply = nullptr;
142 struct AudioHwCapture *hwCapture = reinterpret_cast<struct AudioHwCapture *>(capture);
143 if (hwCapture == nullptr || hwCapture->proxyRemoteHandle == nullptr) {
144 HDF_LOGE("The hwCapture parameter is null");
145 return AUDIO_HAL_ERR_INVALID_PARAM;
146 }
147 if (AudioProxyPreprocessCapture(hwCapture, &data, &reply) < 0) {
148 HDF_LOGE("AudioProxyPreprocessCapture FAIL");
149 return AUDIO_HAL_ERR_INTERNAL;
150 }
151 if (!HdfSbufWriteUint64(data, requestBytes)) {
152 AudioProxyBufReplyRecycle(data, reply);
153 HDF_LOGE("HdfSbufWriteUint64 FAIL");
154 return AUDIO_HAL_ERR_INTERNAL;
155 }
156 int32_t ret = AudioProxyDispatchCall(hwCapture->proxyRemoteHandle, AUDIO_HDI_CAPTURE_CAPTURE_FRAME, data, reply);
157 if (ret < 0) {
158 if (ret != AUDIO_HAL_ERR_INVALID_OBJECT) {
159 HDF_LOGE("AudioCaptureCaptureFrame FAIL");
160 }
161 AudioProxyBufReplyRecycle(data, reply);
162 return ret;
163 }
164 if (!HdfSbufReadUint64(reply, replyBytes)) {
165 AudioProxyBufReplyRecycle(data, reply);
166 HDF_LOGE("HdfSbufReadUint64 FAIL");
167 return AUDIO_HAL_ERR_INTERNAL;
168 }
169 if (!ReadReplyBuffer(reply, *replyBytes, frame, requestBytes)) {
170 AudioProxyBufReplyRecycle(data, reply);
171 HDF_LOGE("memcpy rBuf failed");
172 return AUDIO_HAL_ERR_INTERNAL;
173 }
174 AudioProxyBufReplyRecycle(data, reply);
175 return AUDIO_HAL_SUCCESS;
176 }
177 }