/** * batch decideAddEntryFlavor is the decision layer for adding a single flavor conversion to an entry * * @param BatchJob $parentJob * @param int $entryId * @param int $flavorParamsId * @param string $errDescription * @param string $flavorAssetId * @param array<kOperationAttributes> $dynamicAttributes * @return BatchJob */ public static function decideAddEntryFlavor(BatchJob $parentJob = null, $entryId, $flavorParamsId, &$errDescription, $flavorAssetId = null, array $dynamicAttributes = array()) { KalturaLog::log("entryId [{$entryId}], flavorParamsId [{$flavorParamsId}]"); $originalFlavorAsset = assetPeer::retrieveOriginalByEntryId($entryId); if (is_null($originalFlavorAsset)) { $errDescription = 'Original flavor asset not found'; KalturaLog::err($errDescription); return null; } if ($originalFlavorAsset->getId() != $flavorAssetId && !$originalFlavorAsset->isLocalReadyStatus()) { $errDescription = 'Original flavor asset not ready'; KalturaLog::err($errDescription); return null; } // TODO - if source flavor is remote storage, create import job and mark the flavor as FLAVOR_ASSET_STATUS_WAIT_FOR_CONVERT $mediaInfoId = null; $mediaInfo = mediaInfoPeer::retrieveByFlavorAssetId($originalFlavorAsset->getId()); if ($mediaInfo) { $mediaInfoId = $mediaInfo->getId(); } $flavorParams = assetParamsPeer::retrieveByPK($flavorParamsId); if (!$flavorParams) { KalturaLog::err("Flavor Params Id [{$flavorParamsId}] not found"); return null; } $flavorParams->setDynamicAttributes($dynamicAttributes); $flavor = self::validateFlavorAndMediaInfo($flavorParams, $mediaInfo, $errDescription); if (is_null($flavor)) { KalturaLog::err("Failed to validate media info [{$errDescription}]"); return null; } if ($parentJob) { // prefer the partner id from the parent job, although it should be the same $partnerId = $parentJob->getPartnerId(); } else { $partnerId = $originalFlavorAsset->getPartnerId(); } if (is_null($flavorAssetId)) { $flavorAsset = assetPeer::retrieveByEntryIdAndParams($entryId, $flavorParamsId); if ($flavorAsset) { $flavorAssetId = $flavorAsset->getId(); } } $srcSyncKey = $originalFlavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); $flavor->_force = true; // force to convert the flavor, even if none complied $flavor->setReadyBehavior(flavorParamsConversionProfile::READY_BEHAVIOR_IGNORE); // should not be taken in completion rules check $conversionProfile = myPartnerUtils::getConversionProfile2ForEntry($entryId); if ($conversionProfile) { $flavorParamsConversionProfile = flavorParamsConversionProfilePeer::retrieveByFlavorParamsAndConversionProfile($flavor->getFlavorParamsId(), $conversionProfile->getId()); if ($flavorParamsConversionProfile) { $flavor->setReadyBehavior($flavorParamsConversionProfile->getReadyBehavior()); } } $flavorAsset = kBatchManager::createFlavorAsset($flavor, $partnerId, $entryId, $flavorAssetId); if (!$flavorAsset) { KalturaLog::err("Failed to create flavor asset"); return null; } $flavorAssetId = $flavorAsset->getId(); $collectionTag = $flavor->getCollectionTag(); if ($collectionTag) { $entry = entryPeer::retrieveByPK($entryId); if (!$entry) { throw new APIException(APIErrors::INVALID_ENTRY, $parentJob, $entryId); } $dbConvertCollectionJob = null; if ($parentJob) { $dbConvertCollectionJob = $parentJob->createChild(false); $dbConvertCollectionJob->setEntryId($entryId); $dbConvertCollectionJob->save(); } $flavorAssets = assetPeer::retrieveFlavorsByEntryId($entryId); $flavorAssets = assetPeer::filterByTag($flavorAssets, $collectionTag); $flavors = array(); foreach ($flavorAssets as $tagedFlavorAsset) { if ($tagedFlavorAsset->getStatus() == flavorAsset::FLAVOR_ASSET_STATUS_NOT_APPLICABLE || $tagedFlavorAsset->getStatus() == flavorAsset::FLAVOR_ASSET_STATUS_DELETED) { continue; } $flavorParamsOutput = assetParamsOutputPeer::retrieveByAssetId($tagedFlavorAsset->getId()); if (is_null($flavorParamsOutput)) { KalturaLog::log("Creating flavor params output for asset [" . $tagedFlavorAsset->getId() . "]"); $flavorParams = assetParamsPeer::retrieveByPK($tagedFlavorAsset->getId()); $flavorParamsOutput = self::validateFlavorAndMediaInfo($flavorParams, $mediaInfo, $errDescription); if (is_null($flavorParamsOutput)) { KalturaLog::err("Failed to validate media info [{$errDescription}]"); continue; } } if ($flavorParamsOutput) { KalturaLog::log("Adding Collection flavor [" . $flavorParamsOutput->getId() . "] for asset [" . $tagedFlavorAsset->getId() . "]"); $flavors[$tagedFlavorAsset->getId()] = assetParamsOutputPeer::retrieveByAssetId($tagedFlavorAsset->getId()); } } if ($flavorAssetId) { KalturaLog::log("Updating Collection flavor [" . $flavor->getId() . "] for asset [" . $tagedFlavorAsset->getId() . "]"); $flavors[$flavorAssetId] = $flavor; } switch ($collectionTag) { case flavorParams::TAG_ISM: KalturaLog::log("Calling addConvertIsmCollectionJob with [" . count($flavors) . "] flavor params"); return kJobsManager::addConvertIsmCollectionJob($collectionTag, $srcSyncKey, $entry, $parentJob, $flavors, $dbConvertCollectionJob); default: KalturaLog::log("Error: Invalid collection tag [{$collectionTag}]"); return null; } } $dbConvertFlavorJob = null; if ($parentJob) { $dbConvertFlavorJob = $parentJob->createChild(false); $dbConvertFlavorJob->setEntryId($entryId); $dbConvertFlavorJob->save(); } return kJobsManager::addFlavorConvertJob($srcSyncKey, $flavor, $flavorAsset->getId(), $mediaInfoId, $parentJob, null, $dbConvertFlavorJob); }
/** * batch decideAddEntryFlavor is the decision layer for adding a single flavor conversion to an entry * * @param BatchJob $parentJob * @param int $entryId * @param int $flavorParamsId * @param string $errDescription * @param string $flavorAssetId * @param array<kOperationAttributes> $dynamicAttributes * @return BatchJob */ public static function decideAddEntryFlavor(BatchJob $parentJob = null, $entryId, $flavorParamsId, &$errDescription, $flavorAssetId = null, array $dynamicAttributes = array(), $priority = 0) { KalturaLog::log("entryId [{$entryId}], flavorParamsId [{$flavorParamsId}]"); $originalFlavorAsset = assetPeer::retrieveOriginalByEntryId($entryId); if (is_null($originalFlavorAsset)) { $errDescription = 'Original flavor asset not found'; KalturaLog::err($errDescription); return null; } if ($originalFlavorAsset->getId() != $flavorAssetId && !$originalFlavorAsset->isLocalReadyStatus()) { $errDescription = 'Original flavor asset not ready'; KalturaLog::err($errDescription); return null; } // TODO - if source flavor is remote storage, create import job and mark the flavor as FLAVOR_ASSET_STATUS_WAIT_FOR_CONVERT $mediaInfoId = null; $mediaInfo = mediaInfoPeer::retrieveByFlavorAssetId($originalFlavorAsset->getId()); if ($mediaInfo) { $mediaInfoId = $mediaInfo->getId(); } $flavorParams = assetParamsPeer::retrieveByPK($flavorParamsId); if (!$flavorParams) { KalturaLog::err("Flavor Params Id [{$flavorParamsId}] not found"); return null; } $flavorParams->setDynamicAttributes($dynamicAttributes); self::adjustAssetParams($entryId, array($flavorParams)); $flavor = self::validateFlavorAndMediaInfo($flavorParams, $mediaInfo, $errDescription); if (is_null($flavor)) { KalturaLog::err("Failed to validate media info [{$errDescription}]"); return null; } if ($parentJob) { // prefer the partner id from the parent job, although it should be the same $partnerId = $parentJob->getPartnerId(); } else { $partnerId = $originalFlavorAsset->getPartnerId(); } if (is_null($flavorAssetId)) { $flavorAsset = assetPeer::retrieveByEntryIdAndParams($entryId, $flavorParamsId); if ($flavorAsset) { $flavorAssetId = $flavorAsset->getId(); } } $flavor->_force = true; // force to convert the flavor, even if none complied $conversionProfile = myPartnerUtils::getConversionProfile2ForEntry($entryId); if ($conversionProfile) { $flavorParamsConversionProfile = flavorParamsConversionProfilePeer::retrieveByFlavorParamsAndConversionProfile($flavor->getFlavorParamsId(), $conversionProfile->getId()); if ($flavorParamsConversionProfile) { $flavor->setReadyBehavior($flavorParamsConversionProfile->getReadyBehavior()); } } $flavorAsset = kBatchManager::createFlavorAsset($flavor, $partnerId, $entryId, $flavorAssetId); if (!$flavorAsset) { return null; } if (!$flavorAsset->getIsOriginal()) { $flavor->setReadyBehavior(flavorParamsConversionProfile::READY_BEHAVIOR_IGNORE); } // should not be taken in completion rules check $flavorAssetId = $flavorAsset->getId(); $collectionTag = $flavor->getCollectionTag(); /* * CHANGE: collection porcessing only for ExpressionEncoder jobs * to allow FFmpeg/ISMV processing */ KalturaLog::log("Check for collection case - asset(" . $flavorAssetId . "),engines(" . $flavor->getConversionEngines() . ")"); if ($collectionTag && $flavor->getConversionEngines() == conversionEngineType::EXPRESSION_ENCODER3) { $entry = entryPeer::retrieveByPK($entryId); if (!$entry) { throw new APIException(APIErrors::INVALID_ENTRY, $parentJob, $entryId); } $flavorAssets = assetPeer::retrieveFlavorsByEntryId($entryId); $flavorAssets = assetPeer::filterByTag($flavorAssets, $collectionTag); $flavors = array(); foreach ($flavorAssets as $tagedFlavorAsset) { $errDescription = null; if ($tagedFlavorAsset->getStatus() == flavorAsset::FLAVOR_ASSET_STATUS_NOT_APPLICABLE || $tagedFlavorAsset->getStatus() == flavorAsset::FLAVOR_ASSET_STATUS_DELETED) { continue; } $flavorParamsOutput = assetParamsOutputPeer::retrieveByAssetId($tagedFlavorAsset->getId()); if (is_null($flavorParamsOutput)) { KalturaLog::log("Creating flavor params output for asset [" . $tagedFlavorAsset->getId() . "]"); $flavorParams = assetParamsPeer::retrieveByPK($tagedFlavorAsset->getId()); self::adjustAssetParams($entryId, array($flavorParams)); $flavorParamsOutput = self::validateFlavorAndMediaInfo($flavorParams, $mediaInfo, $errDescription); if (is_null($flavorParamsOutput)) { KalturaLog::err("Failed to validate media info [{$errDescription}]"); continue; } } if ($flavorParamsOutput) { KalturaLog::log("Adding Collection flavor [" . $flavorParamsOutput->getId() . "] for asset [" . $tagedFlavorAsset->getId() . "]"); $flavors[$tagedFlavorAsset->getId()] = assetParamsOutputPeer::retrieveByAssetId($tagedFlavorAsset->getId()); } } if ($flavorAssetId) { KalturaLog::log("Updating Collection flavor [" . $flavor->getId() . "] for asset [" . $tagedFlavorAsset->getId() . "]"); $flavors[$flavorAssetId] = $flavor; } return self::decideCollectionConvert($collectionTag, $originalFlavorAsset, $entry, $parentJob, $flavors); } else { return self::decideFlavorConvert($flavorAsset, $flavor, $originalFlavorAsset, $conversionProfile->getId(), $mediaInfoId, $parentJob, null, false, $priority); } }