group.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import { Message } from 'element-ui';
  2. import { rtc, publishStream, closeVideo, createData, getHistory } from '@/views/live/common';
  3. export {
  4. initSDK,
  5. sendMsg,
  6. getLiveStat,
  7. createScript,
  8. downloadWebSDK,
  9. closeVideo,
  10. handsDown,
  11. chatRoll,
  12. pauseAudio,
  13. playAudio,
  14. pauseVideo,
  15. playVideo
  16. } from '@/views/live/common';
  17. /**
  18. * 取消订阅远程流
  19. */
  20. export function unSubscribeStream(unSubStream) {
  21. rtc.unSubscribeStream({
  22. unSubStream,
  23. success(str) {
  24. console.log('取消订阅流成功', str);
  25. },
  26. fail(str) {
  27. console.log(str);
  28. }
  29. });
  30. }
  31. function createLocalStream(vue) {
  32. rtc.createLocalStream({
  33. streamName: 'picture',
  34. createData: createData(),
  35. success(stream) {
  36. vue.localStream = true;
  37. console.log('创建本地流成功', stream);
  38. vue.hasVideo = stream.hasVideo;
  39. vue.hasAudio = stream.hasAudio;
  40. // 创建本地流成功,将流展示到id为 student 的dom元素盒子中
  41. stream.show('group-local');
  42. publishStream('picture'); // 如果需要立即推流,执行 publish 方法
  43. },
  44. fail(data) {
  45. vue.localStream = false;
  46. console.log('创建本地流失败,应用层处理', data);
  47. // 创建本地流失败,应用层处理
  48. Message({
  49. type: 'error',
  50. message: '创建本地流失败:' + data
  51. });
  52. }
  53. });
  54. }
  55. /**
  56. * 初始化监听事件
  57. */
  58. export function initListener(vue) {
  59. rtc.on('login_success', data => {
  60. console.log('登录成功', data);
  61. Message({
  62. message: '登录成功',
  63. type: 'success'
  64. });
  65. });
  66. rtc.on('login_failed', data => {
  67. console.log('登录失败', data);
  68. Message({
  69. message: '登录失败:' + JSON.stringify(data),
  70. type: 'warning'
  71. });
  72. });
  73. // 教师 必须在加入房间成功的事件回调里创建本地流
  74. rtc.on('conference_join', () => {
  75. console.log('加入房间成功');
  76. getHistory({
  77. success(data) {
  78. let chatLog = data.datas.meta.chatLog.map(({ content, userName }) => {
  79. return { msg: content, username: userName };
  80. });
  81. vue.chatList = chatLog;
  82. },
  83. fail(str) {
  84. console.log(str);
  85. }
  86. });
  87. if (vue.studentSelf.is_teacher_role_in_room === 'false') {
  88. rtc.handsUp({
  89. success: str => {
  90. console.log('举手成功', str);
  91. console.log('创建本地流推流');
  92. createLocalStream(vue);
  93. },
  94. fail: data => {
  95. console.log('举手失败', data);
  96. }
  97. });
  98. } else {
  99. createLocalStream(vue);
  100. }
  101. });
  102. rtc.on('conference_join_failed', err => {
  103. // 加入房间失败 err为错误原因
  104. console.log('加入房间失败', err);
  105. });
  106. // 新增订阅流事件
  107. rtc.on('allow_sub', stream => {
  108. console.log('新增订阅流事件');
  109. if (stream.isMixed()) {
  110. console.log('是混合流,不订阅');
  111. } else {
  112. // 订阅远程流
  113. rtc.trySubscribeStream({
  114. tryStream: stream,
  115. success: stream => {
  116. // 订阅流成功
  117. let streamType = stream.streamType();
  118. console.log('订阅流成功', streamType);
  119. if (streamType === 10) {
  120. vue.streamList.push(stream);
  121. }
  122. if (streamType === 0) {
  123. stream.show('live', 'contain'); // 将流显示到指定 id 的元素中
  124. vue.is_teacher_in_group = true;
  125. }
  126. },
  127. fail(err) {
  128. console.log('订阅流失败', err);
  129. }
  130. });
  131. }
  132. });
  133. // 推流前查询直播状态失败,导致没有推流
  134. rtc.on('local_stream_publish_failed', () => {
  135. console.log('推流前查询直播状态失败,导致没有推流');
  136. Message({
  137. message: '推流前查询直播状态失败,导致没有推流',
  138. type: 'warning'
  139. });
  140. });
  141. // 房间全量信息事件(人员进出时广播)
  142. rtc.on('room_context', roomData => {
  143. vue.roomContext = JSON.parse(roomData);
  144. console.log('房间全量信息事件(人员进出时广播)', JSON.parse(roomData));
  145. });
  146. rtc.on('publish_stream', str => {
  147. console.log('直播已开启', str);
  148. Message({
  149. type: 'success',
  150. message: '直播已开启'
  151. });
  152. vue.liveStat = true;
  153. });
  154. rtc.on('end_stream', str => {
  155. console.log('分组讨论已关闭', str);
  156. Message({
  157. type: 'success',
  158. message: '分组讨论已关闭'
  159. });
  160. vue.liveStat = false;
  161. });
  162. // 单个用户配置监听
  163. rtc.on('switch_user_settings', settingData => {
  164. console.log('单个用户配置监听', JSON.parse(settingData));
  165. });
  166. // 人员列表事件(人员麦序变化时广播)
  167. rtc.on('speak_context', speakData => {
  168. vue.speakData = JSON.parse(speakData);
  169. console.log('人员列表事件(人员麦序变化时广播)', JSON.parse(speakData));
  170. });
  171. rtc.on('switch_settings', data => {
  172. console.log('房间设置事件', JSON.parse(data)); // 房间设置事件
  173. });
  174. rtc.on('publishStreamErr', data => {
  175. console.log('推流意外终止:' + data.streamName);
  176. // 直播开启状态下,尝试重推这条流
  177. });
  178. rtc.on('kick_out', function () {
  179. console.log('自己被踢出房间');
  180. });
  181. // 视频无法自动播放
  182. rtc.on('playError', data => {
  183. console.log('视频无法自动播放', data);
  184. });
  185. // 监听通知移除流事件
  186. rtc.on('stream_removed', stream => {
  187. console.log('监听通知移除流事件');
  188. if (stream.streamType() === 0) {
  189. vue.is_teacher_in_group = false;
  190. }
  191. let num = vue.streamList.findIndex(el => el.id() === stream.id());
  192. if (num !== -1) {
  193. vue.streamList.splice(num, 1);
  194. }
  195. });
  196. // 停止订阅流
  197. rtc.on('unSub', stream => {
  198. console.log('停止订阅流');
  199. unSubscribeStream(stream);
  200. });
  201. /**
  202. * 排麦监听事件
  203. */
  204. // 监听自己被邀请事件
  205. rtc.on('inviteUp', uid => {
  206. console.log('监听自己被邀请事件', uid);
  207. });
  208. rtc.on('mcDown', () => {
  209. closeVideo('picture');
  210. });
  211. rtc.on('videoModeChange', data => {
  212. console.log('连麦音视频模式更新成功监听回调', data.settings.video_mode);
  213. vue.roomInfo.video_mode = data.settings.video_mode;
  214. });
  215. rtc.on('createLocalStream', () => {
  216. console.log('创建本地流推流');
  217. });
  218. /**
  219. * 监听聊天事件
  220. */
  221. rtc.on('chat_message', data => {
  222. let dat = JSON.parse(data);
  223. // 敏感词过滤:如果发送的聊天消息被系统判定包含敏感词,则只有发送者能收到本条消息,房间内其他人都不会收到这条聊天消息。
  224. // 如果返回消息中有 isFilterChat 字段(消息不包含敏感词返回数据中无isFilterChat字段),且isFilterChat的值为1,则说明该消息包含敏感字,除发送者外其他人不会收到这条消息。
  225. if (dat.isFilterChat && dat.isFilterChat === 1) {
  226. return;
  227. }
  228. vue.chatList.push(dat);
  229. });
  230. // 接收自定义消息
  231. rtc.on('publish_message', data => {});
  232. }