-
Notifications
You must be signed in to change notification settings - Fork 5
/
M3u8Generator.php
58 lines (47 loc) · 1.58 KB
/
M3u8Generator.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
<?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\MediaSegmentManagerInterface;
use Chrisyue\Mala\Model\ChannelInterface;
use Chrisyue\PhpM3u8\M3u8\M3u8;
use Chrisyue\PhpM3u8\M3u8\Playlist;
class M3u8Generator
{
private $manager;
private $options;
public function __construct(MediaSegmentManagerInterface $manager, array $options = array())
{
$this->manager = $manager;
$this->options = $options + array(
'version' => 3,
'target_duration' => 10,
);
}
public function generate(ChannelInterface $channel, \DateTime $startsAt = null)
{
$targetDuration = $this->options['target_duration'];
if (null === $startsAt) {
$startsAt = new \DateTime();
}
$segments = $this->manager->findPlaying($channel, $startsAt, $targetDuration * 3);
$playlist = new Playlist($segments);
$first = $playlist->getFirst();
if (null === $first) {
return;
}
$age = $first->getEndsAt()->getTimestamp() - $startsAt->getTimestamp() + 1;
$playlist->setAge($age);
$discontinuitySequence = $first->getProgram()->getSequence();
if ($first->isDiscontinuity()) {
--$discontinuitySequence;
}
return new M3u8($playlist, $this->options['version'], $targetDuration, $discontinuitySequence);
}
}