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

Avatar.js 5.3KB

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