live.js 7.5 KB

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