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.
5 // Copyright (C) 2018 Intel Corporation
8 #ifndef OPENCV_GAPI_GSTREAMING_COMPILED_HPP
9 #define OPENCV_GAPI_GSTREAMING_COMPILED_HPP
13 #include <opencv2/gapi/opencv_includes.hpp>
14 #include <opencv2/gapi/own/assert.hpp>
15 #include <opencv2/gapi/garg.hpp>
16 #include <opencv2/gapi/streaming/source.hpp>
21 * \addtogroup gapi_main_classes
25 * @brief Represents a computation (graph) compiled for streaming.
27 * This class represents a product of graph compilation (calling
28 * cv::GComputation::compileStreaming()). Objects of this class
29 * actually do stream processing, and the whole pipeline execution
30 * complexity is incapsulated into objects of this class. Execution
31 * model has two levels: at the very top, the execution of a
32 * heterogeneous graph is aggressively pipelined; at the very bottom
33 * the execution of every internal block is determined by its
34 * associated backend. Backends are selected based on kernel packages
35 * passed via compilation arguments ( see @ref gapi_compile_args,
36 * GNetworkPackage, GKernelPackage for details).
38 * GStreamingCompiled objects have a "player" semantics -- there are
39 * methods like start() and stop(). GStreamingCompiled has a full
40 * control over a videostream and so is stateful. You need to specify the
41 * input stream data using setSource() and then call start() to
42 * actually start processing. After that, use pull() or try_pull() to
43 * obtain next processed data frame from the graph in a blocking or
44 * non-blocking way, respectively.
46 * Currently a single GStreamingCompiled can process only one video
47 * streat at time. Produce multiple GStreamingCompiled objects to run the
48 * same graph on multiple video streams.
52 class GAPI_EXPORTS GStreamingCompiled
55 class GAPI_EXPORTS Priv;
58 // FIXME: More overloads?
60 * @brief Specify the input data to GStreamingCompiled for
61 * processing, a generic version.
63 * Use gin() to create an input parameter vector.
65 * Input vectors must have the same number of elements as defined
66 * in the cv::GComputation protocol (at the moment of its
67 * construction). Shapes of elements also must conform to protocol
68 * (e.g. cv::Mat needs to be passed where cv::GMat has been
69 * declared as input, and so on). Run-time exception is generated
72 * In contrast with regular GCompiled, user can also pass an
73 * object of type GVideoCapture for a GMat parameter of the parent
74 * GComputation. The compiled pipeline will start fetching data
75 * from that GVideoCapture and feeding it into the
76 * pipeline. Pipeline stops when a GVideoCapture marks end of the
77 * stream (or when stop() is called).
79 * Passing a regular Mat for a GMat parameter makes it "infinite"
80 * source -- pipeline may run forever feeding with this Mat until
83 * Currently only a single GVideoCapture is supported as input. If
84 * the parent GComputation is declared with multiple input GMat's,
85 * one of those can be specified as GVideoCapture but all others
86 * must be regular Mat objects.
88 * Throws if pipeline is already running. Use stop() and then
89 * setSource() to run the graph on a new video stream.
91 * @note This method is not thread-safe (with respect to the user
92 * side) at the moment. Protect the access if
93 * start()/stop()/setSource() may be called on the same object in
94 * multiple threads in your application.
96 * @param ins vector of inputs to process.
99 void setSource(GRunArgs &&ins);
102 * @brief Specify an input video stream for a single-input
103 * computation pipeline.
105 * Throws if pipeline is already running. Use stop() and then
106 * setSource() to run the graph on a new video stream.
109 * @param s a shared pointer to IStreamSource representing the
110 * input video stream.
112 void setSource(const gapi::wip::IStreamSource::Ptr& s);
115 * @brief Start the pipeline execution.
117 * Use pull()/try_pull() to obtain data. Throws an exception if
118 * a video source was not specified.
120 * setSource() must be called first, even if the pipeline has been
121 * working already and then stopped (explicitly via stop() or due
124 * @note This method is not thread-safe (with respect to the user
125 * side) at the moment. Protect the access if
126 * start()/stop()/setSource() may be called on the same object in
127 * multiple threads in your application.
132 * @brief Get the next processed frame from the pipeline.
134 * Use gout() to create an output parameter vector.
136 * Output vectors must have the same number of elements as defined
137 * in the cv::GComputation protocol (at the moment of its
138 * construction). Shapes of elements also must conform to protocol
139 * (e.g. cv::Mat needs to be passed where cv::GMat has been
140 * declared as output, and so on). Run-time exception is generated
143 * This method writes new data into objects passed via output
144 * vector. If there is no data ready yet, this method blocks. Use
145 * try_pull() if you need a non-blocking version.
147 * @param outs vector of output parameters to obtain.
148 * @return true if next result has been obtained,
149 * false marks end of the stream.
151 bool pull(cv::GRunArgsP &&outs);
154 * @brief Try to get the next processed frame from the pipeline.
156 * Use gout() to create an output parameter vector.
158 * This method writes new data into objects passed via output
159 * vector. If there is no data ready yet, the output vector
160 * remains unchanged and false is returned.
162 * @return true if data has been obtained, and false if it was
163 * not. Note: false here doesn't mark the end of the stream.
165 bool try_pull(cv::GRunArgsP &&outs);
168 * @brief Stop (abort) processing the pipeline.
170 * Note - it is not pause but a complete stop. Calling start()
171 * will cause G-API to start processing the stream from the early beginning.
173 * Throws if the pipeline is not running.
178 * @brief Test if the pipeline is running.
180 * @note This method is not thread-safe (with respect to the user
181 * side) at the moment. Protect the access if
182 * start()/stop()/setSource() may be called on the same object in
183 * multiple threads in your application.
185 * @return true if the current stream is not over yet.
187 bool running() const;
193 * @brief Check if compiled object is valid (non-empty)
195 * @return true if the object is runnable (valid), false otherwise
197 explicit operator bool () const;
200 * @brief Vector of metadata this graph was compiled for.
202 * @return Unless _reshape_ is not supported, return value is the
203 * same vector which was passed to cv::GComputation::compile() to
204 * produce this compiled object. Otherwise, it is the latest
205 * metadata vector passed to reshape() (if that call was
208 const GMetaArgs& metas() const; // Meta passed to compile()
211 * @brief Vector of metadata descriptions of graph outputs
213 * @return vector with formats/resolutions of graph's output
214 * objects, auto-inferred from input metadata vector by
215 * operations which form this computation.
217 * @note GCompiled objects produced from the same
218 * cv::GComputiation graph with different input metas may return
219 * different values in this vector.
221 const GMetaArgs& outMetas() const;
225 std::shared_ptr<Priv> m_priv;
231 #endif // OPENCV_GAPI_GSTREAMING_COMPILED_HPP