pr-player 0.0.8 → 0.1.0
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.
- package/README.md +47 -283
- package/dist/PrPlayer.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/index.umd.cjs +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 对 flv 格式的地址进行解析 并输出 canvas、stream,提供 SEI 回调,以及 cut 等相关能力,以支持根据业务层 SEI 对视频进行剪切渲染。
|
|
2
2
|
|
|
3
3
|
## 立即开始
|
|
4
4
|
|
|
@@ -11,324 +11,88 @@ npm i pr-player
|
|
|
11
11
|
### 引入
|
|
12
12
|
|
|
13
13
|
```js
|
|
14
|
-
|
|
15
|
-
import { uuid, random, randomName, md5, regExps, timeFormat, timeFrom, line2hump, hump2line } from 'pr-player'
|
|
14
|
+
import { PrPlayer } from 'pr-player'
|
|
16
15
|
|
|
17
|
-
//
|
|
18
|
-
import
|
|
16
|
+
// 除此之外 如果你需要自定义扩展 为你提供了独立的 Demuxer、Decoder、Render,并且提供对应的worker
|
|
17
|
+
import { Demuxer, DemuxerWorker } from '../../src/index'
|
|
18
|
+
import { Decoder, DecoderWorker } from '../../src/index'
|
|
19
|
+
import { Render, RenderWorker } from '../../src/index'
|
|
19
20
|
```
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
## 快速使用
|
|
22
23
|
|
|
23
24
|
```js
|
|
24
|
-
|
|
25
|
-
{
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
//
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
// filterKeys
|
|
40
|
-
{
|
|
41
|
-
const obj = { name: 'a', age: 10, phone: 22, 1: 3, '2': 44 }
|
|
42
|
-
let res = prTools.filterKeys(obj, ['phone', 'age', 1])
|
|
43
|
-
console.log('\x1b[38;2;0;151;255m%c%s', 'color:#0097ff', `------->Breathe:res`, res)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// arrFilterDup
|
|
47
|
-
{
|
|
48
|
-
const arr = [
|
|
49
|
-
{ name: 'a', age: 10, phone: 123 },
|
|
50
|
-
{ name: 'b', age: 12, phone: 456 },
|
|
51
|
-
{ name: 'c', age: 10, phone: 789 }
|
|
52
|
-
]
|
|
53
|
-
let res = prTools.arrFilterDup(arr, ['age'], true)
|
|
54
|
-
console.log('\x1b[38;2;0;151;255m%c%s', 'color:#0097ff', `------->Breathe:res`, res)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// arrFromEnum
|
|
58
|
-
{
|
|
59
|
-
const enum_template = {
|
|
60
|
-
lable_1: 'value_1',
|
|
61
|
-
lable_2: 'value_2',
|
|
62
|
-
lable_3: 'value_3',
|
|
63
|
-
lable_4: 'value_4'
|
|
64
|
-
} as const
|
|
65
|
-
|
|
66
|
-
const res = arrFromEnum(enum_template, 'key')
|
|
67
|
-
console.log('\x1b[38;2;0;151;255m%c%s', 'color:#0097ff', `------->Breathe:res`, res)
|
|
68
|
-
|
|
69
|
-
// 另外可能会用到的定义
|
|
70
|
-
type O = typeof enum_template
|
|
71
|
-
type K = keyof O // "lable_1" | "lable_2" | "lable_3" | "lable_4"
|
|
72
|
-
type V = O[K] // "value_1" | "value_2" | "value_3" | "value_4"
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// md5
|
|
76
|
-
{
|
|
77
|
-
let str = md5('123456')
|
|
78
|
-
console.log('\x1b[38;2;0;151;255m%c%s', 'color:#0097ff', `------->Breathe:test:md5`, str)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// timeFormat
|
|
82
|
-
{
|
|
83
|
-
let str = timeFormat('2024/11/06 04:06:06', 'YYYY-MM-DD hh:mm:ss 星期WWW')
|
|
84
|
-
console.log('\x1b[38;2;0;151;255m%c%s', 'color:#0097ff', `------->Breathe:test:timeFormat`, str)
|
|
25
|
+
const player = new PrPlayer()
|
|
26
|
+
const play = async () => {
|
|
27
|
+
pause.value = false
|
|
28
|
+
await player.start('https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-720p.flv')
|
|
29
|
+
player.setMute(false)
|
|
30
|
+
|
|
31
|
+
// 渲染方式一 tip: 默认只开启 'stream' 如果需要同时使用 'canvas' 、'stream' 则需要手动设置(Player.setShader)
|
|
32
|
+
{
|
|
33
|
+
const canvas = player.getCanvas()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 渲染方式二
|
|
37
|
+
{
|
|
38
|
+
const stream = player.getStream()
|
|
39
|
+
}
|
|
85
40
|
}
|
|
86
41
|
```
|
|
87
42
|
|
|
88
|
-
|
|
43
|
+
### 暂停渲染
|
|
89
44
|
|
|
90
45
|
```js
|
|
91
|
-
|
|
92
|
-
let res = prTools.highlight('123456', ['3', '5'])
|
|
93
|
-
console.log('\x1b[38;2;0;151;255m%c%s\x1b[0m', 'color:#0097ff;padding:16px 0;', `------->Breathe:res`, res)
|
|
94
|
-
}
|
|
46
|
+
player.setPause(true)
|
|
95
47
|
```
|
|
96
48
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
```jsx
|
|
100
|
-
<div dangerouslySetInnerHTML={{ __html: highlight('123456', ['3', '5']) }}></div>
|
|
101
|
-
```
|
|
49
|
+
### 设置渲染模式
|
|
102
50
|
|
|
103
|
-
|
|
51
|
+
- 默认只开启 'stream' 如果需要同时使用 'canvas' 、'stream' 则需要手动设置
|
|
104
52
|
|
|
105
|
-
```
|
|
106
|
-
|
|
53
|
+
```js
|
|
54
|
+
player.setShader(['canvas', 'stream'])
|
|
107
55
|
```
|
|
108
56
|
|
|
109
|
-
###
|
|
110
|
-
|
|
111
|
-
#### 时间相关函数
|
|
57
|
+
### 停止
|
|
112
58
|
|
|
113
59
|
```js
|
|
114
|
-
|
|
115
|
-
* 获取时间戳 失败返回 0
|
|
116
|
-
* @param _val Date | number | string
|
|
117
|
-
* @example timeStamp()
|
|
118
|
-
* @example timeStamp('2024-05-23')
|
|
119
|
-
* @returns 转换后的时间戳 | 0
|
|
120
|
-
*/
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* 格式化时间
|
|
124
|
-
* @param _val Date | number | string
|
|
125
|
-
* @param _format 格式化模板 YYYY-MM-DD hh:mm:ss
|
|
126
|
-
* @param _options _options: { offset?: number; empty_str?: string }
|
|
127
|
-
* @example timeFormat('2024/09/24 04:06:06', 'YYYY-MM-DD hh:mm:ss')
|
|
128
|
-
* @returns 格式化后的字符串
|
|
129
|
-
*/
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* 多久之前时间
|
|
133
|
-
* @param _val Date | number | string
|
|
134
|
-
* @param format 格式化模板 YYYY-MM-DD hh:mm:ss
|
|
135
|
-
* @param _options _options: { offset?: number; empty_str?: string }
|
|
136
|
-
* @example timeFrom(new Date().getTime() - 5600000)
|
|
137
|
-
* @returns 格式化后的字符串
|
|
138
|
-
*/
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* 获取某个时间的范围日期
|
|
142
|
-
* @param _val Date | number | string
|
|
143
|
-
* @param _options _options: { offset?: number; empty_str?: string }
|
|
144
|
-
* @example timeRange(new Date().getTime())
|
|
145
|
-
* @returns [] 该范围的每一天集合
|
|
146
|
-
*/
|
|
60
|
+
player.stop()
|
|
147
61
|
```
|
|
148
62
|
|
|
149
|
-
|
|
63
|
+
## 以下是剪切相关 API
|
|
150
64
|
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* 把一纬数组按指定长度分割
|
|
154
|
-
* @param _arr 一纬数组
|
|
155
|
-
* @param _size 分割长度
|
|
156
|
-
* @example arrSlice([1, 2, 3, 4, 5], 2)
|
|
157
|
-
* @returns 分割后的二维数组
|
|
158
|
-
*/
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* 在数组里面向上向下取整数的一个范围
|
|
162
|
-
* @param _arr 多个数值的数组
|
|
163
|
-
* @param _accuracy 间隔精度
|
|
164
|
-
* @returns [min,max]
|
|
165
|
-
* @notes 例如传入[-13,37,67] 返回 [-20,70] ,类似于[0,0]将会返回[0,10]
|
|
166
|
-
* @notes 常在echart中使用 const [yAxisMin, yAxisMax] = arrRange([1,87], 10) // 取区间整数 [1,87] => [0,90]
|
|
167
|
-
* @example arrRange([-13, 37, 67]) // [-20,70]
|
|
168
|
-
* @example arrRange([0, 0], 10) // [0,10]
|
|
169
|
-
*/
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* 数组去重
|
|
173
|
-
* @param _arr 数组
|
|
174
|
-
* @param _keys 根据哪些字段去重
|
|
175
|
-
* @param _cover 如果有重复 是否进行覆盖 默认为 true
|
|
176
|
-
* @example arrFilterDup(arr, ['name', 'age'])
|
|
177
|
-
* @returns 去重后的数组
|
|
178
|
-
*/
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* 筛选 数组对象 中指定的key
|
|
182
|
-
* @param _arr 需要筛选的对象数组
|
|
183
|
-
* @param _keys 需要筛选哪些字段
|
|
184
|
-
* @example arrFilterKeys(arr, ['label', 'name'])
|
|
185
|
-
* @returns 筛选后结果 传入对象返回对象 传入数组返回数组
|
|
186
|
-
*/
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* 根据一个枚举对象生成 常用的键值对数组
|
|
190
|
-
* @param _enum 枚举对象 { value: label }
|
|
191
|
-
* @param _value_name 值名
|
|
192
|
-
* @param _label_name 键名
|
|
193
|
-
* @example arrFromEnum(obj, ['value', 'label'])
|
|
194
|
-
* @returns 键值对数组
|
|
195
|
-
*/
|
|
196
|
-
```
|
|
65
|
+
- 例如我需要创建一个名为 cut-any-key 的自定义剪切,将提供以下 api 支持:
|
|
197
66
|
|
|
198
|
-
|
|
67
|
+
### 创建剪切
|
|
199
68
|
|
|
200
69
|
```js
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
*/
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* 随机生成uuid
|
|
211
|
-
* @param _len 长度
|
|
212
|
-
* @param _radix 进制 为了保证唯一性 进制过低时会 按照最低长度返回
|
|
213
|
-
* @example uuid(32, 16)
|
|
214
|
-
* @returns 随机uuid
|
|
215
|
-
*/
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* 随机生成昵称
|
|
219
|
-
* @param _min 最小名称长度(最小1)
|
|
220
|
-
* @param _max 最大名称长度(大于等于1)
|
|
221
|
-
* @example randomName(2, 4)
|
|
222
|
-
* @returns 随机昵称
|
|
223
|
-
*/
|
|
70
|
+
player.cut.create('cut-any-key', { sx: width * 0.25, sy: height * 0.4, sw: width * 0.5, sh: height * 0.5 })
|
|
71
|
+
|
|
72
|
+
const canvas = player.cut.getCanvas('cut-any-key')
|
|
73
|
+
|
|
74
|
+
// 渲染方式二
|
|
75
|
+
const stream = player.cut.getStream('cut-any-key')
|
|
224
76
|
```
|
|
225
77
|
|
|
226
|
-
|
|
78
|
+
### 暂停剪切
|
|
227
79
|
|
|
228
80
|
```js
|
|
229
|
-
|
|
230
|
-
* 对字符串进行md5加密
|
|
231
|
-
* @param _string 需要加密的字符串
|
|
232
|
-
* @example md5('123456')
|
|
233
|
-
* @returns 加密后的字符串
|
|
234
|
-
*/
|
|
81
|
+
player.cut.setPause('cut-any-key', true)
|
|
235
82
|
```
|
|
236
83
|
|
|
237
|
-
|
|
84
|
+
### 设置渲染模式
|
|
85
|
+
|
|
86
|
+
- 默认只开启 'stream' 如果需要同时使用 'canvas' 、'stream' 则需要手动设置
|
|
238
87
|
|
|
239
88
|
```js
|
|
240
|
-
|
|
241
|
-
* ArrayBuffer转十六进制
|
|
242
|
-
* @param _buffer arrayBuffer
|
|
243
|
-
* @returns 十六进制字符串
|
|
244
|
-
*/
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* 十六进制转ArrayBuffer
|
|
248
|
-
* @param _str 十六进制字符串
|
|
249
|
-
* @returns buffer
|
|
250
|
-
*/
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* 十六进制转ASCII码
|
|
254
|
-
* @param _hexCharCodeStr 16进制字符串
|
|
255
|
-
* @returns 转换后的ASCII码
|
|
256
|
-
*/
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* 短划线转换驼峰
|
|
260
|
-
* @param _str 短横线字符串
|
|
261
|
-
* @returns 驼峰字符串
|
|
262
|
-
*/
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* 驼峰转换短横线
|
|
266
|
-
* @param _str 驼峰字符串
|
|
267
|
-
* @returns 短横线字符串
|
|
268
|
-
*/
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* 去除首尾空格
|
|
272
|
-
* @param _str 字符串
|
|
273
|
-
* @returns 结果字符串
|
|
274
|
-
*/
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* 字节单位转换
|
|
278
|
-
* @param _bytes 字节
|
|
279
|
-
* @param _splitStr 值与单位的分割符 默认为一个空格
|
|
280
|
-
* @returns 格式化后的字符串
|
|
281
|
-
*/
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* 把数字分割为千分位计量的字符串
|
|
285
|
-
* @param _number 数值
|
|
286
|
-
* @example num2split(123456789)
|
|
287
|
-
* @returns 千分位计量的字符串
|
|
288
|
-
*/
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* 筛选 对象 中指定的key
|
|
292
|
-
* @param _obj 需要筛选的 对象
|
|
293
|
-
* @param _keys 需要筛选哪些字段
|
|
294
|
-
* @example filterKeys(_obj, ['lable', 'name'])
|
|
295
|
-
* @returns 筛选后结果 传入对象返回对象 传入数组返回数组
|
|
296
|
-
*/
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* 生成高亮字符串的html
|
|
300
|
-
* @param _text 当前文本字符串
|
|
301
|
-
* @param _keys 关键词数组 string[]
|
|
302
|
-
* @example highlight('123456', ['3', '5'])
|
|
303
|
-
* @returns 处理后的 html 字符串
|
|
304
|
-
*/
|
|
89
|
+
player.cut.setShader('cut-any-key', ['canvas', 'stream'])
|
|
305
90
|
```
|
|
306
91
|
|
|
307
|
-
|
|
92
|
+
### 移除剪切
|
|
308
93
|
|
|
309
94
|
```js
|
|
310
|
-
|
|
311
|
-
* 分段执行
|
|
312
|
-
* @param _cuont 一共执行多少次 最小为 0
|
|
313
|
-
* @param _step 每次执行多少次 最小为 1
|
|
314
|
-
* @example exeStep(98, 7, ()=>{})
|
|
315
|
-
* @returns 筛选后结果 传入对象返回对象 传入数组返回数组
|
|
316
|
-
*/
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* 延迟执行 强制将请求延长时间以模拟 loading
|
|
320
|
-
* @param _func 需要执行的函数
|
|
321
|
-
* @param _timeout 最小执行时间ms 默认 500ms
|
|
322
|
-
* @example await exeDelayed(()=>{}, 500)
|
|
323
|
-
* @returns
|
|
324
|
-
*/
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* 检查函数执行消耗时间
|
|
328
|
-
* @param _func 需要执行的函数
|
|
329
|
-
* @example const elapsed = await exeElapsed(()=>{})
|
|
330
|
-
* @returns elapsed 消耗时间 ms
|
|
331
|
-
*/
|
|
95
|
+
player.cut.remove('cut-any-key')
|
|
332
96
|
```
|
|
333
97
|
|
|
334
98
|
## 代码仓库
|
package/dist/PrPlayer.d.ts
CHANGED
|
@@ -61,8 +61,6 @@ export declare class PrPlayer {
|
|
|
61
61
|
private initRender;
|
|
62
62
|
getCanvas: () => HTMLCanvasElement | undefined;
|
|
63
63
|
getStream: () => MediaStream | undefined;
|
|
64
|
-
getCutCanvas: (key: string) => HTMLCanvasElement | undefined;
|
|
65
|
-
getCutStream: (key: string) => MediaStream | undefined;
|
|
66
64
|
setPause: (pause: boolean) => void;
|
|
67
65
|
/**
|
|
68
66
|
* 设置渲染模式
|
|
@@ -88,6 +86,8 @@ export declare class PrPlayer {
|
|
|
88
86
|
canvas: HTMLCanvasElement;
|
|
89
87
|
destroy: Function;
|
|
90
88
|
};
|
|
89
|
+
getCanvas: (key: string) => HTMLCanvasElement | undefined;
|
|
90
|
+
getStream: (key: string) => MediaStream | undefined;
|
|
91
91
|
setPause: (key: string, pause: boolean) => void;
|
|
92
92
|
/**
|
|
93
93
|
* 设置渲染模式
|
package/dist/index.js
CHANGED
|
@@ -434,8 +434,6 @@ class be {
|
|
|
434
434
|
};
|
|
435
435
|
getCanvas = () => this.canvas;
|
|
436
436
|
getStream = () => this.stream;
|
|
437
|
-
getCutCanvas = (t) => this.cutRenders.get(t)?.canvas;
|
|
438
|
-
getCutStream = (t) => this.cutRenders.get(t)?.stream;
|
|
439
437
|
setPause = (t) => {
|
|
440
438
|
this.renderWorker?.setPause(t);
|
|
441
439
|
};
|
|
@@ -458,6 +456,8 @@ class be {
|
|
|
458
456
|
let e = this.cutRenders.get(t);
|
|
459
457
|
return e ? (e.worker.setCut(s), e.worker.setPause(!1), e) : (e = G(this.renderBaseTime), e.worker.setCut(s), this.cutRenders.set(t, e), e);
|
|
460
458
|
},
|
|
459
|
+
getCanvas: (t) => this.cutRenders.get(t)?.canvas,
|
|
460
|
+
getStream: (t) => this.cutRenders.get(t)?.stream,
|
|
461
461
|
setPause: (t, s) => {
|
|
462
462
|
this.cutRenders.get(t)?.worker.setPause(s);
|
|
463
463
|
},
|
package/dist/index.umd.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
(function(m,b){typeof exports=="object"&&typeof module<"u"?b(exports):typeof define=="function"&&define.amd?define(["exports"],b):(m=typeof globalThis<"u"?globalThis:m||self,b(m["pr-player"]={}))})(this,(function(m){"use strict";const b='(function(){"use strict";const U=new TextDecoder("utf-8"),H=(e,s)=>{const a=e.getUint8(s),t=a>>7&1,n=a>>5&3,r=a&31;return{forbidden_zero_bit:t,nal_ref_idc:n,nal_unit_type:r}},b=(e,s)=>e.getUint8(s),P=(e,s,a)=>{const t=new Uint8Array(e.buffer.slice(s,s+a));return U?.decode(t)||""},T=(e,s,a)=>{let t=s,n,r=0;switch(a){case 0:n=e.getFloat64(t,!1),r=8;break;case 1:n=!!e.getUint8(t),r=1;break;case 2:{n="";const o=e.getUint16(t,!1);t=t+2;const c=new Int8Array(e.buffer,t,o).filter(d=>d!==0);n=(U?.decode(c)||"").trim(),r=2+o}break;case 3:for(n={};t<e.byteLength;){const o=e.getUint16(t,!1);if(o===0)break;t=t+2;const c=P(e,t,o);t=t+o;const g=b(e,t);if(g===6)break;t=t+1;const d=T(e,t,g);t=t+d.length,n[c]=d.value,r=2+o+1+d.length}break;case 8:{n={};const o=e.getUint32(t,!1);t=t+4;for(let c=0;c<o;c++){const g=e.getUint16(t,!1);t=t+2;const d=P(e,t,g);t=t+g;const h=b(e,t);t=t+1;const l=T(e,t,h);t=t+l.length,n[d]=l.value,r=2+g+1+l.length}}break;case 10:{n=[];const o=e.getUint32(t,!1);t=t+4;for(let c=0;c<o;c++){const g=b(e,t);t=t+1;const d=T(e,t,g);t=t+d.length,n.push(d.value),r=1+d.length}}break}return{amfType:a,length:r,value:n}},m=(e,s)=>e.getUint8(s)<<16|e.getUint8(s+1)<<8|e.getUint8(s+2);var u={header:{getSignature:e=>{const s=new Int8Array(e.buffer.slice(0,3));return U?.decode(s)||""},getVersion:e=>e.getUint8(3),getFlags:e=>{const a=e.getUint8(0).toString(2).padStart(5,"0").split(""),[,,t,,n]=a;return{audio:n==="1",video:t==="1"}},getDataOffset:e=>e.getUint32(5)},getPreviousTagSize:(e,s)=>e.getUint32(s),isSurplusTag:(e,s)=>{let a=!0;const t=e.byteLength;if(s+4>t)a=!1;else if(s+4+11>t)a=!1;else{const n=m(e,s+4+1);s+4+11+n>t&&(a=!1)}return a},tag:{tagHeader:{getTagType:(e,s)=>{const a=e.getUint8(s);let t;switch(a){case 18:t="script";break;case 8:t="audio";break;case 9:t="video";break}return t},getDataSize:(e,s)=>m(e,s+1),getTimestamp:(e,s)=>m(e,s+4),getTimestampExtended:(e,s)=>e.getUint8(s+7),getStreamID:(e,s)=>m(e,s+8)},tagBody:{parseAudio:(e,s,a)=>{let t=s;const n=e.getUint8(t),r=n>>4&15,i=n>>2&3,o=n>>1&1,c=n&1;t=t+1;const g=e.getUint8(t);t=t+1;const d=a-2,h=new Uint8Array(e.buffer.slice(t,t+d));if(r===10&&g===0){const l=e.getUint8(t),p=e.getUint8(t+1),f=(l&248)>>3,y=(l&7)<<1|p>>7,S=(p&120)>>3,A=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350],D=`mp4a.40.${f}`,z=A[y];return{soundFormat:r,soundRate:i,soundSize:o,soundType:c,accPacketType:g,data:h,audioObjectType:f,samplingFrequencyIndex:y,channelConfiguration:S,codec:D,sampleRate:z}}return{soundFormat:r,soundRate:i,soundSize:o,soundType:c,accPacketType:g,data:h}},parseVideo:(e,s,a)=>{let t=s;const n=e.getUint8(t),r=n>>4&15,i=n&15;t=t+1;const o=e.getUint8(t);t=t+1;const c=m(e,t);t=t+3;const g=a-5,d=new Uint8Array(e.buffer.slice(t,t+g));switch(i){case 7:if(o===0){const h=e.getUint8(t);if(t=t+1,h!==1)throw new Error("Invalid AVC version");const l=e.getUint8(t)&255;t=t+1;const p=e.getUint8(t)&255;t=t+1;const f=e.getUint8(t)&255;t=t+1;const A=`avc1.${Array.from([l,p,f],E=>E.toString(16).padStart(2,"0")).join("")}`,D=(e.getUint8(t)&3)-1;t=t+1;const z=e.getUint8(t)&31;t=t+1;const _=e.getUint16(t,!1);t=t+2;const O=new Uint8Array(e.buffer.slice(t,t+_));t=t+_;const I=e.getUint8(t)&31;t=t+1;const x=e.getUint16(t,!1);t=t+2;const M=new Uint8Array(e.buffer.slice(t,t+x));return t=t+x,{frameType:r,codecID:i,avcPacketType:o,cts:c,data:d,version:h,codec:A,profile:l,compatibility:p,level:f,lengthSizeMinusOne:D,numOfSequenceParameterSets:z,sequenceParameterSetLength:_,sps:O,numOfPictureParameterSets:I,pictureParameterSetLength:x,pps:M}}else if(o===1){const h=[],l=t+a-5;for(;t+4<l;){const p=e.getUint32(t,!1);t=t+4;const f=H(e,t);t=t+1;const y=p-1,S=new Uint8Array(e.buffer.slice(t,t+y));t=t+y,h.push({size:p,header:f,payload:S})}return{frameType:r,codecID:i,avcPacketType:o,cts:c,data:d,nalus:h}}break;default:throw new Error("Unsupported codecID")}return{frameType:r,codecID:i,avcPacketType:o,cts:c,data:d}},parseMetaData:(e,s)=>{let a=s;{if(e.getUint8(a)!==2)throw new Error("Invalid AMF type for onMetaData (expected 0x02)");a=a+1}const t=e.getUint16(a,!1);a=a+2;{const i=new Int8Array(e.buffer.slice(a,a+t));if((U?.decode(i)||"")!=="onMetaData")throw new Error("Expected \'onMetaData\' string");a=a+t}const n=b(e,a);return a=a+1,T(e,a,n).value}}}};class L{parseSpeed=8;pendingPayloads=[];payload=new Uint8Array(0);offset=0;is_parsing=!1;header;tag;on={};constructor(){}init=()=>{this.destroy()};push=s=>{this.pendingPayloads.push(s),this.is_parsing||this.parse()};destroy=()=>{this.pendingPayloads=[],this.payload=new Uint8Array(0),this.offset=0,this.is_parsing=!1,this.header=void 0,this.tag=void 0};parse=async()=>{for(this.is_parsing=!0;;){const s=this.pendingPayloads.shift();if(!s)break;const a=new Uint8Array(this.payload.byteLength+s.byteLength);a.set(this.payload,0),a.set(s,this.payload.byteLength),this.payload=a;const t=new DataView(this.payload.buffer);this.header||this.parseHeader(t),await this.parseTag(t)}this.is_parsing=!1};parseHeader=s=>(this.header={signature:u.header.getSignature(s),version:u.header.getVersion(s),flags:u.header.getFlags(s),dataOffset:u.header.getDataOffset(s)},this.offset=this.header?.dataOffset,this.on.header&&this.on.header(this.header),this.header);parseTag=async s=>{const a=(n,r)=>({tagType:u.tag.tagHeader.getTagType(n,r),dataSize:u.tag.tagHeader.getDataSize(n,r),timestamp:u.tag.tagHeader.getTimestamp(n,r),timestampExtended:u.tag.tagHeader.getTimestampExtended(n,r),streamID:u.tag.tagHeader.getStreamID(n,r)}),t=(n,r,i,o)=>{let c;switch(n){case"script":c=u.tag.tagBody.parseMetaData(r,i);break;case"audio":c=u.tag.tagBody.parseAudio(r,i,o);break;case"video":c=u.tag.tagBody.parseVideo(r,i,o);break}return c};for(;this.offset<s.byteLength;){if(u.isSurplusTag(s,this.offset)===!1){this.payload=this.payload.slice(this.offset),this.offset=0;break}const r=a(s,this.offset+4),{tagType:i,dataSize:o}=r;if(!i)break;const c=t(i,s,this.offset+4+11,o);this.tag={header:r,body:c},this.on.tag&&this.on.tag(this.tag),this.offset=this.offset+4+11+o,await new Promise(g=>setTimeout(()=>g(!0),this.parseSpeed))}}}const k=new L;k.on.header=e=>postMessage({action:"onHeader",data:e}),k.on.tag=e=>postMessage({action:"onTag",data:e}),onmessage=e=>{const{action:s,data:a}=e.data,t=k[s];t&&t(a)}})();\n',M=typeof self<"u"&&self.Blob&&new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);",b],{type:"text/javascript;charset=utf-8"});function E(i){let t;try{if(t=M&&(self.URL||self.webkitURL).createObjectURL(M),!t)throw"";const s=new Worker(t,{name:i?.name});return s.addEventListener("error",()=>{(self.URL||self.webkitURL).revokeObjectURL(t)}),s}catch{return new Worker("data:text/javascript;charset=utf-8,"+encodeURIComponent(b),{name:i?.name})}}class P{worker=new E;on={};constructor(){this.worker.onmessage=t=>{const{action:s,data:e}=t.data;s==="onHeader"&&this.on.header&&this.on.header(e),s==="onTag"&&this.on.tag&&this.on.tag(e)}}init=()=>this.worker.postMessage({action:"init"});push=t=>this.worker.postMessage({action:"push",data:t});destroy=()=>{this.worker.postMessage({action:"destroy",data:{}}),this.worker.terminate()}}const G=`(function(){"use strict";class r{audioDecoderConfig;audioDecoder;videoDecoderConfig;videoDecoder;hasKeyFrame=!1;on={audio:{},video:{}};constructor(){}audio={init:i=>{this.audio.destroy(),this.audioDecoderConfig={...i},this.audioDecoder=new AudioDecoder({output:e=>{this.on.audio.decode&&this.on.audio.decode(e)},error:e=>{this.on.audio.error&&this.on.audio.error(e)}}),this.audioDecoder.configure(this.audioDecoderConfig)},decode:i=>{if(!this.audioDecoder)return;const e=new EncodedAudioChunk(i);this.audioDecoder.decode(e)},flush:()=>{this.audioDecoder?.flush()},destroy:()=>{this.audioDecoderConfig=void 0,this.audioDecoder?.close(),this.audioDecoder=void 0}};video={init:i=>{this.video.destroy(),this.videoDecoderConfig={...i},this.videoDecoder=new VideoDecoder({output:async e=>{const d=await createImageBitmap(e),s=e.timestamp;e.close(),d.width>0&&d.height>0?this.on.video.decode&&this.on.video.decode({timestamp:s,bitmap:d}):d.close()},error:e=>{this.on.video.error&&this.on.video.error(e)}}),this.videoDecoder.configure(this.videoDecoderConfig)},decode:i=>{if(this.videoDecoder&&(i.type==="key"&&(this.hasKeyFrame=!0),this.hasKeyFrame&&this.videoDecoder.decodeQueueSize<2)){const e=new EncodedVideoChunk(i);this.videoDecoder.decode(e)}},flush:()=>{this.videoDecoder?.flush()},destroy:()=>{this.videoDecoderConfig=void 0,this.videoDecoder?.close(),this.videoDecoder=void 0,this.hasKeyFrame=!1}}}const t=new r;t.on.audio.decode=o=>postMessage({type:"audio",action:"onDecode",data:o}),t.on.audio.error=o=>postMessage({type:"audio",action:"onError",data:o}),t.on.video.decode=o=>postMessage({type:"video",action:"onDecode",data:o}),t.on.video.error=o=>postMessage({type:"video",action:"onError",data:o}),onmessage=o=>{const{type:i,action:e,data:d}=o.data,s=t[i][e];s&&s(d)}})();
|
|
2
2
|
`,L=typeof self<"u"&&self.Blob&&new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);",G],{type:"text/javascript;charset=utf-8"});function _(i){let t;try{if(t=L&&(self.URL||self.webkitURL).createObjectURL(L),!t)throw"";const s=new Worker(t,{name:i?.name});return s.addEventListener("error",()=>{(self.URL||self.webkitURL).revokeObjectURL(t)}),s}catch{return new Worker("data:text/javascript;charset=utf-8,"+encodeURIComponent(G),{name:i?.name})}}class N{worker=new _;on={audio:{},video:{}};constructor(){this.worker.onmessage=t=>{const{type:s,action:e,data:a}=t.data;switch(s){case"audio":e==="onDecode"&&this.on.audio.decode&&this.on.audio.decode(a),e==="onError"&&this.on.audio.error&&this.on.audio.error(a);break;case"video":e==="onDecode"&&this.on.video.decode&&this.on.video.decode(a),e==="onError"&&this.on.video.error&&this.on.video.error(a);break}}}audio={init:t=>this.worker.postMessage({type:"audio",action:"init",data:t}),decode:t=>this.worker.postMessage({type:"audio",action:"decode",data:t}),flush:()=>this.worker.postMessage({type:"audio",action:"flush"}),destroy:()=>{this.worker.postMessage({type:"audio",action:"destroy"})}};video={init:t=>this.worker.postMessage({type:"video",action:"init",data:t}),decode:t=>this.worker.postMessage({type:"video",action:"decode",data:t}),flush:()=>this.worker.postMessage({type:"video",action:"flush"}),destroy:()=>{this.worker.postMessage({type:"video",action:"destroy",data:{}})}};destroy=()=>{this.worker.postMessage({type:"audio",action:"destroy"}),this.worker.postMessage({type:"video",action:"destroy",data:{}}),this.worker.terminate()}}const F=`(function(){"use strict";class r{isRendering=!1;pendingFrames=[];offscreenCanvas;writable;writer;ctx;cutOption;baseTime=0;pause=!1;shader=["stream"];constructor(){}init=({offscreenCanvas:e,baseTime:t=performance.timeOrigin,writable:i})=>{this.destroy(),this.offscreenCanvas=e,this.writable=i,this.writer=this.writable.getWriter(),this.ctx=this.offscreenCanvas.getContext("2d"),this.baseTime=t};setShader=e=>{this.shader=e};setSize=({width:e,height:t})=>{this.offscreenCanvas&&(this.offscreenCanvas.width=e,this.offscreenCanvas.height=t)};destroy=()=>{this.isRendering=!1,this.pendingFrames=[],this.offscreenCanvas=void 0,this.ctx=void 0,this.baseTime=0};push=e=>{this.pendingFrames.push(e),this.isRendering===!1&&setTimeout(this.renderFrame,0)};setCut=e=>{this.cutOption=e};setPause=e=>{this.pause=e,this.isRendering===!1&&setTimeout(this.renderFrame,0)};calculateTimeUntilNextFrame=e=>{const t=performance.timeOrigin+performance.now(),s=this.baseTime+e/1e3-t;return Math.max(0,s)};renderFrame=async()=>{for(this.isRendering=!0;;){const e=this.pendingFrames.shift();if(!e)break;let{timestamp:t,bitmap:i}=e;if(this.cutOption){const{sx:a=0,sy:c=0,sw:m=i.width,sh:o=i.height}=this.cutOption;i=await createImageBitmap(i,a,c,m,o)}const s=this.calculateTimeUntilNextFrame(t);await new Promise(a=>setTimeout(()=>a(!0),s)),this.drawImage({timestamp:t,bitmap:i}),this.cutOption&&i.close()}this.isRendering=!1};drawImage=e=>{if(this.pause!==!0){if(this.shader.includes("stream")){const t=new VideoFrame(e.bitmap,{timestamp:e.timestamp});this.writer.write(t),t.close()}this.shader.includes("canvas")&&this.ctx&&this.offscreenCanvas&&this.ctx.drawImage(e.bitmap,0,0,this.offscreenCanvas.width,this.offscreenCanvas.height)}}}const h=new r;onmessage=n=>{const{action:e,data:t}=n.data,i=h[e];i&&i(t)}})();
|
|
3
|
-
`,O=typeof self<"u"&&self.Blob&&new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);",F],{type:"text/javascript;charset=utf-8"});function V(i){let t;try{if(t=O&&(self.URL||self.webkitURL).createObjectURL(O),!t)throw"";const s=new Worker(t,{name:i?.name});return s.addEventListener("error",()=>{(self.URL||self.webkitURL).revokeObjectURL(t)}),s}catch{return new Worker("data:text/javascript;charset=utf-8,"+encodeURIComponent(F),{name:i?.name})}}class W{worker=new V;constructor(){}init=({offscreenCanvas:t,baseTime:s=0,writable:e})=>this.worker.postMessage({action:"init",data:{offscreenCanvas:t,baseTime:s,writable:e}},[t,e]);setShader=t=>this.worker.postMessage({action:"setShader",data:t});setSize=({width:t,height:s})=>this.worker.postMessage({action:"setSize",data:{width:t,height:s}});push=t=>this.worker.postMessage({action:"push",data:t});setCut=async t=>this.worker.postMessage({action:"setCut",data:t});setPause=t=>this.worker.postMessage({action:"setPause",data:t});destroy=()=>{this.worker.postMessage({action:"destroy",data:{}}),this.worker.terminate()}}var j=Object.defineProperty,H=(i,t,s)=>t in i?j(i,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):i[t]=s,c=(i,t,s)=>H(i,typeof t!="symbol"?t+"":t,s);class ${constructor(t,s){c(this,"inputStream",new MediaStream),c(this,"outputStream",new MediaStream),c(this,"inputGain",1),c(this,"enhanceGain",1),c(this,"bgsGain",1),c(this,"bgmGain",1),c(this,"outputGain",1),c(this,"mixAudioMap",new Map),c(this,"audioContext",new AudioContext),c(this,"sourceNode"),c(this,"inputGainNode"),c(this,"enhanceGainNode"),c(this,"bgsGainNode"),c(this,"bgmGainNode"),c(this,"analyserNode"),c(this,"analyserArrayData"),c(this,"outputGainNode"),c(this,"destinationNode"),c(this,"filterStream",e=>e),c(this,"stop",()=>{{const e=this.inputStream.getTracks();for(const a of e)a.stop(),this.inputStream.removeTrack(a)}}),c(this,"getStream",()=>this.filterStream(this.outputStream)),c(this,"setMute",(e=!0)=>{e?this.analyserNode.disconnect(this.outputGainNode):this.analyserNode.connect(this.outputGainNode)}),c(this,"setInputGain",e=>{this.inputGain=e,this.inputGainNode.gain.setValueAtTime(e,this.audioContext.currentTime)}),c(this,"setEnhanceGain",async e=>{this.enhanceGain=e+1,this.enhanceGainNode.gain.setValueAtTime(this.enhanceGain,this.audioContext.currentTime)}),c(this,"setBgsGain",e=>{this.bgsGain=e,this.bgsGainNode.gain.setValueAtTime(e,this.audioContext.currentTime)}),c(this,"setBgmGain",e=>{this.bgmGain=e,this.bgmGainNode.gain.setValueAtTime(e,this.audioContext.currentTime)}),c(this,"setOutputGain",e=>{this.outputGain=e,this.outputGainNode.gain.setValueAtTime(this.outputGain,this.audioContext.currentTime)}),c(this,"getVolume",()=>{const{analyserNode:e,analyserArrayData:a}=this;e.getByteFrequencyData(a);let o=0;for(let r=0;r<a.length;r++)o+=a[r];return Math.ceil(o/a.length)}),c(this,"mixAudio",(e,a="bgm")=>new Promise(async(o,r)=>{try{{const u=this.mixAudioMap.get(a);u&&u.stop()}const n=a==="bgs"?this.bgsGainNode:this.bgmGainNode,d=this.audioContext.createBufferSource();this.mixAudioMap.set(a,d),d.buffer=e,d.connect(n),d.onended=()=>{d.disconnect(n),this.mixAudioMap.delete(a),o(!0)},d.start(0)}catch(n){r(n)}})),c(this,"mixAudioStop",e=>{const a=this.mixAudioMap.get(e);a?.stop()}),c(this,"changeMix",(e,a)=>{const o=e==="bgs"?this.bgsGainNode:this.bgmGainNode;a?o.connect(this.destinationNode):o.disconnect(this.destinationNode)}),s&&(this.audioContext=s),this.inputStream=t,this.sourceNode=this.audioContext.createMediaStreamSource(this.inputStream),this.inputGainNode=this.audioContext.createGain(),this.inputGainNode.gain.setValueAtTime(this.inputGain,this.audioContext.currentTime),this.enhanceGainNode=this.audioContext.createGain(),this.enhanceGainNode.gain.setValueAtTime(this.enhanceGain,this.audioContext.currentTime),this.bgsGainNode=this.audioContext.createGain(),this.bgsGainNode.gain.setValueAtTime(this.bgsGain,this.audioContext.currentTime),this.bgmGainNode=this.audioContext.createGain(),this.bgmGainNode.gain.setValueAtTime(this.bgmGain,this.audioContext.currentTime),this.analyserNode=this.audioContext.createAnalyser(),this.analyserNode.fftSize=512,this.analyserArrayData=new Uint8Array(this.analyserNode.frequencyBinCount),this.outputGainNode=this.audioContext.createGain(),this.outputGainNode.gain.setValueAtTime(this.outputGain,this.audioContext.currentTime),this.destinationNode=this.audioContext.createMediaStreamDestination(),this.outputStream=this.destinationNode.stream;{const{sourceNode:e,inputGainNode:a,enhanceGainNode:o,bgsGainNode:r,bgmGainNode:n,analyserNode:d,outputGainNode:u,destinationNode:h}=this;e.connect(a),a.connect(o),o.connect(d),r.connect(d),n.connect(d),o.connect(h),r.connect(h),n.connect(h),d.connect(u),u.connect(this.audioContext.destination)}this.setMute(!0),this.audioContext.resume()}}const q=async(i,t)=>{try{const{format:s,numberOfChannels:e,numberOfFrames:a,sampleRate:o}=t,r=i.createBuffer(e,a,o);for(let n=0;n<e;n++){const d=t.allocationSize({planeIndex:n}),u=new Uint8Array(d);t.copyTo(u,{planeIndex:n});const h=new DataView(u.buffer),f=r.getChannelData(n);for(let g=0;g<a;g++){let p;switch(s){case"s16":case"s16-planar":p=h.getInt16(g*2,!0)/32768;break;case"f32":case"f32-planar":p=h.getFloat32(g*4,!0);break;case"u8":case"u8-planar":p=(h.getUint8(g)-128)/128;break;default:throw new Error(`Unsupported audio format: ${s}`)}f[g]=Math.max(-1,Math.min(1,p))}}return r}catch(s){throw console.error("Failed to convert AudioData to AudioBuffer:",s),s}};class K{prAudioStream;audioContext;destination;stream=new MediaStream;nextStartTime=0;pendingSources=[];constructor(){}init=t=>{t||(t=new(window.AudioContext||window.webkitAudioContext)),this.audioContext=t,this.destination=this.audioContext.createMediaStreamDestination(),this.stream=new MediaStream,this.stream.addTrack(this.destination.stream.getAudioTracks()[0]),this.prAudioStream=new $(this.stream,this.audioContext),this.nextStartTime=0,this.pendingSources=[]};async push(t){try{if(!this.audioContext||!this.destination)return;const s=await q(this.audioContext,t);if(!s)return;const e=this.audioContext.createBufferSource();e.buffer=s,e.connect(this.destination);const a=Math.max(this.nextStartTime,this.audioContext.currentTime);this.nextStartTime=a+s.duration,e.start(a),this.pendingSources.push(e),e.onended=()=>{this.pendingSources=this.pendingSources.filter(o=>o!==e)},this.audioContext.state==="suspended"&&await this.audioContext.resume()}finally{t.close()}}getStream=()=>this.prAudioStream?.getStream();destroy(){this.audioContext?.close(),this.audioContext=void 0,this.destination=void 0,this.nextStartTime=0,this.prAudioStream?.stop(),this.pendingSources.forEach(t=>t.stop()),this.pendingSources=[]}}class Q{#t={timeout:5*1e3};#e;constructor(t={}){this.#t={...this.#t,...t}}check=(t,s)=>new Promise(async(e,a)=>{this.stop(),this.#e=new AbortController;const o=window.setTimeout(()=>{this.#e?.abort("Timeout."),a({status:"timeout",reason:""})},this.#t.timeout);try{const r=await fetch(t,{...s,method:"HEAD",signal:this.#e?.signal});r.status===200?e({status:"successed",reason:""}):a({status:"failed",reason:`${r.status}`})}catch(r){a({status:"error",reason:r.message})}clearTimeout(o)});request=async(t,s)=>new Promise(async(e,a)=>{try{await this.check(t,s),this.#e=new AbortController;const o=await fetch(t,{...s,signal:this.#e?.signal});e(o)}catch(o){this.stop(),a(o)}});stop=()=>{this.#e?.signal.aborted===!1&&this.#e.abort("Actively stop.")}}const I=i=>{const t=i?.getTracks()||[];for(const s of t)s.stop()},B=i=>{const t=new W,s=document.createElement("canvas"),e=s.transferControlToOffscreen(),a=new MediaStreamTrackGenerator({kind:"video"}),o=new MediaStream([a]),r=()=>{t.destroy(),I(o)};return t.init({offscreenCanvas:e,baseTime:i,writable:a.writable}),{worker:t,canvas:s,stream:o,destroy:r}};class J{prFetch=new Q;demuxerWorker;decoderWorker;audioPlayer;renderWorker;renderBaseTime=0;stream;canvas;on={demuxer:{},decoder:{}};cutRenders=new Map;trackGenerator;constructor(){}init=()=>{this.initDemuxer(),this.initDecoder(),this.audioPlayer=new K,this.audioPlayer.init(),this.initRender()};start=async t=>(this.stop(),this.renderBaseTime=new Date().getTime(),this.init(),this.prFetch.request(t).then(async s=>{const e=s.body?.getReader();if(!e)throw new Error("Reader is error.");const a=()=>{e.read().then(({done:o,value:r})=>{r&&this.demuxerWorker?.push(r),!o&&a()}).catch(o=>{if(o.name!=="AbortError")throw o})};a()}).catch(s=>{console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m","color:#0097ff;","------->Breathe: err",s)}));stop=async()=>{try{this.prFetch.stop()}catch(s){console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m","color:#0097ff;","------->Breathe: error",s)}this.demuxerWorker?.destroy(),this.decoderWorker?.destroy(),this.renderWorker?.destroy(),I(this.stream);const t=[...this.cutRenders.keys()];for(const s of t)this.cut.remove(s);this.audioPlayer?.destroy(),this.renderBaseTime=0,this.canvas=void 0};onTag=t=>{if(!this.decoderWorker)return;const{header:s,body:e}=t,{tagType:a,timestamp:o}=s;switch(a){case"script":{const{width:r,height:n}=e;this.renderWorker?.setSize({width:r,height:n}),this.on.demuxer.script&&this.on.demuxer.script(t)}break;case"audio":{const{accPacketType:r,data:n}=e;if(r===0){const{codec:d,sampleRate:u,channelConfiguration:h}=e,f={codec:d,sampleRate:u,numberOfChannels:h,description:new Uint8Array([])};this.decoderWorker.audio.init(f)}else r===1&&this.decoderWorker.audio.decode({type:"key",timestamp:o*1,data:n});this.on.demuxer.audio&&this.on.demuxer.audio(t)}break;case"video":{const{avcPacketType:r,frameType:n,data:d,nalus:u=[]}=e;if(r===0){const{codec:h,data:f}=e;this.decoderWorker.video.init({codec:h,description:f})}else if(r===1){const h=n===1?"key":"delta";this.decoderWorker.video.decode({type:h,timestamp:o*1e3,data:d});for(const f of u){const{header:g,payload:p}=f,{nal_unit_type:y}=g;y===6&&this.on.demuxer.sei&&this.on.demuxer.sei(p)}}this.on.demuxer.video&&this.on.demuxer.video(t)}break}};initDemuxer=()=>{this.demuxerWorker=new P,this.demuxerWorker.init(),this.demuxerWorker.on.tag=this.onTag};initDecoder=()=>{this.decoderWorker=new N,this.decoderWorker.on.audio.decode=t=>{this.audioPlayer?.push(t),this.on.decoder.audio&&this.on.decoder.audio(t)},this.decoderWorker.on.audio.error=t=>{this.stop(),this.on.error&&this.on.error(t)},this.decoderWorker.on.video.decode=async t=>{this.renderWorker?.push(t);const s=[...this.cutRenders.keys()];for(const e of s)this.cutRenders.get(e)?.worker.push(t);this.on.decoder.video&&this.on.decoder.video(t),t.bitmap.close()},this.decoderWorker.on.video.error=t=>{this.stop(),this.on.error&&this.on.error(t)}};initRender=()=>{const{worker:t,canvas:s,stream:e}=B(this.renderBaseTime);this.renderWorker=t,this.canvas=s,this.stream=e,this.renderWorker.setPause(!1)};getCanvas=()=>this.canvas;getStream=()=>this.stream;getCutCanvas=t=>this.cutRenders.get(t)?.canvas;getCutStream=t=>this.cutRenders.get(t)?.stream;setPause=t=>{this.renderWorker?.setPause(t)};setShader=t=>{this.renderWorker?.setShader(t)};setMute=t=>this.audioPlayer?.prAudioStream?.setMute(t);cut={create:(t,s)=>{let e=this.cutRenders.get(t);return e?(e.worker.setCut(s),e.worker.setPause(!1),e):(e=B(this.renderBaseTime),e.worker.setCut(s),this.cutRenders.set(t,e),e)},setPause:(t,s)=>{this.cutRenders.get(t)?.worker.setPause(s)},setShader:(t,s)=>{this.cutRenders.get(t)?.worker.setShader(s)},remove:t=>{this.cutRenders.get(t)?.destroy(),this.cutRenders.delete(t)}}}const T=new TextDecoder("utf-8"),X=(i,t)=>{const s=i.getUint8(t),e=s>>7&1,a=s>>5&3,o=s&31;return{forbidden_zero_bit:e,nal_ref_idc:a,nal_unit_type:o}},U=(i,t)=>i.getUint8(t),z=(i,t,s)=>{const e=new Uint8Array(i.buffer.slice(t,t+s));return T?.decode(e)||""},v=(i,t,s)=>{let e=t,a,o=0;switch(s){case 0:a=i.getFloat64(e,!1),o=8;break;case 1:a=!!i.getUint8(e),o=1;break;case 2:{a="";const n=i.getUint16(e,!1);e=e+2;const d=new Int8Array(i.buffer,e,n).filter(h=>h!==0);a=(T?.decode(d)||"").trim(),o=2+n}break;case 3:for(a={};e<i.byteLength;){const n=i.getUint16(e,!1);if(n===0)break;e=e+2;const d=z(i,e,n);e=e+n;const u=U(i,e);if(u===6)break;e=e+1;const h=v(i,e,u);e=e+h.length,a[d]=h.value,o=2+n+1+h.length}break;case 8:{a={};const n=i.getUint32(e,!1);e=e+4;for(let d=0;d<n;d++){const u=i.getUint16(e,!1);e=e+2;const h=z(i,e,u);e=e+u;const f=U(i,e);e=e+1;const g=v(i,e,f);e=e+g.length,a[h]=g.value,o=2+u+1+g.length}}break;case 10:{a=[];const n=i.getUint32(e,!1);e=e+4;for(let d=0;d<n;d++){const u=U(i,e);e=e+1;const h=v(i,e,u);e=e+h.length,a.push(h.value),o=1+h.length}}break}return{amfType:s,length:o,value:a}},w=(i,t)=>i.getUint8(t)<<16|i.getUint8(t+1)<<8|i.getUint8(t+2),l={header:{getSignature:i=>{const t=new Int8Array(i.buffer.slice(0,3));return T?.decode(t)||""},getVersion:i=>i.getUint8(3),getFlags:i=>{const s=i.getUint8(0).toString(2).padStart(5,"0").split(""),[,,e,,a]=s;return{audio:a==="1",video:e==="1"}},getDataOffset:i=>i.getUint32(5)},getPreviousTagSize:(i,t)=>i.getUint32(t),isSurplusTag:(i,t)=>{let s=!0;const e=i.byteLength;if(t+4>e)s=!1;else if(t+4+11>e)s=!1;else{const a=w(i,t+4+1);t+4+11+a>e&&(s=!1)}return s},tag:{tagHeader:{getTagType:(i,t)=>{const s=i.getUint8(t);let e;switch(s){case 18:e="script";break;case 8:e="audio";break;case 9:e="video";break}return e},getDataSize:(i,t)=>w(i,t+1),getTimestamp:(i,t)=>w(i,t+4),getTimestampExtended:(i,t)=>i.getUint8(t+7),getStreamID:(i,t)=>w(i,t+8)},tagBody:{parseAudio:(i,t,s)=>{let e=t;const a=i.getUint8(e),o=a>>4&15,r=a>>2&3,n=a>>1&1,d=a&1;e=e+1;const u=i.getUint8(e);e=e+1;const h=s-2,f=new Uint8Array(i.buffer.slice(e,e+h));if(o===10&&u===0){const g=i.getUint8(e),p=i.getUint8(e+1),y=(g&248)>>3,k=(g&7)<<1|p>>7,x=(p&120)>>3,S=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350],D=`mp4a.40.${y}`,C=S[k];return{soundFormat:o,soundRate:r,soundSize:n,soundType:d,accPacketType:u,data:f,audioObjectType:y,samplingFrequencyIndex:k,channelConfiguration:x,codec:D,sampleRate:C}}return{soundFormat:o,soundRate:r,soundSize:n,soundType:d,accPacketType:u,data:f}},parseVideo:(i,t,s)=>{let e=t;const a=i.getUint8(e),o=a>>4&15,r=a&15;e=e+1;const n=i.getUint8(e);e=e+1;const d=w(i,e);e=e+3;const u=s-5,h=new Uint8Array(i.buffer.slice(e,e+u));switch(r){case 7:if(n===0){const f=i.getUint8(e);if(e=e+1,f!==1)throw new Error("Invalid AVC version");const g=i.getUint8(e)&255;e=e+1;const p=i.getUint8(e)&255;e=e+1;const y=i.getUint8(e)&255;e=e+1;const S=`avc1.${Array.from([g,p,y],ae=>ae.toString(16).padStart(2,"0")).join("")}`,D=(i.getUint8(e)&3)-1;e=e+1;const C=i.getUint8(e)&31;e=e+1;const A=i.getUint16(e,!1);e=e+2;const te=new Uint8Array(i.buffer.slice(e,e+A));e=e+A;const se=i.getUint8(e)&31;e=e+1;const R=i.getUint16(e,!1);e=e+2;const ie=new Uint8Array(i.buffer.slice(e,e+R));return e=e+R,{frameType:o,codecID:r,avcPacketType:n,cts:d,data:h,version:f,codec:S,profile:g,compatibility:p,level:y,lengthSizeMinusOne:D,numOfSequenceParameterSets:C,sequenceParameterSetLength:A,sps:te,numOfPictureParameterSets:se,pictureParameterSetLength:R,pps:ie}}else if(n===1){const f=[],g=e+s-5;for(;e+4<g;){const p=i.getUint32(e,!1);e=e+4;const y=X(i,e);e=e+1;const k=p-1,x=new Uint8Array(i.buffer.slice(e,e+k));e=e+k,f.push({size:p,header:y,payload:x})}return{frameType:o,codecID:r,avcPacketType:n,cts:d,data:h,nalus:f}}break;default:throw new Error("Unsupported codecID")}return{frameType:o,codecID:r,avcPacketType:n,cts:d,data:h}},parseMetaData:(i,t)=>{let s=t;{if(i.getUint8(s)!==2)throw new Error("Invalid AMF type for onMetaData (expected 0x02)");s=s+1}const e=i.getUint16(s,!1);s=s+2;{const r=new Int8Array(i.buffer.slice(s,s+e));if((T?.decode(r)||"")!=="onMetaData")throw new Error("Expected 'onMetaData' string");s=s+e}const a=U(i,s);return s=s+1,v(i,s,a).value}}}};class Y{parseSpeed=8;pendingPayloads=[];payload=new Uint8Array(0);offset=0;is_parsing=!1;header;tag;on={};constructor(){}init=()=>{this.destroy()};push=t=>{this.pendingPayloads.push(t),this.is_parsing||this.parse()};destroy=()=>{this.pendingPayloads=[],this.payload=new Uint8Array(0),this.offset=0,this.is_parsing=!1,this.header=void 0,this.tag=void 0};parse=async()=>{for(this.is_parsing=!0;;){const t=this.pendingPayloads.shift();if(!t)break;const s=new Uint8Array(this.payload.byteLength+t.byteLength);s.set(this.payload,0),s.set(t,this.payload.byteLength),this.payload=s;const e=new DataView(this.payload.buffer);this.header||this.parseHeader(e),await this.parseTag(e)}this.is_parsing=!1};parseHeader=t=>(this.header={signature:l.header.getSignature(t),version:l.header.getVersion(t),flags:l.header.getFlags(t),dataOffset:l.header.getDataOffset(t)},this.offset=this.header?.dataOffset,this.on.header&&this.on.header(this.header),this.header);parseTag=async t=>{const s=(a,o)=>({tagType:l.tag.tagHeader.getTagType(a,o),dataSize:l.tag.tagHeader.getDataSize(a,o),timestamp:l.tag.tagHeader.getTimestamp(a,o),timestampExtended:l.tag.tagHeader.getTimestampExtended(a,o),streamID:l.tag.tagHeader.getStreamID(a,o)}),e=(a,o,r,n)=>{let d;switch(a){case"script":d=l.tag.tagBody.parseMetaData(o,r);break;case"audio":d=l.tag.tagBody.parseAudio(o,r,n);break;case"video":d=l.tag.tagBody.parseVideo(o,r,n);break}return d};for(;this.offset<t.byteLength;){if(l.isSurplusTag(t,this.offset)===!1){this.payload=this.payload.slice(this.offset),this.offset=0;break}const o=s(t,this.offset+4),{tagType:r,dataSize:n}=o;if(!r)break;const d=e(r,t,this.offset+4+11,n);this.tag={header:o,body:d},this.on.tag&&this.on.tag(this.tag),this.offset=this.offset+4+11+n,await new Promise(u=>setTimeout(()=>u(!0),this.parseSpeed))}}}class Z{audioDecoderConfig;audioDecoder;videoDecoderConfig;videoDecoder;hasKeyFrame=!1;on={audio:{},video:{}};constructor(){}audio={init:t=>{this.audio.destroy(),this.audioDecoderConfig={...t},this.audioDecoder=new AudioDecoder({output:s=>{this.on.audio.decode&&this.on.audio.decode(s)},error:s=>{this.on.audio.error&&this.on.audio.error(s)}}),this.audioDecoder.configure(this.audioDecoderConfig)},decode:t=>{if(!this.audioDecoder)return;const s=new EncodedAudioChunk(t);this.audioDecoder.decode(s)},flush:()=>{this.audioDecoder?.flush()},destroy:()=>{this.audioDecoderConfig=void 0,this.audioDecoder?.close(),this.audioDecoder=void 0}};video={init:t=>{this.video.destroy(),this.videoDecoderConfig={...t},this.videoDecoder=new VideoDecoder({output:async s=>{const e=await createImageBitmap(s),a=s.timestamp;s.close(),e.width>0&&e.height>0?this.on.video.decode&&this.on.video.decode({timestamp:a,bitmap:e}):e.close()},error:s=>{this.on.video.error&&this.on.video.error(s)}}),this.videoDecoder.configure(this.videoDecoderConfig)},decode:t=>{if(this.videoDecoder&&(t.type==="key"&&(this.hasKeyFrame=!0),this.hasKeyFrame&&this.videoDecoder.decodeQueueSize<2)){const s=new EncodedVideoChunk(t);this.videoDecoder.decode(s)}},flush:()=>{this.videoDecoder?.flush()},destroy:()=>{this.videoDecoderConfig=void 0,this.videoDecoder?.close(),this.videoDecoder=void 0,this.hasKeyFrame=!1}}}class ee{isRendering=!1;pendingFrames=[];offscreenCanvas;writable;writer;ctx;cutOption;baseTime=0;pause=!1;shader=["stream"];constructor(){}init=({offscreenCanvas:t,baseTime:s=performance.timeOrigin,writable:e})=>{this.destroy(),this.offscreenCanvas=t,this.writable=e,this.writer=this.writable.getWriter(),this.ctx=this.offscreenCanvas.getContext("2d"),this.baseTime=s};setShader=t=>{this.shader=t};setSize=({width:t,height:s})=>{this.offscreenCanvas&&(this.offscreenCanvas.width=t,this.offscreenCanvas.height=s)};destroy=()=>{this.isRendering=!1,this.pendingFrames=[],this.offscreenCanvas=void 0,this.ctx=void 0,this.baseTime=0};push=t=>{this.pendingFrames.push(t),this.isRendering===!1&&setTimeout(this.renderFrame,0)};setCut=t=>{this.cutOption=t};setPause=t=>{this.pause=t,this.isRendering===!1&&setTimeout(this.renderFrame,0)};calculateTimeUntilNextFrame=t=>{const s=performance.timeOrigin+performance.now(),a=this.baseTime+t/1e3-s;return Math.max(0,a)};renderFrame=async()=>{for(this.isRendering=!0;;){const t=this.pendingFrames.shift();if(!t)break;let{timestamp:s,bitmap:e}=t;if(this.cutOption){const{sx:o=0,sy:r=0,sw:n=e.width,sh:d=e.height}=this.cutOption;e=await createImageBitmap(e,o,r,n,d)}const a=this.calculateTimeUntilNextFrame(s);await new Promise(o=>setTimeout(()=>o(!0),a)),this.drawImage({timestamp:s,bitmap:e}),this.cutOption&&e.close()}this.isRendering=!1};drawImage=t=>{if(this.pause!==!0){if(this.shader.includes("stream")){const s=new VideoFrame(t.bitmap,{timestamp:t.timestamp});this.writer.write(s),s.close()}this.shader.includes("canvas")&&this.ctx&&this.offscreenCanvas&&this.ctx.drawImage(t.bitmap,0,0,this.offscreenCanvas.width,this.offscreenCanvas.height)}}}m.Decoder=Z,m.DecoderWorker=N,m.Demuxer=Y,m.DemuxerWorker=P,m.PrPlayer=J,m.Render=ee,m.RenderWorker=W,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})}));
|
|
3
|
+
`,O=typeof self<"u"&&self.Blob&&new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);",F],{type:"text/javascript;charset=utf-8"});function V(i){let t;try{if(t=O&&(self.URL||self.webkitURL).createObjectURL(O),!t)throw"";const s=new Worker(t,{name:i?.name});return s.addEventListener("error",()=>{(self.URL||self.webkitURL).revokeObjectURL(t)}),s}catch{return new Worker("data:text/javascript;charset=utf-8,"+encodeURIComponent(F),{name:i?.name})}}class W{worker=new V;constructor(){}init=({offscreenCanvas:t,baseTime:s=0,writable:e})=>this.worker.postMessage({action:"init",data:{offscreenCanvas:t,baseTime:s,writable:e}},[t,e]);setShader=t=>this.worker.postMessage({action:"setShader",data:t});setSize=({width:t,height:s})=>this.worker.postMessage({action:"setSize",data:{width:t,height:s}});push=t=>this.worker.postMessage({action:"push",data:t});setCut=async t=>this.worker.postMessage({action:"setCut",data:t});setPause=t=>this.worker.postMessage({action:"setPause",data:t});destroy=()=>{this.worker.postMessage({action:"destroy",data:{}}),this.worker.terminate()}}var j=Object.defineProperty,H=(i,t,s)=>t in i?j(i,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):i[t]=s,c=(i,t,s)=>H(i,typeof t!="symbol"?t+"":t,s);class ${constructor(t,s){c(this,"inputStream",new MediaStream),c(this,"outputStream",new MediaStream),c(this,"inputGain",1),c(this,"enhanceGain",1),c(this,"bgsGain",1),c(this,"bgmGain",1),c(this,"outputGain",1),c(this,"mixAudioMap",new Map),c(this,"audioContext",new AudioContext),c(this,"sourceNode"),c(this,"inputGainNode"),c(this,"enhanceGainNode"),c(this,"bgsGainNode"),c(this,"bgmGainNode"),c(this,"analyserNode"),c(this,"analyserArrayData"),c(this,"outputGainNode"),c(this,"destinationNode"),c(this,"filterStream",e=>e),c(this,"stop",()=>{{const e=this.inputStream.getTracks();for(const a of e)a.stop(),this.inputStream.removeTrack(a)}}),c(this,"getStream",()=>this.filterStream(this.outputStream)),c(this,"setMute",(e=!0)=>{e?this.analyserNode.disconnect(this.outputGainNode):this.analyserNode.connect(this.outputGainNode)}),c(this,"setInputGain",e=>{this.inputGain=e,this.inputGainNode.gain.setValueAtTime(e,this.audioContext.currentTime)}),c(this,"setEnhanceGain",async e=>{this.enhanceGain=e+1,this.enhanceGainNode.gain.setValueAtTime(this.enhanceGain,this.audioContext.currentTime)}),c(this,"setBgsGain",e=>{this.bgsGain=e,this.bgsGainNode.gain.setValueAtTime(e,this.audioContext.currentTime)}),c(this,"setBgmGain",e=>{this.bgmGain=e,this.bgmGainNode.gain.setValueAtTime(e,this.audioContext.currentTime)}),c(this,"setOutputGain",e=>{this.outputGain=e,this.outputGainNode.gain.setValueAtTime(this.outputGain,this.audioContext.currentTime)}),c(this,"getVolume",()=>{const{analyserNode:e,analyserArrayData:a}=this;e.getByteFrequencyData(a);let o=0;for(let r=0;r<a.length;r++)o+=a[r];return Math.ceil(o/a.length)}),c(this,"mixAudio",(e,a="bgm")=>new Promise(async(o,r)=>{try{{const u=this.mixAudioMap.get(a);u&&u.stop()}const n=a==="bgs"?this.bgsGainNode:this.bgmGainNode,d=this.audioContext.createBufferSource();this.mixAudioMap.set(a,d),d.buffer=e,d.connect(n),d.onended=()=>{d.disconnect(n),this.mixAudioMap.delete(a),o(!0)},d.start(0)}catch(n){r(n)}})),c(this,"mixAudioStop",e=>{const a=this.mixAudioMap.get(e);a?.stop()}),c(this,"changeMix",(e,a)=>{const o=e==="bgs"?this.bgsGainNode:this.bgmGainNode;a?o.connect(this.destinationNode):o.disconnect(this.destinationNode)}),s&&(this.audioContext=s),this.inputStream=t,this.sourceNode=this.audioContext.createMediaStreamSource(this.inputStream),this.inputGainNode=this.audioContext.createGain(),this.inputGainNode.gain.setValueAtTime(this.inputGain,this.audioContext.currentTime),this.enhanceGainNode=this.audioContext.createGain(),this.enhanceGainNode.gain.setValueAtTime(this.enhanceGain,this.audioContext.currentTime),this.bgsGainNode=this.audioContext.createGain(),this.bgsGainNode.gain.setValueAtTime(this.bgsGain,this.audioContext.currentTime),this.bgmGainNode=this.audioContext.createGain(),this.bgmGainNode.gain.setValueAtTime(this.bgmGain,this.audioContext.currentTime),this.analyserNode=this.audioContext.createAnalyser(),this.analyserNode.fftSize=512,this.analyserArrayData=new Uint8Array(this.analyserNode.frequencyBinCount),this.outputGainNode=this.audioContext.createGain(),this.outputGainNode.gain.setValueAtTime(this.outputGain,this.audioContext.currentTime),this.destinationNode=this.audioContext.createMediaStreamDestination(),this.outputStream=this.destinationNode.stream;{const{sourceNode:e,inputGainNode:a,enhanceGainNode:o,bgsGainNode:r,bgmGainNode:n,analyserNode:d,outputGainNode:u,destinationNode:h}=this;e.connect(a),a.connect(o),o.connect(d),r.connect(d),n.connect(d),o.connect(h),r.connect(h),n.connect(h),d.connect(u),u.connect(this.audioContext.destination)}this.setMute(!0),this.audioContext.resume()}}const q=async(i,t)=>{try{const{format:s,numberOfChannels:e,numberOfFrames:a,sampleRate:o}=t,r=i.createBuffer(e,a,o);for(let n=0;n<e;n++){const d=t.allocationSize({planeIndex:n}),u=new Uint8Array(d);t.copyTo(u,{planeIndex:n});const h=new DataView(u.buffer),f=r.getChannelData(n);for(let g=0;g<a;g++){let p;switch(s){case"s16":case"s16-planar":p=h.getInt16(g*2,!0)/32768;break;case"f32":case"f32-planar":p=h.getFloat32(g*4,!0);break;case"u8":case"u8-planar":p=(h.getUint8(g)-128)/128;break;default:throw new Error(`Unsupported audio format: ${s}`)}f[g]=Math.max(-1,Math.min(1,p))}}return r}catch(s){throw console.error("Failed to convert AudioData to AudioBuffer:",s),s}};class K{prAudioStream;audioContext;destination;stream=new MediaStream;nextStartTime=0;pendingSources=[];constructor(){}init=t=>{t||(t=new(window.AudioContext||window.webkitAudioContext)),this.audioContext=t,this.destination=this.audioContext.createMediaStreamDestination(),this.stream=new MediaStream,this.stream.addTrack(this.destination.stream.getAudioTracks()[0]),this.prAudioStream=new $(this.stream,this.audioContext),this.nextStartTime=0,this.pendingSources=[]};async push(t){try{if(!this.audioContext||!this.destination)return;const s=await q(this.audioContext,t);if(!s)return;const e=this.audioContext.createBufferSource();e.buffer=s,e.connect(this.destination);const a=Math.max(this.nextStartTime,this.audioContext.currentTime);this.nextStartTime=a+s.duration,e.start(a),this.pendingSources.push(e),e.onended=()=>{this.pendingSources=this.pendingSources.filter(o=>o!==e)},this.audioContext.state==="suspended"&&await this.audioContext.resume()}finally{t.close()}}getStream=()=>this.prAudioStream?.getStream();destroy(){this.audioContext?.close(),this.audioContext=void 0,this.destination=void 0,this.nextStartTime=0,this.prAudioStream?.stop(),this.pendingSources.forEach(t=>t.stop()),this.pendingSources=[]}}class Q{#t={timeout:5*1e3};#e;constructor(t={}){this.#t={...this.#t,...t}}check=(t,s)=>new Promise(async(e,a)=>{this.stop(),this.#e=new AbortController;const o=window.setTimeout(()=>{this.#e?.abort("Timeout."),a({status:"timeout",reason:""})},this.#t.timeout);try{const r=await fetch(t,{...s,method:"HEAD",signal:this.#e?.signal});r.status===200?e({status:"successed",reason:""}):a({status:"failed",reason:`${r.status}`})}catch(r){a({status:"error",reason:r.message})}clearTimeout(o)});request=async(t,s)=>new Promise(async(e,a)=>{try{await this.check(t,s),this.#e=new AbortController;const o=await fetch(t,{...s,signal:this.#e?.signal});e(o)}catch(o){this.stop(),a(o)}});stop=()=>{this.#e?.signal.aborted===!1&&this.#e.abort("Actively stop.")}}const I=i=>{const t=i?.getTracks()||[];for(const s of t)s.stop()},B=i=>{const t=new W,s=document.createElement("canvas"),e=s.transferControlToOffscreen(),a=new MediaStreamTrackGenerator({kind:"video"}),o=new MediaStream([a]),r=()=>{t.destroy(),I(o)};return t.init({offscreenCanvas:e,baseTime:i,writable:a.writable}),{worker:t,canvas:s,stream:o,destroy:r}};class J{prFetch=new Q;demuxerWorker;decoderWorker;audioPlayer;renderWorker;renderBaseTime=0;stream;canvas;on={demuxer:{},decoder:{}};cutRenders=new Map;trackGenerator;constructor(){}init=()=>{this.initDemuxer(),this.initDecoder(),this.audioPlayer=new K,this.audioPlayer.init(),this.initRender()};start=async t=>(this.stop(),this.renderBaseTime=new Date().getTime(),this.init(),this.prFetch.request(t).then(async s=>{const e=s.body?.getReader();if(!e)throw new Error("Reader is error.");const a=()=>{e.read().then(({done:o,value:r})=>{r&&this.demuxerWorker?.push(r),!o&&a()}).catch(o=>{if(o.name!=="AbortError")throw o})};a()}).catch(s=>{console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m","color:#0097ff;","------->Breathe: err",s)}));stop=async()=>{try{this.prFetch.stop()}catch(s){console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m","color:#0097ff;","------->Breathe: error",s)}this.demuxerWorker?.destroy(),this.decoderWorker?.destroy(),this.renderWorker?.destroy(),I(this.stream);const t=[...this.cutRenders.keys()];for(const s of t)this.cut.remove(s);this.audioPlayer?.destroy(),this.renderBaseTime=0,this.canvas=void 0};onTag=t=>{if(!this.decoderWorker)return;const{header:s,body:e}=t,{tagType:a,timestamp:o}=s;switch(a){case"script":{const{width:r,height:n}=e;this.renderWorker?.setSize({width:r,height:n}),this.on.demuxer.script&&this.on.demuxer.script(t)}break;case"audio":{const{accPacketType:r,data:n}=e;if(r===0){const{codec:d,sampleRate:u,channelConfiguration:h}=e,f={codec:d,sampleRate:u,numberOfChannels:h,description:new Uint8Array([])};this.decoderWorker.audio.init(f)}else r===1&&this.decoderWorker.audio.decode({type:"key",timestamp:o*1,data:n});this.on.demuxer.audio&&this.on.demuxer.audio(t)}break;case"video":{const{avcPacketType:r,frameType:n,data:d,nalus:u=[]}=e;if(r===0){const{codec:h,data:f}=e;this.decoderWorker.video.init({codec:h,description:f})}else if(r===1){const h=n===1?"key":"delta";this.decoderWorker.video.decode({type:h,timestamp:o*1e3,data:d});for(const f of u){const{header:g,payload:p}=f,{nal_unit_type:y}=g;y===6&&this.on.demuxer.sei&&this.on.demuxer.sei(p)}}this.on.demuxer.video&&this.on.demuxer.video(t)}break}};initDemuxer=()=>{this.demuxerWorker=new P,this.demuxerWorker.init(),this.demuxerWorker.on.tag=this.onTag};initDecoder=()=>{this.decoderWorker=new N,this.decoderWorker.on.audio.decode=t=>{this.audioPlayer?.push(t),this.on.decoder.audio&&this.on.decoder.audio(t)},this.decoderWorker.on.audio.error=t=>{this.stop(),this.on.error&&this.on.error(t)},this.decoderWorker.on.video.decode=async t=>{this.renderWorker?.push(t);const s=[...this.cutRenders.keys()];for(const e of s)this.cutRenders.get(e)?.worker.push(t);this.on.decoder.video&&this.on.decoder.video(t),t.bitmap.close()},this.decoderWorker.on.video.error=t=>{this.stop(),this.on.error&&this.on.error(t)}};initRender=()=>{const{worker:t,canvas:s,stream:e}=B(this.renderBaseTime);this.renderWorker=t,this.canvas=s,this.stream=e,this.renderWorker.setPause(!1)};getCanvas=()=>this.canvas;getStream=()=>this.stream;setPause=t=>{this.renderWorker?.setPause(t)};setShader=t=>{this.renderWorker?.setShader(t)};setMute=t=>this.audioPlayer?.prAudioStream?.setMute(t);cut={create:(t,s)=>{let e=this.cutRenders.get(t);return e?(e.worker.setCut(s),e.worker.setPause(!1),e):(e=B(this.renderBaseTime),e.worker.setCut(s),this.cutRenders.set(t,e),e)},getCanvas:t=>this.cutRenders.get(t)?.canvas,getStream:t=>this.cutRenders.get(t)?.stream,setPause:(t,s)=>{this.cutRenders.get(t)?.worker.setPause(s)},setShader:(t,s)=>{this.cutRenders.get(t)?.worker.setShader(s)},remove:t=>{this.cutRenders.get(t)?.destroy(),this.cutRenders.delete(t)}}}const T=new TextDecoder("utf-8"),X=(i,t)=>{const s=i.getUint8(t),e=s>>7&1,a=s>>5&3,o=s&31;return{forbidden_zero_bit:e,nal_ref_idc:a,nal_unit_type:o}},U=(i,t)=>i.getUint8(t),z=(i,t,s)=>{const e=new Uint8Array(i.buffer.slice(t,t+s));return T?.decode(e)||""},v=(i,t,s)=>{let e=t,a,o=0;switch(s){case 0:a=i.getFloat64(e,!1),o=8;break;case 1:a=!!i.getUint8(e),o=1;break;case 2:{a="";const n=i.getUint16(e,!1);e=e+2;const d=new Int8Array(i.buffer,e,n).filter(h=>h!==0);a=(T?.decode(d)||"").trim(),o=2+n}break;case 3:for(a={};e<i.byteLength;){const n=i.getUint16(e,!1);if(n===0)break;e=e+2;const d=z(i,e,n);e=e+n;const u=U(i,e);if(u===6)break;e=e+1;const h=v(i,e,u);e=e+h.length,a[d]=h.value,o=2+n+1+h.length}break;case 8:{a={};const n=i.getUint32(e,!1);e=e+4;for(let d=0;d<n;d++){const u=i.getUint16(e,!1);e=e+2;const h=z(i,e,u);e=e+u;const f=U(i,e);e=e+1;const g=v(i,e,f);e=e+g.length,a[h]=g.value,o=2+u+1+g.length}}break;case 10:{a=[];const n=i.getUint32(e,!1);e=e+4;for(let d=0;d<n;d++){const u=U(i,e);e=e+1;const h=v(i,e,u);e=e+h.length,a.push(h.value),o=1+h.length}}break}return{amfType:s,length:o,value:a}},w=(i,t)=>i.getUint8(t)<<16|i.getUint8(t+1)<<8|i.getUint8(t+2),l={header:{getSignature:i=>{const t=new Int8Array(i.buffer.slice(0,3));return T?.decode(t)||""},getVersion:i=>i.getUint8(3),getFlags:i=>{const s=i.getUint8(0).toString(2).padStart(5,"0").split(""),[,,e,,a]=s;return{audio:a==="1",video:e==="1"}},getDataOffset:i=>i.getUint32(5)},getPreviousTagSize:(i,t)=>i.getUint32(t),isSurplusTag:(i,t)=>{let s=!0;const e=i.byteLength;if(t+4>e)s=!1;else if(t+4+11>e)s=!1;else{const a=w(i,t+4+1);t+4+11+a>e&&(s=!1)}return s},tag:{tagHeader:{getTagType:(i,t)=>{const s=i.getUint8(t);let e;switch(s){case 18:e="script";break;case 8:e="audio";break;case 9:e="video";break}return e},getDataSize:(i,t)=>w(i,t+1),getTimestamp:(i,t)=>w(i,t+4),getTimestampExtended:(i,t)=>i.getUint8(t+7),getStreamID:(i,t)=>w(i,t+8)},tagBody:{parseAudio:(i,t,s)=>{let e=t;const a=i.getUint8(e),o=a>>4&15,r=a>>2&3,n=a>>1&1,d=a&1;e=e+1;const u=i.getUint8(e);e=e+1;const h=s-2,f=new Uint8Array(i.buffer.slice(e,e+h));if(o===10&&u===0){const g=i.getUint8(e),p=i.getUint8(e+1),y=(g&248)>>3,k=(g&7)<<1|p>>7,x=(p&120)>>3,S=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350],D=`mp4a.40.${y}`,C=S[k];return{soundFormat:o,soundRate:r,soundSize:n,soundType:d,accPacketType:u,data:f,audioObjectType:y,samplingFrequencyIndex:k,channelConfiguration:x,codec:D,sampleRate:C}}return{soundFormat:o,soundRate:r,soundSize:n,soundType:d,accPacketType:u,data:f}},parseVideo:(i,t,s)=>{let e=t;const a=i.getUint8(e),o=a>>4&15,r=a&15;e=e+1;const n=i.getUint8(e);e=e+1;const d=w(i,e);e=e+3;const u=s-5,h=new Uint8Array(i.buffer.slice(e,e+u));switch(r){case 7:if(n===0){const f=i.getUint8(e);if(e=e+1,f!==1)throw new Error("Invalid AVC version");const g=i.getUint8(e)&255;e=e+1;const p=i.getUint8(e)&255;e=e+1;const y=i.getUint8(e)&255;e=e+1;const S=`avc1.${Array.from([g,p,y],ae=>ae.toString(16).padStart(2,"0")).join("")}`,D=(i.getUint8(e)&3)-1;e=e+1;const C=i.getUint8(e)&31;e=e+1;const A=i.getUint16(e,!1);e=e+2;const te=new Uint8Array(i.buffer.slice(e,e+A));e=e+A;const se=i.getUint8(e)&31;e=e+1;const R=i.getUint16(e,!1);e=e+2;const ie=new Uint8Array(i.buffer.slice(e,e+R));return e=e+R,{frameType:o,codecID:r,avcPacketType:n,cts:d,data:h,version:f,codec:S,profile:g,compatibility:p,level:y,lengthSizeMinusOne:D,numOfSequenceParameterSets:C,sequenceParameterSetLength:A,sps:te,numOfPictureParameterSets:se,pictureParameterSetLength:R,pps:ie}}else if(n===1){const f=[],g=e+s-5;for(;e+4<g;){const p=i.getUint32(e,!1);e=e+4;const y=X(i,e);e=e+1;const k=p-1,x=new Uint8Array(i.buffer.slice(e,e+k));e=e+k,f.push({size:p,header:y,payload:x})}return{frameType:o,codecID:r,avcPacketType:n,cts:d,data:h,nalus:f}}break;default:throw new Error("Unsupported codecID")}return{frameType:o,codecID:r,avcPacketType:n,cts:d,data:h}},parseMetaData:(i,t)=>{let s=t;{if(i.getUint8(s)!==2)throw new Error("Invalid AMF type for onMetaData (expected 0x02)");s=s+1}const e=i.getUint16(s,!1);s=s+2;{const r=new Int8Array(i.buffer.slice(s,s+e));if((T?.decode(r)||"")!=="onMetaData")throw new Error("Expected 'onMetaData' string");s=s+e}const a=U(i,s);return s=s+1,v(i,s,a).value}}}};class Y{parseSpeed=8;pendingPayloads=[];payload=new Uint8Array(0);offset=0;is_parsing=!1;header;tag;on={};constructor(){}init=()=>{this.destroy()};push=t=>{this.pendingPayloads.push(t),this.is_parsing||this.parse()};destroy=()=>{this.pendingPayloads=[],this.payload=new Uint8Array(0),this.offset=0,this.is_parsing=!1,this.header=void 0,this.tag=void 0};parse=async()=>{for(this.is_parsing=!0;;){const t=this.pendingPayloads.shift();if(!t)break;const s=new Uint8Array(this.payload.byteLength+t.byteLength);s.set(this.payload,0),s.set(t,this.payload.byteLength),this.payload=s;const e=new DataView(this.payload.buffer);this.header||this.parseHeader(e),await this.parseTag(e)}this.is_parsing=!1};parseHeader=t=>(this.header={signature:l.header.getSignature(t),version:l.header.getVersion(t),flags:l.header.getFlags(t),dataOffset:l.header.getDataOffset(t)},this.offset=this.header?.dataOffset,this.on.header&&this.on.header(this.header),this.header);parseTag=async t=>{const s=(a,o)=>({tagType:l.tag.tagHeader.getTagType(a,o),dataSize:l.tag.tagHeader.getDataSize(a,o),timestamp:l.tag.tagHeader.getTimestamp(a,o),timestampExtended:l.tag.tagHeader.getTimestampExtended(a,o),streamID:l.tag.tagHeader.getStreamID(a,o)}),e=(a,o,r,n)=>{let d;switch(a){case"script":d=l.tag.tagBody.parseMetaData(o,r);break;case"audio":d=l.tag.tagBody.parseAudio(o,r,n);break;case"video":d=l.tag.tagBody.parseVideo(o,r,n);break}return d};for(;this.offset<t.byteLength;){if(l.isSurplusTag(t,this.offset)===!1){this.payload=this.payload.slice(this.offset),this.offset=0;break}const o=s(t,this.offset+4),{tagType:r,dataSize:n}=o;if(!r)break;const d=e(r,t,this.offset+4+11,n);this.tag={header:o,body:d},this.on.tag&&this.on.tag(this.tag),this.offset=this.offset+4+11+n,await new Promise(u=>setTimeout(()=>u(!0),this.parseSpeed))}}}class Z{audioDecoderConfig;audioDecoder;videoDecoderConfig;videoDecoder;hasKeyFrame=!1;on={audio:{},video:{}};constructor(){}audio={init:t=>{this.audio.destroy(),this.audioDecoderConfig={...t},this.audioDecoder=new AudioDecoder({output:s=>{this.on.audio.decode&&this.on.audio.decode(s)},error:s=>{this.on.audio.error&&this.on.audio.error(s)}}),this.audioDecoder.configure(this.audioDecoderConfig)},decode:t=>{if(!this.audioDecoder)return;const s=new EncodedAudioChunk(t);this.audioDecoder.decode(s)},flush:()=>{this.audioDecoder?.flush()},destroy:()=>{this.audioDecoderConfig=void 0,this.audioDecoder?.close(),this.audioDecoder=void 0}};video={init:t=>{this.video.destroy(),this.videoDecoderConfig={...t},this.videoDecoder=new VideoDecoder({output:async s=>{const e=await createImageBitmap(s),a=s.timestamp;s.close(),e.width>0&&e.height>0?this.on.video.decode&&this.on.video.decode({timestamp:a,bitmap:e}):e.close()},error:s=>{this.on.video.error&&this.on.video.error(s)}}),this.videoDecoder.configure(this.videoDecoderConfig)},decode:t=>{if(this.videoDecoder&&(t.type==="key"&&(this.hasKeyFrame=!0),this.hasKeyFrame&&this.videoDecoder.decodeQueueSize<2)){const s=new EncodedVideoChunk(t);this.videoDecoder.decode(s)}},flush:()=>{this.videoDecoder?.flush()},destroy:()=>{this.videoDecoderConfig=void 0,this.videoDecoder?.close(),this.videoDecoder=void 0,this.hasKeyFrame=!1}}}class ee{isRendering=!1;pendingFrames=[];offscreenCanvas;writable;writer;ctx;cutOption;baseTime=0;pause=!1;shader=["stream"];constructor(){}init=({offscreenCanvas:t,baseTime:s=performance.timeOrigin,writable:e})=>{this.destroy(),this.offscreenCanvas=t,this.writable=e,this.writer=this.writable.getWriter(),this.ctx=this.offscreenCanvas.getContext("2d"),this.baseTime=s};setShader=t=>{this.shader=t};setSize=({width:t,height:s})=>{this.offscreenCanvas&&(this.offscreenCanvas.width=t,this.offscreenCanvas.height=s)};destroy=()=>{this.isRendering=!1,this.pendingFrames=[],this.offscreenCanvas=void 0,this.ctx=void 0,this.baseTime=0};push=t=>{this.pendingFrames.push(t),this.isRendering===!1&&setTimeout(this.renderFrame,0)};setCut=t=>{this.cutOption=t};setPause=t=>{this.pause=t,this.isRendering===!1&&setTimeout(this.renderFrame,0)};calculateTimeUntilNextFrame=t=>{const s=performance.timeOrigin+performance.now(),a=this.baseTime+t/1e3-s;return Math.max(0,a)};renderFrame=async()=>{for(this.isRendering=!0;;){const t=this.pendingFrames.shift();if(!t)break;let{timestamp:s,bitmap:e}=t;if(this.cutOption){const{sx:o=0,sy:r=0,sw:n=e.width,sh:d=e.height}=this.cutOption;e=await createImageBitmap(e,o,r,n,d)}const a=this.calculateTimeUntilNextFrame(s);await new Promise(o=>setTimeout(()=>o(!0),a)),this.drawImage({timestamp:s,bitmap:e}),this.cutOption&&e.close()}this.isRendering=!1};drawImage=t=>{if(this.pause!==!0){if(this.shader.includes("stream")){const s=new VideoFrame(t.bitmap,{timestamp:t.timestamp});this.writer.write(s),s.close()}this.shader.includes("canvas")&&this.ctx&&this.offscreenCanvas&&this.ctx.drawImage(t.bitmap,0,0,this.offscreenCanvas.width,this.offscreenCanvas.height)}}}m.Decoder=Z,m.DecoderWorker=N,m.Demuxer=Y,m.DemuxerWorker=P,m.PrPlayer=J,m.Render=ee,m.RenderWorker=W,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pr-player",
|
|
3
|
-
"description": "
|
|
4
|
-
"version": "0.0
|
|
3
|
+
"description": "对 flv 格式的地址进行解析 并输出 canvas、stream,提供 SEI 回调,以及 cut 等相关能力,以支持根据业务层 SEI 对视频进行剪切渲染。",
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"build": "tsc && vite build"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"pr-audio-stream": "^0.1.
|
|
22
|
+
"pr-audio-stream": "^0.1.3",
|
|
23
23
|
"pr-fetch": "^0.0.1"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|