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); }
/** * Create asset from array * * @param array $options Array containing values for object properties * * @return WindowsAzure\MediaServices\Models\Job */ public static function createFromOptions($options) { $job = new Job(); $job->fromArray($options); return $job; }
/** * Get list of Jobs. * * @return array of Models\Job */ public function getJobList() { $propertyList = $this->_getEntityList('Jobs'); $result = array(); foreach ($propertyList as $properties) { $result[] = Job::createFromOptions($properties); } return $result; }
/** * @covers WindowsAzure\MediaServices\Models\Job::getState */ public function testGetState() { // Setup $job = new Job(); $options = array('State' => Job::STATE_QUEUED); $job = Job::createFromOptions($options); // Test $result = $job->getState(); // Assert $this->assertEquals($options['State'], $result); }
/** * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::getAssetParentAssets */ public function testGetAssetParentAsset() { // Setup $name = $this->getOutputAssetName(); $mediaProcessor = $this->restProxy->getLatestMediaProcessor('Windows Azure Media Encoder'); $inputAsset = $this->createAssetWithFile(); $taskBody = '<?xml version="1.0" encoding="utf-8"?><taskBody><inputAsset>JobInputAsset(0)</inputAsset><outputAsset assetCreationOptions="0" assetName="' . $name . '">JobOutputAsset(0)</outputAsset></taskBody>'; $task = new Task($taskBody, $mediaProcessor->getId(), TaskOptions::NONE); $task->setConfiguration('H.264 HD 720p VBR'); $job = new Job(); $job->setName($name); $job = $this->createJob($job, array($inputAsset), array($task)); $assetList = $this->restProxy->getAssetList(); // Test foreach ($assetList as $assetElement) { if (strcmp($assetElement->getName(), $name) == 0) { $parentAssetId = $this->restProxy->getAssetParentAssets($assetElement); } } // Assert $this->assertEquals(1, count($parentAssetId)); $this->assertEquals($inputAsset->getId(), $parentAssetId[0]->getId()); }
/** * Add convert job to Microsoft Azure Media Services * @param int $sourceAssetId Asset Id of source file * @param array $preset Preset for encoding * @return int|false Job Id or error */ public function addConvertJob($sourceAssetId, $preset) { if (!is_array($preset)) { return false; } $mediaProcessor = $this->_mediaServiceProxy->getLatestMediaProcessor(self::MEDIA_PROCESSOR_NAME); $sourceAsset = $this->getAssetById($sourceAssetId); if (!$sourceAsset) { return false; } $entryName = $this->getEntryName($sourceAssetId); if ($entryName) { $assetName = $entryName . ' - ' . $preset[self::CONVERT_PARAM_PRESET_NAME]; } else { $assetName = $sourceAsset->getName() . ' - ' . $preset[self::CONVERT_PARAM_PRESET_NAME]; } $confParams = array(); $confParams[self::CONF_PARAM_AUDIO_BITRATE] = $preset[self::CONVERT_PARAM_AUDIO_BITRATE]; $confParams[self::CONF_PARAM_AUDIO_CHANNELS] = $preset[self::CONVERT_PARAM_AUDIO_CHANNELS]; $confParams[self::CONF_PARAM_AUDIO_CODEC] = $this->audioCodecs[$preset[self::CONVERT_PARAM_AUDIO_CODEC]]; $confParams[self::CONF_PARAM_AUDIO_SAMPLE_RATE] = $preset[self::CONVERT_PARAM_AUDIO_SAMPLE_RATE]; $confParams[self::CONF_PARAM_VIDEO_PROFILE_NAME] = $this->videoProfiles[$preset[self::CONVERT_PARAM_VIDEO_CODEC]]; $confParams[self::CONF_PARAM_VIDEO_WIDTH] = $preset[self::CONVERT_PARAM_VIDEO_WIDTH]; $confParams[self::CONF_PARAM_VIDEO_HEIGHT] = $preset[self::CONVERT_PARAM_VIDEO_HEIGHT]; $confParams[self::CONF_PARAM_VIDEO_BITRATE] = $preset[self::CONVERT_PARAM_VIDEO_BITRATE]; $configuration = $this->renderConfiguration($this->confPath . self::TEMPLATE_FILE_CONVERT, $confParams); $taskParams = array(); $taskParams[self::CONF_PARAM_ASSET_NAME] = $assetName; $taskBody = $this->renderConfiguration($this->confPath . self::TEMPLATE_FILE_TASK_BODY, $taskParams); $task = new Task($taskBody, $mediaProcessor->getId(), 0); $task->setConfiguration($configuration); $job = new Job(); $job->setName('Converting ' . $sourceAsset->getName() . ' to ' . $preset[self::CONVERT_PARAM_PRESET_NAME]); return $this->_mediaServiceProxy->createJob($job, array($sourceAsset), array($task))->getId(); }
/** * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::createAsset * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::createJob * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::getJobList * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::getJobInputMediaAssets * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::getJobOutputMediaAssets */ public function testListAllJobsAndAssets() { $asset1 = $this->createAssetWithFile(); $outputAssetName1 = $this->getOutputAssetName(); $asset2 = $this->createAssetWithFile(); $outputAssetName2 = $this->getOutputAssetName(); $taskBody1 = '<?xml version="1.0" encoding="utf-8"?><taskBody><inputAsset>JobInputAsset(0)</inputAsset><outputAsset assetCreationOptions="0" assetName="' . $outputAssetName1 . '">JobOutputAsset(0)</outputAsset></taskBody>'; $mediaProcessorId = 'nb:mpid:UUID:2e7aa8f3-4961-4e0c-b4db-0e0439e524f5'; $task1 = new Task($taskBody1, $mediaProcessorId, TaskOptions::NONE); $task1->setConfiguration('H.264 HD 720p VBR'); $taskBody2 = '<?xml version="1.0" encoding="utf-8"?><taskBody><inputAsset>JobInputAsset(0)</inputAsset><outputAsset assetCreationOptions="0" assetName="' . $outputAssetName2 . '">JobOutputAsset(0)</outputAsset></taskBody>'; $task2 = new Task($taskBody2, $mediaProcessorId, TaskOptions::NONE); $task2->setConfiguration('H.264 HD 720p VBR'); $job1 = new Job(); $job1->setName(TestResources::MEDIA_SERVICES_JOB_NAME . $this->createSuffix()); $jobResult1 = $this->createJob($job1, array($asset1), array($task1)); $job2 = new Job(); $job2->setName(TestResources::MEDIA_SERVICES_JOB_NAME . $this->createSuffix()); $jobResult2 = $this->createJob($job2, array($asset2), array($task2)); // Test $jobList = $this->restProxy->getJobList(); $resultInput1 = $this->restProxy->getJobInputMediaAssets($jobList[0]); $resultOutput1 = $this->restProxy->getJobOutputMediaAssets($jobList[0]); $resultInput2 = $this->restProxy->getJobInputMediaAssets($jobList[1]); $resultOutput2 = $this->restProxy->getJobOutputMediaAssets($jobList[1]); // Assert $this->assertEquals(2, count($resultInput1) + count($resultInput2)); $this->assertEquals(2, count($resultOutput1) + count($resultOutput2)); $this->assertEquals(2, count($jobList)); $this->assertEquals($asset1->getId(), $resultInput2[0]->getId()); $this->assertEquals($asset1->getName(), $resultInput2[0]->getName()); $this->assertNotEquals($asset1->getId(), $resultOutput2[0]->getId()); $this->assertEquals($outputAssetName1, $resultOutput2[0]->getName()); $this->assertEquals($asset2->getId(), $resultInput1[0]->getId()); $this->assertEquals($asset2->getName(), $resultInput1[0]->getName()); $this->assertNotEquals($asset2->getId(), $resultOutput1[0]->getId()); $this->assertEquals($outputAssetName2, $resultOutput1[0]->getName()); }
public function createJobWithTasks($name) { $mediaProcessor = $this->restProxy->getLatestMediaProcessor(TestResources::MEDIA_SERVICES_PROCESSOR_NAME); $inputAsset = $this->createAssetWithFile(); $taskBody = TestResources::getMediaServicesTask($this->getOutputAssetName()); $task = new Task($taskBody, $mediaProcessor->getId(), TaskOptions::NONE); $task->setConfiguration(TestResources::MEDIA_SERVICES_TASK_COFIGURATION); $job = new Job(); $job->setName($name); $jobResult = $this->createJob($job, array($inputAsset), array($task)); $this->job[$jobResult->getId()] = $jobResult; return $jobResult; }
function encodeToAdaptiveBitrateMP4Set($restProxy, $asset) { // 2.1 retrieve the latest 'Media Encoder Standard' processor version $mediaProcessor = $restProxy->getLatestMediaProcessor('Media Encoder Standard'); echo "Using Media Processor: {$mediaProcessor->getName()} version {$mediaProcessor->getVersion()}" . PHP_EOL; // 2.2 Create the Job; this automatically schedules and runs it $outputAssetName = "Encoded " . $asset->getName(); $outputAssetCreationOption = Asset::OPTIONS_NONE; $taskBody = '<?xml version="1.0" encoding="utf-8"?><taskBody><inputAsset>JobInputAsset(0)</inputAsset><outputAsset assetCreationOptions="' . $outputAssetCreationOption . '" assetName="' . $outputAssetName . '">JobOutputAsset(0)</outputAsset></taskBody>'; $task = new Task($taskBody, $mediaProcessor->getId(), TaskOptions::NONE); $task->setConfiguration('H264 Multiple Bitrate 720p'); $job = new Job(); $job->setName('Encoding Job'); $job = $restProxy->createJob($job, array($asset), array($task)); echo "Created Job with Id: {$job->getId()}" . PHP_EOL; // 2.3 Check to see if the Job has completed $result = $restProxy->getJobStatus($job); $jobStatusMap = array('Queued', 'Scheduled', 'Processing', 'Finished', 'Error', 'Canceled', 'Canceling'); while ($result != Job::STATE_FINISHED && $result != Job::STATE_ERROR && $result != Job::STATE_CANCELED) { echo "Job status: {$jobStatusMap[$result]}" . PHP_EOL; sleep(5); $result = $restProxy->getJobStatus($job); } if ($result != Job::STATE_FINISHED) { echo "The job has finished with a wrong status: {$jobStatusMap[$result]}" . PHP_EOL; exit(-1); } echo "Job Finished!" . PHP_EOL; // 2.4 Get output asset $outputAssets = $restProxy->getJobOutputMediaAssets($job); $encodedAsset = $outputAssets[0]; echo "Asset encoded: name={$encodedAsset->getName()} id={$encodedAsset->getId()}" . PHP_EOL; return $encodedAsset; }
// 1.7. notify Media Services that the file upload operation is done to generate the asset file metadata $restProxy->createFileInfos($asset); print "File uploaded: size=" . strlen($fileContent) . "\r\n"; // 1.8. delete the SAS Locator (and Access Policy) for the Asset since we are done uploading files $restProxy->deleteLocator($sasLocator); $restProxy->deleteAccessPolicy($accessPolicy); // Phase 2 - encode the output asset // 2.1 retrieve the last Media Encoder Standard $mediaProcessor = $restProxy->getLatestMediaProcessor('Media Encoder Standard'); print "Using Media Processor: {$mediaProcessor->getName()} version {$mediaProcessor->getVersion()}\r\n"; // 2.2 Create the Job; this automatically schedules and runs it. $outputAssetName = "Encoded " . $mezzanineFileName; $taskBody = '<?xml version="1.0" encoding="utf-8"?><taskBody><inputAsset>JobInputAsset(0)</inputAsset><outputAsset assetCreationOptions="0" assetName="' . $outputAssetName . '">JobOutputAsset(0)</outputAsset></taskBody>'; $task = new Task($taskBody, $mediaProcessor->getId(), TaskOptions::NONE); $task->setConfiguration('H264 Multiple Bitrate 720p'); $job = new Job(); $job->setName('Encoding Job'); $job = $restProxy->createJob($job, array($asset), array($task)); print "Created Job with Id: {$job->getId()}\r\n"; // 2.3 Check to see if the Job has completed $result = $restProxy->getJobStatus($job); while ($result != 3 && $result != 4 && $result != 5) { print "Job status: {$result}\r\n"; sleep(5); $result = $restProxy->getJobStatus($job); } if ($result != 3) { print "The job has finished with a wrong status: {$result}\r\n"; return; } print "Job Finished!\r\n";