Added delay and image selection to CloneSource

This commit is contained in:
Bruno Herbelin
2022-01-15 00:15:52 +01:00
parent 3678e8fb27
commit e58041227b
8 changed files with 211 additions and 26 deletions

View File

@@ -17,38 +17,53 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
**/ **/
#include <gst/gst.h>
#include <glm/gtc/matrix_access.hpp> #include <glm/gtc/matrix_access.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "Log.h" #include "Log.h"
#include "Resource.h" #include "Resource.h"
#include "Visitor.h" #include "Visitor.h"
#include "FrameBuffer.h"
#include "Decorations.h" #include "Decorations.h"
#include "CloneSource.h" #include "CloneSource.h"
CloneSource *Source::clone(uint64_t id)
{ CloneSource::CloneSource(Source *origin, uint64_t id) : Source(id), origin_(origin), cloningsurface_(nullptr),
CloneSource *s = new CloneSource(this, id); read_index_(0), write_index_(0), delay_(0.0), paused_(false), image_mode_(CLONE_TEXTURE)
clones_.push_back(s);
return s;
}
CloneSource::CloneSource(Source *origin, uint64_t id) : Source(id), origin_(origin)
{ {
// initial name copies the origin name: diplucates are namanged in session
name_ = origin->name(); name_ = origin->name();
// set symbol // set symbol
symbol_ = new Symbol(Symbol::CLONE, glm::vec3(0.75f, 0.75f, 0.01f)); symbol_ = new Symbol(Symbol::CLONE, glm::vec3(0.75f, 0.75f, 0.01f));
symbol_->scale_.y = 1.5f; symbol_->scale_.y = 1.5f;
// init array
stack_.fill(nullptr);
timestamp_.fill(0.0);
timer_ = g_timer_new ();
} }
CloneSource::~CloneSource() CloneSource::~CloneSource()
{ {
if (origin_) if (origin_)
origin_->clones_.remove(this); origin_->clones_.remove(this);
// delete all frame buffers
for (size_t i = 0; i < stack_.size(); ++i){
if ( stack_[i] != nullptr )
delete stack_[i];
}
if (cloningsurface_)
delete cloningsurface_;
g_free(timer_);
} }
CloneSource *CloneSource::clone(uint64_t id) CloneSource *CloneSource::clone(uint64_t id)
@@ -64,15 +79,29 @@ void CloneSource::init()
{ {
if (origin_ && origin_->mode_ > Source::UNINITIALIZED) { if (origin_ && origin_->mode_ > Source::UNINITIALIZED) {
// get the texture index from framebuffer of view, apply it to the surface // create frame buffers where to copy frames of the origin source
texturesurface_->setTextureIndex( origin_->texture() ); glm::vec3 res = origin_->frame()->resolution();
for (size_t i = 0; i < stack_.size(); ++i){
stack_[i] = new FrameBuffer( res, origin_->frame()->use_alpha() );
}
// create Frame buffer matching size of session g_timer_start(timer_);
FrameBuffer *renderbuffer = new FrameBuffer( origin_->frame()->resolution(), true);
cloningsurface_ = new Surface;
cloningsurface_->setTextureIndex( origin()->texture() );
// set initial texture surface
texturesurface_->setTextureIndex( stack_[read_index_]->texture() );
// create Frame buffer matching size of images
FrameBuffer *renderbuffer = new FrameBuffer( res, true);
// set the renderbuffer of the source and attach rendering nodes // set the renderbuffer of the source and attach rendering nodes
attach(renderbuffer); attach(renderbuffer);
// force update of activation mode
active_ = true;
// deep update to reorder // deep update to reorder
++View::need_deep_update_; ++View::need_deep_update_;
@@ -83,6 +112,9 @@ void CloneSource::init()
void CloneSource::setActive (bool on) void CloneSource::setActive (bool on)
{ {
// request update
need_update_ |= active_ != on;
active_ = on; active_ = on;
groups_[View::RENDERING]->visible_ = active_; groups_[View::RENDERING]->visible_ = active_;
@@ -98,17 +130,90 @@ void CloneSource::setActive (bool on)
if (active_) if (active_)
activesurface_->setTextureIndex(Resource::getTextureTransparent()); activesurface_->setTextureIndex(Resource::getTextureTransparent());
else else
activesurface_->setTextureIndex(origin_->texture()); activesurface_->setTextureIndex(stack_[read_index_]->texture());
} }
} }
} }
void CloneSource::replay()
{
read_index_ = (write_index_ + 1 )%(stack_.size());
}
void CloneSource::update(float dt)
{
if (active_ && !paused_ && cloningsurface_ != nullptr) {
double now = g_timer_elapsed (timer_, NULL) ;
// increment enplacement of write index
write_index_ = (write_index_+1)%(stack_.size());
timestamp_[write_index_] = now;
// CLONE_RENDER : blit rendered framebuffer in the stack
if (image_mode_ == CLONE_RENDER)
origin_->frame()->blit(stack_[write_index_]);
// CLONE_TEXTURE : render origin texture in the stack
else {
stack_[write_index_]->begin();
cloningsurface_->draw(glm::identity<glm::mat4>(), stack_[write_index_]->projection());
stack_[write_index_]->end();
}
// define emplacement of read index
if (delay_ < 0.001)
// minimal difference if no delay
read_index_ = write_index_;
else
{
// starting where we are at, get the next index that satisfies the delay
size_t previous_index = read_index_;
while ( now - timestamp_[read_index_] > delay_) {
// usually, one frame increment suffice
read_index_ = (read_index_ + 1 )%(stack_.size());
// break the loop if running infinite (never happens)
if (previous_index == read_index_)
break;
}
}
// update the source surface to be rendered
texturesurface_->setTextureIndex( stack_[read_index_]->texture() );
}
Source::update(dt);
}
void CloneSource::setDelay(double second)
{
delay_ = CLAMP(second, 0.0, 1.0);
}
void CloneSource::play (bool on)
{
// if a different state is asked
if (paused_ == on) {
// restart clean if was paused
if (paused_) {
g_timer_reset(timer_);
timestamp_.fill(0.0);
write_index_ = 0;
read_index_ = 1;
}
// toggle state
paused_ = !on;
}
}
uint CloneSource::texture() const uint CloneSource::texture() const
{ {
if (origin_ != nullptr) if (cloningsurface_ != nullptr)
return origin_->texture(); return stack_[read_index_]->texture();
else else
return Resource::getTextureBlack(); return Resource::getTextureTransparent();
} }
void CloneSource::accept(Visitor& v) void CloneSource::accept(Visitor& v)

