首先需要创建一个一对一直播源码的直播间
我这边大致列举了一些功能:标题、描述、聊天室id(后面再讲)、推流地址、拉流地址、直播状态。下面是直播间表结构
package cn.fht.carsys.domain.tencentlive;
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
/**
* 2020/4/10
* 直播间表
*/
public class LiveRoom implements Serializable{
private Long id ;//精品直播主键
private String roomTitle ;//直播间标题
private String roomDesc ;//直播间描述/简介
private String chatroomId ;//聊天室ID
private String pushUrl ;//推流地址
private String pullUrl ;//HTTP拉流地址
private String pullRtmpUrl ;//RTMP拉流地址
private String videoUrl ;//录播视频url
private Integer status ;//直播状态:0-初始化 1-直播中 2-已结束
@Temporal(TemporalType.TIMESTAMP)
private Date createTime ;//创建时间
@PrePersist
private void PrePersist(){
if (this.status == null){
this.status = 0 ;
}
if (this.createTime == null ) {
this.createTime = new Timestamp(System.currentTimeMillis());
}
}
}
然后向手机端提供了直播申请接口
/**
* 直播申请地址
* @param request
* @return
*/
@SystemControllerLog(modelName = "LiveBroadcast.getPushUrl", systemLogType = SystemLogType.CONTROLLER, description = "获取直播推流地址和拉流地址")
@RequestMapping(value = "/tokenCheck/getPushUrl")
@ResponseBody
public Result getPushUrl(HttpServletRequest request ){
AppLoginInfo appLoginInfo = (AppLoginInfo) request.getAttribute("appLoginInfo");//获取用户对象
LiveRoom liveRoom = (LiveRoom) _JsonUtils.toObj(request.getAttribute("jsonParamEntity") + "", LiveRoom.class);
Users users = usersService.findByName(appLoginInfo.getAccount()) ;
if (users == null ){
return ResultUtil.showError(DealLiveBroadcastResultEnum.FAIL_NULLUSERID_MESSAGE);
}
String pushUrl = TecentCloudUtils.getPushUrl(users.getUserID()+"");//推流地址
String pullUrl = TecentCloudUtils.getPullUrl(users.getUserID()+"");//app的拉流
String pullRtmpUrl = TecentCloudUtils.getPullRtmpUrl(users.getUserID()+"");//PC的拉流
liveRoom.setPushUrl(pushUrl);//推流地址
liveRoom.setPullUrl(pullUrl);//app拉流地址
liveRoom.setPullRtmpUrl(pullRtmpUrl);//pc拉流地址
liveRoom.setChatroomId("");//聊天室ID
liveRoom.setVideoUrl("");//录播视频url
LiveRoom DB_one = liveRoomService.save(liveRoom) ;
if (DB_one == null ){
return ResultUtil.showError(DealLiveBroadcastResultEnum.FAIL_SAVE) ;
}
return ResultUtil.showSuccess(DealLiveBroadcastResultEnum.SUCCESS_SAVE,DB_one) ;
}
service层的接口
@Resource
private LiveRoomRepository liveRoomRepository ;
public LiveRoom save(LiveRoom liveRoom){
LiveRoom DB = findByUserId(liveRoom.getUserId()) ;
if (DB == null ){
return liveRoomRepository.save(liveRoom) ;
}
return DB ;
}
public LiveRoom findById(Long id) {
return liveRoomRepository.findOne(id) ;
}
public LiveRoom findByUserId(Long userId) {
String query = "AND_EQ_L_userId@"+userId ;
return liveRoomRepository.findOne(Specifications.get(query,LiveRoom.class) );
}
有需要其他接口的自己补充,这样一个简单的一对一直播源码的直播间就搞定了。