本文提供了Java SDK,HLS数据加密完整的代码示例。
简介
示例调用API进行创建HLS标准加密工作流。创建工作流,请参见新增媒体工作流。
示例代码依赖
MPS SDK,请参见安装。
其他依赖。
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.68.noneautotype</version> </dependency>
示例代码
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.mps.sdk.utils.InitClient;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.mts.model.v20140618.AddMediaWorkflowRequest;
import com.aliyuncs.mts.model.v20140618.AddMediaWorkflowResponse;
/**
* ***** 使用须知 ******
* 本demo为创建标准加密工作流示例, 如果您是初次使用标准加密, 首次需使用本demo,并执行一次该工作流来打通系统配置
* 工作流数据结构较为复杂, 修改时请注意参数的准确性, 工作流数据结构参考 https://help.aliyun.com/document_detail/602860.html
* KeyUri为您的解密服务地址, 请确保可用性, 否则会导致解密播放失败
* 如何使用标准加密 请参考: xxxxxxxxxxxx
*/
public class MediaHlsWorkflow {
/**管道ID, 可以在控制台 【全局设置】->【管道】查看*/
final static String PIPELINE_ID = "be3a2b1ce40a0cbf****";
/**模板ID, 预置模板参考 https://help.aliyun.com/document_detail/29256.html*/
final static String TEMPLATE_ID = "S00000001-100020";
final static String OSS_LOCATION = "oss-cn-beijing";
/**控制台绑定的的输入bucket名称*/
final static String INPUT_BUCKET = "<your bucket name>";
/**触发路径 以 / 结尾*/
final static String INPUT_PATH = "mps-test/demo/";
/**控制台绑定的的输出bucket名称*/
final static String OUTPUT_BUCKET = "<your bucket name>";
/** 加密类型 固定hls-aes-128*/
final static String ENCRYPTION_TYPE = "hls-aes-128";
/**解密服务地址 例如http://example.aliyundoc.com*/
final static String HLS_KEY_URI = "http://127.0.0.1:8888";
final static String ACT_START = "Act-Start";
final static String ACT_ENCRYPTION = "Act-HLS-Encryption";
final static String ACT_REPORT = "Act-Report";
public static void main(String[] args) throws Exception {
//初始化调用 client
DefaultAcsClient client = InitClient.initMpsClient();
AddMediaWorkflowResponse response;
try {
response = addMediaWorkflow(client);
System.out.println("RequestId is:" + response.getRequestId());
} catch (Exception e) {
e.printStackTrace();
}
}
public static AddMediaWorkflowResponse addMediaWorkflow(IAcsClient client) throws Exception {
AddMediaWorkflowRequest request = new AddMediaWorkflowRequest();
request.setTopology(createWorkflow().toJSONString());
request.setName("HLS加密工作流");
return client.getAcsResponse(request);
}
/**
* 创建工作流数据结构
* @return
*/
private static JSONObject createWorkflow() {
JSONObject workflow = new JSONObject();
JSONObject activities = new JSONObject();
activities.put(ACT_START, createStartActivity());
activities.put(ACT_ENCRYPTION, createTrancodeActivity());
activities.put(ACT_REPORT, createReportActivity());
workflow.put("Activities", activities);
workflow.put("Dependencies", createDependencies());
return workflow;
}
/**
* 添加工作流开始节点
* @return
*/
private static JSONObject createStartActivity() {
JSONObject startActivity = new JSONObject();
startActivity.put("Name", ACT_START);
startActivity.put("Type", "Start");
startActivity.put("Parameters", buildStartParameters());
return startActivity;
}
/**
* 添加工作流输入配置
* @return
*/
private static JSONObject buildStartParameters() {
JSONObject parameters = new JSONObject();
parameters.put("PipelineId", PIPELINE_ID);
//输入buket结构
JSONObject inputFile = new JSONObject();
inputFile.put("Bucket", INPUT_BUCKET);
inputFile.put("Location", OSS_LOCATION);
inputFile.put("ObjectPrefix", INPUT_PATH);
parameters.put("InputFile", inputFile);
return parameters;
}
/**
* 添加工作流转码节点
* @return
*/
private static JSONObject createTrancodeActivity() {
JSONObject transcodeActivity = new JSONObject();
transcodeActivity.put("Name", ACT_ENCRYPTION);
transcodeActivity.put("Type", "Transcode");
JSONObject transcodeParamters = new JSONObject();
transcodeParamters.put("OutputBucket", OUTPUT_BUCKET);
transcodeParamters.put("OutputLocation", OSS_LOCATION);
transcodeParamters.put("Outputs", buildOutputsConfig());
transcodeActivity.put("Parameters", transcodeParamters);
return transcodeActivity;
}
/**
* 添加工作流输出配置
* @return
*/
private static JSONArray buildOutputsConfig() {
JSONArray outputs = new JSONArray();
JSONObject output = new JSONObject();
output.put("ObjectRegex", ACT_ENCRYPTION + "/{RunId}/{FileName}");
output.put("TemplateId", TEMPLATE_ID);
output.put("Encryption", buildEncryption());
outputs.add(output);
return outputs;
}
/**
* 加密配置
* @return
*/
private static JSONObject buildEncryption() {
JSONObject encryption = new JSONObject();
encryption.put("Type", ENCRYPTION_TYPE);
encryption.put("KeyUri", HLS_KEY_URI);
return encryption;
}
/**
* 添加Report节点配置
* @return
*/
private static JSONObject createReportActivity() {
JSONObject reportActivity = new JSONObject();
reportActivity.put("Name", ACT_REPORT);
JSONObject parameters = new JSONObject();
parameters.put("PublishType","Auto");
reportActivity.put("Parameters", parameters);
reportActivity.put("Type", "Report");
return reportActivity;
}
/**
* 添加工作流依赖关系
* 依赖关系是拓扑结构中的边,指明活动之间的依赖。
* @return
*/
private static JSONObject createDependencies() {
JSONObject dependencies = new JSONObject();
JSONArray subActivityOfStart = new JSONArray();
subActivityOfStart.add(ACT_ENCRYPTION);
dependencies.put(ACT_START, subActivityOfStart);
JSONArray subActivityOfTranscode = new JSONArray();
subActivityOfTranscode.add(ACT_REPORT);
dependencies.put(ACT_ENCRYPTION, subActivityOfTranscode);
dependencies.put(ACT_REPORT, new JSONArray());
return dependencies;
}
}
文档内容是否对您有帮助?