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.

RTCUtils.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright @ 2015 Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. var RTCBrowserType = require("../../service/RTC/RTCBrowserType.js");
  17. var Resolutions = require("../../service/RTC/Resolutions");
  18. var currentResolution = null;
  19. function getPreviousResolution(resolution) {
  20. if(!Resolutions[resolution])
  21. return null;
  22. var order = Resolutions[resolution].order;
  23. var res = null;
  24. var resName = null;
  25. for(var i in Resolutions)
  26. {
  27. var tmp = Resolutions[i];
  28. if(res == null || (res.order < tmp.order && tmp.order < order))
  29. {
  30. resName = i;
  31. res = tmp;
  32. }
  33. }
  34. return resName;
  35. }
  36. function setResolutionConstraints(constraints, resolution, isAndroid)
  37. {
  38. if (resolution && !constraints.video || isAndroid) {
  39. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  40. }
  41. if(Resolutions[resolution])
  42. {
  43. constraints.video.mandatory.minWidth = Resolutions[resolution].width;
  44. constraints.video.mandatory.minHeight = Resolutions[resolution].height;
  45. }
  46. else
  47. {
  48. if (isAndroid) {
  49. constraints.video.mandatory.minWidth = 320;
  50. constraints.video.mandatory.minHeight = 240;
  51. constraints.video.mandatory.maxFrameRate = 15;
  52. }
  53. }
  54. if (constraints.video.mandatory.minWidth)
  55. constraints.video.mandatory.maxWidth = constraints.video.mandatory.minWidth;
  56. if (constraints.video.mandatory.minHeight)
  57. constraints.video.mandatory.maxHeight = constraints.video.mandatory.minHeight;
  58. }
  59. function getConstraints(um, resolution, bandwidth, fps, desktopStream, isAndroid)
  60. {
  61. var constraints = {audio: false, video: false};
  62. if (um.indexOf('video') >= 0) {
  63. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  64. }
  65. if (um.indexOf('audio') >= 0) {
  66. constraints.audio = { mandatory: {}, optional: []};// same behaviour as true
  67. }
  68. if (um.indexOf('screen') >= 0) {
  69. constraints.video = {
  70. mandatory: {
  71. chromeMediaSource: "screen",
  72. googLeakyBucket: true,
  73. maxWidth: window.screen.width,
  74. maxHeight: window.screen.height,
  75. maxFrameRate: 3
  76. },
  77. optional: []
  78. };
  79. }
  80. if (um.indexOf('desktop') >= 0) {
  81. constraints.video = {
  82. mandatory: {
  83. chromeMediaSource: "desktop",
  84. chromeMediaSourceId: desktopStream,
  85. googLeakyBucket: true,
  86. maxWidth: window.screen.width,
  87. maxHeight: window.screen.height,
  88. maxFrameRate: 3
  89. },
  90. optional: []
  91. };
  92. }
  93. if (constraints.audio) {
  94. // if it is good enough for hangouts...
  95. constraints.audio.optional.push(
  96. {googEchoCancellation: true},
  97. {googAutoGainControl: true},
  98. {googNoiseSupression: true},
  99. {googHighpassFilter: true},
  100. {googNoisesuppression2: true},
  101. {googEchoCancellation2: true},
  102. {googAutoGainControl2: true}
  103. );
  104. }
  105. if (constraints.video) {
  106. constraints.video.optional.push(
  107. {googNoiseReduction: false} // chrome 37 workaround for issue 3807, reenable in M38
  108. );
  109. if (um.indexOf('video') >= 0) {
  110. constraints.video.optional.push(
  111. {googLeakyBucket: true}
  112. );
  113. }
  114. }
  115. if (um.indexOf('video') >= 0) {
  116. setResolutionConstraints(constraints, resolution, isAndroid);
  117. }
  118. if (bandwidth) { // doesn't work currently, see webrtc issue 1846
  119. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};//same behaviour as true
  120. constraints.video.optional.push({bandwidth: bandwidth});
  121. }
  122. if (fps) { // for some cameras it might be necessary to request 30fps
  123. // so they choose 30fps mjpg over 10fps yuy2
  124. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};// same behaviour as true;
  125. constraints.video.mandatory.minFrameRate = fps;
  126. }
  127. return constraints;
  128. }
  129. function RTCUtils(RTCService)
  130. {
  131. this.service = RTCService;
  132. if (navigator.mozGetUserMedia) {
  133. console.log('This appears to be Firefox');
  134. var version = parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  135. if (version >= 40
  136. && config.useBundle && config.useRtcpMux) {
  137. this.peerconnection = mozRTCPeerConnection;
  138. this.browser = RTCBrowserType.RTC_BROWSER_FIREFOX;
  139. this.getUserMedia = navigator.mozGetUserMedia.bind(navigator);
  140. this.pc_constraints = {};
  141. this.attachMediaStream = function (element, stream) {
  142. // srcObject is being standardized and FF will eventually
  143. // support that unprefixed. FF also supports the
  144. // "element.src = URL.createObjectURL(...)" combo, but that
  145. // will be deprecated in favour of srcObject.
  146. //
  147. // https://groups.google.com/forum/#!topic/mozilla.dev.media/pKOiioXonJg
  148. // https://github.com/webrtc/samples/issues/302
  149. if(!element[0])
  150. return;
  151. element[0].mozSrcObject = stream;
  152. element[0].play();
  153. };
  154. this.getStreamID = function (stream) {
  155. var tracks = stream.getVideoTracks();
  156. if(!tracks || tracks.length == 0)
  157. {
  158. tracks = stream.getAudioTracks();
  159. }
  160. return tracks[0].id.replace(/[\{,\}]/g,"");
  161. };
  162. this.getVideoSrc = function (element) {
  163. if(!element)
  164. return null;
  165. return element.mozSrcObject;
  166. };
  167. this.setVideoSrc = function (element, src) {
  168. if(element)
  169. element.mozSrcObject = src;
  170. };
  171. RTCSessionDescription = mozRTCSessionDescription;
  172. RTCIceCandidate = mozRTCIceCandidate;
  173. } else {
  174. window.location.href = 'unsupported_browser.html';
  175. return;
  176. }
  177. } else if (navigator.webkitGetUserMedia) {
  178. console.log('This appears to be Chrome');
  179. this.peerconnection = webkitRTCPeerConnection;
  180. this.browser = RTCBrowserType.RTC_BROWSER_CHROME;
  181. this.getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
  182. this.attachMediaStream = function (element, stream) {
  183. element.attr('src', webkitURL.createObjectURL(stream));
  184. };
  185. this.getStreamID = function (stream) {
  186. // streams from FF endpoints have the characters '{' and '}'
  187. // that make jQuery choke.
  188. return stream.id.replace(/[\{,\}]/g,"");
  189. };
  190. this.getVideoSrc = function (element) {
  191. if(!element)
  192. return null;
  193. return element.getAttribute("src");
  194. };
  195. this.setVideoSrc = function (element, src) {
  196. if(element)
  197. element.setAttribute("src", src);
  198. };
  199. // DTLS should now be enabled by default but..
  200. this.pc_constraints = {'optional': [{'DtlsSrtpKeyAgreement': 'true'}]};
  201. if (navigator.userAgent.indexOf('Android') != -1) {
  202. this.pc_constraints = {}; // disable DTLS on Android
  203. }
  204. if (!webkitMediaStream.prototype.getVideoTracks) {
  205. webkitMediaStream.prototype.getVideoTracks = function () {
  206. return this.videoTracks;
  207. };
  208. }
  209. if (!webkitMediaStream.prototype.getAudioTracks) {
  210. webkitMediaStream.prototype.getAudioTracks = function () {
  211. return this.audioTracks;
  212. };
  213. }
  214. }
  215. else
  216. {
  217. try { console.log('Browser does not appear to be WebRTC-capable'); } catch (e) { }
  218. window.location.href = 'unsupported_browser.html';
  219. return;
  220. }
  221. }
  222. RTCUtils.prototype.getUserMediaWithConstraints = function(
  223. um, success_callback, failure_callback, resolution,bandwidth, fps,
  224. desktopStream)
  225. {
  226. currentResolution = resolution;
  227. // Check if we are running on Android device
  228. var isAndroid = navigator.userAgent.indexOf('Android') != -1;
  229. var constraints = getConstraints(
  230. um, resolution, bandwidth, fps, desktopStream, isAndroid);
  231. var isFF = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
  232. var self = this;
  233. try {
  234. this.getUserMedia(constraints,
  235. function (stream) {
  236. console.log('onUserMediaSuccess');
  237. self.setAvailableDevices(um, true);
  238. success_callback(stream);
  239. },
  240. function (error) {
  241. self.setAvailableDevices(um, false);
  242. console.warn('Failed to get access to local media. Error ',
  243. error, constraints);
  244. if (failure_callback) {
  245. failure_callback(error);
  246. }
  247. });
  248. } catch (e) {
  249. console.error('GUM failed: ', e);
  250. if(failure_callback) {
  251. failure_callback(e);
  252. }
  253. }
  254. };
  255. RTCUtils.prototype.setAvailableDevices = function (um, available) {
  256. var devices = {};
  257. if(um.indexOf("video") != -1)
  258. {
  259. devices.video = available;
  260. }
  261. if(um.indexOf("audio") != -1)
  262. {
  263. devices.audio = available;
  264. }
  265. this.service.setDeviceAvailability(devices);
  266. }
  267. /**
  268. * We ask for audio and video combined stream in order to get permissions and
  269. * not to ask twice.
  270. */
  271. RTCUtils.prototype.obtainAudioAndVideoPermissions =
  272. function(devices, callback, usageOptions)
  273. {
  274. var self = this;
  275. // Get AV
  276. var successCallback = function (stream) {
  277. if(callback)
  278. callback(stream, usageOptions);
  279. else
  280. self.successCallback(stream, usageOptions);
  281. };
  282. if(!devices)
  283. devices = ['audio', 'video'];
  284. var newDevices = [];
  285. if(usageOptions)
  286. for(var i = 0; i < devices.length; i++)
  287. {
  288. var device = devices[i];
  289. if(usageOptions[device] === true)
  290. newDevices.push(device);
  291. }
  292. else
  293. newDevices = devices;
  294. if(newDevices.length === 0)
  295. {
  296. successCallback();
  297. return;
  298. }
  299. if (navigator.mozGetUserMedia) {
  300. // With FF we can't split the stream into audio and video because FF
  301. // doesn't support media stream constructors. So, we need to get the
  302. // audio stream separately from the video stream using two distinct GUM
  303. // calls. Not very user friendly :-( but we don't have many other
  304. // options neither.
  305. //
  306. // Note that we pack those 2 streams in a single object and pass it to
  307. // the successCallback method.
  308. self.getUserMediaWithConstraints(
  309. ['audio'],
  310. function (audioStream) {
  311. self.getUserMediaWithConstraints(
  312. ['video'],
  313. function (videoStream) {
  314. return self.successCallback({
  315. audioStream: audioStream,
  316. videoStream: videoStream
  317. });
  318. },
  319. function (error) {
  320. console.error('failed to obtain video stream - stop',
  321. error);
  322. return self.successCallback(null);
  323. },
  324. config.resolution || '360');
  325. },
  326. function (error) {
  327. console.error('failed to obtain audio stream - stop',
  328. error);
  329. return self.successCallback(null);
  330. }
  331. );
  332. } else {
  333. this.getUserMediaWithConstraints(
  334. newDevices,
  335. function (stream) {
  336. successCallback(stream);
  337. },
  338. function (error) {
  339. self.errorCallback(error);
  340. },
  341. config.resolution || '360');
  342. }
  343. }
  344. RTCUtils.prototype.successCallback = function (stream, usageOptions) {
  345. // If this is FF, the stream parameter is *not* a MediaStream object, it's
  346. // an object with two properties: audioStream, videoStream.
  347. if(stream && !navigator.mozGetUserMedia)
  348. console.log('got', stream, stream.getAudioTracks().length,
  349. stream.getVideoTracks().length);
  350. this.handleLocalStream(stream, usageOptions);
  351. };
  352. RTCUtils.prototype.errorCallback = function (error) {
  353. var self = this;
  354. console.error('failed to obtain audio/video stream - trying audio only', error);
  355. var resolution = getPreviousResolution(currentResolution);
  356. if(typeof error == "object" && error.constraintName && error.name
  357. && (error.name == "ConstraintNotSatisfiedError" ||
  358. error.name == "OverconstrainedError") &&
  359. (error.constraintName == "minWidth" || error.constraintName == "maxWidth" ||
  360. error.constraintName == "minHeight" || error.constraintName == "maxHeight")
  361. && resolution != null)
  362. {
  363. self.getUserMediaWithConstraints(['audio', 'video'],
  364. function (stream) {
  365. return self.successCallback(stream);
  366. }, function (error) {
  367. return self.errorCallback(error);
  368. }, resolution);
  369. }
  370. else
  371. {
  372. self.getUserMediaWithConstraints(
  373. ['audio'],
  374. function (stream) {
  375. return self.successCallback(stream);
  376. },
  377. function (error) {
  378. console.error('failed to obtain audio/video stream - stop',
  379. error);
  380. return self.successCallback(null);
  381. }
  382. );
  383. }
  384. }
  385. RTCUtils.prototype.handleLocalStream = function(stream, usageOptions)
  386. {
  387. // If this is FF, the stream parameter is *not* a MediaStream object, it's
  388. // an object with two properties: audioStream, videoStream.
  389. var audioStream, videoStream;
  390. if(window.webkitMediaStream)
  391. {
  392. audioStream = new webkitMediaStream();
  393. videoStream = new webkitMediaStream();
  394. if(stream) {
  395. var audioTracks = stream.getAudioTracks();
  396. for (var i = 0; i < audioTracks.length; i++) {
  397. audioStream.addTrack(audioTracks[i]);
  398. }
  399. var videoTracks = stream.getVideoTracks();
  400. for (i = 0; i < videoTracks.length; i++) {
  401. videoStream.addTrack(videoTracks[i]);
  402. }
  403. }
  404. }
  405. else
  406. {//firefox
  407. audioStream = stream.audioStream;
  408. videoStream = stream.videoStream;
  409. }
  410. var audioMuted = (usageOptions && usageOptions.audio === false),
  411. videoMuted = (usageOptions && usageOptions.video === false);
  412. var audioGUM = (!usageOptions || usageOptions.audio !== false),
  413. videoGUM = (!usageOptions || usageOptions.video !== false);
  414. this.service.createLocalStream(audioStream, "audio", null, null,
  415. audioMuted, audioGUM);
  416. this.service.createLocalStream(videoStream, "video", null, null,
  417. videoMuted, videoGUM);
  418. };
  419. RTCUtils.prototype.createStream = function(stream, isVideo)
  420. {
  421. var newStream = null;
  422. if(window.webkitMediaStream)
  423. {
  424. newStream = new webkitMediaStream();
  425. if(newStream)
  426. {
  427. var tracks = (isVideo? stream.getVideoTracks() : stream.getAudioTracks());
  428. for (i = 0; i < tracks.length; i++) {
  429. newStream.addTrack(tracks[i]);
  430. }
  431. }
  432. }
  433. else
  434. newStream = stream;
  435. return newStream;
  436. };
  437. module.exports = RTCUtils;