Debian Codegraph @

examples/ogg_player_gtkmm/player_window.cc

raw symbols SCIP
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/stock.h>
20 #include <gtkmm/filechooserdialog.h>
21 #include <gstreamermm/bus.h>
22 #include <gstreamermm/clock.h>
23 #include <gstreamermm/event.h>
24 #include <gstreamermm/message.h>
25 #include <gstreamermm/query.h>
26 #include <glibmm/main.h>
27 #include <glibmm/miscutils.h>
28 #include <iostream>
29 #include <sstream>
30 #include <iomanip>
31 #include "player_window.h"
32
33 PlayerWindow::PlayerWindow(const Glib::RefPtr<FileSrcT>& source_element,
34 const Glib::RefPtr<Gst::Pipeline>& main_pipeline)
35 : m_vbox(false, 5),
36 m_progress_label("000:00:00.000000000 / 000:00:00.000000000"),
37 m_play_button(Gtk::Stock::MEDIA_PLAY),
38 m_pause_button(Gtk::Stock::MEDIA_PAUSE),
39 m_stop_button(Gtk::Stock::MEDIA_STOP),
40 m_rewind_button(Gtk::Stock::MEDIA_REWIND),
41 m_forward_button(Gtk::Stock::MEDIA_FORWARD),
42 m_open_button(Gtk::Stock::OPEN)
43 {
44 set_title("gstreamermm Ogg Player Example");
45
46 add(m_vbox);
47 m_vbox.pack_start(m_progress_label);
48 m_vbox.pack_start(m_progress_scale);
49 m_vbox.pack_start(m_button_box);
50
51 m_progress_label.set_alignment(Gtk::ALIGN_CENTER);
52
53 m_progress_scale.set_range(0, 1);
54 m_progress_scale.set_draw_value(false);
55 m_progress_scale.signal_change_value().connect(
56 sigc::mem_fun(*this, &PlayerWindow::on_scale_value_changed));
57
58 m_button_box.pack_start(m_play_button);
59 m_button_box.pack_start(m_pause_button);
60 m_button_box.pack_start(m_stop_button);
61 m_button_box.pack_start(m_rewind_button);
62 m_button_box.pack_start(m_forward_button);
63 m_button_box.pack_start(m_open_button);
64
65 m_play_button.signal_clicked().connect(
66 sigc::mem_fun(*this, &PlayerWindow::on_button_play ));
67 m_pause_button.signal_clicked().connect(
68 sigc::mem_fun(*this, &PlayerWindow::on_button_pause) );
69 m_stop_button.signal_clicked().connect(
70 sigc::mem_fun(*this, &PlayerWindow::on_button_stop) );
71 m_rewind_button.signal_clicked().connect(
72 sigc::mem_fun(*this, &PlayerWindow::on_button_rewind) );
73 m_forward_button.signal_clicked().connect(
74 sigc::mem_fun(*this, &PlayerWindow::on_button_forward) );
75 m_open_button.signal_clicked().connect(
76 sigc::mem_fun(*this, &PlayerWindow::on_button_open) );
77
78 // Add a bus watch to receive messages from pipeline's bus
79 Glib::RefPtr<Gst::Bus> bus = main_pipeline->get_bus();
80 if(bus)
81 {
82 m_watch_id = bus->add_watch(
83 sigc::mem_fun( *this, &PlayerWindow::on_bus_message) );
84 }
85
86 m_progress_scale.set_sensitive(false);
87 m_play_button.set_sensitive(false);
88 m_pause_button.set_sensitive(false);
89 m_stop_button.set_sensitive(false);
90 m_rewind_button.set_sensitive(false);
91 m_forward_button.set_sensitive(false);
92
93 m_source_element = source_element;
94 m_main_pipeline = main_pipeline;
95
96 show_all_children();
97 m_pause_button.hide();
98 }
99
100 // This function is used to receive asynchronous messages from main_pipeline's bus
101 bool PlayerWindow::on_bus_message(const Glib::RefPtr<Gst::Bus>& /* bus */, const Glib::RefPtr<Gst::Message>& message)
102 {
103 switch(message->get_message_type())
104 {
105 case Gst::MESSAGE_EOS:
106 //We reached the end of the file, so stop playing:
107 on_button_stop();
108 break;
109 case Gst::MESSAGE_ERROR:
110 {
111 //There was an error, so stop playing,
112 //and output some information to stdout:
113 Glib::RefPtr<Gst::MessageError> error_message =
114 Glib::RefPtr<Gst::MessageError>::cast_static(message);
115 if(error_message)
116 {
117 Glib::Error err;
118 err = error_message->parse_error();
119 std::cerr << "Error: " << err.what() << std::endl;
120 }
121 else
122 std::cerr << "Error." << std::endl;
123
124 on_button_stop();
125 break;
126 }
127 default:
128 {
129 //std::cout << "debug: on_bus_message: unhandled message=" << G_OBJECT_TYPE_NAME(message->gobj()) << std::endl;
130 }
131 break;
132 }
133
134 return true;
135 }
136
137 void PlayerWindow::on_button_play()
138 {
139 m_progress_scale.set_sensitive();
140 m_play_button.set_sensitive(false);
141 m_pause_button.set_sensitive();
142 m_stop_button.set_sensitive();
143 m_rewind_button.set_sensitive();
144 m_forward_button.set_sensitive();
145 m_open_button.set_sensitive(false);
146
147 m_play_button.hide();
148 m_pause_button.show();
149
150 // Call on_timeout function at a 200ms
151 // interval to regularly update the position of the stream:
152 m_progress_connection = Glib::signal_timeout().connect(
153 sigc::mem_fun(*this, &PlayerWindow::on_timeout), 200);
154
155 // Set the pipeline to play mode:
156 m_main_pipeline->set_state(Gst::STATE_PLAYING);
157 }
158
159 void PlayerWindow::on_button_pause()
160 {
161 m_play_button.set_sensitive();
162 m_pause_button.set_sensitive(false);
163
164 m_pause_button.hide();
165 m_play_button.show();
166
167 // Disconnect progress callback:
168 m_progress_connection.disconnect();
169
170 // Set the pipeline to pause mode:
171 m_main_pipeline->set_state(Gst::STATE_PAUSED);
172 }
173
174 void PlayerWindow::on_button_stop()
175 {
176 m_progress_scale.set_sensitive(false);
177 m_play_button.set_sensitive();
178 m_pause_button.set_sensitive(false);
179 m_stop_button.set_sensitive(false);
180 m_rewind_button.set_sensitive(false);
181 m_forward_button.set_sensitive(false);
182 m_open_button.set_sensitive();
183
184 m_pause_button.hide();
185 m_play_button.show();
186
187 // Disconnect progress callback:
188 m_progress_connection.disconnect();
189
190 // Set the pipeline to inactive mode:
191 m_main_pipeline->set_state(Gst::STATE_NULL);
192 display_label_progress(0, m_duration);
193 m_progress_scale.set_value(0);
194 }
195
196 bool PlayerWindow::on_scale_value_changed(Gtk::ScrollType /* type_not_used */, double value)
197 {
198 const gint64 new_pos = gint64(value * m_duration);
199
200 if(m_main_pipeline->seek(Gst::FORMAT_TIME, Gst::SEEK_FLAG_FLUSH, new_pos))
201 {
202 display_label_progress(new_pos, m_duration);
203 return true;
204 }
205 else
206 {
207 std::cerr << "Could not seek." << std::endl;
208 return false;
209 }
210 }
211
212 void PlayerWindow::on_button_rewind()
213 {
214 static const gint64 skip_amount = Gst::SECOND * 2;
215
216 gint64 pos = 0;
217 Gst::Format fmt = Gst::FORMAT_TIME;
218
219 if(m_main_pipeline->query_position(fmt, pos))
220 {
221 const gint64 new_pos = (pos > skip_amount) ? (pos - skip_amount) : 0;
222
223 if(m_main_pipeline->seek(Gst::FORMAT_TIME, Gst::SEEK_FLAG_FLUSH, new_pos))
224 {
225 display_label_progress(new_pos, m_duration);
226 m_progress_scale.set_value(double(new_pos) / m_duration);
227 }
228 else
229 std::cerr << "Could not seek." << std::endl;
230 }
231 }
232
233 void PlayerWindow::on_button_forward()
234 {
235 static const gint64 skip_amount = Gst::SECOND * 3;
236
237 Gst::Format fmt = Gst::FORMAT_TIME;
238
239 Glib::RefPtr<Gst::Query> query = Gst::QueryPosition::create(fmt);
240
241 if(m_main_pipeline->query(query))
242 {
243 Glib::RefPtr<Gst::QueryPosition> posQuery =
244 Glib::RefPtr<Gst::QueryPosition>::cast_static(query);
245
246 gint64 pos = posQuery->parse();
247
248 const gint64 new_pos =
249 ((pos + skip_amount) < m_duration) ? (pos + skip_amount) : m_duration;
250
251 Glib::RefPtr<Gst::Event> event = Gst::EventSeek::create(1.0, fmt,
252 Gst::SEEK_FLAG_FLUSH, Gst::SEEK_TYPE_SET, new_pos,
253 Gst::SEEK_TYPE_NONE, -1);
254
255 Glib::RefPtr<Gst::EventSeek> seekEvent =
256 Glib::RefPtr<Gst::EventSeek>::cast_static(event);
257
258 if(m_main_pipeline->send_event(seekEvent))
259 {
260 m_progress_scale.set_value(double(new_pos) / m_duration);
261 display_label_progress(new_pos, m_duration);
262 }
263 else
264 std::cerr << "Could not seek." << std::endl;
265 }
266 }
267
268 void PlayerWindow::on_button_open()
269 {
270 static Glib::ustring working_dir = Glib::get_home_dir();
271
272 //Offer a file chooser dialog to the user:
273 Gtk::FileChooserDialog chooser(*this,
274 "Select ogg file", Gtk::FILE_CHOOSER_ACTION_OPEN);
275 chooser.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
276 chooser.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
277
278 Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
279
280 filter->add_mime_type("application/ogg");
281 filter->set_name("Ogg files");
282 chooser.set_filter(filter);
283
284 chooser.set_current_folder(working_dir);
285
286 const int response = chooser.run();
287 if(response == Gtk::RESPONSE_OK)
288 {
289 working_dir = chooser.get_current_folder();
290
291 // Set uri on the file source.
292 const std::string uri = chooser.get_uri();
293 m_source_element->set_property("uri", uri);
294 set_title(uri);
295
296 m_play_button.set_sensitive();
297 display_label_progress(0, 0);
298 }
299 }
300
301 bool PlayerWindow::on_timeout()
302 {
303 Gst::Format fmt = Gst::FORMAT_TIME;
304 gint64 pos = 0;
305
306 if(m_main_pipeline->query_position(fmt, pos)
307 && m_main_pipeline->query_duration(fmt, m_duration))
308 {
309 m_progress_scale.set_value(double(pos) / m_duration);
310 display_label_progress(pos, m_duration);
311 }
312
313 return true;
314 }
315
316 void PlayerWindow::display_label_progress(gint64 pos, gint64 len)
317 {
318 std::ostringstream locationStream(std::ostringstream::out);
319 locationStream << std::right << std::setfill('0') <<
320 std::setw(3) << Gst::get_hours(pos) << ":" <<
321 std::setw(2) << Gst::get_minutes(pos) << ":" <<
322 std::setw(2) << Gst::get_seconds(pos) << "." <<
323 std::setw(9) << std::left << Gst::get_fractional_seconds(pos);
324
325 std::ostringstream durationStream(std::ostringstream::out);
326 durationStream << std::right << std::setfill('0') <<
327 std::setw(3) << Gst::get_hours(len) << ":" <<
328 std::setw(2) << Gst::get_minutes(len) << ":" <<
329 std::setw(2) << Gst::get_seconds(len) << "." <<
330 std::setw(9) << std::left << Gst::get_fractional_seconds(len);
331
332 m_progress_label.set_text(
333 locationStream.str() + " / " + durationStream.str() );
334 }
335
336 PlayerWindow::~PlayerWindow()
337 {
338 m_main_pipeline->get_bus()->remove_watch(m_watch_id);
339 }