@volcengine/veplayer 1.15.1 → 1.15.2-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of @volcengine/veplayer might be problematic. Click here for more details.

Files changed (245) hide show
  1. package/.changeset/config.json +11 -0
  2. package/.changeset/curvy-yaks-smoke.md +5 -0
  3. package/.changeset/neat-eyes-search.md +5 -0
  4. package/.codebase/pipelines/create-api-doc.yaml +16 -0
  5. package/.eslintignore +5 -0
  6. package/.eslintrc.json +53 -0
  7. package/.stylelintrc.js +51 -0
  8. package/CHANGELOG.md +5665 -0
  9. package/DEV_README.md +39 -0
  10. package/LICENSE +20 -0
  11. package/README_EN.md +46 -0
  12. package/build.sh +44 -0
  13. package/docg.config.js +65 -0
  14. package/env/byteplus.js +30 -0
  15. package/env/volcengine.js +50 -0
  16. package/fixtures/favicon.ico +0 -0
  17. package/fixtures/index.base.js +59 -0
  18. package/fixtures/index.html +41 -0
  19. package/fixtures/index.js +185 -0
  20. package/libd.config.js +147 -0
  21. package/localhost+2-key.pem +28 -0
  22. package/localhost+2.pem +26 -0
  23. package/lux.config.mjs +37 -0
  24. package/package.json +123 -4
  25. package/sdkhub.config.json +10 -0
  26. package/src/@types/global.d.ts +35 -0
  27. package/src/assets/common/error.svg +3 -0
  28. package/src/assets/common/errorImg.png +0 -0
  29. package/src/assets/common/errorImg.svg +12 -0
  30. package/src/assets/icons/mobile/definition.svg +3 -0
  31. package/src/assets/icons/mobile/line.svg +3 -0
  32. package/src/assets/icons/mobile/mobileDanmu.svg +1 -0
  33. package/src/assets/icons/mobile/mobileDanmuAcitive.svg +1 -0
  34. package/src/assets/icons/mobile/mobileDanmuSetting.svg +1 -0
  35. package/src/assets/icons/mobile/mobileExitFullscreen.svg +1 -0
  36. package/src/assets/icons/mobile/mobileFullscreen.svg +1 -0
  37. package/src/assets/icons/mobile/mobilePause.svg +4 -0
  38. package/src/assets/icons/mobile/mobilePlay.svg +4 -0
  39. package/src/assets/icons/mobile/mobilePlaynext.svg +1 -0
  40. package/src/assets/icons/mobile/mobileRefresh.svg +1 -0
  41. package/src/assets/icons/mobile/more.svg +3 -0
  42. package/src/assets/icons/mobile/muted.svg +22 -0
  43. package/src/assets/icons/mobile/playbackrate.svg +3 -0
  44. package/src/assets/icons/pc/danmu.svg +1 -0
  45. package/src/assets/icons/pc/danmuActive.svg +1 -0
  46. package/src/assets/icons/pc/danmuSettings.svg +1 -0
  47. package/src/assets/icons/pc/download.svg +14 -0
  48. package/src/assets/icons/pc/exitFullscreen.svg +1 -0
  49. package/src/assets/icons/pc/exitMirror.svg +10 -0
  50. package/src/assets/icons/pc/extend.svg +1 -0
  51. package/src/assets/icons/pc/fullscreen.svg +1 -0
  52. package/src/assets/icons/pc/getMirror.svg +10 -0
  53. package/src/assets/icons/pc/list.svg +20 -0
  54. package/src/assets/icons/pc/noPoster.svg +119 -0
  55. package/src/assets/icons/pc/pause.svg +1 -0
  56. package/src/assets/icons/pc/pip.svg +1 -0
  57. package/src/assets/icons/pc/pipExit.svg +1 -0
  58. package/src/assets/icons/pc/play-next-btn.svg +4 -0
  59. package/src/assets/icons/pc/play.svg +1 -0
  60. package/src/assets/icons/pc/playNext.svg +1 -0
  61. package/src/assets/icons/pc/playerLoading.svg +1 -0
  62. package/src/assets/icons/pc/refresh.svg +1 -0
  63. package/src/assets/icons/pc/replay.svg +1 -0
  64. package/src/assets/icons/pc/reset.svg +6 -0
  65. package/src/assets/icons/pc/startPlay.svg +1 -0
  66. package/src/assets/icons/pc/subtitleclose.svg +5 -0
  67. package/src/assets/icons/pc/subtitleopen.svg +3 -0
  68. package/src/assets/icons/pc/theaterEnter.svg +1 -0
  69. package/src/assets/icons/pc/theaterExit.svg +1 -0
  70. package/src/assets/icons/pc/volumeLarge.svg +1 -0
  71. package/src/assets/icons/pc/volumeMuted.svg +1 -0
  72. package/src/assets/icons/pc/volumeSmall.svg +1 -0
  73. package/src/config/defaultPreset.ts +110 -0
  74. package/src/config/playerOptionMobile.ts +166 -0
  75. package/src/config/playerOptionPc.ts +147 -0
  76. package/src/config/playerPreset.ts +242 -0
  77. package/src/constants/api.ts +13 -0
  78. package/src/constants/event.ts +577 -0
  79. package/src/constants/player.ts +84 -0
  80. package/src/constants/plugin.ts +19 -0
  81. package/src/constants/umdMap.ts +137 -0
  82. package/src/constants/umdPlugins.json +68 -0
  83. package/src/core/index.ts +7 -0
  84. package/src/core/player.ts +2713 -0
  85. package/src/core/playerData.ts +812 -0
  86. package/src/env.d.ts +47 -0
  87. package/src/index.ts +24 -0
  88. package/src/index.umd.ts +54 -0
  89. package/src/interface/adaptRange.d.ts +33 -0
  90. package/src/interface/api.ts +132 -0
  91. package/src/interface/autoBitrate.d.ts +41 -0
  92. package/src/interface/index.ts +1685 -0
  93. package/src/interface/rtm.ts +56 -0
  94. package/src/interface/sdkErrorPlugin.ts +145 -0
  95. package/src/interface/subtitle.ts +381 -0
  96. package/src/interface/video.ts +107 -0
  97. package/src/interface/xgplayer.ts +748 -0
  98. package/src/lang/constants.ts +69 -0
  99. package/src/lang/en.ts +98 -0
  100. package/src/lang/index.ts +33 -0
  101. package/src/lang/jp.ts +100 -0
  102. package/src/lang/zh-hk.ts +80 -0
  103. package/src/lang/zh.ts +79 -0
  104. package/src/license/index.ts +315 -0
  105. package/src/license/ttlicense2.js +15 -0
  106. package/src/license/ttlicense2.wasm +0 -0
  107. package/src/music/icons/back.svg +3 -0
  108. package/src/music/icons/forward.svg +3 -0
  109. package/src/music/icons/loop.svg +10 -0
  110. package/src/music/icons/order.svg +10 -0
  111. package/src/music/icons/pause-circle.svg +23 -0
  112. package/src/music/icons/play-circle.svg +22 -0
  113. package/src/music/icons/random.svg +10 -0
  114. package/src/music/icons/sloop.svg +10 -0
  115. package/src/music/index.ts +5 -0
  116. package/src/music/music.ts +550 -0
  117. package/src/music/plugins/index.less +185 -0
  118. package/src/music/plugins/index.ts +6 -0
  119. package/src/music/plugins/musicBackward.ts +82 -0
  120. package/src/music/plugins/musicCover.ts +45 -0
  121. package/src/music/plugins/musicForward.ts +82 -0
  122. package/src/music/plugins/musicMeta.ts +32 -0
  123. package/src/music/plugins/musicMode.ts +152 -0
  124. package/src/music/plugins/musicNext.ts +93 -0
  125. package/src/music/plugins/musicPrev.ts +94 -0
  126. package/src/music/preset.ts +69 -0
  127. package/src/music/xhr.ts +37 -0
  128. package/src/plugins/common/extendPluginFactory.ts +132 -0
  129. package/src/plugins/common/mobilePlayerPanel.ts +253 -0
  130. package/src/plugins/external/LiveInfoPanel.ts +340 -0
  131. package/src/plugins/external/ad/adsPlugin.ts +1 -0
  132. package/src/plugins/external/aiSubtitleIconPlugin.ts +270 -0
  133. package/src/plugins/external/aiSubtitlePlugin.ts +452 -0
  134. package/src/plugins/external/definitionDemotePlugin.ts +591 -0
  135. package/src/plugins/external/memoryPlay.ts +247 -0
  136. package/src/plugins/external/mirrorPlugin.ts +141 -0
  137. package/src/plugins/external/playList/OptionList.ts +204 -0
  138. package/src/plugins/external/playList/index.ts +743 -0
  139. package/src/plugins/external/subtitle/index.ts +672 -0
  140. package/src/plugins/external/subtitle/nativeSubTitle.ts +115 -0
  141. package/src/plugins/external/timeShiftPlugin.ts +484 -0
  142. package/src/plugins/external/watermark/dynamicWatermark.less +13 -0
  143. package/src/plugins/external/watermark/dynamicWatermark.ts +449 -0
  144. package/src/plugins/external/watermark/dynamicWatermarkPlugin.ts +185 -0
  145. package/src/plugins/inner/common/autoplayPlugin.ts +435 -0
  146. package/src/plugins/inner/common/danmu/container.ts +120 -0
  147. package/src/plugins/inner/common/danmu/index.ts +683 -0
  148. package/src/plugins/inner/common/danmu/lang.ts +139 -0
  149. package/src/plugins/inner/common/danmu/panel.ts +20 -0
  150. package/src/plugins/inner/common/danmu/slider.ts +210 -0
  151. package/src/plugins/inner/common/danmu/state.ts +118 -0
  152. package/src/plugins/inner/common/danmu/switch.ts +74 -0
  153. package/src/plugins/inner/common/definitionBasePlugin.ts +353 -0
  154. package/src/plugins/inner/common/errorPlugin.ts +544 -0
  155. package/src/plugins/inner/common/liveLogger.ts +137 -0
  156. package/src/plugins/inner/common/poster/index.less +66 -0
  157. package/src/plugins/inner/common/poster/index.ts +178 -0
  158. package/src/plugins/inner/common/refreshPlugin.ts +88 -0
  159. package/src/plugins/inner/common/rtmPlugin.ts +62 -0
  160. package/src/plugins/inner/common/toastPlugin.ts +90 -0
  161. package/src/plugins/inner/common/unmutePlugin.ts +133 -0
  162. package/src/plugins/inner/common/vodLogger.ts +80 -0
  163. package/src/plugins/inner/mobile/DefinitionMobilePlugin.ts +217 -0
  164. package/src/plugins/inner/mobile/LineMobilePlugins.ts +169 -0
  165. package/src/plugins/inner/mobile/MoreButtonPlugin.ts +176 -0
  166. package/src/plugins/inner/mobile/PlaybackRatePlugin.ts +199 -0
  167. package/src/plugins/inner/pc/definitionPlugin.ts +203 -0
  168. package/src/plugins/inner/pc/multilinePlugin.ts +93 -0
  169. package/src/sdkPlugin/abr.ts +67 -0
  170. package/src/sdkPlugin/adaptRange.ts +49 -0
  171. package/src/sdkPlugin/authToken.ts +557 -0
  172. package/src/sdkPlugin/sdkPlugin.ts +125 -0
  173. package/src/sdkPlugin/sdkPluginManager.ts +157 -0
  174. package/src/sdkPlugin/strategy.ts +47 -0
  175. package/src/strategy/index.ts +740 -0
  176. package/src/strategy/vestrategy-h265-wrapper.ts +34 -0
  177. package/src/strategy/vestrategy-preload-wrapper.ts +414 -0
  178. package/src/streamAdapters/base.ts +89 -0
  179. package/src/streamAdapters/dash.ts +230 -0
  180. package/src/streamAdapters/default.ts +53 -0
  181. package/src/streamAdapters/hls.ts +278 -0
  182. package/src/streamAdapters/index.ts +40 -0
  183. package/src/streamAdapters/mp4.ts +214 -0
  184. package/src/style/bytelive/danmu.less +293 -0
  185. package/src/style/bytelive/definitionIcon.less +80 -0
  186. package/src/style/bytelive/error.less +165 -0
  187. package/src/style/bytelive/index.less +62 -0
  188. package/src/style/bytelive/loading.less +5 -0
  189. package/src/style/bytelive/mobiePlugin.less +2 -0
  190. package/src/style/bytelive/mobile.less +76 -0
  191. package/src/style/bytelive/moreButton.less +79 -0
  192. package/src/style/bytelive/panel.less +259 -0
  193. package/src/style/bytelive/pc.less +161 -0
  194. package/src/style/bytelive/refresh.less +3 -0
  195. package/src/style/bytelive/reset.less +4 -0
  196. package/src/style/bytelive/toast.less +61 -0
  197. package/src/style/bytelive/unmute.less +65 -0
  198. package/src/style/external/LiveInfoPanel.less +41 -0
  199. package/src/style/external/aisub.less +139 -0
  200. package/src/style/external/aisubIcon.less +25 -0
  201. package/src/style/external/index.less +5 -0
  202. package/src/style/external/larkWindow.less +36 -0
  203. package/src/style/external/mirror.less +27 -0
  204. package/src/style/external/playList.less +258 -0
  205. package/src/style/external/timeShift.less +102 -0
  206. package/src/style/external/vttSubtitle.less +25 -0
  207. package/src/utils/debug.ts +62 -0
  208. package/src/utils/definition.ts +61 -0
  209. package/src/utils/escapeHtml.ts +93 -0
  210. package/src/utils/eventMiddleWare.ts +108 -0
  211. package/src/utils/index.ts +621 -0
  212. package/src/utils/intervalTimer.ts +38 -0
  213. package/src/utils/isHijackBrowser.ts +71 -0
  214. package/src/utils/proxy.ts +139 -0
  215. package/src/utils/storage.ts +34 -0
  216. package/src/utils/time.ts +19 -0
  217. package/src/utils/toast/index.less +20 -0
  218. package/src/utils/toast/index.ts +21 -0
  219. package/src/utils/token.ts +395 -0
  220. package/src/utils/u8a.ts +4 -0
  221. package/src/utils/umdLoader.ts +193 -0
  222. package/src/utils/video.ts +43 -0
  223. package/src/utils/xhr.ts +160 -0
  224. package/src/veError/error.ts +301 -0
  225. package/src/veError/index.ts +681 -0
  226. package/src/veError/playerProxy.ts +69 -0
  227. package/tsconfig.json +27 -0
  228. package/index.d.ts +0 -6874
  229. package/index.min.css +0 -1
  230. package/index.min.js +0 -2
  231. package/plugin/DashAbralgo.js +0 -2
  232. package/plugin/XGVideo.js +0 -2
  233. package/plugin/danmuMask.js +0 -2
  234. package/plugin/danmujs.js +0 -3
  235. package/plugin/dash.js +0 -2
  236. package/plugin/flv.js +0 -2
  237. package/plugin/hls.js +0 -2
  238. package/plugin/hlsEncrypt.js +0 -2
  239. package/plugin/mp4Encrypt.js +0 -2
  240. package/plugin/preloader.js +0 -2
  241. package/plugin/streamprobe.js +0 -2
  242. package/plugin/vestrategy.js +0 -1
  243. package/plugin/vestrategy_adapt_range.js +0 -1
  244. package/plugin/vestrategy_h265.js +0 -1
  245. package/plugin/vestrategy_preload.js +0 -1
