vue视频直接播放rtsp流;vue视频延迟问题解决;webRTC占cpu太大卡死问题解决;解决webRTC播放卡花屏问题:

这篇具有很好参考价值的文章主要介绍了vue视频直接播放rtsp流;vue视频延迟问题解决;webRTC占cpu太大卡死问题解决;解决webRTC播放卡花屏问题:。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

播放多个视频

 <div class="video-box">
      <div class="video">
        <iframe style="width:100%;height:100%;" name="ddddd" id="iframes" scrolling="auto" :src="videoLeftUrl"></iframe>
      </div>
      <div class="video">
        <iframe style="width:100%;height:100%;" name="ddddd" id="iframes" scrolling="auto" :src="videoRightUrl"></iframe>
      </div>
      <div class="video">
        <iframe style="width:100%;height:100%;" name="ddddd" id="iframes" scrolling="auto" :src="videoRtspUrl"></iframe>
      </div>
    </div>

js部分其中的item就是rtsp视频流

    getShareVideoLeftUrl(item) {
      this.videoLeftUrl = `/static/test.html?data=${item}`
    },
    getShareVideoRightUrl(item) {
      this.videoRightUrl = `/static/test.html?data=${item}`
    },
    getShareVideoRtspUrl(item) {
      this.videoRtspUrl = `/static/test.html?data=${item}`
    },

public/static/test.html内容

<html>
	<head>
		<script src="js/webrtcstreamer.js"></script>
		<script>
			// 接受从vue组件中传过来的参数
			let url = location.search; //这一条语句获取了包括问号开始到参数的最后,不包括前面的路径
			let params = url.substr(1); //去掉问号
			let pa = params.split("&");
			let s = new Object();
             //  设置后端服务地址
            let VIDEOURL = "http://172.18.127.7:8000" //服务视频webrtc

			for (let i = 0; i < pa.length; i++) {
				s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
			}
			console.log(s.data)
			window.onload = function() {
				webRtcServer = new WebRtcStreamer("video", VIDEOURL);
				webRtcServer.connect(s.data);
			}
			window.onbeforeunload = function() {
				webRtcServer.disconnect();
			}
		</script>
	</head>
	<body>
		<h1 value="da3"></h1>
		<video id="video" style="width: 100%;height: 100%;" controls autoplay muted />
	</body>
</html>

其中public/static/js/webrtcstreamer.js文件内容如下

var WebRtcStreamer = (function() {

/** 
 * Interface with WebRTC-streamer API
 * @constructor
 * @param {string} videoElement - id of the video element tag
 * @param {string} srvurl -  url of webrtc-streamer (default is current location)
*/
var WebRtcStreamer = function WebRtcStreamer (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;    

	this.pcOptions        = { "optional": [{"DtlsSrtpKeyAgreement": true} ] };

	this.mediaConstraints = { offerToReceiveAudio: true, offerToReceiveVideo: true };

	this.iceServers = null;
	this.earlyCandidates = [];
}

WebRtcStreamer.prototype._handleHttpErrors = function (response) {
    if (!response.ok) {
        throw Error(response.statusText);
    }
    return response;
}

/** 
 * Connect a WebRTC Stream to videoElement 
 * @param {string} videourl - id of WebRTC video stream
 * @param {string} audiourl - id of WebRTC audio stream
 * @param {string} options -  options of WebRTC call
 * @param {string} stream  -  local stream to send
*/
WebRtcStreamer.prototype.connect = function(videourl, audiourl, options, localstream) {
	this.disconnect();
	
	// getIceServers is not already received
	if (!this.iceServers) {
		console.log("Get IceServers");
		
		fetch(this.srvurl + "/api/getIceServers")
			.then(this._handleHttpErrors)
			.then( (response) => (response.json()) )
			.then( (response) =>  this.onReceiveGetIceServers.call(this,response, videourl, audiourl, options, localstream))
			.catch( (error) => this.onError("getIceServers " + error ))
				
	} else {
		this.onReceiveGetIceServers(this.iceServers, videourl, audiourl, options, localstream);
	}
}

/** 
 * Disconnect a WebRTC Stream and clear videoElement source
*/
WebRtcStreamer.prototype.disconnect = function() {		
	if (this.videoElement) {
		this.videoElement.src = "";
	}
	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;
	}
}    

/*
* GetIceServers callback
*/
WebRtcStreamer.prototype.onReceiveGetIceServers = function(iceServers, videourl, audiourl, options, stream) {
	this.iceServers       = iceServers;
	this.pcConfig         = iceServers || {"iceServers": [] };
	try {            
		this.createPeerConnection();

		var 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);
		}

                // clear early candidates
		this.earlyCandidates.length = 0;
		
		// create Offer
		var bind = this;
		this.pc.createOffer(this.mediaConstraints).then(function(sessionDescription) {
			console.log("Create offer:" + JSON.stringify(sessionDescription));
			
			bind.pc.setLocalDescription(sessionDescription
				, function() {
					fetch(callurl, { method: "POST", body: JSON.stringify(sessionDescription) })
						.then(bind._handleHttpErrors)
						.then( (response) => (response.json()) )
						.catch( (error) => bind.onError("call " + error ))
						.then( (response) =>  bind.onReceiveCall.call(bind,response) )
						.catch( (error) => bind.onError("call " + error ))
				
				}
				, function(error) {
					console.log ("setLocalDescription error:" + JSON.stringify(error)); 
				} );
			
		}, function(error) { 
			alert("Create offer error:" + JSON.stringify(error));
		});

	} catch (e) {
		this.disconnect();
		alert("connect error: " + e);
	}	    
}


