You need to enable JavaScript to run this app.
导航

服务端 SDK for Java 使用说明

最近更新时间2024.03.11 16:24:00

首次发布时间2022.11.15 09:54:09

使用说明

您可通过服务端 SDK for Java 来调用慢直播 OpenAPI,实现快速开发。我们提供了多语言的服务端 SDK,功能包括:

  • SDK 封装了签名,避免过于繁琐的自行计算。

  • SDK 封装了常用的 OpenAPI 调用,包含请求和响应,并提供了对应的示例,例如空间管理、设备管理、视频流管理等。

  • SDK 封装了常用的操作流程,比如服务端生成临时安全凭证。

开源

火山引擎 SDK for Java 项目
火山引擎 SDK for Java 版本发布
慢直播服务端 SDK for Java 示例

安装

使用 Maven 安装

<dependency>
    <groupId>com.volcengine</groupId>
    <artifactId>volc-sdk-java</artifactId>
    <version>LATEST</version><!--1.0.66+-->
</dependency>

使用 Jar 包安装

SDK 包下载地址

下载 jar 包后,导入对应的项目即可。

初始化客户端

您需要获取火山引擎账号的 Access Key 和 Secret Key(AK/AK)来初始化服务端 SDK。可通过火山引擎控制台右上角『个人 > API访问密钥』获取火山引擎账号的 AK/SK(建议使用子账号的 AK/SK),如下图所示:

alt

配置长期访问凭证

为保证账号和数据的安全,建议通过以下步骤将获取的火山引擎账号的 AK/SK 配置为长期访问凭证,然后在代码中使用环境变量获取 AK/SK 的方式进行调用。本章节介绍不同操作系统下配置长期访问凭证的操作步骤。

1. 打开终端并执行以下命令打开文件。
nano ~/.bash_profile
        
2. 在文件末尾添加火山引擎账号的 AK/SK,然后保存文件并退出。
export ACCESS_KEY=AKLTNTM0NDdlZTJkZmEwNDZjNmFjMzhlN2NlNmExxxxxxxx
export SECRET_KEY=TjJVNE1qQmpNemMyWmpNeE5ESXdPRGhqTW1GaU1qSTJNMkpqWldRxxxxxx==
        
3. 执行以下命令生效配置信息。
source ~/.bash_profile
        
4. 执行以下命令验证配置信息。
echo $ACCESS_KEY
echo $SECRET_KEY
        
如果配置成功,则返回如下结果:
AKLTNTM0NDdlZTJkZmEwNDZjNmFjMzhlN2NlNmExxxxxxxx
TjJVNE1qQmpNemMyWmpNeE5ESXdPRGhqTW1GaU1qSTJNMkpqWldRxxxxxx==
        

创建 SDK

参考以下示例,创建 SDK:

import com.volcengine.model.video_aiot.request.*;
import com.volcengine.model.video_aiot.response.*;
import com.volcengine.service.videoaiot.VideoAIoTService;

private VideoAIoTService videoAIoTService = VideoAIoTServiceImpl.getInstance();
{
    videoAIoTService.setAccessKey(System.getenv("ACCESS_KEY"));
    videoAIoTService.setSecretKey(System.getenv("SECRET_KEY"));
}
API 调用示例

空间管理

创建空间

说明:

方法名
是否必须
说明
setSpaceName1~100位,可包含大写字母、小写字母、数字、中划线

setRegion

服务地区,可选:

  • cn-qingdao-a

  • cn-beijing-a

  • cn-beijing-b

  • cn-shanghai-a

  • cn-guangzhou-a

setCallback消息回调地址,回调消息可参考:事件回调接口说明
setDescription空间描述

setAccessType

空间类型,可选:

  • rtmp(RTMP 接入类型)

  • gb28181(GB 接入类型)

setGbOptions设置 GB 选项,详细信息,参考 OpenAPI 接口说明

示例:

