peer_connection.cpp 6.8 KB

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