Skip to content

Commit

Permalink
add / fix JSDoc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mildsunrise committed Oct 16, 2023
1 parent 19c479c commit 581d616
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 56 deletions.
18 changes: 15 additions & 3 deletions lib/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ const Client = require("./Client.js");
const Emitter = require("medooze-event-emitter");
const SharedPointer = require("./SharedPointer.js")

/**
* @typedef {Object} ApplicationEvents
* @property {(self: Application) => void} stopped
* @property {(client: Client) => void} connect
*/

/** @extends {Emitter<ApplicationEvents>} */
class Application extends Emitter
{
constructor()
Expand All @@ -14,16 +21,21 @@ class Application extends Emitter
this.application = new Native.RTMPApplicationImpl(this);

//Client map
this.clients = new Map();
this.clients = /** @type {Map<string, Client>} */ (new Map());

//The event handler
this.onconnect = (peerIp,peerPort,appName,conn) =>{
this.onconnect = (
/** @type {string} */ peerIp,
/** @type {number} */ peerPort,
/** @type {string} */ appName,
/** @type {Native.RTMPNetConnectionImplShared} */ conn,
) =>{
//Create new connection
const client = new Client(SharedPointer(conn),peerIp,peerPort,appName);
//Add to client list
this.clients.set(client.getId(),client);
//event handlers
client.once("stopped",(client)=>{
client.once("stopped",()=>{
//Remove client from map
this.clients.delete(client.getId());
});
Expand Down
26 changes: 21 additions & 5 deletions lib/Client.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
const uuidV4 = require("uuid/v4");
const { v4: uuidV4 } = require("uuid");
const Emitter = require("medooze-event-emitter");
const Stream = require("./Stream");
const SharedPointer = require("./SharedPointer.js")
const SharedPointer = require("./SharedPointer.js");
const Native = require("./Native");


/**
* @typedef {Object} ClientEvents
* @property {(self: Client) => void} stopped
* @property {(stream: Stream) => void} stream
*/

/** @extends {Emitter<ClientEvents>} */
class Client extends Emitter
{
constructor(connection,peerIp,peerPort,appName)
constructor(
/** @type {Native.RTMPNetConnectionImpl} */ connection,
/** @type {string} */ peerIp,
/** @type {number} */ peerPort,
/** @type {string} */ appName)
{
//Init emitter
super();
Expand All @@ -19,12 +32,12 @@ class Client extends Emitter
//Store native connection
this.connection = connection;
//Map of streams
this.streams = new Map();
this.streams = /** @type {Map<number, Stream>} */ (new Map());
//Ids
this.maxId = 0;

//Events
this.onstream = (netstream)=>{
this.onstream = (/** @type {Native.RTMPNetStreamImplShared} */ netstream)=>{
//Create stream wrapper
const stream = new Stream(SharedPointer(netstream));
//New id
Expand All @@ -41,6 +54,7 @@ class Client extends Emitter
};
this.ondisconnect = ()=>{
//NO connection anymore
//@ts-expect-error
this.connection = false;
//Stop us
this.stop();
Expand Down Expand Up @@ -96,7 +110,9 @@ class Client extends Emitter
//Stop emitter
super.stop();
//Release mem
//@ts-expect-error
this.connection = null;
//@ts-expect-error
this.streams = null;
}
}
Expand Down
56 changes: 28 additions & 28 deletions lib/IncomingStreamBridge.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
const uuidV4 = require("uuid/v4");
const { v4: uuidV4 } = require("uuid");
const Native = require("./Native");
const Emitter = require("medooze-event-emitter");
const SharedPointer = require("./SharedPointer");
const IncomingStreamTrackBridge = require("./IncomingStreamTrackBridge");

//@ts-expect-error
const parseInt = /** @type {(x: number) => number} */ (global.parseInt);

/** @typedef {IncomingStreamTrackBridge.TrackStats} TrackStats */
/** @typedef {{ [trackId: string]: TrackStats }} StreamStats */

/**
* @typedef {Object} IncomingStreamBridgeEvents
* @property {(self: IncomingStreamBridge) => void} stopped
* @property {(config: string) => void} aacconfig aac specific config received
*/

/**
* The incoming streams represent the recived media stream from a remote peer.
* @extends {Emitter<IncomingStreamBridgeEvents>}
*/
class IncomingStreamBridge extends Emitter
{
/**
* @ignore
* @hideconstructor
* private constructor
*/
constructor(maxLateOffset, maxBufferingTime)
constructor(
/** @type {number} */ maxLateOffset,
/** @type {number} */ maxBufferingTime)
{
//Init emitter
super();
Expand All @@ -26,27 +35,19 @@ class IncomingStreamBridge extends Emitter
this.bridge = new Native.IncomingStreamBridge(this, parseInt(maxLateOffset), parseInt(maxBufferingTime));

//Store sources
this.tracks = new Map();
this.tracks = /** @type {Map<String, IncomingStreamTrackBridge>} */ (new Map());

//Create audio and video tracks
this.tracks.set("audio",new IncomingStreamTrackBridge("audio","audio",SharedPointer(this.bridge.GetAudio())));
this.tracks.set("video",new IncomingStreamTrackBridge("video","video",SharedPointer(this.bridge.GetVideo())));

//Listen for aac config
this.onaacconfig = (config)=>{
/**
* IncomingStreamBridge aac specific config received
*
* @name aacconfig
* @memberof IncomingStreamBridge
* @kind event
* @argument {String} config
*/
this.onaacconfig = (/** @type {string} */ config)=>{
this.emit("aacconfig", config);
};

//Event listeners
this.onstreamstopped = (stream)=>{
this.onstreamstopped = (/** @type {import("./Stream")} */ stream)=>{
//If it is the same as ours
if (this.stream===stream)
//Dettach
Expand All @@ -67,12 +68,10 @@ class IncomingStreamBridge extends Emitter
* Get statistics for all tracks in the stream
*
* See OutgoingStreamTrack.getStats for information about the stats returned by each track.
*
* @returns {Map<String>,Object} Map with stats by trackId
*/
getStats()
{
const stats = {};
const stats = /** @type {StreamStats} */ ({});

//for each track
for (let track of this.tracks.values())
Expand All @@ -85,7 +84,7 @@ class IncomingStreamBridge extends Emitter
/**
* Get track by id
* @param {String} trackId - The track id
* @returns {IncomingStreamTrack} - requested track or null
* @returns {IncomingStreamTrackBridge | undefined}
*/
getTrack(trackId)
{
Expand All @@ -95,7 +94,7 @@ class IncomingStreamBridge extends Emitter

/**
* Get all the tracks
* @returns {Array<IncomingStreamTrack>} - Array of tracks
* @returns {Array<IncomingStreamTrackBridge>} - Array of tracks
*/
getTracks()
{
Expand All @@ -104,11 +103,11 @@ class IncomingStreamBridge extends Emitter
}
/**
* Get an array of the media stream audio tracks
* @returns {Array<IncomingStreamTrack>} - Array of tracks
* @returns {Array<IncomingStreamTrackBridge>} - Array of tracks
*/
getAudioTracks()
{
var audio = [];
var audio = /** @type {IncomingStreamTrackBridge[]} */ ([]);

//For each track
for (let track of this.tracks.values())
Expand All @@ -122,11 +121,11 @@ class IncomingStreamBridge extends Emitter

/**
* Get an array of the media stream video tracks
* @returns {Array<IncomingStreamTrack>} - Array of tracks
* @returns {Array<IncomingStreamTrackBridge>} - Array of tracks
*/
getVideoTracks()
{
var video = [];
var video = /** @type {IncomingStreamTrackBridge[]} */ ([]);

//For each track
for (let track of this.tracks.values())
Expand All @@ -152,7 +151,7 @@ class IncomingStreamBridge extends Emitter
this.stream = null;
}

attachTo(stream)
attachTo(/** @type {import("./Stream")} */ stream)
{
//Dettach just in case
this.dettach();
Expand Down Expand Up @@ -207,6 +206,7 @@ class IncomingStreamBridge extends Emitter
super.stop();

//Remove brdige reference, so destructor is called on GC
//@ts-expect-error
this.bridge = null;
}
}
Expand Down
11 changes: 8 additions & 3 deletions lib/RTMPServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ const Server = require("./Server.js");
const Application = require("./Application.js");
const Status = require("./Status.js");
const IncomingStreamBridge = require("./IncomingStreamBridge");

/** @typedef {import("./Status").Status} Status */

/** @namespace */
const RTMPServer = Status;
const RTMPServer = {};
RTMPServer.NetStream = Status.NetStream;
RTMPServer.NetConnection = Status.NetConnection;

//INitialize Stuff
Native.RTMPServerModule.Initialize();
Expand Down Expand Up @@ -49,7 +54,7 @@ RTMPServer.enableDebug = function(flag)
* @memberof RTMPServer
* @param {Boolean} flag
*/
RTMPServer.enableUltraDebug = function(flag)
RTMPServer.enableUltraDebug = function(/** @type {boolean} */ flag)
{
//Set flag
Native.RTMPServerModule.EnableUltraDebug(flag);
Expand Down Expand Up @@ -82,7 +87,7 @@ RTMPServer.createIncomingStreamBridge = function()
/**
* Create a new application
* @memberof RTMPServer
* @returns {Applicaton} The new created application
* @returns {Application} The new created application
*/
RTMPServer.createApplication = function()
{
Expand Down
9 changes: 7 additions & 2 deletions lib/Server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const Native = require("./Native.js");

/** @typedef {import("./Application")} Application */

class Server
{
constructor()
Expand All @@ -8,13 +10,15 @@ class Server
this.server = new Native.RTMPServerFacade(this);
}

start(port)
start(/** @type {number} */ port)
{
//Create native server
this.server.Start(port);
}

addApplication(name,app)
addApplication(
/** @type {string} */ name,
/** @type {Application} */ app)
{
//Add to server
this.server.AddApplication(name,app.application);
Expand All @@ -26,6 +30,7 @@ class Server
this.server.Stop();

//Release mem
//@ts-expect-error
this.server = null;
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/Status.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Status
{
constructor(code,level)
constructor(
/** @type {string} */ code,
/** @type {string} */ level)
{
this.code = code;
this.level = level;
Expand All @@ -10,6 +12,7 @@ class Status

module.exports =
{
Status,
NetConnection:
{
Call:
Expand Down
Loading

0 comments on commit 581d616

Please sign in to comment.