Source

h323ac / wavchan.cxx

Full commit
/***************************************************************************
    wavchan.cxx

    Copyright (c) 2002-2003  Vladimir Toncar

    The contents of this file are subject to the Mozilla Public License
    Version 1.1 (the "License"); you may not use this file except in
    compliance with the License. You may obtain a copy of the License at
    http://www.mozilla.org/MPL/

    Software distributed under the License is distributed on an "AS IS"
    basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
    See the License for the specific language governing rights and
    limitations under the License.

    The Original Code is oh323tut.

    The Initial Developer of the Original Code is Vladimir Toncar.
    All Rights Reserved.

    Contributor(s): ______________________________________.

 ***************************************************************************/

#include "wavchan.h"


// ***********************************************************************

WavChannel::WavChannel(const PString & aFileName, MyEndPoint & _myEndPoint): 
	myEndPoint(_myEndPoint), wavFile(aFileName, PFile::ReadOnly) 
{
	if (!wavFile.IsOpen()) 
	{
		PError << "Failed to open WAV file " << aFileName << endl;
      		myEndPoint.CloseProgramNow(); 
		return;
	} 
	if (wavFile.GetFormat() != PWAVFile::fmt_PCM 
		|| wavFile.GetChannels() != 1
		|| wavFile.GetSampleRate() != 8000
		|| wavFile.GetSampleSize() != 16)
	{
		PError << "WAV file " << aFileName << " has wrong format." << endl;
		wavFile.Close();
		myEndPoint.CloseProgramNow(); 
		return;
	} 
	
	PTRACE(1, "Created WAV channel for file " << aFileName);
	
} 


// ***********************************************************************

WavChannel::~WavChannel() 
{
	PTRACE(1, "Deleting WAV channel for file " << wavFile.GetName());
} 


// ***********************************************************************

BOOL WavChannel::Close() 
{
	return wavFile.Close();
} 


// ***********************************************************************

BOOL WavChannel::IsOpen() const 
{
	PTRACE(1, "WavChannnel::IsOpen(): " << wavFile.IsOpen());
	return wavFile.IsOpen();
} 


// ***********************************************************************

BOOL WavChannel::Write(const void * /*buf*/, PINDEX len) 
{
	PTRACE(2, "WavChannel::Write():" << len);
	lastWriteCount = len; 
	writeDelay.Delay(len/2/8); 
	return true;
} 


// ***********************************************************************

BOOL WavChannel::Read(void *buf, PINDEX len) 
{
	if (!wavFile.Read(buf, len))  {
		PTRACE(2, "WavChannel::read of " << len << " bytes failed, return false");
		myEndPoint.CloseProgramNow(); 
		return false;
	}
		
	lastReadCount = wavFile.GetLastReadCount(); 
	readDelay.Delay(lastReadCount/2/8); 
	
	if (lastReadCount < len) 
	{
		PTRACE(1, "WavChannel::Read(): end of file reached");
		myEndPoint.CloseProgramNow(); 
	} 

	if (lastReadCount == 0)
		myEndPoint.CloseProgramNow(); 
	
	return lastReadCount > 0; 
} 


// ***********************************************************************