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 "remote_session_sink_impl.h"
17
18 #include "avsession_sysevent.h"
19 #include "avsession_trace.h"
20
21 namespace OHOS::AVSession {
RemoteSessionSinkImpl()22 RemoteSessionSinkImpl::RemoteSessionSinkImpl()
23 {
24 }
25
26 // LCOV_EXCL_START
CreateRemoteSessionSinkImpl()27 extern "C" RemoteSessionSinkImpl* CreateRemoteSessionSinkImpl()
28 {
29 return new(std::nothrow) RemoteSessionSinkImpl();
30 }
31 // LCOV_EXCL_STOP
32
33 // LCOV_EXCL_START
DestroyRemoteSessionSinkImpl(RemoteSessionSinkImpl * impl)34 extern "C" void DestroyRemoteSessionSinkImpl(RemoteSessionSinkImpl* impl)
35 {
36 delete(impl);
37 }
38 // LCOV_EXCL_STOP
39
40 // LCOV_EXCL_START
CastSessionFromRemote(const sptr<AVSessionItem> & session,const std::string & sourceSessionId,const std::string & sourceDevice,const std::string & sinkDevice,const std::string & sourceCap)41 int32_t RemoteSessionSinkImpl::CastSessionFromRemote(const sptr <AVSessionItem>& session,
42 const std::string& sourceSessionId,
43 const std::string& sourceDevice,
44 const std::string& sinkDevice,
45 const std::string& sourceCap)
46 {
47 syncer_ = std::make_shared<RemoteSessionSyncerImpl>(sourceSessionId, sourceDevice, sinkDevice);
48 CHECK_AND_RETURN_RET_LOG(syncer_ != nullptr, AVSESSION_ERROR, "syncer_ is nullptr");
49 int32_t ret = syncer_->Init();
50 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "syncer init failed");
51 session_ = session;
52 sourceDevice_ = sourceDevice;
53 RemoteSessionCapabilitySet::GetInstance().AddRemoteCapability(session->GetSessionId(), sourceDevice, sourceCap);
54
55 ret = syncer_->RegisterDisconnectNotifier([this](const std::string& deviceId) {
56 SLOGE("device %{public}s disconnected, sessionId is %{public}s", deviceId.c_str(),
57 session_->GetSessionId().c_str());
58 HISYSEVENT_FAULT("REMOTE_CONTROL_FAILED",
59 "BUNDLE_NAME", session_->GetDescriptor().elementName_.GetBundleName(),
60 "SESSION_TYPE", session_->GetDescriptor().sessionType_,
61 "AUDIO_STATUS", HISYSEVENT_GET_AUDIO_STATUS(session_->GetUid()),
62 "ERROR_TYPE", "REMOTE_DISCONNECTED",
63 "ERROR_INFO", "remote disconnected");
64 return AVSESSION_SUCCESS;
65 });
66
67 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "AddDisconnectNotifier failed");
68
69 ret = syncer_->RegisterDataNotifier([this](const SessionDataCategory category, const std::string& deviceId) {
70 SLOGI("device %{public}s category %{public}d changed", deviceId.c_str(), category);
71 CHECK_AND_RETURN_RET_LOG(session_ != nullptr && syncer_ != nullptr, AVSESSION_ERROR, "session_ is nullptr");
72
73 return HandleSessionDataCategory(category);
74 });
75 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "AddDataNotifier failed");
76 return AVSESSION_SUCCESS;
77 }
78 // LCOV_EXCL_STOP
79
80 // LCOV_EXCL_START
HandleSessionDataCategory(const SessionDataCategory category)81 int32_t RemoteSessionSinkImpl::HandleSessionDataCategory(const SessionDataCategory category)
82 {
83 if (category == SESSION_DATA_META) {
84 AVMetaData metaData;
85 AVSESSION_TRACE_SYNC_START("RemoteSessionSinkImpl::GetAVMetaData");
86 CHECK_AND_RETURN_RET_LOG(syncer_->GetAVMetaData(metaData) == AVSESSION_SUCCESS, AVSESSION_ERROR,
87 "GetAVMetaData failed");
88 CHECK_AND_RETURN_RET_LOG(session_->SetAVMetaData(metaData) == AVSESSION_SUCCESS, AVSESSION_ERROR,
89 "SetAVMetaData failed");
90 } else if (category == SESSION_DATA_PLAYBACK_STATE) {
91 AVPlaybackState playbackState;
92 AVSESSION_TRACE_SYNC_START("RemoteSessionSinkImpl::GetAVPlaybackState");
93 CHECK_AND_RETURN_RET_LOG(syncer_->GetAVPlaybackState(playbackState) == AVSESSION_SUCCESS, AVSESSION_ERROR,
94 "GetAVPlaybackState failed");
95 CHECK_AND_RETURN_RET_LOG(session_->SetAVPlaybackState(playbackState) == AVSESSION_SUCCESS, AVSESSION_ERROR,
96 "SetAVPlaybackState failed");
97 } else if (category == SESSION_DATA_SET_EVENT) {
98 std::string event;
99 AAFwk::WantParams args;
100 AVSESSION_TRACE_SYNC_START("RemoteSessionSinkImpl::SetSessionEvent");
101 CHECK_AND_RETURN_RET_LOG(syncer_->GetSessionEvent(event, args) == AVSESSION_SUCCESS, AVSESSION_ERROR,
102 "GetSessionEvent failed");
103 CHECK_AND_RETURN_RET_LOG(session_->SetSessionEvent(event, args) == AVSESSION_SUCCESS, AVSESSION_ERROR,
104 "SetSessionEvent failed");
105 } else if (category == SESSION_DATA_QUEUE_ITEMS) {
106 std::vector<AVQueueItem> items;
107 AVSESSION_TRACE_SYNC_START("RemoteSessionSinkImpl::Get & Set QueueItems");
108 CHECK_AND_RETURN_RET_LOG(syncer_->GetAVQueueItems(items) == AVSESSION_SUCCESS, AVSESSION_ERROR,
109 "GetAVQueueItems failed");
110 CHECK_AND_RETURN_RET_LOG(session_->SetAVQueueItems(items) == AVSESSION_SUCCESS, AVSESSION_ERROR,
111 "SetAVQueueItems failed");
112 } else if (category == SESSION_DATA_QUEUE_TITLE) {
113 std::string title;
114 AVSESSION_TRACE_SYNC_START("RemoteSessionSinkImpl::Get & Set QueueTitle");
115 CHECK_AND_RETURN_RET_LOG(syncer_->GetAVQueueTitle(title) == AVSESSION_SUCCESS, AVSESSION_ERROR,
116 "GetAVQueueTitle failed");
117 CHECK_AND_RETURN_RET_LOG(session_->SetAVQueueTitle(title) == AVSESSION_SUCCESS, AVSESSION_ERROR,
118 "SetAVQueueTitle failed");
119 } else if (category == SESSION_DATA_EXTRAS) {
120 AAFwk::WantParams extras;
121 AVSESSION_TRACE_SYNC_START("RemoteSessionSinkImpl::Get & Set Extras");
122 CHECK_AND_RETURN_RET_LOG(syncer_->GetExtras(extras) == AVSESSION_SUCCESS, AVSESSION_ERROR, "GetExtras failed");
123 CHECK_AND_RETURN_RET_LOG(session_->SetExtras(extras) == AVSESSION_SUCCESS, AVSESSION_ERROR,
124 "SetExtras failed");
125 } else {
126 SLOGE("category is illegal");
127 return AVSESSION_ERROR;
128 }
129 return AVSESSION_SUCCESS;
130 }
131 // LCOV_EXCL_STOP
132
133 // LCOV_EXCL_START
CancelCastSession()134 int32_t RemoteSessionSinkImpl::CancelCastSession()
135 {
136 CHECK_AND_RETURN_RET_LOG(session_ != nullptr, AVSESSION_ERROR, "session is nullptr");
137 RemoteSessionCapabilitySet::GetInstance().RemoveRemoteCapability(session_->GetSessionId(), sourceDevice_);
138 syncer_->Destroy();
139 syncer_ = nullptr;
140 return AVSESSION_SUCCESS;
141 }
142 // LCOV_EXCL_STOP
143
144 // LCOV_EXCL_START
SetControlCommand(const AVControlCommand & command)145 int32_t RemoteSessionSinkImpl::SetControlCommand(const AVControlCommand& command)
146 {
147 CHECK_AND_RETURN_RET_LOG(syncer_ != nullptr, AVSESSION_ERROR, "syncer is nullptr");
148 auto ret = syncer_->PutControlCommand(command);
149 if (ret != AVSESSION_SUCCESS && session_ != nullptr) {
150 HISYSEVENT_FAULT("REMOTE_CONTROL_FAILED",
151 "BUNDLE_NAME", session_->GetDescriptor().elementName_.GetBundleName(),
152 "SESSION_TYPE", session_->GetDescriptor().sessionType_,
153 "AUDIO_STATUS", HISYSEVENT_GET_AUDIO_STATUS(session_->GetUid()),
154 "ERROR_TYPE", "TIME_OUT",
155 "ERROR_INFO", "SetControlCommand time out");
156 }
157 return AVSESSION_SUCCESS;
158 }
159 // LCOV_EXCL_STOP
160
161 // LCOV_EXCL_START
SetCommonCommand(const std::string & commonCommand,const AAFwk::WantParams & commandArgs)162 int32_t RemoteSessionSinkImpl::SetCommonCommand(const std::string& commonCommand,
163 const AAFwk::WantParams& commandArgs)
164 {
165 CHECK_AND_RETURN_RET_LOG(syncer_ != nullptr, AVSESSION_ERROR, "syncer is nullptr");
166 auto ret = syncer_->PutCommonCommand(commonCommand, commandArgs);
167 if (ret != AVSESSION_SUCCESS && session_ != nullptr) {
168 HISYSEVENT_FAULT("REMOTE_CONTROL_FAILED",
169 "BUNDLE_NAME", session_->GetDescriptor().elementName_.GetBundleName(),
170 "SESSION_TYPE", session_->GetDescriptor().sessionType_,
171 "AUDIO_STATUS", HISYSEVENT_GET_AUDIO_STATUS(session_->GetUid()),
172 "ERROR_TYPE", "TIME_OUT",
173 "ERROR_INFO", "SetCommonCommand time out");
174 }
175 return AVSESSION_SUCCESS;
176 }
177 // LCOV_EXCL_STOP
178 } // namespace OHOS::AVSession
179