AJA NTV2 SDK  17.1.1.1245
NTV2 SDK 17.1.1.1245
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  if (!CNTV2DemoCommon::IsValidDevice(deviceSpec))
73  return 1;
74 
75  DolbyCaptureConfig config(deviceSpec);
76 
77  // Channel
78  if ((channelNumber < 1) || (channelNumber > 8))
79  {cerr << "## ERROR: Invalid channel number " << channelNumber << " -- expected 1 thru 8" << endl; return 1;}
80  config.fInputChannel = NTV2Channel(channelNumber - 1);
81 
82  // Input source
83  const string legalSources (CNTV2DemoCommon::GetInputSourceStrings(NTV2_IOKINDS_HDMI, deviceSpec));
84  const string inputSourceStr (pInputSrcSpec ? CNTV2DemoCommon::ToLower(string(pInputSrcSpec)) : "");
85  if (inputSourceStr == "?" || inputSourceStr == "list")
86  {cout << legalSources << endl; return 0;}
87  if (!inputSourceStr.empty())
88  {
91  {cerr << "## ERROR: Input source '" << inputSourceStr << "' not one of:" << endl << legalSources << endl; return 1;}
92  } // if input source specified
93  // Pixel Format
94  const string pixelFormatStr (pPixelFormat ? pPixelFormat : "");
95  config.fPixelFormat = pixelFormatStr.empty() ? NTV2_FBF_8BIT_YCBCR : CNTV2DemoCommon::GetPixelFormatFromString(pixelFormatStr);
96  if (pixelFormatStr == "?" || pixelFormatStr == "list")
97  {cout << CNTV2DemoCommon::GetPixelFormatStrings(PIXEL_FORMATS_ALL, deviceSpec) << endl; return 0;}
98  else if (!pixelFormatStr.empty() && !NTV2_IS_VALID_FRAME_BUFFER_FORMAT(config.fPixelFormat))
99  {
100  cerr << "## ERROR: Invalid '--pixelFormat' value '" << pixelFormatStr << "' -- expected values:" << endl
102  return 2;
103  }
104 
105  // AutoCirculate Frames
106  static const string legalFramesSpec("{frameCount}[@{firstFrameNum}] or {firstFrameNum}-{lastFrameNum}");
107  const string framesSpec (pFramesSpec ? pFramesSpec : "");
108  if (!framesSpec.empty())
109  {
110  const string parseResult(config.fFrames.setFromString(framesSpec));
111  if (!parseResult.empty())
112  {cerr << "## ERROR: Bad 'frames' spec '" << framesSpec << "'\n## " << parseResult << endl; return 1;}
113  }
114  if (!config.fFrames.valid())
115  {cerr << "## ERROR: Bad 'frames' spec '" << framesSpec << "'\n## Expected " << legalFramesSpec << endl; return 1;}
116 
117  // HDMI Aux Capture
118  if (doRecordAux)
119  { // invent a file name...
120  ostringstream fileName; fileName << "ntv2dolbycapture-" << deviceSpec << "-"
121  << ::NTV2ChannelToString(config.fInputChannel,true) << "-" << AJAProcess::GetPid() << ".aux";
122  config.fAncDataFilePath = fileName.str();
123  }
124 
125  // Audio Capture
126  if (doRecordAudio)
127  { // invent a file name...
128  ostringstream fileName; fileName << "ntv2dolbycapture-" << deviceSpec << "-"
129  << ::NTV2ChannelToString(config.fInputChannel,true) << "-" << AJAProcess::GetPid() << ".raw";
130  config.fAudioDataFilePath = fileName.str();
131  }
132 
133  // Dolby Capture
134  if (doRecordDolby)
135  { // invent a file name...
136  ostringstream fileName; fileName << "ntv2dolbycapture-" << deviceSpec << "-"
137  << ::NTV2ChannelToString(config.fInputChannel,true) << "-" << AJAProcess::GetPid() << ".ec3";
138  config.fDolbyDataFilePath = fileName.str();
139  }
140 
141  config.fDoAudioFilter = doAudioFilter ? true : false;
142  config.fDoFrameStats = doFrameStats ? true : false;
143  config.fDoMultiFormat = doMultiFormat ? true : false;
144  config.fWithAnc = doRecordAux ? true : false;
145  config.fWithAudio = doRecordAudio ? true : false;
146 
147  // Instantiate and initialize the ntv2dolbycapture object
148  NTV2DolbyCapture capturer(config);
149  status = capturer.Init();
150  if (AJA_FAILURE(status))
151  {cout << "## ERROR: Initialization failed: " << ::AJAStatusToString(status) << endl; return 1;}
152 
153  ::signal (SIGINT, SignalHandler);
154  #if defined (AJAMac)
155  ::signal (SIGHUP, SignalHandler);
156  ::signal (SIGQUIT, SignalHandler);
157  #endif
158 
159  // Run the capturer...
160  capturer.Run();
161 
162  cout << " Frames Frames Buffer" << endl
163  << " Captured Dropped Level" << endl;
164  do
165  { // Poll its status until stopped...
166  ULWord framesProcessed, framesDropped, bufferLevel;
167  capturer.GetACStatus (framesProcessed, framesDropped, bufferLevel);
168  cout << setw(9) << framesProcessed << setw(9) << framesDropped << setw(9) << bufferLevel << "\r" << flush;
169  AJATime::Sleep(2000);
170  } while (!gGlobalQuit); // loop til quit time
171 
172  cout << endl;
173  return 0;
174 
175 } // main
CaptureConfig::fDoMultiFormat
bool fDoMultiFormat
If true, use multi-format/multi-channel mode, if device supports it; otherwise normal mode.
Definition: ntv2democommon.h:276
DolbyCaptureConfig::fDolbyDataFilePath
std::string fDolbyDataFilePath
Optional path to Dolby binary data file.
Definition: ntv2dolbycapture.h:23
ntv2dolbycapture.h
Declares the NTV2DolbyCapture class.
CNTV2DemoCommon::GetInputSourceFromString
static NTV2InputSource GetInputSourceFromString(const std::string &inStr)
Returns the NTV2InputSource that matches the given string.
Definition: ntv2democommon.cpp:706
DolbyCaptureConfig::fDoAudioFilter
bool fDoAudioFilter
If true, capture only audio anc.
Definition: ntv2dolbycapture.h:24
poptOption
Definition: options_popt.h:148
DolbyCaptureConfig
This class is used to configure an NTV2Capture instance.
Definition: ntv2dolbycapture.h:19
NTV2Channel
NTV2Channel
These enum values are mostly used to identify a specific widget_framestore. They're also commonly use...
Definition: ntv2enums.h:1305
AJA_STATUS_SUCCESS
@ AJA_STATUS_SUCCESS
Definition: types.h:381
NTV2DolbyCapture::GetACStatus
virtual void GetACStatus(ULWord &outGoodFrames, ULWord &outDroppedFrames, ULWord &outBufferLevel)
Provides status information about my input (capture) process.
Definition: ntv2dolbycapture.cpp:532
NTV2DolbyCapture::Init
virtual AJAStatus Init(void)
Initializes me and prepares me to Run.
Definition: ntv2dolbycapture.cpp:71
SignalHandler
void SignalHandler(int inSignal)
Definition: main.cpp:26
CaptureConfig::fPixelFormat
NTV2PixelFormat fPixelFormat
Pixel format to use.
Definition: ntv2democommon.h:273
NTV2_IOKINDS_HDMI
@ NTV2_IOKINDS_HDMI
Specifies HDMI input/output kinds.
Definition: ntv2enums.h:1252
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
nlohmann::json_abiNLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON_v3_11_NLOHMANN_JSON_VERSION_PATCH::detail::void
j template void())
Definition: json.hpp:4893
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
DolbyCaptureConfig::fAudioDataFilePath
std::string fAudioDataFilePath
Optional path to Audio binary data file.
Definition: ntv2dolbycapture.h:22
CNTV2DemoCommon::IsValidDevice
static bool IsValidDevice(const std::string &inDeviceSpec)
Definition: ntv2democommon.cpp:430
AJAStatus
AJAStatus
Definition: types.h:378
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:253
CNTV2DemoCommon::ToLower
static std::string ToLower(const std::string &inStr)
Returns the given string after converting it to lower case.
Definition: ntv2democommon.cpp:926
CNTV2DemoCommon::GetPixelFormatFromString
static NTV2FrameBufferFormat GetPixelFormatFromString(const std::string &inStr)
Returns the NTV2FrameBufferFormat that matches the given string.
Definition: ntv2democommon.cpp:653
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
AJADebug::Open
static AJAStatus Open(bool incrementRefCount=false)
Definition: debug.cpp:44
AJA_NULL
#define AJA_NULL
Definition: ajatypes.h:197
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
CNTV2DemoCommon::GetInputSourceStrings
static std::string GetInputSourceStrings(const NTV2IOKinds inKinds=NTV2_IOKINDS_ALL, const std::string inDeviceSpecifier=std::string())
Definition: ntv2democommon.cpp:678
gGlobalQuit
static bool gGlobalQuit((0))
DolbyCaptureConfig::fDoFrameStats
bool fDoFrameStats
if true, output per frame statistics
Definition: ntv2dolbycapture.h:25
NTV2DolbyCapture::Run
virtual AJAStatus Run(void)
Runs me.
Definition: ntv2dolbycapture.cpp:268
std
Definition: json.hpp:5362
AJAStatusToString
std::string AJAStatusToString(const AJAStatus inStatus, const bool inDetailed)
Definition: debug.cpp:981
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:600
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
NTV2DolbyCapture
I capture HDMI Dolby audio from an HDMI input of an AJA device.
Definition: ntv2dolbycapture.h:45
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:371
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