-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvideoplayer.cpp
262 lines (217 loc) · 7.03 KB
/
videoplayer.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
262
/*
* Copyright (C) 2013-2015 Atmel Corporation.
*
* This file is licensed under the terms of the Atmel LIMITED License Agreement
* as written in the COPYING file that is delivered in the project root directory.
*
* DISCLAIMER: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
* See the COPYING license file for more details.
*/
// NOTE: this class is using gstreamer naming convention
#include "videoplayer.h"
#include <QDesktopWidget>
#define SRC_NAME "srcVideo"
#define PERF_NAME "perf"
#define PROGRESS_NAME "progress"
#define VOLUME_NAME "volume"
#if defined PLANA || defined PLANC
#define PIPE "uridecodebin expose-all-streams=false name=" SRC_NAME " \
caps=video/x-h264;audio/x-raw " SRC_NAME ". ! queue ! h264parse ! \
queue ! g1h264dec ! video/x-raw,width=%2,height=%3,format=BGRx ! \
progressreport silent=true do-query=true update-freq=1 format=time \
name=" PROGRESS_NAME " ! perf name=" PERF_NAME " ! \
g1kmssink gem-name=%1 " SRC_NAME ". ! queue ! audioconvert ! \
audio/x-raw,format=S16LE ! volume name=" VOLUME_NAME " ! \
alsasink async=false enable-last-sample=false"
#else
#define PIPE "uridecodebin expose-all-streams=false name=" SRC_NAME " \
caps=video/x-h264;audio/x-raw " SRC_NAME ". ! queue ! h264parse ! \
queue ! g1h264dec ! video/x-raw,width=%2,height=%3 ! \
progressreport silent=true do-query=true update-freq=1 format=time \
name=" PROGRESS_NAME " ! perf name=" PERF_NAME " ! \
g1fbdevsink zero-memcpy=true max-lateness=-1 async=false \
enable-last-sample=false "
#endif
static gboolean
busCallback (GstBus *bus,
GstMessage *message,
gpointer data)
{
VideoPlayer *_this = (VideoPlayer *)data;
switch (GST_MESSAGE_TYPE (message)) {
case GST_MESSAGE_ERROR: {
GError *err;
gchar *debug;
gst_message_parse_error (message, &err, &debug);
g_print ("Error: %s\n", err->message);
g_error_free (err);
g_free (debug);
// g_main_loop_quit (loop);
break;
}
case GST_MESSAGE_EOS:{
/// end-of-stream
#if ((defined PLANA || defined PLAND) && defined FPSVIEW)
gchar *perf = NULL;
perf = g_strdup_printf ("fps: 0\n"
"bps (Mbit/s): 0");
emit _this->fpsChanged(QString::fromStdString(perf));
#endif //PLANA
emit _this->playState(5);
_this->setMedia(QString("/opt/VideoPlayer/media/Microchip-masters.mp4"));
break;
}
case GST_MESSAGE_ELEMENT:{
#if defined PLANA || defined PLAND
const GstStructure *info = gst_message_get_structure (message);
if(gst_structure_has_name(info, PROGRESS_NAME)){
const GValue *vcurrent;
gint64 current = 0;
const GValue *vtotal;
gint64 total = 0;
vtotal = gst_structure_get_value (info, "total");
total = g_value_get_int64(vtotal);
vcurrent = gst_structure_get_value (info, "current");
current = g_value_get_int64(vcurrent);
emit _this->positionChanged(current);
emit _this->durationChanged(total);
}
#endif //PLANA
break;
}
case GST_MESSAGE_INFO: {
#if ((defined PLANA || defined PLAND) && defined FPSVIEW)
if (!strncmp(GST_MESSAGE_SRC_NAME(message), PERF_NAME, 1)) {
GError *error = NULL;
gchar *debug = NULL;
gchar *bps = NULL;
gchar *fps = NULL;
gchar *perf = NULL;
int bpsi;
gst_message_parse_info (message, &error, &debug);
fps = g_strrstr (debug, "fps: ") + 5;
g_debug ("Frames per second: %s\n", fps);
/* Hack to save up a little performance and tell strrstr to stop
* instead of doing an auxiliary search
*/
*((gchar *)(fps-7)) = '\0';
bps = g_strrstr (debug, "Bps: ") + 5;
g_debug ("Bytes per second: %s\n", bps);
bpsi = atoi(bps);
bpsi = bpsi/1000000;
perf = g_strdup_printf ("fps: %s\n"
"bps (Mbit/s): %d", fps, bpsi);
emit _this->fpsChanged(QString::fromStdString(perf));
g_error_free (error);
g_free (debug);
g_free (perf);
}
#endif //PLANA
break;
}
default:
/* unhandled message */
break;
}
/* we want to be notified again the next time there is a message
* on the bus, so returning TRUE (FALSE means we want to stop watching
* for messages on the bus and our callback should not be called again)
*/
return TRUE;
}
VideoPlayer::VideoPlayer(int gem, int width, int height)
: _gem(gem),
_width(width),
_height(height)
{
gst_init(NULL, NULL);
this->_videoPipeline = NULL;
this->_src = NULL;
this->_volume = NULL;
}
bool
VideoPlayer::createPipeline(){
GError *error = NULL;
GstBus *bus;
guint bus_watch_id;
/* Make sure we don't leave orphan references */
destroyPipeline();
printf("gem = %d _width= %d _height=%d \n", _gem, _width, _height);
QString pipe(QString(PIPE).arg(_gem).arg(_width).arg(_height));
printf("%s\n", pipe.toStdString().c_str());
this->_videoPipeline = gst_parse_launch (pipe.toStdString().c_str(), &error);
if (!this->_videoPipeline) {
this->_videoPipeline = NULL;
printf("failed to create video pipeline...\n");
return false;
}
this->_src = gst_bin_get_by_name(GST_BIN(this->_videoPipeline), SRC_NAME);
if (!this->_src) {
this->_src = NULL;
printf("failed to get video src element...\n");
return false;
}
this->_volume = gst_bin_get_by_name(GST_BIN(this->_videoPipeline), VOLUME_NAME);
if (!this->_volume) {
this->_volume = NULL;
printf("failed to get volume element...\n");
return false;
}
bus = gst_pipeline_get_bus (GST_PIPELINE (this->_videoPipeline));
bus_watch_id = gst_bus_add_watch (bus, &busCallback, this);
gst_object_unref (bus);
return true;
}
void
VideoPlayer::destroyPipeline(){
if (this->_videoPipeline) {
this->null();
g_object_unref(this->_src);
g_object_unref(this->_videoPipeline);
this->_src = NULL;
this->_videoPipeline = NULL;
}
}
VideoPlayer::~VideoPlayer(){
destroyPipeline();
}
bool VideoPlayer::play(bool mute,int volume){
emit playState(this->setState(GST_STATE_PLAYING));
setVolume(volume);
setMute(mute);
}
bool VideoPlayer::pause(){
emit playState(!this->setState(GST_STATE_PAUSED));
}
bool VideoPlayer::null(){
return this->setState(GST_STATE_NULL);
}
bool VideoPlayer::setMedia(QString uri){
destroyPipeline();
createPipeline();
g_object_set(this->_src, "uri", qPrintable("file://" + uri), NULL);
return true;
}
bool VideoPlayer::setVolume(int volume) {
if (!this->_volume)
return false;
g_object_set(this->_volume, "volume", volume/100.0, NULL);
return true;
}
bool VideoPlayer::setMute(bool mute) {
if (!this->_volume)
return false;
g_object_set(this->_volume, "mute", mute, NULL);
return true;
}
bool VideoPlayer::setState(GstState state){
GstStateChangeReturn ret;
if(this->_videoPipeline){
ret = gst_element_set_state(this->_videoPipeline, state);
if (GST_STATE_CHANGE_FAILURE == ret) {
printf ("Unable to set video pipeline pipeline to %d!\n", state);
return false;
}
}else return false;
return true;
}