Skip to content

Commit

Permalink
Merge branch 'master' into es-module
Browse files Browse the repository at this point in the history
  • Loading branch information
atoppi committed Dec 21, 2021
2 parents 5cbd6e2 + 8c0d436 commit c15232a
Show file tree
Hide file tree
Showing 8 changed files with 487 additions and 10 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Janode is a Node.js, browser compatible, adapter for the [Janus WebRTC server](https://github.com/meetecho/janus-gateway).

Internally uses WebSockets to connect to Janus.
Internally uses WebSockets or Unix DGRAM Sockets to connect to Janus.

The library wraps the Janus core API, the Janus Admin API and some of the most popular plugins APIs.

Expand Down Expand Up @@ -75,6 +75,32 @@ const data = await admin.listSessions();

```

## Switching to other transports

The kind of transport used for a connection depends on the protocol/scheme defined in the `url` field of the configuration.

```js
/* Use UNIX DGRAM Sockets */
const admin = await Janode.connect({
is_admin: true,
address: {
url: 'file://tmp/janusapi',
apisecret: 'secret'
}
});
```

```js
/* Use WebSockets */
const admin = await Janode.connect({
is_admin: true,
address: {
url: 'ws://127.0.0.1:7188/',
apisecret: 'secret'
}
});
```

## Installation

```bash
Expand Down
9 changes: 8 additions & 1 deletion examples/videoroom/html/videoroom-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,17 @@ function _listRooms() {
});
}

