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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 Settings = require("../../settings/Settings");
  17. var MediaStreamType = require("../../../service/RTC/MediaStreamTypes");
  18. var users = {};
  19. var activeSpeakerJid;
  20. function setVisibility(selector, show) {
  21. if (selector && selector.length > 0) {
  22. selector.css("visibility", show ? "visible" : "hidden");
  23. }
  24. }
  25. function isUserMuted(jid) {
  26. // XXX(gp) we may want to rename this method to something like
  27. // isUserStreaming, for example.
  28. if (jid && jid != APP.xmpp.myJid()) {
  29. var resource = Strophe.getResourceFromJid(jid);
  30. if (!require("../videolayout/VideoLayout").isInLastN(resource)) {
  31. return true;
  32. }
  33. }
  34. if(jid && jid == APP.xmpp.myJid())
  35. {
  36. var localVideo = APP.RTC.localVideo;
  37. return (!localVideo || localVideo.isMuted());
  38. }
  39. if (!APP.RTC.remoteStreams[jid] || !APP.RTC.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
  40. return null;
  41. }
  42. return APP.RTC.remoteStreams[jid][MediaStreamType.VIDEO_TYPE].muted;
  43. }
  44. function getGravatarUrl(id, size) {
  45. if(id === APP.xmpp.myJid() || !id) {
  46. id = Settings.getSettings().uid;
  47. }
  48. return 'https://www.gravatar.com/avatar/' +
  49. MD5.hexdigest(id.trim().toLowerCase()) +
  50. "?d=wavatar&size=" + (size || "30");
  51. }
  52. var Avatar = {
  53. /**
  54. * Sets the user's avatar in the settings menu(if local user), contact list
  55. * and thumbnail
  56. * @param jid jid of the user
  57. * @param id email or userID to be used as a hash
  58. */
  59. setUserAvatar: function (jid, id) {
  60. if (id) {
  61. if (users[jid] === id) {
  62. return;
  63. }
  64. users[jid] = id;
  65. }
  66. var thumbUrl = getGravatarUrl(users[jid] || jid, 100);
  67. var contactListUrl = getGravatarUrl(users[jid] || jid);
  68. var resourceJid = Strophe.getResourceFromJid(jid);
  69. var thumbnail = $('#participant_' + resourceJid);
  70. var avatar = $('#avatar_' + resourceJid);
  71. // set the avatar in the settings menu if it is local user and get the
  72. // local video container
  73. if (jid === APP.xmpp.myJid()) {
  74. $('#avatar').get(0).src = thumbUrl;
  75. thumbnail = $('#localVideoContainer');
  76. }
  77. // set the avatar in the contact list
  78. var contact = $('#' + resourceJid + '>img');
  79. if (contact && contact.length > 0) {
  80. contact.get(0).src = contactListUrl;
  81. }
  82. // set the avatar in the thumbnail
  83. if (avatar && avatar.length > 0) {
  84. avatar[0].src = thumbUrl;
  85. } else {
  86. if (thumbnail && thumbnail.length > 0) {
  87. avatar = document.createElement('img');
  88. avatar.id = 'avatar_' + resourceJid;
  89. avatar.className = 'userAvatar';
  90. avatar.src = thumbUrl;
  91. thumbnail.append(avatar);
  92. }
  93. }
  94. //if the user is the current active speaker - update the active speaker
  95. // avatar
  96. if (jid === activeSpeakerJid) {
  97. this.updateActiveSpeakerAvatarSrc(jid);
  98. }
  99. },
  100. /**
  101. * Hides or shows the user's avatar
  102. * @param jid jid of the user
  103. * @param show whether we should show the avatar or not
  104. * video because there is no dominant speaker and no focused speaker
  105. */
  106. showUserAvatar: function (jid, show) {
  107. if (users[jid]) {
  108. var resourceJid = Strophe.getResourceFromJid(jid);
  109. var video = $('#participant_' + resourceJid + '>video');
  110. var avatar = $('#avatar_' + resourceJid);
  111. if (jid === APP.xmpp.myJid()) {
  112. video = $('#localVideoWrapper>video');
  113. }
  114. if (show === undefined || show === null) {
  115. show = isUserMuted(jid);
  116. }
  117. //if the user is the currently focused, the dominant speaker or if
  118. //there is no focused and no dominant speaker and the large video is
  119. //currently shown
  120. if (activeSpeakerJid === jid && require("../videolayout/VideoLayout").isLargeVideoOnTop()) {
  121. setVisibility($("#largeVideo"), !show);
  122. setVisibility($('#activeSpeaker'), show);
  123. setVisibility(avatar, false);
  124. setVisibility(video, false);
  125. } else {
  126. if (video && video.length > 0) {
  127. setVisibility(video, !show);
  128. }
  129. setVisibility(avatar, show);
  130. }
  131. }
  132. },
  133. /**
  134. * Updates the src of the active speaker avatar
  135. * @param jid of the current active speaker
  136. */
  137. updateActiveSpeakerAvatarSrc: function (jid) {
  138. if (!jid) {
  139. jid = APP.xmpp.findJidFromResource(
  140. require("../videolayout/VideoLayout").getLargeVideoState().userResourceJid);
  141. }
  142. var avatar = $("#activeSpeakerAvatar")[0];
  143. var url = getGravatarUrl(users[jid],
  144. interfaceConfig.ACTIVE_SPEAKER_AVATAR_SIZE);
  145. if (jid === activeSpeakerJid && avatar.src === url) {
  146. return;
  147. }
  148. activeSpeakerJid = jid;
  149. var isMuted = isUserMuted(jid);
  150. if (jid && isMuted !== null) {
  151. avatar.src = url;
  152. setVisibility($("#largeVideo"), !isMuted);
  153. Avatar.showUserAvatar(jid, isMuted);
  154. }
  155. }
  156. };
  157. module.exports = Avatar;