1 /*
2 * Copyright (c) 2024-2025 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 #include "ability_connection_session.h"
16
17 #include <charconv>
18 #include <chrono>
19 #include <map>
20 #include <unistd.h>
21 #include <sys/prctl.h>
22 #include <sstream>
23 #include <iomanip>
24 #include <future>
25 #include <vector>
26
27 #include "ability_connection_manager.h"
28 #include "dtbcollabmgr_log.h"
29 #include "dtbschedmgr_log.h"
30 #include "distributed_client.h"
31 #include "ipc_skeleton.h"
32 #include "message_data_header.h"
33 #include "openssl/sha.h"
34 #include "tokenid_kit.h"
35
36 namespace OHOS {
37 namespace DistributedCollab {
38 namespace {
39 const std::string TAG = "AbilityConnectionSession";
40 const std::string CONNECT_SESSION_TIMEOUT_END_TASK = "connect_session_timeout_end_task";
41 const std::u16string DMS_PROXY_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
42 const std::string EVENT_CONNECT = "connect";
43 const std::string EVENT_DISCONNECT = "disconnect";
44 const std::string EVENT_RECEIVE_MESSAGE = "receiveMessage";
45 const std::string EVENT_RECEIVE_DATA = "receiveData";
46 const std::string EVENT_RECEIVE_IMAGE = "receiveImage";
47 const std::string EVENT_COLLABORATE = "collaborateEvent";
48 constexpr int32_t DSCHED_COLLAB_PROTOCOL_VERSION = 1;
49 static constexpr uint16_t PROTOCOL_VERSION = 1;
50 constexpr int32_t CHANNEL_NAME_LENGTH = 48;
51 constexpr int32_t VIDEO_BIT_RATE = 80000;
52 constexpr int32_t VIDEO_FRAME_RATE = 30;
53 constexpr int32_t DEFAULT_APP_UID = 0;
54 constexpr int32_t DEFAULT_APP_PID = 0;
55 constexpr int32_t DEFAULT_INSTANCE_ID = 0;
56 constexpr int32_t HEX_WIDTH = 2;
57 constexpr char FILL_CHAR = '0';
58 constexpr int32_t WAIT_FOR_CONNECT = 11;
59 }
60
AbilityConnectionSession(int32_t sessionId,std::string serverSocketName,AbilityConnectionSessionInfo sessionInfo,ConnectOption opt)61 AbilityConnectionSession::AbilityConnectionSession(int32_t sessionId, std::string serverSocketName,
62 AbilityConnectionSessionInfo sessionInfo, ConnectOption opt)
63 {
64 sessionId_ = sessionId;
65 localSocketName_ = serverSocketName;
66 sessionInfo_ = sessionInfo;
67 connectOption_ = opt;
68 version_ = DSCHED_COLLAB_PROTOCOL_VERSION;
69 InitMessageHandlerMap();
70 }
71
~AbilityConnectionSession()72 AbilityConnectionSession::~AbilityConnectionSession()
73 {
74 }
75
InitMessageHandlerMap()76 void AbilityConnectionSession::InitMessageHandlerMap()
77 {
78 messageHandlerMap_[static_cast<uint32_t>(MessageType::NORMAL)] =
79 [this](const std::string& msg) { ExeuteMessageEventCallback(msg); };
80 messageHandlerMap_[static_cast<uint32_t>(MessageType::WIFI_OPEN)] =
81 [this](const std::string&) { ConnectStreamChannel(); };
82 messageHandlerMap_[static_cast<uint32_t>(MessageType::UPDATE_RECV_ENGINE_CHANNEL)] =
83 [this](const std::string&) { UpdateRecvEngineTransChannel(); };
84 messageHandlerMap_[static_cast<uint32_t>(MessageType::UPDATE_SENDER_ENGINE_CHANNEL)] =
85 [this](const std::string&) { UpdateSenderEngineTransChannel(); };
86 messageHandlerMap_[static_cast<uint32_t>(MessageType::CONNECT_FILE_CHANNEL)] =
87 [this](const std::string& msg) { ConnectFileChannel(msg); };
88 messageHandlerMap_[static_cast<uint32_t>(MessageType::FILE_CHANNEL_CONNECT_SUCCESS)] =
89 [this](const std::string&) { NotifyAppConnectResult(true); };
90 messageHandlerMap_[static_cast<uint32_t>(MessageType::FILE_CHANNEL_CONNECT_FAILED)] =
91 [this](const std::string&) { NotifyAppConnectResult(false); };
92 messageHandlerMap_[static_cast<uint32_t>(MessageType::SESSION_CONNECT_SUCCESS)] =
93 [this](const std::string&) { HandleSessionConnect(); };
94 messageHandlerMap_[static_cast<uint32_t>(MessageType::RECEIVE_STREAM_START)] =
95 [this](const std::string&) { UpdateRecvEngineStatus(); };
96 }
97
Init()98 void AbilityConnectionSession::Init()
99 {
100 HILOGI("Init AbilityConnectionSession start");
101 if (eventHandler_ != nullptr) {
102 HILOGI("AbilityConnectionSession already inited, end.");
103 return;
104 }
105 eventThread_ = std::thread(&AbilityConnectionSession::StartEvent, this);
106 std::unique_lock<std::mutex> lock(eventMutex_);
107 eventCon_.wait(lock, [this] {
108 return eventHandler_ != nullptr;
109 });
110 HILOGI("Init AbilityConnectionSession end");
111 }
112
StartEvent()113 void AbilityConnectionSession::StartEvent()
114 {
115 HILOGI("StartEvent start");
116 std::string name = localSocketName_ + std::to_string(sessionId_);
117 prctl(PR_SET_NAME, name.c_str());
118 auto runner = AppExecFwk::EventRunner::Create(false);
119 {
120 std::lock_guard<std::mutex> lock(eventMutex_);
121 eventHandler_ = std::make_shared<OHOS::AppExecFwk::EventHandler>(runner);
122 }
123 eventCon_.notify_one();
124 runner->Run();
125 HILOGI("StartEvent end");
126 }
127
UnInit()128 void AbilityConnectionSession::UnInit()
129 {
130 HILOGI("UnInit start");
131 if (eventHandler_ != nullptr) {
132 eventHandler_->GetEventRunner()->Stop();
133 if (eventThread_.joinable()) {
134 eventThread_.join();
135 }
136 eventHandler_ = nullptr;
137 } else {
138 HILOGE("eventHandler_ is nullptr");
139 }
140 HILOGI("UnInit end");
141 }
142
Release()143 void AbilityConnectionSession::Release()
144 {
145 HILOGI("called.");
146 {
147 std::unique_lock<std::shared_mutex> sessionStatusWriteLock(sessionMutex_);
148 if (sessionStatus_ == SessionStatus::UNCONNECTED) {
149 HILOGI("The session resource has been released.");
150 return;
151 }
152 sessionStatus_ = SessionStatus::UNCONNECTED;
153 }
154 AbilityConnectionManager::GetInstance().DeleteConnectSession(sessionInfo_, sessionId_);
155 DestroyStream();
156
157 std::unique_lock<std::shared_mutex> channelLock(transChannelMutex_);
158 for (auto iter = transChannels_.begin(); iter != transChannels_.end(); iter++) {
159 ChannelManager::GetInstance().DeleteChannel(iter->second.channelId);
160 }
161 transChannels_.clear();
162 }
163
GetPeerInfo()164 PeerInfo AbilityConnectionSession::GetPeerInfo()
165 {
166 return sessionInfo_.peerInfo_;
167 }
168
GetLocalInfo()169 PeerInfo AbilityConnectionSession::GetLocalInfo()
170 {
171 return sessionInfo_.localInfo_;
172 }
173
GetServerToken()174 std::string AbilityConnectionSession::GetServerToken()
175 {
176 return dmsServerToken_;
177 }
178
HandlePeerVersion(int32_t version)179 int32_t AbilityConnectionSession::HandlePeerVersion(int32_t version)
180 {
181 HILOGI("called.");
182 DistributedClient dmsClient;
183 int32_t ret = dmsClient.CollabMission(sessionId_,
184 localSocketName_, sessionInfo_,
185 connectOption_, dmsServerToken_);
186 if (ret != ERR_OK) {
187 HILOGE("collab mission start failed.");
188 ConnectResult connectResult(false, ConnectErrorCode::SYSTEM_INTERNAL_ERROR, "");
189 ExeuteConnectCallback(connectResult);
190 }
191 return ret;
192 }
193
Connect(ConnectCallback & callback)194 int32_t AbilityConnectionSession::Connect(ConnectCallback& callback)
195 {
196 HILOGI("called.");
197 connectCallback_ = callback;
198 if (CheckConnectedSession()) {
199 HILOGE("connected session exists.");
200 return CONNECTED_SESSION_EXISTS;
201 }
202 if (!CheckWifiStatus()) {
203 HILOGI("Wi-Fi is not enabled.");
204 ConnectResult connectResult(false, ConnectErrorCode::LOCAL_WIFI_NOT_OPEN, "");
205 ExeuteConnectCallback(connectResult);
206 return LOCAL_WIFI_NOT_OPEN;
207 }
208
209 {
210 std::unique_lock<std::shared_mutex> sessionStatusWriteLock(sessionMutex_);
211 if (sessionStatus_ != SessionStatus::UNCONNECTED) {
212 HILOGE("session has start to connect, sessionStatus is %{public}d", sessionStatus_);
213 return INVALID_PARAMETERS_ERR;
214 }
215 sessionStatus_ = SessionStatus::CONNECTING;
216 }
217 direction_ = CollabrateDirection::COLLABRATE_SOURCE;
218 dmsServerToken_ = CreateDmsServerToken();
219 DistributedClient dmsClient;
220 int32_t ret = dmsClient.GetPeerVersion(sessionId_, sessionInfo_.peerInfo_.deviceId, dmsServerToken_);
221 if (ret != ERR_OK) {
222 HILOGE("collab mission start failed.");
223 ConnectResult connectResult(false, ConnectErrorCode::SYSTEM_INTERNAL_ERROR, "");
224 ExeuteConnectCallback(connectResult);
225 }
226 return ret;
227 }
228
CheckConnectedSession()229 bool AbilityConnectionSession::CheckConnectedSession()
230 {
231 if (IsConnected()) {
232 HILOGE("session %{public}d connected", sessionId_);
233 ConnectResult connectResult(false, ConnectErrorCode::CONNECTED_SESSION_EXISTS, "");
234 ExeuteConnectCallback(connectResult);
235 return true;
236 }
237
238 int32_t ret = AbilityConnectionManager::GetInstance().UpdateClientSession(sessionInfo_, sessionId_);
239 if (ret != ERR_OK) {
240 HILOGE("connected session exists.");
241 ConnectResult connectResult(false, ConnectErrorCode::CONNECTED_SESSION_EXISTS, "");
242 ExeuteConnectCallback(connectResult);
243 return true;
244 }
245 return false;
246 }
247
CheckWifiStatus()248 bool AbilityConnectionSession::CheckWifiStatus()
249 {
250 uint64_t tokenId = OHOS::IPCSkeleton::GetSelfTokenID();
251 if (OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(tokenId)) {
252 HILOGI("The current application is a system app.");
253 return true;
254 }
255
256 DistributedClient dmsClient;
257 return dmsClient.GetWifiStatus();
258 }
259
CreateDmsServerToken()260 std::string AbilityConnectionSession::CreateDmsServerToken()
261 {
262 auto pid = getprocpid();
263 auto now = std::chrono::system_clock::now();
264 auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
265 std::string input = std::to_string(pid) + std::to_string(sessionId_) + std::to_string(timestamp);
266 unsigned char hash[SHA256_DIGEST_LENGTH];
267 SHA256((const unsigned char*)input.c_str(), input.length(), hash);
268 std::stringstream hashStr;
269 for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
270 hashStr << std::hex << std::setw(HEX_WIDTH) << std::setfill(FILL_CHAR) << (int)hash[i];
271 }
272 return hashStr.str().substr(0, CHANNEL_NAME_LENGTH);
273 }
274
Disconnect()275 int32_t AbilityConnectionSession::Disconnect()
276 {
277 HILOGI("called.");
278 Release();
279 DistributedClient dmsClient;
280 int32_t ret = dmsClient.NotifyCloseCollabSession(dmsServerToken_);
281 HILOGI("Notify Server DisConnect result is %{public}d", ret);
282 return ERR_OK;
283 }
284
AcceptConnect(const std::string & token)285 int32_t AbilityConnectionSession::AcceptConnect(const std::string& token)
286 {
287 HILOGI("called.");
288 DistributedClient dmsClient;
289 if (!CheckWifiStatus()) {
290 HILOGI("Wi-Fi is not enabled.");
291 dmsClient.NotifyPrepareResult(token, PEER_WIFI_NOT_OPEN, sessionId_, localSocketName_);
292 return PEER_WIFI_NOT_OPEN;
293 }
294 {
295 std::unique_lock<std::shared_mutex> sessionStatusWriteLock(sessionMutex_);
296 if (sessionStatus_ != SessionStatus::UNCONNECTED) {
297 HILOGE("session has start to connect, sessionStatus is %{public}d", sessionStatus_);
298 return INVALID_PARAMETERS_ERR;
299 }
300 sessionStatus_ = SessionStatus::CONNECTING;
301 }
302 dmsServerToken_ = token;
303 int32_t ret = AbilityConnectionManager::GetInstance().UpdateServerSession(sessionInfo_, sessionId_);
304 if (ret != ERR_OK) {
305 dmsClient.NotifyPrepareResult(token, ret, sessionId_, localSocketName_);
306 Release();
307 return INVALID_PARAMETERS_ERR;
308 }
309
310 direction_ = CollabrateDirection::COLLABRATE_SINK;
311 ret = InitChannels();
312 if (ret != ERR_OK) {
313 HILOGE("init sink client failed!");
314 dmsClient.NotifyPrepareResult(token, ret, sessionId_, localSocketName_);
315 Release();
316 return ret;
317 }
318 ret = dmsClient.NotifyPrepareResult(token, ERR_OK, sessionId_, localSocketName_);
319 if (ret != ERR_OK) {
320 HILOGE("notify prepare result failed!");
321 Release();
322 }
323 return ERR_OK;
324 }
325
HandleCollabResult(int32_t result,const std::string & peerSocketName,const std::string & dmsServerToken,const std::string & reason)326 int32_t AbilityConnectionSession::HandleCollabResult(int32_t result, const std::string& peerSocketName,
327 const std::string& dmsServerToken, const std::string& reason)
328 {
329 HILOGI("called.");
330 dmsServerToken_ = dmsServerToken;
331 peerSocketName_ = peerSocketName;
332 if (result != ERR_OK) {
333 HILOGE("collab result is failed, ret = %{public}d, reason = %{public}s", result, reason.c_str());
334 NotifyAppConnectResult(false, ConvertToConnectErrorCode(result), reason);
335 return INVALID_PARAMETERS_ERR;
336 }
337
338 if (InitChannels() != ERR_OK || ConnectChannels() != ERR_OK) {
339 NotifyAppConnectResult(false);
340 return INVALID_PARAMETERS_ERR;
341 }
342
343 if (connectOption_.needReceiveFile) {
344 return RequestReceiveFileChannelConnection();
345 }
346
347 NotifyPeerSessionConnected();
348 NotifyAppConnectResult(true);
349 return ERR_OK;
350 }
351
ConvertToConnectErrorCode(int32_t collabResult)352 ConnectErrorCode AbilityConnectionSession::ConvertToConnectErrorCode(int32_t collabResult)
353 {
354 HILOGI("Collaboration failed code is %{public}d.", collabResult);
355 switch (collabResult) {
356 case CONNECTED_SESSION_EXISTS:
357 return ConnectErrorCode::CONNECTED_SESSION_EXISTS;
358 case SAME_SESSION_IS_CONNECTING:
359 return ConnectErrorCode::CONNECTED_SESSION_EXISTS;
360 case PEER_WIFI_NOT_OPEN:
361 return ConnectErrorCode::PEER_WIFI_NOT_OPEN;
362 case DistributedSchedule::COLLAB_ABILITY_REJECT_ERR:
363 return ConnectErrorCode::PEER_APP_REJECTED;
364 case PEER_ABILITY_NO_ONCOLLABORATE:
365 return ConnectErrorCode::PEER_ABILITY_NO_ONCOLLABORATE;
366 default:
367 return ConnectErrorCode::SYSTEM_INTERNAL_ERROR;
368 }
369 }
370
RequestReceiveFileChannelConnection()371 int32_t AbilityConnectionSession::RequestReceiveFileChannelConnection()
372 {
373 HILOGI("notify the peer end bind file.");
374 int32_t ret = SendMessage(localSocketName_, MessageType::CONNECT_FILE_CHANNEL);
375 if (ret != ERR_OK) {
376 HILOGE("Failed to notify the file channel connection, ret = %{public}d", ret);
377 NotifyAppConnectResult(false);
378 }
379 return ret;
380 }
381
NotifyPeerSessionConnected()382 void AbilityConnectionSession::NotifyPeerSessionConnected()
383 {
384 if (!connectOption_.HasFileTransfer()) {
385 HILOGI("No notification required.");
386 return;
387 }
388
389 HILOGI("notify the peer end bind file.");
390 int32_t ret = SendMessage("SESSION_CONNECT_SUCCESS", MessageType::SESSION_CONNECT_SUCCESS);
391 if (ret != ERR_OK) {
392 HILOGE("Failed to notify the session connection result, ret = %{public}d", ret);
393 }
394 }
395
NotifyAppConnectResult(bool isConnected,const ConnectErrorCode errorCode,const std::string & reason)396 void AbilityConnectionSession::NotifyAppConnectResult(bool isConnected,
397 const ConnectErrorCode errorCode, const std::string& reason)
398 {
399 ConnectResult connectResult(isConnected, errorCode, reason);
400 if (isConnected) {
401 std::unique_lock<std::shared_mutex> sessionStatusWriteLock(sessionMutex_);
402 sessionStatus_ = SessionStatus::CONNECTED;
403 connectResult.sessionId = sessionId_;
404 } else {
405 Release();
406 DistributedClient dmsClient;
407 dmsClient.NotifyCloseCollabSession(dmsServerToken_);
408 }
409 ExeuteConnectCallback(connectResult);
410 }
411
HandleDisconnect()412 int32_t AbilityConnectionSession::HandleDisconnect()
413 {
414 HILOGI("called.");
415 {
416 std::shared_lock<std::shared_mutex> sessionStatusReadLock(sessionMutex_);
417 if (sessionStatus_ == SessionStatus::UNCONNECTED) {
418 HILOGI("already disconnect");
419 return ERR_OK;
420 }
421 }
422 Release();
423 std::shared_ptr<IAbilityConnectionSessionListener> listener;
424 {
425 std::shared_lock<std::shared_mutex> lock(sessionListenerMutex_);
426 listener = sessionListener_;
427 }
428 if (listener) {
429 HILOGI("handler sessionListener");
430 listener->OnDisConnect(sessionId_);
431 } else {
432 EventCallbackInfo callbackInfo;
433 callbackInfo.sessionId = sessionId_;
434 callbackInfo.reason = DisconnectReason::PEER_APP_EXIT;
435 ExeuteEventCallback(EVENT_DISCONNECT, callbackInfo);
436 }
437 return ERR_OK;
438 }
439
SendMessage(const std::string & msg,const MessageType & messageType)440 int32_t AbilityConnectionSession::SendMessage(const std::string& msg, const MessageType& messageType)
441 {
442 HILOGI("called.");
443 auto sendData = std::make_shared<AVTransDataBuffer>(msg.length() + 1);
444 int32_t ret = memcpy_s(sendData->Data(), sendData->Capacity(), msg.c_str(), msg.length());
445 if (ret != ERR_OK) {
446 HILOGE("memory copy failed, ret %{public}d", ret);
447 return ret;
448 }
449
450 uint32_t totalLen = sendData->Size() + MessageDataHeader::HEADER_LEN;
451
452 MessageDataHeader headerPara(PROTOCOL_VERSION, static_cast<uint32_t>(messageType), totalLen);
453 auto headerBuffer = headerPara.Serialize();
454 auto sendBuffer = std::make_shared<AVTransDataBuffer>(totalLen);
455 uint8_t* header = sendBuffer->Data();
456
457 if (memcpy_s(header, sendBuffer->Size(), headerBuffer->Data(), MessageDataHeader::HEADER_LEN) != ERR_OK) {
458 HILOGE("Write header failed");
459 return WRITE_SESSION_HEADER_FAILED;
460 }
461
462 ret = memcpy_s(header + MessageDataHeader::HEADER_LEN, sendBuffer->Size() - MessageDataHeader::HEADER_LEN,
463 sendData->Data(), sendData->Size());
464 if (ret != ERR_OK) {
465 HILOGE("Write data failed");
466 return WRITE_SEND_DATA_BUFFER_FAILED;
467 }
468
469 TransChannelInfo transChannelInfo;
470 ret = GetTransChannelInfo(TransChannelType::MESSAGE, transChannelInfo);
471 if (ret != ERR_OK) {
472 HILOGE("message channel not exit");
473 return ret;
474 }
475
476 int32_t channelId = transChannelInfo.channelId;
477 ret = ChannelManager::GetInstance().SendMessage(channelId, sendBuffer);
478 if (ret != ERR_OK) {
479 HILOGE("send message failed, channelId is %{public}d", channelId);
480 return ret;
481 }
482 return ERR_OK;
483 }
484
SendData(const std::shared_ptr<AVTransDataBuffer> & buffer)485 int32_t AbilityConnectionSession::SendData(const std::shared_ptr<AVTransDataBuffer>& buffer)
486 {
487 HILOGI("called.");
488 TransChannelInfo transChannelInfo;
489 int32_t ret = GetTransChannelInfo(TransChannelType::DATA, transChannelInfo);
490 if (ret != ERR_OK) {
491 HILOGE("data channel not exit");
492 return ret;
493 }
494
495 int32_t channelId = transChannelInfo.channelId;
496 ret = ChannelManager::GetInstance().SendBytes(channelId, buffer);
497 if (ret != ERR_OK) {
498 HILOGE("send bytes failed, channelId is %{public}d", channelId);
499 return ret;
500 }
501 return ERR_OK;
502 }
503
SendImage(const std::shared_ptr<Media::PixelMap> & image,int32_t imageQuality)504 int32_t AbilityConnectionSession::SendImage(const std::shared_ptr<Media::PixelMap>& image, int32_t imageQuality)
505 {
506 HILOGI("called.");
507 if (senderEngine_ == nullptr) {
508 HILOGE("senderEngine_ is nullptr.");
509 return INVALID_PARAMETERS_ERR;
510 }
511 int32_t ret = senderEngine_->SendPixelMap(image, imageQuality);
512 if (ret != ERR_OK) {
513 HILOGE("Send image failed, ret is %{public}d.", ret);
514 return ret;
515 }
516 return ERR_OK;
517 }
518
SendFile(const std::vector<std::string> & sFiles,const std::vector<std::string> & dFiles)519 int32_t AbilityConnectionSession::SendFile(const std::vector<std::string>& sFiles,
520 const std::vector<std::string>& dFiles)
521 {
522 HILOGI("called.");
523 TransChannelInfo transChannelInfo;
524 int32_t ret = GetTransChannelInfo(TransChannelType::SEND_FILE, transChannelInfo);
525 if (ret != ERR_OK) {
526 HILOGE("data channel not exit");
527 return ret;
528 }
529
530 int32_t channelId = transChannelInfo.channelId;
531 ret = ChannelManager::GetInstance().SendFile(channelId, sFiles, dFiles);
532 if (ret != ERR_OK) {
533 HILOGE("send bytes failed, channelId is %{public}d", channelId);
534 return ret;
535 }
536 return ERR_OK;
537 }
538
CreateStream(int32_t streamId,const StreamParams & param)539 int32_t AbilityConnectionSession::CreateStream(int32_t streamId, const StreamParams& param)
540 {
541 HILOGI("called. StreamParams role is %{public}d", static_cast<int32_t>(param.role));
542 switch (param.role) {
543 case StreamRole::SOURCE:
544 streamId_ = streamId;
545 return InitSenderEngine();
546 case StreamRole::SINK:
547 streamId_ = streamId;
548 return InitRecvEngine();
549 default:
550 HILOGE("Unrecognized streamRole.");
551 return INVALID_PARAMETERS_ERR;
552 }
553 }
554
InitSenderEngine()555 int32_t AbilityConnectionSession::InitSenderEngine()
556 {
557 if (!connectOption_.needSendStream) {
558 HILOGE("The stream sending option is not configured.");
559 return INVALID_PARAMETERS_ERR;
560 }
561 std::unique_lock<std::shared_mutex> listenerWriteLock(engineMutex_);
562 if (senderEngine_ != nullptr) {
563 HILOGE("The stream sender engine has init.");
564 return ONLY_SUPPORT_ONE_STREAM;
565 }
566 senderEngine_ = std::make_shared<AVSenderEngine>(DEFAULT_APP_UID, DEFAULT_APP_PID,
567 sessionInfo_.localInfo_.bundleName, DEFAULT_INSTANCE_ID);
568 senderEngine_->Init();
569 return ERR_OK;
570 }
571
InitRecvEngine()572 int32_t AbilityConnectionSession::InitRecvEngine()
573 {
574 if (!connectOption_.needReceiveStream) {
575 HILOGE("The stream receive option is not configured.");
576 return INVALID_PARAMETERS_ERR;
577 }
578 std::unique_lock<std::shared_mutex> listenerWriteLock(engineMutex_);
579 if (recvEngine_ != nullptr) {
580 HILOGE("The stream receive has init.");
581 return ONLY_SUPPORT_ONE_STREAM;
582 }
583
584 recvEngine_ = std::make_shared<AVReceiverEngine>();
585 recvEngine_->Init();
586 return ERR_OK;
587 }
588
DestroyStream()589 int32_t AbilityConnectionSession::DestroyStream()
590 {
591 HILOGI("called.");
592 senderEngine_ = nullptr;
593 recvEngine_ = nullptr;
594 recvEngineState_ = EngineState::EMPTY;
595 TransChannelInfo info;
596 std::shared_lock<std::shared_mutex> channelReadLock(transChannelMutex_);
597 auto item = transChannels_.find(TransChannelType::STREAM);
598 if (item != transChannels_.end() && item->second.isConnected) {
599 info = item->second;
600 }
601 ChannelManager::GetInstance().ClearSendTask(info.channelId);
602 HILOGI("stream bytes channel clear task");
603 item = transChannels_.find(TransChannelType::STREAM_BYTES);
604 if (item != transChannels_.end() && item->second.isConnected) {
605 info = item->second;
606 }
607 ChannelManager::GetInstance().ClearSendTask(info.channelId);
608 HILOGI("stream bytes channel clear task");
609 return ERR_OK;
610 }
611
612 template <typename T>
ConfigEngineParam(std::shared_ptr<T> & engine,const SurfaceParams & param)613 int32_t AbilityConnectionSession::ConfigEngineParam(std::shared_ptr<T> &engine, const SurfaceParams& param)
614 {
615 engine->SetVideoSource(static_cast<VideoSourceType>(param.format));
616
617 int32_t ret = ERR_OK;
618 ret = engine->Configure(VidEnc(VideoCodecFormat::H264));
619 if (ret != ERR_OK) {
620 HILOGE("configure videnc failed");
621 return ret;
622 }
623 ret = engine->Configure(VidRectangle(param.width, param.height));
624 if (ret != ERR_OK) {
625 HILOGE("configure VidRectangle failed");
626 return ret;
627 }
628 ret = engine->Configure(VidBitRate(VIDEO_BIT_RATE));
629 if (ret != ERR_OK) {
630 HILOGE("configure VidBitRate failed");
631 return ret;
632 }
633 ret = engine->Configure(VidFrameRate(VIDEO_FRAME_RATE));
634 if (ret != ERR_OK) {
635 HILOGE("configure VidFrameRate failed");
636 return ret;
637 }
638 ret = engine->Configure(VidIsHdr(false));
639 if (ret != ERR_OK) {
640 HILOGE("configure VidIsHdr failed");
641 return ret;
642 }
643 ret = engine->Configure(VidEnableTemporalScale(false));
644 if (ret != ERR_OK) {
645 HILOGE("configure VidEnableTemporalScale failed");
646 return ret;
647 }
648 ret = engine->Configure(VidSurfaceParam(ConvertToSurfaceParam(param)));
649 if (ret != ERR_OK) {
650 HILOGE("configure VidSurfaceParam failed");
651 return ret;
652 }
653 return ERR_OK;
654 }
655
GetSurfaceId(const SurfaceParams & param,std::string & surfaceId)656 int32_t AbilityConnectionSession::GetSurfaceId(const SurfaceParams& param, std::string& surfaceId)
657 {
658 HILOGI("called.");
659 if (senderEngine_ == nullptr) {
660 HILOGE("senderEngine_ Uninitialized.");
661 return INVALID_PARAMETERS_ERR;
662 }
663 int32_t ret = ConfigEngineParam(senderEngine_, param);
664 if (ret != ERR_OK) {
665 HILOGE("config senderEngine param failed.");
666 return ret;
667 }
668
669 ret = senderEngine_->Prepare();
670 if (ret != ERR_OK) {
671 HILOGE("error prepare senderEngine_");
672 return ret;
673 }
674
675 TransChannelInfo info;
676 if (GetStreamTransChannel(info) != ERR_OK) {
677 HILOGE("senderEngine_ SetChannelListener failed");
678 return INVALID_PARAMETERS_ERR;
679 }
680
681 HILOGI("SetChannelListener channelId is %{public}d, channelType is %{public}d",
682 info.channelId, static_cast<int32_t>(info.channelType));
683 senderEngine_->SetTransChannel(info.channelId, info.channelType);
684 surfaceId = std::to_string(senderEngine_->GetSurface());
685 return ERR_OK;
686 }
687
SetSurfaceId(const std::string & surfaceId,const SurfaceParams & param)688 int32_t AbilityConnectionSession::SetSurfaceId(const std::string& surfaceId,
689 const SurfaceParams& param)
690 {
691 HILOGI("called.");
692 if (recvEngine_ == nullptr) {
693 HILOGE("recvEngine_ Uninitialized.");
694 return INVALID_PARAMETERS_ERR;
695 }
696 int32_t ret = ConfigEngineParam(recvEngine_, param);
697 if (ret != ERR_OK) {
698 HILOGE("recvEngine_ ConfigEngineParam failed.");
699 return ret;
700 }
701
702 uint64_t value = 0;
703 auto result = std::from_chars(surfaceId.data(), surfaceId.data() + surfaceId.size(), value);
704 if (result.ec != std::errc()) {
705 HILOGE("Get value failed");
706 return INVALID_PARAMETERS_ERR;
707 }
708 ret = recvEngine_->SetVideoSurface(value);
709 if (ret != ERR_OK) {
710 HILOGE("error set video surface!");
711 return ret;
712 }
713
714 ret = recvEngine_->Prepare();
715 if (ret != ERR_OK) {
716 HILOGE("error prepare recvEngine_");
717 return ret;
718 }
719
720 TransChannelInfo info;
721 if (GetStreamTransChannel(info) != ERR_OK) {
722 HILOGE("recvEngine_ SetChannelListener failed");
723 return INVALID_PARAMETERS_ERR;
724 }
725 HILOGE("SetChannelListener channelId is %{public}d, channelType is %{public}d",
726 info.channelId, static_cast<int32_t>(info.channelType));
727 recvEngine_->SetChannelListener(info.channelId);
728 pixelMapListener = std::make_shared<PixelMapListener>(shared_from_this());
729 recvEngine_->SetEngineListener(pixelMapListener);
730 return ERR_OK;
731 }
732
UpdateSurfaceParam(const SurfaceParams & surfaceParam)733 int32_t AbilityConnectionSession::UpdateSurfaceParam(const SurfaceParams& surfaceParam)
734 {
735 SurfaceParam param = ConvertToSurfaceParam(surfaceParam);
736 HILOGI("SurfaceParam rotate is %{public}d, filp is %{public}d.",
737 static_cast<int32_t>(param.rotate), static_cast<int32_t>(param.filp));
738 if (senderEngine_ != nullptr) {
739 HILOGI("Update senderEngine_ SurfaceParam.");
740 senderEngine_->SetSurfaceParam(param);
741 return ERR_OK;
742 }
743
744 if (recvEngine_ != nullptr) {
745 HILOGI("Update recvEngine_ SurfaceParam.");
746 recvEngine_ -> OnRecvSurfaceParam(param);
747 return ERR_OK;
748 }
749
750 HILOGE("senderEngine_ and recvEngine_ is nullptr!");
751 return INVALID_PARAMETERS_ERR;
752 }
753
ConvertToSurfaceParam(const SurfaceParams & param)754 SurfaceParam AbilityConnectionSession::ConvertToSurfaceParam(const SurfaceParams& param)
755 {
756 SurfaceParam surfaveParam;
757 switch (param.rotation) {
758 case SURFACE_ROTATE_NONE:
759 surfaveParam.rotate = SurfaceRotate::ROTATE_NONE;
760 break;
761 case SURFACE_ROTATE_90:
762 surfaveParam.rotate = SurfaceRotate::ROTATE_90;
763 break;
764 case SURFACE_ROTATE_180:
765 surfaveParam.rotate = SurfaceRotate::ROTATE_180;
766 break;
767 case SURFACE_ROTATE_270:
768 surfaveParam.rotate = SurfaceRotate::ROTATE_270;
769 break;
770 default:
771 surfaveParam.rotate = SurfaceRotate::ROTATE_NONE;
772 break;
773 }
774
775 switch (param.flip) {
776 case FlipOptions::HORIZONTAL:
777 surfaveParam.filp = SurfaceFilp::FLIP_H;
778 break;
779 case FlipOptions::VERTICAL:
780 surfaveParam.filp = SurfaceFilp::FLIP_V;
781 break;
782 default:
783 surfaveParam.filp = SurfaceFilp::FLIP_NONE;
784 break;
785 }
786
787 return surfaveParam;
788 }
789
GetStreamTransChannel(TransChannelInfo & info)790 int32_t AbilityConnectionSession::GetStreamTransChannel(TransChannelInfo& info)
791 {
792 std::shared_lock<std::shared_mutex> channelReadLock(transChannelMutex_);
793 auto item = transChannels_.find(TransChannelType::STREAM);
794 if (item != transChannels_.end() && item->second.isConnected) {
795 info = item->second;
796 return ERR_OK;
797 }
798 HILOGW("stream channel unconnected");
799 item = transChannels_.find(TransChannelType::STREAM_BYTES);
800 if (item != transChannels_.end() && item->second.isConnected) {
801 info = item->second;
802 return ERR_OK;
803 }
804 HILOGE("bytes stream channel unconnected");
805 return INVALID_PARAMETERS_ERR;
806 }
807
StartStream(int32_t streamId)808 int32_t AbilityConnectionSession::StartStream(int32_t streamId)
809 {
810 HILOGI("called.");
811 if (connectOption_.needSendStream && senderEngine_ != nullptr) {
812 return StartSenderEngine();
813 }
814
815 if (connectOption_.needReceiveStream && recvEngine_ != nullptr) {
816 return StartRecvEngine();
817 }
818 HILOGE("not config stream option or engine is null.");
819 return INVALID_PARAMETERS_ERR;
820 }
821
StartRecvEngine()822 int32_t AbilityConnectionSession::StartRecvEngine()
823 {
824 HILOGI("recvEngine_ Start.");
825 int32_t ret = recvEngine_->Start();
826 if (ret != ERR_OK) {
827 HILOGE("recvEngine_ start failed.");
828 return ret;
829 }
830 return SendMessage("recvEngineStart", MessageType::RECEIVE_STREAM_START);
831 }
832
UpdateRecvEngineStatus()833 void AbilityConnectionSession::UpdateRecvEngineStatus()
834 {
835 recvEngineState_ = EngineState::START;
836 }
837
StartSenderEngine()838 int32_t AbilityConnectionSession::StartSenderEngine()
839 {
840 HILOGI("senderEngine_ Start. recvEngineState_ is %{public}d", static_cast<int32_t>(recvEngineState_));
841 return senderEngine_->Start();
842 }
843
StopStream(int32_t streamId)844 int32_t AbilityConnectionSession::StopStream(int32_t streamId)
845 {
846 HILOGI("called.");
847 if (connectOption_.needSendStream && senderEngine_ != nullptr) {
848 HILOGI("senderEngine_ Stop.");
849 return senderEngine_->Stop();
850 }
851
852 if (connectOption_.needReceiveStream && recvEngine_ != nullptr) {
853 HILOGI("recvEngine_ Stop.");
854 return recvEngine_->Stop();
855 }
856 return ERR_OK;
857 }
858
RegisterEventCallback(const std::string & eventType,const std::shared_ptr<JsAbilityConnectionSessionListener> & listener)859 int32_t AbilityConnectionSession::RegisterEventCallback(const std::string& eventType,
860 const std::shared_ptr<JsAbilityConnectionSessionListener>& listener)
861 {
862 HILOGI("called.");
863 std::unique_lock<std::shared_mutex> listenerWriteLock(listenerMutex_);
864 listeners_[eventType] = listener;
865 return ERR_OK;
866 }
867
UnregisterEventCallback(const std::string & eventType)868 int32_t AbilityConnectionSession::UnregisterEventCallback(const std::string& eventType)
869 {
870 HILOGI("called.");
871 std::unique_lock<std::shared_mutex> listenerWriteLock(listenerMutex_);
872 listeners_.erase(eventType);
873 return ERR_OK;
874 }
875
RegisterEventCallback(const std::shared_ptr<IAbilityConnectionSessionListener> & listener)876 int32_t AbilityConnectionSession::RegisterEventCallback(
877 const std::shared_ptr<IAbilityConnectionSessionListener>& listener)
878 {
879 if (listener == nullptr) {
880 HILOGE("listener empty");
881 return INVALID_LISTENER;
882 }
883 std::unique_lock<std::shared_mutex> lock(sessionListenerMutex_);
884 sessionListener_ = listener;
885 return ERR_OK;
886 }
887
UnregisterEventCallback()888 int32_t AbilityConnectionSession::UnregisterEventCallback()
889 {
890 std::unique_lock<std::shared_mutex> lock(sessionListenerMutex_);
891 sessionListener_ = nullptr;
892 return ERR_OK;
893 }
894
ExeuteEventCallback(const std::string & eventType,EventCallbackInfo & info)895 int32_t AbilityConnectionSession::ExeuteEventCallback(const std::string& eventType, EventCallbackInfo& info)
896 {
897 return ExeuteEventCallbackTemplate(eventType, info);
898 }
899
ExeuteEventCallback(const std::string & eventType,CollaborateEventInfo & info)900 int32_t AbilityConnectionSession::ExeuteEventCallback(const std::string& eventType, CollaborateEventInfo& info)
901 {
902 return ExeuteEventCallbackTemplate(eventType, info);
903 }
904
905 template <typename T>
ExeuteEventCallbackTemplate(const std::string & eventType,T & info)906 int32_t AbilityConnectionSession::ExeuteEventCallbackTemplate(const std::string& eventType, T& info)
907 {
908 HILOGI("called, eventType is %{public}s", eventType.c_str());
909 std::shared_lock<std::shared_mutex> listenerReadLock(listenerMutex_);
910 if (listeners_.empty()) {
911 HILOGE("listeners_ is empty");
912 return INVALID_PARAMETERS_ERR;
913 }
914
915 auto item = listeners_.find(eventType);
916 if (item == listeners_.end()) {
917 HILOGE("The event callback is not registered. event is %{public}s", eventType.c_str());
918 return INVALID_PARAMETERS_ERR;
919 }
920
921 if constexpr (std::is_same_v<T, EventCallbackInfo>) {
922 info.eventType = eventType;
923 }
924 auto eventCallback = item->second;
925 if (eventCallback == nullptr) {
926 HILOGE("eventCallback is nullptr");
927 return INVALID_PARAMETERS_ERR;
928 }
929 eventCallback->CallJsMethod(info);
930 return ERR_OK;
931 }
932
InitChannels()933 int32_t AbilityConnectionSession::InitChannels()
934 {
935 HILOGI("called.");
936 channelName_ = GetChannelName(sessionInfo_);
937 channelListener_ = std::make_shared<CollabChannelListener>(shared_from_this());
938 bool isClientChannel = direction_ == CollabrateDirection::COLLABRATE_SOURCE;
939 int32_t ret = CreateChannel(channelName_, ChannelDataType::MESSAGE, TransChannelType::MESSAGE, isClientChannel);
940 if (ret != ERR_OK) {
941 HILOGE("create message channel failed!");
942 return FAILED_TO_CREATE_MESSAGE_CHANNEL;
943 }
944
945 if (connectOption_.needSendData &&
946 CreateChannel(channelName_, ChannelDataType::BYTES, TransChannelType::DATA, isClientChannel) != ERR_OK) {
947 HILOGE("create data channel failed!");
948 return FAILED_TO_CREATE_DATA_CHANNEL;
949 }
950
951 if ((connectOption_.needSendStream || connectOption_.needReceiveStream) &&
952 CreateStreamChannel(channelName_, isClientChannel) != ERR_OK) {
953 HILOGE("create stream channel failed!");
954 return FAILED_TO_CREATE_STREAM_CHANNEL;
955 }
956
957 if (connectOption_.needSendFile && isClientChannel &&
958 CreateChannel(channelName_, ChannelDataType::FILE, TransChannelType::SEND_FILE, true) != ERR_OK) {
959 HILOGE("create send file channel failed!");
960 return FAILED_TO_CREATE_SEND_FILE_CHANNEL;
961 }
962
963 if (connectOption_.needReceiveFile &&
964 CreateChannel(channelName_, ChannelDataType::FILE, TransChannelType::RECEIVE_FILE, false) != ERR_OK) {
965 HILOGE("create receive file channel failed!");
966 return FAILED_TO_CREATE_RECEIVE_FILE_CHANNEL;
967 }
968 return ERR_OK;
969 }
970
CreateStreamChannel(const std::string & channelName,bool isClientChannel)971 int32_t AbilityConnectionSession::CreateStreamChannel(const std::string& channelName, bool isClientChannel)
972 {
973 std::string streamChannelName = channelName + "stream";
974 int32_t ret = CreateChannel(streamChannelName, ChannelDataType::BYTES, TransChannelType::STREAM_BYTES,
975 isClientChannel);
976 if (ret != ERR_OK) {
977 HILOGE("init bytes channel failed!");
978 return INVALID_PARAMETERS_ERR;
979 }
980
981 ret = CreateChannel(streamChannelName, ChannelDataType::VIDEO_STREAM, TransChannelType::STREAM, isClientChannel);
982 if (ret != ERR_OK) {
983 HILOGE("init bytes channel failed!");
984 return INVALID_PARAMETERS_ERR;
985 }
986 return ERR_OK;
987 }
988
CreateChannel(const std::string & channelName,const ChannelDataType & dataType,const TransChannelType & channelType,bool isClientChannel)989 int32_t AbilityConnectionSession::CreateChannel(const std::string& channelName, const ChannelDataType& dataType,
990 const TransChannelType& channelType, bool isClientChannel)
991 {
992 HILOGI("called.");
993 ChannelPeerInfo channelPeerInfo = { peerSocketName_, sessionInfo_.peerInfo_.deviceId };
994 ChannelManager &channelManager = ChannelManager::GetInstance();
995 int32_t channelId = isClientChannel ?
996 channelManager.CreateClientChannel(channelName, dataType, channelPeerInfo) :
997 channelManager.CreateServerChannel(channelName, dataType, channelPeerInfo);
998 if (!channelManager.isValidChannelId(channelId)) {
999 HILOGE("CreateChannel failed, channelId is %{public}d", channelId);
1000 return INVALID_PARAMETERS_ERR;
1001 }
1002
1003 if (channelManager.RegisterChannelListener(channelId, channelListener_) != ERR_OK) {
1004 HILOGE("register channel listener failed, channelId is %{public}d", channelId);
1005 return INVALID_PARAMETERS_ERR;
1006 }
1007
1008 std::unique_lock<std::shared_mutex> channelWriteLock(transChannelMutex_);
1009 TransChannelInfo channelInfo = {channelId, dataType, channelType, false};
1010 transChannels_.emplace(channelType, channelInfo);
1011 return ERR_OK;
1012 }
1013
GetChannelName(const AbilityConnectionSessionInfo & sessionInfo)1014 std::string AbilityConnectionSession::GetChannelName(const AbilityConnectionSessionInfo& sessionInfo)
1015 {
1016 PeerInfo localInfo = sessionInfo.localInfo_;
1017 PeerInfo peerInfo = sessionInfo.peerInfo_;
1018 bool isClientChannel = direction_ == CollabrateDirection::COLLABRATE_SOURCE;
1019 std::string input = isClientChannel ?
1020 localInfo.moduleName + localInfo.abilityName + peerInfo.moduleName + peerInfo.abilityName :
1021 peerInfo.moduleName + peerInfo.abilityName + localInfo.moduleName + localInfo.abilityName;
1022
1023 unsigned char hash[SHA256_DIGEST_LENGTH];
1024 SHA256((const unsigned char*)input.c_str(), input.length(), hash);
1025
1026 std::stringstream hashStr;
1027 for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
1028 hashStr << std::hex << std::setw(HEX_WIDTH) << std::setfill(FILL_CHAR) << (int)hash[i];
1029 }
1030
1031 std::string channelName = hashStr.str().substr(0, CHANNEL_NAME_LENGTH);
1032 return channelName;
1033 }
1034
ConnectChannels()1035 int32_t AbilityConnectionSession::ConnectChannels()
1036 {
1037 HILOGI("parallel connect channels");
1038 std::vector<std::future<int32_t>> futures;
1039 // message
1040 futures.emplace_back(std::async(std::launch::async, [this]() -> int32_t {
1041 if (ConnectTransChannel(TransChannelType::MESSAGE) != ERR_OK) {
1042 HILOGE("connect message channel failed.");
1043 return CONNECT_MESSAGE_CHANNEL_FAILED;
1044 }
1045 return ERR_OK;
1046 }));
1047 // data
1048 if (connectOption_.needSendData) {
1049 futures.emplace_back(std::async(std::launch::async, [this]() -> int32_t {
1050 if (ConnectTransChannel(TransChannelType::DATA) != ERR_OK) {
1051 HILOGE("connect data channel failed.");
1052 return CONNECT_DATA_CHANNEL_FAILED;
1053 }
1054 return ERR_OK;
1055 }));
1056 }
1057 // stream
1058 if (connectOption_.needSendStream || connectOption_.needReceiveStream) {
1059 ConnectStreamChannel();
1060 futures.emplace_back(std::async(std::launch::async, [this]() -> int32_t {
1061 if (ConnectTransChannel(TransChannelType::STREAM_BYTES) != ERR_OK) {
1062 HILOGE("connect stream channel failed.");
1063 return CONNECT_STREAM_CHANNEL_FAILED;
1064 }
1065 return ERR_OK;
1066 }));
1067 }
1068 // file
1069 if (connectOption_.needSendFile && ConnectTransChannel(TransChannelType::SEND_FILE) != ERR_OK) {
1070 HILOGE("connect send file channel failed.");
1071 return CONNECT_SEND_FILE_CHANNEL_FAILED;
1072 }
1073 // wait for task
1074 for (auto&& future : futures) {
1075 int32_t result = future.get();
1076 if (result != ERR_OK) {
1077 return result;
1078 }
1079 }
1080 return ERR_OK;
1081 }
1082
ConnectTransChannel(const TransChannelType channelType)1083 int32_t AbilityConnectionSession::ConnectTransChannel(const TransChannelType channelType)
1084 {
1085 TransChannelInfo info;
1086 int32_t ret = GetTransChannelInfo(channelType, info);
1087 if (ret != ERR_OK) {
1088 HILOGE("stream channel not exits!");
1089 return STREAM_CHANNEL_NOT_EXITS;
1090 }
1091
1092 ret = ChannelManager::GetInstance().ConnectChannel(info.channelId);
1093 if (ret != ERR_OK) {
1094 HILOGE("connect channel failed. ret is %{public}d", ret);
1095 return ret;
1096 }
1097 HILOGI("connect channel success, channel type is %{public}d", static_cast<int32_t>(channelType));
1098 UpdateTransChannelStatus(info.channelId, true);
1099 return ERR_OK;
1100 }
1101
ConnectStreamChannel()1102 int32_t AbilityConnectionSession::ConnectStreamChannel()
1103 {
1104 HILOGI("called.");
1105 if (!connectOption_.needSendStream && !connectOption_.needReceiveStream) {
1106 HILOGI("Streaming is not required.");
1107 return ERR_OK;
1108 }
1109
1110 TransChannelInfo info;
1111 int32_t ret = GetTransChannelInfo(TransChannelType::STREAM, info);
1112 if (ret != ERR_OK) {
1113 HILOGE("stream channel not exits!");
1114 return INVALID_PARAMETERS_ERR;
1115 }
1116
1117 if (info.isConnected) {
1118 HILOGE("stream channel has connected.");
1119 return ERR_OK;
1120 }
1121
1122 if (direction_ != CollabrateDirection::COLLABRATE_SOURCE) {
1123 HILOGI("notify source connect stream channel.");
1124 SendMessage("WIFI_OPEN", MessageType::WIFI_OPEN);
1125 return ERR_OK;
1126 }
1127
1128 std::thread task([this, info]() {
1129 DoConnectStreamChannel(info.channelId);
1130 });
1131 task.detach();
1132 return ERR_OK;
1133 }
1134
DoConnectStreamChannel(int32_t channelId)1135 int32_t AbilityConnectionSession::DoConnectStreamChannel(int32_t channelId)
1136 {
1137 HILOGI("called.");
1138 int32_t ret = ChannelManager::GetInstance().ConnectChannel(channelId);
1139 if (ret != ERR_OK) {
1140 HILOGE("stream channel bind failed, ret is %{public}d", ret);
1141 return ret;
1142 }
1143 HILOGI("stream channel bind success");
1144 UpdateTransChannelStatus(channelId, true);
1145
1146 if (recvEngine_ == nullptr) {
1147 HILOGI("notify peer update recvEngine channel.");
1148 SendMessage("updateRecvEngineTransChannel", MessageType::UPDATE_RECV_ENGINE_CHANNEL);
1149 return ERR_OK;
1150 }
1151 UpdateRecvEngineTransChannel();
1152 return ERR_OK;
1153 }
1154
GetTransChannelInfo(const TransChannelType & type,TransChannelInfo & info)1155 int32_t AbilityConnectionSession::GetTransChannelInfo(const TransChannelType& type, TransChannelInfo& info)
1156 {
1157 HILOGI("called.");
1158 std::shared_lock<std::shared_mutex> channelReadLock(transChannelMutex_);
1159 auto item = transChannels_.find(type);
1160 if (item == transChannels_.end()) {
1161 return INVALID_PARAMETERS_ERR;
1162 }
1163 info = item->second;
1164 return ERR_OK;
1165 }
1166
OnChannelConnect(int32_t channelId)1167 void AbilityConnectionSession::OnChannelConnect(int32_t channelId)
1168 {
1169 HILOGI("called. channelId is %{public}d", channelId);
1170 if (!IsVaildChannel(channelId)) {
1171 HILOGE("is vaild channelId");
1172 return;
1173 }
1174
1175 UpdateTransChannelStatus(channelId, true);
1176 if (IsAllChannelConnected() && !connectOption_.HasFileTransfer()) {
1177 HandleSessionConnect();
1178 }
1179 }
1180
HandleSessionConnect()1181 void AbilityConnectionSession::HandleSessionConnect()
1182 {
1183 HILOGI("called.");
1184 std::unique_lock<std::shared_mutex> sessionStatusWriteLock(sessionMutex_);
1185 if (sessionStatus_ == SessionStatus::CONNECTED) {
1186 HILOGI("session has connected.");
1187 return;
1188 }
1189 sessionStatus_ = SessionStatus::CONNECTED;
1190
1191 std::shared_ptr<IAbilityConnectionSessionListener> listener;
1192 {
1193 std::shared_lock<std::shared_mutex> lock(sessionListenerMutex_);
1194 listener = sessionListener_;
1195 }
1196 if (listener) {
1197 HILOGI("handler sessionListener");
1198 listener->OnConnect(sessionId_);
1199 } else {
1200 EventCallbackInfo callbackInfo;
1201 callbackInfo.sessionId = sessionId_;
1202 ExeuteEventCallback(EVENT_CONNECT, callbackInfo);
1203 }
1204 }
1205
UpdateTransChannelStatus(int32_t channelId,bool isConnected)1206 void AbilityConnectionSession::UpdateTransChannelStatus(int32_t channelId, bool isConnected)
1207 {
1208 std::unique_lock<std::shared_mutex> channelWriteLock(transChannelMutex_);
1209 for (auto& iter : transChannels_) {
1210 if (iter.second.channelId == channelId) {
1211 HILOGI("transType is %{public}d.", static_cast<int32_t>(iter.second.transType));
1212 iter.second.isConnected = isConnected;
1213 }
1214 }
1215 }
1216
IsAllChannelConnected()1217 bool AbilityConnectionSession::IsAllChannelConnected()
1218 {
1219 HILOGD("called.");
1220 std::shared_lock<std::shared_mutex> channelReadLock(transChannelMutex_);
1221 for (auto& iter : transChannels_) {
1222 TransChannelInfo info = iter.second;
1223 if (!info.isConnected && info.transType != TransChannelType::STREAM) {
1224 HILOGI("transType is %{public}d.", static_cast<int32_t>(info.transType));
1225 return false;
1226 }
1227 }
1228 HILOGI("AllChannelConnected.");
1229 return true;
1230 }
1231
OnChannelClosed(int32_t channelId,const ShutdownReason & reason)1232 void AbilityConnectionSession::OnChannelClosed(int32_t channelId, const ShutdownReason& reason)
1233 {
1234 HILOGI("called. channelId is %{public}d", channelId);
1235 if (!IsVaildChannel(channelId)) {
1236 HILOGE("is vaild channelId");
1237 return;
1238 }
1239
1240 if (!IsConnected()) {
1241 HILOGE("session is not connected.");
1242 return;
1243 }
1244
1245 HILOGI("notidy app disconnect");
1246 Disconnect();
1247
1248 std::shared_ptr<IAbilityConnectionSessionListener> listener;
1249 {
1250 std::shared_lock<std::shared_mutex> lock(sessionListenerMutex_);
1251 listener = sessionListener_;
1252 }
1253 if (listener) {
1254 HILOGI("handler sessionListener");
1255 listener->OnDisConnect(sessionId_);
1256 } else {
1257 EventCallbackInfo callbackInfo;
1258 callbackInfo.sessionId = sessionId_;
1259 callbackInfo.reason = ConvertToDisconnectReason(reason);
1260 ExeuteEventCallback(EVENT_DISCONNECT, callbackInfo);
1261 }
1262 }
1263
ConvertToDisconnectReason(const ShutdownReason & reason)1264 DisconnectReason AbilityConnectionSession::ConvertToDisconnectReason(const ShutdownReason& reason)
1265 {
1266 HILOGI("Shutdown reason code is %{public}d.", static_cast<int32_t>(reason));
1267 switch (reason) {
1268 case ShutdownReason::SHUTDOWN_REASON_PEER:
1269 return DisconnectReason::PEER_APP_EXIT;
1270 default:
1271 return DisconnectReason::NETWORK_DISCONNECTED;
1272 }
1273 }
1274
OnMessageReceived(int32_t channelId,const std::shared_ptr<AVTransDataBuffer> dataBuffer)1275 void AbilityConnectionSession::OnMessageReceived(int32_t channelId, const std::shared_ptr<AVTransDataBuffer> dataBuffer)
1276 {
1277 HILOGI("called.");
1278 if (!IsVaildChannel(channelId)) {
1279 return;
1280 }
1281 uint8_t *data = dataBuffer->Data();
1282 auto headerPara = MessageDataHeader::Deserialize(data, dataBuffer->Size());
1283 if (!headerPara) {
1284 HILOGE("read session header from buffer failed");
1285 return;
1286 }
1287 std::string msg(reinterpret_cast<const char *>(data + MessageDataHeader::HEADER_LEN),
1288 dataBuffer->Size() - MessageDataHeader::HEADER_LEN);
1289 HILOGI("headerPara type is %{public}d", headerPara->dataType_);
1290 auto iter = messageHandlerMap_.find(headerPara->dataType_);
1291 if (iter != messageHandlerMap_.end()) {
1292 iter->second(msg);
1293 } else {
1294 HILOGE("unhandled code!");
1295 }
1296 }
1297
OnSendFile(const int32_t channelId,const FileInfo & info)1298 void AbilityConnectionSession::OnSendFile(const int32_t channelId, const FileInfo& info)
1299 {
1300 HILOGI("called.");
1301 if (!IsVaildChannel(channelId)) {
1302 return;
1303 }
1304
1305 std::shared_ptr<IAbilityConnectionSessionListener> listener;
1306 {
1307 std::shared_lock<std::shared_mutex> lock(sessionListenerMutex_);
1308 listener = sessionListener_;
1309 }
1310 if (listener == nullptr) {
1311 HILOGE("listener is nullptr");
1312 return;
1313 }
1314 listener->OnSendFile(sessionId_, info);
1315 }
1316
OnRecvFile(const int32_t channelId,const FileInfo & info)1317 void AbilityConnectionSession::OnRecvFile(const int32_t channelId, const FileInfo& info)
1318 {
1319 HILOGI("called.");
1320 if (!IsVaildChannel(channelId)) {
1321 return;
1322 }
1323
1324 std::shared_ptr<IAbilityConnectionSessionListener> listener;
1325 {
1326 std::shared_lock<std::shared_mutex> lock(sessionListenerMutex_);
1327 listener = sessionListener_;
1328 }
1329 if (listener == nullptr) {
1330 HILOGE("listener is nullptr");
1331 return;
1332 }
1333 listener->OnRecvFile(sessionId_, info);
1334 }
1335
GetRecvPath(const int32_t channelId)1336 const char* AbilityConnectionSession::GetRecvPath(const int32_t channelId)
1337 {
1338 HILOGI("called.");
1339 if (!IsVaildChannel(channelId)) {
1340 return nullptr;
1341 }
1342
1343 std::shared_ptr<IAbilityConnectionSessionListener> listener;
1344 {
1345 std::shared_lock<std::shared_mutex> lock(sessionListenerMutex_);
1346 listener = sessionListener_;
1347 }
1348 if (listener == nullptr) {
1349 HILOGE("listener is nullptr");
1350 return nullptr;
1351 }
1352 return listener->GetRecvPath(sessionId_);
1353 }
1354
ExeuteMessageEventCallback(const std::string msg)1355 void AbilityConnectionSession::ExeuteMessageEventCallback(const std::string msg)
1356 {
1357 HILOGI("called.");
1358 std::shared_ptr<IAbilityConnectionSessionListener> listener;
1359 {
1360 std::shared_lock<std::shared_mutex> lock(sessionListenerMutex_);
1361 listener = sessionListener_;
1362 }
1363 // bi-channel need wait
1364 {
1365 std::unique_lock<std::mutex> lock(connectionMutex_);
1366 bool isConnected = connectionCondition_.wait_for(
1367 lock,
1368 std::chrono::seconds(WAIT_FOR_CONNECT),
1369 [this]() { return sessionStatus_ == SessionStatus::CONNECTED; }
1370 );
1371 if (!isConnected) {
1372 HILOGE("Wait for channel connection timed out after %{public}d seconds.", WAIT_FOR_CONNECT);
1373 return;
1374 }
1375 }
1376 HILOGI("start to add msg callback to handler");
1377 if (listener != nullptr) {
1378 HILOGI("handler sessionListener");
1379 auto func = [listener, msg, this]() {
1380 listener->OnMessage(sessionId_, msg);
1381 };
1382 eventHandler_->PostTask(func, AppExecFwk::EventQueue::Priority::LOW);
1383 } else {
1384 EventCallbackInfo callbackInfo;
1385 callbackInfo.sessionId = sessionId_;
1386 callbackInfo.msg = msg;
1387 auto func = [callbackInfo, this]() mutable {
1388 ExeuteEventCallback(EVENT_RECEIVE_MESSAGE, callbackInfo);
1389 };
1390 eventHandler_->PostTask(func, AppExecFwk::EventQueue::Priority::LOW);
1391 }
1392 }
1393
UpdateRecvEngineTransChannel()1394 void AbilityConnectionSession::UpdateRecvEngineTransChannel()
1395 {
1396 HILOGI("called.");
1397 if (recvEngine_ == nullptr) {
1398 HILOGE("recvEngine_ is nullptr.");
1399 return;
1400 }
1401
1402 TransChannelInfo info;
1403 int32_t ret = GetTransChannelInfo(TransChannelType::STREAM, info);
1404 if (ret != ERR_OK) {
1405 HILOGI("not find stream chennel.");
1406 return;
1407 }
1408 recvEngine_->SetChannelListener(info.channelId);
1409 SendMessage("updateSenderEngineTransChannel", MessageType::UPDATE_SENDER_ENGINE_CHANNEL);
1410 }
1411
UpdateSenderEngineTransChannel()1412 void AbilityConnectionSession::UpdateSenderEngineTransChannel()
1413 {
1414 HILOGI("called.");
1415 if (senderEngine_ == nullptr) {
1416 HILOGE("senderEngine_ is nullptr.");
1417 return;
1418 }
1419
1420 TransChannelInfo info;
1421 int32_t ret = GetTransChannelInfo(TransChannelType::STREAM, info);
1422 if (ret != ERR_OK) {
1423 HILOGI("not find stream chennel.");
1424 return;
1425 }
1426 HILOGI("SetChannelListener channelId is %{public}d, channelType is %{public}d",
1427 info.channelId, static_cast<int32_t>(info.channelType));
1428 senderEngine_->SetTransChannel(info.channelId, info.channelType);
1429 }
1430
ConnectFileChannel(const std::string & peerSocketName)1431 void AbilityConnectionSession::ConnectFileChannel(const std::string& peerSocketName)
1432 {
1433 HILOGI("called.");
1434 peerSocketName_ = peerSocketName;
1435 int32_t ret = CreateChannel(channelName_, ChannelDataType::FILE, TransChannelType::SEND_FILE, true);
1436 if (ret != ERR_OK) {
1437 HILOGE("create send file channel failed!");
1438 return;
1439 }
1440
1441 ret = ConnectTransChannel(TransChannelType::SEND_FILE);
1442 if (ret != ERR_OK) {
1443 HILOGI("connect file chennel failed.");
1444 SendMessage("FILE_CHANNEL_CONNECT_FAILED", MessageType::FILE_CHANNEL_CONNECT_FAILED);
1445 return;
1446 }
1447 SendMessage("FILE_CHANNEL_CONNECT_SUCCESS", MessageType::FILE_CHANNEL_CONNECT_SUCCESS);
1448 HandleSessionConnect();
1449 }
1450
OnRecvPixelMap(const std::shared_ptr<Media::PixelMap> & pixelMap)1451 void AbilityConnectionSession::OnRecvPixelMap(const std::shared_ptr<Media::PixelMap>& pixelMap)
1452 {
1453 HILOGI("called.");
1454 if (pixelMap == nullptr) {
1455 HILOGE("pixelMap is nullptr.");
1456 }
1457
1458 EventCallbackInfo callbackInfo;
1459 callbackInfo.sessionId = sessionId_;
1460 callbackInfo.image = pixelMap;
1461 ExeuteEventCallback(EVENT_RECEIVE_IMAGE, callbackInfo);
1462 }
1463
OnBytesReceived(int32_t channelId,const std::shared_ptr<AVTransDataBuffer> dataBuffer)1464 void AbilityConnectionSession::OnBytesReceived(int32_t channelId, const std::shared_ptr<AVTransDataBuffer> dataBuffer)
1465 {
1466 HILOGI("called.");
1467 if (!IsVaildChannel(channelId)) {
1468 return;
1469 }
1470
1471 if (IsStreamBytesChannel(channelId)) {
1472 HILOGE("is stream bytes channel, no need to send.");
1473 return;
1474 }
1475 // bi-channel need wait
1476 {
1477 std::unique_lock<std::mutex> lock(connectionMutex_);
1478 bool isConnected = connectionCondition_.wait_for(
1479 lock,
1480 std::chrono::seconds(WAIT_FOR_CONNECT),
1481 [this]() { return IsAllChannelConnected(); }
1482 );
1483 if (!isConnected) {
1484 HILOGE("Wait for channel connection timed out.");
1485 return;
1486 }
1487 }
1488 std::shared_ptr<IAbilityConnectionSessionListener> listener;
1489 {
1490 std::shared_lock<std::shared_mutex> lock(sessionListenerMutex_);
1491 listener = sessionListener_;
1492 }
1493 if (listener != nullptr) {
1494 HILOGI("handler sessionListener");
1495 listener->OnData(sessionId_, dataBuffer);
1496 } else {
1497 EventCallbackInfo callbackInfo;
1498 callbackInfo.sessionId = sessionId_;
1499 callbackInfo.data = dataBuffer;
1500 ExeuteEventCallback(EVENT_RECEIVE_DATA, callbackInfo);
1501 }
1502 }
1503
OnError(int32_t channelId,const int32_t errorCode)1504 void AbilityConnectionSession::OnError(int32_t channelId, const int32_t errorCode)
1505 {
1506 HILOGI("error receive");
1507 if (!IsVaildChannel(channelId)) {
1508 return;
1509 }
1510 std::shared_ptr<IAbilityConnectionSessionListener> listener;
1511 {
1512 std::shared_lock<std::shared_mutex> lock(sessionListenerMutex_);
1513 listener = sessionListener_;
1514 }
1515 if (listener != nullptr) {
1516 HILOGI("handler sessionListener");
1517 listener->OnError(sessionId_, errorCode);
1518 } else {
1519 CollaborateEventInfo info;
1520 info.eventType = CollaborateEventType::SEND_FAILURE;
1521 info.sessionId = sessionId_;
1522 ExeuteEventCallback(EVENT_COLLABORATE, info);
1523 }
1524 }
1525
IsStreamBytesChannel(const int32_t channelId)1526 bool AbilityConnectionSession::IsStreamBytesChannel(const int32_t channelId)
1527 {
1528 TransChannelInfo transChannelInfo;
1529 int32_t ret = GetTransChannelInfo(TransChannelType::STREAM_BYTES, transChannelInfo);
1530 if (ret != ERR_OK) {
1531 HILOGE("stream bytes channel not exit!");
1532 return false;
1533 }
1534
1535 return transChannelInfo.channelId == channelId;
1536 }
1537
IsVaildChannel(const int32_t channelId)1538 bool AbilityConnectionSession::IsVaildChannel(const int32_t channelId)
1539 {
1540 HILOGD("called");
1541 std::shared_lock<std::shared_mutex> channelReadLock(transChannelMutex_);
1542 if (transChannels_.empty()) {
1543 HILOGE("transChannels_ is empty");
1544 return false;
1545 }
1546
1547 for (auto& iter : transChannels_) {
1548 if (iter.second.channelId == channelId) {
1549 return true;
1550 }
1551 }
1552
1553 return false;
1554 }
1555
SetTimeOut(int32_t time)1556 void AbilityConnectionSession::SetTimeOut(int32_t time)
1557 {
1558 HILOGD("called.");
1559 auto func = [this]() {
1560 Release();
1561 };
1562 if (eventHandler_ == nullptr) {
1563 HILOGE("eventHandler_ is nullptr");
1564 return;
1565 }
1566 eventHandler_->PostTask(func, CONNECT_SESSION_TIMEOUT_END_TASK, time);
1567 }
1568
RemoveTimeout()1569 void AbilityConnectionSession::RemoveTimeout()
1570 {
1571 HILOGD("called.");
1572 if (eventHandler_ == nullptr) {
1573 HILOGE("eventHandler_ is nullptr");
1574 return;
1575 }
1576 eventHandler_->RemoveTask(CONNECT_SESSION_TIMEOUT_END_TASK);
1577 }
1578
IsConnecting()1579 bool AbilityConnectionSession::IsConnecting()
1580 {
1581 std::shared_lock<std::shared_mutex> sessionStatusReadLock(sessionMutex_);
1582 return sessionStatus_ == SessionStatus::CONNECTING;
1583 }
1584
IsConnected()1585 bool AbilityConnectionSession::IsConnected()
1586 {
1587 std::shared_lock<std::shared_mutex> sessionStatusReadLock(sessionMutex_);
1588 return sessionStatus_ == SessionStatus::CONNECTED;
1589 }
1590
FinishSessionConnect()1591 void AbilityConnectionSession::FinishSessionConnect()
1592 {
1593 HILOGI("finish %{public}d connect callback", sessionId_);
1594 std::lock_guard<std::shared_mutex> sessionStatusLock(sessionMutex_);
1595 sessionStatus_ = SessionStatus::CONNECTED;
1596 connectionCondition_.notify_all();
1597 }
1598
ExeuteConnectCallback(const ConnectResult & result)1599 void AbilityConnectionSession::ExeuteConnectCallback(const ConnectResult& result)
1600 {
1601 HILOGI("called.");
1602 if (eventHandler_ == nullptr) {
1603 HILOGE("eventHandler_ is nullptr");
1604 return;
1605 }
1606
1607 auto task = [this, result, connectCallback = connectCallback_]() {
1608 HILOGI("execute connect callback task.");
1609 if (connectCallback == nullptr) {
1610 HILOGE("connect callback is nullptr.");
1611 return;
1612 }
1613 connectCallback(result);
1614 if (!result.isConnected) {
1615 Release();
1616 }
1617 };
1618 eventHandler_->PostTask(task,
1619 "ExeuteConnectCallback", 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
1620 }
1621
CollabChannelListener(const std::shared_ptr<AbilityConnectionSession> & abilityConnectionSession)1622 AbilityConnectionSession::CollabChannelListener::CollabChannelListener(
1623 const std::shared_ptr<AbilityConnectionSession>& abilityConnectionSession)
1624 : abilityConnectionSession_(abilityConnectionSession)
1625 {
1626 }
1627
OnConnect(const int32_t channelId) const1628 void AbilityConnectionSession::CollabChannelListener::OnConnect(const int32_t channelId) const
1629 {
1630 HILOGI("called.");
1631 std::shared_ptr<AbilityConnectionSession> abilityConnectionSession = abilityConnectionSession_.lock();
1632 if (abilityConnectionSession == nullptr) {
1633 HILOGE("abilityConnectionSession is null");
1634 return;
1635 }
1636
1637 abilityConnectionSession->OnChannelConnect(channelId);
1638 }
1639
OnDisConnect(const int32_t channelId,const ShutdownReason & reason) const1640 void AbilityConnectionSession::CollabChannelListener::OnDisConnect(const int32_t channelId,
1641 const ShutdownReason& reason) const
1642 {
1643 HILOGI("called.");
1644 std::shared_ptr<AbilityConnectionSession> abilityConnectionSession = abilityConnectionSession_.lock();
1645 if (abilityConnectionSession == nullptr) {
1646 HILOGE("abilityConnectionSession is null");
1647 return;
1648 }
1649
1650 abilityConnectionSession->OnChannelClosed(channelId, reason);
1651 }
1652
OnMessage(const int32_t channelId,const std::shared_ptr<AVTransDataBuffer> & buffer) const1653 void AbilityConnectionSession::CollabChannelListener::OnMessage(const int32_t channelId,
1654 const std::shared_ptr<AVTransDataBuffer>& buffer) const
1655 {
1656 HILOGI("called.");
1657 std::shared_ptr<AbilityConnectionSession> abilityConnectionSession = abilityConnectionSession_.lock();
1658 if (abilityConnectionSession == nullptr) {
1659 HILOGE("abilityConnectionSession is null");
1660 return;
1661 }
1662
1663 abilityConnectionSession->OnMessageReceived(channelId, buffer);
1664 }
1665
OnBytes(const int32_t channelId,const std::shared_ptr<AVTransDataBuffer> & buffer) const1666 void AbilityConnectionSession::CollabChannelListener::OnBytes(const int32_t channelId,
1667 const std::shared_ptr<AVTransDataBuffer>& buffer) const
1668 {
1669 HILOGI("called.");
1670 std::shared_ptr<AbilityConnectionSession> abilityConnectionSession = abilityConnectionSession_.lock();
1671 if (abilityConnectionSession == nullptr) {
1672 HILOGE("abilityConnectionSession is null");
1673 return;
1674 }
1675
1676 abilityConnectionSession->OnBytesReceived(channelId, buffer);
1677 }
1678
OnStream(const int32_t channelId,const std::shared_ptr<AVTransStreamData> & sendData) const1679 void AbilityConnectionSession::CollabChannelListener::OnStream(const int32_t channelId,
1680 const std::shared_ptr<AVTransStreamData>& sendData) const
1681 {
1682 }
1683
OnError(const int32_t channelId,const int32_t errorCode) const1684 void AbilityConnectionSession::CollabChannelListener::OnError(const int32_t channelId, const int32_t errorCode) const
1685 {
1686 HILOGI("called.");
1687 if (auto abilityConnectionSession = abilityConnectionSession_.lock()) {
1688 abilityConnectionSession->OnError(channelId, errorCode);
1689 }
1690 }
1691
OnSendFile(const int32_t channelId,const FileInfo & info) const1692 void AbilityConnectionSession::CollabChannelListener::OnSendFile(const int32_t channelId, const FileInfo& info) const
1693 {
1694 HILOGI("called.");
1695 std::shared_ptr<AbilityConnectionSession> abilityConnectionSession = abilityConnectionSession_.lock();
1696 if (abilityConnectionSession == nullptr) {
1697 HILOGE("abilityConnectionSession is null");
1698 return;
1699 }
1700
1701 abilityConnectionSession->OnSendFile(channelId, info);
1702 }
1703
OnRecvFile(const int32_t channelId,const FileInfo & info) const1704 void AbilityConnectionSession::CollabChannelListener::OnRecvFile(const int32_t channelId, const FileInfo& info) const
1705 {
1706 HILOGI("called.");
1707 std::shared_ptr<AbilityConnectionSession> abilityConnectionSession = abilityConnectionSession_.lock();
1708 if (abilityConnectionSession == nullptr) {
1709 HILOGE("abilityConnectionSession is null");
1710 return;
1711 }
1712
1713 abilityConnectionSession->OnRecvFile(channelId, info);
1714 }
1715
GetRecvPath(const int32_t channelId) const1716 const char* AbilityConnectionSession::CollabChannelListener::GetRecvPath(const int32_t channelId) const
1717 {
1718 HILOGI("called.");
1719 std::shared_ptr<AbilityConnectionSession> abilityConnectionSession = abilityConnectionSession_.lock();
1720 if (abilityConnectionSession == nullptr) {
1721 HILOGE("abilityConnectionSession is null");
1722 return nullptr;
1723 }
1724
1725 return abilityConnectionSession->GetRecvPath(channelId);
1726 }
1727
PixelMapListener(const std::shared_ptr<AbilityConnectionSession> & session)1728 AbilityConnectionSession::PixelMapListener::PixelMapListener(
1729 const std::shared_ptr<AbilityConnectionSession>& session) : abilityConnectionSession_(session)
1730 {
1731 }
1732
OnRecvPixelMap(const std::shared_ptr<Media::PixelMap> & pixelMap)1733 void AbilityConnectionSession::PixelMapListener::OnRecvPixelMap(const std::shared_ptr<Media::PixelMap>& pixelMap)
1734 {
1735 HILOGI("called.");
1736 std::shared_ptr<AbilityConnectionSession> abilityConnectionSession = abilityConnectionSession_.lock();
1737 if (abilityConnectionSession == nullptr) {
1738 HILOGE("abilityConnectionSession is null");
1739 return;
1740 }
1741
1742 abilityConnectionSession->OnRecvPixelMap(pixelMap);
1743 }
1744
OnRecvSurfaceParam(const SurfaceParam & param)1745 void AbilityConnectionSession::PixelMapListener::OnRecvSurfaceParam(const SurfaceParam& param)
1746 {
1747 HILOGI("called.");
1748 }
1749 } // namespace DistributedCollab
1750 } // namespace OHOS