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_routing_manager_callback.h"
16 #include "audio_policy_log.h"
17 #include "multimedia_audio_common.h"
18
19 namespace OHOS {
20 namespace AudioStandard {
RegisterFunc(const uint32_t usage,std::function<void (CDeviceChangeAction)> cjCallback)21 void CjAudioManagerAvailableDeviceChangeCallback::RegisterFunc(const uint32_t usage,
22 std::function<void(CDeviceChangeAction)> cjCallback)
23 {
24 callbackList_.push_back({usage, cjCallback});
25 }
26
OnAvailableDeviceChange(const AudioDeviceUsage usage,const DeviceChangeAction & deviceChangeAction)27 void CjAudioManagerAvailableDeviceChangeCallback::OnAvailableDeviceChange(const AudioDeviceUsage usage,
28 const DeviceChangeAction &deviceChangeAction)
29 {
30 std::lock_guard<std::mutex> lock(cbMutex_);
31 std::function<void(CDeviceChangeAction)> func;
32 bool isFind{false};
33 for (auto it = callbackList_.begin(); it != callbackList_.end(); ++it) {
34 if (usage == it->first) {
35 func = it->second;
36 isFind = true;
37 break;
38 }
39 }
40 if (!isFind) {
41 AUDIO_ERR_LOG("[OnAvailableDeviceChange] Registered func is not found.");
42 return;
43 }
44 CDeviceChangeAction cDeviceChangeAct{};
45 cDeviceChangeAct.changeType = deviceChangeAction.type;
46 int32_t errorCode = SUCCESS_CODE;
47 Convert2CArrDeviceDescriptor(cDeviceChangeAct.deviceDescriptors, deviceChangeAction.deviceDescriptors, &errorCode);
48 if (errorCode != SUCCESS_CODE) {
49 FreeCArrDeviceDescriptor(cDeviceChangeAct.deviceDescriptors);
50 return;
51 }
52 func(cDeviceChangeAct);
53 FreeCArrDeviceDescriptor(cDeviceChangeAct.deviceDescriptors);
54 }
55
RegisterFunc(std::function<void (CArrDeviceDescriptor)> cjCallback)56 void CjAudioPreferredInputDeviceChangeCallback::RegisterFunc(std::function<void(CArrDeviceDescriptor)> cjCallback)
57 {
58 func_ = cjCallback;
59 }
60
OnPreferredInputDeviceUpdated(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> & desc)61 void CjAudioPreferredInputDeviceChangeCallback::OnPreferredInputDeviceUpdated(
62 const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc)
63 {
64 std::lock_guard<std::mutex> lock(cbMutex_);
65 if (func_ == nullptr) {
66 return;
67 }
68 CArrDeviceDescriptor arr{};
69 int32_t errorCode = SUCCESS_CODE;
70 Convert2CArrDeviceDescriptor(arr, desc, &errorCode);
71 if (errorCode != SUCCESS_CODE) {
72 FreeCArrDeviceDescriptor(arr);
73 return;
74 }
75 func_(arr);
76 FreeCArrDeviceDescriptor(arr);
77 }
78
RegisterFunc(std::function<void (CArrDeviceDescriptor)> cjCallback)79 void CjAudioPreferredOutputDeviceChangeCallback::RegisterFunc(std::function<void(CArrDeviceDescriptor)> cjCallback)
80 {
81 func_ = cjCallback;
82 }
83
OnPreferredOutputDeviceUpdated(const std::vector<std::shared_ptr<AudioDeviceDescriptor>> & desc)84 void CjAudioPreferredOutputDeviceChangeCallback::OnPreferredOutputDeviceUpdated(
85 const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc)
86 {
87 std::lock_guard<std::mutex> lock(cbMutex_);
88 CArrDeviceDescriptor arr{};
89 int32_t errorCode = SUCCESS_CODE;
90 Convert2CArrDeviceDescriptor(arr, desc, &errorCode);
91 if (errorCode != SUCCESS_CODE) {
92 FreeCArrDeviceDescriptor(arr);
93 return;
94 }
95 func_(arr);
96 FreeCArrDeviceDescriptor(arr);
97 }
98
RegisterFunc(std::function<void (CDeviceChangeAction)> cjCallback)99 void CjAudioManagerDeviceChangeCallback::RegisterFunc(std::function<void(CDeviceChangeAction)> cjCallback)
100 {
101 func_ = cjCallback;
102 }
103
OnDeviceChange(const DeviceChangeAction & deviceChangeAction)104 void CjAudioManagerDeviceChangeCallback::OnDeviceChange(const DeviceChangeAction &deviceChangeAction)
105 {
106 std::lock_guard<std::mutex> lock(cbMutex_);
107 if (func_ == nullptr) {
108 return;
109 }
110 CArrDeviceDescriptor arr{};
111 int32_t errorCode = SUCCESS_CODE;
112 Convert2CArrDeviceDescriptor(arr, deviceChangeAction.deviceDescriptors, &errorCode);
113 if (errorCode != SUCCESS_CODE) {
114 FreeCArrDeviceDescriptor(arr);
115 return;
116 }
117 CDeviceChangeAction action{};
118 action.deviceDescriptors = arr;
119 action.changeType = deviceChangeAction.type;
120 func_(action);
121 FreeCArrDeviceDescriptor(arr);
122 }
123 } // namespace AudioStandard
124 } // namespace OHOS
125