Home Reference Source

src/demux/mp3demuxer.ts

  1. /**
  2. * MP3 demuxer
  3. */
  4. import BaseAudioDemuxer from './base-audio-demuxer';
  5. import * as ID3 from '../demux/id3';
  6. import { logger } from '../utils/logger';
  7. import * as MpegAudio from './mpegaudio';
  8.  
  9. class MP3Demuxer extends BaseAudioDemuxer {
  10. static readonly minProbeByteLength: number = 4;
  11.  
  12. resetInitSegment(audioCodec, videoCodec, duration) {
  13. super.resetInitSegment(audioCodec, videoCodec, duration);
  14. this._audioTrack = {
  15. container: 'audio/mpeg',
  16. type: 'audio',
  17. id: 0,
  18. pid: -1,
  19. sequenceNumber: 0,
  20. isAAC: false,
  21. samples: [],
  22. manifestCodec: audioCodec,
  23. duration: duration,
  24. inputTimeScale: 90000,
  25. dropped: 0,
  26. };
  27. }
  28.  
  29. static probe(data): boolean {
  30. if (!data) {
  31. return false;
  32. }
  33.  
  34. // check if data contains ID3 timestamp and MPEG sync word
  35. // Look for MPEG header | 1111 1111 | 111X XYZX | where X can be either 0 or 1 and Y or Z should be 1
  36. // Layer bits (position 14 and 15) in header should be always different from 0 (Layer I or Layer II or Layer III)
  37. // More info http://www.mp3-tech.org/programmer/frame_header.html
  38. const id3Data = ID3.getID3Data(data, 0) || [];
  39. let offset = id3Data.length;
  40.  
  41. for (let length = data.length; offset < length; offset++) {
  42. if (MpegAudio.probe(data, offset)) {
  43. logger.log('MPEG Audio sync word found !');
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49.  
  50. canParse(data, offset) {
  51. return MpegAudio.canParse(data, offset);
  52. }
  53.  
  54. appendFrame(track, data, offset) {
  55. if (this.initPTS === null) {
  56. return;
  57. }
  58. return MpegAudio.appendFrame(
  59. track,
  60. data,
  61. offset,
  62. this.initPTS,
  63. this.frameIndex
  64. );
  65. }
  66. }
  67.  
  68. export default MP3Demuxer;