public void testCreateSpace() {
    CreateSpaceRequest createSpaceRequest = new CreateSpaceRequest();
    createSpaceRequest.setSpaceName("java-space");
    createSpaceRequest.setRegion("cn-beijing-a");
    createSpaceRequest.setCallback("");
    createSpaceRequest.setDescription("java-sdk-create");
    createSpaceRequest.setAccessType("gb28181");
    createSpaceRequest.setHLSLowLatency(false);
    CreateSpaceRequest.GBOptions gbOptions = new CreateSpaceRequest.GBOptions();
    gbOptions.setPullOnDemand(true);
    createSpaceRequest.setGbOptions(gbOptions);
    try {
        IDResponse space = videoAIoTService.createSpace(createSpaceRequest);
        System.out.printf(JSON.toJSONString(space));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

更新空间信息

说明:

方法名
是否必须
说明
setSpaceID更新的空间 ID
setSpaceName1~100位,可包含大写字母、小写字母、数字、中划线
setCallback消息回调地址,回调消息可参考:事件回调接口说明
setDescription空间描述
setGbOptions设置 GB 选项,详细信息,参考 OpenAPI 接口说明

示例:

public void testUpdateSpace() {
    UpdateSpaceRequest updateSpaceRequest = new UpdateSpaceRequest();
    updateSpaceRequest.setSpaceID(rtmpSpaceID);
    updateSpaceRequest.setSpaceName("java-sdk-update");
    updateSpaceRequest.setCallback("test-callback");
    updateSpaceRequest.setDescription("java-sdk-create");
    try {
        IDResponse idResponse = videoAIoTService.updateSpace(updateSpaceRequest);
        System.out.printf(JSON.toJSONString(idResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

启用/停用空间

示例:

public void testStartSpace() {
    try {
        IDResponse rawResponse = videoAIoTService.startSpace(rtmpSpaceID);
        System.out.printf(JSON.toJSONString(rawResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    
    try {
        IDResponse rawResponse = videoAIoTService.stopSpace(rtmpSpaceID);
        System.out.printf(JSON.toJSONString(rawResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

删除空间

只允许删除已停用的空间。

示例:

public void testDelSpace() {
    try {
        IDResponse rawResp = videoAIoTService.deleteSpace(rtmpSpaceID);
        System.out.printf(JSON.toJSONString(rawResp));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

查看空间信息

示例:

public void testGetSpace() {
    try {
        SpaceResponse space = videoAIoTService.getSpace(gbSpaceID);
        System.out.println(JSON.toJSONString(space));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

查看空间列表

示例:

public void testListSpace() {
    ListSpaceRequest listSpaceRequest = new ListSpaceRequest();
    listSpaceRequest.setPageSize(10);
    listSpaceRequest.setPageNumber(1);
    try {
        ListSpaceResponse listSpaceResponse = videoAIoTService.listSpace(listSpaceRequest);
        ListSpaceResponse.ListSpace spaces = listSpaceResponse.getSpaces();
        System.out.printf(JSON.toJSONString(spaces));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

配置空间模板

说明:

方法名
是否必须
说明
setSpaceID需要配置模板的空间 ID
setTemplateID配置的模板 ID

setTemplateType

配置的模板类型,可选:

  • screenshot(截图模板)

  • record(录制模板)

  • ai(AI 模板)

示例:

public void testSetSpaceTemplate() {
    SetSpaceTemplateRequest setSpaceTemplateRequest = new SetSpaceTemplateRequest();
    setSpaceTemplateRequest.setSpaceID("");
    setSpaceTemplateRequest.setTemplateID("");
    setSpaceTemplateRequest.setTemplateType("screenshot");
    try {
        videoAIoTService.setSpaceTemplate(setSpaceTemplateRequest);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

释放空间模板

示例:

public void testunSetSpaceTemplate() {
    try {
        videoAIoTService.unsetSpaceTemplate("spaceid", "template_type");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

设备管理

创建设备

说明:

方法名
是否必须
说明
setSpaceID设备所属空间 ID
setDeviceName4~40位,可包含大写字母、小写字母、数字、中划线
setDeviceNSID国标 ID,可通过 GenSipID 接口生成国标 ID
setDescription消息回调地址,回调消息可参考:事件回调接口说明

setPassword

空间类型,可选:

  • rtmp(RTMP 接入类型)

  • gb28181(GB 接入类型)

setType

设备类型,可选:

  • IPC

  • NVR

setUserName国标 ID,同 setDeviceNSID

示例:

public void testCreateDevice() {
    String deviceNSID = "";
    try {
        IDResponse ipc = videoAIoTService.genSipID("3402000000xxxxxxxxxx", "IPC");
        deviceNSID = ipc.getId().getId();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if ("".equalsIgnoreCase(deviceNSID)) {
        throw new RuntimeException("deviceNSID is empty");
    }
    CreateDeviceRequest createDeviceRequest = new CreateDeviceRequest();
    createDeviceRequest.setDeviceName("java-sdk-create0");
    createDeviceRequest.setDeviceNSID(deviceNSID);
    createDeviceRequest.setSpaceID(gbSpaceID);
    createDeviceRequest.setDescription("java-sdk-create");
    createDeviceRequest.setPassword("0p@la123xxxxxxxx");
    createDeviceRequest.setType("IPC");
    createDeviceRequest.setUserName(deviceNSID);
    try {
        CreateDeviceResponse device = videoAIoTService.createDevice(createDeviceRequest);
        System.out.printf(JSON.toJSONString(device));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

查看设备信息

说明:

方法名
是否必须
说明
setDeviceID设备唯一 ID
setSipServerID设备所属空间分配的 SIP ID,可通过 控制台 > 设备管理 页面获取
setSpaceID设备所属空间 ID

示例:

public void testGetDevice() {
    do_TestGetDevice("3402000000xxxxxxxxxx", "23b54cf3-077d-450c-ad02-xxxxxxxx",
            "3c207531-6c25-43bf-a192-xxxxxxxx");
}

public void do_TestGetDevice(String sipServerID, String deviceID, String gbSpaceID) {
    GetDeviceRequest getDeviceRequest = new GetDeviceRequest();
    getDeviceRequest.setDeviceID(deviceID);
    getDeviceRequest.setSipServerID(sipServerID);
    getDeviceRequest.setSpaceID(gbSpaceID);
    try {
        GetDeviceResponse device = videoAIoTService.getDevice(getDeviceRequest);
        System.out.printf(JSON.toJSONString(device));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

查看设备列表

示例:

public void testListDevice() {
    ListDeviceRequest listDeviceRequest = new ListDeviceRequest();
    listDeviceRequest.setPageSize(10);
    listDeviceRequest.setPageNumber(1);
    listDeviceRequest.setSpaceID(gbSpaceID);
    try {
        ListDeviceResponse listDeviceResponse = videoAIoTService.listDevice(listDeviceRequest);
        System.out.println(JSON.toJSONString(listDeviceResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

查看设备通道列表

说明:

方法名
是否必须
说明
setDeviceID设备唯一 ID

示例:

public void testGetDeviceChannels() {
    GetDeviceChannelRequest request = new GetDeviceChannelRequest();
    request.setDeviceID(deviceID);
    try {
        GetDeviceChannelResponse resp = videoAIoTService.getDeviceChannels(request);
        System.out.println(JSON.toJSONString(resp));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

编辑设备信息

说明:

方法名
是否必须
说明
setSpaceID设备所属空间 ID
setDeviceID设备唯一 ID
setDeviceName设备名称
setDescription设备描述
setAutoPullAfterRegister是否按需拉流
setCoordinates设备经纬度
setAlertNotification设备告警

示例:

public void testUpdateDevice() {
    do_testUpdateDevice("3c207531-6c25-43bf-a192-xxxxxxxx", "43932980-f513-4aae-bc4c-xxxxxxxx");
}

public void do_testUpdateDevice(String spaceID, String deviceID) {
    UpdateDeviceRequest updateDeviceRequest = new UpdateDeviceRequest();
    updateDeviceRequest.setDeviceID(deviceID);
    updateDeviceRequest.setDeviceName("java-sdk-update" + System.currentTimeMillis());
    updateDeviceRequest.setDescription("desc" + System.currentTimeMillis());
    updateDeviceRequest.setSpaceID(spaceID);
    updateDeviceRequest.setAutoPullAfterRegister(false);
    Device.Coordinates co = new Device.Coordinates();
    co.setLatitude(10.1);
    co.setLongitude(10.2);
    updateDeviceRequest.setLocation("test-location");
    updateDeviceRequest.setCoordinates(co);
    try {
        IDResponse idResponse = videoAIoTService.updateDevice(updateDeviceRequest);
        System.out.printf(JSON.toJSONString(idResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

启用/停用设备

示例:

public void testStartDevice() {
    do_TestStartDevice("a0c97103-f019-42b4-b60a-xxxxxxxx", "925ca8e1-11cc-4473-aace-xxxxxxxx");
}

public void do_TestStartDevice(String spaceID, String deviceID) {
    DeviceRequest deviceRequest = new DeviceRequest();
    deviceRequest.setSpaceID(spaceID);
    deviceRequest.setDeviceID(deviceID);
    try {
        IDResponse idResponse = videoAIoTService.startDevice(deviceRequest);
        System.out.println(JSON.toJSONString(idResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

刷新设备

说明:

方法名
是否必须
说明
setSpaceID设备所属空间 ID
setDeviceID设备唯一 ID

示例:

public void testFreshDevice() {
    do_TestFreshDevice("925ca8e1-11cc-4473-aace-xxxxxxxx", "a0c97103-f019-42b4-b60a-xxxxxxxx");
}

public void do_TestFreshDevice(String deviceID, String spaceID) {
    DeviceRequest deviceRequest = new DeviceRequest();
    deviceRequest.setDeviceID(deviceID);
    deviceRequest.setSpaceID(spaceID);
    try {
        IDResponse rawResponse = videoAIoTService.freshDevice(deviceRequest);
        System.out.println(JSON.toJSONString(rawResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

删除设备

只允许删除离线的设备。

说明:

方法名
是否必须
说明
setSpaceID设备所属空间 ID
setDeviceID设备唯一 ID

示例:

public void testDelDevice() {
    do_TestDeleteDevice("2db38a3a-2a9d-4bf7-afd9-xxxxxxxx", "fb58fc32-1dd7-4ed4-a26c-xxxxxxxx");
}

public void do_TestDeleteDevice(String spaceID, String deviceID) {
    DeviceRequest request = new DeviceRequest();
    request.setSpaceID(spaceID);
    request.setDeviceID(deviceID);
    try {
        IDResponse resp = videoAIoTService.deleteDevice(request);
        System.out.println(JSON.toJSONString(resp));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

视频流管理

创建视频流(仅RTMP空间)

说明:

方法名
是否必须
说明
setSpaceID设备所属空间 ID
setStreamName视频流名称,包含字母、数字、中划线;1 ~ 100 个字符长

示例:

public void testCreateStream() {
    CreateStreamRequest streamRequest = new CreateStreamRequest();
    streamRequest.setSpaceID(rtmpSpaceID);
    streamRequest.setStreamName("stream01");
    try {
        IDResponse idResponse = videoAIoTService.createStream(streamRequest);
        System.out.println(JSON.toJSONString(idResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

禁用视频流

示例:

public void testForbidStream() {
    String streamID = "c518f8f3-84ed-4515-8be4-xxxxxxxx";
    try {
        IDResponse idResponse = videoAIoTService.forbidStream(streamID);
        System.out.println(JSON.toJSONString(idResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

启用视频流

示例:

public void testUnForbidStream() {
    String streamID = "c518f8f3-84ed-4515-8be4-xxxxxxxx";
    try {
        IDResponse idResponse = videoAIoTService.unForbidStream(streamID);
        System.out.println(JSON.toJSONString(idResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

删除视频流(仅RTMP空间)

说明:

方法名
是否必须
说明
setStreamID需要删除的视频流 ID

示例:

public void testDeleteStream() {
    StreamRequest streamRequest = new StreamRequest();
    streamRequest.setStreamID("d4e6611c-ed2b-492a-xxxxxxxx");
    try {
        IDResponse idResponse = videoAIoTService.deleteStream(streamRequest);
        System.out.println(JSON.toJSONString(idResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

查看视频流

示例:

public void testGetStream() {
    GetStreamRequest getStreamRequest = new GetStreamRequest();
    getStreamRequest.setStreamID("3d87ab7c-88b5-4e5c-9aa0-xxxxxxxx");
    try {
        GetStreamResponse stream = videoAIoTService.getStream(getStreamRequest);
        System.out.printf(JSON.toJSONString(stream));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

查看视频流列表

说明:

方法名
是否必须
说明
setSpaceID查询视频流空间
setPageSize分页参数
setPageNumber分页参数

示例:

public void testListStream() {
    ListStreamsRequest listStreamsRequest = new ListStreamsRequest();
    listStreamsRequest.setPageSize(10);
    listStreamsRequest.setPageNumber(1);
    listStreamsRequest.setSpaceID(rtmpSpaceID);
    try {
        ListStreamsResponse listStreamsResponse = videoAIoTService.listStreams(listStreamsRequest);
        System.out.println(JSON.toJSONString(listStreamsResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

启用视频流(仅GB空间)

说明:

方法名
是否必须
说明
setStreamID需要启动的视频流 ID

示例:

public void testStartStream() {
    StreamRequest streamRequest = new StreamRequest();
    streamRequest.setStreamID("d4e6611c-ed2b-492a-b86b-xxxxxxxx");
    try {
        IDResponse idResponse = videoAIoTService.startStream(streamRequest);
        System.out.println(JSON.toJSONString(idResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

停用户视频流(仅GB空间)

说明:

方法名
是否必须
说明
setStreamID需要停止的视频流 ID

示例:

public void testDStream() {
    StreamRequest streamRequest = new StreamRequest();
    streamRequest.setStreamID("d4e6611c-ed2b-492a-b86b-xxxxxxxx");
    try {
        IDResponse idResponse = videoAIoTService.stopStream(streamRequest);
        System.out.println(JSON.toJSONString(idResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

编辑视频流

说明:

方法名
是否必须
说明
setStreamID需要编辑的视频流 ID
setStreamName视频流名称
setDescription视频流描述
setSpaceID视频流所属空间 ID

示例:

public void testUpdateStream() {
    UpdateStreamRequest updateStreamRequest = new UpdateStreamRequest();
    updateStreamRequest.setStreamID(streamID);
    updateStreamRequest.setStreamName("java-sdk-update11111");
    updateStreamRequest.setDescription("123123123");
    updateStreamRequest.setSpaceID(rtmpSpaceID);
    try {
        IDResponse idResponse = videoAIoTService.updateStream(updateStreamRequest);
        System.out.printf(JSON.toJSONString(idResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

获取视频流数据(视频帧率、码率)

说明:

方法名
是否必须
说明
setStreamID查询的流 ID
setStartTime查询开始时间,UTC 时间,单位:秒
setEndTime查询结束时间,UTC 时间,单位:秒

示例:

public void testGetStreamData() {
    // 流维度的数据查询
    GetStreamDataRequest streamRequest = new GetStreamDataRequest();
    streamRequest.setStreamID("dbc2000c-56e0-4ccc-ba9e-xxxxxxxx");
    streamRequest.setStartTime("1662555386");
    streamRequest.setEndTime("1662641786");
    try {
        GetStreamDataResponse dataResponse = videoAIoTService.getStreamData(streamRequest);
        System.out.println(JSON.toJSONString(dataResponse));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

截图管理

视频截图

说明:

方法名
是否必须
说明
setSpaceID查询的空间 ID
setStreamID查询的视频流 ID
setStartTs查询开始时间,RFC3339 格式时间戳,例如:2022-08-25T16:22:17+08:00
setEndTs查询结束时间,RFC3339 格式时间戳,例如:2022-08-26T18:22:16+08:00
setPageSize分页参数,单次最大返回 500,默认 20
setPageNumber分页参数,默认1

示例:

public void testListDeviceScreenshot() {
    ListDeviceRecordsRequest listDeviceRecords = new ListDeviceRecordsRequest();
    listDeviceRecords.setSpaceID("517ff7ec-7700-4862-b1e7-xxxxxxxx");
    listDeviceRecords.setStreamID("2a772f0b-753c-4496-b535-xxxxxxxx");
    listDeviceRecords.setStartTs("2022-08-25T16:22:17+08:00");
    listDeviceRecords.setEndTs("2022-08-26T16:22:17+08:00");
    listDeviceRecords.setPageSize(2);
    listDeviceRecords.setPageNumber(100);
    try {
        ListDeviceRecordsResponse resp = videoAIoTService.listDeviceScreenshots(listDeviceRecords);
        System.out.println(JSON.toJSONString(resp));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

录制管理

查询本地录像回放(已废弃)

说明:

方法名
是否必须
说明
setDeviceNSID设备国标 ID
setChannelID设备通道 ID
setStartTime查询录像开始时间,秒级时间戳
setEndTime查询录像结束时间,秒级时间戳
setTimeoutSec查询的超时时间,单位:秒
RecordType查询的录像类型:可选:all/alarm/time/manual
setOrder返回的录像是否排序,默认:false

示例:

try {
    GetRecordListRequest request = new GetRecordListRequest();
    request.setRecordType("all");
    request.setDeviceNSID("340200819911xxxxxxxx");
    request.setStartTime(1680451200);
    request.setEndTime(1680537599);
    request.setOrder(true);
    request.setTimeoutSec(20);
    request.setChannelID("988800000013xxxxxxxx");
    GetRecordResponse resp = videoAIoTService.getRecordList(request);
    System.out.println(JSON.toJSONString(resp));
} catch (Exception e) {
    e.printStackTrace();
}

查询本地录像回放V2

说明:

方法名
是否必须
说明
setDeviceNSID设备国标 ID
setChannelID设备通道 ID
setStartTime查询录像起始时间,秒级时间戳,例如:1686153600
setEndTime查询录像结束时间,秒级时间戳,例如:1686182400
setTimeoutSec查询的超时时间,单位:秒

setRecordType

查询的录像类型:

  • time(定时录像)

  • alarm(报警录像)

  • manual(手动录像)

  • all(所有,默认)

setOrder

返回的录像是否排序:

  • true(正序)

  • false(倒序)

示例:

public void testGetDeviceRecordListV2() {
    setTest();
    try {
        GetRecordListV2Request request = new GetRecordListV2Request();
        request.setRecordType("all");
        request.setDeviceNSID("34020027991180xxxxxx");
        request.setStartTime(1688572800);
        request.setEndTime(1688659199);
        request.setOrder(true);
        request.setTimeoutInSec(20);
        request.setChannelID("98880000001xxxxxxxxx");
        GetRecordV2Response resp = videoAIoTService.getRecordListV2(request);
        System.out.println(JSON.toJSONString(resp));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

开始本地录像回放

说明:

方法名
是否必须
说明
setDeviceNSID设备国标 ID
setChannelID设备通道 ID
setStartTime录像开始时间,秒级时间戳
setEndTime录像结束时间,秒级时间戳

示例:

try {
    PlaybackStartRequest request = new PlaybackStartRequest();
    request.setStartTime(1680451200);
    request.setEndTime(1680537599);
    request.setChannelID("988800000013xxxxxxxx");
    request.setDeviceNSID("340200819911xxxxxxxx");
    PlaybackStartResponse resp = videoAIoTService.playbackStart(request);
    System.out.println(JSON.toJSONString(resp));
} catch (Exception e) {
    e.printStackTrace();
}

开始本地录像回放V2

说明:

方法名
是否必须
说明
setStreamID视频流 ID
setDeviceNSID设备国标 ID
setChannelID设备通道 ID
setStartTime录像开始时间,Unix 时间戳,精确到秒
setEndTime录像结束时间,Unix 时间戳,精确到秒

示例:

try {
    PlaybackStartRequestV2 request = new PlaybackStartRequestV2();
    request.setStartTime(1680451200);
    request.setEndTime(1680537599);
    request.setStreamID("d4e6611c-ed2b-492a-xxxxxxxx");
    request.setDeviceNSID("34020081991xxxxxxxxx");
    request.setChannelID("98880000001xxxxxxxxx");
    PlaybackStartResponse resp = videoAIoTService.playbackStartV2(request);
    System.out.println(JSON.toJSONString(resp));
} catch (Exception e) {
    e.printStackTrace();
}

结束本地录像回放

示例:

try {
    String id = "ae395018-9d80-4a8f-9805-1a68xxxxxxxx" ; //id 表示 PlaybackStart 接口返回的视频流 ID
    IDResponse resp = videoAIoTService.playbackStop(id); 
    System.out.println(JSON.toJSONString(resp));
} catch (Exception e) {
    e.printStackTrace();
}

录像回放控制

说明:

方法名
是否必须
说明
setStreamIDPlaybackStart 接口返回的视频流 ID

setCmd

操作类型:

  • 0(开始)

  • 1(暂停)

  • 2(倍速)

  • 3(跳转)

setNtp指定起始播放时间,单位为:秒(如果当前片段长度为5分钟,指定60秒即从片段的第1分钟开始播放)

setScale

倍速,可选项(需要按设备或网络支持的条件进行设置):

  • 1.0

  • 2.0

  • 4.0

  • 8.0

示例:

try {
    PlaybackControlRequest request = new PlaybackControlRequest();
    request.setStreamID("5d844cb5-10e2-4113-b642-fea8xxxxxxxx");
    request.setCmd(0);
    request.setNtp(String.valueOf(1));//
    request.setScale(1);
    IDResponse resp = videoAIoTService.playbackControl(request);
    System.out.println(JSON.toJSONString(resp));
} catch (Exception e) {
    e.printStackTrace();
}

查询回放流状态

说明:

方法名
是否必须
说明
setStreamID视频流 ID,可通过 PlayBackStart 接口获取

示例:

public void testPlaybackStat() throws Exception {
    setTest();
    String sid = "3ad6c029-5153-40ca-904f-7810xxxxxxxx";
    for (int i = 0; ; i++) {
        PlaybackStatResponse playbackStatResponse = videoAIoTService.playbackStat(sid);
        System.out.println(JSON.toJSONString(playbackStatResponse));
        if (playbackStatResponse.getResult().getStatus().equals("deleted")) {
            break;
        }
        Thread.sleep(1000);
    }
}

云端录像回放

说明:

方法名
是否必须
说明
setSpaceID查询的空间 ID
setStreamID查询的视频流 ID
setStartTs查询开始时间,RFC3339 格式时间戳,例如:2022-08-25T16:22:17+08:00
setEndTs查询结束时间,RFC3339 格式时间戳,例如:2022-08-26T18:22:16+08:00
setPageSize分页参数,单次最大返回 500,默认 20
setPageNumber分页参数,默认1

示例:

public void testListDeviceRecords() {
    ListDeviceRecordsRequest listDeviceRecords = new ListDeviceRecordsRequest();
    listDeviceRecords.setSpaceID("1d02923f-d5ce-40f2-ac90-xxxxxxxx");
    listDeviceRecords.setStreamID("fcc2adaf-93b8-44e1-800a-xxxxxxxx");
    listDeviceRecords.setStartTs("2022-08-25T16:29:39+08:00");
    listDeviceRecords.setEndTs("2022-08-26T16:29:39+08:00");
    listDeviceRecords.setPageSize(2);
    listDeviceRecords.setPageNumber(1);
    try {
        ListDeviceRecordsResponse resp = videoAIoTService.listDeviceRecords(listDeviceRecords);
        System.out.println(JSON.toJSONString(resp));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

云端录制查询(任意时间段)

说明:

方法名
是否必须
说明
setStreamID查询的视频流 ID
setStartTs查询开始时间,UTC 时间,单位:秒
setEndTs查询结束时间,UTC 时间,单位:秒
setTokenValid返回的 URL Token 过期时间,单位:秒

示例:

public void testCloudRecordPlay() {
    CloudRecordPlayRequest cloudRecordPlayRequest = new CloudRecordPlayRequest();
    cloudRecordPlayRequest.setStreamID("4dba59d1-87f8-47f0-805a-xxxxxxxx");
    cloudRecordPlayRequest.setStartTs(1663916290);
    cloudRecordPlayRequest.setEndTs(1663916410);
    cloudRecordPlayRequest.setTokenValid(3600);
    try {
        CloudPlayResponse resp = videoAIoTService.cloudRecordPlay(cloudRecordPlayRequest);
        System.out.println(JSON.toJSONString(resp));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

下载本地录像文件

说明:

方法名
是否必须
说明
setSpaceID设备所属空间 ID
setDeviceID设备国标 ID
setChannelID设备通道 ID
setStartTime录像开始时间
setEndTime录像结束时间
setVersion版本号,设置为2
setMps设置对下载的录像文件进行媒体处理的选项

示例:

public void testLocalMediaDownload() {
    LocalMediaDownloadRequest localMediaDownloadRequest = new LocalMediaDownloadRequest();
    localMediaDownloadRequest.setSpaceID("7c09b921-xxxx-xxxx-xxxx-e0fce9a072a0\n");
    localMediaDownloadRequest.setChannelID("34020035991320xxxxxx");
    localMediaDownloadRequest.setDeviceID("34020095991320xxxxxx");
    localMediaDownloadRequest.setStartTime(1660719434);
    localMediaDownloadRequest.setEndTime(1660719534);
    localMediaDownloadRequest.setVersion("2");
    LocalMediaDownloadRequest.Mps mps = new LocalMediaDownloadRequest.Mps();
    LocalMediaDownloadRequest.M3U8Option m3u8 = new LocalMediaDownloadRequest.M3U8Option();
    m3u8.setToMp4(true);
    mps.setM3U8Option(m3u8);

    LocalMediaDownloadRequest.Subtitle subt = new LocalMediaDownloadRequest.Subtitle();
    subt.setSubtitleSrc("");

    LocalMediaDownloadRequest.Font font = new LocalMediaDownloadRequest.Font();
    font.setFontSize(2);
    font.setAlignment("");
    font.setPrimaryColor("");

    subt.setFont(font);

    LocalMediaDownloadRequest.SubtitleItem item = new LocalMediaDownloadRequest.SubtitleItem();
    item.setContent("水印文字");
    item.setEnd(9000);
    item.setStart(5000);

    LocalMediaDownloadRequest.SubtitleItem item1 = new LocalMediaDownloadRequest.SubtitleItem();
    item.setContent("水印文字");
    item.setEnd(9000);
    item.setStart(5000);

    List<LocalMediaDownloadRequest.SubtitleItem> list1 = new ArrayList<LocalMediaDownloadRequest.SubtitleItem>();
    list1.add(item);
    //list1.add(item1);

    subt.setSubtitleList(list1);

    mps.setSubtitle(subt);

    localMediaDownloadRequest.setMediaProcess(mps);
    try {
        LocalMediaDownloadResponse download = videoAIoTService.localMediaDownload(localMediaDownloadRequest);
        System.out.printf(JSON.toJSONString(download));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

流录像

流录像不同于录制模板,更适合短时间录制,启停更方便。

启动录像

说明:

方法名
是否必须
说明
setStreamID录制的视频流 ID
setAutoPull是否自动拉流录制,若指定 false,需要先通过 启动视频流【仅GB空间】启用视频流后再开始录制
setTimeout拉流超时时间,单位:秒,如果超过指定的超时时间视频流仍然不在线,则停止录制任务,返回 timeout 错误
setRemux设置转封装,可选字段:mp4,转封装成功后可以在获取录像中查询到转封装结果
setRecordTime预估录制时间,单位:秒,超出该时间会自动停止录制任务,默认不生效,由停止录像/Timeout 控制
setExpire录像过期时间,单位:秒,过期的录像会被主动删除;默认过期时间为7天,-1表示永不过期

示例:

public void testStreamStartRecord() {
    StreamStartRecordRequest streamStartRecordRequest = new StreamStartRecordRequest();
    streamStartRecordRequest.setStreamID("ee9a49ea-916c-4c2f-aced-333e409414df");
    streamStartRecordRequest.setRecordTime(100);
    streamStartRecordRequest.setTimeout(10);
    streamStartRecordRequest.setExpire(-1);
    streamStartRecordRequest.setAutoPull(true);
    try {
        IDResponse idResponse = videoAIoTService.streamStartRecord(streamStartRecordRequest);
        System.out.println(JSON.toJSONString(idResponse));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

返回的录制任务 ID 表示本次录像的唯一 ID,凭此 ID 可以停止录像、查询录像结果、删除录像。

停止录像

示例:

public void testStreamStopRecord() {
        try {
            RawResponse idResponse = videoAIoTService.streamStopRecord("record0x7df8ud0");
            System.out.println(JSON.toJSONString(idResponse));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

获取录像

示例:

public void testGetStreamRecord() {
        try {
            GetStreamRecordResponse idResponse = videoAIoTService.getStreamRecord("record0kvldcqr1");
            System.out.println(JSON.toJSONString(idResponse));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

删除录像

示例:

public void testDeleteStreamRecord() {
    try {
        DeleteStreamRecordResponse idResponse = videoAIoTService.deleteStreamRecord("record0k7248vc4");
        System.out.println(JSON.toJSONString(idResponse));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

设备控制

云台控制

说明:

方法名
是否必须
说明
setDeviceID设备国标 ID
setChannelID设备国标通道 ID
setAction控制操作
setCmd控制指令
setParam操作参数

setAction

可选 Action:

public static final String ACTION_PTZ = "PTZControl";
public static final String ACTION_FI = "FiControl";
public static final String ACTION_PRESET = "PresetControl";

setCmd

可选 Cmd:

//ptz
public static final String PtzCmdStop = "stop";
public static final String PtzCmdRight = "right";
public static final String PtzCmdLeft = "left";
public static final String PtzCmdDown = "down";
public static final String PtzCmdUp = "up";
public static final String PtzCmdRightUp = "rightup";
public static final String PtzCmdRightDown = "rightdown";
public static final String PtzCmdLeftUp = "leftup";
public static final String PtzCmdLeftDown = "leftdown";
public static final String PtzCmdZoomIn = "zoomin";
public static final String PtzCmdZoomOut = "zoomout";
//fi
public static final String FiCmdStop = "stop";
public static final String FiCmdFocusFar = "focusfar";
public static final String FiCmdFocusNear = "focusnear";
public static final String FiCmdIrisIn = "irisin";
public static final String FiCmdIrisOut = "irisout";
//preset
public static final String PresetCmdSet = "set";
public static final String PresetCmdGoto = "goto";
public static final String PresetCmdRemove = "remove";

setParam

Param 参数说明:

控制选项(Action)参数意义可选参数值

PTZ

对于 PTZ 方向参数,如 left、right、up等,表示转动速度

[0, 255]

对于 PTZ zoom 参数,表示放大缩小速度[0, 15]
FI焦距修改速度,值越大,焦距变化越快[0, 0xFF]
Preset预置位 ID,设置时需要确保该预置位 ID 没有被占用[0x01, 0xFF]

示例:

//ptz control
public void testPTZ() throws InterruptedException {
    DeviceCloudControlRequest deviceCloudControlRequest = new DeviceCloudControlRequest();
    deviceCloudControlRequest.setDeviceNSID("34020008991180xxxxxx");
    deviceCloudControlRequest.setAction(DeviceCloudControlRequest.ACTION_PTZ);
    deviceCloudControlRequest.setChannelID("34020000001320xxxxxx");
    deviceCloudControlRequest.setParam(51);
    deviceCloudControlRequest.setCmd(DeviceCloudControlRequest.PtzCmdUp);
    deviceCloudControlRequest.setSipID("34020000002000xxxxxx");
    try {
        RawResponse rawResponse = videoAIoTService.cloudControl(deviceCloudControlRequest);
        System.out.println(JSON.toJSONString(rawResponse));
    } catch (Exception e) {
        e.printStackTrace();
    }
    Thread.sleep(2000);
    deviceCloudControlRequest = new DeviceCloudControlRequest();
    deviceCloudControlRequest.setDeviceNSID("34020008991180xxxxxx");
    deviceCloudControlRequest.setAction(DeviceCloudControlRequest.ACTION_PTZ);
    deviceCloudControlRequest.setChannelID("34020000001320xxxxxx");
    deviceCloudControlRequest.setParam(51);
    deviceCloudControlRequest.setCmd(DeviceCloudControlRequest.PtzCmdStop);
    deviceCloudControlRequest.setSipID("34020000002000xxxxxx");
    try {
        RawResponse rawResponse = videoAIoTService.cloudControl(deviceCloudControlRequest);
        System.out.println(JSON.toJSONString(rawResponse));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//fi control
public void testFi() throws InterruptedException {
    DeviceCloudControlRequest deviceCloudControlRequest = new DeviceCloudControlRequest();
    deviceCloudControlRequest.setDeviceNSID("34020008991180xxxxxx");
    deviceCloudControlRequest.setAction(DeviceCloudControlRequest.ACTION_FI);
    deviceCloudControlRequest.setChannelID("34020000001320xxxxxx");
    deviceCloudControlRequest.setParam(51);
    deviceCloudControlRequest.setCmd(DeviceCloudControlRequest.FiCmdFocusFar);
    deviceCloudControlRequest.setSipID("34020000002000xxxxxx");
    try {
        videoAIoTService.cloudControl(deviceCloudControlRequest);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Thread.sleep(2000);
    deviceCloudControlRequest = new DeviceCloudControlRequest();
    deviceCloudControlRequest.setDeviceNSID("34020008991180xxxxxx");
    deviceCloudControlRequest.setAction(DeviceCloudControlRequest.ACTION_FI);
    deviceCloudControlRequest.setChannelID("34020000001320xxxxxx");
    deviceCloudControlRequest.setParam(51);
    deviceCloudControlRequest.setCmd(DeviceCloudControlRequest.FiCmdStop);
    deviceCloudControlRequest.setSipID("34020000002000xxxxxx");
    try {
        RawResponse rawResponse = videoAIoTService.cloudControl(deviceCloudControlRequest);
        System.out.println(JSON.toJSONString(rawResponse));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//preset control
int presetID = 15;
//添加预置位
public void testAddPreset() {
    DeviceCloudControlRequest deviceCloudControlRequest = new DeviceCloudControlRequest();
    deviceCloudControlRequest.setCmd(DeviceCloudControlRequest.PresetCmdSet);
    deviceCloudControlRequest.setDeviceNSID("340200089911809xxxxxx");
    deviceCloudControlRequest.setAction(DeviceCloudControlRequest.ACTION_PRESET);
    deviceCloudControlRequest.setChannelID("34020000001320xxxxxx");
    deviceCloudControlRequest.setParam(presetID);
    deviceCloudControlRequest.setSipID("34020000002000xxxxxx");

    try {
        videoAIoTService.cloudControl(deviceCloudControlRequest);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//删除预置位
public void testRMPreset() {
    DeviceCloudControlRequest deviceCloudControlRequest = new DeviceCloudControlRequest();
    deviceCloudControlRequest.setCmd(DeviceCloudControlRequest.PresetCmdRemove);
    deviceCloudControlRequest.setDeviceNSID("34020008991180xxxxxx");
    deviceCloudControlRequest.setAction(DeviceCloudControlRequest.ACTION_PRESET);
    deviceCloudControlRequest.setChannelID("34020000001320xxxxxx");
    deviceCloudControlRequest.setParam(presetID);
    deviceCloudControlRequest.setSipID("34020000002000xxxxxx");

    try {
        videoAIoTService.cloudControl(deviceCloudControlRequest);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

查询预置位

说明:

方法名
是否必须
说明
setDeviceID设备国标 ID
setChannelID设备国标通道 ID
setTimeout请求超时时间,单位秒,默认5秒

示例:

public void testQueryPreset() {
    DeviceQueryPresetRequest request = new DeviceQueryPresetRequest();
    request.setChannelID("34020000001320xxxxxx");
    request.setSipID("34020000002000xxxxxx");
    request.setTimeout(5);
    request.setDeviceID("34020008991180xxxxxx");
    try {
        DeviceQueryPresetResponse deviceQueryPresetResponse = videoAIoTService.queryPresetInfo(request);
        System.out.println(JSON.toJSONString(deviceQueryPresetResponse));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

通道巡航控制

说明:

方法名
是否必须
说明
setDeviceNSID设备国标 ID
setChannelID设备国标通道 ID
setAction巡航指令
setTrackID巡航组编号,取值范围:[1, 255]
setPresetID预置位编号,取值范围:[0, 255]
setSpeed巡航速度,取值范围:[1, 4095]
setStaySeconds预置位停留时间,单位:秒,取值范围:[1, 4095]

示例:

public void testCruiseControl() {
    setTest();
    DeviceCruiseControlRequest request = new DeviceCruiseControlRequest();
    request.setDeviceNSID("34020029991180xxxxxx");
    request.setChannelID("988800000013xxxxxxxx");

    // add
    request.setAction(DeviceCruiseControlRequest.Action_Add);
    request.setTrackID(1);
    request.setPresetID(2);
    try {
        RawResponse cruiseControlResponse = videoAIoTService.cruiseControl(request);
        System.out.println(JSON.toJSONString(cruiseControlResponse));
    } catch (Exception e) {
        e.printStackTrace();
    }

    // set stay time and speed
    request.setAction(DeviceCruiseControlRequest.Action_SetSpeed);
    request.setSpeed(500);
    request.setStaySeconds(5);
    try {
        RawResponse cruiseControlResponse = videoAIoTService.cruiseControl(request);
        System.out.println(JSON.toJSONString(cruiseControlResponse));
    } catch (Exception e) {
        e.printStackTrace();
    }

    // start cruise track
    request.setAction(DeviceCruiseControlRequest.Action_Start);
    request.setTrackID(1);
    try {
        RawResponse cruiseControlResponse = videoAIoTService.cruiseControl(request);
        System.out.println(JSON.toJSONString(cruiseControlResponse));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

录像计划

创建录像计划

说明:

方法名
是否必须
说明
setPlanName录像计划名称
setBindTemplate设置绑定的模板 ID
setBindStreams设置绑定的视频流 ID 列表
setDescription录像计划描述

setStatus

是否启用录像计划:

  • enabled(启用)

  • disabled(禁用)

setBindChannels设置绑定的通道(和 setBindStreams 行为是一致的,二选一即可)

示例:

public void testCreateRecordPlan() {
        setTest();
        CreateRecordPlanRequest createRecordPlanRequest = new CreateRecordPlanRequest();
        createRecordPlanRequest.setPlanName("test-plan");
        createRecordPlanRequest.setBindTemplate("0609aef4-eb80-41c7-9858-48347dxxxxxx");
        createRecordPlanRequest.setBindStreams(new ArrayList<String>() {
            {
                add("7f2f1b54-2714-4f66-ae78-cd477axxxxxx");
            }
        });
        createRecordPlanRequest.setDescription("javasdktest");
        createRecordPlanRequest.setStatus("enabled");
        try {
            IDResponse recordPlan = videoAIoTService.createRecordPlan(createRecordPlanRequest);
            System.out.println(recordPlan);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

编辑录像计划

说明:

方法名
是否必须
说明
setPlanID编辑的录像计划 ID
setPlanName录像计划名称
setBindTemplate设置绑定的录像模板 ID
setDelList设置删除的视频流
setAddList设置新增的视频流

setStatus

是否启用录像计划:

  • enabled(启用)

  • disabled(禁用)

示例:

public void testUpdateRecordPlan() {
        setTest();
        UpdateRecordPlanRequest updateRecordPlanRequest = new UpdateRecordPlanRequest();
        updateRecordPlanRequest.setPlanName("sdk-update");
        updateRecordPlanRequest.setPlanID("2f11a13a-038f-445a-800e-03572exxxxxx");
        updateRecordPlanRequest.setBindTemplate("a87c5902-453a-4cb1-a4e2-87171bxxxxxx");
        updateRecordPlanRequest.setStatus("enabled");
        UpdateRecordPlanRequest.ModifyList delList = new UpdateRecordPlanRequest.ModifyList();
        delList.setStreams(new ArrayList<String>(){
            {add("7f2f1b54-2714-4f66-ae78-cd477axxxxxx");}
        });
        updateRecordPlanRequest.setDelList(delList);
        UpdateRecordPlanRequest.ModifyList addList = new UpdateRecordPlanRequest.ModifyList();
        addList.setStreams(new ArrayList<String>(){
            {add("3e088b33-4e28-4af3-a6f1-daff5axxxxxx");}
        });
        updateRecordPlanRequest.setAddList(addList);
        try {
            IDResponse recordPlan = videoAIoTService.updateRecordPlan(updateRecordPlanRequest);
            System.out.println(JSON.toJSONString(recordPlan));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

查询录像计划

示例:

public void testGetRecordPlan() {
    String planID = "";
    try {
        RecordPlanResponse recordPlan = videoAIoTService.getRecordPlan(planID);
        System.out.println(JSON.toJSONString(recordPlan));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

删除录像计划

示例:

public void testGetRecordPlan() {
    String planID = "";
    try {
        IDResponse id = videoAIoTService.deleteRecordPlan(planID);
        System.out.println(JSON.toJSONString(id));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

录像计划列表

说明:

方法名
是否必须
说明
setFilterName模糊搜索录像计划名称

示例:

public void testListRecordPlan() {
        setTest();
        ListRecordPlansRequest listRecordPlansRequest = new ListRecordPlansRequest();
        listRecordPlansRequest.setFilterName("test");
        try {
            ListRecordPlansResponse recordPlan = videoAIoTService.listRecordPlans(listRecordPlansRequest);
            System.out.println(JSON.toJSONString(recordPlan));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

录像计划通道

示例:

public void testListRecordPlanChannel() {
    setTest();
    String planID = "f4aaa8ec-cab4-4120-95cd-5be2cfxxxxxx";
    try {
        ListRecordPlanChannelsResponse recordPlan = videoAIoTService.listRecordPlanChannels(planID);
        System.out.println(JSON.toJSONString(recordPlan));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
播放器 SDK

参考 VePlayer Web 播放器接入说明