index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <template>
  2. <div class="push-wrap">
  3. <div
  4. ref="topRef"
  5. class="left"
  6. >
  7. <div
  8. ref="containerRef"
  9. class="container"
  10. >
  11. <div class="video-wrap">
  12. <video
  13. id="localVideo"
  14. ref="localVideoRef"
  15. autoplay
  16. webkit-playsinline="true"
  17. playsinline
  18. x-webkit-airplay="allow"
  19. x5-video-player-type="h5"
  20. x5-video-player-fullscreen="true"
  21. x5-video-orientation="portraint"
  22. muted
  23. ></video>
  24. <div
  25. v-if="currMediaTypeList.length > 0"
  26. class="controls"
  27. >
  28. <VideoControls></VideoControls>
  29. </div>
  30. <div
  31. v-if="!currMediaTypeList || currMediaTypeList.length <= 0"
  32. class="add-wrap"
  33. >
  34. <n-space>
  35. <n-button
  36. class="item"
  37. @click="startGetUserMedia"
  38. >
  39. 摄像头
  40. </n-button>
  41. <n-button
  42. class="item"
  43. @click="startGetDisplayMedia"
  44. >
  45. 窗口
  46. </n-button>
  47. </n-space>
  48. </div>
  49. </div>
  50. <div class="sidebar">
  51. <div class="title">在线人员</div>
  52. <div
  53. v-for="(item, index) in sidebarList"
  54. :key="index"
  55. class="item"
  56. >
  57. <video
  58. :ref="(el) => (remoteVideoRef[item.socketId] = el)"
  59. autoplay
  60. webkit-playsinline="true"
  61. playsinline
  62. x-webkit-airplay="allow"
  63. x5-video-player-type="h5"
  64. x5-video-player-fullscreen="true"
  65. x5-video-orientation="portraint"
  66. muted
  67. ></video>
  68. <div>{{ item.socketId }}</div>
  69. </div>
  70. </div>
  71. </div>
  72. <div
  73. ref="bottomRef"
  74. class="room-control"
  75. >
  76. <div class="info">
  77. <div
  78. class="avatar"
  79. :style="{ backgroundImage: `url(${userStore.userInfo?.avatar})` }"
  80. ></div>
  81. <div class="detail">
  82. <div class="top">
  83. <n-input-group>
  84. <n-input
  85. v-model:value="roomName"
  86. size="small"
  87. placeholder="输入房间名"
  88. :style="{ width: '50%' }"
  89. :disabled="disabled"
  90. />
  91. <n-button
  92. size="small"
  93. type="primary"
  94. :disabled="disabled"
  95. @click="confirmRoomName"
  96. >
  97. 确定
  98. </n-button>
  99. </n-input-group>
  100. </div>
  101. <div class="bottom">
  102. <span>socketId:{{ getSocketId() }}</span>
  103. </div>
  104. </div>
  105. </div>
  106. <div class="other">
  107. <div class="top">
  108. <span class="item">
  109. <i class="ico"></i>
  110. <span>正在观看人数:{{ liveUserList.length }}</span>
  111. </span>
  112. </div>
  113. <div class="bottom">
  114. <n-space>
  115. <n-button
  116. type="info"
  117. size="small"
  118. @click="startLive"
  119. >
  120. 开始直播
  121. </n-button>
  122. <n-button
  123. type="info"
  124. size="small"
  125. @click="endLive"
  126. >
  127. 结束直播
  128. </n-button>
  129. </n-space>
  130. </div>
  131. </div>
  132. </div>
  133. </div>
  134. <div class="right">
  135. <div class="resource-card">
  136. <div class="title">素材列表</div>
  137. <div class="list">
  138. <div
  139. v-for="(item, index) in currMediaTypeList"
  140. :key="index"
  141. class="item"
  142. >
  143. <span class="name">{{ item.txt }}</span>
  144. </div>
  145. </div>
  146. </div>
  147. <div class="danmu-card">
  148. <div class="title">弹幕互动</div>
  149. <div class="list-wrap">
  150. <div class="list">
  151. <div
  152. v-for="(item, index) in damuList"
  153. :key="index"
  154. class="item"
  155. >
  156. <template v-if="item.msgType === DanmuMsgTypeEnum.danmu">
  157. <span class="name">{{ item.socketId }}:</span>
  158. <span class="msg">{{ item.msg }}</span>
  159. </template>
  160. <template v-else-if="item.msgType === DanmuMsgTypeEnum.otherJoin">
  161. <span class="name system">系统通知:</span>
  162. <span class="msg">{{ item.socketId }}进入直播!</span>
  163. </template>
  164. <template
  165. v-else-if="item.msgType === DanmuMsgTypeEnum.userLeaved"
  166. >
  167. <span class="name system">系统通知:</span>
  168. <span class="msg">{{ item.socketId }}离开直播!</span>
  169. </template>
  170. </div>
  171. </div>
  172. </div>
  173. <div class="send-msg">
  174. <input
  175. v-model="danmuStr"
  176. class="ipt"
  177. @keydown="keydownDanmu"
  178. />
  179. <n-button
  180. type="info"
  181. size="small"
  182. @click="sendDanmu"
  183. >
  184. 发送
  185. </n-button>
  186. </div>
  187. </div>
  188. </div>
  189. </div>
  190. </template>
  191. <script lang="ts" setup>
  192. import { onMounted, onUnmounted, ref } from 'vue';
  193. import { useRoute } from 'vue-router';
  194. import { usePush } from '@/hooks/use-push';
  195. import { DanmuMsgTypeEnum, liveTypeEnum } from '@/interface';
  196. import { useUserStore } from '@/store/user';
  197. const route = useRoute();
  198. const userStore = useUserStore();
  199. const liveType = route.query.liveType;
  200. const topRef = ref<HTMLDivElement>();
  201. const bottomRef = ref<HTMLDivElement>();
  202. const containerRef = ref<HTMLDivElement>();
  203. const localVideoRef = ref<HTMLVideoElement>();
  204. const remoteVideoRef = ref<HTMLVideoElement[]>([]);
  205. const {
  206. initPush,
  207. confirmRoomName,
  208. getSocketId,
  209. startGetDisplayMedia,
  210. startGetUserMedia,
  211. startLive,
  212. endLive,
  213. closeWs,
  214. closeRtc,
  215. sendDanmu,
  216. keydownDanmu,
  217. disabled,
  218. danmuStr,
  219. roomName,
  220. damuList,
  221. liveUserList,
  222. currMediaTypeList,
  223. sidebarList,
  224. } = usePush({
  225. localVideoRef,
  226. remoteVideoRef,
  227. isSRS: liveType === liveTypeEnum.srsPush,
  228. });
  229. onUnmounted(() => {
  230. closeWs();
  231. closeRtc();
  232. });
  233. onMounted(() => {
  234. initPush();
  235. if (topRef.value && bottomRef.value && containerRef.value) {
  236. const res =
  237. bottomRef.value.getBoundingClientRect().top -
  238. topRef.value.getBoundingClientRect().top;
  239. containerRef.value.style.height = `${res}px`;
  240. }
  241. });
  242. </script>
  243. <style lang="scss" scoped>
  244. .push-wrap {
  245. margin: 20px auto 0;
  246. min-width: $large-width;
  247. height: 700px;
  248. text-align: center;
  249. .left {
  250. position: relative;
  251. display: inline-block;
  252. overflow: hidden;
  253. box-sizing: border-box;
  254. width: $large-left-width;
  255. height: 100%;
  256. border-radius: 6px;
  257. background-color: white;
  258. color: #9499a0;
  259. vertical-align: top;
  260. .container {
  261. display: flex;
  262. align-items: center;
  263. justify-content: space-between;
  264. height: 100%;
  265. background-color: #fff;
  266. .video-wrap {
  267. position: relative;
  268. display: flex;
  269. flex: 1;
  270. justify-content: center;
  271. height: 100%;
  272. background-color: rgba($color: #000000, $alpha: 0.5);
  273. #localVideo {
  274. max-width: 100%;
  275. max-height: 100%;
  276. }
  277. .controls {
  278. display: none;
  279. }
  280. &:hover {
  281. .controls {
  282. display: block;
  283. }
  284. }
  285. .add-wrap {
  286. position: absolute;
  287. top: 50%;
  288. left: 50%;
  289. display: flex;
  290. align-items: center;
  291. justify-content: space-around;
  292. padding: 0 20px;
  293. height: 50px;
  294. border-radius: 5px;
  295. background-color: white;
  296. transform: translate(-50%, -50%);
  297. }
  298. }
  299. .sidebar {
  300. width: 130px;
  301. height: 100%;
  302. background-color: rgba($color: #000000, $alpha: 0.3);
  303. .title {
  304. color: white;
  305. }
  306. .join {
  307. color: white;
  308. cursor: pointer;
  309. }
  310. video {
  311. max-width: 100%;
  312. }
  313. }
  314. }
  315. .room-control {
  316. position: absolute;
  317. right: 0;
  318. bottom: 0;
  319. left: 0;
  320. display: flex;
  321. justify-content: space-between;
  322. padding: 20px;
  323. background-color: papayawhip;
  324. .info {
  325. display: flex;
  326. align-items: center;
  327. .avatar {
  328. margin-right: 20px;
  329. width: 64px;
  330. height: 64px;
  331. border-radius: 50%;
  332. background-color: skyblue;
  333. background-position: center center;
  334. background-size: cover;
  335. background-repeat: no-repeat;
  336. }
  337. .detail {
  338. display: flex;
  339. flex-direction: column;
  340. text-align: initial;
  341. .top {
  342. margin-bottom: 10px;
  343. color: #18191c;
  344. .btn {
  345. margin-left: 10px;
  346. }
  347. }
  348. .bottom {
  349. font-size: 14px;
  350. }
  351. }
  352. }
  353. .other {
  354. display: flex;
  355. flex-direction: column;
  356. justify-content: center;
  357. font-size: 12px;
  358. .top {
  359. display: flex;
  360. align-items: center;
  361. .item {
  362. display: flex;
  363. align-items: center;
  364. margin-right: 20px;
  365. .ico {
  366. display: inline-block;
  367. margin-right: 4px;
  368. width: 10px;
  369. height: 10px;
  370. border-radius: 50%;
  371. background-color: skyblue;
  372. }
  373. }
  374. }
  375. .bottom {
  376. margin-top: 10px;
  377. }
  378. }
  379. }
  380. }
  381. .right {
  382. position: relative;
  383. display: inline-block;
  384. box-sizing: border-box;
  385. margin-left: 10px;
  386. width: 240px;
  387. height: 100%;
  388. border-radius: 6px;
  389. background-color: white;
  390. color: #9499a0;
  391. .resource-card {
  392. box-sizing: border-box;
  393. margin-bottom: 5%;
  394. margin-bottom: 10px;
  395. padding: 10px;
  396. width: 100%;
  397. height: 290px;
  398. border-radius: 6px;
  399. background-color: papayawhip;
  400. .title {
  401. text-align: initial;
  402. }
  403. .item {
  404. display: flex;
  405. align-items: center;
  406. justify-content: space-between;
  407. margin: 5px 0;
  408. font-size: 12px;
  409. }
  410. }
  411. .danmu-card {
  412. box-sizing: border-box;
  413. padding: 10px;
  414. width: 100%;
  415. height: 400px;
  416. border-radius: 6px;
  417. background-color: papayawhip;
  418. text-align: initial;
  419. .title {
  420. margin-bottom: 10px;
  421. }
  422. .list {
  423. margin-bottom: 10px;
  424. height: 300px;
  425. overflow: scroll;
  426. .item {
  427. margin-bottom: 10px;
  428. font-size: 12px;
  429. .name {
  430. color: #9499a0;
  431. }
  432. .msg {
  433. color: #61666d;
  434. }
  435. }
  436. }
  437. .send-msg {
  438. display: flex;
  439. align-items: center;
  440. box-sizing: border-box;
  441. .ipt {
  442. display: block;
  443. box-sizing: border-box;
  444. margin: 0 auto;
  445. margin-right: 10px;
  446. padding: 10px;
  447. width: 80%;
  448. height: 30px;
  449. outline: none;
  450. border: 1px solid hsla(0, 0%, 60%, 0.2);
  451. border-radius: 4px;
  452. background-color: #f1f2f3;
  453. font-size: 14px;
  454. }
  455. }
  456. }
  457. }
  458. }
  459. // 屏幕宽度小于$large-width的时候
  460. @media screen and (max-width: $large-width) {
  461. .push-wrap {
  462. .left {
  463. width: $medium-left-width;
  464. }
  465. .right {
  466. .list {
  467. .item {
  468. }
  469. }
  470. }
  471. }
  472. }
  473. </style>