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 {
21 static std::string g_sinkLibraryPath = std::string(SYSTEM_LIB_PATH) + std::string("libremote_session_sink.z.so");
22
RemoteSessionSinkProxy()23 RemoteSessionSinkProxy::RemoteSessionSinkProxy()
24 {
25 LoadSinkImplement();
26 }
27
~RemoteSessionSinkProxy()28 RemoteSessionSinkProxy::~RemoteSessionSinkProxy()
29 {
30 UnLoadSinkImplement();
31 }
32
LoadSinkImplement()33 int32_t RemoteSessionSinkProxy::LoadSinkImplement() __attribute__((no_sanitize("cfi")))
34 {
35 char sinkLibraryRealPath[PATH_MAX] = { 0x00 };
36 if (realpath(g_sinkLibraryPath.c_str(), sinkLibraryRealPath) == nullptr) {
37 SLOGE("check path failed %{public}s", g_sinkLibraryPath.c_str());
38 return AVSESSION_ERROR;
39 }
40 handle_ = dlopen(sinkLibraryRealPath, RTLD_NOW);
41 if (handle_ == nullptr) {
42 SLOGE("Failed to open extension library %{public}s, reason: %{public}sn", g_sinkLibraryPath.c_str(), dlerror());
43 return AVSESSION_ERROR;
44 }
45 using SinkImpl = RemoteSessionSinkImpl* (*)();
46
47 auto createRemoteSessionSinkImpl = (SinkImpl)(dlsym(handle_, "CreateRemoteSessionSinkImpl"));
48 if (createRemoteSessionSinkImpl == nullptr) {
49 if (handle_ != nullptr) {
50 dlclose(handle_);
51 }
52 SLOGE("Failed to get extension symbol %{public}s in %{public}s", "RemoteSessionSinkImpl",
53 g_sinkLibraryPath.c_str());
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 dlclose(handle_);
68 }
69 SLOGE("Failed to get extension symbol %{public}s in %{public}s", "DestroyRemoteSessionSinkImpl",
70 g_sinkLibraryPath.c_str());
71 return AVSESSION_ERROR;
72 }
73 destroyRemoteSessionSinkImpl(sinkImpl_);
74
75 if (handle_ != nullptr) {
76 dlclose(handle_);
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 } // namespace OHOS::AVSession