/***************************************************************************
    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, H323Connection & aConnection): 
	myConnection(aConnection), wavFile(aFileName, PFile::ReadOnly) 
{
	if (!wavFile.IsOpen()) 
	{
		PError << "Failed to open WAV file " << aFileName << endl;
		myConnection.ClearCall();
		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();
		myConnection.ClearCall();
		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 (!myConnection.IsEstablished()) 
	{
		PTRACE(2, "WavChannel::Read(): Connection not yet established");
		memset(buf, 0, len);
		lastReadCount = len; 
		readDelay.Delay(lastReadCount/2/8); 
		return true; 
	} 
	
	if (!wavFile.Read(buf, len)) 
		return false;
		
	lastReadCount = wavFile.GetLastReadCount(); 
	readDelay.Delay(lastReadCount/2/8); 
	
	if (lastReadCount < len) 
	{
		PTRACE(1, "WavChannel::Read(): end of file reached");
		myConnection.ClearCall(); 
	} 
	
	return lastReadCount > 0; 
} 


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

