1 /*
2  * Copyright (c) 2022 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 "ohos_adapter/bridge/ark_audio_system_manager_adapter_wrapper.h"
17 
18 #include "ohos_adapter/bridge/ark_audio_device_desc_adapter_wrapper.h"
19 #include "ohos_adapter/bridge/ark_audio_interrupt_adapter_impl.h"
20 #include "ohos_adapter/bridge/ark_audio_manager_callback_adapter_impl.h"
21 #include "ohos_adapter/bridge/ark_audio_manager_device_change_callback_adapter_impl.h"
22 #include "ohos_adapter/ctocpp/ark_audio_device_desc_adapter_vector_ctocpp.h"
23 
24 #include "base/bridge/ark_web_bridge_macros.h"
25 
26 namespace OHOS::ArkWeb {
27 
ArkAudioSystemManagerAdapterWrapper(ArkWebRefPtr<ArkAudioSystemManagerAdapter> ref)28 ArkAudioSystemManagerAdapterWrapper::ArkAudioSystemManagerAdapterWrapper(ArkWebRefPtr<ArkAudioSystemManagerAdapter> ref)
29     : ctocpp_(ref)
30 {}
31 
HasAudioOutputDevices()32 bool ArkAudioSystemManagerAdapterWrapper::HasAudioOutputDevices()
33 {
34     if (!ctocpp_) {
35         return false;
36     }
37     return ctocpp_->HasAudioOutputDevices();
38 }
39 
HasAudioInputDevices()40 bool ArkAudioSystemManagerAdapterWrapper::HasAudioInputDevices()
41 {
42     if (!ctocpp_) {
43         return false;
44     }
45     return ctocpp_->HasAudioInputDevices();
46 }
47 
RequestAudioFocus(const std::shared_ptr<NWeb::AudioInterruptAdapter> audioInterrupt)48 int32_t ArkAudioSystemManagerAdapterWrapper::RequestAudioFocus(
49     const std::shared_ptr<NWeb::AudioInterruptAdapter> audioInterrupt)
50 {
51     if (!ctocpp_) {
52         return -1;
53     }
54 
55     if (!audioInterrupt) {
56         return ctocpp_->RequestAudioFocus(nullptr);
57     }
58 
59     return ctocpp_->RequestAudioFocus(new ArkAudioInterruptAdapterImpl(audioInterrupt));
60 }
61 
AbandonAudioFocus(const std::shared_ptr<NWeb::AudioInterruptAdapter> audioInterrupt)62 int32_t ArkAudioSystemManagerAdapterWrapper::AbandonAudioFocus(
63     const std::shared_ptr<NWeb::AudioInterruptAdapter> audioInterrupt)
64 {
65     if (!ctocpp_) {
66         return -1;
67     }
68 
69     if (!audioInterrupt) {
70         return ctocpp_->AbandonAudioFocus(nullptr);
71     }
72 
73     return ctocpp_->AbandonAudioFocus(new ArkAudioInterruptAdapterImpl(audioInterrupt));
74 }
75 
SetAudioManagerInterruptCallback(std::shared_ptr<OHOS::NWeb::AudioManagerCallbackAdapter> callback)76 int32_t ArkAudioSystemManagerAdapterWrapper::SetAudioManagerInterruptCallback(
77     std::shared_ptr<OHOS::NWeb::AudioManagerCallbackAdapter> callback)
78 {
79     if (!ctocpp_) {
80         return -1;
81     }
82     if (CHECK_SHARED_PTR_IS_NULL(callback)) {
83         return ctocpp_->SetAudioManagerInterruptCallback(nullptr);
84     }
85 
86     return ctocpp_->SetAudioManagerInterruptCallback(new ArkAudioManagerCallbackAdapterImpl(callback));
87 }
88 
UnsetAudioManagerInterruptCallback()89 int32_t ArkAudioSystemManagerAdapterWrapper::UnsetAudioManagerInterruptCallback()
90 {
91     if (!ctocpp_) {
92         return -1;
93     }
94     return ctocpp_->UnsetAudioManagerInterruptCallback();
95 }
96 
GetDevices(NWeb::AdapterDeviceFlag flag)97 std::vector<std::shared_ptr<NWeb::AudioDeviceDescAdapter>> ArkAudioSystemManagerAdapterWrapper::GetDevices(
98     NWeb::AdapterDeviceFlag flag)
99 {
100     if (!ctocpp_) {
101         return std::vector<std::shared_ptr<NWeb::AudioDeviceDescAdapter>>();
102     }
103 
104     ArkAudioDeviceDescAdapterVector ark_vector = ctocpp_->GetDevices((int32_t)flag);
105 
106     std::vector<std::shared_ptr<NWeb::AudioDeviceDescAdapter>> result =
107         ArkAudioDeviceDescAdapterVectorStructToClass(ark_vector);
108 
109     ArkAudioDeviceDescAdapterVectorStructRelease(ark_vector);
110 
111     return result;
112 }
113 
SelectAudioDeviceById(int32_t deviceId,bool isInput)114 int32_t ArkAudioSystemManagerAdapterWrapper::SelectAudioDeviceById(int32_t deviceId, bool isInput)
115 {
116     if (!ctocpp_) {
117         return -1;
118     }
119 
120     return ctocpp_->SelectAudioDeviceById(deviceId, isInput);
121 }
122 
SetDeviceChangeCallback(const std::shared_ptr<OHOS::NWeb::AudioManagerDeviceChangeCallbackAdapter> callback)123 int32_t ArkAudioSystemManagerAdapterWrapper::SetDeviceChangeCallback(
124     const std::shared_ptr<OHOS::NWeb::AudioManagerDeviceChangeCallbackAdapter> callback)
125 {
126     if (!ctocpp_) {
127         return -1;
128     }
129 
130     if (CHECK_SHARED_PTR_IS_NULL(callback)) {
131         return ctocpp_->SetDeviceChangeCallback(nullptr);
132     }
133 
134     return ctocpp_->SetDeviceChangeCallback(new ArkAudioManagerDeviceChangeCallbackAdapterImpl(callback));
135 }
136 
UnsetDeviceChangeCallback()137 int32_t ArkAudioSystemManagerAdapterWrapper::UnsetDeviceChangeCallback()
138 {
139     if (!ctocpp_) {
140         return -1;
141     }
142     return ctocpp_->UnsetDeviceChangeCallback();
143 }
144 
GetDefaultOutputDevice()145 std::shared_ptr<NWeb::AudioDeviceDescAdapter> ArkAudioSystemManagerAdapterWrapper::GetDefaultOutputDevice()
146 {
147     if (!ctocpp_) {
148         return nullptr;
149     }
150 
151     ArkWebRefPtr<ArkAudioDeviceDescAdapter> adapter = ctocpp_->GetDefaultOutputDevice();
152     if (CHECK_REF_PTR_IS_NULL(adapter)) {
153         return nullptr;
154     }
155 
156     return std::make_shared<ArkAudioDeviceDescAdapterWrapper>(adapter);
157 }
158 
GetDefaultInputDevice()159 std::shared_ptr<NWeb::AudioDeviceDescAdapter> ArkAudioSystemManagerAdapterWrapper::GetDefaultInputDevice()
160 {
161     if (!ctocpp_) {
162         return nullptr;
163     }
164 
165     ArkWebRefPtr<ArkAudioDeviceDescAdapter> adapter = ctocpp_->GetDefaultInputDevice();
166     if (CHECK_REF_PTR_IS_NULL(adapter)) {
167         return nullptr;
168     }
169 
170     return std::make_shared<ArkAudioDeviceDescAdapterWrapper>(adapter);
171 }
172 
SetLanguage(const std::string & language)173 bool ArkAudioSystemManagerAdapterWrapper::SetLanguage(const std::string& language)
174 {
175     if (!ctocpp_) {
176         return false;
177     }
178 
179     return ctocpp_->SetLanguage(ArkWebStringClassToStruct(language));
180 }
181 
182 } // namespace OHOS::ArkWeb
183