diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..e454a52586f29b8ce8a6799163eac1f875e9ac01 --- /dev/null +++ b/LICENSE @@ -0,0 +1,178 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + diff --git a/OAT.xml b/OAT.xml new file mode 100644 index 0000000000000000000000000000000000000000..a7d94aeda1e61d117e05c88679e374986e8c91f7 --- /dev/null +++ b/OAT.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + diff --git a/README.en.md b/README.en.md deleted file mode 100644 index c889106c91c4aafdda1b94d04bb4f892dd575499..0000000000000000000000000000000000000000 --- a/README.en.md +++ /dev/null @@ -1,36 +0,0 @@ -# distributed_screen - -#### Description -{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README.md b/README.md deleted file mode 100644 index 86aba9515eea36d46ebce9f882d5e3d79c6b579b..0000000000000000000000000000000000000000 --- a/README.md +++ /dev/null @@ -1,39 +0,0 @@ -# distributed_screen - -#### 介绍 -{**以下是 Gitee 平台说明,您可以替换此简介** -Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 -无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)} - -#### 软件架构 -软件架构说明 - - -#### 安装教程 - -1. xxxx -2. xxxx -3. xxxx - -#### 使用说明 - -1. xxxx -2. xxxx -3. xxxx - -#### 参与贡献 - -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request - - -#### 特技 - -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README_ZH.md b/README_ZH.md new file mode 100644 index 0000000000000000000000000000000000000000..b2223c472272f2b36821fe0ea1febbe4ac8b92c1 --- /dev/null +++ b/README_ZH.md @@ -0,0 +1,87 @@ +# **分布式屏幕组件** + +## **简介** + +分布式屏幕是一种屏幕虚拟化能力,支持用户指定任意设备的屏幕作为Display的显示区域。在分布式硬件子系统中,分布式屏幕提供跨设备屏幕能力调用,为OpenHarmony操作系统提供系统投屏、屏幕镜像、屏幕分割等能力的实现。 + +系统架构如下图所示: + +![](figures/distributedscreen_arch.png) + +**屏幕区域管理(ScreenRegionManager)**:管理主控端映射在被控端屏幕上的显示区域的状态,包括为显示区域指定显示的display,设置显示区域的宽高,解码类型等参数。 + +**分布式屏幕管理(DScreenManager)**:管理被控端屏幕的参数和状态,负责主控端相关对象的创建和销毁。 + +**屏幕服务(ScreenService)**:分布式屏幕主控端SA服务和分布式屏幕被控端SA服务,负责处理分布式硬件管理框架的IPC调用 + +**软总线适配器(SoftbusAdapter)**:对接软总线传输接口,为屏幕图像、输入事件等提供封装的统一调用接口,实现设备间的流数据、字节数据传输和交互。 + +**屏幕传输组件(ScreenTransport)**:分布式屏幕传输模块,实现屏幕图像数据编码、解码、发送、接收。 + +**屏幕代理客户端(ScreenClient)**:屏幕图像显示代理客户端,用于在设备上显示其他设备投射过来的屏幕图像数据。 + + +## **目录** + +``` +/foundation/distributedhardware/distributedscreen +├── common # 分布式屏幕公共数据定义,包括常量、错误码、日志、工具等 +├── interface # 分布式屏幕SDK,包含主控端和被控端服务调用接口 +├── sa_profile # 分布式屏幕的SA配置信息 +├── services # 分布式屏幕主控端和被控端功能主体业务实现 +│ └── common # 分布式屏幕功能主控端、被控端共用功能实现 +│ ├── databuffer # 屏幕数据存储定义 +│ └── screen_channel # 屏幕数据传输通道接口定义 +│ └── screenclient # 分布式屏幕代理客户端实现 +│ └── screenservice # 分布式屏幕主体功能实现 +│ └── sinkservice # 分布式屏幕被控端服务功能实现 +│ ├── dscreenservice # 分布式屏幕被控端SA +│ └── screenregionmgr # 分布式屏幕被控端显示区域管理 +│ └── sourceservice # 分布式屏幕主控端服务功能实现 +│ ├── dscreenmgr # 分布式屏幕主控端屏幕管理 +│ └── dscreenservice # 分布式屏幕主控端SA +│ └── screentransport # 分布式屏幕传输组件 +│ ├── screendatachannel # 屏幕数据传输通道,用于传输组件和编解码器之间数据传输 +│ ├── screensinkprocessor # 分布式屏幕被控端数据处理模块,包括解码等 +│ ├── screensinktrans # 分布式屏幕被控端数据传输组件,包含数据传输通道channel和数据处理模块processor +│ ├── screensourceprocessor # 分布式屏幕主控端数据处理模块,包括编码等 +│ └── screensourcetrans # 分布式屏幕主控端数据传输组件,包含数据传输通道channel和数据处理模块processor +│ └── softbusadapter # 软总线接口适配器,为屏幕传输、触控事件传输提供统一传输接口 +└── screenhandler # 分布式屏幕硬件信息上报、设备状态变化通知,由分布式硬件管理框架加载 +``` + +## **约束** +**语言限制**:C++语言。 +**组网环境**:必须确保设备在同一个局域网中。 +**操作系统限制**:OpenHarmony操作系统。 + +## **说明** +### **概念说明** +主控端(source):控制端,通过调用分布式屏幕能力,使用被控端的屏幕用于显示主控端设备的屏幕内容。 +被控端(sink):被控制端,通过分布式屏幕接收主控端的控制,在本地对接窗口用于接收和显示主控端设备的屏幕内容。 + +### **接口说明** +分布式屏幕实现分布式硬件管理框架提供的接口,由分布式硬件管理框架统一调用接口实现虚拟屏幕硬件的创建和注册功能。 + +### **流程说明** +#### **1. 设备开机启动** +系统拉起分布式屏幕的SA服务,Source侧被初始化,相关模块被初始化。 + +#### **2. 设备组网上线** +设备上线后,分布式硬件管理框架同步到上线设备的屏幕硬件信息并使能,使能成功后在系统中会新增虚拟屏幕并通知到窗口子系统,窗口子系统统一管理本地屏幕和分布式屏幕;北向应用通过窗口子系统提供的接口可以查询到分布式屏幕,并按照窗口子系统提供的接口来使用分布式屏幕。 + +#### **3. 屏幕数据流转** +(1)主控端图形子系统将需要发送屏幕数据保存在编码器创建的输入Surface中。 +(2)主控端编码器将输入数据进行编码,并将编码结果返回传输组件screensourcetrans。 +(3)主控端传输组件screensourcetrans将编码后的数据通过传输通道screendatachannel发送到softbusadapter,并经由软总线子系统发送到被控端端设备。 +(4)被控端设备软总线子系统收到屏幕数据后,通过softbusadapter返回给被控端传输通道screendatachannel。 +(5)被控端传输通道screendatachannel将获取到的屏幕数据传递给解码器进行解码。 +(6)解码器将屏幕数据解码,并将解码后的数据保存到被控端代理显示窗口设置到解码器的Surface中,最终由窗口将画面显示在屏幕上。 + +#### **4. 设备下线** +设备下线后,分布式硬件管理框架去使能下线设备的屏幕硬件,本地移除对应的虚拟屏幕并通知窗口子系统,此时下线设备的分布式屏幕不可用。 + +## **涉及仓** +**** +**分布式屏幕** +[distributed_screen](https://gitee.com/openharmony/distributed_screen) \ No newline at end of file diff --git a/bundle.json b/bundle.json new file mode 100644 index 0000000000000000000000000000000000000000..c89001c4d7aafd1795574fcb81772b4c54460a96 --- /dev/null +++ b/bundle.json @@ -0,0 +1,87 @@ +{ + "name":"@ohos/distributed_screen", + "description":"distributed hardware screen", + "version":"3.1", + "author":{}, + "repository":"https://gitee.com/openharmony/distributed_screen", + "license":"Apache License 2.0", + "publishAs":"code-segment", + "segment":{ + "destPath":"foundation/distributedhardware/distributedscreen/" + }, + "dirs":{}, + "scripts":{}, + "component":{ + "name":"distributed_screen", + "subsystem":"distributedhardware", + "syscap":[ + "SystemCapability.distributedhardware.distributed_screen" + ], + "features":[], + "adapted_system_type":[ + "standard" + ], + "rom":"2000k", + "ram":"6M", + "deps":{ + "components":[ + "appexecfwk_standard", + "eventhandler", + "hiviewdfx_hilog_native", + "ipc", + "safwk", + "samgr_standard", + "dsoftbus_standard", + "utils_base", + "graphic_standard", + "distributed_hardware_fwk", + "multimedia_media_standard" + ], + "third_party":[ + "json" + ] + }, + "build":{ + "sub_component":[ + "//foundation/distributedhardware/distributedscreen/common:distributed_screen_utils", + "//foundation/distributedhardware/distributedscreen/interfaces/innerkits/native_cpp/screen_sink:distributed_screen_sink_sdk", + "//foundation/distributedhardware/distributedscreen/interfaces/innerkits/native_cpp/screen_source:distributed_screen_source_sdk", + "//foundation/distributedhardware/distributedscreen/services/screenclient:distributed_screen_client", + "//foundation/distributedhardware/distributedscreen/screenhandler:distributed_screen_handler", + "//foundation/distributedhardware/distributedscreen/services/screentransport/screensinktrans:distributed_screen_sinktrans", + "//foundation/distributedhardware/distributedscreen/services/screentransport/screensourcetrans:distributed_screen_sourcetrans", + "//foundation/distributedhardware/distributedscreen/services/screenservice/sinkservice:distributed_screen_sink", + "//foundation/distributedhardware/distributedscreen/services/screenservice/sourceservice:distributed_screen_source", + "//foundation/distributedhardware/distributedscreen/sa_profile:dscreen_sa_profile" + ], + "inner_kits":[ + { + "type": "so", + "name": "//foundation/distributedhardware/distributedscreen/interfaces/innerkits/native_cpp/screen_sink:distributed_screen_sink_sdk", + "header": { + "header_base": "//foundation/distributedhardware/distributedscreen/interfaces/innerkits/native_cpp/screen_sink/include", + "header_files": [ + "idscreen_sink.h" + ] + } + }, + { + "type": "so", + "name": "//foundation/distributedhardware/distributedscreen/interfaces/innerkits/native_cpp/screen_source:distributed_screen_source_sdk", + "header": { + "header_base": "//foundation/distributedhardware/distributedscreen/interfaces/innerkits/native_cpp/screen_source/include", + "header_files": [ + "idscreen_source.h" + ] + } + } + ], + "test":[ + "//foundation/distributedhardware/distributedscreen/services/screentransport/test/unittest:screen_transport_test", + "//foundation/distributedhardware/distributedscreen/services/softbusadapter/test/unittest:SoftBusAdapterTest", + "//foundation/distributedhardware/distributedscreen/services/screenclient/test/unittest:screen_client_test", + "//foundation/distributedhardware/distributedscreen/services/common/test/unittest:service_common_test" + ] + } + } +} \ No newline at end of file diff --git a/common/BUILD.gn b/common/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..7dd03dcd631fe907e5d57d8a7e6079f71e38c6f3 --- /dev/null +++ b/common/BUILD.gn @@ -0,0 +1,47 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +ohos_shared_library("distributed_screen_utils") { + include_dirs = [ + "//utils/native/base/include", + "include", + ] + + sources = [ + "src/dscreen_util.cpp", + "src/dscreen_log.cpp", + ] + + deps = [ + "//utils/native/base:utils", + ] + + external_deps = [ + "dsoftbus_standard:softbus_client", + "hiviewdfx_hilog_native:libhilog", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dscreenutil\"", + "LOG_DOMAIN=0xD004100", + ] + + subsystem_name = "distributedhardware" + + part_name = "distributed_screen" +} \ No newline at end of file diff --git a/common/include/dscreen_constants.h b/common/include/dscreen_constants.h new file mode 100644 index 0000000000000000000000000000000000000000..50c0b15d22d6d8d7a18a24a88f73b6bba3dfad1f --- /dev/null +++ b/common/include/dscreen_constants.h @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_CONSTANTS_H +#define OHOS_DSCREEN_CONSTANTS_H + +#include +#include +#include + +namespace OHOS { +namespace DistributedHardware { +enum RpcEventType { + NOTIFY_SINK_SETUP = 1, + NOTIFY_SOURCE_SETUP_RESULT = 2, +}; + +enum DScreenState { + DISABLED, + ENABLED, + DISABLING, + ENABLING, + CONNECTING, + CONNECTED, + DISCONNECTING, +}; + +enum TaskType { + TASK_ENABLE, + TASK_DISABLE, + TASK_CONNECT, + TASK_DISCONNECT, +}; + +enum CodecType : uint8_t { + VIDEO_CODEC_TYPE_VIDEO_H264 = 0, + VIDEO_CODEC_TYPE_VIDEO_H265 = 1, +}; + +enum VideoFormat : uint8_t { + VIDEO_DATA_FORMAT_YUVI420 = 0, + VIDEO_DATA_FORMAT_NV12 = 1, + VIDEO_DATA_FORMAT_NV21 = 2, +}; + +/* Screen package name */ +const std::string PKG_NAME = "DBinderBus_" + std::to_string(getpid()); + +/* Screen data session name */ +const std::string DATA_SESSION_NAME = "DBinder.ohos.dhardware.dscreen.data"; + +/* Screen session name max len */ +constexpr uint32_t DSCREEN_MAX_SESSION_NAME_LEN = 50; + +constexpr uint32_t DSCREEN_MAX_DEVICE_ID_LEN = 100; +/* Screen data received max length */ +constexpr uint32_t DSCREEN_MAX_RECV_DATA_LEN = 104857600; + +/* Screen max video data width */ +constexpr uint32_t DSCREEN_MAX_VIDEO_DATA_WIDTH = 2560; + +/* Screen max video data height */ +constexpr uint32_t DSCREEN_MAX_VIDEO_DATA_HEIGHT = 1600; + +/* Screen max screen data width */ +constexpr uint32_t DSCREEN_MAX_SCREEN_DATA_WIDTH = 2560; + +/* Screen max screen data height */ +constexpr uint32_t DSCREEN_MAX_SCREEN_DATA_HEIGHT = 1600; + +/* YUV420 buffer size max size */ +constexpr int64_t MAX_YUV420_BUFFER_SIZE = 2560 * 1600 * (3 / 2) * 2; +constexpr int32_t LOG_MAX_LEN = 4096; + +constexpr int32_t INVALID_WINDOW_ID = -1; + +const std::string DSCREEN_LOG_TITLE_TAG = "DSCREEN"; +const std::string DSCREEN_PREFIX = "DISTRIBUTED_SCREEN"; +const std::string SCREEN_PREFIX = "SCREEN"; +const std::string DSCREEN_VERSION = "1.0"; +const std::string SEPERATOR = "#"; +const std::string KEY_VERSION = "screenVersion"; +const std::string KEY_DISPLAY_ID = "displayId"; +const std::string KEY_SCREEN_ID = "screenId"; +const std::string KEY_DISPLAY_RECT = "displayRect"; +const std::string KEY_SCREEN_RECT = "screenRect"; +const std::string KEY_POINT_START_X = "startX"; +const std::string KEY_POINT_START_Y = "startY"; +const std::string KEY_WIDTH = "width"; +const std::string KEY_HEIGHT = "height"; +const std::string KEY_SCREEN_WIDTH = "screenWidth"; +const std::string KEY_SCREEN_HEIGHT = "screenHeight"; +const std::string KEY_VIDEO_WIDTH = "videoWidth"; +const std::string KEY_VIDEO_HEIGHT = "videoHeight"; +const std::string KEY_FPS = "fps"; +const std::string KEY_CODECTYPE = "codecType"; +const std::string SCREEN_CLIENT_WINDOW = "screenClientWindow"; +const std::string KEY_DH_ID = "dhId"; +const std::string KEY_ERR_CODE = "errCode"; +const std::string KEY_ERR_CONTENT = "errContent"; +const std::string KEY_VIDEO_PARAM = "videoParam"; +const std::string KEY_MAPRELATION = "mapRelation"; +constexpr float DEFAULT_DENSITY = 2.0; +constexpr int32_t DEFAULT_SCREEN_FLAGS = 0; +constexpr uint32_t DEFAULT_FPS = 30; +constexpr uint8_t DEFAULT_CODECTYPE = VIDEO_CODEC_TYPE_VIDEO_H264; +constexpr uint8_t DEFAULT_VIDEO_FORMAT = VIDEO_DATA_FORMAT_NV21; +constexpr int32_t DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID = 4807; +constexpr int32_t DISTRIBUTED_HARDWARE_SCREEN_SINK_SA_ID = 4808; +constexpr uint64_t SCREEN_ID_INVALID = -1ULL; +constexpr uint64_t SCREEN_ID_DEFAULT = 0; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/common/include/dscreen_errcode.h b/common/include/dscreen_errcode.h new file mode 100644 index 0000000000000000000000000000000000000000..7315a32b9a438f6f77501730ba475d42b368945a --- /dev/null +++ b/common/include/dscreen_errcode.h @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_ERRCODE_H +#define OHOS_DSCREEN_ERRCODE_H + +namespace OHOS { +namespace DistributedHardware { +enum DScreenErrorCode { + DH_SUCCESS = 0, + // SA error code + ERR_DH_SCREEN_SA_GET_SAMGR_FAIL = -50000, + ERR_DH_SCREEN_SA_GET_SOURCESERVICE_FAIL = -50001, + ERR_DH_SCREEN_SA_GET_SOURCEPROXY_FAIL = -50002, + ERR_DH_SCREEN_SA_GET_SOURCECALLBACKPROXY_FAIL = -50003, + ERR_DH_SCREEN_SA_SOURCEPROXY_NOT_INIT = -50004, + ERR_DH_SCREEN_SA_SOURCEPCALLBACK_NOT_INIT = -50005, + ERR_DH_SCREEN_SA_GET_SINKSERVICE_FAIL = -50006, + ERR_DH_SCREEN_SA_GET_SINKPROXY_FAIL = -50007, + ERR_DH_SCREEN_SA_SINKPROXY_NOT_INIT = -50008, + ERR_DH_SCREEN_SA_REGISTERCALLBACK_NOT_FOUND = -50009, + ERR_DH_SCREEN_SA_UNREGISTERCALLBACK_NOT_FOUND = -50010, + ERR_DH_SCREEN_SA_REQUEST_CODE_INVALID = -50011, + ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED = -50012, + ERR_DH_SCREEN_SA_WRITEPARAM_FAILED = -50013, + ERR_DH_SCREEN_SA_READPARAM_FAILED = -50014, + ERR_DH_SCREEN_SA_DSCREENMGR_NOT_INIT = -50015, + ERR_DH_SCREEN_SA_ENABLE_FAILED = -50016, + ERR_DH_SCREEN_SA_DISABLE_FAILED = -50017, + ERR_DH_SCREEN_SA_SOURCETRANS_NOT_INIT = -50018, + ERR_DH_SCREEN_SA_SCREENREGIONMGR_NOT_INIT = -50019, + ERR_DH_SCREEN_SA_SINKTRANS_NOT_INIT = -50020, + ERR_DH_SCREEN_SA_GET_REMOTE_SOURCE_SERVICE_FAIL = -50021, + ERR_DH_SCREEN_SA_GET_REMOTE_SINK_SERVICE_FAIL = -50022, + ERR_DH_SCREEN_SA_SCREENREGION_SETUP_FAIL = -50023, + ERR_DH_SCREEN_SA_SCREENREGION_START_FAIL = -50024, + ERR_DH_SCREEN_SA_REMOVE_VIRTUALSCREEN_FAIL = -50025, + ERR_DH_SCREEN_SA_DSCREEN_TASK_NOT_VALID = -50026, + ERR_DH_SCREEN_SA_DSCREEN_SCREENGION_SETUP_FAILED = -50027, + ERR_DH_SCREEN_SA_INVALID_IPC_CALL = -50028, + ERR_DH_SCREEN_SA_REGISTER_SCREENLISTENER_FAIL = -520029, + ERR_DH_SCREEN_SA_UNREGISTER_SCREENLISTENER_FAIL = -520030, + // Transport component error code + ERR_DH_SCREEN_TRANS_ERROR = -51000, + ERR_DH_SCREEN_TRANS_TIMEOUT = -51001, + ERR_DH_SCREEN_TRANS_NULL_VALUE = -51002, + ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM = -51003, + ERR_DH_SCREEN_TRANS_ILLEGAL_OPERATION = -51004, + ERR_DH_SCREEN_TRANS_SESSION_CLOSED = -51005, + ERR_DH_SCREEN_TRANS_CREATE_CODEC_FAILED = -51006, + ERR_DH_SCREEN_TRANS_RELEASE_CODEC_FAILED = -51007, + ERR_DH_SCREEN_TRANS_START_CODEC_FAILED = -51008, + ERR_DH_SCREEN_TRANS_STOP_CODEC_FAILED = -51009, + ERR_DH_SCREEN_TRANS_CREATE_SURFACE_FAILED = -51010, + ERR_DH_SCREEN_TRANS_SESSION_NOT_OPEN = -51011, + // adapter + ERR_DH_SCREEN_ADAPTER_SESSION_ID_NOT_FIND = -52000, + ERR_DH_SCREEN_ADAPTER_UNREGISTER_SOFTBUS_LISTENER_FAIL = -52001, + ERR_DH_SCREEN_ADAPTER_REGISTER_SOFTBUS_LISTENER_FAIL = -52002, + ERR_DH_SCREEN_ADAPTER_FIND_SOFTBUS_LISTENER_FAIL = -52003, + ERR_DH_SCREEN_ADAPTER_BAD_VALUE = -52004, + ERR_DH_SCREEN_ADAPTER_OPEN_SESSION_FAIL = -52005, + ERR_DH_SCREEN_ADAPTER_PARA_ERROR = -52006, + // Encoder & Decoder + ERR_DH_SCREEN_CODEC_RELEASE_FAILED = -53000, + ERR_DH_SCREEN_CODEC_PREPARE_FAILED = -53001, + ERR_DH_SCREEN_CODEC_START_FAILED = -53002, + ERR_DH_SCREEN_CODEC_FLUSH_FAILED = -53003, + ERR_DH_SCREEN_CODEC_STOP_FAILED = -53004, + ERR_DH_SCREEN_CODEC_SET_CALLBACK_FAILED = -53005, + ERR_DH_SCREEN_CODEC_CONFIGURE_FAILED = -53006, + ERR_DH_SCREEN_CODEC_SURFACE_ERROR = -53007, + // ScreenClient error code + ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR = -54000, + ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR = -54001, + ERR_DH_SCREEN_SCREENCLIENT_ADD_WINDOW_ERROR = -54002, + ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR = -54003, + ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR = -54004, +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/common/include/dscreen_log.h b/common/include/dscreen_log.h new file mode 100644 index 0000000000000000000000000000000000000000..b8c92965be134d1bdc5f9d3752b791d171344bcb --- /dev/null +++ b/common/include/dscreen_log.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_LOG_H +#define OHOS_DSCREEN_LOG_H + +namespace OHOS { +namespace DistributedHardware { +typedef enum { + DH_LOG_DEBUG, + DH_LOG_INFO, + DH_LOG_WARN, + DH_LOG_ERROR, +} DHLogLevel; + +void DHLog(DHLogLevel logLevel, const char *fmt, ...); + +#define DHLOGD(fmt, ...) DHLog(DH_LOG_DEBUG, \ + (std::string("[") + DH_LOG_TAG + "][" + __FUNCTION__ + "]:" + fmt).c_str(), ##__VA_ARGS__) + +#define DHLOGI(fmt, ...) DHLog(DH_LOG_INFO, \ + (std::string("[") + DH_LOG_TAG + "][" + __FUNCTION__ + "]:" + fmt).c_str(), ##__VA_ARGS__) + +#define DHLOGW(fmt, ...) DHLog(DH_LOG_WARN, \ + (std::string("[") + DH_LOG_TAG + "][" + __FUNCTION__ + "]:" + fmt).c_str(), ##__VA_ARGS__) + +#define DHLOGE(fmt, ...) DHLog(DH_LOG_ERROR, \ + (std::string("[") + DH_LOG_TAG + "][" + __FUNCTION__ + "]:" + fmt).c_str(), ##__VA_ARGS__) +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/common/include/dscreen_util.h b/common/include/dscreen_util.h new file mode 100644 index 0000000000000000000000000000000000000000..8b113e525815b1c1cb1dbb4e8f9144fad7d3fa29 --- /dev/null +++ b/common/include/dscreen_util.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_UTIL_H +#define OHOS_DSCREEN_UTIL_H + +#include + +namespace OHOS { +namespace DistributedHardware { +int32_t GetLocalDeviceNetworkId(std::string &networkId); +std::string GetRandomID(); +std::string GetAnonyString(const std::string &value); +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/common/src/dscreen_log.cpp b/common/src/dscreen_log.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec34b603e0021e976697619e77a3c05747ca98d3 --- /dev/null +++ b/common/src/dscreen_log.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_log.h" + +#include "securec.h" + +#ifdef HI_LOG_ENABLE +#include "hilog/log.h" +#else +#include +#endif + +#include "dscreen_constants.h" + +namespace OHOS { +namespace DistributedHardware { +static void DHLogOut(DHLogLevel logLevel, const char *logBuf) +{ +#ifdef HI_LOG_ENABLE + LogLevel hiLogLevel = LOG_INFO; + switch (logLevel) { + case DH_LOG_DEBUG: + hiLogLevel = LOG_DEBUG; + break; + case DH_LOG_INFO: + hiLogLevel = LOG_INFO; + break; + case DH_LOG_WARN: + hiLogLevel = LOG_WARN; + break; + case DH_LOG_ERROR: + hiLogLevel = LOG_ERROR; + break; + default: + break; + } + (void)HiLogPrint(LOG_CORE, hiLogLevel, LOG_DOMAIN, DSCREEN_LOG_TITLE_TAG.c_str(), "%{public}s", logBuf); +#else + switch (logLevel) { + case DH_LOG_DEBUG: + printf("[D]%s\n", logBuf); + break; + case DH_LOG_INFO: + printf("[I]%s\n", logBuf); + break; + case DH_LOG_WARN: + printf("[W]%s\n", logBuf); + break; + case DH_LOG_ERROR: + printf("[E]%s\n", logBuf); + break; + default: + break; + } +#endif +} + +void DHLog(DHLogLevel logLevel, const char *fmt, ...) +{ + char logBuf[LOG_MAX_LEN] = {0}; + va_list arg; + + (void)memset_s(&arg, sizeof(va_list), 0, sizeof(va_list)); + va_start(arg, fmt); + int32_t ret = vsprintf_s(logBuf, sizeof(logBuf), fmt, arg); + va_end(arg); + if (ret < 0) { + DHLogOut(logLevel, "DH log length error."); + return; + } + DHLogOut(logLevel, logBuf); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/common/src/dscreen_util.cpp b/common/src/dscreen_util.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95ba2b78aa8791f95842c74acf0ecb579c3e10f9 --- /dev/null +++ b/common/src/dscreen_util.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_util.h" + +#include +#include +#include +#include + +#include "softbus_bus_center.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +constexpr int32_t WORD_WIDTH_8 = 8; +constexpr int32_t WORD_WIDTH_4 = 4; + +int32_t GetLocalDeviceNetworkId(std::string &networkId) +{ + NodeBasicInfo basicInfo = { { 0 } }; + int32_t ret = GetLocalNodeDeviceInfo(PKG_NAME.c_str(), &basicInfo); + if (ret != DH_SUCCESS) { + DHLOGE("GetLocalDeviceNetworkId failed ret: %d", ret); + return ret; + } + + networkId = std::string(basicInfo.networkId); + return DH_SUCCESS; +} + +std::string GetRandomID() +{ + static std::random_device rd; + static std::uniform_int_distribution dist(0ULL, 0xFFFFFFFFFFFFFFFFULL); + uint64_t ab = dist(rd); + uint64_t cd = dist(rd); + uint32_t a, b, c, d; + std::stringstream ss; + ab = (ab & 0xFFFFFFFFFFFF0FFFULL) | 0x0000000000004000ULL; + cd = (cd & 0x3FFFFFFFFFFFFFFFULL) | 0x8000000000000000ULL; + a = (ab >> 32U); + b = (ab & 0xFFFFFFFFU); + c = (cd >> 32U); + d = (cd & 0xFFFFFFFFU); + ss << std::hex << std::nouppercase << std::setfill('0'); + ss << std::setw(WORD_WIDTH_8) << (a); + ss << std::setw(WORD_WIDTH_4) << (b >> 16U); + ss << std::setw(WORD_WIDTH_4) << (b & 0xFFFFU); + ss << std::setw(WORD_WIDTH_4) << (c >> 16U); + ss << std::setw(WORD_WIDTH_4) << (c & 0xFFFFU); + ss << std::setw(WORD_WIDTH_8) << d; + + return ss.str(); +} + +std::string GetAnonyString(const std::string &value) +{ + constexpr size_t INT32_SHORT_ID_LENGTH = 20; + constexpr size_t INT32_MIN_ID_LENGTH = 3; + std::string res; + std::string tmpStr("******"); + size_t strLen = value.length(); + if (strLen < INT32_MIN_ID_LENGTH) { + return tmpStr; + } + + if (strLen <= INT32_SHORT_ID_LENGTH) { + res += value[0]; + res += tmpStr; + res += value[strLen - 1]; + } else { + constexpr size_t INT32_PLAINTEXT_LENGTH = 4; + res.append(value, 0, INT32_PLAINTEXT_LENGTH); + res += tmpStr; + res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH); + } + + return res; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/distributedscreen.gni b/distributedscreen.gni new file mode 100644 index 0000000000000000000000000000000000000000..217b56cfbb6cdddad7a933b385d47905c940b325 --- /dev/null +++ b/distributedscreen.gni @@ -0,0 +1,25 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +distributedscreen_path = "//foundation/distributedhardware/distributedscreen" +distributedhardwarefwk_path = "//foundation/distributedhardware/distributedhardwarefwk" +windowmanager_path = "//foundation/windowmanager" +mediastandard_path = "//foundation/multimedia/media_standard" +graphicstandard_path = "//foundation/graphic/standard" +common_path = "${distributedscreen_path}/common" +services_path = "${distributedscreen_path}/services" +interfaces_path = "${distributedscreen_path}/interfaces" +fwk_utils_path = "${distributedhardwarefwk_path}/utils" +fwk_common_path = "${distributedhardwarefwk_path}/common" +fwk_services_path = "${distributedhardwarefwk_path}/services" +build_flags = ["-Werror"] \ No newline at end of file diff --git a/figures/distributedscreen_arch.png b/figures/distributedscreen_arch.png new file mode 100644 index 0000000000000000000000000000000000000000..e4c0deb8bb27e9718ef77cd0a9e4736e5d53ff66 Binary files /dev/null and b/figures/distributedscreen_arch.png differ diff --git a/interfaces/innerkits/native_cpp/screen_sink/BUILD.gn b/interfaces/innerkits/native_cpp/screen_sink/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..bcbb3e9b0c1f0757fcabceb1fd95bbc8cf1d1317 --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_sink/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +ohos_shared_library("distributed_screen_sink_sdk") { + include_dirs = [ + "//utils/system/safwk/native/include", + "${fwk_common_path}/utils/include", + ] + + include_dirs += [ + "include", + "${common_path}/include", + ] + + sources = [ + "src/dscreen_sink_handler.cpp", + "src/dscreen_sink_proxy.cpp", + ] + + deps = [ + "//utils/native/base:utils", + "${common_path}:distributed_screen_utils", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dscreensinksdk\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] + + subsystem_name = "distributedhardware" + + part_name = "distributed_screen" +} \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/screen_sink/include/dscreen_sink_handler.h b/interfaces/innerkits/native_cpp/screen_sink/include/dscreen_sink_handler.h new file mode 100644 index 0000000000000000000000000000000000000000..152884ded32f1622fff676707a4840e759c8c4a1 --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_sink/include/dscreen_sink_handler.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_SINK_HANDLER_H +#define OHOS_DSCREEN_SINK_HANDLER_H + +#include + +#include "idistributed_hardware_sink.h" +#include "single_instance.h" + +#include "idscreen_sink.h" + +namespace OHOS { +namespace DistributedHardware { +class DScreenSinkHandler : public IDistributedHardwareSink { +DECLARE_SINGLE_INSTANCE_BASE(DScreenSinkHandler); +public: + int32_t InitSink(const std::string ¶ms) override; + int32_t ReleaseSink() override; + int32_t SubscribeLocalHardware(const std::string &dhId, const std::string ¶m) override; + int32_t UnsubscribeLocalHardware(const std::string &dhId) override; + void OnRemoteSinkSvrDied(const wptr &remote); +private: + class DScreenSinkSvrRecipient : public IRemoteObject::DeathRecipient { + public: + void OnRemoteDied(const wptr &remote) override; + }; + + DScreenSinkHandler(); + ~DScreenSinkHandler(); + + std::mutex mutex_; + sptr dScreenSinkProxy_ = nullptr; + sptr sinkSvrRecipient_ = nullptr; +}; + +#ifdef __cplusplus +extern "C" { +#endif +__attribute__((visibility("default"))) IDistributedHardwareSink *GetSinkHardwareHandler(); +#ifdef __cplusplus +} +#endif +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/screen_sink/include/dscreen_sink_proxy.h b/interfaces/innerkits/native_cpp/screen_sink/include/dscreen_sink_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..ca69db104f5401a98b17343bafc3fdd5dde5206e --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_sink/include/dscreen_sink_proxy.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_SINK_PROXY_H +#define OHOS_DSCREEN_SINK_PROXY_H + +#include "iremote_broker.h" +#include "iremote_proxy.h" + +#include "idscreen_sink.h" + +namespace OHOS { +namespace DistributedHardware { +class DScreenSinkProxy : public IRemoteProxy { +public: + explicit DScreenSinkProxy(const sptr impl) + : IRemoteProxy(impl) + { + } + + ~DScreenSinkProxy() {} + int32_t InitSink(const std::string ¶ms) override; + int32_t ReleaseSink() override; + int32_t SubscribeLocalHardware(const std::string &dhId, const std::string ¶m) override; + int32_t UnsubscribeLocalHardware(const std::string &dhId) override; + void DScreenNotify(const std::string &devId, int32_t eventCode, const std::string &eventContent) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/interfaces/innerkits/native_cpp/screen_sink/include/idscreen_sink.h b/interfaces/innerkits/native_cpp/screen_sink/include/idscreen_sink.h new file mode 100644 index 0000000000000000000000000000000000000000..96be07e54d07b645956f56109305996d79c7243c --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_sink/include/idscreen_sink.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_IDSCREEN_SINK_H +#define OHOS_IDSCREEN_SINK_H + +#include "iremote_broker.h" + +namespace OHOS { +namespace DistributedHardware { +class IDScreenSink : public OHOS::IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.distributedhardware.distributedscreensink"); + enum { + INIT_SINK = 0, + RELEASE_SINK = 1, + SUBSCRIBE_DISTRIBUTED_HARDWARE = 2, + UNSUBSCRIBE_DISTRIBUTED_HARDWARE = 3, + DSCREEN_NOTIFY = 4, + }; + + IDScreenSink() = default; + virtual ~IDScreenSink() = default; + virtual int32_t InitSink(const std::string ¶ms) = 0; + virtual int32_t ReleaseSink() = 0; + virtual int32_t SubscribeLocalHardware(const std::string &dhId, const std::string ¶m) = 0; + virtual int32_t UnsubscribeLocalHardware(const std::string &dhId) = 0; + virtual void DScreenNotify(const std::string &devId, int32_t eventCode, const std::string &eventContent) = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/screen_sink/src/dscreen_sink_handler.cpp b/interfaces/innerkits/native_cpp/screen_sink/src/dscreen_sink_handler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f4472cc86dbe81d3017d4c9235957fd3d9e33f3 --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_sink/src/dscreen_sink_handler.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_sink_handler.h" + +#include "if_system_ability_manager.h" +#include "iservice_registry.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(DScreenSinkHandler); + +DScreenSinkHandler::DScreenSinkHandler() +{ + DHLOGI("DScreenSinkHandler construct."); + std::lock_guard lock(mutex_); + if (!sinkSvrRecipient_) { + sinkSvrRecipient_ = new DScreenSinkSvrRecipient(); + } +} + +DScreenSinkHandler::~DScreenSinkHandler() +{ + DHLOGI("~DScreenSinkHandler."); +} + +int32_t DScreenSinkHandler::InitSink(const std::string ¶ms) +{ + DHLOGD("InitSink"); + std::lock_guard lock(mutex_); + + if (!dScreenSinkProxy_) { + sptr samgr = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!samgr) { + DHLOGE("Failed to get system ability mgr."); + return ERR_DH_SCREEN_SA_GET_SAMGR_FAIL; + } + sptr remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_SCREEN_SINK_SA_ID); + if (!remoteObject) { + DHLOGE("Failed to get dscreen sink service."); + return ERR_DH_SCREEN_SA_GET_SINKSERVICE_FAIL; + } + + remoteObject->AddDeathRecipient(sinkSvrRecipient_); + dScreenSinkProxy_ = iface_cast(remoteObject); + if ((!dScreenSinkProxy_) || (!dScreenSinkProxy_->AsObject())) { + DHLOGE("Failed to get dscreen sink proxy."); + return ERR_DH_SCREEN_SA_GET_SINKPROXY_FAIL; + } + } + int32_t ret = dScreenSinkProxy_->InitSink(params); + return ret; +} + +int32_t DScreenSinkHandler::ReleaseSink() +{ + DHLOGD("ReleaseSink"); + std::lock_guard lock(mutex_); + if (!dScreenSinkProxy_) { + DHLOGE("screen sink proxy not init."); + return ERR_DH_SCREEN_SA_SINKPROXY_NOT_INIT; + } + + int32_t ret = dScreenSinkProxy_->ReleaseSink(); + return ret; +} + +int32_t DScreenSinkHandler::SubscribeLocalHardware(const std::string &dhId, const std::string ¶m) +{ + DHLOGD("SubscribeLocalHardware"); + std::lock_guard lock(mutex_); + if (!dScreenSinkProxy_) { + DHLOGE("screen sink proxy not init."); + return ERR_DH_SCREEN_SA_SINKPROXY_NOT_INIT; + } + int32_t ret = dScreenSinkProxy_->SubscribeLocalHardware(dhId, param); + return ret; +} + +int32_t DScreenSinkHandler::UnsubscribeLocalHardware(const std::string &dhId) +{ + DHLOGD("UnsubscribeLocalHardware"); + std::lock_guard lock(mutex_); + if (!dScreenSinkProxy_) { + DHLOGE("screen sink proxy not init."); + return ERR_DH_SCREEN_SA_SINKPROXY_NOT_INIT; + } + int32_t ret = dScreenSinkProxy_->UnsubscribeLocalHardware(dhId); + return ret; +} + +void DScreenSinkHandler::DScreenSinkSvrRecipient::OnRemoteDied(const wptr &remote) +{ + DHLOGI("DScreenSinkSvrRecipient OnRemoteDied."); + DScreenSinkHandler::GetInstance().OnRemoteSinkSvrDied(remote); +} + +void DScreenSinkHandler::OnRemoteSinkSvrDied(const wptr &remote) +{ + DHLOGI("OnRemoteSinkSvrDied"); + sptr remoteObject = remote.promote(); + if (!remoteObject) { + DHLOGE("OnRemoteDied remote promoted failed"); + return; + } + std::lock_guard lock(mutex_); + if (!dScreenSinkProxy_) { + dScreenSinkProxy_->AsObject()->RemoveDeathRecipient(sinkSvrRecipient_); + dScreenSinkProxy_ = nullptr; + } +} + +IDistributedHardwareSink *GetSinkHardwareHandler() +{ + DHLOGD("GetSinkHardwareHandler"); + return &DScreenSinkHandler::GetInstance(); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/screen_sink/src/dscreen_sink_proxy.cpp b/interfaces/innerkits/native_cpp/screen_sink/src/dscreen_sink_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6d1e999f2994237f36913bcb3ad2f603f7d659b --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_sink/src/dscreen_sink_proxy.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_sink_proxy.h" + +#include "parcel.h" + +#include "dscreen_errcode.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t DScreenSinkProxy::InitSink(const std::string ¶ms) +{ + DHLOGD("InitSink"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken failed"); + return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED; + } + + if (!data.WriteString(params)) { + DHLOGE("Write param failed."); + return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED; + } + + Remote()->SendRequest(INIT_SINK, data, reply, option); + int32_t ret = reply.ReadInt32(); + return ret; +} + +int32_t DScreenSinkProxy::ReleaseSink() +{ + DHLOGD("ReleaseSink"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken failed"); + return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED; + } + + Remote()->SendRequest(RELEASE_SINK, data, reply, option); + int32_t ret = reply.ReadInt32(); + return ret; +} + +int32_t DScreenSinkProxy::SubscribeLocalHardware(const std::string &dhId, const std::string ¶m) +{ + DHLOGD("SubscribeLocalHardware"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken failed"); + return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED; + } + + if (!data.WriteString(dhId) || !data.WriteString(param)) { + DHLOGE("Write param failed."); + return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED; + } + + Remote()->SendRequest(SUBSCRIBE_DISTRIBUTED_HARDWARE, data, reply, option); + int32_t ret = reply.ReadInt32(); + return ret; +} + +int32_t DScreenSinkProxy::UnsubscribeLocalHardware(const std::string &dhId) +{ + DHLOGD("UnsubscribeLocalHardware"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken failed"); + return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED; + } + + if (!data.WriteString(dhId)) { + DHLOGE("Write param failed."); + return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED; + } + + Remote()->SendRequest(UNSUBSCRIBE_DISTRIBUTED_HARDWARE, data, reply, option); + int32_t ret = reply.ReadInt32(); + return ret; +} + +void DScreenSinkProxy::DScreenNotify(const std::string &devId, int32_t eventCode, const std::string &eventContent) +{ + DHLOGD("DScreenNotify"); + MessageParcel data; + MessageParcel reply; + MessageOption option = { MessageOption::TF_ASYNC }; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken failed"); + return; + } + + if (!data.WriteString(devId) || !data.WriteInt32(eventCode) || !data.WriteString(eventContent)) { + DHLOGE("Write param failed."); + return; + } + + Remote()->SendRequest(DSCREEN_NOTIFY, data, reply, option); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/screen_source/BUILD.gn b/interfaces/innerkits/native_cpp/screen_source/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..fec91820c36aa63208f420b31e63f6815ff8b66d --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_source/BUILD.gn @@ -0,0 +1,58 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +ohos_shared_library("distributed_screen_source_sdk") { + include_dirs = [ + "//utils/system/safwk/native/include", + "${fwk_common_path}/utils/include", + ] + + include_dirs += [ + "include", + "include/callback", + "${common_path}/include", + ] + + sources = [ + "src/dscreen_source_handler.cpp", + "src/dscreen_source_proxy.cpp", + "src/callback/dscreen_source_callback_stub.cpp", + "src/callback/dscreen_source_callback.cpp", + ] + + deps = [ + "//utils/native/base:utils", + "${common_path}:distributed_screen_utils", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dscreensourcesdk\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] + + subsystem_name = "distributedhardware" + + part_name = "distributed_screen" +} \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/screen_source/include/callback/dscreen_source_callback.h b/interfaces/innerkits/native_cpp/screen_source/include/callback/dscreen_source_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..3eb89ddadf28ff3d24c1a77ce1a75658a1cf1d7c --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_source/include/callback/dscreen_source_callback.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_SOURCE_CALLBACK_H +#define OHOS_DSCREEN_SOURCE_CALLBACK_H + +#include + +#include "idistributed_hardware_source.h" + +#include "dscreen_source_callback_stub.h" + +namespace OHOS { +namespace DistributedHardware { +class DScreenSourceCallback : public DScreenSourceCallbackStub { +public: + DScreenSourceCallback() = default; + ~DScreenSourceCallback(); + + int32_t OnNotifyRegResult(const std::string &devId, const std::string &dhId, + const std::string &reqId, int32_t status, const std::string &data) override; + int32_t OnNotifyUnregResult(const std::string &devId, const std::string &dhId, + const std::string &reqId, int32_t status, const std::string &data) override; + + void PushRegRegisterCallback(const std::string &reqId, const std::shared_ptr &callback); + void PopRegRegisterCallback(const std::string &reqId); + void PushUnregisterCallback(const std::string &reqId, const std::shared_ptr &callback); + void PopUnregisterCallback(const std::string &reqId); + +private: + std::map> registerCallbackMap_; + std::map> unregisterCallbackMap_; +}; +} +} +#endif \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/screen_source/include/callback/dscreen_source_callback_stub.h b/interfaces/innerkits/native_cpp/screen_source/include/callback/dscreen_source_callback_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..f85c25e002ef9a03d03245c8a041236a36bd1d36 --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_source/include/callback/dscreen_source_callback_stub.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_SOURCE_CALLBACK_STUB_H +#define OHOS_DSCREEN_SOURCE_CALLBACK_STUB_H + +#include + +#include "iremote_stub.h" + +#include "idscreen_source_callback.h" + +namespace OHOS { +namespace DistributedHardware { +class DScreenSourceCallbackStub : public IRemoteStub { +public: + DScreenSourceCallbackStub(); + virtual ~DScreenSourceCallbackStub(); + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) override; +private: + using DScreenFunc = int32_t (DScreenSourceCallbackStub::*)(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + std::map memberFuncMap_; + + int32_t OnNotifyRegResultInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + int32_t OnNotifyUnregResultInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option); +}; +} +} +#endif \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/screen_source/include/callback/idscreen_source_callback.h b/interfaces/innerkits/native_cpp/screen_source/include/callback/idscreen_source_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..5f880c3757f991fcf38c6f6245cb18faa153ddb0 --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_source/include/callback/idscreen_source_callback.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_IDSCREEN_SOURCE_CALLBACK_H +#define OHOS_IDSCREEN_SOURCE_CALLBACK_H + +#include "iremote_broker.h" + +namespace OHOS { +namespace DistributedHardware { +class IDScreenSourceCallback : public OHOS::IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.distributedhardware.dscreensourcecallback"); + enum { + NOTIFY_REG_RESULT = 0, + NOTIFY_UNREG_RESULT, + }; + + virtual ~IDScreenSourceCallback() {} + virtual int32_t OnNotifyRegResult(const std::string &devId, const std::string &dhId, + const std::string &reqId, int32_t status, const std::string &data) = 0; + virtual int32_t OnNotifyUnregResult(const std::string &devId, const std::string &dhId, + const std::string &reqId, int32_t status, const std::string &data) = 0; +}; +} +} +#endif \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/screen_source/include/dscreen_source_handler.h b/interfaces/innerkits/native_cpp/screen_source/include/dscreen_source_handler.h new file mode 100644 index 0000000000000000000000000000000000000000..0ee40a01795f4aca6ec503cbeb4690d0d68b2c66 --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_source/include/dscreen_source_handler.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_SOURCE_HANDLER_H +#define OHOS_DSCREEN_SOURCE_HANDLER_H + +#include + +#include "dscreen_source_callback.h" +#include "idscreen_source.h" +#include "idistributed_hardware_source.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedHardware { +class DScreenSourceHandler : public IDistributedHardwareSource { +DECLARE_SINGLE_INSTANCE_BASE(DScreenSourceHandler); +public: + int32_t InitSource(const std::string ¶ms) override; + int32_t ReleaseSource() override; + int32_t RegisterDistributedHardware(const std::string &devId, const std::string &dhId, + const EnableParam ¶m, std::shared_ptr callback) override; + int32_t UnregisterDistributedHardware(const std::string &devId, const std::string &dhId, + std::shared_ptr callback) override; + int32_t ConfigDistributedHardware(const std::string &devId, const std::string &dhId, + const std::string &key, const std::string &value) override; + void OnRemoteSourceSvrDied(const wptr &remote); +private: + class DScreenSourceSvrRecipient : public IRemoteObject::DeathRecipient { + public: + void OnRemoteDied(const wptr &remote) override; + }; + + DScreenSourceHandler(); + ~DScreenSourceHandler(); + + std::mutex mutex_; + sptr dScreenSourceProxy_ = nullptr; + sptr dScreenSourceCallback_ = nullptr; + sptr sourceSvrRecipient_ = nullptr; +}; + +#ifdef __cplusplus +extern "C" { +#endif +__attribute__((visibility("default"))) IDistributedHardwareSource *GetSourceHardwareHandler(); +#ifdef __cplusplus +} +#endif +} +} +#endif \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/screen_source/include/dscreen_source_proxy.h b/interfaces/innerkits/native_cpp/screen_source/include/dscreen_source_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..a0d84ba01a37a21787626bfe37c7a61ae9fc8e08 --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_source/include/dscreen_source_proxy.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_SOURCE_PROXY_H +#define OHOS_DSCREEN_SOURCE_PROXY_H + +#include "iremote_broker.h" +#include "iremote_proxy.h" +#include "idscreen_source.h" +#include "idistributed_hardware_source.h" + +namespace OHOS { +namespace DistributedHardware { +class DScreenSourceProxy : public IRemoteProxy { +public: + explicit DScreenSourceProxy(const sptr impl) + : IRemoteProxy(impl) + { + } + + ~DScreenSourceProxy() {} + int32_t InitSource(const std::string ¶ms, const sptr &callback) override; + int32_t ReleaseSource() override; + int32_t RegisterDistributedHardware(const std::string &devId, const std::string &dhId, + const EnableParam ¶m, const std::string &reqId) override; + int32_t UnregisterDistributedHardware(const std::string &devId, const std::string &dhId, + const std::string &reqId) override; + int32_t ConfigDistributedHardware(const std::string &devId, const std::string &dhId, + const std::string &key, const std::string &value) override; + void DScreenNotify(const std::string &devId, int32_t eventCode, const std::string &eventContent) override; + +private: + static inline BrokerDelegator delegator_; +}; +} +} +#endif diff --git a/interfaces/innerkits/native_cpp/screen_source/include/idscreen_source.h b/interfaces/innerkits/native_cpp/screen_source/include/idscreen_source.h new file mode 100644 index 0000000000000000000000000000000000000000..6c1a69edf41eaf1c18b995e577798a9636c4162c --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_source/include/idscreen_source.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_IDSCREEN_SOURCE_H +#define OHOS_IDSCREEN_SOURCE_H + +#include "iremote_broker.h" +#include "idscreen_source_callback.h" +#include "idistributed_hardware_source.h" + +namespace OHOS { +namespace DistributedHardware { +class IDScreenSource : public OHOS::IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.distributedhardware.distributedscreensource"); + enum { + INIT_SOURCE = 0, + RELEASE_SOURCE, + REGISTER_DISTRIBUTED_HARDWARE, + UNREGISTER_DISTRIBUTED_HARDWARE, + CONFIG_DISTRIBUTED_HARDWARE, + DSCREEN_NOTIFY, + }; + + virtual ~IDScreenSource() {} + virtual int32_t InitSource(const std::string ¶ms, const sptr &callback) = 0; + virtual int32_t ReleaseSource() = 0; + virtual int32_t RegisterDistributedHardware(const std::string &devId, const std::string &dhId, + const EnableParam ¶m, const std::string &reqId) = 0; + virtual int32_t UnregisterDistributedHardware(const std::string &devId, const std::string &dhId, + const std::string &reqId) = 0; + virtual int32_t ConfigDistributedHardware(const std::string &devId, const std::string &dhId, + const std::string &key, const std::string &value) = 0; + virtual void DScreenNotify(const std::string &devId, int32_t eventCode, + const std::string &eventContent) = 0; +}; +} +} +#endif \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/screen_source/src/callback/dscreen_source_callback.cpp b/interfaces/innerkits/native_cpp/screen_source/src/callback/dscreen_source_callback.cpp new file mode 100644 index 0000000000000000000000000000000000000000..038f6c1990bcc7cd7b1a65827d3f40662c757802 --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_source/src/callback/dscreen_source_callback.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_source_callback.h" + +#include "dscreen_errcode.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +DScreenSourceCallback::~DScreenSourceCallback() {} + +int32_t DScreenSourceCallback::OnNotifyRegResult(const std::string &devId, const std::string &dhId, + const std::string &reqId, int32_t status, const std::string &data) +{ + DHLOGD("OnNotifyRegResult"); + auto iter = registerCallbackMap_.find(reqId); + if (iter != registerCallbackMap_.end()) { + iter->second->OnRegisterResult(devId, dhId, status, data); + registerCallbackMap_.erase(reqId); + return DH_SUCCESS; + } + + return ERR_DH_SCREEN_SA_REGISTERCALLBACK_NOT_FOUND; +} + +int32_t DScreenSourceCallback::OnNotifyUnregResult(const std::string &devId, const std::string &dhId, + const std::string &reqId, int32_t status, const std::string &data) +{ + DHLOGD("OnNotifyUnregResult"); + auto iter = unregisterCallbackMap_.find(reqId); + if (iter != unregisterCallbackMap_.end()) { + iter->second->OnUnregisterResult(devId, dhId, status, data); + unregisterCallbackMap_.erase(reqId); + return DH_SUCCESS; + } + + return ERR_DH_SCREEN_SA_UNREGISTERCALLBACK_NOT_FOUND; +} + +void DScreenSourceCallback::PushRegRegisterCallback(const std::string &reqId, + const std::shared_ptr &callback) +{ + DHLOGD("PushRegRegisterCallback"); + registerCallbackMap_.emplace(reqId, callback); +} + +void DScreenSourceCallback::PopRegRegisterCallback(const std::string &reqId) +{ + DHLOGD("PopRegRegisterCallback"); + registerCallbackMap_.erase(reqId); +} + +void DScreenSourceCallback::PushUnregisterCallback(const std::string &reqId, + const std::shared_ptr &callback) +{ + DHLOGD("PushUnregisterCallback"); + unregisterCallbackMap_.emplace(reqId, callback); +} + +void DScreenSourceCallback::PopUnregisterCallback(const std::string &reqId) +{ + DHLOGD("PopUnregisterCallback"); + unregisterCallbackMap_.erase(reqId); +} +} +} \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/screen_source/src/callback/dscreen_source_callback_stub.cpp b/interfaces/innerkits/native_cpp/screen_source/src/callback/dscreen_source_callback_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..08f9ce8c09529a817f41cf9ae864512c0332c40a --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_source/src/callback/dscreen_source_callback_stub.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_source_callback_stub.h" + +#include "dscreen_errcode.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +DScreenSourceCallbackStub::DScreenSourceCallbackStub() +{ + memberFuncMap_[NOTIFY_REG_RESULT] = &DScreenSourceCallbackStub::OnNotifyRegResultInner; + memberFuncMap_[NOTIFY_UNREG_RESULT] = &DScreenSourceCallbackStub::OnNotifyUnregResultInner; +} + +DScreenSourceCallbackStub::~DScreenSourceCallbackStub() +{ +} + +int32_t DScreenSourceCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) +{ + std::map::iterator iter = memberFuncMap_.find(code); + if (iter == memberFuncMap_.end()) { + DHLOGE("invalid request code."); + return ERR_DH_SCREEN_SA_REQUEST_CODE_INVALID; + } + DScreenFunc &func = iter->second; + return (this->*func)(data, reply, option); +} + +int32_t DScreenSourceCallbackStub::OnNotifyRegResultInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + std::string devId = data.ReadString(); + std::string dhId = data.ReadString(); + std::string reqId = data.ReadString(); + int32_t status = data.ReadInt32(); + std::string resultData = data.ReadString(); + int32_t ret = OnNotifyRegResult(devId, dhId, reqId, status, resultData); + return ret; +} + +int32_t DScreenSourceCallbackStub::OnNotifyUnregResultInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + std::string devId = data.ReadString(); + std::string dhId = data.ReadString(); + std::string reqId = data.ReadString(); + int32_t status = data.ReadInt32(); + std::string resultData = data.ReadString(); + int32_t ret = OnNotifyRegResult(devId, dhId, reqId, status, resultData); + return ret; +} +} +} \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/screen_source/src/dscreen_source_handler.cpp b/interfaces/innerkits/native_cpp/screen_source/src/dscreen_source_handler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed5e3c0f89c79b46a453b839d4241335ae20c355 --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_source/src/dscreen_source_handler.cpp @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_source_handler.h" + +#include "if_system_ability_manager.h" +#include "iservice_registry.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "dscreen_util.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(DScreenSourceHandler); + +DScreenSourceHandler::DScreenSourceHandler() +{ + DHLOGI("DScreenSourceHandler construct."); + std::lock_guard lock(mutex_); + if (!sourceSvrRecipient_) { + sourceSvrRecipient_ = new DScreenSourceSvrRecipient(); + } + + if (!dScreenSourceCallback_) { + dScreenSourceCallback_ = new DScreenSourceCallback(); + } +} + +DScreenSourceHandler::~DScreenSourceHandler() +{ + DHLOGI("~DScreenSourceHandler."); +} + +int32_t DScreenSourceHandler::InitSource(const std::string ¶ms) +{ + DHLOGD("InitSource"); + std::lock_guard lock(mutex_); + + if (!dScreenSourceProxy_) { + sptr samgr = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!samgr) { + DHLOGE("Failed to get system ability mgr."); + return ERR_DH_SCREEN_SA_GET_SAMGR_FAIL; + } + sptr remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID); + if (!remoteObject) { + DHLOGE("Failed to get dscreen source service."); + return ERR_DH_SCREEN_SA_GET_SOURCESERVICE_FAIL; + } + + remoteObject->AddDeathRecipient(sourceSvrRecipient_); + dScreenSourceProxy_ = iface_cast(remoteObject); + if ((!dScreenSourceProxy_) || (!dScreenSourceProxy_->AsObject())) { + DHLOGE("Failed to get dscreen source proxy."); + return ERR_DH_SCREEN_SA_GET_SOURCEPROXY_FAIL; + } + } + + int32_t ret = dScreenSourceProxy_->InitSource(params, dScreenSourceCallback_); + return ret; +} + +int32_t DScreenSourceHandler::ReleaseSource() +{ + DHLOGD("ReleaseSource"); + std::lock_guard lock(mutex_); + if (!dScreenSourceProxy_) { + DHLOGE("screen source proxy not init."); + return ERR_DH_SCREEN_SA_SOURCEPROXY_NOT_INIT; + } + int32_t ret = dScreenSourceProxy_->ReleaseSource(); + return ret; +} + +int32_t DScreenSourceHandler::RegisterDistributedHardware(const std::string &devId, + const std::string &dhId, const EnableParam ¶m, std::shared_ptr callback) +{ + DHLOGD("RegisterDistributedHardware, devId: %s, dhId: %s", GetAnonyString(devId).c_str(), + GetAnonyString(dhId).c_str()); + std::lock_guard lock(mutex_); + if (!dScreenSourceProxy_) { + DHLOGE("screen source proxy not init."); + return ERR_DH_SCREEN_SA_SOURCEPROXY_NOT_INIT; + } + if (!dScreenSourceCallback_) { + DHLOGE("screen source callback is null."); + return ERR_DH_SCREEN_SA_SOURCEPCALLBACK_NOT_INIT; + } + + std::string reqId = GetRandomID(); + dScreenSourceCallback_->PushRegRegisterCallback(reqId, callback); + int ret = dScreenSourceProxy_->RegisterDistributedHardware(devId, dhId, param, reqId); + return ret; +} + +int32_t DScreenSourceHandler::UnregisterDistributedHardware(const std::string &devId, + const std::string &dhId, std::shared_ptr callback) +{ + DHLOGD("UnregisterDistributedHardware, devId: %s, dhId: %s", GetAnonyString(devId).c_str(), + GetAnonyString(dhId).c_str()); + std::lock_guard lock(mutex_); + if (!dScreenSourceProxy_) { + DHLOGE("screen source proxy not init."); + return ERR_DH_SCREEN_SA_SOURCEPROXY_NOT_INIT; + } + if (!dScreenSourceCallback_) { + DHLOGE("screen source callback is null."); + return ERR_DH_SCREEN_SA_SOURCEPCALLBACK_NOT_INIT; + } + + std::string reqId = GetRandomID(); + dScreenSourceCallback_->PushUnregisterCallback(reqId, callback); + int ret = dScreenSourceProxy_->UnregisterDistributedHardware(devId, dhId, reqId); + return ret; +} + +int32_t DScreenSourceHandler::ConfigDistributedHardware(const std::string &devId, + const std::string &dhId, const std::string &key, const std::string &value) +{ + DHLOGD("ConfigDistributedHardware"); + return DH_SUCCESS; +} + +void DScreenSourceHandler::DScreenSourceSvrRecipient::OnRemoteDied(const wptr &remote) +{ + DHLOGI("DScreenSourceSvrRecipient OnRemoteDied"); + DScreenSourceHandler::GetInstance().OnRemoteSourceSvrDied(remote); +} + +void DScreenSourceHandler::OnRemoteSourceSvrDied(const wptr &remote) +{ + DHLOGI("OnRemoteSourceSvrDied"); + sptr remoteObject = remote.promote(); + if (!remoteObject) { + DHLOGE("OnRemoteDied remote promoted failed"); + return; + } + std::lock_guard lock(mutex_); + if (!dScreenSourceProxy_) { + dScreenSourceProxy_->AsObject()->RemoveDeathRecipient(sourceSvrRecipient_); + dScreenSourceProxy_ = nullptr; + } +} + +IDistributedHardwareSource *GetSourceHardwareHandler() +{ + DHLOGD("GetSourceHardwareHandler"); + return &DScreenSourceHandler::GetInstance(); +} +} +} \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/screen_source/src/dscreen_source_proxy.cpp b/interfaces/innerkits/native_cpp/screen_source/src/dscreen_source_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f0bad83cfaabbd0ac7b1badbbde29a7b2436c4c --- /dev/null +++ b/interfaces/innerkits/native_cpp/screen_source/src/dscreen_source_proxy.cpp @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_source_proxy.h" + +#include "parcel.h" + +#include "dscreen_errcode.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t DScreenSourceProxy::InitSource(const std::string ¶ms, const sptr &callback) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken failed"); + return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED; + } + + if (!data.WriteString(params) + || !data.WriteRemoteObject(callback->AsObject())) { + DHLOGE("Write param failed."); + return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED; + } + + Remote()->SendRequest(INIT_SOURCE, data, reply, option); + int32_t ret = reply.ReadInt32(); + return ret; +} + +int32_t DScreenSourceProxy::ReleaseSource() +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken failed"); + return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED; + } + + Remote()->SendRequest(RELEASE_SOURCE, data, reply, option); + int32_t ret = reply.ReadInt32(); + return ret; +} + +int32_t DScreenSourceProxy::RegisterDistributedHardware(const std::string &devId, + const std::string &dhId, const EnableParam ¶m, const std::string &reqId) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken failed"); + return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED; + } + + if (!data.WriteString(devId) || !data.WriteString(dhId) + || !data.WriteString(param.version) || !data.WriteString(param.attrs) + || !data.WriteString(reqId)) { + DHLOGE("Write param failed."); + return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED; + } + Remote()->SendRequest(REGISTER_DISTRIBUTED_HARDWARE, data, reply, option); + int32_t ret = reply.ReadInt32(); + return ret; +} + +int32_t DScreenSourceProxy::UnregisterDistributedHardware(const std::string &devId, + const std::string &dhId, const std::string &reqId) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken failed"); + return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED; + } + + if (!data.WriteString(devId) || !data.WriteString(dhId) + || !data.WriteString(reqId)) { + DHLOGE("Write param failed."); + return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED; + } + Remote()->SendRequest(UNREGISTER_DISTRIBUTED_HARDWARE, data, reply, option); + int32_t ret = reply.ReadInt32(); + return ret; +} + +int32_t DScreenSourceProxy::ConfigDistributedHardware(const std::string &devId, + const std::string &dhId, const std::string &key, const std::string &value) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken failed"); + return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED; + } + + if (!data.WriteString(devId) || !data.WriteString(dhId) + || !data.WriteString(key) || !data.WriteString(value)) { + DHLOGE("Write param failed."); + return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED; + } + Remote()->SendRequest(CONFIG_DISTRIBUTED_HARDWARE, data, reply, option); + int32_t ret = reply.ReadInt32(); + return ret; +} + +void DScreenSourceProxy::DScreenNotify(const std::string &devId, + int32_t eventCode, const std::string &eventContent) +{ + DHLOGD("DScreenNotify"); + MessageParcel data; + MessageParcel reply; + MessageOption option = { MessageOption::TF_ASYNC }; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken failed"); + return; + } + + if (!data.WriteString(devId) || !data.WriteInt32(eventCode) || !data.WriteString(eventContent)) { + DHLOGE("Write param failed."); + return; + } + + Remote()->SendRequest(DSCREEN_NOTIFY, data, reply, option); +} +} +} diff --git a/sa_profile/4807.xml b/sa_profile/4807.xml new file mode 100644 index 0000000000000000000000000000000000000000..f3f552e2f137992fe8b494662d5a02ea6e3b40cd --- /dev/null +++ b/sa_profile/4807.xml @@ -0,0 +1,27 @@ + + + + dhardware + + 4807 + libdistributed_screen_source.z.so + + + true + true + 1 + + diff --git a/sa_profile/4808.xml b/sa_profile/4808.xml new file mode 100644 index 0000000000000000000000000000000000000000..6ec95fd48110875d465d97392409fb23b3d3a4d9 --- /dev/null +++ b/sa_profile/4808.xml @@ -0,0 +1,27 @@ + + + + dhardware + + 4808 + libdistributed_screen_sink.z.so + + + true + true + 1 + + diff --git a/sa_profile/BUILD.gn b/sa_profile/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..6b167fbbb2f4159d4b1883a946714a5ea5f51368 --- /dev/null +++ b/sa_profile/BUILD.gn @@ -0,0 +1,23 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos/sa_profile/sa_profile.gni") + +ohos_sa_profile("dscreen_sa_profile") { + sources = [ + "4807.xml", + "4808.xml", + ] + + part_name = "distributed_screen" +} \ No newline at end of file diff --git a/screenhandler/BUILD.gn b/screenhandler/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..cd5c1ec49c8f59cd388d9ca2409b79083b66878a --- /dev/null +++ b/screenhandler/BUILD.gn @@ -0,0 +1,56 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +ohos_shared_library("distributed_screen_handler") { + include_dirs = [ + "//third_party/json/include", + "${windowmanager_path}/interfaces/innerkits/dm", + "${mediastandard_path}/interfaces/innerkits/native/media/include", + "${fwk_common_path}/utils/include", + ] + + include_dirs += [ + "include", + "${common_path}/include", + ] + + sources = [ + "src/dscreen_handler.cpp", + ] + + deps = [ + "//utils/native/base:utils", + "${mediastandard_path}/interfaces/innerkits/native/media:media_client", + "${windowmanager_path}/interfaces/innerkits:wm_interface", + "${windowmanager_path}/dm:libdm", + "${common_path}:distributed_screen_utils", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dscreenhandler\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + ] + + subsystem_name = "distributedhardware" + + part_name = "distributed_screen" +} \ No newline at end of file diff --git a/screenhandler/include/dscreen_handler.h b/screenhandler/include/dscreen_handler.h new file mode 100644 index 0000000000000000000000000000000000000000..5dae8783eab0f8f64f394148d511630e76e177d0 --- /dev/null +++ b/screenhandler/include/dscreen_handler.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_HANDLER_H +#define OHOS_DSCREEN_HANDLER_H + +#include "screen_manager.h" + +#include "ihardware_handler.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedHardware { +class DScreenHandler : public IHardwareHandler, public Rosen::ScreenManager::IScreenListener { +DECLARE_SINGLE_INSTANCE_BASE(DScreenHandler); +public: + void OnConnect(uint64_t screenId) override; + void OnDisconnect(uint64_t screenId) override; + void OnChange(uint64_t screenId) override {}; + int32_t Initialize() override; + std::vector Query() override; + std::map QueryExtraInfo() override; + bool IsSupportPlugin() override; + void RegisterPluginListener(std::shared_ptr listener) override; + +private: + DScreenHandler(); + ~DScreenHandler(); + std::string QueryCodecInfo(); + + std::shared_ptr listener_ = nullptr; + std::string codecInfoStr_; +}; + +#ifdef __cplusplus +extern "C" { +#endif +__attribute__((visibility("default"))) IHardwareHandler* GetHardwareHandler(); +#ifdef __cplusplus +} +#endif +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/screenhandler/src/dscreen_handler.cpp b/screenhandler/src/dscreen_handler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7da13653eaa1f0933c422c70d9866f92c9e55aad --- /dev/null +++ b/screenhandler/src/dscreen_handler.cpp @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_handler.h" + +#include "avcodec_info.h" +#include "avcodec_list.h" +#include "nlohmann/json.hpp" +#include "screen.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "dscreen_util.h" + +using json = nlohmann::json; + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(DScreenHandler); + +DScreenHandler::DScreenHandler() +{ + DHLOGI("DScreenHandler constructor."); +} + +DScreenHandler::~DScreenHandler() +{ + DHLOGI("~DScreenHandler"); +} + +int32_t DScreenHandler::Initialize() +{ + DHLOGI("DScreenHandler Initialize"); + bool ret = Rosen::ScreenManager::GetInstance().RegisterScreenListener(this); + if (!ret) { + DHLOGE("register screen listener failed."); + } + return DH_SUCCESS; +} + +void DScreenHandler::OnConnect(uint64_t screenId) +{ + DHLOGI("on screen connect"); + if (screenId != SCREEN_ID_DEFAULT) { + return; + } + std::string dhId = DSCREEN_PREFIX + SEPERATOR + std::to_string(screenId); + sptr screen = Rosen::ScreenManager::GetInstance().GetScreenById(screenId); + uint32_t screenWidth = screen->GetWidth(); + uint32_t screenHeight = screen->GetHeight(); + + json attrJson; + attrJson[KEY_VERSION] = DSCREEN_VERSION; + attrJson[KEY_SCREEN_WIDTH] = screenWidth; + attrJson[KEY_SCREEN_HEIGHT] = screenHeight; + attrJson[KEY_CODECTYPE] = QueryCodecInfo(); + + if (listener_ != nullptr) { + listener_->PluginHardware(dhId, attrJson.dump()); + } +} + +void DScreenHandler::OnDisconnect(uint64_t screenId) +{ + DHLOGI("on screen disconnect"); + std::string dhId = DSCREEN_PREFIX + SEPERATOR + std::to_string(screenId); + if (listener_ != nullptr) { + listener_->UnPluginHardware(dhId); + } +} + +std::vector DScreenHandler::Query() +{ + DHLOGI("DScreenHandler query hardware info"); + std::vector dhItemVec; + std::vector> screens = Rosen::ScreenManager::GetInstance().GetAllScreens(); + sptr screen = screens[SCREEN_ID_DEFAULT]; + std::string dhId = SCREEN_PREFIX + SEPERATOR + std::to_string(screen->GetId()); + uint32_t screenWidth = screen->GetWidth(); + uint32_t screenHeight = screen->GetHeight(); + + json attrJson; + attrJson[KEY_VERSION] = DSCREEN_VERSION; + attrJson[KEY_SCREEN_WIDTH] = screenWidth; + attrJson[KEY_SCREEN_HEIGHT] = screenHeight; + attrJson[KEY_CODECTYPE] = QueryCodecInfo(); + + DHItem dhItem; + dhItem.dhId = dhId; + dhItem.attrs = attrJson.dump(); + dhItemVec.push_back(dhItem); + DHLOGD("query result: dhId: %s, attrs: %s", GetAnonyString(dhId).c_str(), attrJson.dump().c_str()); + + return dhItemVec; +} + +std::map DScreenHandler::QueryExtraInfo() +{ + DHLOGD("DScreenHandler queryExtraInfo"); + std::map extraInfo; + return extraInfo; +} + +bool DScreenHandler::IsSupportPlugin() +{ + DHLOGD("DScreenHandler IsSupportPlugin"); + return true; +} + +void DScreenHandler::RegisterPluginListener(std::shared_ptr listener) +{ + DHLOGD("DScreenHandler register plugin listener"); + listener_ = listener; +} + +std::string DScreenHandler::QueryCodecInfo() +{ + DHLOGD("DScreenHandler QueryCodecInfo"); + if (!codecInfoStr_.empty()) { + return codecInfoStr_; + } + + // query codec info + std::shared_ptr codecList = Media::AVCodecListFactory::CreateAVCodecList(); + std::vector> caps = codecList->GetVideoEncoderCaps(); + for (const auto &cap : caps) { + std::shared_ptr codecInfo = cap->GetCodecInfo(); + codecInfoStr_.append(codecInfo->GetName()); + codecInfoStr_.append(SEPERATOR); + } + return codecInfoStr_; +} + +IHardwareHandler* GetHardwareHandler() +{ + return &DScreenHandler::GetInstance(); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/common/databuffer/include/data_buffer.h b/services/common/databuffer/include/data_buffer.h new file mode 100644 index 0000000000000000000000000000000000000000..0c876f9242626b9ca4518613ac9ebdc63b80dcc2 --- /dev/null +++ b/services/common/databuffer/include/data_buffer.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DATA_BUFFER_H +#define OHOS_DATA_BUFFER_H + +#include + +namespace OHOS { +namespace DistributedHardware { +class DataBuffer { +public: + explicit DataBuffer(size_t capacity); + ~DataBuffer(); + + size_t Capacity() const; + uint8_t *Data() const; + +private: + size_t capacity_ = 0; + uint8_t *data_ = nullptr; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/common/databuffer/src/data_buffer.cpp b/services/common/databuffer/src/data_buffer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb6b9387e396d92dc01e12035da043f717627b45 --- /dev/null +++ b/services/common/databuffer/src/data_buffer.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "data_buffer.h" + +#include + +namespace OHOS { +namespace DistributedHardware { +DataBuffer::DataBuffer(size_t capacity) +{ + if (capacity != 0) { + data_ = new (std::nothrow) uint8_t[capacity] {0}; + if (data_ != nullptr) { + capacity_ = capacity; + } + } +} + +DataBuffer::~DataBuffer() +{ + if (data_ != nullptr) { + delete []data_; + data_ = nullptr; + } + + capacity_ = 0; +} + +size_t DataBuffer::Capacity() const +{ + return capacity_; +} + +uint8_t *DataBuffer::Data() const +{ + return data_; +} +} // namespace DistributedHardware +} // namespcae OHOS \ No newline at end of file diff --git a/services/common/screen_channel/include/iscreen_channel.h b/services/common/screen_channel/include/iscreen_channel.h new file mode 100644 index 0000000000000000000000000000000000000000..b5b06ed85db6e841057baac7dd69b150be1d19ae --- /dev/null +++ b/services/common/screen_channel/include/iscreen_channel.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ISCREEN_CHANNEL_H +#define OHOS_ISCREEN_CHANNEL_H + +#include + +#include "data_buffer.h" +#include "iscreen_channel_listener.h" + +namespace OHOS { +namespace DistributedHardware { +class IScreenChannel { +public: + virtual ~IScreenChannel() = default; + + virtual int32_t CreateSession(const std::shared_ptr &listener) = 0; + virtual int32_t ReleaseSession() = 0; + virtual int32_t OpenSession() = 0; + virtual int32_t CloseSession() = 0; + virtual int32_t SendData(const std::shared_ptr &data) = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/common/screen_channel/include/iscreen_channel_listener.h b/services/common/screen_channel/include/iscreen_channel_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..1fb3b18f6be586ca2e89dcf26cf079902544605b --- /dev/null +++ b/services/common/screen_channel/include/iscreen_channel_listener.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ISCREEN_CHANNEL_LISTENER_H +#define OHOS_ISCREEN_CHANNEL_LISTENER_H + +#include "data_buffer.h" +#include + +namespace OHOS { +namespace DistributedHardware { +class IScreenChannelListener { +public: + virtual ~IScreenChannelListener() = default; + + virtual void OnSessionOpened() = 0; + virtual void OnSessionClosed() = 0; + virtual void OnDataReceived(const std::shared_ptr &data) = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/common/test/unittest/BUILD.gn b/services/common/test/unittest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..2f28638c9377547d0a43063a6ea6530ae2048302 --- /dev/null +++ b/services/common/test/unittest/BUILD.gn @@ -0,0 +1,20 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +group("service_common_test") { + testonly = true + deps = [ + "databuffer:data_buffer_test", + "utils:utils_test", + ] +} \ No newline at end of file diff --git a/services/common/test/unittest/databuffer/BUILD.gn b/services/common/test/unittest/databuffer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..3303511bec70b8e1deb12f1b0770fedc2c04042a --- /dev/null +++ b/services/common/test/unittest/databuffer/BUILD.gn @@ -0,0 +1,68 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +module_out_path = "distributed_screen/services/data_buffer_test" + +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "${windowmanager_path}/interfaces/innerkits/wm", + "${graphicstandard_path}/interfaces/innerkits/surface", + "${graphicstandard_path}/rosen/modules/render_service_client/core/ui", + "${fwk_common_path}/utils/include", + "${fwk_common_path}/log/include", + "${fwk_utils_path}/include/log", + ] + + include_dirs += [ + "include", + "${common_path}/include", + "${services_path}/common/databuffer/include", + "${services_path}/common/utils/include", + ] +} + +## UnitTest data_buffer_test +ohos_unittest("DataBufferTest") { + module_out_path = module_out_path + + sources = [ + "${services_path}/common/test/unittest/databuffer/data_buffer_test.cpp", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + "${fwk_utils_path}:distributedhardwareutils", + "${graphicstandard_path}/frameworks/surface:surface", + "${graphicstandard_path}/rosen/modules/render_service_client:librender_service_client", + "${windowmanager_path}/interfaces/innerkits:wm_interface", + "${windowmanager_path}/wm:libwm", + "//foundation/distributedhardware/distributedscreen/services/screentransport/screensinktrans:distributed_screen_sinktrans", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + ] +} + +group("data_buffer_test") { + testonly = true + deps = [ ":DataBufferTest" ] +} \ No newline at end of file diff --git a/services/common/test/unittest/databuffer/data_buffer_test.cpp b/services/common/test/unittest/databuffer/data_buffer_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fec3459934fceae34344dc9d975a322f39fd92bf --- /dev/null +++ b/services/common/test/unittest/databuffer/data_buffer_test.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define private public +#include "data_buffer_test.h" +#undef private + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void DataBufferTest::SetUpTestCase(void) {} + +void DataBufferTest::TearDownTestCase(void) {} + +void DataBufferTest::SetUp() +{ + dataBuffer_ = std::make_shared(capacity); +} + +void DataBufferTest::TearDown() {} + +/** + * @tc.name: Capacity_001 + * @tc.desc: Verify the Capacity function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DataBufferTest, Capacity_001, TestSize.Level1) +{ + size_t actual = dataBuffer_->Capacity(); + EXPECT_EQ(capacity, actual); +} + +/** + * @tc.name: Data_001 + * @tc.desc: Verify the Data function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DataBufferTest, Data_001, TestSize.Level1) +{ + uint8_t *actual = dataBuffer_->Data(); + EXPECT_NE(nullptr, actual); +} +} +} \ No newline at end of file diff --git a/services/common/test/unittest/databuffer/data_buffer_test.h b/services/common/test/unittest/databuffer/data_buffer_test.h new file mode 100644 index 0000000000000000000000000000000000000000..aa75c15dc91fc4af665278c23a4f512146c86a58 --- /dev/null +++ b/services/common/test/unittest/databuffer/data_buffer_test.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "data_buffer.h" + +namespace OHOS { +namespace DistributedHardware { +class DataBufferTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + size_t capacity = 1; + std::shared_ptr dataBuffer_ = nullptr; +}; +} +} \ No newline at end of file diff --git a/services/common/test/unittest/utils/BUILD.gn b/services/common/test/unittest/utils/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..a7fb715264ea9c94175649f1c74a5720ef01ee27 --- /dev/null +++ b/services/common/test/unittest/utils/BUILD.gn @@ -0,0 +1,70 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +module_out_path = "distributed_screen/services/utils_test" + +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "//third_party/json/include", + "${windowmanager_path}/interfaces/innerkits/wm", + "${graphicstandard_path}/interfaces/innerkits/surface", + "${graphicstandard_path}/rosen/modules/render_service_client/core/ui", + "${fwk_common_path}/utils/include", + "${fwk_common_path}/log/include", + "${fwk_utils_path}/include/log", + ] + + include_dirs += [ + "include", + "${common_path}/include", + "${services_path}/common/databuffer/include", + "${services_path}/common/utils/include", + ] +} + +## UnitTest utils_test +ohos_unittest("UtilsTest") { + module_out_path = module_out_path + + sources = [ + "${services_path}/common/test/unittest/utils/video_param_test.cpp", + "${services_path}/common/test/unittest/utils/dscreen_maprelation_test.cpp", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + "${fwk_utils_path}:distributedhardwareutils", + "${graphicstandard_path}/frameworks/surface:surface", + "${graphicstandard_path}/rosen/modules/render_service_client:librender_service_client", + "${windowmanager_path}/interfaces/innerkits:wm_interface", + "${windowmanager_path}/wm:libwm", + "//foundation/distributedhardware/distributedscreen/services/screenservice/sinkservice:distributed_screen_sink", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + ] +} + +group("utils_test") { + testonly = true + deps = [ ":UtilsTest" ] +} \ No newline at end of file diff --git a/services/common/test/unittest/utils/dscreen_maprelation_test.cpp b/services/common/test/unittest/utils/dscreen_maprelation_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec0078f53576f23a884f547ede6782deeb2ea6a8 --- /dev/null +++ b/services/common/test/unittest/utils/dscreen_maprelation_test.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define private public +#include "dscreen_maprelation_test.h" +#undef private + +using json = nlohmann::json; + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void DScreenMapRelationTest::SetUpTestCase(void) {} + +void DScreenMapRelationTest::TearDownTestCase(void) {} + +void DScreenMapRelationTest::SetUp() +{ + dscreenMapRelation = std::make_shared(); +} + +void DScreenMapRelationTest::TearDown() {} + +/** + * @tc.name: GetDisplayId_001 + * @tc.desc: Verify the GetDisplayId function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ + HWTEST_F(DScreenMapRelationTest, GetDisplayId_001, TestSize.Level1) +{ + uint64_t displayId = 0; + dscreenMapRelation->SetDisplayId(displayId); + uint64_t actual = dscreenMapRelation->GetDisplayId(); + EXPECT_EQ(displayId, actual); +} + +/** + * @tc.name: GetScreenId_001 + * @tc.desc: Verify the GetScreenId function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ + HWTEST_F(DScreenMapRelationTest, GetScreenId_001, TestSize.Level1) +{ + uint64_t screenId = 0; + dscreenMapRelation->SetScreenId(screenId); + uint64_t actual = dscreenMapRelation->GetScreenId(); + EXPECT_EQ(screenId, actual); +} + +/** + * @tc.name: GetDisplayRect_001 + * @tc.desc: Verify the GetDisplayRect function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ + HWTEST_F(DScreenMapRelationTest, GetDisplayRect_001, TestSize.Level1) +{ + DisplayRect res; + int32_t startX = 10; + res.startX = startX; + dscreenMapRelation->SetDisplayRect(res); + DisplayRect actual = dscreenMapRelation->GetDisplayRect(); + EXPECT_EQ(startX, actual.startX); +} + +/** + * @tc.name: GetScreenRect_001 + * @tc.desc: Verify the GetScreenRect function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ + HWTEST_F(DScreenMapRelationTest, GetScreenRect_001, TestSize.Level1) +{ + ScreenRect res; + int32_t startX = 10; + res.startX = startX; + dscreenMapRelation->SetScreenRect(res); + ScreenRect actual = dscreenMapRelation->GetScreenRect(); + EXPECT_EQ(startX, actual.startX); +} +} +} \ No newline at end of file diff --git a/services/common/test/unittest/utils/dscreen_maprelation_test.h b/services/common/test/unittest/utils/dscreen_maprelation_test.h new file mode 100644 index 0000000000000000000000000000000000000000..9806911444001380981ad52f38ba6f6f89bd7c7a --- /dev/null +++ b/services/common/test/unittest/utils/dscreen_maprelation_test.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_maprelation.h" + +using json = nlohmann::json; + +namespace OHOS { +namespace DistributedHardware { +class DScreenMapRelationTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + std::shared_ptr dscreenMapRelation = nullptr; +}; +} +} \ No newline at end of file diff --git a/services/common/test/unittest/utils/video_param_test.cpp b/services/common/test/unittest/utils/video_param_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78052b97ee8d2d4c43a02d86009ad2cf2a32a7f8 --- /dev/null +++ b/services/common/test/unittest/utils/video_param_test.cpp @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define private public +#include "video_param_test.h" +#undef private + +using json = nlohmann::json; + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void VideoParamTest::SetUpTestCase(void) {} + +void VideoParamTest::TearDownTestCase(void) {} + +void VideoParamTest::SetUp() +{ + videoParam_ = std::make_shared(); +} + +void VideoParamTest::TearDown() {} + +/** + * @tc.name: GetScreenWidth_001 + * @tc.desc: Verify the GetScreenWidth function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(VideoParamTest, GetScreenWidth_001, TestSize.Level1) +{ + uint32_t screenWidth = 1; + videoParam_->SetScreenWidth(screenWidth); + uint32_t actual = videoParam_->GetScreenWidth(); + EXPECT_EQ(screenWidth, actual); +} + +/** + * @tc.name: GetScreenHeight_001 + * @tc.desc: Verify the GetScreenHeight function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(VideoParamTest, GetScreenHeight_001, TestSize.Level1) +{ + uint32_t screenHeight = 1; + videoParam_->SetScreenHeight(screenHeight); + uint32_t actual = videoParam_->GetScreenHeight(); + EXPECT_EQ(screenHeight, actual); +} + +/** + * @tc.name: GetVideoWidth_001 + * @tc.desc: Verify the GetVideoWidth function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(VideoParamTest, GetVideoWidth_001, TestSize.Level1) +{ + uint32_t videoWidth = 1; + videoParam_->SetVideoWidth(videoWidth); + uint32_t actual = videoParam_->GetVideoWidth(); + EXPECT_EQ(videoWidth, actual); +} + +/** + * @tc.name: GetVideoHeight_001 + * @tc.desc: Verify the GetVideoHeight function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(VideoParamTest, GetVideoHeight_001, TestSize.Level1) +{ + uint32_t videoHeight = 1; + videoParam_->SetVideoHeight(videoHeight); + uint32_t actual = videoParam_->GetVideoHeight(); + EXPECT_EQ(videoHeight, actual); +} + +/** + * @tc.name: GetFps_001 + * @tc.desc: Verify the GetFps function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(VideoParamTest, GetFps_001, TestSize.Level1) +{ + uint32_t fps = 1; + videoParam_->SetFps(fps); + uint32_t actual = videoParam_->GetFps(); + EXPECT_EQ(fps, actual); +} + +/** + * @tc.name: GetCodecType_001 + * @tc.desc: Verify the GetCodecType function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(VideoParamTest, GetCodecType_001, TestSize.Level1) +{ + uint8_t codecType = 1; + videoParam_->SetCodecType(codecType); + uint8_t actual = videoParam_->GetCodecType(); + EXPECT_EQ(codecType, actual); +} + +/** + * @tc.name: GetVideoFormat_001 + * @tc.desc: Verify the GetVideoFormat function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(VideoParamTest, GetVideoFormat_001, TestSize.Level1) +{ + uint8_t videoFormat = 1; + videoParam_->SetVideoFormat(videoFormat); + uint8_t actual = videoParam_->GetVideoFormat(); + EXPECT_EQ(videoFormat, actual); +} +} +} \ No newline at end of file diff --git a/services/common/test/unittest/utils/video_param_test.h b/services/common/test/unittest/utils/video_param_test.h new file mode 100644 index 0000000000000000000000000000000000000000..d58777991152016fd2707982bb44770bc134cc01 --- /dev/null +++ b/services/common/test/unittest/utils/video_param_test.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "video_param.h" + +using json = nlohmann::json; + +namespace OHOS { +namespace DistributedHardware { +class VideoParamTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + std::shared_ptr videoParam_ = nullptr; +}; +} +} \ No newline at end of file diff --git a/services/common/utils/include/dscreen_maprelation.h b/services/common/utils/include/dscreen_maprelation.h new file mode 100644 index 0000000000000000000000000000000000000000..25c440278e7b9d4f27327825afbc211d71ab9b29 --- /dev/null +++ b/services/common/utils/include/dscreen_maprelation.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_MAP_RELATION_H +#define OHOS_DSCREEN_MAP_RELATION_H + +#include "nlohmann/json.hpp" + +using json = nlohmann::json; + +namespace OHOS { +namespace DistributedHardware { +struct ScreenRect { + int32_t startX; + int32_t startY; + uint16_t width; + uint16_t height; +}; +struct DisplayRect { + int32_t startX; + int32_t startY; + int32_t width; + int32_t height; +}; + +class DScreenMapRelation { +public: + void SetDisplayId(uint64_t displayId); + uint64_t GetDisplayId() const; + void SetScreenId(uint64_t screenId); + uint64_t GetScreenId() const; + void SetDisplayRect(const DisplayRect &displayRect); + DisplayRect& GetDisplayRect(); + void SetScreenRect(const ScreenRect &screenRect); + ScreenRect& GetScreenRect(); +private: + friend void to_json(json &j, const DScreenMapRelation &dScreenMapRelation); + friend void from_json(const json &j, DScreenMapRelation &dScreenMapRelation); + + uint64_t displayId_; + uint64_t screenId_; + DisplayRect displayRect_; + ScreenRect screenRect_; +}; +void to_json(const json &j, const DScreenMapRelation &dScreenMapRelation); +void from_json(const json &j, DScreenMapRelation &dScreenMapRelation); +void to_json(json &j, const DisplayRect &rect); +void to_json(json &j, const ScreenRect &rect); +void from_json(const json &j, DisplayRect &rect); +void from_json(const json &j, ScreenRect &rect); +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/common/utils/include/video_param.h b/services/common/utils/include/video_param.h new file mode 100644 index 0000000000000000000000000000000000000000..3db42ee2ea917249a464f4a0252bd2de1d99dfec --- /dev/null +++ b/services/common/utils/include/video_param.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_VIDEO_PARAM_H +#define OHOS_DSCREEN_VIDEO_PARAM_H + +#include "nlohmann/json.hpp" + +#include "dscreen_constants.h" + +using json = nlohmann::json; + +namespace OHOS { +namespace DistributedHardware { +class VideoParam { +public: + void SetScreenWidth(uint32_t screenWidth); + uint32_t GetScreenWidth() const; + void SetScreenHeight(uint32_t screenHeight); + uint32_t GetScreenHeight() const; + void SetVideoWidth(uint32_t videoWidth); + uint32_t GetVideoWidth() const; + void SetVideoHeight(uint32_t videoHeight); + uint32_t GetVideoHeight() const; + void SetFps(uint32_t fps); + uint32_t GetFps() const; + void SetCodecType(uint8_t codecType); + uint8_t GetCodecType() const; + void SetVideoFormat(uint8_t videoFormat); + uint8_t GetVideoFormat() const; +private: + friend void to_json(json &j, const VideoParam &videoParam); + friend void from_json(const json &j, VideoParam &videoParam); + + uint32_t screenWidth_; + uint32_t screenHeight_; + uint32_t videoWidth_; + uint32_t videoHeight_; + uint32_t fps_ = DEFAULT_FPS; + uint8_t codecType_ = DEFAULT_CODECTYPE; + uint8_t videoFormat_ = DEFAULT_VIDEO_FORMAT; +}; +void to_json(json &j, const VideoParam &videoParam); +void from_json(const json &j, VideoParam &videoParam); +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/common/utils/src/dscreen_maprelation.cpp b/services/common/utils/src/dscreen_maprelation.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28db6e2e66522255389e7450d704f540c58c4a53 --- /dev/null +++ b/services/common/utils/src/dscreen_maprelation.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_maprelation.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" + +using json = nlohmann::json; + +namespace OHOS { +namespace DistributedHardware { +void DScreenMapRelation::SetDisplayId(uint64_t displayId) +{ + displayId_ = displayId; +} + +uint64_t DScreenMapRelation::GetDisplayId() const +{ + return displayId_; +} + +void DScreenMapRelation::SetScreenId(uint64_t screenId) +{ + screenId_ = screenId; +} + +uint64_t DScreenMapRelation::GetScreenId() const +{ + return screenId_; +} + +void DScreenMapRelation::SetDisplayRect(const DisplayRect &displayRect) +{ + displayRect_ = displayRect; +} + +DisplayRect& DScreenMapRelation::GetDisplayRect() +{ + return displayRect_; +} + +void DScreenMapRelation::SetScreenRect(const ScreenRect &screenRect) +{ + screenRect_ = screenRect; +} + +ScreenRect& DScreenMapRelation::GetScreenRect() +{ + return screenRect_; +} + +void to_json(json &j, const DScreenMapRelation &dScreenMapRelation) +{ + json displayRectJson; + json screenRectJson; + to_json(displayRectJson, dScreenMapRelation.displayRect_); + to_json(screenRectJson, dScreenMapRelation.screenRect_); + j = json { + {KEY_DISPLAY_ID, dScreenMapRelation.displayId_}, + {KEY_SCREEN_ID, dScreenMapRelation.screenId_}, + {KEY_DISPLAY_RECT, displayRectJson}, + {KEY_SCREEN_RECT, screenRectJson} + }; +} + +void from_json(const json &j, DScreenMapRelation &dScreenMapRelation) +{ + j.at(KEY_DISPLAY_ID).get_to(dScreenMapRelation.displayId_); + j.at(KEY_SCREEN_ID).get_to(dScreenMapRelation.screenId_); + from_json(j.at(KEY_DISPLAY_RECT), dScreenMapRelation.displayRect_); + from_json(j.at(KEY_SCREEN_RECT), dScreenMapRelation.screenRect_); +} + +void to_json(json &j, const DisplayRect &rect) +{ + j = json { + {KEY_POINT_START_X, rect.startX}, + {KEY_POINT_START_Y, rect.startY}, + {KEY_WIDTH, rect.width}, + {KEY_HEIGHT, rect.height} + }; +} + +void from_json(const json &j, DisplayRect &rect) +{ + j.at(KEY_POINT_START_X).get_to(rect.startX); + j.at(KEY_POINT_START_Y).get_to(rect.startY); + j.at(KEY_WIDTH).get_to(rect.width); + j.at(KEY_HEIGHT).get_to(rect.height); +} + +void to_json(json &j, const ScreenRect &rect) +{ + j = json { + {KEY_POINT_START_X, rect.startX}, + {KEY_POINT_START_Y, rect.startY}, + {KEY_WIDTH, rect.width}, + {KEY_HEIGHT, rect.height} + }; +} + +void from_json(const json &j, ScreenRect &rect) +{ + j.at(KEY_POINT_START_X).get_to(rect.startX); + j.at(KEY_POINT_START_Y).get_to(rect.startY); + j.at(KEY_WIDTH).get_to(rect.width); + j.at(KEY_HEIGHT).get_to(rect.height); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/common/utils/src/video_param.cpp b/services/common/utils/src/video_param.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e1f420d9eca593f934f371a8e79d6381b56ed6e --- /dev/null +++ b/services/common/utils/src/video_param.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "video_param.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" + +using json = nlohmann::json; + +namespace OHOS { +namespace DistributedHardware { +void VideoParam::SetScreenWidth(uint32_t screenWidth) +{ + screenWidth_ = screenWidth; +} + +uint32_t VideoParam::GetScreenWidth() const +{ + return screenWidth_; +} + +void VideoParam::SetScreenHeight(uint32_t screenHeight) +{ + screenHeight_ = screenHeight; +} + +uint32_t VideoParam::GetScreenHeight() const +{ + return screenHeight_; +} + +void VideoParam::SetVideoWidth(uint32_t videoWidth) +{ + videoWidth_ = videoWidth; +} + +uint32_t VideoParam::GetVideoWidth() const +{ + return videoWidth_; +} + +void VideoParam::SetVideoHeight(uint32_t videoHeight) +{ + videoHeight_ = videoHeight; +} + +uint32_t VideoParam::GetVideoHeight() const +{ + return videoHeight_; +} + +void VideoParam::SetFps(uint32_t fps) +{ + fps_ = fps; +} + +uint32_t VideoParam::GetFps() const +{ + return fps_; +} + +void VideoParam::SetCodecType(uint8_t codecType) +{ + codecType_ = codecType; +} + +uint8_t VideoParam::GetCodecType() const +{ + return codecType_; +} + +void VideoParam::SetVideoFormat(uint8_t videoFormat) +{ + videoFormat_ = videoFormat; +} + +uint8_t VideoParam::GetVideoFormat() const +{ + return videoFormat_; +} + +void to_json(json &j, const DistributedHardware::VideoParam &videoParam) +{ + j = json { + {KEY_SCREEN_WIDTH, videoParam.screenWidth_}, + {KEY_SCREEN_HEIGHT, videoParam.screenHeight_}, + {KEY_VIDEO_WIDTH, videoParam.videoWidth_}, + {KEY_VIDEO_HEIGHT, videoParam.videoHeight_}, + {KEY_FPS, videoParam.fps_}, + {KEY_CODECTYPE, videoParam.codecType_} + }; +} + +void from_json(const json &j, DistributedHardware::VideoParam &videoParam) +{ + j.at(KEY_SCREEN_WIDTH).get_to(videoParam.screenWidth_); + j.at(KEY_SCREEN_HEIGHT).get_to(videoParam.screenHeight_); + j.at(KEY_VIDEO_WIDTH).get_to(videoParam.videoWidth_); + j.at(KEY_VIDEO_HEIGHT).get_to(videoParam.videoHeight_); + j.at(KEY_FPS).get_to(videoParam.fps_); + j.at(KEY_CODECTYPE).get_to(videoParam.codecType_); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/screenclient/BUILD.gn b/services/screenclient/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..603c6378320ee2db5c1f01f7afcbe49337cbb27c --- /dev/null +++ b/services/screenclient/BUILD.gn @@ -0,0 +1,59 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +ohos_shared_library("distributed_screen_client") { + + include_dirs = [ + "${windowmanager_path}/interfaces/innerkits/wm", + "${graphicstandard_path}/interfaces/innerkits/surface", + "${graphicstandard_path}/rosen/modules/render_service_client/core/ui", + "${fwk_common_path}/utils/include", + "//foundation/multimodalinput/input/interfaces/native/innerkits/event/include/" + ] + + include_dirs += [ + "include", + "${common_path}/include", + ] + + sources = [ + "${services_path}/screenclient/src/screen_client.cpp", + "${services_path}/screenclient/src/screen_client_window_adapter.cpp", + ] + + deps = [ + "${common_path}:distributed_screen_utils", + "${graphicstandard_path}/frameworks/surface:surface", + "${windowmanager_path}/interfaces/innerkits:wm_interface", + "${windowmanager_path}/wm:libwm", + "//foundation/multimodalinput/input:mmi_uinject", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dscreenclient\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + ] + + part_name = "distributed_screen" + subsystem_name = "distributedhardware" +} + diff --git a/services/screenclient/include/screen_client.h b/services/screenclient/include/screen_client.h new file mode 100644 index 0000000000000000000000000000000000000000..be3e8f995c5b2b7304c00372af42370ab09a0ccd --- /dev/null +++ b/services/screenclient/include/screen_client.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SCREEN_CLIENT_H +#define OHOS_SCREEN_CLIENT_H + +#include +#include +#include +#include +#include +#include +#include + +#include "single_instance.h" +#include "surface.h" + +#include "dscreen_constants.h" +#include "screen_client_common.h" + +namespace OHOS { +namespace DistributedHardware { +class ScreenClient { +DECLARE_SINGLE_INSTANCE_BASE(ScreenClient); +public: + int32_t AddWindow(std::shared_ptr windowProperty); + sptr GetSurface(int32_t windowId); + int32_t ShowWindow(int32_t windowId); + int32_t MoveWindow(int32_t windowId, int32_t startX, int32_t startY); + int32_t RemoveWindow(int32_t windowId); + int32_t HideWindow(int32_t windowId); +private: + ScreenClient() = default; + ~ScreenClient(); + std::map> surfaceMap_; + std::atomic windowId_ = INVALID_WINDOW_ID; + std::mutex surfaceMapMutex_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screenclient/include/screen_client_common.h b/services/screenclient/include/screen_client_common.h new file mode 100644 index 0000000000000000000000000000000000000000..431cc212f2b0704b0d36e529b164cb39fa115e85 --- /dev/null +++ b/services/screenclient/include/screen_client_common.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_CLIENT_COMMON_H +#define OHOS_DSCREEN_CLIENT_COMMON_H + +namespace OHOS { +namespace DistributedHardware { +struct WindowProperty { + int32_t type; + uint64_t displayId; + int32_t startX; + int32_t startY; + uint16_t width; + uint16_t height; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/screenclient/include/screen_client_window_adapter.h b/services/screenclient/include/screen_client_window_adapter.h new file mode 100644 index 0000000000000000000000000000000000000000..80347e6028d52842e6f92b8ff6ec0f7d4db3a447 --- /dev/null +++ b/services/screenclient/include/screen_client_window_adapter.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SCREEN_CLIENT_WINDOW_H +#define OHOS_SCREEN_CLIENT_WINDOW_H + +#include +#include +#include +#include +#include + +#include "i_input_event_consumer.h" +#include "single_instance.h" +#include "surface.h" +#include "window.h" + +#include "screen_client.h" +#include "screen_client_common.h" + +namespace OHOS { +namespace DistributedHardware { +class ScreenClientWindowAdapter { +DECLARE_SINGLE_INSTANCE_BASE(ScreenClientWindowAdapter); +public: + sptr CreateWindow(std::shared_ptr windowProperty, int32_t windowId); + int32_t ShowWindow(int32_t windowId); + int32_t HideWindow(int32_t windowId); + int32_t MoveWindow(int32_t windowId, int32_t startX, int32_t startY); + int32_t RemoveWindow(int32_t windowId); +private: + ScreenClientWindowAdapter() = default; + ~ScreenClientWindowAdapter(); + std::map> windowIdMap_; + std::mutex windowIdMapMutex_; +}; + +class ScreenClientInputEventListener : public RefBase, public MMI::IInputEventConsumer { +public: + ScreenClientInputEventListener() = default; + void OnInputEvent(std::shared_ptr pointerEvent) const override; + void OnInputEvent(std::shared_ptr keyEvent) const override; + void OnInputEvent(std::shared_ptr axisEvent) const override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screenclient/src/screen_client.cpp b/services/screenclient/src/screen_client.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2b17115838b04f7a4a2c4ac913c1ef491bf35a8 --- /dev/null +++ b/services/screenclient/src/screen_client.cpp @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screen_client.h" + +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "screen_client_window_adapter.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(ScreenClient); + +ScreenClient::~ScreenClient() +{ + DHLOGD("~ScreenClient"); + { + std::lock_guard dataLock(surfaceMapMutex_); + for (const auto &item : surfaceMap_) { + int32_t ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(item.first); + if (ret != DH_SUCCESS) { + DHLOGE("windowId (ID = %d) remove failed.", item.first); + return; + } + } + surfaceMap_.clear(); + windowId_ = INVALID_WINDOW_ID; + } + DHLOGD("ScreenClient Destory."); + return; +} + +int32_t ScreenClient::AddWindow(std::shared_ptr windowProperty) +{ + if (windowProperty == nullptr) { + DHLOGE("windowProperty is nullptr."); + return ERR_DH_SCREEN_SCREENCLIENT_ADD_WINDOW_ERROR; + } + int32_t windowId = ++windowId_; + sptr surface = ScreenClientWindowAdapter::GetInstance().CreateWindow(windowProperty, windowId); + if (surface == nullptr) { + DHLOGE("Surface is nullptr."); + return ERR_DH_SCREEN_SCREENCLIENT_ADD_WINDOW_ERROR; + } + { + std::lock_guard dataLock(surfaceMapMutex_); + surfaceMap_.emplace(windowId, surface); + } + DHLOGI("Add window (ID = %d) success.", windowId); + return windowId; +} + +int32_t ScreenClient::ShowWindow(int32_t windowId) +{ + { + std::lock_guard dataLock(surfaceMapMutex_); + auto iter = surfaceMap_.find(windowId); + if (iter == surfaceMap_.end()) { + DHLOGE("windowId (ID = %d) is non-existent.", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR; + } + } + int32_t ret = ScreenClientWindowAdapter::GetInstance().ShowWindow(windowId); + if (DH_SUCCESS != ret) { + DHLOGE("Show window (ID = %d) failed.", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR; + } + DHLOGI("Show window (ID = %d) success.", windowId); + return ret; +} + +int32_t ScreenClient::HideWindow(int32_t windowId) +{ + { + std::lock_guard dataLock(surfaceMapMutex_); + auto iter = surfaceMap_.find(windowId); + if (iter == surfaceMap_.end()) { + DHLOGE("windowId (ID = %d) is non-existent.", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR; + } + } + int32_t ret = ScreenClientWindowAdapter::GetInstance().HideWindow(windowId); + if (DH_SUCCESS != ret) { + DHLOGE("Hide window (ID = %d) failed.", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR; + } + DHLOGI("Hide window (ID = %d) success.", windowId); + return ret; +} + +int32_t ScreenClient::MoveWindow(int32_t windowId, int32_t startX, int32_t startY) +{ + { + std::lock_guard dataLock(surfaceMapMutex_); + auto iter = surfaceMap_.find(windowId); + if (iter == surfaceMap_.end()) { + DHLOGE("windowId (ID = %d) is non-existent.", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR; + } + } + int32_t ret = ScreenClientWindowAdapter::GetInstance().MoveWindow(windowId, startX, startY); + if (DH_SUCCESS != ret) { + DHLOGE("Move window (ID = %d) failed.", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR; + } + DHLOGD("Move window (ID = %d) success.", windowId); + return ret; +} + +sptr ScreenClient::GetSurface(int32_t windowId) +{ + sptr surface = nullptr; + { + std::lock_guard dataLock(surfaceMapMutex_); + auto iter = surfaceMap_.find(windowId); + if (iter == surfaceMap_.end()) { + DHLOGE("windowId (ID = %d) is non-existent.", windowId); + return nullptr; + } + surface = iter->second; + } + if (surface == nullptr) { + DHLOGE("Get surface is null(ID = %d).", windowId); + return nullptr; + } + DHLOGD("Get surface (ID = %d) success.", windowId); + return surface; +} + +int32_t ScreenClient::RemoveWindow(int32_t windowId) +{ + { + std::lock_guard dataLock(surfaceMapMutex_); + auto iter = surfaceMap_.find(windowId); + if (iter == surfaceMap_.end()) { + DHLOGE("windowId (ID = %d) is non-existent.", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR; + } + surfaceMap_.erase(windowId); + } + int32_t ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(windowId); + if (ret != DH_SUCCESS) { + DHLOGE("windowId (ID = %d) remove failed.", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR; + } + DHLOGD("windowId (ID = %d) remove success.", windowId); + return DH_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screenclient/src/screen_client_window_adapter.cpp b/services/screenclient/src/screen_client_window_adapter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e3eb78cb0f42d1892b3c7ab73bb4525f071e4e8 --- /dev/null +++ b/services/screenclient/src/screen_client_window_adapter.cpp @@ -0,0 +1,215 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screen_client_window_adapter.h" + +#include "rs_surface_node.h" +#include "window_option.h" +#include "wm_common.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(ScreenClientWindowAdapter); +ScreenClientWindowAdapter::~ScreenClientWindowAdapter() +{ + DHLOGD("~ScreenClientWindowAdapter"); + { + std::lock_guard dataLock(windowIdMapMutex_); + for (const auto &item : windowIdMap_) { + auto window = item.second; + if (window == nullptr) { + DHLOGE("window is nullptr(windowId = %d).", item.first); + return; + } + if (OHOS::Rosen::WMError::WM_OK != window->Destroy()) { + DHLOGE("screenclientSurface is nullptr(windowId = %d).", item.first); + return; + } + } + windowIdMap_.clear(); + } + DHLOGD("ScreenClientWindowAdapter Destory."); + return; +} + +sptr ScreenClientWindowAdapter::CreateWindow(std::shared_ptr windowProperty, + int32_t windowId) +{ + if (windowProperty == nullptr) { + DHLOGE("windowProperty is nullptr."); + return nullptr; + } + sptr option = new Rosen::WindowOption(); + option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_LAUNCHING); + option->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING); + option->SetDisplayId(windowProperty->displayId); + std::string windowName = SCREEN_CLIENT_WINDOW + std::to_string(windowId); + sptr window = Rosen::Window::Create(windowName, option); + if (window == nullptr || window->GetSurfaceNode() == nullptr) { + DHLOGE("Create screen client window failed."); + return nullptr; + } + auto surface = window->GetSurfaceNode()->GetSurface(); + if (surface == nullptr) { + window->Destroy(); + DHLOGE("surface is nullptr"); + return nullptr; + } + std::shared_ptr listener = + std::make_shared(ScreenClientInputEventListener()); + window->AddInputEventListener(listener); + DHLOGD("Create window name is %s.", windowName.c_str()); + if (OHOS::Rosen::WMError::WM_OK != window->Resize(windowProperty->width, windowProperty->height)) { + window->Destroy(); + DHLOGE("Window resize failed."); + return nullptr; + } + if (OHOS::Rosen::WMError::WM_OK != window->MoveTo(windowProperty->startX, windowProperty->startY)) { + window->Destroy(); + DHLOGE("Window moveto failed."); + return nullptr; + } + { + std::lock_guard dataLock(windowIdMapMutex_); + windowIdMap_.emplace(windowId, window); + } + DHLOGD("Get surface(windowId = %d) success.", windowId); + return surface; +} + +int32_t ScreenClientWindowAdapter::ShowWindow(int32_t windowId) +{ + sptr window = nullptr; + { + std::lock_guard dataLock(windowIdMapMutex_); + auto iter = windowIdMap_.find(windowId); + if (iter == windowIdMap_.end()) { + DHLOGE("ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR."); + return ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR; + } + window = iter->second; + } + if (window == nullptr) { + DHLOGE("Current window is null."); + return ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR; + } + if (OHOS::Rosen::WMError::WM_OK != window->Show()) { + DHLOGE("Show window failed."); + return ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR; + } + DHLOGD("Show window (windowId = %d) success.", windowId); + return DH_SUCCESS; +} + +int32_t ScreenClientWindowAdapter::HideWindow(int32_t windowId) +{ + sptr window = nullptr; + { + std::lock_guard dataLock(windowIdMapMutex_); + auto iter = windowIdMap_.find(windowId); + if (iter == windowIdMap_.end()) { + DHLOGE("ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR."); + return ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR; + } + window = iter->second; + } + if (window == nullptr) { + DHLOGE("Current window is null."); + return ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR; + } + if (OHOS::Rosen::WMError::WM_OK != window->Hide()) { + DHLOGE("Hide window failed."); + return ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR; + } + DHLOGD("Hide window (windowId = %d) success.", windowId); + return DH_SUCCESS; +} + +int32_t ScreenClientWindowAdapter::MoveWindow(int32_t windowId, int32_t startX, int32_t startY) +{ + sptr window = nullptr; + { + std::lock_guard dataLock(windowIdMapMutex_); + auto iter = windowIdMap_.find(windowId); + if (iter == windowIdMap_.end()) { + DHLOGE("Invalid windowId (windowId = %d).", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR; + } + window = iter->second; + } + if (window == nullptr) { + DHLOGE("Current window is null(windowId = %d).", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR; + } + if (OHOS::Rosen::WMError::WM_OK != window->MoveTo(startX, startY)) { + DHLOGE("Move window failed (windowId = %d).", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR; + } + DHLOGD("MoveTo window (windowId = %d) success.", windowId); + return DH_SUCCESS; +} + +int32_t ScreenClientWindowAdapter::RemoveWindow(int32_t windowId) +{ + sptr window = nullptr; + { + std::lock_guard dataLock(windowIdMapMutex_); + auto iter = windowIdMap_.find(windowId); + if (iter == windowIdMap_.end()) { + DHLOGE("Invalid windowId(windowId = %d).", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR; + } + window = iter->second; + windowIdMap_.erase(windowId); + } + if (window == nullptr) { + DHLOGE("Current window is null(windowId = %d).", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR; + } + if (OHOS::Rosen::WMError::WM_OK != window->Hide()) { + DHLOGE("Remove window window failed(windowId = %d).", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR; + } + if (OHOS::Rosen::WMError::WM_OK != window->Destroy()) { + DHLOGE("Remove window window failed(windowId = %d).", windowId); + return ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR; + } + DHLOGD("Remove window success(windowId = %d).", windowId); + return DH_SUCCESS; +} + +void ScreenClientInputEventListener::OnInputEvent(std::shared_ptr pointerEvent) const +{ + DHLOGI("onInputEvent, pointerEvent"); + pointerEvent->MarkProcessed(); +} + +void ScreenClientInputEventListener::OnInputEvent(std::shared_ptr keyEvent) const +{ + DHLOGI("onInputEvent, keyEvent"); + keyEvent->MarkProcessed(); +} + +void ScreenClientInputEventListener::OnInputEvent(std::shared_ptr axisEvent) const +{ + DHLOGI("onInputEvent, axisEvent"); + axisEvent->MarkProcessed(); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screenclient/test/unittest/BUILD.gn b/services/screenclient/test/unittest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4becb3d9ac9c6dcdb9481c4963ca3a6d853c00b8 --- /dev/null +++ b/services/screenclient/test/unittest/BUILD.gn @@ -0,0 +1,69 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +module_out_path = "distributed_screen/screen_client_test" + +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "${windowmanager_path}/interfaces/innerkits/wm", + "${graphicstandard_path}/interfaces/innerkits/surface", + "${graphicstandard_path}/rosen/modules/render_service_client/core/ui", + "${fwk_common_path}/utils/include", + "${fwk_utils_path}/include/log", + ] + + include_dirs += [ + "include", + "${common_path}/include", + "${services_path}/screenclient/include", + "${services_path}/screenclient/test/unittest/include", + ] +} + +## UnitTest screen_client_test +ohos_unittest("ScreenClientTest") { + module_out_path = module_out_path + + sources = [ + "${services_path}/screenclient/test/unittest/src/screen_client_test.cpp", + "${services_path}/screenclient/test/unittest/src/screen_client_window_adapter_test.cpp", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + "${fwk_utils_path}:distributedhardwareutils", + "${graphicstandard_path}/frameworks/surface:surface", + "${graphicstandard_path}/rosen/modules/render_service_client:librender_service_client", + "${windowmanager_path}/interfaces/innerkits:wm_interface", + "${windowmanager_path}/wm:libwm", + "//foundation/distributedhardware/distributedscreen/services/screenclient:distributed_screen_client", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + ] +} + +group("screen_client_test") { + testonly = true + deps = [ ":ScreenClientTest" ] +} + diff --git a/services/screenclient/test/unittest/include/screen_client_test.h b/services/screenclient/test/unittest/include/screen_client_test.h new file mode 100644 index 0000000000000000000000000000000000000000..d085267a8ea5190225b0d12ca72b77f07bfbbc9a --- /dev/null +++ b/services/screenclient/test/unittest/include/screen_client_test.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "screen_client.h" +#include "screen_client_window_adapter.h" + +namespace OHOS { +namespace DistributedHardware { +class ScreenClientTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} +} \ No newline at end of file diff --git a/services/screenclient/test/unittest/include/screen_client_window_adapter_test.h b/services/screenclient/test/unittest/include/screen_client_window_adapter_test.h new file mode 100644 index 0000000000000000000000000000000000000000..1b222a5efe32fab58e5372b9f401d6f21e613698 --- /dev/null +++ b/services/screenclient/test/unittest/include/screen_client_window_adapter_test.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "rs_surface_node.h" +#include "window_option.h" +#include "wm_common.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "screen_client_window_adapter.h" + +namespace OHOS { +namespace DistributedHardware { +class ScreenClientWindowAdapterTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} +} \ No newline at end of file diff --git a/services/screenclient/test/unittest/src/screen_client_test.cpp b/services/screenclient/test/unittest/src/screen_client_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..859df30d566dc2e139d8e37345c404c1fca58355 --- /dev/null +++ b/services/screenclient/test/unittest/src/screen_client_test.cpp @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define private public +#include "screen_client_test.h" +#undef private + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void ScreenClientTest::SetUpTestCase(void) {} + +void ScreenClientTest::TearDownTestCase(void) {} + +void ScreenClientTest::SetUp() {} + +void ScreenClientTest::TearDown() {} + +/** + * @tc.name: ShowWindow_001 + * @tc.desc: Verify the ShowWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientTest, ShowWindow_001, TestSize.Level1) +{ + int32_t windowId = 0; + int32_t actual = ScreenClient::GetInstance().ShowWindow(windowId); + EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR, actual); +} + +/** + * @tc.name: ShowWindow_002 + * @tc.desc: Verify the ShowWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientTest, ShowWindow_002, TestSize.Level1) +{ + std::shared_ptr windowProperty = std::make_shared(); + ScreenClient::GetInstance().windowId_ = 0; + int32_t windowId = ScreenClient::GetInstance().AddWindow(windowProperty); + int32_t actual = ScreenClient::GetInstance().ShowWindow(windowId); + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: AddWindow_001 + * @tc.desc: Verify the AddWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientTest, AddWindow_001, TestSize.Level1) +{ + std::shared_ptr windowProperty = nullptr; + int32_t actual = ScreenClient::GetInstance().AddWindow(windowProperty); + EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_ADD_WINDOW_ERROR, actual); +} + +/** + * @tc.name: AddWindow_002 + * @tc.desc: Verify the AddWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientTest, AddWindow_002, TestSize.Level1) +{ + std::shared_ptr windowProperty = std::make_shared(); + ScreenClient::GetInstance().windowId_ = 2; + int32_t actual = ScreenClient::GetInstance().AddWindow(windowProperty); + EXPECT_EQ(3, actual); +} + +/** + * @tc.name: MoveWindow_001 + * @tc.desc: Verify the MoveWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientTest, MoveWindow_001, TestSize.Level1) +{ + int32_t windowId = 0; + int32_t startX = 0; + int32_t startY = 0; + int32_t actual = ScreenClient::GetInstance().MoveWindow(windowId, startX, startY); + EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR, actual); +} + +/** + * @tc.name: MoveWindow_002 + * @tc.desc: Verify the MoveWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientTest, MoveWindow_002, TestSize.Level1) +{ + int32_t windowId = 1; + int32_t startX = 0; + int32_t startY = 0; + int32_t actual = ScreenClient::GetInstance().MoveWindow(windowId, startX, startY); + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: RemoveWindow_001 + * @tc.desc: Verify the RemoveWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientTest, RemoveWindow_001, TestSize.Level1) +{ + int32_t windowId = 0; + int32_t actual = ScreenClient::GetInstance().RemoveWindow(windowId); + EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR, actual); +} + +/** + * @tc.name: RemoveWindow_002 + * @tc.desc: Verify the RemoveWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientTest, RemoveWindow_002, TestSize.Level1) +{ + int32_t windowId = 1; + int32_t actual = ScreenClient::GetInstance().RemoveWindow(windowId); + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: GetSurface_001 + * @tc.desc: Verify the GetSurface function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientTest, GetSurface_001, TestSize.Level1) +{ + int32_t windowId = 0; + sptr actualSurface = ScreenClient::GetInstance().GetSurface(windowId); + EXPECT_EQ(nullptr, actualSurface); +} + +/** + * @tc.name: GetSurface_002 + * @tc.desc: Verify the GetSurface function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientTest, GetSurface_002, TestSize.Level1) +{ + std::shared_ptr windowProperty = std::make_shared(); + ScreenClient::GetInstance().windowId_ = 0; + int32_t windowId = ScreenClient::GetInstance().AddWindow(windowProperty); + sptr actualSurface = ScreenClient::GetInstance().GetSurface(windowId); + EXPECT_NE(nullptr, actualSurface); +} +} +} \ No newline at end of file diff --git a/services/screenclient/test/unittest/src/screen_client_window_adapter_test.cpp b/services/screenclient/test/unittest/src/screen_client_window_adapter_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..464b9dd87debe4b4c72cc7f1d7cb8dacc4cb46cf --- /dev/null +++ b/services/screenclient/test/unittest/src/screen_client_window_adapter_test.cpp @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define private public +#include "screen_client_window_adapter_test.h" +#undef private + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void ScreenClientWindowAdapterTest::SetUpTestCase(void) {} + +void ScreenClientWindowAdapterTest::TearDownTestCase(void) {} + +void ScreenClientWindowAdapterTest::SetUp() {} + +void ScreenClientWindowAdapterTest::TearDown() {} + +/** + * @tc.name: CreateWindow_001 + * @tc.desc: Verify the CreateWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientWindowAdapterTest, CreateWindow_001, TestSize.Level1) +{ + std::shared_ptr windowProperty = nullptr; + int32_t windowId = 0; + sptr actualSurface = ScreenClientWindowAdapter::GetInstance().CreateWindow(windowProperty, windowId); + EXPECT_EQ(nullptr, actualSurface); +} + +/** + * @tc.name: CreateWindow_002 + * @tc.desc: Verify the CreateWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientWindowAdapterTest, CreateWindow_002, TestSize.Level1) +{ + std::shared_ptr windowProperty = std::make_shared(); + int32_t windowId = 2; + sptr surface = nullptr; + sptr actualSurface = ScreenClientWindowAdapter::GetInstance().CreateWindow(windowProperty, windowId); + EXPECT_NE(surface, actualSurface); +} + +/** + * @tc.name: ShowWindow_001 + * @tc.desc: Verify the ShowWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientWindowAdapterTest, ShowWindow_001, TestSize.Level1) +{ + int32_t windowId = 0; + int32_t actual = ScreenClientWindowAdapter::GetInstance().ShowWindow(windowId); + EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR, actual); +} + +/** + * @tc.name: ShowWindow_002 + * @tc.desc: Verify the ShowWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientWindowAdapterTest, ShowWindow_002, TestSize.Level1) +{ + int32_t windowId = 1; + int32_t actual = ScreenClientWindowAdapter::GetInstance().ShowWindow(windowId); + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: MoveWindow_001 + * @tc.desc: Verify the MoveWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientWindowAdapterTest, MoveWindow_001, TestSize.Level1) +{ + int32_t windowId = 0; + int32_t startX = 0; + int32_t startY = 0; + int32_t actual = ScreenClientWindowAdapter::GetInstance().MoveWindow(windowId, startX, startY); + EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR, actual); +} + +/** + * @tc.name: MoveWindow_002 + * @tc.desc: Verify the MoveWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientWindowAdapterTest, MoveWindow_002, TestSize.Level1) +{ + int32_t windowId = 1; + int32_t startX = 0; + int32_t startY = 0; + int32_t actual = ScreenClientWindowAdapter::GetInstance().MoveWindow(windowId, startX, startY); + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: RemoveWindow_001 + * @tc.desc: Verify the RemoveWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientWindowAdapterTest, RemoveWindow_001, TestSize.Level1) +{ + int32_t windowId = 0; + int32_t actual = ScreenClientWindowAdapter::GetInstance().RemoveWindow(windowId); + EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR, actual); +} + +/** + * @tc.name: RemoveWindow_002 + * @tc.desc: Verify the RemoveWindow function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenClientWindowAdapterTest, RemoveWindow_002, TestSize.Level1) +{ + int32_t windowId = 1; + int32_t actual = ScreenClientWindowAdapter::GetInstance().RemoveWindow(windowId); + EXPECT_EQ(DH_SUCCESS, actual); +} +} +} \ No newline at end of file diff --git a/services/screenservice/sinkservice/BUILD.gn b/services/screenservice/sinkservice/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..806fc477888038bd7078b013116640a3bbf7a995 --- /dev/null +++ b/services/screenservice/sinkservice/BUILD.gn @@ -0,0 +1,83 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +ohos_shared_library("distributed_screen_sink") { + include_dirs = [ + "//third_party/json/include", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "//foundation/graphic/standard/interfaces/innerkits/surface", + "${windowmanager_path}/interfaces/innerkits/dm", + "${fwk_common_path}/utils/include", + ] + + include_dirs += [ + "./dscreenservice/include", + "./screenregionmgr/include", + "${interfaces_path}/innerkits/native_cpp/screen_sink/include", + "${interfaces_path}/innerkits/native_cpp/screen_sink/include/callback", + "${interfaces_path}/innerkits/native_cpp/screen_source/include", + "${interfaces_path}/innerkits/native_cpp/screen_source/include/callback", + "${common_path}/include", + "${services_path}/common/utils/include", + "${services_path}/common/databuffer/include", + "${services_path}/common/screen_channel/include", + "${services_path}/screentransport/screensinktrans/include", + "${services_path}/screentransport/screensinkprocessor/include", + "${services_path}/screentransport/screensinkprocessor/decoder/include", + "${services_path}/screenclient/include/", + ] + + sources = [ + "${services_path}/common/utils/src/dscreen_maprelation.cpp", + "${services_path}/common/utils/src/video_param.cpp", + "${interfaces_path}/innerkits/native_cpp/screen_sink/src/dscreen_sink_proxy.cpp", + "${interfaces_path}/innerkits/native_cpp/screen_source/src/dscreen_source_proxy.cpp", + "./dscreenservice/src/dscreen_sink_service.cpp", + "./dscreenservice/src/dscreen_sink_stub.cpp", + "./screenregionmgr/src/screenregion.cpp", + "./screenregionmgr/src/screenregionmgr.cpp", + ] + + deps = [ + "//utils/native/base:utils", + "${common_path}:distributed_screen_utils", + "${services_path}/screentransport/screensinktrans:distributed_screen_sinktrans", + "${services_path}/screenclient:distributed_screen_client", + "//foundation/graphic/standard/frameworks/surface:surface", + "${windowmanager_path}/interfaces/innerkits:wm_interface", + "${windowmanager_path}/dm:libdm", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dscreensink\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "multimedia_media_standard:media_client", + ] + + subsystem_name = "distributedhardware" + + part_name = "distributed_screen" +} \ No newline at end of file diff --git a/services/screenservice/sinkservice/dscreenservice/include/dscreen_sink_service.h b/services/screenservice/sinkservice/dscreenservice/include/dscreen_sink_service.h new file mode 100644 index 0000000000000000000000000000000000000000..a314bacb659f56489276b189f9871c8fbd7dd874 --- /dev/null +++ b/services/screenservice/sinkservice/dscreenservice/include/dscreen_sink_service.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_SINK_SERVICE_H +#define OHOS_DSCREEN_SINK_SERVICE_H + +#include "ipc_object_stub.h" +#include "system_ability.h" + +#include "dscreen_sink_stub.h" +#include "screenregionmgr.h" + +namespace OHOS { +namespace DistributedHardware { +class DScreenSinkService : public SystemAbility, public DScreenSinkStub, + public std::enable_shared_from_this { +DECLARE_SYSTEM_ABILITY(DScreenSinkService); +public: + DScreenSinkService(int32_t saId, bool runOnCreate); + ~DScreenSinkService() = default; + int32_t InitSink(const std::string ¶ms) override; + int32_t ReleaseSink() override; + int32_t SubscribeLocalHardware(const std::string &dhId, const std::string ¶m) override; + int32_t UnsubscribeLocalHardware(const std::string &dhId) override; + void DScreenNotify(const std::string &devId, int32_t eventCode, const std::string &eventContent) override; + +protected: + void OnStart() override; + void OnStop() override; + DISALLOW_COPY_AND_MOVE(DScreenSinkService); + +private: + std::shared_ptr screenRegionMgr_ = nullptr; + bool registerToService_ = false; + + bool Init(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screenservice/sinkservice/dscreenservice/include/dscreen_sink_stub.h b/services/screenservice/sinkservice/dscreenservice/include/dscreen_sink_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..68f8c47afb0b4a10fdb68a728ae278f28bbb4039 --- /dev/null +++ b/services/screenservice/sinkservice/dscreenservice/include/dscreen_sink_stub.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_SINK_STUB_H +#define OHOS_DSCREEN_SINK_STUB_H + +#include + +#include "iremote_stub.h" + +#include "idscreen_sink.h" + +namespace OHOS { +namespace DistributedHardware { +class DScreenSinkStub : public IRemoteStub { +public: + DScreenSinkStub(); + virtual ~DScreenSinkStub(); + virtual int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) override; +private: + using DScreenSinkFunc = int32_t (DScreenSinkStub::*)(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + std::map memberFuncMap_; + + int32_t InitSinkInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + int32_t ReleaseSinkInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + int32_t SubscribeDistributedHardwareInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + int32_t UnsubscribeDistributedHardwareInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + int32_t DScreenNotifyInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screenservice/sinkservice/dscreenservice/src/dscreen_sink_service.cpp b/services/screenservice/sinkservice/dscreenservice/src/dscreen_sink_service.cpp new file mode 100644 index 0000000000000000000000000000000000000000..643b4155930d7ae1a8cdb572b4da137129c0e06f --- /dev/null +++ b/services/screenservice/sinkservice/dscreenservice/src/dscreen_sink_service.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_sink_service.h" + +#include "if_system_ability_manager.h" +#include "ipc_skeleton.h" +#include "ipc_types.h" +#include "iservice_registry.h" +#include "string_ex.h" +#include "system_ability_definition.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "dscreen_util.h" + +namespace OHOS { +namespace DistributedHardware { +REGISTER_SYSTEM_ABILITY_BY_ID(DScreenSinkService, DISTRIBUTED_HARDWARE_SCREEN_SINK_SA_ID, true); + +DScreenSinkService::DScreenSinkService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate) +{ + DHLOGI("DScreenSinkService construct."); +} + +void DScreenSinkService::OnStart() +{ + DHLOGI("dscreen sink service start."); + if (!Init()) { + DHLOGE("DScreenSinkService init failed."); + return; + } + DHLOGI("DScreenSinkService start success."); +} + +void DScreenSinkService::OnStop() +{ + DHLOGI("dscreen sink service stop."); + registerToService_ = false; +} + +bool DScreenSinkService::Init() +{ + DHLOGI("dscreen sink service start init."); + if (!registerToService_) { + bool ret = Publish(this); + if (!ret) { + DHLOGE("dscreen sink publish service failed."); + return false; + } + registerToService_ = true; + } + DHLOGI("dscreenService init success."); + return true; +} + +int32_t DScreenSinkService::InitSink(const std::string ¶ms) +{ + DHLOGI("InitSink"); + return DH_SUCCESS; +} + +int32_t DScreenSinkService::ReleaseSink() +{ + DHLOGI("ReleaseSink"); + return ScreenRegionManager::GetInstance().ReleaseAllRegions(); +} + +int32_t DScreenSinkService::SubscribeLocalHardware(const std::string &dhId, const std::string ¶m) +{ + DHLOGI("SubscribeLocalHardware"); + return DH_SUCCESS; +} + +int32_t DScreenSinkService::UnsubscribeLocalHardware(const std::string &dhId) +{ + DHLOGI("UnsubscribeLocalHardware"); + return DH_SUCCESS; +} + +void DScreenSinkService::DScreenNotify(const std::string &devId, int32_t eventCode, const std::string &eventContent) +{ + DHLOGI("DScreenNotify, devId:%s, eventCode: %d, eventContent:%s", GetAnonyString(devId).c_str(), + eventCode, eventContent.c_str()); + ScreenRegionManager::GetInstance().HandleDScreenNotify(devId, eventCode, eventContent); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screenservice/sinkservice/dscreenservice/src/dscreen_sink_stub.cpp b/services/screenservice/sinkservice/dscreenservice/src/dscreen_sink_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ca32cd51d4113566e8d08bc6f7f539de478e048 --- /dev/null +++ b/services/screenservice/sinkservice/dscreenservice/src/dscreen_sink_stub.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_sink_stub.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +DScreenSinkStub::DScreenSinkStub() +{ + DHLOGI("DScreenSinkStub construct."); + memberFuncMap_[INIT_SINK] = &DScreenSinkStub::InitSinkInner; + memberFuncMap_[RELEASE_SINK] = &DScreenSinkStub::ReleaseSinkInner; + memberFuncMap_[SUBSCRIBE_DISTRIBUTED_HARDWARE] = &DScreenSinkStub::SubscribeDistributedHardwareInner; + memberFuncMap_[UNSUBSCRIBE_DISTRIBUTED_HARDWARE] = &DScreenSinkStub::UnsubscribeDistributedHardwareInner; + memberFuncMap_[DSCREEN_NOTIFY] = &DScreenSinkStub::DScreenNotifyInner; +} + +DScreenSinkStub::~DScreenSinkStub() +{ + DHLOGI("DScreenSinkStub deconstruct."); +} + +int32_t DScreenSinkStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DHLOGI("OnRemoteRequest, code: %d", code); + std::u16string desc = DScreenSinkStub::GetDescriptor(); + std::u16string remoteDesc = data.ReadInterfaceToken(); + if (desc != remoteDesc) { + DHLOGE("DScreenSinkStub::OnRemoteRequest remoteDesc is invalid!"); + return ERR_INVALID_DATA; + } + + const auto &iter = memberFuncMap_.find(code); + if (iter == memberFuncMap_.end()) { + DHLOGE("invalid request code."); + return ERR_DH_SCREEN_SA_INVALID_IPC_CALL; + } + DScreenSinkFunc &func = iter->second; + return (this->*func)(data, reply, option); +} + +int32_t DScreenSinkStub::InitSinkInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DHLOGI("InitSinkInner"); + std::string param = data.ReadString(); + int32_t ret = InitSink(param); + reply.WriteInt32(ret); + return DH_SUCCESS; +} + +int32_t DScreenSinkStub::ReleaseSinkInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DHLOGI("ReleaseSinkInner"); + int32_t ret = ReleaseSink(); + reply.WriteInt32(ret); + return DH_SUCCESS; +} + +int32_t DScreenSinkStub::SubscribeDistributedHardwareInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DHLOGI("SubscribeDistributedHardwareInner"); + std::string dhId = data.ReadString(); + std::string param = data.ReadString(); + int32_t ret = SubscribeLocalHardware(dhId, param); + reply.WriteInt32(ret); + return DH_SUCCESS; +} + +int32_t DScreenSinkStub::UnsubscribeDistributedHardwareInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DHLOGI("UnsubscribeDistributedHardwareInner"); + std::string dhId = data.ReadString(); + int32_t ret = UnsubscribeLocalHardware(dhId); + reply.WriteInt32(ret); + return DH_SUCCESS; +} + +int32_t DScreenSinkStub::DScreenNotifyInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DHLOGI("DScreenNotifyInner"); + std::string devId = data.ReadString(); + int32_t eventCode = data.ReadInt32(); + std::string eventContent = data.ReadString(); + + DScreenNotify(devId, eventCode, eventContent); + return DH_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screenservice/sinkservice/screenregionmgr/include/screenregion.h b/services/screenservice/sinkservice/screenregionmgr/include/screenregion.h new file mode 100644 index 0000000000000000000000000000000000000000..8c400b67e56c8d38cd093383d6ba5b578b0a16d2 --- /dev/null +++ b/services/screenservice/sinkservice/screenregionmgr/include/screenregion.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_SCREEN_REGION_H +#define OHOS_DSCREEN_SCREEN_REGION_H + +#include "surface.h" + +#include "iscreen_sink_trans_callback.h" +#include "iscreen_sink_trans.h" +#include "dscreen_maprelation.h" +#include "video_param.h" + +namespace OHOS { +namespace DistributedHardware { +class ScreenSinkTransCallback : public IScreenSinkTransCallback { +public: + ~ScreenSinkTransCallback() override {}; + virtual void OnTransError(int32_t err, const std::string &content) = 0; + virtual void OnError(int32_t err, const std::string &content) override + { + OnTransError(err, content); + } +}; + +class ScreenRegion : public ScreenSinkTransCallback, public std::enable_shared_from_this { +public: + ScreenRegion(const std::string &remoteDevId, uint64_t screenId, uint64_t displayId); + ~ScreenRegion(); + void OnTransError(int32_t err, const std::string &content) override; + void SetVideoParam(std::shared_ptr &videoParam); + void SetMapRelation(std::shared_ptr &mapRElation); + std::string GetRemoteDevId(); + uint64_t GetScreenId(); + uint64_t GetDisplayId(); + int32_t SetUp(); + int32_t Start(); + int32_t Stop(); + +private: + std::string remoteDevId_; + uint64_t screenId_; // local screen id + uint64_t displayId_; // local display id + bool isRunning = false; + + std::shared_ptr videoParam_ = nullptr; + std::shared_ptr mapRelation_ = nullptr; + sptr surface_ = nullptr; + int32_t windowId_ = INVALID_WINDOW_ID; + std::shared_ptr sinkTrans_ = nullptr; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screenservice/sinkservice/screenregionmgr/include/screenregionmgr.h b/services/screenservice/sinkservice/screenregionmgr/include/screenregionmgr.h new file mode 100644 index 0000000000000000000000000000000000000000..65fcd09a882ea8fc3f83f4e5b6ebdd77e9641450 --- /dev/null +++ b/services/screenservice/sinkservice/screenregionmgr/include/screenregionmgr.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_SCREEN_REGION_MGR_H +#define OHOS_DSCREEN_SCREEN_REGION_MGR_H + +#include +#include + +#include "single_instance.h" + +#include "screenregion.h" +#include "idscreen_source.h" + +namespace OHOS { +namespace DistributedHardware { +class ScreenRegionManager { +DECLARE_SINGLE_INSTANCE_BASE(ScreenRegionManager); +public: + int32_t ReleaseAllRegions(); + void HandleDScreenNotify(const std::string &devId, int32_t eventCode, const std::string &eventContent); + +private: + ScreenRegionManager(); + ~ScreenRegionManager(); + std::map> screenRegions_; + std::mutex screenRegionsMtx_; + + sptr GetDScreenSourceSA(const std::string &devId); + int32_t NotifyRemoteScreenService(const std::string &remoteDevId, const std::string &dhId, + int32_t eventCode, const std::string &eventContent); + void HandleNotifySetUp(const std::string &remoteDevId, const std::string &eventContent); + void NotifyRemoteSourceSetUpResult(const std::string &remoteDevId, const std::string &dhId, + int32_t errCode, const std::string &errContent); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screenservice/sinkservice/screenregionmgr/src/screenregion.cpp b/services/screenservice/sinkservice/screenregionmgr/src/screenregion.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fdf73b13ddc1d91c21ed0ffaf6ef88eee83c99f --- /dev/null +++ b/services/screenservice/sinkservice/screenregionmgr/src/screenregion.cpp @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screenregion.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "dscreen_util.h" +#include "screen_client.h" +#include "screen_client_common.h" +#include "screen_sink_trans.h" + +namespace OHOS { +namespace DistributedHardware { +ScreenRegion::ScreenRegion(const std::string &remoteDevId, uint64_t screenId, uint64_t displayId) +{ + DHLOGD("ScreenRegion construct"); + remoteDevId_ = remoteDevId; + screenId_ = screenId; + displayId_ = displayId; + windowId_ = INVALID_WINDOW_ID; +} + +ScreenRegion::~ScreenRegion() +{ + DHLOGD("ScreenRegion deConstruct"); + if (!sinkTrans_) { + sinkTrans_->Release(); + } +} + +void ScreenRegion::OnTransError(int32_t err, const std::string &content) +{ + DHLOGE("OnTransError, err: %d", err); + Stop(); +} + +void ScreenRegion::SetVideoParam(std::shared_ptr &videoParam) +{ + videoParam_ = videoParam; +} + +void ScreenRegion::SetMapRelation(std::shared_ptr &mapRelation) +{ + mapRelation_ = mapRelation; +} + +uint64_t ScreenRegion::GetScreenId() +{ + return screenId_; +} + +uint64_t ScreenRegion::GetDisplayId() +{ + return displayId_; +} + +std::string ScreenRegion::GetRemoteDevId() +{ + return remoteDevId_; +} + +int32_t ScreenRegion::SetUp() +{ + DHLOGI("ScreenRegion::SetUp, remoteDevId: %s", GetAnonyString(remoteDevId_).c_str()); + + std::shared_ptr windowProperty = std::make_shared(); + windowProperty->displayId = displayId_; + + ScreenRect screenRect = mapRelation_->GetScreenRect(); + windowProperty->startX = screenRect.startX; + windowProperty->startY = screenRect.startY; + windowProperty->width = screenRect.width; + windowProperty->height = screenRect.height; + + windowId_ = ScreenClient::GetInstance().AddWindow(windowProperty); + if (windowId_ < 0) { + DHLOGE("AddWindow failed."); + return ERR_DH_SCREEN_SA_DSCREEN_SCREENGION_SETUP_FAILED; + } + int32_t ret = ScreenClient::GetInstance().ShowWindow(windowId_); + if (ret != DH_SUCCESS) { + DHLOGE("show window failed."); + return ERR_DH_SCREEN_SA_DSCREEN_SCREENGION_SETUP_FAILED; + } + + sptr surface = ScreenClient::GetInstance().GetSurface(windowId_); + if (!surface) { + DHLOGE("get window surface failed."); + return ERR_DH_SCREEN_SA_DSCREEN_SCREENGION_SETUP_FAILED; + } + + surface_ = surface; + + if (!sinkTrans_) { + sinkTrans_ = std::make_shared(); + } + + sinkTrans_->RegisterStateCallback(shared_from_this()); + sinkTrans_->SetImageSurface(surface_); + ret = sinkTrans_->SetUp(*videoParam_, *videoParam_, remoteDevId_); + if (ret != DH_SUCCESS) { + DHLOGE("sinkTrans setup failed."); + return ERR_DH_SCREEN_SA_DSCREEN_SCREENGION_SETUP_FAILED; + } + + return DH_SUCCESS; +} + +int32_t ScreenRegion::Start() +{ + DHLOGI("ScreenRegion::Start remoteDevId: %s", GetAnonyString(remoteDevId_).c_str()); + if (!sinkTrans_) { + DHLOGE("sink trans not init."); + return ERR_DH_SCREEN_SA_SINKTRANS_NOT_INIT; + } + + int32_t ret = sinkTrans_->Start(); + if (ret != DH_SUCCESS) { + DHLOGE("sink trans start failed."); + return ret; + } + isRunning = true; + return DH_SUCCESS; +} + +int32_t ScreenRegion::Stop() +{ + DHLOGI("ScreenRegion::Stop remoteDevId: %s", GetAnonyString(remoteDevId_).c_str()); + if (!isRunning) { + DHLOGI("ScreenRegion not running, no need stop"); + return DH_SUCCESS; + } + + if (!sinkTrans_) { + DHLOGE("sink trans not init."); + return ERR_DH_SCREEN_SA_SINKTRANS_NOT_INIT; + } + + int32_t ret = sinkTrans_->Stop(); + if (ret != DH_SUCCESS) { + DHLOGE("sink trans stop failed."); + return ret; + } + + ret = sinkTrans_->Release(); + if (ret != DH_SUCCESS) { + DHLOGE("sink trans release failed."); + return ret; + } + + ret = ScreenClient::GetInstance().RemoveWindow(windowId_); + if (ret != DH_SUCCESS) { + DHLOGE("remove window failed."); + return ret; + } + + isRunning = false; + return DH_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screenservice/sinkservice/screenregionmgr/src/screenregionmgr.cpp b/services/screenservice/sinkservice/screenregionmgr/src/screenregionmgr.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48457eeb65876894c1f35366eff3db4bc7e15a21 --- /dev/null +++ b/services/screenservice/sinkservice/screenregionmgr/src/screenregionmgr.cpp @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screenregionmgr.h" + +#include "display_manager.h" +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "nlohmann/json.hpp" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "dscreen_maprelation.h" +#include "dscreen_util.h" +#include "idscreen_source.h" +#include "screen_client.h" +#include "screen_client_common.h" + +using json = nlohmann::json; + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(ScreenRegionManager); +ScreenRegionManager::ScreenRegionManager() +{ + DHLOGI("ScreenRegionManager"); +} + +ScreenRegionManager::~ScreenRegionManager() +{ + DHLOGI("~ScreenRegionManager"); +} + +int32_t ScreenRegionManager::ReleaseAllRegions() +{ + DHLOGI("ScreenRegionManager::ReleaseAllRegion"); + std::lock_guard lock(screenRegionsMtx_); + for (const auto &item : screenRegions_) { + std::shared_ptr screenRegion = item.second; + if (!screenRegion) { + continue; + } + int32_t ret = screenRegion->Stop(); + if (ret != DH_SUCCESS) { + DHLOGE("Release region failed, remoteDevId: %s, err: %d", + GetAnonyString(screenRegion->GetRemoteDevId()).c_str(), ret); + } + } + screenRegions_.clear(); + return DH_SUCCESS; +} + +void ScreenRegionManager::HandleDScreenNotify(const std::string &remoteDevId, int32_t eventCode, + const std::string &eventContent) +{ + DHLOGI("HandleDScreenNotify, remoteDevId: %s, eventCode: %d", GetAnonyString(remoteDevId).c_str(), eventCode); + if (eventCode == NOTIFY_SINK_SETUP) { + HandleNotifySetUp(remoteDevId, eventContent); + return; + } + DHLOGE("invalid event."); +} + +void ScreenRegionManager::HandleNotifySetUp(const std::string &remoteDevId, const std::string &eventContent) +{ + DHLOGI("HandleNotifySetUp, remoteDevId: %s", GetAnonyString(remoteDevId).c_str()); + json eventContentJson = json::parse(eventContent, nullptr, false); + if (eventContentJson.is_discarded()) { + DHLOGE("HandleNotifySetUp, eventJsonContent is invalid."); + NotifyRemoteSourceSetUpResult(remoteDevId, "", ERR_DH_SCREEN_SA_SCREENREGION_SETUP_FAIL, ""); + return; + } + + if (!eventContentJson.contains(KEY_SCREEN_ID) || !eventContentJson.contains(KEY_DH_ID) || + !eventContentJson.contains(KEY_VIDEO_PARAM) || !eventContentJson.contains(KEY_MAPRELATION)) { + DHLOGE("HandleNotifySetUp, eventJsonContent is invalid."); + NotifyRemoteSourceSetUpResult(remoteDevId, "", ERR_DH_SCREEN_SA_SCREENREGION_SETUP_FAIL, ""); + return; + } + + uint64_t screenId = eventContentJson[KEY_SCREEN_ID]; + std::string dhId = eventContentJson[KEY_DH_ID]; + std::shared_ptr videoParam = + std::make_shared(eventContentJson[KEY_VIDEO_PARAM].get()); + std::shared_ptr mapRelation = + std::make_shared(eventContentJson[KEY_MAPRELATION].get()); + + uint64_t displayId = Rosen::DisplayManager::GetInstance().GetDefaultDisplayId(); + std::shared_ptr screenRegion = std::make_shared(remoteDevId, screenId, displayId); + screenRegion->SetVideoParam(videoParam); + screenRegion->SetMapRelation(mapRelation); + + int32_t ret = DH_SUCCESS; + { + std::lock_guard lock(screenRegionsMtx_); + if (screenRegions_.count(remoteDevId) != 0) { + ret = screenRegions_[remoteDevId]->Stop(); + } + + if (ret != DH_SUCCESS) { + DHLOGE("screenRegion stop failed, remoteDevId: %s, err: %d", + GetAnonyString(screenRegions_[remoteDevId]->GetRemoteDevId()).c_str(), ret); + NotifyRemoteSourceSetUpResult(remoteDevId, dhId, ERR_DH_SCREEN_SA_SCREENREGION_SETUP_FAIL, ""); + return; + } + screenRegions_[remoteDevId] = screenRegion; + } + + ret = screenRegion->SetUp(); + if (ret != DH_SUCCESS) { + DHLOGE("screen region start failed"); + NotifyRemoteSourceSetUpResult(remoteDevId, dhId, ERR_DH_SCREEN_SA_SCREENREGION_SETUP_FAIL, ""); + return; + } + + ret = screenRegion->Start(); + if (ret != DH_SUCCESS) { + DHLOGE("screen region start failed"); + NotifyRemoteSourceSetUpResult(remoteDevId, dhId, ERR_DH_SCREEN_SA_SCREENREGION_START_FAIL, ""); + return; + } + + NotifyRemoteSourceSetUpResult(remoteDevId, dhId, DH_SUCCESS, ""); +} + +void ScreenRegionManager::NotifyRemoteSourceSetUpResult(const std::string &remoteDevId, const std::string &dhId, + int32_t errCode, const std::string &errContent) +{ + DHLOGI("NotifyRemoteSourceSetUpResult, sourceDevId: %s, dhId: %s, errCode: %d", + GetAnonyString(remoteDevId).c_str(), GetAnonyString(dhId).c_str(), errCode); + int32_t eventCode = NOTIFY_SOURCE_SETUP_RESULT; + + json eventContentJson; + eventContentJson[KEY_DH_ID] = dhId; + eventContentJson[KEY_ERR_CODE] = errCode; + eventContentJson[KEY_ERR_CONTENT] = errContent; + + std::string eventContent = eventContentJson.dump(); + + NotifyRemoteScreenService(remoteDevId, dhId, eventCode, eventContent); +} + +int32_t ScreenRegionManager::NotifyRemoteScreenService(const std::string &remoteDevId, const std::string &dhId, + int32_t eventCode, const std::string &eventContent) +{ + DHLOGI("Notify remote source screen service, remote devId: %s, eventCode: %d", + GetAnonyString(remoteDevId).c_str(), eventCode); + sptr remoteSourceSA = GetDScreenSourceSA(remoteDevId); + if (!remoteSourceSA) { + DHLOGE("get remote source sa failed."); + return ERR_DH_SCREEN_SA_GET_REMOTE_SOURCE_SERVICE_FAIL; + } + std::string localDevId; + int32_t ret = GetLocalDeviceNetworkId(localDevId); + if (ret != DH_SUCCESS) { + DHLOGE("notify remote screen service failed, cannot get local device id"); + return ret; + } + remoteSourceSA->DScreenNotify(localDevId, eventCode, eventContent); + return DH_SUCCESS; +} + +sptr ScreenRegionManager::GetDScreenSourceSA(const std::string &devId) +{ + DHLOGI("GetDScreenSourceSA, devId: %s", GetAnonyString(devId).c_str()); + sptr samgr = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + DHLOGE("Failed to get system ability mgr."); + return nullptr; + } + auto remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, devId); + if (remoteObject == nullptr) { + DHLOGE("remoteObject is null"); + return nullptr; + } + + sptr remoteSourceSA = iface_cast(remoteObject); + if (remoteSourceSA == nullptr) { + DHLOGE("Failed to get remote dscreen source sa"); + return nullptr; + } + return remoteSourceSA; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screenservice/sourceservice/BUILD.gn b/services/screenservice/sourceservice/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..37777849cda7ceafa886e319683daee46fa1b3fe --- /dev/null +++ b/services/screenservice/sourceservice/BUILD.gn @@ -0,0 +1,83 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +ohos_shared_library("distributed_screen_source") { + include_dirs = [ + "//third_party/json/include", + "${windowmanager_path}/interfaces/innerkits/dm", + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "//foundation/graphic/standard/interfaces/innerkits/surface", + "${fwk_common_path}/utils/include", + ] + + include_dirs += [ + "./dscreenservice/include", + "./dscreenservice/include/callback", + "./dscreenmgr/include", + "${interfaces_path}/innerkits/native_cpp/screen_sink/include", + "${interfaces_path}/innerkits/native_cpp/screen_sink/include/callback", + "${interfaces_path}/innerkits/native_cpp/screen_source/include", + "${interfaces_path}/innerkits/native_cpp/screen_source/include/callback", + "${common_path}/include", + "${services_path}/common/utils/include", + "${services_path}/common/databuffer/include", + "${services_path}/common/screen_channel/include", + "${services_path}/screentransport/screensourceprocessor/include", + "${services_path}/screentransport/screensourceprocessor/encoder/include", + "${services_path}/screentransport/screensourcetrans/include", + ] + + sources = [ + "${services_path}/common/utils/src/dscreen_maprelation.cpp", + "${services_path}/common/utils/src/video_param.cpp", + "${interfaces_path}/innerkits/native_cpp/screen_sink/src/dscreen_sink_proxy.cpp", + "${interfaces_path}/innerkits/native_cpp/screen_source/src/dscreen_source_proxy.cpp", + "./dscreenservice/src/callback/dscreen_source_callback_proxy.cpp", + "./dscreenservice/src/dscreen_source_service.cpp", + "./dscreenservice/src/dscreen_source_stub.cpp", + "./dscreenmgr/src/dscreen.cpp", + "./dscreenmgr/src/dscreen_manager.cpp", + "./dscreenmgr/src/screen_manager_adapter.cpp", + ] + + deps = [ + "//utils/native/base:utils", + "${common_path}:distributed_screen_utils", + "${services_path}/screentransport/screensourcetrans:distributed_screen_sourcetrans", + "//foundation/graphic/standard/frameworks/surface:surface", + "${windowmanager_path}/interfaces/innerkits:wm_interface", + "${windowmanager_path}/dm:libdm", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dscreensource\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] + + subsystem_name = "distributedhardware" + + part_name = "distributed_screen" +} \ No newline at end of file diff --git a/services/screenservice/sourceservice/dscreenmgr/include/dscreen.h b/services/screenservice/sourceservice/dscreenmgr/include/dscreen.h new file mode 100644 index 0000000000000000000000000000000000000000..961ef1e3d36c834ebd99013e587a182d0cef4997 --- /dev/null +++ b/services/screenservice/sourceservice/dscreenmgr/include/dscreen.h @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_H +#define OHOS_DSCREEN_H + +#include +#include +#include +#include + +#include "dscreen_constants.h" +#include "iscreen_source_trans.h" +#include "iscreen_source_trans_callback.h" +#include "video_param.h" + +namespace OHOS { +namespace DistributedHardware { +class DScreen; +class IDScreenCallback { +public: + virtual ~IDScreenCallback() {}; + virtual void OnRegResult(const std::shared_ptr &dScreen, + const std::string &reqId, int32_t status, const std::string &data) = 0; + virtual void OnUnregResult(const std::shared_ptr &dScreen, + const std::string &reqId, int32_t status, const std::string &data) = 0; +}; + +class ScreenSourceTransCallback : public IScreenSourceTransCallback { +public: + ~ScreenSourceTransCallback() override {}; + virtual void OnTransError(int32_t err, const std::string &content) = 0; + virtual void OnError(int32_t err, const std::string &content) override + { + OnTransError(err, content); + } +}; + +class Task { +public: + Task(TaskType taskType, const std::string &taskId, const std::string &taskParam) : taskType_(taskType), + taskId_(taskId), taskParam_(taskParam) {}; + Task(TaskType taskType, const std::string &taskParam) : taskType_(taskType), + taskId_(""), taskParam_(taskParam) {}; + ~Task() {}; + + TaskType GetTaskType() + { + return taskType_; + }; + std::string GetTaskId() + { + return taskId_; + }; + std::string GetTaskParam() + { + return taskParam_; + }; + +private: + TaskType taskType_; + std::string taskId_; + std::string taskParam_; +}; + +class DScreen : public ScreenSourceTransCallback, public std::enable_shared_from_this { +public: + ~DScreen(); + DScreen(const std::string &devId, const std::string &dhId, std::shared_ptr dscreenCallback); + void OnTransError(int32_t err, const std::string &content) override; + + void SetVideoParam(std::shared_ptr &videoParam); + std::shared_ptr GetVideoParam(); + void SetState(DScreenState state); + DScreenState GetState() const; + uint64_t GetScreenId() const; + std::string GetDHId() const; + std::string GetDevId() const; + int32_t AddTask(const std::shared_ptr &task); + +private: + void TaskThreadLoop(); + void HandleTask(const std::shared_ptr &task); + void HandleEnable(const std::string ¶m, const std::string &taskId); + void HandleDisable(const std::string &taskId); + void HandleConnect(); + void HandleDisconnect(); + int32_t SetUp(); + int32_t Start(); + int32_t Stop(); + + static constexpr uint8_t TASK_WAIT_SECONDS = 1; + + std::string devId_; + std::string dhId_; + uint64_t screenId_ = SCREEN_ID_INVALID; + std::shared_ptr videoParam_ = nullptr; + std::shared_ptr sourceTrans_ = nullptr; + std::shared_ptr dscreenCallback_ = nullptr; + + DScreenState curState_; + std::mutex stateMtx_; + std::thread taskQueueThread_; + std::condition_variable taskQueueCond_; + std::mutex taskQueueMtx_; + std::queue> taskQueue_; + bool taskThreadRunning_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screenservice/sourceservice/dscreenmgr/include/dscreen_manager.h b/services/screenservice/sourceservice/dscreenmgr/include/dscreen_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..a32ec75c76505e79060cd322037e4c8b888c40a3 --- /dev/null +++ b/services/screenservice/sourceservice/dscreenmgr/include/dscreen_manager.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_MGR_H +#define OHOS_DSCREEN_MGR_H + +#include "dm_common.h" +#include "single_instance.h" +#include "screen_manager.h" + +#include "dscreen.h" +#include "dscreen_maprelation.h" +#include "idscreen_sink.h" +#include "idscreen_source_callback.h" +#include "video_param.h" + +namespace OHOS { +namespace DistributedHardware { +class DScreenCallback : public IDScreenCallback { +public: + void OnRegResult(const std::shared_ptr &dScreen, const std::string &reqId, + int32_t status, const std::string &data) override; + void OnUnregResult(const std::shared_ptr &dScreen, const std::string &reqId, + int32_t status, const std::string &data) override; +}; + +class DScreenManager : public Rosen::ScreenManager::IScreenGroupListener, + public std::enable_shared_from_this { +DECLARE_SINGLE_INSTANCE_BASE(DScreenManager); +public: + void OnChange(const std::vector &screenIds, Rosen::ScreenGroupChangeEvent event) override; + + void OnRegResult(const std::shared_ptr &dScreen, const std::string &reqId, + int32_t status, const std::string &data); + void OnUnregResult(const std::shared_ptr &dScreen, const std::string &reqId, + int32_t status, const std::string &data); + + int32_t Init(); + int32_t UnInit(); + int32_t EnableDistributedScreen(const std::string &devId, const std::string &dhId, const std::string ¶m, + const std::string &reqId); + int32_t DisableDistributedScreen(const std::string &devId, const std::string &dhId, const std::string &reqId); + void HandleDScreenNotify(const std::string &devId, int32_t eventCode, const std::string &eventContent); + void RegisterDScreenCallback(const sptr &callback); + +private: + ~DScreenManager(); + DScreenManager(); + std::mutex dScreenMapMtx_; + std::mutex dScreenMapRelationMtx_; + std::map> mapRelations_; + std::map> dScreens_; + sptr dScreenSourceCallbackProxy_ = nullptr; + std::shared_ptr dScreenCallback_ = nullptr; + + void HandleScreenChange(const std::shared_ptr &changedScreen, Rosen::ScreenGroupChangeEvent event); + std::shared_ptr FindDScreenByScreenId(uint64_t screenId); + sptr GetDScreenSinkSA(const std::string &devId); + int32_t NotifyRemoteScreenService(const std::string &devId, int32_t eventCode, const std::string &eventContent); + void NotifyRemoteSinkSetUp(const std::shared_ptr &dScreen); + void HandleNotifySetUpResult(const std::string &remoteDevId, const std::string &eventContent); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screenservice/sourceservice/dscreenmgr/include/screen_manager_adapter.h b/services/screenservice/sourceservice/dscreenmgr/include/screen_manager_adapter.h new file mode 100644 index 0000000000000000000000000000000000000000..836c4edd53597f36a0adbacfbe8b337649da82cf --- /dev/null +++ b/services/screenservice/sourceservice/dscreenmgr/include/screen_manager_adapter.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SCREEN_MGR_ADAPTER_H +#define OHOS_SCREEN_MGR_ADAPTER_H + +#include + +#include "single_instance.h" +#include "screen_manager.h" + +#include "dscreen_maprelation.h" +#include "video_param.h" + +namespace OHOS { +namespace DistributedHardware { +class ScreenMgrAdapter { +DECLARE_SINGLE_INSTANCE_BASE(ScreenMgrAdapter); + +public: + uint64_t CreateVirtualScreen(const std::string &devId, const std::string &dhId, + const std::shared_ptr &videoParam); + int32_t RegisterScreenGroupListener(sptr listener); + int32_t UnregisterScreenGroupListener(sptr listener); + int32_t RemoveVirtualScreen(uint64_t screenId); + int32_t SetImageSurface(uint64_t screenId, sptr surface); + std::shared_ptr GetMapRelation(uint64_t screenId); + void RemoveScreenFromGroup(uint64_t screenId); + +private: + ScreenMgrAdapter() = default; + ~ScreenMgrAdapter(); + + bool listenerRegistered = false; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screenservice/sourceservice/dscreenmgr/src/dscreen.cpp b/services/screenservice/sourceservice/dscreenmgr/src/dscreen.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2986ad13990f3fa443fff56a32684bd87fa46417 --- /dev/null +++ b/services/screenservice/sourceservice/dscreenmgr/src/dscreen.cpp @@ -0,0 +1,348 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "dscreen_util.h" +#include "screen_manager_adapter.h" +#include "screen_source_trans.h" + +namespace OHOS { +namespace DistributedHardware { +DScreen::DScreen(const std::string &devId, const std::string &dhId, + std::shared_ptr dscreenCallback) +{ + DHLOGD("DScreen construct, devId: %s, dhId: %s", GetAnonyString(devId).c_str(), + GetAnonyString(dhId).c_str()); + devId_ = devId; + dhId_ = dhId; + dscreenCallback_ = dscreenCallback; + SetState(DISABLED); + taskThreadRunning_ = true; + taskQueueThread_ = std::thread(&DScreen::TaskThreadLoop, this); +} + +DScreen::~DScreen() +{ + DHLOGD("DScreen deconstruct, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(), + GetAnonyString(dhId_).c_str()); + taskThreadRunning_ = false; + taskQueueCond_.notify_all(); + if (taskQueueThread_.joinable()) { + taskQueueThread_.join(); + } + int32_t ret = DH_SUCCESS; + if (sourceTrans_) { + ret = sourceTrans_->Release(); + } + if (ret != DH_SUCCESS) { + DHLOGE("source trans release failed. ret: %d", ret); + } + + if (screenId_ != SCREEN_ID_INVALID) { + ret = ScreenMgrAdapter::GetInstance().RemoveVirtualScreen(screenId_); + } + + if (ret != DH_SUCCESS) { + DHLOGE("remove virtual screen failed."); + } + videoParam_ = nullptr; + sourceTrans_ = nullptr; + DHLOGD("DScreen deconstruct end."); +} + +void DScreen::OnTransError(int32_t err, const std::string &content) +{ + DHLOGD("OnTransError, err: %d", err); + AddTask(std::make_shared(TaskType::TASK_DISCONNECT, "")); + ScreenMgrAdapter::GetInstance().RemoveScreenFromGroup(screenId_); +} + +void DScreen::SetVideoParam(std::shared_ptr &videoParam) +{ + videoParam_ = videoParam; +} + +std::shared_ptr DScreen::GetVideoParam() +{ + return videoParam_; +} + +void DScreen::SetState(DScreenState state) +{ + std::lock_guard lock(stateMtx_); + curState_ = state; +} + +DScreenState DScreen::GetState() const +{ + return curState_; +} + +uint64_t DScreen::GetScreenId() const +{ + return screenId_; +} + +std::string DScreen::GetDHId() const +{ + return dhId_; +} + +std::string DScreen::GetDevId() const +{ + return devId_; +} + +int32_t DScreen::AddTask(const std::shared_ptr &task) +{ + DHLOGI("DScreen::AddTask, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(), + GetAnonyString(dhId_).c_str()); + if (!task) { + DHLOGE("AddTask, task is invalid."); + return ERR_DH_SCREEN_SA_DSCREEN_TASK_NOT_VALID; + } + DHLOGI("AddTask, task type: %d", task->GetTaskType()); + { + std::lock_guard lock(taskQueueMtx_); + taskQueue_.push(task); + } + taskQueueCond_.notify_all(); + return DH_SUCCESS; +} + +void DScreen::TaskThreadLoop() +{ + DHLOGI("DScreen taskThread start. devId: %s, dhId: %s", GetAnonyString(devId_).c_str(), + GetAnonyString(dhId_).c_str()); + while (taskThreadRunning_) { + std::shared_ptr task; + { + std::unique_lock lock(taskQueueMtx_); + taskQueueCond_.wait_for(lock, std::chrono::seconds(TASK_WAIT_SECONDS), + [this]() { return !taskQueue_.empty(); }); + if (taskQueue_.empty()) { + continue; + } + task = taskQueue_.front(); + taskQueue_.pop(); + } + + if (task == nullptr) { + DHLOGD("task is null."); + continue; + } + + DHLOGD("run task, task queue size: %zu", taskQueue_.size()); + HandleTask(task); + } +} + +void DScreen::HandleTask(const std::shared_ptr &task) +{ + int32_t taskType = task->GetTaskType(); + DHLOGI("HandleTask, devId: %s, dhId: %s, task type: %d", GetAnonyString(devId_).c_str(), + GetAnonyString(dhId_).c_str(), taskType); + switch (taskType) { + case TaskType::TASK_ENABLE: + HandleEnable(task->GetTaskParam(), task->GetTaskId()); + break; + case TaskType::TASK_DISABLE: + HandleDisable(task->GetTaskId()); + break; + case TaskType::TASK_CONNECT: + HandleConnect(); + break; + case TaskType::TASK_DISCONNECT: + HandleDisconnect(); + break; + default: + DHLOGD("task type unkown."); + } +} + +void DScreen::HandleEnable(const std::string ¶m, const std::string &taskId) +{ + DHLOGI("HandleEnable, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str()); + if (curState_ == ENABLED || curState_ == ENABLING || + curState_ == CONNECTING || curState_ == CONNECTED) { + dscreenCallback_->OnRegResult(shared_from_this(), taskId, DH_SUCCESS, ""); + return; + } + + SetState(ENABLING); + if (!videoParam_) { + videoParam_ = std::make_shared(); + } + + json attrJson = json::parse(param, nullptr, false); + if (attrJson.is_discarded()) { + DHLOGE("enable param json is invalid."); + dscreenCallback_->OnRegResult(shared_from_this(), taskId, ERR_DH_SCREEN_SA_ENABLE_FAILED, + "enable param json is invalid."); + return; + } + + if (!attrJson.contains(KEY_SCREEN_WIDTH) || + !attrJson.contains(KEY_SCREEN_HEIGHT) || + !attrJson.contains(KEY_CODECTYPE)) { + DHLOGE("enable param is invalid."); + dscreenCallback_->OnRegResult(shared_from_this(), taskId, ERR_DH_SCREEN_SA_ENABLE_FAILED, + "enable param is invalid."); + return; + } + + videoParam_->SetScreenWidth(attrJson[KEY_SCREEN_WIDTH]); + videoParam_->SetScreenHeight(attrJson[KEY_SCREEN_HEIGHT]); + videoParam_->SetVideoFormat(VIDEO_DATA_FORMAT_NV21); + videoParam_->SetCodecType(VIDEO_CODEC_TYPE_VIDEO_H264); + + uint64_t screenId = ScreenMgrAdapter::GetInstance().CreateVirtualScreen(devId_, dhId_, videoParam_); + if (screenId == SCREEN_ID_INVALID) { + DHLOGE("create virtual screen failed."); + dscreenCallback_->OnRegResult(shared_from_this(), taskId, ERR_DH_SCREEN_SA_ENABLE_FAILED, + "create virtual screen failed."); + } + screenId_ = screenId; + SetState(ENABLED); + dscreenCallback_->OnRegResult(shared_from_this(), taskId, DH_SUCCESS, ""); +} + +void DScreen::HandleDisable(const std::string &taskId) +{ + DHLOGI("HandleDisable, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str()); + SetState(DISABLING); + Stop(); + int32_t ret = ScreenMgrAdapter::GetInstance().RemoveVirtualScreen(screenId_); + if (ret != DH_SUCCESS) { + DHLOGE("remove virtual screen failed."); + dscreenCallback_->OnUnregResult(shared_from_this(), taskId, ERR_DH_SCREEN_SA_DISABLE_FAILED, + "remove virtual screen failed."); + return; + } + SetState(DISABLED); + dscreenCallback_->OnUnregResult(shared_from_this(), taskId, DH_SUCCESS, ""); +} + +void DScreen::HandleConnect() +{ + DHLOGI("HandleConnect, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(), + GetAnonyString(dhId_).c_str()); + + int32_t ret = SetUp(); + if (ret != DH_SUCCESS) { + SetState(ENABLED); + ScreenMgrAdapter::GetInstance().RemoveScreenFromGroup(screenId_); + DHLOGE("dScreen SetUp failed."); + return; + } + + ret = Start(); + if (ret != DH_SUCCESS) { + SetState(ENABLED); + ScreenMgrAdapter::GetInstance().RemoveScreenFromGroup(screenId_); + DHLOGE("dScreen Start failed."); + return; + } + SetState(CONNECTED); +} + +void DScreen::HandleDisconnect() +{ + DHLOGD("HandleDisconnect, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(), + GetAnonyString(dhId_).c_str()); + if (curState_ != CONNECTED) { + DHLOGE("dscreen is not connected, cannot disconnect"); + return; + } + SetState(DISCONNECTING); + int32_t ret = Stop(); + if (ret != DH_SUCCESS) { + SetState(CONNECTED); + DHLOGE("dScreen Stop failed."); + return; + } + SetState(ENABLED); +} + +int32_t DScreen::SetUp() +{ + DHLOGD("SetUp, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(), + GetAnonyString(dhId_).c_str()); + + if (!sourceTrans_) { + sourceTrans_ = std::make_shared(); + } + + sourceTrans_->RegisterStateCallback(shared_from_this()); + int32_t ret = sourceTrans_->SetUp(*videoParam_, *videoParam_, devId_); + if (ret != DH_SUCCESS) { + DHLOGE("source trans SetUp failed."); + return ret; + } + + sptr surface = sourceTrans_->GetImageSurface(); + + ScreenMgrAdapter::GetInstance().SetImageSurface(screenId_, surface); + DHLOGI("DScreen SetUp success."); + return DH_SUCCESS; +} + +int32_t DScreen::Start() +{ + DHLOGD("Start, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(), + GetAnonyString(dhId_).c_str()); + if (!sourceTrans_) { + DHLOGE("source trans not init."); + return ERR_DH_SCREEN_SA_SOURCETRANS_NOT_INIT; + } + + int32_t ret = sourceTrans_->Start(); + if (ret != DH_SUCCESS) { + DHLOGE("source trans start failed."); + return ret; + } + return DH_SUCCESS; +} + +int32_t DScreen::Stop() +{ + DHLOGD("Stop, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(), + GetAnonyString(dhId_).c_str()); + if (!sourceTrans_) { + DHLOGE("source trans not init."); + return ERR_DH_SCREEN_SA_SOURCETRANS_NOT_INIT; + } + + int32_t ret = sourceTrans_->Stop(); + if (ret != DH_SUCCESS) { + DHLOGE("source trans stop failed."); + return ret; + } + + ret = sourceTrans_->Release(); + if (ret != DH_SUCCESS) { + DHLOGE("source trans release failed."); + return ret; + } + + sourceTrans_ = nullptr; + return DH_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screenservice/sourceservice/dscreenmgr/src/dscreen_manager.cpp b/services/screenservice/sourceservice/dscreenmgr/src/dscreen_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d7c308b045b1d8150ccbe0a294a31f242d3c40b --- /dev/null +++ b/services/screenservice/sourceservice/dscreenmgr/src/dscreen_manager.cpp @@ -0,0 +1,400 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_manager.h" + +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "nlohmann/json.hpp" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "dscreen_util.h" +#include "idscreen_sink.h" +#include "screen_manager_adapter.h" + +using json = nlohmann::json; + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(DScreenManager); +DScreenManager::DScreenManager() +{ + DHLOGI("DScreenMgr construct."); +} + +DScreenManager::~DScreenManager() +{ + DHLOGI("DScreenMgr deConstruct."); +} + +int32_t DScreenManager::Init() +{ + DHLOGI("DScreenManager::Init"); + int32_t ret = ScreenMgrAdapter::GetInstance().RegisterScreenGroupListener(this); + if (ret != DH_SUCCESS) { + DHLOGE("DScreenManager Init failed, err: %d", ret); + } + if (!dScreenCallback_) { + dScreenCallback_ = std::make_shared(); + } + return ret; +} + +int32_t DScreenManager::UnInit() +{ + DHLOGI("DScreenManager::UnInit"); + int32_t ret = ScreenMgrAdapter::GetInstance().UnregisterScreenGroupListener(this); + if (ret != DH_SUCCESS) { + DHLOGE("DScreenManager UnInit failed, err: %d", ret); + } + dScreenCallback_ = nullptr; + + { + std::lock_guard lock(dScreenMapMtx_); + dScreens_.clear(); + } + + { + std::lock_guard lock(dScreenMapRelationMtx_); + mapRelations_.clear(); + } + DHLOGI("DScreenManager::UnInit success"); + return ret; +} + +void DScreenManager::OnChange(const std::vector &screenIds, Rosen::ScreenGroupChangeEvent event) +{ + DHLOGI("On Screen change, screenIds size: %d", screenIds.size()); + for (uint64_t screenId : screenIds) { + std::shared_ptr changedScreen = nullptr; + changedScreen = FindDScreenByScreenId(screenId); + if (!changedScreen) { + DHLOGD("screen change not about remote screen, screenId: %ulld", screenId); + continue; + } + HandleScreenChange(changedScreen, event); + } +} + +void DScreenManager::HandleScreenChange(const std::shared_ptr &changedScreen, + Rosen::ScreenGroupChangeEvent event) +{ + if (!changedScreen) { + DHLOGE("DScreenManager::HandleScreenChange, dScreen is null."); + return; + } + uint64_t screenId = changedScreen->GetScreenId(); + DHLOGI("DScreenManager::HandleScreenChange, screenId: %ulld, changeEvent: %", screenId, event); + if (event == Rosen::ScreenGroupChangeEvent::ADD_TO_GROUP) { + if (changedScreen->GetState() == CONNECTING) { + DHLOGD("screen is connecting, no need handle change"); + return; + } + std::shared_ptr mapRelation = + ScreenMgrAdapter::GetInstance().GetMapRelation(screenId); + if (!mapRelation) { + DHLOGE("mapRelation construct failed. screenId: %ulld", screenId); + return; + } + + std::shared_ptr videoParam = changedScreen->GetVideoParam(); + DisplayRect displayRect = mapRelation->GetDisplayRect(); + videoParam->SetVideoWidth(displayRect.width); + videoParam->SetVideoHeight(displayRect.height); + changedScreen->SetState(CONNECTING); + + { + std::lock_guard lock(dScreenMapRelationMtx_); + mapRelations_[screenId] = mapRelation; + } + NotifyRemoteSinkSetUp(changedScreen); + } else if (event == Rosen::ScreenGroupChangeEvent::REMOVE_FROM_GROUP) { + if (changedScreen->GetState() == DISCONNECTING) { + DHLOGD("screen is disconnecting, no need handle change"); + return; + } + std::shared_ptr mapRelation = nullptr; + { + std::lock_guard lock(dScreenMapRelationMtx_); + if (mapRelations_.count(screenId) == 0) { + DHLOGE("destroyed relation not found."); + return; + } + mapRelation = mapRelations_[screenId]; + mapRelations_.erase(screenId); + } + changedScreen->AddTask(std::make_shared(TaskType::TASK_DISCONNECT, "")); + } else if (event == Rosen::ScreenGroupChangeEvent::CHANGE_GROUP) { + DHLOGE("CHANGE_GROUP not implement."); + } else { + DHLOGE("unknown change type."); + } +} + +void DScreenCallback::OnRegResult(const std::shared_ptr &dScreen, + const std::string &reqId, int32_t status, const std::string &data) +{ + DHLOGI("DScreenCallback::OnRegResult, devId: %s, dhId: %s, reqId: %s", + GetAnonyString(dScreen->GetDevId()).c_str(), GetAnonyString(dScreen->GetDHId()).c_str(), reqId.c_str()); + DScreenManager::GetInstance().OnRegResult(dScreen, reqId, status, data); +} + +void DScreenCallback::OnUnregResult(const std::shared_ptr &dScreen, + const std::string &reqId, int32_t status, const std::string &data) +{ + DHLOGI("DScreenCallback::OnUnregResult, devId: %s, dhId: %s, reqId: %s", + GetAnonyString(dScreen->GetDevId()).c_str(), GetAnonyString(dScreen->GetDHId()).c_str(), reqId.c_str()); + DScreenManager::GetInstance().OnUnregResult(dScreen, reqId, status, data); +} + +void DScreenManager::OnRegResult(const std::shared_ptr &dScreen, + const std::string &reqId, int32_t status, const std::string &data) +{ + DHLOGI("DScreenManager::OnRegResult, devId: %s, dhId: %s, reqId: %s", + GetAnonyString(dScreen->GetDevId()).c_str(), GetAnonyString(dScreen->GetDHId()).c_str(), reqId.c_str()); + if (!dScreenSourceCallbackProxy_) { + DHLOGE("dScreenSourceCallbackProxy is null"); + return; + } + dScreenSourceCallbackProxy_->OnNotifyRegResult(dScreen->GetDevId(), dScreen->GetDHId(), reqId, status, data); +} + +void DScreenManager::OnUnregResult(const std::shared_ptr &dScreen, + const std::string &reqId, int32_t status, const std::string &data) +{ + DHLOGI("DScreenManager::OnUnregResult, devId: %s, dhId: %s, reqId: %s", + GetAnonyString(dScreen->GetDevId()).c_str(), GetAnonyString(dScreen->GetDHId()).c_str(), reqId.c_str()); + if (!dScreenSourceCallbackProxy_) { + DHLOGE("dScreenSourceCallbackProxy is null"); + return; + } + dScreenSourceCallbackProxy_->OnNotifyUnregResult(dScreen->GetDevId(), dScreen->GetDHId(), reqId, status, data); +} + +int32_t DScreenManager::EnableDistributedScreen(const std::string &devId, const std::string &dhId, + const std::string &attrs, const std::string &reqId) +{ + DHLOGI("EnableDistributedScreen, devId: %s, dhId:%s", + GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); + std::string dScreenIdx = devId + SEPERATOR + dhId; + std::shared_ptr dScreen = nullptr; + if (dScreens_.count(dScreenIdx) != 0) { + dScreen = dScreens_[dScreenIdx]; + } + + if (dScreen == nullptr) { + dScreen = std::make_shared(devId, dhId, dScreenCallback_); + } + + int32_t dScreenState = dScreen->GetState(); + if (dScreenState != DISABLED && dScreenState != DISABLING) { + DHLOGE("dScreen state is invalid."); + return ERR_DH_SCREEN_SA_ENABLE_FAILED; + } + + dScreens_[dScreenIdx] = dScreen; + int32_t ret = dScreen->AddTask(std::make_shared(TaskType::TASK_ENABLE, reqId, attrs)); + if (ret != DH_SUCCESS) { + DHLOGE("EnableDistributedScreen, add task failed. devId: %s, dhId:%s", + GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); + } + return ret; +} + +int32_t DScreenManager::DisableDistributedScreen(const std::string &devId, const std::string &dhId, + const std::string &reqId) +{ + DHLOGI("DisableDistributedScreen, devId: %s, dhId:%s", + GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); + + std::string dScreenIdx = devId + SEPERATOR + dhId; + if (dScreens_.count(dScreenIdx) == 0) { + DHLOGE("dscreen not found, devId: %s, dhId: %s", + GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); + return ERR_DH_SCREEN_SA_DISABLE_FAILED; + } + + int32_t dScreenState = dScreens_[dScreenIdx]->GetState(); + int32_t ret = DH_SUCCESS; + switch (dScreenState) { + case DISABLED: + case DISABLING: + DHLOGE("dScreen state is invalid."); + ret = ERR_DH_SCREEN_SA_DISABLE_FAILED; + break; + case ENABLED: + case ENABLING: + ret = dScreens_[dScreenIdx]->AddTask(std::make_shared(TaskType::TASK_DISABLE, reqId, "")); + break; + case CONNECTING: + case CONNECTED: + case DISCONNECTING: + ret = dScreens_[dScreenIdx]->AddTask(std::make_shared(TaskType::TASK_DISCONNECT, "")); + if (ret == DH_SUCCESS) { + ret = dScreens_[dScreenIdx]->AddTask(std::make_shared(TaskType::TASK_DISABLE, reqId, "")); + } + break; + default: + ret = ERR_DH_SCREEN_SA_DISABLE_FAILED; + break; + } + return ret; +} + +void DScreenManager::RegisterDScreenCallback(const sptr &callback) +{ + DHLOGI("RegisterDScreenCallback"); + dScreenSourceCallbackProxy_ = callback; +} + +std::shared_ptr DScreenManager::FindDScreenByScreenId(uint64_t screenId) +{ + DHLOGD("FindDScreenByScreenId, screenId: %ulld", screenId); + std::lock_guard lock(dScreenMapMtx_); + for (const auto &iter : dScreens_) { + std::shared_ptr dScreen = iter.second; + if (!dScreen) { + continue; + } + + if (dScreen->GetScreenId() == screenId) { + return dScreen; + } + } + DHLOGD("DScreen not found, screenId: %ulld", screenId); + return nullptr; +} + +void DScreenManager::HandleDScreenNotify(const std::string &devId, int32_t eventCode, + const std::string &eventContent) +{ + DHLOGI("HandleDScreenNotify, devId: %s, eventCode: %d", GetAnonyString(devId).c_str(), eventCode); + if (eventCode == NOTIFY_SOURCE_SETUP_RESULT) { + HandleNotifySetUpResult(devId, eventContent); + return; + } + + DHLOGE("invalid eventCode, eventCode: %d", eventCode); +} + +int32_t DScreenManager::NotifyRemoteScreenService(const std::string &devId, int32_t eventCode, + const std::string &eventContent) +{ + DHLOGI("Notify remote sink screen service, remote devId: %s, eventCode: %d", + GetAnonyString(devId).c_str(), eventCode); + sptr remoteSinkSA = GetDScreenSinkSA(devId); + if (!remoteSinkSA) { + DHLOGE("get remote sink sa failed."); + return ERR_DH_SCREEN_SA_GET_REMOTE_SINK_SERVICE_FAIL; + } + std::string localDevId; + int32_t ret = GetLocalDeviceNetworkId(localDevId); + if (ret != DH_SUCCESS) { + DHLOGE("notify remote screen service failed, cannot get local device id"); + return ret; + } + remoteSinkSA->DScreenNotify(localDevId, eventCode, eventContent); + return DH_SUCCESS; +} + +sptr DScreenManager::GetDScreenSinkSA(const std::string &devId) +{ + DHLOGI("GetDScreenSinkSA, devId: %s", GetAnonyString(devId).c_str()); + sptr samgr = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!samgr) { + DHLOGE("Failed to get system ability mgr."); + return nullptr; + } + auto remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_SCREEN_SINK_SA_ID, devId); + if (remoteObject == nullptr) { + DHLOGE("remoteObject is null"); + return nullptr; + } + + sptr remoteSinkSA = iface_cast(remoteObject); + if (remoteSinkSA == nullptr) { + DHLOGE("Failed to get remote dscreen sink sa"); + return nullptr; + } + return remoteSinkSA; +} + +void DScreenManager::NotifyRemoteSinkSetUp(const std::shared_ptr &dScreen) +{ + DHLOGI("NotifyRemoteSinkSetUp"); + int32_t eventCode = NOTIFY_SINK_SETUP; + std::string devId = dScreen->GetDevId(); + json eventContentJson; + eventContentJson[KEY_SCREEN_ID] = dScreen->GetScreenId(); + eventContentJson[KEY_DH_ID] = dScreen->GetDHId(); + if (dScreen->GetVideoParam() == nullptr) { + DHLOGE("videoParam is null, back to enabled state screedId: %ulld", dScreen->GetScreenId()); + dScreen->SetState(ENABLED); + return; + } + eventContentJson[KEY_VIDEO_PARAM] = *(dScreen->GetVideoParam()); + + if (mapRelations_.count(dScreen->GetScreenId()) == 0) { + DHLOGE("mapRelation not found, back to enabled state screedId: %ulld", dScreen->GetScreenId()); + dScreen->SetState(ENABLED); + return; + } + eventContentJson[KEY_MAPRELATION] = *(mapRelations_[dScreen->GetScreenId()]); + + std::string eventContent = eventContentJson.dump(); + DHLOGD("start notify remote screen, eventContent: %s", eventContent.c_str()); + NotifyRemoteScreenService(devId, eventCode, eventContent); +} + +void DScreenManager::HandleNotifySetUpResult(const std::string &remoteDevId, const std::string &eventContent) +{ + DHLOGI("HandleNotifySetUpResult, remoteDevId:%s", GetAnonyString(remoteDevId).c_str()); + json eventContentJson = json::parse(eventContent, nullptr, false); + if (eventContentJson.is_discarded()) { + DHLOGE("HandleNotifySetUpResult, eventContent is invalid"); + return; + } + + if (!eventContentJson.contains(KEY_DH_ID) || + !eventContentJson.contains(KEY_ERR_CODE) || + !eventContentJson.contains(KEY_ERR_CONTENT)) { + DHLOGE("HandleNotifySetUpResult, eventContent is invalid"); + return; + } + + std::string dhId = eventContentJson[KEY_DH_ID]; + int32_t errCode = eventContentJson[KEY_ERR_CODE]; + std::string errContent = eventContentJson[KEY_ERR_CONTENT]; + + std::string dScreenIdx = remoteDevId + SEPERATOR + dhId; + if (dScreens_.count(dScreenIdx) == 0) { + DHLOGE("dScreen not found, remoteDevId:%s, dhId:%s", + GetAnonyString(remoteDevId).c_str(), GetAnonyString(dhId).c_str()); + return; + } + + if (errCode != DH_SUCCESS) { + DHLOGE("remote sink set up failed, errCode: %d, reason: %s", errCode, errContent.c_str()); + dScreens_[dScreenIdx]->SetState(ENABLED); + return; + } + + dScreens_[dScreenIdx]->AddTask(std::make_shared(TaskType::TASK_CONNECT, "")); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screenservice/sourceservice/dscreenmgr/src/screen_manager_adapter.cpp b/services/screenservice/sourceservice/dscreenmgr/src/screen_manager_adapter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f0850bfb97257fef19a0b4a726e083042500a05 --- /dev/null +++ b/services/screenservice/sourceservice/dscreenmgr/src/screen_manager_adapter.cpp @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screen_manager_adapter.h" + +#include + +#include "display_manager.h" +#include "dm_common.h" +#include "screen.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(ScreenMgrAdapter); +ScreenMgrAdapter::~ScreenMgrAdapter() +{ + DHLOGI("~ScreenMgrAdapter"); +} + +uint64_t ScreenMgrAdapter::CreateVirtualScreen(const std::string &devId, const std::string &dhId, + const std::shared_ptr &videoParam) +{ + DHLOGI("CreateVirtualScreen, width: %u, height: %u", videoParam->GetScreenWidth(), + videoParam->GetScreenHeight()); + std::string screenName = DSCREEN_PREFIX + SEPERATOR + devId + SEPERATOR + dhId; + Rosen::VirtualScreenOption option = { + screenName, + videoParam->GetScreenWidth(), + videoParam->GetScreenHeight(), + DEFAULT_DENSITY, + nullptr, + DEFAULT_SCREEN_FLAGS, + false + }; + + uint64_t screenId = Rosen::ScreenManager::GetInstance().CreateVirtualScreen(option); + return screenId; +} + +int32_t ScreenMgrAdapter::RegisterScreenGroupListener(sptr listener) +{ + DHLOGI("RegisterScreenGroupListener"); + if (listenerRegistered) { + DHLOGI("already registered listener."); + return DH_SUCCESS; + } + bool ret = Rosen::ScreenManager::GetInstance().RegisterScreenGroupListener(listener); + if (!ret) { + DHLOGE("RegisterScreenGroupListener Failed."); + return ERR_DH_SCREEN_SA_REGISTER_SCREENLISTENER_FAIL; + } + listenerRegistered = true; + return DH_SUCCESS; +} + +int32_t ScreenMgrAdapter::UnregisterScreenGroupListener(sptr listener) +{ + DHLOGI("UnregisterScreenGroupListener"); + if (!listenerRegistered) { + DHLOGI("listener already unregistered."); + return DH_SUCCESS; + } + bool ret = Rosen::ScreenManager::GetInstance().UnregisterScreenGroupListener(listener); + if (!ret) { + DHLOGE("UnregisterScreenGroupListener Failed."); + return ERR_DH_SCREEN_SA_UNREGISTER_SCREENLISTENER_FAIL; + } + listenerRegistered = false; + return DH_SUCCESS; +} + +void ScreenMgrAdapter::RemoveScreenFromGroup(uint64_t screenId) +{ + DHLOGI("remove screen from group, screenId: %ulld", screenId); + std::vector screenIds; + screenIds.push_back(screenId); + Rosen::ScreenManager::GetInstance().RemoveVirtualScreenFromGroup(screenIds); +} + +int32_t ScreenMgrAdapter::RemoveVirtualScreen(uint64_t screenId) +{ + DHLOGI("remove virtual screen"); + Rosen::DMError err = Rosen::ScreenManager::GetInstance().DestroyVirtualScreen(screenId); + if (err != Rosen::DMError::DM_OK) { + DHLOGE("remove virtual screen failed, screenId:%ulld", screenId); + return ERR_DH_SCREEN_SA_REMOVE_VIRTUALSCREEN_FAIL; + } + return DH_SUCCESS; +} + +int32_t ScreenMgrAdapter::SetImageSurface(uint64_t screenId, sptr surface) +{ + DHLOGI("SetImageSurface for virtualscreen, screenId: %ulld", screenId); + Rosen::ScreenManager::GetInstance().SetVirtualScreenSurface(screenId, surface); + return DH_SUCCESS; +} + +std::shared_ptr ScreenMgrAdapter::GetMapRelation(uint64_t screenId) +{ + DHLOGI("GetMapRelation"); + std::shared_ptr mapRelation = std::make_shared(); + sptr screen = Rosen::ScreenManager::GetInstance().GetScreenById(screenId); + sptr display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay(); + mapRelation->SetDisplayId(display->GetId()); + mapRelation->SetScreenId(screenId); + + ScreenRect screenRect = {0, 0, screen->GetWidth(), screen->GetHeight()}; + DisplayRect displayRect = {0, 0, display->GetWidth(), display->GetHeight()}; + mapRelation->SetDisplayRect(displayRect); + mapRelation->SetScreenRect(screenRect); + return mapRelation; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screenservice/sourceservice/dscreenservice/include/callback/dscreen_source_callback_proxy.h b/services/screenservice/sourceservice/dscreenservice/include/callback/dscreen_source_callback_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..aec646c2fbed9168145bd1164298c1730e4cdf0e --- /dev/null +++ b/services/screenservice/sourceservice/dscreenservice/include/callback/dscreen_source_callback_proxy.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_SOURCE_CALLBACK_PROXY_H +#define OHOS_DSCREEN_SOURCE_CALLBACK_PROXY_H + +#include "iremote_proxy.h" + +#include "idscreen_source_callback.h" + +namespace OHOS { +namespace DistributedHardware { +class DScreenSourceCallbackProxy : public IRemoteProxy { +public: + explicit DScreenSourceCallbackProxy(const sptr impl) + : IRemoteProxy(impl) + { + } + + ~DScreenSourceCallbackProxy() {} + int32_t OnNotifyRegResult(const std::string &devId, const std::string &dhId, const std::string &reqId, + int32_t status, const std::string &resultData) override; + int32_t OnNotifyUnregResult(const std::string &devId, const std::string &dhId, const std::string &reqId, + int32_t status, const std::string &resultData) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/screenservice/sourceservice/dscreenservice/include/dscreen_source_service.h b/services/screenservice/sourceservice/dscreenservice/include/dscreen_source_service.h new file mode 100644 index 0000000000000000000000000000000000000000..b2731fb9eacdc5a4f9c2b842a219776d1f72c5e0 --- /dev/null +++ b/services/screenservice/sourceservice/dscreenservice/include/dscreen_source_service.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_SOURCE_SERVICE_H +#define OHOS_DSCREEN_SOURCE_SERVICE_H + +#include "system_ability.h" +#include "ipc_object_stub.h" + +#include "dscreen_manager.h" +#include "dscreen_source_stub.h" +#include "idscreen_source_callback.h" + +namespace OHOS { +namespace DistributedHardware { +class DScreenSourceService : public SystemAbility, public DScreenSourceStub, + public std::enable_shared_from_this { +DECLARE_SYSTEM_ABILITY(DScreenSourceService); +public: + DScreenSourceService(int32_t saId, bool runOnCreate); + ~DScreenSourceService(); + int32_t InitSource(const std::string ¶ms, const sptr &callback) override; + int32_t ReleaseSource() override; + int32_t RegisterDistributedHardware(const std::string &devId, const std::string &dhId, + const EnableParam ¶m, const std::string &reqId) override; + int32_t UnregisterDistributedHardware(const std::string &devId, const std::string &dhId, + const std::string &reqId) override; + int32_t ConfigDistributedHardware(const std::string &devId, const std::string &dhId, const std::string &key, + const std::string &value) override; + void DScreenNotify(const std::string &devId, const int32_t eventCode, const std::string &eventContent) override; + +protected: + void OnStart() override; + void OnStop() override; + DISALLOW_COPY_AND_MOVE(DScreenSourceService); + +private: + bool Init(); + + sptr dScreenSourceCallbackProxy_ = nullptr; + bool registerToService_ = false; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screenservice/sourceservice/dscreenservice/include/dscreen_source_stub.h b/services/screenservice/sourceservice/dscreenservice/include/dscreen_source_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..59c4fd9e03c3f4f81aae78c755454cd524822705 --- /dev/null +++ b/services/screenservice/sourceservice/dscreenservice/include/dscreen_source_stub.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_SOURCE_STUB_H +#define OHOS_DSCREEN_SOURCE_STUB_H + +#include +#include "iremote_stub.h" +#include "idscreen_source.h" + +namespace OHOS { +namespace DistributedHardware { +class DScreenSourceStub : public IRemoteStub { +public: + DScreenSourceStub(); + virtual ~DScreenSourceStub(); + virtual int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) override; + +private: + int32_t InitSourceInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + int32_t ReleaseSourceInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + int32_t RegisterDistributedHardwareInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + int32_t UnregisterDistributedHardwareInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + int32_t ConfigDistributedHardwareInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + int32_t DScreenNotifyInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + + using DScreenSourceFunc = int32_t (DScreenSourceStub::*)(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + std::map memberFuncMap_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screenservice/sourceservice/dscreenservice/src/callback/dscreen_source_callback_proxy.cpp b/services/screenservice/sourceservice/dscreenservice/src/callback/dscreen_source_callback_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd099abe662bf59f2a2d7c7a599c97a2062aaeed --- /dev/null +++ b/services/screenservice/sourceservice/dscreenservice/src/callback/dscreen_source_callback_proxy.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_source_callback_proxy.h" + +#include "parcel.h" + +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "dscreen_util.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t DScreenSourceCallbackProxy::OnNotifyRegResult(const std::string &devId, const std::string &dhId, + const std::string &reqId, int32_t status, const std::string &resultData) +{ + DHLOGD("OnNotifyRegResult, devId: %s, dhId: %s, reqId: %s, status: %d", + GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), reqId.c_str(), status); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken failed."); + return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED; + } + + if (!data.WriteString(devId) || !data.WriteString(dhId) || + !data.WriteString(reqId) || !data.WriteInt32(status) || + !data.WriteString(resultData)) { + DHLOGE("Write param failed."); + return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED; + } + + Remote()->SendRequest(NOTIFY_REG_RESULT, data, reply, option); + return reply.ReadInt32(); +} + +int32_t DScreenSourceCallbackProxy::OnNotifyUnregResult(const std::string &devId, const std::string &dhId, + const std::string &reqId, int32_t status, const std::string &resultData) +{ + DHLOGD("OnNotifyUnregResult, devId: %s, dhId: %s, reqId: %s, status: %d", + GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), reqId.c_str(), status); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken failed."); + return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED; + } + + if (!data.WriteString(devId) || !data.WriteString(dhId) || + !data.WriteString(reqId) || !data.WriteInt32(status) || + !data.WriteString(resultData)) { + DHLOGE("Write param failed."); + return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED; + } + + Remote()->SendRequest(NOTIFY_UNREG_RESULT, data, reply, option); + return reply.ReadInt32(); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screenservice/sourceservice/dscreenservice/src/dscreen_source_service.cpp b/services/screenservice/sourceservice/dscreenservice/src/dscreen_source_service.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df6efc3e4bece851a9f5b8cbe98b282034fec72d --- /dev/null +++ b/services/screenservice/sourceservice/dscreenservice/src/dscreen_source_service.cpp @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_source_service.h" + +#include "if_system_ability_manager.h" +#include "ipc_skeleton.h" +#include "ipc_types.h" +#include "iservice_registry.h" +#include "string_ex.h" +#include "system_ability_definition.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "dscreen_util.h" + +namespace OHOS { +namespace DistributedHardware { +REGISTER_SYSTEM_ABILITY_BY_ID(DScreenSourceService, DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, true); + +DScreenSourceService::DScreenSourceService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate) +{ + DHLOGI("dscreen source service create."); +} + +DScreenSourceService::~DScreenSourceService() +{ + DHLOGI("~DScreenSourceService."); +} + +void DScreenSourceService::OnStart() +{ + DHLOGI("dscreen source service start."); + Init(); +} + +void DScreenSourceService::OnStop() +{ + DHLOGI("dscreen source service stop."); + dScreenSourceCallbackProxy_ = nullptr; + registerToService_ = false; +} + +bool DScreenSourceService::Init() +{ + DHLOGI("dscreen source service start init."); + if (!registerToService_) { + bool ret = Publish(this); + if (!ret) { + DHLOGE("dscreen source publish service failed."); + return false; + } + registerToService_ = true; + } + DHLOGI("dscreen init success."); + return true; +} + +int32_t DScreenSourceService::InitSource(const std::string ¶ms, const sptr &callback) +{ + DHLOGI("InitSource"); + dScreenSourceCallbackProxy_ = callback; + int32_t ret = DScreenManager::GetInstance().Init(); + if (ret != DH_SUCCESS) { + DHLOGE("Init DScreenManager failed. err: %d", ret); + return ret; + } + + DScreenManager::GetInstance().RegisterDScreenCallback(callback); + return DH_SUCCESS; +} + +int32_t DScreenSourceService::ReleaseSource() +{ + DHLOGI("ReleaseSource"); + dScreenSourceCallbackProxy_ = nullptr; + int32_t ret = DScreenManager::GetInstance().UnInit(); + if (ret != DH_SUCCESS) { + DHLOGE("UnInit DScreenManager failed. err: %d", ret); + return ret; + } + return DH_SUCCESS; +} + +int32_t DScreenSourceService::RegisterDistributedHardware(const std::string &devId, const std::string &dhId, + const EnableParam ¶m, const std::string &reqId) +{ + DHLOGI("RegisterDistributedHardware"); + std::string version = param.version; + std::string attrs = param.attrs; + DHLOGD("enable distributedScreen. devId: %s, dhId: %s, reqId: %s, attrs: %s, version: %s", + GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), reqId.c_str(), attrs.c_str(), version.c_str()); + int ret = DScreenManager::GetInstance().EnableDistributedScreen(devId, dhId, attrs, reqId); + if (ret != DH_SUCCESS) { + DHLOGE("enable distributedScreen failed. devId: %s, dhId: %s, reqId: %s, attrs: %s", + GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), reqId.c_str(), attrs.c_str()); + return ERR_DH_SCREEN_SA_ENABLE_FAILED; + } + return DH_SUCCESS; +} + +int32_t DScreenSourceService::UnregisterDistributedHardware(const std::string &devId, const std::string &dhId, + const std::string &reqId) +{ + DHLOGI("UnregisterDistributedHardware"); + int ret = DScreenManager::GetInstance().DisableDistributedScreen(devId, dhId, reqId); + if (ret != DH_SUCCESS) { + DHLOGE("disable distributedScreen failed. devId: %s, dhId: %s, reqId: %s", + GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), reqId.c_str()); + return ERR_DH_SCREEN_SA_DISABLE_FAILED; + } + return DH_SUCCESS; +} + +int32_t DScreenSourceService::ConfigDistributedHardware(const std::string &devId, const std::string &dhId, + const std::string &key, const std::string &value) +{ + DHLOGI("ConfigDistributedHardware"); + return DH_SUCCESS; +} + +void DScreenSourceService::DScreenNotify(const std::string &devId, const int32_t eventCode, + const std::string &eventContent) +{ + DHLOGI("DScreenNotify, devId: %s, eventCode: %d", GetAnonyString(devId).c_str(), eventCode); + DScreenManager::GetInstance().HandleDScreenNotify(devId, eventCode, eventContent); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screenservice/sourceservice/dscreenservice/src/dscreen_source_stub.cpp b/services/screenservice/sourceservice/dscreenservice/src/dscreen_source_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23630c5a99b00dcdf3d693e888ea704a5582229a --- /dev/null +++ b/services/screenservice/sourceservice/dscreenservice/src/dscreen_source_stub.cpp @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_source_stub.h" + +#include "iservice_registry.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "dscreen_source_callback_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +DScreenSourceStub::DScreenSourceStub() +{ + DHLOGI("DScreenSourceStub construct."); + memberFuncMap_[INIT_SOURCE] = &DScreenSourceStub::InitSourceInner; + memberFuncMap_[RELEASE_SOURCE] = &DScreenSourceStub::ReleaseSourceInner; + memberFuncMap_[REGISTER_DISTRIBUTED_HARDWARE] = &DScreenSourceStub::RegisterDistributedHardwareInner; + memberFuncMap_[UNREGISTER_DISTRIBUTED_HARDWARE] = &DScreenSourceStub::UnregisterDistributedHardwareInner; + memberFuncMap_[CONFIG_DISTRIBUTED_HARDWARE] = &DScreenSourceStub::ConfigDistributedHardwareInner; + memberFuncMap_[DSCREEN_NOTIFY] = &DScreenSourceStub::DScreenNotifyInner; +} + +DScreenSourceStub::~DScreenSourceStub() +{ + DHLOGI("DScreenSourceStub deconstruct."); +} + +int32_t DScreenSourceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DHLOGI("OnRemoteRequest, code: %d", code); + std::u16string desc = DScreenSourceStub::GetDescriptor(); + std::u16string remoteDesc = data.ReadInterfaceToken(); + if (desc != remoteDesc) { + DHLOGE("DScreenSourceStub::OnRemoteRequest remoteDesc is invalid!"); + return ERR_INVALID_DATA; + } + + std::map::iterator iter = memberFuncMap_.find(code); + if (iter == memberFuncMap_.end()) { + DHLOGE("invalid request code."); + return ERR_DH_SCREEN_SA_REQUEST_CODE_INVALID; + } + DScreenSourceFunc &func = iter->second; + return (this->*func)(data, reply, option); +} + +int32_t DScreenSourceStub::InitSourceInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DHLOGI("InitSourceInner"); + std::string param = data.ReadString(); + sptr remoteObject = data.ReadRemoteObject(); + if (remoteObject == nullptr) { + DHLOGE("Read param failed."); + return ERR_DH_SCREEN_SA_READPARAM_FAILED; + } + + sptr dScreenSourceCallbackProxy(new DScreenSourceCallbackProxy(remoteObject)); + int32_t ret = InitSource(param, dScreenSourceCallbackProxy); + reply.WriteInt32(ret); + return DH_SUCCESS; +} + +int32_t DScreenSourceStub::ReleaseSourceInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DHLOGI("ReleaseSourceInner"); + int32_t ret = ReleaseSource(); + reply.WriteInt32(ret); + return DH_SUCCESS; +} + +int32_t DScreenSourceStub::RegisterDistributedHardwareInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DHLOGI("RegisterDistributedHardwareInner"); + std::string devId = data.ReadString(); + std::string dhId = data.ReadString(); + std::string version = data.ReadString(); + std::string attrs = data.ReadString(); + std::string reqId = data.ReadString(); + EnableParam enableParam; + enableParam.version = version; + enableParam.attrs = attrs; + + int32_t ret = RegisterDistributedHardware(devId, dhId, enableParam, reqId); + reply.WriteInt32(ret); + return DH_SUCCESS; +} + +int32_t DScreenSourceStub::UnregisterDistributedHardwareInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DHLOGI("RegisterDistributedHardwareInner"); + std::string devId = data.ReadString(); + std::string dhId = data.ReadString(); + std::string reqId = data.ReadString(); + + int32_t ret = UnregisterDistributedHardware(devId, dhId, reqId); + reply.WriteInt32(ret); + return DH_SUCCESS; +} + +int32_t DScreenSourceStub::ConfigDistributedHardwareInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DHLOGI("ConfigDistributedHardwareInner"); + std::string devId = data.ReadString(); + std::string dhId = data.ReadString(); + std::string key = data.ReadString(); + std::string value = data.ReadString(); + + int32_t ret = ConfigDistributedHardware(devId, dhId, key, value); + reply.WriteInt32(ret); + return DH_SUCCESS; +} + +int32_t DScreenSourceStub::DScreenNotifyInner(MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DHLOGI("DScreenNotifyInner"); + std::string devId = data.ReadString(); + int32_t eventCode = data.ReadInt32(); + std::string eventContent = data.ReadString(); + + DScreenNotify(devId, eventCode, eventContent); + return DH_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screentransport/screendatachannel/include/screen_data_channel_impl.h b/services/screentransport/screendatachannel/include/screen_data_channel_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..9ec1641661ca0da595084176106d803d3e76a9ad --- /dev/null +++ b/services/screentransport/screendatachannel/include/screen_data_channel_impl.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SCREEN_DATA_CHANNEL_IMPL_H +#define OHOS_SCREEN_DATA_CHANNEL_IMPL_H + +#include +#include + +#include "data_buffer.h" +#include "iscreen_channel.h" +#include "softbus_adapter.h" + +namespace OHOS { +namespace DistributedHardware { +class ScreenDataChannelImpl : public IScreenChannel, + public ISoftbusListener, + public std::enable_shared_from_this { +public: + ScreenDataChannelImpl(std::string peerDevId) : peerDevId_(peerDevId) {}; + ~ScreenDataChannelImpl() = default; + + int32_t CreateSession(const std::shared_ptr &listener) override; + int32_t ReleaseSession() override; + int32_t OpenSession() override; + int32_t CloseSession() override; + int32_t SendData(const std::shared_ptr &screenData) override; + + void OnSessionOpened(int32_t sessionId, int32_t result) override; + void OnSessionClosed(int32_t sessionId) override; + void OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) override; + void OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, + const StreamFrameInfo *param) override; + +private: + static const constexpr char *LOG_TAG = "ScreenDataChannel"; + + const std::string peerDevId_; + int32_t sessionId_ = 0; + std::weak_ptr channelListener_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp b/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6097c4a5c64a91659f96305bb884e29a6632358e --- /dev/null +++ b/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp @@ -0,0 +1,197 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screen_data_channel_impl.h" + +#include + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "dscreen_util.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t ScreenDataChannelImpl::CreateSession(const std::shared_ptr &listener) +{ + DHLOGI("%s: CreateSession, peerDevId(%s)", LOG_TAG, GetAnonyString(peerDevId_).c_str()); + if (!listener) { + DHLOGE("%s: Channel listener is null", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = + SoftbusAdapter::GetInstance().CreateSoftbusSessionServer(PKG_NAME, DATA_SESSION_NAME, peerDevId_); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Create softbus session failed ret: %d.", LOG_TAG, ret); + return ret; + } + + std::shared_ptr softbusListener = shared_from_this(); + ret = SoftbusAdapter::GetInstance().RegisterSoftbusListener(softbusListener, DATA_SESSION_NAME, peerDevId_); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Register softbus adapter listener failed ret: %d.", LOG_TAG, ret); + return ret; + } + + channelListener_ = listener; + DHLOGI("%s: Create softbus session success.", LOG_TAG); + return DH_SUCCESS; +} + +int32_t ScreenDataChannelImpl::ReleaseSession() +{ + DHLOGI("%s: ReleaseSession, peerDevId(%s)", LOG_TAG, GetAnonyString(peerDevId_).c_str()); + int32_t ret = SoftbusAdapter::GetInstance().RemoveSoftbusSessionServer(PKG_NAME, DATA_SESSION_NAME, peerDevId_); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Release softbus session failed ret: %d.", LOG_TAG, ret); + return ret; + } + + ret = SoftbusAdapter::GetInstance().UnRegisterSoftbusListener(DATA_SESSION_NAME, peerDevId_); + if (ret != DH_SUCCESS) { + DHLOGE("%s: UnRegister softbus adapter listener failed ret: %d.", LOG_TAG, ret); + return ret; + } + + DHLOGI("%s: Release softbus session success.", LOG_TAG); + return DH_SUCCESS; +} + +int32_t ScreenDataChannelImpl::OpenSession() +{ + DHLOGI("%s: OpenSession, peerDevId(%s)", LOG_TAG, GetAnonyString(peerDevId_).c_str()); + int32_t sessionId = + SoftbusAdapter::GetInstance().OpenSoftbusSession(DATA_SESSION_NAME, DATA_SESSION_NAME, peerDevId_); + if (sessionId < 0) { + DHLOGE("%s: Open screen session failed, ret: %d", LOG_TAG, sessionId); + return ERR_DH_SCREEN_TRANS_ERROR; + } + sessionId_ = sessionId; + + DHLOGI("%s: Open screen session success, sessionId(%d)", LOG_TAG, sessionId_); + return DH_SUCCESS; +} + +int32_t ScreenDataChannelImpl::CloseSession() +{ + DHLOGI("%s: CloseSession, sessionId(%d)", LOG_TAG, sessionId_); + if (sessionId_ == 0) { + DHLOGD("%s: Session is not opened.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_SESSION_NOT_OPEN; + } + + int32_t ret = SoftbusAdapter::GetInstance().CloseSoftbusSession(sessionId_); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Close screen session failed ret: %d.", LOG_TAG, ret); + return ret; + } + sessionId_ = 0; + + DHLOGI("%s: Close screen session success.", LOG_TAG); + return DH_SUCCESS; +} + +int32_t ScreenDataChannelImpl::SendData(const std::shared_ptr &screenData) +{ + DHLOGD("%s: SendData, sessionId(%d)", LOG_TAG, sessionId_); + if (!screenData) { + DHLOGE("%s: Screen data is null", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + StreamData data = {(char *)screenData->Data(), screenData->Capacity()}; + StreamData ext = {0}; + StreamFrameInfo frameInfo = {0}; + + int32_t ret = SoftbusAdapter::GetInstance().SendSoftbusStream(sessionId_, &data, &ext, &frameInfo); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Send screen data failed ret: %d.", LOG_TAG, ret); + return ret; + } + + return DH_SUCCESS; +} + +void ScreenDataChannelImpl::OnSessionOpened(int32_t sessionId, int32_t result) +{ + DHLOGI("%s: OnScreenSessionOpened, sessionId: %d, result: %d", LOG_TAG, sessionId, result); + if (result != 0) { + DHLOGE("Session open failed.", LOG_TAG); + return; + } + + std::shared_ptr listener = channelListener_.lock(); + if (!listener) { + DHLOGE("%s: Channel listener is null", LOG_TAG); + return; + } + listener->OnSessionOpened(); + sessionId_ = sessionId; +} + +void ScreenDataChannelImpl::OnSessionClosed(int32_t sessionId) +{ + DHLOGI("%s: OnScreenSessionClosed, sessionId(%d).", LOG_TAG, sessionId); + std::shared_ptr listener = channelListener_.lock(); + if (!listener) { + DHLOGE("%s: Channel listener is null", LOG_TAG); + return; + } + listener->OnSessionClosed(); +} + +void ScreenDataChannelImpl::OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) +{ + (void) sessionId; + (void) data; + (void) dataLen; + + DHLOGD("%s: OnScreenBytesReceived data channel not support yet.", LOG_TAG); +} + +void ScreenDataChannelImpl::OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, + const StreamFrameInfo *param) +{ + (void)ext; + (void)param; + + if (data == nullptr) { + DHLOGE("%s: Stream data is null.", LOG_TAG); + return; + } + + std::shared_ptr listener = channelListener_.lock(); + if (!listener) { + DHLOGE("%s: Channel listener is null.", LOG_TAG); + return; + } + + DHLOGI("%s: OnScreenStreamReceived, sessionId(%d) dataSize(%zu).", LOG_TAG, sessionId, data->bufLen); + auto dataBuffer = std::make_shared(data->bufLen); + if (!dataBuffer) { + DHLOGE("%s: DataBuffer is null.", LOG_TAG); + return; + } + + int32_t ret = memcpy_s(dataBuffer->Data(), dataBuffer->Capacity(), (uint8_t *)data->buf, data->bufLen); + if (ret != EOK) { + DHLOGE("%s: Data memcpy_s failed.", LOG_TAG); + return; + } + listener->OnDataReceived(dataBuffer); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screentransport/screensinkprocessor/decoder/include/image_decoder_callback.h b/services/screentransport/screensinkprocessor/decoder/include/image_decoder_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..939205ab6a33b50fe5881fcad340f4acd4123f0b --- /dev/null +++ b/services/screentransport/screensinkprocessor/decoder/include/image_decoder_callback.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_IMAGE_DECODER_CALLBACK_H +#define OHOS_IMAGE_DECODER_CALLBACK_H + +#include + +#include "media_errors.h" +#include "avcodec_common.h" +#include "format.h" + +namespace OHOS { +namespace DistributedHardware { +class ImageSinkDecoder; +class ImageDecoderCallback : public Media::AVCodecCallback { +public: + explicit ImageDecoderCallback(const std::shared_ptr &decoder) + : videoDecoder_(decoder) {}; + ~ImageDecoderCallback() = default; + + void OnError(Media::AVCodecErrorType errorType, int32_t errorCode) override; + void OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info, Media::AVCodecBufferFlag flag) override; + void OnInputBufferAvailable(uint32_t index) override; + void OnOutputFormatChanged(const Media::Format &format) override; + +private: + static const constexpr char *LOG_TAG = "ImageDecoderCallback"; + + std::weak_ptr videoDecoder_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensinkprocessor/decoder/include/image_sink_decoder.h b/services/screentransport/screensinkprocessor/decoder/include/image_sink_decoder.h new file mode 100644 index 0000000000000000000000000000000000000000..fa17b5a1ab3a5708b89d4f333f410a3c1668a439 --- /dev/null +++ b/services/screentransport/screensinkprocessor/decoder/include/image_sink_decoder.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_IMAGE_SINK_DECODER_H +#define OHOS_IMAGE_SINK_DECODER_H + +#include +#include +#include +#include + +#include "avsharedmemory.h" +#include "avcodec_common.h" +#include "avcodec_video_decoder.h" +#include "media_errors.h" +#include "format.h" +#include "surface.h" + +#include "data_buffer.h" +#include "iimage_sink_processor_listener.h" +#include "image_decoder_callback.h" +#include "video_param.h" + +namespace OHOS { +namespace DistributedHardware { +class ImageSinkDecoder : public std::enable_shared_from_this { +public: + ImageSinkDecoder(const std::shared_ptr &imageListener) + : imageProcessorListener_(imageListener) {}; + ~ImageSinkDecoder() = default; + + int32_t ConfigureDecoder(const VideoParam &configParam); + int32_t ReleaseDecoder(); + int32_t StartDecoder(); + int32_t StopDecoder(); + int32_t SetOutputSurface(sptr &surface); + int32_t InputScreenData(const std::shared_ptr &data); + + void OnError(Media::AVCodecErrorType errorType, int32_t errorCode); + void OnInputBufferAvailable(uint32_t index); + void OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info, Media::AVCodecBufferFlag flag); + void OnOutputFormatChanged(const Media::Format &format); + +private: + int32_t SetDecoderFormat(const VideoParam &configParam); + int32_t InitVideoDecoder(const VideoParam &configParam); + int32_t StartInputThread(); + int32_t StopInputThread(); + void DecodeScreenData(); + int32_t ProcessData(const std::shared_ptr &screenData, const int32_t bufferIndex); + +private: + static const constexpr char *LOG_TAG = "ImageSinkDecoder"; + static constexpr uint32_t DECODE_WAIT_MILLISECONDS = 5000; + static constexpr size_t DATA_QUEUE_MAX_SIZE = 10; + + std::mutex dataMutex_; + std::mutex decodeMutex_; + std::thread decodeThread_; + std::condition_variable decodeCond_; + + Media::Format imageFormat_; + Media::AVCodecBufferInfo decoderBufferInfo_; + + bool isDecoderReady_ = false; + std::queue> videoDataQueue_; + std::queue bufferIndexQueue_; + std::shared_ptr videoDecoder_; + std::shared_ptr decodeVideoCallback_; + std::weak_ptr imageProcessorListener_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensinkprocessor/decoder/src/image_decoder_callback.cpp b/services/screentransport/screensinkprocessor/decoder/src/image_decoder_callback.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b19fb8708670905c20ec4fa4da4349283ef148c9 --- /dev/null +++ b/services/screentransport/screensinkprocessor/decoder/src/image_decoder_callback.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "image_decoder_callback.h" + +#include "dscreen_log.h" +#include "image_sink_decoder.h" + +namespace OHOS { +namespace DistributedHardware { +void ImageDecoderCallback::OnError(Media::AVCodecErrorType errorType, int32_t errorCode) +{ + DHLOGD("%s: OnError.", LOG_TAG); + std::shared_ptr decoder = videoDecoder_.lock(); + if (decoder == nullptr) { + DHLOGE("decoder is nullptr."); + return; + } + decoder->OnError(errorType, errorCode); +} + +void ImageDecoderCallback::OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info, + Media::AVCodecBufferFlag flag) +{ + DHLOGD("%s: OnOutputBufferAvailable.", LOG_TAG); + std::shared_ptr decoder = videoDecoder_.lock(); + if (decoder == nullptr) { + DHLOGE("decoder is nullptr."); + return; + } + decoder->OnOutputBufferAvailable(index, info, flag); +} + +void ImageDecoderCallback::OnInputBufferAvailable(uint32_t index) +{ + DHLOGD("%s: OnInputBufferAvailable.", LOG_TAG); + std::shared_ptr decoder = videoDecoder_.lock(); + if (decoder == nullptr) { + DHLOGE("decoder is nullptr."); + return; + } + decoder->OnInputBufferAvailable(index); +} + +void ImageDecoderCallback::OnOutputFormatChanged(const Media::Format &format) +{ + DHLOGD("%s: OnOutputFormatChanged.", LOG_TAG); + std::shared_ptr decoder = videoDecoder_.lock(); + if (decoder == nullptr) { + DHLOGE("decoder is nullptr."); + return; + } + decoder->OnOutputFormatChanged(format); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screentransport/screensinkprocessor/decoder/src/image_sink_decoder.cpp b/services/screentransport/screensinkprocessor/decoder/src/image_sink_decoder.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea167bf7a6cbc4e809641be71a5554293adddb01 --- /dev/null +++ b/services/screentransport/screensinkprocessor/decoder/src/image_sink_decoder.cpp @@ -0,0 +1,346 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "image_sink_decoder.h" + +#include +#include + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t ImageSinkDecoder::ConfigureDecoder(const VideoParam &configParam) +{ + DHLOGI("%s: ConfigureDecoder.", LOG_TAG); + int32_t ret = InitVideoDecoder(configParam); + if (ret != DH_SUCCESS) { + DHLOGE("%s: InitVideoDecoder failed.", LOG_TAG); + return ret; + } + + ret = SetDecoderFormat(configParam); + if (ret != DH_SUCCESS) { + DHLOGE("%s: SetDecoderFormat failed.", LOG_TAG); + return ret; + } + + return DH_SUCCESS; +} + +int32_t ImageSinkDecoder::ReleaseDecoder() +{ + DHLOGI("%s: ReleaseDecoder.", LOG_TAG); + if (!videoDecoder_) { + DHLOGE("%s: Decoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = videoDecoder_->Release(); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: ReleaseDecoder failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_RELEASE_FAILED; + } + decodeVideoCallback_ = nullptr; + videoDecoder_ = nullptr; + + return DH_SUCCESS; +} + +int32_t ImageSinkDecoder::StartDecoder() +{ + DHLOGI("%s: StartDecoder.", LOG_TAG); + if (!videoDecoder_) { + DHLOGE("%s: Decoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = videoDecoder_->Prepare(); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: Prepare decoder failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_PREPARE_FAILED; + } + + ret = videoDecoder_->Start(); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: Start decoder failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_START_FAILED; + } + StartInputThread(); + + return DH_SUCCESS; +} + +int32_t ImageSinkDecoder::StopDecoder() +{ + DHLOGI("%s: StopDecoder.", LOG_TAG); + if (!videoDecoder_) { + DHLOGE("%s: Decoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = videoDecoder_->Flush(); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: Flush decoder failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_FLUSH_FAILED; + } + + ret = videoDecoder_->Stop(); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: Stop decoder failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_STOP_FAILED; + } + StopInputThread(); + + return DH_SUCCESS; +} + +int32_t ImageSinkDecoder::InitVideoDecoder(const VideoParam &configParam) +{ + DHLOGI("%s: InitVideoDecoder.", LOG_TAG); + switch (configParam.GetCodecType()) { + case VIDEO_CODEC_TYPE_VIDEO_H264: + videoDecoder_ = Media::VideoDecoderFactory::CreateByName("OMX_hisi_video_decoder_avc"); + break; + case VIDEO_CODEC_TYPE_VIDEO_H265: + videoDecoder_ = Media::VideoDecoderFactory::CreateByMime("video/hevc"); + break; + default: + DHLOGE("%s: codecType is invalid!", LOG_TAG); + videoDecoder_ = nullptr; + } + + if (videoDecoder_ == nullptr) { + DHLOGE("%s: Create videoEncode failed.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + decodeVideoCallback_ = std::make_shared(shared_from_this()); + int32_t ret = videoDecoder_->SetCallback(decodeVideoCallback_); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: Set decoder callback failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_SET_CALLBACK_FAILED; + } + + return DH_SUCCESS; +} + +int32_t ImageSinkDecoder::SetDecoderFormat(const VideoParam &configParam) +{ + DHLOGI("%s: SetDecoderFormat.", LOG_TAG); + if (!videoDecoder_) { + DHLOGE("%s: Decoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + switch (configParam.GetCodecType()) { + case VIDEO_CODEC_TYPE_VIDEO_H264: + imageFormat_.PutStringValue("codec_mime", "video/avc"); + break; + case VIDEO_CODEC_TYPE_VIDEO_H265: + imageFormat_.PutStringValue("codec_mime", "video/hevc"); + break; + default: + DHLOGE("The current codec type does not support decoding."); + return ERR_DH_SCREEN_TRANS_ILLEGAL_OPERATION; + } + switch (configParam.GetVideoFormat()) { + case VIDEO_DATA_FORMAT_YUVI420: + imageFormat_.PutIntValue("pixel_format", Media::VideoPixelFormat::YUVI420); + break; + case VIDEO_DATA_FORMAT_NV12: + imageFormat_.PutIntValue("pixel_format", Media::VideoPixelFormat::NV12); + break; + case VIDEO_DATA_FORMAT_NV21: + imageFormat_.PutIntValue("pixel_format", Media::VideoPixelFormat::NV21); + break; + default: + DHLOGE("The current pixel format does not support decoding."); + return ERR_DH_SCREEN_TRANS_ILLEGAL_OPERATION; + } + + imageFormat_.PutLongValue("max_input_size", MAX_YUV420_BUFFER_SIZE); + imageFormat_.PutIntValue("width", configParam.GetVideoWidth()); + imageFormat_.PutIntValue("height", configParam.GetVideoHeight()); + imageFormat_.PutIntValue("frame_rate", configParam.GetFps()); + + int32_t ret = videoDecoder_->Configure(imageFormat_); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: configure decoder format param failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_CONFIGURE_FAILED; + } + + return DH_SUCCESS; +} + +int32_t ImageSinkDecoder::SetOutputSurface(sptr &surface) +{ + DHLOGI("%s: SetOutputSurface.", LOG_TAG); + if (!videoDecoder_) { + DHLOGE("%s: Decoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = videoDecoder_->SetOutputSurface(surface); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: SetOutputSurface failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_SURFACE_ERROR; + } + + return DH_SUCCESS; +} + +int32_t ImageSinkDecoder::InputScreenData(const std::shared_ptr &data) +{ + DHLOGD("%s: InputScreenData.", LOG_TAG); + std::lock_guard dataLock(dataMutex_); + while (videoDataQueue_.size() >= DATA_QUEUE_MAX_SIZE) { + DHLOGE("%s: videoData queue overflow.", LOG_TAG); + videoDataQueue_.pop(); + } + videoDataQueue_.push(data); + decodeCond_.notify_all(); + + return DH_SUCCESS; +} + +void ImageSinkDecoder::OnError(Media::AVCodecErrorType errorType, int32_t errorCode) +{ + DHLOGI("%s: OnImageDecodeError, errorType(%d), errorCode(%d)", LOG_TAG, errorType, errorCode); + std::shared_ptr listener = imageProcessorListener_.lock(); + if (!listener) { + DHLOGE("%s: Listener is null.", LOG_TAG); + return; + } + listener->OnProcessorStateNotify(errorCode); +} + +void ImageSinkDecoder::OnInputBufferAvailable(uint32_t index) +{ + DHLOGI("%s: OnDecodeInputBufferAvailable: %u.", LOG_TAG, index); + std::lock_guard dataLock(dataMutex_); + bufferIndexQueue_.push(index); +} + +void ImageSinkDecoder::OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info, + Media::AVCodecBufferFlag flag) +{ + DHLOGI("%s: OnDecodeOutputBufferAvailable.", LOG_TAG); + if (!videoDecoder_) { + DHLOGE("%s: Decoder is null.", LOG_TAG); + return; + } + + decoderBufferInfo_ = info; + int32_t ret = videoDecoder_->ReleaseOutputBuffer(index, true); + if (ret != Media::MSERR_OK) { + DHLOGD("%s: ReleaseOutputBuffer failed.", LOG_TAG); + } +} + +void ImageSinkDecoder::OnOutputFormatChanged(const Media::Format &format) +{ + (void) format; +} + +int32_t ImageSinkDecoder::StartInputThread() +{ + DHLOGI("%s: StartInputThread.", LOG_TAG); + isDecoderReady_ = true; + decodeThread_ = std::thread(&ImageSinkDecoder::DecodeScreenData, this); + + return DH_SUCCESS; +} + +int32_t ImageSinkDecoder::StopInputThread() +{ + DHLOGI("%s: StopInputThread.", LOG_TAG); + isDecoderReady_ = false; + decodeThread_.join(); + std::lock_guard dataLock(dataMutex_); + while (!bufferIndexQueue_.empty()) { + bufferIndexQueue_.pop(); + } + while (!videoDataQueue_.empty()) { + videoDataQueue_.pop(); + } + + return DH_SUCCESS; +} + +void ImageSinkDecoder::DecodeScreenData() +{ + while (isDecoderReady_) { + std::shared_ptr screenData; + int32_t bufferIndex = 0; + { + std::unique_lock lock(dataMutex_); + decodeCond_.wait_for(lock, std::chrono::milliseconds(DECODE_WAIT_MILLISECONDS), + [this]() { return (!videoDataQueue_.empty() && !bufferIndexQueue_.empty()); }); + + if (videoDataQueue_.empty() || bufferIndexQueue_.empty()) { + DHLOGD("%s: Index queue or data queue is empty.", LOG_TAG); + continue; + } + bufferIndex = bufferIndexQueue_.front(); + bufferIndexQueue_.pop(); + screenData = videoDataQueue_.front(); + videoDataQueue_.pop(); + } + + int32_t ret = ProcessData(screenData, bufferIndex); + if (ret == ERR_DH_SCREEN_TRANS_NULL_VALUE) { + return; + } else if (ret != DH_SUCCESS) { + continue; + } + } +} + +int32_t ImageSinkDecoder::ProcessData(const std::shared_ptr &screenData, const int32_t bufferIndex) +{ + if (!videoDecoder_) { + DHLOGE("%s: Decoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + auto inputBuffer = videoDecoder_->GetInputBuffer(bufferIndex); + if (inputBuffer == nullptr) { + DHLOGE("%s: GetInputBuffer failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_SURFACE_ERROR; + } + + int32_t ret = memcpy_s(inputBuffer->GetBase(), inputBuffer->GetSize(), screenData->Data(), screenData->Capacity()); + if (ret != EOK) { + DHLOGE("%s: Copy data failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_SURFACE_ERROR; + } + + DHLOGD("%s: Decode screen data.", LOG_TAG); + Media::AVCodecBufferInfo bufferInfo; + bufferInfo.presentationTimeUs = 0; + bufferInfo.size = static_cast(screenData->Capacity()); + bufferInfo.offset = 0; + ret = videoDecoder_->QueueInputBuffer(bufferIndex, bufferInfo, Media::AVCODEC_BUFFER_FLAG_NONE); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: QueueInputBuffer failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_SURFACE_ERROR; + } + return DH_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/screentransport/screensinkprocessor/include/iimage_sink_processor.h b/services/screentransport/screensinkprocessor/include/iimage_sink_processor.h new file mode 100644 index 0000000000000000000000000000000000000000..6deed5fa8e92d9bef95deea1a215402841ba28eb --- /dev/null +++ b/services/screentransport/screensinkprocessor/include/iimage_sink_processor.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_IIMAGE_SINK_PROCESSOR_H +#define OHOS_IIMAGE_SINK_PROCESSOR_H + +#include + +#include "surface.h" + +#include "data_buffer.h" +#include "video_param.h" +#include "iimage_sink_processor_listener.h" + +namespace OHOS { +namespace DistributedHardware { +class IImageSinkProcessor { +public: + virtual ~IImageSinkProcessor() = default; + + virtual int32_t ConfigureImageProcessor(const VideoParam &localParam, const VideoParam &remoteParam, + const std::shared_ptr &listener) = 0; + virtual int32_t ReleaseImageProcessor() = 0; + virtual int32_t StartImageProcessor() = 0; + virtual int32_t StopImageProcessor() = 0; + virtual int32_t SetImageSurface(sptr &surface) = 0; + virtual int32_t ProcessImage(const std::shared_ptr &data) = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensinkprocessor/include/iimage_sink_processor_listener.h b/services/screentransport/screensinkprocessor/include/iimage_sink_processor_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..120881d383de228eb3ca633b3bc43a13fef6af39 --- /dev/null +++ b/services/screentransport/screensinkprocessor/include/iimage_sink_processor_listener.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_IIMAGE_SINK_PROCESSOR_LISTENER_H +#define OHOS_IIMAGE_SINK_PROCESSOR_LISTENER_H + +namespace OHOS { +namespace DistributedHardware { +class IImageSinkProcessorListener { +public: + virtual ~IImageSinkProcessorListener() = default; + + virtual void OnProcessorStateNotify(int32_t state) = 0; +}; +} // namespace DistributedHardware +} // namsspace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensinkprocessor/include/image_sink_processor.h b/services/screentransport/screensinkprocessor/include/image_sink_processor.h new file mode 100644 index 0000000000000000000000000000000000000000..8929a74bcd73679b6bc2d7dcf73cdb0de97677f2 --- /dev/null +++ b/services/screentransport/screensinkprocessor/include/image_sink_processor.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IMAGE_SINK_PROCESSOR_H +#define IMAGE_SINK_PROCESSOR_H + +#include "iimage_sink_processor.h" +#include "image_sink_decoder.h" +#include "video_param.h" +#include "data_buffer.h" + +namespace OHOS { +namespace DistributedHardware { +class ImageSinkProcessor : public IImageSinkProcessor { +public: + ImageSinkProcessor() = default; + ~ImageSinkProcessor() = default; + + int32_t ConfigureImageProcessor(const VideoParam &localParam, const VideoParam &remoteParam, + const std::shared_ptr &imageListener) override; + int32_t ReleaseImageProcessor() override; + int32_t StartImageProcessor() override; + int32_t StopImageProcessor() override; + int32_t SetImageSurface(sptr &surface) override; + int32_t ProcessImage(const std::shared_ptr &data) override; + +private: + static const constexpr char *LOG_TAG = "ImageSinkProcessor"; + VideoParam localParam_; + VideoParam remoteParam_; + std::shared_ptr imageDecoder_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensinkprocessor/src/image_sink_processor.cpp b/services/screentransport/screensinkprocessor/src/image_sink_processor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6572255eae7598aac3b1a627a246b83105c6f62b --- /dev/null +++ b/services/screentransport/screensinkprocessor/src/image_sink_processor.cpp @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "image_sink_processor.h" + +#include "dscreen_errcode.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t ImageSinkProcessor::ConfigureImageProcessor( + const VideoParam &localParam, const VideoParam &remoteParam, + const std::shared_ptr &imageListener) +{ + DHLOGI("%s: ConfigureImageProcessor.", LOG_TAG); + localParam_ = localParam; + remoteParam_ = remoteParam; + + imageDecoder_ = std::make_shared(imageListener); + if (imageDecoder_ == nullptr) { + DHLOGE("%s: Decoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = imageDecoder_->ConfigureDecoder(localParam); + if (ret != DH_SUCCESS) { + DHLOGE("%s: ConfigureDecoder failed ret:%d.", LOG_TAG, ret); + return ret; + } + + return DH_SUCCESS; +} + +int32_t ImageSinkProcessor::ReleaseImageProcessor() +{ + DHLOGI("%s: ReleaseImageProcessor.", LOG_TAG); + if (imageDecoder_ == nullptr) { + DHLOGE("%s: Decoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = imageDecoder_->ReleaseDecoder(); + if (ret != DH_SUCCESS) { + DHLOGE("%s: ReleaseDecoder failed.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ERROR; + } + + return DH_SUCCESS; +} + +int32_t ImageSinkProcessor::StartImageProcessor() +{ + DHLOGI("%s: StartImageProcessor.", LOG_TAG); + if (imageDecoder_ == nullptr) { + DHLOGE("%s: Decoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = imageDecoder_->StartDecoder(); + if (ret != DH_SUCCESS) { + DHLOGE("%s: StartDecoder failed ret:%d.", LOG_TAG, ret); + return ret; + } + + return DH_SUCCESS; +} + +int32_t ImageSinkProcessor::StopImageProcessor() +{ + DHLOGI("%s: StopImageProcessor.", LOG_TAG); + if (imageDecoder_ == nullptr) { + DHLOGE("%s: Decoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = imageDecoder_->StopDecoder(); + if (ret != DH_SUCCESS) { + DHLOGE("%s: StopDecoder failed ret:%d.", LOG_TAG, ret); + return ret; + } + + return DH_SUCCESS; +} + +int32_t ImageSinkProcessor::SetImageSurface(sptr &surface) +{ + DHLOGI("%s: SetImageSurface.", LOG_TAG); + if (imageDecoder_ == nullptr) { + DHLOGE("%s: Decoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = imageDecoder_->SetOutputSurface(surface); + if (ret != DH_SUCCESS) { + DHLOGE("%s: SetOutputSurface failed ret:%d.", LOG_TAG, ret); + return ret; + } + + return DH_SUCCESS; +} + +int32_t ImageSinkProcessor::ProcessImage(const std::shared_ptr &data) +{ + DHLOGI("%s: ProcessImage.", LOG_TAG); + if (imageDecoder_ == nullptr) { + DHLOGE("%s: Decoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = imageDecoder_->InputScreenData(data); + if (ret != DH_SUCCESS) { + DHLOGE("%s: InputScreenData failed ret:%d.", LOG_TAG, ret); + return ret; + } + + return DH_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screentransport/screensinktrans/BUILD.gn b/services/screentransport/screensinktrans/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0db390a50bc0c5c0a81abbbf80db273960759b06 --- /dev/null +++ b/services/screentransport/screensinktrans/BUILD.gn @@ -0,0 +1,70 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +ohos_shared_library("distributed_screen_sinktrans") { + include_dirs = [ + "//third_party/json/include", + "//foundation/graphic/standard/interfaces/innerkits/surface", + "${fwk_common_path}/utils/include", + ] + + include_dirs += [ + "./include", + "${common_path}/include", + "${services_path}/common/databuffer/include", + "${services_path}/common/screen_channel/include", + "${services_path}/common/utils/include", + "${services_path}/screentransport/screendatachannel/include", + "${services_path}/screentransport/screensinktrans/include", + "${services_path}/screentransport/screensinkprocessor/include", + "${services_path}/screentransport/screensinkprocessor/decoder/include", + "${services_path}/softbusadapter/include", + ] + + sources = [ + "./src/screen_sink_trans.cpp", + "${services_path}/common/databuffer/src/data_buffer.cpp", + "${services_path}/common/utils/src/video_param.cpp", + "${services_path}/screentransport/screendatachannel/src/screen_data_channel_impl.cpp", + "${services_path}/screentransport/screensinkprocessor/src/image_sink_processor.cpp", + "${services_path}/screentransport/screensinkprocessor/decoder/src/image_sink_decoder.cpp", + "${services_path}/screentransport/screensinkprocessor/decoder/src/image_decoder_callback.cpp", + "${services_path}/softbusadapter/src/softbus_adapter.cpp", + ] + + deps = [ + "${common_path}:distributed_screen_utils", + "//foundation/graphic/standard/frameworks/surface:surface", + "//utils/native/base:utils", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dscreensinktrans\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "dsoftbus_standard:softbus_client", + "hiviewdfx_hilog_native:libhilog", + "multimedia_media_standard:media_client", + ] + + subsystem_name = "distributedhardware" + + part_name = "distributed_screen" +} \ No newline at end of file diff --git a/services/screentransport/screensinktrans/include/iscreen_sink_trans.h b/services/screentransport/screensinktrans/include/iscreen_sink_trans.h new file mode 100644 index 0000000000000000000000000000000000000000..ad0f37880e972c57ea66350f1651d6b0741fca23 --- /dev/null +++ b/services/screentransport/screensinktrans/include/iscreen_sink_trans.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ISCREEN_SINK_TRANS_H +#define OHOS_ISCREEN_SINK_TRANS_H + +#include "surface.h" + +#include "iscreen_sink_trans_callback.h" +#include "video_param.h" + +namespace OHOS { +namespace DistributedHardware { +class IScreenSinkTrans { +public: + virtual ~IScreenSinkTrans() = default; + + virtual int32_t SetUp(const VideoParam &localParam, const VideoParam &remoteParam, + const std::string &peerDevId) = 0; + virtual int32_t Release() = 0; + virtual int32_t Start() = 0; + virtual int32_t Stop() = 0; + virtual int32_t RegisterStateCallback(const std::shared_ptr &callBack) = 0; + virtual int32_t SetImageSurface(const sptr &surface) = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensinktrans/include/iscreen_sink_trans_callback.h b/services/screentransport/screensinktrans/include/iscreen_sink_trans_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..81a39a4974238dc0021e8022e30e4e4b10f0e4a2 --- /dev/null +++ b/services/screentransport/screensinktrans/include/iscreen_sink_trans_callback.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ISCREEN_SINK_TRANS_CALLBACK_H +#define OHOS_ISCREEN_SINK_TRANS_CALLBACK_H + +#include + +namespace OHOS { +namespace DistributedHardware { +class IScreenSinkTransCallback { +public: + virtual ~IScreenSinkTransCallback() = default; + + virtual void OnError(int32_t err, const std::string &content) = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensinktrans/include/screen_sink_trans.h b/services/screentransport/screensinktrans/include/screen_sink_trans.h new file mode 100644 index 0000000000000000000000000000000000000000..abd9e75ad826bb0ab7ee003a2d6c087a58bf1442 --- /dev/null +++ b/services/screentransport/screensinktrans/include/screen_sink_trans.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SCREEN_SINK_TRANS_H +#define OHOS_SCREEN_SINK_TRANS_H + +#include +#include + +#include "iscreen_sink_trans.h" +#include "iscreen_sink_trans_callback.h" +#include "iimage_sink_processor.h" +#include "iscreen_channel.h" + +namespace OHOS { +namespace DistributedHardware { +class ScreenSinkTrans : public IScreenSinkTrans, + public IScreenChannelListener, + public IImageSinkProcessorListener, + public std::enable_shared_from_this { +public: + ScreenSinkTrans() = default; + ~ScreenSinkTrans() = default; + + int32_t SetUp(const VideoParam &localParam, const VideoParam &remoteParam, const std::string &peerDevId) override; + int32_t Release() override; + int32_t Start() override; + int32_t Stop() override; + int32_t RegisterStateCallback(const std::shared_ptr &callBack) override; + int32_t SetImageSurface(const sptr &surface) override; + + void OnSessionOpened() override; + void OnSessionClosed() override; + void OnDataReceived(const std::shared_ptr &data) override; + void OnProcessorStateNotify(int32_t state) override; + +private: + int32_t CheckVideoParam(const VideoParam ¶m); + int32_t CheckTransParam(const VideoParam &localParam, const VideoParam &remoteParam, const std::string &peerDevId); + int32_t InitScreenTrans(const VideoParam &localParam, const VideoParam &remoteParam, const std::string &peerDevId); + int32_t RegisterChannelListner(); + int32_t RegisterProcessorListner(const VideoParam &localParam, const VideoParam &remoteParam, + const std::string &peerDevId); + +private: + static const constexpr char *LOG_TAG = "ScreenSinkTrans"; + + sptr decoderSurface_; + std::shared_ptr imageProcessor_; + std::shared_ptr screenChannel_; + std::weak_ptr transCallback_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensinktrans/src/screen_sink_trans.cpp b/services/screentransport/screensinktrans/src/screen_sink_trans.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83b10eeee40fb05b6e4170fa96032cb1a90e20f9 --- /dev/null +++ b/services/screentransport/screensinktrans/src/screen_sink_trans.cpp @@ -0,0 +1,309 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screen_sink_trans.h" + +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "image_sink_processor.h" +#include "screen_data_channel_impl.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t ScreenSinkTrans::SetUp(const VideoParam &localParam, const VideoParam &remoteParam, + const std::string &peerDevId) +{ + DHLOGI("%s: SetUp.", LOG_TAG); + int32_t ret = CheckTransParam(localParam, remoteParam, peerDevId); + if (ret != DH_SUCCESS) { + DHLOGE("%s: SetUp failed param error ret: %d.", LOG_TAG, ret); + return ret; + } + + ret = InitScreenTrans(localParam, remoteParam, peerDevId); + if (ret != DH_SUCCESS) { + DHLOGE("%s: SetUp failed ret: %d.", LOG_TAG, ret); + return ret; + } + + DHLOGI("%s: SetUp success.", LOG_TAG); + return DH_SUCCESS; +} + +int32_t ScreenSinkTrans::Release() +{ + DHLOGI("%s: Release.", LOG_TAG); + if (!imageProcessor_ || !screenChannel_) { + DHLOGE("%s: Processor or channel is null, Setup first.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = imageProcessor_->ReleaseImageProcessor(); + if (ret != DH_SUCCESS) { + DHLOGD("%s: Release image processor failed ret: %d.", LOG_TAG, ret); + } + imageProcessor_ = nullptr; + + ret = screenChannel_->ReleaseSession(); + if (ret != DH_SUCCESS) { + DHLOGD("%s: Release channel session failed ret: %d.", LOG_TAG, ret); + } + screenChannel_ = nullptr; + + DHLOGI("%s: Release success.", LOG_TAG); + return DH_SUCCESS; +} + +int32_t ScreenSinkTrans::Start() +{ + DHLOGI("%s: Start.", LOG_TAG); + if (!imageProcessor_ || !screenChannel_) { + DHLOGE("%s: Processor or channel is null, Setup first.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = imageProcessor_->StartImageProcessor(); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Start image processor failed ret: %d.", LOG_TAG, ret); + return ret; + } + + DHLOGI("%s: Start success.", LOG_TAG); + return DH_SUCCESS; +} + +int32_t ScreenSinkTrans::Stop() +{ + DHLOGI("%s: Stop.", LOG_TAG); + if (!imageProcessor_ || !screenChannel_) { + DHLOGE("%s: Processor or channel is null, Setup first.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + bool stopStatus = true; + int32_t ret = imageProcessor_->StopImageProcessor(); + if (ret != DH_SUCCESS) { + DHLOGD("%s: Stop image processor failed ret: %d.", LOG_TAG, ret); + stopStatus = false; + } + + ret = screenChannel_->CloseSession(); + if (ret != DH_SUCCESS && ret != ERR_DH_SCREEN_TRANS_SESSION_NOT_OPEN) { + DHLOGD("%s: Close Session failed ret: %d.", LOG_TAG, ret); + stopStatus = false; + } + + if (!stopStatus) { + DHLOGE("%s: Stop sink trans failed.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ERROR; + } + + DHLOGI("%s: Stop success.", LOG_TAG); + return DH_SUCCESS; +} + +int32_t ScreenSinkTrans::RegisterStateCallback(const std::shared_ptr &callback) +{ + DHLOGI("%s:RegisterStateCallback.", LOG_TAG); + if (!callback) { + DHLOGE("%s: Trans callback is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + transCallback_ = callback; + + return DH_SUCCESS; +} + +int32_t ScreenSinkTrans::SetImageSurface(const sptr &surface) +{ + if (!surface) { + DHLOGE("%s: Image surface is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + decoderSurface_ = surface; + + return DH_SUCCESS; +} + +int32_t ScreenSinkTrans::CheckVideoParam(const VideoParam ¶m) +{ + if ((param.GetCodecType() != VIDEO_CODEC_TYPE_VIDEO_H264) && + (param.GetCodecType() != VIDEO_CODEC_TYPE_VIDEO_H265)) { + DHLOGE("%s: Invalid codec type.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM; + } + + if ((param.GetVideoFormat() != VIDEO_DATA_FORMAT_YUVI420) && + (param.GetVideoFormat() != VIDEO_DATA_FORMAT_NV12) && + (param.GetVideoFormat() != VIDEO_DATA_FORMAT_NV21)) { + DHLOGE("%s: Invalid video data format.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM; + } + + if ((param.GetVideoWidth() > DSCREEN_MAX_VIDEO_DATA_WIDTH) || + (param.GetVideoHeight() > DSCREEN_MAX_VIDEO_DATA_HEIGHT)) { + DHLOGE("%s: Invalid video data size.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM; + } + + if ((param.GetScreenWidth() > DSCREEN_MAX_SCREEN_DATA_WIDTH) || + (param.GetScreenHeight() > DSCREEN_MAX_SCREEN_DATA_HEIGHT)) { + DHLOGE("%s: Invalid screen data size.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM; + } + + return DH_SUCCESS; +} + +int32_t ScreenSinkTrans::CheckTransParam(const VideoParam &localParam, const VideoParam &remoteParam, + const std::string &peerDevId) +{ + if (peerDevId.empty()) { + DHLOGE("%s: Remote device id is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = CheckVideoParam(localParam); + if (ret != DH_SUCCESS) { + DHLOGE("%s: check localParam param failed.", LOG_TAG); + return ret; + } + + ret = CheckVideoParam(remoteParam); + if (ret != DH_SUCCESS) { + DHLOGE("%s: check remoteParam param failed.", LOG_TAG); + return ret; + } + + DHLOGI("%s: Local: codecType(%u), videoFormat(%u), videoSize(%ux%u), screenSize(%ux%u).", LOG_TAG, + localParam.GetCodecType(), localParam.GetVideoFormat(), localParam.GetVideoWidth(), + localParam.GetVideoHeight(), localParam.GetScreenWidth(), localParam.GetScreenHeight()); + DHLOGI("%s: Remote: codecType(%u), videoFormat(%u), videoSize(%ux%u), screenSize(%ux%u).", LOG_TAG, + remoteParam.GetCodecType(), remoteParam.GetVideoFormat(), remoteParam.GetVideoWidth(), + remoteParam.GetVideoHeight(), remoteParam.GetScreenWidth(), remoteParam.GetScreenHeight()); + return DH_SUCCESS; +} + +int32_t ScreenSinkTrans::InitScreenTrans(const VideoParam &localParam, const VideoParam &remoteParam, + const std::string &peerDevId) +{ + screenChannel_ = std::make_shared(peerDevId); + if (!screenChannel_) { + DHLOGE("%s: Create screen data channel failed.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + int32_t ret = RegisterChannelListner(); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Register channel listener failed.", LOG_TAG); + screenChannel_ = nullptr; + return ret; + } + + imageProcessor_ = std::make_shared(); + if (!imageProcessor_) { + DHLOGE("%s: Create image processor failed.", LOG_TAG); + screenChannel_ = nullptr; + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + ret = RegisterProcessorListner(localParam, remoteParam, peerDevId); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Register processor listener failed.", LOG_TAG); + screenChannel_ = nullptr; + imageProcessor_ = nullptr; + return ret; + } + + return DH_SUCCESS; +} + +int32_t ScreenSinkTrans::RegisterChannelListner() +{ + DHLOGI("%s: RegisterChannelListner.", LOG_TAG); + std::shared_ptr listener = shared_from_this(); + if (!listener) { + DHLOGE("%s: Channel Listener is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = screenChannel_->CreateSession(listener); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Register channel listenner failed ret: %d.", LOG_TAG, ret); + return ret; + } + + return DH_SUCCESS; +} + +int32_t ScreenSinkTrans::RegisterProcessorListner(const VideoParam &localParam, const VideoParam &remoteParam, + const std::string &peerDevId) +{ + DHLOGI("%s: RegisterProcessorListner.", LOG_TAG); + std::shared_ptr listener = shared_from_this(); + if (!listener) { + DHLOGE("%s: Channel listener to null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = imageProcessor_->ConfigureImageProcessor(localParam, remoteParam, listener); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Config image processor failed ret: %d.", LOG_TAG, ret); + return ret; + } + + ret = imageProcessor_->SetImageSurface(decoderSurface_); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Set image surface failed ret: %d.", LOG_TAG, ret); + return ret; + } + return DH_SUCCESS; +} + +void ScreenSinkTrans::OnSessionOpened() +{ + DHLOGI("%s: OnChannelSessionOpened.", LOG_TAG); +} + +void ScreenSinkTrans::OnSessionClosed() +{ + DHLOGI("%s:OnChannelSessionClosed.", LOG_TAG); + std::shared_ptr callback = transCallback_.lock(); + if (!callback) { + DHLOGE("%s: Trans callback is null.", LOG_TAG); + return; + } + callback->OnError(ERR_DH_SCREEN_TRANS_SESSION_CLOSED, "OnChannelSessionClosed"); +} + +void ScreenSinkTrans::OnDataReceived(const std::shared_ptr &data) +{ + DHLOGD("%s: OnChannelDataReceived.", LOG_TAG); + int32_t ret = imageProcessor_->ProcessImage(data); + if (ret != DH_SUCCESS) { + DHLOGE("%s: send data to image processor failed ret: %d.", LOG_TAG, ret); + } +} + +void ScreenSinkTrans::OnProcessorStateNotify(int32_t state) +{ + DHLOGI("%s: OnProcessorStateNotify.", LOG_TAG); + std::shared_ptr callback = transCallback_.lock(); + if (!callback) { + DHLOGE("%s: Trans callback is null.", LOG_TAG); + return; + } + callback->OnError(state, "OnProcessorStateNotify"); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screentransport/screensourceprocessor/encoder/include/image_encoder_callback.h b/services/screentransport/screensourceprocessor/encoder/include/image_encoder_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..b3410caf84983e24ddd43479cad5190b8f435371 --- /dev/null +++ b/services/screentransport/screensourceprocessor/encoder/include/image_encoder_callback.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_IMAGE_ENCODER_CALLBACK_H +#define OHOS_IMAGE_ENCODER_CALLBACK_H + +#include + +#include "media_errors.h" +#include "avcodec_common.h" +#include "format.h" + +namespace OHOS { +namespace DistributedHardware { +class ImageSourceEncoder; +class ImageEncoderCallback : public Media::AVCodecCallback { +public: + explicit ImageEncoderCallback(const std::shared_ptr &encoder) + : videoEncoder_(encoder) {}; + ~ImageEncoderCallback() = default; + + void OnError(Media::AVCodecErrorType errorType, int32_t errorCode) override; + void OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info, Media::AVCodecBufferFlag flag) override; + void OnInputBufferAvailable(uint32_t index) override; + void OnOutputFormatChanged(const Media::Format &format) override; + +private: + static const constexpr char *LOG_TAG = "ImageEncoderCallback"; + + std::weak_ptr videoEncoder_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensourceprocessor/encoder/include/image_source_encoder.h b/services/screentransport/screensourceprocessor/encoder/include/image_source_encoder.h new file mode 100644 index 0000000000000000000000000000000000000000..bcf6fa55269534b6fa867a3d4b906c14671f9f0e --- /dev/null +++ b/services/screentransport/screensourceprocessor/encoder/include/image_source_encoder.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_IMAGE_SOURCE_ENCODER_H +#define OHOS_IMAGE_SOURCE_ENCODER_H + +#include + +#include "avsharedmemory.h" +#include "avcodec_video_encoder.h" +#include "avcodec_common.h" +#include "media_errors.h" +#include "format.h" +#include "surface.h" + +#include "iimage_source_processor_listener.h" +#include "image_encoder_callback.h" +#include "video_param.h" + +namespace OHOS { +namespace DistributedHardware { +class ImageSourceEncoder : public std::enable_shared_from_this { +public: + explicit ImageSourceEncoder(const std::shared_ptr &imageListener) + : imageProcessorListener_(imageListener) {}; + ~ImageSourceEncoder() = default; + + int32_t ConfigureEncoder(const VideoParam &cofigParam); + int32_t ReleaseEncoder(); + int32_t StartEncoder(); + int32_t StopEncoder(); + sptr &GetInputSurface() + { + return videoSurface_; + } + + void OnError(Media::AVCodecErrorType errorType, int32_t errorCode); + void OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info, Media::AVCodecBufferFlag flag); + void OnInputBufferAvailable(uint32_t index); + void OnOutputFormatChanged(const Media::Format &format); + +private: + int32_t InitVideoEncoder(const VideoParam &configParam); + int32_t SetEncoderFormat(const VideoParam &cofigParam); + +private: + static const constexpr char *LOG_TAG = "ImageSourceEncoder"; + + Media::Format imageFormat_; + Media::AVCodecBufferInfo encoderBufferInfo_; + + sptr videoSurface_; + std::shared_ptr videoEncoder_; + std::shared_ptr videoSharedMemory_; + std::shared_ptr encodeVideoCallback_; + std::weak_ptr imageProcessorListener_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensourceprocessor/encoder/src/image_encoder_callback.cpp b/services/screentransport/screensourceprocessor/encoder/src/image_encoder_callback.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cfe9ff9215c7a6cb5dcd9e5ed6f088abd355713 --- /dev/null +++ b/services/screentransport/screensourceprocessor/encoder/src/image_encoder_callback.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "image_encoder_callback.h" + +#include "dscreen_log.h" +#include "image_source_encoder.h" + +namespace OHOS { +namespace DistributedHardware { +void ImageEncoderCallback::OnError(Media::AVCodecErrorType errorType, int32_t errorCode) +{ + DHLOGD("%s: OnError.", LOG_TAG); + std::shared_ptr encoder = videoEncoder_.lock(); + if (encoder == nullptr) { + DHLOGE("encoder is nullptr."); + return; + } + encoder->OnError(errorType, errorCode); +} + +void ImageEncoderCallback::OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info, + Media::AVCodecBufferFlag flag) +{ + DHLOGD("%s: OnOutputBufferAvailable.", LOG_TAG); + std::shared_ptr encoder = videoEncoder_.lock(); + if (encoder == nullptr) { + DHLOGE("encoder is nullptr."); + return; + } + encoder->OnOutputBufferAvailable(index, info, flag); +} + +void ImageEncoderCallback::OnInputBufferAvailable(uint32_t index) +{ + DHLOGD("%s: OnInputBufferAvailable.", LOG_TAG); + std::shared_ptr encoder = videoEncoder_.lock(); + if (encoder == nullptr) { + DHLOGE("encoder is nullptr."); + return; + } + encoder->OnInputBufferAvailable(index); +} + +void ImageEncoderCallback::OnOutputFormatChanged(const Media::Format &format) +{ + DHLOGD("%s: OnOutputFormatChanged.", LOG_TAG); + std::shared_ptr encoder = videoEncoder_.lock(); + if (encoder == nullptr) { + DHLOGE("encoder is nullptr."); + return; + } + encoder->OnOutputFormatChanged(format); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screentransport/screensourceprocessor/encoder/src/image_source_encoder.cpp b/services/screentransport/screensourceprocessor/encoder/src/image_source_encoder.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fb31b4bc36185e42458d6a7fc6de886c929e569 --- /dev/null +++ b/services/screentransport/screensourceprocessor/encoder/src/image_source_encoder.cpp @@ -0,0 +1,250 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "image_source_encoder.h" + +#include + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t ImageSourceEncoder::ConfigureEncoder(const VideoParam &configParam) +{ + DHLOGI("%s: ConfigureEncoder.", LOG_TAG); + int32_t ret = InitVideoEncoder(configParam); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Init encoder failed ret:%d.", LOG_TAG, ret); + return ret; + } + + ret = SetEncoderFormat(configParam); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Set encoder format failed ret:%d.", LOG_TAG, ret); + return ret; + } + + videoSurface_ = videoEncoder_->CreateInputSurface(); + if (videoSurface_ == nullptr) { + DHLOGE("%s: Create encoder surface failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_SURFACE_ERROR; + } + + return DH_SUCCESS; +} + +int32_t ImageSourceEncoder::ReleaseEncoder() +{ + DHLOGI("%s: ReleaseEncoder.", LOG_TAG); + if (!videoEncoder_) { + DHLOGE("%s: Encoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = videoEncoder_->Release(); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: Release encoder failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_RELEASE_FAILED; + } + encodeVideoCallback_ = nullptr; + videoEncoder_ = nullptr; + + return DH_SUCCESS; +} + +int32_t ImageSourceEncoder::StartEncoder() +{ + DHLOGI("%s: StartEncoder.", LOG_TAG); + if (!videoEncoder_) { + DHLOGE("%s: Encoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = videoEncoder_->Prepare(); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: Prepare encoder failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_PREPARE_FAILED; + } + + ret = videoEncoder_->Start(); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: Start encoder failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_START_FAILED; + } + + return DH_SUCCESS; +} + +int32_t ImageSourceEncoder::StopEncoder() +{ + DHLOGI("%s: StopEncoder.", LOG_TAG); + if (!videoEncoder_) { + DHLOGE("%s: Encoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = videoEncoder_->Flush(); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: Flush encoder failed.", LOG_TAG); + } + + ret = videoEncoder_->Stop(); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: Stop encoder failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_STOP_FAILED; + } + + return DH_SUCCESS; +} + +int32_t ImageSourceEncoder::InitVideoEncoder(const VideoParam &configParam) +{ + DHLOGI("%s: InitVideoEncoder.", LOG_TAG); + switch (configParam.GetCodecType()) { + case VIDEO_CODEC_TYPE_VIDEO_H264: + videoEncoder_ = Media::VideoEncoderFactory::CreateByName("OMX_hisi_video_encoder_avc"); + break; + case VIDEO_CODEC_TYPE_VIDEO_H265: + videoEncoder_ = Media::VideoEncoderFactory::CreateByMime("video/hevc"); + break; + default: + DHLOGE("%s: codecType is invalid!", LOG_TAG); + videoEncoder_ = nullptr; + } + + if (videoEncoder_ == nullptr) { + DHLOGE("%s: Create videoEncoder failed.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_CREATE_CODEC_FAILED; + } + + encodeVideoCallback_ = std::make_shared(shared_from_this()); + int32_t ret = videoEncoder_->SetCallback(encodeVideoCallback_); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: Set codec callback failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_SET_CALLBACK_FAILED; + } + + return DH_SUCCESS; +} + +int32_t ImageSourceEncoder::SetEncoderFormat(const VideoParam &configParam) +{ + DHLOGI("%s: SetEncoderFormat.", LOG_TAG); + if (!videoEncoder_) { + DHLOGE("%s: Encoder is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + switch (configParam.GetCodecType()) { + case VIDEO_CODEC_TYPE_VIDEO_H264: + imageFormat_.PutStringValue("codec_mime", "video/avc"); + break; + case VIDEO_CODEC_TYPE_VIDEO_H265: + imageFormat_.PutStringValue("codec_mime", "video/hevc"); + break; + default: + DHLOGE("%s: Codec type is invalid.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM; + } + switch (configParam.GetVideoFormat()) { + case VIDEO_DATA_FORMAT_YUVI420: + imageFormat_.PutIntValue("pixel_format", Media::VideoPixelFormat::YUVI420); + break; + case VIDEO_DATA_FORMAT_NV12: + imageFormat_.PutIntValue("pixel_format", Media::VideoPixelFormat::NV12); + break; + case VIDEO_DATA_FORMAT_NV21: + imageFormat_.PutIntValue("pixel_format", Media::VideoPixelFormat::NV21); + break; + default: + DHLOGE("%s: Video format is invalid.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM; + } + imageFormat_.PutLongValue("max_input_size", MAX_YUV420_BUFFER_SIZE); + imageFormat_.PutIntValue("width", configParam.GetVideoWidth()); + imageFormat_.PutIntValue("height", configParam.GetVideoHeight()); + imageFormat_.PutIntValue("frame_rate", configParam.GetFps()); + + int32_t ret = videoEncoder_->Configure(imageFormat_); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: Configure encoder failed.", LOG_TAG); + return ERR_DH_SCREEN_CODEC_CONFIGURE_FAILED; + } + return DH_SUCCESS; +} + +void ImageSourceEncoder::OnError(Media::AVCodecErrorType errorType, int32_t errorCode) +{ + DHLOGI("%s: Encoder error, errorType(%d), errorCode(%d)", LOG_TAG, errorType, errorCode); + std::shared_ptr listener = imageProcessorListener_.lock(); + if (!listener) { + DHLOGE("%s: Processor listener is null", LOG_TAG); + return; + } + listener->OnProcessorStateNotify(errorCode); +} + +void ImageSourceEncoder::OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info, + Media::AVCodecBufferFlag flag) +{ + DHLOGD("%s: OnOutputBufferAvailable.", LOG_TAG); + std::shared_ptr listener = imageProcessorListener_.lock(); + if (!listener) { + DHLOGE("%s: Processor listener is null", LOG_TAG); + return; + } + if (!videoEncoder_) { + DHLOGE("%s: Encoder is null.", LOG_TAG); + return; + } + + encoderBufferInfo_ = info; + videoSharedMemory_ = videoEncoder_->GetOutputBuffer(index); + if (!videoSharedMemory_) { + DHLOGE("%s: GetOutputBuffer failed.", LOG_TAG); + return; + } + + size_t dataSize = static_cast(info.size); + auto dataBuf = std::make_shared(dataSize); + if (!dataBuf) { + DHLOGE("%s: Create buffer failed.", LOG_TAG); + } + int32_t ret = memcpy_s(dataBuf->Data(), dataBuf->Capacity(), videoSharedMemory_->GetBase(), dataSize); + if (ret != EOK) { + DHLOGE("%s: Copy data failed.", LOG_TAG); + return; + } + listener->OnImageProcessDone(dataBuf); + + ret = videoEncoder_->ReleaseOutputBuffer(index); + if (ret != Media::MSERR_OK) { + DHLOGE("%s: videoEncoder ReleaseOutputBuffer failed.", LOG_TAG); + } +} + +void ImageSourceEncoder::OnInputBufferAvailable(uint32_t index) +{ + (void) index; +} + +void ImageSourceEncoder::OnOutputFormatChanged(const Media::Format &format) +{ + (void) format; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screentransport/screensourceprocessor/include/iimage_source_processor.h b/services/screentransport/screensourceprocessor/include/iimage_source_processor.h new file mode 100644 index 0000000000000000000000000000000000000000..06dad68e75c93ca95f7ced1c41f0d8c4cc777b1a --- /dev/null +++ b/services/screentransport/screensourceprocessor/include/iimage_source_processor.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_IIMAGE_SOURCE_PROCESSOR_H +#define OHOS_IIMAGE_SOURCE_PROCESSOR_H + +#include "surface.h" + +#include + +#include "data_buffer.h" +#include "iimage_source_processor_listener.h" +#include "video_param.h" + +namespace OHOS { +namespace DistributedHardware { +class IImageSourceProcessor { +public: + virtual ~IImageSourceProcessor() = default; + + virtual int32_t ConfigureImageProcessor(const VideoParam &localParam, const VideoParam &remoteParam, + const std::shared_ptr &listener) = 0; + virtual int32_t ReleaseImageProcessor() = 0; + virtual int32_t StartImageProcessor() = 0; + virtual int32_t StopImageProcessor() = 0; + virtual sptr &GetImageSurface() = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensourceprocessor/include/iimage_source_processor_listener.h b/services/screentransport/screensourceprocessor/include/iimage_source_processor_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..eab3f25a226944807104678fc6e67b8e39b9ecf6 --- /dev/null +++ b/services/screentransport/screensourceprocessor/include/iimage_source_processor_listener.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_IIMAGE_SOURCE_PROCESSOR_LISTENER_H +#define OHOS_IIMAGE_SOURCE_PROCESSOR_LISTENER_H + +#include "data_buffer.h" + +namespace OHOS { +namespace DistributedHardware { +class IImageSourceProcessorListener { +public: + virtual ~IImageSourceProcessorListener() = default; + + virtual void OnImageProcessDone(const std::shared_ptr &data) = 0; + virtual void OnProcessorStateNotify(int32_t state) = 0; +}; +} // namespace DistributedHardware +} // namsspace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensourceprocessor/include/image_source_processor.h b/services/screentransport/screensourceprocessor/include/image_source_processor.h new file mode 100644 index 0000000000000000000000000000000000000000..b95f9c14d54201865a5238c656f4b08bece72420 --- /dev/null +++ b/services/screentransport/screensourceprocessor/include/image_source_processor.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_IMAGE_SOURCE_PROCESSOR_H +#define OHOS_IMAGE_SOURCE_PROCESSOR_H + +#include "iimage_source_processor.h" +#include "iimage_source_processor_listener.h" +#include "image_source_encoder.h" +#include "video_param.h" + +namespace OHOS { +namespace DistributedHardware { +class ImageSourceProcessor : public IImageSourceProcessor { +public: + ImageSourceProcessor() = default; + ~ImageSourceProcessor() = default; + + int32_t ConfigureImageProcessor(const VideoParam &localParam, const VideoParam &remoteParam, + const std::shared_ptr &listener) override; + int32_t ReleaseImageProcessor() override; + int32_t StartImageProcessor() override; + int32_t StopImageProcessor() override; + sptr &GetImageSurface() override; + +private: + static const constexpr char *LOG_TAG = "ImageSourceProcessor"; + VideoParam localParam_; + VideoParam remoteParam_; + std::shared_ptr imageEncoder_ = nullptr; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensourceprocessor/src/image_source_processor.cpp b/services/screentransport/screensourceprocessor/src/image_source_processor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68e74783291262b99dff9699473f152d242cb0d2 --- /dev/null +++ b/services/screentransport/screensourceprocessor/src/image_source_processor.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "image_source_processor.h" + +#include "dscreen_errcode.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t ImageSourceProcessor::ConfigureImageProcessor(const VideoParam &localParam, const VideoParam &remoteParam, + const std::shared_ptr &listener) +{ + DHLOGI("%s: ConfigureImageProcessor.", LOG_TAG); + imageEncoder_ = std::make_shared(listener); + if (!imageEncoder_) { + DHLOGE("%s: Create screen encoder failed.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = imageEncoder_->ConfigureEncoder(localParam); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Configure screen encoder failed ret: %d.", LOG_TAG, ret); + return ret; + } + + localParam_ = localParam; + remoteParam_ = remoteParam; + return DH_SUCCESS; +} + +int32_t ImageSourceProcessor::ReleaseImageProcessor() +{ + DHLOGI("%s: ReleaseImageProcessor.", LOG_TAG); + if (!imageEncoder_) { + DHLOGE("%s: Create screen encoder failed.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = imageEncoder_->ReleaseEncoder(); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Release screen encoder failed ret: %d.", LOG_TAG, ret); + return ret; + } + + return DH_SUCCESS; +} + +int32_t ImageSourceProcessor::StartImageProcessor() +{ + DHLOGI("%s: StartImageProcessor.", LOG_TAG); + if (!imageEncoder_) { + DHLOGE("%s: Create screen encoder failed.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = imageEncoder_->StartEncoder(); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Start screen encoder failed ret: %d.", LOG_TAG, ret); + return ret; + } + + return DH_SUCCESS; +} + +int32_t ImageSourceProcessor::StopImageProcessor() +{ + DHLOGI("%s: StopImageProcessor.", LOG_TAG); + if (!imageEncoder_) { + DHLOGE("%s: Create screen encoder failed.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = imageEncoder_->StopEncoder(); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Stop screen encoder failed ret: %d.", LOG_TAG, ret); + return ret; + } + + return DH_SUCCESS; +} + +sptr &ImageSourceProcessor::GetImageSurface() +{ + DHLOGI("%s: GetImageSurface.", LOG_TAG); + return imageEncoder_->GetInputSurface(); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screentransport/screensourcetrans/BUILD.gn b/services/screentransport/screensourcetrans/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..09e5afa141078e7cf1337a8e545b7fd8220eb7b6 --- /dev/null +++ b/services/screentransport/screensourcetrans/BUILD.gn @@ -0,0 +1,69 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +ohos_shared_library("distributed_screen_sourcetrans") { + include_dirs = [ + "//third_party/json/include", + "//foundation/graphic/standard/interfaces/innerkits/surface", + "${fwk_common_path}/utils/include", + ] + + include_dirs += [ + "./include", + "${common_path}/include", + "${services_path}/common/databuffer/include", + "${services_path}/common/screen_channel/include", + "${services_path}/common/utils/include", + "${services_path}/screentransport/screendatachannel/include", + "${services_path}/screentransport/screensourceprocessor/include", + "${services_path}/screentransport/screensourceprocessor/encoder/include", + "${services_path}/softbusadapter/include", + ] + + sources = [ + "./src/screen_source_trans.cpp", + "${services_path}/common/databuffer/src/data_buffer.cpp", + "${services_path}/common/utils/src/video_param.cpp", + "${services_path}/screentransport/screendatachannel/src/screen_data_channel_impl.cpp", + "${services_path}/screentransport/screensourceprocessor/src/image_source_processor.cpp", + "${services_path}/screentransport/screensourceprocessor/encoder/src/image_source_encoder.cpp", + "${services_path}/screentransport/screensourceprocessor/encoder/src/image_encoder_callback.cpp", + "${services_path}/softbusadapter/src/softbus_adapter.cpp", + ] + + deps = [ + "${common_path}:distributed_screen_utils", + "//foundation/graphic/standard/frameworks/surface:surface", + "//utils/native/base:utils", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dscreensourcetrans\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "dsoftbus_standard:softbus_client", + "hiviewdfx_hilog_native:libhilog", + "multimedia_media_standard:media_client", + ] + + subsystem_name = "distributedhardware" + + part_name = "distributed_screen" +} \ No newline at end of file diff --git a/services/screentransport/screensourcetrans/include/iscreen_source_trans.h b/services/screentransport/screensourcetrans/include/iscreen_source_trans.h new file mode 100644 index 0000000000000000000000000000000000000000..2b3c8e7ffac44f6668e91811b8d51f30775e78a7 --- /dev/null +++ b/services/screentransport/screensourcetrans/include/iscreen_source_trans.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ISCREEN_SOURCE_TRANS_H +#define OHOS_ISCREEN_SOURCE_TRANS_H + +#include "surface.h" + +#include "iscreen_source_trans_callback.h" +#include "video_param.h" + +namespace OHOS { +namespace DistributedHardware { +class IScreenSourceTrans { +public: + virtual ~IScreenSourceTrans() = default; + + virtual int32_t SetUp(const VideoParam &localParam, const VideoParam &remoteParam, + const std::string &peerDevId) = 0; + virtual int32_t Release() = 0; + virtual int32_t Start() = 0; + virtual int32_t Stop() = 0; + virtual int32_t RegisterStateCallback(const std::shared_ptr &callBack) = 0; + virtual sptr &GetImageSurface() = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensourcetrans/include/iscreen_source_trans_callback.h b/services/screentransport/screensourcetrans/include/iscreen_source_trans_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..fafba69a680417b07d192160e8cd45fdcda50848 --- /dev/null +++ b/services/screentransport/screensourcetrans/include/iscreen_source_trans_callback.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ISCREEN_SOURCE_TRANS_CALLBACK_H +#define OHOS_ISCREEN_SOURCE_TRANS_CALLBACK_H + +#include + +namespace OHOS { +namespace DistributedHardware { +class IScreenSourceTransCallback { +public: + virtual ~IScreenSourceTransCallback() = default; + + virtual void OnError(int32_t err, const std::string &content) = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensourcetrans/include/screen_source_trans.h b/services/screentransport/screensourcetrans/include/screen_source_trans.h new file mode 100644 index 0000000000000000000000000000000000000000..b513c73951b51a0f1ec7542b69c708e2774c8338 --- /dev/null +++ b/services/screentransport/screensourcetrans/include/screen_source_trans.h @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SCREEN_SOURCE_TRANS_H +#define OHOS_SCREEN_SOURCE_TRANS_H + +#include +#include +#include +#include +#include +#include + +#include "iimage_source_processor.h" +#include "iscreen_source_trans.h" +#include "iscreen_source_trans_callback.h" +#include "iscreen_channel.h" + +namespace OHOS { +namespace DistributedHardware { +class ScreenSourceTrans : public IScreenSourceTrans, + public IScreenChannelListener, + public IImageSourceProcessorListener, + public std::enable_shared_from_this { +public: + ScreenSourceTrans() = default; + ~ScreenSourceTrans() = default; + + int32_t SetUp(const VideoParam &localParam, const VideoParam &remoteParam, const std::string &peerDevId) override; + int32_t Release() override; + int32_t Start() override; + int32_t Stop() override; + int32_t RegisterStateCallback(const std::shared_ptr &callBack) override; + sptr &GetImageSurface() override; + + void OnSessionOpened() override; + void OnSessionClosed() override; + void OnDataReceived(const std::shared_ptr &data) override; + void OnImageProcessDone(const std::shared_ptr &data) override; + void OnProcessorStateNotify(int32_t state) override; + +private: + int32_t CheckVideoParam(const VideoParam ¶m); + int32_t CheckTransParam(const VideoParam &localParam, const VideoParam &remoteParam, const std::string &peerDevId); + int32_t InitScreenTrans(const VideoParam &localParam, const VideoParam &remoteParam, const std::string &peerDevId); + int32_t RegisterChannelListner(); + int32_t RegisterProcessorListner(const VideoParam &localParam, const VideoParam &remoteParam); + void FeedChannelData(); + +private: + static const constexpr char *LOG_TAG = "ScreenSourceTrans"; + static constexpr uint8_t SESSION_WAIT_SECONDS = 5; + static constexpr uint8_t DATA_WAIT_SECONDS = 1; + static constexpr size_t DATA_QUEUE_MAX_SIZE = 10; + + std::mutex sessionMtx_; + std::mutex dataMtx_; + std::condition_variable sessionCond_; + std::condition_variable dataCond_; + std::mutex dataQueueMtx_; + std::thread sendDataThread_; + + bool isChannelReady_ = false; + sptr encoderSurface_; + std::queue> dataQueue_; + + std::shared_ptr imageProcessor_; + std::shared_ptr screenChannel_; + std::weak_ptr transCallback_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/screentransport/screensourcetrans/src/screen_source_trans.cpp b/services/screentransport/screensourcetrans/src/screen_source_trans.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13faed9bccaf1a5f344f52d6792243a0dea69900 --- /dev/null +++ b/services/screentransport/screensourcetrans/src/screen_source_trans.cpp @@ -0,0 +1,382 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screen_source_trans.h" + +#include + +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "image_source_processor.h" +#include "screen_data_channel_impl.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t ScreenSourceTrans::SetUp(const VideoParam &localParam, const VideoParam &remoteParam, + const std::string &peerDevId) +{ + DHLOGI("%s: SetUp.", LOG_TAG); + int32_t ret = CheckTransParam(localParam, remoteParam, peerDevId); + if (ret != DH_SUCCESS) { + DHLOGE("%s: SetUp failed param error ret: %d.", LOG_TAG, ret); + return ret; + } + + ret = InitScreenTrans(localParam, remoteParam, peerDevId); + if (ret != DH_SUCCESS) { + DHLOGE("%s: SetUp failed ret: %d.", LOG_TAG, ret); + return ret; + } + + DHLOGI("%s: SetUp success.", LOG_TAG); + return DH_SUCCESS; +} + +int32_t ScreenSourceTrans::Release() +{ + DHLOGI("%s: Release.", LOG_TAG); + if (!imageProcessor_ || !screenChannel_) { + DHLOGE("%s: Processor or channel is null, Setup first.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = imageProcessor_->ReleaseImageProcessor(); + if (ret != DH_SUCCESS) { + DHLOGD("%s: Release image processor failed ret: %d.", LOG_TAG, ret); + } + imageProcessor_ = nullptr; + + ret = screenChannel_->ReleaseSession(); + if (ret != DH_SUCCESS) { + DHLOGD("%s: Release channel session failed ret: %d.", LOG_TAG, ret); + } + screenChannel_ = nullptr; + + std::lock_guard lck(dataQueueMtx_); + while (!dataQueue_.empty()) { + dataQueue_.pop(); + } + + DHLOGI("%s: Release success.", LOG_TAG); + return DH_SUCCESS; +} + +int32_t ScreenSourceTrans::Start() +{ + DHLOGI("%s: Start.", LOG_TAG); + if (!imageProcessor_ || !screenChannel_) { + DHLOGE("%s: Processor or channel is null, Setup first.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = screenChannel_->OpenSession(); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Open channel session failed ret: %d.", LOG_TAG, ret); + return ret; + } + + DHLOGI("%s: Wait for channel session opened.", LOG_TAG); + std::unique_lock lck(sessionMtx_); + auto status = + sessionCond_.wait_for(lck, std::chrono::seconds(SESSION_WAIT_SECONDS), [this]() { return isChannelReady_; }); + if (!status) { + DHLOGE("%s: Open channel session timeout(%ds).", LOG_TAG, SESSION_WAIT_SECONDS); + return ERR_DH_SCREEN_TRANS_TIMEOUT; + } + + DHLOGI("%s: Start success.", LOG_TAG); + return DH_SUCCESS; +} + +int32_t ScreenSourceTrans::Stop() +{ + DHLOGI("%s: Stop.", LOG_TAG); + if (!imageProcessor_ || !screenChannel_) { + DHLOGE("%s: Processor or channel is null, Setup first.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + bool stopStatus = true; + int32_t ret = imageProcessor_->StopImageProcessor(); + if (ret != DH_SUCCESS) { + DHLOGD("%s: Stop image processor failed ret: %d.", LOG_TAG, ret); + stopStatus = false; + } + + ret = screenChannel_->CloseSession(); + if (ret != DH_SUCCESS) { + DHLOGD("%s: Close Session failed ret: %d.", LOG_TAG, ret); + stopStatus = false; + } + isChannelReady_ = false; + sendDataThread_.join(); + + if (!stopStatus) { + DHLOGE("%s: Stop source trans failed.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ERROR; + } + DHLOGI("%s: Stop success.", LOG_TAG); + return DH_SUCCESS; +} + +int32_t ScreenSourceTrans::RegisterStateCallback(const std::shared_ptr &callback) +{ + DHLOGI("%s:RegisterStateCallback.", LOG_TAG); + if (!callback) { + DHLOGE("%s: Trans callback is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + transCallback_ = callback; + + return DH_SUCCESS; +} + +sptr &ScreenSourceTrans::GetImageSurface() +{ + DHLOGI("%s:GetImageSurface.", LOG_TAG); + return encoderSurface_; +} + +int32_t ScreenSourceTrans::CheckVideoParam(const VideoParam ¶m) +{ + if ((param.GetCodecType() != VIDEO_CODEC_TYPE_VIDEO_H264) && + (param.GetCodecType() != VIDEO_CODEC_TYPE_VIDEO_H265)) { + DHLOGE("%s: Invalid codec type.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM; + } + + if ((param.GetVideoFormat() != VIDEO_DATA_FORMAT_YUVI420) && + (param.GetVideoFormat() != VIDEO_DATA_FORMAT_NV12) && + (param.GetVideoFormat() != VIDEO_DATA_FORMAT_NV21)) { + DHLOGE("%s: Invalid video data format.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM; + } + + if ((param.GetVideoWidth() > DSCREEN_MAX_VIDEO_DATA_WIDTH) || + (param.GetVideoHeight() > DSCREEN_MAX_VIDEO_DATA_HEIGHT)) { + DHLOGE("%s: Invalid video data size.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM; + } + + if ((param.GetScreenWidth() > DSCREEN_MAX_SCREEN_DATA_WIDTH) || + (param.GetScreenHeight() > DSCREEN_MAX_SCREEN_DATA_HEIGHT)) { + DHLOGE("%s: Invalid screen data size.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM; + } + + return DH_SUCCESS; +} + +int32_t ScreenSourceTrans::CheckTransParam(const VideoParam &localParam, const VideoParam &remoteParam, + const std::string &peerDevId) +{ + if (peerDevId.empty()) { + DHLOGE("%s: Remote device id is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + int32_t ret = CheckVideoParam(localParam); + if (ret != DH_SUCCESS) { + DHLOGE("%s: check localParam param failed.", LOG_TAG); + return ret; + } + + ret = CheckVideoParam(remoteParam); + if (ret != DH_SUCCESS) { + DHLOGE("%s: check remoteParam param failed.", LOG_TAG); + return ret; + } + + DHLOGI("%s: Local: codecType(%u), videoFormat(%u), videoSize(%ux%u), screenSize(%ux%u).", LOG_TAG, + localParam.GetCodecType(), localParam.GetVideoFormat(), localParam.GetVideoWidth(), + localParam.GetVideoHeight(), localParam.GetScreenWidth(), localParam.GetScreenHeight()); + DHLOGI("%s: Remote: codecType(%u), videoFormat(%u), videoSize(%ux%u), screenSize(%ux%u).", LOG_TAG, + remoteParam.GetCodecType(), remoteParam.GetVideoFormat(), remoteParam.GetVideoWidth(), + remoteParam.GetVideoHeight(), remoteParam.GetScreenWidth(), remoteParam.GetScreenHeight()); + return DH_SUCCESS; +} + +int32_t ScreenSourceTrans::InitScreenTrans(const VideoParam &localParam, const VideoParam &remoteParam, + const std::string &peerDevId) +{ + screenChannel_ = std::make_shared(peerDevId); + if (!screenChannel_) { + DHLOGE("%s: Create screen data channel failed.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + int32_t ret = RegisterChannelListner(); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Register channel listener failed ret: %d.", LOG_TAG, ret); + screenChannel_ = nullptr; + return ret; + } + + imageProcessor_ = std::make_shared(); + if (!imageProcessor_) { + DHLOGE("%s: Create image processor failed.", LOG_TAG); + screenChannel_ = nullptr; + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + ret = RegisterProcessorListner(localParam, remoteParam); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Register data processor listener failed ret: %d.", LOG_TAG, ret); + screenChannel_ = nullptr; + imageProcessor_ = nullptr; + return ret; + } + + return DH_SUCCESS; +} + +int32_t ScreenSourceTrans::RegisterChannelListner() +{ + DHLOGI("%s: RegisterChannelListner.", LOG_TAG); + std::shared_ptr listener = shared_from_this(); + if (!listener) { + DHLOGE("%s: Channel listener is null", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + if (screenChannel_ == nullptr) { + DHLOGE("%s: Channel is null", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + int32_t ret = screenChannel_->CreateSession(listener); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Create session failed ret: %d.", LOG_TAG); + return ret; + } + + return DH_SUCCESS; +} + +int32_t ScreenSourceTrans::RegisterProcessorListner(const VideoParam &localParam, const VideoParam &remoteParam) +{ + DHLOGI("%s: RegisterProcessorListner.", LOG_TAG); + std::shared_ptr listener = shared_from_this(); + if (!listener) { + DHLOGE("%s: Processor listener is null", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ERROR; + } + + int32_t ret = imageProcessor_->ConfigureImageProcessor(localParam, remoteParam, listener); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Config image processor failed ret: %d.", LOG_TAG, ret); + return ret; + } + + encoderSurface_ = imageProcessor_->GetImageSurface(); + if (!encoderSurface_) { + DHLOGE("%s: Surface is null.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_NULL_VALUE; + } + + return DH_SUCCESS; +} + +void ScreenSourceTrans::OnSessionOpened() +{ + DHLOGI("%s: OnChannelSessionOpened.", LOG_TAG); + int32_t ret = imageProcessor_->StartImageProcessor(); + if (ret != DH_SUCCESS) { + DHLOGE("%s: Start image processor failed ret: %d.", LOG_TAG, ret); + return; + } + + isChannelReady_ = true; + DHLOGI("%s: Start thread.", LOG_TAG); + sendDataThread_ = std::thread(&ScreenSourceTrans::FeedChannelData, this); + + std::unique_lock lck(sessionMtx_); + sessionCond_.notify_all(); +} + +void ScreenSourceTrans::OnSessionClosed() +{ + DHLOGI("%s: OnChannelSessionClosed.", LOG_TAG); + isChannelReady_ = false; + sendDataThread_.join(); + + std::shared_ptr callback = transCallback_.lock(); + if (!callback) { + DHLOGE("%s: Trans callback is null.", LOG_TAG); + return; + } + callback->OnError(ERR_DH_SCREEN_TRANS_SESSION_CLOSED, "OnChannelSessionClosed"); +} + +void ScreenSourceTrans::OnDataReceived(const std::shared_ptr &data) +{ + (void) data; + DHLOGI("%s: OnChannelDataReceived source trans not support.", LOG_TAG); +} + +void ScreenSourceTrans::OnImageProcessDone(const std::shared_ptr &data) +{ + DHLOGD("%s: OnProcessorDataReceived received data from data processor.", LOG_TAG); + std::lock_guard lck(dataQueueMtx_); + while (dataQueue_.size() >= DATA_QUEUE_MAX_SIZE) { + DHLOGE("%s: Data queue overflow.", LOG_TAG); + dataQueue_.pop(); + } + dataQueue_.push(data); + dataCond_.notify_all(); +} + +void ScreenSourceTrans::OnProcessorStateNotify(int32_t state) +{ + DHLOGI("%s:OnProcessorStateNotify.", LOG_TAG); + std::shared_ptr callback = transCallback_.lock(); + if (!callback) { + DHLOGE("%s: Trans callback is null.", LOG_TAG); + return; + } + callback->OnError(state, "OnProcessorStateNotify"); +} + +void ScreenSourceTrans::FeedChannelData() +{ + while (isChannelReady_) { + std::shared_ptr screenData; + { + std::unique_lock lock(dataQueueMtx_); + dataCond_.wait_for(lock, std::chrono::seconds(DATA_WAIT_SECONDS), [this]() { return !dataQueue_.empty(); }); + if (dataQueue_.empty()) { + DHLOGD("%s:Data queue is empty.", LOG_TAG); + continue; + } + screenData = dataQueue_.front(); + dataQueue_.pop(); + } + + if (screenChannel_ == nullptr) { + DHLOGE("%s: Channel is null", LOG_TAG); + return; + } + if (screenData == nullptr) { + DHLOGE("%s: Screen data is null", LOG_TAG); + continue; + } + + DHLOGD("%s: FeedChannelData.", LOG_TAG); + int32_t ret = screenChannel_->SendData(screenData); + if (ret != DH_SUCCESS) { + DHLOGD("%s:Send data failed.", LOG_TAG); + } + DHLOGD("%s: FeedChannelData success.", LOG_TAG); + } +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screentransport/test/unittest/BUILD.gn b/services/screentransport/test/unittest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4a59c82f549333eb1a535272d41f115c68d06dbc --- /dev/null +++ b/services/screentransport/test/unittest/BUILD.gn @@ -0,0 +1,23 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +group("screen_transport_test") { + testonly = true + deps = [ + "screensinktrans:sink_trans_test", + "screensinkprocessor:sink_processor_test", + "screensourcetrans:source_trans_test", + "screensourceprocessor:source_processor_test", + "screendatachannel:data_channel_test", + ] +} \ No newline at end of file diff --git a/services/screentransport/test/unittest/screendatachannel/BUILD.gn b/services/screentransport/test/unittest/screendatachannel/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..da29a56d42e5b6f4463efdcb0a1aab4a5450fc2a --- /dev/null +++ b/services/screentransport/test/unittest/screendatachannel/BUILD.gn @@ -0,0 +1,82 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +module_out_path = "distributed_screen/data_channel_test" + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "//third_party/json/include", + "//foundation/graphic/standard/interfaces/innerkits/surface", + "${fwk_common_path}/utils/include", + ] + + include_dirs += [ + "./include", + "${services_path}/screentransport/test/unittest/screentranstestutils/include", + "${common_path}/include", + "${services_path}/common/databuffer/include", + "${services_path}/common/screen_channel/include", + "${services_path}/common/utils/include", + "${services_path}/screentransport/screensinkprocessor/include", + "${services_path}/screentransport/screensinktrans/include", + "${services_path}/screentransport/screensourcetrans/include", + "${services_path}/screentransport/screendatachannel/include", + "${services_path}/screentransport/screensourceprocessor/include", + "${services_path}/screentransport/screensourceprocessor/encoder/include", + "${services_path}/softbusadapter/include", + ] +} + +## UnitTest screen_data_channel_test +ohos_unittest("DataChannelTest") { + module_out_path = module_out_path + + sources = [ "${services_path}/screentransport/test/unittest/screendatachannel/src/screen_data_channel_impl_test.cpp" ] + + configs = [ ":module_private_config" ] + + deps = [ + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + "${services_path}/screentransport/screensourcetrans:distributed_screen_sourcetrans", + "${fwk_utils_path}:distributedhardwareutils", + "//foundation/graphic/standard/frameworks/surface:surface", + "//utils/native/base:utils", + ] + + external_deps = [ + "dsoftbus_standard:softbus_client", + "hiviewdfx_hilog_native:libhilog", + "multimedia_media_standard:media_client", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"datachanneltest\"", + "LOG_DOMAIN=0xD004100", + ] + + cflags = [ "-frtti", + "-fexceptions" + ] + cflags_cc = cflags +} + +group("data_channel_test") { + testonly = true + deps = [ ":DataChannelTest" ] +} diff --git a/services/screentransport/test/unittest/screendatachannel/include/screen_data_channel_impl_test.h b/services/screentransport/test/unittest/screendatachannel/include/screen_data_channel_impl_test.h new file mode 100644 index 0000000000000000000000000000000000000000..f5c34dbbead786b2ec5ebd972442ce15b29857c9 --- /dev/null +++ b/services/screentransport/test/unittest/screendatachannel/include/screen_data_channel_impl_test.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "softbus_common.h" +#include "softbus_bus_center.h" + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "screen_data_channel_impl.h" +#include "screentrans_test_utils.h" + +namespace OHOS { +namespace DistributedHardware { +class ScreenDataChannelImplTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + std::shared_ptr dataChannelImpl_ = nullptr; +}; +} +} \ No newline at end of file diff --git a/services/screentransport/test/unittest/screendatachannel/src/screen_data_channel_impl_test.cpp b/services/screentransport/test/unittest/screendatachannel/src/screen_data_channel_impl_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b1032790c44cfa9df8a51fcd06fdeaccfdc70bf --- /dev/null +++ b/services/screentransport/test/unittest/screendatachannel/src/screen_data_channel_impl_test.cpp @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define private public +#include "screen_data_channel_impl_test.h" +#undef private + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void ScreenDataChannelImplTest::SetUpTestCase(void) {} + +void ScreenDataChannelImplTest::TearDownTestCase(void) {} + +void ScreenDataChannelImplTest::SetUp(void) +{ + std::string peerDevId = "test"; + dataChannelImpl_ = std::make_shared(peerDevId); +} + +void ScreenDataChannelImplTest::TearDown(void) {} + +/** + * @tc.name: CreateSession_001 + * @tc.desc: Verify the CreateSession function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenDataChannelImplTest, CreateSession_001, TestSize.Level1) +{ + std::shared_ptr listener = nullptr; + int32_t ret = dataChannelImpl_->CreateSession(listener); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, ret); +} + +/** + * @tc.name: release_session_test_001 + * @tc.desc: Verify the ReleaseSession function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenDataChannelImplTest, release_session_test_001, TestSize.Level1) +{ + std::shared_ptr listener = + std::make_shared(); + + dataChannelImpl_->CreateSession(listener); + int32_t ret = dataChannelImpl_->ReleaseSession(); + EXPECT_EQ(DH_SUCCESS, ret); +} + +/** + * @tc.name: release_session_test_002 + * @tc.desc: Verify the ReleaseSession function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenDataChannelImplTest, release_session_test_002, TestSize.Level1) +{ + EXPECT_EQ(ERR_DH_SCREEN_TRANS_ILLEGAL_OPERATION, dataChannelImpl_->ReleaseSession()); +} + +/** + * @tc.name: close_session_test_001 + * @tc.desc: Verify the CloseSession function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenDataChannelImplTest, close_session_test_001, TestSize.Level1) +{ + dataChannelImpl_->sessionId_ = 1; + EXPECT_EQ(DH_SUCCESS, dataChannelImpl_->CloseSession()); +} + +/** + * @tc.name: close_session_test_002 + * @tc.desc: Verify the CloseSession function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenDataChannelImplTest, close_session_test_002, TestSize.Level1) +{ + dataChannelImpl_->sessionId_ = 0; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_SESSION_NOT_OPEN, dataChannelImpl_->CloseSession()); +} + +/** + * @tc.name: send_data_test_002 + * @tc.desc: Verify the SendData function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenDataChannelImplTest, send_data_test_002, TestSize.Level1) +{ + std::shared_ptr data = nullptr; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, dataChannelImpl_->SendData(data)); +} + +/** + * @tc.name: on_session_opened_test_001 + * @tc.desc: Verify the OnSessionOpened function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenDataChannelImplTest, on_session_opened_test_001, TestSize.Level1) +{ + std::shared_ptr listener = + std::make_shared(); + dataChannelImpl_->channelListener_ = listener; + int32_t sessionId = 0; + int32_t result = 0; + + dataChannelImpl_->OnSessionOpened(sessionId, result); +} + +/** + * @tc.name: on_session_opened_test_002 + * @tc.desc: Verify the OnSessionOpened function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenDataChannelImplTest, on_session_opened_test_002, TestSize.Level1) +{ + std::shared_ptr listener = + std::make_shared(); + dataChannelImpl_->channelListener_ = listener; + int32_t sessionId = -1; + int32_t result = -1; + + dataChannelImpl_->OnSessionOpened(sessionId, result); +} + +/** + * @tc.name: on_session_opened_test_003 + * @tc.desc: Verify the OnSessionOpened function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenDataChannelImplTest, on_session_opened_test_003, TestSize.Level1) +{ + std::shared_ptr listener = nullptr; + dataChannelImpl_->channelListener_ = listener; + int32_t sessionId = 0; + int32_t result = 0; + + dataChannelImpl_->OnSessionOpened(sessionId, result); +} + +/** + * @tc.name: on_session_closed_test_001 + * @tc.desc: Verify the OnSessionClosed function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenDataChannelImplTest, on_session_closed_test_001, TestSize.Level1) +{ + std::shared_ptr listener = nullptr; + dataChannelImpl_->channelListener_ = listener; + int32_t sessionId = 0; + + dataChannelImpl_->OnSessionClosed(sessionId); +} + +/** + * @tc.name: on_stream_received_test_001 + * @tc.desc: Verify the OnStreamReceived function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenDataChannelImplTest, on_stream_received_test_001, TestSize.Level1) +{ + std::shared_ptr listener = + std::make_shared(); + dataChannelImpl_->channelListener_ = listener; + + StreamData *ext = nullptr; + StreamFrameInfo *param = nullptr; + + int32_t sessionId = 0; + StreamData data; + data.buf = new char[DATA_LEN]; + data.bufLen = DATA_LEN; + + dataChannelImpl_->OnStreamReceived(sessionId, &data, ext, param); + delete[] data.buf; +} +} // DistributedHardware +} // OHOS \ No newline at end of file diff --git a/services/screentransport/test/unittest/screensinkprocessor/BUILD.gn b/services/screentransport/test/unittest/screensinkprocessor/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..c61f8fb00c7fdbc5f7f48b7ad58d6e80e146c1fb --- /dev/null +++ b/services/screentransport/test/unittest/screensinkprocessor/BUILD.gn @@ -0,0 +1,86 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +module_out_path = "distributed_screen/sink_processor_test" + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "//third_party/json/include", + "//foundation/graphic/standard/interfaces/innerkits/surface", + "${fwk_common_path}/utils/include", + ] + + include_dirs += [ + "./include", + "${services_path}/screentransport/test/unittest/screentranstestutils/include", + "${common_path}/include", + "${services_path}/common/databuffer/include", + "${services_path}/common/screen_channel/include", + "${services_path}/common/utils/include", + "${services_path}/screentransport/screendatachannel/include", + "${services_path}/screentransport/screensourcetrans/include", + "${services_path}/screentransport/screensinkprocessor/include", + "${services_path}/screentransport/screensinktrans/include", + "${services_path}/screentransport/screensinkprocessor/include", + "${services_path}/screentransport/screensinkprocessor/decoder/include", + "${services_path}/softbusadapter/include", + ] +} + +## UnitTest screen_sink_processor_test +ohos_unittest("SinkProcessorTest") { + module_out_path = module_out_path + + sources = [ + "${services_path}/screentransport/test/unittest/screensinkprocessor/src/image_sink_decoder_test.cpp", + "${services_path}/screentransport/test/unittest/screensinkprocessor/src/image_sink_processor_test.cpp" + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + "${services_path}/screentransport/screensinktrans:distributed_screen_sinktrans", + "${fwk_utils_path}:distributedhardwareutils", + "//foundation/graphic/standard/frameworks/surface:surface", + "//utils/native/base:utils", + ] + + external_deps = [ + "dsoftbus_standard:softbus_client", + "hiviewdfx_hilog_native:libhilog", + "multimedia_media_standard:media_client", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"sinkprocessorest\"", + "LOG_DOMAIN=0xD004100", + ] + + remove_configs = [ "//build/config/compiler:no_rtti" ] + cflags = [ "-frtti" ] + cflags_cc = cflags +} + +group("sink_processor_test") { + testonly = true + deps = [ ":SinkProcessorTest" ] +} \ No newline at end of file diff --git a/services/screentransport/test/unittest/screensinkprocessor/include/image_sink_decoder_test.h b/services/screentransport/test/unittest/screensinkprocessor/include/image_sink_decoder_test.h new file mode 100644 index 0000000000000000000000000000000000000000..4d6f2f3b3eb153a4f6da16539a5effa0b0ff601d --- /dev/null +++ b/services/screentransport/test/unittest/screensinkprocessor/include/image_sink_decoder_test.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IMAGE_SINK_DECODER_TEST_H +#define IMAGE_SINK_DECODER_TEST_H + +#include + +#define private public +#include "image_sink_decoder.h" +#undef private +#include "iscreen_channel_listener.h" +#include "screentrans_test_utils.h" + +namespace OHOS { +namespace DistributedHardware { +class ImageSinkDecoderTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + VideoParam param_; + std::shared_ptr imageDecoder_ = nullptr; + std::shared_ptr imageListener_ = nullptr; +}; +} +} +#endif \ No newline at end of file diff --git a/services/screentransport/test/unittest/screensinkprocessor/include/image_sink_processor_test.h b/services/screentransport/test/unittest/screensinkprocessor/include/image_sink_processor_test.h new file mode 100644 index 0000000000000000000000000000000000000000..b6db96db19950f96651a19f1b76911f233e8ed84 --- /dev/null +++ b/services/screentransport/test/unittest/screensinkprocessor/include/image_sink_processor_test.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IMAGE_SINK_DECODER_TEST_H +#define IMAGE_SINK_DECODER_TEST_H + +#include + +#define private public +#include "image_sink_processor.h" +#undef private +#include "screentrans_test_utils.h" +#include "iscreen_channel_listener.h" + +namespace OHOS { +namespace DistributedHardware { +class ImageSinkProcessorTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + VideoParam param_; + std::shared_ptr processor_; + std::shared_ptr imageListener_; +}; +} +} +#endif \ No newline at end of file diff --git a/services/screentransport/test/unittest/screensinkprocessor/src/image_sink_decoder_test.cpp b/services/screentransport/test/unittest/screensinkprocessor/src/image_sink_decoder_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1e9d5eaa735081725a0eb25da986c0b11f49796 --- /dev/null +++ b/services/screentransport/test/unittest/screensinkprocessor/src/image_sink_decoder_test.cpp @@ -0,0 +1,344 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "image_sink_decoder_test.h" + +#include +#include + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void ImageSinkDecoderTest::SetUpTestCase(void) {} + +void ImageSinkDecoderTest::TearDownTestCase(void) {} + +void ImageSinkDecoderTest::SetUp(void) +{ + param_.screenWidth_ = DSCREEN_MAX_SCREEN_DATA_WIDTH; + param_.screenHeight_ = DSCREEN_MAX_SCREEN_DATA_HEIGHT; + param_.videoWidth_ = DSCREEN_MAX_VIDEO_DATA_WIDTH; + param_.videoHeight_ = DSCREEN_MAX_VIDEO_DATA_HEIGHT; + param_.codecType_ = VIDEO_CODEC_TYPE_VIDEO_H264; + param_.videoFormat_ = VIDEO_DATA_FORMAT_YUVI420; + param_.fps_ = FPS; + + imageListener_ = std::make_shared(); + imageDecoder_ = std::make_shared(imageListener_); + imageDecoder_->videoDecoder_ = Media::VideoDecoderFactory::CreateByMime("video/avc"); +} + +void ImageSinkDecoderTest::TearDown(void) {} + +/** + * @tc.name: configure_decoder_test_001 + * @tc.desc: Verify the ConfigureDecoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, configure_decoder_test_001, TestSize.Level1) +{ + EXPECT_EQ(DH_SUCCESS, imageDecoder_->ConfigureDecoder(param_)); +} + +/** + * @tc.name: configure_decoder_test_002 + * @tc.desc: Verify the ConfigureDecoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, configure_decoder_test_002, TestSize.Level1) +{ + param_.codecType_ = VIDEO_CODEC_TYPE_INVALID; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, imageDecoder_->ConfigureDecoder(param_)); +} + +/** + * @tc.name: release_decoder_test_001 + * @tc.desc: Verify the ReleaseDecoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, release_decoder_test_001, TestSize.Level1) +{ + EXPECT_EQ(DH_SUCCESS, imageDecoder_->ReleaseDecoder()); +} + +/** + * @tc.name: release_decoder_test_002 + * @tc.desc: Verify the ReleaseDecoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, release_decoder_test_002, TestSize.Level1) +{ + imageDecoder_->videoDecoder_ = nullptr; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, imageDecoder_->ReleaseDecoder()); +} + +/** + * @tc.name: start_decoder_test_001 + * @tc.desc: Verify the StartDecoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, start_decoder_test_001, TestSize.Level1) +{ + EXPECT_EQ(ERR_DH_SCREEN_CODEC_PREPARE_FAILED, imageDecoder_->StartDecoder()); +} + +/** + * @tc.name: start_decoder_test_002 + * @tc.desc: Verify the StartDecoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, start_decoder_test_002, TestSize.Level1) +{ + imageDecoder_->videoDecoder_ = nullptr; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, imageDecoder_->StartDecoder()); +} + +/** + * @tc.name: stop_decoder_test_001 + * @tc.desc: Verify the StopDecoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, stop_decoder_test_001, TestSize.Level1) +{ + imageDecoder_->videoDecoder_ = nullptr; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, imageDecoder_->StopDecoder()); +} + +/** + * @tc.name: init_video_decoder_test_001 + * @tc.desc: Verify the InitVideoDecoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, init_video_decoder_test_001, TestSize.Level1) +{ + EXPECT_EQ(DH_SUCCESS, imageDecoder_->InitVideoDecoder(param_)); +} + +/** + * @tc.name: init_video_decoder_test_002 + * @tc.desc: Verify the InitVideoDecoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, init_video_decoder_test_002, TestSize.Level1) +{ + param_.codecType_ = VIDEO_CODEC_TYPE_VIDEO_H265; + EXPECT_EQ(DH_SUCCESS, imageDecoder_->InitVideoDecoder(param_)); +} + +/** + * @tc.name: init_video_decoder_test_003 + * @tc.desc: Verify the InitVideoDecoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, init_video_decoder_test_003, TestSize.Level1) +{ + param_.codecType_ = VIDEO_CODEC_TYPE_INVALID; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, imageDecoder_->InitVideoDecoder(param_)); +} + +/** + * @tc.name: set_decoder_format_test_001 + * @tc.desc: Verify the SetDecoderFormat function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, set_decoder_format_test_001, TestSize.Level1) +{ + EXPECT_EQ(DH_SUCCESS, imageDecoder_->SetDecoderFormat(param_)); +} + +/** + * @tc.name: set_decoder_format_test_002 + * @tc.desc: Verify the SetDecoderFormat function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, set_decoder_format_test_002, TestSize.Level1) +{ + imageDecoder_->videoDecoder_ = nullptr; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, imageDecoder_->SetDecoderFormat(param_)); +} + +/** + * @tc.name: set_decoder_format_test_003 + * @tc.desc: Verify the SetDecoderFormat function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, set_decoder_format_test_003, TestSize.Level1) +{ + param_.codecType_ = VIDEO_CODEC_TYPE_INVALID; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_ILLEGAL_OPERATION, imageDecoder_->SetDecoderFormat(param_)); +} + +/** + * @tc.name: set_output_surface_test_001 + * @tc.desc: Verify the SetOutputSurface function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, set_output_surface_test_001, TestSize.Level1) +{ + sptr surface = Surface::CreateSurfaceAsConsumer("test"); + imageDecoder_->videoDecoder_ = nullptr; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, imageDecoder_->SetOutputSurface(surface)); +} + +/** + * @tc.name: on_error_test_001 + * @tc.desc: Verify the OnError function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, on_error_test_001, TestSize.Level1) +{ + Media::AVCodecErrorType errorType = Media::AVCODEC_ERROR_EXTEND_START; + int32_t errorCode = DH_SUCCESS; + imageDecoder_->OnError(errorType, errorCode); +} + +/** + * @tc.name: on_error_test_002 + * @tc.desc: Verify the OnError function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, on_error_test_002, TestSize.Level1) +{ + Media::AVCodecErrorType errorType = Media::AVCODEC_ERROR_EXTEND_START; + int32_t errorCode = DH_SUCCESS; + std::shared_ptr listener= nullptr; + imageDecoder_->imageProcessorListener_ = listener; + imageDecoder_->OnError(errorType, errorCode); +} + +/** + * @tc.name: on_input_buffer_available_test_001 + * @tc.desc: Verify the OnInputBufferAvailable function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, on_input_buffer_available_test_001, TestSize.Level1) +{ + uint32_t index = 0; + unsigned int len = 1; + imageDecoder_->OnInputBufferAvailable(index); + EXPECT_EQ(len, imageDecoder_->bufferIndexQueue_.size()); +} + +/** + * @tc.name: on_output_buffer_available_test_001 + * @tc.desc: Verify the OnInputBufferAvailable function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, on_output_buffer_available_test_001, TestSize.Level1) +{ + uint32_t index = 0; + Media::AVCodecBufferFlag flag = Media::AVCODEC_BUFFER_FLAG_CODEC_DATA; + Media::AVCodecBufferInfo info; + info.presentationTimeUs = 1; + + imageDecoder_->OnOutputBufferAvailable(index, info, flag); + EXPECT_EQ(info.presentationTimeUs, imageDecoder_->decoderBufferInfo_.presentationTimeUs); +} + +/** + * @tc.name: on_output_buffer_available_test_002 + * @tc.desc: Verify the OnInputBufferAvailable function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, on_output_buffer_available_test_002, TestSize.Level1) +{ + uint32_t index = 0; + Media::AVCodecBufferFlag flag = Media::AVCODEC_BUFFER_FLAG_CODEC_DATA; + Media::AVCodecBufferInfo info; + info.presentationTimeUs = 1; + imageDecoder_->decoderBufferInfo_.presentationTimeUs = 0; + + imageDecoder_->videoDecoder_ = nullptr; + imageDecoder_->OnOutputBufferAvailable(index, info, flag); + EXPECT_NE(info.presentationTimeUs, imageDecoder_->decoderBufferInfo_.presentationTimeUs); +} + +/** + * @tc.name: on_output_format_changed_test_001 + * @tc.desc: Verify the OnOutputFormatChanged function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, on_output_format_changed_test_001, TestSize.Level1) +{ + Media::Format format; + imageDecoder_->OnOutputFormatChanged(format); + + EXPECT_EQ(false, imageDecoder_->isDecoderReady_); +} + +/** + * @tc.name: start_input_thread_test_001 + * @tc.desc: Verify the StartInputThread StopInputThread function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, start_input_thread_test_001, TestSize.Level1) +{ + imageDecoder_->videoDecoder_ = nullptr; + + imageDecoder_->StartInputThread(); + EXPECT_EQ(true, imageDecoder_->isDecoderReady_); + + imageDecoder_->StopInputThread(); + EXPECT_EQ(false, imageDecoder_->isDecoderReady_); +} + +/** + * @tc.name: process_data_test_001 + * @tc.desc: Verify the StartInputThread StopInputThread function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkDecoderTest, process_data_test_001, TestSize.Level1) +{ + std::shared_ptr data = std::make_shared(DATA_LEN); + imageDecoder_->videoDataQueue_.push(data); + + uint32_t index = 0; + imageDecoder_->bufferIndexQueue_.push(index); + + imageDecoder_->StartInputThread(); + EXPECT_EQ(true, imageDecoder_->isDecoderReady_); + + imageDecoder_->StopInputThread(); + EXPECT_EQ(false, imageDecoder_->isDecoderReady_); +} +} +} \ No newline at end of file diff --git a/services/screentransport/test/unittest/screensinkprocessor/src/image_sink_processor_test.cpp b/services/screentransport/test/unittest/screensinkprocessor/src/image_sink_processor_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8a246ead33be4e353cb560633c4964a5d31df11 --- /dev/null +++ b/services/screentransport/test/unittest/screensinkprocessor/src/image_sink_processor_test.cpp @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "image_sink_processor_test.h" + +#include +#include + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void ImageSinkProcessorTest::SetUpTestCase(void) {} + +void ImageSinkProcessorTest::TearDownTestCase(void) {} + +void ImageSinkProcessorTest::SetUp(void) +{ + param_.screenWidth_ = DSCREEN_MAX_SCREEN_DATA_WIDTH; + param_.screenHeight_ = DSCREEN_MAX_SCREEN_DATA_HEIGHT; + param_.videoWidth_ = DSCREEN_MAX_VIDEO_DATA_WIDTH; + param_.videoHeight_ = DSCREEN_MAX_VIDEO_DATA_HEIGHT; + param_.fps_ = FPS; + param_.codecType_ = VIDEO_CODEC_TYPE_VIDEO_H264; + param_.videoFormat_ = VIDEO_DATA_FORMAT_YUVI420; + + processor_ = std::make_shared(); + imageListener_ = std::make_shared(); + processor_->imageDecoder_ = std::make_shared(imageListener_); + processor_->imageDecoder_->videoDecoder_ = Media::VideoDecoderFactory::CreateByName("OMX_hisi_video_decoder_avc"); +} + +void ImageSinkProcessorTest::TearDown(void) {} + +/** + * @tc.name: configure_image_processor_test_001 + * @tc.desc: Verify the ConfigureImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkProcessorTest, configure_image_processor_test_001, TestSize.Level1) +{ + EXPECT_EQ(DH_SUCCESS, processor_->ConfigureImageProcessor(param_, param_, imageListener_)); +} + +/** + * @tc.name: configure_image_processor_test_002 + * @tc.desc: Verify the ConfigureImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkProcessorTest, configure_image_processor_test_002, TestSize.Level1) +{ + param_.codecType_ = VIDEO_CODEC_TYPE_INVALID; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, + processor_->ConfigureImageProcessor(param_, param_, imageListener_)); +} + +/** + * @tc.name: release_image_processor_test_001 + * @tc.desc: Verify the ReleaseImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkProcessorTest, release_image_processor_test_001, TestSize.Level1) +{ + EXPECT_EQ(DH_SUCCESS, processor_->ReleaseImageProcessor()); +} + +/** + * @tc.name: release_image_processor_test_002 + * @tc.desc: Verify the ReleaseImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkProcessorTest, release_image_processor_test_002, TestSize.Level1) +{ + processor_->imageDecoder_ = nullptr; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, processor_->ReleaseImageProcessor()); +} + +/** + * @tc.name: start_image_processor_test_001 + * @tc.desc: Verify the StartImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkProcessorTest, start_image_processor_test_001, TestSize.Level1) +{ + EXPECT_EQ(ERR_DH_SCREEN_CODEC_PREPARE_FAILED, processor_->StartImageProcessor()); +} + +/** + * @tc.name: start_image_processor_test_002 + * @tc.desc: Verify the StartImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkProcessorTest, start_image_processor_test_002, TestSize.Level1) +{ + processor_->imageDecoder_ = nullptr; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, processor_->StartImageProcessor()); +} + +/** + * @tc.name: stop_image_processor_test_001 + * @tc.desc: Verify the StopImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkProcessorTest, stop_image_processor_test_001, TestSize.Level1) +{ + EXPECT_EQ(ERR_DH_SCREEN_CODEC_FLUSH_FAILED, processor_->StopImageProcessor()); +} + +/** + * @tc.name: stop_image_processor_test_002 + * @tc.desc: Verify the StopImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkProcessorTest, stop_image_processor_test_002, TestSize.Level1) +{ + processor_->imageDecoder_ = nullptr; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, processor_->StopImageProcessor()); +} + +/** + * @tc.name: set_image_surface_test_001 + * @tc.desc: Verify the StopImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkProcessorTest, set_image_surface_test_001, TestSize.Level1) +{ + sptr surface = Surface::CreateSurfaceAsConsumer("test"); + EXPECT_EQ(ERR_DH_SCREEN_CODEC_SURFACE_ERROR, processor_->SetImageSurface(surface)); +} + +/** + * @tc.name: set_image_surface_test_002 + * @tc.desc: Verify the StopImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkProcessorTest, set_image_surface_test_002, TestSize.Level1) +{ + processor_->imageDecoder_ = nullptr; + sptr surface = Surface::CreateSurfaceAsConsumer("test"); + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, processor_->SetImageSurface(surface)); +} + +/** + * @tc.name: processimage_test_001 + * @tc.desc: Verify the StopImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkProcessorTest, processimage_test_001, TestSize.Level1) +{ + std::shared_ptr data = std::make_shared(DATA_LEN); + EXPECT_EQ(DH_SUCCESS, processor_->ProcessImage(data)); +} + +/** + * @tc.name: processimage_test_002 + * @tc.desc: Verify the StopImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSinkProcessorTest, processimage_test_002, TestSize.Level1) +{ + processor_->imageDecoder_ = nullptr; + std::shared_ptr data = std::make_shared(DATA_LEN); + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, processor_->ProcessImage(data)); +} +} +} \ No newline at end of file diff --git a/services/screentransport/test/unittest/screensinktrans/BUILD.gn b/services/screentransport/test/unittest/screensinktrans/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..90ecbf7990cf88ed2c027adb16dbc420ea39b444 --- /dev/null +++ b/services/screentransport/test/unittest/screensinktrans/BUILD.gn @@ -0,0 +1,84 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +module_out_path = "distributed_screen/sink_trans_test" + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "//third_party/json/include", + "//foundation/graphic/standard/interfaces/innerkits/surface", + "${fwk_common_path}/utils/include", + ] + + include_dirs += [ + "./include", + "${services_path}/screentransport/test/unittest/screentranstestutils/include", + "${common_path}/include", + "${services_path}/common/databuffer/include", + "${services_path}/common/screen_channel/include", + "${services_path}/common/utils/include", + "${services_path}/screentransport/screendatachannel/include", + "${services_path}/screentransport/screensourcetrans/include", + "${services_path}/screentransport/screensinktrans/include", + "${services_path}/screentransport/screensinkprocessor/include", + "${services_path}/screentransport/screensinkprocessor/decoder/include", + "${services_path}/softbusadapter/include", + ] +} + +## UnitTest sink_trans_test +ohos_unittest("SinkTransTest") { + module_out_path = module_out_path + + sources = [ + "${services_path}/screentransport/test/unittest/screensinktrans/src/screen_sink_trans_test.cpp" + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + "${services_path}/screentransport/screensinktrans:distributed_screen_sinktrans", + "${fwk_utils_path}:distributedhardwareutils", + "//foundation/graphic/standard/frameworks/surface:surface", + "//utils/native/base:utils", + ] + + external_deps = [ + "dsoftbus_standard:softbus_client", + "hiviewdfx_hilog_native:libhilog", + "multimedia_media_standard:media_client", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"sinktranstest\"", + "LOG_DOMAIN=0xD004100", + ] + + remove_configs = [ "//build/config/compiler:no_rtti" ] + cflags = [ "-frtti" ] + cflags_cc = cflags +} + +group("sink_trans_test") { + testonly = true + deps = [ ":SinkTransTest" ] +} \ No newline at end of file diff --git a/services/screentransport/test/unittest/screensinktrans/include/screen_sink_trans_test.h b/services/screentransport/test/unittest/screensinktrans/include/screen_sink_trans_test.h new file mode 100644 index 0000000000000000000000000000000000000000..7ad2db3e4a78c2818438b7168a9852a27e971e4a --- /dev/null +++ b/services/screentransport/test/unittest/screensinktrans/include/screen_sink_trans_test.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SCREEN_SINK_TRANS_TEST_H +#define SCREEN_SINK_TRANS_TEST_H + +#include + +#define private public +#include "screen_sink_trans.h" +#undef private + +#include "iscreen_sink_trans_callback.h" +#include "screentrans_test_utils.h" + +namespace OHOS { +namespace DistributedHardware { +class ScreenSinkTransTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + std::shared_ptr trans_; + VideoParam param_; + std::string peerDevId_; +}; +} +} +#endif \ No newline at end of file diff --git a/services/screentransport/test/unittest/screensinktrans/src/screen_sink_trans_test.cpp b/services/screentransport/test/unittest/screensinktrans/src/screen_sink_trans_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d88b9bfa036ab6b850ee402ae659a0fb542f1f0 --- /dev/null +++ b/services/screentransport/test/unittest/screensinktrans/src/screen_sink_trans_test.cpp @@ -0,0 +1,258 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screen_sink_trans_test.h" + +#include + +#include "dscreen_errcode.h" +#include "image_sink_processor.h" +#include "screen_data_channel_impl.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void ScreenSinkTransTest::SetUpTestCase(void) {} + +void ScreenSinkTransTest::TearDownTestCase(void) {} + +void ScreenSinkTransTest::SetUp(void) +{ + peerDevId_ = "test"; + trans_ = std::make_shared(); + trans_->imageProcessor_ = std::make_shared(); + trans_->screenChannel_ = std::make_shared(peerDevId_); + trans_->decoderSurface_ = Surface::CreateSurfaceAsConsumer("test"); + trans_->transCallback_ = std::make_shared(); + + param_.screenWidth_ = DSCREEN_MAX_SCREEN_DATA_WIDTH; + param_.screenHeight_ = DSCREEN_MAX_SCREEN_DATA_HEIGHT; + param_.videoWidth_ = DSCREEN_MAX_VIDEO_DATA_WIDTH; + param_.videoHeight_ = DSCREEN_MAX_VIDEO_DATA_HEIGHT; + param_.codecType_ = VIDEO_CODEC_TYPE_VIDEO_H264; + param_.videoFormat_ = VIDEO_DATA_FORMAT_YUVI420; + param_.fps_ = FPS; +} + +void ScreenSinkTransTest::TearDown(void) {} + +/** + * @tc.name: setup_test_001 + * @tc.desc: Verify the SetUp function failed. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, setup_test_001, TestSize.Level1) +{ + param_.screenWidth_ = WIDTH_INVALID; + param_.screenHeight_ = HEIGHT_INVALID; + param_.videoWidth_ = WIDTH_INVALID; + param_.videoHeight_ = HEIGHT_INVALID; + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM, trans_->SetUp(param_, param_, peerDevId_)); +} + +/** + * @tc.name: register_processor_listner_001 + * @tc.desc: Verify the RegisterChannelListner function success. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, register_processor_listner_001, TestSize.Level1) +{ + EXPECT_EQ(DH_SUCCESS, trans_->RegisterProcessorListner(param_, param_, peerDevId_)); +} + +/** + * @tc.name: release_test_001 + * @tc.desc: Verify the Release function success. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, release_test_001, TestSize.Level1) +{ + EXPECT_EQ(DH_SUCCESS, trans_->Release()); +} + +/** + * @tc.name: release_test_002 + * @tc.desc: Verify the Release function fail. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, release_test_002, TestSize.Level1) +{ + trans_->imageProcessor_ = nullptr; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, trans_->Release()); +} + +/** + * @tc.name: start_test_002 + * @tc.desc: Verify the Start function fail. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, start_test_002, TestSize.Level1) +{ + trans_->imageProcessor_ = nullptr; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, trans_->Start()); +} + +/** + * @tc.name: stop_test_002 + * @tc.desc: Verify the Stop function success. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, stop_test_002, TestSize.Level1) +{ + EXPECT_EQ(ERR_DH_SCREEN_TRANS_ERROR, trans_->Stop()); +} + +/** + * @tc.name: register_state_callback_test_001 + * @tc.desc: Verify the RegisterStateCallback function success. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, register_state_callback_test_001, TestSize.Level1) +{ + std::shared_ptr callback = + std::make_shared(); + EXPECT_EQ(DH_SUCCESS, trans_->RegisterStateCallback(callback)); +} + +/** + * @tc.name: register_state_callback_test_002 + * @tc.desc: Verify the RegisterStateCallback function fail. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, register_state_callback_test_002, TestSize.Level1) +{ + std::shared_ptr callback = nullptr; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, trans_->RegisterStateCallback(callback)); +} + +/** + * @tc.name: set_image_surface_test_001 + * @tc.desc: Verify the SetImageSurface function success. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, set_image_surface_test_001, TestSize.Level1) +{ + sptr surface = Surface::CreateSurfaceAsConsumer("test"); + EXPECT_EQ(DH_SUCCESS, trans_->SetImageSurface(surface)); +} + +/** + * @tc.name: set_image_surface_test_002 + * @tc.desc: Verify the SetImageSurface function fail. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, set_image_surface_test_002, TestSize.Level1) +{ + sptr surface = nullptr; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, trans_->SetImageSurface(surface)); +} + +/** + * @tc.name: check_trans_param_test_001 + * @tc.desc: Verify the CheckTransParam function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, check_trans_param_test_001, TestSize.Level1) +{ + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, trans_->CheckTransParam(param_, param_, "")); + + param_.codecType_ = VIDEO_CODEC_TYPE_INVALID; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM, trans_->CheckTransParam(param_, param_, peerDevId_)); + + param_.codecType_ = VIDEO_CODEC_TYPE_VIDEO_H264; + param_.videoFormat_ = VIDEO_DATA_FORMAT_INVALID; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM, trans_->CheckTransParam(param_, param_, peerDevId_)); + + param_.videoFormat_ = VIDEO_DATA_FORMAT_YUVI420; + param_.videoWidth_ = WIDTH_INVALID; + param_.videoHeight_ = HEIGHT_INVALID; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM, trans_->CheckTransParam(param_, param_, peerDevId_)); + + param_.videoWidth_ = DSCREEN_MAX_VIDEO_DATA_WIDTH; + param_.videoHeight_ = DSCREEN_MAX_VIDEO_DATA_HEIGHT; + param_.screenWidth_ = WIDTH_INVALID; + param_.screenHeight_ = HEIGHT_INVALID; + EXPECT_EQ(ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM, trans_->CheckTransParam(param_, param_, peerDevId_)); + + param_.screenWidth_ = DSCREEN_MAX_SCREEN_DATA_WIDTH; + param_.screenHeight_ = DSCREEN_MAX_SCREEN_DATA_HEIGHT; + EXPECT_EQ(DH_SUCCESS, trans_->CheckTransParam(param_, param_, peerDevId_)); +} + +/** + * @tc.name: on_session_closed_test_001 + * @tc.desc: Verify the OnSessionClosed function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, on_session_closed_test_001, TestSize.Level1) +{ + trans_->transCallback_ = std::make_shared(); + trans_->OnSessionClosed(); +} + +/** + * @tc.name: on_data_received_test_001 + * @tc.desc: Verify the OnDataReceived function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, on_data_received_test_001, TestSize.Level1) +{ + std::shared_ptr dataBuffer = nullptr; + trans_->OnDataReceived(dataBuffer); +} + +/** + * @tc.name: on_processor_state_notify_test_001 + * @tc.desc: Verify the RegisterChannelListner function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, on_processor_state_notify_test_001, TestSize.Level1) +{ + int32_t state = DH_SUCCESS; + trans_->transCallback_ = std::make_shared(); + trans_->OnProcessorStateNotify(state); +} + +/** + * @tc.name: on_processor_state_notify_test_002 + * @tc.desc: Verify the RegisterChannelListner function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSinkTransTest, on_processor_state_notify_test_002, TestSize.Level1) +{ + int32_t state = DH_SUCCESS; + std::shared_ptr callback = nullptr; + trans_->transCallback_ = callback; + trans_->OnProcessorStateNotify(state); +} +} +} \ No newline at end of file diff --git a/services/screentransport/test/unittest/screensourceprocessor/BUILD.gn b/services/screentransport/test/unittest/screensourceprocessor/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..2f9353492e94a83bd8af0479f36424e7461c5871 --- /dev/null +++ b/services/screentransport/test/unittest/screensourceprocessor/BUILD.gn @@ -0,0 +1,75 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +module_out_path = "distributed_screen/source_processor_test" + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "//third_party/json/include", + "//foundation/graphic/standard/interfaces/innerkits/surface", + "${fwk_common_path}/utils/include", + "${fwk_utils_path}/include/", + ] + + include_dirs += [ + "./include", + "${services_path}/screentransport/test/unittest/screentranstestutils/include", + "${common_path}/include", + "${services_path}/common/databuffer/include", + "${services_path}/common/screen_channel/include", + "${services_path}/common/utils/include", + "${services_path}/screentransport/test/unittest/screensourcetrans", + "${services_path}/screentransport/screensourcetrans/include", + "${services_path}/screentransport/screendatachannel/include", + "${services_path}/screentransport/screensourceprocessor/include", + "${services_path}/screentransport/screensourceprocessor/encoder/include", + "${services_path}/softbusadapter/include", + "${services_path}/screentransport/test/unittest/screensourceprocessor/include", + ] +} + +## UnitTest screen_source_processor_test +ohos_unittest("SourceProcessorTest") { + module_out_path = module_out_path + + sources = [ + "${services_path}/screentransport/test/unittest/screensourceprocessor/src/image_source_processor_test.cpp", + "${services_path}/screentransport/test/unittest/screensourceprocessor/src/image_source_encoder_test.cpp", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + "${services_path}/screentransport/screensourcetrans:distributed_screen_sourcetrans", + "${fwk_utils_path}:distributedhardwareutils", + "//foundation/graphic/standard/frameworks/surface:surface", + "//utils/native/base:utils", + ] + + external_deps = [ + "dsoftbus_standard:softbus_client", + "hiviewdfx_hilog_native:libhilog", + "multimedia_media_standard:media_client", + ] +} + +group("source_processor_test") { + testonly = true + deps = [ ":SourceProcessorTest" ] +} diff --git a/services/screentransport/test/unittest/screensourceprocessor/include/image_source_encoder_test.h b/services/screentransport/test/unittest/screensourceprocessor/include/image_source_encoder_test.h new file mode 100644 index 0000000000000000000000000000000000000000..f9d3fadd968d86db76b7af7c856ba28b29aeb957 --- /dev/null +++ b/services/screentransport/test/unittest/screensourceprocessor/include/image_source_encoder_test.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "dscreen_constants.h" +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#define private public +#include "image_source_encoder.h" +#undef private + +namespace OHOS { +namespace DistributedHardware { +class ImageSourceEncoderTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + std::shared_ptr encoder = nullptr; +}; +} +} \ No newline at end of file diff --git a/services/screentransport/test/unittest/screensourceprocessor/include/image_source_processor_test.h b/services/screentransport/test/unittest/screensourceprocessor/include/image_source_processor_test.h new file mode 100644 index 0000000000000000000000000000000000000000..3dfafb36daac2d26397d62f40c55c2d9302ace8f --- /dev/null +++ b/services/screentransport/test/unittest/screensourceprocessor/include/image_source_processor_test.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#define private public +#include "image_source_processor.h" +#undef private + +namespace OHOS { +namespace DistributedHardware { +class ImageSourceProcessorTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + ImageSourceProcessor processor; +}; +} +} \ No newline at end of file diff --git a/services/screentransport/test/unittest/screensourceprocessor/src/image_source_encoder_test.cpp b/services/screentransport/test/unittest/screensourceprocessor/src/image_source_encoder_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4805246f79208259e12ed4040887647dd321817a --- /dev/null +++ b/services/screentransport/test/unittest/screensourceprocessor/src/image_source_encoder_test.cpp @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "image_source_encoder_test.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void ImageSourceEncoderTest::SetUpTestCase(void) {} + +void ImageSourceEncoderTest::TearDownTestCase(void) {} + +void ImageSourceEncoderTest::SetUp(void) +{ + std::shared_ptr imageListener = nullptr; + encoder = std::make_shared(imageListener); +} + +void ImageSourceEncoderTest::TearDown(void) {} + +/** + * @tc.name: ConfigureEncoder_001 + * @tc.desc: Verify the ConfigureEncoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceEncoderTest, ConfigureEncoder_001, TestSize.Level1) +{ + VideoParam configParam; + configParam.codecType_ = VIDEO_CODEC_TYPE_VIDEO_H264; + configParam.videoFormat_ = VIDEO_DATA_FORMAT_YUVI420; + + int32_t actual = encoder->ConfigureEncoder(configParam); + EXPECT_EQ(ERR_DH_SCREEN_CODEC_SURFACE_ERROR, actual); +} + +/** + * @tc.name: ConfigureEncoder_002 + * @tc.desc: Verify the ConfigureEncoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceEncoderTest, ConfigureEncoder_002, TestSize.Level1) +{ + VideoParam configParam; + configParam.codecType_ = -1; + + int32_t actual = encoder->ConfigureEncoder(configParam); + EXPECT_EQ(ERR_DH_SCREEN_TRANS_CREATE_CODEC_FAILED, actual); +} + +/** + * @tc.name: ReleaseEncoder_001 + * @tc.desc: Verify the ReleaseEncoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceEncoderTest, ReleaseEncoder_001, TestSize.Level1) +{ + int32_t actual = encoder->ReleaseEncoder(); + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: ReleaseEncoder_002 + * @tc.desc: Verify the ReleaseEncoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceEncoderTest, ReleaseEncoder_002, TestSize.Level1) +{ + encoder->videoEncoder_ = Media::VideoEncoderFactory::CreateByMime("video/avc"); + + int32_t actual = encoder->ReleaseEncoder(); + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: StartEncoder_001 + * @tc.desc: Verify the StartEncoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceEncoderTest, StartEncoder_001, TestSize.Level1) +{ + int32_t actual = encoder->StartEncoder(); + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: StartEncoder_002 + * @tc.desc: Verify the StartEncoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceEncoderTest, StartEncoder_002, TestSize.Level1) +{ + encoder->videoEncoder_ = Media::VideoEncoderFactory::CreateByMime("video/avc"); + + int32_t actual = encoder->StartEncoder(); + EXPECT_EQ(ERR_DH_SCREEN_CODEC_PREPARE_FAILED, actual); +} + +/** + * @tc.name: StopEncoder_001 + * @tc.desc: Verify the StopEncoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceEncoderTest, StopEncoder_001, TestSize.Level1) +{ + int32_t actual = encoder->StopEncoder(); + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: StopEncoder_002 + * @tc.desc: Verify the StartEncoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceEncoderTest, StopEncoder_002, TestSize.Level1) +{ + encoder->videoEncoder_ = Media::VideoEncoderFactory::CreateByMime("video/avc"); + + int32_t actual = encoder->StopEncoder(); + EXPECT_EQ(ERR_DH_SCREEN_CODEC_STOP_FAILED, actual); +} + +/** + * @tc.name: InitVideoEncoder_001 + * @tc.desc: Verify the InitVideoEncoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceEncoderTest, InitVideoEncoder_001, TestSize.Level1) +{ + VideoParam configParam; + configParam.codecType_ = VIDEO_CODEC_TYPE_VIDEO_H264; + + int32_t actual = encoder->InitVideoEncoder(configParam); + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: InitVideoEncoder_002 + * @tc.desc: Verify the InitVideoEncoder function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceEncoderTest, InitVideoEncoder_002, TestSize.Level1) +{ + VideoParam configParam; + configParam.codecType_ = -1; + + int32_t actual = encoder->InitVideoEncoder(configParam); + EXPECT_EQ(ERR_DH_SCREEN_TRANS_CREATE_CODEC_FAILED, actual); +} + +/** + * @tc.name: SetEncoderFormat_001 + * @tc.desc: Verify the SetEncoderFormat function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceEncoderTest, SetEncoderFormat_001, TestSize.Level1) +{ + VideoParam configParam; + configParam.codecType_ = VIDEO_CODEC_TYPE_VIDEO_H264; + configParam.videoFormat_ = VIDEO_DATA_FORMAT_YUVI420; + + int32_t actual = encoder->SetEncoderFormat(configParam); + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: SetEncoderFormat_002 + * @tc.desc: Verify the SetEncoderFormat function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceEncoderTest, SetEncoderFormat_002, TestSize.Level1) +{ + VideoParam configParam; + configParam.codecType_ = -1; + + int32_t actual = encoder->SetEncoderFormat(configParam); + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} +} // DistributedHardware +} // OHOS \ No newline at end of file diff --git a/services/screentransport/test/unittest/screensourceprocessor/src/image_source_processor_test.cpp b/services/screentransport/test/unittest/screensourceprocessor/src/image_source_processor_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5cc926bc6b478b0f71a2f0b2e73f96a10d65d00 --- /dev/null +++ b/services/screentransport/test/unittest/screensourceprocessor/src/image_source_processor_test.cpp @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "image_source_processor_test.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void ImageSourceProcessorTest::SetUpTestCase(void) {} + +void ImageSourceProcessorTest::TearDownTestCase(void) {} + +void ImageSourceProcessorTest::SetUp(void) {} + +void ImageSourceProcessorTest::TearDown(void) {} + +/** + * @tc.name: ConfigureImageProcessor_001 + * @tc.desc: Verify the ConfigureImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceProcessorTest, ConfigureImageProcessor_001, TestSize.Level1) +{ + VideoParam localParam; + VideoParam remoteParam; + std::shared_ptr listener = nullptr; + + int32_t actual = processor.ConfigureImageProcessor(localParam, remoteParam, listener); + + EXPECT_EQ(ERR_DH_SCREEN_CODEC_SURFACE_ERROR, actual); +} + +/** + * @tc.name: ConfigureImageProcessor_002 + * @tc.desc: Verify the ConfigureImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceProcessorTest, ConfigureImageProcessor_002, TestSize.Level1) +{ + VideoParam localParam; + localParam.codecType_ = VIDEO_CODEC_TYPE_VIDEO_H264; + VideoParam remoteParam; + std::shared_ptr listener = nullptr; + + int32_t actual = processor.ConfigureImageProcessor(localParam, remoteParam, listener); + + EXPECT_EQ(ERR_DH_SCREEN_CODEC_SURFACE_ERROR, actual); +} + +/** + * @tc.name: ReleaseImageProcessor_001 + * @tc.desc: Verify the ReleaseImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceProcessorTest, ReleaseImageProcessor_001, TestSize.Level1) +{ + int32_t actual = processor.ReleaseImageProcessor(); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: ReleaseImageProcessor_002 + * @tc.desc: Verify the ReleaseImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceProcessorTest, ReleaseImageProcessor_002, TestSize.Level1) +{ + std::shared_ptr listener = nullptr; + processor.imageEncoder_ = std::make_shared(listener); + int32_t actual = processor.ReleaseImageProcessor(); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: StartImageProcessor_001 + * @tc.desc: Verify the StartImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceProcessorTest, StartImageProcessor_001, TestSize.Level1) +{ + int32_t actual = processor.StartImageProcessor(); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: StartImageProcessor_002 + * @tc.desc: Verify the StartImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceProcessorTest, StartImageProcessor_002, TestSize.Level1) +{ + std::shared_ptr listener = nullptr; + processor.imageEncoder_ = std::make_shared(listener); + int32_t actual = processor.StartImageProcessor(); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: StopImageProcessor_001 + * @tc.desc: Verify the StopImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceProcessorTest, StopImageProcessor_001, TestSize.Level1) +{ + int32_t actual = processor.StopImageProcessor(); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: StopImageProcessor_002 + * @tc.desc: Verify the StopImageProcessor function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceProcessorTest, StopImageProcessor_002, TestSize.Level1) +{ + std::shared_ptr listener = nullptr; + processor.imageEncoder_ = std::make_shared(listener); + int32_t actual = processor.StopImageProcessor(); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: GetImageSurface_001 + * @tc.desc: Verify the GetImageSurface function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ImageSourceProcessorTest, GetImageSurface_001, TestSize.Level1) +{ + std::shared_ptr listener = nullptr; + processor.imageEncoder_ = std::make_shared(listener); + sptr actual = processor.GetImageSurface(); + + EXPECT_EQ(nullptr, actual); +} +} // DistributedHardware +} // OHOS \ No newline at end of file diff --git a/services/screentransport/test/unittest/screensourcetrans/BUILD.gn b/services/screentransport/test/unittest/screensourcetrans/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..09c1d9867dda0aa27736471e306cda454c6b63f9 --- /dev/null +++ b/services/screentransport/test/unittest/screensourcetrans/BUILD.gn @@ -0,0 +1,75 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +module_out_path = "distributed_screen/source_trans_test" + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "//third_party/json/include", + "//foundation/graphic/standard/interfaces/innerkits/surface", + "${fwk_common_path}/log/include", + "${fwk_common_path}/utils/include", + "${fwk_utils_path}/include/", + "${fwk_utils_path}/include/log", + ] + + include_dirs += [ + "./include", + "${services_path}/screentransport/test/unittest/screentranstestutils/include", + "${common_path}/include", + "${services_path}/common/databuffer/include", + "${services_path}/common/screen_channel/include", + "${services_path}/common/utils/include", + "${services_path}/screentransport/screensinktrans/include", + "${services_path}/screentransport/screensinkprocessor/include", + "${services_path}/screentransport/test/unittest/screensourcetrans/include", + "${services_path}/screentransport/screensourcetrans/include", + "${services_path}/screentransport/screendatachannel/include", + "${services_path}/screentransport/screensourceprocessor/include", + "${services_path}/screentransport/screensourceprocessor/encoder/include", + "${services_path}/softbusadapter/include", + ] +} + +## UnitTest screen_source_trans_test +ohos_unittest("SourceTransTest") { + module_out_path = module_out_path + + sources = [ "${services_path}/screentransport/test/unittest/screensourcetrans/src/screen_source_trans_test.cpp" ] + + configs = [ ":module_private_config" ] + + deps = [ + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + "${services_path}/screentransport/screensourcetrans:distributed_screen_sourcetrans", + "${fwk_utils_path}:distributedhardwareutils", + "//foundation/graphic/standard/frameworks/surface:surface", + "//utils/native/base:utils", + ] + + external_deps = [ + "dsoftbus_standard:softbus_client", + "hiviewdfx_hilog_native:libhilog", + "multimedia_media_standard:media_client", + ] +} + +group("source_trans_test") { + testonly = true + deps = [ ":SourceTransTest" ] +} diff --git a/services/screentransport/test/unittest/screensourcetrans/include/screen_source_trans_test.h b/services/screentransport/test/unittest/screensourcetrans/include/screen_source_trans_test.h new file mode 100644 index 0000000000000000000000000000000000000000..333cb9edabaa74ea13c5cbb590ab9c9f3d50addc --- /dev/null +++ b/services/screentransport/test/unittest/screensourcetrans/include/screen_source_trans_test.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#define private public +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "image_source_processor.h" +#include "screen_data_channel_impl.h" +#include "screen_source_trans.h" +#undef private + +namespace OHOS { +namespace DistributedHardware { +class ScreenSourceTransTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + std::shared_ptr trans = nullptr; +}; +} +} diff --git a/services/screentransport/test/unittest/screensourcetrans/src/screen_source_trans_test.cpp b/services/screentransport/test/unittest/screensourcetrans/src/screen_source_trans_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0a5f3ae014b235b519517f3a2be57cff2aafa06 --- /dev/null +++ b/services/screentransport/test/unittest/screensourcetrans/src/screen_source_trans_test.cpp @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "screen_source_trans_test.h" +#include "screentrans_test_utils.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void ScreenSourceTransTest::SetUpTestCase(void) {} + +void ScreenSourceTransTest::TearDownTestCase(void) {} + +void ScreenSourceTransTest::SetUp(void) +{ + trans = std::make_shared(); +} + +void ScreenSourceTransTest::TearDown(void) {} + +/** + * @tc.name: SetUp_001 + * @tc.desc: Verify the SetUp function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSourceTransTest, SetUp_001, TestSize.Level1) +{ + VideoParam localParam; + VideoParam remoteParam; + std::string peerDevId; + + int32_t actual = trans->SetUp(localParam, remoteParam, peerDevId); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: SetUp_002 + * @tc.desc: Verify the SetUp function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSourceTransTest, SetUp_002, TestSize.Level1) +{ + VideoParam localParam; + localParam.codecType_ = VIDEO_CODEC_TYPE_VIDEO_H264; + localParam.videoFormat_ = VIDEO_DATA_FORMAT_YUVI420; + localParam.videoWidth_ = DSCREEN_MAX_VIDEO_DATA_WIDTH; + localParam.videoHeight_ = DSCREEN_MAX_VIDEO_DATA_HEIGHT; + localParam.screenWidth_ = DSCREEN_MAX_SCREEN_DATA_WIDTH; + localParam.screenHeight_ = DSCREEN_MAX_SCREEN_DATA_HEIGHT; + VideoParam remoteParam; + remoteParam.codecType_ = VIDEO_CODEC_TYPE_VIDEO_H264; + remoteParam.videoFormat_ = VIDEO_DATA_FORMAT_YUVI420; + remoteParam.videoWidth_ = DSCREEN_MAX_VIDEO_DATA_WIDTH; + remoteParam.videoHeight_ = DSCREEN_MAX_VIDEO_DATA_HEIGHT; + remoteParam.screenWidth_ = DSCREEN_MAX_SCREEN_DATA_WIDTH; + remoteParam.screenHeight_ = DSCREEN_MAX_SCREEN_DATA_HEIGHT; + std::string peerDevId = "hello"; + + int32_t actual = trans->SetUp(localParam, remoteParam, peerDevId); + + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: Release_001 + * @tc.desc: Verify the Release function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSourceTransTest, Release_001, TestSize.Level1) +{ + int32_t actual = trans->Release(); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: Release_002 + * @tc.desc: Verify the Release function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSourceTransTest, Release_002, TestSize.Level1) +{ + trans->imageProcessor_ = std::make_shared(); + std::string peerDevId = "hello"; + trans->screenChannel_ = std::make_shared(peerDevId); + int32_t actual = trans->Release(); + + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: Start_001 + * @tc.desc: Verify the Start function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSourceTransTest, Start_001, TestSize.Level1) +{ + int32_t actual = trans->Start(); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: Start_002 + * @tc.desc: Verify the Start function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSourceTransTest, Start_002, TestSize.Level1) +{ + trans->imageProcessor_ = std::make_shared(); + std::string peerDevId = "hello"; + trans->screenChannel_ = std::make_shared(peerDevId); + int32_t actual = trans->Start(); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_ERROR, actual); +} + +/** + * @tc.name: RegisterStateCallback_001 + * @tc.desc: Verify the RegisterStateCallback function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSourceTransTest, RegisterStateCallback_001, TestSize.Level1) +{ + std::shared_ptr callback = nullptr; + int32_t actual = trans->RegisterStateCallback(callback); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: RegisterStateCallback_002 + * @tc.desc: Verify the RegisterStateCallback function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSourceTransTest, RegisterStateCallback_002, TestSize.Level1) +{ + std::shared_ptr callback = + std::make_shared(); + int32_t actual = trans->RegisterStateCallback(callback); + + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: CheckTransParam_001 + * @tc.desc: Verify the CheckTransParam function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSourceTransTest, CheckTransParam_001, TestSize.Level1) +{ + VideoParam localParam; + VideoParam remoteParam; + std::string peerDevId; + int32_t actual = trans->CheckTransParam(localParam, remoteParam, peerDevId); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_NULL_VALUE, actual); +} + +/** + * @tc.name: CheckTransParam_002 + * @tc.desc: Verify the CheckTransParam function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSourceTransTest, CheckTransParam_002, TestSize.Level1) +{ + VideoParam localParam; + localParam.codecType_ = VIDEO_CODEC_TYPE_VIDEO_H264; + localParam.videoFormat_ = VIDEO_DATA_FORMAT_YUVI420; + localParam.videoWidth_ = DSCREEN_MAX_VIDEO_DATA_WIDTH; + localParam.videoHeight_ = DSCREEN_MAX_VIDEO_DATA_HEIGHT; + localParam.screenWidth_ = DSCREEN_MAX_SCREEN_DATA_WIDTH; + localParam.screenHeight_ = DSCREEN_MAX_SCREEN_DATA_HEIGHT; + VideoParam remoteParam; + remoteParam.codecType_ = VIDEO_CODEC_TYPE_VIDEO_H264; + remoteParam.videoFormat_ = VIDEO_DATA_FORMAT_YUVI420; + remoteParam.videoWidth_ = DSCREEN_MAX_VIDEO_DATA_WIDTH; + remoteParam.videoHeight_ = DSCREEN_MAX_VIDEO_DATA_HEIGHT; + remoteParam.screenWidth_ = DSCREEN_MAX_SCREEN_DATA_WIDTH; + remoteParam.screenHeight_ = DSCREEN_MAX_SCREEN_DATA_HEIGHT; + std::string peerDevId = "hello"; + int32_t actual = trans->CheckTransParam(localParam, remoteParam, peerDevId); + + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: OnSessionOpened_001 + * @tc.desc: Verify the OnSessionOpened function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenSourceTransTest, OnSessionOpened_001, TestSize.Level1) +{ + trans->imageProcessor_ = std::make_shared(); + + trans->OnSessionOpened(); + + EXPECT_EQ(false, trans->isChannelReady_); +} +} +} diff --git a/services/screentransport/test/unittest/screentranstestutils/include/screentrans_test_utils.h b/services/screentransport/test/unittest/screentranstestutils/include/screentrans_test_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..407b54133069e9c9ff3a38561c9fda63b5eda939 --- /dev/null +++ b/services/screentransport/test/unittest/screentranstestutils/include/screentrans_test_utils.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SCREENTRANS_TEST_UTILS_H +#define SCREENTRANS_TEST_UTILS_H + +#include "dscreen_errcode.h" +#include "dscreen_log.h" +#include "iimage_sink_processor_listener.h" +#include "iscreen_sink_trans_callback.h" +#include "iscreen_source_trans_callback.h" +#include "iscreen_channel_listener.h" + +namespace OHOS { +namespace DistributedHardware { +constexpr int32_t VIDEO_CODEC_TYPE_INVALID = -1; +constexpr int32_t VIDEO_DATA_FORMAT_INVALID = -1; +constexpr int32_t WIDTH_INVALID = 9999; +constexpr int32_t HEIGHT_INVALID = 9999; +constexpr int32_t FPS = 30; +constexpr size_t DATA_LEN = 128; + +class MockIScreenSinkTransCallback : public IScreenSinkTransCallback { +public: + explicit MockIScreenSinkTransCallback() + { + } + ~MockIScreenSinkTransCallback() override = default; + + virtual void OnError(int32_t err, const std::string &content) override + { + (void) err; + (void) content; + } +}; + +class MockIImageSinkProcessorListener : public IImageSinkProcessorListener { +public: + explicit MockIImageSinkProcessorListener() + { + } + ~MockIImageSinkProcessorListener() override = default; + + virtual void OnProcessorStateNotify(int32_t state) override + { + (void) state; + } +}; + +class MockIScreenSourceTransCallback : public IScreenSourceTransCallback { +public: + ~MockIScreenSourceTransCallback() override {}; + + virtual void OnError(int32_t err, const std::string &content) override + { + (void) err; + (void) content; + } +}; + +class MockIScreenChannelListener : public IScreenChannelListener { +public: + void OnSessionOpened() override {} + void OnSessionClosed() override {} + void OnDataReceived(const std::shared_ptr &data) override {} +}; +} +} +#endif \ No newline at end of file diff --git a/services/softbusadapter/include/isoftbus_listener.h b/services/softbusadapter/include/isoftbus_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..874509090efbbeac6256efe24011d9808d669b4e --- /dev/null +++ b/services/softbusadapter/include/isoftbus_listener.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ISOFTBUS_LISTENER +#define OHOS_ISOFTBUS_LISTENER + +#include "data_buffer.h" +#include "session.h" + +namespace OHOS { +namespace DistributedHardware { +class ISoftbusListener { +public: + virtual ~ISoftbusListener() = default; + + virtual void OnSessionOpened(int32_t sessionId, int32_t result) = 0; + virtual void OnSessionClosed(int32_t sessionId) = 0; + virtual void OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) = 0; + virtual void OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, + const StreamFrameInfo *param) = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/softbusadapter/include/softbus_adapter.h b/services/softbusadapter/include/softbus_adapter.h new file mode 100644 index 0000000000000000000000000000000000000000..3d556675ccca8fc1bcb6c839aecd37f51a6cf9b3 --- /dev/null +++ b/services/softbusadapter/include/softbus_adapter.h @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_SOFTBUS_ADAPTER +#define OHOS_SOFTBUS_ADAPTER + +#include +#include +#include + +#include "session.h" +#include "single_instance.h" + +#include "dscreen_constants.h" +#include "dscreen_log.h" +#include "isoftbus_listener.h" + +namespace OHOS { +namespace DistributedHardware { +typedef struct { + std::string sessionName; + std::string peerDevId; +} SessionInfo; + +class SoftbusAdapter { + DECLARE_SINGLE_INSTANCE_BASE(SoftbusAdapter); +public: + int32_t CreateSoftbusSessionServer(const std::string &pkgname, const std::string &sessionName, + const std::string &peerDevId); + int32_t RemoveSoftbusSessionServer(const std::string &pkgname, const std::string &sessionName, + const std::string &peerDevId); + int32_t OpenSoftbusSession(const std::string &mySessionName, const std::string &peerSessionName, + const std::string &peerDevId); + int32_t CloseSoftbusSession(int32_t sessionId); + int32_t SendSoftbusBytes(int32_t sessionId, const void *data, int32_t dataLen); + int32_t SendSoftbusStream(int32_t sessionId, const StreamData *data, const StreamData *ext, + const StreamFrameInfo *param); + int32_t RegisterSoftbusListener(const std::shared_ptr &listener, const std::string &sessionName, + const std::string &peerDevId); + int32_t UnRegisterSoftbusListener(const std::string &sessionName, const std::string &peerDevId); + + int32_t OnSoftbusSessionOpened(int32_t sessionId, int32_t result); + void OnSoftbusSessionClosed(int32_t sessionId); + void OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen); + void OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, + const StreamFrameInfo *StreamFrameInfo); + void OnMessageReceived(int sessionId, const void *data, unsigned int dataLen); + void OnQosEvent(int sessionId, int eventId, int tvCount, const QosTv *tvList); + +private: + SoftbusAdapter(); + ~SoftbusAdapter(); + std::shared_ptr &GetSoftbusListenerByName(int32_t sessionId); + std::shared_ptr &GetSoftbusListenerById(int32_t sessionId); + +private: + static const constexpr char *LOG_TAG = "SoftbusAdapter"; + std::mutex listenerMtx_; + std::mutex sessInfoMtx_; + std::mutex sessSetMtx_; + + ISessionListener sessListener_; + /* while can not find the listener in mapListeners_, return nullListener_ point to null ptr. */ + std::shared_ptr nullListener_; + std::map mapSessionInfos_; + std::map> mapSessionSet_; + std::map> mapListeners_; + std::map> mapSessListeners_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/softbusadapter/src/softbus_adapter.cpp b/services/softbusadapter/src/softbus_adapter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30ce06b752bff17e85b3a93b78bd252a0cf95745 --- /dev/null +++ b/services/softbusadapter/src/softbus_adapter.cpp @@ -0,0 +1,343 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "softbus_adapter.h" + +#include + +#include "softbus_bus_center.h" +#include "softbus_common.h" + +#include "dscreen_errcode.h" +#include "dscreen_util.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(SoftbusAdapter); +static int32_t ScreenOnSoftbusSessionOpened(int32_t sessionId, int32_t result) +{ + return SoftbusAdapter::GetInstance().OnSoftbusSessionOpened(sessionId, result); +} + +static void ScreenOnSoftbusSessionClosed(int32_t sessionId) +{ + SoftbusAdapter::GetInstance().OnSoftbusSessionClosed(sessionId); +} + +static void ScreenOnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) +{ + SoftbusAdapter::GetInstance().OnBytesReceived(sessionId, data, dataLen); +} + +static void ScreenOnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, + const StreamFrameInfo *frameInfo) +{ + SoftbusAdapter::GetInstance().OnStreamReceived(sessionId, data, ext, frameInfo); +} + +static void ScreenOnMessageReceived(int sessionId, const void *data, unsigned int dataLen) +{ + SoftbusAdapter::GetInstance().OnMessageReceived(sessionId, data, dataLen); +} + +static void ScreenOnQosEvent(int sessionId, int eventId, int tvCount, const QosTv *tvList) +{ + SoftbusAdapter::GetInstance().OnQosEvent(sessionId, eventId, tvCount, tvList); +} + +SoftbusAdapter::SoftbusAdapter() +{ + DHLOGI("SoftbusAdapter"); + sessListener_.OnSessionOpened = ScreenOnSoftbusSessionOpened; + sessListener_.OnSessionClosed = ScreenOnSoftbusSessionClosed; + sessListener_.OnBytesReceived = ScreenOnBytesReceived; + sessListener_.OnStreamReceived = ScreenOnStreamReceived; + sessListener_.OnMessageReceived = ScreenOnMessageReceived; + sessListener_.OnQosEvent = ScreenOnQosEvent; +} + +SoftbusAdapter::~SoftbusAdapter() +{ + DHLOGI("~SoftbusAdapter"); +} + +int32_t SoftbusAdapter::RegisterSoftbusListener(const std::shared_ptr &listener, + const std::string &sessionName, const std::string &peerDevId) +{ + DHLOGI("%s: RegisterListener sess:%s id:%s.", LOG_TAG, sessionName.c_str(), GetAnonyString(peerDevId).c_str()); + std::string strListenerKey = sessionName + "_" + peerDevId; + + std::lock_guard lisLock(listenerMtx_); + if (mapListeners_.find(strListenerKey) != mapListeners_.end()) { + DHLOGE("%s: Session listener already register.", LOG_TAG); + return ERR_DH_SCREEN_ADAPTER_REGISTER_SOFTBUS_LISTENER_FAIL; + } + mapListeners_.insert(std::make_pair(strListenerKey, listener)); + + return DH_SUCCESS; +} + +int32_t SoftbusAdapter::UnRegisterSoftbusListener(const std::string &sessionName, const std::string &peerDevId) +{ + DHLOGI("%s: UnRegisterListener sess:%s id:%s.", LOG_TAG, sessionName.c_str(), GetAnonyString(peerDevId).c_str()); + std::string strListenerKey = sessionName + "_" + peerDevId; + + std::lock_guard lisLock(listenerMtx_); + mapListeners_.erase(strListenerKey); + + return DH_SUCCESS; +} + +int32_t SoftbusAdapter::CreateSoftbusSessionServer(const std::string &pkgname, const std::string &sessionName, + const std::string &peerDevId) +{ + DHLOGI("%s: CreateSessionServer sess:%s id:%s.", LOG_TAG, sessionName.c_str(), GetAnonyString(peerDevId).c_str()); + std::lock_guard setLock(sessSetMtx_); + if (mapSessionSet_.find(sessionName) == mapSessionSet_.end()) { + int32_t ret = CreateSessionServer(pkgname.c_str(), sessionName.c_str(), &sessListener_); + if (ret != DH_SUCCESS) { + DHLOGE("%s: CreateSessionServer failed.", LOG_TAG); + return ret; + } + } else { + DHLOGD("%s: Session already create.", sessionName.c_str()); + } + + mapSessionSet_[sessionName].insert(peerDevId); + DHLOGI("%s: CreateSessionServer success.", LOG_TAG); + return DH_SUCCESS; +} + +int32_t SoftbusAdapter::RemoveSoftbusSessionServer(const std::string &pkgname, const std::string &sessionName, + const std::string &peerDevId) +{ + DHLOGI("%s: RemoveSessionServer sess:%s id:%s", LOG_TAG, sessionName.c_str(), GetAnonyString(peerDevId).c_str()); + std::lock_guard setLock(sessSetMtx_); + if (mapSessionSet_.find(sessionName) == mapSessionSet_.end()) { + DHLOGE("%s: Session name can not find.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ILLEGAL_OPERATION; + } else if (mapSessionSet_[sessionName].find(peerDevId) == mapSessionSet_[sessionName].end()) { + DHLOGE("%s: PeerDevId can not find.", LOG_TAG); + return ERR_DH_SCREEN_TRANS_ILLEGAL_OPERATION; + } + + mapSessionSet_[sessionName].erase(peerDevId); + if (mapSessionSet_[sessionName].empty()) { + mapSessionSet_.erase(sessionName); + } + DHLOGI("%s: RemoveSessionServer success.", LOG_TAG); + return DH_SUCCESS; +} + +int32_t SoftbusAdapter::OpenSoftbusSession(const std::string &mySessionName, const std::string &peerSessionName, + const std::string &peerDevId) +{ + DHLOGI("%s: OpenSoftbusSession mysess:%s peersess:%s id:%s.", LOG_TAG, mySessionName.c_str(), + peerSessionName.c_str(), GetAnonyString(peerDevId).c_str()); + int dataType = TYPE_BYTES; + int streamType = -1; + if (mySessionName == DATA_SESSION_NAME) { + dataType = TYPE_STREAM; + streamType = RAW_STREAM; + } + + SessionAttribute attr = { 0 }; + attr.dataType = dataType; + attr.linkTypeNum = LINK_TYPE_MAX; + LinkType linkTypeList[LINK_TYPE_MAX] = { + LINK_TYPE_WIFI_P2P, + LINK_TYPE_WIFI_WLAN_5G, + LINK_TYPE_WIFI_WLAN_2G, + LINK_TYPE_BR, + }; + int32_t ret = memcpy_s(attr.linkType, sizeof(attr.linkType), linkTypeList, sizeof(linkTypeList)); + if (ret != EOK) { + DHLOGE("%s: Data copy failed.", LOG_TAG); + return ERR_DH_SCREEN_ADAPTER_PARA_ERROR; + } + attr.attr.streamAttr.streamType = streamType; + int32_t sessionId = OpenSession(mySessionName.c_str(), peerSessionName.c_str(), peerDevId.c_str(), "0", &attr); + if (sessionId < 0) { + DHLOGE("%s: OpenSession failed sessionId:%d.", LOG_TAG, sessionId); + return ERR_DH_SCREEN_ADAPTER_OPEN_SESSION_FAIL; + } + + DHLOGI("%s: OpenSoftbusSession success sessionId:%d.", LOG_TAG, sessionId); + return sessionId; +} + +int32_t SoftbusAdapter::CloseSoftbusSession(const int32_t sessionId) +{ + DHLOGI("%s: CloseSoftbusSession, sessid:%d.", LOG_TAG, sessionId); + CloseSession(sessionId); + + std::lock_guard lisLock(listenerMtx_); + mapSessListeners_.erase(sessionId); + + DHLOGI("%s: CloseSoftbusSession success.", LOG_TAG); + return DH_SUCCESS; +} + +int32_t SoftbusAdapter::SendSoftbusBytes(int32_t sessionId, const void *data, int32_t dataLen) +{ + DHLOGD("%s: SendSoftbusBytes, sessid:%d.", LOG_TAG, sessionId); + int32_t ret = SendBytes(sessionId, data, dataLen); + if (ret != DH_SUCCESS) { + DHLOGE("%s: SendBytes failed ret:%d.", LOG_TAG, ret); + return ERR_DH_SCREEN_TRANS_ERROR; + } + + return DH_SUCCESS; +} + +int32_t SoftbusAdapter::SendSoftbusStream(int32_t sessionId, const StreamData *data, const StreamData *ext, + const StreamFrameInfo *param) +{ + DHLOGD("%s: SendSoftbusStream, sessid:%d.", LOG_TAG, sessionId); + int32_t ret = SendStream(sessionId, data, ext, param); + if (ret != DH_SUCCESS) { + DHLOGE("%s: SendStream failed ret:%d.", LOG_TAG, ret); + return ERR_DH_SCREEN_TRANS_ERROR; + } + + return DH_SUCCESS; +} + +std::shared_ptr &SoftbusAdapter::GetSoftbusListenerByName(int32_t sessionId) +{ + char sessionName[DSCREEN_MAX_SESSION_NAME_LEN] = ""; + char peerDevId[DSCREEN_MAX_DEVICE_ID_LEN] = ""; + int32_t ret = GetPeerSessionName(sessionId, sessionName, sizeof(sessionName)); + if (ret != DH_SUCCESS) { + DHLOGE("%s: GetPeerSessionName failed ret:%d.", LOG_TAG, ret); + return nullListener_; + } + ret = GetPeerDeviceId(sessionId, peerDevId, sizeof(peerDevId)); + if (ret != DH_SUCCESS) { + DHLOGE("%s: GetPeerDeviceId failed ret:%d.", LOG_TAG, ret); + return nullListener_; + } + + std::string sessionNameStr(sessionName); + std::string peerDevIdStr(peerDevId); + DHLOGD("%s: GetSoftbusListenerByName sessionName:%s, peerDevId:%s.", LOG_TAG, sessionNameStr.c_str(), + GetAnonyString(peerDevIdStr).c_str()); + std::string strListenerKey = sessionNameStr + "_" + peerDevIdStr; + + std::lock_guard lisLock(listenerMtx_); + if (mapListeners_.find(strListenerKey) == mapListeners_.end()) { + DHLOGE("%s: Find listener failed.", LOG_TAG); + return nullListener_; + } + return mapListeners_[strListenerKey]; +} + +std::shared_ptr &SoftbusAdapter::GetSoftbusListenerById(int32_t sessionId) +{ + std::lock_guard lisLock(listenerMtx_); + if (mapSessListeners_.find(sessionId) == mapSessListeners_.end()) { + DHLOGE("%s: Find listener failed.", LOG_TAG); + return nullListener_; + } + + return mapSessListeners_[sessionId]; +} + +int32_t SoftbusAdapter::OnSoftbusSessionOpened(int32_t sessionId, int32_t result) +{ + DHLOGI("%s: OnSessionOpened session:%d, result:%d.", LOG_TAG, sessionId, result); + if (result != DH_SUCCESS) { + DHLOGE("%s: OnSessionOpened failed.", LOG_TAG); + return ERR_DH_SCREEN_ADAPTER_OPEN_SESSION_FAIL; + } + + std::shared_ptr &listener = GetSoftbusListenerByName(sessionId); + if (!listener) { + DHLOGE("Get softbus listener failed."); + return ERR_DH_SCREEN_TRANS_ERROR; + } + listener->OnSessionOpened(sessionId, result); + + std::lock_guard lisLock(listenerMtx_); + mapSessListeners_.insert(std::make_pair(sessionId, listener)); + + return DH_SUCCESS; +} + +void SoftbusAdapter::OnSoftbusSessionClosed(int32_t sessionId) +{ + DHLOGI("%s: OnSessionClosed sessionId:%d.", LOG_TAG, sessionId); + std::shared_ptr &listener = GetSoftbusListenerById(sessionId); + if (!listener) { + DHLOGE("Get softbus listener failed."); + return; + } + listener->OnSessionClosed(sessionId); + + std::lock_guard lisLock(listenerMtx_); + mapSessListeners_.erase(sessionId); +} + +void SoftbusAdapter::OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) +{ + DHLOGD("%s: OnBytesReceived, sessionId:%d.", LOG_TAG, sessionId); + if (data == nullptr) { + DHLOGE("BytesData is null."); + return; + } + if (dataLen == 0 || dataLen > DSCREEN_MAX_RECV_DATA_LEN) { + DHLOGE("BytesData length is too large, dataLen:%u.", dataLen); + return; + } + + std::shared_ptr &listener = GetSoftbusListenerByName(sessionId); + if (!listener) { + DHLOGE("Get softbus listener failed."); + return; + } + listener->OnBytesReceived(sessionId, data, dataLen); +} + +void SoftbusAdapter::OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, + const StreamFrameInfo *frameInfo) +{ + DHLOGD("%s OnStreamReceived, sessionId:%d.", LOG_TAG, sessionId); + if (data == nullptr) { + DHLOGE("StreamData is null."); + return; + } + if (data->bufLen <= 0 || data->bufLen > (int32_t)DSCREEN_MAX_RECV_DATA_LEN) { + DHLOGE("StreamData length is too large, dataLen:%d.", data->bufLen); + return; + } + + std::shared_ptr &listener = GetSoftbusListenerByName(sessionId); + if (!listener) { + DHLOGE("Get softbus listener failed."); + } + listener->OnStreamReceived(sessionId, data, ext, frameInfo); +} + +void SoftbusAdapter::OnMessageReceived(int sessionId, const void *data, unsigned int dataLen) +{ + DHLOGD("%s OnMessageReceived, sessionId:%d.", LOG_TAG, sessionId); +} + +void SoftbusAdapter::OnQosEvent(int sessionId, int eventId, int tvCount, const QosTv *tvList) +{ + DHLOGD("%s OnQosEvent, sessionId:%d.", LOG_TAG, sessionId); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/softbusadapter/test/unittest/BUILD.gn b/services/softbusadapter/test/unittest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..53959db3f0338df0878e0e8b2c7571cafff76668 --- /dev/null +++ b/services/softbusadapter/test/unittest/BUILD.gn @@ -0,0 +1,70 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//foundation/distributedhardware/distributedscreen/distributedscreen.gni") + +module_out_path = "distributed_screen/soft_bus_adapter_test" + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "//third_party/json/include", + "//foundation/graphic/standard/interfaces/innerkits/surface", + "${fwk_common_path}/utils/include", + "${fwk_utils_path}/include/", + ] + + include_dirs += [ + "./include", + "${common_path}/include", + "${services_path}/common/databuffer/include", + "${services_path}/common/screen_channel/include", + "${services_path}/common/utils/include", + "${services_path}/screentransport/test/unittest/screensourcetrans/include", + "${services_path}/screentransport/screensourcetrans/include", + "${services_path}/screentransport/screendatachannel/include", + "${services_path}/screentransport/screensourceprocessor/include", + "${services_path}/screentransport/screensourceprocessor/encoder/include", + "${services_path}/softbusadapter/include", + ] +} + +## UnitTest screen_soft_bus_adapter_test +ohos_unittest("SoftBusAdapterTest") { + module_out_path = module_out_path + + sources = [ "${services_path}/softbusadapter/test/unittest/src/softbus_adapter_test.cpp" ] + + configs = [ ":module_private_config" ] + + deps = [ + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + "${services_path}/screentransport/screensourcetrans:distributed_screen_sourcetrans", + "${fwk_utils_path}:distributedhardwareutils", + "//foundation/graphic/standard/frameworks/surface:surface", + "//utils/native/base:utils", + ] + + external_deps = [ + "dsoftbus_standard:softbus_client", + "hiviewdfx_hilog_native:libhilog", + "multimedia_media_standard:media_client", + ] +} + +group("soft_bus_adapter_test") { + testonly = true + deps = [ ":SoftBusAdapterTest" ] +} diff --git a/services/softbusadapter/test/unittest/include/softbus_adapter_test.h b/services/softbusadapter/test/unittest/include/softbus_adapter_test.h new file mode 100644 index 0000000000000000000000000000000000000000..3f494d4f480bb650dfb824bc4466e71b59783a22 --- /dev/null +++ b/services/softbusadapter/test/unittest/include/softbus_adapter_test.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "softbus_bus_center.h" +#include "softbus_common.h" + +#include "dscreen_errcode.h" +#include "dscreen_util.h" +#define private public +#include "softbus_adapter.h" +#undef private + +namespace OHOS { +namespace DistributedHardware { +class SoftbusAdapterTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + SoftbusAdapter softbusAdapter; +}; +} +} \ No newline at end of file diff --git a/services/softbusadapter/test/unittest/src/softbus_adapter_test.cpp b/services/softbusadapter/test/unittest/src/softbus_adapter_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d696f7050f3710082f64ae7f413b6d5c0e6ad68e --- /dev/null +++ b/services/softbusadapter/test/unittest/src/softbus_adapter_test.cpp @@ -0,0 +1,303 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "softbus_adapter_test.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void SoftbusAdapterTest::SetUpTestCase(void) {} + +void SoftbusAdapterTest::TearDownTestCase(void) {} + +void SoftbusAdapterTest::SetUp(void) {} + +void SoftbusAdapterTest::TearDown(void) {} + +/** + * @tc.name: RegisterSoftbusListener_001 + * @tc.desc: Verify the RegisterSoftbusListener function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, RegisterSoftbusListener_001, TestSize.Level1) +{ + std::shared_ptr listener = nullptr; + std::string sessionName; + std::string peerDevId; + + int32_t actual = softbusAdapter.RegisterSoftbusListener(listener, sessionName, peerDevId); + + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: RegisterSoftbusListener_002 + * @tc.desc: Verify the RegisterSoftbusListener function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, RegisterSoftbusListener_002, TestSize.Level1) +{ + std::shared_ptr listener = nullptr; + std::string sessionName = "hello"; + std::string peerDevId = "world"; + + softbusAdapter.mapListeners_["hello_world"] = listener; + int32_t actual = softbusAdapter.RegisterSoftbusListener(listener, sessionName, peerDevId); + + EXPECT_EQ(ERR_DH_SCREEN_ADAPTER_REGISTER_SOFTBUS_LISTENER_FAIL, actual); +} + +/** + * @tc.name: UnRegisterSoftbusListener_001 + * @tc.desc: Verify the UnRegisterSoftbusListener function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, UnRegisterSoftbusListener_001, TestSize.Level1) +{ + std::string sessionName = "hello"; + std::string peerDevId = "world"; + + int32_t actual = softbusAdapter.UnRegisterSoftbusListener(sessionName, peerDevId); + + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: CreateSoftbusSessionServer_001 + * @tc.desc: Verify the CreateSoftbusSessionServer function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, CreateSoftbusSessionServer_001, TestSize.Level1) +{ + std::string pkgname = "hello"; + std::string sessionName = "world"; + std::string peerDevId = "world"; + + softbusAdapter.mapSessionSet_[sessionName].insert(peerDevId); + int32_t actual = softbusAdapter.CreateSoftbusSessionServer(pkgname, sessionName, peerDevId); + + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: RemoveSoftbusSessionServer_001 + * @tc.desc: Verify the RemoveSoftbusSessionServer function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, RemoveSoftbusSessionServer_001, TestSize.Level1) +{ + std::string pkgname = "hello"; + std::string sessionName = "world"; + std::string peerDevId = "world"; + + int32_t actual = softbusAdapter.RemoveSoftbusSessionServer(pkgname, sessionName, peerDevId); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_ILLEGAL_OPERATION, actual); +} + +/** + * @tc.name: RemoveSoftbusSessionServer_002 + * @tc.desc: Verify the RemoveSoftbusSessionServer function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, RemoveSoftbusSessionServer_002, TestSize.Level1) +{ + std::string pkgname = "hello"; + std::string sessionName = "world"; + std::string peerDevId = "world"; + + softbusAdapter.mapSessionSet_[sessionName].insert(peerDevId); + int32_t actual = softbusAdapter.RemoveSoftbusSessionServer(pkgname, sessionName, peerDevId); + + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: OpenSoftbusSession_001 + * @tc.desc: Verify the OpenSoftbusSession function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, OpenSoftbusSession_001, TestSize.Level1) +{ + std::string mySessionName = DATA_SESSION_NAME; + std::string peerSessionName = "world"; + std::string peerDevId = "world"; + + int32_t actual = softbusAdapter.OpenSoftbusSession(mySessionName, peerSessionName, peerDevId); + + EXPECT_EQ(ERR_DH_SCREEN_ADAPTER_OPEN_SESSION_FAIL, actual); +} + +/** + * @tc.name: OpenSoftbusSession_002 + * @tc.desc: Verify the OpenSoftbusSession function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, OpenSoftbusSession_002, TestSize.Level1) +{ + std::string mySessionName = "hello"; + std::string peerSessionName = "world"; + std::string peerDevId = "world"; + + int32_t actual = softbusAdapter.OpenSoftbusSession(mySessionName, peerSessionName, peerDevId); + + EXPECT_EQ(ERR_DH_SCREEN_ADAPTER_OPEN_SESSION_FAIL, actual); +} + +/** + * @tc.name: CloseSoftbusSession_001 + * @tc.desc: Verify the CloseSoftbusSession function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, CloseSoftbusSession_001, TestSize.Level1) +{ + int32_t sessionId = 0; + + int32_t actual = softbusAdapter.CloseSoftbusSession(sessionId); + + EXPECT_EQ(DH_SUCCESS, actual); +} + +/** + * @tc.name: SendSoftbusBytes_001 + * @tc.desc: Verify the SendSoftbusBytes function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, SendSoftbusBytes_001, TestSize.Level1) +{ + int32_t sessionId = 0; + void *data = nullptr; + int32_t dataLen = 0; + + int32_t actual = softbusAdapter.SendSoftbusBytes(sessionId, data, dataLen); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_ERROR, actual); +} + +/** + * @tc.name: SendSoftbusStream_001 + * @tc.desc: Verify the SendSoftbusStream function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, SendSoftbusStream_001, TestSize.Level1) +{ + int32_t sessionId = 0; + StreamData *data = nullptr; + StreamData *ext = nullptr; + StreamFrameInfo *param = nullptr; + + int32_t actual = softbusAdapter.SendSoftbusStream(sessionId, data, ext, param); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_ERROR, actual); +} + +/** + * @tc.name: GetSoftbusListener_001 + * @tc.desc: Verify the GetSoftbusListener function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, GetSoftbusListener_001, TestSize.Level1) +{ + int32_t sessionId = 0; + + std::shared_ptr actual = softbusAdapter.GetSoftbusListenerById(sessionId); + + EXPECT_EQ(nullptr, actual); +} + +/** + * @tc.name: GetSoftbusListener_002 + * @tc.desc: Verify the GetSoftbusListener function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, GetSoftbusListener_002, TestSize.Level1) +{ + int32_t sessionId = 0; + + SessionInfo sessionInfo; + sessionInfo.sessionName = "hello"; + sessionInfo.peerDevId = "world"; + softbusAdapter.mapSessionInfos_[sessionId] = sessionInfo; + std::shared_ptr listener = nullptr; + softbusAdapter.mapListeners_["hello_world"] = listener; + std::shared_ptr actual = softbusAdapter.GetSoftbusListenerById(sessionId); + + EXPECT_EQ(nullptr, actual); +} + +/** + * @tc.name: OnSoftbusSessionOpened_001 + * @tc.desc: Verify the OnSoftbusSessionOpened function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, OnSoftbusSessionOpened_001, TestSize.Level1) +{ + int32_t sessionId = 0; + int32_t result = 0; + + int32_t actual = softbusAdapter.OnSoftbusSessionOpened(sessionId, result); + + EXPECT_EQ(ERR_DH_SCREEN_TRANS_ERROR, actual); +} + +/** + * @tc.name: OnSoftbusSessionClosed_001 + * @tc.desc: Verify the OnSoftbusSessionClosed function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, OnSoftbusSessionClosed_001, TestSize.Level1) +{ + int32_t sessionId = 0; + + softbusAdapter.OnSoftbusSessionClosed(sessionId); +} + +/** + * @tc.name: OnSoftbusSessionClosed_002 + * @tc.desc: Verify the OnSoftbusSessionClosed function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(SoftbusAdapterTest, OnSoftbusSessionClosed_002, TestSize.Level1) +{ + int32_t sessionId = 0; + + SessionInfo sessionInfo; + sessionInfo.sessionName = "hello"; + sessionInfo.peerDevId = "world"; + softbusAdapter.mapSessionInfos_[sessionId] = sessionInfo; + std::shared_ptr listener = nullptr; + softbusAdapter.mapListeners_["hello_world"] = listener; + + softbusAdapter.OnSoftbusSessionClosed(sessionId); +} +} // DistributedHardware +} // OHOS \ No newline at end of file