public function testIngestEncryptedAssetAndDecryptAtAzure()
 {
     // Setup
     $content = TestResources::MEDIA_SERVICES_DUMMY_FILE_CONTENT;
     $aesKey = Utilities::generateCryptoKey(32);
     $protectionKeyId = $this->restProxy->getProtectionKeyId(ProtectionKeyTypes::X509_CERTIFICATE_THUMBPRINT);
     $protectionKey = $this->restProxy->getProtectionKey($protectionKeyId);
     $contentKey = new ContentKey();
     $contentKey->setContentKey($aesKey, $protectionKey);
     $contentKey->setProtectionKeyId($protectionKeyId);
     $contentKey->setProtectionKeyType(ProtectionKeyTypes::X509_CERTIFICATE_THUMBPRINT);
     $contentKey->setContentKeyType(ContentKeyTypes::STORAGE_ENCRYPTION);
     $contentKey = $this->createContentKey($contentKey);
     $asset = new Asset(Asset::OPTIONS_STORAGE_ENCRYPTED);
     $asset->setName(TestResources::MEDIA_SERVICES_ASSET_NAME . $this->createSuffix());
     $asset = $this->createAsset($asset);
     $this->restProxy->linkContentKeyToAsset($asset, $contentKey);
     $initializationVector = Utilities::generateCryptoKey(8);
     $encrypted = Utilities::ctrCrypt($content, $aesKey, str_pad($initializationVector, 16, chr(0)));
     // Test
     $access = new AccessPolicy(TestResources::MEDIA_SERVICES_ACCESS_POLICY_NAME . $this->createSuffix());
     $access->setDurationInMinutes(30);
     $access->setPermissions(AccessPolicy::PERMISSIONS_WRITE);
     $access = $this->createAccessPolicy($access);
     $locator = new Locator($asset, $access, Locator::TYPE_SAS);
     $locator->setName(TestResources::MEDIA_SERVICES_LOCATOR_NAME . $this->createSuffix());
     $locator->setStartTime(new \DateTime('now -5 minutes'));
     $locator = $this->createLocator($locator);
     $fileName = TestResources::MEDIA_SERVICES_DUMMY_FILE_NAME;
     $this->restProxy->uploadAssetFile($locator, $fileName, $encrypted);
     $this->restProxy->createFileInfos($asset);
     $files = $this->restProxy->getAssetAssetFileList($asset);
     $files[0]->setIsEncrypted(true);
     $files[0]->setEncryptionKeyId($contentKey->getId());
     $files[0]->setEncryptionScheme(EncryptionSchemes::STORAGE_ENCRYPTION);
     $files[0]->setEncryptionVersion(Resources::MEDIA_SERVICES_ENCRYPTION_VERSION);
     $files[0]->setInitializationVector(Utilities::base256ToDec($initializationVector));
     $this->restProxy->updateAssetFile($files[0]);
     $decodeProcessor = $this->restProxy->getLatestMediaProcessor(TestResources::MEDIA_SERVICES_DECODE_PROCESSOR_NAME);
     $task = new Task(TestResources::getMediaServicesTask($this->getOutputAssetName()), $decodeProcessor->getId(), TaskOptions::NONE);
     $job = new Job();
     $job->setName(TestResources::MEDIA_SERVICES_JOB_NAME . $this->createSuffix());
     $job = $this->createJob($job, array($asset), array($task));
     $this->waitJobStatus($job, array(Job::STATE_FINISHED, Job::STATE_ERROR));
     $this->assertEquals($this->restProxy->getJobStatus($job), Job::STATE_FINISHED);
     $outputAssets = $this->restProxy->getJobOutputMediaAssets($job);
     $this->assertCount(1, $outputAssets);
     $accessPolicy = new AccessPolicy(TestResources::MEDIA_SERVICES_ACCESS_POLICY_NAME . $this->createSuffix());
     $accessPolicy->setDurationInMinutes(300);
     $accessPolicy->setPermissions(AccessPolicy::PERMISSIONS_READ);
     $accessPolicy = $this->createAccessPolicy($accessPolicy);
     $locator = new Locator($outputAssets[0], $accessPolicy, Locator::TYPE_SAS);
     $locator->setName(TestResources::MEDIA_SERVICES_LOCATOR_NAME . $this->createSuffix());
     $locator->setStartTime(new \DateTime('now -5 minutes'));
     $locator = $this->createLocator($locator);
     // without sleep() Locator hasn't enough time to create URL, so that's why we have to use at least sleep(30)
     sleep(40);
     $method = Resources::HTTP_GET;
     $url = new Url($locator->getBaseUri() . '/' . TestResources::MEDIA_SERVICES_DUMMY_FILE_NAME . $locator->getContentAccessComponent());
     $filters = array();
     $statusCode = Resources::STATUS_OK;
     $httpClient = new HttpClient();
     $httpClient->setMethod($method);
     $httpClient->setExpectedStatusCode($statusCode);
     $actual = $httpClient->send($filters, $url);
     $this->assertEquals($content, $actual);
 }
 /**
  * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::createAsset
  * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::createAccessPolicy
  * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::createLocator
  */
 public function testCreatingOriginUrlForStreamingContent()
 {
     // Setup
     $asset = $this->createAssetWithFilesForStream();
     $accessPolicy = new AccessPolicy(TestResources::MEDIA_SERVICES_ACCESS_POLICY_NAME . $this->createSuffix());
     $accessPolicy->setDurationInMinutes(300);
     $accessPolicy->setPermissions(AccessPolicy::PERMISSIONS_READ);
     $accessPolicy = $this->createAccessPolicy($accessPolicy);
     $locator = new Locator($asset, $accessPolicy, Locator::TYPE_ON_DEMAND_ORIGIN);
     $locator->setName(TestResources::MEDIA_SERVICES_LOCATOR_NAME . $this->createSuffix());
     $locator->setStartTime(new \DateTime('now -5 minutes'));
     $locator = $this->createLocator($locator);
     $expectedFileContent = TestResources::getSmallIsmc();
     // without sleep() Locator hasn't enough time to create URL, so that's why we have to use at least sleep(30)
     sleep(40);
     // Test
     $method = Resources::HTTP_GET;
     $url = new Url($locator->getPath() . '/' . TestResources::MEDIA_SERVICES_ISM_FILE_NAME . '/' . TestResources::MEDIA_SERVICES_STREAM_APPEND);
     $filters = array();
     $statusCode = Resources::STATUS_OK;
     $httpClient = new HttpClient();
     $httpClient->setMethod($method);
     $httpClient->setExpectedStatusCode($statusCode);
     $result = $httpClient->send($filters, $url);
     // Assert
     $this->assertEquals($expectedFileContent, $result);
 }
 /**
  * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::updateLocator
  */
 public function testUpdateLocator()
 {
     // Setup
     $asset = new Asset(Asset::OPTIONS_NONE);
     $asset->setName(TestResources::MEDIA_SERVICES_ASSET_NAME . $this->createSuffix());
     $asset = $this->createAsset($asset);
     $access = new AccessPolicy(TestResources::MEDIA_SERVICES_ACCESS_POLICY_NAME . $this->createSuffix());
     $access->setDurationInMinutes(30);
     $access->setPermissions(AccessPolicy::PERMISSIONS_READ);
     $access = $this->createAccessPolicy($access);
     $locator = new Locator($asset, $access, Locator::TYPE_ON_DEMAND_ORIGIN);
     $locator->setName(TestResources::MEDIA_SERVICES_LOCATOR_NAME . $this->createSuffix());
     $locator = $this->createLocator($locator);
     $newName = TestResources::MEDIA_SERVICES_LOCATOR_NAME . $this->createSuffix();
     // Test
     $locator->setName($newName);
     $this->restProxy->updateLocator($locator);
     // Assert
     $this->assertEquals($newName, $locator->getName());
 }
