App.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <script setup lang="ts">
  2. import { onUnmounted, ref, watch } from 'vue'
  3. import Login from '@/components/login.vue'
  4. import Gauge from '@/components/gauge.vue'
  5. import Record from '@/components/record.vue'
  6. import Battery from '@/components/battery.vue'
  7. import Loading from '@/components/loading.vue'
  8. const sleep = (ms: number) => new Promise((res) => setTimeout(res, ms))
  9. const iceServers = [
  10. {
  11. urls: [ 'stun:caner.top:3478' ]
  12. },
  13. {
  14. urls: 'turn:caner.top:3478',
  15. username: 'admin',
  16. credential: '123456'
  17. }
  18. ]
  19. const Peer = ref(null as null | RTCPeerConnection | undefined)
  20. const isLogin = ref(false)
  21. const remoteVideo = ref()
  22. const showLoading = ref(true)
  23. const winMaxOrMin = ref(false)
  24. const micState = ref(false)
  25. const audioState = ref(false)
  26. const audioStateNum = ref(0)
  27. const warnAudio = ref(false)
  28. const quantity = ref(0)
  29. const error = ref('')
  30. const conctrlData = ref({
  31. v0: 128, v1: 128, v2: 128, v3: 128
  32. })
  33. const conctrlAnimation = ref(0)
  34. const conctrlGrears = ref(false)
  35. const conctrlNum = ref(0)
  36. const SpeedValue = ref(0)
  37. // 发送控制数据
  38. function sendContrlData() {
  39. if (showLoading.value) return
  40. window.$electron.send('sendMqtt', { type: 'conctrl', conctrl: { ...conctrlData.value } })
  41. conctrlAnimation.value = requestAnimationFrame(sendContrlData)
  42. }
  43. // 关闭
  44. function close(err?: string) {
  45. if (Peer.value) Peer.value?.close()
  46. if (remoteVideo.value) remoteVideo.value.srcObject = null
  47. isLogin.value = false
  48. showLoading.value = false
  49. error.value = err || ''
  50. Peer.value = null
  51. audioStateNum.value = 0
  52. quantity.value = 0
  53. cancelAnimationFrame(conctrlAnimation.value)
  54. }
  55. // 初始化rtc
  56. function initRTC() {
  57. try {
  58. Peer.value = new RTCPeerConnection({
  59. iceServers,
  60. bundlePolicy: 'max-bundle'
  61. })
  62. // listen state
  63. Peer.value.onicegatheringstatechange = () => {
  64. console.log('GatheringState: ', Peer.value?.iceGatheringState)
  65. if (Peer.value?.iceGatheringState === 'complete') {
  66. const answer = Peer.value.localDescription
  67. window.$electron.send('sendMqtt', answer)
  68. }
  69. }
  70. // listen track
  71. Peer.value.ontrack = async (evt) => {
  72. console.log('track', evt)
  73. remoteVideo.value.srcObject = evt.streams[0]
  74. }
  75. // listen changestate·
  76. Peer.value.oniceconnectionstatechange = async () => {
  77. const state = Peer.value?.iceConnectionState
  78. console.log('ICE状态', state)
  79. if (
  80. state === 'failed'
  81. || state === 'disconnected'
  82. || state === 'closed'
  83. ) {
  84. close('P2P通信失败')
  85. }
  86. // ICE连接成功
  87. if (state === 'connected') {
  88. await sleep(3000)
  89. showLoading.value = false
  90. sendContrlData()
  91. }
  92. }
  93. console.log('rtc 初始化成功')
  94. } catch (error) {
  95. close('webrtc初始化失败')
  96. }
  97. }
  98. // 登录
  99. function login(data: { name: string, room: string, url: string }) {
  100. window.$electron.send('loginMqtt', { room: data.room, name: data.name, url: data.url })
  101. }
  102. // 档位计算
  103. function countContrlData(v: number) {
  104. // 转10进制
  105. const num = parseInt(v.toString(), 10)
  106. if (conctrlNum.value % 2) {
  107. // 倒档
  108. return Math.floor(num / 2)
  109. }
  110. // 前进
  111. return Math.floor(((128 - (num / 2)) + 128))
  112. }
  113. // 发送语音
  114. function sendAudio(blob: Blob) {
  115. window.$electron.send('sendMqtt', { type: 'Meadia', Meadia: blob })
  116. }
  117. // 窗口事件
  118. function titleEvent(type: string) {
  119. if (type === 'maxWin') winMaxOrMin.value = !winMaxOrMin.value
  120. if (type === 'loginOut') {
  121. close()
  122. window.$electron.send('closeMqtt')
  123. } else {
  124. window.$electron?.send(type, winMaxOrMin.value)
  125. }
  126. }
  127. // mqtt
  128. window.$electron.on('message', async (msg: any) => {
  129. switch (msg.type) {
  130. case 'connect':
  131. console.log('连接成功')
  132. isLogin.value = true
  133. showLoading.value = true
  134. break
  135. case 'disconnect':
  136. close('服务器连接失败')
  137. break
  138. case 'join':
  139. window.$electron.send('sendMqtt', { type: 'startRTC' })
  140. initRTC()
  141. break
  142. case 'leave':
  143. close('对方断开连接')
  144. break
  145. case 'offer':
  146. {
  147. await Peer.value?.setRemoteDescription(msg)
  148. const answerd = await Peer.value?.createAnswer()
  149. await Peer.value?.setLocalDescription(answerd)
  150. }
  151. break
  152. case 'power':
  153. quantity.value = msg.power
  154. console.log('电量')
  155. break
  156. case 'speed':
  157. SpeedValue.value = Math.floor(msg.data)
  158. console.log('速度')
  159. break
  160. case 'contrl':
  161. {
  162. const db = msg.content
  163. // 倒档 | 前进 =>右拨片
  164. conctrlGrears.value = db[6] === 2
  165. // 鸣笛 =>回车
  166. warnAudio.value = !!db[54]
  167. // 录音 =>左拨片
  168. micState.value = db[6] === 1
  169. // 静音 =>L3
  170. audioState.value = db[6] === 64
  171. // 控制
  172. conctrlData.value = {
  173. v0: 128,
  174. v1: 128,
  175. v2: parseInt(db[44].toString(), 10), // 方向盘
  176. v3: countContrlData(db[46]) // 油门
  177. }
  178. }
  179. console.log('遥控数据', conctrlData.value)
  180. break
  181. default:
  182. break
  183. }
  184. })
  185. // 关闭loadingwin
  186. window.$electron.send('close-loading')
  187. // 监听按钮状态
  188. watch([ audioState, warnAudio, conctrlGrears ], () => {
  189. if (showLoading.value) return
  190. if (audioState.value) {
  191. audioStateNum.value++
  192. window.$electron.send('sendMqtt', { type: 'contrlAudio', contrlAudio: !!(audioStateNum.value % 2) })
  193. }
  194. if (conctrlGrears.value) {
  195. conctrlNum.value++
  196. }
  197. if (warnAudio.value) window.$electron.send('sendMqtt', { type: 'warnAudio', contrlAudio: !!(audioStateNum.value % 2) })
  198. })
  199. onUnmounted(() => close())
  200. </script>
  201. <template>
  202. <template v-if="isLogin">
  203. <video
  204. ref="remoteVideo"
  205. autoplay
  206. playsinline
  207. />
  208. <div class="marke">
  209. <div class="marke-left">
  210. <!-- 音频状态 -->
  211. <Record
  212. class="marke-audio"
  213. :size="25"
  214. :audio-state="micState"
  215. @callBack="sendAudio"
  216. />
  217. <!-- 喇叭状态 -->
  218. <Icon
  219. name="audio"
  220. :size="25"
  221. :color="(!!(audioStateNum % 2)) ? '#00CED1' : 'rgba(0, 0, 0, 0.3)'"
  222. />
  223. <!-- 电量状态 -->
  224. <Battery :quantity="quantity" />
  225. </div>
  226. <div class="marke-right">
  227. <Icon
  228. name="min"
  229. :size="20"
  230. color="#fff"
  231. @click="titleEvent('minWin')"
  232. />
  233. <Icon
  234. :name="winMaxOrMin ? 'max' : 'maxMin'"
  235. :size="20"
  236. color="#fff"
  237. @click="titleEvent('maxWin')"
  238. />
  239. <Icon
  240. name="close"
  241. :size="20"
  242. color="#fff"
  243. @click="titleEvent('loginOut')"
  244. />
  245. </div>
  246. </div>
  247. <!-- 码数 -->
  248. <div class="gauge">
  249. <Gauge
  250. :value="SpeedValue"
  251. :gears="conctrlNum % 2 ? '倒档' : '前进'"
  252. />
  253. </div>
  254. <Loading v-if="showLoading" />
  255. </template>
  256. <Login
  257. v-else
  258. v-model="error"
  259. @loginBack="login"
  260. />
  261. </template>
  262. <style scoped lang="scss">
  263. video {
  264. background: black;
  265. object-fit: fill;
  266. font-size: 0;
  267. }
  268. .marke {
  269. position: fixed;
  270. top: 0;
  271. left: 0;
  272. width: available;
  273. width: -webkit-fill-available;
  274. height: 0.35rem;
  275. z-index: 1;
  276. background: #666666;
  277. display: flex;
  278. align-items: center;
  279. justify-content: flex-end;
  280. overflow: hidden;
  281. border-top-left-radius: 20px;
  282. border-top-right-radius: 20px;
  283. padding-left: 1.5rem;
  284. &>div {
  285. display: flex;
  286. align-items: center;
  287. }
  288. &-left {
  289. justify-content: center;
  290. flex: 1;
  291. -webkit-app-region: drag;
  292. &>* {
  293. margin: 0 0.18rem;
  294. }
  295. }
  296. &-right {
  297. display: flex;
  298. align-items: center;
  299. margin-right: 0.15rem;
  300. &>* {
  301. cursor: pointer;
  302. &:not(:first-child) {
  303. margin-left: 0.15rem;
  304. }
  305. }
  306. }
  307. }
  308. .gauge {
  309. position: fixed;
  310. bottom: 0;
  311. left: 50%;
  312. transform: translate(-50%, 0);
  313. width: 3rem;
  314. height: 1.65rem;
  315. z-index: 9;
  316. }
  317. /* 隐藏滚动条 */
  318. ::-webkit-scrollbar {
  319. width: 0 !important;
  320. display: none;
  321. }
  322. </style>