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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 => group.semantics === semantics
  179. && !ssrcs || ssrcs === group.ssrcs);
  180. }
  181. /**
  182. * Finds all groups matching given semantic's name.
  183. * @param {string} semantics the name of the semantics
  184. * @return {Array.<object>} an array of SSRC group objects as defined by
  185. * the 'sdp-transform' lib.
  186. */
  187. findGroups(semantics) {
  188. return this.ssrcGroups.filter(
  189. group => group.semantics === semantics);
  190. }
  191. /**
  192. * Finds all groups matching given semantic's name and group's primary SSRC.
  193. * @param {string} semantics the name of the semantics
  194. * @param {number} primarySSRC the primary SSRC number to be matched
  195. * @return {Object} SSRC group object as defined by the 'sdp-transform' lib.
  196. */
  197. findGroupByPrimarySSRC(semantics, primarySSRC) {
  198. return this.ssrcGroups.find(
  199. group => group.semantics === semantics
  200. && parsePrimarySSRC(group) === primarySSRC);
  201. }
  202. /**
  203. * Gets the SSRC count for the underlying media description.
  204. * @return {number}
  205. */
  206. getSSRCCount() {
  207. return _getSSRCCount(this.mLine);
  208. }
  209. /**
  210. * Checks whether the underlying media description contains any SSRC groups.
  211. * @return {boolean} <tt>true</tt> if there are any SSRC groups or
  212. * <tt>false</tt> otherwise.
  213. */
  214. containsAnySSRCGroups() {
  215. return this.mLine.ssrcGroups !== undefined;
  216. }
  217. /**
  218. * Finds the primary video SSRC.
  219. * @returns {number|undefined} the primary video ssrc
  220. * @throws Error if the underlying media description is not a video
  221. */
  222. getPrimaryVideoSsrc() {
  223. const mediaType = this.mLine.type;
  224. if (mediaType !== 'video') {
  225. throw new Error(
  226. `getPrimarySsrc doesn't work with '${mediaType}'`);
  227. }
  228. const numSsrcs = _getSSRCCount(this.mLine);
  229. if (numSsrcs === 1) {
  230. // Not using _ssrcs on purpose here
  231. return this.mLine.ssrcs[0].id;
  232. }
  233. // Look for a SIM or FID group
  234. if (this.mLine.ssrcGroups) {
  235. const simGroup = this.findGroup('SIM');
  236. if (simGroup) {
  237. return parsePrimarySSRC(simGroup);
  238. }
  239. const fidGroup = this.findGroup('FID');
  240. if (fidGroup) {
  241. return parsePrimarySSRC(fidGroup);
  242. }
  243. }
  244. }
  245. /**
  246. * Obtains RTX SSRC from the underlying video description (the
  247. * secondary SSRC of the first "FID" group found)
  248. * @param {number} primarySsrc the video ssrc for which to find the
  249. * corresponding rtx ssrc
  250. * @returns {number|undefined} the rtx ssrc (or undefined if there isn't
  251. * one)
  252. */
  253. getRtxSSRC(primarySsrc) {
  254. const fidGroup = this.findGroupByPrimarySSRC('FID', primarySsrc);
  255. return fidGroup && parseSecondarySSRC(fidGroup);
  256. }
  257. /**
  258. * Obtains all SSRCs contained in the underlying media description.
  259. * @return {Array.<number>} an array with all SSRC as numbers.
  260. */
  261. getSSRCs() {
  262. return this._ssrcs
  263. .map(ssrcInfo => ssrcInfo.id)
  264. .filter((ssrc, index, array) => array.indexOf(ssrc) === index);
  265. }
  266. /**
  267. * Obtains primary video SSRCs.
  268. * @return {Array.<number>} an array of all primary video SSRCs as numbers.
  269. * @throws Error if the wrapped media description is not a video.
  270. */
  271. getPrimaryVideoSSRCs() {
  272. const mediaType = this.mLine.type;
  273. if (mediaType !== 'video') {
  274. throw new Error(
  275. `getPrimaryVideoSSRCs doesn't work with ${mediaType}`);
  276. }
  277. const videoSSRCs = this.getSSRCs();
  278. for (const ssrcGroupInfo of this.ssrcGroups) {
  279. // Right now, FID groups are the only ones we parse to
  280. // disqualify streams. If/when others arise we'll
  281. // need to add support for them here
  282. if (ssrcGroupInfo.semantics === 'FID') {
  283. // secondary FID streams should be filtered out
  284. const secondarySsrc = parseSecondarySSRC(ssrcGroupInfo);
  285. videoSSRCs.splice(
  286. videoSSRCs.indexOf(secondarySsrc), 1);
  287. }
  288. }
  289. return videoSSRCs;
  290. }
  291. /**
  292. * Dumps all SSRC groups of this media description to JSON.
  293. */
  294. dumpSSRCGroups() {
  295. return JSON.stringify(this.mLine.ssrcGroups);
  296. }
  297. /**
  298. * Removes all SSRC groups which contain given SSRC number at any position.
  299. * @param {number} ssrc the SSRC for which all matching groups are to be
  300. * removed.
  301. */
  302. removeGroupsWithSSRC(ssrc) {
  303. if (!this.mLine.ssrcGroups) {
  304. return;
  305. }
  306. this.mLine.ssrcGroups = this.mLine.ssrcGroups
  307. .filter(groupInfo => groupInfo.ssrcs.indexOf(`${ssrc}`) === -1);
  308. }
  309. /**
  310. * Removes groups that match given semantics.
  311. * @param {string} semantics e.g. "SIM" or "FID"
  312. */
  313. removeGroupsBySemantics(semantics) {
  314. if (!this.mLine.ssrcGroups) {
  315. return;
  316. }
  317. this.mLine.ssrcGroups
  318. = this.mLine.ssrcGroups
  319. .filter(groupInfo => groupInfo.semantics !== semantics);
  320. }
  321. /**
  322. * Replaces SSRC (does not affect SSRC groups, but only attributes).
  323. * @param {number} oldSSRC the old SSRC number
  324. * @param {number} newSSRC the new SSRC number
  325. */
  326. replaceSSRC(oldSSRC, newSSRC) {
  327. if (this.mLine.ssrcs) {
  328. this.mLine.ssrcs.forEach(ssrcInfo => {
  329. if (ssrcInfo.id === oldSSRC) {
  330. ssrcInfo.id = newSSRC;
  331. }
  332. });
  333. }
  334. }
  335. /**
  336. * Adds given SSRC group to this media description.
  337. * @param {object} group the SSRC group object as defined by
  338. * the 'sdp-transform' lib.
  339. */
  340. addSSRCGroup(group) {
  341. this.ssrcGroups.push(group);
  342. }
  343. }