2018-08-22 17:59:14 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-05 13:27:17 +00:00
|
|
|
namespace App\Services\Streamers;
|
2018-08-22 17:59:14 +00:00
|
|
|
|
2024-02-05 13:27:17 +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;
|
2024-02-05 21:17:41 +00:00
|
|
|
use App\Values\SongStorageTypes;
|
2018-08-22 17:59:14 +00:00
|
|
|
|
|
|
|
class StreamerFactory
|
|
|
|
{
|
2024-02-04 20:31:01 +00:00
|
|
|
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
|
2024-02-05 13:27:17 +00:00
|
|
|
): Streamer {
|
2024-02-05 21:17:41 +00:00
|
|
|
throw_unless(SongStorageTypes::supported($song->storage), KoelPlusRequiredException::class);
|
2024-02-05 13:27:17 +00:00
|
|
|
|
2024-02-05 21:17:41 +00:00
|
|
|
if ($song->storage === SongStorageTypes::S3 || $song->storage === SongStorageTypes::S3_LAMBDA) {
|
2024-02-05 11:50:06 +00:00
|
|
|
return self::makeStreamerFromClass(S3CompatibleStreamer::class, $song);
|
|
|
|
}
|
|
|
|
|
2024-02-05 21:17:41 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-02-04 20:31:01 +00:00
|
|
|
$transcode ??= $this->transcodingService->songShouldBeTranscoded($song);
|
2018-08-22 17:59:14 +00:00
|
|
|
|
|
|
|
if ($transcode) {
|
2024-02-04 20:31:01 +00:00
|
|
|
/** @var TranscodingStreamer $streamer */
|
2024-02-05 11:50:06 +00:00
|
|
|
$streamer = self::makeStreamerFromClass(TranscodingStreamer::class, $song);
|
2024-02-04 20:31:01 +00:00
|
|
|
$streamer->setBitRate($bitRate ?: config('koel.streaming.bitrate'));
|
|
|
|
$streamer->setStartTime($startTime);
|
2018-08-22 17:59:14 +00:00
|
|
|
|
2024-02-04 20:31:01 +00:00
|
|
|
return $streamer;
|
2018-08-22 17:59:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 13:27:17 +00:00
|
|
|
return self::makeStreamerFromClass(LocalStreamer::class, $song);
|
2024-02-05 11:50:06 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 13:27:17 +00:00
|
|
|
private static function makeStreamerFromClass(string $class, Song $song): Streamer
|
2024-02-05 11:50:06 +00:00
|
|
|
{
|
2024-02-05 13:27:17 +00:00
|
|
|
return tap(app($class), static fn (Streamer $streamer) => $streamer->setSong($song));
|
2018-08-22 17:59:14 +00:00
|
|
|
}
|
|
|
|
}
|