cmvr-es/cmvr-es/devices/speaker/speaker_factory.h

61 lines
1.7 KiB
C++

#ifndef CMVR_ES_SPEAKER_FACTORY_H
#define CMVR_ES_SPEAKER_FACTORY_H
#include <memory>
#include <stdexcept>
#include <string>
#include "cmvr/config/speaker_config/speaker_config.pb.h"
#include "common/base/logging/logger.h"
#include "devices/speaker/abstract_speaker.h"
#include "devices/speaker/ffmpeg_speaker/include/ffmpeg_speaker.h"
namespace cmvr::device {
class SpeakerFactory {
public:
static std::shared_ptr<AbstractSpeaker> create(const config::SpeakerDeviceConfig& cfg)
{
if (cfg.id().empty()) {
const std::string error = "[SpeakerFactory]: Speaker id is empty";
CMVR_LOG(ERROR) << error;
return nullptr;
}
switch (cfg.backend_case()) {
case config::SpeakerDeviceConfig::kFfmpeg:
return std::make_shared<ffmpegSpeaker>(
backendWithId_(cfg.id(), cfg.ffmpeg()));
case config::SpeakerDeviceConfig::BACKEND_NOT_SET:
default:
{
const std::string error =
"[SpeakerFactory]: Speaker backend is not configured: " + cfg.id();
CMVR_LOG(ERROR) << error;
return nullptr;
}
}
}
private:
static config::FFMpegSpeakerConfig backendWithId_(
const std::string& id,
const config::FFMpegSpeakerConfig& source)
{
if (!source.id().empty() && source.id() != id) {
const std::string error =
"[SpeakerFactory]: Speaker id does not match backend id: " + id;
CMVR_LOG(ERROR) << error;
return {};
}
auto backend = source;
backend.set_id(id);
return backend;
}
};
} // namespace cmvr::device
#endif // CMVR_ES_SPEAKER_FACTORY_H