Files
wvpl_license_sdk/tools/published_devices_client/tools/PublishedDevicesCli.java
2020-10-06 00:51:14 +00:00

141 lines
5.2 KiB
Java

// Copyright 2020 Google LLC. All rights reserved.
package com.google.video.widevine.jts.tools;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.google.chrome.widevine.contentpartners.v1beta1.PublishedDevices;
import com.google.common.io.BaseEncoding;
import com.google.video.widevine.jts.providers.PublishedDevicesProvider;
import com.google.video.widevine.sdk.wvpl.WvPLEnvironment;
import com.google.video.widevine.sdk.wvpl.WvPLStatus;
import com.google.video.widevine.sdk.wvpl.WvPLStatusException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
/**
* Published Devices Command Line Interface.
*
* Provides a command line interface to get the latest {@code PublishedDevices} data from the
* Widevine Published Devices Service.
*
* Default Widevine Service API path: widevine.googleapis.com.
*
* To build:
* Bazel build java/com/google/video/widevine/jts/tools:published_devices_client_deploy.jar
*
* To run:
* java -Djava.library.path=./path/to/license/sdk.so \
* -jar /path/to/published_devices_client_deploy.jar \
* -service_cert_path /path/to/cert_file.der \
* -service_private_key_path /path/to/private_key.der \
* -service_private_key_passphrase theprivatekeypassphrase
* -service_account_path /path/to/service-account.json \
*/
public final class PublishedDevicesCli {
private WvPLEnvironment environment = null;
private PublishedDevicesProvider publishedDevices = null;
private String serviceCertPath = null;
private String privateKeyPath = null;
private String privateKeyPassphrase = null;
/** Command Line Flags. */
static class Flags{
private Flags() {}
@Parameter(names = "-service_cert_path", description = "Path to service certificate")
private String serviceCertPath = "";
@Parameter(names = "-service_private_key_path",
description = "Path to private key file needed to decrypt service certificate")
private String servicePrivateKeyPath = "";
@Parameter(names = "-service_private_key_passphrase",
description = "Passphrase needed to decrypt the private key")
private String servicePrivateKeyPassphrase = "";
@Parameter(names = "-service_account_path",
description = "Path to a GCP Service Account json file")
private String serviceAccountPath = "";
@Parameter(names = "-api_service_path",
description = "Optional. Path to a Widevine API service.")
private String apiServicePath = "widevine.googleapis.com";
@Parameter(names = "-s",
description = "Save PublishedDevices file path.")
private String saveFilePath = null;
}
public PublishedDevicesCli(String serviceCertPath, String privateKeyPath,
String privateKeyPassphrase, String serviceAccountPath, String apiServicePath)
throws Exception {
this.serviceCertPath = serviceCertPath;
this.privateKeyPath = privateKeyPath;
this.privateKeyPassphrase = privateKeyPassphrase;
initializeWvplEnvironment();
publishedDevices = new PublishedDevicesProvider(
environment, serviceAccountPath, apiServicePath);
}
/**
* Get the latest {@code PublishedDevices} from the Widevine Published Devices Service.
*
* @return The latest PublishedDevices data.
* @throws InterruptedException upon RPC failure.
* @throws WvPLStatusException upon WvPLBaseEnvironment errors.
*/
public PublishedDevices getPublishedDevices() throws InterruptedException, WvPLStatusException {
return publishedDevices.getPublishedDevices();
}
/**
* Saves PublishedDevices proto data to a file.
*
* @param saveFilePath The path to the save file.
* @param publishedDevices A PublishedDevices proto.
*/
public static void savePublishedDevicesDataToFile(String saveFilePath,
PublishedDevices publishedDevices) throws IOException {
Files.write(
Paths.get(saveFilePath),
BaseEncoding.base64Url().encode(publishedDevices.toByteArray()).getBytes(UTF_8));
}
public static void main(String[] args) throws Exception {
Flags flags = new Flags();
new JCommander(flags, args);
PublishedDevicesCli devices = new PublishedDevicesCli(
flags.serviceCertPath,
flags.servicePrivateKeyPath,
flags.servicePrivateKeyPassphrase,
flags.serviceAccountPath,
flags.apiServicePath);
PublishedDevices publishedDevices = devices.getPublishedDevices();
if (flags.saveFilePath != null) {
savePublishedDevicesDataToFile(flags.saveFilePath, publishedDevices);
}
}
private void initializeWvplEnvironment() throws Exception {
environment = new WvPLEnvironment(new HashMap<>());
// Set service certificate.
WvPLStatus status =
environment.setServiceCertificate(
loadDataFromFile(serviceCertPath),
loadDataFromFile(privateKeyPath),
privateKeyPassphrase.getBytes(UTF_8));
if (!status.getStatusCode().equals(WvPLStatus.StatusCode.OK)) {
throw new Exception("Set server certificate status: " + status);
}
}
private static byte[] loadDataFromFile(String filePath) throws IOException {
return Files.readAllBytes(Paths.get(filePath));
}
}