WebRtcStreamer.prototype.getIceCandidate = function() {
	fetch(this.srvurl + "/api/getIceCandidate?peerid=" + this.pc.peerid)
		.then(this._handleHttpErrors)
		.then( (response) => (response.json()) )
		.then( (response) =>  this.onReceiveCandidate.call(this, response))
		.catch( (error) => bind.onError("getIceCandidate " + error ))
}
					
/*
* create RTCPeerConnection 
*/
WebRtcStreamer.prototype.createPeerConnection = function() {
	console.log("createPeerConnection  config: " + JSON.stringify(this.pcConfig) + " option:"+  JSON.stringify(this.pcOptions));
	this.pc = new RTCPeerConnection(this.pcConfig, this.pcOptions);
	var pc = this.pc;
	pc.peerid = Math.random();		
	
	var bind = this;
	pc.onicecandidate = function(evt) { bind.onIceCandidate.call(bind, evt); };
	pc.onaddstream    = function(evt) { bind.onAddStream.call(bind,evt); };
	pc.oniceconnectionstatechange = function(evt) {  
		console.log("oniceconnectionstatechange  state: " + pc.iceConnectionState);
		if (bind.videoElement) {
			if (pc.iceConnectionState === "connected") {
				bind.videoElement.style.opacity = "1.0";
			}			
			else if (pc.iceConnectionState === "disconnected") {
				bind.videoElement.style.opacity = "0.25";
			}			
			else if ( (pc.iceConnectionState === "failed") || (pc.iceConnectionState === "closed") )  {
				bind.videoElement.style.opacity = "0.5";
			} else if (pc.iceConnectionState === "new") {
				bind.getIceCandidate.call(bind)
			}
		}
	}
	pc.ondatachannel = function(evt) {  
		console.log("remote datachannel created:"+JSON.stringify(evt));
		
		evt.channel.onopen = function () {
			console.log("remote datachannel open");
			this.send("remote channel openned");
		}
		evt.channel.onmessage = function (event) {
			console.log("remote datachannel recv:"+JSON.stringify(event.data));
		}
	}
	pc.onicegatheringstatechange = function() {
		if (pc.iceGatheringState === "complete") {
			const recvs = pc.getReceivers();
		
			recvs.forEach((recv) => {
			  if (recv.track && recv.track.kind === "video") {
				console.log("codecs:" + JSON.stringify(recv.getParameters().codecs))
			  }
			});
		  }
	}

	try {
		var dataChannel = pc.createDataChannel("ClientDataChannel");
		dataChannel.onopen = function() {
			console.log("local datachannel open");
			this.send("local channel openned");
		}
		dataChannel.onmessage = function(evt) {
			console.log("local datachannel recv:"+JSON.stringify(evt.data));
		}
	} catch (e) {
		console.log("Cannor create datachannel error: " + e);
	}	
	
	console.log("Created RTCPeerConnnection with config: " + JSON.stringify(this.pcConfig) + "option:"+  JSON.stringify(this.pcOptions) );
	return pc;
}


/*
* RTCPeerConnection IceCandidate callback
*/
WebRtcStreamer.prototype.onIceCandidate = function (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.");
	}
}


WebRtcStreamer.prototype.addIceCandidate = function(peerid, candidate) {
	fetch(this.srvurl + "/api/addIceCandidate?peerid="+peerid, { method: "POST", body: JSON.stringify(candidate) })
		.then(this._handleHttpErrors)
		.then( (response) => (response.json()) )
		.then( (response) =>  {console.log("addIceCandidate ok:" + response)})
		.catch( (error) => this.onError("addIceCandidate " + error ))
}
				
