|
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 = []; |
|
17
|
|
|
|
|
18
|
|
|
private $m3u8; |
|
19
|
|
|
|
|
20
|
|
|
private $position = 0; |
|
21
|
|
|
|
|
22
|
2 |
|
public function __construct(M3u8 $m3u8) |
|
23
|
|
|
{ |
|
24
|
2 |
|
$this->m3u8 = $m3u8; |
|
25
|
2 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function rewind() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->position = 0; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function current() |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->segments[$this->position]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function key() |
|
38
|
1 |
|
{ |
|
39
|
1 |
|
return $this->position; |
|
40
|
1 |
|
} |
|
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
|
|
View Code Duplication |
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
|
|
|
/** |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function offsetExists($offset) |
|
77
|
|
|
{ |
|
78
|
|
|
return isset($this->segments[$offset]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function offsetUnset($offset) |
|
82
|
|
|
{ |
|
83
|
|
|
unset($this->segments[$offset]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
public function add(Segment $segment) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->segments[] = $segment; |
|
89
|
1 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return int|float |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function getDuration() |
|
95
|
|
|
{ |
|
96
|
1 |
|
$duration = 0; |
|
97
|
1 |
|
foreach ($this->segments as $segment) { |
|
98
|
1 |
|
$duration += $segment->getDuration(); |
|
99
|
1 |
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
return $duration; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return Chrisyue\PhpM3u8\Segment |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getFirst() |
|
108
|
|
|
{ |
|
109
|
|
|
$first = reset($this->segments); |
|
110
|
|
|
if (false !== $first) { |
|
111
|
|
|
return $first; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return int |
|
117
|
|
|
*/ |
|
118
|
|
|
public function count() |
|
119
|
|
|
{ |
|
120
|
|
|
return count($this->segments); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
public function readLines(array &$lines) |
|
124
|
|
|
{ |
|
125
|
1 |
|
$mediaSequence = (int) $this->m3u8->getMediaSequence(); |
|
126
|
1 |
|
$discontinuitySequence = (int) $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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.