Skip to content

Commit

Permalink
update test case
Browse files Browse the repository at this point in the history
  • Loading branch information
JackLau1222 committed Jan 5, 2025
1 parent e239d86 commit d4a6065
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 54 deletions.
108 changes: 101 additions & 7 deletions bmf/demo/transcode/test_cb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,111 @@
#include "nlohmann/json.hpp"
#include "connector.hpp"

bmf_sdk::CBytes my_callback_function(bmf_sdk::CBytes input) {
#include <regex>

// Global variable or pass-by-reference as needed
int frame_number_total = 0;
int frame_number_global = 0;

int process_number = 0;
double rest_time = 0;

std::chrono::system_clock::time_point last_encoder_call; // Track last call time
bool first_encoder_call = true; // Flag for first call

std::vector<double> duration_history; // Store recent durations for averaging
constexpr size_t max_history_size = 20; // Limit for the number of durations tracked
constexpr double min_duration_threshold = 10.0; // Ignore durations < 10 ms

double compute_smooth_duration(double new_duration) {
if (new_duration >= min_duration_threshold) {
duration_history.push_back(new_duration);
if (duration_history.size() > max_history_size) {
duration_history.erase(duration_history.begin());
}
}
return duration_history.empty() ? 0.0 :
std::accumulate(duration_history.begin(), duration_history.end(), 0.0) / duration_history.size();
}

// std::string format_time(const std::tm& tm) {
// char buffer[20]; // Buffer to hold formatted time string
// std::snprintf(buffer, sizeof(buffer), "%04d-%02d-%02d %02d:%02d:%02d",
// tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
// tm.tm_hour, tm.tm_min, tm.tm_sec);
// return std::string(buffer);
// }

bmf_sdk::CBytes decoder_callback(bmf_sdk::CBytes input) {
std::string strInfo;
strInfo.assign(reinterpret_cast<const char*>(input.buffer), input.size);
BMFLOG(BMF_INFO) << "====Callback==== " << strInfo;
// BMFLOG(BMF_INFO) << "====Callback==== " << strInfo;


std::regex frame_regex(R"(\btotal frame number:\s*(\d+))");
std::smatch match;

if (std::regex_search(strInfo, match, frame_regex) && match.size() > 1) {
std::istringstream(match[1]) >> frame_number_total; // Convert to int
BMFLOG(BMF_DEBUG) << "Extracted Frame Number: " << frame_number_total;
} else {
BMFLOG(BMF_WARNING) << "Failed to extract frame number";
}

uint8_t bytes[] = {97, 98, 99, 100, 101, 0};
return bmf_sdk::CBytes{bytes, 6};
}

bmf_sdk::CBytes encoder_callback(bmf_sdk::CBytes input) {
std::string strInfo;
strInfo.assign(reinterpret_cast<const char*>(input.buffer), input.size);
// BMFLOG(BMF_INFO) << "====Callback==== " << strInfo;

std::regex frame_regex(R"(\bframe number:\s*(\d+))");
std::smatch match;

if (std::regex_search(strInfo, match, frame_regex) && match.size() > 1) {
std::istringstream(match[1]) >> frame_number_global; // Convert to int
BMFLOG(BMF_DEBUG) << "Extracted Total Frame Number: " << frame_number_global;
process_number = frame_number_global * 100 / frame_number_total;

static auto last_encoder_call_time = std::chrono::system_clock::now();
auto now = std::chrono::system_clock::now();

auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_encoder_call_time).count();
last_encoder_call_time = now;

double smooth_duration = compute_smooth_duration(duration);
if (frame_number_global > 0 && frame_number_total > 0) {
// double progress = static_cast<double>(frame_number_global) / frame_number_total;
rest_time = smooth_duration * (frame_number_total-frame_number_global) / 1000;
}

BMFLOG(BMF_INFO) << "Process Number (percentage): " << process_number << "%\t"
<< "Current duration (milliseconds): " << duration << "\t"
<< "Smoothed Duration: " << smooth_duration << " ms\t"
<< "Estimated Rest Time (seconds): " << rest_time;



if (frame_number_global == frame_number_total) {
BMFLOG(BMF_INFO) << "====Callback==== Finish";
}

} else {
BMFLOG(BMF_WARNING) << "Failed to extract frame number";
}

uint8_t bytes[] = {97, 98, 99, 100, 101, 0};
return bmf_sdk::CBytes{bytes, 6};
}

