index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <template>
  2. <div
  3. ref="containerRef"
  4. class="video-container"
  5. :class="{ 'is-fullscreen': isFullscreen }"
  6. >
  7. <!-- Left sidebar control panel -->
  8. <div v-if="showChannelSelector || ptzEnabled" class="control-sidebar">
  9. <div class="control-panel" :class="{ 'is-collapsed': ptzCollapsed }">
  10. <!-- Channel selector -->
  11. <div v-if="showChannelSelector" class="channel-selector">
  12. <div class="control-label">Channel</div>
  13. <el-select
  14. v-model="selectedChn"
  15. size="small"
  16. @change="handleChnChange"
  17. >
  18. <el-option
  19. v-for="ch in channelOptions"
  20. :key="ch.value"
  21. :label="ch.label"
  22. :value="ch.value"
  23. />
  24. </el-select>
  25. </div>
  26. <!-- PTZ control -->
  27. <template v-if="ptzEnabled">
  28. <div v-if="showChannelSelector" class="control-divider" />
  29. <div class="control-label">PTZ Control</div>
  30. <div class="ptz-grid">
  31. <div class="ptz-placeholder" />
  32. <button
  33. class="ptz-btn"
  34. title="Up"
  35. @mousedown="handlePtzStart(PTZDirection.Up)"
  36. @mouseup="handlePtzStop"
  37. >
  38. <svg viewBox="0 0 24 24" width="22" height="22">
  39. <path fill="currentColor" d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z"/>
  40. </svg>
  41. </button>
  42. <div class="ptz-placeholder" />
  43. <button
  44. class="ptz-btn"
  45. title="Left"
  46. @mousedown="handlePtzStart(PTZDirection.Left)"
  47. @mouseup="handlePtzStop"
  48. >
  49. <svg viewBox="0 0 24 24" width="22" height="22">
  50. <path fill="currentColor" d="M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z"/>
  51. </svg>
  52. </button>
  53. <button
  54. class="ptz-btn ptz-btn-center"
  55. title="Reset"
  56. @click="handlePtzCenter"
  57. >
  58. <svg viewBox="0 0 24 24" width="22" height="22">
  59. <path fill="currentColor" d="M12 6v3l4-4-4-4v3c-4.42 0-8 3.58-8 8 0 1.57.46 3.03 1.24 4.26L6.7 14.8A5.87 5.87 0 0 1 6 12c0-3.31 2.69-6 6-6zm6.76 1.74L17.3 9.2c.44.84.7 1.79.7 2.8 0 3.31-2.69 6-6 6v-3l-4 4 4 4v-3c4.42 0 8-3.58 8-8 0-1.57-.46-3.03-1.24-4.26z"/>
  60. </svg>
  61. </button>
  62. <button
  63. class="ptz-btn"
  64. title="Right"
  65. @mousedown="handlePtzStart(PTZDirection.Right)"
  66. @mouseup="handlePtzStop"
  67. >
  68. <svg viewBox="0 0 24 24" width="22" height="22">
  69. <path fill="currentColor" d="M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z"/>
  70. </svg>
  71. </button>
  72. <div class="ptz-placeholder" />
  73. <button
  74. class="ptz-btn"
  75. title="Down"
  76. @mousedown="handlePtzStart(PTZDirection.Down)"
  77. @mouseup="handlePtzStop"
  78. >
  79. <svg viewBox="0 0 24 24" width="22" height="22">
  80. <path fill="currentColor" d="M7.41 8.59L12 13.17l4.59-4.58L18 10l-6 6-6-6z"/>
  81. </svg>
  82. </button>
  83. <div class="ptz-placeholder" />
  84. </div>
  85. </template>
  86. </div>
  87. <!-- Collapse toggle -->
  88. <button class="panel-toggle" :title="ptzCollapsed ? 'Expand' : 'Collapse'" @click="ptzCollapsed = !ptzCollapsed">
  89. <svg viewBox="0 0 24 24" width="16" height="16">
  90. <path fill="currentColor" :d="ptzCollapsed ? 'M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z' : 'M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z'"/>
  91. </svg>
  92. </button>
  93. </div>
  94. <!-- 视频区域(右侧) -->
  95. <div class="video-wrapper">
  96. <MyVideo
  97. ref="videoRef"
  98. :drag-flag="true"
  99. @video-error="handleVideoError"
  100. />
  101. <button
  102. class="fullscreen-btn"
  103. :title="isFullscreen ? 'Exit Fullscreen (F)' : 'Fullscreen (F)'"
  104. @click="toggleFullscreen"
  105. >
  106. <svg
  107. v-if="!isFullscreen"
  108. viewBox="0 0 24 24"
  109. width="16"
  110. height="16"
  111. >
  112. <path fill="currentColor" d="M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z"/>
  113. </svg>
  114. <svg
  115. v-else
  116. viewBox="0 0 24 24"
  117. width="16"
  118. height="16"
  119. >
  120. <path fill="currentColor" d="M5 16h3v3h2v-5H5v2zm3-8H5v2h5V5H8v3zm6 11h2v-3h3v-2h-5v5zm2-11V5h-2v5h5V8h-3z"/>
  121. </svg>
  122. </button>
  123. </div>
  124. </div>
  125. </template>
  126. <script setup lang="ts">
  127. import { ref, onMounted, onUnmounted } from 'vue'
  128. import { ElMessage } from 'element-plus'
  129. import MyVideo from '@/components/myVideo.vue'
  130. import { ptzControl, PTZDirection, getDeviceNum, putDeviceNum } from '@/api/ptz'
  131. const videoRef = ref()
  132. const isFullscreen = ref(false)
  133. const containerRef = ref<HTMLElement>()
  134. const ptzSpeed = ref(4)
  135. const isPtzMoving = ref(false)
  136. const ptzCollapsed = ref(false)
  137. // Device state
  138. const showChannelSelector = ref(false)
  139. const selectedChn = ref(0) // 0: Fixed, 1: PTZ
  140. const ptzEnabled = ref(false)
  141. const channelOptions = [
  142. { label: 'Fixed Camera', value: 0 },
  143. { label: 'PTZ Camera', value: 1 }
  144. ]
  145. // Fetch device info
  146. const fetchDeviceInfo = async () => {
  147. try {
  148. const res = await getDeviceNum()
  149. // const res = {"data":{"LensNumber":2,"ChnNumber":0}}
  150. const lensNumber = res?.data?.LensNumber ?? 1
  151. const chnNumber = res?.data?.ChnNumber ?? 0
  152. // Dual lens: show channel selector
  153. showChannelSelector.value = lensNumber >= 2
  154. // Sync current selection and PTZ state
  155. selectedChn.value = chnNumber
  156. ptzEnabled.value = chnNumber === 1
  157. } catch (error) {
  158. console.error('Failed to get device info:', error)
  159. }
  160. }
  161. // User switches between Fixed / PTZ camera via dropdown
  162. const handleChnChange = async (val: number) => {
  163. try {
  164. await putDeviceNum({ ChnNumber: val })
  165. // Re-fetch to get confirmed state from device
  166. await fetchDeviceInfo()
  167. // Refresh video stream after switch
  168. if (videoRef.value?.refreshWebSocket) {
  169. videoRef.value.refreshWebSocket()
  170. }
  171. } catch (error) {
  172. console.error('Failed to switch channel:', error)
  173. ElMessage.error('Failed to switch channel, please try again')
  174. // Revert selection on failure
  175. await fetchDeviceInfo()
  176. }
  177. }
  178. const toggleFullscreen = () => {
  179. if (!containerRef.value) return
  180. if (!isFullscreen.value) {
  181. if (containerRef.value.requestFullscreen) {
  182. containerRef.value.requestFullscreen()
  183. }
  184. } else {
  185. if (document.exitFullscreen) {
  186. document.exitFullscreen()
  187. }
  188. }
  189. }
  190. const handleFullscreenChange = () => {
  191. isFullscreen.value = !!document.fullscreenElement
  192. }
  193. const handleKeydown = (event: KeyboardEvent) => {
  194. switch (event.code) {
  195. case 'KeyF':
  196. case 'F11':
  197. event.preventDefault()
  198. toggleFullscreen()
  199. break
  200. case 'Escape':
  201. if (isFullscreen.value) {
  202. toggleFullscreen()
  203. }
  204. break
  205. }
  206. }
  207. const handlePtzStart = async (direction: number) => {
  208. if (isPtzMoving.value) return
  209. isPtzMoving.value = true
  210. try {
  211. await ptzControl({ PtzCmd: direction })
  212. } catch (error) {
  213. console.error('PTZ control failed:', error)
  214. }
  215. }
  216. const handlePtzStop = async () => {
  217. if (!isPtzMoving.value) return
  218. isPtzMoving.value = false
  219. try {
  220. await ptzControl({ PtzCmd: PTZDirection.Stop })
  221. } catch (error) {
  222. console.error('PTZ stop failed:', error)
  223. }
  224. }
  225. const handlePtzCenter = async () => {
  226. try {
  227. await ptzControl({ PtzCmd: PTZDirection.Center })
  228. } catch (error) {
  229. console.error('PTZ Center failed:', error)
  230. }
  231. }
  232. const handleVideoError = (error: any) => {
  233. console.error('Video playback error:', error)
  234. }
  235. onMounted(() => {
  236. document.addEventListener('fullscreenchange', handleFullscreenChange)
  237. document.addEventListener('keydown', handleKeydown)
  238. fetchDeviceInfo()
  239. })
  240. onUnmounted(() => {
  241. document.removeEventListener('fullscreenchange', handleFullscreenChange)
  242. document.removeEventListener('keydown', handleKeydown)
  243. })
  244. </script>
  245. <style lang="scss" scoped>
  246. .video-container {
  247. width: 100%;
  248. height: calc(100vh - 90px);
  249. display: flex;
  250. margin-top: 20px;
  251. justify-content: center;
  252. align-items: center;
  253. gap: 16px;
  254. padding: 0 16px;
  255. position: relative;
  256. overflow: hidden;
  257. &.is-fullscreen {
  258. position: fixed;
  259. top: 0;
  260. left: 0;
  261. z-index: 9999;
  262. width: 100vw;
  263. height: 100vh;
  264. margin-top: 0;
  265. padding: 0;
  266. }
  267. }
  268. .video-wrapper {
  269. position: relative;
  270. width: calc(100vw - 208px);
  271. height: calc((100vw - 208px) / 16 * 9);
  272. max-height: calc(100vh - 110px);
  273. max-width: calc((100vh - 110px) / 9 * 16);
  274. flex-shrink: 0;
  275. :deep(.preview-container) {
  276. width: 100%;
  277. height: 100%;
  278. }
  279. .video-container.is-fullscreen & {
  280. width: 100vw;
  281. height: 100vh;
  282. max-width: none;
  283. max-height: none;
  284. }
  285. :deep(.video_container) {
  286. width: 100%;
  287. height: 100%;
  288. background: #000;
  289. border-radius: 5px;
  290. overflow: hidden;
  291. .video-container.is-fullscreen & {
  292. border-radius: 0;
  293. }
  294. }
  295. :deep(#player) {
  296. width: 100%;
  297. height: 100%;
  298. object-fit: contain;
  299. }
  300. :deep(.video-control) {
  301. opacity: 0;
  302. transition: opacity 0.3s ease;
  303. &:hover { opacity: 1; }
  304. }
  305. &:hover :deep(.video-control) {
  306. opacity: 1;
  307. }
  308. }
  309. .fullscreen-btn {
  310. position: absolute;
  311. top: 16px;
  312. right: 16px;
  313. width: 40px;
  314. height: 40px;
  315. background: rgba(0, 0, 0, 0.6);
  316. border: none;
  317. border-radius: 8px;
  318. color: white;
  319. cursor: pointer;
  320. display: flex;
  321. align-items: center;
  322. justify-content: center;
  323. opacity: 0;
  324. transition: all 0.3s ease;
  325. backdrop-filter: blur(8px);
  326. z-index: 10;
  327. &:hover {
  328. background: rgba(0, 0, 0, 0.8);
  329. transform: scale(1.05);
  330. }
  331. &:active { transform: scale(0.95); }
  332. .video-wrapper:hover & { opacity: 1; }
  333. .video-container.is-fullscreen & {
  334. top: 20px;
  335. right: 20px;
  336. width: 48px;
  337. height: 48px;
  338. }
  339. }
  340. /* Control sidebar */
  341. .control-sidebar {
  342. flex-shrink: 0;
  343. display: flex;
  344. align-items: center;
  345. .video-container.is-fullscreen & {
  346. position: absolute;
  347. left: 20px;
  348. top: 50%;
  349. transform: translateY(-50%);
  350. z-index: 10;
  351. }
  352. }
  353. .control-panel {
  354. width: 140px;
  355. background: #f5f7fa;
  356. border-radius: 10px;
  357. padding: 12px;
  358. display: flex;
  359. flex-direction: column;
  360. gap: 10px;
  361. overflow: hidden;
  362. transition: width 0.3s ease, padding 0.3s ease, opacity 0.3s ease;
  363. &.is-collapsed {
  364. width: 0;
  365. padding: 0;
  366. opacity: 0;
  367. pointer-events: none;
  368. }
  369. .video-container.is-fullscreen & {
  370. background: rgba(0, 0, 0, 0.7);
  371. backdrop-filter: blur(12px);
  372. .control-label { color: #fff; }
  373. .control-divider { border-color: rgba(255, 255, 255, 0.15); }
  374. .ptz-btn {
  375. background: rgba(255, 255, 255, 0.1);
  376. color: #fff;
  377. border-color: rgba(255, 255, 255, 0.15);
  378. &:hover { background: rgba(255, 255, 255, 0.2); border-color: rgba(255, 255, 255, 0.3); }
  379. &:active { background: rgba(255, 255, 255, 0.3); }
  380. }
  381. .ptz-btn-center {
  382. background: rgba(64, 158, 255, 0.3) !important;
  383. border-color: rgba(64, 158, 255, 0.5) !important;
  384. &:hover { background: rgba(64, 158, 255, 0.5) !important; }
  385. }
  386. :deep(.el-input__wrapper) {
  387. background: rgba(255, 255, 255, 0.1);
  388. box-shadow: none;
  389. }
  390. :deep(.el-input__inner) { color: #fff; }
  391. :deep(.el-select__caret) { color: #fff; }
  392. }
  393. }
  394. .control-label {
  395. font-size: 12px;
  396. font-weight: 600;
  397. color: #909399;
  398. white-space: nowrap;
  399. }
  400. .control-divider {
  401. border: none;
  402. border-top: 1px solid #e4e7ed;
  403. margin: 2px 0;
  404. }
  405. .channel-selector {
  406. display: flex;
  407. flex-direction: column;
  408. gap: 6px;
  409. :deep(.el-select) {
  410. width: 100%;
  411. }
  412. }
  413. .panel-toggle {
  414. width: 24px;
  415. height: 48px;
  416. border: 1px solid #e4e7ed;
  417. border-radius: 0 8px 8px 0;
  418. background: #f5f7fa;
  419. color: #909399;
  420. cursor: pointer;
  421. display: flex;
  422. align-items: center;
  423. justify-content: center;
  424. flex-shrink: 0;
  425. transition: all 0.2s ease;
  426. padding: 0;
  427. margin-left: -1px;
  428. &:hover {
  429. background: #ecf5ff;
  430. color: #409eff;
  431. border-color: #b3d8ff;
  432. }
  433. .video-container.is-fullscreen & {
  434. background: rgba(0, 0, 0, 0.7);
  435. border-color: rgba(255, 255, 255, 0.15);
  436. color: #fff;
  437. backdrop-filter: blur(12px);
  438. &:hover {
  439. background: rgba(0, 0, 0, 0.85);
  440. border-color: rgba(255, 255, 255, 0.3);
  441. }
  442. }
  443. }
  444. .ptz-grid {
  445. display: grid;
  446. grid-template-columns: repeat(3, 1fr);
  447. gap: 4px;
  448. width: 100%;
  449. }
  450. .ptz-placeholder { aspect-ratio: 1; }
  451. .ptz-btn {
  452. width: 100%;
  453. aspect-ratio: 1;
  454. border: 1px solid #e4e7ed;
  455. border-radius: 6px;
  456. background: #fff;
  457. color: #606266;
  458. cursor: pointer;
  459. display: flex;
  460. align-items: center;
  461. justify-content: center;
  462. transition: all 0.2s ease;
  463. padding: 0;
  464. &:hover { background: #ecf5ff; border-color: #b3d8ff; color: #409eff; }
  465. &:active { background: #d9ecff; transform: scale(0.95); }
  466. }
  467. .ptz-btn-center {
  468. background: #ecf5ff !important;
  469. border-color: #b3d8ff !important;
  470. color: #409eff !important;
  471. &:hover { background: #d9ecff !important; }
  472. }
  473. @media (max-width: 768px) {
  474. .video-container {
  475. flex-direction: column;
  476. height: auto;
  477. padding: 0;
  478. gap: 12px;
  479. }
  480. .control-sidebar { order: 2; }
  481. .control-panel { width: 100%; max-width: 200px; }
  482. .video-wrapper {
  483. order: 1;
  484. width: 100vw;
  485. height: calc(100vw / 16 * 9);
  486. max-height: none;
  487. max-width: none;
  488. :deep(.video_container) { border-radius: 0; }
  489. }
  490. .fullscreen-btn { width: 36px; height: 36px; top: 12px; right: 12px; svg { width: 14px; height: 14px; } }
  491. }
  492. @media (prefers-reduced-motion: reduce) {
  493. * { transition: none !important; animation: none !important; }
  494. }
  495. </style>