forwardAll.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { ref } from 'vue';
  2. import { fetchRtcV1Publish } from '@/api/srs';
  3. import { SRS_CB_URL_QUERY } from '@/constant';
  4. import { useRTCParams } from '@/hooks/use-rtcParams';
  5. import { useNetworkStore } from '@/store/network';
  6. import { useUserStore } from '@/store/user';
  7. import { LiveRoomTypeEnum } from '@/types/ILiveRoom';
  8. import { WebRTCClass } from '@/utils/network/webRTC';
  9. export const useForwardAll = () => {
  10. const userStore = useUserStore();
  11. const networkStore = useNetworkStore();
  12. const { maxBitrate, maxFramerate, resolutionRatio } = useRTCParams();
  13. const currentMaxBitrate = ref(maxBitrate.value[3].value);
  14. const currentMaxFramerate = ref(maxFramerate.value[2].value);
  15. const currentResolutionRatio = ref(resolutionRatio.value[3].value);
  16. const isPk = ref(false);
  17. const roomId = ref('');
  18. const canvasVideoStream = ref<MediaStream>();
  19. function updateForwardAllConfig(data: { isPk; roomId; canvasVideoStream }) {
  20. isPk.value = data.isPk;
  21. roomId.value = data.roomId;
  22. canvasVideoStream.value = data.canvasVideoStream;
  23. }
  24. const forwardAll = {
  25. newWebRtc: (data: {
  26. sender: string;
  27. receiver: string;
  28. videoEl: HTMLVideoElement;
  29. }) => {
  30. return new WebRTCClass({
  31. maxBitrate: currentMaxBitrate.value,
  32. maxFramerate: currentMaxFramerate.value,
  33. resolutionRatio: currentResolutionRatio.value,
  34. isSRS: true,
  35. roomId: roomId.value,
  36. videoEl: data.videoEl,
  37. sender: data.sender,
  38. receiver: data.receiver,
  39. });
  40. },
  41. /**
  42. * 主播发offer给观众
  43. */
  44. sendOffer: async ({
  45. sender,
  46. receiver,
  47. }: {
  48. sender: string;
  49. receiver: string;
  50. }) => {
  51. console.log('开始ForwardAll的sendOffer', {
  52. sender,
  53. receiver,
  54. });
  55. try {
  56. const ws = networkStore.wsMap.get(roomId.value);
  57. if (!ws) return;
  58. const rtc = networkStore.rtcMap.get(receiver);
  59. if (rtc) {
  60. canvasVideoStream.value?.getTracks().forEach((track) => {
  61. if (canvasVideoStream.value) {
  62. console.log(
  63. 'ForwardAll的canvasVideoStream插入track',
  64. track.kind,
  65. track
  66. );
  67. rtc.peerConnection?.addTrack(track, canvasVideoStream.value);
  68. }
  69. });
  70. const offerSdp = await rtc.createOffer();
  71. if (!offerSdp) {
  72. console.error('ForwardAll的offerSdp为空');
  73. window.$message.error('ForwardAll的offerSdp为空');
  74. return;
  75. }
  76. await rtc.setLocalDescription(offerSdp!);
  77. const liveRooms = userStore.userInfo?.live_rooms;
  78. const myLiveRoom = liveRooms?.[0];
  79. if (!myLiveRoom) {
  80. window.$message.error('你没有开通直播间');
  81. return;
  82. }
  83. const answerRes = await fetchRtcV1Publish({
  84. sdp: offerSdp.sdp!,
  85. streamurl: `${myLiveRoom.rtmp_url!}?${
  86. SRS_CB_URL_QUERY.publishKey
  87. }=${myLiveRoom.key!}&${SRS_CB_URL_QUERY.publishType}=${
  88. isPk.value ? LiveRoomTypeEnum.pk : LiveRoomTypeEnum.forward_all
  89. }`,
  90. });
  91. if (answerRes.data.code !== 0) {
  92. console.error('/rtc/v1/publish/拿不到sdp');
  93. window.$message.error('/rtc/v1/publish/拿不到sdp');
  94. return;
  95. }
  96. await rtc.setRemoteDescription(
  97. new RTCSessionDescription({
  98. type: 'answer',
  99. sdp: answerRes.data.sdp,
  100. })
  101. );
  102. } else {
  103. console.error('rtc不存在');
  104. }
  105. } catch (error) {
  106. console.error('ForwardAll的sendOffer错误');
  107. console.log(error);
  108. }
  109. },
  110. };
  111. return { updateForwardAllConfig, forwardAll };
  112. };