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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* 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 <sal/config.h>
#include <QtCore/QUrl>
#include <QtMultimedia/QAudioOutput>
#include <QtMultimedia/QMediaMetaData>
#include <QtMultimediaWidgets/QVideoWidget>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLayout>
#include <cppuhelper/supportsservice.hxx>
#include <sal/log.hxx>
#include <rtl/string.hxx>
#include <tools/link.hxx>
#include <vcl/BitmapTools.hxx>
#include <vcl/graph.hxx>
#include <vcl/qt/QtUtils.hxx>
#include <vcl/svapp.hxx>
#include <vcl/syschild.hxx>
#include <vcl/sysdata.hxx>
#include <vcl/timer.hxx>
#include <gstwindow.hxx>
#include "QtFrameGrabber.hxx"
#include "QtPlayer.hxx"
#include <QtPlayer.moc>
using namespace ::com::sun::star;
namespace avmedia::qt
{
QtPlayer::QtPlayer()
: QtPlayer_BASE(m_aMutex)
, m_lListener(m_aMutex)
, m_pMediaWidgetParent(nullptr)
{
}
bool QtPlayer::create(const OUString& rURL)
{
const QUrl aQUrl(toQString(rURL));
if (!aQUrl.isValid() || !aQUrl.isLocalFile())
return false;
m_xMediaPlayer = std::make_unique<QMediaPlayer>();
m_xMediaPlayer->setSource(aQUrl);
QAudioOutput* pAudioOutput = new QAudioOutput;
pAudioOutput->setVolume(50);
m_xMediaPlayer->setAudioOutput(pAudioOutput);
return true;
}
void SAL_CALL QtPlayer::start()
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_xMediaPlayer);
m_xMediaPlayer->play();
}
void SAL_CALL QtPlayer::stop()
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_xMediaPlayer);
// don't use QMediaPlayer::stop because XPlayer::stop should leave the position unchanged
m_xMediaPlayer->pause();
}
sal_Bool SAL_CALL QtPlayer::isPlaying()
{
osl::MutexGuard aGuard(m_aMutex);
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
assert(m_xMediaPlayer);
return m_xMediaPlayer->isPlaying();
#else
return false;
#endif
}
double SAL_CALL QtPlayer::getDuration()
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_xMediaPlayer);
return m_xMediaPlayer->duration() / 1000.0;
}
void SAL_CALL QtPlayer::setMediaTime(double fTime)
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_xMediaPlayer);
m_xMediaPlayer->setPosition(fTime * 1000);
}
double SAL_CALL QtPlayer::getMediaTime()
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_xMediaPlayer);
return m_xMediaPlayer->position() / 1000.0;
}
void SAL_CALL QtPlayer::setPlaybackLoop(sal_Bool bSet)
{
assert(m_xMediaPlayer);
const int nLoops = bSet ? QMediaPlayer::Infinite : QMediaPlayer::Once;
m_xMediaPlayer->setLoops(nLoops);
}
sal_Bool SAL_CALL QtPlayer::isPlaybackLoop()
{
assert(m_xMediaPlayer);
return m_xMediaPlayer->loops() == QMediaPlayer::Infinite;
}
void SAL_CALL QtPlayer::setVolumeDB(sal_Int16 nVolumeDB)
{
osl::MutexGuard aGuard(m_aMutex);
// range is -40 for silence to 0 for full volume
const sal_Int16 nVolume = std::clamp<sal_Int16>(nVolumeDB, -40, 0);
double fValue = (nVolume + 40) / 40.0;
assert(m_xMediaPlayer);
QAudioOutput* pAudioOutput = m_xMediaPlayer->audioOutput();
assert(pAudioOutput);
pAudioOutput->setVolume(fValue);
}
sal_Int16 SAL_CALL QtPlayer::getVolumeDB()
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_xMediaPlayer);
QAudioOutput* pAudioOutput = m_xMediaPlayer->audioOutput();
assert(pAudioOutput);
double fVolume = pAudioOutput->volume();
return (fVolume * 40) - 40;
}
void SAL_CALL QtPlayer::setMute(sal_Bool bSet)
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_xMediaPlayer);
QAudioOutput* pAudioOutput = m_xMediaPlayer->audioOutput();
assert(pAudioOutput);
pAudioOutput->setMuted(bSet);
}
sal_Bool SAL_CALL QtPlayer::isMute()
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_xMediaPlayer);
QAudioOutput* pAudioOutput = m_xMediaPlayer->audioOutput();
assert(pAudioOutput);
return pAudioOutput->isMuted();
}
awt::Size SAL_CALL QtPlayer::getPreferredPlayerWindowSize()
{
osl::MutexGuard aGuard(m_aMutex);
assert(m_xMediaPlayer);
const QMediaMetaData aMetaData = m_xMediaPlayer->metaData();
const QVariant aResolutionVariant = aMetaData.value(QMediaMetaData::Resolution);
if (aResolutionVariant.canConvert<QSize>())
{
const QSize aResolution = aResolutionVariant.value<QSize>();
return awt::Size(aResolution.width(), aResolution.height());
}
return awt::Size(0, 0);
}
uno::Reference<::media::XPlayerWindow>
SAL_CALL QtPlayer::createPlayerWindow(const uno::Sequence<uno::Any>& rArguments)
{
osl::MutexGuard aGuard(m_aMutex);
if (rArguments.getLength() > 1)
rArguments[1] >>= m_aPlayerWidgetRect;
if (rArguments.getLength() <= 2)
{
uno::Reference<::media::XPlayerWindow> xRet = new ::avmedia::gstreamer::Window;
return xRet;
}
sal_IntPtr pIntPtr = 0;
rArguments[2] >>= pIntPtr;
SystemChildWindow* pParentWindow = reinterpret_cast<SystemChildWindow*>(pIntPtr);
if (!pParentWindow)
return nullptr;
const SystemEnvData* pParentEnvData = pParentWindow->GetSystemData();
if (!pParentEnvData)
return nullptr;
m_pMediaWidgetParent = static_cast<QWidget*>(pParentEnvData->pWidget);
assert(m_pMediaWidgetParent);
// while media is loading, QMediaPlayer::hasVideo doesn't yet return
// whether media actually has video; defer creating audio/video widget
if (m_xMediaPlayer->mediaStatus() == QMediaPlayer::LoadingMedia)
{
connect(m_xMediaPlayer.get(), &QMediaPlayer::mediaStatusChanged, this,
&QtPlayer::createMediaPlayerWidget, Qt::SingleShotConnection);
}
else
{
createMediaPlayerWidget();
}
uno::Reference<::media::XPlayerWindow> xRet = new ::avmedia::gstreamer::Window;
return xRet;
}
uno::Reference<media::XFrameGrabber> SAL_CALL QtPlayer::createFrameGrabber()
{
osl::MutexGuard aGuard(m_aMutex);
rtl::Reference<QtFrameGrabber> xFrameGrabber = new QtFrameGrabber(m_xMediaPlayer->source());
return xFrameGrabber;
}
void SAL_CALL
QtPlayer::addPlayerListener(const css::uno::Reference<css::media::XPlayerListener>& rListener)
{
m_lListener.addInterface(cppu::UnoType<css::media::XPlayerListener>::get(), rListener);
if (isReadyToPlay())
{
css::lang::EventObject aEvent;
aEvent.Source = getXWeak();
rListener->preferredPlayerWindowSizeAvailable(aEvent);
}
else
{
installNotify();
}
}
void SAL_CALL
QtPlayer::removePlayerListener(const css::uno::Reference<css::media::XPlayerListener>& rListener)
{
m_lListener.removeInterface(cppu::UnoType<css::media::XPlayerListener>::get(), rListener);
}
OUString SAL_CALL QtPlayer::getImplementationName()
{
return u"com.sun.star.comp.avmedia.Player_Qt"_ustr;
}
sal_Bool SAL_CALL QtPlayer::supportsService(const OUString& rServiceName)
{
return cppu::supportsService(this, rServiceName);
}
uno::Sequence<OUString> SAL_CALL QtPlayer::getSupportedServiceNames()
{
return { u"com.sun.star.media.Player_Qt"_ustr };
}
void SAL_CALL QtPlayer::disposing()
{
osl::MutexGuard aGuard(m_aMutex);
stop();
QtPlayer_BASE::disposing();
}
QtPlayer::~QtPlayer()
{
// ensure output objects get deleted as QMediaPlayer doesn't take ownership of them
std::unique_ptr<QObject> xVideoWidget(m_xMediaPlayer->videoOutput());
std::unique_ptr<QAudioOutput> xAudioOutput(m_xMediaPlayer->audioOutput());
m_xMediaPlayer.reset();
}
bool QtPlayer::isReadyToPlay()
{
assert(m_xMediaPlayer);
QMediaPlayer::MediaStatus eStatus = m_xMediaPlayer->mediaStatus();
return eStatus == QMediaPlayer::BufferingMedia || eStatus == QMediaPlayer::BufferedMedia
|| eStatus == QMediaPlayer::LoadedMedia || eStatus == QMediaPlayer::EndOfMedia;
}
void QtPlayer::installNotify()
{
connect(m_xMediaPlayer.get(), &QMediaPlayer::mediaStatusChanged, this,
&QtPlayer::notifyIfReady);
}
void QtPlayer::uninstallNotify()
{
disconnect(m_xMediaPlayer.get(), &QMediaPlayer::mediaStatusChanged, this,
&QtPlayer::notifyIfReady);
}
void QtPlayer::notifyIfReady(QMediaPlayer::MediaStatus)
{
if (isReadyToPlay())
{
rtl::Reference<QtPlayer> xThis(this);
xThis->notifyListeners();
xThis->uninstallNotify();
}
}
void QtPlayer::createMediaPlayerWidget()
{
assert(m_xMediaPlayer);
assert(m_xMediaPlayer->mediaStatus() != QMediaPlayer::LoadingMedia
&& "Media is still loading, detecting video availability not possible.");
assert(m_pMediaWidgetParent && "Parent for media widget not set");
// if media contains video, show the video output,
// otherwise show an audio icon as a placeholder
QWidget* pWidget;
if (m_xMediaPlayer->hasVideo())
{
QVideoWidget* pVideoWidget = new QVideoWidget(m_pMediaWidgetParent);
pVideoWidget->setAspectRatioMode(Qt::IgnoreAspectRatio);
assert(!m_xMediaPlayer->videoOutput() && "Video output already set.");
m_xMediaPlayer->setVideoOutput(pVideoWidget);
pWidget = pVideoWidget;
}
else
{
QPixmap aAudioPixmap = loadQPixmapIcon(u"avmedia/res/avaudiologo.png"_ustr);
aAudioPixmap
= aAudioPixmap.scaled(QSize(m_aPlayerWidgetRect.Width, m_aPlayerWidgetRect.Height));
QLabel* pLabel = new QLabel;
pLabel->setPixmap(aAudioPixmap);
pWidget = pLabel;
}
assert(pWidget);
pWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// retrieve the layout (which is set in the QtObjectWidget ctor)
QLayout* pLayout = m_pMediaWidgetParent->layout();
assert(pLayout);
assert(pLayout->count() == 0 && "Layout already has a widget set");
pLayout->addWidget(pWidget);
}
void QtPlayer::notifyListeners()
{
comphelper::OInterfaceContainerHelper2* pContainer
= m_lListener.getContainer(cppu::UnoType<css::media::XPlayerListener>::get());
if (!pContainer)
return;
css::lang::EventObject aEvent;
aEvent.Source = getXWeak();
comphelper::OInterfaceIteratorHelper2 pIterator(*pContainer);
while (pIterator.hasMoreElements())
{
css::uno::Reference<css::media::XPlayerListener> xListener(
static_cast<css::media::XPlayerListener*>(pIterator.next()));
xListener->preferredPlayerWindowSizeAvailable(aEvent);
}
}
} // namespace
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
|