peer_connection.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include <assert.h>
  2. #include <iostream>
  3. #include <memory>
  4. #include "../common/comm.h"
  5. #include "api.h"
  6. #include "protocol.pb.h"
  7. #include "../common/socket_client.h"
  8. #include "../common/iobuffer.h"
  9. #include "peer_connection.h"
  10. //#include "VideoRenderer.h"
  11. //#include "message_queue.h"
  12. std::mutex CPeerConnection::_lock;
  13. CPeerConnection::CPeerConnection(ChannelType type, SocketClient* d) :_client(d),_type(type)
  14. {
  15. _nativeHandle = nullptr;
  16. bReadyChannel=false;
  17. }
  18. CPeerConnection::~CPeerConnection()
  19. {
  20. if (_nativeHandle != nullptr)
  21. {
  22. }
  23. }
  24. void CPeerConnection::Initialize(int32_t peer,int32_t index,int32_t MinPort,int32_t MaxPort)
  25. {
  26. //20231023
  27. //std::lock_guard<std::mutex> l(_lock);
  28. _nativeHandle = mrsPeerConnectionCreate(MinPort,MaxPort);
  29. std::cout<<"native handle"<<_nativeHandle<<std::endl;
  30. if (_nativeHandle == nullptr)
  31. {
  32. // mrsPeerConnectionClose(&_nativeHandle);
  33. // _nativeHandle = nullptr;
  34. return;
  35. }
  36. mrsPeerConnectionRegisterConnectedCallback(_nativeHandle, &CPeerConnection::ConnectionCallBack, this);
  37. mrsPeerConnectionRegisterLocalSdpReadytoSendCallback(_nativeHandle,peer,index, &CPeerConnection::LocalSdpReadytoSendCallback, this);
  38. mrsPeerConnectionRegisterIceCandidateReadytoSendCallback(_nativeHandle,peer,index, &CPeerConnection::IceCandidateReadytoSendCallback, this);
  39. mrsPeerConnectionRegisterChannelCallback(_nativeHandle, &CPeerConnection::MessageCallback, this, nullptr, nullptr, nullptr, nullptr);
  40. }
  41. void CPeerConnection::Close()
  42. {
  43. //20231023
  44. //std::lock_guard<std::mutex> l(_lock);
  45. if(_nativeHandle==nullptr) return;
  46. bReadyChannel = false;
  47. mrsPeerConnectionRegisterConnectedCallback(_nativeHandle, nullptr, nullptr);
  48. mrsPeerConnectionRegisterLocalSdpReadytoSendCallback(_nativeHandle,0,0, nullptr, nullptr);
  49. mrsPeerConnectionRegisterIceCandidateReadytoSendCallback(_nativeHandle,0, 0,nullptr, nullptr);
  50. //mrsPeerConnectionRemoveDataChannel(_nativeHandle);
  51. mrsPeerConnectionClose(&_nativeHandle);
  52. _nativeHandle=nullptr;
  53. }
  54. void CPeerConnection::AddDataChannel(bool ordered, bool reliable)
  55. {
  56. mrsPeerConnectionAddDataChannel(_nativeHandle, ordered, reliable);
  57. }
  58. void CPeerConnection::RemoveDataChannel()
  59. {
  60. mrsPeerConnectionRemoveDataChannel(_nativeHandle);
  61. }
  62. void CPeerConnection::ConnectionCallBack(void* UserData)
  63. {
  64. CPeerConnection* lhs = static_cast<CPeerConnection*>(UserData);
  65. lhs->OnConnect();
  66. }
  67. void CPeerConnection::OnConnect()
  68. {
  69. bConnected = true;
  70. }
  71. const std::string kOffer = "offer";
  72. const std::string kAnswer = "answer";
  73. void CPeerConnection::LocalSdpReadytoSendCallback(void* user_data,int32_t peer,int32_t view, const char* type, const char* sdp_data)
  74. {
  75. auto lhs = static_cast<CPeerConnection*>(user_data);
  76. lhs->OnLocalSdpReadytoSend(peer,view,type, sdp_data);
  77. }
  78. void CPeerConnection::OnLocalSdpReadytoSend(int32_t peer,int32_t view,const char* type, const char* sdp)
  79. {
  80. //std::string newsdp = ForceSdpCodecs(sdp, PreferredAudioCodec, PreferredAudioCodecExtraParamsRemote, PreferredVideoCodec, PreferredVideoCodecExtraParamsRemote);
  81. //std::cout<<type<<","<<__LINE__<<std::endl;
  82. if (kOffer == type)
  83. {
  84. _client->WriteOffer(peer,view, type, sdp);
  85. }
  86. else if(kAnswer==type)
  87. {
  88. _client->WriteAnswer(peer,view, type, sdp);
  89. }
  90. //mrsWe
  91. }
  92. void CPeerConnection::SetRemoteDescription(const char* type, std::string sdp)
  93. {
  94. //std::string newsdp = ForceSdpCodecs(sdp, PreferredAudioCodec, PreferredAudioCodecExtraParamsLocal, PreferredVideoCodec, PreferredVideoCodecExtraParamsLocal);
  95. auto ret = mrsPeerConnectionSetRemoteDescription(_nativeHandle, type, sdp.c_str());
  96. assert(ret == true);
  97. }
  98. void CPeerConnection::IceCandidateReadytoSendCallback(void* user_data,int32_t peer,int32_t index, const char* sdp, int32_t sdp_mline_index, const char* sdp_mid)
  99. {
  100. auto lhs = static_cast<CPeerConnection*>(user_data);
  101. lhs->OnIceCandidateReadytoSend(peer,index,sdp, sdp_mline_index, sdp_mid);
  102. }
  103. void CPeerConnection::OnIceCandidateReadytoSend(int32_t peer,int32_t index,const char* candidate, int32_t sdp_mline_index, const char* sdp_mid)
  104. {
  105. _client->WriteCandidate(peer,index, candidate, sdp_mline_index, sdp_mid);
  106. }
  107. void CPeerConnection::AddIceCandidate(const char* sdp, int32_t sdp_mline_index, const char* sdp_mid)
  108. {
  109. auto ret = mrsPeerConnectionAddIceCandidate(_nativeHandle, sdp, sdp_mline_index, sdp_mid);
  110. assert(ret == true);
  111. }
  112. bool CPeerConnection::CreateOffer()
  113. {
  114. // std::cout<<__FUNCTION__<<","<<__LINE__<<std::endl;
  115. auto ret = mrsPeerConnectionCreateOffer(_nativeHandle);// == mrsResult::kSuccess;
  116. assert(ret == true);
  117. return true;
  118. }
  119. bool CPeerConnection::CreateAnswer()
  120. {
  121. //std::cout<<__FUNCTION__<<","<<__LINE__<<std::endl;
  122. auto ret = mrsPeerConnectionCreateAnswer(_nativeHandle); ;
  123. assert(ret == true);
  124. return true;
  125. }
  126. void CPeerConnection::AddLocalArgb32VideoFrameReady(PeerConnectionARGBVideoFrameCallback fn, void* user_data)
  127. {
  128. mrsPeerConnectionRegisterARGBLocalVideoFrameCallback(_nativeHandle, fn, user_data);
  129. }
  130. void CPeerConnection::AddLocalAudioTrack()
  131. {
  132. mrsPeerConnectionAddLocalAudioTrack(_nativeHandle);
  133. }
  134. void CPeerConnection::RemoveLocalAudioTrack()
  135. {
  136. mrsPeerConnectionRemoveLocalAudioTrack(_nativeHandle);
  137. }
  138. void CPeerConnection::AddRemoteArgb32VideoFrameReady(PeerConnectionARGBVideoFrameCallback fn, void* user_data)
  139. {
  140. mrsPeerConnectionRegisterARGBRemoteVideoFrameCallback(_nativeHandle, fn, user_data);
  141. }
  142. void CPeerConnection::SetGsml(int64_t time,int64_t StartRecord,int64_t DeviceId)
  143. {
  144. if(_nativeHandle!=nullptr)
  145. mrsPeerConnectionSetGsml(_nativeHandle,time,StartRecord,DeviceId);
  146. }
  147. #ifdef WEBRTC_LINUX
  148. void CPeerConnection::AddLocalVideoTrack(RenderPosition type,int32_t index,int32_t GSML)
  149. {
  150. //PeerConnectionHandle peerHandle, CaptureType type, int32_t fps)
  151. auto ret=mrsPeerConnectionAddLocalVideoTrack(_nativeHandle, type,index,GSML);
  152. }
  153. void CPeerConnection::SwitchCapture(bool front)
  154. {
  155. if(_nativeHandle!=nullptr)
  156. mrsPeerConnectionSwitchCapture(_nativeHandle, front);
  157. }
  158. void CPeerConnection::SetOtherCtx(void * data)
  159. {
  160. mrsPeerConnectionSetCtx(_nativeHandle,data);
  161. }
  162. void * CPeerConnection::GetCurrentCtx()
  163. {
  164. return mrsPeerConnectionCurrentCtx(_nativeHandle);
  165. }
  166. #else
  167. void CPeerConnection::SetDataReady()
  168. {
  169. bReadyChannel = true;
  170. }
  171. bool CPeerConnection::IsDataReady()
  172. {
  173. return bReadyChannel;
  174. }
  175. #endif
  176. void CPeerConnection::MessageCallback(void * user_data,const void * data,const int32_t size)
  177. {
  178. CPeerConnection* lhs=static_cast<CPeerConnection*>(user_data);
  179. lhs->OnPeerMessage(data,size);
  180. }
  181. void CPeerConnection::OnPeerMessage(const void * data,const int32_t size)
  182. {
  183. if(size<MessageHead::Size()) return;
  184. MessageHead Head;
  185. int8_t* Data=(int8_t *)data;
  186. Head.Deserialize(Data);
  187. if(size<MessageHead::Size()+Head.Length) return;
  188. auto ptr=Data+MessageHead::Size();
  189. if(Head.Command==RemoNet::CC_ASKDATACHANNEL)
  190. {
  191. bReadyChannel=true;
  192. //return;
  193. }
  194. if(!bReadyChannel) return;
  195. _client->OnPeerMessage(_type,Head.Command,Head.Length,ptr);
  196. }
  197. void CPeerConnection::SendData(CIOBuffer& pBuffer)
  198. {
  199. if(!bReadyChannel) return;
  200. auto ret=mrsPeerConnectionSendDataChannelMessage(_nativeHandle,pBuffer.Buffer,pBuffer.Length);
  201. if(ret==false)
  202. {
  203. printf("send failed to Peer \n");
  204. }
  205. }