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.

simulcast.js 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /*jslint plusplus: true */
  2. /*jslint nomen: true*/
  3. /**
  4. * Created by gp on 11/08/14.
  5. */
  6. function Simulcast() {
  7. "use strict";
  8. // TODO(gp) split the Simulcast class in two classes : NativeSimulcast and ClassicSimulcast.
  9. this.debugLvl = 1;
  10. }
  11. (function () {
  12. "use strict";
  13. // global state for all transformers.
  14. var localExplosionMap = {}, localVideoSourceCache, emptyCompoundIndex,
  15. remoteVideoSourceCache, remoteMaps = {
  16. msid2Quality: {},
  17. ssrc2Msid: {},
  18. receivingVideoStreams: {}
  19. }, localMaps = {
  20. msids: [],
  21. msid2ssrc: {}
  22. };
  23. Simulcast.prototype._generateGuid = (function () {
  24. function s4() {
  25. return Math.floor((1 + Math.random()) * 0x10000)
  26. .toString(16)
  27. .substring(1);
  28. }
  29. return function () {
  30. return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  31. s4() + '-' + s4() + s4() + s4();
  32. };
  33. }());
  34. Simulcast.prototype._cacheLocalVideoSources = function (lines) {
  35. localVideoSourceCache = this._getVideoSources(lines);
  36. };
  37. Simulcast.prototype._restoreLocalVideoSources = function (lines) {
  38. this._replaceVideoSources(lines, localVideoSourceCache);
  39. };
  40. Simulcast.prototype._cacheRemoteVideoSources = function (lines) {
  41. remoteVideoSourceCache = this._getVideoSources(lines);
  42. };
  43. Simulcast.prototype._restoreRemoteVideoSources = function (lines) {
  44. this._replaceVideoSources(lines, remoteVideoSourceCache);
  45. };
  46. Simulcast.prototype._replaceVideoSources = function (lines, videoSources) {
  47. var i, inVideo = false, index = -1, howMany = 0;
  48. if (this.debugLvl) {
  49. console.info('Replacing video sources...');
  50. }
  51. for (i = 0; i < lines.length; i++) {
  52. if (inVideo && lines[i].substring(0, 'm='.length) === 'm=') {
  53. // Out of video.
  54. break;
  55. }
  56. if (!inVideo && lines[i].substring(0, 'm=video '.length) === 'm=video ') {
  57. // In video.
  58. inVideo = true;
  59. }
  60. if (inVideo && (lines[i].substring(0, 'a=ssrc:'.length) === 'a=ssrc:'
  61. || lines[i].substring(0, 'a=ssrc-group:'.length) === 'a=ssrc-group:')) {
  62. if (index === -1) {
  63. index = i;
  64. }
  65. howMany++;
  66. }
  67. }
  68. // efficiency baby ;)
  69. lines.splice.apply(lines,
  70. [index, howMany].concat(videoSources));
  71. };
  72. Simulcast.prototype._getVideoSources = function (lines) {
  73. var i, inVideo = false, sb = [];
  74. if (this.debugLvl) {
  75. console.info('Getting video sources...');
  76. }
  77. for (i = 0; i < lines.length; i++) {
  78. if (inVideo && lines[i].substring(0, 'm='.length) === 'm=') {
  79. // Out of video.
  80. break;
  81. }
  82. if (!inVideo && lines[i].substring(0, 'm=video '.length) === 'm=video ') {
  83. // In video.
  84. inVideo = true;
  85. }
  86. if (inVideo && lines[i].substring(0, 'a=ssrc:'.length) === 'a=ssrc:') {
  87. // In SSRC.
  88. sb.push(lines[i]);
  89. }
  90. if (inVideo && lines[i].substring(0, 'a=ssrc-group:'.length) === 'a=ssrc-group:') {
  91. sb.push(lines[i]);
  92. }
  93. }
  94. return sb;
  95. };
  96. Simulcast.prototype._parseMedia = function (lines, mediatypes) {
  97. var i, res = [], type, cur_media, idx, ssrcs, cur_ssrc, ssrc,
  98. ssrc_attribute, group, semantics, skip;
  99. if (this.debugLvl) {
  100. console.info('Parsing media sources...');
  101. }
  102. for (i = 0; i < lines.length; i++) {
  103. if (lines[i].substring(0, 'm='.length) === 'm=') {
  104. type = lines[i]
  105. .substr('m='.length, lines[i].indexOf(' ') - 'm='.length);
  106. skip = mediatypes !== undefined && mediatypes.indexOf(type) === -1;
  107. if (!skip) {
  108. cur_media = {
  109. 'type': type,
  110. 'sources': {},
  111. 'groups': []
  112. };
  113. res.push(cur_media);
  114. }
  115. } else if (!skip && lines[i].substring(0, 'a=ssrc:'.length) === 'a=ssrc:') {
  116. idx = lines[i].indexOf(' ');
  117. ssrc = lines[i].substring('a=ssrc:'.length, idx);
  118. if (cur_media.sources[ssrc] === undefined) {
  119. cur_ssrc = {'ssrc': ssrc};
  120. cur_media.sources[ssrc] = cur_ssrc;
  121. }
  122. ssrc_attribute = lines[i].substr(idx + 1).split(':', 2)[0];
  123. cur_ssrc[ssrc_attribute] = lines[i].substr(idx + 1).split(':', 2)[1];
  124. if (cur_media.base === undefined) {
  125. cur_media.base = cur_ssrc;
  126. }
  127. } else if (!skip && lines[i].substring(0, 'a=ssrc-group:'.length) === 'a=ssrc-group:') {
  128. idx = lines[i].indexOf(' ');
  129. semantics = lines[i].substr(0, idx).substr('a=ssrc-group:'.length);
  130. ssrcs = lines[i].substr(idx).trim().split(' ');
  131. group = {
  132. 'semantics': semantics,
  133. 'ssrcs': ssrcs
  134. };
  135. cur_media.groups.push(group);
  136. } else if (!skip && (lines[i].substring(0, 'a=sendrecv'.length) === 'a=sendrecv' ||
  137. lines[i].substring(0, 'a=recvonly'.length) === 'a=recvonly' ||
  138. lines[i].substring(0, 'a=sendonly'.length) === 'a=sendonly' ||
  139. lines[i].substring(0, 'a=inactive'.length) === 'a=inactive')) {
  140. cur_media.direction = lines[i].substring('a='.length, 8);
  141. }
  142. }
  143. return res;
  144. };
  145. // Returns a random integer between min (included) and max (excluded)
  146. // Using Math.round() gives a non-uniform distribution!
  147. Simulcast.prototype._generateRandomSSRC = function () {
  148. var min = 0, max = 0xffffffff;
  149. return Math.floor(Math.random() * (max - min)) + min;
  150. };
  151. function CompoundIndex(obj) {
  152. if (obj !== undefined) {
  153. this.row = obj.row;
  154. this.column = obj.column;
  155. }
  156. }
  157. emptyCompoundIndex = new CompoundIndex();
  158. /**
  159. * The _indexOfArray() method returns the first a CompoundIndex at which a
  160. * given element can be found in the array, or emptyCompoundIndex if it is
  161. * not present.
  162. *
  163. * Example:
  164. *
  165. * _indexOfArray('3', [ 'this is line 1', 'this is line 2', 'this is line 3' ])
  166. *
  167. * returns {row: 2, column: 14}
  168. *
  169. * @param needle
  170. * @param haystack
  171. * @param start
  172. * @returns {CompoundIndex}
  173. * @private
  174. */
  175. Simulcast.prototype._indexOfArray = function (needle, haystack, start) {
  176. var length = haystack.length, idx, i;
  177. if (!start) {
  178. start = 0;
  179. }
  180. for (i = start; i < length; i++) {
  181. idx = haystack[i].indexOf(needle);
  182. if (idx !== -1) {
  183. return new CompoundIndex({row: i, column: idx});
  184. }
  185. }
  186. return emptyCompoundIndex;
  187. };
  188. Simulcast.prototype._removeSimulcastGroup = function (lines) {
  189. var i;
  190. for (i = lines.length - 1; i >= 0; i--) {
  191. if (lines[i].indexOf('a=ssrc-group:SIM') !== -1) {
  192. lines.splice(i, 1);
  193. }
  194. }
  195. };
  196. /**
  197. * Produces a single stream with multiple tracks for local video sources.
  198. *
  199. * @param lines
  200. * @private
  201. */
  202. Simulcast.prototype._explodeLocalSimulcastSources = function (lines) {
  203. var sb, msid, sid, tid, videoSources, self;
  204. if (this.debugLvl) {
  205. console.info('Exploding local video sources...');
  206. }
  207. videoSources = this._parseMedia(lines, ['video'])[0];
  208. self = this;
  209. if (videoSources.groups && videoSources.groups.length !== 0) {
  210. videoSources.groups.forEach(function (group) {
  211. if (group.semantics === 'SIM') {
  212. group.ssrcs.forEach(function (ssrc) {
  213. // Get the msid for this ssrc..
  214. if (localExplosionMap[ssrc]) {
  215. // .. either from the explosion map..
  216. msid = localExplosionMap[ssrc];
  217. } else {
  218. // .. or generate a new one (msid).
  219. sid = videoSources.sources[ssrc].msid
  220. .substring(0, videoSources.sources[ssrc].msid.indexOf(' '));
  221. tid = self._generateGuid();
  222. msid = [sid, tid].join(' ');
  223. localExplosionMap[ssrc] = msid;
  224. }
  225. // Assign it to the source object.
  226. videoSources.sources[ssrc].msid = msid;
  227. // TODO(gp) Change the msid of associated sources.
  228. });
  229. }
  230. });
  231. }
  232. sb = this._compileVideoSources(videoSources);
  233. this._replaceVideoSources(lines, sb);
  234. };
  235. /**
  236. * Groups local video sources together in the ssrc-group:SIM group.
  237. *
  238. * @param lines
  239. * @private
  240. */
  241. Simulcast.prototype._groupLocalVideoSources = function (lines) {
  242. var sb, videoSources, ssrcs = [], ssrc;
  243. if (this.debugLvl) {
  244. console.info('Grouping local video sources...');
  245. }
  246. videoSources = this._parseMedia(lines, ['video'])[0];
  247. for (ssrc in videoSources.sources) {
  248. // jitsi-meet destroys/creates streams at various places causing
  249. // the original local stream ids to change. The only thing that
  250. // remains unchanged is the trackid.
  251. localMaps.msid2ssrc[videoSources.sources[ssrc].msid.split(' ')[1]] = ssrc;
  252. }
  253. // TODO(gp) add only "free" sources.
  254. localMaps.msids.forEach(function (msid) {
  255. ssrcs.push(localMaps.msid2ssrc[msid]);
  256. });
  257. if (!videoSources.groups) {
  258. videoSources.groups = [];
  259. }
  260. videoSources.groups.push({
  261. 'semantics': 'SIM',
  262. 'ssrcs': ssrcs
  263. });
  264. sb = this._compileVideoSources(videoSources);
  265. this._replaceVideoSources(lines, sb);
  266. };
  267. Simulcast.prototype._appendSimulcastGroup = function (lines) {
  268. var videoSources, ssrcGroup, simSSRC, numOfSubs = 3, i, sb, msid;
  269. if (this.debugLvl) {
  270. console.info('Appending simulcast group...');
  271. }
  272. // Get the primary SSRC information.
  273. videoSources = this._parseMedia(lines, ['video'])[0];
  274. // Start building the SIM SSRC group.
  275. ssrcGroup = ['a=ssrc-group:SIM'];
  276. // The video source buffer.
  277. sb = [];
  278. // Create the simulcast sub-streams.
  279. for (i = 0; i < numOfSubs; i++) {
  280. // TODO(gp) prevent SSRC collision.
  281. simSSRC = this._generateRandomSSRC();
  282. ssrcGroup.push(simSSRC);
  283. sb.splice.apply(sb, [sb.length, 0].concat(
  284. [["a=ssrc:", simSSRC, " cname:", videoSources.base.cname].join(''),
  285. ["a=ssrc:", simSSRC, " msid:", videoSources.base.msid].join('')]
  286. ));
  287. if (this.debugLvl) {
  288. console.info(['Generated substream ', i, ' with SSRC ', simSSRC, '.'].join(''));
  289. }
  290. }
  291. // Add the group sim layers.
  292. sb.splice(0, 0, ssrcGroup.join(' '))
  293. this._replaceVideoSources(lines, sb);
  294. };
  295. // Does the actual patching.
  296. Simulcast.prototype._ensureSimulcastGroup = function (lines) {
  297. if (this.debugLvl) {
  298. console.info('Ensuring simulcast group...');
  299. }
  300. if (this._indexOfArray('a=ssrc-group:SIM', lines) === emptyCompoundIndex) {
  301. this._appendSimulcastGroup(lines);
  302. this._cacheLocalVideoSources(lines);
  303. } else {
  304. // verify that the ssrcs participating in the SIM group are present
  305. // in the SDP (needed for presence).
  306. this._restoreLocalVideoSources(lines);
  307. }
  308. };
  309. Simulcast.prototype._ensureGoogConference = function (lines) {
  310. var sb;
  311. if (this.debugLvl) {
  312. console.info('Ensuring x-google-conference flag...')
  313. }
  314. if (this._indexOfArray('a=x-google-flag:conference', lines) === emptyCompoundIndex) {
  315. // Add the google conference flag
  316. sb = this._getVideoSources(lines);
  317. sb = ['a=x-google-flag:conference'].concat(sb);
  318. this._replaceVideoSources(lines, sb);
  319. }
  320. };
  321. Simulcast.prototype._compileVideoSources = function (videoSources) {
  322. var sb = [], ssrc, addedSSRCs = [];
  323. if (this.debugLvl) {
  324. console.info('Compiling video sources...');
  325. }
  326. // Add the groups
  327. if (videoSources.groups && videoSources.groups.length !== 0) {
  328. videoSources.groups.forEach(function (group) {
  329. if (group.ssrcs && group.ssrcs.length !== 0) {
  330. sb.push([['a=ssrc-group:', group.semantics].join(''), group.ssrcs.join(' ')].join(' '));
  331. // if (group.semantics !== 'SIM') {
  332. group.ssrcs.forEach(function (ssrc) {
  333. addedSSRCs.push(ssrc);
  334. sb.splice.apply(sb, [sb.length, 0].concat([
  335. ["a=ssrc:", ssrc, " cname:", videoSources.sources[ssrc].cname].join(''),
  336. ["a=ssrc:", ssrc, " msid:", videoSources.sources[ssrc].msid].join('')]));
  337. });
  338. //}
  339. }
  340. });
  341. }
  342. // Then add any free sources.
  343. if (videoSources.sources) {
  344. for (ssrc in videoSources.sources) {
  345. if (addedSSRCs.indexOf(ssrc) === -1) {
  346. sb.splice.apply(sb, [sb.length, 0].concat([
  347. ["a=ssrc:", ssrc, " cname:", videoSources.sources[ssrc].cname].join(''),
  348. ["a=ssrc:", ssrc, " msid:", videoSources.sources[ssrc].msid].join('')]));
  349. }
  350. }
  351. }
  352. return sb;
  353. };
  354. /**
  355. * Ensures that the simulcast group is present in the answer, _if_ native
  356. * simulcast is enabled,
  357. *
  358. * @param desc
  359. * @returns {*}
  360. */
  361. Simulcast.prototype.transformAnswer = function (desc) {
  362. if (config.enableSimulcast && config.useNativeSimulcast) {
  363. var sb = desc.sdp.split('\r\n');
  364. // Even if we have enabled native simulcasting previously
  365. // (with a call to SLD with an appropriate SDP, for example),
  366. // createAnswer seems to consistently generate incomplete SDP
  367. // with missing SSRCS.
  368. //
  369. // So, subsequent calls to SLD will have missing SSRCS and presence
  370. // won't have the complete list of SRCs.
  371. this._ensureSimulcastGroup(sb);
  372. desc = new RTCSessionDescription({
  373. type: desc.type,
  374. sdp: sb.join('\r\n')
  375. });
  376. if (this.debugLvl && this.debugLvl > 1) {
  377. console.info('Transformed answer');
  378. console.info(desc.sdp);
  379. }
  380. }
  381. return desc;
  382. };
  383. Simulcast.prototype._restoreSimulcastGroups = function (sb) {
  384. this._restoreRemoteVideoSources(sb);
  385. };
  386. /**
  387. * Restores the simulcast groups of the remote description. In
  388. * transformRemoteDescription we remove those in order for the set remote
  389. * description to succeed. The focus needs the signal the groups to new
  390. * participants.
  391. *
  392. * @param desc
  393. * @returns {*}
  394. */
  395. Simulcast.prototype.reverseTransformRemoteDescription = function (desc) {
  396. var sb;
  397. if (!desc || desc == null) {
  398. return desc;
  399. }
  400. if (config.enableSimulcast) {
  401. sb = desc.sdp.split('\r\n');
  402. this._restoreSimulcastGroups(sb);
  403. desc = new RTCSessionDescription({
  404. type: desc.type,
  405. sdp: sb.join('\r\n')
  406. });
  407. }
  408. return desc;
  409. };
  410. /**
  411. * Prepares the local description for public usage (i.e. to be signaled
  412. * through Jingle to the focus).
  413. *
  414. * @param desc
  415. * @returns {RTCSessionDescription}
  416. */
  417. Simulcast.prototype.reverseTransformLocalDescription = function (desc) {
  418. var sb;
  419. if (!desc || desc == null) {
  420. return desc;
  421. }
  422. if (config.enableSimulcast) {
  423. if (config.useNativeSimulcast) {
  424. sb = desc.sdp.split('\r\n');
  425. this._explodeLocalSimulcastSources(sb);
  426. desc = new RTCSessionDescription({
  427. type: desc.type,
  428. sdp: sb.join('\r\n')
  429. });
  430. if (this.debugLvl && this.debugLvl > 1) {
  431. console.info('Exploded local video sources');
  432. console.info(desc.sdp);
  433. }
  434. } else {
  435. sb = desc.sdp.split('\r\n');
  436. this._groupLocalVideoSources(sb);
  437. desc = new RTCSessionDescription({
  438. type: desc.type,
  439. sdp: sb.join('\r\n')
  440. });
  441. if (this.debugLvl && this.debugLvl > 1) {
  442. console.info('Grouped local video sources');
  443. console.info(desc.sdp);
  444. }
  445. }
  446. }
  447. return desc;
  448. };
  449. Simulcast.prototype._ensureOrder = function (lines) {
  450. var videoSources, sb;
  451. videoSources = this._parseMedia(lines, ['video'])[0];
  452. sb = this._compileVideoSources(videoSources);
  453. this._replaceVideoSources(lines, sb);
  454. };
  455. Simulcast.prototype._updateRemoteMaps = function (lines) {
  456. var remoteVideoSources = this._parseMedia(lines, ['video'])[0],
  457. videoSource, quality;
  458. // (re) initialize the remote maps.
  459. remoteMaps.msid2Quality = {};
  460. remoteMaps.ssrc2Msid = {};
  461. if (remoteVideoSources.groups && remoteVideoSources.groups.length !== 0) {
  462. remoteVideoSources.groups.forEach(function (group) {
  463. if (group.semantics === 'SIM' && group.ssrcs && group.ssrcs.length !== 0) {
  464. quality = 0;
  465. group.ssrcs.forEach(function (ssrc) {
  466. videoSource = remoteVideoSources.sources[ssrc];
  467. remoteMaps.msid2Quality[videoSource.msid] = quality++;
  468. remoteMaps.ssrc2Msid[videoSource.ssrc] = videoSource.msid;
  469. });
  470. }
  471. });
  472. }
  473. };
  474. /**
  475. *
  476. *
  477. * @param desc
  478. * @returns {*}
  479. */
  480. Simulcast.prototype.transformLocalDescription = function (desc) {
  481. if (config.enableSimulcast && !config.useNativeSimulcast) {
  482. var sb = desc.sdp.split('\r\n');
  483. this._removeSimulcastGroup(sb);
  484. desc = new RTCSessionDescription({
  485. type: desc.type,
  486. sdp: sb.join('\r\n')
  487. });
  488. if (this.debugLvl && this.debugLvl > 1) {
  489. console.info('Transformed local description');
  490. console.info(desc.sdp);
  491. }
  492. }
  493. return desc;
  494. };
  495. /**
  496. * Removes the ssrc-group:SIM from the remote description bacause Chrome
  497. * either gets confused and thinks this is an FID group or, if an FID group
  498. * is already present, it fails to set the remote description.
  499. *
  500. * @param desc
  501. * @returns {*}
  502. */
  503. Simulcast.prototype.transformRemoteDescription = function (desc) {
  504. if (config.enableSimulcast) {
  505. var sb = desc.sdp.split('\r\n');
  506. this._updateRemoteMaps(sb);
  507. this._cacheRemoteVideoSources(sb);
  508. this._removeSimulcastGroup(sb); // NOTE(gp) this needs to be called after updateRemoteMaps because we need the simulcast group in the _updateRemoteMaps() method.
  509. if (config.useNativeSimulcast) {
  510. // We don't need the goog conference flag if we're not doing
  511. // native simulcast.
  512. this._ensureGoogConference(sb);
  513. }
  514. desc = new RTCSessionDescription({
  515. type: desc.type,
  516. sdp: sb.join('\r\n')
  517. });
  518. if (this.debugLvl && this.debugLvl > 1) {
  519. console.info('Transformed remote description');
  520. console.info(desc.sdp);
  521. }
  522. }
  523. return desc;
  524. };
  525. Simulcast.prototype._setReceivingVideoStream = function (endpoint, ssrc) {
  526. remoteMaps.receivingVideoStreams[endpoint] = ssrc;
  527. };
  528. /**
  529. * Returns a stream with single video track, the one currently being
  530. * received by this endpoint.
  531. *
  532. * @param stream the remote simulcast stream.
  533. * @returns {webkitMediaStream}
  534. */
  535. Simulcast.prototype.getReceivingVideoStream = function (stream) {
  536. var tracks, i, electedTrack, msid, quality = 0, receivingTrackId;
  537. if (config.enableSimulcast) {
  538. stream.getVideoTracks().some(function(track) {
  539. return Object.keys(remoteMaps.receivingVideoStreams).some(function(endpoint) {
  540. var ssrc = remoteMaps.receivingVideoStreams[endpoint];
  541. var msid = remoteMaps.ssrc2Msid[ssrc];
  542. if (msid == [stream.id, track.id].join(' ')) {
  543. electedTrack = track;
  544. return true;
  545. }
  546. });
  547. });
  548. if (!electedTrack) {
  549. // we don't have an elected track, choose by initial quality.
  550. tracks = stream.getVideoTracks();
  551. for (i = 0; i < tracks.length; i++) {
  552. msid = [stream.id, tracks[i].id].join(' ');
  553. if (remoteMaps.msid2Quality[msid] === quality) {
  554. electedTrack = tracks[i];
  555. break;
  556. }
  557. }
  558. // TODO(gp) if the initialQuality could not be satisfied, lower
  559. // the requirement and try again.
  560. }
  561. }
  562. return (electedTrack)
  563. ? new webkitMediaStream([electedTrack])
  564. : stream;
  565. };
  566. var stream;
  567. /**
  568. * GUM for simulcast.
  569. *
  570. * @param constraints
  571. * @param success
  572. * @param err
  573. */
  574. Simulcast.prototype.getUserMedia = function (constraints, success, err) {
  575. // TODO(gp) what if we request a resolution not supported by the hardware?
  576. // TODO(gp) make the lq stream configurable; although this wouldn't work with native simulcast
  577. var lqConstraints = {
  578. audio: false,
  579. video: {
  580. mandatory: {
  581. maxWidth: 320,
  582. maxHeight: 180,
  583. maxFrameRate: 15
  584. }
  585. }
  586. };
  587. console.log('HQ constraints: ', constraints);
  588. console.log('LQ constraints: ', lqConstraints);
  589. if (config.enableSimulcast && !config.useNativeSimulcast) {
  590. // NOTE(gp) if we request the lq stream first webkitGetUserMedia
  591. // fails randomly. Tested with Chrome 37. As fippo suggested, the
  592. // reason appears to be that Chrome only acquires the cam once and
  593. // then downscales the picture (https://code.google.com/p/chromium/issues/detail?id=346616#c11)
  594. navigator.webkitGetUserMedia(constraints, function (hqStream) {
  595. // reset local maps.
  596. localMaps.msids = [];
  597. localMaps.msid2ssrc = {};
  598. // add hq trackid to local map
  599. localMaps.msids.push(hqStream.getVideoTracks()[0].id);
  600. navigator.webkitGetUserMedia(lqConstraints, function (lqStream) {
  601. // NOTE(gp) The specification says Array.forEach() will visit
  602. // the array elements in numeric order, and that it doesn't
  603. // visit elements that don't exist.
  604. // add lq trackid to local map
  605. localMaps.msids.splice(0, 0, lqStream.getVideoTracks()[0].id);
  606. hqStream.addTrack(lqStream.getVideoTracks()[0]);
  607. stream = hqStream;
  608. success(hqStream);
  609. }, err);
  610. }, err);
  611. } else {
  612. // There's nothing special to do for native simulcast, so just do a normal GUM.
  613. navigator.webkitGetUserMedia(constraints, function (hqStream) {
  614. // reset local maps.
  615. localMaps.msids = [];
  616. localMaps.msid2ssrc = {};
  617. // add hq stream to local map
  618. localMaps.msids.push(hqStream.getVideoTracks()[0].id);
  619. stream = hqStream;
  620. success(hqStream);
  621. }, err);
  622. }
  623. };
  624. /**
  625. * Gets the fully qualified msid (stream.id + track.id) associated to the
  626. * SSRC.
  627. *
  628. * @param ssrc
  629. * @returns {*}
  630. */
  631. Simulcast.prototype.getRemoteVideoStreamIdBySSRC = function (ssrc) {
  632. return remoteMaps.ssrc2Msid[ssrc];
  633. };
  634. Simulcast.prototype.parseMedia = function (desc, mediatypes) {
  635. var lines = desc.sdp.split('\r\n');
  636. return this._parseMedia(lines, mediatypes);
  637. };
  638. Simulcast.prototype._setLocalVideoStreamEnabled = function (ssrc, enabled) {
  639. var trackid;
  640. console.log(['Requested to', enabled ? 'enable' : 'disable', ssrc].join(' '));
  641. if (Object.keys(localMaps.msid2ssrc).some(function (tid) {
  642. // Search for the track id that corresponds to the ssrc
  643. if (localMaps.msid2ssrc[tid] == ssrc) {
  644. trackid = tid;
  645. return true;
  646. }
  647. }) && stream.getVideoTracks().some(function(track) {
  648. // Start/stop the track that corresponds to the track id
  649. if (track.id === trackid) {
  650. track.enabled = enabled;
  651. return true;
  652. }
  653. })) {
  654. console.log([trackid, enabled ? 'enabled' : 'disabled'].join(' '));
  655. $(document).trigger(enabled
  656. ? 'simulcastlayerstarted'
  657. : 'simulcastlayerstopped');
  658. } else {
  659. console.error("I don't have a local stream with SSRC " + ssrc);
  660. }
  661. };
  662. Simulcast.prototype.getLocalVideoStream = function() {
  663. var track;
  664. stream.getVideoTracks().some(function(t) {
  665. if ((track = t).enabled) {
  666. return true;
  667. }
  668. });
  669. return new webkitMediaStream([track]);
  670. };
  671. $(document).bind('simulcastlayerschanged', function (event, endpointSimulcastLayers) {
  672. endpointSimulcastLayers.forEach(function (esl) {
  673. var ssrc = esl.simulcastLayer.primarySSRC;
  674. var simulcast = new Simulcast();
  675. simulcast._setReceivingVideoStream(esl.endpoint, ssrc);
  676. });
  677. });
  678. $(document).bind('startsimulcastlayer', function(event, simulcastLayer) {
  679. var ssrc = simulcastLayer.primarySSRC;
  680. var simulcast = new Simulcast();
  681. simulcast._setLocalVideoStreamEnabled(ssrc, true);
  682. });
  683. $(document).bind('stopsimulcastlayer', function(event, simulcastLayer) {
  684. var ssrc = simulcastLayer.primarySSRC;
  685. var simulcast = new Simulcast();
  686. simulcast._setLocalVideoStreamEnabled(ssrc, false);
  687. });
  688. }());