2018-08-22 17:59:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Factories;
|
|
|
|
|
|
|
|
use App\Models\Song;
|
2024-02-04 20:31:01 +00:00
|
|
|
use App\Services\Streamers\LocalStreamerInterface;
|
|
|
|
use App\Services\Streamers\S3CompatibleStreamer;
|
2018-08-22 17:59:14 +00:00
|
|
|
use App\Services\Streamers\StreamerInterface;
|
2024-02-04 20:31:01 +00:00
|
|
|
use App\Services\Streamers\TranscodingStreamer;
|
2018-08-22 19:13:45 +00:00
|
|
|
use App\Services\TranscodingService;
|
2024-02-04 20:31:01 +00:00
|
|
|
use App\Values\SongStorageMetadata\S3CompatibleMetadata;
|
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
|
2019-07-04 19:50:53 +00:00
|
|
|
): StreamerInterface {
|
2024-02-04 20:31:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
$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
|
|
|
|
2024-02-04 20:31:01 +00:00
|
|
|
return $streamer;
|
2018-08-22 17:59:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 20:31:01 +00:00
|
|
|
return tap(
|
|
|
|
app(LocalStreamerInterface::class),
|
|
|
|
static fn (LocalStreamerInterface $streamer) => $streamer->setSong($song)
|
|
|
|
);
|
2018-08-22 17:59:14 +00:00
|
|
|
}
|
|
|
|
}
|