Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove legacy getStats polyfill #1149

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ declare module "webrtc-adapter" {
shimMediaStream(window: Window): void;
shimOnTrack(window: Window): void;
shimGetSendersWithDtmf(window: Window): void;
shimGetStats(window: Window): void;
shimSenderReceiverGetStats(window: Window): void;
shimAddTrackRemoveTrackWithNative(window: Window): void;
shimAddTrackRemoveTrack(window: Window): void;
Expand Down
1 change: 0 additions & 1 deletion src/js/adapter_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export function adapterFactory({window} = {}, options = {
chromeShim.shimOnTrack(window, browserDetails);
chromeShim.shimAddTrackRemoveTrack(window, browserDetails);
chromeShim.shimGetSendersWithDtmf(window, browserDetails);
chromeShim.shimGetStats(window, browserDetails);
chromeShim.shimSenderReceiverGetStats(window, browserDetails);
chromeShim.fixNegotiationNeeded(window, browserDetails);

Expand Down
67 changes: 0 additions & 67 deletions src/js/chrome/chrome_shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,73 +188,6 @@ export function shimGetSendersWithDtmf(window) {
}
}

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

const origGetStats = window.RTCPeerConnection.prototype.getStats;
window.RTCPeerConnection.prototype.getStats = function getStats() {
const [selector, onSucc, onErr] = 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 selector !== '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) {
onSucc(makeMapStats(fixChromeStats_(response)));
};

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

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

export function shimSenderReceiverGetStats(window) {
if (!(typeof window === 'object' && window.RTCPeerConnection &&
window.RTCRtpSender && window.RTCRtpReceiver)) {
Expand Down
51 changes: 0 additions & 51 deletions test/unit/chrome.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ RTCLegacyStatsReport.prototype.stat = function(name) {
return this._data[name];
};

function makeLegacyStatsReport(type, data) {
const report = new RTCLegacyStatsReport();
report.type = type;
report._data = data;
return report;
}

describe('Chrome shim', () => {
const shim = require('../../dist/chrome/chrome_shim');
let window;
Expand All @@ -42,50 +35,6 @@ describe('Chrome shim', () => {
};
});

describe('legacy getStats', () => {
let pc;
beforeEach(() => {
window.RTCPeerConnection.prototype.getStats = function(cb) {
setTimeout(cb, 0, {
result: () => [
makeLegacyStatsReport('localcandidate', {
portNumber: '31337',
ipAddress: '8.8.8.8',
transport: 'udp',
candidateType: 'host',
priority: '12345'
}),
]
});
};
shim.shimGetStats(window);
pc = new window.RTCPeerConnection();
});

it('returns a promise', () => {
return pc.getStats();
});

it('returns chrome legacy getStats when called with a callback', (done) => {
pc.getStats((result) => {
expect(result).toHaveProperty('result');
const report = result.result()[0];
expect(report).toHaveProperty('id');
expect(report).toHaveProperty('type');
expect(report).toHaveProperty('timestamp');
expect(report).toHaveProperty('stat');
done();
});
});

it('is translated into a Map', () => {
return pc.getStats()
.then(result => {
expect(result).toBeInstanceOf(Map);
});
});
});

describe('PeerConnection shim', () => {
it('fail silently if RTCPeerConnection is not present', () => {
window = {};
Expand Down