HLSFilter::initArgs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 14
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 18
rs 9.7998
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\Filters;
13
14
use Streaming\StreamInterface;
15
use Streaming\File;
16
use Streaming\Representation;
17
use Streaming\Utiles;
18
19
class HLSFilter extends FormatFilter
20
{
21
    /**  @var \Streaming\HLS */
22
    private $hls;
23
24
    /** @var string */
25
    private $dirname;
26
27
    /** @var string */
28
    private $filename;
29
30
    /** @var string */
31
    private $seg_sub_dir;
32
33
    /** @var string */
34
    private $base_url;
35
36
    /** @var string */
37
    private $seg_filename;
38
39
    /**
40
     * @param Representation $rep
41
     * @param bool $not_last
42
     * @return array
43
     */
44
    private function playlistPath(Representation $rep, bool $not_last): array
45
    {
46
        return $not_last ? [$this->dirname . "/" . $this->filename . "_" . $rep->getHeight() . "p.m3u8"] : [];
47
    }
48
49
    /**
50
     * @param Representation $rep
51
     * @return array
52
     */
53
    private function getAudioBitrate(Representation $rep): array
54
    {
55
        return $rep->getAudioKiloBitrate() ? ["b:a" => $rep->getAudioKiloBitrate() . "k"] : [];
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    private function getBaseURL(): array
62
    {
63
        return $this->base_url ? ["hls_base_url" => $this->base_url] : [];
64
    }
65
66
    private function flags(): array
67
    {
68
        return !empty($this->hls->getFlags()) ? ["hls_flags" => implode("+", $this->hls->getFlags())] : [];
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    private function getKeyInfo(): array
75
    {
76
        return $this->hls->getHlsKeyInfoFile() ? ["hls_key_info_file" => $this->hls->getHlsKeyInfoFile()] : [];
77
    }
78
79
    /**
80
     * @param Representation $rep
81
     * @return string
82
     */
83
    private function getInitFilename(Representation $rep): string
84
    {
85
        return $this->seg_sub_dir . $this->filename . "_" . $rep->getHeight() ."p_". $this->hls->getHlsFmp4InitFilename();
86
    }
87
88
    /**
89
     * @param Representation $rep
90
     * @return string
91
     */
92
    private function getSegmentFilename(Representation $rep): string
93
    {
94
        $ext = ($this->hls->getHlsSegmentType() === "fmp4") ? "m4s" : "ts";
95
        return $this->seg_filename . "_" . $rep->getHeight() . "p_%04d." . $ext;
96
    }
97
98
    /**
99
     * @param Representation $rep
100
     * @return array
101
     */
102
    private function initArgs(Representation $rep): array
103
    {
104
        $init = [
105
            "hls_list_size"             => $this->hls->getHlsListSize(),
106
            "hls_time"                  => $this->hls->getHlsTime(),
107
            "hls_allow_cache"           => (int)$this->hls->isHlsAllowCache(),
108
            "hls_segment_type"          => $this->hls->getHlsSegmentType(),
109
            "hls_fmp4_init_filename"    => $this->getInitFilename($rep),
110
            "hls_segment_filename"      => $this->getSegmentFilename($rep),
111
            "s:v"                       => $rep->size2string(),
112
            "b:v"                       => $rep->getKiloBitrate() . "k"
113
        ];
114
115
        return array_merge($init,
116
            $this->getAudioBitrate($rep),
117
            $this->getBaseURL(),
118
            $this->flags(),
119
            $this->getKeyInfo());
120
    }
121
122
    /**
123
     * @param Representation $rep
124
     * @param bool $not_last
125
     */
126
    private function getArgs(Representation $rep, bool $not_last): void
127
    {
128
        $this->filter = array_merge(
129
            $this->filter,
130
            $this->getFormatOptions($this->hls->getFormat()),
131
            Utiles::arrayToFFmpegOpt($this->initArgs($rep)),
132
            Utiles::arrayToFFmpegOpt($this->hls->getAdditionalParams()),
133
            ["-strict", $this->hls->getStrict()],
134
            $this->playlistPath($rep, $not_last)
135
        );
136
    }
137
138
    /**
139
     * set segments paths
140
     */
141
    private function segmentPaths()
142
    {
143
        if ($this->hls->getSegSubDirectory()) {
144
            File::makeDir($this->dirname . "/" . $this->hls->getSegSubDirectory() . "/");
145
        }
146
147
        $base = Utiles::appendSlash($this->hls->getHlsBaseUrl());
148
149
        $this->seg_sub_dir = Utiles::appendSlash($this->hls->getSegSubDirectory());
150
        $this->seg_filename = $this->dirname . "/" . $this->seg_sub_dir . $this->filename;
151
        $this->base_url = $base . $this->seg_sub_dir;
152
    }
153
154
    /**
155
     * set paths
156
     */
157
    private function setPaths(): void
158
    {
159
        $this->dirname = str_replace("\\", "/", $this->hls->pathInfo(PATHINFO_DIRNAME));
160
        $this->filename = $this->hls->pathInfo(PATHINFO_FILENAME);
161
        $this->segmentPaths();
162
    }
163
164
    /**
165
     * @param StreamInterface $stream
166
     * @return void
167
     */
168
    public function streamFilter(StreamInterface $stream): void
169
    {
170
        $this->hls = $stream;
0 ignored issues
show
Documentation Bug introduced by
$stream is of type Streaming\StreamInterface, but the property $hls was declared to be of type Streaming\HLS. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
171
        $this->setPaths();
172
        $reps = $this->hls->getRepresentations();
0 ignored issues
show
Bug introduced by
The method getRepresentations() does not exist on Streaming\StreamInterface. It seems like you code against a sub-type of Streaming\StreamInterface such as Streaming\Streaming. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
        /** @scrutinizer ignore-call */ 
173
        $reps = $this->hls->getRepresentations();
Loading history...
173
174
        foreach ($reps as $key => $rep) {
175
            $this->getArgs($rep, $reps->end() !== $rep);
176
        }
177
    }
178
}