@@ -0,0 +1,270 @@
1
+ import { Plugin, Util, Sniffer } from 'xgplayer';
2
+ import OpenIcon from '../../assets/icons/pc/subtitleopen.svg';
3
+ import CloseIcon from '../../assets/icons/pc/subtitleclose.svg';
4
+ import OptionList from 'xgplayer/es/plugins/common/optionList';
5
+ import LiveSubTitlesPlugin from './aiSubtitlePlugin';
6
+
7
+ const { POSITIONS } = Plugin;
8
+
9
+ export default class LiveSubtitlesIconPlugin extends Plugin {
10
+ static get pluginName() {
11
+ return 'LiveSubtitlesIconPlugin';
12
+ }
13
+
14
+ static get defaultConfig() {
15
+ return {
16
+ position: POSITIONS.CONTROLS_RIGHT,
17
+ index: 2.7,
18
+ defaultOpen: true,
19
+ default: 'en',
20
+ list: [
21
+ {
22
+ key: 'zh',
23
+ showText: '中文',
24
+ },
25
+ {
26
+ key: 'en',
27
+ showText: 'En',
28
+ },
29
+ ],
30
+ };
31
+ }
32
+
33
+ public isMobile = Sniffer.device === 'mobile';
34
+ public isOpenState = true;
35
+ public optionList: any = null;
36
+ public currentLang = '';
37
+
38
+ constructor(configs: any) {
39
+ super(configs);
40
+ }
41
+
42
+ afterCreate() {
43
+ this.appendChild('.xgplayer-icon', (this.icons as any).aiSubOpen);
44
+ this.appendChild('.xgplayer-icon', (this.icons as any).aiSubClose);
45
+ this.renderOptionList();
46
+ this.initEvents();
47
+
48
+ this.currentLang = this.config.default;
49
+
50
+ if (this.config.defaultOpen) {
51
+ this.open(true);
52
+ } else {
53
+ this.close();
54
+ }
55
+ }
56
+
57
+ afterPlayerInit() {
58
+ // 播放器调用start初始化播放源之后的逻辑
59
+ if (this.config.defaultOpen) {
60
+ this.changeSubTitleLang(this.config.default, true);
61
+ }
62
+ }
63
+
64
+ registerIcons() {
65
+ return {
66
+ aiSubOpen: {
67
+ icon: OpenIcon,
68
+ class: 'xgplayer-aisub-open-svg',
69
+ },
70
+ aiSubClose: {
71
+ icon: CloseIcon,
72
+ class: 'xgplayer-aisub-close-svg',
73
+ },
74
+ };
75
+ }
76
+
77
+ registerLanguageTexts() {
78
+ return {
79
+ sub: {
80
+ jp: 'AI 字幕',
81
+ en: 'AI Subtitles',
82
+ zh: 'AI 字幕',
83
+ 'zh-hk': 'AI 字幕',
84
+ },
85
+ subOpen: {
86
+ jp: '字幕をオンにする',
87
+ en: 'Show subtitles',
88
+ zh: '开启字幕',
89
+ 'zh-hk': '開啟字幕',
90
+ },
91
+ subClose: {
92
+ jp: 'クローズドキャプション',
93
+ en: 'Hide subtitles',
94
+ zh: '关闭字幕',
95
+ 'zh-hk': '關閉字幕',
96
+ },
97
+ };
98
+ }
99
+
100
+ children() {
101
+ return {
102
+ LiveSubTitlesPlugin: {
103
+ plugin: LiveSubTitlesPlugin,
104
+ options: {
105
+ root: this.player.root,
106
+ showLang: this.currentLang,
107
+ },
108
+ },
109
+ };
110
+ }
111
+
112
+ initEvents() {
113
+ this.bind(['click'], this.handleButtonChange.bind(this));
114
+ this.bind(['mouseenter'], this.showPanel.bind(this));
115
+ this.bind(['mouseleave'], this.hidePanel.bind(this));
116
+ }
117
+
118
+ handleButtonChange() {
119
+ this.close = this.close.bind(this);
120
+ this.open = this.open.bind(this);
121
+
122
+ if (this.isMobile) {
123
+ this.showMobilePanel();
124
+ } else {
125
+ const fn = this.isOpenState ? this.close : this.open;
126
+ fn();
127
+ }
128
+ }
129
+
130
+ showMobilePanel() {
131
+ const $content = this.renderItemList();
132
+ (this.player as any).panel?.showPanel(
133
+ $content,
134
+ (this.langText as any).sub,
135
+ true,
136
+ );
137
+ }
138
+
139
+ renderItemList() {
140
+ const list = [...this.config.list];
141
+ const $content = Util.createDom('div', '', {}, 'ai-panel-content');
142
+
143
+ list.push({
144
+ key: '',
145
+ showText: (this.langText as any).subClose,
146
+ });
147
+
148
+ list.forEach((item: any) => {
149
+ const $item = this.renderItem(item);
150
+ $content.appendChild($item);
151
+ });
152
+
153
+ return $content;
154
+ }
155
+
156
+ renderItem(item: any) {
157
+ const isSelect =
158
+ (item.key === '' && !this.isOpenState) ||
159
+ (this.currentLang === item.key && this.isOpenState);
160
+
161
+ const $item = Util.createDom(
162
+ 'div',
163
+ '',
164
+ {},
165
+ `panel-Item ${isSelect ? 'select-in' : ''}`,
166
+ );
167
+
168
+ $item.innerHTML = item.showText;
169
+ $item.addEventListener('click', e => {
170
+ e.stopPropagation();
171
+ e.preventDefault();
172
+ this.changeSubTitleLang(item.key);
173
+ this.updatePanel();
174
+ });
175
+
176
+ return $item;
177
+ }
178
+
179
+ updatePanel() {
180
+ const children = this.renderItemList();
181
+ (this.player as any).panel?.updatePanel(children);
182
+ }
183
+
184
+ showPanel() {
185
+ !this.isMobile && this.isOpenState && this.optionList?.show();
186
+ }
187
+
188
+ hidePanel() {
189
+ !this.isMobile && this.isOpenState && this.optionList?.hide();
190
+ }
191
+
192
+ open(isInit = false) {
193
+ this.isOpenState = true;
194
+ Util.removeClass(this.root, 'isClose');
195
+ Util.addClass(this.root, 'isOpen');
196
+ !isInit && this.showPanel();
197
+ this.toggleSubTitle();
198
+ }
199
+
200
+ close() {
201
+ this.isOpenState = false;
202
+ Util.removeClass(this.root, 'isOpen');
203
+ Util.addClass(this.root, 'isClose');
204
+ this.optionList?.hide();
205
+ this.toggleSubTitle();
206
+ // this.hidePanel();
207
+ }
208
+
209
+ renderOptionList() {
210
+ if (this.config.list?.length < 2 || this.isMobile) {
211
+ return;
212
+ }
213
+
214
+ const list = this.config.list.map((item: any) => {
215
+ if (item.key === this.config.default) {
216
+ item.isCurrent = true;
217
+ item.selected = true;
218
+ }
219
+ return item;
220
+ });
221
+ const options = {
222
+ config: {
223
+ data: list,
224
+ className: '',
225
+ onItemClick: (e: any, data: any) => {
226
+ e.stopPropagation();
227
+ e.preventDefault();
228
+ this.changeSubTitleLang(data.to?.key);
229
+ },
230
+ },
231
+ root: this.root,
232
+ };
233
+
234
+ this.optionList = new OptionList(options);
235
+ }
236
+
237
+ changeSubTitleLang(key: string, isInit = false) {
238
+ this.currentLang = key;
239
+ if (!key) {
240
+ this.close();
241
+ } else {
242
+ this.open(isInit);
243
+ }
244
+ }
245
+
246
+ toggleSubTitle() {
247
+ this.player
248
+ .getPlugin('LiveSubtitlesPlugin')
249
+ ?.setCurrentLang(this.isOpenState ? this.currentLang : '');
250
+ }
251
+
252
+ destroy() {
253
+ this.unbind(['click'], this.handleButtonChange.bind(this));
254
+ this.unbind(['mouseenter'], this.showPanel.bind(this));
255
+ this.unbind(['mouseleave'], this.hidePanel.bind(this));
256
+ this.optionList?.destroy();
257
+ }
258
+
259
+ render() {
260
+ return `
261
+ <xg-icon class="xgplayer-aisub">
262
+ <div class="xgplayer-icon">
263
+ </div>
264
+ <div class="xg-tips" lang-key="subOpen">${
265
+ (this.langText as any).subOpen
266
+ }</div>
267
+ </xg-icon>
268
+ `;
269
+ }
270
+ }
@@ -0,0 +1,452 @@
1
+ import { PluginEvents } from '@/constants/event';
2
+ import { Uint8ArrayToString } from '@/utils/u8a';
3
+ import { Plugin, Events } from 'xgplayer';
4
+ import { VE_DEBUG } from '@/utils';
5
+
6
+ interface ISubTitles {
7
+ timestamp: number;
8
+ text: string;
9
+ duration: number;
10
+ url: string;
11
+ lang: string;
12
+ delimiter?: string;
13
+ textArr?: string[];
14
+ wordpiece?: any[];
15
+ }
16
+
17
+ interface ISubTitlesState {
18
+ showLang: string;
19
+ subTitleList: ISubTitles[];
20
+ rtmpDtsGap: number;
21
+ showingSubtitle: string;
22
+ currentSub: any;
23
+ dtsBase: number;
24
+ }
25
+
26
+ class LiveSubTitlesPlugin extends Plugin {
27
+ static get pluginName() {
28
+ return 'LiveSubtitlesPlugin';
29
+ }
30
+
31
+ static get defaultConfig() {
32
+ return {
33
+ position: Plugin.POSITIONS.ROOT,
34
+ showLang: '',
35
+ };
36
+ }
37
+
38
+ constructor(configs: any) {
39
+ super(configs);
40
+ this.state.showLang = configs.showLang;
41
+ }
42
+
43
+ public state: ISubTitlesState = {
44
+ showLang: '',
45
+ subTitleList: [],
46
+ rtmpDtsGap: 0,
47
+ dtsBase: 0,
48
+ showingSubtitle: '',
49
+ currentSub: null,
50
+ };
51
+
52
+ public clearLineTimer: any = null;
53
+ public showTimer: any = null;
54
+
55
+ beforePlayerInit() {
56
+ this.on(Events.TIME_UPDATE, this.handleVideoTimeupdate.bind(this));
57
+ this.on(Events.PAUSE, this.onPause.bind(this));
58
+ this.on(Events.PLAY, this.onPlay.bind(this));
59
+ this.on(Events.DESTROY, this.destroy.bind(this));
60
+ this.on(Events.URL_CHANGE, this.clearRtmpDtsGap.bind(this));
61
+
62
+ this.on(Events.SEI_PARSED, this.handleSEIParsed.bind(this));
63
+ // rc版本监听
64
+ this.on('core_event', (data: any) => {
65
+ // 通过判断eventName来区分内核事件
66
+ if (data.eventName === 'core.sei') {
67
+ this.handleSEIParsed(data.sei, data.originPts);
68
+ }
69
+ });
70
+ }
71
+
72
+ onPause() {
73
+ this.clearSubTitle(false);
74
+ }
75
+
76
+ onPlay() {
77
+ this.clearSubTitle(true);
78
+ }
79
+
80
+ destroy() {
81
+ this.clearSubTitle(true);
82
+ this.off(Events.TIME_UPDATE, this.handleVideoTimeupdate.bind(this));
83
+ this.off(Events.SEI_PARSED, this.handleSEIParsed.bind(this));
84
+ this.off(Events.PAUSE, this.onPause.bind(this));
85
+ this.off(Events.PLAY, this.onPlay.bind(this));
86
+ this.off(Events.DESTROY, this.destroy.bind(this));
87
+ this.off(Events.URL_CHANGE, this.clearRtmpDtsGap.bind(this));
88
+ }
89
+
90
+ clearRtmpDtsGap() {
91
+ this.state.rtmpDtsGap = 0;
92
+ this.state.dtsBase = 0;
93
+ this.clearSubTitle(true);
94
+ }
95
+
96
+ clearSubTitle(isClearScreen: boolean) {
97
+ this.state.subTitleList = [];
98
+ this.state.showingSubtitle = '';
99
+ this.state.currentSub = null;
100
+ clearTimeout(this.showTimer);
101
+ clearTimeout(this.clearLineTimer);
102
+ if (isClearScreen) {
103
+ const $contianer = this.find('.xgplayer-subtitles-inner');
104
+ if ($contianer) {
105
+ this.resetDom($contianer);
106
+ }
107
+ }
108
+ }
109
+
110
+ getIsSoft() {
111
+ const { tagName } = (this.player.video as any) ?? {};
112
+ const isSoft = tagName === 'LIVE-VIDEO';
113
+ return isSoft;
114
+ }
115
+
116
+ handleVideoTimeupdate(data: any) {
117
+ const currentTime = (this.player.video as any).currentTime;
118
+ const isSoft = this.getIsSoft();
119
+ const curPts = data?.detail?.pts / 1000;
120
+ const curTime = isSoft ? curPts : currentTime;
121
+
122
+ if (!this.state.showLang) return;
123
+ if (!curTime) return;
124
+ if (!this.player.isPlaying) return;
125
+
126
+ const subTitle = this.findSubTitleByTime(curTime);
127
+
128
+ if (!subTitle) return;
129
+ // 重新匹配到的字幕是正在展示的
130
+ if (subTitle.text === this.state.showingSubtitle) return;
131
+ if (!subTitle.wordpiece?.length) return;
132
+
133
+ this.renderSubtitle(subTitle);
134
+ this.player.emit(PluginEvents.AI_SUBTITLE_MATCH, {
135
+ subTitle,
136
+ lang: this.state.showLang,
137
+ });
138
+
139
+ if (this.state.subTitleList.length > 1e3) {
140
+ this.state.subTitleList = this.state.subTitleList.filter(
141
+ (subTitle: ISubTitles) =>
142
+ subTitle.timestamp + subTitle.duration / 1000 >= curTime,
143
+ );
144
+ }
145
+ }
146
+
147
+ renderSubtitle(subTitle: ISubTitles) {
148
+ const $subContianer: any = this.root;
149
+ const $contianer: any = this.find('.xgplayer-subtitles-inner');
150
+ const $helper: any = this.find('.xgplayer-subtitles-helper');
151
+ if (!$contianer) return;
152
+
153
+ if (this.clearLineTimer) {
154
+ clearTimeout(this.clearLineTimer);
155
+ }
156
+ // 如果上一句未展示完,则把剩下的先展示完
157
+ if (this.showTimer) {
158
+ clearTimeout(this.showTimer);
159
+ this.showSurplus();
160
+ }
161
+
162
+ let subTitleDom: any = this.createNewDom($contianer);
163
+ this.state.showingSubtitle = subTitle.text;
164
+ this.state.currentSub = {
165
+ subObj: subTitle,
166
+ dom: subTitleDom,
167
+ index: 0,
168
+ };
169
+
170
+ const start = Date.now();
171
+ let textContent = '';
172
+ let i = 0;
173
+
174
+ const recursion = () => {
175
+ const { wordpiece = [], delimiter } = subTitle;
176
+ // 最后一个字
177
+ if (i === wordpiece.length) {
178
+ textContent = '';
179
+ // 轮训展示完成
180
+ this.showTimer = null;
181
+ // 结束后一秒清空字幕容器
182
+ this.clearLineTimer = setTimeout(() => {
183
+ this.resetDom($contianer);
184
+ this.clearLineTimer = null;
185
+ }, 1 * 1000);
186
+ return;
187
+ }
188
+
189
+ // 下一个字的剩余展示时长
190
+ let delayTime =
191
+ wordpiece[i].duration - (Date.now() - start - wordpiece[i].OffsetMs);
192
+ if (delayTime < 0) {
193
+ // 如果不足,则往后补足一个字
194
+ while (delayTime < 0 && i < wordpiece.length - 1) {
195
+ i = i + 1;
196
+ textContent = textContent + delimiter + wordpiece[i].text;
197
+ delayTime =
198
+ wordpiece[i].duration -
199
+ (Date.now() - start - wordpiece[i].OffsetMs);
200
+ }
201
+ } else {
202
+ textContent = textContent + delimiter + wordpiece[i].text;
203
+ }
204
+
205
+ // 探测该行有没有超出
206
+ $helper.textContent = textContent;
207
+ const subContianerWidth = $subContianer?.offsetWidth ?? 300;
208
+ if ($helper.offsetWidth > subContianerWidth) {
209
+ // 记录当前dom
210
+ subTitleDom = this.createNewDom($contianer);
211
+ this.state.currentSub.dom = subTitleDom;
212
+ textContent = wordpiece[i].text;
213
+ }
214
+
215
+ subTitleDom.textContent = textContent;
216
+ this.showTimer = setTimeout(recursion, delayTime);
217
+ i = i + 1;
218
+ // 记录当前展示至第i个字
219
+ this.state.currentSub.index = i;
220
+ };
221
+ recursion();
222
+ }
223
+
224
+ handleSEIParsed(sei: any, originPts?) {
225
+ const { dts, code } = sei;
226
+ if (code !== 5) return;
227
+ try {
228
+ const content =
229
+ typeof sei.content === 'string'
230
+ ? sei.content
231
+ : Uint8ArrayToString(sei.content);
232
+ const seiObj = JSON.parse(content);
233
+ const { rtmp_dts: rtmpDts } = seiObj;
234
+ if (rtmpDts && dts) {
235
+ const realDts = originPts ? originPts : dts;
236
+ this.state.rtmpDtsGap = rtmpDts - realDts;
237
+ }
238
+ } catch (e) {
239
+ // DO NOTHING
240
+ }
241
+ }
242
+
243
+ insertSubTitle(...subTitles: ISubTitles[]) {
244
+ subTitles.forEach(subTitle => {
245
+ subTitle.timestamp = this.adjustSubTitleTime(subTitle.timestamp);
246
+ });
247
+
248
+ this.state.subTitleList.push(...subTitles);
249
+ }
250
+
251
+ setCurrentLang(lang: string) {
252
+ this.state.showLang = lang;
253
+
254
+ // 通知外部AI字幕语言改变
255
+ this.player.emit(PluginEvents.AI_SUBTITLE_LANG_CHANGE, lang);
256
+ }
257
+
258
+ adjustSubTitleTime(time: number) {
259
+ const isSoft = this.getIsSoft();
260
+ if (isSoft) {
261
+ return (time - this.state.rtmpDtsGap) / 1000;
262
+ }
263
+
264
+ //compat alpha版本
265
+ const coreAlpha =
266
+ this.player?.plugins?.flvlive || this.player?.plugins?.hlslive;
267
+ if (coreAlpha) {
268
+ const videoLargeGap =
269
+ coreAlpha?._context?.getInstance('COMPATIBILITY')?._videoLargeGap ?? 0;
270
+
271
+ const remuxer = coreAlpha?._context?.getInstance('MP4_REMUXER');
272
+
273
+ //compat alpha.120以下版本
274
+ const videoDtsBase =
275
+ remuxer.remuxer?.videoDtsBase ?? remuxer?.videoDtsBase ?? 0;
276
+
277
+ this.state.dtsBase = videoDtsBase - videoLargeGap;
278
+
279
+ return (
280
+ (time + videoLargeGap - videoDtsBase - this.state.rtmpDtsGap) / 1000
281
+ );
282
+ }
283
+
284
+ //compat next播放器版本
285
+ const coreRc = this.player?.plugins?.flv || this.player?.plugins?.hls;
286
+ if (coreRc) {
287
+ const videoDtsBase =
288
+ coreRc.core._bufferService?._demuxer?._fixer?._baseDts;
289
+ this.state.dtsBase = videoDtsBase;
290
+ return (time - videoDtsBase - this.state.rtmpDtsGap) / 1000;
291
+ }
292
+
293
+ return (time - this.state.rtmpDtsGap) / 1000;
294
+ }
295
+
296
+ findSubTitleByTime(currentTime: number) {
297
+ const subTitleList = this.state.subTitleList;
298
+ const subIndex = subTitleList.findIndex(subTitle => {
299
+ const player: any = this.player;
300
+ const langMatch = subTitle.lang === this.state.showLang;
301
+ const urlMatch = (player?.__curUrl || player.config.url) === subTitle.url;
302
+
303
+ const { timestamp, duration } = subTitle;
304
+ // console.log(timestamp + duration / 1000, currentTime, timestamp);
305
+ const dtsMatch =
306
+ timestamp + duration / 1000 >= currentTime && timestamp <= currentTime;
307
+ // console.log(dtsMatch, urlMatch, langMatch);
308
+ return dtsMatch && urlMatch && langMatch;
309
+ });
310
+
311
+ if (subIndex < 0) {
312
+ return;
313
+ }
314
+
315
+ // 移出该字幕之前的字幕,须保证字幕是按时间顺序排序的
316
+ const deleteList = subTitleList.splice(0, subIndex + 1);
317
+ const matchSubTitle = deleteList[deleteList.length - 1];
318
+
319
+ const subTitle = this.splitSubTitle(matchSubTitle);
320
+ return subTitle;
321
+ }
322
+
323
+ splitSubTitle(subTitle: ISubTitles) {
324
+ const delimiter = subTitle.lang === 'en' ? ' ' : '';
325
+ const textArr = subTitle.text.split(delimiter);
326
+ if (!subTitle.wordpiece) {
327
+ const perTime = subTitle.duration / textArr.length;
328
+ const wordpiece = textArr.map((txt, i) => ({
329
+ text: txt,
330
+ OffsetMs: perTime * i,
331
+ duration: perTime,
332
+ }));
333
+
334
+ return {
335
+ ...subTitle,
336
+ delimiter,
337
+ textArr,
338
+ wordpiece,
339
+ };
340
+ }
341
+
342
+ return {
343
+ ...subTitle,
344
+ delimiter,
345
+ textArr,
346
+ };
347
+ }
348
+
349
+ showSurplus() {
350
+ if (!this.state.currentSub) {
351
+ return;
352
+ }
353
+ const { index, subObj, dom } = this.state.currentSub;
354
+ const { wordpiece, delimiter } = subObj ?? {};
355
+ if (index >= wordpiece.length) {
356
+ return;
357
+ }
358
+ const $helper: any = this.find('.xgplayer-subtitles-helper');
359
+ const $contianer: any = this.find('.xgplayer-subtitles-inner');
360
+ const $subContianer: any = this.root;
361
+ const subContianerWidth = $subContianer?.offsetWidth ?? 300;
362
+ const end = wordpiece.length;
363
+ let textContent = dom.textContent;
364
+ wordpiece.slice(index, end).forEach((item: any) => {
365
+ textContent = textContent + delimiter + item.text;
366
+ $helper.textContent = textContent;
367
+
368
+ if ($helper.offsetWidth > subContianerWidth) {
369
+ this.state.currentSub.dom = this.createNewDom($contianer);
370
+ textContent = item.text;
371
+ }
372
+ this.state.currentSub.dom.textContent = textContent;
373
+ });
374
+ }
375
+
376
+ createNewDom(parent: HTMLElement) {
377
+ if (!parent) return;
378
+
379
+ if (parent.children.length > 2 && parent.firstElementChild) {
380
+ parent.removeChild(parent.firstElementChild);
381
+ }
382
+ const dom = document.createElement('span');
383
+ parent.appendChild(dom);
384
+ if (parent.children.length > 2) {
385
+ const contains1 = parent.classList.contains('scroll-1');
386
+ const contains2 = parent.classList.contains('scroll-2');
387
+ if (contains1) {
388
+ parent.classList.replace('scroll-1', 'scroll-2');
389
+ }
390
+ if (contains2) {
391
+ parent.classList.replace('scroll-2', 'scroll-1');
392
+ }
393
+ if (!contains1 && !contains2) {
394
+ parent.classList.add('scroll-1');
395
+ }
396
+ }
397
+
398
+ return dom;
399
+ }
400
+
401
+ test() {
402
+ const sub = {
403
+ duration: 10000,
404
+ lang: 'zh',
405
+ text: '你呼吸 已改变 停滞于某段流年 离别的你我 才明白挥霍有期限,逝去的人不曾走远 像 突如其来温暖我的晴天 像 倾盆大雨我躲避的屋檐',
406
+ timestamp: 0,
407
+ url: 'xxx',
408
+ };
409
+ const subTitle = this.splitSubTitle(sub);
410
+ this.renderSubtitle(subTitle);
411
+
412
+ setTimeout(() => {
413
+ this.test();
414
+ }, 10000);
415
+ }
416
+
417
+ getPlayTime() {
418
+ const isSoft = this.getIsSoft();
419
+ this.on(Events.TIME_UPDATE, (data: any) => {
420
+ if (!isSoft) {
421
+ const info: any = {
422
+ currentTime: (this.player.video as any).currentTime * 1000,
423
+ dtsBase: this.state.dtsBase,
424
+ rtmpDtsGap: this.state.rtmpDtsGap,
425
+ };
426
+ info.timestamp = info.currentTime + info.dtsBase + info.rtmpDtsGap;
427
+ VE_DEBUG.log('直播当前时间戳:', info.timestamp, info);
428
+ } else {
429
+ const info: any = {
430
+ pts: data?.detail?.pts,
431
+ rtmpDtsGap: this.state.rtmpDtsGap,
432
+ };
433
+ info.timestamp = info.pts + info.rtmpDtsGap;
434
+ VE_DEBUG.log('直播当前时间戳:', info.timestamp, info);
435
+ }
436
+ });
437
+ }
438
+
439
+ resetDom(el: HTMLElement) {
440
+ if (!el) return;
441
+ this.state.currentSub = null;
442
+ if (!el) return;
443
+ el.innerHTML = '';
444
+ el.classList.remove('scroll-1', 'scroll-2');
445
+ }
446
+
447
+ render() {
448
+ return '<xg-subtitles class="xgplayer-subtitles"><xg-subtitles-inner class="xgplayer-subtitles-inner"></xg-subtitles-inner><xg-subtitles-helper class="xgplayer-subtitles-helper"></xg-helper></xg-subtitles>';
449
+ }
450
+ }
451
+
452
+ export default LiveSubTitlesPlugin;