1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include "oglplayer.hxx"
11 #include "oglframegrabber.hxx"
12 #include "oglwindow.hxx"
14 #include <cppuhelper/supportsservice.hxx>
15 #include <tools/stream.hxx>
16 #include <vcl/graph.hxx>
17 #include <vcl/graphicfilter.hxx>
18 #include <tools/urlobj.hxx>
19 #include <vcl/opengl/OpenGLHelper.hxx>
23 using namespace com::sun::star
;
24 using namespace libgltf
;
26 namespace avmedia
{ namespace ogl
{
28 OGLPlayer::OGLPlayer()
29 : Player_BASE(m_aMutex
)
31 , m_xContext(OpenGLContext::Create())
33 , m_bIsRendering(false)
37 OGLPlayer::~OGLPlayer()
39 osl::MutexGuard
aGuard(m_aMutex
);
42 m_xContext
->makeCurrent();
43 gltf_renderer_release(m_pHandle
);
44 m_xContext
->dispose();
49 static bool lcl_LoadFile( glTFFile
* io_pFile
, const OUString
& rURL
)
51 SvFileStream
aStream( rURL
, StreamMode::READ
);
52 if( !aStream
.IsOpen() )
55 const sal_Int64 nBytes
= aStream
.remainingSize();
56 char* pBuffer
= new char[nBytes
];
57 aStream
.Read( pBuffer
, nBytes
);
60 io_pFile
->buffer
= pBuffer
;
61 io_pFile
->size
= nBytes
;
66 bool OGLPlayer::create( const OUString
& rURL
)
68 osl::MutexGuard
aGuard(m_aMutex
);
72 // Convert URL to a system path
73 const INetURLObject
aURLObj(m_sURL
);
74 const std::string sFilePath
= OUStringToOString( aURLObj
.getFSysPath(INetURLObject::FSYS_DETECT
), RTL_TEXTENCODING_UTF8
).getStr();
76 // Load *.json file and init renderer
77 m_pHandle
= gltf_renderer_init(sFilePath
, m_vInputFiles
);
81 SAL_WARN("avmedia.opengl", "gltf_renderer_init returned an invalid glTFHandle");
85 // Load external resources
86 for( size_t i
= 0; i
< m_vInputFiles
.size(); ++i
)
88 glTFFile
& rFile
= m_vInputFiles
[i
];
89 if( !rFile
.filename
.empty() )
91 const OUString sFilesURL
=
92 INetURLObject::GetAbsURL(m_sURL
,OStringToOUString(OString(rFile
.filename
.c_str()),RTL_TEXTENCODING_UTF8
));
93 if( rFile
.type
== GLTF_IMAGE
)
95 // Load images as bitmaps
96 GraphicFilter aFilter
;
98 if( aFilter
.ImportGraphic(aGraphic
, INetURLObject(sFilesURL
)) != GRFILTER_OK
)
101 rFile
.imagewidth
= 0;
102 rFile
.imageheight
= 0;
103 SAL_WARN("avmedia.opengl", "Can't load texture file: " + sFilesURL
);
106 BitmapEx aBitmapEx
= aGraphic
.GetBitmapEx();
107 rFile
.buffer
= new char[4 * aBitmapEx
.GetSizePixel().Width() * aBitmapEx
.GetSizePixel().Height()];
108 OpenGLHelper::ConvertBitmapExToRGBATextureBuffer(aBitmapEx
, reinterpret_cast<sal_uInt8
*>(rFile
.buffer
), true);
109 rFile
.imagewidth
= aBitmapEx
.GetSizePixel().Width();
110 rFile
.imageheight
= aBitmapEx
.GetSizePixel().Height();
112 else if( rFile
.type
== GLTF_BINARY
|| rFile
.type
== GLTF_GLSL
)
114 if( !lcl_LoadFile(&rFile
, sFilesURL
) )
118 SAL_WARN("avmedia.opengl", "Can't load glTF file: " + sFilesURL
);
126 m_aTimer
.SetTimeout(8); // is 125fps enough for anyone ?
127 m_aTimer
.SetPriority(SchedulerPriority::LOW
);
128 m_aTimer
.SetTimeoutHdl(LINK(this,OGLPlayer
,TimerHandler
));
133 void OGLPlayer::releaseInputFiles()
135 for (size_t i
= 0; i
< m_vInputFiles
.size() && m_vInputFiles
[i
].buffer
; ++i
)
137 delete [] m_vInputFiles
[i
].buffer
;
138 m_vInputFiles
[i
].buffer
= 0;
140 m_vInputFiles
.clear();
143 void SAL_CALL
OGLPlayer::start() throw ( uno::RuntimeException
, std::exception
)
145 osl::MutexGuard
aGuard(m_aMutex
);
151 gltf_animation_resume(m_pHandle
);
153 m_bIsRendering
= true;
156 void SAL_CALL
OGLPlayer::stop() throw ( uno::RuntimeException
, std::exception
)
158 osl::MutexGuard
aGuard(m_aMutex
);
161 gltf_animation_stop(m_pHandle
);
162 m_bIsRendering
= false;
165 sal_Bool SAL_CALL
OGLPlayer::isPlaying() throw ( uno::RuntimeException
, std::exception
)
167 osl::MutexGuard
aGuard(m_aMutex
);
169 // Here isPlaying means model is rendered in the window and
170 // able to interact with the user (e.g. moving camera)
171 if( getDuration() > 0.0 )
172 return gltf_animation_is_playing(m_pHandle
);
174 return m_bIsRendering
;
177 double SAL_CALL
OGLPlayer::getDuration() throw ( uno::RuntimeException
, std::exception
)
179 osl::MutexGuard
aGuard(m_aMutex
);
181 return gltf_animation_get_duration(m_pHandle
);
184 void SAL_CALL
OGLPlayer::setMediaTime( double fTime
) throw ( uno::RuntimeException
, std::exception
)
186 osl::MutexGuard
aGuard(m_aMutex
);
188 gltf_animation_set_time(m_pHandle
, fTime
);
191 double SAL_CALL
OGLPlayer::getMediaTime() throw ( ::com::sun::star::uno::RuntimeException
, std::exception
)
193 osl::MutexGuard
aGuard(m_aMutex
);
195 return gltf_animation_get_time(m_pHandle
);
198 void SAL_CALL
OGLPlayer::setPlaybackLoop( sal_Bool bSet
) throw ( uno::RuntimeException
, std::exception
)
200 osl::MutexGuard
aGuard(m_aMutex
);
202 gltf_animation_set_looping(m_pHandle
, bSet
);
205 sal_Bool SAL_CALL
OGLPlayer::isPlaybackLoop() throw ( uno::RuntimeException
, std::exception
)
207 osl::MutexGuard
aGuard(m_aMutex
);
209 return gltf_animation_get_looping(m_pHandle
);
212 void SAL_CALL
OGLPlayer::setVolumeDB( sal_Int16
/*nVolumDB*/ ) throw ( uno::RuntimeException
, std::exception
)
214 // OpenGL models have no sound.
217 sal_Int16 SAL_CALL
OGLPlayer::getVolumeDB() throw ( uno::RuntimeException
, std::exception
)
219 // OpenGL models have no sound.
223 void SAL_CALL
OGLPlayer::setMute( sal_Bool
/*bSet*/ ) throw ( uno::RuntimeException
, std::exception
)
225 // OpenGL models have no sound.
228 sal_Bool SAL_CALL
OGLPlayer::isMute() throw ( uno::RuntimeException
, std::exception
)
230 // OpenGL models have no sound.
234 awt::Size SAL_CALL
OGLPlayer::getPreferredPlayerWindowSize() throw ( uno::RuntimeException
, std::exception
)
236 return awt::Size( 480, 360 );
239 static bool lcl_CheckOpenGLRequirements()
241 return OpenGLHelper::getGLVersion() >= 3.0;
244 uno::Reference
< media::XPlayerWindow
> SAL_CALL
OGLPlayer::createPlayerWindow( const uno::Sequence
< uno::Any
>& rArguments
)
245 throw ( uno::RuntimeException
, std::exception
)
247 osl::MutexGuard
aGuard( m_aMutex
);
249 assert( rArguments
.getLength() >= 3 );
252 sal_IntPtr pIntPtr
= 0;
253 rArguments
[ 2 ] >>= pIntPtr
;
254 SystemChildWindow
*pChildWindow
= reinterpret_cast< SystemChildWindow
* >( pIntPtr
);
258 SAL_WARN("avmedia.opengl", "Failed to get the SystemChildWindow for rendering!");
259 return uno::Reference
< media::XPlayerWindow
>();
261 assert(pChildWindow
->GetParent());
263 if( !m_xContext
->init(pChildWindow
) )
265 SAL_WARN("avmedia.opengl", "Context initialization failed");
266 return uno::Reference
< media::XPlayerWindow
>();
269 if( !m_xContext
->supportMultiSampling() )
271 SAL_WARN("avmedia.opengl", "Context does not support multisampling!");
272 return uno::Reference
< media::XPlayerWindow
>();
275 if( !lcl_CheckOpenGLRequirements() )
277 SAL_WARN("avmedia.opengl", "Your platform does not have the minimal OpenGL requiremenets!");
278 return uno::Reference
< media::XPlayerWindow
>();
281 Size aSize
= pChildWindow
->GetSizePixel();
282 m_xContext
->setWinSize(aSize
);
283 m_pHandle
->viewport
.x
= 0;
284 m_pHandle
->viewport
.y
= 0;
285 m_pHandle
->viewport
.width
= aSize
.Width();
286 m_pHandle
->viewport
.height
= aSize
.Height();
288 // TODO: Use the error codes to print a readable error message
289 int nRet
= gltf_renderer_set_content(m_pHandle
, m_vInputFiles
);
293 SAL_WARN("avmedia.opengl", "Error occurred while setting up the scene! Error code: " << nRet
);
294 return uno::Reference
< media::XPlayerWindow
>();
296 // The background color is white by default, but we need to separate the
297 // OpenGL window from the main window so set background color to grey
298 glClearColor(0.5f
, 0.5f
, 0.5f
, 0.5f
);
299 m_pOGLWindow
= new OGLWindow(*m_pHandle
, m_xContext
, *pChildWindow
->GetParent());
300 return uno::Reference
< media::XPlayerWindow
>( m_pOGLWindow
);
303 uno::Reference
< media::XFrameGrabber
> SAL_CALL
OGLPlayer::createFrameGrabber()
304 throw ( uno::RuntimeException
, std::exception
)
306 osl::MutexGuard
aGuard(m_aMutex
);
309 if( !m_xContext
->init() )
311 SAL_WARN("avmedia.opengl", "Offscreen context initialization failed");
312 return uno::Reference
< media::XFrameGrabber
>();
315 if( !m_xContext
->supportMultiSampling() )
317 SAL_WARN("avmedia.opengl", "Context does not support multisampling!");
318 return uno::Reference
< media::XFrameGrabber
>();
321 if( !lcl_CheckOpenGLRequirements() )
323 SAL_WARN("avmedia.opengl", "Your platform does not have the minimal OpenGL requiremenets!");
324 return uno::Reference
< media::XFrameGrabber
>();
327 m_pHandle
->viewport
.x
= 0;
328 m_pHandle
->viewport
.y
= 0;
329 m_pHandle
->viewport
.width
= getPreferredPlayerWindowSize().Width
;
330 m_pHandle
->viewport
.height
= getPreferredPlayerWindowSize().Height
;
332 int nRet
= gltf_renderer_set_content(m_pHandle
, m_vInputFiles
);
336 SAL_WARN("avmedia.opengl", "Error occurred while setting up the scene! Error code: " << nRet
);
337 return uno::Reference
< media::XFrameGrabber
>();
339 glClearColor(0.5f
, 0.5f
, 0.5f
, 0.5f
);
340 OGLFrameGrabber
*pFrameGrabber
= new OGLFrameGrabber( *m_pHandle
);
341 return uno::Reference
< media::XFrameGrabber
>( pFrameGrabber
);
344 OUString SAL_CALL
OGLPlayer::getImplementationName()
345 throw ( ::com::sun::star::uno::RuntimeException
, std::exception
)
347 return OUString("com.sun.star.comp.avmedia.Player_OpenGL");
350 sal_Bool SAL_CALL
OGLPlayer::supportsService( const OUString
& rServiceName
)
351 throw ( uno::RuntimeException
, std::exception
)
353 return cppu::supportsService(this, rServiceName
);
356 uno::Sequence
< OUString
> SAL_CALL
OGLPlayer::getSupportedServiceNames()
357 throw ( uno::RuntimeException
, std::exception
)
359 uno::Sequence
< OUString
> aRet(1);
360 aRet
[0] = "com.sun.star.media.Player_OpenGL";
364 IMPL_LINK_TYPED(OGLPlayer
,TimerHandler
,Timer
*,pTimer
,void)
366 if (pTimer
== &m_aTimer
)
368 osl::MutexGuard
aGuard(m_aMutex
);
369 assert(m_pOGLWindow
);
370 m_pOGLWindow
->update();
375 } // namespace avmedia
377 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */