peer_connection.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #ifdef WEBRTC_LINUX
  143. void CPeerConnection::AddLocalVideoTrack(RenderPosition type,int32_t index)
  144. {
  145. //PeerConnectionHandle peerHandle, CaptureType type, int32_t fps)
  146. auto ret=mrsPeerConnectionAddLocalVideoTrack(_nativeHandle, type,index);
  147. }
  148. void CPeerConnection::SwitchCapture(bool front)
  149. {
  150. if(_nativeHandle!=nullptr)
  151. mrsPeerConnectionSwitchCapture(_nativeHandle, front);
  152. }
  153. void CPeerConnection::SetOtherCtx(void * data)
  154. {
  155. mrsPeerConnectionSetCtx(_nativeHandle,data);
  156. }
  157. void * CPeerConnection::GetCurrentCtx()
  158. {
  159. return mrsPeerConnectionCurrentCtx(_nativeHandle);
  160. }
  161. #else
  162. void CPeerConnection::SetDataReady()
  163. {
  164. bReadyChannel = true;
  165. }
  166. bool CPeerConnection::IsDataReady()
  167. {
  168. return bReadyChannel;
  169. }
  170. #endif
  171. void CPeerConnection::MessageCallback(void * user_data,const void * data,const int32_t size)
  172. {
  173. CPeerConnection* lhs=static_cast<CPeerConnection*>(user_data);
  174. lhs->OnPeerMessage(data,size);
  175. }
  176. void CPeerConnection::OnPeerMessage(const void * data,const int32_t size)
  177. {
  178. if(size<MessageHead::Size()) return;
  179. MessageHead Head;
  180. int8_t* Data=(int8_t *)data;
  181. Head.Deserialize(Data);
  182. if(size<MessageHead::Size()+Head.Length) return;
  183. auto ptr=Data+MessageHead::Size();
  184. if(Head.Command==RemoNet::CC_ASKDATACHANNEL)
  185. {
  186. bReadyChannel=true;
  187. //return;
  188. }
  189. if(!bReadyChannel) return;
  190. _client->OnPeerMessage(_type,Head.Command,Head.Length,ptr);
  191. }
  192. void CPeerConnection::SendData(CIOBuffer& pBuffer)
  193. {
  194. if(!bReadyChannel) return;
  195. auto ret=mrsPeerConnectionSendDataChannelMessage(_nativeHandle,pBuffer.Buffer,pBuffer.Length);
  196. if(ret==false)
  197. {
  198. printf("send failed to Peer \n");
  199. }
  200. }