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.

mod_speakerstats_component.lua 7.2KB

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