【问题标题】:unable to access a class function from its object instance无法从其对象实例访问类函数
【发布时间】:2021-03-23 07:18:27
【问题描述】:

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

我的 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() 未定义)

非常感谢任何帮助。 谢谢!

【问题讨论】:

    标签: javascript node.js oop ffmpeg


    【解决方案1】:

    已解决:

    等待将对象创建为变量,然后调用该变量上的函数:我得到了一个 Promsie

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 2015-10-12
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      相关资源
      最近更新 更多