1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PhpM3u8 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\PhpM3u8; |
13
|
|
|
|
14
|
|
|
class Segments implements DumpableInterface, \Iterator, \ArrayAccess |
15
|
|
|
{ |
16
|
|
|
private $segments = array(); |
17
|
|
|
private $m3u8; |
18
|
|
|
|
19
|
|
|
private $position = 0; |
20
|
|
|
|
21
|
2 |
|
public function __construct(M3u8 $m3u8) |
22
|
|
|
{ |
23
|
2 |
|
$this->m3u8 = $m3u8; |
24
|
2 |
|
} |
25
|
|
|
|
26
|
1 |
|
public function rewind() |
27
|
1 |
|
{ |
28
|
1 |
|
$this->position = 0; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
public function current() |
32
|
|
|
{ |
33
|
|
|
return $this->segments[$this->position]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function key() |
37
|
|
|
{ |
38
|
|
|
return $this->position; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function next() |
42
|
|
|
{ |
43
|
|
|
++$this->position; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function valid() |
47
|
|
|
{ |
48
|
|
|
return isset($this->segments[$this->position]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function offsetSet($offset, $value) |
52
|
|
|
{ |
53
|
|
|
if (!is_object($value) || !$value instanceof Segment) { |
54
|
|
|
throw new \InvalidArgumentException('Expected $value of class Chrisyue\PhpM3u8\Segment'); |
55
|
|
|
} |
56
|
|
|
if (is_null($offset)) { |
57
|
|
|
$this->add($value); |
58
|
|
|
|
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
$this->segments[$offset] = $value; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return Chrisyue\PhpM3u8\Segment |
66
|
|
|
*/ |
67
|
1 |
|
public function offsetGet($offset) |
68
|
|
|
{ |
69
|
1 |
|
return $this->segments[$offset]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function offsetExists($offset) |
73
|
|
|
{ |
74
|
|
|
return isset($this->segments[$offset]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function offsetUnset($offset) |
78
|
|
|
{ |
79
|
|
|
unset($this->segments[$offset]); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function add(Segment $segment) |
83
|
|
|
{ |
84
|
1 |
|
$this->segments[] = $segment; |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
public function getDuration() |
88
|
|
|
{ |
89
|
1 |
|
$duration = 0; |
90
|
1 |
|
foreach ($this->segments as $segment) { |
91
|
1 |
|
$duration += $segment->getDuration(); |
92
|
1 |
|
} |
93
|
|
|
|
94
|
1 |
|
return $duration; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getFirst() |
98
|
|
|
{ |
99
|
|
|
$first = reset($this->segments); |
100
|
|
|
if (false !== $first) { |
101
|
|
|
return $first; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function count() |
106
|
|
|
{ |
107
|
|
|
return count($this->segments); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
public function readLines(array &$lines) |
111
|
|
|
{ |
112
|
1 |
|
$mediaSequence = $this->m3u8->getMediaSequence(); |
113
|
1 |
|
$discontinuitySequence = $this->m3u8->getDiscontinuitySequence(); |
114
|
1 |
|
while (true) { |
115
|
1 |
|
$segment = new Segment($this->m3u8->getVersion()); |
116
|
1 |
|
$segment->readLines($lines); |
117
|
|
|
|
118
|
1 |
|
if ($segment->isEmpty()) { |
119
|
1 |
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
$segment->setMediaSequence($mediaSequence++); |
123
|
|
|
|
124
|
1 |
|
$segment->setDiscontinuitySequence($discontinuitySequence); |
125
|
1 |
|
if ($segment->isDiscontinuity()) { |
126
|
1 |
|
$segment->setDiscontinuitySequence(++$discontinuitySequence); |
127
|
1 |
|
} |
128
|
1 |
|
$this->segments[] = $segment; |
129
|
1 |
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function dump() |
133
|
|
|
{ |
134
|
1 |
|
$lines = array_map(function (Segment $segment) { |
135
|
1 |
|
return $segment->dump(); |
136
|
1 |
|
}, $this->segments); |
137
|
|
|
|
138
|
1 |
|
return implode("\n", $lines); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|