#include "pch.h" #include #include #include "api/video/video_frame.h" #include "../common/comm.h" #include "../common/iobuffer.h" #include "remote.pb.h" #include "message_queue.h" #include "socket_remote.h" #include "remote_control.h" CRemoteCtrl::CRemoteCtrl(IMonitroNotify* n) :_notify(n) { _client = std::make_unique(this); } void CRemoteCtrl::Login(std::string account,std::string pass) { _client->WriteSigin(_type, account,pass,_name); } void CRemoteCtrl::Start(EgoType type, std::array& ar) { _type = type; _connected = false; Json::Value root; Json::Reader jsonReader; std::ifstream ifile;// if (_type == EgoType::Monitor) ifile.open("monitor.json"); else ifile.open("config.json"); std::string ip; if (jsonReader.parse(ifile, root)) { _accountText = root["account"].asString(); _passText = root["password"].asString(); _name = root["name"].asString(); ip = root["monitor"].asString(); //can_port = root["can_port"].asInt(); //host_port = root["host_port"].asInt(); //can_ip = root["can_ip"].asString(); } _message = std::make_unique(this); _message->Start(type, ar); _client = std::make_unique(this); _client->Start(ip.c_str()); } EgoType CRemoteCtrl::GetType() { return _type; } std::string CRemoteCtrl::GetAccount() { return _accountText; } std::string CRemoteCtrl::GetName() { return _name; } std::string CRemoteCtrl::GetPassword() { return _passText; } SocketRemote* CRemoteCtrl::GetRemoteClient() { return _client.get(); } void CRemoteCtrl::OnVideoOffer(int32_t index, const char* type, const char* sdp) { CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__); OfferDesc* desc = (OfferDesc*)pBuffer->Buffer; strcpy_s(desc->type, type); strcpy_s(desc->sdp, sdp); _message->PostMessage(WM_NOTIFY_OFFER, index,(int64_t)pBuffer); } void CRemoteCtrl::OnVideoAnswer(int32_t index, const char* type, const char* sdp) { CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__); AnswerDesc* p = (AnswerDesc*)(pBuffer->Buffer); strcpy_s(p->type, type); strcpy_s(p->sdp, sdp); _message->PostMessage(WM_NOTIFY_ANSWER, index,(int64_t)pBuffer); index++; if (index < RenderPosition::ALL) { _message->PostMessage(WM_ASK_VIDEOREQ,index);// DelayNextVideoReq(); } } void CRemoteCtrl::ReqUserList() { _client->WriteUserList(); } void CRemoteCtrl::OnVideoCandidate(int32_t index, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid) { CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__); CandidateDesc* desc = (CandidateDesc*)(pBuffer->Buffer); strcpy_s(desc->candidate, candidate); strcpy_s(desc->sdp_mid, sdp_mid); desc->sdp_mline_index = sdp_mline_index; _message->PostMessage(WM_NOTIFY_CANDIDATE, index,(int64_t)pBuffer); } void CRemoteCtrl::OnVideoReq(int32_t video, int32_t peer) { if (video == 0 && _peer != -1) { std::cout << __FUNCTION__ << "," << __LINE__ << std::endl; _client->WriteVideoRep(peer, remote::VideoDesc::Reject, video); return; } _peer = peer; _message->NotifyReq(peer, video); } void CRemoteCtrl::OnLogin(int32_t uid, bool bRet) { if (bRet) { _uid = uid; if(_type==EgoType::Monitor) _client->WriteUserList(); _message->KeepAlive(true); } if(_type==EgoType::Monitor) _notify->OnLogin(bRet); } void CRemoteCtrl::OnNotifyState(int32_t rid, UserState state) { if (_type == EgoType::Monitor) _notify->OnNotifyState(rid, state); } void CRemoteCtrl::OnVideoRep(bool ok, int32_t index, int32_t peer) { if (ok == false) { _peer = -1; if (_type == EgoType::Monitor) _notify->OnNotifyVideoFail(peer); return; } if (peer != _peer) return; _message->PostMessage(WM_NOTIFY_REP, index,(int64_t)peer); } void CRemoteCtrl::OnNotifyDel(int32_t peer, EgoType type) { if (_type == EgoType::Monitor) _notify->OnNotifyDel(peer); } void CRemoteCtrl::OnRobot(const remote::Robot& robot) { auto users = std::make_unique(); users->uid = robot.rid(); users->name = robot.name(); users->type = static_cast(robot.type()); users->state = static_cast(robot.state()); if (_type == EgoType::Monitor) _notify->OnRobot(users); } void CRemoteCtrl::OnConnected(bool bRet) { if (bRet) { _connected = true; if (!_account.empty()) _client->WriteSigin(_type,_account, _pass,_name); } else { _message->KeepAlive(false); _connected = false; if (_peer != -1) { for (int32_t i = RenderPosition::FRONT; i < RenderPosition::ALL; i++) { _message->PostMessage(WM_NOTIFY_LEAVE, i); } _peer = -1; } } } EGOREMOTE_API IRemoteCtrl* GetRemoteController(IMonitroNotify* n) { return new CRemoteCtrl(n); } void CRemoteCtrl::OnConnect(int32_t peer) { if (_type == EgoType::Monitor) { _peer = peer; _message->OnConnect(peer); } } void CRemoteCtrl::OnLeave() { if (_type == EgoType::Monitor) { _client->WriteVideoLeave(_peer); _message->NotifyLeave(); _peer = -1; } } void CRemoteCtrl::OnVideoLeave(int32_t peer, EgoType type) { _message->NotifyLeave(); _peer = -1; if(_type==EgoType::Monitor&& _notify!=nullptr) _notify->OnNotifyLeave(peer); } void CRemoteCtrl::OverlayVideo(RenderPosition pos, const webrtc::VideoFrame& frame) { _message->OverlayVideo(pos,frame); }