LibreOffice Module avmedia (master) 1
framegrabber.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
20#include <sal/config.h>
21
22#include <memory>
23
24#include <prewin.h>
25#include <postwin.h>
26#include <objbase.h>
27#include <strmif.h>
28#include <Amvideo.h>
29#include "interface.hxx"
30#include <uuids.h>
31
32#include "framegrabber.hxx"
33#include "player.hxx"
34
36#include <osl/file.hxx>
37#include <tools/stream.hxx>
38#include <vcl/graph.hxx>
39#include <vcl/dibtools.hxx>
41#include <systools/win32/oleauto.hxx>
42
43constexpr OUStringLiteral AVMEDIA_WIN_FRAMEGRABBER_IMPLEMENTATIONNAME = u"com.sun.star.comp.avmedia.FrameGrabber_DirectX";
44constexpr OUStringLiteral AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME = u"com.sun.star.media.FrameGrabber_DirectX";
45
46using namespace ::com::sun::star;
47
48namespace avmedia::win {
49
50
52 : sal::systools::CoInitializeGuard(COINIT_APARTMENTTHREADED, false,
53 sal::systools::CoInitializeGuard::WhenFailed::NoThrow)
54{
55}
56
57
59
60namespace {
61
62sal::systools::COMReference<IMediaDet> implCreateMediaDet( const OUString& rURL )
63{
64 sal::systools::COMReference<IMediaDet> pDet;
65
66 if( SUCCEEDED(pDet.CoCreateInstance(CLSID_MediaDet, nullptr, CLSCTX_INPROC_SERVER)) )
67 {
68 OUString aLocalStr;
69
70 if( osl::FileBase::getSystemPathFromFileURL( rURL, aLocalStr )
71 == osl::FileBase::E_None )
72 {
73 if( !SUCCEEDED( pDet->put_Filename(sal::systools::BStr(aLocalStr)) ) )
74 pDet.clear();
75 }
76 }
77
78 return pDet;
79}
80
81}
82
83bool FrameGrabber::create( const OUString& rURL )
84{
85 // just check if a MediaDet interface can be created with the given URL
86 if (implCreateMediaDet(rURL))
87 maURL = rURL;
88 else
89 maURL.clear();
90
91 return !maURL.isEmpty();
92}
93
94
95uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
96{
97 uno::Reference< graphic::XGraphic > xRet;
98 if (sal::systools::COMReference<IMediaDet> pDet = implCreateMediaDet(maURL))
99 {
100 double fLength;
101 long nStreamCount;
102 bool bFound = false;
103
104 if( SUCCEEDED( pDet->get_OutputStreams( &nStreamCount ) ) )
105 {
106 for( long n = 0; ( n < nStreamCount ) && !bFound; ++n )
107 {
108 GUID aMajorType;
109
110 if( SUCCEEDED( pDet->put_CurrentStream( n ) ) &&
111 SUCCEEDED( pDet->get_StreamType( &aMajorType ) ) &&
112 ( aMajorType == MEDIATYPE_Video ) )
113 {
114 bFound = true;
115 }
116 }
117 }
118
119 if( bFound &&
120 ( S_OK == pDet->get_StreamLength( &fLength ) ) &&
121 ( fLength > 0.0 ) && ( fMediaTime >= 0.0 ) && ( fMediaTime <= fLength ) )
122 {
123 AM_MEDIA_TYPE aMediaType;
124 LONG nWidth = 0, nHeight = 0;
125 long nSize = 0;
126
127 if( SUCCEEDED( pDet->get_StreamMediaType( &aMediaType ) ) )
128 {
129 if( ( aMediaType.formattype == FORMAT_VideoInfo ) &&
130 ( aMediaType.cbFormat >= sizeof( VIDEOINFOHEADER ) ) )
131 {
132 VIDEOINFOHEADER* pVih = reinterpret_cast< VIDEOINFOHEADER* >( aMediaType.pbFormat );
133
134 nWidth = pVih->bmiHeader.biWidth;
135 nHeight = pVih->bmiHeader.biHeight;
136
137 if( nHeight < 0 )
138 nHeight *= -1;
139 }
140
141 if( aMediaType.cbFormat != 0 )
142 {
143 ::CoTaskMemFree( aMediaType.pbFormat );
144 aMediaType.cbFormat = 0;
145 aMediaType.pbFormat = nullptr;
146 }
147
148 if( aMediaType.pUnk != nullptr )
149 {
150 aMediaType.pUnk->Release();
151 aMediaType.pUnk = nullptr;
152 }
153 }
154
155 if( ( nWidth > 0 ) && ( nHeight > 0 ) &&
156 SUCCEEDED( pDet->GetBitmapBits( 0, &nSize, nullptr, nWidth, nHeight ) ) &&
157 ( nSize > 0 ) )
158 {
159 auto pBuffer = std::make_unique<char[]>(nSize);
160
161 try
162 {
163 if( SUCCEEDED( pDet->GetBitmapBits( fMediaTime, nullptr, pBuffer.get(), nWidth, nHeight ) ) )
164 {
165 SvMemoryStream aMemStm( pBuffer.get(), nSize, StreamMode::READ | StreamMode::WRITE );
166 Bitmap aBmp;
167
168 if( ReadDIB(aBmp, aMemStm, false ) && !aBmp.IsEmpty() )
169 {
170 BitmapEx aBitmapEx(aBmp);
171 Graphic aGraphic(aBitmapEx);
172 xRet = aGraphic.GetXGraphic();
173 }
174 }
175 }
176 catch( ... )
177 {
178 }
179 }
180 }
181 }
182
183 return xRet;
184}
185
186
188{
190}
191
192
193sal_Bool SAL_CALL FrameGrabber::supportsService( const OUString& ServiceName )
194{
196}
197
198
199uno::Sequence< OUString > SAL_CALL FrameGrabber::getSupportedServiceNames( )
200{
202}
203
204} // namespace avmedia::win
205
206
207/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsEmpty() const
css::uno::Reference< css::graphic::XGraphic > GetXGraphic() const
virtual OUString SAL_CALL getImplementationName() override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
bool create(const OUString &rURL)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual css::uno::Reference< css::graphic::XGraphic > SAL_CALL grabFrame(double fMediaTime) override
bool VCL_DLLPUBLIC ReadDIB(Bitmap &rTarget, SvStream &rIStm, bool bFileHeader, bool bMSOFormat=false)
float u
constexpr OUStringLiteral AVMEDIA_WIN_FRAMEGRABBER_IMPLEMENTATIONNAME
constexpr OUStringLiteral AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME
const CLSID CLSID_MediaDet
Definition: interface.hxx:22
sal_Int64 n
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
LONG
unsigned char sal_Bool