一对一直播源码开发——java服务器端直播间的实现

首先需要创建一个一对一直播源码的直播间

我这边大致列举了一些功能:标题、描述、聊天室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) );
}

有需要其他接口的自己补充,这样一个简单的一对一直播源码的直播间就搞定了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值