1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <dlfcn.h>
17 #include "avsession_trace.h"
18 #include "remote_session_sink_proxy.h"
19 
20 namespace OHOS::AVSession {
RemoteSessionSinkProxy()21 RemoteSessionSinkProxy::RemoteSessionSinkProxy()
22 {
23     LoadSinkImplement();
24 }
25 
~RemoteSessionSinkProxy()26 RemoteSessionSinkProxy::~RemoteSessionSinkProxy()
27 {
28     UnLoadSinkImplement();
29 }
30 
LoadSinkImplement()31 int32_t RemoteSessionSinkProxy::LoadSinkImplement() __attribute__((no_sanitize("cfi")))
32 {
33     handle_ = dlopen("libremote_session_sink.z.so", RTLD_NOW);
34     if (handle_ == nullptr) {
35         SLOGE("Failed to open extension library %{public}s, reason: %{public}sn",
36             "libremote_session_sink.z.so", dlerror());
37         return AVSESSION_ERROR;
38     }
39     using SinkImpl = RemoteSessionSinkImpl* (*)();
40 
41     auto createRemoteSessionSinkImpl = (SinkImpl)(dlsym(handle_, "CreateRemoteSessionSinkImpl"));
42     if (createRemoteSessionSinkImpl == nullptr) {
43         if (handle_ != nullptr) {
44 #ifndef TEST_COVERAGE
45             dlclose(handle_);
46 #endif
47         }
48         SLOGE("Failed to get extension symbol %{public}s in %{public}s",
49             "RemoteSessionSinkImpl", "libremote_session_sink.z.so");
50         return AVSESSION_ERROR;
51     }
52 
53     sinkImpl_ = createRemoteSessionSinkImpl();
54     return AVSESSION_SUCCESS;
55 }
56 
UnLoadSinkImplement()57 int32_t RemoteSessionSinkProxy::UnLoadSinkImplement() __attribute__((no_sanitize("cfi")))
58 {
59     using SinkImpl = void(*)(RemoteSessionSinkImpl*);
60     auto destroyRemoteSessionSinkImpl = (SinkImpl)(dlsym(handle_, "DestroyRemoteSessionSinkImpl"));
61     if (destroyRemoteSessionSinkImpl == nullptr) {
62         if (handle_ != nullptr) {
63 #ifndef TEST_COVERAGE
64             dlclose(handle_);
65 #endif
66         }
67         SLOGE("Failed to get extension symbol %{public}s in %{public}s", "DestroyRemoteSessionSinkImpl",
68               "libremote_session_sink.z.so");
69         return AVSESSION_ERROR;
70     }
71     destroyRemoteSessionSinkImpl(sinkImpl_);
72 
73     if (handle_ != nullptr) {
74 #ifndef TEST_COVERAGE
75         dlclose(handle_);
76 #endif
77     }
78     return AVSESSION_SUCCESS;
79 }
80 
CastSessionFromRemote(const sptr<AVSessionItem> & session,const std::string & sourceSessionId,const std::string & sourceDevice,const std::string & sinkDevice,const std::string & sourceCap)81 int32_t RemoteSessionSinkProxy::CastSessionFromRemote(const sptr <AVSessionItem>& session,
82                                                       const std::string& sourceSessionId,
83                                                       const std::string& sourceDevice,
84                                                       const std::string& sinkDevice,
85                                                       const std::string& sourceCap)
86 {
87     CHECK_AND_RETURN_RET_LOG(sinkImpl_ != nullptr, AVSESSION_ERROR, "sinkImpl_ is nullptr");
88     int32_t ret = sinkImpl_->CastSessionFromRemote(session, sourceSessionId, sourceDevice, sinkDevice, sourceCap);
89     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "source CastSessionFromRemote error");
90     return AVSESSION_SUCCESS;
91 }
92 
CancelCastSession()93 int32_t RemoteSessionSinkProxy::CancelCastSession()
94 {
95     CHECK_AND_RETURN_RET_LOG(sinkImpl_ != nullptr, AVSESSION_ERROR, "sinkImpl_ is nullptr");
96     int32_t ret = sinkImpl_->CancelCastSession();
97     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "source CancelCastSession error");
98     return AVSESSION_SUCCESS;
99 }
100 
SetControlCommand(const AVControlCommand & command)101 int32_t RemoteSessionSinkProxy::SetControlCommand(const AVControlCommand& command)
102 {
103     AVSESSION_TRACE_SYNC_START("RemoteSessionSinkProxy::SetControlCommand");
104     CHECK_AND_RETURN_RET_LOG(sinkImpl_ != nullptr, AVSESSION_ERROR, "sinkImpl_ is nullptr");
105     int32_t ret = sinkImpl_->SetControlCommand(command);
106     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "source SetControlCommand error");
107     return AVSESSION_SUCCESS;
108 }
109 
SetCommonCommand(const std::string & commonCommand,const AAFwk::WantParams & commandArgs)110 int32_t RemoteSessionSinkProxy::SetCommonCommand(const std::string& commonCommand,
111     const AAFwk::WantParams& commandArgs)
112 {
113     AVSESSION_TRACE_SYNC_START("RemoteSessionSinkProxy::SetCommonCommand");
114     CHECK_AND_RETURN_RET_LOG(sinkImpl_ != nullptr, AVSESSION_ERROR, "sinkImpl_ is nullptr");
115     int32_t ret = sinkImpl_->SetCommonCommand(commonCommand, commandArgs);
116     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "Source SetCommonCommand error");
117     return AVSESSION_SUCCESS;
118 }
119 } // namespace OHOS::AVSession
120