videojs / mux.js

Lightweight utilities for inspecting and manipulating video container formats.
Apache License 2.0
1.11k stars 210 forks source link

Avoid unwrapping crash when pmt parsing has returned undefined #433

Open gurushida opened 1 year ago

gurushida commented 1 year ago

When trying to transmux or inspect the attached ts segment, mux.js crashes because the pmt parsing returns an undefined value that the caller tries to use as a non empty object. This PR adds fallbacks to use empty objects when that happens.

Here is a typescript file that reproduces the problem:

import { mp2t } from 'mux.js';
import fs from 'fs';

main(process.argv.slice(2)).catch(err => console.error(err));

async function main(args: string[]) {
  for (const file of args) {
    const buffer = fs.readFileSync(file);
    const tsInfo = mp2t.tools.inspect(buffer);
    console.log(`File ${file}: ${JSON.stringify(tsInfo, null,  2)}`);
  }
}

Without the fix, the output is as follows:

$ muxProbe ~/Desktop/pmt.ts
TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at parsePsi_2 (..../muxProbe.js:7423:20)
    at inspectTs_2 (..../muxProbe.js:7705:5)
    at Object.inspect2 [as inspect] (..../muxProbe.js:7737:16)
    at main (..../muxProbe.js:7774:42)
    at Object.<anonymous> (..../muxProbe.js:7770:1)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)

With the fix, it works as expected:

$ muxProbe pmt.ts
File pmt.ts: {
  "video": [
    {
      "pts": 183600,
      "dts": 180000,
      "type": "video",
      "dtsTime": 2,
      "ptsTime": 2.04
    },
    {
      "pts": 500400,
      "dts": 500400,
      "type": "video",
      "dtsTime": 5.56,
      "ptsTime": 5.56
    }
  ],
  "firstKeyFrame": {
    "pts": 183600,
    "dts": 180000,
    "type": "video",
    "dtsTime": 2,
    "ptsTime": 2.04
  },
  "audio": [
    {
      "pts": 180600,
      "dts": 180600,
      "type": "audio",
      "dtsTime": 2.006666666666667,
      "ptsTime": 2.006666666666667
    },
    {
      "pts": 507000,
      "dts": 507000,
      "type": "audio",
      "dtsTime": 5.633333333333334,
      "ptsTime": 5.633333333333334
    }
  ]
}

pmt.ts.zip