[MPlayer-dev-eng] Problem with MPlayer in slave mode.

Christian HUAUX enitalp at videotron.ca
Wed Aug 9 15:57:26 CEST 2006


Hi.

I try since days to make mplayer in slave mode under windows with visual
C++.

If I launch mplayer with -slave toto.mp3 it actually play my mp3 and my app
get the startup console output from mplayer "MPlayer 1.0pre8-3.4.2 (c) ...
blabla"
When I try to write to the pipe I have no error but mplayer does nothing, 
Tried with "mplayer -slave -idle" get the same result. Tried thousand things
but mplayer seems to not listent for stdin .

Any idea before I hang myself ;p ?

For information here is my code.
ReadFromPipe works great, but the WriteToPipe does nothing and doesn't
signal any error.

Thanks.



#include "stdafx.h"
#include "MPlayer.h"


 
MPlayer::MPlayer(void)
{
	piProcInfo.hProcess=NULL;
	WritePipe=ReadPipe=NULL;
}

MPlayer::~MPlayer(void)
{
DWORD excode;
	GetExitCodeProcess(piProcInfo.hProcess, &excode);
	if(excode == STILL_ACTIVE)
	{
		CloseHandle( piProcInfo.hThread );
		CloseHandle(piProcInfo.hProcess);
		CloseHandle(WritePipe);
		CloseHandle(ReadPipe);
		ExitProcess(0);
	}
}
//
// Init
//
bool MPlayer::Init(char *file)
{ 
SECURITY_ATTRIBUTES saAttr; 
BOOL fSuccess; 
 

	saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
	saAttr.bInheritHandle = TRUE; 
	saAttr.lpSecurityDescriptor = NULL;  
	// pipe out read
	if (! CreatePipe(&ReadPipe, &Dummy1, &saAttr, 0)) 
	{
		ASSERTC_Z(false,"Stdout pipe creation failed\n");
		return false;
	} 
	// Pipe in wrtie
	saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
	saAttr.bInheritHandle = TRUE; 
	saAttr.lpSecurityDescriptor = NULL;  
	// Create a pipe for the child process's STDIN. 
	if (! CreatePipe(&Dummy2, &WritePipe, &saAttr, 0)) 
	{
		ASSERTC_Z(false,"Stdin pipe creation failed\n"); 
		return false;
	}
 
	// Now create the child process.    
	fSuccess = CreateChildProcess(file);
	if (! fSuccess)
	{
		ASSERTC_Z(false,"Create process failed"); 
		return false;
	}
	//CloseHandle(Dummy1); 
	//CloseHandle(Dummy2);
	return true; 
} 

BOOL MPlayer::CreateChildProcess(char *file) 
{ 
STARTUPINFO siStartInfo;
BOOL bFuncRetn = FALSE; 
 
	// Set up members of the PROCESS_INFORMATION structure. 
	ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );

	// Set up members of the STARTUPINFO structure. 
 
	ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
	siStartInfo.cb = sizeof(STARTUPINFO); 
	siStartInfo.hStdError = Dummy1;
	siStartInfo.hStdOutput = Dummy1;
	siStartInfo.hStdInput = Dummy2;
	siStartInfo.dwFlags = STARTF_USESTDHANDLES;
 
	// Create the child process. 
	char CommandLine[255];
	sprintf(CommandLine,"mplayer\\mplayer.exe -slave -identify -colorkey
0x101010 -framedrop -autosync 100 -wid %d -idle
-quiet",gData.GetHwnd()->m_hWnd,file);
	bFuncRetn = CreateProcess(NULL, 
      CommandLine,       // command line 
	  NULL,          // process security attributes 
	  NULL,          // primary thread security attributes 
	  TRUE,          // handles are inherited 
	  0/*DETACHED_PROCESS*/,             // creation flags 
	  NULL,          // use parent's environment 
	  NULL,          // use parent's current directory 
	  &siStartInfo,  // STARTUPINFO pointer 
	  &piProcInfo);  // receives PROCESS_INFORMATION 
   
	if (bFuncRetn == 0)
	{
		ASSERTC_Z(false,"MPlayer CreateProcess failed");
		return false;
	}
	else 
	{
		return bFuncRetn;
	}
	return true;
}

//
//
//
void MPlayer::PlayFile(char *fname)
{
char string[255];
DWORD excode;

	GetExitCodeProcess(piProcInfo.hProcess, &excode);
	if(excode != STILL_ACTIVE)
	{
		Init(fname);
	}
	else
	{
		sprintf(string,"loadfile %s\n",fname);
		AddCommand(string);
	}
}

void MPlayer::AddCommand(char *cmd)
{
int Index;
DWORD excode;

	Index=CommandDA.Add();
	strcpy(CommandDA[Index].Command,cmd);
	GetExitCodeProcess(piProcInfo.hProcess, &excode);

}

void MPlayer::Update(void)
{
DWORD excode;

	GetExitCodeProcess(piProcInfo.hProcess, &excode);
	if(excode == STILL_ACTIVE)
	{
		AddCommand("get_percent_pos");
	}
	else
	{
		if (WritePipe!=NULL)
		{
			CloseHandle(WritePipe); 
			CloseHandle(ReadPipe);
			WritePipe=ReadPipe=NULL;
		}
		return;
	}
	ReadFromPipe();
	WriteToPipe();
}


void MPlayer::WriteToPipe(void) 
{ 
DWORD dwRead, dwWritten; 
CHAR chBuf[BUFSIZE]; 

	for(int i=0;i<CommandDA.GetSize();i++)
	{ 
		if (! WriteFile(WritePipe, CommandDA[i].Command,
strlen(CommandDA[i].Command),&dwWritten, NULL)) 
		{
			CommandDA.Empty();
			ASSERTC_Z(false,"Error while writing command");
			break; 
		}
	}  
	CommandDA.Empty();
} 
 
void MPlayer::ReadFromPipe(void) 
{ 
DWORD dwRead, dwWritten,available; 
CHAR chBuf[BUFSIZE]; 

	for (;;) 
	{ 
		if(!PeekNamedPipe(ReadPipe, chBuf,BUFSIZE, &dwRead,
&available, NULL))
			break;
		if (dwRead)
		{
			if( !ReadFile( ReadPipe, chBuf, BUFSIZE,
&dwRead,NULL) || dwRead < BUFSIZE) 
				break; 
		}
		else
		{
			break;
		}
	} 
}




More information about the MPlayer-dev-eng mailing list