2018-10-01 19:02:51 +00:00
|
|
|
local get_room_from_jid = module:require "util".get_room_from_jid;
|
2019-11-13 15:37:09 +00:00
|
|
|
local room_jid_match_rewrite = module:require "util".room_jid_match_rewrite;
|
2020-04-30 21:26:58 +00:00
|
|
|
local is_healthcheck_room = module:require "util".is_healthcheck_room;
|
2018-10-01 19:02:51 +00:00
|
|
|
local jid_resource = require "util.jid".resource;
|
|
|
|
local ext_events = module:require "ext_events"
|
2018-10-23 14:44:58 +00:00
|
|
|
local st = require "util.stanza";
|
|
|
|
local socket = require "socket";
|
|
|
|
local json = require "util.json";
|
2021-06-21 09:23:17 +00:00
|
|
|
local um_is_admin = require "core.usermanager".is_admin;
|
2018-10-01 19:02:51 +00:00
|
|
|
|
2019-12-05 10:09:58 +00:00
|
|
|
-- we use async to detect Prosody 0.10 and earlier
|
|
|
|
local have_async = pcall(require, "util.async");
|
|
|
|
if not have_async then
|
|
|
|
module:log("warn", "speaker stats will not work with Prosody version 0.10 or less.");
|
|
|
|
return;
|
|
|
|
end
|
|
|
|
|
2018-10-01 19:02:51 +00:00
|
|
|
local muc_component_host = module:get_option_string("muc_component");
|
|
|
|
if muc_component_host == nil then
|
|
|
|
log("error", "No muc_component specified. No muc to operate on!");
|
|
|
|
return;
|
|
|
|
end
|
|
|
|
|
2019-07-26 09:21:09 +00:00
|
|
|
log("info", "Starting speakerstats for %s", muc_component_host);
|
2018-10-01 19:02:51 +00:00
|
|
|
|
2021-06-21 09:23:17 +00:00
|
|
|
local function is_admin(jid)
|
|
|
|
return um_is_admin(jid, module.host);
|
|
|
|
end
|
|
|
|
|
2018-10-01 19:02:51 +00:00
|
|
|
-- receives messages from client currently connected to the room
|
|
|
|
-- clients indicates their own dominant speaker events
|
|
|
|
function on_message(event)
|
|
|
|
-- Check the type of the incoming stanza to avoid loops:
|
|
|
|
if event.stanza.attr.type == "error" then
|
|
|
|
return; -- We do not want to reply to these, so leave.
|
|
|
|
end
|
|
|
|
|
|
|
|
local speakerStats
|
|
|
|
= event.stanza:get_child('speakerstats', 'http://jitsi.org/jitmeet');
|
|
|
|
if speakerStats then
|
|
|
|
local roomAddress = speakerStats.attr.room;
|
2019-11-13 15:37:09 +00:00
|
|
|
local room = get_room_from_jid(room_jid_match_rewrite(roomAddress));
|
2018-10-01 19:02:51 +00:00
|
|
|
|
|
|
|
if not room then
|
|
|
|
log("warn", "No room found %s", roomAddress);
|
|
|
|
return false;
|
|
|
|
end
|
2021-11-17 14:33:03 +00:00
|
|
|
|
2020-10-05 14:32:48 +00:00
|
|
|
if not room.speakerStats then
|
|
|
|
log("warn", "No speakerStats found for %s", roomAddress);
|
|
|
|
return false;
|
|
|
|
end
|
2018-10-01 19:02:51 +00:00
|
|
|
|
|
|
|
local roomSpeakerStats = room.speakerStats;
|
|
|
|
local from = event.stanza.attr.from;
|
|
|
|
|
|
|
|
local occupant = room:get_occupant_by_real_jid(from);
|
|
|
|
if not occupant then
|
|
|
|
log("warn", "No occupant %s found for %s", from, roomAddress);
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
|
|
|
|
local newDominantSpeaker = roomSpeakerStats[occupant.jid];
|
|
|
|
local oldDominantSpeakerId = roomSpeakerStats['dominantSpeakerId'];
|
|
|
|
|
|
|
|
if oldDominantSpeakerId then
|
2020-03-06 11:33:41 +00:00
|
|
|
local oldDominantSpeaker = roomSpeakerStats[oldDominantSpeakerId];
|
|
|
|
if oldDominantSpeaker then
|
|
|
|
oldDominantSpeaker:setDominantSpeaker(false);
|
|
|
|
end
|
2018-10-01 19:02:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if newDominantSpeaker then
|
2018-12-28 09:53:22 +00:00
|
|
|
newDominantSpeaker:setDominantSpeaker(true);
|
2018-10-01 19:02:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
room.speakerStats['dominantSpeakerId'] = occupant.jid;
|
|
|
|
end
|
|
|
|
|
2022-04-06 09:10:31 +00:00
|
|
|
local faceExpression = event.stanza:get_child('faceExpression', 'http://jitsi.org/jitmeet');
|
2021-11-17 14:33:03 +00:00
|
|
|
|
2022-04-06 09:10:31 +00:00
|
|
|
if faceExpression then
|
|
|
|
local roomAddress = faceExpression.attr.room;
|
2021-11-17 14:33:03 +00:00
|
|
|
local room = get_room_from_jid(room_jid_match_rewrite(roomAddress));
|
|
|
|
|
|
|
|
if not room then
|
|
|
|
log("warn", "No room found %s", roomAddress);
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
if not room.speakerStats then
|
|
|
|
log("warn", "No speakerStats found for %s", roomAddress);
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
local from = event.stanza.attr.from;
|
|
|
|
|
|
|
|
local occupant = room:get_occupant_by_real_jid(from);
|
|
|
|
if not occupant then
|
|
|
|
log("warn", "No occupant %s found for %s", from, roomAddress);
|
|
|
|
return false;
|
|
|
|
end
|
2022-04-06 09:10:31 +00:00
|
|
|
local faceExpressions = room.speakerStats[occupant.jid].faceExpressions;
|
|
|
|
faceExpressions[faceExpression.attr.expression] =
|
|
|
|
faceExpressions[faceExpression.attr.expression] + tonumber(faceExpression.attr.duration);
|
2021-11-17 14:33:03 +00:00
|
|
|
end
|
|
|
|
|
2018-10-01 19:02:51 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Start SpeakerStats implementation
|
|
|
|
local SpeakerStats = {};
|
|
|
|
SpeakerStats.__index = SpeakerStats;
|
|
|
|
|
2019-10-23 07:58:14 +00:00
|
|
|
function new_SpeakerStats(nick, context_user)
|
2018-10-01 19:02:51 +00:00
|
|
|
return setmetatable({
|
|
|
|
totalDominantSpeakerTime = 0;
|
2018-12-28 09:53:22 +00:00
|
|
|
_dominantSpeakerStart = 0;
|
2018-10-01 19:02:51 +00:00
|
|
|
nick = nick;
|
2019-10-23 07:58:14 +00:00
|
|
|
context_user = context_user;
|
2018-10-01 19:02:51 +00:00
|
|
|
displayName = nil;
|
2022-04-06 09:10:31 +00:00
|
|
|
faceExpressions = {
|
2021-11-17 14:33:03 +00:00
|
|
|
happy = 0,
|
|
|
|
neutral = 0,
|
|
|
|
surprised = 0,
|
|
|
|
angry = 0,
|
|
|
|
fearful = 0,
|
|
|
|
disgusted = 0,
|
|
|
|
sad = 0
|
|
|
|
};
|
2018-10-01 19:02:51 +00:00
|
|
|
}, SpeakerStats);
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Changes the dominantSpeaker data for current occupant
|
|
|
|
-- saves start time if it is new dominat speaker
|
|
|
|
-- or calculates and accumulates time of speaking
|
2018-12-28 09:53:22 +00:00
|
|
|
function SpeakerStats:setDominantSpeaker(isNowDominantSpeaker)
|
2020-11-04 14:25:03 +00:00
|
|
|
-- log("debug", "set isDominant %s for %s", tostring(isNowDominantSpeaker), self.nick);
|
2018-10-01 19:02:51 +00:00
|
|
|
|
2018-12-28 09:53:22 +00:00
|
|
|
if not self:isDominantSpeaker() and isNowDominantSpeaker then
|
2018-10-23 14:44:58 +00:00
|
|
|
self._dominantSpeakerStart = socket.gettime()*1000;
|
2018-12-28 09:53:22 +00:00
|
|
|
elseif self:isDominantSpeaker() and not isNowDominantSpeaker then
|
2018-10-23 14:44:58 +00:00
|
|
|
local now = socket.gettime()*1000;
|
2018-12-28 09:53:22 +00:00
|
|
|
local timeElapsed = math.floor(now - self._dominantSpeakerStart);
|
2018-10-01 19:02:51 +00:00
|
|
|
|
|
|
|
self.totalDominantSpeakerTime
|
|
|
|
= self.totalDominantSpeakerTime + timeElapsed;
|
2018-12-28 09:53:22 +00:00
|
|
|
self._dominantSpeakerStart = 0;
|
2018-10-01 19:02:51 +00:00
|
|
|
end
|
2018-12-28 09:53:22 +00:00
|
|
|
end
|
2018-10-01 19:02:51 +00:00
|
|
|
|
2018-12-28 09:53:22 +00:00
|
|
|
-- Returns true if the tracked user is currently a dominant speaker.
|
|
|
|
function SpeakerStats:isDominantSpeaker()
|
|
|
|
return self._dominantSpeakerStart > 0;
|
2018-10-01 19:02:51 +00:00
|
|
|
end
|
|
|
|
--- End SpeakerStats
|
|
|
|
|
|
|
|
-- create speakerStats for the room
|
|
|
|
function room_created(event)
|
|
|
|
local room = event.room;
|
2020-04-30 21:26:58 +00:00
|
|
|
|
2020-05-12 21:40:39 +00:00
|
|
|
if is_healthcheck_room(room.jid) then
|
2020-04-30 21:26:58 +00:00
|
|
|
return;
|
|
|
|
end
|
|
|
|
|
2018-10-01 19:02:51 +00:00
|
|
|
room.speakerStats = {};
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Create SpeakerStats object for the joined user
|
|
|
|
function occupant_joined(event)
|
2021-06-21 09:23:17 +00:00
|
|
|
local occupant, room = event.occupant, event.room;
|
2020-04-30 21:26:58 +00:00
|
|
|
|
2021-06-21 09:23:17 +00:00
|
|
|
if is_healthcheck_room(room.jid) or is_admin(occupant.bare_jid) then
|
2020-04-30 21:26:58 +00:00
|
|
|
return;
|
|
|
|
end
|
|
|
|
|
2018-10-01 19:02:51 +00:00
|
|
|
local occupant = event.occupant;
|
2019-10-23 07:58:14 +00:00
|
|
|
|
2018-10-01 19:02:51 +00:00
|
|
|
local nick = jid_resource(occupant.nick);
|
|
|
|
|
|
|
|
if room.speakerStats then
|
2018-10-23 14:44:58 +00:00
|
|
|
-- lets send the current speaker stats to that user, so he can update
|
|
|
|
-- its local stats
|
|
|
|
if next(room.speakerStats) ~= nil then
|
|
|
|
local users_json = {};
|
|
|
|
for jid, values in pairs(room.speakerStats) do
|
|
|
|
-- skip reporting those without a nick('dominantSpeakerId')
|
|
|
|
-- and skip focus if sneaked into the table
|
|
|
|
if values.nick ~= nil and values.nick ~= 'focus' then
|
2021-06-21 09:23:17 +00:00
|
|
|
local totalDominantSpeakerTime = values.totalDominantSpeakerTime;
|
2022-04-06 09:10:31 +00:00
|
|
|
local faceExpressions = values.faceExpressions;
|
2021-11-17 14:33:03 +00:00
|
|
|
if totalDominantSpeakerTime > 0 or room:get_occupant_jid(jid) == nil or values:isDominantSpeaker()
|
2022-04-06 09:10:31 +00:00
|
|
|
or get_participant_expressions_count(faceExpressions) > 0 then
|
2021-06-21 09:23:17 +00:00
|
|
|
-- before sending we need to calculate current dominant speaker state
|
|
|
|
if values:isDominantSpeaker() then
|
|
|
|
local timeElapsed = math.floor(socket.gettime()*1000 - values._dominantSpeakerStart);
|
|
|
|
totalDominantSpeakerTime = totalDominantSpeakerTime + timeElapsed;
|
|
|
|
end
|
|
|
|
|
|
|
|
users_json[values.nick] = {
|
|
|
|
displayName = values.displayName,
|
2021-11-17 14:33:03 +00:00
|
|
|
totalDominantSpeakerTime = totalDominantSpeakerTime,
|
2022-04-06 09:10:31 +00:00
|
|
|
faceExpressions = faceExpressions
|
2021-06-21 09:23:17 +00:00
|
|
|
};
|
2018-10-23 14:44:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-15 13:31:29 +00:00
|
|
|
if next(users_json) ~= nil then
|
|
|
|
local body_json = {};
|
|
|
|
body_json.type = 'speakerstats';
|
|
|
|
body_json.users = users_json;
|
|
|
|
|
|
|
|
local stanza = st.message({
|
|
|
|
from = module.host;
|
|
|
|
to = occupant.jid; })
|
|
|
|
:tag("json-message", {xmlns='http://jitsi.org/jitmeet'})
|
|
|
|
:text(json.encode(body_json)):up();
|
|
|
|
|
|
|
|
room:route_stanza(stanza);
|
2021-06-21 09:23:17 +00:00
|
|
|
end
|
2018-10-23 14:44:58 +00:00
|
|
|
end
|
|
|
|
|
2019-10-23 07:58:14 +00:00
|
|
|
local context_user = event.origin and event.origin.jitsi_meet_context_user or nil;
|
|
|
|
room.speakerStats[occupant.jid] = new_SpeakerStats(nick, context_user);
|
2018-10-01 19:02:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Occupant left set its dominant speaker to false and update the store the
|
|
|
|
-- display name
|
|
|
|
function occupant_leaving(event)
|
|
|
|
local room = event.room;
|
2020-04-30 21:26:58 +00:00
|
|
|
|
2020-05-12 21:40:39 +00:00
|
|
|
if is_healthcheck_room(room.jid) then
|
2020-04-30 21:26:58 +00:00
|
|
|
return;
|
|
|
|
end
|
2021-11-17 14:33:03 +00:00
|
|
|
|
2020-10-05 14:32:48 +00:00
|
|
|
if not room.speakerStats then
|
|
|
|
return;
|
|
|
|
end
|
2020-04-30 21:26:58 +00:00
|
|
|
|
2018-10-01 19:02:51 +00:00
|
|
|
local occupant = event.occupant;
|
|
|
|
|
|
|
|
local speakerStatsForOccupant = room.speakerStats[occupant.jid];
|
|
|
|
if speakerStatsForOccupant then
|
2018-12-28 09:53:22 +00:00
|
|
|
speakerStatsForOccupant:setDominantSpeaker(false);
|
2018-10-01 19:02:51 +00:00
|
|
|
|
|
|
|
-- set display name
|
|
|
|
local displayName = occupant:get_presence():get_child_text(
|
|
|
|
'nick', 'http://jabber.org/protocol/nick');
|
|
|
|
speakerStatsForOccupant.displayName = displayName;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Conference ended, send speaker stats
|
|
|
|
function room_destroyed(event)
|
|
|
|
local room = event.room;
|
|
|
|
|
2020-05-12 21:40:39 +00:00
|
|
|
if is_healthcheck_room(room.jid) then
|
2020-04-30 21:26:58 +00:00
|
|
|
return;
|
|
|
|
end
|
|
|
|
|
2018-10-01 19:02:51 +00:00
|
|
|
ext_events.speaker_stats(room, room.speakerStats);
|
|
|
|
end
|
|
|
|
|
|
|
|
module:hook("message/host", on_message);
|
2019-07-26 09:21:09 +00:00
|
|
|
|
|
|
|
-- executed on every host added internally in prosody, including components
|
|
|
|
function process_host(host)
|
|
|
|
if host == muc_component_host then -- the conference muc component
|
|
|
|
module:log("info","Hook to muc events on %s", host);
|
|
|
|
|
|
|
|
local muc_module = module:context(host);
|
|
|
|
muc_module:hook("muc-room-created", room_created, -1);
|
|
|
|
muc_module:hook("muc-occupant-joined", occupant_joined, -1);
|
|
|
|
muc_module:hook("muc-occupant-pre-leave", occupant_leaving, -1);
|
|
|
|
muc_module:hook("muc-room-destroyed", room_destroyed, -1);
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if prosody.hosts[muc_component_host] == nil then
|
|
|
|
module:log("info","No muc component found, will listen for it: %s", muc_component_host)
|
|
|
|
|
|
|
|
-- when a host or component is added
|
|
|
|
prosody.events.add_handler("host-activated", process_host);
|
|
|
|
else
|
|
|
|
process_host(muc_component_host);
|
2019-12-05 10:09:58 +00:00
|
|
|
end
|
2021-11-17 14:33:03 +00:00
|
|
|
|
2022-04-06 09:10:31 +00:00
|
|
|
function get_participant_expressions_count(faceExpressions)
|
2021-11-17 14:33:03 +00:00
|
|
|
local count = 0;
|
2022-04-06 09:10:31 +00:00
|
|
|
for expression, value in pairs(faceExpressions) do
|
2021-11-17 14:33:03 +00:00
|
|
|
count = count + value;
|
|
|
|
end
|
|
|
|
|
|
|
|
return count;
|
|
|
|
end
|