index.vue 12 KB

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