Esempio n. 1
0
 /**
  * addConvertIsmCollectionJob creates a convert collection job 
  * 
  * @param string $tag 
  * @param FileSyncKey $srcSyncKey
  * @param entry $entry
  * @param BatchJob $parentJob
  * @param array<flavorParamsOutput> $flavorParamsOutputs
  * @return BatchJob
  */
 public static function addConvertIsmCollectionJob($tag, FileSyncKey $srcSyncKey, entry $entry, BatchJob $parentJob = null, array $flavorParamsOutputs, $sameRoot = null)
 {
     list($fileSync, $local) = kFileSyncUtils::getReadyFileSyncForKey($srcSyncKey, true, false);
     $srcFileSyncDescriptor = new kSourceFileSyncDescriptor();
     if ($fileSync) {
         if ($fileSync->getFileType() != FileSync::FILE_SYNC_FILE_TYPE_URL) {
             $srcFileSyncDescriptor->setFileSyncLocalPath($fileSync->getFullPath());
         }
         $srcFileSyncDescriptor->setFileSyncRemoteUrl($fileSync->getExternalUrl($entry->getId()));
         $srcFileSyncDescriptor->setAssetId($fileSync->getObjectId());
         $srcFileSyncDescriptor->setFileSyncObjectSubType($srcSyncKey->getObjectSubType());
     }
     // increment entry version
     $ismVersion = $entry->incrementIsmVersion();
     $entry->save();
     $fileName = $entry->generateFileName(0, $ismVersion);
     // creates convert data
     $convertCollectionData = new kConvertCollectionJobData();
     $convertCollectionData->setSrcFileSyncs(array($srcFileSyncDescriptor));
     $convertCollectionData->setDestFileName($fileName);
     $clipOffset = null;
     $clipDuration = null;
     // look for clipping params
     foreach ($flavorParamsOutputs as $flavorParamsOutput) {
         $clipOffset = $flavorParamsOutput->getClipOffset();
         $clipDuration = $flavorParamsOutput->getClipDuration();
         if (isset($clipOffset) || isset($clipDuration)) {
             KalturaLog::log("Found clipping params: clipOffset({$clipOffset}),clipDuration({$clipDuration})");
             break;
         }
     }
     // check bitrates duplications & update clipping params
     foreach ($flavorParamsOutputs as $flavorParamsOutputIndex => $flavorParamsOutput) {
         // if one of clip params exsits - update the object and db
         if (isset($clipOffset)) {
             $flavorParamsOutputs[$flavorParamsOutputIndex]->setClipOffset($clipOffset);
         }
         if (isset($clipDuration)) {
             $flavorParamsOutputs[$flavorParamsOutputIndex]->setClipDuration($clipDuration);
         }
         if (isset($clipOffset) || isset($clipDuration)) {
             $flavorParamsOutputs[$flavorParamsOutputIndex]->save();
         }
     }
     /*
      * Put together all separted flavor XML's into a single Smooth Streaming preset file
      */
     KalturaLog::log("Calling CDLProceessFlavorsForCollection with [" . count($flavorParamsOutputs) . "] flavor params");
     $presetXml = KDLWrap::CDLProceessFlavorsForCollection($flavorParamsOutputs);
     $presetXml = str_replace(KDLCmdlinePlaceholders::OutFileName, $fileName, $presetXml);
     foreach ($flavorParamsOutputs as $flavorParamsOutput) {
         /*
          * Save in case that videoBitrate was changed by the FlavorsForCollection (see above)
          */
         $flavorParamsOutput->save();
         $convertCollectionFlavorData = new kConvertCollectionFlavorData();
         $convertCollectionFlavorData->setFlavorAssetId($flavorParamsOutput->getFlavorAssetId());
         $convertCollectionFlavorData->setFlavorParamsOutputId($flavorParamsOutput->getId());
         $convertCollectionFlavorData->setReadyBehavior($flavorParamsOutput->getReadyBehavior());
         $convertCollectionFlavorData->setVideoBitrate($flavorParamsOutput->getVideoBitrate());
         $convertCollectionFlavorData->setAudioBitrate($flavorParamsOutput->getAudioBitrate());
         $convertCollectionFlavorData->setAudioBitrate($flavorParamsOutput->getAudioBitrate());
         $convertCollectionData->addFlavor($convertCollectionFlavorData);
     }
     $currentConversionEngine = conversionEngineType::EXPRESSION_ENCODER3;
     KalturaLog::log("Using conversion engine [{$currentConversionEngine}]");
     if ($sameRoot == null) {
         // creats a child convert job
         if ($parentJob) {
             $dbConvertCollectionJob = $parentJob->createChild(BatchJobType::CONVERT_COLLECTION, $currentConversionEngine);
             KalturaLog::log("Created from parent convert job with entry id [" . $dbConvertCollectionJob->getEntryId() . "]");
         } else {
             $dbConvertCollectionJob = new BatchJob();
             $dbConvertCollectionJob->setEntryId($entry->getId());
             $dbConvertCollectionJob->setPartnerId($entry->getPartnerId());
             $dbConvertCollectionJob->setJobType(BatchJobType::CONVERT_COLLECTION);
             $dbConvertCollectionJob->setJobSubType($currentConversionEngine);
         }
     } else {
         $dbConvertCollectionJob = $parentJob->createChild(BatchJobType::CONVERT_COLLECTION, $currentConversionEngine, false);
     }
     $dbConvertCollectionJob->setObjectId($entry->getId());
     $dbConvertCollectionJob->setObjectType(BatchJobObjectType::ENTRY);
     $dbConvertCollectionJob->setStatus(BatchJob::BATCHJOB_STATUS_DONT_PROCESS);
     $dbConvertCollectionJob = kJobsManager::addJob($dbConvertCollectionJob, $convertCollectionData, BatchJobType::CONVERT_COLLECTION, $currentConversionEngine);
     $syncKey = $dbConvertCollectionJob->getSyncKey(BatchJob::FILE_SYNC_BATCHJOB_SUB_TYPE_CONFIG);
     kFileSyncUtils::file_put_contents($syncKey, $presetXml);
     $fileSync = kFileSyncUtils::getLocalFileSyncForKey($syncKey);
     $remoteUrl = $fileSync->getExternalUrl($entry->getId());
     $localPath = kFileSyncUtils::getLocalFilePathForKey($syncKey);
     $commandLines = array(conversionEngineType::EXPRESSION_ENCODER3 => KDLCmdlinePlaceholders::InFileName . ' ' . KDLCmdlinePlaceholders::ConfigFileName);
     $commandLinesStr = flavorParamsOutput::buildCommandLinesStr($commandLines);
     $convertCollectionData->setInputXmlLocalPath($localPath);
     $convertCollectionData->setInputXmlRemoteUrl($remoteUrl);
     $convertCollectionData->setCommandLinesStr($commandLinesStr);
     $dbConvertCollectionJob->setData($convertCollectionData);
     return kJobsManager::updateBatchJob($dbConvertCollectionJob, BatchJob::BATCHJOB_STATUS_PENDING);
 }
 /**
  * addConvertIsmCollectionJob creates a convert collection job 
  * 
  * @param string $tag 
  * @param FileSyncKey $srcSyncKey
  * @param entry $entry
  * @param BatchJob $parentJob
  * @param array<flavorParamsOutput> $flavorParamsOutputs
  * @return BatchJob
  */
 public static function addConvertIsmCollectionJob($tag, FileSyncKey $srcSyncKey, entry $entry, BatchJob $parentJob = null, array $flavorParamsOutputs, $dbConvertCollectionJob = null)
 {
     list($fileSync, $local) = kFileSyncUtils::getReadyFileSyncForKey($srcSyncKey, true, false);
     $localPath = null;
     $remoteUrl = null;
     if ($fileSync) {
         if ($fileSync->getFileType() != FileSync::FILE_SYNC_FILE_TYPE_URL) {
             $localPath = $fileSync->getFullPath();
         }
         $remoteUrl = $fileSync->getExternalUrl($entry->getId());
     }
     // increment entry version
     $ismVersion = $entry->incrementIsmVersion();
     $entry->save();
     $fileName = $entry->generateFileName(0, $ismVersion);
     // creates convert data
     $convertCollectionData = new kConvertCollectionJobData();
     $convertCollectionData->setSrcFileSyncLocalPath($localPath);
     $convertCollectionData->setSrcFileSyncRemoteUrl($remoteUrl);
     $convertCollectionData->setDestFileName($fileName);
     $clipOffset = null;
     $clipDuration = null;
     // look for clipping params
     foreach ($flavorParamsOutputs as $flavorParamsOutput) {
         $clipOffset = $flavorParamsOutput->getClipOffset();
         $clipDuration = $flavorParamsOutput->getClipDuration();
         if (isset($clipOffset) || isset($clipDuration)) {
             KalturaLog::log("Found clipping params: clipOffset({$clipOffset}),clipDuration({$clipDuration})");
             break;
         }
     }
     $bitrates = array();
     $finalFlavorParamsOutputs = array();
     // check bitrates duplications & update clipping params
     foreach ($flavorParamsOutputs as $flavorParamsOutputIndex => $flavorParamsOutput) {
         if (!isset($bitrates[$flavorParamsOutput->getVideoBitrate()])) {
             $bitrates[$flavorParamsOutput->getVideoBitrate()] = array();
         }
         // if one of clip params exsits - update the object and db
         if (isset($clipOffset)) {
             $flavorParamsOutputs[$flavorParamsOutputIndex]->setClipOffset($clipOffset);
         }
         if (isset($clipDuration)) {
             $flavorParamsOutputs[$flavorParamsOutputIndex]->setClipDuration($clipDuration);
         }
         if (isset($clipOffset) || isset($clipDuration)) {
             $flavorParamsOutputs[$flavorParamsOutputIndex]->save();
         }
         $bitrates[$flavorParamsOutput->getVideoBitrate()][] = $flavorParamsOutput->getId();
         $finalFlavorParamsOutputs[$flavorParamsOutput->getId()] = $flavorParamsOutput;
     }
     foreach ($bitrates as $bitrate => $flavorParamsOutputIds) {
         if (count($flavorParamsOutputIds) == 1) {
             // no bitrate dupliaction
             continue;
         }
         $tempFlavorParamsOutputs = array();
         foreach ($flavorParamsOutputIds as $index => $flavorParamsOutputId) {
             $tempFlavorParamsOutputs[] = $finalFlavorParamsOutputs[$flavorParamsOutputId];
         }
         // sort the flavors by height
         usort($tempFlavorParamsOutputs, array('kBusinessConvertDL', 'compareFlavorsByHeight'));
         // increment the bitrate so it will be a bit different for each flavor
         $index = 0;
         foreach ($tempFlavorParamsOutputs as $flavorParamsOutput) {
             $finalFlavorParamsOutputs[$flavorParamsOutput->getId()]->setVideoBitrate($bitrate + $index++);
         }
     }
     foreach ($finalFlavorParamsOutputs as $flavorParamsOutput) {
         $convertCollectionFlavorData = new kConvertCollectionFlavorData();
         $convertCollectionFlavorData->setFlavorAssetId($flavorParamsOutput->getFlavorAssetId());
         $convertCollectionFlavorData->setFlavorParamsOutputId($flavorParamsOutput->getId());
         $convertCollectionFlavorData->setReadyBehavior($flavorParamsOutput->getReadyBehavior());
         $convertCollectionFlavorData->setVideoBitrate($flavorParamsOutput->getVideoBitrate());
         $convertCollectionFlavorData->setAudioBitrate($flavorParamsOutput->getAudioBitrate());
         $convertCollectionFlavorData->setAudioBitrate($flavorParamsOutput->getAudioBitrate());
         $convertCollectionData->addFlavor($convertCollectionFlavorData);
     }
     $currentConversionEngine = conversionEngineType::EXPRESSION_ENCODER3;
     KalturaLog::log("Using conversion engine [{$currentConversionEngine}]");
     if (!$dbConvertCollectionJob) {
         // creats a child convert job
         if ($parentJob) {
             $dbConvertCollectionJob = $parentJob->createChild();
             KalturaLog::log("Created from parent convert job with entry id [" . $dbConvertCollectionJob->getEntryId() . "]");
         } else {
             $dbConvertCollectionJob = new BatchJob();
             $dbConvertCollectionJob->setEntryId($entry->getId());
             $dbConvertCollectionJob->setPartnerId($entry->getPartnerId());
             $dbConvertCollectionJob->save();
             KalturaLog::log("Created from convert collection job with entry id [" . $dbConvertCollectionJob->getEntryId() . "]");
         }
     }
     KalturaLog::log("Calling CDLProceessFlavorsForCollection with [" . count($finalFlavorParamsOutputs) . "] flavor params");
     $xml = KDLWrap::CDLProceessFlavorsForCollection($finalFlavorParamsOutputs);
     $xml = str_replace(KDLCmdlinePlaceholders::OutFileName, $fileName, $xml);
     $syncKey = $dbConvertCollectionJob->getSyncKey(BatchJob::FILE_SYNC_BATCHJOB_SUB_TYPE_CONFIG);
     kFileSyncUtils::file_put_contents($syncKey, $xml);
     $fileSync = kFileSyncUtils::getLocalFileSyncForKey($syncKey);
     $remoteUrl = $fileSync->getExternalUrl($entry->getId());
     $localPath = kFileSyncUtils::getLocalFilePathForKey($syncKey);
     $commandLines = array(conversionEngineType::EXPRESSION_ENCODER3 => KDLCmdlinePlaceholders::InFileName . ' ' . KDLCmdlinePlaceholders::ConfigFileName);
     $commandLinesStr = flavorParamsOutput::buildCommandLinesStr($commandLines);
     $convertCollectionData->setInputXmlLocalPath($localPath);
     $convertCollectionData->setInputXmlRemoteUrl($remoteUrl);
     $convertCollectionData->setCommandLinesStr($commandLinesStr);
     $dbConvertCollectionJob->setFileSize(kFile::fileSize($convertCollectionData->getSrcFileSyncLocalPath()));
     return kJobsManager::addJob($dbConvertCollectionJob, $convertCollectionData, BatchJobType::CONVERT_COLLECTION, $currentConversionEngine);
 }