blob: 1b96c23d279a131e644c8664a8aad07491f9511f [file] [log] [blame]
agsantosd09cc6d2020-11-06 17:34:46 -05001/**
Sébastien Blincb783e32021-02-12 11:34:10 -05002 * Copyright (C) 2020-2021 Savoir-faire Linux Inc.
agsantosd09cc6d2020-11-06 17:34:46 -05003 *
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
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#include "CenterCircleVideoSubscriber.h"
22
23extern "C" {
24#include <libavutil/display.h>
25}
26#include <accel.h>
agsantosc9181b42020-11-26 12:03:04 -050027#include <frameUtils.h>
agsantos1bbc7cc2021-05-20 16:43:35 -040028#include <frameScaler.h>
agsantosd09cc6d2020-11-06 17:34:46 -050029#include <pluglog.h>
agsantosd09cc6d2020-11-06 17:34:46 -050030#include <stdio.h>
31#include <opencv2/imgproc.hpp>
32
33const std::string TAG = "CenterCircle";
34const char sep = separator();
35
36namespace jami {
37
38CenterCircleVideoSubscriber::CenterCircleVideoSubscriber(const std::string& dataPath)
39 : path_ {dataPath}
40{}
41
42CenterCircleVideoSubscriber::~CenterCircleVideoSubscriber()
43{
44 std::ostringstream oss;
45 oss << "~CenterCircleMediaProcessor" << std::endl;
46 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
47}
48
49void
agsantosc9181b42020-11-26 12:03:04 -050050CenterCircleVideoSubscriber::update(jami::Observable<AVFrame*>*, AVFrame* const& pluginFrame)
agsantosd09cc6d2020-11-06 17:34:46 -050051{
agsantosc9181b42020-11-26 12:03:04 -050052 if (!pluginFrame)
agsantosd09cc6d2020-11-06 17:34:46 -050053 return;
agsantosd09cc6d2020-11-06 17:34:46 -050054
55 //======================================================================================
56 // GET FRAME ROTATION
agsantosc9181b42020-11-26 12:03:04 -050057 AVFrameSideData* side_data = av_frame_get_side_data(pluginFrame, AV_FRAME_DATA_DISPLAYMATRIX);
agsantosd09cc6d2020-11-06 17:34:46 -050058
59 int angle {0};
60 if (side_data) {
61 auto matrix_rotation = reinterpret_cast<int32_t*>(side_data->data);
62 angle = static_cast<int>(av_display_rotation_get(matrix_rotation));
63 }
64
65 //======================================================================================
66 // GET RAW FRAME
67 // Use a non-const Frame
68 // Convert input frame to RGB
agsantos1bbc7cc2021-05-20 16:43:35 -040069 uniqueFramePtr rgbFrame = {transferToMainMemory(pluginFrame, AV_PIX_FMT_NV12), frameFree};
70 rgbFrame.reset(FrameScaler::convertFormat(rgbFrame.get(), AV_PIX_FMT_RGB24));
71 if (!rgbFrame.get())
agsantosc9181b42020-11-26 12:03:04 -050072 return;
agsantos1bbc7cc2021-05-20 16:43:35 -040073 resultFrame = cv::Mat {rgbFrame->height,
74 rgbFrame->width,
agsantosd09cc6d2020-11-06 17:34:46 -050075 CV_8UC3,
agsantos1bbc7cc2021-05-20 16:43:35 -040076 rgbFrame->data[0],
77 static_cast<size_t>(rgbFrame->linesize[0])};
agsantosd09cc6d2020-11-06 17:34:46 -050078
79 // First clone the frame as the original one is unusable because of
80 // linespace
81 processingFrame = resultFrame.clone();
82
83 if (firstRun) {
84 // we set were the circle will be draw.
agsantos1bbc7cc2021-05-20 16:43:35 -040085 circlePos.y = static_cast<int>(pluginFrame->height / 2);
86 circlePos.x = static_cast<int>(pluginFrame->width / 2);
agsantosd09cc6d2020-11-06 17:34:46 -050087 int w = resultFrame.size().width;
88 int h = resultFrame.size().height;
89 radius = std::min(w, h) / 8;
90 firstRun = false;
91 }
92
93 drawCenterCircle();
agsantos1bbc7cc2021-05-20 16:43:35 -040094 copyByLine(rgbFrame->linesize[0]);
agsantosd09cc6d2020-11-06 17:34:46 -050095
96 //======================================================================================
97 // REPLACE AVFRAME DATA WITH FRAME DATA
agsantos1bbc7cc2021-05-20 16:43:35 -040098 rgbFrame.reset(FrameScaler::convertFormat(rgbFrame.get(), AV_PIX_FMT_YUV420P));
99 moveFrom(pluginFrame, rgbFrame.get());
agsantosd09cc6d2020-11-06 17:34:46 -0500100}
101
102void
103CenterCircleVideoSubscriber::setColor(const std::string& color)
104{
105 int r, g, b = 0;
106 std::sscanf(color.c_str(), "#%02x%02x%02x", &r, &g, &b);
107 baseColor = cv::Scalar(r, g, b);
108 Plog::log(Plog::LogPriority::INFO, TAG, "Color set to: " + color);
109}
110
111void
112CenterCircleVideoSubscriber::copyByLine(const int lineSize)
113{
114 if (3 * processingFrame.cols == lineSize) {
115 std::memcpy(resultFrame.data,
116 processingFrame.data,
117 processingFrame.rows * processingFrame.cols * 3);
118 } else {
119 int rows = processingFrame.rows;
120 int offset = 0;
121 int frameOffset = 0;
122 for (int i = 0; i < rows; i++) {
123 std::memcpy(resultFrame.data + offset, processingFrame.data + frameOffset, lineSize);
124 offset += lineSize;
125 frameOffset += 3 * processingFrame.cols;
126 }
127 }
128}
129
130void
131CenterCircleVideoSubscriber::drawCenterCircle()
132{
133 if (!processingFrame.empty()) {
134 cv::circle(processingFrame, circlePos, radius, baseColor, cv::FILLED);
135 }
136}
137
138void
139CenterCircleVideoSubscriber::attached(jami::Observable<AVFrame*>* observable)
140{
141 std::ostringstream oss;
142 oss << "::Attached ! " << std::endl;
143 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
144 observable_ = observable;
145}
146
147void
148CenterCircleVideoSubscriber::detached(jami::Observable<AVFrame*>*)
149{
150 firstRun = true;
151 observable_ = nullptr;
152 std::ostringstream oss;
153 oss << "::Detached()" << std::endl;
154 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
155}
156
157void
158CenterCircleVideoSubscriber::detach()
159{
160 if (observable_) {
161 firstRun = true;
162 std::ostringstream oss;
163 oss << "::Calling detach()" << std::endl;
164 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
165 observable_->detach(this);
166 }
167}
168} // namespace jami