Update the changelog
[opencv] / otherlibs / cvcam / src / windows / cvcamavi.h
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 #ifndef __CVCAMAVI_H__
43 #define __CVCAMAVI_H__
44
45
46 #include <string>
47 using namespace std ;
48
49
50 /////////////////////////////////////////////////////////////////////////////////////////
51 template <class T> class SafePointer
52 {
53     protected:
54         T* m_pointer;
55         
56     public:
57         SafePointer() : m_pointer(0) {};
58         SafePointer(T* pointer) {m_pointer = pointer;};
59         SafePointer(const SafePointer& p) 
60         {
61             m_pointer = p.m_pointer;
62             if(m_pointer)
63             {
64                 m_pointer->AddRef();
65             }
66         };
67         ~SafePointer() 
68         {
69             if(m_pointer) 
70             {
71                 m_pointer->Release();
72                 m_pointer = NULL;
73             }//if(m_pointer)
74             
75         };
76         
77         T* value() {return m_pointer;};
78         T* operator ->() {return m_pointer;};
79         T** operator &() 
80         {
81             if(m_pointer) 
82             {
83                 m_pointer->Release();
84                 m_pointer = NULL;
85             }//if(m_pointer)
86             
87             return &m_pointer;
88         };
89         void operator =(T* pointer) 
90         {
91             if(m_pointer) m_pointer->Release();
92             m_pointer = pointer;
93         };
94         int is_valid() {return int(m_pointer);};
95 };
96 /////////////////////////////////////////////////////////////////////////////////////////
97 class cvcamUseCom
98 {
99 public:
100     cvcamUseCom()
101     {
102         CoInitialize(0);
103     }
104     
105     ~cvcamUseCom()
106     {
107         CoUninitialize();;
108     }
109 };
110 /////////////////////////////////////////////////////////////////////////////////////////
111 class HrExeption
112 {
113 public:
114     HRESULT hr;
115     int occasion;
116 };
117 /////////////////////////////////////////////////////////////////////////////////////////
118 class SafeHr
119 {
120 public:
121     SafeHr():
122       m_occasion(0)
123       {
124       }
125     
126       HRESULT operator=(HRESULT res)
127     {
128         ++m_occasion;       
129         
130         if (FAILED(res)) 
131         {
132             HrExeption excpt;
133             excpt.hr = res;
134             excpt.occasion = m_occasion;
135             throw excpt;
136
137         }
138
139         return res;
140     }
141 protected:
142     int m_occasion;
143
144 };
145 /////////////////////////////////////////////////////////////////////////////////////////
146
147 class cvcamSourceFile
148 {
149 public:
150         HRESULT WaitForCompletion();
151                                 
152         HRESULT SetWindow(const HWND window);
153     cvcamSourceFile(const char* file = NULL, void (*callback)(void*) = NULL);
154     
155     //Media controlling methods
156     HRESULT Start();
157     HRESULT Stop();
158     HRESULT Pause();
159     HRESULT Resume();
160
161     //Property settings
162     HRESULT SetCallBack(void (*callback)(void*));
163     HRESULT SetWidth(const int width);
164     HRESULT SetHeight(const int height);
165     bool IsRunning();
166     bool IsValid;
167
168     //Seeking methods
169     HRESULT SetTimeFormat(const int format);
170     HRESULT GetDuration(LONGLONG *pDuration);
171     HRESULT GetCurrentPosition(LONGLONG *pCurrent);
172     HRESULT SetPosition(LONGLONG *pCurrent);
173         
174     
175     
176     
177     
178 protected:
179         HWND m_hWnd;
180
181     //Initialise/Release COM
182     cvcamUseCom Iusecom;
183
184     //DirectShow Interfaces
185     SafePointer<IGraphBuilder>   m_pGraph;
186     SafePointer<IMediaControl>   m_pMediaControl;
187     SafePointer<IMediaEventEx>   m_pEvent;
188     SafePointer<IVideoWindow>    m_pVideoWindow;
189     SafePointer<IPin>            m_pPin;
190     SafePointer<IBasicVideo>     m_pBasicVideo;
191     SafePointer<IBaseFilter>     m_pSFilter;
192     SafePointer<IProxyTransform> m_pProxyTrans;
193     SafePointer<IPin>            m_pSourceOut;  
194     SafePointer<IPin>            m_pProxyIn;
195     SafePointer<IPin>            m_pProxyOut;
196     SafePointer<IEnumPins>       m_pEnumPins;
197     SafePointer<IBaseFilter>     m_pProxyBase;
198     SafePointer<IMediaSeeking>   m_pMediaSeeking;
199
200     //Data
201     string m_file;
202     void (*m_pcallback)(void*);
203     int m_width;
204     int m_height;
205     SafeHr m_hr;
206 private:
207 };
208
209 /////////////////////////////////////////////////////////////////////////////////////////
210 #define MAX_AVIS 100
211 #define AVIS_START 1000
212 typedef unsigned int uint;
213 class cvcamAVIs
214 {
215 public:
216     cvcamAVIs()
217     {
218        memset(files,0,MAX_AVIS);
219     };
220     
221     uint AddSource( char* path, void (*callback)(void*) )
222     {
223         cvcamSourceFile* tmp = new cvcamSourceFile(path, callback);
224
225         if (!tmp) 
226             return -1;
227         if(!tmp->IsValid)
228             return -1;
229         
230         uint i=0;
231         
232         while (files[i])
233             i++;
234         if(i>= MAX_AVIS)
235             return (uint)-1;
236
237         files[i] = tmp;
238
239         return i+AVIS_START;
240             
241         
242         
243     }
244     bool ReleaseSource(uint source)
245     {
246 //        source -= AVIS_START;
247         
248         if (files[source]) 
249         {
250             delete files[source];
251             files[source] = NULL;
252             return true;
253         }
254
255         return false;
256     
257     }
258     cvcamSourceFile* operator[](uint source)
259     {
260 //        source -= AVIS_START;
261
262         if( source < MAX_AVIS )
263             return files[source];
264         
265         return NULL;
266     
267     }
268
269     ~cvcamAVIs()
270     {
271 //        for(uint i = AVIS_START; i < AVIS_START+MAX_AVIS; i++)
272                 for(uint i = 0; i < MAX_AVIS; i++)
273             ReleaseSource(i);
274     }
275 protected:
276     cvcamSourceFile* files[MAX_AVIS];
277     
278 private:
279 };
280 /////////////////////////////////////////////////////////////////////////////////////////
281 extern HINSTANCE DLLhinst;
282
283
284 #endif //__CVCAMAVI_H__