简体   繁体   English

如何检测何时不支持`canvas.captureStream()`?

[英]How to detect when `canvas.captureStream()` is not supported?

It is known that iOS Safari does not support canvas.captureStream() to (eg) pipe its content into a video element, see this demo not working in iOS Safari.众所周知,iOS Safari 不支持canvas.captureStream()来(例如)将其内容通过管道传输到视频元素中,请参阅此演示在 iOS Safari 中不起作用。

However, canvas.captureStream() is a valid function in iOS Safari, and correctly returns a CanvasCaptureMediaStreamTrack , it just doesn't function as intended.但是, canvas.captureStream()是 iOS Safari 中的有效函数,并且正确返回CanvasCaptureMediaStreamTrack ,它只是无法按预期运行。 In order to detect browsers that don't support canvas.captureStream , it would have been easy to do a test typeof canvas.captureStream === 'function' , but at least for iOS Safari, we can't rely on that.为了检测不支持canvas.captureStream浏览器,做一个测试typeof canvas.captureStream === 'function'很容易,但至少对于 iOS Safari,我们不能依赖它。 Neither can we rely on the type of the returned value.我们也不能依赖返回值的类型。

How do I write JavaScript that detects whether the current browser effectively supports canvas.captureStream() ?如何编写检测当前浏览器是否有效支持canvas.captureStream() JavaScript?

No iOS to test it here, but according to the comments on the issue you linked to, captureStream() actually works, what doesn't is the HTMLVideoElement's reading of this MediaStream.没有 iOS 在这里测试它,但根据你链接到的问题的评论, captureStream()实际上有效,什么不是 HTMLVideoElement 对这个 MediaStream 的读取。 So that's what you actually want to test.所以这就是你真正想要测试的。

According to the messages there, the video doesn't even fail to load the video (ie the metadata are correctly set and I don't expect events like error to fire, though if it does, then it's quite simple to test: check if a video is able to play such a MediaStream.根据那里的消息,视频甚至没有无法加载视频(即元数据设置正确,我不希望像error这样的事件触发,但如果确实如此,那么测试很简单:检查视频能够播放这样的 MediaStream。

 function testReadingOfCanvasCapturedStream() { // first check the DOM API is available if( !testSupportOfCanvasCapureStream() ) { return Promise.resolve(false); } // create a test canvas const canvas = document.createElement("canvas"); // we need to init a context on the canvas const ctx = canvas.getContext("2d"); const stream = canvas.captureStream(); const vid = document.createElement("video"); vid.muted = true; vid.playsInline = true; vid.srcObject = stream; let supports = false; // Safari needs we draw on the canvas // asynchronously after we requested the MediaStream setTimeout(() => ctx.fillRect(0,0,5,5)); // if it failed, .play() would be enough // but according to the comments on the issue, it isn't return vid.play() .then(() => supports = true) .finally(() => { // clean stream.getTracks().forEach(track => track.stop()); return supports; }); } function testSupportOfCanvasCapureStream() { return "function" === typeof HTMLCanvasElement.prototype.captureStream; } testReadingOfCanvasCapturedStream() .then(supports => console.log(supports));

But if the video is able to play, but no images is painted, then we have to go a bit deeper and check what has been painted on the video.但是如果视频可以播放,但没有绘制图像,那么我们必须更深入地检查视频上绘制的内容。 To do this, we'll draw some color on the canvas, wait for the video to have loaded and draw it back on the canvas before checking the color of the frame on the canvas:为此,我们将在画布上绘制一些颜色,等待视频加载并将其绘制回画布,然后检查画布上帧的颜色:

 async function testReadingOfCanvasCapturedStream() { // first check the DOM API is available if( !testSupportOfCanvasCapureStream() ) { return false; } // create a test canvas const canvas = document.createElement("canvas"); // we need to init a context on the canvas const ctx = canvas.getContext("2d"); const stream = canvas.captureStream(); const clean = () => stream.getTracks().forEach(track => track.stop()); const vid = document.createElement("video"); vid.muted = true; vid.srcObject = stream; // Safari needs we draw on the canvas // asynchronously after we requested the MediaStream setTimeout(() => { // we draw in a well knwown color ctx.fillStyle = "#FF0000"; ctx.fillRect(0,0,300,150); }); try { await vid.play(); } catch(e) { // failed to load, no need to go deeper // it's not supported clean(); return false; } // here we should have our canvas painted on the video // let's keep this image on the video await vid.pause(); // now draw it back on the canvas ctx.clearRect(0,0,300,150); ctx.drawImage(vid,0,0); const pixel_data = ctx.getImageData(5,5,1,1).data; const red_channel = pixel_data[0]; clean(); return red_channel > 0; // it has red } function testSupportOfCanvasCapureStream() { return "function" === typeof HTMLCanvasElement.prototype.captureStream; } testReadingOfCanvasCapturedStream() .then(supports => console.log(supports));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Canvas.captureStream() 在 Angular 8 中不存在 - Canvas.captureStream() does not exist in Angular 8 将drawImage与MediaRecorder一起使用时,为什么canvas.captureStream中的视频为空 - Why is video from canvas.captureStream empty when using drawImage with MediaRecorder 使用 canvas.captureStream() 使用 alpha 通道捕获视频 - Capture video with alpha channel using canvas.captureStream() 将“navigator.mediaDevices.getUserMedia”替换为“canvas.captureStream” - Replace "navigator.mediaDevices.getUserMedia" with "canvas.captureStream" Firefox canvas.captureStream 捕获处于非活动状态的黑帧 - Firefox canvas.captureStream captures black frames as was inactive <video>使用 MediaRecorder() 从<canvas>使用 canvas.captureStream() 在 firefox、chrome 上呈现不同 - <video> playback of recorded stream using MediaRecorder() from <canvas> using canvas.captureStream() renders differently at firefox, chromium 使用captureStream和mediaRecorder进行画布录制 - Canvas recording using captureStream and mediaRecorder 检测 HTML5 的最佳方法<canvas>不支持 - Best way to detect that HTML5 <canvas> is not supported MediaRecorderAPI:如何使用来自 canvas 的 captureStream 和来自音频文件的音频源来生成媒体 Stream - MediaRecorderAPI : How to produce a Media Stream using captureStream from canvas & an audio source from an audio file 如何检测画布何时可以进行操作? - How to detect when a canvas is ready for manipulation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM