remote_control.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "pch.h"
  2. #include <fstream>
  3. #include <json/json.h>
  4. #include "api/video/video_frame.h"
  5. #include "../common/comm.h"
  6. #include "../common/iobuffer.h"
  7. #include "remote.pb.h"
  8. #include "message_queue.h"
  9. #include "socket_remote.h"
  10. #include "remote_control.h"
  11. CRemoteCtrl::CRemoteCtrl(IMonitroNotify* n) :_notify(n)
  12. {
  13. _client = std::make_unique<SocketRemote>(this);
  14. }
  15. void CRemoteCtrl::Login(std::string account,std::string pass)
  16. {
  17. _client->WriteSigin(_type, account,pass,_name);
  18. }
  19. void CRemoteCtrl::Start(EgoType type, std::array<IRender*, RenderPosition::ALL>& ar)
  20. {
  21. _type = type;
  22. _connected = false;
  23. Json::Value root;
  24. Json::Reader jsonReader;
  25. std::ifstream ifile;//
  26. if (_type == EgoType::Monitor)
  27. ifile.open("monitor.json");
  28. else
  29. ifile.open("config.json");
  30. std::string ip;
  31. if (jsonReader.parse(ifile, root))
  32. {
  33. _accountText = root["account"].asString();
  34. _passText = root["password"].asString();
  35. _name = root["name"].asString();
  36. ip = root["monitor"].asString();
  37. //can_port = root["can_port"].asInt();
  38. //host_port = root["host_port"].asInt();
  39. //can_ip = root["can_ip"].asString();
  40. }
  41. _message = std::make_unique<CMessageQueue>(this);
  42. _message->Start(type, ar);
  43. _client = std::make_unique<SocketRemote>(this);
  44. _client->Start(ip.c_str());
  45. }
  46. EgoType CRemoteCtrl::GetType()
  47. {
  48. return _type;
  49. }
  50. std::string CRemoteCtrl::GetAccount()
  51. {
  52. return _accountText;
  53. }
  54. std::string CRemoteCtrl::GetName()
  55. {
  56. return _name;
  57. }
  58. std::string CRemoteCtrl::GetPassword()
  59. {
  60. return _passText;
  61. }
  62. SocketRemote* CRemoteCtrl::GetRemoteClient()
  63. {
  64. return _client.get();
  65. }
  66. void CRemoteCtrl::OnVideoOffer(int32_t index, const char* type, const char* sdp)
  67. {
  68. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  69. OfferDesc* desc = (OfferDesc*)pBuffer->Buffer;
  70. strcpy_s(desc->type, type);
  71. strcpy_s(desc->sdp, sdp);
  72. _message->PostMessage(WM_NOTIFY_OFFER, index,(int64_t)pBuffer);
  73. }
  74. void CRemoteCtrl::OnVideoAnswer(int32_t index, const char* type, const char* sdp)
  75. {
  76. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  77. AnswerDesc* p = (AnswerDesc*)(pBuffer->Buffer);
  78. strcpy_s(p->type, type);
  79. strcpy_s(p->sdp, sdp);
  80. _message->PostMessage(WM_NOTIFY_ANSWER, index,(int64_t)pBuffer);
  81. index++;
  82. if (index < RenderPosition::ALL)
  83. {
  84. _message->PostMessage(WM_ASK_VIDEOREQ,index);// DelayNextVideoReq();
  85. }
  86. }
  87. void CRemoteCtrl::ReqUserList()
  88. {
  89. _client->WriteUserList();
  90. }
  91. void CRemoteCtrl::OnVideoCandidate(int32_t index, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid)
  92. {
  93. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  94. CandidateDesc* desc = (CandidateDesc*)(pBuffer->Buffer);
  95. strcpy_s(desc->candidate, candidate);
  96. strcpy_s(desc->sdp_mid, sdp_mid);
  97. desc->sdp_mline_index = sdp_mline_index;
  98. _message->PostMessage(WM_NOTIFY_CANDIDATE, index,(int64_t)pBuffer);
  99. }
  100. void CRemoteCtrl::OnVideoReq(int32_t video, int32_t peer)
  101. {
  102. if (video == 0 && _peer != -1)
  103. {
  104. std::cout << __FUNCTION__ << "," << __LINE__ << std::endl;
  105. _client->WriteVideoRep(peer, remote::VideoDesc::Reject, video);
  106. return;
  107. }
  108. _peer = peer;
  109. _message->NotifyReq(peer, video);
  110. }
  111. void CRemoteCtrl::OnLogin(int32_t uid, bool bRet)
  112. {
  113. if (bRet)
  114. {
  115. _uid = uid;
  116. if(_type==EgoType::Monitor)
  117. _client->WriteUserList();
  118. _message->KeepAlive(true);
  119. }
  120. if(_type==EgoType::Monitor)
  121. _notify->OnLogin(bRet);
  122. }
  123. void CRemoteCtrl::OnNotifyState(int32_t rid, UserState state)
  124. {
  125. if (_type == EgoType::Monitor)
  126. _notify->OnNotifyState(rid, state);
  127. }
  128. void CRemoteCtrl::OnVideoRep(bool ok, int32_t index, int32_t peer)
  129. {
  130. if (ok == false)
  131. {
  132. _peer = -1;
  133. if (_type == EgoType::Monitor)
  134. _notify->OnNotifyVideoFail(peer);
  135. return;
  136. }
  137. if (peer != _peer) return;
  138. _message->PostMessage(WM_NOTIFY_REP, index,(int64_t)peer);
  139. }
  140. void CRemoteCtrl::OnNotifyDel(int32_t peer, EgoType type)
  141. {
  142. if (_type == EgoType::Monitor)
  143. _notify->OnNotifyDel(peer);
  144. }
  145. void CRemoteCtrl::OnRobot(const remote::Robot& robot)
  146. {
  147. auto users = std::make_unique<UserDriver>();
  148. users->uid = robot.rid();
  149. users->name = robot.name();
  150. users->type = static_cast<EgoType>(robot.type());
  151. users->state = static_cast<UserState>(robot.state());
  152. if (_type == EgoType::Monitor)
  153. _notify->OnRobot(users);
  154. }
  155. void CRemoteCtrl::OnConnected(bool bRet)
  156. {
  157. if (bRet)
  158. {
  159. _connected = true;
  160. if (!_account.empty())
  161. _client->WriteSigin(_type,_account, _pass,_name);
  162. }
  163. else
  164. {
  165. _message->KeepAlive(false);
  166. _connected = false;
  167. if (_peer != -1)
  168. {
  169. for (int32_t i = RenderPosition::FRONT; i < RenderPosition::ALL; i++)
  170. {
  171. _message->PostMessage(WM_NOTIFY_LEAVE, i);
  172. }
  173. _peer = -1;
  174. }
  175. }
  176. }
  177. EGOREMOTE_API IRemoteCtrl* GetRemoteController(IMonitroNotify* n)
  178. {
  179. return new CRemoteCtrl(n);
  180. }
  181. void CRemoteCtrl::OnConnect(int32_t peer)
  182. {
  183. if (_type == EgoType::Monitor)
  184. {
  185. _peer = peer;
  186. _message->OnConnect(peer);
  187. }
  188. }
  189. void CRemoteCtrl::OnLeave()
  190. {
  191. if (_type == EgoType::Monitor)
  192. {
  193. _client->WriteVideoLeave(_peer);
  194. _message->NotifyLeave();
  195. _peer = -1;
  196. }
  197. }
  198. void CRemoteCtrl::OnVideoLeave(int32_t peer, EgoType type)
  199. {
  200. _message->NotifyLeave();
  201. _peer = -1;
  202. if(_type==EgoType::Monitor&& _notify!=nullptr)
  203. _notify->OnNotifyLeave(peer);
  204. }
  205. void CRemoteCtrl::OverlayVideo(RenderPosition pos, const webrtc::VideoFrame& frame)
  206. {
  207. _message->OverlayVideo(pos,frame);
  208. }