View File

@@ -1,6 +1,10 @@
#ifndef CLONESOURCE_H #ifndef CLONESOURCE_H
#define CLONESOURCE_H #define CLONESOURCE_H
#include <array>
#define DELAY_ARRAY_SIZE 70
#include "Source.h" #include "Source.h"
class CloneSource : public Source class CloneSource : public Source
@@ -11,11 +15,12 @@ public:
~CloneSource(); ~CloneSource();
// implementation of source API // implementation of source API
void update (float dt) override;
void setActive (bool on) override; void setActive (bool on) override;
bool playing () const override { return true; } bool playing () const override { return !paused_; }
void play (bool) override {} void play (bool on) override;
bool playable () const override { return false; } bool playable () const override { return true; }
void replay () override {} void replay () override;
uint texture() const override; uint texture() const override;
bool failed() const override { return origin_ == nullptr; } bool failed() const override { return origin_ == nullptr; }
void accept (Visitor& v) override; void accept (Visitor& v) override;
@@ -24,6 +29,17 @@ public:
inline void detach() { origin_ = nullptr; } inline void detach() { origin_ = nullptr; }
inline Source *origin() const { return origin_; } inline Source *origin() const { return origin_; }
typedef enum {
CLONE_TEXTURE = 0,
CLONE_RENDER
} CloneImageMode;
void setImageMode(CloneImageMode m) { image_mode_ = m; }
CloneImageMode imageMode() const { return image_mode_; }
void setDelay(double second);
double delay() const { return delay_; }
glm::ivec2 icon() const override; glm::ivec2 icon() const override;
std::string info() const override; std::string info() const override;
@@ -33,6 +49,19 @@ protected:
void init() override; void init() override;
Source *origin_; Source *origin_;
// cloning
std::array<FrameBuffer *, DELAY_ARRAY_SIZE> stack_;
Surface *cloningsurface_;
size_t read_index_, write_index_;
GTimer *timer_;
std::array<double, DELAY_ARRAY_SIZE> timestamp_;
double delay_;
// control
bool paused_;
CloneImageMode image_mode_;
}; };

View File

