PTLib  Version 2.14.3
 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: 32467 $
27  * $Author: rjongbloed $
28  * $Date: 2014-08-09 09:35:02 +1000 (Sat, 09 Aug 2014) $
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 <ptlib.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  Focus
283  );
284 
286  Types type = EndTypes,
287  int minimum = 0,
288  int maximum = 100,
289  int step = 1,
290  int reset = 0
291  ) : m_type(type)
292  , m_minimum(minimum)
293  , m_maximum(maximum)
294  , m_step(step)
295  , m_reset(reset)
296  , m_current(reset)
297  { }
298 
299  virtual void PrintOn(ostream & strm) const;
300 
301  bool IsValid() const { return m_type != EndTypes; }
302 
303  Types GetType() const { return m_type; }
304  int GetStep() const { return m_step; }
305  int Reset() { return m_current = m_reset; }
306  int GetCurrent() const { return m_current; }
307  int SetCurrent(int current);
308 
309  protected:
310  Types m_type;
313  int m_step;
314  int m_reset;
316 };
317 
318 
322 {
323  PCLASSINFO(PVideoInteractionInfo, PObject);
324  public:
325  P_DECLARE_ENUM(Type,
326  InteractKey,
327  InteractMouse,
328  InteractNavigate,
329  InteractRTSP,
330  InteractOther
331  );
332 
334  virtual void PrintOn(ostream & strm) const;
335  static PString AsString(const Type & type);
336 
337  Type m_type;
338 };
339 
340 
370 {
371  PCLASSINFO(PVideoDevice, PVideoFrameInfo);
372 
373  protected:
376  PVideoDevice();
377 
378 
379  public:
382  virtual ~PVideoDevice();
383 
384  P_DECLARE_STREAMABLE_ENUM(VideoFormat,
385  PAL,
386  NTSC,
387  SECAM,
388  Auto
389  );
390 
393  const PString & GetDeviceName() const
394  { return deviceName; }
395 
398  virtual PStringArray GetDeviceNames() const = 0;
399 
400  struct Attributes {
401  Attributes();
402 
406  int m_hue;
407  int m_gamma;
409  };
410 
411  struct OpenArgs {
412  OpenArgs();
413 
417  VideoFormat videoFormat;
421  unsigned rate;
422  unsigned width;
423  unsigned height;
425  ResizeMode resizeMode;
426  bool flip;
428 
429  template<class PVideoXxxDevice>
430  bool Validate(PVideoDevice::OpenArgs & validated) const
431  {
432  // Check that the input device is legal
433  PVideoXxxDevice * pDevice = PVideoXxxDevice::CreateDeviceByName(deviceName, driverName, pluginMgr);
434  if (pDevice == NULL)
435  return false;
436 
437  delete pDevice;
438  validated = *this;
439  return true;
440  }
441  };
442 
445  virtual PBoolean OpenFull(
446  const OpenArgs & args,
447  PBoolean startImmediate = true
448  );
449 
452  virtual PBoolean Open(
453  const PString & deviceName,
454  PBoolean startImmediate = true
455  ) = 0;
456 
459  virtual PBoolean IsOpen() = 0;
460 
463  virtual PBoolean Close() = 0;
464 
467  virtual PBoolean Start() = 0;
468 
471  virtual PBoolean Stop() = 0;
472 
473 
479  virtual PBoolean SetVideoFormat(
480  VideoFormat videoFormat
481  );
482 
487  virtual VideoFormat GetVideoFormat() const;
488 
493  virtual int GetNumChannels();
494 
497  virtual PStringArray GetChannelNames();
498 
506  virtual PBoolean SetChannel(
507  int channelNumber
508  );
509 
514  virtual int GetChannel() const;
515 
521  virtual bool SetFrameInfoConverter(
522  const PVideoFrameInfo & info // New frame infofor device.
523  );
524 
532  const PString & colourFormat // New colour format for device.
533  );
534 
538  virtual PBoolean GetVFlipState();
539 
543  virtual PBoolean SetVFlipState(
544  PBoolean newVFlipState
545  );
546 
553  unsigned & minWidth,
554  unsigned & minHeight,
555  unsigned & maxWidth,
556  unsigned & maxHeight
557  ) ;
558 
559 
566  unsigned width,
567  unsigned height,
568  ResizeMode resizeMode = eMaxResizeMode
569  );
570 
577  unsigned width,
578  unsigned height,
579  PBoolean /*bScaleNotCrop*/
580  ) { return SetFrameSizeConverter(width,height,eScale); }
581 
582 
591  unsigned width,
592  unsigned height
593  );
594 
603  virtual PBoolean SetFrameSize(
604  unsigned width,
605  unsigned height
606  );
607 
613  virtual PBoolean GetFrameSize(
614  unsigned & width,
615  unsigned & height
616  ) const;
617 
623  virtual const PString& GetColourFormat() const;
624 
633  virtual PINDEX GetMaxFrameBytes() = 0;
634 
635 
638  int GetLastError() const { return lastError; }
639 
640 
643  virtual PBoolean CanCaptureVideo() const = 0;
644 
647  virtual bool GetAttributes(
648  Attributes & attributes
649  );
650 
653  virtual bool SetAttributes(
654  const Attributes & attributes
655  );
656 
657 
661  int channelNumber,
662  VideoFormat videoFormat
663  );
664 
665 
669  void SetPreferredColourFormat(const PString & colourFmt) { preferredColourFormat = colourFmt; }
670 
675 
676  protected:
677  PINDEX GetMaxFrameBytesConverted(PINDEX rawFrameBytes) const;
678  PString GetDeviceNameFromOpenArgs(const OpenArgs & args) const;
679 
682  VideoFormat videoFormat;
684  // Preferred native colour format from video input device, empty == no preference
687 
690 
691  private:
692  P_REMOVE_VIRTUAL(int, GetBrightness(), 0);
693  P_REMOVE_VIRTUAL(PBoolean, SetBrightness(unsigned), false);
694  P_REMOVE_VIRTUAL(int, GetWhiteness(), 0);
695  P_REMOVE_VIRTUAL(PBoolean, SetWhiteness(unsigned), false);
696  P_REMOVE_VIRTUAL(int, GetColour(), 0);
697  P_REMOVE_VIRTUAL(PBoolean, SetColour(unsigned), false);
698  P_REMOVE_VIRTUAL(int, GetContrast(), 0);
699  P_REMOVE_VIRTUAL(PBoolean, SetContrast(unsigned), false);
700  P_REMOVE_VIRTUAL(int, GetHue(), 0);
701  P_REMOVE_VIRTUAL(PBoolean, SetHue(unsigned), false);
702  P_REMOVE_VIRTUAL(PBoolean, GetParameters(int *, int *, int *, int *, int *), false);
703 };
704 
705 
709 {
710  PCLASSINFO(PVideoOutputDevice, PVideoDevice);
711 
712  public:
716 
719  virtual ~PVideoOutputDevice() { Close(); };
720 
724  PPluginManager * pluginMgr = NULL
725  );
726 
734  const PString & driverName,
735  PPluginManager * pluginMgr = NULL
736  );
737 
741  const PString & driverName,
742  PPluginManager * pluginMgr = NULL
743  );
744 
745  /* Create the matching video output device that corresponds to the device name.
746 
747  This is typically used with the return values from GetDriversDeviceNames().
748  */
750  const PString & deviceName,
751  const PString & driverName = PString::Empty(),
752  PPluginManager * pluginMgr = NULL
753  );
754 
761  const PString & driverName,
762  const PString & deviceName,
763  PBoolean startImmediate = true,
764  PPluginManager * pluginMgr = NULL
765  );
766 
770  const OpenArgs & args,
771  PBoolean startImmediate = true
772  );
773 
776  virtual PBoolean Close() { return true; }
777 
780  virtual PBoolean Start() { return true; }
781 
784  virtual PBoolean Stop() { return true; }
785 
788  virtual PBoolean CanCaptureVideo() const;
789 
792  virtual PBoolean SetFrameData(
793  unsigned x,
794  unsigned y,
795  unsigned width,
796  unsigned height,
797  const BYTE * data,
798  PBoolean endFrame = true
799  ) = 0;
800  virtual PBoolean SetFrameData(
801  unsigned x,
802  unsigned y,
803  unsigned width,
804  unsigned height,
805  const BYTE * data,
806  PBoolean endFrame,
807  bool & keyFrameNeeded
808  );
809  virtual PBoolean SetFrameData(
810  unsigned x,
811  unsigned y,
812  unsigned width,
813  unsigned height,
814  unsigned sarWidth,
815  unsigned sarHeight,
816  const BYTE * data,
817  PBoolean endFrame,
818  bool & keyFrameNeeded,
819  const void * mark
820  );
821 
828  virtual PBoolean DisableDecode();
829 
836  virtual PBoolean GetPosition(
837  int & x, // X position of device surface
838  int & y // Y position of device surface
839  ) const;
840 
847  virtual bool SetPosition(
848  int x, // X position of device surface
849  int y // Y position of device surface
850  );
851 };
852 
853 
857 {
859 
860  public:
864 
875  virtual PBoolean SetColourFormat(
876  const PString & colourFormat // New colour format for device.
877  );
878 
887  virtual PBoolean SetFrameSize(
888  unsigned width,
889  unsigned height
890  );
891 
897  virtual PINDEX GetMaxFrameBytes();
898 
901  virtual PBoolean SetFrameData(
902  unsigned x,
903  unsigned y,
904  unsigned width,
905  unsigned height,
906  const BYTE * data,
907  PBoolean endFrame = true
908  );
909 
912  virtual PBoolean FrameComplete() = 0;
913 
914  protected:
919 };
920 
921 
922 #ifdef SHOULD_BE_MOVED_TO_PLUGIN
923 
926 class PVideoOutputDevicePPM : public PVideoOutputDeviceRGB
927 {
928  PCLASSINFO(PVideoOutputDevicePPM, PVideoOutputDeviceRGB);
929 
930  public:
933  PVideoOutputDevicePPM();
934 
937  virtual PBoolean Open(
938  const PString & deviceName,
939  PBoolean startImmediate = true
940  );
941 
944  virtual PBoolean IsOpen();
945 
948  virtual PBoolean Close();
949 
952  virtual PStringArray GetDeviceNames() const;
953 
956  virtual PBoolean EndFrame();
957 
958  protected:
959  unsigned frameNumber;
960 };
961 
962 #endif // SHOULD_BE_MOVED_TO_PLUGIN
963 
964 
968 {
969  PCLASSINFO(PVideoInputDevice, PVideoDevice);
970 
971  public:
974  //PVideoInputDevice();
975 
979 
983  PPluginManager * pluginMgr = NULL
984  );
985 
993  const PString & driverName,
994  PPluginManager * pluginMgr = NULL
995  );
996 
1000  const PString & driverName,
1001  PPluginManager * pluginMgr = NULL
1002  );
1003 
1004  /* Create the matching video input device that corresponds to the device name.
1005  So, for "fake" return a device that will generate fake video.
1006  For "Phillips 680 webcam" (eg) will return appropriate grabber.
1007  Note that Phillips will return the appropriate grabber also.
1008 
1009  This is typically used with the return values from GetDriversDeviceNames().
1010  */
1012  const PString & deviceName,
1013  const PString & driverName = PString::Empty(),
1014  PPluginManager * pluginMgr = NULL
1015  );
1016 
1023  const PString & driverName,
1024  const PString & deviceName,
1025  PBoolean startImmediate = true,
1026  PPluginManager * pluginMgr = NULL
1027  );
1028 
1032  const OpenArgs & args,
1033  PBoolean startImmediate = true
1034  );
1035 
1036  class Capabilities : public PObject {
1037  PCLASSINFO(Capabilities, PObject);
1038  public:
1039  Capabilities();
1040  virtual void PrintOn(ostream & strm) const;
1041 
1042  unsigned m_channels;
1046  bool m_hue;
1047  bool m_gamma;
1049  std::list<PVideoFrameInfo> framesizes;
1050  std::list<PVideoControlInfo> controls;
1051  std::list<PVideoInteractionInfo> interactions;
1052  };
1053 
1056  virtual bool GetDeviceCapabilities(
1057  Capabilities * capabilities
1058  ) const;
1059 
1062  static bool GetDeviceCapabilities(
1063  const PString & deviceName,
1064  Capabilities * capabilities,
1065  PPluginManager * pluginMgr = NULL
1066  );
1067 
1070  static bool GetDeviceCapabilities(
1071  const PString & deviceName,
1072  const PString & driverName,
1073  Capabilities * caps,
1074  PPluginManager * pluginMgr = NULL
1075  );
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 
1167  };
1168 
1169  virtual bool SetControl(PVideoControlInfo::Types type, int value, ControlMode mode);
1170  virtual const PVideoControlInfo & GetControlInfo(PVideoControlInfo::Types type) const { return m_controlInfo[type]; }
1171 
1172  protected:
1173  PVideoControlInfo m_controlInfo[PVideoControlInfo::NumTypes];
1174 
1175  private:
1176  P_REMOVE_VIRTUAL(PBoolean, GetFrameData(BYTE *, PINDEX *, unsigned &), false);
1177  P_REMOVE_VIRTUAL(PBoolean, GetFrameDataNoDelay(BYTE *, PINDEX *, unsigned &), false);
1178 };
1179 
1180 
1182 //
1183 // declare macros and structures needed for video input plugins
1184 //
1185 
1187 
1188 #define PCREATE_VIDINPUT_PLUGIN_EX(name, extra) \
1189  PCREATE_PLUGIN(name, PVideoInputDevice, PVideoInputDevice_##name, PPlugin_PVideoInputDevice, \
1190  virtual PStringArray GetDeviceNames(P_INT_PTR /*userData*/) const { return PVideoInputDevice_##name::GetInputDeviceNames(); } \
1191  virtual bool GetDeviceCapabilities(const PString & deviceName, void * caps) const { return PVideoInputDevice_##name::GetDeviceCapabilities(deviceName, (PVideoInputDevice::Capabilities *)caps); } \
1192  extra)
1193 
1194 #define PCREATE_VIDINPUT_PLUGIN(name) PCREATE_VIDINPUT_PLUGIN_EX(name, )
1195 
1196 
1197 #define P_FAKE_VIDEO_DRIVER "FakeVideo"
1198 #define P_FAKE_VIDEO_PREFIX "Fake/"
1199 #define P_FAKE_VIDEO_MOVING_BLOCKS P_FAKE_VIDEO_PREFIX"MovingBlocks"
1200 #define P_FAKE_VIDEO_MOVING_LINE P_FAKE_VIDEO_PREFIX"MovingLine"
1201 #define P_FAKE_VIDEO_BOUNCING_BOXES P_FAKE_VIDEO_PREFIX"BouncingBoxes"
1202 #define P_FAKE_VIDEO_SOLID_COLOUR P_FAKE_VIDEO_PREFIX"SolidColour"
1203 #define P_FAKE_VIDEO_TEXT P_FAKE_VIDEO_PREFIX"Text"
1204 #define P_FAKE_VIDEO_NTSC P_FAKE_VIDEO_PREFIX"NTSCTest"
1205 
1207 
1208 #ifdef P_APPSHARE
1209  #define P_APPLICATION_VIDEO_DRIVER "Application"
1210  PPLUGIN_STATIC_LOAD(Application, PVideoInputDevice);
1211 #endif
1212 
1213 #if P_FFVDEV
1215 #endif
1216 
1217 #if P_VIDFILE
1218  #define P_VIDEO_FILE_DRIVER "VideoFile"
1220 #endif
1221 
1222 #ifdef WIN32
1223  #define P_VIDEO_FOR_WINDOWS_DRIVER "VideoForWindows"
1224  PPLUGIN_STATIC_LOAD(VideoForWindows, PVideoInputDevice);
1225 #endif
1226 
1227 #ifdef P_DIRECTSHOW
1228  #define P_DIRECT_SHOW_DRIVER "DirectShow"
1230 #endif
1231 
1232 #if defined(P_MACOSX)
1234 #endif
1235 
1236 
1238 //
1239 // declare macros and structures needed for video output plugins
1240 //
1241 
1243 
1244 #define PCREATE_VIDOUTPUT_PLUGIN_EX(name, extra) \
1245  PCREATE_PLUGIN(name, PVideoOutputDevice, PVideoOutputDevice_##name, PPlugin_PVideoOutputDevice, \
1246  virtual PStringArray GetDeviceNames(P_INT_PTR /*userData*/) const { return PVideoOutputDevice_##name::GetOutputDeviceNames(); } \
1247  extra)
1248 
1249 #define PCREATE_VIDOUTPUT_PLUGIN(name) PCREATE_VIDOUTPUT_PLUGIN_EX(name,)
1250 
1251 #define P_NULL_VIDEO_DRIVER "NULLOutput"
1252 #define P_NULL_VIDEO_DEVICE "Null Video Out"
1254 
1255 
1256 #if P_VFW_CAPTURE
1257 #if _WIN32
1258  #define P_MSWIN_VIDEO_DRIVER "Window"
1259  #define P_MSWIN_VIDEO_PREFIX "MSWIN"
1260  #define P_MSWIN_VIDEO_DEVICE(x,y,width,height) P_MSWIN_VIDEO_PREFIX " X=" x " Y=" y " WIDTH=" width " HEIGHT=" height
1262 #endif
1263 #endif
1264 
1265 #if P_SDL
1266  #if defined(P_MACOSX) || defined(P_IOS)
1267  #include <SDL_main.h>
1268  #endif
1269  #define P_SDL_VIDEO_DRIVER "SDL"
1270  #define P_SDL_VIDEO_PREFIX "SDL"
1271  #define P_SDL_VIDEO_DEVICE(x,y,width,height) P_SDL_VIDEO_PREFIX " X=" x " Y=" y " WIDTH=" width " HEIGHT=" height
1273 #endif
1274 
1275 
1277 //
1278 // declare classes needed for access to simple video font
1279 //
1280 
1281 class PVideoFont : public PObject
1282 {
1283  PCLASSINFO(PVideoFont, PObject);
1284  public:
1285  enum {
1287  };
1288  struct LetterData {
1289  char ascii;
1290  const char *line[MAX_L_HEIGHT];
1291  };
1292 
1293  static const LetterData * GetLetterData(char ascii);
1294 };
1295 
1296 #endif // P_VIDEO
1297 
1298 #endif // PTLIB_PVIDEOIO_H
1299 
1300 // End Of File ///////////////////////////////////////////////////////////////
1301