Skip to content

Commit

Permalink
Update index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyiya authored Jun 3, 2024
1 parent 96e0989 commit d37e40d
Showing 1 changed file with 52 additions and 32 deletions.
84 changes: 52 additions & 32 deletions packages/torrent/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import webtorrent from 'webtorrent/webtorrent.min.js'
import type { Player, PlayerPlugin, Source } from '@oplayer/core'
import { loadSDK, type Player, type PlayerPlugin, type Source } from '@oplayer/core'
import type Webtorrent from 'webtorrent'

export type PluginOptions = {
config?: Record<string, any>
matcher?: (src: Source) => boolean
/**
* https://cdn.jsdelivr.net/npm/webtorrent@0.98.18/webtorrent.min.js
*/
library?: string
}

class TorrentPlugin implements PlayerPlugin {
Expand All @@ -16,6 +19,8 @@ class TorrentPlugin implements PlayerPlugin {
static defaultMatcher: PluginOptions['matcher'] = (source) =>
/magnet:?[^\"]+/.test(source.src) || /.*\.torrent/.test(source.src)

static library: Webtorrent.WebTorrent

player!: Player

instance: Webtorrent.Instance
Expand All @@ -28,45 +33,42 @@ class TorrentPlugin implements PlayerPlugin {
}

async load({ $video }: Player, source: Source) {
const { config = {}, matcher = TorrentPlugin.defaultMatcher } = this.options
const { config = {}, matcher = TorrentPlugin.defaultMatcher, library } = this.options

if (!matcher!(source)) return false

if (!TorrentPlugin.library) {
TorrentPlugin.library =
(globalThis as any).Hls ||
(library
? await loadSDK(library, 'WebTorrent')
: (await import('webtorrent/webtorrent.min.js')).default)
}

const webtorrent = TorrentPlugin.library

if (!webtorrent.WEBRTC_SUPPORT) return false

const instance: Webtorrent.Instance = (this.instance = new webtorrent(config))

instance.on('error', (err) => {
this.player.emit('error', {
message: (<Error>err)?.message || err,
pluginName: 'oplayer-plugin-torrent'
})
})
const medias: Webtorrent.TorrentFile[] = []

//TODO: source list
instance.add(source.src, (torrent) => {
let foundMp4 = false
let subtitlePromise: Promise<any>[] = []

torrent.files.forEach((file) => {
if (!foundMp4 && file.name.endsWith('.mp4') && file.renderTo) {
foundMp4 = true
file.renderTo($video)
this.player.once('loadedmetadata', (e) => {
this.player.emit('canplay', e)
})
if (file.name.endsWith('.mp4')) {
medias.push(file)
} else if (file.name.endsWith('.srt')) {
subtitlePromise.push(
new Promise((resolve) => {
file.getBlobURL((err, url) => {
if (err) return
resolve({
name: file.name,
src: url
})
})
})
)
// subtitlePromise.push(
// new Promise((resolve) => {
// file.getBlobURL((err, url) => {
// if (err) return
// resolve({
// name: file.name,
// src: url
// })
// })
// })
// )
} else if (file.name.startsWith('poster')) {
file.getBlobURL((err, url) => {
if (err || !url) return
Expand All @@ -75,8 +77,26 @@ class TorrentPlugin implements PlayerPlugin {
}
})

Promise.all(subtitlePromise).then((subtitles) => {
this.player.context.ui?.subtitle.changeSource(subtitles)
if (!medias.length) throw new Error('media not found')

this.player.on('loadedmetadata', (e) => {
if (this.instance) this.player.emit('canplay', e)
})

medias[0]!.renderTo($video, { controls: false })

this.player.context.ui?.menu.register({
name: 'Torrent',
position: 'top',
children: medias.map((media, i) => ({
name: media.name,
default: i == 0,
value: media
})),
onChange({ value, name }: any, elm: HTMLElement) {
elm.innerText = name
value.renderTo($video, { controls: false })
}
})
})

Expand Down

0 comments on commit d37e40d

Please sign in to comment.