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