gstreamersource.hpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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) 2021 Intel Corporation
  6. #ifndef OPENCV_GAPI_STREAMING_GSTREAMER_GSTREAMERSOURCE_HPP
  7. #define OPENCV_GAPI_STREAMING_GSTREAMER_GSTREAMERSOURCE_HPP
  8. #include <opencv2/gapi/streaming/source.hpp>
  9. #include <opencv2/gapi/garg.hpp>
  10. #include <memory>
  11. namespace cv {
  12. namespace gapi {
  13. namespace wip {
  14. namespace gst {
  15. /**
  16. * @brief OpenCV's GStreamer streaming source.
  17. * Streams cv::Mat-s/cv::MediaFrame from passed GStreamer pipeline.
  18. *
  19. * This class implements IStreamSource interface.
  20. *
  21. * To create GStreamerSource instance you need to pass 'pipeline' and, optionally, 'outputType'
  22. * arguments into constructor.
  23. * 'pipeline' should represent GStreamer pipeline in form of textual description.
  24. * Almost any custom pipeline is supported which can be successfully ran via gst-launch.
  25. * The only two limitations are:
  26. * - there should be __one__ appsink element in the pipeline to pass data to OpenCV app.
  27. * Pipeline can actually contain many sink elements, but it must have one and only one
  28. * appsink among them.
  29. *
  30. * - data passed to appsink should be video-frame in NV12 or GRAY8 format.
  31. *
  32. * 'outputType' is used to select type of output data to produce: 'cv::MediaFrame' or 'cv::Mat'.
  33. * To produce 'cv::MediaFrame'-s you need to pass 'GStreamerSource::OutputType::FRAME' and,
  34. * correspondingly, 'GStreamerSource::OutputType::MAT' to produce 'cv::Mat'-s.
  35. * Please note, that in the last case, output 'cv::Mat' will be of BGR format, internal conversion
  36. * from NV12 / GRAY8 GStreamer data will happen.
  37. * Default value for 'outputType' is 'GStreamerSource::OutputType::MAT'.
  38. *
  39. * @note Stream sources are passed to G-API via shared pointers, so please use gapi::make_src<>
  40. * to create objects and ptr() to pass a GStreamerSource to cv::gin().
  41. *
  42. * @note You need to build OpenCV with GStreamer support to use this class.
  43. */
  44. class GStreamerPipelineFacade;
  45. class GAPI_EXPORTS GStreamerSource : public IStreamSource
  46. {
  47. public:
  48. class Priv;
  49. // Indicates what type of data should be produced by GStreamerSource: cv::MediaFrame or cv::Mat
  50. enum class OutputType {
  51. FRAME,
  52. MAT
  53. };
  54. GStreamerSource(const std::string& pipeline,
  55. const GStreamerSource::OutputType outputType =
  56. GStreamerSource::OutputType::MAT);
  57. GStreamerSource(std::shared_ptr<GStreamerPipelineFacade> pipeline,
  58. const std::string& appsinkName,
  59. const GStreamerSource::OutputType outputType =
  60. GStreamerSource::OutputType::MAT);
  61. bool pull(cv::gapi::wip::Data& data) override;
  62. GMetaArg descr_of() const override;
  63. ~GStreamerSource() override;
  64. protected:
  65. explicit GStreamerSource(std::unique_ptr<Priv> priv);
  66. std::unique_ptr<Priv> m_priv;
  67. };
  68. } // namespace gst
  69. using GStreamerSource = gst::GStreamerSource;
  70. // NB: Overload for using from python
  71. GAPI_EXPORTS_W cv::Ptr<IStreamSource>
  72. inline make_gst_src(const std::string& pipeline,
  73. const GStreamerSource::OutputType outputType =
  74. GStreamerSource::OutputType::MAT)
  75. {
  76. return make_src<GStreamerSource>(pipeline, outputType);
  77. }
  78. } // namespace wip
  79. } // namespace gapi
  80. } // namespace cv
  81. #endif // OPENCV_GAPI_STREAMING_GSTREAMER_GSTREAMERSOURCE_HPP