1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the PHP-FFmpeg-video-streaming package. |
5
|
|
|
* |
6
|
|
|
* (c) Amin Yazdanpanah <[email protected]> |
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 Streaming; |
13
|
|
|
|
14
|
|
|
use Streaming\Filters\HLSFilter; |
15
|
|
|
use Streaming\Filters\StreamFilterInterface; |
16
|
|
|
|
17
|
|
|
class HLS extends Streaming |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private $hls_time = 10; |
22
|
|
|
|
23
|
|
|
/** @var bool */ |
24
|
|
|
private $hls_allow_cache = true; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $hls_key_info_file = ""; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
private $seg_sub_directory = ""; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
private $hls_base_url = ""; |
34
|
|
|
|
35
|
|
|
/** @var int */ |
36
|
|
|
private $hls_list_size = 0; |
37
|
|
|
|
38
|
|
|
/** @var bool */ |
39
|
|
|
private $tmp_key_info_file = false; |
40
|
|
|
|
41
|
|
|
/** @var string */ |
42
|
|
|
public $master_playlist; |
43
|
|
|
|
44
|
|
|
/** @var string */ |
45
|
|
|
private $hls_segment_type = 'mpegts'; |
46
|
|
|
|
47
|
|
|
/** @var string */ |
48
|
|
|
private $hls_fmp4_init_filename = "init.mp4"; |
49
|
|
|
|
50
|
|
|
/** @var array */ |
51
|
|
|
private $stream_des = []; |
52
|
|
|
|
53
|
|
|
/** @var array */ |
54
|
|
|
private $flags = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getSegSubDirectory(): string |
60
|
|
|
{ |
61
|
|
|
return $this->seg_sub_directory; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $ts_sub_directory |
66
|
|
|
* @return HLS |
67
|
|
|
*/ |
68
|
|
|
public function setSegSubDirectory(string $ts_sub_directory) |
69
|
|
|
{ |
70
|
|
|
$this->seg_sub_directory = $ts_sub_directory; |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $hls_time |
76
|
|
|
* @return HLS |
77
|
|
|
*/ |
78
|
|
|
public function setHlsTime(string $hls_time): HLS |
79
|
|
|
{ |
80
|
|
|
$this->hls_time = $hls_time; |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getHlsTime(): string |
88
|
|
|
{ |
89
|
|
|
return $this->hls_time; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param bool $hls_allow_cache |
94
|
|
|
* @return HLS |
95
|
|
|
*/ |
96
|
|
|
public function setHlsAllowCache(bool $hls_allow_cache): HLS |
97
|
|
|
{ |
98
|
|
|
$this->hls_allow_cache = $hls_allow_cache; |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public function isHlsAllowCache(): bool |
106
|
|
|
{ |
107
|
|
|
return $this->hls_allow_cache; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $hls_key_info_file |
112
|
|
|
* @return HLS |
113
|
|
|
*/ |
114
|
|
|
public function setHlsKeyInfoFile(string $hls_key_info_file): HLS |
115
|
|
|
{ |
116
|
|
|
$this->hls_key_info_file = $hls_key_info_file; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $save_to |
122
|
|
|
* @param string $url |
123
|
|
|
* @param int $key_rotation_period |
124
|
|
|
* @param string $search |
125
|
|
|
* @param int $length |
126
|
|
|
* @return HLS |
127
|
|
|
*/ |
128
|
|
|
public function encryption(string $save_to, string $url, int $key_rotation_period = 0, $search = ".ts' for writing", int $length = 16): HLS |
129
|
|
|
{ |
130
|
|
|
$key_info = HLSKeyInfo::create($save_to, $url); |
131
|
|
|
$key_info->setLength($length); |
132
|
|
|
|
133
|
|
|
if ($key_rotation_period > 0) { |
134
|
|
|
$key_info->rotateKey($this->getMedia()->getFFMpegDriver(), $key_rotation_period, $search); |
|
|
|
|
135
|
|
|
array_push($this->flags, HLSFlag::PERIODIC_REKEY); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->setHlsKeyInfoFile((string)$key_info); |
139
|
|
|
$this->tmp_key_info_file = true; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getHlsKeyInfoFile(): string |
148
|
|
|
{ |
149
|
|
|
return $this->hls_key_info_file; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $hls_base_url |
154
|
|
|
* @return HLS |
155
|
|
|
*/ |
156
|
|
|
public function setHlsBaseUrl(string $hls_base_url): HLS |
157
|
|
|
{ |
158
|
|
|
$this->hls_base_url = $hls_base_url; |
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getHlsBaseUrl(): string |
166
|
|
|
{ |
167
|
|
|
return $this->hls_base_url; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param int $hls_list_size |
172
|
|
|
* @return HLS |
173
|
|
|
*/ |
174
|
|
|
public function setHlsListSize(int $hls_list_size): HLS |
175
|
|
|
{ |
176
|
|
|
$this->hls_list_size = $hls_list_size; |
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return int |
182
|
|
|
*/ |
183
|
|
|
public function getHlsListSize(): int |
184
|
|
|
{ |
185
|
|
|
return $this->hls_list_size; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $master_playlist |
190
|
|
|
* @param array $stream_des |
191
|
|
|
* @return HLS |
192
|
|
|
*/ |
193
|
|
|
public function setMasterPlaylist(string $master_playlist, array $stream_des = []): HLS |
194
|
|
|
{ |
195
|
|
|
$this->master_playlist = $master_playlist; |
196
|
|
|
$this->stream_des = $stream_des; |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return HLS |
203
|
|
|
*/ |
204
|
|
|
public function fragmentedMP4(): HLS |
205
|
|
|
{ |
206
|
|
|
$this->setHlsSegmentType("fmp4"); |
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $hls_segment_type |
212
|
|
|
* @return HLS |
213
|
|
|
*/ |
214
|
|
|
public function setHlsSegmentType(string $hls_segment_type): HLS |
215
|
|
|
{ |
216
|
|
|
$this->hls_segment_type = $hls_segment_type; |
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
|
|
public function getHlsSegmentType(): string |
224
|
|
|
{ |
225
|
|
|
return $this->hls_segment_type; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param string $hls_fmp4_init_filename |
230
|
|
|
* @return HLS |
231
|
|
|
*/ |
232
|
|
|
public function setHlsFmp4InitFilename(string $hls_fmp4_init_filename): HLS |
233
|
|
|
{ |
234
|
|
|
$this->hls_fmp4_init_filename = $hls_fmp4_init_filename; |
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
public function getHlsFmp4InitFilename(): string |
242
|
|
|
{ |
243
|
|
|
return $this->hls_fmp4_init_filename; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param array $flags |
248
|
|
|
* @return HLS |
249
|
|
|
*/ |
250
|
|
|
public function setFlags(array $flags): HLS |
251
|
|
|
{ |
252
|
|
|
$this->flags = $flags; |
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return array |
258
|
|
|
*/ |
259
|
|
|
public function getFlags(): array |
260
|
|
|
{ |
261
|
|
|
return $this->flags; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return HLSFilter |
266
|
|
|
*/ |
267
|
|
|
protected function getFilter(): StreamFilterInterface |
268
|
|
|
{ |
269
|
|
|
return new HLSFilter($this); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @return string |
274
|
|
|
*/ |
275
|
|
|
protected function getPath(): string |
276
|
|
|
{ |
277
|
|
|
$path = $this->getFilePath(); |
278
|
|
|
$reps = $this->getRepresentations(); |
279
|
|
|
|
280
|
|
|
$this->savePlaylist($path . ".m3u8"); |
281
|
|
|
|
282
|
|
|
return $path . "_" . $reps->end()->getHeight() . "p.m3u8"; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param $path |
287
|
|
|
*/ |
288
|
|
|
public function savePlaylist(string $path): void |
289
|
|
|
{ |
290
|
|
|
$mater_playlist = new HLSPlaylist($this); |
291
|
|
|
$mater_playlist->save($this->master_playlist ?? $path, $this->stream_des); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Clear key info file if is a temp file |
296
|
|
|
*/ |
297
|
|
|
public function __destruct() |
298
|
|
|
{ |
299
|
|
|
if ($this->tmp_key_info_file) { |
300
|
|
|
File::remove($this->getHlsKeyInfoFile()); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
parent::__destruct(); |
304
|
|
|
} |
305
|
|
|
} |