-
Notifications
You must be signed in to change notification settings - Fork 174
/
kClipManager.php
648 lines (575 loc) · 18.3 KB
/
kClipManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
<?php
/**
* @package server-infra
* @subpackage clipconcat
*/
class kClipManager implements kBatchJobStatusEventConsumer
{
const CLIP_NUMBER = 'clipNumber';
/***
* @param array $dynamicAttributes
* @return bool is clip attribute exist in dynamic attribute
*/
public static function isMultipleClipOperation(array $dynamicAttributes)
{
if (count($dynamicAttributes) <= 1)
{
$dynamicAttribute = reset($dynamicAttributes);
if ($dynamicAttribute instanceof kClipAttributes )
{
$effects = $dynamicAttribute->getEffectArray();
if (!empty($effects))
return true;
}
return false;
}
foreach ($dynamicAttributes as $value)
{
if ($value instanceof kClipAttributes)
{
return true;
}
}
return false;
}
/**
* @param string $sourceEntryId
* @param entry $clipEntry
* @param entry $destEntry
* @param $partnerId
* @param array $operationAttributes
* @param int $priority
*/
public function createParentBatchJob($sourceEntryId,$clipEntry, $destEntry, $partnerId, array $operationAttributes, $priority = 0)
{
$parentJob = new BatchJob();
$parentJob->setPartnerId($partnerId);
$this->setDummyOriginalFlavorAssetReady($clipEntry->getId());
$jobData = new kClipConcatJobData();
$jobData->setDestEntryId($destEntry->getEntryId());
$jobData->setTempEntryId($clipEntry->getEntryId());
//if it is replace(Trim flow) active the copy to destination consumers
$this->fillDestEntry($destEntry,$sourceEntryId, $operationAttributes);
$jobData->setSourceEntryId($sourceEntryId);
$jobData->setPartnerId($partnerId);
$jobData->setPriority($priority);
$jobData->setOperationAttributes($operationAttributes);
kJobsManager::addJob($parentJob,$jobData,BatchJobType::CLIP_CONCAT);
}
/**
* @param BatchJob $batchJob
* @return bool true if should continue to the next consumer
* @throws KalturaErrors
*/
public function updatedJob(BatchJob $batchJob)
{
try
{
if ($batchJob->getJobType() == BatchJobType::CLIP_CONCAT)
{
$this->handleClipConcatParentJob($batchJob);
}
if ($batchJob->getParentJob() && $batchJob->getParentJob()->getJobType() == BatchJobType::CONVERT &&
!$this->concatJobExist($batchJob->getRootJob()))
{
$this->startConcat($batchJob->getRootJob());
}
elseif($batchJob->getParentJob() && $batchJob->getParentJob()->getJobType() == BatchJobType::CONCAT )
{
$this->concatDone($batchJob);
}
}
catch (Exception $ex)
{
KalturaLog::err('Error During Concat Job' . $ex);
return false;
}
return true;
}
/**
* @param BatchJob $batchJob
* @return bool true if the consumer should handle the event
*/
public function shouldConsumeJobStatusEvent(BatchJob $batchJob)
{
if ($batchJob->getJobType() == BatchJobType::CLIP_CONCAT)
{
return true;
}
elseif ($batchJob->getRootJob() && $batchJob->getRootJob()->getJobType() == BatchJobType::CLIP_CONCAT)
{
return $this->areAllClipJobsDone($batchJob);
}
return false;
}
/**
* @param kOperationResource $resource
* @param entry $dbEntry
* @param $operationAttributes
* @param $clipEntry
*/
public function startBatchJob($resource, entry $dbEntry, $operationAttributes, $clipEntry)
{
$internalResource = $resource->getResource();
if ($internalResource instanceof kFileSyncResource && $internalResource->getOriginEntryId()) {
$this->createParentBatchJob($internalResource->getOriginEntryId(), $clipEntry, $dbEntry, $dbEntry->getPartnerId(), $operationAttributes);
} else {
$this->createParentBatchJob(null, $clipEntry, $dbEntry, $dbEntry->getPartnerId(), $operationAttributes);
}
}
/***
* @param kClipAttributes $singleAttribute
* @param string $originalConversionEnginesExtraParams
* @param string $encryptionKey
* @return int
* @throws kCoreException
*/
private function cloneFlavorParam($singleAttribute, $originalConversionEnginesExtraParams, $encryptionKey = null)
{
$flavorParamsObj = assetParamsPeer::getTempAssetParamByPk(kClipAttributes::SYSTEM_DEFAULT_FLAVOR_PARAMS_ID);
$flavorParamsObj->setFormat(flavorParams::CONTAINER_FORMAT_MPEGTS);
$this->fixConversionParam($flavorParamsObj, $singleAttribute, $originalConversionEnginesExtraParams);
if ($encryptionKey)
{
$flavorParamsObj->setIsEncrypted(true);
}
assetParamsPeer::addInstanceToPool($flavorParamsObj);
return $flavorParamsObj->getId();
}
/**
* @param string $entryId
* @return flavorAsset
*/
private function setDummyOriginalFlavorAssetReady($entryId)
{
$flavorAsset = assetPeer::retrieveOriginalByEntryId($entryId);
//set Dummy Ready we will update it later
$flavorAsset->setStatus(flavorAsset::ASSET_STATUS_READY);
$flavorAsset->save();
return $flavorAsset;
}
/**
* @param int $partnerId
* @param string $entryId
* @param $flavorParamId
* @param int $order
* @return flavorAsset
*/
private function createTempClipFlavorAsset($partnerId, $entryId, $flavorParamId,$order)
{
$entry = entryPeer::retrieveByPK($entryId);
if(!$entry)
{
KalturaLog::err("Entry [$entryId] not found");
return null;
}
// creates the flavor asset
$flavorAsset = flavorAsset::getInstance();
$flavorAsset->setStatus(flavorAsset::ASSET_STATUS_QUEUED);
$flavorAsset->incrementVersion();
$flavorAsset->addTags(array(flavorParams::TAG_TEMP_CLIP));
$flavorAsset->setIsOriginal(false);
$flavorAsset->setFlavorParamsId($flavorParamId);
$flavorAsset->setPartnerId($partnerId);
$flavorAsset->setEntryId($entryId);
$flavorAsset->putInCustomData(self::CLIP_NUMBER,$order);
$flavorAsset->save();
return $flavorAsset;
}
/**
* @param BatchJob $batchJob
* @throws Exception
*/
private function startConcat($batchJob)
{
KalturaLog::info('Going To Start Concat Job');
if($batchJob->getJobType() != BatchJobType::CLIP_CONCAT)
{
return;
}
foreach ($batchJob->getChildJobs() as $job)
{
/** @var BatchJob $job */
KalturaLog::info('Child job id [' . $job->getId() . '] status [' . $job->getStatus() . ']' . '] type ['.$job->getJobType() .']' );
KalturaLog::info('Flavor Param Ids:' .$job->getEntry()->getFlavorParamsIds());
}
/** @var kClipConcatJobData $jobData */
$jobData = $batchJob->getData();
$tempEntry = entryPeer::retrieveByPK($jobData->getTempEntryId());
$assets = assetPeer::retrieveByEntryId($jobData->getTempEntryId(), array(assetType::FLAVOR));
usort($assets, array("kClipManager","cmpByOrder"));
$files = $this->getFilesPath($assets);
$flavorAsset = $this->addNewAssetToTargetEntry($tempEntry);
kJobsManager::addConcatJob($batchJob, $flavorAsset, $files,false);
}
/**
* @param $a flavorAsset
* @param $b flavorAsset
* @return int
*/
private function cmpByOrder($a,$b)
{
$aClipNumber = $a->getFromCustomData(self::CLIP_NUMBER);
$bClipNumber = $b->getFromCustomData(self::CLIP_NUMBER);
if (!$aClipNumber )
{
return -1;
}
if (!$bClipNumber )
{
return 1;
}
return ($aClipNumber < $bClipNumber) ? -1 : 1;
}
/***
* @param BatchJob $batchJob
* @throws APIException
* @throws kCoreException
*/
private function handleClipConcatParentJob($batchJob)
{
switch ($batchJob->getStatus()) {
case BatchJob::BATCHJOB_STATUS_PENDING:
$errDesc = '';
/**@var kClipConcatJobData $jobData */
$jobData = $batchJob->getData();
$this->addClipJobs($batchJob, $jobData->getTempEntryId(), $errDesc,
$jobData->getPartnerId(),
$jobData->getOperationAttributes(), $jobData->getPriority());
kJobsManager::updateBatchJob($batchJob, BatchJob::BATCHJOB_STATUS_PROCESSING);
break;
default:
break;
}
}
/**
* @param BatchJob $parentJob clipConcat job
* @param $entryId
* @param $errDescription
* @param $partnerId
* @param array $operationAttributes
* @param int $priority
* @return BatchJob[]
* @throws APIException
* @throws kCoreException
*/
private function addClipJobs($parentJob , $entryId, &$errDescription, $partnerId,
array $operationAttributes, $priority = 0)
{
$batchArray = array();
$order = 0;
$originalConversionEnginesExtraParams =
assetParamsPeer::retrieveByPK(kClipAttributes::SYSTEM_DEFAULT_FLAVOR_PARAMS_ID)->getConversionEnginesExtraParams();
$originalFlavorAsset = assetPeer::retrieveOriginalByEntryId($entryId);
$encryptionKey = null;
if ($originalFlavorAsset)
{
$encryptionKey = $originalFlavorAsset->getEncryptionKey();
}
/* @var $singleAttribute kClipAttributes */
foreach($operationAttributes as $singleAttribute)
{
KalturaLog::info("Going To create Flavor for clip: " . print_r($singleAttribute, true));
if($singleAttribute->getDuration()<=0)
{
KalturaLog::info("Ingnoring clip attribute with non-positive duration");
continue;
}
$clonedID = $this->cloneFlavorParam($singleAttribute, $originalConversionEnginesExtraParams, $encryptionKey);
$flavorAsst = $this->createTempClipFlavorAsset($partnerId,$entryId,$clonedID,$order);
$batchJob = kBusinessPreConvertDL::decideAddEntryFlavor($parentJob, $entryId,
$clonedID, $errDescription,$flavorAsst->getId()
, array($singleAttribute) , $priority);
if(!$batchJob)
{
throw new APIException(KalturaErrors::CANNOT_CREATE_CLIP_FLAVOR_JOB, $parentJob->getJobType(), $parentJob->getId());
}
$batchArray[] = $batchJob;
$order++;
}
return $batchArray;
}
/***
* @param BatchJob $batchJob
* @return bool are all clip batch done
*/
private function areAllClipJobsDone($batchJob)
{
$c = new Criteria();
$c->add(BatchJobPeer::JOB_TYPE,array(BatchJobType::CONVERT,BatchJobType::CONCAT),Criteria::IN);
$c->add(BatchJobPeer::STATUS,array(BatchJob::BATCHJOB_STATUS_FINISHED),Criteria::NOT_IN);
$childJobs = $batchJob->getRootJob()->getChildJobs($c);
if (count($childJobs) != 0)
{
/** @var BatchJob $job */
foreach ($childJobs as $job)
{
KalturaLog::info("number of children: ". count($childJobs));
KalturaLog::info('Child job id [' . $job->getId() . '] status [' . $job->getStatus() . ']');
}
return false;
}
if ($batchJob->getJobType() == BatchJobType::POSTCONVERT &&
$batchJob->getStatus() == BatchJob::BATCHJOB_STATUS_FINISHED)
{
return true;
}
return false;
}
/**
* @param $assets
* @return array
* @throws kCoreException
* @throws Exception
*/
private function getFilesPath($assets)
{
$files = array();
foreach ($assets as $asset) {
/**
* Don't take source it is empty
* @var flavorAsset $asset */
if ($asset->getIsOriginal()) {
continue;
}
$syncKey = $asset->getSyncKey(asset::FILE_SYNC_ASSET_SUB_TYPE_ASSET);
$fileSync = kFileSyncUtils::getReadyFileSyncForKey($syncKey);
//fileSync should be local
if (!$fileSync[1])
{
throw new kCoreException("Clip Does not exist locally operations stops, there will be no concat");
}
/*** @var array $fileSync */
if ($fileSync[0]->getFullPath())
{
$files[] = $fileSync[0]->getFullPath();
}
}
return $files;
}
/**
* @param entry $tempEntry
* @return flavorAsset
* @throws kCoreException
*/
private function addNewAssetToTargetEntry($tempEntry)
{
/** @var flavorAsset $flavorAsset */
$flavorAsset = assetPeer::getNewAsset(assetType::FLAVOR);
// create asset
$flavorAsset->setPartnerId($tempEntry->getPartnerId());
$flavorAsset->setEntryId($tempEntry->getId());
$flavorAsset->setStatus(asset::ASSET_STATUS_QUEUED);
$flavorAsset->setFlavorParamsId(kClipAttributes::SYSTEM_DEFAULT_FLAVOR_PARAMS_ID);
$flavorAsset->setIsOriginal(false);
$flavorAsset->save();
return $flavorAsset;
}
/**
* @param $partnerId
* @return entry
* @throws Exception
*/
public function createTempEntryForClip($partnerId)
{
$tempEntry = new entry();
$tempEntry->setType(entryType::MEDIA_CLIP);
$tempEntry->setMediaType(entry::ENTRY_MEDIA_TYPE_VIDEO);
$tempEntry->setName('TEMP_'.time());
$tempEntry->setPartnerId($partnerId);
$tempEntry->setStatus(entryStatus::NO_CONTENT);
$tempEntry->setDisplayInSearch(EntryDisplayInSearchType::SYSTEM);
$tempEntry->setSourceType(EntrySourceType::CLIP);
$tempEntry->setKuserId(kCurrentContext::getCurrentKsKuserId());
$tempEntry->setConversionProfileId(myPartnerUtils::getConversionProfile2ForPartner($partnerId)->getId());
$tempEntry->save();
KalturaLog::info('Temp ClipConcat Entry Created, Entry ID: ' . $tempEntry->getId());
return $tempEntry;
}
/**
* @param string $entryId
* @param asset $concatAsset
* @throws Exception
* @throws KalturaAPIException
*/
private function addDestinationEntryAsset($entryId, $concatAsset)
{
$concatSyncKey = $concatAsset->getSyncKey(flavorAsset::FILE_SYNC_ASSET_SUB_TYPE_ASSET);
$dbAsset = assetPeer::retrieveOriginalByEntryId($entryId);
$dbEntry = entryPeer::retrieveByPK($entryId);
$isNewAsset = false;
if(!$dbAsset)
{
$isNewAsset = true;
$dbAsset = kFlowHelper::createOriginalFlavorAsset($dbEntry->getPartnerId(), $entryId);
$dbAsset->setFileExt($concatAsset->getFileExt());
$dbAsset->save();
}
if(!$dbAsset)
{
$this->updateAssetFailedToConvert($entryId, $dbEntry);
throw new KalturaAPIException(KalturaErrors::ORIGINAL_FLAVOR_ASSET_NOT_CREATED);
}
$this->updateAssetState($concatSyncKey, $dbAsset, $isNewAsset, $dbEntry);
}
/**
* @param BatchJob $batchJob
* @throws Exception
* @throws KalturaAPIException
*/
private function concatDone(BatchJob $batchJob)
{
/** @var kConcatJobData $concatJobData */
$concatJobData = $batchJob->getParentJob()->getData();
$concatAsset = assetPeer::retrieveById($concatJobData->getFlavorAssetId());
/** @var kClipConcatJobData $clipConcatJobData */
$clipConcatJobData = $batchJob->getRootJob()->getData();
$this->addDestinationEntryAsset($clipConcatJobData->getDestEntryId(), $concatAsset);
$this->deleteEntry($clipConcatJobData->getTempEntryId());
kJobsManager::updateBatchJob($batchJob->getRootJob(), BatchJob::BATCHJOB_STATUS_FINISHED);
}
/**
* @param $entryId
* @param null $entryType
* @throws Exception
* @throws KalturaAPIException
*/
private function deleteEntry($entryId, $entryType = null)
{
$entryToDelete = entryPeer::retrieveByPK($entryId);
if (!$entryToDelete || ($entryType !== null && $entryToDelete->getType() != $entryType))
throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId);
myEntryUtils::deleteEntry($entryToDelete);
}
/**
* @param asset $dbAsset
* @throws PropelException
*/
private function updateMediaFlowOnAsset($dbAsset)
{
$mediaInfo = mediaInfoPeer::retrieveByFlavorAssetId($dbAsset->getId());
if ($mediaInfo) {
$newMediaInfo = $mediaInfo->copy();
$newMediaInfo->setFlavorAssetId($dbAsset->getId());
$newMediaInfo->save();
}
}
/**
* @param asset $dbAsset
* @param entry $dbEntry
*/
private function syncFlavorParamToAsset($dbAsset, $dbEntry)
{
if ($dbAsset->getStatus() == asset::ASSET_STATUS_READY) {
$dbEntry->syncFlavorParamsIds();
$dbEntry->save();
}
}
/**
* @param string $entryId
* @param entry $dbEntry
*/
private function updateAssetFailedToConvert($entryId, $dbEntry)
{
KalturaLog::err("Flavor asset not created for entry [" . $entryId . "]");
if ($dbEntry->getStatus() == entryStatus::NO_CONTENT) {
$dbEntry->setStatus(entryStatus::ERROR_CONVERTING);
$dbEntry->save();
}
}
/**
* @param $concatSyncKey
* @param asset $dbAsset
* @param $isNewAsset
* @param $dbEntry
* @throws PropelException
*/
private function updateAssetState($concatSyncKey, $dbAsset, $isNewAsset, $dbEntry)
{
$newSyncKey = $dbAsset->getSyncKey(flavorAsset::FILE_SYNC_ASSET_SUB_TYPE_ASSET);
kFileSyncUtils::createSyncFileLinkForKey($newSyncKey, $concatSyncKey);
if ($isNewAsset)
kEventsManager::raiseEvent(new kObjectAddedEvent($dbAsset));
kEventsManager::raiseEvent(new kObjectDataChangedEvent($dbAsset));
$this->updateMediaFlowOnAsset($dbAsset);
$this->syncFlavorParamToAsset($dbAsset, $dbEntry);
}
/**
* @param entry $destEntry
* @param $sourceEntryId
* @param array $operationAttributes
*/
private function fillDestEntry($destEntry, $sourceEntryId, array $operationAttributes)
{
if ($destEntry->getIsTemporary())
$destEntry->setFlowType(EntryFlowType::TRIM_CONCAT);
else
$destEntry->setFlowType(EntryFlowType::CLIP_CONCAT);
$destEntry->setSourceEntryId($sourceEntryId);
$destEntry->setOperationAttributes($operationAttributes);
$destEntry->setStatus(entryStatus::PENDING);
$destEntry->save();
}
/**
* @param array $conversionExtraParamsArray
* @param array $conversionEngines
* @param kClipAttributes $singleAttribute
* @return string
*/
private function editConversionEngineExtraParam($conversionEngines, $singleAttribute, $conversionExtraParamsArray = array())
{
$newConversionExtraParams = array();
for ($i = 0; $i < count($conversionEngines) ; $i++)
{
$extraParams = '';
if($i < count($conversionExtraParamsArray))
$extraParams = $conversionExtraParamsArray[$i];
if ($conversionEngines[$i] == conversionEngineType::FFMPEG || $conversionEngines[$i] == conversionEngineType::FFMPEG_AUX)
$extraParams .= $this->addEffects($singleAttribute);
$newConversionExtraParams[] = $extraParams;
}
return implode(' | ',$newConversionExtraParams);
}
/**
* @param assetParams $flavorParamsObj
* @param kClipAttributes $singleAttribute
* @param string $originalConversionEnginesExtraParams
*/
private function fixConversionParam($flavorParamsObj, $singleAttribute, $originalConversionEnginesExtraParams)
{
$conversionEngines = explode(',', $flavorParamsObj->getConversionEngines());
if (is_null($originalConversionEnginesExtraParams))
$newExtraConversionParams = $this->editConversionEngineExtraParam($conversionEngines, $singleAttribute);
else {
$conversionExtraParams = explode('|', $originalConversionEnginesExtraParams);
$newExtraConversionParams =
$this->editConversionEngineExtraParam($conversionEngines, $singleAttribute,$conversionExtraParams);
}
$flavorParamsObj->setConversionEnginesExtraParams($newExtraConversionParams);
}
/**
* @param kClipAttributes $singleAttribute
* @return string
*/
private function addEffects($singleAttribute)
{
$effects = new kEffectsManager();
return $effects->getFFMPEGEffects($singleAttribute);
}
/**
* @param BatchJob $rootJob
* @return bool
*/
private function concatJobExist($rootJob)
{
if (!$rootJob)
return false;
/** @var BatchJob $job */
foreach ($rootJob->getChildJobs() as $job)
{
if ($job->getJobType() == BatchJobType::CONCAT)
return true;
}
return false;
}
}