koel/app/Factories/StreamerFactory.php

50 lines
1.5 KiB
PHP
Raw Normal View History

2018-08-22 17:59:14 +00:00
<?php
namespace App\Factories;
use App\Models\Song;
use App\Services\Streamers\LocalStreamerInterface;
use App\Services\Streamers\S3CompatibleStreamer;
2018-08-22 17:59:14 +00:00
use App\Services\Streamers\StreamerInterface;
use App\Services\Streamers\TranscodingStreamer;
2018-08-22 19:13:45 +00:00
use App\Services\TranscodingService;
use App\Values\SongStorageMetadata\S3CompatibleMetadata;
2018-08-22 17:59:14 +00:00
class StreamerFactory
{
public function __construct(private TranscodingService $transcodingService)
{
2018-08-22 17:59:14 +00:00
}
2018-08-24 15:27:19 +00:00
public function createStreamer(
Song $song,
?bool $transcode = null,
?int $bitRate = null,
2019-06-30 14:22:53 +00:00
float $startTime = 0.0
2019-07-04 19:50:53 +00:00
): StreamerInterface {
if ($song->storage_metadata instanceof S3CompatibleMetadata) {
return tap(
app(S3CompatibleStreamer::class),
static fn (S3CompatibleStreamer $streamer) => $streamer->setSong($song)
);
2018-08-22 17:59:14 +00:00
}
$transcode ??= $this->transcodingService->songShouldBeTranscoded($song);
2018-08-22 17:59:14 +00:00
if ($transcode) {
/** @var TranscodingStreamer $streamer */
$streamer = app(TranscodingStreamer::class);
$streamer->setSong($song);
$streamer->setBitRate($bitRate ?: config('koel.streaming.bitrate'));
$streamer->setStartTime($startTime);
2018-08-22 17:59:14 +00:00
return $streamer;
2018-08-22 17:59:14 +00:00
}
return tap(
app(LocalStreamerInterface::class),
static fn (LocalStreamerInterface $streamer) => $streamer->setSong($song)
);
2018-08-22 17:59:14 +00:00
}
}