通过阅读本文,您可以了解视频互动直播服务端的集成操作。
前提条件
使用限制
如果启用旁路转推服务超过10个并发量时,您需要和客户经理沟通或提交工单(选择视频直播)。
操作步骤
主要功能说明
- 获取正在直播的房间列表。
访问地址:/getChannelList
List<Channel> channelList = channelService.getChannelList(lastChannelId, pageSize); String appId = ConfigMapUtil.getValueByKey("rtc.liveclass.appId"); ResponseResult responseResult = new ResponseResult(); Iterator<Channel> channelIterator = channelList.iterator(); while (channelIterator.hasNext()) { Channel channel = channelIterator.next(); String channelId = channel.getChannelId(); DescribeChannelUsersResponse response = RtcOpenAPI.describeChannelUsers(appId, channelId); if (CollectionUtils.isEmpty(response.getUserList())) { channelService.deleteChannel(channelId); channelIterator.remove(); } } responseResult.setData(channelList);
- 随机生成用户信息和客户端调用RTC SDK加入房间的Token信息。具体生成方式参考RTC帮助文档接入工具。
访问地址:/getRtcAuth
JSONObject rtcToken = RtcOpenAPI.createToken(channelId, userId); responseResult.setData(rtcToken); String appId = ConfigMapUtil.getValueByKey("rtc.liveclass.appId");
- 开启旁路直播。
访问地址:/startMPUTask
StartMPUTaskResponse response = RtcOpenAPI.startMPUTask(channelId, pushUrl, channelId, appId, userId); Channel channel = new Channel(); channel.setChannelId(channelId); channel.setOwnerId(userId); channel.setCoverUrl(coverUrl); channel.setTitle(title); channel.setCreateDateTime(LocalDateTime.now()); channelService.insertChannel(channel);
- 根据房间ID获取播放地址。
访问地址:/getPlayUrl
String livePlayDomain = ConfigMapUtil.getValueByKey("live_play_domain"); String livePlayDomainAuthKey = ConfigMapUtil.getValueByKey("live_play_domain_auth_key"); String appStream = ConfigMapUtil.getValueByKey("live_app_stream_name"); Long timestamp = System.currentTimeMillis() / 1000; String rand = UUID.randomUUID().toString().replace("-", ""); ResponseResult responseResult = new ResponseResult(); // 原画 String playUrlRtmp = "rtmp://" + livePlayDomain + AuthKeyUrlUtil.getAuthedPath(appStream + channelId, livePlayDomainAuthKey, timestamp, rand); String playUrlFlv = "https://" + livePlayDomain + AuthKeyUrlUtil.getAuthedPath(appStream + channelId + ".flv", livePlayDomainAuthKey, timestamp, rand); String playUrlM3u8 = "https://" + livePlayDomain + AuthKeyUrlUtil.getAuthedPath(appStream + channelId + ".m3u8", livePlayDomainAuthKey, timestamp, rand); JSONObject playUrl = new JSONObject(); playUrl.put("rtmp", playUrlRtmp); playUrl.put("flv", playUrlFlv); playUrl.put("m3u8", playUrlM3u8);
- 房间人数变化时更新布局。
访问地址:/updateMPULayout
List<String> userList = getSortedUserList(channelId); UpdateMPULayoutRequest request = new UpdateMPULayoutRequest(); String appId = ConfigMapUtil.getValueByKey("rtc.liveclass.appId"); request.setAppId(appId); request.setTaskId(channelId); List<UpdateMPULayoutRequest.UserPanes> userPanesList = new LinkedList<>(); for (int i = 0; i < userList.size(); i++) { UpdateMPULayoutRequest.UserPanes userPanes = new UpdateMPULayoutRequest.UserPanes(); userPanes.setUserId(userList.get(i)); userPanes.setPaneId(i); userPanes.setSourceType("camera"); userPanesList.add(userPanes); } request.setUserPaness(userPanesList); String layout1Id = ConfigMapUtil.getValueByKey("layout_1"); String layout2Id = ConfigMapUtil.getValueByKey("layout_2"); String layout3Id = ConfigMapUtil.getValueByKey("layout_3"); List layoutIdArr = new ArrayList(); layoutIdArr.add(layout1Id); layoutIdArr.add(layout2Id); layoutIdArr.add(layout3Id); request.setLayoutIdss(layoutIdArr); UpdateMPULayoutResponse response = RtcOpenAPI.updateMPULayout(request); log.info(JSON.toJSONString(response));
- 主播关闭直播后关闭旁路转推任务。
访问地址:/stopMPUTask
StopMPUTaskResponse response = RtcOpenAPI.stopMPUTaskRequest(request); RtcOpenAPI.deleteChannel(appId, channelId); channelService.endChannel(channelId, LocalDateTime.now()); return responseResult;
- 查询正在上麦的用户列表。
访问地址:/getUserList
ResponseResult responseResult = new ResponseResult(); List<String> userList; userList = getSortedUserList(channelId);
- 将指定终端用户从频道踢出。
调用访问地址:/removeTerminals
DescribeChannelUsersResponse response = RtcOpenAPI.describeChannelUsers(appId, channelId); List<String> userList = response.getUserList(); userList.remove(operatorId); RtcOpenAPI.removeTerminals(appId, channelId, userList);