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 .
21 #pragma warning(push, 1)
22 #pragma warning(disable: 4917)
29 #include "interface.hxx"
35 #include "framegrabber.hxx"
38 #include <tools/stream.hxx>
39 #include <vcl/graph.hxx>
40 #include <unotools/localfilehelper.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
{
53 FrameGrabber::FrameGrabber( const uno::Reference
< lang::XMultiServiceFactory
>& rxMgr
) :
56 ::CoInitialize( NULL
);
59 // ------------------------------------------------------------------------------
61 FrameGrabber::~FrameGrabber()
66 // ------------------------------------------------------------------------------
68 IMediaDet
* FrameGrabber::implCreateMediaDet( const OUString
& rURL
) const
70 IMediaDet
* pDet
= NULL
;
72 if( SUCCEEDED( CoCreateInstance( CLSID_MediaDet
, NULL
, CLSCTX_INPROC_SERVER
, IID_IMediaDet
, (void**) &pDet
) ) )
76 if( ::utl::LocalFileHelper::ConvertURLToPhysicalName( rURL
, aLocalStr
) && !aLocalStr
.isEmpty() )
78 if( !SUCCEEDED( pDet
->put_Filename( ::SysAllocString( reinterpret_cast<LPCOLESTR
>(aLocalStr
.getStr()) ) ) ) )
89 // ------------------------------------------------------------------------------
91 bool FrameGrabber::create( const OUString
& rURL
)
93 // just check if a MediaDet interface can be created with the given URL
94 IMediaDet
* pDet
= implCreateMediaDet( rURL
);
105 return( maURL
.getLength() > 0 );
108 // ------------------------------------------------------------------------------
110 uno::Reference
< graphic::XGraphic
> SAL_CALL
FrameGrabber::grabFrame( double fMediaTime
)
111 throw (uno::RuntimeException
)
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( (PVOID
) aMediaType
.pbFormat
);
161 aMediaType
.cbFormat
= 0;
162 aMediaType
.pbFormat
= NULL
;
165 if( aMediaType
.pUnk
!= NULL
)
167 aMediaType
.pUnk
->Release();
168 aMediaType
.pUnk
= NULL
;
172 if( ( nWidth
> 0 ) && ( nHeight
> 0 ) &&
173 SUCCEEDED( pDet
->GetBitmapBits( 0, &nSize
, NULL
, nWidth
, nHeight
) ) &&
176 char* pBuffer
= new char[ nSize
];
180 if( SUCCEEDED( pDet
->GetBitmapBits( fMediaTime
, NULL
, pBuffer
, nWidth
, nHeight
) ) )
182 SvMemoryStream
aMemStm( pBuffer
, nSize
, STREAM_READ
| STREAM_WRITE
);
185 if( aBmp
.Read( aMemStm
, false ) && !aBmp
.IsEmpty() )
187 const Graphic
aGraphic( aBmp
);
188 xRet
= aGraphic
.GetXGraphic();
206 // ------------------------------------------------------------------------------
208 OUString SAL_CALL
FrameGrabber::getImplementationName( )
209 throw (uno::RuntimeException
)
211 return OUString( AVMEDIA_WIN_FRAMEGRABBER_IMPLEMENTATIONNAME
);
214 // ------------------------------------------------------------------------------
216 sal_Bool SAL_CALL
FrameGrabber::supportsService( const OUString
& ServiceName
)
217 throw (uno::RuntimeException
)
219 return ServiceName
== AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME
;
222 // ------------------------------------------------------------------------------
224 uno::Sequence
< OUString
> SAL_CALL
FrameGrabber::getSupportedServiceNames( )
225 throw (uno::RuntimeException
)
227 uno::Sequence
< OUString
> aRet(1);
228 aRet
[0] = AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME
;
234 } // namespace avmedia
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */