File size: 10,077 Bytes
dffe123 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | // This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2021 Intel Corporation
#include "gstreamerenv.hpp"
#include "gstreamer_pipeline_facade.hpp"
#include <opencv2/gapi/streaming/meta.hpp>
#include <logger.hpp>
#include <opencv2/imgproc.hpp>
#ifdef HAVE_GSTREAMER
#include <gst/app/gstappsink.h>
#include <gst/gstbuffer.h>
#include <gst/video/video-frame.h>
#include <gst/pbutils/missing-plugins.h>
namespace cv {
namespace gapi {
namespace wip {
namespace gst {
GStreamerPipelineFacade::GStreamerPipelineFacade():
m_isPrerolled(false),
m_isPlaying(false)
{ }
GStreamerPipelineFacade::GStreamerPipelineFacade(const std::string& pipelineDesc):
GStreamerPipelineFacade()
{
m_pipelineDesc = pipelineDesc;
// Initialize GStreamer library:
GStreamerEnv::init();
// Create GStreamer pipeline:
GError* error = NULL;
// gst_parse_launch [transfer floating]
m_pipeline = GST_ELEMENT(g_object_ref_sink(
gst_parse_launch(m_pipelineDesc.c_str(), &error)));
GStreamerPtr<GError> err(error);
if (err)
{
cv::util::throw_error(
std::runtime_error("Error in parsing pipeline: " + std::string(err->message)));
}
}
// The destructors are noexcept by default (since C++11).
GStreamerPipelineFacade::~GStreamerPipelineFacade()
{
// There is no mutex acquisition here, because we assume that no one will call this method
// directly.
// Destructor may be called on empty GStreamerSource object in case if
// exception is thrown during construction.
if (m_pipeline && GST_IS_ELEMENT(m_pipeline.get()))
{
try
{
setState(GST_STATE_NULL);
}
catch(...)
{
GAPI_LOG_WARNING(NULL, "Unable to stop pipeline in destructor.\n");
}
m_pipeline.release();
}
}
std::vector<GstElement*> GStreamerPipelineFacade::getElementsByFactoryName(
const std::string& factoryName)
{
std::vector<GstElement*> outElements = getElements(
[&factoryName](GstElement* element) {
GStreamerPtr<gchar> name(
gst_object_get_name(GST_OBJECT(gst_element_get_factory(element))));
return name && (0 == strcmp(name, factoryName.c_str()));
});
return outElements;
}
GstElement* GStreamerPipelineFacade::getElementByName(const std::string& elementName)
{
std::vector<GstElement*> outElements = getElements(
[&elementName](GstElement* element) {
GStreamerPtr<gchar> name(gst_element_get_name(element));
return name && (0 == strcmp(name, elementName.c_str()));
});
if (outElements.empty())
{
return nullptr;
}
else
{
GAPI_Assert(1ul == outElements.size());
return outElements[0];
}
}
void GStreamerPipelineFacade::completePreroll() {
// FIXME: If there are multiple sources in pipeline and one of them is live, then pipeline
// will return GST_STATE_CHANGE_NO_PREROLL while pipeline pausing.
// But appsink may not be connected to this live source and only to another,
// not-live ones. So, it is not required to start the playback for appsink to complete
// the preroll.
// Starting of playback for the not-live sources before the first frame pull will lead
// to losing of some amount of frames and pulling of the first frame can return frame
// which is far from the first.
//
// Need to handle this case or forbid to mix multiples sources of different
// categories(live, not-live) in the pipeline explicitly(assert).
if (!m_isPrerolled.load(std::memory_order_acquire))
{
std::lock_guard<std::mutex> lock(m_stateChangeMutex);
if(!m_isPrerolled.load(std::memory_order_relaxed))
{
PipelineState state = queryState();
// Only move forward in the pipeline's state machine
GAPI_Assert(state.current != GST_STATE_PLAYING);
GAPI_Assert(state.pending == GST_STATE_VOID_PENDING);
GstStateChangeReturn status = gst_element_set_state(m_pipeline, GST_STATE_PAUSED);
checkBusMessages();
if (status == GST_STATE_CHANGE_NO_PREROLL)
{
status = gst_element_set_state(m_pipeline, GST_STATE_PLAYING);
m_isPlaying.store(true);
}
verifyStateChange(status);
m_isPrerolled.store(true, std::memory_order_release);
}
}
}
void GStreamerPipelineFacade::play()
{
if (!m_isPlaying.load(std::memory_order_acquire))
{
std::lock_guard<std::mutex> lock(m_stateChangeMutex);
if (!m_isPlaying.load(std::memory_order_relaxed))
{
setState(GST_STATE_PLAYING);
m_isPlaying.store(true, std::memory_order_release);
m_isPrerolled.store(true);
}
}
}
bool GStreamerPipelineFacade::isPlaying() {
return m_isPlaying.load();
}
std::vector<GstElement*> GStreamerPipelineFacade::getElements(
std::function<bool(GstElement*)> comparator)
{
std::vector<GstElement*> outElements;
GStreamerPtr<GstIterator> it(gst_bin_iterate_elements(GST_BIN(m_pipeline.get())));
GValue value = G_VALUE_INIT;
GstIteratorResult status = gst_iterator_next(it, &value);
while (status != GST_ITERATOR_DONE && status != GST_ITERATOR_ERROR)
{
if (status == GST_ITERATOR_OK)
{
GstElement* element = GST_ELEMENT(g_value_get_object(&value));
if (comparator(element))
{
outElements.push_back(GST_ELEMENT(element));
}
g_value_unset(&value);
}
else if (status == GST_ITERATOR_RESYNC)
{
gst_iterator_resync(it);
}
status = gst_iterator_next(it, &value);
}
return outElements;
}
PipelineState GStreamerPipelineFacade::queryState()
{
GAPI_Assert(m_pipeline && GST_IS_ELEMENT(m_pipeline.get()) &&
"GStreamer pipeline has not been created!");
PipelineState state;
GstClockTime timeout = 5 * GST_SECOND;
gst_element_get_state(m_pipeline, &state.current, &state.pending, timeout);
return state;
}
void GStreamerPipelineFacade::setState(GstState newState)
{
PipelineState state = queryState();
GAPI_Assert(state.pending == GST_STATE_VOID_PENDING);
if (state.current != newState)
{
GstStateChangeReturn status = gst_element_set_state(m_pipeline, newState);
verifyStateChange(status);
}
}
void GStreamerPipelineFacade::verifyStateChange(GstStateChangeReturn status)
{
if (status == GST_STATE_CHANGE_ASYNC)
{
// Wait for status update.
status = gst_element_get_state(m_pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
}
if (status == GST_STATE_CHANGE_FAILURE)
{
checkBusMessages();
PipelineState state = queryState();
const gchar* currentState = gst_element_state_get_name(state.current);
const gchar* pendingState = gst_element_state_get_name(state.pending);
cv::util::throw_error(
std::runtime_error(std::string("Unable to change pipeline state from ") +
std::string(currentState) + std::string(" to ") +
std::string(pendingState)));
}
checkBusMessages();
}
// Handles GStreamer bus messages.
// For debugging purposes.
void GStreamerPipelineFacade::checkBusMessages() const
{
GStreamerPtr<GstBus> bus(gst_element_get_bus(m_pipeline));
while (gst_bus_have_pending(bus))
{
GStreamerPtr<GstMessage> msg(gst_bus_pop(bus));
if (!msg || !GST_IS_MESSAGE(msg.get()))
{
continue;
}
if (gst_is_missing_plugin_message(msg))
{
GStreamerPtr<gchar> descr(gst_missing_plugin_message_get_description(msg));
cv::util::throw_error(
std::runtime_error("Your GStreamer installation is missing a required plugin!"
"Details: " + std::string(descr)));
}
else
{
switch (GST_MESSAGE_TYPE(msg))
{
case GST_MESSAGE_STATE_CHANGED:
{
if (GST_MESSAGE_SRC(msg.get()) == GST_OBJECT(m_pipeline.get()))
{
GstState oldState = GST_STATE_NULL,
newState = GST_STATE_NULL;
gst_message_parse_state_changed(msg, &oldState, &newState, NULL);
const gchar* oldStateName = gst_element_state_get_name(oldState);
const gchar* newStateName = gst_element_state_get_name(newState);
GAPI_LOG_INFO(NULL, "Pipeline state changed from " << oldStateName << " to "
<< newStateName);
}
break;
}
case GST_MESSAGE_ERROR:
{
GError* error = NULL;
gchar* debug = NULL;
gst_message_parse_error(msg, &error, &debug); // transfer full for out args
GStreamerPtr<GError> err(error);
GStreamerPtr<gchar> deb(debug);
GStreamerPtr<gchar> name(gst_element_get_name(GST_MESSAGE_SRC(msg.get())));
GAPI_LOG_WARNING(NULL, "Embedded video playback halted; module " << name.get()
<< " reported: " << err->message);
GAPI_LOG_WARNING(NULL, "GStreamer debug: " << deb);
break;
}
default:
break;
}
}
}
}
} // namespace gst
} // namespace wip
} // namespace gapi
} // namespace cv
#endif // HAVE_GSTREAMER
|