fix: fix frame seq and compression = 3
This commit is contained in:
parent
9654779f82
commit
3ea9dbb2f8
@ -5,7 +5,7 @@
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
# ^https://konoplayer.com/api/static/*** resSpeed://10240
|
^https://konoplayer.com/api/static/*** resSpeed://10240
|
||||||
^https://konoplayer.com/api*** reqHeaders://{x-forwarded.json} http://127.0.0.1:5001/api$1
|
^https://konoplayer.com/api*** reqHeaders://{x-forwarded.json} http://127.0.0.1:5001/api$1
|
||||||
^https://konoplayer.com/*** reqHeaders://{x-forwarded.json} http://127.0.0.1:5000/$1 excludeFilter://^https://konoplayer.com/api weinre://test
|
^https://konoplayer.com/*** reqHeaders://{x-forwarded.json} http://127.0.0.1:5000/$1 excludeFilter://^https://konoplayer.com/api weinre://test
|
||||||
^wss://konoplayer.com/*** reqHeaders://{x-forwarded.json} ws://127.0.0.1:5000/$1 excludeFilter://^wss://konoplayer.com/api
|
^wss://konoplayer.com/*** reqHeaders://{x-forwarded.json} ws://127.0.0.1:5000/$1 excludeFilter://^wss://konoplayer.com/api
|
@ -23,3 +23,9 @@ export class ParseCodecErrors extends Error {
|
|||||||
super('failed to parse codecs');
|
super('failed to parse codecs');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class UnimplementedError extends Error {
|
||||||
|
constructor(detail: string) {
|
||||||
|
super(`unimplemented: ${detail}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -6,10 +6,16 @@ import {
|
|||||||
shareReplay,
|
shareReplay,
|
||||||
map,
|
map,
|
||||||
combineLatest,
|
combineLatest,
|
||||||
of, type Observable, delayWhen, pipe, finalize, tap, throwIfEmpty,
|
of,
|
||||||
|
type Observable,
|
||||||
|
delayWhen,
|
||||||
|
throwIfEmpty,
|
||||||
} from 'rxjs';
|
} from 'rxjs';
|
||||||
import { isTagIdPos } from '../util';
|
import { isTagIdPos } from '../util';
|
||||||
import {createRangedEbmlStream, type CreateRangedEbmlStreamOptions} from './resource';
|
import {
|
||||||
|
createRangedEbmlStream,
|
||||||
|
type CreateRangedEbmlStreamOptions,
|
||||||
|
} from './resource';
|
||||||
import { type MatroskaSegmentModel, createMatroskaSegment } from './segment';
|
import { type MatroskaSegmentModel, createMatroskaSegment } from './segment';
|
||||||
|
|
||||||
export type CreateMatroskaOptions = Omit<
|
export type CreateMatroskaOptions = Omit<
|
||||||
@ -24,7 +30,9 @@ export interface MatroskaModel {
|
|||||||
segment: MatroskaSegmentModel;
|
segment: MatroskaSegmentModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createMatroska(options: CreateMatroskaOptions): Observable<MatroskaModel> {
|
export function createMatroska(
|
||||||
|
options: CreateMatroskaOptions
|
||||||
|
): Observable<MatroskaModel> {
|
||||||
const metadataRequest$ = createRangedEbmlStream({
|
const metadataRequest$ = createRangedEbmlStream({
|
||||||
...options,
|
...options,
|
||||||
byteStart: 0,
|
byteStart: 0,
|
||||||
@ -32,21 +40,20 @@ export function createMatroska(options: CreateMatroskaOptions): Observable<Matro
|
|||||||
|
|
||||||
return metadataRequest$.pipe(
|
return metadataRequest$.pipe(
|
||||||
switchMap(({ totalSize, ebml$, response }) => {
|
switchMap(({ totalSize, ebml$, response }) => {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* while [matroska v4](https://www.matroska.org/technical/elements.html) doc tell that there is only one segment in a file
|
* while [matroska v4](https://www.matroska.org/technical/elements.html) doc tell that there is only one segment in a file
|
||||||
* some mkv generated by strange tools will emit several
|
* some mkv generated by strange tools will emit several
|
||||||
*/
|
*/
|
||||||
const segment$ = ebml$.pipe(
|
const segment$ = ebml$.pipe(
|
||||||
filter(isTagIdPos(EbmlTagIdEnum.Segment, EbmlTagPosition.Start)),
|
filter(isTagIdPos(EbmlTagIdEnum.Segment, EbmlTagPosition.Start)),
|
||||||
map((startTag) => createMatroskaSegment({
|
map((startTag) =>
|
||||||
|
createMatroskaSegment({
|
||||||
startTag,
|
startTag,
|
||||||
matroskaOptions: options,
|
matroskaOptions: options,
|
||||||
ebml$,
|
ebml$,
|
||||||
})),
|
})
|
||||||
delayWhen(
|
|
||||||
({ loadedMetadata$ }) => loadedMetadata$
|
|
||||||
),
|
),
|
||||||
|
delayWhen(({ loadedMetadata$ }) => loadedMetadata$),
|
||||||
take(1),
|
take(1),
|
||||||
shareReplay(1)
|
shareReplay(1)
|
||||||
);
|
);
|
||||||
@ -55,7 +62,7 @@ export function createMatroska(options: CreateMatroskaOptions): Observable<Matro
|
|||||||
filter(isTagIdPos(EbmlTagIdEnum.EBML, EbmlTagPosition.End)),
|
filter(isTagIdPos(EbmlTagIdEnum.EBML, EbmlTagPosition.End)),
|
||||||
take(1),
|
take(1),
|
||||||
shareReplay(1),
|
shareReplay(1),
|
||||||
throwIfEmpty(() => new Error("failed to find head tag"))
|
throwIfEmpty(() => new Error('failed to find head tag'))
|
||||||
);
|
);
|
||||||
|
|
||||||
return combineLatest({
|
return combineLatest({
|
||||||
|
@ -24,6 +24,7 @@ import {
|
|||||||
finalize,
|
finalize,
|
||||||
delayWhen,
|
delayWhen,
|
||||||
from,
|
from,
|
||||||
|
combineLatest,
|
||||||
} from 'rxjs';
|
} from 'rxjs';
|
||||||
import type { CreateMatroskaOptions } from '.';
|
import type { CreateMatroskaOptions } from '.';
|
||||||
import { type ClusterType, TrackTypeRestrictionEnum } from '../schema';
|
import { type ClusterType, TrackTypeRestrictionEnum } from '../schema';
|
||||||
@ -113,7 +114,7 @@ export function createMatroskaSegment({
|
|||||||
filter(({ canComplete }) => canComplete),
|
filter(({ canComplete }) => canComplete),
|
||||||
map(({ segment }) => segment),
|
map(({ segment }) => segment),
|
||||||
take(1),
|
take(1),
|
||||||
shareReplay(1),
|
shareReplay(1)
|
||||||
);
|
);
|
||||||
|
|
||||||
const loadedRemoteCues$ = loadedMetadata$.pipe(
|
const loadedRemoteCues$ = loadedMetadata$.pipe(
|
||||||
@ -304,22 +305,33 @@ export function createMatroskaSegment({
|
|||||||
map(({ decoder, frame$ }) => {
|
map(({ decoder, frame$ }) => {
|
||||||
const clusterSystem = segment.cluster;
|
const clusterSystem = segment.cluster;
|
||||||
const infoSystem = segment.info;
|
const infoSystem = segment.info;
|
||||||
|
const trackSystem = segment.track;
|
||||||
const timestampScale = Number(infoSystem.info.TimestampScale) / 1000;
|
const timestampScale = Number(infoSystem.info.TimestampScale) / 1000;
|
||||||
|
|
||||||
|
const frameProcessing = trackSystem.buildFrameEncodingProcessor(
|
||||||
|
track.trackEntry
|
||||||
|
);
|
||||||
|
|
||||||
const decodeSubscription = cluster$.subscribe((cluster) => {
|
const decodeSubscription = cluster$.subscribe((cluster) => {
|
||||||
for (const block of clusterSystem.enumerateBlocks(
|
for (const block of clusterSystem.enumerateBlocks(
|
||||||
cluster,
|
cluster,
|
||||||
track.trackEntry
|
track.trackEntry
|
||||||
)) {
|
)) {
|
||||||
const blockTime = (Number(cluster.Timestamp) + block.relTime) * timestampScale;
|
const blockTime =
|
||||||
|
(Number(cluster.Timestamp) + block.relTime) * timestampScale;
|
||||||
const blockDuration =
|
const blockDuration =
|
||||||
frames.length > 1 ? track.predictBlockDuration(blockTime) * timestampScale : 0;
|
frames.length > 1
|
||||||
|
? track.predictBlockDuration(blockTime) * timestampScale
|
||||||
|
: 0;
|
||||||
const perFrameDuration =
|
const perFrameDuration =
|
||||||
frames.length > 1 && blockDuration
|
frames.length > 1 && blockDuration
|
||||||
? blockDuration / block.frames.length
|
? blockDuration / block.frames.length
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
for (const frame of block.frames) {
|
for (let frame of block.frames) {
|
||||||
|
if (frameProcessing) {
|
||||||
|
frame = frameProcessing(frame);
|
||||||
|
}
|
||||||
const chunk = new EncodedVideoChunk({
|
const chunk = new EncodedVideoChunk({
|
||||||
type: block.keyframe ? 'key' : 'delta',
|
type: block.keyframe ? 'key' : 'delta',
|
||||||
data: frame,
|
data: frame,
|
||||||
@ -334,13 +346,12 @@ export function createMatroskaSegment({
|
|||||||
return {
|
return {
|
||||||
track,
|
track,
|
||||||
decoder,
|
decoder,
|
||||||
frame$: frame$
|
frame$: frame$.pipe(
|
||||||
.pipe(
|
|
||||||
finalize(() => {
|
finalize(() => {
|
||||||
decodeSubscription.unsubscribe();
|
decodeSubscription.unsubscribe();
|
||||||
})
|
})
|
||||||
)
|
),
|
||||||
}
|
};
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@ -353,14 +364,20 @@ export function createMatroskaSegment({
|
|||||||
map(({ decoder, frame$ }) => {
|
map(({ decoder, frame$ }) => {
|
||||||
const clusterSystem = segment.cluster;
|
const clusterSystem = segment.cluster;
|
||||||
const infoSystem = segment.info;
|
const infoSystem = segment.info;
|
||||||
|
const trackSystem = segment.track;
|
||||||
const timestampScale = Number(infoSystem.info.TimestampScale) / 1000;
|
const timestampScale = Number(infoSystem.info.TimestampScale) / 1000;
|
||||||
|
|
||||||
|
const frameProcessing = trackSystem.buildFrameEncodingProcessor(
|
||||||
|
track.trackEntry
|
||||||
|
);
|
||||||
|
|
||||||
const decodeSubscription = cluster$.subscribe((cluster) => {
|
const decodeSubscription = cluster$.subscribe((cluster) => {
|
||||||
for (const block of clusterSystem.enumerateBlocks(
|
for (const block of clusterSystem.enumerateBlocks(
|
||||||
cluster,
|
cluster,
|
||||||
track.trackEntry
|
track.trackEntry
|
||||||
)) {
|
)) {
|
||||||
const blockTime = (Number(cluster.Timestamp) + block.relTime) * timestampScale;
|
const blockTime =
|
||||||
|
(Number(cluster.Timestamp) + block.relTime) * timestampScale;
|
||||||
const blockDuration =
|
const blockDuration =
|
||||||
frames.length > 1 ? track.predictBlockDuration(blockTime) : 0;
|
frames.length > 1 ? track.predictBlockDuration(blockTime) : 0;
|
||||||
const perFrameDuration =
|
const perFrameDuration =
|
||||||
@ -369,7 +386,10 @@ export function createMatroskaSegment({
|
|||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
let i = 0;
|
let i = 0;
|
||||||
for (const frame of block.frames) {
|
for (let frame of block.frames) {
|
||||||
|
if (frameProcessing) {
|
||||||
|
frame = frameProcessing(frame);
|
||||||
|
}
|
||||||
const chunk = new EncodedAudioChunk({
|
const chunk = new EncodedAudioChunk({
|
||||||
type: block.keyframe ? 'key' : 'delta',
|
type: block.keyframe ? 'key' : 'delta',
|
||||||
data: frame,
|
data: frame,
|
||||||
@ -387,7 +407,8 @@ export function createMatroskaSegment({
|
|||||||
decoder,
|
decoder,
|
||||||
frame$: frame$.pipe(finalize(() => decodeSubscription.unsubscribe())),
|
frame$: frame$.pipe(finalize(() => decodeSubscription.unsubscribe())),
|
||||||
};
|
};
|
||||||
}));
|
})
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const defaultVideoTrack$ = loadedMetadata$.pipe(
|
const defaultVideoTrack$ = loadedMetadata$.pipe(
|
||||||
@ -422,6 +443,6 @@ export function createMatroskaSegment({
|
|||||||
videoTrackDecoder,
|
videoTrackDecoder,
|
||||||
audioTrackDecoder,
|
audioTrackDecoder,
|
||||||
defaultVideoTrack$,
|
defaultVideoTrack$,
|
||||||
defaultAudioTrack$
|
defaultAudioTrack$,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ import {
|
|||||||
type TrackEntryType,
|
type TrackEntryType,
|
||||||
} from '../schema';
|
} from '../schema';
|
||||||
import { type SegmentComponent } from './segment';
|
import { type SegmentComponent } from './segment';
|
||||||
import {SegmentComponentSystemTrait} from "./segment-component";
|
import { SegmentComponentSystemTrait } from './segment-component';
|
||||||
|
|
||||||
export abstract class BlockViewTrait {
|
export abstract class BlockViewTrait {
|
||||||
abstract get keyframe(): boolean;
|
abstract get keyframe(): boolean;
|
||||||
@ -82,6 +82,21 @@ export class ClusterSystem extends SegmentComponentSystemTrait<
|
|||||||
cluster: ClusterType,
|
cluster: ClusterType,
|
||||||
track: TrackEntryType
|
track: TrackEntryType
|
||||||
): Generator<BlockViewTrait> {
|
): Generator<BlockViewTrait> {
|
||||||
|
if (cluster.BlockGroup && cluster.SimpleBlock) {
|
||||||
|
const blocks = [];
|
||||||
|
for (const block of cluster.BlockGroup) {
|
||||||
|
if (block.Block.track === track.TrackNumber) {
|
||||||
|
blocks.push(new BlockGroupView(block));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const block of cluster.SimpleBlock) {
|
||||||
|
if (block.track === track.TrackNumber) {
|
||||||
|
blocks.push(new SimpleBlockView(block));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
blocks.sort((a, b) => a.relTime - b.relTime);
|
||||||
|
yield* blocks;
|
||||||
|
} else {
|
||||||
if (cluster.SimpleBlock) {
|
if (cluster.SimpleBlock) {
|
||||||
for (const block of cluster.SimpleBlock) {
|
for (const block of cluster.SimpleBlock) {
|
||||||
if (block.track === track.TrackNumber) {
|
if (block.track === track.TrackNumber) {
|
||||||
@ -98,3 +113,4 @@ export class ClusterSystem extends SegmentComponentSystemTrait<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
ParseCodecErrors,
|
ParseCodecErrors,
|
||||||
|
UnimplementedError,
|
||||||
UnsupportedCodecError,
|
UnsupportedCodecError,
|
||||||
} from '@konoplayer/core/errors';
|
} from '@konoplayer/core/errors';
|
||||||
import {
|
import {
|
||||||
@ -15,13 +16,14 @@ import {
|
|||||||
type VideoDecoderConfigExt,
|
type VideoDecoderConfigExt,
|
||||||
} from '../codecs';
|
} from '../codecs';
|
||||||
import {
|
import {
|
||||||
|
ContentCompAlgoRestrictionEnum,
|
||||||
|
ContentEncodingTypeRestrictionEnum,
|
||||||
TrackEntrySchema,
|
TrackEntrySchema,
|
||||||
type TrackEntryType,
|
type TrackEntryType,
|
||||||
TrackTypeRestrictionEnum,
|
TrackTypeRestrictionEnum,
|
||||||
} from '../schema';
|
} from '../schema';
|
||||||
import type { SegmentComponent } from './segment';
|
import type { SegmentComponent } from './segment';
|
||||||
import {SegmentComponentSystemTrait} from "./segment-component";
|
import { SegmentComponentSystemTrait } from './segment-component';
|
||||||
import {pick} from "lodash-es";
|
|
||||||
|
|
||||||
export interface GetTrackEntryOptions {
|
export interface GetTrackEntryOptions {
|
||||||
priority?: (v: SegmentComponent<TrackEntryType>) => number;
|
priority?: (v: SegmentComponent<TrackEntryType>) => number;
|
||||||
@ -226,4 +228,49 @@ export class TrackSystem extends SegmentComponentSystemTrait<
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buildFrameEncodingProcessor(
|
||||||
|
track: TrackEntryType
|
||||||
|
): undefined | ((source: Uint8Array) => Uint8Array) {
|
||||||
|
let encodings = track.ContentEncodings?.ContentEncoding;
|
||||||
|
if (!encodings?.length) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
encodings = encodings.toSorted(
|
||||||
|
(a, b) => Number(b.ContentEncodingOrder) - Number(a.ContentEncodingOrder)
|
||||||
|
);
|
||||||
|
const processors: Array<(source: Uint8Array) => Uint8Array> = [];
|
||||||
|
for (const encoing of encodings) {
|
||||||
|
if (
|
||||||
|
encoing.ContentEncodingType ===
|
||||||
|
ContentEncodingTypeRestrictionEnum.COMPRESSION
|
||||||
|
) {
|
||||||
|
const compression = encoing.ContentCompression;
|
||||||
|
const algo = compression?.ContentCompAlgo;
|
||||||
|
if (algo === ContentCompAlgoRestrictionEnum.HEADER_STRIPPING) {
|
||||||
|
const settings = compression?.ContentCompSettings;
|
||||||
|
if (settings?.length) {
|
||||||
|
processors.push((source: Uint8Array) => {
|
||||||
|
const dest = new Uint8Array(source.length + settings.length);
|
||||||
|
dest.set(source);
|
||||||
|
dest.set(settings, source.length);
|
||||||
|
return dest;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// TODO: dynamic import packages to support more compression algos
|
||||||
|
throw new UnimplementedError(
|
||||||
|
`compression algo ${ContentCompAlgoRestrictionEnum[algo as ContentCompAlgoRestrictionEnum]}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return function processor(source: Uint8Array): Uint8Array<ArrayBufferLike> {
|
||||||
|
let dest = source;
|
||||||
|
for (const processor of processors) {
|
||||||
|
dest = processor(dest);
|
||||||
|
}
|
||||||
|
return dest;
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user