目录

服务端开发

获取KMS代码集成

KMS在系统中的位置如下图所示:

具体示例代码工程和接口规范样例请从配置AppGallery Connect所示的“数字版权管理服务”页面下载获取。

获取视频加密信息

编码器需要对一个内容进行编码时,可以调用KMS的getKeyInfo接口,KMS生成内容加密密钥Key、KeyID、PSSH Box信息,同时将内容加密密钥Key、KeyID信息存储在KMS的数据库中,然后将KeyInfo、ContentInfo、PSSH Box信息全部返回给编码器。

KMS接口实现的主要步骤如下:

  1. 基于SpringMVC定义一个getKeyInfo接口,接口定义请参见KMS接口规范样例。

    KMS接口规范样例请登录AppGallery Connect网站,选择“我的项目 > 构建 > 数字版权管理服务 > KMS API”。详情参见开通数字版权管理服务。

     @RequestMapping(value = "/getKeyInfo", method = RequestMethod.POST)
     @ResponseBody
     public BaseRsp getKeyInfo(@RequestBody GetKeyInfoReq request) {
         return getKeyinfoService.service(request);
     }

  2. 判断请求中contentInfo.getDistributionMode()返回的播放类型,生成点播或直播对应的KeyContentInfoRecord列表。

     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;
    }

  3. 根据contentId查询数据库,获取keycontentinfo对象(包括:keyinfo信息、content信息)。数据库不存在记录,则创建keyinfo信息。

     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);
      }

  4. 根据streamingMode封装dash或hls对象。

     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));   
         }
      }

  5. 返回getKeyInfo响应。

    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;
      }

样例代码中仅校验了必填字段,业务需要进一步开发完善代码。