LibreOffice Module avmedia (master) 1
mediawindow.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
21#include "mediawindow_impl.hxx"
22#include <mediamisc.hxx>
23#include <bitmaps.hlst>
24#include <strings.hrc>
25#include <tools/urlobj.hxx>
26#include <utility>
27#include <vcl/graph.hxx>
28#include <vcl/svapp.hxx>
29#include <vcl/weld.hxx>
31#include <com/sun/star/awt/Size.hpp>
32#include <com/sun/star/frame/XDispatchHelper.hpp>
33#include <com/sun/star/media/XPlayer.hpp>
34#include <com/sun/star/media/XPlayerNotifier.hpp>
35#include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
36#include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
37#include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
38#include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
39#include <com/sun/star/util/URLTransformer.hpp>
42#include <memory>
43#include <sal/log.hxx>
44#include <o3tl/string_view.hxx>
45
46#define AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME 3.0
47
48using namespace ::com::sun::star;
49
50namespace avmedia {
51
52MediaWindow::MediaWindow( vcl::Window* parent, bool bInternalMediaControl ) :
53 mpImpl( VclPtr<priv::MediaWindowImpl>::Create( parent, this, bInternalMediaControl ) )
54{
55 mpImpl->Show();
56}
57
58
60{
61 mpImpl.disposeAndClear();
62}
63
64
65void MediaWindow::setURL( const OUString& rURL, const OUString& rReferer )
66{
67 mpImpl->setURL( rURL, OUString(), rReferer );
68}
69
70
71const OUString& MediaWindow::getURL() const
72{
73 return mpImpl->getURL();
74}
75
76
78{
79 return mpImpl->isValid();
80}
81
82
84{
85}
86
87
89{
90}
91
92
94{
95}
96
97
99{
100}
101
102
104{
105}
106
108{
109}
110
111
113{
114 return 0;
115}
116
117
119{
120 return 0;
121}
122
123
125{
126}
127
128
130{
131 return mpImpl->getPreferredSize();
132}
133
134
136{
137 mpImpl->setPosSize( rNewRect );
138}
139
140
142{
143 mpImpl->setPointer( nPointer );
144}
145
146
148{
149 return mpImpl->start();
150}
151
153{
154 mpImpl->updateMediaItem( rItem );
155}
156
158{
159 mpImpl->executeMediaItem( rItem );
160}
161
163{
164 mpImpl->Show();
165}
166
168{
169 mpImpl->Hide();
170}
171
173{
174 return mpImpl->IsVisible();
175}
176
178{
179 return mpImpl.get();
180}
181
182
184{
185 return {{"Advanced Audio Coding", "aac"},
186 {"AIF Audio", "aif;aiff"},
187 {"Advanced Systems Format", "asf;wma;wmv"},
188 {"AU Audio", "au"},
189 {"AC3 Audio", "ac3"},
190 {"AVI", "avi"},
191 {"CD Audio", "cda"},
192 {"Digital Video", "dv"},
193 {"FLAC Audio", "flac"},
194 {"Flash Video", "flv"},
195 {"Matroska Media", "mkv"},
196 {"MIDI Audio", "mid;midi"},
197 {"MPEG Audio", "mp2;mp3;mpa;m4a"},
198 {"MPEG Video", "mpg;mpeg;mpv;mp4;m4v"},
199 {"Ogg Audio", "ogg;oga;opus"},
200 {"Ogg Video", "ogv;ogx"},
201 {"Real Audio", "ra"},
202 {"Real Media", "rm"},
203 {"RMI MIDI Audio", "rmi"},
204 {"SND (SouND) Audio", "snd"},
205 {"Quicktime Video", "mov"},
206 {"Vivo Video", "viv"},
207 {"WAVE Audio", "wav"},
208 {"WebM Video", "webm"},
209 {"Windows Media Audio", "wma"},
210 {"Windows Media Video", "wmv"}};
211}
212
213
214bool MediaWindow::executeMediaURLDialog(weld::Window* pParent, OUString& rURL, bool *const o_pbLink)
215{
216 ::sfx2::FileDialogHelper aDlg(o_pbLink != nullptr
217 ? ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW
218 : ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE,
219 FileDialogFlags::NONE, pParent);
220 static const char aWildcard[] = "*.";
222 static const char aSeparator[] = ";";
223 OUStringBuffer aAllTypes;
224
226 aDlg.SetTitle( AvmResId( o_pbLink != nullptr
227 ? AVMEDIA_STR_INSERTMEDIA_DLG : AVMEDIA_STR_OPENMEDIA_DLG ) );
228
229 for( const auto &filter : aFilters )
230 {
231 for( sal_Int32 nIndex = 0; nIndex >= 0; )
232 {
233 if( !aAllTypes.isEmpty() )
234 aAllTypes.append(aSeparator);
235
236 aAllTypes.append(OUString::Concat(aWildcard) + o3tl::getToken(filter.second, 0, ';', nIndex ));
237 }
238 }
239
240 // add filter for all media types
241 aDlg.AddFilter( AvmResId( AVMEDIA_STR_ALL_MEDIAFILES ), aAllTypes.makeStringAndClear() );
242
243 for( const auto &filter : aFilters )
244 {
245 OUStringBuffer aTypes;
246
247 for( sal_Int32 nIndex = 0; nIndex >= 0; )
248 {
249 if( !aTypes.isEmpty() )
250 aTypes.append(aSeparator);
251
252 aTypes.append(OUString::Concat(aWildcard) + o3tl::getToken(filter.second, 0, ';', nIndex ));
253 }
254
255 // add single filters
256 aDlg.AddFilter( filter.first, aTypes.makeStringAndClear() );
257 }
258
259 // add filter for all types
260 aDlg.AddFilter( AvmResId( AVMEDIA_STR_ALL_FILES ), "*.*" );
261
262 uno::Reference<ui::dialogs::XFilePicker3> const xFP(aDlg.GetFilePicker());
263 uno::Reference<ui::dialogs::XFilePickerControlAccess> const xCtrlAcc(xFP,
264 uno::UNO_QUERY_THROW);
265 if (o_pbLink != nullptr)
266 {
267 // for video link should be the default
268 xCtrlAcc->setValue(
269 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK, 0,
270 uno::Any(true) );
271 // disabled for now: TODO: preview?
272 xCtrlAcc->enableControl(
273 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_PREVIEW,
274 false);
275 }
276
277 if( aDlg.Execute() == ERRCODE_NONE )
278 {
279 const INetURLObject aURL( aDlg.GetPath() );
281
282 if (o_pbLink != nullptr)
283 {
284 uno::Any const any = xCtrlAcc->getValue(
285 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK, 0);
286 if (!(any >>= *o_pbLink))
287 {
288 SAL_WARN("avmedia", "invalid link property");
289 *o_pbLink = true;
290 }
291 }
292 }
293 else if( !rURL.isEmpty() )
294 rURL.clear();
295
296 return !rURL.isEmpty();
297}
298
300{
301 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(pParent,
302 VclMessageType::Warning, VclButtonsType::Ok, AvmResId(AVMEDIA_STR_ERR_URL)));
303 xBox->run();
304}
305
306bool MediaWindow::isMediaURL(std::u16string_view rURL, const OUString& rReferer, bool bDeep, rtl::Reference<PlayerListener> xPreferredPixelSizeListener)
307{
308 const INetURLObject aURL( rURL );
309
310 if( aURL.GetProtocol() == INetProtocol::NotValid )
311 return false;
312
313 if (bDeep || xPreferredPixelSizeListener)
314 {
315 try
316 {
317 uno::Reference< media::XPlayer > xPlayer( priv::MediaWindowImpl::createPlayer(
319 rReferer, nullptr ) );
320
321 if( xPlayer.is() )
322 {
323 if (xPreferredPixelSizeListener)
324 {
325 uno::Reference<media::XPlayerNotifier> xPlayerNotifier(xPlayer, css::uno::UNO_QUERY);
326 if (xPlayerNotifier)
327 {
328 // wait until it's possible to query this to get a sensible answer
329 xPreferredPixelSizeListener->startListening(xPlayerNotifier);
330 }
331 else
332 {
333 // assume the size is possible to query immediately
334 xPreferredPixelSizeListener->callPlayerWindowSizeAvailable(xPlayer);
335 }
336 }
337 return true;
338 }
339 }
340 catch( ... )
341 {
342 }
343 }
344 else
345 {
347 const OUString aExt( aURL.getExtension() );
348
349 for( const auto &filter : aFilters )
350 {
351 for( sal_Int32 nIndex = 0; nIndex >= 0; )
352 {
353 if( aExt.equalsIgnoreAsciiCase( o3tl::getToken(filter.second, 0, ';', nIndex ) ) )
354 return true;
355 }
356 }
357 }
358
359 return false;
360}
361
362uno::Reference< media::XPlayer > MediaWindow::createPlayer( const OUString& rURL, const OUString& rReferer, const OUString* pMimeType )
363{
364 return priv::MediaWindowImpl::createPlayer( rURL, rReferer, pMimeType );
365}
366
367uno::Reference<graphic::XGraphic>
368MediaWindow::grabFrame(const uno::Reference<media::XPlayer>& xPlayer,
369 const uno::Reference<graphic::XGraphic>& rGraphic)
370{
371 uno::Reference< graphic::XGraphic > xRet;
372 std::optional< Graphic > oGraphic;
373
374 if( xPlayer.is() )
375 {
376 uno::Reference< media::XFrameGrabber > xGrabber( xPlayer->createFrameGrabber() );
377
378 if( xGrabber.is() )
379 {
381
382 if( fMediaTime >= xPlayer->getDuration() )
383 fMediaTime = ( xPlayer->getDuration() * 0.5 );
384
385 xRet = xGrabber->grabFrame( fMediaTime );
386 }
387
388 if( !xRet.is() )
389 {
390 awt::Size aPrefSize( xPlayer->getPreferredPlayerWindowSize() );
391
392 if( !aPrefSize.Width && !aPrefSize.Height )
393 {
394 const BitmapEx aBmpEx(AVMEDIA_BMP_AUDIOLOGO);
395 oGraphic.emplace( aBmpEx );
396 }
397 }
398 }
399
400 if (!xRet.is() && !oGraphic)
401 {
402 const BitmapEx aBmpEx(AVMEDIA_BMP_EMPTYLOGO);
403 oGraphic.emplace( aBmpEx );
404 }
405
406 if (oGraphic)
407 {
408 if (rGraphic)
409 oGraphic.emplace(rGraphic);
410 xRet = oGraphic->GetXGraphic();
411 }
412
413 return xRet;
414}
415
416uno::Reference< graphic::XGraphic > MediaWindow::grabFrame(const OUString& rURL,
417 const OUString& rReferer,
418 const OUString& sMimeType,
419 rtl::Reference<PlayerListener> xPreferredPixelSizeListener)
420{
421 uno::Reference<media::XPlayer> xPlayer(createPlayer(rURL, rReferer, &sMimeType));
422
423 if (xPreferredPixelSizeListener)
424 {
425 uno::Reference<media::XPlayerNotifier> xPlayerNotifier(xPlayer, css::uno::UNO_QUERY);
426 if (xPlayerNotifier)
427 {
428 // set a callback to call when a more sensible result is available, which
429 // might be called immediately if already available
430 xPreferredPixelSizeListener->startListening(xPlayerNotifier);
431 }
432 else
433 {
434 // assume the size is possible to query immediately
435 xPreferredPixelSizeListener->callPlayerWindowSizeAvailable(xPlayer);
436 }
437
438 return nullptr;
439 }
440
441 return grabFrame(xPlayer);
442}
443
444void MediaWindow::dispatchInsertAVMedia(const css::uno::Reference<css::frame::XDispatchProvider>& rDispatchProvider,
445 const css::awt::Size& rSize, const OUString& rURL, bool bLink)
446{
447 util::URL aDispatchURL;
448 aDispatchURL.Complete = ".uno:InsertAVMedia";
449
450 css::uno::Reference<css::util::XURLTransformer> xTrans(css::util::URLTransformer::create(::comphelper::getProcessComponentContext()));
451 xTrans->parseStrict(aDispatchURL);
452
453 css::uno::Reference<css::frame::XDispatch> xDispatch = rDispatchProvider->queryDispatch(aDispatchURL, "", 0);
454 css::uno::Sequence<css::beans::PropertyValue> aArgs(comphelper::InitPropertySequence({
455 { "URL", css::uno::Any(rURL) },
456 { "Size.Width", uno::Any(rSize.Width)},
457 { "Size.Height", uno::Any(rSize.Height)},
458 { "IsLink", css::uno::Any(bLink) },
459 }));
460 xDispatch->dispatch(aDispatchURL, aArgs);
461}
462
463PlayerListener::PlayerListener(std::function<void(const css::uno::Reference<css::media::XPlayer>&)> fn)
465 , m_aFn(std::move(fn))
466{
467}
468
470{
472 PlayerListener_BASE::dispose();
473}
474
475void PlayerListener::startListening(const css::uno::Reference<media::XPlayerNotifier>& rNotifier)
476{
477 osl::MutexGuard aGuard(m_aMutex);
478
479 m_xNotifier = rNotifier;
480 m_xNotifier->addPlayerListener(this);
481}
482
484{
485 osl::MutexGuard aGuard(m_aMutex);
486 if (!m_xNotifier)
487 return;
488 m_xNotifier->removePlayerListener(this);
489 m_xNotifier.clear();
490}
491
492void SAL_CALL PlayerListener::preferredPlayerWindowSizeAvailable(const css::lang::EventObject&)
493{
494 osl::MutexGuard aGuard(m_aMutex);
495
496 css::uno::Reference<media::XPlayer> xPlayer(m_xNotifier, css::uno::UNO_QUERY_THROW);
498
500}
501
502void SAL_CALL PlayerListener::disposing(const css::lang::EventObject&)
503{
504}
505
507{
508}
509
510} // namespace avmedia
511
512/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const Any & any
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
virtual void MouseMove(const MouseEvent &rMEvt)
Definition: mediawindow.cxx:83
virtual void KeyUp(const KeyEvent &rKEvt)
const OUString & getURL() const
Definition: mediawindow.cxx:71
VclPtr< priv::MediaWindowImpl > mpImpl
void setPosSize(const tools::Rectangle &rNewRect)
static void dispatchInsertAVMedia(const css::uno::Reference< css::frame::XDispatchProvider > &, const css::awt::Size &rSize, const OUString &rURL, bool bLink)
static bool executeMediaURLDialog(weld::Window *pParent, OUString &rURL, bool *const o_pbLink)
void executeMediaItem(const MediaItem &rItem)
void setPointer(PointerStyle aPointer)
bool isVisible() const
static css::uno::Reference< css::media::XPlayer > createPlayer(const OUString &rURL, const OUString &rReferer, const OUString *pMimeType=nullptr)
virtual void Command(const CommandEvent &rCEvt)
Size getPreferredSize() const
MediaWindow(vcl::Window *parent, bool bInternalMediaControl)
Definition: mediawindow.cxx:52
bool isValid() const
Definition: mediawindow.cxx:77
static FilterNameVector getMediaFilters()
virtual void KeyInput(const KeyEvent &rKEvt)
Definition: mediawindow.cxx:98
virtual void StartDrag(sal_Int8 nAction, const Point &rPosPixel)
vcl::Window * getWindow() const
void updateMediaItem(MediaItem &rItem) const
virtual void MouseButtonUp(const MouseEvent &rMEvt)
Definition: mediawindow.cxx:93
virtual void MouseButtonDown(const MouseEvent &rMEvt)
Definition: mediawindow.cxx:88
void setURL(const OUString &rURL, const OUString &rReferer)
Definition: mediawindow.cxx:65
static bool isMediaURL(std::u16string_view rURL, const OUString &rReferer, bool bDeep=false, rtl::Reference< PlayerListener > xPreferredPixelSizeListener=nullptr)
static css::uno::Reference< css::graphic::XGraphic > grabFrame(const css::uno::Reference< css::media::XPlayer > &rPlayer, const css::uno::Reference< css::graphic::XGraphic > &rGraphic=nullptr)
static void executeFormatErrorBox(weld::Window *pParent)
virtual sal_Int8 AcceptDrop(const AcceptDropEvent &rEvt)
virtual sal_Int8 ExecuteDrop(const ExecuteDropEvent &rEvt)
virtual void SAL_CALL disposing(const css::lang::EventObject &rSource) override
css::uno::Reference< css::media::XPlayerNotifier > m_xNotifier
Definition: mediawindow.hxx:63
PlayerListener(std::function< void(const css::uno::Reference< css::media::XPlayer > &)> fn)
void callPlayerWindowSizeAvailable(const css::uno::Reference< css::media::XPlayer > &rPlayer)
Definition: mediawindow.hxx:78
virtual ~PlayerListener() override
virtual void SAL_CALL dispose() override
virtual void SAL_CALL preferredPlayerWindowSizeAvailable(const css::lang::EventObject &rSource) override
void startListening(const css::uno::Reference< css::media::XPlayerNotifier > &rNotifier)
static css::uno::Reference< css::media::XPlayer > createPlayer(const OUString &rURL, const OUString &rReferer, const OUString *pMimeType)
mutable::osl::Mutex m_aMutex
OUString GetPath() const
void AddFilter(const OUString &rFilterName, const OUString &rExtension)
const css::uno::Reference< css::ui::dialogs::XFilePicker3 > & GetFilePicker() const
void SetTitle(const OUString &rNewTitle)
void SetContext(Context _eNewContext)
Reference< XDispatch > xDispatch
URL aURL
#define ERRCODE_NONE
OUString aWildcard
std::mutex m_aMutex
sal_Int32 nIndex
#define SAL_WARN(area, stream)
OUString AvmResId(TranslateId aId)
Definition: mediamisc.hxx:36
#define AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME
Definition: mediawindow.cxx:46
::std::vector< ::std::pair< OUString, OUString > > FilterNameVector
Definition: mediawindow.hxx:52
cppu::WeakComponentImplHelper< css::media::XPlayerListener > PlayerListener_BASE
Definition: mediawindow.hxx:58
css::uno::Sequence< css::beans::PropertyValue > InitPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
void Create(SwFormatVertOrient &rItem, SvStream &rStrm, sal_uInt16 nVersionAbusedAsSize)
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
PointerStyle
signed char sal_Int8
const SvXMLTokenMapEntry aTypes[]