blob: 1bc9465a9ded71d352ddab6f708f6ab6a9169235 [file] [log] [blame]
agsantos5aa39652020-08-11 18:18:04 -04001/**
Sébastien Blincb783e32021-02-12 11:34:10 -05002 * Copyright (C) 2020-2021 Savoir-faire Linux Inc.
agsantos5aa39652020-08-11 18:18:04 -04003 *
4 * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
agsantosac1940d2020-09-17 10:18:40 -040018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA.
agsantos5aa39652020-08-11 18:18:04 -040020 */
21
22#include "pluginMediaHandler.h"
agsantosdd6a62a2021-03-29 17:13:27 -040023
24#include <pluglog.h>
agsantos82678f32020-12-09 15:03:24 -050025#include <string_view>
agsantos5aa39652020-08-11 18:18:04 -040026const char sep = separator();
27const std::string TAG = "FORESEG";
28
29#define NAME "Foreground Segmentation"
30
31namespace jami {
32
agsantos82678f32020-12-09 15:03:24 -050033PluginMediaHandler::PluginMediaHandler(std::map<std::string, std::string>&& preferences,
agsantosac1940d2020-09-17 10:18:40 -040034 std::string&& datapath)
agsantosdd6a62a2021-03-29 17:13:27 -040035 : dataPath_ {datapath}
agsantos82678f32020-12-09 15:03:24 -050036 , preferences_ {preferences}
agsantos5aa39652020-08-11 18:18:04 -040037{
agsantosdd6a62a2021-03-29 17:13:27 -040038 setId(dataPath_);
agsantos2c8525e2021-03-19 11:18:01 -040039#ifdef __ANDROID__
agsantosdd6a62a2021-03-29 17:13:27 -040040 mVS = std::make_shared<VideoSubscriber>(dataPath_ + sep + "model/mModel.ort",
agsantos2c8525e2021-03-19 11:18:01 -040041 preferences_.at("acceleration") == "1");
42#else
43#ifdef NVIDIA
agsantosdd6a62a2021-03-29 17:13:27 -040044 mVS = std::make_shared<VideoSubscriber>(dataPath_ + sep + "model/mModel.onnx",
agsantos2c8525e2021-03-19 11:18:01 -040045 preferences_.at("acceleration") == "1");
46#else
agsantosdd6a62a2021-03-29 17:13:27 -040047 mVS = std::make_shared<VideoSubscriber>(dataPath_ + sep + "model/mModel.onnx");
agsantos2c8525e2021-03-19 11:18:01 -040048#endif // NVIDIA
49#endif // ANDROID
agsantosdd6a62a2021-03-29 17:13:27 -040050 mVS->setBackground(preferences_.at("background"));
51 mVS->setBlur(preferences_.at("blur") == "1");
52 mVS->setBlurLevel(preferences_.at("blurlevel"));
agsantos5aa39652020-08-11 18:18:04 -040053}
54
55void
56PluginMediaHandler::notifyAVFrameSubject(const StreamData& data, jami::avSubjectPtr subject)
57{
agsantosac1940d2020-09-17 10:18:40 -040058 std::ostringstream oss;
agsantos82678f32020-12-09 15:03:24 -050059 std::string_view direction = data.direction ? "Receive" : "Preview";
agsantosac1940d2020-09-17 10:18:40 -040060 oss << "NEW SUBJECT: [" << data.id << "," << direction << "]" << std::endl;
agsantos5aa39652020-08-11 18:18:04 -040061
agsantosac1940d2020-09-17 10:18:40 -040062 bool preferredStreamDirection = false;
agsantos82678f32020-12-09 15:03:24 -050063 auto it = preferences_.find("streamslist");
64 if (it != preferences_.end()) {
agsantosc9181b42020-11-26 12:03:04 -050065 preferredStreamDirection = it->second == "in";
agsantosac1940d2020-09-17 10:18:40 -040066 }
67 oss << "preferredStreamDirection " << preferredStreamDirection << std::endl;
68 if (data.type == StreamType::video && !data.direction
69 && data.direction == preferredStreamDirection) {
agsantosdd6a62a2021-03-29 17:13:27 -040070 detach();
71 subject->attachPriorityObserver(mVS); // my image
agsantosac1940d2020-09-17 10:18:40 -040072 oss << "got my sent image attached" << std::endl;
agsantosc9181b42020-11-26 12:03:04 -050073 attached_ = '1';
agsantosac1940d2020-09-17 10:18:40 -040074 } else if (data.type == StreamType::video && data.direction
agsantosc9181b42020-11-26 12:03:04 -050075 && data.direction == preferredStreamDirection) {
agsantosdd6a62a2021-03-29 17:13:27 -040076 detach();
77 subject->attachPriorityObserver(mVS); // the image I receive from the others on the call
agsantos796b5af2020-12-22 19:38:27 -050078 oss << "got my received image attached" << std::endl;
agsantosc9181b42020-11-26 12:03:04 -050079 attached_ = '1';
80 }
agsantos5aa39652020-08-11 18:18:04 -040081
agsantosac1940d2020-09-17 10:18:40 -040082 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
agsantos5aa39652020-08-11 18:18:04 -040083}
84
85std::map<std::string, std::string>
86PluginMediaHandler::getCallMediaHandlerDetails()
87{
agsantosc9181b42020-11-26 12:03:04 -050088 return {{"name", NAME},
agsantosdd6a62a2021-03-29 17:13:27 -040089 {"iconPath", dataPath_ + sep + "icon.svg"},
agsantosc9181b42020-11-26 12:03:04 -050090 {"pluginId", id()},
91 {"attached", attached_},
92 {"dataType", "1"}};
agsantos5aa39652020-08-11 18:18:04 -040093}
94
95void
96PluginMediaHandler::setPreferenceAttribute(const std::string& key, const std::string& value)
97{
agsantos82678f32020-12-09 15:03:24 -050098 auto it = preferences_.find(key);
99 if (it != preferences_.end() && it->second != value) {
agsantosc9181b42020-11-26 12:03:04 -0500100 it->second = value;
101 if (key == "background") {
102 mVS->setBackground(value);
agsantosac1940d2020-09-17 10:18:40 -0400103 }
agsantosdd6a62a2021-03-29 17:13:27 -0400104 if (key == "blur") {
105 mVS->setBlur(value == "1");
106 }
107 if (key == "blurlevel") {
108 mVS->setBlurLevel(value);
109 }
agsantosac1940d2020-09-17 10:18:40 -0400110 }
agsantos5aa39652020-08-11 18:18:04 -0400111}
112
113bool
114PluginMediaHandler::preferenceMapHasKey(const std::string& key)
115{
agsantosdd6a62a2021-03-29 17:13:27 -0400116 return (key == "background" || key == "blur" || key == "blurlevel");
agsantos5aa39652020-08-11 18:18:04 -0400117}
118
119void
120PluginMediaHandler::detach()
121{
agsantosc9181b42020-11-26 12:03:04 -0500122 attached_ = '0';
agsantosac1940d2020-09-17 10:18:40 -0400123 mVS->detach();
agsantos5aa39652020-08-11 18:18:04 -0400124}
125
126PluginMediaHandler::~PluginMediaHandler()
127{
agsantosac1940d2020-09-17 10:18:40 -0400128 std::ostringstream oss;
129 oss << " ~FORESEG Plugin" << std::endl;
130 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
131 detach();
agsantos5aa39652020-08-11 18:18:04 -0400132}
agsantosac1940d2020-09-17 10:18:40 -0400133} // namespace jami