EgoWindow.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #include "api.h"
  2. #include "./include/EgoInterface.h"
  3. #include "../common/peer_connection.h"
  4. #include "EgoClient.h"
  5. #include "EgoWindow.h"
  6. #include "../common/iobuffer.h"
  7. #include "libyuv/convert_argb.h"
  8. #include "libyuv/planar_functions.h"
  9. #include "./include/EgoInterface.h"
  10. #include <time.h>
  11. #include <assert.h>
  12. #include "Protocol.pb.h"
  13. #define PI 3.141526f
  14. CEgoWindow::CEgoWindow(CEgoClient* c,IRender * render,RenderPosition pos ) :_render(render),_pos(pos), _client(c)
  15. {
  16. //_channelcreated = false;
  17. _control = ControlState::Check;
  18. }
  19. void CEgoWindow::Start()
  20. {
  21. start_ = false;
  22. //assert(n != nullptr);
  23. CMessageQueue::Start();
  24. }
  25. void CEgoWindow::Stop()
  26. {
  27. CMessageQueue::Stop();
  28. }
  29. void CEgoWindow::PostMessage(int32_t type, int64_t l /* = 0 */, int64_t r /* = 0 */)
  30. {
  31. CIOBuffer* pBuffer = CIOBuffer::Alloc(__FILE__, __LINE__);
  32. Message* message = reinterpret_cast<Message*>(pBuffer->Buffer);
  33. message->cmd = type;
  34. message->param_l = l;
  35. message->param_r = r;
  36. EnQueue(pBuffer);
  37. }
  38. void CEgoWindow::SetPeerNotify()
  39. {
  40. }
  41. void CEgoWindow::OnNotifyRep(int32_t peer)
  42. {
  43. //������Ƶ���
  44. //ccc
  45. //int32_t width = 1920;
  46. //int32_t height = 1080;
  47. //int32_t width = 1280;
  48. //int32_t height = 720;
  49. int32_t width = 1080;
  50. int32_t height = 640;
  51. ChannelType channel = (ChannelType)(_pos - RenderPosition::FRONT);
  52. _peerconnection = std::make_unique<CPeerConnection>(channel, _client->GetSocketClient());
  53. //�������ߴ����Ԥ��
  54. SetRenderWindow(width, height);
  55. //�������ݵ������ԺͿɿ���
  56. InitPeerConnection(peer);
  57. _peerconnection->CreateOffer();
  58. }
  59. int32_t CEgoWindow::GetSteerAngle()
  60. {
  61. return _client->GetSteerAngle();
  62. }
  63. void CEgoWindow::SetRenderWindow(int32_t width, int32_t height)
  64. {
  65. ZeroMemory(&bmi_, sizeof(bmi_));
  66. bmi_.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  67. bmi_.bmiHeader.biPlanes = 1;
  68. bmi_.bmiHeader.biBitCount = 32;
  69. bmi_.bmiHeader.biCompression = BI_RGB;
  70. /*if (_pos == RenderPosition::LMIRROR || _pos == RenderPosition::RMIRROR)
  71. {
  72. bmi_.bmiHeader.biWidth = height;
  73. bmi_.bmiHeader.biHeight = -width;
  74. }
  75. else {
  76. bmi_.bmiHeader.biWidth = width;
  77. bmi_.bmiHeader.biHeight = -height;
  78. }
  79. */
  80. bmi_.bmiHeader.biSizeImage =
  81. width * height * (bmi_.bmiHeader.biBitCount >> 3);
  82. image_.reset(new uint8_t[bmi_.bmiHeader.biSizeImage]);
  83. // if (_pos == RenderPosition::LMIRROR || _pos == RenderPosition::RMIRROR)
  84. // _rotate.reset(new uint8_t[bmi_.bmiHeader.biSizeImage]);
  85. }
  86. void CEgoWindow::InitPeerConnection(int32_t peer)
  87. {
  88. _peerconnection->Initialize(peer, _pos);
  89. //_peerconnection->AddDataChannel(true, false);
  90. //�ص�����Ч�ʽ�������
  91. _peerconnection->AddRemoteArgb32VideoFrameReady(&CEgoWindow::FrameCallback, this);
  92. if (_pos == RenderPosition::FRONT)
  93. {
  94. _peerconnection->AddLocalAudioTrack();
  95. }
  96. //StartRender(true);
  97. }
  98. void CEgoWindow::FrameCallback(void* user_data, const uint8_t* yptr, int32_t strideY, const uint8_t* uptr, int32_t strideU, const uint8_t* vptr, int32_t strideV, const int32_t stride, const int frame_width, const int frame_height)
  99. {
  100. auto ptr = static_cast<CEgoWindow*>(user_data);
  101. ptr->OnArgb32FrameReady(yptr, strideY, uptr, strideU, vptr, strideV, stride, frame_width, frame_height);
  102. }
  103. void CEgoWindow::SetReady(bool b)
  104. {
  105. start_ = b;
  106. if (b == false)
  107. {
  108. _render->Empty();
  109. }
  110. }
  111. void CEgoWindow::OnArgb32FrameReady(const uint8_t* yptr, int32_t strideY, const uint8_t* uptr, int32_t strideU, const uint8_t* vptr, int32_t strideV, const int32_t stride, const int width, const int height)
  112. {
  113. {
  114. // int32_t*pixels = (int32_t*)data;
  115. //std::lock_guard < std::mutex> l(buffer_lock_);
  116. if (!start_) return;
  117. if (_pos == RenderPosition::LEFT || _pos == RenderPosition::RIGHT)//|| _pos==RenderPosition::LANCHOR||_pos==RenderPosition::RANCHOR)
  118. {
  119. SetSize(width, height);
  120. int Dst_Stride_Y_mirror = width;
  121. int Dst_Stride_U_mirror = (width+1) >> 1;
  122. int Dst_Stride_V_mirror = Dst_Stride_U_mirror;
  123. // uint8_t* Dst_data=(uint8_t *)malloc(width * height * 3 / 2);
  124. int I420_Y_Size = width * height;
  125. // int I420_U_Size = (src_width >> 1) * (src_height >> 1);
  126. int I420_U_Size = width * height / 4;
  127. unsigned char* Y_data_Dst_rotate = _rotate.get();
  128. unsigned char* U_data_Dst_rotate = _rotate.get() + I420_Y_Size;
  129. unsigned char* V_data_Dst_rotate = _rotate.get() + I420_Y_Size + I420_U_Size;
  130. //libyuv::I420Mirror(yptr, strideY, uptr, strideU, vptr, strideV, Y_data_Dst_rotate, Dst_Stride_Y_mirror, U_data_Dst_rotate, Dst_Stride_U_mirror, V_data_Dst_rotate, Dst_Stride_V_mirror, width, height);
  131. //libyuv::I420ToARGB(Y_data_Dst_rotate, strideY, U_data_Dst_rotate, strideU, V_data_Dst_rotate, strideV, image_.get(), stride, width, height);
  132. libyuv::I420ToARGB(yptr, strideY, uptr, strideU, vptr, strideV, image_.get(), stride, width, height);
  133. //free(Dst_data);
  134. }
  135. else
  136. {
  137. SetSize(width, height);
  138. libyuv::I420ToARGB(yptr, strideY, uptr, strideU, vptr, strideV, image_.get(), stride, width, height);
  139. }
  140. }
  141. _render->OnRender(image_,width,height);
  142. }
  143. void CEgoWindow::SetSize(int32_t width, int32_t height)
  144. {
  145. if (width == bmi_.bmiHeader.biWidth && height == -bmi_.bmiHeader.biHeight) {
  146. return;
  147. }
  148. // if (m_Pos == CameraPosition::CAR_LEFT || m_Pos == CameraPosition::CAR_RIGHT)
  149. // {
  150. // bmi_.bmiHeader.biWidth = height;
  151. // bmi_.bmiHeader.biHeight = -width;
  152. // }
  153. // else
  154. {
  155. bmi_.bmiHeader.biWidth = width;
  156. bmi_.bmiHeader.biHeight = -height;
  157. }
  158. bmi_.bmiHeader.biSizeImage =
  159. width * height * (bmi_.bmiHeader.biBitCount >> 3);
  160. image_.reset(new uint8_t[bmi_.bmiHeader.biSizeImage]);
  161. _rotate.reset(new uint8_t[width * height*3/2]);
  162. }
  163. // void CEgoWindow::WriteCanMessage(std::unordered_map<int32_t, cannet_frame>& node, bool)
  164. // {
  165. // if (_channelcreated == false || node.empty()) return;
  166. // RemoNet::CCCanMesage Req;
  167. // Req.set_islidar(false);
  168. // for (auto& it : node)
  169. // {
  170. // // if (it.second.canid == htonl(0x486))
  171. // {
  172. // auto m = Req.add_message();
  173. // m->set_head(it.second.dlc);
  174. // m->set_canid(it.second.canid);
  175. // m->set_data(it.second.data, 8);
  176. // // m->set_islidar(false);
  177. // }
  178. // // it->second.canid
  179. // }
  180. // MessageHead Head;
  181. // CIOBuffer pBuffer;
  182. // Head.Command = RemoNet::CC_CAN;
  183. // Head.Length = Req.ByteSizeLong();
  184. // Head.Serialize(pBuffer.Buffer);
  185. // auto ptr = pBuffer.Buffer + MessageHead::Size();
  186. // Req.SerializeToArray(ptr, Head.Length);
  187. // pBuffer.Length = MessageHead::Size() + Head.Length;
  188. // _peerconnection->SendData(&pBuffer);
  189. // }
  190. void CEgoWindow::Process(CIOBuffer* pBuffer)
  191. {
  192. Message* message = reinterpret_cast<Message*>(pBuffer->Buffer);
  193. switch (message->cmd)
  194. {
  195. case WM_NOTIFY_REP:
  196. OnNotifyRep((int32_t)message->param_l);
  197. break;;
  198. case WM_NOTIFY_ANSWER:
  199. OnNotifyAnswer((CIOBuffer*)message->param_l);
  200. break;
  201. /*case WM_NOTIFY_LEAVE:
  202. OnNotifyLeave();
  203. break;
  204. */
  205. case WM_NOTIFY_CANDIDATE:
  206. OnNotifyCandidate((CIOBuffer*)message->param_l);
  207. break;
  208. case WM_NOTIFY_OFFER:
  209. OnNotifyOffer((CIOBuffer*)message->param_l);
  210. break;
  211. case WM_ASK_CHANNEL:
  212. //OnAskDataChannel();
  213. break;
  214. case WM_ASK_PING:
  215. OnAskPing();
  216. break;
  217. case WM_ASK_VIDEOREQ:
  218. OnAskVideoReq();
  219. break;
  220. }
  221. }
  222. void CEgoWindow::OnNotifyAnswer(CIOBuffer* pBuffer)
  223. {
  224. AnswerDesc* desc = (AnswerDesc*)pBuffer->Buffer;
  225. const char* type = desc->type;
  226. const char* sdp = desc->sdp;
  227. _peerconnection->SetRemoteDescription(type, sdp);
  228. pBuffer->Release(__FILE__, __LINE__);
  229. PostMessage(WM_ASK_CHANNEL);
  230. // DelayDataChannel();
  231. }
  232. void CEgoWindow::OnNotifyOffer(CIOBuffer* pBuffer)
  233. {
  234. OfferDesc* desc = (OfferDesc*)pBuffer->Buffer;
  235. auto type = desc->type;
  236. auto sdp = desc->sdp;
  237. _peerconnection->SetRemoteDescription(type, sdp);
  238. _peerconnection->CreateAnswer();
  239. pBuffer->Release(__FILE__, __LINE__);
  240. }
  241. void CEgoWindow::OnNotifyCandidate(CIOBuffer* pBuffer)
  242. {
  243. CandidateDesc* desc = (CandidateDesc*)pBuffer->Buffer;
  244. auto candidate = desc->candidate;
  245. auto sdp_mid = desc->sdp_mid;
  246. auto sdp_mline_index = desc->sdp_mline_index;
  247. _peerconnection->AddIceCandidate(candidate, sdp_mline_index, sdp_mid);
  248. pBuffer->Release(__FILE__, __LINE__);
  249. }
  250. void CEgoWindow::OnNotifyLeave()
  251. {
  252. if (start_) return;
  253. assert(start_ == false);
  254. StopPing();
  255. //_client->_can->Stop();
  256. //_client->_udpcan->Stop();
  257. //_controlSensor->Stop();
  258. // _channelcreated = false;
  259. _peer = -1;
  260. /*if (_pos == RenderPosition::FRONT)
  261. {
  262. _peerconnection->RemoveLocalAudioTrack();
  263. }
  264. */
  265. if (_peerconnection != nullptr)
  266. {
  267. _peerconnection->Close();
  268. _peerconnection.reset();
  269. _peerconnection = nullptr;
  270. }
  271. //image_ = nullptr;
  272. if (_render != nullptr)
  273. //_render->OnRender(image_,0,0);
  274. _render->Empty();
  275. _control = ControlState::Check;
  276. }
  277. /*
  278. void CEgoWindow::DelayDataChannel()
  279. {
  280. _delayThread = std::thread(&CEgoWindow::AskDataChannel, this);
  281. _delayThread.detach();
  282. }
  283. void CEgoWindow::DelayStartPing()
  284. {
  285. _delayThread = std::thread(&CEgoWindow::StartPing, this);
  286. _delayThread.detach();
  287. }
  288. */
  289. void CEgoWindow::OnIdle()
  290. {
  291. if (!_ping) return;
  292. if (_pos==RenderPosition::FRONT)
  293. {
  294. int64_t tick=GetTickCount64();
  295. //int64_t tick = _client->;
  296. RemoNet::CCPing Req;
  297. Req.set_tick(tick);
  298. CIOBuffer Buffer;
  299. MessageHead Head;
  300. Head.Command = RemoNet::CC_Ping;
  301. Head.Length = Req.ByteSizeLong();
  302. Head.Serialize(Buffer.Buffer);
  303. auto ptr = Buffer.Buffer + MessageHead::Size();
  304. Req.SerializeToArray(ptr, Head.Length);
  305. Buffer.Length = Head.Length + MessageHead::Size();
  306. //�˴�����ping����
  307. /*if (_peerconnection != nullptr && _peerconnection->bReadyChannel)
  308. _peerconnection->SendData(Buffer);*/
  309. //m_udpcan.WritePing(&Buffer);
  310. _client->_udpcan->Write(Buffer.Buffer, Buffer.Length);
  311. }
  312. }
  313. void CEgoWindow::WriteCan(CIOBuffer *pBuffer)
  314. {
  315. _client->_udpcan->Write(pBuffer->Buffer, pBuffer->Length);
  316. }
  317. void CEgoWindow::WriteStatue(TPCANMsg* CANMsg)
  318. {
  319. _client->SendCanDate(CANMsg);
  320. }
  321. void CEgoWindow::DisplayStatue(FeedData data)
  322. {
  323. _client->OnFeedPage(data);
  324. }
  325. void CEgoWindow::DisplayPing(int32_t temp, double temp1)
  326. {
  327. _client->OnPingValue(temp, temp1);
  328. }
  329. void CEgoWindow::OnAskPing()
  330. {
  331. Sleep(300);
  332. _control = ControlState::Process;
  333. _ping = true;
  334. }
  335. void CEgoWindow::StopPing()
  336. {
  337. _ping = false;
  338. }
  339. void CEgoWindow::SetControlState(ControlState state)
  340. {
  341. _control = state;
  342. }
  343. void CEgoWindow::OnAskDataChannel()
  344. {
  345. Sleep(100);
  346. if (_peerconnection == nullptr) return;
  347. //_peerconnection->SetDataReady();
  348. RemoNet::CCAskDataChannel Req;
  349. CIOBuffer Buffer;
  350. MessageHead Head;
  351. Head.Command = RemoNet::CC_ASKDATACHANNEL;
  352. Head.Length = Req.ByteSizeLong();
  353. Head.Serialize(Buffer.Buffer);
  354. auto ptr = Buffer.Buffer + MessageHead::Size();
  355. Req.SerializeToArray(ptr, Head.Length);
  356. Buffer.Length = Head.Length + MessageHead::Size();
  357. //�˴�����ping����
  358. if (_peerconnection != nullptr && _peerconnection->bReadyChannel)
  359. _peerconnection->SendData(Buffer);
  360. }
  361. /*
  362. void CEgoWindow::DelayNextVideoReq()
  363. {
  364. _delayThread = std::thread(&CEgoWindow::AskVideoReq, this);
  365. _delayThread.detach();
  366. }
  367. */
  368. void CEgoWindow::OnAskVideoReq()
  369. {
  370. Sleep(200);
  371. _client->GetSocketClient()->WriteVideoReq(_peer, _pos);
  372. }
  373. /*
  374. void CEgoWindow::CreateVideoReq()
  375. {
  376. _client->GetSocketClient()->WriteVideoReq(_peer, _pos);
  377. }
  378. */
  379. void CEgoWindow::SetPeer(int32_t peer)
  380. {
  381. _peer = peer;
  382. }
  383. void CEgoWindow::SendData(CIOBuffer& pBuffer)
  384. {
  385. if(_peerconnection!=nullptr)
  386. _peerconnection->SendData(pBuffer);
  387. }
  388. CEgoClient* CEgoWindow::GetEgoClient()
  389. {
  390. return _client;
  391. }
  392. ControlState CEgoWindow::GetControlState()
  393. {
  394. return _control;;
  395. }
  396. void CEgoWindow::DisplayRadar(int32_t radar0, int32_t radar1, int32_t radar2, int32_t radar3, int32_t radar4, int32_t radar5, int32_t radar6, int32_t radar7, int32_t radar8)
  397. {
  398. _client->OnRadarData(radar0, radar1, radar2, radar3, radar4, radar5, radar6, radar7, radar8);
  399. }