function publishEncodedAsset($restProxy, $encodedAsset)
{
    // 6.1 Get the .ISM AssetFile
    $files = $restProxy->getAssetAssetFileList($encodedAsset);
    $manifestFile = null;
    foreach ($files as $file) {
        if (endsWith(strtolower($file->getName()), '.ism')) {
            $manifestFile = $file;
        }
    }
    if ($manifestFile == null) {
        echo "Unable to found the manifest file" . PHP_EOL;
        exit(-1);
    }
    // 6.2 Create a 30-day read-only AccessPolicy
    $access = new AccessPolicy("Streaming Access Policy");
    $access->setDurationInMinutes(60 * 24 * 30);
    $access->setPermissions(AccessPolicy::PERMISSIONS_READ);
    $access = $restProxy->createAccessPolicy($access);
    // 6.3 Create a Locator using the AccessPolicy and Asset
    $locator = new Locator($encodedAsset, $access, Locator::TYPE_ON_DEMAND_ORIGIN);
    $locator->setName("Streaming Locator");
    $locator = $restProxy->createLocator($locator);
    // 6.4 Create a Smooth Streaming base URL
    $stremingUrl = $locator->getPath() . $manifestFile->getName() . "/manifest(format=m3u8-aapl)";
    echo "Streaming URL: {$stremingUrl}" . PHP_EOL;
}
 public function createAssetWithFilesForStream()
 {
     $asset = new Asset(Asset::OPTIONS_NONE);
     $asset->setName(TestResources::MEDIA_SERVICES_ASSET_NAME . $this->createSuffix());
     $asset = $this->createAsset($asset);
     $access = new AccessPolicy(TestResources::MEDIA_SERVICES_ACCESS_POLICY_NAME . $this->createSuffix());
     $access->setDurationInMinutes(30);
     $access->setPermissions(AccessPolicy::PERMISSIONS_WRITE);
     $access = $this->createAccessPolicy($access);
     $locator = new Locator($asset, $access, Locator::TYPE_SAS);
     $locator->setName(TestResources::MEDIA_SERVICES_LOCATOR_NAME . $this->createSuffix());
     $locator->setStartTime(new \DateTime('now -5 minutes'));
     $locator = $this->createLocator($locator);
     $firstFile = TestResources::getSmallIsm();
     $secondFile = TestResources::getSmallIsmc();
     $this->restProxy->uploadAssetFile($locator, TestResources::MEDIA_SERVICES_ISM_FILE_NAME, $firstFile);
     $this->restProxy->uploadAssetFile($locator, TestResources::MEDIA_SERVICES_ISMC_FILE_NAME, $secondFile);
     $this->restProxy->createFileInfos($asset);
     return $asset;
 }
    if ($file->getParentAssetId() === $encodedAsset->getId() && endsWith(strtolower($file->getName()), '.ism')) {
        $manifestFile = $file;
    }
}
if ($manifestFile == null) {
    print "Unable to found the manifest file\r\n";
    return -1;
}
// 6.2 Create a 30-day readonly AccessPolicy
$access = new AccessPolicy("Streaming Access Policy");
$access->setDurationInMinutes(60 * 24 * 30);
$access->setPermissions(AccessPolicy::PERMISSIONS_READ);
$access = $restProxy->createAccessPolicy($access);
// 6.3 Create a Locator using the AccessPolicy and Asset
$locator = new Locator($encodedAsset, $access, Locator::TYPE_ON_DEMAND_ORIGIN);
$locator->setName("Streaming Locator");
$locator = $restProxy->createLocator($locator);
// 6.4 Create a Smooth Streaming base URL
$stremingUrl = $locator->getPath() . $manifestFile->getName() . "/manifest";
print "Done! Streaming URL: {$stremingUrl}\r\n";
// the End
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/// HELPER FUNCTIONS
function endsWith($haystack, $needle)
{
    $length = strlen($needle);
    if ($length == 0) {
        return true;
    }
    return substr($haystack, -$length) === $needle;
}
 public function uploadSingleFile($fileName, $fileContent)
 {
     $asset = new Asset(Asset::OPTIONS_NONE);
     $asset->setName($fileName);
     $asset = $this->createAsset($asset);
     $access = new AccessPolicy(TestResources::MEDIA_SERVICES_ACCESS_POLICY_NAME . $this->createSuffix());
     $access->setDurationInMinutes(30);
     $access->setPermissions(AccessPolicy::PERMISSIONS_WRITE);
     $access = $this->createAccessPolicy($access);
     $locator = new Locator($asset, $access, Locator::TYPE_SAS);
     $locator->setName(TestResources::MEDIA_SERVICES_LOCATOR_NAME . $this->createSuffix());
     $locator->setStartTime(new \DateTime('now -5 minutes'));
     $locator = $this->createLocator($locator);
     $this->restProxy->uploadAssetFile($locator, $fileName, $fileContent);
     $this->restProxy->createFileInfos($asset);
     $list = $this->restProxy->getAssetAssetFileList($asset);
     if (isset($list[0])) {
         $list[0]->setIsPrimary(true);
         $this->restProxy->updateAssetFile($list[0]);
     } else {
         throw new Exception("unable to find the asset file");
     }
     return $asset;
 }
 public function uploadFile($fileName, $fileContent)
 {
     $asset = new Asset(Asset::OPTIONS_NONE);
     $asset->setName(TestResources::MEDIA_SERVICES_ASSET_NAME . $this->createSuffix());
     $asset = $this->createAsset($asset);
     $access = new AccessPolicy(TestResources::MEDIA_SERVICES_ACCESS_POLICY_NAME . $this->createSuffix());
     $access->setDurationInMinutes(30);
     $access->setPermissions(AccessPolicy::PERMISSIONS_WRITE);
     $access = $this->createAccessPolicy($access);
     $locator = new Locator($asset, $access, Locator::TYPE_SAS);
     $locator->setName(TestResources::MEDIA_SERVICES_LOCATOR_NAME . $this->createSuffix());
     $locator->setStartTime(new \DateTime('now -5 minutes'));
     $locator = $this->createLocator($locator);
     $this->restProxy->uploadAssetFile($locator, $fileName, $fileContent);
     $this->restProxy->createFileInfos($asset);
     $accessPolicy = new AccessPolicy(TestResources::MEDIA_SERVICES_ACCESS_POLICY_NAME . $this->createSuffix());
     $accessPolicy->setDurationInMinutes(300);
     $accessPolicy->setPermissions(AccessPolicy::PERMISSIONS_READ);
     $accessPolicy = $this->createAccessPolicy($accessPolicy);
     $locator = new Locator($asset, $accessPolicy, Locator::TYPE_SAS);
     $locator->setName(TestResources::MEDIA_SERVICES_LOCATOR_NAME . $this->createSuffix());
     $locator->setStartTime(new \DateTime('now -5 minutes'));
     $locator = $this->createLocator($locator);
     // without sleep() Locator hasn't enough time to create URL, so that's why we have to use at least sleep(30)
     sleep(40);
     $method = Resources::HTTP_GET;
     $url = new Url($locator->getBaseUri() . '/' . $fileName . $locator->getContentAccessComponent());
     $filters = array();
     $statusCode = Resources::STATUS_OK;
     $httpClient = new HttpClient();
     $httpClient->setMethod($method);
     $httpClient->setExpectedStatusCode($statusCode);
     return $httpClient->send($filters, $url);
 }
 /**
  * @covers WindowsAzure\MediaServices\Models\Locator::getName
  * @covers WindowsAzure\MediaServices\Models\Locator::setName
  */
 public function testGetName()
 {
     // Setup
     $assetId = 'uifygid75';
     $accessId = 'ljhsdfl45';
     $type = Locator::TYPE_NONE;
     $locator = new Locator($assetId, $accessId, $type);
     $name = 'nameName';
     $locator->setName($name);
     // Test
     $actual = $locator->getName();
     // Assert
     $this->assertEquals($name, $actual);
 }
function publishLiveAsset($restProxy, $asset)
{
    // 1 Get the .ISM AssetFile
    echo "Publishing Asset...";
    $files = $restProxy->getAssetAssetFileList($asset);
    $manifestFile = null;
    foreach ($files as $file) {
        if (endsWith(strtolower($file->getName()), '.ism')) {
            $manifestFile = $file;
        }
    }
    if ($manifestFile == null) {
        echo "Unable to found the manifest file" . PHP_EOL;
        exit(-1);
    }
    // 2 Create a 30-day read-only AccessPolicy
    $access = new AccessPolicy('Live Access Policy');
    $access->setDurationInMinutes(60 * 24 * 30);
    $access->setPermissions(AccessPolicy::PERMISSIONS_READ);
    $access = $restProxy->createAccessPolicy($access);
    // 3 Create a Locator using the AccessPolicy and Asset
    $locator = new Locator($asset, $access, Locator::TYPE_ON_DEMAND_ORIGIN);
    $locator->setName('Live Locator');
    $locator = $restProxy->createLocator($locator);
    echo "Done!" . PHP_EOL;
    // 4 Return the Smooth Streaming base URL
    return $locator->getPath() . $manifestFile->getName() . '/manifest';
}