peer_connection.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <assert.h>
  2. #include "api/video/video_frame.h"
  3. #include "remote.pb.h"
  4. #include "peer_connection.h"
  5. #include "socket_remote.h"
  6. CPeerConnection::CPeerConnection(SocketRemote* c):_client(c)
  7. {
  8. }
  9. CPeerConnection::~CPeerConnection()
  10. {
  11. if (_nativeHandle != nullptr)
  12. {
  13. }
  14. }
  15. bool CPeerConnection::CreateOffer()
  16. {
  17. // std::cout<<__FUNCTION__<<","<<__LINE__<<std::endl;
  18. auto ret = mrsPeerConnectionCreateOffer(_nativeHandle);// == mrsResult::kSuccess;
  19. assert(ret == true);
  20. return true;
  21. }
  22. bool CPeerConnection::CreateAnswer()
  23. {
  24. //std::cout<<__FUNCTION__<<","<<__LINE__<<std::endl;
  25. auto ret = mrsPeerConnectionCreateAnswer(_nativeHandle); ;
  26. assert(ret == true);
  27. return true;
  28. }
  29. void CPeerConnection::AddIceCandidate(const char* sdp, int32_t sdp_mline_index, const char* sdp_mid)
  30. {
  31. auto ret = mrsPeerConnectionAddIceCandidate(_nativeHandle, sdp, sdp_mline_index, sdp_mid);
  32. assert(ret == true);
  33. }
  34. void CPeerConnection::SetRemoteDescription(const char* type, std::string sdp)
  35. {
  36. //std::string newsdp = ForceSdpCodecs(sdp, PreferredAudioCodec, PreferredAudioCodecExtraParamsLocal, PreferredVideoCodec, PreferredVideoCodecExtraParamsLocal);
  37. auto ret = mrsPeerConnectionSetRemoteDescription(_nativeHandle, type, sdp.c_str());
  38. assert(ret == true);
  39. }
  40. void CPeerConnection::AddRemoteVideoFrameReady(PeerConnectionVideoFrameCallback fn, void* user_data)
  41. {
  42. mrsPeerConnectionRegisterRemoteVideoFrameCallback(_nativeHandle, fn, user_data);
  43. }
  44. void CPeerConnection::AddLocalVideoTrack(IOverlayVideo* render)
  45. {
  46. //PeerConnectionHandle peerHandle, CaptureType type, int32_t fps)
  47. auto ret = mrsPeerConnectionAddLocalVideoTrack(_nativeHandle, render);
  48. //assert(ret);
  49. }
  50. IOverlayVideo* CPeerConnection::CreateOverlayVideo()
  51. {
  52. auto ret = mrsPeerConnectionCreateOverlayVideo();
  53. assert(ret != nullptr);
  54. return ret;
  55. }
  56. void CPeerConnection::Initialize(int32_t peer, int32_t index)
  57. {
  58. _nativeHandle = mrsPeerConnectionCreate();
  59. if (_nativeHandle == nullptr)
  60. {
  61. // mrsPeerConnectionClose(&_nativeHandle);
  62. // _nativeHandle = nullptr;
  63. return;
  64. }
  65. //mrsPeerConnectionRegisterConnectedCallback(_nativeHandle, &CPeerConnection::ConnectionCallBack, this);
  66. mrsPeerConnectionRegisterLocalSdpReadytoSendCallback(_nativeHandle, peer, index, &CPeerConnection::LocalSdpReadytoSendCallback, this);
  67. mrsPeerConnectionRegisterIceCandidateReadytoSendCallback(_nativeHandle, peer, index, &CPeerConnection::IceCandidateReadytoSendCallback, this);
  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::IceCandidateReadytoSendCallback(void* user_data, int32_t peer, int32_t index, const char* sdp, int32_t sdp_mline_index, const char* sdp_mid)
  91. {
  92. auto lhs = static_cast<CPeerConnection*>(user_data);
  93. lhs->OnIceCandidateReadytoSend(peer, index, sdp, sdp_mline_index, sdp_mid);
  94. }
  95. void CPeerConnection::OnIceCandidateReadytoSend(int32_t peer, int32_t index, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid)
  96. {
  97. _client->WriteCandidate(peer, index, candidate, sdp_mline_index, sdp_mid);
  98. }
  99. void CPeerConnection::Close()
  100. {
  101. //std::lock_guard<std::mutex> l(_lock);
  102. if (_nativeHandle == nullptr) return;
  103. mrsPeerConnectionRegisterLocalSdpReadytoSendCallback(_nativeHandle, 0, 0, nullptr, nullptr);
  104. mrsPeerConnectionRegisterIceCandidateReadytoSendCallback(_nativeHandle, 0, 0, nullptr, nullptr);
  105. mrsPeerConnectionClose(&_nativeHandle);
  106. _nativeHandle = nullptr;
  107. }