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 #include "core/media/player.h"
34 
35 // Uncomment to generate a dot file at the time that the pipeline
36 // goes to the PLAYING state. Make sure to export GST_DEBUG_DUMP_DOT_DIR
37 // before starting media-hub-server. To convert the dot file to some
38 // other image format, use: dot pipeline.dot -Tpng -o pipeline.png
39 //#define DEBUG_GST_PIPELINE
40 
41 namespace gstreamer
42 {
43 struct Playbin
44 {
45  enum PlayFlags
46  {
47  GST_PLAY_FLAG_VIDEO = (1 << 0),
48  GST_PLAY_FLAG_AUDIO = (1 << 1),
49  GST_PLAY_FLAG_TEXT = (1 << 2)
50  };
51 
53  {
57  };
58 
60 
61  static const std::string& pipeline_name();
62 
63  static void about_to_finish(GstElement*, gpointer user_data);
64 
65  static void source_setup(GstElement*,
66  GstElement *source,
67  gpointer user_data);
68 
70  ~Playbin();
71 
72  void reset();
73  void reset_pipeline();
74 
75  void on_new_message(const Bus::Message& message);
76  void on_new_message_async(const Bus::Message& message);
77  void process_message_element(GstMessage *message);
78 
80 
82 
83  void create_video_sink(uint32_t texture_id);
84 
85  void set_volume(double new_volume);
86 
89 
92 
94  uint64_t position() const;
96  uint64_t duration() const;
97 
98  void set_uri(const std::string& uri, const core::ubuntu::media::Player::HeadersType& headers, bool do_pipeline_reset = true);
99  std::string uri() const;
100 
101  void setup_source(GstElement *source);
102 
103  // Sets the pipeline state in the main thread context instead of the possibility of creating
104  // a deadlock in the streaming thread
105  static gboolean set_state_in_main_thread(gpointer user_data);
106  // Sets the pipeline's state (stopped, playing, paused, etc). use_main_thread will set the
107  // pipeline's new_state in the main thread context.
108  bool set_state_and_wait(GstState new_state, bool use_main_thread = false);
109  bool seek(const std::chrono::microseconds& ms);
110 
113 
114  std::string file_info_from_uri(const std::string& uri) const;
115  std::string encode_uri(const std::string& uri) const;
116  std::string decode_uri(const std::string& uri) const;
117  std::string get_file_content_type(const std::string& uri) const;
118 
119  bool is_audio_file(const std::string& uri) const;
120  bool is_video_file(const std::string& uri) const;
121 
123 
124  bool can_play_streams() const;
125 
126  GstElement* pipeline;
129  GstElement* video_sink;
130  GstElement* audio_sink;
133  mutable uint64_t previous_position;
139  struct
140  {
141  core::Signal<void> about_to_finish;
142  core::Signal<Bus::Message::Detail::ErrorWarningInfo> on_error;
143  core::Signal<Bus::Message::Detail::ErrorWarningInfo> on_warning;
144  core::Signal<Bus::Message::Detail::ErrorWarningInfo> on_info;
145  core::Signal<Bus::Message::Detail::Tag> on_tag_available;
146  core::Signal<std::pair<Bus::Message::Detail::StateChanged,std::string>> on_state_changed;
147  core::Signal<uint64_t> on_seeked_to;
148  core::Signal<void> on_end_of_stream;
149  core::Signal<core::ubuntu::media::Player::PlaybackStatus> on_playback_status_changed;
150  core::Signal<core::ubuntu::media::Player::Orientation> on_orientation_changed;
151  core::Signal<core::ubuntu::media::video::Dimensions> on_video_dimensions_changed;
152  core::Signal<void> client_disconnected;
153  core::Signal<int> on_buffering_changed;
154  } signals;
160 
161 private:
162  void setup_video_sink_for_buffer_streaming(void);
163  bool is_supported_video_sink(void) const;
164  bool connect_to_consumer(void);
165  void send_buffer_data(int fd, void *data, size_t len);
166  void send_frame_ready(void);
167  void process_missing_plugin_message(GstMessage *message);
168 
171  std::string video_sink_name;
172  int sock_consumer;
173 };
174 }
175 
176 #endif // GSTREAMER_PLAYBIN_H_
static std::string get_audio_role_str(core::ubuntu::media::Player::AudioStreamRole audio_role)
Definition: playbin.cpp:437
MediaFileType media_file_type() const
Definition: playbin.cpp:891
core::ubuntu::media::video::Dimensions get_video_dimensions() const
Definition: playbin.cpp:696
core::Signal< Bus::Message::Detail::Tag > on_tag_available
Definition: playbin.h:145
bool is_audio_file(const std::string &uri) const
Definition: playbin.cpp:863
static void source_setup(GstElement *, GstElement *source, gpointer user_data)
Definition: playbin.cpp:111
void setup_source(GstElement *source)
Definition: playbin.cpp:564
std::tuple< Height, Width > Dimensions
Height and Width of a video.
Definition: dimensions.h:139
gint audio_stream_id
Definition: playbin.h:157
void set_uri(const std::string &uri, const core::ubuntu::media::Player::HeadersType &headers, bool do_pipeline_reset=true)
Definition: playbin.cpp:526
void process_message_element(GstMessage *message)
Definition: playbin.cpp:285
Definition: bus.h:33
static gboolean set_state_in_main_thread(gpointer user_data)
Definition: playbin.cpp:597
bool seek(const std::chrono::microseconds &ms)
Definition: playbin.cpp:686
void reset_pipeline()
Definition: playbin.cpp:227
core::Connection on_new_message_connection_async
Definition: playbin.h:131
static const std::string & pipeline_name()
Definition: playbin.cpp:99
std::string file_info_from_uri(const std::string &uri) const
Definition: playbin.cpp:725
core::Signal< Bus::Message::Detail::ErrorWarningInfo > on_warning
Definition: playbin.h:143
Playbin(const core::ubuntu::media::Player::PlayerKey key)
Definition: playbin.cpp:121
core::Signal< core::ubuntu::media::Player::PlaybackStatus > on_playback_status_changed
Definition: playbin.h:149
void emit_video_dimensions_changed_if_changed(const core::ubuntu::media::video::Dimensions &new_dimensions)
Definition: playbin.cpp:717
void set_lifetime(core::ubuntu::media::Player::Lifetime)
Definition: playbin.cpp:492
struct gstreamer::Playbin::@12 signals
GstElement * video_sink
Definition: playbin.h:129
void set_audio_stream_role(core::ubuntu::media::Player::AudioStreamRole new_audio_role)
Definition: playbin.cpp:474
bool is_missing_audio_codec
Definition: playbin.h:155
gint video_stream_id
Definition: playbin.h:158
std::map< std::string, std::string > HeadersType
Definition: player.h:65
GstState current_new_state
Definition: playbin.h:159
std::string get_file_content_type(const std::string &uri) const
Definition: playbin.cpp:844
core::Signal< uint64_t > on_seeked_to
Definition: playbin.h:147
void on_new_message(const Bus::Message &message)
core::Signal< core::ubuntu::media::Player::Orientation > on_orientation_changed
Definition: playbin.h:150
core::ubuntu::media::Player::Orientation orientation_lut(const gchar *orientation)
Definition: playbin.cpp:459
uint64_t position() const
Definition: playbin.cpp:497
core::Signal< std::pair< Bus::Message::Detail::StateChanged, std::string > > on_state_changed
Definition: playbin.h:146
gstreamer::Bus & message_bus()
Definition: playbin.cpp:377
core::Signal< void > about_to_finish
Definition: playbin.h:141
bool is_video_file(const std::string &uri) const
Definition: playbin.cpp:877
std::string uri() const
Definition: playbin.cpp:586
void setup_pipeline_for_audio_video()
Definition: playbin.cpp:382
bool set_state_and_wait(GstState new_state, bool use_main_thread=false)
Definition: playbin.cpp:607
std::string decode_uri(const std::string &uri) const
Definition: playbin.cpp:830
GstElement * pipeline
Definition: playbin.h:126
core::Signal< Bus::Message::Detail::ErrorWarningInfo > on_info
Definition: playbin.h:144
core::Signal< void > on_end_of_stream
Definition: playbin.h:148
GstElement * audio_sink
Definition: playbin.h:130
gulong source_setup_handler_id
Definition: playbin.h:138
core::ubuntu::media::video::Dimensions cached_video_dimensions
Definition: playbin.h:134
core::Signal< core::ubuntu::media::video::Dimensions > on_video_dimensions_changed
Definition: playbin.h:151
bool can_play_streams() const
Definition: playbin.cpp:896
void set_volume(double new_volume)
Definition: playbin.cpp:431
void on_new_message_async(const Bus::Message &message)
Definition: playbin.cpp:319
core::Signal< int > on_buffering_changed
Definition: playbin.h:153
core::Signal< Bus::Message::Detail::ErrorWarningInfo > on_error
Definition: playbin.h:142
void create_video_sink(uint32_t texture_id)
Definition: playbin.cpp:421
gulong about_to_finish_handler_id
Definition: playbin.h:137
std::string encode_uri(const std::string &uri) const
Definition: playbin.cpp:764
gstreamer::Bus bus
Definition: playbin.h:127
bool is_missing_video_codec
Definition: playbin.h:156
MediaFileType file_type
Definition: playbin.h:128
core::Signal< void > client_disconnected
Definition: playbin.h:152
core::ubuntu::media::Player::HeadersType request_headers
Definition: playbin.h:135
uint64_t duration() const
Definition: playbin.cpp:517
uint64_t previous_position
Definition: playbin.h:133
core::ubuntu::media::Player::Lifetime player_lifetime
Definition: playbin.h:136