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_LOGD("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_LOGD("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_LOGD("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
NotifyEvent(EventMsg::Ptr eventMsg)146 int32_t Interaction::NotifyEvent(EventMsg::Ptr eventMsg)
147 {
148 SHARING_LOGD("trace.");
149 SharingEvent event;
150 event.eventMsg = std::move(eventMsg);
151 event.eventMsg->fromMgr = ModuleType::MODULE_INTERACTION;
152 event.eventMsg->srcId = GetId();
153 return SendEvent(event);
154 }
155
CreateContext(uint32_t & contextId)156 int32_t Interaction::CreateContext(uint32_t &contextId)
157 {
158 SHARING_LOGD("trace.");
159 auto contextMsg = std::make_shared<ContextEventMsg>();
160 contextMsg->type = EventType::EVENT_CONTEXTMGR_CREATE;
161 contextMsg->toMgr = ModuleType::MODULE_CONTEXT;
162
163 SharingEvent event;
164 event.eventMsg = contextMsg;
165 event.eventMsg->fromMgr = ModuleType::MODULE_INTERACTION;
166 event.eventMsg->srcId = GetId();
167 int32_t ret = SendSyncEvent(event);
168 if (ret != -1) {
169 contextId = contextMsg->dstId;
170 SHARING_LOGI("create context success contextId: %{public}u.", contextId);
171 } else {
172 SHARING_LOGE("create context failed.");
173 }
174
175 return 0;
176 }
177
DestroyContext(uint32_t contextId)178 int32_t Interaction::DestroyContext(uint32_t contextId)
179 {
180 SHARING_LOGD("contextId: %{public}u.", contextId);
181 auto contextMsg = std::make_shared<ContextEventMsg>();
182 contextMsg->type = EventType::EVENT_CONTEXTMGR_DESTROY;
183 contextMsg->toMgr = ModuleType::MODULE_CONTEXT;
184 contextMsg->dstId = contextId;
185
186 int32_t ret = NotifyEvent(contextMsg);
187 if (ret != -1) {
188 SHARING_LOGI("destroy context success contextId: %{public}u.", contextId);
189 } else {
190 SHARING_LOGE("destroy context failed.");
191 }
192
193 return ret;
194 }
195
CreateAgent(uint32_t & contextId,uint32_t & agentId,AgentType agentType,std::string sessionName)196 int32_t Interaction::CreateAgent(uint32_t &contextId, uint32_t &agentId, AgentType agentType, std::string sessionName)
197 {
198 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
199 auto contextMsg = std::make_shared<ContextEventMsg>();
200 contextMsg->type = EventType::EVENT_CONTEXTMGR_AGENT_CREATE;
201 contextMsg->toMgr = ModuleType::MODULE_CONTEXT;
202 contextMsg->dstId = contextId;
203 contextMsg->agentType = agentType;
204 contextMsg->className = std::move(sessionName);
205 contextMsg->agentId = agentId;
206
207 SharingEvent event;
208 event.eventMsg = contextMsg;
209 event.eventMsg->fromMgr = ModuleType::MODULE_INTERACTION;
210 event.eventMsg->srcId = GetId();
211 int32_t ret = SendSyncEvent(event);
212
213 SHARING_LOGI("notify create agent ret: %{public}d agentId: %{public}u.", ret, contextMsg->agentId);
214 if (ret != -1) {
215 if ((agentId == contextMsg->agentId) || contextMsg->agentId == INVALID_ID) {
216 agentId = INVALID_ID;
217 } else {
218 agentId = contextMsg->agentId;
219 }
220 contextId = contextMsg->dstId;
221 SHARING_LOGI("notify create agent success agentId: %{public}u.", agentId);
222 } else {
223 SHARING_LOGE("notify create agent failed!");
224 }
225
226 return ret;
227 }
228
DestroyAgent(uint32_t contextId,uint32_t agentId)229 int32_t Interaction::DestroyAgent(uint32_t contextId, uint32_t agentId)
230 {
231 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
232 auto contextMsg = std::make_shared<AgentEventMsg>();
233 contextMsg->type = EventType::EVENT_CONTEXT_AGENT_DESTROY;
234 contextMsg->toMgr = ModuleType::MODULE_CONTEXT;
235 contextMsg->dstId = contextId;
236 contextMsg->agentId = agentId;
237
238 int32_t ret = NotifyEvent(contextMsg);
239 if (ret != -1) {
240 SHARING_LOGI("destroy agent success agentId: %{public}u.", agentId);
241 } else {
242 SHARING_LOGE("destroy agent failed, agentId: %{public}u.", agentId);
243 }
244
245 return ret;
246 }
247
Stop(uint32_t contextId,uint32_t agentId)248 int32_t Interaction::Stop(uint32_t contextId, uint32_t agentId)
249 {
250 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
251 return 0;
252 }
253
Start(uint32_t contextId,uint32_t agentId)254 int32_t Interaction::Start(uint32_t contextId, uint32_t agentId)
255 {
256 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
257 auto agentMsg = std::make_shared<AgentEventMsg>();
258 agentMsg->type = EventType::EVENT_AGENT_START;
259 agentMsg->toMgr = ModuleType::MODULE_CONTEXT;
260 agentMsg->dstId = contextId;
261 agentMsg->agentId = agentId;
262 int32_t ret = NotifyEvent(agentMsg);
263 if (ret != -1) {
264 SHARING_LOGI("start agent success agentId: %{public}u.", agentId);
265 } else {
266 SHARING_LOGE("start agent failed, agentId: %{public}u.", agentId);
267 }
268
269 return ret;
270 }
271
Pause(uint32_t contextId,uint32_t agentId,MediaType mediaType)272 int32_t Interaction::Pause(uint32_t contextId, uint32_t agentId, MediaType mediaType)
273 {
274 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
275 auto agentMsg = std::make_shared<AgentEventMsg>();
276 agentMsg->type = EventType::EVENT_AGENT_PAUSE;
277 agentMsg->toMgr = ModuleType::MODULE_CONTEXT;
278 agentMsg->dstId = contextId;
279 agentMsg->agentId = agentId;
280 agentMsg->mediaType = mediaType;
281 int32_t ret = NotifyEvent(agentMsg);
282 if (ret != -1) {
283 SHARING_LOGI("pause agent success agentId: %{public}u.", agentId);
284 } else {
285 SHARING_LOGE("pause agent failed, agentId: %{public}u.", agentId);
286 }
287
288 return ret;
289 }
290
Resume(uint32_t contextId,uint32_t agentId,MediaType mediaType)291 int32_t Interaction::Resume(uint32_t contextId, uint32_t agentId, MediaType mediaType)
292 {
293 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
294 auto agentMsg = std::make_shared<AgentEventMsg>();
295 agentMsg->type = EventType::EVENT_AGENT_RESUME;
296 agentMsg->toMgr = ModuleType::MODULE_CONTEXT;
297 agentMsg->dstId = contextId;
298 agentMsg->agentId = agentId;
299 agentMsg->mediaType = mediaType;
300 int32_t ret = NotifyEvent(agentMsg);
301 if (ret != -1) {
302 SHARING_LOGI("resume agent success agentId: %{public}u.", agentId);
303 } else {
304 SHARING_LOGE("resume agent failed, agentId: %{public}u.", agentId);
305 }
306
307 return ret;
308 }
309
ForwardEvent(uint32_t contextId,uint32_t agentId,SharingEvent & event,bool isSync)310 int32_t Interaction::ForwardEvent(uint32_t contextId, uint32_t agentId, SharingEvent &event, bool isSync)
311 {
312 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
313 RETURN_INVALID_IF_NULL(event.eventMsg);
314 event.eventMsg->fromMgr = MODULE_INTERACTION;
315 event.eventMsg->srcId = GetId();
316 event.listenerType = CLASS_TYPE_SCHEDULER;
317
318 if (isSync) {
319 return SendSyncEvent(event);
320 } else {
321 return SendEvent(event);
322 }
323 }
324
Play(uint32_t contextId,uint32_t agentId)325 int32_t Interaction::Play(uint32_t contextId, uint32_t agentId)
326 {
327 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
328 auto agentMsg = std::make_shared<AgentEventMsg>();
329 agentMsg->type = EventType::EVENT_AGENT_PLAY_START;
330 agentMsg->toMgr = ModuleType::MODULE_CONTEXT;
331 agentMsg->dstId = contextId;
332 agentMsg->agentId = agentId;
333 int32_t ret = NotifyEvent(agentMsg);
334 if (ret != -1) {
335 SHARING_LOGI("agent play success agentId: %{public}u.", agentId);
336 } else {
337 SHARING_LOGE("agent play failed, agentId: %{public}u.", agentId);
338 }
339
340 return ret;
341 }
342
Close(uint32_t contextId,uint32_t agentId)343 int32_t Interaction::Close(uint32_t contextId, uint32_t agentId)
344 {
345 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
346 auto agentMsg = std::make_shared<AgentEventMsg>();
347 agentMsg->type = EventType::EVENT_AGENT_PLAY_STOP;
348 agentMsg->toMgr = ModuleType::MODULE_CONTEXT;
349 agentMsg->dstId = contextId;
350 agentMsg->agentId = agentId;
351 int32_t ret = NotifyEvent(agentMsg);
352 if (ret != -1) {
353 SHARING_LOGI("agent close success agentId: %{public}u.", agentId);
354 } else {
355 SHARING_LOGE("agent close failed, agentId: %{public}u.", agentId);
356 }
357
358 return ret;
359 }
360
SetVolume(uint32_t contextId,uint32_t agentId,float volume)361 int32_t Interaction::SetVolume(uint32_t contextId, uint32_t agentId, float volume)
362 {
363 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
364 auto agentMsg = std::make_shared<AgentEventMsg>();
365 agentMsg->type = EventType::EVENT_AGENT_CHANNEL_SETVOLUME;
366 agentMsg->toMgr = ModuleType::MODULE_CONTEXT;
367 agentMsg->dstId = contextId;
368 agentMsg->agentId = agentId;
369 agentMsg->volume = volume;
370 int32_t ret = NotifyEvent(agentMsg);
371 if (ret != -1) {
372 SHARING_LOGI("agent set volume success agentId: %{public}u.", agentId);
373 } else {
374 SHARING_LOGE("agent set volume failed, agentId: %{public}u.", agentId);
375 }
376
377 return 0;
378 }
379
SetKeyPlay(uint32_t contextId,uint32_t agentId,uint64_t surfaceId,bool keyFrame)380 int32_t Interaction::SetKeyPlay(uint32_t contextId, uint32_t agentId, uint64_t surfaceId, bool keyFrame)
381 {
382 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
383 auto agentMsg = std::make_shared<AgentEventMsg>();
384 agentMsg->type = EventType::EVENT_AGENT_CHANNEL_SETSCENETYPE;
385 agentMsg->toMgr = ModuleType::MODULE_CONTEXT;
386 agentMsg->dstId = contextId;
387 agentMsg->agentId = agentId;
388 agentMsg->surfaceId = surfaceId;
389 agentMsg->sceneType = keyFrame ? SceneType::BACKGROUND : SceneType::FOREGROUND;
390
391 int32_t ret = NotifyEvent(agentMsg);
392 if (ret != -1) {
393 SHARING_LOGI("agent set key play success agentId: %{public}u.", agentId);
394 } else {
395 SHARING_LOGE("agent set key play failed, agentId: %{public}u.", agentId);
396 }
397
398 return ret;
399 }
400
SetKeyRedirect(uint32_t contextId,uint32_t agentId,uint64_t surfaceId,bool keyRedirect)401 int32_t Interaction::SetKeyRedirect(uint32_t contextId, uint32_t agentId, uint64_t surfaceId, bool keyRedirect)
402 {
403 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
404 auto agentMsg = std::make_shared<AgentEventMsg>();
405 agentMsg->type = EventType::EVENT_AGENT_CHANNEL_SETKEYREDIRECT;
406 agentMsg->toMgr = ModuleType::MODULE_CONTEXT;
407 agentMsg->dstId = contextId;
408 agentMsg->agentId = agentId;
409 agentMsg->surfaceId = surfaceId;
410 agentMsg->keyRedirect = keyRedirect;
411
412 int32_t ret = NotifyEvent(agentMsg);
413 if (ret != -1) {
414 SHARING_LOGI("agent set key play success agentId: %{public}u.", agentId);
415 } else {
416 SHARING_LOGE("agent set key play failed, agentId: %{public}u.", agentId);
417 }
418
419 return ret;
420 }
421
AppendSurface(uint32_t contextId,uint32_t agentId,sptr<Surface> surface,SceneType sceneType)422 int32_t Interaction::AppendSurface(uint32_t contextId, uint32_t agentId, sptr<Surface> surface, SceneType sceneType)
423 {
424 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
425 RETURN_INVALID_IF_NULL(surface);
426 auto agentMsg = std::make_shared<AgentEventMsg>();
427 agentMsg->type = EventType::EVENT_AGENT_CHANNEL_APPENDSURFACE;
428 agentMsg->toMgr = ModuleType::MODULE_CONTEXT;
429 agentMsg->dstId = contextId;
430 agentMsg->agentId = agentId;
431 agentMsg->surface = surface;
432 agentMsg->sceneType = sceneType;
433 agentMsg->requestId = GetRequestId();
434 int32_t ret = NotifyEvent(agentMsg);
435 if (ret != -1) {
436 SHARING_LOGI("agent set surface success agentId: %{public}u.", agentId);
437 } else {
438 SHARING_LOGE("agent set surface failed, agentId: %{public}u.", agentId);
439 }
440
441 return ret;
442 }
443
RemoveSurface(uint32_t contextId,uint32_t agentId,uint64_t surfaceId)444 int32_t Interaction::RemoveSurface(uint32_t contextId, uint32_t agentId, uint64_t surfaceId)
445 {
446 SHARING_LOGD("contextId: %{public}u, agentId: %{public}u.", contextId, agentId);
447 auto agentMsg = std::make_shared<AgentEventMsg>();
448 agentMsg->type = EventType::EVENT_AGENT_CHANNEL_REMOVESURFACE;
449 agentMsg->toMgr = ModuleType::MODULE_CONTEXT;
450 agentMsg->dstId = contextId;
451 agentMsg->agentId = agentId;
452 agentMsg->surfaceId = surfaceId;
453 agentMsg->requestId = GetRequestId();
454 int32_t ret = NotifyEvent(agentMsg);
455 if (ret != -1) {
456 SHARING_LOGI("agent del surface success agentId: %{public}u.", agentId);
457 } else {
458 SHARING_LOGE("agent del surface failed, agentId: %{public}u.", agentId);
459 }
460
461 return ret;
462 }
463
DestroyWindow(int32_t windowId)464 int32_t Interaction::DestroyWindow(int32_t windowId)
465 {
466 SHARING_LOGD("trace.");
467 return 0;
468 }
469
CreateWindow(int32_t & windowId,WindowProperty & windowProperty)470 int32_t Interaction::CreateWindow(int32_t &windowId, WindowProperty &windowProperty)
471 {
472 SHARING_LOGD("trace.");
473 return 0;
474 }
475
Hide(int32_t windowId)476 int32_t Interaction::Hide(int32_t windowId)
477 {
478 SHARING_LOGD("trace.");
479 return 0;
480 }
481
Show(int32_t windowId)482 int32_t Interaction::Show(int32_t windowId)
483 {
484 SHARING_LOGD("trace.");
485 return 0;
486 }
487
SetFullScreen(int32_t windowId,bool isFull)488 int32_t Interaction::SetFullScreen(int32_t windowId, bool isFull)
489 {
490 SHARING_LOGD("trace.");
491 return 0;
492 }
493
MoveTo(int32_t windowId,int32_t x,int32_t y)494 int32_t Interaction::MoveTo(int32_t windowId, int32_t x, int32_t y)
495 {
496 SHARING_LOGD("trace.");
497 return 0;
498 }
499
GetSurface(int32_t windowId,sptr<Surface> & surface)500 int32_t Interaction::GetSurface(int32_t windowId, sptr<Surface> &surface)
501 {
502 SHARING_LOGD("trace.");
503 return 0;
504 }
505
ReSize(int32_t windowId,int32_t width,int32_t height)506 int32_t Interaction::ReSize(int32_t windowId, int32_t width, int32_t height)
507 {
508 SHARING_LOGD("trace.");
509 return 0;
510 }
511
512 } // namespace Sharing
513 } // namespace OHOS