| 1 | /* gstreamermm - a C++ wrapper for gstreamer |
| 2 | * |
| 3 | * Copyright 2015 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 "player_window.h" |
| 20 | |
| 21 | #include <gstreamermm/xvimagesink.h> |
| 22 | |
| 23 | #if defined (GDK_WINDOWING_X11) |
| 24 | #include <gdk/gdkx.h> |
| 25 | #elif defined (GDK_WINDOWING_WIN32) |
| 26 | #include <gdk/gdkwin32.h> |
| 27 | #endif |
| 28 | |
| 29 | #include <iostream> |
| 30 | #include <iomanip> |
| 31 | |
| 32 | #ifdef GSTREAMERMM_DISABLE_DEPRECATED |
| 33 | |
| 34 | static const Glib::SignalProxyInfo PlayBin_signal_video_changed_info = |
| 35 | { |
| 36 | "video-changed", |
| 37 | (GCallback) &Glib::SignalProxyNormal::slot0_void_callback, |
| 38 | (GCallback) &Glib::SignalProxyNormal::slot0_void_callback |
| 39 | }; |
| 40 | |
| 41 | #endif |
| 42 | |
| 43 | |
| 44 | PlayerWindow::PlayerWindow(const Glib::RefPtr<PlayBinT>& playbin) |
| 45 | : m_vbox(false, 6), |
| 46 | m_progress_label("000:00:00.000000000 / 000:00:00.000000000"), |
| 47 | m_play_button("Play"), |
| 48 | m_pause_button("Pause"), |
| 49 | m_stop_button("Stop"), |
| 50 | m_rewind_button("Rewind"), |
| 51 | m_forward_button("Forward"), |
| 52 | m_open_button("Open") |
| 53 | { |
| 54 | set_title("gstreamermm Media Player Example"); |
| 55 | |
| 56 | add(m_vbox); |
| 57 | m_vbox.pack_start(m_video_area, Gtk::PACK_EXPAND_WIDGET); |
| 58 | m_vbox.pack_start(m_progress_label, Gtk::PACK_SHRINK); |
| 59 | m_vbox.pack_start(m_progress_scale, Gtk::PACK_SHRINK); |
| 60 | m_vbox.pack_start(m_button_box, Gtk::PACK_SHRINK); |
| 61 | |
| 62 | m_progress_label.set_alignment(Gtk::ALIGN_CENTER); |
| 63 | |
| 64 | m_progress_scale.set_range(0, 1); |
| 65 | m_progress_scale.set_draw_value(false); |
| 66 | m_progress_scale.signal_change_value().connect( |
| 67 | sigc::mem_fun(*this, &PlayerWindow::on_scale_value_changed) ); |
| 68 | |
| 69 | m_button_box.pack_start(m_play_button); |
| 70 | m_button_box.pack_start(m_pause_button); |
| 71 | m_button_box.pack_start(m_stop_button); |
| 72 | m_button_box.pack_start(m_rewind_button); |
| 73 | m_button_box.pack_start(m_forward_button); |
| 74 | m_button_box.pack_start(m_open_button); |
| 75 | |
| 76 | m_play_button.signal_clicked().connect(sigc::mem_fun(*this, |
| 77 | &PlayerWindow::on_button_play)); |
| 78 | m_pause_button.signal_clicked().connect(sigc::mem_fun(*this, |
| 79 | &PlayerWindow::on_button_pause)); |
| 80 | m_stop_button.signal_clicked().connect(sigc::mem_fun(*this, |
| 81 | &PlayerWindow::on_button_stop)); |
| 82 | m_rewind_button.signal_clicked().connect(sigc::mem_fun(*this, |
| 83 | &PlayerWindow::on_button_rewind)); |
| 84 | m_forward_button.signal_clicked().connect(sigc::mem_fun(*this, |
| 85 | &PlayerWindow::on_button_forward)); |
| 86 | m_open_button.signal_clicked().connect(sigc::mem_fun(*this, |
| 87 | &PlayerWindow::on_button_open)); |
| 88 | |
| 89 | m_video_area.signal_realize().connect(sigc::mem_fun(*this, |
| 90 | &PlayerWindow::on_video_area_realize)); |
| 91 | |
| 92 | // Get the bus from the pipeline: |
| 93 | Glib::RefPtr<Gst::Bus> bus = playbin->get_bus(); |
| 94 | |
| 95 | // Enable synchronous message emission to set up video (if any) at the |
| 96 | // exact appropriate time |
| 97 | bus->enable_sync_message_emission(); |
| 98 | |
| 99 | // Connect to bus's synchronous message signal (this is done so that |
| 100 | // m_video_area can be set up for drawing at the exact appropriate time): |
| 101 | bus->signal_sync_message().connect( |
| 102 | sigc::mem_fun(*this, &PlayerWindow::on_bus_message_sync)); |
| 103 | |
| 104 | // Add a bus watch to receive messages from the pipeline's bus: |
| 105 | m_watch_id = bus->add_watch( |
| 106 | sigc::mem_fun(*this, &PlayerWindow::on_bus_message) ); |
| 107 | |
| 108 | m_progress_scale.set_sensitive(false); |
| 109 | m_play_button.set_sensitive(false); |
| 110 | m_pause_button.set_sensitive(false); |
| 111 | m_stop_button.set_sensitive(false); |
| 112 | m_rewind_button.set_sensitive(false); |
| 113 | m_forward_button.set_sensitive(false); |
| 114 | |
| 115 | m_playbin = playbin; |
| 116 | |
| 117 | #ifndef GSTREAMERMM_DISABLE_DEPRECATED |
| 118 | m_playbin->property_video_sink() = Gst::XvImageSink::create(); |
| 119 | m_playbin->signal_video_changed().connect( |
| 120 | sigc::mem_fun(*this, &PlayerWindow::on_video_changed) ); |
| 121 | #else |
| 122 | m_playbin->set_property("video-sink", Gst::ElementFactory::create_element("xvimagesink")); |
| 123 | Glib::SignalProxy< void >(m_playbin.operator->(), &PlayBin_signal_video_changed_info).connect( |
| 124 | sigc::mem_fun(*this, &PlayerWindow::on_video_changed) ); |
| 125 | #endif |
| 126 | |
| 127 | show_all_children(); |
| 128 | m_pause_button.hide(); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | void PlayerWindow::on_video_area_realize() |
| 133 | { |
| 134 | // When the video area (the drawing area) is realized, Get its X Window |
| 135 | // ID and save it for when the Gst::VideoOverlay is ready to accept an ID in |
| 136 | // which to draw the video. |
| 137 | #ifdef GDK_WINDOWING_X11 |
| 138 | window_handler = GDK_WINDOW_XID (m_video_area.get_window()->gobj()); |
| 139 | #endif |
| 140 | #ifdef GDK_WINDOWING_WIN32 |
| 141 | window_handler = GDK_WINDOW_HWND ((m_video_area.get_window()->gobj()); |
| 142 | #endif |
| 143 | } |
| 144 | |
| 145 | static Glib::RefPtr<Gst::VideoOverlay> find_overlay(Glib::RefPtr<Gst::Element> element) |
| 146 | { |
| 147 | auto overlay = Glib::RefPtr<Gst::VideoOverlay>::cast_dynamic(element); |
| 148 | |
| 149 | if (overlay) |
| 150 | return overlay; |
| 151 | |
| 152 | auto bin = Glib::RefPtr<Gst::Bin>::cast_dynamic(element); |
| 153 | |
| 154 | if (!bin) |
| 155 | return overlay; |
| 156 | |
| 157 | for (auto e : bin->get_children()) |
| 158 | { |
| 159 | overlay = find_overlay(e); |
| 160 | if (overlay) |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | return overlay; |
| 165 | } |
| 166 | |
| 167 | // This function is used to receive asynchronous messages from mainPipeline's |
| 168 | // bus, specifically to prepare the Gst::VideoOverlay to draw inside the window |
| 169 | // in which we want it to draw to. |
| 170 | void PlayerWindow::on_bus_message_sync(const Glib::RefPtr<Gst::Message>& message) |
| 171 | { |
| 172 | if (!gst_is_video_overlay_prepare_window_handle_message (message->gobj())) |
| 173 | return; |
| 174 | |
| 175 | if (window_handler != 0) |
| 176 | { |
| 177 | Glib::RefPtr< Gst::VideoOverlay > videooverlay = |
| 178 | find_overlay(Glib::RefPtr<Gst::Element>::cast_dynamic(message->get_source())); |
| 179 | |
| 180 | if(videooverlay) |
| 181 | { |
| 182 | videooverlay->set_window_handle(window_handler); |
| 183 | } |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | std::cerr << "Should have obtained video_window_handle by now!"; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // This function is used to receive asynchronous messages from play_bin's bus |
| 192 | bool PlayerWindow::on_bus_message(const Glib::RefPtr<Gst::Bus>& /* bus */, |
| 193 | const Glib::RefPtr<Gst::Message>& message) |
| 194 | { |
| 195 | switch(message->get_message_type()) |
| 196 | { |
| 197 | case Gst::MESSAGE_EOS: |
| 198 | { |
| 199 | on_button_stop(); |
| 200 | break; |
| 201 | } |
| 202 | case Gst::MESSAGE_ERROR: |
| 203 | { |
| 204 | Glib::RefPtr<Gst::MessageError> msg_error = Glib::RefPtr<Gst::MessageError>::cast_static(message); |
| 205 | if(msg_error) |
| 206 | { |
| 207 | Glib::Error err = msg_error->parse_error(); |
| 208 | std::cerr << "Error: " << err.what() << std::endl; |
| 209 | } |
| 210 | else |
| 211 | std::cerr << "Error." << std::endl; |
| 212 | |
| 213 | on_button_stop(); |
| 214 | break; |
| 215 | } |
| 216 | default: |
| 217 | { |
| 218 | //std::cout << "debug: on_bus_message: unhandled message=" << G_OBJECT_TYPE_NAME(message->gobj()) << std::endl; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | void PlayerWindow::on_video_changed() |
| 226 | { |
| 227 | #ifndef GSTREAMERMM_DISABLE_DEPRECATED |
| 228 | Glib::RefPtr<Gst::Pad> pad = m_playbin->get_video_pad(0); |
| 229 | #else |
| 230 | GstPad* result; |
| 231 | g_signal_emit_by_name(m_playbin->gobj(), "get-video-pad", 0, &result, static_cast<void*>(0)); |
| 232 | Glib::RefPtr<Gst::Pad> pad = Glib::wrap(result); |
| 233 | #endif |
| 234 | |
| 235 | if(pad) |
| 236 | { |
| 237 | // Add a buffer probe to the video sink pad which will be removed after |
| 238 | // the first buffer is received in the on_video_pad_got_buffer method. |
| 239 | // When the first buffer arrives, the video size can be extracted. |
| 240 | m_pad_probe_id = pad->add_probe(Gst::PAD_PROBE_TYPE_BUFFER, |
| 241 | sigc::mem_fun(*this, &PlayerWindow::on_video_pad_got_buffer)); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | Gst::PadProbeReturn PlayerWindow::on_video_pad_got_buffer(const Glib::RefPtr<Gst::Pad>& pad, const Gst::PadProbeInfo& /* data */) |
| 246 | { |
| 247 | int width_value; |
| 248 | int height_value; |
| 249 | |
| 250 | Glib::RefPtr<Gst::Caps> caps = pad->get_current_caps(); |
| 251 | caps = caps->create_writable(); |
| 252 | |
| 253 | const Gst::Structure structure = caps->get_structure(0); |
| 254 | if(structure) |
| 255 | { |
| 256 | structure.get_field("width", width_value); |
| 257 | structure.get_field("height", height_value); |
| 258 | } |
| 259 | |
| 260 | m_video_area.set_size_request(width_value, height_value); |
| 261 | |
| 262 | // Resize to minimum when first playing by making size |
| 263 | // smallest then resizing according to video new size: |
| 264 | resize(1, 1); |
| 265 | check_resize(); |
| 266 | |
| 267 | pad->remove_probe(m_pad_probe_id); |
| 268 | m_pad_probe_id = 0; // Clear probe id to indicate that it has been removed |
| 269 | |
| 270 | return Gst::PAD_PROBE_OK; |
| 271 | } |
| 272 | |
| 273 | void PlayerWindow::on_button_play() |
| 274 | { |
| 275 | //Change the UI appropriately: |
| 276 | m_progress_scale.set_sensitive(); |
| 277 | m_play_button.set_sensitive(false); |
| 278 | m_pause_button.set_sensitive(); |
| 279 | m_stop_button.set_sensitive(); |
| 280 | m_rewind_button.set_sensitive(); |
| 281 | m_forward_button.set_sensitive(); |
| 282 | m_open_button.set_sensitive(false); |
| 283 | |
| 284 | m_play_button.hide(); |
| 285 | m_pause_button.show(); |
| 286 | |
| 287 | // Call on_timeout function at a 200ms |
| 288 | // interval to regularly update the position of the stream |
| 289 | m_timeout_connection = Glib::signal_timeout().connect( |
| 290 | sigc::mem_fun(*this, &PlayerWindow::on_timeout), 200); |
| 291 | |
| 292 | // set the pipeline to play mode: |
| 293 | m_playbin->set_state(Gst::STATE_PLAYING); |
| 294 | } |
| 295 | |
| 296 | void PlayerWindow::on_button_pause() |
| 297 | { |
| 298 | m_play_button.set_sensitive(); |
| 299 | m_pause_button.set_sensitive(false); |
| 300 | |
| 301 | m_pause_button.hide(); |
| 302 | m_play_button.show(); |
| 303 | |
| 304 | // Disconnect the progress callback: |
| 305 | m_timeout_connection.disconnect(); |
| 306 | |
| 307 | // Set the pipeline to pause mode: |
| 308 | m_playbin->set_state(Gst::STATE_PAUSED); |
| 309 | } |
| 310 | |
| 311 | void PlayerWindow::on_button_stop() |
| 312 | { |
| 313 | //Change the UI appropriately: |
| 314 | m_progress_scale.set_sensitive(false); |
| 315 | m_play_button.set_sensitive(); |
| 316 | m_pause_button.set_sensitive(false); |
| 317 | m_stop_button.set_sensitive(false); |
| 318 | m_rewind_button.set_sensitive(false); |
| 319 | m_forward_button.set_sensitive(false); |
| 320 | m_open_button.set_sensitive(); |
| 321 | m_pause_button.hide(); |
| 322 | m_play_button.show(); |
| 323 | |
| 324 | // Disconnect the progress signal handler: |
| 325 | m_timeout_connection.disconnect(); |
| 326 | |
| 327 | // Set the pipeline to inactive mode: |
| 328 | m_playbin->set_state(Gst::STATE_NULL); |
| 329 | |
| 330 | // Reset the display: |
| 331 | display_label_progress(0, m_duration); |
| 332 | m_progress_scale.set_value(0); |
| 333 | } |
| 334 | |
| 335 | bool PlayerWindow::on_scale_value_changed(Gtk::ScrollType /* type_not_used */, double value) |
| 336 | { |
| 337 | const gint64 new_pos = gint64(value * m_duration); |
| 338 | |
| 339 | if(m_playbin->seek(Gst::FORMAT_TIME, Gst::SEEK_FLAG_FLUSH, new_pos)) |
| 340 | { |
| 341 | display_label_progress(new_pos, m_duration); |
| 342 | return true; |
| 343 | } |
| 344 | else |
| 345 | { |
| 346 | std::cerr << "Could not seek." << std::endl; |
| 347 | return false; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | void PlayerWindow::on_button_rewind() |
| 352 | { |
| 353 | gint64 pos = 0; |
| 354 | Gst::Format fmt = Gst::FORMAT_TIME; |
| 355 | |
| 356 | if(m_playbin->query_position(fmt, pos)) |
| 357 | { |
| 358 | gint64 new_pos = (pos > rewind_skip_amount) ? (pos - rewind_skip_amount) : 0; |
| 359 | |
| 360 | if(m_playbin->seek(Gst::FORMAT_TIME, Gst::SEEK_FLAG_FLUSH, new_pos)) |
| 361 | { |
| 362 | m_progress_scale.set_value(double(new_pos) / m_duration); |
| 363 | display_label_progress(new_pos, m_duration); |
| 364 | } |
| 365 | else |
| 366 | std::cerr << "Could not seek." << std::endl; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void PlayerWindow::on_button_forward() |
| 371 | { |
| 372 | Gst::Format fmt = Gst::FORMAT_TIME; |
| 373 | Glib::RefPtr<Gst::QueryPosition> query = Gst::QueryPosition::create(fmt); |
| 374 | |
| 375 | if(m_playbin->query(query)) |
| 376 | { |
| 377 | gint64 pos = query->parse(); |
| 378 | gint64 new_pos = ((pos + forward_skip_amount) < m_duration) ? (pos + forward_skip_amount) : |
| 379 | m_duration; |
| 380 | |
| 381 | Glib::RefPtr<Gst::Event> event = Gst::EventSeek::create(1.0, fmt, |
| 382 | Gst::SEEK_FLAG_FLUSH, Gst::SEEK_TYPE_SET, new_pos, |
| 383 | Gst::SEEK_TYPE_NONE, -1); |
| 384 | |
| 385 | if(Glib::RefPtr<Gst::Element>::cast_static(m_playbin)->send_event(std::move(event))) |
| 386 | { |
| 387 | m_progress_scale.set_value(double(new_pos) / m_duration); |
| 388 | display_label_progress(new_pos, m_duration); |
| 389 | } |
| 390 | else |
| 391 | std::cerr << "Could not seek." << std::endl; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | void PlayerWindow::on_button_open() |
| 396 | { |
| 397 | static Glib::ustring working_dir = Glib::get_home_dir(); |
| 398 | |
| 399 | Gtk::FileChooserDialog chooser(*this, |
| 400 | "Select a media file", Gtk::FILE_CHOOSER_ACTION_OPEN); |
| 401 | chooser.add_button("Cancel", Gtk::RESPONSE_CANCEL); |
| 402 | chooser.add_button("OK", Gtk::RESPONSE_OK); |
| 403 | |
| 404 | chooser.set_current_folder(working_dir); |
| 405 | |
| 406 | const int response = chooser.run(); |
| 407 | if(response == Gtk::RESPONSE_OK) |
| 408 | { |
| 409 | working_dir = chooser.get_current_folder(); |
| 410 | |
| 411 | // Set uri property on the playbin. |
| 412 | m_playbin->set_property("uri", chooser.get_uri()); |
| 413 | |
| 414 | // Resize m_video_area and window to minimum when opening a file |
| 415 | m_video_area.set_size_request(0, 0); |
| 416 | resize(1, 1); |
| 417 | |
| 418 | set_title( Glib::filename_display_basename(chooser.get_filename()) ); |
| 419 | |
| 420 | m_play_button.set_sensitive(); |
| 421 | display_label_progress(0, 0); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | bool PlayerWindow::on_timeout() |
| 426 | { |
| 427 | Gst::Format fmt = Gst::FORMAT_TIME; |
| 428 | gint64 pos = 0; |
| 429 | |
| 430 | if(m_playbin->query_position(fmt, pos) |
| 431 | && m_playbin->query_duration(fmt, m_duration)) |
| 432 | { |
| 433 | m_progress_scale.set_value(double(pos) / m_duration); |
| 434 | display_label_progress(pos, m_duration); |
| 435 | } |
| 436 | |
| 437 | return true; |
| 438 | } |
| 439 | |
| 440 | void PlayerWindow::display_label_progress(gint64 pos, gint64 len) |
| 441 | { |
| 442 | std::ostringstream locationStream (std::ostringstream::out); |
| 443 | std::ostringstream durationStream (std::ostringstream::out); |
| 444 | |
| 445 | locationStream << std::right << std::setfill('0') << |
| 446 | std::setw(3) << Gst::get_hours(pos) << ":" << |
| 447 | std::setw(2) << Gst::get_minutes(pos) << ":" << |
| 448 | std::setw(2) << Gst::get_seconds(pos) << "." << |
| 449 | std::setw(9) << std::left << Gst::get_fractional_seconds(pos); |
| 450 | |
| 451 | durationStream << std::right << std::setfill('0') << |
| 452 | std::setw(3) << Gst::get_hours(len) << ":" << |
| 453 | std::setw(2) << Gst::get_minutes(len) << ":" << |
| 454 | std::setw(2) << Gst::get_seconds(len) << "." << |
| 455 | std::setw(9) << std::left << Gst::get_fractional_seconds(len); |
| 456 | |
| 457 | m_progress_label.set_text(locationStream.str() + " / " + durationStream.str()); |
| 458 | } |
| 459 | |
| 460 | PlayerWindow::~PlayerWindow() |
| 461 | { |
| 462 | m_playbin->get_bus()->remove_watch(m_watch_id); |
| 463 | m_playbin->set_state(Gst::STATE_NULL); |
| 464 | } |