1*f39251c4SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f39251c4SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f39251c4SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f39251c4SAndrew Rist  * distributed with this work for additional information
6*f39251c4SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f39251c4SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f39251c4SAndrew Rist  * "License"); you may not use this file except in compliance
9*f39251c4SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f39251c4SAndrew Rist  *
11*f39251c4SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f39251c4SAndrew Rist  *
13*f39251c4SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f39251c4SAndrew Rist  * software distributed under the License is distributed on an
15*f39251c4SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f39251c4SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f39251c4SAndrew Rist  * specific language governing permissions and limitations
18*f39251c4SAndrew Rist  * under the License.
19*f39251c4SAndrew Rist  *
20*f39251c4SAndrew Rist  *************************************************************/
21*f39251c4SAndrew Rist 
22*f39251c4SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <tools/prewin.h>
25cdf0e10cSrcweir #if defined _MSC_VER
26cdf0e10cSrcweir #pragma warning(push, 1)
27cdf0e10cSrcweir #pragma warning(disable: 4917)
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #include <windows.h>
30cdf0e10cSrcweir #include <objbase.h>
31cdf0e10cSrcweir #include <strmif.h>
32cdf0e10cSrcweir #include <Amvideo.h>
33cdf0e10cSrcweir #if defined(_MSC_VER) && (_MSC_VER < 1500)
34cdf0e10cSrcweir #include <Qedit.h>
35cdf0e10cSrcweir #else
36cdf0e10cSrcweir #include "interface.hxx"
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir #include <uuids.h>
39cdf0e10cSrcweir #if defined _MSC_VER
40cdf0e10cSrcweir #pragma warning(pop)
41cdf0e10cSrcweir #endif
42cdf0e10cSrcweir #include <tools/postwin.h>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include "framegrabber.hxx"
45cdf0e10cSrcweir #include "player.hxx"
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #include <tools/stream.hxx>
48cdf0e10cSrcweir #include <vcl/graph.hxx>
49cdf0e10cSrcweir #include <unotools/localfilehelper.hxx>
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #define AVMEDIA_WIN_FRAMEGRABBER_IMPLEMENTATIONNAME "com.sun.star.comp.avmedia.FrameGrabber_DirectX"
52cdf0e10cSrcweir #define AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME "com.sun.star.media.FrameGrabber_DirectX"
53cdf0e10cSrcweir 
54cdf0e10cSrcweir using namespace ::com::sun::star;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir namespace avmedia { namespace win {
57cdf0e10cSrcweir 
58cdf0e10cSrcweir // ----------------
59cdf0e10cSrcweir // - FrameGrabber -
60cdf0e10cSrcweir // ----------------
61cdf0e10cSrcweir 
62cdf0e10cSrcweir FrameGrabber::FrameGrabber( const uno::Reference< lang::XMultiServiceFactory >& rxMgr ) :
63cdf0e10cSrcweir     mxMgr( rxMgr )
64cdf0e10cSrcweir {
65cdf0e10cSrcweir     ::CoInitialize( NULL );
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir // ------------------------------------------------------------------------------
69cdf0e10cSrcweir 
70cdf0e10cSrcweir FrameGrabber::~FrameGrabber()
71cdf0e10cSrcweir {
72cdf0e10cSrcweir     ::CoUninitialize();
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir // ------------------------------------------------------------------------------
76cdf0e10cSrcweir 
77cdf0e10cSrcweir IMediaDet* FrameGrabber::implCreateMediaDet( const ::rtl::OUString& rURL ) const
78cdf0e10cSrcweir {
79cdf0e10cSrcweir     IMediaDet* pDet = NULL;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     if( SUCCEEDED( CoCreateInstance( CLSID_MediaDet, NULL, CLSCTX_INPROC_SERVER, IID_IMediaDet, (void**) &pDet ) ) )
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir 		String aLocalStr;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 		if( ::utl::LocalFileHelper::ConvertURLToPhysicalName( rURL, aLocalStr ) && aLocalStr.Len() )
86cdf0e10cSrcweir 		{
87cdf0e10cSrcweir             if( !SUCCEEDED( pDet->put_Filename( ::SysAllocString( reinterpret_cast<LPCOLESTR>(aLocalStr.GetBuffer()) ) ) ) )
88cdf0e10cSrcweir             {
89cdf0e10cSrcweir                 pDet->Release();
90cdf0e10cSrcweir                 pDet = NULL;
91cdf0e10cSrcweir             }
92cdf0e10cSrcweir         }
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     return pDet;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir // ------------------------------------------------------------------------------
99cdf0e10cSrcweir 
100cdf0e10cSrcweir bool FrameGrabber::create( const ::rtl::OUString& rURL )
101cdf0e10cSrcweir {
102cdf0e10cSrcweir     // just check if a MediaDet interface can be created with the given URL
103cdf0e10cSrcweir     IMediaDet*  pDet = implCreateMediaDet( rURL );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     if( pDet )
106cdf0e10cSrcweir     {
107cdf0e10cSrcweir         maURL = rURL;
108cdf0e10cSrcweir         pDet->Release();
109cdf0e10cSrcweir         pDet = NULL;
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir     else
112cdf0e10cSrcweir         maURL = ::rtl::OUString();
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     return( maURL.getLength() > 0 );
115cdf0e10cSrcweir }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir // ------------------------------------------------------------------------------
118cdf0e10cSrcweir 
119cdf0e10cSrcweir uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
120cdf0e10cSrcweir     throw (uno::RuntimeException)
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     uno::Reference< graphic::XGraphic > xRet;
123cdf0e10cSrcweir     IMediaDet*                          pDet = implCreateMediaDet( maURL );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     if( pDet )
126cdf0e10cSrcweir     {
127cdf0e10cSrcweir         double  fLength;
128cdf0e10cSrcweir         long    nStreamCount;
129cdf0e10cSrcweir         bool    bFound = false;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         if( SUCCEEDED( pDet->get_OutputStreams( &nStreamCount ) ) )
132cdf0e10cSrcweir         {
133cdf0e10cSrcweir             for( long n = 0; ( n < nStreamCount ) && !bFound; ++n )
134cdf0e10cSrcweir             {
135cdf0e10cSrcweir                 GUID aMajorType;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir                 if( SUCCEEDED( pDet->put_CurrentStream( n ) )  &&
138cdf0e10cSrcweir                     SUCCEEDED( pDet->get_StreamType( &aMajorType ) ) &&
139cdf0e10cSrcweir                     ( aMajorType == MEDIATYPE_Video ) )
140cdf0e10cSrcweir                 {
141cdf0e10cSrcweir                     bFound = true;
142cdf0e10cSrcweir                 }
143cdf0e10cSrcweir             }
144cdf0e10cSrcweir         }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         if( bFound &&
147cdf0e10cSrcweir             ( S_OK == pDet->get_StreamLength( &fLength ) ) &&
148cdf0e10cSrcweir             ( fLength > 0.0 ) && ( fMediaTime >= 0.0 ) && ( fMediaTime <= fLength ) )
149cdf0e10cSrcweir         {
150cdf0e10cSrcweir             AM_MEDIA_TYPE   aMediaType;
151cdf0e10cSrcweir             long            nWidth = 0, nHeight = 0, nSize = 0;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir             if( SUCCEEDED( pDet->get_StreamMediaType( &aMediaType ) ) )
154cdf0e10cSrcweir             {
155cdf0e10cSrcweir                 if( ( aMediaType.formattype == FORMAT_VideoInfo ) &&
156cdf0e10cSrcweir                     ( aMediaType.cbFormat >= sizeof( VIDEOINFOHEADER ) ) )
157cdf0e10cSrcweir                 {
158cdf0e10cSrcweir                     VIDEOINFOHEADER* pVih = reinterpret_cast< VIDEOINFOHEADER* >( aMediaType.pbFormat );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir                     nWidth = pVih->bmiHeader.biWidth;
161cdf0e10cSrcweir                     nHeight = pVih->bmiHeader.biHeight;
162cdf0e10cSrcweir 
163cdf0e10cSrcweir                     if( nHeight < 0 )
164cdf0e10cSrcweir                         nHeight *= -1;
165cdf0e10cSrcweir                 }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir                 if( aMediaType.cbFormat != 0 )
168cdf0e10cSrcweir                 {
169cdf0e10cSrcweir                     ::CoTaskMemFree( (PVOID) aMediaType.pbFormat );
170cdf0e10cSrcweir                     aMediaType.cbFormat = 0;
171cdf0e10cSrcweir                     aMediaType.pbFormat = NULL;
172cdf0e10cSrcweir                 }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir                 if( aMediaType.pUnk != NULL )
175cdf0e10cSrcweir                 {
176cdf0e10cSrcweir                     aMediaType.pUnk->Release();
177cdf0e10cSrcweir                     aMediaType.pUnk = NULL;
178cdf0e10cSrcweir                 }
179cdf0e10cSrcweir             }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir             if( ( nWidth > 0 ) && ( nHeight > 0 ) &&
182cdf0e10cSrcweir                 SUCCEEDED( pDet->GetBitmapBits( 0, &nSize, NULL, nWidth, nHeight ) ) &&
183cdf0e10cSrcweir                 ( nSize > 0  ) )
184cdf0e10cSrcweir             {
185cdf0e10cSrcweir                 char* pBuffer = new char[ nSize ];
186cdf0e10cSrcweir 
187cdf0e10cSrcweir                 try
188cdf0e10cSrcweir                 {
189cdf0e10cSrcweir                     if( SUCCEEDED( pDet->GetBitmapBits( fMediaTime, NULL, pBuffer, nWidth, nHeight ) ) )
190cdf0e10cSrcweir                     {
191cdf0e10cSrcweir                         SvMemoryStream  aMemStm( pBuffer, nSize, STREAM_READ | STREAM_WRITE );
192cdf0e10cSrcweir                         Bitmap          aBmp;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir                         if( aBmp.Read( aMemStm, false ) && !aBmp.IsEmpty() )
195cdf0e10cSrcweir                         {
196cdf0e10cSrcweir                             const Graphic aGraphic( aBmp );
197cdf0e10cSrcweir                             xRet = aGraphic.GetXGraphic();
198cdf0e10cSrcweir                         }
199cdf0e10cSrcweir                     }
200cdf0e10cSrcweir                 }
201cdf0e10cSrcweir                 catch( ... )
202cdf0e10cSrcweir                 {
203cdf0e10cSrcweir                 }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir                 delete [] pBuffer;
206cdf0e10cSrcweir             }
207cdf0e10cSrcweir         }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir         pDet->Release();
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir     return xRet;
213cdf0e10cSrcweir }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir // ------------------------------------------------------------------------------
216cdf0e10cSrcweir 
217cdf0e10cSrcweir ::rtl::OUString SAL_CALL FrameGrabber::getImplementationName(  )
218cdf0e10cSrcweir     throw (uno::RuntimeException)
219cdf0e10cSrcweir {
220cdf0e10cSrcweir     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( AVMEDIA_WIN_FRAMEGRABBER_IMPLEMENTATIONNAME ) );
221cdf0e10cSrcweir }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir // ------------------------------------------------------------------------------
224cdf0e10cSrcweir 
225cdf0e10cSrcweir sal_Bool SAL_CALL FrameGrabber::supportsService( const ::rtl::OUString& ServiceName )
226cdf0e10cSrcweir     throw (uno::RuntimeException)
227cdf0e10cSrcweir {
228cdf0e10cSrcweir     return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME ) );
229cdf0e10cSrcweir }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir // ------------------------------------------------------------------------------
232cdf0e10cSrcweir 
233cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL FrameGrabber::getSupportedServiceNames(  )
234cdf0e10cSrcweir     throw (uno::RuntimeException)
235cdf0e10cSrcweir {
236cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > aRet(1);
237cdf0e10cSrcweir     aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME ) );
238cdf0e10cSrcweir 
239cdf0e10cSrcweir     return aRet;
240cdf0e10cSrcweir }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir } // namespace win
243cdf0e10cSrcweir } // namespace avmedia
244