blob: b3e77208738180faf5699885983cc09291bae937 [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 "CoinCircleVideoSubscriber.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 = "CoinCircle";
34const char sep = separator();
35
36namespace jami {
37
38CoinCircleVideoSubscriber::CoinCircleVideoSubscriber(const std::string& dataPath)
39 : path_ {dataPath}
40{}
41
42CoinCircleVideoSubscriber::~CoinCircleVideoSubscriber()
43{
44 std::ostringstream oss;
45 oss << "~CoinCircleMediaProcessor" << std::endl;
46 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
47}
48
49void
agsantosc9181b42020-11-26 12:03:04 -050050CoinCircleVideoSubscriber::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.
85 int w = resultFrame.size().width;
86 int h = resultFrame.size().height;
87 radius = std::min(w, h) / 8;
88 circlePos.y = static_cast<int>(radius);
89 circlePos.x = static_cast<int>(radius);
90 firstRun = false;
91 }
92
93 drawCoinCircle(angle);
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
103CoinCircleVideoSubscriber::drawCoinCircle(const int angle)
104{
105 if (!processingFrame.empty()) {
106 rotateFrame(angle, processingFrame);
107 cv::circle(processingFrame, circlePos, radius, baseColor, cv::FILLED);
108 rotateFrame(-angle, processingFrame);
109 }
110}
111
112void
113CoinCircleVideoSubscriber::rotateFrame(const int angle, cv::Mat& frame)
114{
115 if (angle == -90)
116 cv::rotate(frame, frame, cv::ROTATE_90_COUNTERCLOCKWISE);
117 else if (std::abs(angle) == 180)
118 cv::rotate(frame, frame, cv::ROTATE_180);
119 else if (angle == 90)
120 cv::rotate(frame, frame, cv::ROTATE_90_CLOCKWISE);
121}
122
123void
124CoinCircleVideoSubscriber::setColor(const std::string& color)
125{
126 int r, g, b = 0;
127 std::sscanf(color.c_str(), "#%02x%02x%02x", &r, &g, &b);
128 baseColor = cv::Scalar(r, g, b);
129 Plog::log(Plog::LogPriority::INFO, TAG, "Color set to: " + color);
130}
131
132void
133CoinCircleVideoSubscriber::copyByLine(const int lineSize)
134{
135 if (3 * processingFrame.cols == lineSize) {
136 std::memcpy(resultFrame.data,
137 processingFrame.data,
138 processingFrame.rows * processingFrame.cols * 3);
139 } else {
140 int rows = processingFrame.rows;
141 int offset = 0;
142 int frameOffset = 0;
143 for (int i = 0; i < rows; i++) {
144 std::memcpy(resultFrame.data + offset, processingFrame.data + frameOffset, lineSize);
145 offset += lineSize;
146 frameOffset += 3 * processingFrame.cols;
147 }
148 }
149}
150
151void
152CoinCircleVideoSubscriber::attached(jami::Observable<AVFrame*>* observable)
153{
154 std::ostringstream oss;
155 oss << "::Attached ! " << std::endl;
156 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
157 observable_ = observable;
158}
159
160void
161CoinCircleVideoSubscriber::detached(jami::Observable<AVFrame*>*)
162{
163 firstRun = true;
164 observable_ = nullptr;
165 std::ostringstream oss;
166 oss << "::Detached()" << std::endl;
167 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
168}
169
170void
171CoinCircleVideoSubscriber::detach()
172{
173 if (observable_) {
174 firstRun = true;
175 std::ostringstream oss;
176 oss << "::Calling detach()" << std::endl;
177 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
178 observable_->detach(this);
179 }
180}
181} // namespace jami