RTCRtpSender
方法
replaceTrack()
replaces the track currently being used as the sender's source with a new
MediaStreamTrack
.
The new track must be of the same media kind (audio, video, etc) and switching the track should not require negotiation.
Among the use cases for
replaceTrack()
is the common need to switch between the rear- and front-facing cameras on a phone. With
replaceTrack()
, you can simply have a track object for each camera and switch between the two as needed. See the example
Switching cameras
下文。
trackReplacedPromise = sender.replaceTrack(newTrack);
newTrack
可选
MediaStreamTrack
specifying the track with which to replace the
RTCRtpSender
's current source track. The new track's
kind
must be the same as the current track's, or the replace track request will fail.
A
Promise
which is fulfilled once the track has been successfully replaced. The promise is rejected if the track cannot be replaced for any reason; this is commonly because the change would require renegotiation of the codec, which is not allowed (see
Things that require negotiation
).
若
newTrack
was omitted or was
null
,
replaceTrack()
simply stops the sender. No negotiation is required in this case.
When the promise is fulfilled, the fulfillment handler receives a value of
undefined
.
If the returned promise is rejected, one of the following exceptions is provided to the rejection handler:
InvalidModificationError
RTCRtpSende
r
's current track with the new one would require negotiation.
InvalidStateError
The track on which this method was called is stopped rather than running.
TypeError
kind
doesn't match the original track.
Not all track replacements require renegotiation. In fact, even changes that seem huge can be done without requiring negotation. Here are the changes that can trigger negotiaton:
// example to change video camera, suppose selected value saved into window.selectedCamera
navigator.mediaDevices
.getUserMedia({
video: {
deviceId: {
exact: window.selectedCamera
}
}
})
.then(function(stream) {
let videoTrack = stream.getVideoTracks()[0];
PCs.forEach(function(pc) {
var sender = pc.getSenders().find(function(s) {
return s.track.kind == videoTrack.kind;
});
console.log('found sender:', sender);
sender.replaceTrack(videoTrack);
});
})
.catch(function(err) {
console.error('Error happens:', err);
});
| 规范 | 状态 | 注释 |
|---|---|---|
|
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCRtpSender.replaceTrack()' in that specification. |
候选推荐 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
replaceTrack()
|
Chrome 65 | Edge ≤18 | Firefox Yes | IE 不支持 No | Opera Yes | Safari Yes | WebView Android 65 | Chrome Android 65 | Firefox Android Yes | Opera Android Yes | Safari iOS Yes | Samsung Internet Android 9.0 |
完整支持
不支持
RTCRtpSender
getParameters()
getStats()
replaceTrack()
setParameters()
MediaDevices.getUserMedia()
Navigator.mediaDevices
RTCCertificate
RTCDTMFSender
RTCDTMFToneChangeEvent
RTCDataChannel
RTCDataChannelEvent
RTCDtlsTransport
RTCErrorEvent
RTCIceCandidate
RTCIceTransport
RTCPeerConnection
RTCPeerConnectionIceErrorEvent
RTCPeerConnectionIceEvent
RTCRtpReceiver
RTCRtpTransceiver
RTCSctpTransport
RTCSessionDescription
RTCStatsEvent
RTCStatsReport
RTCTrackEvent