JAVA 对接 声网(agora) 屏幕截图功能实现直播监控

2021/6/1 12:21:08

本文主要是介绍JAVA 对接 声网(agora) 屏幕截图功能实现直播监控,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

描述:

在做直播,社交类项目的时候,通常会用到一些im流监控的手段,来监控视频防止一些涉黄等不良东西出现,然后运营人员能直接监控,并且下线,警告等功能,比如直播的超管就能随时封禁直播间。今天我们就来对接声网的

接口,来实现监控的功能。

流程图

在这里插入图片描述
上面就是大概的业务逻辑,接下来就是看看声网的接口了。

视频录制监控

接口文档 : 云端录制文档
在这里插入图片描述

根据自己的业务来选择调用那个接口,我这里就先用视频截图,的接口了。

https://docs.agora.io/cn/cloud-recording/cloud_recording_screen_capture?platform=RESTful

请大家一定要先看好文档的没一个细节,避免入坑。

获取 resource ID

第一个接口,获取resource Id ,调用这个接口,成功后,会获取 resource Id ,然后再视频截图的接口中会需要用到。
在这里插入图片描述

这里是需要生成加密的一个认证,在其他的接口中,都会用到,所以我们弄成共用的方法来使用。

    public String getKey(){
        // 拼接客户 ID 和客户密钥并使用 base64 编码
        String plainCredentials = agoraToConfig.getAppKey() + ":" + agoraToConfig.getAppSecret();
        String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes()));
        // 创建 authorization header
        String authorizationHeader = "Basic " + base64Credentials;
        return  authorizationHeader;
    }

    public String getAcquire(String cname,String uid) {
        JSONObject jsonObject = JSONUtil.createObj();
        JSONObject object = JSONUtil.createObj();
        object.set("resourceExpiredHour",24);
        object.set("scene",0);
        jsonObject.set("cname",cname);
        jsonObject.set("uid",uid);
        jsonObject.set("clientRequest",object);

        String authorization = HttpRequest.post(ACQUIRE.replaceAll("<yourappid>",agoraToConfig.getAppId()))
                .header("Authorization", this.getKey())
                .header("Content-Type", "application/json;charset=utf-8")
                .body(jsonObject.toString()).execute().body();
        JSONObject jsonObject1 = JSONUtil.parseObj(authorization);
        String resourceId = jsonObject1.getStr("resourceId");
        System.out.println(resourceId);
        return resourceId;
    }

代码中使用了hutool 工具类,大家都加入一下依赖。

       <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.6.5</version>
        </dependency>

这里有一个小的细节,请大家注意,在官方给的文档参数中,这个参数是要在URL中替换的

https://api.agora.io/v1/apps/<yourappid>/cloud_recording/acquire

yourappid 替换成你的appid 这里注意了,不是请求参数,而是直接url上面替换参数。

在这里插入图片描述

开始录制

在这里插入图片描述

https://docs.agora.io/cn/cloud-recording/cloud_recording_api_rest?platform=RESTful#start

详细文档,请大家一定要注意观看,并且cname和uid都是保持一致的,不然就不能成功。

    public String modeStart() throws  Exception{
       String cname = RandomUtil.randomNumbers(6);
        String uid = RandomUtil.randomString("123456789",4);
        JSONObject jsonObject = JSONUtil.createObj();
        JSONObject clientRequest = JSONUtil.createObj();
        clientRequest.set("clientRequest",jsonObject);
        clientRequest.set("cname",cname);
        clientRequest.set("uid",uid);
        jsonObject.set("token",this.getChannelKey(cname,Long.valueOf(uid)).getData());

        JSONObject recordingConfig = JSONUtil.createObj();
        recordingConfig.set("channelType",0);
        recordingConfig.set("subscribeUidGroup",3);
        jsonObject.set("recordingConfig",recordingConfig);

        JSONObject snapshotConfig = JSONUtil.createObj();
        snapshotConfig.set("captureInterval",5);
        JSONArray array = JSONUtil.createArray();
        array.add("jpg");
        snapshotConfig.set("fileType",array.toArray());
        jsonObject.set("snapshotConfig",snapshotConfig);

        JSONObject storageConfig = JSONUtil.createObj();
        storageConfig.set("vendor",0);
        storageConfig.set("region",2);
        storageConfig.set("bucket",agoraToConfig.getBucket());
        storageConfig.set("accessKey",agoraToConfig.getAccessKey());
        storageConfig.set("secretKey",agoraToConfig.getSecretKey());
        JSONArray fileNamePrefix = JSONUtil.createArray();
        fileNamePrefix.add("monitor");
        storageConfig.set("fileNamePrefix",fileNamePrefix.toArray());

        jsonObject.set("storageConfig",storageConfig);
        String url = MODESTART.replaceAll("<yourappid>", agoraToConfig.getAppId()).replaceAll("<resourceid>", this.getAcquire(cname, uid));
        String authorization = HttpRequest.post(url)
                .header("Authorization", this.getKey())
                .header("Content-Type", "application/json;charset=utf-8")
                .body(clientRequest.toString()).execute().body();
        JSONObject object = JSONUtil.createObj();
        JSONObject parseObj = JSONUtil.parseObj(authorization);
        object.set("resourceId",parseObj.getStr("resourceId"));
        object.set("sid",parseObj.getStr("sid"));
        object.set("uid",uid);
        object.set("cname",cname);
        return object.toString();
    }

获取成功后,记得保持信息,在后续的stop 中会需要这几个信息。,resourceId ,sid ,uid,cname都需要

不保持,在后续的停止录制中,无法停止。调用视频截图后,当前接口就会一直监控着直播,然后每5秒截图上次到七牛云上面。

停止录制

在这里插入图片描述

看着官方文档的参数就知道了,在前面接口中的参数都需要保持起来,这里我就保持到redis中就行了。

    /**
     * 结束录制:
     * @return
     * @throws Exception
     */
    public String modeStop(String authorization) throws Exception {
        JSONObject parseObj = JSONUtil.parseObj(authorization);
        JSONObject jsonObject = JSONUtil.createObj();
        JSONObject clientRequest = JSONUtil.createObj();
        jsonObject.set("cname",parseObj.getStr("cname"));
        jsonObject.set("uid",parseObj.getStr("uid"));
        jsonObject.set("clientRequest",clientRequest);
        System.out.println(authorization);
        String url = MODESTOP.replaceAll("<yourappid>", agoraToConfig.getAppId()).replaceAll("<resourceid>", parseObj.getStr("resourceId"))
                .replaceAll("<sid>",parseObj.getStr("sid"));
        String body = HttpRequest.post(url)
                .header("Authorization", this.getKey())
                .header("Content-Type", "application/json;charset=utf-8")
                .body(jsonObject.toString()).execute().body();
        System.out.println(body);
        return body;
    }

目前为止,简单的视频直播监控就基本结束了,请大家注意,由于工作原因,我不可能把所有的代码都弄出来,这里的代码基本都实现了功能,还有一些业务方面的东西需要自己去弄,比如 key等东西。
实例代码在github上面需要的请自行拉取:spring-boot-integrate 然后后续会集成更多的模块进去,需要请点个star。后续会集成更多的接口实现,有需要的请保存。

如果这篇文章,有帮助到大家的,请给作者一个一键三连,谢谢



这篇关于JAVA 对接 声网(agora) 屏幕截图功能实现直播监控的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程