blob: 51b9edfa9a3756c4e9bb7d2df34ee0d153e6bab7 [file] [log] [blame]
agsantos5aa39652020-08-11 18:18:04 -04001/**
2 * Copyright (C) 2020 Savoir-faire Linux Inc.
3 *
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_);
agsantos796b5af2020-12-22 19:38:27 -050039 mVS = std::make_shared<VideoSubscriber>(datapath_, preferences_.at("modellist"), preferences_.at("background"), preferences_.at("acceleration") == "1");
agsantos5aa39652020-08-11 18:18:04 -040040}
41
42void
43PluginMediaHandler::notifyAVFrameSubject(const StreamData& data, jami::avSubjectPtr subject)
44{
agsantosac1940d2020-09-17 10:18:40 -040045 std::ostringstream oss;
agsantos82678f32020-12-09 15:03:24 -050046 std::string_view direction = data.direction ? "Receive" : "Preview";
agsantosac1940d2020-09-17 10:18:40 -040047 oss << "NEW SUBJECT: [" << data.id << "," << direction << "]" << std::endl;
agsantos5aa39652020-08-11 18:18:04 -040048
agsantosac1940d2020-09-17 10:18:40 -040049 bool preferredStreamDirection = false;
agsantos82678f32020-12-09 15:03:24 -050050 auto it = preferences_.find("streamslist");
51 if (it != preferences_.end()) {
agsantosc9181b42020-11-26 12:03:04 -050052 preferredStreamDirection = it->second == "in";
agsantosac1940d2020-09-17 10:18:40 -040053 }
54 oss << "preferredStreamDirection " << preferredStreamDirection << std::endl;
55 if (data.type == StreamType::video && !data.direction
56 && data.direction == preferredStreamDirection) {
57 subject->attach(mVS.get()); // my image
58 oss << "got my sent image attached" << std::endl;
agsantosc9181b42020-11-26 12:03:04 -050059 attached_ = '1';
agsantosac1940d2020-09-17 10:18:40 -040060 } else if (data.type == StreamType::video && data.direction
agsantosc9181b42020-11-26 12:03:04 -050061 && data.direction == preferredStreamDirection) {
agsantosac1940d2020-09-17 10:18:40 -040062 subject->attach(mVS.get()); // the image I receive from the others on the call
agsantos796b5af2020-12-22 19:38:27 -050063 oss << "got my received image attached" << std::endl;
agsantosc9181b42020-11-26 12:03:04 -050064 attached_ = '1';
65 }
agsantos5aa39652020-08-11 18:18:04 -040066
agsantosac1940d2020-09-17 10:18:40 -040067 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
agsantos5aa39652020-08-11 18:18:04 -040068}
69
70std::map<std::string, std::string>
71PluginMediaHandler::getCallMediaHandlerDetails()
72{
agsantosc9181b42020-11-26 12:03:04 -050073 return {{"name", NAME},
74 {"iconPath", datapath_ + sep + "icon.png"},
75 {"pluginId", id()},
76 {"attached", attached_},
77 {"dataType", "1"}};
agsantos5aa39652020-08-11 18:18:04 -040078}
79
80void
81PluginMediaHandler::setPreferenceAttribute(const std::string& key, const std::string& value)
82{
agsantos82678f32020-12-09 15:03:24 -050083 auto it = preferences_.find(key);
84 if (it != preferences_.end() && it->second != value) {
agsantosc9181b42020-11-26 12:03:04 -050085 it->second = value;
86 if (key == "background") {
87 mVS->setBackground(value);
agsantosac1940d2020-09-17 10:18:40 -040088 }
89 }
agsantos5aa39652020-08-11 18:18:04 -040090}
91
92bool
93PluginMediaHandler::preferenceMapHasKey(const std::string& key)
94{
agsantosac1940d2020-09-17 10:18:40 -040095 if (key == "background") {
96 return true;
97 }
98 return false;
agsantos5aa39652020-08-11 18:18:04 -040099}
100
101void
102PluginMediaHandler::detach()
103{
agsantosc9181b42020-11-26 12:03:04 -0500104 attached_ = '0';
agsantosac1940d2020-09-17 10:18:40 -0400105 mVS->detach();
agsantos5aa39652020-08-11 18:18:04 -0400106}
107
108PluginMediaHandler::~PluginMediaHandler()
109{
agsantosac1940d2020-09-17 10:18:40 -0400110 std::ostringstream oss;
111 oss << " ~FORESEG Plugin" << std::endl;
112 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
113 detach();
agsantos5aa39652020-08-11 18:18:04 -0400114}
agsantosac1940d2020-09-17 10:18:40 -0400115} // namespace jami