123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #include <assert.h>
- #include "api/video/video_frame.h"
- #include "remote.pb.h"
- #include "peer_connection.h"
- #include "socket_remote.h"
- CPeerConnection::CPeerConnection(SocketRemote* c):_client(c)
- {
- }
- CPeerConnection::~CPeerConnection()
- {
- if (_nativeHandle != nullptr)
- {
- }
- }
- bool CPeerConnection::CreateOffer()
- {
- // std::cout<<__FUNCTION__<<","<<__LINE__<<std::endl;
- auto ret = mrsPeerConnectionCreateOffer(_nativeHandle);// == mrsResult::kSuccess;
- assert(ret == true);
- return true;
- }
- bool CPeerConnection::CreateAnswer()
- {
- //std::cout<<__FUNCTION__<<","<<__LINE__<<std::endl;
- auto ret = mrsPeerConnectionCreateAnswer(_nativeHandle); ;
- assert(ret == true);
- return true;
- }
- void CPeerConnection::AddIceCandidate(const char* sdp, int32_t sdp_mline_index, const char* sdp_mid)
- {
- auto ret = mrsPeerConnectionAddIceCandidate(_nativeHandle, sdp, sdp_mline_index, sdp_mid);
- assert(ret == true);
- }
- void CPeerConnection::SetRemoteDescription(const char* type, std::string sdp)
- {
- //std::string newsdp = ForceSdpCodecs(sdp, PreferredAudioCodec, PreferredAudioCodecExtraParamsLocal, PreferredVideoCodec, PreferredVideoCodecExtraParamsLocal);
- auto ret = mrsPeerConnectionSetRemoteDescription(_nativeHandle, type, sdp.c_str());
- assert(ret == true);
- }
- void CPeerConnection::AddRemoteVideoFrameReady(PeerConnectionVideoFrameCallback fn, void* user_data)
- {
- mrsPeerConnectionRegisterRemoteVideoFrameCallback(_nativeHandle, fn, user_data);
- }
- void CPeerConnection::AddLocalVideoTrack(IOverlayVideo* render)
- {
- //PeerConnectionHandle peerHandle, CaptureType type, int32_t fps)
- auto ret = mrsPeerConnectionAddLocalVideoTrack(_nativeHandle, render);
- //assert(ret);
- }
- IOverlayVideo* CPeerConnection::CreateOverlayVideo()
- {
- auto ret = mrsPeerConnectionCreateOverlayVideo();
- assert(ret != nullptr);
- return ret;
- }
- void CPeerConnection::Initialize(int32_t peer, int32_t index)
- {
- _nativeHandle = mrsPeerConnectionCreate();
-
- if (_nativeHandle == nullptr)
- {
- // mrsPeerConnectionClose(&_nativeHandle);
- // _nativeHandle = nullptr;
- return;
- }
- //mrsPeerConnectionRegisterConnectedCallback(_nativeHandle, &CPeerConnection::ConnectionCallBack, this);
- mrsPeerConnectionRegisterLocalSdpReadytoSendCallback(_nativeHandle, peer, index, &CPeerConnection::LocalSdpReadytoSendCallback, this);
- mrsPeerConnectionRegisterIceCandidateReadytoSendCallback(_nativeHandle, peer, index, &CPeerConnection::IceCandidateReadytoSendCallback, this);
-
- }
- const std::string kOffer = "offer";
- const std::string kAnswer = "answer";
- void CPeerConnection::LocalSdpReadytoSendCallback(void* user_data, int32_t peer, int32_t view, const char* type, const char* sdp_data)
- {
- auto lhs = static_cast<CPeerConnection*>(user_data);
- lhs->OnLocalSdpReadytoSend(peer, view, type, sdp_data);
- }
- void CPeerConnection::OnLocalSdpReadytoSend(int32_t peer, int32_t view, const char* type, const char* sdp)
- {
- //std::string newsdp = ForceSdpCodecs(sdp, PreferredAudioCodec, PreferredAudioCodecExtraParamsRemote, PreferredVideoCodec, PreferredVideoCodecExtraParamsRemote);
- //std::cout<<type<<","<<__LINE__<<std::endl;
- if (kOffer == type)
- {
- _client->WriteOffer(peer, view, type, sdp);
- }
- else if (kAnswer == type)
- {
- _client->WriteAnswer(peer, view, type, sdp);
- }
- //mrsWe
- }
- void CPeerConnection::IceCandidateReadytoSendCallback(void* user_data, int32_t peer, int32_t index, const char* sdp, int32_t sdp_mline_index, const char* sdp_mid)
- {
- auto lhs = static_cast<CPeerConnection*>(user_data);
- lhs->OnIceCandidateReadytoSend(peer, index, sdp, sdp_mline_index, sdp_mid);
- }
- void CPeerConnection::OnIceCandidateReadytoSend(int32_t peer, int32_t index, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid)
- {
- _client->WriteCandidate(peer, index, candidate, sdp_mline_index, sdp_mid);
- }
- void CPeerConnection::Close()
- {
- //std::lock_guard<std::mutex> l(_lock);
- if (_nativeHandle == nullptr) return;
-
-
- mrsPeerConnectionRegisterLocalSdpReadytoSendCallback(_nativeHandle, 0, 0, nullptr, nullptr);
- mrsPeerConnectionRegisterIceCandidateReadytoSendCallback(_nativeHandle, 0, 0, nullptr, nullptr);
- mrsPeerConnectionClose(&_nativeHandle);
- _nativeHandle = nullptr;
- }
|