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