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/.
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 .
22 #include <osl/conditn.hxx>
23 #include <osl/file.hxx>
24 #include <vcl/graph.hxx>
25 #include <vcl/bitmapaccess.hxx>
26 #include <vcl/pngread.hxx>
27 #include <avmedia/mediawindow.hxx>
28 #include <unotools/tempfile.hxx>
29 #include <unotools/ucbstreamhelper.hxx>
30 #include <tools/stream.hxx>
31 #include <cppuhelper/supportsservice.hxx>
32 #include <sal/log.hxx>
34 #include "vlcframegrabber.hxx"
35 #include "vlcplayer.hxx"
36 #include <wrapper/Player.hxx>
37 #include <wrapper/EventManager.hxx>
39 using namespace ::com::sun::star
;
46 const OUString AVMEDIA_VLC_GRABBER_IMPLEMENTATIONNAME
= "com.sun.star.comp.avmedia.VLCFrameGrabber_VLC";
47 const OUString AVMEDIA_VLC_GRABBER_SERVICENAME
= "com.sun.star.media.VLCFrameGrabber_VLC";
48 const int MSEC_IN_SEC
= 1000;
50 const char * const VLC_ARGS
[] = {
54 "--snapshot-format=png",
55 "--ffmpeg-threads", /* Is deprecated in 2.1.0 */
61 VLCFrameGrabber::VLCFrameGrabber( wrapper::EventHandler
& eh
, const OUString
& url
)
63 , mInstance( SAL_N_ELEMENTS(VLC_ARGS
), VLC_ARGS
)
64 , mMedia( url
, mInstance
)
70 ::uno::Reference
< css::graphic::XGraphic
> SAL_CALL
VLCFrameGrabber::grabFrame( double fMediaTime
)
72 osl::Condition condition
;
74 const OUString
& fileName
= utl::TempFile::CreateTempName();
76 wrapper::EventManager
manager( mPlayer
, mEventHandler
);
77 manager
.onPaused([&condition
](){ condition
.set(); });
79 if ( !mPlayer
.play() )
81 SAL_WARN("avmedia", "Couldn't play when trying to grab frame");
82 return ::uno::Reference
< css::graphic::XGraphic
>();
85 mPlayer
.setTime( std::max(fMediaTime
, 0.0) * MSEC_IN_SEC
);
88 condition
.wait(std::chrono::seconds(2));
90 if ( !mPlayer
.hasVout() )
92 SAL_WARN("avmedia", "Couldn't grab frame");
94 return ::uno::Reference
< css::graphic::XGraphic
>();
97 mPlayer
.takeSnapshot( fileName
);
104 osl::FileBase::getFileURLFromSystemPath( fileName
, url
);
105 std::shared_ptr
<SvStream
> stream( utl::UcbStreamHelper::CreateStream( url
,
106 StreamMode::STD_READ
) );
108 vcl::PNGReader
reader( *stream
);
110 const BitmapEx
& bitmap
= reader
.Read();
112 return Graphic( bitmap
).GetXGraphic();
115 OUString SAL_CALL
VLCFrameGrabber::getImplementationName()
117 return AVMEDIA_VLC_GRABBER_IMPLEMENTATIONNAME
;
120 sal_Bool SAL_CALL
VLCFrameGrabber::supportsService( const OUString
& serviceName
)
122 return cppu::supportsService(this, serviceName
);
125 ::uno::Sequence
< OUString
> SAL_CALL
VLCFrameGrabber::getSupportedServiceNames()
127 return { AVMEDIA_VLC_GRABBER_SERVICENAME
};
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */