Skip to content
Snippets Groups Projects
Commit d09cc6d9 authored by Aline Gondim Santos's avatar Aline Gondim Santos
Browse files

tutorial: HelloWorld

Change-Id: Ie19a7749ba028ceda16a2479398645daeb08f277
parent 1e7736cc
Branches
No related tags found
No related merge requests found
Showing
with 1313 additions and 1 deletion
......@@ -8,4 +8,3 @@ config.mak
*__pycache__*
/foo/
/.vscode/
/HelloWorld/
cmake_minimum_required(VERSION 3.10)
# set the project name
set (ProjectName HelloWorld)
set (Version 1.0.0)
project(${ProjectName} VERSION ${Version})
set (DAEMON ${PROJECT_SOURCE_DIR}/../../daemon)
set (JPL_FILE_NAME ${ProjectName}.jpl)
set (DAEMON_SRC ${DAEMON}/src)
set (CONTRIB_PATH ${DAEMON}/contrib)
set (PLUGINS_LIB ${PROJECT_SOURCE_DIR}/../lib)
set (JPL_DIRECTORY ${PROJECT_BINARY_DIR}/jpl)
set (LIBS_DIR ${PROJECT_SOURCE_DIR}/../contrib/Libs)
if(WIN32)
message(OS:\ WINDOWS\ ${CMAKE_SYSTEM_PROCESSOR})
if (NOT ${CMAKE_CL_64})
message( FATAL_ERROR "\nUse CMake only for x64 Windows" )
endif()
set (CONTRIB_PLATFORM_CURT x64)
set (CONTRIB_PLATFORM ${CONTRIB_PLATFORM_CURT}-windows)
set (LIBRARY_FILE_NAME ${ProjectName}.dll)
set (FFMPEG ${CONTRIB_PATH}/build/ffmpeg/Build/win32/x64)
else()
message( FATAL_ERROR "\nUse CMake only for Windows! For linux or Android (linux host), use our bash scripts." )
endif()
message(Building:\ ${ProjectName}\ ${Version})
message(Build\ path:\ ${PROJECT_BINARY_DIR})
message(JPL\ assembling\ path:\ ${JPL_DIRECTORY})
message(JPL\ path:\ ${JPL_DIRECTORY}/../../../build/${ProjectName}/${JPL_FILE_NAME})
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
set(plugin_SRC CoinCircleMediaHandler.cpp
CenterCircleVideoSubscriber.cpp
CenterCircleMediaHandler.cpp
CoinCircleVideoSubscriber.cpp
main.cpp
../lib/accel.cpp
)
set(plugin_HDR CoinCircleVideoSubscriber.h
CenterCircleVideoSubscriber.h
CoinCircleMediaHandler.h
CenterCircleMediaHandler.h
../lib/accel.h
../lib/framescaler.h
../lib/pluglog.h
)
add_library(${ProjectName} SHARED ${plugin_SRC}
${plugin_HDR}
)
target_include_directories(${ProjectName} PUBLIC ${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}
${PLUGINS_LIB}
${DAEMON_SRC}
${CONTRIB_PATH}
${FFMPEG}/include
${CONTRIB_PATH}/build/opencv/build/install/include
)
target_link_directories(${ProjectName} PUBLIC ${CONTRIB_PATH}
${FFMPEG}/bin
${CONTRIB_PATH}/build/opencv/build/lib/Release
${CONTRIB_PATH}/build/opencv/build/3rdparty/lib/Release
)
target_link_libraries(${ProjectName} PUBLIC swscale avutil opencv_imgproc411 opencv_core411 zlib)
add_custom_command(
TARGET ${ProjectName}
PRE_BUILD
COMMAND python3 ${PROJECT_SOURCE_DIR}/../SDK/jplManipulation.py --preassemble --plugin=${ProjectName}
COMMENT "Assembling Plugin files"
)
add_custom_command(
TARGET ${ProjectName}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/Release/${ProjectName}.lib ${JPL_DIRECTORY}/lib/${CONTRIB_PLATFORM}
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/Release/${LIBRARY_FILE_NAME} ${JPL_DIRECTORY}/lib/${CONTRIB_PLATFORM}
COMMAND python3 ${PROJECT_SOURCE_DIR}/../SDK/jplManipulation.py --assemble --plugin=${ProjectName}
COMMENT "Generating JPL archive"
)
/**
* Copyright (C) 2020 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 "CenterCircleMediaHandler.h"
#include "pluglog.h"
const char sep = separator();
const std::string TAG = "CenterCircle";
#define NAME "CenterCircle"
namespace jami {
CenterCircleMediaHandler::CenterCircleMediaHandler(std::map<std::string, std::string>&& ppm,
std::string&& datapath)
: datapath_ {datapath}
, ppm_ {ppm}
{
setId(datapath_);
mVS = std::make_shared<CenterCircleVideoSubscriber>(datapath_);
auto it = ppm_.find("color");
if (it != ppm_.end()) {
mVS->setColor(it->second);
} else {
mVS->setColor("#0000FF");
}
}
void
CenterCircleMediaHandler::notifyAVFrameSubject(const StreamData& data, jami::avSubjectPtr subject)
{
Plog::log(Plog::LogPriority::INFO, TAG, "IN AVFRAMESUBJECT");
std::ostringstream oss;
std::string direction = data.direction ? "Receive" : "Preview";
oss << "NEW SUBJECT: [" << data.id << "," << direction << "]" << std::endl;
bool preferredStreamDirection = false; // false for output; true for input
auto it = ppm_.find("videostream");
if (it != ppm_.end()) {
preferredStreamDirection = it->second == "1";
}
oss << "preferredStreamDirection " << preferredStreamDirection << std::endl;
if (data.type == StreamType::video && !data.direction
&& data.direction == preferredStreamDirection) {
subject->attach(mVS.get()); // your image
oss << "got my sent image attached" << std::endl;
} else if (data.type == StreamType::video && data.direction
&& data.direction == preferredStreamDirection) {
subject->attach(mVS.get()); // the image you receive from others on the call
oss << "got received image attached" << std::endl;
}
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
}
std::map<std::string, std::string>
CenterCircleMediaHandler::getCallMediaHandlerDetails()
{
return {{"name", NAME}, {"iconPath", datapath_ + sep + "icon.png"}, {"pluginId", id()}};
}
void
CenterCircleMediaHandler::setPreferenceAttribute(const std::string& key, const std::string& value)
{
auto it = ppm_.find(key);
if (it != ppm_.end() && it->second != value) {
it->second = value;
if (key == "color") {
mVS->setColor(value);
return;
}
}
}
bool
CenterCircleMediaHandler::preferenceMapHasKey(const std::string& key)
{
if (key == "color") {
return true;
}
return false;
}
void
CenterCircleMediaHandler::detach()
{
mVS->detach();
}
CenterCircleMediaHandler::~CenterCircleMediaHandler()
{
std::ostringstream oss;
oss << " ~CenterCircleMediaHandler from HelloWorld Plugin" << std::endl;
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
detach();
}
} // namespace jami
/**
* Copyright (C) 2020 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.
*/
#pragma once
// Project
#include "CenterCircleVideoSubscriber.h"
// Plugin
#include "plugin/jamiplugin.h"
#include "plugin/mediahandler.h"
using avSubjectPtr = std::weak_ptr<jami::Observable<AVFrame*>>;
namespace jami {
class CenterCircleMediaHandler : public jami::CallMediaHandler
{
public:
CenterCircleMediaHandler(std::map<std::string, std::string>&& ppm, std::string&& dataPath);
~CenterCircleMediaHandler();
virtual void notifyAVFrameSubject(const StreamData& data, avSubjectPtr subject) override;
virtual std::map<std::string, std::string> getCallMediaHandlerDetails() override;
virtual void detach() override;
virtual void setPreferenceAttribute(const std::string& key, const std::string& value) override;
virtual bool preferenceMapHasKey(const std::string& key) override;
std::shared_ptr<CenterCircleVideoSubscriber> mVS;
const std::string& dataPath() const { return datapath_; }
private:
const std::string datapath_;
std::map<std::string, std::string> ppm_;
};
} // namespace jami
/**
* Copyright (C) 2020 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 "CenterCircleVideoSubscriber.h"
extern "C" {
#include <libavutil/display.h>
}
#include <accel.h>
// LOGGING
#include <pluglog.h>
#include <stdio.h>
#include <opencv2/imgproc.hpp>
const std::string TAG = "CenterCircle";
const char sep = separator();
namespace jami {
CenterCircleVideoSubscriber::CenterCircleVideoSubscriber(const std::string& dataPath)
: path_ {dataPath}
{}
CenterCircleVideoSubscriber::~CenterCircleVideoSubscriber()
{
std::ostringstream oss;
oss << "~CenterCircleMediaProcessor" << std::endl;
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
}
void
CenterCircleVideoSubscriber::update(jami::Observable<AVFrame*>*, AVFrame* const& iFrame)
{
if (!iFrame)
return;
AVFrame* pluginFrame = const_cast<AVFrame*>(iFrame);
//======================================================================================
// GET FRAME ROTATION
AVFrameSideData* side_data = av_frame_get_side_data(iFrame, AV_FRAME_DATA_DISPLAYMATRIX);
int angle {0};
if (side_data) {
auto matrix_rotation = reinterpret_cast<int32_t*>(side_data->data);
angle = static_cast<int>(av_display_rotation_get(matrix_rotation));
}
//======================================================================================
// GET RAW FRAME
// Use a non-const Frame
// Convert input frame to RGB
int inputHeight = pluginFrame->height;
int inputWidth = pluginFrame->width;
FrameUniquePtr bgrFrame = scaler.convertFormat(transferToMainMemory(pluginFrame,
AV_PIX_FMT_NV12),
AV_PIX_FMT_RGB24);
resultFrame = cv::Mat {bgrFrame->height,
bgrFrame->width,
CV_8UC3,
bgrFrame->data[0],
static_cast<size_t>(bgrFrame->linesize[0])};
// First clone the frame as the original one is unusable because of
// linespace
processingFrame = resultFrame.clone();
if (firstRun) {
// we set were the circle will be draw.
circlePos.y = static_cast<int>(inputHeight / 2);
circlePos.x = static_cast<int>(inputWidth / 2);
int w = resultFrame.size().width;
int h = resultFrame.size().height;
radius = std::min(w, h) / 8;
firstRun = false;
}
drawCenterCircle();
copyByLine(bgrFrame->linesize[0]);
//======================================================================================
// REPLACE AVFRAME DATA WITH FRAME DATA
if (bgrFrame && bgrFrame->data[0]) {
uint8_t* frameData = bgrFrame->data[0];
if (angle == 90 || angle == -90) {
std::memmove(frameData,
resultFrame.data,
static_cast<size_t>(pluginFrame->width * pluginFrame->height * 3)
* sizeof(uint8_t));
}
}
// Copy Frame meta data
if (bgrFrame && pluginFrame) {
av_frame_copy_props(bgrFrame.get(), pluginFrame);
scaler.moveFrom(pluginFrame, bgrFrame.get());
}
// Remove the pointer
pluginFrame = nullptr;
}
void
CenterCircleVideoSubscriber::setColor(const std::string& color)
{
int r, g, b = 0;
std::sscanf(color.c_str(), "#%02x%02x%02x", &r, &g, &b);
baseColor = cv::Scalar(r, g, b);
Plog::log(Plog::LogPriority::INFO, TAG, "Color set to: " + color);
}
void
CenterCircleVideoSubscriber::copyByLine(const int lineSize)
{
if (3 * processingFrame.cols == lineSize) {
std::memcpy(resultFrame.data,
processingFrame.data,
processingFrame.rows * processingFrame.cols * 3);
} else {
int rows = processingFrame.rows;
int offset = 0;
int frameOffset = 0;
for (int i = 0; i < rows; i++) {
std::memcpy(resultFrame.data + offset, processingFrame.data + frameOffset, lineSize);
offset += lineSize;
frameOffset += 3 * processingFrame.cols;
}
}
}
void
CenterCircleVideoSubscriber::drawCenterCircle()
{
if (!processingFrame.empty()) {
cv::circle(processingFrame, circlePos, radius, baseColor, cv::FILLED);
}
}
void
CenterCircleVideoSubscriber::attached(jami::Observable<AVFrame*>* observable)
{
std::ostringstream oss;
oss << "::Attached ! " << std::endl;
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
observable_ = observable;
}
void
CenterCircleVideoSubscriber::detached(jami::Observable<AVFrame*>*)
{
firstRun = true;
observable_ = nullptr;
std::ostringstream oss;
oss << "::Detached()" << std::endl;
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
}
void
CenterCircleVideoSubscriber::detach()
{
if (observable_) {
firstRun = true;
std::ostringstream oss;
oss << "::Calling detach()" << std::endl;
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
observable_->detach(this);
}
}
} // namespace jami
/**
* Copyright (C) 2020 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.
*/
#pragma once
// AvFrame
extern "C" {
#include <libavutil/frame.h>
}
#include <observer.h>
// Frame Scaler
#include <framescaler.h>
#include <opencv2/core.hpp>
namespace jami {
class CenterCircleVideoSubscriber : public jami::Observer<AVFrame*>
{
public:
CenterCircleVideoSubscriber(const std::string& dataPath);
~CenterCircleVideoSubscriber();
virtual void update(jami::Observable<AVFrame*>*, AVFrame* const&) override;
virtual void attached(jami::Observable<AVFrame*>*) override;
virtual void detached(jami::Observable<AVFrame*>*) override;
void detach();
void setColor(const std::string& color);
private:
// Observer pattern
Observable<AVFrame*>* observable_ = nullptr;
// Data
std::string path_;
FrameScaler scaler;
// Status variables of the processing
bool firstRun {true};
// define custom variables
void copyByLine(const int lineSize);
void drawCenterCircle();
cv::Point circlePos;
cv::Mat processingFrame;
cv::Mat resultFrame;
int radius;
cv::Scalar baseColor;
};
} // namespace jami
/**
* Copyright (C) 2020 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 "CoinCircleMediaHandler.h"
// Logger
#include "pluglog.h"
const char sep = separator();
const std::string TAG = "CoinCircle";
#define NAME "CoinCircle"
namespace jami {
CoinCircleMediaHandler::CoinCircleMediaHandler(std::map<std::string, std::string>&& ppm,
std::string&& datapath)
: datapath_ {datapath}
, ppm_ {ppm}
{
setId(datapath_);
mVS = std::make_shared<CoinCircleVideoSubscriber>(datapath_);
auto it = ppm_.find("color");
if (it != ppm_.end()) {
mVS->setColor(it->second);
} else {
mVS->setColor("#0000FF");
}
}
void
CoinCircleMediaHandler::notifyAVFrameSubject(const StreamData& data, jami::avSubjectPtr subject)
{
Plog::log(Plog::LogPriority::INFO, TAG, "IN AVFRAMESUBJECT");
std::ostringstream oss;
std::string direction = data.direction ? "Receive" : "Preview";
oss << "NEW SUBJECT: [" << data.id << "," << direction << "]" << std::endl;
bool preferredStreamDirection = false; // false for output; true for input
auto it = ppm_.find("videostream");
if (it != ppm_.end()) {
preferredStreamDirection = it->second == "1";
}
oss << "preferredStreamDirection " << preferredStreamDirection << std::endl;
if (data.type == StreamType::video && !data.direction
&& data.direction == preferredStreamDirection) {
subject->attach(mVS.get()); // your image
oss << "got my sent image attached" << std::endl;
} else if (data.type == StreamType::video && data.direction
&& data.direction == preferredStreamDirection) {
subject->attach(mVS.get()); // the image you receive from others on the call
oss << "got received image attached" << std::endl;
}
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
}
std::map<std::string, std::string>
CoinCircleMediaHandler::getCallMediaHandlerDetails()
{
return {{"name", NAME}, {"iconPath", datapath_ + sep + "icon.png"}, {"pluginId", id()}};
}
void
CoinCircleMediaHandler::setPreferenceAttribute(const std::string& key, const std::string& value)
{
auto it = ppm_.find(key);
if (it != ppm_.end() && it->second != value) {
it->second = value;
}
}
bool
CoinCircleMediaHandler::preferenceMapHasKey(const std::string& key)
{
return false;
}
void
CoinCircleMediaHandler::detach()
{
mVS->detach();
}
CoinCircleMediaHandler::~CoinCircleMediaHandler()
{
std::ostringstream oss;
oss << " ~CoinCircleMediaHandler from HelloWorld Plugin" << std::endl;
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
detach();
}
} // namespace jami
/**
* Copyright (C) 2020 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.
*/
#pragma once
// Project
#include "CoinCircleVideoSubscriber.h"
// Plugin
#include "plugin/jamiplugin.h"
#include "plugin/mediahandler.h"
using avSubjectPtr = std::weak_ptr<jami::Observable<AVFrame*>>;
namespace jami {
class CoinCircleMediaHandler : public jami::CallMediaHandler
{
public:
CoinCircleMediaHandler(std::map<std::string, std::string>&& ppm, std::string&& dataPath);
~CoinCircleMediaHandler();
virtual void notifyAVFrameSubject(const StreamData& data, avSubjectPtr subject) override;
virtual std::map<std::string, std::string> getCallMediaHandlerDetails() override;
virtual void detach() override;
virtual void setPreferenceAttribute(const std::string& key, const std::string& value) override;
virtual bool preferenceMapHasKey(const std::string& key) override;
std::shared_ptr<CoinCircleVideoSubscriber> mVS;
const std::string& dataPath() const { return datapath_; }
private:
const std::string datapath_;
std::map<std::string, std::string> ppm_;
};
} // namespace jami
/**
* Copyright (C) 2020 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 "CoinCircleVideoSubscriber.h"
extern "C" {
#include <libavutil/display.h>
}
#include <accel.h>
// LOGGING
#include <pluglog.h>
#include <stdio.h>
#include <opencv2/imgproc.hpp>
const std::string TAG = "CoinCircle";
const char sep = separator();
namespace jami {
CoinCircleVideoSubscriber::CoinCircleVideoSubscriber(const std::string& dataPath)
: path_ {dataPath}
{}
CoinCircleVideoSubscriber::~CoinCircleVideoSubscriber()
{
std::ostringstream oss;
oss << "~CoinCircleMediaProcessor" << std::endl;
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
}
void
CoinCircleVideoSubscriber::update(jami::Observable<AVFrame*>*, AVFrame* const& iFrame)
{
if (!iFrame)
return;
AVFrame* pluginFrame = const_cast<AVFrame*>(iFrame);
//======================================================================================
// GET FRAME ROTATION
AVFrameSideData* side_data = av_frame_get_side_data(iFrame, AV_FRAME_DATA_DISPLAYMATRIX);
int angle {0};
if (side_data) {
auto matrix_rotation = reinterpret_cast<int32_t*>(side_data->data);
angle = static_cast<int>(av_display_rotation_get(matrix_rotation));
}
//======================================================================================
// GET RAW FRAME
// Use a non-const Frame
// Convert input frame to RGB
int inputHeight = pluginFrame->height;
int inputWidth = pluginFrame->width;
FrameUniquePtr bgrFrame = scaler.convertFormat(transferToMainMemory(pluginFrame,
AV_PIX_FMT_NV12),
AV_PIX_FMT_RGB24);
resultFrame = cv::Mat {bgrFrame->height,
bgrFrame->width,
CV_8UC3,
bgrFrame->data[0],
static_cast<size_t>(bgrFrame->linesize[0])};
// First clone the frame as the original one is unusable because of
// linespace
processingFrame = resultFrame.clone();
if (firstRun) {
// we set were the circle will be draw.
int w = resultFrame.size().width;
int h = resultFrame.size().height;
radius = std::min(w, h) / 8;
circlePos.y = static_cast<int>(radius);
circlePos.x = static_cast<int>(radius);
firstRun = false;
}
drawCoinCircle(angle);
copyByLine(bgrFrame->linesize[0]);
//======================================================================================
// REPLACE AVFRAME DATA WITH FRAME DATA
if (bgrFrame && bgrFrame->data[0]) {
uint8_t* frameData = bgrFrame->data[0];
if (angle == 90 || angle == -90) {
std::memmove(frameData,
resultFrame.data,
static_cast<size_t>(pluginFrame->width * pluginFrame->height * 3)
* sizeof(uint8_t));
}
}
// Copy Frame meta data
if (bgrFrame && pluginFrame) {
av_frame_copy_props(bgrFrame.get(), pluginFrame);
scaler.moveFrom(pluginFrame, bgrFrame.get());
}
// Remove the pointer
pluginFrame = nullptr;
}
void
CoinCircleVideoSubscriber::drawCoinCircle(const int angle)
{
if (!processingFrame.empty()) {
rotateFrame(angle, processingFrame);
cv::circle(processingFrame, circlePos, radius, baseColor, cv::FILLED);
rotateFrame(-angle, processingFrame);
}
}
void
CoinCircleVideoSubscriber::rotateFrame(const int angle, cv::Mat& frame)
{
if (angle == -90)
cv::rotate(frame, frame, cv::ROTATE_90_COUNTERCLOCKWISE);
else if (std::abs(angle) == 180)
cv::rotate(frame, frame, cv::ROTATE_180);
else if (angle == 90)
cv::rotate(frame, frame, cv::ROTATE_90_CLOCKWISE);
}
void
CoinCircleVideoSubscriber::setColor(const std::string& color)
{
int r, g, b = 0;
std::sscanf(color.c_str(), "#%02x%02x%02x", &r, &g, &b);
baseColor = cv::Scalar(r, g, b);
Plog::log(Plog::LogPriority::INFO, TAG, "Color set to: " + color);
}
void
CoinCircleVideoSubscriber::copyByLine(const int lineSize)
{
if (3 * processingFrame.cols == lineSize) {
std::memcpy(resultFrame.data,
processingFrame.data,
processingFrame.rows * processingFrame.cols * 3);
} else {
int rows = processingFrame.rows;
int offset = 0;
int frameOffset = 0;
for (int i = 0; i < rows; i++) {
std::memcpy(resultFrame.data + offset, processingFrame.data + frameOffset, lineSize);
offset += lineSize;
frameOffset += 3 * processingFrame.cols;
}
}
}
void
CoinCircleVideoSubscriber::attached(jami::Observable<AVFrame*>* observable)
{
std::ostringstream oss;
oss << "::Attached ! " << std::endl;
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
observable_ = observable;
}
void
CoinCircleVideoSubscriber::detached(jami::Observable<AVFrame*>*)
{
firstRun = true;
observable_ = nullptr;
std::ostringstream oss;
oss << "::Detached()" << std::endl;
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
}
void
CoinCircleVideoSubscriber::detach()
{
if (observable_) {
firstRun = true;
std::ostringstream oss;
oss << "::Calling detach()" << std::endl;
Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
observable_->detach(this);
}
}
} // namespace jami
/**
* Copyright (C) 2020 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.
*/
#pragma once
// AvFrame
extern "C" {
#include <libavutil/frame.h>
}
#include <observer.h>
// Frame Scaler
#include <framescaler.h>
#include <opencv2/core.hpp>
namespace jami {
class CoinCircleVideoSubscriber : public jami::Observer<AVFrame*>
{
public:
CoinCircleVideoSubscriber(const std::string& dataPath);
~CoinCircleVideoSubscriber();
virtual void update(jami::Observable<AVFrame*>*, AVFrame* const&) override;
virtual void attached(jami::Observable<AVFrame*>*) override;
virtual void detached(jami::Observable<AVFrame*>*) override;
void detach();
void setColor(const std::string& color);
private:
// Observer pattern
Observable<AVFrame*>* observable_ = nullptr;
// Data
std::string path_;
FrameScaler scaler;
// Status variables of the processing
bool firstRun {true};
// define custom variables
void copyByLine(const int lineSize);
void drawCoinCircle(const int angle);
void rotateFrame(const int angle, cv::Mat& frame);
cv::Point circlePos;
cv::Mat processingFrame;
cv::Mat resultFrame;
int radius;
cv::Scalar baseColor;
};
} // namespace jami
#! /bin/bash
# Build the plugin for the project
set -e
export OSTYPE
ARCH=$(arch)
EXTRAPATH=''
# Flags:
# -p: number of processors to use
# -c: Runtime plugin cpu/gpu setting.
# -t: target platform.
if [ -z "${DAEMON}" ]; then
DAEMON="./../../daemon"
echo "DAEMON not provided, building with ${DAEMON}"
fi
PLUGIN_NAME="HelloWorld"
JPL_FILE_NAME="${PLUGIN_NAME}.jpl"
SO_FILE_NAME="lib${PLUGIN_NAME}.so"
DAEMON_SRC="${DAEMON}/src"
CONTRIB_PATH="${DAEMON}/contrib"
PLUGINS_LIB="../lib"
LIBS_DIR="./../contrib/Libs"
if [ -z "${PLATFORM}" ]; then
PLATFORM="linux-gnu"
echo "PLATFORM not provided, building with ${PLATFORM}"
echo "Other options: redhat-linux"
fi
while getopts t:c:p OPT; do
case "$OPT" in
t)
PLATFORM="${OPTARG}"
;;
c)
PROCESSOR="${OPTARG}"
;;
p)
;;
\?)
exit 1
;;
esac
done
if [ "${PLATFORM}" = "linux-gnu" ] || [ "${PLATFORM}" = "redhat-linux" ]
then
python3 ./../SDK/jplManipulation.py --preassemble --plugin=${PLUGIN_NAME}
CONTRIB_PLATFORM_CURT=${ARCH}
CONTRIB_PLATFORM=${CONTRIB_PLATFORM_CURT}-${PLATFORM}
# Compile
clang++ -std=c++17 -shared -fPIC \
-Wl,-Bsymbolic,-rpath,"\${ORIGIN}" \
-Wall -Wextra \
-Wno-unused-variable \
-Wno-unused-function \
-Wno-unused-parameter \
-I"." \
-I"${DAEMON_SRC}" \
-I"${CONTRIB_PATH}/${CONTRIB_PLATFORM}/include" \
-I"${CONTRIB_PATH}/${CONTRIB_PLATFORM}/include/opencv4" \
-I"${PLUGINS_LIB}" \
./../lib/accel.cpp \
CoinCircleMediaHandler.cpp \
CenterCircleVideoSubscriber.cpp \
CenterCircleMediaHandler.cpp \
CoinCircleVideoSubscriber.cpp \
main.cpp \
-L"${CONTRIB_PATH}/${CONTRIB_PLATFORM}/lib/" \
-l:libswscale.a \
-l:libavutil.a \
-lopencv_imgproc \
-lopencv_core \
-lva \
-o "build-local/jpl/lib/${CONTRIB_PLATFORM_CURT}-linux-gnu/${SO_FILE_NAME}"
elif [ "${PLATFORM}" = "android" ]
then
python3 ./../SDK/jplManipulation.py --preassemble --plugin=${PLUGIN_NAME} --distribution=${PLATFORM}
if [ -z "$ANDROID_NDK" ]; then
ANDROID_NDK="/home/${USER}/Android/Sdk/ndk/21.1.6352462"
echo "ANDROID_NDK not provided, building with ${ANDROID_NDK}"
fi
#=========================================================
# Check if the ANDROID_ABI was provided
# if not, set default
#=========================================================
if [ -z "$ANDROID_ABI" ]; then
ANDROID_ABI="armeabi-v7a arm64-v8a x86_64"
echo "ANDROID_ABI not provided, building for ${ANDROID_ABI}"
fi
buildlib() {
echo "$CURRENT_ABI"
#=========================================================
# ANDROID TOOLS
#=========================================================
export HOST_TAG=linux-x86_64
export TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/$HOST_TAG
if [ "$CURRENT_ABI" = armeabi-v7a ]
then
export AR=$TOOLCHAIN/bin/arm-linux-android-ar
export AS=$TOOLCHAIN/bin/arm-linux-android-as
export CC=$TOOLCHAIN/bin/armv7a-linux-androideabi21-clang
export CXX=$TOOLCHAIN/bin/armv7a-linux-androideabi21-clang++
export LD=$TOOLCHAIN/bin/arm-linux-android-ld
export RANLIB=$TOOLCHAIN/bin/arm-linux-android-ranlib
export STRIP=$TOOLCHAIN/bin/arm-linux-androideabi-strip
export ANDROID_SYSROOT=${DAEMON}/../client-android/android-toolchain-21-arm/sysroot
elif [ "$CURRENT_ABI" = arm64-v8a ]
then
export AR=$TOOLCHAIN/bin/aarch64-linux-android-ar
export AS=$TOOLCHAIN/bin/aarch64-linux-android-as
export CC=$TOOLCHAIN/bin/aarch64-linux-android21-clang
export CXX=$TOOLCHAIN/bin/aarch64-linux-android21-clang++
export LD=$TOOLCHAIN/bin/aarch64-linux-android-ld
export RANLIB=$TOOLCHAIN/bin/aarch64-linux-android-ranlib
export STRIP=$TOOLCHAIN/bin/aarch64-linux-android-strip
export ANDROID_SYSROOT=${DAEMON}/../client-android/android-toolchain-21-arm64/sysroot
elif [ "$CURRENT_ABI" = x86_64 ]
then
export AR=$TOOLCHAIN/bin/x86_64-linux-android-ar
export AS=$TOOLCHAIN/bin/x86_64-linux-android-as
export CC=$TOOLCHAIN/bin/x86_64-linux-android21-clang
export CXX=$TOOLCHAIN/bin/x86_64-linux-android21-clang++
export LD=$TOOLCHAIN/bin/x86_64-linux-android-ld
export RANLIB=$TOOLCHAIN/bin/x86_64-linux-android-ranlib
export STRIP=$TOOLCHAIN/bin/x86_64-linux-android-strip
export ANDROID_SYSROOT=${DAEMON}/../client-android/android-toolchain-21-x86_64/sysroot
else
echo "ABI NOT OK" >&2
exit 1
fi
#=========================================================
# CONTRIBS
#=========================================================
if [ "$CURRENT_ABI" = armeabi-v7a ]
then
CONTRIB_PLATFORM=arm-linux-androideabi
elif [ "$CURRENT_ABI" = arm64-v8a ]
then
CONTRIB_PLATFORM=aarch64-linux-android
elif [ "$CURRENT_ABI" = x86_64 ]
then
CONTRIB_PLATFORM=x86_64-linux-android
fi
#NDK SOURCES FOR cpufeatures
NDK_SOURCES=${ANDROID_NDK}/sources/android
#=========================================================
# LD_FLAGS
#=========================================================
if [ "$CURRENT_ABI" = armeabi-v7a ]
then
export EXTRA_LDFLAGS="${EXTRA_LDFLAGS} -L${ANDROID_SYSROOT}/usr/lib/arm-linux-androideabi -L${ANDROID_SYSROOT}/usr/lib/arm-linux-androideabi/21"
elif [ "$CURRENT_ABI" = arm64-v8a ]
then
export EXTRA_LDFLAGS="${EXTRA_LDFLAGS} -L${ANDROID_SYSROOT}/usr/lib/aarch64-linux-android -L${ANDROID_SYSROOT}/usr/lib/aarch64-linux-android/21"
elif [ "$CURRENT_ABI" = x86_64 ]
then
export EXTRA_LDFLAGS="${EXTRA_LDFLAGS} -L${ANDROID_SYSROOT}/usr/lib/x86_64-linux-android -L${ANDROID_SYSROOT}/usr/lib/x86_64-linux-android/21"
fi
#=========================================================
# Compile the plugin
#=========================================================
# Create so destination folder
$CXX --std=c++14 -O3 -g -fPIC \
-Wl,-Bsymbolic,-rpath,"\${ORIGIN}" \
-shared \
-Wall -Wextra \
-Wno-unused-variable \
-Wno-unused-function \
-Wno-unused-parameter \
-I"." \
-I"${DAEMON_SRC}" \
-I"${CONTRIB_PATH}/${CONTRIB_PLATFORM}/include" \
-I"${CONTRIB_PATH}/${CONTRIB_PLATFORM}/include/opencv4" \
-I"${PLUGINS_LIB}" \
./../lib/accel.cpp \
CoinCircleMediaHandler.cpp \
CenterCircleVideoSubscriber.cpp \
CenterCircleMediaHandler.cpp \
CoinCircleVideoSubscriber.cpp \
main.cpp \
-L"${CONTRIB_PATH}/${CONTRIB_PLATFORM}/lib/" \
-lswscale \
-lavutil \
-lopencv_imgproc \
-lopencv_core \
-llog -lz \
--sysroot=$ANDROID_SYSROOT \
-o "build-local/jpl/lib/$CURRENT_ABI/${SO_FILE_NAME}"
}
# Build the so
for i in ${ANDROID_ABI}; do
CURRENT_ABI=$i
buildlib
done
fi
python3 ./../SDK/jplManipulation.py --assemble --plugin=${PLUGIN_NAME} --distribution=${PLATFORM} --extraPath=${EXTRAPATH}
HelloWorld/data/icon.png

