1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
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,
34 ************************************************************************/
36 #include <tools/prewin.h>
43 #include <tools/postwin.h>
45 #include "framegrabber.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
{
63 FrameGrabber::FrameGrabber( const uno::Reference
< lang::XMultiServiceFactory
>& rxMgr
) :
66 ::CoInitialize( NULL
);
69 // ------------------------------------------------------------------------------
71 FrameGrabber::~FrameGrabber()
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
) ) )
86 if( ::utl::LocalFileHelper::ConvertURLToPhysicalName( rURL
, aLocalStr
) && aLocalStr
.Len() )
88 if( !SUCCEEDED( pDet
->put_Filename( ::SysAllocString( aLocalStr
.GetBuffer() ) ) ) )
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
);
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
);
132 if( SUCCEEDED( pDet
->get_OutputStreams( &nStreamCount
) ) )
134 for( long n
= 0; ( n
< nStreamCount
) && !bFound
; ++n
)
138 if( SUCCEEDED( pDet
->put_CurrentStream( n
) ) &&
139 SUCCEEDED( pDet
->get_StreamType( &aMajorType
) ) &&
140 ( aMajorType
== MEDIATYPE_Video
) )
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
;
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
) ) &&
186 char* pBuffer
= new char[ nSize
];
190 if( SUCCEEDED( pDet
->GetBitmapBits( fMediaTime
, NULL
, pBuffer
, nWidth
, nHeight
) ) )
192 SvMemoryStream
aMemStm( pBuffer
, nSize
, STREAM_READ
| STREAM_WRITE
);
195 if( aBmp
.Read( aMemStm
, false ) && !aBmp
.IsEmpty() )
197 const Graphic
aGraphic( aBmp
);
198 xRet
= aGraphic
.GetXGraphic();
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
) );
243 } // namespace gstreamer
244 } // namespace avmedia