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 <openssl/crypto.h>
18 #include "avsession_trace.h"
19 #include "remote_session_sink_proxy.h"
20 
21 namespace OHOS::AVSession {
RemoteSessionSinkProxy()22 RemoteSessionSinkProxy::RemoteSessionSinkProxy()
23 {
24     LoadSinkImplement();
25 }
26 
~RemoteSessionSinkProxy()27 RemoteSessionSinkProxy::~RemoteSessionSinkProxy()
28 {
29     UnLoadSinkImplement();
30 }
31 
LoadSinkImplement()32 int32_t RemoteSessionSinkProxy::LoadSinkImplement() __attribute__((no_sanitize("cfi")))
33 {
34     handle_ = dlopen("libremote_session_sink.z.so", RTLD_NOW);
35     if (handle_ == nullptr) {
36         SLOGE("Failed to open extension library %{public}s, reason: %{public}sn",
37             "libremote_session_sink.z.so", dlerror());
38         return AVSESSION_ERROR;
39     }
40     using SinkImpl = RemoteSessionSinkImpl* (*)();
41 
42     auto createRemoteSessionSinkImpl = (SinkImpl)(dlsym(handle_, "CreateRemoteSessionSinkImpl"));
43     if (createRemoteSessionSinkImpl == nullptr) {
44         if (handle_ != nullptr) {
45 #ifndef TEST_COVERAGE
46             if (handle_ != nullptr) {
47                 OPENSSL_thread_stop();
48             }
49             dlclose(handle_);
50 #endif
51         }
52         SLOGE("Failed to get extension symbol %{public}s in %{public}s",
53             "RemoteSessionSinkImpl", "libremote_session_sink.z.so");
54         return AVSESSION_ERROR;
55     }
56 
57     sinkImpl_ = createRemoteSessionSinkImpl();
58     return AVSESSION_SUCCESS;
59 }
60 
UnLoadSinkImplement()61 int32_t RemoteSessionSinkProxy::UnLoadSinkImplement() __attribute__((no_sanitize("cfi")))
62 {
63     using SinkImpl = void(*)(RemoteSessionSinkImpl*);
64     auto destroyRemoteSessionSinkImpl = (SinkImpl)(dlsym(handle_, "DestroyRemoteSessionSinkImpl"));
65     if (destroyRemoteSessionSinkImpl == nullptr) {
66         if (handle_ != nullptr) {
67 #ifndef TEST_COVERAGE
68             if (handle_ != nullptr) {
69                 OPENSSL_thread_stop();
70             }
71             dlclose(handle_);
72 #endif
73         }
74         SLOGE("Failed to get extension symbol %{public}s in %{public}s", "DestroyRemoteSessionSinkImpl",
75               "libremote_session_sink.z.so");
76         return AVSESSION_ERROR;
77     }
78     destroyRemoteSessionSinkImpl(sinkImpl_);
79 
80     if (handle_ != nullptr) {
81 #ifndef TEST_COVERAGE
82         if (handle_ != nullptr) {
83             OPENSSL_thread_stop();
84         }
85         dlclose(handle_);
86 #endif
87     }
88     return AVSESSION_SUCCESS;
89 }
90 
CastSessionFromRemote(const sptr <AVSessionItem>& session, const std::string& sourceSessionId, const std::string& sourceDevice, const std::string& sinkDevice, const std::string& sourceCap)91 int32_t RemoteSessionSinkProxy::CastSessionFromRemote(const sptr <AVSessionItem>& session,
92                                                       const std::string& sourceSessionId,
93                                                       const std::string& sourceDevice,
94                                                       const std::string& sinkDevice,
95                                                       const std::string& sourceCap)
96 {
97     CHECK_AND_RETURN_RET_LOG(sinkImpl_ != nullptr, AVSESSION_ERROR, "sinkImpl_ is nullptr");
98     int32_t ret = sinkImpl_->CastSessionFromRemote(session, sourceSessionId, sourceDevice, sinkDevice, sourceCap);
99     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "source CastSessionFromRemote error");
100     return AVSESSION_SUCCESS;
101 }
102 
CancelCastSession()103 int32_t RemoteSessionSinkProxy::CancelCastSession()
104 {
105     CHECK_AND_RETURN_RET_LOG(sinkImpl_ != nullptr, AVSESSION_ERROR, "sinkImpl_ is nullptr");
106     int32_t ret = sinkImpl_->CancelCastSession();
107     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "source CancelCastSession error");
108     return AVSESSION_SUCCESS;
109 }
110 
SetControlCommand(const AVControlCommand& command)111 int32_t RemoteSessionSinkProxy::SetControlCommand(const AVControlCommand& command)
112 {
113     AVSESSION_TRACE_SYNC_START("RemoteSessionSinkProxy::SetControlCommand");
114     CHECK_AND_RETURN_RET_LOG(sinkImpl_ != nullptr, AVSESSION_ERROR, "sinkImpl_ is nullptr");
115     int32_t ret = sinkImpl_->SetControlCommand(command);
116     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "source SetControlCommand error");
117     return AVSESSION_SUCCESS;
118 }
119 
SetCommonCommand(const std::string& commonCommand, const AAFwk::WantParams& commandArgs)120 int32_t RemoteSessionSinkProxy::SetCommonCommand(const std::string& commonCommand,
121     const AAFwk::WantParams& commandArgs)
122 {
123     AVSESSION_TRACE_SYNC_START("RemoteSessionSinkProxy::SetCommonCommand");
124     CHECK_AND_RETURN_RET_LOG(sinkImpl_ != nullptr, AVSESSION_ERROR, "sinkImpl_ is nullptr");
125     int32_t ret = sinkImpl_->SetCommonCommand(commonCommand, commandArgs);
126     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "Source SetCommonCommand error");
127     return AVSESSION_SUCCESS;
128 }
129 } // namespace OHOS::AVSession
130