Path: blob/master/modules/videoio/src/cap_winrt_bridge.hpp
16354 views
// videoio to XAML bridge for OpenCV12// Copyright (c) Microsoft Open Technologies, Inc.3// All rights reserved.4//5// (3 - clause BSD License)6//7// Redistribution and use in source and binary forms, with or without modification, are permitted provided that8// the following conditions are met:9//10// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the11// following disclaimer.12// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the13// following disclaimer in the documentation and/or other materials provided with the distribution.14// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or15// promote products derived from this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED18// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A19// PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY20// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,21// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING23// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE24// POSSIBILITY OF SUCH DAMAGE.2526#pragma once2728// this header is included in the XAML App, so it cannot include any29// OpenCV headers, or a static assert will be raised3031#include <ppl.h>32#include <ppltasks.h>33#include <concrt.h>34#include <agile.h>35#include <opencv2\core.hpp>3637#include <mutex>38#include <memory>39#include <atomic>40#include <functional>414243// Class VideoioBridge (singleton) is needed because the interface for44// VideoCapture_WinRT in cap_winrt_capture.hpp is fixed by OpenCV.45class VideoioBridge46{47public:4849static VideoioBridge& getInstance();5051// call after initialization52void setReporter(Concurrency::progress_reporter<int> pr) { reporter = pr; }5354// to be called from cvMain via cap_winrt on bg thread - non-blocking (async)55void requestForUIthreadAsync(int action);5657// TODO: modify in window.cpp: void cv::imshow( const String& winname, InputArray _img )58void imshow(/*cv::InputArray matToShow*/); // shows Mat in the cvImage element59void swapInputBuffers();60void allocateOutputBuffers();61void swapOutputBuffers();62void updateFrameContainer();63bool openCamera();64void allocateBuffers(int width, int height);6566int getDeviceIndex();67void setDeviceIndex(int index);68int getWidth();69void setWidth(int width);70int getHeight();71void setHeight(int height);7273std::atomic<bool> bIsFrameNew;74std::mutex inputBufferMutex; // input is double buffered75unsigned char * frontInputPtr; // OpenCV reads this76unsigned char * backInputPtr; // Video grabber writes this77std::atomic<unsigned long> frameCounter;78unsigned long currentFrame;7980std::mutex outputBufferMutex; // output is double buffered81Windows::UI::Xaml::Media::Imaging::WriteableBitmap^ frontOutputBuffer; // OpenCV write this82Windows::UI::Xaml::Media::Imaging::WriteableBitmap^ backOutputBuffer; // XAML reads this83Windows::UI::Xaml::Controls::Image ^cvImage;8485private:8687VideoioBridge() {88deviceIndex = 0;89width = 640;90height = 480;91deviceReady = false;92bIsFrameNew = false;93currentFrame = 0;94frameCounter = 0;95};9697// singleton98VideoioBridge(VideoioBridge const &);99void operator=(const VideoioBridge &);100101std::atomic<bool> deviceReady;102Concurrency::progress_reporter<int> reporter;103104// Mats are wrapped with singleton class, we do not support more than one105// capture device simultaneously with the design at this time106//107// nb. inputBufferMutex was not able to guarantee that OpenCV Mats were108// ready to accept data in the UI thread (memory access exceptions were thrown109// even though buffer address was good).110// Therefore allocation of Mats is also done on the UI thread before the video111// device is initialized.112cv::Mat frontInputMat;113cv::Mat backInputMat;114115int deviceIndex, width, height;116};117118