blob: e93eb3e62868fc5d195f94d57f50311fbddda065 [file] [log] [blame]
/**
* Copyright (C) 2022 Savoir-faire Linux Inc.
*
* Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "TranscriptMediaHandler.h"
#include "pluglog.h"
#include <string_view>
const char sep = separator();
const std::string TAG = "Transcript";
#define NAME "Transcript"
namespace jami {
TranscriptMediaHandler::TranscriptMediaHandler(std::string&& datapath, PluginPreferenceHandler* prefHandler)
: datapath_ {datapath}
{
aph_ = prefHandler;
setId(datapath_);
auto preferences = aph_->getPreferences("default");
videoSubscriber_ = std::make_shared<TranscriptVideoSubscriber>(datapath_);
audioSubscriber_ = std::make_shared<TranscriptAudioSubscriber>(datapath_, videoSubscriber_.get());
setParameters("default");
#ifdef __DEBUG__
auto it = preferences.find("subtitle");
if (it != preferences.end())
videoSubscriber_->setText(it->second);
#endif
}
void
TranscriptMediaHandler::notifyAVFrameSubject(const StreamData& data, jami::avSubjectPtr subject)
{
std::ostringstream oss;
std::string_view direction = data.direction ? "Receive" : "Preview";
oss << "NEW SUBJECT: [" << data.id << "," << direction << "]" << std::endl;
auto shouldUpdate = accountId_ != data.source;
accountId_ = data.source;
if (shouldUpdate)
setParameters(accountId_);
bool preferredStreamDirection = false; // false for output; true for input
preferredStreamDirection = aph_->getPreferences(accountId_, "avstream") == "in";
oss << "preferredStreamDirection " << preferredStreamDirection << std::endl;
if (data.type == StreamType::audio && !data.direction
&& data.direction == preferredStreamDirection) {
if (attached_[0]) {
attached_[0] = false;
audioSubscriber_->detach();
}
subject->attach(audioSubscriber_.get()); // your audio
oss << "got my sent audio attached" << std::endl;
attached_[0] = true;
} else if (data.type == StreamType::audio && data.direction
&& data.direction == preferredStreamDirection) {
if (attached_[0]) {
attached_[0] = false;
audioSubscriber_->detach();
}
subject->attach(audioSubscriber_.get()); // the audio you receive from others on the call
oss << "got received audio attached" << std::endl;
attached_[0] = true;
}
if (data.type == StreamType::video && !data.direction
&& data.direction == preferredStreamDirection) {
if (attached_[1]) {
attached_[1] = false;
videoSubscriber_->detach();
}
subject->attach(videoSubscriber_.get()); // your video
oss << "got my sent video attached" << std::endl;
attached_[1] = true;
} else if (data.type == StreamType::video && data.direction
&& data.direction == preferredStreamDirection) {
if (attached_[1]) {
attached_[1] = false;
videoSubscriber_->detach();
}
subject->attach(videoSubscriber_.get()); // the video you receive from others on the call
oss << "got received video attached" << std::endl;
attached_[1] = true;
}
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
}
void
TranscriptMediaHandler::setParameters(const std::string& accountId)
{
if (!accountId.empty() && accountId != accountId_)
return;
auto preferences = aph_->getPreferences(accountId_);
try {
videoSubscriber_->setParameter(preferences["fontsize"], Parameter::FONTSIZE);
videoSubscriber_->setParameter(preferences["background"], Parameter::BACKGROUND);
videoSubscriber_->setParameter(preferences["position"], Parameter::POSITION);
audioSubscriber_->setParameter(preferences["language"], Parameter::LANGUAGE);
} catch (std::exception& e) {
Plog::log(Plog::LogPriority::ERR, TAG, e.what());
}
}
std::map<std::string, std::string>
TranscriptMediaHandler::getCallMediaHandlerDetails()
{
return {{"name", NAME},
{"iconPath", datapath_ + sep + "icon.svg"},
{"pluginId", id()},
{"attached", attached_[0] && attached_[1] ? "1" : "0"},
{"dataType", "1"}};
}
void
TranscriptMediaHandler::detach()
{
attached_ = {false, false};
videoSubscriber_->detach();
audioSubscriber_->detach();
}
TranscriptMediaHandler::~TranscriptMediaHandler()
{
Plog::log(Plog::LogPriority::INFO, TAG, "~TranscriptMediaHandler from WhisperTranscript Plugin");
detach();
}
} // namespace jami