blob: 2643d6ded3043ddf4d14024d5f4425f3a6f42209 [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"
23// Logger
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)
35 : datapath_ {datapath}
agsantos82678f32020-12-09 15:03:24 -050036 , preferences_ {preferences}
agsantos5aa39652020-08-11 18:18:04 -040037{
agsantosac1940d2020-09-17 10:18:40 -040038 setId(datapath_);
agsantos2c8525e2021-03-19 11:18:01 -040039#ifdef __ANDROID__
40 mVS = std::make_shared<VideoSubscriber>(datapath_,
41 "mModel.ort",
42 preferences_.at("background"),
43 preferences_.at("acceleration") == "1");
44#else
45#ifdef NVIDIA
46 mVS = std::make_shared<VideoSubscriber>(datapath_,
47 "mModel.onnx",
48 preferences_.at("background"),
49 preferences_.at("acceleration") == "1");
50#else
51 mVS = std::make_shared<VideoSubscriber>(datapath_, "mModel.onnx", preferences_.at("background"));
52#endif // NVIDIA
53#endif // ANDROID
agsantos5aa39652020-08-11 18:18:04 -040054}
55
56void
57PluginMediaHandler::notifyAVFrameSubject(const StreamData& data, jami::avSubjectPtr subject)
58{
agsantosac1940d2020-09-17 10:18:40 -040059 std::ostringstream oss;
agsantos82678f32020-12-09 15:03:24 -050060 std::string_view direction = data.direction ? "Receive" : "Preview";
agsantosac1940d2020-09-17 10:18:40 -040061 oss << "NEW SUBJECT: [" << data.id << "," << direction << "]" << std::endl;
agsantos5aa39652020-08-11 18:18:04 -040062
agsantosac1940d2020-09-17 10:18:40 -040063 bool preferredStreamDirection = false;
agsantos82678f32020-12-09 15:03:24 -050064 auto it = preferences_.find("streamslist");
65 if (it != preferences_.end()) {
agsantosc9181b42020-11-26 12:03:04 -050066 preferredStreamDirection = it->second == "in";
agsantosac1940d2020-09-17 10:18:40 -040067 }
68 oss << "preferredStreamDirection " << preferredStreamDirection << std::endl;
69 if (data.type == StreamType::video && !data.direction
70 && data.direction == preferredStreamDirection) {
71 subject->attach(mVS.get()); // my image
72 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) {
agsantosac1940d2020-09-17 10:18:40 -040076 subject->attach(mVS.get()); // the image I receive from the others on the call
agsantos796b5af2020-12-22 19:38:27 -050077 oss << "got my received image attached" << std::endl;
agsantosc9181b42020-11-26 12:03:04 -050078 attached_ = '1';
79 }
agsantos5aa39652020-08-11 18:18:04 -040080
agsantosac1940d2020-09-17 10:18:40 -040081 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
agsantos5aa39652020-08-11 18:18:04 -040082}
83
84std::map<std::string, std::string>
85PluginMediaHandler::getCallMediaHandlerDetails()
86{
agsantosc9181b42020-11-26 12:03:04 -050087 return {{"name", NAME},
agsantosd00bf412021-01-26 13:43:33 -050088 {"iconPath", datapath_ + sep + "icon.svg"},
agsantosc9181b42020-11-26 12:03:04 -050089 {"pluginId", id()},
90 {"attached", attached_},
91 {"dataType", "1"}};
agsantos5aa39652020-08-11 18:18:04 -040092}
93
94void
95PluginMediaHandler::setPreferenceAttribute(const std::string& key, const std::string& value)
96{
agsantos82678f32020-12-09 15:03:24 -050097 auto it = preferences_.find(key);
98 if (it != preferences_.end() && it->second != value) {
agsantosc9181b42020-11-26 12:03:04 -050099 it->second = value;
100 if (key == "background") {
101 mVS->setBackground(value);
agsantosac1940d2020-09-17 10:18:40 -0400102 }
103 }
agsantos5aa39652020-08-11 18:18:04 -0400104}
105
106bool
107PluginMediaHandler::preferenceMapHasKey(const std::string& key)
108{
agsantosac1940d2020-09-17 10:18:40 -0400109 if (key == "background") {
110 return true;
111 }
112 return false;
agsantos5aa39652020-08-11 18:18:04 -0400113}
114
115void
116PluginMediaHandler::detach()
117{
agsantosc9181b42020-11-26 12:03:04 -0500118 attached_ = '0';
agsantosac1940d2020-09-17 10:18:40 -0400119 mVS->detach();
agsantos5aa39652020-08-11 18:18:04 -0400120}
121
122PluginMediaHandler::~PluginMediaHandler()
123{
agsantosac1940d2020-09-17 10:18:40 -0400124 std::ostringstream oss;
125 oss << " ~FORESEG Plugin" << std::endl;
126 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
127 detach();
agsantos5aa39652020-08-11 18:18:04 -0400128}
agsantosac1940d2020-09-17 10:18:40 -0400129} // namespace jami