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