Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug that push data mode cannot be called by cpp #155

Merged
merged 2 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion bmf/engine/connector/include/builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ enum GraphMode {
ServerMode,
GeneratorMode,
SubGraphMode,
UpdateMode,
sfeiwong marked this conversation as resolved.
Show resolved Hide resolved
PushDataMode,
};
enum InputManagerType { Immediate, Default, Server, FrameSync, ClockSync };
enum ModuleType { CPP, C, Python, Go };
Expand Down Expand Up @@ -236,6 +236,7 @@ class RealGraph : public std::enable_shared_from_this<RealGraph> {
bool dumpGraph, bool needMerge);

int Run(bool dumpGraph, bool needMerge);
int Close();
Packet Generate(std::string streamName, bool block = true);
int FillPacket(std::string stream_name, Packet packet, bool block = false);
std::shared_ptr<RealStream> InputStream(std::string streamName, std::string notify, std::string alias);
Expand Down Expand Up @@ -637,6 +638,8 @@ class BMF_ENGINE_API Graph {

void Start(std::vector<Stream>& generateStreams, bool dumpGraph = true, bool needMerge = true);

int Close();

std::string Dump();

Node
Expand Down
16 changes: 14 additions & 2 deletions bmf/engine/connector/src/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ nlohmann::json RealGraph::Dump() {
case SubGraphMode:
info["mode"] = "Subgraph";
break;
case UpdateMode:
sfeiwong marked this conversation as resolved.
Show resolved Hide resolved
info["mode"] = "Update";
case PushDataMode:
info["mode"] = "Pushdata";
break;
}
for (auto &nd : nodes_)
Expand Down Expand Up @@ -433,6 +433,10 @@ void RealGraph::Start(
Start(dumpGraph, needMerge);
}

int RealGraph::Close() {
return graphInstance_->close();
}

bmf::BMFGraph RealGraph::Instantiate(bool dumpGraph, bool needMerge) {
auto graph_config = Dump().dump(4);
if (dumpGraph || (graphOption_.json_value_.count("dump_graph") &&
Expand Down Expand Up @@ -787,6 +791,10 @@ int Graph::Run(bool dumpGraph, bool needMerge) {
return graph_->Run(dumpGraph, needMerge);
}

void Graph::Start(bool dumpGraph, bool needMerge) {
graph_->Start(dumpGraph, needMerge);
}

void Graph::Start(std::vector<Stream> &generateStreams, bool dumpGraph,
bool needMerge) {
std::vector<std::shared_ptr<internal::RealStream>> generateRealStreams;
Expand All @@ -796,6 +804,10 @@ void Graph::Start(std::vector<Stream> &generateStreams, bool dumpGraph,
graph_->Start(generateRealStreams, dumpGraph, needMerge);
}

int Graph::Close() {
return graph_->Close();
}

Packet Graph::Generate(std::string streamName, bool block) {
return graph_->Generate(streamName, block);
}
Expand Down
96 changes: 96 additions & 0 deletions bmf/test/push_data_into_graph/test_push_data.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
#include "../../engine/c_engine/include/common.h"
#include "../../engine/c_engine/include/graph.h"

#include <bmf/sdk/bmf.h>
#include <bmf/sdk/log.h>
#include <fstream>

#include <hmp/imgproc.h>
// #include <iostream>

USE_BMF_ENGINE_NS
USE_BMF_SDK_NS

int main() {
BMFLOG_SET_LEVEL(BMF_INFO);

time_t time1 = clock();
nlohmann::json graph_json;
std::string config_file = "./original_graph.json";
std::ifstream gs(config_file);
gs >> graph_json;
GraphConfig graph_config(graph_json);
std::map<int, std::shared_ptr<Module>> pre_modules;
std::map<int, std::shared_ptr<ModuleCallbackLayer>> callback_bindings;
std::shared_ptr<Graph> graph =
std::make_shared<Graph>(graph_config, pre_modules, callback_bindings);
std::cout << "init graph success" << std::endl;

graph->start();

int count = 50;
int pts_ = 0;
while(count > 0) {
auto frame = VideoFrame(640, 480, PixelInfo(PixelFormat::PF_YUV420P));
frame.set_pts(pts_);
pts_++;
auto packet = Packet(frame);
count--;
graph->add_input_stream_packet("outside_raw_video", packet);
std::cout << "push data into inputstream" << std::endl;
}
auto packet = Packet::generate_eof_packet();
graph->add_input_stream_packet("outside_raw_video", packet);

graph->close();
time_t time2 = clock();
std::cout << "time:" << time2 - time1 << std::endl;

return 0;
}
*/

#include <builder.hpp>
#include "nlohmann/json.hpp"
#include <bmf/sdk/bmf.h>

int main() {
std::string output_path = "./push_data_output.mp4";

auto graph = bmf::builder::Graph(bmf::builder::PushDataMode);
auto video_stream = graph.InputStream("outside_raw_video", "", "");
nlohmann::json encode_para = {
{"output_path", output_path},
{"video_params", {
{"codec", "h264"},
{"width", 640},
{"height", 480},
{"crf", 23},
{"preset", "veryfast"},
{"vsync", "vfr"}
}
}
};
auto video = graph.Encode(video_stream, bmf_sdk::JsonParam(encode_para));

graph.Start();

int count = 50;
int pts_ = 0;
while (count > 0) {
auto frame = VideoFrame(640, 480, PixelInfo(PixelFormat::PF_YUV420P));
frame.set_pts(pts_);
pts_++;
auto packet = Packet(frame);
count--;
graph.FillPacket(video_stream.GetName(), packet);
std::cout << "push data into inputstream" << std::endl;
}
auto packet = Packet::generate_eof_packet();
graph.FillPacket(video_stream.GetName(), packet);

graph.Close();

return 0;
}