Skip to content

Instantly share code, notes, and snippets.

@voltrevo
Last active June 20, 2016 07:11
Show Gist options
  • Select an option

  • Save voltrevo/f617f1b382bb3c91f618f285b6925de8 to your computer and use it in GitHub Desktop.

Select an option

Save voltrevo/f617f1b382bb3c91f618f285b6925de8 to your computer and use it in GitHub Desktop.
'use strict';
function resolvePromises(val) {
if (Array.isArray(val)) {
return Promise.all(val.map(resolvePromises));
}
if (typeof val === 'object' && typeof val.then !== 'function') {
return Promise.all(Object.keys(val).map(key => {
return resolvePromises(val[key]).then(value => ({key, value}))
})).then(keyValuePairs => {
const obj = {};
keyValuePairs.forEach(({key, value}) => {
obj[key] = value;
});
return obj;
});
}
return Promise.resolve(val);
}
function getSubscriberInfo(subscriber) {
return {
id: subscriber.widgetId,
stats: new Promise((resolve, reject) => subscriber.getStats((err, stats) => {
if (err) {
reject(err);
return;
}
resolve(stats);
}))
};
}
function getStreamInfo(stream) {
const info = {};
info.id = stream.id;
info.channels = stream.channel.map(channel => ({
id: channel.id,
type: channel.type,
active: channel.active,
frameRate: channel.frameRate,
width: channel.width,
height: channel.height
}));
info.creationTime = stream.creationTime;
info.subscribers = OT.subscribers.where(sub => sub.stream === stream).map(getSubscriberInfo);
return info;
}
function getConnectionInfo(connection) {
const info = {};
info.id = connection.id;
const session = OT.sessions.where(s => s.connections.where(1).indexOf(connection) !== -1)[0];
info.mine = (connection === session.connection);
info.streams = session
.streams
.where(stream => stream.connection === connection)
.map(getStreamInfo)
;
return info;
}
function getExtendedSessionInfo(session) {
const info = {};
info.sessionInfo = session.sessionInfo;
info.connections = session
.connections
.where(conn => !conn.id.match(/^symphony\./))
.map(getConnectionInfo)
;
return info;
}
if (window.OT) {
resolvePromises(OT.sessions.where(1).map(getExtendedSessionInfo)).then(extSessionInfos => {
window.open('data:text/json;base64,' + btoa(JSON.stringify(extSessionInfos, null, 2)));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment