RTCPeerConnection
方法
getStats()
returns a promise which resolves with data providing statistics about either the overall connection or about the specified
MediaStreamTrack
.
promise = rtcPeerConnection.getStats(selector)
选择器
可选
MediaStreamTrack
for which to gather statistics. If this is
null
(the default value), statistics will be gathered for the entire
RTCPeerConnection
.
A
Promise
which resolves with an
RTCStatsReport
object providing connection statistics. The contents of the report depend on the
选择器
as well as other details of the connection.
This method does not throw exceptions; instead, it rejects the returned promise with one of the following errors:
InvalidAccessError
RTCRtpSender
or
RTCRtpReceiver
whose
track
matches the specified
选择器
,或
选择器
matches more than one sender or receiver.
先前,
getStats()
used success and failure callbacks to report the results to you, instead of using a promise.
This version of
getStats()
is obsolete; in addition, the data it returns is entirely different from the current specification, and the form of that data was never documented. This form of
getStats()
has been or will soon be removed from most browsers; you
should not use it, and should update existing code to use the new promise-based version
. Check the
浏览器兼容性
table to verify the state of this method.
promise = rtcPeerConnection.getStats(selector, successCallback, failureCallback)
选择器
可选
MediaStreamTrack
for which to gather statistics. If this is
null
(the default value), statistics will be gathered for the entire
RTCPeerConnection
.
successCallback
RTCStatsReport
with the collected statistics. No output is expected from the function.
failureCallback
DOMException
object describing the error which occurred. No return value is expected from the callback.
This example creates a periodic function using
setInterval()
that collects statistics for an
RTCPeerConnection
every second, generating an HTML-formatted report and inserting it into a specific element in the DOM.
window.setInterval(function() {
myPeerConnection.getStats(null).then(stats => {
let statsOutput = "";
stats.forEach(report => {
statsOutput += `<h2>Report: ${report.type}</h3>\n<strong>ID:</strong> ${report.id}<br>\n` +
`<strong>Timestamp:</strong> ${report.timestamp}<br>\n`;
// Now the statistics for this report; we intentially drop the ones we
// sorted to the top above
Object.keys(report).forEach(statName => {
if (statName !== "id" && statName !== "timestamp" && statName !== "type") {
statsOutput += `<strong>${statName}:</strong> ${report[statName]}<br>\n`;
}
});
});
document.querySelector(".stats-box").innerHTML = statsOutput;
});
}, 1000);
This works by calling
getStats()
, then, when the promise is resolved, iterates over the
RTCStats
objects on the returned
RTCStatsReport
. A section is created for each report with a header and all of the statistics below, with the type, ID, and timestamp handled specially to place them at the top of the list.
Once the
HTML
for the report is generated, it is injected into the element whose class is
"stats-box"
by setting its
innerHTML
特性。
| 规范 | 状态 | 注释 |
|---|---|---|
|
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCPeerConnection.getStats()' in that specification. |
候选推荐 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
getStats
|
Chrome
58
|
Edge 15 | Firefox Yes | IE No | Opera 45 | Safari 11 |
WebView Android
58
|
Chrome Android
58
|
Firefox Android Yes | Opera Android 43 | Safari iOS Yes |
Samsung Internet Android
7.0
|
可选
MediaStreamTrack
argument
|
Chrome 67 | Edge ≤79 | Firefox ? | IE No | Opera 54 | Safari ? | WebView Android 67 | Chrome Android 67 | Firefox Android ? | Opera Android 48 | Safari iOS ? | Samsung Internet Android 9.0 |
| Uses callbacks instead of promises 弃用 | Chrome 24 | Edge 15 | Firefox 27 — 66 | IE No | Opera 45 | Safari 11 | WebView Android Yes | Chrome Android Yes | Firefox Android ? | Opera Android 43 | Safari iOS ? | Samsung Internet Android Yes |
完整支持
不支持
兼容性未知
弃用。不要用于新网站。
见实现注意事项。
RTCPeerConnection
canTrickleIceCandidates
connectionState
currentLocalDescription
currentRemoteDescription
getDefaultIceServers()
iceConnectionState
iceGatheringState
localDescription
onaddstream
onconnectionstatechange
ondatachannel
onicecandidate
oniceconnectionstatechange
onicegatheringstatechange
onidentityresult
onidpassertionerror
onidpvalidationerror
onnegotiationneeded
onpeeridentity
onremovestream
onsignalingstatechange
ontrack
peerIdentity
pendingLocalDescription
pendingRemoteDescription
remoteDescription
sctp
signalingState
addIceCandidate()
addStream()
addTrack()
close()
createAnswer()
createDataChannel()
createOffer()
generateCertificate()
getConfiguration()
getIdentityAssertion()
getReceivers()
getSenders()
getStats()
getStreamById()
getTransceivers()
removeStream()
removeTrack()
restartIce()
setConfiguration()
setIdentityProvider()
setLocalDescription()
setRemoteDescription()
MediaDevices.getUserMedia()
Navigator.mediaDevices
RTCCertificate
RTCDTMFSender
RTCDTMFToneChangeEvent
RTCDataChannel
RTCDataChannelEvent
RTCDtlsTransport
RTCErrorEvent
RTCIceCandidate
RTCIceTransport
RTCPeerConnectionIceErrorEvent
RTCPeerConnectionIceEvent
RTCRtpReceiver
RTCRtpSender
RTCRtpTransceiver
RTCSctpTransport
RTCSessionDescription
RTCStatsEvent
RTCStatsReport
RTCTrackEvent