1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 #define WM_GRAPHNOTIFY WM_USER+13
47 #include "iProxyTrans.h"
48 #include "ProxyTransuids.h"
52 IPin* get_pin( IBaseFilter* pFilter, PIN_DIRECTION dir );
53 HWND _cvcamCreateWindow();
55 const int FRAMES_FORMAT = 0;
56 const int TIME_FORMAT = 1;
59 cvcamSourceFile::cvcamSourceFile(const char* file , void (*callback)(void*) ):
62 m_pMediaControl(NULL),
99 fn.lStructSize = sizeof(OPENFILENAME);
101 fn.lpstrFilter = NULL;
104 fn.lpstrFileTitle = NULL;
105 fn.lpstrInitialDir = NULL;
106 fn.lpstrTitle = NULL;
108 fn.lpstrDefExt = "avi";
109 fn.hInstance = DLLhinst;
111 fn.lpstrCustomFilter = NULL;
115 if(!GetOpenFileName(&fn))
120 }//else after if(file)
122 // Create the filter graph manager and query for interfaces.
123 CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
124 IID_IGraphBuilder, (void **)&m_pGraph);
128 mbstowcs(wpath, m_file.c_str(), strlen(m_file.c_str())+1);
131 m_hr = m_pGraph->AddSourceFilter(wpath,L"source",&m_pSFilter);
134 // Create a proxy transform filter
135 if(FAILED(CoCreateInstance(CLSID_ProxyTransform, NULL, CLSCTX_INPROC_SERVER,
136 IID_IProxyTransform, (void**)&m_pProxyTrans)))
142 m_hr = m_pProxyTrans->set_transform((void(__cdecl*)(void*))m_pcallback, 0);
144 //Get Source output pin
145 m_hr = m_pSFilter->EnumPins(&m_pEnumPins);
147 unsigned long fetched(0);
148 m_hr = m_pEnumPins->Next(1,&m_pSourceOut,&fetched);
154 //Put ProxyTrans into graph
155 m_hr = m_pProxyTrans->QueryInterface(IID_IBaseFilter,(void**)&m_pProxyBase);
156 m_hr = m_pGraph->AddFilter(m_pProxyBase.value(),L"proxy");
158 //Get ProxyTrans Pins
159 m_pProxyIn = get_pin( m_pProxyBase.value(), PINDIR_INPUT );
160 m_pProxyOut= get_pin( m_pProxyBase.value(), PINDIR_OUTPUT );
162 m_hr = m_pGraph->Connect(m_pSourceOut.value(),m_pProxyIn.value());
164 m_hr = m_pGraph->Render(m_pProxyOut.value());
168 //Gain additional interfaces
169 m_hr = m_pGraph->QueryInterface(IID_IMediaControl, (void **)&m_pMediaControl);
170 m_hr = m_pGraph->QueryInterface(IID_IMediaEventEx, (void **)&m_pEvent);
171 m_hr = m_pGraph->QueryInterface(IID_IVideoWindow, (void **)&m_pVideoWindow);
172 m_hr = m_pGraph->QueryInterface(IID_IMediaSeeking, (void**)&m_pMediaSeeking);
174 if(!m_pMediaControl.is_valid() ||!m_pEvent.is_valid() ||!m_pVideoWindow.is_valid())
192 bool cvcamSourceFile::IsRunning()
196 HRESULT hr = m_pEvent->WaitForCompletion(0, &evCode);
207 HRESULT cvcamSourceFile::SetWindow(const HWND window)
218 m_hWnd = _cvcamCreateWindow();
226 HRESULT cvcamSourceFile::Start()
238 m_hr = m_pVideoWindow->put_Owner((OAHWND)m_hWnd);
240 m_hr = m_pEvent->SetNotifyWindow((OAHWND)m_hWnd, WM_GRAPHNOTIFY, 0);
241 m_hr = m_pEvent->SetNotifyFlags(0x00);
242 m_hr = m_pEvent->CancelDefaultHandling(EC_COMPLETE);
243 m_hr = m_pVideoWindow->get_WindowStyle(&flags);
244 m_hr = m_pVideoWindow->put_WindowStyle(flags & (~WS_CAPTION) | WS_CHILD);
245 m_hr = m_pVideoWindow->put_MessageDrain((OAHWND)m_hWnd);
247 // Get the rectangle dimensions and resize the client window
248 m_hr = m_pGraph->QueryInterface(IID_IBasicVideo,(void**)&m_pBasicVideo);
250 long left, top, w, h;
251 m_pBasicVideo->GetSourcePosition(&left, &top,&w,&h);
252 m_pBasicVideo->SetDestinationPosition(0, 0, m_width?m_width:w, m_height?m_height:h);
253 m_pVideoWindow->SetWindowPosition(0,0,m_width?m_width:w,m_height?m_height:h);
254 const char* name = cvGetWindowName(m_hWnd);
255 cvResizeWindow(name, m_width?m_width:w, m_height?m_height:h);
260 m_hr = m_pMediaControl->Run();
265 catch(HrExeption exp)
272 HRESULT cvcamSourceFile::Stop()
274 if( m_pMediaControl.is_valid() )
277 m_pMediaControl->GetState(0,&fs);
278 if(fs == State_Stopped)
282 m_pMediaControl->StopWhenReady();
286 if(m_pVideoWindow.is_valid() )
288 m_pVideoWindow->put_Visible(OAFALSE);
289 m_pVideoWindow->put_Owner(NULL);
290 m_pVideoWindow->put_MessageDrain(0);
296 HRESULT cvcamSourceFile::Pause()
298 if( m_pMediaControl.is_valid() )
301 m_pMediaControl->GetState(0,&fs);
302 if(fs == State_Stopped)
304 m_pMediaControl->Pause();
312 HRESULT cvcamSourceFile::Resume()
314 if( m_pMediaControl.is_valid() )
317 m_pMediaControl->GetState(0,&fs);
318 if(fs == State_Stopped)
321 m_pMediaControl->Run();
328 HRESULT cvcamSourceFile::SetCallBack(void (__cdecl *callback)(void *))
333 m_pcallback = callback;
335 m_pProxyTrans->set_transform((void(__cdecl*)(void*))m_pcallback, 0);
340 HRESULT cvcamSourceFile::SetHeight(const int height)
347 HRESULT cvcamSourceFile::SetWidth(const int width)
354 HRESULT cvcamSourceFile::WaitForCompletion()
358 return m_pEvent->WaitForCompletion(INFINITE, &evCode);
362 HRESULT cvcamSourceFile::SetTimeFormat(const int format)
364 if (format == FRAMES_FORMAT)
366 return m_pMediaSeeking->SetTimeFormat(&TIME_FORMAT_FRAME );
368 if (format == TIME_FORMAT)
372 return m_pMediaSeeking->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME);
377 HRESULT cvcamSourceFile::GetDuration(LONGLONG *pDuration)
379 return m_pMediaSeeking->GetDuration(pDuration);
382 HRESULT cvcamSourceFile::GetCurrentPosition(LONGLONG *pCurrent)
385 return m_pMediaSeeking->GetCurrentPosition(pCurrent);
388 HRESULT cvcamSourceFile::SetPosition(LONGLONG *pCurrent)
391 return m_pMediaSeeking->SetPositions(pCurrent,AM_SEEKING_AbsolutePositioning,NULL,
392 AM_SEEKING_NoPositioning);