Skip to content

Commit

Permalink
Implement gop cache
Browse files Browse the repository at this point in the history
  • Loading branch information
illuspas committed Dec 3, 2024
1 parent ead7720 commit e7be993
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ npx node-media-server
## Features
* HTTP/HTTP2-flv Push/Play
* RTMP Push/Play
* GOP cache

## Roadmap
* HTTP-API
* Authentication
* Notification
* Record and Playback
* GOP cache
* Rtmp Proxy

## Usage
Expand Down
50 changes: 47 additions & 3 deletions src/server/broadcast_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export default class BroadcastServer {

/** @type {Buffer | null} */
this.rtmpVideoHeader = null;

/**@type {Set<Buffer> | null} */
this.flvGopCache = null;

/**@type {Set<Buffer> | null} */
this.rtmpGopCache = null;
}

/**
Expand All @@ -47,15 +53,20 @@ export default class BroadcastServer {
switch (session.protocol) {
case "flv":
session.sendBuffer(this.flvHeader);
if (this.flvMetaData != null) {
if (this.flvMetaData !== null) {
session.sendBuffer(this.flvMetaData);
}
if (this.flvAudioHeader != null) {
if (this.flvAudioHeader !== null) {
session.sendBuffer(this.flvAudioHeader);
}
if (this.flvVideoHeader != null) {
if (this.flvVideoHeader !== null) {
session.sendBuffer(this.flvVideoHeader);
}
if (this.flvGopCache !== null) {
this.flvGopCache.forEach((v) => {
session.sendBuffer(v);
});
}
break;
case "rtmp":
if (this.rtmpMetaData != null) {
Expand All @@ -67,6 +78,11 @@ export default class BroadcastServer {
if (this.rtmpVideoHeader != null) {
session.sendBuffer(this.rtmpVideoHeader);
}
if (this.rtmpGopCache !== null) {
this.rtmpGopCache.forEach((v) => {
session.sendBuffer(v);
});
}
}

this.subscribers.set(session.id, session);
Expand Down Expand Up @@ -115,10 +131,38 @@ export default class BroadcastServer {
this.flvAudioHeader = Buffer.from(flvMessage);
this.rtmpAudioHeader = Buffer.from(rtmpMessage);
break;
case 1:
if (this.flvGopCache !== null) {
this.flvGopCache.add(flvMessage);
}
if (this.rtmpGopCache !== null) {
this.rtmpGopCache.add(rtmpMessage);
}
break;
case 2:
this.flvVideoHeader = Buffer.from(flvMessage);
this.rtmpVideoHeader = Buffer.from(rtmpMessage);
break;
case 3:
if (this.flvGopCache !== null) {
this.flvGopCache.clear();
}
if (this.rtmpGopCache !== null) {
this.rtmpGopCache.clear();
}
this.flvGopCache = new Set();
this.rtmpGopCache = new Set();
this.flvGopCache.add(flvMessage);
this.rtmpGopCache.add(rtmpMessage);
break;
case 4:
if (this.flvGopCache !== null) {
this.flvGopCache.add(flvMessage);
}
if (this.rtmpGopCache !== null) {
this.rtmpGopCache.add(rtmpMessage);
}
break;
case 5:
this.flvMetaData = Buffer.from(flvMessage);
this.rtmpMetaData = Buffer.from(rtmpMessage);
Expand Down

0 comments on commit e7be993

Please sign in to comment.