blob: b1e13779749ef2964bb51a939a80cb18a8b2b401 [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>
27
28// LOGGING
29#include <pluglog.h>
30
31#include <stdio.h>
32#include <opencv2/imgproc.hpp>
33
34const std::string TAG = "CenterCircle";
35const char sep = separator();
36
37namespace jami {
38
39CenterCircleVideoSubscriber::CenterCircleVideoSubscriber(const std::string& dataPath)
40 : path_ {dataPath}
41{}
42
43CenterCircleVideoSubscriber::~CenterCircleVideoSubscriber()
44{
45 std::ostringstream oss;
46 oss << "~CenterCircleMediaProcessor" << std::endl;
47 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
48}
49
50void
51CenterCircleVideoSubscriber::update(jami::Observable<AVFrame*>*, AVFrame* const& iFrame)
52{
53 if (!iFrame)
54 return;
55 AVFrame* pluginFrame = const_cast<AVFrame*>(iFrame);
56
57 //======================================================================================
58 // GET FRAME ROTATION
59 AVFrameSideData* side_data = av_frame_get_side_data(iFrame, AV_FRAME_DATA_DISPLAYMATRIX);
60
61 int angle {0};
62 if (side_data) {
63 auto matrix_rotation = reinterpret_cast<int32_t*>(side_data->data);
64 angle = static_cast<int>(av_display_rotation_get(matrix_rotation));
65 }
66
67 //======================================================================================
68 // GET RAW FRAME
69 // Use a non-const Frame
70 // Convert input frame to RGB
71 int inputHeight = pluginFrame->height;
72 int inputWidth = pluginFrame->width;
73
74 FrameUniquePtr bgrFrame = scaler.convertFormat(transferToMainMemory(pluginFrame,
75 AV_PIX_FMT_NV12),
76 AV_PIX_FMT_RGB24);
77 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
102 if (bgrFrame && bgrFrame->data[0]) {
103 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 }
110 }
111 // Copy Frame meta data
112 if (bgrFrame && pluginFrame) {
113 av_frame_copy_props(bgrFrame.get(), pluginFrame);
114 scaler.moveFrom(pluginFrame, bgrFrame.get());
115 }
116
117 // Remove the pointer
118 pluginFrame = nullptr;
119}
120
121void
122CenterCircleVideoSubscriber::setColor(const std::string& color)
123{
124 int r, g, b = 0;
125 std::sscanf(color.c_str(), "#%02x%02x%02x", &r, &g, &b);
126 baseColor = cv::Scalar(r, g, b);
127 Plog::log(Plog::LogPriority::INFO, TAG, "Color set to: " + color);
128}
129
130void
131CenterCircleVideoSubscriber::copyByLine(const int lineSize)
132{
133 if (3 * processingFrame.cols == lineSize) {
134 std::memcpy(resultFrame.data,
135 processingFrame.data,
136 processingFrame.rows * processingFrame.cols * 3);
137 } else {
138 int rows = processingFrame.rows;
139 int offset = 0;
140 int frameOffset = 0;
141 for (int i = 0; i < rows; i++) {
142 std::memcpy(resultFrame.data + offset, processingFrame.data + frameOffset, lineSize);
143 offset += lineSize;
144 frameOffset += 3 * processingFrame.cols;
145 }
146 }
147}
148
149void
150CenterCircleVideoSubscriber::drawCenterCircle()
151{
152 if (!processingFrame.empty()) {
153 cv::circle(processingFrame, circlePos, radius, baseColor, cv::FILLED);
154 }
155}
156
157void
158CenterCircleVideoSubscriber::attached(jami::Observable<AVFrame*>* observable)
159{
160 std::ostringstream oss;
161 oss << "::Attached ! " << std::endl;
162 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
163 observable_ = observable;
164}
165
166void
167CenterCircleVideoSubscriber::detached(jami::Observable<AVFrame*>*)
168{
169 firstRun = true;
170 observable_ = nullptr;
171 std::ostringstream oss;
172 oss << "::Detached()" << std::endl;
173 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
174}
175
176void
177CenterCircleVideoSubscriber::detach()
178{
179 if (observable_) {
180 firstRun = true;
181 std::ostringstream oss;
182 oss << "::Calling detach()" << std::endl;
183 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
184 observable_->detach(this);
185 }
186}
187} // namespace jami