選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RTCUtils.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //This should be uncommented when app.js supports require
  2. //var RTCBrowserType = require("../../service/RTC/RTCBrowserType.js");
  3. var constraints = {audio: false, video: false};
  4. function setResolutionConstraints(resolution, isAndroid)
  5. {
  6. if (resolution && !constraints.video || isAndroid) {
  7. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  8. }
  9. // see https://code.google.com/p/chromium/issues/detail?id=143631#c9 for list of supported resolutions
  10. switch (resolution) {
  11. // 16:9 first
  12. case '1080':
  13. case 'fullhd':
  14. constraints.video.mandatory.minWidth = 1920;
  15. constraints.video.mandatory.minHeight = 1080;
  16. break;
  17. case '720':
  18. case 'hd':
  19. constraints.video.mandatory.minWidth = 1280;
  20. constraints.video.mandatory.minHeight = 720;
  21. break;
  22. case '360':
  23. constraints.video.mandatory.minWidth = 640;
  24. constraints.video.mandatory.minHeight = 360;
  25. break;
  26. case '180':
  27. constraints.video.mandatory.minWidth = 320;
  28. constraints.video.mandatory.minHeight = 180;
  29. break;
  30. // 4:3
  31. case '960':
  32. constraints.video.mandatory.minWidth = 960;
  33. constraints.video.mandatory.minHeight = 720;
  34. break;
  35. case '640':
  36. case 'vga':
  37. constraints.video.mandatory.minWidth = 640;
  38. constraints.video.mandatory.minHeight = 480;
  39. break;
  40. case '320':
  41. constraints.video.mandatory.minWidth = 320;
  42. constraints.video.mandatory.minHeight = 240;
  43. break;
  44. default:
  45. if (isAndroid) {
  46. constraints.video.mandatory.minWidth = 320;
  47. constraints.video.mandatory.minHeight = 240;
  48. constraints.video.mandatory.maxFrameRate = 15;
  49. }
  50. break;
  51. }
  52. if (constraints.video.mandatory.minWidth)
  53. constraints.video.mandatory.maxWidth = constraints.video.mandatory.minWidth;
  54. if (constraints.video.mandatory.minHeight)
  55. constraints.video.mandatory.maxHeight = constraints.video.mandatory.minHeight;
  56. }
  57. function setConstraints(um, resolution, bandwidth, fps, desktopStream, isAndroid)
  58. {
  59. if (um.indexOf('video') >= 0) {
  60. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  61. }
  62. if (um.indexOf('audio') >= 0) {
  63. constraints.audio = { mandatory: {}, optional: []};// same behaviour as true
  64. }
  65. if (um.indexOf('screen') >= 0) {
  66. constraints.video = {
  67. mandatory: {
  68. chromeMediaSource: "screen",
  69. googLeakyBucket: true,
  70. maxWidth: window.screen.width,
  71. maxHeight: window.screen.height,
  72. maxFrameRate: 3
  73. },
  74. optional: []
  75. };
  76. }
  77. if (um.indexOf('desktop') >= 0) {
  78. constraints.video = {
  79. mandatory: {
  80. chromeMediaSource: "desktop",
  81. chromeMediaSourceId: desktopStream,
  82. googLeakyBucket: true,
  83. maxWidth: window.screen.width,
  84. maxHeight: window.screen.height,
  85. maxFrameRate: 3
  86. },
  87. optional: []
  88. };
  89. }
  90. if (constraints.audio) {
  91. // if it is good enough for hangouts...
  92. constraints.audio.optional.push(
  93. {googEchoCancellation: true},
  94. {googAutoGainControl: true},
  95. {googNoiseSupression: true},
  96. {googHighpassFilter: true},
  97. {googNoisesuppression2: true},
  98. {googEchoCancellation2: true},
  99. {googAutoGainControl2: true}
  100. );
  101. }
  102. if (constraints.video) {
  103. constraints.video.optional.push(
  104. {googNoiseReduction: false} // chrome 37 workaround for issue 3807, reenable in M38
  105. );
  106. if (um.indexOf('video') >= 0) {
  107. constraints.video.optional.push(
  108. {googLeakyBucket: true}
  109. );
  110. }
  111. }
  112. setResolutionConstraints(resolution, isAndroid);
  113. if (bandwidth) { // doesn't work currently, see webrtc issue 1846
  114. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};//same behaviour as true
  115. constraints.video.optional.push({bandwidth: bandwidth});
  116. }
  117. if (fps) { // for some cameras it might be necessary to request 30fps
  118. // so they choose 30fps mjpg over 10fps yuy2
  119. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};// same behaviour as true;
  120. constraints.video.mandatory.minFrameRate = fps;
  121. }
  122. }
  123. function RTCUtils(RTCService)
  124. {
  125. this.service = RTCService;
  126. if (navigator.mozGetUserMedia) {
  127. console.log('This appears to be Firefox');
  128. var version = parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  129. if (version >= 22) {
  130. this.peerconnection = mozRTCPeerConnection;
  131. this.browser = RTCBrowserType.RTC_BROWSER_FIREFOX;
  132. this.getUserMedia = navigator.mozGetUserMedia.bind(navigator);
  133. this.pc_constraints = {};
  134. this.attachMediaStream = function (element, stream) {
  135. element[0].mozSrcObject = stream;
  136. element[0].play();
  137. };
  138. this.getStreamID = function (stream) {
  139. var tracks = stream.getVideoTracks();
  140. if(!tracks || tracks.length == 0)
  141. {
  142. tracks = stream.getAudioTracks();
  143. }
  144. return tracks[0].id.replace(/[\{,\}]/g,"");
  145. };
  146. this.getVideoSrc = function (element) {
  147. return element.mozSrcObject;
  148. };
  149. this.setVideoSrc = function (element, src) {
  150. element.mozSrcObject = src;
  151. };
  152. RTCSessionDescription = mozRTCSessionDescription;
  153. RTCIceCandidate = mozRTCIceCandidate;
  154. }
  155. } else if (navigator.webkitGetUserMedia) {
  156. console.log('This appears to be Chrome');
  157. this.peerconnection = webkitRTCPeerConnection;
  158. this.browser = RTCBrowserType.RTC_BROWSER_CHROME;
  159. this.getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
  160. this.attachMediaStream = function (element, stream) {
  161. element.attr('src', webkitURL.createObjectURL(stream));
  162. };
  163. this.getStreamID = function (stream) {
  164. // streams from FF endpoints have the characters '{' and '}'
  165. // that make jQuery choke.
  166. return stream.id.replace(/[\{,\}]/g,"");
  167. };
  168. this.getVideoSrc = function (element) {
  169. return element.getAttribute("src");
  170. };
  171. this.setVideoSrc = function (element, src) {
  172. element.setAttribute("src", src);
  173. };
  174. // DTLS should now be enabled by default but..
  175. this.pc_constraints = {'optional': [{'DtlsSrtpKeyAgreement': 'true'}]};
  176. if (navigator.userAgent.indexOf('Android') != -1) {
  177. this.pc_constraints = {}; // disable DTLS on Android
  178. }
  179. if (!webkitMediaStream.prototype.getVideoTracks) {
  180. webkitMediaStream.prototype.getVideoTracks = function () {
  181. return this.videoTracks;
  182. };
  183. }
  184. if (!webkitMediaStream.prototype.getAudioTracks) {
  185. webkitMediaStream.prototype.getAudioTracks = function () {
  186. return this.audioTracks;
  187. };
  188. }
  189. }
  190. else
  191. {
  192. try { console.log('Browser does not appear to be WebRTC-capable'); } catch (e) { }
  193. window.location.href = 'webrtcrequired.html';
  194. return;
  195. }
  196. if (this.browser !== RTCBrowserType.RTC_BROWSER_CHROME &&
  197. config.enableFirefoxSupport !== true) {
  198. window.location.href = 'chromeonly.html';
  199. return;
  200. }
  201. }
  202. RTCUtils.prototype.getUserMediaWithConstraints = function(
  203. um, success_callback, failure_callback, resolution,bandwidth, fps,
  204. desktopStream)
  205. {
  206. // Check if we are running on Android device
  207. var isAndroid = navigator.userAgent.indexOf('Android') != -1;
  208. setConstraints(um, resolution, bandwidth, fps, desktopStream, isAndroid);
  209. var isFF = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
  210. try {
  211. if (config.enableSimulcast
  212. && constraints.video
  213. && constraints.video.chromeMediaSource !== 'screen'
  214. && constraints.video.chromeMediaSource !== 'desktop'
  215. && !isAndroid
  216. // We currently do not support FF, as it doesn't have multistream support.
  217. && !isFF) {
  218. simulcast.getUserMedia(constraints, function (stream) {
  219. console.log('onUserMediaSuccess');
  220. success_callback(stream);
  221. },
  222. function (error) {
  223. console.warn('Failed to get access to local media. Error ', error);
  224. if (failure_callback) {
  225. failure_callback(error);
  226. }
  227. });
  228. } else {
  229. this.getUserMedia(constraints,
  230. function (stream) {
  231. console.log('onUserMediaSuccess');
  232. success_callback(stream);
  233. },
  234. function (error) {
  235. console.warn('Failed to get access to local media. Error ', error);
  236. if (failure_callback) {
  237. failure_callback(error);
  238. }
  239. });
  240. }
  241. } catch (e) {
  242. console.error('GUM failed: ', e);
  243. if(failure_callback) {
  244. failure_callback(e);
  245. }
  246. }
  247. };
  248. /**
  249. * We ask for audio and video combined stream in order to get permissions and
  250. * not to ask twice.
  251. */
  252. RTCUtils.prototype.obtainAudioAndVideoPermissions = function() {
  253. var self = this;
  254. // Get AV
  255. var cb = function (stream) {
  256. console.log('got', stream, stream.getAudioTracks().length, stream.getVideoTracks().length);
  257. self.handleLocalStream(stream);
  258. trackUsage('localMedia', {
  259. audio: stream.getAudioTracks().length,
  260. video: stream.getVideoTracks().length
  261. });
  262. };
  263. var self = this;
  264. this.getUserMediaWithConstraints(
  265. ['audio', 'video'],
  266. cb,
  267. function (error) {
  268. console.error('failed to obtain audio/video stream - trying audio only', error);
  269. self.getUserMediaWithConstraints(
  270. ['audio'],
  271. cb,
  272. function (error) {
  273. console.error('failed to obtain audio/video stream - stop', error);
  274. trackUsage('localMediaError', {
  275. media: error.media || 'video',
  276. name : error.name
  277. });
  278. messageHandler.showError("Error",
  279. "Failed to obtain permissions to use the local microphone" +
  280. "and/or camera.");
  281. }
  282. );
  283. },
  284. config.resolution || '360');
  285. }
  286. RTCUtils.prototype.handleLocalStream = function(stream)
  287. {
  288. if(window.webkitMediaStream)
  289. {
  290. var audioStream = new webkitMediaStream();
  291. var videoStream = new webkitMediaStream();
  292. var audioTracks = stream.getAudioTracks();
  293. var videoTracks = stream.getVideoTracks();
  294. for (var i = 0; i < audioTracks.length; i++) {
  295. audioStream.addTrack(audioTracks[i]);
  296. }
  297. this.service.createLocalStream(audioStream, "audio");
  298. for (i = 0; i < videoTracks.length; i++) {
  299. videoStream.addTrack(videoTracks[i]);
  300. }
  301. this.service.createLocalStream(videoStream, "video");
  302. }
  303. else
  304. {//firefox
  305. this.service.createLocalStream(stream, "stream");
  306. }
  307. };
  308. module.exports = RTCUtils;