本文提供了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,并執行一次該工作流來打通系統配置
* 工作流數據結構較為復雜, 修改時請注意參數的準確性, 工作流數據結構參考 http://bestwisewords.com/document_detail/602860.html
* KeyUri為您的解密服務地址, 請確保可用性, 否則會導致解密播放失敗
* 如何使用標準加密 請參考: xxxxxxxxxxxx
*/
public class MediaHlsWorkflow {
/**管道ID, 可以在控制臺 【全局設置】->【管道】查看*/
final static String PIPELINE_ID = "be3a2b1ce40a0cbf****";
/**模板ID, 預置模板參考 http://bestwisewords.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;
}
}
文檔內容是否對您有幫助?