Skip to content

Commit

Permalink
flv parser replaces the event with a callback function
Browse files Browse the repository at this point in the history
  • Loading branch information
illuspas committed Nov 29, 2024
1 parent f7fb379 commit 87ace56
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
21 changes: 14 additions & 7 deletions src/protocol/flv.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ const PacketTypeMPEG2TSSequenceStart = 5;

/**
* @class
* @augments EventEmitter
*/
export default class Flv extends EventEmitter {
export default class Flv {
constructor() {
super();
this.parserBuffer = Buffer.alloc(13);
this.parserState = FLV_PARSE_INIT;
this.parserHeaderBytes = 0;
Expand All @@ -48,8 +46,17 @@ export default class Flv extends EventEmitter {
this.parserPreviousBytes = 0;
}

/**
* @abstract
* @param {AVPacket} avpacket
*/
onPacketCallback = (avpacket) => {

};

/**
* @param {Buffer} buffer
* @returns {string | null} error
*/
parserData = (buffer) => {
let s = buffer.length;
Expand Down Expand Up @@ -110,22 +117,23 @@ export default class Flv extends EventEmitter {
this.parserPreviousBytes = 0;
const parserPreviousNSize = this.parserBuffer.readUint32BE();
if (parserPreviousNSize === this.parserTagSize + 11) {
this.emit("flvtag", this.parserTagType, this.parserTagSize, this.parserTagTime, this.parserTagData);
let packet = Flv.parserTag(this.parserTagType, this.parserTagTime, this.parserTagSize, this.parserTagData);
this.onPacketCallback(packet);
} else {
logger.error("flv tag parser error");
return "flv tag parser error";
}
}
break;
}
}
return null;
};

/**
* @param {number} size
*/
parserTagAlloc = (size) => {
if (this.parserTagCapacity < size) {
logger.debug("parserTagAlloc " + size);
this.parserTagCapacity = size * 2;
const newBuffer = Buffer.alloc(this.parserTagCapacity);
this.parserTagData.copy(newBuffer);
Expand Down Expand Up @@ -170,7 +178,6 @@ export default class Flv extends EventEmitter {
return buffer;
};


/**
* @param {number} type
* @param {number} time
Expand Down
30 changes: 14 additions & 16 deletions src/session/flv_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Flv from "../protocol/flv.js";
import AMF from "../protocol/amf.js";
import logger from "../core/logger.js";
import Context from "../core/context.js";
import AVPacket from "../core/avpacket.js";

/**
* @class
Expand Down Expand Up @@ -54,10 +55,10 @@ export default class FlvSession extends BaseSession {
doPush = () => {
logger.info(`FLV session ${this.id} ${this.ip} start push ${this.streamPath}`);
this.isPublisher = true;
this.flv.on("flvtag", this.onFlvTag);
this.flv.onPacketCallback = this.onPacket;
const err = this.broadcast.postPush(this);
if (err != null) {
logger.error(`FLV session ${this.id} ${this.ip} error push ${this.streamPath}, ${err}`);
logger.error(`FLV session ${this.id} ${this.ip} push ${this.streamPath} error, ${err}`);
this.res.end();
}
};
Expand All @@ -66,7 +67,11 @@ export default class FlvSession extends BaseSession {
* @param {Buffer} data
*/
onData = (data) => {
this.flv.parserData(data);
let err = this.flv.parserData(data);
if (err != null) {
logger.error(`FLV session ${this.id} ${this.ip} parserData error, ${err}`);
this.res.end();
}
};

onClose = () => {
Expand All @@ -83,19 +88,16 @@ export default class FlvSession extends BaseSession {
* @param {string} err
*/
onError = (err) => {
logger.info(`FLV session ${this.id} ${this.ip} error, ${err}`);
logger.error(`FLV session ${this.id} ${this.ip} socket error, ${err}`);
};

/**
* @param {number} type
* @param {number} size
* @param {number} time
* @param {Buffer} data
* @param {AVPacket} packet
*/
onFlvTag = (type, size, time, data) => {
if (type === 18) {
const metadata = AMF.parseScriptData(data.buffer, 0, size);
if (metadata != null) {
onPacket = (packet) => {
if (packet.codec_type === 18) {
const metadata = AMF.parseScriptData(packet.data.buffer, 0, packet.size);
if (metadata !== null) {
this.videoCodec = metadata.videocodecid;
this.videoWidth = metadata.width;
this.videoHeight = metadata.height;
Expand All @@ -107,10 +109,6 @@ export default class FlvSession extends BaseSession {
this.audioDatarate = metadata.audiodatarate;
}
}
let packet = Flv.parserTag(type, time, size, data);
// if (packet.codec_type == 9) {
// logger.debug(`flv parser get packet time:${time} type:${packet.codec_type} pts:${packet.pts} dts:${packet.dts} size:${packet.size}`);
// }
this.broadcast.broadcastMessage(packet);
};

Expand Down

0 comments on commit 87ace56

Please sign in to comment.