function _create({ room, description, max_publishers = 6, audiocodec = 'opus', videocodec = 'vp8', permanent = false }) {
function _create({ room, description, max_publishers = 6, audiocodec = 'opus', videocodec = 'vp8', talking_events = false, talking_level_threshold = 25, talking_packets_threshold = 100, permanent = false }) {
socket.emit('create', {
data: {
room,
description,
max_publishers,
audiocodec,
videocodec,
talking_events,
talking_level_threshold,
talking_packets_threshold,
permanent,
},
_id: getId(),
Expand Down Expand Up @@ -324,6 +327,10 @@ socket.on('participants-list', ({ data }) => {
console.log('participants list', data);
});

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

socket.on('kicked', ({ data }) => {
console.log('participant kicked', data);
if (data.feed) {
Expand Down
5 changes: 5 additions & 0 deletions examples/videoroom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ async function initBackEnd() {

session.once(Janode.EVENT.SESSION_DESTROYED, () => {
Logger.info(`${LOG_NS} session ${session.id} destroyed`);
janodeSession = null;
});

const handle = await session.attach(VideoRoomPlugin);
Expand Down Expand Up @@ -188,6 +189,10 @@ function initFrontEnd() {
replyEvent(socket, 'display', evtdata);
});

pubHandle.on(VideoRoomPlugin.EVENT.VIDEOROOM_TALKING, evtdata => {
replyEvent(socket, 'talking', evtdata);
});

pubHandle.on(VideoRoomPlugin.EVENT.VIDEOROOM_KICKED, evtdata => {
const handle = clientHandles.getHandleByFeed(evtdata.feed);
clientHandles.removeHandleByFeed(evtdata.feed);
Expand Down
75 changes: 72 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "janode",
"description": "Meetecho adapter for the Janus WebRTC Server",
"version": "1.5.1",
"version": "1.5.2",
"type": "module",
"keywords": [
"janus",
Expand Down Expand Up @@ -32,6 +32,9 @@
"isomorphic-ws": "^4.0.1",
"ws": "^8.0.0"
},
"optionalDependencies": {
"unix-dgram": "^2.0.4"
},
"engines": {
"node": " >=14.13.1 || >=16.0.0"
},
Expand Down
4 changes: 4 additions & 0 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const LOG_NS = '[connection.js]';
import { getNumericID, checkUrl, newIterator } from './utils/utils.js';
import { JANODE, JANUS, isResponseData, isErrorData } from './protocol.js';
import WsTransport from './transport-ws.js';
import UnixTransport from './transport-unix.js';
import JanodeSession from './session.js';
import TransactionManager from './tmanager.js';

Expand Down Expand Up @@ -107,6 +108,9 @@ class Connection extends EventEmitter {
if (checkUrl(server_config.getAddress()[0].url, ['ws', 'wss', 'ws+unix', 'wss+unix'])) {
transport = new WsTransport(this);
}
if (checkUrl(server_config.getAddress()[0].url, ['file'])) {
transport = new UnixTransport(this);
}
if (transport) this._transport = transport;
} catch (error) {
Logger.error(`${LOG_NS} ${this.name} error while initializing transport (${error.message})`);
Expand Down
41 changes: 37 additions & 4 deletions src/plugins/videoroom-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const PLUGIN_EVENT = {
UNPUBLISHED: 'videoroom_unpublished',
LEAVING: 'videoroom_leaving',
KICKED: 'videoroom_kicked',
TALKING: 'videoroom_talking',
ALLOWED: 'videoroom_allowed',
EXISTS: 'videoroom_exists',
ROOMS_LIST: 'videoroom_list',
Expand Down Expand Up @@ -167,11 +168,12 @@ class VideoRoomHandle extends Handle {

janode_event.data.feed = message_data.id;
janode_event.data.description = message_data.description;
janode_event.data.publishers = message_data.publishers.map(({ id, display }) => {
janode_event.data.publishers = message_data.publishers.map(({ id, display, talking }) => {
const pub = {
feed: id,
display,
};
if (typeof talking !== 'undefined') pub.talking = talking;
return pub;
});
janode_event.event = PLUGIN_EVENT.PUB_JOINED;
Expand All @@ -197,12 +199,13 @@ class VideoRoomHandle extends Handle {

/* Participants list */
case 'participants':
janode_event.data.participants = message_data.participants.map(({ id, display, publisher }) => {
janode_event.data.participants = message_data.participants.map(({ id, display, publisher, talking }) => {
const peer = {
feed: id,
display,
publisher,
};
if (typeof talking !== 'undefined') peer.talking = talking;
return peer;
});
janode_event.event = PLUGIN_EVENT.PARTICIPANTS_LIST;
Expand Down Expand Up @@ -284,6 +287,15 @@ class VideoRoomHandle extends Handle {
janode_event.event = PLUGIN_EVENT.RTP_FWD_LIST;
break;

/* Talking events */
case 'talking':
case 'stopped-talking':
janode_event.data.feed = message_data.id;
janode_event.data.talking = (videoroom === 'talking');
janode_event.data.audio_level = message_data['audio-level-dBov-avg'];
janode_event.event = PLUGIN_EVENT.TALKING;
break;

/* Generic events (error, notifications ...) */
case 'event':
/* VideoRoom Error */
Expand All @@ -305,11 +317,12 @@ class VideoRoomHandle extends Handle {
/* Publisher list notification */
if (message_data.publishers) {
janode_event.event = PLUGIN_EVENT.PUB_LIST;
janode_event.data.publishers = message_data.publishers.map(({ id, display }) => {
janode_event.data.publishers = message_data.publishers.map(({ id, display, talking }) => {
const pub = {
feed: id,
display,
};
if (typeof talking !== 'undefined') pub.talking = talking;
return pub;
});
break;
Expand Down Expand Up @@ -855,14 +868,17 @@ class VideoRoomHandle extends Handle {
* @param {number} [params.fir_freq] - The PLI interval in seconds
* @param {string} [params.audiocodec] - Comma separated list of allowed audio codecs
* @param {string} [params.videocodec] - Comma separated list of allowed video codecs
* @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 {boolean} [params.record] - Wheter to enable recording of any publisher
* @param {string} [params.rec_dir] - Folder where recordings should be stored
* @param {boolean} [params.videoorient] - Whether the video-orientation RTP extension must be negotiated
* @param {string} [params.h264_profile] - H264 specific profile to prefer
* @returns {Promise<module:videoroom-plugin~VIDEOROOM_EVENT_CREATED>}
*/
async create({ room = 0, description, max_publishers, permanent, is_private, secret, pin, bitrate,
bitrate_cap, fir_freq, audiocodec, videocodec, record, rec_dir, videoorient, h264_profile }) {
bitrate_cap, fir_freq, audiocodec, videocodec, talking_events, talking_level_threshold, talking_packets_threshold, record, rec_dir, videoorient, h264_profile }) {
const body = {
request: REQUEST_CREATE,
room,
Expand All @@ -878,6 +894,9 @@ class VideoRoomHandle extends Handle {
if (typeof fir_freq === 'number') body.fir_freq = fir_freq;
if (typeof audiocodec === 'string') body.audiocodec = audiocodec;
if (typeof videocodec === 'string') body.videocodec = videocodec;
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 record === 'boolean') body.record = record;
if (typeof rec_dir === 'string') body.rec_dir = rec_dir;
if (typeof videoorient === 'boolean') body.videoorient_ext = videoorient;
Expand Down Expand Up @@ -1079,6 +1098,7 @@ class VideoRoomHandle extends Handle {
* @property {number|string} participants[].feed - Feed identifier of the participant
* @property {string} [participants[].display] - The participant display name, if available
* @property {boolean} participants[].publisher - Whether the user is an active publisher in the room
* @property {boolean} [participants[].talking] - True if participant is talking
*/

/**
Expand Down Expand Up @@ -1230,6 +1250,7 @@ class VideoRoomHandle extends Handle {
* @property {string} EVENT.VIDEOROOM_LEAVING {@link module:videoroom-plugin~VIDEOROOM_LEAVING}
* @property {string} EVENT.VIDEOROOM_DISPLAY {@link module:videoroom-plugin~VIDEOROOM_DISPLAY}
* @property {string} EVENT.VIDEOROOM_KICKED {@link module:videoroom-plugin~VIDEOROOM_KICKED}
* @property {string} EVENT.VIDEOROOM_TALKING {@link module:videoroom-plugin~VIDEOROOM_TALKING}
* @property {string} EVENT.VIDEOROOM_ERROR {@link module:videoroom-plugin~VIDEOROOM_ERROR}
*/
export default {
Expand Down Expand Up @@ -1306,6 +1327,18 @@ export default {
*/
VIDEOROOM_SLOWLINK: PLUGIN_EVENT.SLOW_LINK,

/**
* Notify if the current user is talking.
*
* @event module:videoroom-plugin~VideoRoomHandle#event:VIDEOROOM_TALKING
* @type {object}
* @property {number|string} room - The involved room
* @property {number|string} feed - The feed of the peer this talking notification refers to
* @property {boolean} talking - True if the participant is talking
* @property {number} audio_level - The audio level of the participant in the range [0,127]
*/
VIDEOROOM_TALKING: PLUGIN_EVENT.TALKING,

/**
* A feed has been kicked out.
*
Expand Down
Loading

0 comments on commit c15232a

Please sign in to comment.