useTRTC.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import consola from 'consola';
  2. import { computed, ref, unref } from 'vue';
  3. // import TRTC from 'trtc-js-sdk';
  4. import type { Client } from 'trtc-js-sdk';
  5. import { useRtcStore } from '/@/store/modules/rtc';
  6. // import Dialog from '/@/components/basic/dialog';
  7. import {
  8. createAgoraClient,
  9. AgoraClient,
  10. unMutedAudio as argoraUnMutedAudio,
  11. mutedAudio as argoraMutedAudio,
  12. unMuteVideo as argoraUnMuteVideo,
  13. muteVideo as argoraMuteVideo,
  14. switchToFrontCam as argoraSwitchToFrontCam,
  15. switchToBackCam as argoraSwitchToBackCam,
  16. cleanAll as argoraCleanAll,
  17. isHasMicrophone as argoraIsHasMicrophone,
  18. isHasCamera as argoraIsHasCamera,
  19. } from './useAgora';
  20. import {
  21. createRTCSocket as createTXRTCSocket,
  22. unMutedAudio as txUnMutedAudio,
  23. mutedAudio as txMutedAudio,
  24. unMuteVideo as txUnMuteVideo,
  25. muteVideo as txMuteVideo,
  26. switchToFrontCam as txSwitchToFrontCam,
  27. switchToBackCam as txSwitchToBackCam,
  28. } from './useTencent';
  29. import { useMiniApp } from './useMiniApp';
  30. import { useRoom } from './useRoom';
  31. let localClient: Client | AgoraClient;
  32. const sdkType = ref(0);
  33. // const muteAudioLeader = ref(false);
  34. export const muteVideoLeader = ref(true);
  35. export const useCameraFront = ref(true);
  36. const { isUsingMiniApp } = useMiniApp();
  37. const globalVideoEnable = computed(
  38. () => Number(import.meta.env.VITE_ENABLE_VIDEO) === 1 && !unref(isUsingMiniApp),
  39. );
  40. //rtc- 腾讯云与声网结合
  41. // console.log('getCameras', await AgoraRTC.getCameras());
  42. // const isInitRTC = computed(() => useRtcStore().isInitRTC);
  43. async function initRtcSDK(): Promise<void> {
  44. createRTCSocket();
  45. }
  46. async function createRTCSocket(): Promise<void> {
  47. try {
  48. if (!unref(isUsingMiniApp)) {
  49. const { getSign } = useRoom();
  50. const rtcStore = useRtcStore();
  51. let uid = rtcStore.userId;
  52. if (rtcStore.role === 'leader') {
  53. uid = `${rtcStore.roomId}-${rtcStore.userId}`;
  54. }
  55. const res = await getSign(uid, rtcStore.roomId, rtcStore.role, 'fire');
  56. // console.warn('web rtc入口', res);
  57. const { sign, operatorType, sdkAppId } = res;
  58. sdkType.value = Number(operatorType);
  59. if (Number(operatorType) === 1) {
  60. localClient = await createAgoraClient(String(sdkAppId), rtcStore.roomId, sign, uid);
  61. } else if (operatorType === 0) {
  62. localClient = await createTXRTCSocket(
  63. String(sdkAppId),
  64. rtcStore.roomId,
  65. sign,
  66. rtcStore.userId,
  67. );
  68. //腾讯云
  69. }
  70. } else {
  71. console.log('小程序关闭rtc入口');
  72. }
  73. } catch (error) {
  74. consola.error({
  75. tag: 'createRTCSocket',
  76. message: error,
  77. });
  78. }
  79. }
  80. const handleLeave = () => {
  81. //通用离开方法
  82. localClient && localClient.leave();
  83. console.error('rtc-leave');
  84. };
  85. const mutedAudio = () => {
  86. //通用禁音方法
  87. if (sdkType.value === 1) {
  88. argoraMutedAudio();
  89. }
  90. if (sdkType.value === 0) {
  91. txMutedAudio();
  92. }
  93. //通用离开方法
  94. };
  95. const unMutedAudio = () => {
  96. //通用开音方法
  97. if (sdkType.value === 1) {
  98. argoraUnMutedAudio();
  99. }
  100. if (sdkType.value === 0) {
  101. txUnMutedAudio();
  102. }
  103. };
  104. const muteVideo = () => {
  105. //通用开音方法
  106. if (sdkType.value === 1) {
  107. argoraMuteVideo();
  108. }
  109. if (sdkType.value === 0) {
  110. txMuteVideo();
  111. }
  112. muteVideoLeader.value = true;
  113. };
  114. const unMuteVideo = () => {
  115. //通用开音方法
  116. if (sdkType.value === 1) {
  117. argoraUnMuteVideo();
  118. }
  119. if (sdkType.value === 0) {
  120. txUnMuteVideo();
  121. }
  122. muteVideoLeader.value = false;
  123. };
  124. const switchToFrontCam = () => {
  125. //通用前置摄像方法
  126. if (sdkType.value === 1) {
  127. argoraSwitchToFrontCam();
  128. }
  129. if (sdkType.value === 0) {
  130. txSwitchToFrontCam();
  131. }
  132. useCameraFront.value = true;
  133. };
  134. const switchToBackCam = () => {
  135. //通用后置摄像方法
  136. if (sdkType.value === 1) {
  137. argoraSwitchToBackCam();
  138. }
  139. if (sdkType.value === 0) {
  140. txSwitchToBackCam();
  141. }
  142. useCameraFront.value = false;
  143. };
  144. const cleanAll = () => {
  145. if (sdkType.value === 1) {
  146. argoraCleanAll();
  147. }
  148. };
  149. const isHasMicrophone = computed(() => {
  150. if (sdkType.value === 1) {
  151. return argoraIsHasMicrophone.value;
  152. } else {
  153. return argoraIsHasMicrophone.value;
  154. }
  155. });
  156. const isHasCamera = computed(() => {
  157. if (sdkType.value === 1) {
  158. return argoraIsHasCamera.value;
  159. } else {
  160. return argoraIsHasCamera.value;
  161. }
  162. });
  163. export function useRtcSdk() {
  164. return {
  165. initRtcSDK,
  166. handleLeave,
  167. mutedAudio,
  168. unMutedAudio,
  169. muteVideo,
  170. unMuteVideo,
  171. muteVideoLeader,
  172. switchToFrontCam,
  173. switchToBackCam,
  174. useCameraFront,
  175. client: localClient,
  176. globalVideoEnable,
  177. cleanAll,
  178. isHasMicrophone,
  179. isHasCamera,
  180. };
  181. }