blob: 25c95a39675a24ffadcce2dbd865af40d54ef837 [file] [log] [blame]
agsantosd09cc6d2020-11-06 17:34:46 -05001/**
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
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>
agsantosd09cc6d2020-11-06 17:34:46 -050028#include <pluglog.h>
agsantosd09cc6d2020-11-06 17:34:46 -050029#include <stdio.h>
30#include <opencv2/imgproc.hpp>
31
32const std::string TAG = "CenterCircle";
33const char sep = separator();
34
35namespace jami {
36
37CenterCircleVideoSubscriber::CenterCircleVideoSubscriber(const std::string& dataPath)
38 : path_ {dataPath}
39{}
40
41CenterCircleVideoSubscriber::~CenterCircleVideoSubscriber()
42{
43 std::ostringstream oss;
44 oss << "~CenterCircleMediaProcessor" << std::endl;
45 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
46}
47
48void
agsantosc9181b42020-11-26 12:03:04 -050049CenterCircleVideoSubscriber::update(jami::Observable<AVFrame*>*, AVFrame* const& pluginFrame)
agsantosd09cc6d2020-11-06 17:34:46 -050050{
agsantosc9181b42020-11-26 12:03:04 -050051 if (!pluginFrame)
agsantosd09cc6d2020-11-06 17:34:46 -050052 return;
agsantosd09cc6d2020-11-06 17:34:46 -050053
54 //======================================================================================
55 // GET FRAME ROTATION
agsantosc9181b42020-11-26 12:03:04 -050056 AVFrameSideData* side_data = av_frame_get_side_data(pluginFrame, AV_FRAME_DATA_DISPLAYMATRIX);
agsantosd09cc6d2020-11-06 17:34:46 -050057
58 int angle {0};
59 if (side_data) {
60 auto matrix_rotation = reinterpret_cast<int32_t*>(side_data->data);
61 angle = static_cast<int>(av_display_rotation_get(matrix_rotation));
62 }
agsantosc9181b42020-11-26 12:03:04 -050063 delete side_data;
agsantosd09cc6d2020-11-06 17:34:46 -050064
65 //======================================================================================
66 // GET RAW FRAME
67 // Use a non-const Frame
68 // Convert input frame to RGB
69 int inputHeight = pluginFrame->height;
70 int inputWidth = pluginFrame->width;
agsantosc9181b42020-11-26 12:03:04 -050071 AVFrame* temp = transferToMainMemory(pluginFrame, AV_PIX_FMT_NV12);
72 AVFrame* bgrFrame = scaler.convertFormat(temp, AV_PIX_FMT_RGB24);
73 av_frame_unref(temp);
74 av_frame_free(&temp);
75 if (!bgrFrame)
76 return;
agsantosd09cc6d2020-11-06 17:34:46 -050077 resultFrame = cv::Mat {bgrFrame->height,
78 bgrFrame->width,
79 CV_8UC3,
80 bgrFrame->data[0],
81 static_cast<size_t>(bgrFrame->linesize[0])};
82
83 // First clone the frame as the original one is unusable because of
84 // linespace
85 processingFrame = resultFrame.clone();
86
87 if (firstRun) {
88 // we set were the circle will be draw.
89 circlePos.y = static_cast<int>(inputHeight / 2);
90 circlePos.x = static_cast<int>(inputWidth / 2);
91 int w = resultFrame.size().width;
92 int h = resultFrame.size().height;
93 radius = std::min(w, h) / 8;
94 firstRun = false;
95 }
96
97 drawCenterCircle();
98 copyByLine(bgrFrame->linesize[0]);
99
100 //======================================================================================
101 // REPLACE AVFRAME DATA WITH FRAME DATA
agsantosc9181b42020-11-26 12:03:04 -0500102 if (bgrFrame->data[0]) {
agsantosd09cc6d2020-11-06 17:34:46 -0500103 uint8_t* frameData = bgrFrame->data[0];
104 if (angle == 90 || angle == -90) {
105 std::memmove(frameData,
106 resultFrame.data,
107 static_cast<size_t>(pluginFrame->width * pluginFrame->height * 3)
108 * sizeof(uint8_t));
109 }
agsantosd09cc6d2020-11-06 17:34:46 -0500110
agsantosc9181b42020-11-26 12:03:04 -0500111 av_frame_copy_props(bgrFrame, pluginFrame);
112 moveFrom(pluginFrame, bgrFrame);
113 }
114 av_frame_unref(bgrFrame);
115 av_frame_free(&bgrFrame);
agsantosd09cc6d2020-11-06 17:34:46 -0500116}
117
118void
119CenterCircleVideoSubscriber::setColor(const std::string& color)
120{
121 int r, g, b = 0;
122 std::sscanf(color.c_str(), "#%02x%02x%02x", &r, &g, &b);
123 baseColor = cv::Scalar(r, g, b);
124 Plog::log(Plog::LogPriority::INFO, TAG, "Color set to: " + color);
125}
126
127void
128CenterCircleVideoSubscriber::copyByLine(const int lineSize)
129{
130 if (3 * processingFrame.cols == lineSize) {
131 std::memcpy(resultFrame.data,
132 processingFrame.data,
133 processingFrame.rows * processingFrame.cols * 3);
134 } else {
135 int rows = processingFrame.rows;
136 int offset = 0;
137 int frameOffset = 0;
138 for (int i = 0; i < rows; i++) {
139 std::memcpy(resultFrame.data + offset, processingFrame.data + frameOffset, lineSize);
140 offset += lineSize;
141 frameOffset += 3 * processingFrame.cols;
142 }
143 }
144}
145
146void
147CenterCircleVideoSubscriber::drawCenterCircle()
148{
149 if (!processingFrame.empty()) {
150 cv::circle(processingFrame, circlePos, radius, baseColor, cv::FILLED);
151 }
152}
153
154void
155CenterCircleVideoSubscriber::attached(jami::Observable<AVFrame*>* observable)
156{
157 std::ostringstream oss;
158 oss << "::Attached ! " << std::endl;
159 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
160 observable_ = observable;
161}
162
163void
164CenterCircleVideoSubscriber::detached(jami::Observable<AVFrame*>*)
165{
166 firstRun = true;
167 observable_ = nullptr;
168 std::ostringstream oss;
169 oss << "::Detached()" << std::endl;
170 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
171}
172
173void
174CenterCircleVideoSubscriber::detach()
175{
176 if (observable_) {
177 firstRun = true;
178 std::ostringstream oss;
179 oss << "::Calling detach()" << std::endl;
180 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
181 observable_->detach(this);
182 }
183}
184} // namespace jami