Passed
Push — master ( 1ce02f...92fd2c )
by Amin
06:06
created

HLSPlaylist::getAudioBitrate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 1
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 3
rs 10
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
15
class HLSPlaylist
16
{
17
    /** @var HLS */
18
    private $hls;
19
20
    private const DEFAULT_AUDIO_BITRATE = 131072;
21
22
    /**
23
     * HLSPlaylist constructor.
24
     * @param HLS $hls
25
     */
26
    public function __construct(HLS $hls)
27
    {
28
        $this->hls = $hls;
29
    }
30
31
    /**
32
     * @param Representation $rep
33
     * @return string
34
     */
35
    private function segmentPath(Representation $rep): string
36
    {
37
        return $this->hls->pathInfo(PATHINFO_FILENAME) . "_" . $rep->getHeight() . "p.m3u8";
38
    }
39
40
    /**
41
     * @param Representation $rep
42
     * @return string
43
     */
44
    private function streamInfo(Representation $rep): string
45
    {
46
        $tag = '#EXT-X-STREAM-INF:';
47
        $params = array_merge(
48
            [
49
                "BANDWIDTH" => $rep->getKiloBitrate() * 1024 + $this->getAudioBitrate($rep),
50
                "RESOLUTION" => $rep->size2string(),
51
                "NAME" => "\"" . $rep->getHeight() . "\""
52
            ],
53
            $rep->getHlsStreamInfo()
54
        );
55
        Utiles::concatKeyValue($params, "=");
56
57
        return $tag . implode(",", $params);
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    private function getVersion(): string
64
    {
65
        $version = $this->hls->getHlsSegmentType() === "fmp4" ? 7 : 3;
66
        return "#EXT-X-VERSION:" . $version;
67
    }
68
69
    /**
70
     * @param array $description
71
     * @return string
72
     */
73
    private function contents(array $description): string
74
    {
75
        $content = array_merge(["#EXTM3U", $this->getVersion()], $description);
76
77
        foreach ($this->hls->getRepresentations() as $rep) {
78
            array_push($content, $this->streamInfo($rep), $this->segmentPath($rep));
79
        }
80
81
        return implode(PHP_EOL, $content);
82
    }
83
84
    /**
85
     * @param string $filename
86
     * @param array $description
87
     */
88
    public function save(string $filename, array $description): void
89
    {
90
        File::put($filename, $this->contents(($description)));
91
    }
92
93
    /**
94
     * @param Representation $rep
95
     * @return int
96
     */
97
    private function getAudioBitrate(Representation $rep): int
98
    {
99
        return $rep->getAudioKiloBitrate() ? $rep->getAudioKiloBitrate() * 1024 : $this->getOriginalAudioBitrate();
100
    }
101
102
    /**
103
     * @return int
104
     */
105
    private function getOriginalAudioBitrate(): int
106
    {
107
        return $this->hls
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hls->getMe...:DEFAULT_AUDIO_BITRATE) returns the type FFMpeg\Media\Video|Streaming\Media which is incompatible with the type-hinted return integer.
Loading history...
108
            ->getMedia()
109
            ->getStreams()
0 ignored issues
show
Bug introduced by
The method getStreams() does not exist on Streaming\Media. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

109
            ->/** @scrutinizer ignore-call */ getStreams()
Loading history...
110
            ->audios()
0 ignored issues
show
Bug introduced by
The method audios() does not exist on Streaming\Media. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

110
            ->/** @scrutinizer ignore-call */ audios()
Loading history...
Bug introduced by
The method audios() does not exist on FFMpeg\Media\Video. ( Ignorable by Annotation )

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

110
            ->/** @scrutinizer ignore-call */ audios()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
            ->first()
0 ignored issues
show
Bug introduced by
The method first() does not exist on FFMpeg\Media\Video. ( Ignorable by Annotation )

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

111
            ->/** @scrutinizer ignore-call */ first()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method first() does not exist on Streaming\Media. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

111
            ->/** @scrutinizer ignore-call */ first()
Loading history...
112
            ->get('bit_rate', static::DEFAULT_AUDIO_BITRATE);
0 ignored issues
show
Bug introduced by
The method get() does not exist on FFMpeg\Media\Video. ( Ignorable by Annotation )

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

112
            ->/** @scrutinizer ignore-call */ get('bit_rate', static::DEFAULT_AUDIO_BITRATE);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method get() does not exist on Streaming\Media. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

112
            ->/** @scrutinizer ignore-call */ get('bit_rate', static::DEFAULT_AUDIO_BITRATE);
Loading history...
113
    }
114
}