1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: framegrabber.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <tools/prewin.h>
33 #pragma warning(push, 1)
34 #pragma warning(disable: 4917)
40 #if defined(_MSC_VER) && (_MSC_VER < 1500)
43 #include "interface.hxx"
49 #include <tools/postwin.h>
51 #include "framegrabber.hxx"
54 #include <tools/stream.hxx>
55 #include <vcl/graph.hxx>
56 #include <unotools/localfilehelper.hxx>
58 #define AVMEDIA_WIN_FRAMEGRABBER_IMPLEMENTATIONNAME "com.sun.star.comp.avmedia.FrameGrabber_DirectX"
59 #define AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME "com.sun.star.media.FrameGrabber_DirectX"
61 using namespace ::com::sun::star
;
63 namespace avmedia
{ namespace win
{
69 FrameGrabber::FrameGrabber( const uno::Reference
< lang::XMultiServiceFactory
>& rxMgr
) :
72 ::CoInitialize( NULL
);
75 // ------------------------------------------------------------------------------
77 FrameGrabber::~FrameGrabber()
82 // ------------------------------------------------------------------------------
84 IMediaDet
* FrameGrabber::implCreateMediaDet( const ::rtl::OUString
& rURL
) const
86 IMediaDet
* pDet
= NULL
;
88 if( SUCCEEDED( CoCreateInstance( CLSID_MediaDet
, NULL
, CLSCTX_INPROC_SERVER
, IID_IMediaDet
, (void**) &pDet
) ) )
92 if( ::utl::LocalFileHelper::ConvertURLToPhysicalName( rURL
, aLocalStr
) && aLocalStr
.Len() )
94 if( !SUCCEEDED( pDet
->put_Filename( ::SysAllocString( reinterpret_cast<LPCOLESTR
>(aLocalStr
.GetBuffer()) ) ) ) )
105 // ------------------------------------------------------------------------------
107 bool FrameGrabber::create( const ::rtl::OUString
& rURL
)
109 // just check if a MediaDet interface can be created with the given URL
110 IMediaDet
* pDet
= implCreateMediaDet( rURL
);
119 maURL
= ::rtl::OUString();
121 return( maURL
.getLength() > 0 );
124 // ------------------------------------------------------------------------------
126 uno::Reference
< graphic::XGraphic
> SAL_CALL
FrameGrabber::grabFrame( double fMediaTime
)
127 throw (uno::RuntimeException
)
129 uno::Reference
< graphic::XGraphic
> xRet
;
130 IMediaDet
* pDet
= implCreateMediaDet( maURL
);
138 if( SUCCEEDED( pDet
->get_OutputStreams( &nStreamCount
) ) )
140 for( long n
= 0; ( n
< nStreamCount
) && !bFound
; ++n
)
144 if( SUCCEEDED( pDet
->put_CurrentStream( n
) ) &&
145 SUCCEEDED( pDet
->get_StreamType( &aMajorType
) ) &&
146 ( aMajorType
== MEDIATYPE_Video
) )
154 ( S_OK
== pDet
->get_StreamLength( &fLength
) ) &&
155 ( fLength
> 0.0 ) && ( fMediaTime
>= 0.0 ) && ( fMediaTime
<= fLength
) )
157 AM_MEDIA_TYPE aMediaType
;
158 long nWidth
= 0, nHeight
= 0, nSize
= 0;
160 if( SUCCEEDED( pDet
->get_StreamMediaType( &aMediaType
) ) )
162 if( ( aMediaType
.formattype
== FORMAT_VideoInfo
) &&
163 ( aMediaType
.cbFormat
>= sizeof( VIDEOINFOHEADER
) ) )
165 VIDEOINFOHEADER
* pVih
= reinterpret_cast< VIDEOINFOHEADER
* >( aMediaType
.pbFormat
);
167 nWidth
= pVih
->bmiHeader
.biWidth
;
168 nHeight
= pVih
->bmiHeader
.biHeight
;
174 if( aMediaType
.cbFormat
!= 0 )
176 ::CoTaskMemFree( (PVOID
) aMediaType
.pbFormat
);
177 aMediaType
.cbFormat
= 0;
178 aMediaType
.pbFormat
= NULL
;
181 if( aMediaType
.pUnk
!= NULL
)
183 aMediaType
.pUnk
->Release();
184 aMediaType
.pUnk
= NULL
;
188 if( ( nWidth
> 0 ) && ( nHeight
> 0 ) &&
189 SUCCEEDED( pDet
->GetBitmapBits( 0, &nSize
, NULL
, nWidth
, nHeight
) ) &&
192 char* pBuffer
= new char[ nSize
];
196 if( SUCCEEDED( pDet
->GetBitmapBits( fMediaTime
, NULL
, pBuffer
, nWidth
, nHeight
) ) )
198 SvMemoryStream
aMemStm( pBuffer
, nSize
, STREAM_READ
| STREAM_WRITE
);
201 if( aBmp
.Read( aMemStm
, false ) && !aBmp
.IsEmpty() )
203 const Graphic
aGraphic( aBmp
);
204 xRet
= aGraphic
.GetXGraphic();
222 // ------------------------------------------------------------------------------
224 ::rtl::OUString SAL_CALL
FrameGrabber::getImplementationName( )
225 throw (uno::RuntimeException
)
227 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( AVMEDIA_WIN_FRAMEGRABBER_IMPLEMENTATIONNAME
) );
230 // ------------------------------------------------------------------------------
232 sal_Bool SAL_CALL
FrameGrabber::supportsService( const ::rtl::OUString
& ServiceName
)
233 throw (uno::RuntimeException
)
235 return ServiceName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME
) );
238 // ------------------------------------------------------------------------------
240 uno::Sequence
< ::rtl::OUString
> SAL_CALL
FrameGrabber::getSupportedServiceNames( )
241 throw (uno::RuntimeException
)
243 uno::Sequence
< ::rtl::OUString
> aRet(1);
244 aRet
[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME
) );
250 } // namespace avmedia