您可以阅读本文,了解视频互动直播服务端的集成操作。
前提条件
- 您已经完成注册阿里云账号,并完成实名认证,具体操作请参见阿里云账号注册流程。
- 您已经开通音视频通信服务,具体操作请参见开通服务。语音聊天室无需购买套餐也可正常搭建。
- 首次开通RTC服务后,系统会自动帮您创建一个应用项目,可以直接使用自动创建好的应用项目进行使用。如果没有应用项目,需要您在阿里云音视频通信RTC控制台创建应用。
- 您已经开通视频直播服务,详情请参见视频直播开通服务。
- 域名已成功备案,备案域名请前往阿里云ICP代备案管理系统。
- 服务端环境需要Java JDK 8的版本,具体操作,请参见安装JDK。
- 服务端环境建议使用MySQL 5.7版本。具体操作,请参见安装MySQL。
说明 Linux环境推荐安装Oracle JDK,不推荐使用Open JDK进行服务端集成。
当您启用旁路转推服务且超过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);
- 生成加入房间的Token信息
访问地址:/getRtcAuth
随机生成用户信息和客户端调用RTC SDK加入房间的Token信息。具体生成方式参考RTC帮助文档生成Token。
JSONObject rtcToken = RtcOpenAPI.createToken(channelId, userId); responseResult.setData(rtcToken); String appId = ConfigMapUtil.getValueByKey("rtc.liveclass.appId");
- 生成加入房间的Token信息
访问地址:/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);
- 获取播放地址
访问地址:/getPlayUrl
根据房间ID获取播放地址。
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);
在文档使用中是否遇到以下问题
更多建议
匿名提交