-
Notifications
You must be signed in to change notification settings - Fork 5
/
PlaylistScheduler.php
74 lines (60 loc) · 2.54 KB
/
PlaylistScheduler.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
<?php
/*
* This file is part of the Mala package.
*
* (c) Chrisyue <http://chrisyue.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Chrisyue\Mala;
use Chrisyue\Mala\Manager\EpgManagerInterface;
use Chrisyue\Mala\Manager\MediaSegmentManagerInterface;
use Chrisyue\Mala\Model\ChannelInterface;
use Chrisyue\PhpM3u8\Parser;
class PlaylistScheduler
{
private $parser;
private $epgManager;
private $mediaSegmentManager;
public function __construct(Parser $parser, EpgManagerInterface $epgManager, MediaSegmentManagerInterface $mediaSegmentManager)
{
$this->parser = $parser;
$this->epgManager = $epgManager;
$this->mediaSegmentManager = $mediaSegmentManager;
}
public function schedule(ChannelInterface $channel, \DateTime $startsAt, \DateTime $endsAt)
{
$epg = $this->epgManager->find($channel, $startsAt, $endsAt);
if (empty($epg)) {
throw new \UnexpectedValueException('there is no epg found');
}
$firstProgramStartsAt = $epg[0]->getStartsAt();
$this->mediaSegmentManager->clear($channel, $firstProgramStartsAt);
$lastMediaSegment = $this->mediaSegmentManager->findLast($channel);
$sequence = 0;
if (null !== $lastMediaSegment && $firstProgramStartsAt->getTimestamp() <= $lastMediaSegment->getEndsAt()->getTimestamp() + 1) {
$sequence = $lastMediaSegment->getSequence();
}
foreach ($epg as $program) {
$isDiscontinuity = true;
$segmentStartsAt = clone $program->getStartsAt();
$handledDuration = 0;
$m3u8 = $this->parser->parseFromUri($program->getVideo()->getUri());
foreach ($m3u8->getPlaylist() as $originSegment) {
$handledDuration += $originSegment->getDuration();
$segmentEndsAt = clone $program->getStartsAt();
$segmentEndsAt->modify(sprintf('+%d seconds', round($handledDuration)));
$segment = $this->mediaSegmentManager->create(
$program, $segmentStartsAt, $segmentEndsAt,
$originSegment->getUri(), $originSegment->getDuration(),
++$sequence, $isDiscontinuity
);
$this->mediaSegmentManager->saveDeferred($segment);
$segmentStartsAt = clone $segmentEndsAt;
$isDiscontinuity = false;
}
$this->mediaSegmentManager->commit();
}
}
}