2019-06-04 02:54:30 +08:00
|
|
|
import Interop from '@jitsi/sdp-interop';
|
|
|
|
import transform from 'sdp-transform';
|
2019-05-22 00:48:01 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
|
|
|
|
|
|
|
// sdp-interop library for unified-plan <-> plan-b translation
|
|
|
|
const interop = new Interop.Interop();
|
|
|
|
|
|
|
|
// Some heuristics to determine if the input SDP is Unified Plan
|
|
|
|
const isUnifiedPlan = (sdp) => {
|
|
|
|
const parsedSDP = transform.parse(sdp);
|
2019-06-04 02:54:30 +08:00
|
|
|
if (parsedSDP.media.length <= 3 && parsedSDP.media.every(m => ['video', 'audio', 'data'].indexOf(m.mid) !== -1)) {
|
2019-05-22 00:48:01 +08:00
|
|
|
logger.info({ logCode: 'sdp_utils_not_unified_plan' }, 'SDP does not look like Unified Plan');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info({ logCode: 'sdp_utils_is_unified_plan' }, 'SDP looks like Unified Plan');
|
2019-05-22 01:38:22 +08:00
|
|
|
|
2019-05-22 00:48:01 +08:00
|
|
|
return true;
|
2019-06-04 02:54:30 +08:00
|
|
|
};
|
2019-05-22 00:48:01 +08:00
|
|
|
|
|
|
|
// Some heuristics to determine if the input SDP is Plan B
|
|
|
|
const isPlanB = (sdp) => {
|
|
|
|
const parsedSDP = transform.parse(sdp);
|
2019-06-04 02:54:30 +08:00
|
|
|
if (parsedSDP.media.length > 3 || !parsedSDP.media.every(m => ['video', 'audio', 'data'].indexOf(m.mid) !== -1)) {
|
2019-05-22 00:48:01 +08:00
|
|
|
logger.info({ logCode: 'sdp_utils_not_plan_b' }, 'SDP does not look like Plan B');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info({ logCode: 'sdp_utils_is_plan_b' }, 'SDP looks like Plan B');
|
|
|
|
|
|
|
|
return true;
|
2019-06-04 02:54:30 +08:00
|
|
|
};
|
2019-05-22 00:48:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Specific method for translating FS SDPs from Plan B to Unified Plan (vice-versa)
|
|
|
|
const toPlanB = (unifiedPlanSDP) => {
|
|
|
|
const planBSDP = interop.toPlanB(unifiedPlanSDP);
|
|
|
|
logger.info({ logCode: 'sdp_utils_unified_plan_to_plan_b' }, `Converted Unified Plan to Plan B ${JSON.stringify(planBSDP)}`);
|
|
|
|
return planBSDP;
|
2019-06-04 02:54:30 +08:00
|
|
|
};
|
2019-05-22 00:48:01 +08:00
|
|
|
|
|
|
|
const toUnifiedPlan = (planBSDP) => {
|
|
|
|
const unifiedPlanSDP = interop.toUnifiedPlan(planBSDP);
|
|
|
|
logger.info({ logCode: 'sdp_utils_plan_b_to_unified_plan' }, `Converted Plan B to Unified Plan ${JSON.stringify(unifiedPlanSDP)}`);
|
|
|
|
return unifiedPlanSDP;
|
2019-06-04 02:54:30 +08:00
|
|
|
};
|
2019-05-22 00:48:01 +08:00
|
|
|
|
2019-06-04 02:54:30 +08:00
|
|
|
const stripMDnsCandidates = (sdp) => {
|
|
|
|
const parsedSDP = transform.parse(sdp);
|
|
|
|
let strippedCandidates = 0;
|
|
|
|
parsedSDP.media.forEach((media) => {
|
2019-06-21 03:13:55 +08:00
|
|
|
if (media.candidates) {
|
|
|
|
media.candidates = media.candidates.filter((candidate) => {
|
|
|
|
if (candidate.ip && candidate.ip.indexOf('.local') === -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
strippedCandidates += 1;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2019-06-04 02:54:30 +08:00
|
|
|
});
|
|
|
|
if (strippedCandidates > 0) {
|
|
|
|
logger.info({ logCode: 'sdp_utils_mdns_candidate_strip' }, `Stripped ${strippedCandidates} mDNS candidates`);
|
|
|
|
}
|
|
|
|
return transform.write(parsedSDP);
|
|
|
|
};
|
|
|
|
|
|
|
|
export {
|
|
|
|
interop, isUnifiedPlan, toPlanB, toUnifiedPlan, stripMDnsCandidates,
|
|
|
|
};
|