webRtc播放rtsp视频流(vue2、vue3+vite+ts)

本文介绍了如何在Windows环境下下载并启动WebRtc-streamer,通过RTSP视频流进行测试。然后详细阐述了如何在Vue2和Vue3+vite项目中集成WebRtc,包括引入JS文件、设置video标签以及连接RTSP流。同时,文章提到了可能出现的内存占用过高和CORS政策问题,并给出了相应的解决策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、下载webRtc

开发环境用的win10版本的。

github上直接下载,速度感人。

Releases · mpromonet/webrtc-streamer · GitHub

提供资源下载,0积分

https://download.csdn.net/download/weiqiang915/87700892

二、启动,测试

webrtc-streamer.exe -o -H 0.0.0.0:9001

-o:无需转码,降低CPU

-H:指定端口号

 打开浏览器,输入 127.0.0.1:9001/webrtcstreamer.html?video=rtsp://192.168.10.112:8557/h264

 测试用的臻识C3相机的rtsp视频流。

到此,说明webRtc插件没问题。下面整合到Vue2、Vue3+vite中使用 。

三、vue2中使用webRtc

1、两个js文件放入public文件夹下。(js文件在下载的插件中找)

  

2、vue源码

<template>
    <div>
        <video id="video" autoplay width="900" height="900"></video>
    </div>
</template>
   
<script>
export default {
    name: 'index1',
    data() {
        return {
            webRtcServer: null
        }
    },
    mounted() {
        //video:需要绑定的video控件ID
        //127.0.0.1:8000:启动webrtc-streamer的设备IP和端口,默认8000
        this.webRtcServer = new WebRtcStreamer('video', location.protocol + '//192.168.10.26:9001')
        //需要查看的rtsp地址
        this.webRtcServer.connect('rtsp://192.168.10.112:8557/h264')
    },
    beforeDestroy() {
        this.webRtcServer.disconnect()
        this.webRtcServer = null
    },
    methods: {
    }
}
</script>
   
<style scoped></style>

四、vue3+ts+vite中使用webRtc

1、assets文件夹下放两个js文件(js文件在下载的插件中找)

2、index.html中引用js

 3.指定vue中使用

<video id="video" autoplay width="1050" height="1050"></video>


//获取webRtc服务
const webRtcServer = ref<any>()
const initWebRtcServer = async () => {
    nextTick(() => {
        video:需要绑定的video控件ID
        //127.0.0.1:8000:启动webrtc-streamer的设备IP和端口,默认8000
        // webRtcServer.value = new WebRtcStreamer('video', location.protocol + '//192.168.10.26:8000')
        webRtcServer.value = new WebRtcStreamer('video', `${location.protocol}//${webRtcServerIp.value}:8000`)
        //需要查看的rtsp地址
        webRtcServer.value.connect(`rtsp://${ipAddr.value}:8557/h264`)
    })
}
//页面销毁时销毁webRtc
const webRtcServerDis = computed(() => {
    webRtcServer.value.disconnect()
    webRtcServer.value = null
})


调用initWebRtcServer()方法即可

4、打包注意事项

打包生成dist文件夹后,在文件内新建两个文件夹src\assets,用于存放两个js文件。否则会出现上线后webRtc无法实例化的情况。

 

 五、坑

1、控制台报错

Uncaught TypeError: Cannot set properties of null (setting 'srcObject')
    at WebRtcStreamer.onAddStream (webrtcstreamer.js:241:37)
    at pc.onaddstream (webrtcstreamer.js:152:40)
WebRtcStreamer.onAddStream @ webrtcstreamer.js:241
pc.onaddstream @ webrtcstreamer.js:152
webrtcstreamer.js:259 

 解决办法

 2、webRtc服务内存占用过高,启动时加 -o 参数。

3、Access to fetch at 'http://192.168.10.26:9001/api/getIceServers' from origin 'http://dpzn.cc:9230' has been blocked by CORS policy: The request client is not a secure context and the resource is in more-private address space `private`.
webrtcstreamer.js:45 
        GET http://192.168.10.26:9001/api/getIceServers net::ERR_FAILED

 解决办法:

浏览器输入:edge://flags/#block-insecure-private-network-requests

 

### 实现 RTSP 视频流播放 为了在 Vue3 项目中实现 RTSP 媒体的播放,可以采用 WebRTC 技术来处理视频传输。具体来说,通过 `webrtc-streamer` 工具将 RTSP 转换成适合网页使用的格式[^1]。 安装并配置好 `webrtc-streamer` 后,在命令行输入 `cmd webrtc-streamer.exe -o` 来启动服务端程序。这一步骤会使得本地服务器能够接收来自 IP 摄像头或其他设备发送过来的 RTSP 数据,并将其转换成可以通过 WebSocket 协议传递给前端页面的形式。 接下来是在 Vue 组件内部集成这部分功能: #### 安装依赖库 首先需要确保已经安装了必要的 npm 包用于操作 MediaDevices 和连接至 Websocket: ```bash npm install simple-peer ws ``` #### 创建 VideoPlayer.vue 文件 创建一个新的单文件组件用来展示视频内容: ```vue <template> <div class="video-container"> <video ref="videoElement" autoplay playsinline></video> </div> </template> <script setup lang="ts"> import { onMounted, ref } from &#39;vue&#39; import SimplePeer from &#39;simple-peer&#39; const videoElement = ref<HTMLVideoElement | null>(null) onMounted(() => { const peer = new SimplePeer({ initiator: true, trickle: false, stream: navigator.mediaDevices.getUserMedia({ video: true }) .then((stream) => { if (videoElement.value) { videoElement.value.srcObject = stream; } }), }) let socket = new WebSocket(&#39;ws://localhost:8000/ws&#39;) socket.onmessage = function(event){ peer.signal(JSON.parse(event.data)) } peer.on(&#39;signal&#39;, data => { socket.send(JSON.stringify(data)); }); }) </script> <style scoped> .video-container{ width: 100%; } video { max-width: 100%; height: auto; } </style> ``` 此代码片段展示了如何利用 `SimplePeer` 库建立 P2P 连接以及怎样把获取到的媒体设置为 `<video>` 标签的内容源。注意这里的 WebSocket URL (`ws://localhost:8000/ws`) 需要根据实际部署环境调整。
评论 55
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值