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