webrtc-stream.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. <template>
  2. <div class="page-container">
  3. <div class="page-header">
  4. <span class="title">WebRTC 播放 (暂时使用go2rtc)</span>
  5. <!-- <el-tag type="success" size="small" style="margin-left: 10px">延迟 &lt; 2s</el-tag> -->
  6. </div>
  7. <!-- 配置区域 -->
  8. <div class="config-section">
  9. <el-form label-width="120px">
  10. <el-form-item label="本地RTC地址">
  11. <el-input v-model="config.go2rtcUrl" placeholder="服务地址" style="width: 400px">
  12. <template #prepend>http://</template>
  13. </el-input>
  14. <el-text type="info" style="margin-left: 10px">默认端口 1984</el-text>
  15. </el-form-item>
  16. <el-form-item label="流名称">
  17. <el-input v-model="config.streamName" placeholder="摄像头流名称,如 camera1" style="width: 300px" />
  18. </el-form-item>
  19. <!-- http://localhost:1984/api/webrtc?src=camera1 -->
  20. <el-form-item label="完整URL">
  21. <el-text>{{ fullGo2rtcUrl }}/api/webrtc?src={{ config.streamName }}</el-text>
  22. </el-form-item>
  23. <!-- <el-form-item label="生成的 URL">
  24. <el-input :value="generatedUrl" readonly style="width: 600px">
  25. <template #append>
  26. <el-button @click="copyUrl">复制</el-button>
  27. </template>
  28. </el-input>
  29. </el-form-item> -->
  30. <el-form-item>
  31. <el-button type="primary" @click="startPlay">播放</el-button>
  32. <el-button @click="handleReconnect">重连</el-button>
  33. <el-button type="danger" @click="handleStop">停止</el-button>
  34. </el-form-item>
  35. </el-form>
  36. </div>
  37. <!-- 播放器和PTZ控制区域 -->
  38. <div class="player-ptz-container">
  39. <!-- 播放器区域 -->
  40. <div class="player-section">
  41. <div v-if="!isPlaying" class="player-placeholder">
  42. <el-icon :size="60" color="#ddd"><VideoPlay /></el-icon>
  43. <p>请配置 go2rtc 地址和流名称后点击播放</p>
  44. </div>
  45. <VideoPlayer
  46. v-else
  47. ref="playerRef"
  48. player-type="webrtc"
  49. :go2rtc-url="fullGo2rtcUrl"
  50. :stream-name="config.streamName"
  51. :autoplay="playConfig.autoplay"
  52. :muted="playConfig.muted"
  53. :controls="true"
  54. @play="onPlay"
  55. @pause="onPause"
  56. @error="onError"
  57. />
  58. </div>
  59. <!-- PTZ 云台控制 -->
  60. <div class="ptz-panel">
  61. <div class="ptz-header">
  62. <span>PTZ 云台控制</span>
  63. </div>
  64. <!-- PTZ 方向控制 九宫格 -->
  65. <div class="ptz-controls">
  66. <div class="ptz-btn" @mousedown="handlePTZ('UP_LEFT')" @mouseup="handlePTZStop" @mouseleave="handlePTZStop">
  67. <el-icon><TopLeft /></el-icon>
  68. </div>
  69. <div class="ptz-btn" @mousedown="handlePTZ('UP')" @mouseup="handlePTZStop" @mouseleave="handlePTZStop">
  70. <el-icon><Top /></el-icon>
  71. </div>
  72. <div class="ptz-btn" @mousedown="handlePTZ('UP_RIGHT')" @mouseup="handlePTZStop" @mouseleave="handlePTZStop">
  73. <el-icon><TopRight /></el-icon>
  74. </div>
  75. <div class="ptz-btn" @mousedown="handlePTZ('LEFT')" @mouseup="handlePTZStop" @mouseleave="handlePTZStop">
  76. <el-icon><Back /></el-icon>
  77. </div>
  78. <div class="ptz-btn ptz-center" @click="handlePTZStop">
  79. <el-icon><Refresh /></el-icon>
  80. </div>
  81. <div class="ptz-btn" @mousedown="handlePTZ('RIGHT')" @mouseup="handlePTZStop" @mouseleave="handlePTZStop">
  82. <el-icon><Right /></el-icon>
  83. </div>
  84. <div class="ptz-btn" @mousedown="handlePTZ('DOWN_LEFT')" @mouseup="handlePTZStop" @mouseleave="handlePTZStop">
  85. <el-icon><BottomLeft /></el-icon>
  86. </div>
  87. <div class="ptz-btn" @mousedown="handlePTZ('DOWN')" @mouseup="handlePTZStop" @mouseleave="handlePTZStop">
  88. <el-icon><Bottom /></el-icon>
  89. </div>
  90. <div
  91. class="ptz-btn"
  92. @mousedown="handlePTZ('DOWN_RIGHT')"
  93. @mouseup="handlePTZStop"
  94. @mouseleave="handlePTZStop"
  95. >
  96. <el-icon><BottomRight /></el-icon>
  97. </div>
  98. </div>
  99. <!-- 速度控制 -->
  100. <div class="speed-control">
  101. <div class="control-label">
  102. <span>速度: {{ ptzSpeed }}</span>
  103. </div>
  104. <el-slider v-model="ptzSpeed" :min="10" :max="100" :step="10" :show-tooltip="true" />
  105. </div>
  106. <!-- 缩放控制 -->
  107. <div class="zoom-controls">
  108. <div class="zoom-header">
  109. <el-icon><ZoomOut /></el-icon>
  110. <span>缩放</span>
  111. <el-icon><ZoomIn /></el-icon>
  112. </div>
  113. <el-slider
  114. v-model="zoomValue"
  115. :min="-100"
  116. :max="100"
  117. :step="10"
  118. :show-tooltip="true"
  119. :format-tooltip="formatZoomTooltip"
  120. @input="handleZoomChange"
  121. @change="handleZoomRelease"
  122. />
  123. </div>
  124. </div>
  125. </div>
  126. <!-- 播放控制 -->
  127. <div class="control-section">
  128. <el-space wrap>
  129. <el-button type="primary" @click="handlePlay">播放</el-button>
  130. <el-button @click="handlePause">暂停</el-button>
  131. <el-button @click="handleScreenshot">截图</el-button>
  132. <el-button @click="handleFullscreen">全屏</el-button>
  133. <el-divider direction="vertical" />
  134. <el-switch v-model="playConfig.muted" active-text="静音" inactive-text="有声" />
  135. <el-switch v-model="playConfig.autoplay" active-text="自动播放" inactive-text="手动" />
  136. </el-space>
  137. </div>
  138. <!-- 日志区域 -->
  139. <div class="log-section">
  140. <div class="log-header">
  141. <h4>事件日志</h4>
  142. <el-button size="small" @click="logs = []">清空</el-button>
  143. </div>
  144. <div class="log-content">
  145. <div v-for="(log, index) in logs" :key="index" class="log-item" :class="log.type">
  146. <span class="time">{{ log.time }}</span>
  147. <span class="message">{{ log.message }}</span>
  148. </div>
  149. <div v-if="logs.length === 0" class="log-empty">暂无日志</div>
  150. </div>
  151. </div>
  152. </div>
  153. </template>
  154. <script setup lang="ts">
  155. import { ref, reactive, computed } from 'vue'
  156. import { ElMessage } from 'element-plus'
  157. import {
  158. VideoPlay,
  159. Top,
  160. Bottom,
  161. Back,
  162. Right,
  163. TopLeft,
  164. TopRight,
  165. BottomLeft,
  166. BottomRight,
  167. Refresh,
  168. ZoomIn,
  169. ZoomOut
  170. } from '@element-plus/icons-vue'
  171. import VideoPlayer from '@/components/VideoPlayer.vue'
  172. import { startPTZ, stopPTZ, PTZ_DIRECTIONS, startZoom, stopZoom, PTZ_ZOOM_DIRECTIONS } from '@/api/ptz'
  173. const playerRef = ref<InstanceType<typeof VideoPlayer>>()
  174. // 配置
  175. const config = reactive({
  176. go2rtcUrl: 'localhost:1984',
  177. streamName: 'camera1'
  178. })
  179. // 播放配置
  180. const playConfig = reactive({
  181. autoplay: true,
  182. muted: true
  183. })
  184. // PTZ 配置
  185. const ptzConfig = reactive({
  186. host: '192.168.0.64',
  187. username: 'admin',
  188. password: 'Wxc767718929'
  189. })
  190. // PTZ 速度和缩放
  191. const ptzSpeed = ref(50)
  192. const zoomValue = ref(0)
  193. // 播放状态
  194. const isPlaying = ref(false)
  195. // 完整的 go2rtc URL
  196. const fullGo2rtcUrl = computed(() => {
  197. if (!config.go2rtcUrl) return ''
  198. const url = config.go2rtcUrl.startsWith('http') ? config.go2rtcUrl : `http://${config.go2rtcUrl}`
  199. return url
  200. })
  201. // 生成的 WebRTC API URL
  202. const generatedUrl = computed(() => {
  203. if (!fullGo2rtcUrl.value || !config.streamName) return ''
  204. return `${fullGo2rtcUrl.value}/api/webrtc?src=${config.streamName}`
  205. })
  206. // 连接状态
  207. const connectionStatus = computed(() => {
  208. return playerRef.value?.webrtcStatus?.value || 'idle'
  209. })
  210. const statusText = computed(() => {
  211. const map: Record<string, string> = {
  212. idle: '未连接',
  213. connecting: '连接中',
  214. connected: '已连接',
  215. failed: '连接失败'
  216. }
  217. return map[connectionStatus.value] || '未知'
  218. })
  219. const statusTagType = computed(() => {
  220. const map: Record<string, '' | 'success' | 'warning' | 'danger' | 'info'> = {
  221. idle: 'info',
  222. connecting: 'warning',
  223. connected: 'success',
  224. failed: 'danger'
  225. }
  226. return map[connectionStatus.value] || 'info'
  227. })
  228. // 日志
  229. interface LogItem {
  230. time: string
  231. type: 'info' | 'success' | 'error'
  232. message: string
  233. }
  234. const logs = ref<LogItem[]>([])
  235. function addLog(message: string, type: LogItem['type'] = 'info') {
  236. const time = new Date().toLocaleTimeString()
  237. logs.value.unshift({ time, type, message })
  238. if (logs.value.length > 100) {
  239. logs.value.pop()
  240. }
  241. }
  242. // 开始播放
  243. function startPlay() {
  244. if (!config.go2rtcUrl) {
  245. ElMessage.warning('请输入 go2rtc 地址')
  246. return
  247. }
  248. if (!config.streamName) {
  249. ElMessage.warning('请输入流名称')
  250. return
  251. }
  252. isPlaying.value = true
  253. addLog(`开始 WebRTC 播放: ${config.streamName}`, 'success')
  254. }
  255. // 复制 URL
  256. function copyUrl() {
  257. if (!generatedUrl.value) {
  258. ElMessage.warning('请先配置 go2rtc 地址和流名称')
  259. return
  260. }
  261. navigator.clipboard.writeText(generatedUrl.value)
  262. ElMessage.success('已复制到剪贴板')
  263. }
  264. // 播放控制
  265. function handlePlay() {
  266. playerRef.value?.play()
  267. addLog('播放', 'info')
  268. }
  269. function handlePause() {
  270. playerRef.value?.pause()
  271. addLog('暂停', 'info')
  272. }
  273. function handleStop() {
  274. isPlaying.value = false
  275. addLog('停止播放', 'info')
  276. }
  277. function handleReconnect() {
  278. playerRef.value?.reconnect()
  279. addLog('重新连接', 'info')
  280. }
  281. function handleScreenshot() {
  282. playerRef.value?.screenshot()
  283. addLog('截图', 'info')
  284. }
  285. function handleFullscreen() {
  286. playerRef.value?.fullscreen()
  287. addLog('全屏', 'info')
  288. }
  289. // 事件处理
  290. function onPlay() {
  291. addLog('视频开始播放', 'success')
  292. }
  293. function onPause() {
  294. addLog('视频已暂停', 'info')
  295. }
  296. function onError(error: any) {
  297. addLog(`播放错误: ${error?.message || JSON.stringify(error)}`, 'error')
  298. }
  299. // PTZ 控制
  300. async function handlePTZ(direction: keyof typeof PTZ_DIRECTIONS) {
  301. if (!ptzConfig.host || !ptzConfig.username || !ptzConfig.password) {
  302. ElMessage.warning('请先配置摄像头信息')
  303. return
  304. }
  305. const result = await startPTZ(
  306. {
  307. host: ptzConfig.host,
  308. username: ptzConfig.username,
  309. password: ptzConfig.password
  310. },
  311. direction,
  312. ptzSpeed.value
  313. )
  314. if (result.success) {
  315. addLog(`PTZ 移动: ${direction} (速度: ${ptzSpeed.value})`, 'info')
  316. } else {
  317. addLog(`PTZ 控制失败: ${result.error}`, 'error')
  318. }
  319. }
  320. async function handlePTZStop() {
  321. if (!ptzConfig.host) return
  322. const result = await stopPTZ({
  323. host: ptzConfig.host,
  324. username: ptzConfig.username,
  325. password: ptzConfig.password
  326. })
  327. if (!result.success) {
  328. addLog(`PTZ 停止失败: ${result.error}`, 'error')
  329. }
  330. }
  331. // 缩放滑块控制
  332. function formatZoomTooltip(val: number) {
  333. if (val === 0) return '停止'
  334. return val > 0 ? `放大 ${val}` : `缩小 ${Math.abs(val)}`
  335. }
  336. async function handleZoomChange(val: number) {
  337. if (!ptzConfig.host || !ptzConfig.username || !ptzConfig.password) return
  338. if (val === 0) {
  339. await stopZoom({
  340. host: ptzConfig.host,
  341. username: ptzConfig.username,
  342. password: ptzConfig.password
  343. })
  344. return
  345. }
  346. const direction = val > 0 ? 'IN' : 'OUT'
  347. const speed = Math.abs(val)
  348. await startZoom(
  349. {
  350. host: ptzConfig.host,
  351. username: ptzConfig.username,
  352. password: ptzConfig.password
  353. },
  354. direction,
  355. speed
  356. )
  357. }
  358. async function handleZoomRelease() {
  359. // 松开滑块时回到中间并停止
  360. zoomValue.value = 0
  361. if (!ptzConfig.host) return
  362. await stopZoom({
  363. host: ptzConfig.host,
  364. username: ptzConfig.username,
  365. password: ptzConfig.password
  366. })
  367. addLog('缩放停止', 'info')
  368. }
  369. </script>
  370. <style lang="scss" scoped>
  371. .page-container {
  372. min-height: 100vh;
  373. background-color: var(--bg-page);
  374. }
  375. .page-header {
  376. display: flex;
  377. align-items: center;
  378. margin-bottom: 20px;
  379. padding: 15px 20px;
  380. background-color: var(--bg-container);
  381. border-radius: var(--radius-base);
  382. color: var(--text-primary);
  383. .title {
  384. font-size: 18px;
  385. font-weight: 600;
  386. }
  387. }
  388. .config-section {
  389. padding: 20px;
  390. margin-bottom: 20px;
  391. background-color: var(--bg-container);
  392. border-radius: var(--radius-base);
  393. }
  394. .player-ptz-container {
  395. padding: 20px;
  396. background-color: var(--bg-container);
  397. display: flex;
  398. gap: 20px;
  399. margin-bottom: 20px;
  400. }
  401. .player-section {
  402. flex: 1;
  403. height: 500px;
  404. border-radius: var(--radius-base);
  405. overflow: hidden;
  406. background-color: #000;
  407. .player-placeholder {
  408. height: 100%;
  409. display: flex;
  410. flex-direction: column;
  411. align-items: center;
  412. justify-content: center;
  413. color: var(--text-secondary);
  414. p {
  415. margin-top: 15px;
  416. font-size: 14px;
  417. }
  418. }
  419. }
  420. .ptz-panel {
  421. width: 280px;
  422. background-color: var(--bg-container);
  423. border-radius: var(--radius-base);
  424. padding: 15px;
  425. .ptz-header {
  426. font-size: 14px;
  427. font-weight: 600;
  428. color: var(--text-primary);
  429. margin-bottom: 15px;
  430. padding-bottom: 10px;
  431. border-bottom: 1px solid var(--border-color);
  432. }
  433. .ptz-config {
  434. margin-bottom: 15px;
  435. }
  436. .ptz-controls {
  437. display: grid;
  438. grid-template-columns: repeat(3, 1fr);
  439. gap: 8px;
  440. margin-bottom: 15px;
  441. }
  442. .ptz-btn {
  443. aspect-ratio: 1;
  444. display: flex;
  445. align-items: center;
  446. justify-content: center;
  447. background-color: var(--bg-hover);
  448. border: 1px solid var(--border-color);
  449. border-radius: var(--radius-sm);
  450. cursor: pointer;
  451. transition: all 0.2s;
  452. color: var(--text-regular);
  453. &:hover {
  454. background-color: var(--color-primary-light-9);
  455. border-color: var(--color-primary);
  456. color: var(--color-primary);
  457. }
  458. &:active {
  459. background-color: var(--color-primary);
  460. color: #fff;
  461. }
  462. .el-icon {
  463. font-size: 20px;
  464. }
  465. }
  466. .ptz-center {
  467. background-color: var(--bg-page);
  468. &:hover {
  469. background-color: var(--color-primary-light-9);
  470. }
  471. }
  472. .speed-control {
  473. margin-top: 15px;
  474. padding-top: 15px;
  475. border-top: 1px solid var(--border-color);
  476. .control-label {
  477. font-size: 12px;
  478. color: var(--text-secondary);
  479. margin-bottom: 8px;
  480. }
  481. }
  482. .zoom-controls {
  483. margin-top: 15px;
  484. padding-top: 15px;
  485. border-top: 1px solid var(--border-color);
  486. .zoom-header {
  487. display: flex;
  488. align-items: center;
  489. justify-content: space-between;
  490. font-size: 12px;
  491. color: var(--text-secondary);
  492. margin-bottom: 8px;
  493. span {
  494. flex: 1;
  495. text-align: center;
  496. }
  497. }
  498. }
  499. .ptz-speed {
  500. display: flex;
  501. align-items: center;
  502. gap: 10px;
  503. span {
  504. font-size: 12px;
  505. color: var(--text-secondary);
  506. white-space: nowrap;
  507. }
  508. .el-slider {
  509. flex: 1;
  510. }
  511. }
  512. }
  513. .control-section {
  514. padding: 15px 20px;
  515. background-color: var(--bg-container);
  516. border-radius: var(--radius-base);
  517. margin-bottom: 20px;
  518. }
  519. .status-section {
  520. margin-bottom: 20px;
  521. padding: 15px 20px;
  522. background-color: var(--bg-container);
  523. border-radius: var(--radius-base);
  524. }
  525. .info-section {
  526. margin-bottom: 20px;
  527. background-color: var(--bg-container);
  528. border-radius: var(--radius-base);
  529. .info-content {
  530. padding: 10px 0;
  531. h4 {
  532. margin: 15px 0 8px;
  533. color: var(--text-primary);
  534. font-size: 14px;
  535. &:first-child {
  536. margin-top: 0;
  537. }
  538. }
  539. p {
  540. margin: 0;
  541. color: var(--text-regular);
  542. font-size: 13px;
  543. }
  544. .code-block {
  545. background-color: var(--bg-hover);
  546. padding: 12px;
  547. border-radius: var(--radius-sm);
  548. font-family: 'Monaco', 'Menlo', monospace;
  549. font-size: 12px;
  550. line-height: 1.6;
  551. overflow-x: auto;
  552. color: var(--text-primary);
  553. }
  554. }
  555. }
  556. .log-section {
  557. padding: 15px 20px;
  558. background-color: var(--bg-container);
  559. border-radius: var(--radius-base);
  560. .log-header {
  561. display: flex;
  562. justify-content: space-between;
  563. align-items: center;
  564. margin-bottom: 10px;
  565. h4 {
  566. font-size: 14px;
  567. margin: 0;
  568. color: var(--text-primary);
  569. }
  570. }
  571. .log-content {
  572. max-height: 200px;
  573. overflow-y: auto;
  574. background-color: var(--bg-hover);
  575. border-radius: var(--radius-sm);
  576. padding: 10px;
  577. }
  578. .log-item {
  579. font-size: 12px;
  580. padding: 4px 0;
  581. border-bottom: 1px solid var(--border-color-light);
  582. color: var(--text-regular);
  583. &:last-child {
  584. border-bottom: none;
  585. }
  586. .time {
  587. color: var(--text-secondary);
  588. margin-right: 10px;
  589. }
  590. &.success .message {
  591. color: var(--color-success);
  592. }
  593. &.error .message {
  594. color: var(--color-danger);
  595. }
  596. }
  597. .log-empty {
  598. text-align: center;
  599. color: var(--text-secondary);
  600. font-size: 12px;
  601. padding: 20px 0;
  602. }
  603. }
  604. </style>