目的
ここでは、暗号化ビデオを再生するための HLS 標準暗号化ワークフロー作成の全体的な手順を説明します。
HLS 標準暗号化のアーキテクチャーの詳細は、「HLS 標準暗号化」をご参照ください。
手順
- HLS 暗号化ワークフローを作成します。
HLS 暗号化ワークフローとデモコードの作成の詳細は、「HLS 標準暗号化ワークフローの作成」をご参照ください。
注 HLS 標準ワークフローを作成するときは、テスト用の "HLS_KEY_URI" パラメーター値に「http: //127.0.0.1:8888」を入力します。 プレイ中、プレイヤーはこのアドレスへのキーをリクエストするので、ユーザーはキーを配布するためのサービスを作成します。 - ビデオをアップロードして暗号化します。
MPS コンソールのメディアファイルを使用し、ビデオをアップロードします。 ワークフローを選択するときは、新しく作成した HLS 標準暗号化ワークフローを選択します。 アップロード後、ワークフローは自動的に暗号化トランスコーディングを開始します。 ビデオが公開ステータスになったら、次の手順に従います。
- ローカル認証サービスを作成します。
HLS 標準暗号化ビデオを再生する際の認証サービスとして機能する、ローカル HTTP サービスを作成し、MtsHlsUriTokenトークンを発行してトークンの検証を行います。
Java コード依存関係の例
https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core
https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-kms
package com.aliyun.smallcode; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.http.ProtocolType; import com.aliyuncs.kms.model.v20160120. DecryptRequest; import com.aliyuncs.kms.model.v20160120. DecryptResponse; import com.aliyuncs.profile.DefaultProfile; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.spi.HttpServerProvider; import org.apache.commons.codec.binary.Base64; import java.io.IOException; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.URI; import java.util.regex.Matcher; import java.util.regex.Pattern; public class AuthorizationServer { private static DefaultAcsClient client; static { String region = ""; String accessKeyId = "<your-access-key-id>" String accessKeySecret = "<your-access-key-secret>"; client = new DefaultAcsClient(DefaultProfile.getProfile(region, accessKeyId, accessKeySecret)); } public class AuthorizationHandler implements HttpHandler { public void handle(HttpExchange httpExchange) throws IOException { String requestMethod = httpExchange.getRequestMethod(); if(requestMethod.equalsIgnoreCase("GET")){ //Get ciphertext and key from URL String ciphertext = getCiphertext(httpExchange); if (null == ciphertext) return; //decrypt ciphertext from KMS, and Base64 decode byte[] key = decrypt(ciphertext); //Set header setHeader(httpExchange, key); //Response key OutputStream responseBody = httpExchange.getResponseBody(); responseBody.write(key); responseBody.close(); } } private void setHeader(HttpExchange httpExchange, byte[] key) throws IOException { Headers responseHeaders = httpExchange.getResponseHeaders(); responseHeaders.set("Access-Control-Allow-Origin", "*"); httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, key.length); } private byte[] decrypt(String ciphertext) { DecryptRequest request = new DecryptRequest(); request.setCiphertextBlob(ciphertext); request.setProtocol(ProtocolType.HTTPS); try { DecryptResponse response = client.getAcsResponse(request); String plaintext = response.getPlaintext(); //Note: require base64 decode return Base64.decodeBase64(plaintext); } catch (ClientException e) { e.printStackTrace(); return null; } } private String getCiphertext(HttpExchange httpExchange) { URI uri = httpExchange.getRequestURI(); String queryString = uri.getQuery(); String pattern = "Ciphertext=(\\w*)"; Pattern r = Pattern.compile(pattern); Matcher m = r.matcher(queryString); if (m.find()) return m.group(1); else { System.out.println("Not Found Ciphertext"); return null; } } } private void startService() throws IOException { HttpServerProvider provider = HttpServerProvider.provider(); //listening port 8888 can accept 10 request simultaneously HttpServer httpserver = provider.createHttpServer(new InetSocketAddress(8888), 10); httpserver.createContext("/", new AuthorizationHandler()); httpserver.start(); System.out.println("server started"); } public static void main(String[] args) throws IOException { AuthorizationServer server = new AuthorizationServer(); server.startService(); } }
Python サンプルコード
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-kms
pip install aliyun-python-sdk-mts
# -*- coding: UTF-8 -*- from BaseHTTPServer import BaseHTTPRequestHandler from aliyunsdkcore.client import AcsClient from aliyunsdkkms.request.v20160120 import DecryptRequest import cgi import json import base64 import urlparse client = AcsClient("","",""); class AuthorizationHandler(BaseHTTPRequestHandler): def do_GET(self): self.check() self.set_header() cipertext = self.get_cihpertext() plaintext = self.decrypt_cihpertext(cipertext) print plaintext key = base64.b64decode(plaintext) print key self.wfile.write(key) def do_POST(self): pass def check(self): #check MtsHlsUriToken, etc. pass def set_header(self): self.send_response(200) #cors self.send_header('Access-Control-Allow-Origin', '*') self.end_headers() def get_cihpertext(self): path = urlparse.urlparse(self.path) query = urlparse.parse_qs(path.query) return query.get('Ciphertext')[0] def decrypt_cihpertext(self, cipertext): request = DecryptRequest.DecryptRequest() request.set_CiphertextBlob(cipertext) response = client.do_action_with_exception(request) jsonResp = json.loads(response) return jsonResp["Plaintext"] if __name__ == '__main__': # Start a simple server, and loop forever from BaseHTTPServer import HTTPServer print "Starting server, use to stop" server = HTTPServer(('127.0.0.1', 8888), AuthorizationHandler) server.serve_forever()
- 再生アドレスを取得します。
再生アドレスは複数の方法で取得することができます。 詳しくは、「MPS ファイル出力に関する質問」をご参照ください。
- ビデオを再生します。
オンラインプレーヤーを使用して、HLS 暗号化ビデオの再生をテストします。 詳しくは、「Alibaba Cloud プレーヤーのユーザー診断ツール」をご参照ください。
次の図に示すように、ダイアログボックスに手順 4 で取得した再生アドレスを入力し、[再生]をクリックします。
注 ブラウザーの DEBUG を使用して、プレイヤーは自動的に認証サーバーをリクエストし、復号化キーを取得します。復号化後、再生操作を行います。