You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.8 KiB
C++
64 lines
1.8 KiB
C++
#include <iostream>
|
|
#include <opencv2/opencv.hpp>
|
|
#include <sys/mman.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
|
|
int main() {
|
|
// 打开共享内存
|
|
int shmFd = shm_open("/video_shm", O_RDWR, 0666);
|
|
if (shmFd == -1) {
|
|
std::cerr << "Failed to open shared memory" << std::endl;
|
|
return 1;
|
|
}
|
|
void* sharedMemory = mmap(NULL, sizeof(int) * 3 + sizeof(double), PROT_READ | PROT_WRITE, MAP_SHARED, shmFd, 0);
|
|
if (sharedMemory == MAP_FAILED) {
|
|
std::cerr << "Failed to map shared memory" << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
// 获取共享内存指针
|
|
char* dataPtr = static_cast<char*>(sharedMemory);
|
|
|
|
// 创建窗口用于显示视频
|
|
cv::namedWindow("Received Video", cv::WINDOW_NORMAL);
|
|
|
|
// 接收并实时播放视频帧
|
|
while (true) {
|
|
// 读取图像尺寸
|
|
int width = *reinterpret_cast<int*>(dataPtr);
|
|
dataPtr += sizeof(int);
|
|
int height = *reinterpret_cast<int*>(dataPtr);
|
|
dataPtr += sizeof(int);
|
|
int type = *reinterpret_cast<int*>(dataPtr);
|
|
dataPtr += sizeof(int);
|
|
|
|
// 计算图像数据的大小
|
|
size_t imageDataSize = width * height * CV_ELEM_SIZE(type);
|
|
|
|
// 创建图像矩阵
|
|
cv::Mat frame(height, width, type, reinterpret_cast<void*>(dataPtr));
|
|
dataPtr += imageDataSize;
|
|
|
|
// 读取帧率信息
|
|
double fps = *reinterpret_cast<double*>(dataPtr);
|
|
dataPtr += sizeof(double);
|
|
|
|
// 显示图像
|
|
cv::imshow("Received Video", frame);
|
|
|
|
// 打印帧率
|
|
std::cout << "Received FPS: " << fps << std::endl;
|
|
|
|
// 按下 ESC 键退出循环
|
|
if (cv::waitKey(1) == 27)
|
|
break;
|
|
}
|
|
|
|
// 关闭共享内存
|
|
munmap(sharedMemory, sizeof(int) * 3 + sizeof(double));
|
|
close(shmFd);
|
|
|
|
return 0;
|
|
}
|