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 .
20 #include <sal/config.h>
29 #include "interface.hxx"
32 #include "framegrabber.hxx"
35 #include <cppuhelper/supportsservice.hxx>
36 #include <osl/file.hxx>
37 #include <tools/stream.hxx>
38 #include <vcl/graph.hxx>
39 #include <vcl/dibtools.hxx>
40 #include <o3tl/char16_t2wchar_t.hxx>
42 #define AVMEDIA_WIN_FRAMEGRABBER_IMPLEMENTATIONNAME "com.sun.star.comp.avmedia.FrameGrabber_DirectX"
43 #define AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME "com.sun.star.media.FrameGrabber_DirectX"
45 using namespace ::com::sun::star
;
47 namespace avmedia
{ namespace win
{
50 FrameGrabber::FrameGrabber( const uno::Reference
< lang::XMultiServiceFactory
>& rxMgr
) :
53 ::CoInitialize( nullptr );
57 FrameGrabber::~FrameGrabber()
64 IMediaDet
* implCreateMediaDet( const OUString
& rURL
)
66 IMediaDet
* pDet
= nullptr;
68 if( SUCCEEDED( CoCreateInstance( CLSID_MediaDet
, nullptr, CLSCTX_INPROC_SERVER
, IID_IMediaDet
, reinterpret_cast<void**>(&pDet
) ) ) )
72 if( osl::FileBase::getSystemPathFromFileURL( rURL
, aLocalStr
)
73 == osl::FileBase::E_None
)
75 BSTR bstrFilename
= SysAllocString(o3tl::toW(aLocalStr
.getStr()));
76 if( !SUCCEEDED( pDet
->put_Filename( bstrFilename
) ) )
78 // Shouldn't we free this string unconditionally, not only in case of failure?
79 // I cannot find information why do we pass a newly allocated BSTR to the put_Filename
80 // and if it frees the string internally
81 SysFreeString(bstrFilename
);
93 bool FrameGrabber::create( const OUString
& rURL
)
95 // just check if a MediaDet interface can be created with the given URL
96 IMediaDet
* pDet
= implCreateMediaDet( rURL
);
107 return !maURL
.isEmpty();
111 uno::Reference
< graphic::XGraphic
> SAL_CALL
FrameGrabber::grabFrame( double fMediaTime
)
113 uno::Reference
< graphic::XGraphic
> xRet
;
114 IMediaDet
* pDet
= implCreateMediaDet( maURL
);
122 if( SUCCEEDED( pDet
->get_OutputStreams( &nStreamCount
) ) )
124 for( long n
= 0; ( n
< nStreamCount
) && !bFound
; ++n
)
128 if( SUCCEEDED( pDet
->put_CurrentStream( n
) ) &&
129 SUCCEEDED( pDet
->get_StreamType( &aMajorType
) ) &&
130 ( aMajorType
== MEDIATYPE_Video
) )
138 ( S_OK
== pDet
->get_StreamLength( &fLength
) ) &&
139 ( fLength
> 0.0 ) && ( fMediaTime
>= 0.0 ) && ( fMediaTime
<= fLength
) )
141 AM_MEDIA_TYPE aMediaType
;
142 long nWidth
= 0, nHeight
= 0, nSize
= 0;
144 if( SUCCEEDED( pDet
->get_StreamMediaType( &aMediaType
) ) )
146 if( ( aMediaType
.formattype
== FORMAT_VideoInfo
) &&
147 ( aMediaType
.cbFormat
>= sizeof( VIDEOINFOHEADER
) ) )
149 VIDEOINFOHEADER
* pVih
= reinterpret_cast< VIDEOINFOHEADER
* >( aMediaType
.pbFormat
);
151 nWidth
= pVih
->bmiHeader
.biWidth
;
152 nHeight
= pVih
->bmiHeader
.biHeight
;
158 if( aMediaType
.cbFormat
!= 0 )
160 ::CoTaskMemFree( aMediaType
.pbFormat
);
161 aMediaType
.cbFormat
= 0;
162 aMediaType
.pbFormat
= nullptr;
165 if( aMediaType
.pUnk
!= nullptr )
167 aMediaType
.pUnk
->Release();
168 aMediaType
.pUnk
= nullptr;
172 if( ( nWidth
> 0 ) && ( nHeight
> 0 ) &&
173 SUCCEEDED( pDet
->GetBitmapBits( 0, &nSize
, nullptr, nWidth
, nHeight
) ) &&
176 auto pBuffer
= std::make_unique
<char[]>(nSize
);
180 if( SUCCEEDED( pDet
->GetBitmapBits( fMediaTime
, nullptr, pBuffer
.get(), nWidth
, nHeight
) ) )
182 SvMemoryStream
aMemStm( pBuffer
.get(), nSize
, StreamMode::READ
| StreamMode::WRITE
);
185 if( ReadDIB(aBmp
, aMemStm
, false ) && !aBmp
.IsEmpty() )
187 const Graphic
aGraphic( aBmp
);
188 xRet
= aGraphic
.GetXGraphic();
205 OUString SAL_CALL
FrameGrabber::getImplementationName( )
207 return OUString( AVMEDIA_WIN_FRAMEGRABBER_IMPLEMENTATIONNAME
);
211 sal_Bool SAL_CALL
FrameGrabber::supportsService( const OUString
& ServiceName
)
213 return cppu::supportsService(this, ServiceName
);
217 uno::Sequence
< OUString
> SAL_CALL
FrameGrabber::getSupportedServiceNames( )
219 return { AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME
};
223 } // namespace avmedia
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */