koel/app/Services/Streamers/StreamerFactory.php

51 lines
1.6 KiB
PHP
Raw Normal View History

2018-08-22 17:59:14 +00:00
<?php
namespace App\Services\Streamers;
2018-08-22 17:59:14 +00:00
use App\Exceptions\KoelPlusRequiredException;
2018-08-22 17:59:14 +00:00
use App\Models\Song;
2018-08-22 19:13:45 +00:00
use App\Services\TranscodingService;
use App\Values\SongStorageTypes;
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
): Streamer {
throw_unless(SongStorageTypes::supported($song->storage), KoelPlusRequiredException::class);
if ($song->storage === SongStorageTypes::S3 || $song->storage === SongStorageTypes::S3_LAMBDA) {
2024-02-05 11:50:06 +00:00
return self::makeStreamerFromClass(S3CompatibleStreamer::class, $song);
}
if ($song->storage === SongStorageTypes::DROPBOX) {
2024-02-05 11:50:06 +00:00
return self::makeStreamerFromClass(DropboxStreamer::class, $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 */
2024-02-05 11:50:06 +00:00
$streamer = self::makeStreamerFromClass(TranscodingStreamer::class, $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 self::makeStreamerFromClass(LocalStreamer::class, $song);
2024-02-05 11:50:06 +00:00
}
private static function makeStreamerFromClass(string $class, Song $song): Streamer
2024-02-05 11:50:06 +00:00
{
return tap(app($class), static fn (Streamer $streamer) => $streamer->setSong($song));
2018-08-22 17:59:14 +00:00
}
}