An in-depth explanation of WebRTC: revealing the black technology of real-time communication

Preface

Hey, fellow coders! Have you ever lamented the black technology in the field of real-time communication? Have you ever thought about implementing magical functions like video conferencing and voice chat on the web? Don't worry, today we are going to uncover the mystery of this black box and explore the wonderful world of WebRTC in a simple and easy-to-understand way.

1. A preliminary study on WebRTC

First of all, we need to understand what WebRTC is. WebRTC, full name Web Real-Time Communication, is an open source project led by Google, dedicated to providing real-time communication in the browser ability. Simply put, it is to enable your browser to transmit audio, video and data in real time like traditional communication tools.

We all know that real-time communication is not as simple as drinking water. The bottom layer must have some fancy technical support. WebRTCMainly includes three core modules: media capture (Media Capture), real-time transmission (Real-Time Transport) and network establishment (Network Establishment). Sounds pretty fancy, don’t worry, we’ll unveil their mysteries one by one.

2. Media Capture

Media capture, as the name suggests, is to capture audio and video from your device and then send it to the communication stage. This stage is your browser. Don’t think about it too complicated. It’s just a “camera switch” and a “microphone mixer”.

Now, let's look at a simple sample code to feel the charm of media capture:

navigator.mediaDevices.getUserMedia({
    
     video: true, audio: true })
  .then((stream) => {
    
    
    // 成功获取媒体流
    const videoElement = document.getElementById('local-video');
    videoElement.srcObject = stream;
  })
  .catch((error) => {
    
    
    console.error('媒体获取失败,原因是:', error);
  });

The above code requests the user's media device through the getUserMedia method, and then displays the media stream in the local-video element on the page. It's as simple as teasing the camera and microphone.

  1. Real-time transmission
    Media capture is only the first step in the entire communication process. The next step is to transmit the captured audio and video data to the other party. This is what real-time transmission does. matter. In WebRTC, we usually use RTP (Real-Time Protocol) to transmit real-time data. Don’t be scared by this term. It is actually a transmission protocol specially designed for real-time communication to ensure that your voice will not be lost halfway and the video will not suddenly turn into a mosaic.

Let's look at a piece of code again to feel the magic of real-time transmission:

const peerConnection = new RTCPeerConnection();

// 假设remoteStream是远程用户的媒体流
peerConnection.addStream(remoteStream);

// 发送ICE候选(网络信息)
peerConnection.onicecandidate = (event) => {
    
    
  if (event.candidate) {
    
    
    sendIceCandidateToRemote(event.candidate);
  }
};

// 假设localStream是本地用户的媒体流
peerConnection.createOffer()
  .then((offer) => peerConnection.setLocalDescription(offer))
  .then(() => sendOfferToRemote(peerConnection.localDescription))
  .catch((error) => console.error('创建Offer失败,原因是:', error));

The above code creates anRTCPeerConnection object for handling real-time communication connections. Add the media stream of the remote user through the addStream method, then create an Offer for establishing the connection through the createOffer method, and finally pass the signaling channel (here we assume that there is already The two methods sendIceCandidateToRemote and sendOfferToRemote are used) to pass the information to the other party.

4. Network establishment

Okay, the two links of media capture and real-time transmission have been connected, but you have to know that the network is not laid out, there must be a process, and this is what the network establishment is about. In WebRTC, network establishment mainly includes obtaining network candidates through the ICE protocol, exchanging media information through the signaling server, etc.

// 在本地端
peerConnection.onicecandidate = (event) => {
    
    
  if (event.candidate) {
    
    
    sendIceCandidateToRemote(event.candidate);
  }
};

// 在远程端
function handleReceivedIceCandidate(candidate) {
    
    
  peerConnection.addIceCandidate(new RTCIceCandidate(candidate))
    .catch((error) => console.error('处理ICE候选失败,原因是:', error));
}

The above code shows how to generate ICE candidates on the local side and then pass them to the remote side through the signaling channel. After the remote end receives the ICE candidate, it uses the addIceCandidate method to add the candidate, thereby establishing a network connection. It's like two people exchanging business cards, except that the business card contains network address information.

