Skip to content

Commit

Permalink
Add talking events to AudioBridge plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
atoppi committed Dec 17, 2021
1 parent c2c8b26 commit be5a5b0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
13 changes: 12 additions & 1 deletion examples/audiobridge/html/audiobridge-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function _listRooms() {
});
}

function _create({ room = myRoom, description, permanent = false, pin = null, secret = null, allow_rtp = true, bitrate = 0, expected_loss = 0, groups } = {}) {
function _create({ room = myRoom, description, permanent = false, pin = null, secret = null, allow_rtp = true, bitrate = 0, expected_loss = 0, talking_events = false, talking_level_threshold = 25, talking_packets_threshold = 100, groups } = {}) {
socket.emit('create', {
data: {
room,
Expand All @@ -159,6 +159,9 @@ function _create({ room = myRoom, description, permanent = false, pin = null, se
allow_rtp,
bitrate,
expected_loss,
talking_events,
talking_level_threshold,
talking_packets_threshold,
groups,
pin,
secret,
Expand Down Expand Up @@ -332,6 +335,14 @@ socket.on('peer-leaving', ({ data }) => {
removeAudioElement(data.feed);
});

socket.on('talking', ({ data }) => {
console.log('own talking notify', data);
});

socket.on('peer-talking', ({ data }) => {
console.log('peer talking notify', data);
});

socket.on('exists', ({ data }) => {
console.log('room exists', data);
});
Expand Down
8 changes: 8 additions & 0 deletions examples/audiobridge/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ function initFrontEnd() {
replyEvent(socket, 'peer-kicked', evtdata);
});

audioHandle.on(AudioBridgePlugin.EVENT.AUDIOBRIDGE_TALKING, evtdata => {
replyEvent(socket, 'talking', evtdata);
});

audioHandle.on(AudioBridgePlugin.EVENT.AUDIOBRIDGE_PEER_TALKING, evtdata => {
replyEvent(socket, 'peer-talking', evtdata);
});

// generic audiobridge events
audioHandle.on(Janode.EVENT.HANDLE_WEBRTCUP, () => Logger.info(`${LOG_NS} ${audioHandle.name} webrtcup event`));
audioHandle.on(Janode.EVENT.HANDLE_MEDIA, evtdata => Logger.info(`${LOG_NS} ${audioHandle.name} media event ${JSON.stringify(evtdata)}`));
Expand Down
51 changes: 48 additions & 3 deletions src/plugins/audiobridge-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const PLUGIN_EVENT = {
PEER_LEAVING: 'audiobridge_peer_leaving',
KICKED: 'audiobridge_kicked',
PEER_KICKED: 'audiobridge_peer_kicked',
TALKING: 'audiobridge_talking',
PEER_TALKING: 'audiobridge_peer_talking',
EXISTS: 'audiobridge_exists',
ROOMS_LIST: 'audiobridge_list',
CREATED: 'audiobridge_created',
Expand Down Expand Up @@ -172,14 +174,15 @@ class AudioBridgeHandle extends Handle {
if (typeof message_data.setup !== 'undefined') janode_event.data.setup = message_data.setup;
if (typeof message_data.rtp !== 'undefined') janode_event.data.rtp = message_data.rtp;
/* Add participants data */
janode_event.data.participants = message_data.participants.map(({ id, display, muted, setup, rtp }) => {
janode_event.data.participants = message_data.participants.map(({ id, display, muted, setup, rtp, talking }) => {
const peer = {
feed: id,
display,
muted,
setup,
rtp,
};
if (typeof talking !== 'undefined') peer.talking = talking;
return peer;
});
janode_event.event = PLUGIN_EVENT.JOINED;
Expand All @@ -191,20 +194,22 @@ class AudioBridgeHandle extends Handle {
if (typeof message_data.participants[0].muted !== 'undefined') janode_event.data.muted = message_data.participants[0].muted;
if (typeof message_data.participants[0].setup !== 'undefined') janode_event.data.setup = message_data.participants[0].setup;
if (typeof message_data.participants[0].rtp !== 'undefined') janode_event.data.rtp = message_data.participants[0].rtp;
if (typeof message_data.participants[0].talking !== 'undefined') janode_event.data.talking = message_data.participants[0].talking;
janode_event.event = PLUGIN_EVENT.PEER_JOINED;
}
break;

/* Participants list */
case 'participants':
janode_event.data.participants = message_data.participants.map(({ id, display, muted, setup, rtp }) => {
janode_event.data.participants = message_data.participants.map(({ id, display, muted, setup, rtp, talking }) => {
const peer = {
feed: id,
display,
muted,
setup,
rtp,
};
if (typeof talking !== 'undefined') peer.talking = talking;
return peer;
});
janode_event.event = PLUGIN_EVENT.PARTICIPANTS_LIST;
Expand Down Expand Up @@ -250,6 +255,14 @@ class AudioBridgeHandle extends Handle {
janode_event.event = PLUGIN_EVENT.FWD_LIST;
break;

/* Talking events */
case 'talking':
case 'stopped-talking':
janode_event.data.feed = message_data.id;
janode_event.data.talking = (audiobridge === 'talking');
janode_event.event = message_data.id !== this.feed ? PLUGIN_EVENT.PEER_TALKING : PLUGIN_EVENT.TALKING;
break;

/* Generic event (e.g. errors) */
case 'event':
/* AudioBridge error */
Expand Down Expand Up @@ -563,13 +576,17 @@ class AudioBridgeHandle extends Handle {
* @param {string} [params.pin] - The ping needed for joining the room
* @param {boolean} [params.record] - True to record the mixed audio
* @param {string} [params.filename] - The recording filename
* @param {boolean} [params.talking_events] - True to enable talking events
* @param {number} [params.talking_level_threshold] - Audio level threshold for talking events in the range [0, 127]
* @param {number} [params.talking_packets_threshold] - Audio packets threshold for talking events
* @param {number} [params.expected_loss] - The expected loss percentage in the audiobridge, if > 0 enables FEC
* @param {number} [params.prebuffer] - The prebuffer to use for every participant
* @param {boolean} [params.allow_rtp] - Allow plain RTP participants
* @param {string[]} [params.groups] - The available groups in the room
* @returns {Promise<module:audiobridge-plugin~AUDIOBRIDGE_EVENT_CREATED>}
*/
async create({ room, description, permanent, sampling_rate, bitrate, is_private, secret, pin, record, filename, expected_loss, prebuffer, allow_rtp, groups }) {
async create({ room, description, permanent, sampling_rate, bitrate, is_private, secret, pin, record, filename,
talking_events, talking_level_threshold, talking_packets_threshold, expected_loss, prebuffer, allow_rtp, groups }) {
const body = {
request: REQUEST_CREATE,
room,
Expand All @@ -583,6 +600,9 @@ class AudioBridgeHandle extends Handle {
if (typeof pin === 'string') body.pin = pin;
if (typeof record === 'boolean') body.record = record;
if (typeof filename === 'string') body.record_file = filename;
if (typeof talking_events === 'boolean') body.audiolevel_event = talking_events;
if (typeof talking_level_threshold === 'number' && talking_level_threshold >= 0 && talking_level_threshold <= 127) body.audio_level_average = talking_level_threshold;
if (typeof talking_packets_threshold === 'number' && talking_packets_threshold > 0) body.audio_active_packets = talking_packets_threshold;
if (typeof expected_loss === 'number') body.default_expectedloss = expected_loss;
if (typeof prebuffer === 'number') body.default_prebuffering = prebuffer;
if (typeof allow_rtp === 'boolean') body.allow_rtp_participants = allow_rtp;
Expand Down Expand Up @@ -799,6 +819,7 @@ class AudioBridgeHandle extends Handle {
* @property {string} [participants[].display] - The participant display name
* @property {boolean} [participants[].muted] - The muted status of the participant
* @property {boolean} [participants[].setup] - True if participant PeerConnection is up
* @property {boolean} [participants[].talking] - True if participant is talking
*/

/**
Expand Down Expand Up @@ -885,6 +906,8 @@ class AudioBridgeHandle extends Handle {
* @property {string} EVENT.AUDIOBRIDGE_PEER_CONFIGURED {@link module:audiobridge-plugin~AUDIOBRIDGE_PEER_CONFIGURED}
* @property {string} EVENT.AUDIOBRIDGE_PEER_KICKED {@link module:audiobridge-plugin~AUDIOBRIDGE_PEER_KICKED}
* @property {string} EVENT.AUDIOBRIDGE_PEER_LEAVING {@link module:audiobridge-plugin~AUDIOBRIDGE_PEER_LEAVING}
* @property {string} EVENT.AUDIOBRIDGE_TALKING {@link module:audiobridge-plugin~AUDIOBRIDGE_TALKING}
* @property {string} EVENT.AUDIOBRIDGE_PEER_TALKING {@link module:audiobridge-plugin~AUDIOBRIDGE_PEER_TALKING}
* @property {string} EVENT.AUDIOBRIDGE_ERROR {@link module:audiobridge-plugin~AUDIOBRIDGE_ERROR}
*/
module.exports = {
Expand Down Expand Up @@ -957,6 +980,28 @@ module.exports = {
*/
AUDIOBRIDGE_PEER_LEAVING: PLUGIN_EVENT.PEER_LEAVING,

/**
* Notify if the current user is talking.
*
* @event module:audiobridge-plugin~AudioBridgeHandle#event:AUDIOBRIDGE_TALKING
* @type {object}
* @property {number|string} room
* @property {number|string} feed
* @property {boolean} talking
*/
AUDIOBRIDGE_TALKING: PLUGIN_EVENT.TALKING,

/**
* Notify if a participant is talking.
*
* @event module:audiobridge-plugin~AudioBridgeHandle#event:AUDIOBRIDGE_PEER_TALKING
* @type {object}
* @property {number|string} room
* @property {number|string} feed
* @property {boolean} talking
*/
AUDIOBRIDGE_PEER_TALKING: PLUGIN_EVENT.PEER_TALKING,

/**
* Generic audiobridge error.
*
Expand Down

0 comments on commit be5a5b0

Please sign in to comment.