1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Usox\HyperSonic\FeatureSet\V1161\Method; |
6
|
|
|
|
7
|
|
|
use Usox\HyperSonic\Exception\ErrorCodeEnum; |
8
|
|
|
use Usox\HyperSonic\Exception\MethodCallFailedException; |
9
|
|
|
use Usox\HyperSonic\FeatureSet\V1161\Contract\StreamDataProviderInterface; |
10
|
|
|
use Usox\HyperSonic\FeatureSet\V1161\Responder\ResponderFactoryInterface; |
11
|
|
|
use Usox\HyperSonic\Response\ResponderInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Streams the requested file and respects the optional stream config parameters |
15
|
|
|
* |
16
|
|
|
* This class covers the `stream.view` method |
17
|
|
|
*/ |
18
|
|
|
final class StreamMethod implements V1161MethodInterface |
19
|
|
|
{ |
20
|
3 |
|
public function __construct( |
21
|
|
|
private readonly ResponderFactoryInterface $responderFactory, |
22
|
|
|
) { |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Calls the stream provider with optional transcoding config |
27
|
|
|
* |
28
|
|
|
* If the transcoding config is missing, the stream provider will |
29
|
|
|
* decides if a transcoding will be performed and uses its default |
30
|
|
|
* settings |
31
|
|
|
* |
32
|
|
|
* @see http://www.subsonic.org/pages/api.jsp#stream |
33
|
|
|
* |
34
|
|
|
* @param array<string, scalar> $queryParams |
35
|
|
|
* @param array<string, scalar> $args |
36
|
|
|
* |
37
|
|
|
* @throws MethodCallFailedException |
38
|
|
|
*/ |
39
|
2 |
|
public function __invoke( |
40
|
|
|
StreamDataProviderInterface $streamDataProvider, |
41
|
|
|
array $queryParams, |
42
|
|
|
array $args |
43
|
|
|
): ResponderInterface { |
44
|
2 |
|
$bitrate = (int) ($queryParams['maxBitRate'] ?? 0); |
45
|
2 |
|
if ($bitrate === 0) { |
46
|
1 |
|
$bitrate = null; |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
$format = (string) ($queryParams['format'] ?? ''); |
50
|
2 |
|
if ($format === '') { |
51
|
1 |
|
$format = null; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
$streamData = $streamDataProvider->stream( |
55
|
2 |
|
(string) ($queryParams['id'] ?? ''), |
56
|
|
|
$format, |
57
|
|
|
$bitrate |
58
|
|
|
); |
59
|
|
|
|
60
|
2 |
|
if ($streamData === null) { |
61
|
1 |
|
throw new MethodCallFailedException( |
62
|
|
|
ErrorCodeEnum::NOT_FOUND |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$estimateContentLength = (bool) ($queryParams['estimateContentLength'] ?? false); |
67
|
|
|
|
68
|
1 |
|
if ($estimateContentLength === true && $bitrate > 0) { |
69
|
1 |
|
$streamData['estimatedContentLength'] = $streamData['length'] * $bitrate * 1000 / 8; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return $this->responderFactory->createStreamResponder( |
73
|
|
|
$streamData, |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|