5. Example: Create a simple video chat application

Okay, let’s get down to business, let’s use a simple example to connect the previous knowledge points and create our ownWebRTC video chat application.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>WebRTC视频聊天</title>
</head>
<body>
  <video id="local-video" autoplay></video>
  <video id="remote-video" autoplay></video>

  <script>
    const localVideo = document.getElementById('local-video');
    const remoteVideo = document.getElementById('remote-video');
    
    navigator.mediaDevices.getUserMedia({
      
       video: true, audio: true })
      .then((stream) => {
      
      
        localVideo.srcObject = stream;

        const peerConnection = new RTCPeerConnection();
        peerConnection.addStream(stream);

        peerConnection.onicecandidate = (event) => {
      
      
          if (event.candidate) {
      
      
            // 将ICE候选发送给远程端
            sendIceCandidateToRemote(event.candidate);
          }
        };

        // 接收远程的媒体流
        peerConnection.onaddstream = (event) => {
      
      
          remoteVideo.srcObject = event.stream;
        };

        // 创建Offer并发送给远程端
        peerConnection.createOffer()
          .then((offer) => peerConnection.setLocalDescription(offer))
          .then(() => sendOfferToRemote(peerConnection.localDescription))
          .catch((error) => console.error('创建Offer失败,原因是:', error));
      })
      .catch((error) => {
      
      
        console.error('媒体获取失败,原因是:', error);
      });

    // 处理远程端发送过来的ICE候选
    function handleReceivedIceCandidate(candidate) {
      
      
      peerConnection.addIceCandidate(new RTCIceCandidate(candidate))
        .catch((error) => console.error('处理ICE候选失败,原因是:', error));
    }

    // 处理远程端发送过来的Offer
    function handleReceivedOffer(offer) {
      
      
      peerConnection.setRemoteDescription(new RTCSessionDescription(offer))
        .then(() => peerConnection.createAnswer())
        .then((answer) => peerConnection.setLocalDescription(answer))
        .then(() => sendAnswerToRemote(peerConnection.localDescription))
        .catch((error) => console.error('处理Offer失败,原因是:', error));
    }

    // 处理远程端发送过来的Answer
    function handleReceivedAnswer(answer) {
      
      
      peerConnection.setRemoteDescription(new RTCSessionDescription(answer))
        .catch((error) => console.error('处理Answer失败,原因是:', error));
    }

    // 假设以下方法已实现,用于通过信令通道发送和接收Offer、Answer、ICE候选
    function sendOfferToRemote(offer) {
      
       /* 实现略 */ }
    function sendAnswerToRemote(answer) {
      
       /* 实现略 */ }
    function sendIceCandidateToRemote(candidate) {
      
       /* 实现略 */ }
    function receiveOfferFromRemote(offer) {
      
       handleReceivedOffer(offer); }
    function receiveAnswerFromRemote(answer) {
      
       handleReceivedAnswer(answer); }
    function receiveIceCandidateFromRemote(candidate) {
      
       handleReceivedIceCandidate(candidate); }
  </script>
</body>
</html>

The above sample code shows how to implement a simple video chat application in the browser. Of course, the signaling channel related methods here are not implemented, you may need to use WebSocket or other communication means to complete.

6. Summary

Through the in-depth explanation of this article, I believe you have gained a deeper understanding ofWebRTC. The three links of media capture, real-time transmission, and network establishment are like the three pillars of communication. Although it is a bit complicated, as long as you follow it step by step, you can master this black technologyWebRTC.

In actual applications, you may also need to consider issues such as the establishment of signaling servers and security, but this is beyond our scope today. I hope this article can openWebRTCthis mysterious door for you, allowing you to swim freely in the field of real-time communication.

Okay, friends, it’s time to put yourWebRTC skills into your pocket and create your own real-time communication miracle! Happy coding!

Guess you like

Origin blog.csdn.net/qq_29669259/article/details/134812445