App.vue 8.5 KB

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