flv.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  3. *
  4. * @author zheng qian <xqq@xqq.im>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. // flv.js TypeScript definition file
  19. declare namespace FlvJs {
  20. interface MediaSegment {
  21. duration: number;
  22. filesize?: number;
  23. url: string;
  24. }
  25. interface MediaDataSource {
  26. type: string;
  27. isLive?: boolean;
  28. cors?: boolean;
  29. withCredentials?: boolean;
  30. hasAudio?: boolean;
  31. hasVideo?: boolean;
  32. duration?: number;
  33. filesize?: number;
  34. url?: string;
  35. segments?: MediaSegment[];
  36. }
  37. interface Config {
  38. /**
  39. * @desc Enable separated thread for transmuxing (unstable for now)
  40. * @defaultvalue false
  41. */
  42. enableWorker?: boolean;
  43. /**
  44. * @desc Enable IO stash buffer. Set to false if you need realtime (minimal latency) for live stream
  45. * playback, but may stalled if there's network jittering.
  46. * @defaultvalue true
  47. */
  48. enableStashBuffer?: boolean;
  49. /**
  50. * @desc Indicates IO stash buffer initial size. Default is `384KB`. Indicate a suitable size can
  51. * improve video load/seek time.
  52. */
  53. stashInitialSize?: number;
  54. /**
  55. * @desc Same to `isLive` in **MediaDataSource**, ignored if has been set in MediaDataSource structure.
  56. * @defaultvalue false
  57. */
  58. isLive?: boolean;
  59. /**
  60. * @desc Abort the http connection if there's enough data for playback.
  61. * @defaultvalue true
  62. */
  63. lazyLoad?: boolean;
  64. /**
  65. * @desc Indicates how many seconds of data to be kept for `lazyLoad`.
  66. * @defaultvalue 3 * 60
  67. */
  68. lazyLoadMaxDuration?: number;
  69. /**
  70. * @desc Indicates the `lazyLoad` recover time boundary in seconds.
  71. * @defaultvalue 30
  72. */
  73. lazyLoadRecoverDuration?: number;
  74. /**
  75. * @desc Do load after MediaSource `sourceopen` event triggered. On Chrome, tabs which
  76. * be opened in background may not trigger `sourceopen` event until switched to that tab.
  77. * @defaultvalue true
  78. */
  79. deferLoadAfterSourceOpen?: boolean;
  80. /**
  81. * @desc Do auto cleanup for SourceBuffer
  82. * @defaultvalue false (from docs)
  83. */
  84. autoCleanupSourceBuffer?: boolean;
  85. /**
  86. * @desc When backward buffer duration exceeded this value (in seconds), do auto cleanup for SourceBuffer
  87. * @defaultvalue 3 * 60
  88. */
  89. autoCleanupMaxBackwardDuration?: number;
  90. /**
  91. * @desc Indicates the duration in seconds to reserve for backward buffer when doing auto cleanup.
  92. * @defaultvalue 2 * 60
  93. */
  94. autoCleanupMinBackwardDuration?: number;
  95. /**
  96. * @defaultvalue 600
  97. */
  98. statisticsInfoReportInterval?: number;
  99. /**
  100. * @desc Fill silent audio frames to avoid a/v unsync when detect large audio timestamp gap.
  101. * @defaultvalue true
  102. */
  103. fixAudioTimestampGap?: boolean;
  104. /**
  105. * @desc Accurate seek to any frame, not limited to video IDR frame, but may a bit slower.
  106. * Available on Chrome > 50, FireFox and Safari.
  107. * @defaultvalue false
  108. */
  109. accurateSeek?: boolean;
  110. /**
  111. * @desc 'range' use range request to seek, or 'param' add params into url to indicate request range.
  112. * @defaultvalue 'range'
  113. */
  114. seekType?: 'range' | 'param' | 'custom';
  115. /**
  116. * @desc Indicates seek start parameter name for seekType = 'param'
  117. * @defaultvalue 'bstart'
  118. */
  119. seekParamStart?: string;
  120. /**
  121. * @desc Indicates seek end parameter name for seekType = 'param'
  122. * @defaultvalue 'bend'
  123. */
  124. seekParamEnd?: string;
  125. /**
  126. * @desc Send Range: bytes=0- for first time load if use Range seek
  127. * @defaultvalue false
  128. */
  129. rangeLoadZeroStart?: boolean;
  130. /**
  131. * @desc Indicates a custom seek handler
  132. * @desc Should implement `SeekHandler` interface
  133. */
  134. customSeekHandler?: CustomSeekHandlerConstructor;
  135. /**
  136. * @desc Reuse 301/302 redirected url for subsequence request like seek, reconnect, etc.
  137. * @defaultvalue false
  138. */
  139. reuseRedirectedURL?: boolean;
  140. /**
  141. * @desc Indicates the Referrer Policy when using FetchStreamLoader
  142. * @defaultvalue 'no-referrer-when-downgrade' (from docs)
  143. */
  144. referrerPolicy?: ReferrerPolicy;
  145. /**
  146. * @desc Indicates additional headers that will be added to request
  147. */
  148. headers?: {
  149. [k: string]: string
  150. }
  151. /**
  152. * @desc Should implement `BaseLoader` interface
  153. */
  154. customLoader?: CustomLoaderConstructor;
  155. }
  156. interface CustomSeekHandlerConstructor {
  157. new(): SeekHandler;
  158. }
  159. interface SeekHandler {
  160. getConfig(sourceURL: string, range: Range): SeekConfig;
  161. removeURLParameters(url: string): string;
  162. }
  163. interface SeekConfig {
  164. url: string;
  165. headers: Headers | object;
  166. }
  167. interface BaseLoaderConstructor {
  168. new(typeName: string): BaseLoader;
  169. }
  170. interface BaseLoader {
  171. _status: number;
  172. _needStash: boolean;
  173. destroy(): void;
  174. isWorking(): boolean;
  175. readonly type: string;
  176. readonly status: number;
  177. readonly needStashBuffer: boolean;
  178. onContentLengthKnown: (contentLength: number) => void;
  179. onURLRedirect: (redirectedURL: string) => void;
  180. onDataArrival: (chunk: ArrayBuffer, byteStart: number, receivedLength?: number) => void;
  181. onError: (errorType: LoaderErrors, errorInfo: LoaderErrorMessage) => void;
  182. onComplete: (rangeFrom: number, rangeTo: number) => void;
  183. open(dataSource: MediaSegment, range: Range): void;
  184. abort(): void;
  185. }
  186. interface CustomLoaderConstructor {
  187. new(seekHandler: SeekHandler, config: Config): BaseLoader;
  188. }
  189. interface Range {
  190. from: number;
  191. to: number;
  192. }
  193. interface LoaderStatus {
  194. readonly kIdle: 0;
  195. readonly kConnecting: 1;
  196. readonly kBuffering: 2;
  197. readonly kError: 3;
  198. readonly kComplete: 4;
  199. }
  200. interface LoaderErrors {
  201. readonly OK: 'OK';
  202. readonly EXCEPTION: 'Exception';
  203. readonly HTTP_STATUS_CODE_INVALID: 'HttpStatusCodeInvalid';
  204. readonly CONNECTING_TIMEOUT: 'ConnectingTimeout';
  205. readonly EARLY_EOF: 'EarlyEof';
  206. readonly UNRECOVERABLE_EARLY_EOF: 'UnrecoverableEarlyEof';
  207. }
  208. interface LoaderErrorMessage {
  209. code: number;
  210. msg: string;
  211. }
  212. interface FeatureList {
  213. mseFlvPlayback: boolean;
  214. mseLiveFlvPlayback: boolean;
  215. networkStreamIO: boolean;
  216. networkLoaderName: string;
  217. nativeMP4H264Playback: boolean;
  218. nativeWebmVP8Playback: boolean;
  219. nativeWebmVP9Playback: boolean;
  220. }
  221. interface PlayerConstructor {
  222. new (mediaDataSource: MediaDataSource, config?: Config): Player;
  223. }
  224. interface Player {
  225. destroy(): void;
  226. on(event: string, listener: (...args: any[]) => void): void;
  227. off(event: string, listener: (...args: any[]) => void): void;
  228. attachMediaElement(mediaElement: HTMLMediaElement): void;
  229. detachMediaElement(): void;
  230. load(): void;
  231. unload(): void;
  232. play(): Promise<void> | void;
  233. pause(): void;
  234. type: string;
  235. buffered: TimeRanges;
  236. duration: number;
  237. volume: number;
  238. muted: boolean;
  239. currentTime: number;
  240. /**
  241. * @deprecated FlvPlayer/NativePlayer have its own `mediaInfo` field.
  242. * @desc Keep it for backwards compatibility
  243. * @since 1.4
  244. */
  245. mediaInfo: NativePlayerMediaInfo | FlvPlayerMediaInfo;
  246. /**
  247. * @deprecated FlvPlayer/NativePlayer have its own `statisticsInfo` field.
  248. * @desc Keep it for backwards compatibility
  249. * @since 1.4
  250. */
  251. statisticsInfo: NativePlayerStatisticsInfo | FlvPlayerStatisticsInfo;
  252. }
  253. interface NativePlayerStatisticsInfo {
  254. playerType: 'NativePlayer';
  255. url: string;
  256. decodedFrames?: number;
  257. droppedFrames?: number;
  258. }
  259. interface FlvPlayerReportStatisticsInfo {
  260. url: string;
  261. hasRedirect: boolean;
  262. redirectedURL?: string;
  263. speed: number; // KB/s
  264. loaderType: string;
  265. currentSegmentIndex: number;
  266. totalSegmentCount: number;
  267. }
  268. interface FlvPlayerStatisticsInfo extends Partial<FlvPlayerReportStatisticsInfo> {
  269. playerType: 'FlvPlayer';
  270. decodedFrames?: number;
  271. droppedFrames?: number;
  272. }
  273. interface NativePlayerMediaInfo {
  274. mimeType: string;
  275. duration?: number;
  276. width?: number;
  277. height?: number;
  278. }
  279. interface FlvPlayerMediaInfo extends NativePlayerMediaInfo {
  280. audioCodec?: string;
  281. videoCodec?: string;
  282. audioDataRate?: number;
  283. videoDataRate?: number;
  284. hasAudio?: boolean;
  285. hasVideo?: boolean;
  286. chromaFormat?: string;
  287. fps?: number;
  288. [k: string]: any;
  289. }
  290. interface FlvPlayer extends Player {
  291. mediaInfo: FlvPlayerMediaInfo;
  292. statisticsInfo: FlvPlayerStatisticsInfo;
  293. }
  294. interface NativePlayer extends Player {
  295. mediaInfo: NativePlayerMediaInfo;
  296. statisticsInfo: NativePlayerStatisticsInfo;
  297. }
  298. interface LoggingControlConfig {
  299. forceGlobalTag: boolean;
  300. globalTag: string;
  301. enableAll: boolean;
  302. enableDebug: boolean;
  303. enableVerbose: boolean;
  304. enableInfo: boolean;
  305. enableWarn: boolean;
  306. enableError: boolean;
  307. }
  308. interface LoggingControl extends LoggingControlConfig {
  309. getConfig(): LoggingControlConfig;
  310. applyConfig(config: Partial<LoggingControlConfig>): void;
  311. addLogListener(listener: (...args: any[]) => void): void;
  312. removeLogListener(listener: (...args: any[]) => void): void;
  313. }
  314. interface Events {
  315. ERROR: string;
  316. LOADING_COMPLETE: string;
  317. RECOVERED_EARLY_EOF: string;
  318. MEDIA_INFO: string;
  319. METADATA_ARRIVED: string;
  320. SCRIPTDATA_ARRIVED: string;
  321. STATISTICS_INFO: string;
  322. }
  323. interface ErrorTypes {
  324. NETWORK_ERROR: string;
  325. MEDIA_ERROR: string;
  326. OTHER_ERROR: string;
  327. }
  328. interface ErrorDetails {
  329. NETWORK_EXCEPTION: string;
  330. NETWORK_STATUS_CODE_INVALID: string;
  331. NETWORK_TIMEOUT: string;
  332. NETWORK_UNRECOVERABLE_EARLY_EOF: string;
  333. MEDIA_MSE_ERROR: string;
  334. MEDIA_FORMAT_ERROR: string;
  335. MEDIA_FORMAT_UNSUPPORTED: string;
  336. MEDIA_CODEC_UNSUPPORTED: string;
  337. }
  338. }
  339. declare var FlvJs: {
  340. createPlayer(mediaDataSource: FlvJs.MediaDataSource, config?: FlvJs.Config): FlvJs.Player;
  341. isSupported(): boolean;
  342. getFeatureList(): FlvJs.FeatureList;
  343. /**
  344. * @deprecated Use `FlvJs.BaseLoaderConstructor` instead.
  345. * Because it's not available on `flvjs` variable.
  346. * @desc implement interface `BaseLoader`
  347. * @since 1.4
  348. */
  349. BaseLoader: FlvJs.BaseLoaderConstructor;
  350. /**
  351. * @deprecated Use `FlvJs.BaseLoaderConstructor` instead.
  352. * Because it's not available on `flvjs` variable.
  353. * @since 1.4
  354. */
  355. LoaderStatus: FlvJs.LoaderStatus;
  356. /**
  357. * @deprecated Use `FlvJs.BaseLoaderConstructor` instead.
  358. * Because it's not available on `flvjs` variable.
  359. * @since 1.4
  360. */
  361. LoaderErrors: FlvJs.LoaderErrors;
  362. readonly version: string;
  363. readonly Events: Readonly<FlvJs.Events>;
  364. readonly ErrorTypes: Readonly<FlvJs.ErrorTypes>;
  365. readonly ErrorDetails: Readonly<FlvJs.ErrorDetails>;
  366. readonly FlvPlayer: FlvJs.PlayerConstructor;
  367. readonly NativePlayer: FlvJs.PlayerConstructor;
  368. readonly LoggingControl: FlvJs.LoggingControl;
  369. };
  370. export default FlvJs;