PTLib  Version 2.12.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
videoio.h
Go to the documentation of this file.
1 /*
2  * videoio.h
3  *
4  * Classes to support streaming video input (grabbing) and output.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-2000 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): Mark Cooke (mpc@star.sr.bham.ac.uk)
25  *
26  * $Revision: 30763 $
27  * $Author: rjongbloed $
28  * $Date: 2013-10-21 12:13:48 +1100 (Mon, 21 Oct 2013) $
29  */
30 
31 
32 #ifndef PTLIB_PVIDEOIO_H
33 #define PTLIB_PVIDEOIO_H
34 
35 #ifdef P_USE_PRAGMA
36 #pragma interface
37 #endif
38 #include <ptbuildopts.h>
39 
40 #if P_VIDEO
41 
42 #include <ptlib/plugin.h>
43 #include <ptlib/pluginmgr.h>
44 #include <list>
45 
46 class PColourConverter;
47 
48 
49 class PVideoFrameInfo : public PObject
50 {
51  PCLASSINFO(PVideoFrameInfo, PObject);
52  public:
53  P_DECLARE_ENUM_EX(ResizeMode,eMaxResizeMode,
54  eScale,0,
55  eCropCentre,
56  eCropTopLeft
57  );
58  friend ostream & operator<<(ostream & strm, ResizeMode mode);
59 
61  SQCIFWidth = 128, SQCIFHeight = 96,
62  QCIFWidth = 176, QCIFHeight = 144,
63  CIFWidth = 352, CIFHeight = 288,
64  CIF4Width = 704, CIF4Height = 576,
65  CIF16Width = 1408, CIF16Height = 1152,
66  HD480Width = 704, HD480Height = 480,
67  i480Width = 704, i480Height = 480,
68  HD720Width = 1280, HD720Height = 720,
69  p720Width = 1280, p720Height = 720,
70  HD1080Width= 1920, HD1080Height= 1080,
71  i1080Width = 1920, i1080Height = 1080,
72  HDTVWidth = 1920, HDTVHeight = 1080,
73  MaxWidth = 1920, MaxHeight = 1200
74  };
75 
79  unsigned frameWidth,
80  unsigned frameHeight,
81  const PString & colourFormat = "YUV420P",
82  unsigned frameRate = 15,
83  ResizeMode resizeMode = eScale
84  );
85 
95  virtual Comparison Compare(
96  const PObject & obj // Object to compare against.
97  ) const;
98 
105  virtual void PrintOn(
106  ostream & strm // Stream to print the object into.
107  ) const;
108 
114  virtual PBoolean SetFrameSize(
115  unsigned width,
116  unsigned height
117  );
118 
124  virtual PBoolean GetFrameSize(
125  unsigned & width,
126  unsigned & height
127  ) const;
128 
133  virtual unsigned GetFrameWidth() const;
134 
139  virtual unsigned GetFrameHeight() const;
140 
146  virtual PBoolean SetFrameSar(
147  unsigned width,
148  unsigned height
149  );
150 
156  virtual PBoolean GetSarSize(
157  unsigned & width,
158  unsigned & height
159  ) const;
160 
165  virtual unsigned GetSarWidth() const;
166 
171  virtual unsigned GetSarHeight() const;
172 
178  virtual PBoolean SetFrameRate(
179  unsigned rate
180  );
181 
186  virtual unsigned GetFrameRate() const;
187 
193  virtual PBoolean SetColourFormat(
194  const PString & colourFormat // New colour format for device.
195  );
196 
201  virtual const PString & GetColourFormat() const;
202 
206  ResizeMode mode
207  ) { if (resizeMode < eMaxResizeMode) resizeMode = mode; }
208 
211  ResizeMode GetResizeMode() const { return resizeMode; }
212 
216  static PINDEX CalculateFrameBytes(
217  unsigned width,
218  unsigned height,
219  const PString & colourFormat
220  );
221 
235  bool Parse(
236  const PString & str
237  );
238 
243  static bool ParseSize(
244  const PString & str,
245  unsigned & width,
246  unsigned & height
247  );
248 
251  static PString AsString(
252  unsigned width,
253  unsigned height
254  );
255 
260  static PStringArray GetSizeNames();
261 
262  protected:
263  unsigned frameWidth;
264  unsigned frameHeight;
265  unsigned sarWidth;
266  unsigned sarHeight;
267  unsigned frameRate;
269  ResizeMode resizeMode;
270 };
271 
272 
274 {
275  PCLASSINFO(PVideoControlInfo, PObject);
276 
277  public:
279  Pan,
280  Tilt,
281  Zoom
282  );
283 
284  Types m_type;
285  long m_min;
286  long m_max;
287  long m_step;
288  long m_default;
289  long m_flags;
290  long m_current;
291 
293  : m_type(EndTypes)
294  , m_min(0)
295  , m_max(0)
296  , m_step(0)
297  , m_default(0)
298  , m_flags(0)
299  , m_current(0)
300  { }
301 
302  bool IsValid() const { return m_max > 0 && m_min < m_max; }
303 };
304 
305 
310 {
311  PCLASSINFO(PVideoInputControl, PObject);
312 
313 public:
315 
316  virtual PBoolean Pan (long value, bool absolute = false);
317  virtual PBoolean Tilt(long value, bool absolute = false);
318  virtual PBoolean Zoom(long value, bool absolute = false);
319 
320  virtual long GetPan() const { return m_control[PVideoControlInfo::Pan ].m_current; }
321  virtual long GetTilt() const { return m_control[PVideoControlInfo::Tilt].m_current; }
322  virtual long GetZoom() const { return m_control[PVideoControlInfo::Zoom].m_current; }
323 
324  virtual void Reset();
325 
326  bool IsValid(PVideoControlInfo::Types type) const { return m_control[type].IsValid(); }
327  const PVideoControlInfo & GetControl(PVideoControlInfo::Types type) const { return m_control[type]; }
328 
329 protected:
330  PVideoControlInfo m_control[PVideoControlInfo::NumTypes];
332 
333 };
334 
338 {
339  PCLASSINFO(PVideoInteractionInfo, PObject);
340 
341  public:
342 
343  typedef enum {
350 
351  static PString AsString(const InputInteractType & type);
352 
354 };
355 
356 
386 {
387  PCLASSINFO(PVideoDevice, PVideoFrameInfo);
388 
389  protected:
392  PVideoDevice();
393 
394 
395  public:
398  virtual ~PVideoDevice();
399 
400  enum VideoFormat {
406  };
407 
410  const PString & GetDeviceName() const
411  { return deviceName; }
412 
415  virtual PStringArray GetDeviceNames() const = 0;
416 
417  struct Attributes {
418  Attributes();
419 
423  int m_hue;
424  int m_gamma;
426  };
427 
428  struct OpenArgs {
429  OpenArgs();
430 
438  unsigned rate;
439  unsigned width;
440  unsigned height;
442  ResizeMode resizeMode;
443  bool flip;
445  };
446 
449  virtual PBoolean OpenFull(
450  const OpenArgs & args,
451  PBoolean startImmediate = true
452  );
453 
456  virtual PBoolean Open(
457  const PString & deviceName,
458  PBoolean startImmediate = true
459  ) = 0;
460 
463  virtual PBoolean IsOpen() = 0;
464 
467  virtual PBoolean Close() = 0;
468 
471  virtual PBoolean Start() = 0;
472 
475  virtual PBoolean Stop() = 0;
476 
477 
478 #if PTRACING
479  friend ostream & operator<<(ostream &, VideoFormat);
480 #endif
481 
487  virtual PBoolean SetVideoFormat(
489  );
490 
495  virtual VideoFormat GetVideoFormat() const;
496 
501  virtual int GetNumChannels();
502 
505  virtual PStringArray GetChannelNames();
506 
514  virtual PBoolean SetChannel(
515  int channelNumber
516  );
517 
522  virtual int GetChannel() const;
523 
529  virtual bool SetFrameInfoConverter(
530  const PVideoFrameInfo & info // New frame infofor device.
531  );
532 
540  const PString & colourFormat // New colour format for device.
541  );
542 
546  virtual PBoolean GetVFlipState();
547 
551  virtual PBoolean SetVFlipState(
552  PBoolean newVFlipState
553  );
554 
561  unsigned & minWidth,
562  unsigned & minHeight,
563  unsigned & maxWidth,
564  unsigned & maxHeight
565  ) ;
566 
567 
574  unsigned width,
575  unsigned height,
576  ResizeMode resizeMode = eMaxResizeMode
577  );
578 
585  unsigned width,
586  unsigned height,
587  PBoolean /*bScaleNotCrop*/
588  ) { return SetFrameSizeConverter(width,height,eScale); }
589 
590 
599  unsigned width,
600  unsigned height
601  );
602 
611  virtual PBoolean SetFrameSize(
612  unsigned width,
613  unsigned height
614  );
615 
621  virtual PBoolean GetFrameSize(
622  unsigned & width,
623  unsigned & height
624  ) const;
625 
631  virtual const PString& GetColourFormat() const;
632 
641  virtual PINDEX GetMaxFrameBytes() = 0;
642 
643 
646  int GetLastError() const { return lastError; }
647 
648 
651  virtual PBoolean CanCaptureVideo() const = 0;
652 
655  virtual bool GetAttributes(
656  Attributes & attributes
657  );
658 
661  virtual bool SetAttributes(
662  const Attributes & attributes
663  );
664 
665 
669  int channelNumber,
671  );
672 
673 
677  void SetPreferredColourFormat(const PString & colourFmt) { preferredColourFormat = colourFmt; }
678 
683 
684  protected:
685  PINDEX GetMaxFrameBytesConverted(PINDEX rawFrameBytes) const;
686 
691  // Preferred native colour format from video input device, empty == no preference
694 
697 
698  private:
699  P_REMOVE_VIRTUAL(int, GetBrightness(), 0);
700  P_REMOVE_VIRTUAL(PBoolean, SetBrightness(unsigned), false);
701  P_REMOVE_VIRTUAL(int, GetWhiteness(), 0);
702  P_REMOVE_VIRTUAL(PBoolean, SetWhiteness(unsigned), false);
703  P_REMOVE_VIRTUAL(int, GetColour(), 0);
704  P_REMOVE_VIRTUAL(PBoolean, SetColour(unsigned), false);
705  P_REMOVE_VIRTUAL(int, GetContrast(), 0);
706  P_REMOVE_VIRTUAL(PBoolean, SetContrast(unsigned), false);
707  P_REMOVE_VIRTUAL(int, GetHue(), 0);
708  P_REMOVE_VIRTUAL(PBoolean, SetHue(unsigned), false);
709  P_REMOVE_VIRTUAL(PBoolean, GetParameters(int *, int *, int *, int *, int *), false);
710 };
711 
712 
716 {
717  PCLASSINFO(PVideoOutputDevice, PVideoDevice);
718 
719  public:
723 
726  virtual ~PVideoOutputDevice() { Close(); };
727 
731  PPluginManager * pluginMgr = NULL
732  );
733 
741  const PString & driverName,
742  PPluginManager * pluginMgr = NULL
743  );
744 
748  const PString & driverName,
749  PPluginManager * pluginMgr = NULL
750  );
751 
752  /* Create the matching video output device that corresponds to the device name.
753 
754  This is typically used with the return values from GetDriversDeviceNames().
755  */
757  const PString & deviceName,
758  const PString & driverName = PString::Empty(),
759  PPluginManager * pluginMgr = NULL
760  );
761 
768  const PString & driverName,
769  const PString & deviceName,
770  PBoolean startImmediate = true,
771  PPluginManager * pluginMgr = NULL
772  );
773 
777  const OpenArgs & args,
778  PBoolean startImmediate = true
779  );
780 
783  virtual PBoolean Close() { return true; }
784 
787  virtual PBoolean Start() { return true; }
788 
791  virtual PBoolean Stop() { return true; }
792 
795  virtual PBoolean CanCaptureVideo() const;
796 
799  virtual PBoolean SetFrameData(
800  unsigned x,
801  unsigned y,
802  unsigned width,
803  unsigned height,
804  const BYTE * data,
805  PBoolean endFrame = true
806  ) = 0;
807  virtual PBoolean SetFrameData(
808  unsigned x,
809  unsigned y,
810  unsigned width,
811  unsigned height,
812  const BYTE * data,
813  PBoolean endFrame,
814  bool & keyFrameNeeded
815  );
816  virtual PBoolean SetFrameData(
817  unsigned x,
818  unsigned y,
819  unsigned width,
820  unsigned height,
821  unsigned sarWidth,
822  unsigned sarHeight,
823  const BYTE * data,
824  PBoolean endFrame,
825  bool & keyFrameNeeded,
826  const void * mark
827  );
828 
835  virtual PBoolean DisableDecode();
836 
843  virtual PBoolean GetPosition(
844  int & x, // X position of device surface
845  int & y // Y position of device surface
846  ) const;
847 
854  virtual bool SetPosition(
855  int x, // X position of device surface
856  int y // Y position of device surface
857  );
858 };
859 
860 
864 {
866 
867  public:
871 
882  virtual PBoolean SetColourFormat(
883  const PString & colourFormat // New colour format for device.
884  );
885 
894  virtual PBoolean SetFrameSize(
895  unsigned width,
896  unsigned height
897  );
898 
904  virtual PINDEX GetMaxFrameBytes();
905 
908  virtual PBoolean SetFrameData(
909  unsigned x,
910  unsigned y,
911  unsigned width,
912  unsigned height,
913  const BYTE * data,
914  PBoolean endFrame = true
915  );
916 
919  virtual PBoolean FrameComplete() = 0;
920 
921  protected:
926 };
927 
928 
929 #ifdef SHOULD_BE_MOVED_TO_PLUGIN
930 
933 class PVideoOutputDevicePPM : public PVideoOutputDeviceRGB
934 {
935  PCLASSINFO(PVideoOutputDevicePPM, PVideoOutputDeviceRGB);
936 
937  public:
940  PVideoOutputDevicePPM();
941 
944  virtual PBoolean Open(
945  const PString & deviceName,
946  PBoolean startImmediate = true
947  );
948 
951  virtual PBoolean IsOpen();
952 
955  virtual PBoolean Close();
956 
959  virtual PStringArray GetDeviceNames() const;
960 
963  virtual PBoolean EndFrame();
964 
965  protected:
966  unsigned frameNumber;
967 };
968 
969 #endif // SHOULD_BE_MOVED_TO_PLUGIN
970 
971 
975 {
976  PCLASSINFO(PVideoInputDevice, PVideoDevice);
977 
978  public:
981  //PVideoInputDevice();
982 
986 
990  PPluginManager * pluginMgr = NULL
991  );
992 
1000  const PString & driverName,
1001  PPluginManager * pluginMgr = NULL
1002  );
1003 
1007  const PString & driverName,
1008  PPluginManager * pluginMgr = NULL
1009  );
1010 
1011  /* Create the matching video input device that corresponds to the device name.
1012  So, for "fake" return a device that will generate fake video.
1013  For "Phillips 680 webcam" (eg) will return appropriate grabber.
1014  Note that Phillips will return the appropriate grabber also.
1015 
1016  This is typically used with the return values from GetDriversDeviceNames().
1017  */
1019  const PString & deviceName,
1020  const PString & driverName = PString::Empty(),
1021  PPluginManager * pluginMgr = NULL
1022  );
1023 
1030  const PString & driverName,
1031  const PString & deviceName,
1032  PBoolean startImmediate = true,
1033  PPluginManager * pluginMgr = NULL
1034  );
1035 
1039  const OpenArgs & args,
1040  PBoolean startImmediate = true
1041  );
1042 
1043  typedef struct {
1044  std::list<PVideoFrameInfo> framesizes;
1045  std::list<PVideoControlInfo> controls;
1046  std::list<PVideoInteractionInfo> interactions;
1047  } Capabilities;
1048 
1052  Capabilities * capabilities
1053  ) const { return GetDeviceCapabilities(GetDeviceName(), capabilities); }
1054 
1058  const PString & deviceName,
1059  Capabilities * capabilities,
1060  PPluginManager * pluginMgr = NULL
1061  );
1062 
1066  const PString & deviceName,
1067  const PString & driverName,
1068  Capabilities * caps,
1069  PPluginManager * pluginMgr = NULL
1070  );
1071 
1076 
1079  virtual PBoolean Open(
1080  const PString & deviceName,
1081  PBoolean startImmediate = true
1082  ) = 0;
1083 
1084  virtual PBoolean Close(
1085  ) { return true; }
1086 
1089  virtual PBoolean CanCaptureVideo() const;
1090 
1093  virtual PBoolean IsCapturing() = 0;
1094 
1102  virtual PBoolean SetNearestFrameSize(
1103  unsigned width,
1104  unsigned height
1105  );
1106 
1109  virtual PBoolean GetFrame(
1110  PBYTEArray & frame
1111  );
1112 
1115  virtual PBoolean GetFrameData(
1116  BYTE * buffer,
1117  PINDEX * bytesReturned,
1118  bool & keyFrame
1120  );
1121  virtual PBoolean GetFrameData(
1122  BYTE * buffer,
1123  PINDEX * bytesReturned = NULL
1124  ) = 0;
1125 
1128  virtual PBoolean GetFrameDataNoDelay(
1129  BYTE * buffer,
1130  PINDEX * bytesReturned,
1131  bool & keyFrame
1133  );
1134  virtual PBoolean GetFrameDataNoDelay(
1135  BYTE * buffer,
1136  PINDEX * bytesReturned = NULL
1137  ) = 0;
1138 
1141  virtual bool FlowControl(const void * flowData);
1142 
1155  virtual bool SetCaptureMode(unsigned mode);
1156 
1160  virtual int GetCaptureMode() const;
1161 
1162  P_REMOVE_VIRTUAL(PBoolean, GetFrameData(BYTE *, PINDEX *, unsigned &), false);
1163  P_REMOVE_VIRTUAL(PBoolean, GetFrameDataNoDelay(BYTE *, PINDEX *, unsigned &), false);
1164 };
1165 
1166 
1168 //
1169 // declare macros and structures needed for video input plugins
1170 //
1171 
1173 {
1174  public:
1175  virtual PObject * CreateInstance(int /*userData*/) const { return new className; }
1176  virtual PStringArray GetDeviceNames(int /*userData*/) const { return className::GetInputDeviceNames(); }
1177  virtual bool GetDeviceCapabilities(const PString & deviceName, void * caps) const
1178  { return className::GetDeviceCapabilities(deviceName, (PVideoInputDevice::Capabilities *)caps); }
1179 };
1180 
1181 #define PCREATE_VIDINPUT_PLUGIN(name) \
1182  static PVideoInputPluginServiceDescriptor<PVideoInputDevice_##name> PVideoInputDevice_##name##_descriptor; \
1183  PCREATE_PLUGIN(name, PVideoInputDevice, &PVideoInputDevice_##name##_descriptor)
1184 
1185 
1186 #define P_FAKE_VIDEO_DRIVER "FakeVideo"
1187 #define P_FAKE_VIDEO_PREFIX "Fake/"
1188 #define P_FAKE_VIDEO_MOVING_BLOCKS P_FAKE_VIDEO_PREFIX"MovingBlocks"
1189 #define P_FAKE_VIDEO_MOVING_LINE P_FAKE_VIDEO_PREFIX"MovingLine"
1190 #define P_FAKE_VIDEO_BOUNCING_BOXES P_FAKE_VIDEO_PREFIX"BouncingBoxes"
1191 #define P_FAKE_VIDEO_SOLID_COLOUR P_FAKE_VIDEO_PREFIX"SolidColour"
1192 #define P_FAKE_VIDEO_TEXT P_FAKE_VIDEO_PREFIX"Text"
1193 #define P_FAKE_VIDEO_NTSC P_FAKE_VIDEO_PREFIX"NTSCTest"
1194 
1196 
1197 #ifdef P_APPSHARE
1198  #define P_APPLICATION_VIDEO_DRIVER "Application"
1199  PPLUGIN_STATIC_LOAD(Application, PVideoInputDevice);
1200 #endif
1201 
1202 #if P_FFVDEV
1204 #endif
1205 
1206 #if P_VIDFILE
1207  #define P_VIDEO_FILE_DRIVER "YUVFile"
1209 #endif
1210 
1211 #ifdef WIN32
1212  #define P_VIDEO_FOR_WINDOWS_DRIVER "VideoForWindows"
1213  PPLUGIN_STATIC_LOAD(VideoForWindows, PVideoInputDevice);
1214 #endif
1215 
1216 #ifdef P_DIRECTSHOW
1217  #define P_DIRECT_SHOW_DRIVER "DirectShow"
1219 #endif
1220 
1221 
1223 //
1224 // declare macros and structures needed for video output plugins
1225 //
1226 
1228 {
1229  public:
1230  virtual PObject * CreateInstance(int /*userData*/) const { return new className; }
1231  virtual PStringArray GetDeviceNames(int /*userData*/) const { return className::GetOutputDeviceNames(); }
1232 };
1233 
1234 #define PCREATE_VIDOUTPUT_PLUGIN(name) \
1235  static PVideoOutputPluginServiceDescriptor<PVideoOutputDevice_##name> PVideoOutputDevice_##name##_descriptor; \
1236  PCREATE_PLUGIN(name, PVideoOutputDevice, &PVideoOutputDevice_##name##_descriptor)
1237 
1238 #define P_NULL_VIDEO_DRIVER "NULLOutput"
1239 #define P_NULL_VIDEO_DEVICE "Null Video Out"
1241 
1242 
1243 #if P_VFW_CAPTURE
1244 #if _WIN32
1245  #define P_MSWIN_VIDEO_DRIVER "Window"
1246  #define P_MSWIN_VIDEO_PREFIX "MSWIN"
1247  #define P_MSWIN_VIDEO_DEVICE(x,y,width,height) P_MSWIN_VIDEO_PREFIX " X=" x " Y=" y " WIDTH=" width " HEIGHT=" height
1249 #endif
1250 #endif
1251 
1252 #if P_SDL
1253  #if P_MACOSX
1254  #include <SDL_main.h>
1255  #endif
1256  #define P_SDL_VIDEO_DRIVER "SDL"
1257  #define P_SDL_VIDEO_PREFIX "SDL"
1258  #define P_SDL_VIDEO_DEVICE(x,y,width,height) P_SDL_VIDEO_PREFIX " X=" x " Y=" y " WIDTH=" width " HEIGHT=" height
1260 #endif
1261 
1262 
1264 //
1265 // declare classes needed for access to simple video font
1266 //
1267 
1268 class PVideoFont : public PObject
1269 {
1270  PCLASSINFO(PVideoFont, PObject);
1271  public:
1272  enum {
1274  };
1275  struct LetterData {
1276  char ascii;
1277  const char *line[MAX_L_HEIGHT];
1278  };
1279 
1280  static const LetterData * GetLetterData(char ascii);
1281 };
1282 
1283 #endif // P_VIDEO
1284 
1285 #endif // PTLIB_PVIDEOIO_H
1286 
1287 // End Of File ///////////////////////////////////////////////////////////////
1288