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.

SimulcastUtils.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. var SimulcastLogger = require("./SimulcastLogger");
  2. /**
  3. *
  4. * @constructor
  5. */
  6. function SimulcastUtils() {
  7. this.logger = new SimulcastLogger("SimulcastUtils", 1);
  8. }
  9. /**
  10. *
  11. * @type {{}}
  12. * @private
  13. */
  14. SimulcastUtils.prototype._emptyCompoundIndex = {};
  15. /**
  16. *
  17. * @param lines
  18. * @param videoSources
  19. * @private
  20. */
  21. SimulcastUtils.prototype._replaceVideoSources = function (lines, videoSources) {
  22. var i, inVideo = false, index = -1, howMany = 0;
  23. this.logger.info('Replacing video sources...');
  24. for (i = 0; i < lines.length; i++) {
  25. if (inVideo && lines[i].substring(0, 'm='.length) === 'm=') {
  26. // Out of video.
  27. break;
  28. }
  29. if (!inVideo && lines[i].substring(0, 'm=video '.length) === 'm=video ') {
  30. // In video.
  31. inVideo = true;
  32. }
  33. if (inVideo && (lines[i].substring(0, 'a=ssrc:'.length) === 'a=ssrc:'
  34. || lines[i].substring(0, 'a=ssrc-group:'.length) === 'a=ssrc-group:')) {
  35. if (index === -1) {
  36. index = i;
  37. }
  38. howMany++;
  39. }
  40. }
  41. // efficiency baby ;)
  42. lines.splice.apply(lines,
  43. [index, howMany].concat(videoSources));
  44. };
  45. SimulcastUtils.prototype.isValidDescription = function (desc)
  46. {
  47. return desc && desc != null
  48. && desc.type && desc.type != ''
  49. && desc.sdp && desc.sdp != '';
  50. };
  51. SimulcastUtils.prototype._getVideoSources = function (lines) {
  52. var i, inVideo = false, sb = [];
  53. this.logger.info('Getting video sources...');
  54. for (i = 0; i < lines.length; i++) {
  55. if (inVideo && lines[i].substring(0, 'm='.length) === 'm=') {
  56. // Out of video.
  57. break;
  58. }
  59. if (!inVideo && lines[i].substring(0, 'm=video '.length) === 'm=video ') {
  60. // In video.
  61. inVideo = true;
  62. }
  63. if (inVideo && lines[i].substring(0, 'a=ssrc:'.length) === 'a=ssrc:') {
  64. // In SSRC.
  65. sb.push(lines[i]);
  66. }
  67. if (inVideo && lines[i].substring(0, 'a=ssrc-group:'.length) === 'a=ssrc-group:') {
  68. sb.push(lines[i]);
  69. }
  70. }
  71. return sb;
  72. };
  73. SimulcastUtils.prototype.parseMedia = function (lines, mediatypes) {
  74. var i, res = [], type, cur_media, idx, ssrcs, cur_ssrc, ssrc,
  75. ssrc_attribute, group, semantics, skip = true;
  76. this.logger.info('Parsing media sources...');
  77. for (i = 0; i < lines.length; i++) {
  78. if (lines[i].substring(0, 'm='.length) === 'm=') {
  79. type = lines[i]
  80. .substr('m='.length, lines[i].indexOf(' ') - 'm='.length);
  81. skip = mediatypes !== undefined && mediatypes.indexOf(type) === -1;
  82. if (!skip) {
  83. cur_media = {
  84. 'type': type,
  85. 'sources': {},
  86. 'groups': []
  87. };
  88. res.push(cur_media);
  89. }
  90. } else if (!skip && lines[i].substring(0, 'a=ssrc:'.length) === 'a=ssrc:') {
  91. idx = lines[i].indexOf(' ');
  92. ssrc = lines[i].substring('a=ssrc:'.length, idx);
  93. if (cur_media.sources[ssrc] === undefined) {
  94. cur_ssrc = {'ssrc': ssrc};
  95. cur_media.sources[ssrc] = cur_ssrc;
  96. }
  97. ssrc_attribute = lines[i].substr(idx + 1).split(':', 2)[0];
  98. cur_ssrc[ssrc_attribute] = lines[i].substr(idx + 1).split(':', 2)[1];
  99. if (cur_media.base === undefined) {
  100. cur_media.base = cur_ssrc;
  101. }
  102. } else if (!skip && lines[i].substring(0, 'a=ssrc-group:'.length) === 'a=ssrc-group:') {
  103. idx = lines[i].indexOf(' ');
  104. semantics = lines[i].substr(0, idx).substr('a=ssrc-group:'.length);
  105. ssrcs = lines[i].substr(idx).trim().split(' ');
  106. group = {
  107. 'semantics': semantics,
  108. 'ssrcs': ssrcs
  109. };
  110. cur_media.groups.push(group);
  111. } else if (!skip && (lines[i].substring(0, 'a=sendrecv'.length) === 'a=sendrecv' ||
  112. lines[i].substring(0, 'a=recvonly'.length) === 'a=recvonly' ||
  113. lines[i].substring(0, 'a=sendonly'.length) === 'a=sendonly' ||
  114. lines[i].substring(0, 'a=inactive'.length) === 'a=inactive')) {
  115. cur_media.direction = lines[i].substring('a='.length);
  116. }
  117. }
  118. return res;
  119. };
  120. /**
  121. * The _indexOfArray() method returns the first a CompoundIndex at which a
  122. * given element can be found in the array, or _emptyCompoundIndex if it is
  123. * not present.
  124. *
  125. * Example:
  126. *
  127. * _indexOfArray('3', [ 'this is line 1', 'this is line 2', 'this is line 3' ])
  128. *
  129. * returns {row: 2, column: 14}
  130. *
  131. * @param needle
  132. * @param haystack
  133. * @param start
  134. * @returns {}
  135. * @private
  136. */
  137. SimulcastUtils.prototype._indexOfArray = function (needle, haystack, start) {
  138. var length = haystack.length, idx, i;
  139. if (!start) {
  140. start = 0;
  141. }
  142. for (i = start; i < length; i++) {
  143. idx = haystack[i].indexOf(needle);
  144. if (idx !== -1) {
  145. return {row: i, column: idx};
  146. }
  147. }
  148. return this._emptyCompoundIndex;
  149. };
  150. SimulcastUtils.prototype._removeSimulcastGroup = function (lines) {
  151. var i;
  152. for (i = lines.length - 1; i >= 0; i--) {
  153. if (lines[i].indexOf('a=ssrc-group:SIM') !== -1) {
  154. lines.splice(i, 1);
  155. }
  156. }
  157. };
  158. SimulcastUtils.prototype._compileVideoSources = function (videoSources) {
  159. var sb = [], ssrc, addedSSRCs = [];
  160. this.logger.info('Compiling video sources...');
  161. // Add the groups
  162. if (videoSources.groups && videoSources.groups.length !== 0) {
  163. videoSources.groups.forEach(function (group) {
  164. if (group.ssrcs && group.ssrcs.length !== 0) {
  165. sb.push([['a=ssrc-group:', group.semantics].join(''), group.ssrcs.join(' ')].join(' '));
  166. // if (group.semantics !== 'SIM') {
  167. group.ssrcs.forEach(function (ssrc) {
  168. addedSSRCs.push(ssrc);
  169. sb.splice.apply(sb, [sb.length, 0].concat([
  170. ["a=ssrc:", ssrc, " cname:", videoSources.sources[ssrc].cname].join(''),
  171. ["a=ssrc:", ssrc, " msid:", videoSources.sources[ssrc].msid].join('')]));
  172. });
  173. //}
  174. }
  175. });
  176. }
  177. // Then add any free sources.
  178. if (videoSources.sources) {
  179. for (ssrc in videoSources.sources) {
  180. if (addedSSRCs.indexOf(ssrc) === -1) {
  181. sb.splice.apply(sb, [sb.length, 0].concat([
  182. ["a=ssrc:", ssrc, " cname:", videoSources.sources[ssrc].cname].join(''),
  183. ["a=ssrc:", ssrc, " msid:", videoSources.sources[ssrc].msid].join('')]));
  184. }
  185. }
  186. }
  187. return sb;
  188. };
  189. module.exports = SimulcastUtils;