source.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Copyright (C) 2019 Intel Corporation
  6. #ifndef OPENCV_GAPI_STREAMING_SOURCE_HPP
  7. #define OPENCV_GAPI_STREAMING_SOURCE_HPP
  8. #include <memory> // shared_ptr
  9. #include <type_traits> // is_base_of
  10. #include <opencv2/gapi/gmetaarg.hpp> // GMetaArg
  11. namespace cv {
  12. namespace gapi {
  13. namespace wip {
  14. struct Data; // forward-declaration of Data to avoid circular dependencies
  15. /**
  16. * @brief Abstract streaming pipeline source.
  17. *
  18. * Implement this interface if you want customize the way how data is
  19. * streaming into GStreamingCompiled.
  20. *
  21. * Objects implementing this interface can be passed to
  22. * GStreamingCompiled using setSource() with cv::gin(). Regular
  23. * compiled graphs (GCompiled) don't support input objects of this
  24. * type.
  25. *
  26. * Default cv::VideoCapture-based implementation is available, see
  27. * cv::gapi::wip::GCaptureSource.
  28. *
  29. * @note stream sources are passed to G-API via shared pointers, so
  30. * please use ptr() when passing a IStreamSource implementation to
  31. * cv::gin().
  32. */
  33. class IStreamSource: public std::enable_shared_from_this<IStreamSource>
  34. {
  35. public:
  36. using Ptr = std::shared_ptr<IStreamSource>;
  37. Ptr ptr() { return shared_from_this(); }
  38. virtual bool pull(Data &data) = 0;
  39. virtual GMetaArg descr_of() const = 0;
  40. virtual void halt() {
  41. // Do nothing by default to maintain compatibility with the existing sources...
  42. // In fact needs to be decorated atop of the child classes to maintain the behavior
  43. // FIXME: Make it mandatory in OpenCV 5.0
  44. };
  45. virtual ~IStreamSource() = default;
  46. };
  47. template<class T, class... Args>
  48. IStreamSource::Ptr inline make_src(Args&&... args)
  49. {
  50. static_assert(std::is_base_of<IStreamSource, T>::value,
  51. "T must implement the cv::gapi::IStreamSource interface!");
  52. auto src_ptr = std::make_shared<T>(std::forward<Args>(args)...);
  53. return src_ptr->ptr();
  54. }
  55. } // namespace wip
  56. } // namespace gapi
  57. } // namespace cv
  58. #endif // OPENCV_GAPI_STREAMING_SOURCE_HPP