| 1 | /* gstreamermm - a C++ wrapper for gstreamer |
| 2 | * |
| 3 | * Copyright 2009 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 | #include <glibmm/exceptionhandler.h> |
| 22 | #include <iostream> |
| 23 | |
| 24 | Glib::RefPtr<Glib::MainLoop> mainloop; |
| 25 | bool have_type = false; |
| 26 | |
| 27 | static bool on_structure_foreach(const Glib::QueryQuark& id, const Glib::ValueBase& value) |
| 28 | { |
| 29 | const Glib::ustring str_id = id; |
| 30 | gchar* str_value = g_strdup_value_contents(value.gobj()); |
| 31 | std::cout << "Structure field: id=" << str_id << ", value=" << str_value << std::endl; |
| 32 | g_free(str_value); |
| 33 | |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | static void on_typefind_have_type(guint probability, const Glib::RefPtr<Gst::Caps>& caps) |
| 38 | { |
| 39 | have_type = true; |
| 40 | |
| 41 | std::cout << "have-type: probability=" << probability << std::endl; |
| 42 | |
| 43 | if(!caps) |
| 44 | { |
| 45 | std::cerr << "on_typefind_have_type(): caps is null" << std::endl; |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | if(!caps->size()) |
| 50 | { |
| 51 | std::cerr << "on_typefind_have_type(): caps is empty" << std::endl; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | Gst::Structure structure = caps->get_structure(0); |
| 56 | const Glib::ustring mime_type = structure.get_name(); |
| 57 | std::cout << "have-type: mime_type=" << mime_type << std::endl; |
| 58 | |
| 59 | structure.foreach( sigc::ptr_fun(&on_structure_foreach) ); |
| 60 | |
| 61 | if(mainloop) |
| 62 | mainloop->quit(); |
| 63 | else |
| 64 | std::cerr << "on_typefind_have_type(): mainloop is null" << std::endl; |
| 65 | } |
| 66 | |
| 67 | #ifdef GSTREAMERMM_DISABLE_DEPRECATED |
| 68 | |
| 69 | #include <gst/gsttypefind.h> |
| 70 | |
| 71 | static void TypeFindElement_signal_have_type_callback(struct _GstTypeFindElement* self, guint p0,GstCaps* p1,void* data) |
| 72 | { |
| 73 | using SlotType = sigc::slot< void,guint,const Glib::RefPtr<Gst::Caps>& >; |
| 74 | |
| 75 | auto obj = dynamic_cast<Gst::Element*>(Glib::ObjectBase::_get_current_wrapper((GObject*) self)); |
| 76 | if(obj) |
| 77 | { |
| 78 | try |
| 79 | { |
| 80 | if(const auto slot = Glib::SignalProxyNormal::data_to_slot(data)) |
| 81 | (*static_cast<SlotType*>(slot))(p0, Glib::wrap(p1, true)); |
| 82 | } |
| 83 | catch(...) |
| 84 | { |
| 85 | Glib::exception_handlers_invoke(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | |
| 91 | static const Glib::SignalProxyInfo TypeFindElement_signal_have_type_info = |
| 92 | { |
| 93 | "have-type", |
| 94 | (GCallback) &TypeFindElement_signal_have_type_callback, |
| 95 | (GCallback) &TypeFindElement_signal_have_type_callback |
| 96 | }; |
| 97 | #endif |
| 98 | |
| 99 | int main(int argc, char** argv) |
| 100 | { |
| 101 | // Initialize Gstreamermm: |
| 102 | Gst::init(argc, argv); |
| 103 | |
| 104 | //Check input arguments: |
| 105 | if(argc < 2) |
| 106 | { |
| 107 | std::cout << "Usage: " << argv[0] << " <filename>" << std::endl; |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | const std::string filename = argv[1]; |
| 112 | |
| 113 | // Create pipeline: |
| 114 | Glib::RefPtr<Gst::Pipeline> pipeline = Gst::Pipeline::create("my-pipeline"); |
| 115 | |
| 116 | // Create elements: |
| 117 | #ifndef GSTREAMERMM_DISABLE_DEPRECATED |
| 118 | Glib::RefPtr<Gst::FileSrc> element_source = Gst::FileSrc::create(); |
| 119 | element_source->property_location() = filename; |
| 120 | Glib::RefPtr<Gst::FakeSink> element_sink = Gst::FakeSink::create(); |
| 121 | Glib::RefPtr<Gst::TypeFindElement> element_typefind = Gst::TypeFindElement::create(); |
| 122 | element_typefind->signal_have_type().connect( |
| 123 | sigc::ptr_fun(&on_typefind_have_type) ); |
| 124 | |
| 125 | #else |
| 126 | Glib::RefPtr<Gst::Element> element_source = Gst::ElementFactory::create_element("filesrc"); |
| 127 | element_source->set_property("location", filename); |
| 128 | Glib::RefPtr<Gst::Element> element_sink = Gst::ElementFactory::create_element("fakesink"); |
| 129 | Glib::RefPtr<Gst::Element> element_typefind = Gst::ElementFactory::create_element("typefind"); |
| 130 | |
| 131 | Glib::SignalProxy< void,guint,const Glib::RefPtr<Gst::Caps>& > proxy(element_typefind.operator->(), &TypeFindElement_signal_have_type_info); |
| 132 | proxy.connect(sigc::ptr_fun(&on_typefind_have_type)); |
| 133 | #endif |
| 134 | |
| 135 | //If using an MP3 file, this should provide more Caps information from typefind. |
| 136 | //Glib::RefPtr<Gst::Element> element_id3demux = Gst::ElementFactory::create_element("id3demux"); |
| 137 | //g_assert(element_id3demux); |
| 138 | |
| 139 | // We must add the elements to the pipeline before linking them: |
| 140 | try |
| 141 | { |
| 142 | pipeline->add(element_source)->add(element_typefind)->add(element_sink); |
| 143 | //pipeline->add(element_source)->add(element_id3demux)->add(element_typefind)->add(element_sink); |
| 144 | } |
| 145 | catch (std::runtime_error& ex) |
| 146 | { |
| 147 | std::cerr << "Exception while adding: " << ex.what() << std::endl; |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | // Link the elements together: |
| 152 | try |
| 153 | { |
| 154 | //element_source->link(element_id3demux)->link(element_typefind)->link(element_sink); |
| 155 | element_source->link(element_typefind)->link(element_sink); |
| 156 | } |
| 157 | catch(const std::runtime_error& error) |
| 158 | { |
| 159 | std::cerr << "Exception while linking: " << error.what() << std::endl; |
| 160 | return 1; |
| 161 | } |
| 162 | |
| 163 | // Now set the whole pipeline to playing and start the main loop: |
| 164 | std::cout << "Setting pipeline to PLAYING." << std::endl; |
| 165 | mainloop = Glib::MainLoop::create(); |
| 166 | pipeline->set_state(Gst::STATE_PLAYING); |
| 167 | std::cout << "Pipeline is playing." << std::endl; |
| 168 | if(!have_type) |
| 169 | mainloop->run(); |
| 170 | |
| 171 | // Clean up nicely: |
| 172 | std::cout << "Returned. Stopping pipeline." << std::endl; |
| 173 | pipeline->set_state(Gst::STATE_NULL); |
| 174 | |
| 175 | return 0; |
| 176 | } |