Пример #1
0
 protected function initFlavorAssetArray()
 {
     if (!$this->shouldInitFlavorAssetsArray()) {
         return;
     }
     $oneOnly = false;
     if ($this->deliveryAttributes->getFormat() == PlaybackProtocol::HTTP || $this->deliveryAttributes->getFormat() == "url" || $this->deliveryAttributes->getFormat() == "rtsp") {
         $oneOnly = true;
     }
     // get initial flavor list by input
     $flavorAssets = array();
     if ($this->flavorIds) {
         $flavorAssets = assetPeer::retrieveReadyByEntryId($this->entryId, $this->flavorIds);
         $flavorAssets = $this->removeNotAllowedFlavors($flavorAssets);
         $flavorAssets = $this->removeMaxBitrateFlavors($flavorAssets);
     }
     if (!$flavorAssets || !count($flavorAssets)) {
         $flavorAssets = assetPeer::retrieveReadyFlavorsByEntryId($this->entryId);
         $flavorAssets = $this->deliveryAttributes->filterFlavorsByTags($flavorAssets);
         $flavorAssets = $this->removeNotAllowedFlavors($flavorAssets);
         $flavorAssets = $this->removeMaxBitrateFlavors($flavorAssets);
     }
     if ($this->deliveryAttributes->getFormat() == PlaybackProtocol::SILVER_LIGHT) {
         $this->initSilverLightManifest($flavorAssets);
         return;
     }
     if ($this->deliveryAttributes->getFormat() == PlaybackProtocol::HDS || $this->deliveryAttributes->getFormat() == PlaybackProtocol::APPLE_HTTP) {
         // try to look for a smil manifest, if it was found, we will use it for hds and hls
         if ($this->initSmilManifest($flavorAssets)) {
             return;
         }
     }
     // get flavors availability
     $servePriority = $this->entry->getPartner()->getStorageServePriority();
     $localFlavors = array();
     $remoteFlavorsByDc = array();
     $remoteFileSyncs = array();
     foreach ($flavorAssets as $flavorAsset) {
         $flavorId = $flavorAsset->getId();
         $key = $flavorAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET);
         $c = new Criteria();
         $c = FileSyncPeer::getCriteriaForFileSyncKey($key);
         $c->addAnd(FileSyncPeer::STATUS, FileSync::FILE_SYNC_STATUS_READY);
         switch ($servePriority) {
             case StorageProfile::STORAGE_SERVE_PRIORITY_KALTURA_ONLY:
                 $c->addAnd(FileSyncPeer::FILE_TYPE, FileSync::FILE_SYNC_FILE_TYPE_URL, Criteria::NOT_EQUAL);
                 break;
             case StorageProfile::STORAGE_SERVE_PRIORITY_EXTERNAL_ONLY:
                 $c->add(FileSyncPeer::FILE_TYPE, FileSync::FILE_SYNC_FILE_TYPE_URL);
                 break;
         }
         if ($this->deliveryAttributes->getStorageId()) {
             $c->addAnd(FileSyncPeer::DC, $this->deliveryAttributes->getStorageId());
         }
         $fileSyncs = FileSyncPeer::doSelect($c);
         foreach ($fileSyncs as $fileSync) {
             if ($fileSync->getFileType() == FileSync::FILE_SYNC_FILE_TYPE_URL) {
                 $dc = $fileSync->getDc();
                 $remoteFlavorsByDc[$dc][$flavorId] = $flavorAsset;
                 $remoteFileSyncs[$dc][$flavorId] = $fileSync;
             } else {
                 $localFlavors[$flavorId] = $flavorAsset;
             }
         }
     }
     // filter out any invalid / disabled storage profiles
     if ($remoteFileSyncs) {
         $storageProfileIds = array_keys($remoteFileSyncs);
         $storageProfiles = StorageProfilePeer::retrieveExternalByPartnerId($this->entry->getPartnerId(), $storageProfileIds);
         $activeStorageProfileIds = array();
         foreach ($storageProfiles as $storageProfile) {
             $activeStorageProfileIds[] = $storageProfile->getId();
         }
         foreach ($storageProfileIds as $storageProfileId) {
             if (in_array($storageProfileId, $activeStorageProfileIds)) {
                 continue;
             }
             unset($remoteFlavorsByDc[$storageProfileId]);
             unset($remoteFileSyncs[$storageProfileId]);
         }
     }
     // choose the storage profile with the highest number of flavors
     $maxDc = null;
     $maxDcFlavorCount = 0;
     $remoteFlavors = array();
     foreach ($remoteFlavorsByDc as $dc => $curDcFlavors) {
         $curDcFlavorCount = count($curDcFlavors);
         if ($curDcFlavorCount <= $maxDcFlavorCount) {
             continue;
         }
         $maxDc = $dc;
         $maxDcFlavorCount = $curDcFlavorCount;
         $remoteFlavors = $curDcFlavors;
     }
     // choose the flavor set according to the serve priority
     if ($this->shouldUseLocalFlavors($localFlavors, $remoteFlavors)) {
         $this->deliveryAttributes->setStorageId(null);
         $this->deliveryAttributes->setFlavorAssets($localFlavors);
     } else {
         if ($maxDc) {
             $this->deliveryAttributes->setStorageId($maxDc);
             $this->deliveryAttributes->setFlavorAssets($remoteFlavors);
             $this->deliveryAttributes->setRemoteFileSyncs($remoteFileSyncs[$maxDc]);
         }
     }
     if (!$this->deliveryAttributes->getFlavorAssets()) {
         KExternalErrors::dieError(KExternalErrors::FLAVOR_NOT_FOUND);
     }
     if ($oneOnly) {
         $flavorAssets = $this->deliveryAttributes->getFlavorAssets();
         $this->deliveryAttributes->setFlavorAssets(array(reset($flavorAssets)));
     }
 }