我已经编写了相关代码,或许能够帮到您。在我的情况下,我正在流式传输一张图片并在ffplay中播放它。我启动了3个线程,其中一个创建管道(在演示管道中执行“initializePipe”),另一个监听管道的创建并启动ffplay,最后一个是写入图像到管道中的写入线程。
demoPipes.cpp
#include "demoPipes.h"
using namespace std;
#define MAX_THREADS 100
void ErrorHandler(LPTSTR lpszFunction);
typedef struct
{
HANDLE pipe;
LPCSTR MEDIA_PIPE;
} Session;
int _tmain()
{
Session* s = (Session*)malloc(sizeof(Session));
s->pipe = NULL;
s->MEDIA_PIPE = "\\\\.\\pipe\\screenRec";
DWORD dwThreadIdArray[MAX_THREADS];
HANDLE hThreadArray[MAX_THREADS];
for( int i=0; i<MAX_THREADS; i++ )
{
if(i==0){
hThreadArray[i] = CreateThread(
NULL,
0,
initializePipe,
(LPVOID) s,
0,
&dwThreadIdArray[i]);
}
if(i>1){
PipeMetaData* pipe_data1 = fetch_file("D:\\images.png", s->pipe);
hThreadArray[i] = CreateThread(
NULL,
0,
send_media_to_recorder,
pipe_data1,
0,
&dwThreadIdArray[i]);
}
if(i==1){
hThreadArray[i] = CreateThread(
NULL,
0,
recorder_pipe_function,
0,
0,
&dwThreadIdArray[i]);
}
if (dwThreadIdArray[i] == NULL)
{
ErrorHandler(TEXT("CreateThread"));
ExitProcess(3);
}
}
WaitForMultipleObjects(MAX_THREADS, hThreadArray, TRUE, INFINITE);
CloseHandle(s->pipe);
for(int i=0; i<MAX_THREADS; i++)
{
CloseHandle(hThreadArray[i]);
}
getchar();
return 0;
}
void ErrorHandler(LPTSTR lpszFunction)
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR) lpMsgBuf) + lstrlen((LPCTSTR) lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR) lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
clientpipe.cpp
#include "client_pipe.h"
using namespace std;
DWORD WINAPI recorder_pipe_function( LPVOID lpParam){
const char* MEDIA_PIPE = "\\\\.\\pipe\\screenRec";
WaitNamedPipeA(MEDIA_PIPE,1000);
start_recording_session(MEDIA_PIPE);
return 0;
}
void start_recording_session(const char* media_pipe){
char* command = (char*)malloc(sizeof(char)*100+1);
command[0] ='\0';
strcpy_s(command,sizeof(char)*100+1,"start ffplay -i ");
strcat_s(command,sizeof(char)*100+1, media_pipe);
std::cout << "Executing ffmpeg command\n";
int i=system (command);
std::cout << "The value returned was: " << i << std::endl;
}
ServerPipe.cpp
#include "server_pipe.h"
using namespace std;
typedef struct
{
HANDLE pipe;
LPCSTR MEDIA_PIPE;
} Session;
DWORD WINAPI initializePipe( LPVOID lpParam ) {
printf("Creating an instance of a named pipe...");
Session* s = (Session*) lpParam;
LPCSTR MEDIA_PIPE = s->MEDIA_PIPE;
s->pipe = CreateNamedPipeA(
MEDIA_PIPE,
PIPE_ACCESS_OUTBOUND,
PIPE_TYPE_BYTE,
1,
0,
0,
0,
NULL
);
if (s->pipe == NULL ||s->pipe == INVALID_HANDLE_VALUE) {
wcout << "Failed to create outbound pipe instance.";
return NULL;
}
wcout << "Waiting for a client to connect to the pipe..." << endl;
BOOL result = ConnectNamedPipe(s->pipe, NULL);
if (!result) {
wcout << "Failed to make connection on named pipe." << endl;
CloseHandle(s->pipe);
system("pause");
return NULL;
}
}
PipeMetaData* fetch_file(char* file_path,HANDLE pipe){
PipeMetaData* data = (PipeMetaData*) malloc(sizeof(PipeMetaData));
ifstream infile;
infile.open(file_path,std::ios::binary);
infile.seekg(0,std::ios::end);
data->byte_size = infile.tellg();
data->file_vector =(char*) malloc(sizeof(char)*data->byte_size);
infile.seekg(0,std::ios::beg);
infile.read(&(data->file_vector)[0],data->byte_size);
data->packet = &(data->file_vector)[0];
data->pipe = pipe;
wcout<<data->byte_size<<endl;
return data;
}
DWORD WINAPI send_media_to_recorder(LPVOID lpParam){
PipeMetaData* p = (PipeMetaData*) lpParam;
DWORD numBytesWritten = 0;
BOOL result = WriteFile(
p->pipe,
p->packet,
p->byte_size,
&numBytesWritten,
NULL
);
if (result) {
wcout << "Number of bytes sent: " << numBytesWritten << endl;
} else {
wcout << "Failed to send data." << endl;
GetLastError();
}
free(p);
return 0;
}