Bug 1531803 - Part 6: Fire track events when that track has been added to a stream. r=jib,smaug

Differential Revision: https://phabricator.services.mozilla.com/D25799

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Byron Campen [:bwc] 2019-04-29 15:52:03 +00:00
parent fc0aece742
commit f28444a083
3 changed files with 68 additions and 71 deletions

View File

@ -1337,28 +1337,51 @@ class RTCPeerConnection {
}
_processTrackAdditionsAndRemovals() {
let postProcessing = {
updateStreamFunctions: [],
muteTracks: [],
trackEvents: [],
};
const removeList = [];
const addList = [];
const muteTracks = [];
const trackEventInits = [];
for (let transceiver of this._transceivers) {
for (const transceiver of this._transceivers) {
transceiver.receiver.processTrackAdditionsAndRemovals(transceiver,
postProcessing);
{removeList, addList, muteTracks, trackEventInits});
}
for (let f of postProcessing.updateStreamFunctions) {
f();
muteTracks.forEach(track => {
// Check this as late as possible, in case JS has messed with this state.
if (!track.muted) {
track.mutedChanged(true);
}
});
for (const {stream, track} of removeList) {
// Check this as late as possible, in case JS messes with the track lists.
if (stream.getTracks().includes(track)) {
stream.removeTrack(track);
// Removing tracks from JS does not result in the stream getting a
// removetrack event, so we need to do that here.
stream.dispatchEvent(
new this._win.MediaStreamTrackEvent("removetrack", { track }));
}
}
for (let t of postProcessing.muteTracks) {
t.mutedChanged(true);
for (const {stream, track} of addList) {
// Check this as late as possible, in case JS messes with the track lists.
if (!stream.getTracks().includes(track)) {
stream.addTrack(track);
// Adding tracks from JS does not result in the stream getting an
// addtrack event, so we need to do that here.
stream.dispatchEvent(
new this._win.MediaStreamTrackEvent("addtrack", { track }));
}
}
for (let ev of postProcessing.trackEvents) {
this.dispatchEvent(ev);
}
trackEventInits.forEach(init => {
this.dispatchEvent(new this._win.RTCTrackEvent("track", init));
// Fire legacy event as well for a little bit.
this.dispatchEvent(new this._win.MediaStreamTrackEvent("addtrack",
{ track: init.track }));
});
}
// TODO(Bug 1241291): Legacy event, remove eventually
@ -2090,15 +2113,15 @@ setupPrototype(RTCRtpSender, {
class RTCRtpReceiver {
constructor(pc, transceiverImpl) {
// We do not set the track here; that is done when _transceiverImpl is set
Object.assign(this,
{
_pc: pc,
_transceiverImpl: transceiverImpl,
track: transceiverImpl.getReceiveTrack(),
_remoteSetSendBit: false,
_ontrackFired: false,
streamIds: [],
_recvBit: false,
_oldRecvBit: false,
_streams: [],
_oldstreams: [],
// Sync and contributing sources must be kept cached so that timestamps
// remain stable, as the timestamp offset can vary
// note key = entry.source + entry.sourceType
@ -2191,64 +2214,39 @@ class RTCRtpReceiver {
}
setStreamIds(streamIds) {
this.streamIds = streamIds;
this._streams = streamIds.map(id => this._pc._getOrCreateStream(id));
}
setRemoteSendBit(sendBit) {
this._remoteSetSendBit = sendBit;
setRecvBit(recvBit) {
this._recvBit = recvBit;
}
processTrackAdditionsAndRemovals(transceiver,
{updateStreamFunctions, muteTracks, trackEvents}) {
let streamsWithTrack = this.streamIds
.map(id => this._pc._getOrCreateStream(id));
{removeList, addList, muteTracks, trackEventInits}) {
const receiver = this.__DOM_IMPL__;
const track = this.track;
const streams = this._streams;
const streamsAdded = streams.filter(s => !this._oldstreams.includes(s));
const streamsRemoved = this._oldstreams.filter(s => !streams.includes(s));
let streamsWithoutTrack = this._pc.getRemoteStreams()
.filter(s => !this.streamIds.includes(s.id));
addList.push(...streamsAdded.map(stream => ({stream, track})));
removeList.push(...streamsRemoved.map(stream => ({stream, track})));
this._oldstreams = this._streams;
updateStreamFunctions.push(...streamsWithTrack.map(stream => () => {
if (!stream.getTracks().includes(this.track)) {
stream.addTrack(this.track);
// Adding tracks from JS does not result in the stream getting
// onaddtrack, so we need to do that here.
stream.dispatchEvent(
new this._pc._win.MediaStreamTrackEvent(
"addtrack", { track: this.track }));
let needsTrackEvent = (streamsAdded.length != 0);
if (this._recvBit != this._oldRecvBit) {
this._oldRecvBit = this._recvBit;
if (this._recvBit) {
// New track, set in case streamsAdded is empty
needsTrackEvent = true;
} else {
muteTracks.push(track);
}
}));
}
updateStreamFunctions.push(...streamsWithoutTrack.map(stream => () => {
// Content JS might remove this track from the stream before this function fires (ugh)
if (stream.getTracks().includes(this.track)) {
stream.removeTrack(this.track);
// Removing tracks from JS does not result in the stream getting
// onremovetrack, so we need to do that here.
stream.dispatchEvent(
new this._pc._win.MediaStreamTrackEvent(
"removetrack", { track: this.track }));
}
}));
if (!this._remoteSetSendBit) {
// remote used "recvonly" or "inactive"
this._ontrackFired = false;
if (!this.track.muted) {
muteTracks.push(this.track);
}
} else if (!this._ontrackFired) {
// remote used "sendrecv" or "sendonly", and we haven't fired ontrack
let ev = new this._pc._win.RTCTrackEvent("track", {
receiver: this.__DOM_IMPL__,
track: this.track,
streams: streamsWithTrack,
transceiver });
trackEvents.push(ev);
this._ontrackFired = true;
// Fire legacy event as well for a little bit.
ev = new this._pc._win.MediaStreamTrackEvent("addtrack",
{ track: this.track });
trackEvents.push(ev);
if (needsTrackEvent) {
trackEventInits.push({track, streams, receiver, transceiver});
}
}
}
@ -2261,7 +2259,7 @@ setupPrototype(RTCRtpReceiver, {
class RTCRtpTransceiver {
constructor(pc, transceiverImpl, init, kind, sendTrack) {
let receiver = pc._win.RTCRtpReceiver._create(
pc._win, new RTCRtpReceiver(pc, transceiverImpl, kind));
pc._win, new RTCRtpReceiver(pc, transceiverImpl));
let streams = (init && init.streams) || [];
let sender = pc._win.RTCRtpSender._create(
pc._win, new RTCRtpSender(pc, transceiverImpl, this, sendTrack, kind, streams));

View File

@ -20,7 +20,7 @@ interface RTCRtpReceiver {
[ChromeOnly]
void setStreamIds(sequence<DOMString> streamIds);
[ChromeOnly]
void setRemoteSendBit(boolean sendBit);
void setRecvBit(boolean recvBit);
[ChromeOnly]
void processTrackAdditionsAndRemovals(
RTCRtpTransceiver transceiver,

View File

@ -494,8 +494,7 @@ void TransceiverImpl::SyncWithJS(dom::RTCRtpTransceiver& aJsTransceiver,
return;
}
receiver->SetRemoteSendBit(mJsepTransceiver->mRecvTrack.GetRemoteSetSendBit(),
aRv);
receiver->SetRecvBit(mJsepTransceiver->mRecvTrack.GetRemoteSetSendBit(), aRv);
if (aRv.Failed()) {
return;
}