使用 WebRtcStreamer 实现实时视频流播放

WebRtcStreamer 是一个基于 WebRTC 协议的轻量级开源工具,可以在浏览器中直接播放 RTSP 视频流。它利用 WebRTC 的强大功能,提供低延迟的视频流播放体验,非常适合实时监控和其他视频流应用场景。

本文将介绍如何在Vue.js项目中使用 WebRtcStreamer 实现实时视频流播放,并分享相关的代码示例。

注意:只支持H264格式

流媒体方式文章
使用 Vue 和 flv.js 实现流媒体视频播放:完整教程
VUE项目中优雅使用EasyPlayer实时播放摄像头多种格式视频使用版本信息为5.xxxx

实现步骤

  • 安装和配置 WebRtcStreamer 服务端
    要使用 WebRtcStreamer,需要先在服务器上部署其服务端。以下是基本的安装步骤:

  • WebRtcStreamer 官方仓库 下载代码。
    在这里插入图片描述
    启动命令
    在这里插入图片描述
    或者双击exe程序
    在这里插入图片描述
    服务启动后,默认会监听 8000 端口,访问 http://<server_ip>:8000 可查看状态。
    在这里插入图片描述
    更改默认端口命令:webrtc-streamer.exe -o -H 0.0.0.0:9527

2.集成到vue中
webRtcStreamer.js 不需要在html文件中引入webRtcStreamer相关代码

/**
 * @constructor
 * @param {string} videoElement -  dom ID
 * @param {string} srvurl -  WebRTC 流媒体服务器的 URL(默认为当前页面地址)
 */
class WebRtcStreamer {
  constructor(videoElement, srvurl) {
    if (typeof videoElement === 'string') {
      this.videoElement = document.getElementById(videoElement);
    } else {
      this.videoElement = videoElement;
    }
    this.srvurl =
      srvurl || `${location.protocol}//${window.location.hostname}:${window.location.port}`;
    this.pc = null; // PeerConnection 实例

    // 媒体约束条件
    this.mediaConstraints = {
      offerToReceiveAudio: true,
      offerToReceiveVideo: true,
    };

    this.iceServers = null; // ICE 服务器配置
    this.earlyCandidates = []; // 提前收集的候选者
  }

  /**
   * HTTP 错误处理器
   * @param {Response} response - HTTP 响应
   * @throws {Error} 当响应不成功时抛出错误
   */
  _handleHttpErrors(response) {
    if (!response.ok) {
      throw Error(response.statusText);
    }
    return response;
  }

  /**
   * 连接 WebRTC 视频流到指定的 videoElement
   * @param {string} videourl - 视频流 URL
   * @param {string} audiourl - 音频流 URL
   * @param {string} options - WebRTC 通话的选项
   * @param {MediaStream} localstream - 本地流
   * @param {string} prefmime - 优先的 MIME 类型
   */
  connect(videourl, audiourl, options, localstream, prefmime) {
    this.disconnect();

    if (!this.iceServers) {
      console.log('获取 ICE 服务器配置...');

      fetch(`${this.srvurl}/api/getIceServers`)
        .then(this._handleHttpErrors)
        .then((response) => response.json())
        .then((response) =>
          this.onReceiveGetIceServers(response, videourl, audiourl, options, localstream, prefmime),
        )
        .catch((error) => this.onError(`获取 ICE 服务器错误: ${error}`));
    } else {
      this.onReceiveGetIceServers(
        this.iceServers,
        videourl,
        audiourl,
        options,
        localstream,
        prefmime,
      );
    }
  }

  /**
   * 断开 WebRTC 视频流,并清空 videoElement 的视频源
   */
  disconnect() {
    if (this.videoElement?.srcObject) {
      this.videoElement.srcObject.getTracks().forEach((track) => {
        track.stop();
        this.videoElement.srcObject.removeTrack(track);
      });
    }
    if (this.pc) {
      fetch(`${this.srvurl}/api/hangup?peerid=${this.pc.peerid}`)
        .then(this._handleHttpErrors)
        .catch((error) => this.onError(`hangup ${error}`));

      try {
        this.pc.close();
      } catch (e) {
        console.log(`Failure close peer connection: ${e}`);
      }
      this.pc = null;
    }
  }

