index.vue 12 KB

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