File: dynamic_pads.cc

package info (click to toggle)
gstreamermm-1.0 1.10.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,488 kB
  • sloc: xml: 68,148; cpp: 6,109; sh: 4,187; makefile: 243; perl: 236
file content (84 lines) | stat: -rw-r--r-- 2,252 bytes parent folder | download | duplicates (5)
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
/*
 * This example presents basic usage of dynamics Gst::Pad objects.
 */
#include <gstreamermm.h>
#include <glibmm/main.h>

#include <iostream>

int main(int argc, char *argv[])
{
  Gst::init(argc, argv);

  Glib::RefPtr<Glib::MainLoop> main_loop = Glib::MainLoop::create();

  // Create pipeline
  Glib::RefPtr<Gst::Pipeline> pipeline = Gst::Pipeline::create("my_pipeline");

  // Create elements
  Glib::RefPtr<Gst::Element> source = Gst::ElementFactory::create_element("videotestsrc", "source"),
      decodebin = Gst::ElementFactory::create_element("decodebin", "decoder"),
      sink = Gst::ElementFactory::create_element("autovideosink", "videosink");

  // Add elements to a pipeline
  try
  {
    pipeline->add(source)->add(decodebin)->add(sink);
  }
  catch (const std::runtime_error& ex)
  {
    std::cerr << "Exception while adding: " << ex.what() << std::endl;
    return 1;
  }

  // Link elements
  try
  {
    // We can't link decodebin with sink, because decodebin
    // doesn't have any src pad on start up.
    source->link(decodebin);
  }
  catch (const std::runtime_error& ex)
  {
    std::cerr << "Exception while linking: " << ex.what() << std::endl;
  }

  // Handle messages posted on bus
  pipeline->get_bus()->add_watch([main_loop] (const Glib::RefPtr<Gst::Bus>&,
                                     const Glib::RefPtr<Gst::Message>& message) {
    switch (message->get_message_type())
    {
    case Gst::MESSAGE_EOS:
    case Gst::MESSAGE_ERROR:
      main_loop->quit();
      break;
    default:
      break;
    }
  return true;
  });

  // Listen for newly created pads
  decodebin->signal_pad_added().connect([decodebin, sink] (const Glib::RefPtr<Gst::Pad>& pad) {
    std::cout << "New pad added to " << decodebin->get_name() << std::endl;
    std::cout << "Pad name: " << pad->get_name() << std::endl;

    Gst::PadLinkReturn ret = pad->link(sink->get_static_pad("sink"));

    if (ret != Gst::PAD_LINK_OK)
    {
      std::cout << "Cannot link pads. Error: " << ret << std::endl;
    }
    else
    {
      std::cout << "Pads linked correctly!" << std::endl;
    }
  });

  // Start the pipeline
  pipeline->set_state(Gst::STATE_PLAYING);
  main_loop->run();
  pipeline->set_state(Gst::STATE_NULL);

  return 0;
}