gpt4 book ai didi

javascript - 无法从其对象实例访问类函数

转载 作者:行者123 更新时间:2023-12-04 23:20:44 25 4
gpt4 key购买 nike

我无法从其对象实例访问类函数-对象正在正确初始化并且构造函数也运行良好-只是无法从该类显式调用函数
我的 FFmpeg 类的代码:

module.exports = class FFmpeg {
constructor(rtpParameters) {
this._rtpParameters = rtpParameters;
this._process = undefined;
this._observer = new EventEmitter();
this._createProcess();
}

_createProcess() {
const sdpString = createSdpText(this._rtpParameters);
const sdpStream = convertStringToStream(sdpString);

console.log("createProcess() [sdpString:%s]", sdpString);

this._process = child_process.spawn("ffmpeg", this._commandArgs);

if (this._process.stderr) {
this._process.stderr.setEncoding("utf-8");

this._process.stderr.on("data", (data) =>
console.log("ffmpeg::process::data [data:%o]", data)
);
}

if (this._process.stdout) {
this._process.stdout.setEncoding("utf-8");

this._process.stdout.on("data", (data) =>
console.log("ffmpeg::process::data [data:%o]", data)
);
}

this._process.on("message", (message) =>
console.log("ffmpeg::process::message [message:%o]", message)
);

this._process.on("error", (error) =>
console.error("ffmpeg::process::error [error:%o]", error)
);

this._process.once("close", () => {
console.log("ffmpeg::process::close");
this._observer.emit("process-close");
});

sdpStream.on("error", (error) =>
console.error("sdpStream::error [error:%o]", error)
);

// Pipe sdp stream to the ffmpeg process
sdpStream.resume();
sdpStream.pipe(this._process.stdin);
}

kill() {
console.log("kill() [pid:%d]", this._process.pid);
this._process.kill("SIGINT");
}

get _commandArgs() {
let commandArgs = [
"-loglevel",
"debug",
"-protocol_whitelist",
"pipe,udp,rtp",
"-fflags",
"+genpts",
"-f",
"sdp",
"-i",
"pipe:0",
];

commandArgs = commandArgs.concat(this._videoArgs);
commandArgs = commandArgs.concat(this._audioArgs);

commandArgs = commandArgs.concat([
"-flags",
"+global_header",
`${RECORD_FILE_LOCATION_PATH}/${this._rtpParameters.fileName}.webm`,
]);

console.log("commandArgs:%o", commandArgs);

return commandArgs;
}

get _videoArgs() {
return ["-map", "0:v:0", "-c:v", "copy"];
}

get _audioArgs() {
return [
"-map",
"0:a:0",
"-strict", // libvorbis is experimental
"-2",
"-c:a",
"copy",
];
}
};
我创建此类实例的实现:
async startRecord() {
if (this._isRecording) return;

this._isRecording = true;
let recordInfo = {};
const joinedPeers = this._getJoinedPeers();
for (const peer of joinedPeers) {
for (const producer of peer.data.producers.values()) {
recordInfo[producer.kind] = await this.publishProducerRtpStream(
peer,
producer
);
recordInfo.fileName = Date.now().toString();

peer.data.process = this.getProcess(recordInfo);
setTimeout(async () => {
console.log(peer.data.process);
// I want to call peer.data.process.kill() HERE!
}, 4000);
}
}
}
async getProcess(recordInfo) {
return new FFmpeg(recordInfo);
}
我想在 FFmpeg 中调用 kill() 函数,它显示未定义。真不明白怎么办。
这是对象实例的日志(在任何地方都没有提到“kill”函数,因此可能表明 .kill() 未定义)
console logging the object created from the class FFmpeg (new FFmpeg)
非常感谢任何帮助。
谢谢!

最佳答案

解决了:
等待将对象创建为变量,并调用该变量上的函数:我得到了 Promsie<>

关于javascript - 无法从其对象实例访问类函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66758764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com