Skip to content

Commit

Permalink
Better types
Browse files Browse the repository at this point in the history
  • Loading branch information
Aqours authored and xqq committed Jun 23, 2018
1 parent 7ab51b1 commit e861e0d
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 43 deletions.
244 changes: 201 additions & 43 deletions d.ts/flv.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
// flv.js TypeScript definition file

declare namespace FlvJs {
interface AnyObject {
[k: string]: any;
}

interface MediaSegment {
duration: number;
filesize?: number;
Expand All @@ -46,37 +42,140 @@ declare namespace FlvJs {
}

interface Config {
/**
* @desc Enable separated thread for transmuxing (unstable for now)
* @defaultvalue false
*/
enableWorker?: boolean;
/**
* @desc Enable IO stash buffer. Set to false if you need realtime (minimal latency) for live stream
* playback, but may stalled if there's network jittering.
* @defaultvalue true
*/
enableStashBuffer?: boolean;
/**
* @desc Indicates IO stash buffer initial size. Default is `384KB`. Indicate a suitable size can
* improve video load/seek time.
*/
stashInitialSize?: number;

/**
* @desc Same to `isLive` in **MediaDataSource**, ignored if has been set in MediaDataSource structure.
* @defaultvalue false
*/
isLive?: boolean;

/**
* @desc Abort the http connection if there's enough data for playback.
* @defaultvalue true
*/
lazyLoad?: boolean;
/**
* @desc Indicates how many seconds of data to be kept for `lazyLoad`.
* @defaultvalue 3 * 60
*/
lazyLoadMaxDuration?: number;
/**
* @desc Indicates the `lazyLoad` recover time boundary in seconds.
* @defaultvalue 30
*/
lazyLoadRecoverDuration?: number;
/**
* @desc Do load after MediaSource `sourceopen` event triggered. On Chrome, tabs which
* be opened in background may not trigger `sourceopen` event until switched to that tab.
* @defaultvalue true
*/
deferLoadAfterSourceOpen?: boolean;

/**
* @desc Do auto cleanup for SourceBuffer
* @defaultvalue false (from docs)
*/
autoCleanupSourceBuffer?: boolean;
/**
* @desc When backward buffer duration exceeded this value (in seconds), do auto cleanup for SourceBuffer
* @defaultvalue 3 * 60
*/
autoCleanupMaxBackwardDuration?: number;
/**
* @desc Indicates the duration in seconds to reserve for backward buffer when doing auto cleanup.
* @defaultvalue 2 * 60
*/
autoCleanupMinBackwardDuration?: number;

/**
* @defaultvalue 600
*/
statisticsInfoReportInterval?: number;

/**
* @desc Fill silent audio frames to avoid a/v unsync when detect large audio timestamp gap.
* @defaultvalue true
*/
fixAudioTimestampGap?: boolean;

/**
* @desc Accurate seek to any frame, not limited to video IDR frame, but may a bit slower.
* Available on Chrome > 50, FireFox and Safari.
* @defaultvalue false
*/
accurateSeek?: boolean;
seekType?: string; // [range, param, custom]
/**
* @desc 'range' use range request to seek, or 'param' add params into url to indicate request range.
* @defaultvalue 'range'
*/
seekType?: 'range' | 'param' | 'custom';
/**
* @desc Indicates seek start parameter name for seekType = 'param'
* @defaultvalue 'bstart'
*/
seekParamStart?: string;
/**
* @desc Indicates seek end parameter name for seekType = 'param'
* @defaultvalue 'bend'
*/
seekParamEnd?: string;
/**
* @desc Send Range: bytes=0- for first time load if use Range seek
* @defaultvalue false
*/
rangeLoadZeroStart?: boolean;
customSeekHandler?: any;
/**
* @desc Indicates a custom seek handler
* @desc Should implement `SeekHandler` interface
*/
customSeekHandler?: CustomSeekHandlerConstructor;
/**
* @desc Reuse 301/302 redirected url for subsequence request like seek, reconnect, etc.
* @defaultvalue false
*/
reuseRedirectedURL?: boolean;
referrerPolicy?: string;

/**
* @desc Indicates the Referrer Policy when using FetchStreamLoader
* @defaultvalue 'no-referrer-when-downgrade' (from docs)
*/
referrerPolicy?: ReferrerPolicy;

/**
* @desc Should implement `BaseLoader` interface
*/
customLoader?: CustomLoaderConstructor;
}

interface CustomSeekHandlerConstructor {
new(): SeekHandler;
}

interface SeekHandler {
getConfig(sourceURL: string, range: Range): SeekConfig;
removeURLParameters(url: string): string;
}

interface SeekConfig {
url: string;
headers: Headers | object;
}

interface BaseLoaderConstructor {
new(typeName: string): BaseLoader;
}
Expand All @@ -85,23 +184,22 @@ declare namespace FlvJs {
_status: number;
_needStash: boolean;

constructor: BaseLoaderConstructor;
destroy(): void;
isWorking(): boolean;
readonly type: string;
readonly status: number;
readonly needStashBuffer: boolean;
onContentLengthKnown: () => void;
onURLRedirect: () => void;
onDataArrival: () => void;
onError: () => void;
onComplete: () => void;
onContentLengthKnown: (contentLength: number) => void;
onURLRedirect: (redirectedURL: string) => void;
onDataArrival: (chunk: ArrayBuffer, byteStart: number, receivedLength?: number) => void;
onError: (errorType: LoaderErrors, errorInfo: LoaderErrorMessage) => void;
onComplete: (rangeFrom: number, rangeTo: number) => void;
open(dataSource: MediaSegment, range: Range): void;
abort(): void;
}

interface CustomLoaderConstructor {
new(seekHandler: any, config: Config): BaseLoader;
new(seekHandler: SeekHandler, config: Config): BaseLoader;
}

interface Range {
Expand All @@ -110,20 +208,25 @@ declare namespace FlvJs {
}

interface LoaderStatus {
kIdle: 0;
kConnecting: 1;
kBuffering: 2;
kError: 3;
kComplete: 4;
readonly kIdle: 0;
readonly kConnecting: 1;
readonly kBuffering: 2;
readonly kError: 3;
readonly kComplete: 4;
}

interface LoaderErrors {
OK: 'OK';
EXCEPTION: 'Exception';
HTTP_STATUS_CODE_INVALID: 'HttpStatusCodeInvalid';
CONNECTING_TIMEOUT: 'ConnectingTimeout';
EARLY_EOF: 'EarlyEof';
UNRECOVERABLE_EARLY_EOF: 'UnrecoverableEarlyEof';
readonly OK: 'OK';
readonly EXCEPTION: 'Exception';
readonly HTTP_STATUS_CODE_INVALID: 'HttpStatusCodeInvalid';
readonly CONNECTING_TIMEOUT: 'ConnectingTimeout';
readonly EARLY_EOF: 'EarlyEof';
readonly UNRECOVERABLE_EARLY_EOF: 'UnrecoverableEarlyEof';
}

interface LoaderErrorMessage {
code: number;
msg: string;
}

interface FeatureList {
Expand All @@ -141,39 +244,73 @@ declare namespace FlvJs {
}

interface Player {
constructor: PlayerConstructor;
destroy(): void;
on(event: string, listener: () => void): void;
off(event: string, listener: () => void): void;
on(event: string, listener: (...args: any[]) => void): void;
off(event: string, listener: (...args: any[]) => void): void;
attachMediaElement(mediaElement: HTMLMediaElement): void;
detachMediaElement(): void;
load(): void;
unload(): void;
play(): Promise<void>;
play(): Promise<void> | void;
pause(): void;
type: string;
buffered: TimeRanges;
duration: number;
volume: number;
muted: boolean;
currentTime: number;
mediaInfo: AnyObject;
statisticsInfo: AnyObject;
mediaInfo: NativePlayerMediaInfo | FlvPlayerMediaInfo;
statisticsInfo: NativePlayerStatisticsInfo | FlvPlayerStatisticsInfo;
}

interface NativePlayerStatisticsInfo {
playerType: 'NativePlayer';
url: string;
decodedFrames?: number;
droppedFrames?: number;
}

interface FlvPlayerStatisticsInfo {
playerType: 'FlvPlayer';
decodedFrames?: number;
droppedFrames?: number;
}

interface NativePlayerMediaInfo {
mimeType: string;
duration?: number;
width?: number;
height?: number;
}

interface FlvPlayerMediaInfo extends NativePlayerMediaInfo {
audioCodec?: string;
videoCodec?: string;
audioDataRate?: number;
videoDataRate?: number;
hasAudio?: boolean;
hasVideo?: boolean;
chromaFormat?: string;
fps?: number;

[k: string]: any;
}

/**
* @deprecated Use FlvJs.Player instead.
* @since 1.4
*/
// tslint:disable-next-line
interface FlvPlayer extends Player {}

/**
* @deprecated Use FlvJs.Player instead.
* @since 1.4
*/
// tslint:disable-next-line
interface NativePlayer extends Player {}

interface LoggingControl {
interface LoggingControlConfig {
forceGlobalTag: boolean;
globalTag: string;
enableAll: boolean;
Expand All @@ -182,10 +319,13 @@ declare namespace FlvJs {
enableInfo: boolean;
enableWarn: boolean;
enableError: boolean;
getConfig(): AnyObject;
applyConfig(config: AnyObject): void;
addLogListener(listener: () => void): void;
removeLogListener(listener: () => void): void;
}

interface LoggingControl extends LoggingControlConfig {
getConfig(): LoggingControlConfig;
applyConfig(config: Partial<LoggingControlConfig>): void;
addLogListener(listener: (...args: any[]) => void): void;
removeLogListener(listener: (...args: any[]) => void): void;
}

interface Events {
Expand Down Expand Up @@ -222,17 +362,35 @@ declare var FlvJs: {
isSupported(): boolean;
getFeatureList(): FlvJs.FeatureList;

/**
* @deprecated Use `FlvJs.BaseLoaderConstructor` instead.
* Because it's not available on `flvjs` variable.
* @desc implement interface `BaseLoader`
* @since 1.4
*/
BaseLoader: FlvJs.BaseLoaderConstructor;
/**
* @deprecated Use `FlvJs.BaseLoaderConstructor` instead.
* Because it's not available on `flvjs` variable.
* @since 1.4
*/
LoaderStatus: FlvJs.LoaderStatus;
/**
* @deprecated Use `FlvJs.BaseLoaderConstructor` instead.
* Because it's not available on `flvjs` variable.
* @since 1.4
*/
LoaderErrors: FlvJs.LoaderErrors;

Events: FlvJs.Events;
ErrorTypes: FlvJs.ErrorTypes;
ErrorDetails: FlvJs.ErrorDetails;
readonly version: string;

readonly Events: Readonly<FlvJs.Events>;
readonly ErrorTypes: Readonly<FlvJs.ErrorTypes>;
readonly ErrorDetails: Readonly<FlvJs.ErrorDetails>;

FlvPlayer: FlvJs.PlayerConstructor;
NativePlayer: FlvJs.PlayerConstructor;
LoggingControl: FlvJs.LoggingControl;
readonly FlvPlayer: FlvJs.PlayerConstructor;
readonly NativePlayer: FlvJs.PlayerConstructor;
readonly LoggingControl: FlvJs.LoggingControl;
};

export default FlvJs;
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// TypeScript Version: 2.3

import '../d.ts/flv.d.ts';

0 comments on commit e861e0d

Please sign in to comment.