1 /*
2 * Copyright (C) 2023-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 * Description: Cast mirror player function realization.
15 * Author: zhangjingnan
16 * Create: 2023-05-27
17 */
18
19 #include "mirror_player.h"
20
21 #include "cast_engine_errors.h"
22 #include "cast_engine_log.h"
23 #include "surface_utils.h"
24
25 namespace OHOS {
26 namespace CastEngine {
27 namespace CastEngineClient {
28 DEFINE_CAST_ENGINE_LABEL("Cast-Client-MirrorPlayer");
29
~MirrorPlayer()30 MirrorPlayer::~MirrorPlayer()
31 {
32 CLOGI("Stop the client mirror player.");
33 }
34
Play(const std::string & deviceId)35 int32_t MirrorPlayer::Play(const std::string &deviceId)
36 {
37 if (deviceId.empty()) {
38 CLOGE("The device id is null");
39 return ERR_INVALID_PARAM;
40 }
41 return proxy_ ? proxy_->Play(deviceId) : CAST_ENGINE_ERROR;
42 }
43
Pause(const std::string & deviceId)44 int32_t MirrorPlayer::Pause(const std::string &deviceId)
45 {
46 if (deviceId.empty()) {
47 CLOGE("The device id is null");
48 return ERR_INVALID_PARAM;
49 }
50 return proxy_ ? proxy_->Pause(deviceId) : CAST_ENGINE_ERROR;
51 }
52
DeliverInputEvent(OHRemoteControlEvent event)53 int32_t MirrorPlayer::DeliverInputEvent(OHRemoteControlEvent event)
54 {
55 return proxy_ ? proxy_->DeliverInputEvent(event) : CAST_ENGINE_ERROR;
56 }
57
InjectEvent(const OHRemoteControlEvent & event)58 int32_t MirrorPlayer::InjectEvent(const OHRemoteControlEvent &event)
59 {
60 return proxy_ ? proxy_->InjectEvent(event) : CAST_ENGINE_ERROR;
61 }
62
Release()63 int32_t MirrorPlayer::Release()
64 {
65 return proxy_ ? proxy_->Release() : CAST_ENGINE_ERROR;
66 }
67
ResizeVirtualScreen(uint32_t width,uint32_t height)68 int32_t MirrorPlayer::ResizeVirtualScreen(uint32_t width, uint32_t height)
69 {
70 return proxy_ ? proxy_->ResizeVirtualScreen(width, height) : CAST_ENGINE_ERROR;
71 }
72
GetDisplayId(std::string & displayId)73 int32_t MirrorPlayer::GetDisplayId(std::string &displayId)
74 {
75 return proxy_ ? proxy_->GetDisplayId(displayId) : CAST_ENGINE_ERROR;
76 }
77
SetAppInfo(const AppInfo & appInfo)78 int32_t MirrorPlayer::SetAppInfo(const AppInfo &appInfo)
79 {
80 return proxy_ ? proxy_->SetAppInfo(appInfo) : CAST_ENGINE_ERROR;
81 }
82
SetSurface(const std::string & surfaceId)83 int32_t MirrorPlayer::SetSurface(const std::string &surfaceId)
84 {
85 errno = 0;
86 char *end = nullptr;
87 uint64_t surfaceUniqueId = static_cast<uint64_t>(std::strtoll(surfaceId.c_str(), &end, 10));
88 if (errno == ERANGE || (surfaceUniqueId == 0 && *end != '\0')) {
89 CLOGE("Failed to strtoll, errno: %{public}d", errno);
90 return ERR_INVALID_PARAM;
91 }
92
93 sptr<Surface> surface = SurfaceUtils::GetInstance()->GetSurface(surfaceUniqueId);
94 if (!surface) {
95 CLOGE("surface is null, surface uniqueId %{public}" PRIu64, surfaceUniqueId);
96 return CAST_ENGINE_ERROR;
97 }
98 sptr<IBufferProducer> producer = surface->GetProducer();
99 if (!producer) {
100 CLOGE("producer is null");
101 return CAST_ENGINE_ERROR;
102 }
103 return proxy_ ? proxy_->SetSurface(producer) : CAST_ENGINE_ERROR;
104 }
105
106 } // namespace CastEngineClient
107 } // namespace CastEngine
108 } // namespace OHOS