cid#1607171 Data race condition
[LibreOffice.git] / avmedia / source / viewer / mediawindow.cxx
blob140eab4dcd4906e4ca6561dfa234a2458563394f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <avmedia/mediawindow.hxx>
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>
30 #include <sfx2/filedlghelper.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>
40 #include <comphelper/processfactory.hxx>
41 #include <comphelper/propertysequence.hxx>
42 #include <memory>
43 #include <sal/log.hxx>
44 #include <o3tl/string_view.hxx>
46 #define AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME 3.0
48 using namespace ::com::sun::star;
50 namespace avmedia {
52 MediaWindow::MediaWindow( vcl::Window* parent, bool bInternalMediaControl ) :
53 mpImpl( VclPtr<priv::MediaWindowImpl>::Create( parent, this, bInternalMediaControl ) )
55 mpImpl->Show();
59 MediaWindow::~MediaWindow()
61 mpImpl.disposeAndClear();
65 void MediaWindow::setURL( const OUString& rURL, const OUString& rReferer )
67 mpImpl->setURL( rURL, OUString(), rReferer );
71 const OUString& MediaWindow::getURL() const
73 return mpImpl->getURL();
77 bool MediaWindow::isValid() const
79 return mpImpl->isValid();
83 void MediaWindow::MouseMove( const MouseEvent& )
88 void MediaWindow::MouseButtonDown( const MouseEvent& )
93 void MediaWindow::MouseButtonUp( const MouseEvent& )
98 void MediaWindow::KeyInput( const KeyEvent& )
103 void MediaWindow::KeyUp( const KeyEvent& )
107 void MediaWindow::Command( const CommandEvent& )
112 sal_Int8 MediaWindow::AcceptDrop( const AcceptDropEvent& )
114 return 0;
118 sal_Int8 MediaWindow::ExecuteDrop( const ExecuteDropEvent& )
120 return 0;
124 void MediaWindow::StartDrag( sal_Int8, const Point& )
129 Size MediaWindow::getPreferredSize() const
131 return mpImpl->getPreferredSize();
135 void MediaWindow::setPosSize( const tools::Rectangle& rNewRect )
137 mpImpl->setPosSize( rNewRect );
141 void MediaWindow::setPointer( PointerStyle nPointer )
143 mpImpl->setPointer( nPointer );
147 bool MediaWindow::start()
149 return mpImpl->start();
152 void MediaWindow::updateMediaItem( MediaItem& rItem ) const
154 mpImpl->updateMediaItem( rItem );
157 void MediaWindow::executeMediaItem( const MediaItem& rItem )
159 mpImpl->executeMediaItem( rItem );
162 void MediaWindow::show()
164 mpImpl->Show();
167 void MediaWindow::hide()
169 mpImpl->Hide();
172 bool MediaWindow::isVisible() const
174 return mpImpl->IsVisible();
177 vcl::Window* MediaWindow::getWindow() const
179 return mpImpl.get();
183 FilterNameVector MediaWindow::getMediaFilters()
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"}};
214 bool MediaWindow::executeMediaURLDialog(weld::Window* pParent, OUString& rURL, bool *const o_pbLink)
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[] = "*.";
221 FilterNameVector aFilters = getMediaFilters();
222 static const char aSeparator[] = ";";
223 OUStringBuffer aAllTypes;
225 aDlg.SetContext(sfx2::FileDialogHelper::InsertMedia);
226 aDlg.SetTitle( AvmResId( o_pbLink != nullptr
227 ? AVMEDIA_STR_INSERTMEDIA_DLG : AVMEDIA_STR_OPENMEDIA_DLG ) );
229 for( const auto &filter : aFilters )
231 for( sal_Int32 nIndex = 0; nIndex >= 0; )
233 if( !aAllTypes.isEmpty() )
234 aAllTypes.append(aSeparator);
236 aAllTypes.append(OUString::Concat(aWildcard) + o3tl::getToken(filter.second, 0, ';', nIndex ));
240 // add filter for all media types
241 aDlg.AddFilter( AvmResId( AVMEDIA_STR_ALL_MEDIAFILES ), aAllTypes.makeStringAndClear() );
243 for( const auto &filter : aFilters )
245 OUStringBuffer aTypes;
247 for( sal_Int32 nIndex = 0; nIndex >= 0; )
249 if( !aTypes.isEmpty() )
250 aTypes.append(aSeparator);
252 aTypes.append(OUString::Concat(aWildcard) + o3tl::getToken(filter.second, 0, ';', nIndex ));
255 // add single filters
256 aDlg.AddFilter( filter.first, aTypes.makeStringAndClear() );
259 // add filter for all types
260 aDlg.AddFilter( AvmResId( AVMEDIA_STR_ALL_FILES ), u"*.*"_ustr );
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)
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);
277 if( aDlg.Execute() == ERRCODE_NONE )
279 const INetURLObject aURL( aDlg.GetPath() );
280 rURL = aURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous );
282 if (o_pbLink != nullptr)
284 uno::Any const any = xCtrlAcc->getValue(
285 ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK, 0);
286 if (!(any >>= *o_pbLink))
288 SAL_WARN("avmedia", "invalid link property");
289 *o_pbLink = true;
293 else if( !rURL.isEmpty() )
294 rURL.clear();
296 return !rURL.isEmpty();
299 void MediaWindow::executeFormatErrorBox(weld::Window* pParent)
301 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(pParent,
302 VclMessageType::Warning, VclButtonsType::Ok, AvmResId(AVMEDIA_STR_ERR_URL)));
303 xBox->run();
306 bool MediaWindow::isMediaURL(std::u16string_view rURL, const OUString& rReferer, bool bDeep, const rtl::Reference<PlayerListener>& xPreferredPixelSizeListener)
308 const INetURLObject aURL( rURL );
310 if( aURL.GetProtocol() == INetProtocol::NotValid )
311 return false;
313 if (bDeep || xPreferredPixelSizeListener)
317 uno::Reference< media::XPlayer > xPlayer( priv::MediaWindowImpl::createPlayer(
318 aURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous ),
319 rReferer, nullptr ) );
321 if( xPlayer.is() )
323 if (xPreferredPixelSizeListener)
325 uno::Reference<media::XPlayerNotifier> xPlayerNotifier(xPlayer, css::uno::UNO_QUERY);
326 if (xPlayerNotifier)
328 // wait until it's possible to query this to get a sensible answer
329 xPreferredPixelSizeListener->startListening(xPlayerNotifier);
331 else
333 // assume the size is possible to query immediately
334 xPreferredPixelSizeListener->callPlayerWindowSizeAvailable(xPlayer);
337 return true;
340 catch( ... )
344 else
346 FilterNameVector aFilters = getMediaFilters();
347 const OUString aExt( aURL.getExtension() );
349 for( const auto &filter : aFilters )
351 for( sal_Int32 nIndex = 0; nIndex >= 0; )
353 if( aExt.equalsIgnoreAsciiCase( o3tl::getToken(filter.second, 0, ';', nIndex ) ) )
354 return true;
359 return false;
362 uno::Reference< media::XPlayer > MediaWindow::createPlayer( const OUString& rURL, const OUString& rReferer, const OUString* pMimeType )
364 return priv::MediaWindowImpl::createPlayer( rURL, rReferer, pMimeType );
367 uno::Reference<graphic::XGraphic>
368 MediaWindow::grabFrame(const uno::Reference<media::XPlayer>& xPlayer,
369 const uno::Reference<graphic::XGraphic>& rGraphic)
371 uno::Reference< graphic::XGraphic > xRet;
372 std::optional< Graphic > oGraphic;
374 if( xPlayer.is() )
376 uno::Reference< media::XFrameGrabber > xGrabber( xPlayer->createFrameGrabber() );
378 if( xGrabber.is() )
380 double fMediaTime = AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME;
382 if( fMediaTime >= xPlayer->getDuration() )
383 fMediaTime = ( xPlayer->getDuration() * 0.5 );
385 xRet = xGrabber->grabFrame( fMediaTime );
388 if( !xRet.is() )
390 awt::Size aPrefSize( xPlayer->getPreferredPlayerWindowSize() );
392 if( !aPrefSize.Width && !aPrefSize.Height )
394 const BitmapEx aBmpEx(AVMEDIA_BMP_AUDIOLOGO);
395 oGraphic.emplace( aBmpEx );
400 if (!xRet.is() && !oGraphic)
402 const BitmapEx aBmpEx(AVMEDIA_BMP_EMPTYLOGO);
403 oGraphic.emplace( aBmpEx );
406 if (oGraphic)
408 if (rGraphic)
409 oGraphic.emplace(rGraphic);
410 xRet = oGraphic->GetXGraphic();
413 return xRet;
416 uno::Reference< graphic::XGraphic > MediaWindow::grabFrame(const OUString& rURL,
417 const OUString& rReferer,
418 const OUString& sMimeType,
419 const rtl::Reference<PlayerListener>& xPreferredPixelSizeListener)
421 uno::Reference<media::XPlayer> xPlayer(createPlayer(rURL, rReferer, &sMimeType));
423 if (xPreferredPixelSizeListener)
425 uno::Reference<media::XPlayerNotifier> xPlayerNotifier(xPlayer, css::uno::UNO_QUERY);
426 if (xPlayerNotifier)
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);
432 else
434 // assume the size is possible to query immediately
435 xPreferredPixelSizeListener->callPlayerWindowSizeAvailable(xPlayer);
438 return nullptr;
441 return grabFrame(xPlayer);
444 void MediaWindow::dispatchInsertAVMedia(const css::uno::Reference<css::frame::XDispatchProvider>& rDispatchProvider,
445 const css::awt::Size& rSize, const OUString& rURL, bool bLink)
447 util::URL aDispatchURL;
448 aDispatchURL.Complete = ".uno:InsertAVMedia";
450 css::uno::Reference<css::util::XURLTransformer> xTrans(css::util::URLTransformer::create(::comphelper::getProcessComponentContext()));
451 xTrans->parseStrict(aDispatchURL);
453 css::uno::Reference<css::frame::XDispatch> xDispatch = rDispatchProvider->queryDispatch(aDispatchURL, u""_ustr, 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);
463 PlayerListener::PlayerListener(std::function<void(const css::uno::Reference<css::media::XPlayer>&)> fn)
464 : m_aFn(std::move(fn))
468 void PlayerListener::disposing(std::unique_lock<std::mutex>& rGuard)
470 stopListening(rGuard);
471 WeakComponentImplHelperBase::disposing(rGuard);
474 void PlayerListener::startListening(const css::uno::Reference<media::XPlayerNotifier>& rNotifier)
476 std::unique_lock aGuard(m_aMutex);
478 m_xNotifier = rNotifier;
479 m_xNotifier->addPlayerListener(this);
482 void PlayerListener::stopListening(std::unique_lock<std::mutex>&)
484 if (!m_xNotifier)
485 return;
486 m_xNotifier->removePlayerListener(this);
487 m_xNotifier.clear();
490 void SAL_CALL PlayerListener::preferredPlayerWindowSizeAvailable(const css::lang::EventObject&)
492 std::unique_lock aGuard(m_aMutex);
494 css::uno::Reference<media::XPlayer> xPlayer(m_xNotifier, css::uno::UNO_QUERY_THROW);
495 aGuard.unlock();
496 callPlayerWindowSizeAvailable(xPlayer);
497 aGuard.lock();
499 stopListening(aGuard);
502 void SAL_CALL PlayerListener::disposing(const css::lang::EventObject&)
506 PlayerListener::~PlayerListener()
510 } // namespace avmedia
512 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */