1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #pragma warning(push, 1)
22 #pragma warning(disable: 4917)
29 #include "interface.hxx"
35 #include "framegrabber.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
{
55 FrameGrabber::FrameGrabber( const uno::Reference
< lang::XMultiServiceFactory
>& rxMgr
) :
58 ::CoInitialize( NULL
);
61 FrameGrabber::~FrameGrabber()
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
) ) )
74 if( ::utl::LocalFileHelper::ConvertURLToPhysicalName( rURL
, aLocalStr
) && !aLocalStr
.isEmpty() )
76 if( !SUCCEEDED( pDet
->put_Filename( ::SysAllocString( reinterpret_cast<LPCOLESTR
>(aLocalStr
.getStr()) ) ) ) )
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
);
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
);
116 if( SUCCEEDED( pDet
->get_OutputStreams( &nStreamCount
) ) )
118 for( long n
= 0; ( n
< nStreamCount
) && !bFound
; ++n
)
122 if( SUCCEEDED( pDet
->put_CurrentStream( n
) ) &&
123 SUCCEEDED( pDet
->get_StreamType( &aMajorType
) ) &&
124 ( aMajorType
== MEDIATYPE_Video
) )
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
;
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
) ) &&
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
);
179 if( ReadDIB(aBmp
, aMemStm
, false ) && !aBmp
.IsEmpty() )
181 const Graphic
aGraphic( aBmp
);
182 xRet
= aGraphic
.GetXGraphic();
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
;
222 } // namespace avmedia
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */