编码器需要对一个内容进行编码时,可以调用KMS的getKeyInfo接口,KMS生成内容加密密钥Key、KeyID、PSSH Box信息,同时将内容加密密钥Key、KeyID信息存储在KMS的数据库中,然后将KeyInfo、ContentInfo、PSSH Box信息全部返回给编码器。
KMS接口实现的主要步骤如下:
@RequestMapping(value = "/getKeyInfo", method = RequestMethod.POST) @ResponseBody public BaseRsp getKeyInfo(@RequestBody GetKeyInfoReq request) { return getKeyinfoService.service(request); }
public List<KeyContentInfoRecord> getKeyInfo(GetKeyInfoReq req) throws KMSException {
List<KeyContentInfoRecord> records = null;
ContentInfo contentInfo = req.getContentInfo();
// if vod generate key info by definition
if (KMSContants.DistributionMode.VOD.equals(contentInfo.getDistributionMode())) {
records = generateVodKeyInfos(req);
} else if (KMSContants.DistributionMode.LIVE.equals(contentInfo.getDistributionMode())) {
records = generateLiveKeyInfos(req);
}
return records;
} KeyContentInfoRecord keyInfo = null;
List<KeyContentInfoRecord> records = this.getKeyInfoRecord(context.contentInfo.getContentId());
// if keyinfo exsit and compare definition and index
if (records != null) {
for (KeyContentInfoRecord record : records) {
if (checkDefinitons(context.definition, record.getDefinition()) && (context.index ==
null||String.valueOf(context.index).equals(record.getLiveIndex()))) {
keyInfo = record;
break;
}
}
}
if (keyInfo == null) {
keyInfo = this.createKeyInfo(context);
this.addKeyInfo(keyInfo);
} DrmSignalization drmSignalization = new DrmSignalization();
List<String> drmTypeList = req.getDrmTypeList();
for (String drmType : drmTypeList) {
// code is support wiseplay
if (!KMSContants.DrmType.WISEPLAY.equals(drmType)) {
continue;
}
if (KMSContants.StreamingMode.DASH.equals(req.getContentInfo().getStreamingMode())) {
drmSignalization.getDash().add(getDash(keyInfos));
} else if (KMSContants.StreamingMode.HLS.equals(req.getContentInfo().getStreamingMode())) {
drmSignalization.getHls().add(getHls(keyInfos));
}
} private KeyInfo getKeyInfo(KeyContentInfoRecord keyContentInfo) throws KMSException {
KeyInfo keyInfo = new KeyInfo();
keyInfo.setKeyId(keyContentInfo.getKeyId());
//iv base64 encode
String iv = AESEncrypter.getInstance().decrypt(keyContentInfo.getIv());
keyInfo.setIv(iv);
String keyEncryptedIV = AESEncrypter.getInstance().decrypt(keyContentInfo.getKeyEncryptedIv());
keyInfo.setKeyEncryptedIV(keyEncryptedIV);
//keyid AES/CBC/PKCS5Padding with aes and iv and then base64 encode
String key = AESEncrypter.getInstance().encrypt(AESEncrypter.getInstance().decrypt(keyContentInfo.getKey()),
Configurator.getConfigValue("enc:aes_key"), Base64.getDecoder().decode(keyEncryptedIV));
keyInfo.setKey(key);
keyInfo.setDefinition(keyContentInfo.getDefinition());
keyInfo.setLiveIndex(keyContentInfo.getLiveIndex());
return keyInfo;
}
样例代码中仅校验了必填字段,业务需要进一步开发完善代码。