webrtc-stream.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. <template>
  2. <div class="page-container">
  3. <div class="page-header">
  4. <span class="title">WebRTC 播放</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. background-color: var(--bg-page);
  373. height: 100%;
  374. overflow: auto;
  375. }
  376. .page-header {
  377. display: flex;
  378. align-items: center;
  379. margin-bottom: 20px;
  380. padding: 15px 20px;
  381. background-color: var(--bg-container);
  382. border-radius: var(--radius-base);
  383. color: var(--text-primary);
  384. .title {
  385. font-size: 18px;
  386. font-weight: 600;
  387. }
  388. }
  389. .config-section {
  390. padding: 20px;
  391. margin-bottom: 20px;
  392. background-color: var(--bg-container);
  393. border-radius: var(--radius-base);
  394. }
  395. .player-ptz-container {
  396. padding: 20px;
  397. background-color: var(--bg-container);
  398. display: flex;
  399. gap: 20px;
  400. margin-bottom: 20px;
  401. }
  402. .player-section {
  403. flex: 1;
  404. height: 500px;
  405. border-radius: var(--radius-base);
  406. overflow: hidden;
  407. background-color: #000;
  408. .player-placeholder {
  409. height: 100%;
  410. display: flex;
  411. flex-direction: column;
  412. align-items: center;
  413. justify-content: center;
  414. color: var(--text-secondary);
  415. p {
  416. margin-top: 15px;
  417. font-size: 14px;
  418. }
  419. }
  420. }
  421. .ptz-panel {
  422. width: 280px;
  423. background-color: var(--bg-container);
  424. border-radius: var(--radius-base);
  425. padding: 15px;
  426. .ptz-header {
  427. font-size: 14px;
  428. font-weight: 600;
  429. color: var(--text-primary);
  430. margin-bottom: 15px;
  431. padding-bottom: 10px;
  432. border-bottom: 1px solid var(--border-color);
  433. }
  434. .ptz-config {
  435. margin-bottom: 15px;
  436. }
  437. .ptz-controls {
  438. display: grid;
  439. grid-template-columns: repeat(3, 1fr);
  440. gap: 8px;
  441. margin-bottom: 15px;
  442. }
  443. .ptz-btn {
  444. aspect-ratio: 1;
  445. display: flex;
  446. align-items: center;
  447. justify-content: center;
  448. background-color: var(--bg-hover);
  449. border: 1px solid var(--border-color);
  450. border-radius: var(--radius-sm);
  451. cursor: pointer;
  452. transition: all 0.2s;
  453. color: var(--text-regular);
  454. &:hover {
  455. background-color: var(--color-primary-light-9);
  456. border-color: var(--color-primary);
  457. color: var(--color-primary);
  458. }
  459. &:active {
  460. background-color: var(--color-primary);
  461. color: #fff;
  462. }
  463. .el-icon {
  464. font-size: 20px;
  465. }
  466. }
  467. .ptz-center {
  468. background-color: var(--bg-page);
  469. &:hover {
  470. background-color: var(--color-primary-light-9);
  471. }
  472. }
  473. .speed-control {
  474. margin-top: 15px;
  475. padding-top: 15px;
  476. border-top: 1px solid var(--border-color);
  477. .control-label {
  478. font-size: 12px;
  479. color: var(--text-secondary);
  480. margin-bottom: 8px;
  481. }
  482. }
  483. .zoom-controls {
  484. margin-top: 15px;
  485. padding-top: 15px;
  486. border-top: 1px solid var(--border-color);
  487. .zoom-header {
  488. display: flex;
  489. align-items: center;
  490. justify-content: space-between;
  491. font-size: 12px;
  492. color: var(--text-secondary);
  493. margin-bottom: 8px;
  494. span {
  495. flex: 1;
  496. text-align: center;
  497. }
  498. }
  499. }
  500. .ptz-speed {
  501. display: flex;
  502. align-items: center;
  503. gap: 10px;
  504. span {
  505. font-size: 12px;
  506. color: var(--text-secondary);
  507. white-space: nowrap;
  508. }
  509. .el-slider {
  510. flex: 1;
  511. }
  512. }
  513. }
  514. .control-section {
  515. padding: 15px 20px;
  516. background-color: var(--bg-container);
  517. border-radius: var(--radius-base);
  518. margin-bottom: 20px;
  519. }
  520. .status-section {
  521. margin-bottom: 20px;
  522. padding: 15px 20px;
  523. background-color: var(--bg-container);
  524. border-radius: var(--radius-base);
  525. }
  526. .info-section {
  527. margin-bottom: 20px;
  528. background-color: var(--bg-container);
  529. border-radius: var(--radius-base);
  530. .info-content {
  531. padding: 10px 0;
  532. h4 {
  533. margin: 15px 0 8px;
  534. color: var(--text-primary);
  535. font-size: 14px;
  536. &:first-child {
  537. margin-top: 0;
  538. }
  539. }
  540. p {
  541. margin: 0;
  542. color: var(--text-regular);
  543. font-size: 13px;
  544. }
  545. .code-block {
  546. background-color: var(--bg-hover);
  547. padding: 12px;
  548. border-radius: var(--radius-sm);
  549. font-family: 'Monaco', 'Menlo', monospace;
  550. font-size: 12px;
  551. line-height: 1.6;
  552. overflow-x: auto;
  553. color: var(--text-primary);
  554. }
  555. }
  556. }
  557. .log-section {
  558. padding: 15px 20px;
  559. background-color: var(--bg-container);
  560. border-radius: var(--radius-base);
  561. .log-header {
  562. display: flex;
  563. justify-content: space-between;
  564. align-items: center;
  565. margin-bottom: 10px;
  566. h4 {
  567. font-size: 14px;
  568. margin: 0;
  569. color: var(--text-primary);
  570. }
  571. }
  572. .log-content {
  573. max-height: 200px;
  574. overflow-y: auto;
  575. background-color: var(--bg-hover);
  576. border-radius: var(--radius-sm);
  577. padding: 10px;
  578. }
  579. .log-item {
  580. font-size: 12px;
  581. padding: 4px 0;
  582. border-bottom: 1px solid var(--border-color-light);
  583. color: var(--text-regular);
  584. &:last-child {
  585. border-bottom: none;
  586. }
  587. .time {
  588. color: var(--text-secondary);
  589. margin-right: 10px;
  590. }
  591. &.success .message {
  592. color: var(--color-success);
  593. }
  594. &.error .message {
  595. color: var(--color-danger);
  596. }
  597. }
  598. .log-empty {
  599. text-align: center;
  600. color: var(--text-secondary);
  601. font-size: 12px;
  602. padding: 20px 0;
  603. }
  604. }
  605. </style>