  /**
   * 获取 ICE 服务器配置的回调
   * @param {Object} iceServers - ICE 服务器配置
   * @param {string} videourl - 视频流 URL
   * @param {string} audiourl - 音频流 URL
   * @param {string} options - WebRTC 通话的选项
   * @param {MediaStream} stream - 本地流
   * @param {string} prefmime - 优先的 MIME 类型
   */
  onReceiveGetIceServers(iceServers, videourl, audiourl, options, stream, prefmime) {
    this.iceServers = iceServers;
    this.pcConfig = iceServers || { iceServers: [] };
    try {
      this.createPeerConnection();

      let callurl = `${this.srvurl}/api/call?peerid=${this.pc.peerid}&url=${encodeURIComponent(
        videourl,
      )}`;
      if (audiourl) {
        callurl += `&audiourl=${encodeURIComponent(audiourl)}`;
      }
      if (options) {
        callurl += `&options=${encodeURIComponent(options)}`;
      }

      if (stream) {
        this.pc.addStream(stream);
      }

      this.earlyCandidates.length = 0;

      this.pc
        .createOffer(this.mediaConstraints)
        .then((sessionDescription) => {
          // console.log(`创建 Offer: ${JSON.stringify(sessionDescription)}`);

          if (prefmime !== undefined) {
            const [prefkind] = prefmime.split('/');
            const codecs = RTCRtpReceiver.getCapabilities(prefkind).codecs;
            const preferredCodecs = codecs.filter((codec) => codec.mimeType === prefmime);

            this.pc
              .getTransceivers()
              .filter((transceiver) => transceiver.receiver.track.kind === prefkind)
              .forEach((tcvr) => {
                if (tcvr.setCodecPreferences) {
                  tcvr.setCodecPreferences(preferredCodecs);
                }
              });
          }

          this.pc
            .setLocalDescription(sessionDescription)
            .then(() => {
              fetch(callurl, {
                method: 'POST',
                body: JSON.stringify(sessionDescription),
              })
                .then(this._handleHttpErrors)
                .then((response) => response.json())
                .then((response) => this.onReceiveCall(response))
                .catch((error) => this.onError(`调用错误: ${error}`));
            })
            .catch((error) => console.log(`setLocalDescription error: ${JSON.stringify(error)}`));
        })
        .catch((error) => console.log(`创建 Offer 失败: ${JSON.stringify(error)}`));
    } catch (e) {
      this.disconnect();
      alert(`连接错误: ${e}`);
    }
  }

  /**
   * 创建 PeerConnection 实例
   */

  createPeerConnection() {
    console.log('创建 PeerConnection...');
    this.pc = new RTCPeerConnection(this.pcConfig);
    this.pc.peerid = Math.random(); // 生成唯一的 peerid

    // 监听 ICE 候选者事件
    this.pc.onicecandidate = (evt) => this.onIceCandidate(evt);
    this.pc.onaddstream = (evt) => this.onAddStream(evt);
    this.pc.oniceconnectionstatechange = () => {
      if (this.videoElement) {
        if (this.pc.iceConnectionState === 'connected') {
          this.videoElement.style.opacity = '1.0';
        } else if (this.pc.iceConnectionState === 'disconnected') {
          this.videoElement.style.opacity = '0.25';
        } else if (['failed', 'closed'].includes(this.pc.iceConnectionState)) {
          this.videoElement.style.opacity = '0.5';
        } else if (this.pc.iceConnectionState === 'new') {
          this.getIceCandidate();
        }
      }
    };
    return this.pc;
  }

  onAddStream(event) {
    console.log(`Remote track added: ${JSON.stringify(event)}`);
    this.videoElement.srcObject = event.stream;
    const promise = this.videoElement.play();
    if (promise !== undefined) {
      promise.catch((error) => {
        console.warn(`error: ${error}`);
        this.videoElement.setAttribute('controls', true);
      });
    }
  }

  onIceCandidate(event) {
    if (event.candidate) {
      if (this.pc.currentRemoteDescription) {
        this.addIceCandidate(this.pc.peerid, event.candidate);
      } else {
        this.earlyCandidates.push(event.candidate);
      }
    } else {
      console.log('End of candidates.');
    }
  }

  /**
   * 添加 ICE 候选者到 PeerConnection
   * @param {RTCIceCandidate} candidate - ICE 候选者
   */
  addIceCandidate(peerid, candidate) {
    fetch(`${this.srvurl}/api/addIceCandidate?peerid=${peerid}`, {
      method: 'POST',
      body: JSON.stringify(candidate),
    })
      .then(this._handleHttpErrors)
      .catch((error) => this.onError(`addIceCandidate ${error}`));
  }

  /**
   * 处理 WebRTC 通话的响应
   * @param {Object} message - 来自服务器的响应消息
   */
  onReceiveCall(dataJson) {
    const descr = new RTCSessionDescription(dataJson);
    this.pc
      .setRemoteDescription(descr)
      .then(() => {
        while (this.earlyCandidates.length) {
          const candidate = this.earlyCandidates.shift();
          this.addIceCandidate(this.pc.peerid, candidate);
        }
        this.getIceCandidate();
      })
      .catch((error) => console.log(`设置描述文件失败: ${JSON.stringify(error)}`));
  }

