react-native-webrtc
react-native-webrtc copied to clipboard
feat: implement clone on MediaStreamTrack and MediaStream
Hey @saghul Would it be possible to have clone implemented on the MediaStream and MediaStreamTrack?
current situation
current implementation is to throw an error that it isn't implemented.
Referencing MDN for context. https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/clone https://developer.mozilla.org/en-US/docs/Web/API/MediaStream/clone
See also https://w3c.github.io/mediacapture-main/#mediastreamtrack https://w3c.github.io/mediacapture-main/#dom-mediastream-clone
criteria
MediaStream
- [ ] 1. Let streamClone be a newly constructed MediaStream object.
- [ ] 2. Initialize streamClone.MediaStream.id to a newly generated value.
- [ ] 3. Clone each track in this MediaStream object and add the result to streamClone's track set.-
- [ ] 4. Return streamClone.
MediaStreamTrack
- [ ] 1. Let track be the MediaStreamTrack object to be cloned.
- [ ] 2. Let trackClone be a newly constructed MediaStreamTrack object.
- [ ] 3. Initialize trackClone.id to a newly generated value.
- [ ] 4. Initialize trackClone's kind, label, readyState, and enabled attributes by copying the corresponding values from track.
- [ ] 5. Let trackClone's underlying source be the source of track.
- [ ] 6. Set trackClone's constraints to the active constrains of track.
- [ ] 7. Return trackClone.
Proposed Solution
I think we can solve this is in two passes. If we relax requirement MediaStreamTrack.3 - which i believe is already the case because AFAIK the track.Id seems stable to me when copied across MediaStreams. We should be able to use the constructor methods to the bulk of the cloning.
MediaStream.js
clone() {
return new MediaStream(this);
}
MediaStreamTrack.js
clone() {
return new MediaStreamTrack({
id: this.id,
kind: this.kind,
label: this.label,
muted: this.muted,
remote: this.remote,
constraints: this._constraints,
enabled: this._enabled,
settings: this._settings
});
}
and in the second pass implement the fact that track should act more like MediaStream in that the track.ids are unique but can be pointing to the same source. AFAIK, the track.id is the same id that's used to look up the PrivateTrack so making that unique would cause the id to lose the video source.
I wasn't sure how to tackle that one, because there are some things that are implemented up stream, so I wasn't sure the effect. implementing this in two parts should be low risk if anybody is assuming the track.id's don't change. And should unblock us in the short term.
The Why?
Twilio-video.js assumes that clone is implemented. So we need this feature and would be willing to work on it.