| 1 | /* |
| 2 | * main.cc |
| 3 | * |
| 4 | * Created on: Sep 12, 2014 |
| 5 | * Author: m.kolny |
| 6 | */ |
| 7 | |
| 8 | #include <gstreamermm.h> |
| 9 | #include <iostream> |
| 10 | #include <glibmm/main.h> |
| 11 | |
| 12 | using namespace Gst; |
| 13 | using Glib::RefPtr; |
| 14 | |
| 15 | |
| 16 | class AllMediaPlayer |
| 17 | { |
| 18 | private: |
| 19 | RefPtr<Glib::MainLoop> main_loop; |
| 20 | RefPtr<Pipeline> pipeline; |
| 21 | |
| 22 | #ifndef GSTREAMERMM_DISABLE_DEPRECATED |
| 23 | RefPtr<FileSrc> source; |
| 24 | #else |
| 25 | RefPtr<Gst::Element> source; |
| 26 | #endif |
| 27 | |
| 28 | RefPtr<Element> decoder; |
| 29 | |
| 30 | bool on_bus_message(const RefPtr<Bus>&, const RefPtr<Message>& message); |
| 31 | void on_decoder_pad_added(const RefPtr<Pad>& pad); |
| 32 | |
| 33 | void init() |
| 34 | { |
| 35 | #ifndef GSTREAMERMM_DISABLE_DEPRECATED |
| 36 | source = FileSrc::create(); |
| 37 | #else |
| 38 | source = ElementFactory::create_element("filesrc"); |
| 39 | #endif |
| 40 | |
| 41 | decoder = ElementFactory::create_element("decodebin"); |
| 42 | |
| 43 | if (!decoder || !source) |
| 44 | { |
| 45 | throw std::runtime_error("One element could not be created."); |
| 46 | } |
| 47 | |
| 48 | pipeline->add(source)->add(decoder); |
| 49 | decoder->signal_pad_added().connect(sigc::mem_fun(*this, &AllMediaPlayer::on_decoder_pad_added)); |
| 50 | |
| 51 | source->link(decoder); |
| 52 | } |
| 53 | |
| 54 | public: |
| 55 | AllMediaPlayer() |
| 56 | { |
| 57 | main_loop = Glib::MainLoop::create(); |
| 58 | pipeline = Pipeline::create(); |
| 59 | pipeline->get_bus()->add_watch(sigc::mem_fun(*this, &AllMediaPlayer::on_bus_message)); |
| 60 | } |
| 61 | |
| 62 | void play_until_eos(const std::string& filename) |
| 63 | { |
| 64 | init(); |
| 65 | |
| 66 | #ifndef GSTREAMERMM_DISABLE_DEPRECATED |
| 67 | source->property_location() = filename; |
| 68 | #else |
| 69 | source->set_property("location", filename); |
| 70 | #endif |
| 71 | |
| 72 | pipeline->set_state(STATE_PLAYING); |
| 73 | main_loop->run(); |
| 74 | pipeline->set_state(STATE_NULL); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | bool AllMediaPlayer::on_bus_message(const RefPtr<Bus>&, const RefPtr<Message>& message) |
| 79 | { |
| 80 | switch(message->get_message_type()) |
| 81 | { |
| 82 | case Gst::MESSAGE_EOS: |
| 83 | std::cout << std::endl << "End of stream" << std::endl; |
| 84 | main_loop->quit(); |
| 85 | return false; |
| 86 | case Gst::MESSAGE_ERROR: |
| 87 | std::cerr << "Error." << RefPtr<MessageError>::cast_static(message)->parse_debug() << std::endl; |
| 88 | main_loop->quit(); |
| 89 | return false; |
| 90 | default: |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | void AllMediaPlayer::on_decoder_pad_added(const RefPtr<Pad>& pad) |
| 98 | { |
| 99 | Glib::ustring caps_format = pad->get_current_caps()->to_string().substr(0, 5); |
| 100 | RefPtr<Bin> parent = parent.cast_dynamic(pad->get_parent()->get_parent()); |
| 101 | |
| 102 | if (!parent) |
| 103 | { |
| 104 | std::cerr << "cannot get parent bin" << std::endl; |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | Glib::ustring factory_name; |
| 109 | |
| 110 | if (caps_format == "video") |
| 111 | { |
| 112 | factory_name = "autovideosink"; |
| 113 | } |
| 114 | else if (caps_format == "audio") |
| 115 | { |
| 116 | factory_name = "autoaudiosink"; |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | std::cerr << "unsupported media type: " << pad->get_current_caps()->to_string() << std::endl; |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | RefPtr<Element> element = ElementFactory::create_element(factory_name); |
| 125 | |
| 126 | if (!element) |
| 127 | { |
| 128 | std::cerr << "cannot create element " << factory_name << std::endl; |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | try |
| 133 | { |
| 134 | parent->add(element); |
| 135 | element->set_state(STATE_PLAYING); |
| 136 | pad->link(element->get_static_pad("sink")); |
| 137 | } |
| 138 | catch (const std::runtime_error& err) |
| 139 | { |
| 140 | std::cerr << "cannot add element to a bin: " << err.what() << std::endl; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | int main(int argc, char** argv) |
| 145 | { |
| 146 | if (argc < 2) |
| 147 | { |
| 148 | std::cout << "Usage: " << argv[0] << " <multimedia filename>" << std::endl; |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | init(argc, argv); |
| 153 | AllMediaPlayer player; |
| 154 | |
| 155 | try |
| 156 | { |
| 157 | player.play_until_eos(argv[1]); |
| 158 | } |
| 159 | catch (const std::runtime_error& err) |
| 160 | { |
| 161 | std::cerr << "runtime error: " << err.what() << std::endl; |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | |
| 168 |