Merging OCC22105, OCC22354, OCC22150 , OCC22199 , OCC22391 and OCC22108
[occt.git] / src / OpenGl / OpenGl_AVIWriter.hxx
1 // File:      OpenGl_AVIWriter.hxx
2 // Created:   15.04.07 12:53
3 // Author:    Alexey MORENOV & Alexander GRIGORIEV
4 // Copyright: Open CASCADE 2007
5
6 #ifndef __OPENGL_AVIWRITER_H
7 #define __OPENGL_AVIWRITER_H
8
9 #ifdef WNT
10 #define THIS void 
11
12 #include <InterfaceGraphic_WNT.hxx>
13 #include <stdlib.h>
14 #include <vfw.h>
15 #include <TCollection_AsciiString.hxx>
16
17 /**
18  * Class providing the API to record AVI files using a codec installed in the
19  * system -- Only on Windows NT/2k/XP/Vista platform under MS Visual Studio.
20  * The following tasks are supported:
21  * <ol>
22  *
23  * <li> Creation of AVI data stream: launched by the constructor.<br>
24  * The constructor accepts the filename, FOURCC video code and the frame rate
25  * setting as parameters.<br>
26  * The default codec name used here is MPG4. To use a different codec, pass
27  * its FOURCC value as the input parameter for dwCodec.
28  * For example,
29  * <ul>
30  * <li>pass mmioFOURCC('D','I','V','X') to use DIVX codec, or</li>
31  * <li>pass mmioFOURCC('I','V','5','0') to use IV50 codec etc...</li>
32  * </ul>
33  * Also, you can pass just 0 to avoid the codecs altogether. In that case,
34  * the frames would be saved as they are without any compression; However,
35  * the output movie file size would be very huge in that case.
36  * <br>
37  * Finally, make sure you have the codec installed on the machine before
38  * passing its Fourcc here.
39  * </li>
40  * <li>
41  * Start the recording: call the method StartRecording(). This method should be
42  * called at least once; execution of the constructor does not begin the
43  * process.
44  * </li>
45  * <li>
46  * Stop the recording: call the method StopRecording(). Can be omitted if the
47  * next to execute would be the destructor.
48  * </li>
49  * <li>
50  * Close the AVI file and exit the process of recording. This is done
51  * automatically by the destructor.
52  * </li>
53  * </ol>
54  */
55 class OpenGl_AVIWriter
56 {
57 public:
58
59   /**
60    * Constructor. Initializes the internal data structures, prepares for the
61    * creation of an AVI stream when the first frame is ready to be captured.
62    * @param theFileName
63    *   Name of the output movie file to create.
64    * @param theCodec
65    *   FourCC of the Video Codec to be used for compression
66    * @param theFrameRate
67    *   The Frames Per Second (FPS) setting to be used for the movie
68    */
69   Standard_EXPORT OpenGl_AVIWriter(const char * theFileName, 
70                                    DWORD theCodec = mmioFOURCC('M','P','G','4'),
71                                    Standard_Integer theFrameRate = 25);
72
73   /**
74    * Destructor: closes the movie file and flushes all the frames
75    */
76   Standard_EXPORT ~OpenGl_AVIWriter     ();
77
78   /**
79    * Begin the recording.
80    */
81   Standard_EXPORT void    StartRecording(const HANDLE hWin = NULL);
82
83   /**
84    * Stop the recording (can be restarted using StartRecording()).
85    */
86   Standard_EXPORT void    StopRecording ();
87
88   /**
89    * Query the state of recording.
90    */
91   inline Standard_Boolean IsRecording   () const { return myIsAllowRecord; }
92
93   /**
94    * Returns the last error message, if any.
95    */
96   inline Standard_EXPORT const TCollection_AsciiString&
97                           GetLastErrorMessage() const
98   { return myErrMsg; }
99
100   /**
101    * Get the instance of AVI Writer class.
102    */ 
103   static Standard_EXPORT OpenGl_AVIWriter *
104                           GetInstance   ();
105
106   /**
107    * Get the Window handle that contains the actual OpenGl context.
108    */
109   inline HANDLE           HWindow () const
110   { return myhWindow; }
111
112   /// Inserts the given HBitmap into the movie as a new Frame at the end.
113   HRESULT         AppendNewFrame(HBITMAP hBitmap);
114
115   /// Inserts the given bitmap bits into the movie as a new Frame at the end.
116   /// The width, height and nBitsPerPixel are the width, height and bits per pixel
117   /// of the bitmap pointed to by the input pBits.
118   HRESULT         AppendNewFrame(int nWidth,
119                                  int nHeight,
120                                  LPVOID pBits,
121                                  int nBitsPerPixel);
122
123 private:
124
125   void call_avi();
126
127 private:
128   static OpenGl_AVIWriter       * MyAVIWriterInstance;
129   Standard_Boolean                myIsAllowRecord;
130
131   BYTE                          * mypBits;
132   UINT                            myWidth;
133   UINT                            myHeight;
134
135   HDC                   myhAviDC;
136   HANDLE                myhHeap;
137   HANDLE                myhWindow;   // window containing the OGL context 
138   LPVOID                mylpBits;    // Useful to hold the bitmap content if any
139   LONG                  mylSample;   // Keeps track of the current Frame Index
140   PAVIFILE              mypAviFile;
141   PAVISTREAM            mypAviStream;
142   PAVISTREAM            mypAviCompressedStream;
143   AVISTREAMINFO         myAviStreamInfo;
144   AVICOMPRESSOPTIONS    myAviCompressOptions;
145   char *                myFileName; // Holds the Output Movie File Name
146   TCollection_AsciiString myErrMsg; // Holds the Last Error Message, if any
147
148   int                   myAppendFuncSelector;  //0=Dummy 1=FirstTime 2=Usual
149
150   HRESULT               AppendFrameFirstTime(HBITMAP );
151   HRESULT               AppendFrameUsual(HBITMAP);
152   HRESULT               AppendDummy(HBITMAP);
153   HRESULT       (OpenGl_AVIWriter::*pAppendFrame[3])(HBITMAP hBitmap);
154
155   HRESULT               AppendFrameBitsFirstTime(int, int, LPVOID,int );
156   HRESULT               AppendFrameBitsUsual(int, int, LPVOID,int );
157   HRESULT               AppendDummyBits(int, int, LPVOID,int );
158   HRESULT       (OpenGl_AVIWriter::*pAppendFrameBits[3])(int, int, LPVOID, int);
159
160   /// Takes care of creating the memory, streams, compression options etc.
161   /// required for the movie
162   HRESULT InitMovieCreation(int nFrameWidth,int nFrameHeight,int nBitsPerPixel);
163
164   /// Takes care of releasing the memory and movie related handles
165   void ReleaseMemory();
166
167   /// Sets the Error Message
168   void SetErrorMessage(const char * theErrMsg);
169 };
170
171 Standard_EXPORT void OpenGl_AVIWriter_AVIWriter(void * pp,
172                                                 int  nWidth,
173                                                 int  nHeight,
174                                                 int  nBitsPerPixel);
175
176 Standard_EXPORT Standard_Boolean OpenGl_AVIWriter_AllowWriting(void * hWin);
177
178 #endif // WNT
179 #endif