|
| 1 | +import React, { Component } from 'react' |
| 2 | + |
| 3 | +import { canPlay, MATCH_URL_MUX } from '../patterns' |
| 4 | + |
| 5 | +const SDK_URL = 'https://cdn.jsdelivr.net/npm/@mux/mux-player@VERSION/dist/mux-player.mjs' |
| 6 | + |
| 7 | +export default class Mux extends Component { |
| 8 | + static displayName = 'Mux' |
| 9 | + static canPlay = canPlay.mux |
| 10 | + |
| 11 | + componentDidMount () { |
| 12 | + this.props.onMount && this.props.onMount(this) |
| 13 | + this.addListeners(this.player) |
| 14 | + const playbackId = this.getPlaybackId(this.props.url) // Ensure src is set in strict mode |
| 15 | + if (playbackId) { |
| 16 | + this.player.playbackId = playbackId |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + componentWillUnmount () { |
| 21 | + this.player.playbackId = null |
| 22 | + this.removeListeners(this.player) |
| 23 | + } |
| 24 | + |
| 25 | + addListeners (player) { |
| 26 | + const { playsinline } = this.props |
| 27 | + player.addEventListener('play', this.onPlay) |
| 28 | + player.addEventListener('waiting', this.onBuffer) |
| 29 | + player.addEventListener('playing', this.onBufferEnd) |
| 30 | + player.addEventListener('pause', this.onPause) |
| 31 | + player.addEventListener('seeked', this.onSeek) |
| 32 | + player.addEventListener('ended', this.onEnded) |
| 33 | + player.addEventListener('error', this.onError) |
| 34 | + player.addEventListener('ratechange', this.onPlayBackRateChange) |
| 35 | + player.addEventListener('enterpictureinpicture', this.onEnablePIP) |
| 36 | + player.addEventListener('leavepictureinpicture', this.onDisablePIP) |
| 37 | + player.addEventListener('webkitpresentationmodechanged', this.onPresentationModeChange) |
| 38 | + player.addEventListener('canplay', this.onReady) |
| 39 | + if (playsinline) { |
| 40 | + player.setAttribute('playsinline', '') |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + removeListeners (player) { |
| 45 | + player.removeEventListener('canplay', this.onReady) |
| 46 | + player.removeEventListener('play', this.onPlay) |
| 47 | + player.removeEventListener('waiting', this.onBuffer) |
| 48 | + player.removeEventListener('playing', this.onBufferEnd) |
| 49 | + player.removeEventListener('pause', this.onPause) |
| 50 | + player.removeEventListener('seeked', this.onSeek) |
| 51 | + player.removeEventListener('ended', this.onEnded) |
| 52 | + player.removeEventListener('error', this.onError) |
| 53 | + player.removeEventListener('ratechange', this.onPlayBackRateChange) |
| 54 | + player.removeEventListener('enterpictureinpicture', this.onEnablePIP) |
| 55 | + player.removeEventListener('leavepictureinpicture', this.onDisablePIP) |
| 56 | + player.removeEventListener('canplay', this.onReady) |
| 57 | + } |
| 58 | + |
| 59 | + // Proxy methods to prevent listener leaks |
| 60 | + onReady = (...args) => this.props.onReady(...args) |
| 61 | + onPlay = (...args) => this.props.onPlay(...args) |
| 62 | + onBuffer = (...args) => this.props.onBuffer(...args) |
| 63 | + onBufferEnd = (...args) => this.props.onBufferEnd(...args) |
| 64 | + onPause = (...args) => this.props.onPause(...args) |
| 65 | + onEnded = (...args) => this.props.onEnded(...args) |
| 66 | + onError = (...args) => this.props.onError(...args) |
| 67 | + onPlayBackRateChange = (event) => this.props.onPlaybackRateChange(event.target.playbackRate) |
| 68 | + onEnablePIP = (...args) => this.props.onEnablePIP(...args) |
| 69 | + |
| 70 | + onSeek = e => { |
| 71 | + this.props.onSeek(e.target.currentTime) |
| 72 | + } |
| 73 | + |
| 74 | + async load (url) { |
| 75 | + const { onError, config } = this.props |
| 76 | + |
| 77 | + if (!globalThis.customElements?.get('mux-player')) { |
| 78 | + try { |
| 79 | + await import(SDK_URL.replace('VERSION', config.version)) |
| 80 | + this.props.onLoaded() |
| 81 | + } catch (error) { |
| 82 | + onError(error) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + const [, id] = url.match(MATCH_URL_MUX) |
| 87 | + this.player.playbackId = id |
| 88 | + } |
| 89 | + |
| 90 | + onDurationChange = () => { |
| 91 | + const duration = this.getDuration() |
| 92 | + this.props.onDuration(duration) |
| 93 | + } |
| 94 | + |
| 95 | + play () { |
| 96 | + const promise = this.player.play() |
| 97 | + if (promise) { |
| 98 | + promise.catch(this.props.onError) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + pause () { |
| 103 | + this.player.pause() |
| 104 | + } |
| 105 | + |
| 106 | + stop () { |
| 107 | + this.player.playbackId = null |
| 108 | + } |
| 109 | + |
| 110 | + seekTo (seconds, keepPlaying = true) { |
| 111 | + this.player.currentTime = seconds |
| 112 | + if (!keepPlaying) { |
| 113 | + this.pause() |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + setVolume (fraction) { |
| 118 | + this.player.volume = fraction |
| 119 | + } |
| 120 | + |
| 121 | + mute = () => { |
| 122 | + this.player.muted = true |
| 123 | + } |
| 124 | + |
| 125 | + unmute = () => { |
| 126 | + this.player.muted = false |
| 127 | + } |
| 128 | + |
| 129 | + enablePIP () { |
| 130 | + if (this.player.requestPictureInPicture && document.pictureInPictureElement !== this.player) { |
| 131 | + this.player.requestPictureInPicture() |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + disablePIP () { |
| 136 | + if (document.exitPictureInPicture && document.pictureInPictureElement === this.player) { |
| 137 | + document.exitPictureInPicture() |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + setPlaybackRate (rate) { |
| 142 | + try { |
| 143 | + this.player.playbackRate = rate |
| 144 | + } catch (error) { |
| 145 | + this.props.onError(error) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + getDuration () { |
| 150 | + if (!this.player) return null |
| 151 | + const { duration, seekable } = this.player |
| 152 | + // on iOS, live streams return Infinity for the duration |
| 153 | + // so instead we use the end of the seekable timerange |
| 154 | + if (duration === Infinity && seekable.length > 0) { |
| 155 | + return seekable.end(seekable.length - 1) |
| 156 | + } |
| 157 | + return duration |
| 158 | + } |
| 159 | + |
| 160 | + getCurrentTime () { |
| 161 | + if (!this.player) return null |
| 162 | + return this.player.currentTime |
| 163 | + } |
| 164 | + |
| 165 | + getSecondsLoaded () { |
| 166 | + if (!this.player) return null |
| 167 | + const { buffered } = this.player |
| 168 | + if (buffered.length === 0) { |
| 169 | + return 0 |
| 170 | + } |
| 171 | + const end = buffered.end(buffered.length - 1) |
| 172 | + const duration = this.getDuration() |
| 173 | + if (end > duration) { |
| 174 | + return duration |
| 175 | + } |
| 176 | + return end |
| 177 | + } |
| 178 | + |
| 179 | + getPlaybackId (url) { |
| 180 | + const [, id] = url.match(MATCH_URL_MUX) |
| 181 | + return id |
| 182 | + } |
| 183 | + |
| 184 | + ref = player => { |
| 185 | + this.player = player |
| 186 | + } |
| 187 | + |
| 188 | + render () { |
| 189 | + const { url, playing, loop, controls, muted, config, width, height } = this.props |
| 190 | + const style = { |
| 191 | + width: width === 'auto' ? width : '100%', |
| 192 | + height: height === 'auto' ? height : '100%' |
| 193 | + } |
| 194 | + if (controls === false) { |
| 195 | + style['--controls'] = 'none' |
| 196 | + } |
| 197 | + return ( |
| 198 | + <mux-player |
| 199 | + ref={this.ref} |
| 200 | + playback-id={this.getPlaybackId(url)} |
| 201 | + style={style} |
| 202 | + preload='auto' |
| 203 | + autoPlay={playing || undefined} |
| 204 | + muted={muted ? '' : undefined} |
| 205 | + loop={loop ? '' : undefined} |
| 206 | + {...config.attributes} |
| 207 | + /> |
| 208 | + ) |
| 209 | + } |
| 210 | +} |
0 commit comments