您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

mod_speakerstats_component.lua 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. local get_room_from_jid = module:require "util".get_room_from_jid;
  2. local room_jid_match_rewrite = module:require "util".room_jid_match_rewrite;
  3. local is_healthcheck_room = module:require "util".is_healthcheck_room;
  4. local jid_resource = require "util.jid".resource;
  5. local ext_events = module:require "ext_events"
  6. local st = require "util.stanza";
  7. local socket = require "socket";
  8. local json = require "util.json";
  9. -- we use async to detect Prosody 0.10 and earlier
  10. local have_async = pcall(require, "util.async");
  11. if not have_async then
  12. module:log("warn", "speaker stats will not work with Prosody version 0.10 or less.");
  13. return;
  14. end
  15. local muc_component_host = module:get_option_string("muc_component");
  16. if muc_component_host == nil then
  17. log("error", "No muc_component specified. No muc to operate on!");
  18. return;
  19. end
  20. log("info", "Starting speakerstats for %s", muc_component_host);
  21. -- receives messages from client currently connected to the room
  22. -- clients indicates their own dominant speaker events
  23. function on_message(event)
  24. -- Check the type of the incoming stanza to avoid loops:
  25. if event.stanza.attr.type == "error" then
  26. return; -- We do not want to reply to these, so leave.
  27. end
  28. local speakerStats
  29. = event.stanza:get_child('speakerstats', 'http://jitsi.org/jitmeet');
  30. if speakerStats then
  31. local roomAddress = speakerStats.attr.room;
  32. local room = get_room_from_jid(room_jid_match_rewrite(roomAddress));
  33. if not room then
  34. log("warn", "No room found %s", roomAddress);
  35. return false;
  36. end
  37. local roomSpeakerStats = room.speakerStats;
  38. local from = event.stanza.attr.from;
  39. local occupant = room:get_occupant_by_real_jid(from);
  40. if not occupant then
  41. log("warn", "No occupant %s found for %s", from, roomAddress);
  42. return false;
  43. end
  44. local newDominantSpeaker = roomSpeakerStats[occupant.jid];
  45. local oldDominantSpeakerId = roomSpeakerStats['dominantSpeakerId'];
  46. if oldDominantSpeakerId then
  47. local oldDominantSpeaker = roomSpeakerStats[oldDominantSpeakerId];
  48. if oldDominantSpeaker then
  49. oldDominantSpeaker:setDominantSpeaker(false);
  50. end
  51. end
  52. if newDominantSpeaker then
  53. newDominantSpeaker:setDominantSpeaker(true);
  54. end
  55. room.speakerStats['dominantSpeakerId'] = occupant.jid;
  56. end
  57. return true
  58. end
  59. --- Start SpeakerStats implementation
  60. local SpeakerStats = {};
  61. SpeakerStats.__index = SpeakerStats;
  62. function new_SpeakerStats(nick, context_user)
  63. return setmetatable({
  64. totalDominantSpeakerTime = 0;
  65. _dominantSpeakerStart = 0;
  66. nick = nick;
  67. context_user = context_user;
  68. displayName = nil;
  69. }, SpeakerStats);
  70. end
  71. -- Changes the dominantSpeaker data for current occupant
  72. -- saves start time if it is new dominat speaker
  73. -- or calculates and accumulates time of speaking
  74. function SpeakerStats:setDominantSpeaker(isNowDominantSpeaker)
  75. log("debug",
  76. "set isDominant %s for %s", tostring(isNowDominantSpeaker), self.nick);
  77. if not self:isDominantSpeaker() and isNowDominantSpeaker then
  78. self._dominantSpeakerStart = socket.gettime()*1000;
  79. elseif self:isDominantSpeaker() and not isNowDominantSpeaker then
  80. local now = socket.gettime()*1000;
  81. local timeElapsed = math.floor(now - self._dominantSpeakerStart);
  82. self.totalDominantSpeakerTime
  83. = self.totalDominantSpeakerTime + timeElapsed;
  84. self._dominantSpeakerStart = 0;
  85. end
  86. end
  87. -- Returns true if the tracked user is currently a dominant speaker.
  88. function SpeakerStats:isDominantSpeaker()
  89. return self._dominantSpeakerStart > 0;
  90. end
  91. --- End SpeakerStats
  92. -- create speakerStats for the room
  93. function room_created(event)
  94. local room = event.room;
  95. if is_healthcheck_room(room) then
  96. return;
  97. end
  98. room.speakerStats = {};
  99. end
  100. -- Create SpeakerStats object for the joined user
  101. function occupant_joined(event)
  102. local room = event.room;
  103. if is_healthcheck_room(room) then
  104. return;
  105. end
  106. local occupant = event.occupant;
  107. local nick = jid_resource(occupant.nick);
  108. if room.speakerStats then
  109. -- lets send the current speaker stats to that user, so he can update
  110. -- its local stats
  111. if next(room.speakerStats) ~= nil then
  112. local users_json = {};
  113. for jid, values in pairs(room.speakerStats) do
  114. -- skip reporting those without a nick('dominantSpeakerId')
  115. -- and skip focus if sneaked into the table
  116. if values.nick ~= nil and values.nick ~= 'focus' then
  117. local resultSpeakerStats = {};
  118. local totalDominantSpeakerTime
  119. = values.totalDominantSpeakerTime;
  120. -- before sending we need to calculate current dominant speaker
  121. -- state
  122. if values:isDominantSpeaker() then
  123. local timeElapsed = math.floor(
  124. socket.gettime()*1000 - values._dominantSpeakerStart);
  125. totalDominantSpeakerTime = totalDominantSpeakerTime
  126. + timeElapsed;
  127. end
  128. resultSpeakerStats.displayName = values.displayName;
  129. resultSpeakerStats.totalDominantSpeakerTime
  130. = totalDominantSpeakerTime;
  131. users_json[values.nick] = resultSpeakerStats;
  132. end
  133. end
  134. local body_json = {};
  135. body_json.type = 'speakerstats';
  136. body_json.users = users_json;
  137. local stanza = st.message({
  138. from = module.host;
  139. to = occupant.jid; })
  140. :tag("json-message", {xmlns='http://jitsi.org/jitmeet'})
  141. :text(json.encode(body_json)):up();
  142. room:route_stanza(stanza);
  143. end
  144. local context_user = event.origin and event.origin.jitsi_meet_context_user or nil;
  145. room.speakerStats[occupant.jid] = new_SpeakerStats(nick, context_user);
  146. end
  147. end
  148. -- Occupant left set its dominant speaker to false and update the store the
  149. -- display name
  150. function occupant_leaving(event)
  151. local room = event.room;
  152. if is_healthcheck_room(room) then
  153. return;
  154. end
  155. local occupant = event.occupant;
  156. local speakerStatsForOccupant = room.speakerStats[occupant.jid];
  157. if speakerStatsForOccupant then
  158. speakerStatsForOccupant:setDominantSpeaker(false);
  159. -- set display name
  160. local displayName = occupant:get_presence():get_child_text(
  161. 'nick', 'http://jabber.org/protocol/nick');
  162. speakerStatsForOccupant.displayName = displayName;
  163. end
  164. end
  165. -- Conference ended, send speaker stats
  166. function room_destroyed(event)
  167. local room = event.room;
  168. if is_healthcheck_room(room) then
  169. return;
  170. end
  171. ext_events.speaker_stats(room, room.speakerStats);
  172. end
  173. module:hook("message/host", on_message);
  174. -- executed on every host added internally in prosody, including components
  175. function process_host(host)
  176. if host == muc_component_host then -- the conference muc component
  177. module:log("info","Hook to muc events on %s", host);
  178. local muc_module = module:context(host);
  179. muc_module:hook("muc-room-created", room_created, -1);
  180. muc_module:hook("muc-occupant-joined", occupant_joined, -1);
  181. muc_module:hook("muc-occupant-pre-leave", occupant_leaving, -1);
  182. muc_module:hook("muc-room-destroyed", room_destroyed, -1);
  183. end
  184. end
  185. if prosody.hosts[muc_component_host] == nil then
  186. module:log("info","No muc component found, will listen for it: %s", muc_component_host)
  187. -- when a host or component is added
  188. prosody.events.add_handler("host-activated", process_host);
  189. else
  190. process_host(muc_component_host);
  191. end