VideoPlayer.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <div class="cockpit-video-surface">
  3. <video v-if="isNative" ref="video" class="cockpit-video-player" :aria-label="label"
  4. :muted="muted" autoplay playsinline
  5. controlslist="nodownload noremoteplayback noplaybackrate" disablepictureinpicture disableremoteplayback
  6. @contextmenu.prevent @playing="emit('ready')" @error="emit('error', { message: '视频地址无法播放,请确认格式和网络' })" />
  7. <div v-else ref="decoder" class="cockpit-video-player" :aria-label="label" data-flv-decoder />
  8. <p v-if="errorText" class="cockpit-video-error" role="alert">{{ errorText }}</p>
  9. </div>
  10. </template>
  11. <script setup>
  12. import { computed, nextTick, onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue'
  13. const props = defineProps({
  14. media: { type: Object, default: null }, label: { type: String, default: '实时视频画面' },
  15. controls: { type: Boolean, default: false }, muted: { type: Boolean, default: true }, volume: { type: Number, default: 0.5 },
  16. })
  17. const emit = defineEmits(['ready', 'error', 'presentation-error'])
  18. const video = shallowRef(null), decoder = shallowRef(null), errorText = ref('')
  19. const isNative = computed(() => props.media?.kind === 'native' || props.media?.native === true)
  20. let current, nativeElement, generation = 0
  21. function loadSdk() {
  22. if (globalThis.Jessibuca) return Promise.resolve()
  23. if (!globalThis.__cockpitJessibuca) {
  24. globalThis.__cockpitJessibuca = new Promise((resolve, reject) => {
  25. const script = document.createElement('script')
  26. script.src = import.meta.env.BASE_URL + 'jessibuca/jessibuca.js'
  27. script.onload = () => {
  28. if (globalThis.Jessibuca) resolve()
  29. else { globalThis.__cockpitJessibuca = null; script.remove(); reject(new Error('视频播放器加载失败')) }
  30. }
  31. script.onerror = () => { globalThis.__cockpitJessibuca = null; script.remove(); reject(new Error('视频播放器加载失败')) }
  32. document.head.appendChild(script)
  33. })
  34. }
  35. return globalThis.__cockpitJessibuca
  36. }
  37. // Each SDK generation owns a different DOM container: late async destroy cannot empty its successor.
  38. function dispose() {
  39. generation++
  40. const owned = current
  41. current = null
  42. if (owned) {
  43. owned.host.remove()
  44. try { Promise.resolve(owned.player.destroy()).catch(() => {}) } catch { /* detached resource */ }
  45. }
  46. // Vue may already have detached the old native branch before this post-flush restart.
  47. const oldNative = nativeElement
  48. nativeElement = null
  49. if (oldNative) { oldNative.pause(); oldNative.removeAttribute('src'); oldNative.load() }
  50. }
  51. function applyAudio() {
  52. const volume = Number.isFinite(props.volume) ? Math.max(0, Math.min(1, props.volume)) : 0.5
  53. try {
  54. if (isNative.value && video.value) { video.value.muted = props.muted; video.value.volume = volume }
  55. else if (current) {
  56. if (props.muted) current.player.mute()
  57. else { current.player.audioResume(); current.player.setVolume(volume) }
  58. }
  59. } catch (error) { emit('presentation-error', error?.message || '无法开启声音,请再次点击取消静音') }
  60. }
  61. async function start() {
  62. dispose()
  63. errorText.value = ''
  64. const version = generation, media = props.media
  65. if (!media?.url) return
  66. try {
  67. await nextTick()
  68. if (version !== generation) return
  69. if (isNative.value) {
  70. // Teardown must not erase the newly selected native source without restoring it.
  71. nativeElement = video.value
  72. nativeElement.setAttribute('src', media.url)
  73. if (props.controls) video.value.setAttribute('controls', '')
  74. else video.value.removeAttribute('controls')
  75. applyAudio()
  76. await video.value.play()
  77. return
  78. }
  79. await loadSdk()
  80. if (version !== generation || !decoder.value) return
  81. const host = document.createElement('div')
  82. host.style.width = '100%'; host.style.height = '100%'
  83. decoder.value.appendChild(host)
  84. let player
  85. try {
  86. player = new globalThis.Jessibuca({ container: host,
  87. decoder: import.meta.env.BASE_URL + 'jessibuca/decoder.js',
  88. isResize: true, useMSE: false, useWCS: false, hasAudio: true, isNotMute: false, videoBuffer: 0.2,
  89. // The enclosing surface owns fullscreen and volume; no duplicate controls or capture features.
  90. operateBtns: { fullscreen: false, screenshot: false, play: false, audio: false }, loadingText: '视频加载中',
  91. })
  92. } catch (error) { host.remove(); throw error }
  93. const owned = { player, host }
  94. current = owned
  95. const active = () => version === generation && current === owned
  96. player.on('error', () => { if (active()) { errorText.value = '视频解码或网络异常,请重试'; emit('error', { message: errorText.value }) } })
  97. player.on('play', () => { if (active()) emit('ready') })
  98. applyAudio()
  99. await player.play(media.url)
  100. } catch (error) {
  101. if (version === generation) { errorText.value = error?.message || '视频加载失败'; emit('error', error) }
  102. }
  103. }
  104. onMounted(start)
  105. watch(() => props.media, start, { flush: 'post' })
  106. // Synchronous application keeps audioResume in the real click/input activation chain, without restarting.
  107. watch(() => [props.muted, props.volume], applyAudio, { flush: 'sync' })
  108. onBeforeUnmount(dispose)
  109. </script>
  110. <style scoped>
  111. .cockpit-video-surface { position: relative; width: 100%; height: 100%; min-height: 0; background: #02090e; }
  112. .cockpit-video-player { display: block; width: 100%; height: 100%; object-fit: contain; background: #02090e; }
  113. .cockpit-video-error { position: absolute; inset: 0; display: grid; place-items: center; margin: 0; padding: 16px; color: #ffc478; }
  114. </style>