index.vue 13 KB

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