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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "oglplayer.hxx"
#include "oglframegrabber.hxx"
#include "oglwindow.hxx"
#include <cppuhelper/supportsservice.hxx>
#include <tools/stream.hxx>
#include <vcl/graph.hxx>
#include <vcl/graphicfilter.hxx>
#include <tools/urlobj.hxx>
#include <vcl/opengl/OpenGLHelper.hxx>
#include <cassert>
using namespace com::sun::star;
namespace avmedia { namespace ogl {
OGLPlayer::OGLPlayer()
: Player_BASE(m_aMutex)
, m_pHandle(NULL)
, m_pOGLWindow(NULL)
, m_bIsRendering(false)
{
}
OGLPlayer::~OGLPlayer()
{
osl::MutexGuard aGuard(m_aMutex);
if( m_pHandle )
{
for (size_t i = 0; i < m_pHandle->size && m_pHandle->files[i].buffer; ++i)
{
if (m_pHandle->files[i].type != GLTF_JSON)
{
delete [] m_pHandle->files[i].buffer;
}
}
m_aContext.makeCurrent();
gltf_renderer_release(m_pHandle);
}
}
static bool lcl_LoadFile( glTFFile* io_pFile, const OUString& rURL)
{
SvFileStream aStream( rURL, STREAM_READ );
if( !aStream.IsOpen() )
return false;
const sal_Int64 nBytes = aStream.remainingSize();
char* pBuffer = new char[nBytes];
aStream.Read( pBuffer, nBytes );
aStream.Close();
io_pFile->buffer = pBuffer;
io_pFile->size = nBytes;
return true;
}
bool OGLPlayer::create( const OUString& rURL )
{
osl::MutexGuard aGuard(m_aMutex);
m_sURL = rURL;
// Load *.json file and init renderer
glTFFile aJsonFile;
aJsonFile.type = GLTF_JSON;
OString sFileName = OUStringToOString(INetURLObject(m_sURL).GetLastName(),RTL_TEXTENCODING_UTF8);
aJsonFile.filename = (char*)sFileName.getStr();
if( !lcl_LoadFile(&aJsonFile, m_sURL) )
{
SAL_WARN("avmedia.opengl", "Can't load *.json file: " + sFileName);
return false;
}
m_pHandle = gltf_renderer_init(&aJsonFile);
delete [] aJsonFile.buffer;
if( !m_pHandle || !m_pHandle->files )
{
SAL_WARN("avmedia.opengl", "gltf_renderer_init returned an invalid glTFHandle");
return false;
}
// Load external resources
for( size_t i = 0; i < m_pHandle->size; ++i )
{
glTFFile& rFile = m_pHandle->files[i];
if( rFile.filename )
{
const OUString sFilesURL =
INetURLObject::GetAbsURL(m_sURL,OStringToOUString(OString(rFile.filename),RTL_TEXTENCODING_UTF8));
if( rFile.type == GLTF_IMAGE )
{
// Load images as bitmaps
GraphicFilter aFilter;
Graphic aGraphic;
if( aFilter.ImportGraphic(aGraphic, INetURLObject(sFilesURL)) != GRFILTER_OK )
{
rFile.buffer = 0;
rFile.imagewidth = 0;
rFile.imageheight = 0;
SAL_WARN("avmedia.opengl", "Can't load texture file: " + sFilesURL);
return false;
}
BitmapEx aBitmapEx = aGraphic.GetBitmapEx();
aBitmapEx.Mirror(BMP_MIRROR_VERT);
rFile.buffer = (char*)OpenGLHelper::ConvertBitmapExToRGBABuffer(aBitmapEx);
rFile.imagewidth = aBitmapEx.GetSizePixel().Width();
rFile.imageheight = aBitmapEx.GetSizePixel().Height();
}
else if( rFile.type == GLTF_BINARY || rFile.type == GLTF_GLSL )
{
if( !lcl_LoadFile(&rFile, sFilesURL) )
{
rFile.buffer = 0;
rFile.size = 0;
SAL_WARN("avmedia.opengl", "Can't load glTF file: " + sFilesURL);
return false;
}
}
}
}
// Set timer
m_aTimer.SetTimeout(10);
m_aTimer.SetTimeoutHdl(LINK(this,OGLPlayer,TimerHandler));
return true;
}
void SAL_CALL OGLPlayer::start() throw ( uno::RuntimeException, std::exception )
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_pHandle);
// gltf_animation_start play animation from the time 0.0,
// but OGLPlayer::start used as play from that time where it was stopped before
double fTime = gltf_animation_get_time(m_pHandle);
gltf_animation_start(m_pHandle);
gltf_animation_set_time(m_pHandle, fTime);
m_aTimer.Start();
m_bIsRendering = true;
}
void SAL_CALL OGLPlayer::stop() throw ( uno::RuntimeException, std::exception )
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_pHandle);
m_aTimer.Stop();
gltf_animation_stop(m_pHandle);
m_bIsRendering = false;
}
sal_Bool SAL_CALL OGLPlayer::isPlaying() throw ( uno::RuntimeException, std::exception )
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_pHandle);
// Here isPlaying means model is rendered in the window and
// able to interact with the user (e.g. moving camera)
if( getDuration() > 0.0 )
return gltf_animation_is_playing(m_pHandle);
else
return m_bIsRendering;
}
double SAL_CALL OGLPlayer::getDuration() throw ( uno::RuntimeException, std::exception )
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_pHandle);
return gltf_animation_get_duration(m_pHandle);
}
void SAL_CALL OGLPlayer::setMediaTime( double fTime ) throw ( uno::RuntimeException, std::exception )
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_pHandle);
gltf_animation_set_time(m_pHandle, fTime);
}
double SAL_CALL OGLPlayer::getMediaTime() throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_pHandle);
return gltf_animation_get_time(m_pHandle);
}
void SAL_CALL OGLPlayer::setPlaybackLoop( sal_Bool bSet ) throw ( uno::RuntimeException, std::exception )
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_pHandle);
gltf_animation_set_looping(m_pHandle, (int)bSet);
}
sal_Bool SAL_CALL OGLPlayer::isPlaybackLoop() throw ( uno::RuntimeException, std::exception )
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_pHandle);
return gltf_animation_get_looping(m_pHandle) != 0;
}
void SAL_CALL OGLPlayer::setVolumeDB( sal_Int16 /*nVolumDB*/ ) throw ( uno::RuntimeException, std::exception )
{
// OpenGL models have no sound.
}
sal_Int16 SAL_CALL OGLPlayer::getVolumeDB() throw ( uno::RuntimeException, std::exception )
{
// OpenGL models have no sound.
return 0;
}
void SAL_CALL OGLPlayer::setMute( sal_Bool /*bSet*/ ) throw ( uno::RuntimeException, std::exception )
{
// OpenGL models have no sound.
}
sal_Bool SAL_CALL OGLPlayer::isMute() throw ( uno::RuntimeException, std::exception )
{
// OpenGL models have no sound.
return false;
}
awt::Size SAL_CALL OGLPlayer::getPreferredPlayerWindowSize() throw ( uno::RuntimeException, std::exception )
{
return awt::Size( 480, 360 );
}
uno::Reference< media::XPlayerWindow > SAL_CALL OGLPlayer::createPlayerWindow( const uno::Sequence< uno::Any >& rArguments )
throw ( uno::RuntimeException, std::exception )
{
osl::MutexGuard aGuard( m_aMutex );
assert( rArguments.getLength() >= 3 );
assert(m_pHandle);
sal_IntPtr pIntPtr = 0;
rArguments[ 2 ] >>= pIntPtr;
SystemChildWindow *pChildWindow = reinterpret_cast< SystemChildWindow* >( pIntPtr );
if( !pChildWindow )
{
SAL_WARN("avmedia.opengl", "Failed to get the SystemChildWindow for rendering!");
return uno::Reference< media::XPlayerWindow >();
}
assert(pChildWindow->GetParent());
if( !m_aContext.init(pChildWindow) )
{
SAL_WARN("avmedia.opengl", "Context initialization failed");
return uno::Reference< media::XPlayerWindow >();
}
Size aSize = pChildWindow->GetSizePixel();
m_aContext.setWinSize(aSize);
m_pHandle->viewport.x = 0;
m_pHandle->viewport.y = 0;
m_pHandle->viewport.width = aSize.Width();
m_pHandle->viewport.height = aSize.Height();
// TODO: In libgltf different return values are defined (for different errors)
// but these error codes are not part of the library interface
int nRet = gltf_renderer_set_content(m_pHandle);
if( nRet != 0 )
{
SAL_WARN("avmedia.opengl", "Error occured while parsing *.json file! Error code: " << nRet);
return uno::Reference< media::XPlayerWindow >();
}
// The background color is white by default, but we need to separate the
// OpenGL window from the main window so set background color to grey
glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
m_pOGLWindow = new OGLWindow(*m_pHandle, m_aContext, *pChildWindow->GetParent());
return uno::Reference< media::XPlayerWindow >( m_pOGLWindow );
}
uno::Reference< media::XFrameGrabber > SAL_CALL OGLPlayer::createFrameGrabber()
throw ( uno::RuntimeException, std::exception )
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_pHandle);
if( !m_aContext.init() )
{
SAL_WARN("avmedia.opengl", "Offscreen context initialization failed");
return uno::Reference< media::XFrameGrabber >();
}
m_pHandle->viewport.x = 0;
m_pHandle->viewport.y = 0;
m_pHandle->viewport.width = getPreferredPlayerWindowSize().Width;
m_pHandle->viewport.height = getPreferredPlayerWindowSize().Height;
int nRet = gltf_renderer_set_content(m_pHandle);
if( nRet != 0 )
{
SAL_WARN("avmedia.opengl", "Error occured while parsing *.json file! Error code: " << nRet);
return uno::Reference< media::XFrameGrabber >();
}
glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
OGLFrameGrabber *pFrameGrabber = new OGLFrameGrabber( *m_pHandle );
return uno::Reference< media::XFrameGrabber >( pFrameGrabber );
}
OUString SAL_CALL OGLPlayer::getImplementationName()
throw ( ::com::sun::star::uno::RuntimeException, std::exception )
{
return OUString("com.sun.star.comp.avmedia.Player_OpenGL");
}
sal_Bool SAL_CALL OGLPlayer::supportsService( const OUString& rServiceName )
throw ( uno::RuntimeException, std::exception )
{
return cppu::supportsService(this, rServiceName);
}
uno::Sequence< OUString > SAL_CALL OGLPlayer::getSupportedServiceNames()
throw ( uno::RuntimeException, std::exception )
{
uno::Sequence< OUString > aRet(1);
aRet[0] = OUString("com.sun.star.media.Player_OpenGL");
return aRet;
}
IMPL_LINK(OGLPlayer,TimerHandler,Timer*,pTimer)
{
if (pTimer == &m_aTimer)
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_pOGLWindow);
m_pOGLWindow->update();
}
return 0;
}
} // namespace ogl
} // namespace avmedia
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|