8 #include "PlusConfigure.h" 13 #include <vtkCommand.h> 14 #include <vtkImageData.h> 15 #include <vtkImageViewer2.h> 16 #include <vtkRenderWindowInteractor.h> 17 #include <vtkRenderer.h> 18 #include <vtkSmartPointer.h> 19 #include <vtksys/CommandLineArguments.hxx> 22 #include <MediaFoundationVideoCaptureApi.h> 24 void PrintLogsCallback(vtkObject* obj,
unsigned long eid,
void* clientdata,
void* calldata);
26 class vtkMyCallback :
public vtkCommand
29 static vtkMyCallback* New()
30 {
return new vtkMyCallback;}
31 virtual void Execute(vtkObject* caller,
unsigned long,
void*)
36 iren->CreateTimer(VTKI_TIMER_UPDATE);
39 vtkRenderWindowInteractor*
iren;
48 int main(
int argc,
char** argv)
50 bool printHelp(
false);
51 bool renderingOff(
false);
52 bool showDialogs(
false);
53 FrameSizeType frameSize;
56 std::string pixelFormatName;
57 bool listDevices =
false;
58 bool listVideoFormats =
false;
60 vtksys::CommandLineArguments args;
61 args.Initialize(argc, argv);
63 int verboseLevel = vtkPlusLogger::LOG_LEVEL_UNDEFINED;
65 std::vector<int> frameSizeInt;
67 args.AddArgument(
"--list-devices", vtksys::CommandLineArguments::NO_ARGUMENT, &listDevices,
"Show the list of available devices and exit");
68 args.AddArgument(
"--list-video-formats", vtksys::CommandLineArguments::NO_ARGUMENT, &listVideoFormats,
"Show the list of supported video formats for the selected device and exit");
69 args.AddArgument(
"--device-id", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &deviceId,
"Capture device ID (default: 0)");
70 args.AddArgument(
"--stream-index", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &streamIndex,
"Stream index (default=0; see --list-devices for available streams");
71 args.AddArgument(
"--frame-size", vtksys::CommandLineArguments::MULTI_ARGUMENT, &frameSizeInt,
"Requested frame size from the capture device");
72 args.AddArgument(
"--video-format", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &pixelFormatName,
"Requested video format (YUY2, ...)");
73 args.AddArgument(
"--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp,
"Print this help.");
74 args.AddArgument(
"--rendering-off", vtksys::CommandLineArguments::NO_ARGUMENT, &renderingOff,
"Run test without rendering.");
75 args.AddArgument(
"--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel,
"Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
79 std::cerr <<
"Problem parsing arguments" << std::endl;
80 std::cout <<
"\n\nvtkPlusMmfVideoSourceTest help:" << args.GetHelp() << std::endl;
88 std::cout <<
"\n\nvtkPlusMmfVideoSourceTest help:" << args.GetHelp() << std::endl;
92 if (frameSize.size() < 2 || frameSize[0] < 0 || frameSize[1] < 0)
94 LOG_ERROR(
"Invalid frame size inputted. At least two non-negative entries required.");
97 vtkSmartPointer<vtkPlusMmfVideoSource> frameGrabber = vtkSmartPointer<vtkPlusMmfVideoSource>::New();
101 frameGrabber->LogListOfCaptureDevices();
105 frameGrabber->SetRequestedDeviceId(deviceId);
107 if (listVideoFormats)
109 frameGrabber->LogListOfCaptureVideoFormats(deviceId);
113 if (pixelFormatName.empty())
115 auto& api = MfVideoCapture::MediaFoundationVideoCaptureApi::GetInstance();
116 auto mediaType = api.GetFormat(deviceId, streamIndex, 0);
117 pixelFormatName = std::string(begin(mediaType.MF_MT_SUBTYPEName), end(mediaType.MF_MT_SUBTYPEName));
118 pixelFormatName = pixelFormatName.substr(pixelFormatName.find(
'_') + 1);
121 frameGrabber->SetRequestedVideoFormat(std::wstring(pixelFormatName.begin(), pixelFormatName.end()));
122 frameGrabber->SetRequestedStreamIndex(streamIndex);
124 if (!frameSize.empty())
126 if (frameSize.size() != 2)
128 LOG_ERROR(
"Frame size shall contain two numbers, separated by a space");
132 for (
unsigned int i = 0;
i < frameSize.size(); ++
i)
134 size[
i] = frameSize[
i];
136 frameGrabber->SetRequestedFrameSize(frameSize);
139 frameGrabber->CreateDefaultOutputChannel();
140 LOG_INFO(
"Initialize...");
141 frameGrabber->Connect();
143 if (frameGrabber->GetConnected())
145 LOG_INFO(
"Start recording...");
146 frameGrabber->StartRecording();
150 frameGrabber->Disconnect();
151 LOG_ERROR(
"Unable to disconnect from media foundation capture device.");
157 LOG_INFO(
"No need for rendering, stop the device...");
158 frameGrabber->Disconnect();
159 LOG_INFO(
"Exit successfully");
163 vtkSmartPointer<vtkImageViewer2>
viewer = vtkSmartPointer<vtkImageViewer2>::New();
165 viewer->SetColorWindow(255);
166 viewer->SetColorLevel(100.5);
168 if (frameGrabber->GetFirstActiveOutputVideoSource(videoSource) !=
PLUS_SUCCESS)
170 LOG_ERROR(
"Unable to retrieve the video source.");
175 viewer->SetInputConnection(frameGrabber->GetOutputPort());
178 vtkSmartPointer<vtkRenderWindowInteractor>
iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
179 iren->SetRenderWindow(
viewer->GetRenderWindow());
185 vtkSmartPointer<vtkMyCallback> call = vtkSmartPointer<vtkMyCallback>::New();
188 iren->AddObserver(vtkCommand::TimerEvent, call);
189 iren->CreateTimer(VTKI_TIMER_FIRST);
195 LOG_INFO(
"Exit successfully");
vtkRenderWindowInteractor * iren
int main(int argc, char **argv)
void PrintLogsCallback(vtkObject *obj, unsigned long eid, void *clientdata, void *calldata)
virtual FrameSizeType GetOutputFrameSize() const
static vtkIGSIOLogger * Instance()
Interface to a 3D positioning tool, video source, or generalized data stream.