merge the formfield patch from ooo-build
[ooovba.git] / avmedia / source / win / framegrabber.cxx
bloba7d1b6bf26a6beb7f2457db7520bfe2f0b17385c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: framegrabber.cxx,v $
10 * $Revision: 1.7 $
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>
32 #if defined _MSC_VER
33 #pragma warning(push, 1)
34 #pragma warning(disable: 4917)
35 #endif
36 #include <windows.h>
37 #include <objbase.h>
38 #include <strmif.h>
39 #include <Amvideo.h>
40 #if defined(_MSC_VER) && (_MSC_VER < 1500)
41 #include <Qedit.h>
42 #else
43 #include "interface.hxx"
44 #endif
45 #include <uuids.h>
46 #if defined _MSC_VER
47 #pragma warning(pop)
48 #endif
49 #include <tools/postwin.h>
51 #include "framegrabber.hxx"
52 #include "player.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 {
65 // ----------------
66 // - FrameGrabber -
67 // ----------------
69 FrameGrabber::FrameGrabber( const uno::Reference< lang::XMultiServiceFactory >& rxMgr ) :
70 mxMgr( rxMgr )
72 ::CoInitialize( NULL );
75 // ------------------------------------------------------------------------------
77 FrameGrabber::~FrameGrabber()
79 ::CoUninitialize();
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 ) ) )
90 String aLocalStr;
92 if( ::utl::LocalFileHelper::ConvertURLToPhysicalName( rURL, aLocalStr ) && aLocalStr.Len() )
94 if( !SUCCEEDED( pDet->put_Filename( ::SysAllocString( reinterpret_cast<LPCOLESTR>(aLocalStr.GetBuffer()) ) ) ) )
96 pDet->Release();
97 pDet = NULL;
102 return pDet;
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 );
112 if( pDet )
114 maURL = rURL;
115 pDet->Release();
116 pDet = NULL;
118 else
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 );
132 if( pDet )
134 double fLength;
135 long nStreamCount;
136 bool bFound = false;
138 if( SUCCEEDED( pDet->get_OutputStreams( &nStreamCount ) ) )
140 for( long n = 0; ( n < nStreamCount ) && !bFound; ++n )
142 GUID aMajorType;
144 if( SUCCEEDED( pDet->put_CurrentStream( n ) ) &&
145 SUCCEEDED( pDet->get_StreamType( &aMajorType ) ) &&
146 ( aMajorType == MEDIATYPE_Video ) )
148 bFound = true;
153 if( bFound &&
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;
170 if( nHeight < 0 )
171 nHeight *= -1;
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 ) ) &&
190 ( nSize > 0 ) )
192 char* pBuffer = new char[ nSize ];
194 try
196 if( SUCCEEDED( pDet->GetBitmapBits( fMediaTime, NULL, pBuffer, nWidth, nHeight ) ) )
198 SvMemoryStream aMemStm( pBuffer, nSize, STREAM_READ | STREAM_WRITE );
199 Bitmap aBmp;
201 if( aBmp.Read( aMemStm, false ) && !aBmp.IsEmpty() )
203 const Graphic aGraphic( aBmp );
204 xRet = aGraphic.GetXGraphic();
208 catch( ... )
212 delete [] pBuffer;
216 pDet->Release();
219 return xRet;
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 ) );
246 return aRet;
249 } // namespace win
250 } // namespace avmedia