Skip to content

Commit

Permalink
Reset init segment when M2TS video configuration changes (#5794)
Browse files Browse the repository at this point in the history
Resolves #2062 (and stale duplicates #1842 #967)
  • Loading branch information
robwalch committed Sep 5, 2023
1 parent 5ffa377 commit d0d5204
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/controller/abr-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,9 @@ class AbrController implements AbrComponentAPI {
lastLoadedFragLevel === -1 ? this.hls.firstLevel : lastLoadedFragLevel;
const { fragCurrent, partCurrent } = this;
const { levels, allAudioTracks, loadLevel } = this.hls;
if (levels.length === 1) {
return 0;
}
const level: Level | undefined = levels[selectionBaseLevel];
const live = !!level?.details?.live;
const firstSelection = loadLevel === -1 || lastLoadedFragLevel === -1;
Expand Down
27 changes: 16 additions & 11 deletions src/demux/video/avc-video-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import {
appendUint8Array,
parseSEIMessageFromNALu,
} from '../../utils/mp4-tools';

import type { PES } from '../tsdemuxer';

import ExpGolomb from './exp-golomb';
import { logger } from '../../utils/logger';

Check warning on line 12 in src/demux/video/avc-video-parser.ts

View workflow job for this annotation

GitHub Actions / build

'logger' is defined but never used

Check warning on line 12 in src/demux/video/avc-video-parser.ts

View workflow job for this annotation

GitHub Actions / build

'logger' is defined but never used
import type { PES } from '../tsdemuxer';

class AvcVideoParser extends BaseVideoParser {
public parseAVCPES(
Expand Down Expand Up @@ -133,17 +132,23 @@ class AvcVideoParser extends BaseVideoParser {
break;
// SPS
}
case 7:
case 7: {
push = true;
spsfound = true;
if (debug && VideoSample) {
VideoSample.debug += 'SPS ';
}
const sps = unit.data;
const expGolombDecoder = new ExpGolomb(sps);
const config = expGolombDecoder.readSPS();

if (!track.sps) {
const sps = unit.data;
const expGolombDecoder = new ExpGolomb(sps);
const config = expGolombDecoder.readSPS();
if (
!track.sps ||
track.width !== config.width ||
track.height !== config.height ||
track.pixelRatio?.[0] !== config.pixelRatio[0] ||
track.pixelRatio?.[1] !== config.pixelRatio[1]
) {
track.width = config.width;
track.height = config.height;
track.pixelRatio = config.pixelRatio;
Expand All @@ -161,17 +166,17 @@ class AvcVideoParser extends BaseVideoParser {
}
track.codec = codecstring;
}

break;
}
// PPS
case 8:
push = true;
if (debug && VideoSample) {
VideoSample.debug += 'PPS ';
}

if (!track.pps) {
track.pps = [unit.data];
}
track.pps = [unit.data];

break;
// AUD
Expand Down
29 changes: 27 additions & 2 deletions src/remux/mp4-remuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export default class MP4Remuxer implements Remuxer {
private videoSampleDuration: number | null = null;
private isAudioContiguous: boolean = false;
private isVideoContiguous: boolean = false;
private videoTrackConfig?: {
width?: number;
height?: number;
pixelRatio?: [number, number];
};

constructor(
observer: HlsEventEmitter,
Expand All @@ -73,7 +78,10 @@ export default class MP4Remuxer implements Remuxer {
}
}

destroy() {}
destroy() {
// @ts-ignore
this.config = this.videoTrackConfig = this._initPTS = this._initDTS = null;
}

resetTimeStamp(defaultTimeStamp: RationalTimestamp | null) {
logger.log('[mp4-remuxer]: initPTS & initDTS reset');
Expand All @@ -89,6 +97,7 @@ export default class MP4Remuxer implements Remuxer {
resetInitSegment() {
logger.log('[mp4-remuxer]: ISGenerated flag reset');
this.ISGenerated = false;
this.videoTrackConfig = undefined;
}

getVideoStartPts(videoSamples) {
Expand Down Expand Up @@ -147,7 +156,18 @@ export default class MP4Remuxer implements Remuxer {
flush;

if (canRemuxAvc) {
if (!this.ISGenerated) {
if (this.ISGenerated) {
const config = this.videoTrackConfig;
if (
config &&
(videoTrack.width !== config.width ||
videoTrack.height !== config.height ||
videoTrack.pixelRatio?.[0] !== config.pixelRatio?.[0] ||
videoTrack.pixelRatio?.[1] !== config.pixelRatio?.[1])
) {
this.resetInitSegment();
}
} else {
initSegment = this.generateIS(
audioTrack,
videoTrack,
Expand Down Expand Up @@ -385,6 +405,11 @@ export default class MP4Remuxer implements Remuxer {
computePTSDTS = false;
}
}
this.videoTrackConfig = {
width: videoTrack.width,
height: videoTrack.height,
pixelRatio: videoTrack.pixelRatio,
};
}

if (Object.keys(tracks).length) {
Expand Down

0 comments on commit d0d5204

Please sign in to comment.