Skip to content

Commit

Permalink
Merge pull request #930 from webrtcHacks/stonehenge
Browse files Browse the repository at this point in the history
chrome: move getStats shim to separate function
  • Loading branch information
fippo authored Mar 4, 2019
2 parents c6c4d31 + d322f28 commit 04a9149
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 63 deletions.
1 change: 1 addition & 0 deletions src/js/adapter_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function adapterFactory({window} = {}, options = {
chromeShim.shimOnTrack(window);
chromeShim.shimAddTrackRemoveTrack(window);
chromeShim.shimGetSendersWithDtmf(window);
chromeShim.shimGetStats(window);
chromeShim.shimSenderReceiverGetStats(window);
chromeShim.fixNegotiationNeeded(window);

Expand Down
130 changes: 68 additions & 62 deletions src/js/chrome/chrome_shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,74 @@ export function shimGetSendersWithDtmf(window) {
}
}

export function shimGetStats(window) {
if (!window.RTCPeerConnection) {
return;
}

const origGetStats = window.RTCPeerConnection.prototype.getStats;
window.RTCPeerConnection.prototype.getStats = function(selector,
successCallback, errorCallback) {
const args = arguments;

// If selector is a function then we are in the old style stats so just
// pass back the original getStats format to avoid breaking old users.
if (arguments.length > 0 && typeof selector === 'function') {
return origGetStats.apply(this, arguments);
}

// When spec-style getStats is supported, return those when called with
// either no arguments or the selector argument is null.
if (origGetStats.length === 0 && (arguments.length === 0 ||
typeof arguments[0] !== 'function')) {
return origGetStats.apply(this, []);
}

const fixChromeStats_ = function(response) {
const standardReport = {};
const reports = response.result();
reports.forEach(report => {
const standardStats = {
id: report.id,
timestamp: report.timestamp,
type: {
localcandidate: 'local-candidate',
remotecandidate: 'remote-candidate'
}[report.type] || report.type
};
report.names().forEach(name => {
standardStats[name] = report.stat(name);
});
standardReport[standardStats.id] = standardStats;
});

return standardReport;
};

// shim getStats with maplike support
const makeMapStats = function(stats) {
return new Map(Object.keys(stats).map(key => [key, stats[key]]));
};

if (arguments.length >= 2) {
const successCallbackWrapper_ = function(response) {
args[1](makeMapStats(fixChromeStats_(response)));
};

return origGetStats.apply(this, [successCallbackWrapper_,
arguments[0]]);
}

// promise-support
return new Promise((resolve, reject) => {
origGetStats.apply(this, [
function(response) {
resolve(makeMapStats(fixChromeStats_(response)));
}, reject]);
}).then(successCallback, errorCallback);
};
}

export function shimSenderReceiverGetStats(window) {
if (!(typeof window === 'object' && window.RTCPeerConnection &&
window.RTCRtpSender && window.RTCRtpReceiver)) {
Expand Down Expand Up @@ -632,68 +700,6 @@ export function shimPeerConnection(window) {
return;
}

const origGetStats = window.RTCPeerConnection.prototype.getStats;
window.RTCPeerConnection.prototype.getStats = function(selector,
successCallback, errorCallback) {
const args = arguments;

// If selector is a function then we are in the old style stats so just
// pass back the original getStats format to avoid breaking old users.
if (arguments.length > 0 && typeof selector === 'function') {
return origGetStats.apply(this, arguments);
}

// When spec-style getStats is supported, return those when called with
// either no arguments or the selector argument is null.
if (origGetStats.length === 0 && (arguments.length === 0 ||
typeof arguments[0] !== 'function')) {
return origGetStats.apply(this, []);
}

const fixChromeStats_ = function(response) {
const standardReport = {};
const reports = response.result();
reports.forEach(report => {
const standardStats = {
id: report.id,
timestamp: report.timestamp,
type: {
localcandidate: 'local-candidate',
remotecandidate: 'remote-candidate'
}[report.type] || report.type
};
report.names().forEach(name => {
standardStats[name] = report.stat(name);
});
standardReport[standardStats.id] = standardStats;
});

return standardReport;
};

// shim getStats with maplike support
const makeMapStats = function(stats) {
return new Map(Object.keys(stats).map(key => [key, stats[key]]));
};

if (arguments.length >= 2) {
const successCallbackWrapper_ = function(response) {
args[1](makeMapStats(fixChromeStats_(response)));
};

return origGetStats.apply(this, [successCallbackWrapper_,
arguments[0]]);
}

// promise-support
return new Promise((resolve, reject) => {
origGetStats.apply(this, [
function(response) {
resolve(makeMapStats(fixChromeStats_(response)));
}, reject]);
}).then(successCallback, errorCallback);
};

// shim implicit creation of RTCSessionDescription/RTCIceCandidate
['setLocalDescription', 'setRemoteDescription', 'addIceCandidate']
.forEach(function(method) {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Chrome shim', () => {
]
});
};
shim.shimPeerConnection(window);
shim.shimGetStats(window);
pc = new window.RTCPeerConnection();
});

Expand Down

0 comments on commit 04a9149

Please sign in to comment.