近来公司有个直播类的项目,让我调研下阿里云直播,花了3、4天调研结束后,做下笔记,一方面自己日后便于查看,另一方面与大家做个交流,有说的不正确的。欢迎指正。
需求说明:本项目有三个媒体端,分别是app、微信和pc。下面分别讲述下三个媒体端的场景。
-
app端是给讲师直播用的
-
微信端是给学员看直播用的
-
pc端是给管理员用的
下面介绍下阿里云的直播使用:
1.在app端需要推流到阿里云cdn,需要应用服务器生成一个推流地址(有推流地址就能拼出拉流地址),且配置回调监听推流状态.(如图-1所示)
2.在微信端需要拉流地址(直播地址),需要从应用服务器拿到拉流地址
3.配置直播录播转点播后,直播推流中断三分钟后,直播的视频会被传输到点播,此时配置一个回调地址(针对两种情况进行回调,如图-2所示)
a.直播视频已经被传输到点播(注意:此时已经得到videoid,但是视频还在转码,还是无法播放)
b.传输到点播的视频已经转码成功(此时可以播放视频了)
package com.yx.tsing.course.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.yx.tsing.common.ConsHint;
import com.yx.tsing.common.Constant;
import com.yx.tsing.common.result.JsonPageResult;
import com.yx.tsing.common.result.JsonResult;
import com.yx.tsing.course.domain.Course;
import com.yx.tsing.course.service.CourseService;
import com.yx.tsing.course.vo.*;
import com.yx.tsing.utils.AliUtil;
import com.yx.tsing.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.MessageFormat;
import java.util.Date;
/**
* create by zml on 2018/3/6 16:52
*/
@RestController
@RequestMapping("/course")
public class CourseController {
@Autowired
private CourseService courseService;
@Value("#{configProperties['live.appName']}")
private String appName;
@Value("#{configProperties['live.streamName']}")
private String streamName;
@Value("#{configProperties['live.vhost']}")
private String vhost;
@Value("#{configProperties['live.key']}")
private String key;
/**
* 根据课程id拿到推流地址
* @param id
* @throws Exception
*/
@RequestMapping(value = "/pushUrl")
public JsonResult pushUrl(long id) {
//传入自定义参数,即传入应用名称和流名称
// String AppName = "tsonline";
String StreamName = streamName +"_"+id;
// String vhost = "live.ayxsoft.top";
//时间戳,有效时间
long time = System.currentTimeMillis() + 1800;
//加密key,即直播后台鉴权里面自行设置
// String key = "qreVeWGNRC";
String strpush = "/" + appName + "/" + StreamName + "-" + time + "-0-0-" + key;
// 里面的直播推流中心服务器域名、vhost域名可根据自身实际情况进行设置
String pushurl = "rtmp://video-center.alivecdn.com/" + appName + "/" + StreamName + "?vhost=" + vhost + "&auth_key=" + time + "-0-0-" + MD5Util.getMD5(strpush);
// $strviewrtmp = "/$AppName/$StreamName-$time-0-0-$key";
// $strviewflv = "/$AppName/$StreamName.flv-$time-0-0-$key";
String strviewm3u8 = "/" + appName + "/" + StreamName + ".m3u8-" + time + "-0-0-" + key;
String rtmpurl = "rtmp://" + vhost + "/" + appName + "/" + StreamName + "?auth_key=" + time + "-0-0-" + MD5Util.getMD5(strpush);
// $flvurl = "http://live1.playzhan.com/$AppName/$StreamName.flv?auth_key=$time-0-0-".md5($strviewflv);
String m3u8url = "http://" + vhost + "/" + appName + "/" + StreamName + ".m3u8?auth_key=" + time + "-0-0-" + MD5Util.getMD5(strviewm3u8);
//打印推流地址,即通过鉴权签名后的推流地址
System.out.println("推流地址== " + pushurl);
//打印三种直播协议播放地址,即鉴权后的播放地址
System.out.println("播放地址rtmp PushUrlVo==" + rtmpurl);
// echo $flvurl.'<br>';
System.out.println("播放地址m3u8url==" + m3u8url);
//保存播放地址到数据库
courseService.playUrl(id,rtmpurl+";"+m3u8url);
return new JsonResult(true,"success!",new PushUrlVo(pushurl));
}
/**
* 阿里云直播平台通知用户当前域名推流状态,例如推流成功或者断流
* 中心推流回调(判断课程是否在直播)
* @throws Exception
*/
@RequestMapping(value = "/pushCallBack")
public void pushCallBack(@RequestParam(value = "action",defaultValue = "",required = false) String action,
@RequestParam(value = "app",defaultValue = "",required = false) String app,
@RequestParam(value = "appname",defaultValue = "",required = false) String appname,
@RequestParam(value = "id",defaultValue = "",required = false) String id,
@RequestParam(value = "ip",defaultValue = "",required = false) String ip,
@RequestParam(value = "node",defaultValue = "",required = false) String node) {
System.out.println("action=="+action);//publish和publish_done
System.out.println("app=="+app);//
System.out.println("appname=="+appname);//
System.out.println("id=="+id);//
System.out.println("ip=="+ip);//171.13.130.227
System.out.println("node=="+node);//et2
long courseId = Long.valueOf(id.split("_")[1]);
int type = 1;
if("publish".equals(action)){
type = 2;
}else if("publish_done".equals(action)){
type = 3;
}
courseService.setType(courseId ,type);
}
/**
* 1.开启直播录制到点播后,当完成一个录制周期或直播流结束时会生成一个点播视频,此时阿里云点播平台会回调此接口
* 2.视频在点播平台转码成功会回调此接口
* 点播回调
* @param request
* @param response
* @throws Exception
*/
@RequestMapping(value = "/playCallBack")
public void callBack(HttpServletRequest request, HttpServletResponse response) throws Exception {
String c_length=request.getHeader("content-length");
int c_i=Integer.parseInt(c_length);
//解析阿里回调服务器的接口数据
String result = AliUtil.GetPostBody(request.getInputStream(), c_i);
System.out.println("resultCallBack=="+result);
JSONObject jsonObject = JSON.parseObject(result);
String EventType = jsonObject.getString("EventType");
String Status = jsonObject.getString("Status");
if("AddLiveRecordVideoComplete".equals(EventType)){ //第一种情况,直播录制转点播成功
if("success".equals(Status)){
//保存返回来的文件信息
//{"Status":"success","VideoId":"e5524b741330406c99e08698433dd387","StreamName":"StreamName_1","RecordStartTime":"2018-03-06T07:51:38Z","EventType":"AddLiveRecordVideoComplete","DomainName":"live.ayxsoft.top","RecordEndTime":"2018-03-06T07:53:47Z","UserId":1023641609718688,"EventTime":"2018-03-06T07:56:47Z","AppName":"tsonline"}
String videoId = jsonObject.getString("VideoId");
String StreamName = jsonObject.getString("StreamName");
long courseId = Long.valueOf(StreamName.split("_")[1]);
Course course = new Course();
course.setId(courseId);
course.setType(3);
course.setRecordPath(videoId);
course.setUpdateTime(new Date());
courseService.updateByPrimaryKeySelective(course);
//返回给点播服务器
AliUtil.response(request, response, "{\"Status\":\"OK\"}", HttpServletResponse.SC_OK);
} else {
AliUtil.response(request, response, "{\"Status\":\"verdify not ok\"}", HttpServletResponse.SC_BAD_REQUEST);
}
}else if("TranscodeComplete".equals(EventType)){//第二种情况,视频在点播平台转码成功
if("success".equals(Status)){
//保存返回来的文件信息d
//{"VideoId": "43q9fjasjdflask","Status": "success","StreamInfos":[],"EventTime": "2017-03-20T07:49:17Z","EventType": "TranscodeComplete"}
String videoId = jsonObject.getString("VideoId");
String status = jsonObject.getString("Status");
if("success".equals(status)){
Example example = new Example(Course.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("recordPath",videoId);
criteria.andEqualTo("type",3);//只考虑 直播录播到点播转码成功 的情况
Course course = new Course();
course.setType(4);
course.setUpdateTime(new Date());
courseService.updateByExampleSelective(course,example);
}
//返回给点播服务器
AliUtil.response(request, response, "{\"Status\":\"OK\"}", HttpServletResponse.SC_OK);
} else {
AliUtil.response(request, response, "{\"Status\":\"verdify not ok\"}", HttpServletResponse.SC_BAD_REQUEST);
}
}
}
}
下面介绍下阿里云的点播使用:
1.上传,上传时需要请求应用服务器拿到上传权限和上传地址
2.播放.播放有两种方式,一种是用阿里云播放器,另一种是使用自有、开源播放器
第一种,需要播放凭证(很安全,确保视频不会被盗用)或者播放地址(可以开启鉴权使生成的地址有时效性,相对安全);
第二种只需要播放地址即可
版权声明:本文为haohao123nana原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。