In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.