merge the formfield patch from ooo-build
[ooovba.git] / avmedia / source / gstreamer / gstframegrabber.cxx
blobbbe006298456c8f9d529a70ac95d6ae9d6819c27
1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
5 * $RCSfile$
7 * $Revision$
9 * last change: $Author$ $Date$
11 * The Contents of this file are made available subject to
12 * the terms of GNU Lesser General Public License Version 2.1.
15 * GNU Lesser General Public License Version 2.1
16 * =============================================
17 * Copyright 2005 by Sun Microsystems, Inc.
18 * 901 San Antonio Road, Palo Alto, CA 94303, USA
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License version 2.1, as published by the Free Software Foundation.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * MA 02111-1307 USA
34 ************************************************************************/
36 #include <tools/prewin.h>
37 #include <windows.h>
38 #include <objbase.h>
39 #include <strmif.h>
40 #include <Amvideo.h>
41 #include <Qedit.h>
42 #include <uuids.h>
43 #include <tools/postwin.h>
45 #include "framegrabber.hxx"
46 #include "player.hxx"
48 #include <tools/stream.hxx>
49 #include <vcl/graph.hxx>
50 #include <unotools/localfilehelper.hxx>
52 #define AVMEDIA_GST_FRAMEGRABBER_IMPLEMENTATIONNAME "com.sun.star.comp.avmedia.FrameGrabber_GStreamer"
53 #define AVMEDIA_GST_FRAMEGRABBER_SERVICENAME "com.sun.star.media.FrameGrabber_GStreamer"
55 using namespace ::com::sun::star;
57 namespace avmedia { namespace gstreamer {
59 // ----------------
60 // - FrameGrabber -
61 // ----------------
63 FrameGrabber::FrameGrabber( const uno::Reference< lang::XMultiServiceFactory >& rxMgr ) :
64 mxMgr( rxMgr )
66 ::CoInitialize( NULL );
69 // ------------------------------------------------------------------------------
71 FrameGrabber::~FrameGrabber()
73 ::CoUninitialize();
76 // ------------------------------------------------------------------------------
78 IMediaDet* FrameGrabber::implCreateMediaDet( const ::rtl::OUString& rURL ) const
80 IMediaDet* pDet = NULL;
82 if( SUCCEEDED( CoCreateInstance( CLSID_MediaDet, NULL, CLSCTX_INPROC_SERVER, IID_IMediaDet, (void**) &pDet ) ) )
84 String aLocalStr;
86 if( ::utl::LocalFileHelper::ConvertURLToPhysicalName( rURL, aLocalStr ) && aLocalStr.Len() )
88 if( !SUCCEEDED( pDet->put_Filename( ::SysAllocString( aLocalStr.GetBuffer() ) ) ) )
90 pDet->Release();
91 pDet = NULL;
96 return pDet;
99 // ------------------------------------------------------------------------------
101 bool FrameGrabber::create( const ::rtl::OUString& rURL )
103 // just check if a MediaDet interface can be created with the given URL
104 IMediaDet* pDet = implCreateMediaDet( rURL );
106 if( pDet )
108 maURL = rURL;
109 pDet->Release();
110 pDet = NULL;
112 else
113 maURL = ::rtl::OUString();
115 return( maURL.getLength() > 0 );
118 // ------------------------------------------------------------------------------
120 uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
121 throw (uno::RuntimeException)
123 uno::Reference< graphic::XGraphic > xRet;
124 IMediaDet* pDet = implCreateMediaDet( maURL );
126 if( pDet )
128 double fLength;
129 long nStreamCount;
130 bool bFound = false;
132 if( SUCCEEDED( pDet->get_OutputStreams( &nStreamCount ) ) )
134 for( long n = 0; ( n < nStreamCount ) && !bFound; ++n )
136 GUID aMajorType;
138 if( SUCCEEDED( pDet->put_CurrentStream( n ) ) &&
139 SUCCEEDED( pDet->get_StreamType( &aMajorType ) ) &&
140 ( aMajorType == MEDIATYPE_Video ) )
142 bFound = true;
147 if( bFound &&
148 ( S_OK == pDet->get_StreamLength( &fLength ) ) &&
149 ( fLength > 0.0 ) && ( fMediaTime >= 0.0 ) && ( fMediaTime <= fLength ) )
151 AM_MEDIA_TYPE aMediaType;
152 long nWidth = 0, nHeight = 0, nSize = 0;
154 if( SUCCEEDED( pDet->get_StreamMediaType( &aMediaType ) ) )
156 if( ( aMediaType.formattype == FORMAT_VideoInfo ) &&
157 ( aMediaType.cbFormat >= sizeof( VIDEOINFOHEADER ) ) )
159 VIDEOINFOHEADER* pVih = reinterpret_cast< VIDEOINFOHEADER* >( aMediaType.pbFormat );
161 nWidth = pVih->bmiHeader.biWidth;
162 nHeight = pVih->bmiHeader.biHeight;
164 if( nHeight < 0 )
165 nHeight *= -1;
168 if( aMediaType.cbFormat != 0 )
170 ::CoTaskMemFree( (PVOID) aMediaType.pbFormat );
171 aMediaType.cbFormat = 0;
172 aMediaType.pbFormat = NULL;
175 if( aMediaType.pUnk != NULL )
177 aMediaType.pUnk->Release();
178 aMediaType.pUnk = NULL;
182 if( ( nWidth > 0 ) && ( nHeight > 0 ) &&
183 SUCCEEDED( pDet->GetBitmapBits( 0, &nSize, NULL, nWidth, nHeight ) ) &&
184 ( nSize > 0 ) )
186 char* pBuffer = new char[ nSize ];
188 try
190 if( SUCCEEDED( pDet->GetBitmapBits( fMediaTime, NULL, pBuffer, nWidth, nHeight ) ) )
192 SvMemoryStream aMemStm( pBuffer, nSize, STREAM_READ | STREAM_WRITE );
193 Bitmap aBmp;
195 if( aBmp.Read( aMemStm, false ) && !aBmp.IsEmpty() )
197 const Graphic aGraphic( aBmp );
198 xRet = aGraphic.GetXGraphic();
202 catch( ... )
206 delete [] pBuffer;
210 pDet->Release();
213 return xRet;
216 // ------------------------------------------------------------------------------
218 ::rtl::OUString SAL_CALL FrameGrabber::getImplementationName( )
219 throw (uno::RuntimeException)
221 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( AVMEDIA_GST_FRAMEGRABBER_IMPLEMENTATIONNAME ) );
224 // ------------------------------------------------------------------------------
226 sal_Bool SAL_CALL FrameGrabber::supportsService( const ::rtl::OUString& ServiceName )
227 throw (uno::RuntimeException)
229 return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( AVMEDIA_GST_FRAMEGRABBER_SERVICENAME ) );
232 // ------------------------------------------------------------------------------
234 uno::Sequence< ::rtl::OUString > SAL_CALL FrameGrabber::getSupportedServiceNames( )
235 throw (uno::RuntimeException)
237 uno::Sequence< ::rtl::OUString > aRet(1);
238 aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( AVMEDIA_GST_FRAMEGRABBER_SERVICENAME ) );
240 return aRet;
243 } // namespace gstreamer
244 } // namespace avmedia