Skip to content

Commit

Permalink
DashMetrics (#3102)
Browse files Browse the repository at this point in the history
* DashMetrics: add method getCurrentBufferState()

* index.d.ts:
- add getcurrentBufferState()
- remove getLatestBufferInfoVO()

* index.d.ts: add type MediaType

* index.d.ts: add type MediaType

* MediaPlayer: update jsdoc for MediaType parameters

* DashMetrics: remove getLatestBufferInfoVO

* remove readonly param from public DashMetrics methods

* update getCurrentBufferLevel calls

* update unit tests

* add jsdoc for MediaType
  • Loading branch information
bbert authored Mar 20, 2020
1 parent 813dc59 commit 2ea3e92
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 112 deletions.
72 changes: 37 additions & 35 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ declare namespace dashjs {
scanType?: string;
}

export type MediaType = 'video' | 'audio' | 'text' | 'fragmentedText' | 'embeddedText' | 'image';

export class MediaInfo {
id: string | null;
index: number | null;
type: 'video' | 'audio' | 'text' | 'fragmentedText' | 'embeddedText' | null;
type: MediaType | null;
streamInfo: StreamInfo | null;
representationCount: number;
labels: string[];
Expand Down Expand Up @@ -238,18 +240,18 @@ declare namespace dashjs {
formatUTC(time: number, locales: string, hour12: boolean, withDate?: boolean): string;
getVersion(): string;
getDebug(): Debug;
getBufferLength(type: 'video' | 'audio' | 'fragmentedText'): number;
getBufferLength(type: MediaType): number;
getVideoModel(): VideoModel;
getTTMLRenderingDiv(): HTMLDivElement | null;
getVideoElement(): HTMLVideoElement;
getSource(): string | object;
getTopBitrateInfoFor(type: 'video' | 'audio'): BitrateInfo;
getTopBitrateInfoFor(type: MediaType): BitrateInfo;
setAutoPlay(value: boolean): void;
getAutoPlay(): boolean;
getDashMetrics(): DashMetrics;
getDashAdapter(): DashAdapter;
getQualityFor(type: 'video' | 'audio' | 'image'): number;
setQualityFor(type: 'video' | 'audio' | 'image', value: number): void;
getQualityFor(type: MediaType): number;
setQualityFor(type: MediaType, value: number): void;
updatePortalSize(): void;
enableText(enable: boolean): void;
setTextTrack(idx: number): void;
Expand All @@ -258,16 +260,16 @@ declare namespace dashjs {
getTextDefaultEnabled(): boolean | undefined;
setTextDefaultEnabled(enable: boolean): void;
getThumbnail(time: number): Thumbnail;
getBitrateInfoListFor(type: 'video' | 'audio' | 'image'): BitrateInfo[];
getBitrateInfoListFor(type: MediaType): BitrateInfo[];
getStreamsFromManifest(manifest: object): StreamInfo[];
getTracksFor(type: 'video' | 'audio' | 'text' | 'fragmentedText'): MediaInfo[];
getTracksForTypeFromManifest(type: 'video' | 'audio' | 'text' | 'fragmentedText', manifest: object, streamInfo: StreamInfo): MediaInfo[];
getCurrentTrackFor(type: 'video' | 'audio' | 'text' | 'fragmentedText'): MediaInfo | null;
setInitialMediaSettingsFor(type: 'video' | 'audio', value: MediaSettings): void;
getInitialMediaSettingsFor(type: 'video' | 'audio'): MediaSettings;
getTracksFor(type: MediaType): MediaInfo[];
getTracksForTypeFromManifest(type: MediaType, manifest: object, streamInfo: StreamInfo): MediaInfo[];
getCurrentTrackFor(type: MediaType): MediaInfo | null;
setInitialMediaSettingsFor(type: MediaType, value: MediaSettings): void;
getInitialMediaSettingsFor(type: MediaType): MediaSettings;
setCurrentTrack(track: MediaInfo): void;
getTrackSwitchModeFor(type: 'video' | 'audio'): TrackSwitchMode;
setTrackSwitchModeFor(type: 'video' | 'audio', mode: TrackSwitchMode): void;
getTrackSwitchModeFor(type: MediaType): TrackSwitchMode;
setTrackSwitchModeFor(type: MediaType, mode: TrackSwitchMode): void;
setSelectionModeForInitialTrack(mode: TrackSelectionMode): void;
getSelectionModeForInitialTrack(): TrackSelectionMode;
retrieveManifest(url: string, callback: (manifest: object | null, error: any) => void): void;
Expand Down Expand Up @@ -418,12 +420,12 @@ declare namespace dashjs {

export interface BufferEvent extends Event {
type: MediaPlayerEvents['BUFFER_EMPTY' | 'BUFFER_LOADED'];
mediaType: 'video' | 'audio' | 'fragmentedText';
mediaType: MediaType;
}

export interface BufferStateChangedEvent extends Event {
type: MediaPlayerEvents['BUFFER_LEVEL_STATE_CHANGED'];
mediaType: 'video' | 'audio' | 'fragmentedText';
mediaType: MediaType;
sender: object;
state: 'bufferStalled' | 'bufferLoaded';
streamInfo: StreamInfo;
Expand Down Expand Up @@ -537,7 +539,7 @@ declare namespace dashjs {
type: MediaPlayerEvents['FRAGMENT_LOADING_ABANDONED'];
streamProcessor: object;
request: object;
mediaType: 'video' | 'audio' | 'fragmentedText';
mediaType: MediaType;
}

export class KeyError {
Expand Down Expand Up @@ -608,14 +610,14 @@ declare namespace dashjs {

export interface MetricEvent extends Event {
type: MediaPlayerEvents['METRIC_ADDED' | 'METRIC_UPDATED'];
mediaType: 'video' | 'audio' | 'fragmentedText';
mediaType: MediaType;
metric: MetricType;
value: object;
}

export interface MetricChangedEvent extends Event {
type: MediaPlayerEvents['METRIC_CHANGED'];
mediaType: 'video' | 'audio' | 'fragmentedText';
mediaType: MediaType;
}

export interface PeriodSwitchEvent extends Event {
Expand Down Expand Up @@ -678,21 +680,21 @@ declare namespace dashjs {

export interface TrackChangeRenderedEvent extends Event {
type: MediaPlayerEvents['TRACK_CHANGE_RENDERED'];
mediaType: 'video' | 'audio' | 'fragmentedText';
mediaType: MediaType;
oldMediaInfo: MediaInfo;
newMediaInfo: MediaInfo;
}

export interface QualityChangeRenderedEvent extends Event {
type: MediaPlayerEvents['QUALITY_CHANGE_RENDERED'];
mediaType: 'video' | 'audio' | 'fragmentedText';
mediaType: MediaType;
oldQuality: number;
newQuality: number;
}

export interface QualityChangeRequestedEvent extends Event {
type: MediaPlayerEvents['QUALITY_CHANGE_REQUESTED'];
mediaType: 'video' | 'audio' | 'fragmentedText';
mediaType: MediaType;
oldQuality: number;
newQuality: number;
streamInfo: StreamInfo | null;
Expand Down Expand Up @@ -746,7 +748,7 @@ declare namespace dashjs {
firstByteDate: Date;
index: number;
mediaInfo: MediaInfo;
mediaType: 'video' | 'audio' | 'text' | 'fragmentedText' | 'embeddedText';
mediaType: MediaType;
quality: number;
representationId: string;
requestStartDate: Date;
Expand Down Expand Up @@ -784,7 +786,7 @@ declare namespace dashjs {
getStartTime(): number;
getId(): string;
getStreamInfo(): StreamInfo | null;
getBitrateListFor(type: 'video' | 'audio' | 'image'): BitrateInfo[];
getBitrateListFor(type: MediaType): BitrateInfo[];
updateData(updatedStreamInfo: StreamInfo): void;
reset(): void;
}
Expand Down Expand Up @@ -813,23 +815,23 @@ declare namespace dashjs {
t: Date;
}

export interface ILatestBufferLevelVO {
level: number;
t: Date;
export interface IBufferState {
target: number;
state: string;
}

export interface DashMetrics {
getCurrentRepresentationSwitch(type: 'video' | 'audio' | 'image', readOnly: boolean): ICurrentRepresentationSwitch;
getLatestBufferInfoVO(): ILatestBufferLevelVO;
getCurrentBufferLevel(type: 'video' | 'audio' | 'image', readOnly: boolean): number;
getCurrentHttpRequest(type: 'video' | 'audio' | 'image', readOnly: boolean): object;
getHttpRequests(type: 'video' | 'audio' | 'image'): object[];
getCurrentRepresentationSwitch(type: MediaType): ICurrentRepresentationSwitch;
getCurrentBufferState(type: MediaType): IBufferState;
getCurrentBufferLevel(type: MediaType): number;
getCurrentHttpRequest(type: MediaType): object;
getHttpRequests(type: MediaType): object[];
getCurrentDroppedFrames(): IDroppedFrames;
getCurrentSchedulingInfo(type: 'video' | 'audio' | 'image'): object;
getCurrentDVRInfo(type: 'video' | 'audio' | 'image'): IDVRInfo[];
getCurrentSchedulingInfo(type: MediaType): object;
getCurrentDVRInfo(type: MediaType): IDVRInfo[];
getCurrentManifestUpdate(): any;
getLatestFragmentRequestHeaderValueByID(id: string): string;
getLatestMPDRequestHeaderValueByID(type: 'video' | 'audio' | 'image', id: string): string;
getLatestMPDRequestHeaderValueByID(type: MediaType, id: string): string;
}

export interface DashAdapter {
Expand All @@ -842,7 +844,7 @@ declare namespace dashjs {
* @param bufferType String 'audio' or 'video',
* @param periodIdx Make sure this is the period index not id
*/
getMaxIndexForBufferType(bufferType: 'video' | 'audio', periodIdx: number): number;
getMaxIndexForBufferType(bufferType: MediaType, periodIdx: number): number;
}

export class ProtectionData {
Expand Down
43 changes: 16 additions & 27 deletions src/dash/DashMetrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ function DashMetrics(config) {

/**
* @param {string} mediaType
* @param {boolean} readOnly
* @returns {*}
* @memberof module:DashMetrics
* @instance
*/
function getCurrentRepresentationSwitch(mediaType, readOnly) {
const metrics = metricsModel.getMetricsFor(mediaType, readOnly);
function getCurrentRepresentationSwitch(mediaType) {
const metrics = metricsModel.getMetricsFor(mediaType, true);
return getCurrent(metrics, MetricsConstants.TRACK_SWITCH);
}

Expand All @@ -94,30 +93,27 @@ function DashMetrics(config) {
}

/**
* @param {string} mediaType
* @param {boolean} readOnly
* @param {string} infoType
* @returns {*}
* @memberof module:DashMetrics
* @param {string} type
* @returns {number}
* @instance
*/
function getLatestBufferInfoVO(mediaType, readOnly, infoType) {
const metrics = metricsModel.getMetricsFor(mediaType, readOnly);
return getCurrent(metrics, infoType);
function getCurrentBufferState(type) {
const metrics = metricsModel.getMetricsFor(type, true);
return getCurrent(metrics, MetricsConstants.BUFFER_STATE);
}

/**
* @param {string} type
* @param {boolean} readOnly
* @returns {number}
* @memberof module:DashMetrics
* @instance
*/
function getCurrentBufferLevel(type, readOnly) {
const vo = getLatestBufferInfoVO(type, readOnly, MetricsConstants.BUFFER_LEVEL);
function getCurrentBufferLevel(type) {
const metrics = metricsModel.getMetricsFor(type, true);
const metric = getCurrent(metrics, MetricsConstants.BUFFER_LEVEL);

if (vo) {
return Round10.round10(vo.level / 1000, -3);
if (metric) {
return Round10.round10(metric.level / 1000, -3);
}

return 0;
Expand Down Expand Up @@ -155,13 +151,12 @@ function DashMetrics(config) {

/**
* @param {string} mediaType
* @param {boolean} readOnly
* @returns {*}
* @memberof module:DashMetrics
* @instance
*/
function getCurrentHttpRequest(mediaType, readOnly) {
const metrics = metricsModel.getMetricsFor(mediaType, readOnly);
function getCurrentHttpRequest(mediaType) {
const metrics = metricsModel.getMetricsFor(mediaType, true);

if (!metrics) {
return null;
Expand Down Expand Up @@ -225,14 +220,8 @@ function DashMetrics(config) {
if (!metrics) {
return null;
}

const list = metrics[metricName];

if (!list || list.length <= 0) {
return null;
}

return list[list.length - 1];
return (!list || list.length === 0) ? null : list[list.length - 1];
}

/**
Expand Down Expand Up @@ -511,7 +500,7 @@ function DashMetrics(config) {

instance = {
getCurrentRepresentationSwitch: getCurrentRepresentationSwitch,
getLatestBufferInfoVO: getLatestBufferInfoVO,
getCurrentBufferState: getCurrentBufferState,
getCurrentBufferLevel: getCurrentBufferLevel,
getCurrentHttpRequest: getCurrentHttpRequest,
getHttpRequests: getHttpRequests,
Expand Down
Loading

0 comments on commit 2ea3e92

Please sign in to comment.