  getIceCandidate() {
    fetch(`${this.srvurl}/api/getIceCandidate?peerid=${this.pc.peerid}`)
      .then(this._handleHttpErrors)
      .then((response) => response.json())
      .then((response) => this.onReceiveCandidate(response))
      .catch((error) => this.onError(`getIceCandidate ${error}`));
  }

  onReceiveCandidate(dataJson) {
    if (dataJson) {
      dataJson.forEach((candidateData) => {
        const candidate = new RTCIceCandidate(candidateData);
        this.pc
          .addIceCandidate(candidate)
          .catch((error) => console.log(`addIceCandidate error: ${JSON.stringify(error)}`));
      });
    }
  }

  /**
   * 错误处理器
   * @param {string} message - 错误信息
   */
  onError(status) {
    console.error(`WebRTC 错误: ${status}`);
  }
}

export default WebRtcStreamer;

组件中使用

<template>
  <div>
    <video id="video" controls muted autoplay></video>
    <button @click="startStream">开始播放</button>
    <button @click="stopStream">停止播放</button>
  </div>
</template>

<script>
import WebRtcStreamer from "@/utils/webRtcStreamer";

export default {
  name: "VideoStreamer",
  data() {
    return {
      webRtcServer: null,
    };
  },
  methods: {
    startStream() {
    const srvurl = "127.0.0.1:9527"
      this.webRtcServer = new WebRtcStreamer(
        "video",
        `${location.protocol}//${srvurl}`
      );
      const videoPath = "stream_name"; // 替换为你的流地址
      this.webRtcServer.connect(videoPath);
    },
    stopStream() {
      if (this.webRtcServer) {
        this.webRtcServer.disconnect(); // 销毁
      }
    },
  },
};
</script>

<style>
video {
  width: 100%;
  height: 100%;
  object-fit: fill;
}
</style>
### WebRTC-Streamer 公网部署配置 为了使 `webrtc-streamer` 能够在公网上正常工作,需要完成几个关键组件的配置: #### 1. 启动命令配置 启动 `webrtc-streamer` 的时候,可以通过特定参数来绑定内外网络接口以及设置访问权限。具体命令如下: ```bash webrtc-streamer.exe -o -H 内网ip:8000 -S 公网ip:公网端口 -T admin:123456@公网ip:公网端口 [^1] ``` 这里 `-H` 参数用于指定内部局域网中的 IP 地址及端口号;而 `-S` 则用来定义外部可访问的服务地址与端口组合;最后通过 `-T` 来设定管理员账户及其密码。 #### 2. TURN 服务器配置 TURN (Traversal Using Relays around NAT) 是一种允许穿越防火墙的技术,在复杂网络环境中尤其重要。对于 `webrtc-streamer` 应用来说,建议单独架设 TURN 服务并将其集成进来。以下是基于 Coturn 实现的一个典型配置实例: ```plaintext listening-port=3478 listening-ip=172.0.3.54 external-ip=116.232.105.146 user=test:12345 cert=/etc/turn_server_cert.pem pkey=/etc/turn_server_pkey.pem min-port=50000 max-port=60000 no-cli cli-password=$5$79a316b350311570$81df9cfb9af7f5e5a76eada31e7097b663a0670f99a3c07ded3f1c8e59c5658a [^3] ``` 此部分重点在于正确填写 `listening-ip`, `external-ip` 和其他安全认证信息。同时还需要确保 SSL/TLS 证书路径无误,并开放必要的 UDP 端口范围供媒体流传输使用。 #### 3. Nginx 反向代理设置 为了让客户端能够顺利连接至位于私有网络内的 `webrtc-streamer` ,通常会在前端放置一台支持 WebSocket 协议转发功能的 HTTP(S) 服务器作为反向代理。下面是一个简单的 Nginx 配置片段示范: ```nginx http { ... upstream websocket_backend { server localhost:8000; } server { listen 443 ssl http2; server_name your_public_domain_or_ip; location / { proxy_pass http://websocket_backend/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; # Other headers... } ssl_certificate /path/to/cert.crt; ssl_certificate_key /path/to/private.key; } } ``` 上述代码段展示了如何利用 Nginx 将来自互联网用户的请求重定向给后台运行着 `webrtc-streamer` 的节点,并处理好 HTTPS 加密通信事宜。 ---
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值