Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize PeerConnection Peer-to-Peer Communication

Shulou Source: shulou.com Published: 2022-06-03 07:16:33 10月03日 Update

This article mainly explains how PeerConnection peer-to-peer communication is realized. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how PeerConnection peer-to-peer communication is realized.

Webrtc is a real-time communication technology, a very simple application of real-time communication technology in web browsers, including audio and video calls. When using webrtc technology, the browser has been basically encapsulated, as long as the corresponding api is called, a simple call can be achieved, one of the main objects is RTCPeerConnection to support audio and video media data communication.

Browser end

1. Create a webrtc peering connection method.

Let pc = new PTCPeerConnection ({iceServers: [{urls: 'stun:stun.l.google.com:19302'}]})

2. Create a stream through the browser api (enable the camera, take a screenshot of the desktop and obtain the canvas stream)

Navigator.getUserMedia = navigator.getUserMedia | | navigator.webkitGetUserMedia | | navigator.mozGetUserMedia; this is the obtained camera stream

3. Add the camera stream to the track of the webrtc stream.

Stream.getTracks.forEach (track = > pc.addTrack (track))

4. All the above steps have been created. The next step is to create offer and answer to exchange data with the acquired sdp through the server, and then transfer the ICE information. Finally, listen to the method ontrack in PTCPeerConnection to get the stream. If disconnected, you can also listen to the onconnectionstatechange method to get the status of disconnection and error.

C++ end

C++ side is much more complex than js side, but the process is roughly the same, slightly more complex.

1. First create a factory for peering connection

Peer_connection_factory_ = webrtc::CreatePeerConnectionFactory (nullptr / * network_thread * /, nullptr / * worker_thread * /, nullptr / * signaling_thread * /, nullptr / * default_adm * /, webrtc::CreateBuiltinAudioEncoderFactory (), webrtc::CreateBuiltinAudioDecoderFactory (), webrtc::CreateBuiltinVideoEncoderFactory (), webrtc::CreateBuiltinVideoDecoderFactory (), nullptr / * audio_mixer * /, nullptr / * audio_processing * /)

2. Create an instance of peering connection through peering connection project

Webrtc::PeerConnectionInterface::RTCConfiguration config; config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan; config.enable_dtls_srtp = dtls; webrtc::PeerConnectionInterface::IceServer server; server.uri = GetPeerConnectionString (); config.servers.push_back (server); peer_connection_ = peer_connection_factory_- > CreatePeerConnection (config, nullptr, nullptr, this)

3. Obtain video and audio streams and add them to the AddTrack method in peer_connection_.

Video:

Rtc::scoped_refptr video_device = CapturerTrackSource::Create (); if (video_device) {rtc::scoped_refptr video_track_ (peer_connection_factory_- > CreateVideoTrack (kVideoLabel, video_device)); auto result_or_error2 = peer_connection_- > AddTrack (video_track_, {kStreamId})

Audio:

Rtc::scoped_refptr audio_track (peer_connection_factory_- > CreateAudioTrack (kAudioLabel, peer_connection_factory_- > CreateAudioSource (cricket::AudioOptions (); auto result_or_error = peer_connection_- > AddTrack (audio_track, {kStreamId})

4. The initiator creates an offer, and the remote end creates an answer to exchange sdp information, listens to the OnIceCandidate method and passes the ice, so that both the local and the remote end can communicate in real time.

Thank you for your reading. the above is the content of "how PeerConnection peer-to-peer communication is realized". After the study of this article, I believe you have a deeper understanding of how PeerConnection peer-to-peer communication is realized, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Peer-to-peer communication method browser browsing real-time technology camera video audio learning camera surveillance complexity information content that is data communication C++ Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Xiaomi Shulou Tech Info Docker NVidia