Skip to content

Commit

Permalink
Move rtcpSenderReportEnabled option to Streamer, make RTCP interval…
Browse files Browse the repository at this point in the history
… time-based
  • Loading branch information
longnguyen2004 committed Feb 4, 2025
1 parent 82e8051 commit 87e4468
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 35 deletions.
5 changes: 5 additions & 0 deletions src/client/Streamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export type StreamerOptions = {
* Force the use of ChaCha20 encryption. Faster on CPUs without AES-NI
*/
forceChacha20Encryption: boolean;
/**
* Enable RTCP Sender Report for synchronization
*/
rtcpSenderReportEnabled: boolean
}

export class Streamer {
Expand All @@ -28,6 +32,7 @@ export class Streamer {
this._client = client;
this._opts = {
forceChacha20Encryption: false,
rtcpSenderReportEnabled: true,
...opts
};

Expand Down
54 changes: 30 additions & 24 deletions src/client/packet/BaseMediaPacketizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ export class BaseMediaPacketizer {

private _totalBytes: number;
private _totalPackets: number;
private _prevTotalPackets: number;
private _lastPacketTime: number;
private _lastRtcpTime: number;
private _currentMediaTimestamp: number;
private _srInterval: number;

private _mediaUdp: MediaUdp;
Expand All @@ -36,12 +37,13 @@ export class BaseMediaPacketizer {
this._timestamp = 0;
this._totalBytes = 0;
this._totalPackets = 0;
this._prevTotalPackets = 0;
this._lastPacketTime = 0;
this._lastRtcpTime = 0;
this._currentMediaTimestamp = 0;
this._mtu = 1200;
this._extensionEnabled = extensionEnabled;

this._srInterval = 512; // Sane fallback value for interval
this._srInterval = 1000;
}

public get ssrc(): number | undefined
Expand All @@ -52,12 +54,11 @@ export class BaseMediaPacketizer {
public set ssrc(value: number)
{
this._ssrc = value;
this._totalBytes = this._totalPackets = this._prevTotalPackets = 0;
this._totalBytes = this._totalPackets = 0;
}

/**
* The interval (number of packets) between 2 consecutive RTCP Sender
* Report packets
* The interval between 2 consecutive RTCP Sender Report packets in ms
*/
public get srInterval(): number
{
Expand All @@ -75,27 +76,32 @@ export class BaseMediaPacketizer {
}

public async onFrameSent(packetsSent: number, bytesSent: number, frametime: number): Promise<void> {
if(!this._mediaUdp.mediaConnection.streamOptions.rtcpSenderReportEnabled) return;

this._totalPackets = this._totalPackets + packetsSent;
this._totalBytes = (this._totalBytes + bytesSent) % max_int32bit;

// Not using modulo here, since the number of packet sent might not be
// exactly a multiple of the interval
if (Math.floor(this._totalPackets / this._srInterval) - Math.floor(this._prevTotalPackets / this._srInterval) > 0)
if (this._mediaUdp.mediaConnection.streamer.opts.rtcpSenderReportEnabled)
{
const senderReport = await this.makeRtcpSenderReport();
this._mediaUdp.sendPacket(senderReport);
this._prevTotalPackets = this._totalPackets;
this._loggerRtcpSr.debug({
stats: {
ssrc: this._ssrc,
timestamp: this._timestamp,
totalPackets: this._totalPackets,
totalBytes: this._totalBytes
}
}, `Sent RTCP sender report for SSRC ${this._ssrc}`);
this._totalPackets = this._totalPackets + packetsSent;
this._totalBytes = (this._totalBytes + bytesSent) % max_int32bit;

/**
* Not using modulo here, since the timestamp might not be an exact
* multiple of the interval
*/
if (Math.floor(this._currentMediaTimestamp / this._srInterval) - Math.floor(this._lastRtcpTime / this._srInterval) > 0)
{
const senderReport = await this.makeRtcpSenderReport();
this._mediaUdp.sendPacket(senderReport);
this._lastRtcpTime = this._currentMediaTimestamp;
this._loggerRtcpSr.debug({
stats: {
ssrc: this._ssrc,
timestamp: this._timestamp,
totalPackets: this._totalPackets,
totalBytes: this._totalBytes
}
}, `Sent RTCP sender report for SSRC ${this._ssrc}`);
}
}
this._currentMediaTimestamp += frametime;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/client/packet/VideoPacketizerAnnexB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class VideoPacketizerAnnexB extends BaseMediaPacketizer {

constructor(connection: MediaUdp, ssrc: number, payloadType: number, nalFunctions: AnnexBHelpers) {
super(connection, ssrc, payloadType, true);
this.srInterval = 5 * connection.mediaConnection.streamOptions.fps * 3; // ~5 seconds, assuming ~3 packets per frame
this._nalFunctions = nalFunctions;
}

Expand Down
1 change: 0 additions & 1 deletion src/client/packet/VideoPacketizerVP8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export class VideoPacketizerVP8 extends BaseMediaPacketizer {
constructor(connection: MediaUdp, ssrc: number) {
super(connection, ssrc, CodecPayloadType.VP8.payload_type, true);
this._pictureId = 0;
this.srInterval = 5 * connection.mediaConnection.streamOptions.fps * 3; // ~5 seconds, assuming ~3 packets per frame
}

private incrementPictureId(): void {
Expand Down
4 changes: 4 additions & 0 deletions src/client/voice/BaseMediaConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ export abstract class BaseMediaConnection extends EventEmitter {
return this._transportEncryptor;
}

public get streamer() {
return this._streamer;
}

stop(): void {
this.interval && clearInterval(this.interval);
this.status.started = false;
Expand Down
9 changes: 0 additions & 9 deletions src/media/newApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,6 @@ export type PlayStreamOptions = {
* See https://ffmpeg.org/ffmpeg.html#:~:text=%2Dreadrate_initial_burst
*/
readrateInitialBurst: number | undefined,

/**
* Enable RTCP Sender Report for synchronization
*/
rtcpSenderReportEnabled: boolean
}

export async function playStream(
Expand All @@ -344,7 +339,6 @@ export async function playStream(
height: video.height,
frameRate: video.framerate_num / video.framerate_den,
readrateInitialBurst: undefined,
rtcpSenderReportEnabled: true,
} satisfies PlayStreamOptions;

function mergeOptions(opts: Partial<PlayStreamOptions>)
Expand Down Expand Up @@ -373,9 +367,6 @@ export async function playStream(
isFiniteNonZero(opts.readrateInitialBurst) && opts.readrateInitialBurst > 0
? opts.readrateInitialBurst
: defaultOptions.readrateInitialBurst,

rtcpSenderReportEnabled:
opts.rtcpSenderReportEnabled ?? defaultOptions.rtcpSenderReportEnabled
} satisfies PlayStreamOptions
}

Expand Down

0 comments on commit 87e4468

Please sign in to comment.