App.vue 8.0 KB

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