1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Mala package. |
5
|
|
|
* |
6
|
|
|
* (c) Chrisyue <http://chrisyue.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Chrisyue\Mala; |
13
|
|
|
|
14
|
|
|
use Chrisyue\Mala\Manager\EpgManagerInterface; |
15
|
|
|
use Chrisyue\Mala\Manager\MediaSegmentManagerInterface; |
16
|
|
|
use Chrisyue\Mala\Model\ChannelInterface; |
17
|
|
|
use Chrisyue\PhpM3u8\Parser; |
18
|
|
|
|
19
|
|
|
class PlaylistScheduler |
20
|
|
|
{ |
21
|
|
|
private $parser; |
22
|
|
|
private $epgManager; |
23
|
|
|
private $mediaSegmentManager; |
24
|
|
|
|
25
|
2 |
|
public function __construct(Parser $parser, EpgManagerInterface $epgManager, MediaSegmentManagerInterface $mediaSegmentManager) |
26
|
|
|
{ |
27
|
2 |
|
$this->parser = $parser; |
28
|
2 |
|
$this->epgManager = $epgManager; |
29
|
2 |
|
$this->mediaSegmentManager = $mediaSegmentManager; |
30
|
2 |
|
} |
31
|
|
|
|
32
|
2 |
|
public function schedule(ChannelInterface $channel, \DateTime $startsAt, \DateTime $endsAt) |
33
|
|
|
{ |
34
|
2 |
|
$epg = $this->epgManager->find($channel, $startsAt, $endsAt); |
35
|
2 |
|
if (empty($epg)) { |
36
|
1 |
|
throw new \UnexpectedValueException('there is no epg found'); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
$firstProgramStartsAt = $epg[0]->getStartsAt(); |
40
|
1 |
|
$this->mediaSegmentManager->clear($channel, $firstProgramStartsAt); |
41
|
|
|
|
42
|
1 |
|
$lastMediaSegment = $this->mediaSegmentManager->findLast($channel); |
43
|
|
|
|
44
|
1 |
|
$sequence = 0; |
45
|
1 |
|
if (null !== $lastMediaSegment && $firstProgramStartsAt->getTimestamp() <= $lastMediaSegment->getEndsAt()->getTimestamp() + 1) { |
46
|
1 |
|
$sequence = $lastMediaSegment->getSequence(); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
1 |
|
foreach ($epg as $program) { |
50
|
1 |
|
$isDiscontinuity = true; |
51
|
1 |
|
$segmentStartsAt = clone $program->getStartsAt(); |
52
|
1 |
|
$handledDuration = 0; |
53
|
|
|
|
54
|
1 |
|
$m3u8 = $this->parser->parseFromUri($program->getVideo()->getUri()); |
55
|
1 |
|
foreach ($m3u8->getPlaylist() as $originSegment) { |
56
|
1 |
|
$handledDuration += $originSegment->getDuration(); |
57
|
1 |
|
$segmentEndsAt = clone $program->getStartsAt(); |
58
|
1 |
|
$segmentEndsAt->modify(sprintf('+%d seconds', round($handledDuration))); |
59
|
|
|
|
60
|
1 |
|
$segment = $this->mediaSegmentManager->create( |
61
|
1 |
|
$program, $segmentStartsAt, $segmentEndsAt, |
62
|
1 |
|
$originSegment->getUri(), $originSegment->getDuration(), |
63
|
1 |
|
++$sequence, $isDiscontinuity |
64
|
1 |
|
); |
65
|
1 |
|
$this->mediaSegmentManager->saveDeferred($segment); |
66
|
|
|
|
67
|
1 |
|
$segmentStartsAt = clone $segmentEndsAt; |
68
|
1 |
|
$isDiscontinuity = false; |
69
|
1 |
|
} |
70
|
|
|
|
71
|
1 |
|
$this->mediaSegmentManager->commit(); |
72
|
1 |
|
} |
73
|
1 |
|
} |
74
|
|
|
} |
75
|
|
|
|