/**
  * @covers WindowsAzure\MediaServices\Models\AssetDeliveryPolicy::getAssetDeliveryPolicyType
  * @covers WindowsAzure\MediaServices\Models\AssetDeliveryPolicy::setAssetDeliveryPolicyType
  */
 public function testGetSetAssetDeliveryPolicyType()
 {
     // Setup
     $expected = AssetDeliveryPolicyType::DYNAMIC_COMMON_ENCRYPTION;
     $assetDeliveryPolicy = new AssetDeliveryPolicy();
     // Test
     $assetDeliveryPolicy->setAssetDeliveryPolicyType($expected);
     $result = $assetDeliveryPolicy->getAssetDeliveryPolicyType();
     // Assert
     $this->assertEquals($expected, $result);
 }
 /**
  * 
  * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::deleteAssetDeliveryPolicy
  */
 public function testDeleteAssetDeliveryPolicy()
 {
     // Setup
     $countBefore = count($this->restProxy->getAssetDeliveryPolicyList());
     $name = TestResources::MEDIA_SERVICES_ASSET_DELIVERY_POLICY_NAME . $this->createSuffix();
     $policy = new AssetDeliveryPolicy();
     $policy->setName($name);
     $policy->setAssetDeliveryProtocol(AssetDeliveryProtocol::ALL);
     $policy->setAssetDeliveryPolicyType(AssetDeliveryPolicyType::BLOCKED);
     $result = $this->restProxy->createAssetDeliveryPolicy($policy);
     $countMiddle = count($this->restProxy->getAssetDeliveryPolicyList());
     // Test
     $this->restProxy->deleteAssetDeliveryPolicy($result);
     $countAfter = count($this->restProxy->getAssetDeliveryPolicyList());
     // Assert
     $this->assertEquals($countMiddle - 1, $countBefore);
     $this->assertEquals($countBefore, $countAfter);
     $this->assertEquals($countMiddle - 1, $countAfter);
 }
 private function makeProgram($name, $channel)
 {
     $asset = new Asset(Asset::OPTIONS_NONE);
     $asset->setName(TestResources::MEDIA_SERVICES_ASSET_NAME . $this->createSuffix());
     $asset = $this->createAsset($asset);
     $policy = new AssetDeliveryPolicy();
     $policy->setName("Clear Policy");
     $policy->setAssetDeliveryProtocol(AssetDeliveryProtocol::SMOOTH_STREAMING | AssetDeliveryProtocol::DASH | AssetDeliveryProtocol::HLS);
     $policy->setAssetDeliveryPolicyType(AssetDeliveryPolicyType::NO_DYNAMIC_ENCRYPTION);
     $policy = $this->createAssetDeliveryPolicy($policy);
     $this->restProxy->linkDeliveryPolicyToAsset($asset, $policy);
     $program = new Program();
     $program->setName($name);
     $program->setAssetId($asset->getId());
     $program->setArchiveWindowLength(new \DateInterval("PT1H"));
     $program->setChannelId($channel->getId());
     return $program;
 }
