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 "multimedia_audio_renderer_callback.h"
17
18 #include "multimedia_audio_common.h"
19
20 namespace OHOS {
21 namespace AudioStandard {
RegisterFunc(std::function<void (int64_t)> cjCallback)22 void CjRendererPositionCallback::RegisterFunc(std::function<void(int64_t)> cjCallback)
23 {
24 std::lock_guard<std::mutex> lock(cbMutex_);
25 func_ = cjCallback;
26 }
27
OnMarkReached(const int64_t & framePosition)28 void CjRendererPositionCallback::OnMarkReached(const int64_t& framePosition)
29 {
30 std::lock_guard<std::mutex> lock(cbMutex_);
31 if (func_ != nullptr) {
32 func_(framePosition);
33 }
34 }
35
RegisterFunc(std::function<void (int64_t)> cjCallback)36 void CjRendererPeriodPositionCallback::RegisterFunc(std::function<void(int64_t)> cjCallback)
37 {
38 std::lock_guard<std::mutex> lock(cbMutex_);
39 func_ = cjCallback;
40 }
41
OnPeriodReached(const int64_t & frameNumber)42 void CjRendererPeriodPositionCallback::OnPeriodReached(const int64_t& frameNumber)
43 {
44 std::lock_guard<std::mutex> lock(cbMutex_);
45 if (func_ != nullptr) {
46 func_(frameNumber);
47 }
48 }
49
RegisterFunc(std::function<void (CArrDeviceDescriptor)> cjCallback)50 void CjAudioRendererOutputDeviceChangeCallback::RegisterFunc(std::function<void(CArrDeviceDescriptor)> cjCallback)
51 {
52 std::lock_guard<std::mutex> lock(cbMutex_);
53 func_ = cjCallback;
54 }
55
OnOutputDeviceChange(const AudioDeviceDescriptor & deviceInfo,const AudioStreamDeviceChangeReason reason)56 void CjAudioRendererOutputDeviceChangeCallback::OnOutputDeviceChange(
57 const AudioDeviceDescriptor& deviceInfo, const AudioStreamDeviceChangeReason reason)
58 {
59 std::lock_guard<std::mutex> lock(cbMutex_);
60 if (func_ == nullptr) {
61 return;
62 }
63 CArrDeviceDescriptor arr {};
64 int32_t errorCode = SUCCESS_CODE;
65 Convert2CArrDeviceDescriptorByDeviceInfo(arr, deviceInfo, &errorCode);
66 if (errorCode != SUCCESS_CODE) {
67 FreeCArrDeviceDescriptor(arr);
68 return;
69 }
70 func_(arr);
71 FreeCArrDeviceDescriptor(arr);
72 }
73
RegisterFunc(std::function<void (CAudioStreamDeviceChangeInfo)> cjCallback)74 void CjAudioRendererOutputDeviceChangeWithInfoCallback::RegisterFunc(
75 std::function<void(CAudioStreamDeviceChangeInfo)> cjCallback)
76 {
77 std::lock_guard<std::mutex> lock(cbMutex_);
78 func_ = cjCallback;
79 }
80
OnOutputDeviceChange(const AudioDeviceDescriptor & deviceInfo,const AudioStreamDeviceChangeReason reason)81 void CjAudioRendererOutputDeviceChangeWithInfoCallback::OnOutputDeviceChange(
82 const AudioDeviceDescriptor& deviceInfo, const AudioStreamDeviceChangeReason reason)
83 {
84 std::lock_guard<std::mutex> lock(cbMutex_);
85 if (func_ == nullptr) {
86 return;
87 }
88 CAudioStreamDeviceChangeInfo info {};
89 CArrDeviceDescriptor arr {};
90 int32_t errorCode = SUCCESS_CODE;
91 Convert2CArrDeviceDescriptorByDeviceInfo(arr, deviceInfo, &errorCode);
92 if (errorCode != SUCCESS_CODE) {
93 FreeCArrDeviceDescriptor(arr);
94 return;
95 }
96 info.deviceDescriptors = arr;
97 info.changeReason = static_cast<int32_t>(reason);
98 func_(info);
99 FreeCArrDeviceDescriptor(arr);
100 }
101
RegisterFunc(std::function<int32_t (CArrUI8)> cjCallback,std::shared_ptr<AudioRenderer> audioRenderer)102 void CjAudioRendererWriteCallback::RegisterFunc(
103 std::function<int32_t(CArrUI8)> cjCallback, std::shared_ptr<AudioRenderer> audioRenderer)
104 {
105 std::lock_guard<std::mutex> lock(cbMutex_);
106 func_ = cjCallback;
107 audioRenderer_ = audioRenderer;
108 }
109
OnWriteData(size_t length)110 void CjAudioRendererWriteCallback::OnWriteData(size_t length)
111 {
112 std::lock_guard<std::mutex> lock(cbMutex_);
113 if (func_ == nullptr) {
114 return;
115 }
116 CArrUI8 arr {};
117 BufferDesc buf {};
118 audioRenderer_->GetBufferDesc(buf);
119 if (buf.buffer == nullptr) {
120 return;
121 }
122 arr.size = std::min(length, buf.bufLength);
123 int32_t mallocSize = static_cast<int32_t>(sizeof(uint8_t)) * static_cast<int32_t>(arr.size);
124 if (mallocSize <= 0 || mallocSize > static_cast<int32_t>(sizeof(uint8_t) * MAX_MEM_MALLOC_SIZE)) {
125 return;
126 }
127 arr.head = static_cast<uint8_t*>(malloc(mallocSize));
128 if (arr.head == nullptr) {
129 return;
130 }
131 if (memset_s(arr.head, mallocSize, 0, mallocSize) != EOK) {
132 free(arr.head);
133 arr.head = nullptr;
134 return;
135 }
136 for (int32_t i = 0; i < static_cast<int32_t>(arr.size); i++) {
137 arr.head[i] = buf.buffer[i];
138 }
139 func_(arr);
140 free(arr.head);
141 arr.head = nullptr;
142 }
143
RegisterFunc(std::function<void (int32_t)> cjCallback)144 void CjAudioRendererCallback::RegisterFunc(std::function<void(int32_t)> cjCallback)
145 {
146 std::lock_guard<std::mutex> lock(cbMutex_);
147 stateChangeCallback_ = cjCallback;
148 }
149
RegisterInterruptFunc(std::function<void (CInterruptEvent)> cjCallback)150 void CjAudioRendererCallback::RegisterInterruptFunc(std::function<void(CInterruptEvent)> cjCallback)
151 {
152 std::lock_guard<std::mutex> lock(cbMutex_);
153 interruptCallback_ = cjCallback;
154 }
155
OnInterrupt(const InterruptEvent & interruptEvent)156 void CjAudioRendererCallback::OnInterrupt(const InterruptEvent& interruptEvent)
157 {
158 std::lock_guard<std::mutex> lock(cbMutex_);
159 if (interruptCallback_ == nullptr) {
160 return;
161 }
162 CInterruptEvent event {};
163 event.eventType = static_cast<int32_t>(interruptEvent.eventType);
164 event.forceType = static_cast<int32_t>(interruptEvent.forceType);
165 event.hintType = static_cast<int32_t>(interruptEvent.hintType);
166 interruptCallback_(event);
167 }
168
OnStateChange(const RendererState state,const StateChangeCmdType cmdType)169 void CjAudioRendererCallback::OnStateChange(
170 const RendererState state, const StateChangeCmdType __attribute__((unused)) cmdType)
171 {
172 std::lock_guard<std::mutex> lock(cbMutex_);
173 if (stateChangeCallback_ == nullptr) {
174 return;
175 }
176 stateChangeCallback_(static_cast<int32_t>(state));
177 }
178 } // namespace AudioStandard
179 } // namespace OHOS
180