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
41namespace gstreamer
42{
43struct Playbin
44{
46 {
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;
160
161private:
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_
std::map< std::string, std::string > HeadersType
Definition: player.h:65
std::tuple< Height, Width > Dimensions
Height and Width of a video.
Definition: dimensions.h:139
Definition: bus.h:34
void reset_pipeline()
Definition: playbin.cpp:227
void setup_pipeline_for_audio_video()
Definition: playbin.cpp:382
static const std::string & pipeline_name()
Definition: playbin.cpp:99
bool set_state_and_wait(GstState new_state, bool use_main_thread=false)
Definition: playbin.cpp:607
uint64_t duration() const
Definition: playbin.cpp:517
core::ubuntu::media::Player::Orientation orientation_lut(const gchar *orientation)
Definition: playbin.cpp:459
bool is_missing_audio_codec
Definition: playbin.h:155
core::Signal< Bus::Message::Detail::ErrorWarningInfo > on_error
Definition: playbin.h:142
core::ubuntu::media::video::Dimensions get_video_dimensions() const
Definition: playbin.cpp:696
bool can_play_streams() const
Definition: playbin.cpp:914
core::Signal< uint64_t > on_seeked_to
Definition: playbin.h:147
std::string encode_uri(const std::string &uri) const
Definition: playbin.cpp:782
GstElement * pipeline
Definition: playbin.h:126
void emit_video_dimensions_changed_if_changed(const core::ubuntu::media::video::Dimensions &new_dimensions)
Definition: playbin.cpp:735
gulong source_setup_handler_id
Definition: playbin.h:138
core::Signal< void > about_to_finish
Definition: playbin.h:141
core::ubuntu::media::video::Dimensions cached_video_dimensions
Definition: playbin.h:134
bool is_missing_video_codec
Definition: playbin.h:156
core::Signal< void > on_end_of_stream
Definition: playbin.h:148
std::string get_file_content_type(const std::string &uri) const
Definition: playbin.cpp:862
core::Signal< Bus::Message::Detail::ErrorWarningInfo > on_warning
Definition: playbin.h:143
core::ubuntu::media::Player::HeadersType request_headers
Definition: playbin.h:135
core::Signal< Bus::Message::Detail::Tag > on_tag_available
Definition: playbin.h:145
gint audio_stream_id
Definition: playbin.h:157
uint64_t position() const
Definition: playbin.cpp:497
core::Signal< void > client_disconnected
Definition: playbin.h:152
void set_volume(double new_volume)
Definition: playbin.cpp:431
void set_lifetime(core::ubuntu::media::Player::Lifetime)
Definition: playbin.cpp:492
core::Signal< int > on_buffering_changed
Definition: playbin.h:153
MediaFileType media_file_type() const
Definition: playbin.cpp:909
void process_message_element(GstMessage *message)
Definition: playbin.cpp:285
MediaFileType file_type
Definition: playbin.h:128
void setup_source(GstElement *source)
Definition: playbin.cpp:564
static std::string get_audio_role_str(core::ubuntu::media::Player::AudioStreamRole audio_role)
Definition: playbin.cpp:437
bool is_video_file(const std::string &uri) const
Definition: playbin.cpp:895
core::Connection on_new_message_connection_async
Definition: playbin.h:131
std::string file_info_from_uri(const std::string &uri) const
Definition: playbin.cpp:743
bool is_audio_file(const std::string &uri) const
Definition: playbin.cpp:881
Playbin(const core::ubuntu::media::Player::PlayerKey key)
Definition: playbin.cpp:121
std::string uri() const
Definition: playbin.cpp:586
void set_audio_stream_role(core::ubuntu::media::Player::AudioStreamRole new_audio_role)
Definition: playbin.cpp:474
std::string decode_uri(const std::string &uri) const
Definition: playbin.cpp:848
core::Signal< core::ubuntu::media::Player::Orientation > on_orientation_changed
Definition: playbin.h:150
core::Signal< core::ubuntu::media::video::Dimensions > on_video_dimensions_changed
Definition: playbin.h:151
core::Signal< Bus::Message::Detail::ErrorWarningInfo > on_info
Definition: playbin.h:144
void set_uri(const std::string &uri, const core::ubuntu::media::Player::HeadersType &headers, bool do_pipeline_reset=true)
Definition: playbin.cpp:526
gstreamer::Bus bus
Definition: playbin.h:127
core::Signal< core::ubuntu::media::Player::PlaybackStatus > on_playback_status_changed
Definition: playbin.h:149
uint64_t previous_position
Definition: playbin.h:133
struct gstreamer::Playbin::@12 signals
core::ubuntu::media::Player::Lifetime player_lifetime
Definition: playbin.h:136
void on_new_message_async(const Bus::Message &message)
Definition: playbin.cpp:319
static void about_to_finish(GstElement *, gpointer user_data)
bool seek(const std::chrono::microseconds &ms)
Definition: playbin.cpp:686
GstElement * audio_sink
Definition: playbin.h:130
void create_video_sink(uint32_t texture_id)
Definition: playbin.cpp:421
core::Signal< std::pair< Bus::Message::Detail::StateChanged, std::string > > on_state_changed
Definition: playbin.h:146
static gboolean set_state_in_main_thread(gpointer user_data)
Definition: playbin.cpp:597
GstElement * video_sink
Definition: playbin.h:129
GstState current_new_state
Definition: playbin.h:159
void on_new_message(const Bus::Message &message)
gstreamer::Bus & message_bus()
Definition: playbin.cpp:377
gint video_stream_id
Definition: playbin.h:158
gulong about_to_finish_handler_id
Definition: playbin.h:137
static void source_setup(GstElement *, GstElement *source, gpointer user_data)
Definition: playbin.cpp:111