Passed
Push — master ( 220de3...37ea2e )
by Amin
13:48
created

HLS::setMasterPlaylist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
    /**
44
     * @return string
45
     */
46
    public function getTsSubDirectory(): string
47
    {
48
        return $this->ts_sub_directory;
49
    }
50
51
    /**
52
     * @param string $ts_sub_directory
53
     * @return HLS
54
     */
55
    public function setTsSubDirectory(string $ts_sub_directory)
56
    {
57
        $this->ts_sub_directory = $ts_sub_directory;
58
        return $this;
59
    }
60
61
    /**
62
     * @param string $hls_time
63
     * @return HLS
64
     */
65
    public function setHlsTime(string $hls_time): HLS
66
    {
67
        $this->hls_time = $hls_time;
68
        return $this;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getHlsTime(): string
75
    {
76
        return $this->hls_time;
77
    }
78
79
    /**
80
     * @param bool $hls_allow_cache
81
     * @return HLS
82
     */
83
    public function setHlsAllowCache(bool $hls_allow_cache): HLS
84
    {
85
        $this->hls_allow_cache = $hls_allow_cache;
86
        return $this;
87
    }
88
89
    /**
90
     * @return bool
91
     */
92
    public function isHlsAllowCache(): bool
93
    {
94
        return $this->hls_allow_cache;
95
    }
96
97
    /**
98
     * @param string $hls_key_info_file
99
     * @return HLS
100
     */
101
    public function setHlsKeyInfoFile(string $hls_key_info_file): HLS
102
    {
103
        $this->hls_key_info_file = $hls_key_info_file;
104
        return $this;
105
    }
106
107
    /**
108
     * @param string $save_to
109
     * @param string $url
110
     * @param int $length
111
     * @return HLS
112
     */
113
    public function encryption(string $save_to, string $url, int $length = 16): HLS
114
    {
115
        $this->setHlsKeyInfoFile(HLSKeyInfo::generate($save_to, $url, $length));
116
        $this->tmp_key_info_file = true;
117
118
        return $this;
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getHlsKeyInfoFile(): string
125
    {
126
        return $this->hls_key_info_file;
127
    }
128
129
    /**
130
     * @param string $hls_base_url
131
     * @return HLS
132
     */
133
    public function setHlsBaseUrl(string $hls_base_url): HLS
134
    {
135
        $this->hls_base_url = $hls_base_url;
136
        return $this;
137
    }
138
139
    /**
140
     * @return string
141
     */
142
    public function getHlsBaseUrl(): string
143
    {
144
        return $this->hls_base_url;
145
    }
146
147
    /**
148
     * @param int $hls_list_size
149
     * @return HLS
150
     */
151
    public function setHlsListSize(int $hls_list_size): HLS
152
    {
153
        $this->hls_list_size = $hls_list_size;
154
        return $this;
155
    }
156
157
    /**
158
     * @return int
159
     */
160
    public function getHlsListSize(): int
161
    {
162
        return $this->hls_list_size;
163
    }
164
165
    /**
166
     * @param string $master_playlist
167
     * @return HLS
168
     */
169
    public function setMasterPlaylist(string $master_playlist): HLS
170
    {
171
        $this->master_playlist = $master_playlist;
172
        return $this;
173
    }
174
175
    /**
176
     * @return Filter
177
     */
178
    protected function getFilter(): Filter
179
    {
180
        return new HLSFilter($this);
181
    }
182
183
    /**
184
     * @return string
185
     */
186
    protected function getPath(): string
187
    {
188
        $path = $this->getFilePath();
189
        $reps = $this->getRepresentations();
190
191
        $this->savePlaylist($path . ".m3u8", $reps);
192
193
        return $path . "_" . end($reps)->getHeight() . "p.m3u8";
194
    }
195
196
    /**
197
     * @param $path
198
     * @param $reps
199
     */
200
    private function savePlaylist($path, $reps)
201
    {
202
        $manifests = pathinfo($path, $this->master_playlist ? PATHINFO_DIRNAME : PATHINFO_FILENAME);
203
        HLSPlaylist::save($this->master_playlist ?? $path, $reps, $manifests);
204
    }
205
}