Music Hub  ..
A session-wide music playback service
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
bus.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2013 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18 
19 #ifndef GSTREAMER_BUS_H_
20 #define GSTREAMER_BUS_H_
21 
22 #include <core/property.h>
23 
24 #include <gst/gst.h>
25 
26 #include <boost/flyweight.hpp>
27 
28 #include <exception>
29 #include <functional>
30 #include <memory>
31 #include <tuple>
32 
33 namespace gstreamer
34 {
35 class Bus
36 {
37 public:
38  struct Message
39  {
41  {
42  }
43 
44  Message(GstMessage* msg)
45  : message(msg),
46  type(GST_MESSAGE_TYPE(msg)),
47  source(GST_MESSAGE_SRC_NAME(msg)),
48  sequence_number(gst_message_get_seqnum(msg))
49  {
50  switch(type)
51  {
52  case GST_MESSAGE_UNKNOWN:
53  throw std::runtime_error("Cannot construct message for type unknown");
54  break;
55  case GST_MESSAGE_EOS:
56  break;
57  case GST_MESSAGE_ERROR:
58  {
59  gst_message_parse_error(
60  msg,
63  cleanup = [this]()
64  {
65  g_error_free(detail.error_warning_info.error);
67  };
68  break;
69  }
70  case GST_MESSAGE_WARNING:
71  gst_message_parse_warning(
72  msg,
75  cleanup = [this]()
76  {
77  g_error_free(detail.error_warning_info.error);
79  };
80  break;
81  break;
82  case GST_MESSAGE_INFO:
83  gst_message_parse_info(
84  msg,
87  cleanup = [this]()
88  {
89  g_error_free(detail.error_warning_info.error);
91  };
92  break;
93  break;
94  case GST_MESSAGE_TAG:
95  gst_message_parse_tag(
96  msg,
98  cleanup = [this]()
99  {
100  gst_tag_list_free(detail.tag.tag_list);
101  };
102  break;
103  case GST_MESSAGE_BUFFERING:
104  gst_message_parse_buffering(
105  msg,
107  break;
108  case GST_MESSAGE_STATE_CHANGED:
109  gst_message_parse_state_changed(
110  msg,
114  break;
115  case GST_MESSAGE_STATE_DIRTY:
116  break;
117  case GST_MESSAGE_STEP_DONE:
118  gst_message_parse_step_done(
119  msg,
127  );
128  break;
129  case GST_MESSAGE_CLOCK_PROVIDE:
130  gst_message_parse_clock_provide(
131  msg,
134  break;
135  case GST_MESSAGE_CLOCK_LOST:
136  gst_message_parse_clock_lost(
137  msg,
139  break;
140  case GST_MESSAGE_NEW_CLOCK:
141  gst_message_parse_new_clock(
142  msg,
144  break;
145  case GST_MESSAGE_STRUCTURE_CHANGE:
146  break;
147  case GST_MESSAGE_STREAM_STATUS:
148  break;
149  case GST_MESSAGE_APPLICATION:
150  break;
151  case GST_MESSAGE_ELEMENT:
152  break;
153  case GST_MESSAGE_SEGMENT_START:
154  gst_message_parse_segment_start(
155  msg,
158  break;
159  case GST_MESSAGE_SEGMENT_DONE:
160  gst_message_parse_segment_done(
161  msg,
164  break;
165  case GST_MESSAGE_DURATION_CHANGED:
166  break;
167  case GST_MESSAGE_LATENCY:
168  break;
169  case GST_MESSAGE_ASYNC_START:
170  break;
171  case GST_MESSAGE_ASYNC_DONE:
172  gst_message_parse_async_done(
173  msg,
175  break;
176  case GST_MESSAGE_REQUEST_STATE:
177  break;
178  case GST_MESSAGE_STEP_START:
179  gst_message_parse_step_start(
180  msg,
187  break;
188  case GST_MESSAGE_QOS:
189  gst_message_parse_qos(
190  msg,
191  &detail.qos.live,
195  &detail.qos.duration);
196  break;
197  case GST_MESSAGE_PROGRESS:
198  break;
199  case GST_MESSAGE_TOC:
200  break;
201  case GST_MESSAGE_RESET_TIME:
202  break;
203  case GST_MESSAGE_STREAM_START:
204  break;
205  case GST_MESSAGE_NEED_CONTEXT:
206  break;
207  case GST_MESSAGE_HAVE_CONTEXT:
208  break;
209  case GST_MESSAGE_ANY:
210  break;
211  }
212  }
213 
214  GstMessage* message;
215  GstMessageType type;
216  boost::flyweight<std::string> source;
217  uint32_t sequence_number;
218 
219  union Detail
220  {
222  {
223  GError* error;
224  gchar* debug;
226  struct Tag
227  {
228  GstTagList* tag_list;
229  } tag;
230  struct
231  {
232  gint percent;
233  } buffering;
234  struct
235  {
236  GstBufferingMode buffering_mode;
237  gint avg_in;
238  gint avg_out;
240  } buffering_stats;
242  {
243  GstState old_state;
244  GstState new_state;
245  GstState pending_state;
246  } state_changed;
247  struct
248  {
249  gboolean active;
250  GstFormat format;
251  guint64 amount;
252  gdouble rate;
253  gboolean flush;
254  gboolean intermediate;
255  } step_start;
256  struct
257  {
258  GstFormat format;
259  guint64 amount;
260  gdouble rate;
261  gboolean flush;
262  gboolean intermediate;
263  guint64 duration;
264  gboolean eos;
265  } step_done;
266  struct
267  {
268  GstClock* clock;
269  gboolean ready;
270  } clock_provide;
271  struct
272  {
273  GstClock* clock;
274  } clock_lost;
275  struct
276  {
277  GstClock* clock;
278  } clock_new;
279  struct
280  {
281  GstFormat format;
282  gint64 position;
283  } segment_start;
284  struct
285  {
286  GstFormat format;
287  gint64 position;
288  } segment_done;
289  struct
290  {
291  GstClockTime running_time;
292  } async_done;
293  struct
294  {
295  gboolean live;
296  guint64 running_time;
297  guint64 stream_time;
298  guint64 timestamp;
299  guint64 duration;
300  } qos;
301  } detail;
302  std::function<void()> cleanup;
303  };
304 
305  static GstBusSyncReply sync_handler(
306  GstBus* bus,
307  GstMessage* msg,
308  gpointer data)
309  {
310  (void) bus;
311 
312  auto thiz = static_cast<Bus*>(data);
313  Message message(msg);
314  thiz->on_new_message(message);
315 
316  return GST_BUS_DROP;
317  }
318 
319  Bus(GstBus* bus) : bus(bus)
320  {
321  if (!bus)
322  throw std::runtime_error("Cannot create Bus instance if underlying instance is NULL.");
323 
324  gst_bus_set_sync_handler(
325  bus,
327  this,
328  nullptr);
329  }
330 
332  {
333  gst_object_unref(bus);
334  }
335 
336  GstBus* bus;
337  core::Signal<Message> on_new_message;
338 };
339 }
340 
341 #endif // GSTREAMER_BUS_H_
core::Signal< Message > on_new_message
Definition: bus.h:337
GstMessageType type
Definition: bus.h:215
struct gstreamer::Bus::Message::Detail::@1 buffering_stats
struct gstreamer::Bus::Message::Detail::@4 clock_provide
GstBus * bus
Definition: bus.h:336
static GstBusSyncReply sync_handler(GstBus *bus, GstMessage *msg, gpointer data)
Definition: bus.h:305
struct gstreamer::Bus::Message::Detail::@8 segment_done
struct gstreamer::Bus::Message::Detail::@5 clock_lost
GstMessage * message
Definition: bus.h:214
union gstreamer::Bus::Message::Detail detail
Message(GstMessage *msg)
Definition: bus.h:44
std::function< void()> cleanup
Definition: bus.h:302
struct gstreamer::Bus::Message::Detail::@0 buffering
struct gstreamer::Bus::Message::Detail::Tag tag
boost::flyweight< std::string > source
Definition: bus.h:216
struct gstreamer::Bus::Message::Detail::@10 qos
struct gstreamer::Bus::Message::Detail::@3 step_done
struct gstreamer::Bus::Message::Detail::ErrorWarningInfo error_warning_info
Bus(GstBus *bus)
Definition: bus.h:319
uint32_t sequence_number
Definition: bus.h:217
struct gstreamer::Bus::Message::Detail::@6 clock_new
GstClockTime running_time
Definition: bus.h:291
struct gstreamer::Bus::Message::Detail::@2 step_start
struct gstreamer::Bus::Message::Detail::@7 segment_start
struct gstreamer::Bus::Message::Detail::@9 async_done
GstBufferingMode buffering_mode
Definition: bus.h:236
struct gstreamer::Bus::Message::Detail::StateChanged state_changed