Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
balazskreith committed May 1, 2024
1 parent d7cb9f5 commit 46f89d7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@observertc/observer-js",
"version": "0.40.31",
"version": "0.40.32",
"description": "Server Side NodeJS Library for processing ObserveRTC Samples",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
37 changes: 27 additions & 10 deletions src/ObservedOutboundTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter
public deltaSentFrames = 0;
public deltaEncodedFrames = 0;

public sendingBitrate = 0;

private readonly _stats = new Map<number, ObservedOutboundTrackStats<Kind>>();
private _lastMaxStatsTimestamp = 0;

Expand All @@ -79,6 +81,8 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter
) {
super();
this.setMaxListeners(Infinity);

if (this._model.sfuStreamId) this._assignSfuStreamId(this._model.sfuStreamId);
}

public get serviceId() {
Expand Down Expand Up @@ -241,16 +245,7 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter

// setting up sfu connection as it is not always available at the first sample
if (sample.sfuStreamId && !this._model.sfuStreamId) {
this._model.sfuStreamId = sample.sfuStreamId;

this.once('close', () => {
if (!this._model.sfuStreamId) return;
if (this.kind === 'audio') this.peerConnection.client.call.sfuStreamIdToOutboundAudioTrack.delete(this._model.sfuStreamId);
else if (this.kind === 'video') this.peerConnection.client.call.sfuStreamIdToOutboundVideoTrack.delete(this._model.sfuStreamId);
});

if (this.kind === 'audio') this.peerConnection.client.call.sfuStreamIdToOutboundAudioTrack.set(this._model.sfuStreamId, this as ObservedOutboundTrack<'audio'>);
else if (this.kind === 'video') this.peerConnection.client.call.sfuStreamIdToOutboundVideoTrack.set(this._model.sfuStreamId, this as ObservedOutboundTrack<'video'>);
this._assignSfuStreamId(sample.sfuStreamId);
}

this.visited = true;
Expand All @@ -270,6 +265,7 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter

this._remoteInboundTracks.set(track.trackId, track);
}
private _lastUpdateMetrics?: number;

public updateMetrics() {
let maxStatsTimestamp = 0;
Expand All @@ -279,6 +275,7 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter
this.bitrate = 0;
this.rttInMs = undefined;

this.sendingBitrate = 0;
this.deltaLostPackets = 0;
this.deltaSentPackets = 0;
this.deltaSentBytes = 0;
Expand All @@ -300,6 +297,13 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter
rttInMsSum += stats.rttInMs ?? 0;
++size;
}

const now = Date.now();

if (this._lastUpdateMetrics) {
this.sendingBitrate = (this.deltaSentBytes * 8) / ((now - this._lastUpdateMetrics) / 1000);
}
this._lastUpdateMetrics = now;

this.totalLostPackets += this.deltaLostPackets;
this.totalSentPackets += this.deltaSentPackets;
Expand All @@ -310,4 +314,17 @@ export class ObservedOutboundTrack<Kind extends MediaKind> extends EventEmitter

this._lastMaxStatsTimestamp = maxStatsTimestamp;
}

private _assignSfuStreamId(sfuStreamId: string) {
this._model.sfuStreamId = sfuStreamId;

this.once('close', () => {
if (!this._model.sfuStreamId) return;
if (this.kind === 'audio') this.peerConnection.client.call.sfuStreamIdToOutboundAudioTrack.delete(this._model.sfuStreamId);
else if (this.kind === 'video') this.peerConnection.client.call.sfuStreamIdToOutboundVideoTrack.delete(this._model.sfuStreamId);
});

if (this.kind === 'audio') this.peerConnection.client.call.sfuStreamIdToOutboundAudioTrack.set(this._model.sfuStreamId, this as ObservedOutboundTrack<'audio'>);
else if (this.kind === 'video') this.peerConnection.client.call.sfuStreamIdToOutboundVideoTrack.set(this._model.sfuStreamId, this as ObservedOutboundTrack<'video'>);
}
}

0 comments on commit 46f89d7

Please sign in to comment.