ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-06-01 12:04:45  阅读:559  来源: 互联网

标签:set JAVA String jsonObject JSONUtil 声网 agora cname uid


描述:

在做直播,社交类项目的时候,通常会用到一些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。后续会集成更多的接口实现,有需要的请保存。

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

标签:set,JAVA,String,jsonObject,JSONUtil,声网,agora,cname,uid
来源: https://blog.csdn.net/qq_41971087/article/details/117440092

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有