modified lib-jitsi-meet dev repo
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SdpTransformUtil.js 12KB

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