cpp_data_handler_test.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <pybind11/pybind11.h>
  2. #include <pybind11/stl.h>
  3. #include <memory>
  4. #include <string>
  5. #include <unordered_map>
  6. #include "cnstream_module.hpp"
  7. #include "data_source.hpp"
  8. namespace py = pybind11;
  9. namespace cnstream {
  10. class TestIModuleObserver : public IModuleObserver {
  11. public:
  12. void notify(std::shared_ptr<CNFrameInfo> data) override {
  13. cnstream::RwLockWriteGuard guard(lock_);
  14. if (stream_counts_.find(data->stream_id) != stream_counts_.end()) {
  15. stream_counts_[data->stream_id]++;
  16. } else {
  17. stream_counts_.insert({data->stream_id, 1});
  18. }
  19. // auto frame = data->collection.Get<std::shared_ptr<CNDataFrame>>(kCNDataFrameTag);
  20. // auto mat = frame->ImageBGR();
  21. // cv::imwrite("./output/" + data->stream_id + "_" + std::to_string(frame->frame_id) + ".jpg", mat);
  22. }
  23. int GetCount(std::string stream_id) {
  24. cnstream::RwLockReadGuard guard(lock_);
  25. if (stream_counts_.find(stream_id) != stream_counts_.end()) {
  26. return stream_counts_[stream_id];
  27. } else {
  28. return -1;
  29. }
  30. }
  31. private:
  32. int count_ = 0;
  33. cnstream::RwLock lock_;
  34. std::unordered_map<std::string, int> stream_counts_;
  35. };
  36. class CppDataHanlderTestHelper {
  37. public:
  38. CppDataHanlderTestHelper() {}
  39. void SetObserver(std::shared_ptr<DataSource> module) {
  40. observer_ = new TestIModuleObserver;
  41. module->SetObserver(observer_);
  42. }
  43. int GetCount(std::string stream_id) {
  44. if (observer_) {
  45. return observer_->GetCount(stream_id);
  46. }
  47. return -1;
  48. }
  49. private:
  50. TestIModuleObserver* observer_ = nullptr;
  51. }; // class CppDataHanlderTestHelper
  52. } // namespace cnstream
  53. void DataHanlderWrapper(const py::module& m) {
  54. py::class_<cnstream::CppDataHanlderTestHelper>(m, "CppDataHanlderTestHelper")
  55. .def(py::init())
  56. .def("set_observer", &cnstream::CppDataHanlderTestHelper::SetObserver)
  57. .def("get_count", &cnstream::CppDataHanlderTestHelper::GetCount);
  58. }