| 1 | /* |
| 2 | * main.cc |
| 3 | * |
| 4 | * Created on: Aug 14, 2013 |
| 5 | * Author: m.kolny |
| 6 | * |
| 7 | * based on sample described here: http://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/section-dynamic-pipelines.html |
| 8 | */ |
| 9 | |
| 10 | #include <gstreamermm.h> |
| 11 | #include <glibmm/main.h> |
| 12 | #include <iostream> |
| 13 | #include <vector> |
| 14 | |
| 15 | using namespace Gst; |
| 16 | using Glib::RefPtr; |
| 17 | |
| 18 | RefPtr<Pad> blockpad; |
| 19 | RefPtr<Element> conv_before, |
| 20 | conv_after, |
| 21 | curr_effect; |
| 22 | RefPtr<Pipeline> pipeline; |
| 23 | std::vector<Glib::ustring> effects = {"identity", "exclusion", |
| 24 | "navigationtest", "agingtv", "videoflip", "vertigotv", |
| 25 | "gaussianblur", "shagadelictv", "edgetv"}; |
| 26 | int curr_position = 0; |
| 27 | |
| 28 | RefPtr<Glib::MainLoop> main_loop; |
| 29 | |
| 30 | PadProbeReturn event_probe_cb (const RefPtr<Pad>& pad, const PadProbeInfo& info) |
| 31 | { |
| 32 | RefPtr<Event> event = info.get_event(); |
| 33 | if (event->get_event_type()!= EVENT_EOS) |
| 34 | return PAD_PROBE_OK; |
| 35 | |
| 36 | pad->remove_probe(info.get_id()); |
| 37 | |
| 38 | curr_effect->set_state(STATE_NULL); |
| 39 | pipeline->remove(curr_effect); |
| 40 | curr_effect = ElementFactory::create_element(effects[curr_position++ % effects.size()]); |
| 41 | pipeline->add(curr_effect); |
| 42 | conv_before->link(curr_effect)->link(conv_after); |
| 43 | curr_effect->set_state(STATE_PLAYING); |
| 44 | |
| 45 | return PAD_PROBE_DROP; |
| 46 | } |
| 47 | |
| 48 | PadProbeReturn pad_probe_cb(const RefPtr<Pad>& pad, const PadProbeInfo& info) |
| 49 | { |
| 50 | pad->remove_probe(info.get_id()); |
| 51 | |
| 52 | RefPtr<Pad> srcpad = curr_effect->get_static_pad("src"); |
| 53 | srcpad->add_probe(PAD_PROBE_TYPE_BLOCK | PAD_PROBE_TYPE_EVENT_DOWNSTREAM, sigc::ptr_fun(event_probe_cb)); |
| 54 | RefPtr<Pad> sinkpad = curr_effect->get_static_pad("sink"); |
| 55 | sinkpad->send_event(EventEos::create()); |
| 56 | return PAD_PROBE_OK; |
| 57 | } |
| 58 | |
| 59 | bool on_bus_message(const Glib::RefPtr<Gst::Bus>&, |
| 60 | const Glib::RefPtr<Gst::Message>& message) |
| 61 | { |
| 62 | switch (message->get_message_type()) |
| 63 | { |
| 64 | case MESSAGE_ERROR: |
| 65 | std::cerr << "Error." << std::endl; |
| 66 | main_loop->quit(); |
| 67 | return false; |
| 68 | default: |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | return true; |
| 73 | } |
| 74 | bool on_timeout() |
| 75 | { |
| 76 | blockpad->add_probe(PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, sigc::ptr_fun(&pad_probe_cb)); |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | int main(int argc, char** argv) |
| 81 | { |
| 82 | init(argc, argv); |
| 83 | main_loop = Glib::MainLoop::create(); |
| 84 | |
| 85 | pipeline = Pipeline::create("effects-pipeline"); |
| 86 | |
| 87 | RefPtr<Element> src = ElementFactory::create_element("videotestsrc"), |
| 88 | sink = ElementFactory::create_element("xvimagesink"); |
| 89 | |
| 90 | conv_before = ElementFactory::create_element("videoconvert"); |
| 91 | curr_effect = ElementFactory::create_element(effects[curr_position++]); |
| 92 | conv_after = ElementFactory::create_element("videoconvert"); |
| 93 | |
| 94 | blockpad = src->get_static_pad("src"); |
| 95 | src->property("is-live", true); |
| 96 | |
| 97 | pipeline->add(src)->add(conv_before)->add(curr_effect)->add(conv_after)->add(sink); |
| 98 | src->link(conv_before)->link(curr_effect)->link(conv_after)->link(sink); |
| 99 | |
| 100 | RefPtr<Bus> bus = pipeline->get_bus(); |
| 101 | bus->add_watch(sigc::ptr_fun(on_bus_message)); |
| 102 | |
| 103 | pipeline->set_state(STATE_PLAYING); |
| 104 | |
| 105 | Glib::signal_timeout().connect(sigc::ptr_fun(&on_timeout), 1000); |
| 106 | main_loop->run(); |
| 107 | |
| 108 | pipeline->set_state(STATE_NULL); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 |