/*
* RTCPeerConnection AddTrack callback
*/
WebRtcStreamer.prototype.onAddStream = function(event) {
	console.log("Remote track added:" +  JSON.stringify(event));
	
	this.videoElement.srcObject = event.stream;
	var promise = this.videoElement.play();
	if (promise !== undefined) {
	  var bind = this;
	  promise.catch(function(error) {
		console.warn("error:"+error);
		bind.videoElement.setAttribute("controls", true);
	  });
	}
}
		
/*
* AJAX /call callback
*/
WebRtcStreamer.prototype.onReceiveCall = function(dataJson) {
	var bind = this;
	console.log("offer: " + JSON.stringify(dataJson));
	var descr = new RTCSessionDescription(dataJson);
	this.pc.setRemoteDescription(descr
		, function()      { 
			console.log ("setRemoteDescription ok");
			while (bind.earlyCandidates.length) {
				var candidate = bind.earlyCandidates.shift();
				bind.addIceCandidate.call(bind, bind.pc.peerid, candidate);				
			}
		
			bind.getIceCandidate.call(bind)
		}
		, function(error) { 
			console.log ("setRemoteDescription error:" + JSON.stringify(error)); 
		});
}	

/*
* AJAX /getIceCandidate callback
*/
WebRtcStreamer.prototype.onReceiveCandidate = function(dataJson) {
	console.log("candidate: " + JSON.stringify(dataJson));
	if (dataJson) {
		for (var i=0; i<dataJson.length; i++) {
			var candidate = new RTCIceCandidate(dataJson[i]);
			
			console.log("Adding ICE candidate :" + JSON.stringify(candidate) );
			this.pc.addIceCandidate(candidate
				, function()      { console.log ("addIceCandidate OK"); }
				, function(error) { console.log ("addIceCandidate error:" + JSON.stringify(error)); } );
		}
		this.pc.addIceCandidate();
	}
}


/*
* AJAX callback for Error
*/
WebRtcStreamer.prototype.onError = function(status) {
	console.log("onError:" + status);
}

return WebRtcStreamer;
})();

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined')
	module.exports = WebRtcStreamer;
else
	window.WebRtcStreamer = WebRtcStreamer;

这里启用需要下载webRTC

  https://github.com/mpromonet/webrtc-streamer/releases

需要注意的是这里启动不要直接双击而是使用cmd命令启动

 webrtcstreamer.prototype.onerror设置,video,vue.js,音视频,前端

start 应用名 -o 

一定加上-o否则webRTC占cpu太大 容易卡死文章来源地址https://www.toymoban.com/news/detail-770846.html


解决卡花屏问题:
在html页面中的webRtcServer.connect(s.data,"","rtptransport=tcp");加上"","rtptransport=tcp"就搞定
<html>
	<head>
		<script src="js/webrtcstreamer.js"></script>
		<script>
			// 接受从vue组件中传过来的参数
			let url = location.search; //这一条语句获取了包括问号开始到参数的最后,不包括前面的路径
			let params = url.substr(1); //去掉问号
			let pa = params.split("&");
			let s = new Object();
             //  设置后端服务地址
            let VIDEOURL = "http://172.18.127.7:8000" //服务视频webrtc

			for (let i = 0; i < pa.length; i++) {
				s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
			}
			console.log(s.data)
			window.onload = function() {
				webRtcServer = new WebRtcStreamer("video", VIDEOURL);
				webRtcServer.connect(s.data,"","rtptransport=tcp");
			}
			window.onbeforeunload = function() {
				webRtcServer.disconnect();
			}
		</script>
	</head>
	<body>
		<h1 value="da3"></h1>
		<video id="video" style="width: 100%;height: 100%;" controls autoplay muted />
	</body>
</html>

