1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "dcamera_channel_source_impl.h" 17 18 #include "dcamera_softbus_adapter.h" 19 20 #include "anonymous_string.h" 21 #include "distributed_camera_constants.h" 22 #include "distributed_camera_errno.h" 23 #include "distributed_hardware_log.h" 24 25 namespace OHOS { 26 namespace DistributedHardware { DCameraChannelSourceImpl()27 DCameraChannelSourceImpl::DCameraChannelSourceImpl() 28 { 29 } 30 ~DCameraChannelSourceImpl()31 DCameraChannelSourceImpl::~DCameraChannelSourceImpl() 32 { 33 } 34 OpenSession()35 int32_t DCameraChannelSourceImpl::OpenSession() 36 { 37 DHLOGI("DCameraChannelSourceImpl OpenSession name: %s", mySessionName_.c_str()); 38 if (softbusSessions_.empty()) { 39 DHLOGE("DCameraChannelSourceImpl OpenSession %s failed", mySessionName_.c_str()); 40 return DCAMERA_BAD_OPERATE; 41 } 42 int32_t ret = DCAMERA_OK; 43 for (auto iter = softbusSessions_.begin(); iter != softbusSessions_.end(); iter++) { 44 int32_t retOpen = (*iter)->OpenSession(); 45 if (retOpen != DCAMERA_OK) { 46 DHLOGE("DCameraChannelSourceImpl OpenSession %s failed, ret: %d", mySessionName_.c_str(), retOpen); 47 ret = DCAMERA_BAD_OPERATE; 48 break; 49 } 50 } 51 52 if (ret != DCAMERA_OK) { 53 CloseSession(); 54 } 55 56 return ret; 57 } 58 CloseSession()59 int32_t DCameraChannelSourceImpl::CloseSession() 60 { 61 DHLOGI("DCameraChannelSourceImpl CloseSession name: %s", mySessionName_.c_str()); 62 if (softbusSessions_.empty()) { 63 DHLOGE("DCameraChannelSourceImpl CloseSession %s failed", mySessionName_.c_str()); 64 return DCAMERA_BAD_OPERATE; 65 } 66 int32_t ret = DCAMERA_OK; 67 for (auto iter = softbusSessions_.begin(); iter != softbusSessions_.end(); iter++) { 68 int32_t retOpen = (*iter)->CloseSession(); 69 if (retOpen != DCAMERA_OK) { 70 DHLOGE("DCameraChannelSourceImpl CloseSession %s failed, ret: %d", mySessionName_.c_str(), retOpen); 71 ret = DCAMERA_BAD_OPERATE; 72 } 73 } 74 75 return ret; 76 } 77 CreateSession(std::vector<DCameraIndex> & camIndexs,std::string sessionFlag,DCameraSessionMode sessionMode,std::shared_ptr<ICameraChannelListener> & listener)78 int32_t DCameraChannelSourceImpl::CreateSession(std::vector<DCameraIndex>& camIndexs, std::string sessionFlag, 79 DCameraSessionMode sessionMode, std::shared_ptr<ICameraChannelListener>& listener) 80 { 81 if (camIndexs.size() > DCAMERA_MAX_NUM || listener == nullptr) { 82 return DCAMERA_BAD_VALUE; 83 } 84 if (!softbusSessions_.empty()) { 85 DHLOGI("DCameraChannelSourceImpl session has already create %s", sessionFlag.c_str()); 86 return DCAMERA_OK; 87 } 88 camIndexs_.assign(camIndexs.begin(), camIndexs.end()); 89 listener_ = listener; 90 mySessionName_ = SESSION_HEAD + sessionFlag; 91 mode_ = sessionMode; 92 std::string myDevId; 93 DCameraSoftbusAdapter::GetInstance().GetLocalNetworkId(myDevId); 94 DHLOGI("DCameraChannelSourceImpl session create name: %s devId: %s", mySessionName_.c_str(), 95 GetAnonyString(myDevId).c_str()); 96 int32_t ret = DCameraSoftbusAdapter::GetInstance().CreateSoftbusSessionServer(mySessionName_, 97 DCAMERA_CHANNLE_ROLE_SOURCE); 98 if (ret != DCAMERA_OK) { 99 DHLOGE("DCameraChannelSourceImpl CreateSession %s failed, ret: %d", mySessionName_.c_str(), ret); 100 return ret; 101 } 102 for (auto iter = camIndexs.begin(); iter != camIndexs.end(); iter++) { 103 std::string peerDevId = (*iter).devId_; 104 std::string peerSessionName = SESSION_HEAD + (*iter).dhId_ + std::string("_") + sessionFlag; 105 std::shared_ptr<DCameraSoftbusSession> softbusSess = std::make_shared<DCameraSoftbusSession>(myDevId, 106 mySessionName_, peerDevId, peerSessionName, listener, sessionMode); 107 softbusSessions_.push_back(softbusSess); 108 DCameraSoftbusAdapter::GetInstance().sourceSessions_[peerDevId + peerSessionName] = softbusSess; 109 } 110 return DCAMERA_OK; 111 } 112 ReleaseSession()113 int32_t DCameraChannelSourceImpl::ReleaseSession() 114 { 115 DHLOGI("DCameraChannelSourceImpl ReleaseSession name: %s", mySessionName_.c_str()); 116 for (auto iter = softbusSessions_.begin(); iter != softbusSessions_.end(); iter++) { 117 std::string sessKey = (*iter)->GetPeerDevId() + (*iter)->GetPeerSessionName(); 118 DCameraSoftbusAdapter::GetInstance().sourceSessions_.erase(sessKey); 119 } 120 std::vector<std::shared_ptr<DCameraSoftbusSession>>().swap(softbusSessions_); 121 int32_t ret = DCameraSoftbusAdapter::GetInstance().DestroySoftbusSessionServer(mySessionName_); 122 if (ret != DCAMERA_OK) { 123 DHLOGE("DCameraChannelSourceImpl ReleaseSession %s failed, ret: %d", mySessionName_.c_str(), ret); 124 } 125 return ret; 126 } 127 SendData(std::shared_ptr<DataBuffer> & buffer)128 int32_t DCameraChannelSourceImpl::SendData(std::shared_ptr<DataBuffer>& buffer) 129 { 130 if (softbusSessions_.empty()) { 131 DHLOGE("DCameraChannelSourceImpl SendData %s failed", mySessionName_.c_str()); 132 return DCAMERA_BAD_OPERATE; 133 } 134 int32_t ret = DCAMERA_OK; 135 for (auto iter = softbusSessions_.begin(); iter != softbusSessions_.end(); iter++) { 136 int32_t retSend = (*iter)->SendData(mode_, buffer); 137 if (retSend != DCAMERA_OK) { 138 DHLOGE("DCameraChannelSourceImpl SendData %s failed, ret: %d", mySessionName_.c_str(), retSend); 139 ret = DCAMERA_BAD_OPERATE; 140 } 141 } 142 return ret; 143 } 144 } 145 }