index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <div class="videoBox">
  3. <div class="box">
  4. <div class="list">
  5. <ul>
  6. <li
  7. v-for="(item, index) in cList"
  8. :key="index"
  9. @click="play(item.uuid)"
  10. >
  11. {{ item.name }}
  12. </li>
  13. </ul>
  14. </div>
  15. <video
  16. id="test"
  17. crossorigin="anonymous"
  18. autoplay
  19. playsinline
  20. muted
  21. controls
  22. />
  23. </div>
  24. <div class="contrl">
  25. <button @click="contrl(8)">
  26. </button>
  27. <button @click="contrl(2)">
  28. </button>
  29. <button @click="contrl(4)">
  30. </button>
  31. <button @click="contrl(6)">
  32. </button>
  33. <button @click="contrl(10)">
  34. 放大
  35. </button>
  36. <button @click="contrl(11)">
  37. 缩小
  38. </button>
  39. </div>
  40. </div>
  41. </template>
  42. <script setup lang="ts">
  43. import axios from 'axios'
  44. import { onMounted, onUnmounted, ref } from 'vue'
  45. let webRtc: any = null
  46. const TOKEN = '833cd2ff8851509d0c4b2cab0dbdb74d'
  47. const cList = ref()
  48. const cUUID = ref('')
  49. let timer:any = null
  50. /**
  51. * rtc 初始化
  52. * @param HOST
  53. * @param token
  54. * @param uuid
  55. * @param callBack
  56. * @param profile 0 主 1 子
  57. */
  58. const initWebRtc = async (HOST: String, token: String, uuid: string, callBack: Function, profile = 0) => {
  59. try {
  60. // init RTC
  61. const Peer = new RTCPeerConnection() as any
  62. // Peer.addTransceiver('audio', { direction: 'recvonly' })
  63. Peer.addTransceiver('video', { direction: 'recvonly' })
  64. const offer = await Peer.createOffer()
  65. await Peer.setLocalDescription(offer)
  66. // 监听视频
  67. Peer.ontrack = callBack
  68. Peer.oniceconnectionstatechange = () => {
  69. const state = Peer.iceConnectionState
  70. console.log('ICE状态', state)
  71. }
  72. Peer.onicegatheringstatechange = () => {
  73. console.log('GatheringState: ', Peer.iceGatheringState)
  74. }
  75. Peer.onconnectionstatechange = () => {
  76. const state = Peer.connectionState
  77. console.log('连接状态', state)
  78. }
  79. // SDP SRS params
  80. const params = {
  81. api: `http://${HOST}:1985/rtc/v1/play/?token=${token}&uuid=${uuid}&stream=${uuid + profile}&profile=${profile}`,
  82. clientip: null,
  83. sdp: offer.sdp,
  84. streamurl: `webrtc://${HOST}/live/${uuid + profile}?token=${token}&uuid=${uuid}&stream=${uuid + profile}&profile=${profile}`,
  85. tid: Number(Math.floor(new Date().getTime() * Math.random() * 100)).toString(16).slice(0, 7)
  86. }
  87. console.log('params', params)
  88. // 发送offer
  89. const res = await window.fetch(params.api, {
  90. method: 'POST',
  91. headers: { 'Content-Type': 'application/json' },
  92. body: JSON.stringify(params)
  93. })
  94. // 接收 answer
  95. const { sdp } = await res.json()
  96. if (sdp) await Peer.setRemoteDescription(new RTCSessionDescription({ type: 'answer', sdp }))
  97. return Peer
  98. } catch (error) {
  99. return null
  100. }
  101. }
  102. /**
  103. * 云台控制
  104. * @param command 指令:数字键盘1-9去掉5,10:焦距放大,11:焦距缩小 12:亮度,13:色彩饱和度,14:对比度,15:清晰度
  105. * @param number [云台速度|焦距参数|色彩饱和度]等值 亮度值 0-100
  106. */
  107. const contrl = (command: number, number = 1) => {
  108. if (!cUUID.value) return
  109. if (timer !== null) clearTimeout(timer)
  110. timer = setTimeout(() => {
  111. window.fetch('http://20.20.20.9:8089', {
  112. method: 'POST',
  113. headers: { 'Content-Type': 'application/json' },
  114. body: JSON.stringify({
  115. code: 'cloudcontrol.control',
  116. token: TOKEN,
  117. body: {
  118. uuid: cUUID.value, // 摄像头id
  119. command,
  120. number // [云台速度|焦距参数|色彩饱和度]等值 亮度值 0-100
  121. }
  122. })
  123. })
  124. }, 500)
  125. }
  126. /**
  127. * 播放
  128. * @param uuid
  129. */
  130. const play = async (uuid: string) => {
  131. if (!uuid) return
  132. if (uuid === cUUID.value) return
  133. if (webRtc) webRtc.close()
  134. cUUID.value = uuid
  135. webRtc = await initWebRtc('20.20.20.9', TOKEN, uuid, (event: any) => {
  136. const dom = document.getElementById('test') as any
  137. const { streams } = event
  138. dom.srcObject = streams[0]
  139. console.log('track', event)
  140. })
  141. }
  142. // 获取摄像头播放地址
  143. const getCamer = async () => {
  144. const { data } = await axios.post('http://20.20.20.9:8089', {
  145. code: 'categorycamera.list',
  146. token: TOKEN,
  147. body: {}
  148. })
  149. // 35可控
  150. cList.value = data.filter((el: { name: string }) => (el.name.includes('35')))
  151. console.log('摄像头列表', cList.value)
  152. }
  153. // 获取目录
  154. // const proList = async () => {
  155. // const { data } = await axios.post('http://20.20.20.9:8089', {
  156. // code: 'category.list',
  157. // token: '833cd2ff8851509d0c4b2cab0dbdb74d',
  158. // body: {}
  159. // })
  160. // // 零食取一个测试
  161. // console.log(666, data)
  162. // }
  163. onMounted(() => {
  164. getCamer()
  165. })
  166. onUnmounted(() => {
  167. if (webRtc) webRtc.close()
  168. if (timer) window.clearTimeout(timer)
  169. })
  170. </script>
  171. <style lang="less" scoped>
  172. .videoBox {
  173. .box {
  174. display: flex;
  175. justify-content: center;
  176. .list {
  177. width: 150px;
  178. color: white;
  179. li {
  180. padding: 10px;
  181. &:hover {
  182. color: aqua;
  183. cursor: pointer;
  184. }
  185. }
  186. }
  187. video {
  188. width: 800px;
  189. height: 500px;
  190. background: none;
  191. object-fit: fill;
  192. font-size: 0;
  193. }
  194. }
  195. .contrl {
  196. margin-top: 10px;
  197. text-align: center;
  198. button {
  199. margin-left: 10px;
  200. }
  201. }
  202. }
  203. </style>