当前位置:网站首页>Introduction to the browser video frame operation method requestvideoframecallback()
Introduction to the browser video frame operation method requestvideoframecallback()
2022-07-28 01:12:00 【InfoQ】
Recently, I found a cool application scenario , Act on the camera , Then the analyzer animation is rendered to 3D Model , After understanding its implementation principle , Take some study notes for some technical points .
requestVideoFrameCallback() Is one of the technical points , Using this method can efficiently process video in the browser .
HTMLVideoElement.requestVideoFrameCallback()
It's a new one WEB API,2021 year 1 month 25 Draft submitted on .
requestVideoFrameCallback() Method allows WEB Developers register a callback method , The callback method runs in the rendering step when a new video frame is sent to the compositor . This is to allow developers to perform efficient video operations per frame , For example, video processing and painting on canvas ( screenshots )、 Video analysis or synchronization with external audio sources .
And requestAnimationFrame() The difference between
Can pass API Execution method
drawImage()
Draw the video frame to the canvas and other operations will be synchronized with the frame rate of the video played on the screen as much as possible . And usually trigger about every second
60 Time of
window.requestAnimationFrame() Different ,
requestVideoFrameCallback() Bound with the actual video frame rate , But there is also an important exception :
The effective rate of running callback is the smaller rate between video rate and browser rate . This means that in
60Hz Drawn in the browser
25fps The video will be
25Hz Trigger callback . In the same
60Hz In the browser
120fps The video will be in
60Hz Trigger callback .
Because of it and
window.requestAnimationFrame() be similar , This method was originally proposed as
video.requestAnimationFrame(), Finally use the new name
requestVideoFrameCallback(), This was agreed after a long discussion , This name is relatively more intuitive .
In front end development , Usually in the use of some new WEB API Its availability needs to be tested when , You can also use the following code to detect :
if ("requestVideoFrameCallback" in HTMLVideoElement.prototype) {
// Support the corresponding API
}
Browser support
Sure
Click the link to check the corresponding support
.

Usage method
If ever used
requestAnimationFrame() Method , Then it will be right immediately
requestVideoFrameCallback() The method is no stranger . Register an initial callback , Then re register when the callback triggers .
const videoFrameCallback = (now, metadata) => {
console.log(now, metadata);
// Re register the callback to get notification about the next frame .
videoElement.requestVideoFrameCallback(videoFrameCallback);
};
// Initially register callback , In order to be notified at the first frame .
videoElement.requestVideoFrameCallback(videoFrameCallback);
In the callback ,
now It's a
DOMHighResTimeStamp,
metadata It's a
VideoFrameMetadata Dictionaries , Has the following properties :
presentationTime:DOMHighResTimeStamptype , The time when the user agent submits the frame for synthesis .
expectedDisplayTime:DOMHighResTimeStamptype , The time the user agent expects the framework to be visible .
width:unsigned longtype , The width of the video frame , In pixels .
height:unsigned longtype , The height of the video frame , In pixels .
mediaTime:doubletype , Media presentation timestamp (PTS), In seconds of rendered frames ( for example , It's invideo.currentTimeTimestamp on the timeline ).
presentedFrames:unsigned longtype , Submit a count of the number of frames used for synthesis . Allow the client to determineVideoFrameRequestCallbackWhether frames are lost between instances .
processingDuration:doubletype , The slave will have the same render timestamp as this frame (PTS) Encoded packet ( for example , AndmediaTimeidentical ) The duration of submitting to the decoder until the decoded frame is ready for rendering ( In seconds ).
about
WebRTC Applications , Other properties may appear :
captureTime:DOMHighResTimeStamptype , For video frames from local or remote sources , This is the time when the camera captures the frame . For remote sources , Use clock synchronization andRTCPThe sender reports to estimate the capture time , In order to RTP Timestamp is converted to capture time .
receiveTime:DOMHighResTimeStamptype , For video frames from remote sources , This is the time when the platform receives the encoded frame , That is, the time when the last packet belonging to the frame is received through the network .
rtpTimestamp:unsigned longtype , Associated with this video frame RTP Time stamp .
Please note that , In some cases ,
width and
height Possible and
videoWidth and
videoHeight Different ( for example , Deformed video may have rectangular pixels ).
In the parameter list above, it is particularly noteworthy that
mediaTime , stay
Chromium In the implementation of , Use audio clock as support
video.currentTime The source of time . and
mediaTime Directly by the frame
presentationTimestamp fill . If you want to accurately identify frames in a reproducible manner , Including accurately identifying missed frames , So you should use
mediaTime.
summary
Being able to access the camera in the browser without using third-party software is an incredible progress . And
canvas And some of the JavaScript Combination , Cameras become fast and easy to access . It can not only use cameras , And because
canvas It's super flexible , Will be able to add sexy in the future
instagram Style image filter .
边栏推荐
- Cross desktop web container evolution
- Recommended system model (III): fine scheduling model [LR, gbdt, wide & deep, DCN, DIN, Dien, MMOE, ple]
- Recommended system - fine tuning model: xdeepfm
- Oracle错误: ORA-01722 无效数字
- UML类图的六大关系,最佳学习理解方式
- Swoole定时器
- 福特SUV版“野马”正式下线,安全、舒适一个不落
- Use of postman
- 字节飞书人力资源套件三面
- 怎么清晰地理解、表达 IaaS 、 PaaS 、 SaaS ?
猜你喜欢
随机推荐
C语言程序设计 | offsetof宏的讲解及其模拟实现
Circular structure of shell system learning
LSB steganography
【OpenCV 例程 300篇】241. 尺度不变特征变换(SIFT)
uniapp显示富文本效果demo(整理)
Programmer growth Chapter 30: do you really understand feedback?
共创文旅新篇章|新起典与国华文旅签订战略合作协议
重新定义分析 - EventBridge 实时事件分析平台发布
Unknown database ‘xxxxx‘
推荐系统-离线召回:u2tag2i、icf
I/O设备的基本概念及分类
Detailed explanation of swoole memory table
【STM32】看门狗模块
Maximize activation
文件系统挂载
Self use drawing bed building tutorial
Swear, swear, swear
c# 反射之Type使用
[STM32] watchdog module
Branch and loop sentence exercises







