-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathMediaPlayerManager.js
More file actions
301 lines (268 loc) · 9.29 KB
/
MediaPlayerManager.js
File metadata and controls
301 lines (268 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import Hls from 'hls.js';
import flvjs from 'flv.js';
import shaka from 'shaka-player/dist/shaka-player.compiled';
import { getCSPConfig } from '@/utils/csp';
// 播放器配置选项
const playerOptions = {
hls: {
maxBufferLength: 600, // 缓冲区最大长度
liveSyncDurationCount: 10, // 直播同步持续时间计数
},
flv: {
mediaDataSource: {
type: 'flv',
isLive: false,
},
optionalConfig: {
enableWorker: false, // 启用分离线程
enableStashBuffer: false, // 关闭IO隐藏缓冲区
autoCleanupSourceBuffer: true, // 自动清除缓存
reuseRedirectedURL: true, // 允许重定向请求
fixAudioTimestampGap: false, // 音视频同步
deferLoadAfterSourceOpen: false, // 允许延迟加载
headers: {},
},
},
dash: {},
};
// 视频格式检测
export const detectVideoFormat = (url) => {
// 确保url是字符串类型
if (typeof url !== 'string') {
console.warn('detectVideoFormat: url must be a string, received:', typeof url, url);
return 'native';
}
const urlLower = url.toLowerCase();
if (urlLower.includes('.m3u8') || urlLower.includes('m3u8')) {
return 'hls';
} else if (urlLower.includes('.flv') || urlLower.includes('flv')) {
return 'flv';
} else if (urlLower.includes('.mpd') || urlLower.includes('mpd')) {
return 'dash';
} else if (urlLower.includes('.ts') || urlLower.includes('ts')) {
return 'mpegts';
} else if (urlLower.includes('magnet:') || urlLower.includes('.torrent')) {
return 'torrent';
}
return 'native';
};
// 自定义播放器创建函数
export const createCustomPlayer = {
// HLS 播放器
hls: (video, url, headers = {}) => {
console.log('🎬 [HLS播放器] 开始播放视频:')
console.log('📺 视频地址:', url)
console.log('📋 请求头:', headers)
if (Hls.isSupported()) {
const options = Object.assign({}, { ...playerOptions.hls });
// 设置 XHR 配置,处理 referer 和自定义 headers
options.xhrSetup = function (xhr, _url) {
// 检查 CSP 绕过开关状态,只有开启时才禁用 referer 发送
const cspConfig = getCSPConfig();
if (cspConfig.autoBypass) {
Object.defineProperty(xhr, 'referrer', {
value: '',
writable: false
});
}
// 设置自定义请求头
if (Object.keys(headers).length > 0) {
for (const key in headers) {
xhr.setRequestHeader(key, headers[key]);
}
}
};
const hls = new Hls(options);
hls.loadSource(url);
hls.attachMedia(video);
return hls;
} else {
console.log('HLS is not supported.');
return null;
}
},
// FLV 播放器
flv: (video, url, headers = {}) => {
console.log('🎬 [FLV播放器] 开始播放视频:')
console.log('📺 视频地址:', url)
console.log('📋 请求头:', headers)
if (flvjs.isSupported()) {
const flvPlayer = flvjs.createPlayer(
Object.assign({}, { ...playerOptions.flv.mediaDataSource }, { url: url }),
Object.assign({}, { ...playerOptions.flv.optionalConfig }, { headers }),
);
flvPlayer.attachMediaElement(video);
flvPlayer.load();
return flvPlayer;
} else {
console.log('FLV is not supported.');
return null;
}
},
// DASH 播放器
dash: (video, url, headers = {}) => {
console.log('🎬 [DASH播放器] 开始播放视频:')
console.log('📺 视频地址:', url)
console.log('📋 请求头:', headers)
if (shaka.Player.isBrowserSupported()) {
const playerShaka = new shaka.Player(video);
// 设置请求过滤器处理自定义请求头
playerShaka.getNetworkingEngine().registerRequestFilter(function (type, request) {
if (type != shaka.net.NetworkingEngine.RequestType.MANIFEST) {
return;
}
for (const header in headers) {
request.headers[header] = headers[header];
}
});
playerShaka.load(url);
const options = playerOptions.dash;
playerShaka.configure(options);
return playerShaka;
} else {
console.log('DASH is not supported.');
return null;
}
},
};
// 播放器切换函数
export const switchCustomPlayer = {
hls: (video, hls, url) => {
hls.stopLoad();
hls.detachMedia();
// 重新加载新的 M3U8 URL
hls.loadSource(url);
hls.attachMedia(video);
// 等待新流解析完成并开始播放
hls.once(Hls.Events.MANIFEST_PARSED, () => {
video.play();
});
return hls;
},
flv: (video, flv, url) => {
flv.pause();
flv.unload();
flv.detachMediaElement();
flv.destroy();
flv = flvjs.createPlayer(
Object.assign({}, playerOptions.flv.mediaDataSource || {}, { url: url }),
playerOptions.flv.optionalConfig || {},
);
flv.attachMediaElement(video);
flv.load();
return flv;
},
dash: (video, dash, url) => {
dash.destroy();
const playerShaka = new shaka.Player(video);
playerShaka.load(url);
const options = playerOptions.dash;
playerShaka.configure(options);
return playerShaka;
},
};
// 播放器销毁函数
export const destroyCustomPlayer = {
hls: (player) => {
if (player?.hls) {
player.hls.destroy();
delete player.hls;
}
},
flv: (player) => {
if (player?.flv) {
player.flv.destroy();
delete player.flv;
}
},
dash: (player) => {
if (player?.dash) {
player.dash.destroy();
delete player.dash;
}
},
};
// 统一的播放器管理器
export class MediaPlayerManager {
constructor(video) {
this.video = video;
this.currentPlayer = null;
this.currentFormat = 'native';
}
// 加载视频
loadVideo(url, headers = {}) {
const format = detectVideoFormat(url);
// 如果格式改变,先销毁当前播放器
if (this.currentFormat !== format && this.currentPlayer) {
this.destroy();
}
this.currentFormat = format;
switch (format) {
case 'hls':
this.currentPlayer = createCustomPlayer.hls(this.video, url, headers);
break;
case 'flv':
this.currentPlayer = createCustomPlayer.flv(this.video, url, headers);
break;
case 'dash':
this.currentPlayer = createCustomPlayer.dash(this.video, url, headers);
break;
default:
// 原生支持的格式
console.log('🎬 [原生播放器] 开始播放视频:')
console.log('📺 视频地址:', url)
console.log('📋 请求头:', headers)
this.video.src = url;
this.currentPlayer = null;
break;
}
return this.currentPlayer;
}
// 切换视频源
switchVideo(url) {
const format = detectVideoFormat(url);
if (format === this.currentFormat && this.currentPlayer) {
// 相同格式,使用切换函数
switch (format) {
case 'hls':
this.currentPlayer = switchCustomPlayer.hls(this.video, this.currentPlayer, url);
break;
case 'flv':
this.currentPlayer = switchCustomPlayer.flv(this.video, this.currentPlayer, url);
break;
case 'dash':
this.currentPlayer = switchCustomPlayer.dash(this.video, this.currentPlayer, url);
break;
}
} else {
// 不同格式,重新加载
this.loadVideo(url);
}
}
// 销毁播放器
destroy() {
if (this.currentPlayer) {
switch (this.currentFormat) {
case 'hls':
destroyCustomPlayer.hls({ hls: this.currentPlayer });
break;
case 'flv':
destroyCustomPlayer.flv({ flv: this.currentPlayer });
break;
case 'dash':
destroyCustomPlayer.dash({ dash: this.currentPlayer });
break;
}
this.currentPlayer = null;
}
this.currentFormat = 'native';
}
// 获取当前播放器
getCurrentPlayer() {
return this.currentPlayer;
}
// 获取当前格式
getCurrentFormat() {
return this.currentFormat;
}
}