/
qtoverlay.cpp
261 lines (212 loc) · 9.52 KB
/
qtoverlay.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <QApplication>
#include <QRect>
#include <Qt>
#include <gst/video/videooverlay.h>
#include <qpa/qplatformnativeinterface.h>
#include <QDebug>
#include "qtoverlay.h"
struct wl_display *display = NULL;
struct wl_surface *surface = NULL;
#define GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE "GstWaylandDisplayHandleContextType"
gboolean gst_is_wayland_display_handle_need_context_message(GstMessage *msg) {
const gchar *type = NULL;
g_return_val_if_fail(GST_IS_MESSAGE(msg), FALSE);
if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_NEED_CONTEXT && gst_message_parse_context_type(msg, &type)) {
return !g_strcmp0(type, GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
}
return FALSE;
}
GstContext *gst_wayland_display_handle_context_new(struct wl_display *display) {
GstContext *context = gst_context_new(GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE, TRUE);
gst_structure_set(gst_context_writable_structure(context), "handle", G_TYPE_POINTER, display, NULL);
return context;
}
PlayerWindow::PlayerWindow(GstElement *p) : pipeline(p), state(GST_STATE_NULL), totalDuration(GST_CLOCK_TIME_NONE) {
playBt = new QPushButton("Play");
pauseBt = new QPushButton("Pause");
stopBt = new QPushButton("Stop");
videoWindow = new QWidget();
slider = new QSlider(Qt::Horizontal);
timer = new QTimer();
// debug only, show video window with red background
QPalette pal(videoWindow->palette());
pal.setColor(QPalette::Background, Qt::red);
videoWindow->setAutoFillBackground(true);
videoWindow->setPalette(pal);
connect(playBt, SIGNAL(clicked()), this, SLOT(onPlayClicked()));
connect(pauseBt, SIGNAL(clicked()), this, SLOT(onPauseClicked()));
connect(stopBt, SIGNAL(clicked()), this, SLOT(onStopClicked()));
connect(slider, SIGNAL(sliderReleased()), this, SLOT(onSeek()));
buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(playBt);
buttonLayout->addWidget(pauseBt);
buttonLayout->addWidget(stopBt);
buttonLayout->addWidget(slider);
playerLayout = new QVBoxLayout;
playerLayout->addWidget(videoWindow);
playerLayout->addLayout(buttonLayout);
this->setLayout(playerLayout);
connect(timer, SIGNAL(timeout()), this, SLOT(refreshSlider()));
connect(this, SIGNAL(sigAlbum(QString)), this, SLOT(onAlbumAvaiable(QString)));
connect(this, SIGNAL(sigState(GstState)), this, SLOT(onState(GstState)));
connect(this, SIGNAL(sigEos()), this, SLOT(onEos()));
}
WId PlayerWindow::getVideoWId() const { return videoWindow->winId(); }
void PlayerWindow::resizeEvent(QResizeEvent *ev) {
g_print("resizeEvent: \n");
g_print("window frameGeometry x: %d, y:%d, w: %d, h: %d\n", this->frameGeometry().x(), this->frameGeometry().y(),
this->frameGeometry().width(), this->frameGeometry().height());
g_print("window geometry x: %d, y:%d, w: %d, h: %d\n", this->geometry().x(), this->geometry().y(),
this->geometry().width(), this->geometry().height());
QRect vre = this->videoWindow->geometry();
g_print("vre x: %d, y:%d, w: %d, h: %d \n", vre.x(), vre.y(), vre.width(), vre.height());
//TODO: resize the gstreamer video overlay
}
void PlayerWindow::onPlayClicked() {
GstState st = GST_STATE_NULL;
gst_element_get_state (GST_ELEMENT(pipeline), &st, NULL, GST_CLOCK_TIME_NONE);
if (st < GST_STATE_PAUSED) {
//TODO: should we recreate the pipeline if stopped?
// Pipeline stopped, we need set overlay again
// GstElement *vsink = gst_element_factory_make ("wayland", "wsink");
// g_object_set(GST_OBJECT(pipeline), "video-sink", vsink, NULL);
}
gst_element_set_state(pipeline, GST_STATE_PLAYING);
}
void PlayerWindow::onPauseClicked() { gst_element_set_state(pipeline, GST_STATE_PAUSED); }
void PlayerWindow::onStopClicked() { gst_element_set_state(pipeline, GST_STATE_NULL); }
void PlayerWindow::onAlbumAvaiable(const QString &album) { setWindowTitle(album); }
void PlayerWindow::onState(GstState st) {
if (state != st) {
state = st;
if (state == GST_STATE_PLAYING) {
timer->start(1000);
}
if (state < GST_STATE_PAUSED) {
timer->stop();
}
}
}
void PlayerWindow::refreshSlider() {
gint64 current = GST_CLOCK_TIME_NONE;
if (state == GST_STATE_PLAYING) {
if (!GST_CLOCK_TIME_IS_VALID(totalDuration)) {
if (gst_element_query_duration(pipeline, GST_FORMAT_TIME, &totalDuration)) {
slider->setRange(0, totalDuration / GST_SECOND);
}
}
if (gst_element_query_position(pipeline, GST_FORMAT_TIME, ¤t)) {
g_print("%ld / %ld\n", current / GST_SECOND, totalDuration / GST_SECOND);
slider->setValue(current / GST_SECOND);
}
}
}
void PlayerWindow::onSeek() {
gint64 pos = slider->sliderPosition();
g_print("seek: %ld\n", pos);
gst_element_seek_simple(pipeline, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH, pos * GST_SECOND);
}
void PlayerWindow::onEos() { gst_element_set_state(pipeline, GST_STATE_NULL); }
gboolean PlayerWindow::postGstMessage(GstBus *bus, GstMessage *message, gpointer user_data) {
PlayerWindow *pw = NULL;
if (user_data) {
pw = reinterpret_cast<PlayerWindow *>(user_data);
}
switch (GST_MESSAGE_TYPE(message)) {
case GST_MESSAGE_STATE_CHANGED: {
GstState old_state, new_state, pending_state;
gst_message_parse_state_changed(message, &old_state, &new_state, &pending_state);
pw->sigState(new_state);
break;
}
case GST_MESSAGE_TAG: {
GstTagList *tags = NULL;
gst_message_parse_tag(message, &tags);
gchar *album = NULL;
if (gst_tag_list_get_string(tags, GST_TAG_ALBUM, &album)) {
pw->sigAlbum(album);
g_free(album);
}
gst_tag_list_unref(tags);
break;
}
case GST_MESSAGE_EOS: {
pw->sigEos();
break;
}
case GST_MESSAGE_NEED_CONTEXT:
g_print("GST_MESSAGE_NEED_CONTEXT\n");
break;
default:
break;
}
return TRUE;
}
static GstBusSyncReply bus_sync_handler(GstBus *bus, GstMessage *message, gpointer user_data) {
PlayerWindow *window = reinterpret_cast<PlayerWindow *>(user_data);
if (gst_is_wayland_display_handle_need_context_message(message)) {
g_print("gst_is_wayland_display_handle_need_context_message\n");
GstContext *context;
// get the wlayland display handle from Qt
QPlatformNativeInterface *native = QGuiApplication::platformNativeInterface();
display = static_cast<struct wl_display *>(native->nativeResourceForWindow("display", NULL));
g_print("display: %p\n", display);
context = gst_wayland_display_handle_context_new(display);
gst_element_set_context(GST_ELEMENT(GST_MESSAGE_SRC(message)), context);
goto drop;
} else if (gst_is_video_overlay_prepare_window_handle_message(message)) {
g_print("gst_is_video_overlay_prepare_window_handle_message\n");
// we can only get the handle for the top window
// the handle for videoWindow will be null !!
QPlatformNativeInterface *native = QGuiApplication::platformNativeInterface();
QWindow *windowHandle = window->windowHandle();
g_print("top window handle: %p\n", windowHandle);
surface = static_cast<struct wl_surface *>(native->nativeResourceForWindow("surface", windowHandle));
g_print("surface: %p\n", surface);
gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(GST_MESSAGE_SRC(message)), (guintptr)surface);
QRect re = window->frameGeometry();
g_print("window rect x: %d, y:%d, w:%d, h:%d\n", re.x(), re.y(), re.width(), re.height());
QRect vre = window->videoWindow->geometry();
g_print("video window rect x: %d, y:%d, w: %d, h: %d \n", vre.x(), vre.y(), vre.width(), vre.height());
gst_video_overlay_set_render_rectangle(GST_VIDEO_OVERLAY(GST_MESSAGE_SRC(message)),
vre.x() - re.x(), vre.y() - re.y(), vre.width(), vre.height());
goto drop;
}
return GST_BUS_PASS;
drop:
gst_message_unref(message);
return GST_BUS_DROP;
}
int main(int argc, char *argv[]) {
gst_init(&argc, &argv);
QApplication app(argc, argv);
app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
qDebug() << "qt platform name: " << QGuiApplication::platformName();
// prepare the pipeline
GstElement *pipeline = gst_parse_launch("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);
// prepare the ui
PlayerWindow *window = new PlayerWindow(pipeline);
window->resize(640, 480);
window->show();
GstElement *vsink = gst_element_factory_make("waylandsink", "wsink");
g_object_set(GST_OBJECT(pipeline), "video-sink", vsink, NULL);
// connect to interesting signals
GstBus *bus = gst_element_get_bus(pipeline);
gst_bus_add_watch(bus, &PlayerWindow::postGstMessage, window);
gst_bus_set_sync_handler(bus, bus_sync_handler, window, NULL);
gst_object_unref(bus);
// run the pipeline
GstStateChangeReturn sret = gst_element_set_state(pipeline, GST_STATE_PLAYING);
if (sret == GST_STATE_CHANGE_FAILURE) {
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
// Exit application
QTimer::singleShot(0, QApplication::activeWindow(), SLOT(quit()));
}
int ret = app.exec();
window->hide();
delete window;
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
return ret;
}