1 /*
2  * main.cxx
3  *
4  * PWLib application source file for vxmltest
5  *
6  * Main program entry point.
7  *
8  * Copyright 2002 Equivalence
9  *
10  * $Revision: 20385 $
11  * $Author: rjongbloed $
12  * $Date: 2008-06-04 05:40:38 -0500 (Wed, 04 Jun 2008) $
13  */
14 
15 #include <ptlib.h>
16 #include <ptlib/sound.h>
17 #include <ptclib/vxml.h>
18 
19 #if !P_EXPAT
20 #error Must have Expat XML support for this application
21 #endif
22 
23 
24 #include "main.h"
25 
26 
27 PCREATE_PROCESS(Vxmltest);
28 
29 #define BUFFER_SIZE 1024
30 
31 class ChannelCopyThread : public PThread
32 {
33   PCLASSINFO(ChannelCopyThread, PThread);
34   public:
ChannelCopyThread(PChannel & _from,PChannel & _to)35     ChannelCopyThread(PChannel & _from, PChannel & _to)
36       : PThread(1000, NoAutoDeleteThread), from(_from), to(_to)
37     { Resume(); }
38 
39     void Main();
40 
41   protected:
42     PChannel & from;
43     PChannel & to;
44 };
45 
Main()46 void ChannelCopyThread::Main()
47 {
48   for (;;) {
49 
50     from.SetReadTimeout(P_MAX_INDEX);
51     PBYTEArray readData;
52     if (!from.Read(readData.GetPointer(BUFFER_SIZE), 2)) {
53       PTRACE(2, "Read error 1");
54       break;
55     }
56     from.SetReadTimeout(0);
57     if (!from.Read(readData.GetPointer()+2, BUFFER_SIZE-2)) {
58       if (from.GetErrorCode(PChannel::LastReadError) != PChannel::Timeout) {
59         PTRACE(2, "Read error 2");
60         break;
61       }
62     }
63     readData.SetSize(from.GetLastReadCount()+2);
64 
65     if (readData.GetSize() > 0) {
66       if (!to.Write((const BYTE *)readData, readData.GetSize())) {
67         PTRACE(2, "Write error");
68         break;
69       }
70     }
71   }
72 }
73 
Vxmltest()74 Vxmltest::Vxmltest()
75   : PProcess("Equivalence", "vxmltest", 1, 0, AlphaCode, 1)
76 {
77 }
78 
79 
Main()80 void Vxmltest::Main()
81 {
82   PArgList & args = GetArguments();
83   args.Parse(
84              "t.-trace."
85              "o:output:"
86              "-tts:"
87              );
88 
89 #if PTRACING
90   PTrace::Initialise(args.GetOptionCount('t'),
91                      args.HasOption('o') ? (const char *)args.GetOptionString('o') : NULL,
92          PTrace::Blocks | PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine);
93 #endif
94 
95   if (args.GetCount() < 1) {
96     PError << "usage: vxmltest [opts] doc\n";
97     return;
98   }
99 
100   PTextToSpeech * tts = NULL;
101   PFactory<PTextToSpeech>::KeyList_T engines = PFactory<PTextToSpeech>::GetKeyList();
102   if (engines.size() != 0)
103     tts = PFactory<PTextToSpeech>::CreateInstance(engines[0]);
104   if (tts == NULL) {
105     PError << "error: cannot select default text to speech engine" << endl;
106     return;
107   }
108 
109   vxml = new PVXMLSession(tts);
110   PString device = PSoundChannel::GetDefaultDevice(PSoundChannel::Player);
111   PSoundChannel player;
112   if (!player.Open(device, PSoundChannel::Player)) {
113     PError << "error: cannot open sound device \"" << device << "\"" << endl;
114     return;
115   }
116   cout << "Using audio device \"" << device << "\"" << endl;
117 
118   if (!vxml->Load(args[0])) {
119     PError << "error: cannot loading VXML document \"" << args[0] << "\" - " << vxml->GetXMLError() << endl;
120     return;
121   }
122 
123   if (!vxml->Open(PTrue)) {
124     PError << "error: cannot open VXML device in PCM mode" << endl;
125     return;
126   }
127 
128   cout << "Starting media" << endl;
129   PThread * thread1 = new ChannelCopyThread(*vxml, player);
130 
131   inputRunning = PTrue;
132   PThread * inputThread = PThread::Create(PCREATE_NOTIFIER(InputThread), 0, NoAutoDeleteThread);
133 
134   thread1->WaitForTermination();
135 
136   inputRunning = PFalse;
137   cout << "Press a key to continue" << endl;
138   inputThread->WaitForTermination();
139 
140   cout << "Media finished" << endl;
141 }
142 
InputThread(PThread &,INT)143 void Vxmltest::InputThread(PThread &, INT)
144 {
145   PConsoleChannel console(PConsoleChannel::StandardInput);
146 
147   while (inputRunning) {
148     console.SetReadTimeout(100);
149     int ch = console.ReadChar();
150     if (ch > 0)
151       vxml->OnUserInput(PString((char)ch));
152   }
153 }
154 
155 
156 // End of File ///////////////////////////////////////////////////////////////
157