webrtc-stream.vue 18 KB

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