fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / avmedia / source / win / framegrabber.cxx
blobe72fc16058db522c13b60122e0b7dfa43398bebb
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 <tools/stream.hxx>
39 #include <vcl/graph.hxx>
40 #include <unotools/localfilehelper.hxx>
42 #define AVMEDIA_WIN_FRAMEGRABBER_IMPLEMENTATIONNAME "com.sun.star.comp.avmedia.FrameGrabber_DirectX"
43 #define AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME "com.sun.star.media.FrameGrabber_DirectX"
45 using namespace ::com::sun::star;
47 namespace avmedia { namespace win {
49 // ----------------
50 // - FrameGrabber -
51 // ----------------
53 FrameGrabber::FrameGrabber( const uno::Reference< lang::XMultiServiceFactory >& rxMgr ) :
54 mxMgr( rxMgr )
56 ::CoInitialize( NULL );
59 // ------------------------------------------------------------------------------
61 FrameGrabber::~FrameGrabber()
63 ::CoUninitialize();
66 // ------------------------------------------------------------------------------
68 IMediaDet* FrameGrabber::implCreateMediaDet( const OUString& rURL ) const
70 IMediaDet* pDet = NULL;
72 if( SUCCEEDED( CoCreateInstance( CLSID_MediaDet, NULL, CLSCTX_INPROC_SERVER, IID_IMediaDet, (void**) &pDet ) ) )
74 OUString aLocalStr;
76 if( ::utl::LocalFileHelper::ConvertURLToPhysicalName( rURL, aLocalStr ) && !aLocalStr.isEmpty() )
78 if( !SUCCEEDED( pDet->put_Filename( ::SysAllocString( reinterpret_cast<LPCOLESTR>(aLocalStr.getStr()) ) ) ) )
80 pDet->Release();
81 pDet = NULL;
86 return pDet;
89 // ------------------------------------------------------------------------------
91 bool FrameGrabber::create( const OUString& rURL )
93 // just check if a MediaDet interface can be created with the given URL
94 IMediaDet* pDet = implCreateMediaDet( rURL );
96 if( pDet )
98 maURL = rURL;
99 pDet->Release();
100 pDet = NULL;
102 else
103 maURL = OUString();
105 return( maURL.getLength() > 0 );
108 // ------------------------------------------------------------------------------
110 uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
111 throw (uno::RuntimeException)
113 uno::Reference< graphic::XGraphic > xRet;
114 IMediaDet* pDet = implCreateMediaDet( maURL );
116 if( pDet )
118 double fLength;
119 long nStreamCount;
120 bool bFound = false;
122 if( SUCCEEDED( pDet->get_OutputStreams( &nStreamCount ) ) )
124 for( long n = 0; ( n < nStreamCount ) && !bFound; ++n )
126 GUID aMajorType;
128 if( SUCCEEDED( pDet->put_CurrentStream( n ) ) &&
129 SUCCEEDED( pDet->get_StreamType( &aMajorType ) ) &&
130 ( aMajorType == MEDIATYPE_Video ) )
132 bFound = true;
137 if( bFound &&
138 ( S_OK == pDet->get_StreamLength( &fLength ) ) &&
139 ( fLength > 0.0 ) && ( fMediaTime >= 0.0 ) && ( fMediaTime <= fLength ) )
141 AM_MEDIA_TYPE aMediaType;
142 long nWidth = 0, nHeight = 0, nSize = 0;
144 if( SUCCEEDED( pDet->get_StreamMediaType( &aMediaType ) ) )
146 if( ( aMediaType.formattype == FORMAT_VideoInfo ) &&
147 ( aMediaType.cbFormat >= sizeof( VIDEOINFOHEADER ) ) )
149 VIDEOINFOHEADER* pVih = reinterpret_cast< VIDEOINFOHEADER* >( aMediaType.pbFormat );
151 nWidth = pVih->bmiHeader.biWidth;
152 nHeight = pVih->bmiHeader.biHeight;
154 if( nHeight < 0 )
155 nHeight *= -1;
158 if( aMediaType.cbFormat != 0 )
160 ::CoTaskMemFree( (PVOID) aMediaType.pbFormat );
161 aMediaType.cbFormat = 0;
162 aMediaType.pbFormat = NULL;
165 if( aMediaType.pUnk != NULL )
167 aMediaType.pUnk->Release();
168 aMediaType.pUnk = NULL;
172 if( ( nWidth > 0 ) && ( nHeight > 0 ) &&
173 SUCCEEDED( pDet->GetBitmapBits( 0, &nSize, NULL, nWidth, nHeight ) ) &&
174 ( nSize > 0 ) )
176 char* pBuffer = new char[ nSize ];
180 if( SUCCEEDED( pDet->GetBitmapBits( fMediaTime, NULL, pBuffer, nWidth, nHeight ) ) )
182 SvMemoryStream aMemStm( pBuffer, nSize, STREAM_READ | STREAM_WRITE );
183 Bitmap aBmp;
185 if( aBmp.Read( aMemStm, false ) && !aBmp.IsEmpty() )
187 const Graphic aGraphic( aBmp );
188 xRet = aGraphic.GetXGraphic();
192 catch( ... )
196 delete [] pBuffer;
200 pDet->Release();
203 return xRet;
206 // ------------------------------------------------------------------------------
208 OUString SAL_CALL FrameGrabber::getImplementationName( )
209 throw (uno::RuntimeException)
211 return OUString( AVMEDIA_WIN_FRAMEGRABBER_IMPLEMENTATIONNAME );
214 // ------------------------------------------------------------------------------
216 sal_Bool SAL_CALL FrameGrabber::supportsService( const OUString& ServiceName )
217 throw (uno::RuntimeException)
219 return ServiceName == AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME;
222 // ------------------------------------------------------------------------------
224 uno::Sequence< OUString > SAL_CALL FrameGrabber::getSupportedServiceNames( )
225 throw (uno::RuntimeException)
227 uno::Sequence< OUString > aRet(1);
228 aRet[0] = AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME ;
230 return aRet;
233 } // namespace win
234 } // namespace avmedia
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */