VideoMonitorPanel.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <template>
  2. <Teleport to="body">
  3. <div v-if="state.open" class="video-monitor-backdrop">
  4. <section class="video-monitor" role="dialog" aria-modal="true" aria-labelledby="video-monitor-title"
  5. data-video-monitor data-video-design="dahua-cockpit-b">
  6. <header class="video-monitor__header">
  7. <div><span class="video-monitor__eyebrow">VIDEO / 现场监控</span><h2 id="video-monitor-title">{{ state.device?.deviceName || '视频监控设备' }}</h2></div>
  8. <button type="button" class="video-monitor__close" aria-label="关闭监控" @click="video.close">关闭 <span aria-hidden="true">×</span></button>
  9. </header>
  10. <dl class="video-monitor__meta" data-video-meta>
  11. <div><dt>设备名称</dt><dd data-video-meta-field="deviceName">{{ state.device?.deviceName || '—' }}</dd></div>
  12. <div><dt>业务设备编码</dt><dd data-video-meta-field="businessCode">{{ state.device?.businessCode || state.device?.deviceCode || '—' }}</dd></div>
  13. <div><dt>接入方式</dt><dd data-video-meta-field="accessType">{{ accessTypeText }}</dd></div>
  14. <div><dt>国标设备 / 通道</dt><dd data-video-meta-field="gb">{{ gbText }}</dd></div>
  15. <div><dt>安装位置</dt><dd data-video-meta-field="location">{{ state.device?.location || '—' }}</dd></div>
  16. <div><dt>厂商</dt><dd data-video-meta-field="manufacturer">{{ state.device?.manufacturer || '—' }}</dd></div>
  17. </dl>
  18. <div class="video-monitor__body">
  19. <aside class="video-monitor__ptz-panel" data-ptz-panel aria-label="云台界面预留">
  20. <div class="video-monitor__section-head"><h3>云台控制</h3><span class="video-monitor__badge">界面预留</span></div>
  21. <div class="video-monitor__ptz-pad" data-ptz-pad role="group" aria-label="八向云台方向盘">
  22. <button v-for="key in PTZ_DIRECTIONS" :key="key" type="button" :data-ptz-direction="key"
  23. :class="'video-monitor__direction--' + key" disabled :title="ptzReason" :aria-label="'云台' + PTZ_LABELS[key]">
  24. <span aria-hidden="true">{{ PTZ_ARROWS[key] }}</span>
  25. </button>
  26. <button type="button" class="video-monitor__ptz-stop" data-ptz-stop disabled :title="ptzReason" aria-label="云台停止"><span aria-hidden="true">■</span><small>停止</small></button>
  27. </div>
  28. <label class="video-monitor__speed">速度 <span>STEP {{ ptzSpeed }}</span><input v-model="ptzSpeed" type="range" min="1" max="8" step="1" disabled :title="ptzReason" aria-label="云台速度" data-ptz-speed /></label>
  29. <div class="video-monitor__lens" role="group" aria-label="云台镜头控制">
  30. <span>变倍</span><button type="button" data-ptz-zoom="out" disabled :title="ptzReason" aria-label="云台缩小">−</button><button type="button" data-ptz-zoom="in" disabled :title="ptzReason" aria-label="云台放大">+</button>
  31. <span>聚焦</span><button type="button" data-ptz-focus="far" disabled :title="ptzReason" aria-label="云台远焦">−</button><button type="button" data-ptz-focus="near" disabled :title="ptzReason" aria-label="云台近焦">+</button>
  32. </div>
  33. <p class="video-monitor__ptz-hint" data-ptz-hint>{{ ptzReason }}</p>
  34. <p class="video-monitor__note">本期仅呈现控制布局,不发送物理控制命令。</p>
  35. </aside>
  36. <div class="video-monitor__viewer">
  37. <div ref="surface" class="video-monitor__surface" data-video-surface>
  38. <div class="video-monitor__screen">
  39. <VideoPlayer v-if="mode === 'live' && state.media" key="live" :media="state.media" :controls="false"
  40. :muted="playbackMuted" :volume="volume / 100" @ready="video.ready" @error="video.fail" @presentation-error="presentationError = $event" />
  41. <VideoPlayer v-else-if="mode === 'history' && state.playbackMedia" key="history" :media="state.playbackMedia" :controls="false"
  42. label="历史录像画面" :muted="playbackMuted" :volume="volume / 100" @ready="video.readyPlayback" @error="video.failPlayback" @presentation-error="presentationError = $event" />
  43. <div v-else class="video-monitor__placeholder"><span aria-hidden="true">▷</span><p>{{ mode === 'history' ? (state.historyError || '请选择已有录像开始回放') : (state.error || '等待视频连接') }}</p></div>
  44. </div>
  45. <div class="video-monitor__toolbar" data-video-toolbar :data-playback-surface="mode === 'history' ? '' : undefined">
  46. <div class="video-monitor__tabs" role="group" aria-label="视频模式">
  47. <button type="button" :aria-pressed="mode === 'live'" :disabled="switching" @click="switchMode('live')">实时视频</button>
  48. <button type="button" :aria-pressed="mode === 'history'" :disabled="switching || !state.device?.capabilities?.records"
  49. :title="state.device?.capabilities?.records ? '' : '此设备未提供录像查询能力'" @click="switchMode('history')">历史录像</button>
  50. </div>
  51. <span class="video-monitor__status" role="status" :data-video-phase="mode === 'live' ? state.phase : state.historyPhase">{{ statusText }}</span>
  52. <div class="video-monitor__presentation">
  53. <button type="button" data-video-mute :data-playback-mute="mode === 'history' ? '' : undefined" :disabled="!activeMedia" :aria-pressed="playbackMuted"
  54. :aria-label="playbackMuted ? '取消静音' : '静音'" @click="togglePlaybackMute">{{ playbackMuted ? '取消静音' : '静音' }}</button>
  55. <label>音量<input v-model="volume" type="range" min="0" max="100" step="1" :disabled="!activeMedia" aria-label="视频音量" data-video-volume /></label>
  56. <button type="button" data-video-fullscreen :data-playback-fullscreen="mode === 'history' ? '' : undefined" :disabled="!activeMedia" aria-label="视频画面全屏" @click="enterPlaybackFullscreen">全屏</button>
  57. </div>
  58. </div>
  59. <template v-if="mode === 'history' && state.playbackMedia">
  60. <div class="video-monitor__playback-controls" role="group" aria-label="录像回放控制">
  61. <button v-for="control in playbackControls" :key="control.key" type="button" :data-playback-control="control.key" :disabled="!control.enabled"
  62. :title="control.enabled ? '' : control.reason" :aria-label="control.label" @click="runControl(control)">{{ control.label }}</button>
  63. </div>
  64. <label v-if="state.playbackControls?.seek" class="video-monitor__seek">跳转位置
  65. <input v-model="playbackPosition" type="number" min="0" :max="playbackDuration" step="1" aria-label="录像跳转秒数" data-playback-position />
  66. <span>/ {{ playbackDuration }} 秒</span>
  67. <input v-model="playbackPosition" type="range" min="0" :max="playbackDuration" step="1" aria-label="选择录像跳转位置" data-playback-seek-slider />
  68. </label>
  69. </template>
  70. <p v-if="presentationError" class="video-monitor__warning" role="alert" data-video-presentation-error>{{ presentationError }}</p>
  71. </div>
  72. <div v-if="mode === 'history'" class="video-monitor__history" data-video-history>
  73. <div class="video-monitor__range">
  74. <label>开始(UTC+8)<input v-model="range.startTime" type="text" placeholder="yyyy-MM-dd HH:mm:ss" aria-label="录像查询开始时间" /></label>
  75. <label>结束(UTC+8)<input v-model="range.endTime" type="text" placeholder="yyyy-MM-dd HH:mm:ss" aria-label="录像查询结束时间" /></label>
  76. <button type="button" :disabled="state.historyPhase === 'querying'" @click="video.queryHistory(range.startTime, range.endTime)">查询录像</button>
  77. </div>
  78. <div class="video-monitor__records-scroll" tabindex="0" aria-label="录像查询结果" data-video-records-region>
  79. <p v-if="state.historyPhase === 'querying'">正在查询录像…</p>
  80. <p v-else-if="state.historyError" role="alert">{{ state.historyError }}</p>
  81. <p v-else-if="state.empty">该时间段没有录像(查询范围不代表留存期限)</p>
  82. <ul v-else-if="state.records?.length" class="video-monitor__records">
  83. <li v-for="(record, index) in state.records" :key="index" :class="{ 'is-selected': selectedRecord === record }">
  84. <span><b>{{ record.name || '录像' }}</b><small>{{ record.type || '录像片段' }} · {{ record.startTime }} ~ {{ record.endTime }}</small></span>
  85. <button type="button" :disabled="switching || state.historyPhase === 'starting'" :aria-label="'播放录像 ' + record.startTime" @click="playRecord(record)">播放</button>
  86. </li>
  87. </ul>
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. <p v-if="retryNote && mode === 'live'" class="video-monitor__status" role="status">{{ retryNote }}</p>
  93. <p v-if="state.error && mode === 'live' && state.media" role="alert">{{ state.error }}</p>
  94. <p v-if="state.cleanupError" class="video-monitor__warning" role="alert">{{ state.cleanupError }}</p>
  95. <footer class="video-monitor__footer">
  96. <button v-if="mode === 'live' && !state.media && !['starting','connecting','retrying'].includes(state.phase)" type="button" :disabled="Boolean(state.cleanupError)" @click="video.retry">重新播放</button>
  97. <button v-if="state.cleanupError" type="button" @click="video.retryCleanup">重试清理</button>
  98. <span>默认静音 · 不提供截图、下载或录制</span><span>Jessibuca / FLV</span>
  99. </footer>
  100. </section>
  101. </div>
  102. </Teleport>
  103. </template>
  104. <script setup>
  105. import { watch, onMounted, onBeforeUnmount, nextTick, computed, ref, reactive } from 'vue'
  106. import { storeToRefs } from 'pinia'
  107. import { useVideoStore } from '../stores/videoStore.js'
  108. import { useUiStore } from '../stores/uiStore.js'
  109. import { useDetailStore } from '../stores/detailStore.js'
  110. import { useEnhancementDevicesStore } from '../stores/enhancementDevicesStore.js'
  111. import VideoPlayer from './VideoPlayer.vue'
  112. const video = useVideoStore(), ui = useUiStore(), detail = useDetailStore(), enhancement = useEnhancementDevicesStore()
  113. const { state } = storeToRefs(video)
  114. const labels = { idle: '未播放', starting: '正在建立视频会话', connecting: '等待视频画面', playing: '正在播放', retrying: '连接中断,正在自动重连', failed: '播放失败', querying: '正在查询录像', listed: '已返回录像列表' }
  115. // Switching tabs ends the current stream but must keep the monitor open.
  116. const mode = ref('live')
  117. const range = reactive(video.historyWindow())
  118. const switching = ref(false)
  119. // This release presents PTZ only; a capability flag alone must never enable physical commands.
  120. const PTZ_DIRECTIONS = ['up-left', 'up', 'up-right', 'left', 'right', 'down-left', 'down', 'down-right']
  121. const PTZ_LABELS = { up: '上', down: '下', left: '左', right: '右', 'up-left': '左上', 'up-right': '右上', 'down-left': '左下', 'down-right': '右下' }
  122. const PTZ_ARROWS = { up: '↑', down: '↓', left: '←', right: '→', 'up-left': '↖', 'up-right': '↗', 'down-left': '↙', 'down-right': '↘' }
  123. const ptzSpeed = ref(4)
  124. const ptzReason = computed(() => state.value.cleanupError ? '会话释放未确认,暂停控制'
  125. : state.value.device?.capabilities?.ptz ? '云台操作尚未接入,本期仅界面预留' : '设备暂不支持云台控制')
  126. // Access mode and GB identifiers come straight from the binding; blanks show as a dash.
  127. const ACCESS_TEXT = { GB28181: '国标接入', MANUAL: '手工接入' }
  128. const accessTypeText = computed(() => {
  129. const type = state.value.device?.sourceType || state.value.device?.accessType
  130. return ACCESS_TEXT[type] || type || '—'
  131. })
  132. const gbText = computed(() => {
  133. const device = state.value.device
  134. return device?.gbDeviceId || device?.gbChannelId
  135. ? `${device.gbDeviceId || '—'} / ${device.gbChannelId || '—'}`
  136. : '—'
  137. })
  138. let modeVersion = 0
  139. const switchMode = async (next) => {
  140. if (next === mode.value || switching.value) return
  141. const version = ++modeVersion
  142. switching.value = true
  143. try {
  144. if (next === 'history') await video.stopLive()
  145. else await video.stopPlayback()
  146. // Close, selection changes and unconfirmed cleanup must not resume an old switch.
  147. if (version !== modeVersion || !state.value.open || state.value.cleanupError) return
  148. mode.value = next
  149. if (next === 'history') await video.queryHistory(range.startTime, range.endTime)
  150. else await video.startLive()
  151. } finally { if (version === modeVersion) switching.value = false }
  152. }
  153. const selectedRecord = ref(null)
  154. const playRecord = async (record) => {
  155. playbackPosition.value = ''
  156. selectedRecord.value = record
  157. await video.play(record.startTime, record.endTime)
  158. }
  159. // Controls describe the server operation set, not a claim of physical device support.
  160. const CONTROL_REASON = '服务端未开放此控制能力'
  161. const playbackControls = computed(() => {
  162. const granted = state.value.playbackControls || {}
  163. // Speed keys are per-rate in the UI but share one confirmed capability on the server.
  164. const control = (key, label, capability = key) => ({ key, label, enabled: granted[capability] === true, reason: CONTROL_REASON })
  165. return [control('pause', '暂停'), control('resume', '继续'), control('speed-0.25', '0.25×', 'speed'),
  166. control('speed-0.5', '0.5×', 'speed'), control('speed-1', '1×', 'speed'), control('speed-2', '2×', 'speed'),
  167. control('speed-4', '4×', 'speed'), control('seek', '跳转')]
  168. })
  169. // A seek needs a position inside the chosen recording; the panel asks for one instead of guessing.
  170. const playbackPosition = ref('')
  171. // The chosen recording's own length, so the seek input cannot promise a position it does not have.
  172. const playbackDuration = computed(() => Math.floor(Number(state.value.durationSeconds) || 0))
  173. // A disabled control must not reach the server even when activated programmatically.
  174. const runControl = (control) => {
  175. if (!control.enabled) return
  176. if (control.key === 'seek') {
  177. // An empty box must not silently mean "seek to 0".
  178. if (String(playbackPosition.value).trim() === '') return
  179. const seconds = Number(playbackPosition.value)
  180. if (!Number.isFinite(seconds)) return
  181. void video.controlPlayback('seek', seconds)
  182. return
  183. }
  184. void video.controlPlayback(control.key)
  185. }
  186. // Local presentation never creates a new business session or sends a device command.
  187. const surface = ref(null)
  188. const playbackMuted = ref(true), volume = ref(50), presentationError = ref('')
  189. const activeMedia = computed(() => mode.value === 'live' ? state.value.media : state.value.playbackMedia)
  190. const statusText = computed(() => labels[mode.value === 'live' ? state.value.phase : state.value.historyPhase] || '未播放')
  191. const togglePlaybackMute = () => { playbackMuted.value = !playbackMuted.value; presentationError.value = '' }
  192. const enterPlaybackFullscreen = async () => {
  193. const target = surface.value
  194. presentationError.value = ''
  195. try {
  196. if (!target?.requestFullscreen) throw new Error('此浏览器不支持画面全屏')
  197. await target.requestFullscreen()
  198. } catch (error) { presentationError.value = error?.message || '无法进入全屏,请检查浏览器权限' }
  199. }
  200. watch(activeMedia, () => { playbackMuted.value = true; presentationError.value = '' })
  201. const retryNote = computed(() => state.value.phase !== 'retrying' || !state.value.retryAttempt
  202. ? ''
  203. : `第 ${state.value.retryAttempt} 次自动重试,等待 ${Math.round((state.value.retryDelayMs || 0) / 1000)} 秒(剩余 ${state.value.retriesLeft ?? 0} 次)`)
  204. watch(() => ui.mode, () => { void video.close() }, { flush: 'sync' })
  205. watch(() => [state.value.ownerSource, detail.selection.deviceCode, enhancement.selected?.code], ([ownerSource, detailCode, enhancementCode]) => {
  206. const currentCode = ownerSource === 'enhancement' ? enhancementCode : detailCode
  207. if (state.value.open && currentCode !== state.value.device?.deviceCode) void video.close()
  208. }, { flush: 'sync' })
  209. let trigger
  210. watch(() => state.value.open, async (open) => {
  211. modeVersion++; switching.value = false
  212. if (!open) { if (trigger?.isConnected) trigger.focus?.(); return }
  213. mode.value = 'live'; selectedRecord.value = null; playbackPosition.value = ''; playbackMuted.value = true; volume.value = 50
  214. Object.assign(range, video.historyWindow())
  215. trigger = document.activeElement
  216. await nextTick()
  217. if (state.value.open) document.querySelector('[data-video-monitor] [aria-label="关闭监控"]')?.focus()
  218. })
  219. const keyboard = (event) => {
  220. if (!state.value.open) return
  221. if (event.key === 'Escape') { event.preventDefault(); void video.close(); return }
  222. if (event.key !== 'Tab') return
  223. // Fullscreen hides the rest of the dialog; never wrap focus onto an invisible close button.
  224. const root = document.fullscreenElement === surface.value ? surface.value : document.querySelector('[data-video-monitor]')
  225. const items = Array.from(root?.querySelectorAll('button:not(:disabled), input:not(:disabled), select:not(:disabled), [tabindex="0"]') || [])
  226. .filter(element => !element.hidden && element.getAttribute('aria-hidden') !== 'true')
  227. if (!items.length) return
  228. const index = items.indexOf(document.activeElement)
  229. if (index < 0 || (event.shiftKey && index === 0) || (!event.shiftKey && index === items.length - 1)) {
  230. event.preventDefault(); items[event.shiftKey ? items.length - 1 : 0].focus?.()
  231. }
  232. }
  233. const hidden = () => { if (document.hidden) void video.close() }
  234. const leave = () => { void video.close() }
  235. onMounted(() => { document.addEventListener('visibilitychange', hidden); document.addEventListener('keydown', keyboard); window.addEventListener('pagehide', leave) })
  236. onBeforeUnmount(() => { document.removeEventListener('visibilitychange', hidden); document.removeEventListener('keydown', keyboard); window.removeEventListener('pagehide', leave); void video.close() })
  237. </script>
  238. <style scoped src="./videoMonitorPanel.css"></style>