Init audio context only on first jukebox use
This commit is contained in:
@@ -27,6 +27,7 @@
|
|||||||
#include <boost/asio/io_context.hpp>
|
#include <boost/asio/io_context.hpp>
|
||||||
#include <boost/asio/post.hpp>
|
#include <boost/asio/post.hpp>
|
||||||
|
|
||||||
|
#include "core/Exception.hpp"
|
||||||
#include "core/ILogger.hpp"
|
#include "core/ILogger.hpp"
|
||||||
#include "core/Random.hpp"
|
#include "core/Random.hpp"
|
||||||
|
|
||||||
@@ -45,14 +46,12 @@ namespace lms::jukebox
|
|||||||
}
|
}
|
||||||
|
|
||||||
JukeboxService::JukeboxService(db::IDb& db, audio::AudioOutputBackend backend)
|
JukeboxService::JukeboxService(db::IDb& db, audio::AudioOutputBackend backend)
|
||||||
: _ioContextRunner{ _ioContext, 1, "Jukebox" }
|
: _backend{ backend }
|
||||||
|
, _state{ ServiceState::Uninitialized }
|
||||||
|
, _ioContextRunner{ _ioContext, 1, "Jukebox" }
|
||||||
, _db{ db }
|
, _db{ db }
|
||||||
, _outputContext{ audio::createAudioOutputContext(_ioContext, "LMS-Jukebox", backend) }
|
|
||||||
{
|
{
|
||||||
LMS_LOG(JUKEBOX, INFO, "Starting service...");
|
LMS_LOG(JUKEBOX, INFO, "Starting service...");
|
||||||
|
|
||||||
// TODO create a context and an output stream only if a song is actually played
|
|
||||||
_outputContext->asyncWaitReady([this] { onContextReady(); });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JukeboxService::~JukeboxService()
|
JukeboxService::~JukeboxService()
|
||||||
@@ -65,12 +64,44 @@ namespace lms::jukebox
|
|||||||
LMS_LOG(JUKEBOX, INFO, "Service stopped!");
|
LMS_LOG(JUKEBOX, INFO, "Service stopped!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void JukeboxService::startInit()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
std::unique_lock lock{ _mutex };
|
||||||
|
|
||||||
|
checkState(ServiceState::Uninitialized);
|
||||||
|
|
||||||
|
LMS_LOG(JUKEBOX, INFO, "Starting audio initialization...");
|
||||||
|
|
||||||
|
_outputContext = audio::createAudioOutputContext(_ioContext, "LMS-Jukebox", _backend);
|
||||||
|
_state = ServiceState::Initializing;
|
||||||
|
|
||||||
|
// TODO create a context and an output stream only if a song is actually played
|
||||||
|
_outputContext->asyncWaitReady([this] { onContextReady(); });
|
||||||
|
}
|
||||||
|
catch (const audio::Exception& e)
|
||||||
|
{
|
||||||
|
LMS_LOG(JUKEBOX, ERROR, "Cannot create audio context: " << e.what());
|
||||||
|
_state = ServiceState::Failed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ServiceState JukeboxService::getState() const
|
||||||
|
{
|
||||||
|
std::unique_lock lock{ _mutex };
|
||||||
|
|
||||||
|
return _state;
|
||||||
|
}
|
||||||
|
|
||||||
void JukeboxService::play(std::size_t trackIndex, std::chrono::microseconds offset)
|
void JukeboxService::play(std::size_t trackIndex, std::chrono::microseconds offset)
|
||||||
{
|
{
|
||||||
LMS_LOG(JUKEBOX, INFO, "Playing track index " << trackIndex << " at offset " << std::format("{:%T}", offset));
|
LMS_LOG(JUKEBOX, DEBUG, "Playing track index " << trackIndex << " at offset " << std::format("{:%T}", offset));
|
||||||
|
|
||||||
std::unique_lock lock{ _mutex };
|
std::unique_lock lock{ _mutex };
|
||||||
|
|
||||||
|
checkState(ServiceState::Ready);
|
||||||
|
|
||||||
if (trackIndex >= _tracks.size())
|
if (trackIndex >= _tracks.size())
|
||||||
{
|
{
|
||||||
LMS_LOG(JUKEBOX, INFO, "Requested track index out of bound: stopping");
|
LMS_LOG(JUKEBOX, INFO, "Requested track index out of bound: stopping");
|
||||||
@@ -78,9 +109,6 @@ namespace lms::jukebox
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_outputStream)
|
|
||||||
return;
|
|
||||||
|
|
||||||
abortDecoder();
|
abortDecoder();
|
||||||
if (startDecoder(trackIndex, offset))
|
if (startDecoder(trackIndex, offset))
|
||||||
{
|
{
|
||||||
@@ -96,7 +124,8 @@ namespace lms::jukebox
|
|||||||
{
|
{
|
||||||
std::unique_lock lock{ _mutex };
|
std::unique_lock lock{ _mutex };
|
||||||
|
|
||||||
if (_outputStream)
|
checkState(ServiceState::Ready);
|
||||||
|
|
||||||
_outputStream->pause();
|
_outputStream->pause();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +133,8 @@ namespace lms::jukebox
|
|||||||
{
|
{
|
||||||
std::unique_lock lock{ _mutex };
|
std::unique_lock lock{ _mutex };
|
||||||
|
|
||||||
if (_outputStream)
|
checkState(ServiceState::Ready);
|
||||||
|
|
||||||
_outputStream->resume();
|
_outputStream->resume();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,8 +142,7 @@ namespace lms::jukebox
|
|||||||
{
|
{
|
||||||
std::shared_lock lock{ _mutex };
|
std::shared_lock lock{ _mutex };
|
||||||
|
|
||||||
if (!_outputStream)
|
checkState(ServiceState::Ready);
|
||||||
return true;
|
|
||||||
|
|
||||||
return _outputStream->isPaused();
|
return _outputStream->isPaused();
|
||||||
}
|
}
|
||||||
@@ -122,6 +151,8 @@ namespace lms::jukebox
|
|||||||
{
|
{
|
||||||
std::unique_lock lock{ _mutex };
|
std::unique_lock lock{ _mutex };
|
||||||
|
|
||||||
|
checkState(ServiceState::Ready);
|
||||||
|
|
||||||
_outputStream->setVolume(volume);
|
_outputStream->setVolume(volume);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,6 +160,8 @@ namespace lms::jukebox
|
|||||||
{
|
{
|
||||||
std::shared_lock lock{ _mutex };
|
std::shared_lock lock{ _mutex };
|
||||||
|
|
||||||
|
checkState(ServiceState::Ready);
|
||||||
|
|
||||||
return _outputStream->getVolume();
|
return _outputStream->getVolume();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,6 +169,8 @@ namespace lms::jukebox
|
|||||||
{
|
{
|
||||||
std::shared_lock lock{ _mutex };
|
std::shared_lock lock{ _mutex };
|
||||||
|
|
||||||
|
checkState(ServiceState::Ready);
|
||||||
|
|
||||||
return _currentTrackIndex;
|
return _currentTrackIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,8 +178,7 @@ namespace lms::jukebox
|
|||||||
{
|
{
|
||||||
std::shared_lock lock{ _mutex };
|
std::shared_lock lock{ _mutex };
|
||||||
|
|
||||||
if (!_outputStream)
|
checkState(ServiceState::Ready);
|
||||||
return {};
|
|
||||||
|
|
||||||
const auto playbackTime{ _outputStream->getPlaybackTime() };
|
const auto playbackTime{ _outputStream->getPlaybackTime() };
|
||||||
|
|
||||||
@@ -161,6 +195,8 @@ namespace lms::jukebox
|
|||||||
{
|
{
|
||||||
std::unique_lock lock{ _mutex };
|
std::unique_lock lock{ _mutex };
|
||||||
|
|
||||||
|
checkState(ServiceState::Ready);
|
||||||
|
|
||||||
_tracks.clear();
|
_tracks.clear();
|
||||||
_currentTrackIndex.reset();
|
_currentTrackIndex.reset();
|
||||||
}
|
}
|
||||||
@@ -169,6 +205,8 @@ namespace lms::jukebox
|
|||||||
{
|
{
|
||||||
std::unique_lock lock{ _mutex };
|
std::unique_lock lock{ _mutex };
|
||||||
|
|
||||||
|
checkState(ServiceState::Ready);
|
||||||
|
|
||||||
if (index >= _tracks.size())
|
if (index >= _tracks.size())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -185,10 +223,10 @@ namespace lms::jukebox
|
|||||||
|
|
||||||
void JukeboxService::appendTracks(std::span<const db::TrackId> tracks)
|
void JukeboxService::appendTracks(std::span<const db::TrackId> tracks)
|
||||||
{
|
{
|
||||||
LMS_LOG(JUKEBOX, INFO, "Appending " << tracks.size() << " tracks");
|
|
||||||
|
|
||||||
std::unique_lock lock{ _mutex };
|
std::unique_lock lock{ _mutex };
|
||||||
|
|
||||||
|
checkState(ServiceState::Ready);
|
||||||
|
|
||||||
_tracks.insert(std::end(_tracks), std::cbegin(tracks), std::cend(tracks));
|
_tracks.insert(std::end(_tracks), std::cbegin(tracks), std::cend(tracks));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,6 +234,8 @@ namespace lms::jukebox
|
|||||||
{
|
{
|
||||||
std::unique_lock lock{ _mutex };
|
std::unique_lock lock{ _mutex };
|
||||||
|
|
||||||
|
checkState(ServiceState::Ready);
|
||||||
|
|
||||||
core::random::shuffleContainer(_tracks);
|
core::random::shuffleContainer(_tracks);
|
||||||
|
|
||||||
// can't really determine the new pos if the song has been enqueued several times
|
// can't really determine the new pos if the song has been enqueued several times
|
||||||
@@ -206,31 +246,51 @@ namespace lms::jukebox
|
|||||||
{
|
{
|
||||||
std::shared_lock lock{ _mutex };
|
std::shared_lock lock{ _mutex };
|
||||||
|
|
||||||
|
checkState(ServiceState::Ready);
|
||||||
|
|
||||||
return _tracks;
|
return _tracks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void JukeboxService::checkState(ServiceState state) const
|
||||||
|
{
|
||||||
|
if (_state != state)
|
||||||
|
throw core::LmsException{ "Unexpected jukebox state!" };
|
||||||
|
}
|
||||||
|
|
||||||
void JukeboxService::onContextReady()
|
void JukeboxService::onContextReady()
|
||||||
|
{
|
||||||
|
std::unique_lock lock{ _mutex };
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
_outputStream = _outputContext->createOutputStream("LMS-jukebox", _pcmParams);
|
_outputStream = _outputContext->createOutputStream("LMS-jukebox", _pcmParams);
|
||||||
_outputStream->asyncWaitReady([this] { onStreamReady(); });
|
_outputStream->asyncWaitReady([this] { onStreamReady(); });
|
||||||
}
|
}
|
||||||
|
catch (const audio::Exception& e)
|
||||||
|
{
|
||||||
|
LMS_LOG(JUKEBOX, ERROR, "Cannot create audio context: " << e.what());
|
||||||
|
_state = ServiceState::Failed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void JukeboxService::onStreamReady()
|
void JukeboxService::onStreamReady()
|
||||||
{
|
{
|
||||||
|
LMS_LOG(JUKEBOX, INFO, "Audio initialization complete!");
|
||||||
|
|
||||||
audio::utils::PcmDecodeStreamerParameters params{
|
audio::utils::PcmDecodeStreamerParameters params{
|
||||||
.outputStream = *_outputStream,
|
.outputStream = *_outputStream,
|
||||||
.bufferCount = 3,
|
.bufferCount = 3,
|
||||||
.bufferDuration = std::chrono::milliseconds{ 100 },
|
.bufferDuration = std::chrono::milliseconds{ 100 },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::unique_lock lock{ _mutex };
|
||||||
|
|
||||||
_decoder = audio::utils::createPcmDecodeStreamer(_ioContext, params);
|
_decoder = audio::utils::createPcmDecodeStreamer(_ioContext, params);
|
||||||
|
_state = ServiceState::Ready;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool JukeboxService::startDecoder(std::size_t trackIndex, std::chrono::microseconds offset)
|
bool JukeboxService::startDecoder(std::size_t trackIndex, std::chrono::microseconds offset)
|
||||||
{
|
{
|
||||||
if (!_decoder)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
std::filesystem::path trackPath;
|
std::filesystem::path trackPath;
|
||||||
{
|
{
|
||||||
auto& session{ _db.getTLSSession() };
|
auto& session{ _db.getTLSSession() };
|
||||||
@@ -264,15 +324,12 @@ namespace lms::jukebox
|
|||||||
void JukeboxService::abortDecoder()
|
void JukeboxService::abortDecoder()
|
||||||
{
|
{
|
||||||
// Must not be called from within owned io_context
|
// Must not be called from within owned io_context
|
||||||
if (_decoder)
|
|
||||||
{
|
|
||||||
_decoder->abort();
|
_decoder->abort();
|
||||||
|
|
||||||
// Should be hopefully short since flushing/aborting
|
// Should be hopefully short since flushing/aborting
|
||||||
while (!_decoder->isComplete())
|
while (!_decoder->isComplete())
|
||||||
std::this_thread::yield(); // TODO: execute some io_context stuff?
|
std::this_thread::yield(); // TODO: execute some io_context stuff?
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void JukeboxService::onDecodeFinished(bool aborted)
|
void JukeboxService::onDecodeFinished(bool aborted)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -45,6 +45,9 @@ namespace lms::jukebox
|
|||||||
JukeboxService& operator=(const JukeboxService&) = delete;
|
JukeboxService& operator=(const JukeboxService&) = delete;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void startInit() override;
|
||||||
|
ServiceState getState() const override;
|
||||||
|
|
||||||
void play(std::size_t trackIndex, std::chrono::microseconds offset) override;
|
void play(std::size_t trackIndex, std::chrono::microseconds offset) override;
|
||||||
|
|
||||||
void pause() override;
|
void pause() override;
|
||||||
@@ -64,6 +67,8 @@ namespace lms::jukebox
|
|||||||
void shuffleTracks() override;
|
void shuffleTracks() override;
|
||||||
std::vector<db::TrackId> getTracks() const override;
|
std::vector<db::TrackId> getTracks() const override;
|
||||||
|
|
||||||
|
void checkState(ServiceState state) const;
|
||||||
|
|
||||||
void onContextReady();
|
void onContextReady();
|
||||||
void onStreamReady();
|
void onStreamReady();
|
||||||
|
|
||||||
@@ -82,6 +87,9 @@ namespace lms::jukebox
|
|||||||
|
|
||||||
mutable std::shared_mutex _mutex;
|
mutable std::shared_mutex _mutex;
|
||||||
|
|
||||||
|
const audio::AudioOutputBackend _backend;
|
||||||
|
ServiceState _state;
|
||||||
|
|
||||||
std::vector<db::TrackId> _tracks; // protected by mutex
|
std::vector<db::TrackId> _tracks; // protected by mutex
|
||||||
std::optional<std::size_t> _currentTrackIndex; // protected by mutex
|
std::optional<std::size_t> _currentTrackIndex; // protected by mutex
|
||||||
std::chrono::microseconds _currentTrackPlaybackTimeOffset{};
|
std::chrono::microseconds _currentTrackPlaybackTimeOffset{};
|
||||||
|
|||||||
@@ -37,11 +37,23 @@ namespace lms
|
|||||||
|
|
||||||
namespace lms::jukebox
|
namespace lms::jukebox
|
||||||
{
|
{
|
||||||
|
enum class ServiceState
|
||||||
|
{
|
||||||
|
Uninitialized, // init not attempted
|
||||||
|
Initializing, // init in progress
|
||||||
|
Ready, // init done
|
||||||
|
Failed, // unrecoverable
|
||||||
|
};
|
||||||
|
|
||||||
class IJukeboxService
|
class IJukeboxService
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~IJukeboxService() = default;
|
virtual ~IJukeboxService() = default;
|
||||||
|
|
||||||
|
virtual void startInit() = 0; // can be called only if state is Uninitialized
|
||||||
|
virtual ServiceState getState() const = 0;
|
||||||
|
|
||||||
|
// Can call all methods below only if ready!
|
||||||
virtual void play(std::size_t trackIndex, std::chrono::microseconds offset) = 0;
|
virtual void play(std::size_t trackIndex, std::chrono::microseconds offset) = 0;
|
||||||
|
|
||||||
virtual void pause() = 0;
|
virtual void pause() = 0;
|
||||||
|
|||||||
@@ -20,23 +20,43 @@
|
|||||||
#include "Jukebox.hpp"
|
#include "Jukebox.hpp"
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include "core/Service.hpp"
|
#include "core/Service.hpp"
|
||||||
|
|
||||||
#include "database/Session.hpp"
|
#include "database/Session.hpp"
|
||||||
#include "database/objects/Track.hpp"
|
#include "database/objects/Track.hpp"
|
||||||
#include "database/objects/User.hpp"
|
#include "database/objects/User.hpp"
|
||||||
#include "responses/Song.hpp"
|
|
||||||
#include "services/jukebox/IJukeboxService.hpp"
|
|
||||||
|
|
||||||
#include "ParameterParsing.hpp"
|
#include "ParameterParsing.hpp"
|
||||||
#include "SubsonicId.hpp"
|
#include "SubsonicId.hpp"
|
||||||
#include "SubsonicResponse.hpp"
|
#include "SubsonicResponse.hpp"
|
||||||
|
#include "responses/Song.hpp"
|
||||||
|
#include "services/jukebox/IJukeboxService.hpp"
|
||||||
|
|
||||||
namespace lms::api::subsonic
|
namespace lms::api::subsonic
|
||||||
{
|
{
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
|
void initJukeboxIfNeeded(jukebox::IJukeboxService& jukeboxService)
|
||||||
|
{
|
||||||
|
switch (jukeboxService.getState())
|
||||||
|
{
|
||||||
|
case jukebox::ServiceState::Uninitialized:
|
||||||
|
jukeboxService.startInit();
|
||||||
|
[[fallthrough]];
|
||||||
|
|
||||||
|
case jukebox::ServiceState::Initializing:
|
||||||
|
while (jukeboxService.getState() == jukebox::ServiceState::Initializing)
|
||||||
|
std::this_thread::yield(); // should be hopefully quite fast
|
||||||
|
break;
|
||||||
|
|
||||||
|
case jukebox::ServiceState::Failed:
|
||||||
|
case jukebox::ServiceState::Ready:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Response::Node createJukeboxStatusNode(const jukebox::IJukeboxService& jukeboxService)
|
Response::Node createJukeboxStatusNode(const jukebox::IJukeboxService& jukeboxService)
|
||||||
{
|
{
|
||||||
Response::Node statusNode;
|
Response::Node statusNode;
|
||||||
@@ -194,11 +214,25 @@ namespace lms::api::subsonic
|
|||||||
|
|
||||||
jukebox::IJukeboxService* jukeboxService{ core::Service<jukebox::IJukeboxService>::get() };
|
jukebox::IJukeboxService* jukeboxService{ core::Service<jukebox::IJukeboxService>::get() };
|
||||||
if (!jukeboxService)
|
if (!jukeboxService)
|
||||||
throw InternalErrorGenericError{ "Jukebox not available!" };
|
throw InternalErrorGenericError{ "Jukebox service disabled" };
|
||||||
|
|
||||||
if (!context.getUser()->isAdmin())
|
if (!context.getUser()->isAdmin())
|
||||||
throw UserNotAuthorizedError{};
|
throw UserNotAuthorizedError{};
|
||||||
|
|
||||||
|
detail::initJukeboxIfNeeded(*jukeboxService);
|
||||||
|
|
||||||
|
switch (jukeboxService->getState())
|
||||||
|
{
|
||||||
|
case jukebox::ServiceState::Failed:
|
||||||
|
throw InternalErrorGenericError{ "Jukebox service failed" };
|
||||||
|
|
||||||
|
case jukebox::ServiceState::Ready:
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw InternalErrorGenericError{ "Bad jukebox state" };
|
||||||
|
}
|
||||||
|
|
||||||
auto itActionHandler{ detail::actionHandlers.find(action) };
|
auto itActionHandler{ detail::actionHandlers.find(action) };
|
||||||
if (itActionHandler == std::end(detail::actionHandlers))
|
if (itActionHandler == std::end(detail::actionHandlers))
|
||||||
throw BadParameterGenericError{ "action" };
|
throw BadParameterGenericError{ "action" };
|
||||||
|
|||||||
Reference in New Issue
Block a user