AJA NTV2 SDK  18.1.0.2262
NTV2 SDK 18.1.0.2262
main.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
9 // Includes
10 #include "ntv2dolbycapture.h"
11 #include "ajabase/system/process.h"
12 #include <signal.h>
13 
14 using namespace std;
15 
16 
17 // Globals
18 static bool gGlobalQuit (false); // Set this "true" to exit gracefully
19 
20 
21 static void SignalHandler (int inSignal)
22 {
23  (void) inSignal;
24  gGlobalQuit = true;
25 }
26 
27 
28 int main (int argc, const char ** argv)
29 {
30  int showVersion (0); // Show version?
31  char * pDeviceSpec (AJA_NULL); // Device specifier string, if any
32  char * pPixelFormat (AJA_NULL); // Pixel format argument
33  char * pFramesSpec (AJA_NULL); // AutoCirculate frames spec
34  char * pInputSrcSpec (AJA_NULL); // SDI source spec
35  uint32_t channelNumber (1); // Channel/FrameStore to use
36  int doMultiFormat (0); // Enable multi-format?
37  int doRecordAux (0); // Record aux?
38  int doRecordAudio (0); // Record audio?
39  int doRecordDolby (0); // Record dolby?
40  int doAudioFilter (0); // Enable anc audio filter?
41  int doFrameStats (0); // Output per frame data?
44 
45  // Command line option descriptions:
46  const CNTV2DemoCommon::PoptOpts optionsTable [] =
47  {
48  {"version", 0, POPT_ARG_NONE, &showVersion, 0, "show version & exit", AJA_NULL },
49  {"device", 'd', POPT_ARG_STRING, &pDeviceSpec, 0, "device to use", "index#, serial#, or model" },
50  {"channel", 'c', POPT_ARG_INT, &channelNumber, 0, "channel to use", "1-8" },
51  {"multiFormat", 'm', POPT_ARG_NONE, &doMultiFormat, 0, "use multi-format/channel", AJA_NULL },
52  {"pixelFormat", 'p', POPT_ARG_STRING, &pPixelFormat, 0, "pixel format to use", "'?' or 'list' to list" },
53  {"frames", 0, POPT_ARG_STRING, &pFramesSpec, 0, "frames to AutoCirculate", "num[@min] or min-max" },
54  {"input", 'i', POPT_ARG_STRING, &pInputSrcSpec, 0, "which HDMI input", "?=list" },
55  {"aux", 0, POPT_ARG_NONE, &doRecordAux, 0, "record all aux to file", AJA_NULL },
56  {"audio", 0, POPT_ARG_NONE, &doRecordAudio, 0, "record aux audio to file", AJA_NULL },
57  {"dolby", 0, POPT_ARG_NONE, &doRecordDolby, 0, "record dolby to file", AJA_NULL },
58  {"filter", 'x', POPT_ARG_NONE, &doAudioFilter, 0, "only capture audio anc", AJA_NULL },
59  {"stats", 's', POPT_ARG_NONE, &doFrameStats, 0, "show per-frame stats", AJA_NULL },
62  };
63 
64  CNTV2DemoCommon::Popt popt(argc, argv, optionsTable);
65  if (!popt)
66  {cerr << "## ERROR: " << popt.errorStr() << endl; return 2;}
67  if (showVersion)
68  {cout << argv[0] << ", NTV2 SDK " << ::NTV2Version() << endl; return 0;}
69 
70  // Device
71  const string deviceSpec (pDeviceSpec ? pDeviceSpec : "0");
72  DolbyCaptureConfig config(deviceSpec);
73 
74  // Channel
75  if ((channelNumber < 1) || (channelNumber > 8))
76  {cerr << "## ERROR: Invalid channel number " << channelNumber << " -- expected 1 thru 8" << endl; return 1;}
77  config.fInputChannel = NTV2Channel(channelNumber - 1);
78 
79  // Input source
80  const string legalSources (CNTV2DemoCommon::GetInputSourceStrings(NTV2_IOKINDS_HDMI, deviceSpec));
81  const string inputSourceStr (pInputSrcSpec ? CNTV2DemoCommon::ToLower(string(pInputSrcSpec)) : "");
82  if (inputSourceStr == "?" || inputSourceStr == "list")
83  {cout << legalSources << endl; return 0;}
84  if (!inputSourceStr.empty())
85  {
88  {cerr << "## ERROR: Input source '" << inputSourceStr << "' not one of:" << endl << legalSources << endl; return 1;}
89  } // if input source specified
90 
91  // Pixel Format
92  const string pixelFormatStr (pPixelFormat ? pPixelFormat : "");
93  config.fPixelFormat = pixelFormatStr.empty() ? NTV2_FBF_8BIT_YCBCR : CNTV2DemoCommon::GetPixelFormatFromString(pixelFormatStr);
94  if (pixelFormatStr == "?" || pixelFormatStr == "list")
95  {cout << CNTV2DemoCommon::GetPixelFormatStrings(PIXEL_FORMATS_ALL, pDeviceSpec ? deviceSpec : "") << endl; return 0;}
96  else if (!pixelFormatStr.empty() && !NTV2_IS_VALID_FRAME_BUFFER_FORMAT(config.fPixelFormat))
97  {
98  cerr << "## ERROR: Invalid '--pixelFormat' value '" << pixelFormatStr << "' -- expected values:" << endl
100  return 2;
101  }
102 
103  // AutoCirculate Frames
104  static const string legalFramesSpec("{frameCount}[@{firstFrameNum}] or {firstFrameNum}-{lastFrameNum}");
105  const string framesSpec (pFramesSpec ? pFramesSpec : "");
106  if (!framesSpec.empty())
107  {
108  const string parseResult(config.fFrames.setFromString(framesSpec));
109  if (!parseResult.empty())
110  {cerr << "## ERROR: Bad 'frames' spec '" << framesSpec << "'\n## " << parseResult << endl; return 1;}
111  }
112  if (!config.fFrames.valid())
113  {cerr << "## ERROR: Bad 'frames' spec '" << framesSpec << "'\n## Expected " << legalFramesSpec << endl; return 1;}
114 
115  // HDMI Aux Capture
116  if (doRecordAux)
117  { // invent a file name...
118  ostringstream fileName; fileName << "ntv2dolbycapture-" << deviceSpec << "-"
119  << ::NTV2ChannelToString(config.fInputChannel,true) << "-" << AJAProcess::GetPid() << ".aux";
120  config.fAncDataFilePath = fileName.str();
121  }
122 
123  // Audio Capture
124  if (doRecordAudio)
125  { // invent a file name...
126  ostringstream fileName; fileName << "ntv2dolbycapture-" << deviceSpec << "-"
127  << ::NTV2ChannelToString(config.fInputChannel,true) << "-" << AJAProcess::GetPid() << ".raw";
128  config.fAudioDataFilePath = fileName.str();
129  }
130 
131  // Dolby Capture
132  if (doRecordDolby)
133  { // invent a file name...
134  ostringstream fileName; fileName << "ntv2dolbycapture-" << deviceSpec << "-"
135  << ::NTV2ChannelToString(config.fInputChannel,true) << "-" << AJAProcess::GetPid() << ".ec3";
136  config.fDolbyDataFilePath = fileName.str();
137  }
138 
139  config.fDoAudioFilter = doAudioFilter ? true : false;
140  config.fDoFrameStats = doFrameStats ? true : false;
141  config.fDoMultiFormat = doMultiFormat ? true : false;
142  config.fWithAnc = doRecordAux ? true : false;
143  config.fWithAudio = doRecordAudio ? true : false;
144 
145  { // Instantiate and initialize the NTV2DolbyCapture object...
146  NTV2DolbyCapture capturer(config);
147  status = capturer.Init();
148  if (AJA_FAILURE(status))
149  {cout << "## ERROR: Initialization failed: " << ::AJAStatusToString(status) << endl; return 1;}
150 
151  ::signal (SIGINT, SignalHandler);
152  #if defined (AJAMac)
153  ::signal (SIGHUP, SignalHandler);
154  ::signal (SIGQUIT, SignalHandler);
155  #endif
156 
157  // Run the capturer...
158  capturer.Run();
159 
160  cout << " Frames Frames Buffer" << endl
161  << " Captured Dropped Level" << endl;
162  do
163  { // Poll its status until stopped...
164  ULWord framesProcessed, framesDropped, bufferLevel;
165  capturer.GetACStatus (framesProcessed, framesDropped, bufferLevel);
166  cout << setw(9) << framesProcessed << setw(9) << framesDropped << setw(9) << bufferLevel << "\r" << flush;
167  AJATime::Sleep(2000);
168  } while (!gGlobalQuit); // loop til quit time
169  cout << endl;
170  } // NTV2DolbyCapture scope
171  return 0;
172 
173 } // main
int main(int argc, const char **argv)
Definition: main.cpp:30
AJAStatus
Definition: types.h:380
bool fDoMultiFormat
If true, use multi-format/multi-channel mode, if device supports it; otherwise normal mode...
static AJAStatus Open(bool incrementRefCount=false)
Definition: debug.cpp:44
static uint64_t GetPid()
Definition: process.cpp:35
#define AJA_FAILURE(_status_)
Definition: types.h:373
static std::string GetInputSourceStrings(const NTV2IOKinds inKinds=NTV2_IOKINDS_ALL, const std::string inDevSpec=std::string())
Specifies HDMI input/output kinds.
Definition: ntv2enums.h:1295
static std::string GetPixelFormatStrings(const NTV2PixelFormatKinds inKinds=PIXEL_FORMATS_ALL, const std::string inDevSpec=std::string())
bool fWithAnc
If true, also capture Anc.
std::string setFromString(const std::string &inStr)
Definition: ntv2utils.cpp:4256
Definition: json.hpp:5362
#define POPT_ARG_INT
Definition: options_popt.h:58
void SignalHandler(int inSignal)
Definition: main.cpp:26
uint32_t ULWord
Definition: ajatypes.h:236
NTV2Channel
These enum values are mostly used to identify a specific widget_framestore. They&#39;re also commonly use...
Definition: ntv2enums.h:1359
bool fDoFrameStats
if true, output per frame statistics
bool fWithAudio
If true, also capture Audio.
#define POPT_AUTOHELP
Definition: options_popt.h:227
NTV2Channel fInputChannel
The device channel to use.
#define true
std::string NTV2Version(const bool inDetailed=false)
Definition: ntv2version.cpp:41
std::string fAncDataFilePath
Optional path to Anc binary data file.
#define AJA_NULL
Definition: ajatypes.h:180
static void Sleep(const int32_t inMilliseconds)
Suspends execution of the current thread for a given number of milliseconds.
Definition: systemtime.cpp:284
static std::string ToLower(const std::string &inStr)
Returns the given string after converting it to lower case.
virtual const std::string & errorStr(void) const
bool fDoAudioFilter
If true, capture only audio anc.
std::string fDolbyDataFilePath
Optional path to Dolby binary data file.
Declares the AJAProcess class.
virtual AJAStatus Run(void)
Runs me.
NTV2InputSource fInputSource
The device input connector to use.
virtual AJAStatus Init(void)
Initializes me and prepares me to Run.
#define NTV2_IS_VALID_INPUT_SOURCE(_inpSrc_)
Definition: ntv2enums.h:1286
Declares the NTV2DolbyCapture class.
#define POPT_TABLEEND
Definition: options_popt.h:222
I capture HDMI Dolby audio from an HDMI input of an AJA device.
static NTV2InputSource GetInputSourceFromString(const std::string &inStr, const NTV2IOKinds inKinds=NTV2_IOKINDS_ALL, const std::string inDevSpec=std::string())
Returns the NTV2InputSource that matches the given string.
NTV2ACFrameRange fFrames
AutoCirculate frame count or range.
NTV2PixelFormat fPixelFormat
Pixel format to use.
std::string AJAStatusToString(const AJAStatus inStatus, const bool inDetailed)
Definition: debug.cpp:987
virtual void GetACStatus(ULWord &outGoodFrames, ULWord &outDroppedFrames, ULWord &outBufferLevel)
Provides status information about my input (capture) process.
#define NTV2_IS_VALID_FRAME_BUFFER_FORMAT(__s__)
Definition: ntv2enums.h:265
bool valid(void) const
Definition: ntv2utils.h:987
std::string NTV2ChannelToString(const NTV2Channel inValue, const bool inForRetailDisplay=false)
Definition: ntv2utils.cpp:5730
#define POPT_ARG_STRING
Definition: options_popt.h:57
This class is used to configure an NTV2Capture instance.
std::string fAudioDataFilePath
Optional path to Audio binary data file.
#define POPT_ARG_NONE
Definition: options_popt.h:56
static NTV2PixelFormat GetPixelFormatFromString(const std::string &inStr, const NTV2PixelFormatKinds inKinds=PIXEL_FORMATS_ALL, const std::string inDevSpec=std::string())
Returns the NTV2PixelFormat that matches the given string.
static bool gGlobalQuit((0))
See 8-Bit YCbCr Format.
Definition: ntv2enums.h:225