import * as transform from 'sdp-transform';
/**
* Parses the primary SSRC of given SSRC group.
* @param {object} group the SSRC group object as defined by the 'sdp-transform'
* @return {Number} the primary SSRC number
*/
export function parsePrimarySSRC(group) {
return parseInt(group.ssrcs.split(' ')[0]);
}
/**
* Parses the secondary SSRC of given SSRC group.
* @param {object} group the SSRC group object as defined by the 'sdp-transform'
* @return {Number} the secondary SSRC number
*/
export function parseSecondarySSRC(group) {
return parseInt(group.ssrcs.split(' ')[1]);
}
/**
* Tells how many distinct SSRCs are contained in given media line.
* @param {Object} mLine the media line object as defined by 'sdp-transform' lib
* @return {number}
*/
function _getSSRCCount(mLine) {
if (!mLine.ssrcs) {
return 0;
}
return mLine.ssrcs
.map(ssrcInfo => ssrcInfo.id)
.filter((ssrc, index, array) => array.indexOf(ssrc) === index)
.length;
}
/**
* Utility class for SDP manipulation using the 'sdp-transform' library.
*
* Typical use usage scenario:
*
* const transformer = new SdpTransformWrap(rawSdp);
* const videoMLine = transformer.selectMedia('video);
* if (videoMLine) {
* videoMLiner.addSSRCAttribute({
* id: 2342343,
* attribute: "cname",
* value: "someCname"
* });
* rawSdp = transformer.toRawSdp();
* }
*/
export class SdpTransformWrap {
/**
* Creates new instance and parses the raw SDP into objects using
* 'sdp-transform' lib.
* @param {string} rawSDP the SDP in raw text format.
*/
constructor(rawSDP) {
this.parsedSDP = transform.parse(rawSDP);
}
/**
* Selects the first media SDP of given name.
* @param {string} mediaType the name of the media e.g. 'audio', 'video',
* 'data'.
* @return {MLineWrap|null} return {@link MLineWrap} instance for the media
* line or null if not found. The object returned references
* the underlying SDP state held by this SdpTransformWrap instance
* (it's not a copy).
*/
selectMedia(mediaType) {
const selectedMLine
= this.parsedSDP.media.find(mLine => mLine.type === mediaType);
return selectedMLine ? new MLineWrap(selectedMLine) : null;
}
/**
* Converts the currently stored SDP state in this instance to raw text SDP
* format.
* @return {string}
*/
toRawSDP() {
return transform.write(this.parsedSDP);
}
}
/**
* A wrapper around 'sdp-transform' media description object which provides
* utility methods for common SDP/SSRC related operations.
*/
class MLineWrap {
/**
* Creates new MLineWrap>
* @param {Object} mLine the media line object as defined by 'sdp-transform'
* lib.
*/
constructor(mLine) {
if (!mLine) {
throw new Error('mLine is undefined');
}
this.mLine = mLine;
}
get _ssrcs() {
if (!this.mLine.ssrcs) {
this.mLine.ssrcs = [];
}
return this.mLine.ssrcs;
}
/**
* Returns the direction of the underlying media description.
* @return {string} the media direction name as defined in the SDP.
*/
get direction() {
return this.mLine.direction;
}
/**
* Modifies the direction of the underlying media description.
* @param {string} direction the new direction to be set
*/
set direction(direction) {
this.mLine.direction = direction;
}
/**
* Exposes the SSRC group array of the underlying media description object.
* @return {Array.