| 1 | /* gstreamermm - a C++ wrapper for gstreamer |
| 2 | * |
| 3 | * Copyright 2011 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/convert.h> |
| 22 | #include <iostream> |
| 23 | #include <stdlib.h> |
| 24 | #include <gstreamermm/playbin.h> |
| 25 | |
| 26 | namespace |
| 27 | { |
| 28 | |
| 29 | Glib::RefPtr<Glib::MainLoop> mainloop; |
| 30 | |
| 31 | // This function is used to receive asynchronous messages in the main loop. |
| 32 | bool on_bus_message(const Glib::RefPtr<Gst::Bus>& /* bus */, |
| 33 | const Glib::RefPtr<Gst::Message>& message) |
| 34 | { |
| 35 | switch(message->get_message_type()) { |
| 36 | case Gst::MESSAGE_EOS: |
| 37 | std::cout << std::endl << "End of stream" << std::endl; |
| 38 | mainloop->quit(); |
| 39 | return false; |
| 40 | case Gst::MESSAGE_ERROR: |
| 41 | { |
| 42 | Glib::RefPtr<Gst::MessageError> msgError = |
| 43 | Glib::RefPtr<Gst::MessageError>::cast_static(message); |
| 44 | |
| 45 | if(msgError) |
| 46 | { |
| 47 | Glib::Error err; |
| 48 | err = msgError->parse_error(); |
| 49 | std::cerr << "Error: " << err.what() << std::endl; |
| 50 | } |
| 51 | else |
| 52 | std::cerr << "Error." << std::endl; |
| 53 | |
| 54 | mainloop->quit(); |
| 55 | return false; |
| 56 | } |
| 57 | default: |
| 58 | break; |
| 59 | } |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | } // anonymous namespace |
| 65 | |
| 66 | int main(int argc, char** argv) |
| 67 | { |
| 68 | // Initialize gstreamermm: |
| 69 | Gst::init(argc, argv); |
| 70 | |
| 71 | // Check input arguments: |
| 72 | if(argc < 2) |
| 73 | { |
| 74 | std::cout << "Usage: " << argv[0] << " <media file or uri>" << std::endl; |
| 75 | return EXIT_FAILURE; |
| 76 | } |
| 77 | |
| 78 | // Create a playbin element. |
| 79 | #ifndef GSTREAMERMM_DISABLE_DEPRECATED |
| 80 | Glib::RefPtr<Gst::PlayBin> playbin = Gst::PlayBin::create(); |
| 81 | #else |
| 82 | Glib::RefPtr<Gst::Element> playbin = Gst::ElementFactory::create_element("playbin"); |
| 83 | #endif |
| 84 | |
| 85 | if(!playbin) |
| 86 | { |
| 87 | std::cerr << "The playbin2 element could not be created." << std::endl; |
| 88 | return EXIT_FAILURE; |
| 89 | } |
| 90 | |
| 91 | // Take the commandline argument and ensure that it is a uri: |
| 92 | Glib::ustring uri; |
| 93 | |
| 94 | if(gst_uri_is_valid(argv[1])) |
| 95 | uri = argv[1]; |
| 96 | else |
| 97 | uri = Glib::filename_to_uri(argv[1]); |
| 98 | |
| 99 | // Set the playbyin2's uri property. |
| 100 | playbin->set_property("uri", uri); |
| 101 | |
| 102 | // Create the main loop. |
| 103 | mainloop = Glib::MainLoop::create(); |
| 104 | |
| 105 | // Get the bus from the playbin, and add a bus watch to the default main |
| 106 | // context with the default priority: |
| 107 | Glib::RefPtr<Gst::Bus> bus = playbin->get_bus(); |
| 108 | bus->add_watch(sigc::ptr_fun(&on_bus_message)); |
| 109 | |
| 110 | // Now set the playbin to the PLAYING state and start the main loop: |
| 111 | std::cout << "Setting to PLAYING." << std::endl; |
| 112 | playbin->set_state(Gst::STATE_PLAYING); |
| 113 | std::cout << "Running." << std::endl; |
| 114 | mainloop->run(); |
| 115 | |
| 116 | // Clean up nicely: |
| 117 | std::cout << "Returned. Setting state to NULL." << std::endl; |
| 118 | playbin->set_state(Gst::STATE_NULL); |
| 119 | |
| 120 | return EXIT_SUCCESS; |
| 121 | } |