From a4ecd3677dbe81bd7f974e9fbfb736cccba73980 Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Mon, 11 Jun 2018 13:19:52 +0200 Subject: [PATCH] openvidu-browser: WebRtcStats get selected ice candidate info --- .../WebRtcStats/WebRtcStats.ts | 49 +++++++++++++++++-- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/openvidu-browser/src/OpenViduInternal/WebRtcStats/WebRtcStats.ts b/openvidu-browser/src/OpenViduInternal/WebRtcStats/WebRtcStats.ts index fc7b02a8..3342b8a5 100644 --- a/openvidu-browser/src/OpenViduInternal/WebRtcStats/WebRtcStats.ts +++ b/openvidu-browser/src/OpenViduInternal/WebRtcStats/WebRtcStats.ts @@ -18,7 +18,6 @@ // tslint:disable:no-string-literal import { Stream } from '../../OpenVidu/Stream'; -import * as adapter from 'webrtc-adapter'; import platform = require('platform'); export class WebRtcStats { @@ -94,6 +93,41 @@ export class WebRtcStats { } } + public getSelectedIceCandidateInfo(): Promise { + return new Promise((resolve, reject) => { + this.getStatsAgnostic(this.stream.getRTCPeerConnection(), + (stats) => { + if (platform.name!.indexOf('Chrome') !== -1) { + let localCandidateId, remoteCandidateId, googCandidatePair; + const localCandidates = {}; + const remoteCandidates = {}; + for (const key in stats) { + const stat = stats[key]; + if (stat.type === 'localcandidate') { + localCandidates[stat.id] = stat; + } else if (stat.type === 'remotecandidate') { + remoteCandidates[stat.id] = stat; + } else if (stat.type === 'googCandidatePair' && (stat.googActiveConnection === 'true')) { + googCandidatePair = stat; + localCandidateId = stat.localCandidateId; + remoteCandidateId = stat.remoteCandidateId; + } + } + resolve({ + googCandidatePair, + localCandidate: localCandidates[localCandidateId], + remoteCandidate: remoteCandidates[remoteCandidateId] + }); + } else { + reject('Selected ICE candidate info only available for Chrome'); + } + }, + (error) => { + reject(error); + }); + }); + } + private sendStatsToHttpEndpoint(instrumentation): void { const sendPost = (json) => { @@ -306,12 +340,17 @@ export class WebRtcStats { } private standardizeReport(response) { + console.log(response); + const standardReport = {}; + if (platform.name!.indexOf('Firefox') !== -1) { + Object.keys(response).forEach(key => { + console.log(response[key]); + }); return response; } - const standardReport = {}; - response.result().forEach((report) => { + response.result().forEach(report => { const standardStats = { id: report.id, timestamp: report.timestamp, @@ -329,10 +368,10 @@ export class WebRtcStats { private getStatsAgnostic(pc, successCb, failureCb) { if (platform.name!.indexOf('Firefox') !== -1) { // getStats takes args in different order in Chrome and Firefox - return pc.getStats(null, (response) => { + return pc.getStats(null).then(response => { const report = this.standardizeReport(response); successCb(report); - }, failureCb); + }).catch(failureCb); } else if (platform.name!.indexOf('Chrome') !== -1) { // In Chrome, the first two arguments are reversed return pc.getStats((response) => {