cloudflare.vue 16 KB

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