cloudflareStream.vue 16 KB

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