Skip to content

Commit

Permalink
feat: add support for CMCD nor (#6091)
Browse files Browse the repository at this point in the history
* feat: add support for CMCD nor
* deps: update @svta/common-media-library to version 0.6.2
Resolves #6088
  • Loading branch information
littlespex authored Jan 29, 2024
1 parent 359ea21 commit 763c129
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/controller/cmcd-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CmcdObjectType } from '@svta/common-media-library/cmcd/CmcdObjectType';
import { CmcdStreamingFormat } from '@svta/common-media-library/cmcd/CmcdStreamingFormat';
import { appendCmcdHeaders } from '@svta/common-media-library/cmcd/appendCmcdHeaders';
import { appendCmcdQuery } from '@svta/common-media-library/cmcd/appendCmcdQuery';
import type { CmcdEncodeOptions } from '@svta/common-media-library/cmcd/CmcdEncodeOptions';
import { uuid } from '@svta/common-media-library/utils/uuid';
import { BufferHelper } from '../utils/buffer-helper';
import { logger } from '../utils/logger';
Expand Down Expand Up @@ -165,7 +166,7 @@ export default class CMCDController implements ComponentAPI {
data.su = this.buffering;
}

// TODO: Implement rtp, nrr, nor, dl
// TODO: Implement rtp, nrr, dl

const { includeKeys } = this;
if (includeKeys) {
Expand All @@ -175,14 +176,18 @@ export default class CMCDController implements ComponentAPI {
}, {});
}

const options: CmcdEncodeOptions = {
baseUrl: context.url,
};

if (this.useHeaders) {
if (!context.headers) {
context.headers = {};
}

appendCmcdHeaders(context.headers, data);
appendCmcdHeaders(context.headers, data, options);
} else {
context.url = appendCmcdQuery(context.url, data);
context.url = appendCmcdQuery(context.url, data, options);
}
}

Expand Down Expand Up @@ -223,12 +228,29 @@ export default class CMCDController implements ComponentAPI {
data.bl = this.getBufferLength(ot);
}

const next = this.getNextFrag(fragment);
if (next) {
if (next.url && next.url !== fragment.url) {
data.nor = next.url;
}
}

this.apply(context, data);
} catch (error) {
logger.warn('Could not generate segment CMCD data.', error);
}
};

private getNextFrag(fragment: Fragment): Fragment | undefined {
const levelDetails = this.hls.levels[fragment.level]?.details;
if (levelDetails) {
const index = (fragment.sn as number) - levelDetails.startSN;
return levelDetails.fragments[index + 1];
}

return undefined;
}

/**
* The CMCD object type.
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/controller/cmcd-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ describe('CMCDController', function () {
expect(url).to.equal(base.url);
expectField(headers[CmcdHeaderField.SESSION], `cid="${contentId}"`);
});

it('uses includeKeys if configured', function () {
const contentId = 'CONTENT_ID';
setupEach({ includeKeys: ['cid'], contentId });

const { url } = applyPlaylistData();

expect(url).to.equal(`${base.url}?CMCD=cid%3D%22${contentId}%22`);
});
});
});
});

0 comments on commit 763c129

Please sign in to comment.