AJA NTV2 SDK  17.0.1.1246
NTV2 SDK 17.0.1.1246
main.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
9 // Includes
10 #include "ntv2capture.h"
11 #include "ajabase/system/process.h" // for GetPID
12 #include "ajabase/common/common.h" // for strip
13 #include <signal.h>
14 
15 using namespace std;
16 
17 
18 // Globals
19 static bool gGlobalQuit (false); // Set this "true" to exit gracefully
20 
21 
22 static void SignalHandler (int inSignal)
23 {
24  (void) inSignal;
25  gGlobalQuit = true;
26 }
27 
28 
29 int main (int argc, const char ** argv)
30 {
31  int showVersion (0); // Show version?
32  char * pDeviceSpec (AJA_NULL); // Device specifier string, if any
33  char * pPixelFormat (AJA_NULL); // Pixel format argument
34  char * pFramesSpec (AJA_NULL); // AutoCirculate frames spec
35  char * pAncFilePath (AJA_NULL); // Anc data filepath to record
36  char * pInputSrcSpec (AJA_NULL); // SDI source spec
37  int channelNumber (1); // Channel/FrameStore to use
38  int doMultiFormat (0); // MultiFormat mode?
39  int noAudio (0); // Disable audio capture?
40  int captureAnc (0); // Capture anc?
42 
43  // Command line option descriptions:
44  const CNTV2DemoCommon::PoptOpts optionsTable [] =
45  {
46  {"version", 0, POPT_ARG_NONE, &showVersion, 0, "show version & exit", AJA_NULL },
47  {"device", 'd', POPT_ARG_STRING, &pDeviceSpec, 0, "device to use", "index#, serial#, or model" },
48  {"channel", 'c', POPT_ARG_INT, &channelNumber, 0, "channel to use", "1-8" },
49  {"multiFormat", 'm', POPT_ARG_NONE, &doMultiFormat, 0, "use multi-format/channel", AJA_NULL },
50  {"pixelFormat", 'p', POPT_ARG_STRING, &pPixelFormat, 0, "pixel format to use", "'?' or 'list' to list" },
51  {"frames", 0, POPT_ARG_STRING, &pFramesSpec, 0, "frames to AutoCirculate", "num[@min] or min-max" },
52  {"input", 'i', POPT_ARG_STRING, &pInputSrcSpec, 0, "which SDI input", "1-8, ?=list" },
53  {"anc", 0, POPT_ARG_STRING, &pAncFilePath, 0, "record anc to file", "path/to/binary/data/file" },
54  {AJA_NULL, 'a', POPT_ARG_NONE, &captureAnc, 0, "capture anc?", AJA_NULL },
55  {"noaudio", 0, POPT_ARG_NONE, &noAudio, 0, "disable audio capture", AJA_NULL },
58  };
59  CNTV2DemoCommon::Popt popt(argc, argv, optionsTable);
60  if (!popt)
61  {cerr << "## ERROR: " << popt.errorStr() << endl; return 2;}
62  if (showVersion)
63  {cout << argv[0] << ", NTV2 SDK " << ::NTV2Version() << endl; return 0;}
64 
65  // Device
66  const string deviceSpec (pDeviceSpec ? pDeviceSpec : "0");
67  if (!CNTV2DemoCommon::IsValidDevice(deviceSpec))
68  return 1;
69 
70  CaptureConfig config(deviceSpec);
71 
72  // Channel
73  if ((channelNumber < 1) || (channelNumber > 8))
74  {cerr << "## ERROR: Invalid channel number " << channelNumber << " -- expected 1 thru 8" << endl; return 1;}
75  config.fInputChannel = NTV2Channel(channelNumber - 1);
76 
77  // Input source
78  const string legalSources (CNTV2DemoCommon::GetInputSourceStrings(NTV2_IOKINDS_SDI, deviceSpec));
79  const string inputSourceStr (pInputSrcSpec ? CNTV2DemoCommon::ToLower(string(pInputSrcSpec)) : "");
80  if (inputSourceStr == "?" || inputSourceStr == "list")
81  {cout << legalSources << endl; return 0;}
82  if (!inputSourceStr.empty())
83  {
86  {cerr << "## ERROR: Input source '" << inputSourceStr << "' not one of:" << endl << legalSources << endl; return 1;}
87  } // if input source specified
88 
89  // Pixel Format
90  const string pixelFormatStr (pPixelFormat ? pPixelFormat : "");
91  config.fPixelFormat = pixelFormatStr.empty() ? NTV2_FBF_8BIT_YCBCR : CNTV2DemoCommon::GetPixelFormatFromString(pixelFormatStr);
92  if (pixelFormatStr == "?" || pixelFormatStr == "list")
93  {cout << CNTV2DemoCommon::GetPixelFormatStrings(PIXEL_FORMATS_ALL, deviceSpec) << endl; return 0;}
94  else if (!pixelFormatStr.empty() && !NTV2_IS_VALID_FRAME_BUFFER_FORMAT(config.fPixelFormat))
95  {
96  cerr << "## ERROR: Invalid '--pixelFormat' value '" << pixelFormatStr << "' -- expected values:" << endl
98  return 2;
99  }
100 
101  // AutoCirculate Frames
102  static const string legalFramesSpec ("{frameCount}[@{firstFrameNum}] or {firstFrameNum}-{lastFrameNum}");
103  const string framesSpec (pFramesSpec ? pFramesSpec : "");
104  if (!framesSpec.empty())
105  {
106  const string parseResult(config.fFrames.setFromString(framesSpec));
107  if (!parseResult.empty())
108  {cerr << "## ERROR: Bad 'frames' spec '" << framesSpec << "'\n## " << parseResult << endl; return 1;}
109  }
110  if (!config.fFrames.valid())
111  {cerr << "## ERROR: Bad 'frames' spec '" << framesSpec << "'\n## Expected " << legalFramesSpec << endl; return 1;}
112 
113  // Anc Capture
114  string ancFilePath (pAncFilePath ? pAncFilePath : "");
115  ancFilePath = aja::strip(ancFilePath);
116  if (ancFilePath.empty() && pAncFilePath)
117  { // User specified an empty filePath -- invent a file name...
118  ostringstream fileName; fileName << "ntv2capture-" << deviceSpec << "-"
119  << ::NTV2ChannelToString(config.fInputChannel,true) << "-" << AJAProcess::GetPid() << ".anc";
120  ancFilePath = fileName.str();
121  }
122  if (pAncFilePath)
123  captureAnc = 1; // Specifying target anc file automatically enables anc capture
124 
125  config.fWithAudio = noAudio ? false : true; // Capture audio?
126  config.fAncDataFilePath = ancFilePath; // Anc capture file path
127  config.fWithAnc = captureAnc ? true : false; // Capture anc?
128  config.fDoMultiFormat = doMultiFormat ? true : false; // Multiformat mode?
129 
130  // Instantiate and initialize the NTV2Capture object...
131  NTV2Capture capturer(config);
132  AJAStatus status = capturer.Init();
133  if (AJA_FAILURE(status))
134  {cout << "## ERROR: Initialization failed: " << ::AJAStatusToString(status) << endl; return 1;}
135 
136  ::signal (SIGINT, SignalHandler);
137  #if defined(AJAMac)
138  ::signal (SIGHUP, SignalHandler);
139  ::signal (SIGQUIT, SignalHandler);
140  #endif
141 
142  // Run it...
143  capturer.Run();
144 
145  cout << " Frames Frames Buffer" << endl
146  << " Captured Dropped Level" << endl;
147  do
148  { // Poll its status until stopped...
149  ULWord framesProcessed, framesDropped, bufferLevel;
150  capturer.GetACStatus (framesProcessed, framesDropped, bufferLevel);
151  cout << setw(9) << framesProcessed << setw(9) << framesDropped << setw(9) << bufferLevel << "\r" << flush;
152  AJATime::Sleep(2000);
153  } while (!gGlobalQuit); // loop til done
154 
155  cout << endl;
156  return 0;
157 
158 } // main
CaptureConfig::fDoMultiFormat
bool fDoMultiFormat
If true, use multi-format/multi-channel mode, if device supports it; otherwise normal mode.
Definition: ntv2democommon.h:276
ntv2capture.h
Declares the NTV2Capture class.
CNTV2DemoCommon::GetInputSourceFromString
static NTV2InputSource GetInputSourceFromString(const std::string &inStr)
Returns the NTV2InputSource that matches the given string.
Definition: ntv2democommon.cpp:713
poptOption
Definition: options_popt.h:148
aja::strip
std::string & strip(std::string &str, const std::string &ws)
Definition: common.cpp:461
NTV2Channel
NTV2Channel
These enum values are mostly used to identify a specific Frame Store. They're also commonly used to i...
Definition: ntv2enums.h:1305
NTV2Capture
Instances of me capture frames in real time from a video signal provided to an input of an AJA device...
Definition: ntv2capture.h:19
CaptureConfig::fPixelFormat
NTV2PixelFormat fPixelFormat
Pixel format to use.
Definition: ntv2democommon.h:273
CaptureConfig::fAncDataFilePath
std::string fAncDataFilePath
Optional path to Anc binary data file.
Definition: ntv2democommon.h:269
main
int main(int argc, const char **argv)
Definition: main.cpp:30
NTV2ACFrameRange::setFromString
std::string setFromString(const std::string &inStr)
Definition: ntv2utils.cpp:4155
POPT_ARG_INT
#define POPT_ARG_INT
Definition: options_popt.h:58
CaptureConfig::fWithAnc
bool fWithAnc
If true, also capture Anc.
Definition: ntv2democommon.h:277
POPT_AUTOHELP
#define POPT_AUTOHELP
Definition: options_popt.h:220
gGlobalQuit
static bool gGlobalQuit(false)
CNTV2DemoCommon::IsValidDevice
static bool IsValidDevice(const std::string &inDeviceSpec)
Definition: ntv2democommon.cpp:433
AJAStatus
AJAStatus
Definition: types.h:365
process.h
Declares the AJAProcess class.
AJATime::Sleep
static void Sleep(const int32_t inMilliseconds)
Suspends execution of the current thread for a given number of milliseconds.
Definition: systemtime.cpp:284
ULWord
uint32_t ULWord
Definition: ajatypes.h:246
CNTV2DemoCommon::ToLower
static std::string ToLower(const std::string &inStr)
Returns the given string after converting it to lower case.
Definition: ntv2democommon.cpp:937
CaptureConfig
This class is used to configure an NTV2Capture instance.
Definition: ntv2democommon.h:265
CNTV2DemoCommon::GetPixelFormatFromString
static NTV2FrameBufferFormat GetPixelFormatFromString(const std::string &inStr)
Returns the NTV2FrameBufferFormat that matches the given string.
Definition: ntv2democommon.cpp:660
AJAProcess::GetPid
static uint64_t GetPid()
Definition: process.cpp:35
PIXEL_FORMATS_ALL
@ PIXEL_FORMATS_ALL
Definition: ntv2democommon.h:240
NTV2_IS_VALID_INPUT_SOURCE
#define NTV2_IS_VALID_INPUT_SOURCE(_inpSrc_)
Definition: ntv2enums.h:1243
NTV2_FBF_8BIT_YCBCR
@ NTV2_FBF_8BIT_YCBCR
See 8-Bit YCbCr Format.
Definition: ntv2enums.h:211
CNTV2DemoCommon::Popt
Definition: ntv2democommon.h:801
CNTV2DemoCommon::Popt::errorStr
virtual const std::string & errorStr(void) const
Definition: ntv2democommon.h:809
NTV2Version
std::string NTV2Version(const bool inDetailed=false)
Definition: ntv2version.cpp:41
NTV2Capture::Run
virtual AJAStatus Run(void)
Runs me.
Definition: ntv2capture.cpp:284
AJADebug::Open
static AJAStatus Open(bool incrementRefCount=false)
Definition: debug.cpp:44
AJA_NULL
#define AJA_NULL
Definition: ajatypes.h:190
CaptureConfig::fFrames
NTV2ACFrameRange fFrames
AutoCirculate frame count or range.
Definition: ntv2democommon.h:272
CaptureConfig::fInputChannel
NTV2Channel fInputChannel
The device channel to use.
Definition: ntv2democommon.h:270
false
#define false
Definition: ntv2devicefeatures.h:25
common.h
Private include file for all ajabase sources.
CNTV2DemoCommon::GetInputSourceStrings
static std::string GetInputSourceStrings(const NTV2IOKinds inKinds=NTV2_IOKINDS_ALL, const std::string inDeviceSpecifier=std::string())
Definition: ntv2democommon.cpp:685
NTV2Capture::GetACStatus
virtual void GetACStatus(ULWord &outGoodFrames, ULWord &outDroppedFrames, ULWord &outBufferLevel)
Provides status information about my input (capture) process.
Definition: ntv2capture.cpp:504
AJAStatusToString
std::string AJAStatusToString(const AJAStatus inStatus, const bool inDetailed)
Definition: debug.cpp:980
POPT_TABLEEND
#define POPT_TABLEEND
Definition: options_popt.h:215
true
#define true
Definition: ntv2devicefeatures.h:26
CNTV2DemoCommon::GetPixelFormatStrings
static std::string GetPixelFormatStrings(const NTV2PixelFormatKinds inKinds=PIXEL_FORMATS_ALL, const std::string inDeviceSpecifier=std::string())
Definition: ntv2democommon.cpp:603
NTV2ACFrameRange::valid
bool valid(void) const
Definition: ntv2utils.h:977
NTV2ChannelToString
std::string NTV2ChannelToString(const NTV2Channel inValue, const bool inForRetailDisplay=false)
Definition: ntv2utils.cpp:5759
NTV2_IOKINDS_SDI
@ NTV2_IOKINDS_SDI
Specifies SDI input/output kinds.
Definition: ntv2enums.h:1251
NTV2_IS_VALID_FRAME_BUFFER_FORMAT
#define NTV2_IS_VALID_FRAME_BUFFER_FORMAT(__s__)
Definition: ntv2enums.h:251
AJA_FAILURE
#define AJA_FAILURE(_status_)
Definition: types.h:358
SignalHandler
static void SignalHandler(int inSignal)
Definition: main.cpp:22
POPT_ARG_STRING
#define POPT_ARG_STRING
Definition: options_popt.h:57
POPT_ARG_NONE
#define POPT_ARG_NONE
Definition: options_popt.h:56
CaptureConfig::fInputSource
NTV2InputSource fInputSource
The device input connector to use.
Definition: ntv2democommon.h:271
CaptureConfig::fWithAudio
bool fWithAudio
If true, also capture Audio.
Definition: ntv2democommon.h:278
NTV2Capture::Init
virtual AJAStatus Init(void)
Initializes me and prepares me to Run.
Definition: ntv2capture.cpp:73