forked from mapmapteam/mapmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaImpl.h
271 lines (219 loc) · 6.5 KB
/
MediaImpl.h
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
263
264
265
266
267
268
269
270
271
/*
* MediaImpl.h
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
* (c) 2012 Jean-Sebastien Senecal
* (c) 2004 Mathieu Guindon, Julien Keable
* Based on code from Drone http://github.com/sofian/drone
* Based on code from the GStreamer Tutorials http://docs.gstreamer.com/display/GstSDK/Tutorials
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef VIDEO_IMPL_H_
#define VIDEO_IMPL_H_
#include <glib.h>
#include <gst/gst.h>
#include <gst/app/gstappsink.h>
#include <QtGlobal>
#include <QtOpenGL>
#include <QMutex>
#include <QWaitCondition>
#if __APPLE__
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
#include <tr1/memory>
/**
* Private declaration of the video player.
* This is done this way so that GStreamer header don't need to be
* included in the whole project. (just in this file)
*/
class MediaImpl
{
public:
/**
* Constructor.
* This media player works for both video files and shared memory sockets.
* If live is true, it's a shared memory socket.
*/
MediaImpl(const QString uri, bool live);
~MediaImpl();
// void setUri(const QString uri);
/**
* Returns whether or not GStreamer video support is ok.
*/
static bool hasVideoSupport();
/**
* Sets up the player.
* Basically calls loadMovie().
*/
void build();
/**
* Returns the width of the video image.
*/
int getWidth() const;
/**
* Returns the height of the video image.
*/
int getHeight() const;
/**
* Returns the path to the media file being played.
*/
QString getUri() const;
/**
* When using the shared memory source, returns whether or not we
* are attached to a shared memory socket.
*/
bool getAttached();
/**
* Returns the raw image of the last video frame.
* It is currently unused!
*/
const uchar* getBits();
/// Returns true iff bits have changed since last call to getBits().
bool bitsHaveChanged() const { return _bitsChanged; }
/**
* Checks if the pipeline is ready.
*
* Returns whether or not the elements in the pipeline are connected,
* and if we are using shmsrc, if the shared memory socket is being read.
*/
bool isReady() const { return _movieReady && _padHandlerData.videoIsConnected; }
bool videoIsConnected() const { return _padHandlerData.videoIsConnected; }
/**
* Performs regular updates (checks if movie is ready and checks messages).
*/
void update();
// void runAudio();
/**
* Loads a new movie file.
*
* Creates a new GStreamer pipeline, opens a movie or a shmsrc socket.
*/
bool loadMovie(QString filename);
bool setPlayState(bool play);
/**
* Tells the MediaImpl that we are actually reading from a shmsrc.
* Called from the GStreamer callback of the shmsrc.
*/
void setAttached(bool attach);
void setRate(double rate=1.0);
double getRate() const { return _rate; }
void resetMovie();
protected:
void unloadMovie();
void freeResources();
private:
/**
* Checks if we reached the end of the video file.
*
* Returns false if the pipeline is not ready yet.
*/
bool _eos() const;
// void _finish();
// void _init();
// bool _preRun();
void _checkMessages();
void _setMovieReady(bool ready);
bool _isMovieReady() const { return _movieReady; }
bool getPlayState() const { return _playState; };
void _setFinished(bool finished);
// Sends the appropriate seek events to adjust to rate.
void _updateRate();
void _freeCurrentSample();
public:
// GStreamer callbacks.
struct GstPadHandlerData {
GstElement* videoToConnect;
GstElement* videoSink;
bool videoIsConnected;
int width;
int height;
GstPadHandlerData() :
videoToConnect(NULL), videoSink(NULL),
videoIsConnected(false),
width(-1), height(-1)
{}
};
// GStreamer callback that simply sets the #newSample# flag to point to TRUE.
static GstFlowReturn gstNewSampleCallback(GstElement*, MediaImpl *p);
//static GstFlowReturn gstNewPreRollCallback (GstAppSink * appsink, gpointer user_data);
// GStreamer callback that plugs the audio/video pads into the proper elements when they
// are made available by the source.
static void gstPadAddedCallback(GstElement *src, GstPad *newPad, MediaImpl::GstPadHandlerData* data);
/// Locks mutex (default = no effect).
void lockMutex();
/// Unlocks mutex (default = no effect).
void unlockMutex();
private:
//locals
// gstreamer elements
GstBus *_bus;
GstElement *_pipeline;
GstElement *_uridecodebin0;
GstElement *_shmsrc0;
GstElement *_gdpdepay0;
//GstElement *_audioQueue;
//GstElement *_audioConvert;
//GstElement *_audioResample;
GstElement *_queue0;
GstElement *_videoconvert0;
//GstElement *_audioSink;
GstElement *_appsink0;
/**
* Temporary contains the image data of the last frame.
*/
GstSample *_currentFrameSample;
GstBuffer *_currentFrameBuffer;
GstMapInfo _mapInfo;
bool _bitsChanged;
/**
* shmsrc socket poller.
*/
GSource *_pollSource;
GstPadHandlerData _padHandlerData;
// unused
int _width;
// unused
int _height;
/// Raw image data of the last video frame.
uchar *_data;
/// Is seek enabled on the current pipeline?
bool _seekEnabled;
/// Playback rate (negative ==> reverse).
double _rate;
/// Whether or not we are reading video from a shmsrc.
bool _isSharedMemorySource;
/// Whether or not we are attached to a shmsrc, if using a shmsrc.
bool _attached;
// unused
bool _terminate;
/// Is the movie (or rather pipeline) ready to play.
bool _movieReady;
/// Is the movie playing (as opposed to paused).
bool _playState;
/// Main mutex.
QMutex _mutex;
/// Main mutex locker (for the lockMutex() / unlockMutex() methods).
QMutexLocker* _mutexLocker;
private:
/**
* Path of the movie file being played.
*/
QString _uri;
static const int MAX_SAMPLES_IN_BUFFER_QUEUES = 30;
};
#endif /* ifndef */