blob: 13398cbfe566264a905ebd9f3fc1bf23c70b0f68 [file] [log] [blame]
agsantos1e7736c2020-10-28 14:39:13 -04001HEADER
2
3#include "GENERICVideoSubscriber.h"
4
5extern "C" {
6#include <libavutil/display.h>
7}
8#include <accel.h>
9
10// LOGGING
11#include <pluglog.h>
12
13const std::string TAG = "GENERIC";
14const char sep = separator();
15
16namespace jami {
17
18GENERICVideoSubscriber::GENERICVideoSubscriber(const std::string& dataPath)
19 : path_ {dataPath}
20{}
21
22GENERICVideoSubscriber::~GENERICVideoSubscriber()
23{
24 std::ostringstream oss;
25 oss << "~GENERICMediaProcessor" << std::endl;
26 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
27}
28
29void
30GENERICVideoSubscriber::update(jami::Observable<AVFrame*>*, AVFrame* const& iFrame)
31{
32 if (!iFrame)
33 return;
34 AVFrame* pluginFrame = const_cast<AVFrame*>(iFrame);
35
36 //======================================================================================
37 // GET FRAME ROTATION
38 AVFrameSideData* side_data = av_frame_get_side_data(iFrame, AV_FRAME_DATA_DISPLAYMATRIX);
39
40 int angle {0};
41 if (side_data) {
42 auto matrix_rotation = reinterpret_cast<int32_t*>(side_data->data);
43 angle = static_cast<int>(av_display_rotation_get(matrix_rotation));
44 }
45
46 //======================================================================================
47 // GET RAW FRAME
48 // Use a non-const Frame
49 // Convert input frame to RGB
50 int inputHeight = pluginFrame->height;
51 int inputWidth = pluginFrame->width;
52 FrameUniquePtr bgrFrame = scaler.convertFormat(transferToMainMemory(pluginFrame,
53 AV_PIX_FMT_NV12),
54 AV_PIX_FMT_RGB24);
55
56 // transferToMainMemory USED TO COPY FRAME TO MAIN MEMORY IF HW ACCEL IS ENABLED
57 // NOT NEEDED TO BE USED IF ALL YOUR PROCESS WILL BE DONE WITHIN A
58 // HW ACCEL PLATFORM
59
60 if (firstRun) {
61 // IMPLEMENT CODE TO CONFIGURE
62 // VARIABLES UPON FIRST RUN
63 firstRun = false;
64 }
65
66 // IMPLEMENT PROCESS
67
68 //======================================================================================
69 // REPLACE AVFRAME DATA WITH FRAME DATA
70 if (bgrFrame && bgrFrame->data[0]) {
71 uint8_t* frameData = bgrFrame->data[0];
72 if (angle == 90 || angle == -90) {
73 std::memmove(frameData,
74 frameData, // PUT HERE YOUR PROCESSED FRAME VARIABLE DATA!
75 static_cast<size_t>(pluginFrame->width * pluginFrame->height * 3)
76 * sizeof(uint8_t));
77 }
78 }
79 // Copy Frame meta data
80 if (bgrFrame && pluginFrame) {
81 av_frame_copy_props(bgrFrame.get(), pluginFrame);
82 scaler.moveFrom(pluginFrame, bgrFrame.get());
83 }
84
85 // Remove the pointer
86 pluginFrame = nullptr;
87}
88
89void
90GENERICVideoSubscriber::attached(jami::Observable<AVFrame*>* observable)
91{
92 std::ostringstream oss;
93 oss << "::Attached ! " << std::endl;
94 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
95 observable_ = observable;
96}
97
98void
99GENERICVideoSubscriber::detached(jami::Observable<AVFrame*>*)
100{
101 firstRun = true;
102 observable_ = nullptr;
103 std::ostringstream oss;
104 oss << "::Detached()" << std::endl;
105 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
106}
107
108void
109GENERICVideoSubscriber::detach()
110{
111 if (observable_) {
112 firstRun = true;
113 std::ostringstream oss;
114 oss << "::Calling detach()" << std::endl;
115 Plog::log(Plog::LogPriority::INFO, TAG, oss.str());
116 observable_->detach(this);
117 }
118}
119} // namespace jami