| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <template>
- <div class="cockpit-video-surface">
- <video v-if="isNative" ref="video" class="cockpit-video-player" :aria-label="label"
- :muted="muted" autoplay playsinline
- controlslist="nodownload noremoteplayback noplaybackrate" disablepictureinpicture disableremoteplayback
- @contextmenu.prevent @playing="emit('ready')" @error="emit('error', { message: '视频地址无法播放,请确认格式和网络' })" />
- <div v-else ref="decoder" class="cockpit-video-player" :aria-label="label" data-flv-decoder />
- <p v-if="errorText" class="cockpit-video-error" role="alert">{{ errorText }}</p>
- </div>
- </template>
- <script setup>
- import { computed, nextTick, onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue'
- const props = defineProps({
- media: { type: Object, default: null }, label: { type: String, default: '实时视频画面' },
- controls: { type: Boolean, default: false }, muted: { type: Boolean, default: true }, volume: { type: Number, default: 0.5 },
- })
- const emit = defineEmits(['ready', 'error', 'presentation-error'])
- const video = shallowRef(null), decoder = shallowRef(null), errorText = ref('')
- const isNative = computed(() => props.media?.kind === 'native' || props.media?.native === true)
- let current, nativeElement, generation = 0
- function loadSdk() {
- if (globalThis.Jessibuca) return Promise.resolve()
- if (!globalThis.__cockpitJessibuca) {
- globalThis.__cockpitJessibuca = new Promise((resolve, reject) => {
- const script = document.createElement('script')
- script.src = import.meta.env.BASE_URL + 'jessibuca/jessibuca.js'
- script.onload = () => {
- if (globalThis.Jessibuca) resolve()
- else { globalThis.__cockpitJessibuca = null; script.remove(); reject(new Error('视频播放器加载失败')) }
- }
- script.onerror = () => { globalThis.__cockpitJessibuca = null; script.remove(); reject(new Error('视频播放器加载失败')) }
- document.head.appendChild(script)
- })
- }
- return globalThis.__cockpitJessibuca
- }
- // Each SDK generation owns a different DOM container: late async destroy cannot empty its successor.
- function dispose() {
- generation++
- const owned = current
- current = null
- if (owned) {
- owned.host.remove()
- try { Promise.resolve(owned.player.destroy()).catch(() => {}) } catch { /* detached resource */ }
- }
- // Vue may already have detached the old native branch before this post-flush restart.
- const oldNative = nativeElement
- nativeElement = null
- if (oldNative) { oldNative.pause(); oldNative.removeAttribute('src'); oldNative.load() }
- }
- function applyAudio() {
- const volume = Number.isFinite(props.volume) ? Math.max(0, Math.min(1, props.volume)) : 0.5
- try {
- if (isNative.value && video.value) { video.value.muted = props.muted; video.value.volume = volume }
- else if (current) {
- if (props.muted) current.player.mute()
- else { current.player.audioResume(); current.player.setVolume(volume) }
- }
- } catch (error) { emit('presentation-error', error?.message || '无法开启声音,请再次点击取消静音') }
- }
- async function start() {
- dispose()
- errorText.value = ''
- const version = generation, media = props.media
- if (!media?.url) return
- try {
- await nextTick()
- if (version !== generation) return
- if (isNative.value) {
- // Teardown must not erase the newly selected native source without restoring it.
- nativeElement = video.value
- nativeElement.setAttribute('src', media.url)
- if (props.controls) video.value.setAttribute('controls', '')
- else video.value.removeAttribute('controls')
- applyAudio()
- await video.value.play()
- return
- }
- await loadSdk()
- if (version !== generation || !decoder.value) return
- const host = document.createElement('div')
- host.style.width = '100%'; host.style.height = '100%'
- decoder.value.appendChild(host)
- let player
- try {
- player = new globalThis.Jessibuca({ container: host,
- decoder: import.meta.env.BASE_URL + 'jessibuca/decoder.js',
- isResize: true, useMSE: false, useWCS: false, hasAudio: true, isNotMute: false, videoBuffer: 0.2,
- // The enclosing surface owns fullscreen and volume; no duplicate controls or capture features.
- operateBtns: { fullscreen: false, screenshot: false, play: false, audio: false }, loadingText: '视频加载中',
- })
- } catch (error) { host.remove(); throw error }
- const owned = { player, host }
- current = owned
- const active = () => version === generation && current === owned
- player.on('error', () => { if (active()) { errorText.value = '视频解码或网络异常,请重试'; emit('error', { message: errorText.value }) } })
- player.on('play', () => { if (active()) emit('ready') })
- applyAudio()
- await player.play(media.url)
- } catch (error) {
- if (version === generation) { errorText.value = error?.message || '视频加载失败'; emit('error', error) }
- }
- }
- onMounted(start)
- watch(() => props.media, start, { flush: 'post' })
- // Synchronous application keeps audioResume in the real click/input activation chain, without restarting.
- watch(() => [props.muted, props.volume], applyAudio, { flush: 'sync' })
- onBeforeUnmount(dispose)
- </script>
- <style scoped>
- .cockpit-video-surface { position: relative; width: 100%; height: 100%; min-height: 0; background: #02090e; }
- .cockpit-video-player { display: block; width: 100%; height: 100%; object-fit: contain; background: #02090e; }
- .cockpit-video-error { position: absolute; inset: 0; display: grid; place-items: center; margin: 0; padding: 16px; color: #ffc478; }
- </style>
|