| 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 <gtkmm/main.h> |
| 20 | #include <gstreamermm/init.h> |
| 21 | #include <gstreamermm/element.h> |
| 22 | #include <gstreamermm/elementfactory.h> |
| 23 | #include <gstreamermm/pad.h> |
| 24 | #include <gstreamermm/pipeline.h> |
| 25 | |
| 26 | // Plug-ins |
| 27 | #include <gstreamermm/alsasink.h> |
| 28 | #include <gstreamermm/audioconvert.h> |
| 29 | #include <gstreamermm/filesrc.h> |
| 30 | #include <gstreamermm/oggdemux.h> |
| 31 | #include <gstreamermm/vorbisdec.h> |
| 32 | |
| 33 | #include <iostream> |
| 34 | #include "player_window.h" |
| 35 | |
| 36 | Glib::RefPtr<Gst::Pipeline> pipeline; |
| 37 | |
| 38 | #ifndef GSTREAMERMM_DISABLE_DEPRECATED |
| 39 | Glib::RefPtr<Gst::VorbisDec> decoder; |
| 40 | #else |
| 41 | Glib::RefPtr<Gst::Element> decoder; |
| 42 | #endif |
| 43 | |
| 44 | void on_parser_pad_added(const Glib::RefPtr<Gst::Pad>& newPad) |
| 45 | { |
| 46 | // We can now link this pad with the audio decoder. |
| 47 | Glib::RefPtr<Gst::Pad> sinkPad = decoder->get_static_pad("sink"); |
| 48 | Gst::PadLinkReturn ret = newPad->link(sinkPad); |
| 49 | |
| 50 | if (ret != Gst::PAD_LINK_OK && ret != Gst::PAD_LINK_WAS_LINKED) |
| 51 | { |
| 52 | std::cerr << "Linking of pads " << newPad->get_name() << " and " << |
| 53 | sinkPad->get_name() << " failed." << std::endl; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | int main(int argc, char** argv) |
| 58 | { |
| 59 | Gtk::Main kit(argc, argv); |
| 60 | Gst::init(argc, argv); |
| 61 | |
| 62 | // Create the pipeline |
| 63 | pipeline = Gst::Pipeline::create("audio-player"); |
| 64 | |
| 65 | // Create the elements |
| 66 | // Reads file from disk |
| 67 | #ifndef GSTREAMERMM_DISABLE_DEPRECATED |
| 68 | Glib::RefPtr<Gst::FileSrc> source = Gst::FileSrc::create(); |
| 69 | #else |
| 70 | Glib::RefPtr<Gst::Element> source = Gst::ElementFactory::create_element("filesrc"); |
| 71 | #endif |
| 72 | if(!source) |
| 73 | { |
| 74 | std::cerr << "filesrc element could not be created" << std::endl; |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | // Parses the ogg streams into elementary streams (note that an ogg file may |
| 79 | // contain a video stream too) |
| 80 | #ifndef GSTREAMERMM_DISABLE_DEPRECATED |
| 81 | Glib::RefPtr<Gst::OggDemux> parser = Gst::OggDemux::create(); |
| 82 | #else |
| 83 | Glib::RefPtr<Gst::Element> parser = Gst::ElementFactory::create_element("oggdemux"); |
| 84 | #endif |
| 85 | if(!parser) |
| 86 | { |
| 87 | std::cerr << "oggdemux element could not be created" << std::endl; |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | // Decodes a vorbis stream |
| 92 | #ifndef GSTREAMERMM_DISABLE_DEPRECATED |
| 93 | decoder = Gst::VorbisDec::create(); |
| 94 | #else |
| 95 | decoder = Gst::ElementFactory::create_element("vorbisdec"); |
| 96 | #endif |
| 97 | |
| 98 | if(!decoder) |
| 99 | { |
| 100 | std::cerr << "vorbisdec element could not be created" << std::endl; |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | // Converts audio to a format which can be used by the next element |
| 105 | #ifndef GSTREAMERMM_DISABLE_DEPRECATED |
| 106 | Glib::RefPtr<Gst::AudioConvert> conv = Gst::AudioConvert::create(); |
| 107 | #else |
| 108 | Glib::RefPtr<Gst::Element> conv = Gst::ElementFactory::create_element("audioconvert"); |
| 109 | #endif |
| 110 | |
| 111 | if(!conv) |
| 112 | { |
| 113 | std::cerr << "audioconvert element could not be created" << std::endl; |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | // Outputs sound to an ALSA audio device |
| 118 | #ifndef GSTREAMERMM_DISABLE_DEPRECATED |
| 119 | Glib::RefPtr<Gst::AlsaSink> sink = Gst::AlsaSink::create(); |
| 120 | #else |
| 121 | Glib::RefPtr<Gst::Element> sink = Gst::ElementFactory::create_element("alsasink"); |
| 122 | #endif |
| 123 | if(!sink) |
| 124 | { |
| 125 | std::cerr << "sink element could not be created" << std::endl; |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | // Put all elements in a pipeline: |
| 131 | try |
| 132 | { |
| 133 | pipeline->add(source)->add(parser)->add(decoder)->add(conv)->add(sink); |
| 134 | } |
| 135 | catch(const std::runtime_error& ex) |
| 136 | { |
| 137 | std::cerr << "Error while adding elements to the pipeline: " << ex.what() << std::endl; |
| 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | // Link the elements together: |
| 142 | try |
| 143 | { |
| 144 | source->link(parser); |
| 145 | |
| 146 | // We cannot link the parser and decoder yet, |
| 147 | // because the parser uses dynamic pads. |
| 148 | // So we do it later in a pad-added signal handler: |
| 149 | parser->signal_pad_added().connect( sigc::ptr_fun(&on_parser_pad_added) ); |
| 150 | |
| 151 | decoder->link(conv)->link(sink); |
| 152 | } |
| 153 | catch(const std::runtime_error& ex) |
| 154 | { |
| 155 | std::cout << "Exception while linking elements: " << ex.what() << std::endl; |
| 156 | } |
| 157 | |
| 158 | // Create our window and show it: |
| 159 | PlayerWindow mainWindow(source, pipeline); |
| 160 | kit.run(mainWindow); |
| 161 | |
| 162 | // Clean up nicely: |
| 163 | pipeline->set_state(Gst::STATE_NULL); |
| 164 | |
| 165 | return 0; |
| 166 | } |