fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / avmedia / source / win / framegrabber.cxx
blob63303827d76b6c6176236821b6de79e1fa4ea219
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #if defined _MSC_VER
21 #pragma warning(push, 1)
22 #pragma warning(disable: 4917)
23 #endif
24 #include <prewin.h>
25 #include <postwin.h>
26 #include <objbase.h>
27 #include <strmif.h>
28 #include <Amvideo.h>
29 #include "interface.hxx"
30 #include <uuids.h>
31 #if defined _MSC_VER
32 #pragma warning(pop)
33 #endif
35 #include "framegrabber.hxx"
36 #include "player.hxx"
38 #include <cppuhelper/supportsservice.hxx>
39 #include <tools/stream.hxx>
40 #include <vcl/graph.hxx>
41 #include <unotools/localfilehelper.hxx>
42 #include <vcl/dibtools.hxx>
44 #define AVMEDIA_WIN_FRAMEGRABBER_IMPLEMENTATIONNAME "com.sun.star.comp.avmedia.FrameGrabber_DirectX"
45 #define AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME "com.sun.star.media.FrameGrabber_DirectX"
47 using namespace ::com::sun::star;
49 namespace avmedia { namespace win {
52 // - FrameGrabber -
55 FrameGrabber::FrameGrabber( const uno::Reference< lang::XMultiServiceFactory >& rxMgr ) :
56 mxMgr( rxMgr )
58 ::CoInitialize( NULL );
61 FrameGrabber::~FrameGrabber()
63 ::CoUninitialize();
66 IMediaDet* FrameGrabber::implCreateMediaDet( const OUString& rURL ) const
68 IMediaDet* pDet = NULL;
70 if( SUCCEEDED( CoCreateInstance( CLSID_MediaDet, NULL, CLSCTX_INPROC_SERVER, IID_IMediaDet, (void**) &pDet ) ) )
72 OUString aLocalStr;
74 if( ::utl::LocalFileHelper::ConvertURLToPhysicalName( rURL, aLocalStr ) && !aLocalStr.isEmpty() )
76 if( !SUCCEEDED( pDet->put_Filename( ::SysAllocString( reinterpret_cast<LPCOLESTR>(aLocalStr.getStr()) ) ) ) )
78 pDet->Release();
79 pDet = NULL;
84 return pDet;
87 bool FrameGrabber::create( const OUString& rURL )
89 // just check if a MediaDet interface can be created with the given URL
90 IMediaDet* pDet = implCreateMediaDet( rURL );
92 if( pDet )
94 maURL = rURL;
95 pDet->Release();
96 pDet = NULL;
98 else
99 maURL.clear();
101 return !maURL.isEmpty();
104 uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
105 throw (uno::RuntimeException)
107 uno::Reference< graphic::XGraphic > xRet;
108 IMediaDet* pDet = implCreateMediaDet( maURL );
110 if( pDet )
112 double fLength;
113 long nStreamCount;
114 bool bFound = false;
116 if( SUCCEEDED( pDet->get_OutputStreams( &nStreamCount ) ) )
118 for( long n = 0; ( n < nStreamCount ) && !bFound; ++n )
120 GUID aMajorType;
122 if( SUCCEEDED( pDet->put_CurrentStream( n ) ) &&
123 SUCCEEDED( pDet->get_StreamType( &aMajorType ) ) &&
124 ( aMajorType == MEDIATYPE_Video ) )
126 bFound = true;
131 if( bFound &&
132 ( S_OK == pDet->get_StreamLength( &fLength ) ) &&
133 ( fLength > 0.0 ) && ( fMediaTime >= 0.0 ) && ( fMediaTime <= fLength ) )
135 AM_MEDIA_TYPE aMediaType;
136 long nWidth = 0, nHeight = 0, nSize = 0;
138 if( SUCCEEDED( pDet->get_StreamMediaType( &aMediaType ) ) )
140 if( ( aMediaType.formattype == FORMAT_VideoInfo ) &&
141 ( aMediaType.cbFormat >= sizeof( VIDEOINFOHEADER ) ) )
143 VIDEOINFOHEADER* pVih = reinterpret_cast< VIDEOINFOHEADER* >( aMediaType.pbFormat );
145 nWidth = pVih->bmiHeader.biWidth;
146 nHeight = pVih->bmiHeader.biHeight;
148 if( nHeight < 0 )
149 nHeight *= -1;
152 if( aMediaType.cbFormat != 0 )
154 ::CoTaskMemFree( (PVOID) aMediaType.pbFormat );
155 aMediaType.cbFormat = 0;
156 aMediaType.pbFormat = NULL;
159 if( aMediaType.pUnk != NULL )
161 aMediaType.pUnk->Release();
162 aMediaType.pUnk = NULL;
166 if( ( nWidth > 0 ) && ( nHeight > 0 ) &&
167 SUCCEEDED( pDet->GetBitmapBits( 0, &nSize, NULL, nWidth, nHeight ) ) &&
168 ( nSize > 0 ) )
170 char* pBuffer = new char[ nSize ];
174 if( SUCCEEDED( pDet->GetBitmapBits( fMediaTime, NULL, pBuffer, nWidth, nHeight ) ) )
176 SvMemoryStream aMemStm( pBuffer, nSize, StreamMode::READ | StreamMode::WRITE );
177 Bitmap aBmp;
179 if( ReadDIB(aBmp, aMemStm, false ) && !aBmp.IsEmpty() )
181 const Graphic aGraphic( aBmp );
182 xRet = aGraphic.GetXGraphic();
186 catch( ... )
190 delete [] pBuffer;
194 pDet->Release();
197 return xRet;
200 OUString SAL_CALL FrameGrabber::getImplementationName( )
201 throw (uno::RuntimeException)
203 return OUString( AVMEDIA_WIN_FRAMEGRABBER_IMPLEMENTATIONNAME );
206 sal_Bool SAL_CALL FrameGrabber::supportsService( const OUString& ServiceName )
207 throw (uno::RuntimeException)
209 return cppu::supportsService(this, ServiceName);
212 uno::Sequence< OUString > SAL_CALL FrameGrabber::getSupportedServiceNames( )
213 throw (uno::RuntimeException)
215 uno::Sequence< OUString > aRet(1);
216 aRet[0] = AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME ;
218 return aRet;
221 } // namespace win
222 } // namespace avmedia
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */