peer_connection.cpp 6.8 KB

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