123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- #include "pch.h"
- #include <fstream>
- #include <json/json.h>
- #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<SocketRemote>(this);
- }
- void CRemoteCtrl::Login(std::string account,std::string pass)
- {
- _client->WriteSigin(_type, account,pass,_name);
- }
- void CRemoteCtrl::Start(EgoType type, std::array<IRender*, RenderPosition::ALL>& 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<CMessageQueue>(this);
- _message->Start(type, ar);
- _client = std::make_unique<SocketRemote>(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<UserDriver>();
- users->uid = robot.rid();
- users->name = robot.name();
- users->type = static_cast<EgoType>(robot.type());
- users->state = static_cast<UserState>(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);
- }
|