| 1 | /* gstreamermm - a C++ wrapper for gstreamer |
| 2 | * |
| 3 | * Copyright 2008 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 <iostream> |
| 22 | #include <iomanip> |
| 23 | |
| 24 | namespace |
| 25 | { |
| 26 | |
| 27 | Glib::RefPtr<Glib::MainLoop> mainloop; |
| 28 | Glib::RefPtr<Gst::Pipeline> pipeline; |
| 29 | Glib::RefPtr<Gst::Element> decoder; |
| 30 | gulong data_probe_id = 0; |
| 31 | |
| 32 | bool on_timeout() |
| 33 | { |
| 34 | Gst::Format fmt = Gst::FORMAT_TIME; |
| 35 | gint64 pos = 0; |
| 36 | gint64 len = 0; |
| 37 | |
| 38 | Glib::RefPtr<Gst::Query> query = Gst::QueryPosition::create(fmt); |
| 39 | |
| 40 | if(pipeline->query(query) |
| 41 | && pipeline->query_duration(fmt, len)) |
| 42 | { |
| 43 | // Cast query's RefPtr to RefPtr<Gst::QueryPosition> to parse the |
| 44 | // pipeline's position query with the Gst::QueryPosition::parse() method |
| 45 | Glib::RefPtr<Gst::QueryPosition> query_pos = |
| 46 | Glib::RefPtr<Gst::QueryPosition>::cast_static(query); |
| 47 | if(query_pos) |
| 48 | pos = query_pos->parse(); |
| 49 | |
| 50 | std::cout << std::right << "Time: " << std::setfill('0') << |
| 51 | std::setw(3) << Gst::get_hours(pos) << ":" << |
| 52 | std::setw(2) << Gst::get_minutes(pos) << ":" << |
| 53 | std::setw(2) << Gst::get_seconds(pos) << "." << |
| 54 | std::setw(9) << std::left << Gst::get_fractional_seconds(pos); |
| 55 | |
| 56 | std::cout << std::right << "/" << |
| 57 | std::setw(3) << Gst::get_hours(len) << ":" << |
| 58 | std::setw(2) << Gst::get_minutes(len) << ":" << |
| 59 | std::setw(2) << Gst::get_seconds(len) << "." << |
| 60 | std::setw(9) << std::left << Gst::get_fractional_seconds(len) << |
| 61 | std::endl << std::flush; |
| 62 | } |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | // This function is used to receive asynchronous messages in the main loop. |
| 68 | bool on_bus_message(const Glib::RefPtr<Gst::Bus>& /* bus */, const Glib::RefPtr<Gst::Message>& message) |
| 69 | { |
| 70 | switch(message->get_message_type()) { |
| 71 | case Gst::MESSAGE_EOS: |
| 72 | std::cout << std::endl << "End of stream" << std::endl; |
| 73 | mainloop->quit(); |
| 74 | return false; |
| 75 | case Gst::MESSAGE_ERROR: |
| 76 | { |
| 77 | Glib::RefPtr<Gst::MessageError> msgError = Glib::RefPtr<Gst::MessageError>::cast_static(message); |
| 78 | if(msgError) |
| 79 | { |
| 80 | Glib::Error err; |
| 81 | err = msgError->parse_error(); |
| 82 | std::cerr << "Error: " << err.what() << std::endl; |
| 83 | } |
| 84 | else |
| 85 | std::cerr << "Error." << std::endl; |
| 86 | |
| 87 | mainloop->quit(); |
| 88 | return false; |
| 89 | } |
| 90 | default: |
| 91 | { |
| 92 | //std::cout << "debug: on_bus_message: unhandled message=" << G_OBJECT_TYPE_NAME(message->gobj()) << std::endl; |
| 93 | } |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | void on_parser_pad_added(const Glib::RefPtr<Gst::Pad>& newPad) |
| 101 | { |
| 102 | // We can now link this pad with the audio decoder |
| 103 | std::cout << "Dynamic pad created. Linking parser/decoder." << std::endl; |
| 104 | Glib::RefPtr<Gst::Pad> sinkPad = decoder->get_static_pad("sink"); |
| 105 | Gst::PadLinkReturn ret = newPad->link(sinkPad); |
| 106 | |
| 107 | if (ret != Gst::PAD_LINK_OK && ret != Gst::PAD_LINK_WAS_LINKED) |
| 108 | { |
| 109 | std::cerr << "Linking of pads " << newPad->get_name() << " and " << |
| 110 | sinkPad->get_name() << " failed." << std::endl; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | Gst::PadProbeReturn on_sink_pad_have_data(const Glib::RefPtr<Gst::Pad>& pad, const Gst::PadProbeInfo&) |
| 115 | { |
| 116 | std::cout << "Sink pad '" << pad->get_name() << "' has received data;"; |
| 117 | std::cout << " will now remove sink data probe id: " << data_probe_id << std::endl; |
| 118 | pad->remove_probe(data_probe_id); |
| 119 | return Gst::PAD_PROBE_OK; |
| 120 | } |
| 121 | |
| 122 | } // anonymous namespace |
| 123 | |
| 124 | int main(int argc, char** argv) |
| 125 | { |
| 126 | // Initialize Gstreamermm: |
| 127 | Gst::init(argc, argv); |
| 128 | |
| 129 | // Check input arguments: |
| 130 | if(argc < 2) |
| 131 | { |
| 132 | std::cout << "Usage: " << argv[0] << " <Ogg/Vorbis filename>" << std::endl; |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | const std::string filename = argv[1]; |
| 137 | |
| 138 | mainloop = Glib::MainLoop::create(); |
| 139 | |
| 140 | // Create the pipeline: |
| 141 | pipeline = Gst::Pipeline::create("audio-player"); |
| 142 | |
| 143 | // Create the elements: |
| 144 | |
| 145 | // filsrc reads the file from disk: |
| 146 | Glib::RefPtr<Gst::Element> source = Gst::ElementFactory::create_element("filesrc"); |
| 147 | if(!source) |
| 148 | std::cerr << "filesrc element could not be created." << std::endl; |
| 149 | |
| 150 | // oggdemux parses the ogg streams into elementary streams (audio and video): |
| 151 | Glib::RefPtr<Gst::Element> parser = Gst::ElementFactory::create_element("oggdemux"); |
| 152 | if(!parser) |
| 153 | std::cerr << "oggdemux element could not be created." << std::endl; |
| 154 | |
| 155 | // vorbisdec decodes a vorbis (audio) stream: |
| 156 | decoder = Gst::ElementFactory::create_element("vorbisdec"); |
| 157 | if(!decoder) |
| 158 | std::cerr << "vorbisdec element could not be created." << std::endl; |
| 159 | |
| 160 | // audioconvert converts raw audio to a format which can be used by the next element |
| 161 | Glib::RefPtr<Gst::Element> conv = Gst::ElementFactory::create_element("audioconvert"); |
| 162 | if(!conv) |
| 163 | std::cerr << "audioconvert element could not be created." << std::endl; |
| 164 | |
| 165 | // Outputs sound to an ALSA audio device |
| 166 | Glib::RefPtr<Gst::Element> sink = Gst::ElementFactory::create_element("alsasink"); |
| 167 | if(!sink) |
| 168 | std::cerr << "alsasink element could not be created." << std::endl; |
| 169 | |
| 170 | //Check that the elements were created: |
| 171 | if(!pipeline || !source || !parser || !decoder || !conv || !sink) |
| 172 | { |
| 173 | std::cerr << "One element could not be created" << std::endl; |
| 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | Glib::RefPtr<Gst::Pad> pad = sink->get_static_pad("sink"); |
| 178 | if(pad) |
| 179 | data_probe_id = pad->add_probe(Gst::PAD_PROBE_TYPE_DATA_DOWNSTREAM, sigc::ptr_fun(&on_sink_pad_have_data)); |
| 180 | |
| 181 | std::cout << "sink data probe id = " << data_probe_id << std::endl; |
| 182 | |
| 183 | source->set_property("location", filename); |
| 184 | |
| 185 | // Get the bus from the pipeline, |
| 186 | // and add a bus watch to the default main context with the default priority: |
| 187 | Glib::RefPtr<Gst::Bus> bus = pipeline->get_bus(); |
| 188 | bus->add_watch( sigc::ptr_fun(&on_bus_message) ); |
| 189 | |
| 190 | |
| 191 | // Put all the elements in a pipeline: |
| 192 | try |
| 193 | { |
| 194 | pipeline->add(source)->add(parser)->add(decoder)->add(conv)->add(sink); |
| 195 | } |
| 196 | catch(const Glib::Error& ex) |
| 197 | { |
| 198 | std::cerr << "Error while adding elements to the pipeline: " << ex.what() << std::endl; |
| 199 | return 1; |
| 200 | } |
| 201 | |
| 202 | // Link the elements together: |
| 203 | try |
| 204 | { |
| 205 | source->link(parser); |
| 206 | |
| 207 | // We cannot link the parser and decoder yet, |
| 208 | // because the parser uses dynamic pads. |
| 209 | // So we do it later in a pad-added signal handler: |
| 210 | parser->signal_pad_added().connect( sigc::ptr_fun(&on_parser_pad_added) ); |
| 211 | |
| 212 | decoder->link(conv)->link(sink); |
| 213 | } |
| 214 | catch(const std::runtime_error& ex) |
| 215 | { |
| 216 | std::cout << "Exception while linking elements: " << ex.what() << std::endl; |
| 217 | } |
| 218 | |
| 219 | // Call on_timeout function at a 200ms |
| 220 | // interval to regularly print the position of the stream |
| 221 | Glib::signal_timeout().connect(sigc::ptr_fun(&on_timeout), 200); |
| 222 | |
| 223 | // Now set the whole pipeline to playing and start the main loop: |
| 224 | std::cout << "Setting to PLAYING." << std::endl; |
| 225 | pipeline->set_state(Gst::STATE_PLAYING); |
| 226 | std::cout << "Running." << std::endl; |
| 227 | mainloop->run(); |
| 228 | |
| 229 | // Clean up nicely: |
| 230 | std::cout << "Returned. Stopping playback." << std::endl; |
| 231 | pipeline->set_state(Gst::STATE_NULL); |
| 232 | |
| 233 | return 0; |
| 234 | } |