我正在尝试通过套接字(C++)发送多个帧(以前取自实际视频文件),然后使用 VLC 播放。
我搜索了很多并没有找到解决方案。希望您能够帮助我。
所以,这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>
#include <iostream>
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
#include <string.h>
#include <time.h>
#include <errno.h>
//#include <fstream>
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp> // OpenCV window I/O
using namespace std;
#define PORT 6666
#define GROUP "127.0.0.1"
//#define INADDR_ANY
int serversock, clientsock;
int is_data_ready = 0;
struct sockaddr_in server, client;
int bytes = 0;
int count = 0;
int addrlen = sizeof(server);
int clielen = sizeof(client);
int opt = 1;
//methods
void quit(char* msg, int retval);
void quit(char* msg, int retval)
{
if (retval == 0) {
fprintf(stdout, (msg == NULL ? "" : msg));
fprintf(stdout, "\n");
} else {
fprintf(stderr, (msg == NULL ? "" : msg));
fprintf(stderr, "\n");
}
if (clientsock) closesocket(clientsock);
if (serversock) closesocket(serversock);
exit(retval);
}
int main()
{
// Initialize Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup failed with error: %ld\n", iResult);
return 1;
}
//char *imgname; //path e nome das imagens
int i=0;
char filename[50];
IplImage *img = cvLoadImage(<path\\imgname.jpg); //1ª imagem como referência
//IplImage *img2;
CvSize size;
size.width = img->width;
size.height = img->height;
/* setup server's IP and port */
memset(&server,0,sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(6666/*PORT*/);
server.sin_addr.s_addr = inet_addr("127.0.0.1"/*GROUP*/);
//server.sin_addr.s_addr = INADDR_ANY;
SOCKET serversock = socket(AF_INET, SOCK_STREAM, 0);
//SOCKET t;
//t = socket(AF_INET, SOCK_STREAM,NULL);
if (serversock < 0) { // or == -1
wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
//quit("socket() failed", 1);
}
/* bind the socket */
int b = bind(serversock, (const sockaddr*)&server, sizeof(server));
if (b < 0) {
wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
quit("bind() failed", 1);
}
/* wait for connection */
int l = listen(serversock, 5);
if(l < 0) {
quit("listen() failed.", 1);
}
setsockopt(serversock, SOL_SOCKET, SO_KEEPALIVE, (const char*) &opt, sizeof(int));
while(img != NULL)
{
sprintf(filename, "filter\\frame_%d.jpg", i);
img = cvLoadImage(filename);
if (img) {
int imgSize = (int) &size;
sendto(serversock, img->imageData, imgSize, 0, (const struct sockaddr*)&server, addrlen);
if(bytes < 0) { //error
wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
quit("sendto FAILED", 1);
}
//end socket stuff
cout << "Image sent!" << endl;
}
i++;
}
cvReleaseImage(&img);
}
Ant 然后我打开 VLC 并将其设置为在下一个地址接收网络流:rtp://127.0.0.1:6666
.
应用程序结束,VLC 不显示任何内容。
非常感谢!