Hello,
I writing a C++ application for Windows that handles video streaming over sockets. In this program, the main thread listens for connection attempts, and creates a new thread for each client that connects. That new thread essentially takes the data stream of the socket, converts it to a Mat, and then uses a VideoWriter to write each frame to an avi file. This new thread also limits each file to a set number of seconds in length. I've tested using one client and video gets recorded just fine. But the moment I try to create another thread for a second socket, VideoWriter fails to open the output in that new thread. Unfortunately VideoWriter fails quietly (I use writer.IsOpen() to check) so it's tough to trace where exactly it is breaking. I am using x264VFW as my video encoder, with default settings. For this example, the frames being sent over the socket are in grayscale, hence the CV_8UC1.
Here is the method that is called inside each client thread:
bool Streaming::DecodeVideoStream(Socket socket, std::string output_file_path, std::string thread_id)
{
Size S = Size({ 320, 240 });
time_t endwait;
time_t start = time(NULL);
time_t seconds = 30;
endwait = start + seconds;
VideoWriter writer(output_file_path, CV_FOURCC('x', '2', '6', '4'), 25, S);
if (!writer.isOpened())
logging.WriteLog("Failed to create video file: " + output_file_path);
while (start < endwait)
{
Mat webcamFrame = Mat::zeros(S, CV_8UC1);
int frameSize = webcamFrame.total()*webcamFrame.elemSize();
char* socketData = new char[frameSize];
memset(socketData, '\0', sizeof(char)*frameSize);
int packetSize = socket.RecvData(socketData, frameSize);
if (packetSize > 0)
{
writer.write(Mat(S, CV_8UC1, socketData));
}
else
{
delete[] socketData;
logging.WriteLog("Recv() returned -1. ", __FUNCTION__);
logging.WriteLog("Error code: " + std::to_string(WSAGetLastError()), __FUNCTION__);
return false;
}
delete[] socketData;
start = time(NULL);
}
logging.WriteLog(thread_id + " Reached clip time limit.", __FUNCTION__);
return true;
}
I would greatly appreciate any suggestions, as I do not have much experience with running VideoWriter in multiple threads. Maybe there's something stupid I'm missing.
-BinaryBits