@@ -15,6 +15,7 @@ Authors include: Adam Rankin
1515// Local includes
1616#include " PlusConfigure.h"
1717#include " PixelCodec.h"
18+ #include " vtkPlusCameraControlParameters.h"
1819#include " vtkPlusChannel.h"
1920#include " vtkPlusDataSource.h"
2021#include " vtkPlusMmfVideoSource.h"
@@ -36,6 +37,7 @@ Authors include: Adam Rankin
3637// Windows includes
3738#include < lmerr.h>
3839#include < shlwapi.h>
40+ #include < strmif.h>
3941#include < tchar.h>
4042#include < windows.h>
4143
@@ -44,7 +46,7 @@ Authors include: Adam Rankin
4446namespace
4547{
4648 const unsigned int DEFAULT_DEVICE_ID = 0 ;
47- const FrameSizeType DEFAULT_FRAME_SIZE = {640 , 480 , 1 };
49+ const FrameSizeType DEFAULT_FRAME_SIZE = { 640 , 480 , 1 };
4850 const double DEFAULT_ACQUISITION_RATE = 30 ;
4951 const std::wstring DEFAULT_PIXEL_TYPE_NAME = L" YUY2" ;
5052 const GUID DEFAULT_PIXEL_TYPE = MFVideoFormat_YUY2; // see http://msdn.microsoft.com/en-us/library/windows/desktop/aa370819(v=vs.85).aspx
@@ -174,7 +176,7 @@ STDMETHODIMP MmfVideoSourceReader::OnReadSample(HRESULT hrStatus, DWORD dwStream
174176 UINT32 actualHeight = 0 ;
175177 ::MFGetAttributeSize (pType, MF_MT_FRAME_SIZE, &actualWidth, &actualHeight);
176178 if (actualWidth != this ->PlusDevice ->ActiveVideoFormat .FrameSize [0 ] ||
177- actualHeight != this ->PlusDevice ->ActiveVideoFormat .FrameSize [1 ])
179+ actualHeight != this ->PlusDevice ->ActiveVideoFormat .FrameSize [1 ])
178180 {
179181 LOG_ERROR (" Unexpected frame size: " << actualWidth << " x" << actualHeight << " (expected: " << this ->PlusDevice ->ActiveVideoFormat .FrameSize [0 ] << " x" << this ->PlusDevice ->ActiveVideoFormat .FrameSize [1 ] << " )" );
180182 return S_FALSE;
@@ -246,6 +248,7 @@ vtkStandardNewMacro(vtkPlusMmfVideoSource);
246248vtkPlusMmfVideoSource::vtkPlusMmfVideoSource ()
247249 : FrameIndex(0 )
248250 , Mutex(vtkSmartPointer<vtkIGSIORecursiveCriticalSection>::New())
251+ , CameraControlParameters(vtkSmartPointer<vtkPlusCameraControlParameters>::New())
249252{
250253 this ->MmfSourceReader = new MmfVideoSourceReader (this );
251254 this ->RequireImageOrientationInConfiguration = true ;
@@ -278,6 +281,8 @@ void vtkPlusMmfVideoSource::PrintSelf(ostream& os, vtkIndent indent)
278281 this ->Superclass ::PrintSelf (os, indent);
279282
280283 os << indent << " FrameIndex: " << (this ->FrameIndex ? " On\n " : " Off\n " );
284+
285+ this ->CameraControlParameters ->PrintSelf (os, indent.GetNextIndent ());
281286}
282287
283288// ----------------------------------------------------------------------------
@@ -305,12 +310,12 @@ PlusStatus vtkPlusMmfVideoSource::InternalConnect()
305310 }
306311
307312 if (!MfVideoCapture::MediaFoundationVideoCaptureApi::GetInstance ().SetupDevice (this ->RequestedVideoFormat .DeviceId , this ->RequestedVideoFormat .StreamIndex ,
308- this ->RequestedVideoFormat .FrameSize [0 ], this ->RequestedVideoFormat .FrameSize [1 ], this ->AcquisitionRate , pixelFormat))
313+ this ->RequestedVideoFormat .FrameSize [0 ], this ->RequestedVideoFormat .FrameSize [1 ], this ->AcquisitionRate , pixelFormat))
309314 {
310315 LOG_WARNING_W (" Unable to init capture device with requested details:"
311- << " device ID: " << this ->RequestedVideoFormat .DeviceId << " (" << GetRequestedDeviceName () << " ) stream " << this ->RequestedVideoFormat .StreamIndex
312- << " , " << this ->RequestedVideoFormat .FrameSize [0 ] << " x" << this ->RequestedVideoFormat .FrameSize [1 ]
313- << " , " << this ->AcquisitionRate << " Hz, " << this ->ActiveVideoFormat .PixelFormatName );
316+ << " device ID: " << this ->RequestedVideoFormat .DeviceId << " (" << GetRequestedDeviceName () << " ) stream " << this ->RequestedVideoFormat .StreamIndex
317+ << " , " << this ->RequestedVideoFormat .FrameSize [0 ] << " x" << this ->RequestedVideoFormat .FrameSize [1 ]
318+ << " , " << this ->AcquisitionRate << " Hz, " << this ->ActiveVideoFormat .PixelFormatName );
314319
315320 LogListOfCaptureVideoFormats (this ->RequestedVideoFormat .DeviceId );
316321
@@ -328,9 +333,9 @@ PlusStatus vtkPlusMmfVideoSource::InternalConnect()
328333 this ->ActiveVideoFormat .PixelFormatName = DEFAULT_PIXEL_TYPE_NAME;
329334
330335 LOG_INFO_W (" Backing up to connecting with default capture settings:"
331- << " device ID: " << this ->ActiveVideoFormat .DeviceId << " (" << GetActiveDeviceName () << " )"
332- << " , " << this ->ActiveVideoFormat .FrameSize [0 ] << " x" << this ->ActiveVideoFormat .FrameSize [1 ]
333- << " , " << DEFAULT_ACQUISITION_RATE << " Hz, " << this ->ActiveVideoFormat .PixelFormatName );
336+ << " device ID: " << this ->ActiveVideoFormat .DeviceId << " (" << GetActiveDeviceName () << " )"
337+ << " , " << this ->ActiveVideoFormat .FrameSize [0 ] << " x" << this ->ActiveVideoFormat .FrameSize [1 ]
338+ << " , " << DEFAULT_ACQUISITION_RATE << " Hz, " << this ->ActiveVideoFormat .PixelFormatName );
334339 }
335340
336341 this ->MmfSourceReader ->CaptureSource = MfVideoCapture::MediaFoundationVideoCaptureApi::GetInstance ().GetMediaSource (this ->ActiveVideoFormat .DeviceId );
@@ -343,6 +348,12 @@ PlusStatus vtkPlusMmfVideoSource::InternalConnect()
343348 unsigned int frameRate = MfVideoCapture::MediaFoundationVideoCaptureApi::GetInstance ().GetFrameRate (this ->ActiveVideoFormat .DeviceId );
344349 LOG_DEBUG_W (" vtkPlusMmfVideoSource connected to device '" << GetActiveDeviceName () << " ' at frame rate of " << frameRate << " Hz" );
345350
351+ if (this ->InternalApplyCameraControlParameterChange () == PLUS_FAIL)
352+ {
353+ LOG_ERROR (" Failed to change imaging parameters in the device" );
354+ return PLUS_FAIL;
355+ }
356+
346357 this ->FrameIndex = 0 ;
347358
348359 return PLUS_SUCCESS;
@@ -394,7 +405,7 @@ PlusStatus vtkPlusMmfVideoSource::InternalStartRecording()
394405 if (FAILED (hr))
395406 {
396407 LOG_WARNING_W (" Unable to set SourceReader output to requested format: " << this ->RequestedVideoFormat .PixelFormatName
397- << " . Using device default." );
408+ << " . Using device default." );
398409 }
399410 SafeRelease (&pDecodeType);
400411
@@ -485,7 +496,7 @@ PlusStatus vtkPlusMmfVideoSource::ReadConfiguration(vtkXMLDataElement* rootConfi
485496 this ->RequestedVideoFormat .StreamIndex = (DWORD)streamIndex;
486497 }
487498
488- int requestedFrameSize[2 ] = {static_cast <int >(DEFAULT_FRAME_SIZE[0 ]), static_cast <int >(DEFAULT_FRAME_SIZE[1 ])};
499+ int requestedFrameSize[2 ] = { static_cast <int >(DEFAULT_FRAME_SIZE[0 ]), static_cast <int >(DEFAULT_FRAME_SIZE[1 ]) };
489500 if (deviceConfig->GetVectorAttribute (" FrameSize" , 2 , requestedFrameSize))
490501 {
491502 if (requestedFrameSize[0 ] < 0 || requestedFrameSize[1 ] < 0 )
@@ -505,6 +516,12 @@ PlusStatus vtkPlusMmfVideoSource::ReadConfiguration(vtkXMLDataElement* rootConfi
505516 this ->RequestedVideoFormat .PixelFormatName = std::wstring (attr.begin (), attr.end ());
506517 }
507518
519+ XML_FIND_NESTED_ELEMENT_OPTIONAL (cameraParameters, deviceConfig, vtkPlusCameraControlParameters::CAMERA_CONTROL_XML_ELEMENT_TAG);
520+ if (cameraParameters != NULL )
521+ {
522+ this ->CameraControlParameters ->ReadConfiguration (deviceConfig);
523+ }
524+
508525 return PLUS_SUCCESS;
509526}
510527
@@ -527,6 +544,8 @@ PlusStatus vtkPlusMmfVideoSource::WriteConfiguration(vtkXMLDataElement* rootConf
527544 auto attr = std::string (this ->RequestedVideoFormat .PixelFormatName .begin (), this ->RequestedVideoFormat .PixelFormatName .end ());
528545 deviceConfig->SetAttribute (" VideoFormat" , attr.c_str ());
529546
547+ this ->CameraControlParameters ->WriteConfiguration (deviceConfig);
548+
530549 return PLUS_SUCCESS;
531550}
532551
@@ -725,4 +744,159 @@ void vtkPlusMmfVideoSource::LogListOfCaptureDevices()
725744std::wstring vtkPlusMmfVideoSource::GetCaptureDeviceName (unsigned int deviceId)
726745{
727746 return MfVideoCapture::MediaFoundationVideoCaptureApi::GetInstance ().GetCaptureDeviceName (deviceId);
747+ }
748+
749+ // ----------------------------------------------------------------------------
750+ PlusStatus vtkPlusMmfVideoSource::InternalApplyCameraControlParameterChange ()
751+ {
752+ if (this ->MmfSourceReader ->CaptureSource == NULL )
753+ {
754+ LOG_ERROR (" Cannot apply camera control parameter change, capture source is not initialized" );
755+ return PLUS_FAIL;
756+ }
757+
758+ MfVideoCapture::CaptureDeviceParameters parameters =
759+ MfVideoCapture::MediaFoundationVideoCaptureApi::GetInstance ().GetParameters (this ->ActiveVideoFormat .DeviceId );
760+
761+ // //////////////////////////
762+ // Pan
763+ if (this ->CameraControlParameters ->IsSet (vtkPlusCameraControlParameters::KEY_PAN_DEGREES)
764+ && this ->CameraControlParameters ->IsPending (vtkPlusCameraControlParameters::KEY_PAN_DEGREES))
765+ {
766+ double panDegrees = 0.0 ;
767+ if (this ->CameraControlParameters ->GetPanDegrees (panDegrees) != PLUS_SUCCESS)
768+ {
769+ LOG_ERROR (" Failed to get pan camera control parameter" );
770+ return PLUS_FAIL;
771+ }
772+ parameters.CameraControlParameters [MfVideoCapture::CaptureDeviceParameters::Pan].CurrentValue = panDegrees;
773+ this ->CameraControlParameters ->SetPending (vtkPlusCameraControlParameters::KEY_PAN_DEGREES, false );
774+ }
775+
776+ // //////////////////////////
777+ // Tilt
778+ if (this ->CameraControlParameters ->IsSet (vtkPlusCameraControlParameters::KEY_TILT_DEGREES)
779+ && this ->CameraControlParameters ->IsPending (vtkPlusCameraControlParameters::KEY_TILT_DEGREES))
780+ {
781+ double tiltDegrees = 0.0 ;
782+ if (this ->CameraControlParameters ->GetTiltDegrees (tiltDegrees) != PLUS_SUCCESS)
783+ {
784+ LOG_ERROR (" Failed to get tilt camera control parameter" );
785+ return PLUS_FAIL;
786+ }
787+ parameters.CameraControlParameters [MfVideoCapture::CaptureDeviceParameters::Tilt].CurrentValue = tiltDegrees;
788+ this ->CameraControlParameters ->SetPending (vtkPlusCameraControlParameters::KEY_TILT_DEGREES, false );
789+ }
790+
791+ // //////////////////////////
792+ // Roll
793+ if (this ->CameraControlParameters ->IsSet (vtkPlusCameraControlParameters::KEY_ROLL_DEGREES)
794+ && this ->CameraControlParameters ->IsPending (vtkPlusCameraControlParameters::KEY_ROLL_DEGREES))
795+ {
796+ double rollDegrees = 0.0 ;
797+ if (this ->CameraControlParameters ->GetRollDegrees (rollDegrees) != PLUS_SUCCESS)
798+ {
799+ LOG_ERROR (" Failed to get roll camera control parameter" );
800+ return PLUS_FAIL;
801+ }
802+ parameters.CameraControlParameters [MfVideoCapture::CaptureDeviceParameters::Roll].CurrentValue = rollDegrees;
803+ this ->CameraControlParameters ->SetPending (vtkPlusCameraControlParameters::KEY_ROLL_DEGREES, false );
804+ }
805+
806+ // //////////////////////////
807+ // Zoom
808+ if (this ->CameraControlParameters ->IsSet (vtkPlusCameraControlParameters::KEY_ZOOM_MM)
809+ && this ->CameraControlParameters ->IsPending (vtkPlusCameraControlParameters::KEY_ZOOM_MM))
810+ {
811+ double zoomMm = 0.0 ;
812+ if (this ->CameraControlParameters ->GetZoomMm (zoomMm) != PLUS_SUCCESS)
813+ {
814+ LOG_ERROR (" Failed to get zoom camera control parameter" );
815+ return PLUS_FAIL;
816+ }
817+ parameters.CameraControlParameters [MfVideoCapture::CaptureDeviceParameters::Zoom].CurrentValue = zoomMm;
818+ this ->CameraControlParameters ->SetPending (vtkPlusCameraControlParameters::KEY_ZOOM_MM, false );
819+ }
820+
821+ // //////////////////////////
822+ // Iris
823+ if (this ->CameraControlParameters ->IsSet (vtkPlusCameraControlParameters::KEY_IRIS_FSTOP)
824+ && this ->CameraControlParameters ->IsPending (vtkPlusCameraControlParameters::KEY_IRIS_FSTOP))
825+ {
826+ int irisFStop = 0.0 ;
827+ if (this ->CameraControlParameters ->GetIrisFStop (irisFStop) != PLUS_SUCCESS)
828+ {
829+ LOG_ERROR (" Failed to get iris camera control parameter" );
830+ return PLUS_FAIL;
831+ }
832+ parameters.CameraControlParameters [MfVideoCapture::CaptureDeviceParameters::Iris].CurrentValue = irisFStop;
833+ this ->CameraControlParameters ->SetPending (vtkPlusCameraControlParameters::KEY_IRIS_FSTOP, false );
834+ }
835+
836+
837+ // //////////////////////////
838+ // Exposure
839+ if (this ->CameraControlParameters ->IsSet (vtkPlusCameraControlParameters::KEY_EXPOSURE_LOG2SECONDS)
840+ && this ->CameraControlParameters ->IsPending (vtkPlusCameraControlParameters::KEY_EXPOSURE_LOG2SECONDS))
841+ {
842+ int exposureLog2Sec = 0.0 ;
843+ if (this ->CameraControlParameters ->GetExposureLog2Seconds (exposureLog2Sec) != PLUS_SUCCESS)
844+ {
845+ LOG_ERROR (" Failed to get exposure camera control parameter" );
846+ return PLUS_FAIL;
847+ }
848+ parameters.CameraControlParameters [MfVideoCapture::CaptureDeviceParameters::Exposure].CurrentValue = exposureLog2Sec;
849+ this ->CameraControlParameters ->SetPending (vtkPlusCameraControlParameters::KEY_EXPOSURE_LOG2SECONDS, false );
850+ }
851+
852+ // //////////////////////////
853+ // AutoExposure
854+ if (this ->CameraControlParameters ->IsSet (vtkPlusCameraControlParameters::KEY_AUTO_EXPOSURE)
855+ && this ->CameraControlParameters ->IsPending (vtkPlusCameraControlParameters::KEY_AUTO_EXPOSURE))
856+ {
857+ bool autoExposure = false ;
858+ if (this ->CameraControlParameters ->GetAutoExposure (autoExposure) != PLUS_SUCCESS)
859+ {
860+ LOG_ERROR (" Failed to get auto exposure camera control parameter" );
861+ return PLUS_FAIL;
862+ }
863+ parameters.CameraControlParameters [MfVideoCapture::CaptureDeviceParameters::Exposure].Flag = autoExposure ? CameraControl_Flags_Auto : CameraControl_Flags_Manual;
864+ this ->CameraControlParameters ->SetPending (vtkPlusCameraControlParameters::KEY_AUTO_EXPOSURE, false );
865+ }
866+
867+ // //////////////////////////
868+ // Focus
869+ if (this ->CameraControlParameters ->IsSet (vtkPlusCameraControlParameters::KEY_FOCUS_MM)
870+ && this ->CameraControlParameters ->IsPending (vtkPlusCameraControlParameters::KEY_FOCUS_MM))
871+ {
872+ double focusMm = 0.0 ;
873+ if (this ->CameraControlParameters ->GetFocusMm (focusMm) != PLUS_SUCCESS)
874+ {
875+ LOG_ERROR (" Failed to get focus camera control parameter" );
876+ return PLUS_FAIL;
877+ }
878+ parameters.CameraControlParameters [MfVideoCapture::CaptureDeviceParameters::Focus].CurrentValue = focusMm;
879+ this ->CameraControlParameters ->SetPending (vtkPlusCameraControlParameters::KEY_FOCUS_MM, false );
880+ }
881+
882+ // //////////////////////////
883+ // AutoFocus
884+ if (this ->CameraControlParameters ->IsSet (vtkPlusCameraControlParameters::KEY_AUTO_FOCUS)
885+ && this ->CameraControlParameters ->IsPending (vtkPlusCameraControlParameters::KEY_AUTO_FOCUS))
886+ {
887+ bool autoFocus = false ;
888+ if (this ->CameraControlParameters ->GetAutoFocus (autoFocus) != PLUS_SUCCESS)
889+ {
890+ LOG_ERROR (" Failed to get auto focus camera control parameter" );
891+ return PLUS_FAIL;
892+ }
893+ parameters.CameraControlParameters [MfVideoCapture::CaptureDeviceParameters::Focus].Flag = autoFocus ? CameraControl_Flags_Auto : CameraControl_Flags_Manual;
894+ this ->CameraControlParameters ->SetPending (vtkPlusCameraControlParameters::KEY_AUTO_FOCUS, false );
895+ }
896+
897+ // //////////////////////////
898+ // Update the parameters
899+ MfVideoCapture::MediaFoundationVideoCaptureApi::GetInstance ().SetParameters (this ->ActiveVideoFormat .DeviceId , parameters);
900+
901+ return PLUS_SUCCESS;
728902}
0 commit comments