diff options
author | Chocobozzz <me@florianbigard.com> | 2019-01-23 15:36:45 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-02-11 09:13:02 +0100 |
commit | 2adfc7ea9a1f858db874df9fe322e7ae833db77c (patch) | |
tree | e27c6ebe01b7c96ea0e053839a38fc1f824d1284 /client/src/assets/player/webtorrent | |
parent | 7eeb6a0ba4028d0e20847b846332dd0b7747c7f8 (diff) | |
download | PeerTube-2adfc7ea9a1f858db874df9fe322e7ae833db77c.tar.gz PeerTube-2adfc7ea9a1f858db874df9fe322e7ae833db77c.tar.zst PeerTube-2adfc7ea9a1f858db874df9fe322e7ae833db77c.zip |
Refractor videojs player
Add fake p2p-media-loader plugin
Diffstat (limited to 'client/src/assets/player/webtorrent')
-rw-r--r-- | client/src/assets/player/webtorrent/peertube-chunk-store.ts | 231 | ||||
-rw-r--r-- | client/src/assets/player/webtorrent/video-renderer.ts | 134 |
2 files changed, 365 insertions, 0 deletions
diff --git a/client/src/assets/player/webtorrent/peertube-chunk-store.ts b/client/src/assets/player/webtorrent/peertube-chunk-store.ts new file mode 100644 index 000000000..54cc0ea64 --- /dev/null +++ b/client/src/assets/player/webtorrent/peertube-chunk-store.ts | |||
@@ -0,0 +1,231 @@ | |||
1 | // From https://github.com/MinEduTDF/idb-chunk-store | ||
2 | // We use temporary IndexDB (all data are removed on destroy) to avoid RAM issues | ||
3 | // Thanks @santiagogil and @Feross | ||
4 | |||
5 | import { EventEmitter } from 'events' | ||
6 | import Dexie from 'dexie' | ||
7 | |||
8 | class ChunkDatabase extends Dexie { | ||
9 | chunks: Dexie.Table<{ id: number, buf: Buffer }, number> | ||
10 | |||
11 | constructor (dbname: string) { | ||
12 | super(dbname) | ||
13 | |||
14 | this.version(1).stores({ | ||
15 | chunks: 'id' | ||
16 | }) | ||
17 | } | ||
18 | } | ||
19 | |||
20 | class ExpirationDatabase extends Dexie { | ||
21 | databases: Dexie.Table<{ name: string, expiration: number }, number> | ||
22 | |||
23 | constructor () { | ||
24 | super('webtorrent-expiration') | ||
25 | |||
26 | this.version(1).stores({ | ||
27 | databases: 'name,expiration' | ||
28 | }) | ||
29 | } | ||
30 | } | ||
31 | |||
32 | export class PeertubeChunkStore extends EventEmitter { | ||
33 | private static readonly BUFFERING_PUT_MS = 1000 | ||
34 | private static readonly CLEANER_INTERVAL_MS = 1000 * 60 // 1 minute | ||
35 | private static readonly CLEANER_EXPIRATION_MS = 1000 * 60 * 5 // 5 minutes | ||
36 | |||
37 | chunkLength: number | ||
38 | |||
39 | private pendingPut: { id: number, buf: Buffer, cb: Function }[] = [] | ||
40 | // If the store is full | ||
41 | private memoryChunks: { [ id: number ]: Buffer | true } = {} | ||
42 | private databaseName: string | ||
43 | private putBulkTimeout: any | ||
44 | private cleanerInterval: any | ||
45 | private db: ChunkDatabase | ||
46 | private expirationDB: ExpirationDatabase | ||
47 | private readonly length: number | ||
48 | private readonly lastChunkLength: number | ||
49 | private readonly lastChunkIndex: number | ||
50 | |||
51 | constructor (chunkLength: number, opts: any) { | ||
52 | super() | ||
53 | |||
54 | this.databaseName = 'webtorrent-chunks-' | ||
55 | |||
56 | if (!opts) opts = {} | ||
57 | if (opts.torrent && opts.torrent.infoHash) this.databaseName += opts.torrent.infoHash | ||
58 | else this.databaseName += '-default' | ||
59 | |||
60 | this.setMaxListeners(100) | ||
61 | |||
62 | this.chunkLength = Number(chunkLength) | ||
63 | if (!this.chunkLength) throw new Error('First argument must be a chunk length') | ||
64 | |||
65 | this.length = Number(opts.length) || Infinity | ||
66 | |||
67 | if (this.length !== Infinity) { | ||
68 | this.lastChunkLength = (this.length % this.chunkLength) || this.chunkLength | ||
69 | this.lastChunkIndex = Math.ceil(this.length / this.chunkLength) - 1 | ||
70 | } | ||
71 | |||
72 | this.db = new ChunkDatabase(this.databaseName) | ||
73 | // Track databases that expired | ||
74 | this.expirationDB = new ExpirationDatabase() | ||
75 | |||
76 | this.runCleaner() | ||
77 | } | ||
78 | |||
79 | put (index: number, buf: Buffer, cb: (err?: Error) => void) { | ||
80 | const isLastChunk = (index === this.lastChunkIndex) | ||
81 | if (isLastChunk && buf.length !== this.lastChunkLength) { | ||
82 | return this.nextTick(cb, new Error('Last chunk length must be ' + this.lastChunkLength)) | ||
83 | } | ||
84 | if (!isLastChunk && buf.length !== this.chunkLength) { | ||
85 | return this.nextTick(cb, new Error('Chunk length must be ' + this.chunkLength)) | ||
86 | } | ||
87 | |||
88 | // Specify we have this chunk | ||
89 | this.memoryChunks[index] = true | ||
90 | |||
91 | // Add it to the pending put | ||
92 | this.pendingPut.push({ id: index, buf, cb }) | ||
93 | // If it's already planned, return | ||
94 | if (this.putBulkTimeout) return | ||
95 | |||
96 | // Plan a future bulk insert | ||
97 | this.putBulkTimeout = setTimeout(async () => { | ||
98 | const processing = this.pendingPut | ||
99 | this.pendingPut = [] | ||
100 | this.putBulkTimeout = undefined | ||
101 | |||
102 | try { | ||
103 | await this.db.transaction('rw', this.db.chunks, () => { | ||
104 | return this.db.chunks.bulkPut(processing.map(p => ({ id: p.id, buf: p.buf }))) | ||
105 | }) | ||
106 | } catch (err) { | ||
107 | console.log('Cannot bulk insert chunks. Store them in memory.', { err }) | ||
108 | |||
109 | processing.forEach(p => this.memoryChunks[ p.id ] = p.buf) | ||
110 | } finally { | ||
111 | processing.forEach(p => p.cb()) | ||
112 | } | ||
113 | }, PeertubeChunkStore.BUFFERING_PUT_MS) | ||
114 | } | ||
115 | |||
116 | get (index: number, opts: any, cb: (err?: Error, buf?: Buffer) => void): void { | ||
117 | if (typeof opts === 'function') return this.get(index, null, opts) | ||
118 | |||
119 | // IndexDB could be slow, use our memory index first | ||
120 | const memoryChunk = this.memoryChunks[index] | ||
121 | if (memoryChunk === undefined) { | ||
122 | const err = new Error('Chunk not found') as any | ||
123 | err['notFound'] = true | ||
124 | |||
125 | return process.nextTick(() => cb(err)) | ||
126 | } | ||
127 | |||
128 | // Chunk in memory | ||
129 | if (memoryChunk !== true) return cb(null, memoryChunk) | ||
130 | |||
131 | // Chunk in store | ||
132 | this.db.transaction('r', this.db.chunks, async () => { | ||
133 | const result = await this.db.chunks.get({ id: index }) | ||
134 | if (result === undefined) return cb(null, new Buffer(0)) | ||
135 | |||
136 | const buf = result.buf | ||
137 | if (!opts) return this.nextTick(cb, null, buf) | ||
138 | |||
139 | const offset = opts.offset || 0 | ||
140 | const len = opts.length || (buf.length - offset) | ||
141 | return cb(null, buf.slice(offset, len + offset)) | ||
142 | }) | ||
143 | .catch(err => { | ||
144 | console.error(err) | ||
145 | return cb(err) | ||
146 | }) | ||
147 | } | ||
148 | |||
149 | close (cb: (err?: Error) => void) { | ||
150 | return this.destroy(cb) | ||
151 | } | ||
152 | |||
153 | async destroy (cb: (err?: Error) => void) { | ||
154 | try { | ||
155 | if (this.pendingPut) { | ||
156 | clearTimeout(this.putBulkTimeout) | ||
157 | this.pendingPut = null | ||
158 | } | ||
159 | if (this.cleanerInterval) { | ||
160 | clearInterval(this.cleanerInterval) | ||
161 | this.cleanerInterval = null | ||
162 | } | ||
163 | |||
164 | if (this.db) { | ||
165 | await this.db.close() | ||
166 | |||
167 | await this.dropDatabase(this.databaseName) | ||
168 | } | ||
169 | |||
170 | if (this.expirationDB) { | ||
171 | await this.expirationDB.close() | ||
172 | this.expirationDB = null | ||
173 | } | ||
174 | |||
175 | return cb() | ||
176 | } catch (err) { | ||
177 | console.error('Cannot destroy peertube chunk store.', err) | ||
178 | return cb(err) | ||
179 | } | ||
180 | } | ||
181 | |||
182 | private runCleaner () { | ||
183 | this.checkExpiration() | ||
184 | |||
185 | this.cleanerInterval = setInterval(async () => { | ||
186 | this.checkExpiration() | ||
187 | }, PeertubeChunkStore.CLEANER_INTERVAL_MS) | ||
188 | } | ||
189 | |||
190 | private async checkExpiration () { | ||
191 | let databasesToDeleteInfo: { name: string }[] = [] | ||
192 | |||
193 | try { | ||
194 | await this.expirationDB.transaction('rw', this.expirationDB.databases, async () => { | ||
195 | // Update our database expiration since we are alive | ||
196 | await this.expirationDB.databases.put({ | ||
197 | name: this.databaseName, | ||
198 | expiration: new Date().getTime() + PeertubeChunkStore.CLEANER_EXPIRATION_MS | ||
199 | }) | ||
200 | |||
201 | const now = new Date().getTime() | ||
202 | databasesToDeleteInfo = await this.expirationDB.databases.where('expiration').below(now).toArray() | ||
203 | }) | ||
204 | } catch (err) { | ||
205 | console.error('Cannot update expiration of fetch expired databases.', err) | ||
206 | } | ||
207 | |||
208 | for (const databaseToDeleteInfo of databasesToDeleteInfo) { | ||
209 | await this.dropDatabase(databaseToDeleteInfo.name) | ||
210 | } | ||
211 | } | ||
212 | |||
213 | private async dropDatabase (databaseName: string) { | ||
214 | const dbToDelete = new ChunkDatabase(databaseName) | ||
215 | console.log('Destroying IndexDB database %s.', databaseName) | ||
216 | |||
217 | try { | ||
218 | await dbToDelete.delete() | ||
219 | |||
220 | await this.expirationDB.transaction('rw', this.expirationDB.databases, () => { | ||
221 | return this.expirationDB.databases.where({ name: databaseName }).delete() | ||
222 | }) | ||
223 | } catch (err) { | ||
224 | console.error('Cannot delete %s.', databaseName, err) | ||
225 | } | ||
226 | } | ||
227 | |||
228 | private nextTick <T> (cb: (err?: Error, val?: T) => void, err: Error, val?: T) { | ||
229 | process.nextTick(() => cb(err, val), undefined) | ||
230 | } | ||
231 | } | ||
diff --git a/client/src/assets/player/webtorrent/video-renderer.ts b/client/src/assets/player/webtorrent/video-renderer.ts new file mode 100644 index 000000000..a3415937b --- /dev/null +++ b/client/src/assets/player/webtorrent/video-renderer.ts | |||
@@ -0,0 +1,134 @@ | |||
1 | // Thanks: https://github.com/feross/render-media | ||
2 | // TODO: use render-media once https://github.com/feross/render-media/issues/32 is fixed | ||
3 | |||
4 | const MediaElementWrapper = require('mediasource') | ||
5 | import { extname } from 'path' | ||
6 | const videostream = require('videostream') | ||
7 | |||
8 | const VIDEOSTREAM_EXTS = [ | ||
9 | '.m4a', | ||
10 | '.m4v', | ||
11 | '.mp4' | ||
12 | ] | ||
13 | |||
14 | type RenderMediaOptions = { | ||
15 | controls: boolean | ||
16 | autoplay: boolean | ||
17 | } | ||
18 | |||
19 | function renderVideo ( | ||
20 | file: any, | ||
21 | elem: HTMLVideoElement, | ||
22 | opts: RenderMediaOptions, | ||
23 | callback: (err: Error, renderer: any) => void | ||
24 | ) { | ||
25 | validateFile(file) | ||
26 | |||
27 | return renderMedia(file, elem, opts, callback) | ||
28 | } | ||
29 | |||
30 | function renderMedia (file: any, elem: HTMLVideoElement, opts: RenderMediaOptions, callback: (err: Error, renderer?: any) => void) { | ||
31 | const extension = extname(file.name).toLowerCase() | ||
32 | let preparedElem: any = undefined | ||
33 | let currentTime = 0 | ||
34 | let renderer: any | ||
35 | |||
36 | try { | ||
37 | if (VIDEOSTREAM_EXTS.indexOf(extension) >= 0) { | ||
38 | renderer = useVideostream() | ||
39 | } else { | ||
40 | renderer = useMediaSource() | ||
41 | } | ||
42 | } catch (err) { | ||
43 | return callback(err) | ||
44 | } | ||
45 | |||
46 | function useVideostream () { | ||
47 | prepareElem() | ||
48 | preparedElem.addEventListener('error', function onError (err: Error) { | ||
49 | preparedElem.removeEventListener('error', onError) | ||
50 | |||
51 | return callback(err) | ||
52 | }) | ||
53 | preparedElem.addEventListener('loadstart', onLoadStart) | ||
54 | return videostream(file, preparedElem) | ||
55 | } | ||
56 | |||
57 | function useMediaSource (useVP9 = false) { | ||
58 | const codecs = getCodec(file.name, useVP9) | ||
59 | |||
60 | prepareElem() | ||
61 | preparedElem.addEventListener('error', function onError (err: Error) { | ||
62 | preparedElem.removeEventListener('error', onError) | ||
63 | |||
64 | // Try with vp9 before returning an error | ||
65 | if (codecs.indexOf('vp8') !== -1) return fallbackToMediaSource(true) | ||
66 | |||
67 | return callback(err) | ||
68 | }) | ||
69 | preparedElem.addEventListener('loadstart', onLoadStart) | ||
70 | |||
71 | const wrapper = new MediaElementWrapper(preparedElem) | ||
72 | const writable = wrapper.createWriteStream(codecs) | ||
73 | file.createReadStream().pipe(writable) | ||
74 | |||
75 | if (currentTime) preparedElem.currentTime = currentTime | ||
76 | |||
77 | return wrapper | ||
78 | } | ||
79 | |||
80 | function fallbackToMediaSource (useVP9 = false) { | ||
81 | if (useVP9 === true) console.log('Falling back to media source with VP9 enabled.') | ||
82 | else console.log('Falling back to media source..') | ||
83 | |||
84 | useMediaSource(useVP9) | ||
85 | } | ||
86 | |||
87 | function prepareElem () { | ||
88 | if (preparedElem === undefined) { | ||
89 | preparedElem = elem | ||
90 | |||
91 | preparedElem.addEventListener('progress', function () { | ||
92 | currentTime = elem.currentTime | ||
93 | }) | ||
94 | } | ||
95 | } | ||
96 | |||
97 | function onLoadStart () { | ||
98 | preparedElem.removeEventListener('loadstart', onLoadStart) | ||
99 | if (opts.autoplay) preparedElem.play() | ||
100 | |||
101 | callback(null, renderer) | ||
102 | } | ||
103 | } | ||
104 | |||
105 | function validateFile (file: any) { | ||
106 | if (file == null) { | ||
107 | throw new Error('file cannot be null or undefined') | ||
108 | } | ||
109 | if (typeof file.name !== 'string') { | ||
110 | throw new Error('missing or invalid file.name property') | ||
111 | } | ||
112 | if (typeof file.createReadStream !== 'function') { | ||
113 | throw new Error('missing or invalid file.createReadStream property') | ||
114 | } | ||
115 | } | ||
116 | |||
117 | function getCodec (name: string, useVP9 = false) { | ||
118 | const ext = extname(name).toLowerCase() | ||
119 | if (ext === '.mp4') { | ||
120 | return 'video/mp4; codecs="avc1.640029, mp4a.40.5"' | ||
121 | } | ||
122 | |||
123 | if (ext === '.webm') { | ||
124 | if (useVP9 === true) return 'video/webm; codecs="vp9, opus"' | ||
125 | |||
126 | return 'video/webm; codecs="vp8, vorbis"' | ||
127 | } | ||
128 | |||
129 | return undefined | ||
130 | } | ||
131 | |||
132 | export { | ||
133 | renderVideo | ||
134 | } | ||