int main() {
std::string output_file = "./cb.mp4";

std::function<bmf_sdk::CBytes(bmf_sdk::CBytes)> de_callback = decoder_callback;
std::function<bmf_sdk::CBytes(bmf_sdk::CBytes)> en_callback = encoder_callback;

nlohmann::json graph_para = {
{"dump_graph", 0}
};
Expand All @@ -23,14 +117,14 @@ int main() {
{"input_path", "../../files/big_bunny_10s_30fps.mp4"},
};
auto video = graph.Decode(bmf_sdk::JsonParam(decode_para));
video.AddCallback(0, std::function<bmf_sdk::CBytes(bmf_sdk::CBytes)>(decoder_callback));

nlohmann::json encode_para = {
{"output_path", output_file},
{"video_params", {
{"codec", "h264"},
{"width", 320},
{"height", 240},
{"crf", 23},
{"preset", "veryfast"}
{"preset", "veryslow"}
}},
{"audio_params", {
{"codec", "aac"},
Expand All @@ -40,12 +134,12 @@ int main() {
}}
};

std::function<bmf_sdk::CBytes(bmf_sdk::CBytes)> callback = my_callback_function;


auto node = graph.Encode(video["video"], video["audio"],
bmf_sdk::JsonParam(encode_para));

node.AddCallback(0, callback);
node.AddCallback(0, en_callback);

graph.Run();

Expand Down
94 changes: 47 additions & 47 deletions bmf/test/cpp_builder/cpp_transcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,53 +318,53 @@ TEST(cpp_transcode, transcode_with_null_audio) {
" \"30.0662251656\"}");
}

TEST(cpp_transcode, transcode_cb) {
std::string output_file = "./cb.mp4";
BMF_CPP_FILE_REMOVE(output_file);

nlohmann::json graph_para = {
{"dump_graph", 0}
};
auto graph = bmf::builder::Graph(bmf::builder::NormalMode,
bmf_sdk::JsonParam(graph_para));

nlohmann::json decode_para = {
{"input_path", "../../files/big_bunny_10s_30fps.mp4"},
};
auto video = graph.Decode(bmf_sdk::JsonParam(decode_para));
nlohmann::json encode_para = {
{"output_path", output_file},
{"video_params", {
{"codec", "h264"},
{"width", 320},
{"height", 240},
{"crf", 23},
{"preset", "veryfast"}
}},
{"audio_params", {
{"codec", "aac"},
{"bit_rate", 128000},
{"sample_rate", 44100},
{"channels", 2}
}}
};
auto cb = [](void *, BMFCBytes) -> BMFCBytes {
BMFLOG(BMF_INFO) << "test cb cpp";
uint8_t bytes[] = {97, 98, 99, 100, 101, 0};
return BMFCBytes{bytes, 6};
};
BMFCallbackInstance cb_ = nullptr;
create_callback(cb, nullptr, &cb_);
graph.Encode(video["video"], video["audio"],
bmf_sdk::JsonParam(encode_para)).AddCallback(0, *cb_);
//std::cout << testing::internal::GetCapturedStdout();
graph.Run();
BMF_CPP_FILE_CHECK(
output_file,
"../transcode/cb.mp4|240|320|7.615000|MOV,MP4,M4A,3GP,3G2,MJ2|366635|348991|h264|{\"fps\":
\"30.0662251656\"}"
);
}
// TEST(cpp_transcode, transcode_cb) {
// std::string output_file = "./cb.mp4";
// BMF_CPP_FILE_REMOVE(output_file);

// nlohmann::json graph_para = {
// {"dump_graph", 0}
// };
// auto graph = bmf::builder::Graph(bmf::builder::NormalMode,
// bmf_sdk::JsonParam(graph_para));

// nlohmann::json decode_para = {
// {"input_path", "../../files/big_bunny_10s_30fps.mp4"},
// };
// auto video = graph.Decode(bmf_sdk::JsonParam(decode_para));
// nlohmann::json encode_para = {
// {"output_path", output_file},
// {"video_params", {
// {"codec", "h264"},
// {"width", 320},
// {"height", 240},
// {"crf", 23},
// {"preset", "veryfast"}
// }},
// {"audio_params", {
// {"codec", "aac"},
// {"bit_rate", 128000},
// {"sample_rate", 44100},
// {"channels", 2}
// }}
// };
// auto cb = [](void *, BMFCBytes) -> BMFCBytes {
// BMFLOG(BMF_INFO) << "test cb cpp";
// uint8_t bytes[] = {97, 98, 99, 100, 101, 0};
// return BMFCBytes{bytes, 6};
// };
// BMFCallbackInstance cb_ = nullptr;
// create_callback(cb, nullptr, &cb_);
// graph.Encode(video["video"], video["audio"],
// bmf_sdk::JsonParam(encode_para)).AddCallback(0, *cb_);
// //std::cout << testing::internal::GetCapturedStdout();
// graph.Run();
// BMF_CPP_FILE_CHECK(
// output_file,
// "../transcode/cb.mp4|240|320|7.615000|MOV,MP4,M4A,3GP,3G2,MJ2|366635|348991|h264|{\"fps\":
// \"30.0662251656\"}"
// );
// }

TEST(cpp_transcode, transcode_hls) {
std::string output_file = "./file000.ts";
Expand Down
2 changes: 2 additions & 0 deletions build_osx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ then
if [ "$1" = "clean" ]
then
rm -rf build_osx
rm -rf output
rm -rf CMakeFiles
exit
fi

Expand Down

0 comments on commit d4a6065

Please sign in to comment.