function createAssetDeliveryPolicy($restProxy, $encodedAsset, $contentKey, $fairPlayIV)
{
    // 5.1 Get the acquisition URL
    $acquisitionUrl = $restProxy->getKeyDeliveryUrl($contentKey, ContentKeyDeliveryType::FAIRPLAY);
    $acquisitionUrl = str_replace_first("https", "skd", $acquisitionUrl);
    // remove query string
    if (strpos($acquisitionUrl, '?') !== false) {
        $acquisitionUrl = substr($acquisitionUrl, 0, strrpos($acquisitionUrl, "?"));
    }
    // 5.2 Generate the AssetDeliveryPolicy Configuration Key
    $configuration = [AssetDeliveryPolicyConfigurationKey::FAIRPLAY_BASE_LICENSE_ACQUISITION_URL => $acquisitionUrl, AssetDeliveryPolicyConfigurationKey::COMMON_ENCRYPTION_IV_FOR_CBCS => $fairPlayIV];
    $confJson = AssetDeliveryPolicyConfigurationKey::stringifyAssetDeliveryPolicyConfiguartionKey($configuration);
    // 5.3 Create the AssetDeliveryPolicy
    $adpolicy = new AssetDeliveryPolicy();
    $adpolicy->setName('Asset Delivery Policy');
    $adpolicy->setAssetDeliveryProtocol(AssetDeliveryProtocol::HLS);
    $adpolicy->setAssetDeliveryPolicyType(AssetDeliveryPolicyType::DYNAMIC_COMMON_ENCRYPTION_CBCS);
    $adpolicy->setAssetDeliveryConfiguration($confJson);
    $adpolicy = $restProxy->createAssetDeliveryPolicy($adpolicy);
    // 5.4 Link the AssetDeliveryPolicy to the Asset
    $restProxy->linkDeliveryPolicyToAsset($encodedAsset, $adpolicy->getId());
    echo "Added Asset Delivery Policy: name={$adpolicy->getName()} id={$adpolicy->getId()}" . PHP_EOL;
}
function createAssetDeliveryPolicy($restProxy, $encodedAsset, $contentKey)
{
    // 5.1 Get the acquisition URL
    $acquisitionUrl = $restProxy->getKeyDeliveryUrl($contentKey, ContentKeyDeliveryType::BASELINE_HTTP);
    // 5.2 Generate the AssetDeliveryPolicy Configuration Key
    $randomKey = Utilities::generateCryptoKey(16);
    $configuration = [AssetDeliveryPolicyConfigurationKey::ENVELOPE_KEY_ACQUISITION_URL => $acquisitionUrl, AssetDeliveryPolicyConfigurationKey::ENVELOPE_ENCRYPTION_IV_AS_BASE64 => base64_encode($randomKey)];
    $confJson = AssetDeliveryPolicyConfigurationKey::stringifyAssetDeliveryPolicyConfiguartionKey($configuration);
    // 5.3 Create the AssetDeliveryPolicy
    $adpolicy = new AssetDeliveryPolicy();
    $adpolicy->setName('Asset Delivery Policy');
    $adpolicy->setAssetDeliveryProtocol(AssetDeliveryProtocol::SMOOTH_STREAMING | AssetDeliveryProtocol::DASH | AssetDeliveryProtocol::HLS);
    $adpolicy->setAssetDeliveryPolicyType(AssetDeliveryPolicyType::DYNAMIC_ENVELOPE_ENCRYPTION);
    $adpolicy->setAssetDeliveryConfiguration($confJson);
    $adpolicy = $restProxy->createAssetDeliveryPolicy($adpolicy);
    // 5.4 Link the AssetDeliveryPolicy to the Asset
    $restProxy->linkDeliveryPolicyToAsset($encodedAsset, $adpolicy->getId());
    print "Added Asset Delivery Policy: name={$adpolicy->getName()} id={$adpolicy->getId()}\r\n";
}
// 4.5 Associate the ContentKeyAuthorizationPolicy with the ContentKey
$contentKey->setAuthorizationPolicyId($ckapolicy->getId());
$restProxy->updateContentKey($contentKey);
print "Added Content Key Authorization Policy: name={$ckapolicy->getName()} id={$ckapolicy->getId()}\r\n";
// Phase 5 - Create the AssetDeliveryPolicy
// 5.1 Get the acquisition URL
$acquisitionUrl = $restProxy->getKeyDeliveryUrl($contentKey, ContentKeyDeliveryType::BASELINE_HTTP);
// 5.2 Generate the AssetDeliveryPolicy Configuration Key
$randomKey = Utilities::generateCryptoKey(16);
$configuration = [AssetDeliveryPolicyConfigurationKey::ENVELOPE_KEY_ACQUISITION_URL => $acquisitionUrl, AssetDeliveryPolicyConfigurationKey::ENVELOPE_ENCRYPTION_IV_AS_BASE64 => base64_encode($randomKey)];
$confJson = AssetDeliveryPolicyConfigurationKey::stringifyAssetDeliveryPolicyConfiguartionKey($configuration);
// 5.3 Create the AssetDeliveryPolicy
$adpolicy = new AssetDeliveryPolicy();
$adpolicy->setName('Asset Delivery Policy');
$adpolicy->setAssetDeliveryProtocol(AssetDeliveryProtocol::SMOOTH_STREAMING | AssetDeliveryProtocol::DASH | AssetDeliveryProtocol::HLS);
$adpolicy->setAssetDeliveryPolicyType(AssetDeliveryPolicyType::DYNAMIC_ENVELOPE_ENCRYPTION);
$adpolicy->setAssetDeliveryConfiguration($confJson);
$adpolicy = $restProxy->createAssetDeliveryPolicy($adpolicy);
// 5.4 Link the AssetDeliveryPolicy to the Asset
$restProxy->linkDeliveryPolicyToAsset($encodedAsset, $adpolicy->getId());
print "Added Asset Delivery Policy: name={$adpolicy->getName()} id={$adpolicy->getId()}\r\n";
// Phase 6 - Publish
// 6.1 Get the .ISM AssetFile
$files = $restProxy->getAssetFileList();
$manifestFile = null;
foreach ($files as $file) {
    //print "File {$file->getName()} assetId={$file->getParentAssetId()}\r\n";
    if ($file->getParentAssetId() === $encodedAsset->getId() && endsWith(strtolower($file->getName()), '.ism')) {
        $manifestFile = $file;
    }
}
function createAssetDeliveryPolicy($restProxy, $encodedAsset, $contentKey)
{
    // 5.1 Get the acquisition URL
    $acquisitionUrl = $restProxy->getKeyDeliveryUrl($contentKey, ContentKeyDeliveryType::PLAYREADY_LICENSE);
    $widevineURl = $restProxy->getKeyDeliveryUrl($contentKey, ContentKeyDeliveryType::WIDEVINE);
    // 5.2 Generate the AssetDeliveryPolicy Configuration Key
    $configuration = [AssetDeliveryPolicyConfigurationKey::PLAYREADY_LICENSE_ACQUISITION_URL => $acquisitionUrl, AssetDeliveryPolicyConfigurationKey::WIDEVINE_LICENSE_ACQUISITION_URL => $widevineURl];
    $confJson = AssetDeliveryPolicyConfigurationKey::stringifyAssetDeliveryPolicyConfiguartionKey($configuration);
    // 5.3 Create the AssetDeliveryPolicy
    $adpolicy = new AssetDeliveryPolicy();
    $adpolicy->setName('Asset Delivery Policy');
    $adpolicy->setAssetDeliveryProtocol(AssetDeliveryProtocol::DASH);
    $adpolicy->setAssetDeliveryPolicyType(AssetDeliveryPolicyType::DYNAMIC_COMMON_ENCRYPTION);
    $adpolicy->setAssetDeliveryConfiguration($confJson);
    $adpolicy = $restProxy->createAssetDeliveryPolicy($adpolicy);
    // 5.4 Link the AssetDeliveryPolicy to the Asset
    $restProxy->linkDeliveryPolicyToAsset($encodedAsset, $adpolicy->getId());
    print "Added Asset Delivery Policy: name={$adpolicy->getName()} id={$adpolicy->getId()}\r\n";
}
function createAssetDeliveryPolicy($restProxy, $encodedAsset, $contentKey)
{
    // 5.1 Get the acquisition URL
    $acquisitionUrl = $restProxy->getKeyDeliveryUrl($contentKey, ContentKeyDeliveryType::PLAYREADY_LICENSE);
    $widevineUrl = $restProxy->getKeyDeliveryUrl($contentKey, ContentKeyDeliveryType::WIDEVINE);
    // remove query string
    if (strpos($widevineUrl, '?') !== false) {
        $widevineUrl = substr($widevineUrl, 0, strrpos($widevineUrl, "?"));
    }
    // 5.2 Generate the AssetDeliveryPolicy Configuration Key
    $configuration = [AssetDeliveryPolicyConfigurationKey::PLAYREADY_LICENSE_ACQUISITION_URL => $acquisitionUrl, AssetDeliveryPolicyConfigurationKey::WIDEVINE_BASE_LICENSE_ACQUISITION_URL => $widevineUrl];
    $confJson = AssetDeliveryPolicyConfigurationKey::stringifyAssetDeliveryPolicyConfiguartionKey($configuration);
    // 5.3 Create the AssetDeliveryPolicy
    $adpolicy = new AssetDeliveryPolicy();
    $adpolicy->setName('Asset Delivery Policy');
    $adpolicy->setAssetDeliveryProtocol(AssetDeliveryProtocol::DASH);
    $adpolicy->setAssetDeliveryPolicyType(AssetDeliveryPolicyType::DYNAMIC_COMMON_ENCRYPTION);
    $adpolicy->setAssetDeliveryConfiguration($confJson);
    $adpolicy = $restProxy->createAssetDeliveryPolicy($adpolicy);
    // 5.4 Link the AssetDeliveryPolicy to the Asset
    $restProxy->linkDeliveryPolicyToAsset($encodedAsset, $adpolicy->getId());
    echo "Added Asset Delivery Policy: name={$adpolicy->getName()} id={$adpolicy->getId()}" . PHP_EOL;
}