@@ -714,6 +714,20 @@ void ImGuiVisitor::visit (CloneSource& s)
Mixer::manager().setCurrentSource(s.origin()); Mixer::manager().setCurrentSource(s.origin());
ImGui::SameLine(); ImGui::SameLine();
ImGui::Text("Source"); ImGui::Text("Source");
ImGui::SetNextItemWidth(IMGUI_RIGHT_ALIGN);
int m = (int) s.imageMode();
if (ImGui::Combo("Image", &m, "Original\0Post-processed\0") )
s.setImageMode((CloneSource::CloneImageMode)m);
ImGui::SetNextItemWidth(IMGUI_RIGHT_ALIGN);
float d = s.delay();
// if (ImGui::SliderFloat("Delay", &d, 0.f, 1.f, "%.2f s")){
if (ImGui::SliderFloat("Delay", &d, 0.f, 1.f, d < 0.01f ? "None" : "%.2f s")){
s.setDelay(d);
}
} }
void ImGuiVisitor::visit (PatternSource& s) void ImGuiVisitor::visit (PatternSource& s)

View File

@@ -178,10 +178,18 @@ void InfoVisitor::visit (RenderSource& s)
void InfoVisitor::visit (CloneSource& s) void InfoVisitor::visit (CloneSource& s)
{ {
if (current_id_ == s.id()) if (current_id_ == s.id() || s.origin() == nullptr)
return; return;
information_ = "Clone of " + s.origin()->name(); std::ostringstream oss;
oss << "Clone of '" << s.origin()->name() << "' " << std::endl;
if (s.frame()){
oss << s.frame()->width() << " x " << s.frame()->height() << ", ";
oss << "RGBA";
}
information_ = oss.str();
current_id_ = s.id(); current_id_ = s.id();
} }

View File

@@ -407,7 +407,7 @@ void SessionLoader::load(XMLElement *sessionNode)
// found the orign source // found the orign source
if (origin != session_->end()) { if (origin != session_->end()) {
// create a new source of type Clone // create a new source of type Clone
Source *clone_source = (*origin)->clone(id_xml_); CloneSource *clone_source = (*origin)->clone(id_xml_);
// add source to session // add source to session
session_->addSource(clone_source); session_->addSource(clone_source);
@@ -1082,3 +1082,16 @@ void SessionLoader::visit (GenericStreamSource& s)
} }
} }
void SessionLoader::visit (CloneSource& s)
{
// set attributes
int imagemode = 0;
xmlCurrent_->QueryIntAttribute("imageMode", &imagemode);
s.setImageMode((CloneSource::CloneImageMode)imagemode);
double delay = 0.0;
xmlCurrent_->QueryDoubleAttribute("delay", &delay);
s.setDelay(delay);
}

View File

@@ -55,6 +55,7 @@ public:
void visit (SessionFileSource& s) override; void visit (SessionFileSource& s) override;
void visit (SessionGroupSource& s) override; void visit (SessionGroupSource& s) override;
void visit (RenderSource& s) override; void visit (RenderSource& s) override;
void visit (CloneSource& s) override;
void visit (PatternSource& s) override; void visit (PatternSource& s) override;
void visit (DeviceSource& s) override; void visit (DeviceSource& s) override;
void visit (NetworkSource& s) override; void visit (NetworkSource& s) override;

View File

@@ -634,12 +634,16 @@ void SessionVisitor::visit (RenderSource&)
void SessionVisitor::visit (CloneSource& s) void SessionVisitor::visit (CloneSource& s)
{ {
xmlCurrent_->SetAttribute("type", "CloneSource"); xmlCurrent_->SetAttribute("type", "CloneSource");
xmlCurrent_->SetAttribute("imageMode", (int) s.imageMode());
xmlCurrent_->SetAttribute("delay", (double) s.delay());
XMLElement *origin = xmlDoc_->NewElement("origin"); XMLElement *origin = xmlDoc_->NewElement("origin");
origin->SetAttribute("id", s.origin()->id()); origin->SetAttribute("id", s.origin()->id());
xmlCurrent_->InsertEndChild(origin); xmlCurrent_->InsertEndChild(origin);
XMLText *text = xmlDoc_->NewText( s.origin()->name().c_str() ); XMLText *text = xmlDoc_->NewText( s.origin()->name().c_str() );
origin->InsertEndChild( text ); origin->InsertEndChild( text );
} }
void SessionVisitor::visit (PatternSource& s) void SessionVisitor::visit (PatternSource& s)

View File

@@ -649,6 +649,17 @@ void Source::call(SourceCallback *callback, bool override)
} }
} }
CloneSource *Source::clone(uint64_t id)
{
CloneSource *s = new CloneSource(this, id);
clones_.push_back(s);
return s;
}
void Source::update(float dt) void Source::update(float dt)
{ {
// keep delta-t // keep delta-t