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