-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture_render.cc
158 lines (127 loc) · 4.8 KB
/
texture_render.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "include/agora_rtc_engine/texture_render.h"
#include <functional>
#include "AgoraMediaBase.h"
using namespace flutter;
TextureRender::TextureRender(flutter::BinaryMessenger *messenger,
flutter::TextureRegistrar *registrar,
agora::iris::IrisRtcRendering *iris_rtc_rendering)
: registrar_(registrar),
iris_rtc_rendering_(iris_rtc_rendering),
delegate_id_(agora::iris::INVALID_DELEGATE_ID),
is_dirty_(false)
{
// Create flutter desktop pixelbuffer texture;
texture_ =
std::make_unique<flutter::TextureVariant>(flutter::PixelBufferTexture(
[this](size_t width,
size_t height) -> const FlutterDesktopPixelBuffer *
{
return this->CopyPixelBuffer(width, height);
}));
texture_id_ = registrar_->RegisterTexture(texture_.get());
method_channel_ = std::make_unique<MethodChannel<EncodableValue>>(
messenger,
"agora_rtc_engine/texture_render_" + std::to_string(texture_id_),
&flutter::StandardMethodCodec::GetInstance());
}
TextureRender::~TextureRender()
{
Dispose();
}
int64_t TextureRender::texture_id() { return texture_id_; }
void TextureRender::OnVideoFrameReceived(const void *videoFrame,
const IrisRtcVideoFrameConfig &config,
bool resize)
{
std::lock_guard<std::mutex> lock_guard(buffer_mutex_);
if (!is_dirty_)
{
const agora::media::base::VideoFrame *video_frame = static_cast<const agora::media::base::VideoFrame *>(videoFrame);
const uint32_t bytes_per_pixel = 4;
const uint32_t pixels_total = video_frame->width * video_frame->height;
const uint32_t data_size = pixels_total * bytes_per_pixel;
if (buffer_.size() != data_size)
{
buffer_.resize(data_size);
flutter::EncodableMap args = {
{EncodableValue("width"), EncodableValue(video_frame->width)},
{EncodableValue("height"), EncodableValue(video_frame->height)}};
method_channel_->InvokeMethod("onSizeChanged", std::make_unique<EncodableValue>(EncodableValue(args)));
}
std::copy(static_cast<uint8_t *>(video_frame->yBuffer), static_cast<uint8_t *>(video_frame->yBuffer) + data_size, buffer_.data());
frame_width_ = video_frame->width;
frame_height_ = video_frame->height;
is_dirty_ = true;
}
if (TextureRegistered() && is_dirty_)
{
registrar_->MarkTextureFrameAvailable(texture_id_);
}
}
const FlutterDesktopPixelBuffer *
TextureRender::CopyPixelBuffer(size_t width, size_t height)
{
std::unique_lock<std::mutex> buffer_lock(buffer_mutex_);
is_dirty_ = false;
if (!TextureRegistered())
{
return nullptr;
}
if (frame_width_ == 0 || frame_height_ == 0)
{
return nullptr;
}
if (!flutter_desktop_pixel_buffer_)
{
flutter_desktop_pixel_buffer_ =
std::make_unique<FlutterDesktopPixelBuffer>();
// Unlocks mutex after texture is processed.
flutter_desktop_pixel_buffer_->release_callback =
[](void *release_context)
{
auto mutex = reinterpret_cast<std::mutex *>(release_context);
mutex->unlock();
};
}
flutter_desktop_pixel_buffer_->buffer = buffer_.data();
flutter_desktop_pixel_buffer_->width = frame_width_;
flutter_desktop_pixel_buffer_->height = frame_height_;
// Releases unique_lock and set mutex pointer for release context.
flutter_desktop_pixel_buffer_->release_context = buffer_lock.release();
return flutter_desktop_pixel_buffer_.get();
}
void TextureRender::UpdateData(unsigned int uid, const std::string &channelId, unsigned int videoSourceType, unsigned int videoViewSetupMode)
{
IrisRtcVideoFrameConfig config = EmptyIrisRtcVideoFrameConfig;
config.uid = uid;
config.video_source_type = videoSourceType;
config.video_frame_format = agora::media::base::VIDEO_PIXEL_FORMAT::VIDEO_PIXEL_RGBA;
if (!channelId.empty())
{
strcpy_s(config.channelId, channelId.c_str());
}
else
{
strcpy_s(config.channelId, "");
}
config.video_view_setup_mode = videoViewSetupMode;
if (iris_rtc_rendering_)
{
delegate_id_ = iris_rtc_rendering_->AddVideoFrameObserverDelegate(config, this);
}
}
void TextureRender::Dispose()
{
if (iris_rtc_rendering_)
{
iris_rtc_rendering_->RemoveVideoFrameObserverDelegate(delegate_id_);
iris_rtc_rendering_ = nullptr;
}
const std::lock_guard<std::mutex> lock(buffer_mutex_);
if (registrar_ && texture_id_ != -1)
{
registrar_->UnregisterTexture(texture_id_);
registrar_ = nullptr;
texture_id_ = -1;
}
}