Music Hub  ..
A session-wide music playback service
playbin.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2013 Canonical Ltd.
3  * Copyright © 2022 UBports Foundation.
4  *
5  * Contact: Alberto Mardegan <mardy@users.sourceforge.net>
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License version 3,
9  * as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Authored by: Thomas Voß <thomas.voss@canonical.com>
20  */
21 
22 #ifndef GSTREAMER_PLAYBIN_H
23 #define GSTREAMER_PLAYBIN_H
24 
25 #include "bus.h"
26 #include "player.h"
27 
28 #include <QObject>
29 #include <QString>
30 #include <QUrl>
31 
32 #include <gio/gio.h>
33 #include <gst/gst.h>
34 
35 #include <chrono>
36 #include <string>
37 
38 // Uncomment to generate a dot file at the time that the pipeline
39 // goes to the PLAYING state. Make sure to export GST_DEBUG_DUMP_DOT_DIR
40 // before starting media-hub-server. To convert the dot file to some
41 // other image format, use: dot pipeline.dot -Tpng -o pipeline.png
42 //#define DEBUG_GST_PIPELINE
43 
44 namespace gstreamer
45 {
46 class Playbin: public QObject
47 {
48  Q_OBJECT
49 
50 public:
51  enum PlayFlags
52  {
53  GST_PLAY_FLAG_VIDEO = (1 << 0),
54  GST_PLAY_FLAG_AUDIO = (1 << 1),
55  GST_PLAY_FLAG_TEXT = (1 << 2)
56  };
57 
59  {
63  };
64 
66 
67  static const std::string& pipeline_name();
68 
69  static void about_to_finish(GstElement*, gpointer user_data);
70 
71  static void source_setup(GstElement*,
72  GstElement *source,
73  gpointer user_data);
74  static void streams_changed(GstElement*, gpointer user_data);
75 
77  ~Playbin();
78 
79  void reset();
80  void reset_pipeline();
81 
82  void on_new_message(const Bus::Message& message);
84  void process_message_element(GstMessage *message);
85 
87 
89 
90  void create_video_sink(uint32_t texture_id);
91 
92  void set_volume(double new_volume);
93 
96 
99 
101  uint64_t position() const;
103  uint64_t duration() const;
104 
105  void set_uri(const QUrl &uri, const lomiri::MediaHubService::Player::HeadersType& headers, bool do_pipeline_reset = true);
106  QUrl uri() const;
107 
108  void setup_source(GstElement *source);
109  void updateMediaFileType();
110 
111  // Sets the pipeline's state (stopped, playing, paused, etc).
112  bool set_state(GstState new_state);
113  bool seek(const std::chrono::microseconds& ms);
114 
115  QSize get_video_dimensions() const;
116 
117  QString file_info_from_uri(const QUrl &uri) const;
118  QString get_file_content_type(const QUrl &uri) const;
119 
120  bool is_audio_file(const QUrl &uri) const;
121  bool is_video_file(const QUrl &uri) const;
122 
124 
125  bool can_play_streams() const;
126 
127  GstElement* pipeline;
130  GstElement* video_sink;
131  GstElement* audio_sink;
133  mutable uint64_t previous_position;
145 
146 Q_SIGNALS:
150 
151  void aboutToFinish();
152  void seekedTo(uint64_t offset);
154  const QByteArray &source);
155  void mediaFileTypeChanged();
158  void videoDimensionChanged(const QSize &size);
159  void bufferingChanged(int progress);
160  void clientDisconnected();
161  void endOfStream();
162 
163 protected:
164  void setMediaFileType(MediaFileType fileType);
165 
166 private:
167  void setup_video_sink_for_buffer_streaming(void);
168  bool is_supported_video_sink(void) const;
169  bool connect_to_consumer(void);
170  void send_buffer_data(int fd, void *data, size_t len);
171  void send_frame_ready(void);
172  void process_missing_plugin_message(GstMessage *message);
173 
176  std::string video_sink_name;
177  int sock_consumer;
178 };
179 }
180 
181 #endif // GSTREAMER_PLAYBIN_H
gstreamer::Playbin::set_uri
void set_uri(const QUrl &uri, const lomiri::MediaHubService::Player::HeadersType &headers, bool do_pipeline_reset=true)
Definition: playbin.cpp:585
gstreamer::Playbin::m_fileType
MediaFileType m_fileType
Definition: playbin.h:129
gstreamer::Playbin::GST_PLAY_FLAG_AUDIO
@ GST_PLAY_FLAG_AUDIO
Definition: playbin.h:54
QObject
gstreamer::Playbin
Definition: playbin.h:46
gstreamer::Playbin::setup_pipeline_for_audio_video
void setup_pipeline_for_audio_video()
Definition: playbin.cpp:437
gstreamer::Playbin::player_lifetime
lomiri::MediaHubService::Player::Lifetime player_lifetime
Definition: playbin.h:135
gstreamer::Bus::Message
Definition: bus.h:39
gstreamer::Playbin::videoDimensionChanged
void videoDimensionChanged(const QSize &size)
gstreamer::Playbin::GST_PLAY_FLAG_VIDEO
@ GST_PLAY_FLAG_VIDEO
Definition: playbin.h:53
gstreamer::Playbin::video_stream_id
gint video_stream_id
Definition: playbin.h:143
gstreamer::Playbin::audio_sink
GstElement * audio_sink
Definition: playbin.h:131
gstreamer::Playbin::get_audio_role_str
static std::string get_audio_role_str(lomiri::MediaHubService::Player::AudioStreamRole audio_role)
Definition: playbin.cpp:496
gstreamer::Playbin::set_audio_stream_role
void set_audio_stream_role(lomiri::MediaHubService::Player::AudioStreamRole new_audio_role)
Definition: playbin.cpp:533
gstreamer::Playbin::audio_stream_id
gint audio_stream_id
Definition: playbin.h:142
gstreamer::Playbin::endOfStream
void endOfStream()
gstreamer::Playbin::position
uint64_t position() const
Definition: playbin.cpp:556
gstreamer::Playbin::about_to_finish_handler_id
gulong about_to_finish_handler_id
Definition: playbin.h:136
gstreamer::Playbin::get_video_dimensions
QSize get_video_dimensions() const
Definition: playbin.cpp:720
gstreamer::Bus
Definition: bus.h:36
gstreamer::Playbin::tagAvailable
void tagAvailable(Bus::Message::Detail::Tag tag)
gstreamer::Playbin::message_bus
gstreamer::Bus & message_bus()
Definition: playbin.cpp:432
gstreamer::Playbin::orientationChanged
void orientationChanged(lomiri::MediaHubService::Player::Orientation o)
gstreamer::Playbin::reset
void reset()
Definition: playbin.cpp:233
gstreamer::Playbin::clientDisconnected
void clientDisconnected()
lomiri::MediaHubService::AVBackend::Backend
Backend
Definition: player.h:36
gstreamer::Playbin::duration
uint64_t duration() const
Definition: playbin.cpp:576
gstreamer::Playbin::mediaFileTypeChanged
void mediaFileTypeChanged()
lomiri::MediaHubService::Player::HeadersType
QMap< QString, QString > HeadersType
Definition: player.h:57
gstreamer::Playbin::aboutToFinish
void aboutToFinish()
gstreamer::Playbin::updateMediaFileType
void updateMediaFileType()
Definition: playbin.cpp:665
gstreamer::Playbin::bufferingChanged
void bufferingChanged(int progress)
gstreamer::Playbin::MEDIA_FILE_TYPE_NONE
@ MEDIA_FILE_TYPE_NONE
Definition: playbin.h:60
gstreamer::Playbin::processVideoSinkStateChanged
void processVideoSinkStateChanged(const Bus::Message::Detail::StateChanged &state)
Definition: playbin.cpp:304
gstreamer::Playbin::set_volume
void set_volume(double new_volume)
Definition: playbin.cpp:490
gstreamer::Playbin::can_play_streams
bool can_play_streams() const
Definition: playbin.cpp:817
gstreamer::Bus::Message::Detail::ErrorWarningInfo
Definition: bus.h:190
gstreamer::Playbin::get_file_content_type
QString get_file_content_type(const QUrl &uri) const
Definition: playbin.cpp:761
lomiri::MediaHubService::Player::AudioStreamRole
AudioStreamRole
Definition: player.h:113
gstreamer::Playbin::m_audioChangedHandlerId
gulong m_audioChangedHandlerId
Definition: playbin.h:138
gstreamer::Playbin::GST_PLAY_FLAG_TEXT
@ GST_PLAY_FLAG_TEXT
Definition: playbin.h:55
gstreamer::Playbin::on_new_message
void on_new_message(const Bus::Message &message)
Definition: playbin.cpp:364
gstreamer::Playbin::errorOccurred
void errorOccurred(const Bus::Message::Detail::ErrorWarningInfo &)
gstreamer::Playbin::set_state
bool set_state(GstState new_state)
Definition: playbin.cpp:690
gstreamer::Playbin::is_audio_file
bool is_audio_file(const QUrl &uri) const
Definition: playbin.cpp:784
gstreamer::Playbin::current_new_state
GstState current_new_state
Definition: playbin.h:144
gstreamer
Definition: bus.h:34
lomiri::MediaHubService::Player::PlayerKey
uint32_t PlayerKey
Definition: player.h:55
gstreamer::Playbin::orientation_lut
lomiri::MediaHubService::Player::Orientation orientation_lut(const gchar *orientation)
Definition: playbin.cpp:518
gstreamer::Playbin::is_seeking
bool is_seeking
Definition: playbin.h:132
gstreamer::Playbin::MEDIA_FILE_TYPE_AUDIO
@ MEDIA_FILE_TYPE_AUDIO
Definition: playbin.h:61
gstreamer::Playbin::setMediaFileType
void setMediaFileType(MediaFileType fileType)
Definition: playbin.cpp:927
gstreamer::Playbin::m_videoChangedHandlerId
gulong m_videoChangedHandlerId
Definition: playbin.h:139
gstreamer::Playbin::mediaFileType
MediaFileType mediaFileType() const
Definition: playbin.cpp:812
gstreamer::Playbin::pipeline
GstElement * pipeline
Definition: playbin.h:127
gstreamer::Playbin::setup_source
void setup_source(GstElement *source)
Definition: playbin.cpp:618
gstreamer::Playbin::source_setup
static void source_setup(GstElement *, GstElement *source, gpointer user_data)
Definition: playbin.cpp:117
gstreamer::Playbin::reset_pipeline
void reset_pipeline()
Definition: playbin.cpp:246
gstreamer::Playbin::infoOccurred
void infoOccurred(const Bus::Message::Detail::ErrorWarningInfo &)
gstreamer::Playbin::uri
QUrl uri() const
Definition: playbin.cpp:679
gstreamer::Playbin::is_video_file
bool is_video_file(const QUrl &uri) const
Definition: playbin.cpp:798
gstreamer::Playbin::Playbin
Playbin(const lomiri::MediaHubService::Player::PlayerKey key)
Definition: playbin.cpp:137
gstreamer::Playbin::pipeline_name
static const std::string & pipeline_name()
Definition: playbin.cpp:105
gstreamer::Playbin::stateChanged
void stateChanged(const Bus::Message::Detail::StateChanged &state, const QByteArray &source)
lomiri::MediaHubService::Player::Lifetime
Lifetime
Definition: player.h:129
gstreamer::Playbin::warningOccurred
void warningOccurred(const Bus::Message::Detail::ErrorWarningInfo &)
gstreamer::Playbin::seek
bool seek(const std::chrono::microseconds &ms)
Definition: playbin.cpp:710
gstreamer::Playbin::file_info_from_uri
QString file_info_from_uri(const QUrl &uri) const
Definition: playbin.cpp:755
gstreamer::Playbin::streams_changed
static void streams_changed(GstElement *, gpointer user_data)
Definition: playbin.cpp:127
gstreamer::Playbin::create_video_sink
void create_video_sink(uint32_t texture_id)
Definition: playbin.cpp:480
gstreamer::Playbin::source_setup_handler_id
gulong source_setup_handler_id
Definition: playbin.h:137
gstreamer::Playbin::process_message_element
void process_message_element(GstMessage *message)
Definition: playbin.cpp:326
gstreamer::Playbin::MEDIA_FILE_TYPE_VIDEO
@ MEDIA_FILE_TYPE_VIDEO
Definition: playbin.h:62
gstreamer::Playbin::is_missing_video_codec
bool is_missing_video_codec
Definition: playbin.h:141
gstreamer::Playbin::video_sink
GstElement * video_sink
Definition: playbin.h:130
gstreamer::Playbin::MediaFileType
MediaFileType
Definition: playbin.h:58
gstreamer::Playbin::PlayFlags
PlayFlags
Definition: playbin.h:51
gstreamer::Playbin::bus
gstreamer::Bus bus
Definition: playbin.h:128
gstreamer::Playbin::is_missing_audio_codec
bool is_missing_audio_codec
Definition: playbin.h:140
gstreamer::Playbin::about_to_finish
static void about_to_finish(GstElement *, gpointer user_data)
Definition: playbin.cpp:111
gstreamer::Bus::Message::Detail::StateChanged
Definition: bus.h:212
bus.h
gstreamer::Playbin::~Playbin
~Playbin()
Definition: playbin.cpp:209
gstreamer::Bus::Message::Detail::Tag
Definition: bus.h:195
gstreamer::Playbin::set_lifetime
void set_lifetime(lomiri::MediaHubService::Player::Lifetime)
Definition: playbin.cpp:551
lomiri::MediaHubService::Player::Orientation
Orientation
Definition: player.h:121
gstreamer::Playbin::previous_position
uint64_t previous_position
Definition: playbin.h:133
gstreamer::Playbin::seekedTo
void seekedTo(uint64_t offset)
gstreamer::Playbin::request_headers
lomiri::MediaHubService::Player::HeadersType request_headers
Definition: playbin.h:134