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 "multimedia_audio_stream_manager_callback.h"
16 #include "multimedia_audio_common.h"
17 #include "multimedia_audio_error.h"
18
19 namespace OHOS {
20 namespace AudioStandard {
RegisterFunc(std::function<void (CArrAudioCapturerChangeInfo)> cjCallback)21 void CjAudioCapturerStateChangeCallback::RegisterFunc(std::function<void(CArrAudioCapturerChangeInfo)> cjCallback)
22 {
23 func_ = cjCallback;
24 }
25
OnCapturerStateChange(const std::vector<std::shared_ptr<AudioCapturerChangeInfo>> & audioCapturerChangeInfos)26 void CjAudioCapturerStateChangeCallback::OnCapturerStateChange(
27 const std::vector<std::shared_ptr<AudioCapturerChangeInfo>> &audioCapturerChangeInfos)
28 {
29 std::lock_guard<std::mutex> lock(cbMutex_);
30 if (func_ == nullptr) {
31 return;
32 }
33 CArrAudioCapturerChangeInfo arrInfo{};
34 arrInfo.size = static_cast<int64_t>(audioCapturerChangeInfos.size());
35 int32_t mallocSize = static_cast<int32_t>(sizeof(CAudioCapturerChangeInfo)) * static_cast<int32_t>(arrInfo.size);
36 if (mallocSize <= 0 || mallocSize > static_cast<int32_t>(sizeof(CAudioCapturerChangeInfo) * MAX_MEM_MALLOC_SIZE)) {
37 return;
38 }
39 auto head = static_cast<CAudioCapturerChangeInfo *>(malloc(mallocSize));
40 if (head == nullptr) {
41 return;
42 }
43 int32_t errorCode = SUCCESS_CODE;
44 arrInfo.head = head;
45 if (memset_s(head, mallocSize, 0, mallocSize) != EOK) {
46 free(head);
47 return;
48 }
49 for (int64_t i = 0; i < arrInfo.size; i++) {
50 Convert2CAudioCapturerChangeInfo(head[i], *(audioCapturerChangeInfos[i]), &errorCode);
51 if (errorCode != SUCCESS_CODE) {
52 FreeCArrAudioCapturerChangeInfo(arrInfo);
53 return;
54 }
55 }
56 func_(arrInfo);
57 FreeCArrAudioCapturerChangeInfo(arrInfo);
58 }
59
RegisterFunc(std::function<void (CArrAudioRendererChangeInfo)> cjCallback)60 void CjAudioRendererStateChangeCallback::RegisterFunc(std::function<void(CArrAudioRendererChangeInfo)> cjCallback)
61 {
62 func_ = cjCallback;
63 }
64
OnRendererStateChange(const std::vector<std::shared_ptr<AudioRendererChangeInfo>> & audioRendererChangeInfos)65 void CjAudioRendererStateChangeCallback::OnRendererStateChange(
66 const std::vector<std::shared_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos)
67 {
68 std::lock_guard<std::mutex> lock(cbMutex_);
69 if (func_ == nullptr) {
70 return;
71 }
72 CArrAudioRendererChangeInfo arrInfo{};
73 arrInfo.size = static_cast<int64_t>(audioRendererChangeInfos.size());
74 int32_t mallocSize = static_cast<int32_t>(sizeof(CAudioRendererChangeInfo)) * static_cast<int32_t>(arrInfo.size);
75 if (mallocSize <= 0 || mallocSize > static_cast<int32_t>(sizeof(CAudioRendererChangeInfo) * MAX_MEM_MALLOC_SIZE)) {
76 return;
77 }
78 auto head = static_cast<CAudioRendererChangeInfo *>(malloc(mallocSize));
79 if (head == nullptr) {
80 return;
81 }
82 int32_t errorCode = SUCCESS_CODE;
83 arrInfo.head = head;
84 if (memset_s(head, mallocSize, 0, mallocSize) != EOK) {
85 free(head);
86 return;
87 }
88 for (int64_t i = 0; i < arrInfo.size; i++) {
89 Convert2CAudioRendererChangeInfo(head[i], *(audioRendererChangeInfos[i]), &errorCode);
90 if (errorCode != SUCCESS_CODE) {
91 FreeCArrAudioRendererChangeInfo(arrInfo);
92 return;
93 }
94 }
95 func_(arrInfo);
96 FreeCArrAudioRendererChangeInfo(arrInfo);
97 }
98 } // namespace AudioStandard
99 } // namespace OHOS
100