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