Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. void GstProducerControl::start(const QUrl &destination)
  2. {
  3. emit stateChanged(GstMediaObject::ObjectState::StartingState);
  4.  
  5. if (!destination.isValid()) {
  6. stop(GstMediaObject::ObjectError::InvalidSourceError,
  7. tr("Invalid destination URL resource"));
  8. return;
  9. } else if (destination.scheme() != QLatin1String("udp")) {
  10. stop(GstMediaObject::ObjectError::InvalidSourceError,
  11. tr("Invalid destination URL scheme"));
  12. return;
  13. }
  14.  
  15. m_appsrc = ::gst_element_factory_make("appsrc", nullptr);
  16.  
  17. ::g_object_set(G_OBJECT(m_appsrc),
  18. "stream-type", GST_APP_STREAM_TYPE_STREAM,
  19. "is-live", true,
  20. "format", GST_FORMAT_TIME,
  21. nullptr);
  22.  
  23. const auto videoconvert = ::gst_element_factory_make("videoconvert", nullptr);
  24.  
  25. const auto encname = detectBestEncoderFactoryName("video/x-h264").toUtf8();
  26. const auto videoenc = ::gst_element_factory_make(encname.constData(), nullptr);
  27. if (!videoenc) {
  28. qCCritical(gstProducerControl) << "Unable to create videoenc element";
  29. }
  30.  
  31. if (encname == "x264enc") {
  32. ::gst_util_set_object_arg(G_OBJECT(videoenc), "tune", "zerolatency");
  33.  
  34. ::g_object_set(G_OBJECT(videoenc),
  35. "key-int-max", 10,
  36. nullptr);
  37. }
  38.  
  39. const auto videofilter = ::gst_element_factory_make("capsfilter", nullptr);
  40. const auto videofiltercaps = ::gst_caps_new_simple(
  41. "video/x-h264",
  42. "profile", G_TYPE_STRING, "main",
  43. nullptr);
  44.  
  45. ::g_object_set(G_OBJECT(videofilter),
  46. "caps", videofiltercaps,
  47. nullptr);
  48.  
  49. const auto pay = ::gst_element_factory_make("rtph264pay", nullptr);
  50.  
  51. ::g_object_set(G_OBJECT(pay),
  52. "config-interval", 1,
  53. nullptr);
  54.  
  55. const auto udpsink = ::gst_element_factory_make("udpsink", nullptr);
  56.  
  57. const auto host = destination.host().toLocal8Bit();
  58. const auto port = destination.port();
  59.  
  60. ::g_object_set(G_OBJECT(udpsink),
  61. "host", host.constData(),
  62. "port", port,
  63. nullptr);
  64.  
  65. m_pipeline = ::gst_pipeline_new("pipeline");
  66.  
  67. const auto bus = ::gst_pipeline_get_bus(GST_PIPELINE(m_pipeline));
  68. ::gst_bus_set_sync_handler(bus, GstProducerControl::processBusMessage,
  69. this, nullptr);
  70. ::gst_object_unref(bus);
  71.  
  72. if (!m_appsrc || !videoconvert || !videoenc || !videofilter
  73. || !pay || !udpsink) {
  74. qCCritical(gstProducerControl) << "Unable to create one of pipeline element";
  75. }
  76.  
  77. ::gst_bin_add_many(GST_BIN(m_pipeline),
  78. m_appsrc,
  79. videoconvert,
  80. videoenc,
  81. videofilter,
  82. pay,
  83. udpsink,
  84. nullptr);
  85.  
  86. const auto result = ::gst_element_link_many(m_appsrc,
  87. videoconvert,
  88. videoenc,
  89. videofilter,
  90. pay,
  91. udpsink,
  92. nullptr);
  93.  
  94. if (!result) {
  95. qCCritical(gstProducerControl) << "Pipeline linking failed";
  96.  
  97. stop(GstMediaObject::ObjectError::ResourceError,
  98. tr("Unable to link the pipeline"));
  99. return;
  100. } else {
  101. const auto status = ::gst_element_set_state(m_pipeline, GST_STATE_PLAYING);
  102. if (status == GST_STATE_CHANGE_FAILURE) {
  103. qCCritical(gstProducerControl) << "Pipeline starting failed";
  104.  
  105. stop(GstMediaObject::ObjectError::ResourceError,
  106. tr("Unable to start pipeline"));
  107. return;
  108. } else {
  109. m_timestamp.start();
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement