#include #include #include #include "../common/comm.h" #include "api.h" #include "protocol.pb.h" #include "../common/socket_client.h" #include "../common/iobuffer.h" #include "peer_connection.h" //#include "VideoRenderer.h" //#include "message_queue.h" std::mutex CPeerConnection::_lock; CPeerConnection::CPeerConnection(ChannelType type, SocketClient* d) :_client(d),_type(type) { _nativeHandle = nullptr; bReadyChannel=false; } CPeerConnection::~CPeerConnection() { if (_nativeHandle != nullptr) { } } void CPeerConnection::Initialize(int32_t peer,int32_t index,int32_t MinPort,int32_t MaxPort) { //20231023 //std::lock_guard l(_lock); _nativeHandle = mrsPeerConnectionCreate(MinPort,MaxPort); std::cout<<"native handle"<<_nativeHandle< l(_lock); if(_nativeHandle==nullptr) return; bReadyChannel = false; mrsPeerConnectionRegisterConnectedCallback(_nativeHandle, nullptr, nullptr); mrsPeerConnectionRegisterLocalSdpReadytoSendCallback(_nativeHandle,0,0, nullptr, nullptr); mrsPeerConnectionRegisterIceCandidateReadytoSendCallback(_nativeHandle,0, 0,nullptr, nullptr); //mrsPeerConnectionRemoveDataChannel(_nativeHandle); mrsPeerConnectionClose(&_nativeHandle); _nativeHandle=nullptr; } void CPeerConnection::AddDataChannel(bool ordered, bool reliable) { mrsPeerConnectionAddDataChannel(_nativeHandle, ordered, reliable); } void CPeerConnection::RemoveDataChannel() { mrsPeerConnectionRemoveDataChannel(_nativeHandle); } void CPeerConnection::ConnectionCallBack(void* UserData) { CPeerConnection* lhs = static_cast(UserData); lhs->OnConnect(); } void CPeerConnection::OnConnect() { bConnected = true; } 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(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<WriteOffer(peer,view, type, sdp); } else if(kAnswer==type) { _client->WriteAnswer(peer,view, type, sdp); } //mrsWe } 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::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(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::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); } bool CPeerConnection::CreateOffer() { // std::cout<<__FUNCTION__<<","<<__LINE__<(user_data); lhs->OnPeerMessage(data,size); } void CPeerConnection::OnPeerMessage(const void * data,const int32_t size) { if(sizeOnPeerMessage(_type,Head.Command,Head.Length,ptr); } void CPeerConnection::SendData(CIOBuffer& pBuffer) { if(!bReadyChannel) return; auto ret=mrsPeerConnectionSendDataChannelMessage(_nativeHandle,pBuffer.Buffer,pBuffer.Length); if(ret==false) { printf("send failed to Peer \n"); } }