-
Notifications
You must be signed in to change notification settings - Fork 454
/
FlvContainer.js
116 lines (103 loc) · 3.75 KB
/
FlvContainer.js
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
/**
* Author: DrowsyFlesh
* Create: 2019/2/4
* Description:
*/
/* flv容器,用来处理分段flv下载合并任务 */
import _ from 'lodash';
import URL from 'url-parse';
import fetchProgress from 'fetch-progress';
import {fetchFromHelper} from 'Utils/functions';
import {DataBase} from './lib/DataBase';
const UPDATE_INTERVAL = 700;
export class FlvFragment {
constructor(db, cid, quality, {size, length, url, order}) {
this.size = size;
this.length = length;
this.url = new URL(url);
this.url.protocol = 'https:';
this.order = order;
this.quality = quality;
this.db = db;
this.cid = cid;
this.downloaded = false;
this.progress = {percentage: 0, total: 0, transferred: 0, speed: 0, remaining: 0};
}
download = () => {
return new Promise((resolve) => {
const res = this.db.get({order: this.order, quality: this.quality});
res.then((blob) => {
this.downloaded = true;
this.progress.percentage = 100;
this.progress.total = blob.size;
resolve(blob);
}, () => {
fetchFromHelper(this.url.toString(), {
method: 'get',
mode: 'cors',
})
.then(fetchProgress({
onProgress: _.throttle((progress) => {
this.progress = progress;
if (progress.percentage === 100) this.downloaded = true;
}, UPDATE_INTERVAL),
}))
.then(response => response.blob())
.then((blob) => {
//this.db.add({order: this.order, quality: this.quality, blob});
resolve(blob);
});
});
});
};
}
export class FlvContainer {
constructor({durl, format, quality, cid}) {
this.durl = durl;
this.format = format;
this.quality = quality;
this.cid = cid;
this.initIndexDataBase();
this.initFragment();
}
// 返回容器内是否所有片段都下载完毕的状态
get downloaded() {
const downloadedNum = _.compact(this.fragments.map((fragment) => fragment.downloaded)).length;
return downloadedNum === this.fragments.length;
}
get percentage() {
let total = 0;
let transferred = 0;
this.fragments.forEach((fragment) => {
total += fragment.size;
transferred += fragment.progress.transferred || (fragment.size * fragment.progress.percentage / 100) || 0;
});
return Math.round(transferred / total * 100);
}
initIndexDataBase = () => {
if (!window.indexedDB) console.error('Your browser is not support feature: indexedDB');
this.db = new DataBase(this.cid);
};
initFragment = () => {
this.fragments = this.durl.map((data) => new FlvFragment(this.db, this.cid, this.quality, data));
};
download = (callback = () => {}) => {
if (this.downloading) return Promise.reject();
this.downloading = true;
return new Promise((resolve, reject) => {
const blobsPromise = Promise.all(this.fragments.map((fragment) => fragment.download()))
.catch(e => reject(e));
const intervalNum = setInterval(() => {
if (this.percentage === 100) {
clearInterval(intervalNum);
this.downloading = false;
blobsPromise.then(blobs => resolve(blobs));
}
callback(this.percentage);
}, UPDATE_INTERVAL);
});
};
clear = (quality) => {
return this.db.clear(quality);
};
}