Skip to content

Commit c4dadbd

Browse files
committed
text: Finer-grained types transport-side
1 parent 40d24a3 commit c4dadbd

File tree

3 files changed

+32
-34
lines changed

3 files changed

+32
-34
lines changed

src/main_thread/text_displayer/html/html_text_displayer.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { onEnded, onSeeked, onSeeking } from "../../../compat/event_listeners";
33
import onHeightWidthChange from "../../../compat/on_height_width_change";
44
import config from "../../../config";
55
import log from "../../../log";
6-
import type { ITextTrackSegmentData } from "../../../transports";
6+
import type {
7+
ISupportedTextTrackFormat,
8+
ITextTrackSegmentData,
9+
} from "../../../transports";
710
import type { IRange } from "../../../utils/ranges";
811
import { convertToRanges } from "../../../utils/ranges";
912
import type { CancellationSignal } from "../../../utils/task_canceller";
@@ -409,11 +412,13 @@ export default class HTMLTextDisplayer implements ITextDisplayer {
409412
}
410413

411414
/** Data of chunks that should be pushed to the `HTMLTextDisplayer`. */
412-
export interface ITextTracksBufferSegmentData {
415+
export interface ITextTracksBufferSegmentData<
416+
TDataFormatName extends ISupportedTextTrackFormat = ISupportedTextTrackFormat,
417+
> {
413418
/** The text track data, in the format indicated in `type`. */
414-
data: string | BufferSource;
419+
data: TDataFormatName extends "mp4vtt" ? BufferSource : string;
415420
/** The format of `data` (examples: "ttml", "srt" or "vtt") */
416-
type: string;
421+
type: TDataFormatName;
417422
/**
418423
* Language in which the text track is, as a language code.
419424
* This is mostly needed for "sami" subtitles, to know which cues can / should
@@ -437,9 +442,7 @@ export interface ITextTracksBufferSegmentData {
437442
*/
438443
if ((__ENVIRONMENT__.CURRENT_ENV as number) === (__ENVIRONMENT__.DEV as number)) {
439444
// @ts-expect-error: uncalled function just for type-checking
440-
function _checkType<T extends string | BufferSource>(
441-
input: ITextTrackSegmentData<T>,
442-
): void {
445+
function _checkType(input: ITextTrackSegmentData): void {
443446
function checkEqual(_arg: ITextTracksBufferSegmentData): void {
444447
/* nothing */
445448
}

src/transports/types.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,12 @@ export type ISupportedTextTrackFormat = "ttml" | "vtt" | "mp4vtt" | "srt" | "sam
435435

436436
/** Text track segment data, once parsed. */
437437
export interface ITextTrackSegmentData<
438-
T extends string | BufferSource = string | BufferSource,
438+
TDataFormatName extends ISupportedTextTrackFormat = ISupportedTextTrackFormat,
439439
> {
440440
/** The text track data, in the format indicated in `type`. */
441-
data: T;
441+
data: TDataFormatName extends "mp4vtt" ? BufferSource : string;
442442
/** The format of `data`. */
443-
type: ISupportedTextTrackFormat;
443+
type: TDataFormatName;
444444
/**
445445
* Language in which the text track is, as a language code.
446446
* This is mostly needed for "sami" subtitles, to know which cues can / should
@@ -491,10 +491,7 @@ export interface ITransportAudioVideoSegmentPipeline {
491491

492492
export interface ITransportTextSegmentPipeline {
493493
loadSegment: ISegmentLoader<ILoadedTextSegmentFormat>;
494-
parseSegment: ISegmentParser<
495-
ILoadedTextSegmentFormat,
496-
ITextTrackSegmentData<Uint8Array | string> | null
497-
>;
494+
parseSegment: ISegmentParser<ILoadedTextSegmentFormat, ITextTrackSegmentData | null>;
498495
}
499496

500497
export type ITransportSegmentPipeline =

src/transports/utils/parse_text_track.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ import type { ISegment } from "../../manifest";
1919
import { getMDAT } from "../../parsers/containers/isobmff";
2020
import startsWith from "../../utils/starts_with";
2121
import { utf8ToStr } from "../../utils/string_parsing";
22-
import type {
23-
IChunkTimeInfo,
24-
ISegmentContext,
25-
ISupportedTextTrackFormat,
26-
ITextTrackSegmentData,
27-
} from "../types";
22+
import type { IChunkTimeInfo, ISegmentContext, ITextTrackSegmentData } from "../types";
2823

2924
/**
3025
* Returns the a string expliciting the format of a text track when that text
@@ -122,24 +117,27 @@ export function getISOBMFFEmbeddedTextTrackData(
122117
}
123118
}
124119

125-
let type: ISupportedTextTrackFormat = getISOBMFFTextTrackFormat(codecs);
126-
let textData: string | BufferSource;
120+
const type: "ttml" | "vtt" = getISOBMFFTextTrackFormat(codecs);
127121
const mdat = getMDAT(chunkBytes);
128122
const mdatStr = mdat !== null ? utf8ToStr(mdat) : "";
129-
if (codecs === "wvtt") {
130-
if (!startsWith(mdatStr, "WEBVTT") && !startsWith(mdatStr, "\xfe\xffWEBVTT")) {
131-
// From how I understand it, we're here in a special WebVTT format embedded
132-
// in an MP4 where the whole chunk contains information, now just the MDAT
133-
textData = chunkBytes;
134-
type = "mp4vtt";
135-
} else {
136-
textData = mdatStr;
137-
}
138-
} else {
139-
textData = mdatStr;
123+
if (
124+
codecs === "wvtt" &&
125+
!startsWith(mdatStr, "WEBVTT") &&
126+
!startsWith(mdatStr, "\xfe\xffWEBVTT")
127+
) {
128+
// From how I understand it, we're here in a special WebVTT format embedded
129+
// in an MP4 where the whole chunk contains information, now just the MDAT
130+
return {
131+
data: chunkBytes,
132+
type: "mp4vtt",
133+
language,
134+
start: startTime,
135+
end: endTime,
136+
timescale: initTimescale ?? null,
137+
};
140138
}
141139
return {
142-
data: textData,
140+
data: mdatStr,
143141
type,
144142
language,
145143
start: startTime,

0 commit comments

Comments
 (0)