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 $ts_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
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getTsSubDirectory(): string |
57
|
|
|
{ |
58
|
|
|
return $this->ts_sub_directory; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $ts_sub_directory |
63
|
|
|
* @return HLS |
64
|
|
|
*/ |
65
|
|
|
public function setTsSubDirectory(string $ts_sub_directory) |
66
|
|
|
{ |
67
|
|
|
$this->ts_sub_directory = $ts_sub_directory; |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $hls_time |
73
|
|
|
* @return HLS |
74
|
|
|
*/ |
75
|
|
|
public function setHlsTime(string $hls_time): HLS |
76
|
|
|
{ |
77
|
|
|
$this->hls_time = $hls_time; |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getHlsTime(): string |
85
|
|
|
{ |
86
|
|
|
return $this->hls_time; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param bool $hls_allow_cache |
91
|
|
|
* @return HLS |
92
|
|
|
*/ |
93
|
|
|
public function setHlsAllowCache(bool $hls_allow_cache): HLS |
94
|
|
|
{ |
95
|
|
|
$this->hls_allow_cache = $hls_allow_cache; |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function isHlsAllowCache(): bool |
103
|
|
|
{ |
104
|
|
|
return $this->hls_allow_cache; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $hls_key_info_file |
109
|
|
|
* @return HLS |
110
|
|
|
*/ |
111
|
|
|
public function setHlsKeyInfoFile(string $hls_key_info_file): HLS |
112
|
|
|
{ |
113
|
|
|
$this->hls_key_info_file = $hls_key_info_file; |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $save_to |
119
|
|
|
* @param string $url |
120
|
|
|
* @param string|null $key_info_path |
121
|
|
|
* @param int $length |
122
|
|
|
* @return HLS |
123
|
|
|
*/ |
124
|
|
|
public function encryption(string $save_to, string $url, string $key_info_path = null, int $length = 16): HLS |
125
|
|
|
{ |
126
|
|
|
$this->setHlsKeyInfoFile(HLSKeyInfo::generate($save_to, $url, $key_info_path, $length)); |
127
|
|
|
$this->tmp_key_info_file = true; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function getHlsKeyInfoFile(): string |
136
|
|
|
{ |
137
|
|
|
return $this->hls_key_info_file; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $hls_base_url |
142
|
|
|
* @return HLS |
143
|
|
|
*/ |
144
|
|
|
public function setHlsBaseUrl(string $hls_base_url): HLS |
145
|
|
|
{ |
146
|
|
|
$this->hls_base_url = $hls_base_url; |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getHlsBaseUrl(): string |
154
|
|
|
{ |
155
|
|
|
return $this->hls_base_url; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param int $hls_list_size |
160
|
|
|
* @return HLS |
161
|
|
|
*/ |
162
|
|
|
public function setHlsListSize(int $hls_list_size): HLS |
163
|
|
|
{ |
164
|
|
|
$this->hls_list_size = $hls_list_size; |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return int |
170
|
|
|
*/ |
171
|
|
|
public function getHlsListSize(): int |
172
|
|
|
{ |
173
|
|
|
return $this->hls_list_size; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $master_playlist |
178
|
|
|
* @param array $stream_des |
179
|
|
|
* @return HLS |
180
|
|
|
*/ |
181
|
|
|
public function setMasterPlaylist(string $master_playlist, array $stream_des = []): HLS |
182
|
|
|
{ |
183
|
|
|
$this->master_playlist = $master_playlist; |
184
|
|
|
$this->stream_des = $stream_des; |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $hls_segment_type |
191
|
|
|
* @return HLS |
192
|
|
|
*/ |
193
|
|
|
public function setHlsSegmentType(string $hls_segment_type): HLS |
194
|
|
|
{ |
195
|
|
|
$this->hls_segment_type = $hls_segment_type; |
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
|
|
public function getHlsSegmentType(): string |
203
|
|
|
{ |
204
|
|
|
return $this->hls_segment_type; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param string $hls_fmp4_init_filename |
209
|
|
|
* @return HLS |
210
|
|
|
*/ |
211
|
|
|
public function setHlsFmp4InitFilename(string $hls_fmp4_init_filename): HLS |
212
|
|
|
{ |
213
|
|
|
$this->hls_fmp4_init_filename = $hls_fmp4_init_filename; |
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
public function getHlsFmp4InitFilename(): string |
221
|
|
|
{ |
222
|
|
|
return $this->hls_fmp4_init_filename; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return HLSFilter |
227
|
|
|
*/ |
228
|
|
|
protected function getFilter(): StreamFilterInterface |
229
|
|
|
{ |
230
|
|
|
return new HLSFilter($this); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
protected function getPath(): string |
237
|
|
|
{ |
238
|
|
|
$path = $this->getFilePath(); |
239
|
|
|
$reps = $this->getRepresentations(); |
240
|
|
|
|
241
|
|
|
$this->savePlaylist($path . ".m3u8"); |
242
|
|
|
|
243
|
|
|
return $path . "_" . $reps->end()->getHeight() . "p.m3u8"; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param $path |
248
|
|
|
*/ |
249
|
|
|
public function savePlaylist(string $path): void |
250
|
|
|
{ |
251
|
|
|
$mater_playlist = new HLSPlaylist($this); |
252
|
|
|
$mater_playlist->save($this->master_playlist ?? $path, $this->stream_des); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Clear key info file if is a temp file |
257
|
|
|
*/ |
258
|
|
|
public function __destruct() |
259
|
|
|
{ |
260
|
|
|
if ($this->tmp_key_info_file) { |
261
|
|
|
File::remove($this->getHlsKeyInfoFile()); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
parent::__destruct(); |
265
|
|
|
} |
266
|
|
|
} |