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\Filter; |
16
|
|
|
|
17
|
|
|
class HLS extends Streaming |
18
|
|
|
{ |
19
|
|
|
/** @var string */ |
20
|
|
|
private $hls_time = 10; |
21
|
|
|
|
22
|
|
|
/** @var bool */ |
23
|
|
|
private $hls_allow_cache = true; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $hls_key_info_file = ""; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $ts_sub_directory = ""; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $hls_base_url = ""; |
33
|
|
|
|
34
|
|
|
/** @var int */ |
35
|
|
|
private $hls_list_size = 0; |
36
|
|
|
|
37
|
|
|
/** @var bool */ |
38
|
|
|
public $tmp_key_info_file = false; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
public $master_playlist; |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
private $hls_segment_type = 'mpegts'; |
45
|
|
|
|
46
|
|
|
/** @var string */ |
47
|
|
|
private $hls_fmp4_init_filename = "init.mp4"; |
48
|
|
|
|
49
|
|
|
/** @var array */ |
50
|
|
|
private $stream_info = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getTsSubDirectory(): string |
56
|
|
|
{ |
57
|
|
|
return $this->ts_sub_directory; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $ts_sub_directory |
62
|
|
|
* @return HLS |
63
|
|
|
*/ |
64
|
|
|
public function setTsSubDirectory(string $ts_sub_directory) |
65
|
|
|
{ |
66
|
|
|
$this->ts_sub_directory = $ts_sub_directory; |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $hls_time |
72
|
|
|
* @return HLS |
73
|
|
|
*/ |
74
|
|
|
public function setHlsTime(string $hls_time): HLS |
75
|
|
|
{ |
76
|
|
|
$this->hls_time = $hls_time; |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getHlsTime(): string |
84
|
|
|
{ |
85
|
|
|
return $this->hls_time; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param bool $hls_allow_cache |
90
|
|
|
* @return HLS |
91
|
|
|
*/ |
92
|
|
|
public function setHlsAllowCache(bool $hls_allow_cache): HLS |
93
|
|
|
{ |
94
|
|
|
$this->hls_allow_cache = $hls_allow_cache; |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function isHlsAllowCache(): bool |
102
|
|
|
{ |
103
|
|
|
return $this->hls_allow_cache; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $hls_key_info_file |
108
|
|
|
* @return HLS |
109
|
|
|
*/ |
110
|
|
|
public function setHlsKeyInfoFile(string $hls_key_info_file): HLS |
111
|
|
|
{ |
112
|
|
|
$this->hls_key_info_file = $hls_key_info_file; |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $save_to |
118
|
|
|
* @param string $url |
119
|
|
|
* @param string|null $key_info_path |
120
|
|
|
* @param int $length |
121
|
|
|
* @return HLS |
122
|
|
|
*/ |
123
|
|
|
public function encryption(string $save_to, string $url, string $key_info_path = null, int $length = 16): HLS |
124
|
|
|
{ |
125
|
|
|
$this->setHlsKeyInfoFile(HLSKeyInfo::generate($save_to, $url, $key_info_path, $length)); |
126
|
|
|
$this->tmp_key_info_file = true; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getHlsKeyInfoFile(): string |
135
|
|
|
{ |
136
|
|
|
return $this->hls_key_info_file; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $hls_base_url |
141
|
|
|
* @return HLS |
142
|
|
|
*/ |
143
|
|
|
public function setHlsBaseUrl(string $hls_base_url): HLS |
144
|
|
|
{ |
145
|
|
|
$this->hls_base_url = $hls_base_url; |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function getHlsBaseUrl(): string |
153
|
|
|
{ |
154
|
|
|
return $this->hls_base_url; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param int $hls_list_size |
159
|
|
|
* @return HLS |
160
|
|
|
*/ |
161
|
|
|
public function setHlsListSize(int $hls_list_size): HLS |
162
|
|
|
{ |
163
|
|
|
$this->hls_list_size = $hls_list_size; |
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return int |
169
|
|
|
*/ |
170
|
|
|
public function getHlsListSize(): int |
171
|
|
|
{ |
172
|
|
|
return $this->hls_list_size; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $master_playlist |
177
|
|
|
* @param array $stream_info |
178
|
|
|
* @return HLS |
179
|
|
|
*/ |
180
|
|
|
public function setMasterPlaylist(string $master_playlist, array $stream_info = []): HLS |
181
|
|
|
{ |
182
|
|
|
$this->master_playlist = $master_playlist; |
183
|
|
|
$this->stream_info = $stream_info; |
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $hls_segment_type |
190
|
|
|
* @return HLS |
191
|
|
|
*/ |
192
|
|
|
public function setHlsSegmentType(string $hls_segment_type): HLS |
193
|
|
|
{ |
194
|
|
|
$this->hls_segment_type = $hls_segment_type; |
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
public function getHlsSegmentType(): string |
202
|
|
|
{ |
203
|
|
|
return $this->hls_segment_type; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string $hls_fmp4_init_filename |
208
|
|
|
* @return HLS |
209
|
|
|
*/ |
210
|
|
|
public function setHlsFmp4InitFilename(string $hls_fmp4_init_filename): HLS |
211
|
|
|
{ |
212
|
|
|
$this->hls_fmp4_init_filename = $hls_fmp4_init_filename; |
213
|
|
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function getHlsFmp4InitFilename(): string |
220
|
|
|
{ |
221
|
|
|
return $this->hls_fmp4_init_filename; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return Filter |
226
|
|
|
*/ |
227
|
|
|
protected function getFilter(): Filter |
228
|
|
|
{ |
229
|
|
|
return new HLSFilter($this); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return string |
234
|
|
|
*/ |
235
|
|
|
protected function getPath(): string |
236
|
|
|
{ |
237
|
|
|
$path = $this->getFilePath(); |
238
|
|
|
$reps = $this->getRepresentations(); |
239
|
|
|
|
240
|
|
|
$this->savePlaylist($path . ".m3u8", $reps); |
241
|
|
|
|
242
|
|
|
return $path . "_" . end($reps)->getHeight() . "p.m3u8"; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param $path |
247
|
|
|
* @param $reps |
248
|
|
|
*/ |
249
|
|
|
private function savePlaylist(string $path, array $reps): void |
250
|
|
|
{ |
251
|
|
|
HLSPlaylist::save( |
252
|
|
|
$this->master_playlist ?? $path, |
253
|
|
|
$reps, |
254
|
|
|
pathinfo($path, PATHINFO_FILENAME), |
255
|
|
|
$this->stream_info |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
} |