You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SdpTransformUtil.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import * as transform from 'sdp-transform';
  2. /**
  3. * Parses the primary SSRC of given SSRC group.
  4. * @param {object} group the SSRC group object as defined by the 'sdp-transform'
  5. * @return {Number} the primary SSRC number
  6. */
  7. export function parsePrimarySSRC(group) {
  8. return parseInt(group.ssrcs.split(' ')[0]);
  9. }
  10. /**
  11. * Parses the secondary SSRC of given SSRC group.
  12. * @param {object} group the SSRC group object as defined by the 'sdp-transform'
  13. * @return {Number} the secondary SSRC number
  14. */
  15. export function parseSecondarySSRC(group) {
  16. return parseInt(group.ssrcs.split(' ')[1]);
  17. }
  18. /**
  19. * Tells how many distinct SSRCs are contained in given media line.
  20. * @param {Object} mLine the media line object as defined by 'sdp-transform' lib
  21. * @return {number}
  22. */
  23. function _getSSRCCount(mLine) {
  24. if (!mLine.ssrcs) {
  25. return 0;
  26. }
  27. return mLine.ssrcs
  28. .map(ssrcInfo => ssrcInfo.id)
  29. .filter((ssrc, index, array) => array.indexOf(ssrc) === index)
  30. .length;
  31. }
  32. /**
  33. * Utility class for SDP manipulation using the 'sdp-transform' library.
  34. *
  35. * Typical use usage scenario:
  36. *
  37. * const transformer = new SdpTransformWrap(rawSdp);
  38. * const videoMLine = transformer.selectMedia('video);
  39. * if (videoMLine) {
  40. * videoMLiner.addSSRCAttribute({
  41. * id: 2342343,
  42. * attribute: "cname",
  43. * value: "someCname"
  44. * });
  45. * rawSdp = transformer.toRawSdp();
  46. * }
  47. */
  48. export class SdpTransformWrap {
  49. /**
  50. * Creates new instance and parses the raw SDP into objects using
  51. * 'sdp-transform' lib.
  52. * @param {string} rawSDP the SDP in raw text format.
  53. */
  54. constructor(rawSDP) {
  55. this.parsedSDP = transform.parse(rawSDP);
  56. }
  57. /**
  58. * Selects the first media SDP of given name.
  59. * @param {string} mediaType the name of the media e.g. 'audio', 'video',
  60. * 'data'.
  61. * @return {MLineWrap|null} return {@link MLineWrap} instance for the media
  62. * line or <tt>null</tt> if not found. The object returned references
  63. * the underlying SDP state held by this <tt>SdpTransformWrap</tt> instance
  64. * (it's not a copy).
  65. */
  66. selectMedia(mediaType) {
  67. const selectedMLine
  68. = this.parsedSDP.media.find(mLine => mLine.type === mediaType);
  69. return selectedMLine ? new MLineWrap(selectedMLine) : null;
  70. }
  71. /**
  72. * Converts the currently stored SDP state in this instance to raw text SDP
  73. * format.
  74. * @return {string}
  75. */
  76. toRawSDP() {
  77. return transform.write(this.parsedSDP);
  78. }
  79. }
  80. /**
  81. * A wrapper around 'sdp-transform' media description object which provides
  82. * utility methods for common SDP/SSRC related operations.
  83. */
  84. class MLineWrap {
  85. /**
  86. * Creates new <tt>MLineWrap</t>>
  87. * @param {Object} mLine the media line object as defined by 'sdp-transform'
  88. * lib.
  89. */
  90. constructor(mLine) {
  91. if (!mLine) {
  92. throw new Error('mLine is undefined');
  93. }
  94. this.mLine = mLine;
  95. }
  96. get _ssrcs() {
  97. if (!this.mLine.ssrcs) {
  98. this.mLine.ssrcs = [];
  99. }
  100. return this.mLine.ssrcs;
  101. }
  102. /**
  103. * Returns the direction of the underlying media description.
  104. * @return {string} the media direction name as defined in the SDP.
  105. */
  106. get direction() {
  107. return this.mLine.direction;
  108. }
  109. /**
  110. * Modifies the direction of the underlying media description.
  111. * @param {string} direction the new direction to be set
  112. */
  113. set direction(direction) {
  114. this.mLine.direction = direction;
  115. }
  116. /**
  117. * Exposes the SSRC group array of the underlying media description object.
  118. * @return {Array.<Object>}
  119. */
  120. get ssrcGroups() {
  121. if (!this.mLine.ssrcGroups) {
  122. this.mLine.ssrcGroups = [];
  123. }
  124. return this.mLine.ssrcGroups;
  125. }
  126. /**
  127. * Modifies the SSRC groups array of the underlying media description
  128. * object.
  129. * @param {Array.<Object>} ssrcGroups
  130. */
  131. set ssrcGroups(ssrcGroups) {
  132. this.mLine.ssrcGroups = ssrcGroups;
  133. }
  134. /**
  135. * Obtains value from SSRC attribute.
  136. * @param {number} ssrcNumber the SSRC number for which attribute is to be
  137. * found
  138. * @param {string} attrName the name of the SSRC attribute to be found.
  139. * @return {string|undefined} the value of SSRC attribute or
  140. * <tt>undefined</tt> if no such attribute exists.
  141. */
  142. getSSRCAttrValue(ssrcNumber, attrName) {
  143. const attribute = this._ssrcs.find(
  144. ssrcObj => ssrcObj.id === ssrcNumber
  145. && ssrcObj.attribute === attrName);
  146. return attribute && attribute.value;
  147. }
  148. /**
  149. * Removes all attributes for given SSRC number.
  150. * @param {number} ssrcNum the SSRC number for which all attributes will be
  151. * removed.
  152. */
  153. removeSSRC(ssrcNum) {
  154. if (!this.mLine.ssrcs || !this.mLine.ssrcs.length) {
  155. return;
  156. }
  157. this.mLine.ssrcs
  158. = this.mLine.ssrcs.filter(ssrcObj => ssrcObj.id !== ssrcNum);
  159. }
  160. /**
  161. * Adds SSRC attribute
  162. * @param {object} ssrcObj the SSRC attribute object as defined in
  163. * the 'sdp-transform' lib.
  164. */
  165. addSSRCAttribute(ssrcObj) {
  166. this._ssrcs.push(ssrcObj);
  167. }
  168. /**
  169. * Finds a SSRC group matching both semantics and SSRCs in order.
  170. * @param {string} semantics the name of the semantics
  171. * @param {string} [ssrcs] group SSRCs as a string (like it's defined in
  172. * SSRC group object of the 'sdp-transform' lib) e.g. "1232546 342344 25434"
  173. * @return {object|undefined} the SSRC group object or <tt>undefined</tt> if
  174. * not found.
  175. */
  176. findGroup(semantics, ssrcs) {
  177. return this.ssrcGroups.find(
  178. group =>
  179. group.semantics === semantics
  180. && (!ssrcs || ssrcs === group.ssrcs));
  181. }
  182. /**
  183. * Finds all groups matching given semantic's name.
  184. * @param {string} semantics the name of the semantics
  185. * @return {Array.<object>} an array of SSRC group objects as defined by
  186. * the 'sdp-transform' lib.
  187. */
  188. findGroups(semantics) {
  189. return this.ssrcGroups.filter(
  190. group => group.semantics === semantics);
  191. }
  192. /**
  193. * Finds all groups matching given semantic's name and group's primary SSRC.
  194. * @param {string} semantics the name of the semantics
  195. * @param {number} primarySSRC the primary SSRC number to be matched
  196. * @return {Object} SSRC group object as defined by the 'sdp-transform' lib.
  197. */
  198. findGroupByPrimarySSRC(semantics, primarySSRC) {
  199. return this.ssrcGroups.find(
  200. group => group.semantics === semantics
  201. && parsePrimarySSRC(group) === primarySSRC);
  202. }
  203. /**
  204. * Gets the SSRC count for the underlying media description.
  205. * @return {number}
  206. */
  207. getSSRCCount() {
  208. return _getSSRCCount(this.mLine);
  209. }
  210. /**
  211. * Checks whether the underlying media description contains any SSRC groups.
  212. * @return {boolean} <tt>true</tt> if there are any SSRC groups or
  213. * <tt>false</tt> otherwise.
  214. */
  215. containsAnySSRCGroups() {
  216. return this.mLine.ssrcGroups !== undefined;
  217. }
  218. /**
  219. * Finds the primary video SSRC.
  220. * @returns {number|undefined} the primary video ssrc
  221. * @throws Error if the underlying media description is not a video
  222. */
  223. getPrimaryVideoSsrc() {
  224. const mediaType = this.mLine.type;
  225. if (mediaType !== 'video') {
  226. throw new Error(
  227. `getPrimarySsrc doesn't work with '${mediaType}'`);
  228. }
  229. const numSsrcs = _getSSRCCount(this.mLine);
  230. if (numSsrcs === 1) {
  231. // Not using _ssrcs on purpose here
  232. return this.mLine.ssrcs[0].id;
  233. }
  234. // Look for a SIM or FID group
  235. if (this.mLine.ssrcGroups) {
  236. const simGroup = this.findGroup('SIM');
  237. if (simGroup) {
  238. return parsePrimarySSRC(simGroup);
  239. }
  240. const fidGroup = this.findGroup('FID');
  241. if (fidGroup) {
  242. return parsePrimarySSRC(fidGroup);
  243. }
  244. }
  245. }
  246. /**
  247. * Obtains RTX SSRC from the underlying video description (the
  248. * secondary SSRC of the first "FID" group found)
  249. * @param {number} primarySsrc the video ssrc for which to find the
  250. * corresponding rtx ssrc
  251. * @returns {number|undefined} the rtx ssrc (or undefined if there isn't
  252. * one)
  253. */
  254. getRtxSSRC(primarySsrc) {
  255. const fidGroup = this.findGroupByPrimarySSRC('FID', primarySsrc);
  256. return fidGroup && parseSecondarySSRC(fidGroup);
  257. }
  258. /**
  259. * Obtains all SSRCs contained in the underlying media description.
  260. * @return {Array.<number>} an array with all SSRC as numbers.
  261. */
  262. getSSRCs() {
  263. return this._ssrcs
  264. .map(ssrcInfo => ssrcInfo.id)
  265. .filter((ssrc, index, array) => array.indexOf(ssrc) === index);
  266. }
  267. /**
  268. * Obtains primary video SSRCs.
  269. * @return {Array.<number>} an array of all primary video SSRCs as numbers.
  270. * @throws Error if the wrapped media description is not a video.
  271. */
  272. getPrimaryVideoSSRCs() {
  273. const mediaType = this.mLine.type;
  274. if (mediaType !== 'video') {
  275. throw new Error(
  276. `getPrimaryVideoSSRCs doesn't work with ${mediaType}`);
  277. }
  278. const videoSSRCs = this.getSSRCs();
  279. for (const ssrcGroupInfo of this.ssrcGroups) {
  280. // Right now, FID groups are the only ones we parse to
  281. // disqualify streams. If/when others arise we'll
  282. // need to add support for them here
  283. if (ssrcGroupInfo.semantics === 'FID') {
  284. // secondary FID streams should be filtered out
  285. const secondarySsrc = parseSecondarySSRC(ssrcGroupInfo);
  286. videoSSRCs.splice(
  287. videoSSRCs.indexOf(secondarySsrc), 1);
  288. }
  289. }
  290. return videoSSRCs;
  291. }
  292. /**
  293. * Dumps all SSRC groups of this media description to JSON.
  294. */
  295. dumpSSRCGroups() {
  296. return JSON.stringify(this.mLine.ssrcGroups);
  297. }
  298. /**
  299. * Removes all SSRC groups which contain given SSRC number at any position.
  300. * @param {number} ssrc the SSRC for which all matching groups are to be
  301. * removed.
  302. */
  303. removeGroupsWithSSRC(ssrc) {
  304. if (!this.mLine.ssrcGroups) {
  305. return;
  306. }
  307. this.mLine.ssrcGroups = this.mLine.ssrcGroups
  308. .filter(groupInfo => groupInfo.ssrcs.indexOf(`${ssrc}`) === -1);
  309. }
  310. /**
  311. * Removes groups that match given semantics.
  312. * @param {string} semantics e.g. "SIM" or "FID"
  313. */
  314. removeGroupsBySemantics(semantics) {
  315. if (!this.mLine.ssrcGroups) {
  316. return;
  317. }
  318. this.mLine.ssrcGroups
  319. = this.mLine.ssrcGroups
  320. .filter(groupInfo => groupInfo.semantics !== semantics);
  321. }
  322. /**
  323. * Replaces SSRC (does not affect SSRC groups, but only attributes).
  324. * @param {number} oldSSRC the old SSRC number
  325. * @param {number} newSSRC the new SSRC number
  326. */
  327. replaceSSRC(oldSSRC, newSSRC) {
  328. if (this.mLine.ssrcs) {
  329. this.mLine.ssrcs.forEach(ssrcInfo => {
  330. if (ssrcInfo.id === oldSSRC) {
  331. ssrcInfo.id = newSSRC;
  332. }
  333. });
  334. }
  335. }
  336. /**
  337. * Adds given SSRC group to this media description.
  338. * @param {object} group the SSRC group object as defined by
  339. * the 'sdp-transform' lib.
  340. */
  341. addSSRCGroup(group) {
  342. this.ssrcGroups.push(group);
  343. }
  344. }