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