Update ooo320-m1
[ooovba.git] / applied_patches / 0381-avmedia-source-gstreamer-gstframegrabber.cxx.diff
blob174b714fd0dd98a117b34c1b3b1fc79d5724034a
1 --- /dev/null 2006-08-13 17:52:05.000000000 +0200
2 +++ avmedia/source/gstreamer/gstframegrabber.cxx 2006-08-22 11:38:56.000000000 +0200
3 @@ -0,0 +1,244 @@
4 +/*************************************************************************
5 + *
6 + * OpenOffice.org - a multi-platform office productivity suite
7 + *
8 + * $RCSfile$
9 + *
10 + * $Revision$
11 + *
12 + * last change: $Author$ $Date$
13 + *
14 + * The Contents of this file are made available subject to
15 + * the terms of GNU Lesser General Public License Version 2.1.
16 + *
17 + *
18 + * GNU Lesser General Public License Version 2.1
19 + * =============================================
20 + * Copyright 2005 by Sun Microsystems, Inc.
21 + * 901 San Antonio Road, Palo Alto, CA 94303, USA
22 + *
23 + * This library is free software; you can redistribute it and/or
24 + * modify it under the terms of the GNU Lesser General Public
25 + * License version 2.1, as published by the Free Software Foundation.
26 + *
27 + * This library is distributed in the hope that it will be useful,
28 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 + * Lesser General Public License for more details.
31 + *
32 + * You should have received a copy of the GNU Lesser General Public
33 + * License along with this library; if not, write to the Free Software
34 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
35 + * MA 02111-1307 USA
36 + *
37 + ************************************************************************/
39 +#include <tools/prewin.h>
40 +#include <windows.h>
41 +#include <objbase.h>
42 +#include <strmif.h>
43 +#include <Amvideo.h>
44 +#include <Qedit.h>
45 +#include <uuids.h>
46 +#include <tools/postwin.h>
48 +#include "framegrabber.hxx"
49 +#include "player.hxx"
51 +#include <tools/stream.hxx>
52 +#include <vcl/graph.hxx>
53 +#include <unotools/localfilehelper.hxx>
55 +#define AVMEDIA_GST_FRAMEGRABBER_IMPLEMENTATIONNAME "com.sun.star.comp.avmedia.FrameGrabber_GStreamer"
56 +#define AVMEDIA_GST_FRAMEGRABBER_SERVICENAME "com.sun.star.media.FrameGrabber_GStreamer"
58 +using namespace ::com::sun::star;
60 +namespace avmedia { namespace gstreamer {
62 +// ----------------
63 +// - FrameGrabber -
64 +// ----------------
66 +FrameGrabber::FrameGrabber( const uno::Reference< lang::XMultiServiceFactory >& rxMgr ) :
67 + mxMgr( rxMgr )
69 + ::CoInitialize( NULL );
72 +// ------------------------------------------------------------------------------
74 +FrameGrabber::~FrameGrabber()
76 + ::CoUninitialize();
79 +// ------------------------------------------------------------------------------
81 +IMediaDet* FrameGrabber::implCreateMediaDet( const ::rtl::OUString& rURL ) const
83 + IMediaDet* pDet = NULL;
85 + if( SUCCEEDED( CoCreateInstance( CLSID_MediaDet, NULL, CLSCTX_INPROC_SERVER, IID_IMediaDet, (void**) &pDet ) ) )
86 + {
87 + String aLocalStr;
89 + if( ::utl::LocalFileHelper::ConvertURLToPhysicalName( rURL, aLocalStr ) && aLocalStr.Len() )
90 + {
91 + if( !SUCCEEDED( pDet->put_Filename( ::SysAllocString( aLocalStr.GetBuffer() ) ) ) )
92 + {
93 + pDet->Release();
94 + pDet = NULL;
95 + }
96 + }
97 + }
99 + return pDet;
102 +// ------------------------------------------------------------------------------
104 +bool FrameGrabber::create( const ::rtl::OUString& rURL )
106 + // just check if a MediaDet interface can be created with the given URL
107 + IMediaDet* pDet = implCreateMediaDet( rURL );
109 + if( pDet )
111 + maURL = rURL;
112 + pDet->Release();
113 + pDet = NULL;
115 + else
116 + maURL = ::rtl::OUString();
118 + return( maURL.getLength() > 0 );
121 +// ------------------------------------------------------------------------------
123 +uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
124 + throw (uno::RuntimeException)
126 + uno::Reference< graphic::XGraphic > xRet;
127 + IMediaDet* pDet = implCreateMediaDet( maURL );
129 + if( pDet )
131 + double fLength;
132 + long nStreamCount;
133 + bool bFound = false;
135 + if( SUCCEEDED( pDet->get_OutputStreams( &nStreamCount ) ) )
137 + for( long n = 0; ( n < nStreamCount ) && !bFound; ++n )
139 + GUID aMajorType;
141 + if( SUCCEEDED( pDet->put_CurrentStream( n ) ) &&
142 + SUCCEEDED( pDet->get_StreamType( &aMajorType ) ) &&
143 + ( aMajorType == MEDIATYPE_Video ) )
145 + bFound = true;
150 + if( bFound &&
151 + ( S_OK == pDet->get_StreamLength( &fLength ) ) &&
152 + ( fLength > 0.0 ) && ( fMediaTime >= 0.0 ) && ( fMediaTime <= fLength ) )
154 + AM_MEDIA_TYPE aMediaType;
155 + long nWidth = 0, nHeight = 0, nSize = 0;
157 + if( SUCCEEDED( pDet->get_StreamMediaType( &aMediaType ) ) )
159 + if( ( aMediaType.formattype == FORMAT_VideoInfo ) &&
160 + ( aMediaType.cbFormat >= sizeof( VIDEOINFOHEADER ) ) )
162 + VIDEOINFOHEADER* pVih = reinterpret_cast< VIDEOINFOHEADER* >( aMediaType.pbFormat );
164 + nWidth = pVih->bmiHeader.biWidth;
165 + nHeight = pVih->bmiHeader.biHeight;
167 + if( nHeight < 0 )
168 + nHeight *= -1;
171 + if( aMediaType.cbFormat != 0 )
173 + ::CoTaskMemFree( (PVOID) aMediaType.pbFormat );
174 + aMediaType.cbFormat = 0;
175 + aMediaType.pbFormat = NULL;
178 + if( aMediaType.pUnk != NULL )
180 + aMediaType.pUnk->Release();
181 + aMediaType.pUnk = NULL;
182 + }
185 + if( ( nWidth > 0 ) && ( nHeight > 0 ) &&
186 + SUCCEEDED( pDet->GetBitmapBits( 0, &nSize, NULL, nWidth, nHeight ) ) &&
187 + ( nSize > 0 ) )
189 + char* pBuffer = new char[ nSize ];
191 + try
193 + if( SUCCEEDED( pDet->GetBitmapBits( fMediaTime, NULL, pBuffer, nWidth, nHeight ) ) )
195 + SvMemoryStream aMemStm( pBuffer, nSize, STREAM_READ | STREAM_WRITE );
196 + Bitmap aBmp;
198 + if( aBmp.Read( aMemStm, false ) && !aBmp.IsEmpty() )
200 + const Graphic aGraphic( aBmp );
201 + xRet = aGraphic.GetXGraphic();
205 + catch( ... )
209 + delete [] pBuffer;
213 + pDet->Release();
216 + return xRet;
219 +// ------------------------------------------------------------------------------
221 +::rtl::OUString SAL_CALL FrameGrabber::getImplementationName( )
222 + throw (uno::RuntimeException)
224 + return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( AVMEDIA_GST_FRAMEGRABBER_IMPLEMENTATIONNAME ) );
227 +// ------------------------------------------------------------------------------
229 +sal_Bool SAL_CALL FrameGrabber::supportsService( const ::rtl::OUString& ServiceName )
230 + throw (uno::RuntimeException)
232 + return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( AVMEDIA_GST_FRAMEGRABBER_SERVICENAME ) );
235 +// ------------------------------------------------------------------------------
237 +uno::Sequence< ::rtl::OUString > SAL_CALL FrameGrabber::getSupportedServiceNames( )
238 + throw (uno::RuntimeException)
240 + uno::Sequence< ::rtl::OUString > aRet(1);
241 + aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( AVMEDIA_GST_FRAMEGRABBER_SERVICENAME ) );
243 + return aRet;
246 +} // namespace gstreamer
247 +} // namespace avmedia