|
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\M3u8; |
|
13
|
|
|
|
|
14
|
|
|
class Playlist implements \Iterator, \ArrayAccess |
|
15
|
|
|
{ |
|
16
|
|
|
private $mediaSegments; |
|
17
|
|
|
private $isEndless; |
|
18
|
|
|
private $age; |
|
19
|
|
|
private $position = 0; |
|
20
|
|
|
|
|
21
|
4 |
|
public function __construct(array $mediaSegments = array(), $isEndless = false, $age = null) |
|
22
|
|
|
{ |
|
23
|
4 |
|
$this->mediaSegments = $mediaSegments; |
|
24
|
4 |
|
$this->isEndless = $isEndless; |
|
25
|
4 |
|
$this->age = $age; |
|
26
|
4 |
|
} |
|
27
|
|
|
|
|
28
|
2 |
|
public function rewind() |
|
29
|
|
|
{ |
|
30
|
2 |
|
$this->position = 0; |
|
31
|
2 |
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
public function current() |
|
34
|
|
|
{ |
|
35
|
2 |
|
return $this->mediaSegments[$this->position]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function key() |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->position; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
public function next() |
|
44
|
|
|
{ |
|
45
|
2 |
|
++$this->position; |
|
46
|
2 |
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
public function valid() |
|
49
|
|
|
{ |
|
50
|
2 |
|
return isset($this->mediaSegments[$this->position]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function offsetSet($offset, $value) |
|
54
|
|
|
{ |
|
55
|
|
|
if (!$value instanceof MediaSegment) { |
|
56
|
|
|
throw new \InvalidArgumentException('$value should be type of Chrisyue\PhpM3u8\M3u8\MediaSegment\MediaSegment'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (is_null($offset)) { |
|
60
|
|
|
$this->add($value); |
|
61
|
|
|
|
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->mediaSegments[$offset] = $value; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function offsetGet($offset) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->mediaSegments[$offset]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function offsetExists($offset) |
|
74
|
|
|
{ |
|
75
|
|
|
return isset($this->mediaSegments[$offset]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function offsetUnset($offset) |
|
79
|
|
|
{ |
|
80
|
|
|
unset($this->mediaSegments[$offset]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function add(MediaSegment $mediaSegment) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->mediaSegments[] = $mediaSegment; |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
public function getFirst() |
|
91
|
|
|
{ |
|
92
|
2 |
|
$first = reset($this->mediaSegments); |
|
93
|
|
|
|
|
94
|
2 |
|
if (false !== $first) { |
|
95
|
2 |
|
return $first; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getDuration() |
|
100
|
|
|
{ |
|
101
|
|
|
$duration = 0; |
|
102
|
|
|
foreach ($this->mediaSegments as $segment) { |
|
103
|
|
|
$duration += $segment->getDuration(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $duration; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function count() |
|
110
|
|
|
{ |
|
111
|
|
|
return count($this->mediaSegments); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function setAge($age) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->age = $age; |
|
117
|
|
|
|
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function getAge() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->age; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
2 |
|
public function isEndless() |
|
127
|
|
|
{ |
|
128
|
2 |
|
return $this->isEndless; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|