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 "remote_session_source_impl.h"
18 #include "avsession_trace.h"
19 #include "remote_session_source_proxy.h"
20
21 namespace OHOS::AVSession {
22 static const std::string g_sourceLibraryPath = std::string(SYSTEM_LIB_PATH) +
23 std::string("libremote_session_source.z.so");
24
RemoteSessionSourceProxy()25 RemoteSessionSourceProxy::RemoteSessionSourceProxy()
26 {
27 LoadSourceImplement();
28 }
~RemoteSessionSourceProxy()29 RemoteSessionSourceProxy::~RemoteSessionSourceProxy()
30 {
31 UnLoadSourceImplement();
32 }
33
LoadSourceImplement()34 int32_t RemoteSessionSourceProxy::LoadSourceImplement() __attribute__((no_sanitize("cfi")))
35 {
36 char sourceLibraryRealPath[PATH_MAX] = { 0x00 };
37 if (realpath(g_sourceLibraryPath.c_str(), sourceLibraryRealPath) == nullptr) {
38 SLOGE("check path failed %{public}s", g_sourceLibraryPath.c_str());
39 return AVSESSION_ERROR;
40 }
41 handle_ = dlopen(sourceLibraryRealPath, RTLD_NOW);
42 if (handle_ == nullptr) {
43 SLOGE("Failed to open library %{public}s, reason: %{public}sn", g_sourceLibraryPath.c_str(), dlerror());
44 return AVSESSION_ERROR;
45 }
46 using SourceImpl = RemoteSessionSourceImpl* (*)();
47
48 auto createRemoteSessionSourceImpl = (SourceImpl)(dlsym(handle_, "CreateRemoteSessionSourceImpl"));
49 if (createRemoteSessionSourceImpl == nullptr) {
50 if (handle_ != nullptr) {
51 dlclose(handle_);
52 }
53 SLOGE("Failed to get extension symbol %{public}s in %{public}s", "RemoteSessionSourceImpl",
54 g_sourceLibraryPath.c_str());
55 return AVSESSION_ERROR;
56 }
57
58 sourceImpl_ = createRemoteSessionSourceImpl();
59 return AVSESSION_SUCCESS;
60 }
61
UnLoadSourceImplement()62 int32_t RemoteSessionSourceProxy::UnLoadSourceImplement() __attribute__((no_sanitize("cfi")))
63 {
64 using SourceImpl = void(*)(RemoteSessionSourceImpl*);
65 auto destroyRemoteSessionSourceImpl = (SourceImpl)(dlsym(handle_, "DestroyRemoteSessionSourceImpl"));
66 if (destroyRemoteSessionSourceImpl == nullptr) {
67 if (handle_ != nullptr) {
68 dlclose(handle_);
69 }
70 SLOGE("Failed to get extension symbol %{public}s in %{public}s", "DestroyRemoteSessionSourceImpl",
71 g_sourceLibraryPath.c_str());
72 return AVSESSION_ERROR;
73 }
74 destroyRemoteSessionSourceImpl(sourceImpl_);
75 if (handle_ != nullptr) {
76 dlclose(handle_);
77 }
78 return AVSESSION_SUCCESS;
79 }
80
CastSessionToRemote(const sptr<AVSessionItem> & session,const std::string & sourceDevice,const std::string & sinkDevice,const std::string & sinkCapability)81 int32_t RemoteSessionSourceProxy::CastSessionToRemote(const sptr <AVSessionItem>& session,
82 const std::string& sourceDevice,
83 const std::string& sinkDevice,
84 const std::string& sinkCapability)
85 {
86 AVSESSION_TRACE_SYNC_START("RemoteSessionSourceProxy::CastSessionToRemote");
87 CHECK_AND_RETURN_RET_LOG(sourceImpl_ != nullptr, AVSESSION_ERROR, "sourceImpl_ is nullptr");
88 int32_t ret = sourceImpl_->CastSessionToRemote(session, sourceDevice, sinkDevice, sinkCapability);
89 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "source CastSessionToRemote error");
90 return AVSESSION_SUCCESS;
91 }
92
CancelCastAudio(const std::string & sinkDevice)93 int32_t RemoteSessionSourceProxy::CancelCastAudio(const std::string& sinkDevice)
94 {
95 CHECK_AND_RETURN_RET_LOG(sourceImpl_ != nullptr, AVSESSION_ERROR, "sourceImpl_ is nullptr");
96 int32_t ret = sourceImpl_->CancelCastAudio(sinkDevice);
97 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "source CastSessionToLocal error");
98 return AVSESSION_SUCCESS;
99 }
100
SetAVMetaData(const AVMetaData & metaData)101 int32_t RemoteSessionSourceProxy::SetAVMetaData(const AVMetaData& metaData)
102 {
103 AVSESSION_TRACE_SYNC_START("RemoteSessionSourceProxy::SetAVMetaData");
104 CHECK_AND_RETURN_RET_LOG(sourceImpl_ != nullptr, AVSESSION_ERROR, "sourceImpl_ is nullptr");
105 int32_t ret = sourceImpl_->SetAVMetaData(metaData);
106 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "source SetAVMetaData error");
107 return AVSESSION_SUCCESS;
108 }
109
SetAVPlaybackState(const AVPlaybackState & state)110 int32_t RemoteSessionSourceProxy::SetAVPlaybackState(const AVPlaybackState& state)
111 {
112 AVSESSION_TRACE_SYNC_START("RemoteSessionSourceProxy::SetAVPlaybackState");
113 CHECK_AND_RETURN_RET_LOG(sourceImpl_ != nullptr, AVSESSION_ERROR, "sourceImpl_ is nullptr");
114 int32_t ret = sourceImpl_->SetAVPlaybackState(state);
115 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "source SetAVPlaybackState error");
116 return AVSESSION_SUCCESS;
117 }
118 } // namespace OHOS::AVSession