Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

Commit

Permalink
fix piped streams not being destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaUnknown committed Aug 8, 2021
1 parent eb97110 commit c04c979
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bundle.js.map

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ export default class WebTorrentPlayer extends WebTorrent {
const [port] = event.ports
const file = this.get(infoHash) && this.get(infoHash).files.find(file => file.path === filePath)
if (!file) return null
const [response, stream] = this.serveFile(file, data)
const [response, stream, raw] = this.serveFile(file, data)
const asyncIterator = stream && stream[Symbol.asyncIterator]()
port.postMessage(response)
stream.on('close', () => stream.destroy())
stream.on('error', () => stream.destroy())

stream.on('close', () => raw.destroy())
stream.on('error', () => raw.destroy())

this.workerPortCount++
port.onmessage = async msg => {
Expand All @@ -59,7 +60,7 @@ export default class WebTorrentPlayer extends WebTorrent {
if (!chunk) port.onmessage = null
if (!this.workerKeepAliveInterval) this.workerKeepAliveInterval = setInterval(() => fetch(`${this.worker.scriptURL.substr(0, this.worker.scriptURL.lastIndexOf('/') + 1).slice(window.location.origin.length)}webtorrent/keepalive/`), keepAliveTime)
} else {
stream.destroy()
raw.destroy()
port.onmessage = null
this.workerPortCount--
if (!this.workerPortCount) {
Expand All @@ -75,7 +76,7 @@ export default class WebTorrentPlayer extends WebTorrent {
// playPause, playNext, playLast, openPlaylist, toggleMute, setVolume, setProgress, selectCaptions, selectAudio, toggleTheatre, toggleFullscreen, togglePopout, forward, rewind

if (this.controls.setVolume) {
this.controls.setVolume.addEventListener('input', (e) => this.setVolume(e.target.value))
this.controls.setVolume.addEventListener('input', e => this.setVolume(e.target.value))
this.setVolume()
this.oldVolume = undefined
if ('audioTracks' in HTMLVideoElement.prototype && this.controls.audioButton) {
Expand Down Expand Up @@ -362,7 +363,7 @@ Style: Default,${options.defaultSSAStyles || 'Roboto Medium,26,&H00FFFFFF,&H0000
stream.pipe(this.subtitleData.stream)
}

return [res, req.method === 'GET' && (this.subtitleData.stream || stream)]
return [res, req.method === 'GET' && (this.subtitleData.stream || stream), req.method === 'GET' && stream]
}

async buildVideo (torrent, opts = {}) { // sets video source and creates a bunch of other media stuff
Expand Down
23 changes: 9 additions & 14 deletions lib/subtitles-octopus.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ class SubtitlesOctopus {
this.canvas = options.canvas // HTML canvas element (optional if video specified)

// choose best render mode based on browser support
this.renderMode = options.renderMode || 'fast'
if (typeof createImageBitmap === 'undefined') {
if (this.renderMode !== 'blend') this.renderMode = 'normal'
} else {
if (typeof OffscreenCanvas !== 'undefined') {
if (this.renderMode === 'fast') this.renderMode = 'offscreen'
} else if (this.renderMode !== 'blend') {
this.renderMode = 'fast'
this.renderMode = options.renderMode || 'offscreen'
if (this.renderMode !== 'blend' && this.renderMode !== 'normal') {
if (typeof createImageBitmap === 'undefined') {
this.renderMode = 'normal'
} else if (typeof OffscreenCanvas !== 'undefined') {
this.renderMode = 'offscreen'
}
}

Expand Down Expand Up @@ -203,9 +201,7 @@ class SubtitlesOctopus {
this.setRate(video.playbackRate)
}, false)

this.video.addEventListener('waiting', () => {
this.setCurrentTime(true, video.currentTime + this.timeOffset)
}, false)
this.video.addEventListener('waiting', timeupdate, false)

// Support Element Resize Observer
if (typeof ResizeObserver !== 'undefined') {
Expand All @@ -216,7 +212,7 @@ class SubtitlesOctopus {
if (this.video.videoWidth > 0) {
this.resize()
} else {
this.video.addEventListener('loadedmetadata', e => {
this.video.addEventListener('loadedmetadata', () => {
this.resize()
}, false)
}
Expand Down Expand Up @@ -633,7 +629,6 @@ class SubtitlesOctopus {
this.resetRenderAheadCache(true)
}

// Message retardism
onWorkerMessage (event) {
if (!this.workerActive) {
this.workerActive = true
Expand Down Expand Up @@ -823,4 +818,4 @@ class SubtitlesOctopus {

if (typeof module !== 'undefined') {
module.exports = SubtitlesOctopus
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webtorrent-player",
"version": "1.2.3",
"version": "1.2.4",
"description": "High performance, no compromise video player for WebTorrent",
"main": "index.js",
"directories": {
Expand All @@ -9,6 +9,7 @@
},
"scripts": {
"test": "standard",
"start": "webpack --watch /index.js",
"build": "webpack"
},
"repository": {
Expand Down

0 comments on commit c04c979

Please sign in to comment.