jessibuca.d.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. declare namespace Jessibuca {
  2. /** 超时信息 */
  3. enum TIMEOUT {
  4. /** 当play()的时候,如果没有数据返回 */
  5. loadingTimeout = 'loadingTimeout',
  6. /** 当播放过程中,如果超过timeout之后没有数据渲染 */
  7. delayTimeout = 'delayTimeout',
  8. }
  9. /** 错误信息 */
  10. enum ERROR {
  11. /** 播放错误,url 为空的时候,调用 play 方法 */
  12. playError = 'playError',
  13. /** http 请求失败 */
  14. fetchError = 'fetchError',
  15. /** websocket 请求失败 */
  16. websocketError = 'websocketError',
  17. /** webcodecs 解码 h265 失败 */
  18. webcodecsH265NotSupport = 'webcodecsH265NotSupport',
  19. /** mediaSource 解码 h265 失败 */
  20. mediaSourceH265NotSupport = 'mediaSourceH265NotSupport',
  21. /** wasm 解码失败 */
  22. wasmDecodeError = 'wasmDecodeError',
  23. }
  24. interface Config {
  25. /**
  26. * 播放器容器
  27. * * 若为 string ,则底层调用的是 document.getElementById('id')
  28. * */
  29. container: HTMLElement | string;
  30. /**
  31. * 设置最大缓冲时长,单位秒,播放器会自动消除延迟
  32. */
  33. videoBuffer?: number;
  34. /**
  35. * worker地址
  36. * * 默认引用的是根目录下面的decoder.js文件 ,decoder.js 与 decoder.wasm文件必须是放在同一个目录下面。 */
  37. decoder?: string;
  38. /**
  39. * 是否不使用离屏模式(提升渲染能力)
  40. */
  41. forceNoOffscreen?: boolean;
  42. /**
  43. * 是否开启当页面的'visibilityState'变为'hidden'的时候,自动暂停播放。
  44. */
  45. hiddenAutoPause?: boolean;
  46. /**
  47. * 是否有音频,如果设置`false`,则不对音频数据解码,提升性能。
  48. */
  49. hasAudio?: boolean;
  50. /**
  51. * 设置旋转角度,只支持,0(默认),180,270 三个值
  52. */
  53. rotate?: boolean;
  54. /**
  55. * 1. 当为`true`的时候:视频画面做等比缩放后,高或宽对齐canvas区域,画面不被拉伸,但有黑边。 等同于 `setScaleMode(1)`
  56. * 2. 当为`false`的时候:视频画面完全填充canvas区域,画面会被拉伸。等同于 `setScaleMode(0)`
  57. */
  58. isResize?: boolean;
  59. /**
  60. * 1. 当为`true`的时候:视频画面做等比缩放后,完全填充canvas区域,画面不被拉伸,没有黑边,但画面显示不全。等同于 `setScaleMode(2)`
  61. */
  62. isFullResize?: boolean;
  63. /**
  64. * 1. 当为`true`的时候:ws协议不检验是否以.flv为依据,进行协议解析。
  65. */
  66. isFlv?: boolean;
  67. /**
  68. * 是否开启控制台调试打
  69. */
  70. debug?: boolean;
  71. /**
  72. * 1. 设置超时时长, 单位秒
  73. * 2. 在连接成功之前(loading)和播放中途(heart),如果超过设定时长无数据返回,则回调timeout事件
  74. */
  75. timeout?: number;
  76. /**
  77. * 1. 设置超时时长, 单位秒
  78. * 2. 在连接成功之前,如果超过设定时长无数据返回,则回调timeout事件
  79. */
  80. heartTimeout?: number;
  81. /**
  82. * 1. 设置超时时长, 单位秒
  83. * 2. 在连接成功之前,如果超过设定时长无数据返回,则回调timeout事件
  84. */
  85. loadingTimeout?: number;
  86. /**
  87. * 是否支持屏幕的双击事件,触发全屏,取消全屏事件
  88. */
  89. supportDblclickFullscreen?: boolean;
  90. /**
  91. * 是否显示网
  92. */
  93. showBandwidth?: boolean;
  94. /**
  95. * 配置操作按钮
  96. */
  97. operateBtns?: {
  98. /** 是否显示全屏按钮 */
  99. fullscreen?: boolean;
  100. /** 是否显示截图按钮 */
  101. screenshot?: boolean;
  102. /** 是否显示播放暂停按钮 */
  103. play?: boolean;
  104. /** 是否显示声音按钮 */
  105. audio?: boolean;
  106. /** 是否显示录制按 */
  107. record?: boolean;
  108. };
  109. /**
  110. * 开启屏幕常亮,在手机浏览器上, canvas标签渲染视频并不会像video标签那样保持屏幕常亮
  111. */
  112. keepScreenOn?: boolean;
  113. /**
  114. * 是否开启声音,默认是关闭声音播放的
  115. */
  116. isNotMute?: boolean;
  117. /**
  118. * 加载过程中文案
  119. */
  120. loadingText?: string;
  121. /**
  122. * 背景图片
  123. */
  124. background?: string;
  125. /**
  126. * 是否开启MediaSource硬解码
  127. * * 视频编码只支持H.264视频(Safari on iOS不支持)
  128. * * 不支持 forceNoOffscreen 为 false (开启离屏渲染)
  129. */
  130. useMSE?: boolean;
  131. /**
  132. * 是否开启Webcodecs硬解码
  133. * * 视频编码只支持H.264视频 (需在chrome 94版本以上,需要https或者localhost环境)
  134. * * 支持 forceNoOffscreen 为 false (开启离屏渲染)
  135. * */
  136. useWCS?: boolean;
  137. /**
  138. * 是否开启键盘快捷键
  139. * 目前支持的键盘快捷键有:esc -> 退出全屏;arrowUp -> 声音增加;arrowDown -> 声音减少;
  140. */
  141. hotKey?: boolean;
  142. /**
  143. * 在使用MSE或者Webcodecs 播放H265的时候,是否自动降级到wasm模式。
  144. * 设置为false 则直接关闭播放,抛出Error 异常,设置为true 则会自动切换成wasm模式播放。
  145. */
  146. autoWasm?: boolean;
  147. /**
  148. * heartTimeout 心跳超时之后自动再播放,不再抛出异常,而直接重新播放视频地址。
  149. */
  150. heartTimeoutReplay?: boolean,
  151. /**
  152. * heartTimeoutReplay 从试次数,超过之后,不再自动播放
  153. */
  154. heartTimeoutReplayTimes?: number,
  155. /**
  156. * loadingTimeout loading之后自动再播放,不再抛出异常,而直接重新播放视频地址。
  157. */
  158. loadingTimeoutReplay?: boolean,
  159. /**
  160. * heartTimeoutReplay 从试次数,超过之后,不再自动播放
  161. */
  162. loadingTimeoutReplayTimes?: number
  163. /**
  164. * wasm解码报错之后,不再抛出异常,而是直接重新播放视频地址。
  165. */
  166. wasmDecodeErrorReplay?: boolean,
  167. /**
  168. * https://github.com/langhuihui/jessibuca/issues/152 解决方案
  169. * 例如:WebGL图像预处理默认每次取4字节的数据,但是540x960分辨率下的U、V分量宽度是540/2=270不能被4整除,导致绿屏。
  170. */
  171. openWebglAlignment?: boolean,
  172. /**
  173. * webcodecs硬解码是否通过video标签渲染
  174. */
  175. wcsUseVideoRender?: boolean,
  176. /**
  177. * 底部控制台是否自动隐藏
  178. */
  179. controlAutoHide?: boolean,
  180. /**
  181. * 录制的视频格式
  182. */
  183. recordType?: 'webm' | 'mp4',
  184. /**
  185. * 是否使用web全屏(旋转90度)(只会在移动端生效)。
  186. */
  187. useWebFullScreen?: boolean,
  188. /**
  189. * 是否自动使用系统全屏
  190. */
  191. autoUseSystemFullScreen?: boolean,
  192. }
  193. }
  194. declare class Jessibuca {
  195. constructor(config?: Jessibuca.Config);
  196. /**
  197. * 是否开启控制台调试打印
  198. @example
  199. // 开启
  200. jessibuca.setDebug(true)
  201. // 关闭
  202. jessibuca.setDebug(false)
  203. */
  204. setDebug(flag: boolean): void;
  205. /**
  206. * 静音
  207. @example
  208. jessibuca.mute()
  209. */
  210. mute(): void;
  211. /**
  212. * 取消静音
  213. @example
  214. jessibuca.cancelMute()
  215. */
  216. cancelMute(): void;
  217. /**
  218. * 留给上层用户操作来触发音频恢复的方法。
  219. *
  220. * iPhone,chrome等要求自动播放时,音频必须静音,需要由一个真实的用户交互操作来恢复,不能使用代码。
  221. *
  222. * https://developers.google.com/web/updates/2017/09/autoplay-policy-changes
  223. */
  224. audioResume(): void;
  225. /**
  226. *
  227. * 设置超时时长, 单位秒
  228. * 在连接成功之前和播放中途,如果超过设定时长无数据返回,则回调timeout事件
  229. @example
  230. jessibuca.setTimeout(10)
  231. jessibuca.on('timeout',function(){
  232. //
  233. });
  234. */
  235. setTimeout(): void;
  236. /**
  237. * @param mode
  238. * 0 视频画面完全填充canvas区域,画面会被拉伸 等同于参数 `isResize` 为false
  239. *
  240. * 1 视频画面做等比缩放后,高或宽对齐canvas区域,画面不被拉伸,但有黑边 等同于参数 `isResize` 为true
  241. *
  242. * 2 视频画面做等比缩放后,完全填充canvas区域,画面不被拉伸,没有黑边,但画面显示不全 等同于参数 `isFullResize` 为true
  243. @example
  244. jessibuca.setScaleMode(0)
  245. jessibuca.setScaleMode(1)
  246. jessibuca.setScaleMode(2)
  247. */
  248. setScaleMode(mode: number): void;
  249. /**
  250. * 暂停播放
  251. *
  252. * 可以在pause 之后,再调用 `play()`方法就继续播放之前的流。
  253. @example
  254. jessibuca.pause().then(()=>{
  255. console.log('pause success')
  256. jessibuca.play().then(()=>{
  257. }).catch((e)=>{
  258. })
  259. }).catch((e)=>{
  260. console.log('pause error',e);
  261. })
  262. */
  263. pause(): Promise<void>;
  264. /**
  265. * 关闭视频,不释放底层资源
  266. @example
  267. jessibuca.close();
  268. */
  269. close(): void;
  270. /**
  271. * 关闭视频,释放底层资源
  272. @example
  273. jessibuca.destroy()
  274. */
  275. destroy(): void;
  276. /**
  277. * 清理画布为黑色背景
  278. @example
  279. jessibuca.clearView()
  280. */
  281. clearView(): void;
  282. /**
  283. * 播放视频
  284. @example
  285. jessibuca.play('url').then(()=>{
  286. console.log('play success')
  287. }).catch((e)=>{
  288. console.log('play error',e)
  289. })
  290. // 添加请求头
  291. jessibuca.play('url',{headers:{'Authorization':'test111'}}).then(()=>{
  292. console.log('play success')
  293. }).catch((e)=>{
  294. console.log('play error',e)
  295. })
  296. */
  297. play(url?: string, options?: {
  298. headers: Object
  299. }): Promise<void>;
  300. /**
  301. * 重新调整视图大小
  302. */
  303. resize(): void;
  304. /**
  305. * 设置最大缓冲时长,单位秒,播放器会自动消除延迟。
  306. *
  307. * 等同于 `videoBuffer` 参数。
  308. *
  309. @example
  310. // 设置 200ms 缓冲
  311. jessibuca.setBufferTime(0.2)
  312. */
  313. setBufferTime(time: number): void;
  314. /**
  315. * 设置旋转角度,只支持,0(默认) ,180,270 三个值。
  316. *
  317. * > 可用于实现监控画面小窗和全屏效果,由于iOS没有全屏API,此方法可以模拟页面内全屏效果而且多端效果一致。 *
  318. @example
  319. jessibuca.setRotate(0)
  320. jessibuca.setRotate(90)
  321. jessibuca.setRotate(270)
  322. */
  323. setRotate(deg: number): void;
  324. /**
  325. *
  326. * 设置音量大小,取值0 — 1
  327. *
  328. * > 区别于 mute 和 cancelMute 方法,虽然设置setVolume(0) 也能达到 mute方法,但是mute 方法是不调用底层播放音频的,能提高性能。而setVolume(0)只是把声音设置为0 ,以达到效果。
  329. * @param volume 当为0时,完全无声;当为1时,最大音量,默认值
  330. @example
  331. jessibuca.setVolume(0.2)
  332. jessibuca.setVolume(0)
  333. jessibuca.setVolume(1)
  334. */
  335. setVolume(volume: number): void;
  336. /**
  337. * 返回是否加载完毕
  338. @example
  339. var result = jessibuca.hasLoaded()
  340. console.log(result) // true
  341. */
  342. hasLoaded(): boolean;
  343. /**
  344. * 开启屏幕常亮,在手机浏览器上, canvas标签渲染视频并不会像video标签那样保持屏幕常亮。
  345. * H5目前在chrome\edge 84, android chrome 84及以上有原生亮屏API, 需要是https页面
  346. * 其余平台为模拟实现,此时为兼容实现,并不保证所有浏览器都支持
  347. @example
  348. jessibuca.setKeepScreenOn()
  349. */
  350. setKeepScreenOn(): boolean;
  351. /**
  352. * 全屏(取消全屏)播放视频
  353. @example
  354. jessibuca.setFullscreen(true)
  355. //
  356. jessibuca.setFullscreen(false)
  357. */
  358. setFullscreen(flag: boolean): void;
  359. /**
  360. *
  361. * 截图,调用后弹出下载框保存截图
  362. * @param filename 可选参数, 保存的文件名, 默认 `时间戳`
  363. * @param format 可选参数, 截图的格式,可选png或jpeg或者webp ,默认 `png`
  364. * @param quality 可选参数, 当格式是jpeg或者webp时,压缩质量,取值0 ~ 1 ,默认 `0.92`
  365. * @param type 可选参数, 可选download或者base64或者blob,默认`download`
  366. @example
  367. jessibuca.screenshot("test","png",0.5)
  368. const base64 = jessibuca.screenshot("test","png",0.5,'base64')
  369. const fileBlob = jessibuca.screenshot("test",'blob')
  370. */
  371. screenshot(filename?: string, format?: string, quality?: number, type?: string): void;
  372. /**
  373. * 开始录制。
  374. * @param fileName 可选,默认时间戳
  375. * @param fileType 可选,默认webm,支持webm 和mp4 格式
  376. @example
  377. jessibuca.startRecord('xxx','webm')
  378. */
  379. startRecord(fileName: string, fileType: string): void;
  380. /**
  381. * 暂停录制并下载。
  382. @example
  383. jessibuca.stopRecordAndSave()
  384. */
  385. stopRecordAndSave(): void;
  386. /**
  387. * 返回是否正在播放中状态。
  388. @example
  389. var result = jessibuca.isPlaying()
  390. console.log(result) // true
  391. */
  392. isPlaying(): boolean;
  393. /**
  394. * 返回是否静音。
  395. @example
  396. var result = jessibuca.isMute()
  397. console.log(result) // true
  398. */
  399. isMute(): boolean;
  400. /**
  401. * 返回是否正在录制。
  402. @example
  403. var result = jessibuca.isRecording()
  404. console.log(result) // true
  405. */
  406. isRecording(): boolean;
  407. /**
  408. * 切换底部控制条 隐藏/显示
  409. * @param isShow
  410. *
  411. * @example
  412. * jessibuca.toggleControlBar(true) // 显示
  413. * jessibuca.toggleControlBar(false) // 隐藏
  414. * jessibuca.toggleControlBar() // 切换 隐藏/显示
  415. */
  416. toggleControlBar(isShow:boolean): void;
  417. /**
  418. * 获取底部控制条是否显示
  419. */
  420. getControlBarShow(): boolean;
  421. /**
  422. * 监听 jessibuca 初始化事件
  423. * @example
  424. * jessibuca.on("load",function(){console.log('load')})
  425. */
  426. on(event: 'load', callback: () => void): void;
  427. /**
  428. * 视频播放持续时间,单位ms
  429. * @example
  430. * jessibuca.on('timeUpdate',function (ts) {console.log('timeUpdate',ts);})
  431. */
  432. on(event: 'timeUpdate', callback: () => void): void;
  433. /**
  434. * 当解析出视频信息时回调,2个回调参数
  435. * @example
  436. * jessibuca.on("videoInfo",function(data){console.log('width:',data.width,'height:',data.width)})
  437. */
  438. on(event: 'videoInfo', callback: (data: {
  439. /** 视频宽 */
  440. width: number;
  441. /** 视频高 */
  442. height: number;
  443. }) => void): void;
  444. /**
  445. * 当解析出音频信息时回调,2个回调参数
  446. * @example
  447. * jessibuca.on("audioInfo",function(data){console.log('numOfChannels:',data.numOfChannels,'sampleRate',data.sampleRate)})
  448. */
  449. on(event: 'audioInfo', callback: (data: {
  450. /** 声频通道 */
  451. numOfChannels: number;
  452. /** 采样率 */
  453. sampleRate: number;
  454. }) => void): void;
  455. /**
  456. * 信息,包含错误信息
  457. * @example
  458. * jessibuca.on("log",function(data){console.log('data:',data)})
  459. */
  460. on(event: 'log', callback: () => void): void;
  461. /**
  462. * 错误信息
  463. * @example
  464. * jessibuca.on("error",function(error){
  465. if(error === Jessibuca.ERROR.fetchError){
  466. //
  467. }
  468. else if(error === Jessibuca.ERROR.webcodecsH265NotSupport){
  469. //
  470. }
  471. console.log('error:',error)
  472. })
  473. */
  474. on(event: 'error', callback: (err: Jessibuca.ERROR) => void): void;
  475. /**
  476. * 当前网速, 单位KB 每秒1次,
  477. * @example
  478. * jessibuca.on("kBps",function(data){console.log('kBps:',data)})
  479. */
  480. on(event: 'kBps', callback: (value: number) => void): void;
  481. /**
  482. * 渲染开始
  483. * @example
  484. * jessibuca.on("start",function(){console.log('start render')})
  485. */
  486. on(event: 'start', callback: () => void): void;
  487. /**
  488. * 当设定的超时时间内无数据返回,则回调
  489. * @example
  490. * jessibuca.on("timeout",function(error){console.log('timeout:',error)})
  491. */
  492. on(event: 'timeout', callback: (error: Jessibuca.TIMEOUT) => void): void;
  493. /**
  494. * 当play()的时候,如果没有数据返回,则回调
  495. * @example
  496. * jessibuca.on("loadingTimeout",function(){console.log('timeout')})
  497. */
  498. on(event: 'loadingTimeout', callback: () => void): void;
  499. /**
  500. * 当播放过程中,如果超过timeout之后没有数据渲染,则抛出异常。
  501. * @example
  502. * jessibuca.on("delayTimeout",function(){console.log('timeout')})
  503. */
  504. on(event: 'delayTimeout', callback: () => void): void;
  505. /**
  506. * 当前是否全屏
  507. * @example
  508. * jessibuca.on("fullscreen",function(flag){console.log('is fullscreen',flag)})
  509. */
  510. on(event: 'fullscreen', callback: () => void): void;
  511. /**
  512. * 触发播放事件
  513. * @example
  514. * jessibuca.on("play",function(flag){console.log('play')})
  515. */
  516. on(event: 'play', callback: () => void): void;
  517. /**
  518. * 触发暂停事件
  519. * @example
  520. * jessibuca.on("pause",function(flag){console.log('pause')})
  521. */
  522. on(event: 'pause', callback: () => void): void;
  523. /**
  524. * 触发声音事件,返回boolean值
  525. * @example
  526. * jessibuca.on("mute",function(flag){console.log('is mute',flag)})
  527. */
  528. on(event: 'mute', callback: () => void): void;
  529. /**
  530. * 流状态统计,流开始播放后回调,每秒1次。
  531. * @example
  532. * jessibuca.on("stats",function(s){console.log("stats is",s)})
  533. */
  534. on(event: 'stats', callback: (stats: {
  535. /** 当前缓冲区时长,单位毫秒 */
  536. buf: number;
  537. /** 当前视频帧率 */
  538. fps: number;
  539. /** 当前音频码率,单位byte */
  540. abps: number;
  541. /** 当前视频码率,单位byte */
  542. vbps: number;
  543. /** 当前视频帧pts,单位毫秒 */
  544. ts: number;
  545. }) => void): void;
  546. /**
  547. * 渲染性能统计,流开始播放后回调,每秒1次。
  548. * @param performance 0: 表示卡顿,1: 表示流畅,2: 表示非常流程
  549. * @example
  550. * jessibuca.on("performance",function(performance){console.log("performance is",performance)})
  551. */
  552. on(event: 'performance', callback: (performance: 0 | 1 | 2) => void): void;
  553. /**
  554. * 录制开始的事件
  555. * @example
  556. * jessibuca.on("recordStart",function(){console.log("record start")})
  557. */
  558. on(event: 'recordStart', callback: () => void): void;
  559. /**
  560. * 录制结束的事件
  561. * @example
  562. * jessibuca.on("recordEnd",function(){console.log("record end")})
  563. */
  564. on(event: 'recordEnd', callback: () => void): void;
  565. /**
  566. * 录制的时候,返回的录制时长,1s一次
  567. * @example
  568. * jessibuca.on("recordingTimestamp",function(timestamp){console.log("recordingTimestamp is",timestamp)})
  569. */
  570. on(event: 'recordingTimestamp', callback: (timestamp: number) => void): void;
  571. /**
  572. * 监听调用play方法 经过 初始化-> 网络请求-> 解封装 -> 解码 -> 渲染 一系列过程的时间消耗
  573. * @param event
  574. * @param callback
  575. */
  576. on(event: 'playToRenderTimes', callback: (times: {
  577. playInitStart: number, // 1 初始化
  578. playStart: number, // 2 初始化
  579. streamStart: number, // 3 网络请求
  580. streamResponse: number, // 4 网络请求
  581. demuxStart: number, // 5 解封装
  582. decodeStart: number, // 6 解码
  583. videoStart: number, // 7 渲染
  584. playTimestamp: number,// playStart- playInitStart
  585. streamTimestamp: number,// streamStart - playStart
  586. streamResponseTimestamp: number,// streamResponse - streamStart
  587. demuxTimestamp: number, // demuxStart - streamResponse
  588. decodeTimestamp: number, // decodeStart - demuxStart
  589. videoTimestamp: number,// videoStart - decodeStart
  590. allTimestamp: number // videoStart - playInitStart
  591. }) => void): void
  592. /**
  593. * 监听方法
  594. *
  595. @example
  596. jessibuca.on("load",function(){console.log('load')})
  597. */
  598. on(event: string, callback: Function): void;
  599. }
  600. export default Jessibuca;