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 "ohos_adapter/bridge/ark_codec_callback_adapter_wrapper.h"
17 
18 #include "ohos_adapter/bridge/ark_buffer_info_adapter_impl.h"
19 #include "ohos_adapter/bridge/ark_codec_format_adapter_impl.h"
20 #include "ohos_adapter/bridge/ark_ohos_buffer_adapter_impl.h"
21 
22 #include "base/bridge/ark_web_bridge_macros.h"
23 
24 namespace OHOS::ArkWeb {
25 
ArkCodecCallbackAdapterWapper(ArkWebRefPtr<ArkCodecCallbackAdapter> ref)26 ArkCodecCallbackAdapterWapper::ArkCodecCallbackAdapterWapper(ArkWebRefPtr<ArkCodecCallbackAdapter> ref) : ctocpp_(ref)
27 {}
28 
OnError(OHOS::NWeb::ErrorType errorType,int32_t errorCode)29 void ArkCodecCallbackAdapterWapper::OnError(OHOS::NWeb::ErrorType errorType, int32_t errorCode)
30 {
31     ctocpp_->OnError((int32_t)errorType, errorCode);
32 }
33 
OnStreamChanged(const std::shared_ptr<NWeb::CodecFormatAdapter> format)34 void ArkCodecCallbackAdapterWapper::OnStreamChanged(const std::shared_ptr<NWeb::CodecFormatAdapter> format)
35 {
36     if (CHECK_SHARED_PTR_IS_NULL(format)) {
37         return ctocpp_->OnStreamChanged(nullptr);
38     }
39 
40     ctocpp_->OnStreamChanged(new ArkCodecFormatAdapterImpl(format));
41 }
42 
OnNeedInputData(uint32_t index,std::shared_ptr<NWeb::OhosBufferAdapter> buffer)43 void ArkCodecCallbackAdapterWapper::OnNeedInputData(uint32_t index, std::shared_ptr<NWeb::OhosBufferAdapter> buffer)
44 {
45     if (CHECK_SHARED_PTR_IS_NULL(buffer)) {
46         return ctocpp_->OnNeedInputData(index, nullptr);
47     }
48 
49     ctocpp_->OnNeedInputData(index, new ArkOhosBufferAdapterImpl(buffer));
50 }
51 
OnNeedOutputData(uint32_t index,std::shared_ptr<NWeb::BufferInfoAdapter> info,NWeb::BufferFlag flag,std::shared_ptr<NWeb::OhosBufferAdapter> buffer)52 void ArkCodecCallbackAdapterWapper::OnNeedOutputData(uint32_t index, std::shared_ptr<NWeb::BufferInfoAdapter> info,
53     NWeb::BufferFlag flag, std::shared_ptr<NWeb::OhosBufferAdapter> buffer)
54 {
55     if (CHECK_SHARED_PTR_IS_NULL(info) && CHECK_SHARED_PTR_IS_NULL(buffer)) {
56         ctocpp_->OnNeedOutputData(index, nullptr, (uint32_t)flag, nullptr);
57     } else if (!CHECK_SHARED_PTR_IS_NULL(info) && !CHECK_SHARED_PTR_IS_NULL(buffer)) {
58         ctocpp_->OnNeedOutputData(
59             index, new ArkBufferInfoAdapterImpl(info), (uint32_t)flag, new ArkOhosBufferAdapterImpl(buffer));
60     } else if (CHECK_SHARED_PTR_IS_NULL(info)) {
61         ctocpp_->OnNeedOutputData(index, nullptr, (uint32_t)flag, new ArkOhosBufferAdapterImpl(buffer));
62     } else {
63         ctocpp_->OnNeedOutputData(index, new ArkBufferInfoAdapterImpl(info), (uint32_t)flag, nullptr);
64     }
65 }
66 
67 } // namespace OHOS::ArkWeb
68