1 /*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "interaction.h"
17 #include "common/common_macro.h"
18 #include "common/event_comm.h"
19 #include "common/reflect_registration.h"
20 #include "common/sharing_log.h"
21 #include "interaction/interaction_manager.h"
22 #include "magic_enum.hpp"
23 #include "scene/base_scene.h"
24
25 namespace OHOS {
26 namespace Sharing {
27
~Interaction()28 Interaction::~Interaction()
29 {
30 SHARING_LOGD("id: %{public}d.", GetId());
31 }
32
CreateScene(const std::string & className)33 bool Interaction::CreateScene(const std::string &className)
34 {
35 SHARING_LOGD("trace.");
36 scene_ = ClassReflector<BaseScene>::Class2Instance(className);
37 if (scene_ == nullptr) {
38 SHARING_LOGE("create scene error.");
39 return false;
40 }
41
42 scene_->SetInteractionId(GetId());
43 scene_->Initialize();
44 scene_->SetSharingAdapter(shared_from_this());
45
46 return true;
47 }
48
OnDomainMsg(std::shared_ptr<BaseDomainMsg> & msg)49 void Interaction::OnDomainMsg(std::shared_ptr<BaseDomainMsg> &msg)
50 {
51 SHARING_LOGD("trace.");
52 if (scene_) {
53 scene_->OnDomainMsg(msg);
54 }
55 }
56
ForwardDomainMsg(std::shared_ptr<BaseDomainMsg> & msg)57 void Interaction::ForwardDomainMsg(std::shared_ptr<BaseDomainMsg> &msg)
58 {
59 SHARING_LOGD("trace.");
60 InteractionManager::GetInstance().SendDomainMsg(msg);
61 }
62
ReleaseScene(uint32_t sceneId)63 void Interaction::ReleaseScene(uint32_t sceneId)
64 {
65 SHARING_LOGD("trace.");
66 auto interactionMsg = std::make_shared<InteractionEventMsg>();
67 interactionMsg->toMgr = ModuleType::MODULE_INTERACTION;
68 interactionMsg->type = EVENT_INTERACTIONMGR_DESTROY_INTERACTION;
69
70 SharingEvent event;
71 event.eventMsg = std::move(interactionMsg);
72 event.eventMsg->fromMgr = ModuleType::MODULE_INTERACTION;
73 event.eventMsg->dstId = GetId();
74 SendEvent(event);
75 }
76
OnSceneNotifyDestroyed(uint32_t sceneId)77 void Interaction::OnSceneNotifyDestroyed(uint32_t sceneId)
78 {
79 SHARING_LOGE("scene destroyed will remove interactionId: %{public}u.", GetId());
80 auto interactionMsg = std::make_shared<InteractionEventMsg>();
81 interactionMsg->toMgr = ModuleType::MODULE_INTERACTION;
82 interactionMsg->type = EVENT_INTERACTIONMGR_REMOVE_INTERACTION;
83
84 SharingEvent event;
85 event.eventMsg = std::move(interactionMsg);
86 event.eventMsg->fromMgr = ModuleType::MODULE_INTERACTION;
87 event.eventMsg->dstId = GetId();
88 SendEvent(event);
89 }
90
Destroy()91 void Interaction::Destroy()
92 {
93 SHARING_LOGE("trace.");
94 if (scene_) {
95 scene_.reset();
96 }
97 }
98
HandleEvent(SharingEvent & event)99 int32_t Interaction::HandleEvent(SharingEvent &event)
100 {
101 SHARING_LOGD("trace.");
102 RETURN_INVALID_IF_NULL(event.eventMsg);
103 SHARING_LOGI("fromMgr: %{public}u, srcId: %{public}u, toMgr: %{public}u, dstId: %{public}u, event: %{public}s.",
104 event.eventMsg->fromMgr, event.eventMsg->srcId, event.eventMsg->toMgr, event.eventMsg->dstId,
105 std::string(magic_enum::enum_name(event.eventMsg->type)).c_str());
106 auto interactionMsg = ConvertEventMsg<InteractionEventMsg>(event);
107 auto contextId = interactionMsg->contextId ? interactionMsg->contextId : interactionMsg->srcId;
108 auto agentId = interactionMsg->agentId;
109 auto agentType = interactionMsg->agentType;
110 auto errorCode = interactionMsg->errorCode;
111
112 switch (event.eventMsg->type) {
113 case EVENT_INTERACTION_MSG_ERROR: {
114 SHARING_LOGI("interaction handle error, errorCode: %{public}d.", errorCode);
115 if (scene_) {
116 scene_->OnInnerError(contextId, agentId, errorCode);
117 if (errorCode == ERR_NETWORK_ERROR || errorCode == ERR_CONNECTION_FAILURE ||
118 errorCode == ERR_INTERACTION_FAILURE || errorCode == ERR_PROTOCOL_INTERACTION_TIMEOUT ||
119 errorCode == ERR_INTAKE_TIMEOUT) {
120 SHARING_LOGE("on inner destroy network error.");
121 scene_->OnInnerDestroy(contextId, agentId, agentType);
122 DestroyAgent(contextId, agentId);
123 }
124 }
125 break;
126 }
127 case EVENT_INTERACTION_STATE_AGENT_DESTROYED:
128 case EVENT_INTERACTION_STATE_CONTEXT_DESTROYED: {
129 SHARING_LOGI("interaction handle event, destroy result.");
130 if (scene_) {
131 scene_->OnInnerDestroy(contextId, agentId, agentType);
132 }
133 break;
134 }
135 default:
136 SHARING_LOGI("interaction forward event to scene.");
137 if (scene_) {
138 scene_->OnInnerEvent(event);
139 }
140 break;
141 }
142
143 return 0;
144 }
145
SendAgentEvent(uint32_t contextId,uint32_t agentId,EventType eventType,std::function<void (std::shared_ptr<AgentEventMsg> &)> setupMsg)146 int32_t Interaction::SendAgentEvent(uint32_t contextId, uint32_t agentId, EventType eventType,
147 std::function<void(std::shared_ptr<AgentEventMsg> &)> setupMsg)
148 {
149 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
150 auto agentMsg = std::make_shared<AgentEventMsg>();
151 if (agentMsg == nullptr) {
152 return -1;
153 }
154 agentMsg->type = eventType;
155 agentMsg->toMgr = ModuleType::MODULE_CONTEXT;
156 agentMsg->dstId = contextId;
157 agentMsg->agentId = agentId;
158 if (setupMsg) {
159 setupMsg(agentMsg);
160 }
161
162 int32_t ret = NotifyEvent(agentMsg);
163 if (ret != -1) {
164 SHARING_LOGI("%{public}d success agentId: %{public}u.", eventType, agentId);
165 } else {
166 SHARING_LOGE("%{public}d failed agentId: %{public}u.", eventType, agentId);
167 }
168
169 return ret;
170 }
171
NotifyEvent(EventMsg::Ptr eventMsg)172 int32_t Interaction::NotifyEvent(EventMsg::Ptr eventMsg)
173 {
174 SHARING_LOGD("trace.");
175 RETURN_INVALID_IF_NULL(eventMsg);
176 SharingEvent event;
177 event.eventMsg = std::move(eventMsg);
178 event.eventMsg->fromMgr = ModuleType::MODULE_INTERACTION;
179 event.eventMsg->srcId = GetId();
180 return SendEvent(event);
181 }
182
CreateContext(uint32_t & contextId)183 int32_t Interaction::CreateContext(uint32_t &contextId)
184 {
185 SHARING_LOGD("trace.");
186 auto contextMsg = std::make_shared<ContextEventMsg>();
187 contextMsg->type = EventType::EVENT_CONTEXTMGR_CREATE;
188 contextMsg->toMgr = ModuleType::MODULE_CONTEXT;
189
190 SharingEvent event;
191 event.eventMsg = contextMsg;
192 event.eventMsg->fromMgr = ModuleType::MODULE_INTERACTION;
193 event.eventMsg->srcId = GetId();
194 int32_t ret = SendSyncEvent(event);
195 if (ret != -1) {
196 contextId = contextMsg->dstId;
197 SHARING_LOGI("create context success contextId: %{public}u.", contextId);
198 } else {
199 SHARING_LOGE("create context failed.");
200 }
201
202 return 0;
203 }
204
DestroyContext(uint32_t contextId)205 int32_t Interaction::DestroyContext(uint32_t contextId)
206 {
207 SHARING_LOGD("contextId: %{public}u.", contextId);
208 auto contextMsg = std::make_shared<ContextEventMsg>();
209 contextMsg->type = EventType::EVENT_CONTEXTMGR_DESTROY;
210 contextMsg->toMgr = ModuleType::MODULE_CONTEXT;
211 contextMsg->dstId = contextId;
212
213 int32_t ret = NotifyEvent(contextMsg);
214 if (ret != -1) {
215 SHARING_LOGI("destroy context success contextId: %{public}u.", contextId);
216 } else {
217 SHARING_LOGE("destroy context failed.");
218 }
219
220 return ret;
221 }
222
CreateAgent(uint32_t & contextId,uint32_t & agentId,AgentType agentType,std::string sessionName)223 int32_t Interaction::CreateAgent(uint32_t &contextId, uint32_t &agentId, AgentType agentType, std::string sessionName)
224 {
225 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
226 auto contextMsg = std::make_shared<ContextEventMsg>();
227 contextMsg->type = EventType::EVENT_CONTEXTMGR_AGENT_CREATE;
228 contextMsg->toMgr = ModuleType::MODULE_CONTEXT;
229 contextMsg->dstId = contextId;
230 contextMsg->agentType = agentType;
231 contextMsg->className = std::move(sessionName);
232 contextMsg->agentId = agentId;
233
234 SharingEvent event;
235 event.eventMsg = contextMsg;
236 event.eventMsg->fromMgr = ModuleType::MODULE_INTERACTION;
237 event.eventMsg->srcId = GetId();
238 int32_t ret = SendSyncEvent(event);
239
240 SHARING_LOGI("notify create agent ret: %{public}d agentId: %{public}u.", ret, contextMsg->agentId);
241 if (ret != -1) {
242 if ((agentId == contextMsg->agentId) || contextMsg->agentId == INVALID_ID) {
243 agentId = INVALID_ID;
244 } else {
245 agentId = contextMsg->agentId;
246 }
247 contextId = contextMsg->dstId;
248 SHARING_LOGI("notify create agent success agentId: %{public}u.", agentId);
249 } else {
250 SHARING_LOGE("notify create agent failed!");
251 }
252
253 return ret;
254 }
255
DestroyAgent(uint32_t contextId,uint32_t agentId)256 int32_t Interaction::DestroyAgent(uint32_t contextId, uint32_t agentId)
257 {
258 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
259 auto contextMsg = std::make_shared<AgentEventMsg>();
260 contextMsg->type = EventType::EVENT_CONTEXT_AGENT_DESTROY;
261 contextMsg->toMgr = ModuleType::MODULE_CONTEXT;
262 contextMsg->dstId = contextId;
263 contextMsg->agentId = agentId;
264
265 int32_t ret = NotifyEvent(contextMsg);
266 if (ret != -1) {
267 SHARING_LOGI("destroy agent success agentId: %{public}u.", agentId);
268 } else {
269 SHARING_LOGE("destroy agent failed, agentId: %{public}u.", agentId);
270 }
271
272 return ret;
273 }
274
Stop(uint32_t contextId,uint32_t agentId)275 int32_t Interaction::Stop(uint32_t contextId, uint32_t agentId)
276 {
277 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
278 return 0;
279 }
280
Start(uint32_t contextId,uint32_t agentId)281 int32_t Interaction::Start(uint32_t contextId, uint32_t agentId)
282 {
283 return SendAgentEvent(contextId, agentId, EventType::EVENT_AGENT_START);
284 }
285
Pause(uint32_t contextId,uint32_t agentId,MediaType mediaType)286 int32_t Interaction::Pause(uint32_t contextId, uint32_t agentId, MediaType mediaType)
287 {
288 return SendAgentEvent(contextId, agentId, EventType::EVENT_AGENT_PAUSE,
289 [mediaType](auto &msg) { msg->mediaType = mediaType; });
290 }
291
Resume(uint32_t contextId,uint32_t agentId,MediaType mediaType)292 int32_t Interaction::Resume(uint32_t contextId, uint32_t agentId, MediaType mediaType)
293 {
294 return SendAgentEvent(contextId, agentId, EventType::EVENT_AGENT_RESUME,
295 [mediaType](auto &msg) { msg->mediaType = mediaType; });
296 }
297
ForwardEvent(uint32_t contextId,uint32_t agentId,SharingEvent & event,bool isSync)298 int32_t Interaction::ForwardEvent(uint32_t contextId, uint32_t agentId, SharingEvent &event, bool isSync)
299 {
300 SHARING_LOGI("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
301 RETURN_INVALID_IF_NULL(event.eventMsg);
302 event.eventMsg->fromMgr = MODULE_INTERACTION;
303 event.eventMsg->srcId = GetId();
304 event.listenerType = CLASS_TYPE_SCHEDULER;
305
306 if (isSync) {
307 return SendSyncEvent(event);
308 } else {
309 return SendEvent(event);
310 }
311 }
312
Play(uint32_t contextId,uint32_t agentId)313 int32_t Interaction::Play(uint32_t contextId, uint32_t agentId)
314 {
315 return SendAgentEvent(contextId, agentId, EventType::EVENT_AGENT_PLAY_START);
316 }
317
Close(uint32_t contextId,uint32_t agentId)318 int32_t Interaction::Close(uint32_t contextId, uint32_t agentId)
319 {
320 return SendAgentEvent(contextId, agentId, EventType::EVENT_AGENT_PLAY_STOP);
321 }
322
SetVolume(uint32_t contextId,uint32_t agentId,float volume)323 int32_t Interaction::SetVolume(uint32_t contextId, uint32_t agentId, float volume)
324 {
325 return SendAgentEvent(contextId, agentId, EventType::EVENT_AGENT_CHANNEL_SETVOLUME,
326 [volume](auto &msg) { msg->volume = volume; });
327 }
328
SetKeyPlay(uint32_t contextId,uint32_t agentId,uint64_t surfaceId,bool keyFrame)329 int32_t Interaction::SetKeyPlay(uint32_t contextId, uint32_t agentId, uint64_t surfaceId, bool keyFrame)
330 {
331 return SendAgentEvent(contextId, agentId, EventType::EVENT_AGENT_CHANNEL_SETSCENETYPE,
332 [surfaceId, keyFrame](auto &msg) {
333 msg->surfaceId = surfaceId;
334 msg->sceneType = keyFrame ? SceneType::BACKGROUND : SceneType::FOREGROUND;
335 });
336 }
337
SetKeyRedirect(uint32_t contextId,uint32_t agentId,uint64_t surfaceId,bool keyRedirect)338 int32_t Interaction::SetKeyRedirect(uint32_t contextId, uint32_t agentId, uint64_t surfaceId, bool keyRedirect)
339 {
340 return SendAgentEvent(contextId, agentId, EventType::EVENT_AGENT_CHANNEL_SETKEYREDIRECT,
341 [surfaceId, keyRedirect](auto &msg) {
342 msg->surfaceId = surfaceId;
343 msg->keyRedirect = keyRedirect;
344 });
345 }
346
AppendSurface(uint32_t contextId,uint32_t agentId,sptr<Surface> surface,SceneType sceneType)347 int32_t Interaction::AppendSurface(uint32_t contextId, uint32_t agentId, sptr<Surface> surface, SceneType sceneType)
348 {
349 RETURN_INVALID_IF_NULL(surface);
350 return SendAgentEvent(contextId, agentId, EventType::EVENT_AGENT_CHANNEL_APPENDSURFACE,
351 [this, surface, sceneType](auto &msg) {
352 msg->surface = surface;
353 msg->sceneType = sceneType;
354 msg->requestId = GetRequestId();
355 });
356 }
357
RemoveSurface(uint32_t contextId,uint32_t agentId,uint64_t surfaceId)358 int32_t Interaction::RemoveSurface(uint32_t contextId, uint32_t agentId, uint64_t surfaceId)
359 {
360 return SendAgentEvent(contextId, agentId, EventType::EVENT_AGENT_CHANNEL_REMOVESURFACE,
361 [surfaceId, this](auto &msg) {
362 msg->surfaceId = surfaceId;
363 msg->requestId = GetRequestId();
364 });
365 }
366
DestroyWindow(int32_t windowId)367 int32_t Interaction::DestroyWindow(int32_t windowId)
368 {
369 SHARING_LOGD("trace.");
370 (void)windowId;
371 return 0;
372 }
373
CreateWindow(int32_t & windowId,WindowProperty & windowProperty)374 int32_t Interaction::CreateWindow(int32_t &windowId, WindowProperty &windowProperty)
375 {
376 SHARING_LOGD("trace.");
377 (void)windowId;
378 (void)windowProperty;
379 return 0;
380 }
381
Hide(int32_t windowId)382 int32_t Interaction::Hide(int32_t windowId)
383 {
384 SHARING_LOGD("trace.");
385 (void)windowId;
386 return 0;
387 }
388
Show(int32_t windowId)389 int32_t Interaction::Show(int32_t windowId)
390 {
391 SHARING_LOGD("trace.");
392 (void)windowId;
393 return 0;
394 }
395
SetFullScreen(int32_t windowId,bool isFull)396 int32_t Interaction::SetFullScreen(int32_t windowId, bool isFull)
397 {
398 SHARING_LOGD("trace.");
399 (void)windowId;
400 (void)isFull;
401 return 0;
402 }
403
MoveTo(int32_t windowId,int32_t x,int32_t y)404 int32_t Interaction::MoveTo(int32_t windowId, int32_t x, int32_t y)
405 {
406 SHARING_LOGD("trace.");
407 (void)windowId;
408 (void)x;
409 (void)y;
410 return 0;
411 }
412
GetSurface(int32_t windowId,sptr<Surface> & surface)413 int32_t Interaction::GetSurface(int32_t windowId, sptr<Surface> &surface)
414 {
415 SHARING_LOGD("trace.");
416 (void)windowId;
417 (void)surface;
418 return 0;
419 }
420
ReSize(int32_t windowId,int32_t width,int32_t height)421 int32_t Interaction::ReSize(int32_t windowId, int32_t width, int32_t height)
422 {
423 SHARING_LOGD("trace.");
424 (void)windowId;
425 (void)width;
426 (void)height;
427 return 0;
428 }
429
430 } // namespace Sharing
431 } // namespace OHOS