接上一篇 直播录制鉴黄配置
- 人脸核身
- 直播推拉流
- 直播录制鉴黄配置
接完直播录制鉴黄等配置后,需要由腾讯云回调至服务端,服务端收到回调后做相对应的处理
创建成功后,还需到对应的推流域名下关联 回调配置,关联成功后约5分钟 - 10分钟生效。
回调配置中,用于接收回调事件的回调地址http或https服务器必须能正常收到响应。
控制台的回调模板管理为域名维度,暂时无法取消关联接口创建的规则,如果是通过直播回调相关接口关联指定流的,则需要通过调用 删除回调规则 解除关联。
直播回调相关协议,请参见 事件消息通知协议。
看完文档发现所有的事件通知是由 eventype 去判断对应的事件通知,所以这里采用策略模式来写实现
定义事件通知枚举类:
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum LiveCallBackEnum {
LIVE_PUSH(1,"直播推流"),
LIVE_STOP(0,"直播断流"),
LIVE_RECORD(100,"直播录制"),
LIVE_PORN(317,"直播鉴黄");
private int eventType;
private String handler;
public static LiveCallBackEnum getEnum(Integer type){
for (LiveCallBackEnum value : LiveCallBackEnum.values()) {
if (type==value.eventType){
return value;
}
}
return null;
}
}
策略类注解实现:
/**
* @author hzl
*/
@Target(ElementType.TYPE) //作用在类上
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited //子类可以继承此注解
public @interface HandlerEventType {
/**
* 策略类型
* @return
*/
LiveCallBackEnum value();
}
策略模式的实现方式:
/**
* @author hzl
*/
@Component
public class HandlerEventTypeContext {
@Autowired
private ApplicationContext applicationContext;
//存放所有策略类Bean的map
public static Map<LiveCallBackEnum, Class<LiveCallBackService>> strategyBeanMap= new HashMap<>();
public LiveCallBackService getStrategy(Integer type){
LiveCallBackEnum callBackEnum = LiveCallBackEnum.getEnum(type);
Class<LiveCallBackService> strategyClass = strategyBeanMap.get(callBackEnum);
if(strategyClass==null) {
return null;
}
//从容器中获取对应的策略Bean
return applicationContext.getBean(strategyClass);
}
}
/**
* @author hzl
*/
@Component
public class HandlerEventTypeProcessor implements ApplicationContextAware {
/**
* 获取所有的策略Beanclass 加入HandlerContext属性中
* @param applicationContext
* @throws BeansException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//获取所有策略注解的Bean
Map<String, Object> eventTypeMap = applicationContext.getBeansWithAnnotation(HandlerEventType.class);
eventTypeMap.forEach((k,v)->{
Class<LiveCallBackService> strategyClass = (Class<LiveCallBackService>) v.getClass();
LiveCallBackEnum value = strategyClass.getAnnotation(HandlerEventType.class).value();
//将class加入map中,type作为key
HandlerEventTypeContext.strategyBeanMap.put(value,strategyClass);
});
}
}
策略业务service:
public interface LiveCallBackService {
void execute(JSONObject json);
}
策略业务具体实现:
拉流
@Component
@HandlerEventType(LiveCallBackEnum.LIVE_PUSH)
public class LivePushCallBackServiceImpl implements LiveCallBackService {
@Override
public void execute(JSONObject json) {
LivePushCallBackResp pushCallBackResp = json.toJavaObject(LivePushCallBackResp.class);
if (pushCallBackResp.getErrcode()==0){
//do something
}
}
}
鉴黄
@Component
@HandlerEventType(LiveCallBackEnum.LIVE_PORN)
public class LivePornCallBackServiceImpl implements LiveCallBackService {
@Override
public void execute(JSONObject json) {
String liveStreamId = json.getString("streamId");
JSONArray type = json.getJSONArray("type");
//do something
}
}
录制
@Component
@HandlerEventType(LiveCallBackEnum.LIVE_RECORD)
public class LiveRecordCallBackServiceImpl implements LiveCallBackService {
@Override
public void execute(JSONObject json) {
LiveRecordCallBackResp resp = json.toJavaObject(LiveRecordCallBackResp.class);
//do something
}
}
断流
@Component
@HandlerEventType(LiveCallBackEnum.LIVE_STOP)
public class LiveStopCallBackServiceImpl implements LiveCallBackService {
@Autowired
private LiveRecordService recordService;
@Override
public void execute(JSONObject json) {
//do something
}
}
直播回调接口controller
/**
* 直播录制回调
*/
@PostMapping("live-notify")
@ResponseBody
public R liveCallBack(@RequestBody Map<String, Object> callBackMap) {
log.info("回调参数{}",callBackMap);
JSONObject params = JSONObject.parseObject(JSONObject.toJSONString(callBackMap));
Integer eventType = params.getInteger("event_type");
LiveCallBackService service = eventTypeContext.getStrategy(eventType);
service.execute(params);
return R.ok();
}
以上是策略模式实现直播回调的详情