到了这里,关于vue视频直接播放rtsp流;vue视频延迟问题解决;webRTC占cpu太大卡死问题解决;解决webRTC播放卡花屏问题:的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • VUE3 播放RTSP实时、回放(NVR录像机)视频流(使用WebRTC)

    1、下载webrtc-streamer,下载的最新window版本 Releases · mpromonet/webrtc-streamer · GitHub  2、解压下载包  3、webrtc-streamer.exe启动服务 (注意:这里可以通过当前文件夹下用cmd命令webrtc-streamer.exe -o这样占用cpu会很少,直接双击exe文件会占用cpu) cmd  webrtc-streamer.exe -o 启动如下图所示,

    2024年04月12日
    浏览(37)
  • VUE3+TS+VITE+webrtc-streamer实现实时视频播放(监控设备-rtsp,进来保你成)

    目录 1、下载webrtc-streamer,下载最新window版本  2、解压下载包  3、双击webrtc-streamer.exe启动服务  4、引入webrtc-streamer         1、将下载包中html文件夹下webrtcstreamer.js文件和html/libs文件夹下adapter.min.js文件复制到VUE项目public目录下 2、在index.html文件里引入这两个js文件 5、使

    2024年02月05日
    浏览(26)
  • 【音视频】基于webrtc协议浏览器播放rtsp

    现阶段直播越来越流行,直播技术发展也越来越快。Webrtc和rtsp是比较火热的技术,而且应用也比较广泛。本文通过实践来展开介绍关于rtsp、webrtc的使用过程。 本文是基于ffmpeg技术将mp4转换为rtsp视频流,并且将流推送到流媒体服务器(EasyDarwin)上,而后采用了webrtc-streamer对

    2024年01月19日
    浏览(53)
  • 视频汇聚平台EasyCVR视频监控播放平台WebRTC流地址无法播放的问题解决方案

    开源EasyDarwin视频监控TSINGSEE青犀视频平台EasyCVR能在复杂的网络环境中,将分散的各类视频资源进行统一汇聚、整合、集中管理,在视频监控播放上,TSINGSEE青犀视频安防监控汇聚平台可支持1、4、9、16个画面窗口播放,可同时播放多路视频流,也能支持视频定时轮播。视频监

    2024年02月12日
    浏览(38)
  • 【Vue2 + webrtc-steamer】rtsp流在Web端实时播放

    操作系统:Win10 vue版本:vue2 必须将rtsp通过 播放器插件/服务器/… 转换为 flv/webrtc/… 最新在线可用rtsp码流地址(可用 flv播放器 测试): rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4 rtmp地址 rtmp://ns8.indexforce.com/home/mystream https地址 : MP4 : https://baikevideo.cdn.bcebos.com/med

    2024年02月02日
    浏览(30)
  • 前端VUE播放RTSP、RTMP、HLS、FLV视频流的解决方案

    最近有个需求是前端在浏览器显示摄像头传回的RTSP视频流,我和后端都没做过视频流的项目,所以一步步摸索过来,方法和经验供大家参考。前端采用的技术有VUE+video.js+flv.js 从上图可以看出,RTSP流不能直接在浏览器播放,所以需要转码: RTMP的流需要在浏览器中用flash播放

    2024年02月06日
    浏览(39)
  • 怎样使用ovsyunlive在web网页上直接播放rtsp/rtmp视频

    业务中需要在网页中直接播放rtsp和rtmp视频,多方比较测试发现ovsyunlive的播放器能直接播放rtsp/rtmp视频,还是非常方便简洁,使用过程如下: 1,Windows系统在github上面下载ovsyunlive绿色包下载解压。 github地址:https://github.com/ccallcn/ovsyunlive 详细使用详见说明 2,启动前修改ovs

    2024年02月02日
    浏览(23)
  • Unity下实现跨平台的RTMP推流|轻量级RTSP服务|RTMP播放|RTSP播放低延迟解决方案

    2018年,我们开始在原生RTSP|RTMP直播播放器的基础上,对接了Unity环境下的低延迟播放,毫秒级延迟,发布后,就得到了业内一致的认可。然后我们覆盖了Windows、Android、iOS、Linux的RTMP推送、轻量级RTSP服务和RTSP|RTMP播放。 目前看,Unity环境下,我们在行业内的延迟几乎是最低的

    2024年01月22日
    浏览(41)
  • RTSP转WebRTC视频协议解决方案

    浏览器或是音视频播放器(如ffplay、VLC、射手影音等)播放互联网上视频文件,需要对接收数据经过以下几个步骤: 解析协议- 解封装 - 解码音视频 - 音视频同步。 简单起见,可将视频数据分为以下四层如下表1-1所示,可结合图1-1了解其数据流在各阶段的处理流程 表1-1 音视频

    2024年02月09日
    浏览(30)
  • vue项目播放rtsp流视频。

    下载webtrc-streamer的压缩包,在github上有 https://github.com/mpromonet/webrtc-streamer/releases 将下载包html文件夹下webrtcstreamer.js文件和html/libs文件夹下adapter.min.js文件复制到VUE项目public/stactic目录下,在index.html文件里引入这两个js文件。 解压之后打开exe文件就可以播放了。(想做到把exe变

    2024年02月02日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包