20.8 KiB

[
{
"category": "stream",
"type": "List",
"key": "videostream",
"title": "Video stream",
"summary": "select a video direction",
"defaultValue": "0",
"scope": "plugin",
"entryValues": [
"0",
"1"
],
"entries": [
"sent",
"received"
]
},
{
"category": "color",
"type": "List",
"key": "color",
"title": "Circle color",
"summary": "select a color",
"defaultValue": "#00FF00",
"scope": "plugin,CenterCircle",
"entryValues": [
"#0000FF",
"#00FF00",
"#FF0000"
],
"entries": [
"blue",
"green",
"red"
]
}
]
\ No newline at end of file
/**
* Copyright (C) 2020 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 <iostream>
#include <string.h>
#include <thread>
#include <memory>
#include <plugin/jamiplugin.h>
#include "CenterCircleMediaHandler.h"
#include "CoinCircleMediaHandler.h"
#ifdef WIN32
#define EXPORT_PLUGIN __declspec(dllexport)
#else
#define EXPORT_PLUGIN
#endif
#define HelloWorld_VERSION_MAJOR 1
#define HelloWorld_VERSION_MINOR 0
#define HelloWorld_VERSION_PATCH 0
extern "C" {
void
pluginExit(void)
{}
EXPORT_PLUGIN JAMI_PluginExitFunc
JAMI_dynPluginInit(const JAMI_PluginAPI* api)
{
std::cout << "**************************" << std::endl << std::endl;
std::cout << "** HelloWorld **" << std::endl;
std::cout << "**************************" << std::endl << std::endl;
std::cout << " Version " << HelloWorld_VERSION_MAJOR << "." << HelloWorld_VERSION_MINOR << "."
<< HelloWorld_VERSION_PATCH << std::endl;
// If invokeService doesn't return an error
if (api) {
std::map<std::string, std::string> ppm;
api->invokeService(api, "getPluginPreferences", &ppm);
std::string dataPath;
api->invokeService(api, "getPluginDataPath", &dataPath);
auto fmpCenterCircleMediaHandler
= std::make_unique<jami::CenterCircleMediaHandler>(std::move(ppm), std::move(dataPath));
if (api->manageComponent(api,
"CallMediaHandlerManager",
fmpCenterCircleMediaHandler.release())) {
return nullptr;
}
auto fmpCoinCircleMediaHandler
= std::make_unique<jami::CoinCircleMediaHandler>(std::move(ppm), std::move(dataPath));
if (api->manageComponent(api,
"CallMediaHandlerManager",
fmpCoinCircleMediaHandler.release())) {
return nullptr;
}
}
return pluginExit;
}
}
{
"name": "HelloWorld",
"description": "HelloWorld draws a circle in the center of a call's video",
"version": "1.0.0"
}
\ No newline at end of file
{
"name": "HelloWorld",
"version": "1.0.0",
"extractLibs": false,
"deps": [
"ffmpeg",
"opencv"
],
"defines": [],
"custom_scripts": {
"pre_build": [
"mkdir msvc"
],
"build": [
"cmake --build ./msvc --config Release"
],
"post_build": []
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment