Skip to content

Commit

Permalink
feat(#48): Added support for negative indexing when using rsq
Browse files Browse the repository at this point in the history
  • Loading branch information
zapfire88 committed Feb 1, 2024
1 parent 8715493 commit 428c892
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/manifests/handlers/hls/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ export default async function hlsMediaHandler(
}
}
}

const [error, allMutations, levelMutations] =
configUtils.getAllManifestConfigs(
mediaSequence,
false,
mediaSequenceOffset
mediaSequenceOffset,
mediaM3U.items.PlaylistItem.length
);
if (error) {
return generateErrorResponse(error);
Expand Down
38 changes: 38 additions & 0 deletions src/manifests/utils/configs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,46 @@ describe('configs', () => {
// Act

const [err, actual] = configs.getAllManifestConfigs(0, false, 100);
// Assert
expect(err).toBeNull();
expect(actual.get(115)).toEqual(
new Map([
[
'statusCode',
{
fields: { code: 400 },
sq: 115
}
]
])
);
expect(actual.get(15)).toEqual(
new Map([
[
'throttle',
{
fields: { rate: 1000 },
sq: 15
}
]
])
);
});
it('should handle media sequence offsets with negative rsq value', () => {
// Arrange
const configs = statefulConfig.corruptorConfigUtils(
new URLSearchParams(
'statusCode=[{rsq:-1,code:400}]&throttle=[{sq:15,rate:1000}]'
)
);

configs.register(statusCodeConfig).register(throttleConfig);

// Act

const [err, actual] = configs.getAllManifestConfigs(0, false, 100, 15);
// Assert
console.log(actual);
expect(err).toBeNull();
expect(actual.get(115)).toEqual(
new Map([
Expand Down
18 changes: 15 additions & 3 deletions src/manifests/utils/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export interface CorruptorConfigUtils {
getAllManifestConfigs: (
mseq?: number,
isDash?: boolean,
mseqOffset?: number
mseqOffset?: number,
playlistSize?: number
) => [
ServiceError | null,
IndexedCorruptorConfigMap | null,
Expand Down Expand Up @@ -131,7 +132,12 @@ export const corruptorConfigUtils = function (
}
return this;
},
getAllManifestConfigs(mseq = 0, isDash = false, mseqOffset = 0) {
getAllManifestConfigs(
mseq = 0,
isDash = false,
mseqOffset = 0,
playlistSize = 0
) {
const outputMap = new CorruptorIndexMap();
const levelMap = new CorruptorLevelMap();
const configs = (
Expand Down Expand Up @@ -171,7 +177,12 @@ export const corruptorConfigUtils = function (
// Replace relative sequence numbers with absolute ones
params = params.map((param) => {
if (param.rsq) {
param.sq = Number(param.rsq) + mseqOffset;
const rsq = Number(param.rsq);
param['sq'] =
rsq < 0 && playlistSize > 0
? mseqOffset + playlistSize + rsq + 1
: Number(param.rsq) + mseqOffset;
console.log('sq: ' + param.sq + ' | rsq: ' + Number(param.rsq));
delete param.rsq;
}
return param;
Expand All @@ -182,6 +193,7 @@ export const corruptorConfigUtils = function (
if (error) {
return [error, null];
}

configList.forEach((item) => {
if (item.i != undefined) {
outputMap.deepSet(item.i, config.name, item, false);
Expand Down
14 changes: 4 additions & 10 deletions src/manifests/utils/hlsManifestUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { M3U, Manifest } from '../../shared/types';
import {
newState,
proxyPathBuilder,
segmentUrlParamString
} from '../../shared/utils';
import { proxyPathBuilder, segmentUrlParamString } from '../../shared/utils';
import { CorruptorConfigMap, IndexedCorruptorConfigMap } from './configs';
import clone from 'clone';

Expand Down Expand Up @@ -143,15 +139,13 @@ export default function (): HLSManifestTools {
configsMap: IndexedCorruptorConfigMap
) {
const m3u: M3U = clone(originalM3U);
const playlistSize = m3u.items.PlaylistItem.length;

// configs for each index
const corruptions = this.utils.mergeMap(
m3u.items.PlaylistItem.length,
configsMap
);
const corruptions = this.utils.mergeMap(playlistSize, configsMap);

// Attach corruptions to manifest
for (let i = 0; i < m3u.items.PlaylistItem.length; i++) {
for (let i = 0; i < playlistSize; i++) {
const item = m3u.items.PlaylistItem[i];
const corruption = corruptions[i];
let sourceSegURL: string = item.get('uri');
Expand Down

0 comments on commit 428c892

Please sign in to comment.