Skip to content

Instantly share code, notes, and snippets.

@xseignard
Created March 29, 2020 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xseignard/d9a91da351537e0e38c56f74dbe3fc18 to your computer and use it in GitHub Desktop.
Save xseignard/d9a91da351537e0e38c56f74dbe3fc18 to your computer and use it in GitHub Desktop.
#include <opencv2/opencv.hpp>
#include "./include/Processing.NDI.Lib.h"
using namespace cv;
using namespace std;
int main(int argc, char* argv[]) {
// Init opencv
VideoCapture capture(0);
Mat cameraFrame;
Mat ndiFrame;
if (!capture.isOpened()) {
cout << "Could not open camera" << endl;
return -1;
}
// Init NDI
if (!NDIlib_initialize()) {
cout << "Failed to init NDI" << endl;
return -1;
}
// Sender
const char* name = "Coucou";
const NDIlib_send_create_t opts = NDIlib_send_create_t(name, NULL, true, true);
NDIlib_send_instance_t pNDI_send = NDIlib_send_create(&opts);
if (!pNDI_send) {
cout << "Failed to create a NDI sender" << endl;
return -1;
}
// NDI frame
// size of the grabbed camera stream
const int W = 1280;
const int H = 720;
const int size = 2;
NDIlib_video_frame_v2_t NDI_video_frame;
NDI_video_frame.xres = W;
NDI_video_frame.yres = H;
/* Available formats:
NDIlib_FourCC_type_UYVY
NDIlib_FourCC_type_UYVA
NDIlib_FourCC_type_P216
NDIlib_FourCC_type_PA16
NDIlib_FourCC_type_YV12
NDIlib_FourCC_type_I420
NDIlib_FourCC_type_NV12
NDIlib_FourCC_type_BGRA
NDIlib_FourCC_type_BGRX
NDIlib_FourCC_type_RGBA
NDIlib_FourCC_type_RGBX
*/
NDI_video_frame.FourCC = NDIlib_FourCC_type_BGRX;
// what's that ?!
NDI_video_frame.line_stride_in_bytes = W * size;
NDI_video_frame.p_data = (uint8_t*)malloc(W * H * size);
while (true) {
capture.read(cameraFrame);
cvtColor(cameraFrame, ndiFrame, COLOR_BGR2XYZ);
NDI_video_frame.p_data = (uint8_t*)ndiFrame.data;
NDIlib_send_send_video_async_v2(pNDI_send, &NDI_video_frame);
imshow("output", ndiFrame);
if (cv::waitKey(50) >= 0) break;
}
// Free the video frames
free(NDI_video_frame.p_data);
free(&cameraFrame);
free(&ndiFrame);
// Destroy the NDI sender
NDIlib_send_destroy(pNDI_send);
// Not required, but nice
NDIlib_destroy();
// Success
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment