Music Hub  ..
A session-wide music playback service
playbin.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_PLAYBIN_H_
20 #define GSTREAMER_PLAYBIN_H_
21 
22 #include <core/media/player.h>
23 
24 #include "bus.h"
25 #include "../mpris/player.h"
26 
27 #include <gio/gio.h>
28 #include <gst/gst.h>
29 
30 #include <chrono>
31 #include <string>
32 
33 // Uncomment to generate a dot file at the time that the pipeline
34 // goes to the PLAYING state. Make sure to export GST_DEBUG_DUMP_DOT_DIR
35 // before starting media-hub-server. To convert the dot file to something
36 // other image format, use: dot pipeline.dot -Tpng -o pipeline.png
37 //#define DEBUG_GST_PIPELINE
38 
39 namespace gstreamer
40 {
41 struct Playbin
42 {
43  enum PlayFlags
44  {
45  GST_PLAY_FLAG_VIDEO = (1 << 0),
46  GST_PLAY_FLAG_AUDIO = (1 << 1),
47  GST_PLAY_FLAG_TEXT = (1 << 2)
48  };
49 
51  {
55  };
56 
58 
59  static const std::string& pipeline_name();
60 
61  static void about_to_finish(GstElement*, gpointer user_data);
62 
63  static void source_setup(GstElement*,
64  GstElement *source,
65  gpointer user_data);
66 
67  Playbin();
68  ~Playbin();
69 
70  void reset();
71  void reset_pipeline();
72 
73  void on_new_message(const Bus::Message& message);
74  void on_new_message_async(const Bus::Message& message);
75  void process_message_element(GstMessage *message);
76 
78 
80 
81  void create_video_sink(uint32_t texture_id);
82 
83  void set_volume(double new_volume);
84 
87 
90 
91  uint64_t position() const;
92  uint64_t duration() const;
93 
94  void set_uri(const std::string& uri, const core::ubuntu::media::Player::HeadersType& headers, bool do_pipeline_reset = true);
95  std::string uri() const;
96 
97  void setup_source(GstElement *source);
98 
99  // Sets the pipeline's state (stopped, playing, paused, etc). Optional parameter makes this call
100  // in the main_loop context.
101  bool set_state_and_wait(GstState new_state);
102  bool seek(const std::chrono::microseconds& ms);
103 
106 
107  std::string file_info_from_uri(const std::string& uri) const;
108  std::string encode_uri(const std::string& uri) const;
109  std::string decode_uri(const std::string& uri) const;
110  std::string get_file_content_type(const std::string& uri) const;
111 
112  bool is_audio_file(const std::string& uri) const;
113  bool is_video_file(const std::string& uri) const;
114 
116 
117  bool can_play_streams() const;
118 
119  GstElement* pipeline;
122  GstElement* video_sink;
123  GstElement* audio_sink;
126  mutable uint64_t previous_position;
132  struct
133  {
134  core::Signal<void> about_to_finish;
135  core::Signal<Bus::Message::Detail::ErrorWarningInfo> on_error;
136  core::Signal<Bus::Message::Detail::ErrorWarningInfo> on_warning;
137  core::Signal<Bus::Message::Detail::ErrorWarningInfo> on_info;
138  core::Signal<Bus::Message::Detail::Tag> on_tag_available;
139  core::Signal<std::pair<Bus::Message::Detail::StateChanged,std::string>> on_state_changed;
140  core::Signal<uint64_t> on_seeked_to;
141  core::Signal<void> on_end_of_stream;
142  core::Signal<core::ubuntu::media::Player::PlaybackStatus> on_playback_status_changed;
143  core::Signal<core::ubuntu::media::Player::Orientation> on_orientation_changed;
144  core::Signal<core::ubuntu::media::video::Dimensions> on_video_dimensions_changed;
145  core::Signal<void> client_disconnected;
146  } signals;
151 };
152 }
153 
154 #endif // GSTREAMER_PLAYBIN_H_
static std::string get_audio_role_str(core::ubuntu::media::Player::AudioStreamRole audio_role)
Definition: playbin.cpp:368
core::Signal< Bus::Message::Detail::Tag > on_tag_available
Definition: playbin.h:138
bool set_state_and_wait(GstState new_state)
Definition: playbin.cpp:529
static void source_setup(GstElement *, GstElement *source, gpointer user_data)
Definition: playbin.cpp:92
void setup_source(GstElement *source)
Definition: playbin.cpp:496
std::tuple< Height, Width > Dimensions
Height and Width of a video.
Definition: dimensions.h:139
gint audio_stream_id
Definition: playbin.h:149
void set_uri(const std::string &uri, const core::ubuntu::media::Player::HeadersType &headers, bool do_pipeline_reset=true)
Definition: playbin.cpp:456
core::ubuntu::media::video::Dimensions get_video_dimensions() const
Definition: playbin.cpp:599
void process_message_element(GstMessage *message)
Definition: playbin.cpp:222
Definition: bus.h:33
bool seek(const std::chrono::microseconds &ms)
Definition: playbin.cpp:589
uint64_t duration() const
Definition: playbin.cpp:447
void reset_pipeline()
Definition: playbin.cpp:199
core::Connection on_new_message_connection_async
Definition: playbin.h:124
static const std::string & pipeline_name()
Definition: playbin.cpp:80
core::Signal< Bus::Message::Detail::ErrorWarningInfo > on_warning
Definition: playbin.h:136
core::Signal< core::ubuntu::media::Player::PlaybackStatus > on_playback_status_changed
Definition: playbin.h:142
void emit_video_dimensions_changed_if_changed(const core::ubuntu::media::video::Dimensions &new_dimensions)
Definition: playbin.cpp:620
bool can_play_streams() const
Definition: playbin.cpp:787
void set_lifetime(core::ubuntu::media::Player::Lifetime)
Definition: playbin.cpp:422
struct gstreamer::Playbin::@12 signals
GstElement * video_sink
Definition: playbin.h:122
void set_audio_stream_role(core::ubuntu::media::Player::AudioStreamRole new_audio_role)
Definition: playbin.cpp:405
bool is_missing_audio_codec
Definition: playbin.h:147
gint video_stream_id
Definition: playbin.h:150
std::map< std::string, std::string > HeadersType
Definition: player.h:49
core::Signal< uint64_t > on_seeked_to
Definition: playbin.h:140
void on_new_message(const Bus::Message &message)
core::Signal< core::ubuntu::media::Player::Orientation > on_orientation_changed
Definition: playbin.h:143
core::ubuntu::media::Player::Orientation orientation_lut(const gchar *orientation)
Definition: playbin.cpp:390
core::Signal< std::pair< Bus::Message::Detail::StateChanged, std::string > > on_state_changed
Definition: playbin.h:139
gstreamer::Bus & message_bus()
Definition: playbin.cpp:307
core::Signal< void > about_to_finish
Definition: playbin.h:134
std::string uri() const
Definition: playbin.cpp:518
void setup_pipeline_for_audio_video()
Definition: playbin.cpp:312
GstElement * pipeline
Definition: playbin.h:119
core::Signal< Bus::Message::Detail::ErrorWarningInfo > on_info
Definition: playbin.h:137
core::Signal< void > on_end_of_stream
Definition: playbin.h:141
GstElement * audio_sink
Definition: playbin.h:123
std::string get_file_content_type(const std::string &uri) const
Definition: playbin.cpp:735
std::string encode_uri(const std::string &uri) const
Definition: playbin.cpp:648
gulong source_setup_handler_id
Definition: playbin.h:131
core::ubuntu::media::video::Dimensions cached_video_dimensions
Definition: playbin.h:127
core::Signal< core::ubuntu::media::video::Dimensions > on_video_dimensions_changed
Definition: playbin.h:144
bool is_audio_file(const std::string &uri) const
Definition: playbin.cpp:754
bool is_video_file(const std::string &uri) const
Definition: playbin.cpp:768
void set_volume(double new_volume)
Definition: playbin.cpp:362
void on_new_message_async(const Bus::Message &message)
Definition: playbin.cpp:256
core::Signal< Bus::Message::Detail::ErrorWarningInfo > on_error
Definition: playbin.h:135
void create_video_sink(uint32_t texture_id)
Definition: playbin.cpp:352
gulong about_to_finish_handler_id
Definition: playbin.h:130
gstreamer::Bus bus
Definition: playbin.h:120
bool is_missing_video_codec
Definition: playbin.h:148
MediaFileType file_type
Definition: playbin.h:121
core::Signal< void > client_disconnected
Definition: playbin.h:145
core::ubuntu::media::Player::HeadersType request_headers
Definition: playbin.h:128
uint64_t position() const
Definition: playbin.cpp:427
std::string decode_uri(const std::string &uri) const
Definition: playbin.cpp:721
MediaFileType media_file_type() const
Definition: playbin.cpp:782
uint64_t previous_position
Definition: playbin.h:126
core::ubuntu::media::Player::Lifetime player_lifetime
Definition: playbin.h:129
std::string file_info_from_uri(const std::string &uri) const
Definition: playbin.cpp:628