index.vue 11 KB

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