| 1 | /* gstreamermm - a C++ wrapper for gstreamer |
| 2 | * |
| 3 | * Copyright 2009-2016 The gstreamermm Development Team |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 |
| 7 | * as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | #include <gstreamermm.h> |
| 20 | #include <glibmm/main.h> |
| 21 | |
| 22 | #include <regex> |
| 23 | #include <iostream> |
| 24 | |
| 25 | Glib::RefPtr<Glib::MainLoop> mainloop; |
| 26 | |
| 27 | // Message watch function |
| 28 | bool bus_message_watch(const Glib::RefPtr<Gst::Bus>& /* bus */, const Glib::RefPtr<Gst::Message>& message) |
| 29 | { |
| 30 | switch (message->get_message_type()) |
| 31 | { |
| 32 | case Gst::MESSAGE_ERROR: |
| 33 | case Gst::MESSAGE_EOS: |
| 34 | mainloop->quit(); |
| 35 | default: |
| 36 | break; |
| 37 | } |
| 38 | |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | int main(int argc, char** argv) |
| 43 | { |
| 44 | // Initialize Gstreamermm: |
| 45 | Gst::init(argc, argv); |
| 46 | |
| 47 | //Check input arguments: |
| 48 | if(argc < 2) |
| 49 | { |
| 50 | std::cout << "Usage: " << argv[0] << " <filename>" << std::endl; |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | const std::string filename = argv[1]; |
| 55 | |
| 56 | // Create pipeline: |
| 57 | Glib::RefPtr<Gst::Pipeline> pipeline = Gst::Pipeline::create("my-pipeline"); |
| 58 | |
| 59 | // Create elements: |
| 60 | Glib::RefPtr<Gst::Element> source = Gst::ElementFactory::create_element("filesrc"), |
| 61 | decoder = Gst::ElementFactory::create_element("decodebin"), |
| 62 | tee = Gst::ElementFactory::create_element("tee"), |
| 63 | audio_queue = Gst::ElementFactory::create_element("queue"), |
| 64 | video_queue = Gst::ElementFactory::create_element("queue"), |
| 65 | audio_convert = Gst::ElementFactory::create_element("audioconvert"), |
| 66 | audio_convert_4v = Gst::ElementFactory::create_element("audioconvert"), |
| 67 | video_convert = Gst::ElementFactory::create_element("videoconvert"), |
| 68 | goom = Gst::ElementFactory::create_element("goom"), |
| 69 | resample = Gst::ElementFactory::create_element("audioresample"), |
| 70 | audio_sink = Gst::ElementFactory::create_element("autoaudiosink"), |
| 71 | video_sink = Gst::ElementFactory::create_element("autovideosink"); |
| 72 | |
| 73 | if (!source || !decoder || !tee || !audio_queue || !video_queue || !goom || !audio_convert_4v |
| 74 | || !audio_convert || !video_convert || !resample || !audio_sink || !video_sink) |
| 75 | std::cerr << "One of the elements could not be created." << std::endl; |
| 76 | |
| 77 | // Set location property of filesrc element |
| 78 | source->set_property<Glib::ustring>("location", argv[1]); |
| 79 | |
| 80 | // We must add the elements to the pipeline before linking them: |
| 81 | try |
| 82 | { |
| 83 | pipeline->add(source)->add(decoder)->add(tee)->add(audio_queue)->add(video_queue) |
| 84 | ->add(goom)->add(audio_convert_4v)->add(audio_convert)->add(video_convert) |
| 85 | ->add(resample)->add(audio_sink)->add(video_sink); |
| 86 | } |
| 87 | catch (std::runtime_error& ex) |
| 88 | { |
| 89 | std::cerr << "Exception while adding: " << ex.what() << std::endl; |
| 90 | return 1; |
| 91 | } |
| 92 | |
| 93 | // Lik tee with queues |
| 94 | Glib::RefPtr<Gst::PadTemplate> tee_src_tpl = tee->get_pad_template("src_%u"); |
| 95 | if (tee->request_pad(tee_src_tpl)->link(audio_queue->get_static_pad("sink")) != Gst::PAD_LINK_OK) |
| 96 | { |
| 97 | std::cerr << "Cannot link tee pad with audio queue" << std::endl; |
| 98 | return 1; |
| 99 | } |
| 100 | if (tee->request_pad(tee_src_tpl)->link(video_queue->get_static_pad("sink")) != Gst::PAD_LINK_OK) |
| 101 | { |
| 102 | std::cerr << "Cannot link tee pad with video queue" << std::endl; |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | // Link other elements together: |
| 107 | try |
| 108 | { |
| 109 | source->link(decoder); |
| 110 | audio_queue->link(audio_convert)->link(resample)->link(audio_sink); |
| 111 | video_queue->link(audio_convert_4v)->link(goom)->link(video_convert)->link(video_sink); |
| 112 | } |
| 113 | catch(const std::runtime_error& error) |
| 114 | { |
| 115 | std::cerr << "Exception while linking: " << error.what() << std::endl; |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | // Signal handler for on-pad-added signal of decoder element |
| 120 | decoder->signal_pad_added().connect([tee] (const Glib::RefPtr<Gst::Pad> &pad) { |
| 121 | Glib::RefPtr<Gst::Caps> caps = pad->get_current_caps(); |
| 122 | std::string media_type = caps->get_structure(0).get_name(); |
| 123 | |
| 124 | std::cout << "New pad: " << pad->get_name() << ", media type: " << media_type << std::endl; |
| 125 | |
| 126 | if (std::regex_search(media_type, std::regex("^audio"))) |
| 127 | { |
| 128 | if (pad->link(tee->get_static_pad("sink")) != Gst::PAD_LINK_OK) |
| 129 | { |
| 130 | std::cerr << "Cannot link pad with queue" << std::endl; |
| 131 | } |
| 132 | } |
| 133 | else |
| 134 | std::cerr << "Unsupported media type" << std::endl; |
| 135 | }); |
| 136 | |
| 137 | // Add watch to pipeline's bus. |
| 138 | pipeline->get_bus()->add_watch(sigc::ptr_fun(&bus_message_watch)); |
| 139 | |
| 140 | |
| 141 | // Now set the whole pipeline to playing and start the main loop: |
| 142 | std::cout << "Setting pipeline to PLAYING." << std::endl; |
| 143 | mainloop = Glib::MainLoop::create(); |
| 144 | pipeline->set_state(Gst::STATE_PLAYING); |
| 145 | std::cout << "Pipeline is playing." << std::endl; |
| 146 | mainloop->run(); |
| 147 | |
| 148 | std::cout << "Returned. Stopping pipeline." << std::endl; |
| 149 | pipeline->set_state(Gst::STATE_NULL); |
| 150 | |
| 151 | return 0; |
| 152 | } |