1 |
/***************************************************************************
|
2 |
wavchan.cxx
|
3 |
|
4 |
Copyright (c) 2002-2003 Vladimir Toncar
|
5 |
|
6 |
The contents of this file are subject to the Mozilla Public License
|
7 |
Version 1.1 (the "License"); you may not use this file except in
|
8 |
compliance with the License. You may obtain a copy of the License at
|
9 |
http://www.mozilla.org/MPL/
|
10 |
|
11 |
Software distributed under the License is distributed on an "AS IS"
|
12 |
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
|
13 |
See the License for the specific language governing rights and
|
14 |
limitations under the License.
|
15 |
|
16 |
The Original Code is oh323tut.
|
17 |
|
18 |
The Initial Developer of the Original Code is Vladimir Toncar.
|
19 |
All Rights Reserved.
|
20 |
|
21 |
Contributor(s): ______________________________________.
|
22 |
|
23 |
***************************************************************************/
|
24 |
|
25 |
#include "wavchan.h"
|
26 |
|
27 |
|
28 |
// ***********************************************************************
|
29 |
|
30 |
WavChannel::WavChannel(const PString & aFileName, H323Connection & aConnection):
|
31 |
myConnection(aConnection), wavFile(aFileName, PFile::ReadOnly)
|
32 |
{
|
33 |
if (!wavFile.IsOpen())
|
34 |
{
|
35 |
PError << "Failed to open WAV file " << aFileName << endl;
|
36 |
myConnection.ClearCall();
|
37 |
return;
|
38 |
}
|
39 |
if (wavFile.GetFormat() != PWAVFile::fmt_PCM
|
40 |
|| wavFile.GetChannels() != 1
|
41 |
|| wavFile.GetSampleRate() != 8000
|
42 |
|| wavFile.GetSampleSize() != 16)
|
43 |
{
|
44 |
PError << "WAV file " << aFileName << " has wrong format." << endl;
|
45 |
wavFile.Close();
|
46 |
myConnection.ClearCall();
|
47 |
return;
|
48 |
}
|
49 |
|
50 |
PTRACE(1, "Created WAV channel for file " << aFileName);
|
51 |
|
52 |
}
|
53 |
|
54 |
|
55 |
// ***********************************************************************
|
56 |
|
57 |
WavChannel::~WavChannel()
|
58 |
{
|
59 |
PTRACE(1, "Deleting WAV channel for file " << wavFile.GetName());
|
60 |
}
|
61 |
|
62 |
|
63 |
// ***********************************************************************
|
64 |
|
65 |
BOOL WavChannel::Close()
|
66 |
{
|
67 |
return wavFile.Close();
|
68 |
}
|
69 |
|
70 |
|
71 |
// ***********************************************************************
|
72 |
|
73 |
BOOL WavChannel::IsOpen() const
|
74 |
{
|
75 |
PTRACE(1, "WavChannnel::IsOpen(): " << wavFile.IsOpen());
|
76 |
return wavFile.IsOpen();
|
77 |
}
|
78 |
|
79 |
|
80 |
// ***********************************************************************
|
81 |
|
82 |
BOOL WavChannel::Write(const void *buf, PINDEX len)
|
83 |
{
|
84 |
PTRACE(2, "WavChannel::Write():" << len);
|
85 |
lastWriteCount = len;
|
86 |
writeDelay.Delay(len/2/8);
|
87 |
return true;
|
88 |
}
|
89 |
|
90 |
|
91 |
// ***********************************************************************
|
92 |
|
93 |
BOOL WavChannel::Read(void *buf, PINDEX len)
|
94 |
{
|
95 |
if (!myConnection.IsEstablished())
|
96 |
{
|
97 |
PTRACE(2, "WavChannel::Read(): Connection not yet established");
|
98 |
memset(buf, 0, len);
|
99 |
lastReadCount = len;
|
100 |
readDelay.Delay(lastReadCount/2/8);
|
101 |
return true;
|
102 |
}
|
103 |
|
104 |
if (!wavFile.Read(buf, len))
|
105 |
return false;
|
106 |
|
107 |
lastReadCount = wavFile.GetLastReadCount();
|
108 |
readDelay.Delay(lastReadCount/2/8);
|
109 |
|
110 |
if (lastReadCount < len)
|
111 |
{
|
112 |
PTRACE(1, "WavChannel::Read(): end of file reached");
|
113 |
myConnection.ClearCall();
|
114 |
}
|
115 |
|
116 |
return lastReadCount > 0;
|
117 |
}
|
118 |
|
119 |
|
120 |
// ***********************************************************************
|
121 |
|