-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrlPlayer.cpp
More file actions
140 lines (108 loc) · 3.37 KB
/
UrlPlayer.cpp
File metadata and controls
140 lines (108 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "UrlPlayer.h"
#include <CxxPtr/GstPtr.h>
#include "Log.h"
struct UrlPlayer::Private
{
Private(UrlPlayer* owner, const UrlPlayer::EosCallback& eosCallback);
gboolean onBusMessage(GstMessage*);
UrlPlayer *const owner;
const UrlPlayer::EosCallback eosCallback;
std::shared_ptr<spdlog::logger> log;
GstElementPtr pipelinePtr;
};
UrlPlayer::Private::Private(UrlPlayer* owner, const UrlPlayer::EosCallback& eosCallback):
owner(owner),
eosCallback(eosCallback),
log(MonitorLog())
{
}
gboolean UrlPlayer::Private::onBusMessage(GstMessage* message)
{
switch(GST_MESSAGE_TYPE(message)) {
case GST_MESSAGE_EOS:
owner->onEos();
break;
case GST_MESSAGE_ERROR: {
gchar* debug = nullptr;
GError* error = nullptr;
gst_message_parse_error(message, &error, &debug);
if(debug) {
log->error("Got error from GStreamer pipeline:\n{}\n{}", error->message, debug);
} else {
log->error("Got error from GStreamer pipeline:\n{}", error->message);
}
if(debug) g_free(debug);
if(error) g_error_free(error);
owner->onEos();
break;
}
default:
break;
}
return TRUE;
}
UrlPlayer::UrlPlayer(
bool showVideoStats,
bool sync,
const EosCallback& eosCallback) noexcept:
_p(std::make_unique<UrlPlayer::Private>(this, eosCallback)),
_showVideoStats(showVideoStats),
_sync(sync)
{
}
UrlPlayer::~UrlPlayer()
{
}
void UrlPlayer::onEos() noexcept
{
if(_p->eosCallback)
_p->eosCallback(*this);
stop();
}
bool UrlPlayer::isPlaying() const noexcept
{
return !!_p->pipelinePtr;
}
bool UrlPlayer::play(const std::string& url) noexcept
{
stop();
GstElementPtr pipelinePtr(gst_pipeline_new(nullptr));
GstElement* pipeline = pipelinePtr.get();
if(!pipeline) {
_p->log->error("Failed to create pipeline element");
return false;
}
GstElementPtr playbinPtr(gst_element_factory_make("playbin3", nullptr));
GstElement* playbin = playbinPtr.get();
if(!playbin) {
_p->log->error("Failed to create \"playbin3\" element");
return false;
}
GstElementPtr sinkPtr(_showVideoStats ?
gst_element_factory_make("fpsdisplaysink", nullptr) :
gst_element_factory_make("autovideosink", nullptr));
GstElement* sink = sinkPtr.get();
g_object_set(sink, "sync", _sync ? TRUE : FALSE, nullptr);
g_object_set(playbin, "video-sink", sinkPtr.release(), nullptr);
gst_bin_add_many(GST_BIN(pipeline), playbinPtr.release(), nullptr);
auto onBusMessageCallback =
+ [] (GstBus* bus, GstMessage* message, gpointer userData) -> gboolean
{
UrlPlayer* self = static_cast<UrlPlayer*>(userData);
return self->_p->onBusMessage(message);
};
GstBusPtr busPtr(gst_pipeline_get_bus(GST_PIPELINE(pipeline)));
gst_bus_add_watch(busPtr.get(), onBusMessageCallback, this);
g_object_set(playbin, "uri", url.c_str(), nullptr);
gst_element_set_state(pipeline, GST_STATE_PLAYING);
_p->pipelinePtr.swap(pipelinePtr);
return true;
}
void UrlPlayer::stop() noexcept
{
if(!_p->pipelinePtr)
return;
GstElement* pipeline = _p->pipelinePtr.get();
gst_element_set_state(pipeline, GST_STATE_NULL);
_p->pipelinePtr.reset();
}